I ported an old Linux audio program, a simple Voice-over-IP & stereo demuxing application, from
OSS to
ALSA.
First impression of ALSA libasound : the documentation sucks, well, no, the documentation really sucks monkey rods ! I wonder why the wannabe 1st Linux audio API have such a pathetic API doc, example :
snd_pcm_hw_params_set_period_time_near. And this is not an exception, the whole API go such poor doxygen comments. Very frustating for someone who is maintaining a library, and made a huge effort on javadocing lazy coder code.
About the API itself, well.. it's looking a bit over-complex near OSS, but it's no rocket science once you found the good examples for your poll based full duplex program :
speexclient. But once it was ported the perfs are awesome, far of OSS and far of Windows sound system. I was able to achieve 5ms latency with a 8 inputs/8 outputs audio card, without using a rt patched kernel, nor huge optimization.
Well it's a pity you got a such technically impressive audio system ruined by poor doc & API.. no wonder why we have Arts, ESD, Pulseaudio, Jackd (the guys who thinks everybody have floating point CPU).
My version of speexclient, transformed for being a simple fullduplex passthru audio program (I removed all the speex encoding/decoding part and the network code for simplicity).
You still need speex for it's very good jitter buffer and alsa_device.c (can be found of speex GIT repository)
/***************************************************************************
Copyright (C) 2004-2006 by Jean-Marc Valin
Copyright (C) 2006 Commonwealth Scientific and Industrial Research
Organisation (CSIRO) Australia
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
- Neither the name of the Xiph.org Foundation nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
****************************************************************************/
#ifdef HAVE_CONFIG_H
#include
#endif
#include
#include
#include
#include
#include
#include
#include
#include /* close() */
#include /* memset() */
#include "alsa_device.h"
#include
#include
#define MAX_MSG 1500
#define SAMPLING_RATE 16000
#define FRAME_SIZE 320
int main(int argc, char *argv[]) {
int nfds;
struct pollfd *pfds;
AlsaDevice *audio_dev;
JitterBuffer * jitterBuffer = jitter_buffer_init(FRAME_SIZE * 2);
int jitterTime = 0;
/* Setup audio device, forced to pughw for avoiding pulseaudio */
audio_dev = alsa_device_open("plughw:0,0", SAMPLING_RATE, 1, FRAME_SIZE);
struct sched_param param;
/*param.sched_priority = 40; */
param.sched_priority = sched_get_priority_min(SCHED_FIFO);
if (sched_setscheduler(0, SCHED_FIFO, ¶m))
perror("sched_setscheduler");
/* Setup all file descriptors for poll()ing */
nfds = alsa_device_nfds(audio_dev);
pfds = malloc(sizeof(*pfds) * (nfds));
alsa_device_getfds(audio_dev, pfds, nfds);
alsa_device_start(audio_dev);
/* Infinite loop on capture, playback */
while (1) {
/* Wait for either 1) capture 2) playback */
poll(pfds, nfds, -1);
/* Ready to play a frame (playback) */
if (alsa_device_playback_ready(audio_dev, pfds, nfds)) {
short pcm[FRAME_SIZE];
/* Get audio from the jitter buffer */
JitterBufferPacket p;
p.data = (char *)pcm;
p.len = FRAME_SIZE * 2;
// eat a buffer
int res=jitter_buffer_get(jitterBuffer,&p,FRAME_SIZE * 2,NULL);
if(res != JITTER_BUFFER_OK ) {
memset(pcm,0,FRAME_SIZE*2);
}
jitter_buffer_tick(jitterBuffer);
/* Playback the audio and reset the echo canceller if we got an underrun */
if (alsa_device_write(audio_dev, pcm, FRAME_SIZE)) {
printf("underrun\n");
}
}
/* Audio available from the soundcard (capture) */
if (alsa_device_capture_ready(audio_dev, pfds, nfds)) {
short pcm[FRAME_SIZE];
/* Get audio from the soundcard */
alsa_device_read(audio_dev, pcm, FRAME_SIZE);
JitterBufferPacket p;
p.data = (char*)(pcm);
p.len = FRAME_SIZE*2;
jitterTime += FRAME_SIZE*2;
p.timestamp = jitterTime;
p.span = FRAME_SIZE*2;
jitter_buffer_put(jitterBuffer, &p);
}
}
return 0;
}