Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// This work is free. You can redistribute it and/or modify it under the
- /// terms of the Do What The Fuck You Want To Public License, Version 2,
- /// as published by Sam Hocevar. See http://www.wtfpl.net/ for more details.
- // gcc -o alsa_record alsa_record.c -lasound
- #include <assert.h>
- #include <alsa/asoundlib.h>
- #include <math.h>
- #define FRAMES 100
- short buffer[FRAMES*2]; // stereo
- int main(int argc, char *argv[]) {
- const char *pcmname = argc == 2 ? argv[1] : "default";
- snd_pcm_t *pcm;
- assert( snd_pcm_open(&pcm, pcmname, SND_PCM_STREAM_CAPTURE, 0) == 0 );
- assert(
- snd_pcm_set_params(pcm, SND_PCM_FORMAT_S16_LE, SND_PCM_ACCESS_RW_INTERLEAVED, 1/*channels*/, 48000/*rate*/, 1, 250*1000/*latency*/) == 0 ||
- snd_pcm_set_params(pcm, SND_PCM_FORMAT_S16_LE, SND_PCM_ACCESS_RW_INTERLEAVED, 2/*channels*/, 48000/*rate*/, 1, 250*1000/*latency*/) == 0
- );
- // Need to set start_threshold
- // PCM reads into readi() buffer and never starts if sizeof(buffer) < start_threshold:
- snd_pcm_sw_params_t *swparams;
- snd_pcm_sw_params_alloca(&swparams);
- assert( snd_pcm_sw_params_current(pcm, swparams) == 0 );
- snd_pcm_uframes_t start_threshold = -1;
- assert( snd_pcm_sw_params_get_start_threshold(swparams, &start_threshold) == 0 );
- fprintf(stderr, "[start_threshold=%d]\n", (int)start_threshold);
- start_threshold = 1;
- assert( snd_pcm_sw_params_set_start_threshold(pcm, swparams, start_threshold) == 0 );
- assert( snd_pcm_sw_params(pcm, swparams) == 0 );
- int i;
- for (i = 0; i < 144000; ) {
- snd_pcm_sframes_t r = snd_pcm_readi(pcm, buffer, FRAMES);
- fprintf(stderr, "snd_pcm_readi(%d)=%d\n", FRAMES, (int)r);
- if (r < 0)
- fprintf(stderr, "snd_pcm_recover()=%d\n", (int)snd_pcm_recover(pcm, r, 1));
- else if (r > 0)
- i += r;
- }
- assert( snd_pcm_close(pcm) == 0 );
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment