1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
|
/* Copyright (C) 2008 The Android Open Source Project
*/
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdint.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <linux/ioctl.h>
#define AUDIO_IOCTL_MAGIC 'a'
#define AUDIO_START _IOW(AUDIO_IOCTL_MAGIC, 0, unsigned)
#define AUDIO_STOP _IOW(AUDIO_IOCTL_MAGIC, 1, unsigned)
#define AUDIO_FLUSH _IOW(AUDIO_IOCTL_MAGIC, 2, unsigned)
#define AUDIO_GET_CONFIG _IOR(AUDIO_IOCTL_MAGIC, 3, unsigned)
#define AUDIO_SET_CONFIG _IOW(AUDIO_IOCTL_MAGIC, 4, unsigned)
#define AUDIO_GET_STATS _IOR(AUDIO_IOCTL_MAGIC, 5, unsigned)
struct msm_audio_config {
uint32_t buffer_size;
uint32_t buffer_count;
uint32_t channel_count;
uint32_t sample_rate;
uint32_t codec_type;
uint32_t unused[3];
};
struct msm_audio_stats {
uint32_t out_bytes;
uint32_t unused[3];
};
int pcm_play(unsigned rate, unsigned channels,
int (*fill)(void *buf, unsigned sz, void *cookie),
void *cookie)
{
struct msm_audio_config config;
struct msm_audio_stats stats;
unsigned sz, n;
char buf[8192];
int afd;
afd = open("/dev/msm_pcm_out", O_RDWR);
if (afd < 0) {
perror("pcm_play: cannot open audio device");
return -1;
}
if(ioctl(afd, AUDIO_GET_CONFIG, &config)) {
perror("could not get config");
return -1;
}
config.channel_count = channels;
config.sample_rate = rate;
if (ioctl(afd, AUDIO_SET_CONFIG, &config)) {
perror("could not set config");
return -1;
}
sz = config.buffer_size;
if (sz > sizeof(buf)) {
fprintf(stderr,"too big\n");
return -1;
}
fprintf(stderr,"prefill\n");
for (n = 0; n < config.buffer_count; n++) {
if (fill(buf, sz, cookie))
break;
if (write(afd, buf, sz) != sz)
break;
}
fprintf(stderr,"start\n");
ioctl(afd, AUDIO_START, 0);
for (;;) {
#if 0
if (ioctl(afd, AUDIO_GET_STATS, &stats) == 0)
fprintf(stderr,"%10d\n", stats.out_bytes);
#endif
if (fill(buf, sz, cookie))
break;
if (write(afd, buf, sz) != sz)
break;
}
done:
close(afd);
return 0;
}
/* http://ccrma.stanford.edu/courses/422/projects/WaveFormat/ */
#define ID_RIFF 0x46464952
#define ID_WAVE 0x45564157
#define ID_FMT 0x20746d66
#define ID_DATA 0x61746164
#define FORMAT_PCM 1
struct wav_header {
uint32_t riff_id;
uint32_t riff_sz;
uint32_t riff_fmt;
uint32_t fmt_id;
uint32_t fmt_sz;
uint16_t audio_format;
uint16_t num_channels;
uint32_t sample_rate;
uint32_t byte_rate; /* sample_rate * num_channels * bps / 8 */
uint16_t block_align; /* num_channels * bps / 8 */
uint16_t bits_per_sample;
uint32_t data_id;
uint32_t data_sz;
};
static char *next;
static unsigned avail;
int fill_buffer(void *buf, unsigned sz, void *cookie)
{
if (sz > avail)
return -1;
memcpy(buf, next, sz);
next += sz;
avail -= sz;
return 0;
}
void play_file(unsigned rate, unsigned channels,
int fd, unsigned count)
{
next = malloc(count);
if (!next) {
fprintf(stderr,"could not allocate %d bytes\n", count);
return;
}
if (read(fd, next, count) != count) {
fprintf(stderr,"could not read %d bytes\n", count);
return;
}
avail = count;
pcm_play(rate, channels, fill_buffer, 0);
}
int wav_play(const char *fn)
{
struct wav_header hdr;
unsigned rate, channels;
int fd;
fd = open(fn, O_RDONLY);
if (fd < 0) {
fprintf(stderr, "playwav: cannot open '%s'\n", fn);
return -1;
}
if (read(fd, &hdr, sizeof(hdr)) != sizeof(hdr)) {
fprintf(stderr, "playwav: cannot read header\n");
return -1;
}
fprintf(stderr,"playwav: %d ch, %d hz, %d bit, %s\n",
hdr.num_channels, hdr.sample_rate, hdr.bits_per_sample,
hdr.audio_format == FORMAT_PCM ? "PCM" : "unknown");
if ((hdr.riff_id != ID_RIFF) ||
(hdr.riff_fmt != ID_WAVE) ||
(hdr.fmt_id != ID_FMT)) {
fprintf(stderr, "playwav: '%s' is not a riff/wave file\n", fn);
return -1;
}
if ((hdr.audio_format != FORMAT_PCM) ||
(hdr.fmt_sz != 16)) {
fprintf(stderr, "playwav: '%s' is not pcm format\n", fn);
return -1;
}
if (hdr.bits_per_sample != 16) {
fprintf(stderr, "playwav: '%s' is not 16bit per sample\n", fn);
return -1;
}
play_file(hdr.sample_rate, hdr.num_channels,
fd, hdr.data_sz);
return 0;
}
int wav_rec(const char *fn, unsigned channels, unsigned rate)
{
struct wav_header hdr;
unsigned char buf[8192];
struct msm_audio_config cfg;
unsigned sz, n;
int fd, afd;
unsigned total = 0;
unsigned char tmp;
hdr.riff_id = ID_RIFF;
hdr.riff_sz = 0;
hdr.riff_fmt = ID_WAVE;
hdr.fmt_id = ID_FMT;
hdr.fmt_sz = 16;
hdr.audio_format = FORMAT_PCM;
hdr.num_channels = channels;
hdr.sample_rate = rate;
hdr.byte_rate = hdr.sample_rate * hdr.num_channels * 2;
hdr.block_align = hdr.num_channels * 2;
hdr.bits_per_sample = 16;
hdr.data_id = ID_DATA;
hdr.data_sz = 0;
fd = open(fn, O_CREAT | O_RDWR, 0666);
if (fd < 0) {
perror("cannot open output file");
return -1;
}
write(fd, &hdr, sizeof(hdr));
afd = open("/dev/msm_pcm_in", O_RDWR);
if (afd < 0) {
perror("cannot open msm_pcm_in");
close(fd);
return -1;
}
/* config change should be a read-modify-write operation */
if (ioctl(afd, AUDIO_GET_CONFIG, &cfg)) {
perror("cannot read audio config");
goto fail;
}
cfg.channel_count = hdr.num_channels;
cfg.sample_rate = hdr.sample_rate;
if (ioctl(afd, AUDIO_SET_CONFIG, &cfg)) {
perror("cannot write audio config");
goto fail;
}
if (ioctl(afd, AUDIO_GET_CONFIG, &cfg)) {
perror("cannot read audio config");
goto fail;
}
sz = cfg.buffer_size;
fprintf(stderr,"buffer size %d x %d\n", sz, cfg.buffer_count);
if (sz > sizeof(buf)) {
fprintf(stderr,"buffer size %d too large\n", sz);
goto fail;
}
if (ioctl(afd, AUDIO_START, 0)) {
perror("cannot start audio");
goto fail;
}
fcntl(0, F_SETFL, O_NONBLOCK);
fprintf(stderr,"\n*** RECORDING * HIT ENTER TO STOP ***\n");
for (;;) {
while (read(0, &tmp, 1) == 1) {
if ((tmp == 13) || (tmp == 10)) goto done;
}
if (read(afd, buf, sz) != sz) {
perror("cannot read buffer");
goto fail;
}
if (write(fd, buf, sz) != sz) {
perror("cannot write buffer");
goto fail;
}
total += sz;
}
done:
close(afd);
/* update lengths in header */
hdr.data_sz = total;
hdr.riff_sz = total + 8 + 16 + 8;
lseek(fd, 0, SEEK_SET);
write(fd, &hdr, sizeof(hdr));
close(fd);
return 0;
fail:
close(afd);
close(fd);
unlink(fn);
return -1;
}
int mp3_play(const char *fn)
{
char buf[64*1024];
int r;
int fd, afd;
fd = open(fn, O_RDONLY);
if (fd < 0) {
perror("cannot open mp3 file");
return -1;
}
afd = open("/dev/msm_mp3", O_RDWR);
if (afd < 0) {
close(fd);
perror("cannot open mp3 output device");
return -1;
}
fprintf(stderr,"MP3 PLAY\n");
ioctl(afd, AUDIO_START, 0);
for (;;) {
r = read(fd, buf, 64*1024);
if (r <= 0) break;
r = write(afd, buf, r);
if (r < 0) break;
}
close(fd);
close(afd);
return 0;
}
int main(int argc, char **argv)
{
const char *fn = 0;
int play = 1;
unsigned channels = 1;
unsigned rate = 44100;
argc--;
argv++;
while (argc > 0) {
if (!strcmp(argv[0],"-rec")) {
play = 0;
} else if (!strcmp(argv[0],"-play")) {
play = 1;
} else if (!strcmp(argv[0],"-stereo")) {
channels = 2;
} else if (!strcmp(argv[0],"-mono")) {
channels = 1;
} else if (!strcmp(argv[0],"-rate")) {
argc--;
argv++;
if (argc == 0) {
fprintf(stderr,"playwav: -rate requires a parameter\n");
return -1;
}
rate = atoi(argv[0]);
} else {
fn = argv[0];
}
argc--;
argv++;
}
if (fn == 0) {
fn = play ? "/data/out.wav" : "/data/rec.wav";
}
if (play) {
const char *dot = strrchr(fn, '.');
if (dot && !strcmp(dot,".mp3")) {
return mp3_play(fn);
} else {
return wav_play(fn);
}
} else {
return wav_rec(fn, channels, rate);
}
return 0;
}
|