Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 1 | /* |
| 2 | ** Copyright 2010, The Android Open-Source Project |
| 3 | ** Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved. |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #include <stdio.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <fcntl.h> |
| 21 | #include <unistd.h> |
| 22 | #include <stdint.h> |
| 23 | #include <string.h> |
| 24 | #include <errno.h> |
| 25 | #include <sys/poll.h> |
| 26 | #include <sys/ioctl.h> |
| 27 | #include <getopt.h> |
| 28 | |
| 29 | #include <sound/asound.h> |
| 30 | #include "alsa_audio.h" |
| 31 | |
| 32 | #ifndef ANDROID |
| 33 | #define strlcat g_strlcat |
| 34 | #define strlcpy g_strlcpy |
| 35 | #endif |
| 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 | #define LOG_NDEBUG 1 |
| 44 | static pcm_flag = 1; |
| 45 | static debug = 0; |
| 46 | static uint32_t play_max_sz = 2147483648LL; |
| 47 | static int format = SNDRV_PCM_FORMAT_S16_LE; |
| 48 | static int period = 0; |
| 49 | static int compressed = 0; |
| 50 | static char *compr_codec; |
| 51 | static int piped = 0; |
| 52 | |
| 53 | static struct option long_options[] = |
| 54 | { |
| 55 | {"pcm", 0, 0, 'P'}, |
| 56 | {"debug", 0, 0, 'V'}, |
| 57 | {"Mmap", 0, 0, 'M'}, |
| 58 | {"HW", 1, 0, 'D'}, |
| 59 | {"Rate", 1, 0, 'R'}, |
| 60 | {"channel", 1, 0, 'C'}, |
| 61 | {"format", 1, 0, 'F'}, |
| 62 | {"period", 1, 0, 'B'}, |
| 63 | {"compressed", 0, 0, 'T'}, |
| 64 | {0, 0, 0, 0} |
| 65 | }; |
| 66 | |
| 67 | struct wav_header { |
| 68 | uint32_t riff_id; |
| 69 | uint32_t riff_sz; |
| 70 | uint32_t riff_fmt; |
| 71 | uint32_t fmt_id; |
| 72 | uint32_t fmt_sz; |
| 73 | uint16_t audio_format; |
| 74 | uint16_t num_channels; |
| 75 | uint32_t sample_rate; |
| 76 | uint32_t byte_rate; /* sample_rate * num_channels * bps / 8 */ |
| 77 | uint16_t block_align; /* num_channels * bps / 8 */ |
| 78 | uint16_t bits_per_sample; |
| 79 | uint32_t data_id; |
| 80 | uint32_t data_sz; |
| 81 | }; |
| 82 | |
| 83 | static int set_params(struct pcm *pcm) |
| 84 | { |
| 85 | struct snd_pcm_hw_params *params; |
| 86 | struct snd_pcm_sw_params *sparams; |
| 87 | |
| 88 | unsigned long periodSize, bufferSize, reqBuffSize; |
| 89 | unsigned int periodTime, bufferTime; |
| 90 | unsigned int requestedRate = pcm->rate; |
| 91 | int channels = (pcm->flags & PCM_MONO) ? 1 : ((pcm->flags & PCM_5POINT1)? 6 : 2 ); |
| 92 | |
| 93 | params = (struct snd_pcm_hw_params*) calloc(1, sizeof(struct snd_pcm_hw_params)); |
| 94 | if (!params) { |
| 95 | fprintf(stderr, "Aplay:Failed to allocate ALSA hardware parameters!"); |
| 96 | return -ENOMEM; |
| 97 | } |
| 98 | |
| 99 | param_init(params); |
| 100 | |
| 101 | param_set_mask(params, SNDRV_PCM_HW_PARAM_ACCESS, |
| 102 | (pcm->flags & PCM_MMAP)? SNDRV_PCM_ACCESS_MMAP_INTERLEAVED : SNDRV_PCM_ACCESS_RW_INTERLEAVED); |
| 103 | param_set_mask(params, SNDRV_PCM_HW_PARAM_FORMAT, pcm->format); |
| 104 | param_set_mask(params, SNDRV_PCM_HW_PARAM_SUBFORMAT, |
| 105 | SNDRV_PCM_SUBFORMAT_STD); |
| 106 | if (period) |
| 107 | param_set_min(params, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, period); |
| 108 | else |
| 109 | param_set_min(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 10); |
| 110 | param_set_int(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, 16); |
| 111 | param_set_int(params, SNDRV_PCM_HW_PARAM_FRAME_BITS, |
| 112 | pcm->channels * 16); |
| 113 | param_set_int(params, SNDRV_PCM_HW_PARAM_CHANNELS, |
| 114 | pcm->channels); |
| 115 | param_set_int(params, SNDRV_PCM_HW_PARAM_RATE, pcm->rate); |
| 116 | param_set_hw_refine(pcm, params); |
| 117 | |
| 118 | if (param_set_hw_params(pcm, params)) { |
| 119 | fprintf(stderr, "Aplay:cannot set hw params\n"); |
| 120 | return -errno; |
| 121 | } |
| 122 | if (debug) |
| 123 | param_dump(params); |
| 124 | |
| 125 | pcm->buffer_size = pcm_buffer_size(params); |
| 126 | pcm->period_size = pcm_period_size(params); |
| 127 | pcm->period_cnt = pcm->buffer_size/pcm->period_size; |
| 128 | if (debug) { |
| 129 | fprintf (stderr,"period_cnt = %d\n", pcm->period_cnt); |
| 130 | fprintf (stderr,"period_size = %d\n", pcm->period_size); |
| 131 | fprintf (stderr,"buffer_size = %d\n", pcm->buffer_size); |
| 132 | } |
| 133 | sparams = (struct snd_pcm_sw_params*) calloc(1, sizeof(struct snd_pcm_sw_params)); |
| 134 | if (!sparams) { |
| 135 | fprintf(stderr, "Aplay:Failed to allocate ALSA software parameters!\n"); |
| 136 | return -ENOMEM; |
| 137 | } |
| 138 | // Get the current software parameters |
| 139 | sparams->tstamp_mode = SNDRV_PCM_TSTAMP_NONE; |
| 140 | sparams->period_step = 1; |
| 141 | |
| 142 | sparams->avail_min = pcm->period_size/(channels * 2) ; |
| 143 | sparams->start_threshold = pcm->period_size/(channels * 2) ; |
| 144 | sparams->stop_threshold = pcm->buffer_size ; |
| 145 | sparams->xfer_align = pcm->period_size/(channels * 2) ; /* needed for old kernels */ |
| 146 | |
| 147 | sparams->silence_size = 0; |
| 148 | sparams->silence_threshold = 0; |
| 149 | |
| 150 | if (param_set_sw_params(pcm, sparams)) { |
| 151 | fprintf(stderr, "Aplay:cannot set sw params"); |
| 152 | return -errno; |
| 153 | } |
| 154 | if (debug) { |
| 155 | fprintf (stderr,"sparams->avail_min= %lu\n", sparams->avail_min); |
| 156 | fprintf (stderr," sparams->start_threshold= %lu\n", sparams->start_threshold); |
| 157 | fprintf (stderr," sparams->stop_threshold= %lu\n", sparams->stop_threshold); |
| 158 | fprintf (stderr," sparams->xfer_align= %lu\n", sparams->xfer_align); |
| 159 | fprintf (stderr," sparams->boundary= %lu\n", sparams->boundary); |
| 160 | } |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | static int play_file(unsigned rate, unsigned channels, int fd, |
| 165 | unsigned flags, const char *device, unsigned data_sz) |
| 166 | { |
| 167 | struct pcm *pcm; |
| 168 | struct mixer *mixer; |
| 169 | struct pcm_ctl *ctl = NULL; |
| 170 | unsigned bufsize; |
| 171 | char *data; |
| 172 | long avail; |
| 173 | long frames; |
| 174 | int nfds = 1; |
| 175 | struct snd_xferi x; |
| 176 | unsigned offset = 0; |
| 177 | int err; |
| 178 | static int start = 0; |
| 179 | struct pollfd pfd[1]; |
| 180 | int remainingData = 0; |
| 181 | |
| 182 | flags |= PCM_OUT; |
| 183 | |
| 184 | if (channels == 1) |
| 185 | flags |= PCM_MONO; |
| 186 | else if (channels == 6) |
| 187 | flags |= PCM_5POINT1; |
| 188 | else |
| 189 | flags |= PCM_STEREO; |
| 190 | |
| 191 | if (debug) |
| 192 | flags |= DEBUG_ON; |
| 193 | else |
| 194 | flags |= DEBUG_OFF; |
| 195 | |
| 196 | pcm = pcm_open(flags, device); |
| 197 | if (pcm < 0) |
| 198 | return pcm; |
| 199 | |
| 200 | if (!pcm_ready(pcm)) { |
| 201 | pcm_close(pcm); |
| 202 | return -EBADFD; |
| 203 | } |
| 204 | |
| 205 | if (compressed) { |
| 206 | struct snd_compr_caps compr_cap; |
| 207 | struct snd_compr_params compr_params; |
| 208 | if (ioctl(pcm->fd, SNDRV_COMPRESS_GET_CAPS, &compr_cap)) { |
| 209 | fprintf(stderr, "Aplay: SNDRV_COMPRESS_GET_CAPS, failed Error no %d \n", errno); |
| 210 | pcm_close(pcm); |
| 211 | return -errno; |
| 212 | } |
| 213 | if (!period) |
| 214 | period = compr_cap.min_fragment_size; |
| 215 | switch (get_compressed_format(compr_codec)) { |
| 216 | case FORMAT_MP3: |
| 217 | compr_params.codec.id = compr_cap.codecs[FORMAT_MP3]; |
| 218 | break; |
| 219 | case FORMAT_AC3_PASS_THROUGH: |
| 220 | compr_params.codec.id = compr_cap.codecs[FORMAT_AC3_PASS_THROUGH]; |
| 221 | printf("codec -d = %x\n", compr_params.codec.id); |
| 222 | break; |
| 223 | default: |
| 224 | break; |
| 225 | } |
| 226 | if (ioctl(pcm->fd, SNDRV_COMPRESS_SET_PARAMS, &compr_params)) { |
| 227 | fprintf(stderr, "Aplay: SNDRV_COMPRESS_SET_PARAMS,failed Error no %d \n", errno); |
| 228 | pcm_close(pcm); |
| 229 | return -errno; |
| 230 | } |
| 231 | } |
| 232 | pcm->channels = channels; |
| 233 | pcm->rate = rate; |
| 234 | pcm->flags = flags; |
| 235 | pcm->format = format; |
| 236 | if (set_params(pcm)) { |
| 237 | fprintf(stderr, "Aplay:params setting failed\n"); |
| 238 | pcm_close(pcm); |
| 239 | return -errno; |
| 240 | } |
| 241 | |
| 242 | if (!pcm_flag) { |
| 243 | if (pcm_prepare(pcm)) { |
| 244 | fprintf(stderr, "Aplay:Failed in pcm_prepare\n"); |
| 245 | pcm_close(pcm); |
| 246 | return -errno; |
| 247 | } |
| 248 | if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_START)) { |
| 249 | fprintf(stderr, "Aplay: Hostless IOCTL_START Error no %d \n", errno); |
| 250 | pcm_close(pcm); |
| 251 | return -errno; |
| 252 | } |
| 253 | while(1); |
| 254 | } |
| 255 | |
| 256 | remainingData = data_sz; |
| 257 | |
| 258 | if (flags & PCM_MMAP) { |
| 259 | u_int8_t *dst_addr = NULL; |
| 260 | struct snd_pcm_sync_ptr *sync_ptr1 = pcm->sync_ptr; |
| 261 | if (mmap_buffer(pcm)) { |
| 262 | fprintf(stderr, "Aplay:params setting failed\n"); |
| 263 | pcm_close(pcm); |
| 264 | return -errno; |
| 265 | } |
| 266 | if (pcm_prepare(pcm)) { |
| 267 | fprintf(stderr, "Aplay:Failed in pcm_prepare\n"); |
| 268 | pcm_close(pcm); |
| 269 | return -errno; |
| 270 | } |
| 271 | |
| 272 | bufsize = pcm->period_size; |
| 273 | if (debug) |
| 274 | fprintf(stderr, "Aplay:bufsize = %d\n", bufsize); |
| 275 | |
| 276 | pfd[0].fd = pcm->timer_fd; |
| 277 | pfd[0].events = POLLIN; |
| 278 | |
| 279 | frames = (pcm->flags & PCM_MONO) ? (bufsize / 2) : (bufsize / 4); |
| 280 | for (;;) { |
| 281 | if (!pcm->running) { |
| 282 | if (pcm_prepare(pcm)) { |
| 283 | fprintf(stderr, "Aplay:Failed in pcm_prepare\n"); |
| 284 | pcm_close(pcm); |
| 285 | return -errno; |
| 286 | } |
| 287 | pcm->running = 1; |
| 288 | start = 0; |
| 289 | } |
| 290 | /* Sync the current Application pointer from the kernel */ |
| 291 | pcm->sync_ptr->flags = SNDRV_PCM_SYNC_PTR_APPL | SNDRV_PCM_SYNC_PTR_AVAIL_MIN;//SNDRV_PCM_SYNC_PTR_HWSYNC; |
| 292 | err = sync_ptr(pcm); |
| 293 | if (err == EPIPE) { |
| 294 | fprintf(stderr, "Aplay:Failed in sync_ptr \n"); |
| 295 | /* we failed to make our window -- try to restart */ |
| 296 | pcm->underruns++; |
| 297 | pcm->running = 0; |
| 298 | continue; |
| 299 | } |
| 300 | /* |
| 301 | * Check for the available buffer in driver. If available buffer is |
| 302 | * less than avail_min we need to wait |
| 303 | */ |
| 304 | avail = pcm_avail(pcm); |
| 305 | if (avail < 0) { |
| 306 | fprintf(stderr, "Aplay:Failed in pcm_avail\n"); |
| 307 | pcm_close(pcm); |
| 308 | return avail; |
| 309 | } |
| 310 | if (avail < pcm->sw_p->avail_min) { |
| 311 | poll(pfd, nfds, TIMEOUT_INFINITE); |
| 312 | continue; |
| 313 | } |
| 314 | /* |
| 315 | * Now that we have buffer size greater than avail_min available to |
| 316 | * to be written we need to calcutate the buffer offset where we can |
| 317 | * start writting. |
| 318 | */ |
| 319 | dst_addr = dst_address(pcm); |
| 320 | |
| 321 | if (debug) { |
| 322 | fprintf(stderr, "dst_addr = 0x%08x\n", dst_addr); |
| 323 | fprintf(stderr, "Aplay:avail = %d frames = %d\n",avail, frames); |
| 324 | fprintf(stderr, "Aplay:sync_ptr->s.status.hw_ptr %ld pcm->buffer_size %d sync_ptr->c.control.appl_ptr %ld\n", |
| 325 | pcm->sync_ptr->s.status.hw_ptr, |
| 326 | pcm->buffer_size, |
| 327 | pcm->sync_ptr->c.control.appl_ptr); |
| 328 | } |
| 329 | |
| 330 | /* |
| 331 | * Read from the file to the destination buffer in kernel mmaped buffer |
| 332 | * This reduces a extra copy of intermediate buffer. |
| 333 | */ |
| 334 | memset(dst_addr, 0x0, bufsize); |
| 335 | |
| 336 | if (data_sz && !piped) { |
| 337 | if (remainingData < bufsize) { |
| 338 | bufsize = remainingData; |
| 339 | frames = (pcm->flags & PCM_MONO) ? (remainingData / 2) : (remainingData / 4); |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | err = read(fd, dst_addr , bufsize); |
| 344 | if (debug) |
| 345 | fprintf(stderr, "read %d bytes from file\n", err); |
| 346 | if (err <= 0) |
| 347 | break; |
| 348 | |
| 349 | if (data_sz && !piped) { |
| 350 | remainingData -= bufsize; |
| 351 | if (remainingData <= 0) |
| 352 | break; |
| 353 | } |
| 354 | |
| 355 | /* |
| 356 | * Increment the application pointer with data written to kernel. |
| 357 | * Update kernel with the new sync pointer. |
| 358 | */ |
| 359 | pcm->sync_ptr->c.control.appl_ptr += frames; |
| 360 | pcm->sync_ptr->flags = 0; |
| 361 | |
| 362 | err = sync_ptr(pcm); |
| 363 | if (err == EPIPE) { |
| 364 | fprintf(stderr, "Aplay:Failed in sync_ptr 2 \n"); |
| 365 | /* we failed to make our window -- try to restart */ |
| 366 | pcm->underruns++; |
| 367 | pcm->running = 0; |
| 368 | continue; |
| 369 | } |
| 370 | |
| 371 | if (debug) { |
| 372 | fprintf(stderr, "Aplay:sync_ptr->s.status.hw_ptr %ld sync_ptr->c.control.appl_ptr %ld\n", |
| 373 | pcm->sync_ptr->s.status.hw_ptr, |
| 374 | pcm->sync_ptr->c.control.appl_ptr); |
| 375 | if (compressed && start) { |
| 376 | struct snd_compr_tstamp tstamp; |
| 377 | if (ioctl(pcm->fd, SNDRV_COMPRESS_TSTAMP, &tstamp)) |
| 378 | fprintf(stderr, "Aplay: failed SNDRV_COMPRESS_TSTAMP\n"); |
| 379 | else |
| 380 | fprintf(stderr, "timestamp = %lld\n", tstamp.timestamp); |
| 381 | } |
| 382 | } |
| 383 | /* |
| 384 | * If we have reached start threshold of buffer prefill, |
| 385 | * its time to start the driver. |
| 386 | */ |
| 387 | if(start) |
| 388 | goto start_done; |
| 389 | if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_START)) { |
| 390 | err = -errno; |
| 391 | if (errno == EPIPE) { |
| 392 | fprintf(stderr, "Aplay:Failed in SNDRV_PCM_IOCTL_START\n"); |
| 393 | /* we failed to make our window -- try to restart */ |
| 394 | pcm->underruns++; |
| 395 | pcm->running = 0; |
| 396 | continue; |
| 397 | } else { |
| 398 | fprintf(stderr, "Aplay:Error no %d \n", errno); |
| 399 | pcm_close(pcm); |
| 400 | return -errno; |
| 401 | } |
| 402 | } else |
| 403 | start = 1; |
| 404 | |
| 405 | start_done: |
| 406 | offset += frames; |
| 407 | } |
| 408 | while(1) { |
| 409 | pcm->sync_ptr->flags = SNDRV_PCM_SYNC_PTR_APPL | SNDRV_PCM_SYNC_PTR_AVAIL_MIN;//SNDRV_PCM_SYNC_PTR_HWSYNC; |
| 410 | sync_ptr(pcm); |
| 411 | /* |
| 412 | * Check for the available buffer in driver. If available buffer is |
| 413 | * less than avail_min we need to wait |
| 414 | */ |
| 415 | if (pcm->sync_ptr->s.status.hw_ptr >= pcm->sync_ptr->c.control.appl_ptr) { |
| 416 | fprintf(stderr, "Aplay:sync_ptr->s.status.hw_ptr %ld sync_ptr->c.control.appl_ptr %ld\n", |
| 417 | pcm->sync_ptr->s.status.hw_ptr, |
| 418 | pcm->sync_ptr->c.control.appl_ptr); |
| 419 | break; |
| 420 | } else |
| 421 | poll(pfd, nfds, TIMEOUT_INFINITE); |
| 422 | } |
| 423 | } else { |
| 424 | if (pcm_prepare(pcm)) { |
| 425 | fprintf(stderr, "Aplay:Failed in pcm_prepare\n"); |
| 426 | pcm_close(pcm); |
| 427 | return -errno; |
| 428 | } |
| 429 | |
| 430 | bufsize = pcm->period_size; |
| 431 | |
| 432 | data = calloc(1, bufsize); |
| 433 | if (!data) { |
| 434 | fprintf(stderr, "Aplay:could not allocate %d bytes\n", bufsize); |
| 435 | pcm_close(pcm); |
| 436 | return -ENOMEM; |
| 437 | } |
| 438 | |
| 439 | if (data_sz && !piped) { |
| 440 | if (remainingData < bufsize) |
| 441 | bufsize = remainingData; |
| 442 | } |
| 443 | |
| 444 | while (read(fd, data, bufsize) > 0) { |
| 445 | if (pcm_write(pcm, data, bufsize)){ |
| 446 | fprintf(stderr, "Aplay: pcm_write failed\n"); |
| 447 | free(data); |
| 448 | pcm_close(pcm); |
| 449 | return -errno; |
| 450 | } |
| 451 | memset(data, 0, bufsize); |
| 452 | |
| 453 | if (data_sz && !piped) { |
| 454 | remainingData -= bufsize; |
| 455 | if (remainingData <= 0) |
| 456 | break; |
| 457 | if (remainingData < bufsize) |
| 458 | bufsize = remainingData; |
| 459 | } |
| 460 | } |
| 461 | free(data); |
| 462 | } |
| 463 | fprintf(stderr, "Aplay: Done playing\n"); |
| 464 | pcm_close(pcm); |
| 465 | return 0; |
| 466 | } |
| 467 | |
| 468 | int play_raw(const char *fg, int rate, int ch, const char *device, const char *fn) |
| 469 | { |
| 470 | int fd; |
| 471 | unsigned flag = 0; |
| 472 | |
| 473 | if(!fn) { |
| 474 | fd = fileno(stdin); |
| 475 | piped = 1; |
| 476 | } else { |
| 477 | fd = open(fn, O_RDONLY); |
| 478 | if (fd < 0) { |
| 479 | fprintf(stderr, "Aplay:aplay: cannot open '%s'\n", fn); |
| 480 | return fd; |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | if (!strncmp(fg, "M", sizeof("M"))) |
| 485 | flag = PCM_MMAP; |
| 486 | else if (!strncmp(fg, "N", sizeof("N"))) |
| 487 | flag = PCM_NMMAP; |
| 488 | |
| 489 | fprintf(stderr, "aplay: Playing '%s': format %s ch = %d\n", |
| 490 | fn, get_format_desc(format), ch ); |
| 491 | return play_file(rate, ch, fd, flag, device, 0); |
| 492 | } |
| 493 | |
| 494 | int play_wav(const char *fg, int rate, int ch, const char *device, const char *fn) |
| 495 | { |
| 496 | struct wav_header hdr; |
| 497 | int fd; |
| 498 | unsigned flag = 0; |
| 499 | |
| 500 | if (pcm_flag) { |
| 501 | if(!fn) { |
| 502 | fd = fileno(stdin); |
| 503 | piped = 1; |
| 504 | } else { |
| 505 | fd = open(fn, O_RDONLY); |
| 506 | if (fd < 0) { |
| 507 | fprintf(stderr, "Aplay:aplay: cannot open '%s'\n", fn); |
| 508 | return fd; |
| 509 | } |
| 510 | } |
| 511 | if (compressed) { |
| 512 | hdr.sample_rate = rate; |
| 513 | hdr.num_channels = ch; |
| 514 | hdr.data_sz = 0; |
| 515 | goto ignore_header; |
| 516 | } |
| 517 | |
| 518 | if (read(fd, &hdr, sizeof(hdr)) != sizeof(hdr)) { |
| 519 | fprintf(stderr, "Aplay:aplay: cannot read header\n"); |
| 520 | return -errno; |
| 521 | } |
| 522 | |
| 523 | if ((hdr.riff_id != ID_RIFF) || |
| 524 | (hdr.riff_fmt != ID_WAVE) || |
| 525 | (hdr.fmt_id != ID_FMT)) { |
| 526 | fprintf(stderr, "Aplay:aplay: '%s' is not a riff/wave file\n", fn); |
| 527 | return -EINVAL; |
| 528 | } |
| 529 | if ((hdr.audio_format != FORMAT_PCM) || |
| 530 | (hdr.fmt_sz != 16)) { |
| 531 | fprintf(stderr, "Aplay:aplay: '%s' is not pcm format\n", fn); |
| 532 | return -EINVAL; |
| 533 | } |
| 534 | if (hdr.bits_per_sample != 16) { |
| 535 | fprintf(stderr, "Aplay:aplay: '%s' is not 16bit per sample\n", fn); |
| 536 | return -EINVAL; |
| 537 | } |
| 538 | } else { |
| 539 | fd = -EBADFD; |
| 540 | hdr.sample_rate = rate; |
| 541 | hdr.num_channels = ch; |
| 542 | hdr.data_sz = 0; |
| 543 | } |
| 544 | |
| 545 | ignore_header: |
| 546 | if (!strncmp(fg, "M", sizeof("M"))) |
| 547 | flag = PCM_MMAP; |
| 548 | else if (!strncmp(fg, "N", sizeof("N"))) |
| 549 | flag = PCM_NMMAP; |
| 550 | fprintf(stderr, "aplay: Playing '%s':%s\n", fn, get_format_desc(format) ); |
| 551 | |
| 552 | return play_file(hdr.sample_rate, hdr.num_channels, fd, flag, device, hdr.data_sz); |
| 553 | } |
| 554 | |
| 555 | int main(int argc, char **argv) |
| 556 | { |
| 557 | int option_index = 0; |
| 558 | int c,i; |
| 559 | int ch = 2; |
| 560 | int rate = 44100; |
| 561 | char *mmap = "N"; |
| 562 | char *device = "hw:0,0"; |
| 563 | char *filename; |
| 564 | int rc = 0; |
| 565 | |
| 566 | if (argc <2) { |
| 567 | printf("\nUsage: aplay [options] <file>\n" |
| 568 | "options:\n" |
| 569 | "-D <hw:C,D> -- Alsa PCM by name\n" |
| 570 | "-M -- Mmap stream\n" |
| 571 | "-P -- Hostless steam[No PCM]\n" |
| 572 | "-C -- Channels\n" |
| 573 | "-R -- Rate\n" |
| 574 | "-V -- verbose\n" |
| 575 | "-F -- Format\n" |
| 576 | "-B -- Period\n" |
| 577 | "-T <MP3, AAC, AC3_PASS_THROUGH> -- Compressed\n" |
| 578 | "<file> \n"); |
| 579 | fprintf(stderr, "Formats Supported:\n"); |
| 580 | for (i = 0; i <= SNDRV_PCM_FORMAT_LAST; ++i) |
| 581 | if (get_format_name(i)) |
| 582 | fprintf(stderr, "%s ", get_format_name(i)); |
| 583 | fprintf(stderr, "\nSome of these may not be available on selected hardware\n"); |
| 584 | return 0; |
| 585 | } |
| 586 | while ((c = getopt_long(argc, argv, "PVMD:R:C:F:B:T:", long_options, &option_index)) != -1) { |
| 587 | switch (c) { |
| 588 | case 'P': |
| 589 | pcm_flag = 0; |
| 590 | break; |
| 591 | case 'V': |
| 592 | debug = 1; |
| 593 | break; |
| 594 | case 'M': |
| 595 | mmap = "M"; |
| 596 | break; |
| 597 | case 'D': |
| 598 | device = optarg; |
| 599 | break; |
| 600 | case 'R': |
| 601 | rate = (int)strtol(optarg, NULL, 0); |
| 602 | break; |
| 603 | case 'C': |
| 604 | ch = (int)strtol(optarg, NULL, 0); |
| 605 | break; |
| 606 | case 'F': |
| 607 | printf("optarg = %s\n", optarg); |
| 608 | format = get_format(optarg); |
| 609 | break; |
| 610 | case 'B': |
| 611 | period = (int)strtol(optarg, NULL, 0); |
| 612 | break; |
| 613 | case 'T': |
| 614 | compressed = 1; |
| 615 | printf("compressed codec type requested = %s\n", optarg); |
| 616 | compr_codec = optarg; |
| 617 | break; |
| 618 | default: |
| 619 | printf("\nUsage: aplay [options] <file>\n" |
| 620 | "options:\n" |
| 621 | "-D <hw:C,D> -- Alsa PCM by name\n" |
| 622 | "-M -- Mmap stream\n" |
| 623 | "-P -- Hostless steam[No PCM]\n" |
| 624 | "-V -- verbose\n" |
| 625 | "-C -- Channels\n" |
| 626 | "-R -- Rate\n" |
| 627 | "-F -- Format\n" |
| 628 | "-B -- Period\n" |
| 629 | "-T -- Compressed\n" |
| 630 | "<file> \n"); |
| 631 | fprintf(stderr, "Formats Supported:\n"); |
| 632 | for (i = 0; i < SNDRV_PCM_FORMAT_LAST; ++i) |
| 633 | if (get_format_name(i)) |
| 634 | fprintf(stderr, "%s ", get_format_name(i)); |
| 635 | fprintf(stderr, "\nSome of these may not be available on selected hardware\n"); |
| 636 | return -EINVAL; |
| 637 | } |
| 638 | |
| 639 | } |
| 640 | filename = (char*) calloc(1, 30); |
| 641 | if (!filename) { |
| 642 | fprintf(stderr, "Aplay:Failed to allocate filename!"); |
| 643 | return -ENOMEM; |
| 644 | } |
| 645 | if (optind > argc - 1) { |
| 646 | free(filename); |
| 647 | filename = NULL; |
| 648 | } else { |
| 649 | strlcpy(filename, argv[optind++], 30); |
| 650 | } |
| 651 | |
| 652 | if (pcm_flag) { |
| 653 | if (format == SNDRV_PCM_FORMAT_S16_LE) |
| 654 | rc = play_wav(mmap, rate, ch, device, filename); |
| 655 | else |
| 656 | rc = play_raw(mmap, rate, ch, device, filename); |
| 657 | } else { |
| 658 | rc = play_wav(mmap, rate, ch, device, "dummy"); |
| 659 | } |
| 660 | if (filename) |
| 661 | free(filename); |
| 662 | |
| 663 | return rc; |
| 664 | } |
| 665 | |