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 card, unsigned int device, unsigned int channels,
58 unsigned int rate, unsigned int bits, unsigned int period_size,
59 unsigned int period_count);
60
main(int argc,char ** argv)61 int main(int argc, char **argv)
62 {
63 FILE *file;
64 struct wav_header header;
65 unsigned int device = 0;
66 unsigned int card = 0;
67 unsigned int period_size = 1024;
68 unsigned int period_count = 4;
69
70 if (argc < 2) {
71 fprintf(stderr, "Usage: %s file.wav [-D card] [-d device] [-p period_size]"
72 " [-n n_periods] \n", argv[0]);
73 return 1;
74 }
75
76 file = fopen(argv[1], "rb");
77 if (!file) {
78 fprintf(stderr, "Unable to open file '%s'\n", argv[1]);
79 return 1;
80 }
81
82 /* parse command line arguments */
83 argv += 2;
84 while (*argv) {
85 if (strcmp(*argv, "-d") == 0) {
86 argv++;
87 if (*argv)
88 device = atoi(*argv);
89 }
90 if (strcmp(*argv, "-p") == 0) {
91 argv++;
92 if (*argv)
93 period_size = atoi(*argv);
94 }
95 if (strcmp(*argv, "-n") == 0) {
96 argv++;
97 if (*argv)
98 period_count = atoi(*argv);
99 }
100 if (strcmp(*argv, "-D") == 0) {
101 argv++;
102 if (*argv)
103 card = atoi(*argv);
104 }
105 if (*argv)
106 argv++;
107 }
108
109 fread(&header, sizeof(struct wav_header), 1, file);
110
111 if ((header.riff_id != ID_RIFF) ||
112 (header.riff_fmt != ID_WAVE) ||
113 (header.fmt_id != ID_FMT) ||
114 (header.audio_format != FORMAT_PCM) ||
115 (header.fmt_sz != 16)) {
116 fprintf(stderr, "Error: '%s' is not a PCM riff/wave file\n", argv[1]);
117 fclose(file);
118 return 1;
119 }
120
121 play_sample(file, card, device, header.num_channels, header.sample_rate,
122 header.bits_per_sample, period_size, period_count);
123
124 fclose(file);
125
126 return 0;
127 }
128
play_sample(FILE * file,unsigned int card,unsigned int device,unsigned int channels,unsigned int rate,unsigned int bits,unsigned int period_size,unsigned int period_count)129 void play_sample(FILE *file, unsigned int card, unsigned int device, unsigned int channels,
130 unsigned int rate, unsigned int bits, unsigned int period_size,
131 unsigned int period_count)
132 {
133 struct pcm_config config;
134 struct pcm *pcm;
135 char *buffer;
136 int size;
137 int num_read;
138
139 config.channels = channels;
140 config.rate = rate;
141 config.period_size = period_size;
142 config.period_count = period_count;
143 if (bits == 32)
144 config.format = PCM_FORMAT_S32_LE;
145 else if (bits == 16)
146 config.format = PCM_FORMAT_S16_LE;
147 config.start_threshold = 0;
148 config.stop_threshold = 0;
149 config.silence_threshold = 0;
150
151 pcm = pcm_open(card, device, PCM_OUT, &config);
152 if (!pcm || !pcm_is_ready(pcm)) {
153 fprintf(stderr, "Unable to open PCM device %u (%s)\n",
154 device, pcm_get_error(pcm));
155 return;
156 }
157
158 size = pcm_frames_to_bytes(pcm, pcm_get_buffer_size(pcm));
159 buffer = malloc(size);
160 if (!buffer) {
161 fprintf(stderr, "Unable to allocate %d bytes\n", size);
162 free(buffer);
163 pcm_close(pcm);
164 return;
165 }
166
167 printf("Playing sample: %u ch, %u hz, %u bit\n", channels, rate, bits);
168
169 do {
170 num_read = fread(buffer, 1, size, file);
171 if (num_read > 0) {
172 if (pcm_write(pcm, buffer, num_read)) {
173 fprintf(stderr, "Error playing sample\n");
174 break;
175 }
176 }
177 } while (num_read > 0);
178
179 free(buffer);
180 pcm_close(pcm);
181 }
182
183