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

Jens
  • 121
  • 1
  • 1
  • 3
  • 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... – R.. GitHub STOP HELPING ICE Mar 03 '11 at 00:57
  • I write an answer to this question at: https://unix.stackexchange.com/a/504526/235261 an attempt to provide a full duplex alsa. – EsmaeelE Mar 05 '19 at 17:48

4 Answers4

6

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

Laurent Parenteau
  • 2,516
  • 20
  • 31
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.

twobob
  • 354
  • 8
  • 22
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

Robel Sharma
  • 964
  • 1
  • 11
  • 26
2

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

sdaau
  • 36,975
  • 46
  • 198
  • 278
  • 2
    don't understand the downvote - the example you link is supposed to utilize full-duplex acc. to it's documentation – slashmais Sep 01 '15 at 07:41