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