blob: b0c19082b86134c82f8cd0f34410ebcfb0615539 [file] [log] [blame]
Simon Wilson7153bff2011-07-14 12:16:41 -07001/* 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*/
28
29#include <tinyalsa/asoundlib.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <stdint.h>
33#include <signal.h>
Simon Wilsonda39e0b2012-11-09 15:16:47 -080034#include <string.h>
David Lie95e65a2020-12-09 02:11:18 +000035#include <time.h>
Simon Wilson7153bff2011-07-14 12:16:41 -070036
37#define ID_RIFF 0x46464952
38#define ID_WAVE 0x45564157
39#define ID_FMT 0x20746d66
40#define ID_DATA 0x61746164
41
42#define FORMAT_PCM 1
43
44struct wav_header {
45 uint32_t riff_id;
46 uint32_t riff_sz;
47 uint32_t riff_fmt;
48 uint32_t fmt_id;
49 uint32_t fmt_sz;
50 uint16_t audio_format;
51 uint16_t num_channels;
52 uint32_t sample_rate;
53 uint32_t byte_rate;
54 uint16_t block_align;
55 uint16_t bits_per_sample;
56 uint32_t data_id;
57 uint32_t data_sz;
58};
59
60int capturing = 1;
61
Simon Wilsondaa83292012-02-28 15:26:02 -080062unsigned int capture_sample(FILE *file, unsigned int card, unsigned int device,
Simon Wilson7153bff2011-07-14 12:16:41 -070063 unsigned int channels, unsigned int rate,
Simon Wilson36ea2d82013-07-17 11:10:45 -070064 enum pcm_format format, unsigned int period_size,
David Lie95e65a2020-12-09 02:11:18 +000065 unsigned int period_count, unsigned int cap_time);
Simon Wilson7153bff2011-07-14 12:16:41 -070066
David Lie95e65a2020-12-09 02:11:18 +000067void sigint_handler(int sig __unused)
Simon Wilson7153bff2011-07-14 12:16:41 -070068{
David Lie95e65a2020-12-09 02:11:18 +000069 capturing = 0;
Simon Wilson7153bff2011-07-14 12:16:41 -070070}
71
72int main(int argc, char **argv)
73{
74 FILE *file;
75 struct wav_header header;
Simon Wilsondaa83292012-02-28 15:26:02 -080076 unsigned int card = 0;
Simon Wilson7153bff2011-07-14 12:16:41 -070077 unsigned int device = 0;
78 unsigned int channels = 2;
David Lie95e65a2020-12-09 02:11:18 +000079 unsigned int rate = 44100;
Simon Wilson7153bff2011-07-14 12:16:41 -070080 unsigned int bits = 16;
81 unsigned int frames;
Simon Wilsondaa83292012-02-28 15:26:02 -080082 unsigned int period_size = 1024;
83 unsigned int period_count = 4;
David Lie95e65a2020-12-09 02:11:18 +000084 unsigned int cap_time = 0;
Simon Wilson36ea2d82013-07-17 11:10:45 -070085 enum pcm_format format;
Simon Wilson7153bff2011-07-14 12:16:41 -070086
87 if (argc < 2) {
David Lie95e65a2020-12-09 02:11:18 +000088 fprintf(stderr, "Usage: %s file.wav [-D card] [-d device]"
89 " [-c channels] [-r rate] [-b bits] [-p period_size]"
90 " [-n n_periods] [-T capture time]\n", argv[0]);
Simon Wilson7153bff2011-07-14 12:16:41 -070091 return 1;
92 }
93
David Lie95e65a2020-12-09 02:11:18 +000094 file = fopen(argv[1], "wb");
95 if (!file) {
96 fprintf(stderr, "Unable to create file '%s'\n", argv[1]);
97 return 1;
Simon Wilson7153bff2011-07-14 12:16:41 -070098 }
99
100 /* parse command line arguments */
David Lie95e65a2020-12-09 02:11:18 +0000101 argv += 2;
102 while (*argv) {
103 if (strcmp(*argv, "-d") == 0) {
104 argv++;
105 if (*argv)
106 device = atoi(*argv);
107 } else if (strcmp(*argv, "-c") == 0) {
108 argv++;
109 if (*argv)
110 channels = atoi(*argv);
111 } else if (strcmp(*argv, "-r") == 0) {
112 argv++;
113 if (*argv)
114 rate = atoi(*argv);
115 } else if (strcmp(*argv, "-b") == 0) {
116 argv++;
117 if (*argv)
118 bits = atoi(*argv);
119 } else if (strcmp(*argv, "-D") == 0) {
120 argv++;
121 if (*argv)
122 card = atoi(*argv);
123 } else if (strcmp(*argv, "-p") == 0) {
124 argv++;
125 if (*argv)
126 period_size = atoi(*argv);
127 } else if (strcmp(*argv, "-n") == 0) {
128 argv++;
129 if (*argv)
130 period_count = atoi(*argv);
131 } else if (strcmp(*argv, "-T") == 0) {
132 argv++;
133 if (*argv)
134 cap_time = atoi(*argv);
Simon Wilson7153bff2011-07-14 12:16:41 -0700135 }
David Lie95e65a2020-12-09 02:11:18 +0000136 if (*argv)
137 argv++;
Simon Wilson7153bff2011-07-14 12:16:41 -0700138 }
139
140 header.riff_id = ID_RIFF;
141 header.riff_sz = 0;
142 header.riff_fmt = ID_WAVE;
143 header.fmt_id = ID_FMT;
144 header.fmt_sz = 16;
145 header.audio_format = FORMAT_PCM;
146 header.num_channels = channels;
147 header.sample_rate = rate;
Simon Wilson36ea2d82013-07-17 11:10:45 -0700148
149 switch (bits) {
150 case 32:
151 format = PCM_FORMAT_S32_LE;
152 break;
153 case 24:
154 format = PCM_FORMAT_S24_LE;
155 break;
156 case 16:
157 format = PCM_FORMAT_S16_LE;
158 break;
159 default:
Dong Jinguanga1902c02017-12-20 10:18:09 +0800160 fprintf(stderr, "%u bits is not supported.\n", bits);
161 fclose(file);
Simon Wilson36ea2d82013-07-17 11:10:45 -0700162 return 1;
163 }
164
165 header.bits_per_sample = pcm_format_to_bits(format);
Simon Wilson7153bff2011-07-14 12:16:41 -0700166 header.byte_rate = (header.bits_per_sample / 8) * channels * rate;
167 header.block_align = channels * (header.bits_per_sample / 8);
Simon Wilson7153bff2011-07-14 12:16:41 -0700168 header.data_id = ID_DATA;
169
170 /* leave enough room for header */
David Lie95e65a2020-12-09 02:11:18 +0000171 fseek(file, sizeof(struct wav_header), SEEK_SET);
Simon Wilson7153bff2011-07-14 12:16:41 -0700172
173 /* install signal handler and begin capturing */
174 signal(SIGINT, sigint_handler);
David Lie95e65a2020-12-09 02:11:18 +0000175 signal(SIGHUP, sigint_handler);
176 signal(SIGTERM, sigint_handler);
Simon Wilsondaa83292012-02-28 15:26:02 -0800177 frames = capture_sample(file, card, device, header.num_channels,
Simon Wilson36ea2d82013-07-17 11:10:45 -0700178 header.sample_rate, format,
David Lie95e65a2020-12-09 02:11:18 +0000179 period_size, period_count, cap_time);
180 printf("Captured %u frames\n", frames);
Simon Wilson7153bff2011-07-14 12:16:41 -0700181
182 /* write header now all information is known */
David Lie95e65a2020-12-09 02:11:18 +0000183 header.data_sz = frames * header.block_align;
184 header.riff_sz = header.data_sz + sizeof(header) - 8;
185 fseek(file, 0, SEEK_SET);
186 fwrite(&header, sizeof(struct wav_header), 1, file);
Simon Wilson7153bff2011-07-14 12:16:41 -0700187
188 fclose(file);
189
190 return 0;
191}
192
Simon Wilsondaa83292012-02-28 15:26:02 -0800193unsigned int capture_sample(FILE *file, unsigned int card, unsigned int device,
Simon Wilson7153bff2011-07-14 12:16:41 -0700194 unsigned int channels, unsigned int rate,
Simon Wilson36ea2d82013-07-17 11:10:45 -0700195 enum pcm_format format, unsigned int period_size,
David Lie95e65a2020-12-09 02:11:18 +0000196 unsigned int period_count, unsigned int cap_time)
Simon Wilson7153bff2011-07-14 12:16:41 -0700197{
198 struct pcm_config config;
199 struct pcm *pcm;
200 char *buffer;
201 unsigned int size;
David Lie95e65a2020-12-09 02:11:18 +0000202 unsigned int bytes_read = 0;
203 unsigned int frames = 0;
204 struct timespec end;
205 struct timespec now;
Simon Wilson7153bff2011-07-14 12:16:41 -0700206
Haynes Mathew George875ff852015-08-25 12:47:13 -0700207 memset(&config, 0, sizeof(config));
Simon Wilson7153bff2011-07-14 12:16:41 -0700208 config.channels = channels;
209 config.rate = rate;
Simon Wilsondaa83292012-02-28 15:26:02 -0800210 config.period_size = period_size;
211 config.period_count = period_count;
Simon Wilson36ea2d82013-07-17 11:10:45 -0700212 config.format = format;
Simon Wilson62104732011-08-05 12:00:00 -0700213 config.start_threshold = 0;
214 config.stop_threshold = 0;
215 config.silence_threshold = 0;
Simon Wilson7153bff2011-07-14 12:16:41 -0700216
Simon Wilsondaa83292012-02-28 15:26:02 -0800217 pcm = pcm_open(card, device, PCM_IN, &config);
Simon Wilson7153bff2011-07-14 12:16:41 -0700218 if (!pcm || !pcm_is_ready(pcm)) {
219 fprintf(stderr, "Unable to open PCM device (%s)\n",
220 pcm_get_error(pcm));
221 return 0;
222 }
223
Simon Wilson9673f572013-05-01 15:10:34 -0700224 size = pcm_frames_to_bytes(pcm, pcm_get_buffer_size(pcm));
Simon Wilson7153bff2011-07-14 12:16:41 -0700225 buffer = malloc(size);
226 if (!buffer) {
Dong Jinguanga1902c02017-12-20 10:18:09 +0800227 fprintf(stderr, "Unable to allocate %u bytes\n", size);
David Lie95e65a2020-12-09 02:11:18 +0000228 free(buffer);
Simon Wilson7153bff2011-07-14 12:16:41 -0700229 pcm_close(pcm);
230 return 0;
231 }
232
David Lie95e65a2020-12-09 02:11:18 +0000233 printf("Capturing sample: %u ch, %u hz, %u bit\n", channels, rate,
Simon Wilson36ea2d82013-07-17 11:10:45 -0700234 pcm_format_to_bits(format));
Simon Wilson7153bff2011-07-14 12:16:41 -0700235
David Lie95e65a2020-12-09 02:11:18 +0000236 clock_gettime(CLOCK_MONOTONIC, &now);
237 end.tv_sec = now.tv_sec + cap_time;
238 end.tv_nsec = now.tv_nsec;
239
240 while (capturing && !pcm_read(pcm, buffer, size)) {
241 if (fwrite(buffer, 1, size, file) != size) {
Simon Wilson7153bff2011-07-14 12:16:41 -0700242 fprintf(stderr,"Error capturing sample\n");
243 break;
244 }
David Lie95e65a2020-12-09 02:11:18 +0000245 bytes_read += size;
246 if (cap_time) {
247 clock_gettime(CLOCK_MONOTONIC, &now);
248 if (now.tv_sec > end.tv_sec ||
249 (now.tv_sec == end.tv_sec && now.tv_nsec >= end.tv_nsec))
250 break;
251 }
Simon Wilson7153bff2011-07-14 12:16:41 -0700252 }
253
David Lie95e65a2020-12-09 02:11:18 +0000254 frames = pcm_bytes_to_frames(pcm, bytes_read);
Simon Wilson7153bff2011-07-14 12:16:41 -0700255 free(buffer);
256 pcm_close(pcm);
David Lie95e65a2020-12-09 02:11:18 +0000257 return frames;
Simon Wilson7153bff2011-07-14 12:16:41 -0700258}