12

is there an example of a full-duplex ALSA connection in C? I've read that it is supported, but all the introductory examples I saw did either record or play a sound sample, but I'd like to have one handler that can do both for my VoIP-app.

Big thanks for help, Jens

2
  • 3
    +1 because I'd like to know the answer too. I'd also be interested in knowing, if you use the OSS api for an ALSA device, whether opening it O_RDWR is sufficient to get full duplex, or if you need to use the ugly old-style OSS full-duplex setup ioctl stuff, or if it even works at all... Mar 3, 2011 at 0:57
  • I write an answer to this question at: unix.stackexchange.com/a/504526/235261 an attempt to provide a full duplex alsa.
    – EsmaeelE
    Mar 5, 2019 at 17:48

4 Answers 4

6

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

3

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 links
snd_pcm_t      *playback_handle;
snd_pcm_t      *capture_handle;

//this is the usual construct... If not fail BLAH
if ((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 CAPTURE
if ((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.

1
  • 1
    A FIFO-stack with some predetermined item-limit would be simpler and safer than a ring-buffer.
    – slashmais
    Aug 28, 2015 at 8:30
3

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@~]# ./IdeaAudioEngineTest
HDA Intel plughw:0,0
HDA Intel plughw:0,2
USB 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

2

See also latency.c, included in alsa-lib source; on the ALSA wiki:

1
  • 2
    don't understand the downvote - the example you link is supposed to utilize full-duplex acc. to it's documentation
    – slashmais
    Sep 1, 2015 at 7:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.