Created
October 29, 2018 08:57
-
-
Save takaswie/1e4e414c71b4541dd971ea57e420e022 to your computer and use it in GitHub Desktop.
pcm-capture-threshold-test.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <errno.h> | |
#include <poll.h> | |
#include <alsa/asoundlib.h> | |
#ifdef PULSE | |
#define NODE "pulse" | |
#else | |
#define NODE "hw:1,0" | |
#endif | |
int main(int argc, const char *const *argv) | |
{ | |
snd_pcm_t *handle; | |
int pfd_count; | |
struct pollfd *pfds; | |
snd_pcm_uframes_t frame_count; | |
int err; | |
err = snd_pcm_open(&handle, NODE, SND_PCM_STREAM_CAPTURE, 0); | |
if (err < 0) { | |
printf("snd_pcm_open(): %s\n", strerror(-err)); | |
return EXIT_FAILURE; | |
} | |
pfd_count = snd_pcm_poll_descriptors_count(handle); | |
if (pfd_count < 0) { | |
printf("snd_pcm_poll_descriptors_count(): %s\n", | |
strerror(-pfd_count)); | |
goto end; | |
} | |
pfds = calloc(pfd_count, sizeof(*pfds)); | |
if (pfds == NULL) { | |
printf("calloc(2): %s\n", strerror(errno)); | |
goto end; | |
} | |
err = snd_pcm_poll_descriptors(handle, pfds, pfd_count); | |
if (err < 0) { | |
printf("snd_pcm_poll_descriptors(): %s\n", strerror(-err)); | |
goto err_pfds; | |
} | |
err = snd_pcm_set_params(handle, SND_PCM_FORMAT_S16, | |
SND_PCM_ACCESS_RW_INTERLEAVED, | |
2, 48000, 0, 100 * 1000); | |
if (err < 0) { | |
printf("snd_pcm_set_params(): %s\n", strerror(-err)); | |
goto err_pfds; | |
} | |
{ | |
snd_pcm_sw_params_t *sw_params; | |
err = snd_pcm_sw_params_malloc(&sw_params); | |
if (err < 0) { | |
printf("snd_pcm_sw_params_malloc(): %s\n", | |
strerror(-err)); | |
goto err_pfds; | |
} | |
err = snd_pcm_sw_params_current(handle, sw_params); | |
if (err < 0) { | |
printf("snd_pcm_sw_params_current(): %s\n", | |
strerror(-err)); | |
} else { | |
err = snd_pcm_sw_params_get_start_threshold(sw_params, | |
&frame_count); | |
if (err < 0) | |
printf("snd_pcm_sw_params_get_start_threshold: %s\n", | |
strerror(-err)); | |
printf("start_threshold: %lu\n", frame_count); | |
} | |
snd_pcm_sw_params_free(sw_params); | |
if (err < 0) | |
goto err_pfds; | |
} | |
//err = snd_pcm_start(handle); | |
//if (err < 0) { | |
// printf("snd_pcm_start(): %s\n", strerror(-err)); | |
// goto err_pfds; | |
//} | |
//frame_count /= 2; | |
while (1) { | |
int count; | |
unsigned short revents; | |
char buf[16 * 2 * frame_count]; | |
snd_pcm_sframes_t handled_frame_count; | |
handled_frame_count = snd_pcm_readi(handle, buf, frame_count); | |
if (handled_frame_count < 0) { | |
err = -handled_frame_count; | |
printf("snd_pcm_readi(): %s\n", strerror(-err)); | |
break; | |
} | |
printf("handled: %lu\n", handled_frame_count); | |
count = poll(pfds, pfd_count, frame_count / 48000); | |
if (count < 0) { | |
if (errno == EINTR || errno == EAGAIN) | |
continue; | |
err = errno; | |
break; | |
} | |
if (count == 0) | |
continue; | |
err = snd_pcm_poll_descriptors_revents(handle, pfds, pfd_count, | |
&revents); | |
if (err < 0) | |
break; | |
} | |
err_pfds: | |
free(pfds); | |
end: | |
snd_pcm_close(handle); | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment