1 //CFLAGS=-lasound make beatdetect; echo -----; ./beatdetect hw:default
2 //http://www.equalarea.com/paul/alsa-audio.html
6 #include <alsa/asoundlib.h>
8 main (int argc
, char *argv
[])
13 snd_pcm_t
*capture_handle
;
14 snd_pcm_hw_params_t
*hw_params
;
16 if ((err
= snd_pcm_open (&capture_handle
, argv
[1], SND_PCM_STREAM_CAPTURE
, 0)) < 0) {
17 fprintf (stderr
, "cannot open audio device %s (%s)\n",
22 if ((err
= snd_pcm_hw_params_malloc (&hw_params
)) < 0) {
23 fprintf (stderr
, "cannot allocate hardware parameter structure (%s)\n",
28 if ((err
= snd_pcm_hw_params_any (capture_handle
, hw_params
)) < 0) {
29 fprintf (stderr
, "cannot initialize hardware parameter structure (%s)\n",
34 if ((err
= snd_pcm_hw_params_set_access (capture_handle
, hw_params
, SND_PCM_ACCESS_RW_INTERLEAVED
)) < 0) {
35 fprintf (stderr
, "cannot set access type (%s)\n",
40 if ((err
= snd_pcm_hw_params_set_format (capture_handle
, hw_params
, SND_PCM_FORMAT_S16_LE
)) < 0) { //SND_PCM_FORMAT_S16_LE
41 fprintf (stderr
, "cannot set sample format (%s)\n",
47 if ((err = snd_pcm_hw_params_set_rate_near (capture_handle, hw_params, 44100, 0)) < 0) {
49 fprintf (stderr, "cannot set sample rate (%s)\n",
54 if ((err = snd_pcm_hw_params_set_channels (capture_handle, hw_params, 2)) < 0) {
55 fprintf (stderr, "cannot set channel count (%s)\n",
61 if ((err
= snd_pcm_hw_params (capture_handle
, hw_params
)) < 0) {
62 fprintf (stderr
, "cannot set parameters (%s)\n",
67 snd_pcm_hw_params_free (hw_params
);
69 if ((err
= snd_pcm_prepare (capture_handle
)) < 0) {
70 fprintf (stderr
, "cannot prepare audio interface for use (%s)\n",
75 //for (i = 0; i < 10; ++i) {
77 if ((err
= snd_pcm_readi (capture_handle
, buf
, 128)) != 128) {
78 fprintf (stderr
, "read from audio interface failed (%s)\n", snd_strerror (err
));
82 for(j
=0;j
<128;j
++) if((int)buf
[j
] > 1000) {
83 //printf("BOOM! %d\n", (int)buf[j]);
92 snd_pcm_close (capture_handle
);