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 | 207ec29 | 2012-10-30 16:09:18 -0700 | [diff] [blame] | 19 | #include <stdio.h> |
| 20 | #include <string.h> |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 21 | #include <errno.h> |
| 22 | |
| 23 | #define WAVE_FORMAT_PCM 1 |
| 24 | #define WAVE_FORMAT_IEEE_FLOAT 3 |
| 25 | #define WAVE_FORMAT_EXTENSIBLE 0xFFFE |
Glenn Kasten | 207ec29 | 2012-10-30 16:09:18 -0700 | [diff] [blame] | 26 | |
| 27 | struct SNDFILE_ { |
Glenn Kasten | eae13dd | 2013-01-04 11:24:55 -0800 | [diff] [blame] | 28 | int mode; |
| 29 | 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] | 30 | FILE *stream; |
| 31 | size_t bytesPerFrame; |
Glenn Kasten | eae13dd | 2013-01-04 11:24:55 -0800 | [diff] [blame] | 32 | 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] | 33 | SF_INFO info; |
| 34 | }; |
| 35 | |
| 36 | static unsigned little2u(unsigned char *ptr) |
| 37 | { |
| 38 | return (ptr[1] << 8) + ptr[0]; |
| 39 | } |
| 40 | |
| 41 | static unsigned little4u(unsigned char *ptr) |
| 42 | { |
| 43 | return (ptr[3] << 24) + (ptr[2] << 16) + (ptr[1] << 8) + ptr[0]; |
| 44 | } |
| 45 | |
| 46 | static int isLittleEndian(void) |
| 47 | { |
| 48 | static const short one = 1; |
| 49 | return *((const char *) &one) == 1; |
| 50 | } |
| 51 | |
Glenn Kasten | 8fdafc9 | 2013-02-12 16:23:42 -0800 | [diff] [blame] | 52 | // "swab" conflicts with OS X <string.h> |
| 53 | static void my_swab(short *ptr, size_t numToSwap) |
Glenn Kasten | 207ec29 | 2012-10-30 16:09:18 -0700 | [diff] [blame] | 54 | { |
| 55 | while (numToSwap > 0) { |
| 56 | *ptr = little2u((unsigned char *) ptr); |
| 57 | --numToSwap; |
| 58 | ++ptr; |
| 59 | } |
| 60 | } |
| 61 | |
Glenn Kasten | eae13dd | 2013-01-04 11:24:55 -0800 | [diff] [blame] | 62 | static SNDFILE *sf_open_read(const char *path, SF_INFO *info) |
Glenn Kasten | 207ec29 | 2012-10-30 16:09:18 -0700 | [diff] [blame] | 63 | { |
Glenn Kasten | 207ec29 | 2012-10-30 16:09:18 -0700 | [diff] [blame] | 64 | FILE *stream = fopen(path, "rb"); |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 65 | if (stream == NULL) { |
| 66 | fprintf(stderr, "fopen %s failed errno %d\n", path, errno); |
Glenn Kasten | 207ec29 | 2012-10-30 16:09:18 -0700 | [diff] [blame] | 67 | return NULL; |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | SNDFILE *handle = (SNDFILE *) malloc(sizeof(SNDFILE)); |
| 71 | handle->mode = SFM_READ; |
| 72 | handle->temp = NULL; |
| 73 | handle->stream = stream; |
| 74 | handle->info.format = SF_FORMAT_WAV; |
| 75 | |
| 76 | // don't attempt to parse all valid forms, just the most common ones |
| 77 | unsigned char wav[12]; |
Glenn Kasten | 207ec29 | 2012-10-30 16:09:18 -0700 | [diff] [blame] | 78 | size_t actual; |
| 79 | actual = fread(wav, sizeof(char), sizeof(wav), stream); |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 80 | if (actual < 12) { |
Kévin PETIT | 96d859c | 2014-03-26 11:56:32 +0000 | [diff] [blame] | 81 | fprintf(stderr, "actual %zu < 44\n", actual); |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 82 | goto close; |
Glenn Kasten | 207ec29 | 2012-10-30 16:09:18 -0700 | [diff] [blame] | 83 | } |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 84 | if (memcmp(wav, "RIFF", 4)) { |
| 85 | fprintf(stderr, "wav != RIFF\n"); |
| 86 | goto close; |
| 87 | } |
| 88 | unsigned riffSize = little4u(&wav[4]); |
| 89 | if (riffSize < 4) { |
| 90 | fprintf(stderr, "riffSize %u < 4\n", riffSize); |
| 91 | goto close; |
| 92 | } |
| 93 | if (memcmp(&wav[8], "WAVE", 4)) { |
| 94 | fprintf(stderr, "missing WAVE\n"); |
| 95 | goto close; |
| 96 | } |
| 97 | size_t remaining = riffSize - 4; |
| 98 | int hadFmt = 0; |
| 99 | int hadData = 0; |
| 100 | while (remaining >= 8) { |
| 101 | unsigned char chunk[8]; |
| 102 | actual = fread(chunk, sizeof(char), sizeof(chunk), stream); |
| 103 | if (actual != sizeof(chunk)) { |
Kévin PETIT | 96d859c | 2014-03-26 11:56:32 +0000 | [diff] [blame] | 104 | fprintf(stderr, "actual %zu != %zu\n", actual, sizeof(chunk)); |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 105 | goto close; |
| 106 | } |
| 107 | remaining -= 8; |
| 108 | unsigned chunkSize = little4u(&chunk[4]); |
| 109 | if (chunkSize > remaining) { |
Kévin PETIT | 96d859c | 2014-03-26 11:56:32 +0000 | [diff] [blame] | 110 | fprintf(stderr, "chunkSize %u > remaining %zu\n", chunkSize, remaining); |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 111 | goto close; |
| 112 | } |
| 113 | if (!memcmp(&chunk[0], "fmt ", 4)) { |
| 114 | if (hadFmt) { |
| 115 | fprintf(stderr, "multiple fmt\n"); |
| 116 | goto close; |
| 117 | } |
| 118 | if (chunkSize < 2) { |
| 119 | fprintf(stderr, "chunkSize %u < 2\n", chunkSize); |
| 120 | goto close; |
| 121 | } |
| 122 | unsigned char fmt[40]; |
| 123 | actual = fread(fmt, sizeof(char), 2, stream); |
| 124 | if (actual != 2) { |
Kévin PETIT | 96d859c | 2014-03-26 11:56:32 +0000 | [diff] [blame] | 125 | fprintf(stderr, "actual %zu != 2\n", actual); |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 126 | goto close; |
| 127 | } |
| 128 | unsigned format = little2u(&fmt[0]); |
| 129 | size_t minSize = 0; |
| 130 | switch (format) { |
| 131 | case WAVE_FORMAT_PCM: |
| 132 | case WAVE_FORMAT_IEEE_FLOAT: |
| 133 | minSize = 16; |
| 134 | break; |
| 135 | case WAVE_FORMAT_EXTENSIBLE: |
| 136 | minSize = 40; |
| 137 | break; |
| 138 | default: |
| 139 | fprintf(stderr, "unsupported format %u\n", format); |
| 140 | goto close; |
| 141 | } |
| 142 | if (chunkSize < minSize) { |
Kévin PETIT | 96d859c | 2014-03-26 11:56:32 +0000 | [diff] [blame] | 143 | fprintf(stderr, "chunkSize %u < minSize %zu\n", chunkSize, minSize); |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 144 | goto close; |
| 145 | } |
| 146 | actual = fread(&fmt[2], sizeof(char), minSize - 2, stream); |
| 147 | if (actual != minSize - 2) { |
Kévin PETIT | 96d859c | 2014-03-26 11:56:32 +0000 | [diff] [blame] | 148 | fprintf(stderr, "actual %zu != %zu\n", actual, minSize - 16); |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 149 | goto close; |
| 150 | } |
| 151 | if (chunkSize > minSize) { |
| 152 | fseek(stream, (long) (chunkSize - minSize), SEEK_CUR); |
| 153 | } |
| 154 | unsigned channels = little2u(&fmt[2]); |
Glenn Kasten | 7c1930a | 2014-05-07 11:20:55 -0700 | [diff] [blame] | 155 | if (channels != 1 && channels != 2 && channels != 4 && channels != 6 && channels != 8) { |
| 156 | fprintf(stderr, "unsupported channels %u\n", channels); |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 157 | goto close; |
| 158 | } |
| 159 | unsigned samplerate = little4u(&fmt[4]); |
| 160 | if (samplerate == 0) { |
| 161 | fprintf(stderr, "samplerate %u == 0\n", samplerate); |
| 162 | goto close; |
| 163 | } |
| 164 | // ignore byte rate |
| 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); |
| 169 | goto close; |
| 170 | } |
| 171 | unsigned bytesPerFrame = (bitsPerSample >> 3) * channels; |
| 172 | handle->bytesPerFrame = bytesPerFrame; |
| 173 | handle->info.samplerate = samplerate; |
| 174 | handle->info.channels = channels; |
| 175 | switch (bitsPerSample) { |
| 176 | case 8: |
| 177 | handle->info.format |= SF_FORMAT_PCM_U8; |
| 178 | break; |
| 179 | case 16: |
| 180 | handle->info.format |= SF_FORMAT_PCM_16; |
| 181 | break; |
| 182 | case 32: |
| 183 | if (format == WAVE_FORMAT_IEEE_FLOAT) |
| 184 | handle->info.format |= SF_FORMAT_FLOAT; |
| 185 | else |
| 186 | handle->info.format |= SF_FORMAT_PCM_32; |
| 187 | break; |
| 188 | } |
| 189 | hadFmt = 1; |
| 190 | } else if (!memcmp(&chunk[0], "data", 4)) { |
| 191 | if (!hadFmt) { |
| 192 | fprintf(stderr, "data not preceded by fmt\n"); |
| 193 | goto close; |
| 194 | } |
| 195 | if (hadData) { |
| 196 | fprintf(stderr, "multiple data\n"); |
| 197 | goto close; |
| 198 | } |
| 199 | handle->remaining = chunkSize / handle->bytesPerFrame; |
| 200 | handle->info.frames = handle->remaining; |
| 201 | hadData = 1; |
| 202 | } else if (!memcmp(&chunk[0], "fact", 4)) { |
| 203 | // ignore fact |
| 204 | if (chunkSize > 0) { |
| 205 | fseek(stream, (long) chunkSize, SEEK_CUR); |
| 206 | } |
| 207 | } else { |
| 208 | // ignore unknown chunk |
| 209 | fprintf(stderr, "ignoring unknown chunk %c%c%c%c\n", |
| 210 | chunk[0], chunk[1], chunk[2], chunk[3]); |
| 211 | if (chunkSize > 0) { |
| 212 | fseek(stream, (long) chunkSize, SEEK_CUR); |
| 213 | } |
| 214 | } |
| 215 | remaining -= chunkSize; |
| 216 | } |
| 217 | if (remaining > 0) { |
Kévin PETIT | 96d859c | 2014-03-26 11:56:32 +0000 | [diff] [blame] | 218 | fprintf(stderr, "partial chunk at end of RIFF, remaining %zu\n", remaining); |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 219 | goto close; |
| 220 | } |
| 221 | if (!hadData) { |
| 222 | fprintf(stderr, "missing data\n"); |
| 223 | goto close; |
| 224 | } |
| 225 | *info = handle->info; |
| 226 | return handle; |
| 227 | |
| 228 | close: |
| 229 | free(handle); |
| 230 | fclose(stream); |
Glenn Kasten | 207ec29 | 2012-10-30 16:09:18 -0700 | [diff] [blame] | 231 | return NULL; |
| 232 | } |
| 233 | |
Glenn Kasten | eae13dd | 2013-01-04 11:24:55 -0800 | [diff] [blame] | 234 | static void write4u(unsigned char *ptr, unsigned u) |
| 235 | { |
| 236 | ptr[0] = u; |
| 237 | ptr[1] = u >> 8; |
| 238 | ptr[2] = u >> 16; |
| 239 | ptr[3] = u >> 24; |
| 240 | } |
| 241 | |
| 242 | static SNDFILE *sf_open_write(const char *path, SF_INFO *info) |
| 243 | { |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 244 | int sub = info->format & SF_FORMAT_SUBMASK; |
Glenn Kasten | eae13dd | 2013-01-04 11:24:55 -0800 | [diff] [blame] | 245 | if (!( |
| 246 | (info->samplerate > 0) && |
Andy Hung | 7095bc7 | 2014-04-09 18:52:06 -0700 | [diff] [blame] | 247 | (info->channels > 0 && info->channels <= 8) && |
Glenn Kasten | eae13dd | 2013-01-04 11:24:55 -0800 | [diff] [blame] | 248 | ((info->format & SF_FORMAT_TYPEMASK) == SF_FORMAT_WAV) && |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 249 | (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] | 250 | )) { |
| 251 | return NULL; |
| 252 | } |
| 253 | FILE *stream = fopen(path, "w+b"); |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 254 | unsigned char wav[58]; |
Glenn Kasten | eae13dd | 2013-01-04 11:24:55 -0800 | [diff] [blame] | 255 | memset(wav, 0, sizeof(wav)); |
| 256 | memcpy(wav, "RIFF", 4); |
Glenn Kasten | eae13dd | 2013-01-04 11:24:55 -0800 | [diff] [blame] | 257 | memcpy(&wav[8], "WAVEfmt ", 8); |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 258 | if (sub == SF_FORMAT_FLOAT) { |
| 259 | wav[4] = 50; // riffSize |
| 260 | wav[16] = 18; // fmtSize |
| 261 | wav[20] = WAVE_FORMAT_IEEE_FLOAT; |
| 262 | } else { |
| 263 | wav[4] = 36; // riffSize |
| 264 | wav[16] = 16; // fmtSize |
| 265 | wav[20] = WAVE_FORMAT_PCM; |
| 266 | } |
Glenn Kasten | eae13dd | 2013-01-04 11:24:55 -0800 | [diff] [blame] | 267 | wav[22] = info->channels; |
| 268 | write4u(&wav[24], info->samplerate); |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 269 | unsigned bitsPerSample; |
| 270 | switch (sub) { |
| 271 | case SF_FORMAT_PCM_16: |
| 272 | bitsPerSample = 16; |
| 273 | break; |
| 274 | case SF_FORMAT_PCM_U8: |
| 275 | bitsPerSample = 8; |
| 276 | break; |
| 277 | case SF_FORMAT_FLOAT: |
| 278 | bitsPerSample = 32; |
| 279 | break; |
| 280 | default: // not reachable |
| 281 | bitsPerSample = 0; |
| 282 | break; |
| 283 | } |
Glenn Kasten | eae13dd | 2013-01-04 11:24:55 -0800 | [diff] [blame] | 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; |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 289 | if (sub == SF_FORMAT_FLOAT) { |
| 290 | memcpy(&wav[38], "fact", 4); |
| 291 | wav[42] = 4; |
| 292 | memcpy(&wav[50], "data", 4); |
| 293 | } else |
| 294 | memcpy(&wav[36], "data", 4); |
Glenn Kasten | eae13dd | 2013-01-04 11:24:55 -0800 | [diff] [blame] | 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; |
| 299 | handle->temp = NULL; |
| 300 | handle->stream = stream; |
| 301 | handle->bytesPerFrame = blockAlignment; |
| 302 | handle->remaining = 0; |
| 303 | handle->info = *info; |
| 304 | return handle; |
| 305 | } |
| 306 | |
| 307 | SNDFILE *sf_open(const char *path, int mode, SF_INFO *info) |
| 308 | { |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 309 | if (path == NULL || info == NULL) { |
| 310 | fprintf(stderr, "path=%p info=%p\n", path, info); |
Glenn Kasten | eae13dd | 2013-01-04 11:24:55 -0800 | [diff] [blame] | 311 | return NULL; |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 312 | } |
Glenn Kasten | eae13dd | 2013-01-04 11:24:55 -0800 | [diff] [blame] | 313 | switch (mode) { |
| 314 | case SFM_READ: |
| 315 | return sf_open_read(path, info); |
| 316 | case SFM_WRITE: |
| 317 | return sf_open_write(path, info); |
| 318 | default: |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 319 | fprintf(stderr, "mode=%d\n", mode); |
Glenn Kasten | eae13dd | 2013-01-04 11:24:55 -0800 | [diff] [blame] | 320 | return NULL; |
| 321 | } |
| 322 | } |
| 323 | |
Glenn Kasten | 207ec29 | 2012-10-30 16:09:18 -0700 | [diff] [blame] | 324 | void sf_close(SNDFILE *handle) |
| 325 | { |
| 326 | if (handle == NULL) |
| 327 | return; |
Glenn Kasten | eae13dd | 2013-01-04 11:24:55 -0800 | [diff] [blame] | 328 | free(handle->temp); |
| 329 | if (handle->mode == SFM_WRITE) { |
| 330 | (void) fflush(handle->stream); |
| 331 | rewind(handle->stream); |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 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); |
Glenn Kasten | eae13dd | 2013-01-04 11:24:55 -0800 | [diff] [blame] | 335 | unsigned dataSize = handle->remaining * handle->bytesPerFrame; |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 336 | write4u(&wav[4], dataSize + 36 + extra); // riffSize |
| 337 | write4u(&wav[40 + extra], dataSize); // dataSize |
Glenn Kasten | eae13dd | 2013-01-04 11:24:55 -0800 | [diff] [blame] | 338 | rewind(handle->stream); |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 339 | (void) fwrite(wav, 44 + extra, 1, handle->stream); |
Glenn Kasten | eae13dd | 2013-01-04 11:24:55 -0800 | [diff] [blame] | 340 | } |
Glenn Kasten | 207ec29 | 2012-10-30 16:09:18 -0700 | [diff] [blame] | 341 | (void) fclose(handle->stream); |
Glenn Kasten | eae13dd | 2013-01-04 11:24:55 -0800 | [diff] [blame] | 342 | free(handle); |
Glenn Kasten | 207ec29 | 2012-10-30 16:09:18 -0700 | [diff] [blame] | 343 | } |
| 344 | |
Glenn Kasten | 36c248b | 2012-11-12 14:42:31 -0800 | [diff] [blame] | 345 | 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] | 346 | { |
Glenn Kasten | eae13dd | 2013-01-04 11:24:55 -0800 | [diff] [blame] | 347 | if (handle == NULL || handle->mode != SFM_READ || ptr == NULL || !handle->remaining || |
| 348 | desiredFrames <= 0) { |
Glenn Kasten | 207ec29 | 2012-10-30 16:09:18 -0700 | [diff] [blame] | 349 | return 0; |
Glenn Kasten | eae13dd | 2013-01-04 11:24:55 -0800 | [diff] [blame] | 350 | } |
Glenn Kasten | c89cb60 | 2014-03-11 09:03:35 -0700 | [diff] [blame] | 351 | if (handle->remaining < (size_t) desiredFrames) { |
Glenn Kasten | 207ec29 | 2012-10-30 16:09:18 -0700 | [diff] [blame] | 352 | desiredFrames = handle->remaining; |
Glenn Kasten | c89cb60 | 2014-03-11 09:03:35 -0700 | [diff] [blame] | 353 | } |
Glenn Kasten | 207ec29 | 2012-10-30 16:09:18 -0700 | [diff] [blame] | 354 | // does not check for numeric overflow |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 355 | size_t desiredBytes = desiredFrames * handle->bytesPerFrame; |
| 356 | size_t actualBytes; |
| 357 | void *temp = NULL; |
| 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); |
| 362 | } else { |
| 363 | actualBytes = fread(ptr, sizeof(char), desiredBytes, handle->stream); |
| 364 | } |
Glenn Kasten | 207ec29 | 2012-10-30 16:09:18 -0700 | [diff] [blame] | 365 | size_t actualFrames = actualBytes / handle->bytesPerFrame; |
| 366 | handle->remaining -= actualFrames; |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 367 | switch (format) { |
Glenn Kasten | 36c248b | 2012-11-12 14:42:31 -0800 | [diff] [blame] | 368 | case SF_FORMAT_PCM_U8: |
| 369 | memcpy_to_i16_from_u8(ptr, (unsigned char *) ptr, actualFrames * handle->info.channels); |
| 370 | break; |
| 371 | case SF_FORMAT_PCM_16: |
| 372 | if (!isLittleEndian()) |
Glenn Kasten | 8fdafc9 | 2013-02-12 16:23:42 -0800 | [diff] [blame] | 373 | my_swab(ptr, actualFrames * handle->info.channels); |
Glenn Kasten | 36c248b | 2012-11-12 14:42:31 -0800 | [diff] [blame] | 374 | break; |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 375 | case SF_FORMAT_PCM_32: |
| 376 | memcpy_to_i16_from_i32(ptr, (const int *) temp, actualFrames * handle->info.channels); |
| 377 | free(temp); |
| 378 | break; |
| 379 | case SF_FORMAT_FLOAT: |
| 380 | memcpy_to_i16_from_float(ptr, (const float *) temp, actualFrames * handle->info.channels); |
| 381 | free(temp); |
| 382 | break; |
| 383 | default: |
| 384 | memset(ptr, 0, actualFrames * handle->info.channels * sizeof(short)); |
| 385 | break; |
Glenn Kasten | 36c248b | 2012-11-12 14:42:31 -0800 | [diff] [blame] | 386 | } |
Glenn Kasten | 207ec29 | 2012-10-30 16:09:18 -0700 | [diff] [blame] | 387 | return actualFrames; |
| 388 | } |
Glenn Kasten | eae13dd | 2013-01-04 11:24:55 -0800 | [diff] [blame] | 389 | |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 390 | sf_count_t sf_readf_float(SNDFILE *handle, float *ptr, sf_count_t desiredFrames) |
| 391 | { |
Glenn Kasten | c89cb60 | 2014-03-11 09:03:35 -0700 | [diff] [blame] | 392 | if (handle == NULL || handle->mode != SFM_READ || ptr == NULL || !handle->remaining || |
| 393 | desiredFrames <= 0) { |
| 394 | return 0; |
| 395 | } |
| 396 | if (handle->remaining < (size_t) desiredFrames) { |
| 397 | desiredFrames = handle->remaining; |
| 398 | } |
| 399 | // does not check for numeric overflow |
| 400 | size_t desiredBytes = desiredFrames * handle->bytesPerFrame; |
| 401 | size_t actualBytes; |
Andy Hung | 5237016 | 2014-05-22 18:17:53 -0700 | [diff] [blame^] | 402 | void *temp = NULL; |
Glenn Kasten | c89cb60 | 2014-03-11 09:03:35 -0700 | [diff] [blame] | 403 | unsigned format = handle->info.format & SF_FORMAT_SUBMASK; |
Andy Hung | 5237016 | 2014-05-22 18:17:53 -0700 | [diff] [blame^] | 404 | if (format == SF_FORMAT_PCM_16 || format == SF_FORMAT_PCM_U8) { |
| 405 | temp = malloc(desiredBytes); |
| 406 | actualBytes = fread(temp, sizeof(char), desiredBytes, handle->stream); |
| 407 | } else { |
| 408 | actualBytes = fread(ptr, sizeof(char), desiredBytes, handle->stream); |
| 409 | } |
Glenn Kasten | c89cb60 | 2014-03-11 09:03:35 -0700 | [diff] [blame] | 410 | size_t actualFrames = actualBytes / handle->bytesPerFrame; |
| 411 | handle->remaining -= actualFrames; |
| 412 | switch (format) { |
Glenn Kasten | c89cb60 | 2014-03-11 09:03:35 -0700 | [diff] [blame] | 413 | case SF_FORMAT_PCM_U8: |
Glenn Kasten | c89cb60 | 2014-03-11 09:03:35 -0700 | [diff] [blame] | 414 | #if 0 |
Andy Hung | 5237016 | 2014-05-22 18:17:53 -0700 | [diff] [blame^] | 415 | // TODO - implement |
| 416 | memcpy_to_float_from_u8(ptr, (const unsigned char *) temp, |
| 417 | actualFrames * handle->info.channels); |
| 418 | #endif |
| 419 | free(temp); |
| 420 | break; |
| 421 | case SF_FORMAT_PCM_16: |
| 422 | memcpy_to_float_from_i16(ptr, (const short *) temp, actualFrames * handle->info.channels); |
| 423 | free(temp); |
| 424 | break; |
Glenn Kasten | c89cb60 | 2014-03-11 09:03:35 -0700 | [diff] [blame] | 425 | case SF_FORMAT_PCM_32: |
| 426 | memcpy_to_float_from_i32(ptr, (const int *) ptr, actualFrames * handle->info.channels); |
| 427 | break; |
Glenn Kasten | c89cb60 | 2014-03-11 09:03:35 -0700 | [diff] [blame] | 428 | case SF_FORMAT_FLOAT: |
| 429 | break; |
| 430 | default: |
| 431 | memset(ptr, 0, actualFrames * handle->info.channels * sizeof(float)); |
| 432 | break; |
| 433 | } |
| 434 | return actualFrames; |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 435 | } |
| 436 | |
Glenn Kasten | eae13dd | 2013-01-04 11:24:55 -0800 | [diff] [blame] | 437 | sf_count_t sf_writef_short(SNDFILE *handle, const short *ptr, sf_count_t desiredFrames) |
| 438 | { |
| 439 | if (handle == NULL || handle->mode != SFM_WRITE || ptr == NULL || desiredFrames <= 0) |
| 440 | return 0; |
| 441 | size_t desiredBytes = desiredFrames * handle->bytesPerFrame; |
| 442 | size_t actualBytes = 0; |
| 443 | switch (handle->info.format & SF_FORMAT_SUBMASK) { |
| 444 | case SF_FORMAT_PCM_U8: |
| 445 | handle->temp = realloc(handle->temp, desiredBytes); |
| 446 | memcpy_to_u8_from_i16(handle->temp, ptr, desiredBytes); |
| 447 | actualBytes = fwrite(handle->temp, sizeof(char), desiredBytes, handle->stream); |
| 448 | break; |
| 449 | case SF_FORMAT_PCM_16: |
| 450 | // does not check for numeric overflow |
| 451 | if (isLittleEndian()) { |
| 452 | actualBytes = fwrite(ptr, sizeof(char), desiredBytes, handle->stream); |
| 453 | } else { |
| 454 | handle->temp = realloc(handle->temp, desiredBytes); |
| 455 | memcpy(handle->temp, ptr, desiredBytes); |
Glenn Kasten | 8fdafc9 | 2013-02-12 16:23:42 -0800 | [diff] [blame] | 456 | my_swab((short *) handle->temp, desiredFrames * handle->info.channels); |
Glenn Kasten | eae13dd | 2013-01-04 11:24:55 -0800 | [diff] [blame] | 457 | actualBytes = fwrite(handle->temp, sizeof(char), desiredBytes, handle->stream); |
| 458 | } |
| 459 | break; |
Glenn Kasten | c0bd715 | 2013-02-05 13:14:08 -0800 | [diff] [blame] | 460 | case SF_FORMAT_FLOAT: // transcoding from short to float not yet implemented |
| 461 | default: |
| 462 | break; |
| 463 | } |
| 464 | size_t actualFrames = actualBytes / handle->bytesPerFrame; |
| 465 | handle->remaining += actualFrames; |
| 466 | return actualFrames; |
| 467 | } |
| 468 | |
| 469 | sf_count_t sf_writef_float(SNDFILE *handle, const float *ptr, sf_count_t desiredFrames) |
| 470 | { |
| 471 | if (handle == NULL || handle->mode != SFM_WRITE || ptr == NULL || desiredFrames <= 0) |
| 472 | return 0; |
| 473 | size_t desiredBytes = desiredFrames * handle->bytesPerFrame; |
| 474 | size_t actualBytes = 0; |
| 475 | switch (handle->info.format & SF_FORMAT_SUBMASK) { |
| 476 | case SF_FORMAT_FLOAT: |
| 477 | actualBytes = fwrite(ptr, sizeof(char), desiredBytes, handle->stream); |
| 478 | break; |
| 479 | case SF_FORMAT_PCM_U8: // transcoding from float to byte/short not yet implemented |
| 480 | case SF_FORMAT_PCM_16: |
| 481 | default: |
| 482 | break; |
Glenn Kasten | eae13dd | 2013-01-04 11:24:55 -0800 | [diff] [blame] | 483 | } |
| 484 | size_t actualFrames = actualBytes / handle->bytesPerFrame; |
| 485 | handle->remaining += actualFrames; |
| 486 | return actualFrames; |
| 487 | } |