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