*** empty log message ***
[chuck-blob.git] / v2 / util_sndfile.c
blobde7787ba9973e5a36786e94247e626a87b9f596c
1 /*----------------------------------------------------------------------------
2 ChucK Concurrent, On-the-fly Audio Programming Language
3 Compiler and Virtual Machine
5 Copyright (c) 2004 Ge Wang and Perry R. Cook. All rights reserved.
6 http://chuck.cs.princeton.edu/
7 http://soundlab.cs.princeton.edu/
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 U.S.A.
23 -----------------------------------------------------------------------------*/
26 ** libsndfile Copyright (C) 1999-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
28 ** This program is free software; you can redistribute it and/or modify
29 ** it under the terms of the GNU Lesser General Public License as published by
30 ** the Free Software Foundation; either version 2.1 of the License, or
31 ** (at your option) any later version.
33 ** This program is distributed in the hope that it will be useful,
34 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
35 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36 ** GNU Lesser General Public License for more details.
38 ** You should have received a copy of the GNU Lesser General Public License
39 ** along with this program; if not, write to the Free Software
40 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
43 //-----------------------------------------------------------------------------
44 // name: util_sndfile.c
45 // desc: libsndfile for ChucK
47 // authors: Ge Wang (gewang@cs.princeton.edu)
48 // Perry R. Cook (prc@cs.princeton.edu)
49 // Ari Lazier (alazier@alumni.princeton.edu)
50 // libsndfile: Erik de Castro Lopo (erikd@mega-nerd.com)
51 //-----------------------------------------------------------------------------
52 #include "util_sndfile.h"
53 #include "chuck_def.h"
54 #include <math.h>
57 * Copyright 1992 by Jutta Degener and Carsten Bormann, Technische
58 * Universitaet Berlin. See the accompanying file "COPYRIGHT" for
59 * details. THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE.
63 * See private.h for the more commonly used macro versions.
66 #include <stdio.h>
67 #include <assert.h>
70 #define saturate(x) \
71 ((x) < MIN_WORD ? MIN_WORD : (x) > MAX_WORD ? MAX_WORD: (x))
73 word gsm_add ( word a, word b)
75 longword sum = (longword)a + (longword)b;
76 return saturate(sum);
79 word gsm_sub ( word a, word b)
81 longword diff = (longword)a - (longword)b;
82 return saturate(diff);
85 word gsm_mult ( word a, word b)
87 if (a == MIN_WORD && b == MIN_WORD)
88 return MAX_WORD;
90 return SASR_L( (longword)a * (longword)b, 15 );
93 word gsm_mult_r ( word a, word b)
95 if (b == MIN_WORD && a == MIN_WORD) return MAX_WORD;
96 else {
97 longword prod = (longword)a * (longword)b + 16384;
98 prod >>= 15;
99 return prod & 0xFFFF;
103 word gsm_abs (word a)
105 return a < 0 ? (a == MIN_WORD ? MAX_WORD : -a) : a;
108 longword gsm_L_mult (word a, word b)
110 assert( a != MIN_WORD || b != MIN_WORD );
111 return ((longword)a * (longword)b) << 1;
114 longword gsm_L_add ( longword a, longword b)
116 if (a < 0) {
117 if (b >= 0) return a + b;
118 else {
119 ulongword A = (ulongword)-(a + 1) + (ulongword)-(b + 1);
120 return A >= MAX_LONGWORD ? MIN_LONGWORD :-(longword)A-2;
123 else if (b <= 0) return a + b;
124 else {
125 ulongword A = (ulongword)a + (ulongword)b;
126 return A > MAX_LONGWORD ? MAX_LONGWORD : A;
130 longword gsm_L_sub ( longword a, longword b)
132 if (a >= 0) {
133 if (b >= 0) return a - b;
134 else {
135 /* a>=0, b<0 */
137 ulongword A = (ulongword)a + -(b + 1);
138 return A >= MAX_LONGWORD ? MAX_LONGWORD : (A + 1);
141 else if (b <= 0) return a - b;
142 else {
143 /* a<0, b>0 */
145 ulongword A = (ulongword)-(a + 1) + b;
146 return A >= MAX_LONGWORD ? MIN_LONGWORD : -(longword)A - 1;
150 static unsigned char const bitoff[ 256 ] = {
151 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
152 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
153 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
154 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
155 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
156 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
157 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
158 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
159 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
160 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
161 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
162 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
163 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
164 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
165 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
166 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
169 word gsm_norm (longword a )
171 * the number of left shifts needed to normalize the 32 bit
172 * variable L_var1 for positive values on the interval
174 * with minimum of
175 * minimum of 1073741824 (01000000000000000000000000000000) and
176 * maximum of 2147483647 (01111111111111111111111111111111)
179 * and for negative values on the interval with
180 * minimum of -2147483648 (-10000000000000000000000000000000) and
181 * maximum of -1073741824 ( -1000000000000000000000000000000).
183 * in order to normalize the result, the following
184 * operation must be done: L_norm_var1 = L_var1 << norm( L_var1 );
186 * (That's 'ffs', only from the left, not the right..)
189 assert(a != 0);
191 if (a < 0) {
192 if (a <= -1073741824) return 0;
193 a = ~a;
196 return a & 0xffff0000
197 ? ( a & 0xff000000
198 ? -1 + bitoff[ 0xFF & (a >> 24) ]
199 : 7 + bitoff[ 0xFF & (a >> 16) ] )
200 : ( a & 0xff00
201 ? 15 + bitoff[ 0xFF & (a >> 8) ]
202 : 23 + bitoff[ 0xFF & a ] );
205 longword gsm_L_asl (longword a, int n)
207 if (n >= 32) return 0;
208 if (n <= -32) return -(a < 0);
209 if (n < 0) return gsm_L_asr(a, -n);
210 return a << n;
213 word gsm_asr (word a, int n)
215 if (n >= 16) return -(a < 0);
216 if (n <= -16) return 0;
217 if (n < 0) return a << -n;
219 return SASR_W (a, (word) n);
222 word gsm_asl (word a, int n)
224 if (n >= 16) return 0;
225 if (n <= -16) return -(a < 0);
226 if (n < 0) return gsm_asr(a, -n);
227 return a << n;
230 longword gsm_L_asr (longword a, int n)
232 if (n >= 32) return -(a < 0);
233 if (n <= -32) return 0;
234 if (n < 0) return a << -n;
236 return SASR_L (a, (word) n);
240 ** word gsm_asr (word a, int n)
241 ** {
242 ** if (n >= 16) return -(a < 0);
243 ** if (n <= -16) return 0;
244 ** if (n < 0) return a << -n;
246 ** # ifdef SASR_W
247 ** return a >> n;
248 ** # else
249 ** if (a >= 0) return a >> n;
250 ** else return -(word)( -(uword)a >> n );
251 ** # endif
252 ** }
256 * (From p. 46, end of section 4.2.5)
258 * NOTE: The following lines gives [sic] one correct implementation
259 * of the div(num, denum) arithmetic operation. Compute div
260 * which is the integer division of num by denum: with denum
261 * >= num > 0
264 word gsm_div (word num, word denum)
266 longword L_num = num;
267 longword L_denum = denum;
268 word div = 0;
269 int k = 15;
271 /* The parameter num sometimes becomes zero.
272 * Although this is explicitly guarded against in 4.2.5,
273 * we assume that the result should then be zero as well.
276 /* assert(num != 0); */
278 assert(num >= 0 && denum >= num);
279 if (num == 0)
280 return 0;
282 while (k--) {
283 div <<= 1;
284 L_num <<= 1;
286 if (L_num >= L_denum) {
287 L_num -= L_denum;
288 div++;
292 return div;
295 ** Do not edit or modify anything in this comment block.
296 ** The arch-tag line is a file identity tag for the GNU Arch
297 ** revision control system.
299 ** arch-tag: a7398579-e2e1-4733-aa2d-4c6efc0c58ff
303 ** Copyright (C) 1999-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
305 ** This program is free software; you can redistribute it and/or modify
306 ** it under the terms of the GNU Lesser General Public License as published by
307 ** the Free Software Foundation; either version 2.1 of the License, or
308 ** (at your option) any later version.
310 ** This program is distributed in the hope that it will be useful,
311 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
312 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
313 ** GNU Lesser General Public License for more details.
315 ** You should have received a copy of the GNU Lesser General Public License
316 ** along with this program; if not, write to the Free Software
317 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
321 #include <stdio.h>
322 #include <stdlib.h>
323 #include <string.h>
324 #include <time.h>
325 #include <ctype.h>
328 /*------------------------------------------------------------------------------
329 * Macros to handle big/little endian issues.
332 #define FORM_MARKER (MAKE_MARKER ('F', 'O', 'R', 'M'))
333 #define AIFF_MARKER (MAKE_MARKER ('A', 'I', 'F', 'F'))
334 #define AIFC_MARKER (MAKE_MARKER ('A', 'I', 'F', 'C'))
335 #define COMM_MARKER (MAKE_MARKER ('C', 'O', 'M', 'M'))
336 #define SSND_MARKER (MAKE_MARKER ('S', 'S', 'N', 'D'))
337 #define MARK_MARKER (MAKE_MARKER ('M', 'A', 'R', 'K'))
338 #define INST_MARKER (MAKE_MARKER ('I', 'N', 'S', 'T'))
339 #define APPL_MARKER (MAKE_MARKER ('A', 'P', 'P', 'L'))
341 #define c_MARKER (MAKE_MARKER ('(', 'c', ')', ' '))
342 #define NAME_MARKER (MAKE_MARKER ('N', 'A', 'M', 'E'))
343 #define AUTH_MARKER (MAKE_MARKER ('A', 'U', 'T', 'H'))
344 #define ANNO_MARKER (MAKE_MARKER ('A', 'N', 'N', 'O'))
345 #define COMT_MARKER (MAKE_MARKER ('C', 'O', 'M', 'T'))
346 #define FVER_MARKER (MAKE_MARKER ('F', 'V', 'E', 'R'))
347 #define SFX_MARKER (MAKE_MARKER ('S', 'F', 'X', '!'))
349 #define PEAK_MARKER (MAKE_MARKER ('P', 'E', 'A', 'K'))
351 /* Supported AIFC encodings.*/
352 #define NONE_MARKER (MAKE_MARKER ('N', 'O', 'N', 'E'))
353 #define sowt_MARKER (MAKE_MARKER ('s', 'o', 'w', 't'))
354 #define twos_MARKER (MAKE_MARKER ('t', 'w', 'o', 's'))
355 #define raw_MARKER (MAKE_MARKER ('r', 'a', 'w', ' '))
356 #define in32_MARKER (MAKE_MARKER ('i', 'n', '3', '2'))
357 #define ni32_MARKER (MAKE_MARKER ('2', '3', 'n', 'i'))
359 #define fl32_MARKER (MAKE_MARKER ('f', 'l', '3', '2'))
360 #define FL32_MARKER (MAKE_MARKER ('F', 'L', '3', '2'))
361 #define fl64_MARKER (MAKE_MARKER ('f', 'l', '6', '4'))
362 #define FL64_MARKER (MAKE_MARKER ('F', 'L', '6', '4'))
364 #define ulaw_MARKER (MAKE_MARKER ('u', 'l', 'a', 'w'))
365 #define ULAW_MARKER (MAKE_MARKER ('U', 'L', 'A', 'W'))
366 #define alaw_MARKER (MAKE_MARKER ('a', 'l', 'a', 'w'))
367 #define ALAW_MARKER (MAKE_MARKER ('A', 'L', 'A', 'W'))
369 #define DWVW_MARKER (MAKE_MARKER ('D', 'W', 'V', 'W'))
370 #define GSM_MARKER (MAKE_MARKER ('G', 'S', 'M', ' '))
371 #define ima4_MARKER (MAKE_MARKER ('i', 'm', 'a', '4'))
373 /* Unsupported AIFC encodings.*/
375 #define MAC3_MARKER (MAKE_MARKER ('M', 'A', 'C', '3'))
376 #define MAC6_MARKER (MAKE_MARKER ('M', 'A', 'C', '6'))
377 #define ADP4_MARKER (MAKE_MARKER ('A', 'D', 'P', '4'))
379 /* Predfined chunk sizes. */
380 #define SIZEOF_AIFF_COMM 18
381 #define SIZEOF_AIFC_COMM_MIN 22
382 #define SIZEOF_AIFC_COMM 24
383 #define SIZEOF_SSND_CHUNK 8
384 #define SIZEOF_INST_CHUNK 20
386 /* AIFC/IMA4 defines. */
387 #define AIFC_IMA4_BLOCK_LEN 34
388 #define AIFC_IMA4_SAMPLES_PER_BLOCK 64
390 /*------------------------------------------------------------------------------
391 * Typedefs for file chunks.
394 enum
395 { aiffHAVE_FORM = 0x01,
396 HAVE_AIFF = 0x02,
397 HAVE_COMM = 0x04,
398 HAVE_SSND = 0x08
401 typedef struct
402 { unsigned int size ;
403 short numChannels ;
404 unsigned int numSampleFrames ;
405 short sampleSize ;
406 unsigned char sampleRate [10] ;
407 unsigned int encoding ;
408 char zero_bytes [2] ;
409 } COMM_CHUNK ;
411 typedef struct
412 { unsigned int offset ;
413 unsigned int blocksize ;
414 } SSND_CHUNK ;
416 typedef struct
417 { short playMode ;
418 unsigned short beginLoop ;
419 unsigned short endLoop ;
420 } INST_LOOP ;
422 typedef struct
423 { char baseNote ; /* all notes are MIDI note numbers */
424 char detune ; /* cents off, only -50 to +50 are significant */
425 char lowNote ;
426 char highNote ;
427 char lowVelocity ; /* 1 to 127 */
428 char highVelocity ; /* 1 to 127 */
429 short gain ; /* in dB, 0 is normal */
430 INST_LOOP sustain_loop ;
431 INST_LOOP release_loop ;
432 } INST_CHUNK ;
434 /*------------------------------------------------------------------------------
435 * Private static functions.
438 static int aiff_close (SF_PRIVATE *psf) ;
440 static int tenbytefloat2int (unsigned char *bytes) ;
441 static void uint2tenbytefloat (unsigned int num, unsigned char *bytes) ;
443 static int aiff_read_comm_chunk (SF_PRIVATE *psf, COMM_CHUNK *comm_fmt) ;
445 static int aiff_read_header (SF_PRIVATE *psf, COMM_CHUNK *comm_fmt) ;
447 static int aiff_write_header (SF_PRIVATE *psf, int calc_length) ;
448 static int aiff_write_tailer (SF_PRIVATE *psf) ;
449 static void aiff_write_strings (SF_PRIVATE *psf, int location) ;
451 static int aiff_command (SF_PRIVATE *psf, int command, void *data, int datasize) ;
453 static const char *get_loop_mode_str (short mode) ;
456 /*------------------------------------------------------------------------------
457 ** Public function.
461 aiff_open (SF_PRIVATE *psf)
462 { COMM_CHUNK comm_fmt ;
463 int error, subformat ;
465 subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
467 if (psf->mode == SFM_READ || (psf->mode == SFM_RDWR && psf->filelength > 0))
468 { if ((error = aiff_read_header (psf, &comm_fmt)))
469 return error ;
471 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
474 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
475 { if (psf->is_pipe)
476 return SFE_NO_PIPE_WRITE ;
478 if ((psf->sf.format & SF_FORMAT_TYPEMASK) != SF_FORMAT_AIFF)
479 return SFE_BAD_OPEN_FORMAT ;
481 if (psf->mode == SFM_WRITE && (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE))
482 { psf->pchunk = calloc (1, sizeof (PEAK_CHUNK) * psf->sf.channels * sizeof (PEAK_POS)) ;
483 if (psf->pchunk == NULL)
484 return SFE_MALLOC_FAILED ;
485 psf->has_peak = SF_TRUE ;
486 psf->peak_loc = SF_PEAK_START ;
489 if (psf->mode != SFM_RDWR || psf->filelength < 40)
490 { psf->filelength = 0 ;
491 psf->datalength = 0 ;
492 psf->dataoffset = 0 ;
493 psf->sf.frames = 0 ;
496 psf->str_flags = SF_STR_ALLOW_START | SF_STR_ALLOW_END ;
498 if ((error = aiff_write_header (psf, SF_FALSE)))
499 return error ;
501 psf->write_header = aiff_write_header ;
504 psf->close = aiff_close ;
505 psf->command = aiff_command ;
507 psf->blockwidth = psf->sf.channels * psf->bytewidth ;
509 switch (psf->sf.format & SF_FORMAT_SUBMASK)
510 { case SF_FORMAT_PCM_U8 :
511 error = pcm_init (psf) ;
512 break ;
514 case SF_FORMAT_PCM_S8 :
515 error = pcm_init (psf) ;
516 break ;
518 case SF_FORMAT_PCM_16 :
519 case SF_FORMAT_PCM_24 :
520 case SF_FORMAT_PCM_32 :
521 error = pcm_init (psf) ;
522 break ;
524 case SF_FORMAT_ULAW :
525 error = ulaw_init (psf) ;
526 break ;
528 case SF_FORMAT_ALAW :
529 error = alaw_init (psf) ;
530 break ;
532 /* Lite remove start */
533 case SF_FORMAT_FLOAT :
534 error = float32_init (psf) ;
535 break ;
537 case SF_FORMAT_DOUBLE :
538 error = double64_init (psf) ;
539 break ;
541 case SF_FORMAT_DWVW_12 :
542 error = dwvw_init (psf, 12) ;
543 break ;
545 case SF_FORMAT_DWVW_16 :
546 error = dwvw_init (psf, 16) ;
547 break ;
549 case SF_FORMAT_DWVW_24 :
550 error = dwvw_init (psf, 24) ;
551 break ;
553 case SF_FORMAT_DWVW_N :
554 if (psf->mode != SFM_READ)
555 { error = SFE_DWVW_BAD_BITWIDTH ;
556 break ;
558 if (comm_fmt.sampleSize >= 8 && comm_fmt.sampleSize < 24)
559 { error = dwvw_init (psf, comm_fmt.sampleSize) ;
560 psf->sf.frames = comm_fmt.numSampleFrames ;
561 break ;
563 psf_log_printf (psf, "AIFC/DWVW : Bad bitwidth %d\n", comm_fmt.sampleSize) ;
564 error = SFE_DWVW_BAD_BITWIDTH ;
565 break ;
567 case SF_FORMAT_IMA_ADPCM :
569 ** IMA ADPCM encoded AIFF files always have a block length
570 ** of 34 which decodes to 64 samples.
572 error = aiff_ima_init (psf, AIFC_IMA4_BLOCK_LEN, AIFC_IMA4_SAMPLES_PER_BLOCK) ;
573 break ;
574 /* Lite remove end */
576 case SF_FORMAT_GSM610 :
577 error = gsm610_init (psf) ;
578 break ;
580 default : return SFE_UNIMPLEMENTED ;
583 if (psf->mode == SFM_READ)
584 psf->blockwidth = psf->sf.channels * psf->bytewidth ;
586 return error ;
587 } /* aiff_open */
589 /*==========================================================================================
590 ** Private functions.
593 static int
594 aiff_read_header (SF_PRIVATE *psf, COMM_CHUNK *comm_fmt)
595 { SSND_CHUNK ssnd_fmt ;
596 int marker, dword, bytesread, k ;
597 int FORMsize, SSNDsize ;
598 int filetype, found_chunk = 0, done = 0, error = 0 ;
599 char *cptr, byte ;
601 /* Set position to start of file to begin reading header. */
602 psf_binheader_readf (psf, "p", 0) ;
604 memset (comm_fmt, 0, sizeof (COMM_CHUNK)) ;
606 /* Until recently AIF* file were all BIG endian. */
607 psf->endian = SF_ENDIAN_BIG ;
609 /* AIFF files can apparently have their chunks in any order. However, they
610 ** must have a FORM chunk. Approach here is to read all the chunks one by
611 ** one and then check for the mandatory chunks at the end.
613 while (! done)
614 { psf_binheader_readf (psf, "m", &marker) ;
616 if (psf->mode == SFM_RDWR && (found_chunk & HAVE_SSND))
617 return SFE_AIFF_RW_SSND_NOT_LAST ;
619 switch (marker)
620 { case FORM_MARKER :
621 if (found_chunk)
622 return SFE_AIFF_NO_FORM ;
624 psf_binheader_readf (psf, "E4", &FORMsize) ;
626 if (psf->fileoffset > 0 && psf->filelength > FORMsize + 8)
627 { /* Set file length. */
628 psf->filelength = FORMsize + 8 ;
629 psf_log_printf (psf, "FORM : %u\n", FORMsize) ;
631 else if (FORMsize != psf->filelength - 2 * SIGNED_SIZEOF (dword))
632 { dword = psf->filelength - 2 * sizeof (dword) ;
633 psf_log_printf (psf, "FORM : %u (should be %u)\n", FORMsize, dword) ;
634 FORMsize = dword ;
636 else
637 psf_log_printf (psf, "FORM : %u\n", FORMsize) ;
638 found_chunk |= aiffHAVE_FORM ;
639 break ;
641 case AIFC_MARKER :
642 case AIFF_MARKER :
643 if (! (found_chunk & aiffHAVE_FORM))
644 return SFE_AIFF_AIFF_NO_FORM ;
645 filetype = marker ;
646 psf_log_printf (psf, " %M\n", marker) ;
647 found_chunk |= HAVE_AIFF ;
648 break ;
650 case COMM_MARKER :
651 error = aiff_read_comm_chunk (psf, comm_fmt) ;
653 psf->sf.samplerate = tenbytefloat2int (comm_fmt->sampleRate) ;
654 psf->sf.frames = comm_fmt->numSampleFrames ;
655 psf->sf.channels = comm_fmt->numChannels ;
656 psf->bytewidth = BITWIDTH2BYTES (comm_fmt->sampleSize) ;
658 if (error)
659 return error ;
661 found_chunk |= HAVE_COMM ;
662 break ;
664 case PEAK_MARKER :
665 /* Must have COMM chunk before PEAK chunk. */
666 if ((found_chunk & (aiffHAVE_FORM | HAVE_AIFF | HAVE_COMM)) != (aiffHAVE_FORM | HAVE_AIFF | HAVE_COMM))
667 return SFE_AIFF_PEAK_B4_COMM ;
669 psf_binheader_readf (psf, "E4", &dword) ;
671 psf_log_printf (psf, "%M : %d\n", marker, dword) ;
672 if (dword != SIGNED_SIZEOF (PEAK_CHUNK) + psf->sf.channels * SIGNED_SIZEOF (PEAK_POS))
673 { psf_binheader_readf (psf, "j", dword) ;
674 psf_log_printf (psf, "*** File PEAK chunk bigger than sizeof (PEAK_CHUNK).\n") ;
675 return SFE_WAV_BAD_PEAK ;
678 psf->pchunk = calloc (1, sizeof (PEAK_CHUNK) * psf->sf.channels * sizeof (PEAK_POS)) ;
679 if (psf->pchunk == NULL)
680 return SFE_MALLOC_FAILED ;
682 /* read in rest of PEAK chunk. */
683 psf_binheader_readf (psf, "E44", &(psf->pchunk->version), &(psf->pchunk->timestamp)) ;
685 if (psf->pchunk->version != 1)
686 psf_log_printf (psf, " version : %d *** (should be version 1)\n", psf->pchunk->version) ;
687 else
688 psf_log_printf (psf, " version : %d\n", psf->pchunk->version) ;
690 psf_log_printf (psf, " time stamp : %d\n", psf->pchunk->timestamp) ;
691 psf_log_printf (psf, " Ch Position Value\n") ;
693 cptr = (char *) psf->buffer ;
694 for (dword = 0 ; dword < psf->sf.channels ; dword++)
695 { psf_binheader_readf (psf, "Ef4", &(psf->pchunk->peaks [dword].value),
696 &(psf->pchunk->peaks [dword].position)) ;
698 LSF_SNPRINTF (cptr, sizeof (psf->buffer), " %2d %-12d %g\n",
699 dword, psf->pchunk->peaks [dword].position, psf->pchunk->peaks [dword].value) ;
700 cptr [sizeof (psf->buffer) - 1] = 0 ;
701 psf_log_printf (psf, cptr) ;
704 psf->has_peak = SF_TRUE ; /* Found PEAK chunk. */
705 break ;
707 case SSND_MARKER :
708 psf_binheader_readf (psf, "E444", &SSNDsize, &(ssnd_fmt.offset), &(ssnd_fmt.blocksize)) ;
710 psf->datalength = SSNDsize - sizeof (ssnd_fmt) ;
711 psf->dataoffset = psf_ftell (psf) ;
713 if (psf->datalength > psf->filelength - psf->dataoffset || psf->datalength < 0)
714 { psf_log_printf (psf, " SSND : %u (should be %D)\n", SSNDsize, psf->filelength - psf->dataoffset + sizeof (SSND_CHUNK)) ;
715 psf->datalength = psf->filelength - psf->dataoffset ;
717 else
718 psf_log_printf (psf, " SSND : %u\n", SSNDsize) ;
720 /* Only set dataend if there really is data at the end. */
721 if (psf->datalength + psf->dataoffset < psf->filelength)
722 psf->dataend = psf->datalength + psf->dataoffset ;
724 psf_log_printf (psf, " Offset : %u\n", ssnd_fmt.offset) ;
725 psf_log_printf (psf, " Block Size : %u\n", ssnd_fmt.blocksize) ;
727 found_chunk |= HAVE_SSND ;
729 if (! psf->sf.seekable)
730 break ;
732 /* Seek to end of SSND chunk. */
733 psf_fseek (psf, psf->dataoffset + psf->datalength + (SSNDsize & 1), SEEK_SET) ;
734 break ;
736 case c_MARKER :
737 psf_binheader_readf (psf, "E4", &dword) ;
738 dword += (dword & 1) ;
739 if (dword == 0)
740 break ;
741 if (dword > SIGNED_SIZEOF (psf->buffer))
742 { psf_log_printf (psf, " %M : %d (too big)\n", marker, dword) ;
743 return SFE_INTERNAL ;
746 cptr = (char*) psf->buffer ;
747 psf_binheader_readf (psf, "b", cptr, dword) ;
748 cptr [dword - 1] = 0 ;
749 psf_log_printf (psf, " %M : %s\n", marker, cptr) ;
750 psf_store_string (psf, SF_STR_COPYRIGHT, cptr) ;
751 break ;
753 case AUTH_MARKER :
754 psf_binheader_readf (psf, "E4", &dword) ;
755 dword += (dword & 1) ;
756 if (dword == 0)
757 break ;
758 if (dword > SIGNED_SIZEOF (psf->buffer))
759 { psf_log_printf (psf, " %M : %d (too big)\n", marker, dword) ;
760 return SFE_INTERNAL ;
763 cptr = (char*) psf->buffer ;
764 psf_binheader_readf (psf, "b", cptr, dword) ;
765 cptr [dword - 1] = 0 ;
766 psf_log_printf (psf, " %M : %s\n", marker, cptr) ;
767 psf_store_string (psf, SF_STR_ARTIST, cptr) ;
768 break ;
770 case COMT_MARKER :
771 psf_binheader_readf (psf, "E4", &dword) ;
772 dword += (dword & 1) ;
773 if (dword == 0)
774 break ;
775 if (dword > SIGNED_SIZEOF (psf->buffer))
776 { psf_log_printf (psf, " %M : %d (too big)\n", marker, dword) ;
777 return SFE_INTERNAL ;
780 cptr = (char*) psf->buffer ;
781 psf_binheader_readf (psf, "b", cptr, dword) ;
782 cptr [dword - 1] = 0 ;
783 psf_log_printf (psf, " %M : %s\n", marker, cptr) ;
784 psf_store_string (psf, SF_STR_COMMENT, cptr) ;
785 break ;
787 case APPL_MARKER :
788 psf_binheader_readf (psf, "E4", &dword) ;
789 dword += (dword & 1) ;
790 if (dword == 0)
791 break ;
792 if (dword >= SIGNED_SIZEOF (psf->buffer))
793 { psf_log_printf (psf, " %M : %d (too big, skipping)\n", marker, dword) ;
794 psf_binheader_readf (psf, "j", dword) ;
795 break ;
798 cptr = (char*) psf->buffer ;
799 psf_binheader_readf (psf, "b", cptr, dword) ;
800 cptr [dword - 1] = 0 ;
802 for (k = 0 ; k < dword ; k++)
803 if (! isprint (cptr [k]))
804 { cptr [k] = 0 ;
805 break ;
808 psf_log_printf (psf, " %M : %s\n", marker, cptr) ;
809 psf_store_string (psf, SF_STR_SOFTWARE, cptr) ;
810 break ;
812 case NAME_MARKER :
813 psf_binheader_readf (psf, "E4", &dword) ;
814 dword += (dword & 1) ;
815 if (dword == 0)
816 break ;
817 if (dword > SIGNED_SIZEOF (psf->buffer))
818 { psf_log_printf (psf, " %M : %d (too big)\n", marker, dword) ;
819 return SFE_INTERNAL ;
822 cptr = (char*) psf->buffer ;
823 psf_binheader_readf (psf, "b", cptr, dword) ;
824 cptr [dword - 1] = 0 ;
825 psf_log_printf (psf, " %M : %s\n", marker, cptr) ;
826 psf_store_string (psf, SF_STR_TITLE, cptr) ;
827 break ;
829 case ANNO_MARKER :
830 psf_binheader_readf (psf, "E4", &dword) ;
831 dword += (dword & 1) ;
832 if (dword == 0)
833 break ;
834 if (dword > SIGNED_SIZEOF (psf->buffer))
835 { psf_log_printf (psf, " %M : %d (too big)\n", marker, dword) ;
836 return SFE_INTERNAL ;
839 cptr = (char*) psf->buffer ;
840 psf_binheader_readf (psf, "b", cptr, dword) ;
841 cptr [dword - 1] = 0 ;
842 psf_log_printf (psf, " %M : %s\n", marker, cptr) ;
843 break ;
845 case INST_MARKER :
846 psf_binheader_readf (psf, "E4", &dword) ;
847 if (dword != SIZEOF_INST_CHUNK)
848 { psf_log_printf (psf, " %M : %d (should be %d)\n", marker, dword, SIZEOF_INST_CHUNK) ;
849 psf_binheader_readf (psf, "j", dword) ;
850 break ;
852 psf_log_printf (psf, " %M : %d\n", marker, dword) ;
853 { unsigned char bytes [6] ;
854 short gain ;
856 psf_binheader_readf (psf, "b", bytes, 6) ;
857 psf_log_printf (psf, " Base Note : %u\n Detune : %u\n"
858 " Low Note : %u\n High Note : %u\n"
859 " Low Vel. : %u\n High Vel. : %u\n",
860 bytes [0], bytes [1], bytes [2], bytes [3], bytes [4], bytes [5]) ;
862 psf_binheader_readf (psf, "E2", &gain) ;
863 psf_log_printf (psf, " Gain (dB) : %d\n", gain) ;
866 { short mode ; /* 0 - no loop, 1 - forward looping, 2 - backward looping */
867 const char *loop_mode ;
868 unsigned short begin, end ;
870 psf_binheader_readf (psf, "E222", &mode, &begin, &end) ;
871 loop_mode = get_loop_mode_str (mode) ;
872 psf_log_printf (psf, " Sustain\n mode : %d => %s\n begin : %u\n end : %u\n",
873 mode, loop_mode, begin, end) ;
874 psf_binheader_readf (psf, "E222", &mode, &begin, &end) ;
875 loop_mode = get_loop_mode_str (mode) ;
876 psf_log_printf (psf, " Release\n mode : %d => %s\n begin : %u\n end : %u\n",
877 mode, loop_mode, begin, end) ;
879 break ;
881 case MARK_MARKER :
882 psf_binheader_readf (psf, "E4", &dword) ;
883 psf_log_printf (psf, " %M : %d\n", marker, dword) ;
884 { unsigned short mark_count, mark_id ;
885 unsigned char pstr_len ;
886 unsigned int position ;
888 bytesread = psf_binheader_readf (psf, "E2", &mark_count) ;
889 psf_log_printf (psf, " Count : %d\n", mark_count) ;
891 while (mark_count && bytesread < dword)
892 { bytesread += psf_binheader_readf (psf, "E241", &mark_id, &position, &pstr_len) ;
893 psf_log_printf (psf, " Mark ID : %u\n Position : %u\n", mark_id, position) ;
895 pstr_len += (pstr_len & 1) + 1 ; /* fudgy, fudgy, hack, hack */
897 bytesread += psf_binheader_readf (psf, "b", psf->buffer, pstr_len) ;
898 psf_log_printf (psf, " Name : %s\n", psf->buffer) ;
900 mark_count -- ;
903 psf_binheader_readf (psf, "j", dword - bytesread) ;
904 break ;
906 case FVER_MARKER :
907 case SFX_MARKER :
908 psf_binheader_readf (psf, "E4", &dword) ;
909 psf_log_printf (psf, " %M : %d\n", marker, dword) ;
911 psf_binheader_readf (psf, "j", dword) ;
912 break ;
914 case NONE_MARKER :
915 /* Fix for broken AIFC files with incorrect COMM chunk length. */
916 psf_binheader_readf (psf, "1", &byte) ;
917 dword = byte ;
918 psf_binheader_readf (psf, "j", dword) ;
919 break ;
921 default :
922 if (isprint ((marker >> 24) & 0xFF) && isprint ((marker >> 16) & 0xFF)
923 && isprint ((marker >> 8) & 0xFF) && isprint (marker & 0xFF))
924 { psf_binheader_readf (psf, "E4", &dword) ;
925 psf_log_printf (psf, "%M : %d (unknown marker)\n", marker, dword) ;
927 psf_binheader_readf (psf, "j", dword) ;
928 break ;
930 if ((dword = psf_ftell (psf)) & 0x03)
931 { psf_log_printf (psf, " Unknown chunk marker %X at position %d. Resyncing.\n", marker, dword - 4) ;
933 psf_binheader_readf (psf, "j", -3) ;
934 break ;
936 psf_log_printf (psf, "*** Unknown chunk marker %X at position %D. Exiting parser.\n", marker, psf_ftell (psf)) ;
937 done = 1 ;
938 break ;
939 } ; /* switch (marker) */
941 if ((! psf->sf.seekable) && (found_chunk & HAVE_SSND))
942 break ;
944 if (psf_ftell (psf) >= psf->filelength - (2 * SIGNED_SIZEOF (dword)))
945 break ;
947 if (psf->logindex >= SIGNED_SIZEOF (psf->logbuffer) - 2)
948 return SFE_LOG_OVERRUN ;
949 } ; /* while (1) */
951 if (! (found_chunk & aiffHAVE_FORM))
952 return SFE_AIFF_NO_FORM ;
954 if (! (found_chunk & HAVE_AIFF))
955 return SFE_AIFF_COMM_NO_FORM ;
957 if (! (found_chunk & HAVE_COMM))
958 return SFE_AIFF_SSND_NO_COMM ;
960 if (! psf->dataoffset)
961 return SFE_AIFF_NO_DATA ;
963 return 0 ;
964 } /* aiff_read_header */
966 static int
967 aiff_close (SF_PRIVATE *psf)
969 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
970 { aiff_write_tailer (psf) ;
972 aiff_write_header (psf, SF_TRUE) ;
975 return 0 ;
976 } /* aiff_close */
978 static int
979 aiff_read_comm_chunk (SF_PRIVATE *psf, COMM_CHUNK *comm_fmt)
980 { int error = 0, bytesread, subformat ;
982 bytesread = psf_binheader_readf (psf, "E4", &(comm_fmt->size)) ;
984 /* The COMM chunk has an int aligned to an odd word boundary. Some
985 ** procesors are not able to deal with this (ie bus fault) so we have
986 ** to take special care.
988 comm_fmt->size += comm_fmt->size & 1 ;
990 bytesread +=
991 psf_binheader_readf (psf, "E242b", &(comm_fmt->numChannels), &(comm_fmt->numSampleFrames),
992 &(comm_fmt->sampleSize), &(comm_fmt->sampleRate), SIGNED_SIZEOF (comm_fmt->sampleRate)) ;
994 if (comm_fmt->size == SIZEOF_AIFF_COMM)
995 comm_fmt->encoding = NONE_MARKER ;
996 else if (comm_fmt->size == SIZEOF_AIFC_COMM_MIN)
997 bytesread += psf_binheader_readf (psf, "Em", &(comm_fmt->encoding)) ;
998 else if (comm_fmt->size >= SIZEOF_AIFC_COMM)
999 { unsigned char encoding_len ;
1001 bytesread += psf_binheader_readf (psf, "Em1", &(comm_fmt->encoding), &encoding_len) ;
1003 memset (psf->buffer, 0, comm_fmt->size) ;
1005 bytesread += psf_binheader_readf (psf, "b", psf->buffer,
1006 comm_fmt->size - SIZEOF_AIFC_COMM + 1) ;
1007 psf->buffer [encoding_len] = 0 ;
1010 psf_log_printf (psf, " COMM : %d\n", comm_fmt->size) ;
1011 psf_log_printf (psf, " Sample Rate : %d\n", tenbytefloat2int (comm_fmt->sampleRate)) ;
1012 psf_log_printf (psf, " Frames : %u%s\n", comm_fmt->numSampleFrames, (comm_fmt->numSampleFrames == 0 && psf->filelength > 100) ? " (Should not be 0)" : "") ;
1013 psf_log_printf (psf, " Channels : %d\n", comm_fmt->numChannels) ;
1015 /* Found some broken 'fl32' files with comm.samplesize == 16. Fix it here. */
1017 if ((comm_fmt->encoding == fl32_MARKER || comm_fmt->encoding == FL32_MARKER) && comm_fmt->sampleSize != 32)
1018 { psf_log_printf (psf, " Sample Size : %d (should be 32)\n", comm_fmt->sampleSize) ;
1019 comm_fmt->sampleSize = 32 ;
1021 else if ((comm_fmt->encoding == fl64_MARKER || comm_fmt->encoding == FL64_MARKER) && comm_fmt->sampleSize != 64)
1022 { psf_log_printf (psf, " Sample Size : %d (should be 64)\n", comm_fmt->sampleSize) ;
1023 comm_fmt->sampleSize = 64 ;
1025 else
1026 psf_log_printf (psf, " Sample Size : %d\n", comm_fmt->sampleSize) ;
1028 subformat = s_bitwidth_to_subformat (comm_fmt->sampleSize) ;
1030 psf->endian = SF_ENDIAN_BIG ;
1032 switch (comm_fmt->encoding)
1033 { case NONE_MARKER :
1034 psf->sf.format = (SF_FORMAT_AIFF | subformat) ;
1035 break ;
1037 case twos_MARKER :
1038 case in32_MARKER :
1039 psf->sf.format = (SF_ENDIAN_BIG | SF_FORMAT_AIFF | subformat) ;
1040 break ;
1042 case sowt_MARKER :
1043 case ni32_MARKER :
1044 psf->endian = SF_ENDIAN_LITTLE ;
1045 psf->sf.format = (SF_ENDIAN_LITTLE | SF_FORMAT_AIFF | subformat) ;
1046 break ;
1048 case fl32_MARKER :
1049 case FL32_MARKER :
1050 psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_FLOAT) ;
1051 break ;
1053 case ulaw_MARKER :
1054 case ULAW_MARKER :
1055 psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_ULAW) ;
1056 break ;
1058 case alaw_MARKER :
1059 case ALAW_MARKER :
1060 psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_ALAW) ;
1061 break ;
1063 case fl64_MARKER :
1064 case FL64_MARKER :
1065 psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_DOUBLE) ;
1066 break ;
1068 case raw_MARKER :
1069 psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_PCM_U8) ;
1070 break ;
1072 case DWVW_MARKER :
1073 psf->sf.format = SF_FORMAT_AIFF ;
1074 switch (comm_fmt->sampleSize)
1075 { case 12 :
1076 psf->sf.format |= SF_FORMAT_DWVW_12 ;
1077 break ;
1078 case 16 :
1079 psf->sf.format |= SF_FORMAT_DWVW_16 ;
1080 break ;
1081 case 24 :
1082 psf->sf.format |= SF_FORMAT_DWVW_24 ;
1083 break ;
1085 default :
1086 psf->sf.format |= SF_FORMAT_DWVW_N ;
1087 break ;
1089 break ;
1091 case GSM_MARKER :
1092 psf->sf.format = SF_FORMAT_AIFF ;
1093 psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_GSM610) ;
1094 break ;
1097 case ima4_MARKER :
1098 psf->endian = SF_ENDIAN_BIG ;
1099 psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_IMA_ADPCM) ;
1100 break ;
1102 default :
1103 psf_log_printf (psf, "AIFC : Unimplemented format : %M\n", comm_fmt->encoding) ;
1104 error = SFE_UNIMPLEMENTED ;
1107 if (! psf->buffer [0])
1108 psf_log_printf (psf, " Encoding : %M\n", comm_fmt->encoding) ;
1109 else
1110 psf_log_printf (psf, " Encoding : %M => %s\n", comm_fmt->encoding, (char*) psf->buffer) ;
1112 return error ;
1113 } /* aiff_read_comm_chunk */
1115 static int
1116 aiff_write_header (SF_PRIVATE *psf, int calc_length)
1117 { sf_count_t current ;
1118 unsigned char comm_sample_rate [10], comm_zero_bytes [2] = { 0, 0 } ;
1119 unsigned int comm_type, comm_size, comm_encoding, comm_frames ;
1120 int k, endian ;
1121 short bit_width ;
1123 current = psf_ftell (psf) ;
1125 if (calc_length)
1126 { psf->filelength = psf_get_filelen (psf) ;
1128 psf->datalength = psf->filelength - psf->dataoffset ;
1129 if (psf->dataend)
1130 psf->datalength -= psf->filelength - psf->dataend ;
1132 if (psf->bytewidth > 0)
1133 psf->sf.frames = psf->datalength / (psf->bytewidth * psf->sf.channels) ;
1136 if (psf->mode == SFM_RDWR && psf->dataoffset > 0)
1137 { /* Assuming here that the header has already been written and just
1138 ** needs to be corrected for new data length. That means that we
1139 ** only change the length fields of the FORM and SSND chunks;
1140 ** everything else can be skipped over.
1143 /* First write new FORM chunk. */
1144 psf->headindex = 0 ;
1145 psf_fseek (psf, 0, SEEK_SET) ;
1147 psf_binheader_writef (psf, "Etm8", FORM_MARKER, psf->filelength - 8) ;
1148 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
1150 /* Now write frame count field of COMM chunk header. */
1151 psf->headindex = 0 ;
1152 psf_fseek (psf, 22, SEEK_SET) ;
1154 psf_binheader_writef (psf, "Et8", psf->sf.frames) ;
1155 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
1157 /* Now write new SSND chunk header. */
1158 psf->headindex = 0 ;
1159 psf_fseek (psf, psf->dataoffset - 16, SEEK_SET) ;
1161 psf_binheader_writef (psf, "Etm8", SSND_MARKER, psf->datalength + SIZEOF_SSND_CHUNK) ;
1162 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
1164 if (current < psf->dataoffset)
1165 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
1166 else if (current > 0)
1167 psf_fseek (psf, current, SEEK_SET) ;
1169 return 0 ;
1172 endian = psf->sf.format & SF_FORMAT_ENDMASK ;
1173 if (CPU_IS_LITTLE_ENDIAN && endian == SF_ENDIAN_CPU)
1174 endian = SF_ENDIAN_LITTLE ;
1176 /* Standard value here. */
1177 bit_width = psf->bytewidth * 8 ;
1178 comm_frames = (psf->sf.frames > 0xFFFFFFFF) ? 0xFFFFFFFF : psf->sf.frames ;
1180 switch (psf->sf.format & SF_FORMAT_SUBMASK)
1181 { case SF_FORMAT_PCM_S8 :
1182 case SF_FORMAT_PCM_16 :
1183 case SF_FORMAT_PCM_24 :
1184 case SF_FORMAT_PCM_32 :
1185 switch (endian)
1186 { case SF_ENDIAN_BIG :
1187 psf->endian = SF_ENDIAN_BIG ;
1188 comm_type = AIFC_MARKER ;
1189 comm_size = SIZEOF_AIFC_COMM ;
1190 comm_encoding = twos_MARKER ;
1191 break ;
1193 case SF_ENDIAN_LITTLE :
1194 psf->endian = SF_ENDIAN_LITTLE ;
1195 comm_type = AIFC_MARKER ;
1196 comm_size = SIZEOF_AIFC_COMM ;
1197 comm_encoding = sowt_MARKER ;
1198 break ;
1200 default : /* SF_ENDIAN_FILE */
1201 psf->endian = SF_ENDIAN_BIG ;
1202 comm_type = AIFF_MARKER ;
1203 comm_size = SIZEOF_AIFF_COMM ;
1204 comm_encoding = 0 ;
1205 break ;
1207 break ;
1209 case SF_FORMAT_FLOAT : /* Big endian floating point. */
1210 psf->endian = SF_ENDIAN_BIG ;
1211 comm_type = AIFC_MARKER ;
1212 comm_size = SIZEOF_AIFC_COMM ;
1213 comm_encoding = FL32_MARKER ; /* Use 'FL32' because its easier to read. */
1214 break ;
1216 case SF_FORMAT_DOUBLE : /* Big endian double precision floating point. */
1217 psf->endian = SF_ENDIAN_BIG ;
1218 comm_type = AIFC_MARKER ;
1219 comm_size = SIZEOF_AIFC_COMM ;
1220 comm_encoding = FL64_MARKER ; /* Use 'FL64' because its easier to read. */
1221 break ;
1223 case SF_FORMAT_ULAW :
1224 psf->endian = SF_ENDIAN_BIG ;
1225 comm_type = AIFC_MARKER ;
1226 comm_size = SIZEOF_AIFC_COMM ;
1227 comm_encoding = ulaw_MARKER ;
1228 break ;
1230 case SF_FORMAT_ALAW :
1231 psf->endian = SF_ENDIAN_BIG ;
1232 comm_type = AIFC_MARKER ;
1233 comm_size = SIZEOF_AIFC_COMM ;
1234 comm_encoding = alaw_MARKER ;
1235 break ;
1237 case SF_FORMAT_PCM_U8 :
1238 psf->endian = SF_ENDIAN_BIG ;
1239 comm_type = AIFC_MARKER ;
1240 comm_size = SIZEOF_AIFC_COMM ;
1241 comm_encoding = raw_MARKER ;
1242 break ;
1244 case SF_FORMAT_DWVW_12 :
1245 psf->endian = SF_ENDIAN_BIG ;
1246 comm_type = AIFC_MARKER ;
1247 comm_size = SIZEOF_AIFC_COMM ;
1248 comm_encoding = DWVW_MARKER ;
1250 /* Override standard value here.*/
1251 bit_width = 12 ;
1252 break ;
1254 case SF_FORMAT_DWVW_16 :
1255 psf->endian = SF_ENDIAN_BIG ;
1256 comm_type = AIFC_MARKER ;
1257 comm_size = SIZEOF_AIFC_COMM ;
1258 comm_encoding = DWVW_MARKER ;
1260 /* Override standard value here.*/
1261 bit_width = 16 ;
1262 break ;
1264 case SF_FORMAT_DWVW_24 :
1265 psf->endian = SF_ENDIAN_BIG ;
1266 comm_type = AIFC_MARKER ;
1267 comm_size = SIZEOF_AIFC_COMM ;
1268 comm_encoding = DWVW_MARKER ;
1270 /* Override standard value here.*/
1271 bit_width = 24 ;
1272 break ;
1274 case SF_FORMAT_GSM610 :
1275 psf->endian = SF_ENDIAN_BIG ;
1276 comm_type = AIFC_MARKER ;
1277 comm_size = SIZEOF_AIFC_COMM ;
1278 comm_encoding = GSM_MARKER ;
1280 /* Override standard value here.*/
1281 bit_width = 16 ;
1282 break ;
1284 case SF_FORMAT_IMA_ADPCM :
1285 psf->endian = SF_ENDIAN_BIG ;
1286 comm_type = AIFC_MARKER ;
1287 comm_size = SIZEOF_AIFC_COMM ;
1288 comm_encoding = ima4_MARKER ;
1290 /* Override standard value here.*/
1291 bit_width = 16 ;
1292 comm_frames = psf->sf.frames / AIFC_IMA4_SAMPLES_PER_BLOCK ;
1293 break ;
1295 default : return SFE_BAD_OPEN_FORMAT ;
1298 /* Reset the current header length to zero. */
1299 psf->header [0] = 0 ;
1300 psf->headindex = 0 ;
1301 psf_fseek (psf, 0, SEEK_SET) ;
1303 psf_binheader_writef (psf, "Etm8", FORM_MARKER, psf->filelength - 8) ;
1305 /* Write COMM chunk. */
1306 psf_binheader_writef (psf, "Emm4", comm_type, COMM_MARKER, comm_size) ;
1308 uint2tenbytefloat (psf->sf.samplerate, comm_sample_rate) ;
1310 psf_binheader_writef (psf, "Et242", psf->sf.channels, comm_frames, bit_width) ;
1311 psf_binheader_writef (psf, "b", comm_sample_rate, sizeof (comm_sample_rate)) ;
1313 /* AIFC chunks have some extra data. */
1314 if (comm_type == AIFC_MARKER)
1315 psf_binheader_writef (psf, "mb", comm_encoding, comm_zero_bytes, sizeof (comm_zero_bytes)) ;
1317 if (psf->str_flags & SF_STR_LOCATE_START)
1318 aiff_write_strings (psf, SF_STR_LOCATE_START) ;
1320 if (psf->has_peak && psf->peak_loc == SF_PEAK_START)
1321 { psf_binheader_writef (psf, "Em4", PEAK_MARKER,
1322 sizeof (PEAK_CHUNK) + psf->sf.channels * sizeof (PEAK_POS)) ;
1323 psf_binheader_writef (psf, "E44", 1, time (NULL)) ;
1324 for (k = 0 ; k < psf->sf.channels ; k++)
1325 psf_binheader_writef (psf, "Ef4", psf->pchunk->peaks [k].value, psf->pchunk->peaks [k].position) ; /* XXXXX */
1328 /* Write SSND chunk. */
1329 psf_binheader_writef (psf, "Etm844", SSND_MARKER, psf->datalength + SIZEOF_SSND_CHUNK, 0, 0) ;
1331 /* Header construction complete so write it out. */
1332 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
1334 if (psf->error)
1335 return psf->error ;
1337 psf->dataoffset = psf->headindex ;
1339 if (current < psf->dataoffset)
1340 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
1341 else if (current > 0)
1342 psf_fseek (psf, current, SEEK_SET) ;
1344 return psf->error ;
1345 } /* aiff_write_header */
1347 static int
1348 aiff_write_tailer (SF_PRIVATE *psf)
1349 { int k ;
1351 /* Reset the current header length to zero. */
1352 psf->header [0] = 0 ;
1353 psf->headindex = 0 ;
1355 psf->dataend = psf_fseek (psf, 0, SEEK_END) ;
1357 if (psf->has_peak && psf->peak_loc == SF_PEAK_END)
1358 { psf_binheader_writef (psf, "Em4", PEAK_MARKER,
1359 sizeof (PEAK_CHUNK) + psf->sf.channels * sizeof (PEAK_POS)) ;
1360 psf_binheader_writef (psf, "E44", 1, time (NULL)) ;
1361 for (k = 0 ; k < psf->sf.channels ; k++)
1362 psf_binheader_writef (psf, "Ef4", psf->pchunk->peaks [k].value, psf->pchunk->peaks [k].position) ; /* XXXXX */
1365 if (psf->str_flags & SF_STR_LOCATE_END)
1366 aiff_write_strings (psf, SF_STR_LOCATE_END) ;
1368 /* Write the tailer. */
1369 if (psf->headindex)
1370 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
1372 return 0 ;
1373 } /* aiff_write_tailer */
1375 static void
1376 aiff_write_strings (SF_PRIVATE *psf, int location)
1377 { int k ;
1379 for (k = 0 ; k < SF_MAX_STRINGS ; k++)
1380 { if (psf->strings [k].type == 0)
1381 break ;
1383 if (psf->strings [k].flags != location)
1384 continue ;
1386 switch (psf->strings [k].type)
1387 { case SF_STR_SOFTWARE :
1388 psf_binheader_writef (psf, "Ems", APPL_MARKER, psf->strings [k].str) ;
1389 break ;
1391 case SF_STR_TITLE :
1392 psf_binheader_writef (psf, "Ems", NAME_MARKER, psf->strings [k].str) ;
1393 break ;
1395 case SF_STR_COPYRIGHT :
1396 psf_binheader_writef (psf, "Ems", c_MARKER, psf->strings [k].str) ;
1397 break ;
1399 case SF_STR_ARTIST :
1400 psf_binheader_writef (psf, "Ems", AUTH_MARKER, psf->strings [k].str) ;
1401 break ;
1403 case SF_STR_COMMENT :
1404 psf_binheader_writef (psf, "Ems", COMT_MARKER, psf->strings [k].str) ;
1405 break ;
1408 case SF_STR_DATE :
1409 psf_binheader_writef (psf, "Ems", ICRD_MARKER, psf->strings [k].str) ;
1410 break ;
1415 return ;
1416 } /* aiff_write_strings */
1418 static int
1419 aiff_command (SF_PRIVATE *psf, int command, void *data, int datasize)
1421 /* Avoid compiler warnings. */
1422 psf = psf ;
1423 data = data ;
1424 datasize = datasize ;
1426 switch (command)
1427 { default : break ;
1430 return 0 ;
1431 } /* aiff_command */
1433 static const char*
1434 get_loop_mode_str (short mode)
1435 { switch (mode)
1436 { case 0 : return "none" ;
1437 case 1 : return "forward" ;
1438 case 2 : return "backward" ;
1441 return "*** unknown" ;
1442 } /* get_loop_mode_str */
1444 /*==========================================================================================
1445 ** Rough hack at converting from 80 bit IEEE float in AIFF header to an int and
1446 ** back again. It assumes that all sample rates are between 1 and 800MHz, which
1447 ** should be OK as other sound file formats use a 32 bit integer to store sample
1448 ** rate.
1449 ** There is another (probably better) version in the source code to the SoX but it
1450 ** has a copyright which probably prevents it from being allowable as GPL/LGPL.
1453 static int
1454 tenbytefloat2int (unsigned char *bytes)
1455 { int val = 3 ;
1457 if (bytes [0] & 0x80) /* Negative number. */
1458 return 0 ;
1460 if (bytes [0] <= 0x3F) /* Less than 1. */
1461 return 1 ;
1463 if (bytes [0] > 0x40) /* Way too big. */
1464 return 0x4000000 ;
1466 if (bytes [0] == 0x40 && bytes [1] > 0x1C) /* Too big. */
1467 return 800000000 ;
1469 /* Ok, can handle it. */
1471 val = (bytes [2] << 23) | (bytes [3] << 15) | (bytes [4] << 7) | (bytes [5] >> 1) ;
1473 val >>= (29 - bytes [1]) ;
1475 return val ;
1476 } /* tenbytefloat2int */
1478 static void
1479 uint2tenbytefloat (unsigned int num, unsigned char *bytes)
1480 { unsigned int mask = 0x40000000 ;
1481 int count ;
1484 memset (bytes, 0, 10) ;
1486 if (num <= 1)
1487 { bytes [0] = 0x3F ;
1488 bytes [1] = 0xFF ;
1489 bytes [2] = 0x80 ;
1490 return ;
1493 bytes [0] = 0x40 ;
1495 if (num >= mask)
1496 { bytes [1] = 0x1D ;
1497 return ;
1500 for (count = 0 ; count <= 32 ; count ++)
1501 { if (num & mask)
1502 break ;
1503 mask >>= 1 ;
1506 num <<= count + 1 ;
1507 bytes [1] = 29 - count ;
1508 bytes [2] = (num >> 24) & 0xFF ;
1509 bytes [3] = (num >> 16) & 0xFF ;
1510 bytes [4] = (num >> 8) & 0xFF ;
1511 bytes [5] = num & 0xFF ;
1513 } /* uint2tenbytefloat */
1516 ** Do not edit or modify anything in this comment block.
1517 ** The arch-tag line is a file identity tag for the GNU Arch
1518 ** revision control system.
1520 ** arch-tag: 7dec56ca-d6f2-48cf-863b-a72e7e17a5d9
1523 ** Copyright (C) 1999-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
1525 ** This program is free software; you can redistribute it and/or modify
1526 ** it under the terms of the GNU Lesser General Public License as published by
1527 ** the Free Software Foundation; either version 2.1 of the License, or
1528 ** (at your option) any later version.
1530 ** This program is distributed in the hope that it will be useful,
1531 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
1532 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1533 ** GNU Lesser General Public License for more details.
1535 ** You should have received a copy of the GNU Lesser General Public License
1536 ** along with this program; if not, write to the Free Software
1537 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
1541 static sf_count_t alaw_read_alaw2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
1542 static sf_count_t alaw_read_alaw2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
1543 static sf_count_t alaw_read_alaw2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
1544 static sf_count_t alaw_read_alaw2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
1546 static sf_count_t alaw_write_s2alaw (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
1547 static sf_count_t alaw_write_i2alaw (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
1548 static sf_count_t alaw_write_f2alaw (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
1549 static sf_count_t alaw_write_d2alaw (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
1551 static void alaw2s_array (unsigned char *buffer, unsigned int count, short *ptr) ;
1552 static void alaw2i_array (unsigned char *buffer, unsigned int count, int *ptr) ;
1553 static void alaw2f_array (unsigned char *buffer, unsigned int count, float *ptr, float normfact) ;
1554 static void alaw2d_array (unsigned char *buffer, unsigned int count, double *ptr, double normfact) ;
1556 static void s2alaw_array (short *buffer, unsigned int count, unsigned char *ptr) ;
1557 static void i2alaw_array (int *buffer, unsigned int count, unsigned char *ptr) ;
1558 static void f2alaw_array (float *buffer, unsigned int count, unsigned char *ptr, float normfact) ;
1559 static void d2alaw_array (double *buffer, unsigned int count, unsigned char *ptr, double normfact) ;
1563 alaw_init (SF_PRIVATE *psf)
1565 if (psf->mode == SFM_READ || psf->mode == SFM_RDWR)
1566 { psf->read_short = alaw_read_alaw2s ;
1567 psf->read_int = alaw_read_alaw2i ;
1568 psf->read_float = alaw_read_alaw2f ;
1569 psf->read_double = alaw_read_alaw2d ;
1572 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
1573 { psf->write_short = alaw_write_s2alaw ;
1574 psf->write_int = alaw_write_i2alaw ;
1575 psf->write_float = alaw_write_f2alaw ;
1576 psf->write_double = alaw_write_d2alaw ;
1579 psf->bytewidth = 1 ;
1580 psf->blockwidth = psf->sf.channels ;
1583 if (psf->filelength > psf->dataoffset)
1584 psf->datalength = (psf->dataend) ? psf->dataend - psf->dataoffset :
1585 psf->filelength - psf->dataoffset ;
1586 else
1587 psf->datalength = 0 ;
1589 psf->sf.frames = psf->datalength / psf->blockwidth ;
1591 return 0 ;
1592 } /* alaw_init */
1594 static sf_count_t
1595 alaw_read_alaw2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
1596 { int bufferlen, readcount, thisread ;
1597 sf_count_t total = 0 ;
1599 bufferlen = sizeof (psf->buffer) / sizeof (char) ;
1601 while (len > 0)
1602 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
1603 thisread = psf_fread (psf->buffer, 1, readcount, psf) ;
1604 alaw2s_array ((unsigned char*) (psf->buffer), thisread, ptr + total) ;
1605 total += thisread ;
1606 if (thisread < readcount)
1607 break ;
1608 len -= thisread ;
1611 return total ;
1612 } /* alaw_read_alaw2s */
1614 static sf_count_t
1615 alaw_read_alaw2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
1616 { int bufferlen, readcount, thisread ;
1617 sf_count_t total = 0 ;
1619 bufferlen = sizeof (psf->buffer) / sizeof (char) ;
1621 while (len > 0)
1622 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
1623 thisread = psf_fread (psf->buffer, 1, readcount, psf) ;
1624 alaw2i_array ((unsigned char*) (psf->buffer), thisread, ptr + total) ;
1625 total += thisread ;
1626 if (thisread < readcount)
1627 break ;
1628 len -= thisread ;
1631 return total ;
1632 } /* alaw_read_alaw2i */
1634 static sf_count_t
1635 alaw_read_alaw2f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
1636 { int bufferlen, readcount, thisread ;
1637 sf_count_t total = 0 ;
1638 float normfact ;
1640 normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x8000) : 1.0 ;
1642 bufferlen = sizeof (psf->buffer) / sizeof (char) ;
1644 while (len > 0)
1645 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
1646 thisread = psf_fread (psf->buffer, 1, readcount, psf) ;
1647 alaw2f_array ((unsigned char*) (psf->buffer), thisread, ptr + total, normfact) ;
1648 total += thisread ;
1649 if (thisread < readcount)
1650 break ;
1651 len -= thisread ;
1654 return total ;
1655 } /* alaw_read_alaw2f */
1657 static sf_count_t
1658 alaw_read_alaw2d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
1659 { int bufferlen, readcount, thisread ;
1660 sf_count_t total = 0 ;
1661 double normfact ;
1663 normfact = (psf->norm_double) ? 1.0 / ((double) 0x8000) : 1.0 ;
1664 bufferlen = sizeof (psf->buffer) / sizeof (char) ;
1666 while (len > 0)
1667 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
1668 thisread = psf_fread (psf->buffer, 1, readcount, psf) ;
1669 alaw2d_array ((unsigned char*) (psf->buffer), thisread, ptr + total, normfact) ;
1670 total += thisread ;
1671 if (thisread < readcount)
1672 break ;
1673 len -= thisread ;
1676 return total ;
1677 } /* alaw_read_alaw2d */
1679 /*=============================================================================================
1682 static sf_count_t
1683 alaw_write_s2alaw (SF_PRIVATE *psf, short *ptr, sf_count_t len)
1684 { int bufferlen, writecount, thiswrite ;
1685 sf_count_t total = 0 ;
1687 bufferlen = sizeof (psf->buffer) / sizeof (char) ;
1689 while (len > 0)
1690 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
1691 s2alaw_array (ptr + total, writecount, (unsigned char*) (psf->buffer)) ;
1692 thiswrite = psf_fwrite (psf->buffer, 1, writecount, psf) ;
1693 total += thiswrite ;
1694 if (thiswrite < writecount)
1695 break ;
1697 len -= thiswrite ;
1700 return total ;
1701 } /* alaw_write_s2alaw */
1703 static sf_count_t
1704 alaw_write_i2alaw (SF_PRIVATE *psf, int *ptr, sf_count_t len)
1705 { int bufferlen, writecount, thiswrite ;
1706 sf_count_t total = 0 ;
1708 bufferlen = sizeof (psf->buffer) / sizeof (char) ;
1710 while (len > 0)
1711 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
1712 i2alaw_array (ptr + total, writecount, (unsigned char*) (psf->buffer)) ;
1713 thiswrite = psf_fwrite (psf->buffer, 1, writecount, psf) ;
1714 total += thiswrite ;
1715 if (thiswrite < writecount)
1716 break ;
1718 len -= thiswrite ;
1721 return total ;
1722 } /* alaw_write_i2alaw */
1724 static sf_count_t
1725 alaw_write_f2alaw (SF_PRIVATE *psf, float *ptr, sf_count_t len)
1726 { int bufferlen, writecount, thiswrite ;
1727 sf_count_t total = 0 ;
1728 float normfact ;
1730 normfact = (psf->norm_float == SF_TRUE) ? (1.0 * 0x7FFF) : 1.0 ;
1732 bufferlen = sizeof (psf->buffer) / sizeof (char) ;
1734 while (len > 0)
1735 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
1736 f2alaw_array (ptr + total, writecount, (unsigned char*) (psf->buffer), normfact) ;
1737 thiswrite = psf_fwrite (psf->buffer, 1, writecount, psf) ;
1738 total += thiswrite ;
1739 if (thiswrite < writecount)
1740 break ;
1742 len -= thiswrite ;
1745 return total ;
1746 } /* alaw_write_f2alaw */
1748 static sf_count_t
1749 alaw_write_d2alaw (SF_PRIVATE *psf, double *ptr, sf_count_t len)
1750 { int bufferlen, writecount, thiswrite ;
1751 sf_count_t total = 0 ;
1752 double normfact ;
1754 normfact = (psf->norm_double) ? (1.0 * 0x7FFF) : 1.0 ;
1756 bufferlen = sizeof (psf->buffer) / sizeof (char) ;
1758 while (len > 0)
1759 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
1760 d2alaw_array (ptr + total, writecount, (unsigned char*) (psf->buffer), normfact) ;
1761 thiswrite = psf_fwrite (psf->buffer, 1, writecount, psf) ;
1762 total += thiswrite ;
1763 if (thiswrite < writecount)
1764 break ;
1766 len -= thiswrite ;
1769 return total ;
1770 } /* alaw_write_d2alaw */
1772 /*=============================================================================================
1773 * Private static functions and data.
1776 static
1777 short alaw_decode [128] =
1778 { -5504, -5248, -6016, -5760, -4480, -4224, -4992, -4736,
1779 -7552, -7296, -8064, -7808, -6528, -6272, -7040, -6784,
1780 -2752, -2624, -3008, -2880, -2240, -2112, -2496, -2368,
1781 -3776, -3648, -4032, -3904, -3264, -3136, -3520, -3392,
1782 -22016, -20992, -24064, -23040, -17920, -16896, -19968, -18944,
1783 -30208, -29184, -32256, -31232, -26112, -25088, -28160, -27136,
1784 -11008, -10496, -12032, -11520, -8960, -8448, -9984, -9472,
1785 -15104, -14592, -16128, -15616, -13056, -12544, -14080, -13568,
1786 -344, -328, -376, -360, -280, -264, -312, -296,
1787 -472, -456, -504, -488, -408, -392, -440, -424,
1788 -88, -72, -120, -104, -24, -8, -56, -40,
1789 -216, -200, -248, -232, -152, -136, -184, -168,
1790 -1376, -1312, -1504, -1440, -1120, -1056, -1248, -1184,
1791 -1888, -1824, -2016, -1952, -1632, -1568, -1760, -1696,
1792 -688, -656, -752, -720, -560, -528, -624, -592,
1793 -944, -912, -1008, -976, -816, -784, -880, -848
1794 } ; /* alaw_decode */
1796 static
1797 unsigned char alaw_encode [2049] =
1798 { 0xD5, 0xD4, 0xD7, 0xD6, 0xD1, 0xD0, 0xD3, 0xD2, 0xDD, 0xDC, 0xDF, 0xDE,
1799 0xD9, 0xD8, 0xDB, 0xDA, 0xC5, 0xC4, 0xC7, 0xC6, 0xC1, 0xC0, 0xC3, 0xC2,
1800 0xCD, 0xCC, 0xCF, 0xCE, 0xC9, 0xC8, 0xCB, 0xCA, 0xF5, 0xF5, 0xF4, 0xF4,
1801 0xF7, 0xF7, 0xF6, 0xF6, 0xF1, 0xF1, 0xF0, 0xF0, 0xF3, 0xF3, 0xF2, 0xF2,
1802 0xFD, 0xFD, 0xFC, 0xFC, 0xFF, 0xFF, 0xFE, 0xFE, 0xF9, 0xF9, 0xF8, 0xF8,
1803 0xFB, 0xFB, 0xFA, 0xFA, 0xE5, 0xE5, 0xE5, 0xE5, 0xE4, 0xE4, 0xE4, 0xE4,
1804 0xE7, 0xE7, 0xE7, 0xE7, 0xE6, 0xE6, 0xE6, 0xE6, 0xE1, 0xE1, 0xE1, 0xE1,
1805 0xE0, 0xE0, 0xE0, 0xE0, 0xE3, 0xE3, 0xE3, 0xE3, 0xE2, 0xE2, 0xE2, 0xE2,
1806 0xED, 0xED, 0xED, 0xED, 0xEC, 0xEC, 0xEC, 0xEC, 0xEF, 0xEF, 0xEF, 0xEF,
1807 0xEE, 0xEE, 0xEE, 0xEE, 0xE9, 0xE9, 0xE9, 0xE9, 0xE8, 0xE8, 0xE8, 0xE8,
1808 0xEB, 0xEB, 0xEB, 0xEB, 0xEA, 0xEA, 0xEA, 0xEA, 0x95, 0x95, 0x95, 0x95,
1809 0x95, 0x95, 0x95, 0x95, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94,
1810 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x96, 0x96, 0x96, 0x96,
1811 0x96, 0x96, 0x96, 0x96, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91,
1812 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x93, 0x93, 0x93, 0x93,
1813 0x93, 0x93, 0x93, 0x93, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92,
1814 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9C, 0x9C, 0x9C, 0x9C,
1815 0x9C, 0x9C, 0x9C, 0x9C, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F,
1816 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x99, 0x99, 0x99, 0x99,
1817 0x99, 0x99, 0x99, 0x99, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98,
1818 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9A, 0x9A, 0x9A, 0x9A,
1819 0x9A, 0x9A, 0x9A, 0x9A, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85,
1820 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x84, 0x84, 0x84, 0x84,
1821 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84,
1822 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87,
1823 0x87, 0x87, 0x87, 0x87, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86,
1824 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x81, 0x81, 0x81, 0x81,
1825 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
1826 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
1827 0x80, 0x80, 0x80, 0x80, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83,
1828 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x82, 0x82, 0x82, 0x82,
1829 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82,
1830 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D,
1831 0x8D, 0x8D, 0x8D, 0x8D, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C,
1832 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8F, 0x8F, 0x8F, 0x8F,
1833 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F,
1834 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E,
1835 0x8E, 0x8E, 0x8E, 0x8E, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89,
1836 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x88, 0x88, 0x88, 0x88,
1837 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88,
1838 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B,
1839 0x8B, 0x8B, 0x8B, 0x8B, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A,
1840 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0xB5, 0xB5, 0xB5, 0xB5,
1841 0xB5, 0xB5, 0xB5, 0xB5, 0xB5, 0xB5, 0xB5, 0xB5, 0xB5, 0xB5, 0xB5, 0xB5,
1842 0xB5, 0xB5, 0xB5, 0xB5, 0xB5, 0xB5, 0xB5, 0xB5, 0xB5, 0xB5, 0xB5, 0xB5,
1843 0xB5, 0xB5, 0xB5, 0xB5, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4,
1844 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4,
1845 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4,
1846 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7,
1847 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7,
1848 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB6, 0xB6, 0xB6, 0xB6,
1849 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB6,
1850 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB6,
1851 0xB6, 0xB6, 0xB6, 0xB6, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1,
1852 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1,
1853 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1,
1854 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0,
1855 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0,
1856 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB3, 0xB3, 0xB3, 0xB3,
1857 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3,
1858 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3,
1859 0xB3, 0xB3, 0xB3, 0xB3, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2,
1860 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2,
1861 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2,
1862 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD,
1863 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD,
1864 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBC, 0xBC, 0xBC, 0xBC,
1865 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC,
1866 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC,
1867 0xBC, 0xBC, 0xBC, 0xBC, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF,
1868 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF,
1869 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF,
1870 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE,
1871 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE,
1872 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xB9, 0xB9, 0xB9, 0xB9,
1873 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB9,
1874 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB9,
1875 0xB9, 0xB9, 0xB9, 0xB9, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8,
1876 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8,
1877 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8,
1878 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
1879 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
1880 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBA, 0xBA, 0xBA, 0xBA,
1881 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA,
1882 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA,
1883 0xBA, 0xBA, 0xBA, 0xBA, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5,
1884 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5,
1885 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5,
1886 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5,
1887 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5,
1888 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA4, 0xA4, 0xA4, 0xA4,
1889 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4,
1890 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4,
1891 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4,
1892 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4,
1893 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4,
1894 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7,
1895 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7,
1896 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7,
1897 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7,
1898 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7,
1899 0xA7, 0xA7, 0xA7, 0xA7, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6,
1900 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6,
1901 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6,
1902 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6,
1903 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6,
1904 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA1, 0xA1, 0xA1, 0xA1,
1905 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1,
1906 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1,
1907 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1,
1908 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1,
1909 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1,
1910 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0,
1911 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0,
1912 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0,
1913 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0,
1914 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0,
1915 0xA0, 0xA0, 0xA0, 0xA0, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3,
1916 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3,
1917 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3,
1918 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3,
1919 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3,
1920 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA2, 0xA2, 0xA2, 0xA2,
1921 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2,
1922 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2,
1923 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2,
1924 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2,
1925 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2,
1926 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD,
1927 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD,
1928 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD,
1929 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD,
1930 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD,
1931 0xAD, 0xAD, 0xAD, 0xAD, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC,
1932 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC,
1933 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC,
1934 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC,
1935 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC,
1936 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAF, 0xAF, 0xAF, 0xAF,
1937 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF,
1938 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF,
1939 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF,
1940 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF,
1941 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF,
1942 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE,
1943 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE,
1944 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE,
1945 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE,
1946 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE,
1947 0xAE, 0xAE, 0xAE, 0xAE, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9,
1948 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9,
1949 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9,
1950 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9,
1951 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9,
1952 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA8, 0xA8, 0xA8, 0xA8,
1953 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8,
1954 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8,
1955 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8,
1956 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8,
1957 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8,
1958 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB,
1959 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB,
1960 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB,
1961 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB,
1962 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB,
1963 0xAB, 0xAB, 0xAB, 0xAB, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
1964 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
1965 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
1966 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
1967 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
1968 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0x2A
1969 } ; /* alaw_encode */
1971 static void
1972 alaw2s_array (unsigned char *buffer, unsigned int count, short *ptr)
1973 { while (count)
1974 { count -- ;
1975 if (buffer [count] & 0x80)
1976 ptr [count] = -1 * alaw_decode [((int) buffer [count]) & 0x7F] ;
1977 else
1978 ptr [count] = alaw_decode [((int) buffer [count]) & 0x7F] ;
1980 } /* alaw2s_array */
1982 static void
1983 alaw2i_array (unsigned char *buffer, unsigned int count, int *ptr)
1984 { while (count)
1985 { count -- ;
1986 if (buffer [count] & 0x80)
1987 ptr [count] = (-1 * alaw_decode [((int) buffer [count]) & 0x7F]) << 16 ;
1988 else
1989 ptr [count] = alaw_decode [((int) buffer [count]) & 0x7F] << 16 ;
1991 } /* alaw2i_array */
1993 static void
1994 alaw2f_array (unsigned char *buffer, unsigned int count, float *ptr, float normfact)
1995 { while (count)
1996 { count -- ;
1997 if (buffer [count] & 0x80)
1998 ptr [count] = -normfact * alaw_decode [((int) buffer [count]) & 0x7F] ;
1999 else
2000 ptr [count] = normfact * alaw_decode [((int) buffer [count]) & 0x7F] ;
2002 } /* alaw2f_array */
2004 static void
2005 alaw2d_array (unsigned char *buffer, unsigned int count, double *ptr, double normfact)
2006 { while (count)
2007 { count -- ;
2008 if (buffer [count] & 0x80)
2009 ptr [count] = -normfact * alaw_decode [((int) buffer [count]) & 0x7F] ;
2010 else
2011 ptr [count] = normfact * alaw_decode [((int) buffer [count]) & 0x7F] ;
2013 } /* alaw2d_array */
2015 static void
2016 s2alaw_array (short *ptr, unsigned int count, unsigned char *buffer)
2017 { while (count)
2018 { count -- ;
2019 if (ptr [count] >= 0)
2020 buffer [count] = alaw_encode [ptr [count] / 16] ;
2021 else
2022 buffer [count] = 0x7F & alaw_encode [ptr [count] / -16] ;
2024 } /* s2alaw_array */
2026 static void
2027 i2alaw_array (int *ptr, unsigned int count, unsigned char *buffer)
2028 { while (count)
2029 { count -- ;
2030 if (ptr [count] >= 0)
2031 buffer [count] = alaw_encode [ptr [count] >> (16 + 4)] ;
2032 else
2033 buffer [count] = 0x7F & alaw_encode [- ptr [count] >> (16 + 4)] ;
2035 } /* i2alaw_array */
2037 static void
2038 f2alaw_array (float *ptr, unsigned int count, unsigned char *buffer, float normfact)
2039 { while (count)
2040 { count -- ;
2041 if (ptr [count] >= 0)
2042 buffer [count] = alaw_encode [(lrintf (normfact * ptr [count])) / 16] ;
2043 else
2044 buffer [count] = 0x7F & alaw_encode [(lrintf (normfact * ptr [count])) / -16] ;
2046 } /* f2alaw_array */
2048 static void
2049 d2alaw_array (double *ptr, unsigned int count, unsigned char *buffer, double normfact)
2050 { while (count)
2051 { count -- ;
2052 if (ptr [count] >= 0)
2053 buffer [count] = alaw_encode [(lrint (normfact * ptr [count])) / 16] ;
2054 else
2055 buffer [count] = 0x7F & alaw_encode [(lrint (normfact * ptr [count])) / -16] ;
2057 } /* d2alaw_array */
2060 ** Do not edit or modify anything in this comment block.
2061 ** The arch-tag line is a file identity tag for the GNU Arch
2062 ** revision control system.
2064 ** arch-tag: 289ccfc2-42a6-4f1f-a29f-4dcc9bfa8752
2067 ** Copyright (C) 1999-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
2069 ** This program is free software; you can redistribute it and/or modify
2070 ** it under the terms of the GNU Lesser General Public License as published by
2071 ** the Free Software Foundation; either version 2.1 of the License, or
2072 ** (at your option) any later version.
2074 ** This program is distributed in the hope that it will be useful,
2075 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
2076 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2077 ** GNU Lesser General Public License for more details.
2079 ** You should have received a copy of the GNU Lesser General Public License
2080 ** along with this program; if not, write to the Free Software
2081 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
2085 #include <stdio.h>
2086 #include <fcntl.h>
2087 #include <string.h>
2088 #include <ctype.h>
2091 /*------------------------------------------------------------------------------
2092 ** Macros to handle big/little endian issues.
2095 #define DOTSND_MARKER (MAKE_MARKER ('.', 's', 'n', 'd'))
2096 #define DNSDOT_MARKER (MAKE_MARKER ('d', 'n', 's', '.'))
2098 #define AU_DATA_OFFSET 24
2100 /*------------------------------------------------------------------------------
2101 ** Known AU file encoding types.
2104 enum
2105 { AU_ENCODING_ULAW_8 = 1, /* 8-bit u-law samples */
2106 AU_ENCODING_PCM_8 = 2, /* 8-bit linear samples */
2107 AU_ENCODING_PCM_16 = 3, /* 16-bit linear samples */
2108 AU_ENCODING_PCM_24 = 4, /* 24-bit linear samples */
2109 AU_ENCODING_PCM_32 = 5, /* 32-bit linear samples */
2111 AU_ENCODING_FLOAT = 6, /* floating-point samples */
2112 AU_ENCODING_DOUBLE = 7, /* double-precision float samples */
2113 AU_ENCODING_INDIRECT = 8, /* fragmented sampled data */
2114 AU_ENCODING_NESTED = 9, /* ? */
2115 AU_ENCODING_DSP_CORE = 10, /* DSP program */
2116 AU_ENCODING_DSP_DATA_8 = 11, /* 8-bit fixed-point samples */
2117 AU_ENCODING_DSP_DATA_16 = 12, /* 16-bit fixed-point samples */
2118 AU_ENCODING_DSP_DATA_24 = 13, /* 24-bit fixed-point samples */
2119 AU_ENCODING_DSP_DATA_32 = 14, /* 32-bit fixed-point samples */
2121 AU_ENCODING_DISPLAY = 16, /* non-audio display data */
2122 AU_ENCODING_MULAW_SQUELCH = 17, /* ? */
2123 AU_ENCODING_EMPHASIZED = 18, /* 16-bit linear with emphasis */
2124 AU_ENCODING_NEXT = 19, /* 16-bit linear with compression (NEXT) */
2125 AU_ENCODING_COMPRESSED_EMPHASIZED = 20, /* A combination of the two above */
2126 AU_ENCODING_DSP_COMMANDS = 21, /* Music Kit DSP commands */
2127 AU_ENCODING_DSP_COMMANDS_SAMPLES = 22, /* ? */
2129 AU_ENCODING_ADPCM_G721_32 = 23, /* G721 32 kbs ADPCM - 4 bits per sample. */
2130 AU_ENCODING_ADPCM_G722 = 24, /* G722 64 kbs ADPCM */
2131 AU_ENCODING_ADPCM_G723_24 = 25, /* G723 24 kbs ADPCM - 3 bits per sample. */
2132 AU_ENCODING_ADPCM_G723_40 = 26, /* G723 40 kbs ADPCM - 5 bits per sample. */
2134 AU_ENCODING_ALAW_8 = 27
2137 /*------------------------------------------------------------------------------
2138 ** Typedefs.
2141 typedef struct
2142 { int dataoffset ;
2143 int datasize ;
2144 int encoding ;
2145 int samplerate ;
2146 int channels ;
2147 } AU_FMT ;
2150 /*------------------------------------------------------------------------------
2151 ** Private static functions.
2154 static int au_close (SF_PRIVATE *psf) ;
2156 static int au_format_to_encoding (int format) ;
2158 static int au_write_header (SF_PRIVATE *psf, int calc_length) ;
2159 static int au_read_header (SF_PRIVATE *psf) ;
2161 /*------------------------------------------------------------------------------
2162 ** Public function.
2166 au_open (SF_PRIVATE *psf)
2167 { int subformat ;
2168 int error = 0 ;
2170 if (psf->mode == SFM_READ || (psf->mode == SFM_RDWR && psf->filelength > 0))
2171 { if ((error = au_read_header (psf)))
2172 return error ;
2175 if ((psf->sf.format & SF_FORMAT_TYPEMASK) != SF_FORMAT_AU)
2176 return SFE_BAD_OPEN_FORMAT ;
2178 subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
2180 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
2181 { psf->endian = psf->sf.format & SF_FORMAT_ENDMASK ;
2182 if (CPU_IS_LITTLE_ENDIAN && psf->endian == SF_ENDIAN_CPU)
2183 psf->endian = SF_ENDIAN_LITTLE ;
2184 else if (psf->endian != SF_ENDIAN_LITTLE)
2185 psf->endian = SF_ENDIAN_BIG ;
2187 if (au_write_header (psf, SF_FALSE))
2188 return psf->error ;
2190 psf->write_header = au_write_header ;
2193 psf->close = au_close ;
2195 psf->blockwidth = psf->bytewidth * psf->sf.channels ;
2197 switch (subformat)
2198 { case SF_FORMAT_ULAW : /* 8-bit Ulaw encoding. */
2199 ulaw_init (psf) ;
2200 break ;
2202 case SF_FORMAT_PCM_S8 : /* 8-bit linear PCM. */
2203 error = pcm_init (psf) ;
2204 break ;
2206 case SF_FORMAT_PCM_16 : /* 16-bit linear PCM. */
2207 case SF_FORMAT_PCM_24 : /* 24-bit linear PCM */
2208 case SF_FORMAT_PCM_32 : /* 32-bit linear PCM. */
2209 error = pcm_init (psf) ;
2210 break ;
2212 case SF_FORMAT_ALAW : /* 8-bit Alaw encoding. */
2213 alaw_init (psf) ;
2214 break ;
2216 /* Lite remove start */
2217 case SF_FORMAT_FLOAT : /* 32-bit floats. */
2218 error = float32_init (psf) ;
2219 break ;
2221 case SF_FORMAT_DOUBLE : /* 64-bit double precision floats. */
2222 error = double64_init (psf) ;
2223 break ;
2225 case SF_FORMAT_G721_32 :
2226 if (psf->mode == SFM_READ)
2227 error = au_g72x_reader_init (psf, AU_H_G721_32) ;
2228 else if (psf->mode == SFM_WRITE)
2229 error = au_g72x_writer_init (psf, AU_H_G721_32) ;
2230 psf->sf.seekable = SF_FALSE ;
2231 break ;
2233 case SF_FORMAT_G723_24 :
2234 if (psf->mode == SFM_READ)
2235 error = au_g72x_reader_init (psf, AU_H_G723_24) ;
2236 else if (psf->mode == SFM_WRITE)
2237 error = au_g72x_writer_init (psf, AU_H_G723_24) ;
2238 psf->sf.seekable = SF_FALSE ;
2239 break ;
2241 case SF_FORMAT_G723_40 :
2242 if (psf->mode == SFM_READ)
2243 error = au_g72x_reader_init (psf, AU_H_G723_40) ;
2244 else if (psf->mode == SFM_WRITE)
2245 error = au_g72x_writer_init (psf, AU_H_G723_40) ;
2246 psf->sf.seekable = SF_FALSE ;
2247 break ;
2248 /* Lite remove end */
2250 default : break ;
2253 return error ;
2254 } /* au_open */
2257 au_nh_open (SF_PRIVATE *psf)
2259 if (psf->mode == SFM_RDWR)
2260 return SFE_BAD_OPEN_FORMAT ;
2262 if (psf_fseek (psf, psf->dataoffset, SEEK_SET))
2263 return SFE_BAD_SEEK ;
2265 psf_log_printf (psf, "Header-less u-law encoded file.\n") ;
2266 psf_log_printf (psf, "Setting up for 8kHz, mono, u-law.\n") ;
2268 psf->sf.format = SF_FORMAT_AU | SF_FORMAT_ULAW ;
2270 psf->dataoffset = 0 ;
2271 psf->endian = 0 ; /* Irrelevant but it must be something. */
2272 psf->sf.samplerate = 8000 ;
2273 psf->sf.channels = 1 ;
2274 psf->bytewidth = 1 ; /* Before decoding */
2276 ulaw_init (psf) ;
2278 psf->close = au_close ;
2280 psf->blockwidth = 1 ;
2281 psf->sf.frames = psf->filelength ;
2282 psf->datalength = psf->filelength - AU_DATA_OFFSET ;
2284 return 0 ;
2285 } /* au_nh_open */
2287 /*------------------------------------------------------------------------------
2290 static int
2291 au_close (SF_PRIVATE *psf)
2293 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
2294 au_write_header (psf, SF_TRUE) ;
2296 return 0 ;
2297 } /* au_close */
2299 static int
2300 au_write_header (SF_PRIVATE *psf, int calc_length)
2301 { sf_count_t current ;
2302 int encoding, datalength ;
2304 if (psf->pipeoffset > 0)
2305 return 0 ;
2307 current = psf_ftell (psf) ;
2309 if (calc_length)
2310 { psf->filelength = psf_get_filelen (psf) ;
2312 psf->datalength = psf->filelength - psf->dataoffset ;
2313 if (psf->dataend)
2314 psf->datalength -= psf->filelength - psf->dataend ;
2316 psf->sf.frames = psf->datalength / (psf->bytewidth * psf->sf.channels) ;
2319 encoding = au_format_to_encoding (psf->sf.format & SF_FORMAT_SUBMASK) ;
2320 if (! encoding)
2321 return (psf->error = SFE_BAD_OPEN_FORMAT) ;
2323 /* Reset the current header length to zero. */
2324 psf->header [0] = 0 ;
2325 psf->headindex = 0 ;
2328 ** Only attempt to seek if we are not writng to a pipe. If we are
2329 ** writing to a pipe we shouldn't be here anyway.
2331 if (psf->is_pipe == SF_FALSE)
2332 psf_fseek (psf, 0, SEEK_SET) ;
2335 ** AU format files allow a datalength value of -1 if the datalength
2336 ** is not know at the time the header is written.
2337 ** Also use this value of -1 if the datalength > 2 gigabytes.
2339 if (psf->datalength < 0 || psf->datalength > 0x7FFFFFFF)
2340 datalength = -1 ;
2341 else
2342 datalength = (int) (psf->datalength & 0x7FFFFFFF) ;
2344 if (psf->endian == SF_ENDIAN_BIG)
2345 { psf_binheader_writef (psf, "Em4", DOTSND_MARKER, AU_DATA_OFFSET) ;
2346 psf_binheader_writef (psf, "E4444", datalength, encoding, psf->sf.samplerate, psf->sf.channels) ;
2348 else if (psf->endian == SF_ENDIAN_LITTLE)
2349 { psf_binheader_writef (psf, "em4", DNSDOT_MARKER, AU_DATA_OFFSET) ;
2350 psf_binheader_writef (psf, "e4444", datalength, encoding, psf->sf.samplerate, psf->sf.channels) ;
2352 else
2353 return (psf->error = SFE_BAD_OPEN_FORMAT) ;
2355 /* Header construction complete so write it out. */
2356 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
2358 if (psf->error)
2359 return psf->error ;
2361 psf->dataoffset = psf->headindex ;
2363 if (current > 0)
2364 psf_fseek (psf, current, SEEK_SET) ;
2366 return psf->error ;
2367 } /* au_write_header */
2369 static int
2370 au_format_to_encoding (int format)
2372 switch (format)
2373 { case SF_FORMAT_PCM_S8 : return AU_ENCODING_PCM_8 ;
2374 case SF_FORMAT_PCM_16 : return AU_ENCODING_PCM_16 ;
2375 case SF_FORMAT_PCM_24 : return AU_ENCODING_PCM_24 ;
2376 case SF_FORMAT_PCM_32 : return AU_ENCODING_PCM_32 ;
2378 case SF_FORMAT_FLOAT : return AU_ENCODING_FLOAT ;
2379 case SF_FORMAT_DOUBLE : return AU_ENCODING_DOUBLE ;
2381 case SF_FORMAT_ULAW : return AU_ENCODING_ULAW_8 ;
2382 case SF_FORMAT_ALAW : return AU_ENCODING_ALAW_8 ;
2384 case SF_FORMAT_G721_32 : return AU_ENCODING_ADPCM_G721_32 ;
2385 case SF_FORMAT_G723_24 : return AU_ENCODING_ADPCM_G723_24 ;
2386 case SF_FORMAT_G723_40 : return AU_ENCODING_ADPCM_G723_40 ;
2388 default : break ;
2390 return 0 ;
2391 } /* au_format_to_encoding */
2393 static int
2394 au_read_header (SF_PRIVATE *psf)
2395 { AU_FMT au_fmt ;
2396 int marker, dword ;
2398 psf_binheader_readf (psf, "pm", 0, &marker) ;
2399 psf_log_printf (psf, "%M\n", marker) ;
2401 if (marker == DOTSND_MARKER)
2402 { psf->endian = SF_ENDIAN_BIG ;
2404 psf_binheader_readf (psf, "E44444", &(au_fmt.dataoffset), &(au_fmt.datasize),
2405 &(au_fmt.encoding), &(au_fmt.samplerate), &(au_fmt.channels)) ;
2407 else if (marker == DNSDOT_MARKER)
2408 { psf->endian = SF_ENDIAN_LITTLE ;
2409 psf_binheader_readf (psf, "e44444", &(au_fmt.dataoffset), &(au_fmt.datasize),
2410 &(au_fmt.encoding), &(au_fmt.samplerate), &(au_fmt.channels)) ;
2412 else
2413 return SFE_AU_NO_DOTSND ;
2415 psf_log_printf (psf, " Data Offset : %d\n", au_fmt.dataoffset) ;
2417 if (psf->fileoffset > 0 && au_fmt.datasize == -1)
2418 { psf_log_printf (psf, " Data Size : -1\n") ;
2419 return SFE_AU_EMBED_BAD_LEN ;
2422 if (psf->fileoffset > 0)
2423 { psf->filelength = au_fmt.dataoffset + au_fmt.datasize ;
2424 psf_log_printf (psf, " Data Size : %d\n", au_fmt.datasize) ;
2426 else if (au_fmt.datasize == -1 || au_fmt.dataoffset + au_fmt.datasize == psf->filelength)
2427 psf_log_printf (psf, " Data Size : %d\n", au_fmt.datasize) ;
2428 else if (au_fmt.dataoffset + au_fmt.datasize < psf->filelength)
2429 { psf->filelength = au_fmt.dataoffset + au_fmt.datasize ;
2430 psf_log_printf (psf, " Data Size : %d\n", au_fmt.datasize) ;
2432 else
2433 { dword = psf->filelength - au_fmt.dataoffset ;
2434 psf_log_printf (psf, " Data Size : %d (should be %d)\n", au_fmt.datasize, dword) ;
2435 au_fmt.datasize = dword ;
2438 psf->dataoffset = au_fmt.dataoffset ;
2439 psf->datalength = psf->filelength - psf->dataoffset ;
2441 if (psf_ftell (psf) < psf->dataoffset)
2442 psf_binheader_readf (psf, "j", psf->dataoffset - psf_ftell (psf)) ;
2444 psf->close = au_close ;
2446 psf->sf.samplerate = au_fmt.samplerate ;
2447 psf->sf.channels = au_fmt.channels ;
2449 /* Only fill in type major. */
2450 if (psf->endian == SF_ENDIAN_BIG)
2451 psf->sf.format = SF_FORMAT_AU ;
2452 else if (psf->endian == SF_ENDIAN_LITTLE)
2453 psf->sf.format = SF_ENDIAN_LITTLE | SF_FORMAT_AU ;
2455 psf_log_printf (psf, " Encoding : %d => ", au_fmt.encoding) ;
2457 psf->sf.format = psf->sf.format & SF_FORMAT_ENDMASK ;
2459 switch (au_fmt.encoding)
2460 { case AU_ENCODING_ULAW_8 :
2461 psf->sf.format |= SF_FORMAT_AU | SF_FORMAT_ULAW ;
2462 psf->bytewidth = 1 ; /* Before decoding */
2463 psf_log_printf (psf, "8-bit ISDN u-law\n") ;
2464 break ;
2466 case AU_ENCODING_PCM_8 :
2467 psf->sf.format |= SF_FORMAT_AU | SF_FORMAT_PCM_S8 ;
2468 psf->bytewidth = 1 ;
2469 psf_log_printf (psf, "8-bit linear PCM\n") ;
2470 break ;
2472 case AU_ENCODING_PCM_16 :
2473 psf->sf.format |= SF_FORMAT_AU | SF_FORMAT_PCM_16 ;
2474 psf->bytewidth = 2 ;
2475 psf_log_printf (psf, "16-bit linear PCM\n") ;
2476 break ;
2478 case AU_ENCODING_PCM_24 :
2479 psf->sf.format |= SF_FORMAT_AU | SF_FORMAT_PCM_24 ;
2480 psf->bytewidth = 3 ;
2481 psf_log_printf (psf, "24-bit linear PCM\n") ;
2482 break ;
2484 case AU_ENCODING_PCM_32 :
2485 psf->sf.format |= SF_FORMAT_AU | SF_FORMAT_PCM_32 ;
2486 psf->bytewidth = 4 ;
2487 psf_log_printf (psf, "32-bit linear PCM\n") ;
2488 break ;
2490 case AU_ENCODING_FLOAT :
2491 psf->sf.format |= SF_FORMAT_AU | SF_FORMAT_FLOAT ;
2492 psf->bytewidth = 4 ;
2493 psf_log_printf (psf, "32-bit float\n") ;
2494 break ;
2496 case AU_ENCODING_DOUBLE :
2497 psf->sf.format |= SF_FORMAT_AU | SF_FORMAT_DOUBLE ;
2498 psf->bytewidth = 8 ;
2499 psf_log_printf (psf, "64-bit double precision float\n") ;
2500 break ;
2502 case AU_ENCODING_ALAW_8 :
2503 psf->sf.format |= SF_FORMAT_AU | SF_FORMAT_ALAW ;
2504 psf->bytewidth = 1 ; /* Before decoding */
2505 psf_log_printf (psf, "8-bit ISDN A-law\n") ;
2506 break ;
2508 case AU_ENCODING_ADPCM_G721_32 :
2509 psf->sf.format |= SF_FORMAT_AU | SF_FORMAT_G721_32 ;
2510 psf->bytewidth = 0 ;
2511 psf_log_printf (psf, "G721 32kbs ADPCM\n") ;
2512 break ;
2514 case AU_ENCODING_ADPCM_G723_24 :
2515 psf->sf.format |= SF_FORMAT_AU | SF_FORMAT_G723_24 ;
2516 psf->bytewidth = 0 ;
2517 psf_log_printf (psf, "G723 24kbs ADPCM\n") ;
2518 break ;
2520 case AU_ENCODING_ADPCM_G723_40 :
2521 psf->sf.format |= SF_FORMAT_AU | SF_FORMAT_G723_40 ;
2522 psf->bytewidth = 0 ;
2523 psf_log_printf (psf, "G723 40kbs ADPCM\n") ;
2524 break ;
2526 case AU_ENCODING_ADPCM_G722 :
2527 psf_log_printf (psf, "G722 64 kbs ADPCM (unsupported)\n") ;
2528 break ;
2530 case AU_ENCODING_NEXT :
2531 psf_log_printf (psf, "Weird NeXT encoding format (unsupported)\n") ;
2532 break ;
2534 default :
2535 psf_log_printf (psf, "Unknown!!\n") ;
2536 break ;
2539 psf_log_printf (psf, " Sample Rate : %d\n", au_fmt.samplerate) ;
2540 psf_log_printf (psf, " Channels : %d\n", au_fmt.channels) ;
2542 psf->blockwidth = psf->sf.channels * psf->bytewidth ;
2544 if (! psf->sf.frames && psf->blockwidth)
2545 psf->sf.frames = (psf->filelength - psf->dataoffset) / psf->blockwidth ;
2547 return 0 ;
2548 } /* au_read_header */
2550 ** Do not edit or modify anything in this comment block.
2551 ** The arch-tag line is a file identity tag for the GNU Arch
2552 ** revision control system.
2554 ** arch-tag: 31f691b1-cde9-4ed2-9469-6bca60fb9cd0
2557 ** Copyright (C) 1999-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
2559 ** This program is free software; you can redistribute it and/or modify
2560 ** it under the terms of the GNU Lesser General Public License as published by
2561 ** the Free Software Foundation; either version 2.1 of the License, or
2562 ** (at your option) any later version.
2564 ** This program is distributed in the hope that it will be useful,
2565 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
2566 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2567 ** GNU Lesser General Public License for more details.
2569 ** You should have received a copy of the GNU Lesser General Public License
2570 ** along with this program; if not, write to the Free Software
2571 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
2575 #include <stdio.h>
2576 #include <stdlib.h>
2577 #include <string.h>
2580 static int au_g72x_read_block (SF_PRIVATE *psf, G72x_DATA *pg72x, short *ptr, int len) ;
2581 static int au_g72x_write_block (SF_PRIVATE *psf, G72x_DATA *pg72x, short *ptr, int len) ;
2583 static int au_g72x_decode_block (SF_PRIVATE *psf, G72x_DATA *pg72x) ;
2584 static int au_g72x_encode_block (SF_PRIVATE *psf, G72x_DATA *pg72x) ;
2586 static sf_count_t au_g72x_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
2587 static sf_count_t au_g72x_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
2588 static sf_count_t au_g72x_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
2589 static sf_count_t au_g72x_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
2591 static sf_count_t au_g72x_write_s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
2592 static sf_count_t au_g72x_write_i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
2593 static sf_count_t au_g72x_write_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
2594 static sf_count_t au_g72x_write_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
2596 static sf_count_t au_g72x_seek (SF_PRIVATE *psf, int mode, sf_count_t offset) ;
2598 static int au_g72x_close (SF_PRIVATE *psf) ;
2601 /*============================================================================================
2602 ** WAV G721 Reader initialisation function.
2606 au_g72x_reader_init (SF_PRIVATE *psf, int codec)
2607 { G72x_DATA *pg72x ;
2608 int bitspersample ;
2610 psf->sf.seekable = SF_FALSE ;
2612 if (psf->sf.channels != 1)
2613 return SFE_G72X_NOT_MONO ;
2615 if (! (pg72x = malloc (sizeof (G72x_DATA))))
2616 return SFE_MALLOC_FAILED ;
2618 psf->fdata = (void*) pg72x ;
2620 pg72x->blockcount = 0 ;
2621 pg72x->samplecount = 0 ;
2623 switch (codec)
2624 { case AU_H_G721_32 :
2625 g72x_reader_init (pg72x, G721_32_BITS_PER_SAMPLE) ;
2626 pg72x->bytesperblock = G721_32_BYTES_PER_BLOCK ;
2627 bitspersample = G721_32_BITS_PER_SAMPLE ;
2628 break ;
2630 case AU_H_G723_24:
2631 g72x_reader_init (pg72x, G723_24_BITS_PER_SAMPLE) ;
2632 pg72x->bytesperblock = G723_24_BYTES_PER_BLOCK ;
2633 bitspersample = G723_24_BITS_PER_SAMPLE ;
2634 break ;
2636 case AU_H_G723_40:
2637 g72x_reader_init (pg72x, G723_40_BITS_PER_SAMPLE) ;
2638 pg72x->bytesperblock = G723_40_BYTES_PER_BLOCK ;
2639 bitspersample = G723_40_BITS_PER_SAMPLE ;
2640 break ;
2642 default : return SFE_UNIMPLEMENTED ;
2645 psf->read_short = au_g72x_read_s ;
2646 psf->read_int = au_g72x_read_i ;
2647 psf->read_float = au_g72x_read_f ;
2648 psf->read_double = au_g72x_read_d ;
2650 psf->seek = au_g72x_seek ;
2651 psf->close = au_g72x_close ;
2653 psf->blockwidth = psf->bytewidth = 1 ;
2655 psf->filelength = psf_get_filelen (psf) ;
2656 psf->datalength = psf->filelength - psf->dataoffset ;
2658 if (psf->datalength % pg72x->blocksize)
2659 pg72x->blocks = (psf->datalength / pg72x->blocksize) + 1 ;
2660 else
2661 pg72x->blocks = psf->datalength / pg72x->blocksize ;
2663 psf->sf.frames = (8 * psf->datalength) / bitspersample ;
2665 if ((psf->sf.frames * bitspersample) / 8 != psf->datalength)
2666 psf_log_printf (psf, "*** Warning : weird psf->datalength.\n") ;
2668 au_g72x_decode_block (psf, pg72x) ;
2670 return 0 ;
2671 } /* au_g72x_reader_init */
2673 /*============================================================================================
2674 ** WAV G721 writer initialisation function.
2678 au_g72x_writer_init (SF_PRIVATE *psf, int codec)
2679 { G72x_DATA *pg72x ;
2680 int bitspersample ;
2682 psf->sf.seekable = SF_FALSE ;
2684 if (psf->sf.channels != 1)
2685 return SFE_G72X_NOT_MONO ;
2687 if (! (pg72x = malloc (sizeof (G72x_DATA))))
2688 return SFE_MALLOC_FAILED ;
2690 psf->fdata = (void*) pg72x ;
2692 pg72x->blockcount = 0 ;
2693 pg72x->samplecount = 0 ;
2695 switch (codec)
2696 { case AU_H_G721_32 :
2697 g72x_writer_init (pg72x, G721_32_BITS_PER_SAMPLE) ;
2698 pg72x->bytesperblock = G721_32_BYTES_PER_BLOCK ;
2699 bitspersample = G721_32_BITS_PER_SAMPLE ;
2700 break ;
2702 case AU_H_G723_24:
2703 g72x_writer_init (pg72x, G723_24_BITS_PER_SAMPLE) ;
2704 pg72x->bytesperblock = G723_24_BYTES_PER_BLOCK ;
2705 bitspersample = G723_24_BITS_PER_SAMPLE ;
2706 break ;
2708 case AU_H_G723_40:
2709 g72x_writer_init (pg72x, G723_40_BITS_PER_SAMPLE) ;
2710 pg72x->bytesperblock = G723_40_BYTES_PER_BLOCK ;
2711 bitspersample = G723_40_BITS_PER_SAMPLE ;
2712 break ;
2714 default : return SFE_UNIMPLEMENTED ;
2717 psf->write_short = au_g72x_write_s ;
2718 psf->write_int = au_g72x_write_i ;
2719 psf->write_float = au_g72x_write_f ;
2720 psf->write_double = au_g72x_write_d ;
2722 psf->close = au_g72x_close ;
2724 psf->blockwidth = psf->bytewidth = 1 ;
2726 psf->filelength = psf_get_filelen (psf) ;
2727 if (psf->filelength < psf->dataoffset)
2728 psf->filelength = psf->dataoffset ;
2730 psf->datalength = psf->filelength - psf->dataoffset ;
2732 if (psf->datalength % pg72x->blocksize)
2733 pg72x->blocks = (psf->datalength / pg72x->blocksize) + 1 ;
2734 else
2735 pg72x->blocks = psf->datalength / pg72x->blocksize ;
2737 if (psf->datalength > 0)
2738 psf->sf.frames = (8 * psf->datalength) / bitspersample ;
2740 if ((psf->sf.frames * bitspersample) / 8 != psf->datalength)
2741 psf_log_printf (psf, "*** Warning : weird psf->datalength.\n") ;
2743 return 0 ;
2744 } /* au_g72x_writer_init */
2746 /*============================================================================================
2747 ** G721 Read Functions.
2750 static int
2751 au_g72x_decode_block (SF_PRIVATE *psf, G72x_DATA *pg72x)
2752 { int k ;
2754 pg72x->blockcount ++ ;
2755 pg72x->samplecount = 0 ;
2757 if (pg72x->samplecount > pg72x->blocksize)
2758 { memset (pg72x->samples, 0, G72x_BLOCK_SIZE * sizeof (short)) ;
2759 return 1 ;
2762 if ((k = psf_fread (pg72x->block, 1, pg72x->bytesperblock, psf)) != pg72x->bytesperblock)
2763 psf_log_printf (psf, "*** Warning : short read (%d != %d).\n", k, pg72x->bytesperblock) ;
2765 pg72x->blocksize = k ;
2766 g72x_decode_block (pg72x) ;
2768 return 1 ;
2769 } /* au_g72x_decode_block */
2771 static int
2772 au_g72x_read_block (SF_PRIVATE *psf, G72x_DATA *pg72x, short *ptr, int len)
2773 { int count, total = 0, indx = 0 ;
2775 while (indx < len)
2776 { if (pg72x->blockcount >= pg72x->blocks && pg72x->samplecount >= pg72x->samplesperblock)
2777 { memset (&(ptr [indx]), 0, (len - indx) * sizeof (short)) ;
2778 return total ;
2781 if (pg72x->samplecount >= pg72x->samplesperblock)
2782 au_g72x_decode_block (psf, pg72x) ;
2784 count = pg72x->samplesperblock - pg72x->samplecount ;
2785 count = (len - indx > count) ? count : len - indx ;
2787 memcpy (&(ptr [indx]), &(pg72x->samples [pg72x->samplecount]), count * sizeof (short)) ;
2788 indx += count ;
2789 pg72x->samplecount += count ;
2790 total = indx ;
2793 return total ;
2794 } /* au_g72x_read_block */
2796 static sf_count_t
2797 au_g72x_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
2798 { G72x_DATA *pg72x ;
2799 int readcount, count ;
2800 sf_count_t total = 0 ;
2802 if (! psf->fdata)
2803 return 0 ;
2804 pg72x = (G72x_DATA*) psf->fdata ;
2806 while (len > 0)
2807 { readcount = (len > 0x10000000) ? 0x10000000 : (int) len ;
2809 count = au_g72x_read_block (psf, pg72x, ptr, readcount) ;
2811 total += count ;
2812 len -= count ;
2814 if (count != readcount)
2815 break ;
2818 return total ;
2819 } /* au_g72x_read_s */
2821 static sf_count_t
2822 au_g72x_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
2823 { G72x_DATA *pg72x ;
2824 short *sptr ;
2825 int k, bufferlen, readcount = 0, count ;
2826 sf_count_t total = 0 ;
2828 if (! psf->fdata)
2829 return 0 ;
2830 pg72x = (G72x_DATA*) psf->fdata ;
2832 sptr = (short*) psf->buffer ;
2833 bufferlen = SF_BUFFER_LEN / sizeof (short) ;
2834 while (len > 0)
2835 { readcount = (len >= bufferlen) ? bufferlen : len ;
2836 count = au_g72x_read_block (psf, pg72x, sptr, readcount) ;
2838 for (k = 0 ; k < readcount ; k++)
2839 ptr [total + k] = sptr [k] << 16 ;
2841 total += count ;
2842 len -= readcount ;
2843 if (count != readcount)
2844 break ;
2847 return total ;
2848 } /* au_g72x_read_i */
2850 static sf_count_t
2851 au_g72x_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
2852 { G72x_DATA *pg72x ;
2853 short *sptr ;
2854 int k, bufferlen, readcount = 0, count ;
2855 sf_count_t total = 0 ;
2856 float normfact ;
2858 if (! psf->fdata)
2859 return 0 ;
2860 pg72x = (G72x_DATA*) psf->fdata ;
2862 normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x8000) : 1.0 ;
2864 sptr = (short*) psf->buffer ;
2865 bufferlen = SF_BUFFER_LEN / sizeof (short) ;
2866 while (len > 0)
2867 { readcount = (len >= bufferlen) ? bufferlen : len ;
2868 count = au_g72x_read_block (psf, pg72x, sptr, readcount) ;
2869 for (k = 0 ; k < readcount ; k++)
2870 ptr [total + k] = normfact * sptr [k] ;
2872 total += count ;
2873 len -= readcount ;
2874 if (count != readcount)
2875 break ;
2878 return total ;
2879 } /* au_g72x_read_f */
2881 static sf_count_t
2882 au_g72x_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
2883 { G72x_DATA *pg72x ;
2884 short *sptr ;
2885 int k, bufferlen, readcount = 0, count ;
2886 sf_count_t total = 0 ;
2887 double normfact ;
2889 if (! psf->fdata)
2890 return 0 ;
2891 pg72x = (G72x_DATA*) psf->fdata ;
2893 normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x8000) : 1.0 ;
2895 sptr = (short*) psf->buffer ;
2896 bufferlen = SF_BUFFER_LEN / sizeof (short) ;
2897 while (len > 0)
2898 { readcount = (len >= bufferlen) ? bufferlen : len ;
2899 count = au_g72x_read_block (psf, pg72x, sptr, readcount) ;
2900 for (k = 0 ; k < readcount ; k++)
2901 ptr [total + k] = normfact * (double) (sptr [k]) ;
2903 total += count ;
2904 len -= readcount ;
2905 if (count != readcount)
2906 break ;
2909 return total ;
2910 } /* au_g72x_read_d */
2912 static sf_count_t
2913 au_g72x_seek (SF_PRIVATE *psf, int mode, sf_count_t offset)
2915 /* Prevent compiler warnings. */
2916 mode ++ ;
2917 offset ++ ;
2919 psf_log_printf (psf, "seek unsupported\n") ;
2921 /* No simple solution. To do properly, would need to seek
2922 ** to start of file and decode everything up to seek position.
2923 ** Maybe implement SEEK_SET to 0 only?
2925 return 0 ;
2928 ** G72x_DATA *pg72x ;
2929 ** int newblock, newsample, samplecount ;
2931 ** if (! psf->fdata)
2932 ** return 0 ;
2933 ** pg72x = (G72x_DATA*) psf->fdata ;
2935 ** if (! (psf->datalength && psf->dataoffset))
2936 ** { psf->error = SFE_BAD_SEEK ;
2937 ** return ((sf_count_t) -1) ;
2938 ** } ;
2940 ** samplecount = (8 * psf->datalength) / G721_32_BITS_PER_SAMPLE ;
2942 ** switch (whence)
2943 ** { case SEEK_SET :
2944 ** if (offset < 0 || offset > samplecount)
2945 ** { psf->error = SFE_BAD_SEEK ;
2946 ** return ((sf_count_t) -1) ;
2947 ** } ;
2948 ** newblock = offset / pg72x->samplesperblock ;
2949 ** newsample = offset % pg72x->samplesperblock ;
2950 ** break ;
2952 ** case SEEK_CUR :
2953 ** if (psf->current + offset < 0 || psf->current + offset > samplecount)
2954 ** { psf->error = SFE_BAD_SEEK ;
2955 ** return ((sf_count_t) -1) ;
2956 ** } ;
2957 ** newblock = (8 * (psf->current + offset)) / pg72x->samplesperblock ;
2958 ** newsample = (8 * (psf->current + offset)) % pg72x->samplesperblock ;
2959 ** break ;
2961 ** case SEEK_END :
2962 ** if (offset > 0 || samplecount + offset < 0)
2963 ** { psf->error = SFE_BAD_SEEK ;
2964 ** return ((sf_count_t) -1) ;
2965 ** } ;
2966 ** newblock = (samplecount + offset) / pg72x->samplesperblock ;
2967 ** newsample = (samplecount + offset) % pg72x->samplesperblock ;
2968 ** break ;
2970 ** default :
2971 ** psf->error = SFE_BAD_SEEK ;
2972 ** return ((sf_count_t) -1) ;
2973 ** } ;
2975 ** if (psf->mode == SFM_READ)
2976 ** { psf_fseek (psf, psf->dataoffset + newblock * pg72x->blocksize, SEEK_SET) ;
2977 ** pg72x->blockcount = newblock ;
2978 ** au_g72x_decode_block (psf, pg72x) ;
2979 ** pg72x->samplecount = newsample ;
2980 ** }
2981 ** else
2982 ** { /+* What to do about write??? *+/
2983 ** psf->error = SFE_BAD_SEEK ;
2984 ** return ((sf_count_t) -1) ;
2985 ** } ;
2987 ** psf->current = newblock * pg72x->samplesperblock + newsample ;
2988 ** return psf->current ;
2991 } /* au_g72x_seek */
2993 /*==========================================================================================
2994 ** G72x Write Functions.
2997 static int
2998 au_g72x_encode_block (SF_PRIVATE *psf, G72x_DATA *pg72x)
2999 { int k ;
3001 /* Encode the samples. */
3002 g72x_encode_block (pg72x) ;
3004 /* Write the block to disk. */
3005 if ((k = psf_fwrite (pg72x->block, 1, pg72x->blocksize, psf)) != pg72x->blocksize)
3006 psf_log_printf (psf, "*** Warning : short write (%d != %d).\n", k, pg72x->blocksize) ;
3008 pg72x->samplecount = 0 ;
3009 pg72x->blockcount ++ ;
3011 /* Set samples to zero for next block. */
3012 memset (pg72x->samples, 0, G72x_BLOCK_SIZE * sizeof (short)) ;
3014 return 1 ;
3015 } /* au_g72x_encode_block */
3017 static int
3018 au_g72x_write_block (SF_PRIVATE *psf, G72x_DATA *pg72x, short *ptr, int len)
3019 { int count, total = 0, indx = 0 ;
3021 while (indx < len)
3022 { count = pg72x->samplesperblock - pg72x->samplecount ;
3024 if (count > len - indx)
3025 count = len - indx ;
3027 memcpy (&(pg72x->samples [pg72x->samplecount]), &(ptr [indx]), count * sizeof (short)) ;
3028 indx += count ;
3029 pg72x->samplecount += count ;
3030 total = indx ;
3032 if (pg72x->samplecount >= pg72x->samplesperblock)
3033 au_g72x_encode_block (psf, pg72x) ;
3036 return total ;
3037 } /* au_g72x_write_block */
3039 static sf_count_t
3040 au_g72x_write_s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
3041 { G72x_DATA *pg72x ;
3042 int writecount, count ;
3043 sf_count_t total = 0 ;
3045 if (! psf->fdata)
3046 return 0 ;
3047 pg72x = (G72x_DATA*) psf->fdata ;
3049 while (len > 0)
3050 { writecount = (len > 0x10000000) ? 0x10000000 : (int) len ;
3052 count = au_g72x_write_block (psf, pg72x, ptr, writecount) ;
3054 total += count ;
3055 len -= count ;
3056 if (count != writecount)
3057 break ;
3060 return total ;
3061 } /* au_g72x_write_s */
3063 static sf_count_t
3064 au_g72x_write_i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
3065 { G72x_DATA *pg72x ;
3066 short *sptr ;
3067 int k, bufferlen, writecount = 0, count ;
3068 sf_count_t total = 0 ;
3070 if (! psf->fdata)
3071 return 0 ;
3072 pg72x = (G72x_DATA*) psf->fdata ;
3074 sptr = (short*) psf->buffer ;
3075 bufferlen = ((SF_BUFFER_LEN / psf->blockwidth) * psf->blockwidth) / sizeof (short) ;
3076 while (len > 0)
3077 { writecount = (len >= bufferlen) ? bufferlen : len ;
3078 for (k = 0 ; k < writecount ; k++)
3079 sptr [k] = ptr [total + k] >> 16 ;
3080 count = au_g72x_write_block (psf, pg72x, sptr, writecount) ;
3082 total += count ;
3083 len -= writecount ;
3084 if (count != writecount)
3085 break ;
3087 return total ;
3088 } /* au_g72x_write_i */
3090 static sf_count_t
3091 au_g72x_write_f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
3092 { G72x_DATA *pg72x ;
3093 short *sptr ;
3094 int k, bufferlen, writecount = 0, count ;
3095 sf_count_t total = 0 ;
3096 float normfact ;
3098 if (! psf->fdata)
3099 return 0 ;
3100 pg72x = (G72x_DATA*) psf->fdata ;
3102 normfact = (psf->norm_float == SF_TRUE) ? (1.0 * 0x8000) : 1.0 ;
3104 sptr = (short*) psf->buffer ;
3105 bufferlen = ((SF_BUFFER_LEN / psf->blockwidth) * psf->blockwidth) / sizeof (short) ;
3106 while (len > 0)
3107 { writecount = (len >= bufferlen) ? bufferlen : len ;
3108 for (k = 0 ; k < writecount ; k++)
3109 sptr [k] = lrintf (normfact * ptr [total + k]) ;
3110 count = au_g72x_write_block (psf, pg72x, sptr, writecount) ;
3112 total += count ;
3113 len -= writecount ;
3114 if (count != writecount)
3115 break ;
3118 return total ;
3119 } /* au_g72x_write_f */
3121 static sf_count_t
3122 au_g72x_write_d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
3123 { G72x_DATA *pg72x ;
3124 short *sptr ;
3125 int k, bufferlen, writecount = 0, count ;
3126 sf_count_t total = 0 ;
3127 double normfact ;
3129 if (! psf->fdata)
3130 return 0 ;
3131 pg72x = (G72x_DATA*) psf->fdata ;
3133 normfact = (psf->norm_double == SF_TRUE) ? (1.0 * 0x8000) : 1.0 ;
3135 sptr = (short*) psf->buffer ;
3136 bufferlen = ((SF_BUFFER_LEN / psf->blockwidth) * psf->blockwidth) / sizeof (short) ;
3137 while (len > 0)
3138 { writecount = (len >= bufferlen) ? bufferlen : len ;
3139 for (k = 0 ; k < writecount ; k++)
3140 sptr [k] = lrint (normfact * ptr [total + k]) ;
3141 count = au_g72x_write_block (psf, pg72x, sptr, writecount) ;
3143 total += count ;
3144 len -= writecount ;
3145 if (count != writecount)
3146 break ;
3149 return total ;
3150 } /* au_g72x_write_d */
3152 static int
3153 au_g72x_close (SF_PRIVATE *psf)
3154 { G72x_DATA *pg72x ;
3156 if (! psf->fdata)
3157 return 0 ;
3159 pg72x = (G72x_DATA*) psf->fdata ;
3161 if (psf->mode == SFM_WRITE)
3162 { /* If a block has been partially assembled, write it out
3163 ** as the final block.
3166 if (pg72x->samplecount && pg72x->samplecount < G72x_BLOCK_SIZE)
3167 au_g72x_encode_block (psf, pg72x) ;
3169 if (psf->write_header)
3170 psf->write_header (psf, SF_FALSE) ;
3173 return 0 ;
3174 } /* au_g72x_close */
3177 ** Do not edit or modify anything in this comment block.
3178 ** The arch-tag line is a file identity tag for the GNU Arch
3179 ** revision control system.
3181 ** arch-tag: 3cc5439e-7247-486b-b2e6-11a4affa5744
3184 ** Copyright (C) 2004 Erik de Castro Lopo <erikd@mega-nerd.com>
3186 ** This program is free software; you can redistribute it and/or modify
3187 ** it under the terms of the GNU Lesser General Public License as published by
3188 ** the Free Software Foundation; either version 2.1 of the License, or
3189 ** (at your option) any later version.
3191 ** This program is distributed in the hope that it will be useful,
3192 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
3193 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3194 ** GNU Lesser General Public License for more details.
3196 ** You should have received a copy of the GNU Lesser General Public License
3197 ** along with this program; if not, write to the Free Software
3198 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
3202 #include <stdio.h>
3203 #include <string.h>
3206 #define TWOBIT_MARKER (MAKE_MARKER ('2', 'B', 'I', 'T'))
3207 #define AVR_HDR_SIZE 128
3209 #define SFE_AVR_X 666
3212 ** From: hyc@hanauma.Jpl.Nasa.Gov (Howard Chu)
3214 ** A lot of PD software exists to play Mac .snd files on the ST. One other
3215 ** format that seems pretty popular (used by a number of commercial packages)
3216 ** is the AVR format (from Audio Visual Research). This format has a 128 byte
3217 ** header that looks like this (its actually packed, but thats not portable):
3220 typedef struct
3221 { int marker ; /* 2BIT */
3222 char name [8] ; /* null-padded sample name */
3223 short mono ; /* 0 = mono, 0xffff = stereo */
3224 short rez ; /* 8 = 8 bit, 16 = 16 bit */
3225 short sign ; /* 0 = unsigned, 0xffff = signed */
3227 short loop ; /* 0 = no loop, 0xffff = looping sample */
3228 short midi ; /* 0xffff = no MIDI note assigned, */
3229 /* 0xffXX = single key note assignment */
3230 /* 0xLLHH = key split, low/hi note */
3231 int srate ; /* sample frequency in hertz */
3232 int frames ; /* sample length in bytes or words (see rez) */
3233 int lbeg ; /* offset to start of loop in bytes or words. */
3234 /* set to zero if unused */
3235 int lend ; /* offset to end of loop in bytes or words. */
3236 /* set to sample length if unused */
3237 short res1 ; /* Reserved, MIDI keyboard split */
3238 short res2 ; /* Reserved, sample compression */
3239 short res3 ; /* Reserved */
3240 char ext [20] ; /* Additional filename space, used if (name[7] != 0) */
3241 char user [64] ; /* User defined. Typically ASCII message */
3242 } AVR_HEADER ;
3244 /*------------------------------------------------------------------------------
3245 ** Private static functions.
3248 static int avr_close (SF_PRIVATE *psf) ;
3250 static int avr_read_header (SF_PRIVATE *psf) ;
3251 static int avr_write_header (SF_PRIVATE *psf, int calc_length) ;
3253 /*------------------------------------------------------------------------------
3254 ** Public function.
3258 avr_open (SF_PRIVATE *psf)
3259 { int subformat ;
3260 int error = 0 ;
3262 if (psf->mode == SFM_READ || (psf->mode == SFM_RDWR && psf->filelength > 0))
3263 { if ((error = avr_read_header (psf)))
3264 return error ;
3267 if ((psf->sf.format & SF_FORMAT_TYPEMASK) != SF_FORMAT_AVR)
3268 return SFE_BAD_OPEN_FORMAT ;
3270 subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
3272 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
3273 { psf->endian = psf->sf.format & SF_FORMAT_ENDMASK ;
3274 psf->endian = SF_ENDIAN_BIG ;
3276 if (avr_write_header (psf, SF_FALSE))
3277 return psf->error ;
3279 psf->write_header = avr_write_header ;
3282 psf->close = avr_close ;
3284 psf->blockwidth = psf->bytewidth * psf->sf.channels ;
3286 error = pcm_init (psf) ;
3288 return error ;
3289 } /* avr_open */
3291 static int
3292 avr_read_header (SF_PRIVATE *psf)
3293 { AVR_HEADER hdr ;
3295 memset (&hdr, 0, sizeof (hdr)) ;
3297 psf_binheader_readf (psf, "pmb", 0, &hdr.marker, &hdr.name, sizeof (hdr.name)) ;
3298 psf_log_printf (psf, "%M\n", hdr.marker) ;
3300 if (hdr.marker != TWOBIT_MARKER)
3301 return SFE_AVR_X ;
3303 psf_log_printf (psf, " Name : %s\n", hdr.name) ;
3305 psf_binheader_readf (psf, "E22222", &hdr.mono, &hdr.rez, &hdr.sign, &hdr.loop, &hdr.midi) ;
3307 psf->sf.channels = (hdr.mono & 1) + 1 ;
3309 psf_log_printf (psf, " Channels : %d\n Bit width : %d\n Signed : %s\n",
3310 (hdr.mono & 1) + 1, hdr.rez, hdr.sign ? "yes" : "no") ;
3312 switch ((hdr.rez << 16) + (hdr.sign & 1))
3313 { case ((8 << 16) + 0) :
3314 psf->sf.format = SF_FORMAT_AVR | SF_FORMAT_PCM_U8 ;
3315 psf->bytewidth = 1 ;
3316 break ;
3318 case ((8 << 16) + 1) :
3319 psf->sf.format = SF_FORMAT_AVR | SF_FORMAT_PCM_S8 ;
3320 psf->bytewidth = 1 ;
3321 break ;
3323 case ((16 << 16) + 1) :
3324 psf->sf.format = SF_FORMAT_AVR | SF_FORMAT_PCM_16 ;
3325 psf->bytewidth = 2 ;
3326 break ;
3328 default :
3329 psf_log_printf (psf, "Error : bad rez/sign combination.\n") ;
3330 return SFE_AVR_X ;
3331 break ;
3334 psf_binheader_readf (psf, "E4444", &hdr.srate, &hdr.frames, &hdr.lbeg, &hdr.lend) ;
3336 psf->sf.frames = hdr.frames ;
3337 psf->sf.samplerate = hdr.srate ;
3339 psf_log_printf (psf, " Frames : %D\n", psf->sf.frames) ;
3340 psf_log_printf (psf, " Sample rate : %d\n", psf->sf.samplerate) ;
3342 psf_binheader_readf (psf, "E222", &hdr.res1, &hdr.res2, &hdr.res3) ;
3343 psf_binheader_readf (psf, "bb", hdr.ext, sizeof (hdr.ext), hdr.user, sizeof (hdr.user)) ;
3345 psf_log_printf (psf, " Ext : %s\n User : %s\n", hdr.ext, hdr.user) ;
3347 psf->endian = SF_ENDIAN_BIG ;
3349 psf->dataoffset = AVR_HDR_SIZE ;
3350 psf->datalength = hdr.frames * (hdr.rez / 8) ;
3352 if (psf->fileoffset > 0)
3353 psf->filelength = AVR_HDR_SIZE + psf->datalength ;
3355 if (psf_ftell (psf) != psf->dataoffset)
3356 psf_binheader_readf (psf, "j", psf->dataoffset - psf_ftell (psf)) ;
3358 psf->close = avr_close ;
3360 psf->blockwidth = psf->sf.channels * psf->bytewidth ;
3362 if (psf->sf.frames == 0 && psf->blockwidth)
3363 psf->sf.frames = (psf->filelength - psf->dataoffset) / psf->blockwidth ;
3365 return 0 ;
3366 } /* avr_read_header */
3368 static int
3369 avr_write_header (SF_PRIVATE *psf, int calc_length)
3370 { sf_count_t current ;
3371 int sign, datalength ;
3373 if (psf->pipeoffset > 0)
3374 return 0 ;
3376 current = psf_ftell (psf) ;
3378 if (calc_length)
3379 { psf->filelength = psf_get_filelen (psf) ;
3381 psf->datalength = psf->filelength - psf->dataoffset ;
3382 if (psf->dataend)
3383 psf->datalength -= psf->filelength - psf->dataend ;
3385 psf->sf.frames = psf->datalength / (psf->bytewidth * psf->sf.channels) ;
3388 /* Reset the current header length to zero. */
3389 psf->header [0] = 0 ;
3390 psf->headindex = 0 ;
3393 ** Only attempt to seek if we are not writng to a pipe. If we are
3394 ** writing to a pipe we shouldn't be here anyway.
3396 if (psf->is_pipe == SF_FALSE)
3397 psf_fseek (psf, 0, SEEK_SET) ;
3399 datalength = (int) (psf->datalength & 0x7FFFFFFF) ;
3401 psf_binheader_writef (psf, "Emz22", TWOBIT_MARKER, (size_t) 8,
3402 psf->sf.channels == 2 ? 0xFFFF : 0, psf->bytewidth * 8) ;
3404 sign = ((psf->sf.format & SF_FORMAT_SUBMASK) == SF_FORMAT_PCM_U8) ? 0 : 0xFFFF ;
3406 psf_binheader_writef (psf, "E222", sign, 0, 0xFFFF) ;
3407 psf_binheader_writef (psf, "E4444", psf->sf.samplerate, psf->sf.frames, 0, 0) ;
3409 psf_binheader_writef (psf, "E222zz", 0, 0, 0, (size_t) 20, (size_t) 64) ;
3411 /* Header construction complete so write it out. */
3412 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
3414 if (psf->error)
3415 return psf->error ;
3417 psf->dataoffset = psf->headindex ;
3419 if (current > 0)
3420 psf_fseek (psf, current, SEEK_SET) ;
3422 return psf->error ;
3423 } /* avr_write_header */
3425 static int
3426 avr_close (SF_PRIVATE *psf)
3428 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
3429 avr_write_header (psf, SF_TRUE) ;
3431 return 0 ;
3432 } /* avr_close */
3435 ** Do not edit or modify anything in this comment block.
3436 ** The arch-tag line is a file identity tag for the GNU Arch
3437 ** revision control system.
3439 ** arch-tag: 0823d454-f39a-4a28-a776-607f1ef33b52
3442 * Copyright 1992 by Jutta Degener and Carsten Bormann, Technische
3443 * Universitaet Berlin. See the accompanying file "COPYRIGHT" for
3444 * details. THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE.
3448 #include <stdlib.h>
3449 #include <string.h>
3454 * 4.2 FIXED POINT IMPLEMENTATION OF THE RPE-LTP CODER
3457 void Gsm_Coder (
3459 struct gsm_state * State,
3461 word * s, /* [0..159] samples IN */
3464 * The RPE-LTD coder works on a frame by frame basis. The length of
3465 * the frame is equal to 160 samples. Some computations are done
3466 * once per frame to produce at the output of the coder the
3467 * LARc[1..8] parameters which are the coded LAR coefficients and
3468 * also to realize the inverse filtering operation for the entire
3469 * frame (160 samples of signal d[0..159]). These parts produce at
3470 * the output of the coder:
3473 word * LARc, /* [0..7] LAR coefficients OUT */
3476 * Procedure 4.2.11 to 4.2.18 are to be executed four times per
3477 * frame. That means once for each sub-segment RPE-LTP analysis of
3478 * 40 samples. These parts produce at the output of the coder:
3481 word * Nc, /* [0..3] LTP lag OUT */
3482 word * bc, /* [0..3] coded LTP gain OUT */
3483 word * Mc, /* [0..3] RPE grid selection OUT */
3484 word * xmaxc,/* [0..3] Coded maximum amplitude OUT */
3485 word * xMc /* [13*4] normalized RPE samples OUT */
3488 int k;
3489 word * dp = State->dp0 + 120; /* [ -120...-1 ] */
3490 word * dpp = dp; /* [ 0...39 ] */
3492 word so[160];
3494 Gsm_Preprocess (State, s, so);
3495 Gsm_LPC_Analysis (State, so, LARc);
3496 Gsm_Short_Term_Analysis_Filter (State, LARc, so);
3498 for (k = 0; k <= 3; k++, xMc += 13) {
3500 Gsm_Long_Term_Predictor ( State,
3501 so+k*40, /* d [0..39] IN */
3502 dp, /* dp [-120..-1] IN */
3503 State->e + 5, /* e [0..39] OUT */
3504 dpp, /* dpp [0..39] OUT */
3505 Nc++,
3506 bc++);
3508 Gsm_RPE_Encoding ( /*-S,-*/
3509 State->e + 5, /* e ][0..39][ IN/OUT */
3510 xmaxc++, Mc++, xMc );
3512 * Gsm_Update_of_reconstructed_short_time_residual_signal
3513 * ( dpp, e + 5, dp );
3516 { register int i;
3517 for (i = 0; i <= 39; i++)
3518 dp[ i ] = GSM_ADD( State->e[5 + i], dpp[i] );
3520 dp += 40;
3521 dpp += 40;
3524 (void)memcpy( (char *)State->dp0, (char *)(State->dp0 + 160),
3525 120 * sizeof(*State->dp0) );
3528 ** Do not edit or modify anything in this comment block.
3529 ** The arch-tag line is a file identity tag for the GNU Arch
3530 ** revision control system.
3532 ** arch-tag: ae8ef1b2-5a1e-4263-94cd-42b15dca81a3
3536 ** Copyright (C) 2001-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
3538 ** This program is free software; you can redistribute it and/or modify
3539 ** it under the terms of the GNU Lesser General Public License as published by
3540 ** the Free Software Foundation; either version 2.1 of the License, or
3541 ** (at your option) any later version.
3543 ** This program is distributed in the hope that it will be useful,
3544 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
3545 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3546 ** GNU Lesser General Public License for more details.
3548 ** You should have received a copy of the GNU Lesser General Public License
3549 ** along with this program; if not, write to the Free Software
3550 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
3553 #include <stdio.h>
3554 #include <string.h>
3555 #include <math.h>
3558 static SF_FORMAT_INFO const simple_formats [] =
3560 { SF_FORMAT_AIFF | SF_FORMAT_PCM_16,
3561 "AIFF (Apple/SGI 16 bit PCM)", "aiff"
3564 { SF_FORMAT_AIFF | SF_FORMAT_FLOAT,
3565 "AIFF (Apple/SGI 32 bit float)", "aifc"
3568 { SF_FORMAT_AIFF | SF_FORMAT_PCM_S8,
3569 "AIFF (Apple/SGI 8 bit PCM)", "aiff"
3572 { SF_FORMAT_AU | SF_FORMAT_PCM_16,
3573 "AU (Sun/Next 16 bit PCM)", "au"
3576 { SF_FORMAT_AU | SF_FORMAT_ULAW,
3577 "AU (Sun/Next 8-bit u-law)", "au"
3580 { SF_FORMAT_RAW | SF_FORMAT_VOX_ADPCM,
3581 "OKI Dialogic VOX ADPCM", "vox"
3584 { SF_FORMAT_WAV | SF_FORMAT_PCM_16,
3585 "WAV (Microsoft 16 bit PCM)", "wav"
3588 { SF_FORMAT_WAV | SF_FORMAT_FLOAT,
3589 "WAV (Microsoft 32 bit float)", "wav"
3592 { SF_FORMAT_WAV | SF_FORMAT_IMA_ADPCM,
3593 "WAV (Microsoft 4 bit IMA ADPCM)", "wav"
3596 { SF_FORMAT_WAV | SF_FORMAT_MS_ADPCM,
3597 "WAV (Microsoft 4 bit MS ADPCM)", "wav"
3600 { SF_FORMAT_WAV | SF_FORMAT_PCM_U8,
3601 "WAV (Microsoft 8 bit PCM)", "wav"
3604 } ; /* simple_formats */
3607 psf_get_format_simple_count (void)
3608 { return (sizeof (simple_formats) / sizeof (SF_FORMAT_INFO)) ;
3609 } /* psf_get_format_simple_count */
3612 psf_get_format_simple (SF_FORMAT_INFO *data)
3613 { int indx ;
3615 if (data->format < 0 || data->format >= (SIGNED_SIZEOF (simple_formats) / SIGNED_SIZEOF (SF_FORMAT_INFO)))
3616 return SFE_BAD_CONTROL_CMD ;
3618 indx = data->format ;
3619 memcpy (data, &(simple_formats [indx]), SIGNED_SIZEOF (SF_FORMAT_INFO)) ;
3621 return 0 ;
3622 } /* psf_get_format_simple */
3624 /*============================================================================
3625 ** Major format info.
3628 static SF_FORMAT_INFO const major_formats [] =
3630 { SF_FORMAT_AIFF, "AIFF (Apple/SGI)", "aiff" },
3631 { SF_FORMAT_AU, "AU (Sun/NeXT)", "au" },
3632 { SF_FORMAT_AVR, "AVR (Audio Visual Research)", "avr" },
3633 { SF_FORMAT_HTK, "HTK (HMM Tool Kit)", "htk" },
3634 { SF_FORMAT_SVX, "IFF (Amiga IFF/SVX8/SV16)", "iff" },
3635 { SF_FORMAT_MAT4, "MAT4 (GNU Octave 2.0 / Matlab 4.2)", "mat" },
3636 { SF_FORMAT_MAT5, "MAT5 (GNU Octave 2.1 / Matlab 5.0)", "mat" },
3637 { SF_FORMAT_PAF, "PAF (Ensoniq PARIS)", "paf" },
3638 { SF_FORMAT_PVF, "PVF (Portable Voice Format)", "pvf" },
3639 { SF_FORMAT_RAW, "RAW (header-less)", "raw" },
3640 { SF_FORMAT_SDS, "SDS (Midi Sample Dump Standard)", "sds" },
3641 /* Not ready for mainstream use yet.
3642 { SF_FORMAT_SD2, "SD2 (Sound Designer II)", "sd2" },
3644 { SF_FORMAT_IRCAM, "SF (Berkeley/IRCAM/CARL)", "sf" },
3645 { SF_FORMAT_VOC, "VOC (Creative Labs)", "voc" },
3646 { SF_FORMAT_W64, "W64 (SoundFoundry WAVE 64)", "w64" },
3647 { SF_FORMAT_WAV, "WAV (Microsoft)", "wav" },
3648 { SF_FORMAT_NIST, "WAV (NIST Sphere)", "wav" },
3649 { SF_FORMAT_WAVEX, "WAVEX (Microsoft)", "wav" },
3650 { SF_FORMAT_XI, "XI (FastTracker 2)", "xi" },
3652 } ; /* major_formats */
3655 psf_get_format_major_count (void)
3656 { return (sizeof (major_formats) / sizeof (SF_FORMAT_INFO)) ;
3657 } /* psf_get_format_major_count */
3660 psf_get_format_major (SF_FORMAT_INFO *data)
3661 { int indx ;
3663 if (data->format < 0 || data->format >= (SIGNED_SIZEOF (major_formats) / SIGNED_SIZEOF (SF_FORMAT_INFO)))
3664 return SFE_BAD_CONTROL_CMD ;
3666 indx = data->format ;
3667 memcpy (data, &(major_formats [indx]), SIGNED_SIZEOF (SF_FORMAT_INFO)) ;
3669 return 0 ;
3670 } /* psf_get_format_major */
3672 /*============================================================================
3673 ** Subtype format info.
3676 static SF_FORMAT_INFO subtype_formats [] =
3678 { SF_FORMAT_PCM_S8, "Signed 8 bit PCM", NULL },
3679 { SF_FORMAT_PCM_16, "Signed 16 bit PCM", NULL },
3680 { SF_FORMAT_PCM_24, "Signed 24 bit PCM", NULL },
3681 { SF_FORMAT_PCM_32, "Signed 32 bit PCM", NULL },
3683 { SF_FORMAT_PCM_U8, "Unsigned 8 bit PCM", NULL },
3685 { SF_FORMAT_FLOAT, "32 bit float", NULL },
3686 { SF_FORMAT_DOUBLE, "64 bit float", NULL },
3688 { SF_FORMAT_ULAW, "U-Law", NULL },
3689 { SF_FORMAT_ALAW, "A-Law", NULL },
3690 { SF_FORMAT_IMA_ADPCM, "IMA ADPCM", NULL },
3691 { SF_FORMAT_MS_ADPCM, "Microsoft ADPCM", NULL },
3693 { SF_FORMAT_GSM610, "GSM 6.10", NULL },
3695 { SF_FORMAT_G721_32, "32kbs G721 ADPCM", NULL },
3696 { SF_FORMAT_G723_24, "24kbs G723 ADPCM", NULL },
3698 { SF_FORMAT_DWVW_12, "12 bit DWVW", NULL },
3699 { SF_FORMAT_DWVW_16, "16 bit DWVW", NULL },
3700 { SF_FORMAT_DWVW_24, "24 bit DWVW", NULL },
3701 { SF_FORMAT_VOX_ADPCM, "VOX ADPCM", "vox" },
3703 { SF_FORMAT_DPCM_16, "16 bit DPCM", NULL },
3704 { SF_FORMAT_DPCM_8, "8 bit DPCM", NULL },
3705 } ; /* subtype_formats */
3708 psf_get_format_subtype_count (void)
3709 { return (sizeof (subtype_formats) / sizeof (SF_FORMAT_INFO)) ;
3710 } /* psf_get_format_subtype_count */
3713 psf_get_format_subtype (SF_FORMAT_INFO *data)
3714 { int indx ;
3716 if (data->format < 0 || data->format >= (SIGNED_SIZEOF (subtype_formats) / SIGNED_SIZEOF (SF_FORMAT_INFO)))
3717 return SFE_BAD_CONTROL_CMD ;
3719 indx = data->format ;
3720 memcpy (data, &(subtype_formats [indx]), sizeof (SF_FORMAT_INFO)) ;
3722 return 0 ;
3723 } /* psf_get_format_subtype */
3725 /*==============================================================================
3729 psf_get_format_info (SF_FORMAT_INFO *data)
3730 { int k, format ;
3732 if (data->format & SF_FORMAT_TYPEMASK)
3733 { format = data->format & SF_FORMAT_TYPEMASK ;
3735 for (k = 0 ; k < (SIGNED_SIZEOF (major_formats) / SIGNED_SIZEOF (SF_FORMAT_INFO)) ; k++)
3736 { if (format == major_formats [k].format)
3737 { memcpy (data, &(major_formats [k]), sizeof (SF_FORMAT_INFO)) ;
3738 return 0 ;
3742 else if (data->format & SF_FORMAT_SUBMASK)
3743 { format = data->format & SF_FORMAT_SUBMASK ;
3745 for (k = 0 ; k < (SIGNED_SIZEOF (subtype_formats) / SIGNED_SIZEOF (SF_FORMAT_INFO)) ; k++)
3746 { if (format == subtype_formats [k].format)
3747 { memcpy (data, &(subtype_formats [k]), sizeof (SF_FORMAT_INFO)) ;
3748 return 0 ;
3753 memset (data, 0, sizeof (SF_FORMAT_INFO)) ;
3755 return SFE_BAD_CONTROL_CMD ;
3756 } /* psf_get_format_info */
3758 /*==============================================================================
3761 double
3762 psf_calc_signal_max (SF_PRIVATE *psf, int normalize)
3763 { sf_count_t position ;
3764 double max_val = 0.0, temp, *data ;
3765 int k, len, readcount, save_state ;
3767 /* If the file is not seekable, there is nothing we can do. */
3768 if (! psf->sf.seekable)
3769 { psf->error = SFE_NOT_SEEKABLE ;
3770 return 0.0 ;
3773 if (! psf->read_double)
3774 { psf->error = SFE_UNIMPLEMENTED ;
3775 return 0.0 ;
3778 save_state = sf_command ((SNDFILE*) psf, SFC_GET_NORM_DOUBLE, NULL, 0) ;
3780 sf_command ((SNDFILE*) psf, SFC_SET_NORM_DOUBLE, NULL, normalize) ;
3782 /* Brute force. Read the whole file and find the biggest sample. */
3783 position = sf_seek ((SNDFILE*) psf, 0, SEEK_CUR) ; /* Get current position in file */
3784 sf_seek ((SNDFILE*) psf, 0, SEEK_SET) ; /* Go to start of file. */
3786 len = sizeof (psf->buffer) / sizeof (double) ;
3788 data = (double*) psf->buffer ;
3790 readcount = len ;
3791 while (readcount > 0)
3792 { readcount = sf_read_double ((SNDFILE*) psf, data, len) ;
3793 for (k = 0 ; k < readcount ; k++)
3794 { temp = fabs (data [k]) ;
3795 max_val = temp > max_val ? temp : max_val ;
3799 sf_seek ((SNDFILE*) psf, position, SEEK_SET) ; /* Return to original position. */
3801 sf_command ((SNDFILE*) psf, SFC_SET_NORM_DOUBLE, NULL, save_state) ;
3803 return max_val ;
3804 } /* psf_calc_signal_max */
3807 psf_calc_max_all_channels (SF_PRIVATE *psf, double *peaks, int normalize)
3808 { sf_count_t position ;
3809 double temp, *data ;
3810 int k, len, readcount, save_state ;
3811 int chan ;
3813 /* If the file is not seekable, there is nothing we can do. */
3814 if (! psf->sf.seekable)
3815 return (psf->error = SFE_NOT_SEEKABLE) ;
3817 if (! psf->read_double)
3818 return (psf->error = SFE_UNIMPLEMENTED) ;
3820 save_state = sf_command ((SNDFILE*) psf, SFC_GET_NORM_DOUBLE, NULL, 0) ;
3821 sf_command ((SNDFILE*) psf, SFC_SET_NORM_DOUBLE, NULL, normalize) ;
3823 memset (peaks, 0, sizeof (double) * psf->sf.channels) ;
3825 /* Brute force. Read the whole file and find the biggest sample for each channel. */
3826 position = sf_seek ((SNDFILE*) psf, 0, SEEK_CUR) ; /* Get current position in file */
3827 sf_seek ((SNDFILE*) psf, 0, SEEK_SET) ; /* Go to start of file. */
3829 len = sizeof (psf->buffer) / sizeof (double) ;
3831 data = (double*) psf->buffer ;
3833 chan = 0 ;
3834 readcount = len ;
3835 while (readcount > 0)
3836 { readcount = sf_read_double ((SNDFILE*) psf, data, len) ;
3837 for (k = 0 ; k < readcount ; k++)
3838 { temp = fabs (data [k]) ;
3839 peaks [chan] = temp > peaks [chan] ? temp : peaks [chan] ;
3840 chan = (chan + 1) % psf->sf.channels ;
3844 sf_seek ((SNDFILE*) psf, position, SEEK_SET) ; /* Return to original position. */
3846 sf_command ((SNDFILE*) psf, SFC_SET_NORM_DOUBLE, NULL, save_state) ;
3848 return 0 ;
3849 } /* psf_calc_signal_max */
3852 ** Do not edit or modify anything in this comment block.
3853 ** The arch-tag line is a file identity tag for the GNU Arch
3854 ** revision control system.
3856 ** arch-tag: 0aae0d9d-ab2b-4d70-ade3-47a534666f8e
3859 ** Copyright (C) 1999-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
3861 ** This program is free software; you can redistribute it and/or modify
3862 ** it under the terms of the GNU Lesser General Public License as published by
3863 ** the Free Software Foundation; either version 2.1 of the License, or
3864 ** (at your option) any later version.
3866 ** This program is distributed in the hope that it will be useful,
3867 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
3868 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3869 ** GNU Lesser General Public License for more details.
3871 ** You should have received a copy of the GNU Lesser General Public License
3872 ** along with this program; if not, write to the Free Software
3873 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
3876 #include <stdarg.h>
3877 #include <string.h>
3878 #include <ctype.h>
3879 #include <math.h>
3880 #include <time.h>
3883 /*-----------------------------------------------------------------------------------------------
3884 ** psf_log_printf allows libsndfile internal functions to print to an internal logbuffer which
3885 ** can later be displayed.
3886 ** The format specifiers are as for printf but without the field width and other modifiers.
3887 ** Printing is performed to the logbuffer char array of the SF_PRIVATE struct.
3888 ** Printing is done in such a way as to guarantee that the log never overflows the end of the
3889 ** logbuffer array.
3892 #define LOG_PUTCHAR(a,b) \
3893 { if ((a)->logindex < SIGNED_SIZEOF ((a)->logbuffer) - 1)\
3894 { (a)->logbuffer [(a)->logindex++] = (b) ; \
3895 (a)->logbuffer [(a)->logindex] = 0 ; \
3899 void
3900 psf_log_printf (SF_PRIVATE *psf, const char *format, ...)
3901 { va_list ap ;
3902 unsigned int u ;
3903 int d, tens, shift, width, width_specifier, left_align ;
3904 char c, *strptr, istr [5], lead_char, sign_char ;
3906 va_start (ap, format) ;
3908 while ((c = *format++))
3909 { if (c != '%')
3910 { LOG_PUTCHAR (psf, c) ;
3911 continue ;
3914 if (format [0] == '%') /* Handle %% */
3915 { LOG_PUTCHAR (psf, '%') ;
3916 format ++ ;
3917 continue ;
3920 sign_char = 0 ;
3921 left_align = SF_FALSE ;
3922 while (1)
3923 { switch (format [0])
3924 { case ' ' :
3925 case '+' :
3926 sign_char = format [0] ;
3927 format ++ ;
3928 continue ;
3930 case '-' :
3931 left_align = SF_TRUE ;
3932 format ++ ;
3933 continue ;
3935 default : break ;
3938 break ;
3941 if (format [0] == 0)
3942 break ;
3944 lead_char = ' ' ;
3945 if (format [0] == '0')
3946 lead_char = '0' ;
3948 width_specifier = 0 ;
3949 while ((c = *format++) && isdigit (c))
3950 width_specifier = width_specifier * 10 + (c - '0') ;
3952 switch (c)
3953 { case 0 : /* NULL character. */
3954 va_end (ap) ;
3955 return ;
3957 case 's': /* string */
3958 strptr = va_arg (ap, char *) ;
3959 if (strptr == NULL)
3960 break ;
3961 width_specifier -= strlen (strptr) ;
3962 if (left_align == SF_FALSE)
3963 while (width_specifier -- > 0)
3964 LOG_PUTCHAR (psf, ' ') ;
3965 while (*strptr)
3966 LOG_PUTCHAR (psf, *strptr++) ;
3967 while (width_specifier -- > 0)
3968 LOG_PUTCHAR (psf, ' ') ;
3969 break ;
3971 case 'd': /* int */
3972 d = va_arg (ap, int) ;
3974 if (d < 0)
3975 { d = -d ;
3976 sign_char = '-' ;
3977 if (lead_char != '0' && left_align == SF_FALSE)
3978 width_specifier -- ;
3981 tens = 1 ;
3982 width = 1 ;
3983 while (d / tens >= 10)
3984 { tens *= 10 ;
3985 width ++ ;
3988 width_specifier -= width ;
3990 if (sign_char == ' ')
3991 { LOG_PUTCHAR (psf, ' ') ;
3992 width_specifier -- ;
3995 if (left_align == SF_FALSE && lead_char != '0')
3996 { if (sign_char == '+')
3997 width_specifier -- ;
3999 while (width_specifier -- > 0)
4000 LOG_PUTCHAR (psf, lead_char) ;
4003 if (sign_char == '+' || sign_char == '-')
4004 { LOG_PUTCHAR (psf, sign_char) ;
4005 width_specifier -- ;
4008 if (left_align == SF_FALSE)
4009 while (width_specifier -- > 0)
4010 LOG_PUTCHAR (psf, lead_char) ;
4012 while (tens > 0)
4013 { LOG_PUTCHAR (psf, '0' + d / tens) ;
4014 d %= tens ;
4015 tens /= 10 ;
4018 while (width_specifier -- > 0)
4019 LOG_PUTCHAR (psf, lead_char) ;
4020 break ;
4022 case 'D': /* sf_count_t */
4023 { sf_count_t D, Tens ;
4025 D = va_arg (ap, sf_count_t) ;
4027 if (D == 0)
4028 { while (-- width_specifier > 0)
4029 LOG_PUTCHAR (psf, lead_char) ;
4030 LOG_PUTCHAR (psf, '0') ;
4031 break ;
4033 if (D < 0)
4034 { LOG_PUTCHAR (psf, '-') ;
4035 D = -D ;
4037 Tens = 1 ;
4038 width = 1 ;
4039 while (D / Tens >= 10)
4040 { Tens *= 10 ;
4041 width ++ ;
4044 while (width_specifier > width)
4045 { LOG_PUTCHAR (psf, lead_char) ;
4046 width_specifier-- ;
4049 while (Tens > 0)
4050 { LOG_PUTCHAR (psf, '0' + D / Tens) ;
4051 D %= Tens ;
4052 Tens /= 10 ;
4055 break ;
4057 case 'u': /* unsigned int */
4058 u = va_arg (ap, unsigned int) ;
4060 tens = 1 ;
4061 width = 1 ;
4062 while (u / tens >= 10)
4063 { tens *= 10 ;
4064 width ++ ;
4067 width_specifier -= width ;
4069 if (sign_char == ' ')
4070 { LOG_PUTCHAR (psf, ' ') ;
4071 width_specifier -- ;
4074 if (left_align == SF_FALSE && lead_char != '0')
4075 { if (sign_char == '+')
4076 width_specifier -- ;
4078 while (width_specifier -- > 0)
4079 LOG_PUTCHAR (psf, lead_char) ;
4082 if (sign_char == '+' || sign_char == '-')
4083 { LOG_PUTCHAR (psf, sign_char) ;
4084 width_specifier -- ;
4087 if (left_align == SF_FALSE)
4088 while (width_specifier -- > 0)
4089 LOG_PUTCHAR (psf, lead_char) ;
4091 while (tens > 0)
4092 { LOG_PUTCHAR (psf, '0' + u / tens) ;
4093 u %= tens ;
4094 tens /= 10 ;
4097 while (width_specifier -- > 0)
4098 LOG_PUTCHAR (psf, lead_char) ;
4099 break ;
4101 case 'c': /* char */
4102 c = va_arg (ap, int) & 0xFF ;
4103 LOG_PUTCHAR (psf, c) ;
4104 break ;
4106 case 'X': /* hex */
4107 d = va_arg (ap, int) ;
4109 if (d == 0)
4110 { while (--width_specifier > 0)
4111 LOG_PUTCHAR (psf, lead_char) ;
4112 LOG_PUTCHAR (psf, '0') ;
4113 break ;
4115 shift = 28 ;
4116 width = (width_specifier < 8) ? 8 : width_specifier ;
4117 while (! ((0xF << shift) & d))
4118 { shift -= 4 ;
4119 width -- ;
4122 while (width > 0 && width_specifier > width)
4123 { LOG_PUTCHAR (psf, lead_char) ;
4124 width_specifier-- ;
4127 while (shift >= 0)
4128 { c = (d >> shift) & 0xF ;
4129 LOG_PUTCHAR (psf, (c > 9) ? c + 'A' - 10 : c + '0') ;
4130 shift -= 4 ;
4132 break ;
4134 case 'M': /* int2str */
4135 d = va_arg (ap, int) ;
4136 if (CPU_IS_LITTLE_ENDIAN)
4137 { istr [0] = d & 0xFF ;
4138 istr [1] = (d >> 8) & 0xFF ;
4139 istr [2] = (d >> 16) & 0xFF ;
4140 istr [3] = (d >> 24) & 0xFF ;
4142 else
4143 { istr [3] = d & 0xFF ;
4144 istr [2] = (d >> 8) & 0xFF ;
4145 istr [1] = (d >> 16) & 0xFF ;
4146 istr [0] = (d >> 24) & 0xFF ;
4148 istr [4] = 0 ;
4149 strptr = istr ;
4150 while (*strptr)
4151 { c = *strptr++ ;
4152 LOG_PUTCHAR (psf, c) ;
4154 break ;
4156 default :
4157 LOG_PUTCHAR (psf, '*') ;
4158 LOG_PUTCHAR (psf, c) ;
4159 LOG_PUTCHAR (psf, '*') ;
4160 break ;
4161 } /* switch */
4162 } /* while */
4164 va_end (ap) ;
4165 return ;
4166 } /* psf_log_printf */
4168 #ifndef PSF_LOG_PRINTF_ONLY
4169 /*-----------------------------------------------------------------------------------------------
4170 ** ASCII header printf functions.
4171 ** Some formats (ie NIST) use ascii text in their headers.
4172 ** Format specifiers are the same as the standard printf specifiers (uses vsnprintf).
4173 ** If this generates a compile error on any system, the author should be notified
4174 ** so an alternative vsnprintf can be provided.
4177 void
4178 psf_asciiheader_printf (SF_PRIVATE *psf, const char *format, ...)
4179 { va_list argptr ;
4180 int maxlen ;
4181 char *start ;
4183 maxlen = strlen ((char*) psf->header) ;
4184 start = ((char*) psf->header) + maxlen ;
4185 maxlen = sizeof (psf->header) - maxlen ;
4187 va_start (argptr, format) ;
4188 LSF_VSNPRINTF (start, maxlen, format, argptr) ;
4189 va_end (argptr) ;
4191 /* Make sure the string is properly terminated. */
4192 start [maxlen - 1] = 0 ;
4194 psf->headindex = strlen ((char*) psf->header) ;
4196 return ;
4197 } /* psf_asciiheader_printf */
4199 /*-----------------------------------------------------------------------------------------------
4200 ** Binary header writing functions. Returns number of bytes written.
4202 ** Format specifiers for psf_binheader_writef are as follows
4203 ** m - marker - four bytes - no endian manipulation
4205 ** e - all following numerical values will be little endian
4206 ** E - all following numerical values will be big endian
4208 ** t - all following O types will be truncated to 4 bytes
4209 ** T - switch off truncation of all following O types
4211 ** 1 - single byte value
4212 ** 2 - two byte value
4213 ** 3 - three byte value
4214 ** 4 - four byte value
4215 ** 8 - eight byte value (sometimes written as 4 bytes)
4217 ** s - string preceded by a four byte length
4218 ** S - string including null terminator
4219 ** f - floating point data
4220 ** d - double precision floating point data
4221 ** h - 16 binary bytes value
4223 ** b - binary data (see below)
4224 ** z - zero bytes (ses below)
4225 ** j - jump forwards or backwards
4227 ** To write a word followed by an int (both little endian) use:
4228 ** psf_binheader_writef ("e24", wordval, longval) ;
4230 ** To write binary data use:
4231 ** psf_binheader_writef ("b", &bindata, sizeof (bindata)) ;
4233 ** To write N zero bytes use:
4234 ** psf_binheader_writef ("z", N) ;
4237 /* These macros may seem a bit messy but do prevent problems with processors which
4238 ** seg. fault when asked to write an int or short to a non-int/short aligned address.
4241 #define PUT_BYTE(psf,x) if ((psf)->headindex < SIGNED_SIZEOF ((psf)->header) - 1) \
4242 { (psf)->header [(psf)->headindex++] = (x) ; }
4244 #if (CPU_IS_BIG_ENDIAN == 1)
4245 #define PUT_MARKER(psf,x) if ((psf)->headindex < SIGNED_SIZEOF ((psf)->header) - 4) \
4246 { (psf)->header [(psf)->headindex++] = ((x) >> 24) ; \
4247 (psf)->header [(psf)->headindex++] = ((x) >> 16) ; \
4248 (psf)->header [(psf)->headindex++] = ((x) >> 8) ; \
4249 (psf)->header [(psf)->headindex++] = (x) ; }
4251 #elif (CPU_IS_LITTLE_ENDIAN == 1)
4252 #define PUT_MARKER(psf,x) if ((psf)->headindex < SIGNED_SIZEOF ((psf)->header) - 4) \
4253 { (psf)->header [(psf)->headindex++] = (x) ; \
4254 (psf)->header [(psf)->headindex++] = ((x) >> 8) ; \
4255 (psf)->header [(psf)->headindex++] = ((x) >> 16) ; \
4256 (psf)->header [(psf)->headindex++] = ((x) >> 24) ; }
4258 #else
4259 # error "Cannot determine endian-ness of processor."
4260 #endif
4263 #define PUT_BE_SHORT(psf,x) if ((psf)->headindex < SIGNED_SIZEOF ((psf)->header) - 2) \
4264 { (psf)->header [(psf)->headindex++] = ((x) >> 8) ; \
4265 (psf)->header [(psf)->headindex++] = (x) ; }
4267 #define PUT_LE_SHORT(psf,x) if ((psf)->headindex < SIGNED_SIZEOF ((psf)->header) - 2) \
4268 { (psf)->header [(psf)->headindex++] = (x) ; \
4269 (psf)->header [(psf)->headindex++] = ((x) >> 8) ; }
4271 #define PUT_BE_3BYTE(psf,x) if ((psf)->headindex < SIGNED_SIZEOF ((psf)->header) - 3) \
4272 { (psf)->header [(psf)->headindex++] = ((x) >> 16) ; \
4273 (psf)->header [(psf)->headindex++] = ((x) >> 8) ; \
4274 (psf)->header [(psf)->headindex++] = (x) ; }
4276 #define PUT_LE_3BYTE(psf,x) if ((psf)->headindex < SIGNED_SIZEOF ((psf)->header) - 3) \
4277 { (psf)->header [(psf)->headindex++] = (x) ; \
4278 (psf)->header [(psf)->headindex++] = ((x) >> 8) ; \
4279 (psf)->header [(psf)->headindex++] = ((x) >> 16) ; }
4281 #define PUT_BE_INT(psf,x) if ((psf)->headindex < SIGNED_SIZEOF ((psf)->header) - 4) \
4282 { (psf)->header [(psf)->headindex++] = ((x) >> 24) ; \
4283 (psf)->header [(psf)->headindex++] = ((x) >> 16) ; \
4284 (psf)->header [(psf)->headindex++] = ((x) >> 8) ; \
4285 (psf)->header [(psf)->headindex++] = (x) ; }
4287 #define PUT_LE_INT(psf,x) if ((psf)->headindex < SIGNED_SIZEOF ((psf)->header) - 4) \
4288 { (psf)->header [(psf)->headindex++] = (x) ; \
4289 (psf)->header [(psf)->headindex++] = ((x) >> 8) ; \
4290 (psf)->header [(psf)->headindex++] = ((x) >> 16) ; \
4291 (psf)->header [(psf)->headindex++] = ((x) >> 24) ; }
4293 #if (SIZEOF_SF_COUNT_T == 4)
4294 #define PUT_BE_8BYTE(psf,x) if ((psf)->headindex < SIGNED_SIZEOF ((psf)->header) - 8) \
4295 { (psf)->header [(psf)->headindex++] = 0 ; \
4296 (psf)->header [(psf)->headindex++] = 0 ; \
4297 (psf)->header [(psf)->headindex++] = 0 ; \
4298 (psf)->header [(psf)->headindex++] = 0 ; \
4299 (psf)->header [(psf)->headindex++] = ((x) >> 24) ; \
4300 (psf)->header [(psf)->headindex++] = ((x) >> 16) ; \
4301 (psf)->header [(psf)->headindex++] = ((x) >> 8) ; \
4302 (psf)->header [(psf)->headindex++] = (x) ; }
4304 #define PUT_LE_8BYTE(psf,x) if ((psf)->headindex < SIGNED_SIZEOF ((psf)->header) - 8) \
4305 { (psf)->header [(psf)->headindex++] = (x) ; \
4306 (psf)->header [(psf)->headindex++] = ((x) >> 8) ; \
4307 (psf)->header [(psf)->headindex++] = ((x) >> 16) ; \
4308 (psf)->header [(psf)->headindex++] = ((x) >> 24) ; \
4309 (psf)->header [(psf)->headindex++] = 0 ; \
4310 (psf)->header [(psf)->headindex++] = 0 ; \
4311 (psf)->header [(psf)->headindex++] = 0 ; \
4312 (psf)->header [(psf)->headindex++] = 0 ; }
4314 #elif (SIZEOF_SF_COUNT_T == 8)
4315 #define PUT_BE_8BYTE(psf,x) if ((psf)->headindex < SIGNED_SIZEOF ((psf)->header) - 8) \
4316 { (psf)->header [(psf)->headindex++] = ((x) >> 56) ; \
4317 (psf)->header [(psf)->headindex++] = ((x) >> 48) ; \
4318 (psf)->header [(psf)->headindex++] = ((x) >> 40) ; \
4319 (psf)->header [(psf)->headindex++] = ((x) >> 32) ; \
4320 (psf)->header [(psf)->headindex++] = ((x) >> 24) ; \
4321 (psf)->header [(psf)->headindex++] = ((x) >> 16) ; \
4322 (psf)->header [(psf)->headindex++] = ((x) >> 8) ; \
4323 (psf)->header [(psf)->headindex++] = (x) ; }
4325 #define PUT_LE_8BYTE(psf,x) if ((psf)->headindex < SIGNED_SIZEOF ((psf)->header) - 8) \
4326 { (psf)->header [(psf)->headindex++] = (x) ; \
4327 (psf)->header [(psf)->headindex++] = ((x) >> 8) ; \
4328 (psf)->header [(psf)->headindex++] = ((x) >> 16) ; \
4329 (psf)->header [(psf)->headindex++] = ((x) >> 24) ; \
4330 (psf)->header [(psf)->headindex++] = ((x) >> 32) ; \
4331 (psf)->header [(psf)->headindex++] = ((x) >> 40) ; \
4332 (psf)->header [(psf)->headindex++] = ((x) >> 48) ; \
4333 (psf)->header [(psf)->headindex++] = ((x) >> 56) ; }
4334 #else
4335 #error "SIZEOF_SF_COUNT_T is not defined."
4336 #endif
4339 psf_binheader_writef (SF_PRIVATE *psf, const char *format, ...)
4340 { va_list argptr ;
4341 sf_count_t countdata ;
4342 unsigned long longdata ;
4343 unsigned int data ;
4344 float floatdata ;
4345 double doubledata ;
4346 void *bindata ;
4347 size_t size ;
4348 char c, *strptr ;
4349 int count = 0, trunc_8to4 ;
4351 trunc_8to4 = SF_FALSE ;
4353 va_start (argptr, format) ;
4355 while ((c = *format++))
4356 { switch (c)
4357 { case 'e' : /* All conversions are now from LE to host. */
4358 psf->rwf_endian = SF_ENDIAN_LITTLE ;
4359 break ;
4361 case 'E' : /* All conversions are now from BE to host. */
4362 psf->rwf_endian = SF_ENDIAN_BIG ;
4363 break ;
4365 case 't' : /* All 8 byte values now get written as 4 bytes. */
4366 trunc_8to4 = SF_TRUE ;
4367 break ;
4369 case 'T' : /* All 8 byte values now get written as 8 bytes. */
4370 trunc_8to4 = SF_FALSE ;
4371 break ;
4373 case 'm' :
4374 data = va_arg (argptr, unsigned int) ;
4375 PUT_MARKER (psf, data) ;
4376 count += 4 ;
4377 break ;
4379 case '1' :
4380 data = va_arg (argptr, unsigned int) ;
4381 PUT_BYTE (psf, data) ;
4382 count += 1 ;
4383 break ;
4385 case '2' :
4386 data = va_arg (argptr, unsigned int) ;
4387 if (psf->rwf_endian == SF_ENDIAN_BIG)
4388 { PUT_BE_SHORT (psf, data) ;
4390 else
4391 { PUT_LE_SHORT (psf, data) ;
4393 count += 2 ;
4394 break ;
4396 case '3' : /* tribyte */
4397 data = va_arg (argptr, unsigned int) ;
4398 if (psf->rwf_endian == SF_ENDIAN_BIG)
4399 { PUT_BE_3BYTE (psf, data) ;
4401 else
4402 { PUT_LE_3BYTE (psf, data) ;
4404 count += 3 ;
4405 break ;
4407 case '4' :
4408 data = va_arg (argptr, unsigned int) ;
4409 if (psf->rwf_endian == SF_ENDIAN_BIG)
4410 { PUT_BE_INT (psf, data) ;
4412 else
4413 { PUT_LE_INT (psf, data) ;
4415 count += 4 ;
4416 break ;
4418 case '8' :
4419 countdata = va_arg (argptr, sf_count_t) ;
4420 if (psf->rwf_endian == SF_ENDIAN_BIG && trunc_8to4 == SF_FALSE)
4421 { PUT_BE_8BYTE (psf, countdata) ;
4422 count += 8 ;
4424 else if (psf->rwf_endian == SF_ENDIAN_LITTLE && trunc_8to4 == SF_FALSE)
4425 { PUT_LE_8BYTE (psf, countdata) ;
4426 count += 8 ;
4428 else if (psf->rwf_endian == SF_ENDIAN_BIG && trunc_8to4 == SF_TRUE)
4429 { longdata = countdata & 0xFFFFFFFF ;
4430 PUT_BE_INT (psf, longdata) ;
4431 count += 4 ;
4433 else if (psf->rwf_endian == SF_ENDIAN_LITTLE && trunc_8to4 == SF_TRUE)
4434 { longdata = countdata & 0xFFFFFFFF ;
4435 PUT_LE_INT (psf, longdata) ;
4436 count += 4 ;
4438 break ;
4440 case 'f' :
4441 /* Floats are passed as doubles. Is this always true? */
4442 floatdata = (float) va_arg (argptr, double) ;
4443 if (psf->rwf_endian == SF_ENDIAN_BIG)
4444 float32_be_write (floatdata, psf->header + psf->headindex) ;
4445 else
4446 float32_le_write (floatdata, psf->header + psf->headindex) ;
4447 psf->headindex += 4 ;
4448 count += 4 ;
4449 break ;
4451 case 'd' :
4452 doubledata = va_arg (argptr, double) ;
4453 if (psf->rwf_endian == SF_ENDIAN_BIG)
4454 double64_be_write (doubledata, psf->header + psf->headindex) ;
4455 else
4456 double64_le_write (doubledata, psf->header + psf->headindex) ;
4457 psf->headindex += 8 ;
4458 count += 8 ;
4459 break ;
4461 case 's' :
4462 strptr = va_arg (argptr, char *) ;
4463 size = strlen (strptr) + 1 ;
4464 size += (size & 1) ;
4465 if (psf->rwf_endian == SF_ENDIAN_BIG)
4466 { PUT_BE_INT (psf, size) ;
4468 else
4469 { PUT_LE_INT (psf, size) ;
4471 memcpy (&(psf->header [psf->headindex]), strptr, size) ;
4472 psf->headindex += size ;
4473 psf->header [psf->headindex - 1] = 0 ;
4474 count += 4 + size ;
4475 break ;
4477 case 'S' :
4478 strptr = va_arg (argptr, char *) ;
4479 size = strlen (strptr) + 1 ;
4480 memcpy (&(psf->header [psf->headindex]), strptr, size) ;
4481 psf->headindex += size ;
4482 count += size ;
4483 break ;
4485 case 'b' :
4486 bindata = va_arg (argptr, void *) ;
4487 size = va_arg (argptr, size_t) ;
4488 memcpy (&(psf->header [psf->headindex]), bindata, size) ;
4489 psf->headindex += size ;
4490 count += size ;
4491 break ;
4493 case 'z' :
4494 size = va_arg (argptr, size_t) ;
4495 count += size ;
4496 while (size)
4497 { psf->header [psf->headindex] = 0 ;
4498 psf->headindex ++ ;
4499 size -- ;
4501 break ;
4503 case 'h' :
4504 bindata = va_arg (argptr, void *) ;
4505 memcpy (&(psf->header [psf->headindex]), bindata, 16) ;
4506 psf->headindex += 16 ;
4507 count += 16 ;
4508 break ;
4510 case 'j' :
4511 size = va_arg (argptr, int) ;
4512 psf->headindex += size ;
4513 count = size ;
4514 break ;
4516 default :
4517 psf_log_printf (psf, "*** Invalid format specifier `%c'\n", c) ;
4518 psf->error = SFE_INTERNAL ;
4519 break ;
4523 va_end (argptr) ;
4524 return count ;
4525 } /* psf_binheader_writef */
4527 /*-----------------------------------------------------------------------------------------------
4528 ** Binary header reading functions. Returns number of bytes read.
4530 ** Format specifiers are the same as for header write function above with the following
4531 ** additions:
4533 ** p - jump a given number of position from start of file.
4535 ** If format is NULL, psf_binheader_readf returns the current offset.
4538 #if (CPU_IS_BIG_ENDIAN == 1)
4539 #define GET_MARKER(ptr) ( ((ptr) [0] << 24) | ((ptr) [1] << 16) | \
4540 ((ptr) [2] << 8) | ((ptr) [3]) )
4542 #elif (CPU_IS_LITTLE_ENDIAN == 1)
4543 #define GET_MARKER(ptr) ( ((ptr) [0]) | ((ptr) [1] << 8) | \
4544 ((ptr) [2] << 16) | ((ptr) [3] << 24) )
4546 #else
4547 # error "Cannot determine endian-ness of processor."
4548 #endif
4550 #define GET_LE_SHORT(ptr) ( ((ptr) [1] << 8) | ((ptr) [0]) )
4551 #define GET_BE_SHORT(ptr) ( ((ptr) [0] << 8) | ((ptr) [1]) )
4553 #define GET_LE_3BYTE(ptr) ( ((ptr) [2] << 16) | ((ptr) [1] << 8) | ((ptr) [0]) )
4554 #define GET_BE_3BYTE(ptr) ( ((ptr) [0] << 16) | ((ptr) [1] << 8) | ((ptr) [2]) )
4556 #define GET_LE_INT(ptr) ( ((ptr) [3] << 24) | ((ptr) [2] << 16) | \
4557 ((ptr) [1] << 8) | ((ptr) [0]) )
4559 #define GET_BE_INT(ptr) ( ((ptr) [0] << 24) | ((ptr) [1] << 16) | \
4560 ((ptr) [2] << 8) | ((ptr) [3]) )
4562 #if (SIZEOF_LONG == 4)
4563 #define GET_LE_8BYTE(ptr) ( ((ptr) [3] << 24) | ((ptr) [2] << 16) | \
4564 ((ptr) [1] << 8) | ((ptr) [0]) )
4566 #define GET_BE_8BYTE(ptr) ( ((ptr) [4] << 24) | ((ptr) [5] << 16) | \
4567 ((ptr) [6] << 8) | ((ptr) [7]) )
4568 #else
4569 #define GET_LE_8BYTE(ptr) ( (((ptr) [7] * 1L) << 56) | (((ptr) [6] * 1L) << 48) | \
4570 (((ptr) [5] * 1L) << 40) | (((ptr) [4] * 1L) << 32) | \
4571 (((ptr) [3] * 1L) << 24) | (((ptr) [2] * 1L) << 16) | \
4572 (((ptr) [1] * 1L) << 8 ) | ((ptr) [0]))
4574 #define GET_BE_8BYTE(ptr) ( (((ptr) [0] * 1L) << 56) | (((ptr) [1] * 1L) << 48) | \
4575 (((ptr) [2] * 1L) << 40) | (((ptr) [3] * 1L) << 32) | \
4576 (((ptr) [4] * 1L) << 24) | (((ptr) [5] * 1L) << 16) | \
4577 (((ptr) [6] * 1L) << 8 ) | ((ptr) [7]))
4579 #endif
4581 static int
4582 header_read (SF_PRIVATE *psf, void *ptr, int bytes)
4583 { int count = 0 ;
4585 if (psf->headindex + bytes > SIGNED_SIZEOF (psf->header))
4586 { if (psf->headend < SIGNED_SIZEOF (psf->header))
4587 psf_log_printf (psf, "Warning : Further header read would overflow buffer.\n") ;
4588 psf->headend = SIGNED_SIZEOF (psf->header) ;
4590 /* This is the best that we can do. */
4591 return psf_fread (ptr, 1, bytes, psf) ;
4594 if (psf->headindex + bytes > psf->headend)
4595 { count = psf_fread (psf->header + psf->headend, 1, bytes - (psf->headend - psf->headindex), psf) ;
4596 if (count != bytes - (int) (psf->headend - psf->headindex))
4597 { psf_log_printf (psf, "Error : psf_fread returned short count.\n") ;
4598 return 0 ;
4600 psf->headend += count ;
4603 memcpy (ptr, psf->header + psf->headindex, bytes) ;
4604 psf->headindex += bytes ;
4606 return bytes ;
4607 } /* header_read */
4609 static void
4610 header_seek (SF_PRIVATE *psf, sf_count_t position, int whence)
4613 switch (whence)
4614 { case SEEK_SET :
4615 if (position > SIGNED_SIZEOF (psf->header))
4616 { /* Too much header to cache so just seek instead. */
4617 psf_fseek (psf, position, whence) ;
4618 return ;
4620 if (position > psf->headend)
4621 psf->headend += psf_fread (psf->header + psf->headend, 1, position - psf->headend, psf) ;
4622 psf->headindex = position ;
4623 break ;
4625 case SEEK_CUR :
4626 if (psf->headindex + position < 0)
4627 break ;
4629 if (psf->headindex >= SIGNED_SIZEOF (psf->header))
4630 { psf_fseek (psf, position, whence) ;
4631 return ;
4634 if (psf->headindex + position <= psf->headend)
4635 { psf->headindex += position ;
4636 break ;
4639 if (psf->headindex + position > SIGNED_SIZEOF (psf->header))
4640 { /* Need to jump this without caching it. */
4641 psf->headindex = psf->headend ;
4642 psf_fseek (psf, position, SEEK_CUR) ;
4643 break ;
4646 psf->headend += psf_fread (psf->header + psf->headend, 1, position - (psf->headend - psf->headindex), psf) ;
4647 psf->headindex = psf->headend ;
4648 break ;
4650 case SEEK_END :
4651 default :
4652 psf_log_printf (psf, "Bad whence param in header_seek().\n") ;
4653 break ;
4656 return ;
4657 } /* header_seek */
4659 static int
4660 header_gets (SF_PRIVATE *psf, char *ptr, int bufsize)
4662 int k ;
4664 for (k = 0 ; k < bufsize - 1 ; k++)
4665 { if (psf->headindex < psf->headend)
4666 { ptr [k] = psf->header [psf->headindex] ;
4667 psf->headindex ++ ;
4669 else
4670 { psf->headend += psf_fread (psf->header + psf->headend, 1, 1, psf) ;
4671 ptr [k] = psf->header [psf->headindex] ;
4672 psf->headindex = psf->headend ;
4675 if (ptr [k] == '\n')
4676 break ;
4679 ptr [k] = 0 ;
4681 return k ;
4682 } /* header_gets */
4685 psf_binheader_readf (SF_PRIVATE *psf, char const *format, ...)
4686 { va_list argptr ;
4687 sf_count_t *countptr, countdata ;
4688 unsigned char *ucptr, sixteen_bytes [16] ;
4689 unsigned int *intptr, intdata ;
4690 unsigned short *shortptr ;
4691 char *charptr ;
4692 float *floatptr ;
4693 double *doubleptr ;
4694 char c ;
4695 int byte_count = 0, count ;
4697 if (! format)
4698 return psf_ftell (psf) ;
4700 va_start (argptr, format) ;
4702 while ((c = *format++))
4703 { switch (c)
4704 { case 'e' : /* All conversions are now from LE to host. */
4705 psf->rwf_endian = SF_ENDIAN_LITTLE ;
4706 break ;
4708 case 'E' : /* All conversions are now from BE to host. */
4709 psf->rwf_endian = SF_ENDIAN_BIG ;
4710 break ;
4712 case 'm' :
4713 intptr = va_arg (argptr, unsigned int*) ;
4714 ucptr = (unsigned char*) intptr ;
4715 byte_count += header_read (psf, ucptr, sizeof (int)) ;
4716 *intptr = GET_MARKER (ucptr) ;
4717 break ;
4719 case 'h' :
4720 intptr = va_arg (argptr, unsigned int*) ;
4721 ucptr = (unsigned char*) intptr ;
4722 byte_count += header_read (psf, sixteen_bytes, sizeof (sixteen_bytes)) ;
4723 { int k ;
4724 intdata = 0 ;
4725 for (k = 0 ; k < 16 ; k++)
4726 intdata ^= sixteen_bytes [k] << k ;
4728 *intptr = intdata ;
4729 break ;
4731 case '1' :
4732 charptr = va_arg (argptr, char*) ;
4733 byte_count += header_read (psf, charptr, sizeof (char)) ;
4734 break ;
4736 case '2' :
4737 shortptr = va_arg (argptr, unsigned short*) ;
4738 ucptr = (unsigned char*) shortptr ;
4739 byte_count += header_read (psf, ucptr, sizeof (short)) ;
4740 if (psf->rwf_endian == SF_ENDIAN_BIG)
4741 *shortptr = GET_BE_SHORT (ucptr) ;
4742 else
4743 *shortptr = GET_LE_SHORT (ucptr) ;
4744 break ;
4746 case '3' :
4747 intptr = va_arg (argptr, unsigned int*) ;
4748 byte_count += header_read (psf, sixteen_bytes, 3) ;
4749 if (psf->rwf_endian == SF_ENDIAN_BIG)
4750 *intptr = GET_BE_3BYTE (sixteen_bytes) ;
4751 else
4752 *intptr = GET_LE_3BYTE (sixteen_bytes) ;
4753 break ;
4755 case '4' :
4756 intptr = va_arg (argptr, unsigned int*) ;
4757 ucptr = (unsigned char*) intptr ;
4758 byte_count += header_read (psf, ucptr, sizeof (int)) ;
4759 if (psf->rwf_endian == SF_ENDIAN_BIG)
4760 *intptr = GET_BE_INT (ucptr) ;
4761 else
4762 *intptr = GET_LE_INT (ucptr) ;
4763 break ;
4765 case '8' :
4766 countptr = va_arg (argptr, sf_count_t*) ;
4767 byte_count += header_read (psf, sixteen_bytes, 8) ;
4768 if (psf->rwf_endian == SF_ENDIAN_BIG)
4769 countdata = GET_BE_8BYTE (sixteen_bytes) ;
4770 else
4771 countdata = GET_LE_8BYTE (sixteen_bytes) ;
4772 *countptr = countdata ;
4773 break ;
4775 case 'f' : /* Float conversion */
4776 floatptr = va_arg (argptr, float *) ;
4777 *floatptr = 0.0 ;
4778 byte_count += header_read (psf, floatptr, sizeof (float)) ;
4779 if (psf->rwf_endian == SF_ENDIAN_BIG)
4780 *floatptr = float32_be_read ((unsigned char*) floatptr) ;
4781 else
4782 *floatptr = float32_le_read ((unsigned char*) floatptr) ;
4783 break ;
4785 case 'd' : /* double conversion */
4786 doubleptr = va_arg (argptr, double *) ;
4787 *doubleptr = 0.0 ;
4788 byte_count += header_read (psf, doubleptr, sizeof (double)) ;
4789 if (psf->rwf_endian == SF_ENDIAN_BIG)
4790 *doubleptr = double64_be_read ((unsigned char*) doubleptr) ;
4791 else
4792 *doubleptr = double64_le_read ((unsigned char*) doubleptr) ;
4793 break ;
4795 case 's' :
4796 psf_log_printf (psf, "Format conversion 's' not implemented yet.\n") ;
4798 strptr = va_arg (argptr, char *) ;
4799 size = strlen (strptr) + 1 ;
4800 size += (size & 1) ;
4801 longdata = H2LE_INT (size) ;
4802 get_int (psf, longdata) ;
4803 memcpy (&(psf->header [psf->headindex]), strptr, size) ;
4804 psf->headindex += size ;
4806 break ;
4808 case 'b' :
4809 charptr = va_arg (argptr, char*) ;
4810 count = va_arg (argptr, int) ;
4811 if (count > 0)
4812 byte_count += header_read (psf, charptr, count) ;
4813 break ;
4815 case 'G' :
4816 charptr = va_arg (argptr, char*) ;
4817 count = va_arg (argptr, int) ;
4818 if (count > 0)
4819 byte_count += header_gets (psf, charptr, count) ;
4820 break ;
4822 case 'z' :
4823 psf_log_printf (psf, "Format conversion 'z' not implemented yet.\n") ;
4825 size = va_arg (argptr, size_t) ;
4826 while (size)
4827 { psf->header [psf->headindex] = 0 ;
4828 psf->headindex ++ ;
4829 size -- ;
4832 break ;
4834 case 'p' :
4835 /* Get the seek position first. */
4836 count = va_arg (argptr, int) ;
4837 header_seek (psf, count, SEEK_SET) ;
4838 byte_count = count ;
4839 break ;
4841 case 'j' :
4842 /* Get the seek position first. */
4843 count = va_arg (argptr, int) ;
4844 header_seek (psf, count, SEEK_CUR) ;
4845 byte_count += count ;
4846 break ;
4848 default :
4849 psf_log_printf (psf, "*** Invalid format specifier `%c'\n", c) ;
4850 psf->error = SFE_INTERNAL ;
4851 break ;
4855 va_end (argptr) ;
4857 return byte_count ;
4858 } /* psf_binheader_readf */
4860 /*-----------------------------------------------------------------------------------------------
4863 sf_count_t
4864 psf_default_seek (SF_PRIVATE *psf, int mode, sf_count_t samples_from_start)
4865 { sf_count_t position, retval ;
4867 if (! (psf->blockwidth && psf->dataoffset >= 0))
4868 { psf->error = SFE_BAD_SEEK ;
4869 return ((sf_count_t) -1) ;
4872 if (! psf->sf.seekable)
4873 { psf->error = SFE_NOT_SEEKABLE ;
4874 return ((sf_count_t) -1) ;
4877 position = psf->dataoffset + psf->blockwidth * samples_from_start ;
4879 if ((retval = psf_fseek (psf, position, SEEK_SET)) != position)
4880 { psf->error = SFE_SEEK_FAILED ;
4881 return ((sf_count_t) -1) ;
4884 mode = mode ;
4886 return samples_from_start ;
4887 } /* psf_default_seek */
4889 /*-----------------------------------------------------------------------------------------------
4892 void
4893 psf_hexdump (void *ptr, int len)
4894 { char ascii [17], *data ;
4895 int k, m ;
4897 if ((data = ptr) == NULL)
4898 return ;
4899 if (len <= 0)
4900 return ;
4902 puts ("") ;
4903 for (k = 0 ; k < len ; k += 16)
4904 { memset (ascii, ' ', sizeof (ascii)) ;
4906 printf ("%08X: ", k) ;
4907 for (m = 0 ; m < 16 && k + m < len ; m++)
4908 { printf (m == 8 ? " %02X " : "%02X ", data [k + m] & 0xFF) ;
4909 ascii [m] = isprint (data [k + m]) ? data [k + m] : '.' ;
4912 if (m <= 8) printf (" ") ;
4913 for ( ; m < 16 ; m++) printf (" ") ;
4915 ascii [16] = 0 ;
4916 printf (" %s\n", ascii) ;
4919 puts ("") ;
4920 } /* psf_hexdump */
4922 void
4923 psf_log_SF_INFO (SF_PRIVATE *psf)
4924 { psf_log_printf (psf, "---------------------------------\n") ;
4926 psf_log_printf (psf, " Sample rate : %d\n", psf->sf.samplerate) ;
4927 psf_log_printf (psf, " Frames : %C\n", psf->sf.frames) ;
4928 psf_log_printf (psf, " Channels : %d\n", psf->sf.channels) ;
4930 psf_log_printf (psf, " Format : 0x%X\n", psf->sf.format) ;
4931 psf_log_printf (psf, " Sections : %d\n", psf->sf.sections) ;
4932 psf_log_printf (psf, " Seekable : %s\n", psf->sf.seekable ? "TRUE" : "FALSE") ;
4934 psf_log_printf (psf, "---------------------------------\n") ;
4935 } /* psf_dump_SFINFO */
4937 /*========================================================================================
4940 void*
4941 psf_memset (void *s, int c, sf_count_t len)
4942 { char *ptr ;
4943 int setcount ;
4945 ptr = (char *) s ;
4947 while (len > 0)
4948 { setcount = (len > 0x10000000) ? 0x10000000 : (int) len ;
4950 memset (ptr, c, setcount) ;
4952 ptr += setcount ;
4953 len -= setcount ;
4956 return s ;
4957 } /* psf_memset */
4959 void psf_get_date_str (char *str, int maxlen)
4960 { time_t current ;
4961 struct tm timedata, *tmptr ;
4963 time (&current) ;
4965 #if defined (HAVE_GMTIME_R)
4966 /* If the re-entrant version is available, use it. */
4967 tmptr = gmtime_r (&current, &timedata) ;
4968 #elif defined (HAVE_GMTIME)
4969 /* Otherwise use the standard one and copy the data to local storage. */
4970 tmptr = gmtime (&current) ;
4971 memcpy (&timedata, tmptr, sizeof (timedata)) ;
4972 #else
4973 tmptr = NULL ;
4974 #endif
4976 if (tmptr)
4977 LSF_SNPRINTF (str, maxlen, "%4d-%02d-%02d %02d:%02d:%02d UTC",
4978 1900 + timedata.tm_year, timedata.tm_mon, timedata.tm_mday,
4979 timedata.tm_hour, timedata.tm_min, timedata.tm_sec) ;
4980 else
4981 LSF_SNPRINTF (str, maxlen, "Unknown date") ;
4983 return ;
4984 } /* psf_get_date_str */
4987 subformat_to_bytewidth (int format)
4989 switch (format)
4990 { case SF_FORMAT_PCM_U8 :
4991 case SF_FORMAT_PCM_S8 :
4992 return 1 ;
4993 case SF_FORMAT_PCM_16 :
4994 return 2 ;
4995 case SF_FORMAT_PCM_24 :
4996 return 3 ;
4997 case SF_FORMAT_PCM_32 :
4998 case SF_FORMAT_FLOAT :
4999 return 4 ;
5000 case SF_FORMAT_DOUBLE :
5001 return 8 ;
5004 return 0 ;
5005 } /* subformat_to_bytewidth */
5008 s_bitwidth_to_subformat (int bits)
5009 { static int array [] =
5010 { SF_FORMAT_PCM_S8, SF_FORMAT_PCM_16, SF_FORMAT_PCM_24, SF_FORMAT_PCM_32
5013 if (bits < 8 || bits > 32)
5014 return 0 ;
5016 return array [((bits + 7) / 8) - 1] ;
5017 } /* bitwidth_to_subformat */
5020 u_bitwidth_to_subformat (int bits)
5021 { static int array [] =
5022 { SF_FORMAT_PCM_U8, SF_FORMAT_PCM_16, SF_FORMAT_PCM_24, SF_FORMAT_PCM_32
5025 if (bits < 8 || bits > 32)
5026 return 0 ;
5028 return array [((bits + 7) / 8) - 1] ;
5029 } /* bitwidth_to_subformat */
5031 #endif /* PSF_LOG_PRINTF_ONLY */
5034 ** Do not edit or modify anything in this comment block.
5035 ** The arch-tag line is a file identity tag for the GNU Arch
5036 ** revision control system.
5038 ** arch-tag: 33e9795e-f717-461a-9feb-65d083a56395
5041 * Copyright 1992 by Jutta Degener and Carsten Bormann, Technische
5042 * Universitaet Berlin. See the accompanying file "COPYRIGHT" for
5043 * details. THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE.
5046 #include <stdio.h>
5050 * 4.3 FIXED POINT IMPLEMENTATION OF THE RPE-LTP DECODER
5053 static void Postprocessing (
5054 struct gsm_state * S,
5055 register word * s)
5057 register int k;
5058 register word msr = S->msr;
5059 register word tmp;
5061 for (k = 160; k--; s++) {
5062 tmp = GSM_MULT_R( msr, 28180 );
5063 msr = GSM_ADD(*s, tmp); /* Deemphasis */
5064 *s = GSM_ADD(msr, msr) & 0xFFF8; /* Truncation & Upscaling */
5066 S->msr = msr;
5069 void Gsm_Decoder (
5070 struct gsm_state * S,
5072 word * LARcr, /* [0..7] IN */
5074 word * Ncr, /* [0..3] IN */
5075 word * bcr, /* [0..3] IN */
5076 word * Mcr, /* [0..3] IN */
5077 word * xmaxcr, /* [0..3] IN */
5078 word * xMcr, /* [0..13*4] IN */
5080 word * s) /* [0..159] OUT */
5082 int j, k;
5083 word erp[40], wt[160];
5084 word * drp = S->dp0 + 120;
5086 for (j=0; j <= 3; j++, xmaxcr++, bcr++, Ncr++, Mcr++, xMcr += 13) {
5088 Gsm_RPE_Decoding( /*-S,-*/ *xmaxcr, *Mcr, xMcr, erp );
5089 Gsm_Long_Term_Synthesis_Filtering( S, *Ncr, *bcr, erp, drp );
5091 for (k = 0; k <= 39; k++) wt[ j * 40 + k ] = drp[ k ];
5094 Gsm_Short_Term_Synthesis_Filter( S, LARcr, wt, s );
5095 Postprocessing(S, s);
5098 ** Do not edit or modify anything in this comment block.
5099 ** The arch-tag line is a file identity tag for the GNU Arch
5100 ** revision control system.
5102 ** arch-tag: 11ae5b90-2e8b-400b-ac64-a69a1fc6cc41
5106 ** Copyright (C) 2003,2004 Erik de Castro Lopo <erikd@mega-nerd.com>
5108 ** This program is free software; you can redistribute it and/or modify
5109 ** it under the terms of the GNU Lesser General Public License as published by
5110 ** the Free Software Foundation; either version 2.1 of the License, or
5111 ** (at your option) any later version.
5113 ** This program is distributed in the hope that it will be useful,
5114 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
5115 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5116 ** GNU Lesser General Public License for more details.
5118 ** You should have received a copy of the GNU Lesser General Public License
5119 ** along with this program; if not, write to the Free Software
5120 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
5123 #include <stdlib.h>
5126 /*============================================================================
5127 ** Rule number 1 is to only apply dither when going from a larger bitwidth
5128 ** to a smaller bitwidth. This can happen on both read and write.
5130 ** Need to apply dither on all conversions marked X below.
5132 ** Dither on write:
5134 ** Input
5135 ** | short int float double
5136 ** --------+-----------------------------------------------
5137 ** O 8 bit | X X X X
5138 ** u 16 bit | none X X X
5139 ** t 24 bit | none X X X
5140 ** p 32 bit | none none X X
5141 ** u float | none none none none
5142 ** t double | none none none none
5144 ** Dither on read:
5146 ** Input
5147 ** O | 8 bit 16 bit 24 bit 32 bit float double
5148 ** u --------+-------------------------------------------------
5149 ** t short | none none X X X X
5150 ** p int | none none none X X X
5151 ** u float | none none none none none none
5152 ** t double | none none none none none none
5155 #define SFE_DITHER_BAD_PTR 666
5156 #define SFE_DITHER_BAD_TYPE 667
5158 typedef struct
5159 { int read_short_dither_bits, read_int_dither_bits ;
5160 int write_short_dither_bits, write_int_dither_bits ;
5161 double read_float_dither_scale, read_double_dither_bits ;
5162 double write_float_dither_scale, write_double_dither_bits ;
5164 sf_count_t (*read_short) (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
5165 sf_count_t (*read_int) (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
5166 sf_count_t (*read_float) (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
5167 sf_count_t (*read_double) (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
5169 sf_count_t (*write_short) (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
5170 sf_count_t (*write_int) (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
5171 sf_count_t (*write_float) (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
5172 sf_count_t (*write_double) (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
5174 double buffer [SF_BUFFER_LEN / sizeof (double)] ;
5175 } DITHER_DATA ;
5177 static sf_count_t dither_read_short (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
5178 static sf_count_t dither_read_int (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
5180 static sf_count_t dither_write_short (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
5181 static sf_count_t dither_write_int (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
5182 static sf_count_t dither_write_float (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
5183 static sf_count_t dither_write_double (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
5186 dither_init (SF_PRIVATE *psf, int mode)
5187 { DITHER_DATA *pdither ;
5189 pdither = psf->dither ; /* This may be NULL. */
5191 /* Turn off dither on read. */
5192 if (mode == SFM_READ && psf->read_dither.type == SFD_NO_DITHER)
5193 { if (pdither == NULL)
5194 return 0 ; /* Dither is already off, so just return. */
5196 if (pdither->read_short)
5197 psf->read_short = pdither->read_short ;
5198 if (pdither->read_int)
5199 psf->read_int = pdither->read_int ;
5200 if (pdither->read_float)
5201 psf->read_float = pdither->read_float ;
5202 if (pdither->read_double)
5203 psf->read_double = pdither->read_double ;
5204 return 0 ;
5207 /* Turn off dither on write. */
5208 if (mode == SFM_WRITE && psf->write_dither.type == SFD_NO_DITHER)
5209 { if (pdither == NULL)
5210 return 0 ; /* Dither is already off, so just return. */
5212 if (pdither->write_short)
5213 psf->write_short = pdither->write_short ;
5214 if (pdither->write_int)
5215 psf->write_int = pdither->write_int ;
5216 if (pdither->write_float)
5217 psf->write_float = pdither->write_float ;
5218 if (pdither->write_double)
5219 psf->write_double = pdither->write_double ;
5220 return 0 ;
5223 /* Turn on dither on read if asked. */
5224 if (mode == SFM_READ && psf->read_dither.type != 0)
5225 { if (pdither == NULL)
5226 pdither = psf->dither = calloc (1, sizeof (DITHER_DATA)) ;
5227 if (pdither == NULL)
5228 return SFE_MALLOC_FAILED ;
5230 switch (psf->sf.format & SF_FORMAT_SUBMASK)
5231 { case SF_FORMAT_DOUBLE :
5232 case SF_FORMAT_FLOAT :
5233 pdither->read_int = psf->read_int ;
5234 psf->read_int = dither_read_int ;
5236 case SF_FORMAT_PCM_32 :
5237 case SF_FORMAT_PCM_24 :
5238 case SF_FORMAT_PCM_16 :
5239 case SF_FORMAT_PCM_S8 :
5240 case SF_FORMAT_PCM_U8 :
5241 pdither->read_short = psf->read_short ;
5242 psf->read_short = dither_read_short ;
5244 default : break ;
5248 /* Turn on dither on write if asked. */
5249 if (mode == SFM_WRITE && psf->write_dither.type != 0)
5250 { if (pdither == NULL)
5251 pdither = psf->dither = calloc (1, sizeof (DITHER_DATA)) ;
5252 if (pdither == NULL)
5253 return SFE_MALLOC_FAILED ;
5255 switch (psf->sf.format & SF_FORMAT_SUBMASK)
5256 { case SF_FORMAT_DOUBLE :
5257 case SF_FORMAT_FLOAT :
5258 pdither->write_int = psf->write_int ;
5259 psf->write_int = dither_write_int ;
5261 case SF_FORMAT_PCM_32 :
5262 case SF_FORMAT_PCM_24 :
5263 case SF_FORMAT_PCM_16 :
5264 case SF_FORMAT_PCM_S8 :
5265 case SF_FORMAT_PCM_U8 :
5267 default : break ;
5270 pdither->write_short = psf->write_short ;
5271 psf->write_short = dither_write_short ;
5273 pdither->write_int = psf->write_int ;
5274 psf->write_int = dither_write_int ;
5276 pdither->write_float = psf->write_float ;
5277 psf->write_float = dither_write_float ;
5279 pdither->write_double = psf->write_double ;
5280 psf->write_double = dither_write_double ;
5283 return 0 ;
5284 } /* dither_init */
5286 /*==============================================================================
5289 static void dither_short (const short *in, short *out, int frames, int channels) ;
5290 static void dither_int (const int *in, int *out, int frames, int channels) ;
5292 static void dither_float (const float *in, float *out, int frames, int channels) ;
5293 static void dither_double (const double *in, double *out, int frames, int channels) ;
5295 static sf_count_t
5296 dither_read_short (SF_PRIVATE *psf, short *ptr, sf_count_t len)
5297 { psf = psf ;
5298 ptr = ptr ;
5299 return len ;
5300 } /* dither_read_short */
5302 static sf_count_t
5303 dither_read_int (SF_PRIVATE *psf, int *ptr, sf_count_t len)
5304 { psf = psf ;
5305 ptr = ptr ;
5306 return len ;
5307 } /* dither_read_int */
5309 /*------------------------------------------------------------------------------
5312 static sf_count_t
5313 dither_write_short (SF_PRIVATE *psf, short *ptr, sf_count_t len)
5314 { DITHER_DATA *pdither ;
5315 int bufferlen, writecount, thiswrite ;
5316 sf_count_t total = 0 ;
5318 if ((pdither = psf->dither) == NULL)
5319 { psf->error = SFE_DITHER_BAD_PTR ;
5320 return 0 ;
5323 switch (psf->sf.format & SF_FORMAT_SUBMASK)
5324 { case SF_FORMAT_PCM_S8 :
5325 case SF_FORMAT_PCM_U8 :
5326 case SF_FORMAT_DPCM_8 :
5327 break ;
5329 default :
5330 return pdither->write_short (psf, ptr, len) ;
5333 bufferlen = sizeof (pdither->buffer) / sizeof (short) ;
5335 while (len > 0)
5336 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
5337 writecount /= psf->sf.channels ;
5338 writecount *= psf->sf.channels ;
5340 dither_short (ptr, (short*) pdither->buffer, writecount / psf->sf.channels, psf->sf.channels) ;
5342 thiswrite = pdither->write_short (psf, (short*) pdither->buffer, writecount) ;
5343 total += thiswrite ;
5344 len -= thiswrite ;
5345 if (thiswrite < writecount)
5346 break ;
5349 return total ;
5350 } /* dither_write_short */
5352 static sf_count_t
5353 dither_write_int (SF_PRIVATE *psf, int *ptr, sf_count_t len)
5354 { DITHER_DATA *pdither ;
5355 int bufferlen, writecount, thiswrite ;
5356 sf_count_t total = 0 ;
5358 if ((pdither = psf->dither) == NULL)
5359 { psf->error = SFE_DITHER_BAD_PTR ;
5360 return 0 ;
5363 switch (psf->sf.format & SF_FORMAT_SUBMASK)
5364 { case SF_FORMAT_PCM_S8 :
5365 case SF_FORMAT_PCM_U8 :
5366 case SF_FORMAT_PCM_16 :
5367 case SF_FORMAT_PCM_24 :
5369 case SF_FORMAT_DPCM_8 :
5370 case SF_FORMAT_DPCM_16 :
5371 break ;
5373 default :
5374 return pdither->write_int (psf, ptr, len) ;
5378 bufferlen = sizeof (pdither->buffer) / sizeof (int) ;
5380 while (len > 0)
5381 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
5382 writecount /= psf->sf.channels ;
5383 writecount *= psf->sf.channels ;
5385 dither_int (ptr, (int*) pdither->buffer, writecount / psf->sf.channels, psf->sf.channels) ;
5387 thiswrite = pdither->write_int (psf, (int*) pdither->buffer, writecount) ;
5388 total += thiswrite ;
5389 len -= thiswrite ;
5390 if (thiswrite < writecount)
5391 break ;
5394 return total ;
5395 } /* dither_write_int */
5397 static sf_count_t
5398 dither_write_float (SF_PRIVATE *psf, float *ptr, sf_count_t len)
5399 { DITHER_DATA *pdither ;
5400 int bufferlen, writecount, thiswrite ;
5401 sf_count_t total = 0 ;
5403 if ((pdither = psf->dither) == NULL)
5404 { psf->error = SFE_DITHER_BAD_PTR ;
5405 return 0 ;
5408 switch (psf->sf.format & SF_FORMAT_SUBMASK)
5409 { case SF_FORMAT_PCM_S8 :
5410 case SF_FORMAT_PCM_U8 :
5411 case SF_FORMAT_PCM_16 :
5412 case SF_FORMAT_PCM_24 :
5414 case SF_FORMAT_DPCM_8 :
5415 case SF_FORMAT_DPCM_16 :
5416 break ;
5418 default :
5419 return pdither->write_float (psf, ptr, len) ;
5422 bufferlen = sizeof (pdither->buffer) / sizeof (float) ;
5424 while (len > 0)
5425 { writecount = (len >= bufferlen) ? bufferlen : (float) len ;
5426 writecount /= psf->sf.channels ;
5427 writecount *= psf->sf.channels ;
5429 dither_float (ptr, (float*) pdither->buffer, writecount / psf->sf.channels, psf->sf.channels) ;
5431 thiswrite = pdither->write_float (psf, (float*) pdither->buffer, writecount) ;
5432 total += thiswrite ;
5433 len -= thiswrite ;
5434 if (thiswrite < writecount)
5435 break ;
5438 return total ;
5439 } /* dither_write_float */
5441 static sf_count_t
5442 dither_write_double (SF_PRIVATE *psf, double *ptr, sf_count_t len)
5443 { DITHER_DATA *pdither ;
5444 int bufferlen, writecount, thiswrite ;
5445 sf_count_t total = 0 ;
5447 if ((pdither = psf->dither) == NULL)
5448 { psf->error = SFE_DITHER_BAD_PTR ;
5449 return 0 ;
5452 switch (psf->sf.format & SF_FORMAT_SUBMASK)
5453 { case SF_FORMAT_PCM_S8 :
5454 case SF_FORMAT_PCM_U8 :
5455 case SF_FORMAT_PCM_16 :
5456 case SF_FORMAT_PCM_24 :
5458 case SF_FORMAT_DPCM_8 :
5459 case SF_FORMAT_DPCM_16 :
5460 break ;
5462 default :
5463 return pdither->write_double (psf, ptr, len) ;
5467 bufferlen = sizeof (pdither->buffer) / sizeof (double) ;
5469 while (len > 0)
5470 { writecount = (len >= bufferlen) ? bufferlen : (double) len ;
5471 writecount /= psf->sf.channels ;
5472 writecount *= psf->sf.channels ;
5474 dither_double (ptr, (double*) pdither->buffer, writecount / psf->sf.channels, psf->sf.channels) ;
5476 thiswrite = pdither->write_double (psf, (double*) pdither->buffer, writecount) ;
5477 total += thiswrite ;
5478 len -= thiswrite ;
5479 if (thiswrite < writecount)
5480 break ;
5483 return total ;
5484 } /* dither_write_double */
5486 /*==============================================================================
5489 static void
5490 dither_short (const short *in, short *out, int frames, int channels)
5491 { int ch, k ;
5493 for (ch = 0 ; ch < channels ; ch++)
5494 for (k = ch ; k < channels * frames ; k += channels)
5495 out [k] = in [k] ;
5497 } /* dither_short */
5499 static void
5500 dither_int (const int *in, int *out, int frames, int channels)
5501 { int ch, k ;
5503 for (ch = 0 ; ch < channels ; ch++)
5504 for (k = ch ; k < channels * frames ; k += channels)
5505 out [k] = in [k] ;
5507 } /* dither_int */
5509 static void
5510 dither_float (const float *in, float *out, int frames, int channels)
5511 { int ch, k ;
5513 for (ch = 0 ; ch < channels ; ch++)
5514 for (k = ch ; k < channels * frames ; k += channels)
5515 out [k] = in [k] ;
5517 } /* dither_float */
5519 static void
5520 dither_double (const double *in, double *out, int frames, int channels)
5521 { int ch, k ;
5523 for (ch = 0 ; ch < channels ; ch++)
5524 for (k = ch ; k < channels * frames ; k += channels)
5525 out [k] = in [k] ;
5527 } /* dither_double */
5529 /*==============================================================================
5531 #if 0
5534 ** Not made public because this (maybe) requires storage of state information.
5536 ** Also maybe need separate state info for each channel!!!!
5540 DO_NOT_USE_sf_dither_short (const SF_DITHER_INFO *dither, const short *in, short *out, int frames, int channels)
5541 { int ch, k ;
5543 if (! dither)
5544 return SFE_DITHER_BAD_PTR ;
5546 switch (dither->type & SFD_TYPEMASK)
5547 { case SFD_WHITE :
5548 case SFD_TRIANGULAR_PDF :
5549 for (ch = 0 ; ch < channels ; ch++)
5550 for (k = ch ; k < channels * frames ; k += channels)
5551 out [k] = in [k] ;
5552 break ;
5554 default :
5555 return SFE_DITHER_BAD_TYPE ;
5558 return 0 ;
5559 } /* DO_NOT_USE_sf_dither_short */
5562 DO_NOT_USE_sf_dither_int (const SF_DITHER_INFO *dither, const int *in, int *out, int frames, int channels)
5563 { int ch, k ;
5565 if (! dither)
5566 return SFE_DITHER_BAD_PTR ;
5568 switch (dither->type & SFD_TYPEMASK)
5569 { case SFD_WHITE :
5570 case SFD_TRIANGULAR_PDF :
5571 for (ch = 0 ; ch < channels ; ch++)
5572 for (k = ch ; k < channels * frames ; k += channels)
5573 out [k] = in [k] ;
5574 break ;
5576 default :
5577 return SFE_DITHER_BAD_TYPE ;
5580 return 0 ;
5581 } /* DO_NOT_USE_sf_dither_int */
5584 DO_NOT_USE_sf_dither_float (const SF_DITHER_INFO *dither, const float *in, float *out, int frames, int channels)
5585 { int ch, k ;
5587 if (! dither)
5588 return SFE_DITHER_BAD_PTR ;
5590 switch (dither->type & SFD_TYPEMASK)
5591 { case SFD_WHITE :
5592 case SFD_TRIANGULAR_PDF :
5593 for (ch = 0 ; ch < channels ; ch++)
5594 for (k = ch ; k < channels * frames ; k += channels)
5595 out [k] = in [k] ;
5596 break ;
5598 default :
5599 return SFE_DITHER_BAD_TYPE ;
5602 return 0 ;
5603 } /* DO_NOT_USE_sf_dither_float */
5606 DO_NOT_USE_sf_dither_double (const SF_DITHER_INFO *dither, const double *in, double *out, int frames, int channels)
5607 { int ch, k ;
5609 if (! dither)
5610 return SFE_DITHER_BAD_PTR ;
5612 switch (dither->type & SFD_TYPEMASK)
5613 { case SFD_WHITE :
5614 case SFD_TRIANGULAR_PDF :
5615 for (ch = 0 ; ch < channels ; ch++)
5616 for (k = ch ; k < channels * frames ; k += channels)
5617 out [k] = in [k] ;
5618 break ;
5620 default :
5621 return SFE_DITHER_BAD_TYPE ;
5624 return 0 ;
5625 } /* DO_NOT_USE_sf_dither_double */
5627 #endif
5629 ** Do not edit or modify anything in this comment block.
5630 ** The arch-tag line is a file identity tag for the GNU Arch
5631 ** revision control system.
5633 ** arch-tag: 673fad58-5314-421c-9144-9d54bfdf104c
5636 ** Copyright (C) 1999-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
5638 ** This program is free software; you can redistribute it and/or modify
5639 ** it under the terms of the GNU Lesser General Public License as published by
5640 ** the Free Software Foundation; either version 2.1 of the License, or
5641 ** (at your option) any later version.
5643 ** This program is distributed in the hope that it will be useful,
5644 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
5645 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5646 ** GNU Lesser General Public License for more details.
5648 ** You should have received a copy of the GNU Lesser General Public License
5649 ** along with this program; if not, write to the Free Software
5650 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
5654 #include <stdio.h>
5655 #include <stdlib.h>
5656 #include <string.h>
5659 #if CPU_IS_LITTLE_ENDIAN
5660 #define DOUBLE64_READ double64_le_read
5661 #define DOUBLE64_WRITE double64_le_write
5662 #elif CPU_IS_BIG_ENDIAN
5663 #define DOUBLE64_READ double64_be_read
5664 #define DOUBLE64_WRITE double64_be_write
5665 #endif
5667 /*--------------------------------------------------------------------------------------------
5668 ** Processor floating point capabilities. double64_get_capability () returns one of the
5669 ** latter three values.
5672 enum
5673 { DOUBLE_UNKNOWN = 0x00,
5674 DOUBLE_CAN_RW_LE = 0x23,
5675 DOUBLE_CAN_RW_BE = 0x34,
5676 DOUBLE_BROKEN_LE = 0x45,
5677 DOUBLE_BROKEN_BE = 0x56
5680 /*--------------------------------------------------------------------------------------------
5681 ** Prototypes for private functions.
5684 static sf_count_t host_read_d2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
5685 static sf_count_t host_read_d2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
5686 static sf_count_t host_read_d2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
5687 static sf_count_t host_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
5689 static sf_count_t host_write_s2d (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
5690 static sf_count_t host_write_i2d (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
5691 static sf_count_t host_write_f2d (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
5692 static sf_count_t host_write_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
5694 static void d2s_array (double *buffer, unsigned int count, short *ptr) ;
5695 static void d2i_array (double *buffer, unsigned int count, int *ptr) ;
5696 static void double64d2f_array (double *buffer, unsigned int count, float *ptr) ;
5698 static void s2d_array (short *ptr, double *buffer, unsigned int count) ;
5699 static void i2d_array (int *ptr, double *buffer, unsigned int count) ;
5700 static void double64f2d_array (float *ptr, double *buffer, unsigned int count) ;
5702 static void double64_peak_update (SF_PRIVATE *psf, double *buffer, int count, int indx) ;
5704 static int double64_get_capability (SF_PRIVATE *psf) ;
5706 static sf_count_t replace_read_d2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
5707 static sf_count_t replace_read_d2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
5708 static sf_count_t replace_read_d2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
5709 static sf_count_t replace_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
5711 static sf_count_t replace_write_s2d (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
5712 static sf_count_t replace_write_i2d (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
5713 static sf_count_t replace_write_f2d (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
5714 static sf_count_t replace_write_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
5716 static void d2bd_read (double *buffer, int count) ;
5717 static void bd2d_write (double *buffer, int count) ;
5719 /*--------------------------------------------------------------------------------------------
5720 ** Exported functions.
5724 double64_init (SF_PRIVATE *psf)
5725 { static int double64_caps ;
5727 double64_caps = double64_get_capability (psf) ;
5729 psf->blockwidth = sizeof (double) * psf->sf.channels ;
5731 if (psf->mode == SFM_READ || psf->mode == SFM_RDWR)
5732 { switch (psf->endian + double64_caps)
5733 { case (SF_ENDIAN_BIG + DOUBLE_CAN_RW_BE) :
5734 psf->float_endswap = SF_FALSE ;
5735 psf->read_short = host_read_d2s ;
5736 psf->read_int = host_read_d2i ;
5737 psf->read_float = host_read_d2f ;
5738 psf->read_double = host_read_d ;
5739 break ;
5741 case (SF_ENDIAN_LITTLE + DOUBLE_CAN_RW_LE) :
5742 psf->float_endswap = SF_FALSE ;
5743 psf->read_short = host_read_d2s ;
5744 psf->read_int = host_read_d2i ;
5745 psf->read_float = host_read_d2f ;
5746 psf->read_double = host_read_d ;
5747 break ;
5749 case (SF_ENDIAN_BIG + DOUBLE_CAN_RW_LE) :
5750 psf->float_endswap = SF_TRUE ;
5751 psf->read_short = host_read_d2s ;
5752 psf->read_int = host_read_d2i ;
5753 psf->read_float = host_read_d2f ;
5754 psf->read_double = host_read_d ;
5755 break ;
5757 case (SF_ENDIAN_LITTLE + DOUBLE_CAN_RW_BE) :
5758 psf->float_endswap = SF_TRUE ;
5759 psf->read_short = host_read_d2s ;
5760 psf->read_int = host_read_d2i ;
5761 psf->read_float = host_read_d2f ;
5762 psf->read_double = host_read_d ;
5763 break ;
5765 /* When the CPU is not IEEE compatible. */
5766 case (SF_ENDIAN_BIG + DOUBLE_BROKEN_BE) :
5767 psf->float_endswap = SF_FALSE ;
5768 psf->read_short = replace_read_d2s ;
5769 psf->read_int = replace_read_d2i ;
5770 psf->read_float = replace_read_d2f ;
5771 psf->read_double = replace_read_d ;
5772 break ;
5774 case (SF_ENDIAN_LITTLE + DOUBLE_BROKEN_LE) :
5775 psf->float_endswap = SF_FALSE ;
5776 psf->read_short = replace_read_d2s ;
5777 psf->read_int = replace_read_d2i ;
5778 psf->read_float = replace_read_d2f ;
5779 psf->read_double = replace_read_d ;
5780 break ;
5782 case (SF_ENDIAN_BIG + DOUBLE_BROKEN_LE) :
5783 psf->float_endswap = SF_TRUE ;
5784 psf->read_short = replace_read_d2s ;
5785 psf->read_int = replace_read_d2i ;
5786 psf->read_float = replace_read_d2f ;
5787 psf->read_double = replace_read_d ;
5788 break ;
5790 case (SF_ENDIAN_LITTLE + DOUBLE_BROKEN_BE) :
5791 psf->float_endswap = SF_TRUE ;
5792 psf->read_short = replace_read_d2s ;
5793 psf->read_int = replace_read_d2i ;
5794 psf->read_float = replace_read_d2f ;
5795 psf->read_double = replace_read_d ;
5796 break ;
5798 default : break ;
5802 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
5803 { switch (psf->endian + double64_caps)
5804 { case (SF_ENDIAN_LITTLE + DOUBLE_CAN_RW_LE) :
5805 psf->float_endswap = SF_FALSE ;
5806 psf->write_short = host_write_s2d ;
5807 psf->write_int = host_write_i2d ;
5808 psf->write_float = host_write_f2d ;
5809 psf->write_double = host_write_d ;
5810 break ;
5812 case (SF_ENDIAN_BIG + DOUBLE_CAN_RW_BE) :
5813 psf->float_endswap = SF_FALSE ;
5814 psf->write_short = host_write_s2d ;
5815 psf->write_int = host_write_i2d ;
5816 psf->write_float = host_write_f2d ;
5817 psf->write_double = host_write_d ;
5818 break ;
5820 case (SF_ENDIAN_BIG + DOUBLE_CAN_RW_LE) :
5821 psf->float_endswap = SF_TRUE ;
5822 psf->write_short = host_write_s2d ;
5823 psf->write_int = host_write_i2d ;
5824 psf->write_float = host_write_f2d ;
5825 psf->write_double = host_write_d ;
5826 break ;
5828 case (SF_ENDIAN_LITTLE + DOUBLE_CAN_RW_BE) :
5829 psf->float_endswap = SF_TRUE ;
5830 psf->write_short = host_write_s2d ;
5831 psf->write_int = host_write_i2d ;
5832 psf->write_float = host_write_f2d ;
5833 psf->write_double = host_write_d ;
5834 break ;
5836 /* When the CPU is not IEEE compatible. */
5837 case (SF_ENDIAN_LITTLE + DOUBLE_BROKEN_LE) :
5838 psf->float_endswap = SF_FALSE ;
5839 psf->write_short = replace_write_s2d ;
5840 psf->write_int = replace_write_i2d ;
5841 psf->write_float = replace_write_f2d ;
5842 psf->write_double = replace_write_d ;
5843 break ;
5845 case (SF_ENDIAN_BIG + DOUBLE_BROKEN_BE) :
5846 psf->float_endswap = SF_FALSE ;
5847 psf->write_short = replace_write_s2d ;
5848 psf->write_int = replace_write_i2d ;
5849 psf->write_float = replace_write_f2d ;
5850 psf->write_double = replace_write_d ;
5851 break ;
5853 case (SF_ENDIAN_BIG + DOUBLE_BROKEN_LE) :
5854 psf->float_endswap = SF_TRUE ;
5855 psf->write_short = replace_write_s2d ;
5856 psf->write_int = replace_write_i2d ;
5857 psf->write_float = replace_write_f2d ;
5858 psf->write_double = replace_write_d ;
5859 break ;
5861 case (SF_ENDIAN_LITTLE + DOUBLE_BROKEN_BE) :
5862 psf->float_endswap = SF_TRUE ;
5863 psf->write_short = replace_write_s2d ;
5864 psf->write_int = replace_write_i2d ;
5865 psf->write_float = replace_write_f2d ;
5866 psf->write_double = replace_write_d ;
5867 break ;
5869 default : break ;
5873 if (psf->filelength > psf->dataoffset)
5874 { psf->datalength = (psf->dataend > 0) ? psf->dataend - psf->dataoffset :
5875 psf->filelength - psf->dataoffset ;
5877 else
5878 psf->datalength = 0 ;
5880 psf->sf.frames = psf->datalength / psf->blockwidth ;
5882 return 0 ;
5883 } /* double64_init */
5885 /*----------------------------------------------------------------------------
5886 ** From : http://www.hpcf.cam.ac.uk/fp_formats.html
5888 ** 64 bit double precision layout (big endian)
5889 ** Sign bit 0
5890 ** Exponent bits 1-11
5891 ** Mantissa bits 12-63
5892 ** Exponent Offset 1023
5894 ** double single
5896 ** +INF 7FF0000000000000 7F800000
5897 ** -INF FFF0000000000000 FF800000
5898 ** NaN 7FF0000000000001 7F800001
5899 ** to to
5900 ** 7FFFFFFFFFFFFFFF 7FFFFFFF
5901 ** and and
5902 ** FFF0000000000001 FF800001
5903 ** to to
5904 ** FFFFFFFFFFFFFFFF FFFFFFFF
5905 ** +OVER 7FEFFFFFFFFFFFFF 7F7FFFFF
5906 ** -OVER FFEFFFFFFFFFFFFF FF7FFFFF
5907 ** +UNDER 0010000000000000 00800000
5908 ** -UNDER 8010000000000000 80800000
5911 double
5912 double64_be_read (unsigned char *cptr)
5913 { int exponent, negative ;
5914 double dvalue ;
5916 negative = (cptr [0] & 0x80) ? 1 : 0 ;
5917 exponent = ((cptr [0] & 0x7F) << 4) | ((cptr [1] >> 4) & 0xF) ;
5919 /* Might not have a 64 bit long, so load the mantissa into a double. */
5920 dvalue = (((cptr [1] & 0xF) << 24) | (cptr [2] << 16) | (cptr [3] << 8) | cptr [4]) ;
5921 dvalue += ((cptr [5] << 16) | (cptr [6] << 8) | cptr [7]) / ((double) 0x1000000) ;
5923 if (exponent == 0 && dvalue == 0.0)
5924 return 0.0 ;
5926 dvalue += 0x10000000 ;
5928 exponent = exponent - 0x3FF ;
5930 dvalue = dvalue / ((double) 0x10000000) ;
5932 if (negative)
5933 dvalue *= -1 ;
5935 if (exponent > 0)
5936 dvalue *= (1 << exponent) ;
5937 else if (exponent < 0)
5938 dvalue /= (1 << abs (exponent)) ;
5940 return dvalue ;
5941 } /* double64_be_read */
5943 double
5944 double64_le_read (unsigned char *cptr)
5945 { int exponent, negative ;
5946 double dvalue ;
5948 negative = (cptr [7] & 0x80) ? 1 : 0 ;
5949 exponent = ((cptr [7] & 0x7F) << 4) | ((cptr [6] >> 4) & 0xF) ;
5951 /* Might not have a 64 bit long, so load the mantissa into a double. */
5952 dvalue = (((cptr [6] & 0xF) << 24) | (cptr [5] << 16) | (cptr [4] << 8) | cptr [3]) ;
5953 dvalue += ((cptr [2] << 16) | (cptr [1] << 8) | cptr [0]) / ((double) 0x1000000) ;
5955 if (exponent == 0 && dvalue == 0.0)
5956 return 0.0 ;
5958 dvalue += 0x10000000 ;
5960 exponent = exponent - 0x3FF ;
5962 dvalue = dvalue / ((double) 0x10000000) ;
5964 if (negative)
5965 dvalue *= -1 ;
5967 if (exponent > 0)
5968 dvalue *= (1 << exponent) ;
5969 else if (exponent < 0)
5970 dvalue /= (1 << abs (exponent)) ;
5972 return dvalue ;
5973 } /* double64_le_read */
5975 void
5976 double64_be_write (double in, unsigned char *out)
5977 { int exponent, mantissa ;
5979 memset (out, 0, sizeof (double)) ;
5981 if (in == 0.0)
5982 return ;
5984 if (in < 0.0)
5985 { in *= -1.0 ;
5986 out [0] |= 0x80 ;
5989 in = frexp (in, &exponent) ;
5991 exponent += 1022 ;
5993 out [0] |= (exponent >> 4) & 0x7F ;
5994 out [1] |= (exponent << 4) & 0xF0 ;
5996 in *= 0x20000000 ;
5997 mantissa = lrint (floor (in)) ;
5999 out [1] |= (mantissa >> 24) & 0xF ;
6000 out [2] = (mantissa >> 16) & 0xFF ;
6001 out [3] = (mantissa >> 8) & 0xFF ;
6002 out [4] = mantissa & 0xFF ;
6004 in = fmod (in, 1.0) ;
6005 in *= 0x1000000 ;
6006 mantissa = lrint (floor (in)) ;
6008 out [5] = (mantissa >> 16) & 0xFF ;
6009 out [6] = (mantissa >> 8) & 0xFF ;
6010 out [7] = mantissa & 0xFF ;
6012 return ;
6013 } /* double64_be_write */
6015 void
6016 double64_le_write (double in, unsigned char *out)
6017 { int exponent, mantissa ;
6019 memset (out, 0, sizeof (double)) ;
6021 if (in == 0.0)
6022 return ;
6024 if (in < 0.0)
6025 { in *= -1.0 ;
6026 out [7] |= 0x80 ;
6029 in = frexp (in, &exponent) ;
6031 exponent += 1022 ;
6033 out [7] |= (exponent >> 4) & 0x7F ;
6034 out [6] |= (exponent << 4) & 0xF0 ;
6036 in *= 0x20000000 ;
6037 mantissa = lrint (floor (in)) ;
6039 out [6] |= (mantissa >> 24) & 0xF ;
6040 out [5] = (mantissa >> 16) & 0xFF ;
6041 out [4] = (mantissa >> 8) & 0xFF ;
6042 out [3] = mantissa & 0xFF ;
6044 in = fmod (in, 1.0) ;
6045 in *= 0x1000000 ;
6046 mantissa = lrint (floor (in)) ;
6048 out [2] = (mantissa >> 16) & 0xFF ;
6049 out [1] = (mantissa >> 8) & 0xFF ;
6050 out [0] = mantissa & 0xFF ;
6052 return ;
6053 } /* double64_le_write */
6055 /*==============================================================================================
6056 ** Private functions.
6059 static void
6060 double64_peak_update (SF_PRIVATE *psf, double *buffer, int count, int indx)
6061 { int chan ;
6062 int k, position ;
6063 float fmaxval ;
6065 for (chan = 0 ; chan < psf->sf.channels ; chan++)
6066 { fmaxval = fabs (buffer [chan]) ;
6067 position = 0 ;
6068 for (k = chan ; k < count ; k += psf->sf.channels)
6069 if (fmaxval < fabs (buffer [k]))
6070 { fmaxval = fabs (buffer [k]) ;
6071 position = k ;
6074 if (fmaxval > psf->pchunk->peaks [chan].value)
6075 { psf->pchunk->peaks [chan].value = fmaxval ;
6076 psf->pchunk->peaks [chan].position = psf->write_current + indx + (position / psf->sf.channels) ;
6080 return ;
6081 } /* double64_peak_update */
6083 static int
6084 double64_get_capability (SF_PRIVATE *psf)
6085 { union
6086 { double d ;
6087 int i [2] ;
6088 unsigned char c [8] ;
6089 } data ;
6091 data.d = 1.234567890123456789 ; /* Some abitrary value. */
6093 if (! psf->ieee_replace)
6094 { /* If this test is true ints and floats are compatible and little endian. */
6095 if (data.i [0] == 0x428c59fb && data.i [1] == 0x3ff3c0ca &&
6096 data.c [0] == 0xfb && data.c [2] == 0x8c && data.c [4] == 0xca && data.c [6] == 0xf3)
6097 return DOUBLE_CAN_RW_LE ;
6099 /* If this test is true ints and floats are compatible and big endian. */
6100 if ((data.i [0] == 0x3ff3c0ca && data.i [1] == 0x428c59fb) &&
6101 (data.c [0] == 0x3f && data.c [2] == 0xc0 && data.c [4] == 0x42 && data.c [6] == 0x59))
6102 return DOUBLE_CAN_RW_BE ;
6105 /* Doubles are broken. Don't expect reading or writing to be fast. */
6106 psf_log_printf (psf, "Using IEEE replacement code for double.\n") ;
6108 return (CPU_IS_LITTLE_ENDIAN) ? DOUBLE_BROKEN_LE : DOUBLE_BROKEN_BE ;
6109 } /* double64_get_capability */
6111 /*----------------------------------------------------------------------------------------------
6115 static sf_count_t
6116 host_read_d2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
6117 { int bufferlen, readcount, thisread ;
6118 sf_count_t total = 0 ;
6120 bufferlen = sizeof (psf->buffer) / sizeof (double) ;
6122 while (len > 0)
6123 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
6124 thisread = psf_fread (psf->buffer, sizeof (double), readcount, psf) ;
6126 if (psf->float_endswap == SF_TRUE)
6127 endswap_long_array ((long*) psf->buffer, readcount) ;
6129 d2s_array ((double*) (psf->buffer), thisread, ptr + total) ;
6130 total += thisread ;
6131 len -= thisread ;
6132 if (thisread < readcount)
6133 break ;
6136 return total ;
6137 } /* host_read_d2s */
6139 static sf_count_t
6140 host_read_d2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
6141 { int bufferlen, readcount, thisread ;
6142 sf_count_t total = 0 ;
6144 bufferlen = sizeof (psf->buffer) / sizeof (double) ;
6146 while (len > 0)
6147 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
6148 thisread = psf_fread (psf->buffer, sizeof (double), readcount, psf) ;
6150 if (psf->float_endswap == SF_TRUE)
6151 endswap_long_array ((long*) psf->buffer, readcount) ;
6153 d2i_array ((double*) (psf->buffer), thisread, ptr + total) ;
6154 total += thisread ;
6155 len -= thisread ;
6156 if (thisread < readcount)
6157 break ;
6160 return total ;
6161 } /* host_read_d2i */
6163 static sf_count_t
6164 host_read_d2f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
6165 { int bufferlen, readcount, thisread ;
6166 sf_count_t total = 0 ;
6168 bufferlen = sizeof (psf->buffer) / sizeof (double) ;
6170 while (len > 0)
6171 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
6172 thisread = psf_fread (psf->buffer, sizeof (double), readcount, psf) ;
6174 if (psf->float_endswap == SF_TRUE)
6175 endswap_long_array ((long*) psf->buffer, readcount) ;
6177 double64d2f_array ((double*) (psf->buffer), thisread, ptr + total) ;
6178 total += thisread ;
6179 len -= thisread ;
6180 if (thisread < readcount)
6181 break ;
6184 return total ;
6185 } /* host_read_d2f */
6187 static sf_count_t
6188 host_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
6189 { int bufferlen, readcount, thisread ;
6190 sf_count_t total = 0 ;
6192 if (psf->float_endswap != SF_TRUE)
6193 return psf_fread (ptr, sizeof (double), len, psf) ;
6195 bufferlen = sizeof (psf->buffer) / sizeof (double) ;
6197 while (len > 0)
6198 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
6199 thisread = psf_fread (psf->buffer, sizeof (double), readcount, psf) ;
6201 endswap_long_copy ((long*) (ptr + total), (long*) psf->buffer, thisread) ;
6203 total += thisread ;
6204 len -= thisread ;
6205 if (thisread < readcount)
6206 break ;
6209 return total ;
6210 } /* host_read_d */
6212 static sf_count_t
6213 host_write_s2d (SF_PRIVATE *psf, short *ptr, sf_count_t len)
6214 { int bufferlen, writecount, thiswrite ;
6215 sf_count_t total = 0 ;
6217 bufferlen = sizeof (psf->buffer) / sizeof (double) ;
6219 while (len > 0)
6220 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
6222 s2d_array (ptr + total, (double*) (psf->buffer), writecount) ;
6224 if (psf->has_peak)
6225 double64_peak_update (psf, (double*) (psf->buffer), writecount, (int) (total / psf->sf.channels)) ;
6227 if (psf->float_endswap == SF_TRUE)
6228 endswap_long_array ((long*) psf->buffer, writecount) ;
6230 thiswrite = psf_fwrite (psf->buffer, sizeof (double), writecount, psf) ;
6231 total += thiswrite ;
6232 len -= thiswrite ;
6233 if (thiswrite < writecount)
6234 break ;
6237 return total ;
6238 } /* host_write_s2d */
6240 static sf_count_t
6241 host_write_i2d (SF_PRIVATE *psf, int *ptr, sf_count_t len)
6242 { int bufferlen, writecount, thiswrite ;
6243 sf_count_t total = 0 ;
6245 bufferlen = sizeof (psf->buffer) / sizeof (double) ;
6247 while (len > 0)
6248 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
6249 i2d_array (ptr + total, (double*) (psf->buffer), writecount) ;
6251 if (psf->has_peak)
6252 double64_peak_update (psf, (double*) (psf->buffer), writecount, (int) (total / psf->sf.channels)) ;
6254 if (psf->float_endswap == SF_TRUE)
6255 endswap_long_array ((long*) psf->buffer, writecount) ;
6257 thiswrite = psf_fwrite (psf->buffer, sizeof (double), writecount, psf) ;
6258 total += thiswrite ;
6259 len -= thiswrite ;
6260 if (thiswrite < writecount)
6261 break ;
6264 return total ;
6265 } /* host_write_i2d */
6267 static sf_count_t
6268 host_write_f2d (SF_PRIVATE *psf, float *ptr, sf_count_t len)
6269 { int bufferlen, writecount, thiswrite ;
6270 sf_count_t total = 0 ;
6272 bufferlen = sizeof (psf->buffer) / sizeof (double) ;
6274 while (len > 0)
6275 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
6276 double64f2d_array (ptr + total, (double*) (psf->buffer), writecount) ;
6278 if (psf->has_peak)
6279 double64_peak_update (psf, (double*) (psf->buffer), writecount, (int) (total / psf->sf.channels)) ;
6281 if (psf->float_endswap == SF_TRUE)
6282 endswap_long_array ((long*) psf->buffer, writecount) ;
6284 thiswrite = psf_fwrite (psf->buffer, sizeof (double), writecount, psf) ;
6285 total += thiswrite ;
6286 len -= thiswrite ;
6287 if (thiswrite < writecount)
6288 break ;
6291 return total ;
6292 } /* host_write_f2d */
6294 static sf_count_t
6295 host_write_d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
6296 { int bufferlen, writecount, thiswrite ;
6297 sf_count_t total = 0 ;
6299 if (psf->has_peak)
6300 double64_peak_update (psf, ptr, len, 0) ;
6302 if (psf->float_endswap != SF_TRUE)
6303 return psf_fwrite (ptr, sizeof (double), len, psf) ;
6305 bufferlen = sizeof (psf->buffer) / sizeof (double) ;
6307 while (len > 0)
6308 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
6310 endswap_long_copy ((long*) psf->buffer, (long*) (ptr + total), writecount) ;
6312 thiswrite = psf_fwrite (psf->buffer, sizeof (double), writecount, psf) ;
6313 total += thiswrite ;
6314 len -= thiswrite ;
6315 if (thiswrite < writecount)
6316 break ;
6319 return total ;
6320 } /* host_write_d */
6322 /*=======================================================================================
6325 static void
6326 d2s_array (double *src, unsigned int count, short *dest)
6327 { while (count)
6328 { count -- ;
6329 dest [count] = lrint (src [count]) ;
6331 } /* d2s_array */
6333 static void
6334 d2i_array (double *src, unsigned int count, int *dest)
6335 { while (count)
6336 { count -- ;
6337 dest [count] = lrint (src [count]) ;
6339 } /* d2i_array */
6341 static void
6342 double64d2f_array (double *src, unsigned int count, float *dest)
6343 { while (count)
6344 { count -- ;
6345 dest [count] = src [count] ;
6347 } /* double64d2f_array */
6349 static void
6350 s2d_array (short *src, double *dest, unsigned int count)
6351 { while (count)
6352 { count -- ;
6353 dest [count] = src [count] ;
6356 } /* s2d_array */
6358 static void
6359 i2d_array (int *src, double *dest, unsigned int count)
6360 { while (count)
6361 { count -- ;
6362 dest [count] = src [count] ;
6364 } /* i2d_array */
6366 static void
6367 double64f2d_array (float *src, double *dest, unsigned int count)
6368 { while (count)
6369 { count -- ;
6370 dest [count] = src [count] ;
6372 } /* double64f2d_array */
6374 /*=======================================================================================
6377 static sf_count_t
6378 replace_read_d2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
6379 { int bufferlen, readcount, thisread ;
6380 sf_count_t total = 0 ;
6382 bufferlen = sizeof (psf->buffer) / sizeof (double) ;
6384 while (len > 0)
6385 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
6386 thisread = psf_fread (psf->buffer, sizeof (double), readcount, psf) ;
6388 if (psf->float_endswap == SF_TRUE)
6389 endswap_long_array ((long*) psf->buffer, readcount) ;
6391 d2bd_read ((double *) (psf->buffer), readcount) ;
6393 d2s_array ((double*) (psf->buffer), thisread, ptr + total) ;
6394 total += thisread ;
6395 if (thisread < readcount)
6396 break ;
6397 len -= thisread ;
6400 return total ;
6401 } /* replace_read_d2s */
6403 static sf_count_t
6404 replace_read_d2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
6405 { int bufferlen, readcount, thisread ;
6406 sf_count_t total = 0 ;
6408 bufferlen = sizeof (psf->buffer) / sizeof (double) ;
6410 while (len > 0)
6411 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
6412 thisread = psf_fread (psf->buffer, sizeof (double), readcount, psf) ;
6414 if (psf->float_endswap == SF_TRUE)
6415 endswap_long_array ((long*) psf->buffer, readcount) ;
6417 d2bd_read ((double *) (psf->buffer), readcount) ;
6419 d2i_array ((double*) (psf->buffer), thisread, ptr + total) ;
6420 total += thisread ;
6421 if (thisread < readcount)
6422 break ;
6423 len -= thisread ;
6426 return total ;
6427 } /* replace_read_d2i */
6429 static sf_count_t
6430 replace_read_d2f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
6431 { int bufferlen, readcount, thisread ;
6432 sf_count_t total = 0 ;
6434 bufferlen = sizeof (psf->buffer) / sizeof (double) ;
6436 while (len > 0)
6437 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
6438 thisread = psf_fread (psf->buffer, sizeof (double), readcount, psf) ;
6440 if (psf->float_endswap == SF_TRUE)
6441 endswap_long_array ((long *) psf->buffer, readcount) ;
6443 d2bd_read ((double *) (psf->buffer), readcount) ;
6445 memcpy (ptr + total, psf->buffer, readcount * sizeof (double)) ;
6447 total += thisread ;
6448 if (thisread < readcount)
6449 break ;
6450 len -= thisread ;
6453 return total ;
6454 } /* replace_read_d2f */
6456 static sf_count_t
6457 replace_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
6458 { int bufferlen, readcount, thisread ;
6459 sf_count_t total = 0 ;
6461 /* FIXME : This is probably nowhere near optimal. */
6462 bufferlen = sizeof (psf->buffer) / sizeof (double) ;
6464 while (len > 0)
6465 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
6466 thisread = psf_fread (psf->buffer, sizeof (double), readcount, psf) ;
6468 if (psf->float_endswap == SF_TRUE)
6469 endswap_long_array ((long*) psf->buffer, thisread) ;
6471 d2bd_read ((double *) (psf->buffer), thisread) ;
6473 memcpy (ptr + total, psf->buffer, thisread * sizeof (double)) ;
6475 total += thisread ;
6476 if (thisread < readcount)
6477 break ;
6478 len -= thisread ;
6481 return total ;
6482 } /* replace_read_d */
6484 static sf_count_t
6485 replace_write_s2d (SF_PRIVATE *psf, short *ptr, sf_count_t len)
6486 { int writecount, bufferlen, thiswrite ;
6487 sf_count_t total = 0 ;
6489 bufferlen = sizeof (psf->buffer) / sizeof (double) ;
6491 while (len > 0)
6492 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
6493 s2d_array (ptr + total, (double *) (psf->buffer), writecount) ;
6495 if (psf->has_peak)
6496 double64_peak_update (psf, (double *) (psf->buffer), writecount, (int) (total / psf->sf.channels)) ;
6498 bd2d_write ((double *) (psf->buffer), writecount) ;
6500 if (psf->float_endswap == SF_TRUE)
6501 endswap_long_array ((long*) psf->buffer, writecount) ;
6503 thiswrite = psf_fwrite (psf->buffer, sizeof (double), writecount, psf) ;
6504 total += thiswrite ;
6505 if (thiswrite < writecount)
6506 break ;
6507 len -= thiswrite ;
6510 return total ;
6511 } /* replace_write_s2d */
6513 static sf_count_t
6514 replace_write_i2d (SF_PRIVATE *psf, int *ptr, sf_count_t len)
6515 { int writecount, bufferlen, thiswrite ;
6516 sf_count_t total = 0 ;
6518 bufferlen = sizeof (psf->buffer) / sizeof (double) ;
6520 while (len > 0)
6521 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
6522 i2d_array (ptr + total, (double*) (psf->buffer), writecount) ;
6524 if (psf->has_peak)
6525 double64_peak_update (psf, (double *) (psf->buffer), writecount, (int) (total / psf->sf.channels)) ;
6527 bd2d_write ((double *) (psf->buffer), writecount) ;
6529 if (psf->float_endswap == SF_TRUE)
6530 endswap_long_array ((long*) psf->buffer, writecount) ;
6532 thiswrite = psf_fwrite (psf->buffer, sizeof (double), writecount, psf) ;
6533 total += thiswrite ;
6534 if (thiswrite < writecount)
6535 break ;
6536 len -= thiswrite ;
6539 return total ;
6540 } /* replace_write_i2d */
6542 static sf_count_t
6543 replace_write_f2d (SF_PRIVATE *psf, float *ptr, sf_count_t len)
6544 { int writecount, bufferlen, thiswrite ;
6545 sf_count_t total = 0 ;
6547 bufferlen = sizeof (psf->buffer) / sizeof (double) ;
6549 while (len > 0)
6550 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
6551 double64f2d_array (ptr + total, (double*) (psf->buffer), writecount) ;
6553 bd2d_write ((double *) (psf->buffer), writecount) ;
6555 if (psf->float_endswap == SF_TRUE)
6556 endswap_long_array ((long*) psf->buffer, writecount) ;
6558 thiswrite = psf_fwrite (psf->buffer, sizeof (double), writecount, psf) ;
6559 total += thiswrite ;
6560 if (thiswrite < writecount)
6561 break ;
6562 len -= thiswrite ;
6565 return total ;
6566 } /* replace_write_f2d */
6568 static sf_count_t
6569 replace_write_d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
6570 { int writecount, bufferlen, thiswrite ;
6571 sf_count_t total = 0 ;
6573 /* FIXME : This is probably nowhere near optimal. */
6574 if (psf->has_peak)
6575 double64_peak_update (psf, ptr, len, 0) ;
6577 bufferlen = sizeof (psf->buffer) / sizeof (double) ;
6579 while (len > 0)
6580 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
6582 memcpy (psf->buffer, ptr + total, writecount * sizeof (double)) ;
6584 bd2d_write ((double *) (psf->buffer), writecount) ;
6586 if (psf->float_endswap == SF_TRUE)
6587 endswap_long_array ((long*) psf->buffer, writecount) ;
6589 thiswrite = psf_fwrite (psf->buffer, sizeof (double), writecount, psf) ;
6590 total += thiswrite ;
6591 if (thiswrite < writecount)
6592 break ;
6593 len -= thiswrite ;
6596 return total ;
6597 } /* replace_write_d */
6599 /*----------------------------------------------------------------------------------------------
6602 static void
6603 d2bd_read (double *buffer, int count)
6604 { while (count)
6605 { count -- ;
6606 buffer [count] = DOUBLE64_READ ((unsigned char *) (buffer + count)) ;
6608 } /* d2bd_read */
6610 static void
6611 bd2d_write (double *buffer, int count)
6612 { while (count)
6613 { count -- ;
6614 DOUBLE64_WRITE (buffer [count], (unsigned char*) (buffer + count)) ;
6616 } /* bd2d_write */
6619 ** Do not edit or modify anything in this comment block.
6620 ** The arch-tag line is a file identity tag for the GNU Arch
6621 ** revision control system.
6623 ** arch-tag: 4ee243b7-8c7a-469b-869c-e9aa0ee3b77f
6626 ** Copyright (C) 2002-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
6628 ** This program is free software; you can redistribute it and/or modify
6629 ** it under the terms of the GNU Lesser General Public License as published by
6630 ** the Free Software Foundation; either version 2.1 of the License, or
6631 ** (at your option) any later version.
6633 ** This program is distributed in the hope that it will be useful,
6634 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
6635 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6636 ** GNU Lesser General Public License for more details.
6638 ** You should have received a copy of the GNU Lesser General Public License
6639 ** along with this program; if not, write to the Free Software
6640 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
6643 #include <stdio.h>
6644 #include <fcntl.h>
6645 #include <string.h>
6646 #include <ctype.h>
6649 #if (ENABLE_EXPERIMENTAL_CODE == 0)
6652 dwd_open (SF_PRIVATE *psf)
6653 { if (psf)
6654 return SFE_UNIMPLEMENTED ;
6655 return (psf && 0) ;
6656 } /* dwd_open */
6658 #else
6660 /*------------------------------------------------------------------------------
6661 ** Macros to handle big/little endian issues.
6664 #define SFE_DWD_NO_DWD 1666
6665 #define SFE_DWD_BAND_BIT_WIDTH 1667
6666 #define SFE_DWD_COMPRESSION 1668
6668 #define DWD_IDENTIFIER "DiamondWare Digitized\n\0\x1a"
6669 #define DWD_IDENTIFIER_LEN 24
6671 #define DWD_HEADER_LEN 57
6673 /*------------------------------------------------------------------------------
6674 ** Typedefs.
6677 /*------------------------------------------------------------------------------
6678 ** Private static functions.
6681 static int dwd_read_header (SF_PRIVATE *psf) ;
6683 static int dwd_close (SF_PRIVATE *psf) ;
6685 /*------------------------------------------------------------------------------
6686 ** Public function.
6690 dwd_open (SF_PRIVATE *psf)
6691 { int subformat, error = 0 ;
6693 if (psf->mode == SFM_READ || (psf->mode == SFM_RDWR && psf->filelength > 0))
6694 { if ((error = dwd_read_header (psf)))
6695 return error ;
6698 if ((psf->sf.format & SF_FORMAT_TYPEMASK) != SF_FORMAT_DWD)
6699 return SFE_BAD_OPEN_FORMAT ;
6701 subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
6703 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
6705 /*-psf->endian = psf->sf.format & SF_FORMAT_ENDMASK ;
6706 if (CPU_IS_LITTLE_ENDIAN && psf->endian == SF_ENDIAN_CPU)
6707 psf->endian = SF_ENDIAN_LITTLE ;
6708 else if (psf->endian != SF_ENDIAN_LITTLE)
6709 psf->endian = SF_ENDIAN_BIG ;
6711 if (! (encoding = dwd_write_header (psf, SF_FALSE)))
6712 return psf->error ;
6714 psf->write_header = dwd_write_header ;
6718 psf->close = dwd_close ;
6720 /*-psf->blockwidth = psf->bytewidth * psf->sf.channels ;-*/
6722 return error ;
6723 } /* dwd_open */
6725 /*------------------------------------------------------------------------------
6728 static int
6729 dwd_close (SF_PRIVATE *psf)
6731 psf = psf ;
6733 return 0 ;
6734 } /* dwd_close */
6736 /* This struct contains all the fields of interest om the DWD header, but does not
6737 ** do so in the same order and layout as the actual file, header.
6738 ** No assumptions are made about the packing of this struct.
6740 typedef struct
6741 { unsigned char major, minor, compression, channels, bitwidth ;
6742 unsigned short srate, maxval ;
6743 unsigned int id, datalen, frames, offset ;
6744 } DWD_HEADER ;
6746 static int
6747 dwd_read_header (SF_PRIVATE *psf)
6748 { DWD_HEADER dwdh ;
6750 memset (psf->buffer, 0, sizeof (psf->buffer)) ;
6751 /* Set position to start of file to begin reading header. */
6752 psf_binheader_readf (psf, "pb", 0, psf->buffer, DWD_IDENTIFIER_LEN) ;
6754 if (memcmp (psf->buffer, DWD_IDENTIFIER, DWD_IDENTIFIER_LEN) != 0)
6755 return SFE_DWD_NO_DWD ;
6757 psf_log_printf (psf, "Read only : DiamondWare Digitized (.dwd)\n", psf->buffer) ;
6759 psf_binheader_readf (psf, "11", &dwdh.major, &dwdh.minor) ;
6760 psf_binheader_readf (psf, "e4j1", &dwdh.id, 1, &dwdh.compression) ;
6761 psf_binheader_readf (psf, "e211", &dwdh.srate, &dwdh.channels, &dwdh.bitwidth) ;
6762 psf_binheader_readf (psf, "e24", &dwdh.maxval, &dwdh.datalen) ;
6763 psf_binheader_readf (psf, "e44", &dwdh.frames, &dwdh.offset) ;
6765 psf_log_printf (psf, " Version Major : %d\n Version Minor : %d\n Unique ID : %08X\n",
6766 dwdh.major, dwdh.minor, dwdh.id) ;
6767 psf_log_printf (psf, " Compression : %d => ", dwdh.compression) ;
6769 if (dwdh.compression != 0)
6770 { psf_log_printf (psf, "Unsupported compression\n") ;
6771 return SFE_DWD_COMPRESSION ;
6773 else
6774 psf_log_printf (psf, "None\n") ;
6776 psf_log_printf (psf, " Sample Rate : %d\n Channels : %d\n"
6777 " Bit Width : %d\n",
6778 dwdh.srate, dwdh.channels, dwdh.bitwidth) ;
6780 switch (dwdh.bitwidth)
6781 { case 8 :
6782 psf->sf.format = SF_FORMAT_DWD | SF_FORMAT_PCM_S8 ;
6783 psf->bytewidth = 1 ;
6784 break ;
6786 case 16 :
6787 psf->sf.format = SF_FORMAT_DWD | SF_FORMAT_PCM_16 ;
6788 psf->bytewidth = 2 ;
6789 break ;
6791 default :
6792 psf_log_printf (psf, "*** Bad bit width %d\n", dwdh.bitwidth) ;
6793 return SFE_DWD_BAND_BIT_WIDTH ;
6796 if (psf->filelength != dwdh.offset + dwdh.datalen)
6797 { psf_log_printf (psf, " Data Length : %d (should be %D)\n", dwdh.datalen, psf->filelength - dwdh.offset) ;
6798 dwdh.datalen = (unsigned int) (psf->filelength - dwdh.offset) ;
6800 else
6801 psf_log_printf (psf, " Data Length : %d\n", dwdh.datalen) ;
6803 psf_log_printf (psf, " Max Value : %d\n", dwdh.maxval) ;
6804 psf_log_printf (psf, " Frames : %d\n", dwdh.frames) ;
6805 psf_log_printf (psf, " Data Offset : %d\n", dwdh.offset) ;
6807 psf->datalength = dwdh.datalen ;
6808 psf->dataoffset = dwdh.offset ;
6810 psf->endian = SF_ENDIAN_LITTLE ;
6812 psf->sf.samplerate = dwdh.srate ;
6813 psf->sf.channels = dwdh.channels ;
6814 psf->sf.sections = 1 ;
6816 return pcm_init (psf) ;
6817 } /* dwd_read_header */
6819 /*------------------------------------------------------------------------------
6822 #endif
6824 ** Do not edit or modify anything in this comment block.
6825 ** The arch-tag line is a file identity tag for the GNU Arch
6826 ** revision control system.
6828 ** arch-tag: a5e1d2a6-a840-4039-a0e7-e1a43eb05a4f
6831 ** Copyright (C) 2002-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
6833 ** This program is free software; you can redistribute it and/or modify
6834 ** it under the terms of the GNU Lesser General Public License as published by
6835 ** the Free Software Foundation; either version 2.1 of the License, or
6836 ** (at your option) any later version.
6838 ** This program is distributed in the hope that it will be useful,
6839 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
6840 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6841 ** GNU Lesser General Public License for more details.
6843 ** You should have received a copy of the GNU Lesser General Public License
6844 ** along with this program; if not, write to the Free Software
6845 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
6848 /*===========================================================================
6849 ** Delta Word Variable Width
6851 ** This decoder and encoder were implemented using information found in this
6852 ** document : http://home.swbell.net/rubywand/R011SNDFMTS.TXT
6854 ** According to the document, the algorithm "was invented 1991 by Magnus
6855 ** Lidstrom and is copyright 1993 by NuEdge Development".
6858 #include <stdio.h>
6859 #include <stdlib.h>
6860 #include <string.h>
6863 typedef struct
6864 { int dwm_maxsize, bit_width, max_delta, span ;
6865 int samplecount ;
6866 int bit_count, bits, last_delta_width, last_sample ;
6867 struct
6868 { int index, end ;
6869 unsigned char buffer [256] ;
6870 } b ;
6871 } DWVW_PRIVATE ;
6873 /*============================================================================================
6876 static sf_count_t dwvw_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
6877 static sf_count_t dwvw_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
6878 static sf_count_t dwvw_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
6879 static sf_count_t dwvw_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
6881 static sf_count_t dwvw_write_s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
6882 static sf_count_t dwvw_write_i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
6883 static sf_count_t dwvw_write_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
6884 static sf_count_t dwvw_write_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
6886 static sf_count_t dwvw_seek (SF_PRIVATE *psf, int mode, sf_count_t offset) ;
6887 static int dwvw_close (SF_PRIVATE *psf) ;
6889 static int dwvw_decode_data (SF_PRIVATE *psf, DWVW_PRIVATE *pdwvw, int *ptr, int len) ;
6890 static int dwvw_decode_load_bits (SF_PRIVATE *psf, DWVW_PRIVATE *pdwvw, int bit_count) ;
6892 static int dwvw_encode_data (SF_PRIVATE *psf, DWVW_PRIVATE *pdwvw, int *ptr, int len) ;
6893 static void dwvw_encode_store_bits (SF_PRIVATE *psf, DWVW_PRIVATE *pdwvw, int data, int new_bits) ;
6894 static void dwvw_read_reset (DWVW_PRIVATE *pdwvw) ;
6896 /*============================================================================================
6897 ** DWVW initialisation function.
6901 dwvw_init (SF_PRIVATE *psf, int bitwidth)
6902 { DWVW_PRIVATE *pdwvw ;
6904 if (bitwidth > 24)
6905 return SFE_DWVW_BAD_BITWIDTH ;
6907 if (psf->mode == SFM_RDWR)
6908 return SFE_BAD_MODE_RW ;
6910 if ((pdwvw = calloc (1, sizeof (DWVW_PRIVATE))) == NULL)
6911 return SFE_MALLOC_FAILED ;
6913 psf->fdata = (void*) pdwvw ;
6915 pdwvw->bit_width = bitwidth ;
6916 pdwvw->dwm_maxsize = bitwidth / 2 ;
6917 pdwvw->max_delta = 1 << (bitwidth - 1) ;
6918 pdwvw->span = 1 << bitwidth ;
6920 dwvw_read_reset (pdwvw) ;
6922 if (psf->mode == SFM_READ)
6923 { psf->read_short = dwvw_read_s ;
6924 psf->read_int = dwvw_read_i ;
6925 psf->read_float = dwvw_read_f ;
6926 psf->read_double = dwvw_read_d ;
6929 if (psf->mode == SFM_WRITE)
6930 { psf->write_short = dwvw_write_s ;
6931 psf->write_int = dwvw_write_i ;
6932 psf->write_float = dwvw_write_f ;
6933 psf->write_double = dwvw_write_d ;
6936 psf->seek = dwvw_seek ;
6937 psf->close = dwvw_close ;
6939 /* FIXME : This s bogus. */
6940 psf->sf.frames = SF_COUNT_MAX ;
6941 psf->datalength = psf->sf.frames ;
6942 /* EMXIF : This s bogus. */
6944 return 0 ;
6945 } /* dwvw_init */
6947 /*--------------------------------------------------------------------------------------------
6950 static int
6951 dwvw_close (SF_PRIVATE *psf)
6952 { DWVW_PRIVATE *pdwvw ;
6954 if (psf->fdata == NULL)
6955 return 0 ;
6956 pdwvw = (DWVW_PRIVATE*) psf->fdata ;
6958 if (psf->mode == SFM_WRITE)
6959 { static int last_values [12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } ;
6961 /* Write 8 zero samples to fully flush output. */
6962 dwvw_encode_data (psf, pdwvw, last_values, 12) ;
6964 /* Write the last buffer worth of data to disk. */
6965 psf_fwrite (pdwvw->b.buffer, 1, pdwvw->b.index, psf) ;
6967 if (psf->write_header)
6968 psf->write_header (psf, SF_TRUE) ;
6971 return 0 ;
6972 } /* dwvw_close */
6974 static sf_count_t
6975 dwvw_seek (SF_PRIVATE *psf, int mode, sf_count_t offset)
6976 { DWVW_PRIVATE *pdwvw ;
6978 mode = mode ;
6980 if (! psf->fdata)
6981 { psf->error = SFE_INTERNAL ;
6982 return ((sf_count_t) -1) ;
6985 pdwvw = (DWVW_PRIVATE*) psf->fdata ;
6987 if (offset == 0)
6988 { psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
6989 dwvw_read_reset (pdwvw) ;
6990 return 0 ;
6993 psf->error = SFE_BAD_SEEK ;
6994 return ((sf_count_t) -1) ;
6995 } /* dwvw_seek */
6998 /*==============================================================================
7001 static sf_count_t
7002 dwvw_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
7003 { DWVW_PRIVATE *pdwvw ;
7004 int *iptr ;
7005 int k, bufferlen, readcount = 0, count ;
7006 sf_count_t total = 0 ;
7008 if (! psf->fdata)
7009 return 0 ;
7010 pdwvw = (DWVW_PRIVATE*) psf->fdata ;
7012 iptr = (int*) psf->buffer ;
7013 bufferlen = SF_BUFFER_LEN / sizeof (int) ;
7014 while (len > 0)
7015 { readcount = (len >= bufferlen) ? bufferlen : len ;
7016 count = dwvw_decode_data (psf, pdwvw, iptr, readcount) ;
7017 for (k = 0 ; k < readcount ; k++)
7018 ptr [total + k] = iptr [k] >> 16 ;
7020 total += count ;
7021 len -= readcount ;
7022 if (count != readcount)
7023 break ;
7026 return total ;
7027 } /* dwvw_read_s */
7029 static sf_count_t
7030 dwvw_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
7031 { DWVW_PRIVATE *pdwvw ;
7032 int readcount, count ;
7033 sf_count_t total = 0 ;
7035 if (! psf->fdata)
7036 return 0 ;
7037 pdwvw = (DWVW_PRIVATE*) psf->fdata ;
7039 while (len > 0)
7040 { readcount = (len > 0x10000000) ? 0x10000000 : (int) len ;
7042 count = dwvw_decode_data (psf, pdwvw, ptr, readcount) ;
7044 total += count ;
7045 len -= count ;
7047 if (count != readcount)
7048 break ;
7051 return total ;
7052 } /* dwvw_read_i */
7054 static sf_count_t
7055 dwvw_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
7056 { DWVW_PRIVATE *pdwvw ;
7057 int *iptr ;
7058 int k, bufferlen, readcount = 0, count ;
7059 sf_count_t total = 0 ;
7060 float normfact ;
7062 if (! psf->fdata)
7063 return 0 ;
7064 pdwvw = (DWVW_PRIVATE*) psf->fdata ;
7066 normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x80000000) : 1.0 ;
7068 iptr = (int*) psf->buffer ;
7069 bufferlen = SF_BUFFER_LEN / sizeof (int) ;
7070 while (len > 0)
7071 { readcount = (len >= bufferlen) ? bufferlen : len ;
7072 count = dwvw_decode_data (psf, pdwvw, iptr, readcount) ;
7073 for (k = 0 ; k < readcount ; k++)
7074 ptr [total + k] = normfact * (float) (iptr [k]) ;
7076 total += count ;
7077 len -= readcount ;
7078 if (count != readcount)
7079 break ;
7082 return total ;
7083 } /* dwvw_read_f */
7085 static sf_count_t
7086 dwvw_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
7087 { DWVW_PRIVATE *pdwvw ;
7088 int *iptr ;
7089 int k, bufferlen, readcount = 0, count ;
7090 sf_count_t total = 0 ;
7091 double normfact ;
7093 if (! psf->fdata)
7094 return 0 ;
7095 pdwvw = (DWVW_PRIVATE*) psf->fdata ;
7097 normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x80000000) : 1.0 ;
7099 iptr = (int*) psf->buffer ;
7100 bufferlen = SF_BUFFER_LEN / sizeof (int) ;
7101 while (len > 0)
7102 { readcount = (len >= bufferlen) ? bufferlen : len ;
7103 count = dwvw_decode_data (psf, pdwvw, iptr, readcount) ;
7104 for (k = 0 ; k < readcount ; k++)
7105 ptr [total + k] = normfact * (double) (iptr [k]) ;
7107 total += count ;
7108 len -= readcount ;
7109 if (count != readcount)
7110 break ;
7113 return total ;
7114 } /* dwvw_read_d */
7116 static int
7117 dwvw_decode_data (SF_PRIVATE *psf, DWVW_PRIVATE *pdwvw, int *ptr, int len)
7118 { int count ;
7119 int delta_width_modifier, delta_width, delta_negative, delta, sample ;
7121 /* Restore state from last decode call. */
7122 delta_width = pdwvw->last_delta_width ;
7123 sample = pdwvw->last_sample ;
7125 for (count = 0 ; count < len ; count++)
7126 { /* If bit_count parameter is zero get the delta_width_modifier. */
7127 delta_width_modifier = dwvw_decode_load_bits (psf, pdwvw, -1) ;
7129 /* Check for end of input bit stream. Break loop if end. */
7130 if (delta_width_modifier < 0)
7131 break ;
7133 if (delta_width_modifier && dwvw_decode_load_bits (psf, pdwvw, 1))
7134 delta_width_modifier = - delta_width_modifier ;
7136 /* Calculate the current word width. */
7137 delta_width = (delta_width + delta_width_modifier + pdwvw->bit_width) % pdwvw->bit_width ;
7139 /* Load the delta. */
7140 delta = 0 ;
7141 if (delta_width)
7142 { delta = dwvw_decode_load_bits (psf, pdwvw, delta_width - 1) | (1 << (delta_width - 1)) ;
7143 delta_negative = dwvw_decode_load_bits (psf, pdwvw, 1) ;
7144 if (delta == pdwvw->max_delta - 1)
7145 delta += dwvw_decode_load_bits (psf, pdwvw, 1) ;
7146 if (delta_negative)
7147 delta = -delta ;
7150 /* Calculate the sample */
7151 sample += delta ;
7153 if (sample >= pdwvw->max_delta)
7154 sample -= pdwvw->span ;
7155 else if (sample < - pdwvw->max_delta)
7156 sample += pdwvw->span ;
7158 /* Store the sample justifying to the most significant bit. */
7159 ptr [count] = sample << (32 - pdwvw->bit_width) ;
7161 if (pdwvw->b.end == 0 && pdwvw->bit_count == 0)
7162 break ;
7165 pdwvw->last_delta_width = delta_width ;
7166 pdwvw->last_sample = sample ;
7168 pdwvw->samplecount += count ;
7170 return count ;
7171 } /* dwvw_decode_data */
7173 static int
7174 dwvw_decode_load_bits (SF_PRIVATE *psf, DWVW_PRIVATE *pdwvw, int bit_count)
7175 { int output = 0, get_dwm = SF_FALSE ;
7178 ** Depending on the value of parameter bit_count, either get the
7179 ** required number of bits (ie bit_count > 0) or the
7180 ** delta_width_modifier (otherwise).
7183 if (bit_count < 0)
7184 { get_dwm = SF_TRUE ;
7185 /* modify bit_count to ensure we have enought bits for finding dwm. */
7186 bit_count = pdwvw->dwm_maxsize ;
7189 /* Load bits in bit reseviour. */
7190 while (pdwvw->bit_count < bit_count)
7191 { if (pdwvw->b.index >= pdwvw->b.end)
7192 { pdwvw->b.end = psf_fread (pdwvw->b.buffer, 1, sizeof (pdwvw->b.buffer), psf) ;
7193 pdwvw->b.index = 0 ;
7196 /* Check for end of input stream. */
7197 if (bit_count < 8 && pdwvw->b.end == 0)
7198 return -1 ;
7200 pdwvw->bits = (pdwvw->bits << 8) ;
7202 if (pdwvw->b.index < pdwvw->b.end)
7203 { pdwvw->bits |= pdwvw->b.buffer [pdwvw->b.index] ;
7204 pdwvw->b.index ++ ;
7206 pdwvw->bit_count += 8 ;
7209 /* If asked to get bits do so. */
7210 if (! get_dwm)
7211 { output = (pdwvw->bits >> (pdwvw->bit_count - bit_count)) & ((1 << bit_count) - 1) ;
7212 pdwvw->bit_count -= bit_count ;
7213 return output ;
7216 /* Otherwise must have been asked to get delta_width_modifier. */
7217 while (output < (pdwvw->dwm_maxsize))
7218 { pdwvw->bit_count -= 1 ;
7219 if (pdwvw->bits & (1 << pdwvw->bit_count))
7220 break ;
7221 output += 1 ;
7224 return output ;
7225 } /* dwvw_decode_load_bits */
7227 static void
7228 dwvw_read_reset (DWVW_PRIVATE *pdwvw)
7229 { pdwvw->samplecount = 0 ;
7230 pdwvw->b.index = 0 ;
7231 pdwvw->b.end = 0 ;
7232 pdwvw->bit_count = 0 ;
7233 pdwvw->bits = 0 ;
7234 pdwvw->last_delta_width = 0 ;
7235 pdwvw->last_sample = 0 ;
7236 } /* dwvw_read_reset */
7238 static void
7239 dwvw_encode_store_bits (SF_PRIVATE *psf, DWVW_PRIVATE *pdwvw, int data, int new_bits)
7240 { int byte ;
7242 /* Shift the bits into the resevoir. */
7243 pdwvw->bits = (pdwvw->bits << new_bits) | (data & ((1 << new_bits) - 1)) ;
7244 pdwvw->bit_count += new_bits ;
7246 /* Transfer bit to buffer. */
7247 while (pdwvw->bit_count >= 8)
7248 { byte = pdwvw->bits >> (pdwvw->bit_count - 8) ;
7249 pdwvw->bit_count -= 8 ;
7250 pdwvw->b.buffer [pdwvw->b.index] = byte & 0xFF ;
7251 pdwvw->b.index ++ ;
7254 if (pdwvw->b.index > SIGNED_SIZEOF (pdwvw->b.buffer) - 4)
7255 { psf_fwrite (pdwvw->b.buffer, 1, pdwvw->b.index, psf) ;
7256 pdwvw->b.index = 0 ;
7259 return ;
7260 } /* dwvw_encode_store_bits */
7262 #if 0
7263 /* Debigging routine. */
7264 static void
7265 dump_bits (DWVW_PRIVATE *pdwvw)
7266 { int k, mask ;
7268 for (k = 0 ; k < 10 && k < pdwvw->b.index ; k++)
7269 { mask = 0x80 ;
7270 while (mask)
7271 { putchar (mask & pdwvw->b.buffer [k] ? '1' : '0') ;
7272 mask >>= 1 ;
7274 putchar (' ') ;
7277 for (k = pdwvw->bit_count - 1 ; k >= 0 ; k --)
7278 putchar (pdwvw->bits & (1 << k) ? '1' : '0') ;
7280 putchar ('\n') ;
7281 } /* dump_bits */
7282 #endif
7284 #define HIGHEST_BIT(x,count) \
7285 { int y = x ; \
7286 (count) = 0 ; \
7287 while (y) \
7288 { (count) ++ ; \
7289 y >>= 1 ; \
7290 } ; \
7293 static int
7294 dwvw_encode_data (SF_PRIVATE *psf, DWVW_PRIVATE *pdwvw, int *ptr, int len)
7295 { int count ;
7296 int delta_width_modifier, delta, delta_negative, delta_width, extra_bit ;
7298 for (count = 0 ; count < len ; count++)
7299 { delta = (ptr [count] >> (32 - pdwvw->bit_width)) - pdwvw->last_sample ;
7301 /* Calculate extra_bit if needed. */
7302 extra_bit = -1 ;
7303 delta_negative = 0 ;
7304 if (delta < -pdwvw->max_delta)
7305 delta = pdwvw->max_delta + (delta % pdwvw->max_delta) ;
7306 else if (delta == -pdwvw->max_delta)
7307 { extra_bit = 1 ;
7308 delta_negative = 1 ;
7309 delta = pdwvw->max_delta - 1 ;
7311 else if (delta > pdwvw->max_delta)
7312 { delta_negative = 1 ;
7313 delta = pdwvw->span - delta ;
7314 delta = abs (delta) ;
7316 else if (delta == pdwvw->max_delta)
7317 { extra_bit = 1 ;
7318 delta = pdwvw->max_delta - 1 ;
7320 else if (delta < 0)
7321 { delta_negative = 1 ;
7322 delta = abs (delta) ;
7325 if (delta == pdwvw->max_delta - 1 && extra_bit == -1)
7326 extra_bit = 0 ;
7328 /* Find width in bits of delta */
7329 HIGHEST_BIT (delta, delta_width) ;
7331 /* Calculate the delta_width_modifier */
7332 delta_width_modifier = (delta_width - pdwvw->last_delta_width) % pdwvw->bit_width ;
7333 if (delta_width_modifier > pdwvw->dwm_maxsize)
7334 delta_width_modifier -= pdwvw->bit_width ;
7335 if (delta_width_modifier < -pdwvw->dwm_maxsize)
7336 delta_width_modifier += pdwvw->bit_width ;
7338 /* Write delta_width_modifier zeros, followed by terminating '1'. */
7339 dwvw_encode_store_bits (psf, pdwvw, 0, abs (delta_width_modifier)) ;
7340 if (abs (delta_width_modifier) != pdwvw->dwm_maxsize)
7341 dwvw_encode_store_bits (psf, pdwvw, 1, 1) ;
7343 /* Write delta_width_modifier sign. */
7344 if (delta_width_modifier < 0)
7345 dwvw_encode_store_bits (psf, pdwvw, 1, 1) ;
7346 if (delta_width_modifier > 0)
7347 dwvw_encode_store_bits (psf, pdwvw, 0, 1) ;
7349 /* Write delta and delta sign bit. */
7350 if (delta_width)
7351 { dwvw_encode_store_bits (psf, pdwvw, delta, abs (delta_width) - 1) ;
7352 dwvw_encode_store_bits (psf, pdwvw, (delta_negative ? 1 : 0), 1) ;
7355 /* Write extra bit!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
7356 if (extra_bit >= 0)
7357 dwvw_encode_store_bits (psf, pdwvw, extra_bit, 1) ;
7359 pdwvw->last_sample = ptr [count] >> (32 - pdwvw->bit_width) ;
7360 pdwvw->last_delta_width = delta_width ;
7363 pdwvw->samplecount += count ;
7365 return count ;
7366 } /* dwvw_encode_data */
7368 static sf_count_t
7369 dwvw_write_s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
7370 { DWVW_PRIVATE *pdwvw ;
7371 int *iptr ;
7372 int k, bufferlen, writecount = 0, count ;
7373 sf_count_t total = 0 ;
7375 if (! psf->fdata)
7376 return 0 ;
7377 pdwvw = (DWVW_PRIVATE*) psf->fdata ;
7379 iptr = (int*) psf->buffer ;
7380 bufferlen = SF_BUFFER_LEN / sizeof (int) ;
7381 while (len > 0)
7382 { writecount = (len >= bufferlen) ? bufferlen : len ;
7383 for (k = 0 ; k < writecount ; k++)
7384 iptr [k] = ptr [total + k] << 16 ;
7385 count = dwvw_encode_data (psf, pdwvw, iptr, writecount) ;
7387 total += count ;
7388 len -= writecount ;
7389 if (count != writecount)
7390 break ;
7393 return total ;
7394 } /* dwvw_write_s */
7396 static sf_count_t
7397 dwvw_write_i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
7398 { DWVW_PRIVATE *pdwvw ;
7399 int writecount, count ;
7400 sf_count_t total = 0 ;
7402 if (! psf->fdata)
7403 return 0 ;
7404 pdwvw = (DWVW_PRIVATE*) psf->fdata ;
7406 while (len > 0)
7407 { writecount = (len > 0x10000000) ? 0x10000000 : (int) len ;
7409 count = dwvw_encode_data (psf, pdwvw, ptr, writecount) ;
7411 total += count ;
7412 len -= count ;
7414 if (count != writecount)
7415 break ;
7418 return total ;
7419 } /* dwvw_write_i */
7421 static sf_count_t
7422 dwvw_write_f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
7423 { DWVW_PRIVATE *pdwvw ;
7424 int *iptr ;
7425 int k, bufferlen, writecount = 0, count ;
7426 sf_count_t total = 0 ;
7427 float normfact ;
7429 if (! psf->fdata)
7430 return 0 ;
7431 pdwvw = (DWVW_PRIVATE*) psf->fdata ;
7433 normfact = (psf->norm_float == SF_TRUE) ? (1.0 * 0x7FFFFFFF) : 1.0 ;
7435 iptr = (int*) psf->buffer ;
7436 bufferlen = SF_BUFFER_LEN / sizeof (short) ;
7437 while (len > 0)
7438 { writecount = (len >= bufferlen) ? bufferlen : len ;
7439 for (k = 0 ; k < writecount ; k++)
7440 iptr [k] = lrintf (normfact * ptr [total + k]) ;
7441 count = dwvw_encode_data (psf, pdwvw, iptr, writecount) ;
7443 total += count ;
7444 len -= writecount ;
7445 if (count != writecount)
7446 break ;
7449 return total ;
7450 } /* dwvw_write_f */
7452 static sf_count_t
7453 dwvw_write_d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
7454 { DWVW_PRIVATE *pdwvw ;
7455 int *iptr ;
7456 int k, bufferlen, writecount = 0, count ;
7457 sf_count_t total = 0 ;
7458 double normfact ;
7460 if (! psf->fdata)
7461 return 0 ;
7462 pdwvw = (DWVW_PRIVATE*) psf->fdata ;
7464 normfact = (psf->norm_double == SF_TRUE) ? (1.0 * 0x7FFFFFFF) : 1.0 ;
7466 iptr = (int*) psf->buffer ;
7467 bufferlen = SF_BUFFER_LEN / sizeof (short) ;
7468 while (len > 0)
7469 { writecount = (len >= bufferlen) ? bufferlen : len ;
7470 for (k = 0 ; k < writecount ; k++)
7471 iptr [k] = lrint (normfact * ptr [total + k]) ;
7472 count = dwvw_encode_data (psf, pdwvw, iptr, writecount) ;
7474 total += count ;
7475 len -= writecount ;
7476 if (count != writecount)
7477 break ;
7480 return total ;
7481 } /* dwvw_write_d */
7483 ** Do not edit or modify anything in this comment block.
7484 ** The arch-tag line is a file identity tag for the GNU Arch
7485 ** revision control system.
7487 ** arch-tag: 1ca09552-b01f-4d7f-9bcf-612f834fe41d
7490 ** Copyright (C) 2002-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
7491 ** Copyright (C) 2003 Ross Bencina <rbencina@iprimus.com.au>
7493 ** This program is free software; you can redistribute it and/or modify
7494 ** it under the terms of the GNU Lesser General Public License as published by
7495 ** the Free Software Foundation; either version 2.1 of the License, or
7496 ** (at your option) any later version.
7498 ** This program is distributed in the hope that it will be useful,
7499 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
7500 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7501 ** GNU Lesser General Public License for more details.
7503 ** You should have received a copy of the GNU Lesser General Public License
7504 ** along with this program; if not, write to the Free Software
7505 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
7509 ** This header file MUST be included before the others to ensure that
7510 ** large file support is enabled.
7514 #include <stdio.h>
7515 #include <stdlib.h>
7517 #ifdef HAVE_UNISTD_H
7518 #include <unistd.h>
7519 #endif
7521 #if (HAVE_DECL_S_IRGRP == 0)
7522 #include <sf_unistd.h>
7523 #endif
7525 #include <string.h>
7526 #include <fcntl.h>
7527 #include <errno.h>
7528 #include <sys/stat.h>
7530 #if (defined (__MWERKS__) && defined (macintosh))
7531 typedef int ssize_t ;
7532 #include <Files.h>
7533 #endif
7536 #define SENSIBLE_SIZE (0x40000000)
7538 static void psf_log_syserr (SF_PRIVATE *psf, int error) ;
7540 #if ((defined (WIN32) || defined (_WIN32)) == 0)
7542 /*------------------------------------------------------------------------------
7543 ** Win32 stuff at the bottom of the file. Unix and other sensible OSes here.
7547 psf_fopen (SF_PRIVATE *psf, const char *pathname, int open_mode)
7548 { int oflag, mode ;
7551 ** Sanity check. If everything is OK, this test and the printfs will
7552 ** be optimised out. This is meant to catch the problems caused by
7553 ** "config.h" being included after <stdio.h>.
7555 if (sizeof (off_t) != sizeof (sf_count_t))
7556 { puts ("\n\n*** Fatal error : sizeof (off_t) != sizeof (sf_count_t)") ;
7557 puts ("*** This means that libsndfile was not configured correctly.\n") ;
7558 exit (1) ;
7561 switch (open_mode)
7562 { case SFM_READ :
7563 oflag = O_RDONLY ;
7564 mode = 0 ;
7565 break ;
7567 case SFM_WRITE :
7568 oflag = O_WRONLY | O_CREAT | O_TRUNC ;
7569 mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH ;
7570 break ;
7572 case SFM_RDWR :
7573 oflag = O_RDWR | O_CREAT ;
7574 mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH ;
7575 break ;
7577 default :
7578 psf->error = SFE_BAD_OPEN_MODE ;
7579 return psf->error ;
7580 break ;
7583 #if defined (__CYGWIN__)
7584 oflag |= O_BINARY ;
7585 #endif
7587 if (mode == 0)
7588 psf->filedes = open (pathname, oflag) ;
7589 else
7590 psf->filedes = open (pathname, oflag, mode) ;
7592 if (psf->filedes == -1)
7593 psf_log_syserr (psf, errno) ;
7595 psf->mode = open_mode ;
7597 return psf->error ;
7598 } /* psf_fopen */
7601 psf_set_stdio (SF_PRIVATE *psf, int mode)
7602 { int error = 0 ;
7604 switch (mode)
7605 { case SFM_RDWR :
7606 error = SFE_OPEN_PIPE_RDWR ;
7607 break ;
7609 case SFM_READ :
7610 psf->filedes = 0 ;
7611 break ;
7613 case SFM_WRITE :
7614 psf->filedes = 1 ;
7615 break ;
7617 default :
7618 error = SFE_BAD_OPEN_MODE ;
7619 break ;
7621 psf->filelength = 0 ;
7623 return error ;
7624 } /* psf_set_stdio */
7626 void
7627 psf_set_file (SF_PRIVATE *psf, int fd)
7628 { psf->filedes = fd ;
7629 } /* psf_set_file */
7632 psf_filedes_valid (SF_PRIVATE *psf)
7633 { return (psf->filedes >= 0) ? SF_TRUE : SF_FALSE ;
7634 } /* psf_set_file */
7636 sf_count_t
7637 psf_fseek (SF_PRIVATE *psf, sf_count_t offset, int whence)
7638 { sf_count_t new_position ;
7640 switch (whence)
7641 { case SEEK_SET :
7642 offset += psf->fileoffset ;
7643 break ;
7645 case SEEK_END :
7646 if (psf->mode == SFM_WRITE)
7647 { new_position = lseek (psf->filedes, offset, whence) ;
7649 if (new_position < 0)
7650 psf_log_syserr (psf, errno) ;
7652 return new_position - psf->fileoffset ;
7655 /* Transform SEEK_END into a SEEK_SET, ie find the file
7656 ** length add the requested offset (should be <= 0) to
7657 ** get the offset wrt the start of file.
7659 whence = SEEK_SET ;
7660 offset = lseek (psf->filedes, 0, SEEK_END) + offset ;
7661 break ;
7663 default :
7664 /* No need to do anything about SEEK_CUR. */
7665 break ;
7668 new_position = lseek (psf->filedes, offset, whence) ;
7670 if (new_position < 0)
7671 psf_log_syserr (psf, errno) ;
7673 new_position -= psf->fileoffset ;
7675 return new_position ;
7676 } /* psf_fseek */
7678 sf_count_t
7679 psf_fread (void *ptr, sf_count_t bytes, sf_count_t items, SF_PRIVATE *psf)
7680 { sf_count_t total = 0 ;
7681 ssize_t count ;
7683 items *= bytes ;
7685 /* Do this check after the multiplication above. */
7686 if (items <= 0)
7687 return 0 ;
7689 while (items > 0)
7690 { /* Break the writes down to a sensible size. */
7691 count = (items > SENSIBLE_SIZE) ? SENSIBLE_SIZE : (ssize_t) items ;
7693 count = read (psf->filedes, ((char*) ptr) + total, (size_t) count) ;
7695 if (count == -1)
7696 { if (errno == EINTR)
7697 continue ;
7699 psf_log_syserr (psf, errno) ;
7700 break ;
7703 if (count == 0)
7704 break ;
7706 total += count ;
7707 items -= count ;
7710 if (psf->is_pipe)
7711 psf->pipeoffset += total ;
7713 return total / bytes ;
7714 } /* psf_fread */
7716 sf_count_t
7717 psf_fwrite (void *ptr, sf_count_t bytes, sf_count_t items, SF_PRIVATE *psf)
7718 { sf_count_t total = 0 ;
7719 ssize_t count ;
7721 items *= bytes ;
7723 /* Do this check after the multiplication above. */
7724 if (items <= 0)
7725 return 0 ;
7727 while (items > 0)
7728 { /* Break the writes down to a sensible size. */
7729 count = (items > SENSIBLE_SIZE) ? SENSIBLE_SIZE : items ;
7731 count = write (psf->filedes, ((char*) ptr) + total, count) ;
7733 if (count == -1)
7734 { if (errno == EINTR)
7735 continue ;
7737 psf_log_syserr (psf, errno) ;
7738 break ;
7741 if (count == 0)
7742 break ;
7744 total += count ;
7745 items -= count ;
7748 if (psf->is_pipe)
7749 psf->pipeoffset += total ;
7751 return total / bytes ;
7752 } /* psf_fwrite */
7754 sf_count_t
7755 psf_ftell (SF_PRIVATE *psf)
7756 { sf_count_t pos ;
7758 if (psf->is_pipe)
7759 return psf->pipeoffset ;
7761 pos = lseek (psf->filedes, 0, SEEK_CUR) ;
7763 if (pos == ((sf_count_t) -1))
7764 { psf_log_syserr (psf, errno) ;
7765 return -1 ;
7768 return pos - psf->fileoffset ;
7769 } /* psf_ftell */
7772 psf_fclose (SF_PRIVATE *psf)
7773 { int retval ;
7775 #if ((defined (__MWERKS__) && defined (macintosh)) == 0)
7776 /* Must be MacOS9 which doesn't have fsync(). */
7777 if (fsync (psf->filedes) == -1 && errno == EBADF)
7778 return 0 ;
7779 #endif
7781 if (psf->do_not_close_descriptor)
7782 { psf->filedes = -1 ;
7783 return 0 ;
7786 while ((retval = close (psf->filedes)) == -1 && errno == EINTR)
7787 /* Do nothing. */ ;
7789 if (retval == -1)
7790 psf_log_syserr (psf, errno) ;
7792 psf->filedes = -1 ;
7794 return retval ;
7795 } /* psf_fclose */
7797 sf_count_t
7798 psf_fgets (char *buffer, sf_count_t bufsize, SF_PRIVATE *psf)
7799 { sf_count_t k = 0 ;
7800 sf_count_t count ;
7802 while (k < bufsize - 1)
7803 { count = read (psf->filedes, &(buffer [k]), 1) ;
7805 if (count == -1)
7806 { if (errno == EINTR)
7807 continue ;
7809 psf_log_syserr (psf, errno) ;
7810 break ;
7813 if (count == 0 || buffer [k++] == '\n')
7814 break ;
7817 buffer [k] = 0 ;
7819 return k ;
7820 } /* psf_fgets */
7823 psf_is_pipe (SF_PRIVATE *psf)
7824 { struct stat statbuf ;
7826 if (fstat (psf->filedes, &statbuf) == -1)
7827 { psf_log_syserr (psf, errno) ;
7828 /* Default to maximum safety. */
7829 return SF_TRUE ;
7832 if (S_ISFIFO (statbuf.st_mode) || S_ISSOCK (statbuf.st_mode))
7833 return SF_TRUE ;
7835 return SF_FALSE ;
7836 } /* psf_is_pipe */
7838 sf_count_t
7839 psf_get_filelen (SF_PRIVATE *psf)
7840 { struct stat statbuf ;
7841 sf_count_t filelen ;
7844 ** Sanity check.
7845 ** If everything is OK, this will be optimised out.
7847 if (sizeof (statbuf.st_size) == 4 && sizeof (sf_count_t) == 8)
7848 return SFE_BAD_STAT_SIZE ;
7850 /* Cygwin seems to need this. */
7851 #if (defined (__CYGWIN__) && HAVE_FSYNC)
7852 fsync (psf->filedes) ;
7853 #endif
7855 if (fstat (psf->filedes, &statbuf) == -1)
7856 { psf_log_syserr (psf, errno) ;
7857 return (sf_count_t) -1 ;
7860 switch (psf->mode)
7861 { case SFM_WRITE :
7862 filelen = statbuf.st_size - psf->fileoffset ;
7863 break ;
7865 case SFM_READ :
7866 if (psf->fileoffset > 0 && psf->filelength > 0)
7867 filelen = psf->filelength ;
7868 else
7869 filelen = statbuf.st_size ;
7870 break ;
7872 case SFM_RDWR :
7874 ** Cannot open embedded files SFM_RDWR so we don't need to
7875 ** subtract psf->fileoffset. We already have the answer we
7876 ** need.
7878 filelen = statbuf.st_size ;
7879 break ;
7881 default :
7882 /* Shouldn't be here, so return error. */
7883 filelen = -1 ;
7886 return filelen ;
7887 } /* psf_get_filelen */
7890 psf_ftruncate (SF_PRIVATE *psf, sf_count_t len)
7891 { int retval ;
7893 /* Returns 0 on success, non-zero on failure. */
7894 if (len < 0)
7895 return -1 ;
7897 if ((sizeof (off_t) < sizeof (sf_count_t)) && len > 0x7FFFFFFF)
7898 return -1 ;
7900 #if (defined (__MWERKS__) && defined (macintosh))
7901 retval = FSSetForkSize (psf->filedes, fsFromStart, len) ;
7902 #else
7903 retval = ftruncate (psf->filedes, len) ;
7904 #endif
7906 if (retval == -1)
7907 psf_log_syserr (psf, errno) ;
7909 return retval ;
7910 } /* psf_ftruncate */
7913 static void
7914 psf_log_syserr (SF_PRIVATE *psf, int error)
7916 /* Only log an error if no error has been set yet. */
7917 if (psf->error == 0)
7918 { psf->error = SFE_SYSTEM ;
7919 LSF_SNPRINTF (psf->syserr, sizeof (psf->syserr), "System error : %s.", strerror (error)) ;
7922 return ;
7923 } /* psf_log_syserr */
7925 //XXX formerly OS_IS_WIN32
7926 #elif __PLATFORM_WIN32__
7928 /* Win32 file i/o functions implemented using native Win32 API */
7930 #include <windows.h>
7931 #include <io.h>
7933 #ifndef HAVE_SSIZE_T
7934 typedef long ssize_t ;
7935 #endif
7937 /* Win32 */ static void
7938 psf_log_syserr (SF_PRIVATE *psf, int error)
7939 { LPVOID lpMsgBuf ;
7941 /* Only log an error if no error has been set yet. */
7942 if (psf->error == 0)
7943 { psf->error = SFE_SYSTEM ;
7945 FormatMessage (
7946 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
7947 NULL,
7948 error,
7949 MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
7950 (LPTSTR) &lpMsgBuf,
7952 NULL
7955 LSF_SNPRINTF (psf->syserr, sizeof (psf->syserr), "System error : %s", lpMsgBuf) ;
7956 LocalFree (lpMsgBuf) ;
7959 return ;
7960 } /* psf_log_syserr */
7963 /* Win32 */ int
7964 psf_fopen (SF_PRIVATE *psf, const char *pathname, int open_mode)
7965 { DWORD dwDesiredAccess ;
7966 DWORD dwShareMode ;
7967 DWORD dwCreationDistribution ;
7968 HANDLE handle ;
7970 switch (open_mode)
7971 { case SFM_READ :
7972 dwDesiredAccess = GENERIC_READ ;
7973 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE ;
7974 dwCreationDistribution = OPEN_EXISTING ;
7975 break ;
7977 case SFM_WRITE :
7978 dwDesiredAccess = GENERIC_WRITE ;
7979 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE ;
7980 dwCreationDistribution = CREATE_ALWAYS ;
7981 break ;
7983 case SFM_RDWR :
7984 dwDesiredAccess = GENERIC_READ | GENERIC_WRITE ;
7985 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE ;
7986 dwCreationDistribution = OPEN_ALWAYS ;
7987 break ;
7989 default :
7990 psf->error = SFE_BAD_OPEN_MODE ;
7991 return psf->error ;
7994 handle = CreateFile (
7995 pathname, /* pointer to name of the file */
7996 dwDesiredAccess, /* access (read-write) mode */
7997 dwShareMode, /* share mode */
7998 0, /* pointer to security attributes */
7999 dwCreationDistribution, /* how to create */
8000 FILE_ATTRIBUTE_NORMAL, /* file attributes (could use FILE_FLAG_SEQUENTIAL_SCAN) */
8001 NULL /* handle to file with attributes to copy */
8004 if (handle == INVALID_HANDLE_VALUE)
8005 { psf_log_syserr (psf, GetLastError ()) ;
8006 return psf->error ;
8009 psf->filedes = (int) handle ;
8010 psf->mode = open_mode ;
8012 return psf->error ;
8013 } /* psf_fopen */
8015 /* Win32 */ int
8016 psf_set_stdio (SF_PRIVATE *psf, int mode)
8017 { HANDLE handle = NULL ;
8018 int error = 0 ;
8020 switch (mode)
8021 { case SFM_RDWR :
8022 error = SFE_OPEN_PIPE_RDWR ;
8023 break ;
8025 case SFM_READ :
8026 handle = GetStdHandle (STD_INPUT_HANDLE) ;
8027 psf->do_not_close_descriptor = 1 ;
8028 break ;
8030 case SFM_WRITE :
8031 handle = GetStdHandle (STD_OUTPUT_HANDLE) ;
8032 psf->do_not_close_descriptor = 1 ;
8033 break ;
8035 default :
8036 error = SFE_BAD_OPEN_MODE ;
8037 break ;
8040 psf->filedes = (int) handle ;
8041 psf->filelength = 0 ;
8043 return error ;
8044 } /* psf_set_stdio */
8046 /* Win32 */ void
8047 psf_set_file (SF_PRIVATE *psf, int fd)
8048 { HANDLE handle ;
8049 long osfhandle ;
8051 osfhandle = _get_osfhandle (fd) ;
8052 handle = (HANDLE) osfhandle ;
8054 if (GetFileType (handle) == FILE_TYPE_DISK)
8055 psf->filedes = (int) handle ;
8056 else
8057 psf->filedes = fd ;
8058 } /* psf_set_file */
8060 /* Win32 */ int
8061 psf_filedes_valid (SF_PRIVATE *psf)
8062 { return (((HANDLE) psf->filedes) != INVALID_HANDLE_VALUE) ? SF_TRUE : SF_FALSE ;
8063 } /* psf_set_file */
8065 /* Win32 */ sf_count_t
8066 psf_fseek (SF_PRIVATE *psf, sf_count_t offset, int whence)
8067 { sf_count_t new_position ;
8068 LONG lDistanceToMove, lDistanceToMoveHigh ;
8069 DWORD dwMoveMethod ;
8070 DWORD dwResult, dwError ;
8072 switch (whence)
8073 { case SEEK_SET :
8074 offset += psf->fileoffset ;
8075 dwMoveMethod = FILE_BEGIN ;
8076 break ;
8078 case SEEK_END :
8079 dwMoveMethod = FILE_END ;
8080 break ;
8082 default :
8083 dwMoveMethod = FILE_CURRENT ;
8084 break ;
8087 lDistanceToMove = (DWORD) (offset & 0xFFFFFFFF) ;
8088 #ifndef __WINDOWS_DS__
8089 lDistanceToMoveHigh = (DWORD) (0xFFFFFFFF & (offset >> 32)) ;
8090 #else
8091 lDistanceToMoveHigh = 0;
8092 #endif
8094 dwResult = SetFilePointer ((HANDLE) psf->filedes, lDistanceToMove, &lDistanceToMoveHigh, dwMoveMethod) ;
8096 if (dwResult == 0xFFFFFFFF)
8097 dwError = GetLastError () ;
8098 else
8099 dwError = NO_ERROR ;
8101 if (dwError != NO_ERROR)
8102 { psf_log_syserr (psf, dwError) ;
8103 return -1 ;
8106 new_position = (dwResult + ((__int64) lDistanceToMoveHigh << 32)) - psf->fileoffset ;
8108 return new_position ;
8109 } /* psf_fseek */
8111 /* Win32 */ sf_count_t
8112 psf_fread (void *ptr, sf_count_t bytes, sf_count_t items, SF_PRIVATE *psf)
8113 { sf_count_t total = 0 ;
8114 ssize_t count ;
8115 DWORD dwNumberOfBytesRead ;
8117 items *= bytes ;
8119 /* Do this check after the multiplication above. */
8120 if (items <= 0)
8121 return 0 ;
8123 while (items > 0)
8124 { /* Break the writes down to a sensible size. */
8125 count = (items > SENSIBLE_SIZE) ? SENSIBLE_SIZE : (ssize_t) items ;
8126 if( items == 16384 ) count = 1024;
8128 if (ReadFile ((HANDLE) psf->filedes, ((char*) ptr) + total, count, &dwNumberOfBytesRead, 0) == 0)
8129 { psf_log_syserr (psf, GetLastError ()) ;
8130 break ;
8132 else
8133 count = dwNumberOfBytesRead ;
8135 if (count == 0)
8136 break ;
8138 total += count ;
8139 items -= count ;
8142 if (psf->is_pipe)
8143 psf->pipeoffset += total ;
8145 return total / bytes ;
8146 } /* psf_fread */
8148 /* Win32 */ sf_count_t
8149 psf_fwrite (void *ptr, sf_count_t bytes, sf_count_t items, SF_PRIVATE *psf)
8150 { sf_count_t total = 0 ;
8151 ssize_t count ;
8152 DWORD dwNumberOfBytesWritten ;
8154 items *= bytes ;
8156 /* Do this check after the multiplication above. */
8157 if (items <= 0)
8158 return 0 ;
8160 while (items > 0)
8161 { /* Break the writes down to a sensible size. */
8162 count = (items > SENSIBLE_SIZE) ? SENSIBLE_SIZE : (ssize_t) items ;
8164 if (WriteFile ((HANDLE) psf->filedes, ((char*) ptr) + total, count, &dwNumberOfBytesWritten, 0) == 0)
8165 { psf_log_syserr (psf, GetLastError ()) ;
8166 break ;
8168 else
8169 count = dwNumberOfBytesWritten ;
8171 if (count == 0)
8172 break ;
8174 total += count ;
8175 items -= count ;
8178 if (psf->is_pipe)
8179 psf->pipeoffset += total ;
8181 return total / bytes ;
8182 } /* psf_fwrite */
8184 /* Win32 */ sf_count_t
8185 psf_ftell (SF_PRIVATE *psf)
8186 { sf_count_t pos ;
8187 LONG lDistanceToMoveLow, lDistanceToMoveHigh ;
8188 DWORD dwResult, dwError ;
8190 if (psf->is_pipe)
8191 return psf->pipeoffset ;
8193 lDistanceToMoveLow = 0 ;
8194 lDistanceToMoveHigh = 0 ;
8196 dwResult = SetFilePointer ((HANDLE) psf->filedes, lDistanceToMoveLow, NULL, FILE_CURRENT) ;
8198 if (dwResult == 0xFFFFFFFF)
8199 dwError = GetLastError () ;
8200 else
8201 dwError = NO_ERROR ;
8203 if (dwError != NO_ERROR)
8204 { psf_log_syserr (psf, dwError) ;
8205 return -1 ;
8208 pos = (dwResult + ((__int64) lDistanceToMoveHigh << 32)) ;
8210 return pos - psf->fileoffset ;
8211 } /* psf_ftell */
8213 /* Win32 */ int
8214 psf_fclose (SF_PRIVATE *psf)
8215 { int retval = 0 ;
8217 if (psf->do_not_close_descriptor)
8218 { (HANDLE) psf->filedes = INVALID_HANDLE_VALUE ;
8219 return 0 ;
8222 if (CloseHandle ((HANDLE) psf->filedes) == 0)
8223 { retval = -1 ;
8224 psf_log_syserr (psf, GetLastError ()) ;
8227 psf->filedes = (int) INVALID_HANDLE_VALUE ;
8229 return retval ;
8230 } /* psf_fclose */
8232 /* Win32 */ sf_count_t
8233 psf_fgets (char *buffer, sf_count_t bufsize, SF_PRIVATE *psf)
8234 { sf_count_t k = 0 ;
8235 sf_count_t count ;
8236 DWORD dwNumberOfBytesRead ;
8238 while (k < bufsize - 1)
8239 { if (ReadFile ((HANDLE) psf->filedes, &(buffer [k]), 1, &dwNumberOfBytesRead, 0) == 0)
8240 { psf_log_syserr (psf, GetLastError ()) ;
8241 break ;
8243 else
8244 { count = dwNumberOfBytesRead ;
8245 /* note that we only check for '\n' not other line endings such as CRLF */
8246 if (count == 0 || buffer [k++] == '\n')
8247 break ;
8251 buffer [k] = '\0' ;
8253 return k ;
8254 } /* psf_fgets */
8256 /* Win32 */ int
8257 psf_is_pipe (SF_PRIVATE *psf)
8258 { if (GetFileType ((HANDLE) psf->filedes) == FILE_TYPE_DISK)
8259 return SF_FALSE ;
8261 /* Default to maximum safety. */
8262 return SF_TRUE ;
8263 } /* psf_is_pipe */
8265 /* Win32 */ sf_count_t
8266 psf_get_filelen (SF_PRIVATE *psf)
8267 { sf_count_t filelen ;
8268 DWORD dwFileSizeLow, dwFileSizeHigh, dwError = NO_ERROR ;
8270 dwFileSizeLow = GetFileSize ((HANDLE) psf->filedes, &dwFileSizeHigh) ;
8272 if (dwFileSizeLow == 0xFFFFFFFF)
8273 dwError = GetLastError () ;
8275 if (dwError != NO_ERROR)
8276 { psf_log_syserr (psf, GetLastError ()) ;
8277 return 0 ;
8279 else
8280 filelen = dwFileSizeLow + ((__int64) dwFileSizeHigh << 32) ;
8282 switch (psf->mode)
8283 { case SFM_WRITE :
8284 filelen = filelen - psf->fileoffset ;
8285 break ;
8287 case SFM_READ :
8288 if (psf->fileoffset > 0 && psf->filelength > 0)
8289 filelen = psf->filelength ;
8290 break ;
8292 case SFM_RDWR :
8294 ** Cannot open embedded files SFM_RDWR so we don't need to
8295 ** subtract psf->fileoffset. We already have the answer we
8296 ** need.
8298 break ;
8300 default :
8301 /* Shouldn't be here, so return error. */
8302 filelen = -1 ;
8305 return filelen ;
8306 } /* psf_get_filelen */
8308 /* Win32 */ int
8309 psf_ftruncate (SF_PRIVATE *psf, sf_count_t len)
8310 { int retval = 0 ;
8311 LONG lDistanceToMoveLow, lDistanceToMoveHigh ;
8312 DWORD dwResult, dwError = NO_ERROR ;
8314 /* This implementation trashes the current file position.
8315 ** should it save and restore it? what if the current position is past
8316 ** the new end of file?
8319 /* Returns 0 on success, non-zero on failure. */
8320 if (len < 0)
8321 return 1 ;
8323 lDistanceToMoveLow = (DWORD) (len & 0xFFFFFFFF) ;
8324 #ifndef __WINDOWS_DS__
8325 lDistanceToMoveHigh = (DWORD) ((len >> 32) & 0xFFFFFFFF) ;
8326 #else
8327 lDistanceToMoveHigh = 0;
8328 #endif
8330 dwResult = SetFilePointer ((HANDLE) psf->filedes, lDistanceToMoveLow, &lDistanceToMoveHigh, FILE_BEGIN) ;
8332 if (dwResult == 0xFFFFFFFF)
8333 dwError = GetLastError () ;
8335 if (dwError != NO_ERROR)
8336 { retval = -1 ;
8337 psf_log_syserr (psf, dwError) ;
8339 else
8340 { /* Note: when SetEndOfFile is used to extend a file, the contents of the
8341 ** new portion of the file is undefined. This is unlike chsize(),
8342 ** which guarantees that the new portion of the file will be zeroed.
8343 ** Not sure if this is important or not.
8345 if (SetEndOfFile ((HANDLE) psf->filedes) == 0)
8346 { retval = -1 ;
8347 psf_log_syserr (psf, GetLastError ()) ;
8351 return retval ;
8352 } /* psf_ftruncate */
8355 #else
8356 /* Win32 file i/o functions implemented using Unix-style file i/o API */
8358 /* Win32 has a 64 file offset seek function:
8360 ** __int64 _lseeki64 (int handle, __int64 offset, int origin) ;
8362 ** It also has a 64 bit fstat function:
8364 ** int fstati64 (int, struct _stati64) ;
8366 ** but the fscking thing doesn't work!!!!! The file size parameter returned
8367 ** by this function is only valid up until more data is written at the end of
8368 ** the file. That makes this function completely 100% useless.
8371 #include <io.h>
8372 #include <direct.h>
8374 #ifndef HAVE_SSIZE_T
8375 typedef long ssize_t ;
8376 #endif
8378 /* Win32 */ int
8379 psf_fopen (SF_PRIVATE *psf, const char *pathname, int open_mode)
8380 { int oflag, mode ;
8382 switch (open_mode)
8383 { case SFM_READ :
8384 oflag = O_RDONLY | O_BINARY ;
8385 mode = 0 ;
8386 break ;
8388 case SFM_WRITE :
8389 oflag = O_WRONLY | O_CREAT | O_TRUNC | O_BINARY ;
8390 mode = S_IRUSR | S_IWUSR | S_IRGRP ;
8391 break ;
8393 case SFM_RDWR :
8394 oflag = O_RDWR | O_CREAT | O_BINARY ;
8395 mode = S_IRUSR | S_IWUSR | S_IRGRP ;
8396 break ;
8398 default :
8399 psf->error = SFE_BAD_OPEN_MODE ;
8400 return -1 ;
8401 break ;
8404 if (mode == 0)
8405 psf->filedes = open (pathname, oflag) ;
8406 else
8407 psf->filedes = open (pathname, oflag, mode) ;
8409 if (psf->filedes == -1)
8410 psf_log_syserr (psf, errno) ;
8412 return psf->filedes ;
8413 } /* psf_fopen */
8415 /* Win32 */ sf_count_t
8416 psf_fseek (SF_PRIVATE *psf, sf_count_t offset, int whence)
8417 { sf_count_t new_position ;
8419 switch (whence)
8420 { case SEEK_SET :
8421 offset += psf->fileoffset ;
8422 break ;
8424 case SEEK_END :
8425 if (psf->mode == SFM_WRITE)
8426 { new_position = _lseeki64 (psf->filedes, offset, whence) ;
8428 if (new_position < 0)
8429 psf_log_syserr (psf, errno) ;
8431 return new_position - psf->fileoffset ;
8434 /* Transform SEEK_END into a SEEK_SET, ie find the file
8435 ** length add the requested offset (should be <= 0) to
8436 ** get the offset wrt the start of file.
8438 whence = SEEK_SET ;
8439 offset = _lseeki64 (psf->filedes, 0, SEEK_END) + offset ;
8440 break ;
8442 default :
8443 /* No need to do anything about SEEK_CUR. */
8444 break ;
8448 ** Bypass weird Win32-ism if necessary.
8449 ** _lseeki64() returns an "invalid parameter" error if called with the
8450 ** offset == 0 and whence == SEEK_CUR.
8451 *** Use the _telli64() function instead.
8453 if (offset == 0 && whence == SEEK_CUR)
8454 new_position = _telli64 (psf->filedes) ;
8455 else
8456 new_position = _lseeki64 (psf->filedes, offset, whence) ;
8458 if (new_position < 0)
8459 psf_log_syserr (psf, errno) ;
8461 new_position -= psf->fileoffset ;
8463 return new_position ;
8464 } /* psf_fseek */
8466 /* Win32 */ sf_count_t
8467 psf_fread (void *ptr, sf_count_t bytes, sf_count_t items, SF_PRIVATE *psf)
8468 { sf_count_t total = 0 ;
8469 ssize_t count ;
8471 items *= bytes ;
8473 /* Do this check after the multiplication above. */
8474 if (items <= 0)
8475 return 0 ;
8477 while (items > 0)
8478 { /* Break the writes down to a sensible size. */
8479 count = (items > SENSIBLE_SIZE) ? SENSIBLE_SIZE : (ssize_t) items ;
8481 count = read (psf->filedes, ((char*) ptr) + total, (size_t) count) ;
8483 if (count == -1)
8484 { if (errno == EINTR)
8485 continue ;
8487 psf_log_syserr (psf, errno) ;
8488 break ;
8491 if (count == 0)
8492 break ;
8494 total += count ;
8495 items -= count ;
8498 return total / bytes ;
8499 } /* psf_fread */
8501 /* Win32 */ sf_count_t
8502 psf_fwrite (void *ptr, sf_count_t bytes, sf_count_t items, SF_PRIVATE *psf)
8503 { sf_count_t total = 0 ;
8504 ssize_t count ;
8506 items *= bytes ;
8508 /* Do this check after the multiplication above. */
8509 if (items <= 0)
8510 return 0 ;
8512 while (items > 0)
8513 { /* Break the writes down to a sensible size. */
8514 count = (items > SENSIBLE_SIZE) ? SENSIBLE_SIZE : items ;
8516 count = write (psf->filedes, ((char*) ptr) + total, count) ;
8518 if (count == -1)
8519 { if (errno == EINTR)
8520 continue ;
8522 psf_log_syserr (psf, errno) ;
8523 break ;
8526 if (count == 0)
8527 break ;
8529 total += count ;
8530 items -= count ;
8533 return total / bytes ;
8534 } /* psf_fwrite */
8536 /* Win32 */ sf_count_t
8537 psf_ftell (SF_PRIVATE *psf)
8538 { sf_count_t pos ;
8540 pos = _telli64 (psf->filedes) ;
8542 if (pos == ((sf_count_t) -1))
8543 { psf_log_syserr (psf, errno) ;
8544 return -1 ;
8547 return pos - psf->fileoffset ;
8548 } /* psf_ftell */
8550 /* Win32 */ int
8551 psf_fclose (SF_PRIVATE *psf)
8552 { int retval ;
8554 while ((retval = close (psf->filedes)) == -1 && errno == EINTR)
8555 /* Do nothing. */ ;
8557 if (retval == -1)
8558 psf_log_syserr (psf, errno) ;
8560 psf->filedes = -1 ;
8562 return retval ;
8563 } /* psf_fclose */
8565 /* Win32 */ sf_count_t
8566 psf_fgets (char *buffer, sf_count_t bufsize, SF_PRIVATE *psf)
8567 { sf_count_t k = 0 ;
8568 sf_count_t count ;
8570 while (k < bufsize - 1)
8571 { count = read (psf->filedes, &(buffer [k]), 1) ;
8573 if (count == -1)
8574 { if (errno == EINTR)
8575 continue ;
8577 psf_log_syserr (psf, errno) ;
8578 break ;
8581 if (count == 0 || buffer [k++] == '\n')
8582 break ;
8585 buffer [k] = 0 ;
8587 return k ;
8588 } /* psf_fgets */
8590 /* Win32 */ int
8591 psf_is_pipe (SF_PRIVATE *psf)
8592 { struct stat statbuf ;
8594 /* Not sure if this works. */
8596 if (fstat (psf->filedes, &statbuf) == -1)
8597 { psf_log_syserr (psf, errno) ;
8598 /* Default to maximum safety. */
8599 return SF_TRUE ;
8602 /* These macros are defined in Win32/unistd.h. */
8603 if (S_ISFIFO (statbuf.st_mode) || S_ISSOCK (statbuf.st_mode))
8604 return SF_TRUE ;
8606 return SF_FALSE ;
8607 } /* psf_checkpipe */
8609 /* Win32 */ sf_count_t
8610 psf_get_filelen (SF_PRIVATE *psf)
8612 #if 0
8614 ** Windoze is SOOOOO FUCKED!!!!!!!
8615 ** This code should work but doesn't. Why?
8616 ** Code below does work.
8618 struct _stati64 statbuf ;
8620 if (_fstati64 (psf->filedes, &statbuf))
8621 { psf_log_syserr (psf, errno) ;
8622 return (sf_count_t) -1 ;
8625 return statbuf.st_size ;
8626 #else
8627 sf_count_t current, filelen ;
8629 if ((current = _telli64 (psf->filedes)) < 0)
8630 { psf_log_syserr (psf, errno) ;
8631 return (sf_count_t) -1 ;
8635 ** Lets face it, windoze if FUBAR!!!
8637 ** For some reason, I have to call _lseeki64() TWICE to get to the
8638 ** end of the file.
8640 ** This might have been avoided if windows had implemented the POSIX
8641 ** standard function fsync() but NO, that would have been too easy.
8643 ** I am VERY close to saying that windoze will no longer be supported
8644 ** by libsndfile and changing the license to GPL at the same time.
8647 _lseeki64 (psf->filedes, 0, SEEK_END) ;
8649 if ((filelen = _lseeki64 (psf->filedes, 0, SEEK_END)) < 0)
8650 { psf_log_syserr (psf, errno) ;
8651 return (sf_count_t) -1 ;
8654 if (filelen > current)
8655 _lseeki64 (psf->filedes, current, SEEK_SET) ;
8657 switch (psf->mode)
8658 { case SFM_WRITE :
8659 filelen = filelen - psf->fileoffset ;
8660 break ;
8662 case SFM_READ :
8663 if (psf->fileoffset > 0 && psf->filelength > 0)
8664 filelen = psf->filelength ;
8665 break ;
8667 case SFM_RDWR :
8669 ** Cannot open embedded files SFM_RDWR so we don't need to
8670 ** subtract psf->fileoffset. We already have the answer we
8671 ** need.
8673 break ;
8675 default :
8676 filelen = 0 ;
8679 return filelen ;
8680 #endif
8681 } /* psf_get_filelen */
8683 /* Win32 */ int
8684 psf_ftruncate (SF_PRIVATE *psf, sf_count_t len)
8685 { int retval ;
8687 /* Returns 0 on success, non-zero on failure. */
8688 if (len < 0)
8689 return 1 ;
8691 /* The global village idiots at micorsoft decided to implement
8692 ** nearly all the required 64 bit file offset functions except
8693 ** for one, truncate. The fscking morons!
8695 ** This is not 64 bit file offset clean. Somone needs to clean
8696 ** this up.
8698 if (len > 0x7FFFFFFF)
8699 return -1 ;
8701 retval = chsize (psf->filedes, len) ;
8703 if (retval == -1)
8704 psf_log_syserr (psf, errno) ;
8706 return retval ;
8707 } /* psf_ftruncate */
8710 static void
8711 psf_log_syserr (SF_PRIVATE *psf, int error)
8713 /* Only log an error if no error has been set yet. */
8714 if (psf->error == 0)
8715 { psf->error = SFE_SYSTEM ;
8716 LSF_SNPRINTF (psf->syserr, sizeof (psf->syserr), "System error : %s", strerror (error)) ;
8719 return ;
8720 } /* psf_log_syserr */
8722 #endif
8724 /*==============================================================================
8728 ** Do not edit or modify anything in this comment block.
8729 ** The arch-tag line is a file identity tag for the GNU Arch
8730 ** revision control system.
8732 ** arch-tag: 749740d7-ecc7-47bd-8cf7-600f31d32e6d
8735 ** Copyright (C) 1999-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
8737 ** This program is free software; you can redistribute it and/or modify
8738 ** it under the terms of the GNU Lesser General Public License as published by
8739 ** the Free Software Foundation; either version 2.1 of the License, or
8740 ** (at your option) any later version.
8742 ** This program is distributed in the hope that it will be useful,
8743 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
8744 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8745 ** GNU Lesser General Public License for more details.
8747 ** You should have received a copy of the GNU Lesser General Public License
8748 ** along with this program; if not, write to the Free Software
8749 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
8752 #include <stdio.h>
8753 #include <stdlib.h>
8754 #include <string.h>
8757 #if CPU_IS_LITTLE_ENDIAN
8758 #define FLOAT32_READ float32_le_read
8759 #define FLOAT32_WRITE float32_le_write
8760 #elif CPU_IS_BIG_ENDIAN
8761 #define FLOAT32_READ float32_be_read
8762 #define FLOAT32_WRITE float32_be_write
8763 #endif
8765 /*--------------------------------------------------------------------------------------------
8766 ** Processor floating point capabilities. float32_get_capability () returns one of the
8767 ** latter four values.
8770 enum
8771 { FLOAT_UNKNOWN = 0x00,
8772 FLOAT_CAN_RW_LE = 0x12,
8773 FLOAT_CAN_RW_BE = 0x23,
8774 FLOAT_BROKEN_LE = 0x34,
8775 FLOAT_BROKEN_BE = 0x45
8778 /*--------------------------------------------------------------------------------------------
8779 ** Prototypes for private functions.
8782 static sf_count_t host_read_f2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
8783 static sf_count_t host_read_f2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
8784 static sf_count_t host_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
8785 static sf_count_t host_read_f2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
8787 static sf_count_t host_write_s2f (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
8788 static sf_count_t host_write_i2f (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
8789 static sf_count_t host_write_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
8790 static sf_count_t host_write_d2f (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
8792 static void f2s_array (float *src, int count, short *dest) ;
8793 static void f2i_array (float *src, int count, int *dest) ;
8794 static void float32f2d_array (float *src, int count, double *dest) ;
8796 static void s2f_array (short *src, float *dest, int count) ;
8797 static void i2f_array (int *src, float *dest, int count) ;
8798 static void float32d2f_array (double *src, float *dest, int count) ;
8800 static void float32_peak_update (SF_PRIVATE *psf, float *buffer, int count, int indx) ;
8802 static sf_count_t replace_read_f2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
8803 static sf_count_t replace_read_f2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
8804 static sf_count_t replace_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
8805 static sf_count_t replace_read_f2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
8807 static sf_count_t replace_write_s2f (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
8808 static sf_count_t replace_write_i2f (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
8809 static sf_count_t replace_write_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
8810 static sf_count_t replace_write_d2f (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
8812 static void bf2f_array (float *buffer, int count) ;
8813 static void f2bf_array (float *buffer, int count) ;
8815 static int float32_get_capability (SF_PRIVATE *psf) ;
8817 /*--------------------------------------------------------------------------------------------
8818 ** Exported functions.
8822 float32_init (SF_PRIVATE *psf)
8823 { static int float_caps ;
8825 float_caps = float32_get_capability (psf) ;
8827 psf->blockwidth = sizeof (float) * psf->sf.channels ;
8829 if (psf->mode == SFM_READ || psf->mode == SFM_RDWR)
8830 { switch (psf->endian + float_caps)
8831 { case (SF_ENDIAN_BIG + FLOAT_CAN_RW_BE) :
8832 psf->float_endswap = SF_FALSE ;
8833 psf->read_short = host_read_f2s ;
8834 psf->read_int = host_read_f2i ;
8835 psf->read_float = host_read_f ;
8836 psf->read_double = host_read_f2d ;
8837 break ;
8839 case (SF_ENDIAN_LITTLE + FLOAT_CAN_RW_LE) :
8840 psf->float_endswap = SF_FALSE ;
8841 psf->read_short = host_read_f2s ;
8842 psf->read_int = host_read_f2i ;
8843 psf->read_float = host_read_f ;
8844 psf->read_double = host_read_f2d ;
8845 break ;
8847 case (SF_ENDIAN_BIG + FLOAT_CAN_RW_LE) :
8848 psf->float_endswap = SF_TRUE ;
8849 psf->read_short = host_read_f2s ;
8850 psf->read_int = host_read_f2i ;
8851 psf->read_float = host_read_f ;
8852 psf->read_double = host_read_f2d ;
8853 break ;
8855 case (SF_ENDIAN_LITTLE + FLOAT_CAN_RW_BE) :
8856 psf->float_endswap = SF_TRUE ;
8857 psf->read_short = host_read_f2s ;
8858 psf->read_int = host_read_f2i ;
8859 psf->read_float = host_read_f ;
8860 psf->read_double = host_read_f2d ;
8861 break ;
8863 /* When the CPU is not IEEE compatible. */
8864 case (SF_ENDIAN_BIG + FLOAT_BROKEN_LE) :
8865 psf->float_endswap = SF_TRUE ;
8866 psf->read_short = replace_read_f2s ;
8867 psf->read_int = replace_read_f2i ;
8868 psf->read_float = replace_read_f ;
8869 psf->read_double = replace_read_f2d ;
8870 break ;
8872 case (SF_ENDIAN_LITTLE + FLOAT_BROKEN_LE) :
8873 psf->float_endswap = SF_FALSE ;
8874 psf->read_short = replace_read_f2s ;
8875 psf->read_int = replace_read_f2i ;
8876 psf->read_float = replace_read_f ;
8877 psf->read_double = replace_read_f2d ;
8878 break ;
8880 case (SF_ENDIAN_BIG + FLOAT_BROKEN_BE) :
8881 psf->float_endswap = SF_FALSE ;
8882 psf->read_short = replace_read_f2s ;
8883 psf->read_int = replace_read_f2i ;
8884 psf->read_float = replace_read_f ;
8885 psf->read_double = replace_read_f2d ;
8886 break ;
8888 case (SF_ENDIAN_LITTLE + FLOAT_BROKEN_BE) :
8889 psf->float_endswap = SF_TRUE ;
8890 psf->read_short = replace_read_f2s ;
8891 psf->read_int = replace_read_f2i ;
8892 psf->read_float = replace_read_f ;
8893 psf->read_double = replace_read_f2d ;
8894 break ;
8896 default : break ;
8900 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
8901 { switch (psf->endian + float_caps)
8902 { case (SF_ENDIAN_LITTLE + FLOAT_CAN_RW_LE) :
8903 psf->float_endswap = SF_FALSE ;
8904 psf->write_short = host_write_s2f ;
8905 psf->write_int = host_write_i2f ;
8906 psf->write_float = host_write_f ;
8907 psf->write_double = host_write_d2f ;
8908 break ;
8910 case (SF_ENDIAN_BIG + FLOAT_CAN_RW_BE) :
8911 psf->float_endswap = SF_FALSE ;
8912 psf->write_short = host_write_s2f ;
8913 psf->write_int = host_write_i2f ;
8914 psf->write_float = host_write_f ;
8915 psf->write_double = host_write_d2f ;
8916 break ;
8918 case (SF_ENDIAN_BIG + FLOAT_CAN_RW_LE) :
8919 psf->float_endswap = SF_TRUE ;
8920 psf->write_short = host_write_s2f ;
8921 psf->write_int = host_write_i2f ;
8922 psf->write_float = host_write_f ;
8923 psf->write_double = host_write_d2f ;
8924 break ;
8926 case (SF_ENDIAN_LITTLE + FLOAT_CAN_RW_BE) :
8927 psf->float_endswap = SF_TRUE ;
8928 psf->write_short = host_write_s2f ;
8929 psf->write_int = host_write_i2f ;
8930 psf->write_float = host_write_f ;
8931 psf->write_double = host_write_d2f ;
8932 break ;
8934 /* When the CPU is not IEEE compatible. */
8935 case (SF_ENDIAN_BIG + FLOAT_BROKEN_LE) :
8936 psf->float_endswap = SF_TRUE ;
8937 psf->write_short = replace_write_s2f ;
8938 psf->write_int = replace_write_i2f ;
8939 psf->write_float = replace_write_f ;
8940 psf->write_double = replace_write_d2f ;
8941 break ;
8943 case (SF_ENDIAN_LITTLE + FLOAT_BROKEN_LE) :
8944 psf->float_endswap = SF_FALSE ;
8945 psf->write_short = replace_write_s2f ;
8946 psf->write_int = replace_write_i2f ;
8947 psf->write_float = replace_write_f ;
8948 psf->write_double = replace_write_d2f ;
8949 break ;
8951 case (SF_ENDIAN_BIG + FLOAT_BROKEN_BE) :
8952 psf->float_endswap = SF_FALSE ;
8953 psf->write_short = replace_write_s2f ;
8954 psf->write_int = replace_write_i2f ;
8955 psf->write_float = replace_write_f ;
8956 psf->write_double = replace_write_d2f ;
8957 break ;
8959 case (SF_ENDIAN_LITTLE + FLOAT_BROKEN_BE) :
8960 psf->float_endswap = SF_TRUE ;
8961 psf->write_short = replace_write_s2f ;
8962 psf->write_int = replace_write_i2f ;
8963 psf->write_float = replace_write_f ;
8964 psf->write_double = replace_write_d2f ;
8965 break ;
8967 default : break ;
8971 if (psf->filelength > psf->dataoffset)
8972 { psf->datalength = (psf->dataend > 0) ? psf->dataend - psf->dataoffset :
8973 psf->filelength - psf->dataoffset ;
8975 else
8976 psf->datalength = 0 ;
8978 psf->sf.frames = psf->datalength / psf->blockwidth ;
8980 return 0 ;
8981 } /* float32_init */
8983 float
8984 float32_be_read (unsigned char *cptr)
8985 { int exponent, mantissa, negative ;
8986 float fvalue ;
8988 negative = cptr [0] & 0x80 ;
8989 exponent = ((cptr [0] & 0x7F) << 1) | ((cptr [1] & 0x80) ? 1 : 0) ;
8990 mantissa = ((cptr [1] & 0x7F) << 16) | (cptr [2] << 8) | (cptr [3]) ;
8992 if (! (exponent || mantissa))
8993 return 0.0 ;
8995 mantissa |= 0x800000 ;
8996 exponent = exponent ? exponent - 127 : 0 ;
8998 fvalue = mantissa ? ((float) mantissa) / ((float) 0x800000) : 0.0 ;
9000 if (negative)
9001 fvalue *= -1 ;
9003 if (exponent > 0)
9004 fvalue *= (1 << exponent) ;
9005 else if (exponent < 0)
9006 fvalue /= (1 << abs (exponent)) ;
9008 return fvalue ;
9009 } /* float32_be_read */
9011 float
9012 float32_le_read (unsigned char *cptr)
9013 { int exponent, mantissa, negative ;
9014 float fvalue ;
9016 negative = cptr [3] & 0x80 ;
9017 exponent = ((cptr [3] & 0x7F) << 1) | ((cptr [2] & 0x80) ? 1 : 0) ;
9018 mantissa = ((cptr [2] & 0x7F) << 16) | (cptr [1] << 8) | (cptr [0]) ;
9020 if (! (exponent || mantissa))
9021 return 0.0 ;
9023 mantissa |= 0x800000 ;
9024 exponent = exponent ? exponent - 127 : 0 ;
9026 fvalue = mantissa ? ((float) mantissa) / ((float) 0x800000) : 0.0 ;
9028 if (negative)
9029 fvalue *= -1 ;
9031 if (exponent > 0)
9032 fvalue *= (1 << exponent) ;
9033 else if (exponent < 0)
9034 fvalue /= (1 << abs (exponent)) ;
9036 return fvalue ;
9037 } /* float32_le_read */
9039 void
9040 float32_le_write (float in, unsigned char *out)
9041 { int exponent, mantissa, negative = 0 ;
9043 memset (out, 0, sizeof (int)) ;
9045 if (in == 0.0)
9046 return ;
9048 if (in < 0.0)
9049 { in *= -1.0 ;
9050 negative = 1 ;
9053 in = frexp (in, &exponent) ;
9055 exponent += 126 ;
9057 in *= (float) 0x1000000 ;
9058 mantissa = (((int) in) & 0x7FFFFF) ;
9060 if (negative)
9061 out [3] |= 0x80 ;
9063 if (exponent & 0x01)
9064 out [2] |= 0x80 ;
9066 out [0] = mantissa & 0xFF ;
9067 out [1] = (mantissa >> 8) & 0xFF ;
9068 out [2] |= (mantissa >> 16) & 0x7F ;
9069 out [3] |= (exponent >> 1) & 0x7F ;
9071 return ;
9072 } /* float32_le_write */
9074 void
9075 float32_be_write (float in, unsigned char *out)
9076 { int exponent, mantissa, negative = 0 ;
9078 memset (out, 0, sizeof (int)) ;
9080 if (in == 0.0)
9081 return ;
9083 if (in < 0.0)
9084 { in *= -1.0 ;
9085 negative = 1 ;
9088 in = frexp (in, &exponent) ;
9090 exponent += 126 ;
9092 in *= (float) 0x1000000 ;
9093 mantissa = (((int) in) & 0x7FFFFF) ;
9095 if (negative)
9096 out [0] |= 0x80 ;
9098 if (exponent & 0x01)
9099 out [1] |= 0x80 ;
9101 out [3] = mantissa & 0xFF ;
9102 out [2] = (mantissa >> 8) & 0xFF ;
9103 out [1] |= (mantissa >> 16) & 0x7F ;
9104 out [0] |= (exponent >> 1) & 0x7F ;
9106 return ;
9107 } /* float32_be_write */
9109 /*==============================================================================================
9110 ** Private functions.
9113 static void
9114 float32_peak_update (SF_PRIVATE *psf, float *buffer, int count, int indx)
9115 { int chan ;
9116 int k, position ;
9117 float fmaxval ;
9119 for (chan = 0 ; chan < psf->sf.channels ; chan++)
9120 { fmaxval = fabs (buffer [chan]) ;
9121 position = 0 ;
9122 for (k = chan ; k < count ; k += psf->sf.channels)
9123 if (fmaxval < fabs (buffer [k]))
9124 { fmaxval = fabs (buffer [k]) ;
9125 position = k ;
9128 if (fmaxval > psf->pchunk->peaks [chan].value)
9129 { psf->pchunk->peaks [chan].value = fmaxval ;
9130 psf->pchunk->peaks [chan].position = psf->write_current + indx + (position / psf->sf.channels) ;
9134 return ;
9135 } /* float32_peak_update */
9137 static int
9138 float32_get_capability (SF_PRIVATE *psf)
9139 { union
9140 { float f ;
9141 int i ;
9142 unsigned char c [4] ;
9143 } data ;
9145 data.f = (float) 1.23456789 ; /* Some abitrary value. */
9147 if (! psf->ieee_replace)
9148 { /* If this test is true ints and floats are compatible and little endian. */
9149 if (data.c [0] == 0x52 && data.c [1] == 0x06 && data.c [2] == 0x9e && data.c [3] == 0x3f)
9150 return FLOAT_CAN_RW_LE ;
9152 /* If this test is true ints and floats are compatible and big endian. */
9153 if (data.c [3] == 0x52 && data.c [2] == 0x06 && data.c [1] == 0x9e && data.c [0] == 0x3f)
9154 return FLOAT_CAN_RW_BE ;
9157 /* Floats are broken. Don't expect reading or writing to be fast. */
9158 psf_log_printf (psf, "Using IEEE replacement code for float.\n") ;
9160 return (CPU_IS_LITTLE_ENDIAN) ? FLOAT_BROKEN_LE : FLOAT_BROKEN_BE ;
9161 } /* float32_get_capability */
9163 /*----------------------------------------------------------------------------------------------
9166 static sf_count_t
9167 host_read_f2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
9168 { int bufferlen, readcount, thisread ;
9169 sf_count_t total = 0 ;
9171 bufferlen = sizeof (psf->buffer) / sizeof (float) ;
9173 while (len > 0)
9174 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
9175 thisread = psf_fread (psf->buffer, sizeof (float), readcount, psf) ;
9177 /* Fix me : Need lef2s_array */
9178 if (psf->float_endswap == SF_TRUE)
9179 endswap_int_array ((int*) psf->buffer, readcount) ;
9181 f2s_array ((float*) (psf->buffer), thisread, ptr + total) ;
9182 total += thisread ;
9183 if (thisread < readcount)
9184 break ;
9185 len -= thisread ;
9188 return total ;
9189 } /* host_read_f2s */
9191 static sf_count_t
9192 host_read_f2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
9193 { int bufferlen, readcount, thisread ;
9194 sf_count_t total = 0 ;
9196 bufferlen = sizeof (psf->buffer) / sizeof (float) ;
9198 while (len > 0)
9199 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
9200 thisread = psf_fread (psf->buffer, sizeof (float), readcount, psf) ;
9202 if (psf->float_endswap == SF_TRUE)
9203 endswap_int_array ((int*) psf->buffer, readcount) ;
9205 f2i_array ((float*) (psf->buffer), thisread, ptr + total) ;
9206 total += thisread ;
9207 if (thisread < readcount)
9208 break ;
9209 len -= thisread ;
9212 return total ;
9213 } /* host_read_f2i */
9215 static sf_count_t
9216 host_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
9217 { int bufferlen, readcount, thisread ;
9218 sf_count_t total = 0 ;
9220 if (psf->float_endswap != SF_TRUE)
9221 return psf_fread (ptr, sizeof (float), len, psf) ;
9223 bufferlen = sizeof (psf->buffer) / sizeof (float) ;
9225 while (len > 0)
9226 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
9227 thisread = psf_fread (psf->buffer, sizeof (float), readcount, psf) ;
9229 endswap_int_copy ((int*) (ptr + total), (int*) psf->buffer, thisread) ;
9231 total += thisread ;
9232 if (thisread < readcount)
9233 break ;
9234 len -= thisread ;
9237 return total ;
9238 } /* host_read_f */
9240 static sf_count_t
9241 host_read_f2d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
9242 { int bufferlen, readcount, thisread ;
9243 sf_count_t total = 0 ;
9245 bufferlen = sizeof (psf->buffer) / sizeof (float) ;
9247 while (len > 0)
9248 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
9249 thisread = psf_fread (psf->buffer, sizeof (float), readcount, psf) ;
9251 if (psf->float_endswap == SF_TRUE)
9252 endswap_int_array ((int*) psf->buffer, readcount) ;
9254 /* Fix me : Need lefloat32f2d_array */
9255 float32f2d_array ((float*) (psf->buffer), thisread, ptr + total) ;
9256 total += thisread ;
9257 if (thisread < readcount)
9258 break ;
9259 len -= thisread ;
9262 return total ;
9263 } /* host_read_f2d */
9265 static sf_count_t
9266 host_write_s2f (SF_PRIVATE *psf, short *ptr, sf_count_t len)
9267 { int bufferlen, writecount, thiswrite ;
9268 sf_count_t total = 0 ;
9270 bufferlen = sizeof (psf->buffer) / sizeof (float) ;
9272 while (len > 0)
9273 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
9274 s2f_array (ptr + total, (float*) (psf->buffer), writecount) ;
9276 if (psf->has_peak)
9277 float32_peak_update (psf, (float *) (psf->buffer), writecount, (int) (total / psf->sf.channels)) ;
9279 if (psf->float_endswap == SF_TRUE)
9280 endswap_int_array ((int*) psf->buffer, writecount) ;
9282 thiswrite = psf_fwrite (psf->buffer, sizeof (float), writecount, psf) ;
9283 total += thiswrite ;
9284 if (thiswrite < writecount)
9285 break ;
9286 len -= thiswrite ;
9289 return total ;
9290 } /* host_write_s2f */
9292 static sf_count_t
9293 host_write_i2f (SF_PRIVATE *psf, int *ptr, sf_count_t len)
9294 { int bufferlen, writecount, thiswrite ;
9295 sf_count_t total = 0 ;
9297 bufferlen = sizeof (psf->buffer) / sizeof (float) ;
9299 while (len > 0)
9300 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
9301 i2f_array (ptr + total, (float*) (psf->buffer), writecount) ;
9303 if (psf->has_peak)
9304 float32_peak_update (psf, (float *) (psf->buffer), writecount, (int) (total / psf->sf.channels)) ;
9306 if (psf->float_endswap == SF_TRUE)
9307 endswap_int_array ((int*) psf->buffer, writecount) ;
9309 thiswrite = psf_fwrite (psf->buffer, sizeof (float) , writecount, psf) ;
9310 total += thiswrite ;
9311 if (thiswrite < writecount)
9312 break ;
9313 len -= thiswrite ;
9316 return total ;
9317 } /* host_write_i2f */
9319 static sf_count_t
9320 host_write_f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
9321 { int bufferlen, writecount, thiswrite ;
9322 sf_count_t total = 0 ;
9324 if (psf->has_peak)
9325 float32_peak_update (psf, ptr, len, 0) ;
9327 if (psf->float_endswap != SF_TRUE)
9328 return psf_fwrite (ptr, sizeof (float), len, psf) ;
9330 bufferlen = sizeof (psf->buffer) / sizeof (float) ;
9332 while (len > 0)
9333 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
9335 endswap_int_copy ((int*) psf->buffer, (int*) (ptr + total), writecount) ;
9337 thiswrite = psf_fwrite (psf->buffer, sizeof (float), writecount, psf) ;
9338 total += thiswrite ;
9339 if (thiswrite < writecount)
9340 break ;
9341 len -= thiswrite ;
9344 return total ;
9345 } /* host_write_f */
9347 static sf_count_t
9348 host_write_d2f (SF_PRIVATE *psf, double *ptr, sf_count_t len)
9349 { int bufferlen, writecount, thiswrite ;
9350 sf_count_t total = 0 ;
9352 bufferlen = sizeof (psf->buffer) / sizeof (float) ;
9354 while (len > 0)
9355 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
9357 float32d2f_array (ptr + total, (float*) (psf->buffer), writecount) ;
9359 if (psf->has_peak)
9360 float32_peak_update (psf, (float *) (psf->buffer), writecount, (int) (total / psf->sf.channels)) ;
9362 if (psf->float_endswap == SF_TRUE)
9363 endswap_int_array ((int*) psf->buffer, writecount) ;
9365 thiswrite = psf_fwrite (psf->buffer, sizeof (float), writecount, psf) ;
9366 total += thiswrite ;
9367 if (thiswrite < writecount)
9368 break ;
9369 len -= thiswrite ;
9372 return total ;
9373 } /* host_write_d2f */
9375 /*=======================================================================================
9378 static void
9379 f2s_array (float *src, int count, short *dest)
9380 { while (count)
9381 { count -- ;
9382 dest [count] = lrintf (src [count]) ;
9384 } /* f2s_array */
9386 static void
9387 f2i_array (float *src, int count, int *dest)
9388 { while (count)
9389 { count -- ;
9390 dest [count] = lrintf (src [count]) ;
9392 } /* f2i_array */
9394 static void
9395 float32f2d_array (float *src, int count, double *dest)
9396 { while (count)
9397 { count -- ;
9398 dest [count] = src [count] ;
9400 } /* float32f2d_array */
9402 static void
9403 s2f_array (short *src, float *dest, int count)
9404 { while (count)
9405 { count -- ;
9406 dest [count] = src [count] ;
9409 } /* s2f_array */
9411 static void
9412 i2f_array (int *src, float *dest, int count)
9413 { while (count)
9414 { count -- ;
9415 dest [count] = src [count] ;
9417 } /* i2f_array */
9419 static void
9420 float32d2f_array (double *src, float *dest, int count)
9421 { while (count)
9422 { count -- ;
9423 dest [count] = src [count] ;
9425 } /* float32d2f_array */
9427 /*=======================================================================================
9430 static sf_count_t
9431 replace_read_f2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
9432 { int bufferlen, readcount, thisread ;
9433 sf_count_t total = 0 ;
9435 bufferlen = sizeof (psf->buffer) / sizeof (float) ;
9437 while (len > 0)
9438 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
9439 thisread = psf_fread (psf->buffer, sizeof (float), readcount, psf) ;
9441 if (psf->float_endswap == SF_TRUE)
9442 endswap_int_array ((int*) psf->buffer, readcount) ;
9444 bf2f_array ((float *) (psf->buffer), readcount) ;
9446 f2s_array ((float*) (psf->buffer), thisread, ptr + total) ;
9447 total += thisread ;
9448 if (thisread < readcount)
9449 break ;
9450 len -= thisread ;
9453 return total ;
9454 } /* replace_read_f2s */
9456 static sf_count_t
9457 replace_read_f2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
9458 { int bufferlen, readcount, thisread ;
9459 sf_count_t total = 0 ;
9461 bufferlen = sizeof (psf->buffer) / sizeof (float) ;
9463 while (len > 0)
9464 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
9465 thisread = psf_fread (psf->buffer, sizeof (float), readcount, psf) ;
9467 if (psf->float_endswap == SF_TRUE)
9468 endswap_int_array ((int*) psf->buffer, readcount) ;
9470 bf2f_array ((float *) (psf->buffer), readcount) ;
9472 f2i_array ((float*) (psf->buffer), thisread, ptr + total) ;
9473 total += thisread ;
9474 if (thisread < readcount)
9475 break ;
9476 len -= thisread ;
9479 return total ;
9480 } /* replace_read_f2i */
9482 static sf_count_t
9483 replace_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
9484 { int bufferlen, readcount, thisread ;
9485 sf_count_t total = 0 ;
9487 /* FIX THIS */
9489 bufferlen = sizeof (psf->buffer) / sizeof (float) ;
9491 while (len > 0)
9492 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
9493 thisread = psf_fread (psf->buffer, sizeof (float), readcount, psf) ;
9495 if (psf->float_endswap == SF_TRUE)
9496 endswap_int_array ((int*) psf->buffer, readcount) ;
9498 bf2f_array ((float *) (psf->buffer), readcount) ;
9500 memcpy (ptr + total, psf->buffer, readcount * sizeof (float)) ;
9502 total += thisread ;
9503 if (thisread < readcount)
9504 break ;
9505 len -= thisread ;
9508 return total ;
9509 } /* replace_read_f */
9511 static sf_count_t
9512 replace_read_f2d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
9513 { int bufferlen, readcount, thisread ;
9514 sf_count_t total = 0 ;
9516 bufferlen = sizeof (psf->buffer) / sizeof (float) ;
9518 while (len > 0)
9519 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
9520 thisread = psf_fread (psf->buffer, sizeof (float), readcount, psf) ;
9522 if (psf->float_endswap == SF_TRUE)
9523 endswap_int_array ((int*) psf->buffer, readcount) ;
9525 bf2f_array ((float *) (psf->buffer), readcount) ;
9527 float32f2d_array ((float*) (psf->buffer), thisread, ptr + total) ;
9528 total += thisread ;
9529 if (thisread < readcount)
9530 break ;
9531 len -= thisread ;
9534 return total ;
9535 } /* replace_read_f2d */
9537 static sf_count_t
9538 replace_write_s2f (SF_PRIVATE *psf, short *ptr, sf_count_t len)
9539 { int writecount, bufferlen, thiswrite ;
9540 sf_count_t total = 0 ;
9542 bufferlen = sizeof (psf->buffer) / sizeof (float) ;
9544 while (len > 0)
9545 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
9546 s2f_array (ptr + total, (float*) (psf->buffer), writecount) ;
9548 if (psf->has_peak)
9549 float32_peak_update (psf, (float *) (psf->buffer), writecount, (int) (total / psf->sf.channels)) ;
9551 f2bf_array ((float *) (psf->buffer), writecount) ;
9553 if (psf->float_endswap == SF_TRUE)
9554 endswap_int_array ((int*) psf->buffer, writecount) ;
9556 thiswrite = psf_fwrite (psf->buffer, sizeof (float), writecount, psf) ;
9557 total += thiswrite ;
9558 if (thiswrite < writecount)
9559 break ;
9560 len -= thiswrite ;
9563 return total ;
9564 } /* replace_write_s2f */
9566 static sf_count_t
9567 replace_write_i2f (SF_PRIVATE *psf, int *ptr, sf_count_t len)
9568 { int writecount, bufferlen, thiswrite ;
9569 sf_count_t total = 0 ;
9571 bufferlen = sizeof (psf->buffer) / sizeof (float) ;
9573 while (len > 0)
9574 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
9575 i2f_array (ptr + total, (float*) (psf->buffer), writecount) ;
9577 if (psf->has_peak)
9578 float32_peak_update (psf, (float *) (psf->buffer), writecount, (int) (total / psf->sf.channels)) ;
9580 f2bf_array ((float *) (psf->buffer), writecount) ;
9582 if (psf->float_endswap == SF_TRUE)
9583 endswap_int_array ((int*) psf->buffer, writecount) ;
9585 thiswrite = psf_fwrite (psf->buffer, sizeof (float), writecount, psf) ;
9586 total += thiswrite ;
9587 if (thiswrite < writecount)
9588 break ;
9589 len -= thiswrite ;
9592 return total ;
9593 } /* replace_write_i2f */
9595 static sf_count_t
9596 replace_write_f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
9597 { int writecount, bufferlen, thiswrite ;
9598 sf_count_t total = 0 ;
9600 /* FIX THIS */
9601 if (psf->has_peak)
9602 float32_peak_update (psf, ptr, len, 0) ;
9604 bufferlen = sizeof (psf->buffer) / sizeof (float) ;
9606 while (len > 0)
9607 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
9609 memcpy (psf->buffer, ptr + total, writecount * sizeof (float)) ;
9611 f2bf_array ((float *) (psf->buffer), writecount) ;
9613 if (psf->float_endswap == SF_TRUE)
9614 endswap_int_array ((int*) psf->buffer, writecount) ;
9616 thiswrite = psf_fwrite (psf->buffer, sizeof (float) , writecount, psf) ;
9617 total += thiswrite ;
9618 if (thiswrite < writecount)
9619 break ;
9620 len -= thiswrite ;
9623 return total ;
9624 } /* replace_write_f */
9626 static sf_count_t
9627 replace_write_d2f (SF_PRIVATE *psf, double *ptr, sf_count_t len)
9628 { int writecount, bufferlen, thiswrite ;
9629 sf_count_t total = 0 ;
9631 bufferlen = sizeof (psf->buffer) / sizeof (float) ;
9633 while (len > 0)
9634 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
9635 float32d2f_array (ptr + total, (float*) (psf->buffer), writecount) ;
9637 if (psf->has_peak)
9638 float32_peak_update (psf, (float *) (psf->buffer), writecount, (int) (total / psf->sf.channels)) ;
9640 f2bf_array ((float *) (psf->buffer), writecount) ;
9642 if (psf->float_endswap == SF_TRUE)
9643 endswap_int_array ((int*) psf->buffer, writecount) ;
9645 thiswrite = psf_fwrite (psf->buffer, sizeof (float), writecount, psf) ;
9646 total += thiswrite ;
9647 if (thiswrite < writecount)
9648 break ;
9649 len -= thiswrite ;
9652 return total ;
9653 } /* replace_write_d2f */
9655 /*----------------------------------------------------------------------------------------------
9658 static void
9659 bf2f_array (float *buffer, int count)
9660 { while (count)
9661 { count -- ;
9662 buffer [count] = FLOAT32_READ ((unsigned char *) (buffer + count)) ;
9664 } /* bf2f_array */
9666 static void
9667 f2bf_array (float *buffer, int count)
9668 { while (count)
9669 { count -- ;
9670 FLOAT32_WRITE (buffer [count], (unsigned char*) (buffer + count)) ;
9672 } /* f2bf_array */
9675 ** Do not edit or modify anything in this comment block.
9676 ** The arch-tag line is a file identity tag for the GNU Arch
9677 ** revision control system.
9679 ** arch-tag: b6c34917-488c-4145-9648-f4371fc4c889
9682 * This source code is a product of Sun Microsystems, Inc. and is provided
9683 * for unrestricted use. Users may copy or modify this source code without
9684 * charge.
9686 * SUN SOURCE CODE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING
9687 * THE WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
9688 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
9690 * Sun source code is provided with no support and without any obligation on
9691 * the part of Sun Microsystems, Inc. to assist in its use, correction,
9692 * modification or enhancement.
9694 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
9695 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS SOFTWARE
9696 * OR ANY PART THEREOF.
9698 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
9699 * or profits or other special, indirect and consequential damages, even if
9700 * Sun has been advised of the possibility of such damages.
9702 * Sun Microsystems, Inc.
9703 * 2550 Garcia Avenue
9704 * Mountain View, California 94043
9708 * g721.c
9710 * Description:
9712 * g721_encoder(), g721_decoder()
9714 * These routines comprise an implementation of the CCITT G.721 ADPCM
9715 * coding algorithm. Essentially, this implementation is identical to
9716 * the bit level description except for a few deviations which
9717 * take advantage of work station attributes, such as hardware 2's
9718 * complement arithmetic and large memory. Specifically, certain time
9719 * consuming operations such as multiplications are replaced
9720 * with lookup tables and software 2's complement operations are
9721 * replaced with hardware 2's complement.
9723 * The deviation from the bit level specification (lookup tables)
9724 * preserves the bit level performance specifications.
9726 * As outlined in the G.721 Recommendation, the algorithm is broken
9727 * down into modules. Each section of code below is preceded by
9728 * the name of the module which it is implementing.
9733 static short qtab_721[7] = {-124, 80, 178, 246, 300, 349, 400};
9735 * Maps G.721 code word to reconstructed scale factor normalized log
9736 * magnitude values.
9738 static short g721_dqlntab[16] = {-2048, 4, 135, 213, 273, 323, 373, 425,
9739 425, 373, 323, 273, 213, 135, 4, -2048};
9741 /* Maps G.721 code word to log of scale factor multiplier. */
9742 static short g721_witab[16] = {-12, 18, 41, 64, 112, 198, 355, 1122,
9743 1122, 355, 198, 112, 64, 41, 18, -12};
9745 * Maps G.721 code words to a set of values whose long and short
9746 * term averages are computed and then compared to give an indication
9747 * how stationary (steady state) the signal is.
9749 static short g721_fitab[16] = {0, 0, 0, 0x200, 0x200, 0x200, 0x600, 0xE00,
9750 0xE00, 0x600, 0x200, 0x200, 0x200, 0, 0, 0};
9753 * g721_encoder()
9755 * Encodes the input vale of linear PCM, A-law or u-law data sl and returns
9756 * the resulting code. -1 is returned for unknown input coding value.
9759 g721_encoder(
9760 int sl,
9761 G72x_STATE *state_ptr)
9763 short sezi, se, sez; /* ACCUM */
9764 short d; /* SUBTA */
9765 short sr; /* ADDB */
9766 short y; /* MIX */
9767 short dqsez; /* ADDC */
9768 short dq, i;
9770 /* linearize input sample to 14-bit PCM */
9771 sl >>= 2; /* 14-bit dynamic range */
9773 sezi = predictor_zero(state_ptr);
9774 sez = sezi >> 1;
9775 se = (sezi + predictor_pole(state_ptr)) >> 1; /* estimated signal */
9777 d = sl - se; /* estimation difference */
9779 /* quantize the prediction difference */
9780 y = step_size(state_ptr); /* quantizer step size */
9781 i = quantize(d, y, qtab_721, 7); /* i = ADPCM code */
9783 dq = reconstruct(i & 8, g721_dqlntab[i], y); /* quantized est diff */
9785 sr = (dq < 0) ? se - (dq & 0x3FFF) : se + dq; /* reconst. signal */
9787 dqsez = sr + sez - se; /* pole prediction diff. */
9789 update(4, y, g721_witab[i] << 5, g721_fitab[i], dq, sr, dqsez, state_ptr);
9791 return (i);
9795 * g721_decoder()
9797 * Description:
9799 * Decodes a 4-bit code of G.721 encoded data of i and
9800 * returns the resulting linear PCM, A-law or u-law value.
9801 * return -1 for unknown out_coding value.
9804 g721_decoder(
9805 int i,
9806 G72x_STATE *state_ptr)
9808 short sezi, sei, sez, se; /* ACCUM */
9809 short y; /* MIX */
9810 short sr; /* ADDB */
9811 short dq;
9812 short dqsez;
9814 i &= 0x0f; /* mask to get proper bits */
9815 sezi = predictor_zero(state_ptr);
9816 sez = sezi >> 1;
9817 sei = sezi + predictor_pole(state_ptr);
9818 se = sei >> 1; /* se = estimated signal */
9820 y = step_size(state_ptr); /* dynamic quantizer step size */
9822 dq = reconstruct(i & 0x08, g721_dqlntab[i], y); /* quantized diff. */
9824 sr = (dq < 0) ? (se - (dq & 0x3FFF)) : se + dq; /* reconst. signal */
9826 dqsez = sr - se + sez; /* pole prediction diff. */
9828 update(4, y, g721_witab[i] << 5, g721_fitab[i], dq, sr, dqsez, state_ptr);
9830 /* sr was 14-bit dynamic range */
9831 return (sr << 2);
9834 ** Do not edit or modify anything in this comment block.
9835 ** The arch-tag line is a file identity tag for the GNU Arch
9836 ** revision control system.
9838 ** arch-tag: 101b6e25-457d-490a-99ae-e2e74a26ea24
9842 * This source code is a product of Sun Microsystems, Inc. and is provided
9843 * for unrestricted use. Users may copy or modify this source code without
9844 * charge.
9846 * SUN SOURCE CODE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING
9847 * THE WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
9848 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
9850 * Sun source code is provided with no support and without any obligation on
9851 * the part of Sun Microsystems, Inc. to assist in its use, correction,
9852 * modification or enhancement.
9854 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
9855 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS SOFTWARE
9856 * OR ANY PART THEREOF.
9858 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
9859 * or profits or other special, indirect and consequential damages, even if
9860 * Sun has been advised of the possibility of such damages.
9862 * Sun Microsystems, Inc.
9863 * 2550 Garcia Avenue
9864 * Mountain View, California 94043
9866 /* 16kbps version created, used 24kbps code and changing as little as possible.
9867 * G.726 specs are available from ITU's gopher or WWW site (http://www.itu.ch)
9868 * If any errors are found, please contact me at mrand@tamu.edu
9869 * -Marc Randolph
9873 * g723_16.c
9875 * Description:
9877 * g723_16_encoder(), g723_16_decoder()
9879 * These routines comprise an implementation of the CCITT G.726 16 Kbps
9880 * ADPCM coding algorithm. Essentially, this implementation is identical to
9881 * the bit level description except for a few deviations which take advantage
9882 * of workstation attributes, such as hardware 2's complement arithmetic.
9888 * Maps G.723_16 code word to reconstructed scale factor normalized log
9889 * magnitude values. Comes from Table 11/G.726
9891 static short g723_16_dqlntab[4] = { 116, 365, 365, 116};
9893 /* Maps G.723_16 code word to log of scale factor multiplier.
9895 * g723_16_witab[4] is actually {-22 , 439, 439, -22}, but FILTD wants it
9896 * as WI << 5 (multiplied by 32), so we'll do that here
9898 static short g723_16_witab[4] = {-704, 14048, 14048, -704};
9901 * Maps G.723_16 code words to a set of values whose long and short
9902 * term averages are computed and then compared to give an indication
9903 * how stationary (steady state) the signal is.
9906 /* Comes from FUNCTF */
9907 static short g723_16_fitab[4] = {0, 0xE00, 0xE00, 0};
9909 /* Comes from quantizer decision level tables (Table 7/G.726)
9911 static short qtab_723_16[1] = {261};
9915 * g723_16_encoder()
9917 * Encodes a linear PCM, A-law or u-law input sample and returns its 2-bit code.
9918 * Returns -1 if invalid input coding value.
9921 g723_16_encoder(
9922 int sl,
9923 G72x_STATE *state_ptr)
9925 short sei, sezi, se, sez; /* ACCUM */
9926 short d; /* SUBTA */
9927 short y; /* MIX */
9928 short sr; /* ADDB */
9929 short dqsez; /* ADDC */
9930 short dq, i;
9932 /* linearize input sample to 14-bit PCM */
9933 sl >>= 2; /* sl of 14-bit dynamic range */
9935 sezi = predictor_zero(state_ptr);
9936 sez = sezi >> 1;
9937 sei = sezi + predictor_pole(state_ptr);
9938 se = sei >> 1; /* se = estimated signal */
9940 d = sl - se; /* d = estimation diff. */
9942 /* quantize prediction difference d */
9943 y = step_size(state_ptr); /* quantizer step size */
9944 i = quantize(d, y, qtab_723_16, 1); /* i = ADPCM code */
9946 /* Since quantize() only produces a three level output
9947 * (1, 2, or 3), we must create the fourth one on our own
9949 if (i == 3) /* i code for the zero region */
9950 if ((d & 0x8000) == 0) /* If d > 0, i=3 isn't right... */
9951 i = 0;
9953 dq = reconstruct(i & 2, g723_16_dqlntab[i], y); /* quantized diff. */
9955 sr = (dq < 0) ? se - (dq & 0x3FFF) : se + dq; /* reconstructed signal */
9957 dqsez = sr + sez - se; /* pole prediction diff. */
9959 update(2, y, g723_16_witab[i], g723_16_fitab[i], dq, sr, dqsez, state_ptr);
9961 return (i);
9965 * g723_16_decoder()
9967 * Decodes a 2-bit CCITT G.723_16 ADPCM code and returns
9968 * the resulting 16-bit linear PCM, A-law or u-law sample value.
9969 * -1 is returned if the output coding is unknown.
9972 g723_16_decoder(
9973 int i,
9974 G72x_STATE *state_ptr)
9976 short sezi, sei, sez, se; /* ACCUM */
9977 short y; /* MIX */
9978 short sr; /* ADDB */
9979 short dq;
9980 short dqsez;
9982 i &= 0x03; /* mask to get proper bits */
9983 sezi = predictor_zero(state_ptr);
9984 sez = sezi >> 1;
9985 sei = sezi + predictor_pole(state_ptr);
9986 se = sei >> 1; /* se = estimated signal */
9988 y = step_size(state_ptr); /* adaptive quantizer step size */
9989 dq = reconstruct(i & 0x02, g723_16_dqlntab[i], y); /* unquantize pred diff */
9991 sr = (dq < 0) ? (se - (dq & 0x3FFF)) : (se + dq); /* reconst. signal */
9993 dqsez = sr - se + sez; /* pole prediction diff. */
9995 update(2, y, g723_16_witab[i], g723_16_fitab[i], dq, sr, dqsez, state_ptr);
9997 /* sr was of 14-bit dynamic range */
9998 return (sr << 2);
10001 ** Do not edit or modify anything in this comment block.
10002 ** The arch-tag line is a file identity tag for the GNU Arch
10003 ** revision control system.
10005 ** arch-tag: ae265466-c3fc-4f83-bb32-edae488a5ca5
10009 * This source code is a product of Sun Microsystems, Inc. and is provided
10010 * for unrestricted use. Users may copy or modify this source code without
10011 * charge.
10013 * SUN SOURCE CODE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING
10014 * THE WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
10015 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
10017 * Sun source code is provided with no support and without any obligation on
10018 * the part of Sun Microsystems, Inc. to assist in its use, correction,
10019 * modification or enhancement.
10021 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
10022 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS SOFTWARE
10023 * OR ANY PART THEREOF.
10025 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
10026 * or profits or other special, indirect and consequential damages, even if
10027 * Sun has been advised of the possibility of such damages.
10029 * Sun Microsystems, Inc.
10030 * 2550 Garcia Avenue
10031 * Mountain View, California 94043
10035 * g723_24.c
10037 * Description:
10039 * g723_24_encoder(), g723_24_decoder()
10041 * These routines comprise an implementation of the CCITT G.723 24 Kbps
10042 * ADPCM coding algorithm. Essentially, this implementation is identical to
10043 * the bit level description except for a few deviations which take advantage
10044 * of workstation attributes, such as hardware 2's complement arithmetic.
10050 * Maps G.723_24 code word to reconstructed scale factor normalized log
10051 * magnitude values.
10053 static short g723_24_dqlntab[8] = {-2048, 135, 273, 373, 373, 273, 135, -2048};
10055 /* Maps G.723_24 code word to log of scale factor multiplier. */
10056 static short g723_24_witab[8] = {-128, 960, 4384, 18624, 18624, 4384, 960, -128};
10059 * Maps G.723_24 code words to a set of values whose long and short
10060 * term averages are computed and then compared to give an indication
10061 * how stationary (steady state) the signal is.
10063 static short g723_24_fitab[8] = {0, 0x200, 0x400, 0xE00, 0xE00, 0x400, 0x200, 0};
10065 static short qtab_723_24[3] = {8, 218, 331};
10068 * g723_24_encoder()
10070 * Encodes a linear PCM, A-law or u-law input sample and returns its 3-bit code.
10071 * Returns -1 if invalid input coding value.
10074 g723_24_encoder(
10075 int sl,
10076 G72x_STATE *state_ptr)
10078 short sei, sezi, se, sez; /* ACCUM */
10079 short d; /* SUBTA */
10080 short y; /* MIX */
10081 short sr; /* ADDB */
10082 short dqsez; /* ADDC */
10083 short dq, i;
10085 /* linearize input sample to 14-bit PCM */
10086 sl >>= 2; /* sl of 14-bit dynamic range */
10088 sezi = predictor_zero(state_ptr);
10089 sez = sezi >> 1;
10090 sei = sezi + predictor_pole(state_ptr);
10091 se = sei >> 1; /* se = estimated signal */
10093 d = sl - se; /* d = estimation diff. */
10095 /* quantize prediction difference d */
10096 y = step_size(state_ptr); /* quantizer step size */
10097 i = quantize(d, y, qtab_723_24, 3); /* i = ADPCM code */
10098 dq = reconstruct(i & 4, g723_24_dqlntab[i], y); /* quantized diff. */
10100 sr = (dq < 0) ? se - (dq & 0x3FFF) : se + dq; /* reconstructed signal */
10102 dqsez = sr + sez - se; /* pole prediction diff. */
10104 update(3, y, g723_24_witab[i], g723_24_fitab[i], dq, sr, dqsez, state_ptr);
10106 return (i);
10110 * g723_24_decoder()
10112 * Decodes a 3-bit CCITT G.723_24 ADPCM code and returns
10113 * the resulting 16-bit linear PCM, A-law or u-law sample value.
10114 * -1 is returned if the output coding is unknown.
10117 g723_24_decoder(
10118 int i,
10119 G72x_STATE *state_ptr)
10121 short sezi, sei, sez, se; /* ACCUM */
10122 short y; /* MIX */
10123 short sr; /* ADDB */
10124 short dq;
10125 short dqsez;
10127 i &= 0x07; /* mask to get proper bits */
10128 sezi = predictor_zero(state_ptr);
10129 sez = sezi >> 1;
10130 sei = sezi + predictor_pole(state_ptr);
10131 se = sei >> 1; /* se = estimated signal */
10133 y = step_size(state_ptr); /* adaptive quantizer step size */
10134 dq = reconstruct(i & 0x04, g723_24_dqlntab[i], y); /* unquantize pred diff */
10136 sr = (dq < 0) ? (se - (dq & 0x3FFF)) : (se + dq); /* reconst. signal */
10138 dqsez = sr - se + sez; /* pole prediction diff. */
10140 update(3, y, g723_24_witab[i], g723_24_fitab[i], dq, sr, dqsez, state_ptr);
10142 return (sr << 2); /* sr was of 14-bit dynamic range */
10145 ** Do not edit or modify anything in this comment block.
10146 ** The arch-tag line is a file identity tag for the GNU Arch
10147 ** revision control system.
10149 ** arch-tag: 75389236-650b-4427-98f3-0df6e8fb24bc
10153 * This source code is a product of Sun Microsystems, Inc. and is provided
10154 * for unrestricted use. Users may copy or modify this source code without
10155 * charge.
10157 * SUN SOURCE CODE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING
10158 * THE WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
10159 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
10161 * Sun source code is provided with no support and without any obligation on
10162 * the part of Sun Microsystems, Inc. to assist in its use, correction,
10163 * modification or enhancement.
10165 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
10166 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS SOFTWARE
10167 * OR ANY PART THEREOF.
10169 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
10170 * or profits or other special, indirect and consequential damages, even if
10171 * Sun has been advised of the possibility of such damages.
10173 * Sun Microsystems, Inc.
10174 * 2550 Garcia Avenue
10175 * Mountain View, California 94043
10179 * g723_40.c
10181 * Description:
10183 * g723_40_encoder(), g723_40_decoder()
10185 * These routines comprise an implementation of the CCITT G.723 40Kbps
10186 * ADPCM coding algorithm. Essentially, this implementation is identical to
10187 * the bit level description except for a few deviations which
10188 * take advantage of workstation attributes, such as hardware 2's
10189 * complement arithmetic.
10191 * The deviation from the bit level specification (lookup tables),
10192 * preserves the bit level performance specifications.
10194 * As outlined in the G.723 Recommendation, the algorithm is broken
10195 * down into modules. Each section of code below is preceded by
10196 * the name of the module which it is implementing.
10202 * Maps G.723_40 code word to ructeconstructed scale factor normalized log
10203 * magnitude values.
10205 static short g723_40_dqlntab[32] = {-2048, -66, 28, 104, 169, 224, 274, 318,
10206 358, 395, 429, 459, 488, 514, 539, 566,
10207 566, 539, 514, 488, 459, 429, 395, 358,
10208 318, 274, 224, 169, 104, 28, -66, -2048};
10210 /* Maps G.723_40 code word to log of scale factor multiplier. */
10211 static short g723_40_witab[32] = {448, 448, 768, 1248, 1280, 1312, 1856, 3200,
10212 4512, 5728, 7008, 8960, 11456, 14080, 16928, 22272,
10213 22272, 16928, 14080, 11456, 8960, 7008, 5728, 4512,
10214 3200, 1856, 1312, 1280, 1248, 768, 448, 448};
10217 * Maps G.723_40 code words to a set of values whose long and short
10218 * term averages are computed and then compared to give an indication
10219 * how stationary (steady state) the signal is.
10221 static short g723_40_fitab[32] = {0, 0, 0, 0, 0, 0x200, 0x200, 0x200,
10222 0x200, 0x200, 0x400, 0x600, 0x800, 0xA00, 0xC00, 0xC00,
10223 0xC00, 0xC00, 0xA00, 0x800, 0x600, 0x400, 0x200, 0x200,
10224 0x200, 0x200, 0x200, 0, 0, 0, 0, 0};
10226 static short qtab_723_40[15] = {-122, -16, 68, 139, 198, 250, 298, 339,
10227 378, 413, 445, 475, 502, 528, 553};
10230 * g723_40_encoder()
10232 * Encodes a 16-bit linear PCM, A-law or u-law input sample and retuens
10233 * the resulting 5-bit CCITT G.723 40Kbps code.
10234 * Returns -1 if the input coding value is invalid.
10236 int g723_40_encoder (int sl, G72x_STATE *state_ptr)
10238 short sei, sezi, se, sez; /* ACCUM */
10239 short d; /* SUBTA */
10240 short y; /* MIX */
10241 short sr; /* ADDB */
10242 short dqsez; /* ADDC */
10243 short dq, i;
10245 /* linearize input sample to 14-bit PCM */
10246 sl >>= 2; /* sl of 14-bit dynamic range */
10248 sezi = predictor_zero(state_ptr);
10249 sez = sezi >> 1;
10250 sei = sezi + predictor_pole(state_ptr);
10251 se = sei >> 1; /* se = estimated signal */
10253 d = sl - se; /* d = estimation difference */
10255 /* quantize prediction difference */
10256 y = step_size(state_ptr); /* adaptive quantizer step size */
10257 i = quantize(d, y, qtab_723_40, 15); /* i = ADPCM code */
10259 dq = reconstruct(i & 0x10, g723_40_dqlntab[i], y); /* quantized diff */
10261 sr = (dq < 0) ? se - (dq & 0x7FFF) : se + dq; /* reconstructed signal */
10263 dqsez = sr + sez - se; /* dqsez = pole prediction diff. */
10265 update(5, y, g723_40_witab[i], g723_40_fitab[i], dq, sr, dqsez, state_ptr);
10267 return (i);
10271 * g723_40_decoder()
10273 * Decodes a 5-bit CCITT G.723 40Kbps code and returns
10274 * the resulting 16-bit linear PCM, A-law or u-law sample value.
10275 * -1 is returned if the output coding is unknown.
10277 int g723_40_decoder (int i, G72x_STATE *state_ptr)
10279 short sezi, sei, sez, se; /* ACCUM */
10280 short y ; /* MIX */
10281 short sr; /* ADDB */
10282 short dq;
10283 short dqsez;
10285 i &= 0x1f; /* mask to get proper bits */
10286 sezi = predictor_zero(state_ptr);
10287 sez = sezi >> 1;
10288 sei = sezi + predictor_pole(state_ptr);
10289 se = sei >> 1; /* se = estimated signal */
10291 y = step_size(state_ptr); /* adaptive quantizer step size */
10292 dq = reconstruct(i & 0x10, g723_40_dqlntab[i], y); /* estimation diff. */
10294 sr = (dq < 0) ? (se - (dq & 0x7FFF)) : (se + dq); /* reconst. signal */
10296 dqsez = sr - se + sez; /* pole prediction diff. */
10298 update(5, y, g723_40_witab[i], g723_40_fitab[i], dq, sr, dqsez, state_ptr);
10300 return (sr << 2); /* sr was of 14-bit dynamic range */
10303 ** Do not edit or modify anything in this comment block.
10304 ** The arch-tag line is a file identity tag for the GNU Arch
10305 ** revision control system.
10307 ** arch-tag: eb8d9a00-32bf-4dd2-b287-01b0336d72bf
10311 * This source code is a product of Sun Microsystems, Inc. and is provided
10312 * for unrestricted use. Users may copy or modify this source code without
10313 * charge.
10315 * SUN SOURCE CODE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING
10316 * THE WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
10317 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
10319 * Sun source code is provided with no support and without any obligation on
10320 * the part of Sun Microsystems, Inc. to assist in its use, correction,
10321 * modification or enhancement.
10323 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
10324 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS SOFTWARE
10325 * OR ANY PART THEREOF.
10327 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
10328 * or profits or other special, indirect and consequential damages, even if
10329 * Sun has been advised of the possibility of such damages.
10331 * Sun Microsystems, Inc.
10332 * 2550 Garcia Avenue
10333 * Mountain View, California 94043
10337 * g72x.c
10339 * Common routines for G.721 and G.723 conversions.
10342 #include <stdio.h>
10343 #include <stdlib.h>
10344 #include <string.h>
10347 static
10348 short power2 [15] =
10349 { 1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80,
10350 0x100, 0x200, 0x400, 0x800, 0x1000, 0x2000, 0x4000
10354 * quan()
10356 * quantizes the input val against the table of size short integers.
10357 * It returns i if table[i - 1] <= val < table[i].
10359 * Using linear search for simple coding.
10361 static
10362 int quan (int val, short *table, int size)
10364 int i;
10366 for (i = 0; i < size; i++)
10367 if (val < *table++)
10368 break;
10369 return (i);
10373 * fmult()
10375 * returns the integer product of the 14-bit integer "an" and
10376 * "floating point" representation (4-bit exponent, 6-bit mantessa) "srn".
10378 static
10379 int fmult (int an, int srn)
10381 short anmag, anexp, anmant;
10382 short wanexp, wanmant;
10383 short retval;
10385 anmag = (an > 0) ? an : ((-an) & 0x1FFF);
10386 anexp = quan(anmag, power2, 15) - 6;
10387 anmant = (anmag == 0) ? 32 :
10388 (anexp >= 0) ? anmag >> anexp : anmag << -anexp;
10389 wanexp = anexp + ((srn >> 6) & 0xF) - 13;
10392 ** The original was :
10393 ** wanmant = (anmant * (srn & 0x37) + 0x30) >> 4 ;
10394 ** but could see no valid reason for the + 0x30.
10395 ** Removed it and it improved the SNR of the codec.
10398 wanmant = (anmant * (srn & 0x37)) >> 4 ;
10400 retval = (wanexp >= 0) ? ((wanmant << wanexp) & 0x7FFF) :
10401 (wanmant >> -wanexp);
10403 return (((an ^ srn) < 0) ? -retval : retval);
10407 * private_init_state()
10409 * This routine initializes and/or resets the G72x_PRIVATE structure
10410 * pointed to by 'state_ptr'.
10411 * All the initial state values are specified in the CCITT G.721 document.
10413 void private_init_state (G72x_STATE *state_ptr)
10415 int cnta;
10417 state_ptr->yl = 34816;
10418 state_ptr->yu = 544;
10419 state_ptr->dms = 0;
10420 state_ptr->dml = 0;
10421 state_ptr->ap = 0;
10422 for (cnta = 0; cnta < 2; cnta++) {
10423 state_ptr->a[cnta] = 0;
10424 state_ptr->pk[cnta] = 0;
10425 state_ptr->sr[cnta] = 32;
10427 for (cnta = 0; cnta < 6; cnta++) {
10428 state_ptr->b[cnta] = 0;
10429 state_ptr->dq[cnta] = 32;
10431 state_ptr->td = 0;
10432 } /* private_init_state */
10434 int g72x_reader_init (G72x_DATA *data, int codec)
10435 { G72x_STATE *pstate ;
10437 if (sizeof (data->sprivateo) < sizeof (G72x_STATE))
10438 { /* This is for safety only. */
10439 return 1 ;
10442 memset (data, 0, sizeof (G72x_DATA)) ;
10444 pstate = (G72x_STATE*) data->sprivateo ;
10445 private_init_state (pstate) ;
10447 pstate->encoder = NULL ;
10449 switch (codec)
10450 { case G723_16_BITS_PER_SAMPLE : /* 2 bits per sample. */
10451 pstate->decoder = g723_16_decoder ;
10452 data->blocksize = G723_16_BYTES_PER_BLOCK ;
10453 data->samplesperblock = G723_16_SAMPLES_PER_BLOCK ;
10454 pstate->codec_bits = 2 ;
10455 break ;
10457 case G723_24_BITS_PER_SAMPLE : /* 3 bits per sample. */
10458 pstate->decoder = g723_24_decoder ;
10459 data->blocksize = G723_24_BYTES_PER_BLOCK ;
10460 data->samplesperblock = G723_24_SAMPLES_PER_BLOCK ;
10461 pstate->codec_bits = 3 ;
10462 break ;
10464 case G721_32_BITS_PER_SAMPLE : /* 4 bits per sample. */
10465 pstate->decoder = g721_decoder ;
10466 data->blocksize = G721_32_BYTES_PER_BLOCK ;
10467 data->samplesperblock = G721_32_SAMPLES_PER_BLOCK ;
10468 pstate->codec_bits = 4 ;
10469 break ;
10471 case G721_40_BITS_PER_SAMPLE : /* 5 bits per sample. */
10472 pstate->decoder = g723_40_decoder ;
10473 data->blocksize = G721_40_BYTES_PER_BLOCK ;
10474 data->samplesperblock = G721_40_SAMPLES_PER_BLOCK ;
10475 pstate->codec_bits = 5 ;
10476 break ;
10478 default : return 1 ;
10481 return 0 ;
10482 } /* g72x_reader_init */
10484 int g72x_writer_init (G72x_DATA *data, int codec)
10485 { G72x_STATE *pstate ;
10487 if (sizeof (data->sprivateo) < sizeof (G72x_STATE))
10488 { /* This is for safety only. Gets optimised out. */
10489 return 1 ;
10492 memset (data, 0, sizeof (G72x_DATA)) ;
10494 pstate = (G72x_STATE*) data->sprivateo ;
10495 private_init_state (pstate) ;
10497 pstate->decoder = NULL ;
10499 switch (codec)
10500 { case G723_16_BITS_PER_SAMPLE : /* 2 bits per sample. */
10501 pstate->encoder = g723_16_encoder ;
10502 data->blocksize = G723_16_BYTES_PER_BLOCK ;
10503 data->samplesperblock = G723_16_SAMPLES_PER_BLOCK ;
10504 pstate->codec_bits = 2 ;
10505 break ;
10507 case G723_24_BITS_PER_SAMPLE : /* 3 bits per sample. */
10508 pstate->encoder = g723_24_encoder ;
10509 data->blocksize = G723_24_BYTES_PER_BLOCK ;
10510 data->samplesperblock = G723_24_SAMPLES_PER_BLOCK ;
10511 pstate->codec_bits = 3 ;
10512 break ;
10514 case G721_32_BITS_PER_SAMPLE : /* 4 bits per sample. */
10515 pstate->encoder = g721_encoder ;
10516 data->blocksize = G721_32_BYTES_PER_BLOCK ;
10517 data->samplesperblock = G721_32_SAMPLES_PER_BLOCK ;
10518 pstate->codec_bits = 4 ;
10519 break ;
10521 case G721_40_BITS_PER_SAMPLE : /* 5 bits per sample. */
10522 pstate->encoder = g723_40_encoder ;
10523 data->blocksize = G721_40_BYTES_PER_BLOCK ;
10524 data->samplesperblock = G721_40_SAMPLES_PER_BLOCK ;
10525 pstate->codec_bits = 5 ;
10526 break ;
10528 default : return 1 ;
10531 return 0 ;
10532 } /* g72x_writer_init */
10534 int unpack_bytes (G72x_DATA *data, int bits)
10535 { unsigned int in_buffer = 0 ;
10536 unsigned char in_byte ;
10537 int k, in_bits = 0, bindex = 0 ;
10539 for (k = 0 ; bindex <= data->blocksize && k < G72x_BLOCK_SIZE ; k++)
10540 { if (in_bits < bits)
10541 { in_byte = data->block [bindex++] ;
10543 in_buffer |= (in_byte << in_bits);
10544 in_bits += 8;
10546 data->samples [k] = in_buffer & ((1 << bits) - 1);
10547 in_buffer >>= bits;
10548 in_bits -= bits;
10551 return k ;
10552 } /* unpack_bytes */
10554 int g72x_decode_block (G72x_DATA *data)
10555 { G72x_STATE *pstate ;
10556 int k, count ;
10558 pstate = (G72x_STATE*) data->sprivateo ;
10560 count = unpack_bytes (data, pstate->codec_bits) ;
10562 for (k = 0 ; k < count ; k++)
10563 data->samples [k] = pstate->decoder (data->samples [k], pstate) ;
10565 return 0 ;
10566 } /* g72x_decode_block */
10568 int pack_bytes (G72x_DATA *data, int bits)
10570 unsigned int out_buffer = 0 ;
10571 int k, bindex = 0, out_bits = 0 ;
10572 unsigned char out_byte ;
10574 for (k = 0 ; k < G72x_BLOCK_SIZE ; k++)
10575 { out_buffer |= (data->samples [k] << out_bits) ;
10576 out_bits += bits ;
10577 if (out_bits >= 8)
10578 { out_byte = out_buffer & 0xFF ;
10579 out_bits -= 8 ;
10580 out_buffer >>= 8 ;
10581 data->block [bindex++] = out_byte ;
10585 return bindex ;
10586 } /* pack_bytes */
10588 int g72x_encode_block (G72x_DATA *data)
10589 { G72x_STATE *pstate ;
10590 int k, count ;
10592 pstate = (G72x_STATE*) data->sprivateo ;
10594 for (k = 0 ; k < data->samplesperblock ; k++)
10595 data->samples [k] = pstate->encoder (data->samples [k], pstate) ;
10597 count = pack_bytes (data, pstate->codec_bits) ;
10599 return count ;
10600 } /* g72x_encode_block */
10603 * predictor_zero()
10605 * computes the estimated signal from 6-zero predictor.
10608 int predictor_zero (G72x_STATE *state_ptr)
10610 int i;
10611 int sezi;
10613 sezi = fmult(state_ptr->b[0] >> 2, state_ptr->dq[0]);
10614 for (i = 1; i < 6; i++) /* ACCUM */
10615 sezi += fmult(state_ptr->b[i] >> 2, state_ptr->dq[i]);
10616 return (sezi);
10619 * predictor_pole()
10621 * computes the estimated signal from 2-pole predictor.
10624 int predictor_pole(G72x_STATE *state_ptr)
10626 return (fmult(state_ptr->a[1] >> 2, state_ptr->sr[1]) +
10627 fmult(state_ptr->a[0] >> 2, state_ptr->sr[0]));
10630 * step_size()
10632 * computes the quantization step size of the adaptive quantizer.
10635 int step_size (G72x_STATE *state_ptr)
10637 int y;
10638 int dif;
10639 int al;
10641 if (state_ptr->ap >= 256)
10642 return (state_ptr->yu);
10643 else {
10644 y = state_ptr->yl >> 6;
10645 dif = state_ptr->yu - y;
10646 al = state_ptr->ap >> 2;
10647 if (dif > 0)
10648 y += (dif * al) >> 6;
10649 else if (dif < 0)
10650 y += (dif * al + 0x3F) >> 6;
10651 return (y);
10656 * quantize()
10658 * Given a raw sample, 'd', of the difference signal and a
10659 * quantization step size scale factor, 'y', this routine returns the
10660 * ADPCM codeword to which that sample gets quantized. The step
10661 * size scale factor division operation is done in the log base 2 domain
10662 * as a subtraction.
10664 int quantize(
10665 int d, /* Raw difference signal sample */
10666 int y, /* Step size multiplier */
10667 short *table, /* quantization table */
10668 int size) /* table size of short integers */
10670 short dqm; /* Magnitude of 'd' */
10671 short expon; /* Integer part of base 2 log of 'd' */
10672 short mant; /* Fractional part of base 2 log */
10673 short dl; /* Log of magnitude of 'd' */
10674 short dln; /* Step size scale factor normalized log */
10675 int i;
10678 * LOG
10680 * Compute base 2 log of 'd', and store in 'dl'.
10682 dqm = abs(d);
10683 expon = quan(dqm >> 1, power2, 15);
10684 mant = ((dqm << 7) >> expon) & 0x7F; /* Fractional portion. */
10685 dl = (expon << 7) + mant;
10688 * SUBTB
10690 * "Divide" by step size multiplier.
10692 dln = dl - (y >> 2);
10695 * QUAN
10697 * Obtain codword i for 'd'.
10699 i = quan(dln, table, size);
10700 if (d < 0) /* take 1's complement of i */
10701 return ((size << 1) + 1 - i);
10702 else if (i == 0) /* take 1's complement of 0 */
10703 return ((size << 1) + 1); /* new in 1988 */
10704 else
10705 return (i);
10708 * reconstruct()
10710 * Returns reconstructed difference signal 'dq' obtained from
10711 * codeword 'i' and quantization step size scale factor 'y'.
10712 * Multiplication is performed in log base 2 domain as addition.
10715 reconstruct(
10716 int sign, /* 0 for non-negative value */
10717 int dqln, /* G.72x codeword */
10718 int y) /* Step size multiplier */
10720 short dql; /* Log of 'dq' magnitude */
10721 short dex; /* Integer part of log */
10722 short dqt;
10723 short dq; /* Reconstructed difference signal sample */
10725 dql = dqln + (y >> 2); /* ADDA */
10727 if (dql < 0) {
10728 return ((sign) ? -0x8000 : 0);
10729 } else { /* ANTILOG */
10730 dex = (dql >> 7) & 15;
10731 dqt = 128 + (dql & 127);
10732 dq = (dqt << 7) >> (14 - dex);
10733 return ((sign) ? (dq - 0x8000) : dq);
10739 * update()
10741 * updates the state variables for each output code
10743 void
10744 update(
10745 int code_size, /* distinguish 723_40 with others */
10746 int y, /* quantizer step size */
10747 int wi, /* scale factor multiplier */
10748 int fi, /* for long/short term energies */
10749 int dq, /* quantized prediction difference */
10750 int sr, /* reconstructed signal */
10751 int dqsez, /* difference from 2-pole predictor */
10752 G72x_STATE *state_ptr) /* coder state pointer */
10754 int cnt;
10755 short mag, expon; /* Adaptive predictor, FLOAT A */
10756 short a2p = 0; /* LIMC */
10757 short a1ul; /* UPA1 */
10758 short pks1; /* UPA2 */
10759 short fa1;
10760 char tr; /* tone/transition detector */
10761 short ylint, thr2, dqthr;
10762 short ylfrac, thr1;
10763 short pk0;
10765 pk0 = (dqsez < 0) ? 1 : 0; /* needed in updating predictor poles */
10767 mag = dq & 0x7FFF; /* prediction difference magnitude */
10768 /* TRANS */
10769 ylint = state_ptr->yl >> 15; /* exponent part of yl */
10770 ylfrac = (state_ptr->yl >> 10) & 0x1F; /* fractional part of yl */
10771 thr1 = (32 + ylfrac) << ylint; /* threshold */
10772 thr2 = (ylint > 9) ? 31 << 10 : thr1; /* limit thr2 to 31 << 10 */
10773 dqthr = (thr2 + (thr2 >> 1)) >> 1; /* dqthr = 0.75 * thr2 */
10774 if (state_ptr->td == 0) /* signal supposed voice */
10775 tr = 0;
10776 else if (mag <= dqthr) /* supposed data, but small mag */
10777 tr = 0; /* treated as voice */
10778 else /* signal is data (modem) */
10779 tr = 1;
10782 * Quantizer scale factor adaptation.
10785 /* FUNCTW & FILTD & DELAY */
10786 /* update non-steady state step size multiplier */
10787 state_ptr->yu = y + ((wi - y) >> 5);
10789 /* LIMB */
10790 if (state_ptr->yu < 544) /* 544 <= yu <= 5120 */
10791 state_ptr->yu = 544;
10792 else if (state_ptr->yu > 5120)
10793 state_ptr->yu = 5120;
10795 /* FILTE & DELAY */
10796 /* update steady state step size multiplier */
10797 state_ptr->yl += state_ptr->yu + ((-state_ptr->yl) >> 6);
10800 * Adaptive predictor coefficients.
10802 if (tr == 1) { /* reset a's and b's for modem signal */
10803 state_ptr->a[0] = 0;
10804 state_ptr->a[1] = 0;
10805 state_ptr->b[0] = 0;
10806 state_ptr->b[1] = 0;
10807 state_ptr->b[2] = 0;
10808 state_ptr->b[3] = 0;
10809 state_ptr->b[4] = 0;
10810 state_ptr->b[5] = 0;
10811 } else { /* update a's and b's */
10812 pks1 = pk0 ^ state_ptr->pk[0]; /* UPA2 */
10814 /* update predictor pole a[1] */
10815 a2p = state_ptr->a[1] - (state_ptr->a[1] >> 7);
10816 if (dqsez != 0) {
10817 fa1 = (pks1) ? state_ptr->a[0] : -state_ptr->a[0];
10818 if (fa1 < -8191) /* a2p = function of fa1 */
10819 a2p -= 0x100;
10820 else if (fa1 > 8191)
10821 a2p += 0xFF;
10822 else
10823 a2p += fa1 >> 5;
10825 if (pk0 ^ state_ptr->pk[1])
10826 { /* LIMC */
10827 if (a2p <= -12160)
10828 a2p = -12288;
10829 else if (a2p >= 12416)
10830 a2p = 12288;
10831 else
10832 a2p -= 0x80;
10834 else if (a2p <= -12416)
10835 a2p = -12288;
10836 else if (a2p >= 12160)
10837 a2p = 12288;
10838 else
10839 a2p += 0x80;
10842 /* TRIGB & DELAY */
10843 state_ptr->a[1] = a2p;
10845 /* UPA1 */
10846 /* update predictor pole a[0] */
10847 state_ptr->a[0] -= state_ptr->a[0] >> 8;
10848 if (dqsez != 0)
10849 { if (pks1 == 0)
10850 state_ptr->a[0] += 192;
10851 else
10852 state_ptr->a[0] -= 192;
10855 /* LIMD */
10856 a1ul = 15360 - a2p;
10857 if (state_ptr->a[0] < -a1ul)
10858 state_ptr->a[0] = -a1ul;
10859 else if (state_ptr->a[0] > a1ul)
10860 state_ptr->a[0] = a1ul;
10862 /* UPB : update predictor zeros b[6] */
10863 for (cnt = 0; cnt < 6; cnt++) {
10864 if (code_size == 5) /* for 40Kbps G.723 */
10865 state_ptr->b[cnt] -= state_ptr->b[cnt] >> 9;
10866 else /* for G.721 and 24Kbps G.723 */
10867 state_ptr->b[cnt] -= state_ptr->b[cnt] >> 8;
10868 if (dq & 0x7FFF) { /* XOR */
10869 if ((dq ^ state_ptr->dq[cnt]) >= 0)
10870 state_ptr->b[cnt] += 128;
10871 else
10872 state_ptr->b[cnt] -= 128;
10877 for (cnt = 5; cnt > 0; cnt--)
10878 state_ptr->dq[cnt] = state_ptr->dq[cnt-1];
10879 /* FLOAT A : convert dq[0] to 4-bit exp, 6-bit mantissa f.p. */
10880 if (mag == 0) {
10881 state_ptr->dq[0] = (dq >= 0) ? 0x20 : 0xFC20;
10882 } else {
10883 expon = quan(mag, power2, 15);
10884 state_ptr->dq[0] = (dq >= 0) ?
10885 (expon << 6) + ((mag << 6) >> expon) :
10886 (expon << 6) + ((mag << 6) >> expon) - 0x400;
10889 state_ptr->sr[1] = state_ptr->sr[0];
10890 /* FLOAT B : convert sr to 4-bit exp., 6-bit mantissa f.p. */
10891 if (sr == 0) {
10892 state_ptr->sr[0] = 0x20;
10893 } else if (sr > 0) {
10894 expon = quan(sr, power2, 15);
10895 state_ptr->sr[0] = (expon << 6) + ((sr << 6) >> expon);
10896 } else if (sr > -32768) {
10897 mag = -sr;
10898 expon = quan(mag, power2, 15);
10899 state_ptr->sr[0] = (expon << 6) + ((mag << 6) >> expon) - 0x400;
10900 } else
10901 state_ptr->sr[0] = (short) 0xFC20;
10903 /* DELAY A */
10904 state_ptr->pk[1] = state_ptr->pk[0];
10905 state_ptr->pk[0] = pk0;
10907 /* TONE */
10908 if (tr == 1) /* this sample has been treated as data */
10909 state_ptr->td = 0; /* next one will be treated as voice */
10910 else if (a2p < -11776) /* small sample-to-sample correlation */
10911 state_ptr->td = 1; /* signal may be data */
10912 else /* signal is voice */
10913 state_ptr->td = 0;
10916 * Adaptation speed control.
10918 state_ptr->dms += (fi - state_ptr->dms) >> 5; /* FILTA */
10919 state_ptr->dml += (((fi << 2) - state_ptr->dml) >> 7); /* FILTB */
10921 if (tr == 1)
10922 state_ptr->ap = 256;
10923 else if (y < 1536) /* SUBTC */
10924 state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
10925 else if (state_ptr->td == 1)
10926 state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
10927 else if (abs((state_ptr->dms << 2) - state_ptr->dml) >=
10928 (state_ptr->dml >> 3))
10929 state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
10930 else
10931 state_ptr->ap += (-state_ptr->ap) >> 4;
10933 return ;
10934 } /* update */
10937 ** Do not edit or modify anything in this comment block.
10938 ** The arch-tag line is a file identity tag for the GNU Arch
10939 ** revision control system.
10941 ** arch-tag: 6298dc75-fd0f-4062-9b90-f73ed69f22d4
10945 ** Copyright (C) 1999-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
10947 ** This program is free software; you can redistribute it and/or modify
10948 ** it under the terms of the GNU Lesser General Public License as published by
10949 ** the Free Software Foundation; either version 2.1 of the License, or
10950 ** (at your option) any later version.
10952 ** This program is distributed in the hope that it will be useful,
10953 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
10954 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10955 ** GNU Lesser General Public License for more details.
10957 ** You should have received a copy of the GNU Lesser General Public License
10958 ** along with this program; if not, write to the Free Software
10959 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
10963 #include <stdio.h>
10964 #include <stdlib.h>
10965 #include <string.h>
10968 #define GSM610_BLOCKSIZE 33
10969 #define GSM610_SAMPLES 160
10971 typedef struct gsm610_tag
10972 { int blocks ;
10973 int blockcount, samplecount ;
10974 int samplesperblock, blocksize ;
10976 int (*decode_block) (SF_PRIVATE *psf, struct gsm610_tag *pgsm610) ;
10977 int (*encode_block) (SF_PRIVATE *psf, struct gsm610_tag *pgsm610) ;
10979 short samples [WAV_W64_GSM610_SAMPLES] ;
10980 unsigned char block [WAV_W64_GSM610_BLOCKSIZE] ;
10982 gsm gsm_data ;
10983 } GSM610_PRIVATE ;
10985 static sf_count_t gsm610_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
10986 static sf_count_t gsm610_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
10987 static sf_count_t gsm610_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
10988 static sf_count_t gsm610_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
10990 static sf_count_t gsm610_write_s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
10991 static sf_count_t gsm610_write_i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
10992 static sf_count_t gsm610_write_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
10993 static sf_count_t gsm610_write_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
10995 static int gsm610_read_block (SF_PRIVATE *psf, GSM610_PRIVATE *pgsm610, short *ptr, int len) ;
10996 static int gsm610_write_block (SF_PRIVATE *psf, GSM610_PRIVATE *pgsm610, short *ptr, int len) ;
10998 static int gsm610_decode_block (SF_PRIVATE *psf, GSM610_PRIVATE *pgsm610) ;
10999 static int gsm610_encode_block (SF_PRIVATE *psf, GSM610_PRIVATE *pgsm610) ;
11001 static int gsm610_wav_decode_block (SF_PRIVATE *psf, GSM610_PRIVATE *pgsm610) ;
11002 static int gsm610_wav_encode_block (SF_PRIVATE *psf, GSM610_PRIVATE *pgsm610) ;
11004 static sf_count_t gsm610_seek (SF_PRIVATE *psf, int mode, sf_count_t offset) ;
11006 static int gsm610_close (SF_PRIVATE *psf) ;
11008 /*============================================================================================
11009 ** WAV GSM610 initialisation function.
11013 gsm610_init (SF_PRIVATE *psf)
11014 { GSM610_PRIVATE *pgsm610 ;
11015 int true_flag = 1 ;
11017 if (psf->mode == SFM_RDWR)
11018 return SFE_BAD_MODE_RW ;
11020 psf->sf.seekable = SF_FALSE ;
11022 if (! (pgsm610 = malloc (sizeof (GSM610_PRIVATE))))
11023 return SFE_MALLOC_FAILED ;
11025 psf->fdata = (void*) pgsm610 ;
11027 memset (pgsm610, 0, sizeof (GSM610_PRIVATE)) ;
11029 /*============================================================
11031 Need separate gsm_data structs for encode and decode.
11033 ============================================================*/
11035 if (! (pgsm610->gsm_data = gsm_create ()))
11036 return SFE_MALLOC_FAILED ;
11038 if ((psf->sf.format & SF_FORMAT_TYPEMASK) == SF_FORMAT_WAV ||
11039 (psf->sf.format & SF_FORMAT_TYPEMASK) == SF_FORMAT_W64)
11040 { gsm_option (pgsm610->gsm_data, GSM_OPT_WAV49, &true_flag) ;
11042 pgsm610->encode_block = gsm610_wav_encode_block ;
11043 pgsm610->decode_block = gsm610_wav_decode_block ;
11045 pgsm610->samplesperblock = WAV_W64_GSM610_SAMPLES ;
11046 pgsm610->blocksize = WAV_W64_GSM610_BLOCKSIZE ;
11048 else
11049 { pgsm610->encode_block = gsm610_encode_block ;
11050 pgsm610->decode_block = gsm610_decode_block ;
11052 pgsm610->samplesperblock = GSM610_SAMPLES ;
11053 pgsm610->blocksize = GSM610_BLOCKSIZE ;
11056 if (psf->mode == SFM_READ)
11057 { if (psf->datalength % pgsm610->blocksize)
11058 { psf_log_printf (psf, "*** Warning : data chunk seems to be truncated.\n") ;
11059 pgsm610->blocks = psf->datalength / pgsm610->blocksize + 1 ;
11061 else
11062 pgsm610->blocks = psf->datalength / pgsm610->blocksize ;
11064 psf->sf.frames = pgsm610->samplesperblock * pgsm610->blocks ;
11066 pgsm610->decode_block (psf, pgsm610) ; /* Read first block. */
11068 psf->read_short = gsm610_read_s ;
11069 psf->read_int = gsm610_read_i ;
11070 psf->read_float = gsm610_read_f ;
11071 psf->read_double = gsm610_read_d ;
11074 if (psf->mode == SFM_WRITE)
11075 { pgsm610->blockcount = 0 ;
11076 pgsm610->samplecount = 0 ;
11078 psf->write_short = gsm610_write_s ;
11079 psf->write_int = gsm610_write_i ;
11080 psf->write_float = gsm610_write_f ;
11081 psf->write_double = gsm610_write_d ;
11084 psf->close = gsm610_close ;
11085 psf->seek = gsm610_seek ;
11087 psf->filelength = psf_get_filelen (psf) ;
11088 psf->datalength = psf->filelength - psf->dataoffset ;
11090 return 0 ;
11091 } /* gsm610_init */
11093 /*============================================================================================
11094 ** GSM 6.10 Read Functions.
11097 static int
11098 gsm610_wav_decode_block (SF_PRIVATE *psf, GSM610_PRIVATE *pgsm610)
11099 { int k ;
11101 pgsm610->blockcount ++ ;
11102 pgsm610->samplecount = 0 ;
11104 if (pgsm610->blockcount > pgsm610->blocks)
11105 { memset (pgsm610->samples, 0, WAV_W64_GSM610_SAMPLES * sizeof (short)) ;
11106 return 1 ;
11109 if ((k = psf_fread (pgsm610->block, 1, WAV_W64_GSM610_BLOCKSIZE, psf)) != WAV_W64_GSM610_BLOCKSIZE)
11110 psf_log_printf (psf, "*** Warning : short read (%d != %d).\n", k, WAV_W64_GSM610_BLOCKSIZE) ;
11112 if (gsm_decode (pgsm610->gsm_data, pgsm610->block, pgsm610->samples) < 0)
11113 { psf_log_printf (psf, "Error from gsm_decode() on frame : %d\n", pgsm610->blockcount) ;
11114 return 0 ;
11117 if (gsm_decode (pgsm610->gsm_data, pgsm610->block + (WAV_W64_GSM610_BLOCKSIZE + 1) / 2, pgsm610->samples + WAV_W64_GSM610_SAMPLES / 2) < 0)
11118 { psf_log_printf (psf, "Error from gsm_decode() on frame : %d.5\n", pgsm610->blockcount) ;
11119 return 0 ;
11122 return 1 ;
11123 } /* gsm610_wav_decode_block */
11125 static int
11126 gsm610_decode_block (SF_PRIVATE *psf, GSM610_PRIVATE *pgsm610)
11127 { int k ;
11129 pgsm610->blockcount ++ ;
11130 pgsm610->samplecount = 0 ;
11132 if (pgsm610->blockcount > pgsm610->blocks)
11133 { memset (pgsm610->samples, 0, GSM610_SAMPLES * sizeof (short)) ;
11134 return 1 ;
11137 if ((k = psf_fread (pgsm610->block, 1, GSM610_BLOCKSIZE, psf)) != GSM610_BLOCKSIZE)
11138 psf_log_printf (psf, "*** Warning : short read (%d != %d).\n", k, GSM610_BLOCKSIZE) ;
11140 if (gsm_decode (pgsm610->gsm_data, pgsm610->block, pgsm610->samples) < 0)
11141 { psf_log_printf (psf, "Error from gsm_decode() on frame : %d\n", pgsm610->blockcount) ;
11142 return 0 ;
11145 return 1 ;
11146 } /* gsm610_decode_block */
11148 static int
11149 gsm610_read_block (SF_PRIVATE *psf, GSM610_PRIVATE *pgsm610, short *ptr, int len)
11150 { int count, total = 0, indx = 0 ;
11152 while (indx < len)
11153 { if (pgsm610->blockcount >= pgsm610->blocks && pgsm610->samplecount >= pgsm610->samplesperblock)
11154 { memset (&(ptr [indx]), 0, (size_t) ((len - indx) * sizeof (short))) ;
11155 return total ;
11158 if (pgsm610->samplecount >= pgsm610->samplesperblock)
11159 pgsm610->decode_block (psf, pgsm610) ;
11161 count = pgsm610->samplesperblock - pgsm610->samplecount ;
11162 count = (len - indx > count) ? count : len - indx ;
11164 memcpy (&(ptr [indx]), &(pgsm610->samples [pgsm610->samplecount]), count * sizeof (short)) ;
11165 indx += count ;
11166 pgsm610->samplecount += count ;
11167 total = indx ;
11170 return total ;
11171 } /* gsm610_read_block */
11173 static sf_count_t
11174 gsm610_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
11175 { GSM610_PRIVATE *pgsm610 ;
11176 int readcount, count ;
11177 sf_count_t total = 0 ;
11179 if (! psf->fdata)
11180 return 0 ;
11181 pgsm610 = (GSM610_PRIVATE*) psf->fdata ;
11183 while (len > 0)
11184 { readcount = (len > 0x10000000) ? 0x1000000 : (int) len ;
11186 count = gsm610_read_block (psf, pgsm610, ptr, readcount) ;
11188 total += count ;
11189 len -= count ;
11191 if (count != readcount)
11192 break ;
11195 return total ;
11196 } /* gsm610_read_s */
11198 static sf_count_t
11199 gsm610_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
11200 { GSM610_PRIVATE *pgsm610 ;
11201 short *sptr ;
11202 int k, bufferlen, readcount = 0, count ;
11203 sf_count_t total = 0 ;
11205 if (! psf->fdata)
11206 return 0 ;
11207 pgsm610 = (GSM610_PRIVATE*) psf->fdata ;
11209 sptr = (short*) psf->buffer ;
11210 bufferlen = SF_BUFFER_LEN / sizeof (short) ;
11211 while (len > 0)
11212 { readcount = (len >= bufferlen) ? bufferlen : len ;
11213 count = gsm610_read_block (psf, pgsm610, sptr, readcount) ;
11214 for (k = 0 ; k < readcount ; k++)
11215 ptr [total + k] = sptr [k] << 16 ;
11217 total += count ;
11218 len -= readcount ;
11220 return total ;
11221 } /* gsm610_read_i */
11223 static sf_count_t
11224 gsm610_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
11225 { GSM610_PRIVATE *pgsm610 ;
11226 short *sptr ;
11227 int k, bufferlen, readcount = 0, count ;
11228 sf_count_t total = 0 ;
11229 float normfact ;
11231 if (! psf->fdata)
11232 return 0 ;
11233 pgsm610 = (GSM610_PRIVATE*) psf->fdata ;
11235 normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x8000) : 1.0 ;
11237 sptr = (short*) psf->buffer ;
11238 bufferlen = SF_BUFFER_LEN / sizeof (short) ;
11239 while (len > 0)
11240 { readcount = (len >= bufferlen) ? bufferlen : len ;
11241 count = gsm610_read_block (psf, pgsm610, sptr, readcount) ;
11242 for (k = 0 ; k < readcount ; k++)
11243 ptr [total + k] = normfact * sptr [k] ;
11245 total += count ;
11246 len -= readcount ;
11248 return total ;
11249 } /* gsm610_read_f */
11251 static sf_count_t
11252 gsm610_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
11253 { GSM610_PRIVATE *pgsm610 ;
11254 short *sptr ;
11255 int k, bufferlen, readcount = 0, count ;
11256 sf_count_t total = 0 ;
11257 double normfact ;
11259 normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x8000) : 1.0 ;
11261 if (! psf->fdata)
11262 return 0 ;
11263 pgsm610 = (GSM610_PRIVATE*) psf->fdata ;
11265 sptr = (short*) psf->buffer ;
11266 bufferlen = SF_BUFFER_LEN / sizeof (short) ;
11267 while (len > 0)
11268 { readcount = (len >= bufferlen) ? bufferlen : len ;
11269 count = gsm610_read_block (psf, pgsm610, sptr, readcount) ;
11270 for (k = 0 ; k < readcount ; k++)
11271 ptr [total + k] = normfact * sptr [k] ;
11273 total += count ;
11274 len -= readcount ;
11276 return total ;
11277 } /* gsm610_read_d */
11279 static sf_count_t
11280 gsm610_seek (SF_PRIVATE *psf, int mode, sf_count_t offset)
11281 { GSM610_PRIVATE *pgsm610 ;
11282 int newblock, newsample ;
11284 mode = mode ;
11286 if (! psf->fdata)
11287 return 0 ;
11288 pgsm610 = (GSM610_PRIVATE*) psf->fdata ;
11290 if (psf->dataoffset < 0)
11291 { psf->error = SFE_BAD_SEEK ;
11292 return ((sf_count_t) -1) ;
11295 if (offset == 0)
11296 { int true_flag = 1 ;
11298 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
11299 pgsm610->blockcount = 0 ;
11301 gsm_init (pgsm610->gsm_data) ;
11302 if ((psf->sf.format & SF_FORMAT_TYPEMASK) == SF_FORMAT_WAV ||
11303 (psf->sf.format & SF_FORMAT_TYPEMASK) == SF_FORMAT_W64)
11304 gsm_option (pgsm610->gsm_data, GSM_OPT_WAV49, &true_flag) ;
11306 pgsm610->decode_block (psf, pgsm610) ;
11307 pgsm610->samplecount = 0 ;
11308 return 0 ;
11311 if (offset < 0 || offset > pgsm610->blocks * pgsm610->samplesperblock)
11312 { psf->error = SFE_BAD_SEEK ;
11313 return ((sf_count_t) -1) ;
11316 newblock = offset / pgsm610->samplesperblock ;
11317 newsample = offset % pgsm610->samplesperblock ;
11319 if (psf->mode == SFM_READ)
11320 { if (psf->read_current != newblock * pgsm610->samplesperblock + newsample)
11321 { psf_fseek (psf, psf->dataoffset + newblock * pgsm610->samplesperblock, SEEK_SET) ;
11322 pgsm610->blockcount = newblock ;
11323 pgsm610->decode_block (psf, pgsm610) ;
11324 pgsm610->samplecount = newsample ;
11327 return newblock * pgsm610->samplesperblock + newsample ;
11330 /* What to do about write??? */
11331 psf->error = SFE_BAD_SEEK ;
11332 return ((sf_count_t) -1) ;
11333 } /* gsm610_seek */
11335 /*==========================================================================================
11336 ** GSM 6.10 Write Functions.
11339 static int
11340 gsm610_encode_block (SF_PRIVATE *psf, GSM610_PRIVATE *pgsm610)
11341 { int k ;
11343 /* Encode the samples. */
11344 gsm_encode (pgsm610->gsm_data, pgsm610->samples, pgsm610->block) ;
11346 /* Write the block to disk. */
11347 if ((k = psf_fwrite (pgsm610->block, 1, GSM610_BLOCKSIZE, psf)) != GSM610_BLOCKSIZE)
11348 psf_log_printf (psf, "*** Warning : short write (%d != %d).\n", k, GSM610_BLOCKSIZE) ;
11350 pgsm610->samplecount = 0 ;
11351 pgsm610->blockcount ++ ;
11353 /* Set samples to zero for next block. */
11354 memset (pgsm610->samples, 0, WAV_W64_GSM610_SAMPLES * sizeof (short)) ;
11356 return 1 ;
11357 } /* gsm610_encode_block */
11359 static int
11360 gsm610_wav_encode_block (SF_PRIVATE *psf, GSM610_PRIVATE *pgsm610)
11361 { int k ;
11363 /* Encode the samples. */
11364 gsm_encode (pgsm610->gsm_data, pgsm610->samples, pgsm610->block) ;
11365 gsm_encode (pgsm610->gsm_data, pgsm610->samples+WAV_W64_GSM610_SAMPLES/2, pgsm610->block+WAV_W64_GSM610_BLOCKSIZE/2) ;
11367 /* Write the block to disk. */
11368 if ((k = psf_fwrite (pgsm610->block, 1, WAV_W64_GSM610_BLOCKSIZE, psf)) != WAV_W64_GSM610_BLOCKSIZE)
11369 psf_log_printf (psf, "*** Warning : short write (%d != %d).\n", k, WAV_W64_GSM610_BLOCKSIZE) ;
11371 pgsm610->samplecount = 0 ;
11372 pgsm610->blockcount ++ ;
11374 /* Set samples to zero for next block. */
11375 memset (pgsm610->samples, 0, WAV_W64_GSM610_SAMPLES * sizeof (short)) ;
11377 return 1 ;
11378 } /* gsm610_wav_encode_block */
11380 static int
11381 gsm610_write_block (SF_PRIVATE *psf, GSM610_PRIVATE *pgsm610, short *ptr, int len)
11382 { int count, total = 0, indx = 0 ;
11384 while (indx < len)
11385 { count = pgsm610->samplesperblock - pgsm610->samplecount ;
11387 if (count > len - indx)
11388 count = len - indx ;
11390 memcpy (&(pgsm610->samples [pgsm610->samplecount]), &(ptr [indx]), count * sizeof (short)) ;
11391 indx += count ;
11392 pgsm610->samplecount += count ;
11393 total = indx ;
11395 if (pgsm610->samplecount >= pgsm610->samplesperblock)
11396 pgsm610->encode_block (psf, pgsm610) ;
11399 return total ;
11400 } /* gsm610_write_block */
11402 static sf_count_t
11403 gsm610_write_s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
11404 { GSM610_PRIVATE *pgsm610 ;
11405 int writecount, count ;
11406 sf_count_t total = 0 ;
11408 if (! psf->fdata)
11409 return 0 ;
11410 pgsm610 = (GSM610_PRIVATE*) psf->fdata ;
11412 while (len > 0)
11413 { writecount = (len > 0x10000000) ? 0x10000000 : (int) len ;
11415 count = gsm610_write_block (psf, pgsm610, ptr, writecount) ;
11417 total += count ;
11418 len -= count ;
11420 if (count != writecount)
11421 break ;
11424 return total ;
11425 } /* gsm610_write_s */
11427 static sf_count_t
11428 gsm610_write_i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
11429 { GSM610_PRIVATE *pgsm610 ;
11430 short *sptr ;
11431 int k, bufferlen, writecount = 0, count ;
11432 sf_count_t total = 0 ;
11434 if (! psf->fdata)
11435 return 0 ;
11436 pgsm610 = (GSM610_PRIVATE*) psf->fdata ;
11438 sptr = (short*) psf->buffer ;
11439 bufferlen = SF_BUFFER_LEN / sizeof (short) ;
11440 while (len > 0)
11441 { writecount = (len >= bufferlen) ? bufferlen : len ;
11442 for (k = 0 ; k < writecount ; k++)
11443 sptr [k] = ptr [total + k] >> 16 ;
11444 count = gsm610_write_block (psf, pgsm610, sptr, writecount) ;
11446 total += count ;
11447 len -= writecount ;
11449 return total ;
11450 } /* gsm610_write_i */
11452 static sf_count_t
11453 gsm610_write_f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
11454 { GSM610_PRIVATE *pgsm610 ;
11455 short *sptr ;
11456 int k, bufferlen, writecount = 0, count ;
11457 sf_count_t total = 0 ;
11458 float normfact ;
11460 if (! psf->fdata)
11461 return 0 ;
11462 pgsm610 = (GSM610_PRIVATE*) psf->fdata ;
11464 normfact = (psf->norm_float == SF_TRUE) ? (1.0 * 0x7FFF) : 1.0 ;
11466 sptr = (short*) psf->buffer ;
11467 bufferlen = SF_BUFFER_LEN / sizeof (short) ;
11468 while (len > 0)
11469 { writecount = (len >= bufferlen) ? bufferlen : len ;
11470 for (k = 0 ; k < writecount ; k++)
11471 sptr [k] = lrintf (normfact * ptr [total + k]) ;
11472 count = gsm610_write_block (psf, pgsm610, sptr, writecount) ;
11474 total += count ;
11475 len -= writecount ;
11477 return total ;
11478 } /* gsm610_write_f */
11480 static sf_count_t
11481 gsm610_write_d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
11482 { GSM610_PRIVATE *pgsm610 ;
11483 short *sptr ;
11484 int k, bufferlen, writecount = 0, count ;
11485 sf_count_t total = 0 ;
11486 double normfact ;
11488 if (! psf->fdata)
11489 return 0 ;
11490 pgsm610 = (GSM610_PRIVATE*) psf->fdata ;
11492 normfact = (psf->norm_double == SF_TRUE) ? (1.0 * 0x7FFF) : 1.0 ;
11494 sptr = (short*) psf->buffer ;
11495 bufferlen = SF_BUFFER_LEN / sizeof (short) ;
11496 while (len > 0)
11497 { writecount = (len >= bufferlen) ? bufferlen : len ;
11498 for (k = 0 ; k < writecount ; k++)
11499 sptr [k] = lrint (normfact * ptr [total + k]) ;
11500 count = gsm610_write_block (psf, pgsm610, sptr, writecount) ;
11502 total += count ;
11503 len -= writecount ;
11505 return total ;
11506 } /* gsm610_write_d */
11508 static int
11509 gsm610_close (SF_PRIVATE *psf)
11510 { GSM610_PRIVATE *pgsm610 ;
11512 if (! psf->fdata)
11513 return 0 ;
11515 pgsm610 = (GSM610_PRIVATE*) psf->fdata ;
11517 if (psf->mode == SFM_WRITE)
11518 { /* If a block has been partially assembled, write it out
11519 ** as the final block.
11522 if (pgsm610->samplecount && pgsm610->samplecount < pgsm610->samplesperblock)
11523 pgsm610->encode_block (psf, pgsm610) ;
11525 if (psf->write_header)
11526 psf->write_header (psf, SF_TRUE) ;
11529 if (pgsm610->gsm_data)
11530 gsm_destroy (pgsm610->gsm_data) ;
11532 return 0 ;
11533 } /* gsm610_close */
11536 ** Do not edit or modify anything in this comment block.
11537 ** The arch-tag line is a file identity tag for the GNU Arch
11538 ** revision control system.
11540 ** arch-tag: 8575187d-af4f-4acf-b9dd-6ff705628345
11543 * Copyright 1992 by Jutta Degener and Carsten Bormann, Technische
11544 * Universitaet Berlin. See the accompanying file "COPYRIGHT" for
11545 * details. THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE.
11549 #include <stdio.h>
11550 #include <stdlib.h>
11551 #include <string.h>
11556 gsm gsm_create (void)
11558 gsm r;
11560 r = malloc (sizeof(struct gsm_state));
11561 if (!r) return r;
11563 memset((char *)r, 0, sizeof (struct gsm_state));
11564 r->nrp = 40;
11566 return r;
11569 /* Added for libsndfile : May 6, 2002. Not sure if it works. */
11570 void gsm_init (gsm state)
11572 memset (state, 0, sizeof (struct gsm_state)) ;
11573 state->nrp = 40 ;
11576 ** Do not edit or modify anything in this comment block.
11577 ** The arch-tag line is a file identity tag for the GNU Arch
11578 ** revision control system.
11580 ** arch-tag: 9fedb6b3-ed99-40c2-aac1-484c536261fe
11584 * Copyright 1992 by Jutta Degener and Carsten Bormann, Technische
11585 * Universitaet Berlin. See the accompanying file "COPYRIGHT" for
11586 * details. THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE.
11591 int gsm_decode (gsm s, gsm_byte * c, gsm_signal * target)
11593 word LARc[8], Nc[4], Mc[4], bc[4], xmaxc[4], xmc[13*4];
11595 #ifdef WAV49
11596 if (s->wav_fmt) {
11598 uword sr = 0;
11600 s->frame_index = !s->frame_index;
11601 if (s->frame_index) {
11603 sr = *c++;
11604 LARc[0] = sr & 0x3f; sr >>= 6;
11605 sr |= (uword)*c++ << 2;
11606 LARc[1] = sr & 0x3f; sr >>= 6;
11607 sr |= (uword)*c++ << 4;
11608 LARc[2] = sr & 0x1f; sr >>= 5;
11609 LARc[3] = sr & 0x1f; sr >>= 5;
11610 sr |= (uword)*c++ << 2;
11611 LARc[4] = sr & 0xf; sr >>= 4;
11612 LARc[5] = sr & 0xf; sr >>= 4;
11613 sr |= (uword)*c++ << 2; /* 5 */
11614 LARc[6] = sr & 0x7; sr >>= 3;
11615 LARc[7] = sr & 0x7; sr >>= 3;
11616 sr |= (uword)*c++ << 4;
11617 Nc[0] = sr & 0x7f; sr >>= 7;
11618 bc[0] = sr & 0x3; sr >>= 2;
11619 Mc[0] = sr & 0x3; sr >>= 2;
11620 sr |= (uword)*c++ << 1;
11621 xmaxc[0] = sr & 0x3f; sr >>= 6;
11622 xmc[0] = sr & 0x7; sr >>= 3;
11623 sr = *c++;
11624 xmc[1] = sr & 0x7; sr >>= 3;
11625 xmc[2] = sr & 0x7; sr >>= 3;
11626 sr |= (uword)*c++ << 2;
11627 xmc[3] = sr & 0x7; sr >>= 3;
11628 xmc[4] = sr & 0x7; sr >>= 3;
11629 xmc[5] = sr & 0x7; sr >>= 3;
11630 sr |= (uword)*c++ << 1; /* 10 */
11631 xmc[6] = sr & 0x7; sr >>= 3;
11632 xmc[7] = sr & 0x7; sr >>= 3;
11633 xmc[8] = sr & 0x7; sr >>= 3;
11634 sr = *c++;
11635 xmc[9] = sr & 0x7; sr >>= 3;
11636 xmc[10] = sr & 0x7; sr >>= 3;
11637 sr |= (uword)*c++ << 2;
11638 xmc[11] = sr & 0x7; sr >>= 3;
11639 xmc[12] = sr & 0x7; sr >>= 3;
11640 sr |= (uword)*c++ << 4;
11641 Nc[1] = sr & 0x7f; sr >>= 7;
11642 bc[1] = sr & 0x3; sr >>= 2;
11643 Mc[1] = sr & 0x3; sr >>= 2;
11644 sr |= (uword)*c++ << 1;
11645 xmaxc[1] = sr & 0x3f; sr >>= 6;
11646 xmc[13] = sr & 0x7; sr >>= 3;
11647 sr = *c++; /* 15 */
11648 xmc[14] = sr & 0x7; sr >>= 3;
11649 xmc[15] = sr & 0x7; sr >>= 3;
11650 sr |= (uword)*c++ << 2;
11651 xmc[16] = sr & 0x7; sr >>= 3;
11652 xmc[17] = sr & 0x7; sr >>= 3;
11653 xmc[18] = sr & 0x7; sr >>= 3;
11654 sr |= (uword)*c++ << 1;
11655 xmc[19] = sr & 0x7; sr >>= 3;
11656 xmc[20] = sr & 0x7; sr >>= 3;
11657 xmc[21] = sr & 0x7; sr >>= 3;
11658 sr = *c++;
11659 xmc[22] = sr & 0x7; sr >>= 3;
11660 xmc[23] = sr & 0x7; sr >>= 3;
11661 sr |= (uword)*c++ << 2;
11662 xmc[24] = sr & 0x7; sr >>= 3;
11663 xmc[25] = sr & 0x7; sr >>= 3;
11664 sr |= (uword)*c++ << 4; /* 20 */
11665 Nc[2] = sr & 0x7f; sr >>= 7;
11666 bc[2] = sr & 0x3; sr >>= 2;
11667 Mc[2] = sr & 0x3; sr >>= 2;
11668 sr |= (uword)*c++ << 1;
11669 xmaxc[2] = sr & 0x3f; sr >>= 6;
11670 xmc[26] = sr & 0x7; sr >>= 3;
11671 sr = *c++;
11672 xmc[27] = sr & 0x7; sr >>= 3;
11673 xmc[28] = sr & 0x7; sr >>= 3;
11674 sr |= (uword)*c++ << 2;
11675 xmc[29] = sr & 0x7; sr >>= 3;
11676 xmc[30] = sr & 0x7; sr >>= 3;
11677 xmc[31] = sr & 0x7; sr >>= 3;
11678 sr |= (uword)*c++ << 1;
11679 xmc[32] = sr & 0x7; sr >>= 3;
11680 xmc[33] = sr & 0x7; sr >>= 3;
11681 xmc[34] = sr & 0x7; sr >>= 3;
11682 sr = *c++; /* 25 */
11683 xmc[35] = sr & 0x7; sr >>= 3;
11684 xmc[36] = sr & 0x7; sr >>= 3;
11685 sr |= (uword)*c++ << 2;
11686 xmc[37] = sr & 0x7; sr >>= 3;
11687 xmc[38] = sr & 0x7; sr >>= 3;
11688 sr |= (uword)*c++ << 4;
11689 Nc[3] = sr & 0x7f; sr >>= 7;
11690 bc[3] = sr & 0x3; sr >>= 2;
11691 Mc[3] = sr & 0x3; sr >>= 2;
11692 sr |= (uword)*c++ << 1;
11693 xmaxc[3] = sr & 0x3f; sr >>= 6;
11694 xmc[39] = sr & 0x7; sr >>= 3;
11695 sr = *c++;
11696 xmc[40] = sr & 0x7; sr >>= 3;
11697 xmc[41] = sr & 0x7; sr >>= 3;
11698 sr |= (uword)*c++ << 2; /* 30 */
11699 xmc[42] = sr & 0x7; sr >>= 3;
11700 xmc[43] = sr & 0x7; sr >>= 3;
11701 xmc[44] = sr & 0x7; sr >>= 3;
11702 sr |= (uword)*c++ << 1;
11703 xmc[45] = sr & 0x7; sr >>= 3;
11704 xmc[46] = sr & 0x7; sr >>= 3;
11705 xmc[47] = sr & 0x7; sr >>= 3;
11706 sr = *c++;
11707 xmc[48] = sr & 0x7; sr >>= 3;
11708 xmc[49] = sr & 0x7; sr >>= 3;
11709 sr |= (uword)*c++ << 2;
11710 xmc[50] = sr & 0x7; sr >>= 3;
11711 xmc[51] = sr & 0x7; sr >>= 3;
11713 s->frame_chain = sr & 0xf;
11715 else {
11716 sr = s->frame_chain;
11717 sr |= (uword)*c++ << 4; /* 1 */
11718 LARc[0] = sr & 0x3f; sr >>= 6;
11719 LARc[1] = sr & 0x3f; sr >>= 6;
11720 sr = *c++;
11721 LARc[2] = sr & 0x1f; sr >>= 5;
11722 sr |= (uword)*c++ << 3;
11723 LARc[3] = sr & 0x1f; sr >>= 5;
11724 LARc[4] = sr & 0xf; sr >>= 4;
11725 sr |= (uword)*c++ << 2;
11726 LARc[5] = sr & 0xf; sr >>= 4;
11727 LARc[6] = sr & 0x7; sr >>= 3;
11728 LARc[7] = sr & 0x7; sr >>= 3;
11729 sr = *c++; /* 5 */
11730 Nc[0] = sr & 0x7f; sr >>= 7;
11731 sr |= (uword)*c++ << 1;
11732 bc[0] = sr & 0x3; sr >>= 2;
11733 Mc[0] = sr & 0x3; sr >>= 2;
11734 sr |= (uword)*c++ << 5;
11735 xmaxc[0] = sr & 0x3f; sr >>= 6;
11736 xmc[0] = sr & 0x7; sr >>= 3;
11737 xmc[1] = sr & 0x7; sr >>= 3;
11738 sr |= (uword)*c++ << 1;
11739 xmc[2] = sr & 0x7; sr >>= 3;
11740 xmc[3] = sr & 0x7; sr >>= 3;
11741 xmc[4] = sr & 0x7; sr >>= 3;
11742 sr = *c++;
11743 xmc[5] = sr & 0x7; sr >>= 3;
11744 xmc[6] = sr & 0x7; sr >>= 3;
11745 sr |= (uword)*c++ << 2; /* 10 */
11746 xmc[7] = sr & 0x7; sr >>= 3;
11747 xmc[8] = sr & 0x7; sr >>= 3;
11748 xmc[9] = sr & 0x7; sr >>= 3;
11749 sr |= (uword)*c++ << 1;
11750 xmc[10] = sr & 0x7; sr >>= 3;
11751 xmc[11] = sr & 0x7; sr >>= 3;
11752 xmc[12] = sr & 0x7; sr >>= 3;
11753 sr = *c++;
11754 Nc[1] = sr & 0x7f; sr >>= 7;
11755 sr |= (uword)*c++ << 1;
11756 bc[1] = sr & 0x3; sr >>= 2;
11757 Mc[1] = sr & 0x3; sr >>= 2;
11758 sr |= (uword)*c++ << 5;
11759 xmaxc[1] = sr & 0x3f; sr >>= 6;
11760 xmc[13] = sr & 0x7; sr >>= 3;
11761 xmc[14] = sr & 0x7; sr >>= 3;
11762 sr |= (uword)*c++ << 1; /* 15 */
11763 xmc[15] = sr & 0x7; sr >>= 3;
11764 xmc[16] = sr & 0x7; sr >>= 3;
11765 xmc[17] = sr & 0x7; sr >>= 3;
11766 sr = *c++;
11767 xmc[18] = sr & 0x7; sr >>= 3;
11768 xmc[19] = sr & 0x7; sr >>= 3;
11769 sr |= (uword)*c++ << 2;
11770 xmc[20] = sr & 0x7; sr >>= 3;
11771 xmc[21] = sr & 0x7; sr >>= 3;
11772 xmc[22] = sr & 0x7; sr >>= 3;
11773 sr |= (uword)*c++ << 1;
11774 xmc[23] = sr & 0x7; sr >>= 3;
11775 xmc[24] = sr & 0x7; sr >>= 3;
11776 xmc[25] = sr & 0x7; sr >>= 3;
11777 sr = *c++;
11778 Nc[2] = sr & 0x7f; sr >>= 7;
11779 sr |= (uword)*c++ << 1; /* 20 */
11780 bc[2] = sr & 0x3; sr >>= 2;
11781 Mc[2] = sr & 0x3; sr >>= 2;
11782 sr |= (uword)*c++ << 5;
11783 xmaxc[2] = sr & 0x3f; sr >>= 6;
11784 xmc[26] = sr & 0x7; sr >>= 3;
11785 xmc[27] = sr & 0x7; sr >>= 3;
11786 sr |= (uword)*c++ << 1;
11787 xmc[28] = sr & 0x7; sr >>= 3;
11788 xmc[29] = sr & 0x7; sr >>= 3;
11789 xmc[30] = sr & 0x7; sr >>= 3;
11790 sr = *c++;
11791 xmc[31] = sr & 0x7; sr >>= 3;
11792 xmc[32] = sr & 0x7; sr >>= 3;
11793 sr |= (uword)*c++ << 2;
11794 xmc[33] = sr & 0x7; sr >>= 3;
11795 xmc[34] = sr & 0x7; sr >>= 3;
11796 xmc[35] = sr & 0x7; sr >>= 3;
11797 sr |= (uword)*c++ << 1; /* 25 */
11798 xmc[36] = sr & 0x7; sr >>= 3;
11799 xmc[37] = sr & 0x7; sr >>= 3;
11800 xmc[38] = sr & 0x7; sr >>= 3;
11801 sr = *c++;
11802 Nc[3] = sr & 0x7f; sr >>= 7;
11803 sr |= (uword)*c++ << 1;
11804 bc[3] = sr & 0x3; sr >>= 2;
11805 Mc[3] = sr & 0x3; sr >>= 2;
11806 sr |= (uword)*c++ << 5;
11807 xmaxc[3] = sr & 0x3f; sr >>= 6;
11808 xmc[39] = sr & 0x7; sr >>= 3;
11809 xmc[40] = sr & 0x7; sr >>= 3;
11810 sr |= (uword)*c++ << 1;
11811 xmc[41] = sr & 0x7; sr >>= 3;
11812 xmc[42] = sr & 0x7; sr >>= 3;
11813 xmc[43] = sr & 0x7; sr >>= 3;
11814 sr = *c++; /* 30 */
11815 xmc[44] = sr & 0x7; sr >>= 3;
11816 xmc[45] = sr & 0x7; sr >>= 3;
11817 sr |= (uword)*c++ << 2;
11818 xmc[46] = sr & 0x7; sr >>= 3;
11819 xmc[47] = sr & 0x7; sr >>= 3;
11820 xmc[48] = sr & 0x7; sr >>= 3;
11821 sr |= (uword)*c++ << 1;
11822 xmc[49] = sr & 0x7; sr >>= 3;
11823 xmc[50] = sr & 0x7; sr >>= 3;
11824 xmc[51] = sr & 0x7; sr >>= 3;
11827 else
11828 #endif
11830 /* GSM_MAGIC = (*c >> 4) & 0xF; */
11832 if (((*c >> 4) & 0x0F) != GSM_MAGIC) return -1;
11834 LARc[0] = (*c++ & 0xF) << 2; /* 1 */
11835 LARc[0] |= (*c >> 6) & 0x3;
11836 LARc[1] = *c++ & 0x3F;
11837 LARc[2] = (*c >> 3) & 0x1F;
11838 LARc[3] = (*c++ & 0x7) << 2;
11839 LARc[3] |= (*c >> 6) & 0x3;
11840 LARc[4] = (*c >> 2) & 0xF;
11841 LARc[5] = (*c++ & 0x3) << 2;
11842 LARc[5] |= (*c >> 6) & 0x3;
11843 LARc[6] = (*c >> 3) & 0x7;
11844 LARc[7] = *c++ & 0x7;
11845 Nc[0] = (*c >> 1) & 0x7F;
11846 bc[0] = (*c++ & 0x1) << 1;
11847 bc[0] |= (*c >> 7) & 0x1;
11848 Mc[0] = (*c >> 5) & 0x3;
11849 xmaxc[0] = (*c++ & 0x1F) << 1;
11850 xmaxc[0] |= (*c >> 7) & 0x1;
11851 xmc[0] = (*c >> 4) & 0x7;
11852 xmc[1] = (*c >> 1) & 0x7;
11853 xmc[2] = (*c++ & 0x1) << 2;
11854 xmc[2] |= (*c >> 6) & 0x3;
11855 xmc[3] = (*c >> 3) & 0x7;
11856 xmc[4] = *c++ & 0x7;
11857 xmc[5] = (*c >> 5) & 0x7;
11858 xmc[6] = (*c >> 2) & 0x7;
11859 xmc[7] = (*c++ & 0x3) << 1; /* 10 */
11860 xmc[7] |= (*c >> 7) & 0x1;
11861 xmc[8] = (*c >> 4) & 0x7;
11862 xmc[9] = (*c >> 1) & 0x7;
11863 xmc[10] = (*c++ & 0x1) << 2;
11864 xmc[10] |= (*c >> 6) & 0x3;
11865 xmc[11] = (*c >> 3) & 0x7;
11866 xmc[12] = *c++ & 0x7;
11867 Nc[1] = (*c >> 1) & 0x7F;
11868 bc[1] = (*c++ & 0x1) << 1;
11869 bc[1] |= (*c >> 7) & 0x1;
11870 Mc[1] = (*c >> 5) & 0x3;
11871 xmaxc[1] = (*c++ & 0x1F) << 1;
11872 xmaxc[1] |= (*c >> 7) & 0x1;
11873 xmc[13] = (*c >> 4) & 0x7;
11874 xmc[14] = (*c >> 1) & 0x7;
11875 xmc[15] = (*c++ & 0x1) << 2;
11876 xmc[15] |= (*c >> 6) & 0x3;
11877 xmc[16] = (*c >> 3) & 0x7;
11878 xmc[17] = *c++ & 0x7;
11879 xmc[18] = (*c >> 5) & 0x7;
11880 xmc[19] = (*c >> 2) & 0x7;
11881 xmc[20] = (*c++ & 0x3) << 1;
11882 xmc[20] |= (*c >> 7) & 0x1;
11883 xmc[21] = (*c >> 4) & 0x7;
11884 xmc[22] = (*c >> 1) & 0x7;
11885 xmc[23] = (*c++ & 0x1) << 2;
11886 xmc[23] |= (*c >> 6) & 0x3;
11887 xmc[24] = (*c >> 3) & 0x7;
11888 xmc[25] = *c++ & 0x7;
11889 Nc[2] = (*c >> 1) & 0x7F;
11890 bc[2] = (*c++ & 0x1) << 1; /* 20 */
11891 bc[2] |= (*c >> 7) & 0x1;
11892 Mc[2] = (*c >> 5) & 0x3;
11893 xmaxc[2] = (*c++ & 0x1F) << 1;
11894 xmaxc[2] |= (*c >> 7) & 0x1;
11895 xmc[26] = (*c >> 4) & 0x7;
11896 xmc[27] = (*c >> 1) & 0x7;
11897 xmc[28] = (*c++ & 0x1) << 2;
11898 xmc[28] |= (*c >> 6) & 0x3;
11899 xmc[29] = (*c >> 3) & 0x7;
11900 xmc[30] = *c++ & 0x7;
11901 xmc[31] = (*c >> 5) & 0x7;
11902 xmc[32] = (*c >> 2) & 0x7;
11903 xmc[33] = (*c++ & 0x3) << 1;
11904 xmc[33] |= (*c >> 7) & 0x1;
11905 xmc[34] = (*c >> 4) & 0x7;
11906 xmc[35] = (*c >> 1) & 0x7;
11907 xmc[36] = (*c++ & 0x1) << 2;
11908 xmc[36] |= (*c >> 6) & 0x3;
11909 xmc[37] = (*c >> 3) & 0x7;
11910 xmc[38] = *c++ & 0x7;
11911 Nc[3] = (*c >> 1) & 0x7F;
11912 bc[3] = (*c++ & 0x1) << 1;
11913 bc[3] |= (*c >> 7) & 0x1;
11914 Mc[3] = (*c >> 5) & 0x3;
11915 xmaxc[3] = (*c++ & 0x1F) << 1;
11916 xmaxc[3] |= (*c >> 7) & 0x1;
11917 xmc[39] = (*c >> 4) & 0x7;
11918 xmc[40] = (*c >> 1) & 0x7;
11919 xmc[41] = (*c++ & 0x1) << 2;
11920 xmc[41] |= (*c >> 6) & 0x3;
11921 xmc[42] = (*c >> 3) & 0x7;
11922 xmc[43] = *c++ & 0x7; /* 30 */
11923 xmc[44] = (*c >> 5) & 0x7;
11924 xmc[45] = (*c >> 2) & 0x7;
11925 xmc[46] = (*c++ & 0x3) << 1;
11926 xmc[46] |= (*c >> 7) & 0x1;
11927 xmc[47] = (*c >> 4) & 0x7;
11928 xmc[48] = (*c >> 1) & 0x7;
11929 xmc[49] = (*c++ & 0x1) << 2;
11930 xmc[49] |= (*c >> 6) & 0x3;
11931 xmc[50] = (*c >> 3) & 0x7;
11932 xmc[51] = *c & 0x7; /* 33 */
11935 Gsm_Decoder(s, LARc, Nc, bc, Mc, xmaxc, xmc, target);
11937 return 0;
11940 ** Do not edit or modify anything in this comment block.
11941 ** The arch-tag line is a file identity tag for the GNU Arch
11942 ** revision control system.
11944 ** arch-tag: 6a9b6628-821c-4a96-84c1-485ebd35f170
11948 * Copyright 1992 by Jutta Degener and Carsten Bormann, Technische
11949 * Universitaet Berlin. See the accompanying file "COPYRIGHT" for
11950 * details. THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE.
11954 #ifdef HAS_STDLIB_H
11955 # include <stdlib.h>
11956 #else
11957 # ifdef HAS_MALLOC_H
11958 # include <malloc.h>
11959 # else
11960 extern void free();
11961 # endif
11962 #endif
11964 void gsm_destroy (gsm S)
11966 if (S) free((char *)S);
11969 ** Do not edit or modify anything in this comment block.
11970 ** The arch-tag line is a file identity tag for the GNU Arch
11971 ** revision control system.
11973 ** arch-tag: f423d09b-6ccc-47e0-9b18-ee1cf7a8e473
11977 * Copyright 1992 by Jutta Degener and Carsten Bormann, Technische
11978 * Universitaet Berlin. See the accompanying file "COPYRIGHT" for
11979 * details. THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE.
11983 void gsm_encode (gsm s, gsm_signal * source, gsm_byte * c)
11985 word LARc[8], Nc[4], Mc[4], bc[4], xmaxc[4], xmc[13*4];
11987 Gsm_Coder(s, source, LARc, Nc, bc, Mc, xmaxc, xmc);
11990 /* variable size
11992 GSM_MAGIC 4
11994 LARc[0] 6
11995 LARc[1] 6
11996 LARc[2] 5
11997 LARc[3] 5
11998 LARc[4] 4
11999 LARc[5] 4
12000 LARc[6] 3
12001 LARc[7] 3
12003 Nc[0] 7
12004 bc[0] 2
12005 Mc[0] 2
12006 xmaxc[0] 6
12007 xmc[0] 3
12008 xmc[1] 3
12009 xmc[2] 3
12010 xmc[3] 3
12011 xmc[4] 3
12012 xmc[5] 3
12013 xmc[6] 3
12014 xmc[7] 3
12015 xmc[8] 3
12016 xmc[9] 3
12017 xmc[10] 3
12018 xmc[11] 3
12019 xmc[12] 3
12021 Nc[1] 7
12022 bc[1] 2
12023 Mc[1] 2
12024 xmaxc[1] 6
12025 xmc[13] 3
12026 xmc[14] 3
12027 xmc[15] 3
12028 xmc[16] 3
12029 xmc[17] 3
12030 xmc[18] 3
12031 xmc[19] 3
12032 xmc[20] 3
12033 xmc[21] 3
12034 xmc[22] 3
12035 xmc[23] 3
12036 xmc[24] 3
12037 xmc[25] 3
12039 Nc[2] 7
12040 bc[2] 2
12041 Mc[2] 2
12042 xmaxc[2] 6
12043 xmc[26] 3
12044 xmc[27] 3
12045 xmc[28] 3
12046 xmc[29] 3
12047 xmc[30] 3
12048 xmc[31] 3
12049 xmc[32] 3
12050 xmc[33] 3
12051 xmc[34] 3
12052 xmc[35] 3
12053 xmc[36] 3
12054 xmc[37] 3
12055 xmc[38] 3
12057 Nc[3] 7
12058 bc[3] 2
12059 Mc[3] 2
12060 xmaxc[3] 6
12061 xmc[39] 3
12062 xmc[40] 3
12063 xmc[41] 3
12064 xmc[42] 3
12065 xmc[43] 3
12066 xmc[44] 3
12067 xmc[45] 3
12068 xmc[46] 3
12069 xmc[47] 3
12070 xmc[48] 3
12071 xmc[49] 3
12072 xmc[50] 3
12073 xmc[51] 3
12076 #ifdef WAV49
12078 if (s->wav_fmt) {
12079 s->frame_index = !s->frame_index;
12080 if (s->frame_index) {
12082 uword sr;
12084 sr = 0;
12085 sr = sr >> 6 | LARc[0] << 10;
12086 sr = sr >> 6 | LARc[1] << 10;
12087 *c++ = sr >> 4;
12088 sr = sr >> 5 | LARc[2] << 11;
12089 *c++ = sr >> 7;
12090 sr = sr >> 5 | LARc[3] << 11;
12091 sr = sr >> 4 | LARc[4] << 12;
12092 *c++ = sr >> 6;
12093 sr = sr >> 4 | LARc[5] << 12;
12094 sr = sr >> 3 | LARc[6] << 13;
12095 *c++ = sr >> 7;
12096 sr = sr >> 3 | LARc[7] << 13;
12097 sr = sr >> 7 | Nc[0] << 9;
12098 *c++ = sr >> 5;
12099 sr = sr >> 2 | bc[0] << 14;
12100 sr = sr >> 2 | Mc[0] << 14;
12101 sr = sr >> 6 | xmaxc[0] << 10;
12102 *c++ = sr >> 3;
12103 sr = sr >> 3 | xmc[0] << 13;
12104 *c++ = sr >> 8;
12105 sr = sr >> 3 | xmc[1] << 13;
12106 sr = sr >> 3 | xmc[2] << 13;
12107 sr = sr >> 3 | xmc[3] << 13;
12108 *c++ = sr >> 7;
12109 sr = sr >> 3 | xmc[4] << 13;
12110 sr = sr >> 3 | xmc[5] << 13;
12111 sr = sr >> 3 | xmc[6] << 13;
12112 *c++ = sr >> 6;
12113 sr = sr >> 3 | xmc[7] << 13;
12114 sr = sr >> 3 | xmc[8] << 13;
12115 *c++ = sr >> 8;
12116 sr = sr >> 3 | xmc[9] << 13;
12117 sr = sr >> 3 | xmc[10] << 13;
12118 sr = sr >> 3 | xmc[11] << 13;
12119 *c++ = sr >> 7;
12120 sr = sr >> 3 | xmc[12] << 13;
12121 sr = sr >> 7 | Nc[1] << 9;
12122 *c++ = sr >> 5;
12123 sr = sr >> 2 | bc[1] << 14;
12124 sr = sr >> 2 | Mc[1] << 14;
12125 sr = sr >> 6 | xmaxc[1] << 10;
12126 *c++ = sr >> 3;
12127 sr = sr >> 3 | xmc[13] << 13;
12128 *c++ = sr >> 8;
12129 sr = sr >> 3 | xmc[14] << 13;
12130 sr = sr >> 3 | xmc[15] << 13;
12131 sr = sr >> 3 | xmc[16] << 13;
12132 *c++ = sr >> 7;
12133 sr = sr >> 3 | xmc[17] << 13;
12134 sr = sr >> 3 | xmc[18] << 13;
12135 sr = sr >> 3 | xmc[19] << 13;
12136 *c++ = sr >> 6;
12137 sr = sr >> 3 | xmc[20] << 13;
12138 sr = sr >> 3 | xmc[21] << 13;
12139 *c++ = sr >> 8;
12140 sr = sr >> 3 | xmc[22] << 13;
12141 sr = sr >> 3 | xmc[23] << 13;
12142 sr = sr >> 3 | xmc[24] << 13;
12143 *c++ = sr >> 7;
12144 sr = sr >> 3 | xmc[25] << 13;
12145 sr = sr >> 7 | Nc[2] << 9;
12146 *c++ = sr >> 5;
12147 sr = sr >> 2 | bc[2] << 14;
12148 sr = sr >> 2 | Mc[2] << 14;
12149 sr = sr >> 6 | xmaxc[2] << 10;
12150 *c++ = sr >> 3;
12151 sr = sr >> 3 | xmc[26] << 13;
12152 *c++ = sr >> 8;
12153 sr = sr >> 3 | xmc[27] << 13;
12154 sr = sr >> 3 | xmc[28] << 13;
12155 sr = sr >> 3 | xmc[29] << 13;
12156 *c++ = sr >> 7;
12157 sr = sr >> 3 | xmc[30] << 13;
12158 sr = sr >> 3 | xmc[31] << 13;
12159 sr = sr >> 3 | xmc[32] << 13;
12160 *c++ = sr >> 6;
12161 sr = sr >> 3 | xmc[33] << 13;
12162 sr = sr >> 3 | xmc[34] << 13;
12163 *c++ = sr >> 8;
12164 sr = sr >> 3 | xmc[35] << 13;
12165 sr = sr >> 3 | xmc[36] << 13;
12166 sr = sr >> 3 | xmc[37] << 13;
12167 *c++ = sr >> 7;
12168 sr = sr >> 3 | xmc[38] << 13;
12169 sr = sr >> 7 | Nc[3] << 9;
12170 *c++ = sr >> 5;
12171 sr = sr >> 2 | bc[3] << 14;
12172 sr = sr >> 2 | Mc[3] << 14;
12173 sr = sr >> 6 | xmaxc[3] << 10;
12174 *c++ = sr >> 3;
12175 sr = sr >> 3 | xmc[39] << 13;
12176 *c++ = sr >> 8;
12177 sr = sr >> 3 | xmc[40] << 13;
12178 sr = sr >> 3 | xmc[41] << 13;
12179 sr = sr >> 3 | xmc[42] << 13;
12180 *c++ = sr >> 7;
12181 sr = sr >> 3 | xmc[43] << 13;
12182 sr = sr >> 3 | xmc[44] << 13;
12183 sr = sr >> 3 | xmc[45] << 13;
12184 *c++ = sr >> 6;
12185 sr = sr >> 3 | xmc[46] << 13;
12186 sr = sr >> 3 | xmc[47] << 13;
12187 *c++ = sr >> 8;
12188 sr = sr >> 3 | xmc[48] << 13;
12189 sr = sr >> 3 | xmc[49] << 13;
12190 sr = sr >> 3 | xmc[50] << 13;
12191 *c++ = sr >> 7;
12192 sr = sr >> 3 | xmc[51] << 13;
12193 sr = sr >> 4;
12194 *c = sr >> 8;
12195 s->frame_chain = *c;
12197 else {
12198 uword sr;
12200 sr = 0;
12201 sr = sr >> 4 | s->frame_chain << 12;
12202 sr = sr >> 6 | LARc[0] << 10;
12203 *c++ = sr >> 6;
12204 sr = sr >> 6 | LARc[1] << 10;
12205 *c++ = sr >> 8;
12206 sr = sr >> 5 | LARc[2] << 11;
12207 sr = sr >> 5 | LARc[3] << 11;
12208 *c++ = sr >> 6;
12209 sr = sr >> 4 | LARc[4] << 12;
12210 sr = sr >> 4 | LARc[5] << 12;
12211 *c++ = sr >> 6;
12212 sr = sr >> 3 | LARc[6] << 13;
12213 sr = sr >> 3 | LARc[7] << 13;
12214 *c++ = sr >> 8;
12215 sr = sr >> 7 | Nc[0] << 9;
12216 sr = sr >> 2 | bc[0] << 14;
12217 *c++ = sr >> 7;
12218 sr = sr >> 2 | Mc[0] << 14;
12219 sr = sr >> 6 | xmaxc[0] << 10;
12220 *c++ = sr >> 7;
12221 sr = sr >> 3 | xmc[0] << 13;
12222 sr = sr >> 3 | xmc[1] << 13;
12223 sr = sr >> 3 | xmc[2] << 13;
12224 *c++ = sr >> 6;
12225 sr = sr >> 3 | xmc[3] << 13;
12226 sr = sr >> 3 | xmc[4] << 13;
12227 *c++ = sr >> 8;
12228 sr = sr >> 3 | xmc[5] << 13;
12229 sr = sr >> 3 | xmc[6] << 13;
12230 sr = sr >> 3 | xmc[7] << 13;
12231 *c++ = sr >> 7;
12232 sr = sr >> 3 | xmc[8] << 13;
12233 sr = sr >> 3 | xmc[9] << 13;
12234 sr = sr >> 3 | xmc[10] << 13;
12235 *c++ = sr >> 6;
12236 sr = sr >> 3 | xmc[11] << 13;
12237 sr = sr >> 3 | xmc[12] << 13;
12238 *c++ = sr >> 8;
12239 sr = sr >> 7 | Nc[1] << 9;
12240 sr = sr >> 2 | bc[1] << 14;
12241 *c++ = sr >> 7;
12242 sr = sr >> 2 | Mc[1] << 14;
12243 sr = sr >> 6 | xmaxc[1] << 10;
12244 *c++ = sr >> 7;
12245 sr = sr >> 3 | xmc[13] << 13;
12246 sr = sr >> 3 | xmc[14] << 13;
12247 sr = sr >> 3 | xmc[15] << 13;
12248 *c++ = sr >> 6;
12249 sr = sr >> 3 | xmc[16] << 13;
12250 sr = sr >> 3 | xmc[17] << 13;
12251 *c++ = sr >> 8;
12252 sr = sr >> 3 | xmc[18] << 13;
12253 sr = sr >> 3 | xmc[19] << 13;
12254 sr = sr >> 3 | xmc[20] << 13;
12255 *c++ = sr >> 7;
12256 sr = sr >> 3 | xmc[21] << 13;
12257 sr = sr >> 3 | xmc[22] << 13;
12258 sr = sr >> 3 | xmc[23] << 13;
12259 *c++ = sr >> 6;
12260 sr = sr >> 3 | xmc[24] << 13;
12261 sr = sr >> 3 | xmc[25] << 13;
12262 *c++ = sr >> 8;
12263 sr = sr >> 7 | Nc[2] << 9;
12264 sr = sr >> 2 | bc[2] << 14;
12265 *c++ = sr >> 7;
12266 sr = sr >> 2 | Mc[2] << 14;
12267 sr = sr >> 6 | xmaxc[2] << 10;
12268 *c++ = sr >> 7;
12269 sr = sr >> 3 | xmc[26] << 13;
12270 sr = sr >> 3 | xmc[27] << 13;
12271 sr = sr >> 3 | xmc[28] << 13;
12272 *c++ = sr >> 6;
12273 sr = sr >> 3 | xmc[29] << 13;
12274 sr = sr >> 3 | xmc[30] << 13;
12275 *c++ = sr >> 8;
12276 sr = sr >> 3 | xmc[31] << 13;
12277 sr = sr >> 3 | xmc[32] << 13;
12278 sr = sr >> 3 | xmc[33] << 13;
12279 *c++ = sr >> 7;
12280 sr = sr >> 3 | xmc[34] << 13;
12281 sr = sr >> 3 | xmc[35] << 13;
12282 sr = sr >> 3 | xmc[36] << 13;
12283 *c++ = sr >> 6;
12284 sr = sr >> 3 | xmc[37] << 13;
12285 sr = sr >> 3 | xmc[38] << 13;
12286 *c++ = sr >> 8;
12287 sr = sr >> 7 | Nc[3] << 9;
12288 sr = sr >> 2 | bc[3] << 14;
12289 *c++ = sr >> 7;
12290 sr = sr >> 2 | Mc[3] << 14;
12291 sr = sr >> 6 | xmaxc[3] << 10;
12292 *c++ = sr >> 7;
12293 sr = sr >> 3 | xmc[39] << 13;
12294 sr = sr >> 3 | xmc[40] << 13;
12295 sr = sr >> 3 | xmc[41] << 13;
12296 *c++ = sr >> 6;
12297 sr = sr >> 3 | xmc[42] << 13;
12298 sr = sr >> 3 | xmc[43] << 13;
12299 *c++ = sr >> 8;
12300 sr = sr >> 3 | xmc[44] << 13;
12301 sr = sr >> 3 | xmc[45] << 13;
12302 sr = sr >> 3 | xmc[46] << 13;
12303 *c++ = sr >> 7;
12304 sr = sr >> 3 | xmc[47] << 13;
12305 sr = sr >> 3 | xmc[48] << 13;
12306 sr = sr >> 3 | xmc[49] << 13;
12307 *c++ = sr >> 6;
12308 sr = sr >> 3 | xmc[50] << 13;
12309 sr = sr >> 3 | xmc[51] << 13;
12310 *c++ = sr >> 8;
12314 else
12316 #endif /* WAV49 */
12319 *c++ = ((GSM_MAGIC & 0xF) << 4) /* 1 */
12320 | ((LARc[0] >> 2) & 0xF);
12321 *c++ = ((LARc[0] & 0x3) << 6)
12322 | (LARc[1] & 0x3F);
12323 *c++ = ((LARc[2] & 0x1F) << 3)
12324 | ((LARc[3] >> 2) & 0x7);
12325 *c++ = ((LARc[3] & 0x3) << 6)
12326 | ((LARc[4] & 0xF) << 2)
12327 | ((LARc[5] >> 2) & 0x3);
12328 *c++ = ((LARc[5] & 0x3) << 6)
12329 | ((LARc[6] & 0x7) << 3)
12330 | (LARc[7] & 0x7);
12331 *c++ = ((Nc[0] & 0x7F) << 1)
12332 | ((bc[0] >> 1) & 0x1);
12333 *c++ = ((bc[0] & 0x1) << 7)
12334 | ((Mc[0] & 0x3) << 5)
12335 | ((xmaxc[0] >> 1) & 0x1F);
12336 *c++ = ((xmaxc[0] & 0x1) << 7)
12337 | ((xmc[0] & 0x7) << 4)
12338 | ((xmc[1] & 0x7) << 1)
12339 | ((xmc[2] >> 2) & 0x1);
12340 *c++ = ((xmc[2] & 0x3) << 6)
12341 | ((xmc[3] & 0x7) << 3)
12342 | (xmc[4] & 0x7);
12343 *c++ = ((xmc[5] & 0x7) << 5) /* 10 */
12344 | ((xmc[6] & 0x7) << 2)
12345 | ((xmc[7] >> 1) & 0x3);
12346 *c++ = ((xmc[7] & 0x1) << 7)
12347 | ((xmc[8] & 0x7) << 4)
12348 | ((xmc[9] & 0x7) << 1)
12349 | ((xmc[10] >> 2) & 0x1);
12350 *c++ = ((xmc[10] & 0x3) << 6)
12351 | ((xmc[11] & 0x7) << 3)
12352 | (xmc[12] & 0x7);
12353 *c++ = ((Nc[1] & 0x7F) << 1)
12354 | ((bc[1] >> 1) & 0x1);
12355 *c++ = ((bc[1] & 0x1) << 7)
12356 | ((Mc[1] & 0x3) << 5)
12357 | ((xmaxc[1] >> 1) & 0x1F);
12358 *c++ = ((xmaxc[1] & 0x1) << 7)
12359 | ((xmc[13] & 0x7) << 4)
12360 | ((xmc[14] & 0x7) << 1)
12361 | ((xmc[15] >> 2) & 0x1);
12362 *c++ = ((xmc[15] & 0x3) << 6)
12363 | ((xmc[16] & 0x7) << 3)
12364 | (xmc[17] & 0x7);
12365 *c++ = ((xmc[18] & 0x7) << 5)
12366 | ((xmc[19] & 0x7) << 2)
12367 | ((xmc[20] >> 1) & 0x3);
12368 *c++ = ((xmc[20] & 0x1) << 7)
12369 | ((xmc[21] & 0x7) << 4)
12370 | ((xmc[22] & 0x7) << 1)
12371 | ((xmc[23] >> 2) & 0x1);
12372 *c++ = ((xmc[23] & 0x3) << 6)
12373 | ((xmc[24] & 0x7) << 3)
12374 | (xmc[25] & 0x7);
12375 *c++ = ((Nc[2] & 0x7F) << 1) /* 20 */
12376 | ((bc[2] >> 1) & 0x1);
12377 *c++ = ((bc[2] & 0x1) << 7)
12378 | ((Mc[2] & 0x3) << 5)
12379 | ((xmaxc[2] >> 1) & 0x1F);
12380 *c++ = ((xmaxc[2] & 0x1) << 7)
12381 | ((xmc[26] & 0x7) << 4)
12382 | ((xmc[27] & 0x7) << 1)
12383 | ((xmc[28] >> 2) & 0x1);
12384 *c++ = ((xmc[28] & 0x3) << 6)
12385 | ((xmc[29] & 0x7) << 3)
12386 | (xmc[30] & 0x7);
12387 *c++ = ((xmc[31] & 0x7) << 5)
12388 | ((xmc[32] & 0x7) << 2)
12389 | ((xmc[33] >> 1) & 0x3);
12390 *c++ = ((xmc[33] & 0x1) << 7)
12391 | ((xmc[34] & 0x7) << 4)
12392 | ((xmc[35] & 0x7) << 1)
12393 | ((xmc[36] >> 2) & 0x1);
12394 *c++ = ((xmc[36] & 0x3) << 6)
12395 | ((xmc[37] & 0x7) << 3)
12396 | (xmc[38] & 0x7);
12397 *c++ = ((Nc[3] & 0x7F) << 1)
12398 | ((bc[3] >> 1) & 0x1);
12399 *c++ = ((bc[3] & 0x1) << 7)
12400 | ((Mc[3] & 0x3) << 5)
12401 | ((xmaxc[3] >> 1) & 0x1F);
12402 *c++ = ((xmaxc[3] & 0x1) << 7)
12403 | ((xmc[39] & 0x7) << 4)
12404 | ((xmc[40] & 0x7) << 1)
12405 | ((xmc[41] >> 2) & 0x1);
12406 *c++ = ((xmc[41] & 0x3) << 6) /* 30 */
12407 | ((xmc[42] & 0x7) << 3)
12408 | (xmc[43] & 0x7);
12409 *c++ = ((xmc[44] & 0x7) << 5)
12410 | ((xmc[45] & 0x7) << 2)
12411 | ((xmc[46] >> 1) & 0x3);
12412 *c++ = ((xmc[46] & 0x1) << 7)
12413 | ((xmc[47] & 0x7) << 4)
12414 | ((xmc[48] & 0x7) << 1)
12415 | ((xmc[49] >> 2) & 0x1);
12416 *c++ = ((xmc[49] & 0x3) << 6)
12417 | ((xmc[50] & 0x7) << 3)
12418 | (xmc[51] & 0x7);
12423 ** Do not edit or modify anything in this comment block.
12424 ** The arch-tag line is a file identity tag for the GNU Arch
12425 ** revision control system.
12427 ** arch-tag: cfe9c43d-d97c-4216-b5e5-ccd6a25b582b
12431 * Copyright 1992 by Jutta Degener and Carsten Bormann, Technische
12432 * Universitaet Berlin. See the accompanying file "COPYRIGHT" for
12433 * details. THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE.
12438 int gsm_option (gsm r, int opt, int * val)
12440 int result = -1;
12442 switch (opt) {
12443 case GSM_OPT_LTP_CUT:
12444 #ifdef LTP_CUT
12445 result = r->ltp_cut;
12446 if (val) r->ltp_cut = *val;
12447 #endif
12448 break;
12450 case GSM_OPT_VERBOSE:
12451 #ifndef NDEBUG
12452 result = r->verbose;
12453 if (val) r->verbose = *val;
12454 #endif
12455 break;
12457 case GSM_OPT_FAST:
12459 #if defined(FAST) && defined(USE_FLOAT_MUL)
12460 result = r->fast;
12461 if (val) r->fast = !!*val;
12462 #endif
12463 break;
12465 case GSM_OPT_FRAME_CHAIN:
12467 #ifdef WAV49
12468 result = r->frame_chain;
12469 if (val) r->frame_chain = *val;
12470 #endif
12471 break;
12473 case GSM_OPT_FRAME_INDEX:
12475 #ifdef WAV49
12476 result = r->frame_index;
12477 if (val) r->frame_index = *val;
12478 #endif
12479 break;
12481 case GSM_OPT_WAV49:
12483 #ifdef WAV49
12484 result = r->wav_fmt;
12485 if (val) r->wav_fmt = !!*val;
12486 #endif
12487 break;
12489 default:
12490 break;
12492 return result;
12495 ** Do not edit or modify anything in this comment block.
12496 ** The arch-tag line is a file identity tag for the GNU Arch
12497 ** revision control system.
12499 ** arch-tag: 963ff156-506f-4359-9145-371e9060b030
12503 ** Copyright (C) 2002-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
12505 ** This program is free software; you can redistribute it and/or modify
12506 ** it under the terms of the GNU Lesser General Public License as published by
12507 ** the Free Software Foundation; either version 2.1 of the License, or
12508 ** (at your option) any later version.
12510 ** This program is distributed in the hope that it will be useful,
12511 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12512 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12513 ** GNU Lesser General Public License for more details.
12515 ** You should have received a copy of the GNU Lesser General Public License
12516 ** along with this program; if not, write to the Free Software
12517 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
12520 #include <stdio.h>
12521 #include <fcntl.h>
12522 #include <string.h>
12523 #include <ctype.h>
12526 /*------------------------------------------------------------------------------
12527 ** Macros to handle big/little endian issues.
12530 #define SFE_HTK_BAD_FILE_LEN 1666
12531 #define SFE_HTK_NOT_WAVEFORM 1667
12533 /*------------------------------------------------------------------------------
12534 ** Private static functions.
12537 static int htk_close (SF_PRIVATE *psf) ;
12539 static int htk_write_header (SF_PRIVATE *psf, int calc_length) ;
12540 static int htk_read_header (SF_PRIVATE *psf) ;
12542 /*------------------------------------------------------------------------------
12543 ** Public function.
12547 htk_open (SF_PRIVATE *psf)
12548 { int subformat ;
12549 int error = 0 ;
12551 if (psf->is_pipe)
12552 return SFE_HTK_NO_PIPE ;
12554 if (psf->mode == SFM_READ || (psf->mode == SFM_RDWR && psf->filelength > 0))
12555 { if ((error = htk_read_header (psf)))
12556 return error ;
12559 subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
12561 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
12562 { if ((psf->sf.format & SF_FORMAT_TYPEMASK) != SF_FORMAT_HTK)
12563 return SFE_BAD_OPEN_FORMAT ;
12565 psf->endian = SF_ENDIAN_BIG ;
12567 if (htk_write_header (psf, SF_FALSE))
12568 return psf->error ;
12570 psf->write_header = htk_write_header ;
12573 psf->close = htk_close ;
12575 psf->blockwidth = psf->bytewidth * psf->sf.channels ;
12577 switch (subformat)
12578 { case SF_FORMAT_PCM_16 : /* 16-bit linear PCM. */
12579 error = pcm_init (psf) ;
12580 break ;
12582 default : break ;
12585 return error ;
12586 } /* htk_open */
12588 /*------------------------------------------------------------------------------
12591 static int
12592 htk_close (SF_PRIVATE *psf)
12594 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
12595 htk_write_header (psf, SF_TRUE) ;
12597 return 0 ;
12598 } /* htk_close */
12600 static int
12601 htk_write_header (SF_PRIVATE *psf, int calc_length)
12602 { sf_count_t current ;
12603 int sample_count, sample_period ;
12605 current = psf_ftell (psf) ;
12607 if (calc_length)
12608 psf->filelength = psf_get_filelen (psf) ;
12610 /* Reset the current header length to zero. */
12611 psf->header [0] = 0 ;
12612 psf->headindex = 0 ;
12613 psf_fseek (psf, 0, SEEK_SET) ;
12615 if (psf->filelength > 12)
12616 sample_count = (psf->filelength - 12) / 2 ;
12617 else
12618 sample_count = 0 ;
12620 sample_period = 10000000 / psf->sf.samplerate ;
12622 psf_binheader_writef (psf, "E444", sample_count, sample_period, 0x20000) ;
12624 /* Header construction complete so write it out. */
12625 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
12627 if (psf->error)
12628 return psf->error ;
12630 psf->dataoffset = psf->headindex ;
12632 if (current > 0)
12633 psf_fseek (psf, current, SEEK_SET) ;
12635 return psf->error ;
12636 } /* htk_write_header */
12639 ** Found the following info in a comment block within Bill Schottstaedt's
12640 ** sndlib library.
12642 ** HTK format files consist of a contiguous sequence of samples preceded by a
12643 ** header. Each sample is a vector of either 2-byte integers or 4-byte floats.
12644 ** 2-byte integers are used for compressed forms as described below and for
12645 ** vector quantised data as described later in section 5.11. HTK format data
12646 ** files can also be used to store speech waveforms as described in section 5.8.
12648 ** The HTK file format header is 12 bytes long and contains the following data
12649 ** nSamples -- number of samples in file (4-byte integer)
12650 ** sampPeriod -- sample period in 100ns units (4-byte integer)
12651 ** sampSize -- number of bytes per sample (2-byte integer)
12652 ** parmKind -- a code indicating the sample kind (2-byte integer)
12654 ** The parameter kind consists of a 6 bit code representing the basic
12655 ** parameter kind plus additional bits for each of the possible qualifiers.
12656 ** The basic parameter kind codes are
12658 ** 0 WAVEFORM sampled waveform
12659 ** 1 LPC linear prediction filter coefficients
12660 ** 2 LPREFC linear prediction reflection coefficients
12661 ** 3 LPCEPSTRA LPC cepstral coefficients
12662 ** 4 LPDELCEP LPC cepstra plus delta coefficients
12663 ** 5 IREFC LPC reflection coef in 16 bit integer format
12664 ** 6 MFCC mel-frequency cepstral coefficients
12665 ** 7 FBANK log mel-filter bank channel outputs
12666 ** 8 MELSPEC linear mel-filter bank channel outputs
12667 ** 9 USER user defined sample kind
12668 ** 10 DISCRETE vector quantised data
12670 ** and the bit-encoding for the qualifiers (in octal) is
12671 ** _E 000100 has energy
12672 ** _N 000200 absolute energy suppressed
12673 ** _D 000400 has delta coefficients
12674 ** _A 001000 has acceleration coefficients
12675 ** _C 002000 is compressed
12676 ** _Z 004000 has zero mean static coef.
12677 ** _K 010000 has CRC checksum
12678 ** _O 020000 has 0'th cepstral coef.
12681 static int
12682 htk_read_header (SF_PRIVATE *psf)
12683 { int sample_count, sample_period, marker ;
12685 psf_binheader_readf (psf, "pE444", 0, &sample_count, &sample_period, &marker) ;
12687 if (2 * sample_count + 12 != psf->filelength)
12688 return SFE_HTK_BAD_FILE_LEN ;
12690 if (marker != 0x20000)
12691 return SFE_HTK_NOT_WAVEFORM ;
12693 psf->sf.channels = 1 ;
12694 psf->sf.samplerate = 10000000 / sample_period ;
12696 psf_log_printf (psf, "HTK Waveform file\n Sample Count : %d\n Sample Period : %d => %d Hz\n",
12697 sample_count, sample_period, psf->sf.samplerate) ;
12699 psf->sf.format = SF_FORMAT_HTK | SF_FORMAT_PCM_16 ;
12700 psf->bytewidth = 2 ;
12702 /* HTK always has a 12 byte header. */
12703 psf->dataoffset = 12 ;
12704 psf->endian = SF_ENDIAN_BIG ;
12706 psf->datalength = psf->filelength - psf->dataoffset ;
12708 psf->close = htk_close ;
12710 psf->blockwidth = psf->sf.channels * psf->bytewidth ;
12712 if (! psf->sf.frames && psf->blockwidth)
12713 psf->sf.frames = (psf->filelength - psf->dataoffset) / psf->blockwidth ;
12715 return 0 ;
12716 } /* htk_read_header */
12718 ** Do not edit or modify anything in this comment block.
12719 ** The arch-tag line is a file identity tag for the GNU Arch
12720 ** revision control system.
12722 ** arch-tag: c350e972-082e-4c20-8934-03391a723560
12725 ** Copyright (C) 1999-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
12727 ** This program is free software; you can redistribute it and/or modify
12728 ** it under the terms of the GNU Lesser General Public License as published by
12729 ** the Free Software Foundation; either version 2.1 of the License, or
12730 ** (at your option) any later version.
12732 ** This program is distributed in the hope that it will be useful,
12733 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12734 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12735 ** GNU Lesser General Public License for more details.
12737 ** You should have received a copy of the GNU Lesser General Public License
12738 ** along with this program; if not, write to the Free Software
12739 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
12742 #include <stdio.h>
12743 #include <stdlib.h>
12744 #include <string.h>
12747 typedef struct IMA_ADPCM_PRIVATE_tag
12748 { int (*decode_block) (SF_PRIVATE *psf, struct IMA_ADPCM_PRIVATE_tag *pima) ;
12749 int (*encode_block) (SF_PRIVATE *psf, struct IMA_ADPCM_PRIVATE_tag *pima) ;
12751 int channels, blocksize, samplesperblock, blocks ;
12752 int blockcount, samplecount ;
12753 int previous [2] ;
12754 int stepindx [2] ;
12755 unsigned char *block ;
12756 short *samples ;
12757 #if HAVE_FLEXIBLE_ARRAY
12758 unsigned short data [] ; /* ISO C99 struct flexible array. */
12759 #else
12760 unsigned short data [1] ; /* This is a hack and might not work. */
12761 #endif
12762 } IMA_ADPCM_PRIVATE ;
12764 /*============================================================================================
12765 ** Predefined IMA ADPCM data.
12768 static int ima_indx_adjust [16] =
12769 { -1, -1, -1, -1, /* +0 - +3, decrease the step size */
12770 2, 4, 6, 8, /* +4 - +7, increase the step size */
12771 -1, -1, -1, -1, /* -0 - -3, decrease the step size */
12772 2, 4, 6, 8, /* -4 - -7, increase the step size */
12775 static int ima_step_size [89] =
12776 { 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
12777 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, 130, 143, 157, 173, 190, 209, 230,
12778 253, 279, 307, 337, 371, 408, 449, 494, 544, 598, 658, 724, 796, 876, 963,
12779 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, 3327,
12780 3660, 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630, 9493, 10442,
12781 11487, 12635, 13899, 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794,
12782 32767
12785 static int ima_reader_init (SF_PRIVATE *psf, int blockalign, int samplesperblock) ;
12786 static int ima_writer_init (SF_PRIVATE *psf, int blockalign) ;
12788 static int ima_read_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima, short *ptr, int len) ;
12789 static int ima_write_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima, short *ptr, int len) ;
12791 static sf_count_t ima_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
12792 static sf_count_t ima_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
12793 static sf_count_t ima_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
12794 static sf_count_t ima_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
12796 static sf_count_t ima_write_s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
12797 static sf_count_t ima_write_i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
12798 static sf_count_t ima_write_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
12799 static sf_count_t ima_write_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
12801 static sf_count_t ima_seek (SF_PRIVATE *psf, int mode, sf_count_t offset) ;
12803 static int wav_w64_ima_close (SF_PRIVATE *psf) ;
12804 static int aiff_ima_close (SF_PRIVATE *psf) ;
12806 static int wav_w64_ima_decode_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima) ;
12807 static int wav_w64_ima_encode_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima) ;
12809 /*-static int aiff_ima_reader_init (SF_PRIVATE *psf, int blockalign, int samplesperblock) ;-*/
12810 static int aiff_ima_decode_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima) ;
12811 static int aiff_ima_encode_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima) ;
12814 /*============================================================================================
12815 ** IMA ADPCM Reader initialisation function.
12819 wav_w64_ima_init (SF_PRIVATE *psf, int blockalign, int samplesperblock)
12820 { int error ;
12822 if (psf->mode == SFM_RDWR)
12823 return SFE_BAD_MODE_RW ;
12825 if (psf->mode == SFM_READ)
12826 if ((error = ima_reader_init (psf, blockalign, samplesperblock)))
12827 return error ;
12829 if (psf->mode == SFM_WRITE)
12830 if ((error = ima_writer_init (psf, blockalign)))
12831 return error ;
12833 psf->seek = ima_seek ;
12834 psf->close = wav_w64_ima_close ;
12836 return 0 ;
12837 } /* wav_w64_ima_init */
12839 static int
12840 wav_w64_ima_close (SF_PRIVATE *psf)
12841 { IMA_ADPCM_PRIVATE *pima ;
12843 if (! psf->fdata)
12844 return 0 ;
12846 pima = (IMA_ADPCM_PRIVATE*) psf->fdata ;
12848 if (psf->mode == SFM_WRITE)
12849 { /* If a block has been partially assembled, write it out
12850 ** as the final block.
12852 if (pima->samplecount && pima->samplecount < pima->samplesperblock)
12853 pima->encode_block (psf, pima) ;
12855 psf->sf.frames = pima->samplesperblock * pima->blockcount / psf->sf.channels ;
12857 if (psf->write_header)
12858 psf->write_header (psf, SF_TRUE) ;
12861 free (psf->fdata) ;
12862 psf->fdata = NULL ;
12864 return 0 ;
12865 } /* wav_w64_ima_close */
12868 aiff_ima_init (SF_PRIVATE *psf, int blockalign, int samplesperblock)
12869 { int error ;
12871 if (psf->mode == SFM_RDWR)
12872 return SFE_BAD_MODE_RW ;
12874 if (psf->mode == SFM_READ)
12875 if ((error = ima_reader_init (psf, blockalign, samplesperblock)))
12876 return error ;
12878 if (psf->mode == SFM_WRITE)
12879 if ((error = ima_writer_init (psf, blockalign)))
12880 return error ;
12882 psf->seek = ima_seek ;
12883 psf->close = aiff_ima_close ;
12885 return 0 ;
12886 } /* aiff_ima_init */
12888 static int
12889 aiff_ima_close (SF_PRIVATE *psf)
12890 { IMA_ADPCM_PRIVATE *pima ;
12892 if (! psf->fdata)
12893 return 0 ;
12895 pima = (IMA_ADPCM_PRIVATE*) psf->fdata ;
12897 if (psf->mode == SFM_WRITE)
12898 { /* If a block has been partially assembled, write it out
12899 ** as the final block.
12902 if (pima->samplecount && pima->samplecount < pima->samplesperblock)
12903 pima->encode_block (psf, pima) ;
12905 if (psf->write_header)
12906 psf->write_header (psf, SF_TRUE) ;
12909 free (psf->fdata) ;
12910 psf->fdata = NULL ;
12912 return 0 ;
12913 } /* aiff_ima_close */
12915 /*============================================================================================
12916 ** IMA ADPCM Read Functions.
12919 static int
12920 ima_reader_init (SF_PRIVATE *psf, int blockalign, int samplesperblock)
12921 { IMA_ADPCM_PRIVATE *pima ;
12922 int pimasize, count ;
12924 if (psf->mode != SFM_READ)
12925 return SFE_BAD_MODE_RW ;
12927 pimasize = sizeof (IMA_ADPCM_PRIVATE) + blockalign * psf->sf.channels + 3 * psf->sf.channels * samplesperblock ;
12929 if (! (pima = malloc (pimasize)))
12930 return SFE_MALLOC_FAILED ;
12932 psf->fdata = (void*) pima ;
12934 memset (pima, 0, pimasize) ;
12936 pima->samples = (short *)pima->data ;
12937 pima->block = (unsigned char*) (pima->data + samplesperblock * psf->sf.channels) ;
12939 pima->channels = psf->sf.channels ;
12940 pima->blocksize = blockalign ;
12941 pima->samplesperblock = samplesperblock ;
12943 psf->filelength = psf_get_filelen (psf) ;
12944 psf->datalength = (psf->dataend) ? psf->dataend - psf->dataoffset :
12945 psf->filelength - psf->dataoffset ;
12947 if (psf->datalength % pima->blocksize)
12948 pima->blocks = psf->datalength / pima->blocksize + 1 ;
12949 else
12950 pima->blocks = psf->datalength / pima->blocksize ;
12952 switch (psf->sf.format & SF_FORMAT_TYPEMASK)
12953 { case SF_FORMAT_WAV :
12954 case SF_FORMAT_W64 :
12955 count = 2 * (pima->blocksize - 4 * pima->channels) / pima->channels + 1 ;
12957 if (pima->samplesperblock != count)
12958 psf_log_printf (psf, "*** Warning : samplesperblock should be %d.\n", count) ;
12960 pima->decode_block = wav_w64_ima_decode_block ;
12962 psf->sf.frames = pima->samplesperblock * pima->blocks ;
12963 break ;
12965 case SF_FORMAT_AIFF :
12966 psf_log_printf (psf, "still need to check block count\n") ;
12967 pima->decode_block = aiff_ima_decode_block ;
12968 psf->sf.frames = pima->samplesperblock * pima->blocks / pima->channels ;
12969 break ;
12971 default :
12972 psf_log_printf (psf, "ima_reader_init: bad psf->sf.format\n") ;
12973 return SFE_INTERNAL ;
12974 break ;
12977 pima->decode_block (psf, pima) ; /* Read first block. */
12979 psf->read_short = ima_read_s ;
12980 psf->read_int = ima_read_i ;
12981 psf->read_float = ima_read_f ;
12982 psf->read_double = ima_read_d ;
12984 return 0 ;
12985 } /* ima_reader_init */
12987 static int
12988 aiff_ima_decode_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima)
12989 { unsigned char *blockdata ;
12990 int chan, k, diff, bytecode ;
12991 short step, stepindx, predictor, *sampledata ;
12993 static int count = 0 ;
12994 count ++ ;
12996 pima->blockcount += pima->channels ;
12997 pima->samplecount = 0 ;
12999 if (pima->blockcount > pima->blocks)
13000 { memset (pima->samples, 0, pima->samplesperblock * pima->channels * sizeof (short)) ;
13001 return 1 ;
13004 if ((k = psf_fread (pima->block, 1, pima->blocksize * pima->channels, psf)) != pima->blocksize * pima->channels)
13005 psf_log_printf (psf, "*** Warning : short read (%d != %d).\n", k, pima->blocksize) ;
13007 /* Read and check the block header. */
13008 for (chan = 0 ; chan < pima->channels ; chan++)
13009 { blockdata = pima->block + chan * 34 ;
13010 sampledata = pima->samples + chan ;
13012 predictor = (blockdata [0] << 8) | (blockdata [1] & 0x80) ;
13013 stepindx = blockdata [1] & 0x7F ;
13016 if (count < 5)
13017 printf ("\nchan: %d predictor: %d stepindx: %d (%d)\n",
13018 chan, predictor, stepindx, ima_step_size [stepindx]) ;
13020 /* FIXME : Do this a better way. */
13021 if (stepindx < 0) stepindx = 0 ;
13022 else if (stepindx > 88) stepindx = 88 ;
13025 ** Pull apart the packed 4 bit samples and store them in their
13026 ** correct sample positions.
13028 for (k = 0 ; k < pima->blocksize - 2 ; k++)
13029 { bytecode = blockdata [k + 2] ;
13030 sampledata [pima->channels * (2 * k + 0)] = bytecode & 0xF ;
13031 sampledata [pima->channels * (2 * k + 1)] = (bytecode >> 4) & 0xF ;
13034 /* Decode the encoded 4 bit samples. */
13035 for (k = 0 ; k < pima->samplesperblock ; k ++)
13036 { step = ima_step_size [stepindx] ;
13038 bytecode = pima->samples [pima->channels * k + chan] ;
13040 stepindx += ima_indx_adjust [bytecode] ;
13042 if (stepindx < 0) stepindx = 0 ;
13043 else if (stepindx > 88) stepindx = 88 ;
13045 diff = step >> 3 ;
13046 if (bytecode & 1) diff += step >> 2 ;
13047 if (bytecode & 2) diff += step >> 1 ;
13048 if (bytecode & 4) diff += step ;
13049 if (bytecode & 8) diff = -diff ;
13051 predictor += diff ;
13053 pima->samples [pima->channels * k + chan] = predictor ;
13057 if (count < 5)
13059 for (k = 0 ; k < 10 ; k++)
13060 printf ("% 7d,", pima->samples [k]) ;
13061 puts ("") ;
13064 return 1 ;
13065 } /* aiff_ima_decode_block */
13067 static int
13068 aiff_ima_encode_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima)
13069 { int chan, k, step, diff, vpdiff, blockindx, indx ;
13070 short bytecode, mask ;
13072 static int count = 0 ;
13073 if (0 && count == 0)
13074 { pima->samples [0] = 0 ;
13075 printf ("blocksize : %d\n", pima->blocksize) ;
13076 printf ("pima->stepindx [0] : %d\n", pima->stepindx [0]) ;
13078 count ++ ;
13080 /* Encode the block header. */
13081 for (chan = 0 ; chan < pima->channels ; chan ++)
13082 { blockindx = chan * pima->blocksize ;
13084 pima->block [blockindx] = (pima->samples [chan] >> 8) & 0xFF ;
13085 pima->block [blockindx + 1] = (pima->samples [chan] & 0x80) + (pima->stepindx [chan] & 0x7F) ;
13087 pima->previous [chan] = pima->samples [chan] ;
13090 /* Encode second and later samples for every block as a 4 bit value. */
13091 for (k = pima->channels ; k < (pima->samplesperblock * pima->channels) ; k ++)
13092 { chan = (pima->channels > 1) ? (k % 2) : 0 ;
13094 diff = pima->samples [k] - pima->previous [chan] ;
13096 bytecode = 0 ;
13097 step = ima_step_size [pima->stepindx [chan]] ;
13098 vpdiff = step >> 3 ;
13099 if (diff < 0)
13100 { bytecode = 8 ;
13101 diff = -diff ;
13103 mask = 4 ;
13104 while (mask)
13105 { if (diff >= step)
13106 { bytecode |= mask ;
13107 diff -= step ;
13108 vpdiff += step ;
13110 step >>= 1 ;
13111 mask >>= 1 ;
13114 if (bytecode & 8)
13115 pima->previous [chan] -= vpdiff ;
13116 else
13117 pima->previous [chan] += vpdiff ;
13119 if (pima->previous [chan] > 32767)
13120 pima->previous [chan] = 32767 ;
13121 else if (pima->previous [chan] < -32768)
13122 pima->previous [chan] = -32768 ;
13124 pima->stepindx [chan] += ima_indx_adjust [bytecode] ;
13125 if (pima->stepindx [chan] < 0)
13126 pima->stepindx [chan] = 0 ;
13127 else if (pima->stepindx [chan] > 88)
13128 pima->stepindx [chan] = 88 ;
13130 pima->samples [k] = bytecode ;
13133 /* Pack the 4 bit encoded samples. */
13135 for (chan = 0 ; chan < pima->channels ; chan ++)
13136 { for (indx = pima->channels ; indx < pima->channels * pima->samplesperblock ; indx += 2 * pima->channels)
13137 { blockindx = chan * pima->blocksize + 2 + indx / 2;
13139 if (0 && count ++ < 5)
13140 printf ("chan: %d blockindx: %3d indx: %3d\n", chan, blockindx, indx) ;
13142 pima->block [blockindx] = pima->samples [indx] & 0x0F ;
13143 pima->block [blockindx] |= (pima->samples [indx + pima->channels] << 4) & 0xF0 ;
13147 /* Write the block to disk. */
13149 if ((k = psf_fwrite (pima->block, 1, pima->channels * pima->blocksize, psf)) != pima->channels * pima->blocksize)
13150 psf_log_printf (psf, "*** Warning : short write (%d != %d).\n", k, pima->channels * pima->blocksize) ;
13152 memset (pima->samples, 0, pima->channels * pima->samplesperblock * sizeof (short)) ;
13153 pima->samplecount = 0 ;
13154 pima->blockcount ++ ;
13156 return 1 ;
13157 } /* aiff_ima_encode_block */
13159 static int
13160 wav_w64_ima_decode_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima)
13161 { int chan, k, current, blockindx, indx, indxstart, diff ;
13162 short step, bytecode, stepindx [2] ;
13164 pima->blockcount ++ ;
13165 pima->samplecount = 0 ;
13167 if (pima->blockcount > pima->blocks)
13168 { memset (pima->samples, 0, pima->samplesperblock * pima->channels * sizeof (short)) ;
13169 return 1 ;
13172 if ((k = psf_fread (pima->block, 1, pima->blocksize, psf)) != pima->blocksize)
13173 psf_log_printf (psf, "*** Warning : short read (%d != %d).\n", k, pima->blocksize) ;
13175 /* Read and check the block header. */
13177 for (chan = 0 ; chan < pima->channels ; chan++)
13178 { current = pima->block [chan*4] | (pima->block [chan*4+1] << 8) ;
13179 if (current & 0x8000)
13180 current -= 0x10000 ;
13182 stepindx [chan] = pima->block [chan*4+2] ;
13183 if (stepindx [chan] < 0)
13184 stepindx [chan] = 0 ;
13185 else if (stepindx [chan] > 88)
13186 stepindx [chan] = 88 ;
13188 if (pima->block [chan*4+3] != 0)
13189 psf_log_printf (psf, "IMA ADPCM synchronisation error.\n") ;
13191 pima->samples [chan] = current ;
13195 ** Pull apart the packed 4 bit samples and store them in their
13196 ** correct sample positions.
13199 blockindx = 4 * pima->channels ;
13201 indxstart = pima->channels ;
13202 while (blockindx < pima->blocksize)
13203 { for (chan = 0 ; chan < pima->channels ; chan++)
13204 { indx = indxstart + chan ;
13205 for (k = 0 ; k < 4 ; k++)
13206 { bytecode = pima->block [blockindx++] ;
13207 pima->samples [indx] = bytecode & 0x0F ;
13208 indx += pima->channels ;
13209 pima->samples [indx] = (bytecode >> 4) & 0x0F ;
13210 indx += pima->channels ;
13213 indxstart += 8 * pima->channels ;
13216 /* Decode the encoded 4 bit samples. */
13218 for (k = pima->channels ; k < (pima->samplesperblock * pima->channels) ; k ++)
13219 { chan = (pima->channels > 1) ? (k % 2) : 0 ;
13221 bytecode = pima->samples [k] & 0xF ;
13223 step = ima_step_size [stepindx [chan]] ;
13224 current = pima->samples [k - pima->channels] ;
13226 diff = step >> 3 ;
13227 if (bytecode & 1)
13228 diff += step >> 2 ;
13229 if (bytecode & 2)
13230 diff += step >> 1 ;
13231 if (bytecode & 4)
13232 diff += step ;
13233 if (bytecode & 8)
13234 diff = -diff ;
13236 current += diff ;
13238 if (current > 32767)
13239 current = 32767 ;
13240 else if (current < -32768)
13241 current = -32768 ;
13243 stepindx [chan] += ima_indx_adjust [bytecode] ;
13245 if (stepindx [chan] < 0)
13246 stepindx [chan] = 0 ;
13247 else if (stepindx [chan] > 88)
13248 stepindx [chan] = 88 ;
13250 pima->samples [k] = current ;
13253 return 1 ;
13254 } /* wav_w64_ima_decode_block */
13256 static int
13257 wav_w64_ima_encode_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima)
13258 { int chan, k, step, diff, vpdiff, blockindx, indx, indxstart ;
13259 short bytecode, mask ;
13261 /* Encode the block header. */
13262 for (chan = 0 ; chan < pima->channels ; chan++)
13263 { pima->block [chan*4] = pima->samples [chan] & 0xFF ;
13264 pima->block [chan*4+1] = (pima->samples [chan] >> 8) & 0xFF ;
13266 pima->block [chan*4+2] = pima->stepindx [chan] ;
13267 pima->block [chan*4+3] = 0 ;
13269 pima->previous [chan] = pima->samples [chan] ;
13272 /* Encode the samples as 4 bit. */
13274 for (k = pima->channels ; k < (pima->samplesperblock * pima->channels) ; k ++)
13275 { chan = (pima->channels > 1) ? (k % 2) : 0 ;
13277 diff = pima->samples [k] - pima->previous [chan] ;
13279 bytecode = 0 ;
13280 step = ima_step_size [pima->stepindx [chan]] ;
13281 vpdiff = step >> 3 ;
13282 if (diff < 0)
13283 { bytecode = 8 ;
13284 diff = -diff ;
13286 mask = 4 ;
13287 while (mask)
13288 { if (diff >= step)
13289 { bytecode |= mask ;
13290 diff -= step ;
13291 vpdiff += step ;
13293 step >>= 1 ;
13294 mask >>= 1 ;
13297 if (bytecode & 8)
13298 pima->previous [chan] -= vpdiff ;
13299 else
13300 pima->previous [chan] += vpdiff ;
13302 if (pima->previous [chan] > 32767)
13303 pima->previous [chan] = 32767 ;
13304 else if (pima->previous [chan] < -32768)
13305 pima->previous [chan] = -32768 ;
13307 pima->stepindx [chan] += ima_indx_adjust [bytecode] ;
13308 if (pima->stepindx [chan] < 0)
13309 pima->stepindx [chan] = 0 ;
13310 else if (pima->stepindx [chan] > 88)
13311 pima->stepindx [chan] = 88 ;
13313 pima->samples [k] = bytecode ;
13316 /* Pack the 4 bit encoded samples. */
13318 blockindx = 4 * pima->channels ;
13320 indxstart = pima->channels ;
13321 while (blockindx < pima->blocksize)
13322 { for (chan = 0 ; chan < pima->channels ; chan++)
13323 { indx = indxstart + chan ;
13324 for (k = 0 ; k < 4 ; k++)
13325 { pima->block [blockindx] = pima->samples [indx] & 0x0F ;
13326 indx += pima->channels ;
13327 pima->block [blockindx] |= (pima->samples [indx] << 4) & 0xF0 ;
13328 indx += pima->channels ;
13329 blockindx ++ ;
13332 indxstart += 8 * pima->channels ;
13335 /* Write the block to disk. */
13337 if ((k = psf_fwrite (pima->block, 1, pima->blocksize, psf)) != pima->blocksize)
13338 psf_log_printf (psf, "*** Warning : short write (%d != %d).\n", k, pima->blocksize) ;
13340 memset (pima->samples, 0, pima->samplesperblock * sizeof (short)) ;
13341 pima->samplecount = 0 ;
13342 pima->blockcount ++ ;
13344 return 1 ;
13345 } /* wav_w64_ima_encode_block */
13347 static int
13348 ima_read_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima, short *ptr, int len)
13349 { int count, total = 0, indx = 0 ;
13351 while (indx < len)
13352 { if (pima->blockcount >= pima->blocks && pima->samplecount >= pima->samplesperblock)
13353 { memset (&(ptr [indx]), 0, (size_t) ((len - indx) * sizeof (short))) ;
13354 return total ;
13357 if (pima->samplecount >= pima->samplesperblock)
13358 pima->decode_block (psf, pima) ;
13360 count = (pima->samplesperblock - pima->samplecount) * pima->channels ;
13361 count = (len - indx > count) ? count : len - indx ;
13363 memcpy (&(ptr [indx]), &(pima->samples [pima->samplecount * pima->channels]), count * sizeof (short)) ;
13364 indx += count ;
13365 pima->samplecount += count / pima->channels ;
13366 total = indx ;
13369 return total ;
13370 } /* ima_read_block */
13372 static sf_count_t
13373 ima_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
13374 { IMA_ADPCM_PRIVATE *pima ;
13375 int readcount, count ;
13376 sf_count_t total = 0 ;
13378 if (! psf->fdata)
13379 return 0 ;
13380 pima = (IMA_ADPCM_PRIVATE*) psf->fdata ;
13382 while (len > 0)
13383 { readcount = (len > 0x10000000) ? 0x10000000 : (int) len ;
13385 count = ima_read_block (psf, pima, ptr, readcount) ;
13387 total += count ;
13388 len -= count ;
13389 if (count != readcount)
13390 break ;
13393 return total ;
13394 } /* ima_read_s */
13396 static sf_count_t
13397 ima_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
13398 { IMA_ADPCM_PRIVATE *pima ;
13399 short *sptr ;
13400 int k, bufferlen, readcount, count ;
13401 sf_count_t total = 0 ;
13403 if (! psf->fdata)
13404 return 0 ;
13405 pima = (IMA_ADPCM_PRIVATE*) psf->fdata ;
13407 sptr = (short*) psf->buffer ;
13408 bufferlen = SF_BUFFER_LEN / sizeof (short) ;
13409 while (len > 0)
13410 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
13411 count = ima_read_block (psf, pima, sptr, readcount) ;
13412 for (k = 0 ; k < readcount ; k++)
13413 ptr [total + k] = ((int) sptr [k]) << 16 ;
13414 total += count ;
13415 len -= readcount ;
13416 if (count != readcount)
13417 break ;
13420 return total ;
13421 } /* ima_read_i */
13423 static sf_count_t
13424 ima_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
13425 { IMA_ADPCM_PRIVATE *pima ;
13426 short *sptr ;
13427 int k, bufferlen, readcount, count ;
13428 sf_count_t total = 0 ;
13429 float normfact ;
13431 if (! psf->fdata)
13432 return 0 ;
13433 pima = (IMA_ADPCM_PRIVATE*) psf->fdata ;
13435 normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x8000) : 1.0 ;
13437 sptr = (short*) psf->buffer ;
13438 bufferlen = SF_BUFFER_LEN / sizeof (short) ;
13439 while (len > 0)
13440 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
13441 count = ima_read_block (psf, pima, sptr, readcount) ;
13442 for (k = 0 ; k < readcount ; k++)
13443 ptr [total + k] = normfact * (float) (sptr [k]) ;
13444 total += count ;
13445 len -= readcount ;
13446 if (count != readcount)
13447 break ;
13450 return total ;
13451 } /* ima_read_f */
13453 static sf_count_t
13454 ima_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
13455 { IMA_ADPCM_PRIVATE *pima ;
13456 short *sptr ;
13457 int k, bufferlen, readcount, count ;
13458 sf_count_t total = 0 ;
13459 double normfact ;
13461 if (! psf->fdata)
13462 return 0 ;
13463 pima = (IMA_ADPCM_PRIVATE*) psf->fdata ;
13465 normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x8000) : 1.0 ;
13467 sptr = (short*) psf->buffer ;
13468 bufferlen = SF_BUFFER_LEN / sizeof (short) ;
13469 while (len > 0)
13470 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
13471 count = ima_read_block (psf, pima, sptr, readcount) ;
13472 for (k = 0 ; k < readcount ; k++)
13473 ptr [total + k] = normfact * (double) (sptr [k]) ;
13474 total += count ;
13475 len -= readcount ;
13476 if (count != readcount)
13477 break ;
13480 return total ;
13481 } /* ima_read_d */
13483 static sf_count_t
13484 ima_seek (SF_PRIVATE *psf, int mode, sf_count_t offset)
13485 { IMA_ADPCM_PRIVATE *pima ;
13486 int newblock, newsample ;
13488 if (! psf->fdata)
13489 return 0 ;
13490 pima = (IMA_ADPCM_PRIVATE*) psf->fdata ;
13492 if (psf->datalength < 0 || psf->dataoffset < 0)
13493 { psf->error = SFE_BAD_SEEK ;
13494 return ((sf_count_t) -1) ;
13497 if (offset == 0)
13498 { psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
13499 pima->blockcount = 0 ;
13500 pima->decode_block (psf, pima) ;
13501 pima->samplecount = 0 ;
13502 return 0 ;
13505 if (offset < 0 || offset > pima->blocks * pima->samplesperblock)
13506 { psf->error = SFE_BAD_SEEK ;
13507 return ((sf_count_t) -1) ;
13510 newblock = offset / pima->samplesperblock ;
13511 newsample = offset % pima->samplesperblock ;
13513 if (mode == SFM_READ)
13514 { psf_fseek (psf, psf->dataoffset + newblock * pima->blocksize, SEEK_SET) ;
13515 pima->blockcount = newblock ;
13516 pima->decode_block (psf, pima) ;
13517 pima->samplecount = newsample ;
13519 else
13520 { /* What to do about write??? */
13521 psf->error = SFE_BAD_SEEK ;
13522 return ((sf_count_t) -1) ;
13525 return newblock * pima->samplesperblock + newsample ;
13526 } /* ima_seek */
13528 /*==========================================================================================
13529 ** IMA ADPCM Write Functions.
13532 static int
13533 ima_writer_init (SF_PRIVATE *psf, int blockalign)
13534 { IMA_ADPCM_PRIVATE *pima ;
13535 int samplesperblock ;
13536 unsigned int pimasize ;
13538 if (psf->mode != SFM_WRITE)
13539 return SFE_BAD_MODE_RW ;
13541 samplesperblock = 2 * (blockalign - 4 * psf->sf.channels) / psf->sf.channels + 1 ;
13543 pimasize = sizeof (IMA_ADPCM_PRIVATE) + blockalign + 3 * psf->sf.channels * samplesperblock ;
13545 if ((pima = calloc (1, pimasize)) == NULL)
13546 return SFE_MALLOC_FAILED ;
13548 psf->fdata = (void*) pima ;
13550 pima->channels = psf->sf.channels ;
13551 pima->blocksize = blockalign ;
13552 pima->samplesperblock = samplesperblock ;
13554 pima->block = (unsigned char*) pima->data ;
13555 pima->samples = (short*) (pima->data + blockalign) ;
13557 pima->samplecount = 0 ;
13559 switch (psf->sf.format & SF_FORMAT_TYPEMASK)
13560 { case SF_FORMAT_WAV :
13561 case SF_FORMAT_W64 :
13562 pima->encode_block = wav_w64_ima_encode_block ;
13563 break ;
13565 case SF_FORMAT_AIFF :
13566 pima->encode_block = aiff_ima_encode_block ;
13567 break ;
13569 default :
13570 psf_log_printf (psf, "ima_reader_init: bad psf->sf.format\n") ;
13571 return SFE_INTERNAL ;
13572 break ;
13575 psf->write_short = ima_write_s ;
13576 psf->write_int = ima_write_i ;
13577 psf->write_float = ima_write_f ;
13578 psf->write_double = ima_write_d ;
13580 return 0 ;
13581 } /* ima_writer_init */
13583 /*==========================================================================================
13586 static int
13587 ima_write_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima, short *ptr, int len)
13588 { int count, total = 0, indx = 0 ;
13590 while (indx < len)
13591 { count = (pima->samplesperblock - pima->samplecount) * pima->channels ;
13593 if (count > len - indx)
13594 count = len - indx ;
13596 memcpy (&(pima->samples [pima->samplecount * pima->channels]), &(ptr [total]), count * sizeof (short)) ;
13597 indx += count ;
13598 pima->samplecount += count / pima->channels ;
13599 total = indx ;
13601 if (pima->samplecount >= pima->samplesperblock)
13602 pima->encode_block (psf, pima) ;
13605 return total ;
13606 } /* ima_write_block */
13608 static sf_count_t
13609 ima_write_s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
13610 { IMA_ADPCM_PRIVATE *pima ;
13611 int writecount, count ;
13612 sf_count_t total = 0 ;
13614 if (! psf->fdata)
13615 return 0 ;
13616 pima = (IMA_ADPCM_PRIVATE*) psf->fdata ;
13618 while (len)
13619 { writecount = (len > 0x10000000) ? 0x10000000 : (int) len ;
13621 count = ima_write_block (psf, pima, ptr, writecount) ;
13623 total += count ;
13624 len -= count ;
13625 if (count != writecount)
13626 break ;
13629 return total ;
13630 } /* ima_write_s */
13632 static sf_count_t
13633 ima_write_i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
13634 { IMA_ADPCM_PRIVATE *pima ;
13635 short *sptr ;
13636 int k, bufferlen, writecount, count ;
13637 sf_count_t total = 0 ;
13639 if (! psf->fdata)
13640 return 0 ;
13641 pima = (IMA_ADPCM_PRIVATE*) psf->fdata ;
13643 sptr = (short*) psf->buffer ;
13644 bufferlen = SF_BUFFER_LEN / sizeof (short) ;
13645 while (len > 0)
13646 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
13647 for (k = 0 ; k < writecount ; k++)
13648 sptr [k] = ptr [total + k] >> 16 ;
13649 count = ima_write_block (psf, pima, sptr, writecount) ;
13650 total += count ;
13651 len -= writecount ;
13652 if (count != writecount)
13653 break ;
13656 return total ;
13657 } /* ima_write_i */
13659 static sf_count_t
13660 ima_write_f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
13661 { IMA_ADPCM_PRIVATE *pima ;
13662 short *sptr ;
13663 int k, bufferlen, writecount, count ;
13664 sf_count_t total = 0 ;
13665 float normfact ;
13667 if (! psf->fdata)
13668 return 0 ;
13669 pima = (IMA_ADPCM_PRIVATE*) psf->fdata ;
13671 normfact = (psf->norm_float == SF_TRUE) ? (1.0 * 0x7FFF) : 1.0 ;
13673 sptr = (short*) psf->buffer ;
13674 bufferlen = SF_BUFFER_LEN / sizeof (short) ;
13675 while (len > 0)
13676 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
13677 for (k = 0 ; k < writecount ; k++)
13678 sptr [k] = lrintf (normfact * ptr [total + k]) ;
13679 count = ima_write_block (psf, pima, sptr, writecount) ;
13680 total += count ;
13681 len -= writecount ;
13682 if (count != writecount)
13683 break ;
13686 return total ;
13687 } /* ima_write_f */
13689 static sf_count_t
13690 ima_write_d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
13691 { IMA_ADPCM_PRIVATE *pima ;
13692 short *sptr ;
13693 int k, bufferlen, writecount, count ;
13694 sf_count_t total = 0 ;
13695 double normfact ;
13697 if (! psf->fdata)
13698 return 0 ;
13699 pima = (IMA_ADPCM_PRIVATE*) psf->fdata ;
13701 normfact = (psf->norm_double == SF_TRUE) ? (1.0 * 0x7FFF) : 1.0 ;
13703 sptr = (short*) psf->buffer ;
13704 bufferlen = SF_BUFFER_LEN / sizeof (short) ;
13705 while (len > 0)
13706 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
13707 for (k = 0 ; k < writecount ; k++)
13708 sptr [k] = lrint (normfact * ptr [total + k]) ;
13709 count = ima_write_block (psf, pima, sptr, writecount) ;
13710 total += count ;
13711 len -= writecount ;
13712 if (count != writecount)
13713 break ;
13716 return total ;
13717 } /* ima_write_d */
13721 ** Do not edit or modify anything in this comment block.
13722 ** The arch-tag line is a file identity tag for the GNU Arch
13723 ** revision control system.
13725 ** arch-tag: 75a54b82-ad18-4758-9933-64e00a7f24e0
13728 ** Copyright (C) 2002-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
13730 ** This program is free software; you can redistribute it and/or modify
13731 ** it under the terms of the GNU Lesser General Public License as published by
13732 ** the Free Software Foundation; either version 2.1 of the License, or
13733 ** (at your option) any later version.
13735 ** This program is distributed in the hope that it will be useful,
13736 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
13737 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13738 ** GNU Lesser General Public License for more details.
13740 ** You should have received a copy of the GNU Lesser General Public License
13741 ** along with this program; if not, write to the Free Software
13742 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
13745 #include <stdlib.h>
13748 #define INTERLEAVE_CHANNELS 6
13750 typedef struct
13751 { double buffer [SF_BUFFER_LEN / sizeof (double)] ;
13753 sf_count_t channel_len ;
13755 sf_count_t (*read_short) (SF_PRIVATE*, short *ptr, sf_count_t len) ;
13756 sf_count_t (*read_int) (SF_PRIVATE*, int *ptr, sf_count_t len) ;
13757 sf_count_t (*read_float) (SF_PRIVATE*, float *ptr, sf_count_t len) ;
13758 sf_count_t (*read_double) (SF_PRIVATE*, double *ptr, sf_count_t len) ;
13760 } INTERLEAVE_DATA ;
13764 static sf_count_t interleave_read_short (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
13765 static sf_count_t interleave_read_int (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
13766 static sf_count_t interleave_read_float (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
13767 static sf_count_t interleave_read_double (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
13769 static sf_count_t interleave_seek (SF_PRIVATE*, int mode, sf_count_t samples_from_start) ;
13775 interleave_init (SF_PRIVATE *psf)
13776 { INTERLEAVE_DATA *pdata ;
13778 if (psf->mode != SFM_READ)
13779 return SFE_INTERLEAVE_MODE ;
13781 if (psf->interleave)
13782 { psf_log_printf (psf, "*** Weird, already have interleave.\n") ;
13783 return 666 ;
13786 /* Free this in sf_close() function. */
13787 if (! (pdata = malloc (sizeof (INTERLEAVE_DATA))))
13788 return SFE_MALLOC_FAILED ;
13790 puts ("interleave_init") ;
13792 psf->interleave = pdata ;
13794 /* Save the existing methods. */
13795 pdata->read_short = psf->read_short ;
13796 pdata->read_int = psf->read_int ;
13797 pdata->read_float = psf->read_float ;
13798 pdata->read_double = psf->read_double ;
13800 pdata->channel_len = psf->sf.frames * psf->bytewidth ;
13802 /* Insert our new methods. */
13803 psf->read_short = interleave_read_short ;
13804 psf->read_int = interleave_read_int ;
13805 psf->read_float = interleave_read_float ;
13806 psf->read_double = interleave_read_double ;
13808 psf->seek = interleave_seek ;
13810 return 0 ;
13811 } /* pcm_interleave_init */
13813 /*------------------------------------------------------------------------------
13816 static sf_count_t
13817 interleave_read_short (SF_PRIVATE *psf, short *ptr, sf_count_t len)
13818 { INTERLEAVE_DATA *pdata ;
13819 sf_count_t offset, templen ;
13820 int chan, count, k ;
13821 short *inptr, *outptr ;
13823 if (! (pdata = psf->interleave))
13824 return 0 ;
13826 inptr = (short*) pdata->buffer ;
13828 for (chan = 0 ; chan < psf->sf.channels ; chan++)
13829 { outptr = ptr + chan ;
13831 offset = psf->dataoffset + chan * psf->bytewidth * psf->read_current ;
13833 if (psf_fseek (psf, offset, SEEK_SET) != offset)
13834 { psf->error = SFE_INTERLEAVE_SEEK ;
13835 return 0 ;
13838 templen = len / psf->sf.channels ;
13840 while (templen > 0)
13841 { if (templen > SIGNED_SIZEOF (pdata->buffer) / SIGNED_SIZEOF (short))
13842 count = SIGNED_SIZEOF (pdata->buffer) / SIGNED_SIZEOF (short) ;
13843 else
13844 count = (int) templen ;
13846 if (pdata->read_short (psf, inptr, count) != count)
13847 { psf->error = SFE_INTERLEAVE_READ ;
13848 return 0 ;
13851 for (k = 0 ; k < count ; k++)
13852 { *outptr = inptr [k] ;
13853 outptr += psf->sf.channels ;
13856 templen -= count ;
13860 return len ;
13861 } /* interleave_read_short */
13863 static sf_count_t
13864 interleave_read_int (SF_PRIVATE *psf, int *ptr, sf_count_t len)
13865 { INTERLEAVE_DATA *pdata ;
13866 sf_count_t offset, templen ;
13867 int chan, count, k ;
13868 int *inptr, *outptr ;
13870 if (! (pdata = psf->interleave))
13871 return 0 ;
13873 inptr = (int*) pdata->buffer ;
13875 for (chan = 0 ; chan < psf->sf.channels ; chan++)
13876 { outptr = ptr + chan ;
13878 offset = psf->dataoffset + chan * psf->bytewidth * psf->read_current ;
13880 if (psf_fseek (psf, offset, SEEK_SET) != offset)
13881 { psf->error = SFE_INTERLEAVE_SEEK ;
13882 return 0 ;
13885 templen = len / psf->sf.channels ;
13887 while (templen > 0)
13888 { if (templen > SIGNED_SIZEOF (pdata->buffer) / SIGNED_SIZEOF (int))
13889 count = SIGNED_SIZEOF (pdata->buffer) / SIGNED_SIZEOF (int) ;
13890 else
13891 count = (int) templen ;
13893 if (pdata->read_int (psf, inptr, count) != count)
13894 { psf->error = SFE_INTERLEAVE_READ ;
13895 return 0 ;
13898 for (k = 0 ; k < count ; k++)
13899 { *outptr = inptr [k] ;
13900 outptr += psf->sf.channels ;
13903 templen -= count ;
13907 return len ;
13908 } /* interleave_read_int */
13910 static sf_count_t
13911 interleave_read_float (SF_PRIVATE *psf, float *ptr, sf_count_t len)
13912 { INTERLEAVE_DATA *pdata ;
13913 sf_count_t offset, templen ;
13914 int chan, count, k ;
13915 float *inptr, *outptr ;
13917 if (! (pdata = psf->interleave))
13918 return 0 ;
13920 inptr = (float*) pdata->buffer ;
13922 for (chan = 0 ; chan < psf->sf.channels ; chan++)
13923 { outptr = ptr + chan ;
13925 offset = psf->dataoffset + pdata->channel_len * chan + psf->read_current * psf->bytewidth ;
13927 /*-printf ("chan : %d read_current : %6lld offset : %6lld\n", chan, psf->read_current, offset) ;-*/
13929 if (psf_fseek (psf, offset, SEEK_SET) != offset)
13930 { psf->error = SFE_INTERLEAVE_SEEK ;
13931 /*-puts ("interleave_seek error") ; exit (1) ;-*/
13932 return 0 ;
13935 templen = len / psf->sf.channels ;
13937 while (templen > 0)
13938 { if (templen > SIGNED_SIZEOF (pdata->buffer) / SIGNED_SIZEOF (float))
13939 count = SIGNED_SIZEOF (pdata->buffer) / SIGNED_SIZEOF (float) ;
13940 else
13941 count = (int) templen ;
13943 if (pdata->read_float (psf, inptr, count) != count)
13944 { psf->error = SFE_INTERLEAVE_READ ;
13945 /*-puts ("interleave_read error") ; exit (1) ;-*/
13946 return 0 ;
13949 for (k = 0 ; k < count ; k++)
13950 { *outptr = inptr [k] ;
13951 outptr += psf->sf.channels ;
13954 templen -= count ;
13958 return len ;
13959 } /* interleave_read_float */
13961 static sf_count_t
13962 interleave_read_double (SF_PRIVATE *psf, double *ptr, sf_count_t len)
13963 { INTERLEAVE_DATA *pdata ;
13964 sf_count_t offset, templen ;
13965 int chan, count, k ;
13966 double *inptr, *outptr ;
13968 if (! (pdata = psf->interleave))
13969 return 0 ;
13971 inptr = (double*) pdata->buffer ;
13973 for (chan = 0 ; chan < psf->sf.channels ; chan++)
13974 { outptr = ptr + chan ;
13976 offset = psf->dataoffset + chan * psf->bytewidth * psf->read_current ;
13978 if (psf_fseek (psf, offset, SEEK_SET) != offset)
13979 { psf->error = SFE_INTERLEAVE_SEEK ;
13980 return 0 ;
13983 templen = len / psf->sf.channels ;
13985 while (templen > 0)
13986 { if (templen > SIGNED_SIZEOF (pdata->buffer) / SIGNED_SIZEOF (double))
13987 count = SIGNED_SIZEOF (pdata->buffer) / SIGNED_SIZEOF (double) ;
13988 else
13989 count = (int) templen ;
13991 if (pdata->read_double (psf, inptr, count) != count)
13992 { psf->error = SFE_INTERLEAVE_READ ;
13993 return 0 ;
13996 for (k = 0 ; k < count ; k++)
13997 { *outptr = inptr [k] ;
13998 outptr += psf->sf.channels ;
14001 templen -= count ;
14005 return len ;
14006 } /* interleave_read_double */
14008 /*------------------------------------------------------------------------------
14011 static sf_count_t
14012 interleave_seek (SF_PRIVATE *psf, int mode, sf_count_t samples_from_start)
14013 { psf = psf ; mode = mode ;
14016 ** Do nothing here. This is a place holder to prevent the default
14017 ** seek function from being called.
14020 return samples_from_start ;
14021 } /* interleave_seek */
14023 ** Do not edit or modify anything in this comment block.
14024 ** The arch-tag line is a file identity tag for the GNU Arch
14025 ** revision control system.
14027 ** arch-tag: 82314e13-0225-4408-a2f2-e6dab3f38904
14030 ** Copyright (C) 2001-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
14032 ** This program is free software; you can redistribute it and/or modify
14033 ** it under the terms of the GNU Lesser General Public License as published by
14034 ** the Free Software Foundation; either version 2.1 of the License, or
14035 ** (at your option) any later version.
14037 ** This program is distributed in the hope that it will be useful,
14038 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
14039 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14040 ** GNU Lesser General Public License for more details.
14042 ** You should have received a copy of the GNU Lesser General Public License
14043 ** along with this program; if not, write to the Free Software
14044 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
14047 #include <stdio.h>
14048 #include <fcntl.h>
14049 #include <string.h>
14050 #include <ctype.h>
14053 /*------------------------------------------------------------------------------
14054 ** Macros to handle big/little endian issues.
14057 /* The IRCAM magic number is weird in that one byte in the number can have
14058 ** values of 0x1, 0x2, 0x03 or 0x04. Hence the need for a marker and a mask.
14061 #define IRCAM_BE_MASK (MAKE_MARKER (0xFF, 0xFF, 0x00, 0xFF))
14062 #define IRCAM_BE_MARKER (MAKE_MARKER (0x64, 0xA3, 0x00, 0x00))
14064 #define IRCAM_LE_MASK (MAKE_MARKER (0xFF, 0x00, 0xFF, 0xFF))
14065 #define IRCAM_LE_MARKER (MAKE_MARKER (0x00, 0x00, 0xA3, 0x64))
14067 #define IRCAM_02B_MARKER (MAKE_MARKER (0x00, 0x02, 0xA3, 0x64))
14068 #define IRCAM_03L_MARKER (MAKE_MARKER (0x64, 0xA3, 0x03, 0x00))
14070 #define IRCAM_DATA_OFFSET (1024)
14072 /*------------------------------------------------------------------------------
14073 ** Typedefs.
14076 enum
14077 { IRCAM_PCM_16 = 0x00002,
14078 IRCAM_FLOAT = 0x00004,
14079 IRCAM_ALAW = 0x10001,
14080 IRCAM_ULAW = 0x20001,
14081 IRCAM_PCM_32 = 0x40004
14085 /*------------------------------------------------------------------------------
14086 ** Private static functions.
14089 static int ircam_close (SF_PRIVATE *psf) ;
14090 static int ircam_write_header (SF_PRIVATE *psf, int calc_length) ;
14091 static int ircam_read_header (SF_PRIVATE *psf) ;
14093 static int get_encoding (int subformat) ;
14095 static const char* get_encoding_str (int encoding) ;
14097 /*------------------------------------------------------------------------------
14098 ** Public function.
14102 ircam_open (SF_PRIVATE *psf)
14103 { int subformat ;
14104 int error = SFE_NO_ERROR ;
14106 if (psf->mode == SFM_READ || (psf->mode == SFM_RDWR && psf->filelength > 0))
14107 { if ((error = ircam_read_header (psf)))
14108 return error ;
14111 subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
14113 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
14114 { if ((psf->sf.format & SF_FORMAT_TYPEMASK) != SF_FORMAT_IRCAM)
14115 return SFE_BAD_OPEN_FORMAT ;
14117 psf->endian = psf->sf.format & SF_FORMAT_ENDMASK ;
14118 if (psf->endian == 0 || psf->endian == SF_ENDIAN_CPU)
14119 psf->endian = (CPU_IS_BIG_ENDIAN) ? SF_ENDIAN_BIG : SF_ENDIAN_LITTLE ;
14121 psf->dataoffset = IRCAM_DATA_OFFSET ;
14123 if ((error = ircam_write_header (psf, SF_FALSE)))
14124 return error ;
14126 psf->write_header = ircam_write_header ;
14129 psf->close = ircam_close ;
14131 switch (subformat)
14132 { case SF_FORMAT_ULAW : /* 8-bit Ulaw encoding. */
14133 error = ulaw_init (psf) ;
14134 break ;
14136 case SF_FORMAT_ALAW : /* 8-bit Alaw encoding. */
14137 error = alaw_init (psf) ;
14138 break ;
14140 case SF_FORMAT_PCM_16 : /* 16-bit linear PCM. */
14141 case SF_FORMAT_PCM_32 : /* 32-bit linear PCM. */
14142 error = pcm_init (psf) ;
14143 break ;
14145 case SF_FORMAT_FLOAT : /* 32-bit linear PCM. */
14146 error = float32_init (psf) ;
14147 break ;
14149 default : break ;
14152 return error ;
14153 } /* ircam_open */
14155 /*------------------------------------------------------------------------------
14158 static int
14159 ircam_read_header (SF_PRIVATE *psf)
14160 { unsigned int marker, encoding ;
14161 float samplerate ;
14162 int error = SFE_NO_ERROR ;
14164 psf_binheader_readf (psf, "epmf44", 0, &marker, &samplerate, &(psf->sf.channels), &encoding) ;
14166 if (((marker & IRCAM_LE_MASK) != IRCAM_LE_MARKER) &&
14167 ((marker & IRCAM_BE_MASK) != IRCAM_BE_MARKER))
14168 { psf_log_printf (psf, "marker: 0x%X\n", marker) ;
14169 return SFE_IRCAM_NO_MARKER ;
14172 psf->endian = SF_ENDIAN_LITTLE ;
14174 if (psf->sf.channels > 256)
14175 { psf_binheader_readf (psf, "Epmf44", 0, &marker, &samplerate, &(psf->sf.channels), &encoding) ;
14177 /* Sanity checking for endian-ness detection. */
14178 if (psf->sf.channels > 256)
14179 { psf_log_printf (psf, "marker: 0x%X\n", marker) ;
14180 return SFE_IRCAM_BAD_CHANNELS ;
14183 psf->endian = SF_ENDIAN_BIG ;
14186 psf_log_printf (psf, "marker: 0x%X\n", marker) ;
14188 psf->sf.samplerate = (int) samplerate ;
14190 psf_log_printf (psf, " Sample Rate : %d\n"
14191 " Channels : %d\n"
14192 " Encoding : %X => %s\n", psf->sf.samplerate, psf->sf.channels, encoding, get_encoding_str (encoding)) ;
14194 switch (encoding)
14195 { case IRCAM_PCM_16 :
14196 psf->bytewidth = 2 ;
14197 psf->blockwidth = psf->sf.channels * psf->bytewidth ;
14199 psf->sf.format = SF_FORMAT_IRCAM | SF_FORMAT_PCM_16 ;
14200 break ;
14202 case IRCAM_PCM_32 :
14203 psf->bytewidth = 4 ;
14204 psf->blockwidth = psf->sf.channels * psf->bytewidth ;
14206 psf->sf.format = SF_FORMAT_IRCAM | SF_FORMAT_PCM_32 ;
14207 break ;
14209 case IRCAM_FLOAT :
14210 psf->bytewidth = 4 ;
14211 psf->blockwidth = psf->sf.channels * psf->bytewidth ;
14213 psf->sf.format = SF_FORMAT_IRCAM | SF_FORMAT_FLOAT ;
14214 break ;
14216 case IRCAM_ALAW :
14217 psf->bytewidth = 1 ;
14218 psf->blockwidth = psf->sf.channels * psf->bytewidth ;
14220 psf->sf.format = SF_FORMAT_IRCAM | SF_FORMAT_ALAW ;
14221 break ;
14223 case IRCAM_ULAW :
14224 psf->bytewidth = 1 ;
14225 psf->blockwidth = psf->sf.channels * psf->bytewidth ;
14227 psf->sf.format = SF_FORMAT_IRCAM | SF_FORMAT_ULAW ;
14228 break ;
14230 default :
14231 error = SFE_IRCAM_UNKNOWN_FORMAT ;
14232 break ;
14235 if (psf->endian == SF_ENDIAN_BIG)
14236 psf->sf.format |= SF_ENDIAN_BIG ;
14237 else
14238 psf->sf.format |= SF_ENDIAN_LITTLE ;
14240 if (error)
14241 return error ;
14243 psf->dataoffset = IRCAM_DATA_OFFSET ;
14244 psf->datalength = psf->filelength - psf->dataoffset ;
14246 if (psf->sf.frames == 0 && psf->blockwidth)
14247 psf->sf.frames = psf->datalength / psf->blockwidth ;
14249 psf_log_printf (psf, " Samples : %d\n", psf->sf.frames) ;
14251 psf_binheader_readf (psf, "p", IRCAM_DATA_OFFSET) ;
14253 return 0 ;
14254 } /* ircam_read_header */
14256 static int
14257 ircam_close (SF_PRIVATE *psf)
14259 psf_log_printf (psf, "close\n") ;
14261 return 0 ;
14262 } /* ircam_close */
14264 static int
14265 ircam_write_header (SF_PRIVATE *psf, int calc_length)
14266 { int encoding ;
14267 float samplerate ;
14268 sf_count_t current ;
14270 if (psf->pipeoffset > 0)
14271 return 0 ;
14273 current = psf_ftell (psf) ;
14275 calc_length = calc_length ;
14277 /* This also sets psf->endian. */
14278 encoding = get_encoding (psf->sf.format & SF_FORMAT_SUBMASK) ;
14280 if (encoding == 0)
14281 return SFE_BAD_OPEN_FORMAT ;
14283 /* Reset the current header length to zero. */
14284 psf->header [0] = 0 ;
14285 psf->headindex = 0 ;
14287 if (psf->is_pipe == SF_FALSE)
14288 psf_fseek (psf, 0, SEEK_SET) ;
14290 samplerate = psf->sf.samplerate ;
14292 switch (psf->endian)
14293 { case SF_ENDIAN_BIG :
14294 psf_binheader_writef (psf, "Emf", IRCAM_02B_MARKER, samplerate) ;
14295 psf_binheader_writef (psf, "E44", psf->sf.channels, encoding) ;
14296 break ;
14298 case SF_ENDIAN_LITTLE :
14299 psf_binheader_writef (psf, "emf", IRCAM_03L_MARKER, samplerate) ;
14300 psf_binheader_writef (psf, "e44", psf->sf.channels, encoding) ;
14301 break ;
14303 default : return SFE_BAD_OPEN_FORMAT ;
14306 psf_binheader_writef (psf, "z", (size_t) (IRCAM_DATA_OFFSET - psf->headindex)) ;
14308 /* Header construction complete so write it out. */
14309 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
14311 if (psf->error)
14312 return psf->error ;
14314 if (current > 0)
14315 psf_fseek (psf, current, SEEK_SET) ;
14317 return psf->error ;
14318 } /* ircam_write_header */
14320 static int
14321 get_encoding (int subformat)
14322 { switch (subformat)
14323 { case SF_FORMAT_PCM_16 : return IRCAM_PCM_16 ;
14324 case SF_FORMAT_PCM_32 : return IRCAM_PCM_32 ;
14326 case SF_FORMAT_FLOAT : return IRCAM_FLOAT ;
14328 case SF_FORMAT_ULAW : return IRCAM_ULAW ;
14329 case SF_FORMAT_ALAW : return IRCAM_ALAW ;
14331 default : break ;
14334 return 0 ;
14335 } /* get_encoding */
14337 static const char*
14338 get_encoding_str (int encoding)
14339 { switch (encoding)
14340 { case IRCAM_PCM_16 : return "16 bit PCM" ;
14341 case IRCAM_FLOAT : return "32 bit float" ;
14342 case IRCAM_ALAW : return "A law" ;
14343 case IRCAM_ULAW : return "u law" ;
14344 case IRCAM_PCM_32 : return "32 bit PCM" ;
14346 return "Unknown encoding" ;
14347 } /* get_encoding_str */
14350 ** Do not edit or modify anything in this comment block.
14351 ** The arch-tag line is a file identity tag for the GNU Arch
14352 ** revision control system.
14354 ** arch-tag: f2714ab8-f286-4c94-9740-edaf673a1c71
14357 * Copyright 1992 by Jutta Degener and Carsten Bormann, Technische
14358 * Universitaet Berlin. See the accompanying file "COPYRIGHT" for
14359 * details. THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE.
14362 #include <stdio.h>
14363 #include <assert.h>
14368 * 4.2.11 .. 4.2.12 LONG TERM PREDICTOR (LTP) SECTION
14373 * This module computes the LTP gain (bc) and the LTP lag (Nc)
14374 * for the long term analysis filter. This is done by calculating a
14375 * maximum of the cross-correlation function between the current
14376 * sub-segment short term residual signal d[0..39] (output of
14377 * the short term analysis filter; for simplification the index
14378 * of this array begins at 0 and ends at 39 for each sub-segment of the
14379 * RPE-LTP analysis) and the previous reconstructed short term
14380 * residual signal dp[ -120 .. -1 ]. A dynamic scaling must be
14381 * performed to avoid overflow.
14384 /* The next procedure exists in six versions. First two integer
14385 * version (if USE_FLOAT_MUL is not defined); then four floating
14386 * point versions, twice with proper scaling (USE_FLOAT_MUL defined),
14387 * once without (USE_FLOAT_MUL and FAST defined, and fast run-time
14388 * option used). Every pair has first a Cut version (see the -C
14389 * option to toast or the LTP_CUT option to gsm_option()), then the
14390 * uncut one. (For a detailed explanation of why this is altogether
14391 * a bad idea, see Henry Spencer and Geoff Collyer, ``#ifdef Considered
14392 * Harmful''.)
14395 #ifndef USE_FLOAT_MUL
14397 #ifdef LTP_CUT
14399 static void Cut_Calculation_of_the_LTP_parameters (
14401 struct gsm_state * st,
14403 register word * d, /* [0..39] IN */
14404 register word * dp, /* [-120..-1] IN */
14405 word * bc_out, /* OUT */
14406 word * Nc_out /* OUT */
14409 register int k, lambda;
14410 word Nc, bc;
14411 word wt[40];
14413 longword L_result;
14414 longword L_max, L_power;
14415 word R, S, dmax, scal, best_k;
14416 word ltp_cut;
14418 register word temp, wt_k;
14420 /* Search of the optimum scaling of d[0..39].
14422 dmax = 0;
14423 for (k = 0; k <= 39; k++) {
14424 temp = d[k];
14425 temp = GSM_ABS( temp );
14426 if (temp > dmax) {
14427 dmax = temp;
14428 best_k = k;
14431 temp = 0;
14432 if (dmax == 0) scal = 0;
14433 else {
14434 assert(dmax > 0);
14435 temp = gsm_norm( (longword)dmax << 16 );
14437 if (temp > 6) scal = 0;
14438 else scal = 6 - temp;
14439 assert(scal >= 0);
14441 /* Search for the maximum cross-correlation and coding of the LTP lag
14443 L_max = 0;
14444 Nc = 40; /* index for the maximum cross-correlation */
14445 wt_k = SASR_W(d[best_k], scal);
14447 for (lambda = 40; lambda <= 120; lambda++) {
14448 L_result = (longword)wt_k * dp[best_k - lambda];
14449 if (L_result > L_max) {
14450 Nc = lambda;
14451 L_max = L_result;
14454 *Nc_out = Nc;
14455 L_max <<= 1;
14457 /* Rescaling of L_max
14459 assert(scal <= 100 && scal >= -100);
14460 L_max = L_max >> (6 - scal); /* sub(6, scal) */
14462 assert( Nc <= 120 && Nc >= 40);
14464 /* Compute the power of the reconstructed short term residual
14465 * signal dp[..]
14467 L_power = 0;
14468 for (k = 0; k <= 39; k++) {
14470 register longword L_temp;
14472 L_temp = SASR_W( dp[k - Nc], 3 );
14473 L_power += L_temp * L_temp;
14475 L_power <<= 1; /* from L_MULT */
14477 /* Normalization of L_max and L_power
14480 if (L_max <= 0) {
14481 *bc_out = 0;
14482 return;
14484 if (L_max >= L_power) {
14485 *bc_out = 3;
14486 return;
14489 temp = gsm_norm( L_power );
14491 R = SASR( L_max << temp, 16 );
14492 S = SASR( L_power << temp, 16 );
14494 /* Coding of the LTP gain
14497 /* Table 4.3a must be used to obtain the level DLB[i] for the
14498 * quantization of the LTP gain b to get the coded version bc.
14500 for (bc = 0; bc <= 2; bc++) if (R <= gsm_mult(S, gsm_DLB[bc])) break;
14501 *bc_out = bc;
14504 #endif /* LTP_CUT */
14506 static void Calculation_of_the_LTP_parameters (
14507 register word * d, /* [0..39] IN */
14508 register word * dp, /* [-120..-1] IN */
14509 word * bc_out, /* OUT */
14510 word * Nc_out /* OUT */
14513 register int k, lambda;
14514 word Nc, bc;
14515 word wt[40];
14517 longword L_max, L_power;
14518 word R, S, dmax, scal;
14519 register word temp;
14521 /* Search of the optimum scaling of d[0..39].
14523 dmax = 0;
14525 for (k = 0; k <= 39; k++) {
14526 temp = d[k];
14527 temp = GSM_ABS( temp );
14528 if (temp > dmax) dmax = temp;
14531 temp = 0;
14532 if (dmax == 0) scal = 0;
14533 else {
14534 assert(dmax > 0);
14535 temp = gsm_norm( (longword)dmax << 16 );
14538 if (temp > 6) scal = 0;
14539 else scal = 6 - temp;
14541 assert(scal >= 0);
14543 /* Initialization of a working array wt
14546 for (k = 0; k <= 39; k++) wt[k] = SASR_W( d[k], scal );
14548 /* Search for the maximum cross-correlation and coding of the LTP lag
14550 L_max = 0;
14551 Nc = 40; /* index for the maximum cross-correlation */
14553 for (lambda = 40; lambda <= 120; lambda++) {
14555 # undef long_termSTEP
14556 # define long_termSTEP(k) (longword)wt[k] * dp[k - lambda]
14558 register longword L_result;
14560 L_result = long_termSTEP(0) ; L_result += long_termSTEP(1) ;
14561 L_result += long_termSTEP(2) ; L_result += long_termSTEP(3) ;
14562 L_result += long_termSTEP(4) ; L_result += long_termSTEP(5) ;
14563 L_result += long_termSTEP(6) ; L_result += long_termSTEP(7) ;
14564 L_result += long_termSTEP(8) ; L_result += long_termSTEP(9) ;
14565 L_result += long_termSTEP(10) ; L_result += long_termSTEP(11) ;
14566 L_result += long_termSTEP(12) ; L_result += long_termSTEP(13) ;
14567 L_result += long_termSTEP(14) ; L_result += long_termSTEP(15) ;
14568 L_result += long_termSTEP(16) ; L_result += long_termSTEP(17) ;
14569 L_result += long_termSTEP(18) ; L_result += long_termSTEP(19) ;
14570 L_result += long_termSTEP(20) ; L_result += long_termSTEP(21) ;
14571 L_result += long_termSTEP(22) ; L_result += long_termSTEP(23) ;
14572 L_result += long_termSTEP(24) ; L_result += long_termSTEP(25) ;
14573 L_result += long_termSTEP(26) ; L_result += long_termSTEP(27) ;
14574 L_result += long_termSTEP(28) ; L_result += long_termSTEP(29) ;
14575 L_result += long_termSTEP(30) ; L_result += long_termSTEP(31) ;
14576 L_result += long_termSTEP(32) ; L_result += long_termSTEP(33) ;
14577 L_result += long_termSTEP(34) ; L_result += long_termSTEP(35) ;
14578 L_result += long_termSTEP(36) ; L_result += long_termSTEP(37) ;
14579 L_result += long_termSTEP(38) ; L_result += long_termSTEP(39) ;
14581 if (L_result > L_max) {
14583 Nc = lambda;
14584 L_max = L_result;
14588 *Nc_out = Nc;
14590 L_max <<= 1;
14592 /* Rescaling of L_max
14594 assert(scal <= 100 && scal >= -100);
14595 L_max = L_max >> (6 - scal); /* sub(6, scal) */
14597 assert( Nc <= 120 && Nc >= 40);
14599 /* Compute the power of the reconstructed short term residual
14600 * signal dp[..]
14602 L_power = 0;
14603 for (k = 0; k <= 39; k++) {
14605 register longword L_temp;
14607 L_temp = SASR_W( dp[k - Nc], 3 );
14608 L_power += L_temp * L_temp;
14610 L_power <<= 1; /* from L_MULT */
14612 /* Normalization of L_max and L_power
14615 if (L_max <= 0) {
14616 *bc_out = 0;
14617 return;
14619 if (L_max >= L_power) {
14620 *bc_out = 3;
14621 return;
14624 temp = gsm_norm( L_power );
14626 R = SASR_L( L_max << temp, 16 );
14627 S = SASR_L( L_power << temp, 16 );
14629 /* Coding of the LTP gain
14632 /* Table 4.3a must be used to obtain the level DLB[i] for the
14633 * quantization of the LTP gain b to get the coded version bc.
14635 for (bc = 0; bc <= 2; bc++) if (R <= gsm_mult(S, gsm_DLB[bc])) break;
14636 *bc_out = bc;
14639 #else /* USE_FLOAT_MUL */
14641 #ifdef LTP_CUT
14643 static void Cut_Calculation_of_the_LTP_parameters (
14644 struct gsm_state * st, /* IN */
14645 register word * d, /* [0..39] IN */
14646 register word * dp, /* [-120..-1] IN */
14647 word * bc_out, /* OUT */
14648 word * Nc_out /* OUT */
14651 register int k, lambda;
14652 word Nc, bc;
14653 word ltp_cut;
14655 float wt_float[40];
14656 float dp_float_base[120], * dp_float = dp_float_base + 120;
14658 longword L_max, L_power;
14659 word R, S, dmax, scal;
14660 register word temp;
14662 /* Search of the optimum scaling of d[0..39].
14664 dmax = 0;
14666 for (k = 0; k <= 39; k++) {
14667 temp = d[k];
14668 temp = GSM_ABS( temp );
14669 if (temp > dmax) dmax = temp;
14672 temp = 0;
14673 if (dmax == 0) scal = 0;
14674 else {
14675 assert(dmax > 0);
14676 temp = gsm_norm( (longword)dmax << 16 );
14679 if (temp > 6) scal = 0;
14680 else scal = 6 - temp;
14682 assert(scal >= 0);
14683 ltp_cut = (longword)SASR_W(dmax, scal) * st->ltp_cut / 100;
14686 /* Initialization of a working array wt
14689 for (k = 0; k < 40; k++) {
14690 register word w = SASR_W( d[k], scal );
14691 if (w < 0 ? w > -ltp_cut : w < ltp_cut) {
14692 wt_float[k] = 0.0;
14694 else {
14695 wt_float[k] = w;
14698 for (k = -120; k < 0; k++) dp_float[k] = dp[k];
14700 /* Search for the maximum cross-correlation and coding of the LTP lag
14702 L_max = 0;
14703 Nc = 40; /* index for the maximum cross-correlation */
14705 for (lambda = 40; lambda <= 120; lambda += 9) {
14707 /* Calculate L_result for l = lambda .. lambda + 9.
14709 register float *lp = dp_float - lambda;
14711 register float W;
14712 register float a = lp[-8], b = lp[-7], c = lp[-6],
14713 d = lp[-5], e = lp[-4], f = lp[-3],
14714 g = lp[-2], h = lp[-1];
14715 register float E;
14716 register float S0 = 0, S1 = 0, S2 = 0, S3 = 0, S4 = 0,
14717 S5 = 0, S6 = 0, S7 = 0, S8 = 0;
14719 # undef long_termSTEP
14720 # define long_termSTEP(K, a, b, c, d, e, f, g, h) \
14721 if ((W = wt_float[K]) != 0.0) { \
14722 E = W * a; S8 += E; \
14723 E = W * b; S7 += E; \
14724 E = W * c; S6 += E; \
14725 E = W * d; S5 += E; \
14726 E = W * e; S4 += E; \
14727 E = W * f; S3 += E; \
14728 E = W * g; S2 += E; \
14729 E = W * h; S1 += E; \
14730 a = lp[K]; \
14731 E = W * a; S0 += E; } else (a = lp[K])
14733 # define long_termSTEP_A(K) long_termSTEP(K, a, b, c, d, e, f, g, h)
14734 # define long_termSTEP_B(K) long_termSTEP(K, b, c, d, e, f, g, h, a)
14735 # define long_termSTEP_C(K) long_termSTEP(K, c, d, e, f, g, h, a, b)
14736 # define long_termSTEP_D(K) long_termSTEP(K, d, e, f, g, h, a, b, c)
14737 # define long_termSTEP_E(K) long_termSTEP(K, e, f, g, h, a, b, c, d)
14738 # define long_termSTEP_F(K) long_termSTEP(K, f, g, h, a, b, c, d, e)
14739 # define long_termSTEP_G(K) long_termSTEP(K, g, h, a, b, c, d, e, f)
14740 # define long_termSTEP_H(K) long_termSTEP(K, h, a, b, c, d, e, f, g)
14742 long_termSTEP_A( 0); long_termSTEP_B( 1); long_termSTEP_C( 2); long_termSTEP_D( 3);
14743 long_termSTEP_E( 4); long_termSTEP_F( 5); long_termSTEP_G( 6); long_termSTEP_H( 7);
14745 long_termSTEP_A( 8); long_termSTEP_B( 9); long_termSTEP_C(10); long_termSTEP_D(11);
14746 long_termSTEP_E(12); long_termSTEP_F(13); long_termSTEP_G(14); long_termSTEP_H(15);
14748 long_termSTEP_A(16); long_termSTEP_B(17); long_termSTEP_C(18); long_termSTEP_D(19);
14749 long_termSTEP_E(20); long_termSTEP_F(21); long_termSTEP_G(22); long_termSTEP_H(23);
14751 long_termSTEP_A(24); long_termSTEP_B(25); long_termSTEP_C(26); long_termSTEP_D(27);
14752 long_termSTEP_E(28); long_termSTEP_F(29); long_termSTEP_G(30); long_termSTEP_H(31);
14754 long_termSTEP_A(32); long_termSTEP_B(33); long_termSTEP_C(34); long_termSTEP_D(35);
14755 long_termSTEP_E(36); long_termSTEP_F(37); long_termSTEP_G(38); long_termSTEP_H(39);
14757 if (S0 > L_max) { L_max = S0; Nc = lambda; }
14758 if (S1 > L_max) { L_max = S1; Nc = lambda + 1; }
14759 if (S2 > L_max) { L_max = S2; Nc = lambda + 2; }
14760 if (S3 > L_max) { L_max = S3; Nc = lambda + 3; }
14761 if (S4 > L_max) { L_max = S4; Nc = lambda + 4; }
14762 if (S5 > L_max) { L_max = S5; Nc = lambda + 5; }
14763 if (S6 > L_max) { L_max = S6; Nc = lambda + 6; }
14764 if (S7 > L_max) { L_max = S7; Nc = lambda + 7; }
14765 if (S8 > L_max) { L_max = S8; Nc = lambda + 8; }
14768 *Nc_out = Nc;
14770 L_max <<= 1;
14772 /* Rescaling of L_max
14774 assert(scal <= 100 && scal >= -100);
14775 L_max = L_max >> (6 - scal); /* sub(6, scal) */
14777 assert( Nc <= 120 && Nc >= 40);
14779 /* Compute the power of the reconstructed short term residual
14780 * signal dp[..]
14782 L_power = 0;
14783 for (k = 0; k <= 39; k++) {
14785 register longword L_temp;
14787 L_temp = SASR_W( dp[k - Nc], 3 );
14788 L_power += L_temp * L_temp;
14790 L_power <<= 1; /* from L_MULT */
14792 /* Normalization of L_max and L_power
14795 if (L_max <= 0) {
14796 *bc_out = 0;
14797 return;
14799 if (L_max >= L_power) {
14800 *bc_out = 3;
14801 return;
14804 temp = gsm_norm( L_power );
14806 R = SASR( L_max << temp, 16 );
14807 S = SASR( L_power << temp, 16 );
14809 /* Coding of the LTP gain
14812 /* Table 4.3a must be used to obtain the level DLB[i] for the
14813 * quantization of the LTP gain b to get the coded version bc.
14815 for (bc = 0; bc <= 2; bc++) if (R <= gsm_mult(S, gsm_DLB[bc])) break;
14816 *bc_out = bc;
14819 #endif /* LTP_CUT */
14821 static void Calculation_of_the_LTP_parameters (
14822 register word * din, /* [0..39] IN */
14823 register word * dp, /* [-120..-1] IN */
14824 word * bc_out, /* OUT */
14825 word * Nc_out /* OUT */
14828 register int k, lambda;
14829 word Nc, bc;
14831 float wt_float[40];
14832 float dp_float_base[120], * dp_float = dp_float_base + 120;
14834 longword L_max, L_power;
14835 word R, S, dmax, scal;
14836 register word temp;
14838 /* Search of the optimum scaling of d[0..39].
14840 dmax = 0;
14842 for (k = 0; k <= 39; k++) {
14843 temp = din [k] ;
14844 temp = GSM_ABS (temp) ;
14845 if (temp > dmax) dmax = temp;
14848 temp = 0;
14849 if (dmax == 0) scal = 0;
14850 else {
14851 assert(dmax > 0);
14852 temp = gsm_norm( (longword)dmax << 16 );
14855 if (temp > 6) scal = 0;
14856 else scal = 6 - temp;
14858 assert(scal >= 0);
14860 /* Initialization of a working array wt
14863 for (k = 0; k < 40; k++) wt_float[k] = SASR_W (din [k], scal) ;
14864 for (k = -120; k < 0; k++) dp_float[k] = dp[k];
14866 /* Search for the maximum cross-correlation and coding of the LTP lag
14868 L_max = 0;
14869 Nc = 40; /* index for the maximum cross-correlation */
14871 for (lambda = 40; lambda <= 120; lambda += 9) {
14873 /* Calculate L_result for l = lambda .. lambda + 9.
14875 register float *lp = dp_float - lambda;
14877 register float W;
14878 register float a = lp[-8], b = lp[-7], c = lp[-6],
14879 d = lp[-5], e = lp[-4], f = lp[-3],
14880 g = lp[-2], h = lp[-1];
14881 register float E;
14882 register float S0 = 0, S1 = 0, S2 = 0, S3 = 0, S4 = 0,
14883 S5 = 0, S6 = 0, S7 = 0, S8 = 0;
14885 # undef long_termSTEP
14886 # define long_termSTEP(K, a, b, c, d, e, f, g, h) \
14887 W = wt_float[K]; \
14888 E = W * a; S8 += E; \
14889 E = W * b; S7 += E; \
14890 E = W * c; S6 += E; \
14891 E = W * d; S5 += E; \
14892 E = W * e; S4 += E; \
14893 E = W * f; S3 += E; \
14894 E = W * g; S2 += E; \
14895 E = W * h; S1 += E; \
14896 a = lp[K]; \
14897 E = W * a; S0 += E
14899 # define long_termSTEP_A(K) long_termSTEP(K, a, b, c, d, e, f, g, h)
14900 # define long_termSTEP_B(K) long_termSTEP(K, b, c, d, e, f, g, h, a)
14901 # define long_termSTEP_C(K) long_termSTEP(K, c, d, e, f, g, h, a, b)
14902 # define long_termSTEP_D(K) long_termSTEP(K, d, e, f, g, h, a, b, c)
14903 # define long_termSTEP_E(K) long_termSTEP(K, e, f, g, h, a, b, c, d)
14904 # define long_termSTEP_F(K) long_termSTEP(K, f, g, h, a, b, c, d, e)
14905 # define long_termSTEP_G(K) long_termSTEP(K, g, h, a, b, c, d, e, f)
14906 # define long_termSTEP_H(K) long_termSTEP(K, h, a, b, c, d, e, f, g)
14908 long_termSTEP_A( 0); long_termSTEP_B( 1); long_termSTEP_C( 2); long_termSTEP_D( 3);
14909 long_termSTEP_E( 4); long_termSTEP_F( 5); long_termSTEP_G( 6); long_termSTEP_H( 7);
14911 long_termSTEP_A( 8); long_termSTEP_B( 9); long_termSTEP_C(10); long_termSTEP_D(11);
14912 long_termSTEP_E(12); long_termSTEP_F(13); long_termSTEP_G(14); long_termSTEP_H(15);
14914 long_termSTEP_A(16); long_termSTEP_B(17); long_termSTEP_C(18); long_termSTEP_D(19);
14915 long_termSTEP_E(20); long_termSTEP_F(21); long_termSTEP_G(22); long_termSTEP_H(23);
14917 long_termSTEP_A(24); long_termSTEP_B(25); long_termSTEP_C(26); long_termSTEP_D(27);
14918 long_termSTEP_E(28); long_termSTEP_F(29); long_termSTEP_G(30); long_termSTEP_H(31);
14920 long_termSTEP_A(32); long_termSTEP_B(33); long_termSTEP_C(34); long_termSTEP_D(35);
14921 long_termSTEP_E(36); long_termSTEP_F(37); long_termSTEP_G(38); long_termSTEP_H(39);
14923 if (S0 > L_max) { L_max = S0; Nc = lambda; }
14924 if (S1 > L_max) { L_max = S1; Nc = lambda + 1; }
14925 if (S2 > L_max) { L_max = S2; Nc = lambda + 2; }
14926 if (S3 > L_max) { L_max = S3; Nc = lambda + 3; }
14927 if (S4 > L_max) { L_max = S4; Nc = lambda + 4; }
14928 if (S5 > L_max) { L_max = S5; Nc = lambda + 5; }
14929 if (S6 > L_max) { L_max = S6; Nc = lambda + 6; }
14930 if (S7 > L_max) { L_max = S7; Nc = lambda + 7; }
14931 if (S8 > L_max) { L_max = S8; Nc = lambda + 8; }
14933 *Nc_out = Nc;
14935 L_max <<= 1;
14937 /* Rescaling of L_max
14939 assert(scal <= 100 && scal >= -100);
14940 L_max = L_max >> (6 - scal); /* sub(6, scal) */
14942 assert( Nc <= 120 && Nc >= 40);
14944 /* Compute the power of the reconstructed short term residual
14945 * signal dp[..]
14947 L_power = 0;
14948 for (k = 0; k <= 39; k++) {
14950 register longword L_temp;
14952 L_temp = SASR_W( dp[k - Nc], 3 );
14953 L_power += L_temp * L_temp;
14955 L_power <<= 1; /* from L_MULT */
14957 /* Normalization of L_max and L_power
14960 if (L_max <= 0) {
14961 *bc_out = 0;
14962 return;
14964 if (L_max >= L_power) {
14965 *bc_out = 3;
14966 return;
14969 temp = gsm_norm( L_power );
14971 R = SASR_L ( L_max << temp, 16 );
14972 S = SASR_L ( L_power << temp, 16 );
14974 /* Coding of the LTP gain
14977 /* Table 4.3a must be used to obtain the level DLB[i] for the
14978 * quantization of the LTP gain b to get the coded version bc.
14980 for (bc = 0; bc <= 2; bc++) if (R <= gsm_mult(S, gsm_DLB[bc])) break;
14981 *bc_out = bc;
14984 #ifdef FAST
14985 #ifdef LTP_CUT
14987 static void Cut_Fast_Calculation_of_the_LTP_parameters (
14988 struct gsm_state * st, /* IN */
14989 register word * d, /* [0..39] IN */
14990 register word * dp, /* [-120..-1] IN */
14991 word * bc_out, /* OUT */
14992 word * Nc_out /* OUT */
14995 register int k, lambda;
14996 register float wt_float;
14997 word Nc, bc;
14998 word wt_max, best_k, ltp_cut;
15000 float dp_float_base[120], * dp_float = dp_float_base + 120;
15002 register float L_result, L_max, L_power;
15004 wt_max = 0;
15006 for (k = 0; k < 40; ++k) {
15007 if ( d[k] > wt_max) wt_max = d[best_k = k];
15008 else if (-d[k] > wt_max) wt_max = -d[best_k = k];
15011 assert(wt_max >= 0);
15012 wt_float = (float)wt_max;
15014 for (k = -120; k < 0; ++k) dp_float[k] = (float)dp[k];
15016 /* Search for the maximum cross-correlation and coding of the LTP lag
15018 L_max = 0;
15019 Nc = 40; /* index for the maximum cross-correlation */
15021 for (lambda = 40; lambda <= 120; lambda++) {
15022 L_result = wt_float * dp_float[best_k - lambda];
15023 if (L_result > L_max) {
15024 Nc = lambda;
15025 L_max = L_result;
15029 *Nc_out = Nc;
15030 if (L_max <= 0.) {
15031 *bc_out = 0;
15032 return;
15035 /* Compute the power of the reconstructed short term residual
15036 * signal dp[..]
15038 dp_float -= Nc;
15039 L_power = 0;
15040 for (k = 0; k < 40; ++k) {
15041 register float f = dp_float[k];
15042 L_power += f * f;
15045 if (L_max >= L_power) {
15046 *bc_out = 3;
15047 return;
15050 /* Coding of the LTP gain
15051 * Table 4.3a must be used to obtain the level DLB[i] for the
15052 * quantization of the LTP gain b to get the coded version bc.
15054 lambda = L_max / L_power * 32768.;
15055 for (bc = 0; bc <= 2; ++bc) if (lambda <= gsm_DLB[bc]) break;
15056 *bc_out = bc;
15059 #endif /* LTP_CUT */
15061 static void Fast_Calculation_of_the_LTP_parameters (
15062 register word * din, /* [0..39] IN */
15063 register word * dp, /* [-120..-1] IN */
15064 word * bc_out, /* OUT */
15065 word * Nc_out /* OUT */
15068 register int k, lambda;
15069 word Nc, bc;
15071 float wt_float[40];
15072 float dp_float_base[120], * dp_float = dp_float_base + 120;
15074 register float L_max, L_power;
15076 for (k = 0; k < 40; ++k) wt_float[k] = (float) din [k] ;
15077 for (k = -120; k < 0; ++k) dp_float[k] = (float) dp [k] ;
15079 /* Search for the maximum cross-correlation and coding of the LTP lag
15081 L_max = 0;
15082 Nc = 40; /* index for the maximum cross-correlation */
15084 for (lambda = 40; lambda <= 120; lambda += 9) {
15086 /* Calculate L_result for l = lambda .. lambda + 9.
15088 register float *lp = dp_float - lambda;
15090 register float W;
15091 register float a = lp[-8], b = lp[-7], c = lp[-6],
15092 d = lp[-5], e = lp[-4], f = lp[-3],
15093 g = lp[-2], h = lp[-1];
15094 register float E;
15095 register float S0 = 0, S1 = 0, S2 = 0, S3 = 0, S4 = 0,
15096 S5 = 0, S6 = 0, S7 = 0, S8 = 0;
15098 # undef long_termSTEP
15099 # define long_termSTEP(K, a, b, c, d, e, f, g, h) \
15100 W = wt_float[K]; \
15101 E = W * a; S8 += E; \
15102 E = W * b; S7 += E; \
15103 E = W * c; S6 += E; \
15104 E = W * d; S5 += E; \
15105 E = W * e; S4 += E; \
15106 E = W * f; S3 += E; \
15107 E = W * g; S2 += E; \
15108 E = W * h; S1 += E; \
15109 a = lp[K]; \
15110 E = W * a; S0 += E
15112 # define long_termSTEP_A(K) long_termSTEP(K, a, b, c, d, e, f, g, h)
15113 # define long_termSTEP_B(K) long_termSTEP(K, b, c, d, e, f, g, h, a)
15114 # define long_termSTEP_C(K) long_termSTEP(K, c, d, e, f, g, h, a, b)
15115 # define long_termSTEP_D(K) long_termSTEP(K, d, e, f, g, h, a, b, c)
15116 # define long_termSTEP_E(K) long_termSTEP(K, e, f, g, h, a, b, c, d)
15117 # define long_termSTEP_F(K) long_termSTEP(K, f, g, h, a, b, c, d, e)
15118 # define long_termSTEP_G(K) long_termSTEP(K, g, h, a, b, c, d, e, f)
15119 # define long_termSTEP_H(K) long_termSTEP(K, h, a, b, c, d, e, f, g)
15121 long_termSTEP_A( 0); long_termSTEP_B( 1); long_termSTEP_C( 2); long_termSTEP_D( 3);
15122 long_termSTEP_E( 4); long_termSTEP_F( 5); long_termSTEP_G( 6); long_termSTEP_H( 7);
15124 long_termSTEP_A( 8); long_termSTEP_B( 9); long_termSTEP_C(10); long_termSTEP_D(11);
15125 long_termSTEP_E(12); long_termSTEP_F(13); long_termSTEP_G(14); long_termSTEP_H(15);
15127 long_termSTEP_A(16); long_termSTEP_B(17); long_termSTEP_C(18); long_termSTEP_D(19);
15128 long_termSTEP_E(20); long_termSTEP_F(21); long_termSTEP_G(22); long_termSTEP_H(23);
15130 long_termSTEP_A(24); long_termSTEP_B(25); long_termSTEP_C(26); long_termSTEP_D(27);
15131 long_termSTEP_E(28); long_termSTEP_F(29); long_termSTEP_G(30); long_termSTEP_H(31);
15133 long_termSTEP_A(32); long_termSTEP_B(33); long_termSTEP_C(34); long_termSTEP_D(35);
15134 long_termSTEP_E(36); long_termSTEP_F(37); long_termSTEP_G(38); long_termSTEP_H(39);
15136 if (S0 > L_max) { L_max = S0; Nc = lambda; }
15137 if (S1 > L_max) { L_max = S1; Nc = lambda + 1; }
15138 if (S2 > L_max) { L_max = S2; Nc = lambda + 2; }
15139 if (S3 > L_max) { L_max = S3; Nc = lambda + 3; }
15140 if (S4 > L_max) { L_max = S4; Nc = lambda + 4; }
15141 if (S5 > L_max) { L_max = S5; Nc = lambda + 5; }
15142 if (S6 > L_max) { L_max = S6; Nc = lambda + 6; }
15143 if (S7 > L_max) { L_max = S7; Nc = lambda + 7; }
15144 if (S8 > L_max) { L_max = S8; Nc = lambda + 8; }
15146 *Nc_out = Nc;
15148 if (L_max <= 0.) {
15149 *bc_out = 0;
15150 return;
15153 /* Compute the power of the reconstructed short term residual
15154 * signal dp[..]
15156 dp_float -= Nc;
15157 L_power = 0;
15158 for (k = 0; k < 40; ++k) {
15159 register float f = dp_float[k];
15160 L_power += f * f;
15163 if (L_max >= L_power) {
15164 *bc_out = 3;
15165 return;
15168 /* Coding of the LTP gain
15169 * Table 4.3a must be used to obtain the level DLB[i] for the
15170 * quantization of the LTP gain b to get the coded version bc.
15172 lambda = L_max / L_power * 32768.;
15173 for (bc = 0; bc <= 2; ++bc) if (lambda <= gsm_DLB[bc]) break;
15174 *bc_out = bc;
15177 #endif /* FAST */
15178 #endif /* USE_FLOAT_MUL */
15181 /* 4.2.12 */
15183 static void Long_term_analysis_filtering (
15184 word bc, /* IN */
15185 word Nc, /* IN */
15186 register word * dp, /* previous d [-120..-1] IN */
15187 register word * d, /* d [0..39] IN */
15188 register word * dpp, /* estimate [0..39] OUT */
15189 register word * e /* long term res. signal [0..39] OUT */
15192 * In this part, we have to decode the bc parameter to compute
15193 * the samples of the estimate dpp[0..39]. The decoding of bc needs the
15194 * use of table 4.3b. The long term residual signal e[0..39]
15195 * is then calculated to be fed to the RPE encoding section.
15198 register int k;
15200 # undef long_termSTEP
15201 # define long_termSTEP(BP) \
15202 for (k = 0; k <= 39; k++) { \
15203 dpp[k] = GSM_MULT_R( BP, dp[k - Nc]); \
15204 e[k] = GSM_SUB( d[k], dpp[k] ); \
15207 switch (bc) {
15208 case 0: long_termSTEP( 3277 ); break;
15209 case 1: long_termSTEP( 11469 ); break;
15210 case 2: long_termSTEP( 21299 ); break;
15211 case 3: long_termSTEP( 32767 ); break;
15215 void Gsm_Long_Term_Predictor ( /* 4x for 160 samples */
15217 struct gsm_state * S,
15219 word * d, /* [0..39] residual signal IN */
15220 word * dp, /* [-120..-1] d' IN */
15222 word * e, /* [0..39] OUT */
15223 word * dpp, /* [0..39] OUT */
15224 word * Nc, /* correlation lag OUT */
15225 word * bc /* gain factor OUT */
15228 assert( d ); assert( dp ); assert( e );
15229 assert( dpp); assert( Nc ); assert( bc );
15231 #if defined(FAST) && defined(USE_FLOAT_MUL)
15232 if (S->fast)
15233 #if defined (LTP_CUT)
15234 if (S->ltp_cut)
15235 Cut_Fast_Calculation_of_the_LTP_parameters(S,
15236 d, dp, bc, Nc);
15237 else
15238 #endif /* LTP_CUT */
15239 Fast_Calculation_of_the_LTP_parameters(d, dp, bc, Nc );
15240 else
15241 #endif /* FAST & USE_FLOAT_MUL */
15242 #ifdef LTP_CUT
15243 if (S->ltp_cut)
15244 Cut_Calculation_of_the_LTP_parameters(S, d, dp, bc, Nc);
15245 else
15246 #endif
15247 Calculation_of_the_LTP_parameters(d, dp, bc, Nc);
15249 Long_term_analysis_filtering( *bc, *Nc, dp, d, dpp, e );
15252 /* 4.3.2 */
15253 void Gsm_Long_Term_Synthesis_Filtering (
15254 struct gsm_state * S,
15256 word Ncr,
15257 word bcr,
15258 register word * erp, /* [0..39] IN */
15259 register word * drp /* [-120..-1] IN, [-120..40] OUT */
15262 * This procedure uses the bcr and Ncr parameter to realize the
15263 * long term synthesis filtering. The decoding of bcr needs
15264 * table 4.3b.
15267 register int k;
15268 word brp, drpp, Nr;
15270 /* Check the limits of Nr.
15272 Nr = Ncr < 40 || Ncr > 120 ? S->nrp : Ncr;
15273 S->nrp = Nr;
15274 assert(Nr >= 40 && Nr <= 120);
15276 /* Decoding of the LTP gain bcr
15278 brp = gsm_QLB[ bcr ];
15280 /* Computation of the reconstructed short term residual
15281 * signal drp[0..39]
15283 assert(brp != MIN_WORD);
15285 for (k = 0; k <= 39; k++) {
15286 drpp = GSM_MULT_R( brp, drp[ k - Nr ] );
15287 drp[k] = GSM_ADD( erp[k], drpp );
15291 * Update of the reconstructed short term residual signal
15292 * drp[ -1..-120 ]
15295 for (k = 0; k <= 119; k++) drp[ -120 + k ] = drp[ -80 + k ];
15298 ** Do not edit or modify anything in this comment block.
15299 ** The arch-tag line is a file identity tag for the GNU Arch
15300 ** revision control system.
15302 ** arch-tag: b369b90d-0284-42a0-87b0-99a25bbd93ac
15306 * Copyright 1992 by Jutta Degener and Carsten Bormann, Technische
15307 * Universitaet Berlin. See the accompanying file "COPYRIGHT" for
15308 * details. THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE.
15311 #include <stdio.h>
15312 #include <assert.h>
15317 * 4.2.4 .. 4.2.7 LPC ANALYSIS SECTION
15320 /* 4.2.4 */
15323 static void Autocorrelation (
15324 word * s, /* [0..159] IN/OUT */
15325 longword * L_ACF) /* [0..8] OUT */
15327 * The goal is to compute the array L_ACF[k]. The signal s[i] must
15328 * be scaled in order to avoid an overflow situation.
15331 register int k, i;
15333 word temp, smax, scalauto;
15335 #ifdef USE_FLOAT_MUL
15336 float float_s[160];
15337 #endif
15339 /* Dynamic scaling of the array s[0..159]
15342 /* Search for the maximum.
15344 smax = 0;
15345 for (k = 0; k <= 159; k++) {
15346 temp = GSM_ABS( s[k] );
15347 if (temp > smax) smax = temp;
15350 /* Computation of the scaling factor.
15352 if (smax == 0) scalauto = 0;
15353 else {
15354 assert(smax > 0);
15355 scalauto = 4 - gsm_norm( (longword)smax << 16 );/* sub(4,..) */
15358 /* Scaling of the array s[0...159]
15361 if (scalauto > 0) {
15363 # ifdef USE_FLOAT_MUL
15364 # define SCALE(n) \
15365 case n: for (k = 0; k <= 159; k++) \
15366 float_s[k] = (float) \
15367 (s[k] = GSM_MULT_R(s[k], 16384 >> (n-1)));\
15368 break;
15369 # else
15370 # define SCALE(n) \
15371 case n: for (k = 0; k <= 159; k++) \
15372 s[k] = GSM_MULT_R( s[k], 16384 >> (n-1) );\
15373 break;
15374 # endif /* USE_FLOAT_MUL */
15376 switch (scalauto) {
15377 SCALE(1)
15378 SCALE(2)
15379 SCALE(3)
15380 SCALE(4)
15382 # undef SCALE
15384 # ifdef USE_FLOAT_MUL
15385 else for (k = 0; k <= 159; k++) float_s[k] = (float) s[k];
15386 # endif
15388 /* Compute the L_ACF[..].
15391 # ifdef USE_FLOAT_MUL
15392 register float * sp = float_s;
15393 register float sl = *sp;
15395 # define lpcSTEP(k) L_ACF[k] += (longword)(sl * sp[ -(k) ]);
15396 # else
15397 word * sp = s;
15398 word sl = *sp;
15400 # define lpcSTEP(k) L_ACF[k] += ((longword)sl * sp[ -(k) ]);
15401 # endif
15403 # define NEXTI sl = *++sp
15406 for (k = 9; k--; L_ACF[k] = 0) ;
15408 lpcSTEP (0);
15409 NEXTI;
15410 lpcSTEP(0); lpcSTEP(1);
15411 NEXTI;
15412 lpcSTEP(0); lpcSTEP(1); lpcSTEP(2);
15413 NEXTI;
15414 lpcSTEP(0); lpcSTEP(1); lpcSTEP(2); lpcSTEP(3);
15415 NEXTI;
15416 lpcSTEP(0); lpcSTEP(1); lpcSTEP(2); lpcSTEP(3); lpcSTEP(4);
15417 NEXTI;
15418 lpcSTEP(0); lpcSTEP(1); lpcSTEP(2); lpcSTEP(3); lpcSTEP(4); lpcSTEP(5);
15419 NEXTI;
15420 lpcSTEP(0); lpcSTEP(1); lpcSTEP(2); lpcSTEP(3); lpcSTEP(4); lpcSTEP(5); lpcSTEP(6);
15421 NEXTI;
15422 lpcSTEP(0); lpcSTEP(1); lpcSTEP(2); lpcSTEP(3); lpcSTEP(4); lpcSTEP(5); lpcSTEP(6); lpcSTEP(7);
15424 for (i = 8; i <= 159; i++) {
15426 NEXTI;
15428 lpcSTEP(0);
15429 lpcSTEP(1); lpcSTEP(2); lpcSTEP(3); lpcSTEP(4);
15430 lpcSTEP(5); lpcSTEP(6); lpcSTEP(7); lpcSTEP(8);
15433 for (k = 9; k--; L_ACF[k] <<= 1) ;
15436 /* Rescaling of the array s[0..159]
15438 if (scalauto > 0) {
15439 assert(scalauto <= 4);
15440 for (k = 160; k--; *s++ <<= scalauto) ;
15444 #if defined(USE_FLOAT_MUL) && defined(FAST)
15446 static void Fast_Autocorrelation (
15447 word * s, /* [0..159] IN/OUT */
15448 longword * L_ACF) /* [0..8] OUT */
15450 register int k, i;
15451 float f_L_ACF[9];
15452 float scale;
15454 float s_f[160];
15455 register float *sf = s_f;
15457 for (i = 0; i < 160; ++i) sf[i] = s[i];
15458 for (k = 0; k <= 8; k++) {
15459 register float L_temp2 = 0;
15460 register float *sfl = sf - k;
15461 for (i = k; i < 160; ++i) L_temp2 += sf[i] * sfl[i];
15462 f_L_ACF[k] = L_temp2;
15464 scale = MAX_LONGWORD / f_L_ACF[0];
15466 for (k = 0; k <= 8; k++) {
15467 L_ACF[k] = f_L_ACF[k] * scale;
15470 #endif /* defined (USE_FLOAT_MUL) && defined (FAST) */
15472 /* 4.2.5 */
15474 static void Reflection_coefficients (
15475 longword * L_ACF, /* 0...8 IN */
15476 register word * r /* 0...7 OUT */
15479 register int i, m, n;
15480 register word temp;
15481 word ACF[9]; /* 0..8 */
15482 word P[ 9]; /* 0..8 */
15483 word K[ 9]; /* 2..8 */
15485 /* Schur recursion with 16 bits arithmetic.
15488 if (L_ACF[0] == 0) {
15489 for (i = 8; i--; *r++ = 0) ;
15490 return;
15493 assert( L_ACF[0] != 0 );
15494 temp = gsm_norm( L_ACF[0] );
15496 assert(temp >= 0 && temp < 32);
15498 /* ? overflow ? */
15499 for (i = 0; i <= 8; i++) ACF[i] = SASR_L( L_ACF[i] << temp, 16 );
15501 /* Initialize array P[..] and K[..] for the recursion.
15504 for (i = 1; i <= 7; i++) K[ i ] = ACF[ i ];
15505 for (i = 0; i <= 8; i++) P[ i ] = ACF[ i ];
15507 /* Compute reflection coefficients
15509 for (n = 1; n <= 8; n++, r++) {
15511 temp = P[1];
15512 temp = GSM_ABS(temp);
15513 if (P[0] < temp) {
15514 for (i = n; i <= 8; i++) *r++ = 0;
15515 return;
15518 *r = gsm_div( temp, P[0] );
15520 assert(*r >= 0);
15521 if (P[1] > 0) *r = -*r; /* r[n] = sub(0, r[n]) */
15522 assert (*r != MIN_WORD);
15523 if (n == 8) return;
15525 /* Schur recursion
15527 temp = GSM_MULT_R( P[1], *r );
15528 P[0] = GSM_ADD( P[0], temp );
15530 for (m = 1; m <= 8 - n; m++) {
15531 temp = GSM_MULT_R( K[ m ], *r );
15532 P[m] = GSM_ADD( P[ m+1 ], temp );
15534 temp = GSM_MULT_R( P[ m+1 ], *r );
15535 K[m] = GSM_ADD( K[ m ], temp );
15540 /* 4.2.6 */
15542 static void Transformation_to_Log_Area_Ratios (
15543 register word * r /* 0..7 IN/OUT */
15546 * The following scaling for r[..] and LAR[..] has been used:
15548 * r[..] = integer( real_r[..]*32768. ); -1 <= real_r < 1.
15549 * LAR[..] = integer( real_LAR[..] * 16384 );
15550 * with -1.625 <= real_LAR <= 1.625
15553 register word temp;
15554 register int i;
15557 /* Computation of the LAR[0..7] from the r[0..7]
15559 for (i = 1; i <= 8; i++, r++) {
15561 temp = *r;
15562 temp = GSM_ABS(temp);
15563 assert(temp >= 0);
15565 if (temp < 22118) {
15566 temp >>= 1;
15567 } else if (temp < 31130) {
15568 assert( temp >= 11059 );
15569 temp -= 11059;
15570 } else {
15571 assert( temp >= 26112 );
15572 temp -= 26112;
15573 temp <<= 2;
15576 *r = *r < 0 ? -temp : temp;
15577 assert( *r != MIN_WORD );
15581 /* 4.2.7 */
15583 static void Quantization_and_coding (
15584 register word * LAR /* [0..7] IN/OUT */
15587 register word temp;
15589 /* This procedure needs four tables; the following equations
15590 * give the optimum scaling for the constants:
15592 * A[0..7] = integer( real_A[0..7] * 1024 )
15593 * B[0..7] = integer( real_B[0..7] * 512 )
15594 * MAC[0..7] = maximum of the LARc[0..7]
15595 * MIC[0..7] = minimum of the LARc[0..7]
15598 # undef lpcSTEP
15599 # define lpcSTEP( A, B, MAC, MIC ) \
15600 temp = GSM_MULT( A, *LAR ); \
15601 temp = GSM_ADD( temp, B ); \
15602 temp = GSM_ADD( temp, 256 ); \
15603 temp = SASR_W( temp, 9 ); \
15604 *LAR = temp>MAC ? MAC - MIC : (temp<MIC ? 0 : temp - MIC); \
15605 LAR++;
15607 lpcSTEP( 20480, 0, 31, -32 );
15608 lpcSTEP( 20480, 0, 31, -32 );
15609 lpcSTEP( 20480, 2048, 15, -16 );
15610 lpcSTEP( 20480, -2560, 15, -16 );
15612 lpcSTEP( 13964, 94, 7, -8 );
15613 lpcSTEP( 15360, -1792, 7, -8 );
15614 lpcSTEP( 8534, -341, 3, -4 );
15615 lpcSTEP( 9036, -1144, 3, -4 );
15617 # undef lpcSTEP
15620 void Gsm_LPC_Analysis (
15621 struct gsm_state *S,
15622 word * s, /* 0..159 signals IN/OUT */
15623 word * LARc) /* 0..7 LARc's OUT */
15625 longword L_ACF[9];
15627 #if defined(USE_FLOAT_MUL) && defined(FAST)
15628 if (S->fast) Fast_Autocorrelation (s, L_ACF );
15629 else
15630 #endif
15631 Autocorrelation (s, L_ACF );
15632 Reflection_coefficients (L_ACF, LARc );
15633 Transformation_to_Log_Area_Ratios (LARc);
15634 Quantization_and_coding (LARc);
15637 ** Do not edit or modify anything in this comment block.
15638 ** The arch-tag line is a file identity tag for the GNU Arch
15639 ** revision control system.
15641 ** arch-tag: 63146664-a002-4e1e-8b7b-f0cc8a6a53da
15645 ** Copyright (C) 2003,2004 Erik de Castro Lopo <erikd@mega-nerd.com>
15647 ** This program is free software; you can redistribute it and/or modify
15648 ** it under the terms of the GNU Lesser General Public License as published by
15649 ** the Free Software Foundation; either version 2.1 of the License, or
15650 ** (at your option) any later version.
15652 ** This program is distributed in the hope that it will be useful,
15653 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15654 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15655 ** GNU Lesser General Public License for more details.
15657 ** You should have received a copy of the GNU Lesser General Public License
15658 ** along with this program; if not, write to the Free Software
15659 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15663 #include <stdlib.h>
15664 #include <string.h>
15667 #if (OS_IS_MACOSX == 1)
15671 macbinary3_open (SF_PRIVATE *psf)
15673 if (psf)
15674 return 0 ;
15676 return 0 ;
15677 } /* macbinary3_open */
15679 #else
15682 macbinary3_open (SF_PRIVATE *psf)
15684 psf = psf ;
15685 return 0 ;
15686 } /* macbinary3_open */
15688 #endif /* OS_IS_MACOSX */
15691 ** Do not edit or modify anything in this comment block.
15692 ** The arch-tag line is a file identity tag for the GNU Arch
15693 ** revision control system.
15695 ** arch-tag: c397a7d7-1a31-4349-9684-bd29ef06211e
15698 ** Copyright (C) 2003,2004 Erik de Castro Lopo <erikd@mega-nerd.com>
15700 ** This program is free software; you can redistribute it and/or modify
15701 ** it under the terms of the GNU Lesser General Public License as published by
15702 ** the Free Software Foundation; either version 2.1 of the License, or
15703 ** (at your option) any later version.
15705 ** This program is distributed in the hope that it will be useful,
15706 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15707 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15708 ** GNU Lesser General Public License for more details.
15710 ** You should have received a copy of the GNU Lesser General Public License
15711 ** along with this program; if not, write to the Free Software
15712 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15716 #include <stdlib.h>
15717 #include <string.h>
15718 #include <sys/stat.h>
15721 #define STR_MARKER MAKE_MARKER ('S', 'T', 'R', ' ')
15724 macos_guess_file_type (SF_PRIVATE *psf, const char *filename)
15725 { static char rsrc_name [1024] ;
15726 struct stat statbuf ;
15727 int format ;
15729 psf = psf ;
15731 snprintf (rsrc_name, sizeof (rsrc_name), "%s/rsrc", filename);
15733 /* If there is no resource fork, just return. */
15734 if (stat (rsrc_name, &statbuf) != 0)
15735 { psf_log_printf (psf, "No resource fork.\n") ;
15736 return 0 ;
15739 if (statbuf.st_size == 0)
15740 { psf_log_printf (psf, "Have zero size resource fork.\n") ;
15741 return 0 ;
15744 format = 0 ;
15746 return format ;
15747 } /* macos_guess_file_type */
15750 ** Do not edit or modify anything in this comment block.
15751 ** The arch-tag line is a file identity tag for the GNU Arch
15752 ** revision control system.
15754 ** arch-tag: 5fbf66d7-9547-442a-9c73-92fd164f3a95
15757 ** Copyright (C) 2002-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
15759 ** This program is free software; you can redistribute it and/or modify
15760 ** it under the terms of the GNU Lesser General Public License as published by
15761 ** the Free Software Foundation; either version 2.1 of the License, or
15762 ** (at your option) any later version.
15764 ** This program is distributed in the hope that it will be useful,
15765 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15766 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15767 ** GNU Lesser General Public License for more details.
15769 ** You should have received a copy of the GNU Lesser General Public License
15770 ** along with this program; if not, write to the Free Software
15771 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15774 #include <stdio.h>
15775 #include <fcntl.h>
15776 #include <string.h>
15777 #include <ctype.h>
15780 /*------------------------------------------------------------------------------
15781 ** Information on how to decode and encode this file was obtained in a PDF
15782 ** file which I found on http://www.wotsit.org/.
15783 ** Also did a lot of testing with GNU Octave but do not have access to
15784 ** Matlab (tm) and so could not test it there.
15787 /*------------------------------------------------------------------------------
15788 ** Macros to handle big/little endian issues.
15791 #define MAT4_BE_DOUBLE (MAKE_MARKER (0, 0, 0x03, 0xE8))
15792 #define MAT4_LE_DOUBLE (MAKE_MARKER (0, 0, 0, 0))
15794 #define MAT4_BE_FLOAT (MAKE_MARKER (0, 0, 0x03, 0xF2))
15795 #define MAT4_LE_FLOAT (MAKE_MARKER (0x0A, 0, 0, 0))
15797 #define MAT4_BE_PCM_32 (MAKE_MARKER (0, 0, 0x03, 0xFC))
15798 #define MAT4_LE_PCM_32 (MAKE_MARKER (0x14, 0, 0, 0))
15800 #define MAT4_BE_PCM_16 (MAKE_MARKER (0, 0, 0x04, 0x06))
15801 #define MAT4_LE_PCM_16 (MAKE_MARKER (0x1E, 0, 0, 0))
15803 /* Can't see any reason to ever implement this. */
15804 #define MAT4_BE_PCM_U8 (MAKE_MARKER (0, 0, 0x04, 0x1A))
15805 #define MAT4_LE_PCM_U8 (MAKE_MARKER (0x32, 0, 0, 0))
15807 /*------------------------------------------------------------------------------
15808 ** Private static functions.
15811 static int mat4_close (SF_PRIVATE *psf) ;
15813 static int mat4_format_to_encoding (int format, int endian) ;
15815 static int mat4_write_header (SF_PRIVATE *psf, int calc_length) ;
15816 static int mat4_read_header (SF_PRIVATE *psf) ;
15818 static const char * mat4_marker_to_str (int marker) ;
15820 /*------------------------------------------------------------------------------
15821 ** Public function.
15825 mat4_open (SF_PRIVATE *psf)
15826 { int subformat, error = 0 ;
15828 if (psf->mode == SFM_READ || (psf->mode == SFM_RDWR && psf->filelength > 0))
15829 { if ((error = mat4_read_header (psf)))
15830 return error ;
15833 if ((psf->sf.format & SF_FORMAT_TYPEMASK) != SF_FORMAT_MAT4)
15834 return SFE_BAD_OPEN_FORMAT ;
15836 subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
15838 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
15839 { if (psf->is_pipe)
15840 return SFE_NO_PIPE_WRITE ;
15842 psf->endian = psf->sf.format & SF_FORMAT_ENDMASK ;
15843 if (CPU_IS_LITTLE_ENDIAN && (psf->endian == SF_ENDIAN_CPU || psf->endian == 0))
15844 psf->endian = SF_ENDIAN_LITTLE ;
15845 else if (CPU_IS_BIG_ENDIAN && (psf->endian == SF_ENDIAN_CPU || psf->endian == 0))
15846 psf->endian = SF_ENDIAN_BIG ;
15848 if ((error = mat4_write_header (psf, SF_FALSE)))
15849 return error ;
15851 psf->write_header = mat4_write_header ;
15854 psf->close = mat4_close ;
15856 psf->blockwidth = psf->bytewidth * psf->sf.channels ;
15858 switch (subformat)
15859 { case SF_FORMAT_PCM_16 :
15860 case SF_FORMAT_PCM_32 :
15861 error = pcm_init (psf) ;
15862 break ;
15864 case SF_FORMAT_FLOAT :
15865 error = float32_init (psf) ;
15866 break ;
15868 case SF_FORMAT_DOUBLE :
15869 error = double64_init (psf) ;
15870 break ;
15872 default : break ;
15875 if (error)
15876 return error ;
15878 return error ;
15879 } /* mat4_open */
15881 /*------------------------------------------------------------------------------
15884 static int
15885 mat4_close (SF_PRIVATE *psf)
15887 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
15888 mat4_write_header (psf, SF_TRUE) ;
15890 return 0 ;
15891 } /* mat4_close */
15893 /*------------------------------------------------------------------------------
15896 static int
15897 mat4_write_header (SF_PRIVATE *psf, int calc_length)
15898 { sf_count_t current ;
15899 int encoding ;
15900 double samplerate ;
15902 current = psf_ftell (psf) ;
15904 if (calc_length)
15905 { psf->filelength = psf_get_filelen (psf) ;
15907 psf->datalength = psf->filelength - psf->dataoffset ;
15908 if (psf->dataend)
15909 psf->datalength -= psf->filelength - psf->dataend ;
15911 psf->sf.frames = psf->datalength / (psf->bytewidth * psf->sf.channels) ;
15914 encoding = mat4_format_to_encoding (psf->sf.format & SF_FORMAT_SUBMASK, psf->endian) ;
15916 if (encoding == -1)
15917 return SFE_BAD_OPEN_FORMAT ;
15919 /* Reset the current header length to zero. */
15920 psf->header [0] = 0 ;
15921 psf->headindex = 0 ;
15922 psf_fseek (psf, 0, SEEK_SET) ;
15924 /* Need sample rate as a double for writing to the header. */
15925 samplerate = psf->sf.samplerate ;
15927 if (psf->endian == SF_ENDIAN_BIG)
15928 { psf_binheader_writef (psf, "Em444", MAT4_BE_DOUBLE, 1, 1, 0) ;
15929 psf_binheader_writef (psf, "E4bd", 11, "samplerate", 11, samplerate) ;
15930 psf_binheader_writef (psf, "tEm484", encoding, psf->sf.channels, psf->sf.frames, 0) ;
15931 psf_binheader_writef (psf, "E4b", 9, "wavedata", 9) ;
15933 else if (psf->endian == SF_ENDIAN_LITTLE)
15934 { psf_binheader_writef (psf, "em444", MAT4_LE_DOUBLE, 1, 1, 0) ;
15935 psf_binheader_writef (psf, "e4bd", 11, "samplerate", 11, samplerate) ;
15936 psf_binheader_writef (psf, "tem484", encoding, psf->sf.channels, psf->sf.frames, 0) ;
15937 psf_binheader_writef (psf, "e4b", 9, "wavedata", 9) ;
15939 else
15940 return SFE_BAD_OPEN_FORMAT ;
15942 /* Header construction complete so write it out. */
15943 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
15945 if (psf->error)
15946 return psf->error ;
15948 psf->dataoffset = psf->headindex ;
15950 if (current > 0)
15951 psf_fseek (psf, current, SEEK_SET) ;
15953 return psf->error ;
15954 } /* mat4_write_header */
15956 static int
15957 mat4_read_header (SF_PRIVATE *psf)
15958 { int marker, namesize, rows, cols, imag ;
15959 double value ;
15960 const char *marker_str ;
15961 char name [64] ;
15963 psf_binheader_readf (psf, "pm", 0, &marker) ;
15965 /* MAT4 file must start with a double for the samplerate. */
15966 if (marker == MAT4_BE_DOUBLE)
15967 { psf->endian = psf->rwf_endian = SF_ENDIAN_BIG ;
15968 marker_str = "big endian double" ;
15970 else if (marker == MAT4_LE_DOUBLE)
15971 { psf->endian = psf->rwf_endian = SF_ENDIAN_LITTLE ;
15972 marker_str = "little endian double" ;
15974 else
15975 return SFE_UNIMPLEMENTED ;
15977 psf_log_printf (psf, "GNU Octave 2.0 / MATLAB v4.2 format\nMarker : %s\n", marker_str) ;
15979 psf_binheader_readf (psf, "444", &rows, &cols, &imag) ;
15981 psf_log_printf (psf, " Rows : %d\n Cols : %d\n Imag : %s\n", rows, cols, imag ? "True" : "False") ;
15983 psf_binheader_readf (psf, "4", &namesize) ;
15985 if (namesize >= SIGNED_SIZEOF (name))
15986 return SFE_MAT4_BAD_NAME ;
15988 psf_binheader_readf (psf, "b", name, namesize) ;
15989 name [namesize] = 0 ;
15991 psf_log_printf (psf, " Name : %s\n", name) ;
15993 psf_binheader_readf (psf, "d", &value) ;
15995 LSF_SNPRINTF ((char*) psf->buffer, sizeof (psf->buffer), " Value : %f\n", value) ;
15996 psf_log_printf (psf, (char*) psf->buffer) ;
15998 if ((rows != 1) || (cols != 1))
15999 return SFE_MAT4_NO_SAMPLERATE ;
16001 psf->sf.samplerate = (int) value ;
16003 /* Now write out the audio data. */
16005 psf_binheader_readf (psf, "m", &marker) ;
16007 psf_log_printf (psf, "Marker : %s\n", mat4_marker_to_str (marker)) ;
16009 psf_binheader_readf (psf, "444", &rows, &cols, &imag) ;
16011 psf_log_printf (psf, " Rows : %d\n Cols : %d\n Imag : %s\n", rows, cols, imag ? "True" : "False") ;
16013 psf_binheader_readf (psf, "4", &namesize) ;
16015 if (namesize >= SIGNED_SIZEOF (name))
16016 return SFE_MAT4_BAD_NAME ;
16018 psf_binheader_readf (psf, "b", name, namesize) ;
16019 name [namesize] = 0 ;
16021 psf_log_printf (psf, " Name : %s\n", name) ;
16023 psf->dataoffset = psf_ftell (psf) ;
16025 if (rows == 0 && cols == 0)
16026 { psf_log_printf (psf, "*** Error : zero channel count.\n") ;
16027 return SFE_MAT4_ZERO_CHANNELS ;
16030 psf->sf.channels = rows ;
16031 psf->sf.frames = cols ;
16033 psf->sf.format = psf->endian | SF_FORMAT_MAT4 ;
16034 switch (marker)
16035 { case MAT4_BE_DOUBLE :
16036 case MAT4_LE_DOUBLE :
16037 psf->sf.format |= SF_FORMAT_DOUBLE ;
16038 psf->bytewidth = 8 ;
16039 break ;
16041 case MAT4_BE_FLOAT :
16042 case MAT4_LE_FLOAT :
16043 psf->sf.format |= SF_FORMAT_FLOAT ;
16044 psf->bytewidth = 4 ;
16045 break ;
16047 case MAT4_BE_PCM_32 :
16048 case MAT4_LE_PCM_32 :
16049 psf->sf.format |= SF_FORMAT_PCM_32 ;
16050 psf->bytewidth = 4 ;
16051 break ;
16053 case MAT4_BE_PCM_16 :
16054 case MAT4_LE_PCM_16 :
16055 psf->sf.format |= SF_FORMAT_PCM_16 ;
16056 psf->bytewidth = 2 ;
16057 break ;
16059 default :
16060 psf_log_printf (psf, "*** Error : Bad marker %08X\n", marker) ;
16061 return SFE_UNIMPLEMENTED ;
16064 if ((psf->filelength - psf->dataoffset) < psf->sf.channels * psf->sf.frames * psf->bytewidth)
16065 { psf_log_printf (psf, "*** File seems to be truncated. %D <--> %D\n",
16066 psf->filelength - psf->dataoffset, psf->sf.channels * psf->sf.frames * psf->bytewidth) ;
16068 else if ((psf->filelength - psf->dataoffset) > psf->sf.channels * psf->sf.frames * psf->bytewidth)
16069 psf->dataend = psf->dataoffset + rows * cols * psf->bytewidth ;
16071 psf->datalength = psf->filelength - psf->dataoffset - psf->dataend ;
16073 psf->sf.sections = 1 ;
16075 return 0 ;
16076 } /* mat4_read_header */
16078 static int
16079 mat4_format_to_encoding (int format, int endian)
16081 switch (format | endian)
16082 { case (SF_FORMAT_PCM_16 | SF_ENDIAN_BIG) :
16083 return MAT4_BE_PCM_16 ;
16085 case (SF_FORMAT_PCM_16 | SF_ENDIAN_LITTLE) :
16086 return MAT4_LE_PCM_16 ;
16088 case (SF_FORMAT_PCM_32 | SF_ENDIAN_BIG) :
16089 return MAT4_BE_PCM_32 ;
16091 case (SF_FORMAT_PCM_32 | SF_ENDIAN_LITTLE) :
16092 return MAT4_LE_PCM_32 ;
16094 case (SF_FORMAT_FLOAT | SF_ENDIAN_BIG) :
16095 return MAT4_BE_FLOAT ;
16097 case (SF_FORMAT_FLOAT | SF_ENDIAN_LITTLE) :
16098 return MAT4_LE_FLOAT ;
16100 case (SF_FORMAT_DOUBLE | SF_ENDIAN_BIG) :
16101 return MAT4_BE_DOUBLE ;
16103 case (SF_FORMAT_DOUBLE | SF_ENDIAN_LITTLE) :
16104 return MAT4_LE_DOUBLE ;
16106 default : break ;
16109 return -1 ;
16110 } /* mat4_format_to_encoding */
16112 static const char *
16113 mat4_marker_to_str (int marker)
16114 { static char str [32] ;
16116 switch (marker)
16118 case MAT4_BE_PCM_16 : return "big endian 16 bit PCM" ;
16119 case MAT4_LE_PCM_16 : return "little endian 16 bit PCM" ;
16121 case MAT4_BE_PCM_32 : return "big endian 32 bit PCM" ;
16122 case MAT4_LE_PCM_32 : return "little endian 32 bit PCM" ;
16125 case MAT4_BE_FLOAT : return "big endian float" ;
16126 case MAT4_LE_FLOAT : return "big endian float" ;
16128 case MAT4_BE_DOUBLE : return "big endian double" ;
16129 case MAT4_LE_DOUBLE : return "little endian double" ;
16132 /* This is a little unsafe but is really only for debugging. */
16133 str [sizeof (str) - 1] = 0 ;
16134 LSF_SNPRINTF (str, sizeof (str) - 1, "%08X", marker) ;
16135 return str ;
16136 } /* mat4_marker_to_str */
16138 ** Do not edit or modify anything in this comment block.
16139 ** The arch-tag line is a file identity tag for the GNU Arch
16140 ** revision control system.
16142 ** arch-tag: f7e5f5d6-fc39-452e-bc4a-59627116ff59
16145 ** Copyright (C) 2002-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
16147 ** This program is free software; you can redistribute it and/or modify
16148 ** it under the terms of the GNU Lesser General Public License as published by
16149 ** the Free Software Foundation; either version 2.1 of the License, or
16150 ** (at your option) any later version.
16152 ** This program is distributed in the hope that it will be useful,
16153 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16154 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16155 ** GNU Lesser General Public License for more details.
16157 ** You should have received a copy of the GNU Lesser General Public License
16158 ** along with this program; if not, write to the Free Software
16159 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16162 #include <stdio.h>
16163 #include <fcntl.h>
16164 #include <string.h>
16165 #include <ctype.h>
16168 /*------------------------------------------------------------------------------
16169 ** Information on how to decode and encode this file was obtained in a PDF
16170 ** file which I found on http://www.wotsit.org/.
16171 ** Also did a lot of testing with GNU Octave but do not have access to
16172 ** Matlab (tm) and so could not test it there.
16175 /*------------------------------------------------------------------------------
16176 ** Macros to handle big/little endian issues.
16179 #define MATL_MARKER (MAKE_MARKER ('M', 'A', 'T', 'L'))
16181 #define IM_MARKER (('I' << 8) + 'M')
16182 #define MI_MARKER (('M' << 8) + 'I')
16184 /*------------------------------------------------------------------------------
16185 ** Enums and typedefs.
16188 enum
16189 { MAT5_TYPE_SCHAR = 0x1,
16190 MAT5_TYPE_UCHAR = 0x2,
16191 MAT5_TYPE_INT16 = 0x3,
16192 MAT5_TYPE_UINT16 = 0x4,
16193 MAT5_TYPE_INT32 = 0x5,
16194 MAT5_TYPE_UINT32 = 0x6,
16195 MAT5_TYPE_FLOAT = 0x7,
16196 MAT5_TYPE_DOUBLE = 0x9,
16197 MAT5_TYPE_ARRAY = 0xE,
16199 MAT5_TYPE_COMP_USHORT = 0x00020004,
16200 MAT5_TYPE_COMP_UINT = 0x00040006
16203 typedef struct
16204 { sf_count_t size ;
16205 int rows, cols ;
16206 char name [32] ;
16207 } MAT5_MATRIX ;
16209 /*------------------------------------------------------------------------------
16210 ** Private static functions.
16213 static int mat5_close (SF_PRIVATE *psf) ;
16215 static int mat5_write_header (SF_PRIVATE *psf, int calc_length) ;
16216 static int mat5_read_header (SF_PRIVATE *psf) ;
16218 /*------------------------------------------------------------------------------
16219 ** Public function.
16223 mat5_open (SF_PRIVATE *psf)
16224 { int subformat, error = 0 ;
16226 if (psf->mode == SFM_READ || (psf->mode == SFM_RDWR && psf->filelength > 0))
16227 { if ((error = mat5_read_header (psf)))
16228 return error ;
16231 if ((psf->sf.format & SF_FORMAT_TYPEMASK) != SF_FORMAT_MAT5)
16232 return SFE_BAD_OPEN_FORMAT ;
16234 subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
16236 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
16237 { if (psf->is_pipe)
16238 return SFE_NO_PIPE_WRITE ;
16240 psf->endian = psf->sf.format & SF_FORMAT_ENDMASK ;
16241 if (CPU_IS_LITTLE_ENDIAN && (psf->endian == SF_ENDIAN_CPU || psf->endian == 0))
16242 psf->endian = SF_ENDIAN_LITTLE ;
16243 else if (CPU_IS_BIG_ENDIAN && (psf->endian == SF_ENDIAN_CPU || psf->endian == 0))
16244 psf->endian = SF_ENDIAN_BIG ;
16246 if ((error = mat5_write_header (psf, SF_FALSE)))
16247 return error ;
16249 psf->write_header = mat5_write_header ;
16252 psf->close = mat5_close ;
16254 psf->blockwidth = psf->bytewidth * psf->sf.channels ;
16256 switch (subformat)
16257 { case SF_FORMAT_PCM_U8 :
16258 case SF_FORMAT_PCM_16 :
16259 case SF_FORMAT_PCM_32 :
16260 error = pcm_init (psf) ;
16261 break ;
16263 case SF_FORMAT_FLOAT :
16264 error = float32_init (psf) ;
16265 break ;
16267 case SF_FORMAT_DOUBLE :
16268 error = double64_init (psf) ;
16269 break ;
16271 default : break ;
16274 return error ;
16275 } /* mat5_open */
16277 /*------------------------------------------------------------------------------
16280 static int
16281 mat5_close (SF_PRIVATE *psf)
16283 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
16284 mat5_write_header (psf, SF_TRUE) ;
16286 return 0 ;
16287 } /* mat5_close */
16289 /*------------------------------------------------------------------------------
16292 static int
16293 mat5_write_header (SF_PRIVATE *psf, int calc_length)
16294 { static const char *sr_name = "samplerate\0\0\0\0\0\0\0\0\0\0\0" ;
16295 static const char *wd_name = "wavedata\0" ;
16296 sf_count_t current, datasize ;
16297 int encoding ;
16299 current = psf_ftell (psf) ;
16301 if (calc_length)
16302 { psf_fseek (psf, 0, SEEK_END) ;
16303 psf->filelength = psf_ftell (psf) ;
16304 psf_fseek (psf, 0, SEEK_SET) ;
16306 psf->datalength = psf->filelength - psf->dataoffset ;
16307 if (psf->dataend)
16308 psf->datalength -= psf->filelength - psf->dataend ;
16310 psf->sf.frames = psf->datalength / (psf->bytewidth * psf->sf.channels) ;
16313 switch (psf->sf.format & SF_FORMAT_SUBMASK)
16314 { case SF_FORMAT_PCM_U8 :
16315 encoding = MAT5_TYPE_UCHAR ;
16316 break ;
16318 case SF_FORMAT_PCM_16 :
16319 encoding = MAT5_TYPE_INT16 ;
16320 break ;
16322 case SF_FORMAT_PCM_32 :
16323 encoding = MAT5_TYPE_INT32 ;
16324 break ;
16326 case SF_FORMAT_FLOAT :
16327 encoding = MAT5_TYPE_FLOAT ;
16328 break ;
16330 case SF_FORMAT_DOUBLE :
16331 encoding = MAT5_TYPE_DOUBLE ;
16332 break ;
16334 default :
16335 return SFE_BAD_OPEN_FORMAT ;
16338 /* Reset the current header length to zero. */
16339 psf->header [0] = 0 ;
16340 psf->headindex = 0 ;
16341 psf_fseek (psf, 0, SEEK_SET) ;
16343 psf_binheader_writef (psf, "S", "MATLAB 5.0 MAT-file, written by " PACKAGE "-" VERSION ", ") ;
16344 psf_get_date_str ((char*) psf->buffer, sizeof (psf->buffer)) ;
16345 psf_binheader_writef (psf, "jS", -1, psf->buffer) ;
16347 memset (psf->buffer, ' ', 124 - psf->headindex) ;
16348 psf_binheader_writef (psf, "b", psf->buffer, 124 - psf->headindex) ;
16350 psf->rwf_endian = psf->endian ;
16352 if (psf->rwf_endian == SF_ENDIAN_BIG)
16353 psf_binheader_writef (psf, "2b", 0x0100, "MI", 2) ;
16354 else
16355 psf_binheader_writef (psf, "2b", 0x0100, "IM", 2) ;
16357 psf_binheader_writef (psf, "444444", MAT5_TYPE_ARRAY, 64, MAT5_TYPE_UINT32, 8, 6, 0) ;
16358 psf_binheader_writef (psf, "4444", MAT5_TYPE_INT32, 8, 1, 1) ;
16359 psf_binheader_writef (psf, "44b", MAT5_TYPE_SCHAR, strlen (sr_name), sr_name, 16) ;
16361 if (psf->sf.samplerate > 0xFFFF)
16362 psf_binheader_writef (psf, "44", MAT5_TYPE_COMP_UINT, psf->sf.samplerate) ;
16363 else
16364 { unsigned short samplerate = psf->sf.samplerate ;
16366 psf_binheader_writef (psf, "422", MAT5_TYPE_COMP_USHORT, samplerate, 0) ;
16369 datasize = psf->sf.frames * psf->sf.channels * psf->bytewidth ;
16371 psf_binheader_writef (psf, "t484444", MAT5_TYPE_ARRAY, datasize + 64, MAT5_TYPE_UINT32, 8, 6, 0) ;
16372 psf_binheader_writef (psf, "t4448", MAT5_TYPE_INT32, 8, psf->sf.channels, psf->sf.frames) ;
16373 psf_binheader_writef (psf, "44b", MAT5_TYPE_SCHAR, strlen (wd_name), wd_name, strlen (wd_name)) ;
16375 datasize = psf->sf.frames * psf->sf.channels * psf->bytewidth ;
16376 if (datasize > 0x7FFFFFFF)
16377 datasize = 0x7FFFFFFF ;
16379 psf_binheader_writef (psf, "t48", encoding, datasize) ;
16381 /* Header construction complete so write it out. */
16382 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
16384 if (psf->error)
16385 return psf->error ;
16387 psf->dataoffset = psf->headindex ;
16389 if (current > 0)
16390 psf_fseek (psf, current, SEEK_SET) ;
16392 return psf->error ;
16393 } /* mat5_write_header */
16395 static int
16396 mat5_read_header (SF_PRIVATE *psf)
16397 { char name [32] ;
16398 short version, endian ;
16399 int type, size, flags1, flags2, rows, cols ;
16401 psf_binheader_readf (psf, "pb", 0, psf->buffer, 124) ;
16403 psf->buffer [125] = 0 ;
16405 if (strlen ((char*) psf->buffer) >= 124)
16406 return SFE_UNIMPLEMENTED ;
16408 if (strstr ((char*) psf->buffer, "MATLAB 5.0 MAT-file") == (char*) psf->buffer)
16409 psf_log_printf (psf, "%s\n", psf->buffer) ;
16412 psf_binheader_readf (psf, "E22", &version, &endian) ;
16414 if (endian == MI_MARKER)
16415 { psf->endian = psf->rwf_endian = SF_ENDIAN_BIG ;
16416 if (CPU_IS_LITTLE_ENDIAN) version = ENDSWAP_SHORT (version) ;
16418 else if (endian == IM_MARKER)
16419 { psf->endian = psf->rwf_endian = SF_ENDIAN_LITTLE ;
16420 if (CPU_IS_BIG_ENDIAN) version = ENDSWAP_SHORT (version) ;
16422 else
16423 return SFE_MAT5_BAD_ENDIAN ;
16425 if ((CPU_IS_LITTLE_ENDIAN && endian == IM_MARKER) ||
16426 (CPU_IS_BIG_ENDIAN && endian == MI_MARKER))
16427 version = ENDSWAP_SHORT (version) ;
16429 psf_log_printf (psf, "Version : 0x%04X\n", version) ;
16430 psf_log_printf (psf, "Endian : 0x%04X => %s\n", endian,
16431 (psf->endian == SF_ENDIAN_LITTLE) ? "Little" : "Big") ;
16433 /*========================================================*/
16434 psf_binheader_readf (psf, "44", &type, &size) ;
16435 psf_log_printf (psf, "Block\n Type : %X Size : %d\n", type, size) ;
16437 if (type != MAT5_TYPE_ARRAY)
16438 return SFE_MAT5_NO_BLOCK ;
16440 psf_binheader_readf (psf, "44", &type, &size) ;
16441 psf_log_printf (psf, " Type : %X Size : %d\n", type, size) ;
16443 if (type != MAT5_TYPE_UINT32)
16444 return SFE_MAT5_NO_BLOCK ;
16446 psf_binheader_readf (psf, "44", &flags1, &flags2) ;
16447 psf_log_printf (psf, " Flg1 : %X Flg2 : %d\n", flags1, flags2) ;
16449 psf_binheader_readf (psf, "44", &type, &size) ;
16450 psf_log_printf (psf, " Type : %X Size : %d\n", type, size) ;
16452 if (type != MAT5_TYPE_INT32)
16453 return SFE_MAT5_NO_BLOCK ;
16455 psf_binheader_readf (psf, "44", &rows, &cols) ;
16456 psf_log_printf (psf, " Rows : %X Cols : %d\n", rows, cols) ;
16458 if (rows != 1 || cols != 1)
16459 return SFE_MAT5_SAMPLE_RATE ;
16461 psf_binheader_readf (psf, "4", &type) ;
16463 if (type == MAT5_TYPE_SCHAR)
16464 { psf_binheader_readf (psf, "4", &size) ;
16465 psf_log_printf (psf, " Type : %X Size : %d\n", type, size) ;
16466 if (size > SIGNED_SIZEOF (name) - 1)
16467 { psf_log_printf (psf, "Error : Bad name length.\n") ;
16468 return SFE_MAT5_NO_BLOCK ;
16471 psf_binheader_readf (psf, "bj", name, size, (8 - (size % 8)) % 8) ;
16472 name [size] = 0 ;
16474 else if ((type & 0xFFFF) == MAT5_TYPE_SCHAR)
16475 { size = type >> 16 ;
16476 if (size > 4)
16477 { psf_log_printf (psf, "Error : Bad name length.\n") ;
16478 return SFE_MAT5_NO_BLOCK ;
16481 psf_log_printf (psf, " Type : %X\n", type) ;
16482 psf_binheader_readf (psf, "4", &name) ;
16483 name [size] = 0 ;
16485 else
16486 return SFE_MAT5_NO_BLOCK ;
16488 psf_log_printf (psf, " Name : %s\n", name) ;
16490 /*-----------------------------------------*/
16492 psf_binheader_readf (psf, "44", &type, &size) ;
16494 switch (type)
16495 { case MAT5_TYPE_DOUBLE :
16496 { double samplerate ;
16498 psf_binheader_readf (psf, "d", &samplerate) ;
16499 LSF_SNPRINTF (name, sizeof (name), "%f\n", samplerate) ;
16500 psf_log_printf (psf, " Val : %s\n", name) ;
16502 psf->sf.samplerate = lrint (samplerate) ;
16504 break ;
16506 case MAT5_TYPE_COMP_USHORT :
16507 { unsigned short samplerate ;
16509 psf_binheader_readf (psf, "j2j", -4, &samplerate, 2) ;
16510 psf_log_printf (psf, " Val : %u\n", samplerate) ;
16511 psf->sf.samplerate = samplerate ;
16513 break ;
16515 case MAT5_TYPE_COMP_UINT :
16516 psf_log_printf (psf, " Val : %u\n", size) ;
16517 psf->sf.samplerate = size ;
16518 break ;
16520 default :
16521 psf_log_printf (psf, " Type : %X Size : %d ***\n", type, size) ;
16522 return SFE_MAT5_SAMPLE_RATE ;
16525 /*-----------------------------------------*/
16528 psf_binheader_readf (psf, "44", &type, &size) ;
16529 psf_log_printf (psf, " Type : %X Size : %d\n", type, size) ;
16531 if (type != MAT5_TYPE_ARRAY)
16532 return SFE_MAT5_NO_BLOCK ;
16534 psf_binheader_readf (psf, "44", &type, &size) ;
16535 psf_log_printf (psf, " Type : %X Size : %d\n", type, size) ;
16537 if (type != MAT5_TYPE_UINT32)
16538 return SFE_MAT5_NO_BLOCK ;
16540 psf_binheader_readf (psf, "44", &flags1, &flags2) ;
16541 psf_log_printf (psf, " Flg1 : %X Flg2 : %d\n", flags1, flags2) ;
16543 psf_binheader_readf (psf, "44", &type, &size) ;
16544 psf_log_printf (psf, " Type : %X Size : %d\n", type, size) ;
16546 if (type != MAT5_TYPE_INT32)
16547 return SFE_MAT5_NO_BLOCK ;
16549 psf_binheader_readf (psf, "44", &rows, &cols) ;
16550 psf_log_printf (psf, " Rows : %X Cols : %d\n", rows, cols) ;
16552 psf_binheader_readf (psf, "4", &type) ;
16554 if (type == MAT5_TYPE_SCHAR)
16555 { psf_binheader_readf (psf, "4", &size) ;
16556 psf_log_printf (psf, " Type : %X Size : %d\n", type, size) ;
16557 if (size > SIGNED_SIZEOF (name) - 1)
16558 { psf_log_printf (psf, "Error : Bad name length.\n") ;
16559 return SFE_MAT5_NO_BLOCK ;
16562 psf_binheader_readf (psf, "bj", name, size, (8 - (size % 8)) % 8) ;
16563 name [size] = 0 ;
16565 else if ((type & 0xFFFF) == MAT5_TYPE_SCHAR)
16566 { size = type >> 16 ;
16567 if (size > 4)
16568 { psf_log_printf (psf, "Error : Bad name length.\n") ;
16569 return SFE_MAT5_NO_BLOCK ;
16572 psf_log_printf (psf, " Type : %X\n", type) ;
16573 psf_binheader_readf (psf, "4", &name) ;
16574 name [size] = 0 ;
16576 else
16577 return SFE_MAT5_NO_BLOCK ;
16579 psf_log_printf (psf, " Name : %s\n", name) ;
16581 psf_binheader_readf (psf, "44", &type, &size) ;
16582 psf_log_printf (psf, " Type : %X Size : %d\n", type, size) ;
16584 /*++++++++++++++++++++++++++++++++++++++++++++++++++*/
16586 if (rows == 0 && cols == 0)
16587 { psf_log_printf (psf, "*** Error : zero channel count.\n") ;
16588 return SFE_MAT5_ZERO_CHANNELS ;
16591 psf->sf.channels = rows ;
16592 psf->sf.frames = cols ;
16594 psf->sf.format = psf->endian | SF_FORMAT_MAT5 ;
16596 switch (type)
16597 { case MAT5_TYPE_DOUBLE :
16598 psf_log_printf (psf, "Data type : double\n") ;
16599 psf->sf.format |= SF_FORMAT_DOUBLE ;
16600 psf->bytewidth = 8 ;
16601 break ;
16603 case MAT5_TYPE_FLOAT :
16604 psf_log_printf (psf, "Data type : float\n") ;
16605 psf->sf.format |= SF_FORMAT_FLOAT ;
16606 psf->bytewidth = 4 ;
16607 break ;
16609 case MAT5_TYPE_INT32 :
16610 psf_log_printf (psf, "Data type : 32 bit PCM\n") ;
16611 psf->sf.format |= SF_FORMAT_PCM_32 ;
16612 psf->bytewidth = 4 ;
16613 break ;
16615 case MAT5_TYPE_INT16 :
16616 psf_log_printf (psf, "Data type : 16 bit PCM\n") ;
16617 psf->sf.format |= SF_FORMAT_PCM_16 ;
16618 psf->bytewidth = 2 ;
16619 break ;
16621 case MAT5_TYPE_UCHAR :
16622 psf_log_printf (psf, "Data type : unsigned 8 bit PCM\n") ;
16623 psf->sf.format |= SF_FORMAT_PCM_U8 ;
16624 psf->bytewidth = 1 ;
16625 break ;
16627 default :
16628 psf_log_printf (psf, "*** Error : Bad marker %08X\n", type) ;
16629 return SFE_UNIMPLEMENTED ;
16632 psf->dataoffset = psf_ftell (psf) ;
16633 psf->datalength = psf->filelength - psf->dataoffset ;
16635 return 0 ;
16636 } /* mat5_read_header */
16639 ** Do not edit or modify anything in this comment block.
16640 ** The arch-tag line is a file identity tag for the GNU Arch
16641 ** revision control system.
16643 ** arch-tag: dfdb6742-b2be-4be8-b390-d0c674e8bc8e
16646 ** Copyright (C) 1999-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
16648 ** This program is free software; you can redistribute it and/or modify
16649 ** it under the terms of the GNU Lesser General Public License as published by
16650 ** the Free Software Foundation; either version 2.1 of the License, or
16651 ** (at your option) any later version.
16653 ** This program is distributed in the hope that it will be useful,
16654 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16655 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16656 ** GNU Lesser General Public License for more details.
16658 ** You should have received a copy of the GNU Lesser General Public License
16659 ** along with this program; if not, write to the Free Software
16660 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16663 #include <stdio.h>
16664 #include <stdlib.h>
16665 #include <string.h>
16668 /* These required here because we write the header in this file. */
16670 #define RIFF_MARKER (MAKE_MARKER ('R', 'I', 'F', 'F'))
16671 #define WAVE_MARKER (MAKE_MARKER ('W', 'A', 'V', 'E'))
16672 #define fmt_MARKER (MAKE_MARKER ('f', 'm', 't', ' '))
16673 #define fact_MARKER (MAKE_MARKER ('f', 'a', 'c', 't'))
16674 #define data_MARKER (MAKE_MARKER ('d', 'a', 't', 'a'))
16676 #define WAVE_FORMAT_MS_ADPCM 0x0002
16678 typedef struct
16679 { int channels, blocksize, samplesperblock, blocks, dataremaining ;
16680 int blockcount ;
16681 sf_count_t samplecount ;
16682 short *samples ;
16683 unsigned char *block ;
16684 #if HAVE_FLEXIBLE_ARRAY
16685 unsigned short dummydata [] ; /* ISO C99 struct flexible array. */
16686 #else
16687 unsigned short dummydata [1] ; /* This is a hack an might not work. */
16688 #endif
16689 } MSADPCM_PRIVATE ;
16691 /*============================================================================================
16692 ** MS ADPCM static data and functions.
16695 static int AdaptationTable [] =
16696 { 230, 230, 230, 230, 307, 409, 512, 614,
16697 768, 614, 512, 409, 307, 230, 230, 230
16700 /* TODO : The first 7 coef's are are always hardcode and must
16701 appear in the actual WAVE file. They should be read in
16702 in case a sound program added extras to the list. */
16704 static int AdaptCoeff1 [MSADPCM_ADAPT_COEFF_COUNT] =
16705 { 256, 512, 0, 192, 240, 460, 392
16708 static int AdaptCoeff2 [MSADPCM_ADAPT_COEFF_COUNT] =
16709 { 0, -256, 0, 64, 0, -208, -232
16712 /*============================================================================================
16713 ** MS ADPCM Block Layout.
16714 ** ======================
16715 ** Block is usually 256, 512 or 1024 bytes depending on sample rate.
16716 ** For a mono file, the block is laid out as follows:
16717 ** byte purpose
16718 ** 0 block predictor [0..6]
16719 ** 1,2 initial idelta (positive)
16720 ** 3,4 sample 1
16721 ** 5,6 sample 0
16722 ** 7..n packed bytecodes
16724 ** For a stereo file, the block is laid out as follows:
16725 ** byte purpose
16726 ** 0 block predictor [0..6] for left channel
16727 ** 1 block predictor [0..6] for right channel
16728 ** 2,3 initial idelta (positive) for left channel
16729 ** 4,5 initial idelta (positive) for right channel
16730 ** 6,7 sample 1 for left channel
16731 ** 8,9 sample 1 for right channel
16732 ** 10,11 sample 0 for left channel
16733 ** 12,13 sample 0 for right channel
16734 ** 14..n packed bytecodes
16737 /*============================================================================================
16738 ** Static functions.
16741 static int msadpcm_decode_block (SF_PRIVATE *psf, MSADPCM_PRIVATE *pms) ;
16742 static sf_count_t msadpcm_read_block (SF_PRIVATE *psf, MSADPCM_PRIVATE *pms, short *ptr, int len) ;
16744 static int msadpcm_encode_block (SF_PRIVATE *psf, MSADPCM_PRIVATE *pms) ;
16745 static sf_count_t msadpcm_write_block (SF_PRIVATE *psf, MSADPCM_PRIVATE *pms, short *ptr, int len) ;
16747 static sf_count_t msadpcm_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
16748 static sf_count_t msadpcm_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
16749 static sf_count_t msadpcm_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
16750 static sf_count_t msadpcm_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
16752 static sf_count_t msadpcm_write_s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
16753 static sf_count_t msadpcm_write_i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
16754 static sf_count_t msadpcm_write_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
16755 static sf_count_t msadpcm_write_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
16757 static sf_count_t msadpcm_seek (SF_PRIVATE *psf, int mode, sf_count_t offset) ;
16758 static int msadpcm_close (SF_PRIVATE *psf) ;
16760 static void choose_predictor (unsigned int channels, short *data, int *bpred, int *idelta) ;
16762 /*============================================================================================
16763 ** MS ADPCM Read Functions.
16767 wav_w64_msadpcm_init (SF_PRIVATE *psf, int blockalign, int samplesperblock)
16768 { MSADPCM_PRIVATE *pms ;
16769 unsigned int pmssize ;
16770 int count ;
16772 if (psf->mode == SFM_WRITE)
16773 samplesperblock = 2 + 2 * (blockalign - 7 * psf->sf.channels) / psf->sf.channels ;
16775 pmssize = sizeof (MSADPCM_PRIVATE) + blockalign + 3 * psf->sf.channels * samplesperblock ;
16777 if (! (psf->fdata = malloc (pmssize)))
16778 return SFE_MALLOC_FAILED ;
16779 pms = (MSADPCM_PRIVATE*) psf->fdata ;
16780 memset (pms, 0, pmssize) ;
16782 pms->samples = (short *)pms->dummydata ;
16783 pms->block = (unsigned char*) (pms->dummydata + psf->sf.channels * samplesperblock) ;
16785 pms->channels = psf->sf.channels ;
16786 pms->blocksize = blockalign ;
16787 pms->samplesperblock = samplesperblock ;
16789 if (psf->mode == SFM_READ)
16790 { pms->dataremaining = psf->datalength ;
16792 if (psf->datalength % pms->blocksize)
16793 pms->blocks = psf->datalength / pms->blocksize + 1 ;
16794 else
16795 pms->blocks = psf->datalength / pms->blocksize ;
16797 count = 2 * (pms->blocksize - 6 * pms->channels) / pms->channels ;
16798 if (pms->samplesperblock != count)
16799 psf_log_printf (psf, "*** Warning : samplesperblock shoud be %d.\n", count) ;
16801 psf->sf.frames = (psf->datalength / pms->blocksize) * pms->samplesperblock ;
16803 psf_log_printf (psf, " bpred idelta\n") ;
16805 msadpcm_decode_block (psf, pms) ;
16807 psf->read_short = msadpcm_read_s ;
16808 psf->read_int = msadpcm_read_i ;
16809 psf->read_float = msadpcm_read_f ;
16810 psf->read_double = msadpcm_read_d ;
16813 if (psf->mode == SFM_WRITE)
16814 { pms->samples = (short *)pms->dummydata ;
16816 pms->samplecount = 0 ;
16818 psf->write_short = msadpcm_write_s ;
16819 psf->write_int = msadpcm_write_i ;
16820 psf->write_float = msadpcm_write_f ;
16821 psf->write_double = msadpcm_write_d ;
16824 psf->seek = msadpcm_seek ;
16825 psf->close = msadpcm_close ;
16827 return 0 ;
16828 } /* wav_w64_msadpcm_init */
16830 static int
16831 msadpcm_decode_block (SF_PRIVATE *psf, MSADPCM_PRIVATE *pms)
16832 { int chan, k, blockindx, sampleindx ;
16833 short bytecode, bpred [2], chan_idelta [2] ;
16835 int predict ;
16836 int current ;
16837 int idelta ;
16839 pms->blockcount ++ ;
16840 pms->samplecount = 0 ;
16842 if (pms->blockcount > pms->blocks)
16843 { memset (pms->samples, 0, pms->samplesperblock * pms->channels) ;
16844 return 1 ;
16847 if ((k = psf_fread (pms->block, 1, pms->blocksize, psf)) != pms->blocksize)
16848 psf_log_printf (psf, "*** Warning : short read (%d != %d).\n", k, pms->blocksize) ;
16850 /* Read and check the block header. */
16852 if (pms->channels == 1)
16853 { bpred [0] = pms->block [0] ;
16855 if (bpred [0] >= 7)
16856 psf_log_printf (psf, "MS ADPCM synchronisation error (%d).\n", bpred [0]) ;
16858 chan_idelta [0] = pms->block [1] | (pms->block [2] << 8) ;
16859 chan_idelta [1] = 0 ;
16861 psf_log_printf (psf, "(%d) (%d)\n", bpred [0], chan_idelta [0]) ;
16863 pms->samples [1] = pms->block [3] | (pms->block [4] << 8) ;
16864 pms->samples [0] = pms->block [5] | (pms->block [6] << 8) ;
16865 blockindx = 7 ;
16867 else
16868 { bpred [0] = pms->block [0] ;
16869 bpred [1] = pms->block [1] ;
16871 if (bpred [0] >= 7 || bpred [1] >= 7)
16872 psf_log_printf (psf, "MS ADPCM synchronisation error (%d %d).\n", bpred [0], bpred [1]) ;
16874 chan_idelta [0] = pms->block [2] | (pms->block [3] << 8) ;
16875 chan_idelta [1] = pms->block [4] | (pms->block [5] << 8) ;
16877 psf_log_printf (psf, "(%d, %d) (%d, %d)\n", bpred [0], bpred [1], chan_idelta [0], chan_idelta [1]) ;
16879 pms->samples [2] = pms->block [6] | (pms->block [7] << 8) ;
16880 pms->samples [3] = pms->block [8] | (pms->block [9] << 8) ;
16882 pms->samples [0] = pms->block [10] | (pms->block [11] << 8) ;
16883 pms->samples [1] = pms->block [12] | (pms->block [13] << 8) ;
16885 blockindx = 14 ;
16888 /*--------------------------------------------------------
16889 This was left over from a time when calculations were done
16890 as ints rather than shorts. Keep this around as a reminder
16891 in case I ever find a file which decodes incorrectly.
16893 if (chan_idelta [0] & 0x8000)
16894 chan_idelta [0] -= 0x10000 ;
16895 if (chan_idelta [1] & 0x8000)
16896 chan_idelta [1] -= 0x10000 ;
16897 --------------------------------------------------------*/
16899 /* Pull apart the packed 4 bit samples and store them in their
16900 ** correct sample positions.
16903 sampleindx = 2 * pms->channels ;
16904 while (blockindx < pms->blocksize)
16905 { bytecode = pms->block [blockindx++] ;
16906 pms->samples [sampleindx++] = (bytecode >> 4) & 0x0F ;
16907 pms->samples [sampleindx++] = bytecode & 0x0F ;
16910 /* Decode the encoded 4 bit samples. */
16912 for (k = 2 * pms->channels ; k < (pms->samplesperblock * pms->channels) ; k ++)
16913 { chan = (pms->channels > 1) ? (k % 2) : 0 ;
16915 bytecode = pms->samples [k] & 0xF ;
16917 /* Compute next Adaptive Scale Factor (ASF) */
16918 idelta = chan_idelta [chan] ;
16919 chan_idelta [chan] = (AdaptationTable [bytecode] * idelta) >> 8 ; /* => / 256 => FIXED_POINT_ADAPTATION_BASE == 256 */
16920 if (chan_idelta [chan] < 16)
16921 chan_idelta [chan] = 16 ;
16922 if (bytecode & 0x8)
16923 bytecode -= 0x10 ;
16925 predict = ((pms->samples [k - pms->channels] * AdaptCoeff1 [bpred [chan]])
16926 + (pms->samples [k - 2 * pms->channels] * AdaptCoeff2 [bpred [chan]])) >> 8 ; /* => / 256 => FIXED_POINT_COEFF_BASE == 256 */
16927 current = (bytecode * idelta) + predict ;
16929 if (current > 32767)
16930 current = 32767 ;
16931 else if (current < -32768)
16932 current = -32768 ;
16934 pms->samples [k] = current ;
16937 return 1 ;
16938 } /* msadpcm_decode_block */
16940 static sf_count_t
16941 msadpcm_read_block (SF_PRIVATE *psf, MSADPCM_PRIVATE *pms, short *ptr, int len)
16942 { int count, total = 0, indx = 0 ;
16944 while (indx < len)
16945 { if (pms->blockcount >= pms->blocks && pms->samplecount >= pms->samplesperblock)
16946 { memset (&(ptr [indx]), 0, (size_t) ((len - indx) * sizeof (short))) ;
16947 return total ;
16950 if (pms->samplecount >= pms->samplesperblock)
16951 msadpcm_decode_block (psf, pms) ;
16953 count = (pms->samplesperblock - pms->samplecount) * pms->channels ;
16954 count = (len - indx > count) ? count : len - indx ;
16956 memcpy (&(ptr [indx]), &(pms->samples [pms->samplecount * pms->channels]), count * sizeof (short)) ;
16957 indx += count ;
16958 pms->samplecount += count / pms->channels ;
16959 total = indx ;
16962 return total ;
16963 } /* msadpcm_read_block */
16965 static sf_count_t
16966 msadpcm_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
16967 { MSADPCM_PRIVATE *pms ;
16968 int readcount, count ;
16969 sf_count_t total = 0 ;
16971 if (! psf->fdata)
16972 return 0 ;
16973 pms = (MSADPCM_PRIVATE*) psf->fdata ;
16975 while (len > 0)
16976 { readcount = (len > 0x10000000) ? 0x10000000 : (int) len ;
16978 count = msadpcm_read_block (psf, pms, ptr, readcount) ;
16980 total += count ;
16981 len -= count ;
16982 if (count != readcount)
16983 break ;
16986 return total ;
16987 } /* msadpcm_read_s */
16989 static sf_count_t
16990 msadpcm_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
16991 { MSADPCM_PRIVATE *pms ;
16992 short *sptr ;
16993 int k, bufferlen, readcount = 0, count ;
16994 sf_count_t total = 0 ;
16996 if (! psf->fdata)
16997 return 0 ;
16998 pms = (MSADPCM_PRIVATE*) psf->fdata ;
17000 sptr = (short*) psf->buffer ;
17001 bufferlen = SF_BUFFER_LEN / sizeof (short) ;
17002 while (len > 0)
17003 { readcount = (len >= bufferlen) ? bufferlen : len ;
17004 count = msadpcm_read_block (psf, pms, sptr, readcount) ;
17005 for (k = 0 ; k < readcount ; k++)
17006 ptr [total + k] = sptr [k] << 16 ;
17007 total += count ;
17008 len -= readcount ;
17009 if (count != readcount)
17010 break ;
17012 return total ;
17013 } /* msadpcm_read_i */
17015 static sf_count_t
17016 msadpcm_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
17017 { MSADPCM_PRIVATE *pms ;
17018 short *sptr ;
17019 int k, bufferlen, readcount = 0, count ;
17020 sf_count_t total = 0 ;
17021 float normfact ;
17023 if (! psf->fdata)
17024 return 0 ;
17025 pms = (MSADPCM_PRIVATE*) psf->fdata ;
17027 normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x8000) : 1.0 ;
17028 sptr = (short*) psf->buffer ;
17029 bufferlen = SF_BUFFER_LEN / sizeof (short) ;
17030 while (len > 0)
17031 { readcount = (len >= bufferlen) ? bufferlen : len ;
17032 count = msadpcm_read_block (psf, pms, sptr, readcount) ;
17033 for (k = 0 ; k < readcount ; k++)
17034 ptr [total + k] = normfact * (float) (sptr [k]) ;
17035 total += count ;
17036 len -= readcount ;
17037 if (count != readcount)
17038 break ;
17040 return total ;
17041 } /* msadpcm_read_f */
17043 static sf_count_t
17044 msadpcm_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
17045 { MSADPCM_PRIVATE *pms ;
17046 short *sptr ;
17047 int k, bufferlen, readcount = 0, count ;
17048 sf_count_t total = 0 ;
17049 double normfact ;
17051 normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x8000) : 1.0 ;
17053 if (! psf->fdata)
17054 return 0 ;
17055 pms = (MSADPCM_PRIVATE*) psf->fdata ;
17057 sptr = (short*) psf->buffer ;
17058 bufferlen = SF_BUFFER_LEN / sizeof (short) ;
17059 while (len > 0)
17060 { readcount = (len >= bufferlen) ? bufferlen : len ;
17061 count = msadpcm_read_block (psf, pms, sptr, readcount) ;
17062 for (k = 0 ; k < readcount ; k++)
17063 ptr [total + k] = normfact * (double) (sptr [k]) ;
17064 total += count ;
17065 len -= readcount ;
17066 if (count != readcount)
17067 break ;
17069 return total ;
17070 } /* msadpcm_read_d */
17072 static sf_count_t
17073 msadpcm_seek (SF_PRIVATE *psf, int mode, sf_count_t offset)
17074 { MSADPCM_PRIVATE *pms ;
17075 int newblock, newsample ;
17077 if (! psf->fdata)
17078 return 0 ;
17079 pms = (MSADPCM_PRIVATE*) psf->fdata ;
17081 if (psf->datalength < 0 || psf->dataoffset < 0)
17082 { psf->error = SFE_BAD_SEEK ;
17083 return ((sf_count_t) -1) ;
17086 if (offset == 0)
17087 { psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
17088 pms->blockcount = 0 ;
17089 msadpcm_decode_block (psf, pms) ;
17090 pms->samplecount = 0 ;
17091 return 0 ;
17094 if (offset < 0 || offset > pms->blocks * pms->samplesperblock)
17095 { psf->error = SFE_BAD_SEEK ;
17096 return ((sf_count_t) -1) ;
17099 newblock = offset / pms->samplesperblock ;
17100 newsample = offset % pms->samplesperblock ;
17102 if (mode == SFM_READ)
17103 { psf_fseek (psf, psf->dataoffset + newblock * pms->blocksize, SEEK_SET) ;
17104 pms->blockcount = newblock ;
17105 msadpcm_decode_block (psf, pms) ;
17106 pms->samplecount = newsample ;
17108 else
17109 { /* What to do about write??? */
17110 psf->error = SFE_BAD_SEEK ;
17111 return ((sf_count_t) -1) ;
17114 return newblock * pms->samplesperblock + newsample ;
17115 } /* msadpcm_seek */
17117 /*==========================================================================================
17118 ** MS ADPCM Write Functions.
17121 void
17122 msadpcm_write_adapt_coeffs (SF_PRIVATE *psf)
17123 { int k ;
17125 for (k = 0 ; k < MSADPCM_ADAPT_COEFF_COUNT ; k++)
17126 psf_binheader_writef (psf, "e22", AdaptCoeff1 [k], AdaptCoeff2 [k]) ;
17127 } /* msadpcm_write_adapt_coeffs */
17129 /*==========================================================================================
17132 static int
17133 msadpcm_encode_block (SF_PRIVATE *psf, MSADPCM_PRIVATE *pms)
17134 { unsigned int blockindx ;
17135 unsigned char byte ;
17136 int chan, k, predict, bpred [2], idelta [2], errordelta, newsamp ;
17138 choose_predictor (pms->channels, pms->samples, bpred, idelta) ;
17140 /* Write the block header. */
17142 if (pms->channels == 1)
17143 { pms->block [0] = bpred [0] ;
17144 pms->block [1] = idelta [0] & 0xFF ;
17145 pms->block [2] = idelta [0] >> 8 ;
17146 pms->block [3] = pms->samples [1] & 0xFF ;
17147 pms->block [4] = pms->samples [1] >> 8 ;
17148 pms->block [5] = pms->samples [0] & 0xFF ;
17149 pms->block [6] = pms->samples [0] >> 8 ;
17151 blockindx = 7 ;
17152 byte = 0 ;
17154 /* Encode the samples as 4 bit. */
17156 for (k = 2 ; k < pms->samplesperblock ; k++)
17157 { predict = (pms->samples [k-1] * AdaptCoeff1 [bpred [0]] + pms->samples [k-2] * AdaptCoeff2 [bpred [0]]) >> 8 ;
17158 errordelta = (pms->samples [k] - predict) / idelta [0] ;
17159 if (errordelta < -8)
17160 errordelta = -8 ;
17161 else if (errordelta > 7)
17162 errordelta = 7 ;
17163 newsamp = predict + (idelta [0] * errordelta) ;
17164 if (newsamp > 32767)
17165 newsamp = 32767 ;
17166 else if (newsamp < -32768)
17167 newsamp = -32768 ;
17168 if (errordelta < 0)
17169 errordelta += 0x10 ;
17171 byte = (byte << 4) | (errordelta & 0xF) ;
17172 if (k % 2)
17173 { pms->block [blockindx++] = byte ;
17174 byte = 0 ;
17177 idelta [0] = (idelta [0] * AdaptationTable [errordelta]) >> 8 ;
17178 if (idelta [0] < 16)
17179 idelta [0] = 16 ;
17180 pms->samples [k] = newsamp ;
17183 else
17184 { /* Stereo file. */
17185 pms->block [0] = bpred [0] ;
17186 pms->block [1] = bpred [1] ;
17188 pms->block [2] = idelta [0] & 0xFF ;
17189 pms->block [3] = idelta [0] >> 8 ;
17190 pms->block [4] = idelta [1] & 0xFF ;
17191 pms->block [5] = idelta [1] >> 8 ;
17193 pms->block [6] = pms->samples [2] & 0xFF ;
17194 pms->block [7] = pms->samples [2] >> 8 ;
17195 pms->block [8] = pms->samples [3] & 0xFF ;
17196 pms->block [9] = pms->samples [3] >> 8 ;
17198 pms->block [10] = pms->samples [0] & 0xFF ;
17199 pms->block [11] = pms->samples [0] >> 8 ;
17200 pms->block [12] = pms->samples [1] & 0xFF ;
17201 pms->block [13] = pms->samples [1] >> 8 ;
17203 blockindx = 14 ;
17204 byte = 0 ;
17205 chan = 1 ;
17207 for (k = 4 ; k < 2 * pms->samplesperblock ; k++)
17208 { chan = k & 1 ;
17210 predict = (pms->samples [k-2] * AdaptCoeff1 [bpred [chan]] + pms->samples [k-4] * AdaptCoeff2 [bpred [chan]]) >> 8 ;
17211 errordelta = (pms->samples [k] - predict) / idelta [chan] ;
17214 if (errordelta < -8)
17215 errordelta = -8 ;
17216 else if (errordelta > 7)
17217 errordelta = 7 ;
17218 newsamp = predict + (idelta [chan] * errordelta) ;
17219 if (newsamp > 32767)
17220 newsamp = 32767 ;
17221 else if (newsamp < -32768)
17222 newsamp = -32768 ;
17223 if (errordelta < 0)
17224 errordelta += 0x10 ;
17226 byte = (byte << 4) | (errordelta & 0xF) ;
17228 if (chan)
17229 { pms->block [blockindx++] = byte ;
17230 byte = 0 ;
17233 idelta [chan] = (idelta [chan] * AdaptationTable [errordelta]) >> 8 ;
17234 if (idelta [chan] < 16)
17235 idelta [chan] = 16 ;
17236 pms->samples [k] = newsamp ;
17240 /* Write the block to disk. */
17242 if ((k = psf_fwrite (pms->block, 1, pms->blocksize, psf)) != pms->blocksize)
17243 psf_log_printf (psf, "*** Warning : short write (%d != %d).\n", k, pms->blocksize) ;
17245 memset (pms->samples, 0, pms->samplesperblock * sizeof (short)) ;
17247 pms->blockcount ++ ;
17248 pms->samplecount = 0 ;
17250 return 1 ;
17251 } /* msadpcm_encode_block */
17253 static sf_count_t
17254 msadpcm_write_block (SF_PRIVATE *psf, MSADPCM_PRIVATE *pms, short *ptr, int len)
17255 { int count, total = 0, indx = 0 ;
17257 while (indx < len)
17258 { count = (pms->samplesperblock - pms->samplecount) * pms->channels ;
17260 if (count > len - indx)
17261 count = len - indx ;
17263 memcpy (&(pms->samples [pms->samplecount * pms->channels]), &(ptr [total]), count * sizeof (short)) ;
17264 indx += count ;
17265 pms->samplecount += count / pms->channels ;
17266 total = indx ;
17268 if (pms->samplecount >= pms->samplesperblock)
17269 msadpcm_encode_block (psf, pms) ;
17272 return total ;
17273 } /* msadpcm_write_block */
17275 static sf_count_t
17276 msadpcm_write_s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
17277 { MSADPCM_PRIVATE *pms ;
17278 int writecount, count ;
17279 sf_count_t total = 0 ;
17281 if (! psf->fdata)
17282 return 0 ;
17283 pms = (MSADPCM_PRIVATE*) psf->fdata ;
17285 while (len > 0)
17286 { writecount = (len > 0x10000000) ? 0x10000000 : (int) len ;
17288 count = msadpcm_write_block (psf, pms, ptr, writecount) ;
17290 total += count ;
17291 len -= count ;
17292 if (count != writecount)
17293 break ;
17296 return total ;
17297 } /* msadpcm_write_s */
17299 static sf_count_t
17300 msadpcm_write_i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
17301 { MSADPCM_PRIVATE *pms ;
17302 short *sptr ;
17303 int k, bufferlen, writecount, count ;
17304 sf_count_t total = 0 ;
17306 if (! psf->fdata)
17307 return 0 ;
17308 pms = (MSADPCM_PRIVATE*) psf->fdata ;
17310 sptr = (short*) psf->buffer ;
17311 bufferlen = SF_BUFFER_LEN / sizeof (short) ;
17312 while (len > 0)
17313 { writecount = (len >= bufferlen) ? bufferlen : len ;
17314 for (k = 0 ; k < writecount ; k++)
17315 sptr [k] = ptr [total + k] >> 16 ;
17316 count = msadpcm_write_block (psf, pms, sptr, writecount) ;
17317 total += count ;
17318 len -= writecount ;
17319 if (count != writecount)
17320 break ;
17322 return total ;
17323 } /* msadpcm_write_i */
17325 static sf_count_t
17326 msadpcm_write_f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
17327 { MSADPCM_PRIVATE *pms ;
17328 short *sptr ;
17329 int k, bufferlen, writecount, count ;
17330 sf_count_t total = 0 ;
17331 float normfact ;
17333 if (! psf->fdata)
17334 return 0 ;
17335 pms = (MSADPCM_PRIVATE*) psf->fdata ;
17337 normfact = (psf->norm_float == SF_TRUE) ? (1.0 * 0x7FFF) : 1.0 ;
17339 sptr = (short*) psf->buffer ;
17340 bufferlen = SF_BUFFER_LEN / sizeof (short) ;
17341 while (len > 0)
17342 { writecount = (len >= bufferlen) ? bufferlen : len ;
17343 for (k = 0 ; k < writecount ; k++)
17344 sptr [k] = lrintf (normfact * ptr [total + k]) ;
17345 count = msadpcm_write_block (psf, pms, sptr, writecount) ;
17346 total += count ;
17347 len -= writecount ;
17348 if (count != writecount)
17349 break ;
17351 return total ;
17352 } /* msadpcm_write_f */
17354 static sf_count_t
17355 msadpcm_write_d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
17356 { MSADPCM_PRIVATE *pms ;
17357 short *sptr ;
17358 int k, bufferlen, writecount, count ;
17359 sf_count_t total = 0 ;
17360 double normfact ;
17362 normfact = (psf->norm_double == SF_TRUE) ? (1.0 * 0x7FFF) : 1.0 ;
17364 if (! psf->fdata)
17365 return 0 ;
17366 pms = (MSADPCM_PRIVATE*) psf->fdata ;
17368 sptr = (short*) psf->buffer ;
17369 bufferlen = SF_BUFFER_LEN / sizeof (short) ;
17370 while (len > 0)
17371 { writecount = (len >= bufferlen) ? bufferlen : len ;
17372 for (k = 0 ; k < writecount ; k++)
17373 sptr [k] = lrint (normfact * ptr [total + k]) ;
17374 count = msadpcm_write_block (psf, pms, sptr, writecount) ;
17375 total += count ;
17376 len -= writecount ;
17377 if (count != writecount)
17378 break ;
17380 return total ;
17381 } /* msadpcm_write_d */
17383 /*========================================================================================
17386 static int
17387 msadpcm_close (SF_PRIVATE *psf)
17388 { MSADPCM_PRIVATE *pms ;
17390 if (! psf->fdata)
17391 return 0 ;
17393 pms = (MSADPCM_PRIVATE*) psf->fdata ;
17395 if (psf->mode == SFM_WRITE)
17396 { /* Now we know static int for certain the length of the file we can
17397 ** re-write the header.
17400 if (pms->samplecount && pms->samplecount < pms->samplesperblock)
17401 msadpcm_encode_block (psf, pms) ;
17403 if (psf->write_header)
17404 psf->write_header (psf, SF_TRUE) ;
17407 return 0 ;
17408 } /* msadpcm_close */
17410 /*========================================================================================
17411 ** Static functions.
17414 /*----------------------------------------------------------------------------------------
17415 ** Choosing the block predictor.
17416 ** Each block requires a predictor and an idelta for each channel.
17417 ** The predictor is in the range [0..6] which is an indx into the two AdaptCoeff tables.
17418 ** The predictor is chosen by trying all of the possible predictors on a small set of
17419 ** samples at the beginning of the block. The predictor with the smallest average
17420 ** abs (idelta) is chosen as the best predictor for this block.
17421 ** The value of idelta is chosen to to give a 4 bit code value of +/- 4 (approx. half the
17422 ** max. code value). If the average abs (idelta) is zero, the sixth predictor is chosen.
17423 ** If the value of idelta is less then 16 it is set to 16.
17425 ** Microsoft uses an IDELTA_COUNT (number of sample pairs used to choose best predictor)
17426 ** value of 3. The best possible results would be obtained by using all the samples to
17427 ** choose the predictor.
17430 #define IDELTA_COUNT 3
17432 static void
17433 choose_predictor (unsigned int channels, short *data, int *block_pred, int *idelta)
17434 { unsigned int chan, k, bpred, idelta_sum, best_bpred, best_idelta ;
17436 for (chan = 0 ; chan < channels ; chan++)
17437 { best_bpred = best_idelta = 0 ;
17439 for (bpred = 0 ; bpred < 7 ; bpred++)
17440 { idelta_sum = 0 ;
17441 for (k = 2 ; k < 2 + IDELTA_COUNT ; k++)
17442 idelta_sum += abs (data [k * channels] - ((data [(k - 1) * channels] * AdaptCoeff1 [bpred] + data [(k - 2) * channels] * AdaptCoeff2 [bpred]) >> 8)) ;
17443 idelta_sum /= (4 * IDELTA_COUNT) ;
17445 if (bpred == 0 || idelta_sum < best_idelta)
17446 { best_bpred = bpred ;
17447 best_idelta = idelta_sum ;
17450 if (! idelta_sum)
17451 { best_bpred = bpred ;
17452 best_idelta = 16 ;
17453 break ;
17456 } ; /* for bpred ... */
17457 if (best_idelta < 16)
17458 best_idelta = 16 ;
17460 block_pred [chan] = best_bpred ;
17461 idelta [chan] = best_idelta ;
17464 return ;
17465 } /* choose_predictor */
17467 ** Do not edit or modify anything in this comment block.
17468 ** The arch-tag line is a file identity tag for the GNU Arch
17469 ** revision control system.
17471 ** arch-tag: a98908a3-5305-4935-872b-77d6a86c330f
17474 ** Copyright (C) 1999-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
17476 ** This program is free software; you can redistribute it and/or modify
17477 ** it under the terms of the GNU Lesser General Public License as published by
17478 ** the Free Software Foundation; either version 2.1 of the License, or
17479 ** (at your option) any later version.
17481 ** This program is distributed in the hope that it will be useful,
17482 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17483 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17484 ** GNU Lesser General Public License for more details.
17486 ** You should have received a copy of the GNU Lesser General Public License
17487 ** along with this program; if not, write to the Free Software
17488 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17492 ** Some of the information used to read NIST files was gleaned from
17493 ** reading the code of Bill Schottstaedt's sndlib library
17494 ** ftp://ccrma-ftp.stanford.edu/pub/Lisp/sndlib.tar.gz
17495 ** However, no code from that package was used.
17498 #include <stdio.h>
17499 #include <fcntl.h>
17500 #include <string.h>
17501 #include <ctype.h>
17504 /*------------------------------------------------------------------------------
17507 #define NIST_HEADER_LENGTH 1024
17509 /*------------------------------------------------------------------------------
17510 ** Private static functions.
17513 static int nist_close (SF_PRIVATE *psf) ;
17514 static int nist_write_header (SF_PRIVATE *psf, int calc_length) ;
17515 static int nist_read_header (SF_PRIVATE *psf) ;
17517 /*------------------------------------------------------------------------------
17521 nist_open (SF_PRIVATE *psf)
17522 { int subformat, error ;
17524 if (psf->mode == SFM_READ || (psf->mode == SFM_RDWR && psf->filelength > 0))
17525 { if ((error = nist_read_header (psf)))
17526 return error ;
17529 subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
17531 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
17532 { if (psf->is_pipe)
17533 return SFE_NO_PIPE_WRITE ;
17535 if ((psf->sf.format & SF_FORMAT_TYPEMASK) != SF_FORMAT_NIST)
17536 return SFE_BAD_OPEN_FORMAT ;
17538 psf->endian = psf->sf.format & SF_FORMAT_ENDMASK ;
17539 if (psf->endian == 0 || psf->endian == SF_ENDIAN_CPU)
17540 psf->endian = (CPU_IS_BIG_ENDIAN) ? SF_ENDIAN_BIG : SF_ENDIAN_LITTLE ;
17542 psf->blockwidth = psf->bytewidth * psf->sf.channels ;
17544 if ((error = nist_write_header (psf, SF_FALSE)))
17545 return error ;
17547 psf->write_header = nist_write_header ;
17550 psf->close = nist_close ;
17552 switch (psf->sf.format & SF_FORMAT_SUBMASK)
17553 { case SF_FORMAT_PCM_S8 :
17554 error = pcm_init (psf) ;
17555 break ;
17557 case SF_FORMAT_PCM_16 :
17558 case SF_FORMAT_PCM_24 :
17559 case SF_FORMAT_PCM_32 :
17560 error = pcm_init (psf) ;
17561 break ;
17563 case SF_FORMAT_ULAW :
17564 error = ulaw_init (psf) ;
17565 break ;
17567 case SF_FORMAT_ALAW :
17568 error = alaw_init (psf) ;
17569 break ;
17571 default : error = SFE_UNIMPLEMENTED ;
17572 break ;
17575 return error ;
17576 } /* nist_open */
17578 /*------------------------------------------------------------------------------
17581 static char bad_header [] =
17582 { 'N', 'I', 'S', 'T', '_', '1', 'A', 0x0d, 0x0a,
17583 ' ', ' ', ' ', '1', '0', '2', '4', 0x0d, 0x0a,
17587 static int
17588 nist_read_header (SF_PRIVATE *psf)
17589 { char *psf_header ;
17590 int bitwidth = 0, bytes = 0, count, encoding ;
17591 char str [64], *cptr ;
17592 long samples ;
17594 psf->sf.format = SF_FORMAT_NIST ;
17596 psf_header = (char*) psf->buffer ;
17598 if (sizeof (psf->header) <= NIST_HEADER_LENGTH)
17599 return SFE_INTERNAL ;
17601 /* Go to start of file and read in the whole header. */
17602 psf_binheader_readf (psf, "pb", 0, psf_header, NIST_HEADER_LENGTH) ;
17604 /* Header is a string, so make sure it is null terminated. */
17605 psf_header [NIST_HEADER_LENGTH] = 0 ;
17607 /* Now trim the header after the end marker. */
17608 if ((cptr = strstr (psf_header, "end_head")))
17609 { cptr += strlen ("end_head") + 1 ;
17610 cptr [0] = 0 ;
17613 if (strstr (psf_header, bad_header) == psf_header)
17614 return SFE_NIST_CRLF_CONVERISON ;
17616 /* Make sure its a NIST file. */
17617 if (strstr (psf_header, "NIST_1A\n 1024\n") != psf_header)
17618 { psf_log_printf (psf, "Not a NIST file.\n") ;
17619 return SFE_NIST_BAD_HEADER ;
17622 /* Determine sample encoding, start by assuming PCM. */
17623 encoding = SF_FORMAT_PCM_U8 ;
17624 if ((cptr = strstr (psf_header, "sample_coding -s")))
17625 { sscanf (cptr, "sample_coding -s%d %63s", &count, str) ;
17627 if (strcmp (str, "pcm") == 0)
17628 encoding = SF_FORMAT_PCM_U8 ;
17629 else if (strcmp (str, "alaw") == 0)
17630 encoding = SF_FORMAT_ALAW ;
17631 else if ((strcmp (str, "ulaw") == 0) || (strcmp (str, "mu-law") == 0))
17632 encoding = SF_FORMAT_ULAW ;
17633 else
17634 { psf_log_printf (psf, "*** Unknown encoding : %s\n", str) ;
17635 encoding = 0 ;
17639 if ((cptr = strstr (psf_header, "channel_count -i ")))
17640 sscanf (cptr, "channel_count -i %d", &(psf->sf.channels)) ;
17642 if ((cptr = strstr (psf_header, "sample_rate -i ")))
17643 sscanf (cptr, "sample_rate -i %d", &(psf->sf.samplerate)) ;
17645 if ((cptr = strstr (psf_header, "sample_count -i ")))
17646 { sscanf (psf_header, "sample_count -i %ld", &samples) ;
17647 psf->sf.frames = samples ;
17650 if ((cptr = strstr (psf_header, "sample_n_bytes -i ")))
17651 sscanf (cptr, "sample_n_bytes -i %d", &(psf->bytewidth)) ;
17653 /* Default endian-ness (for 8 bit, u-law, A-law. */
17654 psf->endian = (CPU_IS_BIG_ENDIAN) ? SF_ENDIAN_BIG : SF_ENDIAN_LITTLE ;
17656 /* This is where we figure out endian-ness. */
17657 if ((cptr = strstr (psf_header, "sample_byte_format -s")))
17658 { sscanf (cptr, "sample_byte_format -s%d %8s", &bytes, str) ;
17659 if (bytes > 1)
17660 { if (psf->bytewidth == 0)
17661 psf->bytewidth = bytes ;
17662 else if (psf->bytewidth != bytes)
17663 { psf_log_printf (psf, "psf->bytewidth (%d) != bytes (%d)\n", psf->bytewidth, bytes) ;
17664 return SFE_NIST_BAD_ENCODING ;
17667 if (strstr (str, "01") == str)
17668 psf->endian = SF_ENDIAN_LITTLE ;
17669 else if (strstr (str, "10"))
17670 psf->endian = SF_ENDIAN_BIG ;
17671 else
17672 { psf_log_printf (psf, "Weird endian-ness : %s\n", str) ;
17673 return SFE_NIST_BAD_ENCODING ;
17677 psf->sf.format |= psf->endian ;
17680 if ((cptr = strstr (psf_header, "sample_sig_bits -i ")))
17681 sscanf (cptr, "sample_sig_bits -i %d", &bitwidth) ;
17683 if (strstr (psf_header, "channels_interleaved -s5 FALSE"))
17684 { psf_log_printf (psf, "Non-interleaved data unsupported.\n", str) ;
17685 return SFE_NIST_BAD_ENCODING ;
17688 psf->dataoffset = NIST_HEADER_LENGTH ;
17689 psf->blockwidth = psf->sf.channels * psf->bytewidth ;
17690 psf->datalength = psf->filelength - psf->dataoffset ;
17692 psf->close = nist_close ;
17694 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
17696 if (encoding == SF_FORMAT_PCM_U8)
17697 { switch (psf->bytewidth)
17698 { case 1 :
17699 psf->sf.format |= SF_FORMAT_PCM_S8 ;
17700 break ;
17702 case 2 :
17703 psf->sf.format |= SF_FORMAT_PCM_16 ;
17704 break ;
17706 case 3 :
17707 psf->sf.format |= SF_FORMAT_PCM_24 ;
17708 break ;
17710 case 4 :
17711 psf->sf.format |= SF_FORMAT_PCM_32 ;
17712 break ;
17714 default : break ;
17717 else if (encoding != 0)
17718 psf->sf.format |= encoding ;
17719 else
17720 return SFE_UNIMPLEMENTED ;
17722 return 0 ;
17723 } /* nist_read_header */
17725 static int
17726 nist_close (SF_PRIVATE *psf)
17728 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
17729 nist_write_header (psf, SF_TRUE) ;
17731 return 0 ;
17732 } /* nist_close */
17734 /*=========================================================================
17737 static int
17738 nist_write_header (SF_PRIVATE *psf, int calc_length)
17739 { const char *end_str ;
17740 long samples ;
17741 sf_count_t current ;
17743 current = psf_ftell (psf) ;
17745 /* Prevent compiler warning. */
17746 calc_length = calc_length ;
17748 if (psf->endian == SF_ENDIAN_BIG)
17749 end_str = "10" ;
17750 else if (psf->endian == SF_ENDIAN_LITTLE)
17751 end_str = "01" ;
17752 else
17753 end_str = "error" ;
17755 /* Clear the whole header. */
17756 memset (psf->header, 0, sizeof (psf->header)) ;
17757 psf->headindex = 0 ;
17759 psf_fseek (psf, 0, SEEK_SET) ;
17761 psf_asciiheader_printf (psf, "NIST_1A\n 1024\n") ;
17762 psf_asciiheader_printf (psf, "channel_count -i %d\n", psf->sf.channels) ;
17763 psf_asciiheader_printf (psf, "sample_rate -i %d\n", psf->sf.samplerate) ;
17765 switch (psf->sf.format & SF_FORMAT_SUBMASK)
17766 { case SF_FORMAT_PCM_S8 :
17767 psf_asciiheader_printf (psf, "sample_coding -s3 pcm\n") ;
17768 psf_asciiheader_printf (psf, "sample_n_bytes -i 1\n"
17769 "sample_sig_bits -i 8\n") ;
17770 break ;
17772 case SF_FORMAT_PCM_16 :
17773 case SF_FORMAT_PCM_24 :
17774 case SF_FORMAT_PCM_32 :
17775 psf_asciiheader_printf (psf, "sample_n_bytes -i %d\n", psf->bytewidth) ;
17776 psf_asciiheader_printf (psf, "sample_sig_bits -i %d\n", psf->bytewidth * 8) ;
17777 psf_asciiheader_printf (psf, "sample_coding -s3 pcm\n"
17778 "sample_byte_format -s%d %s\n", psf->bytewidth, end_str) ;
17779 break ;
17781 case SF_FORMAT_ALAW :
17782 psf_asciiheader_printf (psf, "sample_coding -s4 alaw\n") ;
17783 psf_asciiheader_printf (psf, "sample_n_bytes -s1 1\n") ;
17784 break ;
17786 case SF_FORMAT_ULAW :
17787 psf_asciiheader_printf (psf, "sample_coding -s4 ulaw\n") ;
17788 psf_asciiheader_printf (psf, "sample_n_bytes -s1 1\n") ;
17789 break ;
17791 default : return SFE_UNIMPLEMENTED ;
17794 psf->dataoffset = NIST_HEADER_LENGTH ;
17796 /* Fix this */
17797 samples = psf->sf.frames ;
17798 psf_asciiheader_printf (psf, "sample_count -i %ld\n", samples) ;
17799 psf_asciiheader_printf (psf, "end_head\n") ;
17801 /* Zero fill to dataoffset. */
17802 psf_binheader_writef (psf, "z", (size_t) (NIST_HEADER_LENGTH - psf->headindex)) ;
17804 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
17806 if (psf->error)
17807 return psf->error ;
17809 if (current > 0)
17810 psf_fseek (psf, current, SEEK_SET) ;
17812 return psf->error ;
17813 } /* nist_write_header */
17817 ** Do not edit or modify anything in this comment block.
17818 ** The arch-tag line is a file identity tag for the GNU Arch
17819 ** revision control system.
17821 ** arch-tag: b45ed85d-9e22-4ad9-b78c-4b58b67152a8
17824 ** Copyright (C) 2002-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
17826 ** This program is free software; you can redistribute it and/or modify
17827 ** it under the terms of the GNU Lesser General Public License as published by
17828 ** the Free Software Foundation; either version 2.1 of the License, or
17829 ** (at your option) any later version.
17831 ** This program is distributed in the hope that it will be useful,
17832 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17833 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17834 ** GNU Lesser General Public License for more details.
17836 ** You should have received a copy of the GNU Lesser General Public License
17837 ** along with this program; if not, write to the Free Software
17838 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17841 #include <stdio.h>
17842 #include <fcntl.h>
17843 #include <string.h>
17844 #include <ctype.h>
17847 #if (ENABLE_EXPERIMENTAL_CODE == 0)
17850 ogg_open (SF_PRIVATE *psf)
17851 { if (psf)
17852 return SFE_UNIMPLEMENTED ;
17853 return (psf && 0) ;
17854 } /* ogg_open */
17856 #else
17858 #define SFE_OGG_NOT_OGG 666
17860 /*------------------------------------------------------------------------------
17861 ** Macros to handle big/little endian issues.
17864 #define ALAW_MARKER MAKE_MARKER ('A', 'L', 'a', 'w')
17865 #define SOUN_MARKER MAKE_MARKER ('S', 'o', 'u', 'n')
17866 #define DFIL_MARKER MAKE_MARKER ('d', 'F', 'i', 'l')
17868 /*------------------------------------------------------------------------------
17869 ** Private static functions.
17872 static int ogg_read_header (SF_PRIVATE *psf) ;
17874 /*------------------------------------------------------------------------------
17875 ** Public function.
17879 ogg_open (SF_PRIVATE *psf)
17880 { int subformat, error = 0 ;
17882 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
17883 return SFE_UNIMPLEMENTED ;
17885 if ((error = ogg_read_header (psf)))
17886 return error ;
17888 if ((psf->sf.format & SF_FORMAT_TYPEMASK) != SF_FORMAT_OGG)
17889 return SFE_BAD_OPEN_FORMAT ;
17891 subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
17893 return error ;
17894 } /* ogg_open */
17896 /*------------------------------------------------------------------------------
17899 static int
17900 ogg_read_header (SF_PRIVATE *psf)
17901 { int marker ;
17903 /* Set position to start of file to begin reading header. */
17904 psf_binheader_readf (psf, "pm", 0, &marker) ;
17905 if (marker != ALAW_MARKER)
17906 return SFE_OGG_NOT_OGG ;
17908 psf_binheader_readf (psf, "m", &marker) ;
17909 if (marker != SOUN_MARKER)
17910 return SFE_OGG_NOT_OGG ;
17912 psf_binheader_readf (psf, "m", &marker) ;
17913 if (marker != DFIL_MARKER)
17914 return SFE_OGG_NOT_OGG ;
17916 psf_log_printf (psf, "Read only : Psion Palmtop Alaw (.wve)\n"
17917 " Sample Rate : 8000\n"
17918 " Channels : 1\n"
17919 " Encoding : A-law\n") ;
17921 psf->dataoffset = 0x20 ;
17922 psf->datalength = psf->filelength - psf->dataoffset ;
17924 psf->sf.format = SF_FORMAT_OGG | SF_FORMAT_ALAW ;
17925 psf->sf.samplerate = 8000 ;
17926 psf->sf.frames = psf->datalength ;
17927 psf->sf.channels = 1 ;
17929 return alaw_init (psf) ;
17930 } /* ogg_read_header */
17932 /*------------------------------------------------------------------------------
17935 #endif
17937 ** Do not edit or modify anything in this comment block.
17938 ** The arch-tag line is a file identity tag for the GNU Arch
17939 ** revision control system.
17941 ** arch-tag: 9ff1fe9c-629e-4e9c-9ef5-3d0eb1e427a0
17944 ** Copyright (C) 1999-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
17946 ** This program is free software; you can redistribute it and/or modify
17947 ** it under the terms of the GNU Lesser General Public License as published by
17948 ** the Free Software Foundation; either version 2.1 of the License, or
17949 ** (at your option) any later version.
17951 ** This program is distributed in the hope that it will be useful,
17952 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17953 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17954 ** GNU Lesser General Public License for more details.
17956 ** You should have received a copy of the GNU Lesser General Public License
17957 ** along with this program; if not, write to the Free Software
17958 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17962 #include <stdio.h>
17963 #include <stdlib.h>
17964 #include <fcntl.h>
17965 #include <string.h>
17966 #include <ctype.h>
17969 /*------------------------------------------------------------------------------
17970 ** Macros to handle big/little endian issues.
17973 #define FAP_MARKER (MAKE_MARKER ('f', 'a', 'p', ' '))
17974 #define PAF_MARKER (MAKE_MARKER (' ', 'p', 'a', 'f'))
17976 /*------------------------------------------------------------------------------
17977 ** Other defines.
17980 #define PAF_HEADER_LENGTH 2048
17982 #define PAF24_SAMPLES_PER_BLOCK 10
17983 #define PAF24_BLOCK_SIZE 32
17985 /*------------------------------------------------------------------------------
17986 ** Typedefs.
17989 typedef struct
17990 { int version ;
17991 int endianness ;
17992 int samplerate ;
17993 int format ;
17994 int channels ;
17995 int source ;
17996 } PAF_FMT ;
17998 typedef struct
17999 { int max_blocks, channels, samplesperblock, blocksize ;
18000 int read_block, write_block, read_count, write_count ;
18001 sf_count_t sample_count ;
18002 int *samples ;
18003 unsigned char *block ;
18004 #if HAVE_FLEXIBLE_ARRAY
18005 int data [] ; /* ISO C99 struct flexible array. */
18006 #else
18007 int data [1] ; /* This is a hack and may not work. */
18008 #endif
18009 } PAF24_PRIVATE ;
18011 /*------------------------------------------------------------------------------
18012 ** Private static functions.
18015 static int paf24_init (SF_PRIVATE *psf) ;
18017 static int paf_read_header (SF_PRIVATE *psf) ;
18018 static int paf_write_header (SF_PRIVATE *psf, int calc_length) ;
18020 static sf_count_t paf24_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
18021 static sf_count_t paf24_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
18022 static sf_count_t paf24_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
18023 static sf_count_t paf24_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
18025 static sf_count_t paf24_write_s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
18026 static sf_count_t paf24_write_i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
18027 static sf_count_t paf24_write_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
18028 static sf_count_t paf24_write_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
18030 static sf_count_t paf24_seek (SF_PRIVATE *psf, int mode, sf_count_t offset) ;
18032 enum
18033 { PAF_PCM_16 = 0,
18034 PAF_PCM_24 = 1,
18035 PAF_PCM_S8 = 2
18038 /*------------------------------------------------------------------------------
18039 ** Public function.
18043 paf_open (SF_PRIVATE *psf)
18044 { int subformat, error, endian ;
18046 psf->dataoffset = PAF_HEADER_LENGTH ;
18048 if (psf->mode == SFM_READ || (psf->mode == SFM_RDWR && psf->filelength > 0))
18049 { if ((error = paf_read_header (psf)))
18050 return error ;
18053 subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
18055 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
18056 { if ((psf->sf.format & SF_FORMAT_TYPEMASK) != SF_FORMAT_PAF)
18057 return SFE_BAD_OPEN_FORMAT ;
18059 endian = psf->sf.format & SF_FORMAT_ENDMASK ;
18061 /* PAF is by default big endian. */
18062 psf->endian = SF_ENDIAN_BIG ;
18064 if (endian == SF_ENDIAN_LITTLE || (CPU_IS_LITTLE_ENDIAN && (endian == SF_ENDIAN_CPU)))
18065 psf->endian = SF_ENDIAN_LITTLE ;
18067 if ((error = paf_write_header (psf, SF_FALSE)))
18068 return error ;
18070 psf->write_header = paf_write_header ;
18073 switch (subformat)
18074 { case SF_FORMAT_PCM_S8 :
18075 psf->bytewidth = 1 ;
18076 error = pcm_init (psf) ;
18077 break ;
18079 case SF_FORMAT_PCM_16 :
18080 psf->bytewidth = 2 ;
18081 error = pcm_init (psf) ;
18082 break ;
18084 case SF_FORMAT_PCM_24 :
18085 /* No bytewidth because of whacky 24 bit encoding. */
18086 error = paf24_init (psf) ;
18087 break ;
18089 default : return SFE_PAF_UNKNOWN_FORMAT ;
18092 return error ;
18093 } /* paf_open */
18095 /*------------------------------------------------------------------------------
18098 static int
18099 paf_read_header (SF_PRIVATE *psf)
18100 { PAF_FMT paf_fmt ;
18101 int marker ;
18103 psf_binheader_readf (psf, "pm", 0, &marker) ;
18105 psf_log_printf (psf, "Signature : '%M'\n", marker) ;
18107 if (marker == PAF_MARKER)
18108 { psf_binheader_readf (psf, "E444444", &(paf_fmt.version), &(paf_fmt.endianness),
18109 &(paf_fmt.samplerate), &(paf_fmt.format), &(paf_fmt.channels), &(paf_fmt.source)) ;
18111 else if (marker == FAP_MARKER)
18112 { psf_binheader_readf (psf, "e444444", &(paf_fmt.version), &(paf_fmt.endianness),
18113 &(paf_fmt.samplerate), &(paf_fmt.format), &(paf_fmt.channels), &(paf_fmt.source)) ;
18115 else
18116 return SFE_PAF_NO_MARKER ;
18118 psf_log_printf (psf, "Version : %d\n", paf_fmt.version) ;
18120 if (paf_fmt.version != 0)
18121 { psf_log_printf (psf, "*** Bad version number. should be zero.\n") ;
18122 return SFE_PAF_VERSION ;
18125 psf_log_printf (psf, "Sample Rate : %d\n", paf_fmt.samplerate) ;
18126 psf_log_printf (psf, "Channels : %d\n", paf_fmt.channels) ;
18128 psf_log_printf (psf, "Endianness : %d => ", paf_fmt.endianness) ;
18129 if (paf_fmt.endianness)
18130 { psf_log_printf (psf, "Little\n", paf_fmt.endianness) ;
18131 psf->endian = SF_ENDIAN_LITTLE ;
18133 else
18134 { psf_log_printf (psf, "Big\n", paf_fmt.endianness) ;
18135 psf->endian = SF_ENDIAN_BIG ;
18138 if (psf->filelength < PAF_HEADER_LENGTH)
18139 return SFE_PAF_SHORT_HEADER ;
18141 psf->datalength = psf->filelength - psf->dataoffset ;
18143 psf_binheader_readf (psf, "p", (int) psf->dataoffset) ;
18145 psf->sf.samplerate = paf_fmt.samplerate ;
18146 psf->sf.channels = paf_fmt.channels ;
18148 /* Only fill in type major. */
18149 psf->sf.format = SF_FORMAT_PAF ;
18151 psf_log_printf (psf, "Format : %d => ", paf_fmt.format) ;
18153 /* PAF is by default big endian. */
18154 psf->sf.format |= paf_fmt.endianness ? SF_ENDIAN_LITTLE : SF_ENDIAN_BIG ;
18156 switch (paf_fmt.format)
18157 { case PAF_PCM_S8 :
18158 psf_log_printf (psf, "8 bit linear PCM\n") ;
18159 psf->bytewidth = 1 ;
18161 psf->sf.format |= SF_FORMAT_PCM_S8 ;
18163 psf->blockwidth = psf->bytewidth * psf->sf.channels ;
18164 psf->sf.frames = psf->datalength / psf->blockwidth ;
18165 break ;
18167 case PAF_PCM_16 :
18168 psf_log_printf (psf, "16 bit linear PCM\n") ;
18169 psf->bytewidth = 2 ;
18171 psf->sf.format |= SF_FORMAT_PCM_16 ;
18173 psf->blockwidth = psf->bytewidth * psf->sf.channels ;
18174 psf->sf.frames = psf->datalength / psf->blockwidth ;
18175 break ;
18177 case PAF_PCM_24 :
18178 psf_log_printf (psf, "24 bit linear PCM\n") ;
18179 psf->bytewidth = 3 ;
18181 psf->sf.format |= SF_FORMAT_PCM_24 ;
18183 psf->blockwidth = 0 ;
18184 psf->sf.frames = PAF24_SAMPLES_PER_BLOCK * psf->datalength /
18185 (PAF24_BLOCK_SIZE * psf->sf.channels) ;
18186 break ;
18188 default : psf_log_printf (psf, "Unknown\n") ;
18189 return SFE_PAF_UNKNOWN_FORMAT ;
18190 break ;
18193 psf_log_printf (psf, "Source : %d => ", paf_fmt.source) ;
18195 switch (paf_fmt.source)
18196 { case 1 : psf_log_printf (psf, "Analog Recording\n") ;
18197 break ;
18198 case 2 : psf_log_printf (psf, "Digital Transfer\n") ;
18199 break ;
18200 case 3 : psf_log_printf (psf, "Multi-track Mixdown\n") ;
18201 break ;
18202 case 5 : psf_log_printf (psf, "Audio Resulting From DSP Processing\n") ;
18203 break ;
18204 default : psf_log_printf (psf, "Unknown\n") ;
18205 break ;
18208 return 0 ;
18209 } /* paf_read_header */
18211 static int
18212 paf_write_header (SF_PRIVATE *psf, int calc_length)
18213 { int paf_format ;
18215 /* PAF header already written so no need to re-write. */
18216 if (psf_ftell (psf) >= PAF_HEADER_LENGTH)
18217 return 0 ;
18219 psf->dataoffset = PAF_HEADER_LENGTH ;
18221 psf->dataoffset = PAF_HEADER_LENGTH ;
18223 /* Prevent compiler warning. */
18224 calc_length = calc_length ;
18226 switch (psf->sf.format & SF_FORMAT_SUBMASK)
18227 { case SF_FORMAT_PCM_S8 :
18228 paf_format = PAF_PCM_S8 ;
18229 break ;
18231 case SF_FORMAT_PCM_16 :
18232 paf_format = PAF_PCM_16 ;
18233 break ;
18235 case SF_FORMAT_PCM_24 :
18236 paf_format = PAF_PCM_24 ;
18237 break ;
18239 default : return SFE_PAF_UNKNOWN_FORMAT ;
18242 /* Reset the current header length to zero. */
18243 psf->header [0] = 0 ;
18244 psf->headindex = 0 ;
18246 if (psf->endian == SF_ENDIAN_BIG)
18247 { /* Marker, version, endianness, samplerate */
18248 psf_binheader_writef (psf, "Em444", PAF_MARKER, 0, 0, psf->sf.samplerate) ;
18249 /* format, channels, source */
18250 psf_binheader_writef (psf, "E444", paf_format, psf->sf.channels, 0) ;
18252 else if (psf->endian == SF_ENDIAN_LITTLE)
18253 { /* Marker, version, endianness, samplerate */
18254 psf_binheader_writef (psf, "em444", FAP_MARKER, 0, 1, psf->sf.samplerate) ;
18255 /* format, channels, source */
18256 psf_binheader_writef (psf, "e444", paf_format, psf->sf.channels, 0) ;
18259 /* Zero fill to dataoffset. */
18260 psf_binheader_writef (psf, "z", (size_t) (psf->dataoffset - psf->headindex)) ;
18262 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
18264 return psf->error ;
18265 } /* paf_write_header */
18267 /*===============================================================================
18268 ** 24 bit PAF files have a really weird encoding.
18269 ** For a mono file, 10 samples (each being 3 bytes) are packed into a 32 byte
18270 ** block. The 8 ints in this 32 byte block are then endian swapped (as ints)
18271 ** if necessary before being written to disk.
18272 ** For a stereo file, blocks of 10 samples from the same channel are encoded
18273 ** into 32 bytes as for the mono case. The 32 byte blocks are then interleaved
18274 ** on disk.
18275 ** Reading has to reverse the above process :-).
18276 ** Weird!!!
18278 ** The code below attempts to gain efficiency while maintaining readability.
18281 static int paf24_read_block (SF_PRIVATE *psf, PAF24_PRIVATE *ppaf24) ;
18282 static int paf24_write_block (SF_PRIVATE *psf, PAF24_PRIVATE *ppaf24) ;
18283 static int paf24_close (SF_PRIVATE *psf) ;
18286 static int
18287 paf24_init (SF_PRIVATE *psf)
18288 { PAF24_PRIVATE *ppaf24 ;
18289 int paf24size, max_blocks ;
18291 paf24size = sizeof (PAF24_PRIVATE) + psf->sf.channels *
18292 (PAF24_BLOCK_SIZE + PAF24_SAMPLES_PER_BLOCK * sizeof (int)) ;
18295 ** Not exatly sure why this needs to be here but the tests
18296 ** fail without it.
18298 psf->last_op = 0 ;
18300 if (! (psf->fdata = malloc (paf24size)))
18301 return SFE_MALLOC_FAILED ;
18303 ppaf24 = (PAF24_PRIVATE*) psf->fdata ;
18304 memset (ppaf24, 0, paf24size) ;
18306 ppaf24->channels = psf->sf.channels ;
18307 ppaf24->samples = ppaf24->data ;
18308 ppaf24->block = (unsigned char*) (ppaf24->data + PAF24_SAMPLES_PER_BLOCK * ppaf24->channels) ;
18310 ppaf24->blocksize = PAF24_BLOCK_SIZE * ppaf24->channels ;
18311 ppaf24->samplesperblock = PAF24_SAMPLES_PER_BLOCK ;
18313 if (psf->mode == SFM_READ || psf->mode == SFM_RDWR)
18314 { paf24_read_block (psf, ppaf24) ; /* Read first block. */
18316 psf->read_short = paf24_read_s ;
18317 psf->read_int = paf24_read_i ;
18318 psf->read_float = paf24_read_f ;
18319 psf->read_double = paf24_read_d ;
18322 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
18323 { psf->write_short = paf24_write_s ;
18324 psf->write_int = paf24_write_i ;
18325 psf->write_float = paf24_write_f ;
18326 psf->write_double = paf24_write_d ;
18329 psf->seek = paf24_seek ;
18330 psf->close = paf24_close ;
18332 psf->filelength = psf_get_filelen (psf) ;
18333 psf->datalength = psf->filelength - psf->dataoffset ;
18335 if (psf->datalength % PAF24_BLOCK_SIZE)
18336 { if (psf->mode == SFM_READ)
18337 psf_log_printf (psf, "*** Warning : file seems to be truncated.\n") ;
18338 max_blocks = psf->datalength / ppaf24->blocksize + 1 ;
18340 else
18341 max_blocks = psf->datalength / ppaf24->blocksize ;
18343 ppaf24->read_block = 0 ;
18344 if (psf->mode == SFM_RDWR)
18345 ppaf24->write_block = max_blocks ;
18346 else
18347 ppaf24->write_block = 0 ;
18349 psf->sf.frames = ppaf24->samplesperblock * max_blocks ;
18350 ppaf24->sample_count = psf->sf.frames ;
18352 return 0 ;
18353 } /* paf24_init */
18355 static sf_count_t
18356 paf24_seek (SF_PRIVATE *psf, int mode, sf_count_t offset)
18357 { PAF24_PRIVATE *ppaf24 ;
18358 int newblock, newsample ;
18360 if (psf->fdata == NULL)
18361 { psf->error = SFE_INTERNAL ;
18362 return SF_SEEK_ERROR ;
18365 ppaf24 = (PAF24_PRIVATE*) psf->fdata ;
18367 if (mode == SFM_READ && ppaf24->write_count > 0)
18368 paf24_write_block (psf, ppaf24) ;
18370 newblock = offset / ppaf24->samplesperblock ;
18371 newsample = offset % ppaf24->samplesperblock ;
18373 switch (mode)
18374 { case SFM_READ :
18375 if (offset > ppaf24->read_block * ppaf24->samplesperblock + ppaf24->read_count)
18376 { psf->error = SFE_BAD_SEEK ;
18377 return SF_SEEK_ERROR ;
18380 if (psf->last_op == SFM_WRITE && ppaf24->write_count)
18381 paf24_write_block (psf, ppaf24) ;
18383 psf_fseek (psf, psf->dataoffset + newblock * ppaf24->blocksize, SEEK_SET) ;
18384 ppaf24->read_block = newblock ;
18385 paf24_read_block (psf, ppaf24) ;
18386 ppaf24->read_count = newsample ;
18387 break ;
18389 case SFM_WRITE :
18390 if (offset > ppaf24->sample_count)
18391 { psf->error = SFE_BAD_SEEK ;
18392 return SF_SEEK_ERROR ;
18395 if (psf->last_op == SFM_WRITE && ppaf24->write_count)
18396 paf24_write_block (psf, ppaf24) ;
18398 psf_fseek (psf, psf->dataoffset + newblock * ppaf24->blocksize, SEEK_SET) ;
18399 ppaf24->write_block = newblock ;
18400 paf24_read_block (psf, ppaf24) ;
18401 ppaf24->write_count = newsample ;
18402 break ;
18404 default :
18405 psf->error = SFE_BAD_SEEK ;
18406 return SF_SEEK_ERROR ;
18409 return newblock * ppaf24->samplesperblock + newsample ;
18410 } /* paf24_seek */
18412 static int
18413 paf24_close (SF_PRIVATE *psf)
18414 { PAF24_PRIVATE *ppaf24 ;
18416 if (psf->fdata == NULL)
18417 return 0 ;
18419 ppaf24 = (PAF24_PRIVATE*) psf->fdata ;
18421 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
18422 { if (ppaf24->write_count > 0)
18423 paf24_write_block (psf, ppaf24) ;
18426 return 0 ;
18427 } /* paf24_close */
18429 /*---------------------------------------------------------------------------
18431 static int
18432 paf24_read_block (SF_PRIVATE *psf, PAF24_PRIVATE *ppaf24)
18433 { int k, channel ;
18434 unsigned char *cptr ;
18436 ppaf24->read_block ++ ;
18437 ppaf24->read_count = 0 ;
18439 if (ppaf24->read_block * ppaf24->samplesperblock > ppaf24->sample_count)
18440 { memset (ppaf24->samples, 0, ppaf24->samplesperblock * ppaf24->channels) ;
18441 return 1 ;
18444 /* Read the block. */
18445 if ((k = psf_fread (ppaf24->block, 1, ppaf24->blocksize, psf)) != ppaf24->blocksize)
18446 psf_log_printf (psf, "*** Warning : short read (%d != %d).\n", k, ppaf24->blocksize) ;
18449 if (CPU_IS_LITTLE_ENDIAN)
18450 { /* Do endian swapping if necessary. */
18451 if (psf->endian == SF_ENDIAN_BIG)
18452 endswap_int_array (ppaf24->data, 8 * ppaf24->channels) ;
18454 /* Unpack block. */
18455 for (k = 0 ; k < PAF24_SAMPLES_PER_BLOCK * ppaf24->channels ; k++)
18456 { channel = k % ppaf24->channels ;
18457 cptr = ppaf24->block + PAF24_BLOCK_SIZE * channel + 3 * (k / ppaf24->channels) ;
18458 ppaf24->samples [k] = (cptr [0] << 8) | (cptr [1] << 16) | (cptr [2] << 24) ;
18461 else
18462 { /* Do endian swapping if necessary. */
18463 if (psf->endian == SF_ENDIAN_BIG)
18464 endswap_int_array (ppaf24->data, 8 * ppaf24->channels) ;
18466 /* Unpack block. */
18467 for (k = 0 ; k < PAF24_SAMPLES_PER_BLOCK * ppaf24->channels ; k++)
18468 { channel = k % ppaf24->channels ;
18469 cptr = ppaf24->block + PAF24_BLOCK_SIZE * channel + 3 * (k / ppaf24->channels) ;
18470 ppaf24->samples [k] = (cptr [0] << 8) | (cptr [1] << 16) | (cptr [2] << 24) ;
18474 return 1 ;
18475 } /* paf24_read_block */
18477 static int
18478 paf24_read (SF_PRIVATE *psf, PAF24_PRIVATE *ppaf24, int *ptr, int len)
18479 { int count, total = 0 ;
18481 while (total < len)
18482 { if (ppaf24->read_block * ppaf24->samplesperblock >= ppaf24->sample_count)
18483 { memset (&(ptr [total]), 0, (len - total) * sizeof (int)) ;
18484 return total ;
18487 if (ppaf24->read_count >= ppaf24->samplesperblock)
18488 paf24_read_block (psf, ppaf24) ;
18490 count = (ppaf24->samplesperblock - ppaf24->read_count) * ppaf24->channels ;
18491 count = (len - total > count) ? count : len - total ;
18493 memcpy (&(ptr [total]), &(ppaf24->samples [ppaf24->read_count * ppaf24->channels]), count * sizeof (int)) ;
18494 total += count ;
18495 ppaf24->read_count += count / ppaf24->channels ;
18498 return total ;
18499 } /* paf24_read */
18501 static sf_count_t
18502 paf24_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
18503 { PAF24_PRIVATE *ppaf24 ;
18504 int *iptr ;
18505 int k, bufferlen, readcount, count ;
18506 sf_count_t total = 0 ;
18508 if (psf->fdata == NULL)
18509 return 0 ;
18510 ppaf24 = (PAF24_PRIVATE*) psf->fdata ;
18512 iptr = (int*) psf->buffer ;
18513 bufferlen = sizeof (psf->buffer) / sizeof (int) ;
18514 while (len > 0)
18515 { readcount = (len >= bufferlen) ? bufferlen : len ;
18516 count = paf24_read (psf, ppaf24, iptr, readcount) ;
18517 for (k = 0 ; k < readcount ; k++)
18518 ptr [total + k] = iptr [k] >> 16 ;
18519 total += count ;
18520 len -= readcount ;
18522 return total ;
18523 } /* paf24_read_s */
18525 static sf_count_t
18526 paf24_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
18527 { PAF24_PRIVATE *ppaf24 ;
18528 int total ;
18530 if (psf->fdata == NULL)
18531 return 0 ;
18532 ppaf24 = (PAF24_PRIVATE*) psf->fdata ;
18534 total = paf24_read (psf, ppaf24, ptr, len) ;
18536 return total ;
18537 } /* paf24_read_i */
18539 static sf_count_t
18540 paf24_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
18541 { PAF24_PRIVATE *ppaf24 ;
18542 int *iptr ;
18543 int k, bufferlen, readcount, count ;
18544 sf_count_t total = 0 ;
18545 float normfact ;
18547 if (psf->fdata == NULL)
18548 return 0 ;
18549 ppaf24 = (PAF24_PRIVATE*) psf->fdata ;
18551 normfact = (psf->norm_float == SF_TRUE) ? (1.0 / 0x80000000) : (1.0 / 0x100) ;
18553 iptr = (int*) psf->buffer ;
18554 bufferlen = sizeof (psf->buffer) / sizeof (int) ;
18555 while (len > 0)
18556 { readcount = (len >= bufferlen) ? bufferlen : len ;
18557 count = paf24_read (psf, ppaf24, iptr, readcount) ;
18558 for (k = 0 ; k < readcount ; k++)
18559 ptr [total + k] = normfact * iptr [k] ;
18560 total += count ;
18561 len -= readcount ;
18563 return total ;
18564 } /* paf24_read_f */
18566 static sf_count_t
18567 paf24_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
18568 { PAF24_PRIVATE *ppaf24 ;
18569 int *iptr ;
18570 int k, bufferlen, readcount, count ;
18571 sf_count_t total = 0 ;
18572 double normfact ;
18574 if (psf->fdata == NULL)
18575 return 0 ;
18576 ppaf24 = (PAF24_PRIVATE*) psf->fdata ;
18578 normfact = (psf->norm_double == SF_TRUE) ? (1.0 / 0x80000000) : (1.0 / 0x100) ;
18580 iptr = (int*) psf->buffer ;
18581 bufferlen = sizeof (psf->buffer) / sizeof (int) ;
18582 while (len > 0)
18583 { readcount = (len >= bufferlen) ? bufferlen : len ;
18584 count = paf24_read (psf, ppaf24, iptr, readcount) ;
18585 for (k = 0 ; k < readcount ; k++)
18586 ptr [total + k] = normfact * iptr [k] ;
18587 total += count ;
18588 len -= readcount ;
18590 return total ;
18591 } /* paf24_read_d */
18593 /*---------------------------------------------------------------------------
18596 static int
18597 paf24_write_block (SF_PRIVATE *psf, PAF24_PRIVATE *ppaf24)
18598 { int k, nextsample, channel ;
18599 unsigned char *cptr ;
18601 /* First pack block. */
18603 if (CPU_IS_LITTLE_ENDIAN)
18604 { for (k = 0 ; k < PAF24_SAMPLES_PER_BLOCK * ppaf24->channels ; k++)
18605 { channel = k % ppaf24->channels ;
18606 cptr = ppaf24->block + PAF24_BLOCK_SIZE * channel + 3 * (k / ppaf24->channels) ;
18607 nextsample = ppaf24->samples [k] >> 8 ;
18608 cptr [0] = nextsample ;
18609 cptr [1] = nextsample >> 8 ;
18610 cptr [2] = nextsample >> 16 ;
18613 /* Do endian swapping if necessary. */
18614 if (psf->endian == SF_ENDIAN_BIG)
18615 endswap_int_array (ppaf24->data, 8 * ppaf24->channels) ;
18617 else if (CPU_IS_BIG_ENDIAN)
18618 { /* This is correct. */
18619 for (k = 0 ; k < PAF24_SAMPLES_PER_BLOCK * ppaf24->channels ; k++)
18620 { channel = k % ppaf24->channels ;
18621 cptr = ppaf24->block + PAF24_BLOCK_SIZE * channel + 3 * (k / ppaf24->channels) ;
18622 nextsample = ppaf24->samples [k] >> 8 ;
18623 cptr [0] = nextsample ;
18624 cptr [1] = nextsample >> 8 ;
18625 cptr [2] = nextsample >> 16 ;
18627 if (psf->endian == SF_ENDIAN_BIG)
18628 endswap_int_array (ppaf24->data, 8 * ppaf24->channels) ;
18631 /* Write block to disk. */
18632 if ((k = psf_fwrite (ppaf24->block, 1, ppaf24->blocksize, psf)) != ppaf24->blocksize)
18633 psf_log_printf (psf, "*** Warning : short write (%d != %d).\n", k, ppaf24->blocksize) ;
18635 if (ppaf24->sample_count < ppaf24->write_block * ppaf24->samplesperblock + ppaf24->write_count)
18636 ppaf24->sample_count = ppaf24->write_block * ppaf24->samplesperblock + ppaf24->write_count ;
18638 if (ppaf24->write_count == ppaf24->samplesperblock)
18639 { ppaf24->write_block ++ ;
18640 ppaf24->write_count = 0 ;
18643 return 1 ;
18644 } /* paf24_write_block */
18646 static int
18647 paf24_write (SF_PRIVATE *psf, PAF24_PRIVATE *ppaf24, int *ptr, int len)
18648 { int count, total = 0 ;
18650 while (total < len)
18651 { count = (ppaf24->samplesperblock - ppaf24->write_count) * ppaf24->channels ;
18653 if (count > len - total)
18654 count = len - total ;
18656 memcpy (&(ppaf24->samples [ppaf24->write_count * ppaf24->channels]), &(ptr [total]), count * sizeof (int)) ;
18657 total += count ;
18658 ppaf24->write_count += count / ppaf24->channels ;
18660 if (ppaf24->write_count >= ppaf24->samplesperblock)
18661 paf24_write_block (psf, ppaf24) ;
18664 return total ;
18665 } /* paf24_write */
18667 static sf_count_t
18668 paf24_write_s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
18669 { PAF24_PRIVATE *ppaf24 ;
18670 int *iptr ;
18671 int k, bufferlen, writecount = 0, count ;
18672 sf_count_t total = 0 ;
18674 if (psf->fdata == NULL)
18675 return 0 ;
18676 ppaf24 = (PAF24_PRIVATE*) psf->fdata ;
18678 iptr = (int*) psf->buffer ;
18679 bufferlen = sizeof (psf->buffer) / sizeof (int) ;
18680 while (len > 0)
18681 { writecount = (len >= bufferlen) ? bufferlen : len ;
18682 for (k = 0 ; k < writecount ; k++)
18683 iptr [k] = ptr [total + k] << 16 ;
18684 count = paf24_write (psf, ppaf24, iptr, writecount) ;
18685 total += count ;
18686 len -= writecount ;
18687 if (count != writecount)
18688 break ;
18690 return total ;
18691 } /* paf24_write_s */
18693 static sf_count_t
18694 paf24_write_i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
18695 { PAF24_PRIVATE *ppaf24 ;
18696 int writecount, count ;
18697 sf_count_t total = 0 ;
18699 if (psf->fdata == NULL)
18700 return 0 ;
18701 ppaf24 = (PAF24_PRIVATE*) psf->fdata ;
18703 while (len > 0)
18704 { writecount = (len > 0x10000000) ? 0x10000000 : (int) len ;
18706 count = paf24_write (psf, ppaf24, ptr, writecount) ;
18708 total += count ;
18709 len -= count ;
18710 if (count != writecount)
18711 break ;
18714 return total ;
18715 } /* paf24_write_i */
18717 static sf_count_t
18718 paf24_write_f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
18719 { PAF24_PRIVATE *ppaf24 ;
18720 int *iptr ;
18721 int k, bufferlen, writecount = 0, count ;
18722 sf_count_t total = 0 ;
18723 float normfact ;
18725 if (psf->fdata == NULL)
18726 return 0 ;
18727 ppaf24 = (PAF24_PRIVATE*) psf->fdata ;
18729 normfact = (psf->norm_float == SF_TRUE) ? (1.0 * 0x7FFFFFFF) : (1.0 / 0x100) ;
18731 iptr = (int*) psf->buffer ;
18732 bufferlen = sizeof (psf->buffer) / sizeof (int) ;
18733 while (len > 0)
18734 { writecount = (len >= bufferlen) ? bufferlen : len ;
18735 for (k = 0 ; k < writecount ; k++)
18736 iptr [k] = lrintf (normfact * ptr [total + k]) ;
18737 count = paf24_write (psf, ppaf24, iptr, writecount) ;
18738 total += count ;
18739 len -= writecount ;
18740 if (count != writecount)
18741 break ;
18744 return total ;
18745 } /* paf24_write_f */
18747 static sf_count_t
18748 paf24_write_d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
18749 { PAF24_PRIVATE *ppaf24 ;
18750 int *iptr ;
18751 int k, bufferlen, writecount = 0, count ;
18752 sf_count_t total = 0 ;
18753 double normfact ;
18755 if (psf->fdata == NULL)
18756 return 0 ;
18757 ppaf24 = (PAF24_PRIVATE*) psf->fdata ;
18759 normfact = (psf->norm_double == SF_TRUE) ? (1.0 * 0x7FFFFFFF) : (1.0 / 0x100) ;
18761 iptr = (int*) psf->buffer ;
18762 bufferlen = sizeof (psf->buffer) / sizeof (int) ;
18763 while (len > 0)
18764 { writecount = (len >= bufferlen) ? bufferlen : len ;
18765 for (k = 0 ; k < writecount ; k++)
18766 iptr [k] = lrint (normfact * ptr [total+k]) ;
18767 count = paf24_write (psf, ppaf24, iptr, writecount) ;
18768 total += count ;
18769 len -= writecount ;
18770 if (count != writecount)
18771 break ;
18774 return total ;
18775 } /* paf24_write_d */
18779 ** Do not edit or modify anything in this comment block.
18780 ** The arch-tag line is a file identity tag for the GNU Arch
18781 ** revision control system.
18783 ** arch-tag: 477a5308-451e-4bbd-bab4-fab6caa4e884
18786 ** Copyright (C) 1999-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
18788 ** This program is free software; you can redistribute it and/or modify
18789 ** it under the terms of the GNU Lesser General Public License as published by
18790 ** the Free Software Foundation; either version 2.1 of the License, or
18791 ** (at your option) any later version.
18793 ** This program is distributed in the hope that it will be useful,
18794 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18795 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18796 ** GNU Lesser General Public License for more details.
18798 ** You should have received a copy of the GNU Lesser General Public License
18799 ** along with this program; if not, write to the Free Software
18800 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18804 /* Need to be able to handle 3 byte (24 bit) integers. So defined a
18805 ** type and use SIZEOF_TRIBYTE instead of (tribyte).
18808 typedef void tribyte ;
18810 #define SIZEOF_TRIBYTE 3
18812 static sf_count_t pcm_read_sc2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
18813 static sf_count_t pcm_read_uc2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
18814 static sf_count_t pcm_read_bes2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
18815 static sf_count_t pcm_read_les2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
18816 static sf_count_t pcm_read_bet2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
18817 static sf_count_t pcm_read_let2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
18818 static sf_count_t pcm_read_bei2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
18819 static sf_count_t pcm_read_lei2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
18821 static sf_count_t pcm_read_sc2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
18822 static sf_count_t pcm_read_uc2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
18823 static sf_count_t pcm_read_bes2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
18824 static sf_count_t pcm_read_les2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
18825 static sf_count_t pcm_read_bet2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
18826 static sf_count_t pcm_read_let2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
18827 static sf_count_t pcm_read_bei2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
18828 static sf_count_t pcm_read_lei2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
18830 static sf_count_t pcm_read_sc2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
18831 static sf_count_t pcm_read_uc2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
18832 static sf_count_t pcm_read_bes2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
18833 static sf_count_t pcm_read_les2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
18834 static sf_count_t pcm_read_bet2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
18835 static sf_count_t pcm_read_let2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
18836 static sf_count_t pcm_read_bei2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
18837 static sf_count_t pcm_read_lei2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
18839 static sf_count_t pcm_read_sc2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
18840 static sf_count_t pcm_read_uc2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
18841 static sf_count_t pcm_read_bes2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
18842 static sf_count_t pcm_read_les2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
18843 static sf_count_t pcm_read_bet2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
18844 static sf_count_t pcm_read_let2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
18845 static sf_count_t pcm_read_bei2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
18846 static sf_count_t pcm_read_lei2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
18849 static sf_count_t pcm_write_s2sc (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
18850 static sf_count_t pcm_write_s2uc (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
18851 static sf_count_t pcm_write_s2bes (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
18852 static sf_count_t pcm_write_s2les (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
18853 static sf_count_t pcm_write_s2bet (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
18854 static sf_count_t pcm_write_s2let (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
18855 static sf_count_t pcm_write_s2bei (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
18856 static sf_count_t pcm_write_s2lei (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
18858 static sf_count_t pcm_write_i2sc (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
18859 static sf_count_t pcm_write_i2uc (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
18860 static sf_count_t pcm_write_i2bes (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
18861 static sf_count_t pcm_write_i2les (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
18862 static sf_count_t pcm_write_i2bet (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
18863 static sf_count_t pcm_write_i2let (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
18864 static sf_count_t pcm_write_i2bei (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
18865 static sf_count_t pcm_write_i2lei (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
18867 static sf_count_t pcm_write_f2sc (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
18868 static sf_count_t pcm_write_f2uc (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
18869 static sf_count_t pcm_write_f2bes (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
18870 static sf_count_t pcm_write_f2les (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
18871 static sf_count_t pcm_write_f2bet (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
18872 static sf_count_t pcm_write_f2let (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
18873 static sf_count_t pcm_write_f2bei (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
18874 static sf_count_t pcm_write_f2lei (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
18876 static sf_count_t pcm_write_d2sc (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
18877 static sf_count_t pcm_write_d2uc (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
18878 static sf_count_t pcm_write_d2bes (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
18879 static sf_count_t pcm_write_d2les (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
18880 static sf_count_t pcm_write_d2bet (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
18881 static sf_count_t pcm_write_d2let (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
18882 static sf_count_t pcm_write_d2bei (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
18883 static sf_count_t pcm_write_d2lei (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
18885 static void sc2s_array (signed char *src, int count, short *dest) ;
18886 static void uc2s_array (unsigned char *src, int count, short *dest) ;
18887 static void bet2s_array (tribyte *src, int count, short *dest) ;
18888 static void let2s_array (tribyte *src, int count, short *dest) ;
18889 static void bei2s_array (int *src, int count, short *dest) ;
18890 static void lei2s_array (int *src, int count, short *dest) ;
18892 static void sc2i_array (signed char *src, int count, int *dest) ;
18893 static void uc2i_array (unsigned char *src, int count, int *dest) ;
18894 static void bes2i_array (short *src, int count, int *dest) ;
18895 static void les2i_array (short *src, int count, int *dest) ;
18896 static void bet2i_array (tribyte *src, int count, int *dest) ;
18897 static void let2i_array (tribyte *src, int count, int *dest) ;
18899 static void sc2f_array (signed char *src, int count, float *dest, float normfact) ;
18900 static void uc2f_array (unsigned char *src, int count, float *dest, float normfact) ;
18901 static void bes2f_array (short *src, int count, float *dest, float normfact) ;
18902 static void les2f_array (short *src, int count, float *dest, float normfact) ;
18903 static void bet2f_array (tribyte *src, int count, float *dest, float normfact) ;
18904 static void let2f_array (tribyte *src, int count, float *dest, float normfact) ;
18905 static void bei2f_array (int *src, int count, float *dest, float normfact) ;
18906 static void lei2f_array (int *src, int count, float *dest, float normfact) ;
18908 static void sc2d_array (signed char *src, int count, double *dest, double normfact) ;
18909 static void uc2d_array (unsigned char *src, int count, double *dest, double normfact) ;
18910 static void bes2d_array (short *src, int count, double *dest, double normfact) ;
18911 static void les2d_array (short *src, int count, double *dest, double normfact) ;
18912 static void bet2d_array (tribyte *src, int count, double *dest, double normfact) ;
18913 static void let2d_array (tribyte *src, int count, double *dest, double normfact) ;
18914 static void bei2d_array (int *src, int count, double *dest, double normfact) ;
18915 static void lei2d_array (int *src, int count, double *dest, double normfact) ;
18917 static void s2sc_array (short *src, signed char *dest, int count) ;
18918 static void s2uc_array (short *src, unsigned char *dest, int count) ;
18919 static void s2bet_array (short *src, tribyte *dest, int count) ;
18920 static void s2let_array (short *src, tribyte *dest, int count) ;
18921 static void s2bei_array (short *src, int *dest, int count) ;
18922 static void s2lei_array (short *src, int *dest, int count) ;
18924 static void i2sc_array (int *src, signed char *dest, int count) ;
18925 static void i2uc_array (int *src, unsigned char *dest, int count) ;
18926 static void i2bes_array (int *src, short *dest, int count) ;
18927 static void i2les_array (int *src, short *dest, int count) ;
18928 static void i2bet_array (int *src, tribyte *dest, int count) ;
18929 static void i2let_array (int *src, tribyte *dest, int count) ;
18931 /*-----------------------------------------------------------------------------------------------
18934 enum
18935 { /* Char type for 8 bit files. */
18936 SF_CHARS_SIGNED = 200,
18937 SF_CHARS_UNSIGNED = 201
18940 /*-----------------------------------------------------------------------------------------------
18944 pcm_init (SF_PRIVATE *psf)
18945 { int chars = 0 ;
18947 if (psf->bytewidth == 0 || psf->sf.channels == 0)
18948 return SFE_INTERNAL ;
18951 psf->blockwidth = psf->bytewidth * psf->sf.channels ;
18953 if ((psf->sf.format & SF_FORMAT_SUBMASK) == SF_FORMAT_PCM_S8)
18954 chars = SF_CHARS_SIGNED ;
18955 else if ((psf->sf.format & SF_FORMAT_SUBMASK) == SF_FORMAT_PCM_U8)
18956 chars = SF_CHARS_UNSIGNED ;
18958 if (psf->mode == SFM_READ || psf->mode == SFM_RDWR)
18959 { switch (psf->bytewidth * 0x10000 + psf->endian + chars)
18960 { case (0x10000 + SF_ENDIAN_BIG + SF_CHARS_SIGNED) :
18961 case (0x10000 + SF_ENDIAN_LITTLE + SF_CHARS_SIGNED) :
18962 psf->read_short = pcm_read_sc2s ;
18963 psf->read_int = pcm_read_sc2i ;
18964 psf->read_float = pcm_read_sc2f ;
18965 psf->read_double = pcm_read_sc2d ;
18966 break ;
18967 case (0x10000 + SF_ENDIAN_BIG + SF_CHARS_UNSIGNED) :
18968 case (0x10000 + SF_ENDIAN_LITTLE + SF_CHARS_UNSIGNED) :
18969 psf->read_short = pcm_read_uc2s ;
18970 psf->read_int = pcm_read_uc2i ;
18971 psf->read_float = pcm_read_uc2f ;
18972 psf->read_double = pcm_read_uc2d ;
18973 break ;
18975 case (2 * 0x10000 + SF_ENDIAN_BIG) :
18976 psf->read_short = pcm_read_bes2s ;
18977 psf->read_int = pcm_read_bes2i ;
18978 psf->read_float = pcm_read_bes2f ;
18979 psf->read_double = pcm_read_bes2d ;
18980 break ;
18981 case (3 * 0x10000 + SF_ENDIAN_BIG) :
18982 psf->read_short = pcm_read_bet2s ;
18983 psf->read_int = pcm_read_bet2i ;
18984 psf->read_float = pcm_read_bet2f ;
18985 psf->read_double = pcm_read_bet2d ;
18986 break ;
18987 case (4 * 0x10000 + SF_ENDIAN_BIG) :
18988 psf->read_short = pcm_read_bei2s ;
18989 psf->read_int = pcm_read_bei2i ;
18990 psf->read_float = pcm_read_bei2f ;
18991 psf->read_double = pcm_read_bei2d ;
18992 break ;
18994 case (2 * 0x10000 + SF_ENDIAN_LITTLE) :
18995 psf->read_short = pcm_read_les2s ;
18996 psf->read_int = pcm_read_les2i ;
18997 psf->read_float = pcm_read_les2f ;
18998 psf->read_double = pcm_read_les2d ;
18999 break ;
19000 case (3 * 0x10000 + SF_ENDIAN_LITTLE) :
19001 psf->read_short = pcm_read_let2s ;
19002 psf->read_int = pcm_read_let2i ;
19003 psf->read_float = pcm_read_let2f ;
19004 psf->read_double = pcm_read_let2d ;
19005 break ;
19006 case (4 * 0x10000 + SF_ENDIAN_LITTLE) :
19007 psf->read_short = pcm_read_lei2s ;
19008 psf->read_int = pcm_read_lei2i ;
19009 psf->read_float = pcm_read_lei2f ;
19010 psf->read_double = pcm_read_lei2d ;
19011 break ;
19012 default :
19013 psf_log_printf (psf, "pcm.c returning SFE_UNIMPLEMENTED\n") ;
19014 return SFE_UNIMPLEMENTED ;
19018 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
19019 { switch (psf->bytewidth * 0x10000 + psf->endian + chars)
19020 { case (0x10000 + SF_ENDIAN_BIG + SF_CHARS_SIGNED) :
19021 case (0x10000 + SF_ENDIAN_LITTLE + SF_CHARS_SIGNED) :
19022 psf->write_short = pcm_write_s2sc ;
19023 psf->write_int = pcm_write_i2sc ;
19024 psf->write_float = pcm_write_f2sc ;
19025 psf->write_double = pcm_write_d2sc ;
19026 break ;
19027 case (0x10000 + SF_ENDIAN_BIG + SF_CHARS_UNSIGNED) :
19028 case (0x10000 + SF_ENDIAN_LITTLE + SF_CHARS_UNSIGNED) :
19029 psf->write_short = pcm_write_s2uc ;
19030 psf->write_int = pcm_write_i2uc ;
19031 psf->write_float = pcm_write_f2uc ;
19032 psf->write_double = pcm_write_d2uc ;
19033 break ;
19035 case (2 * 0x10000 + SF_ENDIAN_BIG) :
19036 psf->write_short = pcm_write_s2bes ;
19037 psf->write_int = pcm_write_i2bes ;
19038 psf->write_float = pcm_write_f2bes ;
19039 psf->write_double = pcm_write_d2bes ;
19040 break ;
19042 case (3 * 0x10000 + SF_ENDIAN_BIG) :
19043 psf->write_short = pcm_write_s2bet ;
19044 psf->write_int = pcm_write_i2bet ;
19045 psf->write_float = pcm_write_f2bet ;
19046 psf->write_double = pcm_write_d2bet ;
19047 break ;
19049 case (4 * 0x10000 + SF_ENDIAN_BIG) :
19050 psf->write_short = pcm_write_s2bei ;
19051 psf->write_int = pcm_write_i2bei ;
19052 psf->write_float = pcm_write_f2bei ;
19053 psf->write_double = pcm_write_d2bei ;
19054 break ;
19056 case (2 * 0x10000 + SF_ENDIAN_LITTLE) :
19057 psf->write_short = pcm_write_s2les ;
19058 psf->write_int = pcm_write_i2les ;
19059 psf->write_float = pcm_write_f2les ;
19060 psf->write_double = pcm_write_d2les ;
19061 break ;
19063 case (3 * 0x10000 + SF_ENDIAN_LITTLE) :
19064 psf->write_short = pcm_write_s2let ;
19065 psf->write_int = pcm_write_i2let ;
19066 psf->write_float = pcm_write_f2let ;
19067 psf->write_double = pcm_write_d2let ;
19068 break ;
19070 case (4 * 0x10000 + SF_ENDIAN_LITTLE) :
19071 psf->write_short = pcm_write_s2lei ;
19072 psf->write_int = pcm_write_i2lei ;
19073 psf->write_float = pcm_write_f2lei ;
19074 psf->write_double = pcm_write_d2lei ;
19075 break ;
19077 default :
19078 psf_log_printf (psf, "pcm.c returning SFE_UNIMPLEMENTED\n") ;
19079 return SFE_UNIMPLEMENTED ;
19084 if (psf->filelength > psf->dataoffset)
19085 { psf->datalength = (psf->dataend > 0) ? psf->dataend - psf->dataoffset :
19086 psf->filelength - psf->dataoffset ;
19088 else
19089 psf->datalength = 0 ;
19091 psf->sf.frames = psf->datalength / psf->blockwidth ;
19093 return 0 ;
19094 } /* pcm_init */
19096 /*-----------------------------------------------------------------------------------------------
19099 static sf_count_t
19100 pcm_read_sc2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
19101 { int bufferlen, readcount, thisread ;
19102 sf_count_t total = 0 ;
19104 bufferlen = sizeof (psf->buffer) / sizeof (signed char) ;
19106 while (len > 0)
19107 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
19108 thisread = psf_fread (psf->buffer, sizeof (signed char), readcount, psf) ;
19109 sc2s_array ((signed char*) (psf->buffer), thisread, ptr + total) ;
19110 total += thisread ;
19111 len -= thisread ;
19112 if (thisread < readcount)
19113 break ;
19116 return total ;
19117 } /* pcm_read_sc2s */
19119 static sf_count_t
19120 pcm_read_uc2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
19121 { int bufferlen, readcount, thisread ;
19122 sf_count_t total = 0 ;
19124 bufferlen = sizeof (psf->buffer) / sizeof (unsigned char) ;
19126 while (len > 0)
19127 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
19128 thisread = psf_fread (psf->buffer, sizeof (unsigned char), readcount, psf) ;
19129 uc2s_array ((unsigned char*) (psf->buffer), thisread, ptr + total) ;
19130 total += thisread ;
19131 len -= thisread ;
19132 if (thisread < readcount)
19133 break ;
19136 return total ;
19137 } /* pcm_read_uc2s */
19139 static sf_count_t
19140 pcm_read_bes2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
19141 { int total ;
19143 total = psf_fread (ptr, sizeof (short), len, psf) ;
19144 if (CPU_IS_LITTLE_ENDIAN)
19145 endswap_short_array (ptr, len) ;
19147 return total ;
19148 } /* pcm_read_bes2s */
19150 static sf_count_t
19151 pcm_read_les2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
19152 { int total ;
19154 total = psf_fread (ptr, sizeof (short), len, psf) ;
19155 if (CPU_IS_BIG_ENDIAN)
19156 endswap_short_array (ptr, len) ;
19158 return total ;
19159 } /* pcm_read_les2s */
19161 static sf_count_t
19162 pcm_read_bet2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
19163 { int bufferlen, readcount, thisread ;
19164 sf_count_t total = 0 ;
19166 bufferlen = sizeof (psf->buffer) / SIZEOF_TRIBYTE ;
19168 while (len > 0)
19169 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
19170 thisread = psf_fread (psf->buffer, SIZEOF_TRIBYTE, readcount, psf) ;
19171 bet2s_array ((tribyte*) (psf->buffer), thisread, ptr + total) ;
19172 total += thisread ;
19173 len -= thisread ;
19174 if (thisread < readcount)
19175 break ;
19178 return total ;
19179 } /* pcm_read_bet2s */
19181 static sf_count_t
19182 pcm_read_let2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
19183 { int bufferlen, readcount, thisread ;
19184 sf_count_t total = 0 ;
19186 bufferlen = sizeof (psf->buffer) / SIZEOF_TRIBYTE ;
19188 while (len > 0)
19189 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
19190 thisread = psf_fread (psf->buffer, SIZEOF_TRIBYTE, readcount, psf) ;
19191 let2s_array ((tribyte*) (psf->buffer), thisread, ptr + total) ;
19192 total += thisread ;
19193 len -= thisread ;
19194 if (thisread < readcount)
19195 break ;
19198 return total ;
19199 } /* pcm_read_let2s */
19201 static sf_count_t
19202 pcm_read_bei2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
19203 { int bufferlen, readcount, thisread ;
19204 sf_count_t total = 0 ;
19206 bufferlen = sizeof (psf->buffer) / sizeof (int) ;
19208 while (len > 0)
19209 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
19210 thisread = psf_fread (psf->buffer, sizeof (int), readcount, psf) ;
19211 bei2s_array ((int*) (psf->buffer), thisread, ptr + total) ;
19212 total += thisread ;
19213 len -= thisread ;
19214 if (thisread < readcount)
19215 break ;
19218 return total ;
19219 } /* pcm_read_bei2s */
19221 static sf_count_t
19222 pcm_read_lei2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
19223 { int bufferlen, readcount, thisread ;
19224 sf_count_t total = 0 ;
19226 bufferlen = sizeof (psf->buffer) / sizeof (int) ;
19228 while (len > 0)
19229 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
19230 thisread = psf_fread (psf->buffer, sizeof (int), readcount, psf) ;
19231 lei2s_array ((int*) (psf->buffer), thisread, ptr + total) ;
19232 total += thisread ;
19233 len -= thisread ;
19234 if (thisread < readcount)
19235 break ;
19238 return total ;
19239 } /* pcm_read_lei2s */
19241 /*-----------------------------------------------------------------------------------------------
19244 static sf_count_t
19245 pcm_read_sc2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
19246 { int bufferlen, readcount, thisread ;
19247 sf_count_t total = 0 ;
19249 bufferlen = sizeof (psf->buffer) / sizeof (signed char) ;
19251 while (len > 0)
19252 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
19253 thisread = psf_fread (psf->buffer, sizeof (signed char), readcount, psf) ;
19254 sc2i_array ((signed char*) (psf->buffer), thisread, ptr + total) ;
19255 total += thisread ;
19256 len -= thisread ;
19257 if (thisread < readcount)
19258 break ;
19261 return total ;
19262 } /* pcm_read_sc2i */
19264 static sf_count_t
19265 pcm_read_uc2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
19266 { int bufferlen, readcount, thisread ;
19267 sf_count_t total = 0 ;
19269 bufferlen = sizeof (psf->buffer) / sizeof (unsigned char) ;
19271 while (len > 0)
19272 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
19273 thisread = psf_fread (psf->buffer, sizeof (unsigned char), readcount, psf) ;
19274 uc2i_array ((unsigned char*) (psf->buffer), thisread, ptr + total) ;
19275 total += thisread ;
19276 len -= thisread ;
19277 if (thisread < readcount)
19278 break ;
19281 return total ;
19282 } /* pcm_read_uc2i */
19284 static sf_count_t
19285 pcm_read_bes2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
19286 { int bufferlen, readcount, thisread ;
19287 sf_count_t total = 0 ;
19289 bufferlen = sizeof (psf->buffer) / sizeof (short) ;
19291 while (len > 0)
19292 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
19293 thisread = psf_fread (psf->buffer, sizeof (short), readcount, psf) ;
19294 bes2i_array ((short*) (psf->buffer), thisread, ptr + total) ;
19295 total += thisread ;
19296 len -= thisread ;
19297 if (thisread < readcount)
19298 break ;
19301 return total ;
19302 } /* pcm_read_bes2i */
19304 static sf_count_t
19305 pcm_read_les2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
19306 { int bufferlen, readcount, thisread ;
19307 sf_count_t total = 0 ;
19309 bufferlen = sizeof (psf->buffer) / sizeof (short) ;
19311 while (len > 0)
19312 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
19313 thisread = psf_fread (psf->buffer, sizeof (short), readcount, psf) ;
19314 les2i_array ((short*) (psf->buffer), thisread, ptr + total) ;
19315 total += thisread ;
19316 len -= thisread ;
19317 if (thisread < readcount)
19318 break ;
19321 return total ;
19322 } /* pcm_read_les2i */
19324 static sf_count_t
19325 pcm_read_bet2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
19326 { int bufferlen, readcount, thisread ;
19327 sf_count_t total = 0 ;
19329 bufferlen = sizeof (psf->buffer) / SIZEOF_TRIBYTE ;
19331 while (len > 0)
19332 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
19333 thisread = psf_fread (psf->buffer, SIZEOF_TRIBYTE, readcount, psf) ;
19334 bet2i_array ((tribyte*) (psf->buffer), thisread, ptr + total) ;
19335 total += thisread ;
19336 len -= thisread ;
19337 if (thisread < readcount)
19338 break ;
19341 return total ;
19342 } /* pcm_read_bet2i */
19344 static sf_count_t
19345 pcm_read_let2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
19346 { int bufferlen, readcount, thisread ;
19347 sf_count_t total = 0 ;
19349 bufferlen = sizeof (psf->buffer) / SIZEOF_TRIBYTE ;
19351 while (len > 0)
19352 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
19353 thisread = psf_fread (psf->buffer, SIZEOF_TRIBYTE, readcount, psf) ;
19354 let2i_array ((tribyte*) (psf->buffer), thisread, ptr + total) ;
19355 total += thisread ;
19356 len -= thisread ;
19357 if (thisread < readcount)
19358 break ;
19361 return total ;
19362 } /* pcm_read_let2i */
19364 static sf_count_t
19365 pcm_read_bei2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
19366 { int total ;
19368 total = psf_fread (ptr, sizeof (int), len, psf) ;
19369 if (CPU_IS_LITTLE_ENDIAN)
19370 endswap_int_array (ptr, len) ;
19372 return total ;
19373 } /* pcm_read_bei2i */
19375 static sf_count_t
19376 pcm_read_lei2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
19377 { int total ;
19379 total = psf_fread (ptr, sizeof (int), len, psf) ;
19380 if (CPU_IS_BIG_ENDIAN)
19381 endswap_int_array (ptr, len) ;
19383 return total ;
19384 } /* pcm_read_lei2i */
19386 /*-----------------------------------------------------------------------------------------------
19389 static sf_count_t
19390 pcm_read_sc2f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
19391 { int bufferlen, readcount, thisread ;
19392 sf_count_t total = 0 ;
19393 float normfact ;
19395 normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x80) : 1.0 ;
19397 bufferlen = sizeof (psf->buffer) / sizeof (signed char) ;
19399 while (len > 0)
19400 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
19401 thisread = psf_fread (psf->buffer, sizeof (signed char), readcount, psf) ;
19402 sc2f_array ((signed char*) (psf->buffer), thisread, ptr + total, normfact) ;
19403 total += thisread ;
19404 len -= thisread ;
19405 if (thisread < readcount)
19406 break ;
19409 return total ;
19410 } /* pcm_read_sc2f */
19412 static sf_count_t
19413 pcm_read_uc2f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
19414 { int bufferlen, readcount, thisread ;
19415 sf_count_t total = 0 ;
19416 float normfact ;
19418 normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x80) : 1.0 ;
19420 bufferlen = sizeof (psf->buffer) / sizeof (unsigned char) ;
19422 while (len > 0)
19423 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
19424 thisread = psf_fread (psf->buffer, sizeof (unsigned char), readcount, psf) ;
19425 uc2f_array ((unsigned char*) (psf->buffer), thisread, ptr + total, normfact) ;
19426 total += thisread ;
19427 len -= thisread ;
19428 if (thisread < readcount)
19429 break ;
19432 return total ;
19433 } /* pcm_read_uc2f */
19435 static sf_count_t
19436 pcm_read_bes2f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
19437 { int bufferlen, readcount, thisread ;
19438 sf_count_t total = 0 ;
19439 float normfact ;
19441 normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x8000) : 1.0 ;
19443 bufferlen = sizeof (psf->buffer) / sizeof (short) ;
19445 while (len > 0)
19446 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
19447 thisread = psf_fread (psf->buffer, sizeof (short), readcount, psf) ;
19448 bes2f_array ((short*) (psf->buffer), thisread, ptr + total, normfact) ;
19449 total += thisread ;
19450 len -= thisread ;
19451 if (thisread < readcount)
19452 break ;
19455 return total ;
19456 } /* pcm_read_bes2f */
19458 static sf_count_t
19459 pcm_read_les2f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
19460 { int bufferlen, readcount, thisread ;
19461 sf_count_t total = 0 ;
19462 float normfact ;
19464 normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x8000) : 1.0 ;
19466 bufferlen = sizeof (psf->buffer) / sizeof (short) ;
19468 while (len > 0)
19469 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
19470 thisread = psf_fread (psf->buffer, sizeof (short), readcount, psf) ;
19471 les2f_array ((short*) (psf->buffer), thisread, ptr + total, normfact) ;
19472 total += thisread ;
19473 len -= thisread ;
19474 if (thisread < readcount)
19475 break ;
19478 return total ;
19479 } /* pcm_read_les2f */
19481 static sf_count_t
19482 pcm_read_bet2f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
19483 { int bufferlen, readcount, thisread ;
19484 sf_count_t total = 0 ;
19485 float normfact ;
19487 /* Special normfactor because tribyte value is read into an int. */
19488 normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x80000000) : 1.0 / 256.0 ;
19490 bufferlen = sizeof (psf->buffer) / SIZEOF_TRIBYTE ;
19492 while (len > 0)
19493 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
19494 thisread = psf_fread (psf->buffer, SIZEOF_TRIBYTE, readcount, psf) ;
19495 bet2f_array ((tribyte*) (psf->buffer), thisread, ptr + total, normfact) ;
19496 total += thisread ;
19497 len -= thisread ;
19498 if (thisread < readcount)
19499 break ;
19502 return total ;
19503 } /* pcm_read_bet2f */
19505 static sf_count_t
19506 pcm_read_let2f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
19507 { int bufferlen, readcount, thisread ;
19508 sf_count_t total = 0 ;
19509 float normfact ;
19511 /* Special normfactor because tribyte value is read into an int. */
19512 normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x80000000) : 1.0 / 256.0 ;
19514 bufferlen = sizeof (psf->buffer) / SIZEOF_TRIBYTE ;
19516 while (len > 0)
19517 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
19518 thisread = psf_fread (psf->buffer, SIZEOF_TRIBYTE, readcount, psf) ;
19519 let2f_array ((tribyte*) (psf->buffer), thisread, ptr + total, normfact) ;
19520 total += thisread ;
19521 len -= thisread ;
19522 if (thisread < readcount)
19523 break ;
19526 return total ;
19527 } /* pcm_read_let2f */
19529 static sf_count_t
19530 pcm_read_bei2f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
19531 { int bufferlen, readcount, thisread ;
19532 sf_count_t total = 0 ;
19533 float normfact ;
19535 normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x80000000) : 1.0 ;
19537 bufferlen = sizeof (psf->buffer) / sizeof (int) ;
19539 while (len > 0)
19540 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
19541 thisread = psf_fread (psf->buffer, sizeof (int), readcount, psf) ;
19542 bei2f_array ((int*) (psf->buffer), thisread, ptr + total, normfact) ;
19543 total += thisread ;
19544 len -= thisread ;
19545 if (thisread < readcount)
19546 break ;
19549 return total ;
19550 } /* pcm_read_bei2f */
19552 static sf_count_t
19553 pcm_read_lei2f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
19554 { int bufferlen, readcount, thisread ;
19555 sf_count_t total = 0 ;
19556 float normfact ;
19558 normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x80000000) : 1.0 ;
19560 bufferlen = sizeof (psf->buffer) / sizeof (int) ;
19562 while (len > 0)
19563 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
19564 thisread = psf_fread (psf->buffer, sizeof (int), readcount, psf) ;
19565 lei2f_array ((int*) (psf->buffer), thisread, ptr + total, normfact) ;
19566 total += thisread ;
19567 len -= thisread ;
19568 if (thisread < readcount)
19569 break ;
19572 return total ;
19573 } /* pcm_read_lei2f */
19575 /*-----------------------------------------------------------------------------------------------
19578 static sf_count_t
19579 pcm_read_sc2d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
19580 { int bufferlen, readcount, thisread ;
19581 sf_count_t total = 0 ;
19582 double normfact ;
19584 normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x80) : 1.0 ;
19586 bufferlen = sizeof (psf->buffer) / sizeof (signed char) ;
19588 while (len > 0)
19589 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
19590 thisread = psf_fread (psf->buffer, sizeof (signed char), readcount, psf) ;
19591 sc2d_array ((signed char*) (psf->buffer), thisread, ptr + total, normfact) ;
19592 total += thisread ;
19593 len -= thisread ;
19594 if (thisread < readcount)
19595 break ;
19598 return total ;
19599 } /* pcm_read_sc2d */
19601 static sf_count_t
19602 pcm_read_uc2d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
19603 { int bufferlen, readcount, thisread ;
19604 sf_count_t total = 0 ;
19605 double normfact ;
19607 normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x80) : 1.0 ;
19609 bufferlen = sizeof (psf->buffer) / sizeof (unsigned char) ;
19611 while (len > 0)
19612 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
19613 thisread = psf_fread (psf->buffer, sizeof (unsigned char), readcount, psf) ;
19614 uc2d_array ((unsigned char*) (psf->buffer), thisread, ptr + total, normfact) ;
19615 total += thisread ;
19616 len -= thisread ;
19617 if (thisread < readcount)
19618 break ;
19621 return total ;
19622 } /* pcm_read_uc2d */
19624 static sf_count_t
19625 pcm_read_bes2d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
19626 { int bufferlen, readcount, thisread ;
19627 sf_count_t total = 0 ;
19628 double normfact ;
19630 normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x8000) : 1.0 ;
19632 bufferlen = sizeof (psf->buffer) / sizeof (short) ;
19634 while (len > 0)
19635 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
19636 thisread = psf_fread (psf->buffer, sizeof (short), readcount, psf) ;
19637 bes2d_array ((short*) (psf->buffer), thisread, ptr + total, normfact) ;
19638 total += thisread ;
19639 len -= thisread ;
19640 if (thisread < readcount)
19641 break ;
19644 return total ;
19645 } /* pcm_read_bes2d */
19647 static sf_count_t
19648 pcm_read_les2d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
19649 { int bufferlen, readcount, thisread ;
19650 sf_count_t total = 0 ;
19651 double normfact ;
19653 normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x8000) : 1.0 ;
19655 bufferlen = sizeof (psf->buffer) / sizeof (short) ;
19657 while (len > 0)
19658 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
19659 thisread = psf_fread (psf->buffer, sizeof (short), readcount, psf) ;
19660 les2d_array ((short*) (psf->buffer), thisread, ptr + total, normfact) ;
19661 total += thisread ;
19662 len -= thisread ;
19663 if (thisread < readcount)
19664 break ;
19667 return total ;
19668 } /* pcm_read_les2d */
19670 static sf_count_t
19671 pcm_read_bet2d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
19672 { int bufferlen, readcount, thisread ;
19673 sf_count_t total = 0 ;
19674 double normfact ;
19676 normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x80000000) : 1.0 / 256.0 ;
19678 bufferlen = sizeof (psf->buffer) / SIZEOF_TRIBYTE ;
19680 while (len > 0)
19681 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
19682 thisread = psf_fread (psf->buffer, SIZEOF_TRIBYTE, readcount, psf) ;
19683 bet2d_array ((tribyte*) (psf->buffer), thisread, ptr + total, normfact) ;
19684 total += thisread ;
19685 len -= thisread ;
19686 if (thisread < readcount)
19687 break ;
19690 return total ;
19691 } /* pcm_read_bet2d */
19693 static sf_count_t
19694 pcm_read_let2d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
19695 { int bufferlen, readcount, thisread ;
19696 sf_count_t total = 0 ;
19697 double normfact ;
19699 /* Special normfactor because tribyte value is read into an int. */
19700 normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x80000000) : 1.0 / 256.0 ;
19702 bufferlen = sizeof (psf->buffer) / SIZEOF_TRIBYTE ;
19704 while (len > 0)
19705 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
19706 thisread = psf_fread (psf->buffer, SIZEOF_TRIBYTE, readcount, psf) ;
19707 let2d_array ((tribyte*) (psf->buffer), thisread, ptr + total, normfact) ;
19708 total += thisread ;
19709 len -= thisread ;
19710 if (thisread < readcount)
19711 break ;
19714 return total ;
19715 } /* pcm_read_let2d */
19717 static sf_count_t
19718 pcm_read_bei2d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
19719 { int bufferlen, readcount, thisread ;
19720 sf_count_t total = 0 ;
19721 double normfact ;
19723 normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x80000000) : 1.0 ;
19725 bufferlen = sizeof (psf->buffer) / sizeof (int) ;
19727 while (len > 0)
19728 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
19729 thisread = psf_fread (psf->buffer, sizeof (int), readcount, psf) ;
19730 bei2d_array ((int*) (psf->buffer), thisread, ptr + total, normfact) ;
19731 total += thisread ;
19732 len -= thisread ;
19733 if (thisread < readcount)
19734 break ;
19737 return total ;
19738 } /* pcm_read_bei2d */
19740 static sf_count_t
19741 pcm_read_lei2d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
19742 { int bufferlen, readcount, thisread ;
19743 sf_count_t total = 0 ;
19744 double normfact ;
19746 normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x80000000) : 1.0 ;
19748 bufferlen = sizeof (psf->buffer) / sizeof (int) ;
19750 while (len > 0)
19751 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
19752 thisread = psf_fread (psf->buffer, sizeof (int), readcount, psf) ;
19753 lei2d_array ((int*) (psf->buffer), thisread, ptr + total, normfact) ;
19754 total += thisread ;
19755 len -= thisread ;
19756 if (thisread < readcount)
19757 break ;
19760 return total ;
19761 } /* pcm_read_lei2d */
19763 /*===============================================================================================
19764 **-----------------------------------------------------------------------------------------------
19765 **===============================================================================================
19768 static sf_count_t
19769 pcm_write_s2sc (SF_PRIVATE *psf, short *ptr, sf_count_t len)
19770 { int bufferlen, writecount, thiswrite ;
19771 sf_count_t total = 0 ;
19773 bufferlen = sizeof (psf->buffer) / sizeof (signed char) ;
19775 while (len > 0)
19776 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
19777 s2sc_array (ptr + total, (signed char*) (psf->buffer), writecount) ;
19778 thiswrite = psf_fwrite (psf->buffer, sizeof (signed char), writecount, psf) ;
19779 total += thiswrite ;
19780 len -= thiswrite ;
19781 if (thiswrite < writecount)
19782 break ;
19785 return total ;
19786 } /* pcm_write_s2sc */
19788 static sf_count_t
19789 pcm_write_s2uc (SF_PRIVATE *psf, short *ptr, sf_count_t len)
19790 { int bufferlen, writecount, thiswrite ;
19791 sf_count_t total = 0 ;
19793 bufferlen = sizeof (psf->buffer) / sizeof (unsigned char) ;
19795 while (len > 0)
19796 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
19797 s2uc_array (ptr + total, (unsigned char*) (psf->buffer), writecount) ;
19798 thiswrite = psf_fwrite (psf->buffer, sizeof (unsigned char), writecount, psf) ;
19799 total += thiswrite ;
19800 len -= thiswrite ;
19801 if (thiswrite < writecount)
19802 break ;
19805 return total ;
19806 } /* pcm_write_s2uc */
19808 static sf_count_t
19809 pcm_write_s2bes (SF_PRIVATE *psf, short *ptr, sf_count_t len)
19811 if (CPU_IS_BIG_ENDIAN)
19812 return psf_fwrite (ptr, sizeof (short), len, psf) ;
19813 else
19814 { int bufferlen, writecount, thiswrite ;
19815 sf_count_t total = 0 ;
19817 bufferlen = sizeof (psf->buffer) / sizeof (short) ;
19819 while (len > 0)
19820 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
19821 endswap_short_copy ((short*) (psf->buffer), ptr + total, writecount) ;
19822 thiswrite = psf_fwrite (psf->buffer, sizeof (short), writecount, psf) ;
19823 total += thiswrite ;
19824 len -= thiswrite ;
19825 if (thiswrite < writecount)
19826 break ;
19829 return total ;
19831 } /* pcm_write_s2bes */
19833 static sf_count_t
19834 pcm_write_s2les (SF_PRIVATE *psf, short *ptr, sf_count_t len)
19836 if (CPU_IS_LITTLE_ENDIAN)
19837 return psf_fwrite (ptr, sizeof (short), len, psf) ;
19838 else
19839 { int bufferlen, writecount, thiswrite ;
19840 sf_count_t total = 0 ;
19842 bufferlen = sizeof (psf->buffer) / sizeof (short) ;
19844 while (len > 0)
19845 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
19846 endswap_short_copy ((short*) (psf->buffer), ptr + total, writecount) ;
19847 thiswrite = psf_fwrite (psf->buffer, sizeof (short), writecount, psf) ;
19848 total += thiswrite ;
19849 len -= thiswrite ;
19850 if (thiswrite < writecount)
19851 break ;
19854 return total ;
19856 } /* pcm_write_s2les */
19858 static sf_count_t
19859 pcm_write_s2bet (SF_PRIVATE *psf, short *ptr, sf_count_t len)
19860 { int bufferlen, writecount, thiswrite ;
19861 sf_count_t total = 0 ;
19863 bufferlen = sizeof (psf->buffer) / SIZEOF_TRIBYTE ;
19865 while (len > 0)
19866 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
19867 s2bet_array (ptr + total, (tribyte*) (psf->buffer), writecount) ;
19868 thiswrite = psf_fwrite (psf->buffer, SIZEOF_TRIBYTE, writecount, psf) ;
19869 total += thiswrite ;
19870 len -= thiswrite ;
19871 if (thiswrite < writecount)
19872 break ;
19875 return total ;
19876 } /* pcm_write_s2bet */
19878 static sf_count_t
19879 pcm_write_s2let (SF_PRIVATE *psf, short *ptr, sf_count_t len)
19880 { int bufferlen, writecount, thiswrite ;
19881 sf_count_t total = 0 ;
19883 bufferlen = sizeof (psf->buffer) / SIZEOF_TRIBYTE ;
19885 while (len > 0)
19886 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
19887 s2let_array (ptr + total, (tribyte*) (psf->buffer), writecount) ;
19888 thiswrite = psf_fwrite (psf->buffer, SIZEOF_TRIBYTE, writecount, psf) ;
19889 total += thiswrite ;
19890 len -= thiswrite ;
19891 if (thiswrite < writecount)
19892 break ;
19895 return total ;
19896 } /* pcm_write_s2let */
19898 static sf_count_t
19899 pcm_write_s2bei (SF_PRIVATE *psf, short *ptr, sf_count_t len)
19900 { int bufferlen, writecount, thiswrite ;
19901 sf_count_t total = 0 ;
19903 bufferlen = sizeof (psf->buffer) / sizeof (int) ;
19905 while (len > 0)
19906 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
19907 s2bei_array (ptr + total, (int*) (psf->buffer), writecount) ;
19908 thiswrite = psf_fwrite (psf->buffer, sizeof (int), writecount, psf) ;
19909 total += thiswrite ;
19910 len -= thiswrite ;
19911 if (thiswrite < writecount)
19912 break ;
19915 return total ;
19916 } /* pcm_write_s2bei */
19918 static sf_count_t
19919 pcm_write_s2lei (SF_PRIVATE *psf, short *ptr, sf_count_t len)
19920 { int bufferlen, writecount, thiswrite ;
19921 sf_count_t total = 0 ;
19923 bufferlen = sizeof (psf->buffer) / sizeof (int) ;
19925 while (len > 0)
19926 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
19927 s2lei_array (ptr + total, (int*) (psf->buffer), writecount) ;
19928 thiswrite = psf_fwrite (psf->buffer, sizeof (int), writecount, psf) ;
19929 total += thiswrite ;
19930 len -= thiswrite ;
19931 if (thiswrite < writecount)
19932 break ;
19935 return total ;
19936 } /* pcm_write_s2lei */
19938 /*-----------------------------------------------------------------------------------------------
19941 static sf_count_t
19942 pcm_write_i2sc (SF_PRIVATE *psf, int *ptr, sf_count_t len)
19943 { int bufferlen, writecount, thiswrite ;
19944 sf_count_t total = 0 ;
19946 bufferlen = sizeof (psf->buffer) / sizeof (signed char) ;
19948 while (len > 0)
19949 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
19950 i2sc_array (ptr + total, (signed char*) (psf->buffer), writecount) ;
19951 thiswrite = psf_fwrite (psf->buffer, sizeof (signed char), writecount, psf) ;
19952 total += thiswrite ;
19953 len -= thiswrite ;
19954 if (thiswrite < writecount)
19955 break ;
19958 return total ;
19959 } /* pcm_write_i2sc */
19961 static sf_count_t
19962 pcm_write_i2uc (SF_PRIVATE *psf, int *ptr, sf_count_t len)
19963 { int bufferlen, writecount, thiswrite ;
19964 sf_count_t total = 0 ;
19966 bufferlen = sizeof (psf->buffer) / sizeof (unsigned char) ;
19968 while (len > 0)
19969 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
19970 i2uc_array (ptr + total, (unsigned char*) (psf->buffer), writecount) ;
19971 thiswrite = psf_fwrite (psf->buffer, sizeof (signed char), writecount, psf) ;
19972 total += thiswrite ;
19973 len -= thiswrite ;
19974 if (thiswrite < writecount)
19975 break ;
19978 return total ;
19979 } /* pcm_write_i2uc */
19981 static sf_count_t
19982 pcm_write_i2bes (SF_PRIVATE *psf, int *ptr, sf_count_t len)
19983 { int bufferlen, writecount, thiswrite ;
19984 sf_count_t total = 0 ;
19986 bufferlen = sizeof (psf->buffer) / sizeof (short) ;
19988 while (len > 0)
19989 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
19990 i2bes_array (ptr + total, (short*) (psf->buffer), writecount) ;
19991 thiswrite = psf_fwrite (psf->buffer, sizeof (short), writecount, psf) ;
19992 total += thiswrite ;
19993 len -= thiswrite ;
19994 if (thiswrite < writecount)
19995 break ;
19998 return total ;
19999 } /* pcm_write_i2bes */
20001 static sf_count_t
20002 pcm_write_i2les (SF_PRIVATE *psf, int *ptr, sf_count_t len)
20003 { int bufferlen, writecount, thiswrite ;
20004 sf_count_t total = 0 ;
20006 bufferlen = sizeof (psf->buffer) / sizeof (short) ;
20008 while (len > 0)
20009 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
20010 i2les_array (ptr + total, (short*) (psf->buffer), writecount) ;
20011 thiswrite = psf_fwrite (psf->buffer, sizeof (short), writecount, psf) ;
20012 total += thiswrite ;
20013 len -= thiswrite ;
20014 if (thiswrite < writecount)
20015 break ;
20018 return total ;
20019 } /* pcm_write_i2les */
20021 static sf_count_t
20022 pcm_write_i2bet (SF_PRIVATE *psf, int *ptr, sf_count_t len)
20023 { int bufferlen, writecount, thiswrite ;
20024 sf_count_t total = 0 ;
20026 bufferlen = sizeof (psf->buffer) / SIZEOF_TRIBYTE ;
20028 while (len > 0)
20029 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
20030 i2bet_array (ptr + total, (tribyte*) (psf->buffer), writecount) ;
20031 thiswrite = psf_fwrite (psf->buffer, SIZEOF_TRIBYTE, writecount, psf) ;
20032 total += thiswrite ;
20033 len -= thiswrite ;
20034 if (thiswrite < writecount)
20035 break ;
20038 return total ;
20039 } /* pcm_write_i2bet */
20041 static sf_count_t
20042 pcm_write_i2let (SF_PRIVATE *psf, int *ptr, sf_count_t len)
20043 { int bufferlen, writecount, thiswrite ;
20044 sf_count_t total = 0 ;
20046 bufferlen = sizeof (psf->buffer) / SIZEOF_TRIBYTE ;
20048 while (len > 0)
20049 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
20050 i2let_array (ptr + total, (tribyte*) (psf->buffer), writecount) ;
20051 thiswrite = psf_fwrite (psf->buffer, SIZEOF_TRIBYTE, writecount, psf) ;
20052 total += thiswrite ;
20053 len -= thiswrite ;
20054 if (thiswrite < writecount)
20055 break ;
20058 return total ;
20059 } /* pcm_write_i2les */
20061 static sf_count_t
20062 pcm_write_i2bei (SF_PRIVATE *psf, int *ptr, sf_count_t len)
20064 if (CPU_IS_BIG_ENDIAN)
20065 return psf_fwrite (ptr, sizeof (int), len, psf) ;
20066 else
20067 { int bufferlen, writecount, thiswrite ;
20068 sf_count_t total = 0 ;
20070 bufferlen = sizeof (psf->buffer) / sizeof (int) ;
20072 while (len > 0)
20073 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
20074 endswap_int_copy ((int*) (psf->buffer), ptr + total, writecount) ;
20075 thiswrite = psf_fwrite (psf->buffer, sizeof (int), writecount, psf) ;
20076 total += thiswrite ;
20077 len -= thiswrite ;
20078 if (thiswrite < writecount)
20079 break ;
20082 return total ;
20084 } /* pcm_write_i2bei */
20086 static sf_count_t
20087 pcm_write_i2lei (SF_PRIVATE *psf, int *ptr, sf_count_t len)
20089 if (CPU_IS_LITTLE_ENDIAN)
20090 return psf_fwrite (ptr, sizeof (int), len, psf) ;
20091 else
20092 { int bufferlen, writecount, thiswrite ;
20093 sf_count_t total = 0 ;
20095 bufferlen = sizeof (psf->buffer) / sizeof (int) ;
20097 while (len > 0)
20098 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
20099 endswap_int_copy ((int*) (psf->buffer), ptr + total, writecount) ;
20100 thiswrite = psf_fwrite (psf->buffer, sizeof (int), writecount, psf) ;
20101 total += thiswrite ;
20102 len -= thiswrite ;
20103 if (thiswrite < writecount)
20104 break ;
20107 return total ;
20109 } /* pcm_write_i2lei */
20111 /*------------------------------------------------------------------------------
20112 **==============================================================================
20113 **------------------------------------------------------------------------------
20116 static void
20117 f2sc_array (float *src, signed char *dest, int count, int normalize)
20118 { float normfact ;
20120 normfact = normalize ? (1.0 * 0x7F) : 1.0 ;
20122 while (count)
20123 { count -- ;
20124 dest [count] = lrintf (src [count] * normfact) ;
20126 } /* f2sc_array */
20128 static void
20129 f2sc_clip_array (float *src, signed char *dest, int count, int normalize)
20130 { float normfact, scaled_value ;
20132 normfact = normalize ? (8.0 * 0x10000000) : (1.0 * 0x1000000) ;
20134 while (count)
20135 { count -- ;
20136 scaled_value = src [count] * normfact ;
20137 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF))
20138 { dest [count] = 127 ;
20139 continue ;
20141 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
20142 { dest [count] = -128 ;
20143 continue ;
20146 dest [count] = lrintf (scaled_value) >> 24 ;
20148 } /* f2sc_clip_array */
20150 static sf_count_t
20151 pcm_write_f2sc (SF_PRIVATE *psf, float *ptr, sf_count_t len)
20152 { void (*convert) (float *, signed char *, int, int) ;
20153 int bufferlen, writecount, thiswrite ;
20154 sf_count_t total = 0 ;
20156 convert = (psf->add_clipping) ? f2sc_clip_array : f2sc_array ;
20157 bufferlen = sizeof (psf->buffer) / sizeof (signed char) ;
20159 while (len > 0)
20160 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
20161 convert (ptr + total, (signed char*) (psf->buffer), writecount, psf->norm_float) ;
20162 thiswrite = psf_fwrite (psf->buffer, sizeof (signed char), writecount, psf) ;
20163 total += thiswrite ;
20164 len -= thiswrite ;
20165 if (thiswrite < writecount)
20166 break ;
20169 return total ;
20170 } /* pcm_write_f2sc */
20172 /*==============================================================================
20175 static void
20176 f2uc_array (float *src, unsigned char *dest, int count, int normalize)
20177 { float normfact ;
20179 normfact = normalize ? (1.0 * 0x7F) : 1.0 ;
20181 while (count)
20182 { count -- ;
20183 dest [count] = lrintf (src [count] * normfact) + 128 ;
20185 } /* f2uc_array */
20187 static void
20188 f2uc_clip_array (float *src, unsigned char *dest, int count, int normalize)
20189 { float normfact, scaled_value ;
20191 normfact = normalize ? (8.0 * 0x10000000) : (1.0 * 0x1000000) ;
20193 while (count)
20194 { count -- ;
20195 scaled_value = src [count] * normfact ;
20196 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF))
20197 { dest [count] = 0xFF ;
20198 continue ;
20200 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
20201 { dest [count] = 0 ;
20202 continue ;
20205 dest [count] = (lrintf (scaled_value) >> 24) + 128 ;
20207 } /* f2uc_clip_array */
20209 static sf_count_t
20210 pcm_write_f2uc (SF_PRIVATE *psf, float *ptr, sf_count_t len)
20211 { void (*convert) (float *, unsigned char *, int, int) ;
20212 int bufferlen, writecount, thiswrite ;
20213 sf_count_t total = 0 ;
20215 convert = (psf->add_clipping) ? f2uc_clip_array : f2uc_array ;
20216 bufferlen = sizeof (psf->buffer) / sizeof (unsigned char) ;
20218 while (len > 0)
20219 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
20220 convert (ptr + total, (unsigned char*) (psf->buffer), writecount, psf->norm_float) ;
20221 thiswrite = psf_fwrite (psf->buffer, sizeof (unsigned char), writecount, psf) ;
20222 total += thiswrite ;
20223 len -= thiswrite ;
20224 if (thiswrite < writecount)
20225 break ;
20228 return total ;
20229 } /* pcm_write_f2uc */
20231 /*==============================================================================
20234 static void
20235 f2bes_array (float *src, short *dest, int count, int normalize)
20236 { unsigned char *ucptr ;
20237 float normfact ;
20238 short value ;
20240 normfact = normalize ? (1.0 * 0x7FFF) : 1.0 ;
20241 ucptr = ((unsigned char*) dest) + 2 * count ;
20243 while (count)
20244 { count -- ;
20245 ucptr -= 2 ;
20246 value = lrintf (src [count] * normfact) ;
20247 ucptr [1] = value ;
20248 ucptr [0] = value >> 8 ;
20250 } /* f2bes_array */
20252 static void
20253 f2bes_clip_array (float *src, short *dest, int count, int normalize)
20254 { unsigned char *ucptr ;
20255 float normfact, scaled_value ;
20256 int value ;
20258 normfact = normalize ? (8.0 * 0x10000000) : (1.0 * 0x10000) ;
20259 ucptr = ((unsigned char*) dest) + 2 * count ;
20261 while (count)
20262 { count -- ;
20263 ucptr -= 2 ;
20264 scaled_value = src [count] * normfact ;
20265 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF))
20266 { ucptr [1] = 0xFF ;
20267 ucptr [0] = 0x7F ;
20268 continue ;
20270 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
20271 { ucptr [1] = 0x00 ;
20272 ucptr [0] = 0x80 ;
20273 continue ;
20276 value = lrintf (scaled_value) ;
20277 ucptr [1] = value >> 16 ;
20278 ucptr [0] = value >> 24 ;
20280 } /* f2bes_clip_array */
20282 static sf_count_t
20283 pcm_write_f2bes (SF_PRIVATE *psf, float *ptr, sf_count_t len)
20284 { void (*convert) (float *, short *t, int, int) ;
20285 int bufferlen, writecount, thiswrite ;
20286 sf_count_t total = 0 ;
20288 convert = (psf->add_clipping) ? f2bes_clip_array : f2bes_array ;
20289 bufferlen = sizeof (psf->buffer) / sizeof (short) ;
20291 while (len > 0)
20292 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
20293 convert (ptr + total, (short*) (psf->buffer), writecount, psf->norm_float) ;
20294 thiswrite = psf_fwrite (psf->buffer, sizeof (short), writecount, psf) ;
20295 total += thiswrite ;
20296 len -= thiswrite ;
20297 if (thiswrite < writecount)
20298 break ;
20301 return total ;
20302 } /* pcm_write_f2bes */
20304 /*==============================================================================
20307 static void
20308 f2les_array (float *src, short *dest, int count, int normalize)
20309 { unsigned char *ucptr ;
20310 float normfact ;
20311 int value ;
20313 normfact = normalize ? (1.0 * 0x7FFF) : 1.0 ;
20314 ucptr = ((unsigned char*) dest) + 2 * count ;
20316 while (count)
20317 { count -- ;
20318 ucptr -= 2 ;
20319 value = lrintf (src [count] * normfact) ;
20320 ucptr [0] = value ;
20321 ucptr [1] = value >> 8 ;
20323 } /* f2les_array */
20325 static void
20326 f2les_clip_array (float *src, short *dest, int count, int normalize)
20327 { unsigned char *ucptr ;
20328 float normfact, scaled_value ;
20329 int value ;
20331 normfact = normalize ? (8.0 * 0x10000000) : (1.0 * 0x10000) ;
20332 ucptr = ((unsigned char*) dest) + 2 * count ;
20334 while (count)
20335 { count -- ;
20336 ucptr -= 2 ;
20337 scaled_value = src [count] * normfact ;
20338 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF))
20339 { ucptr [0] = 0xFF ;
20340 ucptr [1] = 0x7F ;
20341 continue ;
20343 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
20344 { ucptr [0] = 0x00 ;
20345 ucptr [1] = 0x80 ;
20346 continue ;
20349 value = lrintf (scaled_value) ;
20350 ucptr [0] = value >> 16 ;
20351 ucptr [1] = value >> 24 ;
20353 } /* f2les_clip_array */
20355 static sf_count_t
20356 pcm_write_f2les (SF_PRIVATE *psf, float *ptr, sf_count_t len)
20357 { void (*convert) (float *, short *t, int, int) ;
20358 int bufferlen, writecount, thiswrite ;
20359 sf_count_t total = 0 ;
20361 convert = (psf->add_clipping) ? f2les_clip_array : f2les_array ;
20362 bufferlen = sizeof (psf->buffer) / sizeof (short) ;
20364 while (len > 0)
20365 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
20366 convert (ptr + total, (short*) (psf->buffer), writecount, psf->norm_float) ;
20367 thiswrite = psf_fwrite (psf->buffer, sizeof (short), writecount, psf) ;
20368 total += thiswrite ;
20369 len -= thiswrite ;
20370 if (thiswrite < writecount)
20371 break ;
20374 return total ;
20375 } /* pcm_write_f2les */
20377 /*==============================================================================
20380 static void
20381 f2let_array (float *src, tribyte *dest, int count, int normalize)
20382 { unsigned char *ucptr ;
20383 float normfact ;
20384 int value ;
20386 normfact = normalize ? (1.0 * 0x7FFFFF) : 1.0 ;
20387 ucptr = ((unsigned char*) dest) + 3 * count ;
20389 while (count)
20390 { count -- ;
20391 ucptr -= 3 ;
20392 value = lrintf (src [count] * normfact) ;
20393 ucptr [0] = value ;
20394 ucptr [1] = value >> 8 ;
20395 ucptr [2] = value >> 16 ;
20397 } /* f2let_array */
20399 static void
20400 f2let_clip_array (float *src, tribyte *dest, int count, int normalize)
20401 { unsigned char *ucptr ;
20402 float normfact, scaled_value ;
20403 int value ;
20405 normfact = normalize ? (8.0 * 0x10000000) : (1.0 * 0x100) ;
20406 ucptr = ((unsigned char*) dest) + 3 * count ;
20408 while (count)
20409 { count -- ;
20410 ucptr -= 3 ;
20411 scaled_value = src [count] * normfact ;
20412 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF))
20413 { ucptr [0] = 0xFF ;
20414 ucptr [1] = 0xFF ;
20415 ucptr [2] = 0x7F ;
20416 continue ;
20418 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
20419 { ucptr [0] = 0x00 ;
20420 ucptr [1] = 0x00 ;
20421 ucptr [2] = 0x80 ;
20422 continue ;
20425 value = lrintf (scaled_value) ;
20426 ucptr [0] = value >> 8 ;
20427 ucptr [1] = value >> 16 ;
20428 ucptr [2] = value >> 24 ;
20430 } /* f2let_clip_array */
20432 static sf_count_t
20433 pcm_write_f2let (SF_PRIVATE *psf, float *ptr, sf_count_t len)
20434 { void (*convert) (float *, tribyte *, int, int) ;
20435 int bufferlen, writecount, thiswrite ;
20436 sf_count_t total = 0 ;
20438 convert = (psf->add_clipping) ? f2let_clip_array : f2let_array ;
20439 bufferlen = sizeof (psf->buffer) / SIZEOF_TRIBYTE ;
20441 while (len > 0)
20442 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
20443 convert (ptr + total, (tribyte*) (psf->buffer), writecount, psf->norm_float) ;
20444 thiswrite = psf_fwrite (psf->buffer, SIZEOF_TRIBYTE, writecount, psf) ;
20445 total += thiswrite ;
20446 len -= thiswrite ;
20447 if (thiswrite < writecount)
20448 break ;
20451 return total ;
20452 } /* pcm_write_f2let */
20454 /*==============================================================================
20457 static void
20458 f2bet_array (float *src, tribyte *dest, int count, int normalize)
20459 { unsigned char *ucptr ;
20460 float normfact ;
20461 int value ;
20463 normfact = normalize ? (1.0 * 0x7FFFFF) : 1.0 ;
20464 ucptr = ((unsigned char*) dest) + 3 * count ;
20466 while (count)
20467 { count -- ;
20468 ucptr -= 3 ;
20469 value = lrintf (src [count] * normfact) ;
20470 ucptr [0] = value >> 16 ;
20471 ucptr [1] = value >> 8 ;
20472 ucptr [2] = value ;
20474 } /* f2bet_array */
20476 static void
20477 f2bet_clip_array (float *src, tribyte *dest, int count, int normalize)
20478 { unsigned char *ucptr ;
20479 float normfact, scaled_value ;
20480 int value ;
20482 normfact = normalize ? (8.0 * 0x10000000) : (1.0 * 0x100) ;
20483 ucptr = ((unsigned char*) dest) + 3 * count ;
20485 while (count)
20486 { count -- ;
20487 ucptr -= 3 ;
20488 scaled_value = src [count] * normfact ;
20489 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF))
20490 { ucptr [0] = 0x7F ;
20491 ucptr [1] = 0xFF ;
20492 ucptr [2] = 0xFF ;
20493 continue ;
20495 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
20496 { ucptr [0] = 0x80 ;
20497 ucptr [1] = 0x00 ;
20498 ucptr [2] = 0x00 ;
20499 continue ;
20502 value = lrint (scaled_value) ;
20503 ucptr [0] = value >> 24 ;
20504 ucptr [1] = value >> 16 ;
20505 ucptr [2] = value >> 8 ;
20507 } /* f2bet_clip_array */
20509 static sf_count_t
20510 pcm_write_f2bet (SF_PRIVATE *psf, float *ptr, sf_count_t len)
20511 { void (*convert) (float *, tribyte *, int, int) ;
20512 int bufferlen, writecount, thiswrite ;
20513 sf_count_t total = 0 ;
20515 convert = (psf->add_clipping) ? f2bet_clip_array : f2bet_array ;
20516 bufferlen = sizeof (psf->buffer) / SIZEOF_TRIBYTE ;
20518 while (len > 0)
20519 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
20520 convert (ptr + total, (tribyte*) (psf->buffer), writecount, psf->norm_float) ;
20521 thiswrite = psf_fwrite (psf->buffer, SIZEOF_TRIBYTE, writecount, psf) ;
20522 total += thiswrite ;
20523 len -= thiswrite ;
20524 if (thiswrite < writecount)
20525 break ;
20528 return total ;
20529 } /* pcm_write_f2bet */
20531 /*==============================================================================
20534 static void
20535 f2bei_array (float *src, int *dest, int count, int normalize)
20536 { unsigned char *ucptr ;
20537 float normfact ;
20538 int value ;
20540 normfact = normalize ? (1.0 * 0x7FFFFFFF) : 1.0 ;
20541 ucptr = ((unsigned char*) dest) + 4 * count ;
20542 while (count)
20543 { count -- ;
20544 ucptr -= 4 ;
20545 value = lrintf (src [count] * normfact) ;
20546 ucptr [0] = value >> 24 ;
20547 ucptr [1] = value >> 16 ;
20548 ucptr [2] = value >> 8 ;
20549 ucptr [3] = value ;
20551 } /* f2bei_array */
20553 static void
20554 f2bei_clip_array (float *src, int *dest, int count, int normalize)
20555 { unsigned char *ucptr ;
20556 float normfact, scaled_value ;
20557 int value ;
20559 normfact = normalize ? (8.0 * 0x10000000) : 1.0 ;
20560 ucptr = ((unsigned char*) dest) + 4 * count ;
20562 while (count)
20563 { count -- ;
20564 ucptr -= 4 ;
20565 scaled_value = src [count] * normfact ;
20566 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= 1.0 * 0x7FFFFFFF)
20567 { ucptr [0] = 0x7F ;
20568 ucptr [1] = 0xFF ;
20569 ucptr [2] = 0xFF ;
20570 ucptr [3] = 0xFF ;
20571 continue ;
20573 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
20574 { ucptr [0] = 0x80 ;
20575 ucptr [1] = 0x00 ;
20576 ucptr [2] = 0x00 ;
20577 ucptr [3] = 0x00 ;
20578 continue ;
20581 value = lrintf (scaled_value) ;
20582 ucptr [0] = value >> 24 ;
20583 ucptr [1] = value >> 16 ;
20584 ucptr [2] = value >> 8 ;
20585 ucptr [3] = value ;
20587 } /* f2bei_clip_array */
20589 static sf_count_t
20590 pcm_write_f2bei (SF_PRIVATE *psf, float *ptr, sf_count_t len)
20591 { void (*convert) (float *, int *, int, int) ;
20592 int bufferlen, writecount, thiswrite ;
20593 sf_count_t total = 0 ;
20595 convert = (psf->add_clipping) ? f2bei_clip_array : f2bei_array ;
20596 bufferlen = sizeof (psf->buffer) / sizeof (int) ;
20598 while (len > 0)
20599 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
20600 convert (ptr + total, (int*) (psf->buffer), writecount, psf->norm_float) ;
20601 thiswrite = psf_fwrite (psf->buffer, sizeof (int), writecount, psf) ;
20602 total += thiswrite ;
20603 len -= thiswrite ;
20604 if (thiswrite < writecount)
20605 break ;
20608 return total ;
20609 } /* pcm_write_f2bei */
20611 /*==============================================================================
20614 static void
20615 f2lei_array (float *src, int *dest, int count, int normalize)
20616 { unsigned char *ucptr ;
20617 float normfact ;
20618 int value ;
20620 normfact = normalize ? (1.0 * 0x7FFFFFFF) : 1.0 ;
20621 ucptr = ((unsigned char*) dest) + 4 * count ;
20623 while (count)
20624 { count -- ;
20625 ucptr -= 4 ;
20626 value = lrintf (src [count] * normfact) ;
20627 ucptr [0] = value ;
20628 ucptr [1] = value >> 8 ;
20629 ucptr [2] = value >> 16 ;
20630 ucptr [3] = value >> 24 ;
20632 } /* f2lei_array */
20634 static void
20635 f2lei_clip_array (float *src, int *dest, int count, int normalize)
20636 { unsigned char *ucptr ;
20637 float normfact, scaled_value ;
20638 int value ;
20640 normfact = normalize ? (8.0 * 0x10000000) : 1.0 ;
20641 ucptr = ((unsigned char*) dest) + 4 * count ;
20643 while (count)
20644 { count -- ;
20645 ucptr -= 4 ;
20646 scaled_value = src [count] * normfact ;
20647 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF))
20648 { ucptr [0] = 0xFF ;
20649 ucptr [1] = 0xFF ;
20650 ucptr [2] = 0xFF ;
20651 ucptr [3] = 0x7F ;
20652 continue ;
20654 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
20655 { ucptr [0] = 0x00 ;
20656 ucptr [1] = 0x00 ;
20657 ucptr [2] = 0x00 ;
20658 ucptr [3] = 0x80 ;
20659 continue ;
20662 value = lrintf (scaled_value) ;
20663 ucptr [0] = value ;
20664 ucptr [1] = value >> 8 ;
20665 ucptr [2] = value >> 16 ;
20666 ucptr [3] = value >> 24 ;
20668 } /* f2lei_clip_array */
20670 static sf_count_t
20671 pcm_write_f2lei (SF_PRIVATE *psf, float *ptr, sf_count_t len)
20672 { void (*convert) (float *, int *, int, int) ;
20673 int bufferlen, writecount, thiswrite ;
20674 sf_count_t total = 0 ;
20676 convert = (psf->add_clipping) ? f2lei_clip_array : f2lei_array ;
20677 bufferlen = sizeof (psf->buffer) / sizeof (int) ;
20679 while (len > 0)
20680 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
20681 convert (ptr + total, (int*) (psf->buffer), writecount, psf->norm_float) ;
20682 thiswrite = psf_fwrite (psf->buffer, sizeof (int), writecount, psf) ;
20683 total += thiswrite ;
20684 len -= thiswrite ;
20685 if (thiswrite < writecount)
20686 break ;
20689 return total ;
20690 } /* pcm_write_f2lei */
20692 /*==============================================================================
20695 static void
20696 d2sc_array (double *src, signed char *dest, int count, int normalize)
20697 { double normfact ;
20699 normfact = normalize ? (1.0 * 0x7F) : 1.0 ;
20701 while (count)
20702 { count -- ;
20703 dest [count] = lrint (src [count] * normfact) ;
20705 } /* d2sc_array */
20707 static void
20708 d2sc_clip_array (double *src, signed char *dest, int count, int normalize)
20709 { double normfact, scaled_value ;
20711 normfact = normalize ? (8.0 * 0x10000000) : (1.0 * 0x1000000) ;
20713 while (count)
20714 { count -- ;
20715 scaled_value = src [count] * normfact ;
20716 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF))
20717 { dest [count] = 127 ;
20718 continue ;
20720 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
20721 { dest [count] = -128 ;
20722 continue ;
20725 dest [count] = lrintf (scaled_value) >> 24 ;
20727 } /* d2sc_clip_array */
20729 static sf_count_t
20730 pcm_write_d2sc (SF_PRIVATE *psf, double *ptr, sf_count_t len)
20731 { void (*convert) (double *, signed char *, int, int) ;
20732 int bufferlen, writecount, thiswrite ;
20733 sf_count_t total = 0 ;
20735 convert = (psf->add_clipping) ? d2sc_clip_array : d2sc_array ;
20736 bufferlen = sizeof (psf->buffer) / sizeof (signed char) ;
20738 while (len > 0)
20739 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
20740 convert (ptr + total, (signed char*) (psf->buffer), writecount, psf->norm_double) ;
20741 thiswrite = psf_fwrite (psf->buffer, sizeof (signed char), writecount, psf) ;
20742 total += thiswrite ;
20743 len -= thiswrite ;
20744 if (thiswrite < writecount)
20745 break ;
20748 return total ;
20749 } /* pcm_write_d2sc */
20751 /*==============================================================================
20754 static void
20755 d2uc_array (double *src, unsigned char *dest, int count, int normalize)
20756 { double normfact ;
20758 normfact = normalize ? (1.0 * 0x7F) : 1.0 ;
20760 while (count)
20761 { count -- ;
20762 dest [count] = lrint (src [count] * normfact) + 128 ;
20764 } /* d2uc_array */
20766 static void
20767 d2uc_clip_array (double *src, unsigned char *dest, int count, int normalize)
20768 { double normfact, scaled_value ;
20770 normfact = normalize ? (8.0 * 0x10000000) : (1.0 * 0x1000000) ;
20772 while (count)
20773 { count -- ;
20774 scaled_value = src [count] * normfact ;
20775 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF))
20776 { dest [count] = 255 ;
20777 continue ;
20779 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
20780 { dest [count] = 0 ;
20781 continue ;
20784 dest [count] = (lrint (src [count] * normfact) >> 24) + 128 ;
20786 } /* d2uc_clip_array */
20789 static sf_count_t
20790 pcm_write_d2uc (SF_PRIVATE *psf, double *ptr, sf_count_t len)
20791 { void (*convert) (double *, unsigned char *, int, int) ;
20792 int bufferlen, writecount, thiswrite ;
20793 sf_count_t total = 0 ;
20795 convert = (psf->add_clipping) ? d2uc_clip_array : d2uc_array ;
20796 bufferlen = sizeof (psf->buffer) / sizeof (unsigned char) ;
20798 while (len > 0)
20799 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
20800 convert (ptr + total, (unsigned char*) (psf->buffer), writecount, psf->norm_double) ;
20801 thiswrite = psf_fwrite (psf->buffer, sizeof (unsigned char), writecount, psf) ;
20802 total += thiswrite ;
20803 len -= thiswrite ;
20804 if (thiswrite < writecount)
20805 break ;
20808 return total ;
20809 } /* pcm_write_d2uc */
20811 /*==============================================================================
20814 static void
20815 d2bes_array (double *src, short *dest, int count, int normalize)
20816 { unsigned char *ucptr ;
20817 short value ;
20818 double normfact ;
20820 normfact = normalize ? (1.0 * 0x7FFF) : 1.0 ;
20821 ucptr = ((unsigned char*) dest) + 2 * count ;
20823 while (count)
20824 { count -- ;
20825 ucptr -= 2 ;
20826 value = lrint (src [count] * normfact) ;
20827 ucptr [1] = value ;
20828 ucptr [0] = value >> 8 ;
20830 } /* d2bes_array */
20832 static void
20833 d2bes_clip_array (double *src, short *dest, int count, int normalize)
20834 { unsigned char *ucptr ;
20835 double normfact, scaled_value ;
20836 int value ;
20838 normfact = normalize ? (8.0 * 0x10000000) : (1.0 * 0x10000) ;
20839 ucptr = ((unsigned char*) dest) + 2 * count ;
20841 while (count)
20842 { count -- ;
20843 ucptr -= 2 ;
20844 scaled_value = src [count] * normfact ;
20845 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF))
20846 { ucptr [1] = 0xFF ;
20847 ucptr [0] = 0x7F ;
20848 continue ;
20850 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
20851 { ucptr [1] = 0x00 ;
20852 ucptr [0] = 0x80 ;
20853 continue ;
20856 value = lrint (scaled_value) ;
20857 ucptr [1] = value >> 16 ;
20858 ucptr [0] = value >> 24 ;
20860 } /* d2bes_clip_array */
20862 static sf_count_t
20863 pcm_write_d2bes (SF_PRIVATE *psf, double *ptr, sf_count_t len)
20864 { void (*convert) (double *, short *, int, int) ;
20865 int bufferlen, writecount, thiswrite ;
20866 sf_count_t total = 0 ;
20868 convert = (psf->add_clipping) ? d2bes_clip_array : d2bes_array ;
20869 bufferlen = sizeof (psf->buffer) / sizeof (short) ;
20871 while (len > 0)
20872 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
20873 convert (ptr + total, (short*) (psf->buffer), writecount, psf->norm_double) ;
20874 thiswrite = psf_fwrite (psf->buffer, sizeof (short), writecount, psf) ;
20875 total += thiswrite ;
20876 len -= thiswrite ;
20877 if (thiswrite < writecount)
20878 break ;
20881 return total ;
20882 } /* pcm_write_d2bes */
20884 /*==============================================================================
20887 static void
20888 d2les_array (double *src, short *dest, int count, int normalize)
20889 { unsigned char *ucptr ;
20890 short value ;
20891 double normfact ;
20893 normfact = normalize ? (1.0 * 0x7FFF) : 1.0 ;
20894 ucptr = ((unsigned char*) dest) + 2 * count ;
20896 while (count)
20897 { count -- ;
20898 ucptr -= 2 ;
20899 value = lrint (src [count] * normfact) ;
20900 ucptr [0] = value ;
20901 ucptr [1] = value >> 8 ;
20903 } /* d2les_array */
20905 static void
20906 d2les_clip_array (double *src, short *dest, int count, int normalize)
20907 { unsigned char *ucptr ;
20908 int value ;
20909 double normfact, scaled_value ;
20911 normfact = normalize ? (8.0 * 0x10000000) : (1.0 * 0x10000) ;
20912 ucptr = ((unsigned char*) dest) + 2 * count ;
20914 while (count)
20915 { count -- ;
20916 ucptr -= 2 ;
20917 scaled_value = src [count] * normfact ;
20918 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF))
20919 { ucptr [0] = 0xFF ;
20920 ucptr [1] = 0x7F ;
20921 continue ;
20923 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
20924 { ucptr [0] = 0x00 ;
20925 ucptr [1] = 0x80 ;
20926 continue ;
20929 value = lrint (scaled_value) ;
20930 ucptr [0] = value >> 16 ;
20931 ucptr [1] = value >> 24 ;
20933 } /* d2les_clip_array */
20935 static sf_count_t
20936 pcm_write_d2les (SF_PRIVATE *psf, double *ptr, sf_count_t len)
20937 { void (*convert) (double *, short *, int, int) ;
20938 int bufferlen, writecount, thiswrite ;
20939 sf_count_t total = 0 ;
20941 convert = (psf->add_clipping) ? d2les_clip_array : d2les_array ;
20942 bufferlen = sizeof (psf->buffer) / sizeof (short) ;
20944 while (len > 0)
20945 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
20946 convert (ptr + total, (short*) (psf->buffer), writecount, psf->norm_double) ;
20947 thiswrite = psf_fwrite (psf->buffer, sizeof (short), writecount, psf) ;
20948 total += thiswrite ;
20949 len -= thiswrite ;
20950 if (thiswrite < writecount)
20951 break ;
20954 return total ;
20955 } /* pcm_write_d2les */
20957 /*==============================================================================
20960 static void
20961 d2let_array (double *src, tribyte *dest, int count, int normalize)
20962 { unsigned char *ucptr ;
20963 int value ;
20964 double normfact ;
20966 normfact = normalize ? (1.0 * 0x7FFFFF) : 1.0 ;
20967 ucptr = ((unsigned char*) dest) + 3 * count ;
20969 while (count)
20970 { count -- ;
20971 ucptr -= 3 ;
20972 value = lrint (src [count] * normfact) ;
20973 ucptr [0] = value ;
20974 ucptr [1] = value >> 8 ;
20975 ucptr [2] = value >> 16 ;
20977 } /* d2let_array */
20979 static void
20980 d2let_clip_array (double *src, tribyte *dest, int count, int normalize)
20981 { unsigned char *ucptr ;
20982 int value ;
20983 double normfact, scaled_value ;
20985 normfact = normalize ? (8.0 * 0x10000000) : (1.0 * 0x100) ;
20986 ucptr = ((unsigned char*) dest) + 3 * count ;
20988 while (count)
20989 { count -- ;
20990 ucptr -= 3 ;
20991 scaled_value = src [count] * normfact ;
20992 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF))
20993 { ucptr [0] = 0xFF ;
20994 ucptr [1] = 0xFF ;
20995 ucptr [2] = 0x7F ;
20996 continue ;
20998 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
20999 { ucptr [0] = 0x00 ;
21000 ucptr [1] = 0x00 ;
21001 ucptr [2] = 0x80 ;
21002 continue ;
21005 value = lrint (scaled_value) ;
21006 ucptr [0] = value >> 8 ;
21007 ucptr [1] = value >> 16 ;
21008 ucptr [2] = value >> 24 ;
21010 } /* d2let_clip_array */
21012 static sf_count_t
21013 pcm_write_d2let (SF_PRIVATE *psf, double *ptr, sf_count_t len)
21014 { void (*convert) (double *, tribyte *, int, int) ;
21015 int bufferlen, writecount, thiswrite ;
21016 sf_count_t total = 0 ;
21018 convert = (psf->add_clipping) ? d2let_clip_array : d2let_array ;
21019 bufferlen = sizeof (psf->buffer) / SIZEOF_TRIBYTE ;
21021 while (len > 0)
21022 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
21023 convert (ptr + total, (tribyte*) (psf->buffer), writecount, psf->norm_double) ;
21024 thiswrite = psf_fwrite (psf->buffer, SIZEOF_TRIBYTE, writecount, psf) ;
21025 total += thiswrite ;
21026 len -= thiswrite ;
21027 if (thiswrite < writecount)
21028 break ;
21031 return total ;
21032 } /* pcm_write_d2let */
21034 /*==============================================================================
21037 static void
21038 d2bet_array (double *src, tribyte *dest, int count, int normalize)
21039 { unsigned char *ucptr ;
21040 int value ;
21041 double normfact ;
21043 normfact = normalize ? (1.0 * 0x7FFFFF) : 1.0 ;
21044 ucptr = ((unsigned char*) dest) + 3 * count ;
21046 while (count)
21047 { count -- ;
21048 ucptr -= 3 ;
21049 value = lrint (src [count] * normfact) ;
21050 ucptr [2] = value ;
21051 ucptr [1] = value >> 8 ;
21052 ucptr [0] = value >> 16 ;
21054 } /* d2bet_array */
21056 static void
21057 d2bet_clip_array (double *src, tribyte *dest, int count, int normalize)
21058 { unsigned char *ucptr ;
21059 int value ;
21060 double normfact, scaled_value ;
21062 normfact = normalize ? (8.0 * 0x10000000) : (1.0 * 0x100) ;
21063 ucptr = ((unsigned char*) dest) + 3 * count ;
21065 while (count)
21066 { count -- ;
21067 ucptr -= 3 ;
21068 scaled_value = src [count] * normfact ;
21069 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF))
21070 { ucptr [2] = 0xFF ;
21071 ucptr [1] = 0xFF ;
21072 ucptr [0] = 0x7F ;
21073 continue ;
21075 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
21076 { ucptr [2] = 0x00 ;
21077 ucptr [1] = 0x00 ;
21078 ucptr [0] = 0x80 ;
21079 continue ;
21082 value = lrint (scaled_value) ;
21083 ucptr [2] = value >> 8 ;
21084 ucptr [1] = value >> 16 ;
21085 ucptr [0] = value >> 24 ;
21087 } /* d2bet_clip_array */
21089 static sf_count_t
21090 pcm_write_d2bet (SF_PRIVATE *psf, double *ptr, sf_count_t len)
21091 { void (*convert) (double *, tribyte *, int, int) ;
21092 int bufferlen, writecount, thiswrite ;
21093 sf_count_t total = 0 ;
21095 convert = (psf->add_clipping) ? d2bet_clip_array : d2bet_array ;
21096 bufferlen = sizeof (psf->buffer) / SIZEOF_TRIBYTE ;
21098 while (len > 0)
21099 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
21100 convert (ptr + total, (tribyte*) (psf->buffer), writecount, psf->norm_double) ;
21101 thiswrite = psf_fwrite (psf->buffer, SIZEOF_TRIBYTE, writecount, psf) ;
21102 total += thiswrite ;
21103 len -= thiswrite ;
21104 if (thiswrite < writecount)
21105 break ;
21108 return total ;
21109 } /* pcm_write_d2bet */
21111 /*==============================================================================
21114 static void
21115 d2bei_array (double *src, int *dest, int count, int normalize)
21116 { unsigned char *ucptr ;
21117 int value ;
21118 double normfact ;
21120 normfact = normalize ? (1.0 * 0x7FFFFFFF) : 1.0 ;
21121 ucptr = ((unsigned char*) dest) + 4 * count ;
21123 while (count)
21124 { count -- ;
21125 ucptr -= 4 ;
21126 value = lrint (src [count] * normfact) ;
21127 ucptr [0] = value >> 24 ;
21128 ucptr [1] = value >> 16 ;
21129 ucptr [2] = value >> 8 ;
21130 ucptr [3] = value ;
21132 } /* d2bei_array */
21134 static void
21135 d2bei_clip_array (double *src, int *dest, int count, int normalize)
21136 { unsigned char *ucptr ;
21137 int value ;
21138 double normfact, scaled_value ;
21140 normfact = normalize ? (8.0 * 0x10000000) : 1.0 ;
21141 ucptr = ((unsigned char*) dest) + 4 * count ;
21143 while (count)
21144 { count -- ;
21145 ucptr -= 4 ;
21146 scaled_value = src [count] * normfact ;
21147 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF))
21148 { ucptr [3] = 0xFF ;
21149 ucptr [2] = 0xFF ;
21150 ucptr [1] = 0xFF ;
21151 ucptr [0] = 0x7F ;
21152 continue ;
21154 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
21155 { ucptr [3] = 0x00 ;
21156 ucptr [2] = 0x00 ;
21157 ucptr [1] = 0x00 ;
21158 ucptr [0] = 0x80 ;
21159 continue ;
21162 value = lrint (scaled_value) ;
21163 ucptr [0] = value >> 24 ;
21164 ucptr [1] = value >> 16 ;
21165 ucptr [2] = value >> 8 ;
21166 ucptr [3] = value ;
21168 } /* d2bei_clip_array */
21170 static sf_count_t
21171 pcm_write_d2bei (SF_PRIVATE *psf, double *ptr, sf_count_t len)
21172 { void (*convert) (double *, int *, int, int) ;
21173 int bufferlen, writecount, thiswrite ;
21174 sf_count_t total = 0 ;
21176 convert = (psf->add_clipping) ? d2bei_clip_array : d2bei_array ;
21177 bufferlen = sizeof (psf->buffer) / sizeof (int) ;
21179 while (len > 0)
21180 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
21181 convert (ptr + total, (int*) (psf->buffer), writecount, psf->norm_double) ;
21182 thiswrite = psf_fwrite (psf->buffer, sizeof (int), writecount, psf) ;
21183 total += thiswrite ;
21184 len -= thiswrite ;
21185 if (thiswrite < writecount)
21186 break ;
21189 return total ;
21190 } /* pcm_write_d2bei */
21192 /*==============================================================================
21195 static void
21196 d2lei_array (double *src, int *dest, int count, int normalize)
21197 { unsigned char *ucptr ;
21198 int value ;
21199 double normfact ;
21201 normfact = normalize ? (1.0 * 0x7FFFFFFF) : 1.0 ;
21202 ucptr = ((unsigned char*) dest) + 4 * count ;
21204 while (count)
21205 { count -- ;
21206 ucptr -= 4 ;
21207 value = lrint (src [count] * normfact) ;
21208 ucptr [0] = value ;
21209 ucptr [1] = value >> 8 ;
21210 ucptr [2] = value >> 16 ;
21211 ucptr [3] = value >> 24 ;
21213 } /* d2lei_array */
21215 static void
21216 d2lei_clip_array (double *src, int *dest, int count, int normalize)
21217 { unsigned char *ucptr ;
21218 int value ;
21219 double normfact, scaled_value ;
21221 normfact = normalize ? (8.0 * 0x10000000) : 1.0 ;
21222 ucptr = ((unsigned char*) dest) + 4 * count ;
21224 while (count)
21225 { count -- ;
21226 ucptr -= 4 ;
21227 scaled_value = src [count] * normfact ;
21228 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF))
21229 { ucptr [0] = 0xFF ;
21230 ucptr [1] = 0xFF ;
21231 ucptr [2] = 0xFF ;
21232 ucptr [3] = 0x7F ;
21233 continue ;
21235 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
21236 { ucptr [0] = 0x00 ;
21237 ucptr [1] = 0x00 ;
21238 ucptr [2] = 0x00 ;
21239 ucptr [3] = 0x80 ;
21240 continue ;
21243 value = lrint (scaled_value) ;
21244 ucptr [0] = value ;
21245 ucptr [1] = value >> 8 ;
21246 ucptr [2] = value >> 16 ;
21247 ucptr [3] = value >> 24 ;
21249 } /* d2lei_clip_array */
21251 static sf_count_t
21252 pcm_write_d2lei (SF_PRIVATE *psf, double *ptr, sf_count_t len)
21253 { void (*convert) (double *, int *, int, int) ;
21254 int bufferlen, writecount, thiswrite ;
21255 sf_count_t total = 0 ;
21257 convert = (psf->add_clipping) ? d2lei_clip_array : d2lei_array ;
21258 bufferlen = sizeof (psf->buffer) / sizeof (int) ;
21260 while (len > 0)
21261 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
21262 convert (ptr + total, (int*) (psf->buffer), writecount, psf->norm_double) ;
21263 thiswrite = psf_fwrite (psf->buffer, sizeof (int), writecount, psf) ;
21264 total += thiswrite ;
21265 len -= thiswrite ;
21266 if (thiswrite < writecount)
21267 break ;
21270 return total ;
21271 } /* pcm_write_d2lei */
21273 /*==============================================================================
21276 static void
21277 sc2s_array (signed char *src, int count, short *dest)
21278 { while (count)
21279 { count -- ;
21280 dest [count] = src [count] << 8 ;
21282 } /* sc2s_array */
21284 static void
21285 uc2s_array (unsigned char *src, int count, short *dest)
21286 { while (count)
21287 { count -- ;
21288 dest [count] = (((short) src [count]) - 0x80) << 8 ;
21290 } /* uc2s_array */
21292 static void
21293 let2s_array (tribyte *src, int count, short *dest)
21294 { unsigned char *ucptr ;
21296 ucptr = ((unsigned char*) src) + 3 * count ;
21297 while (count)
21298 { count -- ;
21299 ucptr -= 3 ;
21300 dest [count] = LET2H_SHORT_PTR (ucptr) ;
21302 } /* let2s_array */
21304 static void
21305 bet2s_array (tribyte *src, int count, short *dest)
21306 { unsigned char *ucptr ;
21308 ucptr = ((unsigned char*) src) + 3 * count ;
21309 while (count)
21310 { count -- ;
21311 ucptr -= 3 ;
21312 dest [count] = BET2H_SHORT_PTR (ucptr) ;
21314 } /* bet2s_array */
21316 static void
21317 lei2s_array (int *src, int count, short *dest)
21318 { unsigned char *ucptr ;
21320 ucptr = ((unsigned char*) src) + 4 * count ;
21321 while (count)
21322 { count -- ;
21323 ucptr -= 4 ;
21324 dest [count] = LEI2H_SHORT_PTR (ucptr) ;
21326 } /* lei2s_array */
21328 static void
21329 bei2s_array (int *src, int count, short *dest)
21330 { unsigned char *ucptr ;
21332 ucptr = ((unsigned char*) src) + 4 * count ;
21333 while (count)
21334 { count -- ;
21335 ucptr -= 4 ;
21336 dest [count] = BEI2H_SHORT_PTR (ucptr) ;
21338 } /* bei2s_array */
21340 /*-----------------------------------------------------------------------------------------------
21343 static void
21344 sc2i_array (signed char *src, int count, int *dest)
21345 { while (count)
21346 { count -- ;
21347 dest [count] = ((int) src [count]) << 24 ;
21349 } /* sc2i_array */
21351 static void
21352 uc2i_array (unsigned char *src, int count, int *dest)
21353 { while (count)
21354 { count -- ;
21355 dest [count] = (((int) src [count]) - 128) << 24 ;
21357 } /* uc2i_array */
21359 static void
21360 bes2i_array (short *src, int count, int *dest)
21361 { unsigned char *ucptr ;
21363 ucptr = ((unsigned char*) src) + 2 * count ;
21364 while (count)
21365 { count -- ;
21366 ucptr -= 2 ;
21367 dest [count] = BES2H_INT_PTR (ucptr) ;
21369 } /* bes2i_array */
21371 static void
21372 les2i_array (short *src, int count, int *dest)
21373 { unsigned char *ucptr ;
21375 ucptr = ((unsigned char*) src) + 2 * count ;
21376 while (count)
21377 { count -- ;
21378 ucptr -= 2 ;
21379 dest [count] = LES2H_INT_PTR (ucptr) ;
21381 } /* les2i_array */
21383 static void
21384 bet2i_array (tribyte *src, int count, int *dest)
21385 { unsigned char *ucptr ;
21387 ucptr = ((unsigned char*) src) + 3 * count ;
21388 while (count)
21389 { count -- ;
21390 ucptr -= 3 ;
21391 dest [count] = BET2H_INT_PTR (ucptr) ;
21393 } /* bet2i_array */
21395 static void
21396 let2i_array (tribyte *src, int count, int *dest)
21397 { unsigned char *ucptr ;
21399 ucptr = ((unsigned char*) src) + 3 * count ;
21400 while (count)
21401 { count -- ;
21402 ucptr -= 3 ;
21403 dest [count] = LET2H_INT_PTR (ucptr) ;
21405 } /* let2i_array */
21407 /*-----------------------------------------------------------------------------------------------
21411 static void
21412 sc2f_array (signed char *src, int count, float *dest, float normfact)
21413 { while (count)
21414 { count -- ;
21415 dest [count] = ((float) src [count]) * normfact ;
21417 } /* sc2f_array */
21419 static void
21420 uc2f_array (unsigned char *src, int count, float *dest, float normfact)
21421 { while (count)
21422 { count -- ;
21423 dest [count] = (((float) src [count]) - 128.0) * normfact ;
21425 } /* uc2f_array */
21427 static void
21428 les2f_array (short *src, int count, float *dest, float normfact)
21429 { short value ;
21431 while (count)
21432 { count -- ;
21433 value = src [count] ;
21434 value = LES2H_SHORT (value) ;
21435 dest [count] = ((float) value) * normfact ;
21437 } /* les2f_array */
21439 static void
21440 bes2f_array (short *src, int count, float *dest, float normfact)
21441 { short value ;
21443 while (count)
21444 { count -- ;
21445 value = src [count] ;
21446 value = BES2H_SHORT (value) ;
21447 dest [count] = ((float) value) * normfact ;
21449 } /* bes2f_array */
21451 static void
21452 let2f_array (tribyte *src, int count, float *dest, float normfact)
21453 { unsigned char *ucptr ;
21454 int value ;
21456 ucptr = ((unsigned char*) src) + 3 * count ;
21457 while (count)
21458 { count -- ;
21459 ucptr -= 3 ;
21460 value = LET2H_INT_PTR (ucptr) ;
21461 dest [count] = ((float) value) * normfact ;
21463 } /* let2f_array */
21465 static void
21466 bet2f_array (tribyte *src, int count, float *dest, float normfact)
21467 { unsigned char *ucptr ;
21468 int value ;
21470 ucptr = ((unsigned char*) src) + 3 * count ;
21471 while (count)
21472 { count -- ;
21473 ucptr -= 3 ;
21474 value = BET2H_INT_PTR (ucptr) ;
21475 dest [count] = ((float) value) * normfact ;
21477 } /* bet2f_array */
21479 static void
21480 lei2f_array (int *src, int count, float *dest, float normfact)
21481 { int value ;
21483 while (count)
21484 { count -- ;
21485 value = src [count] ;
21486 value = LEI2H_INT (value) ;
21487 dest [count] = ((float) value) * normfact ;
21489 } /* lei2f_array */
21491 static void
21492 bei2f_array (int *src, int count, float *dest, float normfact)
21493 { int value ;
21495 while (count)
21496 { count -- ;
21497 value = src [count] ;
21498 value = BEI2H_INT (value) ;
21499 dest [count] = ((float) value) * normfact ;
21501 } /* bei2f_array */
21503 /*-----------------------------------------------------------------------------------------------
21506 static void
21507 sc2d_array (signed char *src, int count, double *dest, double normfact)
21508 { while (count)
21509 { count -- ;
21510 dest [count] = ((double) src [count]) * normfact ;
21512 } /* sc2d_array */
21514 static void
21515 uc2d_array (unsigned char *src, int count, double *dest, double normfact)
21516 { while (count)
21517 { count -- ;
21518 dest [count] = (((double) src [count]) - 128.0) * normfact ;
21520 } /* uc2d_array */
21522 static void
21523 les2d_array (short *src, int count, double *dest, double normfact)
21524 { short value ;
21526 while (count)
21527 { count -- ;
21528 value = src [count] ;
21529 value = LES2H_SHORT (value) ;
21530 dest [count] = ((double) value) * normfact ;
21532 } /* les2d_array */
21534 static void
21535 bes2d_array (short *src, int count, double *dest, double normfact)
21536 { short value ;
21538 while (count)
21539 { count -- ;
21540 value = src [count] ;
21541 value = BES2H_SHORT (value) ;
21542 dest [count] = ((double) value) * normfact ;
21544 } /* bes2d_array */
21546 static void
21547 let2d_array (tribyte *src, int count, double *dest, double normfact)
21548 { unsigned char *ucptr ;
21549 int value ;
21551 ucptr = ((unsigned char*) src) + 3 * count ;
21552 while (count)
21553 { count -- ;
21554 ucptr -= 3 ;
21555 value = LET2H_INT_PTR (ucptr) ;
21556 dest [count] = ((double) value) * normfact ;
21558 } /* let2d_array */
21560 static void
21561 bet2d_array (tribyte *src, int count, double *dest, double normfact)
21562 { unsigned char *ucptr ;
21563 int value ;
21565 ucptr = ((unsigned char*) src) + 3 * count ;
21566 while (count)
21567 { count -- ;
21568 ucptr -= 3 ;
21569 value = (ucptr [0] << 24) | (ucptr [1] << 16) | (ucptr [2] << 8) ;
21570 dest [count] = ((double) value) * normfact ;
21572 } /* bet2d_array */
21574 static void
21575 lei2d_array (int *src, int count, double *dest, double normfact)
21576 { int value ;
21578 while (count)
21579 { count -- ;
21580 value = src [count] ;
21581 value = LEI2H_INT (value) ;
21582 dest [count] = ((double) value) * normfact ;
21584 } /* lei2d_array */
21586 static void
21587 bei2d_array (int *src, int count, double *dest, double normfact)
21588 { int value ;
21590 while (count)
21591 { count -- ;
21592 value = src [count] ;
21593 value = BEI2H_INT (value) ;
21594 dest [count] = ((double) value) * normfact ;
21596 } /* bei2d_array */
21598 /*-----------------------------------------------------------------------------------------------
21601 static void
21602 s2sc_array (short *src, signed char *dest, int count)
21603 { while (count)
21604 { count -- ;
21605 dest [count] = src [count] >> 8 ;
21607 } /* s2sc_array */
21609 static void
21610 s2uc_array (short *src, unsigned char *dest, int count)
21611 { while (count)
21612 { count -- ;
21613 dest [count] = (src [count] >> 8) + 0x80 ;
21615 } /* s2uc_array */
21617 static void
21618 s2let_array (short *src, tribyte *dest, int count)
21619 { unsigned char *ucptr ;
21621 ucptr = ((unsigned char*) dest) + 3 * count ;
21622 while (count)
21623 { count -- ;
21624 ucptr -= 3 ;
21625 ucptr [0] = 0 ;
21626 ucptr [1] = src [count] ;
21627 ucptr [2] = src [count] >> 8 ;
21629 } /* s2let_array */
21631 static void
21632 s2bet_array (short *src, tribyte *dest, int count)
21633 { unsigned char *ucptr ;
21635 ucptr = ((unsigned char*) dest) + 3 * count ;
21636 while (count)
21637 { count -- ;
21638 ucptr -= 3 ;
21639 ucptr [2] = 0 ;
21640 ucptr [1] = src [count] ;
21641 ucptr [0] = src [count] >> 8 ;
21643 } /* s2bet_array */
21645 static void
21646 s2lei_array (short *src, int *dest, int count)
21647 { unsigned char *ucptr ;
21649 ucptr = ((unsigned char*) dest) + 4 * count ;
21650 while (count)
21651 { count -- ;
21652 ucptr -= 4 ;
21653 ucptr [0] = 0 ;
21654 ucptr [1] = 0 ;
21655 ucptr [2] = src [count] ;
21656 ucptr [3] = src [count] >> 8 ;
21658 } /* s2lei_array */
21660 static void
21661 s2bei_array (short *src, int *dest, int count)
21662 { unsigned char *ucptr ;
21664 ucptr = ((unsigned char*) dest) + 4 * count ;
21665 while (count)
21666 { count -- ;
21667 ucptr -= 4 ;
21668 ucptr [0] = src [count] >> 8 ;
21669 ucptr [1] = src [count] ;
21670 ucptr [2] = 0 ;
21671 ucptr [3] = 0 ;
21673 } /* s2bei_array */
21675 /*-----------------------------------------------------------------------------------------------
21678 static void
21679 i2sc_array (int *src, signed char *dest, int count)
21680 { while (count)
21681 { count -- ;
21682 dest [count] = (src [count] >> 24) ;
21684 } /* i2sc_array */
21686 static void
21687 i2uc_array (int *src, unsigned char *dest, int count)
21688 { while (count)
21689 { count -- ;
21690 dest [count] = ((src [count] >> 24) + 128) ;
21692 } /* i2uc_array */
21694 static void
21695 i2bes_array (int *src, short *dest, int count)
21696 { unsigned char *ucptr ;
21698 ucptr = ((unsigned char*) dest) + 2 * count ;
21699 while (count)
21700 { count -- ;
21701 ucptr -= 2 ;
21702 ucptr [0] = src [count] >> 24 ;
21703 ucptr [1] = src [count] >> 16 ;
21705 } /* i2bes_array */
21707 static void
21708 i2les_array (int *src, short *dest, int count)
21709 { unsigned char *ucptr ;
21711 ucptr = ((unsigned char*) dest) + 2 * count ;
21712 while (count)
21713 { count -- ;
21714 ucptr -= 2 ;
21715 ucptr [0] = src [count] >> 16 ;
21716 ucptr [1] = src [count] >> 24 ;
21718 } /* i2les_array */
21720 static void
21721 i2let_array (int *src, tribyte *dest, int count)
21722 { unsigned char *ucptr ;
21723 int value ;
21725 ucptr = ((unsigned char*) dest) + 3 * count ;
21726 while (count)
21727 { count -- ;
21728 ucptr -= 3 ;
21729 value = src [count] >> 8 ;
21730 ucptr [0] = value ;
21731 ucptr [1] = value >> 8 ;
21732 ucptr [2] = value >> 16 ;
21734 } /* i2let_array */
21736 static void
21737 i2bet_array (int *src, tribyte *dest, int count)
21738 { unsigned char *ucptr ;
21739 int value ;
21741 ucptr = ((unsigned char*) dest) + 3 * count ;
21742 while (count)
21743 { count -- ;
21744 ucptr -= 3 ;
21745 value = src [count] >> 8 ;
21746 ucptr [2] = value ;
21747 ucptr [1] = value >> 8 ;
21748 ucptr [0] = value >> 16 ;
21750 } /* i2bet_array */
21752 /*-----------------------------------------------------------------------------------------------
21755 /*-----------------------------------------------------------------------------------------------
21758 /*==============================================================================
21761 ** Do not edit or modify anything in this comment block.
21762 ** The arch-tag line is a file identity tag for the GNU Arch
21763 ** revision control system.
21765 ** arch-tag: d8bc7c0e-1e2f-4ff3-a28f-10ce1fbade3b
21768 * Copyright 1992 by Jutta Degener and Carsten Bormann, Technische
21769 * Universitaet Berlin. See the accompanying file "COPYRIGHT" for
21770 * details. THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE.
21773 #include <stdio.h>
21774 #include <assert.h>
21778 /* 4.2.0 .. 4.2.3 PREPROCESSING SECTION
21780 * After A-law to linear conversion (or directly from the
21781 * Ato D converter) the following scaling is assumed for
21782 * input to the RPE-LTP algorithm:
21784 * in: 0.1.....................12
21785 * S.v.v.v.v.v.v.v.v.v.v.v.v.*.*.*
21787 * Where S is the sign bit, v a valid bit, and * a "don't care" bit.
21788 * The original signal is called sop[..]
21790 * out: 0.1................... 12
21791 * S.S.v.v.v.v.v.v.v.v.v.v.v.v.0.0
21795 void Gsm_Preprocess (
21796 struct gsm_state * S,
21797 word * s,
21798 word * so ) /* [0..159] IN/OUT */
21801 word z1 = S->z1;
21802 longword L_z2 = S->L_z2;
21803 word mp = S->mp;
21805 word s1;
21806 longword L_s2;
21808 longword L_temp;
21810 word msp, lsp;
21811 word SO;
21813 register int k = 160;
21815 while (k--) {
21817 /* 4.2.1 Downscaling of the input signal
21819 SO = SASR_W( *s, 3 ) << 2;
21820 s++;
21822 assert (SO >= -0x4000); /* downscaled by */
21823 assert (SO <= 0x3FFC); /* previous routine. */
21826 /* 4.2.2 Offset compensation
21828 * This part implements a high-pass filter and requires extended
21829 * arithmetic precision for the recursive part of this filter.
21830 * The input of this procedure is the array so[0...159] and the
21831 * output the array sof[ 0...159 ].
21833 /* Compute the non-recursive part
21836 s1 = SO - z1; /* s1 = gsm_sub( *so, z1 ); */
21837 z1 = SO;
21839 assert(s1 != MIN_WORD);
21841 /* Compute the recursive part
21843 L_s2 = s1;
21844 L_s2 <<= 15;
21846 /* Execution of a 31 bv 16 bits multiplication
21849 msp = SASR_L( L_z2, 15 );
21850 lsp = L_z2-((longword)msp<<15); /* gsm_L_sub(L_z2,(msp<<15)); */
21852 L_s2 += GSM_MULT_R( lsp, 32735 );
21853 L_temp = (longword)msp * 32735; /* GSM_L_MULT(msp,32735) >> 1;*/
21854 L_z2 = GSM_L_ADD( L_temp, L_s2 );
21856 /* Compute sof[k] with rounding
21858 L_temp = GSM_L_ADD( L_z2, 16384 );
21860 /* 4.2.3 Preemphasis
21863 msp = GSM_MULT_R( mp, -28180 );
21864 mp = SASR_L( L_temp, 15 );
21865 *so++ = GSM_ADD( mp, msp );
21868 S->z1 = z1;
21869 S->L_z2 = L_z2;
21870 S->mp = mp;
21873 ** Do not edit or modify anything in this comment block.
21874 ** The arch-tag line is a file identity tag for the GNU Arch
21875 ** revision control system.
21877 ** arch-tag: b760b0d9-3a05-4da3-9dc9-441ffb905d87
21881 ** Copyright (C) 2002-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
21883 ** This program is free software; you can redistribute it and/or modify
21884 ** it under the terms of the GNU Lesser General Public License as published by
21885 ** the Free Software Foundation; either version 2.1 of the License, or
21886 ** (at your option) any later version.
21888 ** This program is distributed in the hope that it will be useful,
21889 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
21890 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21891 ** GNU Lesser General Public License for more details.
21893 ** You should have received a copy of the GNU Lesser General Public License
21894 ** along with this program; if not, write to the Free Software
21895 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21898 #include <stdio.h>
21899 #include <fcntl.h>
21900 #include <string.h>
21901 #include <ctype.h>
21904 /*------------------------------------------------------------------------------
21905 ** Macros to handle big/little endian issues.
21908 #define PVF1_MARKER (MAKE_MARKER ('P', 'V', 'F', '1'))
21910 /*------------------------------------------------------------------------------
21911 ** Private static functions.
21914 static int pvf_close (SF_PRIVATE *psf) ;
21916 static int pvf_write_header (SF_PRIVATE *psf, int calc_length) ;
21917 static int pvf_read_header (SF_PRIVATE *psf) ;
21919 /*------------------------------------------------------------------------------
21920 ** Public function.
21924 pvf_open (SF_PRIVATE *psf)
21925 { int subformat ;
21926 int error = 0 ;
21928 if (psf->mode == SFM_READ || (psf->mode == SFM_RDWR && psf->filelength > 0))
21929 { if ((error = pvf_read_header (psf)))
21930 return error ;
21933 subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
21935 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
21936 { if ((psf->sf.format & SF_FORMAT_TYPEMASK) != SF_FORMAT_PVF)
21937 return SFE_BAD_OPEN_FORMAT ;
21939 psf->endian = SF_ENDIAN_BIG ;
21941 if (pvf_write_header (psf, SF_FALSE))
21942 return psf->error ;
21944 psf->write_header = pvf_write_header ;
21947 psf->close = pvf_close ;
21949 psf->blockwidth = psf->bytewidth * psf->sf.channels ;
21951 switch (subformat)
21952 { case SF_FORMAT_PCM_S8 : /* 8-bit linear PCM. */
21953 case SF_FORMAT_PCM_16 : /* 16-bit linear PCM. */
21954 case SF_FORMAT_PCM_32 : /* 32-bit linear PCM. */
21955 error = pcm_init (psf) ;
21956 break ;
21958 default : break ;
21961 return error ;
21962 } /* pvf_open */
21964 /*------------------------------------------------------------------------------
21967 static int
21968 pvf_close (SF_PRIVATE *psf)
21970 psf = psf ;
21972 return 0 ;
21973 } /* pvf_close */
21975 static int
21976 pvf_write_header (SF_PRIVATE *psf, int calc_length)
21977 { sf_count_t current ;
21979 if (psf->pipeoffset > 0)
21980 return 0 ;
21982 calc_length = calc_length ; /* Avoid a compiler warning. */
21984 current = psf_ftell (psf) ;
21986 /* Reset the current header length to zero. */
21987 psf->header [0] = 0 ;
21988 psf->headindex = 0 ;
21990 if (psf->is_pipe == SF_FALSE)
21991 psf_fseek (psf, 0, SEEK_SET) ;
21993 LSF_SNPRINTF ((char*) psf->header, sizeof (psf->header), "PVF1\n%d %d %d\n",
21994 psf->sf.channels, psf->sf.samplerate, psf->bytewidth * 8) ;
21996 psf->headindex = strlen ((char*) psf->header) ;
21998 /* Header construction complete so write it out. */
21999 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
22001 if (psf->error)
22002 return psf->error ;
22004 psf->dataoffset = psf->headindex ;
22006 if (current > 0)
22007 psf_fseek (psf, current, SEEK_SET) ;
22009 return psf->error ;
22010 } /* pvf_write_header */
22012 static int
22013 pvf_read_header (SF_PRIVATE *psf)
22014 { char buffer [32] ;
22015 int marker, channels, samplerate, bitwidth ;
22017 psf_binheader_readf (psf, "pmj", 0, &marker, 1) ;
22018 psf_log_printf (psf, "%M\n", marker) ;
22020 if (marker != PVF1_MARKER)
22021 return SFE_PVF_NO_PVF1 ;
22023 /* Grab characters up until a newline which is replaced by an EOS. */
22024 psf_binheader_readf (psf, "G", buffer, sizeof (buffer)) ;
22026 if (sscanf (buffer, "%d %d %d", &channels, &samplerate, &bitwidth) != 3)
22027 return SFE_PVF_BAD_HEADER ;
22029 psf_log_printf (psf, " Channels : %d\n Sample rate : %d\n Bit width : %d\n",
22030 channels, samplerate, bitwidth) ;
22032 psf->sf.channels = channels ;
22033 psf->sf.samplerate = samplerate ;
22035 switch (bitwidth)
22036 { case 8 :
22037 psf->sf.format = SF_FORMAT_PVF | SF_FORMAT_PCM_S8 ;
22038 psf->bytewidth = 1 ;
22039 break ;
22041 case 16 :
22042 psf->sf.format = SF_FORMAT_PVF | SF_FORMAT_PCM_16 ;
22043 psf->bytewidth = 2 ;
22044 break ;
22045 case 32 :
22046 psf->sf.format = SF_FORMAT_PVF | SF_FORMAT_PCM_32 ;
22047 psf->bytewidth = 4 ;
22048 break ;
22050 default :
22051 return SFE_PVF_BAD_BITWIDTH ;
22054 psf->dataoffset = psf_ftell (psf) ;
22055 psf_log_printf (psf, " Data Offset : %D\n", psf->dataoffset) ;
22057 psf->endian = SF_ENDIAN_BIG ;
22059 psf->datalength = psf->filelength - psf->dataoffset ;
22060 psf->blockwidth = psf->sf.channels * psf->bytewidth ;
22062 psf->close = pvf_close ;
22064 if (! psf->sf.frames && psf->blockwidth)
22065 psf->sf.frames = (psf->filelength - psf->dataoffset) / psf->blockwidth ;
22067 return 0 ;
22068 } /* pvf_read_header */
22070 ** Do not edit or modify anything in this comment block.
22071 ** The arch-tag line is a file identity tag for the GNU Arch
22072 ** revision control system.
22074 ** arch-tag: 20a26761-8bc1-41d7-b1f3-9793bf3d9864
22077 ** Copyright (C) 1999-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
22079 ** This program is free software; you can redistribute it and/or modify
22080 ** it under the terms of the GNU Lesser General Public License as published by
22081 ** the Free Software Foundation; either version 2.1 of the License, or
22082 ** (at your option) any later version.
22084 ** This program is distributed in the hope that it will be useful,
22085 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
22086 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22087 ** GNU Lesser General Public License for more details.
22089 ** You should have received a copy of the GNU Lesser General Public License
22090 ** along with this program; if not, write to the Free Software
22091 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22094 #include <stdio.h>
22097 /*------------------------------------------------------------------------------
22098 ** Public function.
22102 raw_open (SF_PRIVATE *psf)
22103 { int subformat, error = SFE_NO_ERROR ;
22105 subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
22107 psf->endian = psf->sf.format & SF_FORMAT_ENDMASK ;
22109 if (CPU_IS_BIG_ENDIAN && (psf->endian == 0 || psf->endian == SF_ENDIAN_CPU))
22110 psf->endian = SF_ENDIAN_BIG ;
22111 else if (CPU_IS_LITTLE_ENDIAN && (psf->endian == 0 || psf->endian == SF_ENDIAN_CPU))
22112 psf->endian = SF_ENDIAN_LITTLE ;
22114 psf->blockwidth = psf->bytewidth * psf->sf.channels ;
22115 psf->dataoffset = 0 ;
22116 psf->datalength = psf->filelength ;
22117 switch (subformat)
22118 { case SF_FORMAT_PCM_S8 :
22119 error = pcm_init (psf) ;
22120 break ;
22122 case SF_FORMAT_PCM_U8 :
22123 error = pcm_init (psf) ;
22124 break ;
22126 case SF_FORMAT_PCM_16 :
22127 case SF_FORMAT_PCM_24 :
22128 case SF_FORMAT_PCM_32 :
22129 error = pcm_init (psf) ;
22130 break ;
22132 case SF_FORMAT_ULAW :
22133 error = ulaw_init (psf) ;
22134 break ;
22136 case SF_FORMAT_ALAW :
22137 error = alaw_init (psf) ;
22138 break ;
22140 case SF_FORMAT_GSM610 :
22141 error = gsm610_init (psf) ;
22142 break ;
22144 /* Lite remove start */
22145 case SF_FORMAT_FLOAT :
22146 error = float32_init (psf) ;
22147 break ;
22149 case SF_FORMAT_DOUBLE :
22150 error = double64_init (psf) ;
22151 break ;
22153 case SF_FORMAT_DWVW_12 :
22154 error = dwvw_init (psf, 12) ;
22155 break ;
22157 case SF_FORMAT_DWVW_16 :
22158 error = dwvw_init (psf, 16) ;
22159 break ;
22161 case SF_FORMAT_DWVW_24 :
22162 error = dwvw_init (psf, 24) ;
22163 break ;
22165 case SF_FORMAT_VOX_ADPCM :
22166 error = vox_adpcm_init (psf) ;
22167 break ;
22168 /* Lite remove end */
22170 default : return SFE_BAD_OPEN_FORMAT ;
22173 return error ;
22174 } /* raw_open */
22176 ** Do not edit or modify anything in this comment block.
22177 ** The arch-tag line is a file identity tag for the GNU Arch
22178 ** revision control system.
22180 ** arch-tag: f0066de7-d6ce-4f36-a1e0-e475c07d4e1a
22183 * Copyright 1992 by Jutta Degener and Carsten Bormann, Technische
22184 * Universitaet Berlin. See the accompanying file "COPYRIGHT" for
22185 * details. THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE.
22188 #include <stdio.h>
22189 #include <assert.h>
22193 /* 4.2.13 .. 4.2.17 RPE ENCODING SECTION
22196 /* 4.2.13 */
22198 static void Weighting_filter (
22199 register word * e, /* signal [-5..0.39.44] IN */
22200 word * x /* signal [0..39] OUT */
22203 * The coefficients of the weighting filter are stored in a table
22204 * (see table 4.4). The following scaling is used:
22206 * H[0..10] = integer( real_H[ 0..10] * 8192 );
22209 /* word wt[ 50 ]; */
22211 register longword L_result;
22212 register int k /* , i */ ;
22214 /* Initialization of a temporary working array wt[0...49]
22217 /* for (k = 0; k <= 4; k++) wt[k] = 0;
22218 * for (k = 5; k <= 44; k++) wt[k] = *e++;
22219 * for (k = 45; k <= 49; k++) wt[k] = 0;
22221 * (e[-5..-1] and e[40..44] are allocated by the caller,
22222 * are initially zero and are not written anywhere.)
22224 e -= 5;
22226 /* Compute the signal x[0..39]
22228 for (k = 0; k <= 39; k++) {
22230 L_result = 8192 >> 1;
22232 /* for (i = 0; i <= 10; i++) {
22233 * L_temp = GSM_L_MULT( wt[k+i], gsm_H[i] );
22234 * L_result = GSM_L_ADD( L_result, L_temp );
22238 #undef rpeSTEP
22239 #define rpeSTEP( i, H ) (e[ k + i ] * (longword)H)
22241 /* Every one of these multiplications is done twice --
22242 * but I don't see an elegant way to optimize this.
22243 * Do you?
22246 #ifdef STUPID_COMPILER
22247 L_result += rpeSTEP( 0, -134 ) ;
22248 L_result += rpeSTEP( 1, -374 ) ;
22249 /* + rpeSTEP( 2, 0 ) */
22250 L_result += rpeSTEP( 3, 2054 ) ;
22251 L_result += rpeSTEP( 4, 5741 ) ;
22252 L_result += rpeSTEP( 5, 8192 ) ;
22253 L_result += rpeSTEP( 6, 5741 ) ;
22254 L_result += rpeSTEP( 7, 2054 ) ;
22255 /* + rpeSTEP( 8, 0 ) */
22256 L_result += rpeSTEP( 9, -374 ) ;
22257 L_result += rpeSTEP( 10, -134 ) ;
22258 #else
22259 L_result +=
22260 rpeSTEP( 0, -134 )
22261 + rpeSTEP( 1, -374 )
22262 /* + rpeSTEP( 2, 0 ) */
22263 + rpeSTEP( 3, 2054 )
22264 + rpeSTEP( 4, 5741 )
22265 + rpeSTEP( 5, 8192 )
22266 + rpeSTEP( 6, 5741 )
22267 + rpeSTEP( 7, 2054 )
22268 /* + rpeSTEP( 8, 0 ) */
22269 + rpeSTEP( 9, -374 )
22270 + rpeSTEP(10, -134 )
22272 #endif
22274 /* L_result = GSM_L_ADD( L_result, L_result ); (* scaling(x2) *)
22275 * L_result = GSM_L_ADD( L_result, L_result ); (* scaling(x4) *)
22277 * x[k] = SASR( L_result, 16 );
22280 /* 2 adds vs. >>16 => 14, minus one shift to compensate for
22281 * those we lost when replacing L_MULT by '*'.
22284 L_result = SASR_L( L_result, 13 );
22285 x[k] = ( L_result < MIN_WORD ? MIN_WORD
22286 : (L_result > MAX_WORD ? MAX_WORD : L_result ));
22290 /* 4.2.14 */
22292 static void RPE_grid_selection (
22293 word * x, /* [0..39] IN */
22294 word * xM, /* [0..12] OUT */
22295 word * Mc_out /* OUT */
22298 * The signal x[0..39] is used to select the RPE grid which is
22299 * represented by Mc.
22302 /* register word temp1; */
22303 register int /* m, */ i;
22304 register longword L_result, L_temp;
22305 longword EM; /* xxx should be L_EM? */
22306 word Mc;
22308 longword L_common_0_3;
22310 EM = 0;
22311 Mc = 0;
22313 /* for (m = 0; m <= 3; m++) {
22314 * L_result = 0;
22317 * for (i = 0; i <= 12; i++) {
22319 * temp1 = SASR_W( x[m + 3*i], 2 );
22321 * assert(temp1 != MIN_WORD);
22323 * L_temp = GSM_L_MULT( temp1, temp1 );
22324 * L_result = GSM_L_ADD( L_temp, L_result );
22327 * if (L_result > EM) {
22328 * Mc = m;
22329 * EM = L_result;
22334 #undef rpeSTEP
22335 #define rpeSTEP( m, i ) L_temp = SASR_W( x[m + 3 * i], 2 ); \
22336 L_result += L_temp * L_temp;
22338 /* common part of 0 and 3 */
22340 L_result = 0;
22341 rpeSTEP( 0, 1 ); rpeSTEP( 0, 2 ); rpeSTEP( 0, 3 ); rpeSTEP( 0, 4 );
22342 rpeSTEP( 0, 5 ); rpeSTEP( 0, 6 ); rpeSTEP( 0, 7 ); rpeSTEP( 0, 8 );
22343 rpeSTEP( 0, 9 ); rpeSTEP( 0, 10); rpeSTEP( 0, 11); rpeSTEP( 0, 12);
22344 L_common_0_3 = L_result;
22346 /* i = 0 */
22348 rpeSTEP( 0, 0 );
22349 L_result <<= 1; /* implicit in L_MULT */
22350 EM = L_result;
22352 /* i = 1 */
22354 L_result = 0;
22355 rpeSTEP( 1, 0 );
22356 rpeSTEP( 1, 1 ); rpeSTEP( 1, 2 ); rpeSTEP( 1, 3 ); rpeSTEP( 1, 4 );
22357 rpeSTEP( 1, 5 ); rpeSTEP( 1, 6 ); rpeSTEP( 1, 7 ); rpeSTEP( 1, 8 );
22358 rpeSTEP( 1, 9 ); rpeSTEP( 1, 10); rpeSTEP( 1, 11); rpeSTEP( 1, 12);
22359 L_result <<= 1;
22360 if (L_result > EM) {
22361 Mc = 1;
22362 EM = L_result;
22365 /* i = 2 */
22367 L_result = 0;
22368 rpeSTEP( 2, 0 );
22369 rpeSTEP( 2, 1 ); rpeSTEP( 2, 2 ); rpeSTEP( 2, 3 ); rpeSTEP( 2, 4 );
22370 rpeSTEP( 2, 5 ); rpeSTEP( 2, 6 ); rpeSTEP( 2, 7 ); rpeSTEP( 2, 8 );
22371 rpeSTEP( 2, 9 ); rpeSTEP( 2, 10); rpeSTEP( 2, 11); rpeSTEP( 2, 12);
22372 L_result <<= 1;
22373 if (L_result > EM) {
22374 Mc = 2;
22375 EM = L_result;
22378 /* i = 3 */
22380 L_result = L_common_0_3;
22381 rpeSTEP( 3, 12 );
22382 L_result <<= 1;
22383 if (L_result > EM) {
22384 Mc = 3;
22385 EM = L_result;
22388 /**/
22390 /* Down-sampling by a factor 3 to get the selected xM[0..12]
22391 * RPE sequence.
22393 for (i = 0; i <= 12; i ++) xM[i] = x[Mc + 3*i];
22394 *Mc_out = Mc;
22397 /* 4.12.15 */
22399 static void APCM_quantization_xmaxc_to_exp_mant (
22400 word xmaxc, /* IN */
22401 word * expon_out, /* OUT */
22402 word * mant_out ) /* OUT */
22404 word expon, mant;
22406 /* Compute expononent and mantissa of the decoded version of xmaxc
22409 expon = 0;
22410 if (xmaxc > 15) expon = SASR_W(xmaxc, 3) - 1;
22411 mant = xmaxc - (expon << 3);
22413 if (mant == 0) {
22414 expon = -4;
22415 mant = 7;
22417 else {
22418 while (mant <= 7) {
22419 mant = mant << 1 | 1;
22420 expon--;
22422 mant -= 8;
22425 assert( expon >= -4 && expon <= 6 );
22426 assert( mant >= 0 && mant <= 7 );
22428 *expon_out = expon;
22429 *mant_out = mant;
22432 static void APCM_quantization (
22433 word * xM, /* [0..12] IN */
22434 word * xMc, /* [0..12] OUT */
22435 word * mant_out, /* OUT */
22436 word * expon_out, /* OUT */
22437 word * xmaxc_out /* OUT */
22440 int i, itest;
22442 word xmax, xmaxc, temp, temp1, temp2;
22443 word expon, mant;
22446 /* Find the maximum absolute value xmax of xM[0..12].
22449 xmax = 0;
22450 for (i = 0; i <= 12; i++) {
22451 temp = xM[i];
22452 temp = GSM_ABS(temp);
22453 if (temp > xmax) xmax = temp;
22456 /* Qantizing and coding of xmax to get xmaxc.
22459 expon = 0;
22460 temp = SASR_W( xmax, 9 );
22461 itest = 0;
22463 for (i = 0; i <= 5; i++) {
22465 itest |= (temp <= 0);
22466 temp = SASR_W( temp, 1 );
22468 assert(expon <= 5);
22469 if (itest == 0) expon++; /* expon = add (expon, 1) */
22472 assert(expon <= 6 && expon >= 0);
22473 temp = expon + 5;
22475 assert(temp <= 11 && temp >= 0);
22476 xmaxc = gsm_add( SASR_W(xmax, temp), (word) (expon << 3) );
22478 /* Quantizing and coding of the xM[0..12] RPE sequence
22479 * to get the xMc[0..12]
22482 APCM_quantization_xmaxc_to_exp_mant( xmaxc, &expon, &mant );
22484 /* This computation uses the fact that the decoded version of xmaxc
22485 * can be calculated by using the expononent and the mantissa part of
22486 * xmaxc (logarithmic table).
22487 * So, this method avoids any division and uses only a scaling
22488 * of the RPE samples by a function of the expononent. A direct
22489 * multiplication by the inverse of the mantissa (NRFAC[0..7]
22490 * found in table 4.5) gives the 3 bit coded version xMc[0..12]
22491 * of the RPE samples.
22495 /* Direct computation of xMc[0..12] using table 4.5
22498 assert( expon <= 4096 && expon >= -4096);
22499 assert( mant >= 0 && mant <= 7 );
22501 temp1 = 6 - expon; /* normalization by the expononent */
22502 temp2 = gsm_NRFAC[ mant ]; /* inverse mantissa */
22504 for (i = 0; i <= 12; i++) {
22506 assert(temp1 >= 0 && temp1 < 16);
22508 temp = xM[i] << temp1;
22509 temp = GSM_MULT( temp, temp2 );
22510 temp = SASR_W(temp, 12);
22511 xMc[i] = temp + 4; /* see note below */
22514 /* NOTE: This equation is used to make all the xMc[i] positive.
22517 *mant_out = mant;
22518 *expon_out = expon;
22519 *xmaxc_out = xmaxc;
22522 /* 4.2.16 */
22524 static void APCM_inverse_quantization (
22525 register word * xMc, /* [0..12] IN */
22526 word mant,
22527 word expon,
22528 register word * xMp) /* [0..12] OUT */
22530 * This part is for decoding the RPE sequence of coded xMc[0..12]
22531 * samples to obtain the xMp[0..12] array. Table 4.6 is used to get
22532 * the mantissa of xmaxc (FAC[0..7]).
22535 int i;
22536 word temp, temp1, temp2, temp3;
22538 assert( mant >= 0 && mant <= 7 );
22540 temp1 = gsm_FAC[ mant ]; /* see 4.2-15 for mant */
22541 temp2 = gsm_sub( 6, expon ); /* see 4.2-15 for exp */
22542 temp3 = gsm_asl( 1, gsm_sub( temp2, 1 ));
22544 for (i = 13; i--;) {
22546 assert( *xMc <= 7 && *xMc >= 0 ); /* 3 bit unsigned */
22548 /* temp = gsm_sub( *xMc++ << 1, 7 ); */
22549 temp = (*xMc++ << 1) - 7; /* restore sign */
22550 assert( temp <= 7 && temp >= -7 ); /* 4 bit signed */
22552 temp <<= 12; /* 16 bit signed */
22553 temp = GSM_MULT_R( temp1, temp );
22554 temp = GSM_ADD( temp, temp3 );
22555 *xMp++ = gsm_asr( temp, temp2 );
22559 /* 4.2.17 */
22561 static void RPE_grid_positioning (
22562 word Mc, /* grid position IN */
22563 register word * xMp, /* [0..12] IN */
22564 register word * ep /* [0..39] OUT */
22567 * This procedure computes the reconstructed long term residual signal
22568 * ep[0..39] for the LTP analysis filter. The inputs are the Mc
22569 * which is the grid position selection and the xMp[0..12] decoded
22570 * RPE samples which are upsampled by a factor of 3 by inserting zero
22571 * values.
22574 int i = 13;
22576 assert(0 <= Mc && Mc <= 3);
22578 switch (Mc) {
22579 case 3: *ep++ = 0;
22580 case 2: do {
22581 *ep++ = 0;
22582 case 1: *ep++ = 0;
22583 case 0: *ep++ = *xMp++;
22584 } while (--i);
22586 while (++Mc < 4) *ep++ = 0;
22590 int i, k;
22591 for (k = 0; k <= 39; k++) ep[k] = 0;
22592 for (i = 0; i <= 12; i++) {
22593 ep[ Mc + (3*i) ] = xMp[i];
22598 /* 4.2.18 */
22600 /* This procedure adds the reconstructed long term residual signal
22601 * ep[0..39] to the estimated signal dpp[0..39] from the long term
22602 * analysis filter to compute the reconstructed short term residual
22603 * signal dp[-40..-1]; also the reconstructed short term residual
22604 * array dp[-120..-41] is updated.
22607 #if 0 /* Has been inlined in code.c */
22608 void Gsm_Update_of_reconstructed_short_time_residual_signal (
22609 word * dpp, /* [0...39] IN */
22610 word * ep, /* [0...39] IN */
22611 word * dp) /* [-120...-1] IN/OUT */
22613 int k;
22615 for (k = 0; k <= 79; k++)
22616 dp[ -120 + k ] = dp[ -80 + k ];
22618 for (k = 0; k <= 39; k++)
22619 dp[ -40 + k ] = gsm_add( ep[k], dpp[k] );
22621 #endif /* Has been inlined in code.c */
22623 void Gsm_RPE_Encoding (
22624 /*-struct gsm_state * S,-*/
22626 word * e, /* -5..-1][0..39][40..44 IN/OUT */
22627 word * xmaxc, /* OUT */
22628 word * Mc, /* OUT */
22629 word * xMc) /* [0..12] OUT */
22631 word x[40];
22632 word xM[13], xMp[13];
22633 word mant, expon;
22635 Weighting_filter(e, x);
22636 RPE_grid_selection(x, xM, Mc);
22638 APCM_quantization( xM, xMc, &mant, &expon, xmaxc);
22639 APCM_inverse_quantization( xMc, mant, expon, xMp);
22641 RPE_grid_positioning( *Mc, xMp, e );
22645 void Gsm_RPE_Decoding (
22646 /*-struct gsm_state * S,-*/
22648 word xmaxcr,
22649 word Mcr,
22650 word * xMcr, /* [0..12], 3 bits IN */
22651 word * erp /* [0..39] OUT */
22654 word expon, mant;
22655 word xMp[ 13 ];
22657 APCM_quantization_xmaxc_to_exp_mant( xmaxcr, &expon, &mant );
22658 APCM_inverse_quantization( xMcr, mant, expon, xMp );
22659 RPE_grid_positioning( Mcr, xMp, erp );
22663 ** Do not edit or modify anything in this comment block.
22664 ** The arch-tag line is a file identity tag for the GNU Arch
22665 ** revision control system.
22667 ** arch-tag: 82005b9e-1560-4e94-9ddb-00cb14867295
22671 ** Copyright (C) 2001-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
22673 ** This program is free software; you can redistribute it and/or modify
22674 ** it under the terms of the GNU Lesser General Public License as published by
22675 ** the Free Software Foundation; either version 2.1 of the License, or
22676 ** (at your option) any later version.
22678 ** This program is distributed in the hope that it will be useful,
22679 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
22680 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22681 ** GNU Lesser General Public License for more details.
22683 ** You should have received a copy of the GNU Lesser General Public License
22684 ** along with this program; if not, write to the Free Software
22685 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22688 #include <stdio.h>
22689 #include <string.h>
22690 #include <ctype.h>
22691 #include <stdarg.h>
22694 #if (ENABLE_EXPERIMENTAL_CODE == 0)
22697 rx2_open (SF_PRIVATE *psf)
22698 { if (psf)
22699 return SFE_UNIMPLEMENTED ;
22700 return (psf && 0) ;
22701 } /* rx2_open */
22703 #else
22705 /*------------------------------------------------------------------------------
22706 * Macros to handle big/little endian issues.
22709 #define CAT_MARKER (MAKE_MARKER ('C', 'A', 'T', ' '))
22710 #define GLOB_MARKER (MAKE_MARKER ('G', 'L', 'O', 'B'))
22712 #define RECY_MARKER (MAKE_MARKER ('R', 'E', 'C', 'Y'))
22714 #define SLCL_MARKER (MAKE_MARKER ('S', 'L', 'C', 'L'))
22715 #define SLCE_MARKER (MAKE_MARKER ('S', 'L', 'C', 'E'))
22717 #define DEVL_MARKER (MAKE_MARKER ('D', 'E', 'V', 'L'))
22718 #define TRSH_MARKER (MAKE_MARKER ('T', 'R', 'S', 'H'))
22720 #define EQ_MARKER (MAKE_MARKER ('E', 'Q', ' ', ' '))
22721 #define COMP_MARKER (MAKE_MARKER ('C', 'O', 'M', 'P'))
22723 #define SINF_MARKER (MAKE_MARKER ('S', 'I', 'N', 'F'))
22724 #define SDAT_MARKER (MAKE_MARKER ('S', 'D', 'A', 'T'))
22726 /*------------------------------------------------------------------------------
22727 * Typedefs for file chunks.
22731 /*------------------------------------------------------------------------------
22732 * Private static functions.
22734 static int rx2_close (SF_PRIVATE *psf) ;
22736 /*------------------------------------------------------------------------------
22737 ** Public functions.
22741 rx2_open (SF_PRIVATE *psf)
22742 { static char *marker_type [4] =
22743 { "Original Enabled", "Enabled Hidden",
22744 "Additional/PencilTool", "Disabled"
22747 int error, marker, length, glob_offset, slce_count, frames ;
22749 int sdat_length = 0, slce_total = 0 ;
22751 int n_channels ;
22754 /* So far only doing read. */
22756 psf_binheader_readf (psf, "Epm4", 0, &marker, &length) ;
22758 if (marker != CAT_MARKER)
22759 { psf_log_printf (psf, "length : %d\n", length) ;
22760 return -1000 ;
22763 if (length != psf->filelength - 8)
22764 psf_log_printf (psf, "%M : %d (should be %d)\n", marker, length, psf->filelength - 8) ;
22765 else
22766 psf_log_printf (psf, "%M : %d\n", marker, length) ;
22768 /* 'REX2' marker */
22769 psf_binheader_readf (psf, "m", &marker) ;
22770 psf_log_printf (psf, "%M", marker) ;
22772 /* 'HEAD' marker */
22773 psf_binheader_readf (psf, "m", &marker) ;
22774 psf_log_printf (psf, "%M\n", marker) ;
22776 /* Grab 'GLOB' offset. */
22777 psf_binheader_readf (psf, "E4", &glob_offset) ;
22778 glob_offset += 0x14 ; /* Add the current file offset. */
22780 /* Jump to offset 0x30 */
22781 psf_binheader_readf (psf, "p", 0x30) ;
22783 /* Get name length */
22784 length = 0 ;
22785 psf_binheader_readf (psf, "1", &length) ;
22786 if (length >= SIGNED_SIZEOF (psf->buffer))
22787 { psf_log_printf (psf, " Text : %d *** Error : Too sf_count_t!\n") ;
22788 return -1001 ;
22791 memset (psf->buffer, 0, SIGNED_SIZEOF (psf->buffer)) ;
22792 psf_binheader_readf (psf, "b", psf->buffer, length) ;
22793 psf_log_printf (psf, " Text : \"%s\"\n", psf->buffer) ;
22795 /* Jump to GLOB offset position. */
22796 if (glob_offset & 1)
22797 glob_offset ++ ;
22799 psf_binheader_readf (psf, "p", glob_offset) ;
22801 slce_count = 0 ;
22802 /* GLOB */
22803 while (1)
22804 { psf_binheader_readf (psf, "m", &marker) ;
22806 if (marker != SLCE_MARKER && slce_count > 0)
22807 { psf_log_printf (psf, " SLCE count : %d\n", slce_count) ;
22808 slce_count = 0 ;
22810 switch (marker)
22811 { case GLOB_MARKER:
22812 psf_binheader_readf (psf, "E4", &length) ;
22813 psf_log_printf (psf, " %M : %d\n", marker, length) ;
22814 psf_binheader_readf (psf, "j", length) ;
22815 break ;
22817 case RECY_MARKER:
22818 psf_binheader_readf (psf, "E4", &length) ;
22819 psf_log_printf (psf, " %M : %d\n", marker, length) ;
22820 psf_binheader_readf (psf, "j", (length+1) & 0xFFFFFFFE) ; /* ?????? */
22821 break ;
22823 case CAT_MARKER:
22824 psf_binheader_readf (psf, "E4", &length) ;
22825 psf_log_printf (psf, " %M : %d\n", marker, length) ;
22826 /*-psf_binheader_readf (psf, "j", length) ;-*/
22827 break ;
22829 case DEVL_MARKER:
22830 psf_binheader_readf (psf, "mE4", &marker, &length) ;
22831 psf_log_printf (psf, " DEVL%M : %d\n", marker, length) ;
22832 if (length & 1)
22833 length ++ ;
22834 psf_binheader_readf (psf, "j", length) ;
22835 break ;
22837 case EQ_MARKER:
22838 case COMP_MARKER:
22839 psf_binheader_readf (psf, "E4", &length) ;
22840 psf_log_printf (psf, " %M : %d\n", marker, length) ;
22841 /* This is weird!!!! why make this (length - 1) */
22842 if (length & 1)
22843 length ++ ;
22844 psf_binheader_readf (psf, "j", length) ;
22845 break ;
22847 case SLCL_MARKER:
22848 psf_log_printf (psf, " %M\n (Offset, Next Offset, Type)\n", marker) ;
22849 slce_count = 0 ;
22850 break ;
22852 case SLCE_MARKER:
22853 { int len [4], indx ;
22855 psf_binheader_readf (psf, "E4444", &len [0], &len [1], &len [2], &len [3]) ;
22857 indx = ((len [3] & 0x0000FFFF) >> 8) & 3 ;
22859 if (len [2] == 1)
22860 { if (indx != 1)
22861 indx = 3 ; /* 2 cases, where next slice offset = 1 -> disabled & enabled/hidden */
22863 psf_log_printf (psf, " %M : (%6d, ?: 0x%X, %s)\n", marker, len [1], (len [3] & 0xFFFF0000) >> 16, marker_type [indx]) ;
22865 else
22866 { slce_total += len [2] ;
22868 psf_log_printf (psf, " %M : (%6d, SLCE_next_ofs:%d, ?: 0x%X, %s)\n", marker, len [1], len [2], (len [3] & 0xFFFF0000) >> 16, marker_type [indx]) ;
22871 slce_count ++ ;
22873 break ;
22875 case SINF_MARKER:
22876 psf_binheader_readf (psf, "E4", &length) ;
22877 psf_log_printf (psf, " %M : %d\n", marker, length) ;
22879 psf_binheader_readf (psf, "E2", &n_channels) ;
22880 n_channels = (n_channels & 0x0000FF00) >> 8 ;
22881 psf_log_printf (psf, " Channels : %d\n", n_channels) ;
22883 psf_binheader_readf (psf, "E44", &psf->sf.samplerate, &frames) ;
22884 psf->sf.frames = frames ;
22885 psf_log_printf (psf, " Sample Rate : %d\n", psf->sf.samplerate) ;
22886 psf_log_printf (psf, " Frames : %D\n", psf->sf.frames) ;
22888 psf_binheader_readf (psf, "E4", &length) ;
22889 psf_log_printf (psf, " ??????????? : %d\n", length) ;
22891 psf_binheader_readf (psf, "E4", &length) ;
22892 psf_log_printf (psf, " ??????????? : %d\n", length) ;
22893 break ;
22895 case SDAT_MARKER:
22896 psf_binheader_readf (psf, "E4", &length) ;
22898 sdat_length = length ;
22900 /* Get the current offset. */
22901 psf->dataoffset = psf_binheader_readf (psf, NULL) ;
22903 if (psf->dataoffset + length != psf->filelength)
22904 psf_log_printf (psf, " %M : %d (should be %d)\n", marker, length, psf->dataoffset + psf->filelength) ;
22905 else
22906 psf_log_printf (psf, " %M : %d\n", marker, length) ;
22907 break ;
22909 default :
22910 psf_log_printf (psf, "Unknown marker : 0x%X %M", marker, marker) ;
22911 return -1003 ;
22912 break ;
22915 /* SDAT always last marker in file. */
22916 if (marker == SDAT_MARKER)
22917 break ;
22920 puts (psf->logbuffer) ;
22921 puts ("-----------------------------------") ;
22923 printf ("SDAT length : %d\n", sdat_length) ;
22924 printf ("SLCE count : %d\n", slce_count) ;
22926 /* Hack for zero slice count. */
22927 if (slce_count == 0 && slce_total == 1)
22928 slce_total = frames ;
22930 printf ("SLCE samples : %d\n", slce_total) ;
22932 /* Two bytes per sample. */
22933 printf ("Comp Ratio : %f:1\n", (2.0 * slce_total * n_channels) / sdat_length) ;
22935 puts (" ") ;
22937 psf->logbuffer [0] = 0 ;
22939 /* OK, have the header although not too sure what it all means. */
22941 psf->endian = SF_ENDIAN_BIG ;
22943 psf->datalength = psf->filelength - psf->dataoffset ;
22945 if (psf_fseek (psf, psf->dataoffset, SEEK_SET))
22946 return SFE_BAD_SEEK ;
22948 psf->sf.format = (SF_FORMAT_REX2 | SF_FORMAT_DWVW_12) ;
22950 psf->sf.channels = 1 ;
22951 psf->bytewidth = 2 ;
22952 psf->blockwidth = psf->sf.channels * psf->bytewidth ;
22954 if ((error = dwvw_init (psf, 16)))
22955 return error ;
22957 psf->close = rx2_close ;
22959 if (! psf->sf.frames && psf->blockwidth)
22960 psf->sf.frames = psf->datalength / psf->blockwidth ;
22962 /* All done. */
22964 return 0 ;
22965 } /* rx2_open */
22967 /*------------------------------------------------------------------------------
22970 static int
22971 rx2_close (SF_PRIVATE *psf)
22973 if (psf->mode == SFM_WRITE)
22974 { /* Now we know for certain the length of the file we can re-write
22975 ** correct values for the FORM, 8SVX and BODY chunks.
22980 return 0 ;
22981 } /* rx2_close */
22983 #endif
22985 ** Do not edit or modify anything in this comment block.
22986 ** The arch-tag line is a file identity tag for the GNU Arch
22987 ** revision control system.
22989 ** arch-tag: 7366e813-9fee-4d1f-881e-e4a691469370
22992 ** Copyright (C) 2001-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
22994 ** This program is free software; you can redistribute it and/or modify
22995 ** it under the terms of the GNU Lesser General Public License as published by
22996 ** the Free Software Foundation; either version 2.1 of the License, or
22997 ** (at your option) any later version.
22999 ** This program is distributed in the hope that it will be useful,
23000 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
23001 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23002 ** GNU Lesser General Public License for more details.
23004 ** You should have received a copy of the GNU Lesser General Public License
23005 ** along with this program; if not, write to the Free Software
23006 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23009 #include <stdio.h>
23010 #include <string.h>
23014 #if (ENABLE_EXPERIMENTAL_CODE == 0)
23017 sd2_open (SF_PRIVATE *psf)
23018 { if (psf)
23019 return SFE_UNIMPLEMENTED ;
23020 return (psf && 0) ;
23021 } /* sd2_open */
23023 #else
23025 /*------------------------------------------------------------------------------
23026 * Macros to handle big/little endian issues.
23029 #define Sd2f_MARKER MAKE_MARKER ('S', 'd', '2', 'f')
23031 /*------------------------------------------------------------------------------
23032 * Typedefs for file chunks.
23037 /*------------------------------------------------------------------------------
23038 * Private static functions.
23041 static int sd2_close (SF_PRIVATE *psf) ;
23043 /*------------------------------------------------------------------------------
23044 ** Public functions.
23048 sd2_open (SF_PRIVATE *psf)
23049 { int marker, software, rsrc_offset, len ;
23050 int rsrc_data_offset, rsrc_map_offset, rsrc_data_length, rsrc_map_length ;
23051 char slen ;
23052 float srate ;
23054 /* Read only so far. */
23056 psf_binheader_readf (psf, "Epmmj", 0x41, &marker, &software, 14) ;
23058 if (marker != Sd2f_MARKER)
23059 { printf ("Whoops!!!\n") ;
23060 puts (psf->logbuffer) ;
23061 return SFE_INTERNAL ;
23064 psf_log_printf (psf, "Marker : %M\n"
23065 "Software : %M\n",
23066 marker, software) ;
23068 /* This seems to be a constant for binhex files. */
23069 psf->dataoffset = 0x80 ;
23071 /* All SD2 files are big endian. */
23072 psf->endian= SF_ENDIAN_BIG ;
23075 ** Resource header info from:
23076 ** http://developer.apple.com/techpubs/mac/MoreToolbox/MoreToolbox-99.html
23079 rsrc_offset = psf->datalength + psf->dataoffset ;
23080 if (rsrc_offset & 0x7F)
23081 rsrc_offset = rsrc_offset - (rsrc_offset & 0x7F) + psf->dataoffset ;
23083 psf_log_printf (psf, "Resource offset : 0x%X\n", rsrc_offset) ;
23085 /* Jump to the rsrc_offset fork section. */
23086 psf_binheader_readf (psf, "Ep", rsrc_offset) ;
23088 psf_binheader_readf (psf, "E4444", &rsrc_data_offset, &rsrc_map_offset, &rsrc_data_length, &rsrc_map_length) ;
23090 rsrc_data_offset += rsrc_offset ;
23091 rsrc_map_offset += rsrc_offset ;
23093 psf_log_printf (psf, " data offset : 0x%X\n"
23094 " map offset : 0x%X\n"
23095 " data length : 0x%X\n"
23096 " map length : 0x%X\n",
23098 rsrc_data_offset, rsrc_map_offset, rsrc_data_length, rsrc_map_length) ;
23100 if (rsrc_data_offset + rsrc_data_length > rsrc_map_offset || rsrc_map_offset + rsrc_map_length > psf->filelength)
23101 { puts ("##############################") ;
23102 puts (psf->logbuffer) ;
23103 puts ("##############################") ;
23104 return SFE_INTERNAL ;
23107 memset (psf->buffer, 0, sizeof (psf->buffer)) ;
23109 psf_binheader_readf (psf, "Ep41", rsrc_data_offset, &len, &slen) ;
23110 if (slen + 1 == len)
23111 { psf_binheader_readf (psf, "Eb", psf->buffer, len - 1) ;
23112 ((char*) psf->buffer) [len - 1] = 0 ;
23113 if (sscanf ((char*) psf->buffer, "%d", &len) == 1)
23114 psf->bytewidth = len ;
23117 psf_binheader_readf (psf, "E41", &len, &slen) ;
23118 if (slen + 1 == len)
23119 { psf_binheader_readf (psf, "Eb", psf->buffer, len - 1) ;
23120 ((char*) psf->buffer) [len - 1] = 0 ;
23121 if (sscanf ((char*) psf->buffer, "%f", &srate) == 1)
23122 psf->sf.samplerate = srate ;
23125 psf_binheader_readf (psf, "E41", &len, &slen) ;
23126 if (slen + 1 == len)
23127 { psf_binheader_readf (psf, "Eb", psf->buffer, len - 1) ;
23128 ((char*) psf->buffer) [len - 1] = 0 ;
23129 if (sscanf ((char*) psf->buffer, "%d", &len) == 1)
23130 psf->sf.channels = len ;
23133 psf_log_printf (psf, " byte width : %d\n", psf->bytewidth) ;
23134 psf_log_printf (psf, " sample rate : %d\n", psf->sf.samplerate) ;
23135 psf_log_printf (psf, " channels : %d\n", psf->sf.channels) ;
23137 if (psf->bytewidth == 2)
23138 { psf->sf.format = SF_FORMAT_SD2 | SF_FORMAT_PCM_16 ;
23140 psf->blockwidth = psf->bytewidth * psf->sf.channels ;
23142 psf->sf.frames = psf->datalength / psf->blockwidth ;
23145 pcm_init (psf) ;
23147 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
23149 psf->close = sd2_close ;
23151 return 0 ;
23152 } /* sd2_open */
23154 /*------------------------------------------------------------------------------
23157 static int
23158 sd2_close (SF_PRIVATE *psf)
23160 if (psf->mode == SFM_WRITE)
23161 { /* Now we know for certain the audio_length of the file we can re-write
23162 ** correct values for the FORM, 8SVX and BODY chunks.
23167 return 0 ;
23168 } /* sd2_close */
23170 #endif
23172 ** Do not edit or modify anything in this comment block.
23173 ** The arch-tag line is a file identity tag for the GNU Arch
23174 ** revision control system.
23176 ** arch-tag: 1ee183e5-6b9f-4c2c-bd0a-24f35595cefc
23179 ** Copyright (C) 2002-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
23181 ** This program is free software; you can redistribute it and/or modify
23182 ** it under the terms of the GNU Lesser General Public License as published by
23183 ** the Free Software Foundation; either version 2.1 of the License, or
23184 ** (at your option) any later version.
23186 ** This program is distributed in the hope that it will be useful,
23187 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
23188 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23189 ** GNU Lesser General Public License for more details.
23191 ** You should have received a copy of the GNU Lesser General Public License
23192 ** along with this program; if not, write to the Free Software
23193 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23197 #include <stdio.h>
23198 #include <stdlib.h>
23199 #include <fcntl.h>
23200 #include <string.h>
23201 #include <ctype.h>
23204 /*------------------------------------------------------------------------------
23207 #define SDS_DATA_OFFSET 0x15
23208 #define SDS_BLOCK_SIZE 127
23210 #define SDS_AUDIO_BYTES_PER_BLOCK 120
23212 #define SDS_3BYTE_TO_INT_DECODE(x) (((x) & 0x7F) | (((x) & 0x7F00) >> 1) | (((x) & 0x7F0000) >> 2))
23213 #define SDS_INT_TO_3BYTE_ENCODE(x) (((x) & 0x7F) | (((x) << 1) & 0x7F00) | (((x) << 2) & 0x7F0000))
23215 /*------------------------------------------------------------------------------
23216 ** Typedefs.
23219 typedef struct tag_SDS_PRIVATE
23220 { int bitwidth, frames ;
23221 int samplesperblock, total_blocks ;
23223 int (*reader) (SF_PRIVATE *psf, struct tag_SDS_PRIVATE *psds) ;
23224 int (*writer) (SF_PRIVATE *psf, struct tag_SDS_PRIVATE *psds) ;
23226 int read_block, read_count ;
23227 unsigned char read_data [SDS_BLOCK_SIZE] ;
23228 int read_samples [SDS_BLOCK_SIZE / 2] ; /* Maximum samples per block */
23230 int write_block, write_count ;
23231 unsigned char write_data [SDS_BLOCK_SIZE] ;
23232 int write_samples [SDS_BLOCK_SIZE / 2] ; /* Maximum samples per block */
23233 } SDS_PRIVATE ;
23235 /*------------------------------------------------------------------------------
23236 ** Private static functions.
23239 static int sds_close (SF_PRIVATE *psf) ;
23241 static int sds_write_header (SF_PRIVATE *psf, int calc_length) ;
23242 static int sds_read_header (SF_PRIVATE *psf, SDS_PRIVATE *psds) ;
23244 static int sds_init (SF_PRIVATE *psf, SDS_PRIVATE *psds) ;
23246 static sf_count_t sds_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
23247 static sf_count_t sds_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
23248 static sf_count_t sds_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
23249 static sf_count_t sds_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
23251 static sf_count_t sds_write_s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
23252 static sf_count_t sds_write_i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
23253 static sf_count_t sds_write_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
23254 static sf_count_t sds_write_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
23256 static sf_count_t sds_seek (SF_PRIVATE *psf, int mode, sf_count_t offset) ;
23258 static int sds_2byte_read (SF_PRIVATE *psf, SDS_PRIVATE *psds) ;
23259 static int sds_3byte_read (SF_PRIVATE *psf, SDS_PRIVATE *psds) ;
23260 static int sds_4byte_read (SF_PRIVATE *psf, SDS_PRIVATE *psds) ;
23262 static int sds_read (SF_PRIVATE *psf, SDS_PRIVATE *psds, int *iptr, int readcount) ;
23264 static int sds_2byte_write (SF_PRIVATE *psf, SDS_PRIVATE *psds) ;
23265 static int sds_3byte_write (SF_PRIVATE *psf, SDS_PRIVATE *psds) ;
23266 static int sds_4byte_write (SF_PRIVATE *psf, SDS_PRIVATE *psds) ;
23268 static int sds_write (SF_PRIVATE *psf, SDS_PRIVATE *psds, int *iptr, int writecount) ;
23270 /*------------------------------------------------------------------------------
23271 ** Public function.
23275 sds_open (SF_PRIVATE *psf)
23276 { SDS_PRIVATE *psds ;
23277 int subformat, error = 0 ;
23279 /* Hmmmm, need this here to pass update_header_test. */
23280 psf->sf.frames = 0 ;
23282 if (! (psds = calloc (1, sizeof (SDS_PRIVATE))))
23283 return SFE_MALLOC_FAILED ;
23284 psf->fdata = psds ;
23286 if (psf->mode == SFM_READ || (psf->mode == SFM_RDWR && psf->filelength > 0))
23287 { if ((error = sds_read_header (psf, psds)))
23288 return error ;
23291 if ((psf->sf.format & SF_FORMAT_TYPEMASK) != SF_FORMAT_SDS)
23292 return SFE_BAD_OPEN_FORMAT ;
23294 subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
23296 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
23297 { if (sds_write_header (psf, SF_FALSE))
23298 return psf->error ;
23300 psf->write_header = sds_write_header ;
23302 psf_fseek (psf, SDS_DATA_OFFSET, SEEK_SET) ;
23305 if ((error = sds_init (psf, psds)) != 0)
23306 return error ;
23308 psf->seek = sds_seek ;
23309 psf->close = sds_close ;
23311 psf->blockwidth = 0 ;
23313 return error ;
23314 } /* sds_open */
23316 /*------------------------------------------------------------------------------
23319 static int
23320 sds_close (SF_PRIVATE *psf)
23322 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
23323 { SDS_PRIVATE *psds ;
23325 if ((psds = (SDS_PRIVATE *) psf->fdata) == NULL)
23326 { psf_log_printf (psf, "*** Bad psf->fdata ptr.\n") ;
23327 return SFE_INTERNAL ;
23330 if (psds->write_count > 0)
23331 { memset (&(psds->write_data [psds->write_count]), 0, (psds->samplesperblock - psds->write_count) * sizeof (int)) ;
23332 psds->writer (psf, psds) ;
23335 sds_write_header (psf, SF_TRUE) ;
23338 return 0 ;
23339 } /* sds_close */
23341 static int
23342 sds_init (SF_PRIVATE *psf, SDS_PRIVATE *psds)
23344 if (psds->bitwidth < 8 || psds->bitwidth > 28)
23345 return (psf->error = SFE_SDS_BAD_BIT_WIDTH) ;
23347 if (psds->bitwidth < 14)
23348 { psds->reader = sds_2byte_read ;
23349 psds->writer = sds_2byte_write ;
23350 psds->samplesperblock = SDS_AUDIO_BYTES_PER_BLOCK / 2 ;
23352 else if (psds->bitwidth < 21)
23353 { psds->reader = sds_3byte_read ;
23354 psds->writer = sds_3byte_write ;
23355 psds->samplesperblock = SDS_AUDIO_BYTES_PER_BLOCK / 3 ;
23357 else
23358 { psds->reader = sds_4byte_read ;
23359 psds->writer = sds_4byte_write ;
23360 psds->samplesperblock = SDS_AUDIO_BYTES_PER_BLOCK / 4 ;
23363 if (psf->mode == SFM_READ || psf->mode == SFM_RDWR)
23364 { psf->read_short = sds_read_s ;
23365 psf->read_int = sds_read_i ;
23366 psf->read_float = sds_read_f ;
23367 psf->read_double = sds_read_d ;
23369 /* Read first block. */
23370 psds->reader (psf, psds) ;
23373 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
23374 { psf->write_short = sds_write_s ;
23375 psf->write_int = sds_write_i ;
23376 psf->write_float = sds_write_f ;
23377 psf->write_double = sds_write_d ;
23380 return 0 ;
23381 } /* sds_init */
23383 static int
23384 sds_read_header (SF_PRIVATE *psf, SDS_PRIVATE *psds)
23385 { unsigned char channel, bitwidth, loop_type, byte ;
23386 unsigned short sample_no, marker ;
23387 unsigned int samp_period, data_length, sustain_loop_start, sustain_loop_end ;
23388 int bytesread, blockcount ;
23390 /* Set position to start of file to begin reading header. */
23391 bytesread = psf_binheader_readf (psf, "pE211", 0, &marker, &channel, &byte) ;
23393 if (marker != 0xF07E || byte != 0x01)
23394 return SFE_SDS_NOT_SDS ;
23396 psf_log_printf (psf, "Midi Sample Dump Standard (.sds)\nF07E\n Midi Channel : %d\n", channel) ;
23398 bytesread += psf_binheader_readf (psf, "e213", &sample_no, &bitwidth, &samp_period) ;
23400 sample_no = SDS_3BYTE_TO_INT_DECODE (sample_no) ;
23401 samp_period = SDS_3BYTE_TO_INT_DECODE (samp_period) ;
23403 psds->bitwidth = bitwidth ;
23405 psf->sf.samplerate = 1000000000 / samp_period ;
23407 psf_log_printf (psf, " Sample Number : %d\n"
23408 " Bit Width : %d\n"
23409 " Sample Rate : %d\n",
23410 sample_no, psds->bitwidth, psf->sf.samplerate) ;
23412 bytesread += psf_binheader_readf (psf, "e3331", &data_length, &sustain_loop_start, &sustain_loop_end, &loop_type) ;
23414 data_length = SDS_3BYTE_TO_INT_DECODE (data_length) ;
23416 sustain_loop_start = SDS_3BYTE_TO_INT_DECODE (sustain_loop_start) ;
23417 sustain_loop_end = SDS_3BYTE_TO_INT_DECODE (sustain_loop_end) ;
23419 psf_log_printf (psf, " Sustain Loop\n"
23420 " Start : %d\n"
23421 " End : %d\n"
23422 " Loop Type : %d\n",
23423 sustain_loop_start, sustain_loop_end, loop_type) ;
23425 psf->dataoffset = SDS_DATA_OFFSET ;
23426 psf->datalength = psf->filelength - psf->dataoffset ;
23428 if (data_length != psf->filelength - psf->dataoffset)
23429 { psf_log_printf (psf, " Datalength : %d (truncated data??? %d)\n", data_length, psf->filelength - psf->dataoffset) ;
23430 data_length = psf->filelength - psf->dataoffset ;
23432 else
23433 psf_log_printf (psf, " Datalength : %d\n", data_length) ;
23435 bytesread += psf_binheader_readf (psf, "1", &byte) ;
23436 if (byte != 0xF7)
23437 psf_log_printf (psf, "bad end : %X\n", byte & 0xFF) ;
23439 for (blockcount = 0 ; bytesread < psf->filelength ; blockcount++)
23441 bytesread += psf_fread (&marker, 1, 2, psf) ;
23443 if (marker == 0)
23444 break ;
23446 psf_fseek (psf, SDS_BLOCK_SIZE - 2, SEEK_CUR) ;
23447 bytesread += SDS_BLOCK_SIZE - 2 ;
23450 psf_log_printf (psf, "\nBlocks : %d\n", blockcount) ;
23451 psds->total_blocks = blockcount ;
23453 psds->samplesperblock = SDS_AUDIO_BYTES_PER_BLOCK / ((psds->bitwidth + 6) / 7) ;
23454 psf_log_printf (psf, "Samples/Block : %d\n", psds->samplesperblock) ;
23456 psf_log_printf (psf, "Frames : %d\n", blockcount * psds->samplesperblock) ;
23458 psf->sf.frames = blockcount * psds->samplesperblock ;
23459 psds->frames = blockcount * psds->samplesperblock ;
23461 /* Always Mono */
23462 psf->sf.channels = 1 ;
23463 psf->sf.sections = 1 ;
23466 ** Lie to the user about PCM bit width. Always round up to
23467 ** the next multiple of 8.
23469 switch ((psds->bitwidth + 7) / 8)
23470 { case 1 :
23471 psf->sf.format = SF_FORMAT_SDS | SF_FORMAT_PCM_S8 ;
23472 break ;
23474 case 2 :
23475 psf->sf.format = SF_FORMAT_SDS | SF_FORMAT_PCM_16 ;
23476 break ;
23478 case 3 :
23479 psf->sf.format = SF_FORMAT_SDS | SF_FORMAT_PCM_24 ;
23480 break ;
23482 case 4 :
23483 psf->sf.format = SF_FORMAT_SDS | SF_FORMAT_PCM_32 ;
23484 break ;
23486 default :
23487 psf_log_printf (psf, "*** Weird byte width (%d)\n", (psds->bitwidth + 7) / 8) ;
23488 return SFE_SDS_BAD_BIT_WIDTH ;
23491 psf_fseek (psf, SDS_DATA_OFFSET, SEEK_SET) ;
23493 return 0 ;
23494 } /* sds_read_header */
23496 static int
23497 sds_write_header (SF_PRIVATE *psf, int calc_length)
23498 { SDS_PRIVATE *psds ;
23499 sf_count_t current ;
23500 int samp_period, data_length, sustain_loop_start, sustain_loop_end ;
23501 unsigned char loop_type = 0 ;
23503 if ((psds = (SDS_PRIVATE *) psf->fdata) == NULL)
23504 { psf_log_printf (psf, "*** Bad psf->fdata ptr.\n") ;
23505 return SFE_INTERNAL ;
23508 if (psf->pipeoffset > 0)
23509 return 0 ;
23511 current = psf_ftell (psf) ;
23513 if (calc_length)
23514 psf->sf.frames = psds->total_blocks * psds->samplesperblock + psds->write_count ;
23516 if (psds->write_count > 0)
23517 { int current_count = psds->write_count ;
23518 int current_block = psds->write_block ;
23520 psds->writer (psf, psds) ;
23522 psf_fseek (psf, -1 * SDS_BLOCK_SIZE, SEEK_CUR) ;
23524 psds->write_count = current_count ;
23525 psds->write_block = current_block ;
23528 /* Reset the current header length to zero. */
23529 psf->header [0] = 0 ;
23530 psf->headindex = 0 ;
23532 if (psf->is_pipe == SF_FALSE)
23533 psf_fseek (psf, 0, SEEK_SET) ;
23535 psf_binheader_writef (psf, "E211", 0xF07E, 0, 1) ;
23537 switch (psf->sf.format & SF_FORMAT_SUBMASK)
23538 { case SF_FORMAT_PCM_S8 :
23539 psds->bitwidth = 8 ;
23540 break ;
23541 case SF_FORMAT_PCM_16 :
23542 psds->bitwidth = 16 ;
23543 break ;
23544 case SF_FORMAT_PCM_24 :
23545 psds->bitwidth = 24 ;
23546 break ;
23547 default:
23548 return SFE_SDS_BAD_BIT_WIDTH ;
23551 samp_period = SDS_INT_TO_3BYTE_ENCODE (1000000000 / psf->sf.samplerate) ;
23553 psf_binheader_writef (psf, "e213", 0, psds->bitwidth, samp_period) ;
23555 data_length = SDS_INT_TO_3BYTE_ENCODE (psds->total_blocks * SDS_BLOCK_SIZE) ;
23556 sustain_loop_start = SDS_INT_TO_3BYTE_ENCODE (0) ;
23557 sustain_loop_end = SDS_INT_TO_3BYTE_ENCODE (psf->sf.frames) ;
23559 psf_binheader_writef (psf, "e33311", data_length, sustain_loop_start, sustain_loop_end, loop_type, 0xF7) ;
23561 /* Header construction complete so write it out. */
23562 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
23564 if (psf->error)
23565 return psf->error ;
23567 psf->dataoffset = psf->headindex ;
23568 psf->datalength = psds->write_block * SDS_BLOCK_SIZE ;
23570 if (current > 0)
23571 psf_fseek (psf, current, SEEK_SET) ;
23573 return psf->error ;
23574 } /* sds_write_header */
23577 /*------------------------------------------------------------------------------
23580 static int
23581 sds_2byte_read (SF_PRIVATE *psf, SDS_PRIVATE *psds)
23582 { unsigned char *ucptr, checksum ;
23583 unsigned int sample ;
23584 int k ;
23586 psds->read_block ++ ;
23587 psds->read_count = 0 ;
23589 if (psds->read_block * psds->samplesperblock > psds->frames)
23590 { memset (psds->read_samples, 0, psds->samplesperblock * sizeof (int)) ;
23591 return 1 ;
23594 if ((k = psf_fread (psds->read_data, 1, SDS_BLOCK_SIZE, psf)) != SDS_BLOCK_SIZE)
23595 psf_log_printf (psf, "*** Warning : short read (%d != %d).\n", k, SDS_BLOCK_SIZE) ;
23597 if (psds->read_data [0] != 0xF0)
23598 { printf ("Error A : %02X\n", psds->read_data [0] & 0xFF) ;
23601 checksum = psds->read_data [1] ;
23602 if (checksum != 0x7E)
23603 { printf ("Error 1 : %02X\n", checksum & 0xFF) ;
23606 for (k = 2 ; k < SDS_BLOCK_SIZE - 3 ; k ++)
23607 checksum ^= psds->read_data [k] ;
23609 checksum &= 0x7F ;
23611 if (checksum != psds->read_data [SDS_BLOCK_SIZE - 2])
23612 { psf_log_printf (psf, "Block %d : checksum is %02X should be %02X\n", psds->read_data [4], checksum, psds->read_data [SDS_BLOCK_SIZE - 2]) ;
23615 ucptr = psds->read_data + 5 ;
23616 for (k = 0 ; k < 120 ; k += 2)
23617 { sample = (ucptr [k] << 25) + (ucptr [k + 1] << 18) ;
23618 psds->read_samples [k / 2] = (int) (sample - 0x80000000) ;
23621 return 1 ;
23622 } /* sds_2byte_read */
23624 static int
23625 sds_3byte_read (SF_PRIVATE *psf, SDS_PRIVATE *psds)
23626 { unsigned char *ucptr, checksum ;
23627 unsigned int sample ;
23628 int k ;
23630 psds->read_block ++ ;
23631 psds->read_count = 0 ;
23633 if (psds->read_block * psds->samplesperblock > psds->frames)
23634 { memset (psds->read_samples, 0, psds->samplesperblock * sizeof (int)) ;
23635 return 1 ;
23638 if ((k = psf_fread (psds->read_data, 1, SDS_BLOCK_SIZE, psf)) != SDS_BLOCK_SIZE)
23639 psf_log_printf (psf, "*** Warning : short read (%d != %d).\n", k, SDS_BLOCK_SIZE) ;
23641 if (psds->read_data [0] != 0xF0)
23642 { printf ("Error A : %02X\n", psds->read_data [0] & 0xFF) ;
23645 checksum = psds->read_data [1] ;
23646 if (checksum != 0x7E)
23647 { printf ("Error 1 : %02X\n", checksum & 0xFF) ;
23650 for (k = 2 ; k < SDS_BLOCK_SIZE - 3 ; k ++)
23651 checksum ^= psds->read_data [k] ;
23653 checksum &= 0x7F ;
23655 if (checksum != psds->read_data [SDS_BLOCK_SIZE - 2])
23656 { psf_log_printf (psf, "Block %d : checksum is %02X should be %02X\n", psds->read_data [4], checksum, psds->read_data [SDS_BLOCK_SIZE - 2]) ;
23659 ucptr = psds->read_data + 5 ;
23660 for (k = 0 ; k < 120 ; k += 3)
23661 { sample = (ucptr [k] << 25) + (ucptr [k + 1] << 18) + (ucptr [k + 2] << 11) ;
23662 psds->read_samples [k / 3] = (int) (sample - 0x80000000) ;
23665 return 1 ;
23666 } /* sds_3byte_read */
23668 static int
23669 sds_4byte_read (SF_PRIVATE *psf, SDS_PRIVATE *psds)
23670 { unsigned char *ucptr, checksum ;
23671 unsigned int sample ;
23672 int k ;
23674 psds->read_block ++ ;
23675 psds->read_count = 0 ;
23677 if (psds->read_block * psds->samplesperblock > psds->frames)
23678 { memset (psds->read_samples, 0, psds->samplesperblock * sizeof (int)) ;
23679 return 1 ;
23682 if ((k = psf_fread (psds->read_data, 1, SDS_BLOCK_SIZE, psf)) != SDS_BLOCK_SIZE)
23683 psf_log_printf (psf, "*** Warning : short read (%d != %d).\n", k, SDS_BLOCK_SIZE) ;
23685 if (psds->read_data [0] != 0xF0)
23686 { printf ("Error A : %02X\n", psds->read_data [0] & 0xFF) ;
23689 checksum = psds->read_data [1] ;
23690 if (checksum != 0x7E)
23691 { printf ("Error 1 : %02X\n", checksum & 0xFF) ;
23694 for (k = 2 ; k < SDS_BLOCK_SIZE - 3 ; k ++)
23695 checksum ^= psds->read_data [k] ;
23697 checksum &= 0x7F ;
23699 if (checksum != psds->read_data [SDS_BLOCK_SIZE - 2])
23700 { psf_log_printf (psf, "Block %d : checksum is %02X should be %02X\n", psds->read_data [4], checksum, psds->read_data [SDS_BLOCK_SIZE - 2]) ;
23703 ucptr = psds->read_data + 5 ;
23704 for (k = 0 ; k < 120 ; k += 4)
23705 { sample = (ucptr [k] << 25) + (ucptr [k + 1] << 18) + (ucptr [k + 2] << 11) + (ucptr [k + 3] << 4) ;
23706 psds->read_samples [k / 4] = (int) (sample - 0x80000000) ;
23709 return 1 ;
23710 } /* sds_4byte_read */
23713 static sf_count_t
23714 sds_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
23715 { SDS_PRIVATE *psds ;
23716 int *iptr ;
23717 int k, bufferlen, readcount, count ;
23718 sf_count_t total = 0 ;
23720 if (psf->fdata == NULL)
23721 return 0 ;
23722 psds = (SDS_PRIVATE*) psf->fdata ;
23724 iptr = (int*) psf->buffer ;
23725 bufferlen = sizeof (psf->buffer) / sizeof (int) ;
23726 while (len > 0)
23727 { readcount = (len >= bufferlen) ? bufferlen : len ;
23728 count = sds_read (psf, psds, iptr, readcount) ;
23729 for (k = 0 ; k < readcount ; k++)
23730 ptr [total + k] = iptr [k] >> 16 ;
23731 total += count ;
23732 len -= readcount ;
23735 return total ;
23736 } /* sds_read_s */
23738 static sf_count_t
23739 sds_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
23740 { SDS_PRIVATE *psds ;
23741 int total ;
23743 if (psf->fdata == NULL)
23744 return 0 ;
23745 psds = (SDS_PRIVATE*) psf->fdata ;
23747 total = sds_read (psf, psds, ptr, len) ;
23749 return total ;
23750 } /* sds_read_i */
23752 static sf_count_t
23753 sds_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
23754 { SDS_PRIVATE *psds ;
23755 int *iptr ;
23756 int k, bufferlen, readcount, count ;
23757 sf_count_t total = 0 ;
23758 float normfact ;
23760 if (psf->fdata == NULL)
23761 return 0 ;
23762 psds = (SDS_PRIVATE*) psf->fdata ;
23764 if (psf->norm_float == SF_TRUE)
23765 normfact = 1.0 / 0x80000000 ;
23766 else
23767 normfact = 1.0 / (1 << psds->bitwidth) ;
23769 iptr = (int*) psf->buffer ;
23770 bufferlen = sizeof (psf->buffer) / sizeof (int) ;
23771 while (len > 0)
23772 { readcount = (len >= bufferlen) ? bufferlen : len ;
23773 count = sds_read (psf, psds, iptr, readcount) ;
23774 for (k = 0 ; k < readcount ; k++)
23775 ptr [total + k] = normfact * iptr [k] ;
23776 total += count ;
23777 len -= readcount ;
23780 return total ;
23781 } /* sds_read_f */
23783 static sf_count_t
23784 sds_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
23785 { SDS_PRIVATE *psds ;
23786 int *iptr ;
23787 int k, bufferlen, readcount, count ;
23788 sf_count_t total = 0 ;
23789 double normfact ;
23791 if (psf->fdata == NULL)
23792 return 0 ;
23793 psds = (SDS_PRIVATE*) psf->fdata ;
23795 if (psf->norm_double == SF_TRUE)
23796 normfact = 1.0 / 0x80000000 ;
23797 else
23798 normfact = 1.0 / (1 << psds->bitwidth) ;
23800 iptr = (int*) psf->buffer ;
23801 bufferlen = sizeof (psf->buffer) / sizeof (int) ;
23802 while (len > 0)
23803 { readcount = (len >= bufferlen) ? bufferlen : len ;
23804 count = sds_read (psf, psds, iptr, readcount) ;
23805 for (k = 0 ; k < readcount ; k++)
23806 ptr [total + k] = normfact * iptr [k] ;
23807 total += count ;
23808 len -= readcount ;
23811 return total ;
23812 } /* sds_read_d */
23814 static int
23815 sds_read (SF_PRIVATE *psf, SDS_PRIVATE *psds, int *ptr, int len)
23816 { int count, total = 0 ;
23818 while (total < len)
23819 { if (psds->read_block * psds->samplesperblock >= psds->frames)
23820 { memset (&(ptr [total]), 0, (len - total) * sizeof (int)) ;
23821 return total ;
23824 if (psds->read_count >= psds->samplesperblock)
23825 psds->reader (psf, psds) ;
23827 count = (psds->samplesperblock - psds->read_count) ;
23828 count = (len - total > count) ? count : len - total ;
23830 memcpy (&(ptr [total]), &(psds->read_samples [psds->read_count]), count * sizeof (int)) ;
23831 total += count ;
23832 psds->read_count += count ;
23835 return total ;
23836 } /* sds_read */
23838 /*==============================================================================
23841 static sf_count_t
23842 sds_seek (SF_PRIVATE *psf, int mode, sf_count_t seek_from_start)
23843 { SDS_PRIVATE *psds ;
23844 sf_count_t file_offset ;
23845 int newblock, newsample ;
23847 if ((psds = psf->fdata) == NULL)
23848 { psf->error = SFE_INTERNAL ;
23849 return SF_SEEK_ERROR ;
23852 if (psf->datalength < 0 || psf->dataoffset < 0)
23853 { psf->error = SFE_BAD_SEEK ;
23854 return SF_SEEK_ERROR ;
23857 if (seek_from_start < 0 || seek_from_start > psf->sf.frames)
23858 { psf->error = SFE_BAD_SEEK ;
23859 return SF_SEEK_ERROR ;
23862 if (mode == SFM_READ && psds->write_count > 0)
23863 psds->writer (psf, psds) ;
23865 newblock = seek_from_start / psds->samplesperblock ;
23866 newsample = seek_from_start % psds->samplesperblock ;
23868 switch (mode)
23869 { case SFM_READ :
23870 if (newblock > psds->total_blocks)
23871 { psf->error = SFE_BAD_SEEK ;
23872 return SF_SEEK_ERROR ;
23875 file_offset = psf->dataoffset + newblock * SDS_BLOCK_SIZE ;
23877 if (psf_fseek (psf, file_offset, SEEK_SET) != file_offset)
23878 { psf->error = SFE_SEEK_FAILED ;
23879 return SF_SEEK_ERROR ;
23882 psds->read_block = newblock ;
23883 psds->reader (psf, psds) ;
23884 psds->read_count = newsample ;
23885 break ;
23887 case SFM_WRITE :
23888 if (newblock > psds->total_blocks)
23889 { psf->error = SFE_BAD_SEEK ;
23890 return SF_SEEK_ERROR ;
23893 file_offset = psf->dataoffset + newblock * SDS_BLOCK_SIZE ;
23895 if (psf_fseek (psf, file_offset, SEEK_SET) != file_offset)
23896 { psf->error = SFE_SEEK_FAILED ;
23897 return SF_SEEK_ERROR ;
23900 psds->write_block = newblock ;
23901 psds->reader (psf, psds) ;
23902 psds->write_count = newsample ;
23903 break ;
23905 default :
23906 psf->error = SFE_BAD_SEEK ;
23907 return SF_SEEK_ERROR ;
23908 break ;
23911 return seek_from_start ;
23912 } /* sds_seek */
23914 /*==============================================================================
23917 static int
23918 sds_2byte_write (SF_PRIVATE *psf, SDS_PRIVATE *psds)
23919 { unsigned char *ucptr, checksum ;
23920 unsigned int sample ;
23921 int k ;
23923 psds->write_data [0] = 0xF0 ;
23924 psds->write_data [1] = 0x7E ;
23925 psds->write_data [2] = 0 ; /* Channel number */
23926 psds->write_data [3] = psds->write_block & 0x7F ; /* Packet number */
23928 ucptr = psds->write_data + 5 ;
23929 for (k = 0 ; k < 120 ; k += 2)
23930 { sample = psds->write_samples [k / 2] ;
23931 sample += 0x80000000 ;
23932 ucptr [k] = (sample >> 25) & 0x7F ;
23933 ucptr [k + 1] = (sample >> 18) & 0x7F ;
23936 checksum = psds->write_data [1] ;
23937 for (k = 2 ; k < SDS_BLOCK_SIZE - 3 ; k ++)
23938 checksum ^= psds->write_data [k] ;
23939 checksum &= 0x7F ;
23941 psds->write_data [SDS_BLOCK_SIZE - 2] = checksum ;
23942 psds->write_data [SDS_BLOCK_SIZE - 1] = 0xF7 ;
23944 if ((k = psf_fwrite (psds->write_data, 1, SDS_BLOCK_SIZE, psf)) != SDS_BLOCK_SIZE)
23945 psf_log_printf (psf, "*** Warning : psf_fwrite (%d != %d).\n", k, SDS_BLOCK_SIZE) ;
23947 psds->write_block ++ ;
23948 psds->write_count = 0 ;
23950 if (psds->write_block > psds->total_blocks)
23951 psds->total_blocks = psds->write_block ;
23952 psds->frames = psds->total_blocks * psds->samplesperblock ;
23954 return 1 ;
23955 } /* sds_2byte_write */
23957 static int
23958 sds_3byte_write (SF_PRIVATE *psf, SDS_PRIVATE *psds)
23959 { unsigned char *ucptr, checksum ;
23960 unsigned int sample ;
23961 int k ;
23963 psds->write_data [0] = 0xF0 ;
23964 psds->write_data [1] = 0x7E ;
23965 psds->write_data [2] = 0 ; /* Channel number */
23966 psds->write_data [3] = psds->write_block & 0x7F ; /* Packet number */
23968 ucptr = psds->write_data + 5 ;
23969 for (k = 0 ; k < 120 ; k += 3)
23970 { sample = psds->write_samples [k / 3] ;
23971 sample += 0x80000000 ;
23972 ucptr [k] = (sample >> 25) & 0x7F ;
23973 ucptr [k + 1] = (sample >> 18) & 0x7F ;
23974 ucptr [k + 2] = (sample >> 11) & 0x7F ;
23977 checksum = psds->write_data [1] ;
23978 for (k = 2 ; k < SDS_BLOCK_SIZE - 3 ; k ++)
23979 checksum ^= psds->write_data [k] ;
23980 checksum &= 0x7F ;
23982 psds->write_data [SDS_BLOCK_SIZE - 2] = checksum ;
23983 psds->write_data [SDS_BLOCK_SIZE - 1] = 0xF7 ;
23985 if ((k = psf_fwrite (psds->write_data, 1, SDS_BLOCK_SIZE, psf)) != SDS_BLOCK_SIZE)
23986 psf_log_printf (psf, "*** Warning : psf_fwrite (%d != %d).\n", k, SDS_BLOCK_SIZE) ;
23988 psds->write_block ++ ;
23989 psds->write_count = 0 ;
23991 if (psds->write_block > psds->total_blocks)
23992 psds->total_blocks = psds->write_block ;
23993 psds->frames = psds->total_blocks * psds->samplesperblock ;
23995 return 1 ;
23996 } /* sds_3byte_write */
23998 static int
23999 sds_4byte_write (SF_PRIVATE *psf, SDS_PRIVATE *psds)
24000 { unsigned char *ucptr, checksum ;
24001 unsigned int sample ;
24002 int k ;
24004 psds->write_data [0] = 0xF0 ;
24005 psds->write_data [1] = 0x7E ;
24006 psds->write_data [2] = 0 ; /* Channel number */
24007 psds->write_data [3] = psds->write_block & 0x7F ; /* Packet number */
24009 ucptr = psds->write_data + 5 ;
24010 for (k = 0 ; k < 120 ; k += 4)
24011 { sample = psds->write_samples [k / 4] ;
24012 sample += 0x80000000 ;
24013 ucptr [k] = (sample >> 25) & 0x7F ;
24014 ucptr [k + 1] = (sample >> 18) & 0x7F ;
24015 ucptr [k + 2] = (sample >> 11) & 0x7F ;
24016 ucptr [k + 3] = (sample >> 4) & 0x7F ;
24019 checksum = psds->write_data [1] ;
24020 for (k = 2 ; k < SDS_BLOCK_SIZE - 3 ; k ++)
24021 checksum ^= psds->write_data [k] ;
24022 checksum &= 0x7F ;
24024 psds->write_data [SDS_BLOCK_SIZE - 2] = checksum ;
24025 psds->write_data [SDS_BLOCK_SIZE - 1] = 0xF7 ;
24027 if ((k = psf_fwrite (psds->write_data, 1, SDS_BLOCK_SIZE, psf)) != SDS_BLOCK_SIZE)
24028 psf_log_printf (psf, "*** Warning : psf_fwrite (%d != %d).\n", k, SDS_BLOCK_SIZE) ;
24030 psds->write_block ++ ;
24031 psds->write_count = 0 ;
24033 if (psds->write_block > psds->total_blocks)
24034 psds->total_blocks = psds->write_block ;
24035 psds->frames = psds->total_blocks * psds->samplesperblock ;
24037 return 1 ;
24038 } /* sds_4byte_write */
24040 static sf_count_t
24041 sds_write_s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
24042 { SDS_PRIVATE *psds ;
24043 int *iptr ;
24044 int k, bufferlen, writecount, count ;
24045 sf_count_t total = 0 ;
24047 if (psf->fdata == NULL)
24048 return 0 ;
24049 psds = (SDS_PRIVATE*) psf->fdata ;
24051 iptr = (int*) psf->buffer ;
24052 bufferlen = sizeof (psf->buffer) / sizeof (int) ;
24053 while (len > 0)
24054 { writecount = (len >= bufferlen) ? bufferlen : len ;
24055 for (k = 0 ; k < writecount ; k++)
24056 iptr [k] = ptr [total + k] << 16 ;
24057 count = sds_write (psf, psds, iptr, writecount) ;
24058 total += count ;
24059 len -= writecount ;
24062 return total ;
24063 } /* sds_write_s */
24065 static sf_count_t
24066 sds_write_i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
24067 { SDS_PRIVATE *psds ;
24068 int total ;
24070 if (psf->fdata == NULL)
24071 return 0 ;
24072 psds = (SDS_PRIVATE*) psf->fdata ;
24074 total = sds_write (psf, psds, ptr, len) ;
24076 return total ;
24077 } /* sds_write_i */
24079 static sf_count_t
24080 sds_write_f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
24081 { SDS_PRIVATE *psds ;
24082 int *iptr ;
24083 int k, bufferlen, writecount, count ;
24084 sf_count_t total = 0 ;
24085 float normfact ;
24087 if (psf->fdata == NULL)
24088 return 0 ;
24089 psds = (SDS_PRIVATE*) psf->fdata ;
24091 if (psf->norm_float == SF_TRUE)
24092 normfact = 1.0 * 0x80000000 ;
24093 else
24094 normfact = 1.0 * (1 << psds->bitwidth) ;
24096 iptr = (int*) psf->buffer ;
24097 bufferlen = sizeof (psf->buffer) / sizeof (int) ;
24098 while (len > 0)
24099 { writecount = (len >= bufferlen) ? bufferlen : len ;
24100 for (k = 0 ; k < writecount ; k++)
24101 iptr [k] = normfact * ptr [total + k] ;
24102 count = sds_write (psf, psds, iptr, writecount) ;
24103 total += count ;
24104 len -= writecount ;
24107 return total ;
24108 } /* sds_write_f */
24110 static sf_count_t
24111 sds_write_d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
24112 { SDS_PRIVATE *psds ;
24113 int *iptr ;
24114 int k, bufferlen, writecount, count ;
24115 sf_count_t total = 0 ;
24116 double normfact ;
24118 if (psf->fdata == NULL)
24119 return 0 ;
24120 psds = (SDS_PRIVATE*) psf->fdata ;
24122 if (psf->norm_double == SF_TRUE)
24123 normfact = 1.0 * 0x80000000 ;
24124 else
24125 normfact = 1.0 * (1 << psds->bitwidth) ;
24127 iptr = (int*) psf->buffer ;
24128 bufferlen = sizeof (psf->buffer) / sizeof (int) ;
24129 while (len > 0)
24130 { writecount = (len >= bufferlen) ? bufferlen : len ;
24131 for (k = 0 ; k < writecount ; k++)
24132 iptr [k] = normfact * ptr [total + k] ;
24133 count = sds_write (psf, psds, iptr, writecount) ;
24134 total += count ;
24135 len -= writecount ;
24138 return total ;
24139 } /* sds_write_d */
24141 static int
24142 sds_write (SF_PRIVATE *psf, SDS_PRIVATE *psds, int *ptr, int len)
24143 { int count, total = 0 ;
24145 while (total < len)
24146 { count = psds->samplesperblock - psds->write_count ;
24147 if (count > len - total)
24148 count = len - total ;
24150 memcpy (&(psds->write_samples [psds->write_count]), &(ptr [total]), count * sizeof (int)) ;
24151 total += count ;
24152 psds->write_count += count ;
24154 if (psds->write_count >= psds->samplesperblock)
24155 psds->writer (psf, psds) ;
24158 return total ;
24159 } /* sds_write */
24162 ** Do not edit or modify anything in this comment block.
24163 ** The arch-tag line is a file identity tag for the GNU Arch
24164 ** revision control system.
24166 ** arch-tag: d5d26aa3-368c-4ca6-bb85-377e5a2578cc
24169 ** Copyright (C) 1999-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
24171 ** This program is free software; you can redistribute it and/or modify
24172 ** it under the terms of the GNU Lesser General Public License as published by
24173 ** the Free Software Foundation; either version 2.1 of the License, or
24174 ** (at your option) any later version.
24176 ** This program is distributed in the hope that it will be useful,
24177 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
24178 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24179 ** GNU Lesser General Public License for more details.
24181 ** You should have received a copy of the GNU Lesser General Public License
24182 ** along with this program; if not, write to the Free Software
24183 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24187 /*-----------------------------------------------------------------------------------------------
24188 ** Generic functions for performing endian swapping on integer arrays.
24191 void
24192 endswap_short_array (short *ptr, int len)
24195 #if 0
24196 unsigned char *ucptr, temp ;
24198 ucptr = ((unsigned char *) ptr) + 2 * len ;
24199 while (len > 0)
24200 { len -- ;
24201 ucptr -= 2 ;
24202 temp = ucptr [0] ;
24203 ucptr [0] = ucptr [1] ;
24204 ucptr [1] = temp ;
24206 #else
24207 short temp ;
24208 while (len > 0)
24209 { len -- ;
24210 temp = ptr [len] ;
24211 ptr [len] = ENDSWAP_SHORT (temp) ;
24213 #endif
24214 } /* endswap_short_array */
24216 void
24217 endswap_int_array (int *ptr, int len)
24219 #if 0
24220 unsigned char *ucptr, temp ;
24222 ucptr = ((unsigned char *) ptr) + 4 * len ;
24223 while (len > 0)
24224 { len -- ;
24225 ucptr -= 4 ;
24227 temp = ucptr [0] ;
24228 ucptr [0] = ucptr [3] ;
24229 ucptr [3] = temp ;
24231 temp = ucptr [1] ;
24232 ucptr [1] = ucptr [2] ;
24233 ucptr [2] = temp ;
24235 #else
24236 int temp ;
24238 while (len > 0)
24239 { len -- ;
24240 temp = ptr [len] ;
24241 ptr [len] = ENDSWAP_INT (temp) ;
24243 #endif
24244 } /* endswap_int_array */
24246 /* This function assumes that sizeof (long) == 8, but works correctly even
24247 ** is sizeof (long) == 4.
24249 void
24250 endswap_long_array (long *ptr, int len)
24251 { unsigned char *ucptr, temp ;
24253 ucptr = (unsigned char *) ptr + 8 * len ;
24254 while (len > 0)
24255 { len -- ;
24256 ucptr -= 8 ;
24258 temp = ucptr [0] ;
24259 ucptr [0] = ucptr [7] ;
24260 ucptr [7] = temp ;
24262 temp = ucptr [1] ;
24263 ucptr [1] = ucptr [6] ;
24264 ucptr [6] = temp ;
24266 temp = ucptr [2] ;
24267 ucptr [2] = ucptr [5] ;
24268 ucptr [5] = temp ;
24270 temp = ucptr [3] ;
24271 ucptr [3] = ucptr [4] ;
24272 ucptr [4] = temp ;
24274 } /* endswap_long_array */
24276 /*========================================================================================
24279 void
24280 endswap_short_copy (short *dest, short *src, int len)
24282 #if 0
24283 char *psrc, *pdest ;
24285 psrc = ((char *) src) + 2 * len ;
24286 pdest = ((char *) dest) + 2 * len ;
24287 while (len > 0)
24288 { len -- ;
24289 psrc -= 2 ;
24290 pdest -= 2 ;
24292 pdest [0] = psrc [1] ;
24293 pdest [1] = psrc [0] ;
24295 #else
24296 while (len > 0)
24297 { len -- ;
24298 dest [len] = ENDSWAP_SHORT (src [len]) ;
24300 #endif
24301 } /* endswap_short_copy */
24303 void
24304 endswap_int_copy (int *dest, int *src, int len)
24306 #if 0
24307 char *psrc, *pdest ;
24309 psrc = ((char *) src) + 4 * len ;
24310 pdest = ((char *) dest) + 4 * len ;
24311 while (len > 0)
24312 { len -- ;
24313 psrc -= 4 ;
24314 pdest -= 4 ;
24316 pdest [0] = psrc [3] ;
24317 pdest [1] = psrc [2] ;
24318 pdest [2] = psrc [1] ;
24319 pdest [3] = psrc [0] ;
24321 #else
24322 while (len > 0)
24323 { len -- ;
24324 dest [len] = ENDSWAP_INT (src [len]) ;
24326 #endif
24327 } /* endswap_int_copy */
24329 /* This function assumes that sizeof (long) == 8, but works correctly even
24330 ** is sizeof (long) == 4.
24332 void
24333 endswap_long_copy (long *dest, long *src, int len)
24334 { char *psrc, *pdest ;
24336 psrc = (char *) src + 8 * len ;
24337 pdest = (char *) dest + 8 * len ;
24338 while (len > 0)
24339 { len -- ;
24340 psrc -= 8 ;
24341 pdest -= 8 ;
24343 pdest [0] = psrc [7] ;
24344 pdest [1] = psrc [6] ;
24345 pdest [2] = psrc [5] ;
24346 pdest [3] = psrc [4] ;
24347 pdest [4] = psrc [3] ;
24348 pdest [5] = psrc [2] ;
24349 pdest [6] = psrc [1] ;
24350 pdest [7] = psrc [0] ;
24352 } /* endswap_long_copy */
24355 ** Do not edit or modify anything in this comment block.
24356 ** The arch-tag line is a file identity tag for the GNU Arch
24357 ** revision control system.
24359 ** arch-tag: 4cf6cc94-da4e-4ef5-aa4c-03dc6cd16810
24362 * Copyright 1992 by Jutta Degener and Carsten Bormann, Technische
24363 * Universitaet Berlin. See the accompanying file "COPYRIGHT" for
24364 * details. THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE.
24367 #include <stdio.h>
24368 #include <assert.h>
24373 * SHORT TERM ANALYSIS FILTERING SECTION
24376 /* 4.2.8 */
24378 static void Decoding_of_the_coded_Log_Area_Ratios (
24379 word * LARc, /* coded log area ratio [0..7] IN */
24380 word * LARpp) /* out: decoded .. */
24382 register word temp1 /* , temp2 */;
24384 /* This procedure requires for efficient implementation
24385 * two tables.
24387 * INVA[1..8] = integer( (32768 * 8) / real_A[1..8])
24388 * MIC[1..8] = minimum value of the LARc[1..8]
24391 /* Compute the LARpp[1..8]
24394 /* for (i = 1; i <= 8; i++, B++, MIC++, INVA++, LARc++, LARpp++) {
24396 * temp1 = GSM_ADD( *LARc, *MIC ) << 10;
24397 * temp2 = *B << 1;
24398 * temp1 = GSM_SUB( temp1, temp2 );
24400 * assert(*INVA != MIN_WORD);
24402 * temp1 = GSM_MULT_R( *INVA, temp1 );
24403 * *LARpp = GSM_ADD( temp1, temp1 );
24407 #undef short_termSTEP
24408 #define short_termSTEP( B, MIC, INVA ) \
24409 temp1 = GSM_ADD( *LARc++, MIC ) << 10; \
24410 temp1 = GSM_SUB( temp1, B << 1 ); \
24411 temp1 = GSM_MULT_R( INVA, temp1 ); \
24412 *LARpp++ = GSM_ADD( temp1, temp1 );
24414 short_termSTEP( 0, -32, 13107 );
24415 short_termSTEP( 0, -32, 13107 );
24416 short_termSTEP( 2048, -16, 13107 );
24417 short_termSTEP( -2560, -16, 13107 );
24419 short_termSTEP( 94, -8, 19223 );
24420 short_termSTEP( -1792, -8, 17476 );
24421 short_termSTEP( -341, -4, 31454 );
24422 short_termSTEP( -1144, -4, 29708 );
24424 /* NOTE: the addition of *MIC is used to restore
24425 * the sign of *LARc.
24429 /* 4.2.9 */
24430 /* Computation of the quantized reflection coefficients
24433 /* 4.2.9.1 Interpolation of the LARpp[1..8] to get the LARp[1..8]
24437 * Within each frame of 160 analyzed speech samples the short term
24438 * analysis and synthesis filters operate with four different sets of
24439 * coefficients, derived from the previous set of decoded LARs(LARpp(j-1))
24440 * and the actual set of decoded LARs (LARpp(j))
24442 * (Initial value: LARpp(j-1)[1..8] = 0.)
24445 static void Coefficients_0_12 (
24446 register word * LARpp_j_1,
24447 register word * LARpp_j,
24448 register word * LARp)
24450 register int i;
24452 for (i = 1; i <= 8; i++, LARp++, LARpp_j_1++, LARpp_j++) {
24453 *LARp = GSM_ADD( SASR_W( *LARpp_j_1, 2 ), SASR_W( *LARpp_j, 2 ));
24454 *LARp = GSM_ADD( *LARp, SASR_W( *LARpp_j_1, 1));
24458 static void Coefficients_13_26 (
24459 register word * LARpp_j_1,
24460 register word * LARpp_j,
24461 register word * LARp)
24463 register int i;
24464 for (i = 1; i <= 8; i++, LARpp_j_1++, LARpp_j++, LARp++) {
24465 *LARp = GSM_ADD( SASR_W( *LARpp_j_1, 1), SASR_W( *LARpp_j, 1 ));
24469 static void Coefficients_27_39 (
24470 register word * LARpp_j_1,
24471 register word * LARpp_j,
24472 register word * LARp)
24474 register int i;
24476 for (i = 1; i <= 8; i++, LARpp_j_1++, LARpp_j++, LARp++) {
24477 *LARp = GSM_ADD( SASR_W( *LARpp_j_1, 2 ), SASR_W( *LARpp_j, 2 ));
24478 *LARp = GSM_ADD( *LARp, SASR_W( *LARpp_j, 1 ));
24483 static void Coefficients_40_159 (
24484 register word * LARpp_j,
24485 register word * LARp)
24487 register int i;
24489 for (i = 1; i <= 8; i++, LARp++, LARpp_j++)
24490 *LARp = *LARpp_j;
24493 /* 4.2.9.2 */
24495 static void LARp_to_rp (
24496 register word * LARp) /* [0..7] IN/OUT */
24498 * The input of this procedure is the interpolated LARp[0..7] array.
24499 * The reflection coefficients, rp[i], are used in the analysis
24500 * filter and in the synthesis filter.
24503 register int i;
24504 register word temp;
24506 for (i = 1; i <= 8; i++, LARp++) {
24508 /* temp = GSM_ABS( *LARp );
24510 * if (temp < 11059) temp <<= 1;
24511 * else if (temp < 20070) temp += 11059;
24512 * else temp = GSM_ADD( temp >> 2, 26112 );
24514 * *LARp = *LARp < 0 ? -temp : temp;
24517 if (*LARp < 0) {
24518 temp = *LARp == MIN_WORD ? MAX_WORD : -(*LARp);
24519 *LARp = - ((temp < 11059) ? temp << 1
24520 : ((temp < 20070) ? temp + 11059
24521 : GSM_ADD( (word) (temp >> 2), (word) 26112 )));
24522 } else {
24523 temp = *LARp;
24524 *LARp = (temp < 11059) ? temp << 1
24525 : ((temp < 20070) ? temp + 11059
24526 : GSM_ADD( (word) (temp >> 2), (word) 26112 ));
24532 /* 4.2.10 */
24533 static void Short_term_analysis_filtering (
24534 struct gsm_state * S,
24535 register word * rp, /* [0..7] IN */
24536 register int k_n, /* k_end - k_start */
24537 register word * s /* [0..n-1] IN/OUT */
24540 * This procedure computes the short term residual signal d[..] to be fed
24541 * to the RPE-LTP loop from the s[..] signal and from the local rp[..]
24542 * array (quantized reflection coefficients). As the call of this
24543 * procedure can be done in many ways (see the interpolation of the LAR
24544 * coefficient), it is assumed that the computation begins with index
24545 * k_start (for arrays d[..] and s[..]) and stops with index k_end
24546 * (k_start and k_end are defined in 4.2.9.1). This procedure also
24547 * needs to keep the array u[0..7] in memory for each call.
24550 register word * u = S->u;
24551 register int i;
24552 register word di, zzz, ui, sav, rpi;
24554 for (; k_n--; s++) {
24556 di = sav = *s;
24558 for (i = 0; i < 8; i++) { /* YYY */
24560 ui = u[i];
24561 rpi = rp[i];
24562 u[i] = sav;
24564 zzz = GSM_MULT_R(rpi, di);
24565 sav = GSM_ADD( ui, zzz);
24567 zzz = GSM_MULT_R(rpi, ui);
24568 di = GSM_ADD( di, zzz );
24571 *s = di;
24575 #if defined(USE_FLOAT_MUL) && defined(FAST)
24577 static void Fast_Short_term_analysis_filtering (
24578 struct gsm_state * S,
24579 register word * rp, /* [0..7] IN */
24580 register int k_n, /* k_end - k_start */
24581 register word * s /* [0..n-1] IN/OUT */
24584 register word * u = S->u;
24585 register int i;
24587 float uf[8],
24588 rpf[8];
24590 register float scalef = 3.0517578125e-5;
24591 register float sav, di, temp;
24593 for (i = 0; i < 8; ++i) {
24594 uf[i] = u[i];
24595 rpf[i] = rp[i] * scalef;
24597 for (; k_n--; s++) {
24598 sav = di = *s;
24599 for (i = 0; i < 8; ++i) {
24600 register float rpfi = rpf[i];
24601 register float ufi = uf[i];
24603 uf[i] = sav;
24604 temp = rpfi * di + ufi;
24605 di += rpfi * ufi;
24606 sav = temp;
24608 *s = di;
24610 for (i = 0; i < 8; ++i) u[i] = uf[i];
24612 #endif /* ! (defined (USE_FLOAT_MUL) && defined (FAST)) */
24614 static void Short_term_synthesis_filtering (
24615 struct gsm_state * S,
24616 register word * rrp, /* [0..7] IN */
24617 register int k, /* k_end - k_start */
24618 register word * wt, /* [0..k-1] IN */
24619 register word * sr /* [0..k-1] OUT */
24622 register word * v = S->v;
24623 register int i;
24624 register word sri, tmp1, tmp2;
24626 while (k--) {
24627 sri = *wt++;
24628 for (i = 8; i--;) {
24630 /* sri = GSM_SUB( sri, gsm_mult_r( rrp[i], v[i] ) );
24632 tmp1 = rrp[i];
24633 tmp2 = v[i];
24634 tmp2 = ( tmp1 == MIN_WORD && tmp2 == MIN_WORD
24635 ? MAX_WORD
24636 : 0x0FFFF & (( (longword)tmp1 * (longword)tmp2
24637 + 16384) >> 15)) ;
24639 sri = GSM_SUB( sri, tmp2 );
24641 /* v[i+1] = GSM_ADD( v[i], gsm_mult_r( rrp[i], sri ) );
24643 tmp1 = ( tmp1 == MIN_WORD && sri == MIN_WORD
24644 ? MAX_WORD
24645 : 0x0FFFF & (( (longword)tmp1 * (longword)sri
24646 + 16384) >> 15)) ;
24648 v[i+1] = GSM_ADD( v[i], tmp1);
24650 *sr++ = v[0] = sri;
24655 #if defined(FAST) && defined(USE_FLOAT_MUL)
24657 static void Fast_Short_term_synthesis_filtering (
24658 struct gsm_state * S,
24659 register word * rrp, /* [0..7] IN */
24660 register int k, /* k_end - k_start */
24661 register word * wt, /* [0..k-1] IN */
24662 register word * sr /* [0..k-1] OUT */
24665 register word * v = S->v;
24666 register int i;
24668 float va[9], rrpa[8];
24669 register float scalef = 3.0517578125e-5, temp;
24671 for (i = 0; i < 8; ++i) {
24672 va[i] = v[i];
24673 rrpa[i] = (float)rrp[i] * scalef;
24675 while (k--) {
24676 register float sri = *wt++;
24677 for (i = 8; i--;) {
24678 sri -= rrpa[i] * va[i];
24679 if (sri < -32768.) sri = -32768.;
24680 else if (sri > 32767.) sri = 32767.;
24682 temp = va[i] + rrpa[i] * sri;
24683 if (temp < -32768.) temp = -32768.;
24684 else if (temp > 32767.) temp = 32767.;
24685 va[i+1] = temp;
24687 *sr++ = va[0] = sri;
24689 for (i = 0; i < 9; ++i) v[i] = va[i];
24692 #endif /* defined(FAST) && defined(USE_FLOAT_MUL) */
24694 void Gsm_Short_Term_Analysis_Filter (
24696 struct gsm_state * S,
24698 word * LARc, /* coded log area ratio [0..7] IN */
24699 word * s /* signal [0..159] IN/OUT */
24702 word * LARpp_j = S->LARpp[ S->j ];
24703 word * LARpp_j_1 = S->LARpp[ S->j ^= 1 ];
24705 word LARp[8];
24707 #undef FILTER
24708 #if defined(FAST) && defined(USE_FLOAT_MUL)
24709 # define FILTER (* (S->fast \
24710 ? Fast_Short_term_analysis_filtering \
24711 : Short_term_analysis_filtering ))
24713 #else
24714 # define FILTER Short_term_analysis_filtering
24715 #endif
24717 Decoding_of_the_coded_Log_Area_Ratios( LARc, LARpp_j );
24719 Coefficients_0_12( LARpp_j_1, LARpp_j, LARp );
24720 LARp_to_rp( LARp );
24721 FILTER( S, LARp, 13, s);
24723 Coefficients_13_26( LARpp_j_1, LARpp_j, LARp);
24724 LARp_to_rp( LARp );
24725 FILTER( S, LARp, 14, s + 13);
24727 Coefficients_27_39( LARpp_j_1, LARpp_j, LARp);
24728 LARp_to_rp( LARp );
24729 FILTER( S, LARp, 13, s + 27);
24731 Coefficients_40_159( LARpp_j, LARp);
24732 LARp_to_rp( LARp );
24733 FILTER( S, LARp, 120, s + 40);
24736 void Gsm_Short_Term_Synthesis_Filter (
24737 struct gsm_state * S,
24739 word * LARcr, /* received log area ratios [0..7] IN */
24740 word * wt, /* received d [0..159] IN */
24742 word * s /* signal s [0..159] OUT */
24745 word * LARpp_j = S->LARpp[ S->j ];
24746 word * LARpp_j_1 = S->LARpp[ S->j ^=1 ];
24748 word LARp[8];
24750 #undef FILTER
24751 #if defined(FAST) && defined(USE_FLOAT_MUL)
24753 # define FILTER (* (S->fast \
24754 ? Fast_Short_term_synthesis_filtering \
24755 : Short_term_synthesis_filtering ))
24756 #else
24757 # define FILTER Short_term_synthesis_filtering
24758 #endif
24760 Decoding_of_the_coded_Log_Area_Ratios( LARcr, LARpp_j );
24762 Coefficients_0_12( LARpp_j_1, LARpp_j, LARp );
24763 LARp_to_rp( LARp );
24764 FILTER( S, LARp, 13, wt, s );
24766 Coefficients_13_26( LARpp_j_1, LARpp_j, LARp);
24767 LARp_to_rp( LARp );
24768 FILTER( S, LARp, 14, wt + 13, s + 13 );
24770 Coefficients_27_39( LARpp_j_1, LARpp_j, LARp);
24771 LARp_to_rp( LARp );
24772 FILTER( S, LARp, 13, wt + 27, s + 27 );
24774 Coefficients_40_159( LARpp_j, LARp );
24775 LARp_to_rp( LARp );
24776 FILTER(S, LARp, 120, wt + 40, s + 40);
24779 ** Do not edit or modify anything in this comment block.
24780 ** The arch-tag line is a file identity tag for the GNU Arch
24781 ** revision control system.
24783 ** arch-tag: 019ac7ba-c6dd-4540-abf0-8644b6c4a633
24787 ** Copyright (C) 1999-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
24789 ** This program is free software; you can redistribute it and/or modify
24790 ** it under the terms of the GNU Lesser General Public License as published by
24791 ** the Free Software Foundation; either version 2.1 of the License, or
24792 ** (at your option) any later version.
24794 ** This program is distributed in the hope that it will be useful,
24795 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
24796 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24797 ** GNU Lesser General Public License for more details.
24799 ** You should have received a copy of the GNU Lesser General Public License
24800 ** along with this program; if not, write to the Free Software
24801 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24805 #include <stdlib.h>
24806 #include <string.h>
24807 #include <ctype.h>
24810 #define SNDFILE_MAGICK 0x1234C0DE
24812 typedef struct
24813 { int error ;
24814 const char *str ;
24815 } ErrorStruct ;
24817 static
24818 ErrorStruct SndfileErrors [] =
24820 /* Public error values and their associated strings. */
24821 { SF_ERR_NO_ERROR , "No Error." },
24822 { SF_ERR_UNRECOGNISED_FORMAT , "File opened for read. Format not recognised." },
24823 { SF_ERR_SYSTEM , "System error." /* Often replaced. */ },
24825 /* Private error values and their associated strings. */
24826 { SFE_BAD_FILE , "File does not exist or is not a regular file (possibly a pipe?)." },
24827 { SFE_BAD_FILE_READ , "File exists but no data could be read." },
24828 { SFE_OPEN_FAILED , "Could not open file." },
24829 { SFE_BAD_SNDFILE_PTR , "Not a valid SNDFILE* pointer." },
24830 { SFE_BAD_SF_INFO_PTR , "NULL SF_INFO pointer passed to libsndfile." },
24831 { SFE_BAD_SF_INCOMPLETE , "SF_PRIVATE struct incomplete and end of header parsing." },
24832 { SFE_BAD_FILE_PTR , "Bad FILE pointer." },
24833 { SFE_BAD_INT_PTR , "Internal error, Bad pointer." },
24834 { SFE_BAD_STAT_SIZE , "Error : software was misconfigured at compile time (sizeof statbuf.st_size)." },
24836 { SFE_MALLOC_FAILED , "Internal malloc () failed." },
24837 { SFE_UNIMPLEMENTED , "File contains data in an unimplemented format." },
24838 { SFE_BAD_READ_ALIGN , "Attempt to read a non-integer number of channels." },
24839 { SFE_BAD_WRITE_ALIGN , "Attempt to write a non-integer number of channels." },
24840 { SFE_UNKNOWN_FORMAT , "File contains data in an unknown format." },
24841 { SFE_NOT_READMODE , "Read attempted on file currently open for write." },
24842 { SFE_NOT_WRITEMODE , "Write attempted on file currently open for read." },
24843 { SFE_BAD_MODE_RW , "This file format does not support read/write mode." },
24844 { SFE_BAD_SF_INFO , "Internal error : SF_INFO struct incomplete." },
24845 { SFE_BAD_OFFSET , "Error : supplied offset beyond end of file." },
24846 { SFE_NO_EMBED_SUPPORT , "Error : embedding not supported for this file format." },
24847 { SFE_NO_EMBEDDED_RDWR , "Error : cannot open embedded file read/write." },
24848 { SFE_NO_PIPE_WRITE , "Error : this file format does not support pipe write." },
24849 { SFE_BAD_RDWR_FORMAT , "Attempted to open read only format for RDWR." },
24851 { SFE_INTERLEAVE_MODE , "Attempt to write to file with non-interleaved data." },
24852 { SFE_INTERLEAVE_SEEK , "Bad karma in seek during interleave read operation." },
24853 { SFE_INTERLEAVE_READ , "Bad karma in read during interleave read operation." },
24855 { SFE_INTERNAL , "Unspecified internal error." },
24856 { SFE_LOG_OVERRUN , "Log buffer has overrun. File probably broken." },
24857 { SFE_BAD_CONTROL_CMD , "Bad command passed to function sf_command()." },
24858 { SFE_BAD_ENDIAN , "Bad endian-ness. Try default endian-ness" },
24859 { SFE_CHANNEL_COUNT , "Too many channels specified." },
24861 { SFE_BAD_SEEK , "Internal psf_fseek() failed." },
24862 { SFE_NOT_SEEKABLE , "Seek attempted on unseekable file type." },
24863 { SFE_AMBIGUOUS_SEEK , "Error: combination of file open mode and seek command is ambiguous." },
24864 { SFE_WRONG_SEEK , "Error: invalid seek parameters." },
24865 { SFE_SEEK_FAILED , "Error: parameters OK, but psf_seek() failed." },
24867 { SFE_BAD_OPEN_MODE , "Error: bad mode parameter for file open." },
24868 { SFE_OPEN_PIPE_RDWR , "Error: attempt toopen a pipe in read/write mode." },
24869 { SFE_RDWR_POSITION , "Error on RDWR position (cryptic)." },
24871 { SFE_STR_NO_SUPPORT , "Error : File type does not support string data." },
24872 { SFE_STR_MAX_DATA , "Error : Maximum string data storage reached." },
24873 { SFE_STR_MAX_COUNT , "Error : Maximum string data count reached." },
24874 { SFE_STR_BAD_TYPE , "Error : Bad string data type." },
24875 { SFE_STR_NO_ADD_END , "Error : file type does not support strings added at end of file." },
24876 { SFE_STR_BAD_STRING , "Error : bad string." },
24877 { SFE_STR_WEIRD , "Error : Weird string error." },
24878 { SFE_RDWR_BAD_HEADER , "Error : Cannot open file in read/write mode due to string data in header." },
24880 { SFE_WAV_NO_RIFF , "Error in WAV file. No 'RIFF' chunk marker." },
24881 { SFE_WAV_NO_WAVE , "Error in WAV file. No 'WAVE' chunk marker." },
24882 { SFE_WAV_NO_FMT , "Error in WAV file. No 'fmt ' chunk marker." },
24883 { SFE_WAV_FMT_SHORT , "Error in WAV file. Short 'fmt ' chunk." },
24885 { SFE_WAV_FMT_TOO_BIG , "Error in WAV file. 'fmt ' chunk too large." },
24886 { SFE_WAV_BAD_FACT , "Error in WAV file. 'fact' chunk out of place." },
24887 { SFE_WAV_BAD_PEAK , "Error in WAV file. Bad 'PEAK' chunk." },
24888 { SFE_WAV_PEAK_B4_FMT , "Error in WAV file. 'PEAK' chunk found before 'fmt ' chunk." },
24890 { SFE_WAV_BAD_FORMAT , "Error in WAV file. Errors in 'fmt ' chunk." },
24891 { SFE_WAV_BAD_BLOCKALIGN , "Error in WAV file. Block alignment in 'fmt ' chunk is incorrect." },
24892 { SFE_WAV_NO_DATA , "Error in WAV file. No 'data' chunk marker." },
24893 { SFE_WAV_UNKNOWN_CHUNK , "Error in WAV file. File contains an unknown chunk marker." },
24894 { SFE_WAV_WVPK_DATA , "Error in WAV file. Data is in WAVPACK format." },
24896 { SFE_WAV_ADPCM_NOT4BIT , "Error in ADPCM WAV file. Invalid bit width." },
24897 { SFE_WAV_ADPCM_CHANNELS , "Error in ADPCM WAV file. Invalid number of channels." },
24898 { SFE_WAV_GSM610_FORMAT , "Error in GSM610 WAV file. Invalid format chunk." },
24900 { SFE_AIFF_NO_FORM , "Error in AIFF file, bad 'FORM' marker." },
24901 { SFE_AIFF_AIFF_NO_FORM , "Error in AIFF file, 'AIFF' marker without 'FORM'." },
24902 { SFE_AIFF_COMM_NO_FORM , "Error in AIFF file, 'COMM' marker without 'FORM'." },
24903 { SFE_AIFF_SSND_NO_COMM , "Error in AIFF file, 'SSND' marker without 'COMM'." },
24904 { SFE_AIFF_UNKNOWN_CHUNK , "Error in AIFF file, unknown chunk." },
24905 { SFE_AIFF_COMM_CHUNK_SIZE, "Error in AIFF file, bad 'COMM' chunk size." },
24906 { SFE_AIFF_BAD_COMM_CHUNK , "Error in AIFF file, bad 'COMM' chunk." },
24907 { SFE_AIFF_PEAK_B4_COMM , "Error in AIFF file. 'PEAK' chunk found before 'COMM' chunk." },
24908 { SFE_AIFF_BAD_PEAK , "Error in AIFF file. Bad 'PEAK' chunk." },
24909 { SFE_AIFF_NO_SSND , "Error in AIFF file, bad 'SSND' chunk." },
24910 { SFE_AIFF_NO_DATA , "Error in AIFF file, no sound data." },
24911 { SFE_AIFF_RW_SSND_NOT_LAST, "Error in AIFF file, RDWR only possible if SSND chunk at end of file." },
24913 { SFE_AU_UNKNOWN_FORMAT , "Error in AU file, unknown format." },
24914 { SFE_AU_NO_DOTSND , "Error in AU file, missing '.snd' or 'dns.' marker." },
24915 { SFE_AU_EMBED_BAD_LEN , "Embedded AU file with unknown length." },
24917 { SFE_RAW_READ_BAD_SPEC , "Error while opening RAW file for read. Must specify format and channels.\n"
24918 "Possibly trying to open unsupported format."
24920 { SFE_RAW_BAD_BITWIDTH , "Error. RAW file bitwidth must be a multiple of 8." },
24921 { SFE_RAW_BAD_FORMAT , "Error. Bad format field in SF_INFO struct when openning a RAW file for read." },
24923 { SFE_PAF_NO_MARKER , "Error in PAF file, no marker." },
24924 { SFE_PAF_VERSION , "Error in PAF file, bad version." },
24925 { SFE_PAF_UNKNOWN_FORMAT , "Error in PAF file, unknown format." },
24926 { SFE_PAF_SHORT_HEADER , "Error in PAF file. File shorter than minimal header." },
24928 { SFE_SVX_NO_FORM , "Error in 8SVX / 16SV file, no 'FORM' marker." },
24929 { SFE_SVX_NO_BODY , "Error in 8SVX / 16SV file, no 'BODY' marker." },
24930 { SFE_SVX_NO_DATA , "Error in 8SVX / 16SV file, no sound data." },
24931 { SFE_SVX_BAD_COMP , "Error in 8SVX / 16SV file, unsupported compression format." },
24932 { SFE_SVX_BAD_NAME_LENGTH , "Error in 8SVX / 16SV file, NAME chunk too long." },
24934 { SFE_NIST_BAD_HEADER , "Error in NIST file, bad header." },
24935 { SFE_NIST_CRLF_CONVERISON, "Error : NIST file damaged by Windows CR -> CRLF conversion process." },
24936 { SFE_NIST_BAD_ENCODING , "Error in NIST file, unsupported compression format." },
24938 { SFE_VOC_NO_CREATIVE , "Error in VOC file, no 'Creative Voice File' marker." },
24939 { SFE_VOC_BAD_FORMAT , "Error in VOC file, bad format." },
24940 { SFE_VOC_BAD_VERSION , "Error in VOC file, bad version number." },
24941 { SFE_VOC_BAD_MARKER , "Error in VOC file, bad marker in file." },
24942 { SFE_VOC_BAD_SECTIONS , "Error in VOC file, incompatible VOC sections." },
24943 { SFE_VOC_MULTI_SAMPLERATE, "Error in VOC file, more than one sample rate defined." },
24944 { SFE_VOC_MULTI_SECTION , "Unimplemented VOC file feature, file contains multiple sound sections." },
24945 { SFE_VOC_MULTI_PARAM , "Error in VOC file, file contains multiple bit or channel widths." },
24946 { SFE_VOC_SECTION_COUNT , "Error in VOC file, too many sections." },
24947 { SFE_VOC_NO_PIPE , "Error : not able to operate on VOC files over a pipe." },
24949 { SFE_IRCAM_NO_MARKER , "Error in IRCAM file, bad IRCAM marker." },
24950 { SFE_IRCAM_BAD_CHANNELS , "Error in IRCAM file, bad channel count." },
24951 { SFE_IRCAM_UNKNOWN_FORMAT, "Error in IRCAM file, unknow encoding format." },
24953 { SFE_W64_64_BIT , "Error in W64 file, file contains 64 bit offset." },
24955 { SFE_W64_NO_RIFF , "Error in W64 file. No 'riff' chunk marker." },
24956 { SFE_W64_NO_WAVE , "Error in W64 file. No 'wave' chunk marker." },
24957 { SFE_W64_NO_FMT , "Error in W64 file. No 'fmt ' chunk marker." },
24958 { SFE_W64_NO_DATA , "Error in W64 file. No 'data' chunk marker." },
24960 { SFE_W64_FMT_SHORT , "Error in W64 file. Short 'fmt ' chunk." },
24961 { SFE_W64_FMT_TOO_BIG , "Error in W64 file. 'fmt ' chunk too large." },
24963 { SFE_W64_ADPCM_NOT4BIT , "Error in ADPCM W64 file. Invalid bit width." },
24964 { SFE_W64_ADPCM_CHANNELS , "Error in ADPCM W64 file. Invalid number of channels." },
24965 { SFE_W64_GSM610_FORMAT , "Error in GSM610 W64 file. Invalid format chunk." },
24967 { SFE_MAT4_BAD_NAME , "Error in MAT4 file. No variable name." },
24968 { SFE_MAT4_NO_SAMPLERATE , "Error in MAT4 file. No sample rate." },
24969 { SFE_MAT4_ZERO_CHANNELS , "Error in MAT4 file. Channel count is zero." },
24971 { SFE_MAT5_BAD_ENDIAN , "Error in MAT5 file. Not able to determine endian-ness." },
24972 { SFE_MAT5_NO_BLOCK , "Error in MAT5 file. Bad block structure." },
24973 { SFE_MAT5_SAMPLE_RATE , "Error in MAT5 file. Not able to determine sample rate." },
24974 { SFE_MAT5_ZERO_CHANNELS , "Error in MAT5 file. Channel count is zero." },
24976 { SFE_PVF_NO_PVF1 , "Error in PVF file. No PVF1 marker." },
24977 { SFE_PVF_BAD_HEADER , "Error in PVF file. Bad header." },
24978 { SFE_PVF_BAD_BITWIDTH , "Error in PVF file. Bad bit width." },
24980 { SFE_XI_BAD_HEADER , "Error in XI file. Bad header." },
24981 { SFE_XI_EXCESS_SAMPLES , "Error in XI file. Excess samples in file." },
24982 { SFE_XI_NO_PIPE , "Error : not able to operate on XI files over a pipe." },
24984 { SFE_HTK_NO_PIPE , "Error : not able to operate on HTK files over a pipe." },
24986 { SFE_SDS_NOT_SDS , "Error : not an SDS file." },
24987 { SFE_SDS_BAD_BIT_WIDTH , "Error : bad bit width for SDS file." },
24989 { SFE_DWVW_BAD_BITWIDTH , "Error : Bad bit width for DWVW encoding. Must be 12, 16 or 24." },
24990 { SFE_G72X_NOT_MONO , "Error : G72x encoding does not support more than 1 channel." },
24992 { SFE_MAX_ERROR , "Maximum error number." },
24993 { SFE_MAX_ERROR + 1 , NULL }
24996 /*------------------------------------------------------------------------------
24999 static int format_from_extension (const char *filename) ;
25000 static int guess_file_type (SF_PRIVATE *psf, const char *filename) ;
25001 static int validate_sfinfo (SF_INFO *sfinfo) ;
25002 static int validate_psf (SF_PRIVATE *psf) ;
25003 static void save_header_info (SF_PRIVATE *psf) ;
25004 static void copy_filename (SF_PRIVATE *psf, const char *path) ;
25005 static int psf_close (SF_PRIVATE *psf) ;
25006 static int psf_open_file (SF_PRIVATE *psf, int mode, SF_INFO *sfinfo) ;
25008 /*------------------------------------------------------------------------------
25009 ** Private (static) variables.
25012 static int sf_errno = 0 ;
25013 static char sf_logbuffer [SF_BUFFER_LEN] = { 0 } ;
25014 static char sf_syserr [SF_SYSERR_LEN] = { 0 } ;
25016 /*------------------------------------------------------------------------------
25019 #define VALIDATE_SNDFILE_AND_ASSIGN_PSF(a,b,c) \
25020 { if (! (a)) \
25021 { sf_errno = SFE_BAD_SNDFILE_PTR ; \
25022 return 0 ; \
25023 } ; \
25024 (b) = (SF_PRIVATE*) (a) ; \
25025 if (psf_filedes_valid (b) == 0) \
25026 { (b)->error = SFE_BAD_FILE_PTR ; \
25027 return 0 ; \
25028 } ; \
25029 if ((b)->Magick != SNDFILE_MAGICK) \
25030 { (b)->error = SFE_BAD_SNDFILE_PTR ; \
25031 return 0 ; \
25032 } ; \
25033 if (c) (b)->error = 0 ; \
25036 /*------------------------------------------------------------------------------
25037 ** Public functions.
25040 SNDFILE*
25041 sf_open (const char *path, int mode, SF_INFO *sfinfo)
25042 { SF_PRIVATE *psf ;
25043 int error = 0 ;
25045 if ((psf = calloc (1, sizeof (SF_PRIVATE))) == NULL)
25046 { sf_errno = SFE_MALLOC_FAILED ;
25047 return NULL ;
25050 memset (psf, 0, sizeof (SF_PRIVATE)) ;
25052 psf_log_printf (psf, "File : %s\n", path) ;
25054 copy_filename (psf, path) ;
25056 if (strcmp (path, "-") == 0)
25057 error = psf_set_stdio (psf, mode) ;
25058 else
25059 error = psf_fopen (psf, path, mode) ;
25061 if (error == 0)
25062 error = psf_open_file (psf, mode, sfinfo) ;
25064 if (error)
25065 { sf_errno = error ;
25066 if (error == SFE_SYSTEM)
25067 LSF_SNPRINTF (sf_syserr, sizeof (sf_syserr), "%s", psf->syserr) ;
25068 LSF_SNPRINTF (sf_logbuffer, sizeof (sf_logbuffer), "%s", psf->logbuffer) ;
25069 psf_close (psf) ;
25070 return NULL ;
25073 memcpy (sfinfo, &(psf->sf), sizeof (SF_INFO)) ;
25075 return (SNDFILE*) psf ;
25076 } /* sf_open */
25078 SNDFILE*
25079 sf_open_fd (int fd, int mode, SF_INFO *sfinfo, int close_desc)
25080 { SF_PRIVATE *psf ;
25081 int error ;
25083 if ((psf = calloc (1, sizeof (SF_PRIVATE))) == NULL)
25084 { sf_errno = SFE_MALLOC_FAILED ;
25085 return NULL ;
25088 psf_set_file (psf, fd) ;
25089 psf->is_pipe = psf_is_pipe (psf) ;
25090 psf->fileoffset = psf_ftell (psf) ;
25092 if (! close_desc)
25093 psf->do_not_close_descriptor = SF_TRUE ;
25095 error = psf_open_file (psf, mode, sfinfo) ;
25097 if (error)
25098 { sf_errno = error ;
25099 if (error == SFE_SYSTEM)
25100 LSF_SNPRINTF (sf_syserr, sizeof (sf_syserr), "%s", psf->syserr) ;
25101 LSF_SNPRINTF (sf_logbuffer, sizeof (sf_logbuffer), "%s", psf->logbuffer) ;
25102 psf_close (psf) ;
25103 return NULL ;
25106 memcpy (sfinfo, &(psf->sf), sizeof (SF_INFO)) ;
25108 return (SNDFILE*) psf ;
25109 } /* sf_open_fd */
25111 /*------------------------------------------------------------------------------
25115 sf_close (SNDFILE *sndfile)
25116 { SF_PRIVATE *psf ;
25118 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
25120 return psf_close (psf) ;
25121 } /* sf_close */
25123 /*==============================================================================
25126 const char*
25127 sf_error_number (int errnum)
25128 { static const char *bad_errnum =
25129 "No error defined for this error number. This is a bug in libsndfile." ;
25130 int k ;
25132 if (errnum == SFE_MAX_ERROR)
25133 return SndfileErrors [0].str ;
25135 if (errnum < 0 || errnum > SFE_MAX_ERROR)
25136 { /* This really shouldn't happen in release versions. */
25137 printf ("Not a valid error number (%d).\n", errnum) ;
25138 return bad_errnum ;
25141 for (k = 0 ; SndfileErrors [k].str ; k++)
25142 if (errnum == SndfileErrors [k].error)
25143 return SndfileErrors [k].str ;
25145 return bad_errnum ;
25146 } /* sf_error_number */
25148 const char*
25149 sf_strerror (SNDFILE *sndfile)
25150 { SF_PRIVATE *psf = NULL ;
25151 int errnum ;
25153 if (! sndfile)
25154 { errnum = sf_errno ;
25155 if (errnum == SFE_SYSTEM && sf_syserr [0])
25156 return sf_syserr ;
25158 else
25159 { psf = (SF_PRIVATE *) sndfile ;
25161 if (psf->Magick != SNDFILE_MAGICK)
25162 return "sf_strerror : Bad magic number." ;
25164 errnum = psf->error ;
25166 if (errnum == SFE_SYSTEM && psf->syserr [0])
25167 return psf->syserr ;
25170 return sf_error_number (errnum) ;
25171 } /* sf_strerror */
25173 /*------------------------------------------------------------------------------
25177 sf_error (SNDFILE *sndfile)
25178 { SF_PRIVATE *psf ;
25180 if (! sndfile)
25181 { if (sf_error != 0)
25182 return 1 ;
25183 return 0 ;
25186 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 0) ;
25188 if (psf->error)
25189 return 1 ;
25191 return 0 ;
25192 } /* sf_error */
25194 /*------------------------------------------------------------------------------
25198 sf_perror (SNDFILE *sndfile)
25199 { SF_PRIVATE *psf ;
25200 int errnum ;
25202 if (! sndfile)
25203 { errnum = sf_errno ;
25205 else
25206 { VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 0) ;
25207 errnum = psf->error ;
25210 fprintf (stderr, "%s\n", sf_error_number (errnum)) ;
25211 return SFE_NO_ERROR ;
25212 } /* sf_perror */
25215 /*------------------------------------------------------------------------------
25219 sf_error_str (SNDFILE *sndfile, char *str, size_t maxlen)
25220 { SF_PRIVATE *psf ;
25221 int errnum ;
25223 if (! str)
25224 return SFE_INTERNAL ;
25226 if (! sndfile)
25227 errnum = sf_errno ;
25228 else
25229 { VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 0) ;
25230 errnum = psf->error ;
25233 LSF_SNPRINTF (str, maxlen, "%s", sf_error_number (errnum)) ;
25235 return SFE_NO_ERROR ;
25236 } /* sf_error_str */
25238 /*==============================================================================
25242 sf_format_check (const SF_INFO *info)
25243 { int subformat, endian ;
25245 subformat = info->format & SF_FORMAT_SUBMASK ;
25246 endian = info->format & SF_FORMAT_ENDMASK ;
25248 /* This is the place where each file format can check if the suppiled
25249 ** SF_INFO struct is valid.
25250 ** Return 0 on failure, 1 ons success.
25253 if (info->channels < 1 || info->channels > 256)
25254 return 0 ;
25256 if (info->samplerate < 0)
25257 return 0 ;
25259 switch (info->format & SF_FORMAT_TYPEMASK)
25260 { case SF_FORMAT_WAV :
25261 case SF_FORMAT_WAVEX :
25262 /* WAV is strictly little endian. */
25263 if (endian == SF_ENDIAN_BIG || endian == SF_ENDIAN_CPU)
25264 return 0 ;
25265 if (subformat == SF_FORMAT_PCM_U8 || subformat == SF_FORMAT_PCM_16)
25266 return 1 ;
25267 if (subformat == SF_FORMAT_PCM_24 || subformat == SF_FORMAT_PCM_32)
25268 return 1 ;
25269 if ((subformat == SF_FORMAT_IMA_ADPCM || subformat == SF_FORMAT_MS_ADPCM) && info->channels <= 2)
25270 return 1 ;
25271 if (subformat == SF_FORMAT_GSM610 && info->channels == 1)
25272 return 1 ;
25273 if (subformat == SF_FORMAT_ULAW || subformat == SF_FORMAT_ALAW)
25274 return 1 ;
25275 if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE)
25276 return 1 ;
25277 break ;
25279 case SF_FORMAT_AIFF :
25280 /* AIFF does allow both endian-nesses for PCM data.*/
25281 if (subformat == SF_FORMAT_PCM_16 || subformat == SF_FORMAT_PCM_24 || subformat == SF_FORMAT_PCM_32)
25282 return 1 ;
25283 /* Other encodings. Check for endian-ness. */
25284 if (endian == SF_ENDIAN_LITTLE || endian == SF_ENDIAN_CPU)
25285 return 0 ;
25286 if (subformat == SF_FORMAT_PCM_U8 || subformat == SF_FORMAT_PCM_S8)
25287 return 1 ;
25288 if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE)
25289 return 1 ;
25290 if (subformat == SF_FORMAT_ULAW || subformat == SF_FORMAT_ALAW)
25291 return 1 ;
25292 if ((subformat == SF_FORMAT_DWVW_12 || subformat == SF_FORMAT_DWVW_16 ||
25293 subformat == SF_FORMAT_DWVW_24) && info-> channels == 1)
25294 return 1 ;
25295 if (subformat == SF_FORMAT_GSM610 && info->channels == 1)
25296 return 1 ;
25297 if (subformat == SF_FORMAT_IMA_ADPCM && (info->channels == 1 || info->channels == 2))
25298 return 1 ;
25299 break ;
25301 case SF_FORMAT_AU :
25302 if (subformat == SF_FORMAT_PCM_S8 || subformat == SF_FORMAT_PCM_16)
25303 return 1 ;
25304 if (subformat == SF_FORMAT_PCM_24 || subformat == SF_FORMAT_PCM_32)
25305 return 1 ;
25306 if (subformat == SF_FORMAT_ULAW || subformat == SF_FORMAT_ALAW)
25307 return 1 ;
25308 if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE)
25309 return 1 ;
25310 if (subformat == SF_FORMAT_G721_32 && info->channels == 1)
25311 return 1 ;
25312 if (subformat == SF_FORMAT_G723_24 && info->channels == 1)
25313 return 1 ;
25314 if (subformat == SF_FORMAT_G723_40 && info->channels == 1)
25315 return 1 ;
25316 break ;
25318 case SF_FORMAT_RAW :
25319 if (subformat == SF_FORMAT_PCM_U8 || subformat == SF_FORMAT_PCM_S8 || subformat == SF_FORMAT_PCM_16)
25320 return 1 ;
25321 if (subformat == SF_FORMAT_PCM_24 || subformat == SF_FORMAT_PCM_32)
25322 return 1 ;
25323 if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE)
25324 return 1 ;
25325 if (subformat == SF_FORMAT_ALAW || subformat == SF_FORMAT_ULAW)
25326 return 1 ;
25327 if ((subformat == SF_FORMAT_DWVW_12 || subformat == SF_FORMAT_DWVW_16 ||
25328 subformat == SF_FORMAT_DWVW_24) && info-> channels == 1)
25329 return 1 ;
25330 if (subformat == SF_FORMAT_GSM610 && info->channels == 1)
25331 return 1 ;
25332 if (subformat == SF_FORMAT_VOX_ADPCM && info->channels == 1)
25333 return 1 ;
25334 break ;
25336 case SF_FORMAT_PAF :
25337 if (subformat == SF_FORMAT_PCM_S8 || subformat == SF_FORMAT_PCM_16)
25338 return 1 ;
25339 if (subformat == SF_FORMAT_PCM_24 || subformat == SF_FORMAT_PCM_32)
25340 return 1 ;
25341 break ;
25343 case SF_FORMAT_SVX :
25344 /* SVX currently does not support more than one channel for write.
25345 ** Read will allow more than one channel but only allow one here.
25347 if (info->channels != 1)
25348 return 0 ;
25349 /* Always big endian. */
25350 if (endian == SF_ENDIAN_LITTLE || endian == SF_ENDIAN_CPU)
25351 return 0 ;
25353 if ((subformat == SF_FORMAT_PCM_S8 || subformat == SF_FORMAT_PCM_16) && info->channels == 1)
25354 return 1 ;
25355 break ;
25357 case SF_FORMAT_NIST :
25358 if (subformat == SF_FORMAT_PCM_S8 || subformat == SF_FORMAT_PCM_16)
25359 return 1 ;
25360 if (subformat == SF_FORMAT_PCM_24 || subformat == SF_FORMAT_PCM_32)
25361 return 1 ;
25362 if (subformat == SF_FORMAT_ULAW || subformat == SF_FORMAT_ALAW)
25363 return 1 ;
25364 break ;
25366 case SF_FORMAT_IRCAM :
25367 if (subformat == SF_FORMAT_PCM_16 || subformat == SF_FORMAT_PCM_24 || subformat == SF_FORMAT_PCM_32)
25368 return 1 ;
25369 if (subformat == SF_FORMAT_ULAW || subformat == SF_FORMAT_ALAW || subformat == SF_FORMAT_FLOAT)
25370 return 1 ;
25371 break ;
25373 case SF_FORMAT_VOC :
25374 /* VOC is strictly little endian. */
25375 if (endian == SF_ENDIAN_BIG || endian == SF_ENDIAN_CPU)
25376 return 0 ;
25377 if (subformat == SF_FORMAT_PCM_U8 || subformat == SF_FORMAT_PCM_16)
25378 return 1 ;
25379 if (subformat == SF_FORMAT_ULAW || subformat == SF_FORMAT_ALAW)
25380 return 1 ;
25381 break ;
25383 case SF_FORMAT_W64 :
25384 /* W64 is strictly little endian. */
25385 if (endian == SF_ENDIAN_BIG || endian == SF_ENDIAN_CPU)
25386 return 0 ;
25387 if (subformat == SF_FORMAT_PCM_U8 || subformat == SF_FORMAT_PCM_16)
25388 return 1 ;
25389 if (subformat == SF_FORMAT_PCM_24 || subformat == SF_FORMAT_PCM_32)
25390 return 1 ;
25391 if ((subformat == SF_FORMAT_IMA_ADPCM || subformat == SF_FORMAT_MS_ADPCM) && info->channels <= 2)
25392 return 1 ;
25393 if (subformat == SF_FORMAT_GSM610 && info->channels == 1)
25394 return 1 ;
25395 if (subformat == SF_FORMAT_ULAW || subformat == SF_FORMAT_ALAW)
25396 return 1 ;
25397 if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE)
25398 return 1 ;
25399 break ;
25401 case SF_FORMAT_MAT4 :
25402 if (subformat == SF_FORMAT_PCM_16 || subformat == SF_FORMAT_PCM_32)
25403 return 1 ;
25404 if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE)
25405 return 1 ;
25406 break ;
25408 case SF_FORMAT_MAT5 :
25409 if (subformat == SF_FORMAT_PCM_U8 || subformat == SF_FORMAT_PCM_16 || subformat == SF_FORMAT_PCM_32)
25410 return 1 ;
25411 if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE)
25412 return 1 ;
25413 break ;
25415 case SF_FORMAT_PVF :
25416 if (subformat == SF_FORMAT_PCM_S8 || subformat == SF_FORMAT_PCM_16 || subformat == SF_FORMAT_PCM_32)
25417 return 1 ;
25418 break ;
25420 case SF_FORMAT_XI :
25421 if (info->channels != 1)
25422 return 0 ;
25423 if (subformat == SF_FORMAT_DPCM_8 || subformat == SF_FORMAT_DPCM_16)
25424 return 1 ;
25425 break ;
25427 case SF_FORMAT_HTK :
25428 /* HTK is strictly big endian. */
25429 if (endian == SF_ENDIAN_LITTLE || endian == SF_ENDIAN_CPU)
25430 return 0 ;
25431 if (info->channels != 1)
25432 return 0 ;
25433 if (subformat == SF_FORMAT_PCM_16)
25434 return 1 ;
25435 break ;
25437 case SF_FORMAT_SDS :
25438 /* SDS is strictly big endian. */
25439 if (endian == SF_ENDIAN_LITTLE || endian == SF_ENDIAN_CPU)
25440 return 0 ;
25441 if (info->channels != 1)
25442 return 0 ;
25443 if (subformat == SF_FORMAT_PCM_S8 || subformat == SF_FORMAT_PCM_16 || subformat == SF_FORMAT_PCM_24)
25444 return 1 ;
25445 break ;
25447 case SF_FORMAT_AVR :
25448 /* SDS is strictly big endian. */
25449 if (endian == SF_ENDIAN_LITTLE || endian == SF_ENDIAN_CPU)
25450 return 0 ;
25451 if (info->channels < 1 || info->channels > 2)
25452 return 0 ;
25453 if (subformat == SF_FORMAT_PCM_U8 || subformat == SF_FORMAT_PCM_S8 || subformat == SF_FORMAT_PCM_16)
25454 return 1 ;
25455 break ;
25458 case SF_FORMAT_SD2 :
25459 /+* SD2 is strictly big endian. *+/
25460 if (endian == SF_ENDIAN_LITTLE || endian == SF_ENDIAN_CPU)
25461 return 0 ;
25462 if (subformat == SF_FORMAT_PCM_16)
25463 return 1 ;
25464 break ;
25467 default : break ;
25470 return 0 ;
25471 } /* sf_format_check */
25473 /*------------------------------------------------------------------------------
25477 sf_command (SNDFILE *sndfile, int command, void *data, int datasize)
25478 { SF_PRIVATE *psf = NULL ;
25480 /* This set of commands do not need the sndfile parameter. */
25481 switch (command)
25482 { case SFC_GET_LIB_VERSION :
25483 if (data == NULL)
25484 return (psf->error = SFE_BAD_CONTROL_CMD) ;
25485 if (ENABLE_EXPERIMENTAL_CODE)
25486 LSF_SNPRINTF (data, datasize, "%s-%s-exp", PACKAGE_NAME, PACKAGE_VERSION) ;
25487 else
25488 LSF_SNPRINTF (data, datasize, "%s-%s", PACKAGE_NAME, PACKAGE_VERSION) ;
25489 return 0 ;
25491 case SFC_GET_SIMPLE_FORMAT_COUNT :
25492 if (data == NULL || datasize != SIGNED_SIZEOF (int))
25493 return (sf_errno = SFE_BAD_CONTROL_CMD) ;
25494 *((int*) data) = psf_get_format_simple_count () ;
25495 return 0 ;
25497 case SFC_GET_SIMPLE_FORMAT :
25498 if (data == NULL || datasize != SIGNED_SIZEOF (SF_FORMAT_INFO))
25499 return (sf_errno = SFE_BAD_CONTROL_CMD) ;
25500 return psf_get_format_simple (data) ;
25502 case SFC_GET_FORMAT_MAJOR_COUNT :
25503 if (data == NULL || datasize != SIGNED_SIZEOF (int))
25504 return (sf_errno = SFE_BAD_CONTROL_CMD) ;
25505 *((int*) data) = psf_get_format_major_count () ;
25506 return 0 ;
25508 case SFC_GET_FORMAT_MAJOR :
25509 if (data == NULL || datasize != SIGNED_SIZEOF (SF_FORMAT_INFO))
25510 return (sf_errno = SFE_BAD_CONTROL_CMD) ;
25511 return psf_get_format_major (data) ;
25513 case SFC_GET_FORMAT_SUBTYPE_COUNT :
25514 if (data == NULL || datasize != SIGNED_SIZEOF (int))
25515 return (sf_errno = SFE_BAD_CONTROL_CMD) ;
25516 *((int*) data) = psf_get_format_subtype_count () ;
25517 return 0 ;
25519 case SFC_GET_FORMAT_SUBTYPE :
25520 if (data == NULL || datasize != SIGNED_SIZEOF (SF_FORMAT_INFO))
25521 return (sf_errno = SFE_BAD_CONTROL_CMD) ;
25522 return psf_get_format_subtype (data) ;
25524 case SFC_GET_FORMAT_INFO :
25525 if (data == NULL || datasize != SIGNED_SIZEOF (SF_FORMAT_INFO))
25526 return (sf_errno = SFE_BAD_CONTROL_CMD) ;
25527 return psf_get_format_info (data) ;
25530 if (! sndfile && command == SFC_GET_LOG_INFO)
25531 { if (data == NULL)
25532 return (psf->error = SFE_BAD_CONTROL_CMD) ;
25533 LSF_SNPRINTF (data, datasize, "%s", sf_logbuffer) ;
25534 return 0 ;
25537 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
25539 switch (command)
25540 { case SFC_SET_NORM_FLOAT :
25541 psf->norm_float = (datasize) ? SF_TRUE : SF_FALSE ;
25542 return psf->norm_float ;
25544 case SFC_SET_NORM_DOUBLE :
25545 psf->norm_double = (datasize) ? SF_TRUE : SF_FALSE ;
25546 return psf->norm_double ;
25548 case SFC_GET_NORM_FLOAT :
25549 return psf->norm_float ;
25551 case SFC_GET_NORM_DOUBLE :
25552 return psf->norm_double ;
25554 case SFC_SET_ADD_PEAK_CHUNK :
25555 { int format = psf->sf.format & SF_FORMAT_TYPEMASK ;
25557 /* Only WAV and AIFF support the PEAK chunk. */
25558 if (format != SF_FORMAT_WAV && format != SF_FORMAT_WAVEX && format != SF_FORMAT_AIFF)
25559 return SF_FALSE ;
25561 format = psf->sf.format & SF_FORMAT_SUBMASK ;
25563 /* Only files containg the following data types support the PEAK chunk. */
25564 if (format != SF_FORMAT_FLOAT && format != SF_FORMAT_DOUBLE)
25565 return SF_FALSE ;
25568 /* Can only do this is in SFM_WRITE mode. */
25569 if (psf->mode != SFM_WRITE)
25570 return SF_FALSE ;
25571 /* If data has already been written this must fail. */
25572 if (psf->have_written)
25573 return psf->has_peak ;
25574 /* Everything seems OK, so set psf->has_peak and re-write header. */
25575 psf->has_peak = (datasize) ? SF_TRUE : SF_FALSE ;
25576 if (psf->write_header)
25577 psf->write_header (psf, SF_TRUE) ;
25578 return psf->has_peak ;
25580 case SFC_GET_LOG_INFO :
25581 if (data == NULL)
25582 return (psf->error = SFE_BAD_CONTROL_CMD) ;
25583 LSF_SNPRINTF (data, datasize, "%s", psf->logbuffer) ;
25584 break ;
25586 case SFC_CALC_SIGNAL_MAX :
25587 if (data == NULL || datasize != sizeof (double))
25588 return (psf->error = SFE_BAD_CONTROL_CMD) ;
25589 *((double*) data) = psf_calc_signal_max (psf, SF_FALSE) ;
25590 break ;
25592 case SFC_CALC_NORM_SIGNAL_MAX :
25593 if (data == NULL || datasize != sizeof (double))
25594 return (psf->error = SFE_BAD_CONTROL_CMD) ;
25595 *((double*) data) = psf_calc_signal_max (psf, SF_TRUE) ;
25596 break ;
25598 case SFC_CALC_MAX_ALL_CHANNELS :
25599 if (data == NULL || datasize != SIGNED_SIZEOF (double) * psf->sf.channels)
25600 return (psf->error = SFE_BAD_CONTROL_CMD) ;
25601 return psf_calc_max_all_channels (psf, (double*) data, SF_FALSE) ;
25603 case SFC_CALC_NORM_MAX_ALL_CHANNELS :
25604 if (data == NULL || datasize != SIGNED_SIZEOF (double) * psf->sf.channels)
25605 return (psf->error = SFE_BAD_CONTROL_CMD) ;
25606 return psf_calc_max_all_channels (psf, (double*) data, SF_TRUE) ;
25608 case SFC_UPDATE_HEADER_NOW :
25609 if (psf->write_header)
25610 psf->write_header (psf, SF_TRUE) ;
25611 break ;
25613 case SFC_SET_UPDATE_HEADER_AUTO :
25614 psf->auto_header = datasize ? SF_TRUE : SF_FALSE ;
25615 return psf->auto_header ;
25616 break ;
25618 case SFC_SET_ADD_DITHER_ON_WRITE :
25619 case SFC_SET_ADD_DITHER_ON_READ :
25621 ** FIXME !
25622 ** These are obsolete. Just return.
25623 ** Remove some time after version 1.0.8.
25625 break ;
25627 case SFC_SET_DITHER_ON_WRITE :
25628 if (data == NULL || datasize != SIGNED_SIZEOF (SF_DITHER_INFO))
25629 return (psf->error = SFE_BAD_CONTROL_CMD) ;
25630 memcpy (&psf->write_dither, data, sizeof (psf->write_dither)) ;
25631 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
25632 dither_init (psf, SFM_WRITE) ;
25633 break ;
25635 case SFC_SET_DITHER_ON_READ :
25636 if (data == NULL || datasize != SIGNED_SIZEOF (SF_DITHER_INFO))
25637 return (psf->error = SFE_BAD_CONTROL_CMD) ;
25638 memcpy (&psf->read_dither, data, sizeof (psf->read_dither)) ;
25639 if (psf->mode == SFM_READ || psf->mode == SFM_RDWR)
25640 dither_init (psf, SFM_READ) ;
25641 break ;
25643 case SFC_FILE_TRUNCATE :
25644 if (psf->mode != SFM_WRITE && psf->mode != SFM_RDWR)
25645 return SF_TRUE ;
25646 if (datasize != sizeof (sf_count_t))
25647 return SF_TRUE ;
25648 { sf_count_t position ;
25650 position = *((sf_count_t*) data) ;
25652 if (sf_seek (sndfile, position, SEEK_SET) != position)
25653 return SF_TRUE ;
25655 psf->sf.frames = position ;
25657 position = psf_fseek (psf, 0, SEEK_CUR) ;
25659 return psf_ftruncate (psf, position) ;
25661 break ;
25663 case SFC_SET_RAW_START_OFFSET :
25664 if (data == NULL || datasize != sizeof (sf_count_t))
25665 return (psf->error = SFE_BAD_CONTROL_CMD) ;
25666 if ((psf->sf.format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
25667 return (psf->error = SFE_BAD_CONTROL_CMD) ;
25669 psf->dataoffset = *((sf_count_t*) data) ;
25670 sf_seek (sndfile, 0, SEEK_CUR) ;
25671 break ;
25673 case SFC_GET_EMBED_FILE_INFO :
25674 if (data == NULL || datasize != sizeof (SF_EMBED_FILE_INFO))
25675 return (psf->error = SFE_BAD_CONTROL_CMD) ;
25677 ((SF_EMBED_FILE_INFO*) data)->offset = psf->fileoffset ;
25678 ((SF_EMBED_FILE_INFO*) data)->length = psf->filelength ;
25679 break ;
25681 /* Lite remove start */
25682 case SFC_TEST_IEEE_FLOAT_REPLACE :
25683 psf->ieee_replace = (datasize) ? SF_TRUE : SF_FALSE ;
25684 if ((psf->sf.format & SF_FORMAT_SUBMASK) == SF_FORMAT_FLOAT)
25685 float32_init (psf) ;
25686 else if ((psf->sf.format & SF_FORMAT_SUBMASK) == SF_FORMAT_DOUBLE)
25687 double64_init (psf) ;
25688 else
25689 return (psf->error = SFE_BAD_CONTROL_CMD) ;
25690 break ;
25691 /* Lite remove end */
25693 case SFC_SET_CLIPPING :
25694 psf->add_clipping = (datasize) ? SF_TRUE : SF_FALSE ;
25695 return psf->add_clipping ;
25697 case SFC_GET_CLIPPING :
25698 return psf->add_clipping ;
25700 default :
25701 /* Must be a file specific command. Pass it on. */
25702 if (psf->command)
25703 return psf->command (psf, command, data, datasize) ;
25705 psf_log_printf (psf, "*** sf_command : cmd = 0x%X\n", command) ;
25706 return (psf->error = SFE_BAD_CONTROL_CMD) ;
25709 return 0 ;
25710 } /* sf_command */
25712 /*------------------------------------------------------------------------------
25715 sf_count_t
25716 sf_seek (SNDFILE *sndfile, sf_count_t offset, int whence)
25717 { SF_PRIVATE *psf ;
25718 sf_count_t seek_from_start = 0, retval ;
25720 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
25722 if (! psf->sf.seekable)
25723 { psf->error = SFE_NOT_SEEKABLE ;
25724 return ((sf_count_t) -1) ;
25727 /* If the whence parameter has a mode ORed in, check to see that
25728 ** it makes sense.
25730 if (((whence & SFM_MASK) == SFM_WRITE && psf->mode == SFM_READ) ||
25731 ((whence & SFM_MASK) == SFM_WRITE && psf->mode == SFM_WRITE))
25732 { psf->error = SFE_WRONG_SEEK ;
25733 return ((sf_count_t) -1) ;
25736 /* Convert all SEEK_CUR and SEEK_END into seek_from_start to be
25737 ** used with SEEK_SET.
25739 switch (whence)
25740 { /* The SEEK_SET behaviour is independant of mode. */
25741 case SEEK_SET :
25742 case SEEK_SET | SFM_READ :
25743 case SEEK_SET | SFM_WRITE :
25744 case SEEK_SET | SFM_RDWR :
25745 seek_from_start = offset ;
25746 break ;
25748 /* The SEEK_CUR is a little more tricky. */
25749 case SEEK_CUR :
25750 if (offset == 0)
25751 { if (psf->mode == SFM_READ)
25752 return psf->read_current ;
25753 if (psf->mode == SFM_WRITE)
25754 return psf->write_current ;
25756 if (psf->mode == SFM_READ)
25757 seek_from_start = psf->read_current + offset ;
25758 else if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
25759 seek_from_start = psf->write_current + offset ;
25760 else
25761 psf->error = SFE_AMBIGUOUS_SEEK ;
25762 break ;
25764 case SEEK_CUR | SFM_READ :
25765 if (offset == 0)
25766 return psf->read_current ;
25767 seek_from_start = psf->read_current + offset ;
25768 break ;
25770 case SEEK_CUR | SFM_WRITE :
25771 if (offset == 0)
25772 return psf->write_current ;
25773 seek_from_start = psf->write_current + offset ;
25774 break ;
25776 /* The SEEK_END */
25777 case SEEK_END :
25778 case SEEK_END | SFM_READ :
25779 case SEEK_END | SFM_WRITE :
25780 seek_from_start = psf->sf.frames + offset ;
25781 break ;
25783 default :
25784 psf->error = SFE_BAD_SEEK ;
25785 break ;
25788 if (psf->error)
25789 return ((sf_count_t) -1) ;
25791 if (seek_from_start < 0 || seek_from_start > psf->sf.frames)
25792 { psf->error = SFE_BAD_SEEK ;
25793 return ((sf_count_t) -1) ;
25796 if (psf->seek)
25797 { int new_mode = (whence & SFM_MASK) ? (whence & SFM_MASK) : psf->mode ;
25799 retval = psf->seek (psf, new_mode, seek_from_start) ;
25801 switch (new_mode)
25802 { case SFM_READ :
25803 psf->read_current = retval ;
25804 break ;
25805 case SFM_WRITE :
25806 psf->write_current = retval ;
25807 break ;
25808 case SFM_RDWR :
25809 psf->read_current = retval ;
25810 psf->write_current = retval ;
25811 new_mode = SFM_READ ;
25812 break ;
25815 psf->last_op = new_mode ;
25817 return retval ;
25820 psf->error = SFE_AMBIGUOUS_SEEK ;
25821 return ((sf_count_t) -1) ;
25822 } /* sf_seek */
25824 /*------------------------------------------------------------------------------
25827 const char*
25828 sf_get_string (SNDFILE *sndfile, int str_type)
25829 { SF_PRIVATE *psf ;
25831 if ((psf = (SF_PRIVATE*) sndfile) == NULL)
25832 return NULL ;
25833 if (psf->Magick != SNDFILE_MAGICK)
25834 return NULL ;
25836 return psf_get_string (psf, str_type) ;
25837 } /* sf_get_string */
25840 sf_set_string (SNDFILE *sndfile, int str_type, const char* str)
25841 { SF_PRIVATE *psf ;
25843 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
25845 return psf_store_string (psf, str_type, str) ;
25846 } /* sf_get_string */
25848 /*==============================================================================
25851 sf_count_t
25852 sf_read_raw (SNDFILE *sndfile, void *ptr, sf_count_t bytes)
25853 { SF_PRIVATE *psf ;
25854 sf_count_t count ;
25855 int bytewidth, blockwidth ;
25857 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
25859 bytewidth = (psf->bytewidth > 0) ? psf->bytewidth : 1 ;
25860 blockwidth = (psf->blockwidth > 0) ? psf->blockwidth : 1 ;
25862 if (psf->mode == SFM_WRITE)
25863 { psf->error = SFE_NOT_READMODE ;
25864 return 0 ;
25867 if (bytes < 0 || psf->read_current >= psf->datalength)
25868 { psf_memset (ptr, 0, bytes) ;
25869 return 0 ;
25872 if (bytes % (psf->sf.channels * bytewidth))
25873 { psf->error = SFE_BAD_READ_ALIGN ;
25874 return 0 ;
25877 count = psf_fread (ptr, 1, bytes, psf) ;
25879 if (count < bytes)
25880 psf_memset (((char*) ptr) + count, 0, bytes - count) ;
25882 psf->read_current += count / blockwidth ;
25884 psf->last_op = SFM_READ ;
25886 return count ;
25887 } /* sf_read_raw */
25889 /*------------------------------------------------------------------------------
25892 sf_count_t
25893 sf_read_short (SNDFILE *sndfile, short *ptr, sf_count_t len)
25894 { SF_PRIVATE *psf ;
25895 sf_count_t count, extra ;
25897 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
25899 if (psf->mode == SFM_WRITE)
25900 { psf->error = SFE_NOT_READMODE ;
25901 return 0 ;
25904 if (len % psf->sf.channels)
25905 { psf->error = SFE_BAD_READ_ALIGN ;
25906 return 0 ;
25909 if (len <= 0 || psf->read_current >= psf->sf.frames)
25910 { psf_memset (ptr, 0, len * sizeof (short)) ;
25911 return 0 ; /* End of file. */
25914 if (! psf->read_short || psf->seek == NULL)
25915 { psf->error = SFE_UNIMPLEMENTED ;
25916 return 0 ;
25919 if (psf->last_op != SFM_READ)
25920 if (psf->seek (psf, SFM_READ, psf->read_current) < 0)
25921 return 0 ;
25923 count = psf->read_short (psf, ptr, len) ;
25925 if (psf->read_current + count / psf->sf.channels > psf->sf.frames)
25926 { count = (psf->sf.frames - psf->read_current) * psf->sf.channels ;
25927 extra = len - count ;
25928 psf_memset (ptr + count, 0, extra * sizeof (short)) ;
25929 psf->read_current = psf->sf.frames ;
25932 psf->read_current += count / psf->sf.channels ;
25934 psf->last_op = SFM_READ ;
25936 if (psf->read_current > psf->sf.frames)
25937 { count = psf->sf.channels * (psf->read_current - psf->sf.frames) ;
25938 psf->read_current = psf->sf.frames ;
25941 return count ;
25942 } /* sf_read_short */
25944 sf_count_t
25945 sf_readf_short (SNDFILE *sndfile, short *ptr, sf_count_t frames)
25946 { SF_PRIVATE *psf ;
25947 sf_count_t count, extra ;
25949 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
25951 if (psf->mode == SFM_WRITE)
25952 { psf->error = SFE_NOT_READMODE ;
25953 return 0 ;
25956 if (frames <= 0 || psf->read_current >= psf->sf.frames)
25957 { psf_memset (ptr, 0, frames * psf->sf.channels * sizeof (short)) ;
25958 return 0 ; /* End of file. */
25961 if (! psf->read_short || psf->seek == NULL)
25962 { psf->error = SFE_UNIMPLEMENTED ;
25963 return 0 ;
25966 if (psf->last_op != SFM_READ)
25967 if (psf->seek (psf, SFM_READ, psf->read_current) < 0)
25968 return 0 ;
25970 count = psf->read_short (psf, ptr, frames * psf->sf.channels) ;
25972 if (psf->read_current + count / psf->sf.channels > psf->sf.frames)
25973 { count = (psf->sf.frames - psf->read_current) * psf->sf.channels ;
25974 extra = frames * psf->sf.channels - count ;
25975 psf_memset (ptr + count, 0, extra * sizeof (short)) ;
25976 psf->read_current = psf->sf.frames ;
25979 psf->read_current += count / psf->sf.channels ;
25981 psf->last_op = SFM_READ ;
25983 if (psf->read_current > psf->sf.frames)
25984 { count = psf->sf.channels * (psf->read_current - psf->sf.frames) ;
25985 psf->read_current = psf->sf.frames ;
25988 return count / psf->sf.channels ;
25989 } /* sf_readf_short */
25991 /*------------------------------------------------------------------------------
25994 sf_count_t
25995 sf_read_int (SNDFILE *sndfile, int *ptr, sf_count_t len)
25996 { SF_PRIVATE *psf ;
25997 sf_count_t count, extra ;
25999 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
26001 if (psf->mode == SFM_WRITE)
26002 { psf->error = SFE_NOT_READMODE ;
26003 return 0 ;
26006 if (len % psf->sf.channels)
26007 { psf->error = SFE_BAD_READ_ALIGN ;
26008 return 0 ;
26011 if (len <= 0 || psf->read_current >= psf->sf.frames)
26012 { psf_memset (ptr, 0, len * sizeof (int)) ;
26013 return 0 ;
26016 if (! psf->read_int || psf->seek == NULL)
26017 { psf->error = SFE_UNIMPLEMENTED ;
26018 return 0 ;
26021 if (psf->last_op != SFM_READ)
26022 if (psf->seek (psf, SFM_READ, psf->read_current) < 0)
26023 return 0 ;
26025 count = psf->read_int (psf, ptr, len) ;
26027 if (psf->read_current + count / psf->sf.channels > psf->sf.frames)
26028 { count = (psf->sf.frames - psf->read_current) * psf->sf.channels ;
26029 extra = len - count ;
26030 psf_memset (ptr + count, 0, extra * sizeof (int)) ;
26031 psf->read_current = psf->sf.frames ;
26034 psf->read_current += count / psf->sf.channels ;
26036 psf->last_op = SFM_READ ;
26038 if (psf->read_current > psf->sf.frames)
26039 { count = psf->sf.channels * (psf->read_current - psf->sf.frames) ;
26040 psf->read_current = psf->sf.frames ;
26043 return count ;
26044 } /* sf_read_int */
26046 sf_count_t
26047 sf_readf_int (SNDFILE *sndfile, int *ptr, sf_count_t frames)
26048 { SF_PRIVATE *psf ;
26049 sf_count_t count, extra ;
26051 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
26053 if (psf->mode == SFM_WRITE)
26054 { psf->error = SFE_NOT_READMODE ;
26055 return 0 ;
26058 if (frames <= 0 || psf->read_current >= psf->sf.frames)
26059 { psf_memset (ptr, 0, frames * psf->sf.channels * sizeof (int)) ;
26060 return 0 ;
26063 if (! psf->read_int || psf->seek == NULL)
26064 { psf->error = SFE_UNIMPLEMENTED ;
26065 return 0 ;
26068 if (psf->last_op != SFM_READ)
26069 if (psf->seek (psf, SFM_READ, psf->read_current) < 0)
26070 return 0 ;
26072 count = psf->read_int (psf, ptr, frames * psf->sf.channels) ;
26074 if (psf->read_current + count / psf->sf.channels > psf->sf.frames)
26075 { count = (psf->sf.frames - psf->read_current) * psf->sf.channels ;
26076 extra = frames * psf->sf.channels - count ;
26077 psf_memset (ptr + count, 0, extra * sizeof (int)) ;
26078 psf->read_current = psf->sf.frames ;
26081 psf->read_current += count / psf->sf.channels ;
26083 psf->last_op = SFM_READ ;
26085 if (psf->read_current > psf->sf.frames)
26086 { count = psf->sf.channels * (psf->read_current - psf->sf.frames) ;
26087 psf->read_current = psf->sf.frames ;
26090 return count / psf->sf.channels ;
26091 } /* sf_readf_int */
26093 /*------------------------------------------------------------------------------
26096 sf_count_t
26097 sf_read_float (SNDFILE *sndfile, float *ptr, sf_count_t len)
26098 { SF_PRIVATE *psf ;
26099 sf_count_t count, extra ;
26101 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
26103 if (psf->mode == SFM_WRITE)
26104 { psf->error = SFE_NOT_READMODE ;
26105 return 0 ;
26108 if (len % psf->sf.channels)
26109 { psf->error = SFE_BAD_READ_ALIGN ;
26110 return 0 ;
26113 if (len <= 0 || psf->read_current >= psf->sf.frames)
26114 { psf_memset (ptr, 0, len * sizeof (float)) ;
26115 return 0 ;
26118 if (! psf->read_float || psf->seek == NULL)
26119 { psf->error = SFE_UNIMPLEMENTED ;
26120 return 0 ;
26123 if (psf->last_op != SFM_READ)
26124 if (psf->seek (psf, SFM_READ, psf->read_current) < 0)
26125 return 0 ;
26127 count = psf->read_float (psf, ptr, len) ;
26129 if (psf->read_current + count / psf->sf.channels > psf->sf.frames)
26130 { count = (psf->sf.frames - psf->read_current) * psf->sf.channels ;
26131 extra = len - count ;
26132 psf_memset (ptr + count, 0, extra * sizeof (float)) ;
26133 psf->read_current = psf->sf.frames ;
26136 psf->read_current += count / psf->sf.channels ;
26138 psf->last_op = SFM_READ ;
26140 if (psf->read_current > psf->sf.frames)
26141 { count = psf->sf.channels * (psf->read_current - psf->sf.frames) ;
26142 psf->read_current = psf->sf.frames ;
26145 return count ;
26146 } /* sf_read_float */
26148 sf_count_t
26149 sf_readf_float (SNDFILE *sndfile, float *ptr, sf_count_t frames)
26150 { SF_PRIVATE *psf ;
26151 sf_count_t count, extra ;
26153 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
26155 if (psf->mode == SFM_WRITE)
26156 { psf->error = SFE_NOT_READMODE ;
26157 return 0 ;
26160 if (frames <= 0 || psf->read_current >= psf->sf.frames)
26161 { psf_memset (ptr, 0, frames * psf->sf.channels * sizeof (float)) ;
26162 return 0 ;
26165 if (! psf->read_float || psf->seek == NULL)
26166 { psf->error = SFE_UNIMPLEMENTED ;
26167 return 0 ;
26170 if (psf->last_op != SFM_READ)
26171 if (psf->seek (psf, SFM_READ, psf->read_current) < 0)
26172 return 0 ;
26174 count = psf->read_float (psf, ptr, frames * psf->sf.channels) ;
26176 if (psf->read_current + count / psf->sf.channels > psf->sf.frames)
26177 { count = (psf->sf.frames - psf->read_current) * psf->sf.channels ;
26178 extra = frames * psf->sf.channels - count ;
26179 psf_memset (ptr + count, 0, extra * sizeof (float)) ;
26180 psf->read_current = psf->sf.frames ;
26183 psf->read_current += count / psf->sf.channels ;
26185 psf->last_op = SFM_READ ;
26187 if (psf->read_current > psf->sf.frames)
26188 { count = psf->sf.channels * (psf->read_current - psf->sf.frames) ;
26189 psf->read_current = psf->sf.frames ;
26192 return count / psf->sf.channels ;
26193 } /* sf_readf_float */
26195 /*------------------------------------------------------------------------------
26198 sf_count_t
26199 sf_read_double (SNDFILE *sndfile, double *ptr, sf_count_t len)
26200 { SF_PRIVATE *psf ;
26201 sf_count_t count, extra ;
26203 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
26205 if (psf->mode == SFM_WRITE)
26206 { psf->error = SFE_NOT_READMODE ;
26207 return 0 ;
26210 if (len % psf->sf.channels)
26211 { psf->error = SFE_BAD_READ_ALIGN ;
26212 return 0 ;
26215 if (len <= 0 || psf->read_current >= psf->sf.frames)
26216 { psf_memset (ptr, 0, len * sizeof (double)) ;
26217 return 0 ;
26220 if (! psf->read_double || psf->seek == NULL)
26221 { psf->error = SFE_UNIMPLEMENTED ;
26222 return 0 ;
26225 if (psf->last_op != SFM_READ)
26226 if (psf->seek (psf, SFM_READ, psf->read_current) < 0)
26227 return 0 ;
26229 count = psf->read_double (psf, ptr, len) ;
26231 if (psf->read_current + count / psf->sf.channels > psf->sf.frames)
26232 { count = (psf->sf.frames - psf->read_current) * psf->sf.channels ;
26233 extra = len - count ;
26234 psf_memset (ptr + count, 0, extra * sizeof (double)) ;
26235 psf->read_current = psf->sf.frames ;
26238 psf->read_current += count / psf->sf.channels ;
26240 psf->last_op = SFM_READ ;
26242 if (psf->read_current > psf->sf.frames)
26243 { count = psf->sf.channels * (psf->read_current - psf->sf.frames) ;
26244 psf->read_current = psf->sf.frames ;
26247 return count ;
26248 } /* sf_read_double */
26250 sf_count_t
26251 sf_readf_double (SNDFILE *sndfile, double *ptr, sf_count_t frames)
26252 { SF_PRIVATE *psf ;
26253 sf_count_t count, extra ;
26255 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
26257 if (psf->mode == SFM_WRITE)
26258 { psf->error = SFE_NOT_READMODE ;
26259 return 0 ;
26262 if (frames <= 0 || psf->read_current >= psf->sf.frames)
26263 { psf_memset (ptr, 0, frames * psf->sf.channels * sizeof (double)) ;
26264 return 0 ;
26267 if (! psf->read_double || psf->seek == NULL)
26268 { psf->error = SFE_UNIMPLEMENTED ;
26269 return 0 ;
26272 if (psf->last_op != SFM_READ)
26273 if (psf->seek (psf, SFM_READ, psf->read_current) < 0)
26274 return 0 ;
26276 count = psf->read_double (psf, ptr, frames * psf->sf.channels) ;
26278 if (psf->read_current + count / psf->sf.channels > psf->sf.frames)
26279 { count = (psf->sf.frames - psf->read_current) * psf->sf.channels ;
26280 extra = frames * psf->sf.channels - count ;
26281 psf_memset (ptr + count, 0, extra * sizeof (double)) ;
26282 psf->read_current = psf->sf.frames ;
26285 psf->read_current += count / psf->sf.channels ;
26287 psf->last_op = SFM_READ ;
26289 if (psf->read_current > psf->sf.frames)
26290 { count = psf->sf.channels * (psf->read_current - psf->sf.frames) ;
26291 psf->read_current = psf->sf.frames ;
26294 return count / psf->sf.channels ;
26295 } /* sf_readf_double */
26297 /*------------------------------------------------------------------------------
26300 sf_count_t
26301 sf_write_raw (SNDFILE *sndfile, void *ptr, sf_count_t len)
26302 { SF_PRIVATE *psf ;
26303 sf_count_t count ;
26304 int bytewidth, blockwidth ;
26306 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
26308 bytewidth = (psf->bytewidth > 0) ? psf->bytewidth : 1 ;
26309 blockwidth = (psf->blockwidth > 0) ? psf->blockwidth : 1 ;
26311 if (psf->mode == SFM_READ)
26312 { psf->error = SFE_NOT_WRITEMODE ;
26313 return 0 ;
26316 if (len % (psf->sf.channels * bytewidth))
26317 { psf->error = SFE_BAD_WRITE_ALIGN ;
26318 return 0 ;
26321 if (psf->have_written == SF_FALSE && psf->write_header != NULL)
26322 psf->write_header (psf, SF_FALSE) ;
26323 psf->have_written = SF_TRUE ;
26325 count = psf_fwrite (ptr, 1, len, psf) ;
26327 psf->write_current += count / blockwidth ;
26329 if (psf->write_current > psf->sf.frames)
26330 psf->sf.frames = psf->write_current ;
26332 psf->last_op = SFM_WRITE ;
26334 return count ;
26335 } /* sf_write_raw */
26337 /*------------------------------------------------------------------------------
26340 sf_count_t
26341 sf_write_short (SNDFILE *sndfile, short *ptr, sf_count_t len)
26342 { SF_PRIVATE *psf ;
26343 sf_count_t count ;
26345 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
26347 if (psf->mode == SFM_READ)
26348 { psf->error = SFE_NOT_WRITEMODE ;
26349 return 0 ;
26352 if (len % psf->sf.channels)
26353 { psf->error = SFE_BAD_WRITE_ALIGN ;
26354 return 0 ;
26357 if (! psf->write_short || psf->seek == NULL)
26358 { psf->error = SFE_UNIMPLEMENTED ;
26359 return 0 ;
26362 if (psf->last_op != SFM_WRITE)
26363 if (psf->seek (psf, SFM_WRITE, psf->write_current) < 0)
26364 return 0 ;
26366 if (psf->have_written == SF_FALSE && psf->write_header != NULL)
26367 psf->write_header (psf, SF_FALSE) ;
26368 psf->have_written = SF_TRUE ;
26370 count = psf->write_short (psf, ptr, len) ;
26372 psf->write_current += count / psf->sf.channels ;
26374 psf->last_op = SFM_WRITE ;
26376 if (psf->auto_header)
26377 psf->write_header (psf, SF_TRUE) ;
26379 if (psf->write_current > psf->sf.frames)
26380 psf->sf.frames = psf->write_current ;
26382 return count ;
26383 } /* sf_write_short */
26385 sf_count_t
26386 sf_writef_short (SNDFILE *sndfile, short *ptr, sf_count_t frames)
26387 { SF_PRIVATE *psf ;
26388 sf_count_t count ;
26390 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
26392 if (psf->mode == SFM_READ)
26393 { psf->error = SFE_NOT_WRITEMODE ;
26394 return 0 ;
26397 if (! psf->write_short || psf->seek == NULL)
26398 { psf->error = SFE_UNIMPLEMENTED ;
26399 return 0 ;
26402 if (psf->last_op != SFM_WRITE)
26403 if (psf->seek (psf, SFM_WRITE, psf->write_current) < 0)
26404 return 0 ;
26406 if (psf->have_written == SF_FALSE && psf->write_header != NULL)
26407 psf->write_header (psf, SF_FALSE) ;
26408 psf->have_written = SF_TRUE ;
26410 count = psf->write_short (psf, ptr, frames * psf->sf.channels) ;
26412 psf->write_current += count / psf->sf.channels ;
26414 psf->last_op = SFM_WRITE ;
26416 if (psf->auto_header)
26417 psf->write_header (psf, SF_TRUE) ;
26419 if (psf->write_current > psf->sf.frames)
26420 psf->sf.frames = psf->write_current ;
26422 return count / psf->sf.channels ;
26423 } /* sf_writef_short */
26425 /*------------------------------------------------------------------------------
26428 sf_count_t
26429 sf_write_int (SNDFILE *sndfile, int *ptr, sf_count_t len)
26430 { SF_PRIVATE *psf ;
26431 sf_count_t count ;
26433 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
26435 if (psf->mode == SFM_READ)
26436 { psf->error = SFE_NOT_WRITEMODE ;
26437 return 0 ;
26440 if (len % psf->sf.channels)
26441 { psf->error = SFE_BAD_WRITE_ALIGN ;
26442 return 0 ;
26445 if (! psf->write_int || psf->seek == NULL)
26446 { psf->error = SFE_UNIMPLEMENTED ;
26447 return 0 ;
26450 if (psf->last_op != SFM_WRITE)
26451 if (psf->seek (psf, SFM_WRITE, psf->write_current) < 0)
26452 return 0 ;
26454 if (psf->have_written == SF_FALSE && psf->write_header != NULL)
26455 psf->write_header (psf, SF_FALSE) ;
26456 psf->have_written = SF_TRUE ;
26458 count = psf->write_int (psf, ptr, len) ;
26460 psf->write_current += count / psf->sf.channels ;
26462 psf->last_op = SFM_WRITE ;
26464 if (psf->auto_header)
26465 psf->write_header (psf, SF_TRUE) ;
26467 if (psf->write_current > psf->sf.frames)
26468 psf->sf.frames = psf->write_current ;
26470 return count ;
26471 } /* sf_write_int */
26473 sf_count_t
26474 sf_writef_int (SNDFILE *sndfile, int *ptr, sf_count_t frames)
26475 { SF_PRIVATE *psf ;
26476 sf_count_t count ;
26478 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
26480 if (psf->mode == SFM_READ)
26481 { psf->error = SFE_NOT_WRITEMODE ;
26482 return 0 ;
26485 if (! psf->write_int || psf->seek == NULL)
26486 { psf->error = SFE_UNIMPLEMENTED ;
26487 return 0 ;
26490 if (psf->last_op != SFM_WRITE)
26491 if (psf->seek (psf, SFM_WRITE, psf->write_current) < 0)
26492 return 0 ;
26494 if (psf->have_written == SF_FALSE && psf->write_header != NULL)
26495 psf->write_header (psf, SF_FALSE) ;
26496 psf->have_written = SF_TRUE ;
26498 count = psf->write_int (psf, ptr, frames * psf->sf.channels) ;
26500 psf->write_current += count / psf->sf.channels ;
26502 psf->last_op = SFM_WRITE ;
26504 if (psf->auto_header)
26505 psf->write_header (psf, SF_TRUE) ;
26507 if (psf->write_current > psf->sf.frames)
26508 psf->sf.frames = psf->write_current ;
26510 return count / psf->sf.channels ;
26511 } /* sf_writef_int */
26513 /*------------------------------------------------------------------------------
26516 sf_count_t
26517 sf_write_float (SNDFILE *sndfile, float *ptr, sf_count_t len)
26518 { SF_PRIVATE *psf ;
26519 sf_count_t count ;
26521 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
26523 if (psf->mode == SFM_READ)
26524 { psf->error = SFE_NOT_WRITEMODE ;
26525 return 0 ;
26528 if (len % psf->sf.channels)
26529 { psf->error = SFE_BAD_WRITE_ALIGN ;
26530 return 0 ;
26533 if (! psf->write_float || psf->seek == NULL)
26534 { psf->error = SFE_UNIMPLEMENTED ;
26535 return 0 ;
26538 if (psf->last_op != SFM_WRITE)
26539 if (psf->seek (psf, SFM_WRITE, psf->write_current) < 0)
26540 return 0 ;
26542 if (psf->have_written == SF_FALSE && psf->write_header != NULL)
26543 psf->write_header (psf, SF_FALSE) ;
26544 psf->have_written = SF_TRUE ;
26546 count = psf->write_float (psf, ptr, len) ;
26548 psf->write_current += count / psf->sf.channels ;
26550 psf->last_op = SFM_WRITE ;
26552 if (psf->auto_header)
26553 psf->write_header (psf, SF_TRUE) ;
26555 if (psf->write_current > psf->sf.frames)
26556 psf->sf.frames = psf->write_current ;
26558 return count ;
26559 } /* sf_write_float */
26561 sf_count_t
26562 sf_writef_float (SNDFILE *sndfile, float *ptr, sf_count_t frames)
26563 { SF_PRIVATE *psf ;
26564 sf_count_t count ;
26566 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
26568 if (psf->mode == SFM_READ)
26569 { psf->error = SFE_NOT_WRITEMODE ;
26570 return 0 ;
26573 if (! psf->write_float || psf->seek == NULL)
26574 { psf->error = SFE_UNIMPLEMENTED ;
26575 return 0 ;
26578 if (psf->last_op != SFM_WRITE)
26579 if (psf->seek (psf, SFM_WRITE, psf->write_current) < 0)
26580 return 0 ;
26582 if (psf->have_written == SF_FALSE && psf->write_header != NULL)
26583 psf->write_header (psf, SF_FALSE) ;
26584 psf->have_written = SF_TRUE ;
26586 count = psf->write_float (psf, ptr, frames * psf->sf.channels) ;
26588 psf->write_current += count / psf->sf.channels ;
26590 psf->last_op = SFM_WRITE ;
26592 if (psf->auto_header)
26593 psf->write_header (psf, SF_TRUE) ;
26595 if (psf->write_current > psf->sf.frames)
26596 psf->sf.frames = psf->write_current ;
26598 return count / psf->sf.channels ;
26599 } /* sf_writef_float */
26601 /*------------------------------------------------------------------------------
26604 sf_count_t
26605 sf_write_double (SNDFILE *sndfile, double *ptr, sf_count_t len)
26606 { SF_PRIVATE *psf ;
26607 sf_count_t count ;
26609 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
26611 if (psf->mode == SFM_READ)
26612 { psf->error = SFE_NOT_WRITEMODE ;
26613 return 0 ;
26616 if (len % psf->sf.channels)
26617 { psf->error = SFE_BAD_WRITE_ALIGN ;
26618 return 0 ;
26621 if (! psf->write_double || psf->seek == NULL)
26622 { psf->error = SFE_UNIMPLEMENTED ;
26623 return 0 ;
26626 if (psf->last_op != SFM_WRITE)
26627 if (psf->seek (psf, SFM_WRITE, psf->write_current) < 0)
26628 return 0 ;
26630 if (psf->have_written == SF_FALSE && psf->write_header != NULL)
26631 psf->write_header (psf, SF_FALSE) ;
26632 psf->have_written = SF_TRUE ;
26634 count = psf->write_double (psf, ptr, len) ;
26636 psf->write_current += count / psf->sf.channels ;
26638 psf->last_op = SFM_WRITE ;
26640 if (psf->auto_header)
26641 psf->write_header (psf, SF_TRUE) ;
26643 if (psf->write_current > psf->sf.frames)
26644 psf->sf.frames = psf->write_current ;
26646 return count ;
26647 } /* sf_write_double */
26649 sf_count_t
26650 sf_writef_double (SNDFILE *sndfile, double *ptr, sf_count_t frames)
26651 { SF_PRIVATE *psf ;
26652 sf_count_t count ;
26654 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
26656 if (psf->mode == SFM_READ)
26657 { psf->error = SFE_NOT_WRITEMODE ;
26658 return 0 ;
26661 if (! psf->write_double || psf->seek == NULL)
26662 { psf->error = SFE_UNIMPLEMENTED ;
26663 return 0 ;
26666 if (psf->last_op != SFM_WRITE)
26667 if (psf->seek (psf, SFM_WRITE, psf->write_current) < 0)
26668 return 0 ;
26670 if (psf->have_written == SF_FALSE && psf->write_header != NULL)
26671 psf->write_header (psf, SF_FALSE) ;
26672 psf->have_written = SF_TRUE ;
26674 count = psf->write_double (psf, ptr, frames * psf->sf.channels) ;
26676 psf->write_current += count / psf->sf.channels ;
26678 psf->last_op = SFM_WRITE ;
26680 if (psf->auto_header)
26681 psf->write_header (psf, SF_TRUE) ;
26683 if (psf->write_current > psf->sf.frames)
26684 psf->sf.frames = psf->write_current ;
26686 return count / psf->sf.channels ;
26687 } /* sf_writef_double */
26689 /*=========================================================================
26690 ** Private functions.
26693 static int
26694 format_from_extension (const char *filename)
26695 { char *cptr ;
26696 char buffer [16] ;
26698 if (filename == NULL)
26699 return 0 ;
26701 if ((cptr = strrchr (filename, '.')) == NULL)
26702 return 0 ;
26704 cptr ++ ;
26705 if (strlen (cptr) > sizeof (buffer) - 1)
26706 return 0 ;
26708 strncpy (buffer, cptr, sizeof (buffer)) ;
26709 buffer [sizeof (buffer) - 1] = 0 ;
26711 /* Convert everything in the buffer to lower case. */
26712 cptr = buffer ;
26713 while (*cptr)
26714 { *cptr = tolower (*cptr) ;
26715 cptr ++ ;
26718 cptr = buffer ;
26720 if (strcmp (cptr, "au") == 0)
26721 return SF_FORMAT_AU | SF_FORMAT_ULAW ;
26723 if (strcmp (cptr, "snd") == 0)
26724 return SF_FORMAT_AU | SF_FORMAT_ULAW ;
26726 if (strcmp (cptr, "vox") == 0)
26727 return SF_FORMAT_RAW | SF_FORMAT_VOX_ADPCM ;
26729 return 0 ;
26730 } /* format_from_extension */
26732 static int
26733 guess_file_type (SF_PRIVATE *psf, const char *filename)
26734 { int buffer [3], format ;
26735 unsigned char cptr [0x40] ;
26737 if (psf_binheader_readf (psf, "b", &buffer, SIGNED_SIZEOF (buffer)) != SIGNED_SIZEOF (buffer))
26738 { psf->error = SFE_BAD_FILE_READ ;
26739 return 0 ;
26742 if (buffer [0] == MAKE_MARKER ('R', 'I', 'F', 'F') && buffer [2] == MAKE_MARKER ('W', 'A', 'V', 'E'))
26743 return SF_FORMAT_WAV ;
26745 if (buffer [0] == MAKE_MARKER ('F', 'O', 'R', 'M'))
26746 { if (buffer [2] == MAKE_MARKER ('A', 'I', 'F', 'F') || buffer [2] == MAKE_MARKER ('A', 'I', 'F', 'C'))
26747 return SF_FORMAT_AIFF ;
26748 if (buffer [2] == MAKE_MARKER ('8', 'S', 'V', 'X') || buffer [2] == MAKE_MARKER ('1', '6', 'S', 'V'))
26749 return SF_FORMAT_SVX ;
26750 return 0 ;
26753 if (buffer [0] == MAKE_MARKER ('.', 's', 'n', 'd') || buffer [0] == MAKE_MARKER ('d', 'n', 's', '.'))
26754 return SF_FORMAT_AU ;
26756 if ((buffer [0] == MAKE_MARKER ('f', 'a', 'p', ' ') || buffer [0] == MAKE_MARKER (' ', 'p', 'a', 'f')))
26757 return SF_FORMAT_PAF ;
26759 if (buffer [0] == MAKE_MARKER ('N', 'I', 'S', 'T'))
26760 return SF_FORMAT_NIST ;
26762 if (buffer [0] == MAKE_MARKER ('C', 'r', 'e', 'a') && buffer [1] == MAKE_MARKER ('t', 'i', 'v', 'e'))
26763 return SF_FORMAT_VOC ;
26765 if ((buffer [0] & MAKE_MARKER (0xFF, 0xFF, 0xF8, 0xFF)) == MAKE_MARKER (0x64, 0xA3, 0x00, 0x00) ||
26766 (buffer [0] & MAKE_MARKER (0xFF, 0xF8, 0xFF, 0xFF)) == MAKE_MARKER (0x00, 0x00, 0xA3, 0x64))
26767 return SF_FORMAT_IRCAM ;
26769 if (buffer [0] == MAKE_MARKER ('r', 'i', 'f', 'f'))
26770 return SF_FORMAT_W64 ;
26772 if (buffer [0] == MAKE_MARKER (0, 0, 0x03, 0xE8) && buffer [1] == MAKE_MARKER (0, 0, 0, 1) &&
26773 buffer [2] == MAKE_MARKER (0, 0, 0, 1))
26774 return SF_FORMAT_MAT4 ;
26776 if (buffer [0] == MAKE_MARKER (0, 0, 0, 0) && buffer [1] == MAKE_MARKER (1, 0, 0, 0) &&
26777 buffer [2] == MAKE_MARKER (1, 0, 0, 0))
26778 return SF_FORMAT_MAT4 ;
26780 if (buffer [0] == MAKE_MARKER ('M', 'A', 'T', 'L') && buffer [1] == MAKE_MARKER ('A', 'B', ' ', '5'))
26781 return SF_FORMAT_MAT5 ;
26783 if (buffer [0] == MAKE_MARKER ('P', 'V', 'F', '1'))
26784 return SF_FORMAT_PVF ;
26786 if (buffer [0] == MAKE_MARKER ('E', 'x', 't', 'e') && buffer [1] == MAKE_MARKER ('n', 'd', 'e', 'd') &&
26787 buffer [2] == MAKE_MARKER (' ', 'I', 'n', 's'))
26788 return SF_FORMAT_XI ;
26790 if (ENABLE_EXPERIMENTAL_CODE && buffer [0] == MAKE_MARKER ('O', 'g', 'g', 'S'))
26791 return SF_FORMAT_OGG ;
26793 if (buffer [0] == MAKE_MARKER ('A', 'L', 'a', 'w') && buffer [1] == MAKE_MARKER ('S', 'o', 'u', 'n')
26794 && buffer [2] == MAKE_MARKER ('d', 'F', 'i', 'l'))
26795 return SF_FORMAT_WVE ;
26797 if (buffer [0] == MAKE_MARKER ('D', 'i', 'a', 'm') && buffer [1] == MAKE_MARKER ('o', 'n', 'd', 'W')
26798 && buffer [2] == MAKE_MARKER ('a', 'r', 'e', ' '))
26799 return SF_FORMAT_DWD ;
26801 if (buffer [0] == MAKE_MARKER ('L', 'M', '8', '9') || buffer [0] == MAKE_MARKER ('5', '3', 0, 0))
26802 return SF_FORMAT_TXW ;
26804 if ((buffer [0] & MAKE_MARKER (0xFF, 0xFF, 0x80, 0xFF)) == MAKE_MARKER (0xF0, 0x7E, 0, 0x01))
26805 return SF_FORMAT_SDS ;
26807 if (buffer [0] == MAKE_MARKER ('C', 'A', 'T', ' ') && buffer [2] == MAKE_MARKER ('R', 'E', 'X', '2'))
26808 return SF_FORMAT_REX2 ;
26810 if (buffer [0] == MAKE_MARKER (0x30, 0x26, 0xB2, 0x75) && buffer [1] == MAKE_MARKER (0x8E, 0x66, 0xCF, 0x11))
26811 return 0 /*-SF_FORMAT_WMA-*/ ;
26813 /* HMM (Hidden Markov Model) Tool Kit. */
26814 if (2 * BEI2H_INT (buffer [0]) + 12 == psf->filelength && buffer [2] == MAKE_MARKER (0, 2, 0, 0))
26815 return SF_FORMAT_HTK ;
26817 if (buffer [0] == MAKE_MARKER ('f', 'L', 'a', 'C'))
26818 return 0 /*-SF_FORMAT_FLAC-*/ ;
26820 /* Turtle Beach SMP 16-bit */
26821 if (buffer [0] == MAKE_MARKER ('S', 'O', 'U', 'N') && buffer [1] == MAKE_MARKER ('D', ' ', 'S', 'A'))
26822 return 0 ;
26824 if (buffer [0] == MAKE_MARKER ('S', 'Y', '8', '0') || buffer [0] == MAKE_MARKER ('S', 'Y', '8', '5'))
26825 return 0 ;
26827 if (buffer [0] == MAKE_MARKER ('a', 'j', 'k', 'g'))
26828 return 0 /*-SF_FORMAT_SHN-*/ ;
26830 if (buffer [0] == MAKE_MARKER ('2', 'B', 'I', 'T'))
26831 return SF_FORMAT_AVR ;
26833 if (OS_IS_MACOSX && (format = macos_guess_file_type (psf, filename)) != 0)
26834 return format ;
26836 /* Detect wacky MacOS header stuff. This might be "Sound Designer II". */
26837 memcpy (cptr , buffer, sizeof (buffer)) ;
26838 if (cptr [0] == 0 && cptr [1] > 0 && psf->sf.seekable)
26839 { psf_binheader_readf (psf, "pb", 0, &cptr, SIGNED_SIZEOF (cptr)) ;
26841 if (cptr [1] < (sizeof (cptr) - 3) && cptr [cptr [1] + 2] == 0 && strlen (((char*) cptr) + 2) == cptr [1])
26842 { psf_log_printf (psf, "Weird MacOS Header.\n") ;
26843 psf_binheader_readf (psf, "em", &buffer) ;
26844 if (buffer [0] == MAKE_MARKER (0, 'S', 'd', '2'))
26845 return SF_FORMAT_SD2 ;
26849 /* This must be the last one. */
26850 if ((format = format_from_extension (filename)) != 0)
26851 return format ;
26853 /* Default to header-less RAW PCM file type. */
26854 return SF_FORMAT_RAW ;
26855 } /* guess_file_type */
26858 static int
26859 validate_sfinfo (SF_INFO *sfinfo)
26860 { if (sfinfo->samplerate < 1)
26861 return 0 ;
26862 if (sfinfo->frames < 0)
26863 return 0 ;
26864 if (sfinfo->channels < 1)
26865 return 0 ;
26866 if ((sfinfo->format & SF_FORMAT_TYPEMASK) == 0)
26867 return 0 ;
26868 if ((sfinfo->format & SF_FORMAT_SUBMASK) == 0)
26869 return 0 ;
26870 if (sfinfo->sections < 1)
26871 return 0 ;
26872 return 1 ;
26873 } /* validate_sfinfo */
26875 static int
26876 validate_psf (SF_PRIVATE *psf)
26878 if (psf->datalength < 0)
26879 { psf_log_printf (psf, "Invalid SF_PRIVATE field : datalength == %D.\n", psf->datalength) ;
26880 return 0 ;
26882 if (psf->dataoffset < 0)
26883 { psf_log_printf (psf, "Invalid SF_PRIVATE field : dataoffset == %D.\n", psf->dataoffset) ;
26884 return 0 ;
26886 if (psf->blockwidth && psf->blockwidth != psf->sf.channels * psf->bytewidth)
26887 { psf_log_printf (psf, "Invalid SF_PRIVATE field : channels * bytewidth == %d.\n",
26888 psf->sf.channels * psf->bytewidth) ;
26889 return 0 ;
26891 return 1 ;
26892 } /* validate_psf */
26894 static void
26895 save_header_info (SF_PRIVATE *psf)
26896 { LSF_SNPRINTF (sf_logbuffer, sizeof (sf_logbuffer), "%s", psf->logbuffer) ;
26897 } /* save_header_info */
26899 static void
26900 copy_filename (SF_PRIVATE *psf, const char *path)
26901 { const char *cptr ;
26903 if ((cptr = strrchr (path, '/')) || (cptr = strrchr (path, '\\')))
26904 cptr ++ ;
26905 else
26906 cptr = path ;
26908 memset (psf->filename, 0, SF_FILENAME_LEN) ;
26910 LSF_SNPRINTF (psf->filename, sizeof (psf->filename), "%s", cptr) ;
26912 } /* copy_filename */
26914 /*==============================================================================
26917 static int
26918 psf_close (SF_PRIVATE *psf)
26919 { int error ;
26921 if (psf->close)
26922 error = psf->close (psf) ;
26924 psf_fclose (psf) ;
26926 if (psf->fdata)
26927 free (psf->fdata) ;
26929 if (psf->interleave)
26930 free (psf->interleave) ;
26932 if (psf->dither)
26933 free (psf->dither) ;
26935 if (psf->pchunk)
26936 free (psf->pchunk) ;
26938 if (psf->format_desc)
26939 { memset (psf->format_desc, 0, strlen (psf->format_desc)) ;
26940 free (psf->format_desc) ;
26943 memset (psf, 0, sizeof (SF_PRIVATE)) ;
26944 free (psf) ;
26946 return 0 ;
26947 } /* psf_close */
26949 static int
26950 psf_open_file (SF_PRIVATE *psf, int mode, SF_INFO *sfinfo)
26951 { int error, format ;
26953 if (mode != SFM_READ && mode != SFM_WRITE && mode != SFM_RDWR)
26954 return SFE_BAD_OPEN_MODE ;
26956 if (sfinfo == NULL)
26957 return SFE_BAD_SF_INFO_PTR ;
26959 if (mode == SFM_READ)
26960 { if ((sfinfo->format & SF_FORMAT_TYPEMASK) == SF_FORMAT_RAW)
26961 { if (sf_format_check (sfinfo) == 0)
26962 return SFE_RAW_BAD_FORMAT ;
26964 else
26965 memset (sfinfo, 0, sizeof (SF_INFO)) ;
26968 sf_errno = error = 0 ;
26969 sf_logbuffer [0] = 0 ;
26971 memcpy (&(psf->sf), sfinfo, sizeof (SF_INFO)) ;
26973 psf->Magick = SNDFILE_MAGICK ;
26974 psf->norm_float = SF_TRUE ;
26975 psf->norm_double = SF_TRUE ;
26976 psf->mode = mode ;
26977 psf->dataoffset = -1 ;
26978 psf->datalength = -1 ;
26979 psf->read_current = -1 ;
26980 psf->write_current = -1 ;
26981 psf->auto_header = SF_FALSE ;
26982 psf->rwf_endian = SF_ENDIAN_LITTLE ;
26983 psf->seek = psf_default_seek ;
26985 psf->sf.sections = 1 ;
26987 psf->is_pipe = psf_is_pipe (psf) ;
26989 if (psf->is_pipe)
26990 { psf->sf.seekable = SF_FALSE ;
26991 psf->filelength = SF_COUNT_MAX ;
26993 else
26994 { psf->sf.seekable = SF_TRUE ;
26996 /* File is open, so get the length. */
26997 psf->filelength = psf_get_filelen (psf) ;
27000 if (psf->fileoffset > 0)
27001 { switch (psf->mode)
27002 { case SFM_READ :
27003 if (psf->filelength < 44)
27004 { psf_log_printf (psf, "Short filelength: %D (fileoffset: %D)\n", psf->filelength, psf->fileoffset) ;
27005 return SFE_BAD_OFFSET ;
27007 break ;
27009 case SFM_WRITE :
27010 psf->fileoffset = 0 ;
27011 psf_fseek (psf, 0, SEEK_END) ;
27012 psf->fileoffset = psf_ftell (psf) ;
27013 break ;
27015 case SFM_RDWR :
27016 return SFE_NO_EMBEDDED_RDWR ;
27019 psf_log_printf (psf, "Embedded file offset : %D\n", psf->fileoffset) ;
27022 if (psf->filelength == SF_COUNT_MAX)
27023 psf_log_printf (psf, "Length : unknown\n") ;
27024 else
27025 psf_log_printf (psf, "Length : %D\n", psf->filelength) ;
27027 if (mode == SFM_WRITE || (mode == SFM_RDWR && psf->filelength == 0))
27028 { /* If the file is being opened for write or RDWR and the file is currently
27029 ** empty, then the SF_INFO struct must contain valid data.
27031 if (sf_format_check (&(psf->sf)) == 0)
27032 return SFE_BAD_OPEN_FORMAT ;
27034 else if ((psf->sf.format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
27035 { /* If type RAW has not been specified then need to figure out file type. */
27036 psf->sf.format = guess_file_type (psf, psf->filename) ;
27039 /* Prevent unnecessary seeks */
27040 psf->last_op = psf->mode ;
27042 /* Set bytewidth if known. */
27043 switch (psf->sf.format & SF_FORMAT_SUBMASK)
27044 { case SF_FORMAT_PCM_S8 :
27045 case SF_FORMAT_PCM_U8 :
27046 case SF_FORMAT_ULAW :
27047 case SF_FORMAT_ALAW :
27048 case SF_FORMAT_DPCM_8 :
27049 psf->bytewidth = 1 ;
27050 break ;
27052 case SF_FORMAT_PCM_16 :
27053 case SF_FORMAT_DPCM_16 :
27054 psf->bytewidth = 2 ;
27055 break ;
27057 case SF_FORMAT_PCM_24 :
27058 psf->bytewidth = 3 ;
27059 break ;
27061 case SF_FORMAT_PCM_32 :
27062 case SF_FORMAT_FLOAT :
27063 psf->bytewidth = 4 ;
27064 break ;
27066 case SF_FORMAT_DOUBLE :
27067 psf->bytewidth = 8 ;
27068 break ;
27071 /* Call the initialisation function for the relevant file type. */
27072 switch (psf->sf.format & SF_FORMAT_TYPEMASK)
27073 { case SF_FORMAT_WAV :
27074 case SF_FORMAT_WAVEX :
27075 error = wav_open (psf) ;
27076 break ;
27078 case SF_FORMAT_AIFF :
27079 error = aiff_open (psf) ;
27080 break ;
27082 case SF_FORMAT_AU :
27083 error = au_open (psf) ;
27084 break ;
27086 case SF_FORMAT_AU | SF_FORMAT_ULAW :
27087 error = au_nh_open (psf) ;
27088 break ;
27090 case SF_FORMAT_RAW :
27091 error = raw_open (psf) ;
27092 break ;
27094 case SF_FORMAT_W64 :
27095 error = w64_open (psf) ;
27096 break ;
27098 /* Lite remove start */
27099 case SF_FORMAT_PAF :
27100 error = paf_open (psf) ;
27101 break ;
27103 case SF_FORMAT_SVX :
27104 error = svx_open (psf) ;
27105 break ;
27107 case SF_FORMAT_NIST :
27108 error = nist_open (psf) ;
27109 break ;
27111 case SF_FORMAT_IRCAM :
27112 error = ircam_open (psf) ;
27113 break ;
27115 case SF_FORMAT_VOC :
27116 error = voc_open (psf) ;
27117 break ;
27119 case SF_FORMAT_SDS :
27120 error = sds_open (psf) ;
27121 break ;
27123 case SF_FORMAT_OGG :
27124 error = ogg_open (psf) ;
27125 break ;
27127 case SF_FORMAT_TXW :
27128 error = txw_open (psf) ;
27129 break ;
27131 case SF_FORMAT_WVE :
27132 error = wve_open (psf) ;
27133 break ;
27135 case SF_FORMAT_DWD :
27136 error = dwd_open (psf) ;
27137 break ;
27139 case SF_FORMAT_MAT4 :
27140 error = mat4_open (psf) ;
27141 break ;
27143 case SF_FORMAT_MAT5 :
27144 error = mat5_open (psf) ;
27145 break ;
27147 case SF_FORMAT_PVF :
27148 error = pvf_open (psf) ;
27149 break ;
27151 case SF_FORMAT_XI :
27152 error = xi_open (psf) ;
27153 break ;
27155 case SF_FORMAT_HTK :
27156 error = htk_open (psf) ;
27157 break ;
27159 case SF_FORMAT_SD2 :
27160 error = sd2_open (psf) ;
27161 break ;
27163 case SF_FORMAT_REX2 :
27164 error = rx2_open (psf) ;
27165 break ;
27167 case SF_FORMAT_AVR :
27168 error = avr_open (psf) ;
27169 break ;
27171 /* Lite remove end */
27173 default :
27174 error = SFE_UNKNOWN_FORMAT ;
27177 if (error)
27178 return error ;
27180 /* For now, check whether embedding is supported. */
27181 format = psf->sf.format & SF_FORMAT_TYPEMASK ;
27182 if (psf->fileoffset > 0 &&
27183 (format != SF_FORMAT_WAV) && (format != SF_FORMAT_WAVEX) &&
27184 (format != SF_FORMAT_AIFF) && (format != SF_FORMAT_AU)
27186 return SFE_NO_EMBED_SUPPORT ;
27188 if (psf->fileoffset > 0)
27189 psf_log_printf (psf, "Embedded file length : %D\n", psf->filelength) ;
27191 if (mode == SFM_RDWR && sf_format_check (&(psf->sf)) == 0)
27192 return SFE_BAD_RDWR_FORMAT ;
27194 if (validate_sfinfo (&(psf->sf)) == 0)
27195 { psf_log_SF_INFO (psf) ;
27196 save_header_info (psf) ;
27197 return SFE_BAD_SF_INFO ;
27200 if (validate_psf (psf) == 0)
27201 { save_header_info (psf) ;
27202 return SFE_INTERNAL ;
27205 psf->read_current = 0 ;
27206 psf->write_current = (psf->mode == SFM_RDWR) ? psf->sf.frames : 0 ;
27208 memcpy (sfinfo, &(psf->sf), sizeof (SF_INFO)) ;
27210 return 0 ;
27211 } /* psf_open_file */
27214 ** Do not edit or modify anything in this comment block.
27215 ** The arch-tag line is a file identity tag for the GNU Arch
27216 ** revision control system.
27218 ** arch-tag: cd4f9e91-a8ec-4154-9bf6-fe4b8c69a615
27221 ** Copyright (C) 2001-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
27223 ** This program is free software; you can redistribute it and/or modify
27224 ** it under the terms of the GNU Lesser General Public License as published by
27225 ** the Free Software Foundation; either version 2.1 of the License, or
27226 ** (at your option) any later version.
27228 ** This program is distributed in the hope that it will be useful,
27229 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
27230 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27231 ** GNU Lesser General Public License for more details.
27233 ** You should have received a copy of the GNU Lesser General Public License
27234 ** along with this program; if not, write to the Free Software
27235 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27238 #include <stdio.h>
27239 #include <string.h>
27240 #include <math.h>
27243 #define STRINGS_DEBUG 0
27244 #if STRINGS_DEBUG
27245 static void hexdump (void *data, int len) ;
27246 #endif
27249 psf_store_string (SF_PRIVATE *psf, int str_type, const char *str)
27250 { static char lsf_name [] = PACKAGE "-" VERSION ;
27251 static char bracket_name [] = " (" PACKAGE "-" VERSION ")" ;
27252 int k, str_len, len_remaining, str_flags ;
27254 if (str == NULL)
27255 return SFE_STR_BAD_STRING ;
27257 str_len = strlen (str) ;
27259 /* A few extra checks for write mode. */
27260 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
27261 { if ((psf->str_flags & SF_STR_ALLOW_START) == 0)
27262 return SFE_STR_NO_SUPPORT ;
27263 if ((psf->str_flags & SF_STR_ALLOW_END) == 0)
27264 return SFE_STR_NO_SUPPORT ;
27265 /* Only allow zero length strings for software. */
27266 if (str_type != SF_STR_SOFTWARE && str_len == 0)
27267 return SFE_STR_BAD_STRING ;
27270 /* Determine flags */
27271 str_flags = SF_STR_LOCATE_START ;
27272 if (psf->have_written)
27273 { if ((psf->str_flags & SF_STR_ALLOW_END) == 0)
27274 return SFE_STR_NO_ADD_END ;
27275 str_flags = SF_STR_LOCATE_END ;
27278 /* Find next free slot in table. */
27279 for (k = 0 ; k < SF_MAX_STRINGS ; k++)
27280 if (psf->strings [k].type == 0)
27281 break ;
27283 /* More sanity checking. */
27284 if (k >= SF_MAX_STRINGS)
27285 return SFE_STR_MAX_COUNT ;
27287 if (k == 0 && psf->str_end != NULL)
27288 { psf_log_printf (psf, "SFE_STR_WEIRD : k == 0 && psf->str_end != NULL\n") ;
27289 return SFE_STR_WEIRD ;
27292 if (k != 0 && psf->str_end == NULL)
27293 { psf_log_printf (psf, "SFE_STR_WEIRD : k != 0 && psf->str_end == NULL\n") ;
27294 return SFE_STR_WEIRD ;
27297 /* Special case for the first string. */
27298 if (k == 0)
27299 psf->str_end = psf->str_storage ;
27302 #if STRINGS_DEBUG
27303 psf_log_printf (psf, "str_storage : %X\n", (int) psf->str_storage) ;
27304 psf_log_printf (psf, "str_end : %X\n", (int) psf->str_end) ;
27305 psf_log_printf (psf, "sizeof (storage) : %d\n", SIGNED_SIZEOF (psf->str_storage)) ;
27306 #endif
27308 len_remaining = SIGNED_SIZEOF (psf->str_storage) - (psf->str_end - psf->str_storage) ;
27310 if (len_remaining < str_len + 2)
27311 return SFE_STR_MAX_DATA ;
27313 switch (str_type)
27314 { case SF_STR_SOFTWARE :
27315 /* In write mode, want to append libsndfile-version to string. */
27316 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
27317 { psf->strings [k].type = str_type ;
27318 psf->strings [k].str = psf->str_end ;
27319 psf->strings [k].flags = str_flags ;
27321 memcpy (psf->str_end, str, str_len + 1) ;
27322 psf->str_end += str_len ;
27325 ** If the supplied string does not already contain a
27326 ** libsndfile-X.Y.Z component, then add it.
27328 if (strstr (str, PACKAGE) == NULL && len_remaining > (int) (strlen (bracket_name) + str_len + 2))
27329 { if (strlen (str) == 0)
27330 strncat (psf->str_end, lsf_name, len_remaining) ;
27331 else
27332 strncat (psf->str_end, bracket_name, len_remaining) ;
27333 psf->str_end += strlen (psf->str_end) ;
27336 /* Plus one to catch string terminator. */
27337 psf->str_end += 1 ;
27338 break ;
27341 /* Fall though if not write mode. */
27343 case SF_STR_TITLE :
27344 case SF_STR_COPYRIGHT :
27345 case SF_STR_ARTIST :
27346 case SF_STR_COMMENT :
27347 case SF_STR_DATE :
27348 psf->strings [k].type = str_type ;
27349 psf->strings [k].str = psf->str_end ;
27350 psf->strings [k].flags = str_flags ;
27352 /* Plus one to catch string terminator. */
27353 memcpy (psf->str_end, str, str_len + 1) ;
27354 psf->str_end += str_len + 1 ;
27355 break ;
27357 default :
27358 return SFE_STR_BAD_TYPE ;
27361 psf->str_flags |= (psf->have_written) ? SF_STR_LOCATE_END : SF_STR_LOCATE_START ;
27363 #if STRINGS_DEBUG
27364 hexdump (psf->str_storage, 300) ;
27365 #endif
27367 return 0 ;
27368 } /* psf_store_string */
27370 const char*
27371 psf_get_string (SF_PRIVATE *psf, int str_type)
27372 { int k ;
27374 for (k = 0 ; k < SF_MAX_STRINGS ; k++)
27375 if (str_type == psf->strings [k].type)
27376 return psf->strings [k].str ;
27378 return NULL ;
27379 } /* psf_get_string */
27381 #if STRINGS_DEBUG
27383 #include <ctype.h>
27384 static void
27385 hexdump (void *data, int len)
27386 { unsigned char *ptr ;
27387 int k ;
27389 ptr = data ;
27391 puts ("---------------------------------------------------------") ;
27392 while (len >= 16)
27393 { for (k = 0 ; k < 16 ; k++)
27394 printf ("%02X ", ptr [k] & 0xFF) ;
27395 printf (" ") ;
27396 for (k = 0 ; k < 16 ; k++)
27397 printf ("%c", isprint (ptr [k]) ? ptr [k] : '.') ;
27398 puts ("") ;
27399 ptr += 16 ;
27400 len -= 16 ;
27402 } /* hexdump */
27404 #endif
27406 ** Do not edit or modify anything in this comment block.
27407 ** The arch-tag line is a file identity tag for the GNU Arch
27408 ** revision control system.
27410 ** arch-tag: 04393aa1-9389-46fe-baf2-58a7bd544fd6
27413 ** Copyright (C) 1999-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
27415 ** This program is free software; you can redistribute it and/or modify
27416 ** it under the terms of the GNU Lesser General Public License as published by
27417 ** the Free Software Foundation; either version 2.1 of the License, or
27418 ** (at your option) any later version.
27420 ** This program is distributed in the hope that it will be useful,
27421 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
27422 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27423 ** GNU Lesser General Public License for more details.
27425 ** You should have received a copy of the GNU Lesser General Public License
27426 ** along with this program; if not, write to the Free Software
27427 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27431 #include <stdio.h>
27432 #include <string.h>
27433 #include <ctype.h>
27434 #include <stdarg.h>
27438 /*------------------------------------------------------------------------------
27439 * Macros to handle big/little endian issues.
27442 #define FORM_MARKER (MAKE_MARKER ('F', 'O', 'R', 'M'))
27443 #define SVX8_MARKER (MAKE_MARKER ('8', 'S', 'V', 'X'))
27444 #define SV16_MARKER (MAKE_MARKER ('1', '6', 'S', 'V'))
27445 #define VHDR_MARKER (MAKE_MARKER ('V', 'H', 'D', 'R'))
27446 #define BODY_MARKER (MAKE_MARKER ('B', 'O', 'D', 'Y'))
27448 #define ATAK_MARKER (MAKE_MARKER ('A', 'T', 'A', 'K'))
27449 #define RLSE_MARKER (MAKE_MARKER ('R', 'L', 'S', 'E'))
27451 #define c_MARKER (MAKE_MARKER ('(', 'c', ')', ' '))
27452 #define NAME_MARKER (MAKE_MARKER ('N', 'A', 'M', 'E'))
27453 #define AUTH_MARKER (MAKE_MARKER ('A', 'U', 'T', 'H'))
27454 #define ANNO_MARKER (MAKE_MARKER ('A', 'N', 'N', 'O'))
27455 #define CHAN_MARKER (MAKE_MARKER ('C', 'H', 'A', 'N'))
27457 /*------------------------------------------------------------------------------
27458 * Typedefs for file chunks.
27461 typedef struct
27462 { unsigned int oneShotHiSamples, repeatHiSamples, samplesPerHiCycle ;
27463 unsigned short samplesPerSec ;
27464 unsigned char octave, compression ;
27465 unsigned int volume ;
27466 } VHDR_CHUNK ;
27468 enum {
27469 svxHAVE_FORM = 0x01,
27471 HAVE_SVX = 0x02,
27472 HAVE_VHDR = 0x04,
27473 HAVE_BODY = 0x08
27476 /*------------------------------------------------------------------------------
27477 * Private static functions.
27480 static int svx_close (SF_PRIVATE *psf) ;
27481 static int svx_write_header (SF_PRIVATE *psf, int calc_length) ;
27482 static int svx_read_header (SF_PRIVATE *psf) ;
27484 /*------------------------------------------------------------------------------
27485 ** Public function.
27489 svx_open (SF_PRIVATE *psf)
27490 { int error, subformat ;
27492 if (psf->mode == SFM_READ || (psf->mode == SFM_RDWR && psf->filelength > 0))
27493 { if ((error = svx_read_header (psf)))
27494 return error ;
27496 psf->endian = SF_ENDIAN_BIG ; /* All SVX files are big endian. */
27498 psf->blockwidth = psf->sf.channels * psf->bytewidth ;
27499 if (psf->blockwidth)
27500 psf->sf.frames = psf->datalength / psf->blockwidth ;
27502 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
27505 subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
27507 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
27508 { if (psf->is_pipe)
27509 return SFE_NO_PIPE_WRITE ;
27511 if ((psf->sf.format & SF_FORMAT_TYPEMASK) != SF_FORMAT_SVX)
27512 return SFE_BAD_OPEN_FORMAT ;
27514 psf->endian = psf->sf.format & SF_FORMAT_ENDMASK ;
27516 if (psf->endian == SF_ENDIAN_LITTLE || (CPU_IS_LITTLE_ENDIAN && psf->endian == SF_ENDIAN_CPU))
27517 return SFE_BAD_ENDIAN ;
27519 psf->endian = SF_ENDIAN_BIG ; /* All SVX files are big endian. */
27521 error = svx_write_header (psf, SF_FALSE) ;
27522 if (error)
27523 return error ;
27525 psf->write_header = svx_write_header ;
27528 psf->close = svx_close ;
27530 if ((error = pcm_init (psf)))
27531 return error ;
27533 return 0 ;
27534 } /* svx_open */
27536 /*------------------------------------------------------------------------------
27539 static int
27540 svx_read_header (SF_PRIVATE *psf)
27541 { VHDR_CHUNK vhdr ;
27542 unsigned int FORMsize, vhdrsize, dword, marker ;
27543 int filetype = 0, parsestage = 0, done = 0 ;
27544 int bytecount = 0, channels ;
27546 psf_binheader_readf (psf, "p", 0) ;
27548 /* Set default number of channels. */
27549 psf->sf.channels = 1 ;
27551 psf->sf.format = SF_FORMAT_SVX ;
27553 while (! done)
27554 { psf_binheader_readf (psf, "m", &marker) ;
27555 switch (marker)
27556 { case FORM_MARKER :
27557 if (parsestage)
27558 return SFE_SVX_NO_FORM ;
27560 psf_binheader_readf (psf, "E4", &FORMsize) ;
27562 if (FORMsize != psf->filelength - 2 * sizeof (dword))
27563 { dword = psf->filelength - 2 * sizeof (dword) ;
27564 psf_log_printf (psf, "FORM : %d (should be %d)\n", FORMsize, dword) ;
27565 FORMsize = dword ;
27567 else
27568 psf_log_printf (psf, "FORM : %d\n", FORMsize) ;
27569 parsestage |= svxHAVE_FORM ;
27570 break ;
27572 case SVX8_MARKER :
27573 case SV16_MARKER :
27574 if (! (parsestage & svxHAVE_FORM))
27575 return SFE_SVX_NO_FORM ;
27576 filetype = marker ;
27577 psf_log_printf (psf, " %M\n", marker) ;
27578 parsestage |= HAVE_SVX ;
27579 break ;
27581 case VHDR_MARKER :
27582 if (! (parsestage & (svxHAVE_FORM | HAVE_SVX)))
27583 return SFE_SVX_NO_FORM ;
27585 psf_binheader_readf (psf, "E4", &vhdrsize) ;
27587 psf_log_printf (psf, " VHDR : %d\n", vhdrsize) ;
27589 psf_binheader_readf (psf, "E4442114", &(vhdr.oneShotHiSamples), &(vhdr.repeatHiSamples),
27590 &(vhdr.samplesPerHiCycle), &(vhdr.samplesPerSec), &(vhdr.octave), &(vhdr.compression),
27591 &(vhdr.volume)) ;
27593 psf_log_printf (psf, " OneShotHiSamples : %d\n", vhdr.oneShotHiSamples) ;
27594 psf_log_printf (psf, " RepeatHiSamples : %d\n", vhdr.repeatHiSamples) ;
27595 psf_log_printf (psf, " samplesPerHiCycle : %d\n", vhdr.samplesPerHiCycle) ;
27596 psf_log_printf (psf, " Sample Rate : %d\n", vhdr.samplesPerSec) ;
27597 psf_log_printf (psf, " Octave : %d\n", vhdr.octave) ;
27599 psf_log_printf (psf, " Compression : %d => ", vhdr.compression) ;
27601 switch (vhdr.compression)
27602 { case 0 : psf_log_printf (psf, "None.\n") ;
27603 break ;
27604 case 1 : psf_log_printf (psf, "Fibonacci delta\n") ;
27605 break ;
27606 case 2 : psf_log_printf (psf, "Exponential delta\n") ;
27607 break ;
27610 psf_log_printf (psf, " Volume : %d\n", vhdr.volume) ;
27612 psf->sf.samplerate = vhdr.samplesPerSec ;
27614 if (filetype == SVX8_MARKER)
27615 { psf->sf.format |= SF_FORMAT_PCM_S8 ;
27616 psf->bytewidth = 1 ;
27618 else if (filetype == SV16_MARKER)
27619 { psf->sf.format |= SF_FORMAT_PCM_16 ;
27620 psf->bytewidth = 2 ;
27623 parsestage |= HAVE_VHDR ;
27624 break ;
27626 case BODY_MARKER :
27627 if (! (parsestage & HAVE_VHDR))
27628 return SFE_SVX_NO_BODY ;
27630 psf_binheader_readf (psf, "E4", &dword) ;
27631 psf->datalength = dword ;
27633 psf->dataoffset = psf_ftell (psf) ;
27635 if (psf->datalength > psf->filelength - psf->dataoffset)
27636 { psf_log_printf (psf, " BODY : %D (should be %D)\n", psf->datalength, psf->filelength - psf->dataoffset) ;
27637 psf->datalength = psf->filelength - psf->dataoffset ;
27639 else
27640 psf_log_printf (psf, " BODY : %D\n", psf->datalength) ;
27642 parsestage |= HAVE_BODY ;
27644 if (! psf->sf.seekable)
27645 break ;
27647 psf_fseek (psf, psf->datalength, SEEK_CUR) ;
27648 break ;
27650 case NAME_MARKER :
27651 if (! (parsestage & HAVE_SVX))
27652 return SFE_SVX_NO_FORM ;
27654 psf_binheader_readf (psf, "E4", &dword) ;
27656 psf_log_printf (psf, " %M : %d\n", marker, dword) ;
27658 if (strlen (psf->filename) != dword)
27659 { if (dword > sizeof (psf->filename) - 1)
27660 return SFE_SVX_BAD_NAME_LENGTH ;
27662 psf_binheader_readf (psf, "b", psf->filename, dword) ;
27663 psf->filename [dword] = 0 ;
27665 else
27666 psf_binheader_readf (psf, "j", dword) ;
27667 break ;
27669 case ANNO_MARKER :
27670 if (! (parsestage & HAVE_SVX))
27671 return SFE_SVX_NO_FORM ;
27673 psf_binheader_readf (psf, "E4", &dword) ;
27675 psf_log_printf (psf, " %M : %d\n", marker, dword) ;
27677 psf_binheader_readf (psf, "j", dword) ;
27678 break ;
27680 case CHAN_MARKER :
27681 if (! (parsestage & HAVE_SVX))
27682 return SFE_SVX_NO_FORM ;
27684 psf_binheader_readf (psf, "E4", &dword) ;
27686 psf_log_printf (psf, " %M : %d\n", marker, dword) ;
27688 bytecount += psf_binheader_readf (psf, "E4", &channels) ;
27690 psf_log_printf (psf, " Channels : %d => %d\n", channels) ;
27692 psf_binheader_readf (psf, "j", dword - bytecount) ;
27693 break ;
27696 case AUTH_MARKER :
27697 case c_MARKER :
27698 if (! (parsestage & HAVE_SVX))
27699 return SFE_SVX_NO_FORM ;
27701 psf_binheader_readf (psf, "E4", &dword) ;
27703 psf_log_printf (psf, " %M : %d\n", marker, dword) ;
27705 psf_binheader_readf (psf, "j", dword) ;
27706 break ;
27708 default :
27709 if (isprint ((marker >> 24) & 0xFF) && isprint ((marker >> 16) & 0xFF)
27710 && isprint ((marker >> 8) & 0xFF) && isprint (marker & 0xFF))
27711 { psf_binheader_readf (psf, "E4", &dword) ;
27713 psf_log_printf (psf, "%M : %d (unknown marker)\n", marker, dword) ;
27715 psf_binheader_readf (psf, "j", dword) ;
27716 break ;
27718 if ((dword = psf_ftell (psf)) & 0x03)
27719 { psf_log_printf (psf, " Unknown chunk marker at position %d. Resynching.\n", dword - 4) ;
27721 psf_binheader_readf (psf, "j", -3) ;
27722 break ;
27724 psf_log_printf (psf, "*** Unknown chunk marker : %X. Exiting parser.\n", marker) ;
27725 done = 1 ;
27726 } ; /* switch (marker) */
27728 if (! psf->sf.seekable && (parsestage & HAVE_BODY))
27729 break ;
27731 if (psf_ftell (psf) >= psf->filelength - SIGNED_SIZEOF (dword))
27732 break ;
27733 } ; /* while (1) */
27735 if (vhdr.compression)
27736 return SFE_SVX_BAD_COMP ;
27738 if (psf->dataoffset <= 0)
27739 return SFE_SVX_NO_DATA ;
27741 return 0 ;
27742 } /* svx_read_header */
27744 static int
27745 svx_close (SF_PRIVATE *psf)
27747 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
27748 svx_write_header (psf, SF_TRUE) ;
27750 return 0 ;
27751 } /* svx_close */
27753 static int
27754 svx_write_header (SF_PRIVATE *psf, int calc_length)
27755 { static char annotation [] = "libsndfile by Erik de Castro Lopo\0\0\0" ;
27756 sf_count_t current ;
27758 current = psf_ftell (psf) ;
27760 if (calc_length)
27761 { psf->filelength = psf_get_filelen (psf) ;
27763 psf->datalength = psf->filelength - psf->dataoffset ;
27765 if (psf->dataend)
27766 psf->datalength -= psf->filelength - psf->dataend ;
27768 psf->sf.frames = psf->datalength / (psf->bytewidth * psf->sf.channels) ;
27771 psf->header [0] = 0 ;
27772 psf->headindex = 0 ;
27773 psf_fseek (psf, 0, SEEK_SET) ;
27775 /* FORM marker and FORM size. */
27776 psf_binheader_writef (psf, "Etm8", FORM_MARKER, (psf->filelength < 8) ?
27777 psf->filelength * 0 : psf->filelength - 8) ;
27779 psf_binheader_writef (psf, "m", (psf->bytewidth == 1) ? SVX8_MARKER : SV16_MARKER) ;
27781 /* VHDR chunk. */
27782 psf_binheader_writef (psf, "Em4", VHDR_MARKER, sizeof (VHDR_CHUNK)) ;
27783 /* VHDR : oneShotHiSamples, repeatHiSamples, samplesPerHiCycle */
27784 psf_binheader_writef (psf, "E444", psf->sf.frames, 0, 0) ;
27785 /* VHDR : samplesPerSec, octave, compression */
27786 psf_binheader_writef (psf, "E211", psf->sf.samplerate, 1, 0) ;
27787 /* VHDR : volume */
27788 psf_binheader_writef (psf, "E4", (psf->bytewidth == 1) ? 0xFF : 0xFFFF) ;
27790 /* Filename and annotation strings. */
27791 psf_binheader_writef (psf, "Emsms", NAME_MARKER, psf->filename, ANNO_MARKER, annotation) ;
27793 /* BODY marker and size. */
27794 psf_binheader_writef (psf, "Etm8", BODY_MARKER, (psf->datalength < 0) ?
27795 psf->datalength * 0 : psf->datalength) ;
27797 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
27799 if (psf->error)
27800 return psf->error ;
27802 psf->dataoffset = psf->headindex ;
27804 if (current > 0)
27805 psf_fseek (psf, current, SEEK_SET) ;
27807 return psf->error ;
27808 } /* svx_write_header */
27812 ** Do not edit or modify anything in this comment block.
27813 ** The arch-tag line is a file identity tag for the GNU Arch
27814 ** revision control system.
27816 ** arch-tag: a80ab6fb-7d75-4d32-a6b0-0061a3f05d95
27819 * Copyright 1992 by Jutta Degener and Carsten Bormann, Technische
27820 * Universitaet Berlin. See the accompanying file "COPYRIGHT" for
27821 * details. THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE.
27824 /* Most of these tables are inlined at their point of use.
27827 /* 4.4 TABLES USED IN THE FIXED POINT IMPLEMENTATION OF THE RPE-LTP
27828 * CODER AND DECODER
27830 * (Most of them inlined, so watch out.)
27833 #define GSM_TABLE_C
27835 /* Table 4.1 Quantization of the Log.-Area Ratios
27837 /* i 1 2 3 4 5 6 7 8 */
27838 word gsm_A[8] = {20480, 20480, 20480, 20480, 13964, 15360, 8534, 9036};
27839 word gsm_B[8] = { 0, 0, 2048, -2560, 94, -1792, -341, -1144};
27840 word gsm_MIC[8] = { -32, -32, -16, -16, -8, -8, -4, -4 };
27841 word gsm_MAC[8] = { 31, 31, 15, 15, 7, 7, 3, 3 };
27844 /* Table 4.2 Tabulation of 1/A[1..8]
27846 word gsm_INVA[8]={ 13107, 13107, 13107, 13107, 19223, 17476, 31454, 29708 };
27849 /* Table 4.3a Decision level of the LTP gain quantizer
27851 /* bc 0 1 2 3 */
27852 word gsm_DLB[4] = { 6554, 16384, 26214, 32767 };
27855 /* Table 4.3b Quantization levels of the LTP gain quantizer
27857 /* bc 0 1 2 3 */
27858 word gsm_QLB[4] = { 3277, 11469, 21299, 32767 };
27861 /* Table 4.4 Coefficients of the weighting filter
27863 /* i 0 1 2 3 4 5 6 7 8 9 10 */
27864 word gsm_H[11] = {-134, -374, 0, 2054, 5741, 8192, 5741, 2054, 0, -374, -134 };
27867 /* Table 4.5 Normalized inverse mantissa used to compute xM/xmax
27869 /* i 0 1 2 3 4 5 6 7 */
27870 word gsm_NRFAC[8] = { 29128, 26215, 23832, 21846, 20165, 18725, 17476, 16384 };
27873 /* Table 4.6 Normalized direct mantissa used to compute xM/xmax
27875 /* i 0 1 2 3 4 5 6 7 */
27876 word gsm_FAC[8] = { 18431, 20479, 22527, 24575, 26623, 28671, 30719, 32767 };
27878 ** Do not edit or modify anything in this comment block.
27879 ** The arch-tag line is a file identity tag for the GNU Arch
27880 ** revision control system.
27882 ** arch-tag: 8957c531-e6b0-4097-9202-da7ca42729ca
27886 ** Copyright (C) 2002-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
27888 ** This program is free software; you can redistribute it and/or modify
27889 ** it under the terms of the GNU Lesser General Public License as published by
27890 ** the Free Software Foundation; either version 2.1 of the License, or
27891 ** (at your option) any later version.
27893 ** This program is distributed in the hope that it will be useful,
27894 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
27895 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27896 ** GNU Lesser General Public License for more details.
27898 ** You should have received a copy of the GNU Lesser General Public License
27899 ** along with this program; if not, write to the Free Software
27900 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27903 /*===========================================================================
27904 ** Yamaha TX16 Sampler Files.
27906 ** This header parser was written using information from the SoX source code
27907 ** and trial and error experimentation. The code here however is all original.
27910 #include <stdio.h>
27911 #include <fcntl.h>
27912 #include <string.h>
27913 #include <ctype.h>
27916 #if (ENABLE_EXPERIMENTAL_CODE == 0)
27919 txw_open (SF_PRIVATE *psf)
27920 { if (psf)
27921 return SFE_UNIMPLEMENTED ;
27922 return (psf && 0) ;
27923 } /* txw_open */
27925 #else
27927 /*------------------------------------------------------------------------------
27928 ** Markers.
27931 #define TXW_DATA_OFFSET 32
27933 #define TXW_LOOPED 0x49
27934 #define TXW_NO_LOOP 0xC9
27936 /*------------------------------------------------------------------------------
27937 ** Private static functions.
27940 static int txw_read_header (SF_PRIVATE *psf) ;
27942 static sf_count_t txw_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
27943 static sf_count_t txw_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
27944 static sf_count_t txw_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
27945 static sf_count_t txw_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
27947 static sf_count_t txw_seek (SF_PRIVATE *psf, int mode, sf_count_t offset) ;
27949 /*------------------------------------------------------------------------------
27950 ** Public functions.
27954 * ftp://ftp.t0.or.at/pub/sound/tx16w/samples.yamaha
27955 * ftp://ftp.t0.or.at/pub/sound/tx16w/faq/tx16w.tec
27956 * http://www.t0.or.at/~mpakesch/tx16w/
27958 * from tx16w.c sox 12.15: (7-Oct-98) (Mark Lakata and Leigh Smith)
27959 * char filetype[6] "LM8953"
27960 * nulls[10],
27961 * dummy_aeg[6]
27962 * format 0x49 = looped, 0xC9 = non-looped
27963 * sample_rate 1 = 33 kHz, 2 = 50 kHz, 3 = 16 kHz
27964 * atc_length[3] if sample rate 0, [2]&0xfe = 6: 33kHz, 0x10:50, 0xf6: 16,
27965 * depending on [5] but to heck with it
27966 * rpt_length[3] (these are for looped samples, attack and loop lengths)
27967 * unused[2]
27970 typedef struct
27971 { unsigned char format, srate, sr2, sr3 ;
27972 unsigned short srhash ;
27973 unsigned int attacklen, repeatlen ;
27974 } TXW_HEADER ;
27976 #define ERROR_666 666
27979 txw_open (SF_PRIVATE *psf)
27980 { int error ;
27982 if (psf->mode != SFM_READ)
27983 return SFE_UNIMPLEMENTED ;
27985 if ((error = txw_read_header (psf)))
27986 return error ;
27988 if (psf_fseek (psf, psf->dataoffset, SEEK_SET) != psf->dataoffset)
27989 return SFE_BAD_SEEK ;
27991 psf->read_short = txw_read_s ;
27992 psf->read_int = txw_read_i ;
27993 psf->read_float = txw_read_f ;
27994 psf->read_double = txw_read_d ;
27996 psf->seek = txw_seek ;
27998 return 0 ;
27999 } /* txw_open */
28001 /*------------------------------------------------------------------------------
28004 static int
28005 txw_read_header (SF_PRIVATE *psf)
28006 { TXW_HEADER txwh ;
28007 char *strptr ;
28009 memset (&txwh, 0, sizeof (txwh)) ;
28010 memset (psf->buffer, 0, sizeof (psf->buffer)) ;
28011 psf_binheader_readf (psf, "pb", 0, psf->buffer, 16) ;
28013 if (memcmp (psf->buffer, "LM8953\0\0\0\0\0\0\0\0\0\0", 16) != 0)
28014 return ERROR_666 ;
28016 psf_log_printf (psf, "Read only : Yamaha TX-16 Sampler (.txw)\nLM8953\n") ;
28018 /* Jump 6 bytes (dummp_aeg), read format, read sample rate. */
28019 psf_binheader_readf (psf, "j11", 6, &txwh.format, &txwh.srate) ;
28021 /* 8 bytes (atc_length[3], rpt_length[3], unused[2]). */
28022 psf_binheader_readf (psf, "e33j", &txwh.attacklen, &txwh.repeatlen, 2) ;
28023 txwh.sr2 = (txwh.attacklen >> 16) & 0xFE ;
28024 txwh.sr3 = (txwh.repeatlen >> 16) & 0xFE ;
28025 txwh.attacklen &= 0x1FFFF ;
28026 txwh.repeatlen &= 0x1FFFF ;
28028 switch (txwh.format)
28029 { case TXW_LOOPED :
28030 strptr = "looped" ;
28031 break ;
28033 case TXW_NO_LOOP :
28034 strptr = "non-looped" ;
28035 break ;
28037 default :
28038 psf_log_printf (psf, " Format : 0x%02x => ?????\n", txwh.format) ;
28039 return ERROR_666 ;
28042 psf_log_printf (psf, " Format : 0x%02X => %s\n", txwh.format, strptr) ;
28044 strptr = NULL ;
28046 switch (txwh.srate)
28047 { case 1 :
28048 psf->sf.samplerate = 33333 ;
28049 break ;
28051 case 2 :
28052 psf->sf.samplerate = 50000 ;
28053 break ;
28055 case 3 :
28056 psf->sf.samplerate = 16667 ;
28057 break ;
28059 default :
28060 /* This is ugly and braindead. */
28061 txwh.srhash = ((txwh.sr2 & 0xFE) << 8) | (txwh.sr3 & 0xFE) ;
28062 switch (txwh.srhash)
28063 { case ((0x6 << 8) | 0x52) :
28064 psf->sf.samplerate = 33333 ;
28065 break ;
28067 case ((0x10 << 8) | 0x52) :
28068 psf->sf.samplerate = 50000 ;
28069 break ;
28071 case ((0xF6 << 8) | 0x52) :
28072 psf->sf.samplerate = 166667 ;
28073 break ;
28075 default :
28076 strptr = " Sample Rate : Unknown : forcing to 33333\n" ;
28077 psf->sf.samplerate = 33333 ;
28078 break ;
28083 if (strptr)
28084 psf_log_printf (psf, strptr) ;
28085 else if (txwh.srhash)
28086 psf_log_printf (psf, " Sample Rate : %d (0x%X) => %d\n", txwh.srate, txwh.srhash, psf->sf.samplerate) ;
28087 else
28088 psf_log_printf (psf, " Sample Rate : %d => %d\n", txwh.srate, psf->sf.samplerate) ;
28090 if (txwh.format == TXW_LOOPED)
28091 { psf_log_printf (psf, " Attack Len : %d\n", txwh.attacklen) ;
28092 psf_log_printf (psf, " Repeat Len : %d\n", txwh.repeatlen) ;
28095 psf->dataoffset = TXW_DATA_OFFSET ;
28096 psf->datalength = psf->filelength - TXW_DATA_OFFSET ;
28097 psf->sf.frames = 2 * psf->datalength / 3 ;
28100 if (psf->datalength % 3 == 1)
28101 psf_log_printf (psf, "*** File seems to be truncated, %d extra bytes.\n",
28102 (int) (psf->datalength % 3)) ;
28104 if (txwh.attacklen + txwh.repeatlen > psf->sf.frames)
28105 psf_log_printf (psf, "*** File has been truncated.\n") ;
28107 psf->sf.format = SF_FORMAT_TXW | SF_FORMAT_PCM_16 ;
28108 psf->sf.channels = 1 ;
28109 psf->sf.sections = 1 ;
28110 psf->sf.seekable = SF_TRUE ;
28112 return 0 ;
28113 } /* txw_read_header */
28115 static sf_count_t
28116 txw_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
28117 { unsigned char *ucptr ;
28118 short sample ;
28119 int k, bufferlen, readcount, count ;
28120 sf_count_t total = 0 ;
28122 bufferlen = sizeof (psf->buffer) / 3 ;
28123 bufferlen -= (bufferlen & 1) ;
28124 while (len > 0)
28125 { readcount = (len >= bufferlen) ? bufferlen : len ;
28126 count = psf_fread (psf->buffer, 3, readcount, psf) ;
28128 ucptr = (unsigned char *) psf->buffer ;
28129 for (k = 0 ; k < readcount ; k += 2)
28130 { sample = (ucptr [0] << 8) | (ucptr [1] & 0xF0) ;
28131 ptr [total + k] = sample ;
28132 sample = (ucptr [2] << 8) | ((ucptr [1] & 0xF) << 4) ;
28133 ptr [total + k + 1] = sample ;
28134 ucptr += 3 ;
28137 total += count ;
28138 len -= readcount ;
28141 return total ;
28142 } /* txw_read_s */
28144 static sf_count_t
28145 txw_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
28146 { unsigned char *ucptr ;
28147 short sample ;
28148 int k, bufferlen, readcount, count ;
28149 sf_count_t total = 0 ;
28151 bufferlen = sizeof (psf->buffer) / 3 ;
28152 bufferlen -= (bufferlen & 1) ;
28153 while (len > 0)
28154 { readcount = (len >= bufferlen) ? bufferlen : len ;
28155 count = psf_fread (psf->buffer, 3, readcount, psf) ;
28157 ucptr = (unsigned char *) psf->buffer ;
28158 for (k = 0 ; k < readcount ; k += 2)
28159 { sample = (ucptr [0] << 8) | (ucptr [1] & 0xF0) ;
28160 ptr [total + k] = sample << 16 ;
28161 sample = (ucptr [2] << 8) | ((ucptr [1] & 0xF) << 4) ;
28162 ptr [total + k + 1] = sample << 16 ;
28163 ucptr += 3 ;
28166 total += count ;
28167 len -= readcount ;
28170 return total ;
28171 } /* txw_read_i */
28173 static sf_count_t
28174 txw_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
28175 { unsigned char *ucptr ;
28176 short sample ;
28177 int k, bufferlen, readcount, count ;
28178 sf_count_t total = 0 ;
28179 float normfact ;
28181 if (psf->norm_float == SF_TRUE)
28182 normfact = 1.0 / 0x8000 ;
28183 else
28184 normfact = 1.0 / 0x10 ;
28186 bufferlen = sizeof (psf->buffer) / 3 ;
28187 bufferlen -= (bufferlen & 1) ;
28188 while (len > 0)
28189 { readcount = (len >= bufferlen) ? bufferlen : len ;
28190 count = psf_fread (psf->buffer, 3, readcount, psf) ;
28192 ucptr = (unsigned char *) psf->buffer ;
28193 for (k = 0 ; k < readcount ; k += 2)
28194 { sample = (ucptr [0] << 8) | (ucptr [1] & 0xF0) ;
28195 ptr [total + k] = normfact * sample ;
28196 sample = (ucptr [2] << 8) | ((ucptr [1] & 0xF) << 4) ;
28197 ptr [total + k + 1] = normfact * sample ;
28198 ucptr += 3 ;
28201 total += count ;
28202 len -= readcount ;
28205 return total ;
28206 } /* txw_read_f */
28208 static sf_count_t
28209 txw_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
28210 { unsigned char *ucptr ;
28211 short sample ;
28212 int k, bufferlen, readcount, count ;
28213 sf_count_t total = 0 ;
28214 double normfact ;
28216 if (psf->norm_double == SF_TRUE)
28217 normfact = 1.0 / 0x8000 ;
28218 else
28219 normfact = 1.0 / 0x10 ;
28221 bufferlen = sizeof (psf->buffer) / 3 ;
28222 bufferlen -= (bufferlen & 1) ;
28223 while (len > 0)
28224 { readcount = (len >= bufferlen) ? bufferlen : len ;
28225 count = psf_fread (psf->buffer, 3, readcount, psf) ;
28227 ucptr = (unsigned char *) psf->buffer ;
28228 for (k = 0 ; k < readcount ; k += 2)
28229 { sample = (ucptr [0] << 8) | (ucptr [1] & 0xF0) ;
28230 ptr [total + k] = normfact * sample ;
28231 sample = (ucptr [2] << 8) | ((ucptr [1] & 0xF) << 4) ;
28232 ptr [total + k + 1] = normfact * sample ;
28233 ucptr += 3 ;
28236 total += count ;
28237 len -= readcount ;
28240 return total ;
28241 } /* txw_read_d */
28243 static sf_count_t
28244 txw_seek (SF_PRIVATE *psf, int mode, sf_count_t offset)
28245 { if (psf && mode)
28246 return offset ;
28248 return 0 ;
28249 } /* txw_seek */
28251 #endif
28253 ** Do not edit or modify anything in this comment block.
28254 ** The arch-tag line is a file identity tag for the GNU Arch
28255 ** revision control system.
28257 ** arch-tag: 4d0ba7af-b1c5-46b4-a900-7c6f59fd9a89
28260 ** Copyright (C) 1999-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
28262 ** This program is free software; you can redistribute it and/or modify
28263 ** it under the terms of the GNU Lesser General Public License as published by
28264 ** the Free Software Foundation; either version 2.1 of the License, or
28265 ** (at your option) any later version.
28267 ** This program is distributed in the hope that it will be useful,
28268 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
28269 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28270 ** GNU Lesser General Public License for more details.
28272 ** You should have received a copy of the GNU Lesser General Public License
28273 ** along with this program; if not, write to the Free Software
28274 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28278 static sf_count_t ulaw_read_ulaw2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
28279 static sf_count_t ulaw_read_ulaw2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
28280 static sf_count_t ulaw_read_ulaw2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
28281 static sf_count_t ulaw_read_ulaw2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
28283 static sf_count_t ulaw_write_s2ulaw (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
28284 static sf_count_t ulaw_write_i2ulaw (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
28285 static sf_count_t ulaw_write_f2ulaw (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
28286 static sf_count_t ulaw_write_d2ulaw (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
28288 static void ulaw2s_array (unsigned char *buffer, unsigned int count, short *ptr) ;
28289 static void ulaw2i_array (unsigned char *buffer, unsigned int count, int *ptr) ;
28290 static void ulaw2f_array (unsigned char *buffer, unsigned int count, float *ptr, float normfact) ;
28291 static void ulaw2d_array (unsigned char *buffer, unsigned int count, double *ptr, double normfact) ;
28293 static void s2ulaw_array (short *buffer, unsigned int count, unsigned char *ptr) ;
28294 static void i2ulaw_array (int *buffer, unsigned int count, unsigned char *ptr) ;
28295 static void f2ulaw_array (float *buffer, unsigned int count, unsigned char *ptr, float normfact) ;
28296 static void d2ulaw_array (double *buffer, unsigned int count, unsigned char *ptr, double normfact) ;
28300 ulaw_init (SF_PRIVATE *psf)
28302 if (psf->mode == SFM_READ || psf->mode == SFM_RDWR)
28303 { psf->read_short = ulaw_read_ulaw2s ;
28304 psf->read_int = ulaw_read_ulaw2i ;
28305 psf->read_float = ulaw_read_ulaw2f ;
28306 psf->read_double = ulaw_read_ulaw2d ;
28309 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
28310 { psf->write_short = ulaw_write_s2ulaw ;
28311 psf->write_int = ulaw_write_i2ulaw ;
28312 psf->write_float = ulaw_write_f2ulaw ;
28313 psf->write_double = ulaw_write_d2ulaw ;
28316 psf->bytewidth = 1 ;
28317 psf->blockwidth = psf->sf.channels ;
28319 if (psf->filelength > psf->dataoffset)
28320 psf->datalength = (psf->dataend) ? psf->dataend - psf->dataoffset :
28321 psf->filelength - psf->dataoffset ;
28322 else
28323 psf->datalength = 0 ;
28325 psf->sf.frames = psf->datalength / psf->blockwidth ;
28327 return 0 ;
28328 } /* ulaw_init */
28330 static sf_count_t
28331 ulaw_read_ulaw2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
28332 { int bufferlen, readcount, thisread ;
28333 sf_count_t total = 0 ;
28335 bufferlen = sizeof (psf->buffer) / sizeof (char) ;
28337 while (len > 0)
28338 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
28339 thisread = psf_fread (psf->buffer, 1, readcount, psf) ;
28340 ulaw2s_array ((unsigned char*) (psf->buffer), thisread, ptr + total) ;
28341 total += thisread ;
28342 if (thisread < readcount)
28343 break ;
28344 len -= thisread ;
28347 return total ;
28348 } /* ulaw_read_ulaw2s */
28350 static sf_count_t
28351 ulaw_read_ulaw2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
28352 { int bufferlen, readcount, thisread ;
28353 sf_count_t total = 0 ;
28355 bufferlen = sizeof (psf->buffer) / sizeof (char) ;
28357 while (len > 0)
28358 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
28359 thisread = psf_fread (psf->buffer, 1, readcount, psf) ;
28360 ulaw2i_array ((unsigned char*) (psf->buffer), thisread, ptr + total) ;
28361 total += thisread ;
28362 if (thisread < readcount)
28363 break ;
28364 len -= thisread ;
28367 return total ;
28368 } /* ulaw_read_ulaw2i */
28370 static sf_count_t
28371 ulaw_read_ulaw2f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
28372 { int bufferlen, readcount, thisread ;
28373 sf_count_t total = 0 ;
28374 float normfact ;
28376 normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x8000) : 1.0 ;
28378 bufferlen = sizeof (psf->buffer) / sizeof (char) ;
28380 while (len > 0)
28381 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
28382 thisread = psf_fread (psf->buffer, 1, readcount, psf) ;
28383 ulaw2f_array ((unsigned char*) (psf->buffer), thisread, ptr + total, normfact) ;
28384 total += thisread ;
28385 if (thisread < readcount)
28386 break ;
28387 len -= thisread ;
28390 return total ;
28391 } /* ulaw_read_ulaw2f */
28393 static sf_count_t
28394 ulaw_read_ulaw2d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
28395 { int bufferlen, readcount, thisread ;
28396 sf_count_t total = 0 ;
28397 double normfact ;
28399 normfact = (psf->norm_double) ? 1.0 / ((double) 0x8000) : 1.0 ;
28400 bufferlen = sizeof (psf->buffer) / sizeof (char) ;
28402 while (len > 0)
28403 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
28404 thisread = psf_fread (psf->buffer, 1, readcount, psf) ;
28405 ulaw2d_array ((unsigned char*) (psf->buffer), thisread, ptr + total, normfact) ;
28406 total += thisread ;
28407 if (thisread < readcount)
28408 break ;
28409 len -= thisread ;
28412 return total ;
28413 } /* ulaw_read_ulaw2d */
28415 /*=============================================================================================
28418 static sf_count_t
28419 ulaw_write_s2ulaw (SF_PRIVATE *psf, short *ptr, sf_count_t len)
28420 { int bufferlen, writecount, thiswrite ;
28421 sf_count_t total = 0 ;
28423 bufferlen = sizeof (psf->buffer) / sizeof (char) ;
28425 while (len > 0)
28426 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
28427 s2ulaw_array (ptr + total, writecount, (unsigned char*) (psf->buffer)) ;
28428 thiswrite = psf_fwrite (psf->buffer, 1, writecount, psf) ;
28429 total += thiswrite ;
28430 if (thiswrite < writecount)
28431 break ;
28432 len -= thiswrite ;
28435 return total ;
28436 } /* ulaw_write_s2ulaw */
28438 static sf_count_t
28439 ulaw_write_i2ulaw (SF_PRIVATE *psf, int *ptr, sf_count_t len)
28440 { int bufferlen, writecount, thiswrite ;
28441 sf_count_t total = 0 ;
28443 bufferlen = sizeof (psf->buffer) / sizeof (char) ;
28445 while (len > 0)
28446 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
28447 i2ulaw_array (ptr + total, writecount, (unsigned char*) (psf->buffer)) ;
28448 thiswrite = psf_fwrite (psf->buffer, 1, writecount, psf) ;
28449 total += thiswrite ;
28450 if (thiswrite < writecount)
28451 break ;
28452 len -= thiswrite ;
28455 return total ;
28456 } /* ulaw_write_i2ulaw */
28458 static sf_count_t
28459 ulaw_write_f2ulaw (SF_PRIVATE *psf, float *ptr, sf_count_t len)
28460 { int bufferlen, writecount, thiswrite ;
28461 sf_count_t total = 0 ;
28462 float normfact ;
28464 normfact = (psf->norm_float == SF_TRUE) ? (1.0 * 0x7FFF) : 1.0 ;
28466 bufferlen = sizeof (psf->buffer) / sizeof (char) ;
28468 while (len > 0)
28469 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
28470 f2ulaw_array (ptr + total, writecount, (unsigned char*) (psf->buffer), normfact) ;
28471 thiswrite = psf_fwrite (psf->buffer, 1, writecount, psf) ;
28472 total += thiswrite ;
28473 if (thiswrite < writecount)
28474 break ;
28475 len -= thiswrite ;
28478 return total ;
28479 } /* ulaw_write_f2ulaw */
28481 static sf_count_t
28482 ulaw_write_d2ulaw (SF_PRIVATE *psf, double *ptr, sf_count_t len)
28483 { int bufferlen, writecount, thiswrite ;
28484 sf_count_t total = 0 ;
28485 double normfact ;
28487 normfact = (psf->norm_double) ? (1.0 * 0x7FFF) : 1.0 ;
28489 bufferlen = sizeof (psf->buffer) / sizeof (char) ;
28491 while (len > 0)
28492 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
28493 d2ulaw_array (ptr + total, writecount, (unsigned char*) (psf->buffer), normfact) ;
28494 thiswrite = psf_fwrite (psf->buffer, 1, writecount, psf) ;
28495 total += thiswrite ;
28496 if (thiswrite < writecount)
28497 break ;
28498 len -= thiswrite ;
28501 return total ;
28502 } /* ulaw_write_d2ulaw */
28504 /*=============================================================================================
28505 * Private static functions and data.
28508 static short ulaw_decode [128] =
28509 { -32124, -31100, -30076, -29052, -28028, -27004, -25980, -24956,
28510 -23932, -22908, -21884, -20860, -19836, -18812, -17788, -16764,
28511 -15996, -15484, -14972, -14460, -13948, -13436, -12924, -12412,
28512 -11900, -11388, -10876, -10364, -9852, -9340, -8828, -8316,
28513 -7932, -7676, -7420, -7164, -6908, -6652, -6396, -6140,
28514 -5884, -5628, -5372, -5116, -4860, -4604, -4348, -4092,
28515 -3900, -3772, -3644, -3516, -3388, -3260, -3132, -3004,
28516 -2876, -2748, -2620, -2492, -2364, -2236, -2108, -1980,
28517 -1884, -1820, -1756, -1692, -1628, -1564, -1500, -1436,
28518 -1372, -1308, -1244, -1180, -1116, -1052, -988, -924,
28519 -876, -844, -812, -780, -748, -716, -684, -652,
28520 -620, -588, -556, -524, -492, -460, -428, -396,
28521 -372, -356, -340, -324, -308, -292, -276, -260,
28522 -244, -228, -212, -196, -180, -164, -148, -132,
28523 -120, -112, -104, -96, -88, -80, -72, -64,
28524 -56, -48, -40, -32, -24, -16, -8, 0,
28527 static
28528 unsigned char ulaw_encode [8193] =
28529 { 0xFF, 0xFE, 0xFE, 0xFD, 0xFD, 0xFC, 0xFC, 0xFB, 0xFB, 0xFA, 0xFA, 0xF9,
28530 0xF9, 0xF8, 0xF8, 0xF7, 0xF7, 0xF6, 0xF6, 0xF5, 0xF5, 0xF4, 0xF4, 0xF3,
28531 0xF3, 0xF2, 0xF2, 0xF1, 0xF1, 0xF0, 0xF0, 0xEF, 0xEF, 0xEF, 0xEF, 0xEE,
28532 0xEE, 0xEE, 0xEE, 0xED, 0xED, 0xED, 0xED, 0xEC, 0xEC, 0xEC, 0xEC, 0xEB,
28533 0xEB, 0xEB, 0xEB, 0xEA, 0xEA, 0xEA, 0xEA, 0xE9, 0xE9, 0xE9, 0xE9, 0xE8,
28534 0xE8, 0xE8, 0xE8, 0xE7, 0xE7, 0xE7, 0xE7, 0xE6, 0xE6, 0xE6, 0xE6, 0xE5,
28535 0xE5, 0xE5, 0xE5, 0xE4, 0xE4, 0xE4, 0xE4, 0xE3, 0xE3, 0xE3, 0xE3, 0xE2,
28536 0xE2, 0xE2, 0xE2, 0xE1, 0xE1, 0xE1, 0xE1, 0xE0, 0xE0, 0xE0, 0xE0, 0xDF,
28537 0xDF, 0xDF, 0xDF, 0xDF, 0xDF, 0xDF, 0xDF, 0xDE, 0xDE, 0xDE, 0xDE, 0xDE,
28538 0xDE, 0xDE, 0xDE, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDC,
28539 0xDC, 0xDC, 0xDC, 0xDC, 0xDC, 0xDC, 0xDC, 0xDB, 0xDB, 0xDB, 0xDB, 0xDB,
28540 0xDB, 0xDB, 0xDB, 0xDA, 0xDA, 0xDA, 0xDA, 0xDA, 0xDA, 0xDA, 0xDA, 0xD9,
28541 0xD9, 0xD9, 0xD9, 0xD9, 0xD9, 0xD9, 0xD9, 0xD8, 0xD8, 0xD8, 0xD8, 0xD8,
28542 0xD8, 0xD8, 0xD8, 0xD7, 0xD7, 0xD7, 0xD7, 0xD7, 0xD7, 0xD7, 0xD7, 0xD6,
28543 0xD6, 0xD6, 0xD6, 0xD6, 0xD6, 0xD6, 0xD6, 0xD5, 0xD5, 0xD5, 0xD5, 0xD5,
28544 0xD5, 0xD5, 0xD5, 0xD4, 0xD4, 0xD4, 0xD4, 0xD4, 0xD4, 0xD4, 0xD4, 0xD3,
28545 0xD3, 0xD3, 0xD3, 0xD3, 0xD3, 0xD3, 0xD3, 0xD2, 0xD2, 0xD2, 0xD2, 0xD2,
28546 0xD2, 0xD2, 0xD2, 0xD1, 0xD1, 0xD1, 0xD1, 0xD1, 0xD1, 0xD1, 0xD1, 0xD0,
28547 0xD0, 0xD0, 0xD0, 0xD0, 0xD0, 0xD0, 0xD0, 0xCF, 0xCF, 0xCF, 0xCF, 0xCF,
28548 0xCF, 0xCF, 0xCF, 0xCF, 0xCF, 0xCF, 0xCF, 0xCF, 0xCF, 0xCF, 0xCF, 0xCE,
28549 0xCE, 0xCE, 0xCE, 0xCE, 0xCE, 0xCE, 0xCE, 0xCE, 0xCE, 0xCE, 0xCE, 0xCE,
28550 0xCE, 0xCE, 0xCE, 0xCD, 0xCD, 0xCD, 0xCD, 0xCD, 0xCD, 0xCD, 0xCD, 0xCD,
28551 0xCD, 0xCD, 0xCD, 0xCD, 0xCD, 0xCD, 0xCD, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
28552 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCB,
28553 0xCB, 0xCB, 0xCB, 0xCB, 0xCB, 0xCB, 0xCB, 0xCB, 0xCB, 0xCB, 0xCB, 0xCB,
28554 0xCB, 0xCB, 0xCB, 0xCA, 0xCA, 0xCA, 0xCA, 0xCA, 0xCA, 0xCA, 0xCA, 0xCA,
28555 0xCA, 0xCA, 0xCA, 0xCA, 0xCA, 0xCA, 0xCA, 0xC9, 0xC9, 0xC9, 0xC9, 0xC9,
28556 0xC9, 0xC9, 0xC9, 0xC9, 0xC9, 0xC9, 0xC9, 0xC9, 0xC9, 0xC9, 0xC9, 0xC8,
28557 0xC8, 0xC8, 0xC8, 0xC8, 0xC8, 0xC8, 0xC8, 0xC8, 0xC8, 0xC8, 0xC8, 0xC8,
28558 0xC8, 0xC8, 0xC8, 0xC7, 0xC7, 0xC7, 0xC7, 0xC7, 0xC7, 0xC7, 0xC7, 0xC7,
28559 0xC7, 0xC7, 0xC7, 0xC7, 0xC7, 0xC7, 0xC7, 0xC6, 0xC6, 0xC6, 0xC6, 0xC6,
28560 0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0xC5,
28561 0xC5, 0xC5, 0xC5, 0xC5, 0xC5, 0xC5, 0xC5, 0xC5, 0xC5, 0xC5, 0xC5, 0xC5,
28562 0xC5, 0xC5, 0xC5, 0xC4, 0xC4, 0xC4, 0xC4, 0xC4, 0xC4, 0xC4, 0xC4, 0xC4,
28563 0xC4, 0xC4, 0xC4, 0xC4, 0xC4, 0xC4, 0xC4, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3,
28564 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC2,
28565 0xC2, 0xC2, 0xC2, 0xC2, 0xC2, 0xC2, 0xC2, 0xC2, 0xC2, 0xC2, 0xC2, 0xC2,
28566 0xC2, 0xC2, 0xC2, 0xC1, 0xC1, 0xC1, 0xC1, 0xC1, 0xC1, 0xC1, 0xC1, 0xC1,
28567 0xC1, 0xC1, 0xC1, 0xC1, 0xC1, 0xC1, 0xC1, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0,
28568 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xBF,
28569 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF,
28570 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF,
28571 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE,
28572 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE,
28573 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE,
28574 0xBE, 0xBE, 0xBE, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD,
28575 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD,
28576 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBC,
28577 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC,
28578 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC,
28579 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
28580 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
28581 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
28582 0xBB, 0xBB, 0xBB, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA,
28583 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA,
28584 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xB9,
28585 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB9,
28586 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB9,
28587 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8,
28588 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8,
28589 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8,
28590 0xB8, 0xB8, 0xB8, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7,
28591 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7,
28592 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB6,
28593 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB6,
28594 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB6,
28595 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB5, 0xB5, 0xB5, 0xB5, 0xB5,
28596 0xB5, 0xB5, 0xB5, 0xB5, 0xB5, 0xB5, 0xB5, 0xB5, 0xB5, 0xB5, 0xB5, 0xB5,
28597 0xB5, 0xB5, 0xB5, 0xB5, 0xB5, 0xB5, 0xB5, 0xB5, 0xB5, 0xB5, 0xB5, 0xB5,
28598 0xB5, 0xB5, 0xB5, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4,
28599 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4,
28600 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB3,
28601 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3,
28602 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3,
28603 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2,
28604 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2,
28605 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2,
28606 0xB2, 0xB2, 0xB2, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1,
28607 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1,
28608 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB0,
28609 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0,
28610 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0,
28611 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF,
28612 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF,
28613 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF,
28614 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF,
28615 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF,
28616 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAE,
28617 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE,
28618 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE,
28619 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE,
28620 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE,
28621 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE,
28622 0xAE, 0xAE, 0xAE, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD,
28623 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD,
28624 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD,
28625 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD,
28626 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD,
28627 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC,
28628 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC,
28629 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC,
28630 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC,
28631 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC,
28632 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAB,
28633 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB,
28634 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB,
28635 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB,
28636 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB,
28637 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB,
28638 0xAB, 0xAB, 0xAB, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
28639 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
28640 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
28641 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
28642 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
28643 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9,
28644 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9,
28645 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9,
28646 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9,
28647 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9,
28648 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA8,
28649 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8,
28650 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8,
28651 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8,
28652 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8,
28653 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8,
28654 0xA8, 0xA8, 0xA8, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7,
28655 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7,
28656 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7,
28657 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7,
28658 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7,
28659 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6,
28660 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6,
28661 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6,
28662 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6,
28663 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6,
28664 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA5,
28665 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5,
28666 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5,
28667 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5,
28668 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5,
28669 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5,
28670 0xA5, 0xA5, 0xA5, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4,
28671 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4,
28672 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4,
28673 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4,
28674 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4,
28675 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3,
28676 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3,
28677 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3,
28678 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3,
28679 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3,
28680 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA2,
28681 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2,
28682 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2,
28683 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2,
28684 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2,
28685 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2,
28686 0xA2, 0xA2, 0xA2, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1,
28687 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1,
28688 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1,
28689 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1,
28690 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1,
28691 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0,
28692 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0,
28693 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0,
28694 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0,
28695 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0,
28696 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0x9F,
28697 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F,
28698 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F,
28699 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F,
28700 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F,
28701 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F,
28702 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F,
28703 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F,
28704 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F,
28705 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F,
28706 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F,
28707 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E,
28708 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E,
28709 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E,
28710 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E,
28711 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E,
28712 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E,
28713 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E,
28714 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E,
28715 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E,
28716 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E,
28717 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E,
28718 0x9E, 0x9E, 0x9E, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D,
28719 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D,
28720 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D,
28721 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D,
28722 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D,
28723 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D,
28724 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D,
28725 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D,
28726 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D,
28727 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D,
28728 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9C,
28729 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C,
28730 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C,
28731 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C,
28732 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C,
28733 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C,
28734 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C,
28735 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C,
28736 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C,
28737 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C,
28738 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C,
28739 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B,
28740 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B,
28741 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B,
28742 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B,
28743 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B,
28744 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B,
28745 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B,
28746 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B,
28747 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B,
28748 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B,
28749 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B,
28750 0x9B, 0x9B, 0x9B, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A,
28751 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A,
28752 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A,
28753 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A,
28754 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A,
28755 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A,
28756 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A,
28757 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A,
28758 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A,
28759 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A,
28760 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x99,
28761 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
28762 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
28763 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
28764 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
28765 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
28766 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
28767 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
28768 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
28769 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
28770 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
28771 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x98, 0x98, 0x98, 0x98, 0x98,
28772 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98,
28773 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98,
28774 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98,
28775 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98,
28776 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98,
28777 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98,
28778 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98,
28779 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98,
28780 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98,
28781 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98,
28782 0x98, 0x98, 0x98, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97,
28783 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97,
28784 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97,
28785 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97,
28786 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97,
28787 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97,
28788 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97,
28789 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97,
28790 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97,
28791 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97,
28792 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x96,
28793 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96,
28794 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96,
28795 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96,
28796 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96,
28797 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96,
28798 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96,
28799 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96,
28800 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96,
28801 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96,
28802 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96,
28803 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x95, 0x95, 0x95, 0x95, 0x95,
28804 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95,
28805 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95,
28806 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95,
28807 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95,
28808 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95,
28809 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95,
28810 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95,
28811 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95,
28812 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95,
28813 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95,
28814 0x95, 0x95, 0x95, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94,
28815 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94,
28816 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94,
28817 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94,
28818 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94,
28819 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94,
28820 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94,
28821 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94,
28822 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94,
28823 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94,
28824 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x93,
28825 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93,
28826 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93,
28827 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93,
28828 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93,
28829 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93,
28830 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93,
28831 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93,
28832 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93,
28833 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93,
28834 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93,
28835 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x92, 0x92, 0x92, 0x92, 0x92,
28836 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92,
28837 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92,
28838 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92,
28839 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92,
28840 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92,
28841 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92,
28842 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92,
28843 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92,
28844 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92,
28845 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92,
28846 0x92, 0x92, 0x92, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91,
28847 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91,
28848 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91,
28849 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91,
28850 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91,
28851 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91,
28852 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91,
28853 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91,
28854 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91,
28855 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91,
28856 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x90,
28857 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
28858 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
28859 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
28860 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
28861 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
28862 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
28863 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
28864 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
28865 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
28866 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
28867 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F,
28868 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F,
28869 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F,
28870 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F,
28871 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F,
28872 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F,
28873 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F,
28874 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F,
28875 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F,
28876 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F,
28877 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F,
28878 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F,
28879 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F,
28880 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F,
28881 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F,
28882 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F,
28883 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F,
28884 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F,
28885 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F,
28886 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F,
28887 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F,
28888 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8E,
28889 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E,
28890 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E,
28891 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E,
28892 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E,
28893 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E,
28894 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E,
28895 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E,
28896 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E,
28897 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E,
28898 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E,
28899 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E,
28900 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E,
28901 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E,
28902 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E,
28903 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E,
28904 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E,
28905 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E,
28906 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E,
28907 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E,
28908 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E,
28909 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E,
28910 0x8E, 0x8E, 0x8E, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D,
28911 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D,
28912 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D,
28913 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D,
28914 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D,
28915 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D,
28916 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D,
28917 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D,
28918 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D,
28919 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D,
28920 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D,
28921 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D,
28922 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D,
28923 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D,
28924 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D,
28925 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D,
28926 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D,
28927 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D,
28928 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D,
28929 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D,
28930 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D,
28931 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C,
28932 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C,
28933 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C,
28934 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C,
28935 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C,
28936 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C,
28937 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C,
28938 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C,
28939 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C,
28940 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C,
28941 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C,
28942 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C,
28943 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C,
28944 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C,
28945 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C,
28946 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C,
28947 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C,
28948 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C,
28949 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C,
28950 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C,
28951 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C,
28952 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8B,
28953 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B,
28954 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B,
28955 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B,
28956 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B,
28957 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B,
28958 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B,
28959 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B,
28960 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B,
28961 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B,
28962 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B,
28963 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B,
28964 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B,
28965 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B,
28966 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B,
28967 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B,
28968 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B,
28969 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B,
28970 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B,
28971 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B,
28972 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B,
28973 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B,
28974 0x8B, 0x8B, 0x8B, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A,
28975 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A,
28976 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A,
28977 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A,
28978 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A,
28979 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A,
28980 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A,
28981 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A,
28982 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A,
28983 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A,
28984 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A,
28985 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A,
28986 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A,
28987 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A,
28988 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A,
28989 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A,
28990 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A,
28991 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A,
28992 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A,
28993 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A,
28994 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A,
28995 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x89, 0x89, 0x89, 0x89, 0x89,
28996 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89,
28997 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89,
28998 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89,
28999 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89,
29000 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89,
29001 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89,
29002 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89,
29003 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89,
29004 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89,
29005 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89,
29006 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89,
29007 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89,
29008 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89,
29009 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89,
29010 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89,
29011 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89,
29012 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89,
29013 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89,
29014 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89,
29015 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89,
29016 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x88,
29017 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88,
29018 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88,
29019 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88,
29020 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88,
29021 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88,
29022 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88,
29023 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88,
29024 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88,
29025 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88,
29026 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88,
29027 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88,
29028 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88,
29029 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88,
29030 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88,
29031 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88,
29032 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88,
29033 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88,
29034 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88,
29035 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88,
29036 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88,
29037 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88,
29038 0x88, 0x88, 0x88, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87,
29039 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87,
29040 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87,
29041 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87,
29042 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87,
29043 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87,
29044 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87,
29045 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87,
29046 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87,
29047 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87,
29048 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87,
29049 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87,
29050 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87,
29051 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87,
29052 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87,
29053 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87,
29054 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87,
29055 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87,
29056 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87,
29057 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87,
29058 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87,
29059 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x86, 0x86, 0x86, 0x86, 0x86,
29060 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86,
29061 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86,
29062 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86,
29063 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86,
29064 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86,
29065 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86,
29066 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86,
29067 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86,
29068 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86,
29069 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86,
29070 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86,
29071 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86,
29072 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86,
29073 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86,
29074 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86,
29075 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86,
29076 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86,
29077 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86,
29078 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86,
29079 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86,
29080 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x85,
29081 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85,
29082 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85,
29083 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85,
29084 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85,
29085 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85,
29086 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85,
29087 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85,
29088 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85,
29089 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85,
29090 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85,
29091 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85,
29092 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85,
29093 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85,
29094 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85,
29095 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85,
29096 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85,
29097 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85,
29098 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85,
29099 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85,
29100 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85,
29101 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85,
29102 0x85, 0x85, 0x85, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84,
29103 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84,
29104 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84,
29105 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84,
29106 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84,
29107 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84,
29108 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84,
29109 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84,
29110 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84,
29111 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84,
29112 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84,
29113 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84,
29114 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84,
29115 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84,
29116 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84,
29117 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84,
29118 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84,
29119 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84,
29120 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84,
29121 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84,
29122 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84,
29123 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x83, 0x83, 0x83, 0x83, 0x83,
29124 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83,
29125 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83,
29126 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83,
29127 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83,
29128 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83,
29129 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83,
29130 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83,
29131 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83,
29132 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83,
29133 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83,
29134 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83,
29135 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83,
29136 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83,
29137 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83,
29138 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83,
29139 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83,
29140 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83,
29141 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83,
29142 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83,
29143 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83,
29144 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x82,
29145 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82,
29146 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82,
29147 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82,
29148 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82,
29149 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82,
29150 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82,
29151 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82,
29152 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82,
29153 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82,
29154 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82,
29155 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82,
29156 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82,
29157 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82,
29158 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82,
29159 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82,
29160 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82,
29161 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82,
29162 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82,
29163 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82,
29164 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82,
29165 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82,
29166 0x82, 0x82, 0x82, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
29167 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
29168 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
29169 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
29170 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
29171 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
29172 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
29173 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
29174 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
29175 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
29176 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
29177 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
29178 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
29179 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
29180 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
29181 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
29182 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
29183 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
29184 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
29185 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
29186 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
29187 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x80, 0x80, 0x80, 0x80, 0x80,
29188 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
29189 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
29190 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
29191 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
29192 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
29193 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
29194 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
29195 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
29196 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
29197 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
29198 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
29199 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
29200 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
29201 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
29202 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
29203 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
29204 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
29205 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
29206 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
29207 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
29208 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
29209 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
29210 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
29211 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00
29214 static void
29215 ulaw2s_array (unsigned char *buffer, unsigned int count, short *ptr)
29216 { while (count)
29217 { count -- ;
29218 if (buffer [count] & 0x80)
29219 ptr [count] = -1 * ulaw_decode [((int) buffer [count]) & 0x7F] ;
29220 else
29221 ptr [count] = ulaw_decode [((int) buffer [count]) & 0x7F] ;
29223 } /* ulaw2s_array */
29225 static void
29226 ulaw2i_array (unsigned char *buffer, unsigned int count, int *ptr)
29227 { while (count)
29228 { count -- ;
29229 if (buffer [count] & 0x80)
29230 ptr [count] = -1 * ulaw_decode [buffer [count] & 0x7F] << 16 ;
29231 else
29232 ptr [count] = ulaw_decode [buffer [count] & 0x7F] << 16 ;
29234 } /* ulaw2i_array */
29236 static void
29237 ulaw2f_array (unsigned char *buffer, unsigned int count, float *ptr, float normfact)
29238 { while (count)
29239 { count -- ;
29240 if (buffer [count] & 0x80)
29241 ptr [count] = -normfact * ulaw_decode [((int) buffer [count]) & 0x7F] ;
29242 else
29243 ptr [count] = normfact * ulaw_decode [((int) buffer [count]) & 0x7F] ;
29245 } /* ulaw2f_array */
29247 static void
29248 ulaw2d_array (unsigned char *buffer, unsigned int count, double *ptr, double normfact)
29249 { while (count)
29250 { count -- ;
29251 if (buffer [count] & 0x80)
29252 ptr [count] = -normfact * ulaw_decode [((int) buffer [count]) & 0x7F] ;
29253 else
29254 ptr [count] = normfact * ulaw_decode [((int) buffer [count]) & 0x7F] ;
29256 } /* ulaw2d_array */
29258 static void
29259 s2ulaw_array (short *ptr, unsigned int count, unsigned char *buffer)
29260 { while (count)
29261 { count -- ;
29262 if (ptr [count] >= 0)
29263 buffer [count] = ulaw_encode [ptr [count] / 4] ;
29264 else
29265 buffer [count] = 0x7F & ulaw_encode [ptr [count] / -4] ;
29267 } /* s2ulaw_array */
29269 static void
29270 i2ulaw_array (int *ptr, unsigned int count, unsigned char *buffer)
29271 { while (count)
29272 { count -- ;
29273 if (ptr [count] >= 0)
29274 buffer [count] = ulaw_encode [ptr [count] >> (16 + 2)] ;
29275 else
29276 buffer [count] = 0x7F & ulaw_encode [-ptr [count] >> (16 + 2)] ;
29278 } /* i2ulaw_array */
29280 static void
29281 f2ulaw_array (float *ptr, unsigned int count, unsigned char *buffer, float normfact)
29282 { while (count)
29283 { count -- ;
29284 if (ptr [count] >= 0)
29285 buffer [count] = ulaw_encode [(lrintf (normfact * ptr [count])) / 4] ;
29286 else
29287 buffer [count] = 0x7F & ulaw_encode [(lrintf (normfact * ptr [count])) / -4] ;
29289 } /* f2ulaw_array */
29291 static void
29292 d2ulaw_array (double *ptr, unsigned int count, unsigned char *buffer, double normfact)
29293 { while (count)
29294 { count -- ;
29295 if (ptr [count] >= 0)
29296 buffer [count] = ulaw_encode [(lrint (normfact * ptr [count])) / 4] ;
29297 else
29298 buffer [count] = 0x7F & ulaw_encode [(lrint (normfact * ptr [count])) / -4] ;
29300 } /* d2ulaw_array */
29303 ** Do not edit or modify anything in this comment block.
29304 ** The arch-tag line is a file identity tag for the GNU Arch
29305 ** revision control system.
29307 ** arch-tag: 655cc790-f058-45e8-89c9-86967cccc37e
29310 ** Copyright (C) 2001-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
29312 ** This program is free software; you can redistribute it and/or modify
29313 ** it under the terms of the GNU Lesser General Public License as published by
29314 ** the Free Software Foundation; either version 2.1 of the License, or
29315 ** (at your option) any later version.
29317 ** This program is distributed in the hope that it will be useful,
29318 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
29319 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29320 ** GNU Lesser General Public License for more details.
29322 ** You should have received a copy of the GNU Lesser General Public License
29323 ** along with this program; if not, write to the Free Software
29324 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29327 /* RANT:
29328 ** The VOC file format is the most brain damaged format I have yet had to deal
29329 ** with. No one programmer could have bee stupid enough to put this together.
29330 ** Instead it looks like a series of manic, dyslexic assembly language programmers
29331 ** hacked it to fit their needs.
29332 ** Utterly woeful.
29336 #include <stdio.h>
29337 #include <stdlib.h>
29338 #include <string.h>
29342 /*------------------------------------------------------------------------------
29343 * Typedefs for file chunks.
29346 #define VOC_MAX_SECTIONS 200
29348 enum
29349 { VOC_TERMINATOR = 0,
29350 VOC_SOUND_DATA = 1,
29351 VOC_SOUND_CONTINUE = 2,
29352 VOC_SILENCE = 3,
29353 VOC_MARKER = 4,
29354 VOC_ASCII = 5,
29355 VOC_REPEAT = 6,
29356 VOC_END_REPEAT = 7,
29357 VOC_EXTENDED = 8,
29358 VOC_EXTENDED_II = 9
29361 typedef struct
29362 { int samples ;
29363 int offset ; /* Offset of zero => silence. */
29364 } SND_DATA_BLOCKS ;
29366 typedef struct
29367 { unsigned int sections, section_types ;
29368 int samplerate, channels, bitwidth ;
29369 SND_DATA_BLOCKS blocks [VOC_MAX_SECTIONS] ;
29370 } VOC_DATA ;
29372 /*------------------------------------------------------------------------------
29373 * Private static functions.
29376 static int voc_close (SF_PRIVATE *psf) ;
29377 static int voc_write_header (SF_PRIVATE *psf, int calc_length) ;
29378 static int voc_read_header (SF_PRIVATE *psf) ;
29380 static const char* voc_encoding2str (int encoding) ;
29382 #if 0
29384 /* These functions would be required for files with more than one VOC_SOUND_DATA
29385 ** segment. Not sure whether to bother implementing this.
29388 static int voc_multi_init (SF_PRIVATE *psf, VOC_DATA *pvoc) ;
29390 static int voc_multi_read_uc2s (SF_PRIVATE *psf, short *ptr, int len) ;
29391 static int voc_multi_read_les2s (SF_PRIVATE *psf, short *ptr, int len) ;
29393 static int voc_multi_read_uc2i (SF_PRIVATE *psf, int *ptr, int len) ;
29394 static int voc_multi_read_les2i (SF_PRIVATE *psf, int *ptr, int len) ;
29396 static int voc_multi_read_uc2f (SF_PRIVATE *psf, float *ptr, int len) ;
29397 static int voc_multi_read_les2f (SF_PRIVATE *psf, float *ptr, int len) ;
29399 static int voc_multi_read_uc2d (SF_PRIVATE *psf, double *ptr, int len) ;
29400 static int voc_multi_read_les2d (SF_PRIVATE *psf, double *ptr, int len) ;
29401 #endif
29403 /*------------------------------------------------------------------------------
29404 ** Public function.
29408 voc_open (SF_PRIVATE *psf)
29409 { int subformat, error = 0 ;
29411 if (psf->is_pipe)
29412 return SFE_VOC_NO_PIPE ;
29414 if (psf->mode == SFM_READ || (psf->mode == SFM_RDWR && psf->filelength > 0))
29415 { if ((error = voc_read_header (psf)))
29416 return error ;
29419 subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
29421 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
29422 { if ((psf->sf.format & SF_FORMAT_TYPEMASK) != SF_FORMAT_VOC)
29423 return SFE_BAD_OPEN_FORMAT ;
29425 psf->endian = SF_ENDIAN_LITTLE ;
29427 if ((error = voc_write_header (psf, SF_FALSE)))
29428 return error ;
29430 psf->write_header = voc_write_header ;
29433 psf->blockwidth = psf->bytewidth * psf->sf.channels ;
29435 psf->close = voc_close ;
29437 switch (subformat)
29438 { case SF_FORMAT_PCM_U8 :
29439 case SF_FORMAT_PCM_16 :
29440 error = pcm_init (psf) ;
29441 break ;
29443 case SF_FORMAT_ALAW :
29444 error = alaw_init (psf) ;
29445 break ;
29447 case SF_FORMAT_ULAW :
29448 error = ulaw_init (psf) ;
29449 break ;
29451 default : return SFE_UNIMPLEMENTED ;
29454 return error ;
29455 } /* voc_open */
29457 /*------------------------------------------------------------------------------
29460 static int
29461 voc_read_header (SF_PRIVATE *psf)
29462 { VOC_DATA *pvoc ;
29463 char creative [20] ;
29464 unsigned char block_type, rate_byte ;
29465 short version, checksum, encoding, dataoffset ;
29466 int offset ;
29468 /* Set position to start of file to begin reading header. */
29469 offset = psf_binheader_readf (psf, "pb", 0, creative, SIGNED_SIZEOF (creative)) ;
29471 if (creative [sizeof (creative) - 1] != 0x1A)
29472 return SFE_VOC_NO_CREATIVE ;
29474 /* Terminate the string. */
29475 creative [sizeof (creative) - 1] = 0 ;
29477 if (strcmp ("Creative Voice File", creative))
29478 return SFE_VOC_NO_CREATIVE ;
29480 psf_log_printf (psf, "%s\n", creative) ;
29482 offset += psf_binheader_readf (psf, "e222", &dataoffset, &version, &checksum) ;
29484 psf->dataoffset = dataoffset ;
29486 psf_log_printf (psf, "dataoffset : %d\n"
29487 "version : 0x%X\n"
29488 "checksum : 0x%X\n", psf->dataoffset, version, checksum) ;
29490 if (version != 0x010A && version != 0x0114)
29491 return SFE_VOC_BAD_VERSION ;
29493 if (! (psf->fdata = malloc (sizeof (VOC_DATA))))
29494 return SFE_MALLOC_FAILED ;
29496 pvoc = (VOC_DATA*) psf->fdata ;
29498 memset (pvoc, 0, sizeof (VOC_DATA)) ;
29500 /* Set the default encoding now. */
29501 psf->sf.format = SF_FORMAT_VOC ; /* Major format */
29502 encoding = SF_FORMAT_PCM_U8 ; /* Minor format */
29503 psf->endian = SF_ENDIAN_LITTLE ;
29505 while (1)
29506 { offset += psf_binheader_readf (psf, "1", &block_type) ;
29508 switch (block_type)
29509 { case VOC_ASCII :
29510 { int size ;
29512 offset += psf_binheader_readf (psf, "e3", &size) ;
29514 psf_log_printf (psf, " ASCII : %d\n", size) ;
29516 offset += psf_binheader_readf (psf, "b", psf->header, size) ;
29517 psf->header [size] = 0 ;
29518 psf_log_printf (psf, " text : %s\n", psf->header) ;
29520 continue ;
29522 case VOC_SOUND_DATA :
29523 case VOC_EXTENDED :
29524 case VOC_EXTENDED_II :
29525 break ;
29527 default : psf_log_printf (psf, "*** Weird block marker (%d)\n", block_type) ;
29530 break ;
29533 if (block_type == VOC_SOUND_DATA)
29534 { unsigned char compression ;
29535 int size ;
29537 offset += psf_binheader_readf (psf, "e311", &size, &rate_byte, &compression) ;
29539 psf->sf.samplerate = 1000000 / (256 - (rate_byte & 0xFF)) ;
29541 psf_log_printf (psf, " Sound Data : %d\n sr : %d => %dHz\n comp : %d\n",
29542 size, rate_byte, psf->sf.samplerate, compression) ;
29544 if (offset + size - 1 > psf->filelength)
29545 { psf_log_printf (psf, "Seems to be a truncated file.\n") ;
29546 psf_log_printf (psf, "offset: %d size: %d sum: %d filelength: %D\n", offset, size, offset + size, psf->filelength) ;
29547 return SFE_VOC_BAD_SECTIONS ;
29549 else if (offset + size - 1 < psf->filelength)
29550 { psf_log_printf (psf, "Seems to be a multi-segment file (#1).\n") ;
29551 psf_log_printf (psf, "offset: %d size: %d sum: %d filelength: %D\n", offset, size, offset + size, psf->filelength) ;
29552 return SFE_VOC_BAD_SECTIONS ;
29555 psf->dataoffset = offset ;
29556 psf->dataend = psf->filelength - 1 ;
29558 psf->sf.channels = 1 ;
29559 psf->bytewidth = 1 ;
29561 psf->sf.format = SF_FORMAT_VOC | SF_FORMAT_PCM_U8 ;
29563 return 0 ;
29566 if (block_type == VOC_EXTENDED)
29567 { unsigned char pack, stereo, compression ;
29568 unsigned short rate_short ;
29569 int size ;
29571 offset += psf_binheader_readf (psf, "e3211", &size, &rate_short, &pack, &stereo) ;
29573 psf_log_printf (psf, " Extended : %d\n", size) ;
29574 if (size == 4)
29575 psf_log_printf (psf, " size : 4\n") ;
29576 else
29577 psf_log_printf (psf, " size : %d (should be 4)\n", size) ;
29579 psf_log_printf (psf, " pack : %d\n"
29580 " stereo : %s\n", pack, (stereo ? "yes" : "no")) ;
29582 if (stereo)
29583 { psf->sf.channels = 2 ;
29584 psf->sf.samplerate = 128000000 / (65536 - rate_short) ;
29586 else
29587 { psf->sf.channels = 1 ;
29588 psf->sf.samplerate = 256000000 / (65536 - rate_short) ;
29591 psf_log_printf (psf, " sr : %d => %dHz\n", (rate_short & 0xFFFF), psf->sf.samplerate) ;
29593 offset += psf_binheader_readf (psf, "1", &block_type) ;
29595 if (block_type != VOC_SOUND_DATA)
29596 { psf_log_printf (psf, "*** Expecting VOC_SOUND_DATA section.\n") ;
29597 return SFE_VOC_BAD_FORMAT ;
29600 offset += psf_binheader_readf (psf, "e311", &size, &rate_byte, &compression) ;
29602 psf_log_printf (psf, " Sound Data : %d\n"
29603 " sr : %d\n"
29604 " comp : %d\n", size, rate_byte, compression) ;
29607 if (offset + size - 1 > psf->filelength)
29608 { psf_log_printf (psf, "Seems to be a truncated file.\n") ;
29609 psf_log_printf (psf, "offset: %d size: %d sum: %d filelength: %D\n", offset, size, offset + size, psf->filelength) ;
29610 return SFE_VOC_BAD_SECTIONS ;
29612 else if (offset + size - 1 < psf->filelength)
29613 { psf_log_printf (psf, "Seems to be a multi-segment file (#2).\n") ;
29614 psf_log_printf (psf, "offset: %d size: %d sum: %d filelength: %D\n", offset, size, offset + size, psf->filelength) ;
29615 return SFE_VOC_BAD_SECTIONS ;
29618 psf->dataoffset = offset ;
29619 psf->dataend = psf->filelength - 1 ;
29621 psf->bytewidth = 1 ;
29623 psf->sf.format = SF_FORMAT_VOC | SF_FORMAT_PCM_U8 ;
29625 return 0 ;
29628 if (block_type == VOC_EXTENDED_II)
29629 { unsigned char bitwidth, channels ;
29630 int size, fourbytes ;
29632 offset += psf_binheader_readf (psf, "e341124", &size, &psf->sf.samplerate,
29633 &bitwidth, &channels, &encoding, &fourbytes) ;
29635 if (size * 2 == psf->filelength - 39)
29636 { int temp_size = psf->filelength - 31 ;
29638 psf_log_printf (psf, " Extended II : %d (SoX bug: should be %d)\n", size, temp_size) ;
29639 size = temp_size ;
29641 else
29642 psf_log_printf (psf, " Extended II : %d\n", size) ;
29644 psf_log_printf (psf, " sample rate : %d\n"
29645 " bit width : %d\n"
29646 " channels : %d\n", psf->sf.samplerate, bitwidth, channels) ;
29648 if (bitwidth == 16 && encoding == 0)
29649 { encoding = 4 ;
29650 psf_log_printf (psf, " encoding : 0 (SoX bug: should be 4 for 16 bit signed PCM)\n") ;
29652 else
29653 psf_log_printf (psf, " encoding : %d => %s\n", encoding, voc_encoding2str (encoding)) ;
29656 psf_log_printf (psf, " fourbytes : %X\n", fourbytes) ;
29658 psf->sf.channels = channels ;
29660 psf->dataoffset = offset ;
29661 psf->dataend = psf->filelength - 1 ;
29663 if (size + 31 == psf->filelength + 1)
29664 { /* Hack for reading files produced using
29665 ** sf_command (SFC_UPDATE_HEADER_NOW).
29667 psf_log_printf (psf, "Missing zero byte at end of file.\n") ;
29668 size = psf->filelength - 30 ;
29669 psf->dataend = 0 ;
29671 else if (size + 31 > psf->filelength)
29672 { psf_log_printf (psf, "Seems to be a truncated file.\n") ;
29673 size = psf->filelength - 31 ;
29675 else if (size + 31 < psf->filelength)
29676 psf_log_printf (psf, "Seems to be a multi-segment file (#3).\n") ;
29678 switch (encoding)
29679 { case 0 :
29680 psf->sf.format = SF_FORMAT_VOC | SF_FORMAT_PCM_U8 ;
29681 psf->bytewidth = 1 ;
29682 break ;
29684 case 4 :
29685 psf->sf.format = SF_FORMAT_VOC | SF_FORMAT_PCM_16 ;
29686 psf->bytewidth = 2 ;
29687 break ;
29689 case 6 :
29690 psf->sf.format = SF_FORMAT_VOC | SF_FORMAT_ALAW ;
29691 psf->bytewidth = 1 ;
29692 break ;
29694 case 7 :
29695 psf->sf.format = SF_FORMAT_VOC | SF_FORMAT_ULAW ;
29696 psf->bytewidth = 1 ;
29697 break ;
29699 default : /* Unknown */
29700 return SFE_UNKNOWN_FORMAT ;
29701 break ;
29706 return 0 ;
29707 } /* voc_read_header */
29709 /*====================================================================================
29712 static int
29713 voc_write_header (SF_PRIVATE *psf, int calc_length)
29714 { sf_count_t current ;
29715 int rate_const, subformat ;
29717 current = psf_ftell (psf) ;
29719 if (calc_length)
29720 { psf->filelength = psf_get_filelen (psf) ;
29722 psf->datalength = psf->filelength - psf->dataoffset ;
29723 if (psf->dataend)
29724 psf->datalength -= psf->filelength - psf->dataend ;
29726 psf->sf.frames = psf->datalength / (psf->bytewidth * psf->sf.channels) ;
29729 subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
29730 /* Reset the current header length to zero. */
29731 psf->header [0] = 0 ;
29732 psf->headindex = 0 ;
29733 psf_fseek (psf, 0, SEEK_SET) ;
29735 /* VOC marker and 0x1A byte. */
29736 psf_binheader_writef (psf, "eb1", "Creative Voice File", 19, 0x1A) ;
29738 /* Data offset, version and other. */
29739 psf_binheader_writef (psf, "e222", 26, 0x0114, 0x111F) ;
29741 /* Use same logic as SOX.
29742 ** If the file is mono 8 bit data, use VOC_SOUND_DATA.
29743 ** If the file is mono 16 bit data, use VOC_EXTENED.
29744 ** Otherwise use VOC_EXTENED_2.
29747 if (subformat == SF_FORMAT_PCM_U8 && psf->sf.channels == 1)
29748 { /* samplerate = 1000000 / (256 - rate_const) ; */
29749 rate_const = 256 - 1000000 / psf->sf.samplerate ;
29751 /* First type marker, length, rate_const and compression */
29752 psf_binheader_writef (psf, "e1311", VOC_SOUND_DATA, (int) (psf->datalength + 1), rate_const, 0) ;
29754 else if (subformat == SF_FORMAT_PCM_U8 && psf->sf.channels == 2)
29755 { /* sample_rate = 128000000 / (65536 - rate_short) ; */
29756 rate_const = 65536 - 128000000 / psf->sf.samplerate ;
29758 /* First write the VOC_EXTENDED section
29759 ** marker, length, rate_const and compression
29761 psf_binheader_writef (psf, "e13211", VOC_EXTENDED, 4, rate_const, 0, 1) ;
29763 /* samplerate = 1000000 / (256 - rate_const) ; */
29764 rate_const = 256 - 1000000 / psf->sf.samplerate ;
29766 /* Now write the VOC_SOUND_DATA section
29767 ** marker, length, rate_const and compression
29769 psf_binheader_writef (psf, "e1311", VOC_SOUND_DATA, (int) (psf->datalength + 1), rate_const, 0) ;
29771 else
29772 { int length ;
29774 if (psf->sf.channels < 1 || psf->sf.channels > 2)
29775 return SFE_CHANNEL_COUNT ;
29777 switch (subformat)
29778 { case SF_FORMAT_PCM_U8 :
29779 psf->bytewidth = 1 ;
29780 length = psf->sf.frames * psf->sf.channels * psf->bytewidth + 12 ;
29781 /* Marker, length, sample rate, bitwidth, stereo flag, encoding and fourt zero bytes. */
29782 psf_binheader_writef (psf, "e1341124", VOC_EXTENDED_II, length, psf->sf.samplerate, 16, psf->sf.channels, 4, 0) ;
29783 break ;
29785 case SF_FORMAT_PCM_16 :
29786 psf->bytewidth = 2 ;
29787 length = psf->sf.frames * psf->sf.channels * psf->bytewidth + 12 ;
29788 /* Marker, length, sample rate, bitwidth, stereo flag, encoding and fourt zero bytes. */
29789 psf_binheader_writef (psf, "e1341124", VOC_EXTENDED_II, length, psf->sf.samplerate, 16, psf->sf.channels, 4, 0) ;
29790 break ;
29792 case SF_FORMAT_ALAW :
29793 psf->bytewidth = 1 ;
29794 length = psf->sf.frames * psf->sf.channels * psf->bytewidth + 12 ;
29795 psf_binheader_writef (psf, "e1341124", VOC_EXTENDED_II, length, psf->sf.samplerate, 8, psf->sf.channels, 6, 0) ;
29796 break ;
29798 case SF_FORMAT_ULAW :
29799 psf->bytewidth = 1 ;
29800 length = psf->sf.frames * psf->sf.channels * psf->bytewidth + 12 ;
29801 psf_binheader_writef (psf, "e1341124", VOC_EXTENDED_II, length, psf->sf.samplerate, 8, psf->sf.channels, 7, 0) ;
29802 break ;
29804 default : return SFE_UNIMPLEMENTED ;
29808 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
29810 if (psf->error)
29811 return psf->error ;
29813 psf->dataoffset = psf->headindex ;
29815 if (current > 0)
29816 psf_fseek (psf, current, SEEK_SET) ;
29818 return psf->error ;
29819 } /* voc_write_header */
29821 static int
29822 voc_close (SF_PRIVATE *psf)
29824 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
29825 { /* Now we know for certain the length of the file we can re-write
29826 ** correct values for the FORM, 8SVX and BODY chunks.
29828 unsigned byte = VOC_TERMINATOR ;
29831 psf_fseek (psf, 0, SEEK_END) ;
29833 /* Write terminator */
29834 psf_fwrite (&byte, 1, 1, psf) ;
29836 voc_write_header (psf, SF_TRUE) ;
29839 return 0 ;
29840 } /* voc_close */
29842 static const char*
29843 voc_encoding2str (int encoding)
29845 switch (encoding)
29846 { case 0 : return "8 bit unsigned PCM" ;
29847 case 4 : return "16 bit signed PCM" ;
29848 case 6 : return "A-law" ;
29849 case 7 : return "u-law" ;
29850 default : break ;
29852 return "*** Unknown ***" ;
29853 } /* voc_encoding2str */
29855 /*====================================================================================
29858 #if 0
29859 static int
29860 voc_multi_init (SF_PRIVATE *psf, VOC_DATA *pvoc)
29862 psf->sf.frames = 0 ;
29864 if (pvoc->bitwidth == 8)
29865 { psf->read_short = voc_multi_read_uc2s ;
29866 psf->read_int = voc_multi_read_uc2i ;
29867 psf->read_float = voc_multi_read_uc2f ;
29868 psf->read_double = voc_multi_read_uc2d ;
29869 return 0 ;
29872 if (pvoc->bitwidth == 16)
29873 { psf->read_short = voc_multi_read_les2s ;
29874 psf->read_int = voc_multi_read_les2i ;
29875 psf->read_float = voc_multi_read_les2f ;
29876 psf->read_double = voc_multi_read_les2d ;
29877 return 0 ;
29880 psf_log_printf (psf, "Error : bitwith != 8 && bitwidth != 16.\n") ;
29882 return SFE_UNIMPLEMENTED ;
29883 } /* voc_multi_read_int */
29885 /*------------------------------------------------------------------------------------
29888 static int
29889 voc_multi_read_uc2s (SF_PRIVATE *psf, short *ptr, int len)
29892 return 0 ;
29893 } /* voc_multi_read_uc2s */
29895 static int
29896 voc_multi_read_les2s (SF_PRIVATE *psf, short *ptr, int len)
29899 return 0 ;
29900 } /* voc_multi_read_les2s */
29903 static int
29904 voc_multi_read_uc2i (SF_PRIVATE *psf, int *ptr, int len)
29907 return 0 ;
29908 } /* voc_multi_read_uc2i */
29910 static int
29911 voc_multi_read_les2i (SF_PRIVATE *psf, int *ptr, int len)
29914 return 0 ;
29915 } /* voc_multi_read_les2i */
29918 static int
29919 voc_multi_read_uc2f (SF_PRIVATE *psf, float *ptr, int len)
29922 return 0 ;
29923 } /* voc_multi_read_uc2f */
29925 static int
29926 voc_multi_read_les2f (SF_PRIVATE *psf, float *ptr, int len)
29929 return 0 ;
29930 } /* voc_multi_read_les2f */
29933 static int
29934 voc_multi_read_uc2d (SF_PRIVATE *psf, double *ptr, int len)
29937 return 0 ;
29938 } /* voc_multi_read_uc2d */
29940 static int
29941 voc_multi_read_les2d (SF_PRIVATE *psf, double *ptr, int len)
29944 return 0 ;
29945 } /* voc_multi_read_les2d */
29947 #endif
29949 /*------------------------------------------------------------------------------------
29951 Creative Voice (VOC) file format
29952 --------------------------------
29954 ~From: galt@dsd.es.com
29956 (byte numbers are hex!)
29958 HEADER (bytes 00-19)
29959 Series of DATA BLOCKS (bytes 1A+) [Must end w/ Terminator Block]
29961 - ---------------------------------------------------------------
29963 HEADER:
29964 =======
29965 byte # Description
29966 ------ ------------------------------------------
29967 00-12 "Creative Voice File"
29968 13 1A (eof to abort printing of file)
29969 14-15 Offset of first datablock in .voc file (std 1A 00
29970 in Intel Notation)
29971 16-17 Version number (minor,major) (VOC-HDR puts 0A 01)
29972 18-19 1's Comp of Ver. # + 1234h (VOC-HDR puts 29 11)
29974 - ---------------------------------------------------------------
29976 DATA BLOCK:
29977 ===========
29979 Data Block: TYPE(1-byte), SIZE(3-bytes), INFO(0+ bytes)
29980 NOTE: Terminator Block is an exception -- it has only the TYPE byte.
29982 TYPE Description Size (3-byte int) Info
29983 ---- ----------- ----------------- -----------------------
29984 00 Terminator (NONE) (NONE)
29985 01 Sound data 2+length of data *
29986 02 Sound continue length of data Voice Data
29987 03 Silence 3 **
29988 04 Marker 2 Marker# (2 bytes)
29989 05 ASCII length of string null terminated string
29990 06 Repeat 2 Count# (2 bytes)
29991 07 End repeat 0 (NONE)
29992 08 Extended 4 ***
29994 *Sound Info Format:
29995 ---------------------
29996 00 Sample Rate
29997 01 Compression Type
29998 02+ Voice Data
30000 **Silence Info Format:
30001 ----------------------------
30002 00-01 Length of silence - 1
30003 02 Sample Rate
30006 ***Extended Info Format:
30007 ---------------------
30008 00-01 Time Constant: Mono: 65536 - (256000000/sample_rate)
30009 Stereo: 65536 - (25600000/(2*sample_rate))
30010 02 Pack
30011 03 Mode: 0 = mono
30012 1 = stereo
30015 Marker# -- Driver keeps the most recent marker in a status byte
30016 Count# -- Number of repetitions + 1
30017 Count# may be 1 to FFFE for 0 - FFFD repetitions
30018 or FFFF for endless repetitions
30019 Sample Rate -- SR byte = 256-(1000000/sample_rate)
30020 Length of silence -- in units of sampling cycle
30021 Compression Type -- of voice data
30022 8-bits = 0
30023 4-bits = 1
30024 2.6-bits = 2
30025 2-bits = 3
30026 Multi DAC = 3+(# of channels) [interesting--
30027 this isn't in the developer's manual]
30030 ---------------------------------------------------------------------------------
30031 Addendum submitted by Votis Kokavessis:
30033 After some experimenting with .VOC files I found out that there is a Data Block
30034 Type 9, which is not covered in the VOC.TXT file. Here is what I was able to discover
30035 about this block type:
30038 TYPE: 09
30039 SIZE: 12 + length of data
30040 INFO: 12 (twelve) bytes
30042 INFO STRUCTURE:
30044 Bytes 0-1: (Word) Sample Rate (e.g. 44100)
30045 Bytes 2-3: zero (could be that bytes 0-3 are a DWord for Sample Rate)
30046 Byte 4: Sample Size in bits (e.g. 16)
30047 Byte 5: Number of channels (e.g. 1 for mono, 2 for stereo)
30048 Byte 6: Unknown (equal to 4 in all files I examined)
30049 Bytes 7-11: zero
30052 -------------------------------------------------------------------------------------*/
30054 /*=====================================================================================
30055 **=====================================================================================
30056 **=====================================================================================
30057 **=====================================================================================
30060 /*------------------------------------------------------------------------
30061 The following is taken from the Audio File Formats FAQ dated 2-Jan-1995
30062 and submitted by Guido van Rossum <guido@cwi.nl>.
30063 --------------------------------------------------------------------------
30064 Creative Voice (VOC) file format
30065 --------------------------------
30067 From: galt@dsd.es.com
30069 (byte numbers are hex!)
30071 HEADER (bytes 00-19)
30072 Series of DATA BLOCKS (bytes 1A+) [Must end w/ Terminator Block]
30074 - ---------------------------------------------------------------
30076 HEADER:
30077 -------
30078 byte # Description
30079 ------ ------------------------------------------
30080 00-12 "Creative Voice File"
30081 13 1A (eof to abort printing of file)
30082 14-15 Offset of first datablock in .voc file (std 1A 00
30083 in Intel Notation)
30084 16-17 Version number (minor,major) (VOC-HDR puts 0A 01)
30085 18-19 2's Comp of Ver. # + 1234h (VOC-HDR puts 29 11)
30087 - ---------------------------------------------------------------
30089 DATA BLOCK:
30090 -----------
30092 Data Block: TYPE(1-byte), SIZE(3-bytes), INFO(0+ bytes)
30093 NOTE: Terminator Block is an exception -- it has only the TYPE byte.
30095 TYPE Description Size (3-byte int) Info
30096 ---- ----------- ----------------- -----------------------
30097 00 Terminator (NONE) (NONE)
30098 01 Sound data 2+length of data *
30099 02 Sound continue length of data Voice Data
30100 03 Silence 3 **
30101 04 Marker 2 Marker# (2 bytes)
30102 05 ASCII length of string null terminated string
30103 06 Repeat 2 Count# (2 bytes)
30104 07 End repeat 0 (NONE)
30105 08 Extended 4 ***
30107 *Sound Info Format: **Silence Info Format:
30108 --------------------- ----------------------------
30109 00 Sample Rate 00-01 Length of silence - 1
30110 01 Compression Type 02 Sample Rate
30111 02+ Voice Data
30113 ***Extended Info Format:
30114 ---------------------
30115 00-01 Time Constant: Mono: 65536 - (256000000/sample_rate)
30116 Stereo: 65536 - (25600000/(2*sample_rate))
30117 02 Pack
30118 03 Mode: 0 = mono
30119 1 = stereo
30122 Marker# -- Driver keeps the most recent marker in a status byte
30123 Count# -- Number of repetitions + 1
30124 Count# may be 1 to FFFE for 0 - FFFD repetitions
30125 or FFFF for endless repetitions
30126 Sample Rate -- SR byte = 256-(1000000/sample_rate)
30127 Length of silence -- in units of sampling cycle
30128 Compression Type -- of voice data
30129 8-bits = 0
30130 4-bits = 1
30131 2.6-bits = 2
30132 2-bits = 3
30133 Multi DAC = 3+(# of channels) [interesting--
30134 this isn't in the developer's manual]
30136 Detailed description of new data blocks (VOC files version 1.20 and above):
30138 (Source is fax from Barry Boone at Creative Labs, 405/742-6622)
30140 BLOCK 8 - digitized sound attribute extension, must preceed block 1.
30141 Used to define stereo, 8 bit audio
30142 BYTE bBlockID; // = 8
30143 BYTE nBlockLen[3]; // 3 byte length
30144 WORD wTimeConstant; // time constant = same as block 1
30145 BYTE bPackMethod; // same as in block 1
30146 BYTE bVoiceMode; // 0-mono, 1-stereo
30148 Data is stored left, right
30150 BLOCK 9 - data block that supersedes blocks 1 and 8.
30151 Used for stereo, 16 bit.
30153 BYTE bBlockID; // = 9
30154 BYTE nBlockLen[3]; // length 12 plus length of sound
30155 DWORD dwSamplesPerSec; // samples per second, not time const.
30156 BYTE bBitsPerSample; // e.g., 8 or 16
30157 BYTE bChannels; // 1 for mono, 2 for stereo
30158 WORD wFormat; // see below
30159 BYTE reserved[4]; // pad to make block w/o data
30160 // have a size of 16 bytes
30162 Valid values of wFormat are:
30164 0x0000 8-bit unsigned PCM
30165 0x0001 Creative 8-bit to 4-bit ADPCM
30166 0x0002 Creative 8-bit to 3-bit ADPCM
30167 0x0003 Creative 8-bit to 2-bit ADPCM
30168 0x0004 16-bit signed PCM
30169 0x0006 CCITT a-Law
30170 0x0007 CCITT u-Law
30171 0x02000 Creative 16-bit to 4-bit ADPCM
30173 Data is stored left, right
30175 ------------------------------------------------------------------------*/
30177 ** Do not edit or modify anything in this comment block.
30178 ** The arch-tag line is a file identity tag for the GNU Arch
30179 ** revision control system.
30181 ** arch-tag: 40a50167-a81c-463a-9e1d-3282ff84e09d
30184 ** Copyright (C) 2002-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
30186 ** This program is free software; you can redistribute it and/or modify
30187 ** it under the terms of the GNU Lesser General Public License as published by
30188 ** the Free Software Foundation; either version 2.1 of the License, or
30189 ** (at your option) any later version.
30191 ** This program is distributed in the hope that it will be useful,
30192 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
30193 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30194 ** GNU Lesser General Public License for more details.
30196 ** You should have received a copy of the GNU Lesser General Public License
30197 ** along with this program; if not, write to the Free Software
30198 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30202 ** This is the OKI / Dialogic ADPCM encoder/decoder. It converts from
30203 ** 12 bit linear sample data to a 4 bit ADPCM.
30205 ** Implemented from the description found here:
30207 ** http://www.comptek.ru:8100/telephony/tnotes/tt1-13.html
30209 ** and compared against the encoder/decoder found here:
30211 ** http://ibiblio.org/pub/linux/apps/sound/convert/vox.tar.gz
30214 #include <stdio.h>
30215 #include <stdlib.h>
30216 #include <string.h>
30219 #define VOX_DATA_LEN 2048
30220 #define PCM_DATA_LEN (VOX_DATA_LEN *2)
30222 typedef struct
30223 { short last ;
30224 short step_index ;
30226 int vox_bytes, pcm_samples ;
30228 unsigned char vox_data [VOX_DATA_LEN] ;
30229 short pcm_data [PCM_DATA_LEN] ;
30230 } VOX_ADPCM_PRIVATE ;
30232 static int vox_adpcm_encode_block (VOX_ADPCM_PRIVATE *pvox) ;
30233 static int vox_adpcm_decode_block (VOX_ADPCM_PRIVATE *pvox) ;
30235 static short vox_adpcm_decode (char code, VOX_ADPCM_PRIVATE *pvox) ;
30236 static char vox_adpcm_encode (short samp, VOX_ADPCM_PRIVATE *pvox) ;
30238 static sf_count_t vox_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
30239 static sf_count_t vox_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
30240 static sf_count_t vox_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
30241 static sf_count_t vox_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
30243 static sf_count_t vox_write_s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
30244 static sf_count_t vox_write_i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
30245 static sf_count_t vox_write_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
30246 static sf_count_t vox_write_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
30248 static int vox_read_block (SF_PRIVATE *psf, VOX_ADPCM_PRIVATE *pvox, short *ptr, int len) ;
30249 static int vox_write_block (SF_PRIVATE *psf, VOX_ADPCM_PRIVATE *pvox, short *ptr, int len) ;
30251 /*============================================================================================
30252 ** Predefined OKI ADPCM encoder/decoder tables.
30255 static short stepvox_adpcm_size_table [49] =
30256 { 16, 17, 19, 21, 23, 25, 28, 31, 34, 37, 41, 45, 50, 55, 60,
30257 66, 73, 80, 88, 97, 107, 118, 130, 143, 157, 173, 190, 209,
30258 230, 253, 279, 307, 337, 371, 408, 449, 494, 544, 598, 658,
30259 724, 796, 876, 963, 1060, 1166, 1282, 1408, 1552
30260 } ; /* stepvox_adpcm_size_table */
30262 static short stepvox_adpcm_adjust_table [8] =
30263 { -1, -1, -1, -1, 2, 4, 6, 8
30264 } ; /* stepvox_adpcm_adjust_table */
30266 /*------------------------------------------------------------------------------
30270 vox_adpcm_init (SF_PRIVATE *psf)
30271 { VOX_ADPCM_PRIVATE *pvox = NULL ;
30273 if (psf->mode == SFM_RDWR)
30274 return SFE_BAD_MODE_RW ;
30276 if (psf->mode == SFM_WRITE && psf->sf.channels != 1)
30277 return SFE_CHANNEL_COUNT ;
30279 if ((pvox = malloc (sizeof (VOX_ADPCM_PRIVATE))) == NULL)
30280 return SFE_MALLOC_FAILED ;
30282 psf->fdata = (void*) pvox ;
30283 memset (pvox, 0, sizeof (VOX_ADPCM_PRIVATE)) ;
30285 if (psf->mode == SFM_WRITE)
30286 { psf->write_short = vox_write_s ;
30287 psf->write_int = vox_write_i ;
30288 psf->write_float = vox_write_f ;
30289 psf->write_double = vox_write_d ;
30291 else
30292 { psf_log_printf (psf, "Header-less OKI Dialogic ADPCM encoded file.\n") ;
30293 psf_log_printf (psf, "Setting up for 8kHz, mono, Vox ADPCM.\n") ;
30295 psf->read_short = vox_read_s ;
30296 psf->read_int = vox_read_i ;
30297 psf->read_float = vox_read_f ;
30298 psf->read_double = vox_read_d ;
30301 /* Standard sample rate chennels etc. */
30302 if (psf->sf.samplerate < 1)
30303 psf->sf.samplerate = 8000 ;
30304 psf->sf.channels = 1 ;
30306 psf->sf.frames = psf->filelength * 2 ;
30308 psf->sf.seekable = SF_FALSE ;
30310 /* Seek back to start of data. */
30311 if (psf_fseek (psf, 0 , SEEK_SET) == -1)
30312 return SFE_BAD_SEEK ;
30314 return 0 ;
30315 } /* vox_adpcm_init */
30317 /*------------------------------------------------------------------------------
30320 static char
30321 vox_adpcm_encode (short samp, VOX_ADPCM_PRIVATE *pvox)
30322 { short code ;
30323 short diff, error, stepsize ;
30325 stepsize = stepvox_adpcm_size_table [pvox->step_index] ;
30326 code = 0 ;
30328 diff = samp - pvox->last ;
30329 if (diff < 0)
30330 { code = 0x08 ;
30331 error = -diff ;
30333 else
30334 error = diff ;
30336 if (error >= stepsize)
30337 { code = code | 0x04 ;
30338 error -= stepsize ;
30341 if (error >= stepsize / 2)
30342 { code = code | 0x02 ;
30343 error -= stepsize / 2 ;
30346 if (error >= stepsize / 4)
30347 code = code | 0x01 ;
30350 ** To close the feedback loop, the deocder is used to set the
30351 ** estimate of last sample and in doing so, also set the step_index.
30353 pvox->last = vox_adpcm_decode (code, pvox) ;
30355 return code ;
30356 } /* vox_adpcm_encode */
30358 static short
30359 vox_adpcm_decode (char code, VOX_ADPCM_PRIVATE *pvox)
30360 { short diff, error, stepsize, samp ;
30362 stepsize = stepvox_adpcm_size_table [pvox->step_index] ;
30364 error = stepsize / 8 ;
30366 if (code & 0x01)
30367 error += stepsize / 4 ;
30369 if (code & 0x02)
30370 error += stepsize / 2 ;
30372 if (code & 0x04)
30373 error += stepsize ;
30375 diff = (code & 0x08) ? -error : error ;
30376 samp = pvox->last + diff ;
30379 ** Apply clipping.
30381 if (samp > 2048)
30382 samp = 2048 ;
30383 if (samp < -2048)
30384 samp = -2048 ;
30386 pvox->last = samp ;
30387 pvox->step_index += stepvox_adpcm_adjust_table [code & 0x7] ;
30389 if (pvox->step_index < 0)
30390 pvox->step_index = 0 ;
30391 if (pvox->step_index > 48)
30392 pvox->step_index = 48 ;
30394 return samp ;
30395 } /* vox_adpcm_decode */
30397 static int
30398 vox_adpcm_encode_block (VOX_ADPCM_PRIVATE *pvox)
30399 { unsigned char code ;
30400 int j, k ;
30402 /* If data_count is odd, add an extra zero valued sample. */
30403 if (pvox->pcm_samples & 1)
30404 pvox->pcm_data [pvox->pcm_samples++] = 0 ;
30406 for (j = k = 0 ; k < pvox->pcm_samples ; j++)
30407 { code = vox_adpcm_encode (pvox->pcm_data [k++] / 16, pvox) << 4 ;
30408 code |= vox_adpcm_encode (pvox->pcm_data [k++] / 16, pvox) ;
30409 pvox->vox_data [j] = code ;
30412 pvox->vox_bytes = j ;
30414 return 0 ;
30415 } /* vox_adpcm_encode_block */
30417 static int
30418 vox_adpcm_decode_block (VOX_ADPCM_PRIVATE *pvox)
30419 { unsigned char code ;
30420 int j, k ;
30422 for (j = k = 0 ; j < pvox->vox_bytes ; j++)
30423 { code = pvox->vox_data [j] ;
30424 pvox->pcm_data [k++] = 16 * vox_adpcm_decode ((code >> 4) & 0x0f, pvox) ;
30425 pvox->pcm_data [k++] = 16 * vox_adpcm_decode (code & 0x0f, pvox) ;
30428 pvox->pcm_samples = k ;
30430 return 0 ;
30431 } /* vox_adpcm_decode_block */
30433 /*==============================================================================
30436 static int
30437 vox_read_block (SF_PRIVATE *psf, VOX_ADPCM_PRIVATE *pvox, short *ptr, int len)
30438 { int indx = 0, k ;
30440 while (indx < len)
30441 { pvox->vox_bytes = (len - indx > PCM_DATA_LEN) ? VOX_DATA_LEN : (len - indx + 1) / 2 ;
30443 if ((k = psf_fread (pvox->vox_data, 1, pvox->vox_bytes, psf)) != pvox->vox_bytes)
30444 { if (psf_ftell (psf) + k != psf->filelength)
30445 psf_log_printf (psf, "*** Warning : short read (%d != %d).\n", k, pvox->vox_bytes) ;
30446 if (k == 0)
30447 break ;
30450 pvox->vox_bytes = k ;
30452 vox_adpcm_decode_block (pvox) ;
30454 memcpy (&(ptr [indx]), pvox->pcm_data, pvox->pcm_samples * sizeof (short)) ;
30455 indx += pvox->pcm_samples ;
30458 return indx ;
30459 } /* vox_read_block */
30462 static sf_count_t
30463 vox_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
30464 { VOX_ADPCM_PRIVATE *pvox ;
30465 int readcount, count ;
30466 sf_count_t total = 0 ;
30468 if (! psf->fdata)
30469 return 0 ;
30470 pvox = (VOX_ADPCM_PRIVATE*) psf->fdata ;
30472 while (len > 0)
30473 { readcount = (len > 0x10000000) ? 0x10000000 : (int) len ;
30475 count = vox_read_block (psf, pvox, ptr, readcount) ;
30477 total += count ;
30478 len -= count ;
30479 if (count != readcount)
30480 break ;
30483 return total ;
30484 } /* vox_read_s */
30486 static sf_count_t
30487 vox_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
30488 { VOX_ADPCM_PRIVATE *pvox ;
30489 short *sptr ;
30490 int k, bufferlen, readcount, count ;
30491 sf_count_t total = 0 ;
30493 if (! psf->fdata)
30494 return 0 ;
30495 pvox = (VOX_ADPCM_PRIVATE*) psf->fdata ;
30497 sptr = (short*) psf->buffer ;
30498 bufferlen = SF_BUFFER_LEN / sizeof (short) ;
30499 while (len > 0)
30500 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
30501 count = vox_read_block (psf, pvox, sptr, readcount) ;
30502 for (k = 0 ; k < readcount ; k++)
30503 ptr [total + k] = ((int) sptr [k]) << 16 ;
30504 total += count ;
30505 len -= readcount ;
30506 if (count != readcount)
30507 break ;
30510 return total ;
30511 } /* vox_read_i */
30513 static sf_count_t
30514 vox_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
30515 { VOX_ADPCM_PRIVATE *pvox ;
30516 short *sptr ;
30517 int k, bufferlen, readcount, count ;
30518 sf_count_t total = 0 ;
30519 float normfact ;
30521 if (! psf->fdata)
30522 return 0 ;
30523 pvox = (VOX_ADPCM_PRIVATE*) psf->fdata ;
30525 normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x8000) : 1.0 ;
30527 sptr = (short*) psf->buffer ;
30528 bufferlen = SF_BUFFER_LEN / sizeof (short) ;
30529 while (len > 0)
30530 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
30531 count = vox_read_block (psf, pvox, sptr, readcount) ;
30532 for (k = 0 ; k < readcount ; k++)
30533 ptr [total + k] = normfact * (float) (sptr [k]) ;
30534 total += count ;
30535 len -= readcount ;
30536 if (count != readcount)
30537 break ;
30540 return total ;
30541 } /* vox_read_f */
30543 static sf_count_t
30544 vox_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
30545 { VOX_ADPCM_PRIVATE *pvox ;
30546 short *sptr ;
30547 int k, bufferlen, readcount, count ;
30548 sf_count_t total = 0 ;
30549 double normfact ;
30551 if (! psf->fdata)
30552 return 0 ;
30553 pvox = (VOX_ADPCM_PRIVATE*) psf->fdata ;
30555 normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x8000) : 1.0 ;
30557 sptr = (short*) psf->buffer ;
30558 bufferlen = SF_BUFFER_LEN / sizeof (short) ;
30559 while (len > 0)
30560 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
30561 count = vox_read_block (psf, pvox, sptr, readcount) ;
30562 for (k = 0 ; k < readcount ; k++)
30563 ptr [total + k] = normfact * (double) (sptr [k]) ;
30564 total += count ;
30565 len -= readcount ;
30566 if (count != readcount)
30567 break ;
30570 return total ;
30571 } /* vox_read_d */
30573 /*------------------------------------------------------------------------------
30576 static int
30577 vox_write_block (SF_PRIVATE *psf, VOX_ADPCM_PRIVATE *pvox, short *ptr, int len)
30578 { int indx = 0, k ;
30580 while (indx < len)
30581 { pvox->pcm_samples = (len - indx > PCM_DATA_LEN) ? PCM_DATA_LEN : len - indx ;
30583 memcpy (pvox->pcm_data, &(ptr [indx]), pvox->pcm_samples * sizeof (short)) ;
30585 vox_adpcm_encode_block (pvox) ;
30587 if ((k = psf_fwrite (pvox->vox_data, 1, pvox->vox_bytes, psf)) != pvox->vox_bytes)
30588 psf_log_printf (psf, "*** Warning : short read (%d != %d).\n", k, pvox->vox_bytes) ;
30590 indx += pvox->pcm_samples ;
30593 return indx ;
30594 } /* vox_write_block */
30596 static sf_count_t
30597 vox_write_s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
30598 { VOX_ADPCM_PRIVATE *pvox ;
30599 int writecount, count ;
30600 sf_count_t total = 0 ;
30602 if (! psf->fdata)
30603 return 0 ;
30604 pvox = (VOX_ADPCM_PRIVATE*) psf->fdata ;
30606 while (len)
30607 { writecount = (len > 0x10000000) ? 0x10000000 : (int) len ;
30609 count = vox_write_block (psf, pvox, ptr, writecount) ;
30611 total += count ;
30612 len -= count ;
30613 if (count != writecount)
30614 break ;
30617 return total ;
30618 } /* vox_write_s */
30620 static sf_count_t
30621 vox_write_i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
30622 { VOX_ADPCM_PRIVATE *pvox ;
30623 short *sptr ;
30624 int k, bufferlen, writecount, count ;
30625 sf_count_t total = 0 ;
30627 if (! psf->fdata)
30628 return 0 ;
30629 pvox = (VOX_ADPCM_PRIVATE*) psf->fdata ;
30631 sptr = (short*) psf->buffer ;
30632 bufferlen = SF_BUFFER_LEN / sizeof (short) ;
30633 while (len > 0)
30634 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
30635 for (k = 0 ; k < writecount ; k++)
30636 sptr [k] = ptr [total + k] >> 16 ;
30637 count = vox_write_block (psf, pvox, sptr, writecount) ;
30638 total += count ;
30639 len -= writecount ;
30640 if (count != writecount)
30641 break ;
30644 return total ;
30645 } /* vox_write_i */
30647 static sf_count_t
30648 vox_write_f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
30649 { VOX_ADPCM_PRIVATE *pvox ;
30650 short *sptr ;
30651 int k, bufferlen, writecount, count ;
30652 sf_count_t total = 0 ;
30653 float normfact ;
30655 if (! psf->fdata)
30656 return 0 ;
30657 pvox = (VOX_ADPCM_PRIVATE*) psf->fdata ;
30659 normfact = (psf->norm_float == SF_TRUE) ? (1.0 * 0x7FFF) : 1.0 ;
30661 sptr = (short*) psf->buffer ;
30662 bufferlen = SF_BUFFER_LEN / sizeof (short) ;
30663 while (len > 0)
30664 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
30665 for (k = 0 ; k < writecount ; k++)
30666 sptr [k] = lrintf (normfact * ptr [total + k]) ;
30667 count = vox_write_block (psf, pvox, sptr, writecount) ;
30668 total += count ;
30669 len -= writecount ;
30670 if (count != writecount)
30671 break ;
30674 return total ;
30675 } /* vox_write_f */
30677 static sf_count_t
30678 vox_write_d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
30679 { VOX_ADPCM_PRIVATE *pvox ;
30680 short *sptr ;
30681 int k, bufferlen, writecount, count ;
30682 sf_count_t total = 0 ;
30683 double normfact ;
30685 if (! psf->fdata)
30686 return 0 ;
30687 pvox = (VOX_ADPCM_PRIVATE*) psf->fdata ;
30689 normfact = (psf->norm_double == SF_TRUE) ? (1.0 * 0x7FFF) : 1.0 ;
30691 sptr = (short*) psf->buffer ;
30692 bufferlen = SF_BUFFER_LEN / sizeof (short) ;
30693 while (len > 0)
30694 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
30695 for (k = 0 ; k < writecount ; k++)
30696 sptr [k] = lrint (normfact * ptr [total + k]) ;
30697 count = vox_write_block (psf, pvox, sptr, writecount) ;
30698 total += count ;
30699 len -= writecount ;
30700 if (count != writecount)
30701 break ;
30704 return total ;
30705 } /* vox_write_d */
30709 ** Do not edit or modify anything in this comment block.
30710 ** The arch-tag line is a file identity tag for the GNU Arch
30711 ** revision control system.
30713 ** arch-tag: e15e97fe-ff9d-4b46-a489-7059fb2d0b1e
30716 ** Copyright (C) 1999-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
30718 ** This program is free software; you can redistribute it and/or modify
30719 ** it under the terms of the GNU Lesser General Public License as published by
30720 ** the Free Software Foundation; either version 2.1 of the License, or
30721 ** (at your option) any later version.
30723 ** This program is distributed in the hope that it will be useful,
30724 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
30725 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30726 ** GNU Lesser General Public License for more details.
30728 ** You should have received a copy of the GNU Lesser General Public License
30729 ** along with this program; if not, write to the Free Software
30730 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30733 #include <stdio.h>
30734 #include <string.h>
30735 #include <ctype.h>
30736 #include <time.h>
30739 /*------------------------------------------------------------------------------
30740 ** W64 files use 16 byte markers as opposed to the four byte marker of
30741 ** WAV files.
30742 ** For comparison purposes, an integer is required, so make an integer
30743 ** hash for the 16 bytes using MAKE_HASH16 macro, but also create a 16
30744 ** byte array containing the complete 16 bytes required when writing the
30745 ** header.
30748 #define MAKE_HASH16(x0,x1,x2,x3,x4,x5,x6,x7,x8,x9,xa,xb,xc,xd,xe,xf) \
30749 ( (x0) ^ ((x1) << 1) ^ ((x2) << 2) ^ ((x3) << 3) ^ \
30750 ((x4) << 4) ^ ((x5) << 5) ^ ((x6) << 6) ^ ((x7) << 7) ^ \
30751 ((x8) << 8) ^ ((x9) << 9) ^ ((xa) << 10) ^ ((xb) << 11) ^ \
30752 ((xc) << 12) ^ ((xd) << 13) ^ ((xe) << 14) ^ ((xf) << 15) )
30754 #define MAKE_MARKER16(name,x0,x1,x2,x3,x4,x5,x6,x7,x8,x9,xa,xb,xc,xd,xe,xf) \
30755 static unsigned char name [16] = { (x0), (x1), (x2), (x3), (x4), (x5), \
30756 (x6), (x7), (x8), (x9), (xa), (xb), (xc), (xd), (xe), (xf) }
30758 #define riff_HASH16 MAKE_HASH16 ('r', 'i', 'f', 'f', 0x2E, 0x91, 0xCF, 0x11, 0xA5, \
30759 0xD6, 0x28, 0xDB, 0x04, 0xC1, 0x00, 0x00)
30761 #define wave_HASH16 MAKE_HASH16 ('w', 'a', 'v', 'e', 0xF3, 0xAC, 0xD3, 0x11, \
30762 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A)
30764 #define fmt_HASH16 MAKE_HASH16 ('f', 'm', 't', ' ', 0xF3, 0xAC, 0xD3, 0x11, \
30765 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A)
30767 #define fact_HASH16 MAKE_HASH16 ('f', 'a', 'c', 't', 0xF3, 0xAC, 0xD3, 0x11, \
30768 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A)
30770 #define data_HASH16 MAKE_HASH16 ('d', 'a', 't', 'a', 0xF3, 0xAC, 0xD3, 0x11, \
30771 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A)
30773 #define ACID_HASH16 MAKE_HASH16 (0x6D, 0x07, 0x1C, 0xEA, 0xA3, 0xEF, 0x78, 0x4C, \
30774 0x90, 0x57, 0x7F, 0x79, 0xEE, 0x25, 0x2A, 0xAE)
30776 MAKE_MARKER16 (riff_MARKER16, 'r', 'i', 'f', 'f', 0x2E, 0x91, 0xCF, 0x11,
30777 0xA5, 0xD6, 0x28, 0xDB, 0x04, 0xC1, 0x00, 0x00) ;
30780 MAKE_MARKER16 (wave_MARKER16, 'w', 'a', 'v', 'e', 0xF3, 0xAC, 0xD3, 0x11,
30781 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A) ;
30783 MAKE_MARKER16 (fmt_MARKER16, 'f', 'm', 't', ' ', 0xF3, 0xAC, 0xD3, 0x11,
30784 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A) ;
30786 MAKE_MARKER16 (fact_MARKER16, 'f', 'a', 'c', 't', 0xF3, 0xAC, 0xD3, 0x11,
30787 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A) ;
30789 MAKE_MARKER16 (data_MARKER16, 'd', 'a', 't', 'a', 0xF3, 0xAC, 0xD3, 0x11,
30790 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A) ;
30792 enum
30793 { HAVE_riff = 0x01,
30794 HAVE_wave = 0x02,
30795 w64HAVE_fmt = 0x04,
30796 w64HAVE_fact = 0x08,
30797 w64HAVE_data = 0x20
30800 /*------------------------------------------------------------------------------
30801 * Private static functions.
30804 static int w64_read_header (SF_PRIVATE *psf, int *blockalign, int *framesperblock) ;
30805 static int w64_write_header (SF_PRIVATE *psf, int calc_length) ;
30806 static int w64_close (SF_PRIVATE *psf) ;
30808 /*------------------------------------------------------------------------------
30809 ** Public function.
30813 w64_open (SF_PRIVATE *psf)
30814 { int subformat, error, blockalign = 0, framesperblock = 0 ;
30816 if (psf->mode == SFM_READ || (psf->mode == SFM_RDWR &&psf->filelength > 0))
30817 { if ((error = w64_read_header (psf, &blockalign, &framesperblock)))
30818 return error ;
30821 if ((psf->sf.format & SF_FORMAT_TYPEMASK) != SF_FORMAT_W64)
30822 return SFE_BAD_OPEN_FORMAT ;
30824 subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
30826 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
30827 { if (psf->is_pipe)
30828 return SFE_NO_PIPE_WRITE ;
30830 psf->endian = SF_ENDIAN_LITTLE ; /* All W64 files are little endian. */
30832 psf->blockwidth = psf->bytewidth * psf->sf.channels ;
30834 if (subformat == SF_FORMAT_IMA_ADPCM || subformat == SF_FORMAT_MS_ADPCM)
30835 { blockalign = wav_w64_srate2blocksize (psf->sf.samplerate * psf->sf.channels) ;
30836 framesperblock = -1 ;
30838 /* FIXME : This block must go */
30839 psf->filelength = SF_COUNT_MAX ;
30840 psf->datalength = psf->filelength ;
30841 if (psf->sf.frames <= 0)
30842 psf->sf.frames = (psf->blockwidth) ? psf->filelength / psf->blockwidth : psf->filelength ;
30843 /* EMXIF : This block must go */
30846 if ((error = w64_write_header (psf, SF_FALSE)))
30847 return error ;
30849 psf->write_header = w64_write_header ;
30852 psf->close = w64_close ;
30854 switch (subformat)
30855 { case SF_FORMAT_PCM_U8 :
30856 error = pcm_init (psf) ;
30857 break ;
30859 case SF_FORMAT_PCM_16 :
30860 case SF_FORMAT_PCM_24 :
30861 case SF_FORMAT_PCM_32 :
30862 error = pcm_init (psf) ;
30863 break ;
30865 case SF_FORMAT_ULAW :
30866 error = ulaw_init (psf) ;
30867 break ;
30869 case SF_FORMAT_ALAW :
30870 error = alaw_init (psf) ;
30871 break ;
30873 /* Lite remove start */
30874 case SF_FORMAT_FLOAT :
30875 error = float32_init (psf) ;
30876 break ;
30878 case SF_FORMAT_DOUBLE :
30879 error = double64_init (psf) ;
30880 break ;
30882 case SF_FORMAT_IMA_ADPCM :
30883 error = wav_w64_ima_init (psf, blockalign, framesperblock) ;
30884 break ;
30886 case SF_FORMAT_MS_ADPCM :
30887 error = wav_w64_msadpcm_init (psf, blockalign, framesperblock) ;
30888 break ;
30889 /* Lite remove end */
30891 case SF_FORMAT_GSM610 :
30892 error = gsm610_init (psf) ;
30893 break ;
30895 default : return SFE_UNIMPLEMENTED ;
30898 return error ;
30899 } /* w64_open */
30901 /*=========================================================================
30902 ** Private functions.
30905 static int
30906 w64_read_header (SF_PRIVATE *psf, int *blockalign, int *framesperblock)
30907 { WAV_FMT wav_fmt ;
30908 int dword = 0, marker, format = 0 ;
30909 sf_count_t chunk_size, bytesread = 0 ;
30910 int parsestage = 0, error, done = 0 ;
30912 /* Set position to start of file to begin reading header. */
30913 psf_binheader_readf (psf, "p", 0) ;
30915 while (! done)
30916 { /* Read the 4 byte marker and jump 12 bytes. */
30917 bytesread += psf_binheader_readf (psf, "h", &marker) ;
30918 chunk_size = 0 ;
30920 switch (marker)
30921 { case riff_HASH16 :
30922 if (parsestage)
30923 return SFE_W64_NO_RIFF ;
30925 bytesread += psf_binheader_readf (psf, "e8", &chunk_size) ;
30927 if (psf->filelength < chunk_size)
30928 psf_log_printf (psf, "riff : %D (should be %D)\n", chunk_size, psf->filelength) ;
30929 else
30930 psf_log_printf (psf, "riff : %D\n", chunk_size) ;
30932 parsestage |= HAVE_riff ;
30933 break ;
30935 case ACID_HASH16:
30936 psf_log_printf (psf, "Looks like an ACID file. Exiting.\n") ;
30937 return SFE_UNIMPLEMENTED ;
30939 case wave_HASH16 :
30940 if ((parsestage & HAVE_riff) != HAVE_riff)
30941 return SFE_W64_NO_WAVE ;
30942 psf_log_printf (psf, "wave\n") ;
30943 parsestage |= HAVE_wave ;
30944 break ;
30946 case fmt_HASH16 :
30947 if ((parsestage & (HAVE_riff | HAVE_wave)) != (HAVE_riff | HAVE_wave))
30948 return SFE_W64_NO_FMT ;
30950 bytesread += psf_binheader_readf (psf, "e8", &chunk_size) ;
30951 psf_log_printf (psf, " fmt : %D\n", chunk_size) ;
30953 /* size of 16 byte marker and 8 byte chunk_size value. */
30954 chunk_size -= 24 ;
30956 if ((error = wav_w64_read_fmt_chunk (psf, &wav_fmt, (int) chunk_size)))
30957 return error ;
30959 if (chunk_size % 8)
30960 psf_binheader_readf (psf, "j", 8 - (chunk_size % 8)) ;
30962 format = wav_fmt.format ;
30963 parsestage |= w64HAVE_fmt ;
30964 break ;
30966 case fact_HASH16:
30967 { sf_count_t frames ;
30969 psf_binheader_readf (psf, "e88", &chunk_size, &frames) ;
30970 psf_log_printf (psf, " fact : %D\n frames : %D\n",
30971 chunk_size, frames) ;
30973 break ;
30976 case data_HASH16 :
30977 if ((parsestage & (HAVE_riff | HAVE_wave | w64HAVE_fmt)) != (HAVE_riff | HAVE_wave | w64HAVE_fmt))
30978 return SFE_W64_NO_DATA ;
30980 psf_binheader_readf (psf, "e8", &chunk_size) ;
30982 psf->dataoffset = psf_ftell (psf) ;
30984 psf->datalength = chunk_size - 24 ;
30986 if (chunk_size % 8)
30987 chunk_size += 8 - (chunk_size % 8) ;
30989 psf_log_printf (psf, "data : %D\n", chunk_size) ;
30991 parsestage |= w64HAVE_data ;
30993 if (! psf->sf.seekable)
30994 break ;
30996 /* Seek past data and continue reading header. */
30997 psf_fseek (psf, chunk_size, SEEK_CUR) ;
30998 break ;
31000 default :
31001 if (psf_ftell (psf) & 0x0F)
31002 { psf_log_printf (psf, " Unknown chunk marker at position %d. Resynching.\n", dword - 4) ;
31003 psf_binheader_readf (psf, "j", -3) ;
31004 break ;
31006 psf_log_printf (psf, "*** Unknown chunk marker : %X. Exiting parser.\n", marker) ;
31007 done = SF_TRUE ;
31008 break ;
31009 } ; /* switch (dword) */
31011 if (psf->sf.seekable == 0 && (parsestage & w64HAVE_data))
31012 break ;
31014 if (psf_ftell (psf) >= (psf->filelength - (2 * SIGNED_SIZEOF (dword))))
31015 break ;
31017 if (psf->logindex >= SIGNED_SIZEOF (psf->logbuffer) - 2)
31018 return SFE_LOG_OVERRUN ;
31019 } ; /* while (1) */
31021 if (! psf->dataoffset)
31022 return SFE_W64_NO_DATA ;
31024 psf->endian = SF_ENDIAN_LITTLE ; /* All WAV files are little endian. */
31026 if (psf_ftell (psf) != psf->dataoffset)
31027 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
31029 psf->close = w64_close ;
31031 if (psf->blockwidth)
31032 { if (psf->filelength - psf->dataoffset < psf->datalength)
31033 psf->sf.frames = (psf->filelength - psf->dataoffset) / psf->blockwidth ;
31034 else
31035 psf->sf.frames = psf->datalength / psf->blockwidth ;
31038 switch (format)
31039 { case WAVE_FORMAT_PCM :
31040 case WAVE_FORMAT_EXTENSIBLE :
31041 /* extensible might be FLOAT, MULAW, etc as well! */
31042 psf->sf.format = SF_FORMAT_W64 | u_bitwidth_to_subformat (psf->bytewidth * 8) ;
31043 break ;
31045 case WAVE_FORMAT_MULAW :
31046 psf->sf.format = (SF_FORMAT_W64 | SF_FORMAT_ULAW) ;
31047 break ;
31049 case WAVE_FORMAT_ALAW :
31050 psf->sf.format = (SF_FORMAT_W64 | SF_FORMAT_ALAW) ;
31051 break ;
31053 case WAVE_FORMAT_MS_ADPCM :
31054 psf->sf.format = (SF_FORMAT_W64 | SF_FORMAT_MS_ADPCM) ;
31055 *blockalign = wav_fmt.msadpcm.blockalign ;
31056 *framesperblock = wav_fmt.msadpcm.samplesperblock ;
31057 break ;
31059 case WAVE_FORMAT_IMA_ADPCM :
31060 psf->sf.format = (SF_FORMAT_W64 | SF_FORMAT_IMA_ADPCM) ;
31061 *blockalign = wav_fmt.ima.blockalign ;
31062 *framesperblock = wav_fmt.ima.samplesperblock ;
31063 break ;
31065 case WAVE_FORMAT_GSM610 :
31066 psf->sf.format = (SF_FORMAT_W64 | SF_FORMAT_GSM610) ;
31067 break ;
31069 case WAVE_FORMAT_IEEE_FLOAT :
31070 psf->sf.format = SF_FORMAT_W64 ;
31071 psf->sf.format |= (psf->bytewidth == 8) ? SF_FORMAT_DOUBLE : SF_FORMAT_FLOAT ;
31072 break ;
31074 default : return SFE_UNIMPLEMENTED ;
31077 return 0 ;
31078 } /* w64_read_header */
31080 static int
31081 w64_write_header (SF_PRIVATE *psf, int calc_length)
31082 { sf_count_t fmt_size, current ;
31083 size_t fmt_pad = 0 ;
31084 int subformat, add_fact_chunk = SF_FALSE ;
31086 current = psf_ftell (psf) ;
31088 if (calc_length)
31089 { psf->filelength = psf_get_filelen (psf) ;
31091 psf->datalength = psf->filelength - psf->dataoffset ;
31092 if (psf->dataend)
31093 psf->datalength -= psf->filelength - psf->dataend ;
31095 if (psf->bytewidth)
31096 psf->sf.frames = psf->datalength / (psf->bytewidth * psf->sf.channels) ;
31099 /* Reset the current header length to zero. */
31100 psf->header [0] = 0 ;
31101 psf->headindex = 0 ;
31102 psf_fseek (psf, 0, SEEK_SET) ;
31104 /* riff marker, length, wave and 'fmt ' markers. */
31105 psf_binheader_writef (psf, "eh8hh", riff_MARKER16, psf->filelength - 8, wave_MARKER16, fmt_MARKER16) ;
31107 subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
31109 switch (subformat)
31110 { case SF_FORMAT_PCM_U8 :
31111 case SF_FORMAT_PCM_16 :
31112 case SF_FORMAT_PCM_24 :
31113 case SF_FORMAT_PCM_32 :
31114 fmt_size = 24 + 2 + 2 + 4 + 4 + 2 + 2 ;
31115 fmt_pad = (size_t) (8 - (fmt_size & 0x7)) ;
31116 fmt_size += fmt_pad ;
31118 /* fmt : format, channels, samplerate */
31119 psf_binheader_writef (psf, "e8224", fmt_size, WAVE_FORMAT_PCM, psf->sf.channels, psf->sf.samplerate) ;
31120 /* fmt : bytespersec */
31121 psf_binheader_writef (psf, "e4", psf->sf.samplerate * psf->bytewidth * psf->sf.channels) ;
31122 /* fmt : blockalign, bitwidth */
31123 psf_binheader_writef (psf, "e22", psf->bytewidth * psf->sf.channels, psf->bytewidth * 8) ;
31124 break ;
31126 case SF_FORMAT_FLOAT :
31127 case SF_FORMAT_DOUBLE :
31128 fmt_size = 24 + 2 + 2 + 4 + 4 + 2 + 2 ;
31129 fmt_pad = (size_t) (8 - (fmt_size & 0x7)) ;
31130 fmt_size += fmt_pad ;
31132 /* fmt : format, channels, samplerate */
31133 psf_binheader_writef (psf, "e8224", fmt_size, WAVE_FORMAT_IEEE_FLOAT, psf->sf.channels, psf->sf.samplerate) ;
31134 /* fmt : bytespersec */
31135 psf_binheader_writef (psf, "e4", psf->sf.samplerate * psf->bytewidth * psf->sf.channels) ;
31136 /* fmt : blockalign, bitwidth */
31137 psf_binheader_writef (psf, "e22", psf->bytewidth * psf->sf.channels, psf->bytewidth * 8) ;
31139 add_fact_chunk = SF_TRUE ;
31140 break ;
31142 case SF_FORMAT_ULAW :
31143 fmt_size = 24 + 2 + 2 + 4 + 4 + 2 + 2 ;
31144 fmt_pad = (size_t) (8 - (fmt_size & 0x7)) ;
31145 fmt_size += fmt_pad ;
31147 /* fmt : format, channels, samplerate */
31148 psf_binheader_writef (psf, "e8224", fmt_size, WAVE_FORMAT_MULAW, psf->sf.channels, psf->sf.samplerate) ;
31149 /* fmt : bytespersec */
31150 psf_binheader_writef (psf, "e4", psf->sf.samplerate * psf->bytewidth * psf->sf.channels) ;
31151 /* fmt : blockalign, bitwidth */
31152 psf_binheader_writef (psf, "e22", psf->bytewidth * psf->sf.channels, 8) ;
31154 add_fact_chunk = SF_TRUE ;
31155 break ;
31157 case SF_FORMAT_ALAW :
31158 fmt_size = 24 + 2 + 2 + 4 + 4 + 2 + 2 ;
31159 fmt_pad = (size_t) (8 - (fmt_size & 0x7)) ;
31160 fmt_size += fmt_pad ;
31162 /* fmt : format, channels, samplerate */
31163 psf_binheader_writef (psf, "e8224", fmt_size, WAVE_FORMAT_ALAW, psf->sf.channels, psf->sf.samplerate) ;
31164 /* fmt : bytespersec */
31165 psf_binheader_writef (psf, "e4", psf->sf.samplerate * psf->bytewidth * psf->sf.channels) ;
31166 /* fmt : blockalign, bitwidth */
31167 psf_binheader_writef (psf, "e22", psf->bytewidth * psf->sf.channels, 8) ;
31169 add_fact_chunk = SF_TRUE ;
31170 break ;
31172 /* Lite remove start */
31173 case SF_FORMAT_IMA_ADPCM :
31174 { int blockalign, framesperblock, bytespersec ;
31176 blockalign = wav_w64_srate2blocksize (psf->sf.samplerate * psf->sf.channels) ;
31177 framesperblock = 2 * (blockalign - 4 * psf->sf.channels) / psf->sf.channels + 1 ;
31178 bytespersec = (psf->sf.samplerate * blockalign) / framesperblock ;
31180 /* fmt chunk. */
31181 fmt_size = 24 + 2 + 2 + 4 + 4 + 2 + 2 + 2 + 2 ;
31182 fmt_pad = (size_t) (8 - (fmt_size & 0x7)) ;
31183 fmt_size += fmt_pad ;
31185 /* fmt : size, WAV format type, channels. */
31186 psf_binheader_writef (psf, "e822", fmt_size, WAVE_FORMAT_IMA_ADPCM, psf->sf.channels) ;
31188 /* fmt : samplerate, bytespersec. */
31189 psf_binheader_writef (psf, "e44", psf->sf.samplerate, bytespersec) ;
31191 /* fmt : blockalign, bitwidth, extrabytes, framesperblock. */
31192 psf_binheader_writef (psf, "e2222", blockalign, 4, 2, framesperblock) ;
31195 add_fact_chunk = SF_TRUE ;
31196 break ;
31198 case SF_FORMAT_MS_ADPCM :
31199 { int blockalign, framesperblock, bytespersec, extrabytes ;
31201 blockalign = wav_w64_srate2blocksize (psf->sf.samplerate * psf->sf.channels) ;
31202 framesperblock = 2 + 2 * (blockalign - 7 * psf->sf.channels) / psf->sf.channels ;
31203 bytespersec = (psf->sf.samplerate * blockalign) / framesperblock ;
31205 /* fmt chunk. */
31206 extrabytes = 2 + 2 + MSADPCM_ADAPT_COEFF_COUNT * (2 + 2) ;
31207 fmt_size = 24 + 2 + 2 + 4 + 4 + 2 + 2 + 2 + extrabytes ;
31208 fmt_pad = (size_t) (8 - (fmt_size & 0x7)) ;
31209 fmt_size += fmt_pad ;
31211 /* fmt : size, W64 format type, channels. */
31212 psf_binheader_writef (psf, "e822", fmt_size, WAVE_FORMAT_MS_ADPCM, psf->sf.channels) ;
31214 /* fmt : samplerate, bytespersec. */
31215 psf_binheader_writef (psf, "e44", psf->sf.samplerate, bytespersec) ;
31217 /* fmt : blockalign, bitwidth, extrabytes, framesperblock. */
31218 psf_binheader_writef (psf, "e22222", blockalign, 4, extrabytes, framesperblock, 7) ;
31220 msadpcm_write_adapt_coeffs (psf) ;
31223 add_fact_chunk = SF_TRUE ;
31224 break ;
31225 /* Lite remove end */
31227 case SF_FORMAT_GSM610 :
31228 { int bytespersec ;
31230 bytespersec = (psf->sf.samplerate * WAV_W64_GSM610_BLOCKSIZE) / WAV_W64_GSM610_SAMPLES ;
31232 /* fmt chunk. */
31233 fmt_size = 24 + 2 + 2 + 4 + 4 + 2 + 2 + 2 + 2 ;
31234 fmt_pad = (size_t) (8 - (fmt_size & 0x7)) ;
31235 fmt_size += fmt_pad ;
31237 /* fmt : size, WAV format type, channels. */
31238 psf_binheader_writef (psf, "e822", fmt_size, WAVE_FORMAT_GSM610, psf->sf.channels) ;
31240 /* fmt : samplerate, bytespersec. */
31241 psf_binheader_writef (psf, "e44", psf->sf.samplerate, bytespersec) ;
31243 /* fmt : blockalign, bitwidth, extrabytes, framesperblock. */
31244 psf_binheader_writef (psf, "e2222", WAV_W64_GSM610_BLOCKSIZE, 0, 2, WAV_W64_GSM610_SAMPLES) ;
31247 add_fact_chunk = SF_TRUE ;
31248 break ;
31250 default : return SFE_UNIMPLEMENTED ;
31253 /* Pad to 8 bytes with zeros. */
31254 if (fmt_pad > 0)
31255 psf_binheader_writef (psf, "z", fmt_pad) ;
31257 if (add_fact_chunk)
31258 psf_binheader_writef (psf, "eh88", fact_MARKER16, 16 + 8 + 8, psf->sf.frames) ;
31260 psf_binheader_writef (psf, "eh8", data_MARKER16, psf->datalength + 24) ;
31261 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
31263 if (psf->error)
31264 return psf->error ;
31266 psf->dataoffset = psf->headindex ;
31268 if (current > 0)
31269 psf_fseek (psf, current, SEEK_SET) ;
31271 return psf->error ;
31272 } /* w64_write_header */
31274 static int
31275 w64_close (SF_PRIVATE *psf)
31277 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
31278 w64_write_header (psf, SF_TRUE) ;
31280 return 0 ;
31281 } /* w64_close */
31285 ** Do not edit or modify anything in this comment block.
31286 ** The arch-tag line is a file identity tag for the GNU Arch
31287 ** revision control system.
31289 ** arch-tag: 9aa4e141-538a-4dd9-99c9-b3f0f2dd4f4a
31292 ** Copyright (C) 1999-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
31293 ** Copyright (C) 2004 David Viens <davidv@plogue.com>
31295 ** This program is free software; you can redistribute it and/or modify
31296 ** it under the terms of the GNU Lesser General Public License as published by
31297 ** the Free Software Foundation; either version 2.1 of the License, or
31298 ** (at your option) any later version.
31300 ** This program is distributed in the hope that it will be useful,
31301 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
31302 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31303 ** GNU Lesser General Public License for more details.
31305 ** You should have received a copy of the GNU Lesser General Public License
31306 ** along with this program; if not, write to the Free Software
31307 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31311 #include <stdio.h>
31312 #include <stdlib.h>
31313 #include <string.h>
31314 #include <ctype.h>
31315 #include <time.h>
31318 /*------------------------------------------------------------------------------
31319 * Macros to handle big/little endian issues.
31322 #define RIFF_MARKER (MAKE_MARKER ('R', 'I', 'F', 'F'))
31323 #define WAVE_MARKER (MAKE_MARKER ('W', 'A', 'V', 'E'))
31324 #define fmt_MARKER (MAKE_MARKER ('f', 'm', 't', ' '))
31325 #define data_MARKER (MAKE_MARKER ('d', 'a', 't', 'a'))
31326 #define fact_MARKER (MAKE_MARKER ('f', 'a', 'c', 't'))
31327 #define PEAK_MARKER (MAKE_MARKER ('P', 'E', 'A', 'K'))
31329 #define cue_MARKER (MAKE_MARKER ('c', 'u', 'e', ' '))
31330 #define LIST_MARKER (MAKE_MARKER ('L', 'I', 'S', 'T'))
31331 #define slnt_MARKER (MAKE_MARKER ('s', 'l', 'n', 't'))
31332 #define wavl_MARKER (MAKE_MARKER ('w', 'a', 'v', 'l'))
31333 #define INFO_MARKER (MAKE_MARKER ('I', 'N', 'F', 'O'))
31334 #define plst_MARKER (MAKE_MARKER ('p', 'l', 's', 't'))
31335 #define adtl_MARKER (MAKE_MARKER ('a', 'd', 't', 'l'))
31336 #define labl_MARKER (MAKE_MARKER ('l', 'a', 'b', 'l'))
31337 #define ltxt_MARKER (MAKE_MARKER ('l', 't', 'x', 't'))
31338 #define note_MARKER (MAKE_MARKER ('n', 'o', 't', 'e'))
31339 #define smpl_MARKER (MAKE_MARKER ('s', 'm', 'p', 'l'))
31340 #define bext_MARKER (MAKE_MARKER ('b', 'e', 'x', 't'))
31341 #define MEXT_MARKER (MAKE_MARKER ('M', 'E', 'X', 'T'))
31342 #define DISP_MARKER (MAKE_MARKER ('D', 'I', 'S', 'P'))
31343 #define acid_MARKER (MAKE_MARKER ('a', 'c', 'i', 'd'))
31344 #define strc_MARKER (MAKE_MARKER ('s', 't', 'r', 'c'))
31345 #define PAD_MARKER (MAKE_MARKER ('P', 'A', 'D', ' '))
31346 #define afsp_MARKER (MAKE_MARKER ('a', 'f', 's', 'p'))
31347 #define clm_MARKER (MAKE_MARKER ('c', 'l', 'm', ' '))
31349 #define ISFT_MARKER (MAKE_MARKER ('I', 'S', 'F', 'T'))
31350 #define ICRD_MARKER (MAKE_MARKER ('I', 'C', 'R', 'D'))
31351 #define ICOP_MARKER (MAKE_MARKER ('I', 'C', 'O', 'P'))
31352 #define IARL_MARKER (MAKE_MARKER ('I', 'A', 'R', 'L'))
31353 #define IART_MARKER (MAKE_MARKER ('I', 'A', 'R', 'T'))
31354 #define INAM_MARKER (MAKE_MARKER ('I', 'N', 'A', 'M'))
31355 #define IENG_MARKER (MAKE_MARKER ('I', 'E', 'N', 'G'))
31356 #define IART_MARKER (MAKE_MARKER ('I', 'A', 'R', 'T'))
31357 #define ICOP_MARKER (MAKE_MARKER ('I', 'C', 'O', 'P'))
31358 #define IPRD_MARKER (MAKE_MARKER ('I', 'P', 'R', 'D'))
31359 #define ISRC_MARKER (MAKE_MARKER ('I', 'S', 'R', 'C'))
31360 #define ISBJ_MARKER (MAKE_MARKER ('I', 'S', 'B', 'J'))
31361 #define ICMT_MARKER (MAKE_MARKER ('I', 'C', 'M', 'T'))
31363 /* Weird WAVPACK marker which can show up at the start of the DATA section. */
31364 #define wvpk_MARKER (MAKE_MARKER ('w', 'v', 'p', 'k'))
31365 #define OggS_MARKER (MAKE_MARKER ('O', 'g', 'g', 'S'))
31367 enum
31368 { HAVE_RIFF = 0x01,
31369 HAVE_WAVE = 0x02,
31370 wavHAVE_fmt = 0x04,
31371 wavHAVE_fact = 0x08,
31372 HAVE_PEAK = 0x10,
31373 wavHAVE_data = 0x20,
31374 HAVE_other = 0x80000000
31379 /* known WAVEFORMATEXTENSIBLE GUIDS */
31380 static const EXT_SUBFORMAT MSGUID_SUBTYPE_PCM =
31381 { 0x00000001, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }
31384 static const EXT_SUBFORMAT MSGUID_SUBTYPE_MS_ADPCM =
31385 { 0x00000002, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }
31388 static const EXT_SUBFORMAT MSGUID_SUBTYPE_IEEE_FLOAT =
31389 { 0x00000003, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }
31392 static const EXT_SUBFORMAT MSGUID_SUBTYPE_ALAW =
31393 { 0x00000006, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }
31396 static const EXT_SUBFORMAT MSGUID_SUBTYPE_MULAW =
31397 { 0x00000007, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }
31400 #if 0
31401 /* maybe interesting one day to read the following through sf_read_raw */
31402 /* http://www.bath.ac.uk/~masrwd/pvocex/pvocex.html */
31403 static const EXT_SUBFORMAT MSGUID_SUBTYPE_PVOCEX =
31404 { 0x8312B9C2, 0x2E6E, 0x11d4, { 0xA8, 0x24, 0xDE, 0x5B, 0x96, 0xC3, 0xAB, 0x21 }
31406 #endif
31408 /*------------------------------------------------------------------------------
31409 ** Private static functions.
31412 static int wav_read_header (SF_PRIVATE *psf, int *blockalign, int *framesperblock) ;
31413 static int wav_write_header (SF_PRIVATE *psf, int calc_length) ;
31415 static void wavex_write_guid (SF_PRIVATE *psf, const EXT_SUBFORMAT * subformat) ;
31416 static int wavex_write_header (SF_PRIVATE *psf, int calc_length) ;
31418 static int wav_write_tailer (SF_PRIVATE *psf) ;
31419 static void wav_write_strings (SF_PRIVATE *psf, int location) ;
31420 static int wav_command (SF_PRIVATE *psf, int command, void *data, int datasize) ;
31421 static int wav_close (SF_PRIVATE *psf) ;
31423 static int wav_subchunk_parse (SF_PRIVATE *psf, int chunk) ;
31424 static int wav_read_smpl_chunk (SF_PRIVATE *psf, unsigned int chunklen) ;
31425 static int wav_read_acid_chunk (SF_PRIVATE *psf, unsigned int chunklen) ;
31427 static int wavex_write_guid_equal (const EXT_SUBFORMAT * first, const EXT_SUBFORMAT * second) ;
31429 /*------------------------------------------------------------------------------
31430 ** Public function.
31434 wav_open (SF_PRIVATE *psf)
31435 { int format, subformat, error, blockalign = 0, framesperblock = 0 ;
31437 if (psf->mode == SFM_READ || (psf->mode == SFM_RDWR && psf->filelength > 0))
31438 { if ((error = wav_read_header (psf, &blockalign, &framesperblock)))
31439 return error ;
31442 subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
31444 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
31445 { if (psf->is_pipe)
31446 return SFE_NO_PIPE_WRITE ;
31448 format = psf->sf.format & SF_FORMAT_TYPEMASK ;
31449 if (format != SF_FORMAT_WAV && format != SF_FORMAT_WAVEX)
31450 return SFE_BAD_OPEN_FORMAT ;
31452 psf->endian = SF_ENDIAN_LITTLE ; /* All WAV files are little endian. */
31454 psf->blockwidth = psf->bytewidth * psf->sf.channels ;
31456 if (psf->mode != SFM_RDWR || psf->filelength < 44)
31457 { psf->filelength = 0 ;
31458 psf->datalength = 0 ;
31459 psf->dataoffset = 0 ;
31460 psf->sf.frames = 0 ;
31463 if (subformat == SF_FORMAT_IMA_ADPCM || subformat == SF_FORMAT_MS_ADPCM)
31464 { blockalign = wav_w64_srate2blocksize (psf->sf.samplerate * psf->sf.channels) ;
31465 framesperblock = -1 ; /* Corrected later. */
31468 psf->str_flags = SF_STR_ALLOW_START | SF_STR_ALLOW_END ;
31470 /* By default, add the peak chunk to floating point files. Default behaviour
31471 ** can be switched off using sf_command (SFC_SET_PEAK_CHUNK, SF_FALSE).
31473 if (psf->mode == SFM_WRITE && (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE))
31474 { psf->pchunk = calloc (1, sizeof (PEAK_CHUNK) * psf->sf.channels * sizeof (PEAK_POS)) ;
31475 if (psf->pchunk == NULL)
31476 return SFE_MALLOC_FAILED ;
31477 psf->has_peak = SF_TRUE ;
31478 psf->peak_loc = SF_PEAK_START ;
31481 psf->write_header = (format == SF_FORMAT_WAV) ? wav_write_header : wavex_write_header ;
31484 psf->close = wav_close ;
31485 psf->command = wav_command ;
31487 switch (subformat)
31488 { case SF_FORMAT_PCM_U8 :
31489 case SF_FORMAT_PCM_16 :
31490 case SF_FORMAT_PCM_24 :
31491 case SF_FORMAT_PCM_32 :
31492 error = pcm_init (psf) ;
31493 break ;
31495 case SF_FORMAT_ULAW :
31496 error = ulaw_init (psf) ;
31497 break ;
31499 case SF_FORMAT_ALAW :
31500 error = alaw_init (psf) ;
31501 break ;
31503 /* Lite remove start */
31504 case SF_FORMAT_FLOAT :
31505 error = float32_init (psf) ;
31506 break ;
31508 case SF_FORMAT_DOUBLE :
31509 error = double64_init (psf) ;
31510 break ;
31512 case SF_FORMAT_IMA_ADPCM :
31513 error = wav_w64_ima_init (psf, blockalign, framesperblock) ;
31514 break ;
31516 case SF_FORMAT_MS_ADPCM :
31517 error = wav_w64_msadpcm_init (psf, blockalign, framesperblock) ;
31518 break ;
31519 /* Lite remove end */
31521 case SF_FORMAT_GSM610 :
31522 error = gsm610_init (psf) ;
31523 break ;
31525 default : return SFE_UNIMPLEMENTED ;
31528 if (psf->mode == SFM_WRITE || (psf->mode == SFM_RDWR && psf->filelength == 0))
31529 return psf->write_header (psf, SF_FALSE) ;
31531 return error ;
31532 } /* wav_open */
31534 /*=========================================================================
31535 ** Private functions.
31538 static int
31539 wav_read_header (SF_PRIVATE *psf, int *blockalign, int *framesperblock)
31540 { WAV_FMT wav_fmt ;
31541 FACT_CHUNK fact_chunk ;
31542 int dword, marker, RIFFsize, done = 0 ;
31543 int parsestage = 0, error, format = 0 ;
31544 char *cptr ;
31546 /* Set position to start of file to begin reading header. */
31547 psf_binheader_readf (psf, "p", 0) ;
31549 while (! done)
31550 { psf_binheader_readf (psf, "m", &marker) ;
31552 switch (marker)
31553 { case RIFF_MARKER :
31554 if (parsestage)
31555 return SFE_WAV_NO_RIFF ;
31557 parsestage |= HAVE_RIFF ;
31559 psf_binheader_readf (psf, "e4", &RIFFsize) ;
31561 if (psf->fileoffset > 0 && psf->filelength > RIFFsize + 8)
31562 { /* Set file length. */
31563 psf->filelength = RIFFsize + 8 ;
31564 psf_log_printf (psf, "RIFF : %u\n", RIFFsize) ;
31566 else if (psf->filelength < RIFFsize + 2 * SIGNED_SIZEOF (dword))
31567 { psf_log_printf (psf, "RIFF : %u (should be %D)\n", RIFFsize, psf->filelength - 2 * SIGNED_SIZEOF (dword)) ;
31568 RIFFsize = dword ;
31570 else
31571 psf_log_printf (psf, "RIFF : %u\n", RIFFsize) ;
31573 break ;
31575 case WAVE_MARKER :
31576 if ((parsestage & HAVE_RIFF) != HAVE_RIFF)
31577 return SFE_WAV_NO_WAVE ;
31578 parsestage |= HAVE_WAVE ;
31580 psf_log_printf (psf, "WAVE\n") ;
31581 break ;
31583 case fmt_MARKER :
31584 if ((parsestage & (HAVE_RIFF | HAVE_WAVE)) != (HAVE_RIFF | HAVE_WAVE))
31585 return SFE_WAV_NO_FMT ;
31587 /* If this file has a SECOND fmt chunk, I don't want to know about it. */
31588 if (parsestage & wavHAVE_fmt)
31589 break ;
31591 parsestage |= wavHAVE_fmt ;
31593 psf_binheader_readf (psf, "e4", &dword) ;
31594 psf_log_printf (psf, "fmt : %d\n", dword) ;
31596 if ((error = wav_w64_read_fmt_chunk (psf, &wav_fmt, dword)))
31597 return error ;
31599 format = wav_fmt.format ;
31600 break ;
31602 case data_MARKER :
31603 if ((parsestage & (HAVE_RIFF | HAVE_WAVE | wavHAVE_fmt)) != (HAVE_RIFF | HAVE_WAVE | wavHAVE_fmt))
31604 return SFE_WAV_NO_DATA ;
31606 if (psf->mode == SFM_RDWR && (parsestage & HAVE_other) != 0)
31607 return SFE_RDWR_BAD_HEADER ;
31609 parsestage |= wavHAVE_data ;
31611 psf_binheader_readf (psf, "e4", &dword) ;
31613 psf->datalength = dword ;
31614 psf->dataoffset = psf_ftell (psf) ;
31616 if (dword == 0 && RIFFsize == 8 && psf->filelength > 44)
31617 { psf_log_printf (psf, "*** Looks like a WAV file which wasn't closed properly. Fixing it.\n") ;
31618 psf->datalength = dword = psf->filelength - psf->dataoffset ;
31621 if (psf->datalength > psf->filelength - psf->dataoffset)
31622 { psf_log_printf (psf, "data : %D (should be %D)\n", psf->datalength, psf->filelength - psf->dataoffset) ;
31623 psf->datalength = psf->filelength - psf->dataoffset ;
31625 else
31626 psf_log_printf (psf, "data : %D\n", psf->datalength) ;
31628 /* Only set dataend if there really is data at the end. */
31629 if (psf->datalength + psf->dataoffset < psf->filelength)
31630 psf->dataend = psf->datalength + psf->dataoffset ;
31632 if (format == WAVE_FORMAT_MS_ADPCM && psf->datalength % 2)
31633 { psf->datalength ++ ;
31634 psf_log_printf (psf, "*** Data length odd. Increasing it by 1.\n") ;
31637 if (! psf->sf.seekable)
31638 break ;
31640 /* Seek past data and continue reading header. */
31641 psf_fseek (psf, psf->datalength, SEEK_CUR) ;
31643 dword = psf_ftell (psf) ;
31644 if (dword != (sf_count_t) (psf->dataoffset + psf->datalength))
31645 psf_log_printf (psf, "*** psf_fseek past end error ***\n", dword, psf->dataoffset + psf->datalength) ;
31646 break ;
31648 case fact_MARKER :
31649 if ((parsestage & (HAVE_RIFF | HAVE_WAVE)) != (HAVE_RIFF | HAVE_WAVE))
31650 return SFE_WAV_BAD_FACT ;
31652 parsestage |= wavHAVE_fact ;
31654 if ((parsestage & wavHAVE_fmt) != wavHAVE_fmt)
31655 psf_log_printf (psf, "*** Should have 'fmt ' chunk before 'fact'\n") ;
31657 psf_binheader_readf (psf, "e44", &dword, & (fact_chunk.frames)) ;
31659 if (dword > SIGNED_SIZEOF (fact_chunk))
31660 psf_binheader_readf (psf, "j", (int) (dword - SIGNED_SIZEOF (fact_chunk))) ;
31662 if (dword)
31663 psf_log_printf (psf, "%M : %d\n", marker, dword) ;
31664 else
31665 psf_log_printf (psf, "%M : %d (should not be zero)\n", marker, dword) ;
31667 psf_log_printf (psf, " frames : %d\n", fact_chunk.frames) ;
31668 break ;
31670 case PEAK_MARKER :
31671 if ((parsestage & (HAVE_RIFF | HAVE_WAVE | wavHAVE_fmt)) != (HAVE_RIFF | HAVE_WAVE | wavHAVE_fmt))
31672 return SFE_WAV_PEAK_B4_FMT ;
31674 parsestage |= HAVE_PEAK ;
31676 psf_binheader_readf (psf, "e4", &dword) ;
31678 psf_log_printf (psf, "%M : %d\n", marker, dword) ;
31679 if (dword != SIGNED_SIZEOF (PEAK_CHUNK) + psf->sf.channels * SIGNED_SIZEOF (PEAK_POS))
31680 { psf_binheader_readf (psf, "j", dword) ;
31681 psf_log_printf (psf, "*** File PEAK chunk size doesn't fit with number of channels.\n") ;
31682 return SFE_WAV_BAD_PEAK ;
31685 psf->pchunk = calloc (1, sizeof (PEAK_CHUNK) * psf->sf.channels * sizeof (PEAK_POS)) ;
31686 if (psf->pchunk == NULL)
31687 return SFE_MALLOC_FAILED ;
31689 /* read in rest of PEAK chunk. */
31690 psf_binheader_readf (psf, "e44", & (psf->pchunk->version), & (psf->pchunk->timestamp)) ;
31692 if (psf->pchunk->version != 1)
31693 psf_log_printf (psf, " version : %d *** (should be version 1)\n", psf->pchunk->version) ;
31694 else
31695 psf_log_printf (psf, " version : %d\n", psf->pchunk->version) ;
31697 psf_log_printf (psf, " time stamp : %d\n", psf->pchunk->timestamp) ;
31698 psf_log_printf (psf, " Ch Position Value\n") ;
31700 cptr = (char *) psf->buffer ;
31701 for (dword = 0 ; dword < psf->sf.channels ; dword++)
31702 { psf_binheader_readf (psf, "ef4", & (psf->pchunk->peaks [dword].value),
31703 & (psf->pchunk->peaks [dword].position)) ;
31705 LSF_SNPRINTF (cptr, sizeof (psf->buffer), " %2d %-12d %g\n",
31706 dword, psf->pchunk->peaks [dword].position, psf->pchunk->peaks [dword].value) ;
31707 cptr [sizeof (psf->buffer) - 1] = 0 ;
31708 psf_log_printf (psf, cptr) ;
31711 psf->has_peak = SF_TRUE ; /* Found PEAK chunk. */
31712 psf->peak_loc = ((parsestage & wavHAVE_data) == 0) ? SF_PEAK_START : SF_PEAK_END ;
31713 break ;
31715 case cue_MARKER :
31716 parsestage |= HAVE_other ;
31718 { int bytesread, cue_count ;
31719 int id, position, chunk_id, chunk_start, block_start, offset ;
31721 bytesread = psf_binheader_readf (psf, "e44", &dword, &cue_count) ;
31722 bytesread -= 4 ; /* Remove bytes for first dword. */
31723 psf_log_printf (psf, "%M : %u\n Count : %d\n", marker, dword, cue_count) ;
31725 while (cue_count)
31726 { bytesread += psf_binheader_readf (psf, "e444444", &id, &position,
31727 &chunk_id, &chunk_start, &block_start, &offset) ;
31728 psf_log_printf (psf, " Cue ID : %2d"
31729 " Pos : %5u Chunk : %M"
31730 " Chk Start : %d Blk Start : %d"
31731 " Offset : %5d\n",
31732 id, position, chunk_id, chunk_start, block_start, offset) ;
31733 cue_count -- ;
31736 if (bytesread != dword)
31737 { psf_log_printf (psf, "**** Chunk size weirdness (%d != %d)\n", dword, bytesread) ;
31738 psf_binheader_readf (psf, "j", dword - bytesread) ;
31741 break ;
31743 case smpl_MARKER :
31744 parsestage |= HAVE_other ;
31746 psf_binheader_readf (psf, "e4", &dword) ;
31747 psf_log_printf (psf, "smpl : %u\n", dword) ;
31749 if ((error = wav_read_smpl_chunk (psf, dword)))
31750 return error ;
31751 break ;
31753 case acid_MARKER :
31754 parsestage |= HAVE_other ;
31756 psf_binheader_readf (psf, "e4", &dword) ;
31757 psf_log_printf (psf, "acid : %u\n", dword) ;
31759 if ((error = wav_read_acid_chunk (psf, dword)))
31760 return error ;
31761 break ;
31763 case INFO_MARKER :
31764 case LIST_MARKER :
31765 parsestage |= HAVE_other ;
31767 if ((error = wav_subchunk_parse (psf, marker)) != 0)
31768 return error ;
31769 break ;
31771 case strc_MARKER : /* Multiple of 32 bytes. */
31773 case afsp_MARKER :
31774 case bext_MARKER :
31775 case clm_MARKER :
31776 case plst_MARKER :
31777 case DISP_MARKER :
31778 case MEXT_MARKER :
31779 case PAD_MARKER :
31780 parsestage |= HAVE_other ;
31782 psf_binheader_readf (psf, "e4", &dword) ;
31783 psf_log_printf (psf, "%M : %u\n", marker, dword) ;
31784 dword += (dword & 1) ;
31785 psf_binheader_readf (psf, "j", dword) ;
31786 break ;
31788 default :
31789 if (isprint ((marker >> 24) & 0xFF) && isprint ((marker >> 16) & 0xFF)
31790 && isprint ((marker >> 8) & 0xFF) && isprint (marker & 0xFF))
31791 { psf_binheader_readf (psf, "e4", &dword) ;
31792 psf_log_printf (psf, "*** %M : %d (unknown marker)\n", marker, dword) ;
31793 psf_binheader_readf (psf, "j", dword) ;
31794 break ;
31796 if (psf_ftell (psf) & 0x03)
31797 { psf_log_printf (psf, " Unknown chunk marker at position %d. Resynching.\n", dword - 4) ;
31798 psf_binheader_readf (psf, "j", -3) ;
31799 break ;
31801 psf_log_printf (psf, "*** Unknown chunk marker : %X. Exiting parser.\n", marker) ;
31802 done = SF_TRUE ;
31803 break ;
31804 } ; /* switch (dword) */
31806 if (! psf->sf.seekable && (parsestage & wavHAVE_data))
31807 break ;
31809 if (psf_ftell (psf) >= psf->filelength - SIGNED_SIZEOF (dword))
31810 { psf_log_printf (psf, "End\n") ;
31811 break ;
31814 if (psf->logindex >= SIGNED_SIZEOF (psf->logbuffer) - 2)
31815 return SFE_LOG_OVERRUN ;
31816 } ; /* while (1) */
31818 if (! psf->dataoffset)
31819 return SFE_WAV_NO_DATA ;
31821 psf->endian = SF_ENDIAN_LITTLE ; /* All WAV files are little endian. */
31823 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
31825 if (psf->is_pipe == 0)
31826 { /*
31827 ** Check for 'wvpk' at the start of the DATA section. Not able to
31828 ** handle this.
31830 psf_binheader_readf (psf, "e4", &marker) ;
31831 if (marker == wvpk_MARKER || marker == OggS_MARKER)
31832 return SFE_WAV_WVPK_DATA ;
31835 /* Seek to start of DATA section. */
31836 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
31838 psf->close = wav_close ;
31840 if (psf->blockwidth)
31841 { if (psf->filelength - psf->dataoffset < psf->datalength)
31842 psf->sf.frames = (psf->filelength - psf->dataoffset) / psf->blockwidth ;
31843 else
31844 psf->sf.frames = psf->datalength / psf->blockwidth ;
31847 switch (format)
31850 case WAVE_FORMAT_EXTENSIBLE :
31851 /* compare GUIDs for known ones */
31852 if (wavex_write_guid_equal (&wav_fmt.ext.esf, &MSGUID_SUBTYPE_PCM))
31853 psf->sf.format = SF_FORMAT_WAVEX | u_bitwidth_to_subformat (psf->bytewidth * 8) ;
31854 else
31855 if (wavex_write_guid_equal (&wav_fmt.ext.esf, &MSGUID_SUBTYPE_MS_ADPCM))
31856 { psf->sf.format = (SF_FORMAT_WAVEX | SF_FORMAT_MS_ADPCM) ;
31857 *blockalign = wav_fmt.msadpcm.blockalign ;
31858 *framesperblock = wav_fmt.msadpcm.samplesperblock ;
31860 else if (wavex_write_guid_equal (&wav_fmt.ext.esf, &MSGUID_SUBTYPE_IEEE_FLOAT))
31861 psf->sf.format = SF_FORMAT_WAVEX | ((psf->bytewidth == 8) ? SF_FORMAT_DOUBLE : SF_FORMAT_FLOAT) ;
31862 else if (wavex_write_guid_equal (&wav_fmt.ext.esf, &MSGUID_SUBTYPE_ALAW))
31863 psf->sf.format = (SF_FORMAT_WAVEX | SF_FORMAT_ALAW) ;
31864 else if (wavex_write_guid_equal (&wav_fmt.ext.esf, &MSGUID_SUBTYPE_MULAW))
31865 psf->sf.format = (SF_FORMAT_WAVEX | SF_FORMAT_ULAW) ;
31866 else
31867 return SFE_UNIMPLEMENTED ;
31868 break ;
31870 case WAVE_FORMAT_PCM :
31871 psf->sf.format = SF_FORMAT_WAV | u_bitwidth_to_subformat (psf->bytewidth * 8) ;
31872 break ;
31874 case WAVE_FORMAT_MULAW :
31875 case IBM_FORMAT_MULAW :
31876 psf->sf.format = (SF_FORMAT_WAV | SF_FORMAT_ULAW) ;
31877 break ;
31879 case WAVE_FORMAT_ALAW :
31880 case IBM_FORMAT_ALAW :
31881 psf->sf.format = (SF_FORMAT_WAV | SF_FORMAT_ALAW) ;
31882 break ;
31884 case WAVE_FORMAT_MS_ADPCM :
31885 psf->sf.format = (SF_FORMAT_WAV | SF_FORMAT_MS_ADPCM) ;
31886 *blockalign = wav_fmt.msadpcm.blockalign ;
31887 *framesperblock = wav_fmt.msadpcm.samplesperblock ;
31888 break ;
31890 case WAVE_FORMAT_IMA_ADPCM :
31891 psf->sf.format = (SF_FORMAT_WAV | SF_FORMAT_IMA_ADPCM) ;
31892 *blockalign = wav_fmt.ima.blockalign ;
31893 *framesperblock = wav_fmt.ima.samplesperblock ;
31894 break ;
31896 case WAVE_FORMAT_GSM610 :
31897 psf->sf.format = (SF_FORMAT_WAV | SF_FORMAT_GSM610) ;
31898 break ;
31900 case WAVE_FORMAT_IEEE_FLOAT :
31901 psf->sf.format = SF_FORMAT_WAV ;
31902 psf->sf.format |= (psf->bytewidth == 8) ? SF_FORMAT_DOUBLE : SF_FORMAT_FLOAT ;
31903 break ;
31905 default : return SFE_UNIMPLEMENTED ;
31908 return 0 ;
31909 } /* wav_read_header */
31911 static int
31912 wav_write_header (SF_PRIVATE *psf, int calc_length)
31913 { sf_count_t current ;
31914 int fmt_size, k, subformat, add_fact_chunk = SF_FALSE ;
31916 current = psf_ftell (psf) ;
31918 if (calc_length)
31919 { psf->filelength = psf_get_filelen (psf) ;
31921 psf->datalength = psf->filelength - psf->dataoffset ;
31923 if (psf->dataend)
31924 psf->datalength -= psf->filelength - psf->dataend ;
31926 if (psf->bytewidth > 0)
31927 psf->sf.frames = psf->datalength / (psf->bytewidth * psf->sf.channels) ;
31930 /* Reset the current header length to zero. */
31931 psf->header [0] = 0 ;
31932 psf->headindex = 0 ;
31933 psf_fseek (psf, 0, SEEK_SET) ;
31935 /* RIFF marker, length, WAVE and 'fmt ' markers. */
31936 if (psf->filelength < 8)
31937 psf_binheader_writef (psf, "etm8", RIFF_MARKER, 8) ;
31938 else
31939 psf_binheader_writef (psf, "etm8", RIFF_MARKER, psf->filelength - 8) ;
31941 /* WAVE and 'fmt ' markers. */
31942 psf_binheader_writef (psf, "emm", WAVE_MARKER, fmt_MARKER) ;
31944 subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
31946 switch (subformat)
31947 { case SF_FORMAT_PCM_U8 :
31948 case SF_FORMAT_PCM_16 :
31949 case SF_FORMAT_PCM_24 :
31950 case SF_FORMAT_PCM_32 :
31951 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 ;
31953 /* fmt : format, channels, samplerate */
31954 psf_binheader_writef (psf, "e4224", fmt_size, WAVE_FORMAT_PCM, psf->sf.channels, psf->sf.samplerate) ;
31955 /* fmt : bytespersec */
31956 psf_binheader_writef (psf, "e4", psf->sf.samplerate * psf->bytewidth * psf->sf.channels) ;
31957 /* fmt : blockalign, bitwidth */
31958 psf_binheader_writef (psf, "e22", psf->bytewidth * psf->sf.channels, psf->bytewidth * 8) ;
31959 break ;
31961 case SF_FORMAT_FLOAT :
31962 case SF_FORMAT_DOUBLE :
31963 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 ;
31965 /* fmt : format, channels, samplerate */
31966 psf_binheader_writef (psf, "e4224", fmt_size, WAVE_FORMAT_IEEE_FLOAT, psf->sf.channels, psf->sf.samplerate) ;
31967 /* fmt : bytespersec */
31968 psf_binheader_writef (psf, "e4", psf->sf.samplerate * psf->bytewidth * psf->sf.channels) ;
31969 /* fmt : blockalign, bitwidth */
31970 psf_binheader_writef (psf, "e22", psf->bytewidth * psf->sf.channels, psf->bytewidth * 8) ;
31972 add_fact_chunk = SF_TRUE ;
31973 break ;
31975 case SF_FORMAT_ULAW :
31976 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 ;
31978 /* fmt : format, channels, samplerate */
31979 psf_binheader_writef (psf, "e4224", fmt_size, WAVE_FORMAT_MULAW, psf->sf.channels, psf->sf.samplerate) ;
31980 /* fmt : bytespersec */
31981 psf_binheader_writef (psf, "e4", psf->sf.samplerate * psf->bytewidth * psf->sf.channels) ;
31982 /* fmt : blockalign, bitwidth */
31983 psf_binheader_writef (psf, "e22", psf->bytewidth * psf->sf.channels, 8) ;
31985 add_fact_chunk = SF_TRUE ;
31986 break ;
31988 case SF_FORMAT_ALAW :
31989 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 ;
31991 /* fmt : format, channels, samplerate */
31992 psf_binheader_writef (psf, "e4224", fmt_size, WAVE_FORMAT_ALAW, psf->sf.channels, psf->sf.samplerate) ;
31993 /* fmt : bytespersec */
31994 psf_binheader_writef (psf, "e4", psf->sf.samplerate * psf->bytewidth * psf->sf.channels) ;
31995 /* fmt : blockalign, bitwidth */
31996 psf_binheader_writef (psf, "e22", psf->bytewidth * psf->sf.channels, 8) ;
31998 add_fact_chunk = SF_TRUE ;
31999 break ;
32001 /* Lite remove start */
32002 case SF_FORMAT_IMA_ADPCM :
32003 { int blockalign, framesperblock, bytespersec ;
32005 blockalign = wav_w64_srate2blocksize (psf->sf.samplerate * psf->sf.channels) ;
32006 framesperblock = 2 * (blockalign - 4 * psf->sf.channels) / psf->sf.channels + 1 ;
32007 bytespersec = (psf->sf.samplerate * blockalign) / framesperblock ;
32009 /* fmt chunk. */
32010 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 + 2 ;
32012 /* fmt : size, WAV format type, channels, samplerate, bytespersec */
32013 psf_binheader_writef (psf, "e42244", fmt_size, WAVE_FORMAT_IMA_ADPCM,
32014 psf->sf.channels, psf->sf.samplerate, bytespersec) ;
32016 /* fmt : blockalign, bitwidth, extrabytes, framesperblock. */
32017 psf_binheader_writef (psf, "e2222", blockalign, 4, 2, framesperblock) ;
32020 add_fact_chunk = SF_TRUE ;
32021 break ;
32023 case SF_FORMAT_MS_ADPCM :
32024 { int blockalign, framesperblock, bytespersec, extrabytes ;
32026 blockalign = wav_w64_srate2blocksize (psf->sf.samplerate * psf->sf.channels) ;
32027 framesperblock = 2 + 2 * (blockalign - 7 * psf->sf.channels) / psf->sf.channels ;
32028 bytespersec = (psf->sf.samplerate * blockalign) / framesperblock ;
32030 /* fmt chunk. */
32031 extrabytes = 2 + 2 + MSADPCM_ADAPT_COEFF_COUNT * (2 + 2) ;
32032 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 + extrabytes ;
32034 /* fmt : size, WAV format type, channels. */
32035 psf_binheader_writef (psf, "e422", fmt_size, WAVE_FORMAT_MS_ADPCM, psf->sf.channels) ;
32037 /* fmt : samplerate, bytespersec. */
32038 psf_binheader_writef (psf, "e44", psf->sf.samplerate, bytespersec) ;
32040 /* fmt : blockalign, bitwidth, extrabytes, framesperblock. */
32041 psf_binheader_writef (psf, "e22222", blockalign, 4, extrabytes, framesperblock, 7) ;
32043 msadpcm_write_adapt_coeffs (psf) ;
32046 add_fact_chunk = SF_TRUE ;
32047 break ;
32048 /* Lite remove end */
32050 case SF_FORMAT_GSM610 :
32051 { int blockalign, framesperblock, bytespersec ;
32053 blockalign = WAV_W64_GSM610_BLOCKSIZE ;
32054 framesperblock = WAV_W64_GSM610_SAMPLES ;
32055 bytespersec = (psf->sf.samplerate * blockalign) / framesperblock ;
32057 /* fmt chunk. */
32058 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 + 2 ;
32060 /* fmt : size, WAV format type, channels. */
32061 psf_binheader_writef (psf, "e422", fmt_size, WAVE_FORMAT_GSM610, psf->sf.channels) ;
32063 /* fmt : samplerate, bytespersec. */
32064 psf_binheader_writef (psf, "e44", psf->sf.samplerate, bytespersec) ;
32066 /* fmt : blockalign, bitwidth, extrabytes, framesperblock. */
32067 psf_binheader_writef (psf, "e2222", blockalign, 0, 2, framesperblock) ;
32070 add_fact_chunk = SF_TRUE ;
32071 break ;
32073 default : return SFE_UNIMPLEMENTED ;
32076 if (add_fact_chunk)
32077 psf_binheader_writef (psf, "etm48", fact_MARKER, 4, psf->sf.frames) ;
32079 if (psf->str_flags & SF_STR_LOCATE_START)
32080 wav_write_strings (psf, SF_STR_LOCATE_START) ;
32082 if (psf->has_peak && psf->peak_loc == SF_PEAK_START)
32083 { psf_binheader_writef (psf, "em4", PEAK_MARKER,
32084 sizeof (PEAK_CHUNK) + psf->sf.channels * sizeof (PEAK_POS)) ;
32085 psf_binheader_writef (psf, "e44", 1, time (NULL)) ;
32086 for (k = 0 ; k < psf->sf.channels ; k++)
32087 psf_binheader_writef (psf, "ef4", psf->pchunk->peaks [k].value, psf->pchunk->peaks [k].position) ;
32090 psf_binheader_writef (psf, "etm8", data_MARKER, psf->datalength) ;
32091 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
32092 if (psf->error)
32093 return psf->error ;
32095 psf->dataoffset = psf->headindex ;
32097 if (current < psf->dataoffset)
32098 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
32099 else if (current > 0)
32100 psf_fseek (psf, current, SEEK_SET) ;
32102 return psf->error ;
32103 } /* wav_write_header */
32107 static int
32108 wavex_write_guid_equal (const EXT_SUBFORMAT * first, const EXT_SUBFORMAT * second)
32109 { return !memcmp (first, second, sizeof (EXT_SUBFORMAT)) ;
32110 } /* wavex_write_guid_equal */
32113 static void
32114 wavex_write_guid (SF_PRIVATE *psf, const EXT_SUBFORMAT * subformat)
32116 psf_binheader_writef (psf, "e422b", subformat->esf_field1,
32117 subformat->esf_field2, subformat->esf_field3,
32118 subformat->esf_field4, 8) ;
32119 } /* wavex_write_guid */
32122 static int
32123 wavex_write_header (SF_PRIVATE *psf, int calc_length)
32124 { sf_count_t current ;
32125 int fmt_size, k, subformat, add_fact_chunk = SF_FALSE ;
32127 current = psf_ftell (psf) ;
32129 if (calc_length)
32130 { psf->filelength = psf_get_filelen (psf) ;
32132 psf->datalength = psf->filelength - psf->dataoffset ;
32134 if (psf->dataend)
32135 psf->datalength -= psf->filelength - psf->dataend ;
32137 if (psf->bytewidth > 0)
32138 psf->sf.frames = psf->datalength / (psf->bytewidth * psf->sf.channels) ;
32142 /* Reset the current header length to zero. */
32143 psf->header [0] = 0 ;
32144 psf->headindex = 0 ;
32145 psf_fseek (psf, 0, SEEK_SET) ;
32147 /* RIFF marker, length, WAVE and 'fmt ' markers. */
32148 if (psf->filelength < 8)
32149 psf_binheader_writef (psf, "etm8", RIFF_MARKER, 8) ;
32150 else
32151 psf_binheader_writef (psf, "etm8", RIFF_MARKER, psf->filelength - 8) ;
32153 /* WAVE and 'fmt ' markers. */
32154 psf_binheader_writef (psf, "emm", WAVE_MARKER, fmt_MARKER) ;
32156 subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
32158 /* initial section (same for all, it appears) */
32159 switch (subformat)
32160 { case SF_FORMAT_PCM_U8 :
32161 case SF_FORMAT_PCM_16 :
32162 case SF_FORMAT_PCM_24 :
32163 case SF_FORMAT_PCM_32 :
32164 case SF_FORMAT_FLOAT :
32165 case SF_FORMAT_DOUBLE :
32166 case SF_FORMAT_ULAW :
32167 case SF_FORMAT_ALAW :
32168 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 + 2 + 4 + 4 + 2 + 2 + 8 ;
32170 /* fmt : format, channels, samplerate */
32171 psf_binheader_writef (psf, "e4224", fmt_size, WAVE_FORMAT_EXTENSIBLE, psf->sf.channels, psf->sf.samplerate) ;
32172 /* fmt : bytespersec */
32173 psf_binheader_writef (psf, "e4", psf->sf.samplerate * psf->bytewidth * psf->sf.channels) ;
32174 /* fmt : blockalign, bitwidth */
32175 psf_binheader_writef (psf, "e22", psf->bytewidth * psf->sf.channels, psf->bytewidth * 8) ;
32177 /* cbSize 22 is sizeof (WAVEFORMATEXTENSIBLE) - sizeof (WAVEFORMATEX) */
32178 psf_binheader_writef (psf, "e2", 22) ;
32180 /* wValidBitsPerSample, for our use same as bitwidth as we use it fully */
32181 psf_binheader_writef (psf, "e2", psf->bytewidth * 8) ;
32183 if (psf->sf.channels == 2)
32184 psf_binheader_writef (psf, "e4", 0x1 | 0x2 ) ; /* dwChannelMask front left and right */
32185 else
32186 psf_binheader_writef (psf, "e4", 0) ; /* dwChannelMask = 0 when in doubt */
32187 break ;
32189 case SF_FORMAT_MS_ADPCM : /* todo, GUID exists might have different header as per wav_write_header */
32190 default : return SFE_UNIMPLEMENTED ;
32193 /* GUI section, different for each */
32195 switch (subformat)
32196 { case SF_FORMAT_PCM_U8 :
32197 case SF_FORMAT_PCM_16 :
32198 case SF_FORMAT_PCM_24 :
32199 case SF_FORMAT_PCM_32 :
32200 wavex_write_guid (psf, &MSGUID_SUBTYPE_PCM) ;
32201 break ;
32203 case SF_FORMAT_FLOAT :
32204 case SF_FORMAT_DOUBLE :
32205 wavex_write_guid (psf, &MSGUID_SUBTYPE_IEEE_FLOAT) ;
32206 add_fact_chunk = SF_TRUE ;
32207 break ;
32209 case SF_FORMAT_ULAW :
32210 wavex_write_guid (psf, &MSGUID_SUBTYPE_MULAW) ;
32211 add_fact_chunk = SF_TRUE ;
32212 break ;
32214 case SF_FORMAT_ALAW :
32215 wavex_write_guid (psf, &MSGUID_SUBTYPE_ALAW) ;
32216 add_fact_chunk = SF_TRUE ;
32217 break ;
32219 case SF_FORMAT_MS_ADPCM : /* todo, GUID exists */
32221 default : return SFE_UNIMPLEMENTED ;
32225 if (add_fact_chunk)
32226 psf_binheader_writef (psf, "etm48", fact_MARKER, 4, psf->sf.frames) ;
32228 if (psf->str_flags & SF_STR_LOCATE_START)
32229 wav_write_strings (psf, SF_STR_LOCATE_START) ;
32231 if (psf->has_peak && psf->peak_loc == SF_PEAK_START)
32232 { psf_binheader_writef (psf, "em4", PEAK_MARKER,
32233 sizeof (PEAK_CHUNK) + psf->sf.channels * sizeof (PEAK_POS)) ;
32234 psf_binheader_writef (psf, "e44", 1, time (NULL)) ;
32235 for (k = 0 ; k < psf->sf.channels ; k++)
32236 psf_binheader_writef (psf, "ef4", psf->pchunk->peaks [k].value, psf->pchunk->peaks [k].position) ;
32239 psf_binheader_writef (psf, "etm8", data_MARKER, psf->datalength) ;
32240 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
32241 if (psf->error)
32242 return psf->error ;
32244 psf->dataoffset = psf->headindex ;
32246 if (current < psf->dataoffset)
32247 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
32248 else if (current > 0)
32249 psf_fseek (psf, current, SEEK_SET) ;
32251 return psf->error ;
32252 } /* wavex_write_header */
32256 static int
32257 wav_write_tailer (SF_PRIVATE *psf)
32258 { int k ;
32260 /* Reset the current header buffer length to zero. */
32261 psf->header [0] = 0 ;
32262 psf->headindex = 0 ;
32264 psf->dataend = psf_fseek (psf, 0, SEEK_END) ;
32266 /* Add a PEAK chunk if requested. */
32267 if (psf->has_peak && psf->peak_loc == SF_PEAK_END)
32268 { psf_binheader_writef (psf, "em4", PEAK_MARKER,
32269 sizeof (PEAK_CHUNK) + psf->sf.channels * sizeof (PEAK_POS)) ;
32270 psf_binheader_writef (psf, "e44", 1, time (NULL)) ;
32271 for (k = 0 ; k < psf->sf.channels ; k++)
32272 psf_binheader_writef (psf, "ef4", psf->pchunk->peaks [k].value, psf->pchunk->peaks [k].position) ;
32275 if (psf->str_flags & SF_STR_LOCATE_END)
32276 wav_write_strings (psf, SF_STR_LOCATE_END) ;
32278 /* Write the tailer. */
32279 if (psf->headindex > 0)
32280 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
32282 return 0 ;
32283 } /* wav_write_tailer */
32285 static void
32286 wav_write_strings (SF_PRIVATE *psf, int location)
32287 { int k, prev_head_index, saved_head_index ;
32289 prev_head_index = psf->headindex + 4 ;
32291 psf_binheader_writef (psf, "em4m", LIST_MARKER, 0xBADBAD, INFO_MARKER) ;
32293 for (k = 0 ; k < SF_MAX_STRINGS ; k++)
32294 { if (psf->strings [k].type == 0)
32295 break ;
32296 if (psf->strings [k].flags != location)
32297 continue ;
32299 switch (psf->strings [k].type)
32300 { case SF_STR_SOFTWARE :
32301 psf_binheader_writef (psf, "ems", ISFT_MARKER, psf->strings [k].str) ;
32302 break ;
32304 case SF_STR_TITLE :
32305 psf_binheader_writef (psf, "ems", INAM_MARKER, psf->strings [k].str) ;
32306 break ;
32308 case SF_STR_COPYRIGHT :
32309 psf_binheader_writef (psf, "ems", ICOP_MARKER, psf->strings [k].str) ;
32310 break ;
32312 case SF_STR_ARTIST :
32313 psf_binheader_writef (psf, "ems", IART_MARKER, psf->strings [k].str) ;
32314 break ;
32316 case SF_STR_COMMENT :
32317 psf_binheader_writef (psf, "ems", ICMT_MARKER, psf->strings [k].str) ;
32318 break ;
32320 case SF_STR_DATE :
32321 psf_binheader_writef (psf, "ems", ICRD_MARKER, psf->strings [k].str) ;
32322 break ;
32326 saved_head_index = psf->headindex ;
32327 psf->headindex = prev_head_index ;
32328 psf_binheader_writef (psf, "e4", saved_head_index - prev_head_index - 4) ;
32329 psf->headindex = saved_head_index ;
32331 } /* wav_write_strings */
32333 static int
32334 wav_close (SF_PRIVATE *psf)
32336 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
32337 { wav_write_tailer (psf) ;
32339 if ((psf->sf.format & SF_FORMAT_TYPEMASK) == SF_FORMAT_WAV)
32340 wav_write_header (psf, SF_TRUE) ;
32341 else
32342 wavex_write_header (psf, SF_TRUE) ;
32345 return 0 ;
32346 } /* wav_close */
32348 static int
32349 wav_command (SF_PRIVATE *psf, int command, void *data, int datasize)
32351 /* Avoid compiler warnings. */
32352 psf = psf ;
32353 data = data ;
32354 datasize = datasize ;
32356 switch (command)
32357 { default : break ;
32360 return 0 ;
32361 } /* wav_command */
32363 static int
32364 wav_subchunk_parse (SF_PRIVATE *psf, int chunk)
32365 { sf_count_t current_pos ;
32366 char *cptr ;
32367 int dword, bytesread, length ;
32369 current_pos = psf_fseek (psf, 0, SEEK_CUR) ;
32371 bytesread = psf_binheader_readf (psf, "e4", &length) ;
32373 if (current_pos + length > psf->filelength)
32374 { psf_log_printf (psf, "%M : %d (should be %d)\n", chunk, length, (int) (psf->filelength - current_pos)) ;
32375 length = psf->filelength - current_pos ;
32377 else
32378 psf_log_printf (psf, "%M : %d\n", chunk, length) ;
32381 while (bytesread < length)
32382 { bytesread += psf_binheader_readf (psf, "m", &chunk) ;
32384 switch (chunk)
32385 { case adtl_MARKER :
32386 case INFO_MARKER :
32387 /* These markers don't contain anything. */
32388 psf_log_printf (psf, " %M\n", chunk) ;
32389 break ;
32391 case data_MARKER:
32392 psf_log_printf (psf, " %M inside a LIST block??? Backing out.\n", chunk) ;
32393 /* Jump back four bytes and return to caller. */
32394 psf_binheader_readf (psf, "j", -4) ;
32395 return 0 ;
32397 case ISFT_MARKER :
32398 case ICOP_MARKER :
32399 case IARL_MARKER :
32400 case IART_MARKER :
32401 case ICMT_MARKER :
32402 case ICRD_MARKER :
32403 case IENG_MARKER :
32405 case INAM_MARKER :
32406 case IPRD_MARKER :
32407 case ISBJ_MARKER :
32408 case ISRC_MARKER :
32409 bytesread += psf_binheader_readf (psf, "e4", &dword) ;
32410 dword += (dword & 1) ;
32411 if (dword > SIGNED_SIZEOF (psf->buffer))
32412 { psf_log_printf (psf, " *** %M : %d (too big)\n", chunk, dword) ;
32413 return SFE_INTERNAL ;
32416 cptr = (char*) psf->buffer ;
32417 psf_binheader_readf (psf, "b", cptr, dword) ;
32418 bytesread += dword ;
32419 cptr [dword - 1] = 0 ;
32420 psf_log_printf (psf, " %M : %s\n", chunk, cptr) ;
32421 break ;
32423 case labl_MARKER :
32424 { int mark_id ;
32426 bytesread += psf_binheader_readf (psf, "e44", &dword, &mark_id) ;
32427 dword -= 4 ;
32428 dword += (dword & 1) ;
32429 if (dword > SIGNED_SIZEOF (psf->buffer))
32430 { psf_log_printf (psf, " *** %M : %d (too big)\n", chunk, dword) ;
32431 return SFE_INTERNAL ;
32434 cptr = (char*) psf->buffer ;
32435 psf_binheader_readf (psf, "b", cptr, dword) ;
32436 bytesread += dword ;
32437 cptr [dword - 1] = 0 ;
32438 psf_log_printf (psf, " %M : %d : %s\n", chunk, mark_id, cptr) ;
32440 break ;
32443 case DISP_MARKER :
32444 case ltxt_MARKER :
32445 case note_MARKER :
32446 bytesread += psf_binheader_readf (psf, "e4", &dword) ;
32447 dword += (dword & 1) ;
32448 psf_binheader_readf (psf, "j", dword) ;
32449 bytesread += dword ;
32450 psf_log_printf (psf, " %M : %d\n", chunk, dword) ;
32451 break ;
32453 default :
32454 psf_binheader_readf (psf, "e4", &dword) ;
32455 bytesread += sizeof (dword) ;
32456 dword += (dword & 1) ;
32457 psf_binheader_readf (psf, "j", dword) ;
32458 bytesread += dword ;
32459 psf_log_printf (psf, " *** %M : %d\n", chunk, dword) ;
32460 if (dword > length)
32461 return 0 ;
32462 break ;
32465 switch (chunk)
32466 { case ISFT_MARKER :
32467 psf_store_string (psf, SF_STR_SOFTWARE, (char*) psf->buffer) ;
32468 break ;
32469 case ICOP_MARKER :
32470 psf_store_string (psf, SF_STR_COPYRIGHT, (char*) psf->buffer) ;
32471 break ;
32472 case INAM_MARKER :
32473 psf_store_string (psf, SF_STR_TITLE, (char*) psf->buffer) ;
32474 break ;
32475 case IART_MARKER :
32476 psf_store_string (psf, SF_STR_ARTIST, (char*) psf->buffer) ;
32477 break ;
32478 case ICMT_MARKER :
32479 psf_store_string (psf, SF_STR_COMMENT, (char*) psf->buffer) ;
32480 break ;
32481 case ICRD_MARKER :
32482 psf_store_string (psf, SF_STR_DATE, (char*) psf->buffer) ;
32483 break ;
32486 if (psf->logindex >= SIGNED_SIZEOF (psf->logbuffer) - 2)
32487 return SFE_LOG_OVERRUN ;
32490 current_pos = psf_fseek (psf, 0, SEEK_CUR) - current_pos ;
32492 if (current_pos - 4 != length)
32493 psf_log_printf (psf, "**** Bad chunk length %d sbould be %D\n", length, current_pos - 4) ;
32495 return 0 ;
32496 } /* wav_subchunk_parse */
32498 static int
32499 wav_read_smpl_chunk (SF_PRIVATE *psf, unsigned int chunklen)
32500 { unsigned int bytesread = 0, dword, sampler_data, loop_count ;
32501 int k ;
32503 chunklen += (chunklen & 1) ;
32505 bytesread += psf_binheader_readf (psf, "e4", &dword) ;
32506 psf_log_printf (psf, " Manufacturer : %X\n", dword) ;
32508 bytesread += psf_binheader_readf (psf, "e4", &dword) ;
32509 psf_log_printf (psf, " Product : %u\n", dword) ;
32511 bytesread += psf_binheader_readf (psf, "e4", &dword) ;
32512 psf_log_printf (psf, " Period : %u nsec\n", dword) ;
32514 bytesread += psf_binheader_readf (psf, "e4", &dword) ;
32515 psf_log_printf (psf, " Midi Note : %u\n", dword) ;
32517 bytesread += psf_binheader_readf (psf, "e4", &dword) ;
32518 if (dword != 0)
32519 { LSF_SNPRINTF ((char*) psf->buffer, sizeof (psf->buffer), "%f",
32520 (1.0 * 0x80000000) / ((unsigned int) dword)) ;
32521 psf_log_printf (psf, " Pitch Fract. : %s\n", (char*) psf->buffer) ;
32523 else
32524 psf_log_printf (psf, " Pitch Fract. : 0\n") ;
32526 bytesread += psf_binheader_readf (psf, "e4", &dword) ;
32527 psf_log_printf (psf, " SMPTE Format : %u\n", dword) ;
32529 bytesread += psf_binheader_readf (psf, "e4", &dword) ;
32530 LSF_SNPRINTF ((char*) psf->buffer, sizeof (psf->buffer), "%02d:%02d:%02d %02d",
32531 (dword >> 24) & 0x7F, (dword >> 16) & 0x7F, (dword >> 8) & 0x7F, dword & 0x7F) ;
32532 psf_log_printf (psf, " SMPTE Offset : %s\n", psf->buffer) ;
32534 bytesread += psf_binheader_readf (psf, "e4", &loop_count) ;
32535 psf_log_printf (psf, " Loop Count : %u\n", loop_count) ;
32537 /* Sampler Data holds the number of data bytes after the CUE chunks which
32538 ** is not actually CUE data. Display value after CUE data.
32540 bytesread += psf_binheader_readf (psf, "e4", &sampler_data) ;
32542 while (loop_count > 0 && chunklen - bytesread >= 24)
32544 bytesread += psf_binheader_readf (psf, "e4", &dword) ;
32545 psf_log_printf (psf, " Cue ID : %2u", dword) ;
32547 bytesread += psf_binheader_readf (psf, "e4", &dword) ;
32548 psf_log_printf (psf, " Type : %2u", dword) ;
32550 bytesread += psf_binheader_readf (psf, "e4", &dword) ;
32551 psf_log_printf (psf, " Start : %5u", dword) ;
32553 bytesread += psf_binheader_readf (psf, "e4", &dword) ;
32554 psf_log_printf (psf, " End : %5u", dword) ;
32556 bytesread += psf_binheader_readf (psf, "e4", &dword) ;
32557 psf_log_printf (psf, " Fraction : %5u", dword) ;
32559 bytesread += psf_binheader_readf (psf, "e4", &dword) ;
32560 psf_log_printf (psf, " Count : %5u\n", dword) ;
32562 loop_count -- ;
32565 if (chunklen - bytesread == 0)
32566 { if (sampler_data != 0)
32567 psf_log_printf (psf, " Sampler Data : %u (should be 0)\n", sampler_data) ;
32568 else
32569 psf_log_printf (psf, " Sampler Data : %u\n", sampler_data) ;
32571 else
32572 { if (sampler_data != chunklen - bytesread)
32573 { psf_log_printf (psf, " Sampler Data : %u (should have been %u)\n", sampler_data, chunklen - bytesread) ;
32574 sampler_data = chunklen - bytesread ;
32576 else
32577 psf_log_printf (psf, " Sampler Data : %u\n", sampler_data) ;
32579 psf_log_printf (psf, " ") ;
32580 for (k = 0 ; k < (int) sampler_data ; k++)
32581 { char ch ;
32583 if (k > 0 && (k % 20) == 0)
32584 psf_log_printf (psf, "\n ") ;
32586 bytesread += psf_binheader_readf (psf, "1", &ch) ;
32587 psf_log_printf (psf, "%02X ", ch & 0xFF) ;
32590 psf_log_printf (psf, "\n") ;
32593 return 0 ;
32594 } /* wav_read_smpl_chunk */
32597 ** The acid chunk goes a little something like this:
32599 ** 4 bytes 'acid'
32600 ** 4 bytes (int) length of chunk
32601 ** 4 bytes (int) ???
32602 ** 2 bytes (short) root note
32603 ** 2 bytes (short) ???
32604 ** 4 bytes (float) ???
32605 ** 4 bytes (int) number of beats
32606 ** 2 bytes (short) meter denominator
32607 ** 2 bytes (short) meter numerator
32608 ** 4 bytes (float) tempo
32612 static int
32613 wav_read_acid_chunk (SF_PRIVATE *psf, unsigned int chunklen)
32614 { unsigned int bytesread = 0 ;
32615 int beats ;
32616 short rootnote, q1, meter_denom, meter_numer ;
32617 float q2, q3, tempo ;
32619 chunklen += (chunklen & 1) ;
32621 bytesread += psf_binheader_readf (psf, "e224f", &rootnote, &q1, &q2, &q3) ;
32622 LSF_SNPRINTF ((char*) psf->buffer, sizeof (psf->buffer), "%f", q2) ;
32623 psf_log_printf (psf, " Root note : %d\n ???? : %d\n ???? : %s\n ???? : %d\n",
32624 rootnote, q1, psf->buffer, q3) ;
32626 bytesread += psf_binheader_readf (psf, "e422f", &beats, &meter_denom, &meter_numer, &tempo) ;
32627 LSF_SNPRINTF ((char*) psf->buffer, sizeof (psf->buffer), "%f", tempo) ;
32628 psf_log_printf (psf, " Beats : %d\n Meter : %d/%d\n Tempo : %s\n",
32629 beats, meter_denom, meter_numer, psf->buffer) ;
32631 psf_binheader_readf (psf, "j", chunklen - bytesread) ;
32633 return 0 ;
32634 } /* wav_read_acid_chunk */
32636 ** Do not edit or modify anything in this comment block.
32637 ** The arch-tag line is a file identity tag for the GNU Arch
32638 ** revision control system.
32640 ** arch-tag: 9c551689-a1d8-4905-9f56-26a204374f18
32643 ** Copyright (C) 1999-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
32645 ** This program is free software; you can redistribute it and/or modify
32646 ** it under the terms of the GNU Lesser General Public License as published by
32647 ** the Free Software Foundation; either version 2.1 of the License, or
32648 ** (at your option) any later version.
32650 ** This program is distributed in the hope that it will be useful,
32651 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
32652 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32653 ** GNU Lesser General Public License for more details.
32655 ** You should have received a copy of the GNU Lesser General Public License
32656 ** along with this program; if not, write to the Free Software
32657 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
32660 #include <stdio.h>
32661 #include <string.h>
32662 #include <ctype.h>
32663 #include <time.h>
32666 /*------------------------------------------------------------------------------
32667 * Private static functions.
32671 wav_w64_read_fmt_chunk (SF_PRIVATE *psf, WAV_FMT *wav_fmt, int structsize)
32672 { int bytesread, k, bytespersec = 0 ;
32674 memset (wav_fmt, 0, sizeof (WAV_FMT)) ;
32676 if (structsize < 16)
32677 return SFE_WAV_FMT_SHORT ;
32678 if (structsize > SIGNED_SIZEOF (WAV_FMT))
32679 return SFE_WAV_FMT_TOO_BIG ;
32681 /* Read the minimal WAV file header here. */
32682 bytesread =
32683 psf_binheader_readf (psf, "e224422", &(wav_fmt->format), &(wav_fmt->min.channels),
32684 &(wav_fmt->min.samplerate), &(wav_fmt->min.bytespersec),
32685 &(wav_fmt->min.blockalign), &(wav_fmt->min.bitwidth)) ;
32687 psf_log_printf (psf, " Format : 0x%X => %s\n", wav_fmt->format, wav_w64_format_str (wav_fmt->format)) ;
32688 psf_log_printf (psf, " Channels : %d\n", wav_fmt->min.channels) ;
32689 psf_log_printf (psf, " Sample Rate : %d\n", wav_fmt->min.samplerate) ;
32690 psf_log_printf (psf, " Block Align : %d\n", wav_fmt->min.blockalign) ;
32692 if (wav_fmt->format == WAVE_FORMAT_PCM && wav_fmt->min.bitwidth == 24 &&
32693 wav_fmt->min.blockalign == 4 * wav_fmt->min.channels)
32695 psf_log_printf (psf, "\nInvalid file generated by Syntrillium's Cooledit!\n"
32696 "Treating as WAVE_FORMAT_IEEE_FLOAT 32 bit floating point file.\n\n") ;
32697 psf_log_printf (psf, " Bit Width : 24 (should be 32)\n") ;
32698 wav_fmt->min.bitwidth = 32 ;
32699 wav_fmt->format = WAVE_FORMAT_IEEE_FLOAT ;
32701 else if (wav_fmt->format == WAVE_FORMAT_GSM610 && wav_fmt->min.bitwidth != 0)
32702 psf_log_printf (psf, " Bit Width : %d (should be 0)\n", wav_fmt->min.bitwidth) ;
32703 else
32704 psf_log_printf (psf, " Bit Width : %d\n", wav_fmt->min.bitwidth) ;
32707 psf->sf.samplerate = wav_fmt->min.samplerate ;
32708 psf->sf.frames = 0 ; /* Correct this when reading data chunk. */
32709 psf->sf.channels = wav_fmt->min.channels ;
32711 switch (wav_fmt->format)
32712 { case WAVE_FORMAT_PCM :
32713 case WAVE_FORMAT_IEEE_FLOAT :
32714 bytespersec = wav_fmt->min.samplerate * wav_fmt->min.blockalign ;
32715 if (wav_fmt->min.bytespersec != (unsigned) bytespersec)
32716 psf_log_printf (psf, " Bytes/sec : %d (should be %d)\n", wav_fmt->min.bytespersec, bytespersec) ;
32717 else
32718 psf_log_printf (psf, " Bytes/sec : %d\n", wav_fmt->min.bytespersec) ;
32720 psf->bytewidth = BITWIDTH2BYTES (wav_fmt->min.bitwidth) ;
32721 break ;
32723 case WAVE_FORMAT_ALAW :
32724 case WAVE_FORMAT_MULAW :
32725 if (wav_fmt->min.bytespersec / wav_fmt->min.blockalign != wav_fmt->min.samplerate)
32726 psf_log_printf (psf, " Bytes/sec : %d (should be %d)\n", wav_fmt->min.bytespersec, wav_fmt->min.samplerate * wav_fmt->min.blockalign) ;
32727 else
32728 psf_log_printf (psf, " Bytes/sec : %d\n", wav_fmt->min.bytespersec) ;
32730 psf->bytewidth = 1 ;
32731 if (structsize >= 18)
32732 { bytesread += psf_binheader_readf (psf, "e2", &(wav_fmt->size20.extrabytes)) ;
32733 psf_log_printf (psf, " Extra Bytes : %d\n", wav_fmt->size20.extrabytes) ;
32735 break ;
32737 case WAVE_FORMAT_IMA_ADPCM :
32738 if (wav_fmt->min.bitwidth != 4)
32739 return SFE_WAV_ADPCM_NOT4BIT ;
32740 if (wav_fmt->min.channels < 1 || wav_fmt->min.channels > 2)
32741 return SFE_WAV_ADPCM_CHANNELS ;
32743 bytesread +=
32744 psf_binheader_readf (psf, "e22", &(wav_fmt->ima.extrabytes), &(wav_fmt->ima.samplesperblock)) ;
32746 bytespersec = (wav_fmt->ima.samplerate * wav_fmt->ima.blockalign) / wav_fmt->ima.samplesperblock ;
32747 if (wav_fmt->ima.bytespersec != (unsigned) bytespersec)
32748 psf_log_printf (psf, " Bytes/sec : %d (should be %d)\n", wav_fmt->ima.bytespersec, bytespersec) ;
32749 else
32750 psf_log_printf (psf, " Bytes/sec : %d\n", wav_fmt->ima.bytespersec) ;
32752 psf->bytewidth = 2 ;
32753 psf_log_printf (psf, " Extra Bytes : %d\n", wav_fmt->ima.extrabytes) ;
32754 psf_log_printf (psf, " Samples/Block : %d\n", wav_fmt->ima.samplesperblock) ;
32755 break ;
32757 case WAVE_FORMAT_MS_ADPCM :
32758 if (wav_fmt->msadpcm.bitwidth != 4)
32759 return SFE_WAV_ADPCM_NOT4BIT ;
32760 if (wav_fmt->msadpcm.channels < 1 || wav_fmt->msadpcm.channels > 2)
32761 return SFE_WAV_ADPCM_CHANNELS ;
32763 bytesread +=
32764 psf_binheader_readf (psf, "e222", &(wav_fmt->msadpcm.extrabytes),
32765 &(wav_fmt->msadpcm.samplesperblock), &(wav_fmt->msadpcm.numcoeffs)) ;
32767 bytespersec = (wav_fmt->min.samplerate * wav_fmt->min.blockalign) / wav_fmt->msadpcm.samplesperblock ;
32768 if (wav_fmt->min.bytespersec == (unsigned) bytespersec)
32769 psf_log_printf (psf, " Bytes/sec : %d\n", wav_fmt->min.bytespersec) ;
32770 else if (wav_fmt->min.bytespersec == (wav_fmt->min.samplerate / wav_fmt->msadpcm.samplesperblock) * wav_fmt->min.blockalign)
32771 psf_log_printf (psf, " Bytes/sec : %d (should be %d (MS BUG!))\n", wav_fmt->min.bytespersec, bytespersec) ;
32772 else
32773 psf_log_printf (psf, " Bytes/sec : %d (should be %d)\n", wav_fmt->min.bytespersec, bytespersec) ;
32776 psf->bytewidth = 2 ;
32777 psf_log_printf (psf, " Extra Bytes : %d\n", wav_fmt->msadpcm.extrabytes) ;
32778 psf_log_printf (psf, " Samples/Block : %d\n", wav_fmt->msadpcm.samplesperblock) ;
32779 if (wav_fmt->msadpcm.numcoeffs > SIGNED_SIZEOF (MS_ADPCM_WAV_FMT) / SIGNED_SIZEOF (int))
32780 { psf_log_printf (psf, " No. of Coeffs : %d ****\n", wav_fmt->msadpcm.numcoeffs) ;
32781 wav_fmt->msadpcm.numcoeffs = SIGNED_SIZEOF (MS_ADPCM_WAV_FMT) / SIGNED_SIZEOF (int) ;
32783 else
32784 psf_log_printf (psf, " No. of Coeffs : %d\n", wav_fmt->msadpcm.numcoeffs) ;
32786 psf_log_printf (psf, " Index Coeffs1 Coeffs2\n") ;
32787 for (k = 0 ; k < wav_fmt->msadpcm.numcoeffs ; k++)
32788 { bytesread +=
32789 psf_binheader_readf (psf, "e22", &(wav_fmt->msadpcm.coeffs [k].coeff1), &(wav_fmt->msadpcm.coeffs [k].coeff2)) ;
32790 LSF_SNPRINTF ((char*) psf->buffer, sizeof (psf->buffer), " %2d %7d %7d\n", k, wav_fmt->msadpcm.coeffs [k].coeff1, wav_fmt->msadpcm.coeffs [k].coeff2) ;
32791 psf_log_printf (psf, (char*) psf->buffer) ;
32793 break ;
32795 case WAVE_FORMAT_GSM610 :
32796 if (wav_fmt->gsm610.channels != 1 || wav_fmt->gsm610.blockalign != 65)
32797 return SFE_WAV_GSM610_FORMAT ;
32799 bytesread +=
32800 psf_binheader_readf (psf, "e22", &(wav_fmt->gsm610.extrabytes), &(wav_fmt->gsm610.samplesperblock)) ;
32802 if (wav_fmt->gsm610.samplesperblock != 320)
32803 return SFE_WAV_GSM610_FORMAT ;
32805 bytespersec = (wav_fmt->gsm610.samplerate * wav_fmt->gsm610.blockalign) / wav_fmt->gsm610.samplesperblock ;
32806 if (wav_fmt->gsm610.bytespersec != (unsigned) bytespersec)
32807 psf_log_printf (psf, " Bytes/sec : %d (should be %d)\n", wav_fmt->gsm610.bytespersec, bytespersec) ;
32808 else
32809 psf_log_printf (psf, " Bytes/sec : %d\n", wav_fmt->gsm610.bytespersec) ;
32811 psf->bytewidth = 2 ;
32812 psf_log_printf (psf, " Extra Bytes : %d\n", wav_fmt->gsm610.extrabytes) ;
32813 psf_log_printf (psf, " Samples/Block : %d\n", wav_fmt->gsm610.samplesperblock) ;
32814 break ;
32816 case WAVE_FORMAT_EXTENSIBLE :
32817 if (wav_fmt->ext.bytespersec / wav_fmt->ext.blockalign != wav_fmt->ext.samplerate)
32818 psf_log_printf (psf, " Bytes/sec : %d (should be %d)\n", wav_fmt->ext.bytespersec, wav_fmt->ext.samplerate * wav_fmt->ext.blockalign) ;
32819 else
32820 psf_log_printf (psf, " Bytes/sec : %d\n", wav_fmt->ext.bytespersec) ;
32822 bytesread +=
32823 psf_binheader_readf (psf, "e224", &(wav_fmt->ext.extrabytes), &(wav_fmt->ext.validbits),
32824 &(wav_fmt->ext.channelmask)) ;
32826 psf_log_printf (psf, " Valid Bits : %d\n", wav_fmt->ext.validbits) ;
32827 psf_log_printf (psf, " Channel Mask : 0x%X\n", wav_fmt->ext.channelmask) ;
32829 bytesread +=
32830 psf_binheader_readf (psf, "e422", &(wav_fmt->ext.esf.esf_field1), &(wav_fmt->ext.esf.esf_field2),
32831 &(wav_fmt->ext.esf.esf_field3)) ;
32833 /* compare the esf_fields with each known GUID? and print?*/
32834 psf_log_printf (psf, " Subformat\n") ;
32835 psf_log_printf (psf, " esf_field1 : 0x%X\n", wav_fmt->ext.esf.esf_field1) ;
32836 psf_log_printf (psf, " esf_field2 : 0x%X\n", wav_fmt->ext.esf.esf_field2) ;
32837 psf_log_printf (psf, " esf_field3 : 0x%X\n", wav_fmt->ext.esf.esf_field3) ;
32838 psf_log_printf (psf, " esf_field4 : ") ;
32839 for (k = 0 ; k < 8 ; k++)
32840 { bytesread += psf_binheader_readf (psf, "1", &(wav_fmt->ext.esf.esf_field4 [k])) ;
32841 psf_log_printf (psf, "0x%X ", wav_fmt->ext.esf.esf_field4 [k] & 0xFF) ;
32843 psf_log_printf (psf, "\n") ;
32844 psf->bytewidth = BITWIDTH2BYTES (wav_fmt->ext.bitwidth) ;
32845 break ;
32847 default : break ;
32850 if (bytesread > structsize)
32851 { psf_log_printf (psf, "*** wav_w64_read_fmt_chunk (bytesread > structsize)\n") ;
32852 return SFE_W64_FMT_SHORT ;
32854 else
32855 psf_binheader_readf (psf, "j", structsize - bytesread) ;
32857 psf->blockwidth = wav_fmt->min.channels * psf->bytewidth ;
32859 return 0 ;
32860 } /* wav_w64_read_fmt_chunk */
32862 /*==============================================================================
32865 typedef struct
32866 { int ID ;
32867 const char *name ;
32868 } WAV_FORMAT_DESC ;
32870 #define STR(x) #x
32871 #define FORMAT_TYPE(x) { x, STR (x) }
32873 static WAV_FORMAT_DESC wave_descs [] =
32874 { FORMAT_TYPE (WAVE_FORMAT_PCM),
32875 FORMAT_TYPE (WAVE_FORMAT_MS_ADPCM),
32876 FORMAT_TYPE (WAVE_FORMAT_IEEE_FLOAT),
32877 FORMAT_TYPE (WAVE_FORMAT_VSELP),
32878 FORMAT_TYPE (WAVE_FORMAT_IBM_CVSD),
32879 FORMAT_TYPE (WAVE_FORMAT_ALAW),
32880 FORMAT_TYPE (WAVE_FORMAT_MULAW),
32881 FORMAT_TYPE (WAVE_FORMAT_OKI_ADPCM),
32882 FORMAT_TYPE (WAVE_FORMAT_IMA_ADPCM),
32883 FORMAT_TYPE (WAVE_FORMAT_MEDIASPACE_ADPCM),
32884 FORMAT_TYPE (WAVE_FORMAT_SIERRA_ADPCM),
32885 FORMAT_TYPE (WAVE_FORMAT_G723_ADPCM),
32886 FORMAT_TYPE (WAVE_FORMAT_DIGISTD),
32887 FORMAT_TYPE (WAVE_FORMAT_DIGIFIX),
32888 FORMAT_TYPE (WAVE_FORMAT_DIALOGIC_OKI_ADPCM),
32889 FORMAT_TYPE (WAVE_FORMAT_MEDIAVISION_ADPCM),
32890 FORMAT_TYPE (WAVE_FORMAT_CU_CODEC),
32891 FORMAT_TYPE (WAVE_FORMAT_YAMAHA_ADPCM),
32892 FORMAT_TYPE (WAVE_FORMAT_SONARC),
32893 FORMAT_TYPE (WAVE_FORMAT_DSPGROUP_TRUESPEECH),
32894 FORMAT_TYPE (WAVE_FORMAT_ECHOSC1),
32895 FORMAT_TYPE (WAVE_FORMAT_AUDIOFILE_AF36),
32896 FORMAT_TYPE (WAVE_FORMAT_APTX),
32897 FORMAT_TYPE (WAVE_FORMAT_AUDIOFILE_AF10),
32898 FORMAT_TYPE (WAVE_FORMAT_PROSODY_1612),
32899 FORMAT_TYPE (WAVE_FORMAT_LRC),
32900 FORMAT_TYPE (WAVE_FORMAT_DOLBY_AC2),
32901 FORMAT_TYPE (WAVE_FORMAT_GSM610),
32902 FORMAT_TYPE (WAVE_FORMAT_MSNAUDIO),
32903 FORMAT_TYPE (WAVE_FORMAT_ANTEX_ADPCME),
32904 FORMAT_TYPE (WAVE_FORMAT_CONTROL_RES_VQLPC),
32905 FORMAT_TYPE (WAVE_FORMAT_DIGIREAL),
32906 FORMAT_TYPE (WAVE_FORMAT_DIGIADPCM),
32907 FORMAT_TYPE (WAVE_FORMAT_CONTROL_RES_CR10),
32908 FORMAT_TYPE (WAVE_FORMAT_NMS_VBXADPCM),
32909 FORMAT_TYPE (WAVE_FORMAT_ROLAND_RDAC),
32910 FORMAT_TYPE (WAVE_FORMAT_ECHOSC3),
32911 FORMAT_TYPE (WAVE_FORMAT_ROCKWELL_ADPCM),
32912 FORMAT_TYPE (WAVE_FORMAT_ROCKWELL_DIGITALK),
32913 FORMAT_TYPE (WAVE_FORMAT_XEBEC),
32914 FORMAT_TYPE (WAVE_FORMAT_G721_ADPCM),
32915 FORMAT_TYPE (WAVE_FORMAT_G728_CELP),
32916 FORMAT_TYPE (WAVE_FORMAT_MSG723),
32917 FORMAT_TYPE (WAVE_FORMAT_MPEG),
32918 FORMAT_TYPE (WAVE_FORMAT_RT24),
32919 FORMAT_TYPE (WAVE_FORMAT_PAC),
32920 FORMAT_TYPE (WAVE_FORMAT_MPEGLAYER3),
32921 FORMAT_TYPE (WAVE_FORMAT_LUCENT_G723),
32922 FORMAT_TYPE (WAVE_FORMAT_CIRRUS),
32923 FORMAT_TYPE (WAVE_FORMAT_ESPCM),
32924 FORMAT_TYPE (WAVE_FORMAT_VOXWARE),
32925 FORMAT_TYPE (WAVE_FORMAT_CANOPUS_ATRAC),
32926 FORMAT_TYPE (WAVE_FORMAT_G726_ADPCM),
32927 FORMAT_TYPE (WAVE_FORMAT_G722_ADPCM),
32928 FORMAT_TYPE (WAVE_FORMAT_DSAT),
32929 FORMAT_TYPE (WAVE_FORMAT_DSAT_DISPLAY),
32930 FORMAT_TYPE (WAVE_FORMAT_VOXWARE_BYTE_ALIGNED),
32931 FORMAT_TYPE (WAVE_FORMAT_VOXWARE_AC8),
32932 FORMAT_TYPE (WAVE_FORMAT_VOXWARE_AC10),
32933 FORMAT_TYPE (WAVE_FORMAT_VOXWARE_AC16),
32934 FORMAT_TYPE (WAVE_FORMAT_VOXWARE_AC20),
32935 FORMAT_TYPE (WAVE_FORMAT_VOXWARE_RT24),
32936 FORMAT_TYPE (WAVE_FORMAT_VOXWARE_RT29),
32937 FORMAT_TYPE (WAVE_FORMAT_VOXWARE_RT29HW),
32938 FORMAT_TYPE (WAVE_FORMAT_VOXWARE_VR12),
32939 FORMAT_TYPE (WAVE_FORMAT_VOXWARE_VR18),
32940 FORMAT_TYPE (WAVE_FORMAT_VOXWARE_TQ40),
32941 FORMAT_TYPE (WAVE_FORMAT_SOFTSOUND),
32942 FORMAT_TYPE (WAVE_FORMAT_VOXARE_TQ60),
32943 FORMAT_TYPE (WAVE_FORMAT_MSRT24),
32944 FORMAT_TYPE (WAVE_FORMAT_G729A),
32945 FORMAT_TYPE (WAVE_FORMAT_MVI_MV12),
32946 FORMAT_TYPE (WAVE_FORMAT_DF_G726),
32947 FORMAT_TYPE (WAVE_FORMAT_DF_GSM610),
32948 FORMAT_TYPE (WAVE_FORMAT_ONLIVE),
32949 FORMAT_TYPE (WAVE_FORMAT_SBC24),
32950 FORMAT_TYPE (WAVE_FORMAT_DOLBY_AC3_SPDIF),
32951 FORMAT_TYPE (WAVE_FORMAT_ZYXEL_ADPCM),
32952 FORMAT_TYPE (WAVE_FORMAT_PHILIPS_LPCBB),
32953 FORMAT_TYPE (WAVE_FORMAT_PACKED),
32954 FORMAT_TYPE (WAVE_FORMAT_RHETOREX_ADPCM),
32955 FORMAT_TYPE (IBM_FORMAT_MULAW),
32956 FORMAT_TYPE (IBM_FORMAT_ALAW),
32957 FORMAT_TYPE (IBM_FORMAT_ADPCM),
32958 FORMAT_TYPE (WAVE_FORMAT_VIVO_G723),
32959 FORMAT_TYPE (WAVE_FORMAT_VIVO_SIREN),
32960 FORMAT_TYPE (WAVE_FORMAT_DIGITAL_G723),
32961 FORMAT_TYPE (WAVE_FORMAT_CREATIVE_ADPCM),
32962 FORMAT_TYPE (WAVE_FORMAT_CREATIVE_FASTSPEECH8),
32963 FORMAT_TYPE (WAVE_FORMAT_CREATIVE_FASTSPEECH10),
32964 FORMAT_TYPE (WAVE_FORMAT_QUARTERDECK),
32965 FORMAT_TYPE (WAVE_FORMAT_FM_TOWNS_SND),
32966 FORMAT_TYPE (WAVE_FORMAT_BZV_DIGITAL),
32967 FORMAT_TYPE (WAVE_FORMAT_VME_VMPCM),
32968 FORMAT_TYPE (WAVE_FORMAT_OLIGSM),
32969 FORMAT_TYPE (WAVE_FORMAT_OLIADPCM),
32970 FORMAT_TYPE (WAVE_FORMAT_OLICELP),
32971 FORMAT_TYPE (WAVE_FORMAT_OLISBC),
32972 FORMAT_TYPE (WAVE_FORMAT_OLIOPR),
32973 FORMAT_TYPE (WAVE_FORMAT_LH_CODEC),
32974 FORMAT_TYPE (WAVE_FORMAT_NORRIS),
32975 FORMAT_TYPE (WAVE_FORMAT_SOUNDSPACE_MUSICOMPRESS),
32976 FORMAT_TYPE (WAVE_FORMAT_DVM),
32977 FORMAT_TYPE (WAVE_FORMAT_INTERWAV_VSC112),
32978 FORMAT_TYPE (WAVE_FORMAT_EXTENSIBLE),
32981 char const*
32982 wav_w64_format_str (int k)
32983 { int lower, upper, mid ;
32985 lower = -1 ;
32986 upper = sizeof (wave_descs) / sizeof (WAV_FORMAT_DESC) ;
32988 /* binary search */
32989 if ((wave_descs [0].ID <= k) & (k <= wave_descs [upper - 1].ID))
32991 while (lower + 1 < upper)
32992 { mid = (upper + lower) / 2 ;
32994 if (k == wave_descs [mid].ID)
32995 return wave_descs [mid].name ;
32996 if (k < wave_descs [mid].ID)
32997 upper = mid ;
32998 else
32999 lower = mid ;
33003 return "Unknown format" ;
33004 } /* wav_w64_format_str */
33007 wav_w64_srate2blocksize (int srate_chan_product)
33008 { if (srate_chan_product < 12000)
33009 return 256 ;
33010 if (srate_chan_product < 23000)
33011 return 512 ;
33012 if (srate_chan_product < 44000)
33013 return 1024 ;
33014 return 2048 ;
33015 } /* srate2blocksize */
33017 ** Do not edit or modify anything in this comment block.
33018 ** The arch-tag line is a file identity tag for the GNU Arch
33019 ** revision control system.
33021 ** arch-tag: 43c1b1dd-8abd-43da-a8cd-44da914b64a5
33024 ** Copyright (C) 2002-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
33026 ** This program is free software; you can redistribute it and/or modify
33027 ** it under the terms of the GNU Lesser General Public License as published by
33028 ** the Free Software Foundation; either version 2.1 of the License, or
33029 ** (at your option) any later version.
33031 ** This program is distributed in the hope that it will be useful,
33032 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
33033 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33034 ** GNU Lesser General Public License for more details.
33036 ** You should have received a copy of the GNU Lesser General Public License
33037 ** along with this program; if not, write to the Free Software
33038 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
33041 #include <stdio.h>
33042 #include <fcntl.h>
33043 #include <string.h>
33044 #include <ctype.h>
33047 #if (ENABLE_EXPERIMENTAL_CODE == 0)
33050 wve_open (SF_PRIVATE *psf)
33051 { if (psf)
33052 return SFE_UNIMPLEMENTED ;
33053 return (psf && 0) ;
33054 } /* wve_open */
33056 #else
33058 #define SFE_WVE_NOT_WVE 666
33060 /*------------------------------------------------------------------------------
33061 ** Macros to handle big/little endian issues.
33064 #define ALAW_MARKER MAKE_MARKER ('A', 'L', 'a', 'w')
33065 #define SOUN_MARKER MAKE_MARKER ('S', 'o', 'u', 'n')
33066 #define DFIL_MARKER MAKE_MARKER ('d', 'F', 'i', 'l')
33068 /*------------------------------------------------------------------------------
33069 ** Private static functions.
33072 static int wve_read_header (SF_PRIVATE *psf) ;
33074 /*------------------------------------------------------------------------------
33075 ** Public function.
33079 wve_open (SF_PRIVATE *psf)
33080 { int subformat, error = 0 ;
33082 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
33083 return SFE_UNIMPLEMENTED ;
33085 if ((error = wve_read_header (psf)))
33086 return error ;
33088 if ((psf->sf.format & SF_FORMAT_TYPEMASK) != SF_FORMAT_WVE)
33089 return SFE_BAD_OPEN_FORMAT ;
33091 subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
33093 return error ;
33094 } /* wve_open */
33096 /*------------------------------------------------------------------------------
33099 static int
33100 wve_read_header (SF_PRIVATE *psf)
33101 { int marker ;
33103 /* Set position to start of file to begin reading header. */
33104 psf_binheader_readf (psf, "pm", 0, &marker) ;
33105 if (marker != ALAW_MARKER)
33106 return SFE_WVE_NOT_WVE ;
33108 psf_binheader_readf (psf, "m", &marker) ;
33109 if (marker != SOUN_MARKER)
33110 return SFE_WVE_NOT_WVE ;
33112 psf_binheader_readf (psf, "m", &marker) ;
33113 if (marker != DFIL_MARKER)
33114 return SFE_WVE_NOT_WVE ;
33116 psf_log_printf (psf, "Read only : Psion Palmtop Alaw (.wve)\n"
33117 " Sample Rate : 8000\n"
33118 " Channels : 1\n"
33119 " Encoding : A-law\n") ;
33121 psf->dataoffset = 0x20 ;
33122 psf->datalength = psf->filelength - psf->dataoffset ;
33124 psf->sf.format = SF_FORMAT_WVE | SF_FORMAT_ALAW ;
33125 psf->sf.samplerate = 8000 ;
33126 psf->sf.frames = psf->datalength ;
33127 psf->sf.channels = 1 ;
33129 return alaw_init (psf) ;
33130 } /* wve_read_header */
33132 /*------------------------------------------------------------------------------
33135 #endif
33137 ** Do not edit or modify anything in this comment block.
33138 ** The arch-tag line is a file identity tag for the GNU Arch
33139 ** revision control system.
33141 ** arch-tag: ba368cb5-523f-45e4-98c1-5b99a102f73f
33144 ** Copyright (C) 2003,2004 Erik de Castro Lopo <erikd@mega-nerd.com>
33146 ** This program is free software; you can redistribute it and/or modify
33147 ** it under the terms of the GNU Lesser General Public License as published by
33148 ** the Free Software Foundation; either version 2.1 of the License, or
33149 ** (at your option) any later version.
33151 ** This program is distributed in the hope that it will be useful,
33152 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
33153 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33154 ** GNU Lesser General Public License for more details.
33156 ** You should have received a copy of the GNU Lesser General Public License
33157 ** along with this program; if not, write to the Free Software
33158 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
33162 #include <stdio.h>
33163 #include <stdlib.h>
33164 #include <fcntl.h>
33165 #include <string.h>
33166 #include <ctype.h>
33169 #define MAX_XI_SAMPLES 16
33171 /*------------------------------------------------------------------------------
33172 ** Private static functions and tyepdefs.
33175 typedef struct
33176 { /* Warning, this filename is NOT nul terminated. */
33177 char filename [22] ;
33178 char software [20] ;
33179 char sample_name [22] ;
33181 int loop_begin, loop_end ;
33182 int sample_flags ;
33184 /* Data for encoder and decoder. */
33185 short last_16 ;
33186 } XI_PRIVATE ;
33188 static int xi_close (SF_PRIVATE *psf) ;
33189 static int xi_write_header (SF_PRIVATE *psf, int calc_length) ;
33190 static int xi_read_header (SF_PRIVATE *psf) ;
33191 static int dpcm_init (SF_PRIVATE *psf) ;
33194 static sf_count_t dpcm_seek (SF_PRIVATE *psf, int mode, sf_count_t offset) ;
33196 /*------------------------------------------------------------------------------
33197 ** Public function.
33201 xi_open (SF_PRIVATE *psf)
33202 { XI_PRIVATE *pxi ;
33203 int subformat, error = 0 ;
33205 if (psf->is_pipe)
33206 return SFE_XI_NO_PIPE ;
33208 if (psf->fdata)
33209 pxi = psf->fdata ;
33210 else if ((pxi = calloc (1, sizeof (XI_PRIVATE))) == NULL)
33211 return SFE_MALLOC_FAILED ;
33213 psf->fdata = pxi ;
33215 if (psf->mode == SFM_READ || (psf->mode == SFM_RDWR && psf->filelength > 0))
33216 { if ((error = xi_read_header (psf)))
33217 return error ;
33220 subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
33222 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
33223 { if ((psf->sf.format & SF_FORMAT_TYPEMASK) != SF_FORMAT_XI)
33224 return SFE_BAD_OPEN_FORMAT ;
33226 psf->endian = SF_ENDIAN_LITTLE ;
33227 psf->sf.channels = 1 ; /* Always mono */
33228 psf->sf.samplerate = 44100 ; /* Always */
33230 /* Set up default instrument and software name. */
33231 memcpy (pxi->filename, "Default Name ", sizeof (pxi->filename)) ;
33232 memcpy (pxi->software, PACKAGE "-" VERSION " ", sizeof (pxi->software)) ;
33234 memset (pxi->sample_name, 0, sizeof (pxi->sample_name)) ;
33235 LSF_SNPRINTF (pxi->sample_name, sizeof (pxi->sample_name), "%s", "Sample #1") ;
33237 pxi->sample_flags = (subformat == SF_FORMAT_DPCM_16) ? 16 : 0 ;
33239 if (xi_write_header (psf, SF_FALSE))
33240 return psf->error ;
33242 psf->write_header = xi_write_header ;
33245 psf->close = xi_close ;
33246 psf->seek = dpcm_seek ;
33248 psf->sf.seekable = SF_FALSE ;
33250 psf->blockwidth = psf->bytewidth * psf->sf.channels ;
33252 switch (subformat)
33253 { case SF_FORMAT_DPCM_8 : /* 8-bit differential PCM. */
33254 case SF_FORMAT_DPCM_16 : /* 16-bit differential PCM. */
33255 error = dpcm_init (psf) ;
33256 break ;
33258 default : break ;
33261 return error ;
33262 } /* xi_open */
33264 /*------------------------------------------------------------------------------
33267 static int
33268 xi_close (SF_PRIVATE *psf)
33270 psf = psf ;
33272 return 0 ;
33273 } /* xi_close */
33275 /*==============================================================================
33278 static sf_count_t dpcm_read_dsc2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
33279 static sf_count_t dpcm_read_dsc2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
33280 static sf_count_t dpcm_read_dsc2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
33281 static sf_count_t dpcm_read_dsc2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
33283 static sf_count_t dpcm_write_s2dsc (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
33284 static sf_count_t dpcm_write_i2dsc (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
33285 static sf_count_t dpcm_write_f2dsc (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
33286 static sf_count_t dpcm_write_d2dsc (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
33288 static sf_count_t dpcm_read_dles2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
33289 static sf_count_t dpcm_read_dles2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
33290 static sf_count_t dpcm_read_dles2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
33291 static sf_count_t dpcm_read_dles2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
33293 static sf_count_t dpcm_write_s2dles (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
33294 static sf_count_t dpcm_write_i2dles (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
33295 static sf_count_t dpcm_write_f2dles (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
33296 static sf_count_t dpcm_write_d2dles (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
33298 static int
33299 dpcm_init (SF_PRIVATE *psf)
33300 { if (psf->bytewidth == 0 || psf->sf.channels == 0)
33301 return SFE_INTERNAL ;
33303 psf->blockwidth = psf->bytewidth * psf->sf.channels ;
33305 if (psf->mode == SFM_READ || psf->mode == SFM_RDWR)
33306 { switch (psf->bytewidth)
33307 { case 1 :
33308 psf->read_short = dpcm_read_dsc2s ;
33309 psf->read_int = dpcm_read_dsc2i ;
33310 psf->read_float = dpcm_read_dsc2f ;
33311 psf->read_double = dpcm_read_dsc2d ;
33312 break ;
33313 case 2 :
33314 psf->read_short = dpcm_read_dles2s ;
33315 psf->read_int = dpcm_read_dles2i ;
33316 psf->read_float = dpcm_read_dles2f ;
33317 psf->read_double = dpcm_read_dles2d ;
33318 break ;
33319 default :
33320 psf_log_printf (psf, "dpcm_init() returning SFE_UNIMPLEMENTED\n") ;
33321 return SFE_UNIMPLEMENTED ;
33325 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
33326 { switch (psf->bytewidth)
33327 { case 1 :
33328 psf->write_short = dpcm_write_s2dsc ;
33329 psf->write_int = dpcm_write_i2dsc ;
33330 psf->write_float = dpcm_write_f2dsc ;
33331 psf->write_double = dpcm_write_d2dsc ;
33332 break ;
33333 case 2 :
33334 psf->write_short = dpcm_write_s2dles ;
33335 psf->write_int = dpcm_write_i2dles ;
33336 psf->write_float = dpcm_write_f2dles ;
33337 psf->write_double = dpcm_write_d2dles ;
33338 break ;
33339 default :
33340 psf_log_printf (psf, "dpcm_init() returning SFE_UNIMPLEMENTED\n") ;
33341 return SFE_UNIMPLEMENTED ;
33345 psf->filelength = psf_get_filelen (psf) ;
33346 psf->datalength = (psf->dataend) ? psf->dataend - psf->dataoffset :
33347 psf->filelength - psf->dataoffset ;
33348 psf->sf.frames = psf->datalength / psf->blockwidth ;
33350 return 0 ;
33351 } /* dpcm_init */
33353 /*==============================================================================
33356 static sf_count_t
33357 dpcm_seek (SF_PRIVATE *psf, int mode, sf_count_t offset)
33358 { XI_PRIVATE *pxi ;
33359 int total, bufferlen, len ;
33361 if ((pxi = psf->fdata) == NULL)
33362 return SFE_INTERNAL ;
33364 if (psf->datalength < 0 || psf->dataoffset < 0)
33365 { psf->error = SFE_BAD_SEEK ;
33366 return ((sf_count_t) -1) ;
33369 if (offset == 0)
33370 { psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
33371 pxi->last_16 = 0 ;
33372 return 0 ;
33375 if (offset < 0 || offset > psf->sf.frames)
33376 { psf->error = SFE_BAD_SEEK ;
33377 return ((sf_count_t) -1) ;
33380 if (mode != SFM_READ)
33381 { /* What to do about write??? */
33382 psf->error = SFE_BAD_SEEK ;
33383 return ((sf_count_t) -1) ;
33386 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
33388 if ((psf->sf.format & SF_FORMAT_SUBMASK) == SF_FORMAT_DPCM_16)
33389 { total = offset ;
33390 bufferlen = sizeof (psf->buffer) / sizeof (short) ;
33391 while (total > 0)
33392 { len = (total > bufferlen) ? bufferlen : total ;
33393 total -= dpcm_read_dles2s (psf, (short *) psf->buffer, len) ;
33396 else
33397 { total = offset ;
33398 bufferlen = sizeof (psf->buffer) / sizeof (short) ;
33399 while (total > 0)
33400 { len = (total > bufferlen) ? bufferlen : total ;
33401 total -= dpcm_read_dsc2s (psf, (short *) psf->buffer, len) ;
33405 return offset ;
33406 } /* dpcm_seek */
33409 static int
33410 xi_write_header (SF_PRIVATE *psf, int calc_length)
33411 { XI_PRIVATE *pxi ;
33412 sf_count_t current ;
33413 const char *string ;
33415 if ((pxi = psf->fdata) == NULL)
33416 return SFE_INTERNAL ;
33418 calc_length = calc_length ; /* Avoid a compiler warning. */
33420 current = psf_ftell (psf) ;
33422 /* Reset the current header length to zero. */
33423 psf->header [0] = 0 ;
33424 psf->headindex = 0 ;
33425 psf_fseek (psf, 0, SEEK_SET) ;
33427 string = "Extended Instrument: " ;
33428 psf_binheader_writef (psf, "b", string, strlen (string)) ;
33429 psf_binheader_writef (psf, "b1", pxi->filename, sizeof (pxi->filename), 0x1A) ;
33431 /* Write software version and two byte XI version. */
33432 psf_binheader_writef (psf, "eb2", pxi->software, sizeof (pxi->software), (1 << 8) + 2) ;
33435 ** Jump note numbers (96), volume envelope (48), pan envelope (48),
33436 ** volume points (1), pan points (1)
33438 psf_binheader_writef (psf, "z", (size_t) (96 + 48 + 48 + 1 + 1)) ;
33440 /* Jump volume loop (3 bytes), pan loop (3), envelope flags (3), vibrato (3)
33441 ** fade out (2), 22 unknown bytes, and then write sample_count (2 bytes).
33443 psf_binheader_writef (psf, "ez2z2", (size_t) (4 * 3), 0x1234, (size_t) 22, 1) ;
33445 psf->sf.frames = 12 ;
33446 pxi->loop_begin = 0 ;
33447 pxi->loop_end = 0 ;
33449 psf_binheader_writef (psf, "et844", psf->sf.frames, pxi->loop_begin, pxi->loop_end) ;
33451 /* volume, fine tune, flags, pan, note, namelen */
33452 psf_binheader_writef (psf, "111111", 128, 0, pxi->sample_flags, 128, 0, strlen (pxi->sample_name)) ;
33454 psf_binheader_writef (psf, "b", pxi->sample_name, sizeof (pxi->sample_name)) ;
33460 /* Header construction complete so write it out. */
33461 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
33463 if (psf->error)
33464 return psf->error ;
33466 psf->dataoffset = psf->headindex ;
33468 if (current > 0)
33469 psf_fseek (psf, current, SEEK_SET) ;
33471 return psf->error ;
33472 } /* xi_write_header */
33474 static int
33475 xi_read_header (SF_PRIVATE *psf)
33476 { char buffer [64], name [32] ;
33477 short version, fade_out, sample_count ;
33478 int k, loop_begin, loop_end ;
33479 int sample_sizes [MAX_XI_SAMPLES] ;
33481 psf_binheader_readf (psf, "pb", 0, buffer, 21) ;
33483 memset (sample_sizes, 0, sizeof (sample_sizes)) ;
33485 buffer [20] = 0 ;
33486 if (strcmp (buffer, "Extended Instrument:") != 0)
33487 return SFE_XI_BAD_HEADER ;
33489 memset (buffer, 0, sizeof (buffer)) ;
33490 psf_binheader_readf (psf, "b", buffer, 23) ;
33492 if (buffer [22] != 0x1A)
33493 return SFE_XI_BAD_HEADER ;
33495 buffer [22] = 0 ;
33496 psf_log_printf (psf, "Extended Instrument : %s\n", buffer) ;
33498 psf_binheader_readf (psf, "be2", buffer, 20, &version) ;
33499 buffer [19] = 0 ;
33500 psf_log_printf (psf, "Software : %s\nVersion : %d.%02d\n", buffer, version / 256, version % 256) ;
33502 /* Jump note numbers (96), volume envelope (48), pan envelope (48),
33503 ** volume points (1), pan points (1)
33505 psf_binheader_readf (psf, "j", 96 + 48 + 48 + 1 + 1) ;
33507 psf_binheader_readf (psf, "b", buffer, 12) ;
33508 psf_log_printf (psf, "Volume Loop\n sustain : %u\n begin : %u\n end : %u\n",
33509 buffer [0], buffer [1], buffer [2]) ;
33510 psf_log_printf (psf, "Pan Loop\n sustain : %u\n begin : %u\n end : %u\n",
33511 buffer [3], buffer [4], buffer [5]) ;
33512 psf_log_printf (psf, "Envelope Flags\n volume : 0x%X\n pan : 0x%X\n",
33513 buffer [6] & 0xFF, buffer [7] & 0xFF) ;
33515 psf_log_printf (psf, "Vibrato\n type : %u\n sweep : %u\n depth : %u\n rate : %u\n",
33516 buffer [8], buffer [9], buffer [10], buffer [11]) ;
33519 ** Read fade_out then jump reserved (2 bytes) and ???? (20 bytes) and
33520 ** sample_count.
33522 psf_binheader_readf (psf, "e2j2", &fade_out, 2 + 20, &sample_count) ;
33523 psf_log_printf (psf, "Fade out : %d\n", fade_out) ;
33525 /* XI file can contain up to 16 samples. */
33526 if (sample_count > MAX_XI_SAMPLES)
33527 return SFE_XI_EXCESS_SAMPLES ;
33529 /* Log all data for each sample. */
33530 for (k = 0 ; k < sample_count ; k++)
33531 { psf_binheader_readf (psf, "e444", &(sample_sizes [k]), &loop_begin, &loop_end) ;
33533 /* Read 5 know bytes, 1 unknown byte and 22 name bytes. */
33534 psf_binheader_readf (psf, "bb", buffer, 6, name, 22) ;
33535 name [21] = 0 ;
33537 psf_log_printf (psf, "Sample #%d\n name : %s\n size : %d\n", k + 1, name, sample_sizes [k]) ;
33538 psf_log_printf (psf, " loop\n begin : %d\n end : %d\n", loop_begin, loop_end) ;
33540 psf_log_printf (psf, " volume : %u\n f. tune : %d\n flags : 0x%02X ",
33541 buffer [0] & 0xFF, buffer [1] & 0xFF, buffer [2] & 0xFF) ;
33543 psf_log_printf (psf, " (") ;
33544 if (buffer [2] & 1)
33545 psf_log_printf (psf, " Loop") ;
33546 if (buffer [2] & 2)
33547 psf_log_printf (psf, " PingPong") ;
33548 psf_log_printf (psf, (buffer [2] & 16) ? " 16bit" : " 8bit") ;
33549 psf_log_printf (psf, " )\n") ;
33551 psf_log_printf (psf, " pan : %u\n note : %d\n namelen : %d\n",
33552 buffer [3] & 0xFF, buffer [4], buffer [5]) ;
33554 if (k != 0)
33555 continue ;
33557 if (buffer [2] & 16)
33558 { psf->sf.format = SF_FORMAT_XI | SF_FORMAT_DPCM_16 ;
33559 psf->bytewidth = 2 ;
33561 else
33562 { psf->sf.format = SF_FORMAT_XI | SF_FORMAT_DPCM_8 ;
33563 psf->bytewidth = 1 ;
33567 while (sample_count > 1 && sample_sizes [sample_count - 1] == 0)
33568 sample_count -- ;
33570 /* Currently, we can only handle 1 sample per file. */
33572 if (sample_count > 2)
33573 { psf_log_printf (psf, "*** Sample count is less than 16 but more than 1.\n") ;
33574 psf_log_printf (psf, " sample count : %d sample_sizes [%d] : %d\n",
33575 sample_count, sample_count - 1, sample_sizes [sample_count - 1]) ;
33576 return SFE_XI_EXCESS_SAMPLES ;
33579 psf->dataoffset = psf_fseek (psf, 0, SEEK_CUR) ;
33580 psf_log_printf (psf, "Data Offset : %D\n", psf->dataoffset) ;
33582 psf->datalength = sample_sizes [0] ;
33584 if (psf->dataoffset + psf->datalength > psf->filelength)
33585 { psf_log_printf (psf, "*** File seems to be truncated. Should be at least %D bytes long.\n",
33586 psf->dataoffset + sample_sizes [0]) ;
33587 psf->datalength = psf->filelength - psf->dataoffset ;
33590 if (psf_fseek (psf, psf->dataoffset, SEEK_SET) != psf->dataoffset)
33591 return SFE_BAD_SEEK ;
33593 psf->close = xi_close ;
33595 psf->endian = SF_ENDIAN_LITTLE ;
33596 psf->sf.channels = 1 ; /* Always mono */
33597 psf->sf.samplerate = 44100 ; /* Always */
33599 psf->blockwidth = psf->sf.channels * psf->bytewidth ;
33601 if (! psf->sf.frames && psf->blockwidth)
33602 psf->sf.frames = (psf->filelength - psf->dataoffset) / psf->blockwidth ;
33604 return 0 ;
33605 } /* xi_read_header */
33607 /*==============================================================================
33610 static void dsc2s_array (XI_PRIVATE *pxi, signed char *src, int count, short *dest) ;
33611 static void dsc2i_array (XI_PRIVATE *pxi, signed char *src, int count, int *dest) ;
33612 static void dsc2f_array (XI_PRIVATE *pxi, signed char *src, int count, float *dest, float normfact) ;
33613 static void dsc2d_array (XI_PRIVATE *pxi, signed char *src, int count, double *dest, double normfact) ;
33615 static void dles2s_array (XI_PRIVATE *pxi, short *src, int count, short *dest) ;
33616 static void dles2i_array (XI_PRIVATE *pxi, short *src, int count, int *dest) ;
33617 static void dles2f_array (XI_PRIVATE *pxi, short *src, int count, float *dest, float normfact) ;
33618 static void dles2d_array (XI_PRIVATE *pxi, short *src, int count, double *dest, double normfact) ;
33620 static sf_count_t
33621 dpcm_read_dsc2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
33622 { XI_PRIVATE *pxi ;
33623 int bufferlen, readcount, thisread ;
33624 sf_count_t total = 0 ;
33626 if ((pxi = psf->fdata) == NULL)
33627 return 0 ;
33629 bufferlen = sizeof (psf->buffer) / sizeof (signed char) ;
33631 while (len > 0)
33632 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
33633 thisread = psf_fread (psf->buffer, sizeof (signed char), readcount, psf) ;
33634 dsc2s_array (pxi, (signed char*) (psf->buffer), thisread, ptr + total) ;
33635 total += thisread ;
33636 len -= thisread ;
33637 if (thisread < readcount)
33638 break ;
33641 return total ;
33642 } /* dpcm_read_dsc2s */
33644 static sf_count_t
33645 dpcm_read_dsc2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
33646 { XI_PRIVATE *pxi ;
33647 int bufferlen, readcount, thisread ;
33648 sf_count_t total = 0 ;
33650 if ((pxi = psf->fdata) == NULL)
33651 return 0 ;
33653 bufferlen = sizeof (psf->buffer) / sizeof (signed char) ;
33655 while (len > 0)
33656 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
33657 thisread = psf_fread (psf->buffer, sizeof (signed char), readcount, psf) ;
33658 dsc2i_array (pxi, (signed char*) (psf->buffer), thisread, ptr + total) ;
33659 total += thisread ;
33660 len -= thisread ;
33661 if (thisread < readcount)
33662 break ;
33665 return total ;
33666 } /* dpcm_read_dsc2i */
33668 static sf_count_t
33669 dpcm_read_dsc2f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
33670 { XI_PRIVATE *pxi ;
33671 int bufferlen, readcount, thisread ;
33672 sf_count_t total = 0 ;
33673 float normfact ;
33675 if ((pxi = psf->fdata) == NULL)
33676 return 0 ;
33678 normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x80) : 1.0 ;
33680 bufferlen = sizeof (psf->buffer) / sizeof (signed char) ;
33682 while (len > 0)
33683 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
33684 thisread = psf_fread (psf->buffer, sizeof (signed char), readcount, psf) ;
33685 dsc2f_array (pxi, (signed char*) (psf->buffer), thisread, ptr + total, normfact) ;
33686 total += thisread ;
33687 len -= thisread ;
33688 if (thisread < readcount)
33689 break ;
33692 return total ;
33693 } /* dpcm_read_dsc2f */
33695 static sf_count_t
33696 dpcm_read_dsc2d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
33697 { XI_PRIVATE *pxi ;
33698 int bufferlen, readcount, thisread ;
33699 sf_count_t total = 0 ;
33700 double normfact ;
33702 if ((pxi = psf->fdata) == NULL)
33703 return 0 ;
33705 normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x80) : 1.0 ;
33707 bufferlen = sizeof (psf->buffer) / sizeof (signed char) ;
33709 while (len > 0)
33710 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
33711 thisread = psf_fread (psf->buffer, sizeof (signed char), readcount, psf) ;
33712 dsc2d_array (pxi, (signed char*) (psf->buffer), thisread, ptr + total, normfact) ;
33713 total += thisread ;
33714 len -= thisread ;
33715 if (thisread < readcount)
33716 break ;
33719 return total ;
33720 } /* dpcm_read_dsc2d */
33722 /*------------------------------------------------------------------------------
33725 static sf_count_t
33726 dpcm_read_dles2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
33727 { XI_PRIVATE *pxi ;
33728 int bufferlen, readcount, thisread ;
33729 sf_count_t total = 0 ;
33731 if ((pxi = psf->fdata) == NULL)
33732 return 0 ;
33734 bufferlen = sizeof (psf->buffer) / sizeof (short) ;
33736 while (len > 0)
33737 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
33738 thisread = psf_fread (psf->buffer, sizeof (short), readcount, psf) ;
33739 dles2s_array (pxi, (short*) (psf->buffer), thisread, ptr + total) ;
33740 total += thisread ;
33741 len -= thisread ;
33742 if (thisread < readcount)
33743 break ;
33746 return total ;
33747 } /* dpcm_read_dles2s */
33749 static sf_count_t
33750 dpcm_read_dles2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
33751 { XI_PRIVATE *pxi ;
33752 int bufferlen, readcount, thisread ;
33753 sf_count_t total = 0 ;
33755 if ((pxi = psf->fdata) == NULL)
33756 return 0 ;
33758 bufferlen = sizeof (psf->buffer) / sizeof (short) ;
33760 while (len > 0)
33761 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
33762 thisread = psf_fread (psf->buffer, sizeof (short), readcount, psf) ;
33763 dles2i_array (pxi, (short*) (psf->buffer), thisread, ptr + total) ;
33764 total += thisread ;
33765 len -= thisread ;
33766 if (thisread < readcount)
33767 break ;
33770 return total ;
33771 } /* dpcm_read_dles2i */
33773 static sf_count_t
33774 dpcm_read_dles2f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
33775 { XI_PRIVATE *pxi ;
33776 int bufferlen, readcount, thisread ;
33777 sf_count_t total = 0 ;
33778 float normfact ;
33780 if ((pxi = psf->fdata) == NULL)
33781 return 0 ;
33783 normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x8000) : 1.0 ;
33785 bufferlen = sizeof (psf->buffer) / sizeof (short) ;
33787 while (len > 0)
33788 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
33789 thisread = psf_fread (psf->buffer, sizeof (short), readcount, psf) ;
33790 dles2f_array (pxi, (short*) (psf->buffer), thisread, ptr + total, normfact) ;
33791 total += thisread ;
33792 len -= thisread ;
33793 if (thisread < readcount)
33794 break ;
33797 return total ;
33798 } /* dpcm_read_dles2f */
33800 static sf_count_t
33801 dpcm_read_dles2d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
33802 { XI_PRIVATE *pxi ;
33803 int bufferlen, readcount, thisread ;
33804 sf_count_t total = 0 ;
33805 double normfact ;
33807 if ((pxi = psf->fdata) == NULL)
33808 return 0 ;
33810 normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x8000) : 1.0 ;
33812 bufferlen = sizeof (psf->buffer) / sizeof (short) ;
33814 while (len > 0)
33815 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
33816 thisread = psf_fread (psf->buffer, sizeof (short), readcount, psf) ;
33817 dles2d_array (pxi, (short*) (psf->buffer), thisread, ptr + total, normfact) ;
33818 total += thisread ;
33819 len -= thisread ;
33820 if (thisread < readcount)
33821 break ;
33824 return total ;
33825 } /* dpcm_read_dles2d */
33827 /*==============================================================================
33830 static void s2dsc_array (XI_PRIVATE *pxi, short *src, signed char *dest, int count) ;
33831 static void i2dsc_array (XI_PRIVATE *pxi, int *src, signed char *dest, int count) ;
33832 static void f2dsc_array (XI_PRIVATE *pxi, float *src, signed char *dest, int count, float normfact) ;
33833 static void d2dsc_array (XI_PRIVATE *pxi, double *src, signed char *dest, int count, double normfact) ;
33835 static void s2dles_array (XI_PRIVATE *pxi, short *src, short *dest, int count) ;
33836 static void i2dles_array (XI_PRIVATE *pxi, int *src, short *dest, int count) ;
33837 static void f2dles_array (XI_PRIVATE *pxi, float *src, short *dest, int count, float normfact) ;
33838 static void d2dles_array (XI_PRIVATE *pxi, double *src, short *dest, int count, double normfact) ;
33841 static sf_count_t
33842 dpcm_write_s2dsc (SF_PRIVATE *psf, short *ptr, sf_count_t len)
33843 { XI_PRIVATE *pxi ;
33844 int bufferlen, writecount, thiswrite ;
33845 sf_count_t total = 0 ;
33847 if ((pxi = psf->fdata) == NULL)
33848 return 0 ;
33850 bufferlen = sizeof (psf->buffer) / sizeof (signed char) ;
33852 while (len > 0)
33853 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
33854 s2dsc_array (pxi, ptr + total, (signed char*) (psf->buffer), writecount) ;
33855 thiswrite = psf_fwrite (psf->buffer, sizeof (signed char), writecount, psf) ;
33856 total += thiswrite ;
33857 len -= thiswrite ;
33858 if (thiswrite < writecount)
33859 break ;
33862 return total ;
33863 } /* dpcm_write_s2dsc */
33865 static sf_count_t
33866 dpcm_write_i2dsc (SF_PRIVATE *psf, int *ptr, sf_count_t len)
33867 { XI_PRIVATE *pxi ;
33868 int bufferlen, writecount, thiswrite ;
33869 sf_count_t total = 0 ;
33871 if ((pxi = psf->fdata) == NULL)
33872 return 0 ;
33874 bufferlen = sizeof (psf->buffer) / sizeof (signed char) ;
33876 while (len > 0)
33877 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
33878 i2dsc_array (pxi, ptr + total, (signed char*) (psf->buffer), writecount) ;
33879 thiswrite = psf_fwrite (psf->buffer, sizeof (signed char), writecount, psf) ;
33880 total += thiswrite ;
33881 len -= thiswrite ;
33882 if (thiswrite < writecount)
33883 break ;
33886 return total ;
33887 } /* dpcm_write_i2dsc */
33889 static sf_count_t
33890 dpcm_write_f2dsc (SF_PRIVATE *psf, float *ptr, sf_count_t len)
33891 { XI_PRIVATE *pxi ;
33892 int bufferlen, writecount, thiswrite ;
33893 sf_count_t total = 0 ;
33894 float normfact ;
33896 if ((pxi = psf->fdata) == NULL)
33897 return 0 ;
33899 normfact = (psf->norm_float == SF_TRUE) ? (1.0 * 0x7F) : 1.0 ;
33901 bufferlen = sizeof (psf->buffer) / sizeof (signed char) ;
33903 while (len > 0)
33904 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
33905 f2dsc_array (pxi, ptr + total, (signed char*) (psf->buffer), writecount, normfact) ;
33906 thiswrite = psf_fwrite (psf->buffer, sizeof (signed char), writecount, psf) ;
33907 total += thiswrite ;
33908 len -= thiswrite ;
33909 if (thiswrite < writecount)
33910 break ;
33913 return total ;
33914 } /* dpcm_write_f2dsc */
33916 static sf_count_t
33917 dpcm_write_d2dsc (SF_PRIVATE *psf, double *ptr, sf_count_t len)
33918 { XI_PRIVATE *pxi ;
33919 int bufferlen, writecount, thiswrite ;
33920 sf_count_t total = 0 ;
33921 double normfact ;
33923 if ((pxi = psf->fdata) == NULL)
33924 return 0 ;
33926 normfact = (psf->norm_double == SF_TRUE) ? (1.0 * 0x7F) : 1.0 ;
33928 bufferlen = sizeof (psf->buffer) / sizeof (signed char) ;
33930 while (len > 0)
33931 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
33932 d2dsc_array (pxi, ptr + total, (signed char*) (psf->buffer), writecount, normfact) ;
33933 thiswrite = psf_fwrite (psf->buffer, sizeof (signed char), writecount, psf) ;
33934 total += thiswrite ;
33935 len -= thiswrite ;
33936 if (thiswrite < writecount)
33937 break ;
33940 return total ;
33941 } /* dpcm_write_d2dsc */
33944 static sf_count_t
33945 dpcm_write_s2dles (SF_PRIVATE *psf, short *ptr, sf_count_t len)
33946 { XI_PRIVATE *pxi ;
33947 int bufferlen, writecount, thiswrite ;
33948 sf_count_t total = 0 ;
33950 if ((pxi = psf->fdata) == NULL)
33951 return 0 ;
33953 bufferlen = sizeof (psf->buffer) / sizeof (short) ;
33955 while (len > 0)
33956 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
33957 s2dles_array (pxi, ptr + total, (short*) (psf->buffer), writecount) ;
33958 thiswrite = psf_fwrite (psf->buffer, sizeof (short), writecount, psf) ;
33959 total += thiswrite ;
33960 len -= thiswrite ;
33961 if (thiswrite < writecount)
33962 break ;
33965 return total ;
33966 } /* dpcm_write_s2dles */
33968 static sf_count_t
33969 dpcm_write_i2dles (SF_PRIVATE *psf, int *ptr, sf_count_t len)
33970 { XI_PRIVATE *pxi ;
33971 int bufferlen, writecount, thiswrite ;
33972 sf_count_t total = 0 ;
33974 if ((pxi = psf->fdata) == NULL)
33975 return 0 ;
33977 bufferlen = sizeof (psf->buffer) / sizeof (short) ;
33979 while (len > 0)
33980 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
33981 i2dles_array (pxi, ptr + total, (short*) (psf->buffer), writecount) ;
33982 thiswrite = psf_fwrite (psf->buffer, sizeof (short), writecount, psf) ;
33983 total += thiswrite ;
33984 len -= thiswrite ;
33985 if (thiswrite < writecount)
33986 break ;
33989 return total ;
33990 } /* dpcm_write_i2dles */
33992 static sf_count_t
33993 dpcm_write_f2dles (SF_PRIVATE *psf, float *ptr, sf_count_t len)
33994 { XI_PRIVATE *pxi ;
33995 int bufferlen, writecount, thiswrite ;
33996 sf_count_t total = 0 ;
33997 float normfact ;
33999 if ((pxi = psf->fdata) == NULL)
34000 return 0 ;
34002 normfact = (psf->norm_float == SF_TRUE) ? (1.0 * 0x7FFF) : 1.0 ;
34004 bufferlen = sizeof (psf->buffer) / sizeof (short) ;
34006 while (len > 0)
34007 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
34008 f2dles_array (pxi, ptr + total, (short*) (psf->buffer), writecount, normfact) ;
34009 thiswrite = psf_fwrite (psf->buffer, sizeof (short), writecount, psf) ;
34010 total += thiswrite ;
34011 len -= thiswrite ;
34012 if (thiswrite < writecount)
34013 break ;
34016 return total ;
34017 } /* dpcm_write_f2dles */
34019 static sf_count_t
34020 dpcm_write_d2dles (SF_PRIVATE *psf, double *ptr, sf_count_t len)
34021 { XI_PRIVATE *pxi ;
34022 int bufferlen, writecount, thiswrite ;
34023 sf_count_t total = 0 ;
34024 double normfact ;
34026 if ((pxi = psf->fdata) == NULL)
34027 return 0 ;
34029 normfact = (psf->norm_double == SF_TRUE) ? (1.0 * 0x7FFF) : 1.0 ;
34031 bufferlen = sizeof (psf->buffer) / sizeof (short) ;
34033 while (len > 0)
34034 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
34035 d2dles_array (pxi, ptr + total, (short*) (psf->buffer), writecount, normfact) ;
34036 thiswrite = psf_fwrite (psf->buffer, sizeof (short), writecount, psf) ;
34037 total += thiswrite ;
34038 len -= thiswrite ;
34039 if (thiswrite < writecount)
34040 break ;
34043 return total ;
34044 } /* dpcm_write_d2dles */
34047 /*==============================================================================
34050 static void
34051 dsc2s_array (XI_PRIVATE *pxi, signed char *src, int count, short *dest)
34052 { signed char last_val ;
34053 int k ;
34055 last_val = pxi->last_16 >> 8 ;
34057 for (k = 0 ; k < count ; k++)
34058 { last_val += src [k] ;
34059 dest [k] = last_val << 8 ;
34062 pxi->last_16 = last_val << 8 ;
34063 } /* dsc2s_array */
34065 static void
34066 dsc2i_array (XI_PRIVATE *pxi, signed char *src, int count, int *dest)
34067 { signed char last_val ;
34068 int k ;
34070 last_val = pxi->last_16 >> 8 ;
34072 for (k = 0 ; k < count ; k++)
34073 { last_val += src [k] ;
34074 dest [k] = last_val << 24 ;
34077 pxi->last_16 = last_val << 8 ;
34078 } /* dsc2i_array */
34080 static void
34081 dsc2f_array (XI_PRIVATE *pxi, signed char *src, int count, float *dest, float normfact)
34082 { signed char last_val ;
34083 int k ;
34085 last_val = pxi->last_16 >> 8 ;
34087 for (k = 0 ; k < count ; k++)
34088 { last_val += src [k] ;
34089 dest [k] = last_val * normfact ;
34092 pxi->last_16 = last_val << 8 ;
34093 } /* dsc2f_array */
34095 static void
34096 dsc2d_array (XI_PRIVATE *pxi, signed char *src, int count, double *dest, double normfact)
34097 { signed char last_val ;
34098 int k ;
34100 last_val = pxi->last_16 >> 8 ;
34102 for (k = 0 ; k < count ; k++)
34103 { last_val += src [k] ;
34104 dest [k] = last_val * normfact ;
34107 pxi->last_16 = last_val << 8 ;
34108 } /* dsc2d_array */
34110 /*------------------------------------------------------------------------------
34113 static void
34114 s2dsc_array (XI_PRIVATE *pxi, short *src, signed char *dest, int count)
34115 { signed char last_val, current ;
34116 int k ;
34118 last_val = pxi->last_16 >> 8 ;
34120 for (k = 0 ; k < count ; k++)
34121 { current = src [k] >> 8 ;
34122 dest [k] = current - last_val ;
34123 last_val = current ;
34126 pxi->last_16 = last_val << 8 ;
34127 } /* s2dsc_array */
34129 static void
34130 i2dsc_array (XI_PRIVATE *pxi, int *src, signed char *dest, int count)
34131 { signed char last_val, current ;
34132 int k ;
34134 last_val = pxi->last_16 >> 8 ;
34136 for (k = 0 ; k < count ; k++)
34137 { current = src [k] >> 24 ;
34138 dest [k] = current - last_val ;
34139 last_val = current ;
34142 pxi->last_16 = last_val << 8 ;
34143 } /* i2dsc_array */
34145 static void
34146 f2dsc_array (XI_PRIVATE *pxi, float *src, signed char *dest, int count, float normfact)
34147 { signed char last_val, current ;
34148 int k ;
34150 last_val = pxi->last_16 >> 8 ;
34152 for (k = 0 ; k < count ; k++)
34153 { current = lrintf (src [k] * normfact) ;
34154 dest [k] = current - last_val ;
34155 last_val = current ;
34158 pxi->last_16 = last_val << 8 ;
34159 } /* f2dsc_array */
34161 static void
34162 d2dsc_array (XI_PRIVATE *pxi, double *src, signed char *dest, int count, double normfact)
34163 { signed char last_val, current ;
34164 int k ;
34166 last_val = pxi->last_16 >> 8 ;
34168 for (k = 0 ; k < count ; k++)
34169 { current = lrint (src [k] * normfact) ;
34170 dest [k] = current - last_val ;
34171 last_val = current ;
34174 pxi->last_16 = last_val << 8 ;
34175 } /* d2dsc_array */
34177 /*==============================================================================
34180 static void
34181 dles2s_array (XI_PRIVATE *pxi, short *src, int count, short *dest)
34182 { short last_val ;
34183 int k ;
34185 last_val = pxi->last_16 ;
34187 for (k = 0 ; k < count ; k++)
34188 { last_val += LES2H_SHORT (src [k]) ;
34189 dest [k] = last_val ;
34192 pxi->last_16 = last_val ;
34193 } /* dles2s_array */
34195 static void
34196 dles2i_array (XI_PRIVATE *pxi, short *src, int count, int *dest)
34197 { short last_val ;
34198 int k ;
34200 last_val = pxi->last_16 ;
34202 for (k = 0 ; k < count ; k++)
34203 { last_val += LES2H_SHORT (src [k]) ;
34204 dest [k] = last_val << 16 ;
34207 pxi->last_16 = last_val ;
34208 } /* dles2i_array */
34210 static void
34211 dles2f_array (XI_PRIVATE *pxi, short *src, int count, float *dest, float normfact)
34212 { short last_val ;
34213 int k ;
34215 last_val = pxi->last_16 ;
34217 for (k = 0 ; k < count ; k++)
34218 { last_val += LES2H_SHORT (src [k]) ;
34219 dest [k] = last_val * normfact ;
34222 pxi->last_16 = last_val ;
34223 } /* dles2f_array */
34225 static void
34226 dles2d_array (XI_PRIVATE *pxi, short *src, int count, double *dest, double normfact)
34227 { short last_val ;
34228 int k ;
34230 last_val = pxi->last_16 ;
34232 for (k = 0 ; k < count ; k++)
34233 { last_val += LES2H_SHORT (src [k]) ;
34234 dest [k] = last_val * normfact ;
34237 pxi->last_16 = last_val ;
34238 } /* dles2d_array */
34240 /*------------------------------------------------------------------------------
34243 static void
34244 s2dles_array (XI_PRIVATE *pxi, short *src, short *dest, int count)
34245 { short diff, last_val ;
34246 int k ;
34248 last_val = pxi->last_16 ;
34250 for (k = 0 ; k < count ; k++)
34251 { diff = src [k] - last_val ;
34252 dest [k] = LES2H_SHORT (diff) ;
34253 last_val = src [k] ;
34256 pxi->last_16 = last_val ;
34257 } /* s2dles_array */
34259 static void
34260 i2dles_array (XI_PRIVATE *pxi, int *src, short *dest, int count)
34261 { short diff, last_val ;
34262 int k ;
34264 last_val = pxi->last_16 ;
34266 for (k = 0 ; k < count ; k++)
34267 { diff = (src [k] >> 16) - last_val ;
34268 dest [k] = LES2H_SHORT (diff) ;
34269 last_val = src [k] >> 16 ;
34272 pxi->last_16 = last_val ;
34273 } /* i2dles_array */
34275 static void
34276 f2dles_array (XI_PRIVATE *pxi, float *src, short *dest, int count, float normfact)
34277 { short diff, last_val, current ;
34278 int k ;
34280 last_val = pxi->last_16 ;
34282 for (k = 0 ; k < count ; k++)
34283 { current = lrintf (src [k] * normfact) ;
34284 diff = current - last_val ;
34285 dest [k] = LES2H_SHORT (diff) ;
34286 last_val = current ;
34289 pxi->last_16 = last_val ;
34290 } /* f2dles_array */
34292 static void
34293 d2dles_array (XI_PRIVATE *pxi, double *src, short *dest, int count, double normfact)
34294 { short diff, last_val, current ;
34295 int k ;
34297 last_val = pxi->last_16 ;
34299 for (k = 0 ; k < count ; k++)
34300 { current = lrint (src [k] * normfact) ;
34301 diff = current - last_val ;
34302 dest [k] = LES2H_SHORT (diff) ;
34303 last_val = current ;
34306 pxi->last_16 = last_val ;
34307 } /* d2dles_array */
34310 ** Do not edit or modify anything in this comment block.
34311 ** The arch-tag line is a file identity tag for the GNU Arch
34312 ** revision control system.
34314 ** arch-tag: 1ab2dbe0-29af-4d80-9c6f-cb21b67521bc