7c7d527f474332d701ab5c774375cf83a101c0f3
[Faustine.git] / interpretor / lib / src / libsndfile-1.0.25 / src / paf.c
1 /*
2 ** Copyright (C) 1999-2011 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 <stdio.h>
22 #include <stdlib.h>
23 #include <fcntl.h>
24 #include <string.h>
25 #include <ctype.h>
26 #include <math.h>
27
28 #include "sndfile.h"
29 #include "sfendian.h"
30 #include "common.h"
31
32 /*------------------------------------------------------------------------------
33 ** Macros to handle big/little endian issues.
34 */
35
36 #define FAP_MARKER (MAKE_MARKER ('f', 'a', 'p', ' '))
37 #define PAF_MARKER (MAKE_MARKER (' ', 'p', 'a', 'f'))
38
39 /*------------------------------------------------------------------------------
40 ** Other defines.
41 */
42
43 #define PAF_HEADER_LENGTH 2048
44
45 #define PAF24_SAMPLES_PER_BLOCK 10
46 #define PAF24_BLOCK_SIZE 32
47
48 /*------------------------------------------------------------------------------
49 ** Typedefs.
50 */
51
52 typedef struct
53 { int version ;
54 int endianness ;
55 int samplerate ;
56 int format ;
57 int channels ;
58 int source ;
59 } PAF_FMT ;
60
61 typedef struct
62 { int max_blocks, channels, samplesperblock, blocksize ;
63 int read_block, write_block, read_count, write_count ;
64 sf_count_t sample_count ;
65 int *samples ;
66 unsigned char *block ;
67 #if HAVE_FLEXIBLE_ARRAY
68 int data [] ; /* ISO C99 struct flexible array. */
69 #else
70 int data [1] ; /* This is a hack and may not work. */
71 #endif
72 } PAF24_PRIVATE ;
73
74 /*------------------------------------------------------------------------------
75 ** Private static functions.
76 */
77
78 static int paf24_init (SF_PRIVATE *psf) ;
79
80 static int paf_read_header (SF_PRIVATE *psf) ;
81 static int paf_write_header (SF_PRIVATE *psf, int calc_length) ;
82
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) ;
87
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) ;
92
93 static sf_count_t paf24_seek (SF_PRIVATE *psf, int mode, sf_count_t offset) ;
94
95 enum
96 { PAF_PCM_16 = 0,
97 PAF_PCM_24 = 1,
98 PAF_PCM_S8 = 2
99 } ;
100
101 /*------------------------------------------------------------------------------
102 ** Public function.
103 */
104
105 int
106 paf_open (SF_PRIVATE *psf)
107 { int subformat, error, endian ;
108
109 psf->dataoffset = PAF_HEADER_LENGTH ;
110
111 if (psf->file.mode == SFM_READ || (psf->file.mode == SFM_RDWR && psf->filelength > 0))
112 { if ((error = paf_read_header (psf)))
113 return error ;
114 } ;
115
116 subformat = SF_CODEC (psf->sf.format) ;
117
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 ;
121
122 endian = SF_ENDIAN (psf->sf.format) ;
123
124 /* PAF is by default big endian. */
125 psf->endian = SF_ENDIAN_BIG ;
126
127 if (endian == SF_ENDIAN_LITTLE || (CPU_IS_LITTLE_ENDIAN && (endian == SF_ENDIAN_CPU)))
128 psf->endian = SF_ENDIAN_LITTLE ;
129
130 if ((error = paf_write_header (psf, SF_FALSE)))
131 return error ;
132
133 psf->write_header = paf_write_header ;
134 } ;
135
136 switch (subformat)
137 { case SF_FORMAT_PCM_S8 :
138 psf->bytewidth = 1 ;
139 error = pcm_init (psf) ;
140 break ;
141
142 case SF_FORMAT_PCM_16 :
143 psf->bytewidth = 2 ;
144 error = pcm_init (psf) ;
145 break ;
146
147 case SF_FORMAT_PCM_24 :
148 /* No bytewidth because of whacky 24 bit encoding. */
149 error = paf24_init (psf) ;
150 break ;
151
152 default : return SFE_PAF_UNKNOWN_FORMAT ;
153 } ;
154
155 return error ;
156 } /* paf_open */
157
158 /*------------------------------------------------------------------------------
159 */
160
161 static int
162 paf_read_header (SF_PRIVATE *psf)
163 { PAF_FMT paf_fmt ;
164 int marker ;
165
166 if (psf->filelength < PAF_HEADER_LENGTH)
167 return SFE_PAF_SHORT_HEADER ;
168
169 memset (&paf_fmt, 0, sizeof (paf_fmt)) ;
170 psf_binheader_readf (psf, "pm", 0, &marker) ;
171
172 psf_log_printf (psf, "Signature : '%M'\n", marker) ;
173
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)) ;
177 }
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)) ;
181 }
182 else
183 return SFE_PAF_NO_MARKER ;
184
185 psf_log_printf (psf, "Version : %d\n", paf_fmt.version) ;
186
187 if (paf_fmt.version != 0)
188 { psf_log_printf (psf, "*** Bad version number. should be zero.\n") ;
189 return SFE_PAF_VERSION ;
190 } ;
191
192 psf_log_printf (psf, "Sample Rate : %d\n", paf_fmt.samplerate) ;
193 psf_log_printf (psf, "Channels : %d\n", paf_fmt.channels) ;
194
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 ;
199 }
200 else
201 { psf_log_printf (psf, "Big\n", paf_fmt.endianness) ;
202 psf->endian = SF_ENDIAN_BIG ;
203 } ;
204
205 if (paf_fmt.channels > SF_MAX_CHANNELS)
206 return SFE_PAF_BAD_CHANNELS ;
207
208 psf->datalength = psf->filelength - psf->dataoffset ;
209
210 psf_binheader_readf (psf, "p", (int) psf->dataoffset) ;
211
212 psf->sf.samplerate = paf_fmt.samplerate ;
213 psf->sf.channels = paf_fmt.channels ;
214
215 /* Only fill in type major. */
216 psf->sf.format = SF_FORMAT_PAF ;
217
218 psf_log_printf (psf, "Format : %d => ", paf_fmt.format) ;
219
220 /* PAF is by default big endian. */
221 psf->sf.format |= paf_fmt.endianness ? SF_ENDIAN_LITTLE : SF_ENDIAN_BIG ;
222
223 switch (paf_fmt.format)
224 { case PAF_PCM_S8 :
225 psf_log_printf (psf, "8 bit linear PCM\n") ;
226 psf->bytewidth = 1 ;
227
228 psf->sf.format |= SF_FORMAT_PCM_S8 ;
229
230 psf->blockwidth = psf->bytewidth * psf->sf.channels ;
231 psf->sf.frames = psf->datalength / psf->blockwidth ;
232 break ;
233
234 case PAF_PCM_16 :
235 psf_log_printf (psf, "16 bit linear PCM\n") ;
236 psf->bytewidth = 2 ;
237
238 psf->sf.format |= SF_FORMAT_PCM_16 ;
239
240 psf->blockwidth = psf->bytewidth * psf->sf.channels ;
241 psf->sf.frames = psf->datalength / psf->blockwidth ;
242 break ;
243
244 case PAF_PCM_24 :
245 psf_log_printf (psf, "24 bit linear PCM\n") ;
246 psf->bytewidth = 3 ;
247
248 psf->sf.format |= SF_FORMAT_PCM_24 ;
249
250 psf->blockwidth = 0 ;
251 psf->sf.frames = PAF24_SAMPLES_PER_BLOCK * psf->datalength /
252 (PAF24_BLOCK_SIZE * psf->sf.channels) ;
253 break ;
254
255 default : psf_log_printf (psf, "Unknown\n") ;
256 return SFE_PAF_UNKNOWN_FORMAT ;
257 break ;
258 } ;
259
260 psf_log_printf (psf, "Source : %d => ", paf_fmt.source) ;
261
262 switch (paf_fmt.source)
263 { case 1 : psf_log_printf (psf, "Analog Recording\n") ;
264 break ;
265 case 2 : psf_log_printf (psf, "Digital Transfer\n") ;
266 break ;
267 case 3 : psf_log_printf (psf, "Multi-track Mixdown\n") ;
268 break ;
269 case 5 : psf_log_printf (psf, "Audio Resulting From DSP Processing\n") ;
270 break ;
271 default : psf_log_printf (psf, "Unknown\n") ;
272 break ;
273 } ;
274
275 return 0 ;
276 } /* paf_read_header */
277
278 static int
279 paf_write_header (SF_PRIVATE *psf, int UNUSED (calc_length))
280 { int paf_format ;
281
282 /* PAF header already written so no need to re-write. */
283 if (psf_ftell (psf) >= PAF_HEADER_LENGTH)
284 return 0 ;
285
286 psf->dataoffset = PAF_HEADER_LENGTH ;
287
288 switch (SF_CODEC (psf->sf.format))
289 { case SF_FORMAT_PCM_S8 :
290 paf_format = PAF_PCM_S8 ;
291 break ;
292
293 case SF_FORMAT_PCM_16 :
294 paf_format = PAF_PCM_16 ;
295 break ;
296
297 case SF_FORMAT_PCM_24 :
298 paf_format = PAF_PCM_24 ;
299 break ;
300
301 default : return SFE_PAF_UNKNOWN_FORMAT ;
302 } ;
303
304 /* Reset the current header length to zero. */
305 psf->header [0] = 0 ;
306 psf->headindex = 0 ;
307
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) ;
313 }
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) ;
319 } ;
320
321 /* Zero fill to dataoffset. */
322 psf_binheader_writef (psf, "z", (size_t) (psf->dataoffset - psf->headindex)) ;
323
324 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
325
326 return psf->error ;
327 } /* paf_write_header */
328
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
336 ** on disk.
337 ** Reading has to reverse the above process :-).
338 ** Weird!!!
339 **
340 ** The code below attempts to gain efficiency while maintaining readability.
341 */
342
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) ;
346
347
348 static int
349 paf24_init (SF_PRIVATE *psf)
350 { PAF24_PRIVATE *ppaf24 ;
351 int paf24size ;
352
353 paf24size = sizeof (PAF24_PRIVATE) + psf->sf.channels *
354 (PAF24_BLOCK_SIZE + PAF24_SAMPLES_PER_BLOCK * sizeof (int)) ;
355
356 /*
357 ** Not exatly sure why this needs to be here but the tests
358 ** fail without it.
359 */
360 psf->last_op = 0 ;
361
362 if (! (psf->codec_data = calloc (1, paf24size)))
363 return SFE_MALLOC_FAILED ;
364
365 ppaf24 = (PAF24_PRIVATE*) psf->codec_data ;
366
367 ppaf24->channels = psf->sf.channels ;
368 ppaf24->samples = ppaf24->data ;
369 ppaf24->block = (unsigned char*) (ppaf24->data + PAF24_SAMPLES_PER_BLOCK * ppaf24->channels) ;
370
371 ppaf24->blocksize = PAF24_BLOCK_SIZE * ppaf24->channels ;
372 ppaf24->samplesperblock = PAF24_SAMPLES_PER_BLOCK ;
373
374 if (psf->file.mode == SFM_READ || psf->file.mode == SFM_RDWR)
375 { paf24_read_block (psf, ppaf24) ; /* Read first block. */
376
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 ;
381 } ;
382
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 ;
388 } ;
389
390 psf->seek = paf24_seek ;
391 psf->container_close = paf24_close ;
392
393 psf->filelength = psf_get_filelen (psf) ;
394 psf->datalength = psf->filelength - psf->dataoffset ;
395
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 ;
400 }
401 else
402 ppaf24->max_blocks = psf->datalength / ppaf24->blocksize ;
403
404 ppaf24->read_block = 0 ;
405 if (psf->file.mode == SFM_RDWR)
406 ppaf24->write_block = ppaf24->max_blocks ;
407 else
408 ppaf24->write_block = 0 ;
409
410 psf->sf.frames = ppaf24->samplesperblock * ppaf24->max_blocks ;
411 ppaf24->sample_count = psf->sf.frames ;
412
413 return 0 ;
414 } /* paf24_init */
415
416 static sf_count_t
417 paf24_seek (SF_PRIVATE *psf, int mode, sf_count_t offset)
418 { PAF24_PRIVATE *ppaf24 ;
419 int newblock, newsample ;
420
421 if (psf->codec_data == NULL)
422 { psf->error = SFE_INTERNAL ;
423 return PSF_SEEK_ERROR ;
424 } ;
425
426 ppaf24 = (PAF24_PRIVATE*) psf->codec_data ;
427
428 if (mode == SFM_READ && ppaf24->write_count > 0)
429 paf24_write_block (psf, ppaf24) ;
430
431 newblock = offset / ppaf24->samplesperblock ;
432 newsample = offset % ppaf24->samplesperblock ;
433
434 switch (mode)
435 { case SFM_READ :
436 if (psf->last_op == SFM_WRITE && ppaf24->write_count)
437 paf24_write_block (psf, ppaf24) ;
438
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 ;
443 break ;
444
445 case SFM_WRITE :
446 if (offset > ppaf24->sample_count)
447 { psf->error = SFE_BAD_SEEK ;
448 return PSF_SEEK_ERROR ;
449 } ;
450
451 if (psf->last_op == SFM_WRITE && ppaf24->write_count)
452 paf24_write_block (psf, ppaf24) ;
453
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 ;
458 break ;
459
460 default :
461 psf->error = SFE_BAD_SEEK ;
462 return PSF_SEEK_ERROR ;
463 } ;
464
465 return newblock * ppaf24->samplesperblock + newsample ;
466 } /* paf24_seek */
467
468 static int
469 paf24_close (SF_PRIVATE *psf)
470 { PAF24_PRIVATE *ppaf24 ;
471
472 if (psf->codec_data == NULL)
473 return 0 ;
474
475 ppaf24 = (PAF24_PRIVATE*) psf->codec_data ;
476
477 if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
478 { if (ppaf24->write_count > 0)
479 paf24_write_block (psf, ppaf24) ;
480 } ;
481
482 return 0 ;
483 } /* paf24_close */
484
485 /*---------------------------------------------------------------------------
486 */
487 static int
488 paf24_read_block (SF_PRIVATE *psf, PAF24_PRIVATE *ppaf24)
489 { int k, channel ;
490 unsigned char *cptr ;
491
492 ppaf24->read_block ++ ;
493 ppaf24->read_count = 0 ;
494
495 if (ppaf24->read_block * ppaf24->samplesperblock > ppaf24->sample_count)
496 { memset (ppaf24->samples, 0, ppaf24->samplesperblock * ppaf24->channels) ;
497 return 1 ;
498 } ;
499
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) ;
503
504
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) ;
509
510 /* Unpack block. */
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) ;
515 } ;
516 }
517 else
518 { /* Do endian swapping if necessary. */
519 if (psf->endian == SF_ENDIAN_BIG)
520 endswap_int_array (ppaf24->data, 8 * ppaf24->channels) ;
521
522 /* Unpack block. */
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) ;
527 } ;
528 } ;
529
530 return 1 ;
531 } /* paf24_read_block */
532
533 static int
534 paf24_read (SF_PRIVATE *psf, PAF24_PRIVATE *ppaf24, int *ptr, int len)
535 { int count, total = 0 ;
536
537 while (total < len)
538 { if (ppaf24->read_block * ppaf24->samplesperblock >= ppaf24->sample_count)
539 { memset (&(ptr [total]), 0, (len - total) * sizeof (int)) ;
540 return total ;
541 } ;
542
543 if (ppaf24->read_count >= ppaf24->samplesperblock)
544 paf24_read_block (psf, ppaf24) ;
545
546 count = (ppaf24->samplesperblock - ppaf24->read_count) * ppaf24->channels ;
547 count = (len - total > count) ? count : len - total ;
548
549 memcpy (&(ptr [total]), &(ppaf24->samples [ppaf24->read_count * ppaf24->channels]), count * sizeof (int)) ;
550 total += count ;
551 ppaf24->read_count += count / ppaf24->channels ;
552 } ;
553
554 return total ;
555 } /* paf24_read */
556
557 static sf_count_t
558 paf24_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
559 { PAF24_PRIVATE *ppaf24 ;
560 int *iptr ;
561 int k, bufferlen, readcount, count ;
562 sf_count_t total = 0 ;
563
564 if (psf->codec_data == NULL)
565 return 0 ;
566 ppaf24 = (PAF24_PRIVATE*) psf->codec_data ;
567
568 iptr = psf->u.ibuf ;
569 bufferlen = ARRAY_LEN (psf->u.ibuf) ;
570 while (len > 0)
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 ;
575 total += count ;
576 len -= readcount ;
577 } ;
578 return total ;
579 } /* paf24_read_s */
580
581 static sf_count_t
582 paf24_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
583 { PAF24_PRIVATE *ppaf24 ;
584 int total ;
585
586 if (psf->codec_data == NULL)
587 return 0 ;
588 ppaf24 = (PAF24_PRIVATE*) psf->codec_data ;
589
590 total = paf24_read (psf, ppaf24, ptr, len) ;
591
592 return total ;
593 } /* paf24_read_i */
594
595 static sf_count_t
596 paf24_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
597 { PAF24_PRIVATE *ppaf24 ;
598 int *iptr ;
599 int k, bufferlen, readcount, count ;
600 sf_count_t total = 0 ;
601 float normfact ;
602
603 if (psf->codec_data == NULL)
604 return 0 ;
605 ppaf24 = (PAF24_PRIVATE*) psf->codec_data ;
606
607 normfact = (psf->norm_float == SF_TRUE) ? (1.0 / 0x80000000) : (1.0 / 0x100) ;
608
609 iptr = psf->u.ibuf ;
610 bufferlen = ARRAY_LEN (psf->u.ibuf) ;
611 while (len > 0)
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] ;
616 total += count ;
617 len -= readcount ;
618 } ;
619 return total ;
620 } /* paf24_read_f */
621
622 static sf_count_t
623 paf24_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
624 { PAF24_PRIVATE *ppaf24 ;
625 int *iptr ;
626 int k, bufferlen, readcount, count ;
627 sf_count_t total = 0 ;
628 double normfact ;
629
630 if (psf->codec_data == NULL)
631 return 0 ;
632 ppaf24 = (PAF24_PRIVATE*) psf->codec_data ;
633
634 normfact = (psf->norm_double == SF_TRUE) ? (1.0 / 0x80000000) : (1.0 / 0x100) ;
635
636 iptr = psf->u.ibuf ;
637 bufferlen = ARRAY_LEN (psf->u.ibuf) ;
638 while (len > 0)
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] ;
643 total += count ;
644 len -= readcount ;
645 } ;
646 return total ;
647 } /* paf24_read_d */
648
649 /*---------------------------------------------------------------------------
650 */
651
652 static int
653 paf24_write_block (SF_PRIVATE *psf, PAF24_PRIVATE *ppaf24)
654 { int k, nextsample, channel ;
655 unsigned char *cptr ;
656
657 /* First pack block. */
658
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 ;
667 } ;
668
669 /* Do endian swapping if necessary. */
670 if (psf->endian == SF_ENDIAN_BIG)
671 endswap_int_array (ppaf24->data, 8 * ppaf24->channels) ;
672 }
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 ;
682 } ;
683 if (psf->endian == SF_ENDIAN_BIG)
684 endswap_int_array (ppaf24->data, 8 * ppaf24->channels) ;
685 } ;
686
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) ;
690
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 ;
693
694 if (ppaf24->write_count == ppaf24->samplesperblock)
695 { ppaf24->write_block ++ ;
696 ppaf24->write_count = 0 ;
697 } ;
698
699 return 1 ;
700 } /* paf24_write_block */
701
702 static int
703 paf24_write (SF_PRIVATE *psf, PAF24_PRIVATE *ppaf24, const int *ptr, int len)
704 { int count, total = 0 ;
705
706 while (total < len)
707 { count = (ppaf24->samplesperblock - ppaf24->write_count) * ppaf24->channels ;
708
709 if (count > len - total)
710 count = len - total ;
711
712 memcpy (&(ppaf24->samples [ppaf24->write_count * ppaf24->channels]), &(ptr [total]), count * sizeof (int)) ;
713 total += count ;
714 ppaf24->write_count += count / ppaf24->channels ;
715
716 if (ppaf24->write_count >= ppaf24->samplesperblock)
717 paf24_write_block (psf, ppaf24) ;
718 } ;
719
720 return total ;
721 } /* paf24_write */
722
723 static sf_count_t
724 paf24_write_s (SF_PRIVATE *psf, const short *ptr, sf_count_t len)
725 { PAF24_PRIVATE *ppaf24 ;
726 int *iptr ;
727 int k, bufferlen, writecount = 0, count ;
728 sf_count_t total = 0 ;
729
730 if (psf->codec_data == NULL)
731 return 0 ;
732 ppaf24 = (PAF24_PRIVATE*) psf->codec_data ;
733
734 iptr = psf->u.ibuf ;
735 bufferlen = ARRAY_LEN (psf->u.ibuf) ;
736 while (len > 0)
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) ;
741 total += count ;
742 len -= writecount ;
743 if (count != writecount)
744 break ;
745 } ;
746 return total ;
747 } /* paf24_write_s */
748
749 static sf_count_t
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 ;
754
755 if (psf->codec_data == NULL)
756 return 0 ;
757 ppaf24 = (PAF24_PRIVATE*) psf->codec_data ;
758
759 while (len > 0)
760 { writecount = (len > 0x10000000) ? 0x10000000 : (int) len ;
761
762 count = paf24_write (psf, ppaf24, ptr, writecount) ;
763
764 total += count ;
765 len -= count ;
766 if (count != writecount)
767 break ;
768 } ;
769
770 return total ;
771 } /* paf24_write_i */
772
773 static sf_count_t
774 paf24_write_f (SF_PRIVATE *psf, const float *ptr, sf_count_t len)
775 { PAF24_PRIVATE *ppaf24 ;
776 int *iptr ;
777 int k, bufferlen, writecount = 0, count ;
778 sf_count_t total = 0 ;
779 float normfact ;
780
781 if (psf->codec_data == NULL)
782 return 0 ;
783 ppaf24 = (PAF24_PRIVATE*) psf->codec_data ;
784
785 normfact = (psf->norm_float == SF_TRUE) ? (1.0 * 0x7FFFFFFF) : (1.0 / 0x100) ;
786
787 iptr = psf->u.ibuf ;
788 bufferlen = ARRAY_LEN (psf->u.ibuf) ;
789 while (len > 0)
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) ;
794 total += count ;
795 len -= writecount ;
796 if (count != writecount)
797 break ;
798 } ;
799
800 return total ;
801 } /* paf24_write_f */
802
803 static sf_count_t
804 paf24_write_d (SF_PRIVATE *psf, const double *ptr, sf_count_t len)
805 { PAF24_PRIVATE *ppaf24 ;
806 int *iptr ;
807 int k, bufferlen, writecount = 0, count ;
808 sf_count_t total = 0 ;
809 double normfact ;
810
811 if (psf->codec_data == NULL)
812 return 0 ;
813 ppaf24 = (PAF24_PRIVATE*) psf->codec_data ;
814
815 normfact = (psf->norm_double == SF_TRUE) ? (1.0 * 0x7FFFFFFF) : (1.0 / 0x100) ;
816
817 iptr = psf->u.ibuf ;
818 bufferlen = ARRAY_LEN (psf->u.ibuf) ;
819 while (len > 0)
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) ;
824 total += count ;
825 len -= writecount ;
826 if (count != writecount)
827 break ;
828 } ;
829
830 return total ;
831 } /* paf24_write_d */
832