blob: 915a1eae953288c40cd4334304cb44eff6f0f04a [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>
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
41struct 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
Simon Wilson62104732011-08-05 12:00:00 -070057void play_sample(FILE *file, unsigned int device, unsigned int channels,
58 unsigned int rate, unsigned int bits);
Simon Wilsonedff7082011-06-06 15:33:34 -070059
60int main(int argc, char **argv)
61{
62 FILE *file;
63 struct wav_header header;
Simon Wilson62104732011-08-05 12:00:00 -070064 unsigned int device = 0;
Simon Wilsonedff7082011-06-06 15:33:34 -070065
Simon Wilson62104732011-08-05 12:00:00 -070066 if (argc < 2) {
67 fprintf(stderr, "Usage: %s file.wav [-d device]\n", argv[0]);
Simon Wilsonedff7082011-06-06 15:33:34 -070068 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
Simon Wilson62104732011-08-05 12:00:00 -070077 /* parse command line arguments */
78 argv += 2;
Simon Wilsone9942c82011-10-13 13:57:25 -070079 while (*argv) {
80 if (strcmp(*argv, "-d") == 0) {
81 argv++;
82 device = atoi(*argv);
83 }
Simon Wilson62104732011-08-05 12:00:00 -070084 argv++;
Simon Wilson62104732011-08-05 12:00:00 -070085 }
86
Simon Wilsonedff7082011-06-06 15:33:34 -070087 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
Simon Wilson62104732011-08-05 12:00:00 -070099 play_sample(file, device, header.num_channels, header.sample_rate,
Simon Wilsonedff7082011-06-06 15:33:34 -0700100 header.bits_per_sample);
101
102 fclose(file);
103
104 return 0;
105}
106
Simon Wilson62104732011-08-05 12:00:00 -0700107void play_sample(FILE *file, unsigned int device, unsigned int channels,
108 unsigned int rate, unsigned int bits)
Simon Wilsonedff7082011-06-06 15:33:34 -0700109{
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;
Simon Wilson62104732011-08-05 12:00:00 -0700124 config.start_threshold = 0;
125 config.stop_threshold = 0;
126 config.silence_threshold = 0;
Simon Wilsonedff7082011-06-06 15:33:34 -0700127
Simon Wilson62104732011-08-05 12:00:00 -0700128 pcm = pcm_open(0, device, PCM_OUT, &config);
Simon Wilsonedff7082011-06-06 15:33:34 -0700129 if (!pcm || !pcm_is_ready(pcm)) {
Simon Wilson62104732011-08-05 12:00:00 -0700130 fprintf(stderr, "Unable to open PCM device %u (%s)\n",
131 device, pcm_get_error(pcm));
Simon Wilsonedff7082011-06-06 15:33:34 -0700132 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