~ubuntu-branches/ubuntu/gutsy/audacity/gutsy-backports

« back to all changes in this revision

Viewing changes to lib-src/libsndfile/src/pcm.c

  • Committer: Bazaar Package Importer
  • Author(s): John Dong
  • Date: 2008-02-18 21:58:19 UTC
  • mfrom: (13.1.2 hardy)
  • Revision ID: james.westby@ubuntu.com-20080218215819-tmbcf1rx238r8gdv
Tags: 1.3.4-1.1ubuntu1~gutsy1
Automated backport upload; no source changes.

Show diffs side-by-side

added added

removed removed

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