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 <audio_utils/sndfile.h>
18 #include <audio_utils/primitives.h>
25 #define WAVE_FORMAT_PCM 1
26 #define WAVE_FORMAT_IEEE_FLOAT 3
27 #define WAVE_FORMAT_EXTENSIBLE 0xFFFE
31 uint8_t *temp; // realloc buffer used for shrinking 16 bits to 8 bits and byte-swapping
34 size_t remaining; // frames unread for SFM_READ, frames written for SFM_WRITE
38 static unsigned little2u(unsigned char *ptr)
40 return (ptr[1] << 8) + ptr[0];
43 static unsigned little4u(unsigned char *ptr)
45 return (ptr[3] << 24) + (ptr[2] << 16) + (ptr[1] << 8) + ptr[0];
48 static int isLittleEndian(void)
50 static const short one = 1;
51 return *((const char *) &one) == 1;
54 // "swab" conflicts with OS X <string.h>
55 static void my_swab(short *ptr, size_t numToSwap)
57 while (numToSwap > 0) {
58 *ptr = little2u((unsigned char *) ptr);
64 static SNDFILE *sf_open_read(const char *path, SF_INFO *info)
66 FILE *stream = fopen(path, "rb");
69 fprintf(stderr, "fopen %s failed errno %d\n", path, errno);
74 SNDFILE *handle = (SNDFILE *) malloc(sizeof(SNDFILE));
75 handle->mode = SFM_READ;
77 handle->stream = stream;
78 handle->info.format = SF_FORMAT_WAV;
80 // don't attempt to parse all valid forms, just the most common ones
81 unsigned char wav[12];
83 actual = fread(wav, sizeof(char), sizeof(wav), stream);
86 fprintf(stderr, "actual %zu < 44\n", actual);
90 if (memcmp(wav, "RIFF", 4)) {
92 fprintf(stderr, "wav != RIFF\n");
96 unsigned riffSize = little4u(&wav[4]);
99 fprintf(stderr, "riffSize %u < 4\n", riffSize);
103 if (memcmp(&wav[8], "WAVE", 4)) {
105 fprintf(stderr, "missing WAVE\n");
109 size_t remaining = riffSize - 4;
113 while (remaining >= 8) {
114 unsigned char chunk[8];
115 actual = fread(chunk, sizeof(char), sizeof(chunk), stream);
116 if (actual != sizeof(chunk)) {
118 fprintf(stderr, "actual %zu != %zu\n", actual, sizeof(chunk));
123 unsigned chunkSize = little4u(&chunk[4]);
124 if (chunkSize > remaining) {
126 fprintf(stderr, "chunkSize %u > remaining %zu\n", chunkSize, remaining);
130 if (!memcmp(&chunk[0], "fmt ", 4)) {
133 fprintf(stderr, "multiple fmt\n");
139 fprintf(stderr, "chunkSize %u < 2\n", chunkSize);
143 unsigned char fmt[40];
144 actual = fread(fmt, sizeof(char), 2, stream);
147 fprintf(stderr, "actual %zu != 2\n", actual);
151 unsigned format = little2u(&fmt[0]);
154 case WAVE_FORMAT_PCM:
155 case WAVE_FORMAT_IEEE_FLOAT:
158 case WAVE_FORMAT_EXTENSIBLE:
163 fprintf(stderr, "unsupported format %u\n", format);
167 if (chunkSize < minSize) {
169 fprintf(stderr, "chunkSize %u < minSize %zu\n", chunkSize, minSize);
173 actual = fread(&fmt[2], sizeof(char), minSize - 2, stream);
174 if (actual != minSize - 2) {
176 fprintf(stderr, "actual %zu != %zu\n", actual, minSize - 16);
180 if (chunkSize > minSize) {
181 fseek(stream, (long) (chunkSize - minSize), SEEK_CUR);
183 unsigned channels = little2u(&fmt[2]);
185 if (channels != 1 && channels != 2 && channels != 4 && channels != 6 && channels != 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) &&
303 (info->channels > 0 && info->channels <= 8) &&
304 ((info->format & SF_FORMAT_TYPEMASK) == SF_FORMAT_WAV) &&
305 (sub == SF_FORMAT_PCM_16 || sub == SF_FORMAT_PCM_U8 || sub == SF_FORMAT_FLOAT ||
306 sub == SF_FORMAT_PCM_24 || sub == SF_FORMAT_PCM_32)
310 FILE *stream = fopen(path, "w+b");
311 if (stream == NULL) {
313 fprintf(stderr, "fopen %s failed errno %d\n", path, errno);
317 unsigned char wav[58];
318 memset(wav, 0, sizeof(wav));
319 memcpy(wav, "RIFF", 4);
320 memcpy(&wav[8], "WAVEfmt ", 8);
321 if (sub == SF_FORMAT_FLOAT) {
322 wav[4] = 50; // riffSize
323 wav[16] = 18; // fmtSize
324 wav[20] = WAVE_FORMAT_IEEE_FLOAT;
326 wav[4] = 36; // riffSize
327 wav[16] = 16; // fmtSize
328 wav[20] = WAVE_FORMAT_PCM;
330 wav[22] = info->channels;
331 write4u(&wav[24], info->samplerate);
332 unsigned bitsPerSample;
334 case SF_FORMAT_PCM_16:
337 case SF_FORMAT_PCM_U8:
340 case SF_FORMAT_FLOAT:
343 case SF_FORMAT_PCM_24:
346 case SF_FORMAT_PCM_32:
349 default: // not reachable
353 unsigned blockAlignment = (bitsPerSample >> 3) * info->channels;
354 unsigned byteRate = info->samplerate * blockAlignment;
355 write4u(&wav[28], byteRate);
356 wav[32] = blockAlignment;
357 wav[34] = bitsPerSample;
359 if (sub == SF_FORMAT_FLOAT) {
360 memcpy(&wav[38], "fact", 4);
362 memcpy(&wav[50], "data", 4);
365 memcpy(&wav[36], "data", 4);
366 // dataSize is initially zero
367 (void) fwrite(wav, 44 + extra, 1, stream);
368 SNDFILE *handle = (SNDFILE *) malloc(sizeof(SNDFILE));
369 handle->mode = SFM_WRITE;
371 handle->stream = stream;
372 handle->bytesPerFrame = blockAlignment;
373 handle->remaining = 0;
374 handle->info = *info;
378 SNDFILE *sf_open(const char *path, int mode, SF_INFO *info)
380 if (path == NULL || info == NULL) {
382 fprintf(stderr, "path=%p info=%p\n", path, info);
388 return sf_open_read(path, info);
390 return sf_open_write(path, info);
393 fprintf(stderr, "mode=%d\n", mode);
399 void sf_close(SNDFILE *handle)
404 if (handle->mode == SFM_WRITE) {
405 (void) fflush(handle->stream);
406 rewind(handle->stream);
407 unsigned char wav[58];
408 size_t extra = (handle->info.format & SF_FORMAT_SUBMASK) == SF_FORMAT_FLOAT ? 14 : 0;
409 (void) fread(wav, 44 + extra, 1, handle->stream);
410 unsigned dataSize = handle->remaining * handle->bytesPerFrame;
411 write4u(&wav[4], dataSize + 36 + extra); // riffSize
412 write4u(&wav[40 + extra], dataSize); // dataSize
413 rewind(handle->stream);
414 (void) fwrite(wav, 44 + extra, 1, handle->stream);
416 (void) fclose(handle->stream);
420 sf_count_t sf_readf_short(SNDFILE *handle, short *ptr, sf_count_t desiredFrames)
422 if (handle == NULL || handle->mode != SFM_READ || ptr == NULL || !handle->remaining ||
423 desiredFrames <= 0) {
426 if (handle->remaining < (size_t) desiredFrames) {
427 desiredFrames = handle->remaining;
429 // does not check for numeric overflow
430 size_t desiredBytes = desiredFrames * handle->bytesPerFrame;
433 unsigned format = handle->info.format & SF_FORMAT_SUBMASK;
434 if (format == SF_FORMAT_PCM_32 || format == SF_FORMAT_FLOAT || format == SF_FORMAT_PCM_24) {
435 temp = malloc(desiredBytes);
436 actualBytes = fread(temp, sizeof(char), desiredBytes, handle->stream);
438 actualBytes = fread(ptr, sizeof(char), desiredBytes, handle->stream);
440 size_t actualFrames = actualBytes / handle->bytesPerFrame;
441 handle->remaining -= actualFrames;
443 case SF_FORMAT_PCM_U8:
444 memcpy_to_i16_from_u8(ptr, (unsigned char *) ptr, actualFrames * handle->info.channels);
446 case SF_FORMAT_PCM_16:
447 if (!isLittleEndian())
448 my_swab(ptr, actualFrames * handle->info.channels);
450 case SF_FORMAT_PCM_32:
451 memcpy_to_i16_from_i32(ptr, (const int *) temp, actualFrames * handle->info.channels);
454 case SF_FORMAT_FLOAT:
455 memcpy_to_i16_from_float(ptr, (const float *) temp, actualFrames * handle->info.channels);
458 case SF_FORMAT_PCM_24:
459 memcpy_to_i16_from_p24(ptr, (const uint8_t *) temp, actualFrames * handle->info.channels);
463 memset(ptr, 0, actualFrames * handle->info.channels * sizeof(short));
469 sf_count_t sf_readf_float(SNDFILE *handle, float *ptr, sf_count_t desiredFrames)
471 if (handle == NULL || handle->mode != SFM_READ || ptr == NULL || !handle->remaining ||
472 desiredFrames <= 0) {
475 if (handle->remaining < (size_t) desiredFrames) {
476 desiredFrames = handle->remaining;
478 // does not check for numeric overflow
479 size_t desiredBytes = desiredFrames * handle->bytesPerFrame;
482 unsigned format = handle->info.format & SF_FORMAT_SUBMASK;
483 if (format == SF_FORMAT_PCM_16 || format == SF_FORMAT_PCM_U8 || format == SF_FORMAT_PCM_24) {
484 temp = malloc(desiredBytes);
485 actualBytes = fread(temp, sizeof(char), desiredBytes, handle->stream);
487 actualBytes = fread(ptr, sizeof(char), desiredBytes, handle->stream);
489 size_t actualFrames = actualBytes / handle->bytesPerFrame;
490 handle->remaining -= actualFrames;
492 case SF_FORMAT_PCM_U8:
495 memcpy_to_float_from_u8(ptr, (const unsigned char *) temp,
496 actualFrames * handle->info.channels);
500 case SF_FORMAT_PCM_16:
501 memcpy_to_float_from_i16(ptr, (const short *) temp, actualFrames * handle->info.channels);
504 case SF_FORMAT_PCM_32:
505 memcpy_to_float_from_i32(ptr, (const int *) ptr, actualFrames * handle->info.channels);
507 case SF_FORMAT_FLOAT:
509 case SF_FORMAT_PCM_24:
510 memcpy_to_float_from_p24(ptr, (const uint8_t *) temp, actualFrames * handle->info.channels);
514 memset(ptr, 0, actualFrames * handle->info.channels * sizeof(float));
520 sf_count_t sf_readf_int(SNDFILE *handle, int *ptr, sf_count_t desiredFrames)
522 if (handle == NULL || handle->mode != SFM_READ || ptr == NULL || !handle->remaining ||
523 desiredFrames <= 0) {
526 if (handle->remaining < (size_t) desiredFrames) {
527 desiredFrames = handle->remaining;
529 // does not check for numeric overflow
530 size_t desiredBytes = desiredFrames * handle->bytesPerFrame;
532 unsigned format = handle->info.format & SF_FORMAT_SUBMASK;
534 if (format == SF_FORMAT_PCM_16 || format == SF_FORMAT_PCM_U8 || format == SF_FORMAT_PCM_24) {
535 temp = malloc(desiredBytes);
536 actualBytes = fread(temp, sizeof(char), desiredBytes, handle->stream);
538 actualBytes = fread(ptr, sizeof(char), desiredBytes, handle->stream);
540 size_t actualFrames = actualBytes / handle->bytesPerFrame;
541 handle->remaining -= actualFrames;
543 case SF_FORMAT_PCM_U8:
546 memcpy_to_i32_from_u8(ptr, (const unsigned char *) temp,
547 actualFrames * handle->info.channels);
551 case SF_FORMAT_PCM_16:
552 memcpy_to_i32_from_i16(ptr, (const short *) temp, actualFrames * handle->info.channels);
555 case SF_FORMAT_PCM_32:
557 case SF_FORMAT_FLOAT:
558 memcpy_to_i32_from_float(ptr, (const float *) ptr, actualFrames * handle->info.channels);
560 case SF_FORMAT_PCM_24:
561 memcpy_to_i32_from_p24(ptr, (const uint8_t *) temp, actualFrames * handle->info.channels);
565 memset(ptr, 0, actualFrames * handle->info.channels * sizeof(int));
571 sf_count_t sf_writef_short(SNDFILE *handle, const short *ptr, sf_count_t desiredFrames)
573 if (handle == NULL || handle->mode != SFM_WRITE || ptr == NULL || desiredFrames <= 0)
575 size_t desiredBytes = desiredFrames * handle->bytesPerFrame;
576 size_t actualBytes = 0;
577 switch (handle->info.format & SF_FORMAT_SUBMASK) {
578 case SF_FORMAT_PCM_U8:
579 handle->temp = realloc(handle->temp, desiredBytes);
580 memcpy_to_u8_from_i16(handle->temp, ptr, desiredBytes);
581 actualBytes = fwrite(handle->temp, sizeof(char), desiredBytes, handle->stream);
583 case SF_FORMAT_PCM_16:
584 // does not check for numeric overflow
585 if (isLittleEndian()) {
586 actualBytes = fwrite(ptr, sizeof(char), desiredBytes, handle->stream);
588 handle->temp = realloc(handle->temp, desiredBytes);
589 memcpy(handle->temp, ptr, desiredBytes);
590 my_swab((short *) handle->temp, desiredFrames * handle->info.channels);
591 actualBytes = fwrite(handle->temp, sizeof(char), desiredBytes, handle->stream);
594 case SF_FORMAT_FLOAT:
595 handle->temp = realloc(handle->temp, desiredBytes);
596 memcpy_to_float_from_i16((float *) handle->temp, ptr,
597 desiredFrames * handle->info.channels);
598 actualBytes = fwrite(handle->temp, sizeof(char), desiredBytes, handle->stream);
603 size_t actualFrames = actualBytes / handle->bytesPerFrame;
604 handle->remaining += actualFrames;
608 sf_count_t sf_writef_float(SNDFILE *handle, const float *ptr, sf_count_t desiredFrames)
610 if (handle == NULL || handle->mode != SFM_WRITE || ptr == NULL || desiredFrames <= 0)
612 size_t desiredBytes = desiredFrames * handle->bytesPerFrame;
613 size_t actualBytes = 0;
614 switch (handle->info.format & SF_FORMAT_SUBMASK) {
615 case SF_FORMAT_FLOAT:
616 actualBytes = fwrite(ptr, sizeof(char), desiredBytes, handle->stream);
618 case SF_FORMAT_PCM_16:
619 handle->temp = realloc(handle->temp, desiredBytes);
620 memcpy_to_i16_from_float((short *) handle->temp, ptr,
621 desiredFrames * handle->info.channels);
622 actualBytes = fwrite(handle->temp, sizeof(char), desiredBytes, handle->stream);
624 case SF_FORMAT_PCM_U8: // transcoding from float to byte not yet implemented
628 size_t actualFrames = actualBytes / handle->bytesPerFrame;
629 handle->remaining += actualFrames;
633 sf_count_t sf_writef_int(SNDFILE *handle, const int *ptr, sf_count_t desiredFrames)
635 if (handle == NULL || handle->mode != SFM_WRITE || ptr == NULL || desiredFrames <= 0)
637 size_t desiredBytes = desiredFrames * handle->bytesPerFrame;
638 size_t actualBytes = 0;
639 switch (handle->info.format & SF_FORMAT_SUBMASK) {
640 case SF_FORMAT_PCM_32:
641 actualBytes = fwrite(ptr, sizeof(char), desiredBytes, handle->stream);
643 default: // transcoding from other formats not yet implemented
646 size_t actualFrames = actualBytes / handle->bytesPerFrame;
647 handle->remaining += actualFrames;