Coverage Report

Created: 2025-10-21 06:11

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