Skip to content

Instantly share code, notes, and snippets.

@ferryzhou
Created February 22, 2012 20:18
Show Gist options
  • Save ferryzhou/1886976 to your computer and use it in GitHub Desktop.
Save ferryzhou/1886976 to your computer and use it in GitHub Desktop.
A Minimal ALSA Loopback Program
#include <stdio.h>
#include <stdlib.h>
#include <alsa/asoundlib.h>
#define BUF_BYTES 128
int main (int argc, char *argv[]) {
int err;
unsigned char buf[BUF_BYTES];
snd_pcm_t *playback_handle;
snd_pcm_t *capture_handle;
char* device = "default";
if (argc > 1) device = argv[1];
unsigned int rate = 8000;
unsigned int nchannels = 2;
snd_pcm_format_t format = SND_PCM_FORMAT_S16_LE;
unsigned int buf_frames = BUF_BYTES / nchannels / 2;
if ((err = snd_pcm_open (&playback_handle, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
fprintf (stderr, "cannot open audio device %s (%s)\n", device, snd_strerror (err)); exit (1);
}
if ((err = snd_pcm_set_params(playback_handle, format, SND_PCM_ACCESS_RW_INTERLEAVED, nchannels, rate, 1, 500000)) < 0) { /* 0.5sec */
fprintf(stderr, "Playback open error: %s\n", snd_strerror(err)); exit(1);
}
if ((err = snd_pcm_open (&capture_handle, device, SND_PCM_STREAM_CAPTURE, 0)) < 0) {
fprintf (stderr, "cannot open audio device %s (%s)\n", device, snd_strerror (err)); exit (1);
}
if ((err = snd_pcm_set_params(capture_handle, format, SND_PCM_ACCESS_RW_INTERLEAVED, nchannels, rate, 1, 500000)) < 0) { /* 0.5sec */
fprintf(stderr, "capture open error: %s\n", snd_strerror(err)); exit(1);
}
while(1) {
if ((err = snd_pcm_readi (capture_handle, buf, buf_frames)) != buf_frames) {
fprintf (stderr, "read from audio interface failed (%s)\n", snd_strerror (err)); exit (1);
}
if ((err = snd_pcm_writei (playback_handle, buf, buf_frames)) != buf_frames) {
fprintf (stderr, "write to audio interface failed (%s)\n", snd_strerror (err)); exit (1);
}
}
fprintf (stderr, "close handles\n");
snd_pcm_close (playback_handle);
snd_pcm_close (capture_handle);
return 0;
}
@b23prodtm
Copy link

There is the alsaloop linux cmd line why don't use it? Thereby making adjustable synchronisation and quality requires errors checking.

alsaloop -C hw:0,0 -P hw:1,0 -t 50000-A0 -S5 

The issue is that are you able to decrease the pcmtime to a value of 10000? ~approx 10ms on Pi

@nospam2k
Copy link

There is the alsaloop linux cmd line why don't use it? Thereby making adjustable synchronisation and quality requires errors checking.

alsaloop -C hw:0,0 -P hw:1,0 -t 50000-A0 -S5 

The issue is that are you able to decrease the pcmtime to a value of 10000? ~approx 10ms on Pi

I explained this in my post. I'm getting buffer underruns that cause me to have to restart alsaloop

@b23prodtm
Copy link

Its about tweaking the sample rate converter and sync, there are two parameters -A 5 and -S 5 that if set to automatic will improve the performance of alsaloop. This a raspberrypi3 A with 512 MB and QUAD CORE ARM get correct stream line with ~10-30ms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment