blob: 8c9fcfb016bd37e514f877ab90155d7657ad4f4d [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>
Simon Wilson7153bff2011-07-14 12:16:41 -070035
36#define ID_RIFF 0x46464952
37#define ID_WAVE 0x45564157
38#define ID_FMT 0x20746d66
39#define ID_DATA 0x61746164
40
41#define FORMAT_PCM 1
42
43struct 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};
58
59int capturing = 1;
60
Simon Wilsondaa83292012-02-28 15:26:02 -080061unsigned int capture_sample(FILE *file, unsigned int card, unsigned int device,
Simon Wilson7153bff2011-07-14 12:16:41 -070062 unsigned int channels, unsigned int rate,
Simon Wilsondaa83292012-02-28 15:26:02 -080063 unsigned int bits, unsigned int period_size,
64 unsigned int period_count);
Simon Wilson7153bff2011-07-14 12:16:41 -070065
66void sigint_handler(int sig)
67{
68 capturing = 0;
69}
70
71int main(int argc, char **argv)
72{
73 FILE *file;
74 struct wav_header header;
Simon Wilsondaa83292012-02-28 15:26:02 -080075 unsigned int card = 0;
Simon Wilson7153bff2011-07-14 12:16:41 -070076 unsigned int device = 0;
77 unsigned int channels = 2;
78 unsigned int rate = 44100;
79 unsigned int bits = 16;
80 unsigned int frames;
Simon Wilsondaa83292012-02-28 15:26:02 -080081 unsigned int period_size = 1024;
82 unsigned int period_count = 4;
Simon Wilson7153bff2011-07-14 12:16:41 -070083
84 if (argc < 2) {
Simon Wilsondaa83292012-02-28 15:26:02 -080085 fprintf(stderr, "Usage: %s file.wav [-D card] [-d device] [-c channels] "
Simon Wilsone44e30a2012-03-08 10:45:31 -080086 "[-r rate] [-b bits] [-p period_size] [-n n_periods]\n", argv[0]);
Simon Wilson7153bff2011-07-14 12:16:41 -070087 return 1;
88 }
89
90 file = fopen(argv[1], "wb");
91 if (!file) {
92 fprintf(stderr, "Unable to create file '%s'\n", argv[1]);
93 return 1;
94 }
95
96 /* parse command line arguments */
97 argv += 2;
98 while (*argv) {
Simon Wilsondd88f132011-07-25 10:58:30 -070099 if (strcmp(*argv, "-d") == 0) {
Simon Wilson7153bff2011-07-14 12:16:41 -0700100 argv++;
Simon Wilsondaa83292012-02-28 15:26:02 -0800101 if (*argv)
102 device = atoi(*argv);
Simon Wilsondd88f132011-07-25 10:58:30 -0700103 } else if (strcmp(*argv, "-c") == 0) {
Simon Wilson7153bff2011-07-14 12:16:41 -0700104 argv++;
Simon Wilsondaa83292012-02-28 15:26:02 -0800105 if (*argv)
106 channels = atoi(*argv);
Simon Wilsondd88f132011-07-25 10:58:30 -0700107 } else if (strcmp(*argv, "-r") == 0) {
Simon Wilson7153bff2011-07-14 12:16:41 -0700108 argv++;
Simon Wilsondaa83292012-02-28 15:26:02 -0800109 if (*argv)
110 rate = atoi(*argv);
Simon Wilsondd88f132011-07-25 10:58:30 -0700111 } else if (strcmp(*argv, "-b") == 0) {
Simon Wilson7153bff2011-07-14 12:16:41 -0700112 argv++;
Simon Wilsondaa83292012-02-28 15:26:02 -0800113 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);
Simon Wilson7153bff2011-07-14 12:16:41 -0700127 }
Simon Wilsondaa83292012-02-28 15:26:02 -0800128 if (*argv)
129 argv++;
Simon Wilson7153bff2011-07-14 12:16:41 -0700130 }
131
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;
Simon Wilson85dc38f2012-05-15 17:37:19 -0700140 header.bits_per_sample = bits;
Simon Wilson7153bff2011-07-14 12:16:41 -0700141 header.byte_rate = (header.bits_per_sample / 8) * channels * rate;
142 header.block_align = channels * (header.bits_per_sample / 8);
Simon Wilson7153bff2011-07-14 12:16:41 -0700143 header.data_id = ID_DATA;
144
145 /* leave enough room for header */
146 fseek(file, sizeof(struct wav_header), SEEK_SET);
147
148 /* install signal handler and begin capturing */
149 signal(SIGINT, sigint_handler);
Simon Wilsondaa83292012-02-28 15:26:02 -0800150 frames = capture_sample(file, card, device, header.num_channels,
151 header.sample_rate, header.bits_per_sample,
152 period_size, period_count);
Simon Wilson7153bff2011-07-14 12:16:41 -0700153 printf("Captured %d frames\n", frames);
154
155 /* write header now all information is known */
156 header.data_sz = frames * header.block_align;
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800157 header.riff_sz = header.data_sz + sizeof(header) - 8;
Simon Wilson7153bff2011-07-14 12:16:41 -0700158 fseek(file, 0, SEEK_SET);
159 fwrite(&header, sizeof(struct wav_header), 1, file);
160
161 fclose(file);
162
163 return 0;
164}
165
Simon Wilsondaa83292012-02-28 15:26:02 -0800166unsigned int capture_sample(FILE *file, unsigned int card, unsigned int device,
Simon Wilson7153bff2011-07-14 12:16:41 -0700167 unsigned int channels, unsigned int rate,
Simon Wilsondaa83292012-02-28 15:26:02 -0800168 unsigned int bits, unsigned int period_size,
169 unsigned int period_count)
Simon Wilson7153bff2011-07-14 12:16:41 -0700170{
171 struct pcm_config config;
172 struct pcm *pcm;
173 char *buffer;
174 unsigned int size;
175 unsigned int bytes_read = 0;
176
177 config.channels = channels;
178 config.rate = rate;
Simon Wilsondaa83292012-02-28 15:26:02 -0800179 config.period_size = period_size;
180 config.period_count = period_count;
Simon Wilson7153bff2011-07-14 12:16:41 -0700181 if (bits == 32)
182 config.format = PCM_FORMAT_S32_LE;
183 else if (bits == 16)
184 config.format = PCM_FORMAT_S16_LE;
Simon Wilson62104732011-08-05 12:00:00 -0700185 config.start_threshold = 0;
186 config.stop_threshold = 0;
187 config.silence_threshold = 0;
Simon Wilson7153bff2011-07-14 12:16:41 -0700188
Simon Wilsondaa83292012-02-28 15:26:02 -0800189 pcm = pcm_open(card, device, PCM_IN, &config);
Simon Wilson7153bff2011-07-14 12:16:41 -0700190 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 }
195
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 }
204
205 printf("Capturing sample: %u ch, %u hz, %u bit\n", channels, rate, bits);
206
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 }
214
215 free(buffer);
216 pcm_close(pcm);
217 return bytes_read / ((bits / 8) * channels);
218}
219