blob: d7e7d4628500f27edbbf03426850be5f13d0b9e6 [file] [log] [blame]
Simon Wilsonedff7082011-06-06 15:33:34 -07001/* 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 Wilsonda39e0b2012-11-09 15:16:47 -080033#include <string.h>
34#include <signal.h>
Simon Wilsonedff7082011-06-06 15:33:34 -070035
36#define ID_RIFF 0x46464952
37#define ID_WAVE 0x45564157
38#define ID_FMT 0x20746d66
39#define ID_DATA 0x61746164
40
Simon Wilson85dc38f2012-05-15 17:37:19 -070041struct riff_wave_header {
Simon Wilsonedff7082011-06-06 15:33:34 -070042 uint32_t riff_id;
43 uint32_t riff_sz;
Simon Wilson85dc38f2012-05-15 17:37:19 -070044 uint32_t wave_id;
45};
46
47struct chunk_header {
48 uint32_t id;
49 uint32_t sz;
50};
51
52struct chunk_fmt {
Simon Wilsonedff7082011-06-06 15:33:34 -070053 uint16_t audio_format;
54 uint16_t num_channels;
55 uint32_t sample_rate;
56 uint32_t byte_rate;
57 uint16_t block_align;
58 uint16_t bits_per_sample;
Simon Wilsonedff7082011-06-06 15:33:34 -070059};
60
Simon Wilsonda39e0b2012-11-09 15:16:47 -080061static int close = 0;
62
Simon Wilsondaa83292012-02-28 15:26:02 -080063void play_sample(FILE *file, unsigned int card, unsigned int device, unsigned int channels,
64 unsigned int rate, unsigned int bits, unsigned int period_size,
65 unsigned int period_count);
Simon Wilsonedff7082011-06-06 15:33:34 -070066
Simon Wilsonda39e0b2012-11-09 15:16:47 -080067void stream_close(int sig)
68{
69 /* allow the stream to be closed gracefully */
70 signal(sig, SIG_IGN);
71 close = 1;
72}
73
Simon Wilsonedff7082011-06-06 15:33:34 -070074int main(int argc, char **argv)
75{
76 FILE *file;
Simon Wilson85dc38f2012-05-15 17:37:19 -070077 struct riff_wave_header riff_wave_header;
78 struct chunk_header chunk_header;
79 struct chunk_fmt chunk_fmt;
Simon Wilson62104732011-08-05 12:00:00 -070080 unsigned int device = 0;
Simon Wilsondaa83292012-02-28 15:26:02 -080081 unsigned int card = 0;
82 unsigned int period_size = 1024;
83 unsigned int period_count = 4;
Simon Wilson85dc38f2012-05-15 17:37:19 -070084 char *filename;
85 int more_chunks = 1;
Simon Wilsonedff7082011-06-06 15:33:34 -070086
Simon Wilson62104732011-08-05 12:00:00 -070087 if (argc < 2) {
Simon Wilsondaa83292012-02-28 15:26:02 -080088 fprintf(stderr, "Usage: %s file.wav [-D card] [-d device] [-p period_size]"
89 " [-n n_periods] \n", argv[0]);
Simon Wilsonedff7082011-06-06 15:33:34 -070090 return 1;
91 }
92
Simon Wilson85dc38f2012-05-15 17:37:19 -070093 filename = argv[1];
94 file = fopen(filename, "rb");
Simon Wilsonedff7082011-06-06 15:33:34 -070095 if (!file) {
Simon Wilson85dc38f2012-05-15 17:37:19 -070096 fprintf(stderr, "Unable to open file '%s'\n", filename);
Simon Wilsonedff7082011-06-06 15:33:34 -070097 return 1;
98 }
99
Simon Wilson85dc38f2012-05-15 17:37:19 -0700100 fread(&riff_wave_header, sizeof(riff_wave_header), 1, file);
101 if ((riff_wave_header.riff_id != ID_RIFF) ||
102 (riff_wave_header.wave_id != ID_WAVE)) {
103 fprintf(stderr, "Error: '%s' is not a riff/wave file\n", filename);
104 fclose(file);
105 return 1;
106 }
107
108 do {
109 fread(&chunk_header, sizeof(chunk_header), 1, file);
110
111 switch (chunk_header.id) {
112 case ID_FMT:
113 fread(&chunk_fmt, sizeof(chunk_fmt), 1, file);
114 /* If the format header is larger, skip the rest */
115 if (chunk_header.sz > sizeof(chunk_fmt))
116 fseek(file, chunk_header.sz - sizeof(chunk_fmt), SEEK_CUR);
117 break;
118 case ID_DATA:
119 /* Stop looking for chunks */
120 more_chunks = 0;
121 break;
122 default:
123 /* Unknown chunk, skip bytes */
124 fseek(file, chunk_header.sz, SEEK_CUR);
125 }
126 } while (more_chunks);
127
Simon Wilson62104732011-08-05 12:00:00 -0700128 /* parse command line arguments */
129 argv += 2;
Simon Wilsone9942c82011-10-13 13:57:25 -0700130 while (*argv) {
131 if (strcmp(*argv, "-d") == 0) {
132 argv++;
Simon Wilsondaa83292012-02-28 15:26:02 -0800133 if (*argv)
134 device = atoi(*argv);
Simon Wilsone9942c82011-10-13 13:57:25 -0700135 }
Simon Wilsondaa83292012-02-28 15:26:02 -0800136 if (strcmp(*argv, "-p") == 0) {
137 argv++;
138 if (*argv)
139 period_size = atoi(*argv);
140 }
141 if (strcmp(*argv, "-n") == 0) {
142 argv++;
143 if (*argv)
144 period_count = atoi(*argv);
145 }
146 if (strcmp(*argv, "-D") == 0) {
147 argv++;
148 if (*argv)
149 card = atoi(*argv);
150 }
151 if (*argv)
152 argv++;
Simon Wilson62104732011-08-05 12:00:00 -0700153 }
154
Simon Wilson85dc38f2012-05-15 17:37:19 -0700155 play_sample(file, card, device, chunk_fmt.num_channels, chunk_fmt.sample_rate,
156 chunk_fmt.bits_per_sample, period_size, period_count);
Simon Wilsonedff7082011-06-06 15:33:34 -0700157
158 fclose(file);
159
160 return 0;
161}
162
Simon Wilsondaa83292012-02-28 15:26:02 -0800163void play_sample(FILE *file, unsigned int card, unsigned int device, unsigned int channels,
164 unsigned int rate, unsigned int bits, unsigned int period_size,
165 unsigned int period_count)
Simon Wilsonedff7082011-06-06 15:33:34 -0700166{
167 struct pcm_config config;
168 struct pcm *pcm;
169 char *buffer;
170 int size;
171 int num_read;
172
173 config.channels = channels;
174 config.rate = rate;
Simon Wilsondaa83292012-02-28 15:26:02 -0800175 config.period_size = period_size;
176 config.period_count = period_count;
Simon Wilsonedff7082011-06-06 15:33:34 -0700177 if (bits == 32)
178 config.format = PCM_FORMAT_S32_LE;
179 else if (bits == 16)
180 config.format = PCM_FORMAT_S16_LE;
Simon Wilson62104732011-08-05 12:00:00 -0700181 config.start_threshold = 0;
182 config.stop_threshold = 0;
183 config.silence_threshold = 0;
Simon Wilsonedff7082011-06-06 15:33:34 -0700184
Simon Wilsondaa83292012-02-28 15:26:02 -0800185 pcm = pcm_open(card, device, PCM_OUT, &config);
Simon Wilsonedff7082011-06-06 15:33:34 -0700186 if (!pcm || !pcm_is_ready(pcm)) {
Simon Wilson62104732011-08-05 12:00:00 -0700187 fprintf(stderr, "Unable to open PCM device %u (%s)\n",
188 device, pcm_get_error(pcm));
Simon Wilsonedff7082011-06-06 15:33:34 -0700189 return;
190 }
191
Simon Wilsondaa83292012-02-28 15:26:02 -0800192 size = pcm_frames_to_bytes(pcm, pcm_get_buffer_size(pcm));
Simon Wilsonedff7082011-06-06 15:33:34 -0700193 buffer = malloc(size);
194 if (!buffer) {
195 fprintf(stderr, "Unable to allocate %d bytes\n", size);
196 free(buffer);
197 pcm_close(pcm);
198 return;
199 }
200
201 printf("Playing sample: %u ch, %u hz, %u bit\n", channels, rate, bits);
202
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800203 /* catch ctrl-c to shutdown cleanly */
204 signal(SIGINT, stream_close);
205
Simon Wilsonedff7082011-06-06 15:33:34 -0700206 do {
207 num_read = fread(buffer, 1, size, file);
208 if (num_read > 0) {
209 if (pcm_write(pcm, buffer, num_read)) {
210 fprintf(stderr, "Error playing sample\n");
211 break;
212 }
213 }
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800214 } while (!close && num_read > 0);
Simon Wilsonedff7082011-06-06 15:33:34 -0700215
216 free(buffer);
217 pcm_close(pcm);
218}
219