Non-blocking PCM playback stream ... noisy
Hi,
I have an app (a soft phone) which is currently using OSS and i want to implement Alsa ... sounds better than OSS.
Thanks in advance for the replies.
Actually, i already have it "running" with alsa, but in blocking mode. The problem with blocking is that i need the playback consumer thread to read every 20ms ... but as it blocks for that much time, add the processing time ... it turns out that i end up not being able to catch up the producer thread. Apart from this "small" detail, audio sounds and with very good quality.
So what i did is try to use non-block io for the playback only (record is still blocking, is it correct to do so?).
The problem is that in non-blocking, i get the audio stream but with a disgusting noisy background ... kind of a
fixed white noise ...
Here is an abstraction of the code:
--------------------------------------------------------------------------------------------------------write_to_card( void * buff, int frames) {
//make sure that 20 ms have elapsed between this call and the previous // sleep for some miliseconds if needed
short * frameBuff = (short *) buff; //my samples are 2-byte each ... shorts int totalFramesWritten = 0;
while( totalFramesWritten < frames ) {
written = snd_pcm_writei( writeHandle,
& frameBuff[ totalFramesWritten * 2], //advance the buffer ... frames * 2 chans = num samples
frames - totalFramesWritten );
if( written == -EAGAIN || written == -EINTR )
continue; // non blocking ...
if( written < 0 )
return -1; //error
else
totalFramesWritten += written;
}
}
--------------------------------------------------------------------------------------------------------
The timing part works fine ...
This same code structure (the while loop and so ) works fine with blocking audio ... but then,
when using blocking io, all the frames are written at once ... but when using non-blocking, the while
needs a few iterations (i have 960 frames ... it writes a big chunck, say 832, then in groups of 16 frames).
Could this have something to do? can i do something (alsa params) so all the frames can be written at once?
Any idea on why the noise? what am i doing wrong?
Tks a lot!
Regards,
Cesc