Alsa broken pipe (example code)
- From: kamilpe@xxxxxxxxx
- Date: 4 Sep 2006 19:53:27 -0700
Hi, now i working on capture application but i have problem with alsa
library. Sometimes work corretly, sometimes i got "broken pipe". I am
not sure this line:
snd_pcm_hw_params_set_buffer_size (capture_handle, hw_params,
(8192*2)>>2)
Because i do not know what is period and periodsize, buffer size can be
wrong.
If someone have tips what can be wrong, please help.
This is capture example (sorry if its too long but can be directly
paste and compile:
gcc record.c `pkg-config --libs alsa` `pkg-config --cflags alsa` -g
-ggdb -o record)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <alsa/asoundlib.h>
#define BUF_LEN 512
int recording = 1;
struct wav_header {
char chunk_id[4];
unsigned int chunk_size;
char format[4];
char subchunk1_id[4];
unsigned int subchunk1_size;
unsigned short int audio_format;
unsigned short int channels;
unsigned int sample_rate;
unsigned int byte_range;
unsigned short int block_align;
unsigned short int bits_per_sample;
char subchunk2_id[4];
unsigned int subchunk2_size;
};
void catch_signal(int signal)
{
if (signal == SIGINT)
recording = 0;
}
int main()
{
struct wav_header header;
FILE *output;
int i;
int err;
/* int give me four bytes per frame */
int buf[BUF_LEN];
snd_pcm_t *capture_handle;
snd_pcm_hw_params_t *hw_params;
/* init wave header */
memcpy(header.chunk_id,"RIFF",4);
header.chunk_size = 0;
memcpy(header.format, "WAVE",4);
memcpy(header.subchunk1_id, "fmt ", 4);
header.subchunk1_size = 16;
header.audio_format = 1;
header.channels = 2;
header.sample_rate = 44100;
header.bits_per_sample = 16;
header.block_align = header.channels * header.bits_per_sample/8;
header.byte_range = header.sample_rate * header.channels *
header.bits_per_sample/8;
memcpy(header.subchunk2_id, "data", 4);
header.subchunk2_size = 0;
output = fopen("record.wav","w+");
fwrite(&header, sizeof(struct wav_header), 1, output);
/* initialise alsa */
if ((err = snd_pcm_open (&capture_handle, "plughw:0,0",
SND_PCM_STREAM_CAPTURE, 0)) < 0) {
fprintf (stderr, "cannot open audio device plughw:0,0 (%s)\n",
snd_strerror (err));
exit (1);
}
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 (capture_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 (capture_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 (capture_handle, hw_params,
SND_PCM_FORMAT_S16_LE)) < 0) {
fprintf (stderr, "cannot set sample format (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params_set_rate_near (capture_handle,
hw_params, &header.sample_rate, 0)) < 0) {
fprintf (stderr, "cannot set sample rate (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params_set_channels (capture_handle, hw_params,
2)) < 0) {
fprintf (stderr, "cannot set channel count (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params_set_periods (capture_handle, hw_params,
2, 0)) < 0) {
fprintf (stderr, "cannot set channel periods (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params_set_buffer_size (capture_handle,
hw_params, (8192*2)>>2)) < 0) {
fprintf (stderr, "cannot set buffer size (%s)\n",
snd_strerror (err));
exit (1);
}
if ((err = snd_pcm_hw_params (capture_handle, hw_params)) < 0) {
fprintf (stderr, "cannot set parameters (%s)\n",
snd_strerror (err));
exit (1);
}
snd_pcm_hw_params_free (hw_params);
if ((err = snd_pcm_prepare (capture_handle)) < 0) {
fprintf (stderr, "cannot prepare audio interface for use (%s)\n",
snd_strerror (err));
exit (1);
}
/* start recording */
signal(SIGINT, catch_signal);
while(recording) {
if ((err = snd_pcm_readi (capture_handle, buf, BUF_LEN)) !=
BUF_LEN) {
fprintf (stderr, "read from audio interface failed (%s)\n",
snd_strerror (err));
exit (1);
}
header.subchunk2_size += BUF_LEN * 4;
fwrite(buf, BUF_LEN * 4, 1, output);
}
snd_pcm_close (capture_handle);
/* save rest of the data */
header.chunk_size = ftell(output) - 8;
fseek(output, 0, SEEK_SET);
fwrite(&header, sizeof(struct wav_header), 1, output);
fclose(output);
printf("File %d bytes, audio length: %d samples\n",
header.chunk_size,
header.subchunk2_size / header.channels / (header.bits_per_sample /
8));
return 0;
}
.
- Prev by Date: Re: Linux shared libraries, was: gdb stepping into boring stuff
- Next by Date: Re: Accurate timer
- Previous by thread: Accurate timer
- Next by thread: fork after open a device node
- Index(es):
Relevant Pages
|
|