I am trying to build a brain device using EEG input and outputting light pulses and binaural beats in "close to real-time" on a Raspberry PI. The light-output is no problem using WiringPi, but any audio output seems to be a major hurdle. The math of calculating a sinewave for a buffer is straightforward, but playing two frequencies on two chaels via any standard libraries seems to be a very complicated process, and I can't come up with any relevant examples. I have successfully opened and closed an ALSA device thanks to this tutorial which complicates my fairly simple code tremendously but appears necessary for ALSA. I would be incredibly grateful if someone could show me the easiest method for playing two different calculated tones on left and right chaels. The code below is the simplest ALSA playback example I could find.
#include <alsa/asoundlib.h>
#include <iostream>
using namespace std;
// Globals are generally a bad idea in code. We're using one here to keep it simple.
snd_pcm_t * _soundDevice;
bool Init(const char *name)
{
int i;
int err;
snd_pcm_hw_params_t *hw_params;
if( name == NULL )
{
// Try to open the default device
err = snd_pcm_open( &_soundDevice, "plughw:0,0", SND_PCM_STREAM_PLAYBACK, 0 );
}
else
{
// Open the device we were told to open.
err = snd_pcm_open (&_soundDevice, name, SND_PCM_STREAM_PLAYBACK, 0);
}
// Check for error on open.
if( err < 0 )
{
cout << "Init: caot open audio device " << name << " (" << snd_strerror (err) << ")" << endl;
retu false;
}
else
{
cout << "Audio device opened successfully." << endl;
}
// Allocate the hardware parameter structure.
if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0)
{
cout << "Init: caot allocate hardware parameter structure (" << snd_strerror (err) << ")" << endl;
retu false;
}
if ((err = snd_pcm_hw_params_any (_soundDevice, hw_params)) < 0)
{
cout << "Init: caot initialize hardware parameter structure (" << snd_strerror (err) << ")" << endl;
retu false;
}
// Enable resampling.
unsigned int resample = 1;
err = snd_pcm_hw_params_set_rate_resample(_soundDevice, hw_params, resample);
if (err < 0)
{
cout << "Init: Resampling setup failed for playback: " << snd_strerror(err) << endl;
retu err;
}
// Set access to RW interleaved.
if ((err = snd_pcm_hw_params_set_access (_soundDevice, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
{
cout << "Init: caot set access type (" << snd_strerror (err) << ")" << endl;
retu false;
}
if ((err = snd_pcm_hw_params_set_format (_soundDevice, hw_params, SND_PCM_FORMAT_S16_LE)) < 0)
{
cout << "Init: caot set sample format (" << snd_strerror (err) << ")" << endl;
retu false;
}
// Set chaels to stereo (2).
if ((err = snd_pcm_hw_params_set_chaels (_soundDevice, hw_params, 2)) < 0)
{
cout << "Init: caot set chael count (" << snd_strerror (err) << ")" << endl;
retu false;
}
// Set sample rate.
unsigned int actualRate = 44100;
if ((err = snd_pcm_hw_params_set_rate_near (_soundDevice, hw_params, &actualRate, 0)) < 0)
{
cout << "Init: caot set sample rate to 44100. (" << snd_strerror (err) << ")" << endl;
retu false;
}
if( actualRate < 44100 )
{
cout << "Init: sample rate does not match requested rate. (" << "44100 requested, " << actualRate << " acquired)" << endl;
}
// Apply the hardware parameters that we've set.
if ((err = snd_pcm_hw_params (_soundDevice, hw_params)) < 0)
{
cout << "Init: caot set parameters (" << snd_strerror (err) << ")" << endl;
retu false;
}
else
{
cout << "Audio device parameters have been set successfully." << endl;
}
// Get the buffer size.
snd_pcm_uframes_t bufferSize;
snd_pcm_hw_params_get_buffer_size( hw_params, &bufferSize );
// If we were going to do more with our sound device we would want to store
// the buffer size so we know how much data we will need to fill it with.
cout << "Init: Buffer size = " << bufferSize << " frames." << endl;
// Display the bit size of samples.
cout << "Init: Significant bits for linear samples = " << snd_pcm_hw_params_get_sbits(hw_params) << endl;
// Free the hardware parameters now that we're done with them.
snd_pcm_hw_params_free (hw_params);
// Prepare interface for use.
if ((err = snd_pcm_prepare (_soundDevice)) < 0)
{
cout << "Init: caot prepare audio interface for use (" << snd_strerror (err) << ")" << endl;
retu false;
}
else
{
cout << "Audio device has been prepared for use." << endl;
}
retu true;
}
bool UnInit()
{
snd_pcm_close (_soundDevice);
cout << "Audio device has been uninitialized." << endl;
retu true;
}
int main( char *argc, int argv )
{
Init(NULL);
UnInit();
retu 0;
}
