1 /* Copyright (C) 2008 The Android Open Source Project
10 #include <sys/ioctl.h>
13 #include <linux/ioctl.h>
15 #define AUDIO_IOCTL_MAGIC 'a'
17 #define AUDIO_START _IOW(AUDIO_IOCTL_MAGIC, 0, unsigned)
18 #define AUDIO_STOP _IOW(AUDIO_IOCTL_MAGIC, 1, unsigned)
19 #define AUDIO_FLUSH _IOW(AUDIO_IOCTL_MAGIC, 2, unsigned)
20 #define AUDIO_GET_CONFIG _IOR(AUDIO_IOCTL_MAGIC, 3, unsigned)
21 #define AUDIO_SET_CONFIG _IOW(AUDIO_IOCTL_MAGIC, 4, unsigned)
22 #define AUDIO_GET_STATS _IOR(AUDIO_IOCTL_MAGIC, 5, unsigned)
24 struct msm_audio_config {
26 uint32_t buffer_count;
27 uint32_t channel_count;
33 struct msm_audio_stats {
38 int pcm_play(unsigned rate, unsigned channels,
39 int (*fill)(void *buf, unsigned sz, void *cookie),
42 struct msm_audio_config config;
43 struct msm_audio_stats stats;
48 afd = open("/dev/msm_pcm_out", O_RDWR);
50 perror("pcm_play: cannot open audio device");
54 if(ioctl(afd, AUDIO_GET_CONFIG, &config)) {
55 perror("could not get config");
59 config.channel_count = channels;
60 config.sample_rate = rate;
61 if (ioctl(afd, AUDIO_SET_CONFIG, &config)) {
62 perror("could not set config");
65 sz = config.buffer_size;
66 if (sz > sizeof(buf)) {
67 fprintf(stderr,"too big\n");
71 fprintf(stderr,"prefill\n");
72 for (n = 0; n < config.buffer_count; n++) {
73 if (fill(buf, sz, cookie))
75 if (write(afd, buf, sz) != sz)
79 fprintf(stderr,"start\n");
80 ioctl(afd, AUDIO_START, 0);
84 if (ioctl(afd, AUDIO_GET_STATS, &stats) == 0)
85 fprintf(stderr,"%10d\n", stats.out_bytes);
87 if (fill(buf, sz, cookie))
89 if (write(afd, buf, sz) != sz)
98 /* http://ccrma.stanford.edu/courses/422/projects/WaveFormat/ */
100 #define ID_RIFF 0x46464952
101 #define ID_WAVE 0x45564157
102 #define ID_FMT 0x20746d66
103 #define ID_DATA 0x61746164
113 uint16_t audio_format;
114 uint16_t num_channels;
115 uint32_t sample_rate;
116 uint32_t byte_rate; /* sample_rate * num_channels * bps / 8 */
117 uint16_t block_align; /* num_channels * bps / 8 */
118 uint16_t bits_per_sample;
125 static unsigned avail;
127 int fill_buffer(void *buf, unsigned sz, void *cookie)
131 memcpy(buf, next, sz);
137 void play_file(unsigned rate, unsigned channels,
138 int fd, unsigned count)
140 next = malloc(count);
142 fprintf(stderr,"could not allocate %d bytes\n", count);
145 if (read(fd, next, count) != count) {
146 fprintf(stderr,"could not read %d bytes\n", count);
150 pcm_play(rate, channels, fill_buffer, 0);
153 int wav_play(const char *fn)
155 struct wav_header hdr;
156 unsigned rate, channels;
158 fd = open(fn, O_RDONLY);
160 fprintf(stderr, "playwav: cannot open '%s'\n", fn);
163 if (read(fd, &hdr, sizeof(hdr)) != sizeof(hdr)) {
164 fprintf(stderr, "playwav: cannot read header\n");
167 fprintf(stderr,"playwav: %d ch, %d hz, %d bit, %s\n",
168 hdr.num_channels, hdr.sample_rate, hdr.bits_per_sample,
169 hdr.audio_format == FORMAT_PCM ? "PCM" : "unknown");
171 if ((hdr.riff_id != ID_RIFF) ||
172 (hdr.riff_fmt != ID_WAVE) ||
173 (hdr.fmt_id != ID_FMT)) {
174 fprintf(stderr, "playwav: '%s' is not a riff/wave file\n", fn);
177 if ((hdr.audio_format != FORMAT_PCM) ||
178 (hdr.fmt_sz != 16)) {
179 fprintf(stderr, "playwav: '%s' is not pcm format\n", fn);
182 if (hdr.bits_per_sample != 16) {
183 fprintf(stderr, "playwav: '%s' is not 16bit per sample\n", fn);
187 play_file(hdr.sample_rate, hdr.num_channels,
193 int wav_rec(const char *fn, unsigned channels, unsigned rate)
195 struct wav_header hdr;
196 unsigned char buf[8192];
197 struct msm_audio_config cfg;
203 hdr.riff_id = ID_RIFF;
205 hdr.riff_fmt = ID_WAVE;
208 hdr.audio_format = FORMAT_PCM;
209 hdr.num_channels = channels;
210 hdr.sample_rate = rate;
211 hdr.byte_rate = hdr.sample_rate * hdr.num_channels * 2;
212 hdr.block_align = hdr.num_channels * 2;
213 hdr.bits_per_sample = 16;
214 hdr.data_id = ID_DATA;
217 fd = open(fn, O_CREAT | O_RDWR, 0666);
219 perror("cannot open output file");
222 write(fd, &hdr, sizeof(hdr));
224 afd = open("/dev/msm_pcm_in", O_RDWR);
226 perror("cannot open msm_pcm_in");
231 /* config change should be a read-modify-write operation */
232 if (ioctl(afd, AUDIO_GET_CONFIG, &cfg)) {
233 perror("cannot read audio config");
237 cfg.channel_count = hdr.num_channels;
238 cfg.sample_rate = hdr.sample_rate;
239 if (ioctl(afd, AUDIO_SET_CONFIG, &cfg)) {
240 perror("cannot write audio config");
244 if (ioctl(afd, AUDIO_GET_CONFIG, &cfg)) {
245 perror("cannot read audio config");
249 sz = cfg.buffer_size;
250 fprintf(stderr,"buffer size %d x %d\n", sz, cfg.buffer_count);
251 if (sz > sizeof(buf)) {
252 fprintf(stderr,"buffer size %d too large\n", sz);
256 if (ioctl(afd, AUDIO_START, 0)) {
257 perror("cannot start audio");
261 fcntl(0, F_SETFL, O_NONBLOCK);
262 fprintf(stderr,"\n*** RECORDING * HIT ENTER TO STOP ***\n");
265 while (read(0, &tmp, 1) == 1) {
266 if ((tmp == 13) || (tmp == 10)) goto done;
268 if (read(afd, buf, sz) != sz) {
269 perror("cannot read buffer");
272 if (write(fd, buf, sz) != sz) {
273 perror("cannot write buffer");
282 /* update lengths in header */
284 hdr.riff_sz = total + 8 + 16 + 8;
285 lseek(fd, 0, SEEK_SET);
286 write(fd, &hdr, sizeof(hdr));
297 int mp3_play(const char *fn)
303 fd = open(fn, O_RDONLY);
305 perror("cannot open mp3 file");
309 afd = open("/dev/msm_mp3", O_RDWR);
312 perror("cannot open mp3 output device");
316 fprintf(stderr,"MP3 PLAY\n");
317 ioctl(afd, AUDIO_START, 0);
320 r = read(fd, buf, 64*1024);
322 r = write(afd, buf, r);
331 int main(int argc, char **argv)
335 unsigned channels = 1;
336 unsigned rate = 44100;
341 if (!strcmp(argv[0],"-rec")) {
343 } else if (!strcmp(argv[0],"-play")) {
345 } else if (!strcmp(argv[0],"-stereo")) {
347 } else if (!strcmp(argv[0],"-mono")) {
349 } else if (!strcmp(argv[0],"-rate")) {
353 fprintf(stderr,"playwav: -rate requires a parameter\n");
356 rate = atoi(argv[0]);
365 fn = play ? "/data/out.wav" : "/data/rec.wav";
369 const char *dot = strrchr(fn, '.');
370 if (dot && !strcmp(dot,".mp3")) {
376 return wav_rec(fn, channels, rate);