Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 1 | /* tinyplay.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> |
Simon Wilson | da39e0b | 2012-11-09 15:16:47 -0800 | [diff] [blame] | 33 | #include <string.h> |
| 34 | #include <signal.h> |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 35 | #include <endian.h> |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -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 | |
Simon Wilson | 85dc38f | 2012-05-15 17:37:19 -0700 | [diff] [blame] | 42 | struct riff_wave_header { |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 43 | uint32_t riff_id; |
| 44 | uint32_t riff_sz; |
Simon Wilson | 85dc38f | 2012-05-15 17:37:19 -0700 | [diff] [blame] | 45 | uint32_t wave_id; |
| 46 | }; |
| 47 | |
| 48 | struct chunk_header { |
| 49 | uint32_t id; |
| 50 | uint32_t sz; |
| 51 | }; |
| 52 | |
| 53 | struct chunk_fmt { |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 54 | uint16_t audio_format; |
| 55 | uint16_t num_channels; |
| 56 | uint32_t sample_rate; |
| 57 | uint32_t byte_rate; |
| 58 | uint16_t block_align; |
| 59 | uint16_t bits_per_sample; |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 60 | }; |
| 61 | |
Simon Wilson | da39e0b | 2012-11-09 15:16:47 -0800 | [diff] [blame] | 62 | static int close = 0; |
| 63 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 64 | void play_sample(FILE *file, unsigned int card, unsigned int device, unsigned int channels, |
| 65 | unsigned int rate, unsigned int bits, unsigned int period_size, |
| 66 | unsigned int period_count, uint32_t data_sz); |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 67 | |
Simon Wilson | da39e0b | 2012-11-09 15:16:47 -0800 | [diff] [blame] | 68 | void stream_close(int sig) |
| 69 | { |
| 70 | /* allow the stream to be closed gracefully */ |
| 71 | signal(sig, SIG_IGN); |
| 72 | close = 1; |
| 73 | } |
| 74 | |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 75 | int main(int argc, char **argv) |
| 76 | { |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 77 | FILE *file; |
| 78 | struct riff_wave_header riff_wave_header; |
| 79 | struct chunk_header chunk_header; |
| 80 | struct chunk_fmt chunk_fmt; |
| 81 | unsigned int device = 0; |
| 82 | unsigned int card = 0; |
| 83 | unsigned int period_size = 1024; |
| 84 | unsigned int period_count = 4; |
| 85 | char *filename; |
| 86 | int more_chunks = 1; |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 87 | |
Simon Wilson | 6210473 | 2011-08-05 12:00:00 -0700 | [diff] [blame] | 88 | if (argc < 2) { |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 89 | fprintf(stderr, "Usage: %s file.wav [-D card] [-d device] [-p period_size]" |
| 90 | " [-n n_periods] \n", argv[0]); |
| 91 | return 1; |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 92 | } |
| 93 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 94 | filename = argv[1]; |
| 95 | file = fopen(filename, "rb"); |
| 96 | if (!file) { |
| 97 | fprintf(stderr, "Unable to open file '%s'\n", filename); |
| 98 | return 1; |
| 99 | } |
| 100 | |
| 101 | fread(&riff_wave_header, sizeof(riff_wave_header), 1, file); |
| 102 | if ((riff_wave_header.riff_id != ID_RIFF) || |
| 103 | (riff_wave_header.wave_id != ID_WAVE)) { |
| 104 | fprintf(stderr, "Error: '%s' is not a riff/wave file\n", filename); |
| 105 | fclose(file); |
| 106 | return 1; |
| 107 | } |
| 108 | |
| 109 | do { |
| 110 | fread(&chunk_header, sizeof(chunk_header), 1, file); |
| 111 | |
| 112 | switch (chunk_header.id) { |
| 113 | case ID_FMT: |
| 114 | fread(&chunk_fmt, sizeof(chunk_fmt), 1, file); |
| 115 | /* If the format header is larger, skip the rest */ |
| 116 | if (chunk_header.sz > sizeof(chunk_fmt)) |
| 117 | fseek(file, chunk_header.sz - sizeof(chunk_fmt), SEEK_CUR); |
Simon Wilson | 85dc38f | 2012-05-15 17:37:19 -0700 | [diff] [blame] | 118 | break; |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 119 | case ID_DATA: |
| 120 | /* Stop looking for chunks */ |
| 121 | more_chunks = 0; |
| 122 | chunk_header.sz = le32toh(chunk_header.sz); |
Simon Wilson | 85dc38f | 2012-05-15 17:37:19 -0700 | [diff] [blame] | 123 | break; |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 124 | default: |
| 125 | /* Unknown chunk, skip bytes */ |
| 126 | fseek(file, chunk_header.sz, SEEK_CUR); |
Simon Wilson | 85dc38f | 2012-05-15 17:37:19 -0700 | [diff] [blame] | 127 | } |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 128 | } while (more_chunks); |
Simon Wilson | 85dc38f | 2012-05-15 17:37:19 -0700 | [diff] [blame] | 129 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 130 | /* parse command line arguments */ |
| 131 | argv += 2; |
| 132 | while (*argv) { |
| 133 | if (strcmp(*argv, "-d") == 0) { |
| 134 | argv++; |
| 135 | if (*argv) |
| 136 | device = atoi(*argv); |
| 137 | } |
| 138 | if (strcmp(*argv, "-p") == 0) { |
| 139 | argv++; |
| 140 | if (*argv) |
| 141 | period_size = atoi(*argv); |
| 142 | } |
| 143 | if (strcmp(*argv, "-n") == 0) { |
| 144 | argv++; |
| 145 | if (*argv) |
| 146 | period_count = atoi(*argv); |
| 147 | } |
| 148 | if (strcmp(*argv, "-D") == 0) { |
| 149 | argv++; |
| 150 | if (*argv) |
| 151 | card = atoi(*argv); |
| 152 | } |
| 153 | if (*argv) |
| 154 | argv++; |
Simon Wilson | 6210473 | 2011-08-05 12:00:00 -0700 | [diff] [blame] | 155 | } |
| 156 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 157 | play_sample(file, card, device, chunk_fmt.num_channels, chunk_fmt.sample_rate, |
| 158 | chunk_fmt.bits_per_sample, period_size, period_count, chunk_header.sz); |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 159 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 160 | fclose(file); |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 161 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 162 | return 0; |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Simon Wilson | 9673f57 | 2013-05-01 15:10:34 -0700 | [diff] [blame] | 165 | int check_param(struct pcm_params *params, unsigned int param, unsigned int value, |
| 166 | char *param_name, char *param_unit) |
| 167 | { |
| 168 | unsigned int min; |
| 169 | unsigned int max; |
| 170 | int is_within_bounds = 1; |
| 171 | |
| 172 | min = pcm_params_get_min(params, param); |
| 173 | if (value < min) { |
| 174 | fprintf(stderr, "%s is %u%s, device only supports >= %u%s\n", param_name, value, |
| 175 | param_unit, min, param_unit); |
| 176 | is_within_bounds = 0; |
| 177 | } |
| 178 | |
| 179 | max = pcm_params_get_max(params, param); |
| 180 | if (value > max) { |
| 181 | fprintf(stderr, "%s is %u%s, device only supports <= %u%s\n", param_name, value, |
| 182 | param_unit, max, param_unit); |
| 183 | is_within_bounds = 0; |
| 184 | } |
| 185 | |
| 186 | return is_within_bounds; |
| 187 | } |
| 188 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 189 | int sample_is_playable(unsigned int card, unsigned int device, unsigned int channels, |
| 190 | unsigned int rate, unsigned int bits, unsigned int period_size, |
| 191 | unsigned int period_count) |
Simon Wilson | 9673f57 | 2013-05-01 15:10:34 -0700 | [diff] [blame] | 192 | { |
| 193 | struct pcm_params *params; |
| 194 | int can_play; |
| 195 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 196 | params = pcm_params_get(card, device, PCM_OUT); |
Simon Wilson | 9673f57 | 2013-05-01 15:10:34 -0700 | [diff] [blame] | 197 | if (params == NULL) { |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 198 | fprintf(stderr, "Unable to open PCM device %u.\n", device); |
Simon Wilson | 9673f57 | 2013-05-01 15:10:34 -0700 | [diff] [blame] | 199 | return 0; |
| 200 | } |
| 201 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 202 | can_play = check_param(params, PCM_PARAM_RATE, rate, "Sample rate", "Hz"); |
| 203 | can_play &= check_param(params, PCM_PARAM_CHANNELS, channels, "Sample", " channels"); |
Xinhui Zhou | acd5599 | 2021-10-13 22:53:10 -0700 | [diff] [blame] | 204 | can_play &= check_param(params, PCM_PARAM_SAMPLE_BITS, bits, "Bitwidth", " bits"); |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 205 | can_play &= check_param(params, PCM_PARAM_PERIOD_SIZE, period_size, "Period size", " frames"); |
| 206 | can_play &= check_param(params, PCM_PARAM_PERIODS, period_count, "Period count", " periods"); |
Simon Wilson | 9673f57 | 2013-05-01 15:10:34 -0700 | [diff] [blame] | 207 | |
| 208 | pcm_params_free(params); |
| 209 | |
| 210 | return can_play; |
| 211 | } |
| 212 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 213 | void play_sample(FILE *file, unsigned int card, unsigned int device, unsigned int channels, |
| 214 | unsigned int rate, unsigned int bits, unsigned int period_size, |
| 215 | unsigned int period_count, uint32_t data_sz) |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 216 | { |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 217 | struct pcm_config config; |
| 218 | struct pcm *pcm; |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 219 | char *buffer; |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 220 | unsigned int size, read_sz; |
| 221 | int num_read; |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 222 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 223 | memset(&config, 0, sizeof(config)); |
| 224 | config.channels = channels; |
| 225 | config.rate = rate; |
| 226 | config.period_size = period_size; |
| 227 | config.period_count = period_count; |
| 228 | if (bits == 32) |
| 229 | config.format = PCM_FORMAT_S32_LE; |
| 230 | else if (bits == 24) |
| 231 | config.format = PCM_FORMAT_S24_3LE; |
| 232 | else if (bits == 16) |
| 233 | config.format = PCM_FORMAT_S16_LE; |
| 234 | config.start_threshold = 0; |
| 235 | config.stop_threshold = 0; |
| 236 | config.silence_threshold = 0; |
| 237 | |
| 238 | if (!sample_is_playable(card, device, channels, rate, bits, period_size, period_count)) { |
| 239 | return; |
Simon Wilson | 9673f57 | 2013-05-01 15:10:34 -0700 | [diff] [blame] | 240 | } |
| 241 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 242 | pcm = pcm_open(card, device, PCM_OUT, &config); |
| 243 | if (!pcm || !pcm_is_ready(pcm)) { |
| 244 | fprintf(stderr, "Unable to open PCM device %u (%s)\n", |
| 245 | device, pcm_get_error(pcm)); |
| 246 | return; |
| 247 | } |
| 248 | |
| 249 | size = pcm_frames_to_bytes(pcm, pcm_get_buffer_size(pcm)); |
| 250 | buffer = malloc(size); |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 251 | if (!buffer) { |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 252 | fprintf(stderr, "Unable to allocate %d bytes\n", size); |
| 253 | free(buffer); |
| 254 | pcm_close(pcm); |
| 255 | return; |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 256 | } |
| 257 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 258 | printf("Playing sample: %u ch, %u hz, %u bit %u bytes\n", channels, rate, bits, data_sz); |
| 259 | |
Simon Wilson | da39e0b | 2012-11-09 15:16:47 -0800 | [diff] [blame] | 260 | /* catch ctrl-c to shutdown cleanly */ |
| 261 | signal(SIGINT, stream_close); |
| 262 | |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 263 | do { |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 264 | read_sz = size < data_sz ? size : data_sz; |
| 265 | num_read = fread(buffer, 1, read_sz, file); |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 266 | if (num_read > 0) { |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 267 | if (pcm_write(pcm, buffer, num_read)) { |
| 268 | fprintf(stderr, "Error playing sample\n"); |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 269 | break; |
| 270 | } |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 271 | data_sz -= num_read; |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 272 | } |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 273 | } while (!close && num_read > 0 && data_sz > 0); |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 274 | |
| 275 | free(buffer); |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 276 | pcm_close(pcm); |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 277 | } |
| 278 | |