Unable to playback captured audio using USB sound card

Clash Royale CLAN TAG#URR8PPP
I am trying to capture and play audio using C program. For this I got this tutorial.. Here is the program which I am running:-
/**
* Jan Newmarch
*/
#define PERIOD_SIZE 1024
#define BUF_SIZE (PERIOD_SIZE * 2)
#include <stdio.h>
#include <stdlib.h>
#include <alsa/asoundlib.h>
void print_pcm_state(snd_pcm_t *handle, char *name)
switch (snd_pcm_state(handle))
case SND_PCM_STATE_OPEN:
printf("state open %sn", name);
break;
case SND_PCM_STATE_SETUP:
printf("state setup %sn", name);
break;
case SND_PCM_STATE_PREPARED:
printf("state prepare %sn", name);
break;
case SND_PCM_STATE_RUNNING:
printf("state running %sn", name);
break;
case SND_PCM_STATE_XRUN:
printf("state xrun %sn", name);
break;
default:
printf("state other %sn", name);
break;
int setparams(snd_pcm_t *handle, char *name)
snd_pcm_hw_params_t *hw_params;
int err;
if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0)
fprintf (stderr, "cannot allocate hardware parameter structure (%s)n",
snd_strerror (err));
exit (1);
if ((err = snd_pcm_hw_params_any (handle, hw_params)) < 0)
fprintf (stderr, "cannot initialize hardware parameter structure (%s)n",
snd_strerror (err));
exit (1);
if ((err = snd_pcm_hw_params_set_access (handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
fprintf (stderr, "cannot set access type (%s)n",
snd_strerror (err));
exit (1);
if ((err = snd_pcm_hw_params_set_format (handle, hw_params, SND_PCM_FORMAT_S16_LE)) < 0)
fprintf (stderr, "cannot set sample format (%s)n",
snd_strerror (err));
exit (1);
unsigned int rate = 48000;
if ((err = snd_pcm_hw_params_set_rate_near (handle, hw_params, &rate, 0)) < 0)
fprintf (stderr, "cannot set sample rate (%s)n",
snd_strerror (err));
exit (1);
printf("Rate for %s is %dn", name, rate);
if ((err = snd_pcm_hw_params_set_channels (handle, hw_params, 2)) < 0)
fprintf (stderr, "cannot set channel count (%s)n",
snd_strerror (err));
exit (1);
snd_pcm_uframes_t buffersize = BUF_SIZE;
if ((err = snd_pcm_hw_params_set_buffer_size_near(handle, hw_params, &buffersize)) < 0)
printf("Unable to set buffer size %li: %sn", BUF_SIZE, snd_strerror(err));
exit (1);;
snd_pcm_uframes_t periodsize = PERIOD_SIZE;
fprintf(stderr, "period size now %dn", periodsize);
if ((err = snd_pcm_hw_params_set_period_size_near(handle, hw_params, &periodsize, 0)) < 0)
printf("Unable to set period size %li: %sn", periodsize, snd_strerror(err));
exit (1);
if ((err = snd_pcm_hw_params (handle, hw_params)) < 0)
fprintf (stderr, "cannot set parameters (%s)n",
snd_strerror (err));
exit (1);
snd_pcm_uframes_t p_psize;
snd_pcm_hw_params_get_period_size(hw_params, &p_psize, NULL);
fprintf(stderr, "period size %dn", p_psize);
snd_pcm_hw_params_get_buffer_size(hw_params, &p_psize);
fprintf(stderr, "buffer size %dn", p_psize);
snd_pcm_hw_params_free (hw_params);
if ((err = snd_pcm_prepare (handle)) < 0)
fprintf (stderr, "cannot prepare audio interface for use (%s)n",
snd_strerror (err));
exit (1);
return 0;
int set_sw_params(snd_pcm_t *handle, char *name)
snd_pcm_sw_params_t *swparams;
int err;
snd_pcm_sw_params_alloca(&swparams);
err = snd_pcm_sw_params_current(handle, swparams);
if (err < 0)
fprintf(stderr, "Broken configuration for this PCM: no configurations availablen");
exit(1);
err = snd_pcm_sw_params_set_start_threshold(handle, swparams, PERIOD_SIZE);
if (err < 0)
printf("Unable to set start threshold: %sn", snd_strerror(err));
return err;
err = snd_pcm_sw_params_set_avail_min(handle, swparams, PERIOD_SIZE);
if (err < 0)
printf("Unable to set avail min: %sn", snd_strerror(err));
return err;
if (snd_pcm_sw_params(handle, swparams) < 0)
fprintf(stderr, "unable to install sw params:n");
exit(1);
return 0;
/************** some code from latency.c *****************/
main (int argc, char *argv)
int i;
int err;
int buf[BUF_SIZE];
snd_pcm_t *playback_handle;
snd_pcm_t *capture_handle;
snd_pcm_hw_params_t *hw_params;
FILE *fin;
size_t nread;
snd_pcm_format_t format = SND_PCM_FORMAT_S16_LE;
if (argc != 3)
fprintf(stderr, "Usage: %s in-card out-cardn", argv[0]);
exit(1);
/**** Out card *******/
if ((err = snd_pcm_open (&playback_handle, argv[2], SND_PCM_STREAM_PLAYBACK, 0)) < 0)
fprintf (stderr, "cannot open audio device %s (%s)n",
argv[2],
snd_strerror (err));
exit (1);
setparams(playback_handle, "playback");
set_sw_params(playback_handle, "playback");
/*********** In card **********/
if ((err = snd_pcm_open (&capture_handle, argv[1], SND_PCM_STREAM_CAPTURE, 0)) < 0)
fprintf (stderr, "cannot open audio device %s (%s)n",
argv[1],
snd_strerror (err));
exit (1);
setparams(capture_handle, "capture");
set_sw_params(capture_handle, "capture");
if ((err = snd_pcm_link(capture_handle, playback_handle)) < 0)
printf("Streams link error: %sn", snd_strerror(err));
exit(0);
if ((err = snd_pcm_prepare (playback_handle)) < 0)
fprintf (stderr, "cannot prepare playback audio interface for use (%s)n",
snd_strerror (err));
exit (1);
/**************** stuff something into the playback buffer ****************/
if (snd_pcm_format_set_silence(format, buf, 2*BUF_SIZE) < 0)
fprintf(stderr, "silence errorn");
exit(1);
int n = 0;
while (n++ < 2)
if (snd_pcm_writei (playback_handle, buf, BUF_SIZE) < 0)
fprintf(stderr, "write errorn");
exit(1);
/************* COPY ************/
while (1)
int nread;
if ((nread = snd_pcm_readi (capture_handle, buf, BUF_SIZE)) != BUF_SIZE)
if (nread < 0)
fprintf (stderr, "read from audio interface failed (%s)n",
snd_strerror (nread));
else
fprintf (stderr, "read from audio interface failed after %d framesn", nread);
snd_pcm_prepare(capture_handle);
continue;
if ((err = snd_pcm_writei (playback_handle, buf, nread)) != nread)
if (err < 0)
fprintf (stderr, "write to audio interface failed (%s)n",
snd_strerror (err));
else
fprintf (stderr, "write to audio interface failed after %d framesn", err);
snd_pcm_prepare(playback_handle);
snd_pcm_drain(playback_handle);
snd_pcm_close (playback_handle);
exit (0);
I compile it and run with following arguments:-
./playback-capture hw:0 hw:0
Till now my code is running fine, but now I decide to run this program using USB sound card. For this I edit
/etc/modprobe.d/alsa-base.conf
Here are the changes :-
I replace
options snd_usb_audio index=-2
options snd_hda_intel index=-1
with
options snd_usb_audio index=-1
options snd_hda_intel index=-2
And I replace
# Keep snd-usb-audio from beeing loaded as first soundcard
options snd-usb-audio index=-1
with
# Keep snd-usb-audio from beeing loaded as first soundcard
options snd-usb-audio index=-1
Now, when i run my code , I got this output:-
Rate for playback is 48000
period size now 1024
period size 1024
buffer size 2048
Rate for capture is 48000
cannot set channel count (Invalid argument)
So, can anyone tell me what else should I do to run my code using sound card.
Note:- I run command
aplay -l
and got this output:-
card 0: Device [USB PnP Sound Device], device 0: USB Audio [USB Audio]
Subdevices: 1/1
Subdevice #0: subdevice #0
card 1: PCH [HDA Intel PCH], device 0: ALC221 Analog [ALC221 Analog]
Subdevices: 1/1
Subdevice #0: subdevice #0
audio alsa usb-device usb-audio
add a comment |
I am trying to capture and play audio using C program. For this I got this tutorial.. Here is the program which I am running:-
/**
* Jan Newmarch
*/
#define PERIOD_SIZE 1024
#define BUF_SIZE (PERIOD_SIZE * 2)
#include <stdio.h>
#include <stdlib.h>
#include <alsa/asoundlib.h>
void print_pcm_state(snd_pcm_t *handle, char *name)
switch (snd_pcm_state(handle))
case SND_PCM_STATE_OPEN:
printf("state open %sn", name);
break;
case SND_PCM_STATE_SETUP:
printf("state setup %sn", name);
break;
case SND_PCM_STATE_PREPARED:
printf("state prepare %sn", name);
break;
case SND_PCM_STATE_RUNNING:
printf("state running %sn", name);
break;
case SND_PCM_STATE_XRUN:
printf("state xrun %sn", name);
break;
default:
printf("state other %sn", name);
break;
int setparams(snd_pcm_t *handle, char *name)
snd_pcm_hw_params_t *hw_params;
int err;
if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0)
fprintf (stderr, "cannot allocate hardware parameter structure (%s)n",
snd_strerror (err));
exit (1);
if ((err = snd_pcm_hw_params_any (handle, hw_params)) < 0)
fprintf (stderr, "cannot initialize hardware parameter structure (%s)n",
snd_strerror (err));
exit (1);
if ((err = snd_pcm_hw_params_set_access (handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
fprintf (stderr, "cannot set access type (%s)n",
snd_strerror (err));
exit (1);
if ((err = snd_pcm_hw_params_set_format (handle, hw_params, SND_PCM_FORMAT_S16_LE)) < 0)
fprintf (stderr, "cannot set sample format (%s)n",
snd_strerror (err));
exit (1);
unsigned int rate = 48000;
if ((err = snd_pcm_hw_params_set_rate_near (handle, hw_params, &rate, 0)) < 0)
fprintf (stderr, "cannot set sample rate (%s)n",
snd_strerror (err));
exit (1);
printf("Rate for %s is %dn", name, rate);
if ((err = snd_pcm_hw_params_set_channels (handle, hw_params, 2)) < 0)
fprintf (stderr, "cannot set channel count (%s)n",
snd_strerror (err));
exit (1);
snd_pcm_uframes_t buffersize = BUF_SIZE;
if ((err = snd_pcm_hw_params_set_buffer_size_near(handle, hw_params, &buffersize)) < 0)
printf("Unable to set buffer size %li: %sn", BUF_SIZE, snd_strerror(err));
exit (1);;
snd_pcm_uframes_t periodsize = PERIOD_SIZE;
fprintf(stderr, "period size now %dn", periodsize);
if ((err = snd_pcm_hw_params_set_period_size_near(handle, hw_params, &periodsize, 0)) < 0)
printf("Unable to set period size %li: %sn", periodsize, snd_strerror(err));
exit (1);
if ((err = snd_pcm_hw_params (handle, hw_params)) < 0)
fprintf (stderr, "cannot set parameters (%s)n",
snd_strerror (err));
exit (1);
snd_pcm_uframes_t p_psize;
snd_pcm_hw_params_get_period_size(hw_params, &p_psize, NULL);
fprintf(stderr, "period size %dn", p_psize);
snd_pcm_hw_params_get_buffer_size(hw_params, &p_psize);
fprintf(stderr, "buffer size %dn", p_psize);
snd_pcm_hw_params_free (hw_params);
if ((err = snd_pcm_prepare (handle)) < 0)
fprintf (stderr, "cannot prepare audio interface for use (%s)n",
snd_strerror (err));
exit (1);
return 0;
int set_sw_params(snd_pcm_t *handle, char *name)
snd_pcm_sw_params_t *swparams;
int err;
snd_pcm_sw_params_alloca(&swparams);
err = snd_pcm_sw_params_current(handle, swparams);
if (err < 0)
fprintf(stderr, "Broken configuration for this PCM: no configurations availablen");
exit(1);
err = snd_pcm_sw_params_set_start_threshold(handle, swparams, PERIOD_SIZE);
if (err < 0)
printf("Unable to set start threshold: %sn", snd_strerror(err));
return err;
err = snd_pcm_sw_params_set_avail_min(handle, swparams, PERIOD_SIZE);
if (err < 0)
printf("Unable to set avail min: %sn", snd_strerror(err));
return err;
if (snd_pcm_sw_params(handle, swparams) < 0)
fprintf(stderr, "unable to install sw params:n");
exit(1);
return 0;
/************** some code from latency.c *****************/
main (int argc, char *argv)
int i;
int err;
int buf[BUF_SIZE];
snd_pcm_t *playback_handle;
snd_pcm_t *capture_handle;
snd_pcm_hw_params_t *hw_params;
FILE *fin;
size_t nread;
snd_pcm_format_t format = SND_PCM_FORMAT_S16_LE;
if (argc != 3)
fprintf(stderr, "Usage: %s in-card out-cardn", argv[0]);
exit(1);
/**** Out card *******/
if ((err = snd_pcm_open (&playback_handle, argv[2], SND_PCM_STREAM_PLAYBACK, 0)) < 0)
fprintf (stderr, "cannot open audio device %s (%s)n",
argv[2],
snd_strerror (err));
exit (1);
setparams(playback_handle, "playback");
set_sw_params(playback_handle, "playback");
/*********** In card **********/
if ((err = snd_pcm_open (&capture_handle, argv[1], SND_PCM_STREAM_CAPTURE, 0)) < 0)
fprintf (stderr, "cannot open audio device %s (%s)n",
argv[1],
snd_strerror (err));
exit (1);
setparams(capture_handle, "capture");
set_sw_params(capture_handle, "capture");
if ((err = snd_pcm_link(capture_handle, playback_handle)) < 0)
printf("Streams link error: %sn", snd_strerror(err));
exit(0);
if ((err = snd_pcm_prepare (playback_handle)) < 0)
fprintf (stderr, "cannot prepare playback audio interface for use (%s)n",
snd_strerror (err));
exit (1);
/**************** stuff something into the playback buffer ****************/
if (snd_pcm_format_set_silence(format, buf, 2*BUF_SIZE) < 0)
fprintf(stderr, "silence errorn");
exit(1);
int n = 0;
while (n++ < 2)
if (snd_pcm_writei (playback_handle, buf, BUF_SIZE) < 0)
fprintf(stderr, "write errorn");
exit(1);
/************* COPY ************/
while (1)
int nread;
if ((nread = snd_pcm_readi (capture_handle, buf, BUF_SIZE)) != BUF_SIZE)
if (nread < 0)
fprintf (stderr, "read from audio interface failed (%s)n",
snd_strerror (nread));
else
fprintf (stderr, "read from audio interface failed after %d framesn", nread);
snd_pcm_prepare(capture_handle);
continue;
if ((err = snd_pcm_writei (playback_handle, buf, nread)) != nread)
if (err < 0)
fprintf (stderr, "write to audio interface failed (%s)n",
snd_strerror (err));
else
fprintf (stderr, "write to audio interface failed after %d framesn", err);
snd_pcm_prepare(playback_handle);
snd_pcm_drain(playback_handle);
snd_pcm_close (playback_handle);
exit (0);
I compile it and run with following arguments:-
./playback-capture hw:0 hw:0
Till now my code is running fine, but now I decide to run this program using USB sound card. For this I edit
/etc/modprobe.d/alsa-base.conf
Here are the changes :-
I replace
options snd_usb_audio index=-2
options snd_hda_intel index=-1
with
options snd_usb_audio index=-1
options snd_hda_intel index=-2
And I replace
# Keep snd-usb-audio from beeing loaded as first soundcard
options snd-usb-audio index=-1
with
# Keep snd-usb-audio from beeing loaded as first soundcard
options snd-usb-audio index=-1
Now, when i run my code , I got this output:-
Rate for playback is 48000
period size now 1024
period size 1024
buffer size 2048
Rate for capture is 48000
cannot set channel count (Invalid argument)
So, can anyone tell me what else should I do to run my code using sound card.
Note:- I run command
aplay -l
and got this output:-
card 0: Device [USB PnP Sound Device], device 0: USB Audio [USB Audio]
Subdevices: 1/1
Subdevice #0: subdevice #0
card 1: PCH [HDA Intel PCH], device 0: ALC221 Analog [ALC221 Analog]
Subdevices: 1/1
Subdevice #0: subdevice #0
audio alsa usb-device usb-audio
1
Apparently, your device does not support two channels. Tryplughwinstead ofhw.
– CL.
Apr 19 '16 at 12:04
I tried this command './playback-capture plughw:0 plughw:0' and get this output:- ALSA lib pcm_hw.c:1667:(_snd_pcm_hw_open) Invalid value for card cannot open audio device plughw:0 (No such file or directory)
– tabish
Apr 19 '16 at 12:07
@tabish run with sudo:sudo './playback-capture plughw:0 plughw:0
– EsmaeelE
Mar 9 at 18:41
add a comment |
I am trying to capture and play audio using C program. For this I got this tutorial.. Here is the program which I am running:-
/**
* Jan Newmarch
*/
#define PERIOD_SIZE 1024
#define BUF_SIZE (PERIOD_SIZE * 2)
#include <stdio.h>
#include <stdlib.h>
#include <alsa/asoundlib.h>
void print_pcm_state(snd_pcm_t *handle, char *name)
switch (snd_pcm_state(handle))
case SND_PCM_STATE_OPEN:
printf("state open %sn", name);
break;
case SND_PCM_STATE_SETUP:
printf("state setup %sn", name);
break;
case SND_PCM_STATE_PREPARED:
printf("state prepare %sn", name);
break;
case SND_PCM_STATE_RUNNING:
printf("state running %sn", name);
break;
case SND_PCM_STATE_XRUN:
printf("state xrun %sn", name);
break;
default:
printf("state other %sn", name);
break;
int setparams(snd_pcm_t *handle, char *name)
snd_pcm_hw_params_t *hw_params;
int err;
if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0)
fprintf (stderr, "cannot allocate hardware parameter structure (%s)n",
snd_strerror (err));
exit (1);
if ((err = snd_pcm_hw_params_any (handle, hw_params)) < 0)
fprintf (stderr, "cannot initialize hardware parameter structure (%s)n",
snd_strerror (err));
exit (1);
if ((err = snd_pcm_hw_params_set_access (handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
fprintf (stderr, "cannot set access type (%s)n",
snd_strerror (err));
exit (1);
if ((err = snd_pcm_hw_params_set_format (handle, hw_params, SND_PCM_FORMAT_S16_LE)) < 0)
fprintf (stderr, "cannot set sample format (%s)n",
snd_strerror (err));
exit (1);
unsigned int rate = 48000;
if ((err = snd_pcm_hw_params_set_rate_near (handle, hw_params, &rate, 0)) < 0)
fprintf (stderr, "cannot set sample rate (%s)n",
snd_strerror (err));
exit (1);
printf("Rate for %s is %dn", name, rate);
if ((err = snd_pcm_hw_params_set_channels (handle, hw_params, 2)) < 0)
fprintf (stderr, "cannot set channel count (%s)n",
snd_strerror (err));
exit (1);
snd_pcm_uframes_t buffersize = BUF_SIZE;
if ((err = snd_pcm_hw_params_set_buffer_size_near(handle, hw_params, &buffersize)) < 0)
printf("Unable to set buffer size %li: %sn", BUF_SIZE, snd_strerror(err));
exit (1);;
snd_pcm_uframes_t periodsize = PERIOD_SIZE;
fprintf(stderr, "period size now %dn", periodsize);
if ((err = snd_pcm_hw_params_set_period_size_near(handle, hw_params, &periodsize, 0)) < 0)
printf("Unable to set period size %li: %sn", periodsize, snd_strerror(err));
exit (1);
if ((err = snd_pcm_hw_params (handle, hw_params)) < 0)
fprintf (stderr, "cannot set parameters (%s)n",
snd_strerror (err));
exit (1);
snd_pcm_uframes_t p_psize;
snd_pcm_hw_params_get_period_size(hw_params, &p_psize, NULL);
fprintf(stderr, "period size %dn", p_psize);
snd_pcm_hw_params_get_buffer_size(hw_params, &p_psize);
fprintf(stderr, "buffer size %dn", p_psize);
snd_pcm_hw_params_free (hw_params);
if ((err = snd_pcm_prepare (handle)) < 0)
fprintf (stderr, "cannot prepare audio interface for use (%s)n",
snd_strerror (err));
exit (1);
return 0;
int set_sw_params(snd_pcm_t *handle, char *name)
snd_pcm_sw_params_t *swparams;
int err;
snd_pcm_sw_params_alloca(&swparams);
err = snd_pcm_sw_params_current(handle, swparams);
if (err < 0)
fprintf(stderr, "Broken configuration for this PCM: no configurations availablen");
exit(1);
err = snd_pcm_sw_params_set_start_threshold(handle, swparams, PERIOD_SIZE);
if (err < 0)
printf("Unable to set start threshold: %sn", snd_strerror(err));
return err;
err = snd_pcm_sw_params_set_avail_min(handle, swparams, PERIOD_SIZE);
if (err < 0)
printf("Unable to set avail min: %sn", snd_strerror(err));
return err;
if (snd_pcm_sw_params(handle, swparams) < 0)
fprintf(stderr, "unable to install sw params:n");
exit(1);
return 0;
/************** some code from latency.c *****************/
main (int argc, char *argv)
int i;
int err;
int buf[BUF_SIZE];
snd_pcm_t *playback_handle;
snd_pcm_t *capture_handle;
snd_pcm_hw_params_t *hw_params;
FILE *fin;
size_t nread;
snd_pcm_format_t format = SND_PCM_FORMAT_S16_LE;
if (argc != 3)
fprintf(stderr, "Usage: %s in-card out-cardn", argv[0]);
exit(1);
/**** Out card *******/
if ((err = snd_pcm_open (&playback_handle, argv[2], SND_PCM_STREAM_PLAYBACK, 0)) < 0)
fprintf (stderr, "cannot open audio device %s (%s)n",
argv[2],
snd_strerror (err));
exit (1);
setparams(playback_handle, "playback");
set_sw_params(playback_handle, "playback");
/*********** In card **********/
if ((err = snd_pcm_open (&capture_handle, argv[1], SND_PCM_STREAM_CAPTURE, 0)) < 0)
fprintf (stderr, "cannot open audio device %s (%s)n",
argv[1],
snd_strerror (err));
exit (1);
setparams(capture_handle, "capture");
set_sw_params(capture_handle, "capture");
if ((err = snd_pcm_link(capture_handle, playback_handle)) < 0)
printf("Streams link error: %sn", snd_strerror(err));
exit(0);
if ((err = snd_pcm_prepare (playback_handle)) < 0)
fprintf (stderr, "cannot prepare playback audio interface for use (%s)n",
snd_strerror (err));
exit (1);
/**************** stuff something into the playback buffer ****************/
if (snd_pcm_format_set_silence(format, buf, 2*BUF_SIZE) < 0)
fprintf(stderr, "silence errorn");
exit(1);
int n = 0;
while (n++ < 2)
if (snd_pcm_writei (playback_handle, buf, BUF_SIZE) < 0)
fprintf(stderr, "write errorn");
exit(1);
/************* COPY ************/
while (1)
int nread;
if ((nread = snd_pcm_readi (capture_handle, buf, BUF_SIZE)) != BUF_SIZE)
if (nread < 0)
fprintf (stderr, "read from audio interface failed (%s)n",
snd_strerror (nread));
else
fprintf (stderr, "read from audio interface failed after %d framesn", nread);
snd_pcm_prepare(capture_handle);
continue;
if ((err = snd_pcm_writei (playback_handle, buf, nread)) != nread)
if (err < 0)
fprintf (stderr, "write to audio interface failed (%s)n",
snd_strerror (err));
else
fprintf (stderr, "write to audio interface failed after %d framesn", err);
snd_pcm_prepare(playback_handle);
snd_pcm_drain(playback_handle);
snd_pcm_close (playback_handle);
exit (0);
I compile it and run with following arguments:-
./playback-capture hw:0 hw:0
Till now my code is running fine, but now I decide to run this program using USB sound card. For this I edit
/etc/modprobe.d/alsa-base.conf
Here are the changes :-
I replace
options snd_usb_audio index=-2
options snd_hda_intel index=-1
with
options snd_usb_audio index=-1
options snd_hda_intel index=-2
And I replace
# Keep snd-usb-audio from beeing loaded as first soundcard
options snd-usb-audio index=-1
with
# Keep snd-usb-audio from beeing loaded as first soundcard
options snd-usb-audio index=-1
Now, when i run my code , I got this output:-
Rate for playback is 48000
period size now 1024
period size 1024
buffer size 2048
Rate for capture is 48000
cannot set channel count (Invalid argument)
So, can anyone tell me what else should I do to run my code using sound card.
Note:- I run command
aplay -l
and got this output:-
card 0: Device [USB PnP Sound Device], device 0: USB Audio [USB Audio]
Subdevices: 1/1
Subdevice #0: subdevice #0
card 1: PCH [HDA Intel PCH], device 0: ALC221 Analog [ALC221 Analog]
Subdevices: 1/1
Subdevice #0: subdevice #0
audio alsa usb-device usb-audio
I am trying to capture and play audio using C program. For this I got this tutorial.. Here is the program which I am running:-
/**
* Jan Newmarch
*/
#define PERIOD_SIZE 1024
#define BUF_SIZE (PERIOD_SIZE * 2)
#include <stdio.h>
#include <stdlib.h>
#include <alsa/asoundlib.h>
void print_pcm_state(snd_pcm_t *handle, char *name)
switch (snd_pcm_state(handle))
case SND_PCM_STATE_OPEN:
printf("state open %sn", name);
break;
case SND_PCM_STATE_SETUP:
printf("state setup %sn", name);
break;
case SND_PCM_STATE_PREPARED:
printf("state prepare %sn", name);
break;
case SND_PCM_STATE_RUNNING:
printf("state running %sn", name);
break;
case SND_PCM_STATE_XRUN:
printf("state xrun %sn", name);
break;
default:
printf("state other %sn", name);
break;
int setparams(snd_pcm_t *handle, char *name)
snd_pcm_hw_params_t *hw_params;
int err;
if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0)
fprintf (stderr, "cannot allocate hardware parameter structure (%s)n",
snd_strerror (err));
exit (1);
if ((err = snd_pcm_hw_params_any (handle, hw_params)) < 0)
fprintf (stderr, "cannot initialize hardware parameter structure (%s)n",
snd_strerror (err));
exit (1);
if ((err = snd_pcm_hw_params_set_access (handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
fprintf (stderr, "cannot set access type (%s)n",
snd_strerror (err));
exit (1);
if ((err = snd_pcm_hw_params_set_format (handle, hw_params, SND_PCM_FORMAT_S16_LE)) < 0)
fprintf (stderr, "cannot set sample format (%s)n",
snd_strerror (err));
exit (1);
unsigned int rate = 48000;
if ((err = snd_pcm_hw_params_set_rate_near (handle, hw_params, &rate, 0)) < 0)
fprintf (stderr, "cannot set sample rate (%s)n",
snd_strerror (err));
exit (1);
printf("Rate for %s is %dn", name, rate);
if ((err = snd_pcm_hw_params_set_channels (handle, hw_params, 2)) < 0)
fprintf (stderr, "cannot set channel count (%s)n",
snd_strerror (err));
exit (1);
snd_pcm_uframes_t buffersize = BUF_SIZE;
if ((err = snd_pcm_hw_params_set_buffer_size_near(handle, hw_params, &buffersize)) < 0)
printf("Unable to set buffer size %li: %sn", BUF_SIZE, snd_strerror(err));
exit (1);;
snd_pcm_uframes_t periodsize = PERIOD_SIZE;
fprintf(stderr, "period size now %dn", periodsize);
if ((err = snd_pcm_hw_params_set_period_size_near(handle, hw_params, &periodsize, 0)) < 0)
printf("Unable to set period size %li: %sn", periodsize, snd_strerror(err));
exit (1);
if ((err = snd_pcm_hw_params (handle, hw_params)) < 0)
fprintf (stderr, "cannot set parameters (%s)n",
snd_strerror (err));
exit (1);
snd_pcm_uframes_t p_psize;
snd_pcm_hw_params_get_period_size(hw_params, &p_psize, NULL);
fprintf(stderr, "period size %dn", p_psize);
snd_pcm_hw_params_get_buffer_size(hw_params, &p_psize);
fprintf(stderr, "buffer size %dn", p_psize);
snd_pcm_hw_params_free (hw_params);
if ((err = snd_pcm_prepare (handle)) < 0)
fprintf (stderr, "cannot prepare audio interface for use (%s)n",
snd_strerror (err));
exit (1);
return 0;
int set_sw_params(snd_pcm_t *handle, char *name)
snd_pcm_sw_params_t *swparams;
int err;
snd_pcm_sw_params_alloca(&swparams);
err = snd_pcm_sw_params_current(handle, swparams);
if (err < 0)
fprintf(stderr, "Broken configuration for this PCM: no configurations availablen");
exit(1);
err = snd_pcm_sw_params_set_start_threshold(handle, swparams, PERIOD_SIZE);
if (err < 0)
printf("Unable to set start threshold: %sn", snd_strerror(err));
return err;
err = snd_pcm_sw_params_set_avail_min(handle, swparams, PERIOD_SIZE);
if (err < 0)
printf("Unable to set avail min: %sn", snd_strerror(err));
return err;
if (snd_pcm_sw_params(handle, swparams) < 0)
fprintf(stderr, "unable to install sw params:n");
exit(1);
return 0;
/************** some code from latency.c *****************/
main (int argc, char *argv)
int i;
int err;
int buf[BUF_SIZE];
snd_pcm_t *playback_handle;
snd_pcm_t *capture_handle;
snd_pcm_hw_params_t *hw_params;
FILE *fin;
size_t nread;
snd_pcm_format_t format = SND_PCM_FORMAT_S16_LE;
if (argc != 3)
fprintf(stderr, "Usage: %s in-card out-cardn", argv[0]);
exit(1);
/**** Out card *******/
if ((err = snd_pcm_open (&playback_handle, argv[2], SND_PCM_STREAM_PLAYBACK, 0)) < 0)
fprintf (stderr, "cannot open audio device %s (%s)n",
argv[2],
snd_strerror (err));
exit (1);
setparams(playback_handle, "playback");
set_sw_params(playback_handle, "playback");
/*********** In card **********/
if ((err = snd_pcm_open (&capture_handle, argv[1], SND_PCM_STREAM_CAPTURE, 0)) < 0)
fprintf (stderr, "cannot open audio device %s (%s)n",
argv[1],
snd_strerror (err));
exit (1);
setparams(capture_handle, "capture");
set_sw_params(capture_handle, "capture");
if ((err = snd_pcm_link(capture_handle, playback_handle)) < 0)
printf("Streams link error: %sn", snd_strerror(err));
exit(0);
if ((err = snd_pcm_prepare (playback_handle)) < 0)
fprintf (stderr, "cannot prepare playback audio interface for use (%s)n",
snd_strerror (err));
exit (1);
/**************** stuff something into the playback buffer ****************/
if (snd_pcm_format_set_silence(format, buf, 2*BUF_SIZE) < 0)
fprintf(stderr, "silence errorn");
exit(1);
int n = 0;
while (n++ < 2)
if (snd_pcm_writei (playback_handle, buf, BUF_SIZE) < 0)
fprintf(stderr, "write errorn");
exit(1);
/************* COPY ************/
while (1)
int nread;
if ((nread = snd_pcm_readi (capture_handle, buf, BUF_SIZE)) != BUF_SIZE)
if (nread < 0)
fprintf (stderr, "read from audio interface failed (%s)n",
snd_strerror (nread));
else
fprintf (stderr, "read from audio interface failed after %d framesn", nread);
snd_pcm_prepare(capture_handle);
continue;
if ((err = snd_pcm_writei (playback_handle, buf, nread)) != nread)
if (err < 0)
fprintf (stderr, "write to audio interface failed (%s)n",
snd_strerror (err));
else
fprintf (stderr, "write to audio interface failed after %d framesn", err);
snd_pcm_prepare(playback_handle);
snd_pcm_drain(playback_handle);
snd_pcm_close (playback_handle);
exit (0);
I compile it and run with following arguments:-
./playback-capture hw:0 hw:0
Till now my code is running fine, but now I decide to run this program using USB sound card. For this I edit
/etc/modprobe.d/alsa-base.conf
Here are the changes :-
I replace
options snd_usb_audio index=-2
options snd_hda_intel index=-1
with
options snd_usb_audio index=-1
options snd_hda_intel index=-2
And I replace
# Keep snd-usb-audio from beeing loaded as first soundcard
options snd-usb-audio index=-1
with
# Keep snd-usb-audio from beeing loaded as first soundcard
options snd-usb-audio index=-1
Now, when i run my code , I got this output:-
Rate for playback is 48000
period size now 1024
period size 1024
buffer size 2048
Rate for capture is 48000
cannot set channel count (Invalid argument)
So, can anyone tell me what else should I do to run my code using sound card.
Note:- I run command
aplay -l
and got this output:-
card 0: Device [USB PnP Sound Device], device 0: USB Audio [USB Audio]
Subdevices: 1/1
Subdevice #0: subdevice #0
card 1: PCH [HDA Intel PCH], device 0: ALC221 Analog [ALC221 Analog]
Subdevices: 1/1
Subdevice #0: subdevice #0
audio alsa usb-device usb-audio
audio alsa usb-device usb-audio
edited Apr 19 '16 at 12:09
tabish
asked Apr 19 '16 at 9:07
tabishtabish
1582314
1582314
1
Apparently, your device does not support two channels. Tryplughwinstead ofhw.
– CL.
Apr 19 '16 at 12:04
I tried this command './playback-capture plughw:0 plughw:0' and get this output:- ALSA lib pcm_hw.c:1667:(_snd_pcm_hw_open) Invalid value for card cannot open audio device plughw:0 (No such file or directory)
– tabish
Apr 19 '16 at 12:07
@tabish run with sudo:sudo './playback-capture plughw:0 plughw:0
– EsmaeelE
Mar 9 at 18:41
add a comment |
1
Apparently, your device does not support two channels. Tryplughwinstead ofhw.
– CL.
Apr 19 '16 at 12:04
I tried this command './playback-capture plughw:0 plughw:0' and get this output:- ALSA lib pcm_hw.c:1667:(_snd_pcm_hw_open) Invalid value for card cannot open audio device plughw:0 (No such file or directory)
– tabish
Apr 19 '16 at 12:07
@tabish run with sudo:sudo './playback-capture plughw:0 plughw:0
– EsmaeelE
Mar 9 at 18:41
1
1
Apparently, your device does not support two channels. Try
plughw instead of hw.– CL.
Apr 19 '16 at 12:04
Apparently, your device does not support two channels. Try
plughw instead of hw.– CL.
Apr 19 '16 at 12:04
I tried this command './playback-capture plughw:0 plughw:0' and get this output:- ALSA lib pcm_hw.c:1667:(_snd_pcm_hw_open) Invalid value for card cannot open audio device plughw:0 (No such file or directory)
– tabish
Apr 19 '16 at 12:07
I tried this command './playback-capture plughw:0 plughw:0' and get this output:- ALSA lib pcm_hw.c:1667:(_snd_pcm_hw_open) Invalid value for card cannot open audio device plughw:0 (No such file or directory)
– tabish
Apr 19 '16 at 12:07
@tabish run with sudo:
sudo './playback-capture plughw:0 plughw:0– EsmaeelE
Mar 9 at 18:41
@tabish run with sudo:
sudo './playback-capture plughw:0 plughw:0– EsmaeelE
Mar 9 at 18:41
add a comment |
1 Answer
1
active
oldest
votes
As a part of a project i want to have a Full Duplex
ALSA Advance Linux Sound Architecture example that can :
- Capture sound from a sound card thru microphone jack (pink).
- Play on other USB sound card output (Line-out green jack).
By performing some little change on your Code i can implement a running version of two-way ALSA Capture-play example.
Here is the code
Code
/**
* Jan Newmarch
*/
/*
* File name: rec-play-inline.c
*
* compile: gcc rec-play-inline.c -o rec-play-inline -lasound
*
* run: ./rec-play-inline "plughw:2,0" "plughw:0,0"
*
*
* */
#define PERIOD_SIZE 1024
#define BUF_SIZE (PERIOD_SIZE * 2)
#include <stdio.h>
#include <stdlib.h>
#include <alsa/asoundlib.h>
void print_pcm_state(snd_pcm_t *handle, char *name)
switch (snd_pcm_state(handle))
case SND_PCM_STATE_OPEN:
printf("state open %sn", name);
break;
case SND_PCM_STATE_SETUP:
printf("state setup %sn", name);
break;
case SND_PCM_STATE_PREPARED:
printf("state prepare %sn", name);
break;
case SND_PCM_STATE_RUNNING:
printf("state running %sn", name);
break;
case SND_PCM_STATE_XRUN:
printf("state xrun %sn", name);
break;
default:
printf("state other %sn", name);
break;
int setparams(snd_pcm_t *handle, char *name)
snd_pcm_hw_params_t *hw_params;
int err;
if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0)
fprintf (stderr, "cannot allocate hardware parameter structure (%s)n",
snd_strerror (err));
exit (1);
if ((err = snd_pcm_hw_params_any (handle, hw_params)) < 0)
fprintf (stderr, "cannot initialize hardware parameter structure (%s)n",
snd_strerror (err));
exit (1);
if ((err = snd_pcm_hw_params_set_access (handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
fprintf (stderr, "cannot set access type (%s)n",
snd_strerror (err));
exit (1);
if ((err = snd_pcm_hw_params_set_format (handle, hw_params, SND_PCM_FORMAT_S16_LE)) < 0)
fprintf (stderr, "cannot set sample format (%s)n",
snd_strerror (err));
exit (1);
unsigned int rate = 48000;
if ((err = snd_pcm_hw_params_set_rate_near (handle, hw_params, &rate, 0)) < 0)
fprintf (stderr, "cannot set sample rate (%s)n",
snd_strerror (err));
exit (1);
printf("Rate for %s is %dn", name, rate);
if ((err = snd_pcm_hw_params_set_channels (handle, hw_params, 2)) < 0)
fprintf (stderr, "cannot set channel count (%s)n",
snd_strerror (err));
exit (1);
snd_pcm_uframes_t buffersize = BUF_SIZE;
if ((err = snd_pcm_hw_params_set_buffer_size_near(handle, hw_params, &buffersize)) < 0)
printf("Unable to set buffer size %li: %sn", (long int)BUF_SIZE, snd_strerror(err));
exit (1);;
snd_pcm_uframes_t periodsize = PERIOD_SIZE;
fprintf(stderr, "period size now %dn", (int) periodsize);
if ((err = snd_pcm_hw_params_set_period_size_near(handle, hw_params, &periodsize, 0)) < 0)
printf("Unable to set period size %li: %sn", periodsize, snd_strerror(err));
exit (1);
if ((err = snd_pcm_hw_params (handle, hw_params)) < 0)
fprintf (stderr, "cannot set parameters (%s)n",
snd_strerror (err));
exit (1);
snd_pcm_uframes_t p_psize;
snd_pcm_hw_params_get_period_size(hw_params, &p_psize, NULL);
fprintf(stderr, "period size %dn", (int)p_psize);
snd_pcm_hw_params_get_buffer_size(hw_params, &p_psize);
fprintf(stderr, "buffer size %dn", (int)p_psize);
snd_pcm_hw_params_free (hw_params);
if ((err = snd_pcm_prepare (handle)) < 0)
fprintf (stderr, "cannot prepare audio interface for use (%s)n",
snd_strerror (err));
exit (1);
return 0;
int set_sw_params(snd_pcm_t *handle, char *name)
snd_pcm_sw_params_t *swparams;
int err;
snd_pcm_sw_params_alloca(&swparams);
err = snd_pcm_sw_params_current(handle, swparams);
if (err < 0)
fprintf(stderr, "Broken configuration for this PCM: no configurations availablen");
exit(1);
err = snd_pcm_sw_params_set_start_threshold(handle, swparams, PERIOD_SIZE);
if (err < 0)
printf("Unable to set start threshold: %sn", snd_strerror(err));
return err;
err = snd_pcm_sw_params_set_avail_min(handle, swparams, PERIOD_SIZE);
if (err < 0)
printf("Unable to set avail min: %sn", snd_strerror(err));
return err;
if (snd_pcm_sw_params(handle, swparams) < 0)
fprintf(stderr, "unable to install sw params:n");
exit(1);
return 0;
/************** some code from latency.c *****************/
int main (int argc, char *argv)
int i;
int err;
int buf[BUF_SIZE];
snd_pcm_t *playback_handle;
snd_pcm_t *capture_handle;
snd_pcm_hw_params_t *hw_params;
FILE *fin;
size_t nread;
snd_pcm_format_t format = SND_PCM_FORMAT_S16_LE;
///Check for command line arguments
if (argc != 3)
fprintf(stderr, "Usage: %s input-soundCard output-soundCardn", argv[0]);
exit(1);
/**** Out card *******/
if ((err = snd_pcm_open (&playback_handle, argv[1], SND_PCM_STREAM_PLAYBACK, 0)) < 0)
fprintf (stderr, "cannot open audio device %s (%s)n",
argv[2],
snd_strerror (err));
exit (1);
setparams(playback_handle, "playback");
set_sw_params(playback_handle, "playback");
/*********** In card **********/
if ((err = snd_pcm_open (&capture_handle, argv[2], SND_PCM_STREAM_CAPTURE, 0)) < 0)
fprintf (stderr, "cannot open audio device %s (%s)n",
argv[1],
snd_strerror (err));
exit (1);
setparams(capture_handle, "capture");
set_sw_params(capture_handle, "capture");
///Comment by EE
/*
if ((err = snd_pcm_link(capture_handle, playback_handle)) < 0)
printf("Streams link error: %sn", snd_strerror(err));
exit(0);
*/
if ((err = snd_pcm_prepare (playback_handle)) < 0)
fprintf (stderr, "cannot prepare playback audio interface for use (%s)n",
snd_strerror (err));
exit (1);
/**************** stuff something into the playback buffer ****************/
if (snd_pcm_format_set_silence(format, buf, 2*BUF_SIZE) < 0)
fprintf(stderr, "silence errorn");
exit(1);
///Comment by EE
/*
int n = 0;
while (n++ < 2)
if (snd_pcm_writei (playback_handle, buf, BUF_SIZE) < 0)
fprintf(stderr, "write errorn");
exit(1);
*/
///
/************* Capture and Play Voice ***************/
while (1)
int nread;
if ((nread = snd_pcm_readi (capture_handle, buf, BUF_SIZE)) != BUF_SIZE)
if (nread < 0)
fprintf (stderr, "read from audio interface failed (%s)n",
snd_strerror (nread));
else
fprintf (stderr, "read from audio interface failed after %d framesn", nread);
snd_pcm_prepare(capture_handle);
continue;
///added by EE
snd_pcm_prepare(playback_handle);
///
if ((err = snd_pcm_writei (playback_handle, buf, nread)) != nread)
if (err < 0)
fprintf (stderr, "write to audio interface failed (%s)n",
snd_strerror (err));
else
fprintf (stderr, "write to audio interface failed after %d framesn", err);
snd_pcm_prepare(playback_handle);
snd_pcm_drain(playback_handle);
snd_pcm_close (playback_handle);
exit (0);
return 0;
Explanation
My onboard sound card is : plughw:0,0 and USB attached external sound card is: plughw:2,0
I found out these name by running following commands:
- To have a list of play devices:
aplay --list--devices - To have a list of capture devices:
arecord --list-devices
[ToDo]
- Full comment the code to make it clearer.
- Improve code.
- Provide some useful Linux ALSA tutorial links.
Source
By adding snd_pcm_prepare(playback_handle); before call writei() resolve issue from
ALSA: buffer underrun on snd_pcm_writei call
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f277480%2funable-to-playback-captured-audio-using-usb-sound-card%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
As a part of a project i want to have a Full Duplex
ALSA Advance Linux Sound Architecture example that can :
- Capture sound from a sound card thru microphone jack (pink).
- Play on other USB sound card output (Line-out green jack).
By performing some little change on your Code i can implement a running version of two-way ALSA Capture-play example.
Here is the code
Code
/**
* Jan Newmarch
*/
/*
* File name: rec-play-inline.c
*
* compile: gcc rec-play-inline.c -o rec-play-inline -lasound
*
* run: ./rec-play-inline "plughw:2,0" "plughw:0,0"
*
*
* */
#define PERIOD_SIZE 1024
#define BUF_SIZE (PERIOD_SIZE * 2)
#include <stdio.h>
#include <stdlib.h>
#include <alsa/asoundlib.h>
void print_pcm_state(snd_pcm_t *handle, char *name)
switch (snd_pcm_state(handle))
case SND_PCM_STATE_OPEN:
printf("state open %sn", name);
break;
case SND_PCM_STATE_SETUP:
printf("state setup %sn", name);
break;
case SND_PCM_STATE_PREPARED:
printf("state prepare %sn", name);
break;
case SND_PCM_STATE_RUNNING:
printf("state running %sn", name);
break;
case SND_PCM_STATE_XRUN:
printf("state xrun %sn", name);
break;
default:
printf("state other %sn", name);
break;
int setparams(snd_pcm_t *handle, char *name)
snd_pcm_hw_params_t *hw_params;
int err;
if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0)
fprintf (stderr, "cannot allocate hardware parameter structure (%s)n",
snd_strerror (err));
exit (1);
if ((err = snd_pcm_hw_params_any (handle, hw_params)) < 0)
fprintf (stderr, "cannot initialize hardware parameter structure (%s)n",
snd_strerror (err));
exit (1);
if ((err = snd_pcm_hw_params_set_access (handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
fprintf (stderr, "cannot set access type (%s)n",
snd_strerror (err));
exit (1);
if ((err = snd_pcm_hw_params_set_format (handle, hw_params, SND_PCM_FORMAT_S16_LE)) < 0)
fprintf (stderr, "cannot set sample format (%s)n",
snd_strerror (err));
exit (1);
unsigned int rate = 48000;
if ((err = snd_pcm_hw_params_set_rate_near (handle, hw_params, &rate, 0)) < 0)
fprintf (stderr, "cannot set sample rate (%s)n",
snd_strerror (err));
exit (1);
printf("Rate for %s is %dn", name, rate);
if ((err = snd_pcm_hw_params_set_channels (handle, hw_params, 2)) < 0)
fprintf (stderr, "cannot set channel count (%s)n",
snd_strerror (err));
exit (1);
snd_pcm_uframes_t buffersize = BUF_SIZE;
if ((err = snd_pcm_hw_params_set_buffer_size_near(handle, hw_params, &buffersize)) < 0)
printf("Unable to set buffer size %li: %sn", (long int)BUF_SIZE, snd_strerror(err));
exit (1);;
snd_pcm_uframes_t periodsize = PERIOD_SIZE;
fprintf(stderr, "period size now %dn", (int) periodsize);
if ((err = snd_pcm_hw_params_set_period_size_near(handle, hw_params, &periodsize, 0)) < 0)
printf("Unable to set period size %li: %sn", periodsize, snd_strerror(err));
exit (1);
if ((err = snd_pcm_hw_params (handle, hw_params)) < 0)
fprintf (stderr, "cannot set parameters (%s)n",
snd_strerror (err));
exit (1);
snd_pcm_uframes_t p_psize;
snd_pcm_hw_params_get_period_size(hw_params, &p_psize, NULL);
fprintf(stderr, "period size %dn", (int)p_psize);
snd_pcm_hw_params_get_buffer_size(hw_params, &p_psize);
fprintf(stderr, "buffer size %dn", (int)p_psize);
snd_pcm_hw_params_free (hw_params);
if ((err = snd_pcm_prepare (handle)) < 0)
fprintf (stderr, "cannot prepare audio interface for use (%s)n",
snd_strerror (err));
exit (1);
return 0;
int set_sw_params(snd_pcm_t *handle, char *name)
snd_pcm_sw_params_t *swparams;
int err;
snd_pcm_sw_params_alloca(&swparams);
err = snd_pcm_sw_params_current(handle, swparams);
if (err < 0)
fprintf(stderr, "Broken configuration for this PCM: no configurations availablen");
exit(1);
err = snd_pcm_sw_params_set_start_threshold(handle, swparams, PERIOD_SIZE);
if (err < 0)
printf("Unable to set start threshold: %sn", snd_strerror(err));
return err;
err = snd_pcm_sw_params_set_avail_min(handle, swparams, PERIOD_SIZE);
if (err < 0)
printf("Unable to set avail min: %sn", snd_strerror(err));
return err;
if (snd_pcm_sw_params(handle, swparams) < 0)
fprintf(stderr, "unable to install sw params:n");
exit(1);
return 0;
/************** some code from latency.c *****************/
int main (int argc, char *argv)
int i;
int err;
int buf[BUF_SIZE];
snd_pcm_t *playback_handle;
snd_pcm_t *capture_handle;
snd_pcm_hw_params_t *hw_params;
FILE *fin;
size_t nread;
snd_pcm_format_t format = SND_PCM_FORMAT_S16_LE;
///Check for command line arguments
if (argc != 3)
fprintf(stderr, "Usage: %s input-soundCard output-soundCardn", argv[0]);
exit(1);
/**** Out card *******/
if ((err = snd_pcm_open (&playback_handle, argv[1], SND_PCM_STREAM_PLAYBACK, 0)) < 0)
fprintf (stderr, "cannot open audio device %s (%s)n",
argv[2],
snd_strerror (err));
exit (1);
setparams(playback_handle, "playback");
set_sw_params(playback_handle, "playback");
/*********** In card **********/
if ((err = snd_pcm_open (&capture_handle, argv[2], SND_PCM_STREAM_CAPTURE, 0)) < 0)
fprintf (stderr, "cannot open audio device %s (%s)n",
argv[1],
snd_strerror (err));
exit (1);
setparams(capture_handle, "capture");
set_sw_params(capture_handle, "capture");
///Comment by EE
/*
if ((err = snd_pcm_link(capture_handle, playback_handle)) < 0)
printf("Streams link error: %sn", snd_strerror(err));
exit(0);
*/
if ((err = snd_pcm_prepare (playback_handle)) < 0)
fprintf (stderr, "cannot prepare playback audio interface for use (%s)n",
snd_strerror (err));
exit (1);
/**************** stuff something into the playback buffer ****************/
if (snd_pcm_format_set_silence(format, buf, 2*BUF_SIZE) < 0)
fprintf(stderr, "silence errorn");
exit(1);
///Comment by EE
/*
int n = 0;
while (n++ < 2)
if (snd_pcm_writei (playback_handle, buf, BUF_SIZE) < 0)
fprintf(stderr, "write errorn");
exit(1);
*/
///
/************* Capture and Play Voice ***************/
while (1)
int nread;
if ((nread = snd_pcm_readi (capture_handle, buf, BUF_SIZE)) != BUF_SIZE)
if (nread < 0)
fprintf (stderr, "read from audio interface failed (%s)n",
snd_strerror (nread));
else
fprintf (stderr, "read from audio interface failed after %d framesn", nread);
snd_pcm_prepare(capture_handle);
continue;
///added by EE
snd_pcm_prepare(playback_handle);
///
if ((err = snd_pcm_writei (playback_handle, buf, nread)) != nread)
if (err < 0)
fprintf (stderr, "write to audio interface failed (%s)n",
snd_strerror (err));
else
fprintf (stderr, "write to audio interface failed after %d framesn", err);
snd_pcm_prepare(playback_handle);
snd_pcm_drain(playback_handle);
snd_pcm_close (playback_handle);
exit (0);
return 0;
Explanation
My onboard sound card is : plughw:0,0 and USB attached external sound card is: plughw:2,0
I found out these name by running following commands:
- To have a list of play devices:
aplay --list--devices - To have a list of capture devices:
arecord --list-devices
[ToDo]
- Full comment the code to make it clearer.
- Improve code.
- Provide some useful Linux ALSA tutorial links.
Source
By adding snd_pcm_prepare(playback_handle); before call writei() resolve issue from
ALSA: buffer underrun on snd_pcm_writei call
add a comment |
As a part of a project i want to have a Full Duplex
ALSA Advance Linux Sound Architecture example that can :
- Capture sound from a sound card thru microphone jack (pink).
- Play on other USB sound card output (Line-out green jack).
By performing some little change on your Code i can implement a running version of two-way ALSA Capture-play example.
Here is the code
Code
/**
* Jan Newmarch
*/
/*
* File name: rec-play-inline.c
*
* compile: gcc rec-play-inline.c -o rec-play-inline -lasound
*
* run: ./rec-play-inline "plughw:2,0" "plughw:0,0"
*
*
* */
#define PERIOD_SIZE 1024
#define BUF_SIZE (PERIOD_SIZE * 2)
#include <stdio.h>
#include <stdlib.h>
#include <alsa/asoundlib.h>
void print_pcm_state(snd_pcm_t *handle, char *name)
switch (snd_pcm_state(handle))
case SND_PCM_STATE_OPEN:
printf("state open %sn", name);
break;
case SND_PCM_STATE_SETUP:
printf("state setup %sn", name);
break;
case SND_PCM_STATE_PREPARED:
printf("state prepare %sn", name);
break;
case SND_PCM_STATE_RUNNING:
printf("state running %sn", name);
break;
case SND_PCM_STATE_XRUN:
printf("state xrun %sn", name);
break;
default:
printf("state other %sn", name);
break;
int setparams(snd_pcm_t *handle, char *name)
snd_pcm_hw_params_t *hw_params;
int err;
if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0)
fprintf (stderr, "cannot allocate hardware parameter structure (%s)n",
snd_strerror (err));
exit (1);
if ((err = snd_pcm_hw_params_any (handle, hw_params)) < 0)
fprintf (stderr, "cannot initialize hardware parameter structure (%s)n",
snd_strerror (err));
exit (1);
if ((err = snd_pcm_hw_params_set_access (handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
fprintf (stderr, "cannot set access type (%s)n",
snd_strerror (err));
exit (1);
if ((err = snd_pcm_hw_params_set_format (handle, hw_params, SND_PCM_FORMAT_S16_LE)) < 0)
fprintf (stderr, "cannot set sample format (%s)n",
snd_strerror (err));
exit (1);
unsigned int rate = 48000;
if ((err = snd_pcm_hw_params_set_rate_near (handle, hw_params, &rate, 0)) < 0)
fprintf (stderr, "cannot set sample rate (%s)n",
snd_strerror (err));
exit (1);
printf("Rate for %s is %dn", name, rate);
if ((err = snd_pcm_hw_params_set_channels (handle, hw_params, 2)) < 0)
fprintf (stderr, "cannot set channel count (%s)n",
snd_strerror (err));
exit (1);
snd_pcm_uframes_t buffersize = BUF_SIZE;
if ((err = snd_pcm_hw_params_set_buffer_size_near(handle, hw_params, &buffersize)) < 0)
printf("Unable to set buffer size %li: %sn", (long int)BUF_SIZE, snd_strerror(err));
exit (1);;
snd_pcm_uframes_t periodsize = PERIOD_SIZE;
fprintf(stderr, "period size now %dn", (int) periodsize);
if ((err = snd_pcm_hw_params_set_period_size_near(handle, hw_params, &periodsize, 0)) < 0)
printf("Unable to set period size %li: %sn", periodsize, snd_strerror(err));
exit (1);
if ((err = snd_pcm_hw_params (handle, hw_params)) < 0)
fprintf (stderr, "cannot set parameters (%s)n",
snd_strerror (err));
exit (1);
snd_pcm_uframes_t p_psize;
snd_pcm_hw_params_get_period_size(hw_params, &p_psize, NULL);
fprintf(stderr, "period size %dn", (int)p_psize);
snd_pcm_hw_params_get_buffer_size(hw_params, &p_psize);
fprintf(stderr, "buffer size %dn", (int)p_psize);
snd_pcm_hw_params_free (hw_params);
if ((err = snd_pcm_prepare (handle)) < 0)
fprintf (stderr, "cannot prepare audio interface for use (%s)n",
snd_strerror (err));
exit (1);
return 0;
int set_sw_params(snd_pcm_t *handle, char *name)
snd_pcm_sw_params_t *swparams;
int err;
snd_pcm_sw_params_alloca(&swparams);
err = snd_pcm_sw_params_current(handle, swparams);
if (err < 0)
fprintf(stderr, "Broken configuration for this PCM: no configurations availablen");
exit(1);
err = snd_pcm_sw_params_set_start_threshold(handle, swparams, PERIOD_SIZE);
if (err < 0)
printf("Unable to set start threshold: %sn", snd_strerror(err));
return err;
err = snd_pcm_sw_params_set_avail_min(handle, swparams, PERIOD_SIZE);
if (err < 0)
printf("Unable to set avail min: %sn", snd_strerror(err));
return err;
if (snd_pcm_sw_params(handle, swparams) < 0)
fprintf(stderr, "unable to install sw params:n");
exit(1);
return 0;
/************** some code from latency.c *****************/
int main (int argc, char *argv)
int i;
int err;
int buf[BUF_SIZE];
snd_pcm_t *playback_handle;
snd_pcm_t *capture_handle;
snd_pcm_hw_params_t *hw_params;
FILE *fin;
size_t nread;
snd_pcm_format_t format = SND_PCM_FORMAT_S16_LE;
///Check for command line arguments
if (argc != 3)
fprintf(stderr, "Usage: %s input-soundCard output-soundCardn", argv[0]);
exit(1);
/**** Out card *******/
if ((err = snd_pcm_open (&playback_handle, argv[1], SND_PCM_STREAM_PLAYBACK, 0)) < 0)
fprintf (stderr, "cannot open audio device %s (%s)n",
argv[2],
snd_strerror (err));
exit (1);
setparams(playback_handle, "playback");
set_sw_params(playback_handle, "playback");
/*********** In card **********/
if ((err = snd_pcm_open (&capture_handle, argv[2], SND_PCM_STREAM_CAPTURE, 0)) < 0)
fprintf (stderr, "cannot open audio device %s (%s)n",
argv[1],
snd_strerror (err));
exit (1);
setparams(capture_handle, "capture");
set_sw_params(capture_handle, "capture");
///Comment by EE
/*
if ((err = snd_pcm_link(capture_handle, playback_handle)) < 0)
printf("Streams link error: %sn", snd_strerror(err));
exit(0);
*/
if ((err = snd_pcm_prepare (playback_handle)) < 0)
fprintf (stderr, "cannot prepare playback audio interface for use (%s)n",
snd_strerror (err));
exit (1);
/**************** stuff something into the playback buffer ****************/
if (snd_pcm_format_set_silence(format, buf, 2*BUF_SIZE) < 0)
fprintf(stderr, "silence errorn");
exit(1);
///Comment by EE
/*
int n = 0;
while (n++ < 2)
if (snd_pcm_writei (playback_handle, buf, BUF_SIZE) < 0)
fprintf(stderr, "write errorn");
exit(1);
*/
///
/************* Capture and Play Voice ***************/
while (1)
int nread;
if ((nread = snd_pcm_readi (capture_handle, buf, BUF_SIZE)) != BUF_SIZE)
if (nread < 0)
fprintf (stderr, "read from audio interface failed (%s)n",
snd_strerror (nread));
else
fprintf (stderr, "read from audio interface failed after %d framesn", nread);
snd_pcm_prepare(capture_handle);
continue;
///added by EE
snd_pcm_prepare(playback_handle);
///
if ((err = snd_pcm_writei (playback_handle, buf, nread)) != nread)
if (err < 0)
fprintf (stderr, "write to audio interface failed (%s)n",
snd_strerror (err));
else
fprintf (stderr, "write to audio interface failed after %d framesn", err);
snd_pcm_prepare(playback_handle);
snd_pcm_drain(playback_handle);
snd_pcm_close (playback_handle);
exit (0);
return 0;
Explanation
My onboard sound card is : plughw:0,0 and USB attached external sound card is: plughw:2,0
I found out these name by running following commands:
- To have a list of play devices:
aplay --list--devices - To have a list of capture devices:
arecord --list-devices
[ToDo]
- Full comment the code to make it clearer.
- Improve code.
- Provide some useful Linux ALSA tutorial links.
Source
By adding snd_pcm_prepare(playback_handle); before call writei() resolve issue from
ALSA: buffer underrun on snd_pcm_writei call
add a comment |
As a part of a project i want to have a Full Duplex
ALSA Advance Linux Sound Architecture example that can :
- Capture sound from a sound card thru microphone jack (pink).
- Play on other USB sound card output (Line-out green jack).
By performing some little change on your Code i can implement a running version of two-way ALSA Capture-play example.
Here is the code
Code
/**
* Jan Newmarch
*/
/*
* File name: rec-play-inline.c
*
* compile: gcc rec-play-inline.c -o rec-play-inline -lasound
*
* run: ./rec-play-inline "plughw:2,0" "plughw:0,0"
*
*
* */
#define PERIOD_SIZE 1024
#define BUF_SIZE (PERIOD_SIZE * 2)
#include <stdio.h>
#include <stdlib.h>
#include <alsa/asoundlib.h>
void print_pcm_state(snd_pcm_t *handle, char *name)
switch (snd_pcm_state(handle))
case SND_PCM_STATE_OPEN:
printf("state open %sn", name);
break;
case SND_PCM_STATE_SETUP:
printf("state setup %sn", name);
break;
case SND_PCM_STATE_PREPARED:
printf("state prepare %sn", name);
break;
case SND_PCM_STATE_RUNNING:
printf("state running %sn", name);
break;
case SND_PCM_STATE_XRUN:
printf("state xrun %sn", name);
break;
default:
printf("state other %sn", name);
break;
int setparams(snd_pcm_t *handle, char *name)
snd_pcm_hw_params_t *hw_params;
int err;
if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0)
fprintf (stderr, "cannot allocate hardware parameter structure (%s)n",
snd_strerror (err));
exit (1);
if ((err = snd_pcm_hw_params_any (handle, hw_params)) < 0)
fprintf (stderr, "cannot initialize hardware parameter structure (%s)n",
snd_strerror (err));
exit (1);
if ((err = snd_pcm_hw_params_set_access (handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
fprintf (stderr, "cannot set access type (%s)n",
snd_strerror (err));
exit (1);
if ((err = snd_pcm_hw_params_set_format (handle, hw_params, SND_PCM_FORMAT_S16_LE)) < 0)
fprintf (stderr, "cannot set sample format (%s)n",
snd_strerror (err));
exit (1);
unsigned int rate = 48000;
if ((err = snd_pcm_hw_params_set_rate_near (handle, hw_params, &rate, 0)) < 0)
fprintf (stderr, "cannot set sample rate (%s)n",
snd_strerror (err));
exit (1);
printf("Rate for %s is %dn", name, rate);
if ((err = snd_pcm_hw_params_set_channels (handle, hw_params, 2)) < 0)
fprintf (stderr, "cannot set channel count (%s)n",
snd_strerror (err));
exit (1);
snd_pcm_uframes_t buffersize = BUF_SIZE;
if ((err = snd_pcm_hw_params_set_buffer_size_near(handle, hw_params, &buffersize)) < 0)
printf("Unable to set buffer size %li: %sn", (long int)BUF_SIZE, snd_strerror(err));
exit (1);;
snd_pcm_uframes_t periodsize = PERIOD_SIZE;
fprintf(stderr, "period size now %dn", (int) periodsize);
if ((err = snd_pcm_hw_params_set_period_size_near(handle, hw_params, &periodsize, 0)) < 0)
printf("Unable to set period size %li: %sn", periodsize, snd_strerror(err));
exit (1);
if ((err = snd_pcm_hw_params (handle, hw_params)) < 0)
fprintf (stderr, "cannot set parameters (%s)n",
snd_strerror (err));
exit (1);
snd_pcm_uframes_t p_psize;
snd_pcm_hw_params_get_period_size(hw_params, &p_psize, NULL);
fprintf(stderr, "period size %dn", (int)p_psize);
snd_pcm_hw_params_get_buffer_size(hw_params, &p_psize);
fprintf(stderr, "buffer size %dn", (int)p_psize);
snd_pcm_hw_params_free (hw_params);
if ((err = snd_pcm_prepare (handle)) < 0)
fprintf (stderr, "cannot prepare audio interface for use (%s)n",
snd_strerror (err));
exit (1);
return 0;
int set_sw_params(snd_pcm_t *handle, char *name)
snd_pcm_sw_params_t *swparams;
int err;
snd_pcm_sw_params_alloca(&swparams);
err = snd_pcm_sw_params_current(handle, swparams);
if (err < 0)
fprintf(stderr, "Broken configuration for this PCM: no configurations availablen");
exit(1);
err = snd_pcm_sw_params_set_start_threshold(handle, swparams, PERIOD_SIZE);
if (err < 0)
printf("Unable to set start threshold: %sn", snd_strerror(err));
return err;
err = snd_pcm_sw_params_set_avail_min(handle, swparams, PERIOD_SIZE);
if (err < 0)
printf("Unable to set avail min: %sn", snd_strerror(err));
return err;
if (snd_pcm_sw_params(handle, swparams) < 0)
fprintf(stderr, "unable to install sw params:n");
exit(1);
return 0;
/************** some code from latency.c *****************/
int main (int argc, char *argv)
int i;
int err;
int buf[BUF_SIZE];
snd_pcm_t *playback_handle;
snd_pcm_t *capture_handle;
snd_pcm_hw_params_t *hw_params;
FILE *fin;
size_t nread;
snd_pcm_format_t format = SND_PCM_FORMAT_S16_LE;
///Check for command line arguments
if (argc != 3)
fprintf(stderr, "Usage: %s input-soundCard output-soundCardn", argv[0]);
exit(1);
/**** Out card *******/
if ((err = snd_pcm_open (&playback_handle, argv[1], SND_PCM_STREAM_PLAYBACK, 0)) < 0)
fprintf (stderr, "cannot open audio device %s (%s)n",
argv[2],
snd_strerror (err));
exit (1);
setparams(playback_handle, "playback");
set_sw_params(playback_handle, "playback");
/*********** In card **********/
if ((err = snd_pcm_open (&capture_handle, argv[2], SND_PCM_STREAM_CAPTURE, 0)) < 0)
fprintf (stderr, "cannot open audio device %s (%s)n",
argv[1],
snd_strerror (err));
exit (1);
setparams(capture_handle, "capture");
set_sw_params(capture_handle, "capture");
///Comment by EE
/*
if ((err = snd_pcm_link(capture_handle, playback_handle)) < 0)
printf("Streams link error: %sn", snd_strerror(err));
exit(0);
*/
if ((err = snd_pcm_prepare (playback_handle)) < 0)
fprintf (stderr, "cannot prepare playback audio interface for use (%s)n",
snd_strerror (err));
exit (1);
/**************** stuff something into the playback buffer ****************/
if (snd_pcm_format_set_silence(format, buf, 2*BUF_SIZE) < 0)
fprintf(stderr, "silence errorn");
exit(1);
///Comment by EE
/*
int n = 0;
while (n++ < 2)
if (snd_pcm_writei (playback_handle, buf, BUF_SIZE) < 0)
fprintf(stderr, "write errorn");
exit(1);
*/
///
/************* Capture and Play Voice ***************/
while (1)
int nread;
if ((nread = snd_pcm_readi (capture_handle, buf, BUF_SIZE)) != BUF_SIZE)
if (nread < 0)
fprintf (stderr, "read from audio interface failed (%s)n",
snd_strerror (nread));
else
fprintf (stderr, "read from audio interface failed after %d framesn", nread);
snd_pcm_prepare(capture_handle);
continue;
///added by EE
snd_pcm_prepare(playback_handle);
///
if ((err = snd_pcm_writei (playback_handle, buf, nread)) != nread)
if (err < 0)
fprintf (stderr, "write to audio interface failed (%s)n",
snd_strerror (err));
else
fprintf (stderr, "write to audio interface failed after %d framesn", err);
snd_pcm_prepare(playback_handle);
snd_pcm_drain(playback_handle);
snd_pcm_close (playback_handle);
exit (0);
return 0;
Explanation
My onboard sound card is : plughw:0,0 and USB attached external sound card is: plughw:2,0
I found out these name by running following commands:
- To have a list of play devices:
aplay --list--devices - To have a list of capture devices:
arecord --list-devices
[ToDo]
- Full comment the code to make it clearer.
- Improve code.
- Provide some useful Linux ALSA tutorial links.
Source
By adding snd_pcm_prepare(playback_handle); before call writei() resolve issue from
ALSA: buffer underrun on snd_pcm_writei call
As a part of a project i want to have a Full Duplex
ALSA Advance Linux Sound Architecture example that can :
- Capture sound from a sound card thru microphone jack (pink).
- Play on other USB sound card output (Line-out green jack).
By performing some little change on your Code i can implement a running version of two-way ALSA Capture-play example.
Here is the code
Code
/**
* Jan Newmarch
*/
/*
* File name: rec-play-inline.c
*
* compile: gcc rec-play-inline.c -o rec-play-inline -lasound
*
* run: ./rec-play-inline "plughw:2,0" "plughw:0,0"
*
*
* */
#define PERIOD_SIZE 1024
#define BUF_SIZE (PERIOD_SIZE * 2)
#include <stdio.h>
#include <stdlib.h>
#include <alsa/asoundlib.h>
void print_pcm_state(snd_pcm_t *handle, char *name)
switch (snd_pcm_state(handle))
case SND_PCM_STATE_OPEN:
printf("state open %sn", name);
break;
case SND_PCM_STATE_SETUP:
printf("state setup %sn", name);
break;
case SND_PCM_STATE_PREPARED:
printf("state prepare %sn", name);
break;
case SND_PCM_STATE_RUNNING:
printf("state running %sn", name);
break;
case SND_PCM_STATE_XRUN:
printf("state xrun %sn", name);
break;
default:
printf("state other %sn", name);
break;
int setparams(snd_pcm_t *handle, char *name)
snd_pcm_hw_params_t *hw_params;
int err;
if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0)
fprintf (stderr, "cannot allocate hardware parameter structure (%s)n",
snd_strerror (err));
exit (1);
if ((err = snd_pcm_hw_params_any (handle, hw_params)) < 0)
fprintf (stderr, "cannot initialize hardware parameter structure (%s)n",
snd_strerror (err));
exit (1);
if ((err = snd_pcm_hw_params_set_access (handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
fprintf (stderr, "cannot set access type (%s)n",
snd_strerror (err));
exit (1);
if ((err = snd_pcm_hw_params_set_format (handle, hw_params, SND_PCM_FORMAT_S16_LE)) < 0)
fprintf (stderr, "cannot set sample format (%s)n",
snd_strerror (err));
exit (1);
unsigned int rate = 48000;
if ((err = snd_pcm_hw_params_set_rate_near (handle, hw_params, &rate, 0)) < 0)
fprintf (stderr, "cannot set sample rate (%s)n",
snd_strerror (err));
exit (1);
printf("Rate for %s is %dn", name, rate);
if ((err = snd_pcm_hw_params_set_channels (handle, hw_params, 2)) < 0)
fprintf (stderr, "cannot set channel count (%s)n",
snd_strerror (err));
exit (1);
snd_pcm_uframes_t buffersize = BUF_SIZE;
if ((err = snd_pcm_hw_params_set_buffer_size_near(handle, hw_params, &buffersize)) < 0)
printf("Unable to set buffer size %li: %sn", (long int)BUF_SIZE, snd_strerror(err));
exit (1);;
snd_pcm_uframes_t periodsize = PERIOD_SIZE;
fprintf(stderr, "period size now %dn", (int) periodsize);
if ((err = snd_pcm_hw_params_set_period_size_near(handle, hw_params, &periodsize, 0)) < 0)
printf("Unable to set period size %li: %sn", periodsize, snd_strerror(err));
exit (1);
if ((err = snd_pcm_hw_params (handle, hw_params)) < 0)
fprintf (stderr, "cannot set parameters (%s)n",
snd_strerror (err));
exit (1);
snd_pcm_uframes_t p_psize;
snd_pcm_hw_params_get_period_size(hw_params, &p_psize, NULL);
fprintf(stderr, "period size %dn", (int)p_psize);
snd_pcm_hw_params_get_buffer_size(hw_params, &p_psize);
fprintf(stderr, "buffer size %dn", (int)p_psize);
snd_pcm_hw_params_free (hw_params);
if ((err = snd_pcm_prepare (handle)) < 0)
fprintf (stderr, "cannot prepare audio interface for use (%s)n",
snd_strerror (err));
exit (1);
return 0;
int set_sw_params(snd_pcm_t *handle, char *name)
snd_pcm_sw_params_t *swparams;
int err;
snd_pcm_sw_params_alloca(&swparams);
err = snd_pcm_sw_params_current(handle, swparams);
if (err < 0)
fprintf(stderr, "Broken configuration for this PCM: no configurations availablen");
exit(1);
err = snd_pcm_sw_params_set_start_threshold(handle, swparams, PERIOD_SIZE);
if (err < 0)
printf("Unable to set start threshold: %sn", snd_strerror(err));
return err;
err = snd_pcm_sw_params_set_avail_min(handle, swparams, PERIOD_SIZE);
if (err < 0)
printf("Unable to set avail min: %sn", snd_strerror(err));
return err;
if (snd_pcm_sw_params(handle, swparams) < 0)
fprintf(stderr, "unable to install sw params:n");
exit(1);
return 0;
/************** some code from latency.c *****************/
int main (int argc, char *argv)
int i;
int err;
int buf[BUF_SIZE];
snd_pcm_t *playback_handle;
snd_pcm_t *capture_handle;
snd_pcm_hw_params_t *hw_params;
FILE *fin;
size_t nread;
snd_pcm_format_t format = SND_PCM_FORMAT_S16_LE;
///Check for command line arguments
if (argc != 3)
fprintf(stderr, "Usage: %s input-soundCard output-soundCardn", argv[0]);
exit(1);
/**** Out card *******/
if ((err = snd_pcm_open (&playback_handle, argv[1], SND_PCM_STREAM_PLAYBACK, 0)) < 0)
fprintf (stderr, "cannot open audio device %s (%s)n",
argv[2],
snd_strerror (err));
exit (1);
setparams(playback_handle, "playback");
set_sw_params(playback_handle, "playback");
/*********** In card **********/
if ((err = snd_pcm_open (&capture_handle, argv[2], SND_PCM_STREAM_CAPTURE, 0)) < 0)
fprintf (stderr, "cannot open audio device %s (%s)n",
argv[1],
snd_strerror (err));
exit (1);
setparams(capture_handle, "capture");
set_sw_params(capture_handle, "capture");
///Comment by EE
/*
if ((err = snd_pcm_link(capture_handle, playback_handle)) < 0)
printf("Streams link error: %sn", snd_strerror(err));
exit(0);
*/
if ((err = snd_pcm_prepare (playback_handle)) < 0)
fprintf (stderr, "cannot prepare playback audio interface for use (%s)n",
snd_strerror (err));
exit (1);
/**************** stuff something into the playback buffer ****************/
if (snd_pcm_format_set_silence(format, buf, 2*BUF_SIZE) < 0)
fprintf(stderr, "silence errorn");
exit(1);
///Comment by EE
/*
int n = 0;
while (n++ < 2)
if (snd_pcm_writei (playback_handle, buf, BUF_SIZE) < 0)
fprintf(stderr, "write errorn");
exit(1);
*/
///
/************* Capture and Play Voice ***************/
while (1)
int nread;
if ((nread = snd_pcm_readi (capture_handle, buf, BUF_SIZE)) != BUF_SIZE)
if (nread < 0)
fprintf (stderr, "read from audio interface failed (%s)n",
snd_strerror (nread));
else
fprintf (stderr, "read from audio interface failed after %d framesn", nread);
snd_pcm_prepare(capture_handle);
continue;
///added by EE
snd_pcm_prepare(playback_handle);
///
if ((err = snd_pcm_writei (playback_handle, buf, nread)) != nread)
if (err < 0)
fprintf (stderr, "write to audio interface failed (%s)n",
snd_strerror (err));
else
fprintf (stderr, "write to audio interface failed after %d framesn", err);
snd_pcm_prepare(playback_handle);
snd_pcm_drain(playback_handle);
snd_pcm_close (playback_handle);
exit (0);
return 0;
Explanation
My onboard sound card is : plughw:0,0 and USB attached external sound card is: plughw:2,0
I found out these name by running following commands:
- To have a list of play devices:
aplay --list--devices - To have a list of capture devices:
arecord --list-devices
[ToDo]
- Full comment the code to make it clearer.
- Improve code.
- Provide some useful Linux ALSA tutorial links.
Source
By adding snd_pcm_prepare(playback_handle); before call writei() resolve issue from
ALSA: buffer underrun on snd_pcm_writei call
edited Mar 9 at 18:39
answered Mar 5 at 15:39
EsmaeelEEsmaeelE
1013
1013
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f277480%2funable-to-playback-captured-audio-using-usb-sound-card%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Apparently, your device does not support two channels. Try
plughwinstead ofhw.– CL.
Apr 19 '16 at 12:04
I tried this command './playback-capture plughw:0 plughw:0' and get this output:- ALSA lib pcm_hw.c:1667:(_snd_pcm_hw_open) Invalid value for card cannot open audio device plughw:0 (No such file or directory)
– tabish
Apr 19 '16 at 12:07
@tabish run with sudo:
sudo './playback-capture plughw:0 plughw:0– EsmaeelE
Mar 9 at 18:41