Glenn Kasten | 207ec29 | 2012-10-30 16:09:18 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 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 |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 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. |
| 15 | */ |
| 16 | |
| 17 | #include <audio_utils/sndfile.h> |
Glenn Kasten | 36c248b | 2012-11-12 14:42:31 -0800 | [diff] [blame] | 18 | #include <audio_utils/primitives.h> |
Glenn Kasten | 2cee4e4 | 2015-04-01 11:09:55 -0700 | [diff] [blame^] | 19 | #ifdef HAVE_STDERR |
Glenn Kasten | 207ec29 | 2012-10-30 16:09:18 -0700 | [diff] [blame] | 20 | #include <stdio.h> |
Glenn Kasten | 2cee4e4 | 2015-04-01 11:09:55 -0700 | [diff] [blame^] | 21 | #endif |
Glenn Kasten | 207ec29 | 2012-10-30 16:09:18 -0700 | [diff] [blame] | 22 | #include <string.h> |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 23 | #include <errno.h> |
| 24 | |
| 25 | #define WAVE_FORMAT_PCM 1 |
| 26 | #define WAVE_FORMAT_IEEE_FLOAT 3 |
| 27 | #define WAVE_FORMAT_EXTENSIBLE 0xFFFE |
Glenn Kasten | 207ec29 | 2012-10-30 16:09:18 -0700 | [diff] [blame] | 28 | |
| 29 | struct SNDFILE_ { |
Glenn Kasten | eae13dd | 2013-01-04 11:24:55 -0800 | [diff] [blame] | 30 | int mode; |
| 31 | uint8_t *temp; // realloc buffer used for shrinking 16 bits to 8 bits and byte-swapping |
Glenn Kasten | 207ec29 | 2012-10-30 16:09:18 -0700 | [diff] [blame] | 32 | FILE *stream; |
| 33 | size_t bytesPerFrame; |
Glenn Kasten | eae13dd | 2013-01-04 11:24:55 -0800 | [diff] [blame] | 34 | size_t remaining; // frames unread for SFM_READ, frames written for SFM_WRITE |
Glenn Kasten | 207ec29 | 2012-10-30 16:09:18 -0700 | [diff] [blame] | 35 | SF_INFO info; |
| 36 | }; |
| 37 | |
| 38 | static unsigned little2u(unsigned char *ptr) |
| 39 | { |
| 40 | return (ptr[1] << 8) + ptr[0]; |
| 41 | } |
| 42 | |
| 43 | static unsigned little4u(unsigned char *ptr) |
| 44 | { |
| 45 | return (ptr[3] << 24) + (ptr[2] << 16) + (ptr[1] << 8) + ptr[0]; |
| 46 | } |
| 47 | |
| 48 | static int isLittleEndian(void) |
| 49 | { |
| 50 | static const short one = 1; |
| 51 | return *((const char *) &one) == 1; |
| 52 | } |
| 53 | |
Glenn Kasten | 8fdafc9 | 2013-02-12 16:23:42 -0800 | [diff] [blame] | 54 | // "swab" conflicts with OS X <string.h> |
| 55 | static void my_swab(short *ptr, size_t numToSwap) |
Glenn Kasten | 207ec29 | 2012-10-30 16:09:18 -0700 | [diff] [blame] | 56 | { |
| 57 | while (numToSwap > 0) { |
| 58 | *ptr = little2u((unsigned char *) ptr); |
| 59 | --numToSwap; |
| 60 | ++ptr; |
| 61 | } |
| 62 | } |
| 63 | |
Glenn Kasten | eae13dd | 2013-01-04 11:24:55 -0800 | [diff] [blame] | 64 | static SNDFILE *sf_open_read(const char *path, SF_INFO *info) |
Glenn Kasten | 207ec29 | 2012-10-30 16:09:18 -0700 | [diff] [blame] | 65 | { |
Glenn Kasten | 207ec29 | 2012-10-30 16:09:18 -0700 | [diff] [blame] | 66 | FILE *stream = fopen(path, "rb"); |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 67 | if (stream == NULL) { |
Glenn Kasten | 2cee4e4 | 2015-04-01 11:09:55 -0700 | [diff] [blame^] | 68 | #ifdef HAVE_STDERR |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 69 | fprintf(stderr, "fopen %s failed errno %d\n", path, errno); |
Glenn Kasten | 2cee4e4 | 2015-04-01 11:09:55 -0700 | [diff] [blame^] | 70 | #endif |
Glenn Kasten | 207ec29 | 2012-10-30 16:09:18 -0700 | [diff] [blame] | 71 | return NULL; |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | SNDFILE *handle = (SNDFILE *) malloc(sizeof(SNDFILE)); |
| 75 | handle->mode = SFM_READ; |
| 76 | handle->temp = NULL; |
| 77 | handle->stream = stream; |
| 78 | handle->info.format = SF_FORMAT_WAV; |
| 79 | |
| 80 | // don't attempt to parse all valid forms, just the most common ones |
| 81 | unsigned char wav[12]; |
Glenn Kasten | 207ec29 | 2012-10-30 16:09:18 -0700 | [diff] [blame] | 82 | size_t actual; |
| 83 | actual = fread(wav, sizeof(char), sizeof(wav), stream); |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 84 | if (actual < 12) { |
Glenn Kasten | 2cee4e4 | 2015-04-01 11:09:55 -0700 | [diff] [blame^] | 85 | #ifdef HAVE_STDERR |
Kévin PETIT | 96d859c | 2014-03-26 11:56:32 +0000 | [diff] [blame] | 86 | fprintf(stderr, "actual %zu < 44\n", actual); |
Glenn Kasten | 2cee4e4 | 2015-04-01 11:09:55 -0700 | [diff] [blame^] | 87 | #endif |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 88 | goto close; |
Glenn Kasten | 207ec29 | 2012-10-30 16:09:18 -0700 | [diff] [blame] | 89 | } |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 90 | if (memcmp(wav, "RIFF", 4)) { |
Glenn Kasten | 2cee4e4 | 2015-04-01 11:09:55 -0700 | [diff] [blame^] | 91 | #ifdef HAVE_STDERR |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 92 | fprintf(stderr, "wav != RIFF\n"); |
Glenn Kasten | 2cee4e4 | 2015-04-01 11:09:55 -0700 | [diff] [blame^] | 93 | #endif |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 94 | goto close; |
| 95 | } |
| 96 | unsigned riffSize = little4u(&wav[4]); |
| 97 | if (riffSize < 4) { |
Glenn Kasten | 2cee4e4 | 2015-04-01 11:09:55 -0700 | [diff] [blame^] | 98 | #ifdef HAVE_STDERR |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 99 | fprintf(stderr, "riffSize %u < 4\n", riffSize); |
Glenn Kasten | 2cee4e4 | 2015-04-01 11:09:55 -0700 | [diff] [blame^] | 100 | #endif |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 101 | goto close; |
| 102 | } |
| 103 | if (memcmp(&wav[8], "WAVE", 4)) { |
Glenn Kasten | 2cee4e4 | 2015-04-01 11:09:55 -0700 | [diff] [blame^] | 104 | #ifdef HAVE_STDERR |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 105 | fprintf(stderr, "missing WAVE\n"); |
Glenn Kasten | 2cee4e4 | 2015-04-01 11:09:55 -0700 | [diff] [blame^] | 106 | #endif |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 107 | goto close; |
| 108 | } |
| 109 | size_t remaining = riffSize - 4; |
| 110 | int hadFmt = 0; |
| 111 | int hadData = 0; |
Glenn Kasten | 0c3baaa | 2014-09-29 11:22:26 -0700 | [diff] [blame] | 112 | long dataTell = 0L; |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 113 | while (remaining >= 8) { |
| 114 | unsigned char chunk[8]; |
| 115 | actual = fread(chunk, sizeof(char), sizeof(chunk), stream); |
| 116 | if (actual != sizeof(chunk)) { |
Glenn Kasten | 2cee4e4 | 2015-04-01 11:09:55 -0700 | [diff] [blame^] | 117 | #ifdef HAVE_STDERR |
Kévin PETIT | 96d859c | 2014-03-26 11:56:32 +0000 | [diff] [blame] | 118 | fprintf(stderr, "actual %zu != %zu\n", actual, sizeof(chunk)); |
Glenn Kasten | 2cee4e4 | 2015-04-01 11:09:55 -0700 | [diff] [blame^] | 119 | #endif |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 120 | goto close; |
| 121 | } |
| 122 | remaining -= 8; |
| 123 | unsigned chunkSize = little4u(&chunk[4]); |
| 124 | if (chunkSize > remaining) { |
Glenn Kasten | 2cee4e4 | 2015-04-01 11:09:55 -0700 | [diff] [blame^] | 125 | #ifdef HAVE_STDERR |
Kévin PETIT | 96d859c | 2014-03-26 11:56:32 +0000 | [diff] [blame] | 126 | fprintf(stderr, "chunkSize %u > remaining %zu\n", chunkSize, remaining); |
Glenn Kasten | 2cee4e4 | 2015-04-01 11:09:55 -0700 | [diff] [blame^] | 127 | #endif |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 128 | goto close; |
| 129 | } |
| 130 | if (!memcmp(&chunk[0], "fmt ", 4)) { |
| 131 | if (hadFmt) { |
Glenn Kasten | 2cee4e4 | 2015-04-01 11:09:55 -0700 | [diff] [blame^] | 132 | #ifdef HAVE_STDERR |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 133 | fprintf(stderr, "multiple fmt\n"); |
Glenn Kasten | 2cee4e4 | 2015-04-01 11:09:55 -0700 | [diff] [blame^] | 134 | #endif |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 135 | goto close; |
| 136 | } |
| 137 | if (chunkSize < 2) { |
Glenn Kasten | 2cee4e4 | 2015-04-01 11:09:55 -0700 | [diff] [blame^] | 138 | #ifdef HAVE_STDERR |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 139 | fprintf(stderr, "chunkSize %u < 2\n", chunkSize); |
Glenn Kasten | 2cee4e4 | 2015-04-01 11:09:55 -0700 | [diff] [blame^] | 140 | #endif |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 141 | goto close; |
| 142 | } |
| 143 | unsigned char fmt[40]; |
| 144 | actual = fread(fmt, sizeof(char), 2, stream); |
| 145 | if (actual != 2) { |
Glenn Kasten | 2cee4e4 | 2015-04-01 11:09:55 -0700 | [diff] [blame^] | 146 | #ifdef HAVE_STDERR |
Kévin PETIT | 96d859c | 2014-03-26 11:56:32 +0000 | [diff] [blame] | 147 | fprintf(stderr, "actual %zu != 2\n", actual); |
Glenn Kasten | 2cee4e4 | 2015-04-01 11:09:55 -0700 | [diff] [blame^] | 148 | #endif |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 149 | goto close; |
| 150 | } |
| 151 | unsigned format = little2u(&fmt[0]); |
| 152 | size_t minSize = 0; |
| 153 | switch (format) { |
| 154 | case WAVE_FORMAT_PCM: |
| 155 | case WAVE_FORMAT_IEEE_FLOAT: |
| 156 | minSize = 16; |
| 157 | break; |
| 158 | case WAVE_FORMAT_EXTENSIBLE: |
| 159 | minSize = 40; |
| 160 | break; |
| 161 | default: |
Glenn Kasten | 2cee4e4 | 2015-04-01 11:09:55 -0700 | [diff] [blame^] | 162 | #ifdef HAVE_STDERR |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 163 | fprintf(stderr, "unsupported format %u\n", format); |
Glenn Kasten | 2cee4e4 | 2015-04-01 11:09:55 -0700 | [diff] [blame^] | 164 | #endif |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 165 | goto close; |
| 166 | } |
| 167 | if (chunkSize < minSize) { |
Glenn Kasten | 2cee4e4 | 2015-04-01 11:09:55 -0700 | [diff] [blame^] | 168 | #ifdef HAVE_STDERR |
Kévin PETIT | 96d859c | 2014-03-26 11:56:32 +0000 | [diff] [blame] | 169 | fprintf(stderr, "chunkSize %u < minSize %zu\n", chunkSize, minSize); |
Glenn Kasten | 2cee4e4 | 2015-04-01 11:09:55 -0700 | [diff] [blame^] | 170 | #endif |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 171 | goto close; |
| 172 | } |
| 173 | actual = fread(&fmt[2], sizeof(char), minSize - 2, stream); |
| 174 | if (actual != minSize - 2) { |
Glenn Kasten | 2cee4e4 | 2015-04-01 11:09:55 -0700 | [diff] [blame^] | 175 | #ifdef HAVE_STDERR |
Kévin PETIT | 96d859c | 2014-03-26 11:56:32 +0000 | [diff] [blame] | 176 | fprintf(stderr, "actual %zu != %zu\n", actual, minSize - 16); |
Glenn Kasten | 2cee4e4 | 2015-04-01 11:09:55 -0700 | [diff] [blame^] | 177 | #endif |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 178 | goto close; |
| 179 | } |
| 180 | if (chunkSize > minSize) { |
| 181 | fseek(stream, (long) (chunkSize - minSize), SEEK_CUR); |
| 182 | } |
| 183 | unsigned channels = little2u(&fmt[2]); |
Glenn Kasten | 7c1930a | 2014-05-07 11:20:55 -0700 | [diff] [blame] | 184 | if (channels != 1 && channels != 2 && channels != 4 && channels != 6 && channels != 8) { |
Glenn Kasten | 2cee4e4 | 2015-04-01 11:09:55 -0700 | [diff] [blame^] | 185 | #ifdef HAVE_STDERR |
Glenn Kasten | 7c1930a | 2014-05-07 11:20:55 -0700 | [diff] [blame] | 186 | fprintf(stderr, "unsupported channels %u\n", channels); |
Glenn Kasten | 2cee4e4 | 2015-04-01 11:09:55 -0700 | [diff] [blame^] | 187 | #endif |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 188 | goto close; |
| 189 | } |
| 190 | unsigned samplerate = little4u(&fmt[4]); |
| 191 | if (samplerate == 0) { |
Glenn Kasten | 2cee4e4 | 2015-04-01 11:09:55 -0700 | [diff] [blame^] | 192 | #ifdef HAVE_STDERR |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 193 | fprintf(stderr, "samplerate %u == 0\n", samplerate); |
Glenn Kasten | 2cee4e4 | 2015-04-01 11:09:55 -0700 | [diff] [blame^] | 194 | #endif |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 195 | goto close; |
| 196 | } |
| 197 | // ignore byte rate |
| 198 | // ignore block alignment |
| 199 | unsigned bitsPerSample = little2u(&fmt[14]); |
| 200 | if (bitsPerSample != 8 && bitsPerSample != 16 && bitsPerSample != 32) { |
Glenn Kasten | 2cee4e4 | 2015-04-01 11:09:55 -0700 | [diff] [blame^] | 201 | #ifdef HAVE_STDERR |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 202 | fprintf(stderr, "bitsPerSample %u != 8 or 16 or 32\n", bitsPerSample); |
Glenn Kasten | 2cee4e4 | 2015-04-01 11:09:55 -0700 | [diff] [blame^] | 203 | #endif |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 204 | goto close; |
| 205 | } |
| 206 | unsigned bytesPerFrame = (bitsPerSample >> 3) * channels; |
| 207 | handle->bytesPerFrame = bytesPerFrame; |
| 208 | handle->info.samplerate = samplerate; |
| 209 | handle->info.channels = channels; |
| 210 | switch (bitsPerSample) { |
| 211 | case 8: |
| 212 | handle->info.format |= SF_FORMAT_PCM_U8; |
| 213 | break; |
| 214 | case 16: |
| 215 | handle->info.format |= SF_FORMAT_PCM_16; |
| 216 | break; |
| 217 | case 32: |
| 218 | if (format == WAVE_FORMAT_IEEE_FLOAT) |
| 219 | handle->info.format |= SF_FORMAT_FLOAT; |
| 220 | else |
| 221 | handle->info.format |= SF_FORMAT_PCM_32; |
| 222 | break; |
| 223 | } |
| 224 | hadFmt = 1; |
| 225 | } else if (!memcmp(&chunk[0], "data", 4)) { |
| 226 | if (!hadFmt) { |
Glenn Kasten | 2cee4e4 | 2015-04-01 11:09:55 -0700 | [diff] [blame^] | 227 | #ifdef HAVE_STDERR |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 228 | fprintf(stderr, "data not preceded by fmt\n"); |
Glenn Kasten | 2cee4e4 | 2015-04-01 11:09:55 -0700 | [diff] [blame^] | 229 | #endif |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 230 | goto close; |
| 231 | } |
| 232 | if (hadData) { |
Glenn Kasten | 2cee4e4 | 2015-04-01 11:09:55 -0700 | [diff] [blame^] | 233 | #ifdef HAVE_STDERR |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 234 | fprintf(stderr, "multiple data\n"); |
Glenn Kasten | 2cee4e4 | 2015-04-01 11:09:55 -0700 | [diff] [blame^] | 235 | #endif |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 236 | goto close; |
| 237 | } |
| 238 | handle->remaining = chunkSize / handle->bytesPerFrame; |
| 239 | handle->info.frames = handle->remaining; |
Glenn Kasten | 0c3baaa | 2014-09-29 11:22:26 -0700 | [diff] [blame] | 240 | dataTell = ftell(stream); |
| 241 | if (chunkSize > 0) { |
| 242 | fseek(stream, (long) chunkSize, SEEK_CUR); |
| 243 | } |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 244 | hadData = 1; |
| 245 | } else if (!memcmp(&chunk[0], "fact", 4)) { |
| 246 | // ignore fact |
| 247 | if (chunkSize > 0) { |
| 248 | fseek(stream, (long) chunkSize, SEEK_CUR); |
| 249 | } |
| 250 | } else { |
| 251 | // ignore unknown chunk |
Glenn Kasten | 2cee4e4 | 2015-04-01 11:09:55 -0700 | [diff] [blame^] | 252 | #ifdef HAVE_STDERR |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 253 | fprintf(stderr, "ignoring unknown chunk %c%c%c%c\n", |
| 254 | chunk[0], chunk[1], chunk[2], chunk[3]); |
Glenn Kasten | 2cee4e4 | 2015-04-01 11:09:55 -0700 | [diff] [blame^] | 255 | #endif |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 256 | if (chunkSize > 0) { |
| 257 | fseek(stream, (long) chunkSize, SEEK_CUR); |
| 258 | } |
| 259 | } |
| 260 | remaining -= chunkSize; |
| 261 | } |
| 262 | if (remaining > 0) { |
Glenn Kasten | 2cee4e4 | 2015-04-01 11:09:55 -0700 | [diff] [blame^] | 263 | #ifdef HAVE_STDERR |
Kévin PETIT | 96d859c | 2014-03-26 11:56:32 +0000 | [diff] [blame] | 264 | fprintf(stderr, "partial chunk at end of RIFF, remaining %zu\n", remaining); |
Glenn Kasten | 2cee4e4 | 2015-04-01 11:09:55 -0700 | [diff] [blame^] | 265 | #endif |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 266 | goto close; |
| 267 | } |
| 268 | if (!hadData) { |
Glenn Kasten | 2cee4e4 | 2015-04-01 11:09:55 -0700 | [diff] [blame^] | 269 | #ifdef HAVE_STDERR |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 270 | fprintf(stderr, "missing data\n"); |
Glenn Kasten | 2cee4e4 | 2015-04-01 11:09:55 -0700 | [diff] [blame^] | 271 | #endif |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 272 | goto close; |
| 273 | } |
Glenn Kasten | 0c3baaa | 2014-09-29 11:22:26 -0700 | [diff] [blame] | 274 | (void) fseek(stream, dataTell, SEEK_SET); |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 275 | *info = handle->info; |
| 276 | return handle; |
| 277 | |
| 278 | close: |
| 279 | free(handle); |
| 280 | fclose(stream); |
Glenn Kasten | 207ec29 | 2012-10-30 16:09:18 -0700 | [diff] [blame] | 281 | return NULL; |
| 282 | } |
| 283 | |
Glenn Kasten | eae13dd | 2013-01-04 11:24:55 -0800 | [diff] [blame] | 284 | static void write4u(unsigned char *ptr, unsigned u) |
| 285 | { |
| 286 | ptr[0] = u; |
| 287 | ptr[1] = u >> 8; |
| 288 | ptr[2] = u >> 16; |
| 289 | ptr[3] = u >> 24; |
| 290 | } |
| 291 | |
| 292 | static SNDFILE *sf_open_write(const char *path, SF_INFO *info) |
| 293 | { |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 294 | int sub = info->format & SF_FORMAT_SUBMASK; |
Glenn Kasten | eae13dd | 2013-01-04 11:24:55 -0800 | [diff] [blame] | 295 | if (!( |
| 296 | (info->samplerate > 0) && |
Andy Hung | 7095bc7 | 2014-04-09 18:52:06 -0700 | [diff] [blame] | 297 | (info->channels > 0 && info->channels <= 8) && |
Glenn Kasten | eae13dd | 2013-01-04 11:24:55 -0800 | [diff] [blame] | 298 | ((info->format & SF_FORMAT_TYPEMASK) == SF_FORMAT_WAV) && |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 299 | (sub == SF_FORMAT_PCM_16 || sub == SF_FORMAT_PCM_U8 || sub == SF_FORMAT_FLOAT) |
Glenn Kasten | eae13dd | 2013-01-04 11:24:55 -0800 | [diff] [blame] | 300 | )) { |
| 301 | return NULL; |
| 302 | } |
| 303 | FILE *stream = fopen(path, "w+b"); |
Andy Hung | 1fa816b | 2014-06-17 14:18:08 -0700 | [diff] [blame] | 304 | if (stream == NULL) { |
Glenn Kasten | 2cee4e4 | 2015-04-01 11:09:55 -0700 | [diff] [blame^] | 305 | #ifdef HAVE_STDERR |
Andy Hung | 1fa816b | 2014-06-17 14:18:08 -0700 | [diff] [blame] | 306 | fprintf(stderr, "fopen %s failed errno %d\n", path, errno); |
Glenn Kasten | 2cee4e4 | 2015-04-01 11:09:55 -0700 | [diff] [blame^] | 307 | #endif |
Andy Hung | 1fa816b | 2014-06-17 14:18:08 -0700 | [diff] [blame] | 308 | return NULL; |
| 309 | } |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 310 | unsigned char wav[58]; |
Glenn Kasten | eae13dd | 2013-01-04 11:24:55 -0800 | [diff] [blame] | 311 | memset(wav, 0, sizeof(wav)); |
| 312 | memcpy(wav, "RIFF", 4); |
Glenn Kasten | eae13dd | 2013-01-04 11:24:55 -0800 | [diff] [blame] | 313 | memcpy(&wav[8], "WAVEfmt ", 8); |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 314 | if (sub == SF_FORMAT_FLOAT) { |
| 315 | wav[4] = 50; // riffSize |
| 316 | wav[16] = 18; // fmtSize |
| 317 | wav[20] = WAVE_FORMAT_IEEE_FLOAT; |
| 318 | } else { |
| 319 | wav[4] = 36; // riffSize |
| 320 | wav[16] = 16; // fmtSize |
| 321 | wav[20] = WAVE_FORMAT_PCM; |
| 322 | } |
Glenn Kasten | eae13dd | 2013-01-04 11:24:55 -0800 | [diff] [blame] | 323 | wav[22] = info->channels; |
| 324 | write4u(&wav[24], info->samplerate); |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 325 | unsigned bitsPerSample; |
| 326 | switch (sub) { |
| 327 | case SF_FORMAT_PCM_16: |
| 328 | bitsPerSample = 16; |
| 329 | break; |
| 330 | case SF_FORMAT_PCM_U8: |
| 331 | bitsPerSample = 8; |
| 332 | break; |
| 333 | case SF_FORMAT_FLOAT: |
| 334 | bitsPerSample = 32; |
| 335 | break; |
| 336 | default: // not reachable |
| 337 | bitsPerSample = 0; |
| 338 | break; |
| 339 | } |
Glenn Kasten | eae13dd | 2013-01-04 11:24:55 -0800 | [diff] [blame] | 340 | unsigned blockAlignment = (bitsPerSample >> 3) * info->channels; |
| 341 | unsigned byteRate = info->samplerate * blockAlignment; |
| 342 | write4u(&wav[28], byteRate); |
| 343 | wav[32] = blockAlignment; |
| 344 | wav[34] = bitsPerSample; |
Andy Hung | c74f4b7 | 2014-06-03 19:15:18 -0700 | [diff] [blame] | 345 | size_t extra = 0; |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 346 | if (sub == SF_FORMAT_FLOAT) { |
| 347 | memcpy(&wav[38], "fact", 4); |
| 348 | wav[42] = 4; |
| 349 | memcpy(&wav[50], "data", 4); |
Andy Hung | c74f4b7 | 2014-06-03 19:15:18 -0700 | [diff] [blame] | 350 | extra = 14; |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 351 | } else |
| 352 | memcpy(&wav[36], "data", 4); |
Glenn Kasten | eae13dd | 2013-01-04 11:24:55 -0800 | [diff] [blame] | 353 | // dataSize is initially zero |
Andy Hung | c74f4b7 | 2014-06-03 19:15:18 -0700 | [diff] [blame] | 354 | (void) fwrite(wav, 44 + extra, 1, stream); |
Glenn Kasten | eae13dd | 2013-01-04 11:24:55 -0800 | [diff] [blame] | 355 | SNDFILE *handle = (SNDFILE *) malloc(sizeof(SNDFILE)); |
| 356 | handle->mode = SFM_WRITE; |
| 357 | handle->temp = NULL; |
| 358 | handle->stream = stream; |
| 359 | handle->bytesPerFrame = blockAlignment; |
| 360 | handle->remaining = 0; |
| 361 | handle->info = *info; |
| 362 | return handle; |
| 363 | } |
| 364 | |
| 365 | SNDFILE *sf_open(const char *path, int mode, SF_INFO *info) |
| 366 | { |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 367 | if (path == NULL || info == NULL) { |
Glenn Kasten | 2cee4e4 | 2015-04-01 11:09:55 -0700 | [diff] [blame^] | 368 | #ifdef HAVE_STDERR |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 369 | fprintf(stderr, "path=%p info=%p\n", path, info); |
Glenn Kasten | 2cee4e4 | 2015-04-01 11:09:55 -0700 | [diff] [blame^] | 370 | #endif |
Glenn Kasten | eae13dd | 2013-01-04 11:24:55 -0800 | [diff] [blame] | 371 | return NULL; |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 372 | } |
Glenn Kasten | eae13dd | 2013-01-04 11:24:55 -0800 | [diff] [blame] | 373 | switch (mode) { |
| 374 | case SFM_READ: |
| 375 | return sf_open_read(path, info); |
| 376 | case SFM_WRITE: |
| 377 | return sf_open_write(path, info); |
| 378 | default: |
Glenn Kasten | 2cee4e4 | 2015-04-01 11:09:55 -0700 | [diff] [blame^] | 379 | #ifdef HAVE_STDERR |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 380 | fprintf(stderr, "mode=%d\n", mode); |
Glenn Kasten | 2cee4e4 | 2015-04-01 11:09:55 -0700 | [diff] [blame^] | 381 | #endif |
Glenn Kasten | eae13dd | 2013-01-04 11:24:55 -0800 | [diff] [blame] | 382 | return NULL; |
| 383 | } |
| 384 | } |
| 385 | |
Glenn Kasten | 207ec29 | 2012-10-30 16:09:18 -0700 | [diff] [blame] | 386 | void sf_close(SNDFILE *handle) |
| 387 | { |
| 388 | if (handle == NULL) |
| 389 | return; |
Glenn Kasten | eae13dd | 2013-01-04 11:24:55 -0800 | [diff] [blame] | 390 | free(handle->temp); |
| 391 | if (handle->mode == SFM_WRITE) { |
| 392 | (void) fflush(handle->stream); |
| 393 | rewind(handle->stream); |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 394 | unsigned char wav[58]; |
| 395 | size_t extra = (handle->info.format & SF_FORMAT_SUBMASK) == SF_FORMAT_FLOAT ? 14 : 0; |
| 396 | (void) fread(wav, 44 + extra, 1, handle->stream); |
Glenn Kasten | eae13dd | 2013-01-04 11:24:55 -0800 | [diff] [blame] | 397 | unsigned dataSize = handle->remaining * handle->bytesPerFrame; |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 398 | write4u(&wav[4], dataSize + 36 + extra); // riffSize |
| 399 | write4u(&wav[40 + extra], dataSize); // dataSize |
Glenn Kasten | eae13dd | 2013-01-04 11:24:55 -0800 | [diff] [blame] | 400 | rewind(handle->stream); |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 401 | (void) fwrite(wav, 44 + extra, 1, handle->stream); |
Glenn Kasten | eae13dd | 2013-01-04 11:24:55 -0800 | [diff] [blame] | 402 | } |
Glenn Kasten | 207ec29 | 2012-10-30 16:09:18 -0700 | [diff] [blame] | 403 | (void) fclose(handle->stream); |
Glenn Kasten | eae13dd | 2013-01-04 11:24:55 -0800 | [diff] [blame] | 404 | free(handle); |
Glenn Kasten | 207ec29 | 2012-10-30 16:09:18 -0700 | [diff] [blame] | 405 | } |
| 406 | |
Glenn Kasten | 36c248b | 2012-11-12 14:42:31 -0800 | [diff] [blame] | 407 | sf_count_t sf_readf_short(SNDFILE *handle, short *ptr, sf_count_t desiredFrames) |
Glenn Kasten | 207ec29 | 2012-10-30 16:09:18 -0700 | [diff] [blame] | 408 | { |
Glenn Kasten | eae13dd | 2013-01-04 11:24:55 -0800 | [diff] [blame] | 409 | if (handle == NULL || handle->mode != SFM_READ || ptr == NULL || !handle->remaining || |
| 410 | desiredFrames <= 0) { |
Glenn Kasten | 207ec29 | 2012-10-30 16:09:18 -0700 | [diff] [blame] | 411 | return 0; |
Glenn Kasten | eae13dd | 2013-01-04 11:24:55 -0800 | [diff] [blame] | 412 | } |
Glenn Kasten | c89cb60 | 2014-03-11 09:03:35 -0700 | [diff] [blame] | 413 | if (handle->remaining < (size_t) desiredFrames) { |
Glenn Kasten | 207ec29 | 2012-10-30 16:09:18 -0700 | [diff] [blame] | 414 | desiredFrames = handle->remaining; |
Glenn Kasten | c89cb60 | 2014-03-11 09:03:35 -0700 | [diff] [blame] | 415 | } |
Glenn Kasten | 207ec29 | 2012-10-30 16:09:18 -0700 | [diff] [blame] | 416 | // does not check for numeric overflow |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 417 | size_t desiredBytes = desiredFrames * handle->bytesPerFrame; |
| 418 | size_t actualBytes; |
| 419 | void *temp = NULL; |
| 420 | unsigned format = handle->info.format & SF_FORMAT_SUBMASK; |
| 421 | if (format == SF_FORMAT_PCM_32 || format == SF_FORMAT_FLOAT) { |
| 422 | temp = malloc(desiredBytes); |
| 423 | actualBytes = fread(temp, sizeof(char), desiredBytes, handle->stream); |
| 424 | } else { |
| 425 | actualBytes = fread(ptr, sizeof(char), desiredBytes, handle->stream); |
| 426 | } |
Glenn Kasten | 207ec29 | 2012-10-30 16:09:18 -0700 | [diff] [blame] | 427 | size_t actualFrames = actualBytes / handle->bytesPerFrame; |
| 428 | handle->remaining -= actualFrames; |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 429 | switch (format) { |
Glenn Kasten | 36c248b | 2012-11-12 14:42:31 -0800 | [diff] [blame] | 430 | case SF_FORMAT_PCM_U8: |
| 431 | memcpy_to_i16_from_u8(ptr, (unsigned char *) ptr, actualFrames * handle->info.channels); |
| 432 | break; |
| 433 | case SF_FORMAT_PCM_16: |
| 434 | if (!isLittleEndian()) |
Glenn Kasten | 8fdafc9 | 2013-02-12 16:23:42 -0800 | [diff] [blame] | 435 | my_swab(ptr, actualFrames * handle->info.channels); |
Glenn Kasten | 36c248b | 2012-11-12 14:42:31 -0800 | [diff] [blame] | 436 | break; |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 437 | case SF_FORMAT_PCM_32: |
| 438 | memcpy_to_i16_from_i32(ptr, (const int *) temp, actualFrames * handle->info.channels); |
| 439 | free(temp); |
| 440 | break; |
| 441 | case SF_FORMAT_FLOAT: |
| 442 | memcpy_to_i16_from_float(ptr, (const float *) temp, actualFrames * handle->info.channels); |
| 443 | free(temp); |
| 444 | break; |
| 445 | default: |
| 446 | memset(ptr, 0, actualFrames * handle->info.channels * sizeof(short)); |
| 447 | break; |
Glenn Kasten | 36c248b | 2012-11-12 14:42:31 -0800 | [diff] [blame] | 448 | } |
Glenn Kasten | 207ec29 | 2012-10-30 16:09:18 -0700 | [diff] [blame] | 449 | return actualFrames; |
| 450 | } |
Glenn Kasten | eae13dd | 2013-01-04 11:24:55 -0800 | [diff] [blame] | 451 | |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 452 | sf_count_t sf_readf_float(SNDFILE *handle, float *ptr, sf_count_t desiredFrames) |
| 453 | { |
Glenn Kasten | c89cb60 | 2014-03-11 09:03:35 -0700 | [diff] [blame] | 454 | if (handle == NULL || handle->mode != SFM_READ || ptr == NULL || !handle->remaining || |
| 455 | desiredFrames <= 0) { |
| 456 | return 0; |
| 457 | } |
| 458 | if (handle->remaining < (size_t) desiredFrames) { |
| 459 | desiredFrames = handle->remaining; |
| 460 | } |
| 461 | // does not check for numeric overflow |
| 462 | size_t desiredBytes = desiredFrames * handle->bytesPerFrame; |
| 463 | size_t actualBytes; |
Andy Hung | 5237016 | 2014-05-22 18:17:53 -0700 | [diff] [blame] | 464 | void *temp = NULL; |
Glenn Kasten | c89cb60 | 2014-03-11 09:03:35 -0700 | [diff] [blame] | 465 | unsigned format = handle->info.format & SF_FORMAT_SUBMASK; |
Andy Hung | 5237016 | 2014-05-22 18:17:53 -0700 | [diff] [blame] | 466 | if (format == SF_FORMAT_PCM_16 || format == SF_FORMAT_PCM_U8) { |
| 467 | temp = malloc(desiredBytes); |
| 468 | actualBytes = fread(temp, sizeof(char), desiredBytes, handle->stream); |
| 469 | } else { |
| 470 | actualBytes = fread(ptr, sizeof(char), desiredBytes, handle->stream); |
| 471 | } |
Glenn Kasten | c89cb60 | 2014-03-11 09:03:35 -0700 | [diff] [blame] | 472 | size_t actualFrames = actualBytes / handle->bytesPerFrame; |
| 473 | handle->remaining -= actualFrames; |
| 474 | switch (format) { |
Glenn Kasten | c89cb60 | 2014-03-11 09:03:35 -0700 | [diff] [blame] | 475 | case SF_FORMAT_PCM_U8: |
Glenn Kasten | c89cb60 | 2014-03-11 09:03:35 -0700 | [diff] [blame] | 476 | #if 0 |
Andy Hung | 5237016 | 2014-05-22 18:17:53 -0700 | [diff] [blame] | 477 | // TODO - implement |
| 478 | memcpy_to_float_from_u8(ptr, (const unsigned char *) temp, |
| 479 | actualFrames * handle->info.channels); |
| 480 | #endif |
| 481 | free(temp); |
| 482 | break; |
| 483 | case SF_FORMAT_PCM_16: |
| 484 | memcpy_to_float_from_i16(ptr, (const short *) temp, actualFrames * handle->info.channels); |
| 485 | free(temp); |
| 486 | break; |
Glenn Kasten | c89cb60 | 2014-03-11 09:03:35 -0700 | [diff] [blame] | 487 | case SF_FORMAT_PCM_32: |
| 488 | memcpy_to_float_from_i32(ptr, (const int *) ptr, actualFrames * handle->info.channels); |
| 489 | break; |
Glenn Kasten | c89cb60 | 2014-03-11 09:03:35 -0700 | [diff] [blame] | 490 | case SF_FORMAT_FLOAT: |
| 491 | break; |
| 492 | default: |
| 493 | memset(ptr, 0, actualFrames * handle->info.channels * sizeof(float)); |
| 494 | break; |
| 495 | } |
| 496 | return actualFrames; |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 497 | } |
| 498 | |
Andy Hung | 7b93781 | 2014-05-22 18:26:07 -0700 | [diff] [blame] | 499 | sf_count_t sf_readf_int(SNDFILE *handle, int *ptr, sf_count_t desiredFrames) |
| 500 | { |
| 501 | if (handle == NULL || handle->mode != SFM_READ || ptr == NULL || !handle->remaining || |
| 502 | desiredFrames <= 0) { |
| 503 | return 0; |
| 504 | } |
| 505 | if (handle->remaining < (size_t) desiredFrames) { |
| 506 | desiredFrames = handle->remaining; |
| 507 | } |
| 508 | // does not check for numeric overflow |
| 509 | size_t desiredBytes = desiredFrames * handle->bytesPerFrame; |
| 510 | void *temp = NULL; |
| 511 | unsigned format = handle->info.format & SF_FORMAT_SUBMASK; |
| 512 | size_t actualBytes; |
| 513 | if (format == SF_FORMAT_PCM_16 || format == SF_FORMAT_PCM_U8) { |
| 514 | temp = malloc(desiredBytes); |
| 515 | actualBytes = fread(temp, sizeof(char), desiredBytes, handle->stream); |
| 516 | } else { |
| 517 | actualBytes = fread(ptr, sizeof(char), desiredBytes, handle->stream); |
| 518 | } |
| 519 | size_t actualFrames = actualBytes / handle->bytesPerFrame; |
| 520 | handle->remaining -= actualFrames; |
| 521 | switch (format) { |
| 522 | case SF_FORMAT_PCM_U8: |
| 523 | #if 0 |
| 524 | // TODO - implement |
| 525 | memcpy_to_i32_from_u8(ptr, (const unsigned char *) temp, |
| 526 | actualFrames * handle->info.channels); |
| 527 | #endif |
| 528 | free(temp); |
| 529 | break; |
| 530 | case SF_FORMAT_PCM_16: |
| 531 | memcpy_to_i32_from_i16(ptr, (const short *) temp, actualFrames * handle->info.channels); |
| 532 | free(temp); |
| 533 | break; |
| 534 | case SF_FORMAT_PCM_32: |
| 535 | break; |
| 536 | case SF_FORMAT_FLOAT: |
| 537 | memcpy_to_i32_from_float(ptr, (const float *) ptr, actualFrames * handle->info.channels); |
| 538 | break; |
| 539 | default: |
| 540 | memset(ptr, 0, actualFrames * handle->info.channels * sizeof(int)); |
| 541 | break; |
| 542 | } |
| 543 | return actualFrames; |
| 544 | } |
| 545 | |
Glenn Kasten | eae13dd | 2013-01-04 11:24:55 -0800 | [diff] [blame] | 546 | sf_count_t sf_writef_short(SNDFILE *handle, const short *ptr, sf_count_t desiredFrames) |
| 547 | { |
| 548 | if (handle == NULL || handle->mode != SFM_WRITE || ptr == NULL || desiredFrames <= 0) |
| 549 | return 0; |
| 550 | size_t desiredBytes = desiredFrames * handle->bytesPerFrame; |
| 551 | size_t actualBytes = 0; |
| 552 | switch (handle->info.format & SF_FORMAT_SUBMASK) { |
| 553 | case SF_FORMAT_PCM_U8: |
| 554 | handle->temp = realloc(handle->temp, desiredBytes); |
| 555 | memcpy_to_u8_from_i16(handle->temp, ptr, desiredBytes); |
| 556 | actualBytes = fwrite(handle->temp, sizeof(char), desiredBytes, handle->stream); |
| 557 | break; |
| 558 | case SF_FORMAT_PCM_16: |
| 559 | // does not check for numeric overflow |
| 560 | if (isLittleEndian()) { |
| 561 | actualBytes = fwrite(ptr, sizeof(char), desiredBytes, handle->stream); |
| 562 | } else { |
| 563 | handle->temp = realloc(handle->temp, desiredBytes); |
| 564 | memcpy(handle->temp, ptr, desiredBytes); |
Glenn Kasten | 8fdafc9 | 2013-02-12 16:23:42 -0800 | [diff] [blame] | 565 | my_swab((short *) handle->temp, desiredFrames * handle->info.channels); |
Glenn Kasten | eae13dd | 2013-01-04 11:24:55 -0800 | [diff] [blame] | 566 | actualBytes = fwrite(handle->temp, sizeof(char), desiredBytes, handle->stream); |
| 567 | } |
| 568 | break; |
Andy Hung | e9b333c | 2014-06-03 19:22:26 -0700 | [diff] [blame] | 569 | case SF_FORMAT_FLOAT: |
| 570 | handle->temp = realloc(handle->temp, desiredBytes); |
Andy Hung | 1fa816b | 2014-06-17 14:18:08 -0700 | [diff] [blame] | 571 | memcpy_to_float_from_i16((float *) handle->temp, ptr, |
| 572 | desiredFrames * handle->info.channels); |
Andy Hung | e9b333c | 2014-06-03 19:22:26 -0700 | [diff] [blame] | 573 | actualBytes = fwrite(handle->temp, sizeof(char), desiredBytes, handle->stream); |
| 574 | break; |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 575 | default: |
| 576 | break; |
| 577 | } |
| 578 | size_t actualFrames = actualBytes / handle->bytesPerFrame; |
| 579 | handle->remaining += actualFrames; |
| 580 | return actualFrames; |
| 581 | } |
| 582 | |
| 583 | sf_count_t sf_writef_float(SNDFILE *handle, const float *ptr, sf_count_t desiredFrames) |
| 584 | { |
| 585 | if (handle == NULL || handle->mode != SFM_WRITE || ptr == NULL || desiredFrames <= 0) |
| 586 | return 0; |
| 587 | size_t desiredBytes = desiredFrames * handle->bytesPerFrame; |
| 588 | size_t actualBytes = 0; |
| 589 | switch (handle->info.format & SF_FORMAT_SUBMASK) { |
| 590 | case SF_FORMAT_FLOAT: |
| 591 | actualBytes = fwrite(ptr, sizeof(char), desiredBytes, handle->stream); |
| 592 | break; |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 593 | case SF_FORMAT_PCM_16: |
Andy Hung | e9b333c | 2014-06-03 19:22:26 -0700 | [diff] [blame] | 594 | handle->temp = realloc(handle->temp, desiredBytes); |
Andy Hung | 1fa816b | 2014-06-17 14:18:08 -0700 | [diff] [blame] | 595 | memcpy_to_i16_from_float((short *) handle->temp, ptr, |
| 596 | desiredFrames * handle->info.channels); |
Andy Hung | e9b333c | 2014-06-03 19:22:26 -0700 | [diff] [blame] | 597 | actualBytes = fwrite(handle->temp, sizeof(char), desiredBytes, handle->stream); |
| 598 | break; |
| 599 | case SF_FORMAT_PCM_U8: // transcoding from float to byte not yet implemented |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 600 | default: |
| 601 | break; |
Glenn Kasten | eae13dd | 2013-01-04 11:24:55 -0800 | [diff] [blame] | 602 | } |
| 603 | size_t actualFrames = actualBytes / handle->bytesPerFrame; |
| 604 | handle->remaining += actualFrames; |
| 605 | return actualFrames; |
| 606 | } |