ALSA: Full duplex C example?
Asked Answered
A

4

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

Anzac answered 2/3, 2011 at 22:25 Comment(2)
+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...Noleta
I write an answer to this question at: unix.stackexchange.com/a/504526/235261 an attempt to provide a full duplex alsa.Grizzle
M
6

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

Menadione answered 9/3, 2011 at 2:15 Comment(0)
M
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.

Masson answered 9/7, 2012 at 21:24 Comment(1)
A FIFO-stack with some predetermined item-limit would be simpler and safer than a ring-buffer.Faxan
G
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

Granger answered 4/8, 2013 at 20:22 Comment(0)
A
2

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

Arnst answered 18/9, 2013 at 6:3 Comment(1)
don't understand the downvote - the example you link is supposed to utilize full-duplex acc. to it's documentationFaxan

© 2022 - 2024 — McMap. All rights reserved.