Eric Laurent | c902d7f | 2013-03-08 14:50:45 -0800 | [diff] [blame] | 1 | /* |
| 2 | * BSD LICENSE |
| 3 | * |
| 4 | * tinyplay command line player for compress audio offload in alsa |
| 5 | * Copyright (c) 2011-2012, Intel Corporation |
| 6 | * All rights reserved. |
| 7 | * |
| 8 | * Author: Vinod Koul <vinod.koul@linux.intel.com> |
| 9 | * |
| 10 | * Redistribution and use in source and binary forms, with or without |
| 11 | * modification, are permitted provided that the following conditions are met: |
| 12 | * |
| 13 | * Redistributions of source code must retain the above copyright notice, |
| 14 | * this list of conditions and the following disclaimer. |
| 15 | * Redistributions in binary form must reproduce the above copyright notice, |
| 16 | * this list of conditions and the following disclaimer in the documentation |
| 17 | * and/or other materials provided with the distribution. |
| 18 | * Neither the name of Intel Corporation nor the names of its contributors |
| 19 | * may be used to endorse or promote products derived from this software |
| 20 | * without specific prior written permission. |
| 21 | * |
| 22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 23 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 26 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 27 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 28 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 29 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 30 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 31 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 32 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 33 | * |
| 34 | * LGPL LICENSE |
| 35 | * |
| 36 | * tinyplay command line player for compress audio offload in alsa |
| 37 | * Copyright (c) 2011-2012, Intel Corporation. |
| 38 | * |
| 39 | * This program is free software; you can redistribute it and/or modify it |
| 40 | * under the terms and conditions of the GNU Lesser General Public License, |
| 41 | * version 2.1, as published by the Free Software Foundation. |
| 42 | * |
| 43 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 44 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 45 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
| 46 | * License for more details. |
| 47 | * |
| 48 | * You should have received a copy of the GNU Lesser General Public License |
| 49 | * along with this program; if not, write to |
| 50 | * the Free Software Foundation, Inc., |
| 51 | * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. |
| 52 | */ |
| 53 | |
| 54 | #include <stdint.h> |
| 55 | #include <linux/types.h> |
| 56 | #include <fcntl.h> |
| 57 | #include <errno.h> |
| 58 | #include <unistd.h> |
| 59 | #include <stdio.h> |
| 60 | #include <stdlib.h> |
| 61 | #include <string.h> |
| 62 | #include <signal.h> |
| 63 | #include <stdbool.h> |
| 64 | #include <getopt.h> |
| 65 | #include <sys/time.h> |
| 66 | #define __force |
| 67 | #define __bitwise |
| 68 | #define __user |
Eric Laurent | 79c3707 | 2013-06-07 10:50:05 -0700 | [diff] [blame] | 69 | #include "sound/compress_params.h" |
Yuhao Chen | 00302a5 | 2014-06-18 17:42:46 -0700 | [diff] [blame] | 70 | #include "sound/asound.h" |
Eric Laurent | 79c3707 | 2013-06-07 10:50:05 -0700 | [diff] [blame] | 71 | #include "tinycompress/tinycompress.h" |
| 72 | #include "tinycompress/tinymp3.h" |
Yuhao Chen | 00302a5 | 2014-06-18 17:42:46 -0700 | [diff] [blame] | 73 | #include <string.h> |
Eric Laurent | c902d7f | 2013-03-08 14:50:45 -0800 | [diff] [blame] | 74 | |
| 75 | static int verbose; |
| 76 | |
| 77 | static void usage(void) |
| 78 | { |
| 79 | fprintf(stderr, "usage: cplay [OPTIONS] filename\n" |
| 80 | "-c\tcard number\n" |
| 81 | "-d\tdevice node\n" |
| 82 | "-b\tbuffer size\n" |
| 83 | "-f\tfragments\n\n" |
| 84 | "-v\tverbose mode\n" |
Yuhao Chen | 00302a5 | 2014-06-18 17:42:46 -0700 | [diff] [blame] | 85 | "-p\tpcm input\n" |
Eric Laurent | c902d7f | 2013-03-08 14:50:45 -0800 | [diff] [blame] | 86 | "-h\tPrints this help list\n\n" |
| 87 | "Example:\n" |
| 88 | "\tcplay -c 1 -d 2 test.mp3\n" |
| 89 | "\tcplay -f 5 test.mp3\n"); |
| 90 | |
| 91 | exit(EXIT_FAILURE); |
| 92 | } |
| 93 | |
| 94 | void play_samples(char *name, unsigned int card, unsigned int device, |
| 95 | unsigned long buffer_size, unsigned int frag); |
Yuhao Chen | 00302a5 | 2014-06-18 17:42:46 -0700 | [diff] [blame] | 96 | void play_pcm_samples(char *name, unsigned int card, unsigned int device, |
| 97 | unsigned long buffer_size, unsigned int frag); |
Eric Laurent | c902d7f | 2013-03-08 14:50:45 -0800 | [diff] [blame] | 98 | struct mp3_header { |
| 99 | uint16_t sync; |
| 100 | uint8_t format1; |
| 101 | uint8_t format2; |
| 102 | }; |
| 103 | |
| 104 | int parse_mp3_header(struct mp3_header *header, unsigned int *num_channels, |
| 105 | unsigned int *sample_rate, unsigned int *bit_rate) |
| 106 | { |
| 107 | int ver_idx, mp3_version, layer, bit_rate_idx, sample_rate_idx, channel_idx; |
| 108 | |
| 109 | /* check sync bits */ |
| 110 | if ((header->sync & MP3_SYNC) != MP3_SYNC) { |
| 111 | fprintf(stderr, "Error: Can't find sync word\n"); |
| 112 | return -1; |
| 113 | } |
| 114 | ver_idx = (header->sync >> 11) & 0x03; |
| 115 | mp3_version = ver_idx == 0 ? MPEG25 : ((ver_idx & 0x1) ? MPEG1 : MPEG2); |
| 116 | layer = 4 - ((header->sync >> 9) & 0x03); |
| 117 | bit_rate_idx = ((header->format1 >> 4) & 0x0f); |
| 118 | sample_rate_idx = ((header->format1 >> 2) & 0x03); |
| 119 | channel_idx = ((header->format2 >> 6) & 0x03); |
| 120 | |
| 121 | if (sample_rate_idx == 3 || layer == 4 || bit_rate_idx == 15) { |
| 122 | fprintf(stderr, "Error: Can't find valid header\n"); |
| 123 | return -1; |
| 124 | } |
| 125 | *num_channels = (channel_idx == MONO ? 1 : 2); |
| 126 | *sample_rate = mp3_sample_rates[mp3_version][sample_rate_idx]; |
| 127 | *bit_rate = (mp3_bit_rates[mp3_version][layer - 1][bit_rate_idx]) * 1000; |
| 128 | if (verbose) |
| 129 | printf("%s: exit\n", __func__); |
| 130 | return 0; |
| 131 | } |
| 132 | |
Yuhao Chen | 00302a5 | 2014-06-18 17:42:46 -0700 | [diff] [blame] | 133 | int parse_pcm_header(FILE **file, uint16_t *num_channels, |
| 134 | unsigned int *sample_rate, uint16_t *bit_rate) |
| 135 | { |
| 136 | char title[5] = " "; |
| 137 | int size = 0; |
| 138 | fread(title, 4, 1, *file); |
| 139 | if (strcmp(title, "RIFF") != 0) { |
| 140 | fprintf(stderr, "invalid file\n"); |
| 141 | return -1; |
| 142 | } |
| 143 | |
| 144 | //skip total file size |
| 145 | fseek(*file, 4, SEEK_CUR); |
| 146 | |
| 147 | char format[5] = " "; |
| 148 | fread(format, 4, 1, *file); |
| 149 | if (strcmp(format, "WAVE") != 0) { |
| 150 | fprintf(stderr, "invalid format\n"); |
| 151 | return -1; |
| 152 | } |
| 153 | |
| 154 | fread(title, 4, 1, *file); |
| 155 | fread(&size, 4, 1, *file); |
| 156 | while (strcmp(title, "fmt ") != 0){ |
| 157 | fseek(*file, size, SEEK_CUR); |
| 158 | fread(title, 4, 1, *file); |
| 159 | fread(&size, 4, 1, *file); |
| 160 | if (feof(*file)) { |
| 161 | fprintf(stderr, "missing format information\n"); |
| 162 | return -1; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | uint16_t audioformat, blockalign; |
| 167 | unsigned int byterate; |
| 168 | fread(&audioformat, 2, 1, *file); |
| 169 | fread(num_channels, 2, 1, *file); |
| 170 | fread(sample_rate, 4, 1, *file); |
| 171 | fread(&byterate, 4, 1, *file); |
| 172 | fread(&blockalign, 2, 1, *file); |
| 173 | fread(bit_rate, 2, 1, *file); |
| 174 | fseek(*file, size - 16, SEEK_CUR); |
| 175 | |
| 176 | fread(title, 4, 1, *file); |
| 177 | fread(&size, 4, 1, *file); |
| 178 | while (strcmp(title, "data") != 0){ |
| 179 | fseek(*file, size, SEEK_CUR); |
| 180 | fread(title, 4, 1, *file); |
| 181 | fread(&size, 4, 1, *file); |
| 182 | if (feof(*file)) { |
| 183 | fprintf(stderr, "missing data\n"); |
| 184 | return -1; |
| 185 | } |
| 186 | } |
| 187 | return 0; |
| 188 | } |
| 189 | |
Eric Laurent | c902d7f | 2013-03-08 14:50:45 -0800 | [diff] [blame] | 190 | int check_codec_format_supported(unsigned int card, unsigned int device, struct snd_codec *codec) |
| 191 | { |
| 192 | if (is_codec_supported(card, device, COMPRESS_IN, codec) == false) { |
| 193 | fprintf(stderr, "Error: This codec or format is not supported by DSP\n"); |
| 194 | return -1; |
| 195 | } |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | static int print_time(struct compress *compress) |
| 200 | { |
| 201 | unsigned int avail; |
| 202 | struct timespec tstamp; |
| 203 | |
| 204 | if (compress_get_hpointer(compress, &avail, &tstamp) != 0) { |
| 205 | fprintf(stderr, "Error querying timestamp\n"); |
| 206 | fprintf(stderr, "ERR: %s\n", compress_get_error(compress)); |
| 207 | return -1; |
| 208 | } else |
| 209 | fprintf(stderr, "DSP played %jd.%jd\n", (intmax_t)tstamp.tv_sec, (intmax_t)tstamp.tv_nsec*1000); |
| 210 | return 0; |
| 211 | } |
| 212 | |
Yuhao Chen | 00302a5 | 2014-06-18 17:42:46 -0700 | [diff] [blame] | 213 | static int readFromFile2Buffer(char *buffer, FILE** file, int size, uint16_t bits){ |
| 214 | int num_read = 0; |
| 215 | if (bits == 24){ |
| 216 | int i; |
| 217 | int pos = 0; |
| 218 | for (i = 0; i < size; i = i + 4) { |
| 219 | *(buffer + pos) = 0; |
| 220 | pos += 1; |
| 221 | num_read += 3 * fread(buffer + pos, 3, 1, *file); |
| 222 | pos += 3; |
| 223 | num_read += 1; |
| 224 | } |
| 225 | } else { |
| 226 | num_read = fread(buffer, 1, size, *file); |
| 227 | } |
| 228 | |
| 229 | return num_read; |
| 230 | } |
| 231 | |
Eric Laurent | c902d7f | 2013-03-08 14:50:45 -0800 | [diff] [blame] | 232 | int main(int argc, char **argv) |
| 233 | { |
Yuhao Chen | 00302a5 | 2014-06-18 17:42:46 -0700 | [diff] [blame] | 234 | char *file; |
Eric Laurent | c902d7f | 2013-03-08 14:50:45 -0800 | [diff] [blame] | 235 | unsigned long buffer_size = 0; |
| 236 | int c; |
| 237 | unsigned int card = 0, device = 0, frag = 0; |
Yuhao Chen | 00302a5 | 2014-06-18 17:42:46 -0700 | [diff] [blame] | 238 | bool pcm = false; |
Eric Laurent | c902d7f | 2013-03-08 14:50:45 -0800 | [diff] [blame] | 239 | |
Yuhao Chen | 00302a5 | 2014-06-18 17:42:46 -0700 | [diff] [blame] | 240 | if (argc < 2) |
Eric Laurent | c902d7f | 2013-03-08 14:50:45 -0800 | [diff] [blame] | 241 | usage(); |
| 242 | |
Yuhao Chen | 00302a5 | 2014-06-18 17:42:46 -0700 | [diff] [blame] | 243 | verbose = 0; |
| 244 | while ((c = getopt(argc, argv, "hvpb:f:c:d:")) != -1) { |
Eric Laurent | c902d7f | 2013-03-08 14:50:45 -0800 | [diff] [blame] | 245 | switch (c) { |
| 246 | case 'h': |
| 247 | usage(); |
| 248 | break; |
| 249 | case 'b': |
| 250 | buffer_size = strtol(optarg, NULL, 0); |
| 251 | break; |
| 252 | case 'f': |
| 253 | frag = strtol(optarg, NULL, 10); |
| 254 | break; |
| 255 | case 'c': |
| 256 | card = strtol(optarg, NULL, 10); |
| 257 | break; |
| 258 | case 'd': |
| 259 | device = strtol(optarg, NULL, 10); |
| 260 | break; |
| 261 | case 'v': |
| 262 | verbose = 1; |
| 263 | break; |
Yuhao Chen | 00302a5 | 2014-06-18 17:42:46 -0700 | [diff] [blame] | 264 | case 'p': |
| 265 | pcm = true; |
| 266 | break; |
Eric Laurent | c902d7f | 2013-03-08 14:50:45 -0800 | [diff] [blame] | 267 | default: |
| 268 | exit(EXIT_FAILURE); |
| 269 | } |
Yuhao Chen | 00302a5 | 2014-06-18 17:42:46 -0700 | [diff] [blame] | 270 | } |
Eric Laurent | c902d7f | 2013-03-08 14:50:45 -0800 | [diff] [blame] | 271 | if (optind >= argc) |
| 272 | usage(); |
| 273 | |
| 274 | file = argv[optind]; |
Yuhao Chen | 00302a5 | 2014-06-18 17:42:46 -0700 | [diff] [blame] | 275 | if (!pcm) { |
| 276 | play_samples(file, card, device, buffer_size, frag); |
| 277 | } else if (pcm) { |
| 278 | fprintf(stderr, "Playing pcm samples\n"); |
| 279 | play_pcm_samples(file, card, device, buffer_size, frag); |
| 280 | } |
Eric Laurent | c902d7f | 2013-03-08 14:50:45 -0800 | [diff] [blame] | 281 | |
| 282 | fprintf(stderr, "Finish Playing.... Close Normally\n"); |
| 283 | exit(EXIT_SUCCESS); |
| 284 | } |
| 285 | |
| 286 | void play_samples(char *name, unsigned int card, unsigned int device, |
| 287 | unsigned long buffer_size, unsigned int frag) |
| 288 | { |
| 289 | struct compr_config config; |
| 290 | struct snd_codec codec; |
| 291 | struct compress *compress; |
| 292 | struct mp3_header header; |
| 293 | FILE *file; |
| 294 | char *buffer; |
| 295 | int size, num_read, wrote; |
| 296 | unsigned int channels, rate, bits; |
| 297 | |
| 298 | if (verbose) |
| 299 | printf("%s: entry\n", __func__); |
| 300 | file = fopen(name, "rb"); |
| 301 | if (!file) { |
| 302 | fprintf(stderr, "Unable to open file '%s'\n", name); |
| 303 | exit(EXIT_FAILURE); |
| 304 | } |
| 305 | |
| 306 | fread(&header, sizeof(header), 1, file); |
| 307 | |
| 308 | if (parse_mp3_header(&header, &channels, &rate, &bits) == -1) { |
| 309 | fclose(file); |
| 310 | exit(EXIT_FAILURE); |
| 311 | } |
| 312 | |
| 313 | codec.id = SND_AUDIOCODEC_MP3; |
| 314 | codec.ch_in = channels; |
| 315 | codec.ch_out = channels; |
Eric Laurent | d885bbf | 2014-10-28 12:17:44 -0700 | [diff] [blame] | 316 | codec.sample_rate = rate; |
Eric Laurent | 79c3707 | 2013-06-07 10:50:05 -0700 | [diff] [blame] | 317 | if (!codec.sample_rate) { |
| 318 | fprintf(stderr, "invalid sample rate %d\n", rate); |
| 319 | fclose(file); |
| 320 | exit(EXIT_FAILURE); |
Eric Laurent | c902d7f | 2013-03-08 14:50:45 -0800 | [diff] [blame] | 321 | } |
| 322 | codec.bit_rate = bits; |
| 323 | codec.rate_control = 0; |
| 324 | codec.profile = 0; |
| 325 | codec.level = 0; |
| 326 | codec.ch_mode = 0; |
| 327 | codec.format = 0; |
| 328 | if ((buffer_size != 0) && (frag != 0)) { |
| 329 | config.fragment_size = buffer_size/frag; |
| 330 | config.fragments = frag; |
| 331 | } else { |
| 332 | /* use driver defaults */ |
| 333 | config.fragment_size = 0; |
| 334 | config.fragments = 0; |
| 335 | } |
| 336 | config.codec = &codec; |
| 337 | |
| 338 | compress = compress_open(card, device, COMPRESS_IN, &config); |
| 339 | if (!compress || !is_compress_ready(compress)) { |
| 340 | fprintf(stderr, "Unable to open Compress device %d:%d\n", |
| 341 | card, device); |
| 342 | fprintf(stderr, "ERR: %s\n", compress_get_error(compress)); |
| 343 | goto FILE_EXIT; |
| 344 | }; |
| 345 | if (verbose) |
| 346 | printf("%s: Opened compress device\n", __func__); |
| 347 | size = config.fragment_size; |
| 348 | buffer = malloc(size * config.fragments); |
| 349 | if (!buffer) { |
| 350 | fprintf(stderr, "Unable to allocate %d bytes\n", size); |
| 351 | goto COMP_EXIT; |
| 352 | } |
| 353 | |
| 354 | /* we will write frag fragment_size and then start */ |
| 355 | num_read = fread(buffer, 1, size * config.fragments, file); |
| 356 | if (num_read > 0) { |
| 357 | if (verbose) |
| 358 | printf("%s: Doing first buffer write of %d\n", __func__, num_read); |
| 359 | wrote = compress_write(compress, buffer, num_read); |
| 360 | if (wrote < 0) { |
| 361 | fprintf(stderr, "Error %d playing sample\n", wrote); |
| 362 | fprintf(stderr, "ERR: %s\n", compress_get_error(compress)); |
| 363 | goto BUF_EXIT; |
| 364 | } |
| 365 | if (wrote != num_read) { |
| 366 | /* TODO: Buufer pointer needs to be set here */ |
| 367 | fprintf(stderr, "We wrote %d, DSP accepted %d\n", num_read, wrote); |
| 368 | } |
| 369 | } |
| 370 | printf("Playing file %s On Card %u device %u, with buffer of %lu bytes\n", |
| 371 | name, card, device, buffer_size); |
| 372 | printf("Format %u Channels %u, %u Hz, Bit Rate %d\n", |
| 373 | SND_AUDIOCODEC_MP3, channels, rate, bits); |
| 374 | |
| 375 | compress_start(compress); |
| 376 | if (verbose) |
| 377 | printf("%s: You should hear audio NOW!!!\n", __func__); |
| 378 | |
| 379 | do { |
| 380 | num_read = fread(buffer, 1, size, file); |
| 381 | if (num_read > 0) { |
| 382 | wrote = compress_write(compress, buffer, num_read); |
| 383 | if (wrote < 0) { |
| 384 | fprintf(stderr, "Error playing sample\n"); |
| 385 | fprintf(stderr, "ERR: %s\n", compress_get_error(compress)); |
| 386 | goto BUF_EXIT; |
| 387 | } |
| 388 | if (wrote != num_read) { |
Eric Laurent | f0c4078 | 2013-05-02 18:10:20 -0700 | [diff] [blame] | 389 | /* TODO: Buffer pointer needs to be set here */ |
Eric Laurent | c902d7f | 2013-03-08 14:50:45 -0800 | [diff] [blame] | 390 | fprintf(stderr, "We wrote %d, DSP accepted %d\n", num_read, wrote); |
| 391 | } |
| 392 | if (verbose) { |
| 393 | print_time(compress); |
| 394 | printf("%s: wrote %d\n", __func__, wrote); |
| 395 | } |
| 396 | } |
| 397 | } while (num_read > 0); |
| 398 | |
| 399 | if (verbose) |
Eric Laurent | f0c4078 | 2013-05-02 18:10:20 -0700 | [diff] [blame] | 400 | printf("%s: exit success\n", __func__); |
Eric Laurent | c902d7f | 2013-03-08 14:50:45 -0800 | [diff] [blame] | 401 | /* issue drain if it supports */ |
| 402 | compress_drain(compress); |
| 403 | free(buffer); |
| 404 | fclose(file); |
| 405 | compress_close(compress); |
| 406 | return; |
| 407 | BUF_EXIT: |
| 408 | free(buffer); |
| 409 | COMP_EXIT: |
| 410 | compress_close(compress); |
| 411 | FILE_EXIT: |
| 412 | fclose(file); |
| 413 | if (verbose) |
| 414 | printf("%s: exit failure\n", __func__); |
| 415 | exit(EXIT_FAILURE); |
| 416 | } |
| 417 | |
Yuhao Chen | 00302a5 | 2014-06-18 17:42:46 -0700 | [diff] [blame] | 418 | void play_pcm_samples(char *name, unsigned int card, unsigned int device, |
| 419 | unsigned long buffer_size, unsigned int frag) |
| 420 | { |
| 421 | struct compr_config config; |
| 422 | struct snd_codec codec; |
| 423 | struct compress *compress; |
| 424 | struct mp3_header header; |
| 425 | FILE *file; |
| 426 | char *buffer; |
| 427 | int frag_size, num_read, wrote; |
| 428 | unsigned int rate, byterate; |
| 429 | uint16_t channels, bits; |
| 430 | |
| 431 | if (verbose) |
| 432 | printf("%s: entry\n", __func__); |
| 433 | file = fopen(name, "rb"); |
| 434 | if (!file) { |
| 435 | fprintf(stderr, "Unable to open file '%s'\n", name); |
| 436 | exit(EXIT_FAILURE); |
| 437 | } |
| 438 | |
| 439 | if (parse_pcm_header(&file, &channels, &rate, &bits) == -1){ |
| 440 | fprintf(stderr, "invalid header\n"); |
| 441 | exit(EXIT_FAILURE); |
| 442 | } |
| 443 | |
| 444 | codec.id = SND_AUDIOCODEC_PCM; |
| 445 | codec.ch_in = channels; |
| 446 | codec.ch_out = channels; |
| 447 | codec.sample_rate = compress_get_alsa_rate(rate); |
| 448 | if (!codec.sample_rate) { |
| 449 | fprintf(stderr, "invalid sample rate %d\n", rate); |
| 450 | fclose(file); |
| 451 | exit(EXIT_FAILURE); |
| 452 | } |
| 453 | codec.bit_rate = bits; |
| 454 | codec.rate_control = 0; |
| 455 | codec.profile = 0; |
| 456 | codec.level = 0; |
| 457 | codec.ch_mode = 0; |
| 458 | if (bits == 16) { |
| 459 | codec.format = SNDRV_PCM_FORMAT_S16_LE; |
| 460 | } else if (bits == 24) { |
| 461 | codec.format = SNDRV_PCM_FORMAT_S24_LE; |
| 462 | } else { |
| 463 | fprintf(stderr, "invalid bit rate %d\n", bits); |
| 464 | fclose(file); |
| 465 | exit(EXIT_FAILURE); |
| 466 | } |
| 467 | |
| 468 | if ((buffer_size != 0) && (frag != 0)) { |
| 469 | /* Make sure the buffer size and fragments are multiple of 2, or 24 for multi channel*/ |
| 470 | unsigned int temp_size = (channels > 2)? 24 : 2; |
| 471 | while (temp_size < buffer_size) |
| 472 | temp_size *= 2; |
| 473 | buffer_size = temp_size; |
| 474 | |
| 475 | unsigned int temp_frag = 1; |
| 476 | while (temp_frag < frag) |
| 477 | temp_frag *= 2; |
| 478 | |
| 479 | config.fragment_size = temp_size/temp_frag; |
| 480 | config.fragments = temp_frag; |
| 481 | } else { |
| 482 | /* Now set to suggested value. set to 0 for using driver defaults */ |
| 483 | config.fragment_size = (channels > 2)? 12288 : 8192; |
| 484 | config.fragments = 4; |
| 485 | } |
| 486 | if (verbose) |
| 487 | printf("%s: Buffer size: %d Fragment size: %d Fragments: %d\n", |
| 488 | __func__, config.fragment_size * config.fragments, |
| 489 | config.fragment_size, config.fragments); |
| 490 | |
| 491 | config.codec = &codec; |
| 492 | |
| 493 | compress = compress_open(card, device, COMPRESS_IN, &config); |
| 494 | if (!compress || !is_compress_ready(compress)) { |
| 495 | fprintf(stderr, "Unable to open Compress device %d:%d\n", |
| 496 | card, device); |
| 497 | fprintf(stderr, "ERR: %s\n", compress_get_error(compress)); |
| 498 | goto FILE_EXIT; |
| 499 | }; |
| 500 | if (verbose) |
| 501 | printf("%s: Opened compress device\n", __func__); |
| 502 | frag_size = config.fragment_size; |
| 503 | printf("frag_size %d frags %d", frag_size, config.fragments); |
| 504 | buffer = malloc(frag_size * config.fragments); |
| 505 | if (!buffer) { |
| 506 | fprintf(stderr, "Unable to allocate %d bytes\n", frag_size); |
| 507 | goto COMP_EXIT; |
| 508 | } |
| 509 | |
| 510 | /* we will write frag fragment_size and then start */ |
| 511 | num_read = readFromFile2Buffer(buffer, &file, frag_size * config.fragments, bits); |
| 512 | /*Zero padded each 3 byte into 4 byte, the number read must be greater than size / 4 */ |
| 513 | if (num_read > ((bits == 16)? 0 : frag_size * (int)config.fragments / 4)) { |
| 514 | if (verbose) |
| 515 | printf("%s: Doing first buffer write of %d\n", __func__, num_read); |
| 516 | wrote = compress_write(compress, buffer, num_read); |
| 517 | if (wrote < 0) { |
| 518 | fprintf(stderr, "Error %d playing sample\n", wrote); |
| 519 | fprintf(stderr, "ERR: %s\n", compress_get_error(compress)); |
| 520 | goto BUF_EXIT; |
| 521 | } |
| 522 | if (wrote != num_read) { |
| 523 | /* TODO: Buufer pointer needs to be set here */ |
| 524 | fprintf(stderr, "We wrote %d, DSP accepted %d\n", num_read, wrote); |
| 525 | } |
| 526 | } |
| 527 | printf("Playing file %s On Card %u device %u, with buffer of %lu bytes\n", |
| 528 | name, card, device, buffer_size); |
| 529 | printf("Format %u Channels %u, %u Hz, Bit Rate %d\n", |
| 530 | SND_AUDIOCODEC_MP3, channels, rate, bits); |
| 531 | |
| 532 | compress_start(compress); |
| 533 | if (verbose) |
| 534 | printf("%s: You should hear audio NOW!!!\n", __func__); |
| 535 | |
| 536 | do { |
| 537 | num_read = readFromFile2Buffer(buffer, &file, frag_size, bits); |
| 538 | if (num_read > ((bits == 16)? 0 : frag_size / 4)) { |
| 539 | wrote = compress_write(compress, buffer, num_read); |
| 540 | if (wrote < 0) { |
| 541 | fprintf(stderr, "Error playing sample\n"); |
| 542 | fprintf(stderr, "ERR: %s\n", compress_get_error(compress)); |
| 543 | goto BUF_EXIT; |
| 544 | } |
| 545 | if (wrote != num_read) { |
| 546 | /* TODO: Buffer pointer needs to be set here */ |
| 547 | fprintf(stderr, "We wrote %d, DSP accepted %d\n", num_read, wrote); |
| 548 | } |
| 549 | if (verbose) { |
| 550 | print_time(compress); |
| 551 | printf("%s: wrote %d\n", __func__, wrote); |
| 552 | } |
| 553 | } |
| 554 | } while ((!feof(file))); |
| 555 | |
| 556 | if (verbose) |
| 557 | printf("%s: exit success\n", __func__); |
| 558 | /* issue drain if it supports */ |
| 559 | compress_drain(compress); |
| 560 | free(buffer); |
| 561 | fclose(file); |
| 562 | compress_close(compress); |
| 563 | return; |
| 564 | BUF_EXIT: |
| 565 | free(buffer); |
| 566 | COMP_EXIT: |
| 567 | compress_close(compress); |
| 568 | FILE_EXIT: |
| 569 | fclose(file); |
| 570 | if (verbose) |
| 571 | printf("%s: exit failure\n", __func__); |
| 572 | exit(EXIT_FAILURE); |
| 573 | } |
| 574 | |