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