-
-
Save ferryzhou/1886976 to your computer and use it in GitHub Desktop.
#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; | |
} |
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
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
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
I'm using this on a raspberry pi. I am using a USB mic and looping to hdmi out. I'm getting "read from audio interface failed (Input/output error)". I was getting buffer underruns from alsaloop and thought I would try this as I have to keep restarting alsaloop once the buffer underruns start. I tried changing buffer size but that doesn't seem to work. What would be the best way to address this issue. I'm trying to do live audio from the pi to another room that can hear the original audio so I have to keep the latency as low as possible. I'm not too worried about the occasional pop or click, I just need to balance keeping the stream going with minimal latency and as clean as possible. My USB mic requires 44100 and 1 channel as well.