2 ** Copyright (C) 1999-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
4 ** This program is free software; you can redistribute it and/or modify
5 ** it under the terms of the GNU Lesser General Public License as published by
6 ** the Free Software Foundation; either version 2.1 of the License, or
7 ** (at your option) any later version.
9 ** This program is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ** GNU Lesser General Public License for more details.
14 ** You should have received a copy of the GNU Lesser General Public License
15 ** along with this program; if not, write to the Free Software
16 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
32 /*------------------------------------------------------------------------------
33 ** Macros to handle big/little endian issues.
36 #define FAP_MARKER (MAKE_MARKER ('f', 'a', 'p', ' '))
37 #define PAF_MARKER (MAKE_MARKER (' ', 'p', 'a', 'f'))
39 /*------------------------------------------------------------------------------
43 #define PAF_HEADER_LENGTH 2048
45 #define PAF24_SAMPLES_PER_BLOCK 10
46 #define PAF24_BLOCK_SIZE 32
48 /*------------------------------------------------------------------------------
62 { int max_blocks
, channels
, samplesperblock
, blocksize
;
63 int read_block
, write_block
, read_count
, write_count
;
64 sf_count_t sample_count
;
66 unsigned char *block
;
67 #if HAVE_FLEXIBLE_ARRAY
68 int data
[] ; /* ISO C99 struct flexible array. */
70 int data
[1] ; /* This is a hack and may not work. */
74 /*------------------------------------------------------------------------------
75 ** Private static functions.
78 static int paf24_init (SF_PRIVATE
*psf
) ;
80 static int paf_read_header (SF_PRIVATE
*psf
) ;
81 static int paf_write_header (SF_PRIVATE
*psf
, int calc_length
) ;
83 static sf_count_t
paf24_read_s (SF_PRIVATE
*psf
, short *ptr
, sf_count_t len
) ;
84 static sf_count_t
paf24_read_i (SF_PRIVATE
*psf
, int *ptr
, sf_count_t len
) ;
85 static sf_count_t
paf24_read_f (SF_PRIVATE
*psf
, float *ptr
, sf_count_t len
) ;
86 static sf_count_t
paf24_read_d (SF_PRIVATE
*psf
, double *ptr
, sf_count_t len
) ;
88 static sf_count_t
paf24_write_s (SF_PRIVATE
*psf
, const short *ptr
, sf_count_t len
) ;
89 static sf_count_t
paf24_write_i (SF_PRIVATE
*psf
, const int *ptr
, sf_count_t len
) ;
90 static sf_count_t
paf24_write_f (SF_PRIVATE
*psf
, const float *ptr
, sf_count_t len
) ;
91 static sf_count_t
paf24_write_d (SF_PRIVATE
*psf
, const double *ptr
, sf_count_t len
) ;
93 static sf_count_t
paf24_seek (SF_PRIVATE
*psf
, int mode
, sf_count_t offset
) ;
101 /*------------------------------------------------------------------------------
106 paf_open (SF_PRIVATE
*psf
)
107 { int subformat
, error
, endian
;
109 psf
->dataoffset
= PAF_HEADER_LENGTH
;
111 if (psf
->file
.mode
== SFM_READ
|| (psf
->file
.mode
== SFM_RDWR
&& psf
->filelength
> 0))
112 { if ((error
= paf_read_header (psf
)))
116 subformat
= SF_CODEC (psf
->sf
.format
) ;
118 if (psf
->file
.mode
== SFM_WRITE
|| psf
->file
.mode
== SFM_RDWR
)
119 { if ((SF_CONTAINER (psf
->sf
.format
)) != SF_FORMAT_PAF
)
120 return SFE_BAD_OPEN_FORMAT
;
122 endian
= SF_ENDIAN (psf
->sf
.format
) ;
124 /* PAF is by default big endian. */
125 psf
->endian
= SF_ENDIAN_BIG
;
127 if (endian
== SF_ENDIAN_LITTLE
|| (CPU_IS_LITTLE_ENDIAN
&& (endian
== SF_ENDIAN_CPU
)))
128 psf
->endian
= SF_ENDIAN_LITTLE
;
130 if ((error
= paf_write_header (psf
, SF_FALSE
)))
133 psf
->write_header
= paf_write_header
;
137 { case SF_FORMAT_PCM_S8
:
139 error
= pcm_init (psf
) ;
142 case SF_FORMAT_PCM_16
:
144 error
= pcm_init (psf
) ;
147 case SF_FORMAT_PCM_24
:
148 /* No bytewidth because of whacky 24 bit encoding. */
149 error
= paf24_init (psf
) ;
152 default : return SFE_PAF_UNKNOWN_FORMAT
;
158 /*------------------------------------------------------------------------------
162 paf_read_header (SF_PRIVATE
*psf
)
166 if (psf
->filelength
< PAF_HEADER_LENGTH
)
167 return SFE_PAF_SHORT_HEADER
;
169 memset (&paf_fmt
, 0, sizeof (paf_fmt
)) ;
170 psf_binheader_readf (psf
, "pm", 0, &marker
) ;
172 psf_log_printf (psf
, "Signature : '%M'\n", marker
) ;
174 if (marker
== PAF_MARKER
)
175 { psf_binheader_readf (psf
, "E444444", &(paf_fmt
.version
), &(paf_fmt
.endianness
),
176 &(paf_fmt
.samplerate
), &(paf_fmt
.format
), &(paf_fmt
.channels
), &(paf_fmt
.source
)) ;
178 else if (marker
== FAP_MARKER
)
179 { psf_binheader_readf (psf
, "e444444", &(paf_fmt
.version
), &(paf_fmt
.endianness
),
180 &(paf_fmt
.samplerate
), &(paf_fmt
.format
), &(paf_fmt
.channels
), &(paf_fmt
.source
)) ;
183 return SFE_PAF_NO_MARKER
;
185 psf_log_printf (psf
, "Version : %d\n", paf_fmt
.version
) ;
187 if (paf_fmt
.version
!= 0)
188 { psf_log_printf (psf
, "*** Bad version number. should be zero.\n") ;
189 return SFE_PAF_VERSION
;
192 psf_log_printf (psf
, "Sample Rate : %d\n", paf_fmt
.samplerate
) ;
193 psf_log_printf (psf
, "Channels : %d\n", paf_fmt
.channels
) ;
195 psf_log_printf (psf
, "Endianness : %d => ", paf_fmt
.endianness
) ;
196 if (paf_fmt
.endianness
)
197 { psf_log_printf (psf
, "Little\n", paf_fmt
.endianness
) ;
198 psf
->endian
= SF_ENDIAN_LITTLE
;
201 { psf_log_printf (psf
, "Big\n", paf_fmt
.endianness
) ;
202 psf
->endian
= SF_ENDIAN_BIG
;
205 if (paf_fmt
.channels
> SF_MAX_CHANNELS
)
206 return SFE_PAF_BAD_CHANNELS
;
208 psf
->datalength
= psf
->filelength
- psf
->dataoffset
;
210 psf_binheader_readf (psf
, "p", (int) psf
->dataoffset
) ;
212 psf
->sf
.samplerate
= paf_fmt
.samplerate
;
213 psf
->sf
.channels
= paf_fmt
.channels
;
215 /* Only fill in type major. */
216 psf
->sf
.format
= SF_FORMAT_PAF
;
218 psf_log_printf (psf
, "Format : %d => ", paf_fmt
.format
) ;
220 /* PAF is by default big endian. */
221 psf
->sf
.format
|= paf_fmt
.endianness
? SF_ENDIAN_LITTLE
: SF_ENDIAN_BIG
;
223 switch (paf_fmt
.format
)
225 psf_log_printf (psf
, "8 bit linear PCM\n") ;
228 psf
->sf
.format
|= SF_FORMAT_PCM_S8
;
230 psf
->blockwidth
= psf
->bytewidth
* psf
->sf
.channels
;
231 psf
->sf
.frames
= psf
->datalength
/ psf
->blockwidth
;
235 psf_log_printf (psf
, "16 bit linear PCM\n") ;
238 psf
->sf
.format
|= SF_FORMAT_PCM_16
;
240 psf
->blockwidth
= psf
->bytewidth
* psf
->sf
.channels
;
241 psf
->sf
.frames
= psf
->datalength
/ psf
->blockwidth
;
245 psf_log_printf (psf
, "24 bit linear PCM\n") ;
248 psf
->sf
.format
|= SF_FORMAT_PCM_24
;
250 psf
->blockwidth
= 0 ;
251 psf
->sf
.frames
= PAF24_SAMPLES_PER_BLOCK
* psf
->datalength
/
252 (PAF24_BLOCK_SIZE
* psf
->sf
.channels
) ;
255 default : psf_log_printf (psf
, "Unknown\n") ;
256 return SFE_PAF_UNKNOWN_FORMAT
;
260 psf_log_printf (psf
, "Source : %d => ", paf_fmt
.source
) ;
262 switch (paf_fmt
.source
)
263 { case 1 : psf_log_printf (psf
, "Analog Recording\n") ;
265 case 2 : psf_log_printf (psf
, "Digital Transfer\n") ;
267 case 3 : psf_log_printf (psf
, "Multi-track Mixdown\n") ;
269 case 5 : psf_log_printf (psf
, "Audio Resulting From DSP Processing\n") ;
271 default : psf_log_printf (psf
, "Unknown\n") ;
276 } /* paf_read_header */
279 paf_write_header (SF_PRIVATE
*psf
, int UNUSED (calc_length
))
282 /* PAF header already written so no need to re-write. */
283 if (psf_ftell (psf
) >= PAF_HEADER_LENGTH
)
286 psf
->dataoffset
= PAF_HEADER_LENGTH
;
288 switch (SF_CODEC (psf
->sf
.format
))
289 { case SF_FORMAT_PCM_S8
:
290 paf_format
= PAF_PCM_S8
;
293 case SF_FORMAT_PCM_16
:
294 paf_format
= PAF_PCM_16
;
297 case SF_FORMAT_PCM_24
:
298 paf_format
= PAF_PCM_24
;
301 default : return SFE_PAF_UNKNOWN_FORMAT
;
304 /* Reset the current header length to zero. */
305 psf
->header
[0] = 0 ;
308 if (psf
->endian
== SF_ENDIAN_BIG
)
309 { /* Marker, version, endianness, samplerate */
310 psf_binheader_writef (psf
, "Em444", PAF_MARKER
, 0, 0, psf
->sf
.samplerate
) ;
311 /* format, channels, source */
312 psf_binheader_writef (psf
, "E444", paf_format
, psf
->sf
.channels
, 0) ;
314 else if (psf
->endian
== SF_ENDIAN_LITTLE
)
315 { /* Marker, version, endianness, samplerate */
316 psf_binheader_writef (psf
, "em444", FAP_MARKER
, 0, 1, psf
->sf
.samplerate
) ;
317 /* format, channels, source */
318 psf_binheader_writef (psf
, "e444", paf_format
, psf
->sf
.channels
, 0) ;
321 /* Zero fill to dataoffset. */
322 psf_binheader_writef (psf
, "z", (size_t) (psf
->dataoffset
- psf
->headindex
)) ;
324 psf_fwrite (psf
->header
, psf
->headindex
, 1, psf
) ;
327 } /* paf_write_header */
329 /*===============================================================================
330 ** 24 bit PAF files have a really weird encoding.
331 ** For a mono file, 10 samples (each being 3 bytes) are packed into a 32 byte
332 ** block. The 8 ints in this 32 byte block are then endian swapped (as ints)
333 ** if necessary before being written to disk.
334 ** For a stereo file, blocks of 10 samples from the same channel are encoded
335 ** into 32 bytes as for the mono case. The 32 byte blocks are then interleaved
337 ** Reading has to reverse the above process :-).
340 ** The code below attempts to gain efficiency while maintaining readability.
343 static int paf24_read_block (SF_PRIVATE
*psf
, PAF24_PRIVATE
*ppaf24
) ;
344 static int paf24_write_block (SF_PRIVATE
*psf
, PAF24_PRIVATE
*ppaf24
) ;
345 static int paf24_close (SF_PRIVATE
*psf
) ;
349 paf24_init (SF_PRIVATE
*psf
)
350 { PAF24_PRIVATE
*ppaf24
;
353 paf24size
= sizeof (PAF24_PRIVATE
) + psf
->sf
.channels
*
354 (PAF24_BLOCK_SIZE
+ PAF24_SAMPLES_PER_BLOCK
* sizeof (int)) ;
357 ** Not exatly sure why this needs to be here but the tests
362 if (! (psf
->codec_data
= calloc (1, paf24size
)))
363 return SFE_MALLOC_FAILED
;
365 ppaf24
= (PAF24_PRIVATE
*) psf
->codec_data
;
367 ppaf24
->channels
= psf
->sf
.channels
;
368 ppaf24
->samples
= ppaf24
->data
;
369 ppaf24
->block
= (unsigned char*) (ppaf24
->data
+ PAF24_SAMPLES_PER_BLOCK
* ppaf24
->channels
) ;
371 ppaf24
->blocksize
= PAF24_BLOCK_SIZE
* ppaf24
->channels
;
372 ppaf24
->samplesperblock
= PAF24_SAMPLES_PER_BLOCK
;
374 if (psf
->file
.mode
== SFM_READ
|| psf
->file
.mode
== SFM_RDWR
)
375 { paf24_read_block (psf
, ppaf24
) ; /* Read first block. */
377 psf
->read_short
= paf24_read_s
;
378 psf
->read_int
= paf24_read_i
;
379 psf
->read_float
= paf24_read_f
;
380 psf
->read_double
= paf24_read_d
;
383 if (psf
->file
.mode
== SFM_WRITE
|| psf
->file
.mode
== SFM_RDWR
)
384 { psf
->write_short
= paf24_write_s
;
385 psf
->write_int
= paf24_write_i
;
386 psf
->write_float
= paf24_write_f
;
387 psf
->write_double
= paf24_write_d
;
390 psf
->seek
= paf24_seek
;
391 psf
->container_close
= paf24_close
;
393 psf
->filelength
= psf_get_filelen (psf
) ;
394 psf
->datalength
= psf
->filelength
- psf
->dataoffset
;
396 if (psf
->datalength
% PAF24_BLOCK_SIZE
)
397 { if (psf
->file
.mode
== SFM_READ
)
398 psf_log_printf (psf
, "*** Warning : file seems to be truncated.\n") ;
399 ppaf24
->max_blocks
= psf
->datalength
/ ppaf24
->blocksize
+ 1 ;
402 ppaf24
->max_blocks
= psf
->datalength
/ ppaf24
->blocksize
;
404 ppaf24
->read_block
= 0 ;
405 if (psf
->file
.mode
== SFM_RDWR
)
406 ppaf24
->write_block
= ppaf24
->max_blocks
;
408 ppaf24
->write_block
= 0 ;
410 psf
->sf
.frames
= ppaf24
->samplesperblock
* ppaf24
->max_blocks
;
411 ppaf24
->sample_count
= psf
->sf
.frames
;
417 paf24_seek (SF_PRIVATE
*psf
, int mode
, sf_count_t offset
)
418 { PAF24_PRIVATE
*ppaf24
;
419 int newblock
, newsample
;
421 if (psf
->codec_data
== NULL
)
422 { psf
->error
= SFE_INTERNAL
;
423 return PSF_SEEK_ERROR
;
426 ppaf24
= (PAF24_PRIVATE
*) psf
->codec_data
;
428 if (mode
== SFM_READ
&& ppaf24
->write_count
> 0)
429 paf24_write_block (psf
, ppaf24
) ;
431 newblock
= offset
/ ppaf24
->samplesperblock
;
432 newsample
= offset
% ppaf24
->samplesperblock
;
436 if (psf
->last_op
== SFM_WRITE
&& ppaf24
->write_count
)
437 paf24_write_block (psf
, ppaf24
) ;
439 psf_fseek (psf
, psf
->dataoffset
+ newblock
* ppaf24
->blocksize
, SEEK_SET
) ;
440 ppaf24
->read_block
= newblock
;
441 paf24_read_block (psf
, ppaf24
) ;
442 ppaf24
->read_count
= newsample
;
446 if (offset
> ppaf24
->sample_count
)
447 { psf
->error
= SFE_BAD_SEEK
;
448 return PSF_SEEK_ERROR
;
451 if (psf
->last_op
== SFM_WRITE
&& ppaf24
->write_count
)
452 paf24_write_block (psf
, ppaf24
) ;
454 psf_fseek (psf
, psf
->dataoffset
+ newblock
* ppaf24
->blocksize
, SEEK_SET
) ;
455 ppaf24
->write_block
= newblock
;
456 paf24_read_block (psf
, ppaf24
) ;
457 ppaf24
->write_count
= newsample
;
461 psf
->error
= SFE_BAD_SEEK
;
462 return PSF_SEEK_ERROR
;
465 return newblock
* ppaf24
->samplesperblock
+ newsample
;
469 paf24_close (SF_PRIVATE
*psf
)
470 { PAF24_PRIVATE
*ppaf24
;
472 if (psf
->codec_data
== NULL
)
475 ppaf24
= (PAF24_PRIVATE
*) psf
->codec_data
;
477 if (psf
->file
.mode
== SFM_WRITE
|| psf
->file
.mode
== SFM_RDWR
)
478 { if (ppaf24
->write_count
> 0)
479 paf24_write_block (psf
, ppaf24
) ;
485 /*---------------------------------------------------------------------------
488 paf24_read_block (SF_PRIVATE
*psf
, PAF24_PRIVATE
*ppaf24
)
490 unsigned char *cptr
;
492 ppaf24
->read_block
++ ;
493 ppaf24
->read_count
= 0 ;
495 if (ppaf24
->read_block
* ppaf24
->samplesperblock
> ppaf24
->sample_count
)
496 { memset (ppaf24
->samples
, 0, ppaf24
->samplesperblock
* ppaf24
->channels
) ;
500 /* Read the block. */
501 if ((k
= psf_fread (ppaf24
->block
, 1, ppaf24
->blocksize
, psf
)) != ppaf24
->blocksize
)
502 psf_log_printf (psf
, "*** Warning : short read (%d != %d).\n", k
, ppaf24
->blocksize
) ;
505 if (CPU_IS_LITTLE_ENDIAN
)
506 { /* Do endian swapping if necessary. */
507 if (psf
->endian
== SF_ENDIAN_BIG
)
508 endswap_int_array (ppaf24
->data
, 8 * ppaf24
->channels
) ;
511 for (k
= 0 ; k
< PAF24_SAMPLES_PER_BLOCK
* ppaf24
->channels
; k
++)
512 { channel
= k
% ppaf24
->channels
;
513 cptr
= ppaf24
->block
+ PAF24_BLOCK_SIZE
* channel
+ 3 * (k
/ ppaf24
->channels
) ;
514 ppaf24
->samples
[k
] = (cptr
[0] << 8) | (cptr
[1] << 16) | (cptr
[2] << 24) ;
518 { /* Do endian swapping if necessary. */
519 if (psf
->endian
== SF_ENDIAN_BIG
)
520 endswap_int_array (ppaf24
->data
, 8 * ppaf24
->channels
) ;
523 for (k
= 0 ; k
< PAF24_SAMPLES_PER_BLOCK
* ppaf24
->channels
; k
++)
524 { channel
= k
% ppaf24
->channels
;
525 cptr
= ppaf24
->block
+ PAF24_BLOCK_SIZE
* channel
+ 3 * (k
/ ppaf24
->channels
) ;
526 ppaf24
->samples
[k
] = (cptr
[0] << 8) | (cptr
[1] << 16) | (cptr
[2] << 24) ;
531 } /* paf24_read_block */
534 paf24_read (SF_PRIVATE
*psf
, PAF24_PRIVATE
*ppaf24
, int *ptr
, int len
)
535 { int count
, total
= 0 ;
538 { if (ppaf24
->read_block
* ppaf24
->samplesperblock
>= ppaf24
->sample_count
)
539 { memset (&(ptr
[total
]), 0, (len
- total
) * sizeof (int)) ;
543 if (ppaf24
->read_count
>= ppaf24
->samplesperblock
)
544 paf24_read_block (psf
, ppaf24
) ;
546 count
= (ppaf24
->samplesperblock
- ppaf24
->read_count
) * ppaf24
->channels
;
547 count
= (len
- total
> count
) ? count
: len
- total
;
549 memcpy (&(ptr
[total
]), &(ppaf24
->samples
[ppaf24
->read_count
* ppaf24
->channels
]), count
* sizeof (int)) ;
551 ppaf24
->read_count
+= count
/ ppaf24
->channels
;
558 paf24_read_s (SF_PRIVATE
*psf
, short *ptr
, sf_count_t len
)
559 { PAF24_PRIVATE
*ppaf24
;
561 int k
, bufferlen
, readcount
, count
;
562 sf_count_t total
= 0 ;
564 if (psf
->codec_data
== NULL
)
566 ppaf24
= (PAF24_PRIVATE
*) psf
->codec_data
;
569 bufferlen
= ARRAY_LEN (psf
->u
.ibuf
) ;
571 { readcount
= (len
>= bufferlen
) ? bufferlen
: len
;
572 count
= paf24_read (psf
, ppaf24
, iptr
, readcount
) ;
573 for (k
= 0 ; k
< readcount
; k
++)
574 ptr
[total
+ k
] = iptr
[k
] >> 16 ;
582 paf24_read_i (SF_PRIVATE
*psf
, int *ptr
, sf_count_t len
)
583 { PAF24_PRIVATE
*ppaf24
;
586 if (psf
->codec_data
== NULL
)
588 ppaf24
= (PAF24_PRIVATE
*) psf
->codec_data
;
590 total
= paf24_read (psf
, ppaf24
, ptr
, len
) ;
596 paf24_read_f (SF_PRIVATE
*psf
, float *ptr
, sf_count_t len
)
597 { PAF24_PRIVATE
*ppaf24
;
599 int k
, bufferlen
, readcount
, count
;
600 sf_count_t total
= 0 ;
603 if (psf
->codec_data
== NULL
)
605 ppaf24
= (PAF24_PRIVATE
*) psf
->codec_data
;
607 normfact
= (psf
->norm_float
== SF_TRUE
) ? (1.0 / 0x80000000) : (1.0 / 0x100) ;
610 bufferlen
= ARRAY_LEN (psf
->u
.ibuf
) ;
612 { readcount
= (len
>= bufferlen
) ? bufferlen
: len
;
613 count
= paf24_read (psf
, ppaf24
, iptr
, readcount
) ;
614 for (k
= 0 ; k
< readcount
; k
++)
615 ptr
[total
+ k
] = normfact
* iptr
[k
] ;
623 paf24_read_d (SF_PRIVATE
*psf
, double *ptr
, sf_count_t len
)
624 { PAF24_PRIVATE
*ppaf24
;
626 int k
, bufferlen
, readcount
, count
;
627 sf_count_t total
= 0 ;
630 if (psf
->codec_data
== NULL
)
632 ppaf24
= (PAF24_PRIVATE
*) psf
->codec_data
;
634 normfact
= (psf
->norm_double
== SF_TRUE
) ? (1.0 / 0x80000000) : (1.0 / 0x100) ;
637 bufferlen
= ARRAY_LEN (psf
->u
.ibuf
) ;
639 { readcount
= (len
>= bufferlen
) ? bufferlen
: len
;
640 count
= paf24_read (psf
, ppaf24
, iptr
, readcount
) ;
641 for (k
= 0 ; k
< readcount
; k
++)
642 ptr
[total
+ k
] = normfact
* iptr
[k
] ;
649 /*---------------------------------------------------------------------------
653 paf24_write_block (SF_PRIVATE
*psf
, PAF24_PRIVATE
*ppaf24
)
654 { int k
, nextsample
, channel
;
655 unsigned char *cptr
;
657 /* First pack block. */
659 if (CPU_IS_LITTLE_ENDIAN
)
660 { for (k
= 0 ; k
< PAF24_SAMPLES_PER_BLOCK
* ppaf24
->channels
; k
++)
661 { channel
= k
% ppaf24
->channels
;
662 cptr
= ppaf24
->block
+ PAF24_BLOCK_SIZE
* channel
+ 3 * (k
/ ppaf24
->channels
) ;
663 nextsample
= ppaf24
->samples
[k
] >> 8 ;
664 cptr
[0] = nextsample
;
665 cptr
[1] = nextsample
>> 8 ;
666 cptr
[2] = nextsample
>> 16 ;
669 /* Do endian swapping if necessary. */
670 if (psf
->endian
== SF_ENDIAN_BIG
)
671 endswap_int_array (ppaf24
->data
, 8 * ppaf24
->channels
) ;
673 else if (CPU_IS_BIG_ENDIAN
)
674 { /* This is correct. */
675 for (k
= 0 ; k
< PAF24_SAMPLES_PER_BLOCK
* ppaf24
->channels
; k
++)
676 { channel
= k
% ppaf24
->channels
;
677 cptr
= ppaf24
->block
+ PAF24_BLOCK_SIZE
* channel
+ 3 * (k
/ ppaf24
->channels
) ;
678 nextsample
= ppaf24
->samples
[k
] >> 8 ;
679 cptr
[0] = nextsample
;
680 cptr
[1] = nextsample
>> 8 ;
681 cptr
[2] = nextsample
>> 16 ;
683 if (psf
->endian
== SF_ENDIAN_BIG
)
684 endswap_int_array (ppaf24
->data
, 8 * ppaf24
->channels
) ;
687 /* Write block to disk. */
688 if ((k
= psf_fwrite (ppaf24
->block
, 1, ppaf24
->blocksize
, psf
)) != ppaf24
->blocksize
)
689 psf_log_printf (psf
, "*** Warning : short write (%d != %d).\n", k
, ppaf24
->blocksize
) ;
691 if (ppaf24
->sample_count
< ppaf24
->write_block
* ppaf24
->samplesperblock
+ ppaf24
->write_count
)
692 ppaf24
->sample_count
= ppaf24
->write_block
* ppaf24
->samplesperblock
+ ppaf24
->write_count
;
694 if (ppaf24
->write_count
== ppaf24
->samplesperblock
)
695 { ppaf24
->write_block
++ ;
696 ppaf24
->write_count
= 0 ;
700 } /* paf24_write_block */
703 paf24_write (SF_PRIVATE
*psf
, PAF24_PRIVATE
*ppaf24
, const int *ptr
, int len
)
704 { int count
, total
= 0 ;
707 { count
= (ppaf24
->samplesperblock
- ppaf24
->write_count
) * ppaf24
->channels
;
709 if (count
> len
- total
)
710 count
= len
- total
;
712 memcpy (&(ppaf24
->samples
[ppaf24
->write_count
* ppaf24
->channels
]), &(ptr
[total
]), count
* sizeof (int)) ;
714 ppaf24
->write_count
+= count
/ ppaf24
->channels
;
716 if (ppaf24
->write_count
>= ppaf24
->samplesperblock
)
717 paf24_write_block (psf
, ppaf24
) ;
724 paf24_write_s (SF_PRIVATE
*psf
, const short *ptr
, sf_count_t len
)
725 { PAF24_PRIVATE
*ppaf24
;
727 int k
, bufferlen
, writecount
= 0, count
;
728 sf_count_t total
= 0 ;
730 if (psf
->codec_data
== NULL
)
732 ppaf24
= (PAF24_PRIVATE
*) psf
->codec_data
;
735 bufferlen
= ARRAY_LEN (psf
->u
.ibuf
) ;
737 { writecount
= (len
>= bufferlen
) ? bufferlen
: len
;
738 for (k
= 0 ; k
< writecount
; k
++)
739 iptr
[k
] = ptr
[total
+ k
] << 16 ;
740 count
= paf24_write (psf
, ppaf24
, iptr
, writecount
) ;
743 if (count
!= writecount
)
747 } /* paf24_write_s */
750 paf24_write_i (SF_PRIVATE
*psf
, const int *ptr
, sf_count_t len
)
751 { PAF24_PRIVATE
*ppaf24
;
752 int writecount
, count
;
753 sf_count_t total
= 0 ;
755 if (psf
->codec_data
== NULL
)
757 ppaf24
= (PAF24_PRIVATE
*) psf
->codec_data
;
760 { writecount
= (len
> 0x10000000) ? 0x10000000 : (int) len
;
762 count
= paf24_write (psf
, ppaf24
, ptr
, writecount
) ;
766 if (count
!= writecount
)
771 } /* paf24_write_i */
774 paf24_write_f (SF_PRIVATE
*psf
, const float *ptr
, sf_count_t len
)
775 { PAF24_PRIVATE
*ppaf24
;
777 int k
, bufferlen
, writecount
= 0, count
;
778 sf_count_t total
= 0 ;
781 if (psf
->codec_data
== NULL
)
783 ppaf24
= (PAF24_PRIVATE
*) psf
->codec_data
;
785 normfact
= (psf
->norm_float
== SF_TRUE
) ? (1.0 * 0x7FFFFFFF) : (1.0 / 0x100) ;
788 bufferlen
= ARRAY_LEN (psf
->u
.ibuf
) ;
790 { writecount
= (len
>= bufferlen
) ? bufferlen
: len
;
791 for (k
= 0 ; k
< writecount
; k
++)
792 iptr
[k
] = lrintf (normfact
* ptr
[total
+ k
]) ;
793 count
= paf24_write (psf
, ppaf24
, iptr
, writecount
) ;
796 if (count
!= writecount
)
801 } /* paf24_write_f */
804 paf24_write_d (SF_PRIVATE
*psf
, const double *ptr
, sf_count_t len
)
805 { PAF24_PRIVATE
*ppaf24
;
807 int k
, bufferlen
, writecount
= 0, count
;
808 sf_count_t total
= 0 ;
811 if (psf
->codec_data
== NULL
)
813 ppaf24
= (PAF24_PRIVATE
*) psf
->codec_data
;
815 normfact
= (psf
->norm_double
== SF_TRUE
) ? (1.0 * 0x7FFFFFFF) : (1.0 / 0x100) ;
818 bufferlen
= ARRAY_LEN (psf
->u
.ibuf
) ;
820 { writecount
= (len
>= bufferlen
) ? bufferlen
: len
;
821 for (k
= 0 ; k
< writecount
; k
++)
822 iptr
[k
] = lrint (normfact
* ptr
[total
+k
]) ;
823 count
= paf24_write (psf
, ppaf24
, iptr
, writecount
) ;
826 if (count
!= writecount
)
831 } /* paf24_write_d */