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>
33
34 #define ID_RIFF 0x46464952
35 #define ID_WAVE 0x45564157
36 #define ID_FMT 0x20746d66
37 #define ID_DATA 0x61746164
38
39 #define FORMAT_PCM 1
40
41 struct wav_header {
42 uint32_t riff_id;
43 uint32_t riff_sz;
44 uint32_t riff_fmt;
45 uint32_t fmt_id;
46 uint32_t fmt_sz;
47 uint16_t audio_format;
48 uint16_t num_channels;
49 uint32_t sample_rate;
50 uint32_t byte_rate;
51 uint16_t block_align;
52 uint16_t bits_per_sample;
53 uint32_t data_id;
54 uint32_t data_sz;
55 };
56
57 void play_sample(FILE *file, unsigned int device, unsigned int channels,
58 unsigned int rate, unsigned int bits);
59
main(int argc,char ** argv)60 int main(int argc, char **argv)
61 {
62 FILE *file;
63 struct wav_header header;
64 unsigned int device = 0;
65
66 if (argc < 2) {
67 fprintf(stderr, "Usage: %s file.wav [-d device]\n", argv[0]);
68 return 1;
69 }
70
71 file = fopen(argv[1], "rb");
72 if (!file) {
73 fprintf(stderr, "Unable to open file '%s'\n", argv[1]);
74 return 1;
75 }
76
77 /* parse command line arguments */
78 argv += 2;
79 while (*argv) {
80 if (strcmp(*argv, "-d") == 0) {
81 argv++;
82 device = atoi(*argv);
83 }
84 argv++;
85 }
86
87 fread(&header, sizeof(struct wav_header), 1, file);
88
89 if ((header.riff_id != ID_RIFF) ||
90 (header.riff_fmt != ID_WAVE) ||
91 (header.fmt_id != ID_FMT) ||
92 (header.audio_format != FORMAT_PCM) ||
93 (header.fmt_sz != 16)) {
94 fprintf(stderr, "Error: '%s' is not a PCM riff/wave file\n", argv[1]);
95 fclose(file);
96 return 1;
97 }
98
99 play_sample(file, device, header.num_channels, header.sample_rate,
100 header.bits_per_sample);
101
102 fclose(file);
103
104 return 0;
105 }
106
play_sample(FILE * file,unsigned int device,unsigned int channels,unsigned int rate,unsigned int bits)107 void play_sample(FILE *file, unsigned int device, unsigned int channels,
108 unsigned int rate, unsigned int bits)
109 {
110 struct pcm_config config;
111 struct pcm *pcm;
112 char *buffer;
113 int size;
114 int num_read;
115
116 config.channels = channels;
117 config.rate = rate;
118 config.period_size = 1024;
119 config.period_count = 4;
120 if (bits == 32)
121 config.format = PCM_FORMAT_S32_LE;
122 else if (bits == 16)
123 config.format = PCM_FORMAT_S16_LE;
124 config.start_threshold = 0;
125 config.stop_threshold = 0;
126 config.silence_threshold = 0;
127
128 pcm = pcm_open(0, device, PCM_OUT, &config);
129 if (!pcm || !pcm_is_ready(pcm)) {
130 fprintf(stderr, "Unable to open PCM device %u (%s)\n",
131 device, pcm_get_error(pcm));
132 return;
133 }
134
135 size = pcm_get_buffer_size(pcm);
136 buffer = malloc(size);
137 if (!buffer) {
138 fprintf(stderr, "Unable to allocate %d bytes\n", size);
139 free(buffer);
140 pcm_close(pcm);
141 return;
142 }
143
144 printf("Playing sample: %u ch, %u hz, %u bit\n", channels, rate, bits);
145
146 do {
147 num_read = fread(buffer, 1, size, file);
148 if (num_read > 0) {
149 if (pcm_write(pcm, buffer, num_read)) {
150 fprintf(stderr, "Error playing sample\n");
151 break;
152 }
153 }
154 } while (num_read > 0);
155
156 free(buffer);
157 pcm_close(pcm);
158 }
159
160