2 * Copyright (C) 2012 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <system/audio.h>
18 #include <audio_utils/sndfile.h>
19 #include <audio_utils/primitives.h>
26 #define WAVE_FORMAT_PCM 1
27 #define WAVE_FORMAT_IEEE_FLOAT 3
28 #define WAVE_FORMAT_EXTENSIBLE 0xFFFE
32 uint8_t *temp; // realloc buffer used for shrinking 16 bits to 8 bits and byte-swapping
35 size_t remaining; // frames unread for SFM_READ, frames written for SFM_WRITE
39 static unsigned little2u(unsigned char *ptr)
41 return (ptr[1] << 8) + ptr[0];
44 static unsigned little4u(unsigned char *ptr)
46 return (ptr[3] << 24) + (ptr[2] << 16) + (ptr[1] << 8) + ptr[0];
49 static int isLittleEndian(void)
51 static const short one = 1;
52 return *((const char *) &one) == 1;
55 // "swab" conflicts with OS X <string.h>
56 static void my_swab(short *ptr, size_t numToSwap)
58 while (numToSwap > 0) {
59 *ptr = little2u((unsigned char *) ptr);
65 static SNDFILE *sf_open_read(const char *path, SF_INFO *info)
67 FILE *stream = fopen(path, "rb");
70 fprintf(stderr, "fopen %s failed errno %d\n", path, errno);
75 SNDFILE *handle = (SNDFILE *) malloc(sizeof(SNDFILE));
76 handle->mode = SFM_READ;
78 handle->stream = stream;
79 handle->info.format = SF_FORMAT_WAV;
81 // don't attempt to parse all valid forms, just the most common ones
82 unsigned char wav[12];
84 actual = fread(wav, sizeof(char), sizeof(wav), stream);
87 fprintf(stderr, "actual %zu < 44\n", actual);
91 if (memcmp(wav, "RIFF", 4)) {
93 fprintf(stderr, "wav != RIFF\n");
97 unsigned riffSize = little4u(&wav[4]);
100 fprintf(stderr, "riffSize %u < 4\n", riffSize);
104 if (memcmp(&wav[8], "WAVE", 4)) {
106 fprintf(stderr, "missing WAVE\n");
110 size_t remaining = riffSize - 4;
114 while (remaining >= 8) {
115 unsigned char chunk[8];
116 actual = fread(chunk, sizeof(char), sizeof(chunk), stream);
117 if (actual != sizeof(chunk)) {
119 fprintf(stderr, "actual %zu != %zu\n", actual, sizeof(chunk));
124 unsigned chunkSize = little4u(&chunk[4]);
125 if (chunkSize > remaining) {
127 fprintf(stderr, "chunkSize %u > remaining %zu\n", chunkSize, remaining);
131 if (!memcmp(&chunk[0], "fmt ", 4)) {
134 fprintf(stderr, "multiple fmt\n");
140 fprintf(stderr, "chunkSize %u < 2\n", chunkSize);
144 unsigned char fmt[40];
145 actual = fread(fmt, sizeof(char), 2, stream);
148 fprintf(stderr, "actual %zu != 2\n", actual);
152 unsigned format = little2u(&fmt[0]);
155 case WAVE_FORMAT_PCM:
156 case WAVE_FORMAT_IEEE_FLOAT:
159 case WAVE_FORMAT_EXTENSIBLE:
164 fprintf(stderr, "unsupported format %u\n", format);
168 if (chunkSize < minSize) {
170 fprintf(stderr, "chunkSize %u < minSize %zu\n", chunkSize, minSize);
174 actual = fread(&fmt[2], sizeof(char), minSize - 2, stream);
175 if (actual != minSize - 2) {
177 fprintf(stderr, "actual %zu != %zu\n", actual, minSize - 16);
181 if (chunkSize > minSize) {
182 fseek(stream, (long) (chunkSize - minSize), SEEK_CUR);
184 unsigned channels = little2u(&fmt[2]);
185 if ((channels < 1) || (channels > FCC_8)) {
187 fprintf(stderr, "unsupported channels %u\n", channels);
191 unsigned samplerate = little4u(&fmt[4]);
192 if (samplerate == 0) {
194 fprintf(stderr, "samplerate %u == 0\n", samplerate);
199 // ignore block alignment
200 unsigned bitsPerSample = little2u(&fmt[14]);
201 if (bitsPerSample != 8 && bitsPerSample != 16 && bitsPerSample != 24 &&
202 bitsPerSample != 32) {
204 fprintf(stderr, "bitsPerSample %u != 8 or 16 or 24 or 32\n", bitsPerSample);
208 unsigned bytesPerFrame = (bitsPerSample >> 3) * channels;
209 handle->bytesPerFrame = bytesPerFrame;
210 handle->info.samplerate = samplerate;
211 handle->info.channels = channels;
212 switch (bitsPerSample) {
214 handle->info.format |= SF_FORMAT_PCM_U8;
217 handle->info.format |= SF_FORMAT_PCM_16;
220 handle->info.format |= SF_FORMAT_PCM_24;
223 if (format == WAVE_FORMAT_IEEE_FLOAT)
224 handle->info.format |= SF_FORMAT_FLOAT;
226 handle->info.format |= SF_FORMAT_PCM_32;
230 } else if (!memcmp(&chunk[0], "data", 4)) {
233 fprintf(stderr, "data not preceded by fmt\n");
239 fprintf(stderr, "multiple data\n");
243 handle->remaining = chunkSize / handle->bytesPerFrame;
244 handle->info.frames = handle->remaining;
245 dataTell = ftell(stream);
247 fseek(stream, (long) chunkSize, SEEK_CUR);
250 } else if (!memcmp(&chunk[0], "fact", 4)) {
253 fseek(stream, (long) chunkSize, SEEK_CUR);
256 // ignore unknown chunk
258 fprintf(stderr, "ignoring unknown chunk %c%c%c%c\n",
259 chunk[0], chunk[1], chunk[2], chunk[3]);
262 fseek(stream, (long) chunkSize, SEEK_CUR);
265 remaining -= chunkSize;
269 fprintf(stderr, "partial chunk at end of RIFF, remaining %zu\n", remaining);
275 fprintf(stderr, "missing data\n");
279 (void) fseek(stream, dataTell, SEEK_SET);
280 *info = handle->info;
289 static void write4u(unsigned char *ptr, unsigned u)
297 static SNDFILE *sf_open_write(const char *path, SF_INFO *info)
299 int sub = info->format & SF_FORMAT_SUBMASK;
301 (info->samplerate > 0) &&
302 (info->channels > 0 && info->channels <= FCC_8) &&
303 ((info->format & SF_FORMAT_TYPEMASK) == SF_FORMAT_WAV) &&
304 (sub == SF_FORMAT_PCM_16 || sub == SF_FORMAT_PCM_U8 || sub == SF_FORMAT_FLOAT ||
305 sub == SF_FORMAT_PCM_24 || sub == SF_FORMAT_PCM_32)
309 FILE *stream = fopen(path, "w+b");
310 if (stream == NULL) {
312 fprintf(stderr, "fopen %s failed errno %d\n", path, errno);
316 unsigned char wav[58];
317 memset(wav, 0, sizeof(wav));
318 memcpy(wav, "RIFF", 4);
319 memcpy(&wav[8], "WAVEfmt ", 8);
320 if (sub == SF_FORMAT_FLOAT) {
321 wav[4] = 50; // riffSize
322 wav[16] = 18; // fmtSize
323 wav[20] = WAVE_FORMAT_IEEE_FLOAT;
325 wav[4] = 36; // riffSize
326 wav[16] = 16; // fmtSize
327 wav[20] = WAVE_FORMAT_PCM;
329 wav[22] = info->channels;
330 write4u(&wav[24], info->samplerate);
331 unsigned bitsPerSample;
333 case SF_FORMAT_PCM_16:
336 case SF_FORMAT_PCM_U8:
339 case SF_FORMAT_FLOAT:
342 case SF_FORMAT_PCM_24:
345 case SF_FORMAT_PCM_32:
348 default: // not reachable
352 unsigned blockAlignment = (bitsPerSample >> 3) * info->channels;
353 unsigned byteRate = info->samplerate * blockAlignment;
354 write4u(&wav[28], byteRate);
355 wav[32] = blockAlignment;
356 wav[34] = bitsPerSample;
358 if (sub == SF_FORMAT_FLOAT) {
359 memcpy(&wav[38], "fact", 4);
361 memcpy(&wav[50], "data", 4);
364 memcpy(&wav[36], "data", 4);
365 // dataSize is initially zero
366 (void) fwrite(wav, 44 + extra, 1, stream);
367 SNDFILE *handle = (SNDFILE *) malloc(sizeof(SNDFILE));
368 handle->mode = SFM_WRITE;
370 handle->stream = stream;
371 handle->bytesPerFrame = blockAlignment;
372 handle->remaining = 0;
373 handle->info = *info;
377 SNDFILE *sf_open(const char *path, int mode, SF_INFO *info)
379 if (path == NULL || info == NULL) {
381 fprintf(stderr, "path=%p info=%p\n", path, info);
387 return sf_open_read(path, info);
389 return sf_open_write(path, info);
392 fprintf(stderr, "mode=%d\n", mode);
398 void sf_close(SNDFILE *handle)
403 if (handle->mode == SFM_WRITE) {
404 (void) fflush(handle->stream);
405 rewind(handle->stream);
406 unsigned char wav[58];
407 size_t extra = (handle->info.format & SF_FORMAT_SUBMASK) == SF_FORMAT_FLOAT ? 14 : 0;
408 (void) fread(wav, 44 + extra, 1, handle->stream);
409 unsigned dataSize = handle->remaining * handle->bytesPerFrame;
410 write4u(&wav[4], dataSize + 36 + extra); // riffSize
411 write4u(&wav[40 + extra], dataSize); // dataSize
412 rewind(handle->stream);
413 (void) fwrite(wav, 44 + extra, 1, handle->stream);
415 (void) fclose(handle->stream);
419 sf_count_t sf_readf_short(SNDFILE *handle, short *ptr, sf_count_t desiredFrames)
421 if (handle == NULL || handle->mode != SFM_READ || ptr == NULL || !handle->remaining ||
422 desiredFrames <= 0) {
425 if (handle->remaining < (size_t) desiredFrames) {
426 desiredFrames = handle->remaining;
428 // does not check for numeric overflow
429 size_t desiredBytes = desiredFrames * handle->bytesPerFrame;
432 unsigned format = handle->info.format & SF_FORMAT_SUBMASK;
433 if (format == SF_FORMAT_PCM_32 || format == SF_FORMAT_FLOAT || format == SF_FORMAT_PCM_24) {
434 temp = malloc(desiredBytes);
435 actualBytes = fread(temp, sizeof(char), desiredBytes, handle->stream);
437 actualBytes = fread(ptr, sizeof(char), desiredBytes, handle->stream);
439 size_t actualFrames = actualBytes / handle->bytesPerFrame;
440 handle->remaining -= actualFrames;
442 case SF_FORMAT_PCM_U8:
443 memcpy_to_i16_from_u8(ptr, (unsigned char *) ptr, actualFrames * handle->info.channels);
445 case SF_FORMAT_PCM_16:
446 if (!isLittleEndian())
447 my_swab(ptr, actualFrames * handle->info.channels);
449 case SF_FORMAT_PCM_32:
450 memcpy_to_i16_from_i32(ptr, (const int *) temp, actualFrames * handle->info.channels);
453 case SF_FORMAT_FLOAT:
454 memcpy_to_i16_from_float(ptr, (const float *) temp, actualFrames * handle->info.channels);
457 case SF_FORMAT_PCM_24:
458 memcpy_to_i16_from_p24(ptr, (const uint8_t *) temp, actualFrames * handle->info.channels);
462 memset(ptr, 0, actualFrames * handle->info.channels * sizeof(short));
468 sf_count_t sf_readf_float(SNDFILE *handle, float *ptr, sf_count_t desiredFrames)
470 if (handle == NULL || handle->mode != SFM_READ || ptr == NULL || !handle->remaining ||
471 desiredFrames <= 0) {
474 if (handle->remaining < (size_t) desiredFrames) {
475 desiredFrames = handle->remaining;
477 // does not check for numeric overflow
478 size_t desiredBytes = desiredFrames * handle->bytesPerFrame;
481 unsigned format = handle->info.format & SF_FORMAT_SUBMASK;
482 if (format == SF_FORMAT_PCM_16 || format == SF_FORMAT_PCM_U8 || format == SF_FORMAT_PCM_24) {
483 temp = malloc(desiredBytes);
484 actualBytes = fread(temp, sizeof(char), desiredBytes, handle->stream);
486 actualBytes = fread(ptr, sizeof(char), desiredBytes, handle->stream);
488 size_t actualFrames = actualBytes / handle->bytesPerFrame;
489 handle->remaining -= actualFrames;
491 case SF_FORMAT_PCM_U8:
494 memcpy_to_float_from_u8(ptr, (const unsigned char *) temp,
495 actualFrames * handle->info.channels);
499 case SF_FORMAT_PCM_16:
500 memcpy_to_float_from_i16(ptr, (const short *) temp, actualFrames * handle->info.channels);
503 case SF_FORMAT_PCM_32:
504 memcpy_to_float_from_i32(ptr, (const int *) ptr, actualFrames * handle->info.channels);
506 case SF_FORMAT_FLOAT:
508 case SF_FORMAT_PCM_24:
509 memcpy_to_float_from_p24(ptr, (const uint8_t *) temp, actualFrames * handle->info.channels);
513 memset(ptr, 0, actualFrames * handle->info.channels * sizeof(float));
519 sf_count_t sf_readf_int(SNDFILE *handle, int *ptr, sf_count_t desiredFrames)
521 if (handle == NULL || handle->mode != SFM_READ || ptr == NULL || !handle->remaining ||
522 desiredFrames <= 0) {
525 if (handle->remaining < (size_t) desiredFrames) {
526 desiredFrames = handle->remaining;
528 // does not check for numeric overflow
529 size_t desiredBytes = desiredFrames * handle->bytesPerFrame;
531 unsigned format = handle->info.format & SF_FORMAT_SUBMASK;
533 if (format == SF_FORMAT_PCM_16 || format == SF_FORMAT_PCM_U8 || format == SF_FORMAT_PCM_24) {
534 temp = malloc(desiredBytes);
535 actualBytes = fread(temp, sizeof(char), desiredBytes, handle->stream);
537 actualBytes = fread(ptr, sizeof(char), desiredBytes, handle->stream);
539 size_t actualFrames = actualBytes / handle->bytesPerFrame;
540 handle->remaining -= actualFrames;
542 case SF_FORMAT_PCM_U8:
545 memcpy_to_i32_from_u8(ptr, (const unsigned char *) temp,
546 actualFrames * handle->info.channels);
550 case SF_FORMAT_PCM_16:
551 memcpy_to_i32_from_i16(ptr, (const short *) temp, actualFrames * handle->info.channels);
554 case SF_FORMAT_PCM_32:
556 case SF_FORMAT_FLOAT:
557 memcpy_to_i32_from_float(ptr, (const float *) ptr, actualFrames * handle->info.channels);
559 case SF_FORMAT_PCM_24:
560 memcpy_to_i32_from_p24(ptr, (const uint8_t *) temp, actualFrames * handle->info.channels);
564 memset(ptr, 0, actualFrames * handle->info.channels * sizeof(int));
570 sf_count_t sf_writef_short(SNDFILE *handle, const short *ptr, sf_count_t desiredFrames)
572 if (handle == NULL || handle->mode != SFM_WRITE || ptr == NULL || desiredFrames <= 0)
574 size_t desiredBytes = desiredFrames * handle->bytesPerFrame;
575 size_t actualBytes = 0;
576 switch (handle->info.format & SF_FORMAT_SUBMASK) {
577 case SF_FORMAT_PCM_U8:
578 handle->temp = realloc(handle->temp, desiredBytes);
579 memcpy_to_u8_from_i16(handle->temp, ptr, desiredBytes);
580 actualBytes = fwrite(handle->temp, sizeof(char), desiredBytes, handle->stream);
582 case SF_FORMAT_PCM_16:
583 // does not check for numeric overflow
584 if (isLittleEndian()) {
585 actualBytes = fwrite(ptr, sizeof(char), desiredBytes, handle->stream);
587 handle->temp = realloc(handle->temp, desiredBytes);
588 memcpy(handle->temp, ptr, desiredBytes);
589 my_swab((short *) handle->temp, desiredFrames * handle->info.channels);
590 actualBytes = fwrite(handle->temp, sizeof(char), desiredBytes, handle->stream);
593 case SF_FORMAT_FLOAT:
594 handle->temp = realloc(handle->temp, desiredBytes);
595 memcpy_to_float_from_i16((float *) handle->temp, ptr,
596 desiredFrames * handle->info.channels);
597 actualBytes = fwrite(handle->temp, sizeof(char), desiredBytes, handle->stream);
602 size_t actualFrames = actualBytes / handle->bytesPerFrame;
603 handle->remaining += actualFrames;
607 sf_count_t sf_writef_float(SNDFILE *handle, const float *ptr, sf_count_t desiredFrames)
609 if (handle == NULL || handle->mode != SFM_WRITE || ptr == NULL || desiredFrames <= 0)
611 size_t desiredBytes = desiredFrames * handle->bytesPerFrame;
612 size_t actualBytes = 0;
613 switch (handle->info.format & SF_FORMAT_SUBMASK) {
614 case SF_FORMAT_FLOAT:
615 actualBytes = fwrite(ptr, sizeof(char), desiredBytes, handle->stream);
617 case SF_FORMAT_PCM_16:
618 handle->temp = realloc(handle->temp, desiredBytes);
619 memcpy_to_i16_from_float((short *) handle->temp, ptr,
620 desiredFrames * handle->info.channels);
621 actualBytes = fwrite(handle->temp, sizeof(char), desiredBytes, handle->stream);
623 case SF_FORMAT_PCM_U8: // transcoding from float to byte not yet implemented
627 size_t actualFrames = actualBytes / handle->bytesPerFrame;
628 handle->remaining += actualFrames;
632 sf_count_t sf_writef_int(SNDFILE *handle, const int *ptr, sf_count_t desiredFrames)
634 if (handle == NULL || handle->mode != SFM_WRITE || ptr == NULL || desiredFrames <= 0)
636 size_t desiredBytes = desiredFrames * handle->bytesPerFrame;
637 size_t actualBytes = 0;
638 switch (handle->info.format & SF_FORMAT_SUBMASK) {
639 case SF_FORMAT_PCM_32:
640 actualBytes = fwrite(ptr, sizeof(char), desiredBytes, handle->stream);
642 default: // transcoding from other formats not yet implemented
645 size_t actualFrames = actualBytes / handle->bytesPerFrame;
646 handle->remaining += actualFrames;