1 /* Copyright (C) 2008 The Android Open Source Project
11 #include <linux/ioctl.h>
13 #define AUDIO_IOCTL_MAGIC 'a'
15 #define AUDIO_START _IOW(AUDIO_IOCTL_MAGIC, 0, unsigned)
16 #define AUDIO_STOP _IOW(AUDIO_IOCTL_MAGIC, 1, unsigned)
17 #define AUDIO_FLUSH _IOW(AUDIO_IOCTL_MAGIC, 2, unsigned)
18 #define AUDIO_GET_CONFIG _IOR(AUDIO_IOCTL_MAGIC, 3, unsigned)
19 #define AUDIO_SET_CONFIG _IOW(AUDIO_IOCTL_MAGIC, 4, unsigned)
20 #define AUDIO_GET_STATS _IOR(AUDIO_IOCTL_MAGIC, 5, unsigned)
22 struct msm_audio_config {
24 uint32_t buffer_count;
25 uint32_t channel_count;
31 struct msm_audio_stats {
36 int pcm_play(unsigned rate, unsigned channels,
37 int (*fill)(void *buf, unsigned sz, void *cookie),
40 struct msm_audio_config config;
41 struct msm_audio_stats stats;
46 afd = open("/dev/msm_pcm_out", O_RDWR);
48 perror("pcm_play: cannot open audio device");
52 if(ioctl(afd, AUDIO_GET_CONFIG, &config)) {
53 perror("could not get config");
57 config.channel_count = channels;
58 config.sample_rate = rate;
59 if (ioctl(afd, AUDIO_SET_CONFIG, &config)) {
60 perror("could not set config");
63 sz = config.buffer_size;
64 if (sz > sizeof(buf)) {
65 fprintf(stderr,"too big\n");
69 fprintf(stderr,"prefill\n");
70 for (n = 0; n < config.buffer_count; n++) {
71 if (fill(buf, sz, cookie))
73 if (write(afd, buf, sz) != sz)
77 fprintf(stderr,"start\n");
78 ioctl(afd, AUDIO_START, 0);
82 if (ioctl(afd, AUDIO_GET_STATS, &stats) == 0)
83 fprintf(stderr,"%10d\n", stats.out_bytes);
85 if (fill(buf, sz, cookie))
87 if (write(afd, buf, sz) != sz)
96 /* http://ccrma.stanford.edu/courses/422/projects/WaveFormat/ */
98 #define ID_RIFF 0x46464952
99 #define ID_WAVE 0x45564157
100 #define ID_FMT 0x20746d66
101 #define ID_DATA 0x61746164
111 uint16_t audio_format;
112 uint16_t num_channels;
113 uint32_t sample_rate;
114 uint32_t byte_rate; /* sample_rate * num_channels * bps / 8 */
115 uint16_t block_align; /* num_channels * bps / 8 */
116 uint16_t bits_per_sample;
123 static unsigned avail;
125 int fill_buffer(void *buf, unsigned sz, void *cookie)
129 memcpy(buf, next, sz);
135 void play_file(unsigned rate, unsigned channels,
136 int fd, unsigned count)
138 next = malloc(count);
140 fprintf(stderr,"could not allocate %d bytes\n", count);
143 if (read(fd, next, count) != count) {
144 fprintf(stderr,"could not read %d bytes\n", count);
148 pcm_play(rate, channels, fill_buffer, 0);
151 int wav_play(const char *fn)
153 struct wav_header hdr;
154 unsigned rate, channels;
156 fd = open(fn, O_RDONLY);
158 fprintf(stderr, "playwav: cannot open '%s'\n", fn);
161 if (read(fd, &hdr, sizeof(hdr)) != sizeof(hdr)) {
162 fprintf(stderr, "playwav: cannot read header\n");
165 fprintf(stderr,"playwav: %d ch, %d hz, %d bit, %s\n",
166 hdr.num_channels, hdr.sample_rate, hdr.bits_per_sample,
167 hdr.audio_format == FORMAT_PCM ? "PCM" : "unknown");
169 if ((hdr.riff_id != ID_RIFF) ||
170 (hdr.riff_fmt != ID_WAVE) ||
171 (hdr.fmt_id != ID_FMT)) {
172 fprintf(stderr, "playwav: '%s' is not a riff/wave file\n", fn);
175 if ((hdr.audio_format != FORMAT_PCM) ||
176 (hdr.fmt_sz != 16)) {
177 fprintf(stderr, "playwav: '%s' is not pcm format\n", fn);
180 if (hdr.bits_per_sample != 16) {
181 fprintf(stderr, "playwav: '%s' is not 16bit per sample\n", fn);
185 play_file(hdr.sample_rate, hdr.num_channels,
191 int wav_rec(const char *fn, unsigned channels, unsigned rate)
193 struct wav_header hdr;
194 unsigned char buf[8192];
195 struct msm_audio_config cfg;
201 hdr.riff_id = ID_RIFF;
203 hdr.riff_fmt = ID_WAVE;
206 hdr.audio_format = FORMAT_PCM;
207 hdr.num_channels = channels;
208 hdr.sample_rate = rate;
209 hdr.byte_rate = hdr.sample_rate * hdr.num_channels * 2;
210 hdr.block_align = hdr.num_channels * 2;
211 hdr.bits_per_sample = 16;
212 hdr.data_id = ID_DATA;
215 fd = open(fn, O_CREAT | O_RDWR, 0666);
217 perror("cannot open output file");
220 write(fd, &hdr, sizeof(hdr));
222 afd = open("/dev/msm_pcm_in", O_RDWR);
224 perror("cannot open msm_pcm_in");
229 /* config change should be a read-modify-write operation */
230 if (ioctl(afd, AUDIO_GET_CONFIG, &cfg)) {
231 perror("cannot read audio config");
235 cfg.channel_count = hdr.num_channels;
236 cfg.sample_rate = hdr.sample_rate;
237 if (ioctl(afd, AUDIO_SET_CONFIG, &cfg)) {
238 perror("cannot write audio config");
242 if (ioctl(afd, AUDIO_GET_CONFIG, &cfg)) {
243 perror("cannot read audio config");
247 sz = cfg.buffer_size;
248 fprintf(stderr,"buffer size %d x %d\n", sz, cfg.buffer_count);
249 if (sz > sizeof(buf)) {
250 fprintf(stderr,"buffer size %d too large\n", sz);
254 if (ioctl(afd, AUDIO_START, 0)) {
255 perror("cannot start audio");
259 fcntl(0, F_SETFL, O_NONBLOCK);
260 fprintf(stderr,"\n*** RECORDING * HIT ENTER TO STOP ***\n");
263 while (read(0, &tmp, 1) == 1) {
264 if ((tmp == 13) || (tmp == 10)) goto done;
266 if (read(afd, buf, sz) != sz) {
267 perror("cannot read buffer");
270 if (write(fd, buf, sz) != sz) {
271 perror("cannot write buffer");
280 /* update lengths in header */
282 hdr.riff_sz = total + 8 + 16 + 8;
283 lseek(fd, 0, SEEK_SET);
284 write(fd, &hdr, sizeof(hdr));
295 int mp3_play(const char *fn)
301 fd = open(fn, O_RDONLY);
303 perror("cannot open mp3 file");
307 afd = open("/dev/msm_mp3", O_RDWR);
310 perror("cannot open mp3 output device");
314 fprintf(stderr,"MP3 PLAY\n");
315 ioctl(afd, AUDIO_START, 0);
318 r = read(fd, buf, 64*1024);
320 r = write(afd, buf, r);
329 int main(int argc, char **argv)
333 unsigned channels = 1;
334 unsigned rate = 44100;
339 if (!strcmp(argv[0],"-rec")) {
341 } else if (!strcmp(argv[0],"-play")) {
343 } else if (!strcmp(argv[0],"-stereo")) {
345 } else if (!strcmp(argv[0],"-mono")) {
347 } else if (!strcmp(argv[0],"-rate")) {
351 fprintf(stderr,"playwav: -rate requires a parameter\n");
354 rate = atoi(argv[0]);
363 fn = play ? "/data/out.wav" : "/data/rec.wav";
367 const char *dot = strrchr(fn, '.');
368 if (dot && !strcmp(dot,".mp3")) {
374 return wav_rec(fn, channels, rate);