1 /*
2 ** Copyright (C) 1999-2016 Erik de Castro Lopo <erikd@mega-nerd.com>
3 **
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.
8 **
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.
13 **
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.
17 */
18 
19 #include	"sfconfig.h"
20 
21 #include <math.h>
22 
23 #include	"sndfile.h"
24 #include	"sfendian.h"
25 #include	"common.h"
26 
27 /* Need to be able to handle 3 byte (24 bit) integers. So defined a
28 ** type and use SIZEOF_TRIBYTE instead of (tribyte).
29 */
30 
31 typedef	struct tribyte
32 {	uint8_t bytes [3] ;
33 	} tribyte ;
34 
35 #define	SIZEOF_TRIBYTE	3
36 
37 static sf_count_t	pcm_read_sc2s	(SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
38 static sf_count_t	pcm_read_uc2s	(SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
39 static sf_count_t	pcm_read_bes2s	(SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
40 static sf_count_t	pcm_read_les2s	(SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
41 static sf_count_t	pcm_read_bet2s	(SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
42 static sf_count_t	pcm_read_let2s	(SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
43 static sf_count_t	pcm_read_bei2s	(SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
44 static sf_count_t	pcm_read_lei2s	(SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
45 
46 static sf_count_t	pcm_read_sc2i	(SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
47 static sf_count_t	pcm_read_uc2i	(SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
48 static sf_count_t	pcm_read_bes2i	(SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
49 static sf_count_t	pcm_read_les2i	(SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
50 static sf_count_t	pcm_read_bet2i	(SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
51 static sf_count_t	pcm_read_let2i	(SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
52 static sf_count_t	pcm_read_bei2i	(SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
53 static sf_count_t	pcm_read_lei2i	(SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
54 
55 static sf_count_t	pcm_read_sc2f	(SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
56 static sf_count_t	pcm_read_uc2f	(SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
57 static sf_count_t	pcm_read_bes2f	(SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
58 static sf_count_t	pcm_read_les2f	(SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
59 static sf_count_t	pcm_read_bet2f	(SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
60 static sf_count_t	pcm_read_let2f	(SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
61 static sf_count_t	pcm_read_bei2f	(SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
62 static sf_count_t	pcm_read_lei2f	(SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
63 
64 static sf_count_t	pcm_read_sc2d	(SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
65 static sf_count_t	pcm_read_uc2d	(SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
66 static sf_count_t	pcm_read_bes2d	(SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
67 static sf_count_t	pcm_read_les2d	(SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
68 static sf_count_t	pcm_read_bet2d	(SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
69 static sf_count_t	pcm_read_let2d	(SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
70 static sf_count_t	pcm_read_bei2d	(SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
71 static sf_count_t	pcm_read_lei2d	(SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
72 
73 static sf_count_t	pcm_write_s2sc	(SF_PRIVATE *psf, const short *ptr, sf_count_t len) ;
74 static sf_count_t	pcm_write_s2uc	(SF_PRIVATE *psf, const short *ptr, sf_count_t len) ;
75 static sf_count_t	pcm_write_s2bes (SF_PRIVATE *psf, const short *ptr, sf_count_t len) ;
76 static sf_count_t	pcm_write_s2les (SF_PRIVATE *psf, const short *ptr, sf_count_t len) ;
77 static sf_count_t	pcm_write_s2bet (SF_PRIVATE *psf, const short *ptr, sf_count_t len) ;
78 static sf_count_t	pcm_write_s2let (SF_PRIVATE *psf, const short *ptr, sf_count_t len) ;
79 static sf_count_t	pcm_write_s2bei (SF_PRIVATE *psf, const short *ptr, sf_count_t len) ;
80 static sf_count_t	pcm_write_s2lei (SF_PRIVATE *psf, const short *ptr, sf_count_t len) ;
81 
82 static sf_count_t	pcm_write_i2sc	(SF_PRIVATE *psf, const int *ptr, sf_count_t len) ;
83 static sf_count_t	pcm_write_i2uc	(SF_PRIVATE *psf, const int *ptr, sf_count_t len) ;
84 static sf_count_t	pcm_write_i2bes (SF_PRIVATE *psf, const int *ptr, sf_count_t len) ;
85 static sf_count_t	pcm_write_i2les (SF_PRIVATE *psf, const int *ptr, sf_count_t len) ;
86 static sf_count_t	pcm_write_i2bet (SF_PRIVATE *psf, const int *ptr, sf_count_t len) ;
87 static sf_count_t	pcm_write_i2let (SF_PRIVATE *psf, const int *ptr, sf_count_t len) ;
88 static sf_count_t	pcm_write_i2bei (SF_PRIVATE *psf, const int *ptr, sf_count_t len) ;
89 static sf_count_t	pcm_write_i2lei (SF_PRIVATE *psf, const int *ptr, sf_count_t len) ;
90 
91 static sf_count_t	pcm_write_f2sc	(SF_PRIVATE *psf, const float *ptr, sf_count_t len) ;
92 static sf_count_t	pcm_write_f2uc	(SF_PRIVATE *psf, const float *ptr, sf_count_t len) ;
93 static sf_count_t	pcm_write_f2bes (SF_PRIVATE *psf, const float *ptr, sf_count_t len) ;
94 static sf_count_t	pcm_write_f2les (SF_PRIVATE *psf, const float *ptr, sf_count_t len) ;
95 static sf_count_t	pcm_write_f2bet (SF_PRIVATE *psf, const float *ptr, sf_count_t len) ;
96 static sf_count_t	pcm_write_f2let (SF_PRIVATE *psf, const float *ptr, sf_count_t len) ;
97 static sf_count_t	pcm_write_f2bei (SF_PRIVATE *psf, const float *ptr, sf_count_t len) ;
98 static sf_count_t	pcm_write_f2lei (SF_PRIVATE *psf, const float *ptr, sf_count_t len) ;
99 
100 static sf_count_t	pcm_write_d2sc	(SF_PRIVATE *psf, const double *ptr, sf_count_t len) ;
101 static sf_count_t	pcm_write_d2uc	(SF_PRIVATE *psf, const double *ptr, sf_count_t len) ;
102 static sf_count_t	pcm_write_d2bes (SF_PRIVATE *psf, const double *ptr, sf_count_t len) ;
103 static sf_count_t	pcm_write_d2les (SF_PRIVATE *psf, const double *ptr, sf_count_t len) ;
104 static sf_count_t	pcm_write_d2bet (SF_PRIVATE *psf, const double *ptr, sf_count_t len) ;
105 static sf_count_t	pcm_write_d2let (SF_PRIVATE *psf, const double *ptr, sf_count_t len) ;
106 static sf_count_t	pcm_write_d2bei (SF_PRIVATE *psf, const double *ptr, sf_count_t len) ;
107 static sf_count_t	pcm_write_d2lei (SF_PRIVATE *psf, const double *ptr, sf_count_t len) ;
108 
109 /*-----------------------------------------------------------------------------------------------
110 */
111 
112 enum
113 {	/* Char type for 8 bit files. */
114 	SF_CHARS_SIGNED		= 200,
115 	SF_CHARS_UNSIGNED	= 201
116 } ;
117 
118 /*-----------------------------------------------------------------------------------------------
119 */
120 
121 int
pcm_init(SF_PRIVATE * psf)122 pcm_init (SF_PRIVATE *psf)
123 {	int chars = 0 ;
124 
125 	if (psf->bytewidth == 0 || psf->sf.channels == 0)
126 	{	psf_log_printf (psf, "pcm_init : internal error : bytewitdh = %d, channels = %d\n", psf->bytewidth, psf->sf.channels) ;
127 		return SFE_INTERNAL ;
128 		} ;
129 
130 	psf->blockwidth = psf->bytewidth * psf->sf.channels ;
131 
132 	if ((SF_CODEC (psf->sf.format)) == SF_FORMAT_PCM_S8)
133 		chars = SF_CHARS_SIGNED ;
134 	else if ((SF_CODEC (psf->sf.format)) == SF_FORMAT_PCM_U8)
135 		chars = SF_CHARS_UNSIGNED ;
136 
137 	if (CPU_IS_BIG_ENDIAN)
138 		psf->data_endswap = (psf->endian == SF_ENDIAN_BIG) ? SF_FALSE : SF_TRUE ;
139 	else
140 		psf->data_endswap = (psf->endian == SF_ENDIAN_LITTLE) ? SF_FALSE : SF_TRUE ;
141 
142 	if (psf->file.mode == SFM_READ || psf->file.mode == SFM_RDWR)
143 	{	switch (psf->bytewidth * 0x10000 + psf->endian + chars)
144 		{	case (0x10000 + SF_ENDIAN_BIG + SF_CHARS_SIGNED) :
145 			case (0x10000 + SF_ENDIAN_LITTLE + SF_CHARS_SIGNED) :
146 					psf->read_short		= pcm_read_sc2s ;
147 					psf->read_int		= pcm_read_sc2i ;
148 					psf->read_float		= pcm_read_sc2f ;
149 					psf->read_double	= pcm_read_sc2d ;
150 					break ;
151 			case (0x10000 + SF_ENDIAN_BIG + SF_CHARS_UNSIGNED) :
152 			case (0x10000 + SF_ENDIAN_LITTLE + SF_CHARS_UNSIGNED) :
153 					psf->read_short		= pcm_read_uc2s ;
154 					psf->read_int		= pcm_read_uc2i ;
155 					psf->read_float		= pcm_read_uc2f ;
156 					psf->read_double	= pcm_read_uc2d ;
157 					break ;
158 
159 			case (2 * 0x10000 + SF_ENDIAN_BIG) :
160 					psf->read_short		= pcm_read_bes2s ;
161 					psf->read_int		= pcm_read_bes2i ;
162 					psf->read_float		= pcm_read_bes2f ;
163 					psf->read_double	= pcm_read_bes2d ;
164 					break ;
165 			case (3 * 0x10000 + SF_ENDIAN_BIG) :
166 					psf->read_short		= pcm_read_bet2s ;
167 					psf->read_int		= pcm_read_bet2i ;
168 					psf->read_float		= pcm_read_bet2f ;
169 					psf->read_double	= pcm_read_bet2d ;
170 					break ;
171 			case (4 * 0x10000 + SF_ENDIAN_BIG) :
172 
173 					psf->read_short		= pcm_read_bei2s ;
174 					psf->read_int		= pcm_read_bei2i ;
175 					psf->read_float		= pcm_read_bei2f ;
176 					psf->read_double	= pcm_read_bei2d ;
177 					break ;
178 
179 			case (2 * 0x10000 + SF_ENDIAN_LITTLE) :
180 					psf->read_short		= pcm_read_les2s ;
181 					psf->read_int		= pcm_read_les2i ;
182 					psf->read_float		= pcm_read_les2f ;
183 					psf->read_double	= pcm_read_les2d ;
184 					break ;
185 			case (3 * 0x10000 + SF_ENDIAN_LITTLE) :
186 					psf->read_short		= pcm_read_let2s ;
187 					psf->read_int		= pcm_read_let2i ;
188 					psf->read_float		= pcm_read_let2f ;
189 					psf->read_double	= pcm_read_let2d ;
190 					break ;
191 			case (4 * 0x10000 + SF_ENDIAN_LITTLE) :
192 					psf->read_short		= pcm_read_lei2s ;
193 					psf->read_int		= pcm_read_lei2i ;
194 					psf->read_float		= pcm_read_lei2f ;
195 					psf->read_double	= pcm_read_lei2d ;
196 					break ;
197 			default :
198 				psf_log_printf (psf, "pcm.c returning SFE_UNIMPLEMENTED\nbytewidth %d    endian %d\n", psf->bytewidth, psf->endian) ;
199 				return SFE_UNIMPLEMENTED ;
200 			} ;
201 		} ;
202 
203 	if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
204 	{	switch (psf->bytewidth * 0x10000 + psf->endian + chars)
205 		{	case (0x10000 + SF_ENDIAN_BIG + SF_CHARS_SIGNED) :
206 			case (0x10000 + SF_ENDIAN_LITTLE + SF_CHARS_SIGNED) :
207 					psf->write_short	= pcm_write_s2sc ;
208 					psf->write_int		= pcm_write_i2sc ;
209 					psf->write_float	= pcm_write_f2sc ;
210 					psf->write_double	= pcm_write_d2sc ;
211 					break ;
212 			case (0x10000 + SF_ENDIAN_BIG + SF_CHARS_UNSIGNED) :
213 			case (0x10000 + SF_ENDIAN_LITTLE + SF_CHARS_UNSIGNED) :
214 					psf->write_short	= pcm_write_s2uc ;
215 					psf->write_int		= pcm_write_i2uc ;
216 					psf->write_float	= pcm_write_f2uc ;
217 					psf->write_double	= pcm_write_d2uc ;
218 					break ;
219 
220 			case (2 * 0x10000 + SF_ENDIAN_BIG) :
221 					psf->write_short	= pcm_write_s2bes ;
222 					psf->write_int		= pcm_write_i2bes ;
223 					psf->write_float	= pcm_write_f2bes ;
224 					psf->write_double	= pcm_write_d2bes ;
225 					break ;
226 
227 			case (3 * 0x10000 + SF_ENDIAN_BIG) :
228 					psf->write_short	= pcm_write_s2bet ;
229 					psf->write_int		= pcm_write_i2bet ;
230 					psf->write_float	= pcm_write_f2bet ;
231 					psf->write_double	= pcm_write_d2bet ;
232 					break ;
233 
234 			case (4 * 0x10000 + SF_ENDIAN_BIG) :
235 					psf->write_short	= pcm_write_s2bei ;
236 					psf->write_int		= pcm_write_i2bei ;
237 					psf->write_float	= pcm_write_f2bei ;
238 					psf->write_double	= pcm_write_d2bei ;
239 					break ;
240 
241 			case (2 * 0x10000 + SF_ENDIAN_LITTLE) :
242 					psf->write_short	= pcm_write_s2les ;
243 					psf->write_int		= pcm_write_i2les ;
244 					psf->write_float	= pcm_write_f2les ;
245 					psf->write_double	= pcm_write_d2les ;
246 					break ;
247 
248 			case (3 * 0x10000 + SF_ENDIAN_LITTLE) :
249 					psf->write_short	= pcm_write_s2let ;
250 					psf->write_int		= pcm_write_i2let ;
251 					psf->write_float	= pcm_write_f2let ;
252 					psf->write_double	= pcm_write_d2let ;
253 					break ;
254 
255 			case (4 * 0x10000 + SF_ENDIAN_LITTLE) :
256 					psf->write_short	= pcm_write_s2lei ;
257 					psf->write_int		= pcm_write_i2lei ;
258 					psf->write_float	= pcm_write_f2lei ;
259 					psf->write_double	= pcm_write_d2lei ;
260 					break ;
261 
262 			default :
263 				psf_log_printf (psf, "pcm.c returning SFE_UNIMPLEMENTED\nbytewidth %d    endian %d\n", psf->bytewidth, psf->endian) ;
264 				return SFE_UNIMPLEMENTED ;
265 			} ;
266 
267 		} ;
268 
269 	if (psf->filelength > psf->dataoffset)
270 	{	psf->datalength = (psf->dataend > 0) ? psf->dataend - psf->dataoffset :
271 							psf->filelength - psf->dataoffset ;
272 		}
273 	else
274 		psf->datalength = 0 ;
275 
276 	psf->sf.frames = psf->blockwidth > 0 ? psf->datalength / psf->blockwidth : 0 ;
277 
278 	return 0 ;
279 } /* pcm_init */
280 
281 /*==============================================================================
282 */
283 
284 static inline void
sc2s_array(const signed char * src,int count,short * dest)285 sc2s_array	(const signed char *src, int count, short *dest)
286 {	while (--count >= 0)
287 	{	dest [count] = ((uint16_t) src [count]) << 8 ;
288 		} ;
289 } /* sc2s_array */
290 
291 static inline void
uc2s_array(const unsigned char * src,int count,short * dest)292 uc2s_array	(const unsigned char *src, int count, short *dest)
293 {	while (--count >= 0)
294 	{	dest [count] = (((uint32_t) src [count]) - 0x80) << 8 ;
295 		} ;
296 } /* uc2s_array */
297 
298 static inline void
let2s_array(const tribyte * src,int count,short * dest)299 let2s_array (const tribyte *src, int count, short *dest)
300 {	while (--count >= 0)
301 		dest [count] = LET2H_16_PTR (src [count].bytes) ;
302 } /* let2s_array */
303 
304 static inline void
bet2s_array(const tribyte * src,int count,short * dest)305 bet2s_array (const tribyte *src, int count, short *dest)
306 {	while (--count >= 0)
307 		dest [count] = BET2H_16_PTR (src [count].bytes) ;
308 } /* bet2s_array */
309 
310 static inline void
lei2s_array(const int * src,int count,short * dest)311 lei2s_array (const int *src, int count, short *dest)
312 {	int value ;
313 
314 	while (--count >= 0)
315 	{	value = LE2H_32 (src [count]) ;
316 		dest [count] = value >> 16 ;
317 		} ;
318 } /* lei2s_array */
319 
320 static inline void
bei2s_array(const int * src,int count,short * dest)321 bei2s_array (const int *src, int count, short *dest)
322 {	int value ;
323 
324 	while (--count >= 0)
325 	{	value = BE2H_32 (src [count]) ;
326 		dest [count] = value >> 16 ;
327 		} ;
328 } /* bei2s_array */
329 
330 /*--------------------------------------------------------------------------
331 */
332 
333 static inline void
sc2i_array(const signed char * src,int count,int * dest)334 sc2i_array	(const signed char *src, int count, int *dest)
335 {	while (--count >= 0)
336 	{	dest [count] = arith_shift_left ((int) src [count], 24) ;
337 		} ;
338 } /* sc2i_array */
339 
340 static inline void
uc2i_array(const unsigned char * src,int count,int * dest)341 uc2i_array	(const unsigned char *src, int count, int *dest)
342 {	while (--count >= 0)
343 	{	dest [count] = arith_shift_left (((int) src [count]) - 128, 24) ;
344 		} ;
345 } /* uc2i_array */
346 
347 static inline void
bes2i_array(const short * src,int count,int * dest)348 bes2i_array (const short *src, int count, int *dest)
349 {	short value ;
350 
351 	while (--count >= 0)
352 	{	value = BE2H_16 (src [count]) ;
353 		dest [count] = arith_shift_left (value, 16) ;
354 		} ;
355 } /* bes2i_array */
356 
357 static inline void
les2i_array(const short * src,int count,int * dest)358 les2i_array (const short *src, int count, int *dest)
359 {	short value ;
360 
361 	while (--count >= 0)
362 	{	value = LE2H_16 (src [count]) ;
363 		dest [count] = arith_shift_left (value, 16) ;
364 		} ;
365 } /* les2i_array */
366 
367 static inline void
bet2i_array(const tribyte * src,int count,int * dest)368 bet2i_array (const tribyte *src, int count, int *dest)
369 {	while (--count >= 0)
370 		dest [count] = psf_get_be24 (src [count].bytes, 0) ;
371 } /* bet2i_array */
372 
373 static inline void
let2i_array(const tribyte * src,int count,int * dest)374 let2i_array (const tribyte *src, int count, int *dest)
375 {	while (--count >= 0)
376 		dest [count] = psf_get_le24 (src [count].bytes, 0) ;
377 } /* let2i_array */
378 
379 /*--------------------------------------------------------------------------
380 */
381 
382 static inline void
sc2f_array(const signed char * src,int count,float * dest,float normfact)383 sc2f_array	(const signed char *src, int count, float *dest, float normfact)
384 {	while (--count >= 0)
385 		dest [count] = ((float) src [count]) * normfact ;
386 } /* sc2f_array */
387 
388 static inline void
uc2f_array(const unsigned char * src,int count,float * dest,float normfact)389 uc2f_array	(const unsigned char *src, int count, float *dest, float normfact)
390 {	while (--count >= 0)
391 		dest [count] = (((int) src [count]) - 128) * normfact ;
392 } /* uc2f_array */
393 
394 static inline void
les2f_array(const short * src,int count,float * dest,float normfact)395 les2f_array (const short *src, int count, float *dest, float normfact)
396 {	short	value ;
397 
398 	while (--count >= 0)
399 	{	value = src [count] ;
400 		value = LE2H_16 (value) ;
401 		dest [count] = ((float) value) * normfact ;
402 		} ;
403 } /* les2f_array */
404 
405 static inline void
bes2f_array(const short * src,int count,float * dest,float normfact)406 bes2f_array (const short *src, int count, float *dest, float normfact)
407 {	short			value ;
408 
409 	while (--count >= 0)
410 	{	value = src [count] ;
411 		value = BE2H_16 (value) ;
412 		dest [count] = ((float) value) * normfact ;
413 		} ;
414 } /* bes2f_array */
415 
416 static inline void
let2f_array(const tribyte * src,int count,float * dest,float normfact)417 let2f_array (const tribyte *src, int count, float *dest, float normfact)
418 {	int value ;
419 
420 	while (--count >= 0)
421 	{	value = psf_get_le24 (src [count].bytes, 0) ;
422 		dest [count] = ((float) value) * normfact ;
423 		} ;
424 } /* let2f_array */
425 
426 static inline void
bet2f_array(const tribyte * src,int count,float * dest,float normfact)427 bet2f_array (const tribyte *src, int count, float *dest, float normfact)
428 {	int value ;
429 
430 	while (--count >= 0)
431 	{	value = psf_get_be24 (src [count].bytes, 0) ;
432 		dest [count] = ((float) value) * normfact ;
433 		} ;
434 } /* bet2f_array */
435 
436 static inline void
lei2f_array(const int * src,int count,float * dest,float normfact)437 lei2f_array (const int *src, int count, float *dest, float normfact)
438 {	int 			value ;
439 
440 	while (--count >= 0)
441 	{	value = src [count] ;
442 		value = LE2H_32 (value) ;
443 		dest [count] = ((float) value) * normfact ;
444 		} ;
445 } /* lei2f_array */
446 
447 static inline void
bei2f_array(const int * src,int count,float * dest,float normfact)448 bei2f_array (const int *src, int count, float *dest, float normfact)
449 {	int 			value ;
450 
451 	while (--count >= 0)
452 	{	value = src [count] ;
453 		value = BE2H_32 (value) ;
454 		dest [count] = ((float) value) * normfact ;
455 		} ;
456 } /* bei2f_array */
457 
458 /*--------------------------------------------------------------------------
459 */
460 
461 static inline void
sc2d_array(const signed char * src,int count,double * dest,double normfact)462 sc2d_array	(const signed char *src, int count, double *dest, double normfact)
463 {	while (--count >= 0)
464 		dest [count] = ((double) src [count]) * normfact ;
465 } /* sc2d_array */
466 
467 static inline void
uc2d_array(const unsigned char * src,int count,double * dest,double normfact)468 uc2d_array	(const unsigned char *src, int count, double *dest, double normfact)
469 {	while (--count >= 0)
470 		dest [count] = (((int) src [count]) - 128) * normfact ;
471 } /* uc2d_array */
472 
473 static inline void
les2d_array(const short * src,int count,double * dest,double normfact)474 les2d_array (const short *src, int count, double *dest, double normfact)
475 {	short	value ;
476 
477 	while (--count >= 0)
478 	{	value = src [count] ;
479 		value = LE2H_16 (value) ;
480 		dest [count] = ((double) value) * normfact ;
481 		} ;
482 } /* les2d_array */
483 
484 static inline void
bes2d_array(const short * src,int count,double * dest,double normfact)485 bes2d_array (const short *src, int count, double *dest, double normfact)
486 {	short	value ;
487 
488 	while (--count >= 0)
489 	{	value = src [count] ;
490 		value = BE2H_16 (value) ;
491 		dest [count] = ((double) value) * normfact ;
492 		} ;
493 } /* bes2d_array */
494 
495 static inline void
let2d_array(const tribyte * src,int count,double * dest,double normfact)496 let2d_array (const tribyte *src, int count, double *dest, double normfact)
497 {	int value ;
498 
499 	while (--count >= 0)
500 	{	value = psf_get_le24 (src [count].bytes, 0) ;
501 		dest [count] = ((double) value) * normfact ;
502 		} ;
503 } /* let2d_array */
504 
505 static inline void
bet2d_array(const tribyte * src,int count,double * dest,double normfact)506 bet2d_array (const tribyte *src, int count, double *dest, double normfact)
507 {	int value ;
508 
509 	while (--count >= 0)
510 	{	value = psf_get_be24 (src [count].bytes, 0) ;
511 		dest [count] = ((double) value) * normfact ;
512 		} ;
513 } /* bet2d_array */
514 
515 static inline void
lei2d_array(const int * src,int count,double * dest,double normfact)516 lei2d_array (const int *src, int count, double *dest, double normfact)
517 {	int 	value ;
518 
519 	while (--count >= 0)
520 	{	value = src [count] ;
521 		value = LE2H_32 (value) ;
522 		dest [count] = ((double) value) * normfact ;
523 		} ;
524 } /* lei2d_array */
525 
526 static inline void
bei2d_array(const int * src,int count,double * dest,double normfact)527 bei2d_array (const int *src, int count, double *dest, double normfact)
528 {	int 	value ;
529 
530 	while (--count >= 0)
531 	{	value = src [count] ;
532 		value = BE2H_32 (value) ;
533 		dest [count] = ((double) value) * normfact ;
534 		} ;
535 } /* bei2d_array */
536 
537 /*--------------------------------------------------------------------------
538 */
539 
540 static inline void
s2sc_array(const short * src,signed char * dest,int count)541 s2sc_array	(const short *src, signed char *dest, int count)
542 {	while (--count >= 0)
543 		dest [count] = src [count] >> 8 ;
544 } /* s2sc_array */
545 
546 static inline void
s2uc_array(const short * src,unsigned char * dest,int count)547 s2uc_array	(const short *src, unsigned char *dest, int count)
548 {	while (--count >= 0)
549 		dest [count] = (src [count] >> 8) + 0x80 ;
550 } /* s2uc_array */
551 
552 static inline void
s2let_array(const short * src,tribyte * dest,int count)553 s2let_array (const short *src, tribyte *dest, int count)
554 {	while (--count >= 0)
555 	{	dest [count].bytes [0] = 0 ;
556 		dest [count].bytes [1] = src [count] ;
557 		dest [count].bytes [2] = src [count] >> 8 ;
558 		} ;
559 } /* s2let_array */
560 
561 static inline void
s2bet_array(const short * src,tribyte * dest,int count)562 s2bet_array (const short *src, tribyte *dest, int count)
563 {	while (--count >= 0)
564 	{	dest [count].bytes [2] = 0 ;
565 		dest [count].bytes [1] = src [count] ;
566 		dest [count].bytes [0] = src [count] >> 8 ;
567 		} ;
568 } /* s2bet_array */
569 
570 static inline void
s2lei_array(const short * src,int * dest,int count)571 s2lei_array (const short *src, int *dest, int count)
572 {	unsigned char	*ucptr ;
573 
574 	ucptr = ((unsigned char*) dest) + 4 * count ;
575 	while (--count >= 0)
576 	{	ucptr -= 4 ;
577 		ucptr [0] = 0 ;
578 		ucptr [1] = 0 ;
579 		ucptr [2] = src [count] ;
580 		ucptr [3] = src [count] >> 8 ;
581 		} ;
582 } /* s2lei_array */
583 
584 static inline void
s2bei_array(const short * src,int * dest,int count)585 s2bei_array (const short *src, int *dest, int count)
586 {	unsigned char	*ucptr ;
587 
588 	ucptr = ((unsigned char*) dest) + 4 * count ;
589 	while (--count >= 0)
590 	{	ucptr -= 4 ;
591 		ucptr [0] = src [count] >> 8 ;
592 		ucptr [1] = src [count] ;
593 		ucptr [2] = 0 ;
594 		ucptr [3] = 0 ;
595 		} ;
596 } /* s2bei_array */
597 
598 /*--------------------------------------------------------------------------
599 */
600 
601 static inline void
i2sc_array(const int * src,signed char * dest,int count)602 i2sc_array	(const int *src, signed char *dest, int count)
603 {	while (--count >= 0)
604 		dest [count] = (src [count] >> 24) ;
605 } /* i2sc_array */
606 
607 static inline void
i2uc_array(const int * src,unsigned char * dest,int count)608 i2uc_array	(const int *src, unsigned char *dest, int count)
609 {	while (--count >= 0)
610 		dest [count] = ((src [count] >> 24) + 128) ;
611 } /* i2uc_array */
612 
613 static inline void
i2bes_array(const int * src,short * dest,int count)614 i2bes_array (const int *src, short *dest, int count)
615 {	unsigned char	*ucptr ;
616 
617 	ucptr = ((unsigned char*) dest) + 2 * count ;
618 	while (--count >= 0)
619 	{	ucptr -= 2 ;
620 		ucptr [0] = src [count] >> 24 ;
621 		ucptr [1] = src [count] >> 16 ;
622 		} ;
623 } /* i2bes_array */
624 
625 static inline void
i2les_array(const int * src,short * dest,int count)626 i2les_array (const int *src, short *dest, int count)
627 {	unsigned char	*ucptr ;
628 
629 	ucptr = ((unsigned char*) dest) + 2 * count ;
630 	while (--count >= 0)
631 	{	ucptr -= 2 ;
632 		ucptr [0] = src [count] >> 16 ;
633 		ucptr [1] = src [count] >> 24 ;
634 		} ;
635 } /* i2les_array */
636 
637 static inline void
i2let_array(const int * src,tribyte * dest,int count)638 i2let_array (const int *src, tribyte *dest, int count)
639 {	int value ;
640 
641 	while (--count >= 0)
642 	{	value = src [count] >> 8 ;
643 		dest [count].bytes [0] = value ;
644 		dest [count].bytes [1] = value >> 8 ;
645 		dest [count].bytes [2] = value >> 16 ;
646 		} ;
647 } /* i2let_array */
648 
649 static inline void
i2bet_array(const int * src,tribyte * dest,int count)650 i2bet_array (const int *src, tribyte *dest, int count)
651 {	int value ;
652 
653 	while (--count >= 0)
654 	{	value = src [count] >> 8 ;
655 		dest [count].bytes [2] = value ;
656 		dest [count].bytes [1] = value >> 8 ;
657 		dest [count].bytes [0] = value >> 16 ;
658 		} ;
659 } /* i2bet_array */
660 
661 /*===============================================================================================
662 */
663 
664 static sf_count_t
pcm_read_sc2s(SF_PRIVATE * psf,short * ptr,sf_count_t len)665 pcm_read_sc2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
666 {	BUF_UNION	ubuf ;
667 	int			bufferlen, readcount ;
668 	sf_count_t	total = 0 ;
669 
670 	bufferlen = ARRAY_LEN (ubuf.scbuf) ;
671 
672 	while (len > 0)
673 	{	if (len < bufferlen)
674 			bufferlen = (int) len ;
675 		readcount = psf_fread (ubuf.scbuf, sizeof (signed char), bufferlen, psf) ;
676 		sc2s_array (ubuf.scbuf, readcount, ptr + total) ;
677 		total += readcount ;
678 		if (readcount < bufferlen)
679 			break ;
680 		len -= readcount ;
681 		} ;
682 
683 	return total ;
684 } /* pcm_read_sc2s */
685 
686 static sf_count_t
pcm_read_uc2s(SF_PRIVATE * psf,short * ptr,sf_count_t len)687 pcm_read_uc2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
688 {	BUF_UNION	ubuf ;
689 	int			bufferlen, readcount ;
690 	sf_count_t	total = 0 ;
691 
692 	bufferlen = ARRAY_LEN (ubuf.ucbuf) ;
693 
694 	while (len > 0)
695 	{	if (len < bufferlen)
696 			bufferlen = (int) len ;
697 		readcount = psf_fread (ubuf.ucbuf, sizeof (unsigned char), bufferlen, psf) ;
698 		uc2s_array (ubuf.ucbuf, readcount, ptr + total) ;
699 		total += readcount ;
700 		if (readcount < bufferlen)
701 			break ;
702 		len -= readcount ;
703 		} ;
704 
705 	return total ;
706 } /* pcm_read_uc2s */
707 
708 static sf_count_t
pcm_read_bes2s(SF_PRIVATE * psf,short * ptr,sf_count_t len)709 pcm_read_bes2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
710 {	int		total ;
711 
712 	total = psf_fread (ptr, sizeof (short), len, psf) ;
713 	if (CPU_IS_LITTLE_ENDIAN)
714 		endswap_short_array (ptr, len) ;
715 
716 	return total ;
717 } /* pcm_read_bes2s */
718 
719 static sf_count_t
pcm_read_les2s(SF_PRIVATE * psf,short * ptr,sf_count_t len)720 pcm_read_les2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
721 {	int		total ;
722 
723 	total = psf_fread (ptr, sizeof (short), len, psf) ;
724 	if (CPU_IS_BIG_ENDIAN)
725 		endswap_short_array (ptr, len) ;
726 
727 	return total ;
728 } /* pcm_read_les2s */
729 
730 static sf_count_t
pcm_read_bet2s(SF_PRIVATE * psf,short * ptr,sf_count_t len)731 pcm_read_bet2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
732 {	BUF_UNION	ubuf ;
733 	int			bufferlen, readcount ;
734 	sf_count_t	total = 0 ;
735 
736 	bufferlen = sizeof (ubuf.ucbuf) / SIZEOF_TRIBYTE ;
737 
738 	while (len > 0)
739 	{	if (len < bufferlen)
740 			bufferlen = (int) len ;
741 		readcount = psf_fread (ubuf.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ;
742 		bet2s_array ((tribyte*) (ubuf.ucbuf), readcount, ptr + total) ;
743 		total += readcount ;
744 		if (readcount < bufferlen)
745 			break ;
746 		len -= readcount ;
747 		} ;
748 
749 	return total ;
750 } /* pcm_read_bet2s */
751 
752 static sf_count_t
pcm_read_let2s(SF_PRIVATE * psf,short * ptr,sf_count_t len)753 pcm_read_let2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
754 {	BUF_UNION	ubuf ;
755 	int			bufferlen, readcount ;
756 	sf_count_t	total = 0 ;
757 
758 	bufferlen = sizeof (ubuf.ucbuf) / SIZEOF_TRIBYTE ;
759 
760 	while (len > 0)
761 	{	if (len < bufferlen)
762 			bufferlen = (int) len ;
763 		readcount = psf_fread (ubuf.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ;
764 		let2s_array ((tribyte*) (ubuf.ucbuf), readcount, ptr + total) ;
765 		total += readcount ;
766 		if (readcount < bufferlen)
767 			break ;
768 		len -= readcount ;
769 		} ;
770 
771 	return total ;
772 } /* pcm_read_let2s */
773 
774 static sf_count_t
pcm_read_bei2s(SF_PRIVATE * psf,short * ptr,sf_count_t len)775 pcm_read_bei2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
776 {	BUF_UNION	ubuf ;
777 	int			bufferlen, readcount ;
778 	sf_count_t	total = 0 ;
779 
780 	bufferlen = ARRAY_LEN (ubuf.ibuf) ;
781 
782 	while (len > 0)
783 	{	if (len < bufferlen)
784 			bufferlen = (int) len ;
785 		readcount = psf_fread (ubuf.ibuf, sizeof (int), bufferlen, psf) ;
786 		bei2s_array (ubuf.ibuf, readcount, ptr + total) ;
787 		total += readcount ;
788 		if (readcount < bufferlen)
789 			break ;
790 		len -= readcount ;
791 		} ;
792 
793 	return total ;
794 } /* pcm_read_bei2s */
795 
796 static sf_count_t
pcm_read_lei2s(SF_PRIVATE * psf,short * ptr,sf_count_t len)797 pcm_read_lei2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
798 {	BUF_UNION	ubuf ;
799 	int			bufferlen, readcount ;
800 	sf_count_t	total = 0 ;
801 
802 	bufferlen = ARRAY_LEN (ubuf.ibuf) ;
803 
804 	while (len > 0)
805 	{	if (len < bufferlen)
806 			bufferlen = (int) len ;
807 		readcount = psf_fread (ubuf.ibuf, sizeof (int), bufferlen, psf) ;
808 		lei2s_array (ubuf.ibuf, readcount, ptr + total) ;
809 		total += readcount ;
810 		if (readcount < bufferlen)
811 			break ;
812 		len -= readcount ;
813 		} ;
814 
815 	return total ;
816 } /* pcm_read_lei2s */
817 
818 /*-----------------------------------------------------------------------------------------------
819 */
820 
821 static sf_count_t
pcm_read_sc2i(SF_PRIVATE * psf,int * ptr,sf_count_t len)822 pcm_read_sc2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
823 {	BUF_UNION	ubuf ;
824 	int			bufferlen, readcount ;
825 	sf_count_t	total = 0 ;
826 
827 	bufferlen = ARRAY_LEN (ubuf.scbuf) ;
828 
829 	while (len > 0)
830 	{	if (len < bufferlen)
831 			bufferlen = (int) len ;
832 		readcount = psf_fread (ubuf.scbuf, sizeof (signed char), bufferlen, psf) ;
833 		sc2i_array (ubuf.scbuf, readcount, ptr + total) ;
834 		total += readcount ;
835 		if (readcount < bufferlen)
836 			break ;
837 		len -= readcount ;
838 		} ;
839 
840 	return total ;
841 } /* pcm_read_sc2i */
842 
843 static sf_count_t
pcm_read_uc2i(SF_PRIVATE * psf,int * ptr,sf_count_t len)844 pcm_read_uc2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
845 {	BUF_UNION	ubuf ;
846 	int			bufferlen, readcount ;
847 	sf_count_t	total = 0 ;
848 
849 	bufferlen = ARRAY_LEN (ubuf.ucbuf) ;
850 
851 	while (len > 0)
852 	{	if (len < bufferlen)
853 			bufferlen = (int) len ;
854 		readcount = psf_fread (ubuf.ucbuf, sizeof (unsigned char), bufferlen, psf) ;
855 		uc2i_array (ubuf.ucbuf, readcount, ptr + total) ;
856 		total += readcount ;
857 		if (readcount < bufferlen)
858 			break ;
859 		len -= readcount ;
860 		} ;
861 
862 	return total ;
863 } /* pcm_read_uc2i */
864 
865 static sf_count_t
pcm_read_bes2i(SF_PRIVATE * psf,int * ptr,sf_count_t len)866 pcm_read_bes2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
867 {	BUF_UNION	ubuf ;
868 	int			bufferlen, readcount ;
869 	sf_count_t	total = 0 ;
870 
871 	bufferlen = ARRAY_LEN (ubuf.sbuf) ;
872 
873 	while (len > 0)
874 	{	if (len < bufferlen)
875 			bufferlen = (int) len ;
876 		readcount = psf_fread (ubuf.sbuf, sizeof (short), bufferlen, psf) ;
877 		bes2i_array (ubuf.sbuf, readcount, ptr + total) ;
878 		total += readcount ;
879 		if (readcount < bufferlen)
880 			break ;
881 		len -= readcount ;
882 		} ;
883 
884 	return total ;
885 } /* pcm_read_bes2i */
886 
887 static sf_count_t
pcm_read_les2i(SF_PRIVATE * psf,int * ptr,sf_count_t len)888 pcm_read_les2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
889 {	BUF_UNION	ubuf ;
890 	int			bufferlen, readcount ;
891 	sf_count_t	total = 0 ;
892 
893 	bufferlen = ARRAY_LEN (ubuf.sbuf) ;
894 
895 	while (len > 0)
896 	{	if (len < bufferlen)
897 			bufferlen = (int) len ;
898 		readcount = psf_fread (ubuf.sbuf, sizeof (short), bufferlen, psf) ;
899 		les2i_array (ubuf.sbuf, readcount, ptr + total) ;
900 		total += readcount ;
901 		if (readcount < bufferlen)
902 			break ;
903 		len -= readcount ;
904 		} ;
905 
906 	return total ;
907 } /* pcm_read_les2i */
908 
909 static sf_count_t
pcm_read_bet2i(SF_PRIVATE * psf,int * ptr,sf_count_t len)910 pcm_read_bet2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
911 {	BUF_UNION	ubuf ;
912 	int			bufferlen, readcount ;
913 	sf_count_t	total = 0 ;
914 
915 	bufferlen = sizeof (ubuf.ucbuf) / SIZEOF_TRIBYTE ;
916 
917 	while (len > 0)
918 	{	if (len < bufferlen)
919 			bufferlen = (int) len ;
920 		readcount = psf_fread (ubuf.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ;
921 		bet2i_array ((tribyte*) (ubuf.ucbuf), readcount, ptr + total) ;
922 		total += readcount ;
923 		if (readcount < bufferlen)
924 			break ;
925 		len -= readcount ;
926 		} ;
927 
928 	return total ;
929 } /* pcm_read_bet2i */
930 
931 static sf_count_t
pcm_read_let2i(SF_PRIVATE * psf,int * ptr,sf_count_t len)932 pcm_read_let2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
933 {	BUF_UNION	ubuf ;
934 	int			bufferlen, readcount ;
935 	sf_count_t	total = 0 ;
936 
937 	bufferlen = sizeof (ubuf.ucbuf) / SIZEOF_TRIBYTE ;
938 
939 	while (len > 0)
940 	{	if (len < bufferlen)
941 			bufferlen = (int) len ;
942 		readcount = psf_fread (ubuf.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ;
943 		let2i_array ((tribyte*) (ubuf.ucbuf), readcount, ptr + total) ;
944 		total += readcount ;
945 		if (readcount < bufferlen)
946 			break ;
947 		len -= readcount ;
948 		} ;
949 
950 	return total ;
951 } /* pcm_read_let2i */
952 
953 static sf_count_t
pcm_read_bei2i(SF_PRIVATE * psf,int * ptr,sf_count_t len)954 pcm_read_bei2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
955 {	int		total ;
956 
957 	total = psf_fread (ptr, sizeof (int), len, psf) ;
958 	if (CPU_IS_LITTLE_ENDIAN)
959 		endswap_int_array	(ptr, len) ;
960 
961 	return total ;
962 } /* pcm_read_bei2i */
963 
964 static sf_count_t
pcm_read_lei2i(SF_PRIVATE * psf,int * ptr,sf_count_t len)965 pcm_read_lei2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
966 {	int		total ;
967 
968 	total = psf_fread (ptr, sizeof (int), len, psf) ;
969 	if (CPU_IS_BIG_ENDIAN)
970 		endswap_int_array	(ptr, len) ;
971 
972 	return total ;
973 } /* pcm_read_lei2i */
974 
975 /*-----------------------------------------------------------------------------------------------
976 */
977 
978 static sf_count_t
pcm_read_sc2f(SF_PRIVATE * psf,float * ptr,sf_count_t len)979 pcm_read_sc2f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
980 {	BUF_UNION	ubuf ;
981 	int			bufferlen, readcount ;
982 	sf_count_t	total = 0 ;
983 	float	normfact ;
984 
985 	normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x80) : 1.0 ;
986 
987 	bufferlen = ARRAY_LEN (ubuf.scbuf) ;
988 
989 	while (len > 0)
990 	{	if (len < bufferlen)
991 			bufferlen = (int) len ;
992 		readcount = psf_fread (ubuf.scbuf, sizeof (signed char), bufferlen, psf) ;
993 		sc2f_array (ubuf.scbuf, readcount, ptr + total, normfact) ;
994 		total += readcount ;
995 		if (readcount < bufferlen)
996 			break ;
997 		len -= readcount ;
998 		} ;
999 
1000 	return total ;
1001 } /* pcm_read_sc2f */
1002 
1003 static sf_count_t
pcm_read_uc2f(SF_PRIVATE * psf,float * ptr,sf_count_t len)1004 pcm_read_uc2f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
1005 {	BUF_UNION	ubuf ;
1006 	int			bufferlen, readcount ;
1007 	sf_count_t	total = 0 ;
1008 	float	normfact ;
1009 
1010 	normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x80) : 1.0 ;
1011 
1012 	bufferlen = ARRAY_LEN (ubuf.ucbuf) ;
1013 
1014 	while (len > 0)
1015 	{	if (len < bufferlen)
1016 			bufferlen = (int) len ;
1017 		readcount = psf_fread (ubuf.ucbuf, sizeof (unsigned char), bufferlen, psf) ;
1018 		uc2f_array (ubuf.ucbuf, readcount, ptr + total, normfact) ;
1019 		total += readcount ;
1020 		if (readcount < bufferlen)
1021 			break ;
1022 		len -= readcount ;
1023 		} ;
1024 
1025 	return total ;
1026 } /* pcm_read_uc2f */
1027 
1028 static sf_count_t
pcm_read_bes2f(SF_PRIVATE * psf,float * ptr,sf_count_t len)1029 pcm_read_bes2f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
1030 {	BUF_UNION	ubuf ;
1031 	int			bufferlen, readcount ;
1032 	sf_count_t	total = 0 ;
1033 	float	normfact ;
1034 
1035 	normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x8000) : 1.0 ;
1036 
1037 	bufferlen = ARRAY_LEN (ubuf.sbuf) ;
1038 
1039 	while (len > 0)
1040 	{	if (len < bufferlen)
1041 			bufferlen = (int) len ;
1042 		readcount = psf_fread (ubuf.sbuf, sizeof (short), bufferlen, psf) ;
1043 		bes2f_array (ubuf.sbuf, readcount, ptr + total, normfact) ;
1044 		total += readcount ;
1045 		if (readcount < bufferlen)
1046 			break ;
1047 		len -= readcount ;
1048 		} ;
1049 
1050 	return total ;
1051 } /* pcm_read_bes2f */
1052 
1053 static sf_count_t
pcm_read_les2f(SF_PRIVATE * psf,float * ptr,sf_count_t len)1054 pcm_read_les2f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
1055 {	BUF_UNION	ubuf ;
1056 	int			bufferlen, readcount ;
1057 	sf_count_t	total = 0 ;
1058 	float	normfact ;
1059 
1060 	normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x8000) : 1.0 ;
1061 
1062 	bufferlen = ARRAY_LEN (ubuf.sbuf) ;
1063 
1064 	while (len > 0)
1065 	{	if (len < bufferlen)
1066 			bufferlen = (int) len ;
1067 		readcount = psf_fread (ubuf.sbuf, sizeof (short), bufferlen, psf) ;
1068 		les2f_array (ubuf.sbuf, readcount, ptr + total, normfact) ;
1069 		total += readcount ;
1070 		if (readcount < bufferlen)
1071 			break ;
1072 		len -= readcount ;
1073 		} ;
1074 
1075 	return total ;
1076 } /* pcm_read_les2f */
1077 
1078 static sf_count_t
pcm_read_bet2f(SF_PRIVATE * psf,float * ptr,sf_count_t len)1079 pcm_read_bet2f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
1080 {	BUF_UNION	ubuf ;
1081 	int			bufferlen, readcount ;
1082 	sf_count_t	total = 0 ;
1083 	float	normfact ;
1084 
1085 	/* Special normfactor because tribyte value is read into an int. */
1086 	normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x80000000) : 1.0 / 256.0 ;
1087 
1088 	bufferlen = sizeof (ubuf.ucbuf) / SIZEOF_TRIBYTE ;
1089 
1090 	while (len > 0)
1091 	{	if (len < bufferlen)
1092 			bufferlen = (int) len ;
1093 		readcount = psf_fread (ubuf.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ;
1094 		bet2f_array ((tribyte*) (ubuf.ucbuf), readcount, ptr + total, normfact) ;
1095 		total += readcount ;
1096 		if (readcount < bufferlen)
1097 			break ;
1098 		len -= readcount ;
1099 		} ;
1100 
1101 	return total ;
1102 } /* pcm_read_bet2f */
1103 
1104 static sf_count_t
pcm_read_let2f(SF_PRIVATE * psf,float * ptr,sf_count_t len)1105 pcm_read_let2f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
1106 {	BUF_UNION	ubuf ;
1107 	int			bufferlen, readcount ;
1108 	sf_count_t	total = 0 ;
1109 	float	normfact ;
1110 
1111 	/* Special normfactor because tribyte value is read into an int. */
1112 	normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x80000000) : 1.0 / 256.0 ;
1113 
1114 	bufferlen = sizeof (ubuf.ucbuf) / SIZEOF_TRIBYTE ;
1115 
1116 	while (len > 0)
1117 	{	if (len < bufferlen)
1118 			bufferlen = (int) len ;
1119 		readcount = psf_fread (ubuf.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ;
1120 		let2f_array ((tribyte*) (ubuf.ucbuf), readcount, ptr + total, normfact) ;
1121 		total += readcount ;
1122 		if (readcount < bufferlen)
1123 			break ;
1124 		len -= readcount ;
1125 		} ;
1126 
1127 	return total ;
1128 } /* pcm_read_let2f */
1129 
1130 static sf_count_t
pcm_read_bei2f(SF_PRIVATE * psf,float * ptr,sf_count_t len)1131 pcm_read_bei2f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
1132 {	BUF_UNION	ubuf ;
1133 	int			bufferlen, readcount ;
1134 	sf_count_t	total = 0 ;
1135 	float	normfact ;
1136 
1137 	normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x80000000) : 1.0 ;
1138 
1139 	bufferlen = ARRAY_LEN (ubuf.ibuf) ;
1140 
1141 	while (len > 0)
1142 	{	if (len < bufferlen)
1143 			bufferlen = (int) len ;
1144 		readcount = psf_fread (ubuf.ibuf, sizeof (int), bufferlen, psf) ;
1145 		bei2f_array (ubuf.ibuf, readcount, ptr + total, normfact) ;
1146 		total += readcount ;
1147 		if (readcount < bufferlen)
1148 			break ;
1149 		len -= readcount ;
1150 		} ;
1151 
1152 	return total ;
1153 } /* pcm_read_bei2f */
1154 
1155 static sf_count_t
pcm_read_lei2f(SF_PRIVATE * psf,float * ptr,sf_count_t len)1156 pcm_read_lei2f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
1157 {	BUF_UNION	ubuf ;
1158 	int			bufferlen, readcount ;
1159 	sf_count_t	total = 0 ;
1160 	float	normfact ;
1161 
1162 	normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x80000000) : 1.0 ;
1163 
1164 	bufferlen = ARRAY_LEN (ubuf.ibuf) ;
1165 
1166 	while (len > 0)
1167 	{	if (len < bufferlen)
1168 			bufferlen = (int) len ;
1169 		readcount = psf_fread (ubuf.ibuf, sizeof (int), bufferlen, psf) ;
1170 		lei2f_array (ubuf.ibuf, readcount, ptr + total, normfact) ;
1171 		total += readcount ;
1172 		if (readcount < bufferlen)
1173 			break ;
1174 		len -= readcount ;
1175 		} ;
1176 
1177 	return total ;
1178 } /* pcm_read_lei2f */
1179 
1180 /*-----------------------------------------------------------------------------------------------
1181 */
1182 
1183 static sf_count_t
pcm_read_sc2d(SF_PRIVATE * psf,double * ptr,sf_count_t len)1184 pcm_read_sc2d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
1185 {	BUF_UNION	ubuf ;
1186 	int			bufferlen, readcount ;
1187 	sf_count_t	total = 0 ;
1188 	double		normfact ;
1189 
1190 	normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x80) : 1.0 ;
1191 
1192 	bufferlen = ARRAY_LEN (ubuf.scbuf) ;
1193 
1194 	while (len > 0)
1195 	{	if (len < bufferlen)
1196 			bufferlen = (int) len ;
1197 		readcount = psf_fread (ubuf.scbuf, sizeof (signed char), bufferlen, psf) ;
1198 		sc2d_array (ubuf.scbuf, readcount, ptr + total, normfact) ;
1199 		total += readcount ;
1200 		if (readcount < bufferlen)
1201 			break ;
1202 		len -= readcount ;
1203 		} ;
1204 
1205 	return total ;
1206 } /* pcm_read_sc2d */
1207 
1208 static sf_count_t
pcm_read_uc2d(SF_PRIVATE * psf,double * ptr,sf_count_t len)1209 pcm_read_uc2d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
1210 {	BUF_UNION	ubuf ;
1211 	int			bufferlen, readcount ;
1212 	sf_count_t	total = 0 ;
1213 	double		normfact ;
1214 
1215 	normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x80) : 1.0 ;
1216 
1217 	bufferlen = ARRAY_LEN (ubuf.ucbuf) ;
1218 
1219 	while (len > 0)
1220 	{	if (len < bufferlen)
1221 			bufferlen = (int) len ;
1222 		readcount = psf_fread (ubuf.ucbuf, sizeof (unsigned char), bufferlen, psf) ;
1223 		uc2d_array (ubuf.ucbuf, readcount, ptr + total, normfact) ;
1224 		total += readcount ;
1225 		if (readcount < bufferlen)
1226 			break ;
1227 		len -= readcount ;
1228 		} ;
1229 
1230 	return total ;
1231 } /* pcm_read_uc2d */
1232 
1233 static sf_count_t
pcm_read_bes2d(SF_PRIVATE * psf,double * ptr,sf_count_t len)1234 pcm_read_bes2d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
1235 {	BUF_UNION	ubuf ;
1236 	int			bufferlen, readcount ;
1237 	sf_count_t	total = 0 ;
1238 	double		normfact ;
1239 
1240 	normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x8000) : 1.0 ;
1241 
1242 	bufferlen = ARRAY_LEN (ubuf.sbuf) ;
1243 
1244 	while (len > 0)
1245 	{	if (len < bufferlen)
1246 			bufferlen = (int) len ;
1247 		readcount = psf_fread (ubuf.sbuf, sizeof (short), bufferlen, psf) ;
1248 		bes2d_array (ubuf.sbuf, readcount, ptr + total, normfact) ;
1249 		total += readcount ;
1250 		if (readcount < bufferlen)
1251 			break ;
1252 		len -= readcount ;
1253 		} ;
1254 
1255 	return total ;
1256 } /* pcm_read_bes2d */
1257 
1258 static sf_count_t
pcm_read_les2d(SF_PRIVATE * psf,double * ptr,sf_count_t len)1259 pcm_read_les2d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
1260 {	BUF_UNION	ubuf ;
1261 	int			bufferlen, readcount ;
1262 	sf_count_t	total = 0 ;
1263 	double		normfact ;
1264 
1265 	normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x8000) : 1.0 ;
1266 
1267 	bufferlen = ARRAY_LEN (ubuf.sbuf) ;
1268 
1269 	while (len > 0)
1270 	{	if (len < bufferlen)
1271 			bufferlen = (int) len ;
1272 		readcount = psf_fread (ubuf.sbuf, sizeof (short), bufferlen, psf) ;
1273 		les2d_array (ubuf.sbuf, readcount, ptr + total, normfact) ;
1274 		total += readcount ;
1275 		if (readcount < bufferlen)
1276 			break ;
1277 		len -= readcount ;
1278 		} ;
1279 
1280 	return total ;
1281 } /* pcm_read_les2d */
1282 
1283 static sf_count_t
pcm_read_bet2d(SF_PRIVATE * psf,double * ptr,sf_count_t len)1284 pcm_read_bet2d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
1285 {	BUF_UNION	ubuf ;
1286 	int			bufferlen, readcount ;
1287 	sf_count_t	total = 0 ;
1288 	double		normfact ;
1289 
1290 	normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x80000000) : 1.0 / 256.0 ;
1291 
1292 	bufferlen = sizeof (ubuf.ucbuf) / SIZEOF_TRIBYTE ;
1293 
1294 	while (len > 0)
1295 	{	if (len < bufferlen)
1296 			bufferlen = (int) len ;
1297 		readcount = psf_fread (ubuf.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ;
1298 		bet2d_array ((tribyte*) (ubuf.ucbuf), readcount, ptr + total, normfact) ;
1299 		total += readcount ;
1300 		if (readcount < bufferlen)
1301 			break ;
1302 		len -= readcount ;
1303 		} ;
1304 
1305 	return total ;
1306 } /* pcm_read_bet2d */
1307 
1308 static sf_count_t
pcm_read_let2d(SF_PRIVATE * psf,double * ptr,sf_count_t len)1309 pcm_read_let2d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
1310 {	BUF_UNION	ubuf ;
1311 	int			bufferlen, readcount ;
1312 	sf_count_t	total = 0 ;
1313 	double		normfact ;
1314 
1315 	/* Special normfactor because tribyte value is read into an int. */
1316 	normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x80000000) : 1.0 / 256.0 ;
1317 
1318 	bufferlen = sizeof (ubuf.ucbuf) / SIZEOF_TRIBYTE ;
1319 
1320 	while (len > 0)
1321 	{	if (len < bufferlen)
1322 			bufferlen = (int) len ;
1323 		readcount = psf_fread (ubuf.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ;
1324 		let2d_array ((tribyte*) (ubuf.ucbuf), readcount, ptr + total, normfact) ;
1325 		total += readcount ;
1326 		if (readcount < bufferlen)
1327 			break ;
1328 		len -= readcount ;
1329 		} ;
1330 
1331 	return total ;
1332 } /* pcm_read_let2d */
1333 
1334 static sf_count_t
pcm_read_bei2d(SF_PRIVATE * psf,double * ptr,sf_count_t len)1335 pcm_read_bei2d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
1336 {	BUF_UNION	ubuf ;
1337 	int			bufferlen, readcount ;
1338 	sf_count_t	total = 0 ;
1339 	double		normfact ;
1340 
1341 	normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x80000000) : 1.0 ;
1342 
1343 	bufferlen = ARRAY_LEN (ubuf.ibuf) ;
1344 
1345 	while (len > 0)
1346 	{	if (len < bufferlen)
1347 			bufferlen = (int) len ;
1348 		readcount = psf_fread (ubuf.ibuf, sizeof (int), bufferlen, psf) ;
1349 		bei2d_array (ubuf.ibuf, readcount, ptr + total, normfact) ;
1350 		total += readcount ;
1351 		if (readcount < bufferlen)
1352 			break ;
1353 		len -= readcount ;
1354 		} ;
1355 
1356 	return total ;
1357 } /* pcm_read_bei2d */
1358 
1359 static sf_count_t
pcm_read_lei2d(SF_PRIVATE * psf,double * ptr,sf_count_t len)1360 pcm_read_lei2d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
1361 {	BUF_UNION	ubuf ;
1362 	int			bufferlen, readcount ;
1363 	sf_count_t	total = 0 ;
1364 	double		normfact ;
1365 
1366 	normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x80000000) : 1.0 ;
1367 
1368 	bufferlen = ARRAY_LEN (ubuf.ibuf) ;
1369 
1370 	while (len > 0)
1371 	{	if (len < bufferlen)
1372 			bufferlen = (int) len ;
1373 		readcount = psf_fread (ubuf.ibuf, sizeof (int), bufferlen, psf) ;
1374 		lei2d_array (ubuf.ibuf, readcount, ptr + total, normfact) ;
1375 		total += readcount ;
1376 		if (readcount < bufferlen)
1377 			break ;
1378 		len -= readcount ;
1379 		} ;
1380 
1381 	return total ;
1382 } /* pcm_read_lei2d */
1383 
1384 /*===============================================================================================
1385 **-----------------------------------------------------------------------------------------------
1386 **===============================================================================================
1387 */
1388 
1389 static sf_count_t
pcm_write_s2sc(SF_PRIVATE * psf,const short * ptr,sf_count_t len)1390 pcm_write_s2sc	(SF_PRIVATE *psf, const short *ptr, sf_count_t len)
1391 {	BUF_UNION	ubuf ;
1392 	int			bufferlen, writecount ;
1393 	sf_count_t	total = 0 ;
1394 
1395 	bufferlen = ARRAY_LEN (ubuf.scbuf) ;
1396 
1397 	while (len > 0)
1398 	{	if (len < bufferlen)
1399 			bufferlen = (int) len ;
1400 		s2sc_array (ptr + total, ubuf.scbuf, bufferlen) ;
1401 		writecount = psf_fwrite (ubuf.scbuf, sizeof (signed char), bufferlen, psf) ;
1402 		total += writecount ;
1403 		if (writecount < bufferlen)
1404 			break ;
1405 		len -= writecount ;
1406 		} ;
1407 
1408 	return total ;
1409 } /* pcm_write_s2sc */
1410 
1411 static sf_count_t
pcm_write_s2uc(SF_PRIVATE * psf,const short * ptr,sf_count_t len)1412 pcm_write_s2uc	(SF_PRIVATE *psf, const short *ptr, sf_count_t len)
1413 {	BUF_UNION	ubuf ;
1414 	int			bufferlen, writecount ;
1415 	sf_count_t	total = 0 ;
1416 
1417 	bufferlen = ARRAY_LEN (ubuf.ucbuf) ;
1418 
1419 	while (len > 0)
1420 	{	if (len < bufferlen)
1421 			bufferlen = (int) len ;
1422 		s2uc_array (ptr + total, ubuf.ucbuf, bufferlen) ;
1423 		writecount = psf_fwrite (ubuf.ucbuf, sizeof (unsigned char), bufferlen, psf) ;
1424 		total += writecount ;
1425 		if (writecount < bufferlen)
1426 			break ;
1427 		len -= writecount ;
1428 		} ;
1429 
1430 	return total ;
1431 } /* pcm_write_s2uc */
1432 
1433 static sf_count_t
pcm_write_s2bes(SF_PRIVATE * psf,const short * ptr,sf_count_t len)1434 pcm_write_s2bes	(SF_PRIVATE *psf, const short *ptr, sf_count_t len)
1435 {	BUF_UNION	ubuf ;
1436 	int			bufferlen, writecount ;
1437 	sf_count_t	total = 0 ;
1438 
1439 	if (CPU_IS_BIG_ENDIAN)
1440 		return psf_fwrite (ptr, sizeof (short), len, psf) ;
1441 	else
1442 
1443 	bufferlen = ARRAY_LEN (ubuf.sbuf) ;
1444 
1445 	while (len > 0)
1446 	{	if (len < bufferlen)
1447 			bufferlen = (int) len ;
1448 		endswap_short_copy (ubuf.sbuf, ptr + total, bufferlen) ;
1449 		writecount = psf_fwrite (ubuf.sbuf, sizeof (short), bufferlen, psf) ;
1450 		total += writecount ;
1451 		if (writecount < bufferlen)
1452 			break ;
1453 		len -= writecount ;
1454 		} ;
1455 
1456 	return total ;
1457 } /* pcm_write_s2bes */
1458 
1459 static sf_count_t
pcm_write_s2les(SF_PRIVATE * psf,const short * ptr,sf_count_t len)1460 pcm_write_s2les	(SF_PRIVATE *psf, const short *ptr, sf_count_t len)
1461 {	BUF_UNION	ubuf ;
1462 	int			bufferlen, writecount ;
1463 	sf_count_t	total = 0 ;
1464 
1465 	if (CPU_IS_LITTLE_ENDIAN)
1466 		return psf_fwrite (ptr, sizeof (short), len, psf) ;
1467 
1468 	bufferlen = ARRAY_LEN (ubuf.sbuf) ;
1469 
1470 	while (len > 0)
1471 	{	if (len < bufferlen)
1472 			bufferlen = (int) len ;
1473 		endswap_short_copy (ubuf.sbuf, ptr + total, bufferlen) ;
1474 		writecount = psf_fwrite (ubuf.sbuf, sizeof (short), bufferlen, psf) ;
1475 		total += writecount ;
1476 		if (writecount < bufferlen)
1477 			break ;
1478 		len -= writecount ;
1479 		} ;
1480 
1481 	return total ;
1482 } /* pcm_write_s2les */
1483 
1484 static sf_count_t
pcm_write_s2bet(SF_PRIVATE * psf,const short * ptr,sf_count_t len)1485 pcm_write_s2bet	(SF_PRIVATE *psf, const short *ptr, sf_count_t len)
1486 {	BUF_UNION	ubuf ;
1487 	int			bufferlen, writecount ;
1488 	sf_count_t	total = 0 ;
1489 
1490 	bufferlen = sizeof (ubuf.ucbuf) / SIZEOF_TRIBYTE ;
1491 
1492 	while (len > 0)
1493 	{	if (len < bufferlen)
1494 			bufferlen = (int) len ;
1495 		s2bet_array (ptr + total, (tribyte*) (ubuf.ucbuf), bufferlen) ;
1496 		writecount = psf_fwrite (ubuf.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ;
1497 		total += writecount ;
1498 		if (writecount < bufferlen)
1499 			break ;
1500 		len -= writecount ;
1501 		} ;
1502 
1503 	return total ;
1504 } /* pcm_write_s2bet */
1505 
1506 static sf_count_t
pcm_write_s2let(SF_PRIVATE * psf,const short * ptr,sf_count_t len)1507 pcm_write_s2let	(SF_PRIVATE *psf, const short *ptr, sf_count_t len)
1508 {	BUF_UNION	ubuf ;
1509 	int			bufferlen, writecount ;
1510 	sf_count_t	total = 0 ;
1511 
1512 	bufferlen = sizeof (ubuf.ucbuf) / SIZEOF_TRIBYTE ;
1513 
1514 	while (len > 0)
1515 	{	if (len < bufferlen)
1516 			bufferlen = (int) len ;
1517 		s2let_array (ptr + total, (tribyte*) (ubuf.ucbuf), bufferlen) ;
1518 		writecount = psf_fwrite (ubuf.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ;
1519 		total += writecount ;
1520 		if (writecount < bufferlen)
1521 			break ;
1522 		len -= writecount ;
1523 		} ;
1524 
1525 	return total ;
1526 } /* pcm_write_s2let */
1527 
1528 static sf_count_t
pcm_write_s2bei(SF_PRIVATE * psf,const short * ptr,sf_count_t len)1529 pcm_write_s2bei	(SF_PRIVATE *psf, const short *ptr, sf_count_t len)
1530 {	BUF_UNION	ubuf ;
1531 	int			bufferlen, writecount ;
1532 	sf_count_t	total = 0 ;
1533 
1534 	bufferlen = ARRAY_LEN (ubuf.ibuf) ;
1535 
1536 	while (len > 0)
1537 	{	if (len < bufferlen)
1538 			bufferlen = (int) len ;
1539 		s2bei_array (ptr + total, ubuf.ibuf, bufferlen) ;
1540 		writecount = psf_fwrite (ubuf.ibuf, sizeof (int), bufferlen, psf) ;
1541 		total += writecount ;
1542 		if (writecount < bufferlen)
1543 			break ;
1544 		len -= writecount ;
1545 		} ;
1546 
1547 	return total ;
1548 } /* pcm_write_s2bei */
1549 
1550 static sf_count_t
pcm_write_s2lei(SF_PRIVATE * psf,const short * ptr,sf_count_t len)1551 pcm_write_s2lei	(SF_PRIVATE *psf, const short *ptr, sf_count_t len)
1552 {	BUF_UNION	ubuf ;
1553 	int			bufferlen, writecount ;
1554 	sf_count_t	total = 0 ;
1555 
1556 	bufferlen = ARRAY_LEN (ubuf.ibuf) ;
1557 
1558 	while (len > 0)
1559 	{	if (len < bufferlen)
1560 			bufferlen = (int) len ;
1561 		s2lei_array (ptr + total, ubuf.ibuf, bufferlen) ;
1562 		writecount = psf_fwrite (ubuf.ibuf, sizeof (int), bufferlen, psf) ;
1563 		total += writecount ;
1564 		if (writecount < bufferlen)
1565 			break ;
1566 		len -= writecount ;
1567 		} ;
1568 
1569 	return total ;
1570 } /* pcm_write_s2lei */
1571 
1572 /*-----------------------------------------------------------------------------------------------
1573 */
1574 
1575 static sf_count_t
pcm_write_i2sc(SF_PRIVATE * psf,const int * ptr,sf_count_t len)1576 pcm_write_i2sc	(SF_PRIVATE *psf, const int *ptr, sf_count_t len)
1577 {	BUF_UNION	ubuf ;
1578 	int			bufferlen, writecount ;
1579 	sf_count_t	total = 0 ;
1580 
1581 	bufferlen = ARRAY_LEN (ubuf.scbuf) ;
1582 
1583 	while (len > 0)
1584 	{	if (len < bufferlen)
1585 			bufferlen = (int) len ;
1586 		i2sc_array (ptr + total, ubuf.scbuf, bufferlen) ;
1587 		writecount = psf_fwrite (ubuf.scbuf, sizeof (signed char), bufferlen, psf) ;
1588 		total += writecount ;
1589 		if (writecount < bufferlen)
1590 			break ;
1591 		len -= writecount ;
1592 		} ;
1593 
1594 	return total ;
1595 } /* pcm_write_i2sc */
1596 
1597 static sf_count_t
pcm_write_i2uc(SF_PRIVATE * psf,const int * ptr,sf_count_t len)1598 pcm_write_i2uc	(SF_PRIVATE *psf, const int *ptr, sf_count_t len)
1599 {	BUF_UNION	ubuf ;
1600 	int			bufferlen, writecount ;
1601 	sf_count_t	total = 0 ;
1602 
1603 	bufferlen = ARRAY_LEN (ubuf.ucbuf) ;
1604 
1605 	while (len > 0)
1606 	{	if (len < bufferlen)
1607 			bufferlen = (int) len ;
1608 		i2uc_array (ptr + total, ubuf.ucbuf, bufferlen) ;
1609 		writecount = psf_fwrite (ubuf.ucbuf, sizeof (signed char), bufferlen, psf) ;
1610 		total += writecount ;
1611 		if (writecount < bufferlen)
1612 			break ;
1613 		len -= writecount ;
1614 		} ;
1615 
1616 	return total ;
1617 } /* pcm_write_i2uc */
1618 
1619 static sf_count_t
pcm_write_i2bes(SF_PRIVATE * psf,const int * ptr,sf_count_t len)1620 pcm_write_i2bes	(SF_PRIVATE *psf, const int *ptr, sf_count_t len)
1621 {	BUF_UNION	ubuf ;
1622 	int			bufferlen, writecount ;
1623 	sf_count_t	total = 0 ;
1624 
1625 	bufferlen = ARRAY_LEN (ubuf.sbuf) ;
1626 
1627 	while (len > 0)
1628 	{	if (len < bufferlen)
1629 			bufferlen = (int) len ;
1630 		i2bes_array (ptr + total, ubuf.sbuf, bufferlen) ;
1631 		writecount = psf_fwrite (ubuf.sbuf, sizeof (short), bufferlen, psf) ;
1632 		total += writecount ;
1633 		if (writecount < bufferlen)
1634 			break ;
1635 		len -= writecount ;
1636 		} ;
1637 
1638 	return total ;
1639 } /* pcm_write_i2bes */
1640 
1641 static sf_count_t
pcm_write_i2les(SF_PRIVATE * psf,const int * ptr,sf_count_t len)1642 pcm_write_i2les	(SF_PRIVATE *psf, const int *ptr, sf_count_t len)
1643 {	BUF_UNION	ubuf ;
1644 	int			bufferlen, writecount ;
1645 	sf_count_t	total = 0 ;
1646 
1647 	bufferlen = ARRAY_LEN (ubuf.sbuf) ;
1648 
1649 	while (len > 0)
1650 	{	if (len < bufferlen)
1651 			bufferlen = (int) len ;
1652 		i2les_array (ptr + total, ubuf.sbuf, bufferlen) ;
1653 		writecount = psf_fwrite (ubuf.sbuf, sizeof (short), bufferlen, psf) ;
1654 		total += writecount ;
1655 		if (writecount < bufferlen)
1656 			break ;
1657 		len -= writecount ;
1658 		} ;
1659 
1660 	return total ;
1661 } /* pcm_write_i2les */
1662 
1663 static sf_count_t
pcm_write_i2bet(SF_PRIVATE * psf,const int * ptr,sf_count_t len)1664 pcm_write_i2bet	(SF_PRIVATE *psf, const int *ptr, sf_count_t len)
1665 {	BUF_UNION	ubuf ;
1666 	int			bufferlen, writecount ;
1667 	sf_count_t	total = 0 ;
1668 
1669 	bufferlen = sizeof (ubuf.ucbuf) / SIZEOF_TRIBYTE ;
1670 
1671 	while (len > 0)
1672 	{	if (len < bufferlen)
1673 			bufferlen = (int) len ;
1674 		i2bet_array (ptr + total, (tribyte*) (ubuf.ucbuf), bufferlen) ;
1675 		writecount = psf_fwrite (ubuf.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ;
1676 		total += writecount ;
1677 		if (writecount < bufferlen)
1678 			break ;
1679 		len -= writecount ;
1680 		} ;
1681 
1682 	return total ;
1683 } /* pcm_write_i2bet */
1684 
1685 static sf_count_t
pcm_write_i2let(SF_PRIVATE * psf,const int * ptr,sf_count_t len)1686 pcm_write_i2let	(SF_PRIVATE *psf, const int *ptr, sf_count_t len)
1687 {	BUF_UNION	ubuf ;
1688 	int			bufferlen, writecount ;
1689 	sf_count_t	total = 0 ;
1690 
1691 	bufferlen = sizeof (ubuf.ucbuf) / SIZEOF_TRIBYTE ;
1692 
1693 	while (len > 0)
1694 	{	if (len < bufferlen)
1695 			bufferlen = (int) len ;
1696 		i2let_array (ptr + total, (tribyte*) (ubuf.ucbuf), bufferlen) ;
1697 		writecount = psf_fwrite (ubuf.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ;
1698 		total += writecount ;
1699 		if (writecount < bufferlen)
1700 			break ;
1701 		len -= writecount ;
1702 		} ;
1703 
1704 	return total ;
1705 } /* pcm_write_i2les */
1706 
1707 static sf_count_t
pcm_write_i2bei(SF_PRIVATE * psf,const int * ptr,sf_count_t len)1708 pcm_write_i2bei	(SF_PRIVATE *psf, const int *ptr, sf_count_t len)
1709 {	BUF_UNION	ubuf ;
1710 	int			bufferlen, writecount ;
1711 	sf_count_t	total = 0 ;
1712 
1713 	if (CPU_IS_BIG_ENDIAN)
1714 		return psf_fwrite (ptr, sizeof (int), len, psf) ;
1715 
1716 	bufferlen = ARRAY_LEN (ubuf.ibuf) ;
1717 
1718 	while (len > 0)
1719 	{	if (len < bufferlen)
1720 			bufferlen = (int) len ;
1721 		endswap_int_copy (ubuf.ibuf, ptr + total, bufferlen) ;
1722 		writecount = psf_fwrite (ubuf.ibuf, sizeof (int), bufferlen, psf) ;
1723 		total += writecount ;
1724 		if (writecount < bufferlen)
1725 			break ;
1726 		len -= writecount ;
1727 		} ;
1728 
1729 	return total ;
1730 } /* pcm_write_i2bei */
1731 
1732 static sf_count_t
pcm_write_i2lei(SF_PRIVATE * psf,const int * ptr,sf_count_t len)1733 pcm_write_i2lei	(SF_PRIVATE *psf, const int *ptr, sf_count_t len)
1734 {	BUF_UNION	ubuf ;
1735 	int			bufferlen, writecount ;
1736 	sf_count_t	total = 0 ;
1737 
1738 	if (CPU_IS_LITTLE_ENDIAN)
1739 		return psf_fwrite (ptr, sizeof (int), len, psf) ;
1740 
1741 	bufferlen = ARRAY_LEN (ubuf.ibuf) ;
1742 
1743 	while (len > 0)
1744 	{	if (len < bufferlen)
1745 			bufferlen = (int) len ;
1746 		endswap_int_copy (ubuf.ibuf, ptr + total, bufferlen) ;
1747 		writecount = psf_fwrite (ubuf.ibuf, sizeof (int), bufferlen, psf) ;
1748 		total += writecount ;
1749 		if (writecount < bufferlen)
1750 			break ;
1751 		len -= writecount ;
1752 		} ;
1753 
1754 	return total ;
1755 } /* pcm_write_i2lei */
1756 
1757 /*------------------------------------------------------------------------------
1758 **==============================================================================
1759 **------------------------------------------------------------------------------
1760 */
1761 
1762 static void
f2sc_array(const float * src,signed char * dest,int count,int normalize)1763 f2sc_array (const float *src, signed char *dest, int count, int normalize)
1764 {	float normfact ;
1765 
1766 	normfact = normalize ? (1.0 * 0x7F) : 1.0 ;
1767 
1768 	while (--count >= 0)
1769 	{	dest [count] = psf_lrintf (src [count] * normfact) ;
1770 		} ;
1771 } /* f2sc_array */
1772 
1773 static void
f2sc_clip_array(const float * src,signed char * dest,int count,int normalize)1774 f2sc_clip_array (const float *src, signed char *dest, int count, int normalize)
1775 {	float	normfact, scaled_value ;
1776 
1777 	normfact = normalize ? (8.0 * 0x10000000) : (1.0 * 0x1000000) ;
1778 
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 ;
1783 			continue ;
1784 			} ;
1785 		if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
1786 		{	dest [count] = -128 ;
1787 			continue ;
1788 			} ;
1789 
1790 		dest [count] = psf_lrintf (scaled_value) >> 24 ;
1791 		} ;
1792 } /* f2sc_clip_array */
1793 
1794 static sf_count_t
pcm_write_f2sc(SF_PRIVATE * psf,const float * ptr,sf_count_t len)1795 pcm_write_f2sc	(SF_PRIVATE *psf, const float *ptr, sf_count_t len)
1796 {	BUF_UNION	ubuf ;
1797 	void		(*convert) (const float *, signed char *, int, int) ;
1798 	int			bufferlen, writecount ;
1799 	sf_count_t	total = 0 ;
1800 
1801 	convert = (psf->add_clipping) ? f2sc_clip_array : f2sc_array ;
1802 	bufferlen = ARRAY_LEN (ubuf.scbuf) ;
1803 
1804 	while (len > 0)
1805 	{	if (len < bufferlen)
1806 			bufferlen = (int) len ;
1807 		convert (ptr + total, ubuf.scbuf, bufferlen, psf->norm_float) ;
1808 		writecount = psf_fwrite (ubuf.scbuf, sizeof (signed char), bufferlen, psf) ;
1809 		total += writecount ;
1810 		if (writecount < bufferlen)
1811 			break ;
1812 		len -= writecount ;
1813 		} ;
1814 
1815 	return total ;
1816 } /* pcm_write_f2sc */
1817 
1818 /*==============================================================================
1819 */
1820 
1821 static	void
f2uc_array(const float * src,unsigned char * dest,int count,int normalize)1822 f2uc_array	(const float *src, unsigned char *dest, int count, int normalize)
1823 {	float normfact ;
1824 
1825 	normfact = normalize ? (1.0 * 0x7F) : 1.0 ;
1826 
1827 	while (--count >= 0)
1828 	{	dest [count] = psf_lrintf (src [count] * normfact) + 128 ;
1829 		} ;
1830 } /* f2uc_array */
1831 
1832 static	void
f2uc_clip_array(const float * src,unsigned char * dest,int count,int normalize)1833 f2uc_clip_array	(const float *src, unsigned char *dest, int count, int normalize)
1834 {	float	normfact, scaled_value ;
1835 
1836 	normfact = normalize ? (8.0 * 0x10000000) : (1.0 * 0x1000000) ;
1837 
1838 	while (--count >= 0)
1839 	{	scaled_value = src [count] * normfact ;
1840 		if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF))
1841 		{	dest [count] = 0xFF ;
1842 			continue ;
1843 			} ;
1844 		if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
1845 		{	dest [count] = 0 ;
1846 			continue ;
1847 			} ;
1848 
1849 		dest [count] = (psf_lrintf (scaled_value) >> 24) + 128 ;
1850 		} ;
1851 } /* f2uc_clip_array */
1852 
1853 static sf_count_t
pcm_write_f2uc(SF_PRIVATE * psf,const float * ptr,sf_count_t len)1854 pcm_write_f2uc	(SF_PRIVATE *psf, const float *ptr, sf_count_t len)
1855 {	BUF_UNION	ubuf ;
1856 	void		(*convert) (const float *, unsigned char *, int, int) ;
1857 	int			bufferlen, writecount ;
1858 	sf_count_t	total = 0 ;
1859 
1860 	convert = (psf->add_clipping) ? f2uc_clip_array : f2uc_array ;
1861 	bufferlen = ARRAY_LEN (ubuf.ucbuf) ;
1862 
1863 	while (len > 0)
1864 	{	if (len < bufferlen)
1865 			bufferlen = (int) len ;
1866 		convert (ptr + total, ubuf.ucbuf, bufferlen, psf->norm_float) ;
1867 		writecount = psf_fwrite (ubuf.ucbuf, sizeof (unsigned char), bufferlen, psf) ;
1868 		total += writecount ;
1869 		if (writecount < bufferlen)
1870 			break ;
1871 		len -= writecount ;
1872 		} ;
1873 
1874 	return total ;
1875 } /* pcm_write_f2uc */
1876 
1877 /*==============================================================================
1878 */
1879 
1880 static void
f2bes_array(const float * src,short * dest,int count,int normalize)1881 f2bes_array (const float *src, short *dest, int count, int normalize)
1882 {	unsigned char	*ucptr ;
1883 	float 			normfact ;
1884 	short			value ;
1885 
1886 	normfact = normalize ? (1.0 * 0x7FFF) : 1.0 ;
1887 	ucptr = ((unsigned char*) dest) + 2 * count ;
1888 
1889 	while (--count >= 0)
1890 	{	ucptr -= 2 ;
1891 		value = psf_lrintf (src [count] * normfact) ;
1892 		ucptr [1] = value ;
1893 		ucptr [0] = value >> 8 ;
1894 			} ;
1895 } /* f2bes_array */
1896 
1897 static void
f2bes_clip_array(const float * src,short * dest,int count,int normalize)1898 f2bes_clip_array (const float *src, short *dest, int count, int normalize)
1899 {	unsigned char	*ucptr ;
1900 	float			normfact, scaled_value ;
1901 	int				value ;
1902 
1903 	normfact = normalize ? (8.0 * 0x10000000) : (1.0 * 0x10000) ;
1904 	ucptr = ((unsigned char*) dest) + 2 * count ;
1905 
1906 	while (--count >= 0)
1907 	{	ucptr -= 2 ;
1908 		scaled_value = src [count] * normfact ;
1909 		if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF))
1910 		{	ucptr [1] = 0xFF ;
1911 			ucptr [0] = 0x7F ;
1912 			continue ;
1913 		} ;
1914 		if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
1915 		{	ucptr [1] = 0x00 ;
1916 			ucptr [0] = 0x80 ;
1917 			continue ;
1918 			} ;
1919 
1920 		value = psf_lrintf (scaled_value) ;
1921 		ucptr [1] = value >> 16 ;
1922 		ucptr [0] = value >> 24 ;
1923 		} ;
1924 } /* f2bes_clip_array */
1925 
1926 static sf_count_t
pcm_write_f2bes(SF_PRIVATE * psf,const float * ptr,sf_count_t len)1927 pcm_write_f2bes	(SF_PRIVATE *psf, const float *ptr, sf_count_t len)
1928 {	BUF_UNION	ubuf ;
1929 	void		(*convert) (const float *, short *t, int, int) ;
1930 	int			bufferlen, writecount ;
1931 	sf_count_t	total = 0 ;
1932 
1933 	convert = (psf->add_clipping) ? f2bes_clip_array : f2bes_array ;
1934 	bufferlen = ARRAY_LEN (ubuf.sbuf) ;
1935 
1936 	while (len > 0)
1937 	{	if (len < bufferlen)
1938 			bufferlen = (int) len ;
1939 		convert (ptr + total, ubuf.sbuf, bufferlen, psf->norm_float) ;
1940 		writecount = psf_fwrite (ubuf.sbuf, sizeof (short), bufferlen, psf) ;
1941 		total += writecount ;
1942 		if (writecount < bufferlen)
1943 				break ;
1944 		len -= writecount ;
1945 		} ;
1946 
1947 	return total ;
1948 } /* pcm_write_f2bes */
1949 
1950 /*==============================================================================
1951 */
1952 
1953 static void
f2les_array(const float * src,short * dest,int count,int normalize)1954 f2les_array (const float *src, short *dest, int count, int normalize)
1955 {	unsigned char	*ucptr ;
1956 	float			normfact ;
1957 	int				value ;
1958 
1959 	normfact = normalize ? (1.0 * 0x7FFF) : 1.0 ;
1960 	ucptr = ((unsigned char*) dest) + 2 * count ;
1961 
1962 	while (--count >= 0)
1963 	{	ucptr -= 2 ;
1964 		value = psf_lrintf (src [count] * normfact) ;
1965 		ucptr [0] = value ;
1966 		ucptr [1] = value >> 8 ;
1967 		} ;
1968 } /* f2les_array */
1969 
1970 static void
f2les_clip_array(const float * src,short * dest,int count,int normalize)1971 f2les_clip_array (const float *src, short *dest, int count, int normalize)
1972 {	unsigned char	*ucptr ;
1973 	float			normfact, scaled_value ;
1974 	int				value ;
1975 
1976 	normfact = normalize ? (8.0 * 0x10000000) : (1.0 * 0x10000) ;
1977 	ucptr = ((unsigned char*) dest) + 2 * count ;
1978 
1979 	while (--count >= 0)
1980 	{	ucptr -= 2 ;
1981 		scaled_value = src [count] * normfact ;
1982 		if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF))
1983 		{	ucptr [0] = 0xFF ;
1984 			ucptr [1] = 0x7F ;
1985 			continue ;
1986 			} ;
1987 		if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
1988 		{	ucptr [0] = 0x00 ;
1989 			ucptr [1] = 0x80 ;
1990 			continue ;
1991 			} ;
1992 
1993 		value = psf_lrintf (scaled_value) ;
1994 		ucptr [0] = value >> 16 ;
1995 		ucptr [1] = value >> 24 ;
1996 		} ;
1997 } /* f2les_clip_array */
1998 
1999 static sf_count_t
pcm_write_f2les(SF_PRIVATE * psf,const float * ptr,sf_count_t len)2000 pcm_write_f2les	(SF_PRIVATE *psf, const float *ptr, sf_count_t len)
2001 {	BUF_UNION	ubuf ;
2002 	void		(*convert) (const float *, short *t, int, int) ;
2003 	int			bufferlen, writecount ;
2004 	sf_count_t	total = 0 ;
2005 
2006 	convert = (psf->add_clipping) ? f2les_clip_array : f2les_array ;
2007 	bufferlen = ARRAY_LEN (ubuf.sbuf) ;
2008 
2009 	while (len > 0)
2010 	{	if (len < bufferlen)
2011 			bufferlen = (int) len ;
2012 		convert (ptr + total, ubuf.sbuf, bufferlen, psf->norm_float) ;
2013 		writecount = psf_fwrite (ubuf.sbuf, sizeof (short), bufferlen, psf) ;
2014 		total += writecount ;
2015 		if (writecount < bufferlen)
2016 			break ;
2017 		len -= writecount ;
2018 		} ;
2019 
2020 	return total ;
2021 } /* pcm_write_f2les */
2022 
2023 /*==============================================================================
2024 */
2025 
2026 static void
f2let_array(const float * src,tribyte * dest,int count,int normalize)2027 f2let_array (const float *src, tribyte *dest, int count, int normalize)
2028 {	float	normfact ;
2029 	int		value ;
2030 
2031 	normfact = normalize ? (1.0 * 0x7FFFFF) : 1.0 ;
2032 
2033 	while (--count >= 0)
2034 	{	value = psf_lrintf (src [count] * normfact) ;
2035 		dest [count].bytes [0] = value ;
2036 		dest [count].bytes [1] = value >> 8 ;
2037 		dest [count].bytes [2] = value >> 16 ;
2038 		} ;
2039 } /* f2let_array */
2040 
2041 static void
f2let_clip_array(const float * src,tribyte * dest,int count,int normalize)2042 f2let_clip_array (const float *src, tribyte *dest, int count, int normalize)
2043 {	float	normfact, scaled_value ;
2044 	int		value ;
2045 
2046 	normfact = normalize ? (8.0 * 0x10000000) : (1.0 * 0x100) ;
2047 
2048 	while (--count >= 0)
2049 	{	scaled_value = src [count] * normfact ;
2050 		if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF))
2051 		{	dest [count].bytes [0] = 0xFF ;
2052 			dest [count].bytes [1] = 0xFF ;
2053 			dest [count].bytes [2] = 0x7F ;
2054 			continue ;
2055 			} ;
2056 		if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
2057 		{	dest [count].bytes [0] = 0x00 ;
2058 			dest [count].bytes [1] = 0x00 ;
2059 			dest [count].bytes [2] = 0x80 ;
2060 			continue ;
2061 		} ;
2062 
2063 		value = psf_lrintf (scaled_value) ;
2064 		dest [count].bytes [0] = value >> 8 ;
2065 		dest [count].bytes [1] = value >> 16 ;
2066 		dest [count].bytes [2] = value >> 24 ;
2067 		} ;
2068 } /* f2let_clip_array */
2069 
2070 static sf_count_t
pcm_write_f2let(SF_PRIVATE * psf,const float * ptr,sf_count_t len)2071 pcm_write_f2let	(SF_PRIVATE *psf, const float *ptr, sf_count_t len)
2072 {	BUF_UNION	ubuf ;
2073 	void		(*convert) (const float *, tribyte *, int, int) ;
2074 	int			bufferlen, writecount ;
2075 	sf_count_t	total = 0 ;
2076 
2077 	convert = (psf->add_clipping) ? f2let_clip_array : f2let_array ;
2078 	bufferlen = sizeof (ubuf.ucbuf) / SIZEOF_TRIBYTE ;
2079 
2080 	while (len > 0)
2081 	{	if (len < bufferlen)
2082 			bufferlen = (int) len ;
2083 		convert (ptr + total, (tribyte*) (ubuf.ucbuf), bufferlen, psf->norm_float) ;
2084 		writecount = psf_fwrite (ubuf.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ;
2085 		total += writecount ;
2086 		if (writecount < bufferlen)
2087 			break ;
2088 		len -= writecount ;
2089 		} ;
2090 
2091 	return total ;
2092 } /* pcm_write_f2let */
2093 
2094 /*==============================================================================
2095 */
2096 
2097 static void
f2bet_array(const float * src,tribyte * dest,int count,int normalize)2098 f2bet_array (const float *src, tribyte *dest, int count, int normalize)
2099 {	float	normfact ;
2100 	int		value ;
2101 
2102 	normfact = normalize ? (1.0 * 0x7FFFFF) : 1.0 ;
2103 
2104 	while (--count >= 0)
2105 	{	value = psf_lrintf (src [count] * normfact) ;
2106 		dest [count].bytes [0] = value >> 16 ;
2107 		dest [count].bytes [1] = value >> 8 ;
2108 		dest [count].bytes [2] = value ;
2109 		} ;
2110 } /* f2bet_array */
2111 
2112 static void
f2bet_clip_array(const float * src,tribyte * dest,int count,int normalize)2113 f2bet_clip_array (const float *src, tribyte *dest, int count, int normalize)
2114 {	float	normfact, scaled_value ;
2115 	int		value ;
2116 
2117 	normfact = normalize ? (8.0 * 0x10000000) : (1.0 * 0x100) ;
2118 
2119 	while (--count >= 0)
2120 	{	scaled_value = src [count] * normfact ;
2121 		if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF))
2122 		{	dest [count].bytes [0] = 0x7F ;
2123 			dest [count].bytes [1] = 0xFF ;
2124 			dest [count].bytes [2] = 0xFF ;
2125 			continue ;
2126 			} ;
2127 		if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
2128 		{	dest [count].bytes [0] = 0x80 ;
2129 			dest [count].bytes [1] = 0x00 ;
2130 			dest [count].bytes [2] = 0x00 ;
2131 			continue ;
2132 		} ;
2133 
2134 		value = psf_lrint (scaled_value) ;
2135 		dest [count].bytes [0] = value >> 24 ;
2136 		dest [count].bytes [1] = value >> 16 ;
2137 		dest [count].bytes [2] = value >> 8 ;
2138 		} ;
2139 } /* f2bet_clip_array */
2140 
2141 static sf_count_t
pcm_write_f2bet(SF_PRIVATE * psf,const float * ptr,sf_count_t len)2142 pcm_write_f2bet	(SF_PRIVATE *psf, const float *ptr, sf_count_t len)
2143 {	BUF_UNION	ubuf ;
2144 	void		(*convert) (const float *, tribyte *, int, int) ;
2145 	int			bufferlen, writecount ;
2146 	sf_count_t	total = 0 ;
2147 
2148 	convert = (psf->add_clipping) ? f2bet_clip_array : f2bet_array ;
2149 	bufferlen = sizeof (ubuf.ucbuf) / SIZEOF_TRIBYTE ;
2150 
2151 	while (len > 0)
2152 	{	if (len < bufferlen)
2153 			bufferlen = (int) len ;
2154 		convert (ptr + total, (tribyte*) (ubuf.ucbuf), bufferlen, psf->norm_float) ;
2155 		writecount = psf_fwrite (ubuf.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ;
2156 		total += writecount ;
2157 		if (writecount < bufferlen)
2158 			break ;
2159 		len -= writecount ;
2160 		} ;
2161 
2162 	return total ;
2163 } /* pcm_write_f2bet */
2164 
2165 /*==============================================================================
2166 */
2167 
2168 static void
f2bei_array(const float * src,int * dest,int count,int normalize)2169 f2bei_array (const float *src, int *dest, int count, int normalize)
2170 {	unsigned char	*ucptr ;
2171 	float			normfact ;
2172 	int				value ;
2173 
2174 	normfact = normalize ? (1.0 * 0x7FFFFFFF) : 1.0 ;
2175 	ucptr = ((unsigned char*) dest) + 4 * count ;
2176 	while (--count >= 0)
2177 	{	ucptr -= 4 ;
2178 		value = psf_lrintf (src [count] * normfact) ;
2179 		ucptr [0] = value >> 24 ;
2180 		ucptr [1] = value >> 16 ;
2181 		ucptr [2] = value >> 8 ;
2182 		ucptr [3] = value ;
2183 		} ;
2184 } /* f2bei_array */
2185 
2186 static void
f2bei_clip_array(const float * src,int * dest,int count,int normalize)2187 f2bei_clip_array (const float *src, int *dest, int count, int normalize)
2188 {	unsigned char	*ucptr ;
2189 	float			normfact, scaled_value ;
2190 	int				value ;
2191 
2192 	normfact = normalize ? (8.0 * 0x10000000) : 1.0 ;
2193 	ucptr = ((unsigned char*) dest) + 4 * count ;
2194 
2195 	while (--count >= 0)
2196 	{	ucptr -= 4 ;
2197 		scaled_value = src [count] * normfact ;
2198 		if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= 1.0 * 0x7FFFFFFF)
2199 		{	ucptr [0] = 0x7F ;
2200 			ucptr [1] = 0xFF ;
2201 			ucptr [2] = 0xFF ;
2202 			ucptr [3] = 0xFF ;
2203 			continue ;
2204 			} ;
2205 		if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
2206 		{	ucptr [0] = 0x80 ;
2207 			ucptr [1] = 0x00 ;
2208 			ucptr [2] = 0x00 ;
2209 			ucptr [3] = 0x00 ;
2210 			continue ;
2211 		} ;
2212 
2213 		value = psf_lrintf (scaled_value) ;
2214 		ucptr [0] = value >> 24 ;
2215 		ucptr [1] = value >> 16 ;
2216 		ucptr [2] = value >> 8 ;
2217 		ucptr [3] = value ;
2218 		} ;
2219 } /* f2bei_clip_array */
2220 
2221 static sf_count_t
pcm_write_f2bei(SF_PRIVATE * psf,const float * ptr,sf_count_t len)2222 pcm_write_f2bei	(SF_PRIVATE *psf, const float *ptr, sf_count_t len)
2223 {	BUF_UNION	ubuf ;
2224 	void		(*convert) (const float *, int *, int, int) ;
2225 	int			bufferlen, writecount ;
2226 	sf_count_t	total = 0 ;
2227 
2228 	convert = (psf->add_clipping) ? f2bei_clip_array : f2bei_array ;
2229 	bufferlen = ARRAY_LEN (ubuf.ibuf) ;
2230 
2231 	while (len > 0)
2232 	{	if (len < bufferlen)
2233 			bufferlen = (int) len ;
2234 		convert (ptr + total, ubuf.ibuf, bufferlen, psf->norm_float) ;
2235 		writecount = psf_fwrite (ubuf.ibuf, sizeof (int), bufferlen, psf) ;
2236 		total += writecount ;
2237 		if (writecount < bufferlen)
2238 			break ;
2239 		len -= writecount ;
2240 		} ;
2241 
2242 	return total ;
2243 } /* pcm_write_f2bei */
2244 
2245 /*==============================================================================
2246 */
2247 
2248 static void
f2lei_array(const float * src,int * dest,int count,int normalize)2249 f2lei_array (const float *src, int *dest, int count, int normalize)
2250 {	unsigned char	*ucptr ;
2251 	float			normfact ;
2252 	int				value ;
2253 
2254 	normfact = normalize ? (1.0 * 0x7FFFFFFF) : 1.0 ;
2255 	ucptr = ((unsigned char*) dest) + 4 * count ;
2256 
2257 	while (--count >= 0)
2258 	{	ucptr -= 4 ;
2259 		value = psf_lrintf (src [count] * normfact) ;
2260 		ucptr [0] = value ;
2261 		ucptr [1] = value >> 8 ;
2262 		ucptr [2] = value >> 16 ;
2263 		ucptr [3] = value >> 24 ;
2264 		} ;
2265 } /* f2lei_array */
2266 
2267 static void
f2lei_clip_array(const float * src,int * dest,int count,int normalize)2268 f2lei_clip_array (const float *src, int *dest, int count, int normalize)
2269 {	unsigned char	*ucptr ;
2270 	float			normfact, scaled_value ;
2271 	int				value ;
2272 
2273 	normfact = normalize ? (8.0 * 0x10000000) : 1.0 ;
2274 	ucptr = ((unsigned char*) dest) + 4 * count ;
2275 
2276 	while (--count >= 0)
2277 	{	ucptr -= 4 ;
2278 		scaled_value = src [count] * normfact ;
2279 		if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF))
2280 		{	ucptr [0] = 0xFF ;
2281 			ucptr [1] = 0xFF ;
2282 			ucptr [2] = 0xFF ;
2283 			ucptr [3] = 0x7F ;
2284 			continue ;
2285 			} ;
2286 		if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
2287 		{	ucptr [0] = 0x00 ;
2288 			ucptr [1] = 0x00 ;
2289 			ucptr [2] = 0x00 ;
2290 			ucptr [3] = 0x80 ;
2291 			continue ;
2292 			} ;
2293 
2294 		value = psf_lrintf (scaled_value) ;
2295 		ucptr [0] = value ;
2296 		ucptr [1] = value >> 8 ;
2297 		ucptr [2] = value >> 16 ;
2298 		ucptr [3] = value >> 24 ;
2299 		} ;
2300 } /* f2lei_clip_array */
2301 
2302 static sf_count_t
pcm_write_f2lei(SF_PRIVATE * psf,const float * ptr,sf_count_t len)2303 pcm_write_f2lei	(SF_PRIVATE *psf, const float *ptr, sf_count_t len)
2304 {	BUF_UNION	ubuf ;
2305 	void		(*convert) (const float *, int *, int, int) ;
2306 	int			bufferlen, writecount ;
2307 	sf_count_t	total = 0 ;
2308 
2309 	convert = (psf->add_clipping) ? f2lei_clip_array : f2lei_array ;
2310 	bufferlen = ARRAY_LEN (ubuf.ibuf) ;
2311 
2312 	while (len > 0)
2313 	{	if (len < bufferlen)
2314 			bufferlen = (int) len ;
2315 		convert (ptr + total, ubuf.ibuf, bufferlen, psf->norm_float) ;
2316 		writecount = psf_fwrite (ubuf.ibuf, sizeof (int), bufferlen, psf) ;
2317 		total += writecount ;
2318 		if (writecount < bufferlen)
2319 			break ;
2320 		len -= writecount ;
2321 		} ;
2322 
2323 	return total ;
2324 } /* pcm_write_f2lei */
2325 
2326 /*==============================================================================
2327 */
2328 
2329 static void
d2sc_array(const double * src,signed char * dest,int count,int normalize)2330 d2sc_array	(const double *src, signed char *dest, int count, int normalize)
2331 {	double	normfact ;
2332 
2333 	normfact = normalize ? (1.0 * 0x7F) : 1.0 ;
2334 
2335 	while (--count >= 0)
2336 	{	dest [count] = psf_lrint (src [count] * normfact) ;
2337 		} ;
2338 } /* d2sc_array */
2339 
2340 static void
d2sc_clip_array(const double * src,signed char * dest,int count,int normalize)2341 d2sc_clip_array	(const double *src, signed char *dest, int count, int normalize)
2342 {	double	normfact, scaled_value ;
2343 
2344 	normfact = normalize ? (8.0 * 0x10000000) : (1.0 * 0x1000000) ;
2345 
2346 	while (--count >= 0)
2347 	{	scaled_value = src [count] * normfact ;
2348 		if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF))
2349 		{	dest [count] = 127 ;
2350 			continue ;
2351 			} ;
2352 		if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
2353 		{	dest [count] = -128 ;
2354 			continue ;
2355 			} ;
2356 
2357 		dest [count] = psf_lrintf (scaled_value) >> 24 ;
2358 		} ;
2359 } /* d2sc_clip_array */
2360 
2361 static sf_count_t
pcm_write_d2sc(SF_PRIVATE * psf,const double * ptr,sf_count_t len)2362 pcm_write_d2sc	(SF_PRIVATE *psf, const double *ptr, sf_count_t len)
2363 {	BUF_UNION	ubuf ;
2364 	void		(*convert) (const double *, signed char *, int, int) ;
2365 	int			bufferlen, writecount ;
2366 	sf_count_t	total = 0 ;
2367 
2368 	convert = (psf->add_clipping) ? d2sc_clip_array : d2sc_array ;
2369 	bufferlen = ARRAY_LEN (ubuf.scbuf) ;
2370 
2371 	while (len > 0)
2372 	{	if (len < bufferlen)
2373 			bufferlen = (int) len ;
2374 		convert (ptr + total, ubuf.scbuf, bufferlen, psf->norm_double) ;
2375 		writecount = psf_fwrite (ubuf.scbuf, sizeof (signed char), bufferlen, psf) ;
2376 		total += writecount ;
2377 		if (writecount < bufferlen)
2378 			break ;
2379 		len -= writecount ;
2380 		} ;
2381 
2382 	return total ;
2383 } /* pcm_write_d2sc */
2384 
2385 /*==============================================================================
2386 */
2387 
2388 static	void
d2uc_array(const double * src,unsigned char * dest,int count,int normalize)2389 d2uc_array	(const double *src, unsigned char *dest, int count, int normalize)
2390 {	double normfact ;
2391 
2392 	normfact = normalize ? (1.0 * 0x7F) : 1.0 ;
2393 
2394 	while (--count >= 0)
2395 	{	dest [count] = psf_lrint (src [count] * normfact) + 128 ;
2396 		} ;
2397 } /* d2uc_array */
2398 
2399 static	void
d2uc_clip_array(const double * src,unsigned char * dest,int count,int normalize)2400 d2uc_clip_array	(const double *src, unsigned char *dest, int count, int normalize)
2401 {	double	normfact, scaled_value ;
2402 
2403 	normfact = normalize ? (8.0 * 0x10000000) : (1.0 * 0x1000000) ;
2404 
2405 	while (--count >= 0)
2406 	{	scaled_value = src [count] * normfact ;
2407 		if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF))
2408 		{	dest [count] = 255 ;
2409 			continue ;
2410 			} ;
2411 		if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
2412 		{	dest [count] = 0 ;
2413 			continue ;
2414 			} ;
2415 
2416 		dest [count] = (psf_lrint (src [count] * normfact) >> 24) + 128 ;
2417 		} ;
2418 } /* d2uc_clip_array */
2419 
2420 static sf_count_t
pcm_write_d2uc(SF_PRIVATE * psf,const double * ptr,sf_count_t len)2421 pcm_write_d2uc	(SF_PRIVATE *psf, const double *ptr, sf_count_t len)
2422 {	BUF_UNION	ubuf ;
2423 	void		(*convert) (const double *, unsigned char *, int, int) ;
2424 	int			bufferlen, writecount ;
2425 	sf_count_t	total = 0 ;
2426 
2427 	convert = (psf->add_clipping) ? d2uc_clip_array : d2uc_array ;
2428 	bufferlen = ARRAY_LEN (ubuf.ucbuf) ;
2429 
2430 	while (len > 0)
2431 	{	if (len < bufferlen)
2432 			bufferlen = (int) len ;
2433 		convert (ptr + total, ubuf.ucbuf, bufferlen, psf->norm_double) ;
2434 		writecount = psf_fwrite (ubuf.ucbuf, sizeof (unsigned char), bufferlen, psf) ;
2435 		total += writecount ;
2436 		if (writecount < bufferlen)
2437 			break ;
2438 		len -= writecount ;
2439 		} ;
2440 
2441 	return total ;
2442 } /* pcm_write_d2uc */
2443 
2444 /*==============================================================================
2445 */
2446 
2447 static void
d2bes_array(const double * src,short * dest,int count,int normalize)2448 d2bes_array (const double *src, short *dest, int count, int normalize)
2449 {	unsigned char	*ucptr ;
2450 	short			value ;
2451 	double			normfact ;
2452 
2453 	normfact = normalize ? (1.0 * 0x7FFF) : 1.0 ;
2454 	ucptr = ((unsigned char*) dest) + 2 * count ;
2455 
2456 	while (--count >= 0)
2457 	{	ucptr -= 2 ;
2458 		value = psf_lrint (src [count] * normfact) ;
2459 		ucptr [1] = value ;
2460 		ucptr [0] = value >> 8 ;
2461 		} ;
2462 } /* d2bes_array */
2463 
2464 static void
d2bes_clip_array(const double * src,short * dest,int count,int normalize)2465 d2bes_clip_array (const double *src, short *dest, int count, int normalize)
2466 {	unsigned char	*ucptr ;
2467 	double			normfact, scaled_value ;
2468 	int				value ;
2469 
2470 	normfact = normalize ? (8.0 * 0x10000000) : (1.0 * 0x10000) ;
2471 	ucptr = ((unsigned char*) dest) + 2 * count ;
2472 
2473 	while (--count >= 0)
2474 	{	ucptr -= 2 ;
2475 		scaled_value = src [count] * normfact ;
2476 		if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF))
2477 		{	ucptr [1] = 0xFF ;
2478 			ucptr [0] = 0x7F ;
2479 			continue ;
2480 			} ;
2481 		if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
2482 		{	ucptr [1] = 0x00 ;
2483 			ucptr [0] = 0x80 ;
2484 			continue ;
2485 			} ;
2486 
2487 		value = psf_lrint (scaled_value) ;
2488 		ucptr [1] = value >> 16 ;
2489 		ucptr [0] = value >> 24 ;
2490 		} ;
2491 } /* d2bes_clip_array */
2492 
2493 static sf_count_t
pcm_write_d2bes(SF_PRIVATE * psf,const double * ptr,sf_count_t len)2494 pcm_write_d2bes	(SF_PRIVATE *psf, const double *ptr, sf_count_t len)
2495 {	BUF_UNION	ubuf ;
2496 	void		(*convert) (const double *, short *, int, int) ;
2497 	int			bufferlen, writecount ;
2498 	sf_count_t	total = 0 ;
2499 
2500 	convert = (psf->add_clipping) ? d2bes_clip_array : d2bes_array ;
2501 	bufferlen = ARRAY_LEN (ubuf.sbuf) ;
2502 
2503 	while (len > 0)
2504 	{	if (len < bufferlen)
2505 			bufferlen = (int) len ;
2506 		convert (ptr + total, ubuf.sbuf, bufferlen, psf->norm_double) ;
2507 		writecount = psf_fwrite (ubuf.sbuf, sizeof (short), bufferlen, psf) ;
2508 		total += writecount ;
2509 		if (writecount < bufferlen)
2510 			break ;
2511 		len -= writecount ;
2512 		} ;
2513 
2514 	return total ;
2515 } /* pcm_write_d2bes */
2516 
2517 /*==============================================================================
2518 */
2519 
2520 static void
d2les_array(const double * src,short * dest,int count,int normalize)2521 d2les_array (const double *src, short *dest, int count, int normalize)
2522 {	unsigned char	*ucptr ;
2523 	short			value ;
2524 	double			normfact ;
2525 
2526 	normfact = normalize ? (1.0 * 0x7FFF) : 1.0 ;
2527 	ucptr = ((unsigned char*) dest) + 2 * count ;
2528 
2529 	while (--count >= 0)
2530 	{	ucptr -= 2 ;
2531 		value = psf_lrint (src [count] * normfact) ;
2532 		ucptr [0] = value ;
2533 		ucptr [1] = value >> 8 ;
2534 		} ;
2535 } /* d2les_array */
2536 
2537 static void
d2les_clip_array(const double * src,short * dest,int count,int normalize)2538 d2les_clip_array (const double *src, short *dest, int count, int normalize)
2539 {	unsigned char	*ucptr ;
2540 	int				value ;
2541 	double			normfact, scaled_value ;
2542 
2543 	normfact = normalize ? (8.0 * 0x10000000) : (1.0 * 0x10000) ;
2544 	ucptr = ((unsigned char*) dest) + 2 * count ;
2545 
2546 	while (--count >= 0)
2547 	{	ucptr -= 2 ;
2548 		scaled_value = src [count] * normfact ;
2549 		if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF))
2550 		{	ucptr [0] = 0xFF ;
2551 			ucptr [1] = 0x7F ;
2552 			continue ;
2553 			} ;
2554 		if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
2555 		{	ucptr [0] = 0x00 ;
2556 			ucptr [1] = 0x80 ;
2557 			continue ;
2558 			} ;
2559 
2560 		value = psf_lrint (scaled_value) ;
2561 		ucptr [0] = value >> 16 ;
2562 		ucptr [1] = value >> 24 ;
2563 		} ;
2564 } /* d2les_clip_array */
2565 
2566 static sf_count_t
pcm_write_d2les(SF_PRIVATE * psf,const double * ptr,sf_count_t len)2567 pcm_write_d2les	(SF_PRIVATE *psf, const double *ptr, sf_count_t len)
2568 {	BUF_UNION	ubuf ;
2569 	void		(*convert) (const double *, short *, int, int) ;
2570 	int			bufferlen, writecount ;
2571 	sf_count_t	total = 0 ;
2572 
2573 	convert = (psf->add_clipping) ? d2les_clip_array : d2les_array ;
2574 	bufferlen = ARRAY_LEN (ubuf.sbuf) ;
2575 
2576 	while (len > 0)
2577 	{	if (len < bufferlen)
2578 			bufferlen = (int) len ;
2579 		convert (ptr + total, ubuf.sbuf, bufferlen, psf->norm_double) ;
2580 		writecount = psf_fwrite (ubuf.sbuf, sizeof (short), bufferlen, psf) ;
2581 		total += writecount ;
2582 		if (writecount < bufferlen)
2583 			break ;
2584 		len -= writecount ;
2585 		} ;
2586 
2587 	return total ;
2588 } /* pcm_write_d2les */
2589 
2590 /*==============================================================================
2591 */
2592 
2593 static void
d2let_array(const double * src,tribyte * dest,int count,int normalize)2594 d2let_array (const double *src, tribyte *dest, int count, int normalize)
2595 {	int		value ;
2596 	double	normfact ;
2597 
2598 	normfact = normalize ? (1.0 * 0x7FFFFF) : 1.0 ;
2599 
2600 	while (--count >= 0)
2601 	{	value = psf_lrint (src [count] * normfact) ;
2602 		dest [count].bytes [0] = value ;
2603 		dest [count].bytes [1] = value >> 8 ;
2604 		dest [count].bytes [2] = value >> 16 ;
2605 		} ;
2606 } /* d2let_array */
2607 
2608 static void
d2let_clip_array(const double * src,tribyte * dest,int count,int normalize)2609 d2let_clip_array (const double *src, tribyte *dest, int count, int normalize)
2610 {	int		value ;
2611 	double	normfact, scaled_value ;
2612 
2613 	normfact = normalize ? (8.0 * 0x10000000) : (1.0 * 0x100) ;
2614 
2615 	while (--count >= 0)
2616 	{	scaled_value = src [count] * normfact ;
2617 		if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF))
2618 		{	dest [count].bytes [0] = 0xFF ;
2619 			dest [count].bytes [1] = 0xFF ;
2620 			dest [count].bytes [2] = 0x7F ;
2621 			continue ;
2622 			} ;
2623 		if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
2624 		{	dest [count].bytes [0] = 0x00 ;
2625 			dest [count].bytes [1] = 0x00 ;
2626 			dest [count].bytes [2] = 0x80 ;
2627 			continue ;
2628 			} ;
2629 
2630 		value = psf_lrint (scaled_value) ;
2631 		dest [count].bytes [0] = value >> 8 ;
2632 		dest [count].bytes [1] = value >> 16 ;
2633 		dest [count].bytes [2] = value >> 24 ;
2634 		} ;
2635 } /* d2let_clip_array */
2636 
2637 static sf_count_t
pcm_write_d2let(SF_PRIVATE * psf,const double * ptr,sf_count_t len)2638 pcm_write_d2let	(SF_PRIVATE *psf, const double *ptr, sf_count_t len)
2639 {	BUF_UNION	ubuf ;
2640 	void		(*convert) (const double *, tribyte *, int, int) ;
2641 	int			bufferlen, writecount ;
2642 	sf_count_t	total = 0 ;
2643 
2644 	convert = (psf->add_clipping) ? d2let_clip_array : d2let_array ;
2645 	bufferlen = sizeof (ubuf.ucbuf) / SIZEOF_TRIBYTE ;
2646 
2647 	while (len > 0)
2648 	{	if (len < bufferlen)
2649 			bufferlen = (int) len ;
2650 		convert (ptr + total, (tribyte*) (ubuf.ucbuf), bufferlen, psf->norm_double) ;
2651 		writecount = psf_fwrite (ubuf.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ;
2652 		total += writecount ;
2653 		if (writecount < bufferlen)
2654 			break ;
2655 		len -= writecount ;
2656 		} ;
2657 
2658 	return total ;
2659 } /* pcm_write_d2let */
2660 
2661 /*==============================================================================
2662 */
2663 
2664 static void
d2bet_array(const double * src,tribyte * dest,int count,int normalize)2665 d2bet_array (const double *src, tribyte *dest, int count, int normalize)
2666 {	int		value ;
2667 	double	normfact ;
2668 
2669 	normfact = normalize ? (1.0 * 0x7FFFFF) : 1.0 ;
2670 
2671 	while (--count >= 0)
2672 	{	value = psf_lrint (src [count] * normfact) ;
2673 		dest [count].bytes [2] = value ;
2674 		dest [count].bytes [1] = value >> 8 ;
2675 		dest [count].bytes [0] = value >> 16 ;
2676 		} ;
2677 } /* d2bet_array */
2678 
2679 static void
d2bet_clip_array(const double * src,tribyte * dest,int count,int normalize)2680 d2bet_clip_array (const double *src, tribyte *dest, int count, int normalize)
2681 {	int		value ;
2682 	double	normfact, scaled_value ;
2683 
2684 	normfact = normalize ? (8.0 * 0x10000000) : (1.0 * 0x100) ;
2685 
2686 	while (--count >= 0)
2687 	{	scaled_value = src [count] * normfact ;
2688 		if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF))
2689 		{	dest [count].bytes [2] = 0xFF ;
2690 			dest [count].bytes [1] = 0xFF ;
2691 			dest [count].bytes [0] = 0x7F ;
2692 			continue ;
2693 			} ;
2694 		if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
2695 		{	dest [count].bytes [2] = 0x00 ;
2696 			dest [count].bytes [1] = 0x00 ;
2697 			dest [count].bytes [0] = 0x80 ;
2698 			continue ;
2699 			} ;
2700 
2701 		value = psf_lrint (scaled_value) ;
2702 		dest [count].bytes [2] = value >> 8 ;
2703 		dest [count].bytes [1] = value >> 16 ;
2704 		dest [count].bytes [0] = value >> 24 ;
2705 		} ;
2706 } /* d2bet_clip_array */
2707 
2708 static sf_count_t
pcm_write_d2bet(SF_PRIVATE * psf,const double * ptr,sf_count_t len)2709 pcm_write_d2bet	(SF_PRIVATE *psf, const double *ptr, sf_count_t len)
2710 {	BUF_UNION	ubuf ;
2711 	void		(*convert) (const double *, tribyte *, int, int) ;
2712 	int			bufferlen, writecount ;
2713 	sf_count_t	total = 0 ;
2714 
2715 	convert = (psf->add_clipping) ? d2bet_clip_array : d2bet_array ;
2716 	bufferlen = sizeof (ubuf.ucbuf) / SIZEOF_TRIBYTE ;
2717 
2718 	while (len > 0)
2719 	{	if (len < bufferlen)
2720 			bufferlen = (int) len ;
2721 		convert (ptr + total, (tribyte*) (ubuf.ucbuf), bufferlen, psf->norm_double) ;
2722 		writecount = psf_fwrite (ubuf.ucbuf, SIZEOF_TRIBYTE, bufferlen, psf) ;
2723 		total += writecount ;
2724 		if (writecount < bufferlen)
2725 			break ;
2726 		len -= writecount ;
2727 		} ;
2728 
2729 	return total ;
2730 } /* pcm_write_d2bet */
2731 
2732 /*==============================================================================
2733 */
2734 
2735 static void
d2bei_array(const double * src,int * dest,int count,int normalize)2736 d2bei_array (const double *src, int *dest, int count, int normalize)
2737 {	unsigned char	*ucptr ;
2738 	int				value ;
2739 	double			normfact ;
2740 
2741 	normfact = normalize ? (1.0 * 0x7FFFFFFF) : 1.0 ;
2742 	ucptr = ((unsigned char*) dest) + 4 * count ;
2743 
2744 	while (--count >= 0)
2745 	{	ucptr -= 4 ;
2746 		value = psf_lrint (src [count] * normfact) ;
2747 		ucptr [0] = value >> 24 ;
2748 		ucptr [1] = value >> 16 ;
2749 		ucptr [2] = value >> 8 ;
2750 		ucptr [3] = value ;
2751 		} ;
2752 } /* d2bei_array */
2753 
2754 static void
d2bei_clip_array(const double * src,int * dest,int count,int normalize)2755 d2bei_clip_array (const double *src, int *dest, int count, int normalize)
2756 {	unsigned char	*ucptr ;
2757 	int				value ;
2758 	double			normfact, scaled_value ;
2759 
2760 	normfact = normalize ? (8.0 * 0x10000000) : 1.0 ;
2761 	ucptr = ((unsigned char*) dest) + 4 * count ;
2762 
2763 	while (--count >= 0)
2764 	{	ucptr -= 4 ;
2765 		scaled_value = src [count] * normfact ;
2766 		if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF))
2767 		{	ucptr [3] = 0xFF ;
2768 			ucptr [2] = 0xFF ;
2769 			ucptr [1] = 0xFF ;
2770 			ucptr [0] = 0x7F ;
2771 			continue ;
2772 			} ;
2773 		if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
2774 		{	ucptr [3] = 0x00 ;
2775 			ucptr [2] = 0x00 ;
2776 			ucptr [1] = 0x00 ;
2777 			ucptr [0] = 0x80 ;
2778 			continue ;
2779 			} ;
2780 
2781 		value = psf_lrint (scaled_value) ;
2782 		ucptr [0] = value >> 24 ;
2783 		ucptr [1] = value >> 16 ;
2784 		ucptr [2] = value >> 8 ;
2785 		ucptr [3] = value ;
2786 		} ;
2787 } /* d2bei_clip_array */
2788 
2789 static sf_count_t
pcm_write_d2bei(SF_PRIVATE * psf,const double * ptr,sf_count_t len)2790 pcm_write_d2bei	(SF_PRIVATE *psf, const double *ptr, sf_count_t len)
2791 {	BUF_UNION	ubuf ;
2792 	void		(*convert) (const double *, int *, int, int) ;
2793 	int			bufferlen, writecount ;
2794 	sf_count_t	total = 0 ;
2795 
2796 	convert = (psf->add_clipping) ? d2bei_clip_array : d2bei_array ;
2797 	bufferlen = ARRAY_LEN (ubuf.ibuf) ;
2798 
2799 	while (len > 0)
2800 	{	if (len < bufferlen)
2801 			bufferlen = (int) len ;
2802 		convert (ptr + total, ubuf.ibuf, bufferlen, psf->norm_double) ;
2803 		writecount = psf_fwrite (ubuf.ibuf, sizeof (int), bufferlen, psf) ;
2804 		total += writecount ;
2805 		if (writecount < bufferlen)
2806 			break ;
2807 		len -= writecount ;
2808 		} ;
2809 
2810 	return total ;
2811 } /* pcm_write_d2bei */
2812 
2813 /*==============================================================================
2814 */
2815 
2816 static void
d2lei_array(const double * src,int * dest,int count,int normalize)2817 d2lei_array (const double *src, int *dest, int count, int normalize)
2818 {	unsigned char	*ucptr ;
2819 	int				value ;
2820 	double			normfact ;
2821 
2822 	normfact = normalize ? (1.0 * 0x7FFFFFFF) : 1.0 ;
2823 	ucptr = ((unsigned char*) dest) + 4 * count ;
2824 
2825 	while (--count >= 0)
2826 	{	ucptr -= 4 ;
2827 		value = psf_lrint (src [count] * normfact) ;
2828 		ucptr [0] = value ;
2829 		ucptr [1] = value >> 8 ;
2830 		ucptr [2] = value >> 16 ;
2831 		ucptr [3] = value >> 24 ;
2832 		} ;
2833 } /* d2lei_array */
2834 
2835 static void
d2lei_clip_array(const double * src,int * dest,int count,int normalize)2836 d2lei_clip_array (const double *src, int *dest, int count, int normalize)
2837 {	unsigned char	*ucptr ;
2838 	int				value ;
2839 	double			normfact, scaled_value ;
2840 
2841 	normfact = normalize ? (8.0 * 0x10000000) : 1.0 ;
2842 	ucptr = ((unsigned char*) dest) + 4 * count ;
2843 
2844 	while (--count >= 0)
2845 	{	ucptr -= 4 ;
2846 		scaled_value = src [count] * normfact ;
2847 		if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF))
2848 		{	ucptr [0] = 0xFF ;
2849 			ucptr [1] = 0xFF ;
2850 			ucptr [2] = 0xFF ;
2851 			ucptr [3] = 0x7F ;
2852 			continue ;
2853 			} ;
2854 		if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
2855 		{	ucptr [0] = 0x00 ;
2856 			ucptr [1] = 0x00 ;
2857 			ucptr [2] = 0x00 ;
2858 			ucptr [3] = 0x80 ;
2859 			continue ;
2860 			} ;
2861 
2862 		value = psf_lrint (scaled_value) ;
2863 		ucptr [0] = value ;
2864 		ucptr [1] = value >> 8 ;
2865 		ucptr [2] = value >> 16 ;
2866 		ucptr [3] = value >> 24 ;
2867 		} ;
2868 } /* d2lei_clip_array */
2869 
2870 static sf_count_t
pcm_write_d2lei(SF_PRIVATE * psf,const double * ptr,sf_count_t len)2871 pcm_write_d2lei	(SF_PRIVATE *psf, const double *ptr, sf_count_t len)
2872 {	BUF_UNION	ubuf ;
2873 	void		(*convert) (const double *, int *, int, int) ;
2874 	int			bufferlen, writecount ;
2875 	sf_count_t	total = 0 ;
2876 
2877 	convert = (psf->add_clipping) ? d2lei_clip_array : d2lei_array ;
2878 	bufferlen = ARRAY_LEN (ubuf.ibuf) ;
2879 
2880 	while (len > 0)
2881 	{	if (len < bufferlen)
2882 			bufferlen = (int) len ;
2883 		convert (ptr + total, ubuf.ibuf, bufferlen, psf->norm_double) ;
2884 		writecount = psf_fwrite (ubuf.ibuf, sizeof (int), bufferlen, psf) ;
2885 		total += writecount ;
2886 		if (writecount < bufferlen)
2887 			break ;
2888 		len -= writecount ;
2889 		} ;
2890 
2891 	return total ;
2892 } /* pcm_write_d2lei */
2893 
2894