docs
[mirrors/Programs.git] / c / ghetto-sound-system / beatdetect.c
1 //CFLAGS=-lasound make beatdetect; echo -----; ./beatdetect hw:default
2 //http://www.equalarea.com/paul/alsa-audio.html
3
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <alsa/asoundlib.h>
7
8 main (int argc, char *argv[])
9 {
10 int i;
11 int err;
12 short buf[128];
13 snd_pcm_t *capture_handle;
14 snd_pcm_hw_params_t *hw_params;
15
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",
18 argv[1],
19 snd_strerror (err));
20 exit (1);
21 }
22 if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0) {
23 fprintf (stderr, "cannot allocate hardware parameter structure (%s)\n",
24 snd_strerror (err));
25 exit (1);
26 }
27
28 if ((err = snd_pcm_hw_params_any (capture_handle, hw_params)) < 0) {
29 fprintf (stderr, "cannot initialize hardware parameter structure (%s)\n",
30 snd_strerror (err));
31 exit (1);
32 }
33
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",
36 snd_strerror (err));
37 exit (1);
38 }
39
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",
42 snd_strerror (err));
43 exit (1);
44 }
45
46 /* puts("lolopre");
47 if ((err = snd_pcm_hw_params_set_rate_near (capture_handle, hw_params, 44100, 0)) < 0) {
48 puts("loloerr");
49 fprintf (stderr, "cannot set sample rate (%s)\n",
50 snd_strerror (err));
51 exit (1);
52 }
53
54 if ((err = snd_pcm_hw_params_set_channels (capture_handle, hw_params, 2)) < 0) {
55 fprintf (stderr, "cannot set channel count (%s)\n",
56 snd_strerror (err));
57 exit (1);
58 }
59 */
60
61 if ((err = snd_pcm_hw_params (capture_handle, hw_params)) < 0) {
62 fprintf (stderr, "cannot set parameters (%s)\n",
63 snd_strerror (err));
64 exit (1);
65 }
66
67 snd_pcm_hw_params_free (hw_params);
68
69 if ((err = snd_pcm_prepare (capture_handle)) < 0) {
70 fprintf (stderr, "cannot prepare audio interface for use (%s)\n",
71 snd_strerror (err));
72 exit (1);
73 }
74
75 //for (i = 0; i < 10; ++i) {
76 for (i = 0; 1; ++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));
79 exit (1);
80 } else {
81 int j;
82 for(j=0;j<128;j++) if((int)buf[j] > 1000) {
83 //printf("BOOM! %d\n", (int)buf[j]);
84 //puts("");
85 putchar('!');
86 fflush(stdout);
87 break;
88 }
89 }
90 }
91
92 snd_pcm_close (capture_handle);
93 exit (0);
94 }
This page took 0.362145 seconds and 4 git commands to generate.