2 ** Copyright (C) 1999-2005 Erik de Castro Lopo <erikd@mega-nerd.com>
4 ** This program is free software; you can redistribute it and/or modify
5 ** it under the terms of the GNU Lesser General Public License as published by
6 ** the Free Software Foundation; either version 2.1 of the License, or
7 ** (at your option) any later version.
9 ** This program is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ** GNU Lesser General Public License for more details.
14 ** You should have received a copy of the GNU Lesser General Public License
15 ** along with this program; if not, write to the Free Software
16 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 #include "float_cast.h"
30 #if CPU_IS_LITTLE_ENDIAN
31 #define FLOAT32_READ float32_le_read
32 #define FLOAT32_WRITE float32_le_write
33 #elif CPU_IS_BIG_ENDIAN
34 #define FLOAT32_READ float32_be_read
35 #define FLOAT32_WRITE float32_be_write
38 /*--------------------------------------------------------------------------------------------
39 ** Processor floating point capabilities. float32_get_capability () returns one of the
40 ** latter four values.
44 { FLOAT_UNKNOWN = 0x00,
45 FLOAT_CAN_RW_LE = 0x12,
46 FLOAT_CAN_RW_BE = 0x23,
47 FLOAT_BROKEN_LE = 0x34,
48 FLOAT_BROKEN_BE = 0x45
51 /*--------------------------------------------------------------------------------------------
52 ** Prototypes for private functions.
55 static sf_count_t host_read_f2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
56 static sf_count_t host_read_f2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
57 static sf_count_t host_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
58 static sf_count_t host_read_f2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
60 static sf_count_t host_write_s2f (SF_PRIVATE *psf, const short *ptr, sf_count_t len) ;
61 static sf_count_t host_write_i2f (SF_PRIVATE *psf, const int *ptr, sf_count_t len) ;
62 static sf_count_t host_write_f (SF_PRIVATE *psf, const float *ptr, sf_count_t len) ;
63 static sf_count_t host_write_d2f (SF_PRIVATE *psf, const double *ptr, sf_count_t len) ;
65 static void float32_peak_update (SF_PRIVATE *psf, const float *buffer, int count, sf_count_t indx) ;
67 static sf_count_t replace_read_f2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
68 static sf_count_t replace_read_f2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
69 static sf_count_t replace_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
70 static sf_count_t replace_read_f2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
72 static sf_count_t replace_write_s2f (SF_PRIVATE *psf, const short *ptr, sf_count_t len) ;
73 static sf_count_t replace_write_i2f (SF_PRIVATE *psf, const int *ptr, sf_count_t len) ;
74 static sf_count_t replace_write_f (SF_PRIVATE *psf, const float *ptr, sf_count_t len) ;
75 static sf_count_t replace_write_d2f (SF_PRIVATE *psf, const double *ptr, sf_count_t len) ;
77 static void bf2f_array (float *buffer, int count) ;
78 static void f2bf_array (float *buffer, int count) ;
80 static int float32_get_capability (SF_PRIVATE *psf) ;
82 /*--------------------------------------------------------------------------------------------
83 ** Exported functions.
87 float32_init (SF_PRIVATE *psf)
88 { static int float_caps ;
90 float_caps = float32_get_capability (psf) ;
92 psf->blockwidth = sizeof (float) * psf->sf.channels ;
94 if (psf->mode == SFM_READ || psf->mode == SFM_RDWR)
95 { switch (psf->endian + float_caps)
96 { case (SF_ENDIAN_BIG + FLOAT_CAN_RW_BE) :
97 psf->float_endswap = SF_FALSE ;
98 psf->read_short = host_read_f2s ;
99 psf->read_int = host_read_f2i ;
100 psf->read_float = host_read_f ;
101 psf->read_double = host_read_f2d ;
104 case (SF_ENDIAN_LITTLE + FLOAT_CAN_RW_LE) :
105 psf->float_endswap = SF_FALSE ;
106 psf->read_short = host_read_f2s ;
107 psf->read_int = host_read_f2i ;
108 psf->read_float = host_read_f ;
109 psf->read_double = host_read_f2d ;
112 case (SF_ENDIAN_BIG + FLOAT_CAN_RW_LE) :
113 psf->float_endswap = SF_TRUE ;
114 psf->read_short = host_read_f2s ;
115 psf->read_int = host_read_f2i ;
116 psf->read_float = host_read_f ;
117 psf->read_double = host_read_f2d ;
120 case (SF_ENDIAN_LITTLE + FLOAT_CAN_RW_BE) :
121 psf->float_endswap = SF_TRUE ;
122 psf->read_short = host_read_f2s ;
123 psf->read_int = host_read_f2i ;
124 psf->read_float = host_read_f ;
125 psf->read_double = host_read_f2d ;
128 /* When the CPU is not IEEE compatible. */
129 case (SF_ENDIAN_BIG + FLOAT_BROKEN_LE) :
130 psf->float_endswap = SF_TRUE ;
131 psf->read_short = replace_read_f2s ;
132 psf->read_int = replace_read_f2i ;
133 psf->read_float = replace_read_f ;
134 psf->read_double = replace_read_f2d ;
137 case (SF_ENDIAN_LITTLE + FLOAT_BROKEN_LE) :
138 psf->float_endswap = SF_FALSE ;
139 psf->read_short = replace_read_f2s ;
140 psf->read_int = replace_read_f2i ;
141 psf->read_float = replace_read_f ;
142 psf->read_double = replace_read_f2d ;
145 case (SF_ENDIAN_BIG + FLOAT_BROKEN_BE) :
146 psf->float_endswap = SF_FALSE ;
147 psf->read_short = replace_read_f2s ;
148 psf->read_int = replace_read_f2i ;
149 psf->read_float = replace_read_f ;
150 psf->read_double = replace_read_f2d ;
153 case (SF_ENDIAN_LITTLE + FLOAT_BROKEN_BE) :
154 psf->float_endswap = SF_TRUE ;
155 psf->read_short = replace_read_f2s ;
156 psf->read_int = replace_read_f2i ;
157 psf->read_float = replace_read_f ;
158 psf->read_double = replace_read_f2d ;
165 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
166 { switch (psf->endian + float_caps)
167 { case (SF_ENDIAN_LITTLE + FLOAT_CAN_RW_LE) :
168 psf->float_endswap = SF_FALSE ;
169 psf->write_short = host_write_s2f ;
170 psf->write_int = host_write_i2f ;
171 psf->write_float = host_write_f ;
172 psf->write_double = host_write_d2f ;
175 case (SF_ENDIAN_BIG + FLOAT_CAN_RW_BE) :
176 psf->float_endswap = SF_FALSE ;
177 psf->write_short = host_write_s2f ;
178 psf->write_int = host_write_i2f ;
179 psf->write_float = host_write_f ;
180 psf->write_double = host_write_d2f ;
183 case (SF_ENDIAN_BIG + FLOAT_CAN_RW_LE) :
184 psf->float_endswap = SF_TRUE ;
185 psf->write_short = host_write_s2f ;
186 psf->write_int = host_write_i2f ;
187 psf->write_float = host_write_f ;
188 psf->write_double = host_write_d2f ;
191 case (SF_ENDIAN_LITTLE + FLOAT_CAN_RW_BE) :
192 psf->float_endswap = SF_TRUE ;
193 psf->write_short = host_write_s2f ;
194 psf->write_int = host_write_i2f ;
195 psf->write_float = host_write_f ;
196 psf->write_double = host_write_d2f ;
199 /* When the CPU is not IEEE compatible. */
200 case (SF_ENDIAN_BIG + FLOAT_BROKEN_LE) :
201 psf->float_endswap = SF_TRUE ;
202 psf->write_short = replace_write_s2f ;
203 psf->write_int = replace_write_i2f ;
204 psf->write_float = replace_write_f ;
205 psf->write_double = replace_write_d2f ;
208 case (SF_ENDIAN_LITTLE + FLOAT_BROKEN_LE) :
209 psf->float_endswap = SF_FALSE ;
210 psf->write_short = replace_write_s2f ;
211 psf->write_int = replace_write_i2f ;
212 psf->write_float = replace_write_f ;
213 psf->write_double = replace_write_d2f ;
216 case (SF_ENDIAN_BIG + FLOAT_BROKEN_BE) :
217 psf->float_endswap = SF_FALSE ;
218 psf->write_short = replace_write_s2f ;
219 psf->write_int = replace_write_i2f ;
220 psf->write_float = replace_write_f ;
221 psf->write_double = replace_write_d2f ;
224 case (SF_ENDIAN_LITTLE + FLOAT_BROKEN_BE) :
225 psf->float_endswap = SF_TRUE ;
226 psf->write_short = replace_write_s2f ;
227 psf->write_int = replace_write_i2f ;
228 psf->write_float = replace_write_f ;
229 psf->write_double = replace_write_d2f ;
236 if (psf->filelength > psf->dataoffset)
237 { psf->datalength = (psf->dataend > 0) ? psf->dataend - psf->dataoffset :
238 psf->filelength - psf->dataoffset ;
241 psf->datalength = 0 ;
243 psf->sf.frames = psf->datalength / psf->blockwidth ;
249 float32_be_read (unsigned char *cptr)
250 { int exponent, mantissa, negative ;
253 negative = cptr [0] & 0x80 ;
254 exponent = ((cptr [0] & 0x7F) << 1) | ((cptr [1] & 0x80) ? 1 : 0) ;
255 mantissa = ((cptr [1] & 0x7F) << 16) | (cptr [2] << 8) | (cptr [3]) ;
257 if (! (exponent || mantissa))
260 mantissa |= 0x800000 ;
261 exponent = exponent ? exponent - 127 : 0 ;
263 fvalue = mantissa ? ((float) mantissa) / ((float) 0x800000) : 0.0 ;
269 fvalue *= (1 << exponent) ;
270 else if (exponent < 0)
271 fvalue /= (1 << abs (exponent)) ;
274 } /* float32_be_read */
277 float32_le_read (unsigned char *cptr)
278 { int exponent, mantissa, negative ;
281 negative = cptr [3] & 0x80 ;
282 exponent = ((cptr [3] & 0x7F) << 1) | ((cptr [2] & 0x80) ? 1 : 0) ;
283 mantissa = ((cptr [2] & 0x7F) << 16) | (cptr [1] << 8) | (cptr [0]) ;
285 if (! (exponent || mantissa))
288 mantissa |= 0x800000 ;
289 exponent = exponent ? exponent - 127 : 0 ;
291 fvalue = mantissa ? ((float) mantissa) / ((float) 0x800000) : 0.0 ;
297 fvalue *= (1 << exponent) ;
298 else if (exponent < 0)
299 fvalue /= (1 << abs (exponent)) ;
302 } /* float32_le_read */
305 float32_le_write (float in, unsigned char *out)
306 { int exponent, mantissa, negative = 0 ;
308 memset (out, 0, sizeof (int)) ;
310 if (fabs (in) < 1e-30)
318 in = frexp (in, &exponent) ;
322 in *= (float) 0x1000000 ;
323 mantissa = (((int) in) & 0x7FFFFF) ;
331 out [0] = mantissa & 0xFF ;
332 out [1] = (mantissa >> 8) & 0xFF ;
333 out [2] |= (mantissa >> 16) & 0x7F ;
334 out [3] |= (exponent >> 1) & 0x7F ;
337 } /* float32_le_write */
340 float32_be_write (float in, unsigned char *out)
341 { int exponent, mantissa, negative = 0 ;
343 memset (out, 0, sizeof (int)) ;
345 if (fabs (in) < 1e-30)
353 in = frexp (in, &exponent) ;
357 in *= (float) 0x1000000 ;
358 mantissa = (((int) in) & 0x7FFFFF) ;
366 out [3] = mantissa & 0xFF ;
367 out [2] = (mantissa >> 8) & 0xFF ;
368 out [1] |= (mantissa >> 16) & 0x7F ;
369 out [0] |= (exponent >> 1) & 0x7F ;
372 } /* float32_be_write */
374 /*==============================================================================================
375 ** Private functions.
379 float32_peak_update (SF_PRIVATE *psf, const float *buffer, int count, sf_count_t indx)
384 for (chan = 0 ; chan < psf->sf.channels ; chan++)
385 { fmaxval = fabs (buffer [chan]) ;
387 for (k = chan ; k < count ; k += psf->sf.channels)
388 if (fmaxval < fabs (buffer [k]))
389 { fmaxval = fabs (buffer [k]) ;
393 if (fmaxval > psf->peak_info->peaks [chan].value)
394 { psf->peak_info->peaks [chan].value = fmaxval ;
395 psf->peak_info->peaks [chan].position = psf->write_current + indx + (position / psf->sf.channels) ;
400 } /* float32_peak_update */
403 float32_get_capability (SF_PRIVATE *psf)
407 unsigned char c [4] ;
410 data.f = (float) 1.23456789 ; /* Some abitrary value. */
412 if (! psf->ieee_replace)
413 { /* If this test is true ints and floats are compatible and little endian. */
414 if (data.c [0] == 0x52 && data.c [1] == 0x06 && data.c [2] == 0x9e && data.c [3] == 0x3f)
415 return FLOAT_CAN_RW_LE ;
417 /* If this test is true ints and floats are compatible and big endian. */
418 if (data.c [3] == 0x52 && data.c [2] == 0x06 && data.c [1] == 0x9e && data.c [0] == 0x3f)
419 return FLOAT_CAN_RW_BE ;
422 /* Floats are broken. Don't expect reading or writing to be fast. */
423 psf_log_printf (psf, "Using IEEE replacement code for float.\n") ;
425 return (CPU_IS_LITTLE_ENDIAN) ? FLOAT_BROKEN_LE : FLOAT_BROKEN_BE ;
426 } /* float32_get_capability */
428 /*=======================================================================================
432 f2s_array (const float *src, int count, short *dest, float scale)
433 { while (--count >= 0)
434 { dest [count] = lrintf (scale * src [count]) ;
439 f2i_array (const float *src, int count, int *dest, float scale)
440 { while (--count >= 0)
441 { dest [count] = lrintf (scale * src [count]) ;
446 f2d_array (const float *src, int count, double *dest)
447 { while (--count >= 0)
448 { dest [count] = src [count] ;
453 s2f_array (const short *src, float *dest, int count)
454 { while (--count >= 0)
455 { dest [count] = src [count] ;
461 i2f_array (const int *src, float *dest, int count)
462 { while (--count >= 0)
463 { dest [count] = src [count] ;
468 d2f_array (const double *src, float *dest, int count)
469 { while (--count >= 0)
470 { dest [count] = src [count] ;
474 /*----------------------------------------------------------------------------------------------
478 host_read_f2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
479 { int bufferlen, readcount ;
480 sf_count_t total = 0 ;
483 bufferlen = ARRAY_LEN (psf->u.fbuf) ;
484 scale = (psf->float_int_mult == 0) ? 1.0 : 0x7FFF / psf->float_max ;
487 { if (len < bufferlen)
488 bufferlen = (int) len ;
489 readcount = psf_fread (psf->u.fbuf, sizeof (float), bufferlen, psf) ;
491 /* Fix me : Need lef2s_array */
492 if (psf->float_endswap == SF_TRUE)
493 endswap_int_array (psf->u.ibuf, bufferlen) ;
495 f2s_array (psf->u.fbuf, readcount, ptr + total, scale) ;
497 if (readcount < bufferlen)
503 } /* host_read_f2s */
506 host_read_f2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
507 { int bufferlen, readcount ;
508 sf_count_t total = 0 ;
511 bufferlen = ARRAY_LEN (psf->u.fbuf) ;
512 scale = (psf->float_int_mult == 0) ? 1.0 : 0x7FFFFFFF / psf->float_max ;
515 { if (len < bufferlen)
516 bufferlen = (int) len ;
517 readcount = psf_fread (psf->u.fbuf, sizeof (float), bufferlen, psf) ;
519 if (psf->float_endswap == SF_TRUE)
520 endswap_int_array (psf->u.ibuf, bufferlen) ;
522 f2i_array (psf->u.fbuf, readcount, ptr + total, scale) ;
524 if (readcount < bufferlen)
530 } /* host_read_f2i */
533 host_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
534 { int bufferlen, readcount ;
535 sf_count_t total = 0 ;
537 if (psf->float_endswap != SF_TRUE)
538 return psf_fread (ptr, sizeof (float), len, psf) ;
540 bufferlen = ARRAY_LEN (psf->u.fbuf) ;
543 { if (len < bufferlen)
544 bufferlen = (int) len ;
545 readcount = psf_fread (psf->u.fbuf, sizeof (float), bufferlen, psf) ;
547 endswap_int_copy ((int*) (ptr + total), psf->u.ibuf, readcount) ;
550 if (readcount < bufferlen)
559 host_read_f2d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
560 { int bufferlen, readcount ;
561 sf_count_t total = 0 ;
563 bufferlen = ARRAY_LEN (psf->u.fbuf) ;
566 { if (len < bufferlen)
567 bufferlen = (int) len ;
568 readcount = psf_fread (psf->u.fbuf, sizeof (float), bufferlen, psf) ;
570 if (psf->float_endswap == SF_TRUE)
571 endswap_int_array (psf->u.ibuf, bufferlen) ;
573 /* Fix me : Need lef2d_array */
574 f2d_array (psf->u.fbuf, readcount, ptr + total) ;
576 if (readcount < bufferlen)
582 } /* host_read_f2d */
585 host_write_s2f (SF_PRIVATE *psf, const short *ptr, sf_count_t len)
586 { int bufferlen, writecount ;
587 sf_count_t total = 0 ;
589 bufferlen = ARRAY_LEN (psf->u.fbuf) ;
592 { if (len < bufferlen)
593 bufferlen = (int) len ;
594 s2f_array (ptr + total, psf->u.fbuf, bufferlen) ;
597 float32_peak_update (psf, psf->u.fbuf, bufferlen, total / psf->sf.channels) ;
599 if (psf->float_endswap == SF_TRUE)
600 endswap_int_array (psf->u.ibuf, bufferlen) ;
602 writecount = psf_fwrite (psf->u.fbuf, sizeof (float), bufferlen, psf) ;
603 total += writecount ;
604 if (writecount < bufferlen)
610 } /* host_write_s2f */
613 host_write_i2f (SF_PRIVATE *psf, const int *ptr, sf_count_t len)
614 { int bufferlen, writecount ;
615 sf_count_t total = 0 ;
617 bufferlen = ARRAY_LEN (psf->u.fbuf) ;
620 { if (len < bufferlen)
621 bufferlen = (int) len ;
622 i2f_array (ptr + total, psf->u.fbuf, bufferlen) ;
625 float32_peak_update (psf, psf->u.fbuf, bufferlen, total / psf->sf.channels) ;
627 if (psf->float_endswap == SF_TRUE)
628 endswap_int_array (psf->u.ibuf, bufferlen) ;
630 writecount = psf_fwrite (psf->u.fbuf, sizeof (float) , bufferlen, psf) ;
631 total += writecount ;
632 if (writecount < bufferlen)
638 } /* host_write_i2f */
641 host_write_f (SF_PRIVATE *psf, const float *ptr, sf_count_t len)
642 { int bufferlen, writecount ;
643 sf_count_t total = 0 ;
646 float32_peak_update (psf, ptr, len, 0) ;
648 if (psf->float_endswap != SF_TRUE)
649 return psf_fwrite (ptr, sizeof (float), len, psf) ;
651 bufferlen = ARRAY_LEN (psf->u.fbuf) ;
654 { if (len < bufferlen)
655 bufferlen = (int) len ;
657 endswap_int_copy (psf->u.ibuf, (const int*) (ptr + total), bufferlen) ;
659 writecount = psf_fwrite (psf->u.fbuf, sizeof (float), bufferlen, psf) ;
660 total += writecount ;
661 if (writecount < bufferlen)
670 host_write_d2f (SF_PRIVATE *psf, const double *ptr, sf_count_t len)
671 { int bufferlen, writecount ;
672 sf_count_t total = 0 ;
674 bufferlen = ARRAY_LEN (psf->u.fbuf) ;
677 { if (len < bufferlen)
678 bufferlen = (int) len ;
680 d2f_array (ptr + total, psf->u.fbuf, bufferlen) ;
683 float32_peak_update (psf, psf->u.fbuf, bufferlen, total / psf->sf.channels) ;
685 if (psf->float_endswap == SF_TRUE)
686 endswap_int_array (psf->u.ibuf, bufferlen) ;
688 writecount = psf_fwrite (psf->u.fbuf, sizeof (float), bufferlen, psf) ;
689 total += writecount ;
690 if (writecount < bufferlen)
696 } /* host_write_d2f */
698 /*=======================================================================================
702 replace_read_f2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
703 { int bufferlen, readcount ;
704 sf_count_t total = 0 ;
707 bufferlen = ARRAY_LEN (psf->u.fbuf) ;
708 scale = (psf->float_int_mult == 0) ? 1.0 : 0x7FFF / psf->float_max ;
711 { if (len < bufferlen)
712 bufferlen = (int) len ;
713 readcount = psf_fread (psf->u.fbuf, sizeof (float), bufferlen, psf) ;
715 if (psf->float_endswap == SF_TRUE)
716 endswap_int_array (psf->u.ibuf, bufferlen) ;
718 bf2f_array (psf->u.fbuf, bufferlen) ;
720 f2s_array (psf->u.fbuf, readcount, ptr + total, scale) ;
722 if (readcount < bufferlen)
728 } /* replace_read_f2s */
731 replace_read_f2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
732 { int bufferlen, readcount ;
733 sf_count_t total = 0 ;
736 bufferlen = ARRAY_LEN (psf->u.fbuf) ;
737 scale = (psf->float_int_mult == 0) ? 1.0 : 0x7FFF / psf->float_max ;
740 { if (len < bufferlen)
741 bufferlen = (int) len ;
742 readcount = psf_fread (psf->u.fbuf, sizeof (float), bufferlen, psf) ;
744 if (psf->float_endswap == SF_TRUE)
745 endswap_int_array (psf->u.ibuf, bufferlen) ;
747 bf2f_array (psf->u.fbuf, bufferlen) ;
749 f2i_array (psf->u.fbuf, readcount, ptr + total, scale) ;
751 if (readcount < bufferlen)
757 } /* replace_read_f2i */
760 replace_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
761 { int bufferlen, readcount ;
762 sf_count_t total = 0 ;
766 bufferlen = ARRAY_LEN (psf->u.fbuf) ;
769 { if (len < bufferlen)
770 bufferlen = (int) len ;
771 readcount = psf_fread (psf->u.fbuf, sizeof (float), bufferlen, psf) ;
773 if (psf->float_endswap == SF_TRUE)
774 endswap_int_array (psf->u.ibuf, bufferlen) ;
776 bf2f_array (psf->u.fbuf, bufferlen) ;
778 memcpy (ptr + total, psf->u.fbuf, bufferlen * sizeof (float)) ;
781 if (readcount < bufferlen)
787 } /* replace_read_f */
790 replace_read_f2d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
791 { int bufferlen, readcount ;
792 sf_count_t total = 0 ;
794 bufferlen = ARRAY_LEN (psf->u.fbuf) ;
797 { if (len < bufferlen)
798 bufferlen = (int) len ;
799 readcount = psf_fread (psf->u.fbuf, sizeof (float), bufferlen, psf) ;
801 if (psf->float_endswap == SF_TRUE)
802 endswap_int_array (psf->u.ibuf, bufferlen) ;
804 bf2f_array (psf->u.fbuf, bufferlen) ;
806 f2d_array (psf->u.fbuf, readcount, ptr + total) ;
808 if (readcount < bufferlen)
814 } /* replace_read_f2d */
817 replace_write_s2f (SF_PRIVATE *psf, const short *ptr, sf_count_t len)
818 { int bufferlen, writecount ;
819 sf_count_t total = 0 ;
821 bufferlen = ARRAY_LEN (psf->u.fbuf) ;
824 { if (len < bufferlen)
825 bufferlen = (int) len ;
826 s2f_array (ptr + total, psf->u.fbuf, bufferlen) ;
829 float32_peak_update (psf, psf->u.fbuf, bufferlen, total / psf->sf.channels) ;
831 f2bf_array (psf->u.fbuf, bufferlen) ;
833 if (psf->float_endswap == SF_TRUE)
834 endswap_int_array (psf->u.ibuf, bufferlen) ;
836 writecount = psf_fwrite (psf->u.fbuf, sizeof (float), bufferlen, psf) ;
837 total += writecount ;
838 if (writecount < bufferlen)
844 } /* replace_write_s2f */
847 replace_write_i2f (SF_PRIVATE *psf, const int *ptr, sf_count_t len)
848 { int bufferlen, writecount ;
849 sf_count_t total = 0 ;
851 bufferlen = ARRAY_LEN (psf->u.fbuf) ;
854 { if (len < bufferlen)
855 bufferlen = (int) len ;
856 i2f_array (ptr + total, psf->u.fbuf, bufferlen) ;
859 float32_peak_update (psf, psf->u.fbuf, bufferlen, total / psf->sf.channels) ;
861 f2bf_array (psf->u.fbuf, bufferlen) ;
863 if (psf->float_endswap == SF_TRUE)
864 endswap_int_array (psf->u.ibuf, bufferlen) ;
866 writecount = psf_fwrite (psf->u.fbuf, sizeof (float), bufferlen, psf) ;
867 total += writecount ;
868 if (writecount < bufferlen)
874 } /* replace_write_i2f */
877 replace_write_f (SF_PRIVATE *psf, const float *ptr, sf_count_t len)
878 { int bufferlen, writecount ;
879 sf_count_t total = 0 ;
883 float32_peak_update (psf, ptr, len, 0) ;
885 bufferlen = ARRAY_LEN (psf->u.fbuf) ;
888 { if (len < bufferlen)
889 bufferlen = (int) len ;
891 memcpy (psf->u.fbuf, ptr + total, bufferlen * sizeof (float)) ;
893 f2bf_array (psf->u.fbuf, bufferlen) ;
895 if (psf->float_endswap == SF_TRUE)
896 endswap_int_array (psf->u.ibuf, bufferlen) ;
898 writecount = psf_fwrite (psf->u.fbuf, sizeof (float) , bufferlen, psf) ;
899 total += writecount ;
900 if (writecount < bufferlen)
906 } /* replace_write_f */
909 replace_write_d2f (SF_PRIVATE *psf, const double *ptr, sf_count_t len)
910 { int bufferlen, writecount ;
911 sf_count_t total = 0 ;
913 bufferlen = ARRAY_LEN (psf->u.fbuf) ;
916 { if (len < bufferlen)
917 bufferlen = (int) len ;
918 d2f_array (ptr + total, psf->u.fbuf, bufferlen) ;
921 float32_peak_update (psf, psf->u.fbuf, bufferlen, total / psf->sf.channels) ;
923 f2bf_array (psf->u.fbuf, bufferlen) ;
925 if (psf->float_endswap == SF_TRUE)
926 endswap_int_array (psf->u.ibuf, bufferlen) ;
928 writecount = psf_fwrite (psf->u.fbuf, sizeof (float), bufferlen, psf) ;
929 total += writecount ;
930 if (writecount < bufferlen)
936 } /* replace_write_d2f */
938 /*----------------------------------------------------------------------------------------------
942 bf2f_array (float *buffer, int count)
943 { while (--count >= 0)
944 { buffer [count] = FLOAT32_READ ((unsigned char *) (buffer + count)) ;
949 f2bf_array (float *buffer, int count)
950 { while (--count >= 0)
951 { FLOAT32_WRITE (buffer [count], (unsigned char*) (buffer + count)) ;
956 ** Do not edit or modify anything in this comment block.
957 ** The arch-tag line is a file identity tag for the GNU Arch
958 ** revision control system.
960 ** arch-tag: b6c34917-488c-4145-9648-f4371fc4c889