I can nicely output a sine wave with the RPI using ALSA as long as the samples are 1 channel, 8 bit. When I try 16 bit or 2 channels it isn't pretty.
Here are the 8 bit details:
if ((err = snd_pcm_set_params(handle, SND_PCM_FORMAT_U8, SND_PCM_ACCESS_RW_INTERLEAVED, 1, 48000, 1, 100000 ) ) < 0)
{
unsigned char buffer [ 96 ]; /* samples to play */
int tempint = 255 * sin ( baseVal [ keyPlaying ] * keyPos );
buffer[innr] = tempint & 0xff;
Here is my best guess (doesn't work) at 16 bit, one channel:
if ((err = snd_pcm_set_params(handle, SND_PCM_FORMAT_U16_LE, SND_PCM_ACCESS_RW_INTERLEAVED, 1, 48000, 1, 100000 ) ) < 0)
unsigned char buffer [ 2 * 96 ]; /* samples to play */
int tempint = 37763 * sin ( baseVal [ keyPlaying ] * keyPos );
buffer[innr + 1] = ( tempint % 256 ) & 0xff;
buffer[innr] = ( tempint / 256 ) & 0xff;
Anyone see what I am doing wrong?
Additionally, to play silence in 8 bit (works great) I use:
for ( innr = 0; innr < sizeof ( buffer ); innr++ )
{
buffer[innr] = 0 & 0xff;
}
But in 16 bit there is a tone output:
for ( innr = 0; innr < sizeof ( buffer ); innr++ )
{
buffer[innr] = 0 & 0xff;
}
The ALSA site has tons of info, but I think I need "ALSA for Dummies"
Mark