Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ALSA: Full duplex C example?

Tags:

c

linux

alsa

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

like image 831
Jens Avatar asked Mar 02 '11 22:03

Jens


2 Answers

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

  • http://www.alsa-project.org/main/index.php/Test_latency.c
like image 148
sdaau Avatar answered Sep 21 '22 08:09

sdaau


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.

like image 20
twobob Avatar answered Sep 19 '22 08:09

twobob