1 //CFLAGS=-lasound make beatdetect; echo -----; ./beatdetect hw:default
4 #include <alsa/asoundlib.h>
6 main (int argc
, char *argv
[])
11 snd_pcm_t
*capture_handle
;
12 snd_pcm_hw_params_t
*hw_params
;
14 if ((err
= snd_pcm_open (&capture_handle
, argv
[1], SND_PCM_STREAM_CAPTURE
, 0)) < 0) {
15 fprintf (stderr
, "cannot open audio device %s (%s)\n",
20 if ((err
= snd_pcm_hw_params_malloc (&hw_params
)) < 0) {
21 fprintf (stderr
, "cannot allocate hardware parameter structure (%s)\n",
26 if ((err
= snd_pcm_hw_params_any (capture_handle
, hw_params
)) < 0) {
27 fprintf (stderr
, "cannot initialize hardware parameter structure (%s)\n",
32 if ((err
= snd_pcm_hw_params_set_access (capture_handle
, hw_params
, SND_PCM_ACCESS_RW_INTERLEAVED
)) < 0) {
33 fprintf (stderr
, "cannot set access type (%s)\n",
38 if ((err
= snd_pcm_hw_params_set_format (capture_handle
, hw_params
, SND_PCM_FORMAT_S16_LE
)) < 0) { //SND_PCM_FORMAT_S16_LE
39 fprintf (stderr
, "cannot set sample format (%s)\n",
45 if ((err = snd_pcm_hw_params_set_rate_near (capture_handle, hw_params, 44100, 0)) < 0) {
47 fprintf (stderr, "cannot set sample rate (%s)\n",
52 if ((err = snd_pcm_hw_params_set_channels (capture_handle, hw_params, 2)) < 0) {
53 fprintf (stderr, "cannot set channel count (%s)\n",
59 if ((err
= snd_pcm_hw_params (capture_handle
, hw_params
)) < 0) {
60 fprintf (stderr
, "cannot set parameters (%s)\n",
65 snd_pcm_hw_params_free (hw_params
);
67 if ((err
= snd_pcm_prepare (capture_handle
)) < 0) {
68 fprintf (stderr
, "cannot prepare audio interface for use (%s)\n",
73 //for (i = 0; i < 10; ++i) {
75 if ((err
= snd_pcm_readi (capture_handle
, buf
, 128)) != 128) {
76 fprintf (stderr
, "read from audio interface failed (%s)\n", snd_strerror (err
));
80 for(j
=0;j
<128;j
++) if((int)buf
[j
] > 1000) {
81 printf("BOOM! %d\n", (int)buf
[j
]);
87 snd_pcm_close (capture_handle
);