ALSA: Full duplex C example? ALSA: Full duplex C example? linux linux

ALSA: Full duplex C example?


Some guy named Alan has published this good (but old) tutorial, Full Duplex ALSA, which is written in C.


You provide a link to both handles and pump them in turn.Here's alan's code elided and commented.

// the device plughw handle dynamic sample rate and type conversion.// there are a range of alternate devices defined in your alsa.conf// try: // locate alsa.conf// and check out what devices you have in there//  // The following device is PLUG:HW:Device:0:Subdevice:0// Often simply plug, plughw, plughw:0, will have the same effect //char           *snd_device_in  = "plughw:0,0";char           *snd_device_out = "plughw:0,0";// handle constructs to populate with our linkssnd_pcm_t      *playback_handle;snd_pcm_t      *capture_handle;//this is the usual construct... If not fail BLAHif ((err = snd_pcm_open(&playback_handle, snd_device_out, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {fprintf(stderr, "cannot open output audio device %s: %s\n", snd_device_in, snd_strerror(err));exit(1);}// And now the CAPTUREif ((err = snd_pcm_open(&capture_handle, snd_device_in, SND_PCM_STREAM_CAPTURE, 0)) < 0) {fprintf(stderr, "cannot open input audio device %s: %s\n", snd_device_out, snd_strerror(err));exit(1);}

then config and pump them.

A ring mod could do the job: http://soundprogramming.net/programming_and_apis/creating_a_ring_buffer or you could use alans way outlined above.


It was my first requirements to a Linux/Unix VoIP projects where I need to know about all of the available audio devices capability and name. Then I need to use these devices to capture and playback the audio.

For everyone's help I have made a (.so) library and a sample Application demonstrating the use of this library in c++.

The output of my library is like-

[root@~]# ./IdeaAudioEngineTestHDA Intel plughw:0,0HDA Intel plughw:0,2USB Audio Device plughw:1,0

The library provides functionality to capture and playback real-time audio data.

Full source with documentation is available in IdeaAudio library with Duplex Alsa Audio

Library source is now open at github.com