8c9fcfb016bd37e514f877ab90155d7657ad4f4d
1 /* tinycap.c
2 **
3 ** Copyright 2011, The Android Open Source Project
4 **
5 ** Redistribution and use in source and binary forms, with or without
6 ** modification, are permitted provided that the following conditions are met:
7 ** * Redistributions of source code must retain the above copyright
8 ** notice, this list of conditions and the following disclaimer.
9 ** * Redistributions in binary form must reproduce the above copyright
10 ** notice, this list of conditions and the following disclaimer in the
11 ** documentation and/or other materials provided with the distribution.
12 ** * Neither the name of The Android Open Source Project nor the names of
13 ** its contributors may be used to endorse or promote products derived
14 ** from this software without specific prior written permission.
15 **
16 ** THIS SOFTWARE IS PROVIDED BY The Android Open Source Project ``AS IS'' AND
17 ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 ** ARE DISCLAIMED. IN NO EVENT SHALL The Android Open Source Project BE LIABLE
20 ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 ** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 ** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
26 ** DAMAGE.
27 */
29 #include <tinyalsa/asoundlib.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <stdint.h>
33 #include <signal.h>
34 #include <string.h>
36 #define ID_RIFF 0x46464952
37 #define ID_WAVE 0x45564157
38 #define ID_FMT 0x20746d66
39 #define ID_DATA 0x61746164
41 #define FORMAT_PCM 1
43 struct wav_header {
44 uint32_t riff_id;
45 uint32_t riff_sz;
46 uint32_t riff_fmt;
47 uint32_t fmt_id;
48 uint32_t fmt_sz;
49 uint16_t audio_format;
50 uint16_t num_channels;
51 uint32_t sample_rate;
52 uint32_t byte_rate;
53 uint16_t block_align;
54 uint16_t bits_per_sample;
55 uint32_t data_id;
56 uint32_t data_sz;
57 };
59 int capturing = 1;
61 unsigned int capture_sample(FILE *file, unsigned int card, unsigned int device,
62 unsigned int channels, unsigned int rate,
63 unsigned int bits, unsigned int period_size,
64 unsigned int period_count);
66 void sigint_handler(int sig)
67 {
68 capturing = 0;
69 }
71 int main(int argc, char **argv)
72 {
73 FILE *file;
74 struct wav_header header;
75 unsigned int card = 0;
76 unsigned int device = 0;
77 unsigned int channels = 2;
78 unsigned int rate = 44100;
79 unsigned int bits = 16;
80 unsigned int frames;
81 unsigned int period_size = 1024;
82 unsigned int period_count = 4;
84 if (argc < 2) {
85 fprintf(stderr, "Usage: %s file.wav [-D card] [-d device] [-c channels] "
86 "[-r rate] [-b bits] [-p period_size] [-n n_periods]\n", argv[0]);
87 return 1;
88 }
90 file = fopen(argv[1], "wb");
91 if (!file) {
92 fprintf(stderr, "Unable to create file '%s'\n", argv[1]);
93 return 1;
94 }
96 /* parse command line arguments */
97 argv += 2;
98 while (*argv) {
99 if (strcmp(*argv, "-d") == 0) {
100 argv++;
101 if (*argv)
102 device = atoi(*argv);
103 } else if (strcmp(*argv, "-c") == 0) {
104 argv++;
105 if (*argv)
106 channels = atoi(*argv);
107 } else if (strcmp(*argv, "-r") == 0) {
108 argv++;
109 if (*argv)
110 rate = atoi(*argv);
111 } else if (strcmp(*argv, "-b") == 0) {
112 argv++;
113 if (*argv)
114 bits = atoi(*argv);
115 } else if (strcmp(*argv, "-D") == 0) {
116 argv++;
117 if (*argv)
118 card = atoi(*argv);
119 } else if (strcmp(*argv, "-p") == 0) {
120 argv++;
121 if (*argv)
122 period_size = atoi(*argv);
123 } else if (strcmp(*argv, "-n") == 0) {
124 argv++;
125 if (*argv)
126 period_count = atoi(*argv);
127 }
128 if (*argv)
129 argv++;
130 }
132 header.riff_id = ID_RIFF;
133 header.riff_sz = 0;
134 header.riff_fmt = ID_WAVE;
135 header.fmt_id = ID_FMT;
136 header.fmt_sz = 16;
137 header.audio_format = FORMAT_PCM;
138 header.num_channels = channels;
139 header.sample_rate = rate;
140 header.bits_per_sample = bits;
141 header.byte_rate = (header.bits_per_sample / 8) * channels * rate;
142 header.block_align = channels * (header.bits_per_sample / 8);
143 header.data_id = ID_DATA;
145 /* leave enough room for header */
146 fseek(file, sizeof(struct wav_header), SEEK_SET);
148 /* install signal handler and begin capturing */
149 signal(SIGINT, sigint_handler);
150 frames = capture_sample(file, card, device, header.num_channels,
151 header.sample_rate, header.bits_per_sample,
152 period_size, period_count);
153 printf("Captured %d frames\n", frames);
155 /* write header now all information is known */
156 header.data_sz = frames * header.block_align;
157 header.riff_sz = header.data_sz + sizeof(header) - 8;
158 fseek(file, 0, SEEK_SET);
159 fwrite(&header, sizeof(struct wav_header), 1, file);
161 fclose(file);
163 return 0;
164 }
166 unsigned int capture_sample(FILE *file, unsigned int card, unsigned int device,
167 unsigned int channels, unsigned int rate,
168 unsigned int bits, unsigned int period_size,
169 unsigned int period_count)
170 {
171 struct pcm_config config;
172 struct pcm *pcm;
173 char *buffer;
174 unsigned int size;
175 unsigned int bytes_read = 0;
177 config.channels = channels;
178 config.rate = rate;
179 config.period_size = period_size;
180 config.period_count = period_count;
181 if (bits == 32)
182 config.format = PCM_FORMAT_S32_LE;
183 else if (bits == 16)
184 config.format = PCM_FORMAT_S16_LE;
185 config.start_threshold = 0;
186 config.stop_threshold = 0;
187 config.silence_threshold = 0;
189 pcm = pcm_open(card, device, PCM_IN, &config);
190 if (!pcm || !pcm_is_ready(pcm)) {
191 fprintf(stderr, "Unable to open PCM device (%s)\n",
192 pcm_get_error(pcm));
193 return 0;
194 }
196 size = pcm_get_buffer_size(pcm);
197 buffer = malloc(size);
198 if (!buffer) {
199 fprintf(stderr, "Unable to allocate %d bytes\n", size);
200 free(buffer);
201 pcm_close(pcm);
202 return 0;
203 }
205 printf("Capturing sample: %u ch, %u hz, %u bit\n", channels, rate, bits);
207 while (capturing && !pcm_read(pcm, buffer, size)) {
208 if (fwrite(buffer, 1, size, file) != size) {
209 fprintf(stderr,"Error capturing sample\n");
210 break;
211 }
212 bytes_read += size;
213 }
215 free(buffer);
216 pcm_close(pcm);
217 return bytes_read / ((bits / 8) * channels);
218 }