Simon Wilson | 7153bff | 2011-07-14 12:16:41 -0700 | [diff] [blame] | 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 | */ |
| 28 | |
| 29 | #include <tinyalsa/asoundlib.h> |
| 30 | #include <stdio.h> |
| 31 | #include <stdlib.h> |
| 32 | #include <stdint.h> |
| 33 | #include <signal.h> |
Simon Wilson | da39e0b | 2012-11-09 15:16:47 -0800 | [diff] [blame] | 34 | #include <string.h> |
John Muir | 47d3951 | 2017-07-05 15:57:35 -0700 | [diff] [blame] | 35 | #include <time.h> |
Simon Wilson | 7153bff | 2011-07-14 12:16:41 -0700 | [diff] [blame] | 36 | |
| 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 | |
| 44 | struct 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 | |
| 60 | int capturing = 1; |
| 61 | |
Simon Wilson | daa8329 | 2012-02-28 15:26:02 -0800 | [diff] [blame] | 62 | unsigned int capture_sample(FILE *file, unsigned int card, unsigned int device, |
Simon Wilson | 7153bff | 2011-07-14 12:16:41 -0700 | [diff] [blame] | 63 | unsigned int channels, unsigned int rate, |
Simon Wilson | 36ea2d8 | 2013-07-17 11:10:45 -0700 | [diff] [blame] | 64 | enum pcm_format format, unsigned int period_size, |
John Muir | 47d3951 | 2017-07-05 15:57:35 -0700 | [diff] [blame] | 65 | unsigned int period_count, unsigned int cap_time); |
Simon Wilson | 7153bff | 2011-07-14 12:16:41 -0700 | [diff] [blame] | 66 | |
Glenn Kasten | fde0bf1 | 2016-02-08 14:42:36 -0800 | [diff] [blame] | 67 | void sigint_handler(int sig __unused) |
Simon Wilson | 7153bff | 2011-07-14 12:16:41 -0700 | [diff] [blame] | 68 | { |
| 69 | capturing = 0; |
| 70 | } |
| 71 | |
| 72 | int main(int argc, char **argv) |
| 73 | { |
| 74 | FILE *file; |
| 75 | struct wav_header header; |
Simon Wilson | daa8329 | 2012-02-28 15:26:02 -0800 | [diff] [blame] | 76 | unsigned int card = 0; |
Simon Wilson | 7153bff | 2011-07-14 12:16:41 -0700 | [diff] [blame] | 77 | unsigned int device = 0; |
| 78 | unsigned int channels = 2; |
| 79 | unsigned int rate = 44100; |
| 80 | unsigned int bits = 16; |
| 81 | unsigned int frames; |
Simon Wilson | daa8329 | 2012-02-28 15:26:02 -0800 | [diff] [blame] | 82 | unsigned int period_size = 1024; |
| 83 | unsigned int period_count = 4; |
John Muir | 47d3951 | 2017-07-05 15:57:35 -0700 | [diff] [blame] | 84 | unsigned int cap_time = 0; |
Simon Wilson | 36ea2d8 | 2013-07-17 11:10:45 -0700 | [diff] [blame] | 85 | enum pcm_format format; |
Simon Wilson | 7153bff | 2011-07-14 12:16:41 -0700 | [diff] [blame] | 86 | |
| 87 | if (argc < 2) { |
John Muir | 47d3951 | 2017-07-05 15:57:35 -0700 | [diff] [blame] | 88 | 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 Wilson | 7153bff | 2011-07-14 12:16:41 -0700 | [diff] [blame] | 91 | 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 Wilson | dd88f13 | 2011-07-25 10:58:30 -0700 | [diff] [blame] | 103 | if (strcmp(*argv, "-d") == 0) { |
Simon Wilson | 7153bff | 2011-07-14 12:16:41 -0700 | [diff] [blame] | 104 | argv++; |
Simon Wilson | daa8329 | 2012-02-28 15:26:02 -0800 | [diff] [blame] | 105 | if (*argv) |
| 106 | device = atoi(*argv); |
Simon Wilson | dd88f13 | 2011-07-25 10:58:30 -0700 | [diff] [blame] | 107 | } else if (strcmp(*argv, "-c") == 0) { |
Simon Wilson | 7153bff | 2011-07-14 12:16:41 -0700 | [diff] [blame] | 108 | argv++; |
Simon Wilson | daa8329 | 2012-02-28 15:26:02 -0800 | [diff] [blame] | 109 | if (*argv) |
| 110 | channels = atoi(*argv); |
Simon Wilson | dd88f13 | 2011-07-25 10:58:30 -0700 | [diff] [blame] | 111 | } else if (strcmp(*argv, "-r") == 0) { |
Simon Wilson | 7153bff | 2011-07-14 12:16:41 -0700 | [diff] [blame] | 112 | argv++; |
Simon Wilson | daa8329 | 2012-02-28 15:26:02 -0800 | [diff] [blame] | 113 | if (*argv) |
| 114 | rate = atoi(*argv); |
Simon Wilson | dd88f13 | 2011-07-25 10:58:30 -0700 | [diff] [blame] | 115 | } else if (strcmp(*argv, "-b") == 0) { |
Simon Wilson | 7153bff | 2011-07-14 12:16:41 -0700 | [diff] [blame] | 116 | argv++; |
Simon Wilson | daa8329 | 2012-02-28 15:26:02 -0800 | [diff] [blame] | 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); |
John Muir | 47d3951 | 2017-07-05 15:57:35 -0700 | [diff] [blame] | 131 | } else if (strcmp(*argv, "-T") == 0) { |
| 132 | argv++; |
| 133 | if (*argv) |
| 134 | cap_time = atoi(*argv); |
Simon Wilson | 7153bff | 2011-07-14 12:16:41 -0700 | [diff] [blame] | 135 | } |
Simon Wilson | daa8329 | 2012-02-28 15:26:02 -0800 | [diff] [blame] | 136 | if (*argv) |
| 137 | argv++; |
Simon Wilson | 7153bff | 2011-07-14 12:16:41 -0700 | [diff] [blame] | 138 | } |
| 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 Wilson | 36ea2d8 | 2013-07-17 11:10:45 -0700 | [diff] [blame] | 148 | |
| 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 Wilson | 7153bff | 2011-07-14 12:16:41 -0700 | [diff] [blame] | 165 | header.byte_rate = (header.bits_per_sample / 8) * channels * rate; |
| 166 | header.block_align = channels * (header.bits_per_sample / 8); |
Simon Wilson | 7153bff | 2011-07-14 12:16:41 -0700 | [diff] [blame] | 167 | 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 Muir | 47d3951 | 2017-07-05 15:57:35 -0700 | [diff] [blame] | 174 | signal(SIGHUP, sigint_handler); |
| 175 | signal(SIGTERM, sigint_handler); |
Simon Wilson | daa8329 | 2012-02-28 15:26:02 -0800 | [diff] [blame] | 176 | frames = capture_sample(file, card, device, header.num_channels, |
Simon Wilson | 36ea2d8 | 2013-07-17 11:10:45 -0700 | [diff] [blame] | 177 | header.sample_rate, format, |
John Muir | 47d3951 | 2017-07-05 15:57:35 -0700 | [diff] [blame] | 178 | period_size, period_count, cap_time); |
Simon Wilson | 7153bff | 2011-07-14 12:16:41 -0700 | [diff] [blame] | 179 | printf("Captured %d frames\n", frames); |
| 180 | |
| 181 | /* write header now all information is known */ |
| 182 | header.data_sz = frames * header.block_align; |
Simon Wilson | da39e0b | 2012-11-09 15:16:47 -0800 | [diff] [blame] | 183 | header.riff_sz = header.data_sz + sizeof(header) - 8; |
Simon Wilson | 7153bff | 2011-07-14 12:16:41 -0700 | [diff] [blame] | 184 | 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 Wilson | daa8329 | 2012-02-28 15:26:02 -0800 | [diff] [blame] | 192 | unsigned int capture_sample(FILE *file, unsigned int card, unsigned int device, |
Simon Wilson | 7153bff | 2011-07-14 12:16:41 -0700 | [diff] [blame] | 193 | unsigned int channels, unsigned int rate, |
Simon Wilson | 36ea2d8 | 2013-07-17 11:10:45 -0700 | [diff] [blame] | 194 | enum pcm_format format, unsigned int period_size, |
John Muir | 47d3951 | 2017-07-05 15:57:35 -0700 | [diff] [blame] | 195 | unsigned int period_count, unsigned int cap_time) |
Simon Wilson | 7153bff | 2011-07-14 12:16:41 -0700 | [diff] [blame] | 196 | { |
| 197 | struct pcm_config config; |
| 198 | struct pcm *pcm; |
| 199 | char *buffer; |
| 200 | unsigned int size; |
| 201 | unsigned int bytes_read = 0; |
John Muir | 47d3951 | 2017-07-05 15:57:35 -0700 | [diff] [blame] | 202 | struct timespec end; |
| 203 | struct timespec now; |
Simon Wilson | 7153bff | 2011-07-14 12:16:41 -0700 | [diff] [blame] | 204 | |
Haynes Mathew George | 875ff85 | 2015-08-25 12:47:13 -0700 | [diff] [blame] | 205 | memset(&config, 0, sizeof(config)); |
Simon Wilson | 7153bff | 2011-07-14 12:16:41 -0700 | [diff] [blame] | 206 | config.channels = channels; |
| 207 | config.rate = rate; |
Simon Wilson | daa8329 | 2012-02-28 15:26:02 -0800 | [diff] [blame] | 208 | config.period_size = period_size; |
| 209 | config.period_count = period_count; |
Simon Wilson | 36ea2d8 | 2013-07-17 11:10:45 -0700 | [diff] [blame] | 210 | config.format = format; |
Simon Wilson | 6210473 | 2011-08-05 12:00:00 -0700 | [diff] [blame] | 211 | config.start_threshold = 0; |
| 212 | config.stop_threshold = 0; |
| 213 | config.silence_threshold = 0; |
Simon Wilson | 7153bff | 2011-07-14 12:16:41 -0700 | [diff] [blame] | 214 | |
Simon Wilson | daa8329 | 2012-02-28 15:26:02 -0800 | [diff] [blame] | 215 | pcm = pcm_open(card, device, PCM_IN, &config); |
Simon Wilson | 7153bff | 2011-07-14 12:16:41 -0700 | [diff] [blame] | 216 | 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 Wilson | 9673f57 | 2013-05-01 15:10:34 -0700 | [diff] [blame] | 222 | size = pcm_frames_to_bytes(pcm, pcm_get_buffer_size(pcm)); |
Simon Wilson | 7153bff | 2011-07-14 12:16:41 -0700 | [diff] [blame] | 223 | 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 Wilson | 36ea2d8 | 2013-07-17 11:10:45 -0700 | [diff] [blame] | 231 | printf("Capturing sample: %u ch, %u hz, %u bit\n", channels, rate, |
| 232 | pcm_format_to_bits(format)); |
Simon Wilson | 7153bff | 2011-07-14 12:16:41 -0700 | [diff] [blame] | 233 | |
John Muir | 47d3951 | 2017-07-05 15:57:35 -0700 | [diff] [blame] | 234 | clock_gettime(CLOCK_MONOTONIC, &now); |
| 235 | end.tv_sec = now.tv_sec + cap_time; |
| 236 | end.tv_nsec = now.tv_nsec; |
| 237 | |
Simon Wilson | 7153bff | 2011-07-14 12:16:41 -0700 | [diff] [blame] | 238 | 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 Muir | 47d3951 | 2017-07-05 15:57:35 -0700 | [diff] [blame] | 244 | 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 Wilson | 7153bff | 2011-07-14 12:16:41 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | free(buffer); |
| 253 | pcm_close(pcm); |
Simon Wilson | 36ea2d8 | 2013-07-17 11:10:45 -0700 | [diff] [blame] | 254 | return pcm_bytes_to_frames(pcm, bytes_read); |
Simon Wilson | 7153bff | 2011-07-14 12:16:41 -0700 | [diff] [blame] | 255 | } |