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> |
Simon Wilson | 7153bff | 2011-07-14 12:16:41 -0700 | [diff] [blame] | 35 | |
| 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 | |
| 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 | }; |
| 58 | |
| 59 | int capturing = 1; |
| 60 | |
Simon Wilson | daa8329 | 2012-02-28 15:26:02 -0800 | [diff] [blame] | 61 | unsigned int capture_sample(FILE *file, unsigned int card, unsigned int device, |
Simon Wilson | 7153bff | 2011-07-14 12:16:41 -0700 | [diff] [blame] | 62 | unsigned int channels, unsigned int rate, |
Simon Wilson | daa8329 | 2012-02-28 15:26:02 -0800 | [diff] [blame] | 63 | unsigned int bits, unsigned int period_size, |
| 64 | unsigned int period_count); |
Simon Wilson | 7153bff | 2011-07-14 12:16:41 -0700 | [diff] [blame] | 65 | |
| 66 | void sigint_handler(int sig) |
| 67 | { |
| 68 | capturing = 0; |
| 69 | } |
| 70 | |
| 71 | int main(int argc, char **argv) |
| 72 | { |
| 73 | FILE *file; |
| 74 | struct wav_header header; |
Simon Wilson | daa8329 | 2012-02-28 15:26:02 -0800 | [diff] [blame] | 75 | unsigned int card = 0; |
Simon Wilson | 7153bff | 2011-07-14 12:16:41 -0700 | [diff] [blame] | 76 | unsigned int device = 0; |
| 77 | unsigned int channels = 2; |
| 78 | unsigned int rate = 44100; |
| 79 | unsigned int bits = 16; |
| 80 | unsigned int frames; |
Simon Wilson | daa8329 | 2012-02-28 15:26:02 -0800 | [diff] [blame] | 81 | unsigned int period_size = 1024; |
| 82 | unsigned int period_count = 4; |
Simon Wilson | 7153bff | 2011-07-14 12:16:41 -0700 | [diff] [blame] | 83 | |
| 84 | if (argc < 2) { |
Simon Wilson | daa8329 | 2012-02-28 15:26:02 -0800 | [diff] [blame] | 85 | fprintf(stderr, "Usage: %s file.wav [-D card] [-d device] [-c channels] " |
Simon Wilson | e44e30a | 2012-03-08 10:45:31 -0800 | [diff] [blame] | 86 | "[-r rate] [-b bits] [-p period_size] [-n n_periods]\n", argv[0]); |
Simon Wilson | 7153bff | 2011-07-14 12:16:41 -0700 | [diff] [blame] | 87 | 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 Wilson | dd88f13 | 2011-07-25 10:58:30 -0700 | [diff] [blame] | 99 | if (strcmp(*argv, "-d") == 0) { |
Simon Wilson | 7153bff | 2011-07-14 12:16:41 -0700 | [diff] [blame] | 100 | argv++; |
Simon Wilson | daa8329 | 2012-02-28 15:26:02 -0800 | [diff] [blame] | 101 | if (*argv) |
| 102 | device = atoi(*argv); |
Simon Wilson | dd88f13 | 2011-07-25 10:58:30 -0700 | [diff] [blame] | 103 | } else if (strcmp(*argv, "-c") == 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 | channels = atoi(*argv); |
Simon Wilson | dd88f13 | 2011-07-25 10:58:30 -0700 | [diff] [blame] | 107 | } else if (strcmp(*argv, "-r") == 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 | rate = atoi(*argv); |
Simon Wilson | dd88f13 | 2011-07-25 10:58:30 -0700 | [diff] [blame] | 111 | } else if (strcmp(*argv, "-b") == 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 | 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 Wilson | 7153bff | 2011-07-14 12:16:41 -0700 | [diff] [blame] | 127 | } |
Simon Wilson | daa8329 | 2012-02-28 15:26:02 -0800 | [diff] [blame] | 128 | if (*argv) |
| 129 | argv++; |
Simon Wilson | 7153bff | 2011-07-14 12:16:41 -0700 | [diff] [blame] | 130 | } |
| 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 Wilson | 85dc38f | 2012-05-15 17:37:19 -0700 | [diff] [blame] | 140 | header.bits_per_sample = bits; |
Simon Wilson | 7153bff | 2011-07-14 12:16:41 -0700 | [diff] [blame] | 141 | header.byte_rate = (header.bits_per_sample / 8) * channels * rate; |
| 142 | header.block_align = channels * (header.bits_per_sample / 8); |
Simon Wilson | 7153bff | 2011-07-14 12:16:41 -0700 | [diff] [blame] | 143 | 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 Wilson | daa8329 | 2012-02-28 15:26:02 -0800 | [diff] [blame] | 150 | frames = capture_sample(file, card, device, header.num_channels, |
| 151 | header.sample_rate, header.bits_per_sample, |
| 152 | period_size, period_count); |
Simon Wilson | 7153bff | 2011-07-14 12:16:41 -0700 | [diff] [blame] | 153 | printf("Captured %d frames\n", frames); |
| 154 | |
| 155 | /* write header now all information is known */ |
| 156 | header.data_sz = frames * header.block_align; |
Simon Wilson | da39e0b | 2012-11-09 15:16:47 -0800 | [diff] [blame^] | 157 | header.riff_sz = header.data_sz + sizeof(header) - 8; |
Simon Wilson | 7153bff | 2011-07-14 12:16:41 -0700 | [diff] [blame] | 158 | 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 Wilson | daa8329 | 2012-02-28 15:26:02 -0800 | [diff] [blame] | 166 | unsigned int capture_sample(FILE *file, unsigned int card, unsigned int device, |
Simon Wilson | 7153bff | 2011-07-14 12:16:41 -0700 | [diff] [blame] | 167 | unsigned int channels, unsigned int rate, |
Simon Wilson | daa8329 | 2012-02-28 15:26:02 -0800 | [diff] [blame] | 168 | unsigned int bits, unsigned int period_size, |
| 169 | unsigned int period_count) |
Simon Wilson | 7153bff | 2011-07-14 12:16:41 -0700 | [diff] [blame] | 170 | { |
| 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 Wilson | daa8329 | 2012-02-28 15:26:02 -0800 | [diff] [blame] | 179 | config.period_size = period_size; |
| 180 | config.period_count = period_count; |
Simon Wilson | 7153bff | 2011-07-14 12:16:41 -0700 | [diff] [blame] | 181 | if (bits == 32) |
| 182 | config.format = PCM_FORMAT_S32_LE; |
| 183 | else if (bits == 16) |
| 184 | config.format = PCM_FORMAT_S16_LE; |
Simon Wilson | 6210473 | 2011-08-05 12:00:00 -0700 | [diff] [blame] | 185 | config.start_threshold = 0; |
| 186 | config.stop_threshold = 0; |
| 187 | config.silence_threshold = 0; |
Simon Wilson | 7153bff | 2011-07-14 12:16:41 -0700 | [diff] [blame] | 188 | |
Simon Wilson | daa8329 | 2012-02-28 15:26:02 -0800 | [diff] [blame] | 189 | pcm = pcm_open(card, device, PCM_IN, &config); |
Simon Wilson | 7153bff | 2011-07-14 12:16:41 -0700 | [diff] [blame] | 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 | } |
| 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 | |