2 ** Copyright (C) 1999-2011 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.
27 /* Need to be able to handle 3 byte (24 bit) integers. So defined a
28 ** type and use SIZEOF_TRIBYTE instead of (tribyte).
31 typedef void tribyte
;
33 #define SIZEOF_TRIBYTE 3
35 static sf_count_t
pcm_read_sc2s (SF_PRIVATE
*psf
, short *ptr
, sf_count_t len
) ;
36 static sf_count_t
pcm_read_uc2s (SF_PRIVATE
*psf
, short *ptr
, sf_count_t len
) ;
37 static sf_count_t
pcm_read_bes2s (SF_PRIVATE
*psf
, short *ptr
, sf_count_t len
) ;
38 static sf_count_t
pcm_read_les2s (SF_PRIVATE
*psf
, short *ptr
, sf_count_t len
) ;
39 static sf_count_t
pcm_read_bet2s (SF_PRIVATE
*psf
, short *ptr
, sf_count_t len
) ;
40 static sf_count_t
pcm_read_let2s (SF_PRIVATE
*psf
, short *ptr
, sf_count_t len
) ;
41 static sf_count_t
pcm_read_bei2s (SF_PRIVATE
*psf
, short *ptr
, sf_count_t len
) ;
42 static sf_count_t
pcm_read_lei2s (SF_PRIVATE
*psf
, short *ptr
, sf_count_t len
) ;
44 static sf_count_t
pcm_read_sc2i (SF_PRIVATE
*psf
, int *ptr
, sf_count_t len
) ;
45 static sf_count_t
pcm_read_uc2i (SF_PRIVATE
*psf
, int *ptr
, sf_count_t len
) ;
46 static sf_count_t
pcm_read_bes2i (SF_PRIVATE
*psf
, int *ptr
, sf_count_t len
) ;
47 static sf_count_t
pcm_read_les2i (SF_PRIVATE
*psf
, int *ptr
, sf_count_t len
) ;
48 static sf_count_t
pcm_read_bet2i (SF_PRIVATE
*psf
, int *ptr
, sf_count_t len
) ;
49 static sf_count_t
pcm_read_let2i (SF_PRIVATE
*psf
, int *ptr
, sf_count_t len
) ;
50 static sf_count_t
pcm_read_bei2i (SF_PRIVATE
*psf
, int *ptr
, sf_count_t len
) ;
51 static sf_count_t
pcm_read_lei2i (SF_PRIVATE
*psf
, int *ptr
, sf_count_t len
) ;
53 static sf_count_t
pcm_read_sc2f (SF_PRIVATE
*psf
, float *ptr
, sf_count_t len
) ;
54 static sf_count_t
pcm_read_uc2f (SF_PRIVATE
*psf
, float *ptr
, sf_count_t len
) ;
55 static sf_count_t
pcm_read_bes2f (SF_PRIVATE
*psf
, float *ptr
, sf_count_t len
) ;
56 static sf_count_t
pcm_read_les2f (SF_PRIVATE
*psf
, float *ptr
, sf_count_t len
) ;
57 static sf_count_t
pcm_read_bet2f (SF_PRIVATE
*psf
, float *ptr
, sf_count_t len
) ;
58 static sf_count_t
pcm_read_let2f (SF_PRIVATE
*psf
, float *ptr
, sf_count_t len
) ;
59 static sf_count_t
pcm_read_bei2f (SF_PRIVATE
*psf
, float *ptr
, sf_count_t len
) ;
60 static sf_count_t
pcm_read_lei2f (SF_PRIVATE
*psf
, float *ptr
, sf_count_t len
) ;
62 static sf_count_t
pcm_read_sc2d (SF_PRIVATE
*psf
, double *ptr
, sf_count_t len
) ;
63 static sf_count_t
pcm_read_uc2d (SF_PRIVATE
*psf
, double *ptr
, sf_count_t len
) ;
64 static sf_count_t
pcm_read_bes2d (SF_PRIVATE
*psf
, double *ptr
, sf_count_t len
) ;
65 static sf_count_t
pcm_read_les2d (SF_PRIVATE
*psf
, double *ptr
, sf_count_t len
) ;
66 static sf_count_t
pcm_read_bet2d (SF_PRIVATE
*psf
, double *ptr
, sf_count_t len
) ;
67 static sf_count_t
pcm_read_let2d (SF_PRIVATE
*psf
, double *ptr
, sf_count_t len
) ;
68 static sf_count_t
pcm_read_bei2d (SF_PRIVATE
*psf
, double *ptr
, sf_count_t len
) ;
69 static sf_count_t
pcm_read_lei2d (SF_PRIVATE
*psf
, double *ptr
, sf_count_t len
) ;
71 static sf_count_t
pcm_write_s2sc (SF_PRIVATE
*psf
, const short *ptr
, sf_count_t len
) ;
72 static sf_count_t
pcm_write_s2uc (SF_PRIVATE
*psf
, const short *ptr
, sf_count_t len
) ;
73 static sf_count_t
pcm_write_s2bes (SF_PRIVATE
*psf
, const short *ptr
, sf_count_t len
) ;
74 static sf_count_t
pcm_write_s2les (SF_PRIVATE
*psf
, const short *ptr
, sf_count_t len
) ;
75 static sf_count_t
pcm_write_s2bet (SF_PRIVATE
*psf
, const short *ptr
, sf_count_t len
) ;
76 static sf_count_t
pcm_write_s2let (SF_PRIVATE
*psf
, const short *ptr
, sf_count_t len
) ;
77 static sf_count_t
pcm_write_s2bei (SF_PRIVATE
*psf
, const short *ptr
, sf_count_t len
) ;
78 static sf_count_t
pcm_write_s2lei (SF_PRIVATE
*psf
, const short *ptr
, sf_count_t len
) ;
80 static sf_count_t
pcm_write_i2sc (SF_PRIVATE
*psf
, const int *ptr
, sf_count_t len
) ;
81 static sf_count_t
pcm_write_i2uc (SF_PRIVATE
*psf
, const int *ptr
, sf_count_t len
) ;
82 static sf_count_t
pcm_write_i2bes (SF_PRIVATE
*psf
, const int *ptr
, sf_count_t len
) ;
83 static sf_count_t
pcm_write_i2les (SF_PRIVATE
*psf
, const int *ptr
, sf_count_t len
) ;
84 static sf_count_t
pcm_write_i2bet (SF_PRIVATE
*psf
, const int *ptr
, sf_count_t len
) ;
85 static sf_count_t
pcm_write_i2let (SF_PRIVATE
*psf
, const int *ptr
, sf_count_t len
) ;
86 static sf_count_t
pcm_write_i2bei (SF_PRIVATE
*psf
, const int *ptr
, sf_count_t len
) ;
87 static sf_count_t
pcm_write_i2lei (SF_PRIVATE
*psf
, const int *ptr
, sf_count_t len
) ;
89 static sf_count_t
pcm_write_f2sc (SF_PRIVATE
*psf
, const float *ptr
, sf_count_t len
) ;
90 static sf_count_t
pcm_write_f2uc (SF_PRIVATE
*psf
, const float *ptr
, sf_count_t len
) ;
91 static sf_count_t
pcm_write_f2bes (SF_PRIVATE
*psf
, const float *ptr
, sf_count_t len
) ;
92 static sf_count_t
pcm_write_f2les (SF_PRIVATE
*psf
, const float *ptr
, sf_count_t len
) ;
93 static sf_count_t
pcm_write_f2bet (SF_PRIVATE
*psf
, const float *ptr
, sf_count_t len
) ;
94 static sf_count_t
pcm_write_f2let (SF_PRIVATE
*psf
, const float *ptr
, sf_count_t len
) ;
95 static sf_count_t
pcm_write_f2bei (SF_PRIVATE
*psf
, const float *ptr
, sf_count_t len
) ;
96 static sf_count_t
pcm_write_f2lei (SF_PRIVATE
*psf
, const float *ptr
, sf_count_t len
) ;
98 static sf_count_t
pcm_write_d2sc (SF_PRIVATE
*psf
, const double *ptr
, sf_count_t len
) ;
99 static sf_count_t
pcm_write_d2uc (SF_PRIVATE
*psf
, const double *ptr
, sf_count_t len
) ;
100 static sf_count_t
pcm_write_d2bes (SF_PRIVATE
*psf
, const double *ptr
, sf_count_t len
) ;
101 static sf_count_t
pcm_write_d2les (SF_PRIVATE
*psf
, const double *ptr
, sf_count_t len
) ;
102 static sf_count_t
pcm_write_d2bet (SF_PRIVATE
*psf
, const double *ptr
, sf_count_t len
) ;
103 static sf_count_t
pcm_write_d2let (SF_PRIVATE
*psf
, const double *ptr
, sf_count_t len
) ;
104 static sf_count_t
pcm_write_d2bei (SF_PRIVATE
*psf
, const double *ptr
, sf_count_t len
) ;
105 static sf_count_t
pcm_write_d2lei (SF_PRIVATE
*psf
, const double *ptr
, sf_count_t len
) ;
107 /*-----------------------------------------------------------------------------------------------
111 { /* Char type for 8 bit files. */
112 SF_CHARS_SIGNED
= 200,
113 SF_CHARS_UNSIGNED
= 201
116 /*-----------------------------------------------------------------------------------------------
120 pcm_init (SF_PRIVATE
*psf
)
123 if (psf
->bytewidth
== 0 || psf
->sf
.channels
== 0)
124 { psf_log_printf (psf
, "pcm_init : internal error : bytewitdh = %d, channels = %d\n", psf
->bytewidth
, psf
->sf
.channels
) ;
125 return SFE_INTERNAL
;
128 psf
->blockwidth
= psf
->bytewidth
* psf
->sf
.channels
;
130 if ((SF_CODEC (psf
->sf
.format
)) == SF_FORMAT_PCM_S8
)
131 chars
= SF_CHARS_SIGNED
;
132 else if ((SF_CODEC (psf
->sf
.format
)) == SF_FORMAT_PCM_U8
)
133 chars
= SF_CHARS_UNSIGNED
;
135 if (CPU_IS_BIG_ENDIAN
)
136 psf
->data_endswap
= (psf
->endian
== SF_ENDIAN_BIG
) ? SF_FALSE
: SF_TRUE
;
138 psf
->data_endswap
= (psf
->endian
== SF_ENDIAN_LITTLE
) ? SF_FALSE
: SF_TRUE
;
140 if (psf
->file
.mode
== SFM_READ
|| psf
->file
.mode
== SFM_RDWR
)
141 { switch (psf
->bytewidth
* 0x10000 + psf
->endian
+ chars
)
142 { case (0x10000 + SF_ENDIAN_BIG
+ SF_CHARS_SIGNED
) :
143 case (0x10000 + SF_ENDIAN_LITTLE
+ SF_CHARS_SIGNED
) :
144 psf
->read_short
= pcm_read_sc2s
;
145 psf
->read_int
= pcm_read_sc2i
;
146 psf
->read_float
= pcm_read_sc2f
;
147 psf
->read_double
= pcm_read_sc2d
;
149 case (0x10000 + SF_ENDIAN_BIG
+ SF_CHARS_UNSIGNED
) :
150 case (0x10000 + SF_ENDIAN_LITTLE
+ SF_CHARS_UNSIGNED
) :
151 psf
->read_short
= pcm_read_uc2s
;
152 psf
->read_int
= pcm_read_uc2i
;
153 psf
->read_float
= pcm_read_uc2f
;
154 psf
->read_double
= pcm_read_uc2d
;
157 case (2 * 0x10000 + SF_ENDIAN_BIG
) :
158 psf
->read_short
= pcm_read_bes2s
;
159 psf
->read_int
= pcm_read_bes2i
;
160 psf
->read_float
= pcm_read_bes2f
;
161 psf
->read_double
= pcm_read_bes2d
;
163 case (3 * 0x10000 + SF_ENDIAN_BIG
) :
164 psf
->read_short
= pcm_read_bet2s
;
165 psf
->read_int
= pcm_read_bet2i
;
166 psf
->read_float
= pcm_read_bet2f
;
167 psf
->read_double
= pcm_read_bet2d
;
169 case (4 * 0x10000 + SF_ENDIAN_BIG
) :
171 psf
->read_short
= pcm_read_bei2s
;
172 psf
->read_int
= pcm_read_bei2i
;
173 psf
->read_float
= pcm_read_bei2f
;
174 psf
->read_double
= pcm_read_bei2d
;
177 case (2 * 0x10000 + SF_ENDIAN_LITTLE
) :
178 psf
->read_short
= pcm_read_les2s
;
179 psf
->read_int
= pcm_read_les2i
;
180 psf
->read_float
= pcm_read_les2f
;
181 psf
->read_double
= pcm_read_les2d
;
183 case (3 * 0x10000 + SF_ENDIAN_LITTLE
) :
184 psf
->read_short
= pcm_read_let2s
;
185 psf
->read_int
= pcm_read_let2i
;
186 psf
->read_float
= pcm_read_let2f
;
187 psf
->read_double
= pcm_read_let2d
;
189 case (4 * 0x10000 + SF_ENDIAN_LITTLE
) :
190 psf
->read_short
= pcm_read_lei2s
;
191 psf
->read_int
= pcm_read_lei2i
;
192 psf
->read_float
= pcm_read_lei2f
;
193 psf
->read_double
= pcm_read_lei2d
;
196 psf_log_printf (psf
, "pcm.c returning SFE_UNIMPLEMENTED\nbytewidth %d endian %d\n", psf
->bytewidth
, psf
->endian
) ;
197 return SFE_UNIMPLEMENTED
;
201 if (psf
->file
.mode
== SFM_WRITE
|| psf
->file
.mode
== SFM_RDWR
)
202 { switch (psf
->bytewidth
* 0x10000 + psf
->endian
+ chars
)
203 { case (0x10000 + SF_ENDIAN_BIG
+ SF_CHARS_SIGNED
) :
204 case (0x10000 + SF_ENDIAN_LITTLE
+ SF_CHARS_SIGNED
) :
205 psf
->write_short
= pcm_write_s2sc
;
206 psf
->write_int
= pcm_write_i2sc
;
207 psf
->write_float
= pcm_write_f2sc
;
208 psf
->write_double
= pcm_write_d2sc
;
210 case (0x10000 + SF_ENDIAN_BIG
+ SF_CHARS_UNSIGNED
) :
211 case (0x10000 + SF_ENDIAN_LITTLE
+ SF_CHARS_UNSIGNED
) :
212 psf
->write_short
= pcm_write_s2uc
;
213 psf
->write_int
= pcm_write_i2uc
;
214 psf
->write_float
= pcm_write_f2uc
;
215 psf
->write_double
= pcm_write_d2uc
;
218 case (2 * 0x10000 + SF_ENDIAN_BIG
) :
219 psf
->write_short
= pcm_write_s2bes
;
220 psf
->write_int
= pcm_write_i2bes
;
221 psf
->write_float
= pcm_write_f2bes
;
222 psf
->write_double
= pcm_write_d2bes
;
225 case (3 * 0x10000 + SF_ENDIAN_BIG
) :
226 psf
->write_short
= pcm_write_s2bet
;
227 psf
->write_int
= pcm_write_i2bet
;
228 psf
->write_float
= pcm_write_f2bet
;
229 psf
->write_double
= pcm_write_d2bet
;
232 case (4 * 0x10000 + SF_ENDIAN_BIG
) :
233 psf
->write_short
= pcm_write_s2bei
;
234 psf
->write_int
= pcm_write_i2bei
;
235 psf
->write_float
= pcm_write_f2bei
;
236 psf
->write_double
= pcm_write_d2bei
;
239 case (2 * 0x10000 + SF_ENDIAN_LITTLE
) :
240 psf
->write_short
= pcm_write_s2les
;
241 psf
->write_int
= pcm_write_i2les
;
242 psf
->write_float
= pcm_write_f2les
;
243 psf
->write_double
= pcm_write_d2les
;
246 case (3 * 0x10000 + SF_ENDIAN_LITTLE
) :
247 psf
->write_short
= pcm_write_s2let
;
248 psf
->write_int
= pcm_write_i2let
;
249 psf
->write_float
= pcm_write_f2let
;
250 psf
->write_double
= pcm_write_d2let
;
253 case (4 * 0x10000 + SF_ENDIAN_LITTLE
) :
254 psf
->write_short
= pcm_write_s2lei
;
255 psf
->write_int
= pcm_write_i2lei
;
256 psf
->write_float
= pcm_write_f2lei
;
257 psf
->write_double
= pcm_write_d2lei
;
261 psf_log_printf (psf
, "pcm.c returning SFE_UNIMPLEMENTED\nbytewidth %s endian %d\n", psf
->bytewidth
, psf
->endian
) ;
262 return SFE_UNIMPLEMENTED
;
267 if (psf
->filelength
> psf
->dataoffset
)
268 { psf
->datalength
= (psf
->dataend
> 0) ? psf
->dataend
- psf
->dataoffset
:
269 psf
->filelength
- psf
->dataoffset
;
272 psf
->datalength
= 0 ;
274 psf
->sf
.frames
= psf
->blockwidth
> 0 ? psf
->datalength
/ psf
->blockwidth
: 0 ;
279 /*==============================================================================
283 sc2s_array (signed char *src
, int count
, short *dest
)
284 { while (--count
>= 0)
285 { dest
[count
] = src
[count
] << 8 ;
290 uc2s_array (unsigned char *src
, int count
, short *dest
)
291 { while (--count
>= 0)
292 { dest
[count
] = (((short) src
[count
]) - 0x80) << 8 ;
297 let2s_array (tribyte
*src
, int count
, short *dest
)
298 { unsigned char *ucptr
;
300 ucptr
= ((unsigned char*) src
) + 3 * count
;
303 dest
[count
] = LET2H_SHORT_PTR (ucptr
) ;
308 bet2s_array (tribyte
*src
, int count
, short *dest
)
309 { unsigned char *ucptr
;
311 ucptr
= ((unsigned char*) src
) + 3 * count
;
314 dest
[count
] = BET2H_SHORT_PTR (ucptr
) ;
319 lei2s_array (int *src
, int count
, short *dest
)
323 { value
= LEI2H_INT (src
[count
]) ;
324 dest
[count
] = value
>> 16 ;
329 bei2s_array (int *src
, int count
, short *dest
)
333 { value
= BEI2H_INT (src
[count
]) ;
334 dest
[count
] = value
>> 16 ;
338 /*--------------------------------------------------------------------------
342 sc2i_array (signed char *src
, int count
, int *dest
)
343 { while (--count
>= 0)
344 { dest
[count
] = ((int) src
[count
]) << 24 ;
349 uc2i_array (unsigned char *src
, int count
, int *dest
)
350 { while (--count
>= 0)
351 { dest
[count
] = (((int) src
[count
]) - 128) << 24 ;
356 bes2i_array (short *src
, int count
, int *dest
)
360 { value
= BES2H_SHORT (src
[count
]) ;
361 dest
[count
] = value
<< 16 ;
366 les2i_array (short *src
, int count
, int *dest
)
370 { value
= LES2H_SHORT (src
[count
]) ;
371 dest
[count
] = value
<< 16 ;
376 bet2i_array (tribyte
*src
, int count
, int *dest
)
377 { unsigned char *ucptr
;
379 ucptr
= ((unsigned char*) src
) + 3 * count
;
382 dest
[count
] = BET2H_INT_PTR (ucptr
) ;
387 let2i_array (tribyte
*src
, int count
, int *dest
)
388 { unsigned char *ucptr
;
390 ucptr
= ((unsigned char*) src
) + 3 * count
;
393 dest
[count
] = LET2H_INT_PTR (ucptr
) ;
397 /*--------------------------------------------------------------------------
401 sc2f_array (signed char *src
, int count
, float *dest
, float normfact
)
402 { while (--count
>= 0)
403 dest
[count
] = ((float) src
[count
]) * normfact
;
407 uc2f_array (unsigned char *src
, int count
, float *dest
, float normfact
)
408 { while (--count
>= 0)
409 dest
[count
] = (((int) src
[count
]) - 128) * normfact
;
413 les2f_array (short *src
, int count
, float *dest
, float normfact
)
417 { value
= src
[count
] ;
418 value
= LES2H_SHORT (value
) ;
419 dest
[count
] = ((float) value
) * normfact
;
424 bes2f_array (short *src
, int count
, float *dest
, float normfact
)
428 { value
= src
[count
] ;
429 value
= BES2H_SHORT (value
) ;
430 dest
[count
] = ((float) value
) * normfact
;
435 let2f_array (tribyte
*src
, int count
, float *dest
, float normfact
)
436 { unsigned char *ucptr
;
439 ucptr
= ((unsigned char*) src
) + 3 * count
;
442 value
= LET2H_INT_PTR (ucptr
) ;
443 dest
[count
] = ((float) value
) * normfact
;
448 bet2f_array (tribyte
*src
, int count
, float *dest
, float normfact
)
449 { unsigned char *ucptr
;
452 ucptr
= ((unsigned char*) src
) + 3 * count
;
455 value
= BET2H_INT_PTR (ucptr
) ;
456 dest
[count
] = ((float) value
) * normfact
;
461 lei2f_array (int *src
, int count
, float *dest
, float normfact
)
465 { value
= src
[count
] ;
466 value
= LEI2H_INT (value
) ;
467 dest
[count
] = ((float) value
) * normfact
;
472 bei2f_array (int *src
, int count
, float *dest
, float normfact
)
476 { value
= src
[count
] ;
477 value
= BEI2H_INT (value
) ;
478 dest
[count
] = ((float) value
) * normfact
;
482 /*--------------------------------------------------------------------------
486 sc2d_array (signed char *src
, int count
, double *dest
, double normfact
)
487 { while (--count
>= 0)
488 dest
[count
] = ((double) src
[count
]) * normfact
;
492 uc2d_array (unsigned char *src
, int count
, double *dest
, double normfact
)
493 { while (--count
>= 0)
494 dest
[count
] = (((int) src
[count
]) - 128) * normfact
;
498 les2d_array (short *src
, int count
, double *dest
, double normfact
)
502 { value
= src
[count
] ;
503 value
= LES2H_SHORT (value
) ;
504 dest
[count
] = ((double) value
) * normfact
;
509 bes2d_array (short *src
, int count
, double *dest
, double normfact
)
513 { value
= src
[count
] ;
514 value
= BES2H_SHORT (value
) ;
515 dest
[count
] = ((double) value
) * normfact
;
520 let2d_array (tribyte
*src
, int count
, double *dest
, double normfact
)
521 { unsigned char *ucptr
;
524 ucptr
= ((unsigned char*) src
) + 3 * count
;
527 value
= LET2H_INT_PTR (ucptr
) ;
528 dest
[count
] = ((double) value
) * normfact
;
533 bet2d_array (tribyte
*src
, int count
, double *dest
, double normfact
)
534 { unsigned char *ucptr
;
537 ucptr
= ((unsigned char*) src
) + 3 * count
;
540 value
= (ucptr
[0] << 24) | (ucptr
[1] << 16) | (ucptr
[2] << 8) ;
541 dest
[count
] = ((double) value
) * normfact
;
546 lei2d_array (int *src
, int count
, double *dest
, double normfact
)
550 { value
= src
[count
] ;
551 value
= LEI2H_INT (value
) ;
552 dest
[count
] = ((double) value
) * normfact
;
557 bei2d_array (int *src
, int count
, double *dest
, double normfact
)
561 { value
= src
[count
] ;
562 value
= BEI2H_INT (value
) ;
563 dest
[count
] = ((double) value
) * normfact
;
567 /*--------------------------------------------------------------------------
571 s2sc_array (const short *src
, signed char *dest
, int count
)
572 { while (--count
>= 0)
573 dest
[count
] = src
[count
] >> 8 ;
577 s2uc_array (const short *src
, unsigned char *dest
, int count
)
578 { while (--count
>= 0)
579 dest
[count
] = (src
[count
] >> 8) + 0x80 ;
583 s2let_array (const short *src
, tribyte
*dest
, int count
)
584 { unsigned char *ucptr
;
586 ucptr
= ((unsigned char*) dest
) + 3 * count
;
590 ucptr
[1] = src
[count
] ;
591 ucptr
[2] = src
[count
] >> 8 ;
596 s2bet_array (const short *src
, tribyte
*dest
, int count
)
597 { unsigned char *ucptr
;
599 ucptr
= ((unsigned char*) dest
) + 3 * count
;
603 ucptr
[1] = src
[count
] ;
604 ucptr
[0] = src
[count
] >> 8 ;
609 s2lei_array (const short *src
, int *dest
, int count
)
610 { unsigned char *ucptr
;
612 ucptr
= ((unsigned char*) dest
) + 4 * count
;
617 ucptr
[2] = src
[count
] ;
618 ucptr
[3] = src
[count
] >> 8 ;
623 s2bei_array (const short *src
, int *dest
, int count
)
624 { unsigned char *ucptr
;
626 ucptr
= ((unsigned char*) dest
) + 4 * count
;
629 ucptr
[0] = src
[count
] >> 8 ;
630 ucptr
[1] = src
[count
] ;
636 /*--------------------------------------------------------------------------
640 i2sc_array (const int *src
, signed char *dest
, int count
)
641 { while (--count
>= 0)
642 dest
[count
] = (src
[count
] >> 24) ;
646 i2uc_array (const int *src
, unsigned char *dest
, int count
)
647 { while (--count
>= 0)
648 dest
[count
] = ((src
[count
] >> 24) + 128) ;
652 i2bes_array (const int *src
, short *dest
, int count
)
653 { unsigned char *ucptr
;
655 ucptr
= ((unsigned char*) dest
) + 2 * count
;
658 ucptr
[0] = src
[count
] >> 24 ;
659 ucptr
[1] = src
[count
] >> 16 ;
664 i2les_array (const int *src
, short *dest
, int count
)
665 { unsigned char *ucptr
;
667 ucptr
= ((unsigned char*) dest
) + 2 * count
;
670 ucptr
[0] = src
[count
] >> 16 ;
671 ucptr
[1] = src
[count
] >> 24 ;
676 i2let_array (const int *src
, tribyte
*dest
, int count
)
677 { unsigned char *ucptr
;
680 ucptr
= ((unsigned char*) dest
) + 3 * count
;
683 value
= src
[count
] >> 8 ;
685 ucptr
[1] = value
>> 8 ;
686 ucptr
[2] = value
>> 16 ;
691 i2bet_array (const int *src
, tribyte
*dest
, int count
)
692 { unsigned char *ucptr
;
695 ucptr
= ((unsigned char*) dest
) + 3 * count
;
698 value
= src
[count
] >> 8 ;
700 ucptr
[1] = value
>> 8 ;
701 ucptr
[0] = value
>> 16 ;
705 /*===============================================================================================
709 pcm_read_sc2s (SF_PRIVATE
*psf
, short *ptr
, sf_count_t len
)
710 { int bufferlen
, readcount
;
711 sf_count_t total
= 0 ;
713 bufferlen
= ARRAY_LEN (psf
->u
.scbuf
) ;
716 { if (len
< bufferlen
)
717 bufferlen
= (int) len
;
718 readcount
= psf_fread (psf
->u
.scbuf
, sizeof (signed char), bufferlen
, psf
) ;
719 sc2s_array (psf
->u
.scbuf
, readcount
, ptr
+ total
) ;
721 if (readcount
< bufferlen
)
727 } /* pcm_read_sc2s */
730 pcm_read_uc2s (SF_PRIVATE
*psf
, short *ptr
, sf_count_t len
)
731 { int bufferlen
, readcount
;
732 sf_count_t total
= 0 ;
734 bufferlen
= ARRAY_LEN (psf
->u
.ucbuf
) ;
737 { if (len
< bufferlen
)
738 bufferlen
= (int) len
;
739 readcount
= psf_fread (psf
->u
.ucbuf
, sizeof (unsigned char), bufferlen
, psf
) ;
740 uc2s_array (psf
->u
.ucbuf
, readcount
, ptr
+ total
) ;
742 if (readcount
< bufferlen
)
748 } /* pcm_read_uc2s */
751 pcm_read_bes2s (SF_PRIVATE
*psf
, short *ptr
, sf_count_t len
)
754 total
= psf_fread (ptr
, sizeof (short), len
, psf
) ;
755 if (CPU_IS_LITTLE_ENDIAN
)
756 endswap_short_array (ptr
, len
) ;
759 } /* pcm_read_bes2s */
762 pcm_read_les2s (SF_PRIVATE
*psf
, short *ptr
, sf_count_t len
)
765 total
= psf_fread (ptr
, sizeof (short), len
, psf
) ;
766 if (CPU_IS_BIG_ENDIAN
)
767 endswap_short_array (ptr
, len
) ;
770 } /* pcm_read_les2s */
773 pcm_read_bet2s (SF_PRIVATE
*psf
, short *ptr
, sf_count_t len
)
774 { int bufferlen
, readcount
;
775 sf_count_t total
= 0 ;
777 bufferlen
= sizeof (psf
->u
.ucbuf
) / SIZEOF_TRIBYTE
;
780 { if (len
< bufferlen
)
781 bufferlen
= (int) len
;
782 readcount
= psf_fread (psf
->u
.ucbuf
, SIZEOF_TRIBYTE
, bufferlen
, psf
) ;
783 bet2s_array ((tribyte
*) (psf
->u
.ucbuf
), readcount
, ptr
+ total
) ;
785 if (readcount
< bufferlen
)
791 } /* pcm_read_bet2s */
794 pcm_read_let2s (SF_PRIVATE
*psf
, short *ptr
, sf_count_t len
)
795 { int bufferlen
, readcount
;
796 sf_count_t total
= 0 ;
798 bufferlen
= sizeof (psf
->u
.ucbuf
) / SIZEOF_TRIBYTE
;
801 { if (len
< bufferlen
)
802 bufferlen
= (int) len
;
803 readcount
= psf_fread (psf
->u
.ucbuf
, SIZEOF_TRIBYTE
, bufferlen
, psf
) ;
804 let2s_array ((tribyte
*) (psf
->u
.ucbuf
), readcount
, ptr
+ total
) ;
806 if (readcount
< bufferlen
)
812 } /* pcm_read_let2s */
815 pcm_read_bei2s (SF_PRIVATE
*psf
, short *ptr
, sf_count_t len
)
816 { int bufferlen
, readcount
;
817 sf_count_t total
= 0 ;
819 bufferlen
= ARRAY_LEN (psf
->u
.ibuf
) ;
822 { if (len
< bufferlen
)
823 bufferlen
= (int) len
;
824 readcount
= psf_fread (psf
->u
.ibuf
, sizeof (int), bufferlen
, psf
) ;
825 bei2s_array (psf
->u
.ibuf
, readcount
, ptr
+ total
) ;
827 if (readcount
< bufferlen
)
833 } /* pcm_read_bei2s */
836 pcm_read_lei2s (SF_PRIVATE
*psf
, short *ptr
, sf_count_t len
)
837 { int bufferlen
, readcount
;
838 sf_count_t total
= 0 ;
840 bufferlen
= ARRAY_LEN (psf
->u
.ibuf
) ;
843 { if (len
< bufferlen
)
844 bufferlen
= (int) len
;
845 readcount
= psf_fread (psf
->u
.ibuf
, sizeof (int), bufferlen
, psf
) ;
846 lei2s_array (psf
->u
.ibuf
, readcount
, ptr
+ total
) ;
848 if (readcount
< bufferlen
)
854 } /* pcm_read_lei2s */
856 /*-----------------------------------------------------------------------------------------------
860 pcm_read_sc2i (SF_PRIVATE
*psf
, int *ptr
, sf_count_t len
)
861 { int bufferlen
, readcount
;
862 sf_count_t total
= 0 ;
864 bufferlen
= ARRAY_LEN (psf
->u
.scbuf
) ;
867 { if (len
< bufferlen
)
868 bufferlen
= (int) len
;
869 readcount
= psf_fread (psf
->u
.scbuf
, sizeof (signed char), bufferlen
, psf
) ;
870 sc2i_array (psf
->u
.scbuf
, readcount
, ptr
+ total
) ;
872 if (readcount
< bufferlen
)
878 } /* pcm_read_sc2i */
881 pcm_read_uc2i (SF_PRIVATE
*psf
, int *ptr
, sf_count_t len
)
882 { int bufferlen
, readcount
;
883 sf_count_t total
= 0 ;
885 bufferlen
= ARRAY_LEN (psf
->u
.ucbuf
) ;
888 { if (len
< bufferlen
)
889 bufferlen
= (int) len
;
890 readcount
= psf_fread (psf
->u
.ucbuf
, sizeof (unsigned char), bufferlen
, psf
) ;
891 uc2i_array (psf
->u
.ucbuf
, readcount
, ptr
+ total
) ;
893 if (readcount
< bufferlen
)
899 } /* pcm_read_uc2i */
902 pcm_read_bes2i (SF_PRIVATE
*psf
, int *ptr
, sf_count_t len
)
903 { int bufferlen
, readcount
;
904 sf_count_t total
= 0 ;
906 bufferlen
= ARRAY_LEN (psf
->u
.sbuf
) ;
909 { if (len
< bufferlen
)
910 bufferlen
= (int) len
;
911 readcount
= psf_fread (psf
->u
.sbuf
, sizeof (short), bufferlen
, psf
) ;
912 bes2i_array (psf
->u
.sbuf
, readcount
, ptr
+ total
) ;
914 if (readcount
< bufferlen
)
920 } /* pcm_read_bes2i */
923 pcm_read_les2i (SF_PRIVATE
*psf
, int *ptr
, sf_count_t len
)
924 { int bufferlen
, readcount
;
925 sf_count_t total
= 0 ;
927 bufferlen
= ARRAY_LEN (psf
->u
.sbuf
) ;
930 { if (len
< bufferlen
)
931 bufferlen
= (int) len
;
932 readcount
= psf_fread (psf
->u
.sbuf
, sizeof (short), bufferlen
, psf
) ;
933 les2i_array (psf
->u
.sbuf
, readcount
, ptr
+ total
) ;
935 if (readcount
< bufferlen
)
941 } /* pcm_read_les2i */
944 pcm_read_bet2i (SF_PRIVATE
*psf
, int *ptr
, sf_count_t len
)
945 { int bufferlen
, readcount
;
946 sf_count_t total
= 0 ;
948 bufferlen
= sizeof (psf
->u
.ucbuf
) / SIZEOF_TRIBYTE
;
951 { if (len
< bufferlen
)
952 bufferlen
= (int) len
;
953 readcount
= psf_fread (psf
->u
.ucbuf
, SIZEOF_TRIBYTE
, bufferlen
, psf
) ;
954 bet2i_array ((tribyte
*) (psf
->u
.ucbuf
), readcount
, ptr
+ total
) ;
956 if (readcount
< bufferlen
)
962 } /* pcm_read_bet2i */
965 pcm_read_let2i (SF_PRIVATE
*psf
, int *ptr
, sf_count_t len
)
966 { int bufferlen
, readcount
;
967 sf_count_t total
= 0 ;
969 bufferlen
= sizeof (psf
->u
.ucbuf
) / SIZEOF_TRIBYTE
;
972 { if (len
< bufferlen
)
973 bufferlen
= (int) len
;
974 readcount
= psf_fread (psf
->u
.ucbuf
, SIZEOF_TRIBYTE
, bufferlen
, psf
) ;
975 let2i_array ((tribyte
*) (psf
->u
.ucbuf
), readcount
, ptr
+ total
) ;
977 if (readcount
< bufferlen
)
983 } /* pcm_read_let2i */
986 pcm_read_bei2i (SF_PRIVATE
*psf
, int *ptr
, sf_count_t len
)
989 total
= psf_fread (ptr
, sizeof (int), len
, psf
) ;
990 if (CPU_IS_LITTLE_ENDIAN
)
991 endswap_int_array (ptr
, len
) ;
994 } /* pcm_read_bei2i */
997 pcm_read_lei2i (SF_PRIVATE
*psf
, int *ptr
, sf_count_t len
)
1000 total
= psf_fread (ptr
, sizeof (int), len
, psf
) ;
1001 if (CPU_IS_BIG_ENDIAN
)
1002 endswap_int_array (ptr
, len
) ;
1005 } /* pcm_read_lei2i */
1007 /*-----------------------------------------------------------------------------------------------
1011 pcm_read_sc2f (SF_PRIVATE
*psf
, float *ptr
, sf_count_t len
)
1012 { int bufferlen
, readcount
;
1013 sf_count_t total
= 0 ;
1016 normfact
= (psf
->norm_float
== SF_TRUE
) ? 1.0 / ((float) 0x80) : 1.0 ;
1018 bufferlen
= ARRAY_LEN (psf
->u
.scbuf
) ;
1021 { if (len
< bufferlen
)
1022 bufferlen
= (int) len
;
1023 readcount
= psf_fread (psf
->u
.scbuf
, sizeof (signed char), bufferlen
, psf
) ;
1024 sc2f_array (psf
->u
.scbuf
, readcount
, ptr
+ total
, normfact
) ;
1025 total
+= readcount
;
1026 if (readcount
< bufferlen
)
1032 } /* pcm_read_sc2f */
1035 pcm_read_uc2f (SF_PRIVATE
*psf
, float *ptr
, sf_count_t len
)
1036 { int bufferlen
, readcount
;
1037 sf_count_t total
= 0 ;
1040 normfact
= (psf
->norm_float
== SF_TRUE
) ? 1.0 / ((float) 0x80) : 1.0 ;
1042 bufferlen
= ARRAY_LEN (psf
->u
.ucbuf
) ;
1045 { if (len
< bufferlen
)
1046 bufferlen
= (int) len
;
1047 readcount
= psf_fread (psf
->u
.ucbuf
, sizeof (unsigned char), bufferlen
, psf
) ;
1048 uc2f_array (psf
->u
.ucbuf
, readcount
, ptr
+ total
, normfact
) ;
1049 total
+= readcount
;
1050 if (readcount
< bufferlen
)
1056 } /* pcm_read_uc2f */
1059 pcm_read_bes2f (SF_PRIVATE
*psf
, float *ptr
, sf_count_t len
)
1060 { int bufferlen
, readcount
;
1061 sf_count_t total
= 0 ;
1064 normfact
= (psf
->norm_float
== SF_TRUE
) ? 1.0 / ((float) 0x8000) : 1.0 ;
1066 bufferlen
= ARRAY_LEN (psf
->u
.sbuf
) ;
1069 { if (len
< bufferlen
)
1070 bufferlen
= (int) len
;
1071 readcount
= psf_fread (psf
->u
.sbuf
, sizeof (short), bufferlen
, psf
) ;
1072 bes2f_array (psf
->u
.sbuf
, readcount
, ptr
+ total
, normfact
) ;
1073 total
+= readcount
;
1074 if (readcount
< bufferlen
)
1080 } /* pcm_read_bes2f */
1083 pcm_read_les2f (SF_PRIVATE
*psf
, float *ptr
, sf_count_t len
)
1084 { int bufferlen
, readcount
;
1085 sf_count_t total
= 0 ;
1088 normfact
= (psf
->norm_float
== SF_TRUE
) ? 1.0 / ((float) 0x8000) : 1.0 ;
1090 bufferlen
= ARRAY_LEN (psf
->u
.sbuf
) ;
1093 { if (len
< bufferlen
)
1094 bufferlen
= (int) len
;
1095 readcount
= psf_fread (psf
->u
.sbuf
, sizeof (short), bufferlen
, psf
) ;
1096 les2f_array (psf
->u
.sbuf
, readcount
, ptr
+ total
, normfact
) ;
1097 total
+= readcount
;
1098 if (readcount
< bufferlen
)
1104 } /* pcm_read_les2f */
1107 pcm_read_bet2f (SF_PRIVATE
*psf
, float *ptr
, sf_count_t len
)
1108 { int bufferlen
, readcount
;
1109 sf_count_t total
= 0 ;
1112 /* Special normfactor because tribyte value is read into an int. */
1113 normfact
= (psf
->norm_float
== SF_TRUE
) ? 1.0 / ((float) 0x80000000) : 1.0 / 256.0 ;
1115 bufferlen
= sizeof (psf
->u
.ucbuf
) / SIZEOF_TRIBYTE
;
1118 { if (len
< bufferlen
)
1119 bufferlen
= (int) len
;
1120 readcount
= psf_fread (psf
->u
.ucbuf
, SIZEOF_TRIBYTE
, bufferlen
, psf
) ;
1121 bet2f_array ((tribyte
*) (psf
->u
.ucbuf
), readcount
, ptr
+ total
, normfact
) ;
1122 total
+= readcount
;
1123 if (readcount
< bufferlen
)
1129 } /* pcm_read_bet2f */
1132 pcm_read_let2f (SF_PRIVATE
*psf
, float *ptr
, sf_count_t len
)
1133 { int bufferlen
, readcount
;
1134 sf_count_t total
= 0 ;
1137 /* Special normfactor because tribyte value is read into an int. */
1138 normfact
= (psf
->norm_float
== SF_TRUE
) ? 1.0 / ((float) 0x80000000) : 1.0 / 256.0 ;
1140 bufferlen
= sizeof (psf
->u
.ucbuf
) / SIZEOF_TRIBYTE
;
1143 { if (len
< bufferlen
)
1144 bufferlen
= (int) len
;
1145 readcount
= psf_fread (psf
->u
.ucbuf
, SIZEOF_TRIBYTE
, bufferlen
, psf
) ;
1146 let2f_array ((tribyte
*) (psf
->u
.ucbuf
), readcount
, ptr
+ total
, normfact
) ;
1147 total
+= readcount
;
1148 if (readcount
< bufferlen
)
1154 } /* pcm_read_let2f */
1157 pcm_read_bei2f (SF_PRIVATE
*psf
, float *ptr
, sf_count_t len
)
1158 { int bufferlen
, readcount
;
1159 sf_count_t total
= 0 ;
1162 normfact
= (psf
->norm_float
== SF_TRUE
) ? 1.0 / ((float) 0x80000000) : 1.0 ;
1164 bufferlen
= ARRAY_LEN (psf
->u
.ibuf
) ;
1167 { if (len
< bufferlen
)
1168 bufferlen
= (int) len
;
1169 readcount
= psf_fread (psf
->u
.ibuf
, sizeof (int), bufferlen
, psf
) ;
1170 bei2f_array (psf
->u
.ibuf
, readcount
, ptr
+ total
, normfact
) ;
1171 total
+= readcount
;
1172 if (readcount
< bufferlen
)
1178 } /* pcm_read_bei2f */
1181 pcm_read_lei2f (SF_PRIVATE
*psf
, float *ptr
, sf_count_t len
)
1182 { int bufferlen
, readcount
;
1183 sf_count_t total
= 0 ;
1186 normfact
= (psf
->norm_float
== SF_TRUE
) ? 1.0 / ((float) 0x80000000) : 1.0 ;
1188 bufferlen
= ARRAY_LEN (psf
->u
.ibuf
) ;
1191 { if (len
< bufferlen
)
1192 bufferlen
= (int) len
;
1193 readcount
= psf_fread (psf
->u
.ibuf
, sizeof (int), bufferlen
, psf
) ;
1194 lei2f_array (psf
->u
.ibuf
, readcount
, ptr
+ total
, normfact
) ;
1195 total
+= readcount
;
1196 if (readcount
< bufferlen
)
1202 } /* pcm_read_lei2f */
1204 /*-----------------------------------------------------------------------------------------------
1208 pcm_read_sc2d (SF_PRIVATE
*psf
, double *ptr
, sf_count_t len
)
1209 { int bufferlen
, readcount
;
1210 sf_count_t total
= 0 ;
1213 normfact
= (psf
->norm_double
== SF_TRUE
) ? 1.0 / ((double) 0x80) : 1.0 ;
1215 bufferlen
= ARRAY_LEN (psf
->u
.scbuf
) ;
1218 { if (len
< bufferlen
)
1219 bufferlen
= (int) len
;
1220 readcount
= psf_fread (psf
->u
.scbuf
, sizeof (signed char), bufferlen
, psf
) ;
1221 sc2d_array (psf
->u
.scbuf
, readcount
, ptr
+ total
, normfact
) ;
1222 total
+= readcount
;
1223 if (readcount
< bufferlen
)
1229 } /* pcm_read_sc2d */
1232 pcm_read_uc2d (SF_PRIVATE
*psf
, double *ptr
, sf_count_t len
)
1233 { int bufferlen
, readcount
;
1234 sf_count_t total
= 0 ;
1237 normfact
= (psf
->norm_double
== SF_TRUE
) ? 1.0 / ((double) 0x80) : 1.0 ;
1239 bufferlen
= ARRAY_LEN (psf
->u
.ucbuf
) ;
1242 { if (len
< bufferlen
)
1243 bufferlen
= (int) len
;
1244 readcount
= psf_fread (psf
->u
.ucbuf
, sizeof (unsigned char), bufferlen
, psf
) ;
1245 uc2d_array (psf
->u
.ucbuf
, readcount
, ptr
+ total
, normfact
) ;
1246 total
+= readcount
;
1247 if (readcount
< bufferlen
)
1253 } /* pcm_read_uc2d */
1256 pcm_read_bes2d (SF_PRIVATE
*psf
, double *ptr
, sf_count_t len
)
1257 { int bufferlen
, readcount
;
1258 sf_count_t total
= 0 ;
1261 normfact
= (psf
->norm_double
== SF_TRUE
) ? 1.0 / ((double) 0x8000) : 1.0 ;
1263 bufferlen
= ARRAY_LEN (psf
->u
.sbuf
) ;
1266 { if (len
< bufferlen
)
1267 bufferlen
= (int) len
;
1268 readcount
= psf_fread (psf
->u
.sbuf
, sizeof (short), bufferlen
, psf
) ;
1269 bes2d_array (psf
->u
.sbuf
, readcount
, ptr
+ total
, normfact
) ;
1270 total
+= readcount
;
1271 if (readcount
< bufferlen
)
1277 } /* pcm_read_bes2d */
1280 pcm_read_les2d (SF_PRIVATE
*psf
, double *ptr
, sf_count_t len
)
1281 { int bufferlen
, readcount
;
1282 sf_count_t total
= 0 ;
1285 normfact
= (psf
->norm_double
== SF_TRUE
) ? 1.0 / ((double) 0x8000) : 1.0 ;
1287 bufferlen
= ARRAY_LEN (psf
->u
.sbuf
) ;
1290 { if (len
< bufferlen
)
1291 bufferlen
= (int) len
;
1292 readcount
= psf_fread (psf
->u
.sbuf
, sizeof (short), bufferlen
, psf
) ;
1293 les2d_array (psf
->u
.sbuf
, readcount
, ptr
+ total
, normfact
) ;
1294 total
+= readcount
;
1295 if (readcount
< bufferlen
)
1301 } /* pcm_read_les2d */
1304 pcm_read_bet2d (SF_PRIVATE
*psf
, double *ptr
, sf_count_t len
)
1305 { int bufferlen
, readcount
;
1306 sf_count_t total
= 0 ;
1309 normfact
= (psf
->norm_double
== SF_TRUE
) ? 1.0 / ((double) 0x80000000) : 1.0 / 256.0 ;
1311 bufferlen
= sizeof (psf
->u
.ucbuf
) / SIZEOF_TRIBYTE
;
1314 { if (len
< bufferlen
)
1315 bufferlen
= (int) len
;
1316 readcount
= psf_fread (psf
->u
.ucbuf
, SIZEOF_TRIBYTE
, bufferlen
, psf
) ;
1317 bet2d_array ((tribyte
*) (psf
->u
.ucbuf
), readcount
, ptr
+ total
, normfact
) ;
1318 total
+= readcount
;
1319 if (readcount
< bufferlen
)
1325 } /* pcm_read_bet2d */
1328 pcm_read_let2d (SF_PRIVATE
*psf
, double *ptr
, sf_count_t len
)
1329 { int bufferlen
, readcount
;
1330 sf_count_t total
= 0 ;
1333 /* Special normfactor because tribyte value is read into an int. */
1334 normfact
= (psf
->norm_double
== SF_TRUE
) ? 1.0 / ((double) 0x80000000) : 1.0 / 256.0 ;
1336 bufferlen
= sizeof (psf
->u
.ucbuf
) / SIZEOF_TRIBYTE
;
1339 { if (len
< bufferlen
)
1340 bufferlen
= (int) len
;
1341 readcount
= psf_fread (psf
->u
.ucbuf
, SIZEOF_TRIBYTE
, bufferlen
, psf
) ;
1342 let2d_array ((tribyte
*) (psf
->u
.ucbuf
), readcount
, ptr
+ total
, normfact
) ;
1343 total
+= readcount
;
1344 if (readcount
< bufferlen
)
1350 } /* pcm_read_let2d */
1353 pcm_read_bei2d (SF_PRIVATE
*psf
, double *ptr
, sf_count_t len
)
1354 { int bufferlen
, readcount
;
1355 sf_count_t total
= 0 ;
1358 normfact
= (psf
->norm_double
== SF_TRUE
) ? 1.0 / ((double) 0x80000000) : 1.0 ;
1360 bufferlen
= ARRAY_LEN (psf
->u
.ibuf
) ;
1363 { if (len
< bufferlen
)
1364 bufferlen
= (int) len
;
1365 readcount
= psf_fread (psf
->u
.ibuf
, sizeof (int), bufferlen
, psf
) ;
1366 bei2d_array (psf
->u
.ibuf
, readcount
, ptr
+ total
, normfact
) ;
1367 total
+= readcount
;
1368 if (readcount
< bufferlen
)
1374 } /* pcm_read_bei2d */
1377 pcm_read_lei2d (SF_PRIVATE
*psf
, double *ptr
, sf_count_t len
)
1378 { int bufferlen
, readcount
;
1379 sf_count_t total
= 0 ;
1382 normfact
= (psf
->norm_double
== SF_TRUE
) ? 1.0 / ((double) 0x80000000) : 1.0 ;
1384 bufferlen
= ARRAY_LEN (psf
->u
.ibuf
) ;
1387 { if (len
< bufferlen
)
1388 bufferlen
= (int) len
;
1389 readcount
= psf_fread (psf
->u
.ibuf
, sizeof (int), bufferlen
, psf
) ;
1390 lei2d_array (psf
->u
.ibuf
, readcount
, ptr
+ total
, normfact
) ;
1391 total
+= readcount
;
1392 if (readcount
< bufferlen
)
1398 } /* pcm_read_lei2d */
1400 /*===============================================================================================
1401 **-----------------------------------------------------------------------------------------------
1402 **===============================================================================================
1406 pcm_write_s2sc (SF_PRIVATE
*psf
, const short *ptr
, sf_count_t len
)
1407 { int bufferlen
, writecount
;
1408 sf_count_t total
= 0 ;
1410 bufferlen
= ARRAY_LEN (psf
->u
.scbuf
) ;
1413 { if (len
< bufferlen
)
1414 bufferlen
= (int) len
;
1415 s2sc_array (ptr
+ total
, psf
->u
.scbuf
, bufferlen
) ;
1416 writecount
= psf_fwrite (psf
->u
.scbuf
, sizeof (signed char), bufferlen
, psf
) ;
1417 total
+= writecount
;
1418 if (writecount
< bufferlen
)
1424 } /* pcm_write_s2sc */
1427 pcm_write_s2uc (SF_PRIVATE
*psf
, const short *ptr
, sf_count_t len
)
1428 { int bufferlen
, writecount
;
1429 sf_count_t total
= 0 ;
1431 bufferlen
= ARRAY_LEN (psf
->u
.ucbuf
) ;
1434 { if (len
< bufferlen
)
1435 bufferlen
= (int) len
;
1436 s2uc_array (ptr
+ total
, psf
->u
.ucbuf
, bufferlen
) ;
1437 writecount
= psf_fwrite (psf
->u
.ucbuf
, sizeof (unsigned char), bufferlen
, psf
) ;
1438 total
+= writecount
;
1439 if (writecount
< bufferlen
)
1445 } /* pcm_write_s2uc */
1448 pcm_write_s2bes (SF_PRIVATE
*psf
, const short *ptr
, sf_count_t len
)
1449 { int bufferlen
, writecount
;
1450 sf_count_t total
= 0 ;
1452 if (CPU_IS_BIG_ENDIAN
)
1453 return psf_fwrite (ptr
, sizeof (short), len
, psf
) ;
1456 bufferlen
= ARRAY_LEN (psf
->u
.sbuf
) ;
1459 { if (len
< bufferlen
)
1460 bufferlen
= (int) len
;
1461 endswap_short_copy (psf
->u
.sbuf
, ptr
+ total
, bufferlen
) ;
1462 writecount
= psf_fwrite (psf
->u
.sbuf
, sizeof (short), bufferlen
, psf
) ;
1463 total
+= writecount
;
1464 if (writecount
< bufferlen
)
1470 } /* pcm_write_s2bes */
1473 pcm_write_s2les (SF_PRIVATE
*psf
, const short *ptr
, sf_count_t len
)
1474 { int bufferlen
, writecount
;
1475 sf_count_t total
= 0 ;
1477 if (CPU_IS_LITTLE_ENDIAN
)
1478 return psf_fwrite (ptr
, sizeof (short), len
, psf
) ;
1480 bufferlen
= ARRAY_LEN (psf
->u
.sbuf
) ;
1483 { if (len
< bufferlen
)
1484 bufferlen
= (int) len
;
1485 endswap_short_copy (psf
->u
.sbuf
, ptr
+ total
, bufferlen
) ;
1486 writecount
= psf_fwrite (psf
->u
.sbuf
, sizeof (short), bufferlen
, psf
) ;
1487 total
+= writecount
;
1488 if (writecount
< bufferlen
)
1494 } /* pcm_write_s2les */
1497 pcm_write_s2bet (SF_PRIVATE
*psf
, const short *ptr
, sf_count_t len
)
1498 { int bufferlen
, writecount
;
1499 sf_count_t total
= 0 ;
1501 bufferlen
= sizeof (psf
->u
.ucbuf
) / SIZEOF_TRIBYTE
;
1504 { if (len
< bufferlen
)
1505 bufferlen
= (int) len
;
1506 s2bet_array (ptr
+ total
, (tribyte
*) (psf
->u
.ucbuf
), bufferlen
) ;
1507 writecount
= psf_fwrite (psf
->u
.ucbuf
, SIZEOF_TRIBYTE
, bufferlen
, psf
) ;
1508 total
+= writecount
;
1509 if (writecount
< bufferlen
)
1515 } /* pcm_write_s2bet */
1518 pcm_write_s2let (SF_PRIVATE
*psf
, const short *ptr
, sf_count_t len
)
1519 { int bufferlen
, writecount
;
1520 sf_count_t total
= 0 ;
1522 bufferlen
= sizeof (psf
->u
.ucbuf
) / SIZEOF_TRIBYTE
;
1525 { if (len
< bufferlen
)
1526 bufferlen
= (int) len
;
1527 s2let_array (ptr
+ total
, (tribyte
*) (psf
->u
.ucbuf
), bufferlen
) ;
1528 writecount
= psf_fwrite (psf
->u
.ucbuf
, SIZEOF_TRIBYTE
, bufferlen
, psf
) ;
1529 total
+= writecount
;
1530 if (writecount
< bufferlen
)
1536 } /* pcm_write_s2let */
1539 pcm_write_s2bei (SF_PRIVATE
*psf
, const short *ptr
, sf_count_t len
)
1540 { int bufferlen
, writecount
;
1541 sf_count_t total
= 0 ;
1543 bufferlen
= ARRAY_LEN (psf
->u
.ibuf
) ;
1546 { if (len
< bufferlen
)
1547 bufferlen
= (int) len
;
1548 s2bei_array (ptr
+ total
, psf
->u
.ibuf
, bufferlen
) ;
1549 writecount
= psf_fwrite (psf
->u
.ibuf
, sizeof (int), bufferlen
, psf
) ;
1550 total
+= writecount
;
1551 if (writecount
< bufferlen
)
1557 } /* pcm_write_s2bei */
1560 pcm_write_s2lei (SF_PRIVATE
*psf
, const short *ptr
, sf_count_t len
)
1561 { int bufferlen
, writecount
;
1562 sf_count_t total
= 0 ;
1564 bufferlen
= ARRAY_LEN (psf
->u
.ibuf
) ;
1567 { if (len
< bufferlen
)
1568 bufferlen
= (int) len
;
1569 s2lei_array (ptr
+ total
, psf
->u
.ibuf
, bufferlen
) ;
1570 writecount
= psf_fwrite (psf
->u
.ibuf
, sizeof (int), bufferlen
, psf
) ;
1571 total
+= writecount
;
1572 if (writecount
< bufferlen
)
1578 } /* pcm_write_s2lei */
1580 /*-----------------------------------------------------------------------------------------------
1584 pcm_write_i2sc (SF_PRIVATE
*psf
, const int *ptr
, sf_count_t len
)
1585 { int bufferlen
, writecount
;
1586 sf_count_t total
= 0 ;
1588 bufferlen
= ARRAY_LEN (psf
->u
.scbuf
) ;
1591 { if (len
< bufferlen
)
1592 bufferlen
= (int) len
;
1593 i2sc_array (ptr
+ total
, psf
->u
.scbuf
, bufferlen
) ;
1594 writecount
= psf_fwrite (psf
->u
.scbuf
, sizeof (signed char), bufferlen
, psf
) ;
1595 total
+= writecount
;
1596 if (writecount
< bufferlen
)
1602 } /* pcm_write_i2sc */
1605 pcm_write_i2uc (SF_PRIVATE
*psf
, const int *ptr
, sf_count_t len
)
1606 { int bufferlen
, writecount
;
1607 sf_count_t total
= 0 ;
1609 bufferlen
= ARRAY_LEN (psf
->u
.ucbuf
) ;
1612 { if (len
< bufferlen
)
1613 bufferlen
= (int) len
;
1614 i2uc_array (ptr
+ total
, psf
->u
.ucbuf
, bufferlen
) ;
1615 writecount
= psf_fwrite (psf
->u
.ucbuf
, sizeof (signed char), bufferlen
, psf
) ;
1616 total
+= writecount
;
1617 if (writecount
< bufferlen
)
1623 } /* pcm_write_i2uc */
1626 pcm_write_i2bes (SF_PRIVATE
*psf
, const int *ptr
, sf_count_t len
)
1627 { int bufferlen
, writecount
;
1628 sf_count_t total
= 0 ;
1630 bufferlen
= ARRAY_LEN (psf
->u
.sbuf
) ;
1633 { if (len
< bufferlen
)
1634 bufferlen
= (int) len
;
1635 i2bes_array (ptr
+ total
, psf
->u
.sbuf
, bufferlen
) ;
1636 writecount
= psf_fwrite (psf
->u
.sbuf
, sizeof (short), bufferlen
, psf
) ;
1637 total
+= writecount
;
1638 if (writecount
< bufferlen
)
1644 } /* pcm_write_i2bes */
1647 pcm_write_i2les (SF_PRIVATE
*psf
, const int *ptr
, sf_count_t len
)
1648 { int bufferlen
, writecount
;
1649 sf_count_t total
= 0 ;
1651 bufferlen
= ARRAY_LEN (psf
->u
.sbuf
) ;
1654 { if (len
< bufferlen
)
1655 bufferlen
= (int) len
;
1656 i2les_array (ptr
+ total
, psf
->u
.sbuf
, bufferlen
) ;
1657 writecount
= psf_fwrite (psf
->u
.sbuf
, sizeof (short), bufferlen
, psf
) ;
1658 total
+= writecount
;
1659 if (writecount
< bufferlen
)
1665 } /* pcm_write_i2les */
1668 pcm_write_i2bet (SF_PRIVATE
*psf
, const int *ptr
, sf_count_t len
)
1669 { int bufferlen
, writecount
;
1670 sf_count_t total
= 0 ;
1672 bufferlen
= sizeof (psf
->u
.ucbuf
) / SIZEOF_TRIBYTE
;
1675 { if (len
< bufferlen
)
1676 bufferlen
= (int) len
;
1677 i2bet_array (ptr
+ total
, (tribyte
*) (psf
->u
.ucbuf
), bufferlen
) ;
1678 writecount
= psf_fwrite (psf
->u
.ucbuf
, SIZEOF_TRIBYTE
, bufferlen
, psf
) ;
1679 total
+= writecount
;
1680 if (writecount
< bufferlen
)
1686 } /* pcm_write_i2bet */
1689 pcm_write_i2let (SF_PRIVATE
*psf
, const int *ptr
, sf_count_t len
)
1690 { int bufferlen
, writecount
;
1691 sf_count_t total
= 0 ;
1693 bufferlen
= sizeof (psf
->u
.ucbuf
) / SIZEOF_TRIBYTE
;
1696 { if (len
< bufferlen
)
1697 bufferlen
= (int) len
;
1698 i2let_array (ptr
+ total
, (tribyte
*) (psf
->u
.ucbuf
), bufferlen
) ;
1699 writecount
= psf_fwrite (psf
->u
.ucbuf
, SIZEOF_TRIBYTE
, bufferlen
, psf
) ;
1700 total
+= writecount
;
1701 if (writecount
< bufferlen
)
1707 } /* pcm_write_i2les */
1710 pcm_write_i2bei (SF_PRIVATE
*psf
, const int *ptr
, sf_count_t len
)
1711 { int bufferlen
, writecount
;
1712 sf_count_t total
= 0 ;
1714 if (CPU_IS_BIG_ENDIAN
)
1715 return psf_fwrite (ptr
, sizeof (int), len
, psf
) ;
1717 bufferlen
= ARRAY_LEN (psf
->u
.ibuf
) ;
1720 { if (len
< bufferlen
)
1721 bufferlen
= (int) len
;
1722 endswap_int_copy (psf
->u
.ibuf
, ptr
+ total
, bufferlen
) ;
1723 writecount
= psf_fwrite (psf
->u
.ibuf
, sizeof (int), bufferlen
, psf
) ;
1724 total
+= writecount
;
1725 if (writecount
< bufferlen
)
1731 } /* pcm_write_i2bei */
1734 pcm_write_i2lei (SF_PRIVATE
*psf
, const int *ptr
, sf_count_t len
)
1735 { int bufferlen
, writecount
;
1736 sf_count_t total
= 0 ;
1738 if (CPU_IS_LITTLE_ENDIAN
)
1739 return psf_fwrite (ptr
, sizeof (int), len
, psf
) ;
1741 bufferlen
= ARRAY_LEN (psf
->u
.ibuf
) ;
1744 { if (len
< bufferlen
)
1745 bufferlen
= (int) len
;
1746 endswap_int_copy (psf
->u
.ibuf
, ptr
+ total
, bufferlen
) ;
1747 writecount
= psf_fwrite (psf
->u
.ibuf
, sizeof (int), bufferlen
, psf
) ;
1748 total
+= writecount
;
1749 if (writecount
< bufferlen
)
1755 } /* pcm_write_i2lei */
1757 /*------------------------------------------------------------------------------
1758 **==============================================================================
1759 **------------------------------------------------------------------------------
1763 f2sc_array (const float *src
, signed char *dest
, int count
, int normalize
)
1766 normfact
= normalize
? (1.0 * 0x7F) : 1.0 ;
1768 while (--count
>= 0)
1769 { dest
[count
] = lrintf (src
[count
] * normfact
) ;
1774 f2sc_clip_array (const float *src
, signed char *dest
, int count
, int normalize
)
1775 { float normfact
, scaled_value
;
1777 normfact
= normalize
? (8.0 * 0x10000000) : (1.0 * 0x1000000) ;
1779 while (--count
>= 0)
1780 { scaled_value
= src
[count
] * normfact
;
1781 if (CPU_CLIPS_POSITIVE
== 0 && scaled_value
>= (1.0 * 0x7FFFFFFF))
1782 { dest
[count
] = 127 ;
1785 if (CPU_CLIPS_NEGATIVE
== 0 && scaled_value
<= (-8.0 * 0x10000000))
1786 { dest
[count
] = -128 ;
1790 dest
[count
] = lrintf (scaled_value
) >> 24 ;
1792 } /* f2sc_clip_array */
1795 pcm_write_f2sc (SF_PRIVATE
*psf
, const float *ptr
, sf_count_t len
)
1796 { void (*convert
) (const float *, signed char *, int, int) ;
1797 int bufferlen
, writecount
;
1798 sf_count_t total
= 0 ;
1800 convert
= (psf
->add_clipping
) ? f2sc_clip_array
: f2sc_array
;
1801 bufferlen
= ARRAY_LEN (psf
->u
.scbuf
) ;
1804 { if (len
< bufferlen
)
1805 bufferlen
= (int) len
;
1806 convert (ptr
+ total
, psf
->u
.scbuf
, bufferlen
, psf
->norm_float
) ;
1807 writecount
= psf_fwrite (psf
->u
.scbuf
, sizeof (signed char), bufferlen
, psf
) ;
1808 total
+= writecount
;
1809 if (writecount
< bufferlen
)
1815 } /* pcm_write_f2sc */
1817 /*==============================================================================
1821 f2uc_array (const float *src
, unsigned char *dest
, int count
, int normalize
)
1824 normfact
= normalize
? (1.0 * 0x7F) : 1.0 ;
1826 while (--count
>= 0)
1827 { dest
[count
] = lrintf (src
[count
] * normfact
) + 128 ;
1832 f2uc_clip_array (const float *src
, unsigned char *dest
, int count
, int normalize
)
1833 { float normfact
, scaled_value
;
1835 normfact
= normalize
? (8.0 * 0x10000000) : (1.0 * 0x1000000) ;
1837 while (--count
>= 0)
1838 { scaled_value
= src
[count
] * normfact
;
1839 if (CPU_CLIPS_POSITIVE
== 0 && scaled_value
>= (1.0 * 0x7FFFFFFF))
1840 { dest
[count
] = 0xFF ;
1843 if (CPU_CLIPS_NEGATIVE
== 0 && scaled_value
<= (-8.0 * 0x10000000))
1844 { dest
[count
] = 0 ;
1848 dest
[count
] = (lrintf (scaled_value
) >> 24) + 128 ;
1850 } /* f2uc_clip_array */
1853 pcm_write_f2uc (SF_PRIVATE
*psf
, const float *ptr
, sf_count_t len
)
1854 { void (*convert
) (const float *, unsigned char *, int, int) ;
1855 int bufferlen
, writecount
;
1856 sf_count_t total
= 0 ;
1858 convert
= (psf
->add_clipping
) ? f2uc_clip_array
: f2uc_array
;
1859 bufferlen
= ARRAY_LEN (psf
->u
.ucbuf
) ;
1862 { if (len
< bufferlen
)
1863 bufferlen
= (int) len
;
1864 convert (ptr
+ total
, psf
->u
.ucbuf
, bufferlen
, psf
->norm_float
) ;
1865 writecount
= psf_fwrite (psf
->u
.ucbuf
, sizeof (unsigned char), bufferlen
, psf
) ;
1866 total
+= writecount
;
1867 if (writecount
< bufferlen
)
1873 } /* pcm_write_f2uc */
1875 /*==============================================================================
1879 f2bes_array (const float *src
, short *dest
, int count
, int normalize
)
1880 { unsigned char *ucptr
;
1884 normfact
= normalize
? (1.0 * 0x7FFF) : 1.0 ;
1885 ucptr
= ((unsigned char*) dest
) + 2 * count
;
1887 while (--count
>= 0)
1889 value
= lrintf (src
[count
] * normfact
) ;
1891 ucptr
[0] = value
>> 8 ;
1896 f2bes_clip_array (const float *src
, short *dest
, int count
, int normalize
)
1897 { unsigned char *ucptr
;
1898 float normfact
, scaled_value
;
1901 normfact
= normalize
? (8.0 * 0x10000000) : (1.0 * 0x10000) ;
1902 ucptr
= ((unsigned char*) dest
) + 2 * count
;
1904 while (--count
>= 0)
1906 scaled_value
= src
[count
] * normfact
;
1907 if (CPU_CLIPS_POSITIVE
== 0 && scaled_value
>= (1.0 * 0x7FFFFFFF))
1908 { ucptr
[1] = 0xFF ;
1912 if (CPU_CLIPS_NEGATIVE
== 0 && scaled_value
<= (-8.0 * 0x10000000))
1913 { ucptr
[1] = 0x00 ;
1918 value
= lrintf (scaled_value
) ;
1919 ucptr
[1] = value
>> 16 ;
1920 ucptr
[0] = value
>> 24 ;
1922 } /* f2bes_clip_array */
1925 pcm_write_f2bes (SF_PRIVATE
*psf
, const float *ptr
, sf_count_t len
)
1926 { void (*convert
) (const float *, short *t
, int, int) ;
1927 int bufferlen
, writecount
;
1928 sf_count_t total
= 0 ;
1930 convert
= (psf
->add_clipping
) ? f2bes_clip_array
: f2bes_array
;
1931 bufferlen
= ARRAY_LEN (psf
->u
.sbuf
) ;
1934 { if (len
< bufferlen
)
1935 bufferlen
= (int) len
;
1936 convert (ptr
+ total
, psf
->u
.sbuf
, bufferlen
, psf
->norm_float
) ;
1937 writecount
= psf_fwrite (psf
->u
.sbuf
, sizeof (short), bufferlen
, psf
) ;
1938 total
+= writecount
;
1939 if (writecount
< bufferlen
)
1945 } /* pcm_write_f2bes */
1947 /*==============================================================================
1951 f2les_array (const float *src
, short *dest
, int count
, int normalize
)
1952 { unsigned char *ucptr
;
1956 normfact
= normalize
? (1.0 * 0x7FFF) : 1.0 ;
1957 ucptr
= ((unsigned char*) dest
) + 2 * count
;
1959 while (--count
>= 0)
1961 value
= lrintf (src
[count
] * normfact
) ;
1963 ucptr
[1] = value
>> 8 ;
1968 f2les_clip_array (const float *src
, short *dest
, int count
, int normalize
)
1969 { unsigned char *ucptr
;
1970 float normfact
, scaled_value
;
1973 normfact
= normalize
? (8.0 * 0x10000000) : (1.0 * 0x10000) ;
1974 ucptr
= ((unsigned char*) dest
) + 2 * count
;
1976 while (--count
>= 0)
1978 scaled_value
= src
[count
] * normfact
;
1979 if (CPU_CLIPS_POSITIVE
== 0 && scaled_value
>= (1.0 * 0x7FFFFFFF))
1980 { ucptr
[0] = 0xFF ;
1984 if (CPU_CLIPS_NEGATIVE
== 0 && scaled_value
<= (-8.0 * 0x10000000))
1985 { ucptr
[0] = 0x00 ;
1990 value
= lrintf (scaled_value
) ;
1991 ucptr
[0] = value
>> 16 ;
1992 ucptr
[1] = value
>> 24 ;
1994 } /* f2les_clip_array */
1997 pcm_write_f2les (SF_PRIVATE
*psf
, const float *ptr
, sf_count_t len
)
1998 { void (*convert
) (const float *, short *t
, int, int) ;
1999 int bufferlen
, writecount
;
2000 sf_count_t total
= 0 ;
2002 convert
= (psf
->add_clipping
) ? f2les_clip_array
: f2les_array
;
2003 bufferlen
= ARRAY_LEN (psf
->u
.sbuf
) ;
2006 { if (len
< bufferlen
)
2007 bufferlen
= (int) len
;
2008 convert (ptr
+ total
, psf
->u
.sbuf
, bufferlen
, psf
->norm_float
) ;
2009 writecount
= psf_fwrite (psf
->u
.sbuf
, sizeof (short), bufferlen
, psf
) ;
2010 total
+= writecount
;
2011 if (writecount
< bufferlen
)
2017 } /* pcm_write_f2les */
2019 /*==============================================================================
2023 f2let_array (const float *src
, tribyte
*dest
, int count
, int normalize
)
2024 { unsigned char *ucptr
;
2028 normfact
= normalize
? (1.0 * 0x7FFFFF) : 1.0 ;
2029 ucptr
= ((unsigned char*) dest
) + 3 * count
;
2031 while (--count
>= 0)
2033 value
= lrintf (src
[count
] * normfact
) ;
2035 ucptr
[1] = value
>> 8 ;
2036 ucptr
[2] = value
>> 16 ;
2041 f2let_clip_array (const float *src
, tribyte
*dest
, int count
, int normalize
)
2042 { unsigned char *ucptr
;
2043 float normfact
, scaled_value
;
2046 normfact
= normalize
? (8.0 * 0x10000000) : (1.0 * 0x100) ;
2047 ucptr
= ((unsigned char*) dest
) + 3 * count
;
2049 while (--count
>= 0)
2051 scaled_value
= src
[count
] * normfact
;
2052 if (CPU_CLIPS_POSITIVE
== 0 && scaled_value
>= (1.0 * 0x7FFFFFFF))
2053 { ucptr
[0] = 0xFF ;
2058 if (CPU_CLIPS_NEGATIVE
== 0 && scaled_value
<= (-8.0 * 0x10000000))
2059 { ucptr
[0] = 0x00 ;
2065 value
= lrintf (scaled_value
) ;
2066 ucptr
[0] = value
>> 8 ;
2067 ucptr
[1] = value
>> 16 ;
2068 ucptr
[2] = value
>> 24 ;
2070 } /* f2let_clip_array */
2073 pcm_write_f2let (SF_PRIVATE
*psf
, const float *ptr
, sf_count_t len
)
2074 { void (*convert
) (const float *, tribyte
*, int, int) ;
2075 int bufferlen
, writecount
;
2076 sf_count_t total
= 0 ;
2078 convert
= (psf
->add_clipping
) ? f2let_clip_array
: f2let_array
;
2079 bufferlen
= sizeof (psf
->u
.ucbuf
) / SIZEOF_TRIBYTE
;
2082 { if (len
< bufferlen
)
2083 bufferlen
= (int) len
;
2084 convert (ptr
+ total
, (tribyte
*) (psf
->u
.ucbuf
), bufferlen
, psf
->norm_float
) ;
2085 writecount
= psf_fwrite (psf
->u
.ucbuf
, SIZEOF_TRIBYTE
, bufferlen
, psf
) ;
2086 total
+= writecount
;
2087 if (writecount
< bufferlen
)
2093 } /* pcm_write_f2let */
2095 /*==============================================================================
2099 f2bet_array (const float *src
, tribyte
*dest
, int count
, int normalize
)
2100 { unsigned char *ucptr
;
2104 normfact
= normalize
? (1.0 * 0x7FFFFF) : 1.0 ;
2105 ucptr
= ((unsigned char*) dest
) + 3 * count
;
2107 while (--count
>= 0)
2109 value
= lrintf (src
[count
] * normfact
) ;
2110 ucptr
[0] = value
>> 16 ;
2111 ucptr
[1] = value
>> 8 ;
2117 f2bet_clip_array (const float *src
, tribyte
*dest
, int count
, int normalize
)
2118 { unsigned char *ucptr
;
2119 float normfact
, scaled_value
;
2122 normfact
= normalize
? (8.0 * 0x10000000) : (1.0 * 0x100) ;
2123 ucptr
= ((unsigned char*) dest
) + 3 * count
;
2125 while (--count
>= 0)
2127 scaled_value
= src
[count
] * normfact
;
2128 if (CPU_CLIPS_POSITIVE
== 0 && scaled_value
>= (1.0 * 0x7FFFFFFF))
2129 { ucptr
[0] = 0x7F ;
2134 if (CPU_CLIPS_NEGATIVE
== 0 && scaled_value
<= (-8.0 * 0x10000000))
2135 { ucptr
[0] = 0x80 ;
2141 value
= lrint (scaled_value
) ;
2142 ucptr
[0] = value
>> 24 ;
2143 ucptr
[1] = value
>> 16 ;
2144 ucptr
[2] = value
>> 8 ;
2146 } /* f2bet_clip_array */
2149 pcm_write_f2bet (SF_PRIVATE
*psf
, const float *ptr
, sf_count_t len
)
2150 { void (*convert
) (const float *, tribyte
*, int, int) ;
2151 int bufferlen
, writecount
;
2152 sf_count_t total
= 0 ;
2154 convert
= (psf
->add_clipping
) ? f2bet_clip_array
: f2bet_array
;
2155 bufferlen
= sizeof (psf
->u
.ucbuf
) / SIZEOF_TRIBYTE
;
2158 { if (len
< bufferlen
)
2159 bufferlen
= (int) len
;
2160 convert (ptr
+ total
, (tribyte
*) (psf
->u
.ucbuf
), bufferlen
, psf
->norm_float
) ;
2161 writecount
= psf_fwrite (psf
->u
.ucbuf
, SIZEOF_TRIBYTE
, bufferlen
, psf
) ;
2162 total
+= writecount
;
2163 if (writecount
< bufferlen
)
2169 } /* pcm_write_f2bet */
2171 /*==============================================================================
2175 f2bei_array (const float *src
, int *dest
, int count
, int normalize
)
2176 { unsigned char *ucptr
;
2180 normfact
= normalize
? (1.0 * 0x7FFFFFFF) : 1.0 ;
2181 ucptr
= ((unsigned char*) dest
) + 4 * count
;
2182 while (--count
>= 0)
2184 value
= lrintf (src
[count
] * normfact
) ;
2185 ucptr
[0] = value
>> 24 ;
2186 ucptr
[1] = value
>> 16 ;
2187 ucptr
[2] = value
>> 8 ;
2193 f2bei_clip_array (const float *src
, int *dest
, int count
, int normalize
)
2194 { unsigned char *ucptr
;
2195 float normfact
, scaled_value
;
2198 normfact
= normalize
? (8.0 * 0x10000000) : 1.0 ;
2199 ucptr
= ((unsigned char*) dest
) + 4 * count
;
2201 while (--count
>= 0)
2203 scaled_value
= src
[count
] * normfact
;
2204 if (CPU_CLIPS_POSITIVE
== 0 && scaled_value
>= 1.0 * 0x7FFFFFFF)
2205 { ucptr
[0] = 0x7F ;
2211 if (CPU_CLIPS_NEGATIVE
== 0 && scaled_value
<= (-8.0 * 0x10000000))
2212 { ucptr
[0] = 0x80 ;
2219 value
= lrintf (scaled_value
) ;
2220 ucptr
[0] = value
>> 24 ;
2221 ucptr
[1] = value
>> 16 ;
2222 ucptr
[2] = value
>> 8 ;
2225 } /* f2bei_clip_array */
2228 pcm_write_f2bei (SF_PRIVATE
*psf
, const float *ptr
, sf_count_t len
)
2229 { void (*convert
) (const float *, int *, int, int) ;
2230 int bufferlen
, writecount
;
2231 sf_count_t total
= 0 ;
2233 convert
= (psf
->add_clipping
) ? f2bei_clip_array
: f2bei_array
;
2234 bufferlen
= ARRAY_LEN (psf
->u
.ibuf
) ;
2237 { if (len
< bufferlen
)
2238 bufferlen
= (int) len
;
2239 convert (ptr
+ total
, psf
->u
.ibuf
, bufferlen
, psf
->norm_float
) ;
2240 writecount
= psf_fwrite (psf
->u
.ibuf
, sizeof (int), bufferlen
, psf
) ;
2241 total
+= writecount
;
2242 if (writecount
< bufferlen
)
2248 } /* pcm_write_f2bei */
2250 /*==============================================================================
2254 f2lei_array (const float *src
, int *dest
, int count
, int normalize
)
2255 { unsigned char *ucptr
;
2259 normfact
= normalize
? (1.0 * 0x7FFFFFFF) : 1.0 ;
2260 ucptr
= ((unsigned char*) dest
) + 4 * count
;
2262 while (--count
>= 0)
2264 value
= lrintf (src
[count
] * normfact
) ;
2266 ucptr
[1] = value
>> 8 ;
2267 ucptr
[2] = value
>> 16 ;
2268 ucptr
[3] = value
>> 24 ;
2273 f2lei_clip_array (const float *src
, int *dest
, int count
, int normalize
)
2274 { unsigned char *ucptr
;
2275 float normfact
, scaled_value
;
2278 normfact
= normalize
? (8.0 * 0x10000000) : 1.0 ;
2279 ucptr
= ((unsigned char*) dest
) + 4 * count
;
2281 while (--count
>= 0)
2283 scaled_value
= src
[count
] * normfact
;
2284 if (CPU_CLIPS_POSITIVE
== 0 && scaled_value
>= (1.0 * 0x7FFFFFFF))
2285 { ucptr
[0] = 0xFF ;
2291 if (CPU_CLIPS_NEGATIVE
== 0 && scaled_value
<= (-8.0 * 0x10000000))
2292 { ucptr
[0] = 0x00 ;
2299 value
= lrintf (scaled_value
) ;
2301 ucptr
[1] = value
>> 8 ;
2302 ucptr
[2] = value
>> 16 ;
2303 ucptr
[3] = value
>> 24 ;
2305 } /* f2lei_clip_array */
2308 pcm_write_f2lei (SF_PRIVATE
*psf
, const float *ptr
, sf_count_t len
)
2309 { void (*convert
) (const float *, int *, int, int) ;
2310 int bufferlen
, writecount
;
2311 sf_count_t total
= 0 ;
2313 convert
= (psf
->add_clipping
) ? f2lei_clip_array
: f2lei_array
;
2314 bufferlen
= ARRAY_LEN (psf
->u
.ibuf
) ;
2317 { if (len
< bufferlen
)
2318 bufferlen
= (int) len
;
2319 convert (ptr
+ total
, psf
->u
.ibuf
, bufferlen
, psf
->norm_float
) ;
2320 writecount
= psf_fwrite (psf
->u
.ibuf
, sizeof (int), bufferlen
, psf
) ;
2321 total
+= writecount
;
2322 if (writecount
< bufferlen
)
2328 } /* pcm_write_f2lei */
2330 /*==============================================================================
2334 d2sc_array (const double *src
, signed char *dest
, int count
, int normalize
)
2337 normfact
= normalize
? (1.0 * 0x7F) : 1.0 ;
2339 while (--count
>= 0)
2340 { dest
[count
] = lrint (src
[count
] * normfact
) ;
2345 d2sc_clip_array (const double *src
, signed char *dest
, int count
, int normalize
)
2346 { double normfact
, scaled_value
;
2348 normfact
= normalize
? (8.0 * 0x10000000) : (1.0 * 0x1000000) ;
2350 while (--count
>= 0)
2351 { scaled_value
= src
[count
] * normfact
;
2352 if (CPU_CLIPS_POSITIVE
== 0 && scaled_value
>= (1.0 * 0x7FFFFFFF))
2353 { dest
[count
] = 127 ;
2356 if (CPU_CLIPS_NEGATIVE
== 0 && scaled_value
<= (-8.0 * 0x10000000))
2357 { dest
[count
] = -128 ;
2361 dest
[count
] = lrintf (scaled_value
) >> 24 ;
2363 } /* d2sc_clip_array */
2366 pcm_write_d2sc (SF_PRIVATE
*psf
, const double *ptr
, sf_count_t len
)
2367 { void (*convert
) (const double *, signed char *, int, int) ;
2368 int bufferlen
, writecount
;
2369 sf_count_t total
= 0 ;
2371 convert
= (psf
->add_clipping
) ? d2sc_clip_array
: d2sc_array
;
2372 bufferlen
= ARRAY_LEN (psf
->u
.scbuf
) ;
2375 { if (len
< bufferlen
)
2376 bufferlen
= (int) len
;
2377 convert (ptr
+ total
, psf
->u
.scbuf
, bufferlen
, psf
->norm_double
) ;
2378 writecount
= psf_fwrite (psf
->u
.scbuf
, sizeof (signed char), bufferlen
, psf
) ;
2379 total
+= writecount
;
2380 if (writecount
< bufferlen
)
2386 } /* pcm_write_d2sc */
2388 /*==============================================================================
2392 d2uc_array (const double *src
, unsigned char *dest
, int count
, int normalize
)
2395 normfact
= normalize
? (1.0 * 0x7F) : 1.0 ;
2397 while (--count
>= 0)
2398 { dest
[count
] = lrint (src
[count
] * normfact
) + 128 ;
2403 d2uc_clip_array (const double *src
, unsigned char *dest
, int count
, int normalize
)
2404 { double normfact
, scaled_value
;
2406 normfact
= normalize
? (8.0 * 0x10000000) : (1.0 * 0x1000000) ;
2408 while (--count
>= 0)
2409 { scaled_value
= src
[count
] * normfact
;
2410 if (CPU_CLIPS_POSITIVE
== 0 && scaled_value
>= (1.0 * 0x7FFFFFFF))
2411 { dest
[count
] = 255 ;
2414 if (CPU_CLIPS_NEGATIVE
== 0 && scaled_value
<= (-8.0 * 0x10000000))
2415 { dest
[count
] = 0 ;
2419 dest
[count
] = (lrint (src
[count
] * normfact
) >> 24) + 128 ;
2421 } /* d2uc_clip_array */
2424 pcm_write_d2uc (SF_PRIVATE
*psf
, const double *ptr
, sf_count_t len
)
2425 { void (*convert
) (const double *, unsigned char *, int, int) ;
2426 int bufferlen
, writecount
;
2427 sf_count_t total
= 0 ;
2429 convert
= (psf
->add_clipping
) ? d2uc_clip_array
: d2uc_array
;
2430 bufferlen
= ARRAY_LEN (psf
->u
.ucbuf
) ;
2433 { if (len
< bufferlen
)
2434 bufferlen
= (int) len
;
2435 convert (ptr
+ total
, psf
->u
.ucbuf
, bufferlen
, psf
->norm_double
) ;
2436 writecount
= psf_fwrite (psf
->u
.ucbuf
, sizeof (unsigned char), bufferlen
, psf
) ;
2437 total
+= writecount
;
2438 if (writecount
< bufferlen
)
2444 } /* pcm_write_d2uc */
2446 /*==============================================================================
2450 d2bes_array (const double *src
, short *dest
, int count
, int normalize
)
2451 { unsigned char *ucptr
;
2455 normfact
= normalize
? (1.0 * 0x7FFF) : 1.0 ;
2456 ucptr
= ((unsigned char*) dest
) + 2 * count
;
2458 while (--count
>= 0)
2460 value
= lrint (src
[count
] * normfact
) ;
2462 ucptr
[0] = value
>> 8 ;
2467 d2bes_clip_array (const double *src
, short *dest
, int count
, int normalize
)
2468 { unsigned char *ucptr
;
2469 double normfact
, scaled_value
;
2472 normfact
= normalize
? (8.0 * 0x10000000) : (1.0 * 0x10000) ;
2473 ucptr
= ((unsigned char*) dest
) + 2 * count
;
2475 while (--count
>= 0)
2477 scaled_value
= src
[count
] * normfact
;
2478 if (CPU_CLIPS_POSITIVE
== 0 && scaled_value
>= (1.0 * 0x7FFFFFFF))
2479 { ucptr
[1] = 0xFF ;
2483 if (CPU_CLIPS_NEGATIVE
== 0 && scaled_value
<= (-8.0 * 0x10000000))
2484 { ucptr
[1] = 0x00 ;
2489 value
= lrint (scaled_value
) ;
2490 ucptr
[1] = value
>> 16 ;
2491 ucptr
[0] = value
>> 24 ;
2493 } /* d2bes_clip_array */
2496 pcm_write_d2bes (SF_PRIVATE
*psf
, const double *ptr
, sf_count_t len
)
2497 { void (*convert
) (const double *, short *, int, int) ;
2498 int bufferlen
, writecount
;
2499 sf_count_t total
= 0 ;
2501 convert
= (psf
->add_clipping
) ? d2bes_clip_array
: d2bes_array
;
2502 bufferlen
= ARRAY_LEN (psf
->u
.sbuf
) ;
2505 { if (len
< bufferlen
)
2506 bufferlen
= (int) len
;
2507 convert (ptr
+ total
, psf
->u
.sbuf
, bufferlen
, psf
->norm_double
) ;
2508 writecount
= psf_fwrite (psf
->u
.sbuf
, sizeof (short), bufferlen
, psf
) ;
2509 total
+= writecount
;
2510 if (writecount
< bufferlen
)
2516 } /* pcm_write_d2bes */
2518 /*==============================================================================
2522 d2les_array (const double *src
, short *dest
, int count
, int normalize
)
2523 { unsigned char *ucptr
;
2527 normfact
= normalize
? (1.0 * 0x7FFF) : 1.0 ;
2528 ucptr
= ((unsigned char*) dest
) + 2 * count
;
2530 while (--count
>= 0)
2532 value
= lrint (src
[count
] * normfact
) ;
2534 ucptr
[1] = value
>> 8 ;
2539 d2les_clip_array (const double *src
, short *dest
, int count
, int normalize
)
2540 { unsigned char *ucptr
;
2542 double normfact
, scaled_value
;
2544 normfact
= normalize
? (8.0 * 0x10000000) : (1.0 * 0x10000) ;
2545 ucptr
= ((unsigned char*) dest
) + 2 * count
;
2547 while (--count
>= 0)
2549 scaled_value
= src
[count
] * normfact
;
2550 if (CPU_CLIPS_POSITIVE
== 0 && scaled_value
>= (1.0 * 0x7FFFFFFF))
2551 { ucptr
[0] = 0xFF ;
2555 if (CPU_CLIPS_NEGATIVE
== 0 && scaled_value
<= (-8.0 * 0x10000000))
2556 { ucptr
[0] = 0x00 ;
2561 value
= lrint (scaled_value
) ;
2562 ucptr
[0] = value
>> 16 ;
2563 ucptr
[1] = value
>> 24 ;
2565 } /* d2les_clip_array */
2568 pcm_write_d2les (SF_PRIVATE
*psf
, const double *ptr
, sf_count_t len
)
2569 { void (*convert
) (const double *, short *, int, int) ;
2570 int bufferlen
, writecount
;
2571 sf_count_t total
= 0 ;
2573 convert
= (psf
->add_clipping
) ? d2les_clip_array
: d2les_array
;
2574 bufferlen
= ARRAY_LEN (psf
->u
.sbuf
) ;
2577 { if (len
< bufferlen
)
2578 bufferlen
= (int) len
;
2579 convert (ptr
+ total
, psf
->u
.sbuf
, bufferlen
, psf
->norm_double
) ;
2580 writecount
= psf_fwrite (psf
->u
.sbuf
, sizeof (short), bufferlen
, psf
) ;
2581 total
+= writecount
;
2582 if (writecount
< bufferlen
)
2588 } /* pcm_write_d2les */
2590 /*==============================================================================
2594 d2let_array (const double *src
, tribyte
*dest
, int count
, int normalize
)
2595 { unsigned char *ucptr
;
2599 normfact
= normalize
? (1.0 * 0x7FFFFF) : 1.0 ;
2600 ucptr
= ((unsigned char*) dest
) + 3 * count
;
2602 while (--count
>= 0)
2604 value
= lrint (src
[count
] * normfact
) ;
2606 ucptr
[1] = value
>> 8 ;
2607 ucptr
[2] = value
>> 16 ;
2612 d2let_clip_array (const double *src
, tribyte
*dest
, int count
, int normalize
)
2613 { unsigned char *ucptr
;
2615 double normfact
, scaled_value
;
2617 normfact
= normalize
? (8.0 * 0x10000000) : (1.0 * 0x100) ;
2618 ucptr
= ((unsigned char*) dest
) + 3 * count
;
2620 while (--count
>= 0)
2622 scaled_value
= src
[count
] * normfact
;
2623 if (CPU_CLIPS_POSITIVE
== 0 && scaled_value
>= (1.0 * 0x7FFFFFFF))
2624 { ucptr
[0] = 0xFF ;
2629 if (CPU_CLIPS_NEGATIVE
== 0 && scaled_value
<= (-8.0 * 0x10000000))
2630 { ucptr
[0] = 0x00 ;
2636 value
= lrint (scaled_value
) ;
2637 ucptr
[0] = value
>> 8 ;
2638 ucptr
[1] = value
>> 16 ;
2639 ucptr
[2] = value
>> 24 ;
2641 } /* d2let_clip_array */
2644 pcm_write_d2let (SF_PRIVATE
*psf
, const double *ptr
, sf_count_t len
)
2645 { void (*convert
) (const double *, tribyte
*, int, int) ;
2646 int bufferlen
, writecount
;
2647 sf_count_t total
= 0 ;
2649 convert
= (psf
->add_clipping
) ? d2let_clip_array
: d2let_array
;
2650 bufferlen
= sizeof (psf
->u
.ucbuf
) / SIZEOF_TRIBYTE
;
2653 { if (len
< bufferlen
)
2654 bufferlen
= (int) len
;
2655 convert (ptr
+ total
, (tribyte
*) (psf
->u
.ucbuf
), bufferlen
, psf
->norm_double
) ;
2656 writecount
= psf_fwrite (psf
->u
.ucbuf
, SIZEOF_TRIBYTE
, bufferlen
, psf
) ;
2657 total
+= writecount
;
2658 if (writecount
< bufferlen
)
2664 } /* pcm_write_d2let */
2666 /*==============================================================================
2670 d2bet_array (const double *src
, tribyte
*dest
, int count
, int normalize
)
2671 { unsigned char *ucptr
;
2675 normfact
= normalize
? (1.0 * 0x7FFFFF) : 1.0 ;
2676 ucptr
= ((unsigned char*) dest
) + 3 * count
;
2678 while (--count
>= 0)
2680 value
= lrint (src
[count
] * normfact
) ;
2682 ucptr
[1] = value
>> 8 ;
2683 ucptr
[0] = value
>> 16 ;
2688 d2bet_clip_array (const double *src
, tribyte
*dest
, int count
, int normalize
)
2689 { unsigned char *ucptr
;
2691 double normfact
, scaled_value
;
2693 normfact
= normalize
? (8.0 * 0x10000000) : (1.0 * 0x100) ;
2694 ucptr
= ((unsigned char*) dest
) + 3 * count
;
2696 while (--count
>= 0)
2698 scaled_value
= src
[count
] * normfact
;
2699 if (CPU_CLIPS_POSITIVE
== 0 && scaled_value
>= (1.0 * 0x7FFFFFFF))
2700 { ucptr
[2] = 0xFF ;
2705 if (CPU_CLIPS_NEGATIVE
== 0 && scaled_value
<= (-8.0 * 0x10000000))
2706 { ucptr
[2] = 0x00 ;
2712 value
= lrint (scaled_value
) ;
2713 ucptr
[2] = value
>> 8 ;
2714 ucptr
[1] = value
>> 16 ;
2715 ucptr
[0] = value
>> 24 ;
2717 } /* d2bet_clip_array */
2720 pcm_write_d2bet (SF_PRIVATE
*psf
, const double *ptr
, sf_count_t len
)
2721 { void (*convert
) (const double *, tribyte
*, int, int) ;
2722 int bufferlen
, writecount
;
2723 sf_count_t total
= 0 ;
2725 convert
= (psf
->add_clipping
) ? d2bet_clip_array
: d2bet_array
;
2726 bufferlen
= sizeof (psf
->u
.ucbuf
) / SIZEOF_TRIBYTE
;
2729 { if (len
< bufferlen
)
2730 bufferlen
= (int) len
;
2731 convert (ptr
+ total
, (tribyte
*) (psf
->u
.ucbuf
), bufferlen
, psf
->norm_double
) ;
2732 writecount
= psf_fwrite (psf
->u
.ucbuf
, SIZEOF_TRIBYTE
, bufferlen
, psf
) ;
2733 total
+= writecount
;
2734 if (writecount
< bufferlen
)
2740 } /* pcm_write_d2bet */
2742 /*==============================================================================
2746 d2bei_array (const double *src
, int *dest
, int count
, int normalize
)
2747 { unsigned char *ucptr
;
2751 normfact
= normalize
? (1.0 * 0x7FFFFFFF) : 1.0 ;
2752 ucptr
= ((unsigned char*) dest
) + 4 * count
;
2754 while (--count
>= 0)
2756 value
= lrint (src
[count
] * normfact
) ;
2757 ucptr
[0] = value
>> 24 ;
2758 ucptr
[1] = value
>> 16 ;
2759 ucptr
[2] = value
>> 8 ;
2765 d2bei_clip_array (const double *src
, int *dest
, int count
, int normalize
)
2766 { unsigned char *ucptr
;
2768 double normfact
, scaled_value
;
2770 normfact
= normalize
? (8.0 * 0x10000000) : 1.0 ;
2771 ucptr
= ((unsigned char*) dest
) + 4 * count
;
2773 while (--count
>= 0)
2775 scaled_value
= src
[count
] * normfact
;
2776 if (CPU_CLIPS_POSITIVE
== 0 && scaled_value
>= (1.0 * 0x7FFFFFFF))
2777 { ucptr
[3] = 0xFF ;
2783 if (CPU_CLIPS_NEGATIVE
== 0 && scaled_value
<= (-8.0 * 0x10000000))
2784 { ucptr
[3] = 0x00 ;
2791 value
= lrint (scaled_value
) ;
2792 ucptr
[0] = value
>> 24 ;
2793 ucptr
[1] = value
>> 16 ;
2794 ucptr
[2] = value
>> 8 ;
2797 } /* d2bei_clip_array */
2800 pcm_write_d2bei (SF_PRIVATE
*psf
, const double *ptr
, sf_count_t len
)
2801 { void (*convert
) (const double *, int *, int, int) ;
2802 int bufferlen
, writecount
;
2803 sf_count_t total
= 0 ;
2805 convert
= (psf
->add_clipping
) ? d2bei_clip_array
: d2bei_array
;
2806 bufferlen
= ARRAY_LEN (psf
->u
.ibuf
) ;
2809 { if (len
< bufferlen
)
2810 bufferlen
= (int) len
;
2811 convert (ptr
+ total
, psf
->u
.ibuf
, bufferlen
, psf
->norm_double
) ;
2812 writecount
= psf_fwrite (psf
->u
.ibuf
, sizeof (int), bufferlen
, psf
) ;
2813 total
+= writecount
;
2814 if (writecount
< bufferlen
)
2820 } /* pcm_write_d2bei */
2822 /*==============================================================================
2826 d2lei_array (const double *src
, int *dest
, int count
, int normalize
)
2827 { unsigned char *ucptr
;
2831 normfact
= normalize
? (1.0 * 0x7FFFFFFF) : 1.0 ;
2832 ucptr
= ((unsigned char*) dest
) + 4 * count
;
2834 while (--count
>= 0)
2836 value
= lrint (src
[count
] * normfact
) ;
2838 ucptr
[1] = value
>> 8 ;
2839 ucptr
[2] = value
>> 16 ;
2840 ucptr
[3] = value
>> 24 ;
2845 d2lei_clip_array (const double *src
, int *dest
, int count
, int normalize
)
2846 { unsigned char *ucptr
;
2848 double normfact
, scaled_value
;
2850 normfact
= normalize
? (8.0 * 0x10000000) : 1.0 ;
2851 ucptr
= ((unsigned char*) dest
) + 4 * count
;
2853 while (--count
>= 0)
2855 scaled_value
= src
[count
] * normfact
;
2856 if (CPU_CLIPS_POSITIVE
== 0 && scaled_value
>= (1.0 * 0x7FFFFFFF))
2857 { ucptr
[0] = 0xFF ;
2863 if (CPU_CLIPS_NEGATIVE
== 0 && scaled_value
<= (-8.0 * 0x10000000))
2864 { ucptr
[0] = 0x00 ;
2871 value
= lrint (scaled_value
) ;
2873 ucptr
[1] = value
>> 8 ;
2874 ucptr
[2] = value
>> 16 ;
2875 ucptr
[3] = value
>> 24 ;
2877 } /* d2lei_clip_array */
2880 pcm_write_d2lei (SF_PRIVATE
*psf
, const double *ptr
, sf_count_t len
)
2881 { void (*convert
) (const double *, int *, int, int) ;
2882 int bufferlen
, writecount
;
2883 sf_count_t total
= 0 ;
2885 convert
= (psf
->add_clipping
) ? d2lei_clip_array
: d2lei_array
;
2886 bufferlen
= ARRAY_LEN (psf
->u
.ibuf
) ;
2889 { if (len
< bufferlen
)
2890 bufferlen
= (int) len
;
2891 convert (ptr
+ total
, psf
->u
.ibuf
, bufferlen
, psf
->norm_double
) ;
2892 writecount
= psf_fwrite (psf
->u
.ibuf
, sizeof (int), bufferlen
, psf
) ;
2893 total
+= writecount
;
2894 if (writecount
< bufferlen
)
2900 } /* pcm_write_d2lei */