516 |
kaklik |
1 |
#include <stdio.h>
|
|
|
2 |
#include <stdlib.h>
|
|
|
3 |
#include <alsa/asoundlib.h>
|
|
|
4 |
|
|
|
5 |
int rate = 44100;
|
|
|
6 |
int exact_rate;
|
|
|
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 |
|
|
|
23 |
if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0) {
|
|
|
24 |
fprintf (stderr, "cannot allocate hardware parameter structure (%s)\n",
|
|
|
25 |
snd_strerror (err));
|
|
|
26 |
exit (1);
|
|
|
27 |
}
|
|
|
28 |
|
|
|
29 |
if ((err = snd_pcm_hw_params_any (capture_handle, hw_params)) < 0) {
|
|
|
30 |
fprintf (stderr, "cannot initialize hardware parameter structure (%s)\n",
|
|
|
31 |
snd_strerror (err));
|
|
|
32 |
exit (1);
|
|
|
33 |
}
|
|
|
34 |
|
|
|
35 |
if ((err = snd_pcm_hw_params_set_access (capture_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
|
|
|
36 |
fprintf (stderr, "cannot set access type (%s)\n",
|
|
|
37 |
snd_strerror (err));
|
|
|
38 |
exit (1);
|
|
|
39 |
}
|
|
|
40 |
|
|
|
41 |
if ((err = snd_pcm_hw_params_set_format (capture_handle, hw_params, SND_PCM_FORMAT_S16_LE)) < 0) {
|
|
|
42 |
fprintf (stderr, "cannot set sample format (%s)\n",
|
|
|
43 |
snd_strerror (err));
|
|
|
44 |
exit (1);
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
exact_rate = rate;
|
|
|
48 |
if ((err = snd_pcm_hw_params_set_rate_near (capture_handle, hw_params, &exact_rate, 0)) < 0) {
|
|
|
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 |
if ((err = snd_pcm_hw_params (capture_handle, hw_params)) < 0) {
|
|
|
61 |
fprintf (stderr, "cannot set parameters (%s)\n",
|
|
|
62 |
snd_strerror (err));
|
|
|
63 |
exit (1);
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
snd_pcm_hw_params_free (hw_params);
|
|
|
67 |
|
|
|
68 |
if ((err = snd_pcm_prepare (capture_handle)) < 0) {
|
|
|
69 |
fprintf (stderr, "cannot prepare audio interface for use (%s)\n",
|
|
|
70 |
snd_strerror (err));
|
|
|
71 |
exit (1);
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
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",
|
|
|
77 |
snd_strerror (err));
|
|
|
78 |
exit (1);
|
|
|
79 |
}
|
517 |
kaklik |
80 |
/*if ((err = snd_pcm_writei (capture_handle, buf, 128)) != 128) {
|
516 |
kaklik |
81 |
fprintf (stderr, "write to audio interface failed (%s)\n",
|
|
|
82 |
snd_strerror (err));
|
|
|
83 |
exit (1);
|
517 |
kaklik |
84 |
}*/
|
516 |
kaklik |
85 |
}
|
|
|
86 |
|
|
|
87 |
|
|
|
88 |
snd_pcm_close (capture_handle);
|
|
|
89 |
exit (0);
|
|
|
90 |
}
|