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>
23 #define WAVE_FORMAT_PCM 1
24 #define WAVE_FORMAT_IEEE_FLOAT 3
25 #define WAVE_FORMAT_EXTENSIBLE 0xFFFE
29 uint8_t *temp; // realloc buffer used for shrinking 16 bits to 8 bits and byte-swapping
32 size_t remaining; // frames unread for SFM_READ, frames written for SFM_WRITE
36 static unsigned little2u(unsigned char *ptr)
38 return (ptr[1] << 8) + ptr[0];
41 static unsigned little4u(unsigned char *ptr)
43 return (ptr[3] << 24) + (ptr[2] << 16) + (ptr[1] << 8) + ptr[0];
46 static int isLittleEndian(void)
48 static const short one = 1;
49 return *((const char *) &one) == 1;
52 // "swab" conflicts with OS X <string.h>
53 static void my_swab(short *ptr, size_t numToSwap)
55 while (numToSwap > 0) {
56 *ptr = little2u((unsigned char *) ptr);
62 static SNDFILE *sf_open_read(const char *path, SF_INFO *info)
64 FILE *stream = fopen(path, "rb");
66 fprintf(stderr, "fopen %s failed errno %d\n", path, errno);
70 SNDFILE *handle = (SNDFILE *) malloc(sizeof(SNDFILE));
71 handle->mode = SFM_READ;
73 handle->stream = stream;
74 handle->info.format = SF_FORMAT_WAV;
76 // don't attempt to parse all valid forms, just the most common ones
77 unsigned char wav[12];
79 actual = fread(wav, sizeof(char), sizeof(wav), stream);
81 fprintf(stderr, "actual %zu < 44\n", actual);
84 if (memcmp(wav, "RIFF", 4)) {
85 fprintf(stderr, "wav != RIFF\n");
88 unsigned riffSize = little4u(&wav[4]);
90 fprintf(stderr, "riffSize %u < 4\n", riffSize);
93 if (memcmp(&wav[8], "WAVE", 4)) {
94 fprintf(stderr, "missing WAVE\n");
97 size_t remaining = riffSize - 4;
100 while (remaining >= 8) {
101 unsigned char chunk[8];
102 actual = fread(chunk, sizeof(char), sizeof(chunk), stream);
103 if (actual != sizeof(chunk)) {
104 fprintf(stderr, "actual %zu != %zu\n", actual, sizeof(chunk));
108 unsigned chunkSize = little4u(&chunk[4]);
109 if (chunkSize > remaining) {
110 fprintf(stderr, "chunkSize %u > remaining %zu\n", chunkSize, remaining);
113 if (!memcmp(&chunk[0], "fmt ", 4)) {
115 fprintf(stderr, "multiple fmt\n");
119 fprintf(stderr, "chunkSize %u < 2\n", chunkSize);
122 unsigned char fmt[40];
123 actual = fread(fmt, sizeof(char), 2, stream);
125 fprintf(stderr, "actual %zu != 2\n", actual);
128 unsigned format = little2u(&fmt[0]);
131 case WAVE_FORMAT_PCM:
132 case WAVE_FORMAT_IEEE_FLOAT:
135 case WAVE_FORMAT_EXTENSIBLE:
139 fprintf(stderr, "unsupported format %u\n", format);
142 if (chunkSize < minSize) {
143 fprintf(stderr, "chunkSize %u < minSize %zu\n", chunkSize, minSize);
146 actual = fread(&fmt[2], sizeof(char), minSize - 2, stream);
147 if (actual != minSize - 2) {
148 fprintf(stderr, "actual %zu != %zu\n", actual, minSize - 16);
151 if (chunkSize > minSize) {
152 fseek(stream, (long) (chunkSize - minSize), SEEK_CUR);
154 unsigned channels = little2u(&fmt[2]);
155 if (channels != 1 && channels != 2) {
156 fprintf(stderr, "channels %u != 1 or 2\n", channels);
159 unsigned samplerate = little4u(&fmt[4]);
160 if (samplerate == 0) {
161 fprintf(stderr, "samplerate %u == 0\n", samplerate);
165 // ignore block alignment
166 unsigned bitsPerSample = little2u(&fmt[14]);
167 if (bitsPerSample != 8 && bitsPerSample != 16 && bitsPerSample != 32) {
168 fprintf(stderr, "bitsPerSample %u != 8 or 16 or 32\n", bitsPerSample);
171 unsigned bytesPerFrame = (bitsPerSample >> 3) * channels;
172 handle->bytesPerFrame = bytesPerFrame;
173 handle->info.samplerate = samplerate;
174 handle->info.channels = channels;
175 switch (bitsPerSample) {
177 handle->info.format |= SF_FORMAT_PCM_U8;
180 handle->info.format |= SF_FORMAT_PCM_16;
183 if (format == WAVE_FORMAT_IEEE_FLOAT)
184 handle->info.format |= SF_FORMAT_FLOAT;
186 handle->info.format |= SF_FORMAT_PCM_32;
190 } else if (!memcmp(&chunk[0], "data", 4)) {
192 fprintf(stderr, "data not preceded by fmt\n");
196 fprintf(stderr, "multiple data\n");
199 handle->remaining = chunkSize / handle->bytesPerFrame;
200 handle->info.frames = handle->remaining;
202 } else if (!memcmp(&chunk[0], "fact", 4)) {
205 fseek(stream, (long) chunkSize, SEEK_CUR);
208 // ignore unknown chunk
209 fprintf(stderr, "ignoring unknown chunk %c%c%c%c\n",
210 chunk[0], chunk[1], chunk[2], chunk[3]);
212 fseek(stream, (long) chunkSize, SEEK_CUR);
215 remaining -= chunkSize;
218 fprintf(stderr, "partial chunk at end of RIFF, remaining %zu\n", remaining);
222 fprintf(stderr, "missing data\n");
225 *info = handle->info;
234 static void write4u(unsigned char *ptr, unsigned u)
242 static SNDFILE *sf_open_write(const char *path, SF_INFO *info)
244 int sub = info->format & SF_FORMAT_SUBMASK;
246 (info->samplerate > 0) &&
247 (info->channels > 0 && info->channels <= 8) &&
248 ((info->format & SF_FORMAT_TYPEMASK) == SF_FORMAT_WAV) &&
249 (sub == SF_FORMAT_PCM_16 || sub == SF_FORMAT_PCM_U8 || sub == SF_FORMAT_FLOAT)
253 FILE *stream = fopen(path, "w+b");
254 unsigned char wav[58];
255 memset(wav, 0, sizeof(wav));
256 memcpy(wav, "RIFF", 4);
257 memcpy(&wav[8], "WAVEfmt ", 8);
258 if (sub == SF_FORMAT_FLOAT) {
259 wav[4] = 50; // riffSize
260 wav[16] = 18; // fmtSize
261 wav[20] = WAVE_FORMAT_IEEE_FLOAT;
263 wav[4] = 36; // riffSize
264 wav[16] = 16; // fmtSize
265 wav[20] = WAVE_FORMAT_PCM;
267 wav[22] = info->channels;
268 write4u(&wav[24], info->samplerate);
269 unsigned bitsPerSample;
271 case SF_FORMAT_PCM_16:
274 case SF_FORMAT_PCM_U8:
277 case SF_FORMAT_FLOAT:
280 default: // not reachable
284 unsigned blockAlignment = (bitsPerSample >> 3) * info->channels;
285 unsigned byteRate = info->samplerate * blockAlignment;
286 write4u(&wav[28], byteRate);
287 wav[32] = blockAlignment;
288 wav[34] = bitsPerSample;
289 if (sub == SF_FORMAT_FLOAT) {
290 memcpy(&wav[38], "fact", 4);
292 memcpy(&wav[50], "data", 4);
294 memcpy(&wav[36], "data", 4);
295 // dataSize is initially zero
296 (void) fwrite(wav, sizeof(wav), 1, stream);
297 SNDFILE *handle = (SNDFILE *) malloc(sizeof(SNDFILE));
298 handle->mode = SFM_WRITE;
300 handle->stream = stream;
301 handle->bytesPerFrame = blockAlignment;
302 handle->remaining = 0;
303 handle->info = *info;
307 SNDFILE *sf_open(const char *path, int mode, SF_INFO *info)
309 if (path == NULL || info == NULL) {
310 fprintf(stderr, "path=%p info=%p\n", path, info);
315 return sf_open_read(path, info);
317 return sf_open_write(path, info);
319 fprintf(stderr, "mode=%d\n", mode);
324 void sf_close(SNDFILE *handle)
329 if (handle->mode == SFM_WRITE) {
330 (void) fflush(handle->stream);
331 rewind(handle->stream);
332 unsigned char wav[58];
333 size_t extra = (handle->info.format & SF_FORMAT_SUBMASK) == SF_FORMAT_FLOAT ? 14 : 0;
334 (void) fread(wav, 44 + extra, 1, handle->stream);
335 unsigned dataSize = handle->remaining * handle->bytesPerFrame;
336 write4u(&wav[4], dataSize + 36 + extra); // riffSize
337 write4u(&wav[40 + extra], dataSize); // dataSize
338 rewind(handle->stream);
339 (void) fwrite(wav, 44 + extra, 1, handle->stream);
341 (void) fclose(handle->stream);
345 sf_count_t sf_readf_short(SNDFILE *handle, short *ptr, sf_count_t desiredFrames)
347 if (handle == NULL || handle->mode != SFM_READ || ptr == NULL || !handle->remaining ||
348 desiredFrames <= 0) {
351 if (handle->remaining < (size_t) desiredFrames) {
352 desiredFrames = handle->remaining;
354 // does not check for numeric overflow
355 size_t desiredBytes = desiredFrames * handle->bytesPerFrame;
358 unsigned format = handle->info.format & SF_FORMAT_SUBMASK;
359 if (format == SF_FORMAT_PCM_32 || format == SF_FORMAT_FLOAT) {
360 temp = malloc(desiredBytes);
361 actualBytes = fread(temp, sizeof(char), desiredBytes, handle->stream);
363 actualBytes = fread(ptr, sizeof(char), desiredBytes, handle->stream);
365 size_t actualFrames = actualBytes / handle->bytesPerFrame;
366 handle->remaining -= actualFrames;
368 case SF_FORMAT_PCM_U8:
369 memcpy_to_i16_from_u8(ptr, (unsigned char *) ptr, actualFrames * handle->info.channels);
371 case SF_FORMAT_PCM_16:
372 if (!isLittleEndian())
373 my_swab(ptr, actualFrames * handle->info.channels);
375 case SF_FORMAT_PCM_32:
376 memcpy_to_i16_from_i32(ptr, (const int *) temp, actualFrames * handle->info.channels);
379 case SF_FORMAT_FLOAT:
380 memcpy_to_i16_from_float(ptr, (const float *) temp, actualFrames * handle->info.channels);
384 memset(ptr, 0, actualFrames * handle->info.channels * sizeof(short));
390 sf_count_t sf_readf_float(SNDFILE *handle, float *ptr, sf_count_t desiredFrames)
392 if (handle == NULL || handle->mode != SFM_READ || ptr == NULL || !handle->remaining ||
393 desiredFrames <= 0) {
396 if (handle->remaining < (size_t) desiredFrames) {
397 desiredFrames = handle->remaining;
399 // does not check for numeric overflow
400 size_t desiredBytes = desiredFrames * handle->bytesPerFrame;
402 unsigned format = handle->info.format & SF_FORMAT_SUBMASK;
403 actualBytes = fread(ptr, sizeof(char), desiredBytes, handle->stream);
404 size_t actualFrames = actualBytes / handle->bytesPerFrame;
405 handle->remaining -= actualFrames;
408 case SF_FORMAT_PCM_U8:
409 memcpy_to_float_from_u8(ptr, (const unsigned char *) ptr,
410 actualFrames * handle->info.channels);
413 case SF_FORMAT_PCM_16:
414 memcpy_to_float_from_i16(ptr, (const short *) ptr, actualFrames * handle->info.channels);
417 case SF_FORMAT_PCM_32:
418 memcpy_to_float_from_i32(ptr, (const int *) ptr, actualFrames * handle->info.channels);
421 case SF_FORMAT_FLOAT:
424 memset(ptr, 0, actualFrames * handle->info.channels * sizeof(float));
430 sf_count_t sf_writef_short(SNDFILE *handle, const short *ptr, sf_count_t desiredFrames)
432 if (handle == NULL || handle->mode != SFM_WRITE || ptr == NULL || desiredFrames <= 0)
434 size_t desiredBytes = desiredFrames * handle->bytesPerFrame;
435 size_t actualBytes = 0;
436 switch (handle->info.format & SF_FORMAT_SUBMASK) {
437 case SF_FORMAT_PCM_U8:
438 handle->temp = realloc(handle->temp, desiredBytes);
439 memcpy_to_u8_from_i16(handle->temp, ptr, desiredBytes);
440 actualBytes = fwrite(handle->temp, sizeof(char), desiredBytes, handle->stream);
442 case SF_FORMAT_PCM_16:
443 // does not check for numeric overflow
444 if (isLittleEndian()) {
445 actualBytes = fwrite(ptr, sizeof(char), desiredBytes, handle->stream);
447 handle->temp = realloc(handle->temp, desiredBytes);
448 memcpy(handle->temp, ptr, desiredBytes);
449 my_swab((short *) handle->temp, desiredFrames * handle->info.channels);
450 actualBytes = fwrite(handle->temp, sizeof(char), desiredBytes, handle->stream);
453 case SF_FORMAT_FLOAT: // transcoding from short to float not yet implemented
457 size_t actualFrames = actualBytes / handle->bytesPerFrame;
458 handle->remaining += actualFrames;
462 sf_count_t sf_writef_float(SNDFILE *handle, const float *ptr, sf_count_t desiredFrames)
464 if (handle == NULL || handle->mode != SFM_WRITE || ptr == NULL || desiredFrames <= 0)
466 size_t desiredBytes = desiredFrames * handle->bytesPerFrame;
467 size_t actualBytes = 0;
468 switch (handle->info.format & SF_FORMAT_SUBMASK) {
469 case SF_FORMAT_FLOAT:
470 actualBytes = fwrite(ptr, sizeof(char), desiredBytes, handle->stream);
472 case SF_FORMAT_PCM_U8: // transcoding from float to byte/short not yet implemented
473 case SF_FORMAT_PCM_16:
477 size_t actualFrames = actualBytes / handle->bytesPerFrame;
478 handle->remaining += actualFrames;