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