blob: fe80de760135f386a73a6182c6dcdb391773bbe3 [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>
John Muir47d39512017-07-05 15:57:35 -070035#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,
John Muir47d39512017-07-05 15:57:35 -070065 unsigned int period_count, unsigned int cap_time);
Simon Wilson7153bff2011-07-14 12:16:41 -070066
Glenn Kastenfde0bf12016-02-08 14:42:36 -080067void sigint_handler(int sig __unused)
Simon Wilson7153bff2011-07-14 12:16:41 -070068{
69 capturing = 0;
70}
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;
79 unsigned int rate = 44100;
80 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;
John Muir47d39512017-07-05 15:57:35 -070084 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) {
John Muir47d39512017-07-05 15:57:35 -070088 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
94 file = fopen(argv[1], "wb");
95 if (!file) {
96 fprintf(stderr, "Unable to create file '%s'\n", argv[1]);
97 return 1;
98 }
99
100 /* parse command line arguments */
101 argv += 2;
102 while (*argv) {
Simon Wilsondd88f132011-07-25 10:58:30 -0700103 if (strcmp(*argv, "-d") == 0) {
Simon Wilson7153bff2011-07-14 12:16:41 -0700104 argv++;
Simon Wilsondaa83292012-02-28 15:26:02 -0800105 if (*argv)
106 device = atoi(*argv);
Simon Wilsondd88f132011-07-25 10:58:30 -0700107 } else if (strcmp(*argv, "-c") == 0) {
Simon Wilson7153bff2011-07-14 12:16:41 -0700108 argv++;
Simon Wilsondaa83292012-02-28 15:26:02 -0800109 if (*argv)
110 channels = atoi(*argv);
Simon Wilsondd88f132011-07-25 10:58:30 -0700111 } else if (strcmp(*argv, "-r") == 0) {
Simon Wilson7153bff2011-07-14 12:16:41 -0700112 argv++;
Simon Wilsondaa83292012-02-28 15:26:02 -0800113 if (*argv)
114 rate = atoi(*argv);
Simon Wilsondd88f132011-07-25 10:58:30 -0700115 } else if (strcmp(*argv, "-b") == 0) {
Simon Wilson7153bff2011-07-14 12:16:41 -0700116 argv++;
Simon Wilsondaa83292012-02-28 15:26:02 -0800117 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);
John Muir47d39512017-07-05 15:57:35 -0700131 } else if (strcmp(*argv, "-T") == 0) {
132 argv++;
133 if (*argv)
134 cap_time = atoi(*argv);
Simon Wilson7153bff2011-07-14 12:16:41 -0700135 }
Simon Wilsondaa83292012-02-28 15:26:02 -0800136 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:
160 fprintf(stderr, "%d bits is not supported.\n", bits);
161 return 1;
162 }
163
164 header.bits_per_sample = pcm_format_to_bits(format);
Simon Wilson7153bff2011-07-14 12:16:41 -0700165 header.byte_rate = (header.bits_per_sample / 8) * channels * rate;
166 header.block_align = channels * (header.bits_per_sample / 8);
Simon Wilson7153bff2011-07-14 12:16:41 -0700167 header.data_id = ID_DATA;
168
169 /* leave enough room for header */
170 fseek(file, sizeof(struct wav_header), SEEK_SET);
171
172 /* install signal handler and begin capturing */
173 signal(SIGINT, sigint_handler);
John Muir47d39512017-07-05 15:57:35 -0700174 signal(SIGHUP, sigint_handler);
175 signal(SIGTERM, sigint_handler);
Simon Wilsondaa83292012-02-28 15:26:02 -0800176 frames = capture_sample(file, card, device, header.num_channels,
Simon Wilson36ea2d82013-07-17 11:10:45 -0700177 header.sample_rate, format,
John Muir47d39512017-07-05 15:57:35 -0700178 period_size, period_count, cap_time);
Simon Wilson7153bff2011-07-14 12:16:41 -0700179 printf("Captured %d frames\n", frames);
180
181 /* write header now all information is known */
182 header.data_sz = frames * header.block_align;
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800183 header.riff_sz = header.data_sz + sizeof(header) - 8;
Simon Wilson7153bff2011-07-14 12:16:41 -0700184 fseek(file, 0, SEEK_SET);
185 fwrite(&header, sizeof(struct wav_header), 1, file);
186
187 fclose(file);
188
189 return 0;
190}
191
Simon Wilsondaa83292012-02-28 15:26:02 -0800192unsigned int capture_sample(FILE *file, unsigned int card, unsigned int device,
Simon Wilson7153bff2011-07-14 12:16:41 -0700193 unsigned int channels, unsigned int rate,
Simon Wilson36ea2d82013-07-17 11:10:45 -0700194 enum pcm_format format, unsigned int period_size,
John Muir47d39512017-07-05 15:57:35 -0700195 unsigned int period_count, unsigned int cap_time)
Simon Wilson7153bff2011-07-14 12:16:41 -0700196{
197 struct pcm_config config;
198 struct pcm *pcm;
199 char *buffer;
200 unsigned int size;
201 unsigned int bytes_read = 0;
John Muir47d39512017-07-05 15:57:35 -0700202 struct timespec end;
203 struct timespec now;
Simon Wilson7153bff2011-07-14 12:16:41 -0700204
Haynes Mathew George875ff852015-08-25 12:47:13 -0700205 memset(&config, 0, sizeof(config));
Simon Wilson7153bff2011-07-14 12:16:41 -0700206 config.channels = channels;
207 config.rate = rate;
Simon Wilsondaa83292012-02-28 15:26:02 -0800208 config.period_size = period_size;
209 config.period_count = period_count;
Simon Wilson36ea2d82013-07-17 11:10:45 -0700210 config.format = format;
Simon Wilson62104732011-08-05 12:00:00 -0700211 config.start_threshold = 0;
212 config.stop_threshold = 0;
213 config.silence_threshold = 0;
Simon Wilson7153bff2011-07-14 12:16:41 -0700214
Simon Wilsondaa83292012-02-28 15:26:02 -0800215 pcm = pcm_open(card, device, PCM_IN, &config);
Simon Wilson7153bff2011-07-14 12:16:41 -0700216 if (!pcm || !pcm_is_ready(pcm)) {
217 fprintf(stderr, "Unable to open PCM device (%s)\n",
218 pcm_get_error(pcm));
219 return 0;
220 }
221
Simon Wilson9673f572013-05-01 15:10:34 -0700222 size = pcm_frames_to_bytes(pcm, pcm_get_buffer_size(pcm));
Simon Wilson7153bff2011-07-14 12:16:41 -0700223 buffer = malloc(size);
224 if (!buffer) {
225 fprintf(stderr, "Unable to allocate %d bytes\n", size);
226 free(buffer);
227 pcm_close(pcm);
228 return 0;
229 }
230
Simon Wilson36ea2d82013-07-17 11:10:45 -0700231 printf("Capturing sample: %u ch, %u hz, %u bit\n", channels, rate,
232 pcm_format_to_bits(format));
Simon Wilson7153bff2011-07-14 12:16:41 -0700233
John Muir47d39512017-07-05 15:57:35 -0700234 clock_gettime(CLOCK_MONOTONIC, &now);
235 end.tv_sec = now.tv_sec + cap_time;
236 end.tv_nsec = now.tv_nsec;
237
Simon Wilson7153bff2011-07-14 12:16:41 -0700238 while (capturing && !pcm_read(pcm, buffer, size)) {
239 if (fwrite(buffer, 1, size, file) != size) {
240 fprintf(stderr,"Error capturing sample\n");
241 break;
242 }
243 bytes_read += size;
John Muir47d39512017-07-05 15:57:35 -0700244 if (cap_time) {
245 clock_gettime(CLOCK_MONOTONIC, &now);
246 if (now.tv_sec > end.tv_sec ||
247 (now.tv_sec == end.tv_sec && now.tv_nsec >= end.tv_nsec))
248 break;
249 }
Simon Wilson7153bff2011-07-14 12:16:41 -0700250 }
251
252 free(buffer);
253 pcm_close(pcm);
Simon Wilson36ea2d82013-07-17 11:10:45 -0700254 return pcm_bytes_to_frames(pcm, bytes_read);
Simon Wilson7153bff2011-07-14 12:16:41 -0700255}