*** empty log message ***
[chuck-blob.git] / exile / v1 / src / util_sndfile.c
blob11704c4f2db8defa1be4a1e333c2da1daf8a9b81
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"
56 * Copyright 1992 by Jutta Degener and Carsten Bormann, Technische
57 * Universitaet Berlin. See the accompanying file "COPYRIGHT" for
58 * details. THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE.
62 * See private.h for the more commonly used macro versions.
65 #include <stdio.h>
66 #include <assert.h>
69 #define saturate(x) \
70 ((x) < MIN_WORD ? MIN_WORD : (x) > MAX_WORD ? MAX_WORD: (x))
72 word gsm_add ( word a, word b)
74 longword sum = (longword)a + (longword)b;
75 return saturate(sum);
78 word gsm_sub ( word a, word b)
80 longword diff = (longword)a - (longword)b;
81 return saturate(diff);
84 word gsm_mult ( word a, word b)
86 if (a == MIN_WORD && b == MIN_WORD)
87 return MAX_WORD;
89 return SASR_L( (longword)a * (longword)b, 15 );
92 word gsm_mult_r ( word a, word b)
94 if (b == MIN_WORD && a == MIN_WORD) return MAX_WORD;
95 else {
96 longword prod = (longword)a * (longword)b + 16384;
97 prod >>= 15;
98 return prod & 0xFFFF;
102 word gsm_abs (word a)
104 return a < 0 ? (a == MIN_WORD ? MAX_WORD : -a) : a;
107 longword gsm_L_mult (word a, word b)
109 assert( a != MIN_WORD || b != MIN_WORD );
110 return ((longword)a * (longword)b) << 1;
113 longword gsm_L_add ( longword a, longword b)
115 if (a < 0) {
116 if (b >= 0) return a + b;
117 else {
118 ulongword A = (ulongword)-(a + 1) + (ulongword)-(b + 1);
119 return A >= MAX_LONGWORD ? MIN_LONGWORD :-(longword)A-2;
122 else if (b <= 0) return a + b;
123 else {
124 ulongword A = (ulongword)a + (ulongword)b;
125 return A > MAX_LONGWORD ? MAX_LONGWORD : A;
129 longword gsm_L_sub ( longword a, longword b)
131 if (a >= 0) {
132 if (b >= 0) return a - b;
133 else {
134 /* a>=0, b<0 */
136 ulongword A = (ulongword)a + -(b + 1);
137 return A >= MAX_LONGWORD ? MAX_LONGWORD : (A + 1);
140 else if (b <= 0) return a - b;
141 else {
142 /* a<0, b>0 */
144 ulongword A = (ulongword)-(a + 1) + b;
145 return A >= MAX_LONGWORD ? MIN_LONGWORD : -(longword)A - 1;
149 static unsigned char const bitoff[ 256 ] = {
150 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
151 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
152 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
153 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
154 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
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 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
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
168 word gsm_norm (longword a )
170 * the number of left shifts needed to normalize the 32 bit
171 * variable L_var1 for positive values on the interval
173 * with minimum of
174 * minimum of 1073741824 (01000000000000000000000000000000) and
175 * maximum of 2147483647 (01111111111111111111111111111111)
178 * and for negative values on the interval with
179 * minimum of -2147483648 (-10000000000000000000000000000000) and
180 * maximum of -1073741824 ( -1000000000000000000000000000000).
182 * in order to normalize the result, the following
183 * operation must be done: L_norm_var1 = L_var1 << norm( L_var1 );
185 * (That's 'ffs', only from the left, not the right..)
188 assert(a != 0);
190 if (a < 0) {
191 if (a <= -1073741824) return 0;
192 a = ~a;
195 return a & 0xffff0000
196 ? ( a & 0xff000000
197 ? -1 + bitoff[ 0xFF & (a >> 24) ]
198 : 7 + bitoff[ 0xFF & (a >> 16) ] )
199 : ( a & 0xff00
200 ? 15 + bitoff[ 0xFF & (a >> 8) ]
201 : 23 + bitoff[ 0xFF & a ] );
204 longword gsm_L_asl (longword a, int n)
206 if (n >= 32) return 0;
207 if (n <= -32) return -(a < 0);
208 if (n < 0) return gsm_L_asr(a, -n);
209 return a << n;
212 word gsm_asr (word a, int n)
214 if (n >= 16) return -(a < 0);
215 if (n <= -16) return 0;
216 if (n < 0) return a << -n;
218 return SASR_W (a, (word) n);
221 word gsm_asl (word a, int n)
223 if (n >= 16) return 0;
224 if (n <= -16) return -(a < 0);
225 if (n < 0) return gsm_asr(a, -n);
226 return a << n;
229 longword gsm_L_asr (longword a, int n)
231 if (n >= 32) return -(a < 0);
232 if (n <= -32) return 0;
233 if (n < 0) return a << -n;
235 return SASR_L (a, (word) n);
239 ** word gsm_asr (word a, int n)
240 ** {
241 ** if (n >= 16) return -(a < 0);
242 ** if (n <= -16) return 0;
243 ** if (n < 0) return a << -n;
245 ** # ifdef SASR_W
246 ** return a >> n;
247 ** # else
248 ** if (a >= 0) return a >> n;
249 ** else return -(word)( -(uword)a >> n );
250 ** # endif
251 ** }
255 * (From p. 46, end of section 4.2.5)
257 * NOTE: The following lines gives [sic] one correct implementation
258 * of the div(num, denum) arithmetic operation. Compute div
259 * which is the integer division of num by denum: with denum
260 * >= num > 0
263 word gsm_div (word num, word denum)
265 longword L_num = num;
266 longword L_denum = denum;
267 word div = 0;
268 int k = 15;
270 /* The parameter num sometimes becomes zero.
271 * Although this is explicitly guarded against in 4.2.5,
272 * we assume that the result should then be zero as well.
275 /* assert(num != 0); */
277 assert(num >= 0 && denum >= num);
278 if (num == 0)
279 return 0;
281 while (k--) {
282 div <<= 1;
283 L_num <<= 1;
285 if (L_num >= L_denum) {
286 L_num -= L_denum;
287 div++;
291 return div;
294 ** Do not edit or modify anything in this comment block.
295 ** The arch-tag line is a file identity tag for the GNU Arch
296 ** revision control system.
298 ** arch-tag: a7398579-e2e1-4733-aa2d-4c6efc0c58ff
302 ** Copyright (C) 1999-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
304 ** This program is free software; you can redistribute it and/or modify
305 ** it under the terms of the GNU Lesser General Public License as published by
306 ** the Free Software Foundation; either version 2.1 of the License, or
307 ** (at your option) any later version.
309 ** This program is distributed in the hope that it will be useful,
310 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
311 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
312 ** GNU Lesser General Public License for more details.
314 ** You should have received a copy of the GNU Lesser General Public License
315 ** along with this program; if not, write to the Free Software
316 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
320 #include <stdio.h>
321 #include <stdlib.h>
322 #include <string.h>
323 #include <time.h>
324 #include <ctype.h>
327 /*------------------------------------------------------------------------------
328 * Macros to handle big/little endian issues.
331 #define FORM_MARKER (MAKE_MARKER ('F', 'O', 'R', 'M'))
332 #define AIFF_MARKER (MAKE_MARKER ('A', 'I', 'F', 'F'))
333 #define AIFC_MARKER (MAKE_MARKER ('A', 'I', 'F', 'C'))
334 #define COMM_MARKER (MAKE_MARKER ('C', 'O', 'M', 'M'))
335 #define SSND_MARKER (MAKE_MARKER ('S', 'S', 'N', 'D'))
336 #define MARK_MARKER (MAKE_MARKER ('M', 'A', 'R', 'K'))
337 #define INST_MARKER (MAKE_MARKER ('I', 'N', 'S', 'T'))
338 #define APPL_MARKER (MAKE_MARKER ('A', 'P', 'P', 'L'))
340 #define c_MARKER (MAKE_MARKER ('(', 'c', ')', ' '))
341 #define NAME_MARKER (MAKE_MARKER ('N', 'A', 'M', 'E'))
342 #define AUTH_MARKER (MAKE_MARKER ('A', 'U', 'T', 'H'))
343 #define ANNO_MARKER (MAKE_MARKER ('A', 'N', 'N', 'O'))
344 #define COMT_MARKER (MAKE_MARKER ('C', 'O', 'M', 'T'))
345 #define FVER_MARKER (MAKE_MARKER ('F', 'V', 'E', 'R'))
346 #define SFX_MARKER (MAKE_MARKER ('S', 'F', 'X', '!'))
348 #define PEAK_MARKER (MAKE_MARKER ('P', 'E', 'A', 'K'))
350 /* Supported AIFC encodings.*/
351 #define NONE_MARKER (MAKE_MARKER ('N', 'O', 'N', 'E'))
352 #define sowt_MARKER (MAKE_MARKER ('s', 'o', 'w', 't'))
353 #define twos_MARKER (MAKE_MARKER ('t', 'w', 'o', 's'))
354 #define raw_MARKER (MAKE_MARKER ('r', 'a', 'w', ' '))
355 #define in32_MARKER (MAKE_MARKER ('i', 'n', '3', '2'))
356 #define ni32_MARKER (MAKE_MARKER ('2', '3', 'n', 'i'))
358 #define fl32_MARKER (MAKE_MARKER ('f', 'l', '3', '2'))
359 #define FL32_MARKER (MAKE_MARKER ('F', 'L', '3', '2'))
360 #define fl64_MARKER (MAKE_MARKER ('f', 'l', '6', '4'))
361 #define FL64_MARKER (MAKE_MARKER ('F', 'L', '6', '4'))
363 #define ulaw_MARKER (MAKE_MARKER ('u', 'l', 'a', 'w'))
364 #define ULAW_MARKER (MAKE_MARKER ('U', 'L', 'A', 'W'))
365 #define alaw_MARKER (MAKE_MARKER ('a', 'l', 'a', 'w'))
366 #define ALAW_MARKER (MAKE_MARKER ('A', 'L', 'A', 'W'))
368 #define DWVW_MARKER (MAKE_MARKER ('D', 'W', 'V', 'W'))
369 #define GSM_MARKER (MAKE_MARKER ('G', 'S', 'M', ' '))
370 #define ima4_MARKER (MAKE_MARKER ('i', 'm', 'a', '4'))
372 /* Unsupported AIFC encodings.*/
374 #define MAC3_MARKER (MAKE_MARKER ('M', 'A', 'C', '3'))
375 #define MAC6_MARKER (MAKE_MARKER ('M', 'A', 'C', '6'))
376 #define ADP4_MARKER (MAKE_MARKER ('A', 'D', 'P', '4'))
378 /* Predfined chunk sizes. */
379 #define SIZEOF_AIFF_COMM 18
380 #define SIZEOF_AIFC_COMM_MIN 22
381 #define SIZEOF_AIFC_COMM 24
382 #define SIZEOF_SSND_CHUNK 8
383 #define SIZEOF_INST_CHUNK 20
385 /* AIFC/IMA4 defines. */
386 #define AIFC_IMA4_BLOCK_LEN 34
387 #define AIFC_IMA4_SAMPLES_PER_BLOCK 64
389 /*------------------------------------------------------------------------------
390 * Typedefs for file chunks.
393 enum
394 { aiffHAVE_FORM = 0x01,
395 HAVE_AIFF = 0x02,
396 HAVE_COMM = 0x04,
397 HAVE_SSND = 0x08
400 typedef struct
401 { unsigned int size ;
402 short numChannels ;
403 unsigned int numSampleFrames ;
404 short sampleSize ;
405 unsigned char sampleRate [10] ;
406 unsigned int encoding ;
407 char zero_bytes [2] ;
408 } COMM_CHUNK ;
410 typedef struct
411 { unsigned int offset ;
412 unsigned int blocksize ;
413 } SSND_CHUNK ;
415 typedef struct
416 { short playMode ;
417 unsigned short beginLoop ;
418 unsigned short endLoop ;
419 } INST_LOOP ;
421 typedef struct
422 { char baseNote ; /* all notes are MIDI note numbers */
423 char detune ; /* cents off, only -50 to +50 are significant */
424 char lowNote ;
425 char highNote ;
426 char lowVelocity ; /* 1 to 127 */
427 char highVelocity ; /* 1 to 127 */
428 short gain ; /* in dB, 0 is normal */
429 INST_LOOP sustain_loop ;
430 INST_LOOP release_loop ;
431 } INST_CHUNK ;
433 /*------------------------------------------------------------------------------
434 * Private static functions.
437 static int aiff_close (SF_PRIVATE *psf) ;
439 static int tenbytefloat2int (unsigned char *bytes) ;
440 static void uint2tenbytefloat (unsigned int num, unsigned char *bytes) ;
442 static int aiff_read_comm_chunk (SF_PRIVATE *psf, COMM_CHUNK *comm_fmt) ;
444 static int aiff_read_header (SF_PRIVATE *psf, COMM_CHUNK *comm_fmt) ;
446 static int aiff_write_header (SF_PRIVATE *psf, int calc_length) ;
447 static int aiff_write_tailer (SF_PRIVATE *psf) ;
448 static void aiff_write_strings (SF_PRIVATE *psf, int location) ;
450 static int aiff_command (SF_PRIVATE *psf, int command, void *data, int datasize) ;
452 static const char *get_loop_mode_str (short mode) ;
455 /*------------------------------------------------------------------------------
456 ** Public function.
460 aiff_open (SF_PRIVATE *psf)
461 { COMM_CHUNK comm_fmt ;
462 int error, subformat ;
464 subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
466 if (psf->mode == SFM_READ || (psf->mode == SFM_RDWR && psf->filelength > 0))
467 { if ((error = aiff_read_header (psf, &comm_fmt)))
468 return error ;
470 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
473 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
474 { if (psf->is_pipe)
475 return SFE_NO_PIPE_WRITE ;
477 if ((psf->sf.format & SF_FORMAT_TYPEMASK) != SF_FORMAT_AIFF)
478 return SFE_BAD_OPEN_FORMAT ;
480 if (psf->mode == SFM_WRITE && (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE))
481 { psf->pchunk = calloc (1, sizeof (PEAK_CHUNK) * psf->sf.channels * sizeof (PEAK_POS)) ;
482 if (psf->pchunk == NULL)
483 return SFE_MALLOC_FAILED ;
484 psf->has_peak = SF_TRUE ;
485 psf->peak_loc = SF_PEAK_START ;
488 if (psf->mode != SFM_RDWR || psf->filelength < 40)
489 { psf->filelength = 0 ;
490 psf->datalength = 0 ;
491 psf->dataoffset = 0 ;
492 psf->sf.frames = 0 ;
495 psf->str_flags = SF_STR_ALLOW_START | SF_STR_ALLOW_END ;
497 if ((error = aiff_write_header (psf, SF_FALSE)))
498 return error ;
500 psf->write_header = aiff_write_header ;
503 psf->close = aiff_close ;
504 psf->command = aiff_command ;
506 psf->blockwidth = psf->sf.channels * psf->bytewidth ;
508 switch (psf->sf.format & SF_FORMAT_SUBMASK)
509 { case SF_FORMAT_PCM_U8 :
510 error = pcm_init (psf) ;
511 break ;
513 case SF_FORMAT_PCM_S8 :
514 error = pcm_init (psf) ;
515 break ;
517 case SF_FORMAT_PCM_16 :
518 case SF_FORMAT_PCM_24 :
519 case SF_FORMAT_PCM_32 :
520 error = pcm_init (psf) ;
521 break ;
523 case SF_FORMAT_ULAW :
524 error = ulaw_init (psf) ;
525 break ;
527 case SF_FORMAT_ALAW :
528 error = alaw_init (psf) ;
529 break ;
531 /* Lite remove start */
532 case SF_FORMAT_FLOAT :
533 error = float32_init (psf) ;
534 break ;
536 case SF_FORMAT_DOUBLE :
537 error = double64_init (psf) ;
538 break ;
540 case SF_FORMAT_DWVW_12 :
541 error = dwvw_init (psf, 12) ;
542 break ;
544 case SF_FORMAT_DWVW_16 :
545 error = dwvw_init (psf, 16) ;
546 break ;
548 case SF_FORMAT_DWVW_24 :
549 error = dwvw_init (psf, 24) ;
550 break ;
552 case SF_FORMAT_DWVW_N :
553 if (psf->mode != SFM_READ)
554 { error = SFE_DWVW_BAD_BITWIDTH ;
555 break ;
557 if (comm_fmt.sampleSize >= 8 && comm_fmt.sampleSize < 24)
558 { error = dwvw_init (psf, comm_fmt.sampleSize) ;
559 psf->sf.frames = comm_fmt.numSampleFrames ;
560 break ;
562 psf_log_printf (psf, "AIFC/DWVW : Bad bitwidth %d\n", comm_fmt.sampleSize) ;
563 error = SFE_DWVW_BAD_BITWIDTH ;
564 break ;
566 case SF_FORMAT_IMA_ADPCM :
568 ** IMA ADPCM encoded AIFF files always have a block length
569 ** of 34 which decodes to 64 samples.
571 error = aiff_ima_init (psf, AIFC_IMA4_BLOCK_LEN, AIFC_IMA4_SAMPLES_PER_BLOCK) ;
572 break ;
573 /* Lite remove end */
575 case SF_FORMAT_GSM610 :
576 error = gsm610_init (psf) ;
577 break ;
579 default : return SFE_UNIMPLEMENTED ;
582 if (psf->mode == SFM_READ)
583 psf->blockwidth = psf->sf.channels * psf->bytewidth ;
585 return error ;
586 } /* aiff_open */
588 /*==========================================================================================
589 ** Private functions.
592 static int
593 aiff_read_header (SF_PRIVATE *psf, COMM_CHUNK *comm_fmt)
594 { SSND_CHUNK ssnd_fmt ;
595 int marker, dword, bytesread, k ;
596 int FORMsize, SSNDsize ;
597 int filetype, found_chunk = 0, done = 0, error = 0 ;
598 char *cptr, byte ;
600 /* Set position to start of file to begin reading header. */
601 psf_binheader_readf (psf, "p", 0) ;
603 memset (comm_fmt, 0, sizeof (COMM_CHUNK)) ;
605 /* Until recently AIF* file were all BIG endian. */
606 psf->endian = SF_ENDIAN_BIG ;
608 /* AIFF files can apparently have their chunks in any order. However, they
609 ** must have a FORM chunk. Approach here is to read all the chunks one by
610 ** one and then check for the mandatory chunks at the end.
612 while (! done)
613 { psf_binheader_readf (psf, "m", &marker) ;
615 if (psf->mode == SFM_RDWR && (found_chunk & HAVE_SSND))
616 return SFE_AIFF_RW_SSND_NOT_LAST ;
618 switch (marker)
619 { case FORM_MARKER :
620 if (found_chunk)
621 return SFE_AIFF_NO_FORM ;
623 psf_binheader_readf (psf, "E4", &FORMsize) ;
625 if (psf->fileoffset > 0 && psf->filelength > FORMsize + 8)
626 { /* Set file length. */
627 psf->filelength = FORMsize + 8 ;
628 psf_log_printf (psf, "FORM : %u\n", FORMsize) ;
630 else if (FORMsize != psf->filelength - 2 * SIGNED_SIZEOF (dword))
631 { dword = psf->filelength - 2 * sizeof (dword) ;
632 psf_log_printf (psf, "FORM : %u (should be %u)\n", FORMsize, dword) ;
633 FORMsize = dword ;
635 else
636 psf_log_printf (psf, "FORM : %u\n", FORMsize) ;
637 found_chunk |= aiffHAVE_FORM ;
638 break ;
640 case AIFC_MARKER :
641 case AIFF_MARKER :
642 if (! (found_chunk & aiffHAVE_FORM))
643 return SFE_AIFF_AIFF_NO_FORM ;
644 filetype = marker ;
645 psf_log_printf (psf, " %M\n", marker) ;
646 found_chunk |= HAVE_AIFF ;
647 break ;
649 case COMM_MARKER :
650 error = aiff_read_comm_chunk (psf, comm_fmt) ;
652 psf->sf.samplerate = tenbytefloat2int (comm_fmt->sampleRate) ;
653 psf->sf.frames = comm_fmt->numSampleFrames ;
654 psf->sf.channels = comm_fmt->numChannels ;
655 psf->bytewidth = BITWIDTH2BYTES (comm_fmt->sampleSize) ;
657 if (error)
658 return error ;
660 found_chunk |= HAVE_COMM ;
661 break ;
663 case PEAK_MARKER :
664 /* Must have COMM chunk before PEAK chunk. */
665 if ((found_chunk & (aiffHAVE_FORM | HAVE_AIFF | HAVE_COMM)) != (aiffHAVE_FORM | HAVE_AIFF | HAVE_COMM))
666 return SFE_AIFF_PEAK_B4_COMM ;
668 psf_binheader_readf (psf, "E4", &dword) ;
670 psf_log_printf (psf, "%M : %d\n", marker, dword) ;
671 if (dword != SIGNED_SIZEOF (PEAK_CHUNK) + psf->sf.channels * SIGNED_SIZEOF (PEAK_POS))
672 { psf_binheader_readf (psf, "j", dword) ;
673 psf_log_printf (psf, "*** File PEAK chunk bigger than sizeof (PEAK_CHUNK).\n") ;
674 return SFE_WAV_BAD_PEAK ;
677 psf->pchunk = calloc (1, sizeof (PEAK_CHUNK) * psf->sf.channels * sizeof (PEAK_POS)) ;
678 if (psf->pchunk == NULL)
679 return SFE_MALLOC_FAILED ;
681 /* read in rest of PEAK chunk. */
682 psf_binheader_readf (psf, "E44", &(psf->pchunk->version), &(psf->pchunk->timestamp)) ;
684 if (psf->pchunk->version != 1)
685 psf_log_printf (psf, " version : %d *** (should be version 1)\n", psf->pchunk->version) ;
686 else
687 psf_log_printf (psf, " version : %d\n", psf->pchunk->version) ;
689 psf_log_printf (psf, " time stamp : %d\n", psf->pchunk->timestamp) ;
690 psf_log_printf (psf, " Ch Position Value\n") ;
692 cptr = (char *) psf->buffer ;
693 for (dword = 0 ; dword < psf->sf.channels ; dword++)
694 { psf_binheader_readf (psf, "Ef4", &(psf->pchunk->peaks [dword].value),
695 &(psf->pchunk->peaks [dword].position)) ;
697 LSF_SNPRINTF (cptr, sizeof (psf->buffer), " %2d %-12d %g\n",
698 dword, psf->pchunk->peaks [dword].position, psf->pchunk->peaks [dword].value) ;
699 cptr [sizeof (psf->buffer) - 1] = 0 ;
700 psf_log_printf (psf, cptr) ;
703 psf->has_peak = SF_TRUE ; /* Found PEAK chunk. */
704 break ;
706 case SSND_MARKER :
707 psf_binheader_readf (psf, "E444", &SSNDsize, &(ssnd_fmt.offset), &(ssnd_fmt.blocksize)) ;
709 psf->datalength = SSNDsize - sizeof (ssnd_fmt) ;
710 psf->dataoffset = psf_ftell (psf) ;
712 if (psf->datalength > psf->filelength - psf->dataoffset || psf->datalength < 0)
713 { psf_log_printf (psf, " SSND : %u (should be %D)\n", SSNDsize, psf->filelength - psf->dataoffset + sizeof (SSND_CHUNK)) ;
714 psf->datalength = psf->filelength - psf->dataoffset ;
716 else
717 psf_log_printf (psf, " SSND : %u\n", SSNDsize) ;
719 /* Only set dataend if there really is data at the end. */
720 if (psf->datalength + psf->dataoffset < psf->filelength)
721 psf->dataend = psf->datalength + psf->dataoffset ;
723 psf_log_printf (psf, " Offset : %u\n", ssnd_fmt.offset) ;
724 psf_log_printf (psf, " Block Size : %u\n", ssnd_fmt.blocksize) ;
726 found_chunk |= HAVE_SSND ;
728 if (! psf->sf.seekable)
729 break ;
731 /* Seek to end of SSND chunk. */
732 psf_fseek (psf, psf->dataoffset + psf->datalength + (SSNDsize & 1), SEEK_SET) ;
733 break ;
735 case c_MARKER :
736 psf_binheader_readf (psf, "E4", &dword) ;
737 dword += (dword & 1) ;
738 if (dword == 0)
739 break ;
740 if (dword > SIGNED_SIZEOF (psf->buffer))
741 { psf_log_printf (psf, " %M : %d (too big)\n", marker, dword) ;
742 return SFE_INTERNAL ;
745 cptr = (char*) psf->buffer ;
746 psf_binheader_readf (psf, "b", cptr, dword) ;
747 cptr [dword - 1] = 0 ;
748 psf_log_printf (psf, " %M : %s\n", marker, cptr) ;
749 psf_store_string (psf, SF_STR_COPYRIGHT, cptr) ;
750 break ;
752 case AUTH_MARKER :
753 psf_binheader_readf (psf, "E4", &dword) ;
754 dword += (dword & 1) ;
755 if (dword == 0)
756 break ;
757 if (dword > SIGNED_SIZEOF (psf->buffer))
758 { psf_log_printf (psf, " %M : %d (too big)\n", marker, dword) ;
759 return SFE_INTERNAL ;
762 cptr = (char*) psf->buffer ;
763 psf_binheader_readf (psf, "b", cptr, dword) ;
764 cptr [dword - 1] = 0 ;
765 psf_log_printf (psf, " %M : %s\n", marker, cptr) ;
766 psf_store_string (psf, SF_STR_ARTIST, cptr) ;
767 break ;
769 case COMT_MARKER :
770 psf_binheader_readf (psf, "E4", &dword) ;
771 dword += (dword & 1) ;
772 if (dword == 0)
773 break ;
774 if (dword > SIGNED_SIZEOF (psf->buffer))
775 { psf_log_printf (psf, " %M : %d (too big)\n", marker, dword) ;
776 return SFE_INTERNAL ;
779 cptr = (char*) psf->buffer ;
780 psf_binheader_readf (psf, "b", cptr, dword) ;
781 cptr [dword - 1] = 0 ;
782 psf_log_printf (psf, " %M : %s\n", marker, cptr) ;
783 psf_store_string (psf, SF_STR_COMMENT, cptr) ;
784 break ;
786 case APPL_MARKER :
787 psf_binheader_readf (psf, "E4", &dword) ;
788 dword += (dword & 1) ;
789 if (dword == 0)
790 break ;
791 if (dword >= SIGNED_SIZEOF (psf->buffer))
792 { psf_log_printf (psf, " %M : %d (too big, skipping)\n", marker, dword) ;
793 psf_binheader_readf (psf, "j", dword) ;
794 break ;
797 cptr = (char*) psf->buffer ;
798 psf_binheader_readf (psf, "b", cptr, dword) ;
799 cptr [dword - 1] = 0 ;
801 for (k = 0 ; k < dword ; k++)
802 if (! isprint (cptr [k]))
803 { cptr [k] = 0 ;
804 break ;
807 psf_log_printf (psf, " %M : %s\n", marker, cptr) ;
808 psf_store_string (psf, SF_STR_SOFTWARE, cptr) ;
809 break ;
811 case NAME_MARKER :
812 psf_binheader_readf (psf, "E4", &dword) ;
813 dword += (dword & 1) ;
814 if (dword == 0)
815 break ;
816 if (dword > SIGNED_SIZEOF (psf->buffer))
817 { psf_log_printf (psf, " %M : %d (too big)\n", marker, dword) ;
818 return SFE_INTERNAL ;
821 cptr = (char*) psf->buffer ;
822 psf_binheader_readf (psf, "b", cptr, dword) ;
823 cptr [dword - 1] = 0 ;
824 psf_log_printf (psf, " %M : %s\n", marker, cptr) ;
825 psf_store_string (psf, SF_STR_TITLE, cptr) ;
826 break ;
828 case ANNO_MARKER :
829 psf_binheader_readf (psf, "E4", &dword) ;
830 dword += (dword & 1) ;
831 if (dword == 0)
832 break ;
833 if (dword > SIGNED_SIZEOF (psf->buffer))
834 { psf_log_printf (psf, " %M : %d (too big)\n", marker, dword) ;
835 return SFE_INTERNAL ;
838 cptr = (char*) psf->buffer ;
839 psf_binheader_readf (psf, "b", cptr, dword) ;
840 cptr [dword - 1] = 0 ;
841 psf_log_printf (psf, " %M : %s\n", marker, cptr) ;
842 break ;
844 case INST_MARKER :
845 psf_binheader_readf (psf, "E4", &dword) ;
846 if (dword != SIZEOF_INST_CHUNK)
847 { psf_log_printf (psf, " %M : %d (should be %d)\n", marker, dword, SIZEOF_INST_CHUNK) ;
848 psf_binheader_readf (psf, "j", dword) ;
849 break ;
851 psf_log_printf (psf, " %M : %d\n", marker, dword) ;
852 { unsigned char bytes [6] ;
853 short gain ;
855 psf_binheader_readf (psf, "b", bytes, 6) ;
856 psf_log_printf (psf, " Base Note : %u\n Detune : %u\n"
857 " Low Note : %u\n High Note : %u\n"
858 " Low Vel. : %u\n High Vel. : %u\n",
859 bytes [0], bytes [1], bytes [2], bytes [3], bytes [4], bytes [5]) ;
861 psf_binheader_readf (psf, "E2", &gain) ;
862 psf_log_printf (psf, " Gain (dB) : %d\n", gain) ;
865 { short mode ; /* 0 - no loop, 1 - forward looping, 2 - backward looping */
866 const char *loop_mode ;
867 unsigned short begin, end ;
869 psf_binheader_readf (psf, "E222", &mode, &begin, &end) ;
870 loop_mode = get_loop_mode_str (mode) ;
871 psf_log_printf (psf, " Sustain\n mode : %d => %s\n begin : %u\n end : %u\n",
872 mode, loop_mode, begin, end) ;
873 psf_binheader_readf (psf, "E222", &mode, &begin, &end) ;
874 loop_mode = get_loop_mode_str (mode) ;
875 psf_log_printf (psf, " Release\n mode : %d => %s\n begin : %u\n end : %u\n",
876 mode, loop_mode, begin, end) ;
878 break ;
880 case MARK_MARKER :
881 psf_binheader_readf (psf, "E4", &dword) ;
882 psf_log_printf (psf, " %M : %d\n", marker, dword) ;
883 { unsigned short mark_count, mark_id ;
884 unsigned char pstr_len ;
885 unsigned int position ;
887 bytesread = psf_binheader_readf (psf, "E2", &mark_count) ;
888 psf_log_printf (psf, " Count : %d\n", mark_count) ;
890 while (mark_count && bytesread < dword)
891 { bytesread += psf_binheader_readf (psf, "E241", &mark_id, &position, &pstr_len) ;
892 psf_log_printf (psf, " Mark ID : %u\n Position : %u\n", mark_id, position) ;
894 pstr_len += (pstr_len & 1) + 1 ; /* fudgy, fudgy, hack, hack */
896 bytesread += psf_binheader_readf (psf, "b", psf->buffer, pstr_len) ;
897 psf_log_printf (psf, " Name : %s\n", psf->buffer) ;
899 mark_count -- ;
902 psf_binheader_readf (psf, "j", dword - bytesread) ;
903 break ;
905 case FVER_MARKER :
906 case SFX_MARKER :
907 psf_binheader_readf (psf, "E4", &dword) ;
908 psf_log_printf (psf, " %M : %d\n", marker, dword) ;
910 psf_binheader_readf (psf, "j", dword) ;
911 break ;
913 case NONE_MARKER :
914 /* Fix for broken AIFC files with incorrect COMM chunk length. */
915 psf_binheader_readf (psf, "1", &byte) ;
916 dword = byte ;
917 psf_binheader_readf (psf, "j", dword) ;
918 break ;
920 default :
921 if (isprint ((marker >> 24) & 0xFF) && isprint ((marker >> 16) & 0xFF)
922 && isprint ((marker >> 8) & 0xFF) && isprint (marker & 0xFF))
923 { psf_binheader_readf (psf, "E4", &dword) ;
924 psf_log_printf (psf, "%M : %d (unknown marker)\n", marker, dword) ;
926 psf_binheader_readf (psf, "j", dword) ;
927 break ;
929 if ((dword = psf_ftell (psf)) & 0x03)
930 { psf_log_printf (psf, " Unknown chunk marker %X at position %d. Resyncing.\n", marker, dword - 4) ;
932 psf_binheader_readf (psf, "j", -3) ;
933 break ;
935 psf_log_printf (psf, "*** Unknown chunk marker %X at position %D. Exiting parser.\n", marker, psf_ftell (psf)) ;
936 done = 1 ;
937 break ;
938 } ; /* switch (marker) */
940 if ((! psf->sf.seekable) && (found_chunk & HAVE_SSND))
941 break ;
943 if (psf_ftell (psf) >= psf->filelength - (2 * SIGNED_SIZEOF (dword)))
944 break ;
946 if (psf->logindex >= SIGNED_SIZEOF (psf->logbuffer) - 2)
947 return SFE_LOG_OVERRUN ;
948 } ; /* while (1) */
950 if (! (found_chunk & aiffHAVE_FORM))
951 return SFE_AIFF_NO_FORM ;
953 if (! (found_chunk & HAVE_AIFF))
954 return SFE_AIFF_COMM_NO_FORM ;
956 if (! (found_chunk & HAVE_COMM))
957 return SFE_AIFF_SSND_NO_COMM ;
959 if (! psf->dataoffset)
960 return SFE_AIFF_NO_DATA ;
962 return 0 ;
963 } /* aiff_read_header */
965 static int
966 aiff_close (SF_PRIVATE *psf)
968 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
969 { aiff_write_tailer (psf) ;
971 aiff_write_header (psf, SF_TRUE) ;
974 return 0 ;
975 } /* aiff_close */
977 static int
978 aiff_read_comm_chunk (SF_PRIVATE *psf, COMM_CHUNK *comm_fmt)
979 { int error = 0, bytesread, subformat ;
981 bytesread = psf_binheader_readf (psf, "E4", &(comm_fmt->size)) ;
983 /* The COMM chunk has an int aligned to an odd word boundary. Some
984 ** procesors are not able to deal with this (ie bus fault) so we have
985 ** to take special care.
987 comm_fmt->size += comm_fmt->size & 1 ;
989 bytesread +=
990 psf_binheader_readf (psf, "E242b", &(comm_fmt->numChannels), &(comm_fmt->numSampleFrames),
991 &(comm_fmt->sampleSize), &(comm_fmt->sampleRate), SIGNED_SIZEOF (comm_fmt->sampleRate)) ;
993 if (comm_fmt->size == SIZEOF_AIFF_COMM)
994 comm_fmt->encoding = NONE_MARKER ;
995 else if (comm_fmt->size == SIZEOF_AIFC_COMM_MIN)
996 bytesread += psf_binheader_readf (psf, "Em", &(comm_fmt->encoding)) ;
997 else if (comm_fmt->size >= SIZEOF_AIFC_COMM)
998 { unsigned char encoding_len ;
1000 bytesread += psf_binheader_readf (psf, "Em1", &(comm_fmt->encoding), &encoding_len) ;
1002 memset (psf->buffer, 0, comm_fmt->size) ;
1004 bytesread += psf_binheader_readf (psf, "b", psf->buffer,
1005 comm_fmt->size - SIZEOF_AIFC_COMM + 1) ;
1006 psf->buffer [encoding_len] = 0 ;
1009 psf_log_printf (psf, " COMM : %d\n", comm_fmt->size) ;
1010 psf_log_printf (psf, " Sample Rate : %d\n", tenbytefloat2int (comm_fmt->sampleRate)) ;
1011 psf_log_printf (psf, " Frames : %u%s\n", comm_fmt->numSampleFrames, (comm_fmt->numSampleFrames == 0 && psf->filelength > 100) ? " (Should not be 0)" : "") ;
1012 psf_log_printf (psf, " Channels : %d\n", comm_fmt->numChannels) ;
1014 /* Found some broken 'fl32' files with comm.samplesize == 16. Fix it here. */
1016 if ((comm_fmt->encoding == fl32_MARKER || comm_fmt->encoding == FL32_MARKER) && comm_fmt->sampleSize != 32)
1017 { psf_log_printf (psf, " Sample Size : %d (should be 32)\n", comm_fmt->sampleSize) ;
1018 comm_fmt->sampleSize = 32 ;
1020 else if ((comm_fmt->encoding == fl64_MARKER || comm_fmt->encoding == FL64_MARKER) && comm_fmt->sampleSize != 64)
1021 { psf_log_printf (psf, " Sample Size : %d (should be 64)\n", comm_fmt->sampleSize) ;
1022 comm_fmt->sampleSize = 64 ;
1024 else
1025 psf_log_printf (psf, " Sample Size : %d\n", comm_fmt->sampleSize) ;
1027 subformat = s_bitwidth_to_subformat (comm_fmt->sampleSize) ;
1029 psf->endian = SF_ENDIAN_BIG ;
1031 switch (comm_fmt->encoding)
1032 { case NONE_MARKER :
1033 psf->sf.format = (SF_FORMAT_AIFF | subformat) ;
1034 break ;
1036 case twos_MARKER :
1037 case in32_MARKER :
1038 psf->sf.format = (SF_ENDIAN_BIG | SF_FORMAT_AIFF | subformat) ;
1039 break ;
1041 case sowt_MARKER :
1042 case ni32_MARKER :
1043 psf->endian = SF_ENDIAN_LITTLE ;
1044 psf->sf.format = (SF_ENDIAN_LITTLE | SF_FORMAT_AIFF | subformat) ;
1045 break ;
1047 case fl32_MARKER :
1048 case FL32_MARKER :
1049 psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_FLOAT) ;
1050 break ;
1052 case ulaw_MARKER :
1053 case ULAW_MARKER :
1054 psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_ULAW) ;
1055 break ;
1057 case alaw_MARKER :
1058 case ALAW_MARKER :
1059 psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_ALAW) ;
1060 break ;
1062 case fl64_MARKER :
1063 case FL64_MARKER :
1064 psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_DOUBLE) ;
1065 break ;
1067 case raw_MARKER :
1068 psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_PCM_U8) ;
1069 break ;
1071 case DWVW_MARKER :
1072 psf->sf.format = SF_FORMAT_AIFF ;
1073 switch (comm_fmt->sampleSize)
1074 { case 12 :
1075 psf->sf.format |= SF_FORMAT_DWVW_12 ;
1076 break ;
1077 case 16 :
1078 psf->sf.format |= SF_FORMAT_DWVW_16 ;
1079 break ;
1080 case 24 :
1081 psf->sf.format |= SF_FORMAT_DWVW_24 ;
1082 break ;
1084 default :
1085 psf->sf.format |= SF_FORMAT_DWVW_N ;
1086 break ;
1088 break ;
1090 case GSM_MARKER :
1091 psf->sf.format = SF_FORMAT_AIFF ;
1092 psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_GSM610) ;
1093 break ;
1096 case ima4_MARKER :
1097 psf->endian = SF_ENDIAN_BIG ;
1098 psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_IMA_ADPCM) ;
1099 break ;
1101 default :
1102 psf_log_printf (psf, "AIFC : Unimplemented format : %M\n", comm_fmt->encoding) ;
1103 error = SFE_UNIMPLEMENTED ;
1106 if (! psf->buffer [0])
1107 psf_log_printf (psf, " Encoding : %M\n", comm_fmt->encoding) ;
1108 else
1109 psf_log_printf (psf, " Encoding : %M => %s\n", comm_fmt->encoding, (char*) psf->buffer) ;
1111 return error ;
1112 } /* aiff_read_comm_chunk */
1114 static int
1115 aiff_write_header (SF_PRIVATE *psf, int calc_length)
1116 { sf_count_t current ;
1117 unsigned char comm_sample_rate [10], comm_zero_bytes [2] = { 0, 0 } ;
1118 unsigned int comm_type, comm_size, comm_encoding, comm_frames ;
1119 int k, endian ;
1120 short bit_width ;
1122 current = psf_ftell (psf) ;
1124 if (calc_length)
1125 { psf->filelength = psf_get_filelen (psf) ;
1127 psf->datalength = psf->filelength - psf->dataoffset ;
1128 if (psf->dataend)
1129 psf->datalength -= psf->filelength - psf->dataend ;
1131 if (psf->bytewidth > 0)
1132 psf->sf.frames = psf->datalength / (psf->bytewidth * psf->sf.channels) ;
1135 if (psf->mode == SFM_RDWR && psf->dataoffset > 0)
1136 { /* Assuming here that the header has already been written and just
1137 ** needs to be corrected for new data length. That means that we
1138 ** only change the length fields of the FORM and SSND chunks;
1139 ** everything else can be skipped over.
1142 /* First write new FORM chunk. */
1143 psf->headindex = 0 ;
1144 psf_fseek (psf, 0, SEEK_SET) ;
1146 psf_binheader_writef (psf, "Etm8", FORM_MARKER, psf->filelength - 8) ;
1147 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
1149 /* Now write frame count field of COMM chunk header. */
1150 psf->headindex = 0 ;
1151 psf_fseek (psf, 22, SEEK_SET) ;
1153 psf_binheader_writef (psf, "Et8", psf->sf.frames) ;
1154 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
1156 /* Now write new SSND chunk header. */
1157 psf->headindex = 0 ;
1158 psf_fseek (psf, psf->dataoffset - 16, SEEK_SET) ;
1160 psf_binheader_writef (psf, "Etm8", SSND_MARKER, psf->datalength + SIZEOF_SSND_CHUNK) ;
1161 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
1163 if (current < psf->dataoffset)
1164 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
1165 else if (current > 0)
1166 psf_fseek (psf, current, SEEK_SET) ;
1168 return 0 ;
1171 endian = psf->sf.format & SF_FORMAT_ENDMASK ;
1172 if (CPU_IS_LITTLE_ENDIAN && endian == SF_ENDIAN_CPU)
1173 endian = SF_ENDIAN_LITTLE ;
1175 /* Standard value here. */
1176 bit_width = psf->bytewidth * 8 ;
1177 comm_frames = (psf->sf.frames > 0xFFFFFFFF) ? 0xFFFFFFFF : psf->sf.frames ;
1179 switch (psf->sf.format & SF_FORMAT_SUBMASK)
1180 { case SF_FORMAT_PCM_S8 :
1181 case SF_FORMAT_PCM_16 :
1182 case SF_FORMAT_PCM_24 :
1183 case SF_FORMAT_PCM_32 :
1184 switch (endian)
1185 { case SF_ENDIAN_BIG :
1186 psf->endian = SF_ENDIAN_BIG ;
1187 comm_type = AIFC_MARKER ;
1188 comm_size = SIZEOF_AIFC_COMM ;
1189 comm_encoding = twos_MARKER ;
1190 break ;
1192 case SF_ENDIAN_LITTLE :
1193 psf->endian = SF_ENDIAN_LITTLE ;
1194 comm_type = AIFC_MARKER ;
1195 comm_size = SIZEOF_AIFC_COMM ;
1196 comm_encoding = sowt_MARKER ;
1197 break ;
1199 default : /* SF_ENDIAN_FILE */
1200 psf->endian = SF_ENDIAN_BIG ;
1201 comm_type = AIFF_MARKER ;
1202 comm_size = SIZEOF_AIFF_COMM ;
1203 comm_encoding = 0 ;
1204 break ;
1206 break ;
1208 case SF_FORMAT_FLOAT : /* Big endian floating point. */
1209 psf->endian = SF_ENDIAN_BIG ;
1210 comm_type = AIFC_MARKER ;
1211 comm_size = SIZEOF_AIFC_COMM ;
1212 comm_encoding = FL32_MARKER ; /* Use 'FL32' because its easier to read. */
1213 break ;
1215 case SF_FORMAT_DOUBLE : /* Big endian double precision floating point. */
1216 psf->endian = SF_ENDIAN_BIG ;
1217 comm_type = AIFC_MARKER ;
1218 comm_size = SIZEOF_AIFC_COMM ;
1219 comm_encoding = FL64_MARKER ; /* Use 'FL64' because its easier to read. */
1220 break ;
1222 case SF_FORMAT_ULAW :
1223 psf->endian = SF_ENDIAN_BIG ;
1224 comm_type = AIFC_MARKER ;
1225 comm_size = SIZEOF_AIFC_COMM ;
1226 comm_encoding = ulaw_MARKER ;
1227 break ;
1229 case SF_FORMAT_ALAW :
1230 psf->endian = SF_ENDIAN_BIG ;
1231 comm_type = AIFC_MARKER ;
1232 comm_size = SIZEOF_AIFC_COMM ;
1233 comm_encoding = alaw_MARKER ;
1234 break ;
1236 case SF_FORMAT_PCM_U8 :
1237 psf->endian = SF_ENDIAN_BIG ;
1238 comm_type = AIFC_MARKER ;
1239 comm_size = SIZEOF_AIFC_COMM ;
1240 comm_encoding = raw_MARKER ;
1241 break ;
1243 case SF_FORMAT_DWVW_12 :
1244 psf->endian = SF_ENDIAN_BIG ;
1245 comm_type = AIFC_MARKER ;
1246 comm_size = SIZEOF_AIFC_COMM ;
1247 comm_encoding = DWVW_MARKER ;
1249 /* Override standard value here.*/
1250 bit_width = 12 ;
1251 break ;
1253 case SF_FORMAT_DWVW_16 :
1254 psf->endian = SF_ENDIAN_BIG ;
1255 comm_type = AIFC_MARKER ;
1256 comm_size = SIZEOF_AIFC_COMM ;
1257 comm_encoding = DWVW_MARKER ;
1259 /* Override standard value here.*/
1260 bit_width = 16 ;
1261 break ;
1263 case SF_FORMAT_DWVW_24 :
1264 psf->endian = SF_ENDIAN_BIG ;
1265 comm_type = AIFC_MARKER ;
1266 comm_size = SIZEOF_AIFC_COMM ;
1267 comm_encoding = DWVW_MARKER ;
1269 /* Override standard value here.*/
1270 bit_width = 24 ;
1271 break ;
1273 case SF_FORMAT_GSM610 :
1274 psf->endian = SF_ENDIAN_BIG ;
1275 comm_type = AIFC_MARKER ;
1276 comm_size = SIZEOF_AIFC_COMM ;
1277 comm_encoding = GSM_MARKER ;
1279 /* Override standard value here.*/
1280 bit_width = 16 ;
1281 break ;
1283 case SF_FORMAT_IMA_ADPCM :
1284 psf->endian = SF_ENDIAN_BIG ;
1285 comm_type = AIFC_MARKER ;
1286 comm_size = SIZEOF_AIFC_COMM ;
1287 comm_encoding = ima4_MARKER ;
1289 /* Override standard value here.*/
1290 bit_width = 16 ;
1291 comm_frames = psf->sf.frames / AIFC_IMA4_SAMPLES_PER_BLOCK ;
1292 break ;
1294 default : return SFE_BAD_OPEN_FORMAT ;
1297 /* Reset the current header length to zero. */
1298 psf->header [0] = 0 ;
1299 psf->headindex = 0 ;
1300 psf_fseek (psf, 0, SEEK_SET) ;
1302 psf_binheader_writef (psf, "Etm8", FORM_MARKER, psf->filelength - 8) ;
1304 /* Write COMM chunk. */
1305 psf_binheader_writef (psf, "Emm4", comm_type, COMM_MARKER, comm_size) ;
1307 uint2tenbytefloat (psf->sf.samplerate, comm_sample_rate) ;
1309 psf_binheader_writef (psf, "Et242", psf->sf.channels, comm_frames, bit_width) ;
1310 psf_binheader_writef (psf, "b", comm_sample_rate, sizeof (comm_sample_rate)) ;
1312 /* AIFC chunks have some extra data. */
1313 if (comm_type == AIFC_MARKER)
1314 psf_binheader_writef (psf, "mb", comm_encoding, comm_zero_bytes, sizeof (comm_zero_bytes)) ;
1316 if (psf->str_flags & SF_STR_LOCATE_START)
1317 aiff_write_strings (psf, SF_STR_LOCATE_START) ;
1319 if (psf->has_peak && psf->peak_loc == SF_PEAK_START)
1320 { psf_binheader_writef (psf, "Em4", PEAK_MARKER,
1321 sizeof (PEAK_CHUNK) + psf->sf.channels * sizeof (PEAK_POS)) ;
1322 psf_binheader_writef (psf, "E44", 1, time (NULL)) ;
1323 for (k = 0 ; k < psf->sf.channels ; k++)
1324 psf_binheader_writef (psf, "Ef4", psf->pchunk->peaks [k].value, psf->pchunk->peaks [k].position) ; /* XXXXX */
1327 /* Write SSND chunk. */
1328 psf_binheader_writef (psf, "Etm844", SSND_MARKER, psf->datalength + SIZEOF_SSND_CHUNK, 0, 0) ;
1330 /* Header construction complete so write it out. */
1331 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
1333 if (psf->error)
1334 return psf->error ;
1336 psf->dataoffset = psf->headindex ;
1338 if (current < psf->dataoffset)
1339 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
1340 else if (current > 0)
1341 psf_fseek (psf, current, SEEK_SET) ;
1343 return psf->error ;
1344 } /* aiff_write_header */
1346 static int
1347 aiff_write_tailer (SF_PRIVATE *psf)
1348 { int k ;
1350 /* Reset the current header length to zero. */
1351 psf->header [0] = 0 ;
1352 psf->headindex = 0 ;
1354 psf->dataend = psf_fseek (psf, 0, SEEK_END) ;
1356 if (psf->has_peak && psf->peak_loc == SF_PEAK_END)
1357 { psf_binheader_writef (psf, "Em4", PEAK_MARKER,
1358 sizeof (PEAK_CHUNK) + psf->sf.channels * sizeof (PEAK_POS)) ;
1359 psf_binheader_writef (psf, "E44", 1, time (NULL)) ;
1360 for (k = 0 ; k < psf->sf.channels ; k++)
1361 psf_binheader_writef (psf, "Ef4", psf->pchunk->peaks [k].value, psf->pchunk->peaks [k].position) ; /* XXXXX */
1364 if (psf->str_flags & SF_STR_LOCATE_END)
1365 aiff_write_strings (psf, SF_STR_LOCATE_END) ;
1367 /* Write the tailer. */
1368 if (psf->headindex)
1369 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
1371 return 0 ;
1372 } /* aiff_write_tailer */
1374 static void
1375 aiff_write_strings (SF_PRIVATE *psf, int location)
1376 { int k ;
1378 for (k = 0 ; k < SF_MAX_STRINGS ; k++)
1379 { if (psf->strings [k].type == 0)
1380 break ;
1382 if (psf->strings [k].flags != location)
1383 continue ;
1385 switch (psf->strings [k].type)
1386 { case SF_STR_SOFTWARE :
1387 psf_binheader_writef (psf, "Ems", APPL_MARKER, psf->strings [k].str) ;
1388 break ;
1390 case SF_STR_TITLE :
1391 psf_binheader_writef (psf, "Ems", NAME_MARKER, psf->strings [k].str) ;
1392 break ;
1394 case SF_STR_COPYRIGHT :
1395 psf_binheader_writef (psf, "Ems", c_MARKER, psf->strings [k].str) ;
1396 break ;
1398 case SF_STR_ARTIST :
1399 psf_binheader_writef (psf, "Ems", AUTH_MARKER, psf->strings [k].str) ;
1400 break ;
1402 case SF_STR_COMMENT :
1403 psf_binheader_writef (psf, "Ems", COMT_MARKER, psf->strings [k].str) ;
1404 break ;
1407 case SF_STR_DATE :
1408 psf_binheader_writef (psf, "Ems", ICRD_MARKER, psf->strings [k].str) ;
1409 break ;
1414 return ;
1415 } /* aiff_write_strings */
1417 static int
1418 aiff_command (SF_PRIVATE *psf, int command, void *data, int datasize)
1420 /* Avoid compiler warnings. */
1421 psf = psf ;
1422 data = data ;
1423 datasize = datasize ;
1425 switch (command)
1426 { default : break ;
1429 return 0 ;
1430 } /* aiff_command */
1432 static const char*
1433 get_loop_mode_str (short mode)
1434 { switch (mode)
1435 { case 0 : return "none" ;
1436 case 1 : return "forward" ;
1437 case 2 : return "backward" ;
1440 return "*** unknown" ;
1441 } /* get_loop_mode_str */
1443 /*==========================================================================================
1444 ** Rough hack at converting from 80 bit IEEE float in AIFF header to an int and
1445 ** back again. It assumes that all sample rates are between 1 and 800MHz, which
1446 ** should be OK as other sound file formats use a 32 bit integer to store sample
1447 ** rate.
1448 ** There is another (probably better) version in the source code to the SoX but it
1449 ** has a copyright which probably prevents it from being allowable as GPL/LGPL.
1452 static int
1453 tenbytefloat2int (unsigned char *bytes)
1454 { int val = 3 ;
1456 if (bytes [0] & 0x80) /* Negative number. */
1457 return 0 ;
1459 if (bytes [0] <= 0x3F) /* Less than 1. */
1460 return 1 ;
1462 if (bytes [0] > 0x40) /* Way too big. */
1463 return 0x4000000 ;
1465 if (bytes [0] == 0x40 && bytes [1] > 0x1C) /* Too big. */
1466 return 800000000 ;
1468 /* Ok, can handle it. */
1470 val = (bytes [2] << 23) | (bytes [3] << 15) | (bytes [4] << 7) | (bytes [5] >> 1) ;
1472 val >>= (29 - bytes [1]) ;
1474 return val ;
1475 } /* tenbytefloat2int */
1477 static void
1478 uint2tenbytefloat (unsigned int num, unsigned char *bytes)
1479 { unsigned int mask = 0x40000000 ;
1480 int count ;
1483 memset (bytes, 0, 10) ;
1485 if (num <= 1)
1486 { bytes [0] = 0x3F ;
1487 bytes [1] = 0xFF ;
1488 bytes [2] = 0x80 ;
1489 return ;
1492 bytes [0] = 0x40 ;
1494 if (num >= mask)
1495 { bytes [1] = 0x1D ;
1496 return ;
1499 for (count = 0 ; count <= 32 ; count ++)
1500 { if (num & mask)
1501 break ;
1502 mask >>= 1 ;
1505 num <<= count + 1 ;
1506 bytes [1] = 29 - count ;
1507 bytes [2] = (num >> 24) & 0xFF ;
1508 bytes [3] = (num >> 16) & 0xFF ;
1509 bytes [4] = (num >> 8) & 0xFF ;
1510 bytes [5] = num & 0xFF ;
1512 } /* uint2tenbytefloat */
1515 ** Do not edit or modify anything in this comment block.
1516 ** The arch-tag line is a file identity tag for the GNU Arch
1517 ** revision control system.
1519 ** arch-tag: 7dec56ca-d6f2-48cf-863b-a72e7e17a5d9
1522 ** Copyright (C) 1999-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
1524 ** This program is free software; you can redistribute it and/or modify
1525 ** it under the terms of the GNU Lesser General Public License as published by
1526 ** the Free Software Foundation; either version 2.1 of the License, or
1527 ** (at your option) any later version.
1529 ** This program is distributed in the hope that it will be useful,
1530 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
1531 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1532 ** GNU Lesser General Public License for more details.
1534 ** You should have received a copy of the GNU Lesser General Public License
1535 ** along with this program; if not, write to the Free Software
1536 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
1540 static sf_count_t alaw_read_alaw2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
1541 static sf_count_t alaw_read_alaw2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
1542 static sf_count_t alaw_read_alaw2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
1543 static sf_count_t alaw_read_alaw2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
1545 static sf_count_t alaw_write_s2alaw (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
1546 static sf_count_t alaw_write_i2alaw (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
1547 static sf_count_t alaw_write_f2alaw (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
1548 static sf_count_t alaw_write_d2alaw (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
1550 static void alaw2s_array (unsigned char *buffer, unsigned int count, short *ptr) ;
1551 static void alaw2i_array (unsigned char *buffer, unsigned int count, int *ptr) ;
1552 static void alaw2f_array (unsigned char *buffer, unsigned int count, float *ptr, float normfact) ;
1553 static void alaw2d_array (unsigned char *buffer, unsigned int count, double *ptr, double normfact) ;
1555 static void s2alaw_array (short *buffer, unsigned int count, unsigned char *ptr) ;
1556 static void i2alaw_array (int *buffer, unsigned int count, unsigned char *ptr) ;
1557 static void f2alaw_array (float *buffer, unsigned int count, unsigned char *ptr, float normfact) ;
1558 static void d2alaw_array (double *buffer, unsigned int count, unsigned char *ptr, double normfact) ;
1562 alaw_init (SF_PRIVATE *psf)
1564 if (psf->mode == SFM_READ || psf->mode == SFM_RDWR)
1565 { psf->read_short = alaw_read_alaw2s ;
1566 psf->read_int = alaw_read_alaw2i ;
1567 psf->read_float = alaw_read_alaw2f ;
1568 psf->read_double = alaw_read_alaw2d ;
1571 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
1572 { psf->write_short = alaw_write_s2alaw ;
1573 psf->write_int = alaw_write_i2alaw ;
1574 psf->write_float = alaw_write_f2alaw ;
1575 psf->write_double = alaw_write_d2alaw ;
1578 psf->bytewidth = 1 ;
1579 psf->blockwidth = psf->sf.channels ;
1582 if (psf->filelength > psf->dataoffset)
1583 psf->datalength = (psf->dataend) ? psf->dataend - psf->dataoffset :
1584 psf->filelength - psf->dataoffset ;
1585 else
1586 psf->datalength = 0 ;
1588 psf->sf.frames = psf->datalength / psf->blockwidth ;
1590 return 0 ;
1591 } /* alaw_init */
1593 static sf_count_t
1594 alaw_read_alaw2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
1595 { int bufferlen, readcount, thisread ;
1596 sf_count_t total = 0 ;
1598 bufferlen = sizeof (psf->buffer) / sizeof (char) ;
1600 while (len > 0)
1601 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
1602 thisread = psf_fread (psf->buffer, 1, readcount, psf) ;
1603 alaw2s_array ((unsigned char*) (psf->buffer), thisread, ptr + total) ;
1604 total += thisread ;
1605 if (thisread < readcount)
1606 break ;
1607 len -= thisread ;
1610 return total ;
1611 } /* alaw_read_alaw2s */
1613 static sf_count_t
1614 alaw_read_alaw2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
1615 { int bufferlen, readcount, thisread ;
1616 sf_count_t total = 0 ;
1618 bufferlen = sizeof (psf->buffer) / sizeof (char) ;
1620 while (len > 0)
1621 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
1622 thisread = psf_fread (psf->buffer, 1, readcount, psf) ;
1623 alaw2i_array ((unsigned char*) (psf->buffer), thisread, ptr + total) ;
1624 total += thisread ;
1625 if (thisread < readcount)
1626 break ;
1627 len -= thisread ;
1630 return total ;
1631 } /* alaw_read_alaw2i */
1633 static sf_count_t
1634 alaw_read_alaw2f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
1635 { int bufferlen, readcount, thisread ;
1636 sf_count_t total = 0 ;
1637 float normfact ;
1639 normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x8000) : 1.0 ;
1641 bufferlen = sizeof (psf->buffer) / sizeof (char) ;
1643 while (len > 0)
1644 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
1645 thisread = psf_fread (psf->buffer, 1, readcount, psf) ;
1646 alaw2f_array ((unsigned char*) (psf->buffer), thisread, ptr + total, normfact) ;
1647 total += thisread ;
1648 if (thisread < readcount)
1649 break ;
1650 len -= thisread ;
1653 return total ;
1654 } /* alaw_read_alaw2f */
1656 static sf_count_t
1657 alaw_read_alaw2d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
1658 { int bufferlen, readcount, thisread ;
1659 sf_count_t total = 0 ;
1660 double normfact ;
1662 normfact = (psf->norm_double) ? 1.0 / ((double) 0x8000) : 1.0 ;
1663 bufferlen = sizeof (psf->buffer) / sizeof (char) ;
1665 while (len > 0)
1666 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
1667 thisread = psf_fread (psf->buffer, 1, readcount, psf) ;
1668 alaw2d_array ((unsigned char*) (psf->buffer), thisread, ptr + total, normfact) ;
1669 total += thisread ;
1670 if (thisread < readcount)
1671 break ;
1672 len -= thisread ;
1675 return total ;
1676 } /* alaw_read_alaw2d */
1678 /*=============================================================================================
1681 static sf_count_t
1682 alaw_write_s2alaw (SF_PRIVATE *psf, short *ptr, sf_count_t len)
1683 { int bufferlen, writecount, thiswrite ;
1684 sf_count_t total = 0 ;
1686 bufferlen = sizeof (psf->buffer) / sizeof (char) ;
1688 while (len > 0)
1689 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
1690 s2alaw_array (ptr + total, writecount, (unsigned char*) (psf->buffer)) ;
1691 thiswrite = psf_fwrite (psf->buffer, 1, writecount, psf) ;
1692 total += thiswrite ;
1693 if (thiswrite < writecount)
1694 break ;
1696 len -= thiswrite ;
1699 return total ;
1700 } /* alaw_write_s2alaw */
1702 static sf_count_t
1703 alaw_write_i2alaw (SF_PRIVATE *psf, int *ptr, sf_count_t len)
1704 { int bufferlen, writecount, thiswrite ;
1705 sf_count_t total = 0 ;
1707 bufferlen = sizeof (psf->buffer) / sizeof (char) ;
1709 while (len > 0)
1710 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
1711 i2alaw_array (ptr + total, writecount, (unsigned char*) (psf->buffer)) ;
1712 thiswrite = psf_fwrite (psf->buffer, 1, writecount, psf) ;
1713 total += thiswrite ;
1714 if (thiswrite < writecount)
1715 break ;
1717 len -= thiswrite ;
1720 return total ;
1721 } /* alaw_write_i2alaw */
1723 static sf_count_t
1724 alaw_write_f2alaw (SF_PRIVATE *psf, float *ptr, sf_count_t len)
1725 { int bufferlen, writecount, thiswrite ;
1726 sf_count_t total = 0 ;
1727 float normfact ;
1729 normfact = (psf->norm_float == SF_TRUE) ? (1.0 * 0x7FFF) : 1.0 ;
1731 bufferlen = sizeof (psf->buffer) / sizeof (char) ;
1733 while (len > 0)
1734 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
1735 f2alaw_array (ptr + total, writecount, (unsigned char*) (psf->buffer), normfact) ;
1736 thiswrite = psf_fwrite (psf->buffer, 1, writecount, psf) ;
1737 total += thiswrite ;
1738 if (thiswrite < writecount)
1739 break ;
1741 len -= thiswrite ;
1744 return total ;
1745 } /* alaw_write_f2alaw */
1747 static sf_count_t
1748 alaw_write_d2alaw (SF_PRIVATE *psf, double *ptr, sf_count_t len)
1749 { int bufferlen, writecount, thiswrite ;
1750 sf_count_t total = 0 ;
1751 double normfact ;
1753 normfact = (psf->norm_double) ? (1.0 * 0x7FFF) : 1.0 ;
1755 bufferlen = sizeof (psf->buffer) / sizeof (char) ;
1757 while (len > 0)
1758 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
1759 d2alaw_array (ptr + total, writecount, (unsigned char*) (psf->buffer), normfact) ;
1760 thiswrite = psf_fwrite (psf->buffer, 1, writecount, psf) ;
1761 total += thiswrite ;
1762 if (thiswrite < writecount)
1763 break ;
1765 len -= thiswrite ;
1768 return total ;
1769 } /* alaw_write_d2alaw */
1771 /*=============================================================================================
1772 * Private static functions and data.
1775 static
1776 short alaw_decode [128] =
1777 { -5504, -5248, -6016, -5760, -4480, -4224, -4992, -4736,
1778 -7552, -7296, -8064, -7808, -6528, -6272, -7040, -6784,
1779 -2752, -2624, -3008, -2880, -2240, -2112, -2496, -2368,
1780 -3776, -3648, -4032, -3904, -3264, -3136, -3520, -3392,
1781 -22016, -20992, -24064, -23040, -17920, -16896, -19968, -18944,
1782 -30208, -29184, -32256, -31232, -26112, -25088, -28160, -27136,
1783 -11008, -10496, -12032, -11520, -8960, -8448, -9984, -9472,
1784 -15104, -14592, -16128, -15616, -13056, -12544, -14080, -13568,
1785 -344, -328, -376, -360, -280, -264, -312, -296,
1786 -472, -456, -504, -488, -408, -392, -440, -424,
1787 -88, -72, -120, -104, -24, -8, -56, -40,
1788 -216, -200, -248, -232, -152, -136, -184, -168,
1789 -1376, -1312, -1504, -1440, -1120, -1056, -1248, -1184,
1790 -1888, -1824, -2016, -1952, -1632, -1568, -1760, -1696,
1791 -688, -656, -752, -720, -560, -528, -624, -592,
1792 -944, -912, -1008, -976, -816, -784, -880, -848
1793 } ; /* alaw_decode */
1795 static
1796 unsigned char alaw_encode [2049] =
1797 { 0xD5, 0xD4, 0xD7, 0xD6, 0xD1, 0xD0, 0xD3, 0xD2, 0xDD, 0xDC, 0xDF, 0xDE,
1798 0xD9, 0xD8, 0xDB, 0xDA, 0xC5, 0xC4, 0xC7, 0xC6, 0xC1, 0xC0, 0xC3, 0xC2,
1799 0xCD, 0xCC, 0xCF, 0xCE, 0xC9, 0xC8, 0xCB, 0xCA, 0xF5, 0xF5, 0xF4, 0xF4,
1800 0xF7, 0xF7, 0xF6, 0xF6, 0xF1, 0xF1, 0xF0, 0xF0, 0xF3, 0xF3, 0xF2, 0xF2,
1801 0xFD, 0xFD, 0xFC, 0xFC, 0xFF, 0xFF, 0xFE, 0xFE, 0xF9, 0xF9, 0xF8, 0xF8,
1802 0xFB, 0xFB, 0xFA, 0xFA, 0xE5, 0xE5, 0xE5, 0xE5, 0xE4, 0xE4, 0xE4, 0xE4,
1803 0xE7, 0xE7, 0xE7, 0xE7, 0xE6, 0xE6, 0xE6, 0xE6, 0xE1, 0xE1, 0xE1, 0xE1,
1804 0xE0, 0xE0, 0xE0, 0xE0, 0xE3, 0xE3, 0xE3, 0xE3, 0xE2, 0xE2, 0xE2, 0xE2,
1805 0xED, 0xED, 0xED, 0xED, 0xEC, 0xEC, 0xEC, 0xEC, 0xEF, 0xEF, 0xEF, 0xEF,
1806 0xEE, 0xEE, 0xEE, 0xEE, 0xE9, 0xE9, 0xE9, 0xE9, 0xE8, 0xE8, 0xE8, 0xE8,
1807 0xEB, 0xEB, 0xEB, 0xEB, 0xEA, 0xEA, 0xEA, 0xEA, 0x95, 0x95, 0x95, 0x95,
1808 0x95, 0x95, 0x95, 0x95, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94,
1809 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x96, 0x96, 0x96, 0x96,
1810 0x96, 0x96, 0x96, 0x96, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91,
1811 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x93, 0x93, 0x93, 0x93,
1812 0x93, 0x93, 0x93, 0x93, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92,
1813 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9C, 0x9C, 0x9C, 0x9C,
1814 0x9C, 0x9C, 0x9C, 0x9C, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F,
1815 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x99, 0x99, 0x99, 0x99,
1816 0x99, 0x99, 0x99, 0x99, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98,
1817 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9A, 0x9A, 0x9A, 0x9A,
1818 0x9A, 0x9A, 0x9A, 0x9A, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85,
1819 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x84, 0x84, 0x84, 0x84,
1820 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84,
1821 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87,
1822 0x87, 0x87, 0x87, 0x87, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86,
1823 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x81, 0x81, 0x81, 0x81,
1824 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
1825 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
1826 0x80, 0x80, 0x80, 0x80, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83,
1827 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x82, 0x82, 0x82, 0x82,
1828 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82,
1829 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D,
1830 0x8D, 0x8D, 0x8D, 0x8D, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C,
1831 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8F, 0x8F, 0x8F, 0x8F,
1832 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F,
1833 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E,
1834 0x8E, 0x8E, 0x8E, 0x8E, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89,
1835 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x88, 0x88, 0x88, 0x88,
1836 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88,
1837 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B,
1838 0x8B, 0x8B, 0x8B, 0x8B, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A,
1839 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0xB5, 0xB5, 0xB5, 0xB5,
1840 0xB5, 0xB5, 0xB5, 0xB5, 0xB5, 0xB5, 0xB5, 0xB5, 0xB5, 0xB5, 0xB5, 0xB5,
1841 0xB5, 0xB5, 0xB5, 0xB5, 0xB5, 0xB5, 0xB5, 0xB5, 0xB5, 0xB5, 0xB5, 0xB5,
1842 0xB5, 0xB5, 0xB5, 0xB5, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4,
1843 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4,
1844 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4,
1845 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7,
1846 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7,
1847 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB6, 0xB6, 0xB6, 0xB6,
1848 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB6,
1849 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB6,
1850 0xB6, 0xB6, 0xB6, 0xB6, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1,
1851 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1,
1852 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1,
1853 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0,
1854 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0,
1855 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB3, 0xB3, 0xB3, 0xB3,
1856 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3,
1857 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3,
1858 0xB3, 0xB3, 0xB3, 0xB3, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2,
1859 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2,
1860 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2,
1861 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD,
1862 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD,
1863 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBC, 0xBC, 0xBC, 0xBC,
1864 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC,
1865 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC,
1866 0xBC, 0xBC, 0xBC, 0xBC, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF,
1867 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF,
1868 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF,
1869 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE,
1870 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE,
1871 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xB9, 0xB9, 0xB9, 0xB9,
1872 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB9,
1873 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB9,
1874 0xB9, 0xB9, 0xB9, 0xB9, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8,
1875 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8,
1876 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8,
1877 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
1878 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
1879 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBA, 0xBA, 0xBA, 0xBA,
1880 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA,
1881 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA,
1882 0xBA, 0xBA, 0xBA, 0xBA, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5,
1883 0xA5, 0xA5, 0xA5, 0xA5, 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, 0xA4, 0xA4, 0xA4, 0xA4,
1888 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 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 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7,
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, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6,
1899 0xA6, 0xA6, 0xA6, 0xA6, 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, 0xA1, 0xA1, 0xA1, 0xA1,
1904 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 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 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0,
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, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3,
1915 0xA3, 0xA3, 0xA3, 0xA3, 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, 0xA2, 0xA2, 0xA2, 0xA2,
1920 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 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 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD,
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, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC,
1931 0xAC, 0xAC, 0xAC, 0xAC, 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, 0xAF, 0xAF, 0xAF, 0xAF,
1936 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 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 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE,
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, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9,
1947 0xA9, 0xA9, 0xA9, 0xA9, 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, 0xA8, 0xA8, 0xA8, 0xA8,
1952 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 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 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB,
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, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
1963 0xAA, 0xAA, 0xAA, 0xAA, 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, 0x2A
1968 } ; /* alaw_encode */
1970 static void
1971 alaw2s_array (unsigned char *buffer, unsigned int count, short *ptr)
1972 { while (count)
1973 { count -- ;
1974 if (buffer [count] & 0x80)
1975 ptr [count] = -1 * alaw_decode [((int) buffer [count]) & 0x7F] ;
1976 else
1977 ptr [count] = alaw_decode [((int) buffer [count]) & 0x7F] ;
1979 } /* alaw2s_array */
1981 static void
1982 alaw2i_array (unsigned char *buffer, unsigned int count, int *ptr)
1983 { while (count)
1984 { count -- ;
1985 if (buffer [count] & 0x80)
1986 ptr [count] = (-1 * alaw_decode [((int) buffer [count]) & 0x7F]) << 16 ;
1987 else
1988 ptr [count] = alaw_decode [((int) buffer [count]) & 0x7F] << 16 ;
1990 } /* alaw2i_array */
1992 static void
1993 alaw2f_array (unsigned char *buffer, unsigned int count, float *ptr, float normfact)
1994 { while (count)
1995 { count -- ;
1996 if (buffer [count] & 0x80)
1997 ptr [count] = -normfact * alaw_decode [((int) buffer [count]) & 0x7F] ;
1998 else
1999 ptr [count] = normfact * alaw_decode [((int) buffer [count]) & 0x7F] ;
2001 } /* alaw2f_array */
2003 static void
2004 alaw2d_array (unsigned char *buffer, unsigned int count, double *ptr, double normfact)
2005 { while (count)
2006 { count -- ;
2007 if (buffer [count] & 0x80)
2008 ptr [count] = -normfact * alaw_decode [((int) buffer [count]) & 0x7F] ;
2009 else
2010 ptr [count] = normfact * alaw_decode [((int) buffer [count]) & 0x7F] ;
2012 } /* alaw2d_array */
2014 static void
2015 s2alaw_array (short *ptr, unsigned int count, unsigned char *buffer)
2016 { while (count)
2017 { count -- ;
2018 if (ptr [count] >= 0)
2019 buffer [count] = alaw_encode [ptr [count] / 16] ;
2020 else
2021 buffer [count] = 0x7F & alaw_encode [ptr [count] / -16] ;
2023 } /* s2alaw_array */
2025 static void
2026 i2alaw_array (int *ptr, unsigned int count, unsigned char *buffer)
2027 { while (count)
2028 { count -- ;
2029 if (ptr [count] >= 0)
2030 buffer [count] = alaw_encode [ptr [count] >> (16 + 4)] ;
2031 else
2032 buffer [count] = 0x7F & alaw_encode [- ptr [count] >> (16 + 4)] ;
2034 } /* i2alaw_array */
2036 static void
2037 f2alaw_array (float *ptr, unsigned int count, unsigned char *buffer, float normfact)
2038 { while (count)
2039 { count -- ;
2040 if (ptr [count] >= 0)
2041 buffer [count] = alaw_encode [(lrintf (normfact * ptr [count])) / 16] ;
2042 else
2043 buffer [count] = 0x7F & alaw_encode [(lrintf (normfact * ptr [count])) / -16] ;
2045 } /* f2alaw_array */
2047 static void
2048 d2alaw_array (double *ptr, unsigned int count, unsigned char *buffer, double normfact)
2049 { while (count)
2050 { count -- ;
2051 if (ptr [count] >= 0)
2052 buffer [count] = alaw_encode [(lrint (normfact * ptr [count])) / 16] ;
2053 else
2054 buffer [count] = 0x7F & alaw_encode [(lrint (normfact * ptr [count])) / -16] ;
2056 } /* d2alaw_array */
2059 ** Do not edit or modify anything in this comment block.
2060 ** The arch-tag line is a file identity tag for the GNU Arch
2061 ** revision control system.
2063 ** arch-tag: 289ccfc2-42a6-4f1f-a29f-4dcc9bfa8752
2066 ** Copyright (C) 1999-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
2068 ** This program is free software; you can redistribute it and/or modify
2069 ** it under the terms of the GNU Lesser General Public License as published by
2070 ** the Free Software Foundation; either version 2.1 of the License, or
2071 ** (at your option) any later version.
2073 ** This program is distributed in the hope that it will be useful,
2074 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
2075 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2076 ** GNU Lesser General Public License for more details.
2078 ** You should have received a copy of the GNU Lesser General Public License
2079 ** along with this program; if not, write to the Free Software
2080 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
2084 #include <stdio.h>
2085 #include <fcntl.h>
2086 #include <string.h>
2087 #include <ctype.h>
2090 /*------------------------------------------------------------------------------
2091 ** Macros to handle big/little endian issues.
2094 #define DOTSND_MARKER (MAKE_MARKER ('.', 's', 'n', 'd'))
2095 #define DNSDOT_MARKER (MAKE_MARKER ('d', 'n', 's', '.'))
2097 #define AU_DATA_OFFSET 24
2099 /*------------------------------------------------------------------------------
2100 ** Known AU file encoding types.
2103 enum
2104 { AU_ENCODING_ULAW_8 = 1, /* 8-bit u-law samples */
2105 AU_ENCODING_PCM_8 = 2, /* 8-bit linear samples */
2106 AU_ENCODING_PCM_16 = 3, /* 16-bit linear samples */
2107 AU_ENCODING_PCM_24 = 4, /* 24-bit linear samples */
2108 AU_ENCODING_PCM_32 = 5, /* 32-bit linear samples */
2110 AU_ENCODING_FLOAT = 6, /* floating-point samples */
2111 AU_ENCODING_DOUBLE = 7, /* double-precision float samples */
2112 AU_ENCODING_INDIRECT = 8, /* fragmented sampled data */
2113 AU_ENCODING_NESTED = 9, /* ? */
2114 AU_ENCODING_DSP_CORE = 10, /* DSP program */
2115 AU_ENCODING_DSP_DATA_8 = 11, /* 8-bit fixed-point samples */
2116 AU_ENCODING_DSP_DATA_16 = 12, /* 16-bit fixed-point samples */
2117 AU_ENCODING_DSP_DATA_24 = 13, /* 24-bit fixed-point samples */
2118 AU_ENCODING_DSP_DATA_32 = 14, /* 32-bit fixed-point samples */
2120 AU_ENCODING_DISPLAY = 16, /* non-audio display data */
2121 AU_ENCODING_MULAW_SQUELCH = 17, /* ? */
2122 AU_ENCODING_EMPHASIZED = 18, /* 16-bit linear with emphasis */
2123 AU_ENCODING_NEXT = 19, /* 16-bit linear with compression (NEXT) */
2124 AU_ENCODING_COMPRESSED_EMPHASIZED = 20, /* A combination of the two above */
2125 AU_ENCODING_DSP_COMMANDS = 21, /* Music Kit DSP commands */
2126 AU_ENCODING_DSP_COMMANDS_SAMPLES = 22, /* ? */
2128 AU_ENCODING_ADPCM_G721_32 = 23, /* G721 32 kbs ADPCM - 4 bits per sample. */
2129 AU_ENCODING_ADPCM_G722 = 24, /* G722 64 kbs ADPCM */
2130 AU_ENCODING_ADPCM_G723_24 = 25, /* G723 24 kbs ADPCM - 3 bits per sample. */
2131 AU_ENCODING_ADPCM_G723_40 = 26, /* G723 40 kbs ADPCM - 5 bits per sample. */
2133 AU_ENCODING_ALAW_8 = 27
2136 /*------------------------------------------------------------------------------
2137 ** Typedefs.
2140 typedef struct
2141 { int dataoffset ;
2142 int datasize ;
2143 int encoding ;
2144 int samplerate ;
2145 int channels ;
2146 } AU_FMT ;
2149 /*------------------------------------------------------------------------------
2150 ** Private static functions.
2153 static int au_close (SF_PRIVATE *psf) ;
2155 static int au_format_to_encoding (int format) ;
2157 static int au_write_header (SF_PRIVATE *psf, int calc_length) ;
2158 static int au_read_header (SF_PRIVATE *psf) ;
2160 /*------------------------------------------------------------------------------
2161 ** Public function.
2165 au_open (SF_PRIVATE *psf)
2166 { int subformat ;
2167 int error = 0 ;
2169 if (psf->mode == SFM_READ || (psf->mode == SFM_RDWR && psf->filelength > 0))
2170 { if ((error = au_read_header (psf)))
2171 return error ;
2174 if ((psf->sf.format & SF_FORMAT_TYPEMASK) != SF_FORMAT_AU)
2175 return SFE_BAD_OPEN_FORMAT ;
2177 subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
2179 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
2180 { psf->endian = psf->sf.format & SF_FORMAT_ENDMASK ;
2181 if (CPU_IS_LITTLE_ENDIAN && psf->endian == SF_ENDIAN_CPU)
2182 psf->endian = SF_ENDIAN_LITTLE ;
2183 else if (psf->endian != SF_ENDIAN_LITTLE)
2184 psf->endian = SF_ENDIAN_BIG ;
2186 if (au_write_header (psf, SF_FALSE))
2187 return psf->error ;
2189 psf->write_header = au_write_header ;
2192 psf->close = au_close ;
2194 psf->blockwidth = psf->bytewidth * psf->sf.channels ;
2196 switch (subformat)
2197 { case SF_FORMAT_ULAW : /* 8-bit Ulaw encoding. */
2198 ulaw_init (psf) ;
2199 break ;
2201 case SF_FORMAT_PCM_S8 : /* 8-bit linear PCM. */
2202 error = pcm_init (psf) ;
2203 break ;
2205 case SF_FORMAT_PCM_16 : /* 16-bit linear PCM. */
2206 case SF_FORMAT_PCM_24 : /* 24-bit linear PCM */
2207 case SF_FORMAT_PCM_32 : /* 32-bit linear PCM. */
2208 error = pcm_init (psf) ;
2209 break ;
2211 case SF_FORMAT_ALAW : /* 8-bit Alaw encoding. */
2212 alaw_init (psf) ;
2213 break ;
2215 /* Lite remove start */
2216 case SF_FORMAT_FLOAT : /* 32-bit floats. */
2217 error = float32_init (psf) ;
2218 break ;
2220 case SF_FORMAT_DOUBLE : /* 64-bit double precision floats. */
2221 error = double64_init (psf) ;
2222 break ;
2224 case SF_FORMAT_G721_32 :
2225 if (psf->mode == SFM_READ)
2226 error = au_g72x_reader_init (psf, AU_H_G721_32) ;
2227 else if (psf->mode == SFM_WRITE)
2228 error = au_g72x_writer_init (psf, AU_H_G721_32) ;
2229 psf->sf.seekable = SF_FALSE ;
2230 break ;
2232 case SF_FORMAT_G723_24 :
2233 if (psf->mode == SFM_READ)
2234 error = au_g72x_reader_init (psf, AU_H_G723_24) ;
2235 else if (psf->mode == SFM_WRITE)
2236 error = au_g72x_writer_init (psf, AU_H_G723_24) ;
2237 psf->sf.seekable = SF_FALSE ;
2238 break ;
2240 case SF_FORMAT_G723_40 :
2241 if (psf->mode == SFM_READ)
2242 error = au_g72x_reader_init (psf, AU_H_G723_40) ;
2243 else if (psf->mode == SFM_WRITE)
2244 error = au_g72x_writer_init (psf, AU_H_G723_40) ;
2245 psf->sf.seekable = SF_FALSE ;
2246 break ;
2247 /* Lite remove end */
2249 default : break ;
2252 return error ;
2253 } /* au_open */
2256 au_nh_open (SF_PRIVATE *psf)
2258 if (psf->mode == SFM_RDWR)
2259 return SFE_BAD_OPEN_FORMAT ;
2261 if (psf_fseek (psf, psf->dataoffset, SEEK_SET))
2262 return SFE_BAD_SEEK ;
2264 psf_log_printf (psf, "Header-less u-law encoded file.\n") ;
2265 psf_log_printf (psf, "Setting up for 8kHz, mono, u-law.\n") ;
2267 psf->sf.format = SF_FORMAT_AU | SF_FORMAT_ULAW ;
2269 psf->dataoffset = 0 ;
2270 psf->endian = 0 ; /* Irrelevant but it must be something. */
2271 psf->sf.samplerate = 8000 ;
2272 psf->sf.channels = 1 ;
2273 psf->bytewidth = 1 ; /* Before decoding */
2275 ulaw_init (psf) ;
2277 psf->close = au_close ;
2279 psf->blockwidth = 1 ;
2280 psf->sf.frames = psf->filelength ;
2281 psf->datalength = psf->filelength - AU_DATA_OFFSET ;
2283 return 0 ;
2284 } /* au_nh_open */
2286 /*------------------------------------------------------------------------------
2289 static int
2290 au_close (SF_PRIVATE *psf)
2292 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
2293 au_write_header (psf, SF_TRUE) ;
2295 return 0 ;
2296 } /* au_close */
2298 static int
2299 au_write_header (SF_PRIVATE *psf, int calc_length)
2300 { sf_count_t current ;
2301 int encoding, datalength ;
2303 if (psf->pipeoffset > 0)
2304 return 0 ;
2306 current = psf_ftell (psf) ;
2308 if (calc_length)
2309 { psf->filelength = psf_get_filelen (psf) ;
2311 psf->datalength = psf->filelength - psf->dataoffset ;
2312 if (psf->dataend)
2313 psf->datalength -= psf->filelength - psf->dataend ;
2315 psf->sf.frames = psf->datalength / (psf->bytewidth * psf->sf.channels) ;
2318 encoding = au_format_to_encoding (psf->sf.format & SF_FORMAT_SUBMASK) ;
2319 if (! encoding)
2320 return (psf->error = SFE_BAD_OPEN_FORMAT) ;
2322 /* Reset the current header length to zero. */
2323 psf->header [0] = 0 ;
2324 psf->headindex = 0 ;
2327 ** Only attempt to seek if we are not writng to a pipe. If we are
2328 ** writing to a pipe we shouldn't be here anyway.
2330 if (psf->is_pipe == SF_FALSE)
2331 psf_fseek (psf, 0, SEEK_SET) ;
2334 ** AU format files allow a datalength value of -1 if the datalength
2335 ** is not know at the time the header is written.
2336 ** Also use this value of -1 if the datalength > 2 gigabytes.
2338 if (psf->datalength < 0 || psf->datalength > 0x7FFFFFFF)
2339 datalength = -1 ;
2340 else
2341 datalength = (int) (psf->datalength & 0x7FFFFFFF) ;
2343 if (psf->endian == SF_ENDIAN_BIG)
2344 { psf_binheader_writef (psf, "Em4", DOTSND_MARKER, AU_DATA_OFFSET) ;
2345 psf_binheader_writef (psf, "E4444", datalength, encoding, psf->sf.samplerate, psf->sf.channels) ;
2347 else if (psf->endian == SF_ENDIAN_LITTLE)
2348 { psf_binheader_writef (psf, "em4", DNSDOT_MARKER, AU_DATA_OFFSET) ;
2349 psf_binheader_writef (psf, "e4444", datalength, encoding, psf->sf.samplerate, psf->sf.channels) ;
2351 else
2352 return (psf->error = SFE_BAD_OPEN_FORMAT) ;
2354 /* Header construction complete so write it out. */
2355 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
2357 if (psf->error)
2358 return psf->error ;
2360 psf->dataoffset = psf->headindex ;
2362 if (current > 0)
2363 psf_fseek (psf, current, SEEK_SET) ;
2365 return psf->error ;
2366 } /* au_write_header */
2368 static int
2369 au_format_to_encoding (int format)
2371 switch (format)
2372 { case SF_FORMAT_PCM_S8 : return AU_ENCODING_PCM_8 ;
2373 case SF_FORMAT_PCM_16 : return AU_ENCODING_PCM_16 ;
2374 case SF_FORMAT_PCM_24 : return AU_ENCODING_PCM_24 ;
2375 case SF_FORMAT_PCM_32 : return AU_ENCODING_PCM_32 ;
2377 case SF_FORMAT_FLOAT : return AU_ENCODING_FLOAT ;
2378 case SF_FORMAT_DOUBLE : return AU_ENCODING_DOUBLE ;
2380 case SF_FORMAT_ULAW : return AU_ENCODING_ULAW_8 ;
2381 case SF_FORMAT_ALAW : return AU_ENCODING_ALAW_8 ;
2383 case SF_FORMAT_G721_32 : return AU_ENCODING_ADPCM_G721_32 ;
2384 case SF_FORMAT_G723_24 : return AU_ENCODING_ADPCM_G723_24 ;
2385 case SF_FORMAT_G723_40 : return AU_ENCODING_ADPCM_G723_40 ;
2387 default : break ;
2389 return 0 ;
2390 } /* au_format_to_encoding */
2392 static int
2393 au_read_header (SF_PRIVATE *psf)
2394 { AU_FMT au_fmt ;
2395 int marker, dword ;
2397 psf_binheader_readf (psf, "pm", 0, &marker) ;
2398 psf_log_printf (psf, "%M\n", marker) ;
2400 if (marker == DOTSND_MARKER)
2401 { psf->endian = SF_ENDIAN_BIG ;
2403 psf_binheader_readf (psf, "E44444", &(au_fmt.dataoffset), &(au_fmt.datasize),
2404 &(au_fmt.encoding), &(au_fmt.samplerate), &(au_fmt.channels)) ;
2406 else if (marker == DNSDOT_MARKER)
2407 { psf->endian = SF_ENDIAN_LITTLE ;
2408 psf_binheader_readf (psf, "e44444", &(au_fmt.dataoffset), &(au_fmt.datasize),
2409 &(au_fmt.encoding), &(au_fmt.samplerate), &(au_fmt.channels)) ;
2411 else
2412 return SFE_AU_NO_DOTSND ;
2414 psf_log_printf (psf, " Data Offset : %d\n", au_fmt.dataoffset) ;
2416 if (psf->fileoffset > 0 && au_fmt.datasize == -1)
2417 { psf_log_printf (psf, " Data Size : -1\n") ;
2418 return SFE_AU_EMBED_BAD_LEN ;
2421 if (psf->fileoffset > 0)
2422 { psf->filelength = au_fmt.dataoffset + au_fmt.datasize ;
2423 psf_log_printf (psf, " Data Size : %d\n", au_fmt.datasize) ;
2425 else if (au_fmt.datasize == -1 || au_fmt.dataoffset + au_fmt.datasize == psf->filelength)
2426 psf_log_printf (psf, " Data Size : %d\n", au_fmt.datasize) ;
2427 else if (au_fmt.dataoffset + au_fmt.datasize < psf->filelength)
2428 { psf->filelength = au_fmt.dataoffset + au_fmt.datasize ;
2429 psf_log_printf (psf, " Data Size : %d\n", au_fmt.datasize) ;
2431 else
2432 { dword = psf->filelength - au_fmt.dataoffset ;
2433 psf_log_printf (psf, " Data Size : %d (should be %d)\n", au_fmt.datasize, dword) ;
2434 au_fmt.datasize = dword ;
2437 psf->dataoffset = au_fmt.dataoffset ;
2438 psf->datalength = psf->filelength - psf->dataoffset ;
2440 if (psf_ftell (psf) < psf->dataoffset)
2441 psf_binheader_readf (psf, "j", psf->dataoffset - psf_ftell (psf)) ;
2443 psf->close = au_close ;
2445 psf->sf.samplerate = au_fmt.samplerate ;
2446 psf->sf.channels = au_fmt.channels ;
2448 /* Only fill in type major. */
2449 if (psf->endian == SF_ENDIAN_BIG)
2450 psf->sf.format = SF_FORMAT_AU ;
2451 else if (psf->endian == SF_ENDIAN_LITTLE)
2452 psf->sf.format = SF_ENDIAN_LITTLE | SF_FORMAT_AU ;
2454 psf_log_printf (psf, " Encoding : %d => ", au_fmt.encoding) ;
2456 psf->sf.format = psf->sf.format & SF_FORMAT_ENDMASK ;
2458 switch (au_fmt.encoding)
2459 { case AU_ENCODING_ULAW_8 :
2460 psf->sf.format |= SF_FORMAT_AU | SF_FORMAT_ULAW ;
2461 psf->bytewidth = 1 ; /* Before decoding */
2462 psf_log_printf (psf, "8-bit ISDN u-law\n") ;
2463 break ;
2465 case AU_ENCODING_PCM_8 :
2466 psf->sf.format |= SF_FORMAT_AU | SF_FORMAT_PCM_S8 ;
2467 psf->bytewidth = 1 ;
2468 psf_log_printf (psf, "8-bit linear PCM\n") ;
2469 break ;
2471 case AU_ENCODING_PCM_16 :
2472 psf->sf.format |= SF_FORMAT_AU | SF_FORMAT_PCM_16 ;
2473 psf->bytewidth = 2 ;
2474 psf_log_printf (psf, "16-bit linear PCM\n") ;
2475 break ;
2477 case AU_ENCODING_PCM_24 :
2478 psf->sf.format |= SF_FORMAT_AU | SF_FORMAT_PCM_24 ;
2479 psf->bytewidth = 3 ;
2480 psf_log_printf (psf, "24-bit linear PCM\n") ;
2481 break ;
2483 case AU_ENCODING_PCM_32 :
2484 psf->sf.format |= SF_FORMAT_AU | SF_FORMAT_PCM_32 ;
2485 psf->bytewidth = 4 ;
2486 psf_log_printf (psf, "32-bit linear PCM\n") ;
2487 break ;
2489 case AU_ENCODING_FLOAT :
2490 psf->sf.format |= SF_FORMAT_AU | SF_FORMAT_FLOAT ;
2491 psf->bytewidth = 4 ;
2492 psf_log_printf (psf, "32-bit float\n") ;
2493 break ;
2495 case AU_ENCODING_DOUBLE :
2496 psf->sf.format |= SF_FORMAT_AU | SF_FORMAT_DOUBLE ;
2497 psf->bytewidth = 8 ;
2498 psf_log_printf (psf, "64-bit double precision float\n") ;
2499 break ;
2501 case AU_ENCODING_ALAW_8 :
2502 psf->sf.format |= SF_FORMAT_AU | SF_FORMAT_ALAW ;
2503 psf->bytewidth = 1 ; /* Before decoding */
2504 psf_log_printf (psf, "8-bit ISDN A-law\n") ;
2505 break ;
2507 case AU_ENCODING_ADPCM_G721_32 :
2508 psf->sf.format |= SF_FORMAT_AU | SF_FORMAT_G721_32 ;
2509 psf->bytewidth = 0 ;
2510 psf_log_printf (psf, "G721 32kbs ADPCM\n") ;
2511 break ;
2513 case AU_ENCODING_ADPCM_G723_24 :
2514 psf->sf.format |= SF_FORMAT_AU | SF_FORMAT_G723_24 ;
2515 psf->bytewidth = 0 ;
2516 psf_log_printf (psf, "G723 24kbs ADPCM\n") ;
2517 break ;
2519 case AU_ENCODING_ADPCM_G723_40 :
2520 psf->sf.format |= SF_FORMAT_AU | SF_FORMAT_G723_40 ;
2521 psf->bytewidth = 0 ;
2522 psf_log_printf (psf, "G723 40kbs ADPCM\n") ;
2523 break ;
2525 case AU_ENCODING_ADPCM_G722 :
2526 psf_log_printf (psf, "G722 64 kbs ADPCM (unsupported)\n") ;
2527 break ;
2529 case AU_ENCODING_NEXT :
2530 psf_log_printf (psf, "Weird NeXT encoding format (unsupported)\n") ;
2531 break ;
2533 default :
2534 psf_log_printf (psf, "Unknown!!\n") ;
2535 break ;
2538 psf_log_printf (psf, " Sample Rate : %d\n", au_fmt.samplerate) ;
2539 psf_log_printf (psf, " Channels : %d\n", au_fmt.channels) ;
2541 psf->blockwidth = psf->sf.channels * psf->bytewidth ;
2543 if (! psf->sf.frames && psf->blockwidth)
2544 psf->sf.frames = (psf->filelength - psf->dataoffset) / psf->blockwidth ;
2546 return 0 ;
2547 } /* au_read_header */
2549 ** Do not edit or modify anything in this comment block.
2550 ** The arch-tag line is a file identity tag for the GNU Arch
2551 ** revision control system.
2553 ** arch-tag: 31f691b1-cde9-4ed2-9469-6bca60fb9cd0
2556 ** Copyright (C) 1999-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
2558 ** This program is free software; you can redistribute it and/or modify
2559 ** it under the terms of the GNU Lesser General Public License as published by
2560 ** the Free Software Foundation; either version 2.1 of the License, or
2561 ** (at your option) any later version.
2563 ** This program is distributed in the hope that it will be useful,
2564 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
2565 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2566 ** GNU Lesser General Public License for more details.
2568 ** You should have received a copy of the GNU Lesser General Public License
2569 ** along with this program; if not, write to the Free Software
2570 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
2574 #include <stdio.h>
2575 #include <stdlib.h>
2576 #include <string.h>
2579 static int au_g72x_read_block (SF_PRIVATE *psf, G72x_DATA *pg72x, short *ptr, int len) ;
2580 static int au_g72x_write_block (SF_PRIVATE *psf, G72x_DATA *pg72x, short *ptr, int len) ;
2582 static int au_g72x_decode_block (SF_PRIVATE *psf, G72x_DATA *pg72x) ;
2583 static int au_g72x_encode_block (SF_PRIVATE *psf, G72x_DATA *pg72x) ;
2585 static sf_count_t au_g72x_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
2586 static sf_count_t au_g72x_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
2587 static sf_count_t au_g72x_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
2588 static sf_count_t au_g72x_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
2590 static sf_count_t au_g72x_write_s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
2591 static sf_count_t au_g72x_write_i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
2592 static sf_count_t au_g72x_write_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
2593 static sf_count_t au_g72x_write_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
2595 static sf_count_t au_g72x_seek (SF_PRIVATE *psf, int mode, sf_count_t offset) ;
2597 static int au_g72x_close (SF_PRIVATE *psf) ;
2600 /*============================================================================================
2601 ** WAV G721 Reader initialisation function.
2605 au_g72x_reader_init (SF_PRIVATE *psf, int codec)
2606 { G72x_DATA *pg72x ;
2607 int bitspersample ;
2609 psf->sf.seekable = SF_FALSE ;
2611 if (psf->sf.channels != 1)
2612 return SFE_G72X_NOT_MONO ;
2614 if (! (pg72x = malloc (sizeof (G72x_DATA))))
2615 return SFE_MALLOC_FAILED ;
2617 psf->fdata = (void*) pg72x ;
2619 pg72x->blockcount = 0 ;
2620 pg72x->samplecount = 0 ;
2622 switch (codec)
2623 { case AU_H_G721_32 :
2624 g72x_reader_init (pg72x, G721_32_BITS_PER_SAMPLE) ;
2625 pg72x->bytesperblock = G721_32_BYTES_PER_BLOCK ;
2626 bitspersample = G721_32_BITS_PER_SAMPLE ;
2627 break ;
2629 case AU_H_G723_24:
2630 g72x_reader_init (pg72x, G723_24_BITS_PER_SAMPLE) ;
2631 pg72x->bytesperblock = G723_24_BYTES_PER_BLOCK ;
2632 bitspersample = G723_24_BITS_PER_SAMPLE ;
2633 break ;
2635 case AU_H_G723_40:
2636 g72x_reader_init (pg72x, G723_40_BITS_PER_SAMPLE) ;
2637 pg72x->bytesperblock = G723_40_BYTES_PER_BLOCK ;
2638 bitspersample = G723_40_BITS_PER_SAMPLE ;
2639 break ;
2641 default : return SFE_UNIMPLEMENTED ;
2644 psf->read_short = au_g72x_read_s ;
2645 psf->read_int = au_g72x_read_i ;
2646 psf->read_float = au_g72x_read_f ;
2647 psf->read_double = au_g72x_read_d ;
2649 psf->seek = au_g72x_seek ;
2650 psf->close = au_g72x_close ;
2652 psf->blockwidth = psf->bytewidth = 1 ;
2654 psf->filelength = psf_get_filelen (psf) ;
2655 psf->datalength = psf->filelength - psf->dataoffset ;
2657 if (psf->datalength % pg72x->blocksize)
2658 pg72x->blocks = (psf->datalength / pg72x->blocksize) + 1 ;
2659 else
2660 pg72x->blocks = psf->datalength / pg72x->blocksize ;
2662 psf->sf.frames = (8 * psf->datalength) / bitspersample ;
2664 if ((psf->sf.frames * bitspersample) / 8 != psf->datalength)
2665 psf_log_printf (psf, "*** Warning : weird psf->datalength.\n") ;
2667 au_g72x_decode_block (psf, pg72x) ;
2669 return 0 ;
2670 } /* au_g72x_reader_init */
2672 /*============================================================================================
2673 ** WAV G721 writer initialisation function.
2677 au_g72x_writer_init (SF_PRIVATE *psf, int codec)
2678 { G72x_DATA *pg72x ;
2679 int bitspersample ;
2681 psf->sf.seekable = SF_FALSE ;
2683 if (psf->sf.channels != 1)
2684 return SFE_G72X_NOT_MONO ;
2686 if (! (pg72x = malloc (sizeof (G72x_DATA))))
2687 return SFE_MALLOC_FAILED ;
2689 psf->fdata = (void*) pg72x ;
2691 pg72x->blockcount = 0 ;
2692 pg72x->samplecount = 0 ;
2694 switch (codec)
2695 { case AU_H_G721_32 :
2696 g72x_writer_init (pg72x, G721_32_BITS_PER_SAMPLE) ;
2697 pg72x->bytesperblock = G721_32_BYTES_PER_BLOCK ;
2698 bitspersample = G721_32_BITS_PER_SAMPLE ;
2699 break ;
2701 case AU_H_G723_24:
2702 g72x_writer_init (pg72x, G723_24_BITS_PER_SAMPLE) ;
2703 pg72x->bytesperblock = G723_24_BYTES_PER_BLOCK ;
2704 bitspersample = G723_24_BITS_PER_SAMPLE ;
2705 break ;
2707 case AU_H_G723_40:
2708 g72x_writer_init (pg72x, G723_40_BITS_PER_SAMPLE) ;
2709 pg72x->bytesperblock = G723_40_BYTES_PER_BLOCK ;
2710 bitspersample = G723_40_BITS_PER_SAMPLE ;
2711 break ;
2713 default : return SFE_UNIMPLEMENTED ;
2716 psf->write_short = au_g72x_write_s ;
2717 psf->write_int = au_g72x_write_i ;
2718 psf->write_float = au_g72x_write_f ;
2719 psf->write_double = au_g72x_write_d ;
2721 psf->close = au_g72x_close ;
2723 psf->blockwidth = psf->bytewidth = 1 ;
2725 psf->filelength = psf_get_filelen (psf) ;
2726 if (psf->filelength < psf->dataoffset)
2727 psf->filelength = psf->dataoffset ;
2729 psf->datalength = psf->filelength - psf->dataoffset ;
2731 if (psf->datalength % pg72x->blocksize)
2732 pg72x->blocks = (psf->datalength / pg72x->blocksize) + 1 ;
2733 else
2734 pg72x->blocks = psf->datalength / pg72x->blocksize ;
2736 if (psf->datalength > 0)
2737 psf->sf.frames = (8 * psf->datalength) / bitspersample ;
2739 if ((psf->sf.frames * bitspersample) / 8 != psf->datalength)
2740 psf_log_printf (psf, "*** Warning : weird psf->datalength.\n") ;
2742 return 0 ;
2743 } /* au_g72x_writer_init */
2745 /*============================================================================================
2746 ** G721 Read Functions.
2749 static int
2750 au_g72x_decode_block (SF_PRIVATE *psf, G72x_DATA *pg72x)
2751 { int k ;
2753 pg72x->blockcount ++ ;
2754 pg72x->samplecount = 0 ;
2756 if (pg72x->samplecount > pg72x->blocksize)
2757 { memset (pg72x->samples, 0, G72x_BLOCK_SIZE * sizeof (short)) ;
2758 return 1 ;
2761 if ((k = psf_fread (pg72x->block, 1, pg72x->bytesperblock, psf)) != pg72x->bytesperblock)
2762 psf_log_printf (psf, "*** Warning : short read (%d != %d).\n", k, pg72x->bytesperblock) ;
2764 pg72x->blocksize = k ;
2765 g72x_decode_block (pg72x) ;
2767 return 1 ;
2768 } /* au_g72x_decode_block */
2770 static int
2771 au_g72x_read_block (SF_PRIVATE *psf, G72x_DATA *pg72x, short *ptr, int len)
2772 { int count, total = 0, indx = 0 ;
2774 while (indx < len)
2775 { if (pg72x->blockcount >= pg72x->blocks && pg72x->samplecount >= pg72x->samplesperblock)
2776 { memset (&(ptr [indx]), 0, (len - indx) * sizeof (short)) ;
2777 return total ;
2780 if (pg72x->samplecount >= pg72x->samplesperblock)
2781 au_g72x_decode_block (psf, pg72x) ;
2783 count = pg72x->samplesperblock - pg72x->samplecount ;
2784 count = (len - indx > count) ? count : len - indx ;
2786 memcpy (&(ptr [indx]), &(pg72x->samples [pg72x->samplecount]), count * sizeof (short)) ;
2787 indx += count ;
2788 pg72x->samplecount += count ;
2789 total = indx ;
2792 return total ;
2793 } /* au_g72x_read_block */
2795 static sf_count_t
2796 au_g72x_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
2797 { G72x_DATA *pg72x ;
2798 int readcount, count ;
2799 sf_count_t total = 0 ;
2801 if (! psf->fdata)
2802 return 0 ;
2803 pg72x = (G72x_DATA*) psf->fdata ;
2805 while (len > 0)
2806 { readcount = (len > 0x10000000) ? 0x10000000 : (int) len ;
2808 count = au_g72x_read_block (psf, pg72x, ptr, readcount) ;
2810 total += count ;
2811 len -= count ;
2813 if (count != readcount)
2814 break ;
2817 return total ;
2818 } /* au_g72x_read_s */
2820 static sf_count_t
2821 au_g72x_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
2822 { G72x_DATA *pg72x ;
2823 short *sptr ;
2824 int k, bufferlen, readcount = 0, count ;
2825 sf_count_t total = 0 ;
2827 if (! psf->fdata)
2828 return 0 ;
2829 pg72x = (G72x_DATA*) psf->fdata ;
2831 sptr = (short*) psf->buffer ;
2832 bufferlen = SF_BUFFER_LEN / sizeof (short) ;
2833 while (len > 0)
2834 { readcount = (len >= bufferlen) ? bufferlen : len ;
2835 count = au_g72x_read_block (psf, pg72x, sptr, readcount) ;
2837 for (k = 0 ; k < readcount ; k++)
2838 ptr [total + k] = sptr [k] << 16 ;
2840 total += count ;
2841 len -= readcount ;
2842 if (count != readcount)
2843 break ;
2846 return total ;
2847 } /* au_g72x_read_i */
2849 static sf_count_t
2850 au_g72x_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
2851 { G72x_DATA *pg72x ;
2852 short *sptr ;
2853 int k, bufferlen, readcount = 0, count ;
2854 sf_count_t total = 0 ;
2855 float normfact ;
2857 if (! psf->fdata)
2858 return 0 ;
2859 pg72x = (G72x_DATA*) psf->fdata ;
2861 normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x8000) : 1.0 ;
2863 sptr = (short*) psf->buffer ;
2864 bufferlen = SF_BUFFER_LEN / sizeof (short) ;
2865 while (len > 0)
2866 { readcount = (len >= bufferlen) ? bufferlen : len ;
2867 count = au_g72x_read_block (psf, pg72x, sptr, readcount) ;
2868 for (k = 0 ; k < readcount ; k++)
2869 ptr [total + k] = normfact * sptr [k] ;
2871 total += count ;
2872 len -= readcount ;
2873 if (count != readcount)
2874 break ;
2877 return total ;
2878 } /* au_g72x_read_f */
2880 static sf_count_t
2881 au_g72x_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
2882 { G72x_DATA *pg72x ;
2883 short *sptr ;
2884 int k, bufferlen, readcount = 0, count ;
2885 sf_count_t total = 0 ;
2886 double normfact ;
2888 if (! psf->fdata)
2889 return 0 ;
2890 pg72x = (G72x_DATA*) psf->fdata ;
2892 normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x8000) : 1.0 ;
2894 sptr = (short*) psf->buffer ;
2895 bufferlen = SF_BUFFER_LEN / sizeof (short) ;
2896 while (len > 0)
2897 { readcount = (len >= bufferlen) ? bufferlen : len ;
2898 count = au_g72x_read_block (psf, pg72x, sptr, readcount) ;
2899 for (k = 0 ; k < readcount ; k++)
2900 ptr [total + k] = normfact * (double) (sptr [k]) ;
2902 total += count ;
2903 len -= readcount ;
2904 if (count != readcount)
2905 break ;
2908 return total ;
2909 } /* au_g72x_read_d */
2911 static sf_count_t
2912 au_g72x_seek (SF_PRIVATE *psf, int mode, sf_count_t offset)
2914 /* Prevent compiler warnings. */
2915 mode ++ ;
2916 offset ++ ;
2918 psf_log_printf (psf, "seek unsupported\n") ;
2920 /* No simple solution. To do properly, would need to seek
2921 ** to start of file and decode everything up to seek position.
2922 ** Maybe implement SEEK_SET to 0 only?
2924 return 0 ;
2927 ** G72x_DATA *pg72x ;
2928 ** int newblock, newsample, samplecount ;
2930 ** if (! psf->fdata)
2931 ** return 0 ;
2932 ** pg72x = (G72x_DATA*) psf->fdata ;
2934 ** if (! (psf->datalength && psf->dataoffset))
2935 ** { psf->error = SFE_BAD_SEEK ;
2936 ** return ((sf_count_t) -1) ;
2937 ** } ;
2939 ** samplecount = (8 * psf->datalength) / G721_32_BITS_PER_SAMPLE ;
2941 ** switch (whence)
2942 ** { case SEEK_SET :
2943 ** if (offset < 0 || offset > samplecount)
2944 ** { psf->error = SFE_BAD_SEEK ;
2945 ** return ((sf_count_t) -1) ;
2946 ** } ;
2947 ** newblock = offset / pg72x->samplesperblock ;
2948 ** newsample = offset % pg72x->samplesperblock ;
2949 ** break ;
2951 ** case SEEK_CUR :
2952 ** if (psf->current + offset < 0 || psf->current + offset > samplecount)
2953 ** { psf->error = SFE_BAD_SEEK ;
2954 ** return ((sf_count_t) -1) ;
2955 ** } ;
2956 ** newblock = (8 * (psf->current + offset)) / pg72x->samplesperblock ;
2957 ** newsample = (8 * (psf->current + offset)) % pg72x->samplesperblock ;
2958 ** break ;
2960 ** case SEEK_END :
2961 ** if (offset > 0 || samplecount + offset < 0)
2962 ** { psf->error = SFE_BAD_SEEK ;
2963 ** return ((sf_count_t) -1) ;
2964 ** } ;
2965 ** newblock = (samplecount + offset) / pg72x->samplesperblock ;
2966 ** newsample = (samplecount + offset) % pg72x->samplesperblock ;
2967 ** break ;
2969 ** default :
2970 ** psf->error = SFE_BAD_SEEK ;
2971 ** return ((sf_count_t) -1) ;
2972 ** } ;
2974 ** if (psf->mode == SFM_READ)
2975 ** { psf_fseek (psf, psf->dataoffset + newblock * pg72x->blocksize, SEEK_SET) ;
2976 ** pg72x->blockcount = newblock ;
2977 ** au_g72x_decode_block (psf, pg72x) ;
2978 ** pg72x->samplecount = newsample ;
2979 ** }
2980 ** else
2981 ** { /+* What to do about write??? *+/
2982 ** psf->error = SFE_BAD_SEEK ;
2983 ** return ((sf_count_t) -1) ;
2984 ** } ;
2986 ** psf->current = newblock * pg72x->samplesperblock + newsample ;
2987 ** return psf->current ;
2990 } /* au_g72x_seek */
2992 /*==========================================================================================
2993 ** G72x Write Functions.
2996 static int
2997 au_g72x_encode_block (SF_PRIVATE *psf, G72x_DATA *pg72x)
2998 { int k ;
3000 /* Encode the samples. */
3001 g72x_encode_block (pg72x) ;
3003 /* Write the block to disk. */
3004 if ((k = psf_fwrite (pg72x->block, 1, pg72x->blocksize, psf)) != pg72x->blocksize)
3005 psf_log_printf (psf, "*** Warning : short write (%d != %d).\n", k, pg72x->blocksize) ;
3007 pg72x->samplecount = 0 ;
3008 pg72x->blockcount ++ ;
3010 /* Set samples to zero for next block. */
3011 memset (pg72x->samples, 0, G72x_BLOCK_SIZE * sizeof (short)) ;
3013 return 1 ;
3014 } /* au_g72x_encode_block */
3016 static int
3017 au_g72x_write_block (SF_PRIVATE *psf, G72x_DATA *pg72x, short *ptr, int len)
3018 { int count, total = 0, indx = 0 ;
3020 while (indx < len)
3021 { count = pg72x->samplesperblock - pg72x->samplecount ;
3023 if (count > len - indx)
3024 count = len - indx ;
3026 memcpy (&(pg72x->samples [pg72x->samplecount]), &(ptr [indx]), count * sizeof (short)) ;
3027 indx += count ;
3028 pg72x->samplecount += count ;
3029 total = indx ;
3031 if (pg72x->samplecount >= pg72x->samplesperblock)
3032 au_g72x_encode_block (psf, pg72x) ;
3035 return total ;
3036 } /* au_g72x_write_block */
3038 static sf_count_t
3039 au_g72x_write_s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
3040 { G72x_DATA *pg72x ;
3041 int writecount, count ;
3042 sf_count_t total = 0 ;
3044 if (! psf->fdata)
3045 return 0 ;
3046 pg72x = (G72x_DATA*) psf->fdata ;
3048 while (len > 0)
3049 { writecount = (len > 0x10000000) ? 0x10000000 : (int) len ;
3051 count = au_g72x_write_block (psf, pg72x, ptr, writecount) ;
3053 total += count ;
3054 len -= count ;
3055 if (count != writecount)
3056 break ;
3059 return total ;
3060 } /* au_g72x_write_s */
3062 static sf_count_t
3063 au_g72x_write_i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
3064 { G72x_DATA *pg72x ;
3065 short *sptr ;
3066 int k, bufferlen, writecount = 0, count ;
3067 sf_count_t total = 0 ;
3069 if (! psf->fdata)
3070 return 0 ;
3071 pg72x = (G72x_DATA*) psf->fdata ;
3073 sptr = (short*) psf->buffer ;
3074 bufferlen = ((SF_BUFFER_LEN / psf->blockwidth) * psf->blockwidth) / sizeof (short) ;
3075 while (len > 0)
3076 { writecount = (len >= bufferlen) ? bufferlen : len ;
3077 for (k = 0 ; k < writecount ; k++)
3078 sptr [k] = ptr [total + k] >> 16 ;
3079 count = au_g72x_write_block (psf, pg72x, sptr, writecount) ;
3081 total += count ;
3082 len -= writecount ;
3083 if (count != writecount)
3084 break ;
3086 return total ;
3087 } /* au_g72x_write_i */
3089 static sf_count_t
3090 au_g72x_write_f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
3091 { G72x_DATA *pg72x ;
3092 short *sptr ;
3093 int k, bufferlen, writecount = 0, count ;
3094 sf_count_t total = 0 ;
3095 float normfact ;
3097 if (! psf->fdata)
3098 return 0 ;
3099 pg72x = (G72x_DATA*) psf->fdata ;
3101 normfact = (psf->norm_float == SF_TRUE) ? (1.0 * 0x8000) : 1.0 ;
3103 sptr = (short*) psf->buffer ;
3104 bufferlen = ((SF_BUFFER_LEN / psf->blockwidth) * psf->blockwidth) / sizeof (short) ;
3105 while (len > 0)
3106 { writecount = (len >= bufferlen) ? bufferlen : len ;
3107 for (k = 0 ; k < writecount ; k++)
3108 sptr [k] = lrintf (normfact * ptr [total + k]) ;
3109 count = au_g72x_write_block (psf, pg72x, sptr, writecount) ;
3111 total += count ;
3112 len -= writecount ;
3113 if (count != writecount)
3114 break ;
3117 return total ;
3118 } /* au_g72x_write_f */
3120 static sf_count_t
3121 au_g72x_write_d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
3122 { G72x_DATA *pg72x ;
3123 short *sptr ;
3124 int k, bufferlen, writecount = 0, count ;
3125 sf_count_t total = 0 ;
3126 double normfact ;
3128 if (! psf->fdata)
3129 return 0 ;
3130 pg72x = (G72x_DATA*) psf->fdata ;
3132 normfact = (psf->norm_double == SF_TRUE) ? (1.0 * 0x8000) : 1.0 ;
3134 sptr = (short*) psf->buffer ;
3135 bufferlen = ((SF_BUFFER_LEN / psf->blockwidth) * psf->blockwidth) / sizeof (short) ;
3136 while (len > 0)
3137 { writecount = (len >= bufferlen) ? bufferlen : len ;
3138 for (k = 0 ; k < writecount ; k++)
3139 sptr [k] = lrint (normfact * ptr [total + k]) ;
3140 count = au_g72x_write_block (psf, pg72x, sptr, writecount) ;
3142 total += count ;
3143 len -= writecount ;
3144 if (count != writecount)
3145 break ;
3148 return total ;
3149 } /* au_g72x_write_d */
3151 static int
3152 au_g72x_close (SF_PRIVATE *psf)
3153 { G72x_DATA *pg72x ;
3155 if (! psf->fdata)
3156 return 0 ;
3158 pg72x = (G72x_DATA*) psf->fdata ;
3160 if (psf->mode == SFM_WRITE)
3161 { /* If a block has been partially assembled, write it out
3162 ** as the final block.
3165 if (pg72x->samplecount && pg72x->samplecount < G72x_BLOCK_SIZE)
3166 au_g72x_encode_block (psf, pg72x) ;
3168 if (psf->write_header)
3169 psf->write_header (psf, SF_FALSE) ;
3172 return 0 ;
3173 } /* au_g72x_close */
3176 ** Do not edit or modify anything in this comment block.
3177 ** The arch-tag line is a file identity tag for the GNU Arch
3178 ** revision control system.
3180 ** arch-tag: 3cc5439e-7247-486b-b2e6-11a4affa5744
3183 ** Copyright (C) 2004 Erik de Castro Lopo <erikd@mega-nerd.com>
3185 ** This program is free software; you can redistribute it and/or modify
3186 ** it under the terms of the GNU Lesser General Public License as published by
3187 ** the Free Software Foundation; either version 2.1 of the License, or
3188 ** (at your option) any later version.
3190 ** This program is distributed in the hope that it will be useful,
3191 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
3192 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3193 ** GNU Lesser General Public License for more details.
3195 ** You should have received a copy of the GNU Lesser General Public License
3196 ** along with this program; if not, write to the Free Software
3197 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
3201 #include <stdio.h>
3202 #include <string.h>
3205 #define TWOBIT_MARKER (MAKE_MARKER ('2', 'B', 'I', 'T'))
3206 #define AVR_HDR_SIZE 128
3208 #define SFE_AVR_X 666
3211 ** From: hyc@hanauma.Jpl.Nasa.Gov (Howard Chu)
3213 ** A lot of PD software exists to play Mac .snd files on the ST. One other
3214 ** format that seems pretty popular (used by a number of commercial packages)
3215 ** is the AVR format (from Audio Visual Research). This format has a 128 byte
3216 ** header that looks like this (its actually packed, but thats not portable):
3219 typedef struct
3220 { int marker ; /* 2BIT */
3221 char name [8] ; /* null-padded sample name */
3222 short mono ; /* 0 = mono, 0xffff = stereo */
3223 short rez ; /* 8 = 8 bit, 16 = 16 bit */
3224 short sign ; /* 0 = unsigned, 0xffff = signed */
3226 short loop ; /* 0 = no loop, 0xffff = looping sample */
3227 short midi ; /* 0xffff = no MIDI note assigned, */
3228 /* 0xffXX = single key note assignment */
3229 /* 0xLLHH = key split, low/hi note */
3230 int srate ; /* sample frequency in hertz */
3231 int frames ; /* sample length in bytes or words (see rez) */
3232 int lbeg ; /* offset to start of loop in bytes or words. */
3233 /* set to zero if unused */
3234 int lend ; /* offset to end of loop in bytes or words. */
3235 /* set to sample length if unused */
3236 short res1 ; /* Reserved, MIDI keyboard split */
3237 short res2 ; /* Reserved, sample compression */
3238 short res3 ; /* Reserved */
3239 char ext [20] ; /* Additional filename space, used if (name[7] != 0) */
3240 char user [64] ; /* User defined. Typically ASCII message */
3241 } AVR_HEADER ;
3243 /*------------------------------------------------------------------------------
3244 ** Private static functions.
3247 static int avr_close (SF_PRIVATE *psf) ;
3249 static int avr_read_header (SF_PRIVATE *psf) ;
3250 static int avr_write_header (SF_PRIVATE *psf, int calc_length) ;
3252 /*------------------------------------------------------------------------------
3253 ** Public function.
3257 avr_open (SF_PRIVATE *psf)
3258 { int subformat ;
3259 int error = 0 ;
3261 if (psf->mode == SFM_READ || (psf->mode == SFM_RDWR && psf->filelength > 0))
3262 { if ((error = avr_read_header (psf)))
3263 return error ;
3266 if ((psf->sf.format & SF_FORMAT_TYPEMASK) != SF_FORMAT_AVR)
3267 return SFE_BAD_OPEN_FORMAT ;
3269 subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
3271 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
3272 { psf->endian = psf->sf.format & SF_FORMAT_ENDMASK ;
3273 psf->endian = SF_ENDIAN_BIG ;
3275 if (avr_write_header (psf, SF_FALSE))
3276 return psf->error ;
3278 psf->write_header = avr_write_header ;
3281 psf->close = avr_close ;
3283 psf->blockwidth = psf->bytewidth * psf->sf.channels ;
3285 error = pcm_init (psf) ;
3287 return error ;
3288 } /* avr_open */
3290 static int
3291 avr_read_header (SF_PRIVATE *psf)
3292 { AVR_HEADER hdr ;
3294 memset (&hdr, 0, sizeof (hdr)) ;
3296 psf_binheader_readf (psf, "pmb", 0, &hdr.marker, &hdr.name, sizeof (hdr.name)) ;
3297 psf_log_printf (psf, "%M\n", hdr.marker) ;
3299 if (hdr.marker != TWOBIT_MARKER)
3300 return SFE_AVR_X ;
3302 psf_log_printf (psf, " Name : %s\n", hdr.name) ;
3304 psf_binheader_readf (psf, "E22222", &hdr.mono, &hdr.rez, &hdr.sign, &hdr.loop, &hdr.midi) ;
3306 psf->sf.channels = (hdr.mono & 1) + 1 ;
3308 psf_log_printf (psf, " Channels : %d\n Bit width : %d\n Signed : %s\n",
3309 (hdr.mono & 1) + 1, hdr.rez, hdr.sign ? "yes" : "no") ;
3311 switch ((hdr.rez << 16) + (hdr.sign & 1))
3312 { case ((8 << 16) + 0) :
3313 psf->sf.format = SF_FORMAT_AVR | SF_FORMAT_PCM_U8 ;
3314 psf->bytewidth = 1 ;
3315 break ;
3317 case ((8 << 16) + 1) :
3318 psf->sf.format = SF_FORMAT_AVR | SF_FORMAT_PCM_S8 ;
3319 psf->bytewidth = 1 ;
3320 break ;
3322 case ((16 << 16) + 1) :
3323 psf->sf.format = SF_FORMAT_AVR | SF_FORMAT_PCM_16 ;
3324 psf->bytewidth = 2 ;
3325 break ;
3327 default :
3328 psf_log_printf (psf, "Error : bad rez/sign combination.\n") ;
3329 return SFE_AVR_X ;
3330 break ;
3333 psf_binheader_readf (psf, "E4444", &hdr.srate, &hdr.frames, &hdr.lbeg, &hdr.lend) ;
3335 psf->sf.frames = hdr.frames ;
3336 psf->sf.samplerate = hdr.srate ;
3338 psf_log_printf (psf, " Frames : %D\n", psf->sf.frames) ;
3339 psf_log_printf (psf, " Sample rate : %d\n", psf->sf.samplerate) ;
3341 psf_binheader_readf (psf, "E222", &hdr.res1, &hdr.res2, &hdr.res3) ;
3342 psf_binheader_readf (psf, "bb", hdr.ext, sizeof (hdr.ext), hdr.user, sizeof (hdr.user)) ;
3344 psf_log_printf (psf, " Ext : %s\n User : %s\n", hdr.ext, hdr.user) ;
3346 psf->endian = SF_ENDIAN_BIG ;
3348 psf->dataoffset = AVR_HDR_SIZE ;
3349 psf->datalength = hdr.frames * (hdr.rez / 8) ;
3351 if (psf->fileoffset > 0)
3352 psf->filelength = AVR_HDR_SIZE + psf->datalength ;
3354 if (psf_ftell (psf) != psf->dataoffset)
3355 psf_binheader_readf (psf, "j", psf->dataoffset - psf_ftell (psf)) ;
3357 psf->close = avr_close ;
3359 psf->blockwidth = psf->sf.channels * psf->bytewidth ;
3361 if (psf->sf.frames == 0 && psf->blockwidth)
3362 psf->sf.frames = (psf->filelength - psf->dataoffset) / psf->blockwidth ;
3364 return 0 ;
3365 } /* avr_read_header */
3367 static int
3368 avr_write_header (SF_PRIVATE *psf, int calc_length)
3369 { sf_count_t current ;
3370 int sign, datalength ;
3372 if (psf->pipeoffset > 0)
3373 return 0 ;
3375 current = psf_ftell (psf) ;
3377 if (calc_length)
3378 { psf->filelength = psf_get_filelen (psf) ;
3380 psf->datalength = psf->filelength - psf->dataoffset ;
3381 if (psf->dataend)
3382 psf->datalength -= psf->filelength - psf->dataend ;
3384 psf->sf.frames = psf->datalength / (psf->bytewidth * psf->sf.channels) ;
3387 /* Reset the current header length to zero. */
3388 psf->header [0] = 0 ;
3389 psf->headindex = 0 ;
3392 ** Only attempt to seek if we are not writng to a pipe. If we are
3393 ** writing to a pipe we shouldn't be here anyway.
3395 if (psf->is_pipe == SF_FALSE)
3396 psf_fseek (psf, 0, SEEK_SET) ;
3398 datalength = (int) (psf->datalength & 0x7FFFFFFF) ;
3400 psf_binheader_writef (psf, "Emz22", TWOBIT_MARKER, (size_t) 8,
3401 psf->sf.channels == 2 ? 0xFFFF : 0, psf->bytewidth * 8) ;
3403 sign = ((psf->sf.format & SF_FORMAT_SUBMASK) == SF_FORMAT_PCM_U8) ? 0 : 0xFFFF ;
3405 psf_binheader_writef (psf, "E222", sign, 0, 0xFFFF) ;
3406 psf_binheader_writef (psf, "E4444", psf->sf.samplerate, psf->sf.frames, 0, 0) ;
3408 psf_binheader_writef (psf, "E222zz", 0, 0, 0, (size_t) 20, (size_t) 64) ;
3410 /* Header construction complete so write it out. */
3411 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
3413 if (psf->error)
3414 return psf->error ;
3416 psf->dataoffset = psf->headindex ;
3418 if (current > 0)
3419 psf_fseek (psf, current, SEEK_SET) ;
3421 return psf->error ;
3422 } /* avr_write_header */
3424 static int
3425 avr_close (SF_PRIVATE *psf)
3427 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
3428 avr_write_header (psf, SF_TRUE) ;
3430 return 0 ;
3431 } /* avr_close */
3434 ** Do not edit or modify anything in this comment block.
3435 ** The arch-tag line is a file identity tag for the GNU Arch
3436 ** revision control system.
3438 ** arch-tag: 0823d454-f39a-4a28-a776-607f1ef33b52
3441 * Copyright 1992 by Jutta Degener and Carsten Bormann, Technische
3442 * Universitaet Berlin. See the accompanying file "COPYRIGHT" for
3443 * details. THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE.
3447 #include <stdlib.h>
3448 #include <string.h>
3453 * 4.2 FIXED POINT IMPLEMENTATION OF THE RPE-LTP CODER
3456 void Gsm_Coder (
3458 struct gsm_state * State,
3460 word * s, /* [0..159] samples IN */
3463 * The RPE-LTD coder works on a frame by frame basis. The length of
3464 * the frame is equal to 160 samples. Some computations are done
3465 * once per frame to produce at the output of the coder the
3466 * LARc[1..8] parameters which are the coded LAR coefficients and
3467 * also to realize the inverse filtering operation for the entire
3468 * frame (160 samples of signal d[0..159]). These parts produce at
3469 * the output of the coder:
3472 word * LARc, /* [0..7] LAR coefficients OUT */
3475 * Procedure 4.2.11 to 4.2.18 are to be executed four times per
3476 * frame. That means once for each sub-segment RPE-LTP analysis of
3477 * 40 samples. These parts produce at the output of the coder:
3480 word * Nc, /* [0..3] LTP lag OUT */
3481 word * bc, /* [0..3] coded LTP gain OUT */
3482 word * Mc, /* [0..3] RPE grid selection OUT */
3483 word * xmaxc,/* [0..3] Coded maximum amplitude OUT */
3484 word * xMc /* [13*4] normalized RPE samples OUT */
3487 int k;
3488 word * dp = State->dp0 + 120; /* [ -120...-1 ] */
3489 word * dpp = dp; /* [ 0...39 ] */
3491 word so[160];
3493 Gsm_Preprocess (State, s, so);
3494 Gsm_LPC_Analysis (State, so, LARc);
3495 Gsm_Short_Term_Analysis_Filter (State, LARc, so);
3497 for (k = 0; k <= 3; k++, xMc += 13) {
3499 Gsm_Long_Term_Predictor ( State,
3500 so+k*40, /* d [0..39] IN */
3501 dp, /* dp [-120..-1] IN */
3502 State->e + 5, /* e [0..39] OUT */
3503 dpp, /* dpp [0..39] OUT */
3504 Nc++,
3505 bc++);
3507 Gsm_RPE_Encoding ( /*-S,-*/
3508 State->e + 5, /* e ][0..39][ IN/OUT */
3509 xmaxc++, Mc++, xMc );
3511 * Gsm_Update_of_reconstructed_short_time_residual_signal
3512 * ( dpp, e + 5, dp );
3515 { register int i;
3516 for (i = 0; i <= 39; i++)
3517 dp[ i ] = GSM_ADD( State->e[5 + i], dpp[i] );
3519 dp += 40;
3520 dpp += 40;
3523 (void)memcpy( (char *)State->dp0, (char *)(State->dp0 + 160),
3524 120 * sizeof(*State->dp0) );
3527 ** Do not edit or modify anything in this comment block.
3528 ** The arch-tag line is a file identity tag for the GNU Arch
3529 ** revision control system.
3531 ** arch-tag: ae8ef1b2-5a1e-4263-94cd-42b15dca81a3
3535 ** Copyright (C) 2001-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
3537 ** This program is free software; you can redistribute it and/or modify
3538 ** it under the terms of the GNU Lesser General Public License as published by
3539 ** the Free Software Foundation; either version 2.1 of the License, or
3540 ** (at your option) any later version.
3542 ** This program is distributed in the hope that it will be useful,
3543 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
3544 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3545 ** GNU Lesser General Public License for more details.
3547 ** You should have received a copy of the GNU Lesser General Public License
3548 ** along with this program; if not, write to the Free Software
3549 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
3552 #include <stdio.h>
3553 #include <string.h>
3554 #include <math.h>
3557 static SF_FORMAT_INFO const simple_formats [] =
3559 { SF_FORMAT_AIFF | SF_FORMAT_PCM_16,
3560 "AIFF (Apple/SGI 16 bit PCM)", "aiff"
3563 { SF_FORMAT_AIFF | SF_FORMAT_FLOAT,
3564 "AIFF (Apple/SGI 32 bit float)", "aifc"
3567 { SF_FORMAT_AIFF | SF_FORMAT_PCM_S8,
3568 "AIFF (Apple/SGI 8 bit PCM)", "aiff"
3571 { SF_FORMAT_AU | SF_FORMAT_PCM_16,
3572 "AU (Sun/Next 16 bit PCM)", "au"
3575 { SF_FORMAT_AU | SF_FORMAT_ULAW,
3576 "AU (Sun/Next 8-bit u-law)", "au"
3579 { SF_FORMAT_RAW | SF_FORMAT_VOX_ADPCM,
3580 "OKI Dialogic VOX ADPCM", "vox"
3583 { SF_FORMAT_WAV | SF_FORMAT_PCM_16,
3584 "WAV (Microsoft 16 bit PCM)", "wav"
3587 { SF_FORMAT_WAV | SF_FORMAT_FLOAT,
3588 "WAV (Microsoft 32 bit float)", "wav"
3591 { SF_FORMAT_WAV | SF_FORMAT_IMA_ADPCM,
3592 "WAV (Microsoft 4 bit IMA ADPCM)", "wav"
3595 { SF_FORMAT_WAV | SF_FORMAT_MS_ADPCM,
3596 "WAV (Microsoft 4 bit MS ADPCM)", "wav"
3599 { SF_FORMAT_WAV | SF_FORMAT_PCM_U8,
3600 "WAV (Microsoft 8 bit PCM)", "wav"
3603 } ; /* simple_formats */
3606 psf_get_format_simple_count (void)
3607 { return (sizeof (simple_formats) / sizeof (SF_FORMAT_INFO)) ;
3608 } /* psf_get_format_simple_count */
3611 psf_get_format_simple (SF_FORMAT_INFO *data)
3612 { int indx ;
3614 if (data->format < 0 || data->format >= (SIGNED_SIZEOF (simple_formats) / SIGNED_SIZEOF (SF_FORMAT_INFO)))
3615 return SFE_BAD_CONTROL_CMD ;
3617 indx = data->format ;
3618 memcpy (data, &(simple_formats [indx]), SIGNED_SIZEOF (SF_FORMAT_INFO)) ;
3620 return 0 ;
3621 } /* psf_get_format_simple */
3623 /*============================================================================
3624 ** Major format info.
3627 static SF_FORMAT_INFO const major_formats [] =
3629 { SF_FORMAT_AIFF, "AIFF (Apple/SGI)", "aiff" },
3630 { SF_FORMAT_AU, "AU (Sun/NeXT)", "au" },
3631 { SF_FORMAT_AVR, "AVR (Audio Visual Research)", "avr" },
3632 { SF_FORMAT_HTK, "HTK (HMM Tool Kit)", "htk" },
3633 { SF_FORMAT_SVX, "IFF (Amiga IFF/SVX8/SV16)", "iff" },
3634 { SF_FORMAT_MAT4, "MAT4 (GNU Octave 2.0 / Matlab 4.2)", "mat" },
3635 { SF_FORMAT_MAT5, "MAT5 (GNU Octave 2.1 / Matlab 5.0)", "mat" },
3636 { SF_FORMAT_PAF, "PAF (Ensoniq PARIS)", "paf" },
3637 { SF_FORMAT_PVF, "PVF (Portable Voice Format)", "pvf" },
3638 { SF_FORMAT_RAW, "RAW (header-less)", "raw" },
3639 { SF_FORMAT_SDS, "SDS (Midi Sample Dump Standard)", "sds" },
3640 /* Not ready for mainstream use yet.
3641 { SF_FORMAT_SD2, "SD2 (Sound Designer II)", "sd2" },
3643 { SF_FORMAT_IRCAM, "SF (Berkeley/IRCAM/CARL)", "sf" },
3644 { SF_FORMAT_VOC, "VOC (Creative Labs)", "voc" },
3645 { SF_FORMAT_W64, "W64 (SoundFoundry WAVE 64)", "w64" },
3646 { SF_FORMAT_WAV, "WAV (Microsoft)", "wav" },
3647 { SF_FORMAT_NIST, "WAV (NIST Sphere)", "wav" },
3648 { SF_FORMAT_WAVEX, "WAVEX (Microsoft)", "wav" },
3649 { SF_FORMAT_XI, "XI (FastTracker 2)", "xi" },
3651 } ; /* major_formats */
3654 psf_get_format_major_count (void)
3655 { return (sizeof (major_formats) / sizeof (SF_FORMAT_INFO)) ;
3656 } /* psf_get_format_major_count */
3659 psf_get_format_major (SF_FORMAT_INFO *data)
3660 { int indx ;
3662 if (data->format < 0 || data->format >= (SIGNED_SIZEOF (major_formats) / SIGNED_SIZEOF (SF_FORMAT_INFO)))
3663 return SFE_BAD_CONTROL_CMD ;
3665 indx = data->format ;
3666 memcpy (data, &(major_formats [indx]), SIGNED_SIZEOF (SF_FORMAT_INFO)) ;
3668 return 0 ;
3669 } /* psf_get_format_major */
3671 /*============================================================================
3672 ** Subtype format info.
3675 static SF_FORMAT_INFO subtype_formats [] =
3677 { SF_FORMAT_PCM_S8, "Signed 8 bit PCM", NULL },
3678 { SF_FORMAT_PCM_16, "Signed 16 bit PCM", NULL },
3679 { SF_FORMAT_PCM_24, "Signed 24 bit PCM", NULL },
3680 { SF_FORMAT_PCM_32, "Signed 32 bit PCM", NULL },
3682 { SF_FORMAT_PCM_U8, "Unsigned 8 bit PCM", NULL },
3684 { SF_FORMAT_FLOAT, "32 bit float", NULL },
3685 { SF_FORMAT_DOUBLE, "64 bit float", NULL },
3687 { SF_FORMAT_ULAW, "U-Law", NULL },
3688 { SF_FORMAT_ALAW, "A-Law", NULL },
3689 { SF_FORMAT_IMA_ADPCM, "IMA ADPCM", NULL },
3690 { SF_FORMAT_MS_ADPCM, "Microsoft ADPCM", NULL },
3692 { SF_FORMAT_GSM610, "GSM 6.10", NULL },
3694 { SF_FORMAT_G721_32, "32kbs G721 ADPCM", NULL },
3695 { SF_FORMAT_G723_24, "24kbs G723 ADPCM", NULL },
3697 { SF_FORMAT_DWVW_12, "12 bit DWVW", NULL },
3698 { SF_FORMAT_DWVW_16, "16 bit DWVW", NULL },
3699 { SF_FORMAT_DWVW_24, "24 bit DWVW", NULL },
3700 { SF_FORMAT_VOX_ADPCM, "VOX ADPCM", "vox" },
3702 { SF_FORMAT_DPCM_16, "16 bit DPCM", NULL },
3703 { SF_FORMAT_DPCM_8, "8 bit DPCM", NULL },
3704 } ; /* subtype_formats */
3707 psf_get_format_subtype_count (void)
3708 { return (sizeof (subtype_formats) / sizeof (SF_FORMAT_INFO)) ;
3709 } /* psf_get_format_subtype_count */
3712 psf_get_format_subtype (SF_FORMAT_INFO *data)
3713 { int indx ;
3715 if (data->format < 0 || data->format >= (SIGNED_SIZEOF (subtype_formats) / SIGNED_SIZEOF (SF_FORMAT_INFO)))
3716 return SFE_BAD_CONTROL_CMD ;
3718 indx = data->format ;
3719 memcpy (data, &(subtype_formats [indx]), sizeof (SF_FORMAT_INFO)) ;
3721 return 0 ;
3722 } /* psf_get_format_subtype */
3724 /*==============================================================================
3728 psf_get_format_info (SF_FORMAT_INFO *data)
3729 { int k, format ;
3731 if (data->format & SF_FORMAT_TYPEMASK)
3732 { format = data->format & SF_FORMAT_TYPEMASK ;
3734 for (k = 0 ; k < (SIGNED_SIZEOF (major_formats) / SIGNED_SIZEOF (SF_FORMAT_INFO)) ; k++)
3735 { if (format == major_formats [k].format)
3736 { memcpy (data, &(major_formats [k]), sizeof (SF_FORMAT_INFO)) ;
3737 return 0 ;
3741 else if (data->format & SF_FORMAT_SUBMASK)
3742 { format = data->format & SF_FORMAT_SUBMASK ;
3744 for (k = 0 ; k < (SIGNED_SIZEOF (subtype_formats) / SIGNED_SIZEOF (SF_FORMAT_INFO)) ; k++)
3745 { if (format == subtype_formats [k].format)
3746 { memcpy (data, &(subtype_formats [k]), sizeof (SF_FORMAT_INFO)) ;
3747 return 0 ;
3752 memset (data, 0, sizeof (SF_FORMAT_INFO)) ;
3754 return SFE_BAD_CONTROL_CMD ;
3755 } /* psf_get_format_info */
3757 /*==============================================================================
3760 double
3761 psf_calc_signal_max (SF_PRIVATE *psf, int normalize)
3762 { sf_count_t position ;
3763 double max_val = 0.0, temp, *data ;
3764 int k, len, readcount, save_state ;
3766 /* If the file is not seekable, there is nothing we can do. */
3767 if (! psf->sf.seekable)
3768 { psf->error = SFE_NOT_SEEKABLE ;
3769 return 0.0 ;
3772 if (! psf->read_double)
3773 { psf->error = SFE_UNIMPLEMENTED ;
3774 return 0.0 ;
3777 save_state = sf_command ((SNDFILE*) psf, SFC_GET_NORM_DOUBLE, NULL, 0) ;
3779 sf_command ((SNDFILE*) psf, SFC_SET_NORM_DOUBLE, NULL, normalize) ;
3781 /* Brute force. Read the whole file and find the biggest sample. */
3782 position = sf_seek ((SNDFILE*) psf, 0, SEEK_CUR) ; /* Get current position in file */
3783 sf_seek ((SNDFILE*) psf, 0, SEEK_SET) ; /* Go to start of file. */
3785 len = sizeof (psf->buffer) / sizeof (double) ;
3787 data = (double*) psf->buffer ;
3789 readcount = len ;
3790 while (readcount > 0)
3791 { readcount = sf_read_double ((SNDFILE*) psf, data, len) ;
3792 for (k = 0 ; k < readcount ; k++)
3793 { temp = fabs (data [k]) ;
3794 max_val = temp > max_val ? temp : max_val ;
3798 sf_seek ((SNDFILE*) psf, position, SEEK_SET) ; /* Return to original position. */
3800 sf_command ((SNDFILE*) psf, SFC_SET_NORM_DOUBLE, NULL, save_state) ;
3802 return max_val ;
3803 } /* psf_calc_signal_max */
3806 psf_calc_max_all_channels (SF_PRIVATE *psf, double *peaks, int normalize)
3807 { sf_count_t position ;
3808 double temp, *data ;
3809 int k, len, readcount, save_state ;
3810 int chan ;
3812 /* If the file is not seekable, there is nothing we can do. */
3813 if (! psf->sf.seekable)
3814 return (psf->error = SFE_NOT_SEEKABLE) ;
3816 if (! psf->read_double)
3817 return (psf->error = SFE_UNIMPLEMENTED) ;
3819 save_state = sf_command ((SNDFILE*) psf, SFC_GET_NORM_DOUBLE, NULL, 0) ;
3820 sf_command ((SNDFILE*) psf, SFC_SET_NORM_DOUBLE, NULL, normalize) ;
3822 memset (peaks, 0, sizeof (double) * psf->sf.channels) ;
3824 /* Brute force. Read the whole file and find the biggest sample for each channel. */
3825 position = sf_seek ((SNDFILE*) psf, 0, SEEK_CUR) ; /* Get current position in file */
3826 sf_seek ((SNDFILE*) psf, 0, SEEK_SET) ; /* Go to start of file. */
3828 len = sizeof (psf->buffer) / sizeof (double) ;
3830 data = (double*) psf->buffer ;
3832 chan = 0 ;
3833 readcount = len ;
3834 while (readcount > 0)
3835 { readcount = sf_read_double ((SNDFILE*) psf, data, len) ;
3836 for (k = 0 ; k < readcount ; k++)
3837 { temp = fabs (data [k]) ;
3838 peaks [chan] = temp > peaks [chan] ? temp : peaks [chan] ;
3839 chan = (chan + 1) % psf->sf.channels ;
3843 sf_seek ((SNDFILE*) psf, position, SEEK_SET) ; /* Return to original position. */
3845 sf_command ((SNDFILE*) psf, SFC_SET_NORM_DOUBLE, NULL, save_state) ;
3847 return 0 ;
3848 } /* psf_calc_signal_max */
3851 ** Do not edit or modify anything in this comment block.
3852 ** The arch-tag line is a file identity tag for the GNU Arch
3853 ** revision control system.
3855 ** arch-tag: 0aae0d9d-ab2b-4d70-ade3-47a534666f8e
3858 ** Copyright (C) 1999-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
3860 ** This program is free software; you can redistribute it and/or modify
3861 ** it under the terms of the GNU Lesser General Public License as published by
3862 ** the Free Software Foundation; either version 2.1 of the License, or
3863 ** (at your option) any later version.
3865 ** This program is distributed in the hope that it will be useful,
3866 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
3867 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3868 ** GNU Lesser General Public License for more details.
3870 ** You should have received a copy of the GNU Lesser General Public License
3871 ** along with this program; if not, write to the Free Software
3872 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
3875 #include <stdarg.h>
3876 #include <string.h>
3877 #include <ctype.h>
3878 #include <math.h>
3879 #include <time.h>
3882 /*-----------------------------------------------------------------------------------------------
3883 ** psf_log_printf allows libsndfile internal functions to print to an internal logbuffer which
3884 ** can later be displayed.
3885 ** The format specifiers are as for printf but without the field width and other modifiers.
3886 ** Printing is performed to the logbuffer char array of the SF_PRIVATE struct.
3887 ** Printing is done in such a way as to guarantee that the log never overflows the end of the
3888 ** logbuffer array.
3891 #define LOG_PUTCHAR(a,b) \
3892 { if ((a)->logindex < SIGNED_SIZEOF ((a)->logbuffer) - 1)\
3893 { (a)->logbuffer [(a)->logindex++] = (b) ; \
3894 (a)->logbuffer [(a)->logindex] = 0 ; \
3898 void
3899 psf_log_printf (SF_PRIVATE *psf, const char *format, ...)
3900 { va_list ap ;
3901 unsigned int u ;
3902 int d, tens, shift, width, width_specifier, left_align ;
3903 char c, *strptr, istr [5], lead_char, sign_char ;
3905 va_start (ap, format) ;
3907 while ((c = *format++))
3908 { if (c != '%')
3909 { LOG_PUTCHAR (psf, c) ;
3910 continue ;
3913 if (format [0] == '%') /* Handle %% */
3914 { LOG_PUTCHAR (psf, '%') ;
3915 format ++ ;
3916 continue ;
3919 sign_char = 0 ;
3920 left_align = SF_FALSE ;
3921 while (1)
3922 { switch (format [0])
3923 { case ' ' :
3924 case '+' :
3925 sign_char = format [0] ;
3926 format ++ ;
3927 continue ;
3929 case '-' :
3930 left_align = SF_TRUE ;
3931 format ++ ;
3932 continue ;
3934 default : break ;
3937 break ;
3940 if (format [0] == 0)
3941 break ;
3943 lead_char = ' ' ;
3944 if (format [0] == '0')
3945 lead_char = '0' ;
3947 width_specifier = 0 ;
3948 while ((c = *format++) && isdigit (c))
3949 width_specifier = width_specifier * 10 + (c - '0') ;
3951 switch (c)
3952 { case 0 : /* NULL character. */
3953 va_end (ap) ;
3954 return ;
3956 case 's': /* string */
3957 strptr = va_arg (ap, char *) ;
3958 if (strptr == NULL)
3959 break ;
3960 width_specifier -= strlen (strptr) ;
3961 if (left_align == SF_FALSE)
3962 while (width_specifier -- > 0)
3963 LOG_PUTCHAR (psf, ' ') ;
3964 while (*strptr)
3965 LOG_PUTCHAR (psf, *strptr++) ;
3966 while (width_specifier -- > 0)
3967 LOG_PUTCHAR (psf, ' ') ;
3968 break ;
3970 case 'd': /* int */
3971 d = va_arg (ap, int) ;
3973 if (d < 0)
3974 { d = -d ;
3975 sign_char = '-' ;
3976 if (lead_char != '0' && left_align == SF_FALSE)
3977 width_specifier -- ;
3980 tens = 1 ;
3981 width = 1 ;
3982 while (d / tens >= 10)
3983 { tens *= 10 ;
3984 width ++ ;
3987 width_specifier -= width ;
3989 if (sign_char == ' ')
3990 { LOG_PUTCHAR (psf, ' ') ;
3991 width_specifier -- ;
3994 if (left_align == SF_FALSE && lead_char != '0')
3995 { if (sign_char == '+')
3996 width_specifier -- ;
3998 while (width_specifier -- > 0)
3999 LOG_PUTCHAR (psf, lead_char) ;
4002 if (sign_char == '+' || sign_char == '-')
4003 { LOG_PUTCHAR (psf, sign_char) ;
4004 width_specifier -- ;
4007 if (left_align == SF_FALSE)
4008 while (width_specifier -- > 0)
4009 LOG_PUTCHAR (psf, lead_char) ;
4011 while (tens > 0)
4012 { LOG_PUTCHAR (psf, '0' + d / tens) ;
4013 d %= tens ;
4014 tens /= 10 ;
4017 while (width_specifier -- > 0)
4018 LOG_PUTCHAR (psf, lead_char) ;
4019 break ;
4021 case 'D': /* sf_count_t */
4022 { sf_count_t D, Tens ;
4024 D = va_arg (ap, sf_count_t) ;
4026 if (D == 0)
4027 { while (-- width_specifier > 0)
4028 LOG_PUTCHAR (psf, lead_char) ;
4029 LOG_PUTCHAR (psf, '0') ;
4030 break ;
4032 if (D < 0)
4033 { LOG_PUTCHAR (psf, '-') ;
4034 D = -D ;
4036 Tens = 1 ;
4037 width = 1 ;
4038 while (D / Tens >= 10)
4039 { Tens *= 10 ;
4040 width ++ ;
4043 while (width_specifier > width)
4044 { LOG_PUTCHAR (psf, lead_char) ;
4045 width_specifier-- ;
4048 while (Tens > 0)
4049 { LOG_PUTCHAR (psf, '0' + D / Tens) ;
4050 D %= Tens ;
4051 Tens /= 10 ;
4054 break ;
4056 case 'u': /* unsigned int */
4057 u = va_arg (ap, unsigned int) ;
4059 tens = 1 ;
4060 width = 1 ;
4061 while (u / tens >= 10)
4062 { tens *= 10 ;
4063 width ++ ;
4066 width_specifier -= width ;
4068 if (sign_char == ' ')
4069 { LOG_PUTCHAR (psf, ' ') ;
4070 width_specifier -- ;
4073 if (left_align == SF_FALSE && lead_char != '0')
4074 { if (sign_char == '+')
4075 width_specifier -- ;
4077 while (width_specifier -- > 0)
4078 LOG_PUTCHAR (psf, lead_char) ;
4081 if (sign_char == '+' || sign_char == '-')
4082 { LOG_PUTCHAR (psf, sign_char) ;
4083 width_specifier -- ;
4086 if (left_align == SF_FALSE)
4087 while (width_specifier -- > 0)
4088 LOG_PUTCHAR (psf, lead_char) ;
4090 while (tens > 0)
4091 { LOG_PUTCHAR (psf, '0' + u / tens) ;
4092 u %= tens ;
4093 tens /= 10 ;
4096 while (width_specifier -- > 0)
4097 LOG_PUTCHAR (psf, lead_char) ;
4098 break ;
4100 case 'c': /* char */
4101 c = va_arg (ap, int) & 0xFF ;
4102 LOG_PUTCHAR (psf, c) ;
4103 break ;
4105 case 'X': /* hex */
4106 d = va_arg (ap, int) ;
4108 if (d == 0)
4109 { while (--width_specifier > 0)
4110 LOG_PUTCHAR (psf, lead_char) ;
4111 LOG_PUTCHAR (psf, '0') ;
4112 break ;
4114 shift = 28 ;
4115 width = (width_specifier < 8) ? 8 : width_specifier ;
4116 while (! ((0xF << shift) & d))
4117 { shift -= 4 ;
4118 width -- ;
4121 while (width > 0 && width_specifier > width)
4122 { LOG_PUTCHAR (psf, lead_char) ;
4123 width_specifier-- ;
4126 while (shift >= 0)
4127 { c = (d >> shift) & 0xF ;
4128 LOG_PUTCHAR (psf, (c > 9) ? c + 'A' - 10 : c + '0') ;
4129 shift -= 4 ;
4131 break ;
4133 case 'M': /* int2str */
4134 d = va_arg (ap, int) ;
4135 if (CPU_IS_LITTLE_ENDIAN)
4136 { istr [0] = d & 0xFF ;
4137 istr [1] = (d >> 8) & 0xFF ;
4138 istr [2] = (d >> 16) & 0xFF ;
4139 istr [3] = (d >> 24) & 0xFF ;
4141 else
4142 { istr [3] = d & 0xFF ;
4143 istr [2] = (d >> 8) & 0xFF ;
4144 istr [1] = (d >> 16) & 0xFF ;
4145 istr [0] = (d >> 24) & 0xFF ;
4147 istr [4] = 0 ;
4148 strptr = istr ;
4149 while (*strptr)
4150 { c = *strptr++ ;
4151 LOG_PUTCHAR (psf, c) ;
4153 break ;
4155 default :
4156 LOG_PUTCHAR (psf, '*') ;
4157 LOG_PUTCHAR (psf, c) ;
4158 LOG_PUTCHAR (psf, '*') ;
4159 break ;
4160 } /* switch */
4161 } /* while */
4163 va_end (ap) ;
4164 return ;
4165 } /* psf_log_printf */
4167 #ifndef PSF_LOG_PRINTF_ONLY
4168 /*-----------------------------------------------------------------------------------------------
4169 ** ASCII header printf functions.
4170 ** Some formats (ie NIST) use ascii text in their headers.
4171 ** Format specifiers are the same as the standard printf specifiers (uses vsnprintf).
4172 ** If this generates a compile error on any system, the author should be notified
4173 ** so an alternative vsnprintf can be provided.
4176 void
4177 psf_asciiheader_printf (SF_PRIVATE *psf, const char *format, ...)
4178 { va_list argptr ;
4179 int maxlen ;
4180 char *start ;
4182 maxlen = strlen ((char*) psf->header) ;
4183 start = ((char*) psf->header) + maxlen ;
4184 maxlen = sizeof (psf->header) - maxlen ;
4186 va_start (argptr, format) ;
4187 LSF_VSNPRINTF (start, maxlen, format, argptr) ;
4188 va_end (argptr) ;
4190 /* Make sure the string is properly terminated. */
4191 start [maxlen - 1] = 0 ;
4193 psf->headindex = strlen ((char*) psf->header) ;
4195 return ;
4196 } /* psf_asciiheader_printf */
4198 /*-----------------------------------------------------------------------------------------------
4199 ** Binary header writing functions. Returns number of bytes written.
4201 ** Format specifiers for psf_binheader_writef are as follows
4202 ** m - marker - four bytes - no endian manipulation
4204 ** e - all following numerical values will be little endian
4205 ** E - all following numerical values will be big endian
4207 ** t - all following O types will be truncated to 4 bytes
4208 ** T - switch off truncation of all following O types
4210 ** 1 - single byte value
4211 ** 2 - two byte value
4212 ** 3 - three byte value
4213 ** 4 - four byte value
4214 ** 8 - eight byte value (sometimes written as 4 bytes)
4216 ** s - string preceded by a four byte length
4217 ** S - string including null terminator
4218 ** f - floating point data
4219 ** d - double precision floating point data
4220 ** h - 16 binary bytes value
4222 ** b - binary data (see below)
4223 ** z - zero bytes (ses below)
4224 ** j - jump forwards or backwards
4226 ** To write a word followed by an int (both little endian) use:
4227 ** psf_binheader_writef ("e24", wordval, longval) ;
4229 ** To write binary data use:
4230 ** psf_binheader_writef ("b", &bindata, sizeof (bindata)) ;
4232 ** To write N zero bytes use:
4233 ** psf_binheader_writef ("z", N) ;
4236 /* These macros may seem a bit messy but do prevent problems with processors which
4237 ** seg. fault when asked to write an int or short to a non-int/short aligned address.
4240 #define PUT_BYTE(psf,x) if ((psf)->headindex < SIGNED_SIZEOF ((psf)->header) - 1) \
4241 { (psf)->header [(psf)->headindex++] = (x) ; }
4243 #if (CPU_IS_BIG_ENDIAN == 1)
4244 #define PUT_MARKER(psf,x) if ((psf)->headindex < SIGNED_SIZEOF ((psf)->header) - 4) \
4245 { (psf)->header [(psf)->headindex++] = ((x) >> 24) ; \
4246 (psf)->header [(psf)->headindex++] = ((x) >> 16) ; \
4247 (psf)->header [(psf)->headindex++] = ((x) >> 8) ; \
4248 (psf)->header [(psf)->headindex++] = (x) ; }
4250 #elif (CPU_IS_LITTLE_ENDIAN == 1)
4251 #define PUT_MARKER(psf,x) if ((psf)->headindex < SIGNED_SIZEOF ((psf)->header) - 4) \
4252 { (psf)->header [(psf)->headindex++] = (x) ; \
4253 (psf)->header [(psf)->headindex++] = ((x) >> 8) ; \
4254 (psf)->header [(psf)->headindex++] = ((x) >> 16) ; \
4255 (psf)->header [(psf)->headindex++] = ((x) >> 24) ; }
4257 #else
4258 # error "Cannot determine endian-ness of processor."
4259 #endif
4262 #define PUT_BE_SHORT(psf,x) if ((psf)->headindex < SIGNED_SIZEOF ((psf)->header) - 2) \
4263 { (psf)->header [(psf)->headindex++] = ((x) >> 8) ; \
4264 (psf)->header [(psf)->headindex++] = (x) ; }
4266 #define PUT_LE_SHORT(psf,x) if ((psf)->headindex < SIGNED_SIZEOF ((psf)->header) - 2) \
4267 { (psf)->header [(psf)->headindex++] = (x) ; \
4268 (psf)->header [(psf)->headindex++] = ((x) >> 8) ; }
4270 #define PUT_BE_3BYTE(psf,x) if ((psf)->headindex < SIGNED_SIZEOF ((psf)->header) - 3) \
4271 { (psf)->header [(psf)->headindex++] = ((x) >> 16) ; \
4272 (psf)->header [(psf)->headindex++] = ((x) >> 8) ; \
4273 (psf)->header [(psf)->headindex++] = (x) ; }
4275 #define PUT_LE_3BYTE(psf,x) if ((psf)->headindex < SIGNED_SIZEOF ((psf)->header) - 3) \
4276 { (psf)->header [(psf)->headindex++] = (x) ; \
4277 (psf)->header [(psf)->headindex++] = ((x) >> 8) ; \
4278 (psf)->header [(psf)->headindex++] = ((x) >> 16) ; }
4280 #define PUT_BE_INT(psf,x) if ((psf)->headindex < SIGNED_SIZEOF ((psf)->header) - 4) \
4281 { (psf)->header [(psf)->headindex++] = ((x) >> 24) ; \
4282 (psf)->header [(psf)->headindex++] = ((x) >> 16) ; \
4283 (psf)->header [(psf)->headindex++] = ((x) >> 8) ; \
4284 (psf)->header [(psf)->headindex++] = (x) ; }
4286 #define PUT_LE_INT(psf,x) if ((psf)->headindex < SIGNED_SIZEOF ((psf)->header) - 4) \
4287 { (psf)->header [(psf)->headindex++] = (x) ; \
4288 (psf)->header [(psf)->headindex++] = ((x) >> 8) ; \
4289 (psf)->header [(psf)->headindex++] = ((x) >> 16) ; \
4290 (psf)->header [(psf)->headindex++] = ((x) >> 24) ; }
4292 #if (SIZEOF_SF_COUNT_T == 4)
4293 #define PUT_BE_8BYTE(psf,x) if ((psf)->headindex < SIGNED_SIZEOF ((psf)->header) - 8) \
4294 { (psf)->header [(psf)->headindex++] = 0 ; \
4295 (psf)->header [(psf)->headindex++] = 0 ; \
4296 (psf)->header [(psf)->headindex++] = 0 ; \
4297 (psf)->header [(psf)->headindex++] = 0 ; \
4298 (psf)->header [(psf)->headindex++] = ((x) >> 24) ; \
4299 (psf)->header [(psf)->headindex++] = ((x) >> 16) ; \
4300 (psf)->header [(psf)->headindex++] = ((x) >> 8) ; \
4301 (psf)->header [(psf)->headindex++] = (x) ; }
4303 #define PUT_LE_8BYTE(psf,x) if ((psf)->headindex < SIGNED_SIZEOF ((psf)->header) - 8) \
4304 { (psf)->header [(psf)->headindex++] = (x) ; \
4305 (psf)->header [(psf)->headindex++] = ((x) >> 8) ; \
4306 (psf)->header [(psf)->headindex++] = ((x) >> 16) ; \
4307 (psf)->header [(psf)->headindex++] = ((x) >> 24) ; \
4308 (psf)->header [(psf)->headindex++] = 0 ; \
4309 (psf)->header [(psf)->headindex++] = 0 ; \
4310 (psf)->header [(psf)->headindex++] = 0 ; \
4311 (psf)->header [(psf)->headindex++] = 0 ; }
4313 #elif (SIZEOF_SF_COUNT_T == 8)
4314 #define PUT_BE_8BYTE(psf,x) if ((psf)->headindex < SIGNED_SIZEOF ((psf)->header) - 8) \
4315 { (psf)->header [(psf)->headindex++] = ((x) >> 56) ; \
4316 (psf)->header [(psf)->headindex++] = ((x) >> 48) ; \
4317 (psf)->header [(psf)->headindex++] = ((x) >> 40) ; \
4318 (psf)->header [(psf)->headindex++] = ((x) >> 32) ; \
4319 (psf)->header [(psf)->headindex++] = ((x) >> 24) ; \
4320 (psf)->header [(psf)->headindex++] = ((x) >> 16) ; \
4321 (psf)->header [(psf)->headindex++] = ((x) >> 8) ; \
4322 (psf)->header [(psf)->headindex++] = (x) ; }
4324 #define PUT_LE_8BYTE(psf,x) if ((psf)->headindex < SIGNED_SIZEOF ((psf)->header) - 8) \
4325 { (psf)->header [(psf)->headindex++] = (x) ; \
4326 (psf)->header [(psf)->headindex++] = ((x) >> 8) ; \
4327 (psf)->header [(psf)->headindex++] = ((x) >> 16) ; \
4328 (psf)->header [(psf)->headindex++] = ((x) >> 24) ; \
4329 (psf)->header [(psf)->headindex++] = ((x) >> 32) ; \
4330 (psf)->header [(psf)->headindex++] = ((x) >> 40) ; \
4331 (psf)->header [(psf)->headindex++] = ((x) >> 48) ; \
4332 (psf)->header [(psf)->headindex++] = ((x) >> 56) ; }
4333 #else
4334 #error "SIZEOF_SF_COUNT_T is not defined."
4335 #endif
4338 psf_binheader_writef (SF_PRIVATE *psf, const char *format, ...)
4339 { va_list argptr ;
4340 sf_count_t countdata ;
4341 unsigned long longdata ;
4342 unsigned int data ;
4343 float floatdata ;
4344 double doubledata ;
4345 void *bindata ;
4346 size_t size ;
4347 char c, *strptr ;
4348 int count = 0, trunc_8to4 ;
4350 trunc_8to4 = SF_FALSE ;
4352 va_start (argptr, format) ;
4354 while ((c = *format++))
4355 { switch (c)
4356 { case 'e' : /* All conversions are now from LE to host. */
4357 psf->rwf_endian = SF_ENDIAN_LITTLE ;
4358 break ;
4360 case 'E' : /* All conversions are now from BE to host. */
4361 psf->rwf_endian = SF_ENDIAN_BIG ;
4362 break ;
4364 case 't' : /* All 8 byte values now get written as 4 bytes. */
4365 trunc_8to4 = SF_TRUE ;
4366 break ;
4368 case 'T' : /* All 8 byte values now get written as 8 bytes. */
4369 trunc_8to4 = SF_FALSE ;
4370 break ;
4372 case 'm' :
4373 data = va_arg (argptr, unsigned int) ;
4374 PUT_MARKER (psf, data) ;
4375 count += 4 ;
4376 break ;
4378 case '1' :
4379 data = va_arg (argptr, unsigned int) ;
4380 PUT_BYTE (psf, data) ;
4381 count += 1 ;
4382 break ;
4384 case '2' :
4385 data = va_arg (argptr, unsigned int) ;
4386 if (psf->rwf_endian == SF_ENDIAN_BIG)
4387 { PUT_BE_SHORT (psf, data) ;
4389 else
4390 { PUT_LE_SHORT (psf, data) ;
4392 count += 2 ;
4393 break ;
4395 case '3' : /* tribyte */
4396 data = va_arg (argptr, unsigned int) ;
4397 if (psf->rwf_endian == SF_ENDIAN_BIG)
4398 { PUT_BE_3BYTE (psf, data) ;
4400 else
4401 { PUT_LE_3BYTE (psf, data) ;
4403 count += 3 ;
4404 break ;
4406 case '4' :
4407 data = va_arg (argptr, unsigned int) ;
4408 if (psf->rwf_endian == SF_ENDIAN_BIG)
4409 { PUT_BE_INT (psf, data) ;
4411 else
4412 { PUT_LE_INT (psf, data) ;
4414 count += 4 ;
4415 break ;
4417 case '8' :
4418 countdata = va_arg (argptr, sf_count_t) ;
4419 if (psf->rwf_endian == SF_ENDIAN_BIG && trunc_8to4 == SF_FALSE)
4420 { PUT_BE_8BYTE (psf, countdata) ;
4421 count += 8 ;
4423 else if (psf->rwf_endian == SF_ENDIAN_LITTLE && trunc_8to4 == SF_FALSE)
4424 { PUT_LE_8BYTE (psf, countdata) ;
4425 count += 8 ;
4427 else if (psf->rwf_endian == SF_ENDIAN_BIG && trunc_8to4 == SF_TRUE)
4428 { longdata = countdata & 0xFFFFFFFF ;
4429 PUT_BE_INT (psf, longdata) ;
4430 count += 4 ;
4432 else if (psf->rwf_endian == SF_ENDIAN_LITTLE && trunc_8to4 == SF_TRUE)
4433 { longdata = countdata & 0xFFFFFFFF ;
4434 PUT_LE_INT (psf, longdata) ;
4435 count += 4 ;
4437 break ;
4439 case 'f' :
4440 /* Floats are passed as doubles. Is this always true? */
4441 floatdata = (float) va_arg (argptr, double) ;
4442 if (psf->rwf_endian == SF_ENDIAN_BIG)
4443 float32_be_write (floatdata, psf->header + psf->headindex) ;
4444 else
4445 float32_le_write (floatdata, psf->header + psf->headindex) ;
4446 psf->headindex += 4 ;
4447 count += 4 ;
4448 break ;
4450 case 'd' :
4451 doubledata = va_arg (argptr, double) ;
4452 if (psf->rwf_endian == SF_ENDIAN_BIG)
4453 double64_be_write (doubledata, psf->header + psf->headindex) ;
4454 else
4455 double64_le_write (doubledata, psf->header + psf->headindex) ;
4456 psf->headindex += 8 ;
4457 count += 8 ;
4458 break ;
4460 case 's' :
4461 strptr = va_arg (argptr, char *) ;
4462 size = strlen (strptr) + 1 ;
4463 size += (size & 1) ;
4464 if (psf->rwf_endian == SF_ENDIAN_BIG)
4465 { PUT_BE_INT (psf, size) ;
4467 else
4468 { PUT_LE_INT (psf, size) ;
4470 memcpy (&(psf->header [psf->headindex]), strptr, size) ;
4471 psf->headindex += size ;
4472 psf->header [psf->headindex - 1] = 0 ;
4473 count += 4 + size ;
4474 break ;
4476 case 'S' :
4477 strptr = va_arg (argptr, char *) ;
4478 size = strlen (strptr) + 1 ;
4479 memcpy (&(psf->header [psf->headindex]), strptr, size) ;
4480 psf->headindex += size ;
4481 count += size ;
4482 break ;
4484 case 'b' :
4485 bindata = va_arg (argptr, void *) ;
4486 size = va_arg (argptr, size_t) ;
4487 memcpy (&(psf->header [psf->headindex]), bindata, size) ;
4488 psf->headindex += size ;
4489 count += size ;
4490 break ;
4492 case 'z' :
4493 size = va_arg (argptr, size_t) ;
4494 count += size ;
4495 while (size)
4496 { psf->header [psf->headindex] = 0 ;
4497 psf->headindex ++ ;
4498 size -- ;
4500 break ;
4502 case 'h' :
4503 bindata = va_arg (argptr, void *) ;
4504 memcpy (&(psf->header [psf->headindex]), bindata, 16) ;
4505 psf->headindex += 16 ;
4506 count += 16 ;
4507 break ;
4509 case 'j' :
4510 size = va_arg (argptr, int) ;
4511 psf->headindex += size ;
4512 count = size ;
4513 break ;
4515 default :
4516 psf_log_printf (psf, "*** Invalid format specifier `%c'\n", c) ;
4517 psf->error = SFE_INTERNAL ;
4518 break ;
4522 va_end (argptr) ;
4523 return count ;
4524 } /* psf_binheader_writef */
4526 /*-----------------------------------------------------------------------------------------------
4527 ** Binary header reading functions. Returns number of bytes read.
4529 ** Format specifiers are the same as for header write function above with the following
4530 ** additions:
4532 ** p - jump a given number of position from start of file.
4534 ** If format is NULL, psf_binheader_readf returns the current offset.
4537 #if (CPU_IS_BIG_ENDIAN == 1)
4538 #define GET_MARKER(ptr) ( ((ptr) [0] << 24) | ((ptr) [1] << 16) | \
4539 ((ptr) [2] << 8) | ((ptr) [3]) )
4541 #elif (CPU_IS_LITTLE_ENDIAN == 1)
4542 #define GET_MARKER(ptr) ( ((ptr) [0]) | ((ptr) [1] << 8) | \
4543 ((ptr) [2] << 16) | ((ptr) [3] << 24) )
4545 #else
4546 # error "Cannot determine endian-ness of processor."
4547 #endif
4549 #define GET_LE_SHORT(ptr) ( ((ptr) [1] << 8) | ((ptr) [0]) )
4550 #define GET_BE_SHORT(ptr) ( ((ptr) [0] << 8) | ((ptr) [1]) )
4552 #define GET_LE_3BYTE(ptr) ( ((ptr) [2] << 16) | ((ptr) [1] << 8) | ((ptr) [0]) )
4553 #define GET_BE_3BYTE(ptr) ( ((ptr) [0] << 16) | ((ptr) [1] << 8) | ((ptr) [2]) )
4555 #define GET_LE_INT(ptr) ( ((ptr) [3] << 24) | ((ptr) [2] << 16) | \
4556 ((ptr) [1] << 8) | ((ptr) [0]) )
4558 #define GET_BE_INT(ptr) ( ((ptr) [0] << 24) | ((ptr) [1] << 16) | \
4559 ((ptr) [2] << 8) | ((ptr) [3]) )
4561 #if (SIZEOF_LONG == 4)
4562 #define GET_LE_8BYTE(ptr) ( ((ptr) [3] << 24) | ((ptr) [2] << 16) | \
4563 ((ptr) [1] << 8) | ((ptr) [0]) )
4565 #define GET_BE_8BYTE(ptr) ( ((ptr) [4] << 24) | ((ptr) [5] << 16) | \
4566 ((ptr) [6] << 8) | ((ptr) [7]) )
4567 #else
4568 #define GET_LE_8BYTE(ptr) ( (((ptr) [7] * 1L) << 56) | (((ptr) [6] * 1L) << 48) | \
4569 (((ptr) [5] * 1L) << 40) | (((ptr) [4] * 1L) << 32) | \
4570 (((ptr) [3] * 1L) << 24) | (((ptr) [2] * 1L) << 16) | \
4571 (((ptr) [1] * 1L) << 8 ) | ((ptr) [0]))
4573 #define GET_BE_8BYTE(ptr) ( (((ptr) [0] * 1L) << 56) | (((ptr) [1] * 1L) << 48) | \
4574 (((ptr) [2] * 1L) << 40) | (((ptr) [3] * 1L) << 32) | \
4575 (((ptr) [4] * 1L) << 24) | (((ptr) [5] * 1L) << 16) | \
4576 (((ptr) [6] * 1L) << 8 ) | ((ptr) [7]))
4578 #endif
4580 static int
4581 header_read (SF_PRIVATE *psf, void *ptr, int bytes)
4582 { int count = 0 ;
4584 if (psf->headindex + bytes > SIGNED_SIZEOF (psf->header))
4585 { if (psf->headend < SIGNED_SIZEOF (psf->header))
4586 psf_log_printf (psf, "Warning : Further header read would overflow buffer.\n") ;
4587 psf->headend = SIGNED_SIZEOF (psf->header) ;
4589 /* This is the best that we can do. */
4590 return psf_fread (ptr, 1, bytes, psf) ;
4593 if (psf->headindex + bytes > psf->headend)
4594 { count = psf_fread (psf->header + psf->headend, 1, bytes - (psf->headend - psf->headindex), psf) ;
4595 if (count != bytes - (int) (psf->headend - psf->headindex))
4596 { psf_log_printf (psf, "Error : psf_fread returned short count.\n") ;
4597 return 0 ;
4599 psf->headend += count ;
4602 memcpy (ptr, psf->header + psf->headindex, bytes) ;
4603 psf->headindex += bytes ;
4605 return bytes ;
4606 } /* header_read */
4608 static void
4609 header_seek (SF_PRIVATE *psf, sf_count_t position, int whence)
4612 switch (whence)
4613 { case SEEK_SET :
4614 if (position > SIGNED_SIZEOF (psf->header))
4615 { /* Too much header to cache so just seek instead. */
4616 psf_fseek (psf, position, whence) ;
4617 return ;
4619 if (position > psf->headend)
4620 psf->headend += psf_fread (psf->header + psf->headend, 1, position - psf->headend, psf) ;
4621 psf->headindex = position ;
4622 break ;
4624 case SEEK_CUR :
4625 if (psf->headindex + position < 0)
4626 break ;
4628 if (psf->headindex >= SIGNED_SIZEOF (psf->header))
4629 { psf_fseek (psf, position, whence) ;
4630 return ;
4633 if (psf->headindex + position <= psf->headend)
4634 { psf->headindex += position ;
4635 break ;
4638 if (psf->headindex + position > SIGNED_SIZEOF (psf->header))
4639 { /* Need to jump this without caching it. */
4640 psf->headindex = psf->headend ;
4641 psf_fseek (psf, position, SEEK_CUR) ;
4642 break ;
4645 psf->headend += psf_fread (psf->header + psf->headend, 1, position - (psf->headend - psf->headindex), psf) ;
4646 psf->headindex = psf->headend ;
4647 break ;
4649 case SEEK_END :
4650 default :
4651 psf_log_printf (psf, "Bad whence param in header_seek().\n") ;
4652 break ;
4655 return ;
4656 } /* header_seek */
4658 static int
4659 header_gets (SF_PRIVATE *psf, char *ptr, int bufsize)
4661 int k ;
4663 for (k = 0 ; k < bufsize - 1 ; k++)
4664 { if (psf->headindex < psf->headend)
4665 { ptr [k] = psf->header [psf->headindex] ;
4666 psf->headindex ++ ;
4668 else
4669 { psf->headend += psf_fread (psf->header + psf->headend, 1, 1, psf) ;
4670 ptr [k] = psf->header [psf->headindex] ;
4671 psf->headindex = psf->headend ;
4674 if (ptr [k] == '\n')
4675 break ;
4678 ptr [k] = 0 ;
4680 return k ;
4681 } /* header_gets */
4684 psf_binheader_readf (SF_PRIVATE *psf, char const *format, ...)
4685 { va_list argptr ;
4686 sf_count_t *countptr, countdata ;
4687 unsigned char *ucptr, sixteen_bytes [16] ;
4688 unsigned int *intptr, intdata ;
4689 unsigned short *shortptr ;
4690 char *charptr ;
4691 float *floatptr ;
4692 double *doubleptr ;
4693 char c ;
4694 int byte_count = 0, count ;
4696 if (! format)
4697 return psf_ftell (psf) ;
4699 va_start (argptr, format) ;
4701 while ((c = *format++))
4702 { switch (c)
4703 { case 'e' : /* All conversions are now from LE to host. */
4704 psf->rwf_endian = SF_ENDIAN_LITTLE ;
4705 break ;
4707 case 'E' : /* All conversions are now from BE to host. */
4708 psf->rwf_endian = SF_ENDIAN_BIG ;
4709 break ;
4711 case 'm' :
4712 intptr = va_arg (argptr, unsigned int*) ;
4713 ucptr = (unsigned char*) intptr ;
4714 byte_count += header_read (psf, ucptr, sizeof (int)) ;
4715 *intptr = GET_MARKER (ucptr) ;
4716 break ;
4718 case 'h' :
4719 intptr = va_arg (argptr, unsigned int*) ;
4720 ucptr = (unsigned char*) intptr ;
4721 byte_count += header_read (psf, sixteen_bytes, sizeof (sixteen_bytes)) ;
4722 { int k ;
4723 intdata = 0 ;
4724 for (k = 0 ; k < 16 ; k++)
4725 intdata ^= sixteen_bytes [k] << k ;
4727 *intptr = intdata ;
4728 break ;
4730 case '1' :
4731 charptr = va_arg (argptr, char*) ;
4732 byte_count += header_read (psf, charptr, sizeof (char)) ;
4733 break ;
4735 case '2' :
4736 shortptr = va_arg (argptr, unsigned short*) ;
4737 ucptr = (unsigned char*) shortptr ;
4738 byte_count += header_read (psf, ucptr, sizeof (short)) ;
4739 if (psf->rwf_endian == SF_ENDIAN_BIG)
4740 *shortptr = GET_BE_SHORT (ucptr) ;
4741 else
4742 *shortptr = GET_LE_SHORT (ucptr) ;
4743 break ;
4745 case '3' :
4746 intptr = va_arg (argptr, unsigned int*) ;
4747 byte_count += header_read (psf, sixteen_bytes, 3) ;
4748 if (psf->rwf_endian == SF_ENDIAN_BIG)
4749 *intptr = GET_BE_3BYTE (sixteen_bytes) ;
4750 else
4751 *intptr = GET_LE_3BYTE (sixteen_bytes) ;
4752 break ;
4754 case '4' :
4755 intptr = va_arg (argptr, unsigned int*) ;
4756 ucptr = (unsigned char*) intptr ;
4757 byte_count += header_read (psf, ucptr, sizeof (int)) ;
4758 if (psf->rwf_endian == SF_ENDIAN_BIG)
4759 *intptr = GET_BE_INT (ucptr) ;
4760 else
4761 *intptr = GET_LE_INT (ucptr) ;
4762 break ;
4764 case '8' :
4765 countptr = va_arg (argptr, sf_count_t*) ;
4766 byte_count += header_read (psf, sixteen_bytes, 8) ;
4767 if (psf->rwf_endian == SF_ENDIAN_BIG)
4768 countdata = GET_BE_8BYTE (sixteen_bytes) ;
4769 else
4770 countdata = GET_LE_8BYTE (sixteen_bytes) ;
4771 *countptr = countdata ;
4772 break ;
4774 case 'f' : /* Float conversion */
4775 floatptr = va_arg (argptr, float *) ;
4776 *floatptr = 0.0 ;
4777 byte_count += header_read (psf, floatptr, sizeof (float)) ;
4778 if (psf->rwf_endian == SF_ENDIAN_BIG)
4779 *floatptr = float32_be_read ((unsigned char*) floatptr) ;
4780 else
4781 *floatptr = float32_le_read ((unsigned char*) floatptr) ;
4782 break ;
4784 case 'd' : /* double conversion */
4785 doubleptr = va_arg (argptr, double *) ;
4786 *doubleptr = 0.0 ;
4787 byte_count += header_read (psf, doubleptr, sizeof (double)) ;
4788 if (psf->rwf_endian == SF_ENDIAN_BIG)
4789 *doubleptr = double64_be_read ((unsigned char*) doubleptr) ;
4790 else
4791 *doubleptr = double64_le_read ((unsigned char*) doubleptr) ;
4792 break ;
4794 case 's' :
4795 psf_log_printf (psf, "Format conversion 's' not implemented yet.\n") ;
4797 strptr = va_arg (argptr, char *) ;
4798 size = strlen (strptr) + 1 ;
4799 size += (size & 1) ;
4800 longdata = H2LE_INT (size) ;
4801 get_int (psf, longdata) ;
4802 memcpy (&(psf->header [psf->headindex]), strptr, size) ;
4803 psf->headindex += size ;
4805 break ;
4807 case 'b' :
4808 charptr = va_arg (argptr, char*) ;
4809 count = va_arg (argptr, int) ;
4810 if (count > 0)
4811 byte_count += header_read (psf, charptr, count) ;
4812 break ;
4814 case 'G' :
4815 charptr = va_arg (argptr, char*) ;
4816 count = va_arg (argptr, int) ;
4817 if (count > 0)
4818 byte_count += header_gets (psf, charptr, count) ;
4819 break ;
4821 case 'z' :
4822 psf_log_printf (psf, "Format conversion 'z' not implemented yet.\n") ;
4824 size = va_arg (argptr, size_t) ;
4825 while (size)
4826 { psf->header [psf->headindex] = 0 ;
4827 psf->headindex ++ ;
4828 size -- ;
4831 break ;
4833 case 'p' :
4834 /* Get the seek position first. */
4835 count = va_arg (argptr, int) ;
4836 header_seek (psf, count, SEEK_SET) ;
4837 byte_count = count ;
4838 break ;
4840 case 'j' :
4841 /* Get the seek position first. */
4842 count = va_arg (argptr, int) ;
4843 header_seek (psf, count, SEEK_CUR) ;
4844 byte_count += count ;
4845 break ;
4847 default :
4848 psf_log_printf (psf, "*** Invalid format specifier `%c'\n", c) ;
4849 psf->error = SFE_INTERNAL ;
4850 break ;
4854 va_end (argptr) ;
4856 return byte_count ;
4857 } /* psf_binheader_readf */
4859 /*-----------------------------------------------------------------------------------------------
4862 sf_count_t
4863 psf_default_seek (SF_PRIVATE *psf, int mode, sf_count_t samples_from_start)
4864 { sf_count_t position, retval ;
4866 if (! (psf->blockwidth && psf->dataoffset >= 0))
4867 { psf->error = SFE_BAD_SEEK ;
4868 return ((sf_count_t) -1) ;
4871 if (! psf->sf.seekable)
4872 { psf->error = SFE_NOT_SEEKABLE ;
4873 return ((sf_count_t) -1) ;
4876 position = psf->dataoffset + psf->blockwidth * samples_from_start ;
4878 if ((retval = psf_fseek (psf, position, SEEK_SET)) != position)
4879 { psf->error = SFE_SEEK_FAILED ;
4880 return ((sf_count_t) -1) ;
4883 mode = mode ;
4885 return samples_from_start ;
4886 } /* psf_default_seek */
4888 /*-----------------------------------------------------------------------------------------------
4891 void
4892 psf_hexdump (void *ptr, int len)
4893 { char ascii [17], *data ;
4894 int k, m ;
4896 if ((data = ptr) == NULL)
4897 return ;
4898 if (len <= 0)
4899 return ;
4901 puts ("") ;
4902 for (k = 0 ; k < len ; k += 16)
4903 { memset (ascii, ' ', sizeof (ascii)) ;
4905 printf ("%08X: ", k) ;
4906 for (m = 0 ; m < 16 && k + m < len ; m++)
4907 { printf (m == 8 ? " %02X " : "%02X ", data [k + m] & 0xFF) ;
4908 ascii [m] = isprint (data [k + m]) ? data [k + m] : '.' ;
4911 if (m <= 8) printf (" ") ;
4912 for ( ; m < 16 ; m++) printf (" ") ;
4914 ascii [16] = 0 ;
4915 printf (" %s\n", ascii) ;
4918 puts ("") ;
4919 } /* psf_hexdump */
4921 void
4922 psf_log_SF_INFO (SF_PRIVATE *psf)
4923 { psf_log_printf (psf, "---------------------------------\n") ;
4925 psf_log_printf (psf, " Sample rate : %d\n", psf->sf.samplerate) ;
4926 psf_log_printf (psf, " Frames : %C\n", psf->sf.frames) ;
4927 psf_log_printf (psf, " Channels : %d\n", psf->sf.channels) ;
4929 psf_log_printf (psf, " Format : 0x%X\n", psf->sf.format) ;
4930 psf_log_printf (psf, " Sections : %d\n", psf->sf.sections) ;
4931 psf_log_printf (psf, " Seekable : %s\n", psf->sf.seekable ? "TRUE" : "FALSE") ;
4933 psf_log_printf (psf, "---------------------------------\n") ;
4934 } /* psf_dump_SFINFO */
4936 /*========================================================================================
4939 void*
4940 psf_memset (void *s, int c, sf_count_t len)
4941 { char *ptr ;
4942 int setcount ;
4944 ptr = (char *) s ;
4946 while (len > 0)
4947 { setcount = (len > 0x10000000) ? 0x10000000 : (int) len ;
4949 memset (ptr, c, setcount) ;
4951 ptr += setcount ;
4952 len -= setcount ;
4955 return s ;
4956 } /* psf_memset */
4958 void psf_get_date_str (char *str, int maxlen)
4959 { time_t current ;
4960 struct tm timedata, *tmptr ;
4962 time (&current) ;
4964 #if defined (HAVE_GMTIME_R)
4965 /* If the re-entrant version is available, use it. */
4966 tmptr = gmtime_r (&current, &timedata) ;
4967 #elif defined (HAVE_GMTIME)
4968 /* Otherwise use the standard one and copy the data to local storage. */
4969 tmptr = gmtime (&current) ;
4970 memcpy (&timedata, tmptr, sizeof (timedata)) ;
4971 #else
4972 tmptr = NULL ;
4973 #endif
4975 if (tmptr)
4976 LSF_SNPRINTF (str, maxlen, "%4d-%02d-%02d %02d:%02d:%02d UTC",
4977 1900 + timedata.tm_year, timedata.tm_mon, timedata.tm_mday,
4978 timedata.tm_hour, timedata.tm_min, timedata.tm_sec) ;
4979 else
4980 LSF_SNPRINTF (str, maxlen, "Unknown date") ;
4982 return ;
4983 } /* psf_get_date_str */
4986 subformat_to_bytewidth (int format)
4988 switch (format)
4989 { case SF_FORMAT_PCM_U8 :
4990 case SF_FORMAT_PCM_S8 :
4991 return 1 ;
4992 case SF_FORMAT_PCM_16 :
4993 return 2 ;
4994 case SF_FORMAT_PCM_24 :
4995 return 3 ;
4996 case SF_FORMAT_PCM_32 :
4997 case SF_FORMAT_FLOAT :
4998 return 4 ;
4999 case SF_FORMAT_DOUBLE :
5000 return 8 ;
5003 return 0 ;
5004 } /* subformat_to_bytewidth */
5007 s_bitwidth_to_subformat (int bits)
5008 { static int array [] =
5009 { SF_FORMAT_PCM_S8, SF_FORMAT_PCM_16, SF_FORMAT_PCM_24, SF_FORMAT_PCM_32
5012 if (bits < 8 || bits > 32)
5013 return 0 ;
5015 return array [((bits + 7) / 8) - 1] ;
5016 } /* bitwidth_to_subformat */
5019 u_bitwidth_to_subformat (int bits)
5020 { static int array [] =
5021 { SF_FORMAT_PCM_U8, SF_FORMAT_PCM_16, SF_FORMAT_PCM_24, SF_FORMAT_PCM_32
5024 if (bits < 8 || bits > 32)
5025 return 0 ;
5027 return array [((bits + 7) / 8) - 1] ;
5028 } /* bitwidth_to_subformat */
5030 #endif /* PSF_LOG_PRINTF_ONLY */
5033 ** Do not edit or modify anything in this comment block.
5034 ** The arch-tag line is a file identity tag for the GNU Arch
5035 ** revision control system.
5037 ** arch-tag: 33e9795e-f717-461a-9feb-65d083a56395
5040 * Copyright 1992 by Jutta Degener and Carsten Bormann, Technische
5041 * Universitaet Berlin. See the accompanying file "COPYRIGHT" for
5042 * details. THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE.
5045 #include <stdio.h>
5049 * 4.3 FIXED POINT IMPLEMENTATION OF THE RPE-LTP DECODER
5052 static void Postprocessing (
5053 struct gsm_state * S,
5054 register word * s)
5056 register int k;
5057 register word msr = S->msr;
5058 register word tmp;
5060 for (k = 160; k--; s++) {
5061 tmp = GSM_MULT_R( msr, 28180 );
5062 msr = GSM_ADD(*s, tmp); /* Deemphasis */
5063 *s = GSM_ADD(msr, msr) & 0xFFF8; /* Truncation & Upscaling */
5065 S->msr = msr;
5068 void Gsm_Decoder (
5069 struct gsm_state * S,
5071 word * LARcr, /* [0..7] IN */
5073 word * Ncr, /* [0..3] IN */
5074 word * bcr, /* [0..3] IN */
5075 word * Mcr, /* [0..3] IN */
5076 word * xmaxcr, /* [0..3] IN */
5077 word * xMcr, /* [0..13*4] IN */
5079 word * s) /* [0..159] OUT */
5081 int j, k;
5082 word erp[40], wt[160];
5083 word * drp = S->dp0 + 120;
5085 for (j=0; j <= 3; j++, xmaxcr++, bcr++, Ncr++, Mcr++, xMcr += 13) {
5087 Gsm_RPE_Decoding( /*-S,-*/ *xmaxcr, *Mcr, xMcr, erp );
5088 Gsm_Long_Term_Synthesis_Filtering( S, *Ncr, *bcr, erp, drp );
5090 for (k = 0; k <= 39; k++) wt[ j * 40 + k ] = drp[ k ];
5093 Gsm_Short_Term_Synthesis_Filter( S, LARcr, wt, s );
5094 Postprocessing(S, s);
5097 ** Do not edit or modify anything in this comment block.
5098 ** The arch-tag line is a file identity tag for the GNU Arch
5099 ** revision control system.
5101 ** arch-tag: 11ae5b90-2e8b-400b-ac64-a69a1fc6cc41
5105 ** Copyright (C) 2003,2004 Erik de Castro Lopo <erikd@mega-nerd.com>
5107 ** This program is free software; you can redistribute it and/or modify
5108 ** it under the terms of the GNU Lesser General Public License as published by
5109 ** the Free Software Foundation; either version 2.1 of the License, or
5110 ** (at your option) any later version.
5112 ** This program is distributed in the hope that it will be useful,
5113 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
5114 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5115 ** GNU Lesser General Public License for more details.
5117 ** You should have received a copy of the GNU Lesser General Public License
5118 ** along with this program; if not, write to the Free Software
5119 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
5122 #include <stdlib.h>
5125 /*============================================================================
5126 ** Rule number 1 is to only apply dither when going from a larger bitwidth
5127 ** to a smaller bitwidth. This can happen on both read and write.
5129 ** Need to apply dither on all conversions marked X below.
5131 ** Dither on write:
5133 ** Input
5134 ** | short int float double
5135 ** --------+-----------------------------------------------
5136 ** O 8 bit | X X X X
5137 ** u 16 bit | none X X X
5138 ** t 24 bit | none X X X
5139 ** p 32 bit | none none X X
5140 ** u float | none none none none
5141 ** t double | none none none none
5143 ** Dither on read:
5145 ** Input
5146 ** O | 8 bit 16 bit 24 bit 32 bit float double
5147 ** u --------+-------------------------------------------------
5148 ** t short | none none X X X X
5149 ** p int | none none none X X X
5150 ** u float | none none none none none none
5151 ** t double | none none none none none none
5154 #define SFE_DITHER_BAD_PTR 666
5155 #define SFE_DITHER_BAD_TYPE 667
5157 typedef struct
5158 { int read_short_dither_bits, read_int_dither_bits ;
5159 int write_short_dither_bits, write_int_dither_bits ;
5160 double read_float_dither_scale, read_double_dither_bits ;
5161 double write_float_dither_scale, write_double_dither_bits ;
5163 sf_count_t (*read_short) (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
5164 sf_count_t (*read_int) (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
5165 sf_count_t (*read_float) (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
5166 sf_count_t (*read_double) (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
5168 sf_count_t (*write_short) (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
5169 sf_count_t (*write_int) (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
5170 sf_count_t (*write_float) (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
5171 sf_count_t (*write_double) (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
5173 double buffer [SF_BUFFER_LEN / sizeof (double)] ;
5174 } DITHER_DATA ;
5176 static sf_count_t dither_read_short (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
5177 static sf_count_t dither_read_int (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
5179 static sf_count_t dither_write_short (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
5180 static sf_count_t dither_write_int (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
5181 static sf_count_t dither_write_float (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
5182 static sf_count_t dither_write_double (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
5185 dither_init (SF_PRIVATE *psf, int mode)
5186 { DITHER_DATA *pdither ;
5188 pdither = psf->dither ; /* This may be NULL. */
5190 /* Turn off dither on read. */
5191 if (mode == SFM_READ && psf->read_dither.type == SFD_NO_DITHER)
5192 { if (pdither == NULL)
5193 return 0 ; /* Dither is already off, so just return. */
5195 if (pdither->read_short)
5196 psf->read_short = pdither->read_short ;
5197 if (pdither->read_int)
5198 psf->read_int = pdither->read_int ;
5199 if (pdither->read_float)
5200 psf->read_float = pdither->read_float ;
5201 if (pdither->read_double)
5202 psf->read_double = pdither->read_double ;
5203 return 0 ;
5206 /* Turn off dither on write. */
5207 if (mode == SFM_WRITE && psf->write_dither.type == SFD_NO_DITHER)
5208 { if (pdither == NULL)
5209 return 0 ; /* Dither is already off, so just return. */
5211 if (pdither->write_short)
5212 psf->write_short = pdither->write_short ;
5213 if (pdither->write_int)
5214 psf->write_int = pdither->write_int ;
5215 if (pdither->write_float)
5216 psf->write_float = pdither->write_float ;
5217 if (pdither->write_double)
5218 psf->write_double = pdither->write_double ;
5219 return 0 ;
5222 /* Turn on dither on read if asked. */
5223 if (mode == SFM_READ && psf->read_dither.type != 0)
5224 { if (pdither == NULL)
5225 pdither = psf->dither = calloc (1, sizeof (DITHER_DATA)) ;
5226 if (pdither == NULL)
5227 return SFE_MALLOC_FAILED ;
5229 switch (psf->sf.format & SF_FORMAT_SUBMASK)
5230 { case SF_FORMAT_DOUBLE :
5231 case SF_FORMAT_FLOAT :
5232 pdither->read_int = psf->read_int ;
5233 psf->read_int = dither_read_int ;
5235 case SF_FORMAT_PCM_32 :
5236 case SF_FORMAT_PCM_24 :
5237 case SF_FORMAT_PCM_16 :
5238 case SF_FORMAT_PCM_S8 :
5239 case SF_FORMAT_PCM_U8 :
5240 pdither->read_short = psf->read_short ;
5241 psf->read_short = dither_read_short ;
5243 default : break ;
5247 /* Turn on dither on write if asked. */
5248 if (mode == SFM_WRITE && psf->write_dither.type != 0)
5249 { if (pdither == NULL)
5250 pdither = psf->dither = calloc (1, sizeof (DITHER_DATA)) ;
5251 if (pdither == NULL)
5252 return SFE_MALLOC_FAILED ;
5254 switch (psf->sf.format & SF_FORMAT_SUBMASK)
5255 { case SF_FORMAT_DOUBLE :
5256 case SF_FORMAT_FLOAT :
5257 pdither->write_int = psf->write_int ;
5258 psf->write_int = dither_write_int ;
5260 case SF_FORMAT_PCM_32 :
5261 case SF_FORMAT_PCM_24 :
5262 case SF_FORMAT_PCM_16 :
5263 case SF_FORMAT_PCM_S8 :
5264 case SF_FORMAT_PCM_U8 :
5266 default : break ;
5269 pdither->write_short = psf->write_short ;
5270 psf->write_short = dither_write_short ;
5272 pdither->write_int = psf->write_int ;
5273 psf->write_int = dither_write_int ;
5275 pdither->write_float = psf->write_float ;
5276 psf->write_float = dither_write_float ;
5278 pdither->write_double = psf->write_double ;
5279 psf->write_double = dither_write_double ;
5282 return 0 ;
5283 } /* dither_init */
5285 /*==============================================================================
5288 static void dither_short (const short *in, short *out, int frames, int channels) ;
5289 static void dither_int (const int *in, int *out, int frames, int channels) ;
5291 static void dither_float (const float *in, float *out, int frames, int channels) ;
5292 static void dither_double (const double *in, double *out, int frames, int channels) ;
5294 static sf_count_t
5295 dither_read_short (SF_PRIVATE *psf, short *ptr, sf_count_t len)
5296 { psf = psf ;
5297 ptr = ptr ;
5298 return len ;
5299 } /* dither_read_short */
5301 static sf_count_t
5302 dither_read_int (SF_PRIVATE *psf, int *ptr, sf_count_t len)
5303 { psf = psf ;
5304 ptr = ptr ;
5305 return len ;
5306 } /* dither_read_int */
5308 /*------------------------------------------------------------------------------
5311 static sf_count_t
5312 dither_write_short (SF_PRIVATE *psf, short *ptr, sf_count_t len)
5313 { DITHER_DATA *pdither ;
5314 int bufferlen, writecount, thiswrite ;
5315 sf_count_t total = 0 ;
5317 if ((pdither = psf->dither) == NULL)
5318 { psf->error = SFE_DITHER_BAD_PTR ;
5319 return 0 ;
5322 switch (psf->sf.format & SF_FORMAT_SUBMASK)
5323 { case SF_FORMAT_PCM_S8 :
5324 case SF_FORMAT_PCM_U8 :
5325 case SF_FORMAT_DPCM_8 :
5326 break ;
5328 default :
5329 return pdither->write_short (psf, ptr, len) ;
5332 bufferlen = sizeof (pdither->buffer) / sizeof (short) ;
5334 while (len > 0)
5335 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
5336 writecount /= psf->sf.channels ;
5337 writecount *= psf->sf.channels ;
5339 dither_short (ptr, (short*) pdither->buffer, writecount / psf->sf.channels, psf->sf.channels) ;
5341 thiswrite = pdither->write_short (psf, (short*) pdither->buffer, writecount) ;
5342 total += thiswrite ;
5343 len -= thiswrite ;
5344 if (thiswrite < writecount)
5345 break ;
5348 return total ;
5349 } /* dither_write_short */
5351 static sf_count_t
5352 dither_write_int (SF_PRIVATE *psf, int *ptr, sf_count_t len)
5353 { DITHER_DATA *pdither ;
5354 int bufferlen, writecount, thiswrite ;
5355 sf_count_t total = 0 ;
5357 if ((pdither = psf->dither) == NULL)
5358 { psf->error = SFE_DITHER_BAD_PTR ;
5359 return 0 ;
5362 switch (psf->sf.format & SF_FORMAT_SUBMASK)
5363 { case SF_FORMAT_PCM_S8 :
5364 case SF_FORMAT_PCM_U8 :
5365 case SF_FORMAT_PCM_16 :
5366 case SF_FORMAT_PCM_24 :
5368 case SF_FORMAT_DPCM_8 :
5369 case SF_FORMAT_DPCM_16 :
5370 break ;
5372 default :
5373 return pdither->write_int (psf, ptr, len) ;
5377 bufferlen = sizeof (pdither->buffer) / sizeof (int) ;
5379 while (len > 0)
5380 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
5381 writecount /= psf->sf.channels ;
5382 writecount *= psf->sf.channels ;
5384 dither_int (ptr, (int*) pdither->buffer, writecount / psf->sf.channels, psf->sf.channels) ;
5386 thiswrite = pdither->write_int (psf, (int*) pdither->buffer, writecount) ;
5387 total += thiswrite ;
5388 len -= thiswrite ;
5389 if (thiswrite < writecount)
5390 break ;
5393 return total ;
5394 } /* dither_write_int */
5396 static sf_count_t
5397 dither_write_float (SF_PRIVATE *psf, float *ptr, sf_count_t len)
5398 { DITHER_DATA *pdither ;
5399 int bufferlen, writecount, thiswrite ;
5400 sf_count_t total = 0 ;
5402 if ((pdither = psf->dither) == NULL)
5403 { psf->error = SFE_DITHER_BAD_PTR ;
5404 return 0 ;
5407 switch (psf->sf.format & SF_FORMAT_SUBMASK)
5408 { case SF_FORMAT_PCM_S8 :
5409 case SF_FORMAT_PCM_U8 :
5410 case SF_FORMAT_PCM_16 :
5411 case SF_FORMAT_PCM_24 :
5413 case SF_FORMAT_DPCM_8 :
5414 case SF_FORMAT_DPCM_16 :
5415 break ;
5417 default :
5418 return pdither->write_float (psf, ptr, len) ;
5421 bufferlen = sizeof (pdither->buffer) / sizeof (float) ;
5423 while (len > 0)
5424 { writecount = (len >= bufferlen) ? bufferlen : (float) len ;
5425 writecount /= psf->sf.channels ;
5426 writecount *= psf->sf.channels ;
5428 dither_float (ptr, (float*) pdither->buffer, writecount / psf->sf.channels, psf->sf.channels) ;
5430 thiswrite = pdither->write_float (psf, (float*) pdither->buffer, writecount) ;
5431 total += thiswrite ;
5432 len -= thiswrite ;
5433 if (thiswrite < writecount)
5434 break ;
5437 return total ;
5438 } /* dither_write_float */
5440 static sf_count_t
5441 dither_write_double (SF_PRIVATE *psf, double *ptr, sf_count_t len)
5442 { DITHER_DATA *pdither ;
5443 int bufferlen, writecount, thiswrite ;
5444 sf_count_t total = 0 ;
5446 if ((pdither = psf->dither) == NULL)
5447 { psf->error = SFE_DITHER_BAD_PTR ;
5448 return 0 ;
5451 switch (psf->sf.format & SF_FORMAT_SUBMASK)
5452 { case SF_FORMAT_PCM_S8 :
5453 case SF_FORMAT_PCM_U8 :
5454 case SF_FORMAT_PCM_16 :
5455 case SF_FORMAT_PCM_24 :
5457 case SF_FORMAT_DPCM_8 :
5458 case SF_FORMAT_DPCM_16 :
5459 break ;
5461 default :
5462 return pdither->write_double (psf, ptr, len) ;
5466 bufferlen = sizeof (pdither->buffer) / sizeof (double) ;
5468 while (len > 0)
5469 { writecount = (len >= bufferlen) ? bufferlen : (double) len ;
5470 writecount /= psf->sf.channels ;
5471 writecount *= psf->sf.channels ;
5473 dither_double (ptr, (double*) pdither->buffer, writecount / psf->sf.channels, psf->sf.channels) ;
5475 thiswrite = pdither->write_double (psf, (double*) pdither->buffer, writecount) ;
5476 total += thiswrite ;
5477 len -= thiswrite ;
5478 if (thiswrite < writecount)
5479 break ;
5482 return total ;
5483 } /* dither_write_double */
5485 /*==============================================================================
5488 static void
5489 dither_short (const short *in, short *out, int frames, int channels)
5490 { int ch, k ;
5492 for (ch = 0 ; ch < channels ; ch++)
5493 for (k = ch ; k < channels * frames ; k += channels)
5494 out [k] = in [k] ;
5496 } /* dither_short */
5498 static void
5499 dither_int (const int *in, int *out, int frames, int channels)
5500 { int ch, k ;
5502 for (ch = 0 ; ch < channels ; ch++)
5503 for (k = ch ; k < channels * frames ; k += channels)
5504 out [k] = in [k] ;
5506 } /* dither_int */
5508 static void
5509 dither_float (const float *in, float *out, int frames, int channels)
5510 { int ch, k ;
5512 for (ch = 0 ; ch < channels ; ch++)
5513 for (k = ch ; k < channels * frames ; k += channels)
5514 out [k] = in [k] ;
5516 } /* dither_float */
5518 static void
5519 dither_double (const double *in, double *out, int frames, int channels)
5520 { int ch, k ;
5522 for (ch = 0 ; ch < channels ; ch++)
5523 for (k = ch ; k < channels * frames ; k += channels)
5524 out [k] = in [k] ;
5526 } /* dither_double */
5528 /*==============================================================================
5530 #if 0
5533 ** Not made public because this (maybe) requires storage of state information.
5535 ** Also maybe need separate state info for each channel!!!!
5539 DO_NOT_USE_sf_dither_short (const SF_DITHER_INFO *dither, const short *in, short *out, int frames, int channels)
5540 { int ch, k ;
5542 if (! dither)
5543 return SFE_DITHER_BAD_PTR ;
5545 switch (dither->type & SFD_TYPEMASK)
5546 { case SFD_WHITE :
5547 case SFD_TRIANGULAR_PDF :
5548 for (ch = 0 ; ch < channels ; ch++)
5549 for (k = ch ; k < channels * frames ; k += channels)
5550 out [k] = in [k] ;
5551 break ;
5553 default :
5554 return SFE_DITHER_BAD_TYPE ;
5557 return 0 ;
5558 } /* DO_NOT_USE_sf_dither_short */
5561 DO_NOT_USE_sf_dither_int (const SF_DITHER_INFO *dither, const int *in, int *out, int frames, int channels)
5562 { int ch, k ;
5564 if (! dither)
5565 return SFE_DITHER_BAD_PTR ;
5567 switch (dither->type & SFD_TYPEMASK)
5568 { case SFD_WHITE :
5569 case SFD_TRIANGULAR_PDF :
5570 for (ch = 0 ; ch < channels ; ch++)
5571 for (k = ch ; k < channels * frames ; k += channels)
5572 out [k] = in [k] ;
5573 break ;
5575 default :
5576 return SFE_DITHER_BAD_TYPE ;
5579 return 0 ;
5580 } /* DO_NOT_USE_sf_dither_int */
5583 DO_NOT_USE_sf_dither_float (const SF_DITHER_INFO *dither, const float *in, float *out, int frames, int channels)
5584 { int ch, k ;
5586 if (! dither)
5587 return SFE_DITHER_BAD_PTR ;
5589 switch (dither->type & SFD_TYPEMASK)
5590 { case SFD_WHITE :
5591 case SFD_TRIANGULAR_PDF :
5592 for (ch = 0 ; ch < channels ; ch++)
5593 for (k = ch ; k < channels * frames ; k += channels)
5594 out [k] = in [k] ;
5595 break ;
5597 default :
5598 return SFE_DITHER_BAD_TYPE ;
5601 return 0 ;
5602 } /* DO_NOT_USE_sf_dither_float */
5605 DO_NOT_USE_sf_dither_double (const SF_DITHER_INFO *dither, const double *in, double *out, int frames, int channels)
5606 { int ch, k ;
5608 if (! dither)
5609 return SFE_DITHER_BAD_PTR ;
5611 switch (dither->type & SFD_TYPEMASK)
5612 { case SFD_WHITE :
5613 case SFD_TRIANGULAR_PDF :
5614 for (ch = 0 ; ch < channels ; ch++)
5615 for (k = ch ; k < channels * frames ; k += channels)
5616 out [k] = in [k] ;
5617 break ;
5619 default :
5620 return SFE_DITHER_BAD_TYPE ;
5623 return 0 ;
5624 } /* DO_NOT_USE_sf_dither_double */
5626 #endif
5628 ** Do not edit or modify anything in this comment block.
5629 ** The arch-tag line is a file identity tag for the GNU Arch
5630 ** revision control system.
5632 ** arch-tag: 673fad58-5314-421c-9144-9d54bfdf104c
5635 ** Copyright (C) 1999-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
5637 ** This program is free software; you can redistribute it and/or modify
5638 ** it under the terms of the GNU Lesser General Public License as published by
5639 ** the Free Software Foundation; either version 2.1 of the License, or
5640 ** (at your option) any later version.
5642 ** This program is distributed in the hope that it will be useful,
5643 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
5644 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5645 ** GNU Lesser General Public License for more details.
5647 ** You should have received a copy of the GNU Lesser General Public License
5648 ** along with this program; if not, write to the Free Software
5649 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
5653 #include <stdio.h>
5654 #include <stdlib.h>
5655 #include <string.h>
5658 #if CPU_IS_LITTLE_ENDIAN
5659 #define DOUBLE64_READ double64_le_read
5660 #define DOUBLE64_WRITE double64_le_write
5661 #elif CPU_IS_BIG_ENDIAN
5662 #define DOUBLE64_READ double64_be_read
5663 #define DOUBLE64_WRITE double64_be_write
5664 #endif
5666 /*--------------------------------------------------------------------------------------------
5667 ** Processor floating point capabilities. double64_get_capability () returns one of the
5668 ** latter three values.
5671 enum
5672 { DOUBLE_UNKNOWN = 0x00,
5673 DOUBLE_CAN_RW_LE = 0x23,
5674 DOUBLE_CAN_RW_BE = 0x34,
5675 DOUBLE_BROKEN_LE = 0x45,
5676 DOUBLE_BROKEN_BE = 0x56
5679 /*--------------------------------------------------------------------------------------------
5680 ** Prototypes for private functions.
5683 static sf_count_t host_read_d2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
5684 static sf_count_t host_read_d2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
5685 static sf_count_t host_read_d2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
5686 static sf_count_t host_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
5688 static sf_count_t host_write_s2d (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
5689 static sf_count_t host_write_i2d (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
5690 static sf_count_t host_write_f2d (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
5691 static sf_count_t host_write_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
5693 static void d2s_array (double *buffer, unsigned int count, short *ptr) ;
5694 static void d2i_array (double *buffer, unsigned int count, int *ptr) ;
5695 static void double64d2f_array (double *buffer, unsigned int count, float *ptr) ;
5697 static void s2d_array (short *ptr, double *buffer, unsigned int count) ;
5698 static void i2d_array (int *ptr, double *buffer, unsigned int count) ;
5699 static void double64f2d_array (float *ptr, double *buffer, unsigned int count) ;
5701 static void double64_peak_update (SF_PRIVATE *psf, double *buffer, int count, int indx) ;
5703 static int double64_get_capability (SF_PRIVATE *psf) ;
5705 static sf_count_t replace_read_d2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
5706 static sf_count_t replace_read_d2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
5707 static sf_count_t replace_read_d2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
5708 static sf_count_t replace_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
5710 static sf_count_t replace_write_s2d (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
5711 static sf_count_t replace_write_i2d (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
5712 static sf_count_t replace_write_f2d (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
5713 static sf_count_t replace_write_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
5715 static void d2bd_read (double *buffer, int count) ;
5716 static void bd2d_write (double *buffer, int count) ;
5718 /*--------------------------------------------------------------------------------------------
5719 ** Exported functions.
5723 double64_init (SF_PRIVATE *psf)
5724 { static int double64_caps ;
5726 double64_caps = double64_get_capability (psf) ;
5728 psf->blockwidth = sizeof (double) * psf->sf.channels ;
5730 if (psf->mode == SFM_READ || psf->mode == SFM_RDWR)
5731 { switch (psf->endian + double64_caps)
5732 { case (SF_ENDIAN_BIG + DOUBLE_CAN_RW_BE) :
5733 psf->float_endswap = SF_FALSE ;
5734 psf->read_short = host_read_d2s ;
5735 psf->read_int = host_read_d2i ;
5736 psf->read_float = host_read_d2f ;
5737 psf->read_double = host_read_d ;
5738 break ;
5740 case (SF_ENDIAN_LITTLE + DOUBLE_CAN_RW_LE) :
5741 psf->float_endswap = SF_FALSE ;
5742 psf->read_short = host_read_d2s ;
5743 psf->read_int = host_read_d2i ;
5744 psf->read_float = host_read_d2f ;
5745 psf->read_double = host_read_d ;
5746 break ;
5748 case (SF_ENDIAN_BIG + DOUBLE_CAN_RW_LE) :
5749 psf->float_endswap = SF_TRUE ;
5750 psf->read_short = host_read_d2s ;
5751 psf->read_int = host_read_d2i ;
5752 psf->read_float = host_read_d2f ;
5753 psf->read_double = host_read_d ;
5754 break ;
5756 case (SF_ENDIAN_LITTLE + DOUBLE_CAN_RW_BE) :
5757 psf->float_endswap = SF_TRUE ;
5758 psf->read_short = host_read_d2s ;
5759 psf->read_int = host_read_d2i ;
5760 psf->read_float = host_read_d2f ;
5761 psf->read_double = host_read_d ;
5762 break ;
5764 /* When the CPU is not IEEE compatible. */
5765 case (SF_ENDIAN_BIG + DOUBLE_BROKEN_BE) :
5766 psf->float_endswap = SF_FALSE ;
5767 psf->read_short = replace_read_d2s ;
5768 psf->read_int = replace_read_d2i ;
5769 psf->read_float = replace_read_d2f ;
5770 psf->read_double = replace_read_d ;
5771 break ;
5773 case (SF_ENDIAN_LITTLE + DOUBLE_BROKEN_LE) :
5774 psf->float_endswap = SF_FALSE ;
5775 psf->read_short = replace_read_d2s ;
5776 psf->read_int = replace_read_d2i ;
5777 psf->read_float = replace_read_d2f ;
5778 psf->read_double = replace_read_d ;
5779 break ;
5781 case (SF_ENDIAN_BIG + DOUBLE_BROKEN_LE) :
5782 psf->float_endswap = SF_TRUE ;
5783 psf->read_short = replace_read_d2s ;
5784 psf->read_int = replace_read_d2i ;
5785 psf->read_float = replace_read_d2f ;
5786 psf->read_double = replace_read_d ;
5787 break ;
5789 case (SF_ENDIAN_LITTLE + DOUBLE_BROKEN_BE) :
5790 psf->float_endswap = SF_TRUE ;
5791 psf->read_short = replace_read_d2s ;
5792 psf->read_int = replace_read_d2i ;
5793 psf->read_float = replace_read_d2f ;
5794 psf->read_double = replace_read_d ;
5795 break ;
5797 default : break ;
5801 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
5802 { switch (psf->endian + double64_caps)
5803 { case (SF_ENDIAN_LITTLE + DOUBLE_CAN_RW_LE) :
5804 psf->float_endswap = SF_FALSE ;
5805 psf->write_short = host_write_s2d ;
5806 psf->write_int = host_write_i2d ;
5807 psf->write_float = host_write_f2d ;
5808 psf->write_double = host_write_d ;
5809 break ;
5811 case (SF_ENDIAN_BIG + DOUBLE_CAN_RW_BE) :
5812 psf->float_endswap = SF_FALSE ;
5813 psf->write_short = host_write_s2d ;
5814 psf->write_int = host_write_i2d ;
5815 psf->write_float = host_write_f2d ;
5816 psf->write_double = host_write_d ;
5817 break ;
5819 case (SF_ENDIAN_BIG + DOUBLE_CAN_RW_LE) :
5820 psf->float_endswap = SF_TRUE ;
5821 psf->write_short = host_write_s2d ;
5822 psf->write_int = host_write_i2d ;
5823 psf->write_float = host_write_f2d ;
5824 psf->write_double = host_write_d ;
5825 break ;
5827 case (SF_ENDIAN_LITTLE + DOUBLE_CAN_RW_BE) :
5828 psf->float_endswap = SF_TRUE ;
5829 psf->write_short = host_write_s2d ;
5830 psf->write_int = host_write_i2d ;
5831 psf->write_float = host_write_f2d ;
5832 psf->write_double = host_write_d ;
5833 break ;
5835 /* When the CPU is not IEEE compatible. */
5836 case (SF_ENDIAN_LITTLE + DOUBLE_BROKEN_LE) :
5837 psf->float_endswap = SF_FALSE ;
5838 psf->write_short = replace_write_s2d ;
5839 psf->write_int = replace_write_i2d ;
5840 psf->write_float = replace_write_f2d ;
5841 psf->write_double = replace_write_d ;
5842 break ;
5844 case (SF_ENDIAN_BIG + DOUBLE_BROKEN_BE) :
5845 psf->float_endswap = SF_FALSE ;
5846 psf->write_short = replace_write_s2d ;
5847 psf->write_int = replace_write_i2d ;
5848 psf->write_float = replace_write_f2d ;
5849 psf->write_double = replace_write_d ;
5850 break ;
5852 case (SF_ENDIAN_BIG + DOUBLE_BROKEN_LE) :
5853 psf->float_endswap = SF_TRUE ;
5854 psf->write_short = replace_write_s2d ;
5855 psf->write_int = replace_write_i2d ;
5856 psf->write_float = replace_write_f2d ;
5857 psf->write_double = replace_write_d ;
5858 break ;
5860 case (SF_ENDIAN_LITTLE + DOUBLE_BROKEN_BE) :
5861 psf->float_endswap = SF_TRUE ;
5862 psf->write_short = replace_write_s2d ;
5863 psf->write_int = replace_write_i2d ;
5864 psf->write_float = replace_write_f2d ;
5865 psf->write_double = replace_write_d ;
5866 break ;
5868 default : break ;
5872 if (psf->filelength > psf->dataoffset)
5873 { psf->datalength = (psf->dataend > 0) ? psf->dataend - psf->dataoffset :
5874 psf->filelength - psf->dataoffset ;
5876 else
5877 psf->datalength = 0 ;
5879 psf->sf.frames = psf->datalength / psf->blockwidth ;
5881 return 0 ;
5882 } /* double64_init */
5884 /*----------------------------------------------------------------------------
5885 ** From : http://www.hpcf.cam.ac.uk/fp_formats.html
5887 ** 64 bit double precision layout (big endian)
5888 ** Sign bit 0
5889 ** Exponent bits 1-11
5890 ** Mantissa bits 12-63
5891 ** Exponent Offset 1023
5893 ** double single
5895 ** +INF 7FF0000000000000 7F800000
5896 ** -INF FFF0000000000000 FF800000
5897 ** NaN 7FF0000000000001 7F800001
5898 ** to to
5899 ** 7FFFFFFFFFFFFFFF 7FFFFFFF
5900 ** and and
5901 ** FFF0000000000001 FF800001
5902 ** to to
5903 ** FFFFFFFFFFFFFFFF FFFFFFFF
5904 ** +OVER 7FEFFFFFFFFFFFFF 7F7FFFFF
5905 ** -OVER FFEFFFFFFFFFFFFF FF7FFFFF
5906 ** +UNDER 0010000000000000 00800000
5907 ** -UNDER 8010000000000000 80800000
5910 double
5911 double64_be_read (unsigned char *cptr)
5912 { int exponent, negative ;
5913 double dvalue ;
5915 negative = (cptr [0] & 0x80) ? 1 : 0 ;
5916 exponent = ((cptr [0] & 0x7F) << 4) | ((cptr [1] >> 4) & 0xF) ;
5918 /* Might not have a 64 bit long, so load the mantissa into a double. */
5919 dvalue = (((cptr [1] & 0xF) << 24) | (cptr [2] << 16) | (cptr [3] << 8) | cptr [4]) ;
5920 dvalue += ((cptr [5] << 16) | (cptr [6] << 8) | cptr [7]) / ((double) 0x1000000) ;
5922 if (exponent == 0 && dvalue == 0.0)
5923 return 0.0 ;
5925 dvalue += 0x10000000 ;
5927 exponent = exponent - 0x3FF ;
5929 dvalue = dvalue / ((double) 0x10000000) ;
5931 if (negative)
5932 dvalue *= -1 ;
5934 if (exponent > 0)
5935 dvalue *= (1 << exponent) ;
5936 else if (exponent < 0)
5937 dvalue /= (1 << abs (exponent)) ;
5939 return dvalue ;
5940 } /* double64_be_read */
5942 double
5943 double64_le_read (unsigned char *cptr)
5944 { int exponent, negative ;
5945 double dvalue ;
5947 negative = (cptr [7] & 0x80) ? 1 : 0 ;
5948 exponent = ((cptr [7] & 0x7F) << 4) | ((cptr [6] >> 4) & 0xF) ;
5950 /* Might not have a 64 bit long, so load the mantissa into a double. */
5951 dvalue = (((cptr [6] & 0xF) << 24) | (cptr [5] << 16) | (cptr [4] << 8) | cptr [3]) ;
5952 dvalue += ((cptr [2] << 16) | (cptr [1] << 8) | cptr [0]) / ((double) 0x1000000) ;
5954 if (exponent == 0 && dvalue == 0.0)
5955 return 0.0 ;
5957 dvalue += 0x10000000 ;
5959 exponent = exponent - 0x3FF ;
5961 dvalue = dvalue / ((double) 0x10000000) ;
5963 if (negative)
5964 dvalue *= -1 ;
5966 if (exponent > 0)
5967 dvalue *= (1 << exponent) ;
5968 else if (exponent < 0)
5969 dvalue /= (1 << abs (exponent)) ;
5971 return dvalue ;
5972 } /* double64_le_read */
5974 void
5975 double64_be_write (double in, unsigned char *out)
5976 { int exponent, mantissa ;
5978 memset (out, 0, sizeof (double)) ;
5980 if (in == 0.0)
5981 return ;
5983 if (in < 0.0)
5984 { in *= -1.0 ;
5985 out [0] |= 0x80 ;
5988 in = frexp (in, &exponent) ;
5990 exponent += 1022 ;
5992 out [0] |= (exponent >> 4) & 0x7F ;
5993 out [1] |= (exponent << 4) & 0xF0 ;
5995 in *= 0x20000000 ;
5996 mantissa = lrint (floor (in)) ;
5998 out [1] |= (mantissa >> 24) & 0xF ;
5999 out [2] = (mantissa >> 16) & 0xFF ;
6000 out [3] = (mantissa >> 8) & 0xFF ;
6001 out [4] = mantissa & 0xFF ;
6003 in = fmod (in, 1.0) ;
6004 in *= 0x1000000 ;
6005 mantissa = lrint (floor (in)) ;
6007 out [5] = (mantissa >> 16) & 0xFF ;
6008 out [6] = (mantissa >> 8) & 0xFF ;
6009 out [7] = mantissa & 0xFF ;
6011 return ;
6012 } /* double64_be_write */
6014 void
6015 double64_le_write (double in, unsigned char *out)
6016 { int exponent, mantissa ;
6018 memset (out, 0, sizeof (double)) ;
6020 if (in == 0.0)
6021 return ;
6023 if (in < 0.0)
6024 { in *= -1.0 ;
6025 out [7] |= 0x80 ;
6028 in = frexp (in, &exponent) ;
6030 exponent += 1022 ;
6032 out [7] |= (exponent >> 4) & 0x7F ;
6033 out [6] |= (exponent << 4) & 0xF0 ;
6035 in *= 0x20000000 ;
6036 mantissa = lrint (floor (in)) ;
6038 out [6] |= (mantissa >> 24) & 0xF ;
6039 out [5] = (mantissa >> 16) & 0xFF ;
6040 out [4] = (mantissa >> 8) & 0xFF ;
6041 out [3] = mantissa & 0xFF ;
6043 in = fmod (in, 1.0) ;
6044 in *= 0x1000000 ;
6045 mantissa = lrint (floor (in)) ;
6047 out [2] = (mantissa >> 16) & 0xFF ;
6048 out [1] = (mantissa >> 8) & 0xFF ;
6049 out [0] = mantissa & 0xFF ;
6051 return ;
6052 } /* double64_le_write */
6054 /*==============================================================================================
6055 ** Private functions.
6058 static void
6059 double64_peak_update (SF_PRIVATE *psf, double *buffer, int count, int indx)
6060 { int chan ;
6061 int k, position ;
6062 float fmaxval ;
6064 for (chan = 0 ; chan < psf->sf.channels ; chan++)
6065 { fmaxval = fabs (buffer [chan]) ;
6066 position = 0 ;
6067 for (k = chan ; k < count ; k += psf->sf.channels)
6068 if (fmaxval < fabs (buffer [k]))
6069 { fmaxval = fabs (buffer [k]) ;
6070 position = k ;
6073 if (fmaxval > psf->pchunk->peaks [chan].value)
6074 { psf->pchunk->peaks [chan].value = fmaxval ;
6075 psf->pchunk->peaks [chan].position = psf->write_current + indx + (position / psf->sf.channels) ;
6079 return ;
6080 } /* double64_peak_update */
6082 static int
6083 double64_get_capability (SF_PRIVATE *psf)
6084 { union
6085 { double d ;
6086 int i [2] ;
6087 unsigned char c [8] ;
6088 } data ;
6090 data.d = 1.234567890123456789 ; /* Some abitrary value. */
6092 if (! psf->ieee_replace)
6093 { /* If this test is true ints and floats are compatible and little endian. */
6094 if (data.i [0] == 0x428c59fb && data.i [1] == 0x3ff3c0ca &&
6095 data.c [0] == 0xfb && data.c [2] == 0x8c && data.c [4] == 0xca && data.c [6] == 0xf3)
6096 return DOUBLE_CAN_RW_LE ;
6098 /* If this test is true ints and floats are compatible and big endian. */
6099 if ((data.i [0] == 0x3ff3c0ca && data.i [1] == 0x428c59fb) &&
6100 (data.c [0] == 0x3f && data.c [2] == 0xc0 && data.c [4] == 0x42 && data.c [6] == 0x59))
6101 return DOUBLE_CAN_RW_BE ;
6104 /* Doubles are broken. Don't expect reading or writing to be fast. */
6105 psf_log_printf (psf, "Using IEEE replacement code for double.\n") ;
6107 return (CPU_IS_LITTLE_ENDIAN) ? DOUBLE_BROKEN_LE : DOUBLE_BROKEN_BE ;
6108 } /* double64_get_capability */
6110 /*----------------------------------------------------------------------------------------------
6114 static sf_count_t
6115 host_read_d2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
6116 { int bufferlen, readcount, thisread ;
6117 sf_count_t total = 0 ;
6119 bufferlen = sizeof (psf->buffer) / sizeof (double) ;
6121 while (len > 0)
6122 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
6123 thisread = psf_fread (psf->buffer, sizeof (double), readcount, psf) ;
6125 if (psf->float_endswap == SF_TRUE)
6126 endswap_long_array ((long*) psf->buffer, readcount) ;
6128 d2s_array ((double*) (psf->buffer), thisread, ptr + total) ;
6129 total += thisread ;
6130 len -= thisread ;
6131 if (thisread < readcount)
6132 break ;
6135 return total ;
6136 } /* host_read_d2s */
6138 static sf_count_t
6139 host_read_d2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
6140 { int bufferlen, readcount, thisread ;
6141 sf_count_t total = 0 ;
6143 bufferlen = sizeof (psf->buffer) / sizeof (double) ;
6145 while (len > 0)
6146 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
6147 thisread = psf_fread (psf->buffer, sizeof (double), readcount, psf) ;
6149 if (psf->float_endswap == SF_TRUE)
6150 endswap_long_array ((long*) psf->buffer, readcount) ;
6152 d2i_array ((double*) (psf->buffer), thisread, ptr + total) ;
6153 total += thisread ;
6154 len -= thisread ;
6155 if (thisread < readcount)
6156 break ;
6159 return total ;
6160 } /* host_read_d2i */
6162 static sf_count_t
6163 host_read_d2f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
6164 { int bufferlen, readcount, thisread ;
6165 sf_count_t total = 0 ;
6167 bufferlen = sizeof (psf->buffer) / sizeof (double) ;
6169 while (len > 0)
6170 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
6171 thisread = psf_fread (psf->buffer, sizeof (double), readcount, psf) ;
6173 if (psf->float_endswap == SF_TRUE)
6174 endswap_long_array ((long*) psf->buffer, readcount) ;
6176 double64d2f_array ((double*) (psf->buffer), thisread, ptr + total) ;
6177 total += thisread ;
6178 len -= thisread ;
6179 if (thisread < readcount)
6180 break ;
6183 return total ;
6184 } /* host_read_d2f */
6186 static sf_count_t
6187 host_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
6188 { int bufferlen, readcount, thisread ;
6189 sf_count_t total = 0 ;
6191 if (psf->float_endswap != SF_TRUE)
6192 return psf_fread (ptr, sizeof (double), len, psf) ;
6194 bufferlen = sizeof (psf->buffer) / sizeof (double) ;
6196 while (len > 0)
6197 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
6198 thisread = psf_fread (psf->buffer, sizeof (double), readcount, psf) ;
6200 endswap_long_copy ((long*) (ptr + total), (long*) psf->buffer, thisread) ;
6202 total += thisread ;
6203 len -= thisread ;
6204 if (thisread < readcount)
6205 break ;
6208 return total ;
6209 } /* host_read_d */
6211 static sf_count_t
6212 host_write_s2d (SF_PRIVATE *psf, short *ptr, sf_count_t len)
6213 { int bufferlen, writecount, thiswrite ;
6214 sf_count_t total = 0 ;
6216 bufferlen = sizeof (psf->buffer) / sizeof (double) ;
6218 while (len > 0)
6219 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
6221 s2d_array (ptr + total, (double*) (psf->buffer), writecount) ;
6223 if (psf->has_peak)
6224 double64_peak_update (psf, (double*) (psf->buffer), writecount, (int) (total / psf->sf.channels)) ;
6226 if (psf->float_endswap == SF_TRUE)
6227 endswap_long_array ((long*) psf->buffer, writecount) ;
6229 thiswrite = psf_fwrite (psf->buffer, sizeof (double), writecount, psf) ;
6230 total += thiswrite ;
6231 len -= thiswrite ;
6232 if (thiswrite < writecount)
6233 break ;
6236 return total ;
6237 } /* host_write_s2d */
6239 static sf_count_t
6240 host_write_i2d (SF_PRIVATE *psf, int *ptr, sf_count_t len)
6241 { int bufferlen, writecount, thiswrite ;
6242 sf_count_t total = 0 ;
6244 bufferlen = sizeof (psf->buffer) / sizeof (double) ;
6246 while (len > 0)
6247 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
6248 i2d_array (ptr + total, (double*) (psf->buffer), writecount) ;
6250 if (psf->has_peak)
6251 double64_peak_update (psf, (double*) (psf->buffer), writecount, (int) (total / psf->sf.channels)) ;
6253 if (psf->float_endswap == SF_TRUE)
6254 endswap_long_array ((long*) psf->buffer, writecount) ;
6256 thiswrite = psf_fwrite (psf->buffer, sizeof (double), writecount, psf) ;
6257 total += thiswrite ;
6258 len -= thiswrite ;
6259 if (thiswrite < writecount)
6260 break ;
6263 return total ;
6264 } /* host_write_i2d */
6266 static sf_count_t
6267 host_write_f2d (SF_PRIVATE *psf, float *ptr, sf_count_t len)
6268 { int bufferlen, writecount, thiswrite ;
6269 sf_count_t total = 0 ;
6271 bufferlen = sizeof (psf->buffer) / sizeof (double) ;
6273 while (len > 0)
6274 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
6275 double64f2d_array (ptr + total, (double*) (psf->buffer), writecount) ;
6277 if (psf->has_peak)
6278 double64_peak_update (psf, (double*) (psf->buffer), writecount, (int) (total / psf->sf.channels)) ;
6280 if (psf->float_endswap == SF_TRUE)
6281 endswap_long_array ((long*) psf->buffer, writecount) ;
6283 thiswrite = psf_fwrite (psf->buffer, sizeof (double), writecount, psf) ;
6284 total += thiswrite ;
6285 len -= thiswrite ;
6286 if (thiswrite < writecount)
6287 break ;
6290 return total ;
6291 } /* host_write_f2d */
6293 static sf_count_t
6294 host_write_d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
6295 { int bufferlen, writecount, thiswrite ;
6296 sf_count_t total = 0 ;
6298 if (psf->has_peak)
6299 double64_peak_update (psf, ptr, len, 0) ;
6301 if (psf->float_endswap != SF_TRUE)
6302 return psf_fwrite (ptr, sizeof (double), len, psf) ;
6304 bufferlen = sizeof (psf->buffer) / sizeof (double) ;
6306 while (len > 0)
6307 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
6309 endswap_long_copy ((long*) psf->buffer, (long*) (ptr + total), writecount) ;
6311 thiswrite = psf_fwrite (psf->buffer, sizeof (double), writecount, psf) ;
6312 total += thiswrite ;
6313 len -= thiswrite ;
6314 if (thiswrite < writecount)
6315 break ;
6318 return total ;
6319 } /* host_write_d */
6321 /*=======================================================================================
6324 static void
6325 d2s_array (double *src, unsigned int count, short *dest)
6326 { while (count)
6327 { count -- ;
6328 dest [count] = lrint (src [count]) ;
6330 } /* d2s_array */
6332 static void
6333 d2i_array (double *src, unsigned int count, int *dest)
6334 { while (count)
6335 { count -- ;
6336 dest [count] = lrint (src [count]) ;
6338 } /* d2i_array */
6340 static void
6341 double64d2f_array (double *src, unsigned int count, float *dest)
6342 { while (count)
6343 { count -- ;
6344 dest [count] = src [count] ;
6346 } /* double64d2f_array */
6348 static void
6349 s2d_array (short *src, double *dest, unsigned int count)
6350 { while (count)
6351 { count -- ;
6352 dest [count] = src [count] ;
6355 } /* s2d_array */
6357 static void
6358 i2d_array (int *src, double *dest, unsigned int count)
6359 { while (count)
6360 { count -- ;
6361 dest [count] = src [count] ;
6363 } /* i2d_array */
6365 static void
6366 double64f2d_array (float *src, double *dest, unsigned int count)
6367 { while (count)
6368 { count -- ;
6369 dest [count] = src [count] ;
6371 } /* double64f2d_array */
6373 /*=======================================================================================
6376 static sf_count_t
6377 replace_read_d2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
6378 { int bufferlen, readcount, thisread ;
6379 sf_count_t total = 0 ;
6381 bufferlen = sizeof (psf->buffer) / sizeof (double) ;
6383 while (len > 0)
6384 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
6385 thisread = psf_fread (psf->buffer, sizeof (double), readcount, psf) ;
6387 if (psf->float_endswap == SF_TRUE)
6388 endswap_long_array ((long*) psf->buffer, readcount) ;
6390 d2bd_read ((double *) (psf->buffer), readcount) ;
6392 d2s_array ((double*) (psf->buffer), thisread, ptr + total) ;
6393 total += thisread ;
6394 if (thisread < readcount)
6395 break ;
6396 len -= thisread ;
6399 return total ;
6400 } /* replace_read_d2s */
6402 static sf_count_t
6403 replace_read_d2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
6404 { int bufferlen, readcount, thisread ;
6405 sf_count_t total = 0 ;
6407 bufferlen = sizeof (psf->buffer) / sizeof (double) ;
6409 while (len > 0)
6410 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
6411 thisread = psf_fread (psf->buffer, sizeof (double), readcount, psf) ;
6413 if (psf->float_endswap == SF_TRUE)
6414 endswap_long_array ((long*) psf->buffer, readcount) ;
6416 d2bd_read ((double *) (psf->buffer), readcount) ;
6418 d2i_array ((double*) (psf->buffer), thisread, ptr + total) ;
6419 total += thisread ;
6420 if (thisread < readcount)
6421 break ;
6422 len -= thisread ;
6425 return total ;
6426 } /* replace_read_d2i */
6428 static sf_count_t
6429 replace_read_d2f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
6430 { int bufferlen, readcount, thisread ;
6431 sf_count_t total = 0 ;
6433 bufferlen = sizeof (psf->buffer) / sizeof (double) ;
6435 while (len > 0)
6436 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
6437 thisread = psf_fread (psf->buffer, sizeof (double), readcount, psf) ;
6439 if (psf->float_endswap == SF_TRUE)
6440 endswap_long_array ((long *) psf->buffer, readcount) ;
6442 d2bd_read ((double *) (psf->buffer), readcount) ;
6444 memcpy (ptr + total, psf->buffer, readcount * sizeof (double)) ;
6446 total += thisread ;
6447 if (thisread < readcount)
6448 break ;
6449 len -= thisread ;
6452 return total ;
6453 } /* replace_read_d2f */
6455 static sf_count_t
6456 replace_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
6457 { int bufferlen, readcount, thisread ;
6458 sf_count_t total = 0 ;
6460 /* FIXME : This is probably nowhere near optimal. */
6461 bufferlen = sizeof (psf->buffer) / sizeof (double) ;
6463 while (len > 0)
6464 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
6465 thisread = psf_fread (psf->buffer, sizeof (double), readcount, psf) ;
6467 if (psf->float_endswap == SF_TRUE)
6468 endswap_long_array ((long*) psf->buffer, thisread) ;
6470 d2bd_read ((double *) (psf->buffer), thisread) ;
6472 memcpy (ptr + total, psf->buffer, thisread * sizeof (double)) ;
6474 total += thisread ;
6475 if (thisread < readcount)
6476 break ;
6477 len -= thisread ;
6480 return total ;
6481 } /* replace_read_d */
6483 static sf_count_t
6484 replace_write_s2d (SF_PRIVATE *psf, short *ptr, sf_count_t len)
6485 { int writecount, bufferlen, thiswrite ;
6486 sf_count_t total = 0 ;
6488 bufferlen = sizeof (psf->buffer) / sizeof (double) ;
6490 while (len > 0)
6491 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
6492 s2d_array (ptr + total, (double *) (psf->buffer), writecount) ;
6494 if (psf->has_peak)
6495 double64_peak_update (psf, (double *) (psf->buffer), writecount, (int) (total / psf->sf.channels)) ;
6497 bd2d_write ((double *) (psf->buffer), writecount) ;
6499 if (psf->float_endswap == SF_TRUE)
6500 endswap_long_array ((long*) psf->buffer, writecount) ;
6502 thiswrite = psf_fwrite (psf->buffer, sizeof (double), writecount, psf) ;
6503 total += thiswrite ;
6504 if (thiswrite < writecount)
6505 break ;
6506 len -= thiswrite ;
6509 return total ;
6510 } /* replace_write_s2d */
6512 static sf_count_t
6513 replace_write_i2d (SF_PRIVATE *psf, int *ptr, sf_count_t len)
6514 { int writecount, bufferlen, thiswrite ;
6515 sf_count_t total = 0 ;
6517 bufferlen = sizeof (psf->buffer) / sizeof (double) ;
6519 while (len > 0)
6520 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
6521 i2d_array (ptr + total, (double*) (psf->buffer), writecount) ;
6523 if (psf->has_peak)
6524 double64_peak_update (psf, (double *) (psf->buffer), writecount, (int) (total / psf->sf.channels)) ;
6526 bd2d_write ((double *) (psf->buffer), writecount) ;
6528 if (psf->float_endswap == SF_TRUE)
6529 endswap_long_array ((long*) psf->buffer, writecount) ;
6531 thiswrite = psf_fwrite (psf->buffer, sizeof (double), writecount, psf) ;
6532 total += thiswrite ;
6533 if (thiswrite < writecount)
6534 break ;
6535 len -= thiswrite ;
6538 return total ;
6539 } /* replace_write_i2d */
6541 static sf_count_t
6542 replace_write_f2d (SF_PRIVATE *psf, float *ptr, sf_count_t len)
6543 { int writecount, bufferlen, thiswrite ;
6544 sf_count_t total = 0 ;
6546 bufferlen = sizeof (psf->buffer) / sizeof (double) ;
6548 while (len > 0)
6549 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
6550 double64f2d_array (ptr + total, (double*) (psf->buffer), writecount) ;
6552 bd2d_write ((double *) (psf->buffer), writecount) ;
6554 if (psf->float_endswap == SF_TRUE)
6555 endswap_long_array ((long*) psf->buffer, writecount) ;
6557 thiswrite = psf_fwrite (psf->buffer, sizeof (double), writecount, psf) ;
6558 total += thiswrite ;
6559 if (thiswrite < writecount)
6560 break ;
6561 len -= thiswrite ;
6564 return total ;
6565 } /* replace_write_f2d */
6567 static sf_count_t
6568 replace_write_d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
6569 { int writecount, bufferlen, thiswrite ;
6570 sf_count_t total = 0 ;
6572 /* FIXME : This is probably nowhere near optimal. */
6573 if (psf->has_peak)
6574 double64_peak_update (psf, ptr, len, 0) ;
6576 bufferlen = sizeof (psf->buffer) / sizeof (double) ;
6578 while (len > 0)
6579 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
6581 memcpy (psf->buffer, ptr + total, writecount * sizeof (double)) ;
6583 bd2d_write ((double *) (psf->buffer), writecount) ;
6585 if (psf->float_endswap == SF_TRUE)
6586 endswap_long_array ((long*) psf->buffer, writecount) ;
6588 thiswrite = psf_fwrite (psf->buffer, sizeof (double), writecount, psf) ;
6589 total += thiswrite ;
6590 if (thiswrite < writecount)
6591 break ;
6592 len -= thiswrite ;
6595 return total ;
6596 } /* replace_write_d */
6598 /*----------------------------------------------------------------------------------------------
6601 static void
6602 d2bd_read (double *buffer, int count)
6603 { while (count)
6604 { count -- ;
6605 buffer [count] = DOUBLE64_READ ((unsigned char *) (buffer + count)) ;
6607 } /* d2bd_read */
6609 static void
6610 bd2d_write (double *buffer, int count)
6611 { while (count)
6612 { count -- ;
6613 DOUBLE64_WRITE (buffer [count], (unsigned char*) (buffer + count)) ;
6615 } /* bd2d_write */
6618 ** Do not edit or modify anything in this comment block.
6619 ** The arch-tag line is a file identity tag for the GNU Arch
6620 ** revision control system.
6622 ** arch-tag: 4ee243b7-8c7a-469b-869c-e9aa0ee3b77f
6625 ** Copyright (C) 2002-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
6627 ** This program is free software; you can redistribute it and/or modify
6628 ** it under the terms of the GNU Lesser General Public License as published by
6629 ** the Free Software Foundation; either version 2.1 of the License, or
6630 ** (at your option) any later version.
6632 ** This program is distributed in the hope that it will be useful,
6633 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
6634 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6635 ** GNU Lesser General Public License for more details.
6637 ** You should have received a copy of the GNU Lesser General Public License
6638 ** along with this program; if not, write to the Free Software
6639 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
6642 #include <stdio.h>
6643 #include <fcntl.h>
6644 #include <string.h>
6645 #include <ctype.h>
6648 #if (ENABLE_EXPERIMENTAL_CODE == 0)
6651 dwd_open (SF_PRIVATE *psf)
6652 { if (psf)
6653 return SFE_UNIMPLEMENTED ;
6654 return (psf && 0) ;
6655 } /* dwd_open */
6657 #else
6659 /*------------------------------------------------------------------------------
6660 ** Macros to handle big/little endian issues.
6663 #define SFE_DWD_NO_DWD 1666
6664 #define SFE_DWD_BAND_BIT_WIDTH 1667
6665 #define SFE_DWD_COMPRESSION 1668
6667 #define DWD_IDENTIFIER "DiamondWare Digitized\n\0\x1a"
6668 #define DWD_IDENTIFIER_LEN 24
6670 #define DWD_HEADER_LEN 57
6672 /*------------------------------------------------------------------------------
6673 ** Typedefs.
6676 /*------------------------------------------------------------------------------
6677 ** Private static functions.
6680 static int dwd_read_header (SF_PRIVATE *psf) ;
6682 static int dwd_close (SF_PRIVATE *psf) ;
6684 /*------------------------------------------------------------------------------
6685 ** Public function.
6689 dwd_open (SF_PRIVATE *psf)
6690 { int subformat, error = 0 ;
6692 if (psf->mode == SFM_READ || (psf->mode == SFM_RDWR && psf->filelength > 0))
6693 { if ((error = dwd_read_header (psf)))
6694 return error ;
6697 if ((psf->sf.format & SF_FORMAT_TYPEMASK) != SF_FORMAT_DWD)
6698 return SFE_BAD_OPEN_FORMAT ;
6700 subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
6702 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
6704 /*-psf->endian = psf->sf.format & SF_FORMAT_ENDMASK ;
6705 if (CPU_IS_LITTLE_ENDIAN && psf->endian == SF_ENDIAN_CPU)
6706 psf->endian = SF_ENDIAN_LITTLE ;
6707 else if (psf->endian != SF_ENDIAN_LITTLE)
6708 psf->endian = SF_ENDIAN_BIG ;
6710 if (! (encoding = dwd_write_header (psf, SF_FALSE)))
6711 return psf->error ;
6713 psf->write_header = dwd_write_header ;
6717 psf->close = dwd_close ;
6719 /*-psf->blockwidth = psf->bytewidth * psf->sf.channels ;-*/
6721 return error ;
6722 } /* dwd_open */
6724 /*------------------------------------------------------------------------------
6727 static int
6728 dwd_close (SF_PRIVATE *psf)
6730 psf = psf ;
6732 return 0 ;
6733 } /* dwd_close */
6735 /* This struct contains all the fields of interest om the DWD header, but does not
6736 ** do so in the same order and layout as the actual file, header.
6737 ** No assumptions are made about the packing of this struct.
6739 typedef struct
6740 { unsigned char major, minor, compression, channels, bitwidth ;
6741 unsigned short srate, maxval ;
6742 unsigned int id, datalen, frames, offset ;
6743 } DWD_HEADER ;
6745 static int
6746 dwd_read_header (SF_PRIVATE *psf)
6747 { DWD_HEADER dwdh ;
6749 memset (psf->buffer, 0, sizeof (psf->buffer)) ;
6750 /* Set position to start of file to begin reading header. */
6751 psf_binheader_readf (psf, "pb", 0, psf->buffer, DWD_IDENTIFIER_LEN) ;
6753 if (memcmp (psf->buffer, DWD_IDENTIFIER, DWD_IDENTIFIER_LEN) != 0)
6754 return SFE_DWD_NO_DWD ;
6756 psf_log_printf (psf, "Read only : DiamondWare Digitized (.dwd)\n", psf->buffer) ;
6758 psf_binheader_readf (psf, "11", &dwdh.major, &dwdh.minor) ;
6759 psf_binheader_readf (psf, "e4j1", &dwdh.id, 1, &dwdh.compression) ;
6760 psf_binheader_readf (psf, "e211", &dwdh.srate, &dwdh.channels, &dwdh.bitwidth) ;
6761 psf_binheader_readf (psf, "e24", &dwdh.maxval, &dwdh.datalen) ;
6762 psf_binheader_readf (psf, "e44", &dwdh.frames, &dwdh.offset) ;
6764 psf_log_printf (psf, " Version Major : %d\n Version Minor : %d\n Unique ID : %08X\n",
6765 dwdh.major, dwdh.minor, dwdh.id) ;
6766 psf_log_printf (psf, " Compression : %d => ", dwdh.compression) ;
6768 if (dwdh.compression != 0)
6769 { psf_log_printf (psf, "Unsupported compression\n") ;
6770 return SFE_DWD_COMPRESSION ;
6772 else
6773 psf_log_printf (psf, "None\n") ;
6775 psf_log_printf (psf, " Sample Rate : %d\n Channels : %d\n"
6776 " Bit Width : %d\n",
6777 dwdh.srate, dwdh.channels, dwdh.bitwidth) ;
6779 switch (dwdh.bitwidth)
6780 { case 8 :
6781 psf->sf.format = SF_FORMAT_DWD | SF_FORMAT_PCM_S8 ;
6782 psf->bytewidth = 1 ;
6783 break ;
6785 case 16 :
6786 psf->sf.format = SF_FORMAT_DWD | SF_FORMAT_PCM_16 ;
6787 psf->bytewidth = 2 ;
6788 break ;
6790 default :
6791 psf_log_printf (psf, "*** Bad bit width %d\n", dwdh.bitwidth) ;
6792 return SFE_DWD_BAND_BIT_WIDTH ;
6795 if (psf->filelength != dwdh.offset + dwdh.datalen)
6796 { psf_log_printf (psf, " Data Length : %d (should be %D)\n", dwdh.datalen, psf->filelength - dwdh.offset) ;
6797 dwdh.datalen = (unsigned int) (psf->filelength - dwdh.offset) ;
6799 else
6800 psf_log_printf (psf, " Data Length : %d\n", dwdh.datalen) ;
6802 psf_log_printf (psf, " Max Value : %d\n", dwdh.maxval) ;
6803 psf_log_printf (psf, " Frames : %d\n", dwdh.frames) ;
6804 psf_log_printf (psf, " Data Offset : %d\n", dwdh.offset) ;
6806 psf->datalength = dwdh.datalen ;
6807 psf->dataoffset = dwdh.offset ;
6809 psf->endian = SF_ENDIAN_LITTLE ;
6811 psf->sf.samplerate = dwdh.srate ;
6812 psf->sf.channels = dwdh.channels ;
6813 psf->sf.sections = 1 ;
6815 return pcm_init (psf) ;
6816 } /* dwd_read_header */
6818 /*------------------------------------------------------------------------------
6821 #endif
6823 ** Do not edit or modify anything in this comment block.
6824 ** The arch-tag line is a file identity tag for the GNU Arch
6825 ** revision control system.
6827 ** arch-tag: a5e1d2a6-a840-4039-a0e7-e1a43eb05a4f
6830 ** Copyright (C) 2002-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
6832 ** This program is free software; you can redistribute it and/or modify
6833 ** it under the terms of the GNU Lesser General Public License as published by
6834 ** the Free Software Foundation; either version 2.1 of the License, or
6835 ** (at your option) any later version.
6837 ** This program is distributed in the hope that it will be useful,
6838 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
6839 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6840 ** GNU Lesser General Public License for more details.
6842 ** You should have received a copy of the GNU Lesser General Public License
6843 ** along with this program; if not, write to the Free Software
6844 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
6847 /*===========================================================================
6848 ** Delta Word Variable Width
6850 ** This decoder and encoder were implemented using information found in this
6851 ** document : http://home.swbell.net/rubywand/R011SNDFMTS.TXT
6853 ** According to the document, the algorithm "was invented 1991 by Magnus
6854 ** Lidstrom and is copyright 1993 by NuEdge Development".
6857 #include <stdio.h>
6858 #include <stdlib.h>
6859 #include <string.h>
6862 typedef struct
6863 { int dwm_maxsize, bit_width, max_delta, span ;
6864 int samplecount ;
6865 int bit_count, bits, last_delta_width, last_sample ;
6866 struct
6867 { int index, end ;
6868 unsigned char buffer [256] ;
6869 } b ;
6870 } DWVW_PRIVATE ;
6872 /*============================================================================================
6875 static sf_count_t dwvw_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
6876 static sf_count_t dwvw_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
6877 static sf_count_t dwvw_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
6878 static sf_count_t dwvw_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
6880 static sf_count_t dwvw_write_s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
6881 static sf_count_t dwvw_write_i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
6882 static sf_count_t dwvw_write_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
6883 static sf_count_t dwvw_write_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
6885 static sf_count_t dwvw_seek (SF_PRIVATE *psf, int mode, sf_count_t offset) ;
6886 static int dwvw_close (SF_PRIVATE *psf) ;
6888 static int dwvw_decode_data (SF_PRIVATE *psf, DWVW_PRIVATE *pdwvw, int *ptr, int len) ;
6889 static int dwvw_decode_load_bits (SF_PRIVATE *psf, DWVW_PRIVATE *pdwvw, int bit_count) ;
6891 static int dwvw_encode_data (SF_PRIVATE *psf, DWVW_PRIVATE *pdwvw, int *ptr, int len) ;
6892 static void dwvw_encode_store_bits (SF_PRIVATE *psf, DWVW_PRIVATE *pdwvw, int data, int new_bits) ;
6893 static void dwvw_read_reset (DWVW_PRIVATE *pdwvw) ;
6895 /*============================================================================================
6896 ** DWVW initialisation function.
6900 dwvw_init (SF_PRIVATE *psf, int bitwidth)
6901 { DWVW_PRIVATE *pdwvw ;
6903 if (bitwidth > 24)
6904 return SFE_DWVW_BAD_BITWIDTH ;
6906 if (psf->mode == SFM_RDWR)
6907 return SFE_BAD_MODE_RW ;
6909 if ((pdwvw = calloc (1, sizeof (DWVW_PRIVATE))) == NULL)
6910 return SFE_MALLOC_FAILED ;
6912 psf->fdata = (void*) pdwvw ;
6914 pdwvw->bit_width = bitwidth ;
6915 pdwvw->dwm_maxsize = bitwidth / 2 ;
6916 pdwvw->max_delta = 1 << (bitwidth - 1) ;
6917 pdwvw->span = 1 << bitwidth ;
6919 dwvw_read_reset (pdwvw) ;
6921 if (psf->mode == SFM_READ)
6922 { psf->read_short = dwvw_read_s ;
6923 psf->read_int = dwvw_read_i ;
6924 psf->read_float = dwvw_read_f ;
6925 psf->read_double = dwvw_read_d ;
6928 if (psf->mode == SFM_WRITE)
6929 { psf->write_short = dwvw_write_s ;
6930 psf->write_int = dwvw_write_i ;
6931 psf->write_float = dwvw_write_f ;
6932 psf->write_double = dwvw_write_d ;
6935 psf->seek = dwvw_seek ;
6936 psf->close = dwvw_close ;
6938 /* FIXME : This s bogus. */
6939 psf->sf.frames = SF_COUNT_MAX ;
6940 psf->datalength = psf->sf.frames ;
6941 /* EMXIF : This s bogus. */
6943 return 0 ;
6944 } /* dwvw_init */
6946 /*--------------------------------------------------------------------------------------------
6949 static int
6950 dwvw_close (SF_PRIVATE *psf)
6951 { DWVW_PRIVATE *pdwvw ;
6953 if (psf->fdata == NULL)
6954 return 0 ;
6955 pdwvw = (DWVW_PRIVATE*) psf->fdata ;
6957 if (psf->mode == SFM_WRITE)
6958 { static int last_values [12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } ;
6960 /* Write 8 zero samples to fully flush output. */
6961 dwvw_encode_data (psf, pdwvw, last_values, 12) ;
6963 /* Write the last buffer worth of data to disk. */
6964 psf_fwrite (pdwvw->b.buffer, 1, pdwvw->b.index, psf) ;
6966 if (psf->write_header)
6967 psf->write_header (psf, SF_TRUE) ;
6970 return 0 ;
6971 } /* dwvw_close */
6973 static sf_count_t
6974 dwvw_seek (SF_PRIVATE *psf, int mode, sf_count_t offset)
6975 { DWVW_PRIVATE *pdwvw ;
6977 mode = mode ;
6979 if (! psf->fdata)
6980 { psf->error = SFE_INTERNAL ;
6981 return ((sf_count_t) -1) ;
6984 pdwvw = (DWVW_PRIVATE*) psf->fdata ;
6986 if (offset == 0)
6987 { psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
6988 dwvw_read_reset (pdwvw) ;
6989 return 0 ;
6992 psf->error = SFE_BAD_SEEK ;
6993 return ((sf_count_t) -1) ;
6994 } /* dwvw_seek */
6997 /*==============================================================================
7000 static sf_count_t
7001 dwvw_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
7002 { DWVW_PRIVATE *pdwvw ;
7003 int *iptr ;
7004 int k, bufferlen, readcount = 0, count ;
7005 sf_count_t total = 0 ;
7007 if (! psf->fdata)
7008 return 0 ;
7009 pdwvw = (DWVW_PRIVATE*) psf->fdata ;
7011 iptr = (int*) psf->buffer ;
7012 bufferlen = SF_BUFFER_LEN / sizeof (int) ;
7013 while (len > 0)
7014 { readcount = (len >= bufferlen) ? bufferlen : len ;
7015 count = dwvw_decode_data (psf, pdwvw, iptr, readcount) ;
7016 for (k = 0 ; k < readcount ; k++)
7017 ptr [total + k] = iptr [k] >> 16 ;
7019 total += count ;
7020 len -= readcount ;
7021 if (count != readcount)
7022 break ;
7025 return total ;
7026 } /* dwvw_read_s */
7028 static sf_count_t
7029 dwvw_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
7030 { DWVW_PRIVATE *pdwvw ;
7031 int readcount, count ;
7032 sf_count_t total = 0 ;
7034 if (! psf->fdata)
7035 return 0 ;
7036 pdwvw = (DWVW_PRIVATE*) psf->fdata ;
7038 while (len > 0)
7039 { readcount = (len > 0x10000000) ? 0x10000000 : (int) len ;
7041 count = dwvw_decode_data (psf, pdwvw, ptr, readcount) ;
7043 total += count ;
7044 len -= count ;
7046 if (count != readcount)
7047 break ;
7050 return total ;
7051 } /* dwvw_read_i */
7053 static sf_count_t
7054 dwvw_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
7055 { DWVW_PRIVATE *pdwvw ;
7056 int *iptr ;
7057 int k, bufferlen, readcount = 0, count ;
7058 sf_count_t total = 0 ;
7059 float normfact ;
7061 if (! psf->fdata)
7062 return 0 ;
7063 pdwvw = (DWVW_PRIVATE*) psf->fdata ;
7065 normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x80000000) : 1.0 ;
7067 iptr = (int*) psf->buffer ;
7068 bufferlen = SF_BUFFER_LEN / sizeof (int) ;
7069 while (len > 0)
7070 { readcount = (len >= bufferlen) ? bufferlen : len ;
7071 count = dwvw_decode_data (psf, pdwvw, iptr, readcount) ;
7072 for (k = 0 ; k < readcount ; k++)
7073 ptr [total + k] = normfact * (float) (iptr [k]) ;
7075 total += count ;
7076 len -= readcount ;
7077 if (count != readcount)
7078 break ;
7081 return total ;
7082 } /* dwvw_read_f */
7084 static sf_count_t
7085 dwvw_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
7086 { DWVW_PRIVATE *pdwvw ;
7087 int *iptr ;
7088 int k, bufferlen, readcount = 0, count ;
7089 sf_count_t total = 0 ;
7090 double normfact ;
7092 if (! psf->fdata)
7093 return 0 ;
7094 pdwvw = (DWVW_PRIVATE*) psf->fdata ;
7096 normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x80000000) : 1.0 ;
7098 iptr = (int*) psf->buffer ;
7099 bufferlen = SF_BUFFER_LEN / sizeof (int) ;
7100 while (len > 0)
7101 { readcount = (len >= bufferlen) ? bufferlen : len ;
7102 count = dwvw_decode_data (psf, pdwvw, iptr, readcount) ;
7103 for (k = 0 ; k < readcount ; k++)
7104 ptr [total + k] = normfact * (double) (iptr [k]) ;
7106 total += count ;
7107 len -= readcount ;
7108 if (count != readcount)
7109 break ;
7112 return total ;
7113 } /* dwvw_read_d */
7115 static int
7116 dwvw_decode_data (SF_PRIVATE *psf, DWVW_PRIVATE *pdwvw, int *ptr, int len)
7117 { int count ;
7118 int delta_width_modifier, delta_width, delta_negative, delta, sample ;
7120 /* Restore state from last decode call. */
7121 delta_width = pdwvw->last_delta_width ;
7122 sample = pdwvw->last_sample ;
7124 for (count = 0 ; count < len ; count++)
7125 { /* If bit_count parameter is zero get the delta_width_modifier. */
7126 delta_width_modifier = dwvw_decode_load_bits (psf, pdwvw, -1) ;
7128 /* Check for end of input bit stream. Break loop if end. */
7129 if (delta_width_modifier < 0)
7130 break ;
7132 if (delta_width_modifier && dwvw_decode_load_bits (psf, pdwvw, 1))
7133 delta_width_modifier = - delta_width_modifier ;
7135 /* Calculate the current word width. */
7136 delta_width = (delta_width + delta_width_modifier + pdwvw->bit_width) % pdwvw->bit_width ;
7138 /* Load the delta. */
7139 delta = 0 ;
7140 if (delta_width)
7141 { delta = dwvw_decode_load_bits (psf, pdwvw, delta_width - 1) | (1 << (delta_width - 1)) ;
7142 delta_negative = dwvw_decode_load_bits (psf, pdwvw, 1) ;
7143 if (delta == pdwvw->max_delta - 1)
7144 delta += dwvw_decode_load_bits (psf, pdwvw, 1) ;
7145 if (delta_negative)
7146 delta = -delta ;
7149 /* Calculate the sample */
7150 sample += delta ;
7152 if (sample >= pdwvw->max_delta)
7153 sample -= pdwvw->span ;
7154 else if (sample < - pdwvw->max_delta)
7155 sample += pdwvw->span ;
7157 /* Store the sample justifying to the most significant bit. */
7158 ptr [count] = sample << (32 - pdwvw->bit_width) ;
7160 if (pdwvw->b.end == 0 && pdwvw->bit_count == 0)
7161 break ;
7164 pdwvw->last_delta_width = delta_width ;
7165 pdwvw->last_sample = sample ;
7167 pdwvw->samplecount += count ;
7169 return count ;
7170 } /* dwvw_decode_data */
7172 static int
7173 dwvw_decode_load_bits (SF_PRIVATE *psf, DWVW_PRIVATE *pdwvw, int bit_count)
7174 { int output = 0, get_dwm = SF_FALSE ;
7177 ** Depending on the value of parameter bit_count, either get the
7178 ** required number of bits (ie bit_count > 0) or the
7179 ** delta_width_modifier (otherwise).
7182 if (bit_count < 0)
7183 { get_dwm = SF_TRUE ;
7184 /* modify bit_count to ensure we have enought bits for finding dwm. */
7185 bit_count = pdwvw->dwm_maxsize ;
7188 /* Load bits in bit reseviour. */
7189 while (pdwvw->bit_count < bit_count)
7190 { if (pdwvw->b.index >= pdwvw->b.end)
7191 { pdwvw->b.end = psf_fread (pdwvw->b.buffer, 1, sizeof (pdwvw->b.buffer), psf) ;
7192 pdwvw->b.index = 0 ;
7195 /* Check for end of input stream. */
7196 if (bit_count < 8 && pdwvw->b.end == 0)
7197 return -1 ;
7199 pdwvw->bits = (pdwvw->bits << 8) ;
7201 if (pdwvw->b.index < pdwvw->b.end)
7202 { pdwvw->bits |= pdwvw->b.buffer [pdwvw->b.index] ;
7203 pdwvw->b.index ++ ;
7205 pdwvw->bit_count += 8 ;
7208 /* If asked to get bits do so. */
7209 if (! get_dwm)
7210 { output = (pdwvw->bits >> (pdwvw->bit_count - bit_count)) & ((1 << bit_count) - 1) ;
7211 pdwvw->bit_count -= bit_count ;
7212 return output ;
7215 /* Otherwise must have been asked to get delta_width_modifier. */
7216 while (output < (pdwvw->dwm_maxsize))
7217 { pdwvw->bit_count -= 1 ;
7218 if (pdwvw->bits & (1 << pdwvw->bit_count))
7219 break ;
7220 output += 1 ;
7223 return output ;
7224 } /* dwvw_decode_load_bits */
7226 static void
7227 dwvw_read_reset (DWVW_PRIVATE *pdwvw)
7228 { pdwvw->samplecount = 0 ;
7229 pdwvw->b.index = 0 ;
7230 pdwvw->b.end = 0 ;
7231 pdwvw->bit_count = 0 ;
7232 pdwvw->bits = 0 ;
7233 pdwvw->last_delta_width = 0 ;
7234 pdwvw->last_sample = 0 ;
7235 } /* dwvw_read_reset */
7237 static void
7238 dwvw_encode_store_bits (SF_PRIVATE *psf, DWVW_PRIVATE *pdwvw, int data, int new_bits)
7239 { int byte ;
7241 /* Shift the bits into the resevoir. */
7242 pdwvw->bits = (pdwvw->bits << new_bits) | (data & ((1 << new_bits) - 1)) ;
7243 pdwvw->bit_count += new_bits ;
7245 /* Transfer bit to buffer. */
7246 while (pdwvw->bit_count >= 8)
7247 { byte = pdwvw->bits >> (pdwvw->bit_count - 8) ;
7248 pdwvw->bit_count -= 8 ;
7249 pdwvw->b.buffer [pdwvw->b.index] = byte & 0xFF ;
7250 pdwvw->b.index ++ ;
7253 if (pdwvw->b.index > SIGNED_SIZEOF (pdwvw->b.buffer) - 4)
7254 { psf_fwrite (pdwvw->b.buffer, 1, pdwvw->b.index, psf) ;
7255 pdwvw->b.index = 0 ;
7258 return ;
7259 } /* dwvw_encode_store_bits */
7261 #if 0
7262 /* Debigging routine. */
7263 static void
7264 dump_bits (DWVW_PRIVATE *pdwvw)
7265 { int k, mask ;
7267 for (k = 0 ; k < 10 && k < pdwvw->b.index ; k++)
7268 { mask = 0x80 ;
7269 while (mask)
7270 { putchar (mask & pdwvw->b.buffer [k] ? '1' : '0') ;
7271 mask >>= 1 ;
7273 putchar (' ') ;
7276 for (k = pdwvw->bit_count - 1 ; k >= 0 ; k --)
7277 putchar (pdwvw->bits & (1 << k) ? '1' : '0') ;
7279 putchar ('\n') ;
7280 } /* dump_bits */
7281 #endif
7283 #define HIGHEST_BIT(x,count) \
7284 { int y = x ; \
7285 (count) = 0 ; \
7286 while (y) \
7287 { (count) ++ ; \
7288 y >>= 1 ; \
7289 } ; \
7292 static int
7293 dwvw_encode_data (SF_PRIVATE *psf, DWVW_PRIVATE *pdwvw, int *ptr, int len)
7294 { int count ;
7295 int delta_width_modifier, delta, delta_negative, delta_width, extra_bit ;
7297 for (count = 0 ; count < len ; count++)
7298 { delta = (ptr [count] >> (32 - pdwvw->bit_width)) - pdwvw->last_sample ;
7300 /* Calculate extra_bit if needed. */
7301 extra_bit = -1 ;
7302 delta_negative = 0 ;
7303 if (delta < -pdwvw->max_delta)
7304 delta = pdwvw->max_delta + (delta % pdwvw->max_delta) ;
7305 else if (delta == -pdwvw->max_delta)
7306 { extra_bit = 1 ;
7307 delta_negative = 1 ;
7308 delta = pdwvw->max_delta - 1 ;
7310 else if (delta > pdwvw->max_delta)
7311 { delta_negative = 1 ;
7312 delta = pdwvw->span - delta ;
7313 delta = abs (delta) ;
7315 else if (delta == pdwvw->max_delta)
7316 { extra_bit = 1 ;
7317 delta = pdwvw->max_delta - 1 ;
7319 else if (delta < 0)
7320 { delta_negative = 1 ;
7321 delta = abs (delta) ;
7324 if (delta == pdwvw->max_delta - 1 && extra_bit == -1)
7325 extra_bit = 0 ;
7327 /* Find width in bits of delta */
7328 HIGHEST_BIT (delta, delta_width) ;
7330 /* Calculate the delta_width_modifier */
7331 delta_width_modifier = (delta_width - pdwvw->last_delta_width) % pdwvw->bit_width ;
7332 if (delta_width_modifier > pdwvw->dwm_maxsize)
7333 delta_width_modifier -= pdwvw->bit_width ;
7334 if (delta_width_modifier < -pdwvw->dwm_maxsize)
7335 delta_width_modifier += pdwvw->bit_width ;
7337 /* Write delta_width_modifier zeros, followed by terminating '1'. */
7338 dwvw_encode_store_bits (psf, pdwvw, 0, abs (delta_width_modifier)) ;
7339 if (abs (delta_width_modifier) != pdwvw->dwm_maxsize)
7340 dwvw_encode_store_bits (psf, pdwvw, 1, 1) ;
7342 /* Write delta_width_modifier sign. */
7343 if (delta_width_modifier < 0)
7344 dwvw_encode_store_bits (psf, pdwvw, 1, 1) ;
7345 if (delta_width_modifier > 0)
7346 dwvw_encode_store_bits (psf, pdwvw, 0, 1) ;
7348 /* Write delta and delta sign bit. */
7349 if (delta_width)
7350 { dwvw_encode_store_bits (psf, pdwvw, delta, abs (delta_width) - 1) ;
7351 dwvw_encode_store_bits (psf, pdwvw, (delta_negative ? 1 : 0), 1) ;
7354 /* Write extra bit!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
7355 if (extra_bit >= 0)
7356 dwvw_encode_store_bits (psf, pdwvw, extra_bit, 1) ;
7358 pdwvw->last_sample = ptr [count] >> (32 - pdwvw->bit_width) ;
7359 pdwvw->last_delta_width = delta_width ;
7362 pdwvw->samplecount += count ;
7364 return count ;
7365 } /* dwvw_encode_data */
7367 static sf_count_t
7368 dwvw_write_s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
7369 { DWVW_PRIVATE *pdwvw ;
7370 int *iptr ;
7371 int k, bufferlen, writecount = 0, count ;
7372 sf_count_t total = 0 ;
7374 if (! psf->fdata)
7375 return 0 ;
7376 pdwvw = (DWVW_PRIVATE*) psf->fdata ;
7378 iptr = (int*) psf->buffer ;
7379 bufferlen = SF_BUFFER_LEN / sizeof (int) ;
7380 while (len > 0)
7381 { writecount = (len >= bufferlen) ? bufferlen : len ;
7382 for (k = 0 ; k < writecount ; k++)
7383 iptr [k] = ptr [total + k] << 16 ;
7384 count = dwvw_encode_data (psf, pdwvw, iptr, writecount) ;
7386 total += count ;
7387 len -= writecount ;
7388 if (count != writecount)
7389 break ;
7392 return total ;
7393 } /* dwvw_write_s */
7395 static sf_count_t
7396 dwvw_write_i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
7397 { DWVW_PRIVATE *pdwvw ;
7398 int writecount, count ;
7399 sf_count_t total = 0 ;
7401 if (! psf->fdata)
7402 return 0 ;
7403 pdwvw = (DWVW_PRIVATE*) psf->fdata ;
7405 while (len > 0)
7406 { writecount = (len > 0x10000000) ? 0x10000000 : (int) len ;
7408 count = dwvw_encode_data (psf, pdwvw, ptr, writecount) ;
7410 total += count ;
7411 len -= count ;
7413 if (count != writecount)
7414 break ;
7417 return total ;
7418 } /* dwvw_write_i */
7420 static sf_count_t
7421 dwvw_write_f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
7422 { DWVW_PRIVATE *pdwvw ;
7423 int *iptr ;
7424 int k, bufferlen, writecount = 0, count ;
7425 sf_count_t total = 0 ;
7426 float normfact ;
7428 if (! psf->fdata)
7429 return 0 ;
7430 pdwvw = (DWVW_PRIVATE*) psf->fdata ;
7432 normfact = (psf->norm_float == SF_TRUE) ? (1.0 * 0x7FFFFFFF) : 1.0 ;
7434 iptr = (int*) psf->buffer ;
7435 bufferlen = SF_BUFFER_LEN / sizeof (short) ;
7436 while (len > 0)
7437 { writecount = (len >= bufferlen) ? bufferlen : len ;
7438 for (k = 0 ; k < writecount ; k++)
7439 iptr [k] = lrintf (normfact * ptr [total + k]) ;
7440 count = dwvw_encode_data (psf, pdwvw, iptr, writecount) ;
7442 total += count ;
7443 len -= writecount ;
7444 if (count != writecount)
7445 break ;
7448 return total ;
7449 } /* dwvw_write_f */
7451 static sf_count_t
7452 dwvw_write_d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
7453 { DWVW_PRIVATE *pdwvw ;
7454 int *iptr ;
7455 int k, bufferlen, writecount = 0, count ;
7456 sf_count_t total = 0 ;
7457 double normfact ;
7459 if (! psf->fdata)
7460 return 0 ;
7461 pdwvw = (DWVW_PRIVATE*) psf->fdata ;
7463 normfact = (psf->norm_double == SF_TRUE) ? (1.0 * 0x7FFFFFFF) : 1.0 ;
7465 iptr = (int*) psf->buffer ;
7466 bufferlen = SF_BUFFER_LEN / sizeof (short) ;
7467 while (len > 0)
7468 { writecount = (len >= bufferlen) ? bufferlen : len ;
7469 for (k = 0 ; k < writecount ; k++)
7470 iptr [k] = lrint (normfact * ptr [total + k]) ;
7471 count = dwvw_encode_data (psf, pdwvw, iptr, writecount) ;
7473 total += count ;
7474 len -= writecount ;
7475 if (count != writecount)
7476 break ;
7479 return total ;
7480 } /* dwvw_write_d */
7482 ** Do not edit or modify anything in this comment block.
7483 ** The arch-tag line is a file identity tag for the GNU Arch
7484 ** revision control system.
7486 ** arch-tag: 1ca09552-b01f-4d7f-9bcf-612f834fe41d
7489 ** Copyright (C) 2002-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
7490 ** Copyright (C) 2003 Ross Bencina <rbencina@iprimus.com.au>
7492 ** This program is free software; you can redistribute it and/or modify
7493 ** it under the terms of the GNU Lesser General Public License as published by
7494 ** the Free Software Foundation; either version 2.1 of the License, or
7495 ** (at your option) any later version.
7497 ** This program is distributed in the hope that it will be useful,
7498 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
7499 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7500 ** GNU Lesser General Public License for more details.
7502 ** You should have received a copy of the GNU Lesser General Public License
7503 ** along with this program; if not, write to the Free Software
7504 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
7508 ** This header file MUST be included before the others to ensure that
7509 ** large file support is enabled.
7513 #include <stdio.h>
7514 #include <stdlib.h>
7516 #ifdef HAVE_UNISTD_H
7517 #include <unistd.h>
7518 #endif
7520 #if (HAVE_DECL_S_IRGRP == 0)
7521 #include <sf_unistd.h>
7522 #endif
7524 #include <string.h>
7525 #include <fcntl.h>
7526 #include <errno.h>
7527 #include <sys/stat.h>
7529 #if (defined (__MWERKS__) && defined (macintosh))
7530 typedef int ssize_t ;
7531 #include <Files.h>
7532 #endif
7535 #define SENSIBLE_SIZE (0x40000000)
7537 static void psf_log_syserr (SF_PRIVATE *psf, int error) ;
7539 #if ((defined (WIN32) || defined (_WIN32)) == 0)
7541 /*------------------------------------------------------------------------------
7542 ** Win32 stuff at the bottom of the file. Unix and other sensible OSes here.
7546 psf_fopen (SF_PRIVATE *psf, const char *pathname, int open_mode)
7547 { int oflag, mode ;
7550 ** Sanity check. If everything is OK, this test and the printfs will
7551 ** be optimised out. This is meant to catch the problems caused by
7552 ** "config.h" being included after <stdio.h>.
7554 if (sizeof (off_t) != sizeof (sf_count_t))
7555 { puts ("\n\n*** Fatal error : sizeof (off_t) != sizeof (sf_count_t)") ;
7556 puts ("*** This means that libsndfile was not configured correctly.\n") ;
7557 exit (1) ;
7560 switch (open_mode)
7561 { case SFM_READ :
7562 oflag = O_RDONLY ;
7563 mode = 0 ;
7564 break ;
7566 case SFM_WRITE :
7567 oflag = O_WRONLY | O_CREAT | O_TRUNC ;
7568 mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH ;
7569 break ;
7571 case SFM_RDWR :
7572 oflag = O_RDWR | O_CREAT ;
7573 mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH ;
7574 break ;
7576 default :
7577 psf->error = SFE_BAD_OPEN_MODE ;
7578 return psf->error ;
7579 break ;
7582 #if defined (__CYGWIN__)
7583 oflag |= O_BINARY ;
7584 #endif
7586 if (mode == 0)
7587 psf->filedes = open (pathname, oflag) ;
7588 else
7589 psf->filedes = open (pathname, oflag, mode) ;
7591 if (psf->filedes == -1)
7592 psf_log_syserr (psf, errno) ;
7594 psf->mode = open_mode ;
7596 return psf->error ;
7597 } /* psf_fopen */
7600 psf_set_stdio (SF_PRIVATE *psf, int mode)
7601 { int error = 0 ;
7603 switch (mode)
7604 { case SFM_RDWR :
7605 error = SFE_OPEN_PIPE_RDWR ;
7606 break ;
7608 case SFM_READ :
7609 psf->filedes = 0 ;
7610 break ;
7612 case SFM_WRITE :
7613 psf->filedes = 1 ;
7614 break ;
7616 default :
7617 error = SFE_BAD_OPEN_MODE ;
7618 break ;
7620 psf->filelength = 0 ;
7622 return error ;
7623 } /* psf_set_stdio */
7625 void
7626 psf_set_file (SF_PRIVATE *psf, int fd)
7627 { psf->filedes = fd ;
7628 } /* psf_set_file */
7631 psf_filedes_valid (SF_PRIVATE *psf)
7632 { return (psf->filedes >= 0) ? SF_TRUE : SF_FALSE ;
7633 } /* psf_set_file */
7635 sf_count_t
7636 psf_fseek (SF_PRIVATE *psf, sf_count_t offset, int whence)
7637 { sf_count_t new_position ;
7639 switch (whence)
7640 { case SEEK_SET :
7641 offset += psf->fileoffset ;
7642 break ;
7644 case SEEK_END :
7645 if (psf->mode == SFM_WRITE)
7646 { new_position = lseek (psf->filedes, offset, whence) ;
7648 if (new_position < 0)
7649 psf_log_syserr (psf, errno) ;
7651 return new_position - psf->fileoffset ;
7654 /* Transform SEEK_END into a SEEK_SET, ie find the file
7655 ** length add the requested offset (should be <= 0) to
7656 ** get the offset wrt the start of file.
7658 whence = SEEK_SET ;
7659 offset = lseek (psf->filedes, 0, SEEK_END) + offset ;
7660 break ;
7662 default :
7663 /* No need to do anything about SEEK_CUR. */
7664 break ;
7667 new_position = lseek (psf->filedes, offset, whence) ;
7669 if (new_position < 0)
7670 psf_log_syserr (psf, errno) ;
7672 new_position -= psf->fileoffset ;
7674 return new_position ;
7675 } /* psf_fseek */
7677 sf_count_t
7678 psf_fread (void *ptr, sf_count_t bytes, sf_count_t items, SF_PRIVATE *psf)
7679 { sf_count_t total = 0 ;
7680 ssize_t count ;
7682 items *= bytes ;
7684 /* Do this check after the multiplication above. */
7685 if (items <= 0)
7686 return 0 ;
7688 while (items > 0)
7689 { /* Break the writes down to a sensible size. */
7690 count = (items > SENSIBLE_SIZE) ? SENSIBLE_SIZE : (ssize_t) items ;
7692 count = read (psf->filedes, ((char*) ptr) + total, (size_t) count) ;
7694 if (count == -1)
7695 { if (errno == EINTR)
7696 continue ;
7698 psf_log_syserr (psf, errno) ;
7699 break ;
7702 if (count == 0)
7703 break ;
7705 total += count ;
7706 items -= count ;
7709 if (psf->is_pipe)
7710 psf->pipeoffset += total ;
7712 return total / bytes ;
7713 } /* psf_fread */
7715 sf_count_t
7716 psf_fwrite (void *ptr, sf_count_t bytes, sf_count_t items, SF_PRIVATE *psf)
7717 { sf_count_t total = 0 ;
7718 ssize_t count ;
7720 items *= bytes ;
7722 /* Do this check after the multiplication above. */
7723 if (items <= 0)
7724 return 0 ;
7726 while (items > 0)
7727 { /* Break the writes down to a sensible size. */
7728 count = (items > SENSIBLE_SIZE) ? SENSIBLE_SIZE : items ;
7730 count = write (psf->filedes, ((char*) ptr) + total, count) ;
7732 if (count == -1)
7733 { if (errno == EINTR)
7734 continue ;
7736 psf_log_syserr (psf, errno) ;
7737 break ;
7740 if (count == 0)
7741 break ;
7743 total += count ;
7744 items -= count ;
7747 if (psf->is_pipe)
7748 psf->pipeoffset += total ;
7750 return total / bytes ;
7751 } /* psf_fwrite */
7753 sf_count_t
7754 psf_ftell (SF_PRIVATE *psf)
7755 { sf_count_t pos ;
7757 if (psf->is_pipe)
7758 return psf->pipeoffset ;
7760 pos = lseek (psf->filedes, 0, SEEK_CUR) ;
7762 if (pos == ((sf_count_t) -1))
7763 { psf_log_syserr (psf, errno) ;
7764 return -1 ;
7767 return pos - psf->fileoffset ;
7768 } /* psf_ftell */
7771 psf_fclose (SF_PRIVATE *psf)
7772 { int retval ;
7774 #if ((defined (__MWERKS__) && defined (macintosh)) == 0)
7775 /* Must be MacOS9 which doesn't have fsync(). */
7776 if (fsync (psf->filedes) == -1 && errno == EBADF)
7777 return 0 ;
7778 #endif
7780 if (psf->do_not_close_descriptor)
7781 { psf->filedes = -1 ;
7782 return 0 ;
7785 while ((retval = close (psf->filedes)) == -1 && errno == EINTR)
7786 /* Do nothing. */ ;
7788 if (retval == -1)
7789 psf_log_syserr (psf, errno) ;
7791 psf->filedes = -1 ;
7793 return retval ;
7794 } /* psf_fclose */
7796 sf_count_t
7797 psf_fgets (char *buffer, sf_count_t bufsize, SF_PRIVATE *psf)
7798 { sf_count_t k = 0 ;
7799 sf_count_t count ;
7801 while (k < bufsize - 1)
7802 { count = read (psf->filedes, &(buffer [k]), 1) ;
7804 if (count == -1)
7805 { if (errno == EINTR)
7806 continue ;
7808 psf_log_syserr (psf, errno) ;
7809 break ;
7812 if (count == 0 || buffer [k++] == '\n')
7813 break ;
7816 buffer [k] = 0 ;
7818 return k ;
7819 } /* psf_fgets */
7822 psf_is_pipe (SF_PRIVATE *psf)
7823 { struct stat statbuf ;
7825 if (fstat (psf->filedes, &statbuf) == -1)
7826 { psf_log_syserr (psf, errno) ;
7827 /* Default to maximum safety. */
7828 return SF_TRUE ;
7831 if (S_ISFIFO (statbuf.st_mode) || S_ISSOCK (statbuf.st_mode))
7832 return SF_TRUE ;
7834 return SF_FALSE ;
7835 } /* psf_is_pipe */
7837 sf_count_t
7838 psf_get_filelen (SF_PRIVATE *psf)
7839 { struct stat statbuf ;
7840 sf_count_t filelen ;
7843 ** Sanity check.
7844 ** If everything is OK, this will be optimised out.
7846 if (sizeof (statbuf.st_size) == 4 && sizeof (sf_count_t) == 8)
7847 return SFE_BAD_STAT_SIZE ;
7849 /* Cygwin seems to need this. */
7850 #if (defined (__CYGWIN__) && HAVE_FSYNC)
7851 fsync (psf->filedes) ;
7852 #endif
7854 if (fstat (psf->filedes, &statbuf) == -1)
7855 { psf_log_syserr (psf, errno) ;
7856 return (sf_count_t) -1 ;
7859 switch (psf->mode)
7860 { case SFM_WRITE :
7861 filelen = statbuf.st_size - psf->fileoffset ;
7862 break ;
7864 case SFM_READ :
7865 if (psf->fileoffset > 0 && psf->filelength > 0)
7866 filelen = psf->filelength ;
7867 else
7868 filelen = statbuf.st_size ;
7869 break ;
7871 case SFM_RDWR :
7873 ** Cannot open embedded files SFM_RDWR so we don't need to
7874 ** subtract psf->fileoffset. We already have the answer we
7875 ** need.
7877 filelen = statbuf.st_size ;
7878 break ;
7880 default :
7881 /* Shouldn't be here, so return error. */
7882 filelen = -1 ;
7885 return filelen ;
7886 } /* psf_get_filelen */
7889 psf_ftruncate (SF_PRIVATE *psf, sf_count_t len)
7890 { int retval ;
7892 /* Returns 0 on success, non-zero on failure. */
7893 if (len < 0)
7894 return -1 ;
7896 if ((sizeof (off_t) < sizeof (sf_count_t)) && len > 0x7FFFFFFF)
7897 return -1 ;
7899 #if (defined (__MWERKS__) && defined (macintosh))
7900 retval = FSSetForkSize (psf->filedes, fsFromStart, len) ;
7901 #else
7902 retval = ftruncate (psf->filedes, len) ;
7903 #endif
7905 if (retval == -1)
7906 psf_log_syserr (psf, errno) ;
7908 return retval ;
7909 } /* psf_ftruncate */
7912 static void
7913 psf_log_syserr (SF_PRIVATE *psf, int error)
7915 /* Only log an error if no error has been set yet. */
7916 if (psf->error == 0)
7917 { psf->error = SFE_SYSTEM ;
7918 LSF_SNPRINTF (psf->syserr, sizeof (psf->syserr), "System error : %s.", strerror (error)) ;
7921 return ;
7922 } /* psf_log_syserr */
7924 //XXX formerly OS_IS_WIN32
7925 #elif __PLATFORM_WIN32__
7927 /* Win32 file i/o functions implemented using native Win32 API */
7929 #include <windows.h>
7930 #include <io.h>
7932 #ifndef HAVE_SSIZE_T
7933 typedef long ssize_t ;
7934 #endif
7936 /* Win32 */ static void
7937 psf_log_syserr (SF_PRIVATE *psf, int error)
7938 { LPVOID lpMsgBuf ;
7940 /* Only log an error if no error has been set yet. */
7941 if (psf->error == 0)
7942 { psf->error = SFE_SYSTEM ;
7944 FormatMessage (
7945 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
7946 NULL,
7947 error,
7948 MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
7949 (LPTSTR) &lpMsgBuf,
7951 NULL
7954 LSF_SNPRINTF (psf->syserr, sizeof (psf->syserr), "System error : %s", lpMsgBuf) ;
7955 LocalFree (lpMsgBuf) ;
7958 return ;
7959 } /* psf_log_syserr */
7962 /* Win32 */ int
7963 psf_fopen (SF_PRIVATE *psf, const char *pathname, int open_mode)
7964 { DWORD dwDesiredAccess ;
7965 DWORD dwShareMode ;
7966 DWORD dwCreationDistribution ;
7967 HANDLE handle ;
7969 switch (open_mode)
7970 { case SFM_READ :
7971 dwDesiredAccess = GENERIC_READ ;
7972 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE ;
7973 dwCreationDistribution = OPEN_EXISTING ;
7974 break ;
7976 case SFM_WRITE :
7977 dwDesiredAccess = GENERIC_WRITE ;
7978 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE ;
7979 dwCreationDistribution = CREATE_ALWAYS ;
7980 break ;
7982 case SFM_RDWR :
7983 dwDesiredAccess = GENERIC_READ | GENERIC_WRITE ;
7984 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE ;
7985 dwCreationDistribution = OPEN_ALWAYS ;
7986 break ;
7988 default :
7989 psf->error = SFE_BAD_OPEN_MODE ;
7990 return psf->error ;
7993 handle = CreateFile (
7994 pathname, /* pointer to name of the file */
7995 dwDesiredAccess, /* access (read-write) mode */
7996 dwShareMode, /* share mode */
7997 0, /* pointer to security attributes */
7998 dwCreationDistribution, /* how to create */
7999 FILE_ATTRIBUTE_NORMAL, /* file attributes (could use FILE_FLAG_SEQUENTIAL_SCAN) */
8000 NULL /* handle to file with attributes to copy */
8003 if (handle == INVALID_HANDLE_VALUE)
8004 { psf_log_syserr (psf, GetLastError ()) ;
8005 return psf->error ;
8008 psf->filedes = (int) handle ;
8009 psf->mode = open_mode ;
8011 return psf->error ;
8012 } /* psf_fopen */
8014 /* Win32 */ int
8015 psf_set_stdio (SF_PRIVATE *psf, int mode)
8016 { HANDLE handle = NULL ;
8017 int error = 0 ;
8019 switch (mode)
8020 { case SFM_RDWR :
8021 error = SFE_OPEN_PIPE_RDWR ;
8022 break ;
8024 case SFM_READ :
8025 handle = GetStdHandle (STD_INPUT_HANDLE) ;
8026 psf->do_not_close_descriptor = 1 ;
8027 break ;
8029 case SFM_WRITE :
8030 handle = GetStdHandle (STD_OUTPUT_HANDLE) ;
8031 psf->do_not_close_descriptor = 1 ;
8032 break ;
8034 default :
8035 error = SFE_BAD_OPEN_MODE ;
8036 break ;
8039 psf->filedes = (int) handle ;
8040 psf->filelength = 0 ;
8042 return error ;
8043 } /* psf_set_stdio */
8045 /* Win32 */ void
8046 psf_set_file (SF_PRIVATE *psf, int fd)
8047 { HANDLE handle ;
8048 long osfhandle ;
8050 osfhandle = _get_osfhandle (fd) ;
8051 handle = (HANDLE) osfhandle ;
8053 if (GetFileType (handle) == FILE_TYPE_DISK)
8054 psf->filedes = (int) handle ;
8055 else
8056 psf->filedes = fd ;
8057 } /* psf_set_file */
8059 /* Win32 */ int
8060 psf_filedes_valid (SF_PRIVATE *psf)
8061 { return (((HANDLE) psf->filedes) != INVALID_HANDLE_VALUE) ? SF_TRUE : SF_FALSE ;
8062 } /* psf_set_file */
8064 /* Win32 */ sf_count_t
8065 psf_fseek (SF_PRIVATE *psf, sf_count_t offset, int whence)
8066 { sf_count_t new_position ;
8067 LONG lDistanceToMove, lDistanceToMoveHigh ;
8068 DWORD dwMoveMethod ;
8069 DWORD dwResult, dwError ;
8071 switch (whence)
8072 { case SEEK_SET :
8073 offset += psf->fileoffset ;
8074 dwMoveMethod = FILE_BEGIN ;
8075 break ;
8077 case SEEK_END :
8078 dwMoveMethod = FILE_END ;
8079 break ;
8081 default :
8082 dwMoveMethod = FILE_CURRENT ;
8083 break ;
8086 lDistanceToMove = (DWORD) (offset & 0xFFFFFFFF) ;
8087 #ifndef __WINDOWS_DS__
8088 lDistanceToMoveHigh = (DWORD) (0xFFFFFFFF & (offset >> 32)) ;
8089 #else
8090 lDistanceToMoveHigh = 0;
8091 #endif
8093 dwResult = SetFilePointer ((HANDLE) psf->filedes, lDistanceToMove, &lDistanceToMoveHigh, dwMoveMethod) ;
8095 if (dwResult == 0xFFFFFFFF)
8096 dwError = GetLastError () ;
8097 else
8098 dwError = NO_ERROR ;
8100 if (dwError != NO_ERROR)
8101 { psf_log_syserr (psf, dwError) ;
8102 return -1 ;
8105 new_position = (dwResult + ((__int64) lDistanceToMoveHigh << 32)) - psf->fileoffset ;
8107 return new_position ;
8108 } /* psf_fseek */
8110 /* Win32 */ sf_count_t
8111 psf_fread (void *ptr, sf_count_t bytes, sf_count_t items, SF_PRIVATE *psf)
8112 { sf_count_t total = 0 ;
8113 ssize_t count ;
8114 DWORD dwNumberOfBytesRead ;
8116 items *= bytes ;
8118 /* Do this check after the multiplication above. */
8119 if (items <= 0)
8120 return 0 ;
8122 while (items > 0)
8123 { /* Break the writes down to a sensible size. */
8124 count = (items > SENSIBLE_SIZE) ? SENSIBLE_SIZE : (ssize_t) items ;
8125 if( items == 16384 ) count = 1024;
8127 if (ReadFile ((HANDLE) psf->filedes, ((char*) ptr) + total, count, &dwNumberOfBytesRead, 0) == 0)
8128 { psf_log_syserr (psf, GetLastError ()) ;
8129 break ;
8131 else
8132 count = dwNumberOfBytesRead ;
8134 if (count == 0)
8135 break ;
8137 total += count ;
8138 items -= count ;
8141 if (psf->is_pipe)
8142 psf->pipeoffset += total ;
8144 return total / bytes ;
8145 } /* psf_fread */
8147 /* Win32 */ sf_count_t
8148 psf_fwrite (void *ptr, sf_count_t bytes, sf_count_t items, SF_PRIVATE *psf)
8149 { sf_count_t total = 0 ;
8150 ssize_t count ;
8151 DWORD dwNumberOfBytesWritten ;
8153 items *= bytes ;
8155 /* Do this check after the multiplication above. */
8156 if (items <= 0)
8157 return 0 ;
8159 while (items > 0)
8160 { /* Break the writes down to a sensible size. */
8161 count = (items > SENSIBLE_SIZE) ? SENSIBLE_SIZE : (ssize_t) items ;
8163 if (WriteFile ((HANDLE) psf->filedes, ((char*) ptr) + total, count, &dwNumberOfBytesWritten, 0) == 0)
8164 { psf_log_syserr (psf, GetLastError ()) ;
8165 break ;
8167 else
8168 count = dwNumberOfBytesWritten ;
8170 if (count == 0)
8171 break ;
8173 total += count ;
8174 items -= count ;
8177 if (psf->is_pipe)
8178 psf->pipeoffset += total ;
8180 return total / bytes ;
8181 } /* psf_fwrite */
8183 /* Win32 */ sf_count_t
8184 psf_ftell (SF_PRIVATE *psf)
8185 { sf_count_t pos ;
8186 LONG lDistanceToMoveLow, lDistanceToMoveHigh ;
8187 DWORD dwResult, dwError ;
8189 if (psf->is_pipe)
8190 return psf->pipeoffset ;
8192 lDistanceToMoveLow = 0 ;
8193 lDistanceToMoveHigh = 0 ;
8195 dwResult = SetFilePointer ((HANDLE) psf->filedes, lDistanceToMoveLow, NULL, FILE_CURRENT) ;
8197 if (dwResult == 0xFFFFFFFF)
8198 dwError = GetLastError () ;
8199 else
8200 dwError = NO_ERROR ;
8202 if (dwError != NO_ERROR)
8203 { psf_log_syserr (psf, dwError) ;
8204 return -1 ;
8207 pos = (dwResult + ((__int64) lDistanceToMoveHigh << 32)) ;
8209 return pos - psf->fileoffset ;
8210 } /* psf_ftell */
8212 /* Win32 */ int
8213 psf_fclose (SF_PRIVATE *psf)
8214 { int retval = 0 ;
8216 if (psf->do_not_close_descriptor)
8217 { (HANDLE) psf->filedes = INVALID_HANDLE_VALUE ;
8218 return 0 ;
8221 if (CloseHandle ((HANDLE) psf->filedes) == 0)
8222 { retval = -1 ;
8223 psf_log_syserr (psf, GetLastError ()) ;
8226 psf->filedes = (int) INVALID_HANDLE_VALUE ;
8228 return retval ;
8229 } /* psf_fclose */
8231 /* Win32 */ sf_count_t
8232 psf_fgets (char *buffer, sf_count_t bufsize, SF_PRIVATE *psf)
8233 { sf_count_t k = 0 ;
8234 sf_count_t count ;
8235 DWORD dwNumberOfBytesRead ;
8237 while (k < bufsize - 1)
8238 { if (ReadFile ((HANDLE) psf->filedes, &(buffer [k]), 1, &dwNumberOfBytesRead, 0) == 0)
8239 { psf_log_syserr (psf, GetLastError ()) ;
8240 break ;
8242 else
8243 { count = dwNumberOfBytesRead ;
8244 /* note that we only check for '\n' not other line endings such as CRLF */
8245 if (count == 0 || buffer [k++] == '\n')
8246 break ;
8250 buffer [k] = '\0' ;
8252 return k ;
8253 } /* psf_fgets */
8255 /* Win32 */ int
8256 psf_is_pipe (SF_PRIVATE *psf)
8257 { if (GetFileType ((HANDLE) psf->filedes) == FILE_TYPE_DISK)
8258 return SF_FALSE ;
8260 /* Default to maximum safety. */
8261 return SF_TRUE ;
8262 } /* psf_is_pipe */
8264 /* Win32 */ sf_count_t
8265 psf_get_filelen (SF_PRIVATE *psf)
8266 { sf_count_t filelen ;
8267 DWORD dwFileSizeLow, dwFileSizeHigh, dwError = NO_ERROR ;
8269 dwFileSizeLow = GetFileSize ((HANDLE) psf->filedes, &dwFileSizeHigh) ;
8271 if (dwFileSizeLow == 0xFFFFFFFF)
8272 dwError = GetLastError () ;
8274 if (dwError != NO_ERROR)
8275 { psf_log_syserr (psf, GetLastError ()) ;
8276 return 0 ;
8278 else
8279 filelen = dwFileSizeLow + ((__int64) dwFileSizeHigh << 32) ;
8281 switch (psf->mode)
8282 { case SFM_WRITE :
8283 filelen = filelen - psf->fileoffset ;
8284 break ;
8286 case SFM_READ :
8287 if (psf->fileoffset > 0 && psf->filelength > 0)
8288 filelen = psf->filelength ;
8289 break ;
8291 case SFM_RDWR :
8293 ** Cannot open embedded files SFM_RDWR so we don't need to
8294 ** subtract psf->fileoffset. We already have the answer we
8295 ** need.
8297 break ;
8299 default :
8300 /* Shouldn't be here, so return error. */
8301 filelen = -1 ;
8304 return filelen ;
8305 } /* psf_get_filelen */
8307 /* Win32 */ int
8308 psf_ftruncate (SF_PRIVATE *psf, sf_count_t len)
8309 { int retval = 0 ;
8310 LONG lDistanceToMoveLow, lDistanceToMoveHigh ;
8311 DWORD dwResult, dwError = NO_ERROR ;
8313 /* This implementation trashes the current file position.
8314 ** should it save and restore it? what if the current position is past
8315 ** the new end of file?
8318 /* Returns 0 on success, non-zero on failure. */
8319 if (len < 0)
8320 return 1 ;
8322 lDistanceToMoveLow = (DWORD) (len & 0xFFFFFFFF) ;
8323 #ifndef __WINDOWS_DS__
8324 lDistanceToMoveHigh = (DWORD) ((len >> 32) & 0xFFFFFFFF) ;
8325 #else
8326 lDistanceToMoveHigh = 0;
8327 #endif
8329 dwResult = SetFilePointer ((HANDLE) psf->filedes, lDistanceToMoveLow, &lDistanceToMoveHigh, FILE_BEGIN) ;
8331 if (dwResult == 0xFFFFFFFF)
8332 dwError = GetLastError () ;
8334 if (dwError != NO_ERROR)
8335 { retval = -1 ;
8336 psf_log_syserr (psf, dwError) ;
8338 else
8339 { /* Note: when SetEndOfFile is used to extend a file, the contents of the
8340 ** new portion of the file is undefined. This is unlike chsize(),
8341 ** which guarantees that the new portion of the file will be zeroed.
8342 ** Not sure if this is important or not.
8344 if (SetEndOfFile ((HANDLE) psf->filedes) == 0)
8345 { retval = -1 ;
8346 psf_log_syserr (psf, GetLastError ()) ;
8350 return retval ;
8351 } /* psf_ftruncate */
8354 #else
8355 /* Win32 file i/o functions implemented using Unix-style file i/o API */
8357 /* Win32 has a 64 file offset seek function:
8359 ** __int64 _lseeki64 (int handle, __int64 offset, int origin) ;
8361 ** It also has a 64 bit fstat function:
8363 ** int fstati64 (int, struct _stati64) ;
8365 ** but the fscking thing doesn't work!!!!! The file size parameter returned
8366 ** by this function is only valid up until more data is written at the end of
8367 ** the file. That makes this function completely 100% useless.
8370 #include <io.h>
8371 #include <direct.h>
8373 #ifndef HAVE_SSIZE_T
8374 typedef long ssize_t ;
8375 #endif
8377 /* Win32 */ int
8378 psf_fopen (SF_PRIVATE *psf, const char *pathname, int open_mode)
8379 { int oflag, mode ;
8381 switch (open_mode)
8382 { case SFM_READ :
8383 oflag = O_RDONLY | O_BINARY ;
8384 mode = 0 ;
8385 break ;
8387 case SFM_WRITE :
8388 oflag = O_WRONLY | O_CREAT | O_TRUNC | O_BINARY ;
8389 mode = S_IRUSR | S_IWUSR | S_IRGRP ;
8390 break ;
8392 case SFM_RDWR :
8393 oflag = O_RDWR | O_CREAT | O_BINARY ;
8394 mode = S_IRUSR | S_IWUSR | S_IRGRP ;
8395 break ;
8397 default :
8398 psf->error = SFE_BAD_OPEN_MODE ;
8399 return -1 ;
8400 break ;
8403 if (mode == 0)
8404 psf->filedes = open (pathname, oflag) ;
8405 else
8406 psf->filedes = open (pathname, oflag, mode) ;
8408 if (psf->filedes == -1)
8409 psf_log_syserr (psf, errno) ;
8411 return psf->filedes ;
8412 } /* psf_fopen */
8414 /* Win32 */ sf_count_t
8415 psf_fseek (SF_PRIVATE *psf, sf_count_t offset, int whence)
8416 { sf_count_t new_position ;
8418 switch (whence)
8419 { case SEEK_SET :
8420 offset += psf->fileoffset ;
8421 break ;
8423 case SEEK_END :
8424 if (psf->mode == SFM_WRITE)
8425 { new_position = _lseeki64 (psf->filedes, offset, whence) ;
8427 if (new_position < 0)
8428 psf_log_syserr (psf, errno) ;
8430 return new_position - psf->fileoffset ;
8433 /* Transform SEEK_END into a SEEK_SET, ie find the file
8434 ** length add the requested offset (should be <= 0) to
8435 ** get the offset wrt the start of file.
8437 whence = SEEK_SET ;
8438 offset = _lseeki64 (psf->filedes, 0, SEEK_END) + offset ;
8439 break ;
8441 default :
8442 /* No need to do anything about SEEK_CUR. */
8443 break ;
8447 ** Bypass weird Win32-ism if necessary.
8448 ** _lseeki64() returns an "invalid parameter" error if called with the
8449 ** offset == 0 and whence == SEEK_CUR.
8450 *** Use the _telli64() function instead.
8452 if (offset == 0 && whence == SEEK_CUR)
8453 new_position = _telli64 (psf->filedes) ;
8454 else
8455 new_position = _lseeki64 (psf->filedes, offset, whence) ;
8457 if (new_position < 0)
8458 psf_log_syserr (psf, errno) ;
8460 new_position -= psf->fileoffset ;
8462 return new_position ;
8463 } /* psf_fseek */
8465 /* Win32 */ sf_count_t
8466 psf_fread (void *ptr, sf_count_t bytes, sf_count_t items, SF_PRIVATE *psf)
8467 { sf_count_t total = 0 ;
8468 ssize_t count ;
8470 items *= bytes ;
8472 /* Do this check after the multiplication above. */
8473 if (items <= 0)
8474 return 0 ;
8476 while (items > 0)
8477 { /* Break the writes down to a sensible size. */
8478 count = (items > SENSIBLE_SIZE) ? SENSIBLE_SIZE : (ssize_t) items ;
8480 count = read (psf->filedes, ((char*) ptr) + total, (size_t) count) ;
8482 if (count == -1)
8483 { if (errno == EINTR)
8484 continue ;
8486 psf_log_syserr (psf, errno) ;
8487 break ;
8490 if (count == 0)
8491 break ;
8493 total += count ;
8494 items -= count ;
8497 return total / bytes ;
8498 } /* psf_fread */
8500 /* Win32 */ sf_count_t
8501 psf_fwrite (void *ptr, sf_count_t bytes, sf_count_t items, SF_PRIVATE *psf)
8502 { sf_count_t total = 0 ;
8503 ssize_t count ;
8505 items *= bytes ;
8507 /* Do this check after the multiplication above. */
8508 if (items <= 0)
8509 return 0 ;
8511 while (items > 0)
8512 { /* Break the writes down to a sensible size. */
8513 count = (items > SENSIBLE_SIZE) ? SENSIBLE_SIZE : items ;
8515 count = write (psf->filedes, ((char*) ptr) + total, count) ;
8517 if (count == -1)
8518 { if (errno == EINTR)
8519 continue ;
8521 psf_log_syserr (psf, errno) ;
8522 break ;
8525 if (count == 0)
8526 break ;
8528 total += count ;
8529 items -= count ;
8532 return total / bytes ;
8533 } /* psf_fwrite */
8535 /* Win32 */ sf_count_t
8536 psf_ftell (SF_PRIVATE *psf)
8537 { sf_count_t pos ;
8539 pos = _telli64 (psf->filedes) ;
8541 if (pos == ((sf_count_t) -1))
8542 { psf_log_syserr (psf, errno) ;
8543 return -1 ;
8546 return pos - psf->fileoffset ;
8547 } /* psf_ftell */
8549 /* Win32 */ int
8550 psf_fclose (SF_PRIVATE *psf)
8551 { int retval ;
8553 while ((retval = close (psf->filedes)) == -1 && errno == EINTR)
8554 /* Do nothing. */ ;
8556 if (retval == -1)
8557 psf_log_syserr (psf, errno) ;
8559 psf->filedes = -1 ;
8561 return retval ;
8562 } /* psf_fclose */
8564 /* Win32 */ sf_count_t
8565 psf_fgets (char *buffer, sf_count_t bufsize, SF_PRIVATE *psf)
8566 { sf_count_t k = 0 ;
8567 sf_count_t count ;
8569 while (k < bufsize - 1)
8570 { count = read (psf->filedes, &(buffer [k]), 1) ;
8572 if (count == -1)
8573 { if (errno == EINTR)
8574 continue ;
8576 psf_log_syserr (psf, errno) ;
8577 break ;
8580 if (count == 0 || buffer [k++] == '\n')
8581 break ;
8584 buffer [k] = 0 ;
8586 return k ;
8587 } /* psf_fgets */
8589 /* Win32 */ int
8590 psf_is_pipe (SF_PRIVATE *psf)
8591 { struct stat statbuf ;
8593 /* Not sure if this works. */
8595 if (fstat (psf->filedes, &statbuf) == -1)
8596 { psf_log_syserr (psf, errno) ;
8597 /* Default to maximum safety. */
8598 return SF_TRUE ;
8601 /* These macros are defined in Win32/unistd.h. */
8602 if (S_ISFIFO (statbuf.st_mode) || S_ISSOCK (statbuf.st_mode))
8603 return SF_TRUE ;
8605 return SF_FALSE ;
8606 } /* psf_checkpipe */
8608 /* Win32 */ sf_count_t
8609 psf_get_filelen (SF_PRIVATE *psf)
8611 #if 0
8613 ** Windoze is SOOOOO FUCKED!!!!!!!
8614 ** This code should work but doesn't. Why?
8615 ** Code below does work.
8617 struct _stati64 statbuf ;
8619 if (_fstati64 (psf->filedes, &statbuf))
8620 { psf_log_syserr (psf, errno) ;
8621 return (sf_count_t) -1 ;
8624 return statbuf.st_size ;
8625 #else
8626 sf_count_t current, filelen ;
8628 if ((current = _telli64 (psf->filedes)) < 0)
8629 { psf_log_syserr (psf, errno) ;
8630 return (sf_count_t) -1 ;
8634 ** Lets face it, windoze if FUBAR!!!
8636 ** For some reason, I have to call _lseeki64() TWICE to get to the
8637 ** end of the file.
8639 ** This might have been avoided if windows had implemented the POSIX
8640 ** standard function fsync() but NO, that would have been too easy.
8642 ** I am VERY close to saying that windoze will no longer be supported
8643 ** by libsndfile and changing the license to GPL at the same time.
8646 _lseeki64 (psf->filedes, 0, SEEK_END) ;
8648 if ((filelen = _lseeki64 (psf->filedes, 0, SEEK_END)) < 0)
8649 { psf_log_syserr (psf, errno) ;
8650 return (sf_count_t) -1 ;
8653 if (filelen > current)
8654 _lseeki64 (psf->filedes, current, SEEK_SET) ;
8656 switch (psf->mode)
8657 { case SFM_WRITE :
8658 filelen = filelen - psf->fileoffset ;
8659 break ;
8661 case SFM_READ :
8662 if (psf->fileoffset > 0 && psf->filelength > 0)
8663 filelen = psf->filelength ;
8664 break ;
8666 case SFM_RDWR :
8668 ** Cannot open embedded files SFM_RDWR so we don't need to
8669 ** subtract psf->fileoffset. We already have the answer we
8670 ** need.
8672 break ;
8674 default :
8675 filelen = 0 ;
8678 return filelen ;
8679 #endif
8680 } /* psf_get_filelen */
8682 /* Win32 */ int
8683 psf_ftruncate (SF_PRIVATE *psf, sf_count_t len)
8684 { int retval ;
8686 /* Returns 0 on success, non-zero on failure. */
8687 if (len < 0)
8688 return 1 ;
8690 /* The global village idiots at micorsoft decided to implement
8691 ** nearly all the required 64 bit file offset functions except
8692 ** for one, truncate. The fscking morons!
8694 ** This is not 64 bit file offset clean. Somone needs to clean
8695 ** this up.
8697 if (len > 0x7FFFFFFF)
8698 return -1 ;
8700 retval = chsize (psf->filedes, len) ;
8702 if (retval == -1)
8703 psf_log_syserr (psf, errno) ;
8705 return retval ;
8706 } /* psf_ftruncate */
8709 static void
8710 psf_log_syserr (SF_PRIVATE *psf, int error)
8712 /* Only log an error if no error has been set yet. */
8713 if (psf->error == 0)
8714 { psf->error = SFE_SYSTEM ;
8715 LSF_SNPRINTF (psf->syserr, sizeof (psf->syserr), "System error : %s", strerror (error)) ;
8718 return ;
8719 } /* psf_log_syserr */
8721 #endif
8723 /*==============================================================================
8727 ** Do not edit or modify anything in this comment block.
8728 ** The arch-tag line is a file identity tag for the GNU Arch
8729 ** revision control system.
8731 ** arch-tag: 749740d7-ecc7-47bd-8cf7-600f31d32e6d
8734 ** Copyright (C) 1999-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
8736 ** This program is free software; you can redistribute it and/or modify
8737 ** it under the terms of the GNU Lesser General Public License as published by
8738 ** the Free Software Foundation; either version 2.1 of the License, or
8739 ** (at your option) any later version.
8741 ** This program is distributed in the hope that it will be useful,
8742 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
8743 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8744 ** GNU Lesser General Public License for more details.
8746 ** You should have received a copy of the GNU Lesser General Public License
8747 ** along with this program; if not, write to the Free Software
8748 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
8751 #include <stdio.h>
8752 #include <stdlib.h>
8753 #include <string.h>
8756 #if CPU_IS_LITTLE_ENDIAN
8757 #define FLOAT32_READ float32_le_read
8758 #define FLOAT32_WRITE float32_le_write
8759 #elif CPU_IS_BIG_ENDIAN
8760 #define FLOAT32_READ float32_be_read
8761 #define FLOAT32_WRITE float32_be_write
8762 #endif
8764 /*--------------------------------------------------------------------------------------------
8765 ** Processor floating point capabilities. float32_get_capability () returns one of the
8766 ** latter four values.
8769 enum
8770 { FLOAT_UNKNOWN = 0x00,
8771 FLOAT_CAN_RW_LE = 0x12,
8772 FLOAT_CAN_RW_BE = 0x23,
8773 FLOAT_BROKEN_LE = 0x34,
8774 FLOAT_BROKEN_BE = 0x45
8777 /*--------------------------------------------------------------------------------------------
8778 ** Prototypes for private functions.
8781 static sf_count_t host_read_f2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
8782 static sf_count_t host_read_f2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
8783 static sf_count_t host_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
8784 static sf_count_t host_read_f2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
8786 static sf_count_t host_write_s2f (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
8787 static sf_count_t host_write_i2f (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
8788 static sf_count_t host_write_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
8789 static sf_count_t host_write_d2f (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
8791 static void f2s_array (float *src, int count, short *dest) ;
8792 static void f2i_array (float *src, int count, int *dest) ;
8793 static void float32f2d_array (float *src, int count, double *dest) ;
8795 static void s2f_array (short *src, float *dest, int count) ;
8796 static void i2f_array (int *src, float *dest, int count) ;
8797 static void float32d2f_array (double *src, float *dest, int count) ;
8799 static void float32_peak_update (SF_PRIVATE *psf, float *buffer, int count, int indx) ;
8801 static sf_count_t replace_read_f2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
8802 static sf_count_t replace_read_f2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
8803 static sf_count_t replace_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
8804 static sf_count_t replace_read_f2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
8806 static sf_count_t replace_write_s2f (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
8807 static sf_count_t replace_write_i2f (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
8808 static sf_count_t replace_write_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
8809 static sf_count_t replace_write_d2f (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
8811 static void bf2f_array (float *buffer, int count) ;
8812 static void f2bf_array (float *buffer, int count) ;
8814 static int float32_get_capability (SF_PRIVATE *psf) ;
8816 /*--------------------------------------------------------------------------------------------
8817 ** Exported functions.
8821 float32_init (SF_PRIVATE *psf)
8822 { static int float_caps ;
8824 float_caps = float32_get_capability (psf) ;
8826 psf->blockwidth = sizeof (float) * psf->sf.channels ;
8828 if (psf->mode == SFM_READ || psf->mode == SFM_RDWR)
8829 { switch (psf->endian + float_caps)
8830 { case (SF_ENDIAN_BIG + FLOAT_CAN_RW_BE) :
8831 psf->float_endswap = SF_FALSE ;
8832 psf->read_short = host_read_f2s ;
8833 psf->read_int = host_read_f2i ;
8834 psf->read_float = host_read_f ;
8835 psf->read_double = host_read_f2d ;
8836 break ;
8838 case (SF_ENDIAN_LITTLE + FLOAT_CAN_RW_LE) :
8839 psf->float_endswap = SF_FALSE ;
8840 psf->read_short = host_read_f2s ;
8841 psf->read_int = host_read_f2i ;
8842 psf->read_float = host_read_f ;
8843 psf->read_double = host_read_f2d ;
8844 break ;
8846 case (SF_ENDIAN_BIG + FLOAT_CAN_RW_LE) :
8847 psf->float_endswap = SF_TRUE ;
8848 psf->read_short = host_read_f2s ;
8849 psf->read_int = host_read_f2i ;
8850 psf->read_float = host_read_f ;
8851 psf->read_double = host_read_f2d ;
8852 break ;
8854 case (SF_ENDIAN_LITTLE + FLOAT_CAN_RW_BE) :
8855 psf->float_endswap = SF_TRUE ;
8856 psf->read_short = host_read_f2s ;
8857 psf->read_int = host_read_f2i ;
8858 psf->read_float = host_read_f ;
8859 psf->read_double = host_read_f2d ;
8860 break ;
8862 /* When the CPU is not IEEE compatible. */
8863 case (SF_ENDIAN_BIG + FLOAT_BROKEN_LE) :
8864 psf->float_endswap = SF_TRUE ;
8865 psf->read_short = replace_read_f2s ;
8866 psf->read_int = replace_read_f2i ;
8867 psf->read_float = replace_read_f ;
8868 psf->read_double = replace_read_f2d ;
8869 break ;
8871 case (SF_ENDIAN_LITTLE + FLOAT_BROKEN_LE) :
8872 psf->float_endswap = SF_FALSE ;
8873 psf->read_short = replace_read_f2s ;
8874 psf->read_int = replace_read_f2i ;
8875 psf->read_float = replace_read_f ;
8876 psf->read_double = replace_read_f2d ;
8877 break ;
8879 case (SF_ENDIAN_BIG + FLOAT_BROKEN_BE) :
8880 psf->float_endswap = SF_FALSE ;
8881 psf->read_short = replace_read_f2s ;
8882 psf->read_int = replace_read_f2i ;
8883 psf->read_float = replace_read_f ;
8884 psf->read_double = replace_read_f2d ;
8885 break ;
8887 case (SF_ENDIAN_LITTLE + FLOAT_BROKEN_BE) :
8888 psf->float_endswap = SF_TRUE ;
8889 psf->read_short = replace_read_f2s ;
8890 psf->read_int = replace_read_f2i ;
8891 psf->read_float = replace_read_f ;
8892 psf->read_double = replace_read_f2d ;
8893 break ;
8895 default : break ;
8899 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
8900 { switch (psf->endian + float_caps)
8901 { case (SF_ENDIAN_LITTLE + FLOAT_CAN_RW_LE) :
8902 psf->float_endswap = SF_FALSE ;
8903 psf->write_short = host_write_s2f ;
8904 psf->write_int = host_write_i2f ;
8905 psf->write_float = host_write_f ;
8906 psf->write_double = host_write_d2f ;
8907 break ;
8909 case (SF_ENDIAN_BIG + FLOAT_CAN_RW_BE) :
8910 psf->float_endswap = SF_FALSE ;
8911 psf->write_short = host_write_s2f ;
8912 psf->write_int = host_write_i2f ;
8913 psf->write_float = host_write_f ;
8914 psf->write_double = host_write_d2f ;
8915 break ;
8917 case (SF_ENDIAN_BIG + FLOAT_CAN_RW_LE) :
8918 psf->float_endswap = SF_TRUE ;
8919 psf->write_short = host_write_s2f ;
8920 psf->write_int = host_write_i2f ;
8921 psf->write_float = host_write_f ;
8922 psf->write_double = host_write_d2f ;
8923 break ;
8925 case (SF_ENDIAN_LITTLE + FLOAT_CAN_RW_BE) :
8926 psf->float_endswap = SF_TRUE ;
8927 psf->write_short = host_write_s2f ;
8928 psf->write_int = host_write_i2f ;
8929 psf->write_float = host_write_f ;
8930 psf->write_double = host_write_d2f ;
8931 break ;
8933 /* When the CPU is not IEEE compatible. */
8934 case (SF_ENDIAN_BIG + FLOAT_BROKEN_LE) :
8935 psf->float_endswap = SF_TRUE ;
8936 psf->write_short = replace_write_s2f ;
8937 psf->write_int = replace_write_i2f ;
8938 psf->write_float = replace_write_f ;
8939 psf->write_double = replace_write_d2f ;
8940 break ;
8942 case (SF_ENDIAN_LITTLE + FLOAT_BROKEN_LE) :
8943 psf->float_endswap = SF_FALSE ;
8944 psf->write_short = replace_write_s2f ;
8945 psf->write_int = replace_write_i2f ;
8946 psf->write_float = replace_write_f ;
8947 psf->write_double = replace_write_d2f ;
8948 break ;
8950 case (SF_ENDIAN_BIG + FLOAT_BROKEN_BE) :
8951 psf->float_endswap = SF_FALSE ;
8952 psf->write_short = replace_write_s2f ;
8953 psf->write_int = replace_write_i2f ;
8954 psf->write_float = replace_write_f ;
8955 psf->write_double = replace_write_d2f ;
8956 break ;
8958 case (SF_ENDIAN_LITTLE + FLOAT_BROKEN_BE) :
8959 psf->float_endswap = SF_TRUE ;
8960 psf->write_short = replace_write_s2f ;
8961 psf->write_int = replace_write_i2f ;
8962 psf->write_float = replace_write_f ;
8963 psf->write_double = replace_write_d2f ;
8964 break ;
8966 default : break ;
8970 if (psf->filelength > psf->dataoffset)
8971 { psf->datalength = (psf->dataend > 0) ? psf->dataend - psf->dataoffset :
8972 psf->filelength - psf->dataoffset ;
8974 else
8975 psf->datalength = 0 ;
8977 psf->sf.frames = psf->datalength / psf->blockwidth ;
8979 return 0 ;
8980 } /* float32_init */
8982 float
8983 float32_be_read (unsigned char *cptr)
8984 { int exponent, mantissa, negative ;
8985 float fvalue ;
8987 negative = cptr [0] & 0x80 ;
8988 exponent = ((cptr [0] & 0x7F) << 1) | ((cptr [1] & 0x80) ? 1 : 0) ;
8989 mantissa = ((cptr [1] & 0x7F) << 16) | (cptr [2] << 8) | (cptr [3]) ;
8991 if (! (exponent || mantissa))
8992 return 0.0 ;
8994 mantissa |= 0x800000 ;
8995 exponent = exponent ? exponent - 127 : 0 ;
8997 fvalue = mantissa ? ((float) mantissa) / ((float) 0x800000) : 0.0 ;
8999 if (negative)
9000 fvalue *= -1 ;
9002 if (exponent > 0)
9003 fvalue *= (1 << exponent) ;
9004 else if (exponent < 0)
9005 fvalue /= (1 << abs (exponent)) ;
9007 return fvalue ;
9008 } /* float32_be_read */
9010 float
9011 float32_le_read (unsigned char *cptr)
9012 { int exponent, mantissa, negative ;
9013 float fvalue ;
9015 negative = cptr [3] & 0x80 ;
9016 exponent = ((cptr [3] & 0x7F) << 1) | ((cptr [2] & 0x80) ? 1 : 0) ;
9017 mantissa = ((cptr [2] & 0x7F) << 16) | (cptr [1] << 8) | (cptr [0]) ;
9019 if (! (exponent || mantissa))
9020 return 0.0 ;
9022 mantissa |= 0x800000 ;
9023 exponent = exponent ? exponent - 127 : 0 ;
9025 fvalue = mantissa ? ((float) mantissa) / ((float) 0x800000) : 0.0 ;
9027 if (negative)
9028 fvalue *= -1 ;
9030 if (exponent > 0)
9031 fvalue *= (1 << exponent) ;
9032 else if (exponent < 0)
9033 fvalue /= (1 << abs (exponent)) ;
9035 return fvalue ;
9036 } /* float32_le_read */
9038 void
9039 float32_le_write (float in, unsigned char *out)
9040 { int exponent, mantissa, negative = 0 ;
9042 memset (out, 0, sizeof (int)) ;
9044 if (in == 0.0)
9045 return ;
9047 if (in < 0.0)
9048 { in *= -1.0 ;
9049 negative = 1 ;
9052 in = frexp (in, &exponent) ;
9054 exponent += 126 ;
9056 in *= (float) 0x1000000 ;
9057 mantissa = (((int) in) & 0x7FFFFF) ;
9059 if (negative)
9060 out [3] |= 0x80 ;
9062 if (exponent & 0x01)
9063 out [2] |= 0x80 ;
9065 out [0] = mantissa & 0xFF ;
9066 out [1] = (mantissa >> 8) & 0xFF ;
9067 out [2] |= (mantissa >> 16) & 0x7F ;
9068 out [3] |= (exponent >> 1) & 0x7F ;
9070 return ;
9071 } /* float32_le_write */
9073 void
9074 float32_be_write (float in, unsigned char *out)
9075 { int exponent, mantissa, negative = 0 ;
9077 memset (out, 0, sizeof (int)) ;
9079 if (in == 0.0)
9080 return ;
9082 if (in < 0.0)
9083 { in *= -1.0 ;
9084 negative = 1 ;
9087 in = frexp (in, &exponent) ;
9089 exponent += 126 ;
9091 in *= (float) 0x1000000 ;
9092 mantissa = (((int) in) & 0x7FFFFF) ;
9094 if (negative)
9095 out [0] |= 0x80 ;
9097 if (exponent & 0x01)
9098 out [1] |= 0x80 ;
9100 out [3] = mantissa & 0xFF ;
9101 out [2] = (mantissa >> 8) & 0xFF ;
9102 out [1] |= (mantissa >> 16) & 0x7F ;
9103 out [0] |= (exponent >> 1) & 0x7F ;
9105 return ;
9106 } /* float32_be_write */
9108 /*==============================================================================================
9109 ** Private functions.
9112 static void
9113 float32_peak_update (SF_PRIVATE *psf, float *buffer, int count, int indx)
9114 { int chan ;
9115 int k, position ;
9116 float fmaxval ;
9118 for (chan = 0 ; chan < psf->sf.channels ; chan++)
9119 { fmaxval = fabs (buffer [chan]) ;
9120 position = 0 ;
9121 for (k = chan ; k < count ; k += psf->sf.channels)
9122 if (fmaxval < fabs (buffer [k]))
9123 { fmaxval = fabs (buffer [k]) ;
9124 position = k ;
9127 if (fmaxval > psf->pchunk->peaks [chan].value)
9128 { psf->pchunk->peaks [chan].value = fmaxval ;
9129 psf->pchunk->peaks [chan].position = psf->write_current + indx + (position / psf->sf.channels) ;
9133 return ;
9134 } /* float32_peak_update */
9136 static int
9137 float32_get_capability (SF_PRIVATE *psf)
9138 { union
9139 { float f ;
9140 int i ;
9141 unsigned char c [4] ;
9142 } data ;
9144 data.f = (float) 1.23456789 ; /* Some abitrary value. */
9146 if (! psf->ieee_replace)
9147 { /* If this test is true ints and floats are compatible and little endian. */
9148 if (data.c [0] == 0x52 && data.c [1] == 0x06 && data.c [2] == 0x9e && data.c [3] == 0x3f)
9149 return FLOAT_CAN_RW_LE ;
9151 /* If this test is true ints and floats are compatible and big endian. */
9152 if (data.c [3] == 0x52 && data.c [2] == 0x06 && data.c [1] == 0x9e && data.c [0] == 0x3f)
9153 return FLOAT_CAN_RW_BE ;
9156 /* Floats are broken. Don't expect reading or writing to be fast. */
9157 psf_log_printf (psf, "Using IEEE replacement code for float.\n") ;
9159 return (CPU_IS_LITTLE_ENDIAN) ? FLOAT_BROKEN_LE : FLOAT_BROKEN_BE ;
9160 } /* float32_get_capability */
9162 /*----------------------------------------------------------------------------------------------
9165 static sf_count_t
9166 host_read_f2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
9167 { int bufferlen, readcount, thisread ;
9168 sf_count_t total = 0 ;
9170 bufferlen = sizeof (psf->buffer) / sizeof (float) ;
9172 while (len > 0)
9173 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
9174 thisread = psf_fread (psf->buffer, sizeof (float), readcount, psf) ;
9176 /* Fix me : Need lef2s_array */
9177 if (psf->float_endswap == SF_TRUE)
9178 endswap_int_array ((int*) psf->buffer, readcount) ;
9180 f2s_array ((float*) (psf->buffer), thisread, ptr + total) ;
9181 total += thisread ;
9182 if (thisread < readcount)
9183 break ;
9184 len -= thisread ;
9187 return total ;
9188 } /* host_read_f2s */
9190 static sf_count_t
9191 host_read_f2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
9192 { int bufferlen, readcount, thisread ;
9193 sf_count_t total = 0 ;
9195 bufferlen = sizeof (psf->buffer) / sizeof (float) ;
9197 while (len > 0)
9198 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
9199 thisread = psf_fread (psf->buffer, sizeof (float), readcount, psf) ;
9201 if (psf->float_endswap == SF_TRUE)
9202 endswap_int_array ((int*) psf->buffer, readcount) ;
9204 f2i_array ((float*) (psf->buffer), thisread, ptr + total) ;
9205 total += thisread ;
9206 if (thisread < readcount)
9207 break ;
9208 len -= thisread ;
9211 return total ;
9212 } /* host_read_f2i */
9214 static sf_count_t
9215 host_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
9216 { int bufferlen, readcount, thisread ;
9217 sf_count_t total = 0 ;
9219 if (psf->float_endswap != SF_TRUE)
9220 return psf_fread (ptr, sizeof (float), len, psf) ;
9222 bufferlen = sizeof (psf->buffer) / sizeof (float) ;
9224 while (len > 0)
9225 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
9226 thisread = psf_fread (psf->buffer, sizeof (float), readcount, psf) ;
9228 endswap_int_copy ((int*) (ptr + total), (int*) psf->buffer, thisread) ;
9230 total += thisread ;
9231 if (thisread < readcount)
9232 break ;
9233 len -= thisread ;
9236 return total ;
9237 } /* host_read_f */
9239 static sf_count_t
9240 host_read_f2d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
9241 { int bufferlen, readcount, thisread ;
9242 sf_count_t total = 0 ;
9244 bufferlen = sizeof (psf->buffer) / sizeof (float) ;
9246 while (len > 0)
9247 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
9248 thisread = psf_fread (psf->buffer, sizeof (float), readcount, psf) ;
9250 if (psf->float_endswap == SF_TRUE)
9251 endswap_int_array ((int*) psf->buffer, readcount) ;
9253 /* Fix me : Need lefloat32f2d_array */
9254 float32f2d_array ((float*) (psf->buffer), thisread, ptr + total) ;
9255 total += thisread ;
9256 if (thisread < readcount)
9257 break ;
9258 len -= thisread ;
9261 return total ;
9262 } /* host_read_f2d */
9264 static sf_count_t
9265 host_write_s2f (SF_PRIVATE *psf, short *ptr, sf_count_t len)
9266 { int bufferlen, writecount, thiswrite ;
9267 sf_count_t total = 0 ;
9269 bufferlen = sizeof (psf->buffer) / sizeof (float) ;
9271 while (len > 0)
9272 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
9273 s2f_array (ptr + total, (float*) (psf->buffer), writecount) ;
9275 if (psf->has_peak)
9276 float32_peak_update (psf, (float *) (psf->buffer), writecount, (int) (total / psf->sf.channels)) ;
9278 if (psf->float_endswap == SF_TRUE)
9279 endswap_int_array ((int*) psf->buffer, writecount) ;
9281 thiswrite = psf_fwrite (psf->buffer, sizeof (float), writecount, psf) ;
9282 total += thiswrite ;
9283 if (thiswrite < writecount)
9284 break ;
9285 len -= thiswrite ;
9288 return total ;
9289 } /* host_write_s2f */
9291 static sf_count_t
9292 host_write_i2f (SF_PRIVATE *psf, int *ptr, sf_count_t len)
9293 { int bufferlen, writecount, thiswrite ;
9294 sf_count_t total = 0 ;
9296 bufferlen = sizeof (psf->buffer) / sizeof (float) ;
9298 while (len > 0)
9299 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
9300 i2f_array (ptr + total, (float*) (psf->buffer), writecount) ;
9302 if (psf->has_peak)
9303 float32_peak_update (psf, (float *) (psf->buffer), writecount, (int) (total / psf->sf.channels)) ;
9305 if (psf->float_endswap == SF_TRUE)
9306 endswap_int_array ((int*) psf->buffer, writecount) ;
9308 thiswrite = psf_fwrite (psf->buffer, sizeof (float) , writecount, psf) ;
9309 total += thiswrite ;
9310 if (thiswrite < writecount)
9311 break ;
9312 len -= thiswrite ;
9315 return total ;
9316 } /* host_write_i2f */
9318 static sf_count_t
9319 host_write_f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
9320 { int bufferlen, writecount, thiswrite ;
9321 sf_count_t total = 0 ;
9323 if (psf->has_peak)
9324 float32_peak_update (psf, ptr, len, 0) ;
9326 if (psf->float_endswap != SF_TRUE)
9327 return psf_fwrite (ptr, sizeof (float), len, psf) ;
9329 bufferlen = sizeof (psf->buffer) / sizeof (float) ;
9331 while (len > 0)
9332 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
9334 endswap_int_copy ((int*) psf->buffer, (int*) (ptr + total), writecount) ;
9336 thiswrite = psf_fwrite (psf->buffer, sizeof (float), writecount, psf) ;
9337 total += thiswrite ;
9338 if (thiswrite < writecount)
9339 break ;
9340 len -= thiswrite ;
9343 return total ;
9344 } /* host_write_f */
9346 static sf_count_t
9347 host_write_d2f (SF_PRIVATE *psf, double *ptr, sf_count_t len)
9348 { int bufferlen, writecount, thiswrite ;
9349 sf_count_t total = 0 ;
9351 bufferlen = sizeof (psf->buffer) / sizeof (float) ;
9353 while (len > 0)
9354 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
9356 float32d2f_array (ptr + total, (float*) (psf->buffer), writecount) ;
9358 if (psf->has_peak)
9359 float32_peak_update (psf, (float *) (psf->buffer), writecount, (int) (total / psf->sf.channels)) ;
9361 if (psf->float_endswap == SF_TRUE)
9362 endswap_int_array ((int*) psf->buffer, writecount) ;
9364 thiswrite = psf_fwrite (psf->buffer, sizeof (float), writecount, psf) ;
9365 total += thiswrite ;
9366 if (thiswrite < writecount)
9367 break ;
9368 len -= thiswrite ;
9371 return total ;
9372 } /* host_write_d2f */
9374 /*=======================================================================================
9377 static void
9378 f2s_array (float *src, int count, short *dest)
9379 { while (count)
9380 { count -- ;
9381 dest [count] = lrintf (src [count]) ;
9383 } /* f2s_array */
9385 static void
9386 f2i_array (float *src, int count, int *dest)
9387 { while (count)
9388 { count -- ;
9389 dest [count] = lrintf (src [count]) ;
9391 } /* f2i_array */
9393 static void
9394 float32f2d_array (float *src, int count, double *dest)
9395 { while (count)
9396 { count -- ;
9397 dest [count] = src [count] ;
9399 } /* float32f2d_array */
9401 static void
9402 s2f_array (short *src, float *dest, int count)
9403 { while (count)
9404 { count -- ;
9405 dest [count] = src [count] ;
9408 } /* s2f_array */
9410 static void
9411 i2f_array (int *src, float *dest, int count)
9412 { while (count)
9413 { count -- ;
9414 dest [count] = src [count] ;
9416 } /* i2f_array */
9418 static void
9419 float32d2f_array (double *src, float *dest, int count)
9420 { while (count)
9421 { count -- ;
9422 dest [count] = src [count] ;
9424 } /* float32d2f_array */
9426 /*=======================================================================================
9429 static sf_count_t
9430 replace_read_f2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
9431 { int bufferlen, readcount, thisread ;
9432 sf_count_t total = 0 ;
9434 bufferlen = sizeof (psf->buffer) / sizeof (float) ;
9436 while (len > 0)
9437 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
9438 thisread = psf_fread (psf->buffer, sizeof (float), readcount, psf) ;
9440 if (psf->float_endswap == SF_TRUE)
9441 endswap_int_array ((int*) psf->buffer, readcount) ;
9443 bf2f_array ((float *) (psf->buffer), readcount) ;
9445 f2s_array ((float*) (psf->buffer), thisread, ptr + total) ;
9446 total += thisread ;
9447 if (thisread < readcount)
9448 break ;
9449 len -= thisread ;
9452 return total ;
9453 } /* replace_read_f2s */
9455 static sf_count_t
9456 replace_read_f2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
9457 { int bufferlen, readcount, thisread ;
9458 sf_count_t total = 0 ;
9460 bufferlen = sizeof (psf->buffer) / sizeof (float) ;
9462 while (len > 0)
9463 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
9464 thisread = psf_fread (psf->buffer, sizeof (float), readcount, psf) ;
9466 if (psf->float_endswap == SF_TRUE)
9467 endswap_int_array ((int*) psf->buffer, readcount) ;
9469 bf2f_array ((float *) (psf->buffer), readcount) ;
9471 f2i_array ((float*) (psf->buffer), thisread, ptr + total) ;
9472 total += thisread ;
9473 if (thisread < readcount)
9474 break ;
9475 len -= thisread ;
9478 return total ;
9479 } /* replace_read_f2i */
9481 static sf_count_t
9482 replace_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
9483 { int bufferlen, readcount, thisread ;
9484 sf_count_t total = 0 ;
9486 /* FIX THIS */
9488 bufferlen = sizeof (psf->buffer) / sizeof (float) ;
9490 while (len > 0)
9491 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
9492 thisread = psf_fread (psf->buffer, sizeof (float), readcount, psf) ;
9494 if (psf->float_endswap == SF_TRUE)
9495 endswap_int_array ((int*) psf->buffer, readcount) ;
9497 bf2f_array ((float *) (psf->buffer), readcount) ;
9499 memcpy (ptr + total, psf->buffer, readcount * sizeof (float)) ;
9501 total += thisread ;
9502 if (thisread < readcount)
9503 break ;
9504 len -= thisread ;
9507 return total ;
9508 } /* replace_read_f */
9510 static sf_count_t
9511 replace_read_f2d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
9512 { int bufferlen, readcount, thisread ;
9513 sf_count_t total = 0 ;
9515 bufferlen = sizeof (psf->buffer) / sizeof (float) ;
9517 while (len > 0)
9518 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
9519 thisread = psf_fread (psf->buffer, sizeof (float), readcount, psf) ;
9521 if (psf->float_endswap == SF_TRUE)
9522 endswap_int_array ((int*) psf->buffer, readcount) ;
9524 bf2f_array ((float *) (psf->buffer), readcount) ;
9526 float32f2d_array ((float*) (psf->buffer), thisread, ptr + total) ;
9527 total += thisread ;
9528 if (thisread < readcount)
9529 break ;
9530 len -= thisread ;
9533 return total ;
9534 } /* replace_read_f2d */
9536 static sf_count_t
9537 replace_write_s2f (SF_PRIVATE *psf, short *ptr, sf_count_t len)
9538 { int writecount, bufferlen, thiswrite ;
9539 sf_count_t total = 0 ;
9541 bufferlen = sizeof (psf->buffer) / sizeof (float) ;
9543 while (len > 0)
9544 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
9545 s2f_array (ptr + total, (float*) (psf->buffer), writecount) ;
9547 if (psf->has_peak)
9548 float32_peak_update (psf, (float *) (psf->buffer), writecount, (int) (total / psf->sf.channels)) ;
9550 f2bf_array ((float *) (psf->buffer), writecount) ;
9552 if (psf->float_endswap == SF_TRUE)
9553 endswap_int_array ((int*) psf->buffer, writecount) ;
9555 thiswrite = psf_fwrite (psf->buffer, sizeof (float), writecount, psf) ;
9556 total += thiswrite ;
9557 if (thiswrite < writecount)
9558 break ;
9559 len -= thiswrite ;
9562 return total ;
9563 } /* replace_write_s2f */
9565 static sf_count_t
9566 replace_write_i2f (SF_PRIVATE *psf, int *ptr, sf_count_t len)
9567 { int writecount, bufferlen, thiswrite ;
9568 sf_count_t total = 0 ;
9570 bufferlen = sizeof (psf->buffer) / sizeof (float) ;
9572 while (len > 0)
9573 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
9574 i2f_array (ptr + total, (float*) (psf->buffer), writecount) ;
9576 if (psf->has_peak)
9577 float32_peak_update (psf, (float *) (psf->buffer), writecount, (int) (total / psf->sf.channels)) ;
9579 f2bf_array ((float *) (psf->buffer), writecount) ;
9581 if (psf->float_endswap == SF_TRUE)
9582 endswap_int_array ((int*) psf->buffer, writecount) ;
9584 thiswrite = psf_fwrite (psf->buffer, sizeof (float), writecount, psf) ;
9585 total += thiswrite ;
9586 if (thiswrite < writecount)
9587 break ;
9588 len -= thiswrite ;
9591 return total ;
9592 } /* replace_write_i2f */
9594 static sf_count_t
9595 replace_write_f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
9596 { int writecount, bufferlen, thiswrite ;
9597 sf_count_t total = 0 ;
9599 /* FIX THIS */
9600 if (psf->has_peak)
9601 float32_peak_update (psf, ptr, len, 0) ;
9603 bufferlen = sizeof (psf->buffer) / sizeof (float) ;
9605 while (len > 0)
9606 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
9608 memcpy (psf->buffer, ptr + total, writecount * sizeof (float)) ;
9610 f2bf_array ((float *) (psf->buffer), writecount) ;
9612 if (psf->float_endswap == SF_TRUE)
9613 endswap_int_array ((int*) psf->buffer, writecount) ;
9615 thiswrite = psf_fwrite (psf->buffer, sizeof (float) , writecount, psf) ;
9616 total += thiswrite ;
9617 if (thiswrite < writecount)
9618 break ;
9619 len -= thiswrite ;
9622 return total ;
9623 } /* replace_write_f */
9625 static sf_count_t
9626 replace_write_d2f (SF_PRIVATE *psf, double *ptr, sf_count_t len)
9627 { int writecount, bufferlen, thiswrite ;
9628 sf_count_t total = 0 ;
9630 bufferlen = sizeof (psf->buffer) / sizeof (float) ;
9632 while (len > 0)
9633 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
9634 float32d2f_array (ptr + total, (float*) (psf->buffer), writecount) ;
9636 if (psf->has_peak)
9637 float32_peak_update (psf, (float *) (psf->buffer), writecount, (int) (total / psf->sf.channels)) ;
9639 f2bf_array ((float *) (psf->buffer), writecount) ;
9641 if (psf->float_endswap == SF_TRUE)
9642 endswap_int_array ((int*) psf->buffer, writecount) ;
9644 thiswrite = psf_fwrite (psf->buffer, sizeof (float), writecount, psf) ;
9645 total += thiswrite ;
9646 if (thiswrite < writecount)
9647 break ;
9648 len -= thiswrite ;
9651 return total ;
9652 } /* replace_write_d2f */
9654 /*----------------------------------------------------------------------------------------------
9657 static void
9658 bf2f_array (float *buffer, int count)
9659 { while (count)
9660 { count -- ;
9661 buffer [count] = FLOAT32_READ ((unsigned char *) (buffer + count)) ;
9663 } /* bf2f_array */
9665 static void
9666 f2bf_array (float *buffer, int count)
9667 { while (count)
9668 { count -- ;
9669 FLOAT32_WRITE (buffer [count], (unsigned char*) (buffer + count)) ;
9671 } /* f2bf_array */
9674 ** Do not edit or modify anything in this comment block.
9675 ** The arch-tag line is a file identity tag for the GNU Arch
9676 ** revision control system.
9678 ** arch-tag: b6c34917-488c-4145-9648-f4371fc4c889
9681 * This source code is a product of Sun Microsystems, Inc. and is provided
9682 * for unrestricted use. Users may copy or modify this source code without
9683 * charge.
9685 * SUN SOURCE CODE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING
9686 * THE WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
9687 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
9689 * Sun source code is provided with no support and without any obligation on
9690 * the part of Sun Microsystems, Inc. to assist in its use, correction,
9691 * modification or enhancement.
9693 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
9694 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS SOFTWARE
9695 * OR ANY PART THEREOF.
9697 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
9698 * or profits or other special, indirect and consequential damages, even if
9699 * Sun has been advised of the possibility of such damages.
9701 * Sun Microsystems, Inc.
9702 * 2550 Garcia Avenue
9703 * Mountain View, California 94043
9707 * g721.c
9709 * Description:
9711 * g721_encoder(), g721_decoder()
9713 * These routines comprise an implementation of the CCITT G.721 ADPCM
9714 * coding algorithm. Essentially, this implementation is identical to
9715 * the bit level description except for a few deviations which
9716 * take advantage of work station attributes, such as hardware 2's
9717 * complement arithmetic and large memory. Specifically, certain time
9718 * consuming operations such as multiplications are replaced
9719 * with lookup tables and software 2's complement operations are
9720 * replaced with hardware 2's complement.
9722 * The deviation from the bit level specification (lookup tables)
9723 * preserves the bit level performance specifications.
9725 * As outlined in the G.721 Recommendation, the algorithm is broken
9726 * down into modules. Each section of code below is preceded by
9727 * the name of the module which it is implementing.
9732 static short qtab_721[7] = {-124, 80, 178, 246, 300, 349, 400};
9734 * Maps G.721 code word to reconstructed scale factor normalized log
9735 * magnitude values.
9737 static short g721_dqlntab[16] = {-2048, 4, 135, 213, 273, 323, 373, 425,
9738 425, 373, 323, 273, 213, 135, 4, -2048};
9740 /* Maps G.721 code word to log of scale factor multiplier. */
9741 static short g721_witab[16] = {-12, 18, 41, 64, 112, 198, 355, 1122,
9742 1122, 355, 198, 112, 64, 41, 18, -12};
9744 * Maps G.721 code words to a set of values whose long and short
9745 * term averages are computed and then compared to give an indication
9746 * how stationary (steady state) the signal is.
9748 static short g721_fitab[16] = {0, 0, 0, 0x200, 0x200, 0x200, 0x600, 0xE00,
9749 0xE00, 0x600, 0x200, 0x200, 0x200, 0, 0, 0};
9752 * g721_encoder()
9754 * Encodes the input vale of linear PCM, A-law or u-law data sl and returns
9755 * the resulting code. -1 is returned for unknown input coding value.
9758 g721_encoder(
9759 int sl,
9760 G72x_STATE *state_ptr)
9762 short sezi, se, sez; /* ACCUM */
9763 short d; /* SUBTA */
9764 short sr; /* ADDB */
9765 short y; /* MIX */
9766 short dqsez; /* ADDC */
9767 short dq, i;
9769 /* linearize input sample to 14-bit PCM */
9770 sl >>= 2; /* 14-bit dynamic range */
9772 sezi = predictor_zero(state_ptr);
9773 sez = sezi >> 1;
9774 se = (sezi + predictor_pole(state_ptr)) >> 1; /* estimated signal */
9776 d = sl - se; /* estimation difference */
9778 /* quantize the prediction difference */
9779 y = step_size(state_ptr); /* quantizer step size */
9780 i = quantize(d, y, qtab_721, 7); /* i = ADPCM code */
9782 dq = reconstruct(i & 8, g721_dqlntab[i], y); /* quantized est diff */
9784 sr = (dq < 0) ? se - (dq & 0x3FFF) : se + dq; /* reconst. signal */
9786 dqsez = sr + sez - se; /* pole prediction diff. */
9788 update(4, y, g721_witab[i] << 5, g721_fitab[i], dq, sr, dqsez, state_ptr);
9790 return (i);
9794 * g721_decoder()
9796 * Description:
9798 * Decodes a 4-bit code of G.721 encoded data of i and
9799 * returns the resulting linear PCM, A-law or u-law value.
9800 * return -1 for unknown out_coding value.
9803 g721_decoder(
9804 int i,
9805 G72x_STATE *state_ptr)
9807 short sezi, sei, sez, se; /* ACCUM */
9808 short y; /* MIX */
9809 short sr; /* ADDB */
9810 short dq;
9811 short dqsez;
9813 i &= 0x0f; /* mask to get proper bits */
9814 sezi = predictor_zero(state_ptr);
9815 sez = sezi >> 1;
9816 sei = sezi + predictor_pole(state_ptr);
9817 se = sei >> 1; /* se = estimated signal */
9819 y = step_size(state_ptr); /* dynamic quantizer step size */
9821 dq = reconstruct(i & 0x08, g721_dqlntab[i], y); /* quantized diff. */
9823 sr = (dq < 0) ? (se - (dq & 0x3FFF)) : se + dq; /* reconst. signal */
9825 dqsez = sr - se + sez; /* pole prediction diff. */
9827 update(4, y, g721_witab[i] << 5, g721_fitab[i], dq, sr, dqsez, state_ptr);
9829 /* sr was 14-bit dynamic range */
9830 return (sr << 2);
9833 ** Do not edit or modify anything in this comment block.
9834 ** The arch-tag line is a file identity tag for the GNU Arch
9835 ** revision control system.
9837 ** arch-tag: 101b6e25-457d-490a-99ae-e2e74a26ea24
9841 * This source code is a product of Sun Microsystems, Inc. and is provided
9842 * for unrestricted use. Users may copy or modify this source code without
9843 * charge.
9845 * SUN SOURCE CODE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING
9846 * THE WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
9847 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
9849 * Sun source code is provided with no support and without any obligation on
9850 * the part of Sun Microsystems, Inc. to assist in its use, correction,
9851 * modification or enhancement.
9853 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
9854 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS SOFTWARE
9855 * OR ANY PART THEREOF.
9857 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
9858 * or profits or other special, indirect and consequential damages, even if
9859 * Sun has been advised of the possibility of such damages.
9861 * Sun Microsystems, Inc.
9862 * 2550 Garcia Avenue
9863 * Mountain View, California 94043
9865 /* 16kbps version created, used 24kbps code and changing as little as possible.
9866 * G.726 specs are available from ITU's gopher or WWW site (http://www.itu.ch)
9867 * If any errors are found, please contact me at mrand@tamu.edu
9868 * -Marc Randolph
9872 * g723_16.c
9874 * Description:
9876 * g723_16_encoder(), g723_16_decoder()
9878 * These routines comprise an implementation of the CCITT G.726 16 Kbps
9879 * ADPCM coding algorithm. Essentially, this implementation is identical to
9880 * the bit level description except for a few deviations which take advantage
9881 * of workstation attributes, such as hardware 2's complement arithmetic.
9887 * Maps G.723_16 code word to reconstructed scale factor normalized log
9888 * magnitude values. Comes from Table 11/G.726
9890 static short g723_16_dqlntab[4] = { 116, 365, 365, 116};
9892 /* Maps G.723_16 code word to log of scale factor multiplier.
9894 * g723_16_witab[4] is actually {-22 , 439, 439, -22}, but FILTD wants it
9895 * as WI << 5 (multiplied by 32), so we'll do that here
9897 static short g723_16_witab[4] = {-704, 14048, 14048, -704};
9900 * Maps G.723_16 code words to a set of values whose long and short
9901 * term averages are computed and then compared to give an indication
9902 * how stationary (steady state) the signal is.
9905 /* Comes from FUNCTF */
9906 static short g723_16_fitab[4] = {0, 0xE00, 0xE00, 0};
9908 /* Comes from quantizer decision level tables (Table 7/G.726)
9910 static short qtab_723_16[1] = {261};
9914 * g723_16_encoder()
9916 * Encodes a linear PCM, A-law or u-law input sample and returns its 2-bit code.
9917 * Returns -1 if invalid input coding value.
9920 g723_16_encoder(
9921 int sl,
9922 G72x_STATE *state_ptr)
9924 short sei, sezi, se, sez; /* ACCUM */
9925 short d; /* SUBTA */
9926 short y; /* MIX */
9927 short sr; /* ADDB */
9928 short dqsez; /* ADDC */
9929 short dq, i;
9931 /* linearize input sample to 14-bit PCM */
9932 sl >>= 2; /* sl of 14-bit dynamic range */
9934 sezi = predictor_zero(state_ptr);
9935 sez = sezi >> 1;
9936 sei = sezi + predictor_pole(state_ptr);
9937 se = sei >> 1; /* se = estimated signal */
9939 d = sl - se; /* d = estimation diff. */
9941 /* quantize prediction difference d */
9942 y = step_size(state_ptr); /* quantizer step size */
9943 i = quantize(d, y, qtab_723_16, 1); /* i = ADPCM code */
9945 /* Since quantize() only produces a three level output
9946 * (1, 2, or 3), we must create the fourth one on our own
9948 if (i == 3) /* i code for the zero region */
9949 if ((d & 0x8000) == 0) /* If d > 0, i=3 isn't right... */
9950 i = 0;
9952 dq = reconstruct(i & 2, g723_16_dqlntab[i], y); /* quantized diff. */
9954 sr = (dq < 0) ? se - (dq & 0x3FFF) : se + dq; /* reconstructed signal */
9956 dqsez = sr + sez - se; /* pole prediction diff. */
9958 update(2, y, g723_16_witab[i], g723_16_fitab[i], dq, sr, dqsez, state_ptr);
9960 return (i);
9964 * g723_16_decoder()
9966 * Decodes a 2-bit CCITT G.723_16 ADPCM code and returns
9967 * the resulting 16-bit linear PCM, A-law or u-law sample value.
9968 * -1 is returned if the output coding is unknown.
9971 g723_16_decoder(
9972 int i,
9973 G72x_STATE *state_ptr)
9975 short sezi, sei, sez, se; /* ACCUM */
9976 short y; /* MIX */
9977 short sr; /* ADDB */
9978 short dq;
9979 short dqsez;
9981 i &= 0x03; /* mask to get proper bits */
9982 sezi = predictor_zero(state_ptr);
9983 sez = sezi >> 1;
9984 sei = sezi + predictor_pole(state_ptr);
9985 se = sei >> 1; /* se = estimated signal */
9987 y = step_size(state_ptr); /* adaptive quantizer step size */
9988 dq = reconstruct(i & 0x02, g723_16_dqlntab[i], y); /* unquantize pred diff */
9990 sr = (dq < 0) ? (se - (dq & 0x3FFF)) : (se + dq); /* reconst. signal */
9992 dqsez = sr - se + sez; /* pole prediction diff. */
9994 update(2, y, g723_16_witab[i], g723_16_fitab[i], dq, sr, dqsez, state_ptr);
9996 /* sr was of 14-bit dynamic range */
9997 return (sr << 2);
10000 ** Do not edit or modify anything in this comment block.
10001 ** The arch-tag line is a file identity tag for the GNU Arch
10002 ** revision control system.
10004 ** arch-tag: ae265466-c3fc-4f83-bb32-edae488a5ca5
10008 * This source code is a product of Sun Microsystems, Inc. and is provided
10009 * for unrestricted use. Users may copy or modify this source code without
10010 * charge.
10012 * SUN SOURCE CODE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING
10013 * THE WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
10014 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
10016 * Sun source code is provided with no support and without any obligation on
10017 * the part of Sun Microsystems, Inc. to assist in its use, correction,
10018 * modification or enhancement.
10020 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
10021 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS SOFTWARE
10022 * OR ANY PART THEREOF.
10024 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
10025 * or profits or other special, indirect and consequential damages, even if
10026 * Sun has been advised of the possibility of such damages.
10028 * Sun Microsystems, Inc.
10029 * 2550 Garcia Avenue
10030 * Mountain View, California 94043
10034 * g723_24.c
10036 * Description:
10038 * g723_24_encoder(), g723_24_decoder()
10040 * These routines comprise an implementation of the CCITT G.723 24 Kbps
10041 * ADPCM coding algorithm. Essentially, this implementation is identical to
10042 * the bit level description except for a few deviations which take advantage
10043 * of workstation attributes, such as hardware 2's complement arithmetic.
10049 * Maps G.723_24 code word to reconstructed scale factor normalized log
10050 * magnitude values.
10052 static short g723_24_dqlntab[8] = {-2048, 135, 273, 373, 373, 273, 135, -2048};
10054 /* Maps G.723_24 code word to log of scale factor multiplier. */
10055 static short g723_24_witab[8] = {-128, 960, 4384, 18624, 18624, 4384, 960, -128};
10058 * Maps G.723_24 code words to a set of values whose long and short
10059 * term averages are computed and then compared to give an indication
10060 * how stationary (steady state) the signal is.
10062 static short g723_24_fitab[8] = {0, 0x200, 0x400, 0xE00, 0xE00, 0x400, 0x200, 0};
10064 static short qtab_723_24[3] = {8, 218, 331};
10067 * g723_24_encoder()
10069 * Encodes a linear PCM, A-law or u-law input sample and returns its 3-bit code.
10070 * Returns -1 if invalid input coding value.
10073 g723_24_encoder(
10074 int sl,
10075 G72x_STATE *state_ptr)
10077 short sei, sezi, se, sez; /* ACCUM */
10078 short d; /* SUBTA */
10079 short y; /* MIX */
10080 short sr; /* ADDB */
10081 short dqsez; /* ADDC */
10082 short dq, i;
10084 /* linearize input sample to 14-bit PCM */
10085 sl >>= 2; /* sl of 14-bit dynamic range */
10087 sezi = predictor_zero(state_ptr);
10088 sez = sezi >> 1;
10089 sei = sezi + predictor_pole(state_ptr);
10090 se = sei >> 1; /* se = estimated signal */
10092 d = sl - se; /* d = estimation diff. */
10094 /* quantize prediction difference d */
10095 y = step_size(state_ptr); /* quantizer step size */
10096 i = quantize(d, y, qtab_723_24, 3); /* i = ADPCM code */
10097 dq = reconstruct(i & 4, g723_24_dqlntab[i], y); /* quantized diff. */
10099 sr = (dq < 0) ? se - (dq & 0x3FFF) : se + dq; /* reconstructed signal */
10101 dqsez = sr + sez - se; /* pole prediction diff. */
10103 update(3, y, g723_24_witab[i], g723_24_fitab[i], dq, sr, dqsez, state_ptr);
10105 return (i);
10109 * g723_24_decoder()
10111 * Decodes a 3-bit CCITT G.723_24 ADPCM code and returns
10112 * the resulting 16-bit linear PCM, A-law or u-law sample value.
10113 * -1 is returned if the output coding is unknown.
10116 g723_24_decoder(
10117 int i,
10118 G72x_STATE *state_ptr)
10120 short sezi, sei, sez, se; /* ACCUM */
10121 short y; /* MIX */
10122 short sr; /* ADDB */
10123 short dq;
10124 short dqsez;
10126 i &= 0x07; /* mask to get proper bits */
10127 sezi = predictor_zero(state_ptr);
10128 sez = sezi >> 1;
10129 sei = sezi + predictor_pole(state_ptr);
10130 se = sei >> 1; /* se = estimated signal */
10132 y = step_size(state_ptr); /* adaptive quantizer step size */
10133 dq = reconstruct(i & 0x04, g723_24_dqlntab[i], y); /* unquantize pred diff */
10135 sr = (dq < 0) ? (se - (dq & 0x3FFF)) : (se + dq); /* reconst. signal */
10137 dqsez = sr - se + sez; /* pole prediction diff. */
10139 update(3, y, g723_24_witab[i], g723_24_fitab[i], dq, sr, dqsez, state_ptr);
10141 return (sr << 2); /* sr was of 14-bit dynamic range */
10144 ** Do not edit or modify anything in this comment block.
10145 ** The arch-tag line is a file identity tag for the GNU Arch
10146 ** revision control system.
10148 ** arch-tag: 75389236-650b-4427-98f3-0df6e8fb24bc
10152 * This source code is a product of Sun Microsystems, Inc. and is provided
10153 * for unrestricted use. Users may copy or modify this source code without
10154 * charge.
10156 * SUN SOURCE CODE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING
10157 * THE WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
10158 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
10160 * Sun source code is provided with no support and without any obligation on
10161 * the part of Sun Microsystems, Inc. to assist in its use, correction,
10162 * modification or enhancement.
10164 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
10165 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS SOFTWARE
10166 * OR ANY PART THEREOF.
10168 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
10169 * or profits or other special, indirect and consequential damages, even if
10170 * Sun has been advised of the possibility of such damages.
10172 * Sun Microsystems, Inc.
10173 * 2550 Garcia Avenue
10174 * Mountain View, California 94043
10178 * g723_40.c
10180 * Description:
10182 * g723_40_encoder(), g723_40_decoder()
10184 * These routines comprise an implementation of the CCITT G.723 40Kbps
10185 * ADPCM coding algorithm. Essentially, this implementation is identical to
10186 * the bit level description except for a few deviations which
10187 * take advantage of workstation attributes, such as hardware 2's
10188 * complement arithmetic.
10190 * The deviation from the bit level specification (lookup tables),
10191 * preserves the bit level performance specifications.
10193 * As outlined in the G.723 Recommendation, the algorithm is broken
10194 * down into modules. Each section of code below is preceded by
10195 * the name of the module which it is implementing.
10201 * Maps G.723_40 code word to ructeconstructed scale factor normalized log
10202 * magnitude values.
10204 static short g723_40_dqlntab[32] = {-2048, -66, 28, 104, 169, 224, 274, 318,
10205 358, 395, 429, 459, 488, 514, 539, 566,
10206 566, 539, 514, 488, 459, 429, 395, 358,
10207 318, 274, 224, 169, 104, 28, -66, -2048};
10209 /* Maps G.723_40 code word to log of scale factor multiplier. */
10210 static short g723_40_witab[32] = {448, 448, 768, 1248, 1280, 1312, 1856, 3200,
10211 4512, 5728, 7008, 8960, 11456, 14080, 16928, 22272,
10212 22272, 16928, 14080, 11456, 8960, 7008, 5728, 4512,
10213 3200, 1856, 1312, 1280, 1248, 768, 448, 448};
10216 * Maps G.723_40 code words to a set of values whose long and short
10217 * term averages are computed and then compared to give an indication
10218 * how stationary (steady state) the signal is.
10220 static short g723_40_fitab[32] = {0, 0, 0, 0, 0, 0x200, 0x200, 0x200,
10221 0x200, 0x200, 0x400, 0x600, 0x800, 0xA00, 0xC00, 0xC00,
10222 0xC00, 0xC00, 0xA00, 0x800, 0x600, 0x400, 0x200, 0x200,
10223 0x200, 0x200, 0x200, 0, 0, 0, 0, 0};
10225 static short qtab_723_40[15] = {-122, -16, 68, 139, 198, 250, 298, 339,
10226 378, 413, 445, 475, 502, 528, 553};
10229 * g723_40_encoder()
10231 * Encodes a 16-bit linear PCM, A-law or u-law input sample and retuens
10232 * the resulting 5-bit CCITT G.723 40Kbps code.
10233 * Returns -1 if the input coding value is invalid.
10235 int g723_40_encoder (int sl, G72x_STATE *state_ptr)
10237 short sei, sezi, se, sez; /* ACCUM */
10238 short d; /* SUBTA */
10239 short y; /* MIX */
10240 short sr; /* ADDB */
10241 short dqsez; /* ADDC */
10242 short dq, i;
10244 /* linearize input sample to 14-bit PCM */
10245 sl >>= 2; /* sl of 14-bit dynamic range */
10247 sezi = predictor_zero(state_ptr);
10248 sez = sezi >> 1;
10249 sei = sezi + predictor_pole(state_ptr);
10250 se = sei >> 1; /* se = estimated signal */
10252 d = sl - se; /* d = estimation difference */
10254 /* quantize prediction difference */
10255 y = step_size(state_ptr); /* adaptive quantizer step size */
10256 i = quantize(d, y, qtab_723_40, 15); /* i = ADPCM code */
10258 dq = reconstruct(i & 0x10, g723_40_dqlntab[i], y); /* quantized diff */
10260 sr = (dq < 0) ? se - (dq & 0x7FFF) : se + dq; /* reconstructed signal */
10262 dqsez = sr + sez - se; /* dqsez = pole prediction diff. */
10264 update(5, y, g723_40_witab[i], g723_40_fitab[i], dq, sr, dqsez, state_ptr);
10266 return (i);
10270 * g723_40_decoder()
10272 * Decodes a 5-bit CCITT G.723 40Kbps code and returns
10273 * the resulting 16-bit linear PCM, A-law or u-law sample value.
10274 * -1 is returned if the output coding is unknown.
10276 int g723_40_decoder (int i, G72x_STATE *state_ptr)
10278 short sezi, sei, sez, se; /* ACCUM */
10279 short y ; /* MIX */
10280 short sr; /* ADDB */
10281 short dq;
10282 short dqsez;
10284 i &= 0x1f; /* mask to get proper bits */
10285 sezi = predictor_zero(state_ptr);
10286 sez = sezi >> 1;
10287 sei = sezi + predictor_pole(state_ptr);
10288 se = sei >> 1; /* se = estimated signal */
10290 y = step_size(state_ptr); /* adaptive quantizer step size */
10291 dq = reconstruct(i & 0x10, g723_40_dqlntab[i], y); /* estimation diff. */
10293 sr = (dq < 0) ? (se - (dq & 0x7FFF)) : (se + dq); /* reconst. signal */
10295 dqsez = sr - se + sez; /* pole prediction diff. */
10297 update(5, y, g723_40_witab[i], g723_40_fitab[i], dq, sr, dqsez, state_ptr);
10299 return (sr << 2); /* sr was of 14-bit dynamic range */
10302 ** Do not edit or modify anything in this comment block.
10303 ** The arch-tag line is a file identity tag for the GNU Arch
10304 ** revision control system.
10306 ** arch-tag: eb8d9a00-32bf-4dd2-b287-01b0336d72bf
10310 * This source code is a product of Sun Microsystems, Inc. and is provided
10311 * for unrestricted use. Users may copy or modify this source code without
10312 * charge.
10314 * SUN SOURCE CODE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING
10315 * THE WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
10316 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
10318 * Sun source code is provided with no support and without any obligation on
10319 * the part of Sun Microsystems, Inc. to assist in its use, correction,
10320 * modification or enhancement.
10322 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
10323 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS SOFTWARE
10324 * OR ANY PART THEREOF.
10326 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
10327 * or profits or other special, indirect and consequential damages, even if
10328 * Sun has been advised of the possibility of such damages.
10330 * Sun Microsystems, Inc.
10331 * 2550 Garcia Avenue
10332 * Mountain View, California 94043
10336 * g72x.c
10338 * Common routines for G.721 and G.723 conversions.
10341 #include <stdio.h>
10342 #include <stdlib.h>
10343 #include <string.h>
10346 static
10347 short power2 [15] =
10348 { 1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80,
10349 0x100, 0x200, 0x400, 0x800, 0x1000, 0x2000, 0x4000
10353 * quan()
10355 * quantizes the input val against the table of size short integers.
10356 * It returns i if table[i - 1] <= val < table[i].
10358 * Using linear search for simple coding.
10360 static
10361 int quan (int val, short *table, int size)
10363 int i;
10365 for (i = 0; i < size; i++)
10366 if (val < *table++)
10367 break;
10368 return (i);
10372 * fmult()
10374 * returns the integer product of the 14-bit integer "an" and
10375 * "floating point" representation (4-bit exponent, 6-bit mantessa) "srn".
10377 static
10378 int fmult (int an, int srn)
10380 short anmag, anexp, anmant;
10381 short wanexp, wanmant;
10382 short retval;
10384 anmag = (an > 0) ? an : ((-an) & 0x1FFF);
10385 anexp = quan(anmag, power2, 15) - 6;
10386 anmant = (anmag == 0) ? 32 :
10387 (anexp >= 0) ? anmag >> anexp : anmag << -anexp;
10388 wanexp = anexp + ((srn >> 6) & 0xF) - 13;
10391 ** The original was :
10392 ** wanmant = (anmant * (srn & 0x37) + 0x30) >> 4 ;
10393 ** but could see no valid reason for the + 0x30.
10394 ** Removed it and it improved the SNR of the codec.
10397 wanmant = (anmant * (srn & 0x37)) >> 4 ;
10399 retval = (wanexp >= 0) ? ((wanmant << wanexp) & 0x7FFF) :
10400 (wanmant >> -wanexp);
10402 return (((an ^ srn) < 0) ? -retval : retval);
10406 * private_init_state()
10408 * This routine initializes and/or resets the G72x_PRIVATE structure
10409 * pointed to by 'state_ptr'.
10410 * All the initial state values are specified in the CCITT G.721 document.
10412 void private_init_state (G72x_STATE *state_ptr)
10414 int cnta;
10416 state_ptr->yl = 34816;
10417 state_ptr->yu = 544;
10418 state_ptr->dms = 0;
10419 state_ptr->dml = 0;
10420 state_ptr->ap = 0;
10421 for (cnta = 0; cnta < 2; cnta++) {
10422 state_ptr->a[cnta] = 0;
10423 state_ptr->pk[cnta] = 0;
10424 state_ptr->sr[cnta] = 32;
10426 for (cnta = 0; cnta < 6; cnta++) {
10427 state_ptr->b[cnta] = 0;
10428 state_ptr->dq[cnta] = 32;
10430 state_ptr->td = 0;
10431 } /* private_init_state */
10433 int g72x_reader_init (G72x_DATA *data, int codec)
10434 { G72x_STATE *pstate ;
10436 if (sizeof (data->sprivateo) < sizeof (G72x_STATE))
10437 { /* This is for safety only. */
10438 return 1 ;
10441 memset (data, 0, sizeof (G72x_DATA)) ;
10443 pstate = (G72x_STATE*) data->sprivateo ;
10444 private_init_state (pstate) ;
10446 pstate->encoder = NULL ;
10448 switch (codec)
10449 { case G723_16_BITS_PER_SAMPLE : /* 2 bits per sample. */
10450 pstate->decoder = g723_16_decoder ;
10451 data->blocksize = G723_16_BYTES_PER_BLOCK ;
10452 data->samplesperblock = G723_16_SAMPLES_PER_BLOCK ;
10453 pstate->codec_bits = 2 ;
10454 break ;
10456 case G723_24_BITS_PER_SAMPLE : /* 3 bits per sample. */
10457 pstate->decoder = g723_24_decoder ;
10458 data->blocksize = G723_24_BYTES_PER_BLOCK ;
10459 data->samplesperblock = G723_24_SAMPLES_PER_BLOCK ;
10460 pstate->codec_bits = 3 ;
10461 break ;
10463 case G721_32_BITS_PER_SAMPLE : /* 4 bits per sample. */
10464 pstate->decoder = g721_decoder ;
10465 data->blocksize = G721_32_BYTES_PER_BLOCK ;
10466 data->samplesperblock = G721_32_SAMPLES_PER_BLOCK ;
10467 pstate->codec_bits = 4 ;
10468 break ;
10470 case G721_40_BITS_PER_SAMPLE : /* 5 bits per sample. */
10471 pstate->decoder = g723_40_decoder ;
10472 data->blocksize = G721_40_BYTES_PER_BLOCK ;
10473 data->samplesperblock = G721_40_SAMPLES_PER_BLOCK ;
10474 pstate->codec_bits = 5 ;
10475 break ;
10477 default : return 1 ;
10480 return 0 ;
10481 } /* g72x_reader_init */
10483 int g72x_writer_init (G72x_DATA *data, int codec)
10484 { G72x_STATE *pstate ;
10486 if (sizeof (data->sprivateo) < sizeof (G72x_STATE))
10487 { /* This is for safety only. Gets optimised out. */
10488 return 1 ;
10491 memset (data, 0, sizeof (G72x_DATA)) ;
10493 pstate = (G72x_STATE*) data->sprivateo ;
10494 private_init_state (pstate) ;
10496 pstate->decoder = NULL ;
10498 switch (codec)
10499 { case G723_16_BITS_PER_SAMPLE : /* 2 bits per sample. */
10500 pstate->encoder = g723_16_encoder ;
10501 data->blocksize = G723_16_BYTES_PER_BLOCK ;
10502 data->samplesperblock = G723_16_SAMPLES_PER_BLOCK ;
10503 pstate->codec_bits = 2 ;
10504 break ;
10506 case G723_24_BITS_PER_SAMPLE : /* 3 bits per sample. */
10507 pstate->encoder = g723_24_encoder ;
10508 data->blocksize = G723_24_BYTES_PER_BLOCK ;
10509 data->samplesperblock = G723_24_SAMPLES_PER_BLOCK ;
10510 pstate->codec_bits = 3 ;
10511 break ;
10513 case G721_32_BITS_PER_SAMPLE : /* 4 bits per sample. */
10514 pstate->encoder = g721_encoder ;
10515 data->blocksize = G721_32_BYTES_PER_BLOCK ;
10516 data->samplesperblock = G721_32_SAMPLES_PER_BLOCK ;
10517 pstate->codec_bits = 4 ;
10518 break ;
10520 case G721_40_BITS_PER_SAMPLE : /* 5 bits per sample. */
10521 pstate->encoder = g723_40_encoder ;
10522 data->blocksize = G721_40_BYTES_PER_BLOCK ;
10523 data->samplesperblock = G721_40_SAMPLES_PER_BLOCK ;
10524 pstate->codec_bits = 5 ;
10525 break ;
10527 default : return 1 ;
10530 return 0 ;
10531 } /* g72x_writer_init */
10533 int unpack_bytes (G72x_DATA *data, int bits)
10534 { unsigned int in_buffer = 0 ;
10535 unsigned char in_byte ;
10536 int k, in_bits = 0, bindex = 0 ;
10538 for (k = 0 ; bindex <= data->blocksize && k < G72x_BLOCK_SIZE ; k++)
10539 { if (in_bits < bits)
10540 { in_byte = data->block [bindex++] ;
10542 in_buffer |= (in_byte << in_bits);
10543 in_bits += 8;
10545 data->samples [k] = in_buffer & ((1 << bits) - 1);
10546 in_buffer >>= bits;
10547 in_bits -= bits;
10550 return k ;
10551 } /* unpack_bytes */
10553 int g72x_decode_block (G72x_DATA *data)
10554 { G72x_STATE *pstate ;
10555 int k, count ;
10557 pstate = (G72x_STATE*) data->sprivateo ;
10559 count = unpack_bytes (data, pstate->codec_bits) ;
10561 for (k = 0 ; k < count ; k++)
10562 data->samples [k] = pstate->decoder (data->samples [k], pstate) ;
10564 return 0 ;
10565 } /* g72x_decode_block */
10567 int pack_bytes (G72x_DATA *data, int bits)
10569 unsigned int out_buffer = 0 ;
10570 int k, bindex = 0, out_bits = 0 ;
10571 unsigned char out_byte ;
10573 for (k = 0 ; k < G72x_BLOCK_SIZE ; k++)
10574 { out_buffer |= (data->samples [k] << out_bits) ;
10575 out_bits += bits ;
10576 if (out_bits >= 8)
10577 { out_byte = out_buffer & 0xFF ;
10578 out_bits -= 8 ;
10579 out_buffer >>= 8 ;
10580 data->block [bindex++] = out_byte ;
10584 return bindex ;
10585 } /* pack_bytes */
10587 int g72x_encode_block (G72x_DATA *data)
10588 { G72x_STATE *pstate ;
10589 int k, count ;
10591 pstate = (G72x_STATE*) data->sprivateo ;
10593 for (k = 0 ; k < data->samplesperblock ; k++)
10594 data->samples [k] = pstate->encoder (data->samples [k], pstate) ;
10596 count = pack_bytes (data, pstate->codec_bits) ;
10598 return count ;
10599 } /* g72x_encode_block */
10602 * predictor_zero()
10604 * computes the estimated signal from 6-zero predictor.
10607 int predictor_zero (G72x_STATE *state_ptr)
10609 int i;
10610 int sezi;
10612 sezi = fmult(state_ptr->b[0] >> 2, state_ptr->dq[0]);
10613 for (i = 1; i < 6; i++) /* ACCUM */
10614 sezi += fmult(state_ptr->b[i] >> 2, state_ptr->dq[i]);
10615 return (sezi);
10618 * predictor_pole()
10620 * computes the estimated signal from 2-pole predictor.
10623 int predictor_pole(G72x_STATE *state_ptr)
10625 return (fmult(state_ptr->a[1] >> 2, state_ptr->sr[1]) +
10626 fmult(state_ptr->a[0] >> 2, state_ptr->sr[0]));
10629 * step_size()
10631 * computes the quantization step size of the adaptive quantizer.
10634 int step_size (G72x_STATE *state_ptr)
10636 int y;
10637 int dif;
10638 int al;
10640 if (state_ptr->ap >= 256)
10641 return (state_ptr->yu);
10642 else {
10643 y = state_ptr->yl >> 6;
10644 dif = state_ptr->yu - y;
10645 al = state_ptr->ap >> 2;
10646 if (dif > 0)
10647 y += (dif * al) >> 6;
10648 else if (dif < 0)
10649 y += (dif * al + 0x3F) >> 6;
10650 return (y);
10655 * quantize()
10657 * Given a raw sample, 'd', of the difference signal and a
10658 * quantization step size scale factor, 'y', this routine returns the
10659 * ADPCM codeword to which that sample gets quantized. The step
10660 * size scale factor division operation is done in the log base 2 domain
10661 * as a subtraction.
10663 int quantize(
10664 int d, /* Raw difference signal sample */
10665 int y, /* Step size multiplier */
10666 short *table, /* quantization table */
10667 int size) /* table size of short integers */
10669 short dqm; /* Magnitude of 'd' */
10670 short expon; /* Integer part of base 2 log of 'd' */
10671 short mant; /* Fractional part of base 2 log */
10672 short dl; /* Log of magnitude of 'd' */
10673 short dln; /* Step size scale factor normalized log */
10674 int i;
10677 * LOG
10679 * Compute base 2 log of 'd', and store in 'dl'.
10681 dqm = abs(d);
10682 expon = quan(dqm >> 1, power2, 15);
10683 mant = ((dqm << 7) >> expon) & 0x7F; /* Fractional portion. */
10684 dl = (expon << 7) + mant;
10687 * SUBTB
10689 * "Divide" by step size multiplier.
10691 dln = dl - (y >> 2);
10694 * QUAN
10696 * Obtain codword i for 'd'.
10698 i = quan(dln, table, size);
10699 if (d < 0) /* take 1's complement of i */
10700 return ((size << 1) + 1 - i);
10701 else if (i == 0) /* take 1's complement of 0 */
10702 return ((size << 1) + 1); /* new in 1988 */
10703 else
10704 return (i);
10707 * reconstruct()
10709 * Returns reconstructed difference signal 'dq' obtained from
10710 * codeword 'i' and quantization step size scale factor 'y'.
10711 * Multiplication is performed in log base 2 domain as addition.
10714 reconstruct(
10715 int sign, /* 0 for non-negative value */
10716 int dqln, /* G.72x codeword */
10717 int y) /* Step size multiplier */
10719 short dql; /* Log of 'dq' magnitude */
10720 short dex; /* Integer part of log */
10721 short dqt;
10722 short dq; /* Reconstructed difference signal sample */
10724 dql = dqln + (y >> 2); /* ADDA */
10726 if (dql < 0) {
10727 return ((sign) ? -0x8000 : 0);
10728 } else { /* ANTILOG */
10729 dex = (dql >> 7) & 15;
10730 dqt = 128 + (dql & 127);
10731 dq = (dqt << 7) >> (14 - dex);
10732 return ((sign) ? (dq - 0x8000) : dq);
10738 * update()
10740 * updates the state variables for each output code
10742 void
10743 update(
10744 int code_size, /* distinguish 723_40 with others */
10745 int y, /* quantizer step size */
10746 int wi, /* scale factor multiplier */
10747 int fi, /* for long/short term energies */
10748 int dq, /* quantized prediction difference */
10749 int sr, /* reconstructed signal */
10750 int dqsez, /* difference from 2-pole predictor */
10751 G72x_STATE *state_ptr) /* coder state pointer */
10753 int cnt;
10754 short mag, expon; /* Adaptive predictor, FLOAT A */
10755 short a2p = 0; /* LIMC */
10756 short a1ul; /* UPA1 */
10757 short pks1; /* UPA2 */
10758 short fa1;
10759 char tr; /* tone/transition detector */
10760 short ylint, thr2, dqthr;
10761 short ylfrac, thr1;
10762 short pk0;
10764 pk0 = (dqsez < 0) ? 1 : 0; /* needed in updating predictor poles */
10766 mag = dq & 0x7FFF; /* prediction difference magnitude */
10767 /* TRANS */
10768 ylint = state_ptr->yl >> 15; /* exponent part of yl */
10769 ylfrac = (state_ptr->yl >> 10) & 0x1F; /* fractional part of yl */
10770 thr1 = (32 + ylfrac) << ylint; /* threshold */
10771 thr2 = (ylint > 9) ? 31 << 10 : thr1; /* limit thr2 to 31 << 10 */
10772 dqthr = (thr2 + (thr2 >> 1)) >> 1; /* dqthr = 0.75 * thr2 */
10773 if (state_ptr->td == 0) /* signal supposed voice */
10774 tr = 0;
10775 else if (mag <= dqthr) /* supposed data, but small mag */
10776 tr = 0; /* treated as voice */
10777 else /* signal is data (modem) */
10778 tr = 1;
10781 * Quantizer scale factor adaptation.
10784 /* FUNCTW & FILTD & DELAY */
10785 /* update non-steady state step size multiplier */
10786 state_ptr->yu = y + ((wi - y) >> 5);
10788 /* LIMB */
10789 if (state_ptr->yu < 544) /* 544 <= yu <= 5120 */
10790 state_ptr->yu = 544;
10791 else if (state_ptr->yu > 5120)
10792 state_ptr->yu = 5120;
10794 /* FILTE & DELAY */
10795 /* update steady state step size multiplier */
10796 state_ptr->yl += state_ptr->yu + ((-state_ptr->yl) >> 6);
10799 * Adaptive predictor coefficients.
10801 if (tr == 1) { /* reset a's and b's for modem signal */
10802 state_ptr->a[0] = 0;
10803 state_ptr->a[1] = 0;
10804 state_ptr->b[0] = 0;
10805 state_ptr->b[1] = 0;
10806 state_ptr->b[2] = 0;
10807 state_ptr->b[3] = 0;
10808 state_ptr->b[4] = 0;
10809 state_ptr->b[5] = 0;
10810 } else { /* update a's and b's */
10811 pks1 = pk0 ^ state_ptr->pk[0]; /* UPA2 */
10813 /* update predictor pole a[1] */
10814 a2p = state_ptr->a[1] - (state_ptr->a[1] >> 7);
10815 if (dqsez != 0) {
10816 fa1 = (pks1) ? state_ptr->a[0] : -state_ptr->a[0];
10817 if (fa1 < -8191) /* a2p = function of fa1 */
10818 a2p -= 0x100;
10819 else if (fa1 > 8191)
10820 a2p += 0xFF;
10821 else
10822 a2p += fa1 >> 5;
10824 if (pk0 ^ state_ptr->pk[1])
10825 { /* LIMC */
10826 if (a2p <= -12160)
10827 a2p = -12288;
10828 else if (a2p >= 12416)
10829 a2p = 12288;
10830 else
10831 a2p -= 0x80;
10833 else if (a2p <= -12416)
10834 a2p = -12288;
10835 else if (a2p >= 12160)
10836 a2p = 12288;
10837 else
10838 a2p += 0x80;
10841 /* TRIGB & DELAY */
10842 state_ptr->a[1] = a2p;
10844 /* UPA1 */
10845 /* update predictor pole a[0] */
10846 state_ptr->a[0] -= state_ptr->a[0] >> 8;
10847 if (dqsez != 0)
10848 { if (pks1 == 0)
10849 state_ptr->a[0] += 192;
10850 else
10851 state_ptr->a[0] -= 192;
10854 /* LIMD */
10855 a1ul = 15360 - a2p;
10856 if (state_ptr->a[0] < -a1ul)
10857 state_ptr->a[0] = -a1ul;
10858 else if (state_ptr->a[0] > a1ul)
10859 state_ptr->a[0] = a1ul;
10861 /* UPB : update predictor zeros b[6] */
10862 for (cnt = 0; cnt < 6; cnt++) {
10863 if (code_size == 5) /* for 40Kbps G.723 */
10864 state_ptr->b[cnt] -= state_ptr->b[cnt] >> 9;
10865 else /* for G.721 and 24Kbps G.723 */
10866 state_ptr->b[cnt] -= state_ptr->b[cnt] >> 8;
10867 if (dq & 0x7FFF) { /* XOR */
10868 if ((dq ^ state_ptr->dq[cnt]) >= 0)
10869 state_ptr->b[cnt] += 128;
10870 else
10871 state_ptr->b[cnt] -= 128;
10876 for (cnt = 5; cnt > 0; cnt--)
10877 state_ptr->dq[cnt] = state_ptr->dq[cnt-1];
10878 /* FLOAT A : convert dq[0] to 4-bit exp, 6-bit mantissa f.p. */
10879 if (mag == 0) {
10880 state_ptr->dq[0] = (dq >= 0) ? 0x20 : 0xFC20;
10881 } else {
10882 expon = quan(mag, power2, 15);
10883 state_ptr->dq[0] = (dq >= 0) ?
10884 (expon << 6) + ((mag << 6) >> expon) :
10885 (expon << 6) + ((mag << 6) >> expon) - 0x400;
10888 state_ptr->sr[1] = state_ptr->sr[0];
10889 /* FLOAT B : convert sr to 4-bit exp., 6-bit mantissa f.p. */
10890 if (sr == 0) {
10891 state_ptr->sr[0] = 0x20;
10892 } else if (sr > 0) {
10893 expon = quan(sr, power2, 15);
10894 state_ptr->sr[0] = (expon << 6) + ((sr << 6) >> expon);
10895 } else if (sr > -32768) {
10896 mag = -sr;
10897 expon = quan(mag, power2, 15);
10898 state_ptr->sr[0] = (expon << 6) + ((mag << 6) >> expon) - 0x400;
10899 } else
10900 state_ptr->sr[0] = (short) 0xFC20;
10902 /* DELAY A */
10903 state_ptr->pk[1] = state_ptr->pk[0];
10904 state_ptr->pk[0] = pk0;
10906 /* TONE */
10907 if (tr == 1) /* this sample has been treated as data */
10908 state_ptr->td = 0; /* next one will be treated as voice */
10909 else if (a2p < -11776) /* small sample-to-sample correlation */
10910 state_ptr->td = 1; /* signal may be data */
10911 else /* signal is voice */
10912 state_ptr->td = 0;
10915 * Adaptation speed control.
10917 state_ptr->dms += (fi - state_ptr->dms) >> 5; /* FILTA */
10918 state_ptr->dml += (((fi << 2) - state_ptr->dml) >> 7); /* FILTB */
10920 if (tr == 1)
10921 state_ptr->ap = 256;
10922 else if (y < 1536) /* SUBTC */
10923 state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
10924 else if (state_ptr->td == 1)
10925 state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
10926 else if (abs((state_ptr->dms << 2) - state_ptr->dml) >=
10927 (state_ptr->dml >> 3))
10928 state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
10929 else
10930 state_ptr->ap += (-state_ptr->ap) >> 4;
10932 return ;
10933 } /* update */
10936 ** Do not edit or modify anything in this comment block.
10937 ** The arch-tag line is a file identity tag for the GNU Arch
10938 ** revision control system.
10940 ** arch-tag: 6298dc75-fd0f-4062-9b90-f73ed69f22d4
10944 ** Copyright (C) 1999-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
10946 ** This program is free software; you can redistribute it and/or modify
10947 ** it under the terms of the GNU Lesser General Public License as published by
10948 ** the Free Software Foundation; either version 2.1 of the License, or
10949 ** (at your option) any later version.
10951 ** This program is distributed in the hope that it will be useful,
10952 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
10953 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10954 ** GNU Lesser General Public License for more details.
10956 ** You should have received a copy of the GNU Lesser General Public License
10957 ** along with this program; if not, write to the Free Software
10958 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
10962 #include <stdio.h>
10963 #include <stdlib.h>
10964 #include <string.h>
10967 #define GSM610_BLOCKSIZE 33
10968 #define GSM610_SAMPLES 160
10970 typedef struct gsm610_tag
10971 { int blocks ;
10972 int blockcount, samplecount ;
10973 int samplesperblock, blocksize ;
10975 int (*decode_block) (SF_PRIVATE *psf, struct gsm610_tag *pgsm610) ;
10976 int (*encode_block) (SF_PRIVATE *psf, struct gsm610_tag *pgsm610) ;
10978 short samples [WAV_W64_GSM610_SAMPLES] ;
10979 unsigned char block [WAV_W64_GSM610_BLOCKSIZE] ;
10981 gsm gsm_data ;
10982 } GSM610_PRIVATE ;
10984 static sf_count_t gsm610_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
10985 static sf_count_t gsm610_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
10986 static sf_count_t gsm610_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
10987 static sf_count_t gsm610_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
10989 static sf_count_t gsm610_write_s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
10990 static sf_count_t gsm610_write_i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
10991 static sf_count_t gsm610_write_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
10992 static sf_count_t gsm610_write_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
10994 static int gsm610_read_block (SF_PRIVATE *psf, GSM610_PRIVATE *pgsm610, short *ptr, int len) ;
10995 static int gsm610_write_block (SF_PRIVATE *psf, GSM610_PRIVATE *pgsm610, short *ptr, int len) ;
10997 static int gsm610_decode_block (SF_PRIVATE *psf, GSM610_PRIVATE *pgsm610) ;
10998 static int gsm610_encode_block (SF_PRIVATE *psf, GSM610_PRIVATE *pgsm610) ;
11000 static int gsm610_wav_decode_block (SF_PRIVATE *psf, GSM610_PRIVATE *pgsm610) ;
11001 static int gsm610_wav_encode_block (SF_PRIVATE *psf, GSM610_PRIVATE *pgsm610) ;
11003 static sf_count_t gsm610_seek (SF_PRIVATE *psf, int mode, sf_count_t offset) ;
11005 static int gsm610_close (SF_PRIVATE *psf) ;
11007 /*============================================================================================
11008 ** WAV GSM610 initialisation function.
11012 gsm610_init (SF_PRIVATE *psf)
11013 { GSM610_PRIVATE *pgsm610 ;
11014 int true_flag = 1 ;
11016 if (psf->mode == SFM_RDWR)
11017 return SFE_BAD_MODE_RW ;
11019 psf->sf.seekable = SF_FALSE ;
11021 if (! (pgsm610 = malloc (sizeof (GSM610_PRIVATE))))
11022 return SFE_MALLOC_FAILED ;
11024 psf->fdata = (void*) pgsm610 ;
11026 memset (pgsm610, 0, sizeof (GSM610_PRIVATE)) ;
11028 /*============================================================
11030 Need separate gsm_data structs for encode and decode.
11032 ============================================================*/
11034 if (! (pgsm610->gsm_data = gsm_create ()))
11035 return SFE_MALLOC_FAILED ;
11037 if ((psf->sf.format & SF_FORMAT_TYPEMASK) == SF_FORMAT_WAV ||
11038 (psf->sf.format & SF_FORMAT_TYPEMASK) == SF_FORMAT_W64)
11039 { gsm_option (pgsm610->gsm_data, GSM_OPT_WAV49, &true_flag) ;
11041 pgsm610->encode_block = gsm610_wav_encode_block ;
11042 pgsm610->decode_block = gsm610_wav_decode_block ;
11044 pgsm610->samplesperblock = WAV_W64_GSM610_SAMPLES ;
11045 pgsm610->blocksize = WAV_W64_GSM610_BLOCKSIZE ;
11047 else
11048 { pgsm610->encode_block = gsm610_encode_block ;
11049 pgsm610->decode_block = gsm610_decode_block ;
11051 pgsm610->samplesperblock = GSM610_SAMPLES ;
11052 pgsm610->blocksize = GSM610_BLOCKSIZE ;
11055 if (psf->mode == SFM_READ)
11056 { if (psf->datalength % pgsm610->blocksize)
11057 { psf_log_printf (psf, "*** Warning : data chunk seems to be truncated.\n") ;
11058 pgsm610->blocks = psf->datalength / pgsm610->blocksize + 1 ;
11060 else
11061 pgsm610->blocks = psf->datalength / pgsm610->blocksize ;
11063 psf->sf.frames = pgsm610->samplesperblock * pgsm610->blocks ;
11065 pgsm610->decode_block (psf, pgsm610) ; /* Read first block. */
11067 psf->read_short = gsm610_read_s ;
11068 psf->read_int = gsm610_read_i ;
11069 psf->read_float = gsm610_read_f ;
11070 psf->read_double = gsm610_read_d ;
11073 if (psf->mode == SFM_WRITE)
11074 { pgsm610->blockcount = 0 ;
11075 pgsm610->samplecount = 0 ;
11077 psf->write_short = gsm610_write_s ;
11078 psf->write_int = gsm610_write_i ;
11079 psf->write_float = gsm610_write_f ;
11080 psf->write_double = gsm610_write_d ;
11083 psf->close = gsm610_close ;
11084 psf->seek = gsm610_seek ;
11086 psf->filelength = psf_get_filelen (psf) ;
11087 psf->datalength = psf->filelength - psf->dataoffset ;
11089 return 0 ;
11090 } /* gsm610_init */
11092 /*============================================================================================
11093 ** GSM 6.10 Read Functions.
11096 static int
11097 gsm610_wav_decode_block (SF_PRIVATE *psf, GSM610_PRIVATE *pgsm610)
11098 { int k ;
11100 pgsm610->blockcount ++ ;
11101 pgsm610->samplecount = 0 ;
11103 if (pgsm610->blockcount > pgsm610->blocks)
11104 { memset (pgsm610->samples, 0, WAV_W64_GSM610_SAMPLES * sizeof (short)) ;
11105 return 1 ;
11108 if ((k = psf_fread (pgsm610->block, 1, WAV_W64_GSM610_BLOCKSIZE, psf)) != WAV_W64_GSM610_BLOCKSIZE)
11109 psf_log_printf (psf, "*** Warning : short read (%d != %d).\n", k, WAV_W64_GSM610_BLOCKSIZE) ;
11111 if (gsm_decode (pgsm610->gsm_data, pgsm610->block, pgsm610->samples) < 0)
11112 { psf_log_printf (psf, "Error from gsm_decode() on frame : %d\n", pgsm610->blockcount) ;
11113 return 0 ;
11116 if (gsm_decode (pgsm610->gsm_data, pgsm610->block + (WAV_W64_GSM610_BLOCKSIZE + 1) / 2, pgsm610->samples + WAV_W64_GSM610_SAMPLES / 2) < 0)
11117 { psf_log_printf (psf, "Error from gsm_decode() on frame : %d.5\n", pgsm610->blockcount) ;
11118 return 0 ;
11121 return 1 ;
11122 } /* gsm610_wav_decode_block */
11124 static int
11125 gsm610_decode_block (SF_PRIVATE *psf, GSM610_PRIVATE *pgsm610)
11126 { int k ;
11128 pgsm610->blockcount ++ ;
11129 pgsm610->samplecount = 0 ;
11131 if (pgsm610->blockcount > pgsm610->blocks)
11132 { memset (pgsm610->samples, 0, GSM610_SAMPLES * sizeof (short)) ;
11133 return 1 ;
11136 if ((k = psf_fread (pgsm610->block, 1, GSM610_BLOCKSIZE, psf)) != GSM610_BLOCKSIZE)
11137 psf_log_printf (psf, "*** Warning : short read (%d != %d).\n", k, GSM610_BLOCKSIZE) ;
11139 if (gsm_decode (pgsm610->gsm_data, pgsm610->block, pgsm610->samples) < 0)
11140 { psf_log_printf (psf, "Error from gsm_decode() on frame : %d\n", pgsm610->blockcount) ;
11141 return 0 ;
11144 return 1 ;
11145 } /* gsm610_decode_block */
11147 static int
11148 gsm610_read_block (SF_PRIVATE *psf, GSM610_PRIVATE *pgsm610, short *ptr, int len)
11149 { int count, total = 0, indx = 0 ;
11151 while (indx < len)
11152 { if (pgsm610->blockcount >= pgsm610->blocks && pgsm610->samplecount >= pgsm610->samplesperblock)
11153 { memset (&(ptr [indx]), 0, (size_t) ((len - indx) * sizeof (short))) ;
11154 return total ;
11157 if (pgsm610->samplecount >= pgsm610->samplesperblock)
11158 pgsm610->decode_block (psf, pgsm610) ;
11160 count = pgsm610->samplesperblock - pgsm610->samplecount ;
11161 count = (len - indx > count) ? count : len - indx ;
11163 memcpy (&(ptr [indx]), &(pgsm610->samples [pgsm610->samplecount]), count * sizeof (short)) ;
11164 indx += count ;
11165 pgsm610->samplecount += count ;
11166 total = indx ;
11169 return total ;
11170 } /* gsm610_read_block */
11172 static sf_count_t
11173 gsm610_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
11174 { GSM610_PRIVATE *pgsm610 ;
11175 int readcount, count ;
11176 sf_count_t total = 0 ;
11178 if (! psf->fdata)
11179 return 0 ;
11180 pgsm610 = (GSM610_PRIVATE*) psf->fdata ;
11182 while (len > 0)
11183 { readcount = (len > 0x10000000) ? 0x1000000 : (int) len ;
11185 count = gsm610_read_block (psf, pgsm610, ptr, readcount) ;
11187 total += count ;
11188 len -= count ;
11190 if (count != readcount)
11191 break ;
11194 return total ;
11195 } /* gsm610_read_s */
11197 static sf_count_t
11198 gsm610_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
11199 { GSM610_PRIVATE *pgsm610 ;
11200 short *sptr ;
11201 int k, bufferlen, readcount = 0, count ;
11202 sf_count_t total = 0 ;
11204 if (! psf->fdata)
11205 return 0 ;
11206 pgsm610 = (GSM610_PRIVATE*) psf->fdata ;
11208 sptr = (short*) psf->buffer ;
11209 bufferlen = SF_BUFFER_LEN / sizeof (short) ;
11210 while (len > 0)
11211 { readcount = (len >= bufferlen) ? bufferlen : len ;
11212 count = gsm610_read_block (psf, pgsm610, sptr, readcount) ;
11213 for (k = 0 ; k < readcount ; k++)
11214 ptr [total + k] = sptr [k] << 16 ;
11216 total += count ;
11217 len -= readcount ;
11219 return total ;
11220 } /* gsm610_read_i */
11222 static sf_count_t
11223 gsm610_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
11224 { GSM610_PRIVATE *pgsm610 ;
11225 short *sptr ;
11226 int k, bufferlen, readcount = 0, count ;
11227 sf_count_t total = 0 ;
11228 float normfact ;
11230 if (! psf->fdata)
11231 return 0 ;
11232 pgsm610 = (GSM610_PRIVATE*) psf->fdata ;
11234 normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x8000) : 1.0 ;
11236 sptr = (short*) psf->buffer ;
11237 bufferlen = SF_BUFFER_LEN / sizeof (short) ;
11238 while (len > 0)
11239 { readcount = (len >= bufferlen) ? bufferlen : len ;
11240 count = gsm610_read_block (psf, pgsm610, sptr, readcount) ;
11241 for (k = 0 ; k < readcount ; k++)
11242 ptr [total + k] = normfact * sptr [k] ;
11244 total += count ;
11245 len -= readcount ;
11247 return total ;
11248 } /* gsm610_read_f */
11250 static sf_count_t
11251 gsm610_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
11252 { GSM610_PRIVATE *pgsm610 ;
11253 short *sptr ;
11254 int k, bufferlen, readcount = 0, count ;
11255 sf_count_t total = 0 ;
11256 double normfact ;
11258 normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x8000) : 1.0 ;
11260 if (! psf->fdata)
11261 return 0 ;
11262 pgsm610 = (GSM610_PRIVATE*) psf->fdata ;
11264 sptr = (short*) psf->buffer ;
11265 bufferlen = SF_BUFFER_LEN / sizeof (short) ;
11266 while (len > 0)
11267 { readcount = (len >= bufferlen) ? bufferlen : len ;
11268 count = gsm610_read_block (psf, pgsm610, sptr, readcount) ;
11269 for (k = 0 ; k < readcount ; k++)
11270 ptr [total + k] = normfact * sptr [k] ;
11272 total += count ;
11273 len -= readcount ;
11275 return total ;
11276 } /* gsm610_read_d */
11278 static sf_count_t
11279 gsm610_seek (SF_PRIVATE *psf, int mode, sf_count_t offset)
11280 { GSM610_PRIVATE *pgsm610 ;
11281 int newblock, newsample ;
11283 mode = mode ;
11285 if (! psf->fdata)
11286 return 0 ;
11287 pgsm610 = (GSM610_PRIVATE*) psf->fdata ;
11289 if (psf->dataoffset < 0)
11290 { psf->error = SFE_BAD_SEEK ;
11291 return ((sf_count_t) -1) ;
11294 if (offset == 0)
11295 { int true_flag = 1 ;
11297 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
11298 pgsm610->blockcount = 0 ;
11300 gsm_init (pgsm610->gsm_data) ;
11301 if ((psf->sf.format & SF_FORMAT_TYPEMASK) == SF_FORMAT_WAV ||
11302 (psf->sf.format & SF_FORMAT_TYPEMASK) == SF_FORMAT_W64)
11303 gsm_option (pgsm610->gsm_data, GSM_OPT_WAV49, &true_flag) ;
11305 pgsm610->decode_block (psf, pgsm610) ;
11306 pgsm610->samplecount = 0 ;
11307 return 0 ;
11310 if (offset < 0 || offset > pgsm610->blocks * pgsm610->samplesperblock)
11311 { psf->error = SFE_BAD_SEEK ;
11312 return ((sf_count_t) -1) ;
11315 newblock = offset / pgsm610->samplesperblock ;
11316 newsample = offset % pgsm610->samplesperblock ;
11318 if (psf->mode == SFM_READ)
11319 { if (psf->read_current != newblock * pgsm610->samplesperblock + newsample)
11320 { psf_fseek (psf, psf->dataoffset + newblock * pgsm610->samplesperblock, SEEK_SET) ;
11321 pgsm610->blockcount = newblock ;
11322 pgsm610->decode_block (psf, pgsm610) ;
11323 pgsm610->samplecount = newsample ;
11326 return newblock * pgsm610->samplesperblock + newsample ;
11329 /* What to do about write??? */
11330 psf->error = SFE_BAD_SEEK ;
11331 return ((sf_count_t) -1) ;
11332 } /* gsm610_seek */
11334 /*==========================================================================================
11335 ** GSM 6.10 Write Functions.
11338 static int
11339 gsm610_encode_block (SF_PRIVATE *psf, GSM610_PRIVATE *pgsm610)
11340 { int k ;
11342 /* Encode the samples. */
11343 gsm_encode (pgsm610->gsm_data, pgsm610->samples, pgsm610->block) ;
11345 /* Write the block to disk. */
11346 if ((k = psf_fwrite (pgsm610->block, 1, GSM610_BLOCKSIZE, psf)) != GSM610_BLOCKSIZE)
11347 psf_log_printf (psf, "*** Warning : short write (%d != %d).\n", k, GSM610_BLOCKSIZE) ;
11349 pgsm610->samplecount = 0 ;
11350 pgsm610->blockcount ++ ;
11352 /* Set samples to zero for next block. */
11353 memset (pgsm610->samples, 0, WAV_W64_GSM610_SAMPLES * sizeof (short)) ;
11355 return 1 ;
11356 } /* gsm610_encode_block */
11358 static int
11359 gsm610_wav_encode_block (SF_PRIVATE *psf, GSM610_PRIVATE *pgsm610)
11360 { int k ;
11362 /* Encode the samples. */
11363 gsm_encode (pgsm610->gsm_data, pgsm610->samples, pgsm610->block) ;
11364 gsm_encode (pgsm610->gsm_data, pgsm610->samples+WAV_W64_GSM610_SAMPLES/2, pgsm610->block+WAV_W64_GSM610_BLOCKSIZE/2) ;
11366 /* Write the block to disk. */
11367 if ((k = psf_fwrite (pgsm610->block, 1, WAV_W64_GSM610_BLOCKSIZE, psf)) != WAV_W64_GSM610_BLOCKSIZE)
11368 psf_log_printf (psf, "*** Warning : short write (%d != %d).\n", k, WAV_W64_GSM610_BLOCKSIZE) ;
11370 pgsm610->samplecount = 0 ;
11371 pgsm610->blockcount ++ ;
11373 /* Set samples to zero for next block. */
11374 memset (pgsm610->samples, 0, WAV_W64_GSM610_SAMPLES * sizeof (short)) ;
11376 return 1 ;
11377 } /* gsm610_wav_encode_block */
11379 static int
11380 gsm610_write_block (SF_PRIVATE *psf, GSM610_PRIVATE *pgsm610, short *ptr, int len)
11381 { int count, total = 0, indx = 0 ;
11383 while (indx < len)
11384 { count = pgsm610->samplesperblock - pgsm610->samplecount ;
11386 if (count > len - indx)
11387 count = len - indx ;
11389 memcpy (&(pgsm610->samples [pgsm610->samplecount]), &(ptr [indx]), count * sizeof (short)) ;
11390 indx += count ;
11391 pgsm610->samplecount += count ;
11392 total = indx ;
11394 if (pgsm610->samplecount >= pgsm610->samplesperblock)
11395 pgsm610->encode_block (psf, pgsm610) ;
11398 return total ;
11399 } /* gsm610_write_block */
11401 static sf_count_t
11402 gsm610_write_s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
11403 { GSM610_PRIVATE *pgsm610 ;
11404 int writecount, count ;
11405 sf_count_t total = 0 ;
11407 if (! psf->fdata)
11408 return 0 ;
11409 pgsm610 = (GSM610_PRIVATE*) psf->fdata ;
11411 while (len > 0)
11412 { writecount = (len > 0x10000000) ? 0x10000000 : (int) len ;
11414 count = gsm610_write_block (psf, pgsm610, ptr, writecount) ;
11416 total += count ;
11417 len -= count ;
11419 if (count != writecount)
11420 break ;
11423 return total ;
11424 } /* gsm610_write_s */
11426 static sf_count_t
11427 gsm610_write_i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
11428 { GSM610_PRIVATE *pgsm610 ;
11429 short *sptr ;
11430 int k, bufferlen, writecount = 0, count ;
11431 sf_count_t total = 0 ;
11433 if (! psf->fdata)
11434 return 0 ;
11435 pgsm610 = (GSM610_PRIVATE*) psf->fdata ;
11437 sptr = (short*) psf->buffer ;
11438 bufferlen = SF_BUFFER_LEN / sizeof (short) ;
11439 while (len > 0)
11440 { writecount = (len >= bufferlen) ? bufferlen : len ;
11441 for (k = 0 ; k < writecount ; k++)
11442 sptr [k] = ptr [total + k] >> 16 ;
11443 count = gsm610_write_block (psf, pgsm610, sptr, writecount) ;
11445 total += count ;
11446 len -= writecount ;
11448 return total ;
11449 } /* gsm610_write_i */
11451 static sf_count_t
11452 gsm610_write_f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
11453 { GSM610_PRIVATE *pgsm610 ;
11454 short *sptr ;
11455 int k, bufferlen, writecount = 0, count ;
11456 sf_count_t total = 0 ;
11457 float normfact ;
11459 if (! psf->fdata)
11460 return 0 ;
11461 pgsm610 = (GSM610_PRIVATE*) psf->fdata ;
11463 normfact = (psf->norm_float == SF_TRUE) ? (1.0 * 0x7FFF) : 1.0 ;
11465 sptr = (short*) psf->buffer ;
11466 bufferlen = SF_BUFFER_LEN / sizeof (short) ;
11467 while (len > 0)
11468 { writecount = (len >= bufferlen) ? bufferlen : len ;
11469 for (k = 0 ; k < writecount ; k++)
11470 sptr [k] = lrintf (normfact * ptr [total + k]) ;
11471 count = gsm610_write_block (psf, pgsm610, sptr, writecount) ;
11473 total += count ;
11474 len -= writecount ;
11476 return total ;
11477 } /* gsm610_write_f */
11479 static sf_count_t
11480 gsm610_write_d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
11481 { GSM610_PRIVATE *pgsm610 ;
11482 short *sptr ;
11483 int k, bufferlen, writecount = 0, count ;
11484 sf_count_t total = 0 ;
11485 double normfact ;
11487 if (! psf->fdata)
11488 return 0 ;
11489 pgsm610 = (GSM610_PRIVATE*) psf->fdata ;
11491 normfact = (psf->norm_double == SF_TRUE) ? (1.0 * 0x7FFF) : 1.0 ;
11493 sptr = (short*) psf->buffer ;
11494 bufferlen = SF_BUFFER_LEN / sizeof (short) ;
11495 while (len > 0)
11496 { writecount = (len >= bufferlen) ? bufferlen : len ;
11497 for (k = 0 ; k < writecount ; k++)
11498 sptr [k] = lrint (normfact * ptr [total + k]) ;
11499 count = gsm610_write_block (psf, pgsm610, sptr, writecount) ;
11501 total += count ;
11502 len -= writecount ;
11504 return total ;
11505 } /* gsm610_write_d */
11507 static int
11508 gsm610_close (SF_PRIVATE *psf)
11509 { GSM610_PRIVATE *pgsm610 ;
11511 if (! psf->fdata)
11512 return 0 ;
11514 pgsm610 = (GSM610_PRIVATE*) psf->fdata ;
11516 if (psf->mode == SFM_WRITE)
11517 { /* If a block has been partially assembled, write it out
11518 ** as the final block.
11521 if (pgsm610->samplecount && pgsm610->samplecount < pgsm610->samplesperblock)
11522 pgsm610->encode_block (psf, pgsm610) ;
11524 if (psf->write_header)
11525 psf->write_header (psf, SF_TRUE) ;
11528 if (pgsm610->gsm_data)
11529 gsm_destroy (pgsm610->gsm_data) ;
11531 return 0 ;
11532 } /* gsm610_close */
11535 ** Do not edit or modify anything in this comment block.
11536 ** The arch-tag line is a file identity tag for the GNU Arch
11537 ** revision control system.
11539 ** arch-tag: 8575187d-af4f-4acf-b9dd-6ff705628345
11542 * Copyright 1992 by Jutta Degener and Carsten Bormann, Technische
11543 * Universitaet Berlin. See the accompanying file "COPYRIGHT" for
11544 * details. THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE.
11548 #include <stdio.h>
11549 #include <stdlib.h>
11550 #include <string.h>
11555 gsm gsm_create (void)
11557 gsm r;
11559 r = malloc (sizeof(struct gsm_state));
11560 if (!r) return r;
11562 memset((char *)r, 0, sizeof (struct gsm_state));
11563 r->nrp = 40;
11565 return r;
11568 /* Added for libsndfile : May 6, 2002. Not sure if it works. */
11569 void gsm_init (gsm state)
11571 memset (state, 0, sizeof (struct gsm_state)) ;
11572 state->nrp = 40 ;
11575 ** Do not edit or modify anything in this comment block.
11576 ** The arch-tag line is a file identity tag for the GNU Arch
11577 ** revision control system.
11579 ** arch-tag: 9fedb6b3-ed99-40c2-aac1-484c536261fe
11583 * Copyright 1992 by Jutta Degener and Carsten Bormann, Technische
11584 * Universitaet Berlin. See the accompanying file "COPYRIGHT" for
11585 * details. THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE.
11590 int gsm_decode (gsm s, gsm_byte * c, gsm_signal * target)
11592 word LARc[8], Nc[4], Mc[4], bc[4], xmaxc[4], xmc[13*4];
11594 #ifdef WAV49
11595 if (s->wav_fmt) {
11597 uword sr = 0;
11599 s->frame_index = !s->frame_index;
11600 if (s->frame_index) {
11602 sr = *c++;
11603 LARc[0] = sr & 0x3f; sr >>= 6;
11604 sr |= (uword)*c++ << 2;
11605 LARc[1] = sr & 0x3f; sr >>= 6;
11606 sr |= (uword)*c++ << 4;
11607 LARc[2] = sr & 0x1f; sr >>= 5;
11608 LARc[3] = sr & 0x1f; sr >>= 5;
11609 sr |= (uword)*c++ << 2;
11610 LARc[4] = sr & 0xf; sr >>= 4;
11611 LARc[5] = sr & 0xf; sr >>= 4;
11612 sr |= (uword)*c++ << 2; /* 5 */
11613 LARc[6] = sr & 0x7; sr >>= 3;
11614 LARc[7] = sr & 0x7; sr >>= 3;
11615 sr |= (uword)*c++ << 4;
11616 Nc[0] = sr & 0x7f; sr >>= 7;
11617 bc[0] = sr & 0x3; sr >>= 2;
11618 Mc[0] = sr & 0x3; sr >>= 2;
11619 sr |= (uword)*c++ << 1;
11620 xmaxc[0] = sr & 0x3f; sr >>= 6;
11621 xmc[0] = sr & 0x7; sr >>= 3;
11622 sr = *c++;
11623 xmc[1] = sr & 0x7; sr >>= 3;
11624 xmc[2] = sr & 0x7; sr >>= 3;
11625 sr |= (uword)*c++ << 2;
11626 xmc[3] = sr & 0x7; sr >>= 3;
11627 xmc[4] = sr & 0x7; sr >>= 3;
11628 xmc[5] = sr & 0x7; sr >>= 3;
11629 sr |= (uword)*c++ << 1; /* 10 */
11630 xmc[6] = sr & 0x7; sr >>= 3;
11631 xmc[7] = sr & 0x7; sr >>= 3;
11632 xmc[8] = sr & 0x7; sr >>= 3;
11633 sr = *c++;
11634 xmc[9] = sr & 0x7; sr >>= 3;
11635 xmc[10] = sr & 0x7; sr >>= 3;
11636 sr |= (uword)*c++ << 2;
11637 xmc[11] = sr & 0x7; sr >>= 3;
11638 xmc[12] = sr & 0x7; sr >>= 3;
11639 sr |= (uword)*c++ << 4;
11640 Nc[1] = sr & 0x7f; sr >>= 7;
11641 bc[1] = sr & 0x3; sr >>= 2;
11642 Mc[1] = sr & 0x3; sr >>= 2;
11643 sr |= (uword)*c++ << 1;
11644 xmaxc[1] = sr & 0x3f; sr >>= 6;
11645 xmc[13] = sr & 0x7; sr >>= 3;
11646 sr = *c++; /* 15 */
11647 xmc[14] = sr & 0x7; sr >>= 3;
11648 xmc[15] = sr & 0x7; sr >>= 3;
11649 sr |= (uword)*c++ << 2;
11650 xmc[16] = sr & 0x7; sr >>= 3;
11651 xmc[17] = sr & 0x7; sr >>= 3;
11652 xmc[18] = sr & 0x7; sr >>= 3;
11653 sr |= (uword)*c++ << 1;
11654 xmc[19] = sr & 0x7; sr >>= 3;
11655 xmc[20] = sr & 0x7; sr >>= 3;
11656 xmc[21] = sr & 0x7; sr >>= 3;
11657 sr = *c++;
11658 xmc[22] = sr & 0x7; sr >>= 3;
11659 xmc[23] = sr & 0x7; sr >>= 3;
11660 sr |= (uword)*c++ << 2;
11661 xmc[24] = sr & 0x7; sr >>= 3;
11662 xmc[25] = sr & 0x7; sr >>= 3;
11663 sr |= (uword)*c++ << 4; /* 20 */
11664 Nc[2] = sr & 0x7f; sr >>= 7;
11665 bc[2] = sr & 0x3; sr >>= 2;
11666 Mc[2] = sr & 0x3; sr >>= 2;
11667 sr |= (uword)*c++ << 1;
11668 xmaxc[2] = sr & 0x3f; sr >>= 6;
11669 xmc[26] = sr & 0x7; sr >>= 3;
11670 sr = *c++;
11671 xmc[27] = sr & 0x7; sr >>= 3;
11672 xmc[28] = sr & 0x7; sr >>= 3;
11673 sr |= (uword)*c++ << 2;
11674 xmc[29] = sr & 0x7; sr >>= 3;
11675 xmc[30] = sr & 0x7; sr >>= 3;
11676 xmc[31] = sr & 0x7; sr >>= 3;
11677 sr |= (uword)*c++ << 1;
11678 xmc[32] = sr & 0x7; sr >>= 3;
11679 xmc[33] = sr & 0x7; sr >>= 3;
11680 xmc[34] = sr & 0x7; sr >>= 3;
11681 sr = *c++; /* 25 */
11682 xmc[35] = sr & 0x7; sr >>= 3;
11683 xmc[36] = sr & 0x7; sr >>= 3;
11684 sr |= (uword)*c++ << 2;
11685 xmc[37] = sr & 0x7; sr >>= 3;
11686 xmc[38] = sr & 0x7; sr >>= 3;
11687 sr |= (uword)*c++ << 4;
11688 Nc[3] = sr & 0x7f; sr >>= 7;
11689 bc[3] = sr & 0x3; sr >>= 2;
11690 Mc[3] = sr & 0x3; sr >>= 2;
11691 sr |= (uword)*c++ << 1;
11692 xmaxc[3] = sr & 0x3f; sr >>= 6;
11693 xmc[39] = sr & 0x7; sr >>= 3;
11694 sr = *c++;
11695 xmc[40] = sr & 0x7; sr >>= 3;
11696 xmc[41] = sr & 0x7; sr >>= 3;
11697 sr |= (uword)*c++ << 2; /* 30 */
11698 xmc[42] = sr & 0x7; sr >>= 3;
11699 xmc[43] = sr & 0x7; sr >>= 3;
11700 xmc[44] = sr & 0x7; sr >>= 3;
11701 sr |= (uword)*c++ << 1;
11702 xmc[45] = sr & 0x7; sr >>= 3;
11703 xmc[46] = sr & 0x7; sr >>= 3;
11704 xmc[47] = sr & 0x7; sr >>= 3;
11705 sr = *c++;
11706 xmc[48] = sr & 0x7; sr >>= 3;
11707 xmc[49] = sr & 0x7; sr >>= 3;
11708 sr |= (uword)*c++ << 2;
11709 xmc[50] = sr & 0x7; sr >>= 3;
11710 xmc[51] = sr & 0x7; sr >>= 3;
11712 s->frame_chain = sr & 0xf;
11714 else {
11715 sr = s->frame_chain;
11716 sr |= (uword)*c++ << 4; /* 1 */
11717 LARc[0] = sr & 0x3f; sr >>= 6;
11718 LARc[1] = sr & 0x3f; sr >>= 6;
11719 sr = *c++;
11720 LARc[2] = sr & 0x1f; sr >>= 5;
11721 sr |= (uword)*c++ << 3;
11722 LARc[3] = sr & 0x1f; sr >>= 5;
11723 LARc[4] = sr & 0xf; sr >>= 4;
11724 sr |= (uword)*c++ << 2;
11725 LARc[5] = sr & 0xf; sr >>= 4;
11726 LARc[6] = sr & 0x7; sr >>= 3;
11727 LARc[7] = sr & 0x7; sr >>= 3;
11728 sr = *c++; /* 5 */
11729 Nc[0] = sr & 0x7f; sr >>= 7;
11730 sr |= (uword)*c++ << 1;
11731 bc[0] = sr & 0x3; sr >>= 2;
11732 Mc[0] = sr & 0x3; sr >>= 2;
11733 sr |= (uword)*c++ << 5;
11734 xmaxc[0] = sr & 0x3f; sr >>= 6;
11735 xmc[0] = sr & 0x7; sr >>= 3;
11736 xmc[1] = sr & 0x7; sr >>= 3;
11737 sr |= (uword)*c++ << 1;
11738 xmc[2] = sr & 0x7; sr >>= 3;
11739 xmc[3] = sr & 0x7; sr >>= 3;
11740 xmc[4] = sr & 0x7; sr >>= 3;
11741 sr = *c++;
11742 xmc[5] = sr & 0x7; sr >>= 3;
11743 xmc[6] = sr & 0x7; sr >>= 3;
11744 sr |= (uword)*c++ << 2; /* 10 */
11745 xmc[7] = sr & 0x7; sr >>= 3;
11746 xmc[8] = sr & 0x7; sr >>= 3;
11747 xmc[9] = sr & 0x7; sr >>= 3;
11748 sr |= (uword)*c++ << 1;
11749 xmc[10] = sr & 0x7; sr >>= 3;
11750 xmc[11] = sr & 0x7; sr >>= 3;
11751 xmc[12] = sr & 0x7; sr >>= 3;
11752 sr = *c++;
11753 Nc[1] = sr & 0x7f; sr >>= 7;
11754 sr |= (uword)*c++ << 1;
11755 bc[1] = sr & 0x3; sr >>= 2;
11756 Mc[1] = sr & 0x3; sr >>= 2;
11757 sr |= (uword)*c++ << 5;
11758 xmaxc[1] = sr & 0x3f; sr >>= 6;
11759 xmc[13] = sr & 0x7; sr >>= 3;
11760 xmc[14] = sr & 0x7; sr >>= 3;
11761 sr |= (uword)*c++ << 1; /* 15 */
11762 xmc[15] = sr & 0x7; sr >>= 3;
11763 xmc[16] = sr & 0x7; sr >>= 3;
11764 xmc[17] = sr & 0x7; sr >>= 3;
11765 sr = *c++;
11766 xmc[18] = sr & 0x7; sr >>= 3;
11767 xmc[19] = sr & 0x7; sr >>= 3;
11768 sr |= (uword)*c++ << 2;
11769 xmc[20] = sr & 0x7; sr >>= 3;
11770 xmc[21] = sr & 0x7; sr >>= 3;
11771 xmc[22] = sr & 0x7; sr >>= 3;
11772 sr |= (uword)*c++ << 1;
11773 xmc[23] = sr & 0x7; sr >>= 3;
11774 xmc[24] = sr & 0x7; sr >>= 3;
11775 xmc[25] = sr & 0x7; sr >>= 3;
11776 sr = *c++;
11777 Nc[2] = sr & 0x7f; sr >>= 7;
11778 sr |= (uword)*c++ << 1; /* 20 */
11779 bc[2] = sr & 0x3; sr >>= 2;
11780 Mc[2] = sr & 0x3; sr >>= 2;
11781 sr |= (uword)*c++ << 5;
11782 xmaxc[2] = sr & 0x3f; sr >>= 6;
11783 xmc[26] = sr & 0x7; sr >>= 3;
11784 xmc[27] = sr & 0x7; sr >>= 3;
11785 sr |= (uword)*c++ << 1;
11786 xmc[28] = sr & 0x7; sr >>= 3;
11787 xmc[29] = sr & 0x7; sr >>= 3;
11788 xmc[30] = sr & 0x7; sr >>= 3;
11789 sr = *c++;
11790 xmc[31] = sr & 0x7; sr >>= 3;
11791 xmc[32] = sr & 0x7; sr >>= 3;
11792 sr |= (uword)*c++ << 2;
11793 xmc[33] = sr & 0x7; sr >>= 3;
11794 xmc[34] = sr & 0x7; sr >>= 3;
11795 xmc[35] = sr & 0x7; sr >>= 3;
11796 sr |= (uword)*c++ << 1; /* 25 */
11797 xmc[36] = sr & 0x7; sr >>= 3;
11798 xmc[37] = sr & 0x7; sr >>= 3;
11799 xmc[38] = sr & 0x7; sr >>= 3;
11800 sr = *c++;
11801 Nc[3] = sr & 0x7f; sr >>= 7;
11802 sr |= (uword)*c++ << 1;
11803 bc[3] = sr & 0x3; sr >>= 2;
11804 Mc[3] = sr & 0x3; sr >>= 2;
11805 sr |= (uword)*c++ << 5;
11806 xmaxc[3] = sr & 0x3f; sr >>= 6;
11807 xmc[39] = sr & 0x7; sr >>= 3;
11808 xmc[40] = sr & 0x7; sr >>= 3;
11809 sr |= (uword)*c++ << 1;
11810 xmc[41] = sr & 0x7; sr >>= 3;
11811 xmc[42] = sr & 0x7; sr >>= 3;
11812 xmc[43] = sr & 0x7; sr >>= 3;
11813 sr = *c++; /* 30 */
11814 xmc[44] = sr & 0x7; sr >>= 3;
11815 xmc[45] = sr & 0x7; sr >>= 3;
11816 sr |= (uword)*c++ << 2;
11817 xmc[46] = sr & 0x7; sr >>= 3;
11818 xmc[47] = sr & 0x7; sr >>= 3;
11819 xmc[48] = sr & 0x7; sr >>= 3;
11820 sr |= (uword)*c++ << 1;
11821 xmc[49] = sr & 0x7; sr >>= 3;
11822 xmc[50] = sr & 0x7; sr >>= 3;
11823 xmc[51] = sr & 0x7; sr >>= 3;
11826 else
11827 #endif
11829 /* GSM_MAGIC = (*c >> 4) & 0xF; */
11831 if (((*c >> 4) & 0x0F) != GSM_MAGIC) return -1;
11833 LARc[0] = (*c++ & 0xF) << 2; /* 1 */
11834 LARc[0] |= (*c >> 6) & 0x3;
11835 LARc[1] = *c++ & 0x3F;
11836 LARc[2] = (*c >> 3) & 0x1F;
11837 LARc[3] = (*c++ & 0x7) << 2;
11838 LARc[3] |= (*c >> 6) & 0x3;
11839 LARc[4] = (*c >> 2) & 0xF;
11840 LARc[5] = (*c++ & 0x3) << 2;
11841 LARc[5] |= (*c >> 6) & 0x3;
11842 LARc[6] = (*c >> 3) & 0x7;
11843 LARc[7] = *c++ & 0x7;
11844 Nc[0] = (*c >> 1) & 0x7F;
11845 bc[0] = (*c++ & 0x1) << 1;
11846 bc[0] |= (*c >> 7) & 0x1;
11847 Mc[0] = (*c >> 5) & 0x3;
11848 xmaxc[0] = (*c++ & 0x1F) << 1;
11849 xmaxc[0] |= (*c >> 7) & 0x1;
11850 xmc[0] = (*c >> 4) & 0x7;
11851 xmc[1] = (*c >> 1) & 0x7;
11852 xmc[2] = (*c++ & 0x1) << 2;
11853 xmc[2] |= (*c >> 6) & 0x3;
11854 xmc[3] = (*c >> 3) & 0x7;
11855 xmc[4] = *c++ & 0x7;
11856 xmc[5] = (*c >> 5) & 0x7;
11857 xmc[6] = (*c >> 2) & 0x7;
11858 xmc[7] = (*c++ & 0x3) << 1; /* 10 */
11859 xmc[7] |= (*c >> 7) & 0x1;
11860 xmc[8] = (*c >> 4) & 0x7;
11861 xmc[9] = (*c >> 1) & 0x7;
11862 xmc[10] = (*c++ & 0x1) << 2;
11863 xmc[10] |= (*c >> 6) & 0x3;
11864 xmc[11] = (*c >> 3) & 0x7;
11865 xmc[12] = *c++ & 0x7;
11866 Nc[1] = (*c >> 1) & 0x7F;
11867 bc[1] = (*c++ & 0x1) << 1;
11868 bc[1] |= (*c >> 7) & 0x1;
11869 Mc[1] = (*c >> 5) & 0x3;
11870 xmaxc[1] = (*c++ & 0x1F) << 1;
11871 xmaxc[1] |= (*c >> 7) & 0x1;
11872 xmc[13] = (*c >> 4) & 0x7;
11873 xmc[14] = (*c >> 1) & 0x7;
11874 xmc[15] = (*c++ & 0x1) << 2;
11875 xmc[15] |= (*c >> 6) & 0x3;
11876 xmc[16] = (*c >> 3) & 0x7;
11877 xmc[17] = *c++ & 0x7;
11878 xmc[18] = (*c >> 5) & 0x7;
11879 xmc[19] = (*c >> 2) & 0x7;
11880 xmc[20] = (*c++ & 0x3) << 1;
11881 xmc[20] |= (*c >> 7) & 0x1;
11882 xmc[21] = (*c >> 4) & 0x7;
11883 xmc[22] = (*c >> 1) & 0x7;
11884 xmc[23] = (*c++ & 0x1) << 2;
11885 xmc[23] |= (*c >> 6) & 0x3;
11886 xmc[24] = (*c >> 3) & 0x7;
11887 xmc[25] = *c++ & 0x7;
11888 Nc[2] = (*c >> 1) & 0x7F;
11889 bc[2] = (*c++ & 0x1) << 1; /* 20 */
11890 bc[2] |= (*c >> 7) & 0x1;
11891 Mc[2] = (*c >> 5) & 0x3;
11892 xmaxc[2] = (*c++ & 0x1F) << 1;
11893 xmaxc[2] |= (*c >> 7) & 0x1;
11894 xmc[26] = (*c >> 4) & 0x7;
11895 xmc[27] = (*c >> 1) & 0x7;
11896 xmc[28] = (*c++ & 0x1) << 2;
11897 xmc[28] |= (*c >> 6) & 0x3;
11898 xmc[29] = (*c >> 3) & 0x7;
11899 xmc[30] = *c++ & 0x7;
11900 xmc[31] = (*c >> 5) & 0x7;
11901 xmc[32] = (*c >> 2) & 0x7;
11902 xmc[33] = (*c++ & 0x3) << 1;
11903 xmc[33] |= (*c >> 7) & 0x1;
11904 xmc[34] = (*c >> 4) & 0x7;
11905 xmc[35] = (*c >> 1) & 0x7;
11906 xmc[36] = (*c++ & 0x1) << 2;
11907 xmc[36] |= (*c >> 6) & 0x3;
11908 xmc[37] = (*c >> 3) & 0x7;
11909 xmc[38] = *c++ & 0x7;
11910 Nc[3] = (*c >> 1) & 0x7F;
11911 bc[3] = (*c++ & 0x1) << 1;
11912 bc[3] |= (*c >> 7) & 0x1;
11913 Mc[3] = (*c >> 5) & 0x3;
11914 xmaxc[3] = (*c++ & 0x1F) << 1;
11915 xmaxc[3] |= (*c >> 7) & 0x1;
11916 xmc[39] = (*c >> 4) & 0x7;
11917 xmc[40] = (*c >> 1) & 0x7;
11918 xmc[41] = (*c++ & 0x1) << 2;
11919 xmc[41] |= (*c >> 6) & 0x3;
11920 xmc[42] = (*c >> 3) & 0x7;
11921 xmc[43] = *c++ & 0x7; /* 30 */
11922 xmc[44] = (*c >> 5) & 0x7;
11923 xmc[45] = (*c >> 2) & 0x7;
11924 xmc[46] = (*c++ & 0x3) << 1;
11925 xmc[46] |= (*c >> 7) & 0x1;
11926 xmc[47] = (*c >> 4) & 0x7;
11927 xmc[48] = (*c >> 1) & 0x7;
11928 xmc[49] = (*c++ & 0x1) << 2;
11929 xmc[49] |= (*c >> 6) & 0x3;
11930 xmc[50] = (*c >> 3) & 0x7;
11931 xmc[51] = *c & 0x7; /* 33 */
11934 Gsm_Decoder(s, LARc, Nc, bc, Mc, xmaxc, xmc, target);
11936 return 0;
11939 ** Do not edit or modify anything in this comment block.
11940 ** The arch-tag line is a file identity tag for the GNU Arch
11941 ** revision control system.
11943 ** arch-tag: 6a9b6628-821c-4a96-84c1-485ebd35f170
11947 * Copyright 1992 by Jutta Degener and Carsten Bormann, Technische
11948 * Universitaet Berlin. See the accompanying file "COPYRIGHT" for
11949 * details. THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE.
11953 #ifdef HAS_STDLIB_H
11954 # include <stdlib.h>
11955 #else
11956 # ifdef HAS_MALLOC_H
11957 # include <malloc.h>
11958 # else
11959 extern void free();
11960 # endif
11961 #endif
11963 void gsm_destroy (gsm S)
11965 if (S) free((char *)S);
11968 ** Do not edit or modify anything in this comment block.
11969 ** The arch-tag line is a file identity tag for the GNU Arch
11970 ** revision control system.
11972 ** arch-tag: f423d09b-6ccc-47e0-9b18-ee1cf7a8e473
11976 * Copyright 1992 by Jutta Degener and Carsten Bormann, Technische
11977 * Universitaet Berlin. See the accompanying file "COPYRIGHT" for
11978 * details. THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE.
11982 void gsm_encode (gsm s, gsm_signal * source, gsm_byte * c)
11984 word LARc[8], Nc[4], Mc[4], bc[4], xmaxc[4], xmc[13*4];
11986 Gsm_Coder(s, source, LARc, Nc, bc, Mc, xmaxc, xmc);
11989 /* variable size
11991 GSM_MAGIC 4
11993 LARc[0] 6
11994 LARc[1] 6
11995 LARc[2] 5
11996 LARc[3] 5
11997 LARc[4] 4
11998 LARc[5] 4
11999 LARc[6] 3
12000 LARc[7] 3
12002 Nc[0] 7
12003 bc[0] 2
12004 Mc[0] 2
12005 xmaxc[0] 6
12006 xmc[0] 3
12007 xmc[1] 3
12008 xmc[2] 3
12009 xmc[3] 3
12010 xmc[4] 3
12011 xmc[5] 3
12012 xmc[6] 3
12013 xmc[7] 3
12014 xmc[8] 3
12015 xmc[9] 3
12016 xmc[10] 3
12017 xmc[11] 3
12018 xmc[12] 3
12020 Nc[1] 7
12021 bc[1] 2
12022 Mc[1] 2
12023 xmaxc[1] 6
12024 xmc[13] 3
12025 xmc[14] 3
12026 xmc[15] 3
12027 xmc[16] 3
12028 xmc[17] 3
12029 xmc[18] 3
12030 xmc[19] 3
12031 xmc[20] 3
12032 xmc[21] 3
12033 xmc[22] 3
12034 xmc[23] 3
12035 xmc[24] 3
12036 xmc[25] 3
12038 Nc[2] 7
12039 bc[2] 2
12040 Mc[2] 2
12041 xmaxc[2] 6
12042 xmc[26] 3
12043 xmc[27] 3
12044 xmc[28] 3
12045 xmc[29] 3
12046 xmc[30] 3
12047 xmc[31] 3
12048 xmc[32] 3
12049 xmc[33] 3
12050 xmc[34] 3
12051 xmc[35] 3
12052 xmc[36] 3
12053 xmc[37] 3
12054 xmc[38] 3
12056 Nc[3] 7
12057 bc[3] 2
12058 Mc[3] 2
12059 xmaxc[3] 6
12060 xmc[39] 3
12061 xmc[40] 3
12062 xmc[41] 3
12063 xmc[42] 3
12064 xmc[43] 3
12065 xmc[44] 3
12066 xmc[45] 3
12067 xmc[46] 3
12068 xmc[47] 3
12069 xmc[48] 3
12070 xmc[49] 3
12071 xmc[50] 3
12072 xmc[51] 3
12075 #ifdef WAV49
12077 if (s->wav_fmt) {
12078 s->frame_index = !s->frame_index;
12079 if (s->frame_index) {
12081 uword sr;
12083 sr = 0;
12084 sr = sr >> 6 | LARc[0] << 10;
12085 sr = sr >> 6 | LARc[1] << 10;
12086 *c++ = sr >> 4;
12087 sr = sr >> 5 | LARc[2] << 11;
12088 *c++ = sr >> 7;
12089 sr = sr >> 5 | LARc[3] << 11;
12090 sr = sr >> 4 | LARc[4] << 12;
12091 *c++ = sr >> 6;
12092 sr = sr >> 4 | LARc[5] << 12;
12093 sr = sr >> 3 | LARc[6] << 13;
12094 *c++ = sr >> 7;
12095 sr = sr >> 3 | LARc[7] << 13;
12096 sr = sr >> 7 | Nc[0] << 9;
12097 *c++ = sr >> 5;
12098 sr = sr >> 2 | bc[0] << 14;
12099 sr = sr >> 2 | Mc[0] << 14;
12100 sr = sr >> 6 | xmaxc[0] << 10;
12101 *c++ = sr >> 3;
12102 sr = sr >> 3 | xmc[0] << 13;
12103 *c++ = sr >> 8;
12104 sr = sr >> 3 | xmc[1] << 13;
12105 sr = sr >> 3 | xmc[2] << 13;
12106 sr = sr >> 3 | xmc[3] << 13;
12107 *c++ = sr >> 7;
12108 sr = sr >> 3 | xmc[4] << 13;
12109 sr = sr >> 3 | xmc[5] << 13;
12110 sr = sr >> 3 | xmc[6] << 13;
12111 *c++ = sr >> 6;
12112 sr = sr >> 3 | xmc[7] << 13;
12113 sr = sr >> 3 | xmc[8] << 13;
12114 *c++ = sr >> 8;
12115 sr = sr >> 3 | xmc[9] << 13;
12116 sr = sr >> 3 | xmc[10] << 13;
12117 sr = sr >> 3 | xmc[11] << 13;
12118 *c++ = sr >> 7;
12119 sr = sr >> 3 | xmc[12] << 13;
12120 sr = sr >> 7 | Nc[1] << 9;
12121 *c++ = sr >> 5;
12122 sr = sr >> 2 | bc[1] << 14;
12123 sr = sr >> 2 | Mc[1] << 14;
12124 sr = sr >> 6 | xmaxc[1] << 10;
12125 *c++ = sr >> 3;
12126 sr = sr >> 3 | xmc[13] << 13;
12127 *c++ = sr >> 8;
12128 sr = sr >> 3 | xmc[14] << 13;
12129 sr = sr >> 3 | xmc[15] << 13;
12130 sr = sr >> 3 | xmc[16] << 13;
12131 *c++ = sr >> 7;
12132 sr = sr >> 3 | xmc[17] << 13;
12133 sr = sr >> 3 | xmc[18] << 13;
12134 sr = sr >> 3 | xmc[19] << 13;
12135 *c++ = sr >> 6;
12136 sr = sr >> 3 | xmc[20] << 13;
12137 sr = sr >> 3 | xmc[21] << 13;
12138 *c++ = sr >> 8;
12139 sr = sr >> 3 | xmc[22] << 13;
12140 sr = sr >> 3 | xmc[23] << 13;
12141 sr = sr >> 3 | xmc[24] << 13;
12142 *c++ = sr >> 7;
12143 sr = sr >> 3 | xmc[25] << 13;
12144 sr = sr >> 7 | Nc[2] << 9;
12145 *c++ = sr >> 5;
12146 sr = sr >> 2 | bc[2] << 14;
12147 sr = sr >> 2 | Mc[2] << 14;
12148 sr = sr >> 6 | xmaxc[2] << 10;
12149 *c++ = sr >> 3;
12150 sr = sr >> 3 | xmc[26] << 13;
12151 *c++ = sr >> 8;
12152 sr = sr >> 3 | xmc[27] << 13;
12153 sr = sr >> 3 | xmc[28] << 13;
12154 sr = sr >> 3 | xmc[29] << 13;
12155 *c++ = sr >> 7;
12156 sr = sr >> 3 | xmc[30] << 13;
12157 sr = sr >> 3 | xmc[31] << 13;
12158 sr = sr >> 3 | xmc[32] << 13;
12159 *c++ = sr >> 6;
12160 sr = sr >> 3 | xmc[33] << 13;
12161 sr = sr >> 3 | xmc[34] << 13;
12162 *c++ = sr >> 8;
12163 sr = sr >> 3 | xmc[35] << 13;
12164 sr = sr >> 3 | xmc[36] << 13;
12165 sr = sr >> 3 | xmc[37] << 13;
12166 *c++ = sr >> 7;
12167 sr = sr >> 3 | xmc[38] << 13;
12168 sr = sr >> 7 | Nc[3] << 9;
12169 *c++ = sr >> 5;
12170 sr = sr >> 2 | bc[3] << 14;
12171 sr = sr >> 2 | Mc[3] << 14;
12172 sr = sr >> 6 | xmaxc[3] << 10;
12173 *c++ = sr >> 3;
12174 sr = sr >> 3 | xmc[39] << 13;
12175 *c++ = sr >> 8;
12176 sr = sr >> 3 | xmc[40] << 13;
12177 sr = sr >> 3 | xmc[41] << 13;
12178 sr = sr >> 3 | xmc[42] << 13;
12179 *c++ = sr >> 7;
12180 sr = sr >> 3 | xmc[43] << 13;
12181 sr = sr >> 3 | xmc[44] << 13;
12182 sr = sr >> 3 | xmc[45] << 13;
12183 *c++ = sr >> 6;
12184 sr = sr >> 3 | xmc[46] << 13;
12185 sr = sr >> 3 | xmc[47] << 13;
12186 *c++ = sr >> 8;
12187 sr = sr >> 3 | xmc[48] << 13;
12188 sr = sr >> 3 | xmc[49] << 13;
12189 sr = sr >> 3 | xmc[50] << 13;
12190 *c++ = sr >> 7;
12191 sr = sr >> 3 | xmc[51] << 13;
12192 sr = sr >> 4;
12193 *c = sr >> 8;
12194 s->frame_chain = *c;
12196 else {
12197 uword sr;
12199 sr = 0;
12200 sr = sr >> 4 | s->frame_chain << 12;
12201 sr = sr >> 6 | LARc[0] << 10;
12202 *c++ = sr >> 6;
12203 sr = sr >> 6 | LARc[1] << 10;
12204 *c++ = sr >> 8;
12205 sr = sr >> 5 | LARc[2] << 11;
12206 sr = sr >> 5 | LARc[3] << 11;
12207 *c++ = sr >> 6;
12208 sr = sr >> 4 | LARc[4] << 12;
12209 sr = sr >> 4 | LARc[5] << 12;
12210 *c++ = sr >> 6;
12211 sr = sr >> 3 | LARc[6] << 13;
12212 sr = sr >> 3 | LARc[7] << 13;
12213 *c++ = sr >> 8;
12214 sr = sr >> 7 | Nc[0] << 9;
12215 sr = sr >> 2 | bc[0] << 14;
12216 *c++ = sr >> 7;
12217 sr = sr >> 2 | Mc[0] << 14;
12218 sr = sr >> 6 | xmaxc[0] << 10;
12219 *c++ = sr >> 7;
12220 sr = sr >> 3 | xmc[0] << 13;
12221 sr = sr >> 3 | xmc[1] << 13;
12222 sr = sr >> 3 | xmc[2] << 13;
12223 *c++ = sr >> 6;
12224 sr = sr >> 3 | xmc[3] << 13;
12225 sr = sr >> 3 | xmc[4] << 13;
12226 *c++ = sr >> 8;
12227 sr = sr >> 3 | xmc[5] << 13;
12228 sr = sr >> 3 | xmc[6] << 13;
12229 sr = sr >> 3 | xmc[7] << 13;
12230 *c++ = sr >> 7;
12231 sr = sr >> 3 | xmc[8] << 13;
12232 sr = sr >> 3 | xmc[9] << 13;
12233 sr = sr >> 3 | xmc[10] << 13;
12234 *c++ = sr >> 6;
12235 sr = sr >> 3 | xmc[11] << 13;
12236 sr = sr >> 3 | xmc[12] << 13;
12237 *c++ = sr >> 8;
12238 sr = sr >> 7 | Nc[1] << 9;
12239 sr = sr >> 2 | bc[1] << 14;
12240 *c++ = sr >> 7;
12241 sr = sr >> 2 | Mc[1] << 14;
12242 sr = sr >> 6 | xmaxc[1] << 10;
12243 *c++ = sr >> 7;
12244 sr = sr >> 3 | xmc[13] << 13;
12245 sr = sr >> 3 | xmc[14] << 13;
12246 sr = sr >> 3 | xmc[15] << 13;
12247 *c++ = sr >> 6;
12248 sr = sr >> 3 | xmc[16] << 13;
12249 sr = sr >> 3 | xmc[17] << 13;
12250 *c++ = sr >> 8;
12251 sr = sr >> 3 | xmc[18] << 13;
12252 sr = sr >> 3 | xmc[19] << 13;
12253 sr = sr >> 3 | xmc[20] << 13;
12254 *c++ = sr >> 7;
12255 sr = sr >> 3 | xmc[21] << 13;
12256 sr = sr >> 3 | xmc[22] << 13;
12257 sr = sr >> 3 | xmc[23] << 13;
12258 *c++ = sr >> 6;
12259 sr = sr >> 3 | xmc[24] << 13;
12260 sr = sr >> 3 | xmc[25] << 13;
12261 *c++ = sr >> 8;
12262 sr = sr >> 7 | Nc[2] << 9;
12263 sr = sr >> 2 | bc[2] << 14;
12264 *c++ = sr >> 7;
12265 sr = sr >> 2 | Mc[2] << 14;
12266 sr = sr >> 6 | xmaxc[2] << 10;
12267 *c++ = sr >> 7;
12268 sr = sr >> 3 | xmc[26] << 13;
12269 sr = sr >> 3 | xmc[27] << 13;
12270 sr = sr >> 3 | xmc[28] << 13;
12271 *c++ = sr >> 6;
12272 sr = sr >> 3 | xmc[29] << 13;
12273 sr = sr >> 3 | xmc[30] << 13;
12274 *c++ = sr >> 8;
12275 sr = sr >> 3 | xmc[31] << 13;
12276 sr = sr >> 3 | xmc[32] << 13;
12277 sr = sr >> 3 | xmc[33] << 13;
12278 *c++ = sr >> 7;
12279 sr = sr >> 3 | xmc[34] << 13;
12280 sr = sr >> 3 | xmc[35] << 13;
12281 sr = sr >> 3 | xmc[36] << 13;
12282 *c++ = sr >> 6;
12283 sr = sr >> 3 | xmc[37] << 13;
12284 sr = sr >> 3 | xmc[38] << 13;
12285 *c++ = sr >> 8;
12286 sr = sr >> 7 | Nc[3] << 9;
12287 sr = sr >> 2 | bc[3] << 14;
12288 *c++ = sr >> 7;
12289 sr = sr >> 2 | Mc[3] << 14;
12290 sr = sr >> 6 | xmaxc[3] << 10;
12291 *c++ = sr >> 7;
12292 sr = sr >> 3 | xmc[39] << 13;
12293 sr = sr >> 3 | xmc[40] << 13;
12294 sr = sr >> 3 | xmc[41] << 13;
12295 *c++ = sr >> 6;
12296 sr = sr >> 3 | xmc[42] << 13;
12297 sr = sr >> 3 | xmc[43] << 13;
12298 *c++ = sr >> 8;
12299 sr = sr >> 3 | xmc[44] << 13;
12300 sr = sr >> 3 | xmc[45] << 13;
12301 sr = sr >> 3 | xmc[46] << 13;
12302 *c++ = sr >> 7;
12303 sr = sr >> 3 | xmc[47] << 13;
12304 sr = sr >> 3 | xmc[48] << 13;
12305 sr = sr >> 3 | xmc[49] << 13;
12306 *c++ = sr >> 6;
12307 sr = sr >> 3 | xmc[50] << 13;
12308 sr = sr >> 3 | xmc[51] << 13;
12309 *c++ = sr >> 8;
12313 else
12315 #endif /* WAV49 */
12318 *c++ = ((GSM_MAGIC & 0xF) << 4) /* 1 */
12319 | ((LARc[0] >> 2) & 0xF);
12320 *c++ = ((LARc[0] & 0x3) << 6)
12321 | (LARc[1] & 0x3F);
12322 *c++ = ((LARc[2] & 0x1F) << 3)
12323 | ((LARc[3] >> 2) & 0x7);
12324 *c++ = ((LARc[3] & 0x3) << 6)
12325 | ((LARc[4] & 0xF) << 2)
12326 | ((LARc[5] >> 2) & 0x3);
12327 *c++ = ((LARc[5] & 0x3) << 6)
12328 | ((LARc[6] & 0x7) << 3)
12329 | (LARc[7] & 0x7);
12330 *c++ = ((Nc[0] & 0x7F) << 1)
12331 | ((bc[0] >> 1) & 0x1);
12332 *c++ = ((bc[0] & 0x1) << 7)
12333 | ((Mc[0] & 0x3) << 5)
12334 | ((xmaxc[0] >> 1) & 0x1F);
12335 *c++ = ((xmaxc[0] & 0x1) << 7)
12336 | ((xmc[0] & 0x7) << 4)
12337 | ((xmc[1] & 0x7) << 1)
12338 | ((xmc[2] >> 2) & 0x1);
12339 *c++ = ((xmc[2] & 0x3) << 6)
12340 | ((xmc[3] & 0x7) << 3)
12341 | (xmc[4] & 0x7);
12342 *c++ = ((xmc[5] & 0x7) << 5) /* 10 */
12343 | ((xmc[6] & 0x7) << 2)
12344 | ((xmc[7] >> 1) & 0x3);
12345 *c++ = ((xmc[7] & 0x1) << 7)
12346 | ((xmc[8] & 0x7) << 4)
12347 | ((xmc[9] & 0x7) << 1)
12348 | ((xmc[10] >> 2) & 0x1);
12349 *c++ = ((xmc[10] & 0x3) << 6)
12350 | ((xmc[11] & 0x7) << 3)
12351 | (xmc[12] & 0x7);
12352 *c++ = ((Nc[1] & 0x7F) << 1)
12353 | ((bc[1] >> 1) & 0x1);
12354 *c++ = ((bc[1] & 0x1) << 7)
12355 | ((Mc[1] & 0x3) << 5)
12356 | ((xmaxc[1] >> 1) & 0x1F);
12357 *c++ = ((xmaxc[1] & 0x1) << 7)
12358 | ((xmc[13] & 0x7) << 4)
12359 | ((xmc[14] & 0x7) << 1)
12360 | ((xmc[15] >> 2) & 0x1);
12361 *c++ = ((xmc[15] & 0x3) << 6)
12362 | ((xmc[16] & 0x7) << 3)
12363 | (xmc[17] & 0x7);
12364 *c++ = ((xmc[18] & 0x7) << 5)
12365 | ((xmc[19] & 0x7) << 2)
12366 | ((xmc[20] >> 1) & 0x3);
12367 *c++ = ((xmc[20] & 0x1) << 7)
12368 | ((xmc[21] & 0x7) << 4)
12369 | ((xmc[22] & 0x7) << 1)
12370 | ((xmc[23] >> 2) & 0x1);
12371 *c++ = ((xmc[23] & 0x3) << 6)
12372 | ((xmc[24] & 0x7) << 3)
12373 | (xmc[25] & 0x7);
12374 *c++ = ((Nc[2] & 0x7F) << 1) /* 20 */
12375 | ((bc[2] >> 1) & 0x1);
12376 *c++ = ((bc[2] & 0x1) << 7)
12377 | ((Mc[2] & 0x3) << 5)
12378 | ((xmaxc[2] >> 1) & 0x1F);
12379 *c++ = ((xmaxc[2] & 0x1) << 7)
12380 | ((xmc[26] & 0x7) << 4)
12381 | ((xmc[27] & 0x7) << 1)
12382 | ((xmc[28] >> 2) & 0x1);
12383 *c++ = ((xmc[28] & 0x3) << 6)
12384 | ((xmc[29] & 0x7) << 3)
12385 | (xmc[30] & 0x7);
12386 *c++ = ((xmc[31] & 0x7) << 5)
12387 | ((xmc[32] & 0x7) << 2)
12388 | ((xmc[33] >> 1) & 0x3);
12389 *c++ = ((xmc[33] & 0x1) << 7)
12390 | ((xmc[34] & 0x7) << 4)
12391 | ((xmc[35] & 0x7) << 1)
12392 | ((xmc[36] >> 2) & 0x1);
12393 *c++ = ((xmc[36] & 0x3) << 6)
12394 | ((xmc[37] & 0x7) << 3)
12395 | (xmc[38] & 0x7);
12396 *c++ = ((Nc[3] & 0x7F) << 1)
12397 | ((bc[3] >> 1) & 0x1);
12398 *c++ = ((bc[3] & 0x1) << 7)
12399 | ((Mc[3] & 0x3) << 5)
12400 | ((xmaxc[3] >> 1) & 0x1F);
12401 *c++ = ((xmaxc[3] & 0x1) << 7)
12402 | ((xmc[39] & 0x7) << 4)
12403 | ((xmc[40] & 0x7) << 1)
12404 | ((xmc[41] >> 2) & 0x1);
12405 *c++ = ((xmc[41] & 0x3) << 6) /* 30 */
12406 | ((xmc[42] & 0x7) << 3)
12407 | (xmc[43] & 0x7);
12408 *c++ = ((xmc[44] & 0x7) << 5)
12409 | ((xmc[45] & 0x7) << 2)
12410 | ((xmc[46] >> 1) & 0x3);
12411 *c++ = ((xmc[46] & 0x1) << 7)
12412 | ((xmc[47] & 0x7) << 4)
12413 | ((xmc[48] & 0x7) << 1)
12414 | ((xmc[49] >> 2) & 0x1);
12415 *c++ = ((xmc[49] & 0x3) << 6)
12416 | ((xmc[50] & 0x7) << 3)
12417 | (xmc[51] & 0x7);
12422 ** Do not edit or modify anything in this comment block.
12423 ** The arch-tag line is a file identity tag for the GNU Arch
12424 ** revision control system.
12426 ** arch-tag: cfe9c43d-d97c-4216-b5e5-ccd6a25b582b
12430 * Copyright 1992 by Jutta Degener and Carsten Bormann, Technische
12431 * Universitaet Berlin. See the accompanying file "COPYRIGHT" for
12432 * details. THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE.
12437 int gsm_option (gsm r, int opt, int * val)
12439 int result = -1;
12441 switch (opt) {
12442 case GSM_OPT_LTP_CUT:
12443 #ifdef LTP_CUT
12444 result = r->ltp_cut;
12445 if (val) r->ltp_cut = *val;
12446 #endif
12447 break;
12449 case GSM_OPT_VERBOSE:
12450 #ifndef NDEBUG
12451 result = r->verbose;
12452 if (val) r->verbose = *val;
12453 #endif
12454 break;
12456 case GSM_OPT_FAST:
12458 #if defined(FAST) && defined(USE_FLOAT_MUL)
12459 result = r->fast;
12460 if (val) r->fast = !!*val;
12461 #endif
12462 break;
12464 case GSM_OPT_FRAME_CHAIN:
12466 #ifdef WAV49
12467 result = r->frame_chain;
12468 if (val) r->frame_chain = *val;
12469 #endif
12470 break;
12472 case GSM_OPT_FRAME_INDEX:
12474 #ifdef WAV49
12475 result = r->frame_index;
12476 if (val) r->frame_index = *val;
12477 #endif
12478 break;
12480 case GSM_OPT_WAV49:
12482 #ifdef WAV49
12483 result = r->wav_fmt;
12484 if (val) r->wav_fmt = !!*val;
12485 #endif
12486 break;
12488 default:
12489 break;
12491 return result;
12494 ** Do not edit or modify anything in this comment block.
12495 ** The arch-tag line is a file identity tag for the GNU Arch
12496 ** revision control system.
12498 ** arch-tag: 963ff156-506f-4359-9145-371e9060b030
12502 ** Copyright (C) 2002-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
12504 ** This program is free software; you can redistribute it and/or modify
12505 ** it under the terms of the GNU Lesser General Public License as published by
12506 ** the Free Software Foundation; either version 2.1 of the License, or
12507 ** (at your option) any later version.
12509 ** This program is distributed in the hope that it will be useful,
12510 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12511 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12512 ** GNU Lesser General Public License for more details.
12514 ** You should have received a copy of the GNU Lesser General Public License
12515 ** along with this program; if not, write to the Free Software
12516 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
12519 #include <stdio.h>
12520 #include <fcntl.h>
12521 #include <string.h>
12522 #include <ctype.h>
12525 /*------------------------------------------------------------------------------
12526 ** Macros to handle big/little endian issues.
12529 #define SFE_HTK_BAD_FILE_LEN 1666
12530 #define SFE_HTK_NOT_WAVEFORM 1667
12532 /*------------------------------------------------------------------------------
12533 ** Private static functions.
12536 static int htk_close (SF_PRIVATE *psf) ;
12538 static int htk_write_header (SF_PRIVATE *psf, int calc_length) ;
12539 static int htk_read_header (SF_PRIVATE *psf) ;
12541 /*------------------------------------------------------------------------------
12542 ** Public function.
12546 htk_open (SF_PRIVATE *psf)
12547 { int subformat ;
12548 int error = 0 ;
12550 if (psf->is_pipe)
12551 return SFE_HTK_NO_PIPE ;
12553 if (psf->mode == SFM_READ || (psf->mode == SFM_RDWR && psf->filelength > 0))
12554 { if ((error = htk_read_header (psf)))
12555 return error ;
12558 subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
12560 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
12561 { if ((psf->sf.format & SF_FORMAT_TYPEMASK) != SF_FORMAT_HTK)
12562 return SFE_BAD_OPEN_FORMAT ;
12564 psf->endian = SF_ENDIAN_BIG ;
12566 if (htk_write_header (psf, SF_FALSE))
12567 return psf->error ;
12569 psf->write_header = htk_write_header ;
12572 psf->close = htk_close ;
12574 psf->blockwidth = psf->bytewidth * psf->sf.channels ;
12576 switch (subformat)
12577 { case SF_FORMAT_PCM_16 : /* 16-bit linear PCM. */
12578 error = pcm_init (psf) ;
12579 break ;
12581 default : break ;
12584 return error ;
12585 } /* htk_open */
12587 /*------------------------------------------------------------------------------
12590 static int
12591 htk_close (SF_PRIVATE *psf)
12593 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
12594 htk_write_header (psf, SF_TRUE) ;
12596 return 0 ;
12597 } /* htk_close */
12599 static int
12600 htk_write_header (SF_PRIVATE *psf, int calc_length)
12601 { sf_count_t current ;
12602 int sample_count, sample_period ;
12604 current = psf_ftell (psf) ;
12606 if (calc_length)
12607 psf->filelength = psf_get_filelen (psf) ;
12609 /* Reset the current header length to zero. */
12610 psf->header [0] = 0 ;
12611 psf->headindex = 0 ;
12612 psf_fseek (psf, 0, SEEK_SET) ;
12614 if (psf->filelength > 12)
12615 sample_count = (psf->filelength - 12) / 2 ;
12616 else
12617 sample_count = 0 ;
12619 sample_period = 10000000 / psf->sf.samplerate ;
12621 psf_binheader_writef (psf, "E444", sample_count, sample_period, 0x20000) ;
12623 /* Header construction complete so write it out. */
12624 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
12626 if (psf->error)
12627 return psf->error ;
12629 psf->dataoffset = psf->headindex ;
12631 if (current > 0)
12632 psf_fseek (psf, current, SEEK_SET) ;
12634 return psf->error ;
12635 } /* htk_write_header */
12638 ** Found the following info in a comment block within Bill Schottstaedt's
12639 ** sndlib library.
12641 ** HTK format files consist of a contiguous sequence of samples preceded by a
12642 ** header. Each sample is a vector of either 2-byte integers or 4-byte floats.
12643 ** 2-byte integers are used for compressed forms as described below and for
12644 ** vector quantised data as described later in section 5.11. HTK format data
12645 ** files can also be used to store speech waveforms as described in section 5.8.
12647 ** The HTK file format header is 12 bytes long and contains the following data
12648 ** nSamples -- number of samples in file (4-byte integer)
12649 ** sampPeriod -- sample period in 100ns units (4-byte integer)
12650 ** sampSize -- number of bytes per sample (2-byte integer)
12651 ** parmKind -- a code indicating the sample kind (2-byte integer)
12653 ** The parameter kind consists of a 6 bit code representing the basic
12654 ** parameter kind plus additional bits for each of the possible qualifiers.
12655 ** The basic parameter kind codes are
12657 ** 0 WAVEFORM sampled waveform
12658 ** 1 LPC linear prediction filter coefficients
12659 ** 2 LPREFC linear prediction reflection coefficients
12660 ** 3 LPCEPSTRA LPC cepstral coefficients
12661 ** 4 LPDELCEP LPC cepstra plus delta coefficients
12662 ** 5 IREFC LPC reflection coef in 16 bit integer format
12663 ** 6 MFCC mel-frequency cepstral coefficients
12664 ** 7 FBANK log mel-filter bank channel outputs
12665 ** 8 MELSPEC linear mel-filter bank channel outputs
12666 ** 9 USER user defined sample kind
12667 ** 10 DISCRETE vector quantised data
12669 ** and the bit-encoding for the qualifiers (in octal) is
12670 ** _E 000100 has energy
12671 ** _N 000200 absolute energy suppressed
12672 ** _D 000400 has delta coefficients
12673 ** _A 001000 has acceleration coefficients
12674 ** _C 002000 is compressed
12675 ** _Z 004000 has zero mean static coef.
12676 ** _K 010000 has CRC checksum
12677 ** _O 020000 has 0'th cepstral coef.
12680 static int
12681 htk_read_header (SF_PRIVATE *psf)
12682 { int sample_count, sample_period, marker ;
12684 psf_binheader_readf (psf, "pE444", 0, &sample_count, &sample_period, &marker) ;
12686 if (2 * sample_count + 12 != psf->filelength)
12687 return SFE_HTK_BAD_FILE_LEN ;
12689 if (marker != 0x20000)
12690 return SFE_HTK_NOT_WAVEFORM ;
12692 psf->sf.channels = 1 ;
12693 psf->sf.samplerate = 10000000 / sample_period ;
12695 psf_log_printf (psf, "HTK Waveform file\n Sample Count : %d\n Sample Period : %d => %d Hz\n",
12696 sample_count, sample_period, psf->sf.samplerate) ;
12698 psf->sf.format = SF_FORMAT_HTK | SF_FORMAT_PCM_16 ;
12699 psf->bytewidth = 2 ;
12701 /* HTK always has a 12 byte header. */
12702 psf->dataoffset = 12 ;
12703 psf->endian = SF_ENDIAN_BIG ;
12705 psf->datalength = psf->filelength - psf->dataoffset ;
12707 psf->close = htk_close ;
12709 psf->blockwidth = psf->sf.channels * psf->bytewidth ;
12711 if (! psf->sf.frames && psf->blockwidth)
12712 psf->sf.frames = (psf->filelength - psf->dataoffset) / psf->blockwidth ;
12714 return 0 ;
12715 } /* htk_read_header */
12717 ** Do not edit or modify anything in this comment block.
12718 ** The arch-tag line is a file identity tag for the GNU Arch
12719 ** revision control system.
12721 ** arch-tag: c350e972-082e-4c20-8934-03391a723560
12724 ** Copyright (C) 1999-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
12726 ** This program is free software; you can redistribute it and/or modify
12727 ** it under the terms of the GNU Lesser General Public License as published by
12728 ** the Free Software Foundation; either version 2.1 of the License, or
12729 ** (at your option) any later version.
12731 ** This program is distributed in the hope that it will be useful,
12732 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12733 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12734 ** GNU Lesser General Public License for more details.
12736 ** You should have received a copy of the GNU Lesser General Public License
12737 ** along with this program; if not, write to the Free Software
12738 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
12741 #include <stdio.h>
12742 #include <stdlib.h>
12743 #include <string.h>
12746 typedef struct IMA_ADPCM_PRIVATE_tag
12747 { int (*decode_block) (SF_PRIVATE *psf, struct IMA_ADPCM_PRIVATE_tag *pima) ;
12748 int (*encode_block) (SF_PRIVATE *psf, struct IMA_ADPCM_PRIVATE_tag *pima) ;
12750 int channels, blocksize, samplesperblock, blocks ;
12751 int blockcount, samplecount ;
12752 int previous [2] ;
12753 int stepindx [2] ;
12754 unsigned char *block ;
12755 short *samples ;
12756 #if HAVE_FLEXIBLE_ARRAY
12757 unsigned short data [] ; /* ISO C99 struct flexible array. */
12758 #else
12759 unsigned short data [1] ; /* This is a hack and might not work. */
12760 #endif
12761 } IMA_ADPCM_PRIVATE ;
12763 /*============================================================================================
12764 ** Predefined IMA ADPCM data.
12767 static int ima_indx_adjust [16] =
12768 { -1, -1, -1, -1, /* +0 - +3, decrease the step size */
12769 2, 4, 6, 8, /* +4 - +7, increase the step size */
12770 -1, -1, -1, -1, /* -0 - -3, decrease the step size */
12771 2, 4, 6, 8, /* -4 - -7, increase the step size */
12774 static int ima_step_size [89] =
12775 { 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
12776 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, 130, 143, 157, 173, 190, 209, 230,
12777 253, 279, 307, 337, 371, 408, 449, 494, 544, 598, 658, 724, 796, 876, 963,
12778 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, 3327,
12779 3660, 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630, 9493, 10442,
12780 11487, 12635, 13899, 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794,
12781 32767
12784 static int ima_reader_init (SF_PRIVATE *psf, int blockalign, int samplesperblock) ;
12785 static int ima_writer_init (SF_PRIVATE *psf, int blockalign) ;
12787 static int ima_read_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima, short *ptr, int len) ;
12788 static int ima_write_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima, short *ptr, int len) ;
12790 static sf_count_t ima_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
12791 static sf_count_t ima_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
12792 static sf_count_t ima_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
12793 static sf_count_t ima_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
12795 static sf_count_t ima_write_s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
12796 static sf_count_t ima_write_i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
12797 static sf_count_t ima_write_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
12798 static sf_count_t ima_write_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
12800 static sf_count_t ima_seek (SF_PRIVATE *psf, int mode, sf_count_t offset) ;
12802 static int wav_w64_ima_close (SF_PRIVATE *psf) ;
12803 static int aiff_ima_close (SF_PRIVATE *psf) ;
12805 static int wav_w64_ima_decode_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima) ;
12806 static int wav_w64_ima_encode_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima) ;
12808 /*-static int aiff_ima_reader_init (SF_PRIVATE *psf, int blockalign, int samplesperblock) ;-*/
12809 static int aiff_ima_decode_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima) ;
12810 static int aiff_ima_encode_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima) ;
12813 /*============================================================================================
12814 ** IMA ADPCM Reader initialisation function.
12818 wav_w64_ima_init (SF_PRIVATE *psf, int blockalign, int samplesperblock)
12819 { int error ;
12821 if (psf->mode == SFM_RDWR)
12822 return SFE_BAD_MODE_RW ;
12824 if (psf->mode == SFM_READ)
12825 if ((error = ima_reader_init (psf, blockalign, samplesperblock)))
12826 return error ;
12828 if (psf->mode == SFM_WRITE)
12829 if ((error = ima_writer_init (psf, blockalign)))
12830 return error ;
12832 psf->seek = ima_seek ;
12833 psf->close = wav_w64_ima_close ;
12835 return 0 ;
12836 } /* wav_w64_ima_init */
12838 static int
12839 wav_w64_ima_close (SF_PRIVATE *psf)
12840 { IMA_ADPCM_PRIVATE *pima ;
12842 if (! psf->fdata)
12843 return 0 ;
12845 pima = (IMA_ADPCM_PRIVATE*) psf->fdata ;
12847 if (psf->mode == SFM_WRITE)
12848 { /* If a block has been partially assembled, write it out
12849 ** as the final block.
12851 if (pima->samplecount && pima->samplecount < pima->samplesperblock)
12852 pima->encode_block (psf, pima) ;
12854 psf->sf.frames = pima->samplesperblock * pima->blockcount / psf->sf.channels ;
12856 if (psf->write_header)
12857 psf->write_header (psf, SF_TRUE) ;
12860 free (psf->fdata) ;
12861 psf->fdata = NULL ;
12863 return 0 ;
12864 } /* wav_w64_ima_close */
12867 aiff_ima_init (SF_PRIVATE *psf, int blockalign, int samplesperblock)
12868 { int error ;
12870 if (psf->mode == SFM_RDWR)
12871 return SFE_BAD_MODE_RW ;
12873 if (psf->mode == SFM_READ)
12874 if ((error = ima_reader_init (psf, blockalign, samplesperblock)))
12875 return error ;
12877 if (psf->mode == SFM_WRITE)
12878 if ((error = ima_writer_init (psf, blockalign)))
12879 return error ;
12881 psf->seek = ima_seek ;
12882 psf->close = aiff_ima_close ;
12884 return 0 ;
12885 } /* aiff_ima_init */
12887 static int
12888 aiff_ima_close (SF_PRIVATE *psf)
12889 { IMA_ADPCM_PRIVATE *pima ;
12891 if (! psf->fdata)
12892 return 0 ;
12894 pima = (IMA_ADPCM_PRIVATE*) psf->fdata ;
12896 if (psf->mode == SFM_WRITE)
12897 { /* If a block has been partially assembled, write it out
12898 ** as the final block.
12901 if (pima->samplecount && pima->samplecount < pima->samplesperblock)
12902 pima->encode_block (psf, pima) ;
12904 if (psf->write_header)
12905 psf->write_header (psf, SF_TRUE) ;
12908 free (psf->fdata) ;
12909 psf->fdata = NULL ;
12911 return 0 ;
12912 } /* aiff_ima_close */
12914 /*============================================================================================
12915 ** IMA ADPCM Read Functions.
12918 static int
12919 ima_reader_init (SF_PRIVATE *psf, int blockalign, int samplesperblock)
12920 { IMA_ADPCM_PRIVATE *pima ;
12921 int pimasize, count ;
12923 if (psf->mode != SFM_READ)
12924 return SFE_BAD_MODE_RW ;
12926 pimasize = sizeof (IMA_ADPCM_PRIVATE) + blockalign * psf->sf.channels + 3 * psf->sf.channels * samplesperblock ;
12928 if (! (pima = malloc (pimasize)))
12929 return SFE_MALLOC_FAILED ;
12931 psf->fdata = (void*) pima ;
12933 memset (pima, 0, pimasize) ;
12935 pima->samples = pima->data ;
12936 pima->block = (unsigned char*) (pima->data + samplesperblock * psf->sf.channels) ;
12938 pima->channels = psf->sf.channels ;
12939 pima->blocksize = blockalign ;
12940 pima->samplesperblock = samplesperblock ;
12942 psf->filelength = psf_get_filelen (psf) ;
12943 psf->datalength = (psf->dataend) ? psf->dataend - psf->dataoffset :
12944 psf->filelength - psf->dataoffset ;
12946 if (psf->datalength % pima->blocksize)
12947 pima->blocks = psf->datalength / pima->blocksize + 1 ;
12948 else
12949 pima->blocks = psf->datalength / pima->blocksize ;
12951 switch (psf->sf.format & SF_FORMAT_TYPEMASK)
12952 { case SF_FORMAT_WAV :
12953 case SF_FORMAT_W64 :
12954 count = 2 * (pima->blocksize - 4 * pima->channels) / pima->channels + 1 ;
12956 if (pima->samplesperblock != count)
12957 psf_log_printf (psf, "*** Warning : samplesperblock should be %d.\n", count) ;
12959 pima->decode_block = wav_w64_ima_decode_block ;
12961 psf->sf.frames = pima->samplesperblock * pima->blocks ;
12962 break ;
12964 case SF_FORMAT_AIFF :
12965 psf_log_printf (psf, "still need to check block count\n") ;
12966 pima->decode_block = aiff_ima_decode_block ;
12967 psf->sf.frames = pima->samplesperblock * pima->blocks / pima->channels ;
12968 break ;
12970 default :
12971 psf_log_printf (psf, "ima_reader_init: bad psf->sf.format\n") ;
12972 return SFE_INTERNAL ;
12973 break ;
12976 pima->decode_block (psf, pima) ; /* Read first block. */
12978 psf->read_short = ima_read_s ;
12979 psf->read_int = ima_read_i ;
12980 psf->read_float = ima_read_f ;
12981 psf->read_double = ima_read_d ;
12983 return 0 ;
12984 } /* ima_reader_init */
12986 static int
12987 aiff_ima_decode_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima)
12988 { unsigned char *blockdata ;
12989 int chan, k, diff, bytecode ;
12990 short step, stepindx, predictor, *sampledata ;
12992 static int count = 0 ;
12993 count ++ ;
12995 pima->blockcount += pima->channels ;
12996 pima->samplecount = 0 ;
12998 if (pima->blockcount > pima->blocks)
12999 { memset (pima->samples, 0, pima->samplesperblock * pima->channels * sizeof (short)) ;
13000 return 1 ;
13003 if ((k = psf_fread (pima->block, 1, pima->blocksize * pima->channels, psf)) != pima->blocksize * pima->channels)
13004 psf_log_printf (psf, "*** Warning : short read (%d != %d).\n", k, pima->blocksize) ;
13006 /* Read and check the block header. */
13007 for (chan = 0 ; chan < pima->channels ; chan++)
13008 { blockdata = pima->block + chan * 34 ;
13009 sampledata = pima->samples + chan ;
13011 predictor = (blockdata [0] << 8) | (blockdata [1] & 0x80) ;
13012 stepindx = blockdata [1] & 0x7F ;
13015 if (count < 5)
13016 printf ("\nchan: %d predictor: %d stepindx: %d (%d)\n",
13017 chan, predictor, stepindx, ima_step_size [stepindx]) ;
13019 /* FIXME : Do this a better way. */
13020 if (stepindx < 0) stepindx = 0 ;
13021 else if (stepindx > 88) stepindx = 88 ;
13024 ** Pull apart the packed 4 bit samples and store them in their
13025 ** correct sample positions.
13027 for (k = 0 ; k < pima->blocksize - 2 ; k++)
13028 { bytecode = blockdata [k + 2] ;
13029 sampledata [pima->channels * (2 * k + 0)] = bytecode & 0xF ;
13030 sampledata [pima->channels * (2 * k + 1)] = (bytecode >> 4) & 0xF ;
13033 /* Decode the encoded 4 bit samples. */
13034 for (k = 0 ; k < pima->samplesperblock ; k ++)
13035 { step = ima_step_size [stepindx] ;
13037 bytecode = pima->samples [pima->channels * k + chan] ;
13039 stepindx += ima_indx_adjust [bytecode] ;
13041 if (stepindx < 0) stepindx = 0 ;
13042 else if (stepindx > 88) stepindx = 88 ;
13044 diff = step >> 3 ;
13045 if (bytecode & 1) diff += step >> 2 ;
13046 if (bytecode & 2) diff += step >> 1 ;
13047 if (bytecode & 4) diff += step ;
13048 if (bytecode & 8) diff = -diff ;
13050 predictor += diff ;
13052 pima->samples [pima->channels * k + chan] = predictor ;
13056 if (count < 5)
13058 for (k = 0 ; k < 10 ; k++)
13059 printf ("% 7d,", pima->samples [k]) ;
13060 puts ("") ;
13063 return 1 ;
13064 } /* aiff_ima_decode_block */
13066 static int
13067 aiff_ima_encode_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima)
13068 { int chan, k, step, diff, vpdiff, blockindx, indx ;
13069 short bytecode, mask ;
13071 static int count = 0 ;
13072 if (0 && count == 0)
13073 { pima->samples [0] = 0 ;
13074 printf ("blocksize : %d\n", pima->blocksize) ;
13075 printf ("pima->stepindx [0] : %d\n", pima->stepindx [0]) ;
13077 count ++ ;
13079 /* Encode the block header. */
13080 for (chan = 0 ; chan < pima->channels ; chan ++)
13081 { blockindx = chan * pima->blocksize ;
13083 pima->block [blockindx] = (pima->samples [chan] >> 8) & 0xFF ;
13084 pima->block [blockindx + 1] = (pima->samples [chan] & 0x80) + (pima->stepindx [chan] & 0x7F) ;
13086 pima->previous [chan] = pima->samples [chan] ;
13089 /* Encode second and later samples for every block as a 4 bit value. */
13090 for (k = pima->channels ; k < (pima->samplesperblock * pima->channels) ; k ++)
13091 { chan = (pima->channels > 1) ? (k % 2) : 0 ;
13093 diff = pima->samples [k] - pima->previous [chan] ;
13095 bytecode = 0 ;
13096 step = ima_step_size [pima->stepindx [chan]] ;
13097 vpdiff = step >> 3 ;
13098 if (diff < 0)
13099 { bytecode = 8 ;
13100 diff = -diff ;
13102 mask = 4 ;
13103 while (mask)
13104 { if (diff >= step)
13105 { bytecode |= mask ;
13106 diff -= step ;
13107 vpdiff += step ;
13109 step >>= 1 ;
13110 mask >>= 1 ;
13113 if (bytecode & 8)
13114 pima->previous [chan] -= vpdiff ;
13115 else
13116 pima->previous [chan] += vpdiff ;
13118 if (pima->previous [chan] > 32767)
13119 pima->previous [chan] = 32767 ;
13120 else if (pima->previous [chan] < -32768)
13121 pima->previous [chan] = -32768 ;
13123 pima->stepindx [chan] += ima_indx_adjust [bytecode] ;
13124 if (pima->stepindx [chan] < 0)
13125 pima->stepindx [chan] = 0 ;
13126 else if (pima->stepindx [chan] > 88)
13127 pima->stepindx [chan] = 88 ;
13129 pima->samples [k] = bytecode ;
13132 /* Pack the 4 bit encoded samples. */
13134 for (chan = 0 ; chan < pima->channels ; chan ++)
13135 { for (indx = pima->channels ; indx < pima->channels * pima->samplesperblock ; indx += 2 * pima->channels)
13136 { blockindx = chan * pima->blocksize + 2 + indx / 2;
13138 if (0 && count ++ < 5)
13139 printf ("chan: %d blockindx: %3d indx: %3d\n", chan, blockindx, indx) ;
13141 pima->block [blockindx] = pima->samples [indx] & 0x0F ;
13142 pima->block [blockindx] |= (pima->samples [indx + pima->channels] << 4) & 0xF0 ;
13146 /* Write the block to disk. */
13148 if ((k = psf_fwrite (pima->block, 1, pima->channels * pima->blocksize, psf)) != pima->channels * pima->blocksize)
13149 psf_log_printf (psf, "*** Warning : short write (%d != %d).\n", k, pima->channels * pima->blocksize) ;
13151 memset (pima->samples, 0, pima->channels * pima->samplesperblock * sizeof (short)) ;
13152 pima->samplecount = 0 ;
13153 pima->blockcount ++ ;
13155 return 1 ;
13156 } /* aiff_ima_encode_block */
13158 static int
13159 wav_w64_ima_decode_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima)
13160 { int chan, k, current, blockindx, indx, indxstart, diff ;
13161 short step, bytecode, stepindx [2] ;
13163 pima->blockcount ++ ;
13164 pima->samplecount = 0 ;
13166 if (pima->blockcount > pima->blocks)
13167 { memset (pima->samples, 0, pima->samplesperblock * pima->channels * sizeof (short)) ;
13168 return 1 ;
13171 if ((k = psf_fread (pima->block, 1, pima->blocksize, psf)) != pima->blocksize)
13172 psf_log_printf (psf, "*** Warning : short read (%d != %d).\n", k, pima->blocksize) ;
13174 /* Read and check the block header. */
13176 for (chan = 0 ; chan < pima->channels ; chan++)
13177 { current = pima->block [chan*4] | (pima->block [chan*4+1] << 8) ;
13178 if (current & 0x8000)
13179 current -= 0x10000 ;
13181 stepindx [chan] = pima->block [chan*4+2] ;
13182 if (stepindx [chan] < 0)
13183 stepindx [chan] = 0 ;
13184 else if (stepindx [chan] > 88)
13185 stepindx [chan] = 88 ;
13187 if (pima->block [chan*4+3] != 0)
13188 psf_log_printf (psf, "IMA ADPCM synchronisation error.\n") ;
13190 pima->samples [chan] = current ;
13194 ** Pull apart the packed 4 bit samples and store them in their
13195 ** correct sample positions.
13198 blockindx = 4 * pima->channels ;
13200 indxstart = pima->channels ;
13201 while (blockindx < pima->blocksize)
13202 { for (chan = 0 ; chan < pima->channels ; chan++)
13203 { indx = indxstart + chan ;
13204 for (k = 0 ; k < 4 ; k++)
13205 { bytecode = pima->block [blockindx++] ;
13206 pima->samples [indx] = bytecode & 0x0F ;
13207 indx += pima->channels ;
13208 pima->samples [indx] = (bytecode >> 4) & 0x0F ;
13209 indx += pima->channels ;
13212 indxstart += 8 * pima->channels ;
13215 /* Decode the encoded 4 bit samples. */
13217 for (k = pima->channels ; k < (pima->samplesperblock * pima->channels) ; k ++)
13218 { chan = (pima->channels > 1) ? (k % 2) : 0 ;
13220 bytecode = pima->samples [k] & 0xF ;
13222 step = ima_step_size [stepindx [chan]] ;
13223 current = pima->samples [k - pima->channels] ;
13225 diff = step >> 3 ;
13226 if (bytecode & 1)
13227 diff += step >> 2 ;
13228 if (bytecode & 2)
13229 diff += step >> 1 ;
13230 if (bytecode & 4)
13231 diff += step ;
13232 if (bytecode & 8)
13233 diff = -diff ;
13235 current += diff ;
13237 if (current > 32767)
13238 current = 32767 ;
13239 else if (current < -32768)
13240 current = -32768 ;
13242 stepindx [chan] += ima_indx_adjust [bytecode] ;
13244 if (stepindx [chan] < 0)
13245 stepindx [chan] = 0 ;
13246 else if (stepindx [chan] > 88)
13247 stepindx [chan] = 88 ;
13249 pima->samples [k] = current ;
13252 return 1 ;
13253 } /* wav_w64_ima_decode_block */
13255 static int
13256 wav_w64_ima_encode_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima)
13257 { int chan, k, step, diff, vpdiff, blockindx, indx, indxstart ;
13258 short bytecode, mask ;
13260 /* Encode the block header. */
13261 for (chan = 0 ; chan < pima->channels ; chan++)
13262 { pima->block [chan*4] = pima->samples [chan] & 0xFF ;
13263 pima->block [chan*4+1] = (pima->samples [chan] >> 8) & 0xFF ;
13265 pima->block [chan*4+2] = pima->stepindx [chan] ;
13266 pima->block [chan*4+3] = 0 ;
13268 pima->previous [chan] = pima->samples [chan] ;
13271 /* Encode the samples as 4 bit. */
13273 for (k = pima->channels ; k < (pima->samplesperblock * pima->channels) ; k ++)
13274 { chan = (pima->channels > 1) ? (k % 2) : 0 ;
13276 diff = pima->samples [k] - pima->previous [chan] ;
13278 bytecode = 0 ;
13279 step = ima_step_size [pima->stepindx [chan]] ;
13280 vpdiff = step >> 3 ;
13281 if (diff < 0)
13282 { bytecode = 8 ;
13283 diff = -diff ;
13285 mask = 4 ;
13286 while (mask)
13287 { if (diff >= step)
13288 { bytecode |= mask ;
13289 diff -= step ;
13290 vpdiff += step ;
13292 step >>= 1 ;
13293 mask >>= 1 ;
13296 if (bytecode & 8)
13297 pima->previous [chan] -= vpdiff ;
13298 else
13299 pima->previous [chan] += vpdiff ;
13301 if (pima->previous [chan] > 32767)
13302 pima->previous [chan] = 32767 ;
13303 else if (pima->previous [chan] < -32768)
13304 pima->previous [chan] = -32768 ;
13306 pima->stepindx [chan] += ima_indx_adjust [bytecode] ;
13307 if (pima->stepindx [chan] < 0)
13308 pima->stepindx [chan] = 0 ;
13309 else if (pima->stepindx [chan] > 88)
13310 pima->stepindx [chan] = 88 ;
13312 pima->samples [k] = bytecode ;
13315 /* Pack the 4 bit encoded samples. */
13317 blockindx = 4 * pima->channels ;
13319 indxstart = pima->channels ;
13320 while (blockindx < pima->blocksize)
13321 { for (chan = 0 ; chan < pima->channels ; chan++)
13322 { indx = indxstart + chan ;
13323 for (k = 0 ; k < 4 ; k++)
13324 { pima->block [blockindx] = pima->samples [indx] & 0x0F ;
13325 indx += pima->channels ;
13326 pima->block [blockindx] |= (pima->samples [indx] << 4) & 0xF0 ;
13327 indx += pima->channels ;
13328 blockindx ++ ;
13331 indxstart += 8 * pima->channels ;
13334 /* Write the block to disk. */
13336 if ((k = psf_fwrite (pima->block, 1, pima->blocksize, psf)) != pima->blocksize)
13337 psf_log_printf (psf, "*** Warning : short write (%d != %d).\n", k, pima->blocksize) ;
13339 memset (pima->samples, 0, pima->samplesperblock * sizeof (short)) ;
13340 pima->samplecount = 0 ;
13341 pima->blockcount ++ ;
13343 return 1 ;
13344 } /* wav_w64_ima_encode_block */
13346 static int
13347 ima_read_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima, short *ptr, int len)
13348 { int count, total = 0, indx = 0 ;
13350 while (indx < len)
13351 { if (pima->blockcount >= pima->blocks && pima->samplecount >= pima->samplesperblock)
13352 { memset (&(ptr [indx]), 0, (size_t) ((len - indx) * sizeof (short))) ;
13353 return total ;
13356 if (pima->samplecount >= pima->samplesperblock)
13357 pima->decode_block (psf, pima) ;
13359 count = (pima->samplesperblock - pima->samplecount) * pima->channels ;
13360 count = (len - indx > count) ? count : len - indx ;
13362 memcpy (&(ptr [indx]), &(pima->samples [pima->samplecount * pima->channels]), count * sizeof (short)) ;
13363 indx += count ;
13364 pima->samplecount += count / pima->channels ;
13365 total = indx ;
13368 return total ;
13369 } /* ima_read_block */
13371 static sf_count_t
13372 ima_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
13373 { IMA_ADPCM_PRIVATE *pima ;
13374 int readcount, count ;
13375 sf_count_t total = 0 ;
13377 if (! psf->fdata)
13378 return 0 ;
13379 pima = (IMA_ADPCM_PRIVATE*) psf->fdata ;
13381 while (len > 0)
13382 { readcount = (len > 0x10000000) ? 0x10000000 : (int) len ;
13384 count = ima_read_block (psf, pima, ptr, readcount) ;
13386 total += count ;
13387 len -= count ;
13388 if (count != readcount)
13389 break ;
13392 return total ;
13393 } /* ima_read_s */
13395 static sf_count_t
13396 ima_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
13397 { IMA_ADPCM_PRIVATE *pima ;
13398 short *sptr ;
13399 int k, bufferlen, readcount, count ;
13400 sf_count_t total = 0 ;
13402 if (! psf->fdata)
13403 return 0 ;
13404 pima = (IMA_ADPCM_PRIVATE*) psf->fdata ;
13406 sptr = (short*) psf->buffer ;
13407 bufferlen = SF_BUFFER_LEN / sizeof (short) ;
13408 while (len > 0)
13409 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
13410 count = ima_read_block (psf, pima, sptr, readcount) ;
13411 for (k = 0 ; k < readcount ; k++)
13412 ptr [total + k] = ((int) sptr [k]) << 16 ;
13413 total += count ;
13414 len -= readcount ;
13415 if (count != readcount)
13416 break ;
13419 return total ;
13420 } /* ima_read_i */
13422 static sf_count_t
13423 ima_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
13424 { IMA_ADPCM_PRIVATE *pima ;
13425 short *sptr ;
13426 int k, bufferlen, readcount, count ;
13427 sf_count_t total = 0 ;
13428 float normfact ;
13430 if (! psf->fdata)
13431 return 0 ;
13432 pima = (IMA_ADPCM_PRIVATE*) psf->fdata ;
13434 normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x8000) : 1.0 ;
13436 sptr = (short*) psf->buffer ;
13437 bufferlen = SF_BUFFER_LEN / sizeof (short) ;
13438 while (len > 0)
13439 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
13440 count = ima_read_block (psf, pima, sptr, readcount) ;
13441 for (k = 0 ; k < readcount ; k++)
13442 ptr [total + k] = normfact * (float) (sptr [k]) ;
13443 total += count ;
13444 len -= readcount ;
13445 if (count != readcount)
13446 break ;
13449 return total ;
13450 } /* ima_read_f */
13452 static sf_count_t
13453 ima_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
13454 { IMA_ADPCM_PRIVATE *pima ;
13455 short *sptr ;
13456 int k, bufferlen, readcount, count ;
13457 sf_count_t total = 0 ;
13458 double normfact ;
13460 if (! psf->fdata)
13461 return 0 ;
13462 pima = (IMA_ADPCM_PRIVATE*) psf->fdata ;
13464 normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x8000) : 1.0 ;
13466 sptr = (short*) psf->buffer ;
13467 bufferlen = SF_BUFFER_LEN / sizeof (short) ;
13468 while (len > 0)
13469 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
13470 count = ima_read_block (psf, pima, sptr, readcount) ;
13471 for (k = 0 ; k < readcount ; k++)
13472 ptr [total + k] = normfact * (double) (sptr [k]) ;
13473 total += count ;
13474 len -= readcount ;
13475 if (count != readcount)
13476 break ;
13479 return total ;
13480 } /* ima_read_d */
13482 static sf_count_t
13483 ima_seek (SF_PRIVATE *psf, int mode, sf_count_t offset)
13484 { IMA_ADPCM_PRIVATE *pima ;
13485 int newblock, newsample ;
13487 if (! psf->fdata)
13488 return 0 ;
13489 pima = (IMA_ADPCM_PRIVATE*) psf->fdata ;
13491 if (psf->datalength < 0 || psf->dataoffset < 0)
13492 { psf->error = SFE_BAD_SEEK ;
13493 return ((sf_count_t) -1) ;
13496 if (offset == 0)
13497 { psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
13498 pima->blockcount = 0 ;
13499 pima->decode_block (psf, pima) ;
13500 pima->samplecount = 0 ;
13501 return 0 ;
13504 if (offset < 0 || offset > pima->blocks * pima->samplesperblock)
13505 { psf->error = SFE_BAD_SEEK ;
13506 return ((sf_count_t) -1) ;
13509 newblock = offset / pima->samplesperblock ;
13510 newsample = offset % pima->samplesperblock ;
13512 if (mode == SFM_READ)
13513 { psf_fseek (psf, psf->dataoffset + newblock * pima->blocksize, SEEK_SET) ;
13514 pima->blockcount = newblock ;
13515 pima->decode_block (psf, pima) ;
13516 pima->samplecount = newsample ;
13518 else
13519 { /* What to do about write??? */
13520 psf->error = SFE_BAD_SEEK ;
13521 return ((sf_count_t) -1) ;
13524 return newblock * pima->samplesperblock + newsample ;
13525 } /* ima_seek */
13527 /*==========================================================================================
13528 ** IMA ADPCM Write Functions.
13531 static int
13532 ima_writer_init (SF_PRIVATE *psf, int blockalign)
13533 { IMA_ADPCM_PRIVATE *pima ;
13534 int samplesperblock ;
13535 unsigned int pimasize ;
13537 if (psf->mode != SFM_WRITE)
13538 return SFE_BAD_MODE_RW ;
13540 samplesperblock = 2 * (blockalign - 4 * psf->sf.channels) / psf->sf.channels + 1 ;
13542 pimasize = sizeof (IMA_ADPCM_PRIVATE) + blockalign + 3 * psf->sf.channels * samplesperblock ;
13544 if ((pima = calloc (1, pimasize)) == NULL)
13545 return SFE_MALLOC_FAILED ;
13547 psf->fdata = (void*) pima ;
13549 pima->channels = psf->sf.channels ;
13550 pima->blocksize = blockalign ;
13551 pima->samplesperblock = samplesperblock ;
13553 pima->block = (unsigned char*) pima->data ;
13554 pima->samples = (short*) (pima->data + blockalign) ;
13556 pima->samplecount = 0 ;
13558 switch (psf->sf.format & SF_FORMAT_TYPEMASK)
13559 { case SF_FORMAT_WAV :
13560 case SF_FORMAT_W64 :
13561 pima->encode_block = wav_w64_ima_encode_block ;
13562 break ;
13564 case SF_FORMAT_AIFF :
13565 pima->encode_block = aiff_ima_encode_block ;
13566 break ;
13568 default :
13569 psf_log_printf (psf, "ima_reader_init: bad psf->sf.format\n") ;
13570 return SFE_INTERNAL ;
13571 break ;
13574 psf->write_short = ima_write_s ;
13575 psf->write_int = ima_write_i ;
13576 psf->write_float = ima_write_f ;
13577 psf->write_double = ima_write_d ;
13579 return 0 ;
13580 } /* ima_writer_init */
13582 /*==========================================================================================
13585 static int
13586 ima_write_block (SF_PRIVATE *psf, IMA_ADPCM_PRIVATE *pima, short *ptr, int len)
13587 { int count, total = 0, indx = 0 ;
13589 while (indx < len)
13590 { count = (pima->samplesperblock - pima->samplecount) * pima->channels ;
13592 if (count > len - indx)
13593 count = len - indx ;
13595 memcpy (&(pima->samples [pima->samplecount * pima->channels]), &(ptr [total]), count * sizeof (short)) ;
13596 indx += count ;
13597 pima->samplecount += count / pima->channels ;
13598 total = indx ;
13600 if (pima->samplecount >= pima->samplesperblock)
13601 pima->encode_block (psf, pima) ;
13604 return total ;
13605 } /* ima_write_block */
13607 static sf_count_t
13608 ima_write_s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
13609 { IMA_ADPCM_PRIVATE *pima ;
13610 int writecount, count ;
13611 sf_count_t total = 0 ;
13613 if (! psf->fdata)
13614 return 0 ;
13615 pima = (IMA_ADPCM_PRIVATE*) psf->fdata ;
13617 while (len)
13618 { writecount = (len > 0x10000000) ? 0x10000000 : (int) len ;
13620 count = ima_write_block (psf, pima, ptr, writecount) ;
13622 total += count ;
13623 len -= count ;
13624 if (count != writecount)
13625 break ;
13628 return total ;
13629 } /* ima_write_s */
13631 static sf_count_t
13632 ima_write_i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
13633 { IMA_ADPCM_PRIVATE *pima ;
13634 short *sptr ;
13635 int k, bufferlen, writecount, count ;
13636 sf_count_t total = 0 ;
13638 if (! psf->fdata)
13639 return 0 ;
13640 pima = (IMA_ADPCM_PRIVATE*) psf->fdata ;
13642 sptr = (short*) psf->buffer ;
13643 bufferlen = SF_BUFFER_LEN / sizeof (short) ;
13644 while (len > 0)
13645 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
13646 for (k = 0 ; k < writecount ; k++)
13647 sptr [k] = ptr [total + k] >> 16 ;
13648 count = ima_write_block (psf, pima, sptr, writecount) ;
13649 total += count ;
13650 len -= writecount ;
13651 if (count != writecount)
13652 break ;
13655 return total ;
13656 } /* ima_write_i */
13658 static sf_count_t
13659 ima_write_f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
13660 { IMA_ADPCM_PRIVATE *pima ;
13661 short *sptr ;
13662 int k, bufferlen, writecount, count ;
13663 sf_count_t total = 0 ;
13664 float normfact ;
13666 if (! psf->fdata)
13667 return 0 ;
13668 pima = (IMA_ADPCM_PRIVATE*) psf->fdata ;
13670 normfact = (psf->norm_float == SF_TRUE) ? (1.0 * 0x7FFF) : 1.0 ;
13672 sptr = (short*) psf->buffer ;
13673 bufferlen = SF_BUFFER_LEN / sizeof (short) ;
13674 while (len > 0)
13675 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
13676 for (k = 0 ; k < writecount ; k++)
13677 sptr [k] = lrintf (normfact * ptr [total + k]) ;
13678 count = ima_write_block (psf, pima, sptr, writecount) ;
13679 total += count ;
13680 len -= writecount ;
13681 if (count != writecount)
13682 break ;
13685 return total ;
13686 } /* ima_write_f */
13688 static sf_count_t
13689 ima_write_d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
13690 { IMA_ADPCM_PRIVATE *pima ;
13691 short *sptr ;
13692 int k, bufferlen, writecount, count ;
13693 sf_count_t total = 0 ;
13694 double normfact ;
13696 if (! psf->fdata)
13697 return 0 ;
13698 pima = (IMA_ADPCM_PRIVATE*) psf->fdata ;
13700 normfact = (psf->norm_double == SF_TRUE) ? (1.0 * 0x7FFF) : 1.0 ;
13702 sptr = (short*) psf->buffer ;
13703 bufferlen = SF_BUFFER_LEN / sizeof (short) ;
13704 while (len > 0)
13705 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
13706 for (k = 0 ; k < writecount ; k++)
13707 sptr [k] = lrint (normfact * ptr [total + k]) ;
13708 count = ima_write_block (psf, pima, sptr, writecount) ;
13709 total += count ;
13710 len -= writecount ;
13711 if (count != writecount)
13712 break ;
13715 return total ;
13716 } /* ima_write_d */
13720 ** Do not edit or modify anything in this comment block.
13721 ** The arch-tag line is a file identity tag for the GNU Arch
13722 ** revision control system.
13724 ** arch-tag: 75a54b82-ad18-4758-9933-64e00a7f24e0
13727 ** Copyright (C) 2002-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
13729 ** This program is free software; you can redistribute it and/or modify
13730 ** it under the terms of the GNU Lesser General Public License as published by
13731 ** the Free Software Foundation; either version 2.1 of the License, or
13732 ** (at your option) any later version.
13734 ** This program is distributed in the hope that it will be useful,
13735 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
13736 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13737 ** GNU Lesser General Public License for more details.
13739 ** You should have received a copy of the GNU Lesser General Public License
13740 ** along with this program; if not, write to the Free Software
13741 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
13744 #include <stdlib.h>
13747 #define INTERLEAVE_CHANNELS 6
13749 typedef struct
13750 { double buffer [SF_BUFFER_LEN / sizeof (double)] ;
13752 sf_count_t channel_len ;
13754 sf_count_t (*read_short) (SF_PRIVATE*, short *ptr, sf_count_t len) ;
13755 sf_count_t (*read_int) (SF_PRIVATE*, int *ptr, sf_count_t len) ;
13756 sf_count_t (*read_float) (SF_PRIVATE*, float *ptr, sf_count_t len) ;
13757 sf_count_t (*read_double) (SF_PRIVATE*, double *ptr, sf_count_t len) ;
13759 } INTERLEAVE_DATA ;
13763 static sf_count_t interleave_read_short (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
13764 static sf_count_t interleave_read_int (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
13765 static sf_count_t interleave_read_float (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
13766 static sf_count_t interleave_read_double (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
13768 static sf_count_t interleave_seek (SF_PRIVATE*, int mode, sf_count_t samples_from_start) ;
13774 interleave_init (SF_PRIVATE *psf)
13775 { INTERLEAVE_DATA *pdata ;
13777 if (psf->mode != SFM_READ)
13778 return SFE_INTERLEAVE_MODE ;
13780 if (psf->interleave)
13781 { psf_log_printf (psf, "*** Weird, already have interleave.\n") ;
13782 return 666 ;
13785 /* Free this in sf_close() function. */
13786 if (! (pdata = malloc (sizeof (INTERLEAVE_DATA))))
13787 return SFE_MALLOC_FAILED ;
13789 puts ("interleave_init") ;
13791 psf->interleave = pdata ;
13793 /* Save the existing methods. */
13794 pdata->read_short = psf->read_short ;
13795 pdata->read_int = psf->read_int ;
13796 pdata->read_float = psf->read_float ;
13797 pdata->read_double = psf->read_double ;
13799 pdata->channel_len = psf->sf.frames * psf->bytewidth ;
13801 /* Insert our new methods. */
13802 psf->read_short = interleave_read_short ;
13803 psf->read_int = interleave_read_int ;
13804 psf->read_float = interleave_read_float ;
13805 psf->read_double = interleave_read_double ;
13807 psf->seek = interleave_seek ;
13809 return 0 ;
13810 } /* pcm_interleave_init */
13812 /*------------------------------------------------------------------------------
13815 static sf_count_t
13816 interleave_read_short (SF_PRIVATE *psf, short *ptr, sf_count_t len)
13817 { INTERLEAVE_DATA *pdata ;
13818 sf_count_t offset, templen ;
13819 int chan, count, k ;
13820 short *inptr, *outptr ;
13822 if (! (pdata = psf->interleave))
13823 return 0 ;
13825 inptr = (short*) pdata->buffer ;
13827 for (chan = 0 ; chan < psf->sf.channels ; chan++)
13828 { outptr = ptr + chan ;
13830 offset = psf->dataoffset + chan * psf->bytewidth * psf->read_current ;
13832 if (psf_fseek (psf, offset, SEEK_SET) != offset)
13833 { psf->error = SFE_INTERLEAVE_SEEK ;
13834 return 0 ;
13837 templen = len / psf->sf.channels ;
13839 while (templen > 0)
13840 { if (templen > SIGNED_SIZEOF (pdata->buffer) / SIGNED_SIZEOF (short))
13841 count = SIGNED_SIZEOF (pdata->buffer) / SIGNED_SIZEOF (short) ;
13842 else
13843 count = (int) templen ;
13845 if (pdata->read_short (psf, inptr, count) != count)
13846 { psf->error = SFE_INTERLEAVE_READ ;
13847 return 0 ;
13850 for (k = 0 ; k < count ; k++)
13851 { *outptr = inptr [k] ;
13852 outptr += psf->sf.channels ;
13855 templen -= count ;
13859 return len ;
13860 } /* interleave_read_short */
13862 static sf_count_t
13863 interleave_read_int (SF_PRIVATE *psf, int *ptr, sf_count_t len)
13864 { INTERLEAVE_DATA *pdata ;
13865 sf_count_t offset, templen ;
13866 int chan, count, k ;
13867 int *inptr, *outptr ;
13869 if (! (pdata = psf->interleave))
13870 return 0 ;
13872 inptr = (int*) pdata->buffer ;
13874 for (chan = 0 ; chan < psf->sf.channels ; chan++)
13875 { outptr = ptr + chan ;
13877 offset = psf->dataoffset + chan * psf->bytewidth * psf->read_current ;
13879 if (psf_fseek (psf, offset, SEEK_SET) != offset)
13880 { psf->error = SFE_INTERLEAVE_SEEK ;
13881 return 0 ;
13884 templen = len / psf->sf.channels ;
13886 while (templen > 0)
13887 { if (templen > SIGNED_SIZEOF (pdata->buffer) / SIGNED_SIZEOF (int))
13888 count = SIGNED_SIZEOF (pdata->buffer) / SIGNED_SIZEOF (int) ;
13889 else
13890 count = (int) templen ;
13892 if (pdata->read_int (psf, inptr, count) != count)
13893 { psf->error = SFE_INTERLEAVE_READ ;
13894 return 0 ;
13897 for (k = 0 ; k < count ; k++)
13898 { *outptr = inptr [k] ;
13899 outptr += psf->sf.channels ;
13902 templen -= count ;
13906 return len ;
13907 } /* interleave_read_int */
13909 static sf_count_t
13910 interleave_read_float (SF_PRIVATE *psf, float *ptr, sf_count_t len)
13911 { INTERLEAVE_DATA *pdata ;
13912 sf_count_t offset, templen ;
13913 int chan, count, k ;
13914 float *inptr, *outptr ;
13916 if (! (pdata = psf->interleave))
13917 return 0 ;
13919 inptr = (float*) pdata->buffer ;
13921 for (chan = 0 ; chan < psf->sf.channels ; chan++)
13922 { outptr = ptr + chan ;
13924 offset = psf->dataoffset + pdata->channel_len * chan + psf->read_current * psf->bytewidth ;
13926 /*-printf ("chan : %d read_current : %6lld offset : %6lld\n", chan, psf->read_current, offset) ;-*/
13928 if (psf_fseek (psf, offset, SEEK_SET) != offset)
13929 { psf->error = SFE_INTERLEAVE_SEEK ;
13930 /*-puts ("interleave_seek error") ; exit (1) ;-*/
13931 return 0 ;
13934 templen = len / psf->sf.channels ;
13936 while (templen > 0)
13937 { if (templen > SIGNED_SIZEOF (pdata->buffer) / SIGNED_SIZEOF (float))
13938 count = SIGNED_SIZEOF (pdata->buffer) / SIGNED_SIZEOF (float) ;
13939 else
13940 count = (int) templen ;
13942 if (pdata->read_float (psf, inptr, count) != count)
13943 { psf->error = SFE_INTERLEAVE_READ ;
13944 /*-puts ("interleave_read error") ; exit (1) ;-*/
13945 return 0 ;
13948 for (k = 0 ; k < count ; k++)
13949 { *outptr = inptr [k] ;
13950 outptr += psf->sf.channels ;
13953 templen -= count ;
13957 return len ;
13958 } /* interleave_read_float */
13960 static sf_count_t
13961 interleave_read_double (SF_PRIVATE *psf, double *ptr, sf_count_t len)
13962 { INTERLEAVE_DATA *pdata ;
13963 sf_count_t offset, templen ;
13964 int chan, count, k ;
13965 double *inptr, *outptr ;
13967 if (! (pdata = psf->interleave))
13968 return 0 ;
13970 inptr = (double*) pdata->buffer ;
13972 for (chan = 0 ; chan < psf->sf.channels ; chan++)
13973 { outptr = ptr + chan ;
13975 offset = psf->dataoffset + chan * psf->bytewidth * psf->read_current ;
13977 if (psf_fseek (psf, offset, SEEK_SET) != offset)
13978 { psf->error = SFE_INTERLEAVE_SEEK ;
13979 return 0 ;
13982 templen = len / psf->sf.channels ;
13984 while (templen > 0)
13985 { if (templen > SIGNED_SIZEOF (pdata->buffer) / SIGNED_SIZEOF (double))
13986 count = SIGNED_SIZEOF (pdata->buffer) / SIGNED_SIZEOF (double) ;
13987 else
13988 count = (int) templen ;
13990 if (pdata->read_double (psf, inptr, count) != count)
13991 { psf->error = SFE_INTERLEAVE_READ ;
13992 return 0 ;
13995 for (k = 0 ; k < count ; k++)
13996 { *outptr = inptr [k] ;
13997 outptr += psf->sf.channels ;
14000 templen -= count ;
14004 return len ;
14005 } /* interleave_read_double */
14007 /*------------------------------------------------------------------------------
14010 static sf_count_t
14011 interleave_seek (SF_PRIVATE *psf, int mode, sf_count_t samples_from_start)
14012 { psf = psf ; mode = mode ;
14015 ** Do nothing here. This is a place holder to prevent the default
14016 ** seek function from being called.
14019 return samples_from_start ;
14020 } /* interleave_seek */
14022 ** Do not edit or modify anything in this comment block.
14023 ** The arch-tag line is a file identity tag for the GNU Arch
14024 ** revision control system.
14026 ** arch-tag: 82314e13-0225-4408-a2f2-e6dab3f38904
14029 ** Copyright (C) 2001-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
14031 ** This program is free software; you can redistribute it and/or modify
14032 ** it under the terms of the GNU Lesser General Public License as published by
14033 ** the Free Software Foundation; either version 2.1 of the License, or
14034 ** (at your option) any later version.
14036 ** This program is distributed in the hope that it will be useful,
14037 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
14038 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14039 ** GNU Lesser General Public License for more details.
14041 ** You should have received a copy of the GNU Lesser General Public License
14042 ** along with this program; if not, write to the Free Software
14043 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
14046 #include <stdio.h>
14047 #include <fcntl.h>
14048 #include <string.h>
14049 #include <ctype.h>
14052 /*------------------------------------------------------------------------------
14053 ** Macros to handle big/little endian issues.
14056 /* The IRCAM magic number is weird in that one byte in the number can have
14057 ** values of 0x1, 0x2, 0x03 or 0x04. Hence the need for a marker and a mask.
14060 #define IRCAM_BE_MASK (MAKE_MARKER (0xFF, 0xFF, 0x00, 0xFF))
14061 #define IRCAM_BE_MARKER (MAKE_MARKER (0x64, 0xA3, 0x00, 0x00))
14063 #define IRCAM_LE_MASK (MAKE_MARKER (0xFF, 0x00, 0xFF, 0xFF))
14064 #define IRCAM_LE_MARKER (MAKE_MARKER (0x00, 0x00, 0xA3, 0x64))
14066 #define IRCAM_02B_MARKER (MAKE_MARKER (0x00, 0x02, 0xA3, 0x64))
14067 #define IRCAM_03L_MARKER (MAKE_MARKER (0x64, 0xA3, 0x03, 0x00))
14069 #define IRCAM_DATA_OFFSET (1024)
14071 /*------------------------------------------------------------------------------
14072 ** Typedefs.
14075 enum
14076 { IRCAM_PCM_16 = 0x00002,
14077 IRCAM_FLOAT = 0x00004,
14078 IRCAM_ALAW = 0x10001,
14079 IRCAM_ULAW = 0x20001,
14080 IRCAM_PCM_32 = 0x40004
14084 /*------------------------------------------------------------------------------
14085 ** Private static functions.
14088 static int ircam_close (SF_PRIVATE *psf) ;
14089 static int ircam_write_header (SF_PRIVATE *psf, int calc_length) ;
14090 static int ircam_read_header (SF_PRIVATE *psf) ;
14092 static int get_encoding (int subformat) ;
14094 static const char* get_encoding_str (int encoding) ;
14096 /*------------------------------------------------------------------------------
14097 ** Public function.
14101 ircam_open (SF_PRIVATE *psf)
14102 { int subformat ;
14103 int error = SFE_NO_ERROR ;
14105 if (psf->mode == SFM_READ || (psf->mode == SFM_RDWR && psf->filelength > 0))
14106 { if ((error = ircam_read_header (psf)))
14107 return error ;
14110 subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
14112 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
14113 { if ((psf->sf.format & SF_FORMAT_TYPEMASK) != SF_FORMAT_IRCAM)
14114 return SFE_BAD_OPEN_FORMAT ;
14116 psf->endian = psf->sf.format & SF_FORMAT_ENDMASK ;
14117 if (psf->endian == 0 || psf->endian == SF_ENDIAN_CPU)
14118 psf->endian = (CPU_IS_BIG_ENDIAN) ? SF_ENDIAN_BIG : SF_ENDIAN_LITTLE ;
14120 psf->dataoffset = IRCAM_DATA_OFFSET ;
14122 if ((error = ircam_write_header (psf, SF_FALSE)))
14123 return error ;
14125 psf->write_header = ircam_write_header ;
14128 psf->close = ircam_close ;
14130 switch (subformat)
14131 { case SF_FORMAT_ULAW : /* 8-bit Ulaw encoding. */
14132 error = ulaw_init (psf) ;
14133 break ;
14135 case SF_FORMAT_ALAW : /* 8-bit Alaw encoding. */
14136 error = alaw_init (psf) ;
14137 break ;
14139 case SF_FORMAT_PCM_16 : /* 16-bit linear PCM. */
14140 case SF_FORMAT_PCM_32 : /* 32-bit linear PCM. */
14141 error = pcm_init (psf) ;
14142 break ;
14144 case SF_FORMAT_FLOAT : /* 32-bit linear PCM. */
14145 error = float32_init (psf) ;
14146 break ;
14148 default : break ;
14151 return error ;
14152 } /* ircam_open */
14154 /*------------------------------------------------------------------------------
14157 static int
14158 ircam_read_header (SF_PRIVATE *psf)
14159 { unsigned int marker, encoding ;
14160 float samplerate ;
14161 int error = SFE_NO_ERROR ;
14163 psf_binheader_readf (psf, "epmf44", 0, &marker, &samplerate, &(psf->sf.channels), &encoding) ;
14165 if (((marker & IRCAM_LE_MASK) != IRCAM_LE_MARKER) &&
14166 ((marker & IRCAM_BE_MASK) != IRCAM_BE_MARKER))
14167 { psf_log_printf (psf, "marker: 0x%X\n", marker) ;
14168 return SFE_IRCAM_NO_MARKER ;
14171 psf->endian = SF_ENDIAN_LITTLE ;
14173 if (psf->sf.channels > 256)
14174 { psf_binheader_readf (psf, "Epmf44", 0, &marker, &samplerate, &(psf->sf.channels), &encoding) ;
14176 /* Sanity checking for endian-ness detection. */
14177 if (psf->sf.channels > 256)
14178 { psf_log_printf (psf, "marker: 0x%X\n", marker) ;
14179 return SFE_IRCAM_BAD_CHANNELS ;
14182 psf->endian = SF_ENDIAN_BIG ;
14185 psf_log_printf (psf, "marker: 0x%X\n", marker) ;
14187 psf->sf.samplerate = (int) samplerate ;
14189 psf_log_printf (psf, " Sample Rate : %d\n"
14190 " Channels : %d\n"
14191 " Encoding : %X => %s\n", psf->sf.samplerate, psf->sf.channels, encoding, get_encoding_str (encoding)) ;
14193 switch (encoding)
14194 { case IRCAM_PCM_16 :
14195 psf->bytewidth = 2 ;
14196 psf->blockwidth = psf->sf.channels * psf->bytewidth ;
14198 psf->sf.format = SF_FORMAT_IRCAM | SF_FORMAT_PCM_16 ;
14199 break ;
14201 case IRCAM_PCM_32 :
14202 psf->bytewidth = 4 ;
14203 psf->blockwidth = psf->sf.channels * psf->bytewidth ;
14205 psf->sf.format = SF_FORMAT_IRCAM | SF_FORMAT_PCM_32 ;
14206 break ;
14208 case IRCAM_FLOAT :
14209 psf->bytewidth = 4 ;
14210 psf->blockwidth = psf->sf.channels * psf->bytewidth ;
14212 psf->sf.format = SF_FORMAT_IRCAM | SF_FORMAT_FLOAT ;
14213 break ;
14215 case IRCAM_ALAW :
14216 psf->bytewidth = 1 ;
14217 psf->blockwidth = psf->sf.channels * psf->bytewidth ;
14219 psf->sf.format = SF_FORMAT_IRCAM | SF_FORMAT_ALAW ;
14220 break ;
14222 case IRCAM_ULAW :
14223 psf->bytewidth = 1 ;
14224 psf->blockwidth = psf->sf.channels * psf->bytewidth ;
14226 psf->sf.format = SF_FORMAT_IRCAM | SF_FORMAT_ULAW ;
14227 break ;
14229 default :
14230 error = SFE_IRCAM_UNKNOWN_FORMAT ;
14231 break ;
14234 if (psf->endian == SF_ENDIAN_BIG)
14235 psf->sf.format |= SF_ENDIAN_BIG ;
14236 else
14237 psf->sf.format |= SF_ENDIAN_LITTLE ;
14239 if (error)
14240 return error ;
14242 psf->dataoffset = IRCAM_DATA_OFFSET ;
14243 psf->datalength = psf->filelength - psf->dataoffset ;
14245 if (psf->sf.frames == 0 && psf->blockwidth)
14246 psf->sf.frames = psf->datalength / psf->blockwidth ;
14248 psf_log_printf (psf, " Samples : %d\n", psf->sf.frames) ;
14250 psf_binheader_readf (psf, "p", IRCAM_DATA_OFFSET) ;
14252 return 0 ;
14253 } /* ircam_read_header */
14255 static int
14256 ircam_close (SF_PRIVATE *psf)
14258 psf_log_printf (psf, "close\n") ;
14260 return 0 ;
14261 } /* ircam_close */
14263 static int
14264 ircam_write_header (SF_PRIVATE *psf, int calc_length)
14265 { int encoding ;
14266 float samplerate ;
14267 sf_count_t current ;
14269 if (psf->pipeoffset > 0)
14270 return 0 ;
14272 current = psf_ftell (psf) ;
14274 calc_length = calc_length ;
14276 /* This also sets psf->endian. */
14277 encoding = get_encoding (psf->sf.format & SF_FORMAT_SUBMASK) ;
14279 if (encoding == 0)
14280 return SFE_BAD_OPEN_FORMAT ;
14282 /* Reset the current header length to zero. */
14283 psf->header [0] = 0 ;
14284 psf->headindex = 0 ;
14286 if (psf->is_pipe == SF_FALSE)
14287 psf_fseek (psf, 0, SEEK_SET) ;
14289 samplerate = psf->sf.samplerate ;
14291 switch (psf->endian)
14292 { case SF_ENDIAN_BIG :
14293 psf_binheader_writef (psf, "Emf", IRCAM_02B_MARKER, samplerate) ;
14294 psf_binheader_writef (psf, "E44", psf->sf.channels, encoding) ;
14295 break ;
14297 case SF_ENDIAN_LITTLE :
14298 psf_binheader_writef (psf, "emf", IRCAM_03L_MARKER, samplerate) ;
14299 psf_binheader_writef (psf, "e44", psf->sf.channels, encoding) ;
14300 break ;
14302 default : return SFE_BAD_OPEN_FORMAT ;
14305 psf_binheader_writef (psf, "z", (size_t) (IRCAM_DATA_OFFSET - psf->headindex)) ;
14307 /* Header construction complete so write it out. */
14308 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
14310 if (psf->error)
14311 return psf->error ;
14313 if (current > 0)
14314 psf_fseek (psf, current, SEEK_SET) ;
14316 return psf->error ;
14317 } /* ircam_write_header */
14319 static int
14320 get_encoding (int subformat)
14321 { switch (subformat)
14322 { case SF_FORMAT_PCM_16 : return IRCAM_PCM_16 ;
14323 case SF_FORMAT_PCM_32 : return IRCAM_PCM_32 ;
14325 case SF_FORMAT_FLOAT : return IRCAM_FLOAT ;
14327 case SF_FORMAT_ULAW : return IRCAM_ULAW ;
14328 case SF_FORMAT_ALAW : return IRCAM_ALAW ;
14330 default : break ;
14333 return 0 ;
14334 } /* get_encoding */
14336 static const char*
14337 get_encoding_str (int encoding)
14338 { switch (encoding)
14339 { case IRCAM_PCM_16 : return "16 bit PCM" ;
14340 case IRCAM_FLOAT : return "32 bit float" ;
14341 case IRCAM_ALAW : return "A law" ;
14342 case IRCAM_ULAW : return "u law" ;
14343 case IRCAM_PCM_32 : return "32 bit PCM" ;
14345 return "Unknown encoding" ;
14346 } /* get_encoding_str */
14349 ** Do not edit or modify anything in this comment block.
14350 ** The arch-tag line is a file identity tag for the GNU Arch
14351 ** revision control system.
14353 ** arch-tag: f2714ab8-f286-4c94-9740-edaf673a1c71
14356 * Copyright 1992 by Jutta Degener and Carsten Bormann, Technische
14357 * Universitaet Berlin. See the accompanying file "COPYRIGHT" for
14358 * details. THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE.
14361 #include <stdio.h>
14362 #include <assert.h>
14367 * 4.2.11 .. 4.2.12 LONG TERM PREDICTOR (LTP) SECTION
14372 * This module computes the LTP gain (bc) and the LTP lag (Nc)
14373 * for the long term analysis filter. This is done by calculating a
14374 * maximum of the cross-correlation function between the current
14375 * sub-segment short term residual signal d[0..39] (output of
14376 * the short term analysis filter; for simplification the index
14377 * of this array begins at 0 and ends at 39 for each sub-segment of the
14378 * RPE-LTP analysis) and the previous reconstructed short term
14379 * residual signal dp[ -120 .. -1 ]. A dynamic scaling must be
14380 * performed to avoid overflow.
14383 /* The next procedure exists in six versions. First two integer
14384 * version (if USE_FLOAT_MUL is not defined); then four floating
14385 * point versions, twice with proper scaling (USE_FLOAT_MUL defined),
14386 * once without (USE_FLOAT_MUL and FAST defined, and fast run-time
14387 * option used). Every pair has first a Cut version (see the -C
14388 * option to toast or the LTP_CUT option to gsm_option()), then the
14389 * uncut one. (For a detailed explanation of why this is altogether
14390 * a bad idea, see Henry Spencer and Geoff Collyer, ``#ifdef Considered
14391 * Harmful''.)
14394 #ifndef USE_FLOAT_MUL
14396 #ifdef LTP_CUT
14398 static void Cut_Calculation_of_the_LTP_parameters (
14400 struct gsm_state * st,
14402 register word * d, /* [0..39] IN */
14403 register word * dp, /* [-120..-1] IN */
14404 word * bc_out, /* OUT */
14405 word * Nc_out /* OUT */
14408 register int k, lambda;
14409 word Nc, bc;
14410 word wt[40];
14412 longword L_result;
14413 longword L_max, L_power;
14414 word R, S, dmax, scal, best_k;
14415 word ltp_cut;
14417 register word temp, wt_k;
14419 /* Search of the optimum scaling of d[0..39].
14421 dmax = 0;
14422 for (k = 0; k <= 39; k++) {
14423 temp = d[k];
14424 temp = GSM_ABS( temp );
14425 if (temp > dmax) {
14426 dmax = temp;
14427 best_k = k;
14430 temp = 0;
14431 if (dmax == 0) scal = 0;
14432 else {
14433 assert(dmax > 0);
14434 temp = gsm_norm( (longword)dmax << 16 );
14436 if (temp > 6) scal = 0;
14437 else scal = 6 - temp;
14438 assert(scal >= 0);
14440 /* Search for the maximum cross-correlation and coding of the LTP lag
14442 L_max = 0;
14443 Nc = 40; /* index for the maximum cross-correlation */
14444 wt_k = SASR_W(d[best_k], scal);
14446 for (lambda = 40; lambda <= 120; lambda++) {
14447 L_result = (longword)wt_k * dp[best_k - lambda];
14448 if (L_result > L_max) {
14449 Nc = lambda;
14450 L_max = L_result;
14453 *Nc_out = Nc;
14454 L_max <<= 1;
14456 /* Rescaling of L_max
14458 assert(scal <= 100 && scal >= -100);
14459 L_max = L_max >> (6 - scal); /* sub(6, scal) */
14461 assert( Nc <= 120 && Nc >= 40);
14463 /* Compute the power of the reconstructed short term residual
14464 * signal dp[..]
14466 L_power = 0;
14467 for (k = 0; k <= 39; k++) {
14469 register longword L_temp;
14471 L_temp = SASR_W( dp[k - Nc], 3 );
14472 L_power += L_temp * L_temp;
14474 L_power <<= 1; /* from L_MULT */
14476 /* Normalization of L_max and L_power
14479 if (L_max <= 0) {
14480 *bc_out = 0;
14481 return;
14483 if (L_max >= L_power) {
14484 *bc_out = 3;
14485 return;
14488 temp = gsm_norm( L_power );
14490 R = SASR( L_max << temp, 16 );
14491 S = SASR( L_power << temp, 16 );
14493 /* Coding of the LTP gain
14496 /* Table 4.3a must be used to obtain the level DLB[i] for the
14497 * quantization of the LTP gain b to get the coded version bc.
14499 for (bc = 0; bc <= 2; bc++) if (R <= gsm_mult(S, gsm_DLB[bc])) break;
14500 *bc_out = bc;
14503 #endif /* LTP_CUT */
14505 static void Calculation_of_the_LTP_parameters (
14506 register word * d, /* [0..39] IN */
14507 register word * dp, /* [-120..-1] IN */
14508 word * bc_out, /* OUT */
14509 word * Nc_out /* OUT */
14512 register int k, lambda;
14513 word Nc, bc;
14514 word wt[40];
14516 longword L_max, L_power;
14517 word R, S, dmax, scal;
14518 register word temp;
14520 /* Search of the optimum scaling of d[0..39].
14522 dmax = 0;
14524 for (k = 0; k <= 39; k++) {
14525 temp = d[k];
14526 temp = GSM_ABS( temp );
14527 if (temp > dmax) dmax = temp;
14530 temp = 0;
14531 if (dmax == 0) scal = 0;
14532 else {
14533 assert(dmax > 0);
14534 temp = gsm_norm( (longword)dmax << 16 );
14537 if (temp > 6) scal = 0;
14538 else scal = 6 - temp;
14540 assert(scal >= 0);
14542 /* Initialization of a working array wt
14545 for (k = 0; k <= 39; k++) wt[k] = SASR_W( d[k], scal );
14547 /* Search for the maximum cross-correlation and coding of the LTP lag
14549 L_max = 0;
14550 Nc = 40; /* index for the maximum cross-correlation */
14552 for (lambda = 40; lambda <= 120; lambda++) {
14554 # undef long_termSTEP
14555 # define long_termSTEP(k) (longword)wt[k] * dp[k - lambda]
14557 register longword L_result;
14559 L_result = long_termSTEP(0) ; L_result += long_termSTEP(1) ;
14560 L_result += long_termSTEP(2) ; L_result += long_termSTEP(3) ;
14561 L_result += long_termSTEP(4) ; L_result += long_termSTEP(5) ;
14562 L_result += long_termSTEP(6) ; L_result += long_termSTEP(7) ;
14563 L_result += long_termSTEP(8) ; L_result += long_termSTEP(9) ;
14564 L_result += long_termSTEP(10) ; L_result += long_termSTEP(11) ;
14565 L_result += long_termSTEP(12) ; L_result += long_termSTEP(13) ;
14566 L_result += long_termSTEP(14) ; L_result += long_termSTEP(15) ;
14567 L_result += long_termSTEP(16) ; L_result += long_termSTEP(17) ;
14568 L_result += long_termSTEP(18) ; L_result += long_termSTEP(19) ;
14569 L_result += long_termSTEP(20) ; L_result += long_termSTEP(21) ;
14570 L_result += long_termSTEP(22) ; L_result += long_termSTEP(23) ;
14571 L_result += long_termSTEP(24) ; L_result += long_termSTEP(25) ;
14572 L_result += long_termSTEP(26) ; L_result += long_termSTEP(27) ;
14573 L_result += long_termSTEP(28) ; L_result += long_termSTEP(29) ;
14574 L_result += long_termSTEP(30) ; L_result += long_termSTEP(31) ;
14575 L_result += long_termSTEP(32) ; L_result += long_termSTEP(33) ;
14576 L_result += long_termSTEP(34) ; L_result += long_termSTEP(35) ;
14577 L_result += long_termSTEP(36) ; L_result += long_termSTEP(37) ;
14578 L_result += long_termSTEP(38) ; L_result += long_termSTEP(39) ;
14580 if (L_result > L_max) {
14582 Nc = lambda;
14583 L_max = L_result;
14587 *Nc_out = Nc;
14589 L_max <<= 1;
14591 /* Rescaling of L_max
14593 assert(scal <= 100 && scal >= -100);
14594 L_max = L_max >> (6 - scal); /* sub(6, scal) */
14596 assert( Nc <= 120 && Nc >= 40);
14598 /* Compute the power of the reconstructed short term residual
14599 * signal dp[..]
14601 L_power = 0;
14602 for (k = 0; k <= 39; k++) {
14604 register longword L_temp;
14606 L_temp = SASR_W( dp[k - Nc], 3 );
14607 L_power += L_temp * L_temp;
14609 L_power <<= 1; /* from L_MULT */
14611 /* Normalization of L_max and L_power
14614 if (L_max <= 0) {
14615 *bc_out = 0;
14616 return;
14618 if (L_max >= L_power) {
14619 *bc_out = 3;
14620 return;
14623 temp = gsm_norm( L_power );
14625 R = SASR_L( L_max << temp, 16 );
14626 S = SASR_L( L_power << temp, 16 );
14628 /* Coding of the LTP gain
14631 /* Table 4.3a must be used to obtain the level DLB[i] for the
14632 * quantization of the LTP gain b to get the coded version bc.
14634 for (bc = 0; bc <= 2; bc++) if (R <= gsm_mult(S, gsm_DLB[bc])) break;
14635 *bc_out = bc;
14638 #else /* USE_FLOAT_MUL */
14640 #ifdef LTP_CUT
14642 static void Cut_Calculation_of_the_LTP_parameters (
14643 struct gsm_state * st, /* IN */
14644 register word * d, /* [0..39] IN */
14645 register word * dp, /* [-120..-1] IN */
14646 word * bc_out, /* OUT */
14647 word * Nc_out /* OUT */
14650 register int k, lambda;
14651 word Nc, bc;
14652 word ltp_cut;
14654 float wt_float[40];
14655 float dp_float_base[120], * dp_float = dp_float_base + 120;
14657 longword L_max, L_power;
14658 word R, S, dmax, scal;
14659 register word temp;
14661 /* Search of the optimum scaling of d[0..39].
14663 dmax = 0;
14665 for (k = 0; k <= 39; k++) {
14666 temp = d[k];
14667 temp = GSM_ABS( temp );
14668 if (temp > dmax) dmax = temp;
14671 temp = 0;
14672 if (dmax == 0) scal = 0;
14673 else {
14674 assert(dmax > 0);
14675 temp = gsm_norm( (longword)dmax << 16 );
14678 if (temp > 6) scal = 0;
14679 else scal = 6 - temp;
14681 assert(scal >= 0);
14682 ltp_cut = (longword)SASR_W(dmax, scal) * st->ltp_cut / 100;
14685 /* Initialization of a working array wt
14688 for (k = 0; k < 40; k++) {
14689 register word w = SASR_W( d[k], scal );
14690 if (w < 0 ? w > -ltp_cut : w < ltp_cut) {
14691 wt_float[k] = 0.0;
14693 else {
14694 wt_float[k] = w;
14697 for (k = -120; k < 0; k++) dp_float[k] = dp[k];
14699 /* Search for the maximum cross-correlation and coding of the LTP lag
14701 L_max = 0;
14702 Nc = 40; /* index for the maximum cross-correlation */
14704 for (lambda = 40; lambda <= 120; lambda += 9) {
14706 /* Calculate L_result for l = lambda .. lambda + 9.
14708 register float *lp = dp_float - lambda;
14710 register float W;
14711 register float a = lp[-8], b = lp[-7], c = lp[-6],
14712 d = lp[-5], e = lp[-4], f = lp[-3],
14713 g = lp[-2], h = lp[-1];
14714 register float E;
14715 register float S0 = 0, S1 = 0, S2 = 0, S3 = 0, S4 = 0,
14716 S5 = 0, S6 = 0, S7 = 0, S8 = 0;
14718 # undef long_termSTEP
14719 # define long_termSTEP(K, a, b, c, d, e, f, g, h) \
14720 if ((W = wt_float[K]) != 0.0) { \
14721 E = W * a; S8 += E; \
14722 E = W * b; S7 += E; \
14723 E = W * c; S6 += E; \
14724 E = W * d; S5 += E; \
14725 E = W * e; S4 += E; \
14726 E = W * f; S3 += E; \
14727 E = W * g; S2 += E; \
14728 E = W * h; S1 += E; \
14729 a = lp[K]; \
14730 E = W * a; S0 += E; } else (a = lp[K])
14732 # define long_termSTEP_A(K) long_termSTEP(K, a, b, c, d, e, f, g, h)
14733 # define long_termSTEP_B(K) long_termSTEP(K, b, c, d, e, f, g, h, a)
14734 # define long_termSTEP_C(K) long_termSTEP(K, c, d, e, f, g, h, a, b)
14735 # define long_termSTEP_D(K) long_termSTEP(K, d, e, f, g, h, a, b, c)
14736 # define long_termSTEP_E(K) long_termSTEP(K, e, f, g, h, a, b, c, d)
14737 # define long_termSTEP_F(K) long_termSTEP(K, f, g, h, a, b, c, d, e)
14738 # define long_termSTEP_G(K) long_termSTEP(K, g, h, a, b, c, d, e, f)
14739 # define long_termSTEP_H(K) long_termSTEP(K, h, a, b, c, d, e, f, g)
14741 long_termSTEP_A( 0); long_termSTEP_B( 1); long_termSTEP_C( 2); long_termSTEP_D( 3);
14742 long_termSTEP_E( 4); long_termSTEP_F( 5); long_termSTEP_G( 6); long_termSTEP_H( 7);
14744 long_termSTEP_A( 8); long_termSTEP_B( 9); long_termSTEP_C(10); long_termSTEP_D(11);
14745 long_termSTEP_E(12); long_termSTEP_F(13); long_termSTEP_G(14); long_termSTEP_H(15);
14747 long_termSTEP_A(16); long_termSTEP_B(17); long_termSTEP_C(18); long_termSTEP_D(19);
14748 long_termSTEP_E(20); long_termSTEP_F(21); long_termSTEP_G(22); long_termSTEP_H(23);
14750 long_termSTEP_A(24); long_termSTEP_B(25); long_termSTEP_C(26); long_termSTEP_D(27);
14751 long_termSTEP_E(28); long_termSTEP_F(29); long_termSTEP_G(30); long_termSTEP_H(31);
14753 long_termSTEP_A(32); long_termSTEP_B(33); long_termSTEP_C(34); long_termSTEP_D(35);
14754 long_termSTEP_E(36); long_termSTEP_F(37); long_termSTEP_G(38); long_termSTEP_H(39);
14756 if (S0 > L_max) { L_max = S0; Nc = lambda; }
14757 if (S1 > L_max) { L_max = S1; Nc = lambda + 1; }
14758 if (S2 > L_max) { L_max = S2; Nc = lambda + 2; }
14759 if (S3 > L_max) { L_max = S3; Nc = lambda + 3; }
14760 if (S4 > L_max) { L_max = S4; Nc = lambda + 4; }
14761 if (S5 > L_max) { L_max = S5; Nc = lambda + 5; }
14762 if (S6 > L_max) { L_max = S6; Nc = lambda + 6; }
14763 if (S7 > L_max) { L_max = S7; Nc = lambda + 7; }
14764 if (S8 > L_max) { L_max = S8; Nc = lambda + 8; }
14767 *Nc_out = Nc;
14769 L_max <<= 1;
14771 /* Rescaling of L_max
14773 assert(scal <= 100 && scal >= -100);
14774 L_max = L_max >> (6 - scal); /* sub(6, scal) */
14776 assert( Nc <= 120 && Nc >= 40);
14778 /* Compute the power of the reconstructed short term residual
14779 * signal dp[..]
14781 L_power = 0;
14782 for (k = 0; k <= 39; k++) {
14784 register longword L_temp;
14786 L_temp = SASR_W( dp[k - Nc], 3 );
14787 L_power += L_temp * L_temp;
14789 L_power <<= 1; /* from L_MULT */
14791 /* Normalization of L_max and L_power
14794 if (L_max <= 0) {
14795 *bc_out = 0;
14796 return;
14798 if (L_max >= L_power) {
14799 *bc_out = 3;
14800 return;
14803 temp = gsm_norm( L_power );
14805 R = SASR( L_max << temp, 16 );
14806 S = SASR( L_power << temp, 16 );
14808 /* Coding of the LTP gain
14811 /* Table 4.3a must be used to obtain the level DLB[i] for the
14812 * quantization of the LTP gain b to get the coded version bc.
14814 for (bc = 0; bc <= 2; bc++) if (R <= gsm_mult(S, gsm_DLB[bc])) break;
14815 *bc_out = bc;
14818 #endif /* LTP_CUT */
14820 static void Calculation_of_the_LTP_parameters (
14821 register word * din, /* [0..39] IN */
14822 register word * dp, /* [-120..-1] IN */
14823 word * bc_out, /* OUT */
14824 word * Nc_out /* OUT */
14827 register int k, lambda;
14828 word Nc, bc;
14830 float wt_float[40];
14831 float dp_float_base[120], * dp_float = dp_float_base + 120;
14833 longword L_max, L_power;
14834 word R, S, dmax, scal;
14835 register word temp;
14837 /* Search of the optimum scaling of d[0..39].
14839 dmax = 0;
14841 for (k = 0; k <= 39; k++) {
14842 temp = din [k] ;
14843 temp = GSM_ABS (temp) ;
14844 if (temp > dmax) dmax = temp;
14847 temp = 0;
14848 if (dmax == 0) scal = 0;
14849 else {
14850 assert(dmax > 0);
14851 temp = gsm_norm( (longword)dmax << 16 );
14854 if (temp > 6) scal = 0;
14855 else scal = 6 - temp;
14857 assert(scal >= 0);
14859 /* Initialization of a working array wt
14862 for (k = 0; k < 40; k++) wt_float[k] = SASR_W (din [k], scal) ;
14863 for (k = -120; k < 0; k++) dp_float[k] = dp[k];
14865 /* Search for the maximum cross-correlation and coding of the LTP lag
14867 L_max = 0;
14868 Nc = 40; /* index for the maximum cross-correlation */
14870 for (lambda = 40; lambda <= 120; lambda += 9) {
14872 /* Calculate L_result for l = lambda .. lambda + 9.
14874 register float *lp = dp_float - lambda;
14876 register float W;
14877 register float a = lp[-8], b = lp[-7], c = lp[-6],
14878 d = lp[-5], e = lp[-4], f = lp[-3],
14879 g = lp[-2], h = lp[-1];
14880 register float E;
14881 register float S0 = 0, S1 = 0, S2 = 0, S3 = 0, S4 = 0,
14882 S5 = 0, S6 = 0, S7 = 0, S8 = 0;
14884 # undef long_termSTEP
14885 # define long_termSTEP(K, a, b, c, d, e, f, g, h) \
14886 W = wt_float[K]; \
14887 E = W * a; S8 += E; \
14888 E = W * b; S7 += E; \
14889 E = W * c; S6 += E; \
14890 E = W * d; S5 += E; \
14891 E = W * e; S4 += E; \
14892 E = W * f; S3 += E; \
14893 E = W * g; S2 += E; \
14894 E = W * h; S1 += E; \
14895 a = lp[K]; \
14896 E = W * a; S0 += E
14898 # define long_termSTEP_A(K) long_termSTEP(K, a, b, c, d, e, f, g, h)
14899 # define long_termSTEP_B(K) long_termSTEP(K, b, c, d, e, f, g, h, a)
14900 # define long_termSTEP_C(K) long_termSTEP(K, c, d, e, f, g, h, a, b)
14901 # define long_termSTEP_D(K) long_termSTEP(K, d, e, f, g, h, a, b, c)
14902 # define long_termSTEP_E(K) long_termSTEP(K, e, f, g, h, a, b, c, d)
14903 # define long_termSTEP_F(K) long_termSTEP(K, f, g, h, a, b, c, d, e)
14904 # define long_termSTEP_G(K) long_termSTEP(K, g, h, a, b, c, d, e, f)
14905 # define long_termSTEP_H(K) long_termSTEP(K, h, a, b, c, d, e, f, g)
14907 long_termSTEP_A( 0); long_termSTEP_B( 1); long_termSTEP_C( 2); long_termSTEP_D( 3);
14908 long_termSTEP_E( 4); long_termSTEP_F( 5); long_termSTEP_G( 6); long_termSTEP_H( 7);
14910 long_termSTEP_A( 8); long_termSTEP_B( 9); long_termSTEP_C(10); long_termSTEP_D(11);
14911 long_termSTEP_E(12); long_termSTEP_F(13); long_termSTEP_G(14); long_termSTEP_H(15);
14913 long_termSTEP_A(16); long_termSTEP_B(17); long_termSTEP_C(18); long_termSTEP_D(19);
14914 long_termSTEP_E(20); long_termSTEP_F(21); long_termSTEP_G(22); long_termSTEP_H(23);
14916 long_termSTEP_A(24); long_termSTEP_B(25); long_termSTEP_C(26); long_termSTEP_D(27);
14917 long_termSTEP_E(28); long_termSTEP_F(29); long_termSTEP_G(30); long_termSTEP_H(31);
14919 long_termSTEP_A(32); long_termSTEP_B(33); long_termSTEP_C(34); long_termSTEP_D(35);
14920 long_termSTEP_E(36); long_termSTEP_F(37); long_termSTEP_G(38); long_termSTEP_H(39);
14922 if (S0 > L_max) { L_max = S0; Nc = lambda; }
14923 if (S1 > L_max) { L_max = S1; Nc = lambda + 1; }
14924 if (S2 > L_max) { L_max = S2; Nc = lambda + 2; }
14925 if (S3 > L_max) { L_max = S3; Nc = lambda + 3; }
14926 if (S4 > L_max) { L_max = S4; Nc = lambda + 4; }
14927 if (S5 > L_max) { L_max = S5; Nc = lambda + 5; }
14928 if (S6 > L_max) { L_max = S6; Nc = lambda + 6; }
14929 if (S7 > L_max) { L_max = S7; Nc = lambda + 7; }
14930 if (S8 > L_max) { L_max = S8; Nc = lambda + 8; }
14932 *Nc_out = Nc;
14934 L_max <<= 1;
14936 /* Rescaling of L_max
14938 assert(scal <= 100 && scal >= -100);
14939 L_max = L_max >> (6 - scal); /* sub(6, scal) */
14941 assert( Nc <= 120 && Nc >= 40);
14943 /* Compute the power of the reconstructed short term residual
14944 * signal dp[..]
14946 L_power = 0;
14947 for (k = 0; k <= 39; k++) {
14949 register longword L_temp;
14951 L_temp = SASR_W( dp[k - Nc], 3 );
14952 L_power += L_temp * L_temp;
14954 L_power <<= 1; /* from L_MULT */
14956 /* Normalization of L_max and L_power
14959 if (L_max <= 0) {
14960 *bc_out = 0;
14961 return;
14963 if (L_max >= L_power) {
14964 *bc_out = 3;
14965 return;
14968 temp = gsm_norm( L_power );
14970 R = SASR_L ( L_max << temp, 16 );
14971 S = SASR_L ( L_power << temp, 16 );
14973 /* Coding of the LTP gain
14976 /* Table 4.3a must be used to obtain the level DLB[i] for the
14977 * quantization of the LTP gain b to get the coded version bc.
14979 for (bc = 0; bc <= 2; bc++) if (R <= gsm_mult(S, gsm_DLB[bc])) break;
14980 *bc_out = bc;
14983 #ifdef FAST
14984 #ifdef LTP_CUT
14986 static void Cut_Fast_Calculation_of_the_LTP_parameters (
14987 struct gsm_state * st, /* IN */
14988 register word * d, /* [0..39] IN */
14989 register word * dp, /* [-120..-1] IN */
14990 word * bc_out, /* OUT */
14991 word * Nc_out /* OUT */
14994 register int k, lambda;
14995 register float wt_float;
14996 word Nc, bc;
14997 word wt_max, best_k, ltp_cut;
14999 float dp_float_base[120], * dp_float = dp_float_base + 120;
15001 register float L_result, L_max, L_power;
15003 wt_max = 0;
15005 for (k = 0; k < 40; ++k) {
15006 if ( d[k] > wt_max) wt_max = d[best_k = k];
15007 else if (-d[k] > wt_max) wt_max = -d[best_k = k];
15010 assert(wt_max >= 0);
15011 wt_float = (float)wt_max;
15013 for (k = -120; k < 0; ++k) dp_float[k] = (float)dp[k];
15015 /* Search for the maximum cross-correlation and coding of the LTP lag
15017 L_max = 0;
15018 Nc = 40; /* index for the maximum cross-correlation */
15020 for (lambda = 40; lambda <= 120; lambda++) {
15021 L_result = wt_float * dp_float[best_k - lambda];
15022 if (L_result > L_max) {
15023 Nc = lambda;
15024 L_max = L_result;
15028 *Nc_out = Nc;
15029 if (L_max <= 0.) {
15030 *bc_out = 0;
15031 return;
15034 /* Compute the power of the reconstructed short term residual
15035 * signal dp[..]
15037 dp_float -= Nc;
15038 L_power = 0;
15039 for (k = 0; k < 40; ++k) {
15040 register float f = dp_float[k];
15041 L_power += f * f;
15044 if (L_max >= L_power) {
15045 *bc_out = 3;
15046 return;
15049 /* Coding of the LTP gain
15050 * Table 4.3a must be used to obtain the level DLB[i] for the
15051 * quantization of the LTP gain b to get the coded version bc.
15053 lambda = L_max / L_power * 32768.;
15054 for (bc = 0; bc <= 2; ++bc) if (lambda <= gsm_DLB[bc]) break;
15055 *bc_out = bc;
15058 #endif /* LTP_CUT */
15060 static void Fast_Calculation_of_the_LTP_parameters (
15061 register word * din, /* [0..39] IN */
15062 register word * dp, /* [-120..-1] IN */
15063 word * bc_out, /* OUT */
15064 word * Nc_out /* OUT */
15067 register int k, lambda;
15068 word Nc, bc;
15070 float wt_float[40];
15071 float dp_float_base[120], * dp_float = dp_float_base + 120;
15073 register float L_max, L_power;
15075 for (k = 0; k < 40; ++k) wt_float[k] = (float) din [k] ;
15076 for (k = -120; k < 0; ++k) dp_float[k] = (float) dp [k] ;
15078 /* Search for the maximum cross-correlation and coding of the LTP lag
15080 L_max = 0;
15081 Nc = 40; /* index for the maximum cross-correlation */
15083 for (lambda = 40; lambda <= 120; lambda += 9) {
15085 /* Calculate L_result for l = lambda .. lambda + 9.
15087 register float *lp = dp_float - lambda;
15089 register float W;
15090 register float a = lp[-8], b = lp[-7], c = lp[-6],
15091 d = lp[-5], e = lp[-4], f = lp[-3],
15092 g = lp[-2], h = lp[-1];
15093 register float E;
15094 register float S0 = 0, S1 = 0, S2 = 0, S3 = 0, S4 = 0,
15095 S5 = 0, S6 = 0, S7 = 0, S8 = 0;
15097 # undef long_termSTEP
15098 # define long_termSTEP(K, a, b, c, d, e, f, g, h) \
15099 W = wt_float[K]; \
15100 E = W * a; S8 += E; \
15101 E = W * b; S7 += E; \
15102 E = W * c; S6 += E; \
15103 E = W * d; S5 += E; \
15104 E = W * e; S4 += E; \
15105 E = W * f; S3 += E; \
15106 E = W * g; S2 += E; \
15107 E = W * h; S1 += E; \
15108 a = lp[K]; \
15109 E = W * a; S0 += E
15111 # define long_termSTEP_A(K) long_termSTEP(K, a, b, c, d, e, f, g, h)
15112 # define long_termSTEP_B(K) long_termSTEP(K, b, c, d, e, f, g, h, a)
15113 # define long_termSTEP_C(K) long_termSTEP(K, c, d, e, f, g, h, a, b)
15114 # define long_termSTEP_D(K) long_termSTEP(K, d, e, f, g, h, a, b, c)
15115 # define long_termSTEP_E(K) long_termSTEP(K, e, f, g, h, a, b, c, d)
15116 # define long_termSTEP_F(K) long_termSTEP(K, f, g, h, a, b, c, d, e)
15117 # define long_termSTEP_G(K) long_termSTEP(K, g, h, a, b, c, d, e, f)
15118 # define long_termSTEP_H(K) long_termSTEP(K, h, a, b, c, d, e, f, g)
15120 long_termSTEP_A( 0); long_termSTEP_B( 1); long_termSTEP_C( 2); long_termSTEP_D( 3);
15121 long_termSTEP_E( 4); long_termSTEP_F( 5); long_termSTEP_G( 6); long_termSTEP_H( 7);
15123 long_termSTEP_A( 8); long_termSTEP_B( 9); long_termSTEP_C(10); long_termSTEP_D(11);
15124 long_termSTEP_E(12); long_termSTEP_F(13); long_termSTEP_G(14); long_termSTEP_H(15);
15126 long_termSTEP_A(16); long_termSTEP_B(17); long_termSTEP_C(18); long_termSTEP_D(19);
15127 long_termSTEP_E(20); long_termSTEP_F(21); long_termSTEP_G(22); long_termSTEP_H(23);
15129 long_termSTEP_A(24); long_termSTEP_B(25); long_termSTEP_C(26); long_termSTEP_D(27);
15130 long_termSTEP_E(28); long_termSTEP_F(29); long_termSTEP_G(30); long_termSTEP_H(31);
15132 long_termSTEP_A(32); long_termSTEP_B(33); long_termSTEP_C(34); long_termSTEP_D(35);
15133 long_termSTEP_E(36); long_termSTEP_F(37); long_termSTEP_G(38); long_termSTEP_H(39);
15135 if (S0 > L_max) { L_max = S0; Nc = lambda; }
15136 if (S1 > L_max) { L_max = S1; Nc = lambda + 1; }
15137 if (S2 > L_max) { L_max = S2; Nc = lambda + 2; }
15138 if (S3 > L_max) { L_max = S3; Nc = lambda + 3; }
15139 if (S4 > L_max) { L_max = S4; Nc = lambda + 4; }
15140 if (S5 > L_max) { L_max = S5; Nc = lambda + 5; }
15141 if (S6 > L_max) { L_max = S6; Nc = lambda + 6; }
15142 if (S7 > L_max) { L_max = S7; Nc = lambda + 7; }
15143 if (S8 > L_max) { L_max = S8; Nc = lambda + 8; }
15145 *Nc_out = Nc;
15147 if (L_max <= 0.) {
15148 *bc_out = 0;
15149 return;
15152 /* Compute the power of the reconstructed short term residual
15153 * signal dp[..]
15155 dp_float -= Nc;
15156 L_power = 0;
15157 for (k = 0; k < 40; ++k) {
15158 register float f = dp_float[k];
15159 L_power += f * f;
15162 if (L_max >= L_power) {
15163 *bc_out = 3;
15164 return;
15167 /* Coding of the LTP gain
15168 * Table 4.3a must be used to obtain the level DLB[i] for the
15169 * quantization of the LTP gain b to get the coded version bc.
15171 lambda = L_max / L_power * 32768.;
15172 for (bc = 0; bc <= 2; ++bc) if (lambda <= gsm_DLB[bc]) break;
15173 *bc_out = bc;
15176 #endif /* FAST */
15177 #endif /* USE_FLOAT_MUL */
15180 /* 4.2.12 */
15182 static void Long_term_analysis_filtering (
15183 word bc, /* IN */
15184 word Nc, /* IN */
15185 register word * dp, /* previous d [-120..-1] IN */
15186 register word * d, /* d [0..39] IN */
15187 register word * dpp, /* estimate [0..39] OUT */
15188 register word * e /* long term res. signal [0..39] OUT */
15191 * In this part, we have to decode the bc parameter to compute
15192 * the samples of the estimate dpp[0..39]. The decoding of bc needs the
15193 * use of table 4.3b. The long term residual signal e[0..39]
15194 * is then calculated to be fed to the RPE encoding section.
15197 register int k;
15199 # undef long_termSTEP
15200 # define long_termSTEP(BP) \
15201 for (k = 0; k <= 39; k++) { \
15202 dpp[k] = GSM_MULT_R( BP, dp[k - Nc]); \
15203 e[k] = GSM_SUB( d[k], dpp[k] ); \
15206 switch (bc) {
15207 case 0: long_termSTEP( 3277 ); break;
15208 case 1: long_termSTEP( 11469 ); break;
15209 case 2: long_termSTEP( 21299 ); break;
15210 case 3: long_termSTEP( 32767 ); break;
15214 void Gsm_Long_Term_Predictor ( /* 4x for 160 samples */
15216 struct gsm_state * S,
15218 word * d, /* [0..39] residual signal IN */
15219 word * dp, /* [-120..-1] d' IN */
15221 word * e, /* [0..39] OUT */
15222 word * dpp, /* [0..39] OUT */
15223 word * Nc, /* correlation lag OUT */
15224 word * bc /* gain factor OUT */
15227 assert( d ); assert( dp ); assert( e );
15228 assert( dpp); assert( Nc ); assert( bc );
15230 #if defined(FAST) && defined(USE_FLOAT_MUL)
15231 if (S->fast)
15232 #if defined (LTP_CUT)
15233 if (S->ltp_cut)
15234 Cut_Fast_Calculation_of_the_LTP_parameters(S,
15235 d, dp, bc, Nc);
15236 else
15237 #endif /* LTP_CUT */
15238 Fast_Calculation_of_the_LTP_parameters(d, dp, bc, Nc );
15239 else
15240 #endif /* FAST & USE_FLOAT_MUL */
15241 #ifdef LTP_CUT
15242 if (S->ltp_cut)
15243 Cut_Calculation_of_the_LTP_parameters(S, d, dp, bc, Nc);
15244 else
15245 #endif
15246 Calculation_of_the_LTP_parameters(d, dp, bc, Nc);
15248 Long_term_analysis_filtering( *bc, *Nc, dp, d, dpp, e );
15251 /* 4.3.2 */
15252 void Gsm_Long_Term_Synthesis_Filtering (
15253 struct gsm_state * S,
15255 word Ncr,
15256 word bcr,
15257 register word * erp, /* [0..39] IN */
15258 register word * drp /* [-120..-1] IN, [-120..40] OUT */
15261 * This procedure uses the bcr and Ncr parameter to realize the
15262 * long term synthesis filtering. The decoding of bcr needs
15263 * table 4.3b.
15266 register int k;
15267 word brp, drpp, Nr;
15269 /* Check the limits of Nr.
15271 Nr = Ncr < 40 || Ncr > 120 ? S->nrp : Ncr;
15272 S->nrp = Nr;
15273 assert(Nr >= 40 && Nr <= 120);
15275 /* Decoding of the LTP gain bcr
15277 brp = gsm_QLB[ bcr ];
15279 /* Computation of the reconstructed short term residual
15280 * signal drp[0..39]
15282 assert(brp != MIN_WORD);
15284 for (k = 0; k <= 39; k++) {
15285 drpp = GSM_MULT_R( brp, drp[ k - Nr ] );
15286 drp[k] = GSM_ADD( erp[k], drpp );
15290 * Update of the reconstructed short term residual signal
15291 * drp[ -1..-120 ]
15294 for (k = 0; k <= 119; k++) drp[ -120 + k ] = drp[ -80 + k ];
15297 ** Do not edit or modify anything in this comment block.
15298 ** The arch-tag line is a file identity tag for the GNU Arch
15299 ** revision control system.
15301 ** arch-tag: b369b90d-0284-42a0-87b0-99a25bbd93ac
15305 * Copyright 1992 by Jutta Degener and Carsten Bormann, Technische
15306 * Universitaet Berlin. See the accompanying file "COPYRIGHT" for
15307 * details. THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE.
15310 #include <stdio.h>
15311 #include <assert.h>
15316 * 4.2.4 .. 4.2.7 LPC ANALYSIS SECTION
15319 /* 4.2.4 */
15322 static void Autocorrelation (
15323 word * s, /* [0..159] IN/OUT */
15324 longword * L_ACF) /* [0..8] OUT */
15326 * The goal is to compute the array L_ACF[k]. The signal s[i] must
15327 * be scaled in order to avoid an overflow situation.
15330 register int k, i;
15332 word temp, smax, scalauto;
15334 #ifdef USE_FLOAT_MUL
15335 float float_s[160];
15336 #endif
15338 /* Dynamic scaling of the array s[0..159]
15341 /* Search for the maximum.
15343 smax = 0;
15344 for (k = 0; k <= 159; k++) {
15345 temp = GSM_ABS( s[k] );
15346 if (temp > smax) smax = temp;
15349 /* Computation of the scaling factor.
15351 if (smax == 0) scalauto = 0;
15352 else {
15353 assert(smax > 0);
15354 scalauto = 4 - gsm_norm( (longword)smax << 16 );/* sub(4,..) */
15357 /* Scaling of the array s[0...159]
15360 if (scalauto > 0) {
15362 # ifdef USE_FLOAT_MUL
15363 # define SCALE(n) \
15364 case n: for (k = 0; k <= 159; k++) \
15365 float_s[k] = (float) \
15366 (s[k] = GSM_MULT_R(s[k], 16384 >> (n-1)));\
15367 break;
15368 # else
15369 # define SCALE(n) \
15370 case n: for (k = 0; k <= 159; k++) \
15371 s[k] = GSM_MULT_R( s[k], 16384 >> (n-1) );\
15372 break;
15373 # endif /* USE_FLOAT_MUL */
15375 switch (scalauto) {
15376 SCALE(1)
15377 SCALE(2)
15378 SCALE(3)
15379 SCALE(4)
15381 # undef SCALE
15383 # ifdef USE_FLOAT_MUL
15384 else for (k = 0; k <= 159; k++) float_s[k] = (float) s[k];
15385 # endif
15387 /* Compute the L_ACF[..].
15390 # ifdef USE_FLOAT_MUL
15391 register float * sp = float_s;
15392 register float sl = *sp;
15394 # define lpcSTEP(k) L_ACF[k] += (longword)(sl * sp[ -(k) ]);
15395 # else
15396 word * sp = s;
15397 word sl = *sp;
15399 # define lpcSTEP(k) L_ACF[k] += ((longword)sl * sp[ -(k) ]);
15400 # endif
15402 # define NEXTI sl = *++sp
15405 for (k = 9; k--; L_ACF[k] = 0) ;
15407 lpcSTEP (0);
15408 NEXTI;
15409 lpcSTEP(0); lpcSTEP(1);
15410 NEXTI;
15411 lpcSTEP(0); lpcSTEP(1); lpcSTEP(2);
15412 NEXTI;
15413 lpcSTEP(0); lpcSTEP(1); lpcSTEP(2); lpcSTEP(3);
15414 NEXTI;
15415 lpcSTEP(0); lpcSTEP(1); lpcSTEP(2); lpcSTEP(3); lpcSTEP(4);
15416 NEXTI;
15417 lpcSTEP(0); lpcSTEP(1); lpcSTEP(2); lpcSTEP(3); lpcSTEP(4); lpcSTEP(5);
15418 NEXTI;
15419 lpcSTEP(0); lpcSTEP(1); lpcSTEP(2); lpcSTEP(3); lpcSTEP(4); lpcSTEP(5); lpcSTEP(6);
15420 NEXTI;
15421 lpcSTEP(0); lpcSTEP(1); lpcSTEP(2); lpcSTEP(3); lpcSTEP(4); lpcSTEP(5); lpcSTEP(6); lpcSTEP(7);
15423 for (i = 8; i <= 159; i++) {
15425 NEXTI;
15427 lpcSTEP(0);
15428 lpcSTEP(1); lpcSTEP(2); lpcSTEP(3); lpcSTEP(4);
15429 lpcSTEP(5); lpcSTEP(6); lpcSTEP(7); lpcSTEP(8);
15432 for (k = 9; k--; L_ACF[k] <<= 1) ;
15435 /* Rescaling of the array s[0..159]
15437 if (scalauto > 0) {
15438 assert(scalauto <= 4);
15439 for (k = 160; k--; *s++ <<= scalauto) ;
15443 #if defined(USE_FLOAT_MUL) && defined(FAST)
15445 static void Fast_Autocorrelation (
15446 word * s, /* [0..159] IN/OUT */
15447 longword * L_ACF) /* [0..8] OUT */
15449 register int k, i;
15450 float f_L_ACF[9];
15451 float scale;
15453 float s_f[160];
15454 register float *sf = s_f;
15456 for (i = 0; i < 160; ++i) sf[i] = s[i];
15457 for (k = 0; k <= 8; k++) {
15458 register float L_temp2 = 0;
15459 register float *sfl = sf - k;
15460 for (i = k; i < 160; ++i) L_temp2 += sf[i] * sfl[i];
15461 f_L_ACF[k] = L_temp2;
15463 scale = MAX_LONGWORD / f_L_ACF[0];
15465 for (k = 0; k <= 8; k++) {
15466 L_ACF[k] = f_L_ACF[k] * scale;
15469 #endif /* defined (USE_FLOAT_MUL) && defined (FAST) */
15471 /* 4.2.5 */
15473 static void Reflection_coefficients (
15474 longword * L_ACF, /* 0...8 IN */
15475 register word * r /* 0...7 OUT */
15478 register int i, m, n;
15479 register word temp;
15480 word ACF[9]; /* 0..8 */
15481 word P[ 9]; /* 0..8 */
15482 word K[ 9]; /* 2..8 */
15484 /* Schur recursion with 16 bits arithmetic.
15487 if (L_ACF[0] == 0) {
15488 for (i = 8; i--; *r++ = 0) ;
15489 return;
15492 assert( L_ACF[0] != 0 );
15493 temp = gsm_norm( L_ACF[0] );
15495 assert(temp >= 0 && temp < 32);
15497 /* ? overflow ? */
15498 for (i = 0; i <= 8; i++) ACF[i] = SASR_L( L_ACF[i] << temp, 16 );
15500 /* Initialize array P[..] and K[..] for the recursion.
15503 for (i = 1; i <= 7; i++) K[ i ] = ACF[ i ];
15504 for (i = 0; i <= 8; i++) P[ i ] = ACF[ i ];
15506 /* Compute reflection coefficients
15508 for (n = 1; n <= 8; n++, r++) {
15510 temp = P[1];
15511 temp = GSM_ABS(temp);
15512 if (P[0] < temp) {
15513 for (i = n; i <= 8; i++) *r++ = 0;
15514 return;
15517 *r = gsm_div( temp, P[0] );
15519 assert(*r >= 0);
15520 if (P[1] > 0) *r = -*r; /* r[n] = sub(0, r[n]) */
15521 assert (*r != MIN_WORD);
15522 if (n == 8) return;
15524 /* Schur recursion
15526 temp = GSM_MULT_R( P[1], *r );
15527 P[0] = GSM_ADD( P[0], temp );
15529 for (m = 1; m <= 8 - n; m++) {
15530 temp = GSM_MULT_R( K[ m ], *r );
15531 P[m] = GSM_ADD( P[ m+1 ], temp );
15533 temp = GSM_MULT_R( P[ m+1 ], *r );
15534 K[m] = GSM_ADD( K[ m ], temp );
15539 /* 4.2.6 */
15541 static void Transformation_to_Log_Area_Ratios (
15542 register word * r /* 0..7 IN/OUT */
15545 * The following scaling for r[..] and LAR[..] has been used:
15547 * r[..] = integer( real_r[..]*32768. ); -1 <= real_r < 1.
15548 * LAR[..] = integer( real_LAR[..] * 16384 );
15549 * with -1.625 <= real_LAR <= 1.625
15552 register word temp;
15553 register int i;
15556 /* Computation of the LAR[0..7] from the r[0..7]
15558 for (i = 1; i <= 8; i++, r++) {
15560 temp = *r;
15561 temp = GSM_ABS(temp);
15562 assert(temp >= 0);
15564 if (temp < 22118) {
15565 temp >>= 1;
15566 } else if (temp < 31130) {
15567 assert( temp >= 11059 );
15568 temp -= 11059;
15569 } else {
15570 assert( temp >= 26112 );
15571 temp -= 26112;
15572 temp <<= 2;
15575 *r = *r < 0 ? -temp : temp;
15576 assert( *r != MIN_WORD );
15580 /* 4.2.7 */
15582 static void Quantization_and_coding (
15583 register word * LAR /* [0..7] IN/OUT */
15586 register word temp;
15588 /* This procedure needs four tables; the following equations
15589 * give the optimum scaling for the constants:
15591 * A[0..7] = integer( real_A[0..7] * 1024 )
15592 * B[0..7] = integer( real_B[0..7] * 512 )
15593 * MAC[0..7] = maximum of the LARc[0..7]
15594 * MIC[0..7] = minimum of the LARc[0..7]
15597 # undef lpcSTEP
15598 # define lpcSTEP( A, B, MAC, MIC ) \
15599 temp = GSM_MULT( A, *LAR ); \
15600 temp = GSM_ADD( temp, B ); \
15601 temp = GSM_ADD( temp, 256 ); \
15602 temp = SASR_W( temp, 9 ); \
15603 *LAR = temp>MAC ? MAC - MIC : (temp<MIC ? 0 : temp - MIC); \
15604 LAR++;
15606 lpcSTEP( 20480, 0, 31, -32 );
15607 lpcSTEP( 20480, 0, 31, -32 );
15608 lpcSTEP( 20480, 2048, 15, -16 );
15609 lpcSTEP( 20480, -2560, 15, -16 );
15611 lpcSTEP( 13964, 94, 7, -8 );
15612 lpcSTEP( 15360, -1792, 7, -8 );
15613 lpcSTEP( 8534, -341, 3, -4 );
15614 lpcSTEP( 9036, -1144, 3, -4 );
15616 # undef lpcSTEP
15619 void Gsm_LPC_Analysis (
15620 struct gsm_state *S,
15621 word * s, /* 0..159 signals IN/OUT */
15622 word * LARc) /* 0..7 LARc's OUT */
15624 longword L_ACF[9];
15626 #if defined(USE_FLOAT_MUL) && defined(FAST)
15627 if (S->fast) Fast_Autocorrelation (s, L_ACF );
15628 else
15629 #endif
15630 Autocorrelation (s, L_ACF );
15631 Reflection_coefficients (L_ACF, LARc );
15632 Transformation_to_Log_Area_Ratios (LARc);
15633 Quantization_and_coding (LARc);
15636 ** Do not edit or modify anything in this comment block.
15637 ** The arch-tag line is a file identity tag for the GNU Arch
15638 ** revision control system.
15640 ** arch-tag: 63146664-a002-4e1e-8b7b-f0cc8a6a53da
15644 ** Copyright (C) 2003,2004 Erik de Castro Lopo <erikd@mega-nerd.com>
15646 ** This program is free software; you can redistribute it and/or modify
15647 ** it under the terms of the GNU Lesser General Public License as published by
15648 ** the Free Software Foundation; either version 2.1 of the License, or
15649 ** (at your option) any later version.
15651 ** This program is distributed in the hope that it will be useful,
15652 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15653 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15654 ** GNU Lesser General Public License for more details.
15656 ** You should have received a copy of the GNU Lesser General Public License
15657 ** along with this program; if not, write to the Free Software
15658 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15662 #include <stdlib.h>
15663 #include <string.h>
15666 #if (OS_IS_MACOSX == 1)
15670 macbinary3_open (SF_PRIVATE *psf)
15672 if (psf)
15673 return 0 ;
15675 return 0 ;
15676 } /* macbinary3_open */
15678 #else
15681 macbinary3_open (SF_PRIVATE *psf)
15683 psf = psf ;
15684 return 0 ;
15685 } /* macbinary3_open */
15687 #endif /* OS_IS_MACOSX */
15690 ** Do not edit or modify anything in this comment block.
15691 ** The arch-tag line is a file identity tag for the GNU Arch
15692 ** revision control system.
15694 ** arch-tag: c397a7d7-1a31-4349-9684-bd29ef06211e
15697 ** Copyright (C) 2003,2004 Erik de Castro Lopo <erikd@mega-nerd.com>
15699 ** This program is free software; you can redistribute it and/or modify
15700 ** it under the terms of the GNU Lesser General Public License as published by
15701 ** the Free Software Foundation; either version 2.1 of the License, or
15702 ** (at your option) any later version.
15704 ** This program is distributed in the hope that it will be useful,
15705 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15706 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15707 ** GNU Lesser General Public License for more details.
15709 ** You should have received a copy of the GNU Lesser General Public License
15710 ** along with this program; if not, write to the Free Software
15711 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15715 #include <stdlib.h>
15716 #include <string.h>
15717 #include <sys/stat.h>
15720 #define STR_MARKER MAKE_MARKER ('S', 'T', 'R', ' ')
15723 macos_guess_file_type (SF_PRIVATE *psf, const char *filename)
15724 { static char rsrc_name [1024] ;
15725 struct stat statbuf ;
15726 int format ;
15728 psf = psf ;
15730 snprintf (rsrc_name, sizeof (rsrc_name), "%s/rsrc", filename);
15732 /* If there is no resource fork, just return. */
15733 if (stat (rsrc_name, &statbuf) != 0)
15734 { psf_log_printf (psf, "No resource fork.\n") ;
15735 return 0 ;
15738 if (statbuf.st_size == 0)
15739 { psf_log_printf (psf, "Have zero size resource fork.\n") ;
15740 return 0 ;
15743 format = 0 ;
15745 return format ;
15746 } /* macos_guess_file_type */
15749 ** Do not edit or modify anything in this comment block.
15750 ** The arch-tag line is a file identity tag for the GNU Arch
15751 ** revision control system.
15753 ** arch-tag: 5fbf66d7-9547-442a-9c73-92fd164f3a95
15756 ** Copyright (C) 2002-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
15758 ** This program is free software; you can redistribute it and/or modify
15759 ** it under the terms of the GNU Lesser General Public License as published by
15760 ** the Free Software Foundation; either version 2.1 of the License, or
15761 ** (at your option) any later version.
15763 ** This program is distributed in the hope that it will be useful,
15764 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15765 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15766 ** GNU Lesser General Public License for more details.
15768 ** You should have received a copy of the GNU Lesser General Public License
15769 ** along with this program; if not, write to the Free Software
15770 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15773 #include <stdio.h>
15774 #include <fcntl.h>
15775 #include <string.h>
15776 #include <ctype.h>
15779 /*------------------------------------------------------------------------------
15780 ** Information on how to decode and encode this file was obtained in a PDF
15781 ** file which I found on http://www.wotsit.org/.
15782 ** Also did a lot of testing with GNU Octave but do not have access to
15783 ** Matlab (tm) and so could not test it there.
15786 /*------------------------------------------------------------------------------
15787 ** Macros to handle big/little endian issues.
15790 #define MAT4_BE_DOUBLE (MAKE_MARKER (0, 0, 0x03, 0xE8))
15791 #define MAT4_LE_DOUBLE (MAKE_MARKER (0, 0, 0, 0))
15793 #define MAT4_BE_FLOAT (MAKE_MARKER (0, 0, 0x03, 0xF2))
15794 #define MAT4_LE_FLOAT (MAKE_MARKER (0x0A, 0, 0, 0))
15796 #define MAT4_BE_PCM_32 (MAKE_MARKER (0, 0, 0x03, 0xFC))
15797 #define MAT4_LE_PCM_32 (MAKE_MARKER (0x14, 0, 0, 0))
15799 #define MAT4_BE_PCM_16 (MAKE_MARKER (0, 0, 0x04, 0x06))
15800 #define MAT4_LE_PCM_16 (MAKE_MARKER (0x1E, 0, 0, 0))
15802 /* Can't see any reason to ever implement this. */
15803 #define MAT4_BE_PCM_U8 (MAKE_MARKER (0, 0, 0x04, 0x1A))
15804 #define MAT4_LE_PCM_U8 (MAKE_MARKER (0x32, 0, 0, 0))
15806 /*------------------------------------------------------------------------------
15807 ** Private static functions.
15810 static int mat4_close (SF_PRIVATE *psf) ;
15812 static int mat4_format_to_encoding (int format, int endian) ;
15814 static int mat4_write_header (SF_PRIVATE *psf, int calc_length) ;
15815 static int mat4_read_header (SF_PRIVATE *psf) ;
15817 static const char * mat4_marker_to_str (int marker) ;
15819 /*------------------------------------------------------------------------------
15820 ** Public function.
15824 mat4_open (SF_PRIVATE *psf)
15825 { int subformat, error = 0 ;
15827 if (psf->mode == SFM_READ || (psf->mode == SFM_RDWR && psf->filelength > 0))
15828 { if ((error = mat4_read_header (psf)))
15829 return error ;
15832 if ((psf->sf.format & SF_FORMAT_TYPEMASK) != SF_FORMAT_MAT4)
15833 return SFE_BAD_OPEN_FORMAT ;
15835 subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
15837 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
15838 { if (psf->is_pipe)
15839 return SFE_NO_PIPE_WRITE ;
15841 psf->endian = psf->sf.format & SF_FORMAT_ENDMASK ;
15842 if (CPU_IS_LITTLE_ENDIAN && (psf->endian == SF_ENDIAN_CPU || psf->endian == 0))
15843 psf->endian = SF_ENDIAN_LITTLE ;
15844 else if (CPU_IS_BIG_ENDIAN && (psf->endian == SF_ENDIAN_CPU || psf->endian == 0))
15845 psf->endian = SF_ENDIAN_BIG ;
15847 if ((error = mat4_write_header (psf, SF_FALSE)))
15848 return error ;
15850 psf->write_header = mat4_write_header ;
15853 psf->close = mat4_close ;
15855 psf->blockwidth = psf->bytewidth * psf->sf.channels ;
15857 switch (subformat)
15858 { case SF_FORMAT_PCM_16 :
15859 case SF_FORMAT_PCM_32 :
15860 error = pcm_init (psf) ;
15861 break ;
15863 case SF_FORMAT_FLOAT :
15864 error = float32_init (psf) ;
15865 break ;
15867 case SF_FORMAT_DOUBLE :
15868 error = double64_init (psf) ;
15869 break ;
15871 default : break ;
15874 if (error)
15875 return error ;
15877 return error ;
15878 } /* mat4_open */
15880 /*------------------------------------------------------------------------------
15883 static int
15884 mat4_close (SF_PRIVATE *psf)
15886 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
15887 mat4_write_header (psf, SF_TRUE) ;
15889 return 0 ;
15890 } /* mat4_close */
15892 /*------------------------------------------------------------------------------
15895 static int
15896 mat4_write_header (SF_PRIVATE *psf, int calc_length)
15897 { sf_count_t current ;
15898 int encoding ;
15899 double samplerate ;
15901 current = psf_ftell (psf) ;
15903 if (calc_length)
15904 { psf->filelength = psf_get_filelen (psf) ;
15906 psf->datalength = psf->filelength - psf->dataoffset ;
15907 if (psf->dataend)
15908 psf->datalength -= psf->filelength - psf->dataend ;
15910 psf->sf.frames = psf->datalength / (psf->bytewidth * psf->sf.channels) ;
15913 encoding = mat4_format_to_encoding (psf->sf.format & SF_FORMAT_SUBMASK, psf->endian) ;
15915 if (encoding == -1)
15916 return SFE_BAD_OPEN_FORMAT ;
15918 /* Reset the current header length to zero. */
15919 psf->header [0] = 0 ;
15920 psf->headindex = 0 ;
15921 psf_fseek (psf, 0, SEEK_SET) ;
15923 /* Need sample rate as a double for writing to the header. */
15924 samplerate = psf->sf.samplerate ;
15926 if (psf->endian == SF_ENDIAN_BIG)
15927 { psf_binheader_writef (psf, "Em444", MAT4_BE_DOUBLE, 1, 1, 0) ;
15928 psf_binheader_writef (psf, "E4bd", 11, "samplerate", 11, samplerate) ;
15929 psf_binheader_writef (psf, "tEm484", encoding, psf->sf.channels, psf->sf.frames, 0) ;
15930 psf_binheader_writef (psf, "E4b", 9, "wavedata", 9) ;
15932 else if (psf->endian == SF_ENDIAN_LITTLE)
15933 { psf_binheader_writef (psf, "em444", MAT4_LE_DOUBLE, 1, 1, 0) ;
15934 psf_binheader_writef (psf, "e4bd", 11, "samplerate", 11, samplerate) ;
15935 psf_binheader_writef (psf, "tem484", encoding, psf->sf.channels, psf->sf.frames, 0) ;
15936 psf_binheader_writef (psf, "e4b", 9, "wavedata", 9) ;
15938 else
15939 return SFE_BAD_OPEN_FORMAT ;
15941 /* Header construction complete so write it out. */
15942 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
15944 if (psf->error)
15945 return psf->error ;
15947 psf->dataoffset = psf->headindex ;
15949 if (current > 0)
15950 psf_fseek (psf, current, SEEK_SET) ;
15952 return psf->error ;
15953 } /* mat4_write_header */
15955 static int
15956 mat4_read_header (SF_PRIVATE *psf)
15957 { int marker, namesize, rows, cols, imag ;
15958 double value ;
15959 const char *marker_str ;
15960 char name [64] ;
15962 psf_binheader_readf (psf, "pm", 0, &marker) ;
15964 /* MAT4 file must start with a double for the samplerate. */
15965 if (marker == MAT4_BE_DOUBLE)
15966 { psf->endian = psf->rwf_endian = SF_ENDIAN_BIG ;
15967 marker_str = "big endian double" ;
15969 else if (marker == MAT4_LE_DOUBLE)
15970 { psf->endian = psf->rwf_endian = SF_ENDIAN_LITTLE ;
15971 marker_str = "little endian double" ;
15973 else
15974 return SFE_UNIMPLEMENTED ;
15976 psf_log_printf (psf, "GNU Octave 2.0 / MATLAB v4.2 format\nMarker : %s\n", marker_str) ;
15978 psf_binheader_readf (psf, "444", &rows, &cols, &imag) ;
15980 psf_log_printf (psf, " Rows : %d\n Cols : %d\n Imag : %s\n", rows, cols, imag ? "True" : "False") ;
15982 psf_binheader_readf (psf, "4", &namesize) ;
15984 if (namesize >= SIGNED_SIZEOF (name))
15985 return SFE_MAT4_BAD_NAME ;
15987 psf_binheader_readf (psf, "b", name, namesize) ;
15988 name [namesize] = 0 ;
15990 psf_log_printf (psf, " Name : %s\n", name) ;
15992 psf_binheader_readf (psf, "d", &value) ;
15994 LSF_SNPRINTF ((char*) psf->buffer, sizeof (psf->buffer), " Value : %f\n", value) ;
15995 psf_log_printf (psf, (char*) psf->buffer) ;
15997 if ((rows != 1) || (cols != 1))
15998 return SFE_MAT4_NO_SAMPLERATE ;
16000 psf->sf.samplerate = (int) value ;
16002 /* Now write out the audio data. */
16004 psf_binheader_readf (psf, "m", &marker) ;
16006 psf_log_printf (psf, "Marker : %s\n", mat4_marker_to_str (marker)) ;
16008 psf_binheader_readf (psf, "444", &rows, &cols, &imag) ;
16010 psf_log_printf (psf, " Rows : %d\n Cols : %d\n Imag : %s\n", rows, cols, imag ? "True" : "False") ;
16012 psf_binheader_readf (psf, "4", &namesize) ;
16014 if (namesize >= SIGNED_SIZEOF (name))
16015 return SFE_MAT4_BAD_NAME ;
16017 psf_binheader_readf (psf, "b", name, namesize) ;
16018 name [namesize] = 0 ;
16020 psf_log_printf (psf, " Name : %s\n", name) ;
16022 psf->dataoffset = psf_ftell (psf) ;
16024 if (rows == 0 && cols == 0)
16025 { psf_log_printf (psf, "*** Error : zero channel count.\n") ;
16026 return SFE_MAT4_ZERO_CHANNELS ;
16029 psf->sf.channels = rows ;
16030 psf->sf.frames = cols ;
16032 psf->sf.format = psf->endian | SF_FORMAT_MAT4 ;
16033 switch (marker)
16034 { case MAT4_BE_DOUBLE :
16035 case MAT4_LE_DOUBLE :
16036 psf->sf.format |= SF_FORMAT_DOUBLE ;
16037 psf->bytewidth = 8 ;
16038 break ;
16040 case MAT4_BE_FLOAT :
16041 case MAT4_LE_FLOAT :
16042 psf->sf.format |= SF_FORMAT_FLOAT ;
16043 psf->bytewidth = 4 ;
16044 break ;
16046 case MAT4_BE_PCM_32 :
16047 case MAT4_LE_PCM_32 :
16048 psf->sf.format |= SF_FORMAT_PCM_32 ;
16049 psf->bytewidth = 4 ;
16050 break ;
16052 case MAT4_BE_PCM_16 :
16053 case MAT4_LE_PCM_16 :
16054 psf->sf.format |= SF_FORMAT_PCM_16 ;
16055 psf->bytewidth = 2 ;
16056 break ;
16058 default :
16059 psf_log_printf (psf, "*** Error : Bad marker %08X\n", marker) ;
16060 return SFE_UNIMPLEMENTED ;
16063 if ((psf->filelength - psf->dataoffset) < psf->sf.channels * psf->sf.frames * psf->bytewidth)
16064 { psf_log_printf (psf, "*** File seems to be truncated. %D <--> %D\n",
16065 psf->filelength - psf->dataoffset, psf->sf.channels * psf->sf.frames * psf->bytewidth) ;
16067 else if ((psf->filelength - psf->dataoffset) > psf->sf.channels * psf->sf.frames * psf->bytewidth)
16068 psf->dataend = psf->dataoffset + rows * cols * psf->bytewidth ;
16070 psf->datalength = psf->filelength - psf->dataoffset - psf->dataend ;
16072 psf->sf.sections = 1 ;
16074 return 0 ;
16075 } /* mat4_read_header */
16077 static int
16078 mat4_format_to_encoding (int format, int endian)
16080 switch (format | endian)
16081 { case (SF_FORMAT_PCM_16 | SF_ENDIAN_BIG) :
16082 return MAT4_BE_PCM_16 ;
16084 case (SF_FORMAT_PCM_16 | SF_ENDIAN_LITTLE) :
16085 return MAT4_LE_PCM_16 ;
16087 case (SF_FORMAT_PCM_32 | SF_ENDIAN_BIG) :
16088 return MAT4_BE_PCM_32 ;
16090 case (SF_FORMAT_PCM_32 | SF_ENDIAN_LITTLE) :
16091 return MAT4_LE_PCM_32 ;
16093 case (SF_FORMAT_FLOAT | SF_ENDIAN_BIG) :
16094 return MAT4_BE_FLOAT ;
16096 case (SF_FORMAT_FLOAT | SF_ENDIAN_LITTLE) :
16097 return MAT4_LE_FLOAT ;
16099 case (SF_FORMAT_DOUBLE | SF_ENDIAN_BIG) :
16100 return MAT4_BE_DOUBLE ;
16102 case (SF_FORMAT_DOUBLE | SF_ENDIAN_LITTLE) :
16103 return MAT4_LE_DOUBLE ;
16105 default : break ;
16108 return -1 ;
16109 } /* mat4_format_to_encoding */
16111 static const char *
16112 mat4_marker_to_str (int marker)
16113 { static char str [32] ;
16115 switch (marker)
16117 case MAT4_BE_PCM_16 : return "big endian 16 bit PCM" ;
16118 case MAT4_LE_PCM_16 : return "little endian 16 bit PCM" ;
16120 case MAT4_BE_PCM_32 : return "big endian 32 bit PCM" ;
16121 case MAT4_LE_PCM_32 : return "little endian 32 bit PCM" ;
16124 case MAT4_BE_FLOAT : return "big endian float" ;
16125 case MAT4_LE_FLOAT : return "big endian float" ;
16127 case MAT4_BE_DOUBLE : return "big endian double" ;
16128 case MAT4_LE_DOUBLE : return "little endian double" ;
16131 /* This is a little unsafe but is really only for debugging. */
16132 str [sizeof (str) - 1] = 0 ;
16133 LSF_SNPRINTF (str, sizeof (str) - 1, "%08X", marker) ;
16134 return str ;
16135 } /* mat4_marker_to_str */
16137 ** Do not edit or modify anything in this comment block.
16138 ** The arch-tag line is a file identity tag for the GNU Arch
16139 ** revision control system.
16141 ** arch-tag: f7e5f5d6-fc39-452e-bc4a-59627116ff59
16144 ** Copyright (C) 2002-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
16146 ** This program is free software; you can redistribute it and/or modify
16147 ** it under the terms of the GNU Lesser General Public License as published by
16148 ** the Free Software Foundation; either version 2.1 of the License, or
16149 ** (at your option) any later version.
16151 ** This program is distributed in the hope that it will be useful,
16152 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16153 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16154 ** GNU Lesser General Public License for more details.
16156 ** You should have received a copy of the GNU Lesser General Public License
16157 ** along with this program; if not, write to the Free Software
16158 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16161 #include <stdio.h>
16162 #include <fcntl.h>
16163 #include <string.h>
16164 #include <ctype.h>
16167 /*------------------------------------------------------------------------------
16168 ** Information on how to decode and encode this file was obtained in a PDF
16169 ** file which I found on http://www.wotsit.org/.
16170 ** Also did a lot of testing with GNU Octave but do not have access to
16171 ** Matlab (tm) and so could not test it there.
16174 /*------------------------------------------------------------------------------
16175 ** Macros to handle big/little endian issues.
16178 #define MATL_MARKER (MAKE_MARKER ('M', 'A', 'T', 'L'))
16180 #define IM_MARKER (('I' << 8) + 'M')
16181 #define MI_MARKER (('M' << 8) + 'I')
16183 /*------------------------------------------------------------------------------
16184 ** Enums and typedefs.
16187 enum
16188 { MAT5_TYPE_SCHAR = 0x1,
16189 MAT5_TYPE_UCHAR = 0x2,
16190 MAT5_TYPE_INT16 = 0x3,
16191 MAT5_TYPE_UINT16 = 0x4,
16192 MAT5_TYPE_INT32 = 0x5,
16193 MAT5_TYPE_UINT32 = 0x6,
16194 MAT5_TYPE_FLOAT = 0x7,
16195 MAT5_TYPE_DOUBLE = 0x9,
16196 MAT5_TYPE_ARRAY = 0xE,
16198 MAT5_TYPE_COMP_USHORT = 0x00020004,
16199 MAT5_TYPE_COMP_UINT = 0x00040006
16202 typedef struct
16203 { sf_count_t size ;
16204 int rows, cols ;
16205 char name [32] ;
16206 } MAT5_MATRIX ;
16208 /*------------------------------------------------------------------------------
16209 ** Private static functions.
16212 static int mat5_close (SF_PRIVATE *psf) ;
16214 static int mat5_write_header (SF_PRIVATE *psf, int calc_length) ;
16215 static int mat5_read_header (SF_PRIVATE *psf) ;
16217 /*------------------------------------------------------------------------------
16218 ** Public function.
16222 mat5_open (SF_PRIVATE *psf)
16223 { int subformat, error = 0 ;
16225 if (psf->mode == SFM_READ || (psf->mode == SFM_RDWR && psf->filelength > 0))
16226 { if ((error = mat5_read_header (psf)))
16227 return error ;
16230 if ((psf->sf.format & SF_FORMAT_TYPEMASK) != SF_FORMAT_MAT5)
16231 return SFE_BAD_OPEN_FORMAT ;
16233 subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
16235 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
16236 { if (psf->is_pipe)
16237 return SFE_NO_PIPE_WRITE ;
16239 psf->endian = psf->sf.format & SF_FORMAT_ENDMASK ;
16240 if (CPU_IS_LITTLE_ENDIAN && (psf->endian == SF_ENDIAN_CPU || psf->endian == 0))
16241 psf->endian = SF_ENDIAN_LITTLE ;
16242 else if (CPU_IS_BIG_ENDIAN && (psf->endian == SF_ENDIAN_CPU || psf->endian == 0))
16243 psf->endian = SF_ENDIAN_BIG ;
16245 if ((error = mat5_write_header (psf, SF_FALSE)))
16246 return error ;
16248 psf->write_header = mat5_write_header ;
16251 psf->close = mat5_close ;
16253 psf->blockwidth = psf->bytewidth * psf->sf.channels ;
16255 switch (subformat)
16256 { case SF_FORMAT_PCM_U8 :
16257 case SF_FORMAT_PCM_16 :
16258 case SF_FORMAT_PCM_32 :
16259 error = pcm_init (psf) ;
16260 break ;
16262 case SF_FORMAT_FLOAT :
16263 error = float32_init (psf) ;
16264 break ;
16266 case SF_FORMAT_DOUBLE :
16267 error = double64_init (psf) ;
16268 break ;
16270 default : break ;
16273 return error ;
16274 } /* mat5_open */
16276 /*------------------------------------------------------------------------------
16279 static int
16280 mat5_close (SF_PRIVATE *psf)
16282 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
16283 mat5_write_header (psf, SF_TRUE) ;
16285 return 0 ;
16286 } /* mat5_close */
16288 /*------------------------------------------------------------------------------
16291 static int
16292 mat5_write_header (SF_PRIVATE *psf, int calc_length)
16293 { static const char *sr_name = "samplerate\0\0\0\0\0\0\0\0\0\0\0" ;
16294 static const char *wd_name = "wavedata\0" ;
16295 sf_count_t current, datasize ;
16296 int encoding ;
16298 current = psf_ftell (psf) ;
16300 if (calc_length)
16301 { psf_fseek (psf, 0, SEEK_END) ;
16302 psf->filelength = psf_ftell (psf) ;
16303 psf_fseek (psf, 0, SEEK_SET) ;
16305 psf->datalength = psf->filelength - psf->dataoffset ;
16306 if (psf->dataend)
16307 psf->datalength -= psf->filelength - psf->dataend ;
16309 psf->sf.frames = psf->datalength / (psf->bytewidth * psf->sf.channels) ;
16312 switch (psf->sf.format & SF_FORMAT_SUBMASK)
16313 { case SF_FORMAT_PCM_U8 :
16314 encoding = MAT5_TYPE_UCHAR ;
16315 break ;
16317 case SF_FORMAT_PCM_16 :
16318 encoding = MAT5_TYPE_INT16 ;
16319 break ;
16321 case SF_FORMAT_PCM_32 :
16322 encoding = MAT5_TYPE_INT32 ;
16323 break ;
16325 case SF_FORMAT_FLOAT :
16326 encoding = MAT5_TYPE_FLOAT ;
16327 break ;
16329 case SF_FORMAT_DOUBLE :
16330 encoding = MAT5_TYPE_DOUBLE ;
16331 break ;
16333 default :
16334 return SFE_BAD_OPEN_FORMAT ;
16337 /* Reset the current header length to zero. */
16338 psf->header [0] = 0 ;
16339 psf->headindex = 0 ;
16340 psf_fseek (psf, 0, SEEK_SET) ;
16342 psf_binheader_writef (psf, "S", "MATLAB 5.0 MAT-file, written by " PACKAGE "-" VERSION ", ") ;
16343 psf_get_date_str ((char*) psf->buffer, sizeof (psf->buffer)) ;
16344 psf_binheader_writef (psf, "jS", -1, psf->buffer) ;
16346 memset (psf->buffer, ' ', 124 - psf->headindex) ;
16347 psf_binheader_writef (psf, "b", psf->buffer, 124 - psf->headindex) ;
16349 psf->rwf_endian = psf->endian ;
16351 if (psf->rwf_endian == SF_ENDIAN_BIG)
16352 psf_binheader_writef (psf, "2b", 0x0100, "MI", 2) ;
16353 else
16354 psf_binheader_writef (psf, "2b", 0x0100, "IM", 2) ;
16356 psf_binheader_writef (psf, "444444", MAT5_TYPE_ARRAY, 64, MAT5_TYPE_UINT32, 8, 6, 0) ;
16357 psf_binheader_writef (psf, "4444", MAT5_TYPE_INT32, 8, 1, 1) ;
16358 psf_binheader_writef (psf, "44b", MAT5_TYPE_SCHAR, strlen (sr_name), sr_name, 16) ;
16360 if (psf->sf.samplerate > 0xFFFF)
16361 psf_binheader_writef (psf, "44", MAT5_TYPE_COMP_UINT, psf->sf.samplerate) ;
16362 else
16363 { unsigned short samplerate = psf->sf.samplerate ;
16365 psf_binheader_writef (psf, "422", MAT5_TYPE_COMP_USHORT, samplerate, 0) ;
16368 datasize = psf->sf.frames * psf->sf.channels * psf->bytewidth ;
16370 psf_binheader_writef (psf, "t484444", MAT5_TYPE_ARRAY, datasize + 64, MAT5_TYPE_UINT32, 8, 6, 0) ;
16371 psf_binheader_writef (psf, "t4448", MAT5_TYPE_INT32, 8, psf->sf.channels, psf->sf.frames) ;
16372 psf_binheader_writef (psf, "44b", MAT5_TYPE_SCHAR, strlen (wd_name), wd_name, strlen (wd_name)) ;
16374 datasize = psf->sf.frames * psf->sf.channels * psf->bytewidth ;
16375 if (datasize > 0x7FFFFFFF)
16376 datasize = 0x7FFFFFFF ;
16378 psf_binheader_writef (psf, "t48", encoding, datasize) ;
16380 /* Header construction complete so write it out. */
16381 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
16383 if (psf->error)
16384 return psf->error ;
16386 psf->dataoffset = psf->headindex ;
16388 if (current > 0)
16389 psf_fseek (psf, current, SEEK_SET) ;
16391 return psf->error ;
16392 } /* mat5_write_header */
16394 static int
16395 mat5_read_header (SF_PRIVATE *psf)
16396 { char name [32] ;
16397 short version, endian ;
16398 int type, size, flags1, flags2, rows, cols ;
16400 psf_binheader_readf (psf, "pb", 0, psf->buffer, 124) ;
16402 psf->buffer [125] = 0 ;
16404 if (strlen ((char*) psf->buffer) >= 124)
16405 return SFE_UNIMPLEMENTED ;
16407 if (strstr ((char*) psf->buffer, "MATLAB 5.0 MAT-file") == (char*) psf->buffer)
16408 psf_log_printf (psf, "%s\n", psf->buffer) ;
16411 psf_binheader_readf (psf, "E22", &version, &endian) ;
16413 if (endian == MI_MARKER)
16414 { psf->endian = psf->rwf_endian = SF_ENDIAN_BIG ;
16415 if (CPU_IS_LITTLE_ENDIAN) version = ENDSWAP_SHORT (version) ;
16417 else if (endian == IM_MARKER)
16418 { psf->endian = psf->rwf_endian = SF_ENDIAN_LITTLE ;
16419 if (CPU_IS_BIG_ENDIAN) version = ENDSWAP_SHORT (version) ;
16421 else
16422 return SFE_MAT5_BAD_ENDIAN ;
16424 if ((CPU_IS_LITTLE_ENDIAN && endian == IM_MARKER) ||
16425 (CPU_IS_BIG_ENDIAN && endian == MI_MARKER))
16426 version = ENDSWAP_SHORT (version) ;
16428 psf_log_printf (psf, "Version : 0x%04X\n", version) ;
16429 psf_log_printf (psf, "Endian : 0x%04X => %s\n", endian,
16430 (psf->endian == SF_ENDIAN_LITTLE) ? "Little" : "Big") ;
16432 /*========================================================*/
16433 psf_binheader_readf (psf, "44", &type, &size) ;
16434 psf_log_printf (psf, "Block\n Type : %X Size : %d\n", type, size) ;
16436 if (type != MAT5_TYPE_ARRAY)
16437 return SFE_MAT5_NO_BLOCK ;
16439 psf_binheader_readf (psf, "44", &type, &size) ;
16440 psf_log_printf (psf, " Type : %X Size : %d\n", type, size) ;
16442 if (type != MAT5_TYPE_UINT32)
16443 return SFE_MAT5_NO_BLOCK ;
16445 psf_binheader_readf (psf, "44", &flags1, &flags2) ;
16446 psf_log_printf (psf, " Flg1 : %X Flg2 : %d\n", flags1, flags2) ;
16448 psf_binheader_readf (psf, "44", &type, &size) ;
16449 psf_log_printf (psf, " Type : %X Size : %d\n", type, size) ;
16451 if (type != MAT5_TYPE_INT32)
16452 return SFE_MAT5_NO_BLOCK ;
16454 psf_binheader_readf (psf, "44", &rows, &cols) ;
16455 psf_log_printf (psf, " Rows : %X Cols : %d\n", rows, cols) ;
16457 if (rows != 1 || cols != 1)
16458 return SFE_MAT5_SAMPLE_RATE ;
16460 psf_binheader_readf (psf, "4", &type) ;
16462 if (type == MAT5_TYPE_SCHAR)
16463 { psf_binheader_readf (psf, "4", &size) ;
16464 psf_log_printf (psf, " Type : %X Size : %d\n", type, size) ;
16465 if (size > SIGNED_SIZEOF (name) - 1)
16466 { psf_log_printf (psf, "Error : Bad name length.\n") ;
16467 return SFE_MAT5_NO_BLOCK ;
16470 psf_binheader_readf (psf, "bj", name, size, (8 - (size % 8)) % 8) ;
16471 name [size] = 0 ;
16473 else if ((type & 0xFFFF) == MAT5_TYPE_SCHAR)
16474 { size = type >> 16 ;
16475 if (size > 4)
16476 { psf_log_printf (psf, "Error : Bad name length.\n") ;
16477 return SFE_MAT5_NO_BLOCK ;
16480 psf_log_printf (psf, " Type : %X\n", type) ;
16481 psf_binheader_readf (psf, "4", &name) ;
16482 name [size] = 0 ;
16484 else
16485 return SFE_MAT5_NO_BLOCK ;
16487 psf_log_printf (psf, " Name : %s\n", name) ;
16489 /*-----------------------------------------*/
16491 psf_binheader_readf (psf, "44", &type, &size) ;
16493 switch (type)
16494 { case MAT5_TYPE_DOUBLE :
16495 { double samplerate ;
16497 psf_binheader_readf (psf, "d", &samplerate) ;
16498 LSF_SNPRINTF (name, sizeof (name), "%f\n", samplerate) ;
16499 psf_log_printf (psf, " Val : %s\n", name) ;
16501 psf->sf.samplerate = lrint (samplerate) ;
16503 break ;
16505 case MAT5_TYPE_COMP_USHORT :
16506 { unsigned short samplerate ;
16508 psf_binheader_readf (psf, "j2j", -4, &samplerate, 2) ;
16509 psf_log_printf (psf, " Val : %u\n", samplerate) ;
16510 psf->sf.samplerate = samplerate ;
16512 break ;
16514 case MAT5_TYPE_COMP_UINT :
16515 psf_log_printf (psf, " Val : %u\n", size) ;
16516 psf->sf.samplerate = size ;
16517 break ;
16519 default :
16520 psf_log_printf (psf, " Type : %X Size : %d ***\n", type, size) ;
16521 return SFE_MAT5_SAMPLE_RATE ;
16524 /*-----------------------------------------*/
16527 psf_binheader_readf (psf, "44", &type, &size) ;
16528 psf_log_printf (psf, " Type : %X Size : %d\n", type, size) ;
16530 if (type != MAT5_TYPE_ARRAY)
16531 return SFE_MAT5_NO_BLOCK ;
16533 psf_binheader_readf (psf, "44", &type, &size) ;
16534 psf_log_printf (psf, " Type : %X Size : %d\n", type, size) ;
16536 if (type != MAT5_TYPE_UINT32)
16537 return SFE_MAT5_NO_BLOCK ;
16539 psf_binheader_readf (psf, "44", &flags1, &flags2) ;
16540 psf_log_printf (psf, " Flg1 : %X Flg2 : %d\n", flags1, flags2) ;
16542 psf_binheader_readf (psf, "44", &type, &size) ;
16543 psf_log_printf (psf, " Type : %X Size : %d\n", type, size) ;
16545 if (type != MAT5_TYPE_INT32)
16546 return SFE_MAT5_NO_BLOCK ;
16548 psf_binheader_readf (psf, "44", &rows, &cols) ;
16549 psf_log_printf (psf, " Rows : %X Cols : %d\n", rows, cols) ;
16551 psf_binheader_readf (psf, "4", &type) ;
16553 if (type == MAT5_TYPE_SCHAR)
16554 { psf_binheader_readf (psf, "4", &size) ;
16555 psf_log_printf (psf, " Type : %X Size : %d\n", type, size) ;
16556 if (size > SIGNED_SIZEOF (name) - 1)
16557 { psf_log_printf (psf, "Error : Bad name length.\n") ;
16558 return SFE_MAT5_NO_BLOCK ;
16561 psf_binheader_readf (psf, "bj", name, size, (8 - (size % 8)) % 8) ;
16562 name [size] = 0 ;
16564 else if ((type & 0xFFFF) == MAT5_TYPE_SCHAR)
16565 { size = type >> 16 ;
16566 if (size > 4)
16567 { psf_log_printf (psf, "Error : Bad name length.\n") ;
16568 return SFE_MAT5_NO_BLOCK ;
16571 psf_log_printf (psf, " Type : %X\n", type) ;
16572 psf_binheader_readf (psf, "4", &name) ;
16573 name [size] = 0 ;
16575 else
16576 return SFE_MAT5_NO_BLOCK ;
16578 psf_log_printf (psf, " Name : %s\n", name) ;
16580 psf_binheader_readf (psf, "44", &type, &size) ;
16581 psf_log_printf (psf, " Type : %X Size : %d\n", type, size) ;
16583 /*++++++++++++++++++++++++++++++++++++++++++++++++++*/
16585 if (rows == 0 && cols == 0)
16586 { psf_log_printf (psf, "*** Error : zero channel count.\n") ;
16587 return SFE_MAT5_ZERO_CHANNELS ;
16590 psf->sf.channels = rows ;
16591 psf->sf.frames = cols ;
16593 psf->sf.format = psf->endian | SF_FORMAT_MAT5 ;
16595 switch (type)
16596 { case MAT5_TYPE_DOUBLE :
16597 psf_log_printf (psf, "Data type : double\n") ;
16598 psf->sf.format |= SF_FORMAT_DOUBLE ;
16599 psf->bytewidth = 8 ;
16600 break ;
16602 case MAT5_TYPE_FLOAT :
16603 psf_log_printf (psf, "Data type : float\n") ;
16604 psf->sf.format |= SF_FORMAT_FLOAT ;
16605 psf->bytewidth = 4 ;
16606 break ;
16608 case MAT5_TYPE_INT32 :
16609 psf_log_printf (psf, "Data type : 32 bit PCM\n") ;
16610 psf->sf.format |= SF_FORMAT_PCM_32 ;
16611 psf->bytewidth = 4 ;
16612 break ;
16614 case MAT5_TYPE_INT16 :
16615 psf_log_printf (psf, "Data type : 16 bit PCM\n") ;
16616 psf->sf.format |= SF_FORMAT_PCM_16 ;
16617 psf->bytewidth = 2 ;
16618 break ;
16620 case MAT5_TYPE_UCHAR :
16621 psf_log_printf (psf, "Data type : unsigned 8 bit PCM\n") ;
16622 psf->sf.format |= SF_FORMAT_PCM_U8 ;
16623 psf->bytewidth = 1 ;
16624 break ;
16626 default :
16627 psf_log_printf (psf, "*** Error : Bad marker %08X\n", type) ;
16628 return SFE_UNIMPLEMENTED ;
16631 psf->dataoffset = psf_ftell (psf) ;
16632 psf->datalength = psf->filelength - psf->dataoffset ;
16634 return 0 ;
16635 } /* mat5_read_header */
16638 ** Do not edit or modify anything in this comment block.
16639 ** The arch-tag line is a file identity tag for the GNU Arch
16640 ** revision control system.
16642 ** arch-tag: dfdb6742-b2be-4be8-b390-d0c674e8bc8e
16645 ** Copyright (C) 1999-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
16647 ** This program is free software; you can redistribute it and/or modify
16648 ** it under the terms of the GNU Lesser General Public License as published by
16649 ** the Free Software Foundation; either version 2.1 of the License, or
16650 ** (at your option) any later version.
16652 ** This program is distributed in the hope that it will be useful,
16653 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16654 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16655 ** GNU Lesser General Public License for more details.
16657 ** You should have received a copy of the GNU Lesser General Public License
16658 ** along with this program; if not, write to the Free Software
16659 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16662 #include <stdio.h>
16663 #include <stdlib.h>
16664 #include <string.h>
16667 /* These required here because we write the header in this file. */
16669 #define RIFF_MARKER (MAKE_MARKER ('R', 'I', 'F', 'F'))
16670 #define WAVE_MARKER (MAKE_MARKER ('W', 'A', 'V', 'E'))
16671 #define fmt_MARKER (MAKE_MARKER ('f', 'm', 't', ' '))
16672 #define fact_MARKER (MAKE_MARKER ('f', 'a', 'c', 't'))
16673 #define data_MARKER (MAKE_MARKER ('d', 'a', 't', 'a'))
16675 #define WAVE_FORMAT_MS_ADPCM 0x0002
16677 typedef struct
16678 { int channels, blocksize, samplesperblock, blocks, dataremaining ;
16679 int blockcount ;
16680 sf_count_t samplecount ;
16681 short *samples ;
16682 unsigned char *block ;
16683 #if HAVE_FLEXIBLE_ARRAY
16684 unsigned short dummydata [] ; /* ISO C99 struct flexible array. */
16685 #else
16686 unsigned short dummydata [1] ; /* This is a hack an might not work. */
16687 #endif
16688 } MSADPCM_PRIVATE ;
16690 /*============================================================================================
16691 ** MS ADPCM static data and functions.
16694 static int AdaptationTable [] =
16695 { 230, 230, 230, 230, 307, 409, 512, 614,
16696 768, 614, 512, 409, 307, 230, 230, 230
16699 /* TODO : The first 7 coef's are are always hardcode and must
16700 appear in the actual WAVE file. They should be read in
16701 in case a sound program added extras to the list. */
16703 static int AdaptCoeff1 [MSADPCM_ADAPT_COEFF_COUNT] =
16704 { 256, 512, 0, 192, 240, 460, 392
16707 static int AdaptCoeff2 [MSADPCM_ADAPT_COEFF_COUNT] =
16708 { 0, -256, 0, 64, 0, -208, -232
16711 /*============================================================================================
16712 ** MS ADPCM Block Layout.
16713 ** ======================
16714 ** Block is usually 256, 512 or 1024 bytes depending on sample rate.
16715 ** For a mono file, the block is laid out as follows:
16716 ** byte purpose
16717 ** 0 block predictor [0..6]
16718 ** 1,2 initial idelta (positive)
16719 ** 3,4 sample 1
16720 ** 5,6 sample 0
16721 ** 7..n packed bytecodes
16723 ** For a stereo file, the block is laid out as follows:
16724 ** byte purpose
16725 ** 0 block predictor [0..6] for left channel
16726 ** 1 block predictor [0..6] for right channel
16727 ** 2,3 initial idelta (positive) for left channel
16728 ** 4,5 initial idelta (positive) for right channel
16729 ** 6,7 sample 1 for left channel
16730 ** 8,9 sample 1 for right channel
16731 ** 10,11 sample 0 for left channel
16732 ** 12,13 sample 0 for right channel
16733 ** 14..n packed bytecodes
16736 /*============================================================================================
16737 ** Static functions.
16740 static int msadpcm_decode_block (SF_PRIVATE *psf, MSADPCM_PRIVATE *pms) ;
16741 static sf_count_t msadpcm_read_block (SF_PRIVATE *psf, MSADPCM_PRIVATE *pms, short *ptr, int len) ;
16743 static int msadpcm_encode_block (SF_PRIVATE *psf, MSADPCM_PRIVATE *pms) ;
16744 static sf_count_t msadpcm_write_block (SF_PRIVATE *psf, MSADPCM_PRIVATE *pms, short *ptr, int len) ;
16746 static sf_count_t msadpcm_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
16747 static sf_count_t msadpcm_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
16748 static sf_count_t msadpcm_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
16749 static sf_count_t msadpcm_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
16751 static sf_count_t msadpcm_write_s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
16752 static sf_count_t msadpcm_write_i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
16753 static sf_count_t msadpcm_write_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
16754 static sf_count_t msadpcm_write_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
16756 static sf_count_t msadpcm_seek (SF_PRIVATE *psf, int mode, sf_count_t offset) ;
16757 static int msadpcm_close (SF_PRIVATE *psf) ;
16759 static void choose_predictor (unsigned int channels, short *data, int *bpred, int *idelta) ;
16761 /*============================================================================================
16762 ** MS ADPCM Read Functions.
16766 wav_w64_msadpcm_init (SF_PRIVATE *psf, int blockalign, int samplesperblock)
16767 { MSADPCM_PRIVATE *pms ;
16768 unsigned int pmssize ;
16769 int count ;
16771 if (psf->mode == SFM_WRITE)
16772 samplesperblock = 2 + 2 * (blockalign - 7 * psf->sf.channels) / psf->sf.channels ;
16774 pmssize = sizeof (MSADPCM_PRIVATE) + blockalign + 3 * psf->sf.channels * samplesperblock ;
16776 if (! (psf->fdata = malloc (pmssize)))
16777 return SFE_MALLOC_FAILED ;
16778 pms = (MSADPCM_PRIVATE*) psf->fdata ;
16779 memset (pms, 0, pmssize) ;
16781 pms->samples = pms->dummydata ;
16782 pms->block = (unsigned char*) (pms->dummydata + psf->sf.channels * samplesperblock) ;
16784 pms->channels = psf->sf.channels ;
16785 pms->blocksize = blockalign ;
16786 pms->samplesperblock = samplesperblock ;
16788 if (psf->mode == SFM_READ)
16789 { pms->dataremaining = psf->datalength ;
16791 if (psf->datalength % pms->blocksize)
16792 pms->blocks = psf->datalength / pms->blocksize + 1 ;
16793 else
16794 pms->blocks = psf->datalength / pms->blocksize ;
16796 count = 2 * (pms->blocksize - 6 * pms->channels) / pms->channels ;
16797 if (pms->samplesperblock != count)
16798 psf_log_printf (psf, "*** Warning : samplesperblock shoud be %d.\n", count) ;
16800 psf->sf.frames = (psf->datalength / pms->blocksize) * pms->samplesperblock ;
16802 psf_log_printf (psf, " bpred idelta\n") ;
16804 msadpcm_decode_block (psf, pms) ;
16806 psf->read_short = msadpcm_read_s ;
16807 psf->read_int = msadpcm_read_i ;
16808 psf->read_float = msadpcm_read_f ;
16809 psf->read_double = msadpcm_read_d ;
16812 if (psf->mode == SFM_WRITE)
16813 { pms->samples = pms->dummydata ;
16815 pms->samplecount = 0 ;
16817 psf->write_short = msadpcm_write_s ;
16818 psf->write_int = msadpcm_write_i ;
16819 psf->write_float = msadpcm_write_f ;
16820 psf->write_double = msadpcm_write_d ;
16823 psf->seek = msadpcm_seek ;
16824 psf->close = msadpcm_close ;
16826 return 0 ;
16827 } /* wav_w64_msadpcm_init */
16829 static int
16830 msadpcm_decode_block (SF_PRIVATE *psf, MSADPCM_PRIVATE *pms)
16831 { int chan, k, blockindx, sampleindx ;
16832 short bytecode, bpred [2], chan_idelta [2] ;
16834 int predict ;
16835 int current ;
16836 int idelta ;
16838 pms->blockcount ++ ;
16839 pms->samplecount = 0 ;
16841 if (pms->blockcount > pms->blocks)
16842 { memset (pms->samples, 0, pms->samplesperblock * pms->channels) ;
16843 return 1 ;
16846 if ((k = psf_fread (pms->block, 1, pms->blocksize, psf)) != pms->blocksize)
16847 psf_log_printf (psf, "*** Warning : short read (%d != %d).\n", k, pms->blocksize) ;
16849 /* Read and check the block header. */
16851 if (pms->channels == 1)
16852 { bpred [0] = pms->block [0] ;
16854 if (bpred [0] >= 7)
16855 psf_log_printf (psf, "MS ADPCM synchronisation error (%d).\n", bpred [0]) ;
16857 chan_idelta [0] = pms->block [1] | (pms->block [2] << 8) ;
16858 chan_idelta [1] = 0 ;
16860 psf_log_printf (psf, "(%d) (%d)\n", bpred [0], chan_idelta [0]) ;
16862 pms->samples [1] = pms->block [3] | (pms->block [4] << 8) ;
16863 pms->samples [0] = pms->block [5] | (pms->block [6] << 8) ;
16864 blockindx = 7 ;
16866 else
16867 { bpred [0] = pms->block [0] ;
16868 bpred [1] = pms->block [1] ;
16870 if (bpred [0] >= 7 || bpred [1] >= 7)
16871 psf_log_printf (psf, "MS ADPCM synchronisation error (%d %d).\n", bpred [0], bpred [1]) ;
16873 chan_idelta [0] = pms->block [2] | (pms->block [3] << 8) ;
16874 chan_idelta [1] = pms->block [4] | (pms->block [5] << 8) ;
16876 psf_log_printf (psf, "(%d, %d) (%d, %d)\n", bpred [0], bpred [1], chan_idelta [0], chan_idelta [1]) ;
16878 pms->samples [2] = pms->block [6] | (pms->block [7] << 8) ;
16879 pms->samples [3] = pms->block [8] | (pms->block [9] << 8) ;
16881 pms->samples [0] = pms->block [10] | (pms->block [11] << 8) ;
16882 pms->samples [1] = pms->block [12] | (pms->block [13] << 8) ;
16884 blockindx = 14 ;
16887 /*--------------------------------------------------------
16888 This was left over from a time when calculations were done
16889 as ints rather than shorts. Keep this around as a reminder
16890 in case I ever find a file which decodes incorrectly.
16892 if (chan_idelta [0] & 0x8000)
16893 chan_idelta [0] -= 0x10000 ;
16894 if (chan_idelta [1] & 0x8000)
16895 chan_idelta [1] -= 0x10000 ;
16896 --------------------------------------------------------*/
16898 /* Pull apart the packed 4 bit samples and store them in their
16899 ** correct sample positions.
16902 sampleindx = 2 * pms->channels ;
16903 while (blockindx < pms->blocksize)
16904 { bytecode = pms->block [blockindx++] ;
16905 pms->samples [sampleindx++] = (bytecode >> 4) & 0x0F ;
16906 pms->samples [sampleindx++] = bytecode & 0x0F ;
16909 /* Decode the encoded 4 bit samples. */
16911 for (k = 2 * pms->channels ; k < (pms->samplesperblock * pms->channels) ; k ++)
16912 { chan = (pms->channels > 1) ? (k % 2) : 0 ;
16914 bytecode = pms->samples [k] & 0xF ;
16916 /* Compute next Adaptive Scale Factor (ASF) */
16917 idelta = chan_idelta [chan] ;
16918 chan_idelta [chan] = (AdaptationTable [bytecode] * idelta) >> 8 ; /* => / 256 => FIXED_POINT_ADAPTATION_BASE == 256 */
16919 if (chan_idelta [chan] < 16)
16920 chan_idelta [chan] = 16 ;
16921 if (bytecode & 0x8)
16922 bytecode -= 0x10 ;
16924 predict = ((pms->samples [k - pms->channels] * AdaptCoeff1 [bpred [chan]])
16925 + (pms->samples [k - 2 * pms->channels] * AdaptCoeff2 [bpred [chan]])) >> 8 ; /* => / 256 => FIXED_POINT_COEFF_BASE == 256 */
16926 current = (bytecode * idelta) + predict ;
16928 if (current > 32767)
16929 current = 32767 ;
16930 else if (current < -32768)
16931 current = -32768 ;
16933 pms->samples [k] = current ;
16936 return 1 ;
16937 } /* msadpcm_decode_block */
16939 static sf_count_t
16940 msadpcm_read_block (SF_PRIVATE *psf, MSADPCM_PRIVATE *pms, short *ptr, int len)
16941 { int count, total = 0, indx = 0 ;
16943 while (indx < len)
16944 { if (pms->blockcount >= pms->blocks && pms->samplecount >= pms->samplesperblock)
16945 { memset (&(ptr [indx]), 0, (size_t) ((len - indx) * sizeof (short))) ;
16946 return total ;
16949 if (pms->samplecount >= pms->samplesperblock)
16950 msadpcm_decode_block (psf, pms) ;
16952 count = (pms->samplesperblock - pms->samplecount) * pms->channels ;
16953 count = (len - indx > count) ? count : len - indx ;
16955 memcpy (&(ptr [indx]), &(pms->samples [pms->samplecount * pms->channels]), count * sizeof (short)) ;
16956 indx += count ;
16957 pms->samplecount += count / pms->channels ;
16958 total = indx ;
16961 return total ;
16962 } /* msadpcm_read_block */
16964 static sf_count_t
16965 msadpcm_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
16966 { MSADPCM_PRIVATE *pms ;
16967 int readcount, count ;
16968 sf_count_t total = 0 ;
16970 if (! psf->fdata)
16971 return 0 ;
16972 pms = (MSADPCM_PRIVATE*) psf->fdata ;
16974 while (len > 0)
16975 { readcount = (len > 0x10000000) ? 0x10000000 : (int) len ;
16977 count = msadpcm_read_block (psf, pms, ptr, readcount) ;
16979 total += count ;
16980 len -= count ;
16981 if (count != readcount)
16982 break ;
16985 return total ;
16986 } /* msadpcm_read_s */
16988 static sf_count_t
16989 msadpcm_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
16990 { MSADPCM_PRIVATE *pms ;
16991 short *sptr ;
16992 int k, bufferlen, readcount = 0, count ;
16993 sf_count_t total = 0 ;
16995 if (! psf->fdata)
16996 return 0 ;
16997 pms = (MSADPCM_PRIVATE*) psf->fdata ;
16999 sptr = (short*) psf->buffer ;
17000 bufferlen = SF_BUFFER_LEN / sizeof (short) ;
17001 while (len > 0)
17002 { readcount = (len >= bufferlen) ? bufferlen : len ;
17003 count = msadpcm_read_block (psf, pms, sptr, readcount) ;
17004 for (k = 0 ; k < readcount ; k++)
17005 ptr [total + k] = sptr [k] << 16 ;
17006 total += count ;
17007 len -= readcount ;
17008 if (count != readcount)
17009 break ;
17011 return total ;
17012 } /* msadpcm_read_i */
17014 static sf_count_t
17015 msadpcm_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
17016 { MSADPCM_PRIVATE *pms ;
17017 short *sptr ;
17018 int k, bufferlen, readcount = 0, count ;
17019 sf_count_t total = 0 ;
17020 float normfact ;
17022 if (! psf->fdata)
17023 return 0 ;
17024 pms = (MSADPCM_PRIVATE*) psf->fdata ;
17026 normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x8000) : 1.0 ;
17027 sptr = (short*) psf->buffer ;
17028 bufferlen = SF_BUFFER_LEN / sizeof (short) ;
17029 while (len > 0)
17030 { readcount = (len >= bufferlen) ? bufferlen : len ;
17031 count = msadpcm_read_block (psf, pms, sptr, readcount) ;
17032 for (k = 0 ; k < readcount ; k++)
17033 ptr [total + k] = normfact * (float) (sptr [k]) ;
17034 total += count ;
17035 len -= readcount ;
17036 if (count != readcount)
17037 break ;
17039 return total ;
17040 } /* msadpcm_read_f */
17042 static sf_count_t
17043 msadpcm_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
17044 { MSADPCM_PRIVATE *pms ;
17045 short *sptr ;
17046 int k, bufferlen, readcount = 0, count ;
17047 sf_count_t total = 0 ;
17048 double normfact ;
17050 normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x8000) : 1.0 ;
17052 if (! psf->fdata)
17053 return 0 ;
17054 pms = (MSADPCM_PRIVATE*) psf->fdata ;
17056 sptr = (short*) psf->buffer ;
17057 bufferlen = SF_BUFFER_LEN / sizeof (short) ;
17058 while (len > 0)
17059 { readcount = (len >= bufferlen) ? bufferlen : len ;
17060 count = msadpcm_read_block (psf, pms, sptr, readcount) ;
17061 for (k = 0 ; k < readcount ; k++)
17062 ptr [total + k] = normfact * (double) (sptr [k]) ;
17063 total += count ;
17064 len -= readcount ;
17065 if (count != readcount)
17066 break ;
17068 return total ;
17069 } /* msadpcm_read_d */
17071 static sf_count_t
17072 msadpcm_seek (SF_PRIVATE *psf, int mode, sf_count_t offset)
17073 { MSADPCM_PRIVATE *pms ;
17074 int newblock, newsample ;
17076 if (! psf->fdata)
17077 return 0 ;
17078 pms = (MSADPCM_PRIVATE*) psf->fdata ;
17080 if (psf->datalength < 0 || psf->dataoffset < 0)
17081 { psf->error = SFE_BAD_SEEK ;
17082 return ((sf_count_t) -1) ;
17085 if (offset == 0)
17086 { psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
17087 pms->blockcount = 0 ;
17088 msadpcm_decode_block (psf, pms) ;
17089 pms->samplecount = 0 ;
17090 return 0 ;
17093 if (offset < 0 || offset > pms->blocks * pms->samplesperblock)
17094 { psf->error = SFE_BAD_SEEK ;
17095 return ((sf_count_t) -1) ;
17098 newblock = offset / pms->samplesperblock ;
17099 newsample = offset % pms->samplesperblock ;
17101 if (mode == SFM_READ)
17102 { psf_fseek (psf, psf->dataoffset + newblock * pms->blocksize, SEEK_SET) ;
17103 pms->blockcount = newblock ;
17104 msadpcm_decode_block (psf, pms) ;
17105 pms->samplecount = newsample ;
17107 else
17108 { /* What to do about write??? */
17109 psf->error = SFE_BAD_SEEK ;
17110 return ((sf_count_t) -1) ;
17113 return newblock * pms->samplesperblock + newsample ;
17114 } /* msadpcm_seek */
17116 /*==========================================================================================
17117 ** MS ADPCM Write Functions.
17120 void
17121 msadpcm_write_adapt_coeffs (SF_PRIVATE *psf)
17122 { int k ;
17124 for (k = 0 ; k < MSADPCM_ADAPT_COEFF_COUNT ; k++)
17125 psf_binheader_writef (psf, "e22", AdaptCoeff1 [k], AdaptCoeff2 [k]) ;
17126 } /* msadpcm_write_adapt_coeffs */
17128 /*==========================================================================================
17131 static int
17132 msadpcm_encode_block (SF_PRIVATE *psf, MSADPCM_PRIVATE *pms)
17133 { unsigned int blockindx ;
17134 unsigned char byte ;
17135 int chan, k, predict, bpred [2], idelta [2], errordelta, newsamp ;
17137 choose_predictor (pms->channels, pms->samples, bpred, idelta) ;
17139 /* Write the block header. */
17141 if (pms->channels == 1)
17142 { pms->block [0] = bpred [0] ;
17143 pms->block [1] = idelta [0] & 0xFF ;
17144 pms->block [2] = idelta [0] >> 8 ;
17145 pms->block [3] = pms->samples [1] & 0xFF ;
17146 pms->block [4] = pms->samples [1] >> 8 ;
17147 pms->block [5] = pms->samples [0] & 0xFF ;
17148 pms->block [6] = pms->samples [0] >> 8 ;
17150 blockindx = 7 ;
17151 byte = 0 ;
17153 /* Encode the samples as 4 bit. */
17155 for (k = 2 ; k < pms->samplesperblock ; k++)
17156 { predict = (pms->samples [k-1] * AdaptCoeff1 [bpred [0]] + pms->samples [k-2] * AdaptCoeff2 [bpred [0]]) >> 8 ;
17157 errordelta = (pms->samples [k] - predict) / idelta [0] ;
17158 if (errordelta < -8)
17159 errordelta = -8 ;
17160 else if (errordelta > 7)
17161 errordelta = 7 ;
17162 newsamp = predict + (idelta [0] * errordelta) ;
17163 if (newsamp > 32767)
17164 newsamp = 32767 ;
17165 else if (newsamp < -32768)
17166 newsamp = -32768 ;
17167 if (errordelta < 0)
17168 errordelta += 0x10 ;
17170 byte = (byte << 4) | (errordelta & 0xF) ;
17171 if (k % 2)
17172 { pms->block [blockindx++] = byte ;
17173 byte = 0 ;
17176 idelta [0] = (idelta [0] * AdaptationTable [errordelta]) >> 8 ;
17177 if (idelta [0] < 16)
17178 idelta [0] = 16 ;
17179 pms->samples [k] = newsamp ;
17182 else
17183 { /* Stereo file. */
17184 pms->block [0] = bpred [0] ;
17185 pms->block [1] = bpred [1] ;
17187 pms->block [2] = idelta [0] & 0xFF ;
17188 pms->block [3] = idelta [0] >> 8 ;
17189 pms->block [4] = idelta [1] & 0xFF ;
17190 pms->block [5] = idelta [1] >> 8 ;
17192 pms->block [6] = pms->samples [2] & 0xFF ;
17193 pms->block [7] = pms->samples [2] >> 8 ;
17194 pms->block [8] = pms->samples [3] & 0xFF ;
17195 pms->block [9] = pms->samples [3] >> 8 ;
17197 pms->block [10] = pms->samples [0] & 0xFF ;
17198 pms->block [11] = pms->samples [0] >> 8 ;
17199 pms->block [12] = pms->samples [1] & 0xFF ;
17200 pms->block [13] = pms->samples [1] >> 8 ;
17202 blockindx = 14 ;
17203 byte = 0 ;
17204 chan = 1 ;
17206 for (k = 4 ; k < 2 * pms->samplesperblock ; k++)
17207 { chan = k & 1 ;
17209 predict = (pms->samples [k-2] * AdaptCoeff1 [bpred [chan]] + pms->samples [k-4] * AdaptCoeff2 [bpred [chan]]) >> 8 ;
17210 errordelta = (pms->samples [k] - predict) / idelta [chan] ;
17213 if (errordelta < -8)
17214 errordelta = -8 ;
17215 else if (errordelta > 7)
17216 errordelta = 7 ;
17217 newsamp = predict + (idelta [chan] * errordelta) ;
17218 if (newsamp > 32767)
17219 newsamp = 32767 ;
17220 else if (newsamp < -32768)
17221 newsamp = -32768 ;
17222 if (errordelta < 0)
17223 errordelta += 0x10 ;
17225 byte = (byte << 4) | (errordelta & 0xF) ;
17227 if (chan)
17228 { pms->block [blockindx++] = byte ;
17229 byte = 0 ;
17232 idelta [chan] = (idelta [chan] * AdaptationTable [errordelta]) >> 8 ;
17233 if (idelta [chan] < 16)
17234 idelta [chan] = 16 ;
17235 pms->samples [k] = newsamp ;
17239 /* Write the block to disk. */
17241 if ((k = psf_fwrite (pms->block, 1, pms->blocksize, psf)) != pms->blocksize)
17242 psf_log_printf (psf, "*** Warning : short write (%d != %d).\n", k, pms->blocksize) ;
17244 memset (pms->samples, 0, pms->samplesperblock * sizeof (short)) ;
17246 pms->blockcount ++ ;
17247 pms->samplecount = 0 ;
17249 return 1 ;
17250 } /* msadpcm_encode_block */
17252 static sf_count_t
17253 msadpcm_write_block (SF_PRIVATE *psf, MSADPCM_PRIVATE *pms, short *ptr, int len)
17254 { int count, total = 0, indx = 0 ;
17256 while (indx < len)
17257 { count = (pms->samplesperblock - pms->samplecount) * pms->channels ;
17259 if (count > len - indx)
17260 count = len - indx ;
17262 memcpy (&(pms->samples [pms->samplecount * pms->channels]), &(ptr [total]), count * sizeof (short)) ;
17263 indx += count ;
17264 pms->samplecount += count / pms->channels ;
17265 total = indx ;
17267 if (pms->samplecount >= pms->samplesperblock)
17268 msadpcm_encode_block (psf, pms) ;
17271 return total ;
17272 } /* msadpcm_write_block */
17274 static sf_count_t
17275 msadpcm_write_s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
17276 { MSADPCM_PRIVATE *pms ;
17277 int writecount, count ;
17278 sf_count_t total = 0 ;
17280 if (! psf->fdata)
17281 return 0 ;
17282 pms = (MSADPCM_PRIVATE*) psf->fdata ;
17284 while (len > 0)
17285 { writecount = (len > 0x10000000) ? 0x10000000 : (int) len ;
17287 count = msadpcm_write_block (psf, pms, ptr, writecount) ;
17289 total += count ;
17290 len -= count ;
17291 if (count != writecount)
17292 break ;
17295 return total ;
17296 } /* msadpcm_write_s */
17298 static sf_count_t
17299 msadpcm_write_i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
17300 { MSADPCM_PRIVATE *pms ;
17301 short *sptr ;
17302 int k, bufferlen, writecount, count ;
17303 sf_count_t total = 0 ;
17305 if (! psf->fdata)
17306 return 0 ;
17307 pms = (MSADPCM_PRIVATE*) psf->fdata ;
17309 sptr = (short*) psf->buffer ;
17310 bufferlen = SF_BUFFER_LEN / sizeof (short) ;
17311 while (len > 0)
17312 { writecount = (len >= bufferlen) ? bufferlen : len ;
17313 for (k = 0 ; k < writecount ; k++)
17314 sptr [k] = ptr [total + k] >> 16 ;
17315 count = msadpcm_write_block (psf, pms, sptr, writecount) ;
17316 total += count ;
17317 len -= writecount ;
17318 if (count != writecount)
17319 break ;
17321 return total ;
17322 } /* msadpcm_write_i */
17324 static sf_count_t
17325 msadpcm_write_f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
17326 { MSADPCM_PRIVATE *pms ;
17327 short *sptr ;
17328 int k, bufferlen, writecount, count ;
17329 sf_count_t total = 0 ;
17330 float normfact ;
17332 if (! psf->fdata)
17333 return 0 ;
17334 pms = (MSADPCM_PRIVATE*) psf->fdata ;
17336 normfact = (psf->norm_float == SF_TRUE) ? (1.0 * 0x7FFF) : 1.0 ;
17338 sptr = (short*) psf->buffer ;
17339 bufferlen = SF_BUFFER_LEN / sizeof (short) ;
17340 while (len > 0)
17341 { writecount = (len >= bufferlen) ? bufferlen : len ;
17342 for (k = 0 ; k < writecount ; k++)
17343 sptr [k] = lrintf (normfact * ptr [total + k]) ;
17344 count = msadpcm_write_block (psf, pms, sptr, writecount) ;
17345 total += count ;
17346 len -= writecount ;
17347 if (count != writecount)
17348 break ;
17350 return total ;
17351 } /* msadpcm_write_f */
17353 static sf_count_t
17354 msadpcm_write_d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
17355 { MSADPCM_PRIVATE *pms ;
17356 short *sptr ;
17357 int k, bufferlen, writecount, count ;
17358 sf_count_t total = 0 ;
17359 double normfact ;
17361 normfact = (psf->norm_double == SF_TRUE) ? (1.0 * 0x7FFF) : 1.0 ;
17363 if (! psf->fdata)
17364 return 0 ;
17365 pms = (MSADPCM_PRIVATE*) psf->fdata ;
17367 sptr = (short*) psf->buffer ;
17368 bufferlen = SF_BUFFER_LEN / sizeof (short) ;
17369 while (len > 0)
17370 { writecount = (len >= bufferlen) ? bufferlen : len ;
17371 for (k = 0 ; k < writecount ; k++)
17372 sptr [k] = lrint (normfact * ptr [total + k]) ;
17373 count = msadpcm_write_block (psf, pms, sptr, writecount) ;
17374 total += count ;
17375 len -= writecount ;
17376 if (count != writecount)
17377 break ;
17379 return total ;
17380 } /* msadpcm_write_d */
17382 /*========================================================================================
17385 static int
17386 msadpcm_close (SF_PRIVATE *psf)
17387 { MSADPCM_PRIVATE *pms ;
17389 if (! psf->fdata)
17390 return 0 ;
17392 pms = (MSADPCM_PRIVATE*) psf->fdata ;
17394 if (psf->mode == SFM_WRITE)
17395 { /* Now we know static int for certain the length of the file we can
17396 ** re-write the header.
17399 if (pms->samplecount && pms->samplecount < pms->samplesperblock)
17400 msadpcm_encode_block (psf, pms) ;
17402 if (psf->write_header)
17403 psf->write_header (psf, SF_TRUE) ;
17406 return 0 ;
17407 } /* msadpcm_close */
17409 /*========================================================================================
17410 ** Static functions.
17413 /*----------------------------------------------------------------------------------------
17414 ** Choosing the block predictor.
17415 ** Each block requires a predictor and an idelta for each channel.
17416 ** The predictor is in the range [0..6] which is an indx into the two AdaptCoeff tables.
17417 ** The predictor is chosen by trying all of the possible predictors on a small set of
17418 ** samples at the beginning of the block. The predictor with the smallest average
17419 ** abs (idelta) is chosen as the best predictor for this block.
17420 ** The value of idelta is chosen to to give a 4 bit code value of +/- 4 (approx. half the
17421 ** max. code value). If the average abs (idelta) is zero, the sixth predictor is chosen.
17422 ** If the value of idelta is less then 16 it is set to 16.
17424 ** Microsoft uses an IDELTA_COUNT (number of sample pairs used to choose best predictor)
17425 ** value of 3. The best possible results would be obtained by using all the samples to
17426 ** choose the predictor.
17429 #define IDELTA_COUNT 3
17431 static void
17432 choose_predictor (unsigned int channels, short *data, int *block_pred, int *idelta)
17433 { unsigned int chan, k, bpred, idelta_sum, best_bpred, best_idelta ;
17435 for (chan = 0 ; chan < channels ; chan++)
17436 { best_bpred = best_idelta = 0 ;
17438 for (bpred = 0 ; bpred < 7 ; bpred++)
17439 { idelta_sum = 0 ;
17440 for (k = 2 ; k < 2 + IDELTA_COUNT ; k++)
17441 idelta_sum += abs (data [k * channels] - ((data [(k - 1) * channels] * AdaptCoeff1 [bpred] + data [(k - 2) * channels] * AdaptCoeff2 [bpred]) >> 8)) ;
17442 idelta_sum /= (4 * IDELTA_COUNT) ;
17444 if (bpred == 0 || idelta_sum < best_idelta)
17445 { best_bpred = bpred ;
17446 best_idelta = idelta_sum ;
17449 if (! idelta_sum)
17450 { best_bpred = bpred ;
17451 best_idelta = 16 ;
17452 break ;
17455 } ; /* for bpred ... */
17456 if (best_idelta < 16)
17457 best_idelta = 16 ;
17459 block_pred [chan] = best_bpred ;
17460 idelta [chan] = best_idelta ;
17463 return ;
17464 } /* choose_predictor */
17466 ** Do not edit or modify anything in this comment block.
17467 ** The arch-tag line is a file identity tag for the GNU Arch
17468 ** revision control system.
17470 ** arch-tag: a98908a3-5305-4935-872b-77d6a86c330f
17473 ** Copyright (C) 1999-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
17475 ** This program is free software; you can redistribute it and/or modify
17476 ** it under the terms of the GNU Lesser General Public License as published by
17477 ** the Free Software Foundation; either version 2.1 of the License, or
17478 ** (at your option) any later version.
17480 ** This program is distributed in the hope that it will be useful,
17481 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17482 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17483 ** GNU Lesser General Public License for more details.
17485 ** You should have received a copy of the GNU Lesser General Public License
17486 ** along with this program; if not, write to the Free Software
17487 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17491 ** Some of the information used to read NIST files was gleaned from
17492 ** reading the code of Bill Schottstaedt's sndlib library
17493 ** ftp://ccrma-ftp.stanford.edu/pub/Lisp/sndlib.tar.gz
17494 ** However, no code from that package was used.
17497 #include <stdio.h>
17498 #include <fcntl.h>
17499 #include <string.h>
17500 #include <ctype.h>
17503 /*------------------------------------------------------------------------------
17506 #define NIST_HEADER_LENGTH 1024
17508 /*------------------------------------------------------------------------------
17509 ** Private static functions.
17512 static int nist_close (SF_PRIVATE *psf) ;
17513 static int nist_write_header (SF_PRIVATE *psf, int calc_length) ;
17514 static int nist_read_header (SF_PRIVATE *psf) ;
17516 /*------------------------------------------------------------------------------
17520 nist_open (SF_PRIVATE *psf)
17521 { int subformat, error ;
17523 if (psf->mode == SFM_READ || (psf->mode == SFM_RDWR && psf->filelength > 0))
17524 { if ((error = nist_read_header (psf)))
17525 return error ;
17528 subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
17530 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
17531 { if (psf->is_pipe)
17532 return SFE_NO_PIPE_WRITE ;
17534 if ((psf->sf.format & SF_FORMAT_TYPEMASK) != SF_FORMAT_NIST)
17535 return SFE_BAD_OPEN_FORMAT ;
17537 psf->endian = psf->sf.format & SF_FORMAT_ENDMASK ;
17538 if (psf->endian == 0 || psf->endian == SF_ENDIAN_CPU)
17539 psf->endian = (CPU_IS_BIG_ENDIAN) ? SF_ENDIAN_BIG : SF_ENDIAN_LITTLE ;
17541 psf->blockwidth = psf->bytewidth * psf->sf.channels ;
17543 if ((error = nist_write_header (psf, SF_FALSE)))
17544 return error ;
17546 psf->write_header = nist_write_header ;
17549 psf->close = nist_close ;
17551 switch (psf->sf.format & SF_FORMAT_SUBMASK)
17552 { case SF_FORMAT_PCM_S8 :
17553 error = pcm_init (psf) ;
17554 break ;
17556 case SF_FORMAT_PCM_16 :
17557 case SF_FORMAT_PCM_24 :
17558 case SF_FORMAT_PCM_32 :
17559 error = pcm_init (psf) ;
17560 break ;
17562 case SF_FORMAT_ULAW :
17563 error = ulaw_init (psf) ;
17564 break ;
17566 case SF_FORMAT_ALAW :
17567 error = alaw_init (psf) ;
17568 break ;
17570 default : error = SFE_UNIMPLEMENTED ;
17571 break ;
17574 return error ;
17575 } /* nist_open */
17577 /*------------------------------------------------------------------------------
17580 static char bad_header [] =
17581 { 'N', 'I', 'S', 'T', '_', '1', 'A', 0x0d, 0x0a,
17582 ' ', ' ', ' ', '1', '0', '2', '4', 0x0d, 0x0a,
17586 static int
17587 nist_read_header (SF_PRIVATE *psf)
17588 { char *psf_header ;
17589 int bitwidth = 0, bytes = 0, count, encoding ;
17590 char str [64], *cptr ;
17591 long samples ;
17593 psf->sf.format = SF_FORMAT_NIST ;
17595 psf_header = (char*) psf->buffer ;
17597 if (sizeof (psf->header) <= NIST_HEADER_LENGTH)
17598 return SFE_INTERNAL ;
17600 /* Go to start of file and read in the whole header. */
17601 psf_binheader_readf (psf, "pb", 0, psf_header, NIST_HEADER_LENGTH) ;
17603 /* Header is a string, so make sure it is null terminated. */
17604 psf_header [NIST_HEADER_LENGTH] = 0 ;
17606 /* Now trim the header after the end marker. */
17607 if ((cptr = strstr (psf_header, "end_head")))
17608 { cptr += strlen ("end_head") + 1 ;
17609 cptr [0] = 0 ;
17612 if (strstr (psf_header, bad_header) == psf_header)
17613 return SFE_NIST_CRLF_CONVERISON ;
17615 /* Make sure its a NIST file. */
17616 if (strstr (psf_header, "NIST_1A\n 1024\n") != psf_header)
17617 { psf_log_printf (psf, "Not a NIST file.\n") ;
17618 return SFE_NIST_BAD_HEADER ;
17621 /* Determine sample encoding, start by assuming PCM. */
17622 encoding = SF_FORMAT_PCM_U8 ;
17623 if ((cptr = strstr (psf_header, "sample_coding -s")))
17624 { sscanf (cptr, "sample_coding -s%d %63s", &count, str) ;
17626 if (strcmp (str, "pcm") == 0)
17627 encoding = SF_FORMAT_PCM_U8 ;
17628 else if (strcmp (str, "alaw") == 0)
17629 encoding = SF_FORMAT_ALAW ;
17630 else if ((strcmp (str, "ulaw") == 0) || (strcmp (str, "mu-law") == 0))
17631 encoding = SF_FORMAT_ULAW ;
17632 else
17633 { psf_log_printf (psf, "*** Unknown encoding : %s\n", str) ;
17634 encoding = 0 ;
17638 if ((cptr = strstr (psf_header, "channel_count -i ")))
17639 sscanf (cptr, "channel_count -i %d", &(psf->sf.channels)) ;
17641 if ((cptr = strstr (psf_header, "sample_rate -i ")))
17642 sscanf (cptr, "sample_rate -i %d", &(psf->sf.samplerate)) ;
17644 if ((cptr = strstr (psf_header, "sample_count -i ")))
17645 { sscanf (psf_header, "sample_count -i %ld", &samples) ;
17646 psf->sf.frames = samples ;
17649 if ((cptr = strstr (psf_header, "sample_n_bytes -i ")))
17650 sscanf (cptr, "sample_n_bytes -i %d", &(psf->bytewidth)) ;
17652 /* Default endian-ness (for 8 bit, u-law, A-law. */
17653 psf->endian = (CPU_IS_BIG_ENDIAN) ? SF_ENDIAN_BIG : SF_ENDIAN_LITTLE ;
17655 /* This is where we figure out endian-ness. */
17656 if ((cptr = strstr (psf_header, "sample_byte_format -s")))
17657 { sscanf (cptr, "sample_byte_format -s%d %8s", &bytes, str) ;
17658 if (bytes > 1)
17659 { if (psf->bytewidth == 0)
17660 psf->bytewidth = bytes ;
17661 else if (psf->bytewidth != bytes)
17662 { psf_log_printf (psf, "psf->bytewidth (%d) != bytes (%d)\n", psf->bytewidth, bytes) ;
17663 return SFE_NIST_BAD_ENCODING ;
17666 if (strstr (str, "01") == str)
17667 psf->endian = SF_ENDIAN_LITTLE ;
17668 else if (strstr (str, "10"))
17669 psf->endian = SF_ENDIAN_BIG ;
17670 else
17671 { psf_log_printf (psf, "Weird endian-ness : %s\n", str) ;
17672 return SFE_NIST_BAD_ENCODING ;
17676 psf->sf.format |= psf->endian ;
17679 if ((cptr = strstr (psf_header, "sample_sig_bits -i ")))
17680 sscanf (cptr, "sample_sig_bits -i %d", &bitwidth) ;
17682 if (strstr (psf_header, "channels_interleaved -s5 FALSE"))
17683 { psf_log_printf (psf, "Non-interleaved data unsupported.\n", str) ;
17684 return SFE_NIST_BAD_ENCODING ;
17687 psf->dataoffset = NIST_HEADER_LENGTH ;
17688 psf->blockwidth = psf->sf.channels * psf->bytewidth ;
17689 psf->datalength = psf->filelength - psf->dataoffset ;
17691 psf->close = nist_close ;
17693 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
17695 if (encoding == SF_FORMAT_PCM_U8)
17696 { switch (psf->bytewidth)
17697 { case 1 :
17698 psf->sf.format |= SF_FORMAT_PCM_S8 ;
17699 break ;
17701 case 2 :
17702 psf->sf.format |= SF_FORMAT_PCM_16 ;
17703 break ;
17705 case 3 :
17706 psf->sf.format |= SF_FORMAT_PCM_24 ;
17707 break ;
17709 case 4 :
17710 psf->sf.format |= SF_FORMAT_PCM_32 ;
17711 break ;
17713 default : break ;
17716 else if (encoding != 0)
17717 psf->sf.format |= encoding ;
17718 else
17719 return SFE_UNIMPLEMENTED ;
17721 return 0 ;
17722 } /* nist_read_header */
17724 static int
17725 nist_close (SF_PRIVATE *psf)
17727 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
17728 nist_write_header (psf, SF_TRUE) ;
17730 return 0 ;
17731 } /* nist_close */
17733 /*=========================================================================
17736 static int
17737 nist_write_header (SF_PRIVATE *psf, int calc_length)
17738 { const char *end_str ;
17739 long samples ;
17740 sf_count_t current ;
17742 current = psf_ftell (psf) ;
17744 /* Prevent compiler warning. */
17745 calc_length = calc_length ;
17747 if (psf->endian == SF_ENDIAN_BIG)
17748 end_str = "10" ;
17749 else if (psf->endian == SF_ENDIAN_LITTLE)
17750 end_str = "01" ;
17751 else
17752 end_str = "error" ;
17754 /* Clear the whole header. */
17755 memset (psf->header, 0, sizeof (psf->header)) ;
17756 psf->headindex = 0 ;
17758 psf_fseek (psf, 0, SEEK_SET) ;
17760 psf_asciiheader_printf (psf, "NIST_1A\n 1024\n") ;
17761 psf_asciiheader_printf (psf, "channel_count -i %d\n", psf->sf.channels) ;
17762 psf_asciiheader_printf (psf, "sample_rate -i %d\n", psf->sf.samplerate) ;
17764 switch (psf->sf.format & SF_FORMAT_SUBMASK)
17765 { case SF_FORMAT_PCM_S8 :
17766 psf_asciiheader_printf (psf, "sample_coding -s3 pcm\n") ;
17767 psf_asciiheader_printf (psf, "sample_n_bytes -i 1\n"
17768 "sample_sig_bits -i 8\n") ;
17769 break ;
17771 case SF_FORMAT_PCM_16 :
17772 case SF_FORMAT_PCM_24 :
17773 case SF_FORMAT_PCM_32 :
17774 psf_asciiheader_printf (psf, "sample_n_bytes -i %d\n", psf->bytewidth) ;
17775 psf_asciiheader_printf (psf, "sample_sig_bits -i %d\n", psf->bytewidth * 8) ;
17776 psf_asciiheader_printf (psf, "sample_coding -s3 pcm\n"
17777 "sample_byte_format -s%d %s\n", psf->bytewidth, end_str) ;
17778 break ;
17780 case SF_FORMAT_ALAW :
17781 psf_asciiheader_printf (psf, "sample_coding -s4 alaw\n") ;
17782 psf_asciiheader_printf (psf, "sample_n_bytes -s1 1\n") ;
17783 break ;
17785 case SF_FORMAT_ULAW :
17786 psf_asciiheader_printf (psf, "sample_coding -s4 ulaw\n") ;
17787 psf_asciiheader_printf (psf, "sample_n_bytes -s1 1\n") ;
17788 break ;
17790 default : return SFE_UNIMPLEMENTED ;
17793 psf->dataoffset = NIST_HEADER_LENGTH ;
17795 /* Fix this */
17796 samples = psf->sf.frames ;
17797 psf_asciiheader_printf (psf, "sample_count -i %ld\n", samples) ;
17798 psf_asciiheader_printf (psf, "end_head\n") ;
17800 /* Zero fill to dataoffset. */
17801 psf_binheader_writef (psf, "z", (size_t) (NIST_HEADER_LENGTH - psf->headindex)) ;
17803 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
17805 if (psf->error)
17806 return psf->error ;
17808 if (current > 0)
17809 psf_fseek (psf, current, SEEK_SET) ;
17811 return psf->error ;
17812 } /* nist_write_header */
17816 ** Do not edit or modify anything in this comment block.
17817 ** The arch-tag line is a file identity tag for the GNU Arch
17818 ** revision control system.
17820 ** arch-tag: b45ed85d-9e22-4ad9-b78c-4b58b67152a8
17823 ** Copyright (C) 2002-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
17825 ** This program is free software; you can redistribute it and/or modify
17826 ** it under the terms of the GNU Lesser General Public License as published by
17827 ** the Free Software Foundation; either version 2.1 of the License, or
17828 ** (at your option) any later version.
17830 ** This program is distributed in the hope that it will be useful,
17831 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17832 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17833 ** GNU Lesser General Public License for more details.
17835 ** You should have received a copy of the GNU Lesser General Public License
17836 ** along with this program; if not, write to the Free Software
17837 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17840 #include <stdio.h>
17841 #include <fcntl.h>
17842 #include <string.h>
17843 #include <ctype.h>
17846 #if (ENABLE_EXPERIMENTAL_CODE == 0)
17849 ogg_open (SF_PRIVATE *psf)
17850 { if (psf)
17851 return SFE_UNIMPLEMENTED ;
17852 return (psf && 0) ;
17853 } /* ogg_open */
17855 #else
17857 #define SFE_OGG_NOT_OGG 666
17859 /*------------------------------------------------------------------------------
17860 ** Macros to handle big/little endian issues.
17863 #define ALAW_MARKER MAKE_MARKER ('A', 'L', 'a', 'w')
17864 #define SOUN_MARKER MAKE_MARKER ('S', 'o', 'u', 'n')
17865 #define DFIL_MARKER MAKE_MARKER ('d', 'F', 'i', 'l')
17867 /*------------------------------------------------------------------------------
17868 ** Private static functions.
17871 static int ogg_read_header (SF_PRIVATE *psf) ;
17873 /*------------------------------------------------------------------------------
17874 ** Public function.
17878 ogg_open (SF_PRIVATE *psf)
17879 { int subformat, error = 0 ;
17881 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
17882 return SFE_UNIMPLEMENTED ;
17884 if ((error = ogg_read_header (psf)))
17885 return error ;
17887 if ((psf->sf.format & SF_FORMAT_TYPEMASK) != SF_FORMAT_OGG)
17888 return SFE_BAD_OPEN_FORMAT ;
17890 subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
17892 return error ;
17893 } /* ogg_open */
17895 /*------------------------------------------------------------------------------
17898 static int
17899 ogg_read_header (SF_PRIVATE *psf)
17900 { int marker ;
17902 /* Set position to start of file to begin reading header. */
17903 psf_binheader_readf (psf, "pm", 0, &marker) ;
17904 if (marker != ALAW_MARKER)
17905 return SFE_OGG_NOT_OGG ;
17907 psf_binheader_readf (psf, "m", &marker) ;
17908 if (marker != SOUN_MARKER)
17909 return SFE_OGG_NOT_OGG ;
17911 psf_binheader_readf (psf, "m", &marker) ;
17912 if (marker != DFIL_MARKER)
17913 return SFE_OGG_NOT_OGG ;
17915 psf_log_printf (psf, "Read only : Psion Palmtop Alaw (.wve)\n"
17916 " Sample Rate : 8000\n"
17917 " Channels : 1\n"
17918 " Encoding : A-law\n") ;
17920 psf->dataoffset = 0x20 ;
17921 psf->datalength = psf->filelength - psf->dataoffset ;
17923 psf->sf.format = SF_FORMAT_OGG | SF_FORMAT_ALAW ;
17924 psf->sf.samplerate = 8000 ;
17925 psf->sf.frames = psf->datalength ;
17926 psf->sf.channels = 1 ;
17928 return alaw_init (psf) ;
17929 } /* ogg_read_header */
17931 /*------------------------------------------------------------------------------
17934 #endif
17936 ** Do not edit or modify anything in this comment block.
17937 ** The arch-tag line is a file identity tag for the GNU Arch
17938 ** revision control system.
17940 ** arch-tag: 9ff1fe9c-629e-4e9c-9ef5-3d0eb1e427a0
17943 ** Copyright (C) 1999-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
17945 ** This program is free software; you can redistribute it and/or modify
17946 ** it under the terms of the GNU Lesser General Public License as published by
17947 ** the Free Software Foundation; either version 2.1 of the License, or
17948 ** (at your option) any later version.
17950 ** This program is distributed in the hope that it will be useful,
17951 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17952 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17953 ** GNU Lesser General Public License for more details.
17955 ** You should have received a copy of the GNU Lesser General Public License
17956 ** along with this program; if not, write to the Free Software
17957 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17961 #include <stdio.h>
17962 #include <stdlib.h>
17963 #include <fcntl.h>
17964 #include <string.h>
17965 #include <ctype.h>
17968 /*------------------------------------------------------------------------------
17969 ** Macros to handle big/little endian issues.
17972 #define FAP_MARKER (MAKE_MARKER ('f', 'a', 'p', ' '))
17973 #define PAF_MARKER (MAKE_MARKER (' ', 'p', 'a', 'f'))
17975 /*------------------------------------------------------------------------------
17976 ** Other defines.
17979 #define PAF_HEADER_LENGTH 2048
17981 #define PAF24_SAMPLES_PER_BLOCK 10
17982 #define PAF24_BLOCK_SIZE 32
17984 /*------------------------------------------------------------------------------
17985 ** Typedefs.
17988 typedef struct
17989 { int version ;
17990 int endianness ;
17991 int samplerate ;
17992 int format ;
17993 int channels ;
17994 int source ;
17995 } PAF_FMT ;
17997 typedef struct
17998 { int max_blocks, channels, samplesperblock, blocksize ;
17999 int read_block, write_block, read_count, write_count ;
18000 sf_count_t sample_count ;
18001 int *samples ;
18002 unsigned char *block ;
18003 #if HAVE_FLEXIBLE_ARRAY
18004 int data [] ; /* ISO C99 struct flexible array. */
18005 #else
18006 int data [1] ; /* This is a hack and may not work. */
18007 #endif
18008 } PAF24_PRIVATE ;
18010 /*------------------------------------------------------------------------------
18011 ** Private static functions.
18014 static int paf24_init (SF_PRIVATE *psf) ;
18016 static int paf_read_header (SF_PRIVATE *psf) ;
18017 static int paf_write_header (SF_PRIVATE *psf, int calc_length) ;
18019 static sf_count_t paf24_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
18020 static sf_count_t paf24_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
18021 static sf_count_t paf24_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
18022 static sf_count_t paf24_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
18024 static sf_count_t paf24_write_s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
18025 static sf_count_t paf24_write_i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
18026 static sf_count_t paf24_write_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
18027 static sf_count_t paf24_write_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
18029 static sf_count_t paf24_seek (SF_PRIVATE *psf, int mode, sf_count_t offset) ;
18031 enum
18032 { PAF_PCM_16 = 0,
18033 PAF_PCM_24 = 1,
18034 PAF_PCM_S8 = 2
18037 /*------------------------------------------------------------------------------
18038 ** Public function.
18042 paf_open (SF_PRIVATE *psf)
18043 { int subformat, error, endian ;
18045 psf->dataoffset = PAF_HEADER_LENGTH ;
18047 if (psf->mode == SFM_READ || (psf->mode == SFM_RDWR && psf->filelength > 0))
18048 { if ((error = paf_read_header (psf)))
18049 return error ;
18052 subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
18054 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
18055 { if ((psf->sf.format & SF_FORMAT_TYPEMASK) != SF_FORMAT_PAF)
18056 return SFE_BAD_OPEN_FORMAT ;
18058 endian = psf->sf.format & SF_FORMAT_ENDMASK ;
18060 /* PAF is by default big endian. */
18061 psf->endian = SF_ENDIAN_BIG ;
18063 if (endian == SF_ENDIAN_LITTLE || (CPU_IS_LITTLE_ENDIAN && (endian == SF_ENDIAN_CPU)))
18064 psf->endian = SF_ENDIAN_LITTLE ;
18066 if ((error = paf_write_header (psf, SF_FALSE)))
18067 return error ;
18069 psf->write_header = paf_write_header ;
18072 switch (subformat)
18073 { case SF_FORMAT_PCM_S8 :
18074 psf->bytewidth = 1 ;
18075 error = pcm_init (psf) ;
18076 break ;
18078 case SF_FORMAT_PCM_16 :
18079 psf->bytewidth = 2 ;
18080 error = pcm_init (psf) ;
18081 break ;
18083 case SF_FORMAT_PCM_24 :
18084 /* No bytewidth because of whacky 24 bit encoding. */
18085 error = paf24_init (psf) ;
18086 break ;
18088 default : return SFE_PAF_UNKNOWN_FORMAT ;
18091 return error ;
18092 } /* paf_open */
18094 /*------------------------------------------------------------------------------
18097 static int
18098 paf_read_header (SF_PRIVATE *psf)
18099 { PAF_FMT paf_fmt ;
18100 int marker ;
18102 psf_binheader_readf (psf, "pm", 0, &marker) ;
18104 psf_log_printf (psf, "Signature : '%M'\n", marker) ;
18106 if (marker == PAF_MARKER)
18107 { psf_binheader_readf (psf, "E444444", &(paf_fmt.version), &(paf_fmt.endianness),
18108 &(paf_fmt.samplerate), &(paf_fmt.format), &(paf_fmt.channels), &(paf_fmt.source)) ;
18110 else if (marker == FAP_MARKER)
18111 { psf_binheader_readf (psf, "e444444", &(paf_fmt.version), &(paf_fmt.endianness),
18112 &(paf_fmt.samplerate), &(paf_fmt.format), &(paf_fmt.channels), &(paf_fmt.source)) ;
18114 else
18115 return SFE_PAF_NO_MARKER ;
18117 psf_log_printf (psf, "Version : %d\n", paf_fmt.version) ;
18119 if (paf_fmt.version != 0)
18120 { psf_log_printf (psf, "*** Bad version number. should be zero.\n") ;
18121 return SFE_PAF_VERSION ;
18124 psf_log_printf (psf, "Sample Rate : %d\n", paf_fmt.samplerate) ;
18125 psf_log_printf (psf, "Channels : %d\n", paf_fmt.channels) ;
18127 psf_log_printf (psf, "Endianness : %d => ", paf_fmt.endianness) ;
18128 if (paf_fmt.endianness)
18129 { psf_log_printf (psf, "Little\n", paf_fmt.endianness) ;
18130 psf->endian = SF_ENDIAN_LITTLE ;
18132 else
18133 { psf_log_printf (psf, "Big\n", paf_fmt.endianness) ;
18134 psf->endian = SF_ENDIAN_BIG ;
18137 if (psf->filelength < PAF_HEADER_LENGTH)
18138 return SFE_PAF_SHORT_HEADER ;
18140 psf->datalength = psf->filelength - psf->dataoffset ;
18142 psf_binheader_readf (psf, "p", (int) psf->dataoffset) ;
18144 psf->sf.samplerate = paf_fmt.samplerate ;
18145 psf->sf.channels = paf_fmt.channels ;
18147 /* Only fill in type major. */
18148 psf->sf.format = SF_FORMAT_PAF ;
18150 psf_log_printf (psf, "Format : %d => ", paf_fmt.format) ;
18152 /* PAF is by default big endian. */
18153 psf->sf.format |= paf_fmt.endianness ? SF_ENDIAN_LITTLE : SF_ENDIAN_BIG ;
18155 switch (paf_fmt.format)
18156 { case PAF_PCM_S8 :
18157 psf_log_printf (psf, "8 bit linear PCM\n") ;
18158 psf->bytewidth = 1 ;
18160 psf->sf.format |= SF_FORMAT_PCM_S8 ;
18162 psf->blockwidth = psf->bytewidth * psf->sf.channels ;
18163 psf->sf.frames = psf->datalength / psf->blockwidth ;
18164 break ;
18166 case PAF_PCM_16 :
18167 psf_log_printf (psf, "16 bit linear PCM\n") ;
18168 psf->bytewidth = 2 ;
18170 psf->sf.format |= SF_FORMAT_PCM_16 ;
18172 psf->blockwidth = psf->bytewidth * psf->sf.channels ;
18173 psf->sf.frames = psf->datalength / psf->blockwidth ;
18174 break ;
18176 case PAF_PCM_24 :
18177 psf_log_printf (psf, "24 bit linear PCM\n") ;
18178 psf->bytewidth = 3 ;
18180 psf->sf.format |= SF_FORMAT_PCM_24 ;
18182 psf->blockwidth = 0 ;
18183 psf->sf.frames = PAF24_SAMPLES_PER_BLOCK * psf->datalength /
18184 (PAF24_BLOCK_SIZE * psf->sf.channels) ;
18185 break ;
18187 default : psf_log_printf (psf, "Unknown\n") ;
18188 return SFE_PAF_UNKNOWN_FORMAT ;
18189 break ;
18192 psf_log_printf (psf, "Source : %d => ", paf_fmt.source) ;
18194 switch (paf_fmt.source)
18195 { case 1 : psf_log_printf (psf, "Analog Recording\n") ;
18196 break ;
18197 case 2 : psf_log_printf (psf, "Digital Transfer\n") ;
18198 break ;
18199 case 3 : psf_log_printf (psf, "Multi-track Mixdown\n") ;
18200 break ;
18201 case 5 : psf_log_printf (psf, "Audio Resulting From DSP Processing\n") ;
18202 break ;
18203 default : psf_log_printf (psf, "Unknown\n") ;
18204 break ;
18207 return 0 ;
18208 } /* paf_read_header */
18210 static int
18211 paf_write_header (SF_PRIVATE *psf, int calc_length)
18212 { int paf_format ;
18214 /* PAF header already written so no need to re-write. */
18215 if (psf_ftell (psf) >= PAF_HEADER_LENGTH)
18216 return 0 ;
18218 psf->dataoffset = PAF_HEADER_LENGTH ;
18220 psf->dataoffset = PAF_HEADER_LENGTH ;
18222 /* Prevent compiler warning. */
18223 calc_length = calc_length ;
18225 switch (psf->sf.format & SF_FORMAT_SUBMASK)
18226 { case SF_FORMAT_PCM_S8 :
18227 paf_format = PAF_PCM_S8 ;
18228 break ;
18230 case SF_FORMAT_PCM_16 :
18231 paf_format = PAF_PCM_16 ;
18232 break ;
18234 case SF_FORMAT_PCM_24 :
18235 paf_format = PAF_PCM_24 ;
18236 break ;
18238 default : return SFE_PAF_UNKNOWN_FORMAT ;
18241 /* Reset the current header length to zero. */
18242 psf->header [0] = 0 ;
18243 psf->headindex = 0 ;
18245 if (psf->endian == SF_ENDIAN_BIG)
18246 { /* Marker, version, endianness, samplerate */
18247 psf_binheader_writef (psf, "Em444", PAF_MARKER, 0, 0, psf->sf.samplerate) ;
18248 /* format, channels, source */
18249 psf_binheader_writef (psf, "E444", paf_format, psf->sf.channels, 0) ;
18251 else if (psf->endian == SF_ENDIAN_LITTLE)
18252 { /* Marker, version, endianness, samplerate */
18253 psf_binheader_writef (psf, "em444", FAP_MARKER, 0, 1, psf->sf.samplerate) ;
18254 /* format, channels, source */
18255 psf_binheader_writef (psf, "e444", paf_format, psf->sf.channels, 0) ;
18258 /* Zero fill to dataoffset. */
18259 psf_binheader_writef (psf, "z", (size_t) (psf->dataoffset - psf->headindex)) ;
18261 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
18263 return psf->error ;
18264 } /* paf_write_header */
18266 /*===============================================================================
18267 ** 24 bit PAF files have a really weird encoding.
18268 ** For a mono file, 10 samples (each being 3 bytes) are packed into a 32 byte
18269 ** block. The 8 ints in this 32 byte block are then endian swapped (as ints)
18270 ** if necessary before being written to disk.
18271 ** For a stereo file, blocks of 10 samples from the same channel are encoded
18272 ** into 32 bytes as for the mono case. The 32 byte blocks are then interleaved
18273 ** on disk.
18274 ** Reading has to reverse the above process :-).
18275 ** Weird!!!
18277 ** The code below attempts to gain efficiency while maintaining readability.
18280 static int paf24_read_block (SF_PRIVATE *psf, PAF24_PRIVATE *ppaf24) ;
18281 static int paf24_write_block (SF_PRIVATE *psf, PAF24_PRIVATE *ppaf24) ;
18282 static int paf24_close (SF_PRIVATE *psf) ;
18285 static int
18286 paf24_init (SF_PRIVATE *psf)
18287 { PAF24_PRIVATE *ppaf24 ;
18288 int paf24size, max_blocks ;
18290 paf24size = sizeof (PAF24_PRIVATE) + psf->sf.channels *
18291 (PAF24_BLOCK_SIZE + PAF24_SAMPLES_PER_BLOCK * sizeof (int)) ;
18294 ** Not exatly sure why this needs to be here but the tests
18295 ** fail without it.
18297 psf->last_op = 0 ;
18299 if (! (psf->fdata = malloc (paf24size)))
18300 return SFE_MALLOC_FAILED ;
18302 ppaf24 = (PAF24_PRIVATE*) psf->fdata ;
18303 memset (ppaf24, 0, paf24size) ;
18305 ppaf24->channels = psf->sf.channels ;
18306 ppaf24->samples = ppaf24->data ;
18307 ppaf24->block = (unsigned char*) (ppaf24->data + PAF24_SAMPLES_PER_BLOCK * ppaf24->channels) ;
18309 ppaf24->blocksize = PAF24_BLOCK_SIZE * ppaf24->channels ;
18310 ppaf24->samplesperblock = PAF24_SAMPLES_PER_BLOCK ;
18312 if (psf->mode == SFM_READ || psf->mode == SFM_RDWR)
18313 { paf24_read_block (psf, ppaf24) ; /* Read first block. */
18315 psf->read_short = paf24_read_s ;
18316 psf->read_int = paf24_read_i ;
18317 psf->read_float = paf24_read_f ;
18318 psf->read_double = paf24_read_d ;
18321 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
18322 { psf->write_short = paf24_write_s ;
18323 psf->write_int = paf24_write_i ;
18324 psf->write_float = paf24_write_f ;
18325 psf->write_double = paf24_write_d ;
18328 psf->seek = paf24_seek ;
18329 psf->close = paf24_close ;
18331 psf->filelength = psf_get_filelen (psf) ;
18332 psf->datalength = psf->filelength - psf->dataoffset ;
18334 if (psf->datalength % PAF24_BLOCK_SIZE)
18335 { if (psf->mode == SFM_READ)
18336 psf_log_printf (psf, "*** Warning : file seems to be truncated.\n") ;
18337 max_blocks = psf->datalength / ppaf24->blocksize + 1 ;
18339 else
18340 max_blocks = psf->datalength / ppaf24->blocksize ;
18342 ppaf24->read_block = 0 ;
18343 if (psf->mode == SFM_RDWR)
18344 ppaf24->write_block = max_blocks ;
18345 else
18346 ppaf24->write_block = 0 ;
18348 psf->sf.frames = ppaf24->samplesperblock * max_blocks ;
18349 ppaf24->sample_count = psf->sf.frames ;
18351 return 0 ;
18352 } /* paf24_init */
18354 static sf_count_t
18355 paf24_seek (SF_PRIVATE *psf, int mode, sf_count_t offset)
18356 { PAF24_PRIVATE *ppaf24 ;
18357 int newblock, newsample ;
18359 if (psf->fdata == NULL)
18360 { psf->error = SFE_INTERNAL ;
18361 return SF_SEEK_ERROR ;
18364 ppaf24 = (PAF24_PRIVATE*) psf->fdata ;
18366 if (mode == SFM_READ && ppaf24->write_count > 0)
18367 paf24_write_block (psf, ppaf24) ;
18369 newblock = offset / ppaf24->samplesperblock ;
18370 newsample = offset % ppaf24->samplesperblock ;
18372 switch (mode)
18373 { case SFM_READ :
18374 if (offset > ppaf24->read_block * ppaf24->samplesperblock + ppaf24->read_count)
18375 { psf->error = SFE_BAD_SEEK ;
18376 return SF_SEEK_ERROR ;
18379 if (psf->last_op == SFM_WRITE && ppaf24->write_count)
18380 paf24_write_block (psf, ppaf24) ;
18382 psf_fseek (psf, psf->dataoffset + newblock * ppaf24->blocksize, SEEK_SET) ;
18383 ppaf24->read_block = newblock ;
18384 paf24_read_block (psf, ppaf24) ;
18385 ppaf24->read_count = newsample ;
18386 break ;
18388 case SFM_WRITE :
18389 if (offset > ppaf24->sample_count)
18390 { psf->error = SFE_BAD_SEEK ;
18391 return SF_SEEK_ERROR ;
18394 if (psf->last_op == SFM_WRITE && ppaf24->write_count)
18395 paf24_write_block (psf, ppaf24) ;
18397 psf_fseek (psf, psf->dataoffset + newblock * ppaf24->blocksize, SEEK_SET) ;
18398 ppaf24->write_block = newblock ;
18399 paf24_read_block (psf, ppaf24) ;
18400 ppaf24->write_count = newsample ;
18401 break ;
18403 default :
18404 psf->error = SFE_BAD_SEEK ;
18405 return SF_SEEK_ERROR ;
18408 return newblock * ppaf24->samplesperblock + newsample ;
18409 } /* paf24_seek */
18411 static int
18412 paf24_close (SF_PRIVATE *psf)
18413 { PAF24_PRIVATE *ppaf24 ;
18415 if (psf->fdata == NULL)
18416 return 0 ;
18418 ppaf24 = (PAF24_PRIVATE*) psf->fdata ;
18420 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
18421 { if (ppaf24->write_count > 0)
18422 paf24_write_block (psf, ppaf24) ;
18425 return 0 ;
18426 } /* paf24_close */
18428 /*---------------------------------------------------------------------------
18430 static int
18431 paf24_read_block (SF_PRIVATE *psf, PAF24_PRIVATE *ppaf24)
18432 { int k, channel ;
18433 unsigned char *cptr ;
18435 ppaf24->read_block ++ ;
18436 ppaf24->read_count = 0 ;
18438 if (ppaf24->read_block * ppaf24->samplesperblock > ppaf24->sample_count)
18439 { memset (ppaf24->samples, 0, ppaf24->samplesperblock * ppaf24->channels) ;
18440 return 1 ;
18443 /* Read the block. */
18444 if ((k = psf_fread (ppaf24->block, 1, ppaf24->blocksize, psf)) != ppaf24->blocksize)
18445 psf_log_printf (psf, "*** Warning : short read (%d != %d).\n", k, ppaf24->blocksize) ;
18448 if (CPU_IS_LITTLE_ENDIAN)
18449 { /* Do endian swapping if necessary. */
18450 if (psf->endian == SF_ENDIAN_BIG)
18451 endswap_int_array (ppaf24->data, 8 * ppaf24->channels) ;
18453 /* Unpack block. */
18454 for (k = 0 ; k < PAF24_SAMPLES_PER_BLOCK * ppaf24->channels ; k++)
18455 { channel = k % ppaf24->channels ;
18456 cptr = ppaf24->block + PAF24_BLOCK_SIZE * channel + 3 * (k / ppaf24->channels) ;
18457 ppaf24->samples [k] = (cptr [0] << 8) | (cptr [1] << 16) | (cptr [2] << 24) ;
18460 else
18461 { /* Do endian swapping if necessary. */
18462 if (psf->endian == SF_ENDIAN_BIG)
18463 endswap_int_array (ppaf24->data, 8 * ppaf24->channels) ;
18465 /* Unpack block. */
18466 for (k = 0 ; k < PAF24_SAMPLES_PER_BLOCK * ppaf24->channels ; k++)
18467 { channel = k % ppaf24->channels ;
18468 cptr = ppaf24->block + PAF24_BLOCK_SIZE * channel + 3 * (k / ppaf24->channels) ;
18469 ppaf24->samples [k] = (cptr [0] << 8) | (cptr [1] << 16) | (cptr [2] << 24) ;
18473 return 1 ;
18474 } /* paf24_read_block */
18476 static int
18477 paf24_read (SF_PRIVATE *psf, PAF24_PRIVATE *ppaf24, int *ptr, int len)
18478 { int count, total = 0 ;
18480 while (total < len)
18481 { if (ppaf24->read_block * ppaf24->samplesperblock >= ppaf24->sample_count)
18482 { memset (&(ptr [total]), 0, (len - total) * sizeof (int)) ;
18483 return total ;
18486 if (ppaf24->read_count >= ppaf24->samplesperblock)
18487 paf24_read_block (psf, ppaf24) ;
18489 count = (ppaf24->samplesperblock - ppaf24->read_count) * ppaf24->channels ;
18490 count = (len - total > count) ? count : len - total ;
18492 memcpy (&(ptr [total]), &(ppaf24->samples [ppaf24->read_count * ppaf24->channels]), count * sizeof (int)) ;
18493 total += count ;
18494 ppaf24->read_count += count / ppaf24->channels ;
18497 return total ;
18498 } /* paf24_read */
18500 static sf_count_t
18501 paf24_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
18502 { PAF24_PRIVATE *ppaf24 ;
18503 int *iptr ;
18504 int k, bufferlen, readcount, count ;
18505 sf_count_t total = 0 ;
18507 if (psf->fdata == NULL)
18508 return 0 ;
18509 ppaf24 = (PAF24_PRIVATE*) psf->fdata ;
18511 iptr = (int*) psf->buffer ;
18512 bufferlen = sizeof (psf->buffer) / sizeof (int) ;
18513 while (len > 0)
18514 { readcount = (len >= bufferlen) ? bufferlen : len ;
18515 count = paf24_read (psf, ppaf24, iptr, readcount) ;
18516 for (k = 0 ; k < readcount ; k++)
18517 ptr [total + k] = iptr [k] >> 16 ;
18518 total += count ;
18519 len -= readcount ;
18521 return total ;
18522 } /* paf24_read_s */
18524 static sf_count_t
18525 paf24_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
18526 { PAF24_PRIVATE *ppaf24 ;
18527 int total ;
18529 if (psf->fdata == NULL)
18530 return 0 ;
18531 ppaf24 = (PAF24_PRIVATE*) psf->fdata ;
18533 total = paf24_read (psf, ppaf24, ptr, len) ;
18535 return total ;
18536 } /* paf24_read_i */
18538 static sf_count_t
18539 paf24_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
18540 { PAF24_PRIVATE *ppaf24 ;
18541 int *iptr ;
18542 int k, bufferlen, readcount, count ;
18543 sf_count_t total = 0 ;
18544 float normfact ;
18546 if (psf->fdata == NULL)
18547 return 0 ;
18548 ppaf24 = (PAF24_PRIVATE*) psf->fdata ;
18550 normfact = (psf->norm_float == SF_TRUE) ? (1.0 / 0x80000000) : (1.0 / 0x100) ;
18552 iptr = (int*) psf->buffer ;
18553 bufferlen = sizeof (psf->buffer) / sizeof (int) ;
18554 while (len > 0)
18555 { readcount = (len >= bufferlen) ? bufferlen : len ;
18556 count = paf24_read (psf, ppaf24, iptr, readcount) ;
18557 for (k = 0 ; k < readcount ; k++)
18558 ptr [total + k] = normfact * iptr [k] ;
18559 total += count ;
18560 len -= readcount ;
18562 return total ;
18563 } /* paf24_read_f */
18565 static sf_count_t
18566 paf24_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
18567 { PAF24_PRIVATE *ppaf24 ;
18568 int *iptr ;
18569 int k, bufferlen, readcount, count ;
18570 sf_count_t total = 0 ;
18571 double normfact ;
18573 if (psf->fdata == NULL)
18574 return 0 ;
18575 ppaf24 = (PAF24_PRIVATE*) psf->fdata ;
18577 normfact = (psf->norm_double == SF_TRUE) ? (1.0 / 0x80000000) : (1.0 / 0x100) ;
18579 iptr = (int*) psf->buffer ;
18580 bufferlen = sizeof (psf->buffer) / sizeof (int) ;
18581 while (len > 0)
18582 { readcount = (len >= bufferlen) ? bufferlen : len ;
18583 count = paf24_read (psf, ppaf24, iptr, readcount) ;
18584 for (k = 0 ; k < readcount ; k++)
18585 ptr [total + k] = normfact * iptr [k] ;
18586 total += count ;
18587 len -= readcount ;
18589 return total ;
18590 } /* paf24_read_d */
18592 /*---------------------------------------------------------------------------
18595 static int
18596 paf24_write_block (SF_PRIVATE *psf, PAF24_PRIVATE *ppaf24)
18597 { int k, nextsample, channel ;
18598 unsigned char *cptr ;
18600 /* First pack block. */
18602 if (CPU_IS_LITTLE_ENDIAN)
18603 { for (k = 0 ; k < PAF24_SAMPLES_PER_BLOCK * ppaf24->channels ; k++)
18604 { channel = k % ppaf24->channels ;
18605 cptr = ppaf24->block + PAF24_BLOCK_SIZE * channel + 3 * (k / ppaf24->channels) ;
18606 nextsample = ppaf24->samples [k] >> 8 ;
18607 cptr [0] = nextsample ;
18608 cptr [1] = nextsample >> 8 ;
18609 cptr [2] = nextsample >> 16 ;
18612 /* Do endian swapping if necessary. */
18613 if (psf->endian == SF_ENDIAN_BIG)
18614 endswap_int_array (ppaf24->data, 8 * ppaf24->channels) ;
18616 else if (CPU_IS_BIG_ENDIAN)
18617 { /* This is correct. */
18618 for (k = 0 ; k < PAF24_SAMPLES_PER_BLOCK * ppaf24->channels ; k++)
18619 { channel = k % ppaf24->channels ;
18620 cptr = ppaf24->block + PAF24_BLOCK_SIZE * channel + 3 * (k / ppaf24->channels) ;
18621 nextsample = ppaf24->samples [k] >> 8 ;
18622 cptr [0] = nextsample ;
18623 cptr [1] = nextsample >> 8 ;
18624 cptr [2] = nextsample >> 16 ;
18626 if (psf->endian == SF_ENDIAN_BIG)
18627 endswap_int_array (ppaf24->data, 8 * ppaf24->channels) ;
18630 /* Write block to disk. */
18631 if ((k = psf_fwrite (ppaf24->block, 1, ppaf24->blocksize, psf)) != ppaf24->blocksize)
18632 psf_log_printf (psf, "*** Warning : short write (%d != %d).\n", k, ppaf24->blocksize) ;
18634 if (ppaf24->sample_count < ppaf24->write_block * ppaf24->samplesperblock + ppaf24->write_count)
18635 ppaf24->sample_count = ppaf24->write_block * ppaf24->samplesperblock + ppaf24->write_count ;
18637 if (ppaf24->write_count == ppaf24->samplesperblock)
18638 { ppaf24->write_block ++ ;
18639 ppaf24->write_count = 0 ;
18642 return 1 ;
18643 } /* paf24_write_block */
18645 static int
18646 paf24_write (SF_PRIVATE *psf, PAF24_PRIVATE *ppaf24, int *ptr, int len)
18647 { int count, total = 0 ;
18649 while (total < len)
18650 { count = (ppaf24->samplesperblock - ppaf24->write_count) * ppaf24->channels ;
18652 if (count > len - total)
18653 count = len - total ;
18655 memcpy (&(ppaf24->samples [ppaf24->write_count * ppaf24->channels]), &(ptr [total]), count * sizeof (int)) ;
18656 total += count ;
18657 ppaf24->write_count += count / ppaf24->channels ;
18659 if (ppaf24->write_count >= ppaf24->samplesperblock)
18660 paf24_write_block (psf, ppaf24) ;
18663 return total ;
18664 } /* paf24_write */
18666 static sf_count_t
18667 paf24_write_s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
18668 { PAF24_PRIVATE *ppaf24 ;
18669 int *iptr ;
18670 int k, bufferlen, writecount = 0, count ;
18671 sf_count_t total = 0 ;
18673 if (psf->fdata == NULL)
18674 return 0 ;
18675 ppaf24 = (PAF24_PRIVATE*) psf->fdata ;
18677 iptr = (int*) psf->buffer ;
18678 bufferlen = sizeof (psf->buffer) / sizeof (int) ;
18679 while (len > 0)
18680 { writecount = (len >= bufferlen) ? bufferlen : len ;
18681 for (k = 0 ; k < writecount ; k++)
18682 iptr [k] = ptr [total + k] << 16 ;
18683 count = paf24_write (psf, ppaf24, iptr, writecount) ;
18684 total += count ;
18685 len -= writecount ;
18686 if (count != writecount)
18687 break ;
18689 return total ;
18690 } /* paf24_write_s */
18692 static sf_count_t
18693 paf24_write_i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
18694 { PAF24_PRIVATE *ppaf24 ;
18695 int writecount, count ;
18696 sf_count_t total = 0 ;
18698 if (psf->fdata == NULL)
18699 return 0 ;
18700 ppaf24 = (PAF24_PRIVATE*) psf->fdata ;
18702 while (len > 0)
18703 { writecount = (len > 0x10000000) ? 0x10000000 : (int) len ;
18705 count = paf24_write (psf, ppaf24, ptr, writecount) ;
18707 total += count ;
18708 len -= count ;
18709 if (count != writecount)
18710 break ;
18713 return total ;
18714 } /* paf24_write_i */
18716 static sf_count_t
18717 paf24_write_f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
18718 { PAF24_PRIVATE *ppaf24 ;
18719 int *iptr ;
18720 int k, bufferlen, writecount = 0, count ;
18721 sf_count_t total = 0 ;
18722 float normfact ;
18724 if (psf->fdata == NULL)
18725 return 0 ;
18726 ppaf24 = (PAF24_PRIVATE*) psf->fdata ;
18728 normfact = (psf->norm_float == SF_TRUE) ? (1.0 * 0x7FFFFFFF) : (1.0 / 0x100) ;
18730 iptr = (int*) psf->buffer ;
18731 bufferlen = sizeof (psf->buffer) / sizeof (int) ;
18732 while (len > 0)
18733 { writecount = (len >= bufferlen) ? bufferlen : len ;
18734 for (k = 0 ; k < writecount ; k++)
18735 iptr [k] = lrintf (normfact * ptr [total + k]) ;
18736 count = paf24_write (psf, ppaf24, iptr, writecount) ;
18737 total += count ;
18738 len -= writecount ;
18739 if (count != writecount)
18740 break ;
18743 return total ;
18744 } /* paf24_write_f */
18746 static sf_count_t
18747 paf24_write_d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
18748 { PAF24_PRIVATE *ppaf24 ;
18749 int *iptr ;
18750 int k, bufferlen, writecount = 0, count ;
18751 sf_count_t total = 0 ;
18752 double normfact ;
18754 if (psf->fdata == NULL)
18755 return 0 ;
18756 ppaf24 = (PAF24_PRIVATE*) psf->fdata ;
18758 normfact = (psf->norm_double == SF_TRUE) ? (1.0 * 0x7FFFFFFF) : (1.0 / 0x100) ;
18760 iptr = (int*) psf->buffer ;
18761 bufferlen = sizeof (psf->buffer) / sizeof (int) ;
18762 while (len > 0)
18763 { writecount = (len >= bufferlen) ? bufferlen : len ;
18764 for (k = 0 ; k < writecount ; k++)
18765 iptr [k] = lrint (normfact * ptr [total+k]) ;
18766 count = paf24_write (psf, ppaf24, iptr, writecount) ;
18767 total += count ;
18768 len -= writecount ;
18769 if (count != writecount)
18770 break ;
18773 return total ;
18774 } /* paf24_write_d */
18778 ** Do not edit or modify anything in this comment block.
18779 ** The arch-tag line is a file identity tag for the GNU Arch
18780 ** revision control system.
18782 ** arch-tag: 477a5308-451e-4bbd-bab4-fab6caa4e884
18785 ** Copyright (C) 1999-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
18787 ** This program is free software; you can redistribute it and/or modify
18788 ** it under the terms of the GNU Lesser General Public License as published by
18789 ** the Free Software Foundation; either version 2.1 of the License, or
18790 ** (at your option) any later version.
18792 ** This program is distributed in the hope that it will be useful,
18793 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18794 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18795 ** GNU Lesser General Public License for more details.
18797 ** You should have received a copy of the GNU Lesser General Public License
18798 ** along with this program; if not, write to the Free Software
18799 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18803 /* Need to be able to handle 3 byte (24 bit) integers. So defined a
18804 ** type and use SIZEOF_TRIBYTE instead of (tribyte).
18807 typedef void tribyte ;
18809 #define SIZEOF_TRIBYTE 3
18811 static sf_count_t pcm_read_sc2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
18812 static sf_count_t pcm_read_uc2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
18813 static sf_count_t pcm_read_bes2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
18814 static sf_count_t pcm_read_les2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
18815 static sf_count_t pcm_read_bet2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
18816 static sf_count_t pcm_read_let2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
18817 static sf_count_t pcm_read_bei2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
18818 static sf_count_t pcm_read_lei2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
18820 static sf_count_t pcm_read_sc2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
18821 static sf_count_t pcm_read_uc2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
18822 static sf_count_t pcm_read_bes2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
18823 static sf_count_t pcm_read_les2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
18824 static sf_count_t pcm_read_bet2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
18825 static sf_count_t pcm_read_let2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
18826 static sf_count_t pcm_read_bei2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
18827 static sf_count_t pcm_read_lei2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
18829 static sf_count_t pcm_read_sc2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
18830 static sf_count_t pcm_read_uc2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
18831 static sf_count_t pcm_read_bes2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
18832 static sf_count_t pcm_read_les2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
18833 static sf_count_t pcm_read_bet2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
18834 static sf_count_t pcm_read_let2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
18835 static sf_count_t pcm_read_bei2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
18836 static sf_count_t pcm_read_lei2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
18838 static sf_count_t pcm_read_sc2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
18839 static sf_count_t pcm_read_uc2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
18840 static sf_count_t pcm_read_bes2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
18841 static sf_count_t pcm_read_les2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
18842 static sf_count_t pcm_read_bet2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
18843 static sf_count_t pcm_read_let2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
18844 static sf_count_t pcm_read_bei2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
18845 static sf_count_t pcm_read_lei2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
18848 static sf_count_t pcm_write_s2sc (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
18849 static sf_count_t pcm_write_s2uc (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
18850 static sf_count_t pcm_write_s2bes (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
18851 static sf_count_t pcm_write_s2les (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
18852 static sf_count_t pcm_write_s2bet (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
18853 static sf_count_t pcm_write_s2let (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
18854 static sf_count_t pcm_write_s2bei (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
18855 static sf_count_t pcm_write_s2lei (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
18857 static sf_count_t pcm_write_i2sc (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
18858 static sf_count_t pcm_write_i2uc (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
18859 static sf_count_t pcm_write_i2bes (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
18860 static sf_count_t pcm_write_i2les (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
18861 static sf_count_t pcm_write_i2bet (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
18862 static sf_count_t pcm_write_i2let (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
18863 static sf_count_t pcm_write_i2bei (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
18864 static sf_count_t pcm_write_i2lei (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
18866 static sf_count_t pcm_write_f2sc (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
18867 static sf_count_t pcm_write_f2uc (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
18868 static sf_count_t pcm_write_f2bes (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
18869 static sf_count_t pcm_write_f2les (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
18870 static sf_count_t pcm_write_f2bet (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
18871 static sf_count_t pcm_write_f2let (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
18872 static sf_count_t pcm_write_f2bei (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
18873 static sf_count_t pcm_write_f2lei (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
18875 static sf_count_t pcm_write_d2sc (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
18876 static sf_count_t pcm_write_d2uc (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
18877 static sf_count_t pcm_write_d2bes (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
18878 static sf_count_t pcm_write_d2les (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
18879 static sf_count_t pcm_write_d2bet (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
18880 static sf_count_t pcm_write_d2let (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
18881 static sf_count_t pcm_write_d2bei (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
18882 static sf_count_t pcm_write_d2lei (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
18884 static void sc2s_array (signed char *src, int count, short *dest) ;
18885 static void uc2s_array (unsigned char *src, int count, short *dest) ;
18886 static void bet2s_array (tribyte *src, int count, short *dest) ;
18887 static void let2s_array (tribyte *src, int count, short *dest) ;
18888 static void bei2s_array (int *src, int count, short *dest) ;
18889 static void lei2s_array (int *src, int count, short *dest) ;
18891 static void sc2i_array (signed char *src, int count, int *dest) ;
18892 static void uc2i_array (unsigned char *src, int count, int *dest) ;
18893 static void bes2i_array (short *src, int count, int *dest) ;
18894 static void les2i_array (short *src, int count, int *dest) ;
18895 static void bet2i_array (tribyte *src, int count, int *dest) ;
18896 static void let2i_array (tribyte *src, int count, int *dest) ;
18898 static void sc2f_array (signed char *src, int count, float *dest, float normfact) ;
18899 static void uc2f_array (unsigned char *src, int count, float *dest, float normfact) ;
18900 static void bes2f_array (short *src, int count, float *dest, float normfact) ;
18901 static void les2f_array (short *src, int count, float *dest, float normfact) ;
18902 static void bet2f_array (tribyte *src, int count, float *dest, float normfact) ;
18903 static void let2f_array (tribyte *src, int count, float *dest, float normfact) ;
18904 static void bei2f_array (int *src, int count, float *dest, float normfact) ;
18905 static void lei2f_array (int *src, int count, float *dest, float normfact) ;
18907 static void sc2d_array (signed char *src, int count, double *dest, double normfact) ;
18908 static void uc2d_array (unsigned char *src, int count, double *dest, double normfact) ;
18909 static void bes2d_array (short *src, int count, double *dest, double normfact) ;
18910 static void les2d_array (short *src, int count, double *dest, double normfact) ;
18911 static void bet2d_array (tribyte *src, int count, double *dest, double normfact) ;
18912 static void let2d_array (tribyte *src, int count, double *dest, double normfact) ;
18913 static void bei2d_array (int *src, int count, double *dest, double normfact) ;
18914 static void lei2d_array (int *src, int count, double *dest, double normfact) ;
18916 static void s2sc_array (short *src, signed char *dest, int count) ;
18917 static void s2uc_array (short *src, unsigned char *dest, int count) ;
18918 static void s2bet_array (short *src, tribyte *dest, int count) ;
18919 static void s2let_array (short *src, tribyte *dest, int count) ;
18920 static void s2bei_array (short *src, int *dest, int count) ;
18921 static void s2lei_array (short *src, int *dest, int count) ;
18923 static void i2sc_array (int *src, signed char *dest, int count) ;
18924 static void i2uc_array (int *src, unsigned char *dest, int count) ;
18925 static void i2bes_array (int *src, short *dest, int count) ;
18926 static void i2les_array (int *src, short *dest, int count) ;
18927 static void i2bet_array (int *src, tribyte *dest, int count) ;
18928 static void i2let_array (int *src, tribyte *dest, int count) ;
18930 /*-----------------------------------------------------------------------------------------------
18933 enum
18934 { /* Char type for 8 bit files. */
18935 SF_CHARS_SIGNED = 200,
18936 SF_CHARS_UNSIGNED = 201
18939 /*-----------------------------------------------------------------------------------------------
18943 pcm_init (SF_PRIVATE *psf)
18944 { int chars = 0 ;
18946 if (psf->bytewidth == 0 || psf->sf.channels == 0)
18947 return SFE_INTERNAL ;
18950 psf->blockwidth = psf->bytewidth * psf->sf.channels ;
18952 if ((psf->sf.format & SF_FORMAT_SUBMASK) == SF_FORMAT_PCM_S8)
18953 chars = SF_CHARS_SIGNED ;
18954 else if ((psf->sf.format & SF_FORMAT_SUBMASK) == SF_FORMAT_PCM_U8)
18955 chars = SF_CHARS_UNSIGNED ;
18957 if (psf->mode == SFM_READ || psf->mode == SFM_RDWR)
18958 { switch (psf->bytewidth * 0x10000 + psf->endian + chars)
18959 { case (0x10000 + SF_ENDIAN_BIG + SF_CHARS_SIGNED) :
18960 case (0x10000 + SF_ENDIAN_LITTLE + SF_CHARS_SIGNED) :
18961 psf->read_short = pcm_read_sc2s ;
18962 psf->read_int = pcm_read_sc2i ;
18963 psf->read_float = pcm_read_sc2f ;
18964 psf->read_double = pcm_read_sc2d ;
18965 break ;
18966 case (0x10000 + SF_ENDIAN_BIG + SF_CHARS_UNSIGNED) :
18967 case (0x10000 + SF_ENDIAN_LITTLE + SF_CHARS_UNSIGNED) :
18968 psf->read_short = pcm_read_uc2s ;
18969 psf->read_int = pcm_read_uc2i ;
18970 psf->read_float = pcm_read_uc2f ;
18971 psf->read_double = pcm_read_uc2d ;
18972 break ;
18974 case (2 * 0x10000 + SF_ENDIAN_BIG) :
18975 psf->read_short = pcm_read_bes2s ;
18976 psf->read_int = pcm_read_bes2i ;
18977 psf->read_float = pcm_read_bes2f ;
18978 psf->read_double = pcm_read_bes2d ;
18979 break ;
18980 case (3 * 0x10000 + SF_ENDIAN_BIG) :
18981 psf->read_short = pcm_read_bet2s ;
18982 psf->read_int = pcm_read_bet2i ;
18983 psf->read_float = pcm_read_bet2f ;
18984 psf->read_double = pcm_read_bet2d ;
18985 break ;
18986 case (4 * 0x10000 + SF_ENDIAN_BIG) :
18987 psf->read_short = pcm_read_bei2s ;
18988 psf->read_int = pcm_read_bei2i ;
18989 psf->read_float = pcm_read_bei2f ;
18990 psf->read_double = pcm_read_bei2d ;
18991 break ;
18993 case (2 * 0x10000 + SF_ENDIAN_LITTLE) :
18994 psf->read_short = pcm_read_les2s ;
18995 psf->read_int = pcm_read_les2i ;
18996 psf->read_float = pcm_read_les2f ;
18997 psf->read_double = pcm_read_les2d ;
18998 break ;
18999 case (3 * 0x10000 + SF_ENDIAN_LITTLE) :
19000 psf->read_short = pcm_read_let2s ;
19001 psf->read_int = pcm_read_let2i ;
19002 psf->read_float = pcm_read_let2f ;
19003 psf->read_double = pcm_read_let2d ;
19004 break ;
19005 case (4 * 0x10000 + SF_ENDIAN_LITTLE) :
19006 psf->read_short = pcm_read_lei2s ;
19007 psf->read_int = pcm_read_lei2i ;
19008 psf->read_float = pcm_read_lei2f ;
19009 psf->read_double = pcm_read_lei2d ;
19010 break ;
19011 default :
19012 psf_log_printf (psf, "pcm.c returning SFE_UNIMPLEMENTED\n") ;
19013 return SFE_UNIMPLEMENTED ;
19017 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
19018 { switch (psf->bytewidth * 0x10000 + psf->endian + chars)
19019 { case (0x10000 + SF_ENDIAN_BIG + SF_CHARS_SIGNED) :
19020 case (0x10000 + SF_ENDIAN_LITTLE + SF_CHARS_SIGNED) :
19021 psf->write_short = pcm_write_s2sc ;
19022 psf->write_int = pcm_write_i2sc ;
19023 psf->write_float = pcm_write_f2sc ;
19024 psf->write_double = pcm_write_d2sc ;
19025 break ;
19026 case (0x10000 + SF_ENDIAN_BIG + SF_CHARS_UNSIGNED) :
19027 case (0x10000 + SF_ENDIAN_LITTLE + SF_CHARS_UNSIGNED) :
19028 psf->write_short = pcm_write_s2uc ;
19029 psf->write_int = pcm_write_i2uc ;
19030 psf->write_float = pcm_write_f2uc ;
19031 psf->write_double = pcm_write_d2uc ;
19032 break ;
19034 case (2 * 0x10000 + SF_ENDIAN_BIG) :
19035 psf->write_short = pcm_write_s2bes ;
19036 psf->write_int = pcm_write_i2bes ;
19037 psf->write_float = pcm_write_f2bes ;
19038 psf->write_double = pcm_write_d2bes ;
19039 break ;
19041 case (3 * 0x10000 + SF_ENDIAN_BIG) :
19042 psf->write_short = pcm_write_s2bet ;
19043 psf->write_int = pcm_write_i2bet ;
19044 psf->write_float = pcm_write_f2bet ;
19045 psf->write_double = pcm_write_d2bet ;
19046 break ;
19048 case (4 * 0x10000 + SF_ENDIAN_BIG) :
19049 psf->write_short = pcm_write_s2bei ;
19050 psf->write_int = pcm_write_i2bei ;
19051 psf->write_float = pcm_write_f2bei ;
19052 psf->write_double = pcm_write_d2bei ;
19053 break ;
19055 case (2 * 0x10000 + SF_ENDIAN_LITTLE) :
19056 psf->write_short = pcm_write_s2les ;
19057 psf->write_int = pcm_write_i2les ;
19058 psf->write_float = pcm_write_f2les ;
19059 psf->write_double = pcm_write_d2les ;
19060 break ;
19062 case (3 * 0x10000 + SF_ENDIAN_LITTLE) :
19063 psf->write_short = pcm_write_s2let ;
19064 psf->write_int = pcm_write_i2let ;
19065 psf->write_float = pcm_write_f2let ;
19066 psf->write_double = pcm_write_d2let ;
19067 break ;
19069 case (4 * 0x10000 + SF_ENDIAN_LITTLE) :
19070 psf->write_short = pcm_write_s2lei ;
19071 psf->write_int = pcm_write_i2lei ;
19072 psf->write_float = pcm_write_f2lei ;
19073 psf->write_double = pcm_write_d2lei ;
19074 break ;
19076 default :
19077 psf_log_printf (psf, "pcm.c returning SFE_UNIMPLEMENTED\n") ;
19078 return SFE_UNIMPLEMENTED ;
19083 if (psf->filelength > psf->dataoffset)
19084 { psf->datalength = (psf->dataend > 0) ? psf->dataend - psf->dataoffset :
19085 psf->filelength - psf->dataoffset ;
19087 else
19088 psf->datalength = 0 ;
19090 psf->sf.frames = psf->datalength / psf->blockwidth ;
19092 return 0 ;
19093 } /* pcm_init */
19095 /*-----------------------------------------------------------------------------------------------
19098 static sf_count_t
19099 pcm_read_sc2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
19100 { int bufferlen, readcount, thisread ;
19101 sf_count_t total = 0 ;
19103 bufferlen = sizeof (psf->buffer) / sizeof (signed char) ;
19105 while (len > 0)
19106 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
19107 thisread = psf_fread (psf->buffer, sizeof (signed char), readcount, psf) ;
19108 sc2s_array ((signed char*) (psf->buffer), thisread, ptr + total) ;
19109 total += thisread ;
19110 len -= thisread ;
19111 if (thisread < readcount)
19112 break ;
19115 return total ;
19116 } /* pcm_read_sc2s */
19118 static sf_count_t
19119 pcm_read_uc2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
19120 { int bufferlen, readcount, thisread ;
19121 sf_count_t total = 0 ;
19123 bufferlen = sizeof (psf->buffer) / sizeof (unsigned char) ;
19125 while (len > 0)
19126 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
19127 thisread = psf_fread (psf->buffer, sizeof (unsigned char), readcount, psf) ;
19128 uc2s_array ((unsigned char*) (psf->buffer), thisread, ptr + total) ;
19129 total += thisread ;
19130 len -= thisread ;
19131 if (thisread < readcount)
19132 break ;
19135 return total ;
19136 } /* pcm_read_uc2s */
19138 static sf_count_t
19139 pcm_read_bes2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
19140 { int total ;
19142 total = psf_fread (ptr, sizeof (short), len, psf) ;
19143 if (CPU_IS_LITTLE_ENDIAN)
19144 endswap_short_array (ptr, len) ;
19146 return total ;
19147 } /* pcm_read_bes2s */
19149 static sf_count_t
19150 pcm_read_les2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
19151 { int total ;
19153 total = psf_fread (ptr, sizeof (short), len, psf) ;
19154 if (CPU_IS_BIG_ENDIAN)
19155 endswap_short_array (ptr, len) ;
19157 return total ;
19158 } /* pcm_read_les2s */
19160 static sf_count_t
19161 pcm_read_bet2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
19162 { int bufferlen, readcount, thisread ;
19163 sf_count_t total = 0 ;
19165 bufferlen = sizeof (psf->buffer) / SIZEOF_TRIBYTE ;
19167 while (len > 0)
19168 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
19169 thisread = psf_fread (psf->buffer, SIZEOF_TRIBYTE, readcount, psf) ;
19170 bet2s_array ((tribyte*) (psf->buffer), thisread, ptr + total) ;
19171 total += thisread ;
19172 len -= thisread ;
19173 if (thisread < readcount)
19174 break ;
19177 return total ;
19178 } /* pcm_read_bet2s */
19180 static sf_count_t
19181 pcm_read_let2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
19182 { int bufferlen, readcount, thisread ;
19183 sf_count_t total = 0 ;
19185 bufferlen = sizeof (psf->buffer) / SIZEOF_TRIBYTE ;
19187 while (len > 0)
19188 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
19189 thisread = psf_fread (psf->buffer, SIZEOF_TRIBYTE, readcount, psf) ;
19190 let2s_array ((tribyte*) (psf->buffer), thisread, ptr + total) ;
19191 total += thisread ;
19192 len -= thisread ;
19193 if (thisread < readcount)
19194 break ;
19197 return total ;
19198 } /* pcm_read_let2s */
19200 static sf_count_t
19201 pcm_read_bei2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
19202 { int bufferlen, readcount, thisread ;
19203 sf_count_t total = 0 ;
19205 bufferlen = sizeof (psf->buffer) / sizeof (int) ;
19207 while (len > 0)
19208 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
19209 thisread = psf_fread (psf->buffer, sizeof (int), readcount, psf) ;
19210 bei2s_array ((int*) (psf->buffer), thisread, ptr + total) ;
19211 total += thisread ;
19212 len -= thisread ;
19213 if (thisread < readcount)
19214 break ;
19217 return total ;
19218 } /* pcm_read_bei2s */
19220 static sf_count_t
19221 pcm_read_lei2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
19222 { int bufferlen, readcount, thisread ;
19223 sf_count_t total = 0 ;
19225 bufferlen = sizeof (psf->buffer) / sizeof (int) ;
19227 while (len > 0)
19228 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
19229 thisread = psf_fread (psf->buffer, sizeof (int), readcount, psf) ;
19230 lei2s_array ((int*) (psf->buffer), thisread, ptr + total) ;
19231 total += thisread ;
19232 len -= thisread ;
19233 if (thisread < readcount)
19234 break ;
19237 return total ;
19238 } /* pcm_read_lei2s */
19240 /*-----------------------------------------------------------------------------------------------
19243 static sf_count_t
19244 pcm_read_sc2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
19245 { int bufferlen, readcount, thisread ;
19246 sf_count_t total = 0 ;
19248 bufferlen = sizeof (psf->buffer) / sizeof (signed char) ;
19250 while (len > 0)
19251 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
19252 thisread = psf_fread (psf->buffer, sizeof (signed char), readcount, psf) ;
19253 sc2i_array ((signed char*) (psf->buffer), thisread, ptr + total) ;
19254 total += thisread ;
19255 len -= thisread ;
19256 if (thisread < readcount)
19257 break ;
19260 return total ;
19261 } /* pcm_read_sc2i */
19263 static sf_count_t
19264 pcm_read_uc2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
19265 { int bufferlen, readcount, thisread ;
19266 sf_count_t total = 0 ;
19268 bufferlen = sizeof (psf->buffer) / sizeof (unsigned char) ;
19270 while (len > 0)
19271 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
19272 thisread = psf_fread (psf->buffer, sizeof (unsigned char), readcount, psf) ;
19273 uc2i_array ((unsigned char*) (psf->buffer), thisread, ptr + total) ;
19274 total += thisread ;
19275 len -= thisread ;
19276 if (thisread < readcount)
19277 break ;
19280 return total ;
19281 } /* pcm_read_uc2i */
19283 static sf_count_t
19284 pcm_read_bes2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
19285 { int bufferlen, readcount, thisread ;
19286 sf_count_t total = 0 ;
19288 bufferlen = sizeof (psf->buffer) / sizeof (short) ;
19290 while (len > 0)
19291 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
19292 thisread = psf_fread (psf->buffer, sizeof (short), readcount, psf) ;
19293 bes2i_array ((short*) (psf->buffer), thisread, ptr + total) ;
19294 total += thisread ;
19295 len -= thisread ;
19296 if (thisread < readcount)
19297 break ;
19300 return total ;
19301 } /* pcm_read_bes2i */
19303 static sf_count_t
19304 pcm_read_les2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
19305 { int bufferlen, readcount, thisread ;
19306 sf_count_t total = 0 ;
19308 bufferlen = sizeof (psf->buffer) / sizeof (short) ;
19310 while (len > 0)
19311 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
19312 thisread = psf_fread (psf->buffer, sizeof (short), readcount, psf) ;
19313 les2i_array ((short*) (psf->buffer), thisread, ptr + total) ;
19314 total += thisread ;
19315 len -= thisread ;
19316 if (thisread < readcount)
19317 break ;
19320 return total ;
19321 } /* pcm_read_les2i */
19323 static sf_count_t
19324 pcm_read_bet2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
19325 { int bufferlen, readcount, thisread ;
19326 sf_count_t total = 0 ;
19328 bufferlen = sizeof (psf->buffer) / SIZEOF_TRIBYTE ;
19330 while (len > 0)
19331 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
19332 thisread = psf_fread (psf->buffer, SIZEOF_TRIBYTE, readcount, psf) ;
19333 bet2i_array ((tribyte*) (psf->buffer), thisread, ptr + total) ;
19334 total += thisread ;
19335 len -= thisread ;
19336 if (thisread < readcount)
19337 break ;
19340 return total ;
19341 } /* pcm_read_bet2i */
19343 static sf_count_t
19344 pcm_read_let2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
19345 { int bufferlen, readcount, thisread ;
19346 sf_count_t total = 0 ;
19348 bufferlen = sizeof (psf->buffer) / SIZEOF_TRIBYTE ;
19350 while (len > 0)
19351 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
19352 thisread = psf_fread (psf->buffer, SIZEOF_TRIBYTE, readcount, psf) ;
19353 let2i_array ((tribyte*) (psf->buffer), thisread, ptr + total) ;
19354 total += thisread ;
19355 len -= thisread ;
19356 if (thisread < readcount)
19357 break ;
19360 return total ;
19361 } /* pcm_read_let2i */
19363 static sf_count_t
19364 pcm_read_bei2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
19365 { int total ;
19367 total = psf_fread (ptr, sizeof (int), len, psf) ;
19368 if (CPU_IS_LITTLE_ENDIAN)
19369 endswap_int_array (ptr, len) ;
19371 return total ;
19372 } /* pcm_read_bei2i */
19374 static sf_count_t
19375 pcm_read_lei2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
19376 { int total ;
19378 total = psf_fread (ptr, sizeof (int), len, psf) ;
19379 if (CPU_IS_BIG_ENDIAN)
19380 endswap_int_array (ptr, len) ;
19382 return total ;
19383 } /* pcm_read_lei2i */
19385 /*-----------------------------------------------------------------------------------------------
19388 static sf_count_t
19389 pcm_read_sc2f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
19390 { int bufferlen, readcount, thisread ;
19391 sf_count_t total = 0 ;
19392 float normfact ;
19394 normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x80) : 1.0 ;
19396 bufferlen = sizeof (psf->buffer) / sizeof (signed char) ;
19398 while (len > 0)
19399 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
19400 thisread = psf_fread (psf->buffer, sizeof (signed char), readcount, psf) ;
19401 sc2f_array ((signed char*) (psf->buffer), thisread, ptr + total, normfact) ;
19402 total += thisread ;
19403 len -= thisread ;
19404 if (thisread < readcount)
19405 break ;
19408 return total ;
19409 } /* pcm_read_sc2f */
19411 static sf_count_t
19412 pcm_read_uc2f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
19413 { int bufferlen, readcount, thisread ;
19414 sf_count_t total = 0 ;
19415 float normfact ;
19417 normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x80) : 1.0 ;
19419 bufferlen = sizeof (psf->buffer) / sizeof (unsigned char) ;
19421 while (len > 0)
19422 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
19423 thisread = psf_fread (psf->buffer, sizeof (unsigned char), readcount, psf) ;
19424 uc2f_array ((unsigned char*) (psf->buffer), thisread, ptr + total, normfact) ;
19425 total += thisread ;
19426 len -= thisread ;
19427 if (thisread < readcount)
19428 break ;
19431 return total ;
19432 } /* pcm_read_uc2f */
19434 static sf_count_t
19435 pcm_read_bes2f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
19436 { int bufferlen, readcount, thisread ;
19437 sf_count_t total = 0 ;
19438 float normfact ;
19440 normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x8000) : 1.0 ;
19442 bufferlen = sizeof (psf->buffer) / sizeof (short) ;
19444 while (len > 0)
19445 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
19446 thisread = psf_fread (psf->buffer, sizeof (short), readcount, psf) ;
19447 bes2f_array ((short*) (psf->buffer), thisread, ptr + total, normfact) ;
19448 total += thisread ;
19449 len -= thisread ;
19450 if (thisread < readcount)
19451 break ;
19454 return total ;
19455 } /* pcm_read_bes2f */
19457 static sf_count_t
19458 pcm_read_les2f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
19459 { int bufferlen, readcount, thisread ;
19460 sf_count_t total = 0 ;
19461 float normfact ;
19463 normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x8000) : 1.0 ;
19465 bufferlen = sizeof (psf->buffer) / sizeof (short) ;
19467 while (len > 0)
19468 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
19469 thisread = psf_fread (psf->buffer, sizeof (short), readcount, psf) ;
19470 les2f_array ((short*) (psf->buffer), thisread, ptr + total, normfact) ;
19471 total += thisread ;
19472 len -= thisread ;
19473 if (thisread < readcount)
19474 break ;
19477 return total ;
19478 } /* pcm_read_les2f */
19480 static sf_count_t
19481 pcm_read_bet2f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
19482 { int bufferlen, readcount, thisread ;
19483 sf_count_t total = 0 ;
19484 float normfact ;
19486 /* Special normfactor because tribyte value is read into an int. */
19487 normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x80000000) : 1.0 / 256.0 ;
19489 bufferlen = sizeof (psf->buffer) / SIZEOF_TRIBYTE ;
19491 while (len > 0)
19492 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
19493 thisread = psf_fread (psf->buffer, SIZEOF_TRIBYTE, readcount, psf) ;
19494 bet2f_array ((tribyte*) (psf->buffer), thisread, ptr + total, normfact) ;
19495 total += thisread ;
19496 len -= thisread ;
19497 if (thisread < readcount)
19498 break ;
19501 return total ;
19502 } /* pcm_read_bet2f */
19504 static sf_count_t
19505 pcm_read_let2f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
19506 { int bufferlen, readcount, thisread ;
19507 sf_count_t total = 0 ;
19508 float normfact ;
19510 /* Special normfactor because tribyte value is read into an int. */
19511 normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x80000000) : 1.0 / 256.0 ;
19513 bufferlen = sizeof (psf->buffer) / SIZEOF_TRIBYTE ;
19515 while (len > 0)
19516 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
19517 thisread = psf_fread (psf->buffer, SIZEOF_TRIBYTE, readcount, psf) ;
19518 let2f_array ((tribyte*) (psf->buffer), thisread, ptr + total, normfact) ;
19519 total += thisread ;
19520 len -= thisread ;
19521 if (thisread < readcount)
19522 break ;
19525 return total ;
19526 } /* pcm_read_let2f */
19528 static sf_count_t
19529 pcm_read_bei2f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
19530 { int bufferlen, readcount, thisread ;
19531 sf_count_t total = 0 ;
19532 float normfact ;
19534 normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x80000000) : 1.0 ;
19536 bufferlen = sizeof (psf->buffer) / sizeof (int) ;
19538 while (len > 0)
19539 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
19540 thisread = psf_fread (psf->buffer, sizeof (int), readcount, psf) ;
19541 bei2f_array ((int*) (psf->buffer), thisread, ptr + total, normfact) ;
19542 total += thisread ;
19543 len -= thisread ;
19544 if (thisread < readcount)
19545 break ;
19548 return total ;
19549 } /* pcm_read_bei2f */
19551 static sf_count_t
19552 pcm_read_lei2f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
19553 { int bufferlen, readcount, thisread ;
19554 sf_count_t total = 0 ;
19555 float normfact ;
19557 normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x80000000) : 1.0 ;
19559 bufferlen = sizeof (psf->buffer) / sizeof (int) ;
19561 while (len > 0)
19562 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
19563 thisread = psf_fread (psf->buffer, sizeof (int), readcount, psf) ;
19564 lei2f_array ((int*) (psf->buffer), thisread, ptr + total, normfact) ;
19565 total += thisread ;
19566 len -= thisread ;
19567 if (thisread < readcount)
19568 break ;
19571 return total ;
19572 } /* pcm_read_lei2f */
19574 /*-----------------------------------------------------------------------------------------------
19577 static sf_count_t
19578 pcm_read_sc2d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
19579 { int bufferlen, readcount, thisread ;
19580 sf_count_t total = 0 ;
19581 double normfact ;
19583 normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x80) : 1.0 ;
19585 bufferlen = sizeof (psf->buffer) / sizeof (signed char) ;
19587 while (len > 0)
19588 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
19589 thisread = psf_fread (psf->buffer, sizeof (signed char), readcount, psf) ;
19590 sc2d_array ((signed char*) (psf->buffer), thisread, ptr + total, normfact) ;
19591 total += thisread ;
19592 len -= thisread ;
19593 if (thisread < readcount)
19594 break ;
19597 return total ;
19598 } /* pcm_read_sc2d */
19600 static sf_count_t
19601 pcm_read_uc2d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
19602 { int bufferlen, readcount, thisread ;
19603 sf_count_t total = 0 ;
19604 double normfact ;
19606 normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x80) : 1.0 ;
19608 bufferlen = sizeof (psf->buffer) / sizeof (unsigned char) ;
19610 while (len > 0)
19611 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
19612 thisread = psf_fread (psf->buffer, sizeof (unsigned char), readcount, psf) ;
19613 uc2d_array ((unsigned char*) (psf->buffer), thisread, ptr + total, normfact) ;
19614 total += thisread ;
19615 len -= thisread ;
19616 if (thisread < readcount)
19617 break ;
19620 return total ;
19621 } /* pcm_read_uc2d */
19623 static sf_count_t
19624 pcm_read_bes2d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
19625 { int bufferlen, readcount, thisread ;
19626 sf_count_t total = 0 ;
19627 double normfact ;
19629 normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x8000) : 1.0 ;
19631 bufferlen = sizeof (psf->buffer) / sizeof (short) ;
19633 while (len > 0)
19634 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
19635 thisread = psf_fread (psf->buffer, sizeof (short), readcount, psf) ;
19636 bes2d_array ((short*) (psf->buffer), thisread, ptr + total, normfact) ;
19637 total += thisread ;
19638 len -= thisread ;
19639 if (thisread < readcount)
19640 break ;
19643 return total ;
19644 } /* pcm_read_bes2d */
19646 static sf_count_t
19647 pcm_read_les2d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
19648 { int bufferlen, readcount, thisread ;
19649 sf_count_t total = 0 ;
19650 double normfact ;
19652 normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x8000) : 1.0 ;
19654 bufferlen = sizeof (psf->buffer) / sizeof (short) ;
19656 while (len > 0)
19657 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
19658 thisread = psf_fread (psf->buffer, sizeof (short), readcount, psf) ;
19659 les2d_array ((short*) (psf->buffer), thisread, ptr + total, normfact) ;
19660 total += thisread ;
19661 len -= thisread ;
19662 if (thisread < readcount)
19663 break ;
19666 return total ;
19667 } /* pcm_read_les2d */
19669 static sf_count_t
19670 pcm_read_bet2d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
19671 { int bufferlen, readcount, thisread ;
19672 sf_count_t total = 0 ;
19673 double normfact ;
19675 normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x80000000) : 1.0 / 256.0 ;
19677 bufferlen = sizeof (psf->buffer) / SIZEOF_TRIBYTE ;
19679 while (len > 0)
19680 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
19681 thisread = psf_fread (psf->buffer, SIZEOF_TRIBYTE, readcount, psf) ;
19682 bet2d_array ((tribyte*) (psf->buffer), thisread, ptr + total, normfact) ;
19683 total += thisread ;
19684 len -= thisread ;
19685 if (thisread < readcount)
19686 break ;
19689 return total ;
19690 } /* pcm_read_bet2d */
19692 static sf_count_t
19693 pcm_read_let2d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
19694 { int bufferlen, readcount, thisread ;
19695 sf_count_t total = 0 ;
19696 double normfact ;
19698 /* Special normfactor because tribyte value is read into an int. */
19699 normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x80000000) : 1.0 / 256.0 ;
19701 bufferlen = sizeof (psf->buffer) / SIZEOF_TRIBYTE ;
19703 while (len > 0)
19704 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
19705 thisread = psf_fread (psf->buffer, SIZEOF_TRIBYTE, readcount, psf) ;
19706 let2d_array ((tribyte*) (psf->buffer), thisread, ptr + total, normfact) ;
19707 total += thisread ;
19708 len -= thisread ;
19709 if (thisread < readcount)
19710 break ;
19713 return total ;
19714 } /* pcm_read_let2d */
19716 static sf_count_t
19717 pcm_read_bei2d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
19718 { int bufferlen, readcount, thisread ;
19719 sf_count_t total = 0 ;
19720 double normfact ;
19722 normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x80000000) : 1.0 ;
19724 bufferlen = sizeof (psf->buffer) / sizeof (int) ;
19726 while (len > 0)
19727 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
19728 thisread = psf_fread (psf->buffer, sizeof (int), readcount, psf) ;
19729 bei2d_array ((int*) (psf->buffer), thisread, ptr + total, normfact) ;
19730 total += thisread ;
19731 len -= thisread ;
19732 if (thisread < readcount)
19733 break ;
19736 return total ;
19737 } /* pcm_read_bei2d */
19739 static sf_count_t
19740 pcm_read_lei2d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
19741 { int bufferlen, readcount, thisread ;
19742 sf_count_t total = 0 ;
19743 double normfact ;
19745 normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x80000000) : 1.0 ;
19747 bufferlen = sizeof (psf->buffer) / sizeof (int) ;
19749 while (len > 0)
19750 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
19751 thisread = psf_fread (psf->buffer, sizeof (int), readcount, psf) ;
19752 lei2d_array ((int*) (psf->buffer), thisread, ptr + total, normfact) ;
19753 total += thisread ;
19754 len -= thisread ;
19755 if (thisread < readcount)
19756 break ;
19759 return total ;
19760 } /* pcm_read_lei2d */
19762 /*===============================================================================================
19763 **-----------------------------------------------------------------------------------------------
19764 **===============================================================================================
19767 static sf_count_t
19768 pcm_write_s2sc (SF_PRIVATE *psf, short *ptr, sf_count_t len)
19769 { int bufferlen, writecount, thiswrite ;
19770 sf_count_t total = 0 ;
19772 bufferlen = sizeof (psf->buffer) / sizeof (signed char) ;
19774 while (len > 0)
19775 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
19776 s2sc_array (ptr + total, (signed char*) (psf->buffer), writecount) ;
19777 thiswrite = psf_fwrite (psf->buffer, sizeof (signed char), writecount, psf) ;
19778 total += thiswrite ;
19779 len -= thiswrite ;
19780 if (thiswrite < writecount)
19781 break ;
19784 return total ;
19785 } /* pcm_write_s2sc */
19787 static sf_count_t
19788 pcm_write_s2uc (SF_PRIVATE *psf, short *ptr, sf_count_t len)
19789 { int bufferlen, writecount, thiswrite ;
19790 sf_count_t total = 0 ;
19792 bufferlen = sizeof (psf->buffer) / sizeof (unsigned char) ;
19794 while (len > 0)
19795 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
19796 s2uc_array (ptr + total, (unsigned char*) (psf->buffer), writecount) ;
19797 thiswrite = psf_fwrite (psf->buffer, sizeof (unsigned char), writecount, psf) ;
19798 total += thiswrite ;
19799 len -= thiswrite ;
19800 if (thiswrite < writecount)
19801 break ;
19804 return total ;
19805 } /* pcm_write_s2uc */
19807 static sf_count_t
19808 pcm_write_s2bes (SF_PRIVATE *psf, short *ptr, sf_count_t len)
19810 if (CPU_IS_BIG_ENDIAN)
19811 return psf_fwrite (ptr, sizeof (short), len, psf) ;
19812 else
19813 { int bufferlen, writecount, thiswrite ;
19814 sf_count_t total = 0 ;
19816 bufferlen = sizeof (psf->buffer) / sizeof (short) ;
19818 while (len > 0)
19819 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
19820 endswap_short_copy ((short*) (psf->buffer), ptr + total, writecount) ;
19821 thiswrite = psf_fwrite (psf->buffer, sizeof (short), writecount, psf) ;
19822 total += thiswrite ;
19823 len -= thiswrite ;
19824 if (thiswrite < writecount)
19825 break ;
19828 return total ;
19830 } /* pcm_write_s2bes */
19832 static sf_count_t
19833 pcm_write_s2les (SF_PRIVATE *psf, short *ptr, sf_count_t len)
19835 if (CPU_IS_LITTLE_ENDIAN)
19836 return psf_fwrite (ptr, sizeof (short), len, psf) ;
19837 else
19838 { int bufferlen, writecount, thiswrite ;
19839 sf_count_t total = 0 ;
19841 bufferlen = sizeof (psf->buffer) / sizeof (short) ;
19843 while (len > 0)
19844 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
19845 endswap_short_copy ((short*) (psf->buffer), ptr + total, writecount) ;
19846 thiswrite = psf_fwrite (psf->buffer, sizeof (short), writecount, psf) ;
19847 total += thiswrite ;
19848 len -= thiswrite ;
19849 if (thiswrite < writecount)
19850 break ;
19853 return total ;
19855 } /* pcm_write_s2les */
19857 static sf_count_t
19858 pcm_write_s2bet (SF_PRIVATE *psf, short *ptr, sf_count_t len)
19859 { int bufferlen, writecount, thiswrite ;
19860 sf_count_t total = 0 ;
19862 bufferlen = sizeof (psf->buffer) / SIZEOF_TRIBYTE ;
19864 while (len > 0)
19865 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
19866 s2bet_array (ptr + total, (tribyte*) (psf->buffer), writecount) ;
19867 thiswrite = psf_fwrite (psf->buffer, SIZEOF_TRIBYTE, writecount, psf) ;
19868 total += thiswrite ;
19869 len -= thiswrite ;
19870 if (thiswrite < writecount)
19871 break ;
19874 return total ;
19875 } /* pcm_write_s2bet */
19877 static sf_count_t
19878 pcm_write_s2let (SF_PRIVATE *psf, short *ptr, sf_count_t len)
19879 { int bufferlen, writecount, thiswrite ;
19880 sf_count_t total = 0 ;
19882 bufferlen = sizeof (psf->buffer) / SIZEOF_TRIBYTE ;
19884 while (len > 0)
19885 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
19886 s2let_array (ptr + total, (tribyte*) (psf->buffer), writecount) ;
19887 thiswrite = psf_fwrite (psf->buffer, SIZEOF_TRIBYTE, writecount, psf) ;
19888 total += thiswrite ;
19889 len -= thiswrite ;
19890 if (thiswrite < writecount)
19891 break ;
19894 return total ;
19895 } /* pcm_write_s2let */
19897 static sf_count_t
19898 pcm_write_s2bei (SF_PRIVATE *psf, short *ptr, sf_count_t len)
19899 { int bufferlen, writecount, thiswrite ;
19900 sf_count_t total = 0 ;
19902 bufferlen = sizeof (psf->buffer) / sizeof (int) ;
19904 while (len > 0)
19905 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
19906 s2bei_array (ptr + total, (int*) (psf->buffer), writecount) ;
19907 thiswrite = psf_fwrite (psf->buffer, sizeof (int), writecount, psf) ;
19908 total += thiswrite ;
19909 len -= thiswrite ;
19910 if (thiswrite < writecount)
19911 break ;
19914 return total ;
19915 } /* pcm_write_s2bei */
19917 static sf_count_t
19918 pcm_write_s2lei (SF_PRIVATE *psf, short *ptr, sf_count_t len)
19919 { int bufferlen, writecount, thiswrite ;
19920 sf_count_t total = 0 ;
19922 bufferlen = sizeof (psf->buffer) / sizeof (int) ;
19924 while (len > 0)
19925 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
19926 s2lei_array (ptr + total, (int*) (psf->buffer), writecount) ;
19927 thiswrite = psf_fwrite (psf->buffer, sizeof (int), writecount, psf) ;
19928 total += thiswrite ;
19929 len -= thiswrite ;
19930 if (thiswrite < writecount)
19931 break ;
19934 return total ;
19935 } /* pcm_write_s2lei */
19937 /*-----------------------------------------------------------------------------------------------
19940 static sf_count_t
19941 pcm_write_i2sc (SF_PRIVATE *psf, int *ptr, sf_count_t len)
19942 { int bufferlen, writecount, thiswrite ;
19943 sf_count_t total = 0 ;
19945 bufferlen = sizeof (psf->buffer) / sizeof (signed char) ;
19947 while (len > 0)
19948 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
19949 i2sc_array (ptr + total, (signed char*) (psf->buffer), writecount) ;
19950 thiswrite = psf_fwrite (psf->buffer, sizeof (signed char), writecount, psf) ;
19951 total += thiswrite ;
19952 len -= thiswrite ;
19953 if (thiswrite < writecount)
19954 break ;
19957 return total ;
19958 } /* pcm_write_i2sc */
19960 static sf_count_t
19961 pcm_write_i2uc (SF_PRIVATE *psf, int *ptr, sf_count_t len)
19962 { int bufferlen, writecount, thiswrite ;
19963 sf_count_t total = 0 ;
19965 bufferlen = sizeof (psf->buffer) / sizeof (unsigned char) ;
19967 while (len > 0)
19968 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
19969 i2uc_array (ptr + total, (unsigned char*) (psf->buffer), writecount) ;
19970 thiswrite = psf_fwrite (psf->buffer, sizeof (signed char), writecount, psf) ;
19971 total += thiswrite ;
19972 len -= thiswrite ;
19973 if (thiswrite < writecount)
19974 break ;
19977 return total ;
19978 } /* pcm_write_i2uc */
19980 static sf_count_t
19981 pcm_write_i2bes (SF_PRIVATE *psf, int *ptr, sf_count_t len)
19982 { int bufferlen, writecount, thiswrite ;
19983 sf_count_t total = 0 ;
19985 bufferlen = sizeof (psf->buffer) / sizeof (short) ;
19987 while (len > 0)
19988 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
19989 i2bes_array (ptr + total, (short*) (psf->buffer), writecount) ;
19990 thiswrite = psf_fwrite (psf->buffer, sizeof (short), writecount, psf) ;
19991 total += thiswrite ;
19992 len -= thiswrite ;
19993 if (thiswrite < writecount)
19994 break ;
19997 return total ;
19998 } /* pcm_write_i2bes */
20000 static sf_count_t
20001 pcm_write_i2les (SF_PRIVATE *psf, int *ptr, sf_count_t len)
20002 { int bufferlen, writecount, thiswrite ;
20003 sf_count_t total = 0 ;
20005 bufferlen = sizeof (psf->buffer) / sizeof (short) ;
20007 while (len > 0)
20008 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
20009 i2les_array (ptr + total, (short*) (psf->buffer), writecount) ;
20010 thiswrite = psf_fwrite (psf->buffer, sizeof (short), writecount, psf) ;
20011 total += thiswrite ;
20012 len -= thiswrite ;
20013 if (thiswrite < writecount)
20014 break ;
20017 return total ;
20018 } /* pcm_write_i2les */
20020 static sf_count_t
20021 pcm_write_i2bet (SF_PRIVATE *psf, int *ptr, sf_count_t len)
20022 { int bufferlen, writecount, thiswrite ;
20023 sf_count_t total = 0 ;
20025 bufferlen = sizeof (psf->buffer) / SIZEOF_TRIBYTE ;
20027 while (len > 0)
20028 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
20029 i2bet_array (ptr + total, (tribyte*) (psf->buffer), writecount) ;
20030 thiswrite = psf_fwrite (psf->buffer, SIZEOF_TRIBYTE, writecount, psf) ;
20031 total += thiswrite ;
20032 len -= thiswrite ;
20033 if (thiswrite < writecount)
20034 break ;
20037 return total ;
20038 } /* pcm_write_i2bet */
20040 static sf_count_t
20041 pcm_write_i2let (SF_PRIVATE *psf, int *ptr, sf_count_t len)
20042 { int bufferlen, writecount, thiswrite ;
20043 sf_count_t total = 0 ;
20045 bufferlen = sizeof (psf->buffer) / SIZEOF_TRIBYTE ;
20047 while (len > 0)
20048 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
20049 i2let_array (ptr + total, (tribyte*) (psf->buffer), writecount) ;
20050 thiswrite = psf_fwrite (psf->buffer, SIZEOF_TRIBYTE, writecount, psf) ;
20051 total += thiswrite ;
20052 len -= thiswrite ;
20053 if (thiswrite < writecount)
20054 break ;
20057 return total ;
20058 } /* pcm_write_i2les */
20060 static sf_count_t
20061 pcm_write_i2bei (SF_PRIVATE *psf, int *ptr, sf_count_t len)
20063 if (CPU_IS_BIG_ENDIAN)
20064 return psf_fwrite (ptr, sizeof (int), len, psf) ;
20065 else
20066 { int bufferlen, writecount, thiswrite ;
20067 sf_count_t total = 0 ;
20069 bufferlen = sizeof (psf->buffer) / sizeof (int) ;
20071 while (len > 0)
20072 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
20073 endswap_int_copy ((int*) (psf->buffer), ptr + total, writecount) ;
20074 thiswrite = psf_fwrite (psf->buffer, sizeof (int), writecount, psf) ;
20075 total += thiswrite ;
20076 len -= thiswrite ;
20077 if (thiswrite < writecount)
20078 break ;
20081 return total ;
20083 } /* pcm_write_i2bei */
20085 static sf_count_t
20086 pcm_write_i2lei (SF_PRIVATE *psf, int *ptr, sf_count_t len)
20088 if (CPU_IS_LITTLE_ENDIAN)
20089 return psf_fwrite (ptr, sizeof (int), len, psf) ;
20090 else
20091 { int bufferlen, writecount, thiswrite ;
20092 sf_count_t total = 0 ;
20094 bufferlen = sizeof (psf->buffer) / sizeof (int) ;
20096 while (len > 0)
20097 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
20098 endswap_int_copy ((int*) (psf->buffer), ptr + total, writecount) ;
20099 thiswrite = psf_fwrite (psf->buffer, sizeof (int), writecount, psf) ;
20100 total += thiswrite ;
20101 len -= thiswrite ;
20102 if (thiswrite < writecount)
20103 break ;
20106 return total ;
20108 } /* pcm_write_i2lei */
20110 /*------------------------------------------------------------------------------
20111 **==============================================================================
20112 **------------------------------------------------------------------------------
20115 static void
20116 f2sc_array (float *src, signed char *dest, int count, int normalize)
20117 { float normfact ;
20119 normfact = normalize ? (1.0 * 0x7F) : 1.0 ;
20121 while (count)
20122 { count -- ;
20123 dest [count] = lrintf (src [count] * normfact) ;
20125 } /* f2sc_array */
20127 static void
20128 f2sc_clip_array (float *src, signed char *dest, int count, int normalize)
20129 { float normfact, scaled_value ;
20131 normfact = normalize ? (8.0 * 0x10000000) : (1.0 * 0x1000000) ;
20133 while (count)
20134 { count -- ;
20135 scaled_value = src [count] * normfact ;
20136 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF))
20137 { dest [count] = 127 ;
20138 continue ;
20140 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
20141 { dest [count] = -128 ;
20142 continue ;
20145 dest [count] = lrintf (scaled_value) >> 24 ;
20147 } /* f2sc_clip_array */
20149 static sf_count_t
20150 pcm_write_f2sc (SF_PRIVATE *psf, float *ptr, sf_count_t len)
20151 { void (*convert) (float *, signed char *, int, int) ;
20152 int bufferlen, writecount, thiswrite ;
20153 sf_count_t total = 0 ;
20155 convert = (psf->add_clipping) ? f2sc_clip_array : f2sc_array ;
20156 bufferlen = sizeof (psf->buffer) / sizeof (signed char) ;
20158 while (len > 0)
20159 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
20160 convert (ptr + total, (signed char*) (psf->buffer), writecount, psf->norm_float) ;
20161 thiswrite = psf_fwrite (psf->buffer, sizeof (signed char), writecount, psf) ;
20162 total += thiswrite ;
20163 len -= thiswrite ;
20164 if (thiswrite < writecount)
20165 break ;
20168 return total ;
20169 } /* pcm_write_f2sc */
20171 /*==============================================================================
20174 static void
20175 f2uc_array (float *src, unsigned char *dest, int count, int normalize)
20176 { float normfact ;
20178 normfact = normalize ? (1.0 * 0x7F) : 1.0 ;
20180 while (count)
20181 { count -- ;
20182 dest [count] = lrintf (src [count] * normfact) + 128 ;
20184 } /* f2uc_array */
20186 static void
20187 f2uc_clip_array (float *src, unsigned char *dest, int count, int normalize)
20188 { float normfact, scaled_value ;
20190 normfact = normalize ? (8.0 * 0x10000000) : (1.0 * 0x1000000) ;
20192 while (count)
20193 { count -- ;
20194 scaled_value = src [count] * normfact ;
20195 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF))
20196 { dest [count] = 0xFF ;
20197 continue ;
20199 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
20200 { dest [count] = 0 ;
20201 continue ;
20204 dest [count] = (lrintf (scaled_value) >> 24) + 128 ;
20206 } /* f2uc_clip_array */
20208 static sf_count_t
20209 pcm_write_f2uc (SF_PRIVATE *psf, float *ptr, sf_count_t len)
20210 { void (*convert) (float *, unsigned char *, int, int) ;
20211 int bufferlen, writecount, thiswrite ;
20212 sf_count_t total = 0 ;
20214 convert = (psf->add_clipping) ? f2uc_clip_array : f2uc_array ;
20215 bufferlen = sizeof (psf->buffer) / sizeof (unsigned char) ;
20217 while (len > 0)
20218 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
20219 convert (ptr + total, (unsigned char*) (psf->buffer), writecount, psf->norm_float) ;
20220 thiswrite = psf_fwrite (psf->buffer, sizeof (unsigned char), writecount, psf) ;
20221 total += thiswrite ;
20222 len -= thiswrite ;
20223 if (thiswrite < writecount)
20224 break ;
20227 return total ;
20228 } /* pcm_write_f2uc */
20230 /*==============================================================================
20233 static void
20234 f2bes_array (float *src, short *dest, int count, int normalize)
20235 { unsigned char *ucptr ;
20236 float normfact ;
20237 short value ;
20239 normfact = normalize ? (1.0 * 0x7FFF) : 1.0 ;
20240 ucptr = ((unsigned char*) dest) + 2 * count ;
20242 while (count)
20243 { count -- ;
20244 ucptr -= 2 ;
20245 value = lrintf (src [count] * normfact) ;
20246 ucptr [1] = value ;
20247 ucptr [0] = value >> 8 ;
20249 } /* f2bes_array */
20251 static void
20252 f2bes_clip_array (float *src, short *dest, int count, int normalize)
20253 { unsigned char *ucptr ;
20254 float normfact, scaled_value ;
20255 int value ;
20257 normfact = normalize ? (8.0 * 0x10000000) : (1.0 * 0x10000) ;
20258 ucptr = ((unsigned char*) dest) + 2 * count ;
20260 while (count)
20261 { count -- ;
20262 ucptr -= 2 ;
20263 scaled_value = src [count] * normfact ;
20264 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF))
20265 { ucptr [1] = 0xFF ;
20266 ucptr [0] = 0x7F ;
20267 continue ;
20269 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
20270 { ucptr [1] = 0x00 ;
20271 ucptr [0] = 0x80 ;
20272 continue ;
20275 value = lrintf (scaled_value) ;
20276 ucptr [1] = value >> 16 ;
20277 ucptr [0] = value >> 24 ;
20279 } /* f2bes_clip_array */
20281 static sf_count_t
20282 pcm_write_f2bes (SF_PRIVATE *psf, float *ptr, sf_count_t len)
20283 { void (*convert) (float *, short *t, int, int) ;
20284 int bufferlen, writecount, thiswrite ;
20285 sf_count_t total = 0 ;
20287 convert = (psf->add_clipping) ? f2bes_clip_array : f2bes_array ;
20288 bufferlen = sizeof (psf->buffer) / sizeof (short) ;
20290 while (len > 0)
20291 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
20292 convert (ptr + total, (short*) (psf->buffer), writecount, psf->norm_float) ;
20293 thiswrite = psf_fwrite (psf->buffer, sizeof (short), writecount, psf) ;
20294 total += thiswrite ;
20295 len -= thiswrite ;
20296 if (thiswrite < writecount)
20297 break ;
20300 return total ;
20301 } /* pcm_write_f2bes */
20303 /*==============================================================================
20306 static void
20307 f2les_array (float *src, short *dest, int count, int normalize)
20308 { unsigned char *ucptr ;
20309 float normfact ;
20310 int value ;
20312 normfact = normalize ? (1.0 * 0x7FFF) : 1.0 ;
20313 ucptr = ((unsigned char*) dest) + 2 * count ;
20315 while (count)
20316 { count -- ;
20317 ucptr -= 2 ;
20318 value = lrintf (src [count] * normfact) ;
20319 ucptr [0] = value ;
20320 ucptr [1] = value >> 8 ;
20322 } /* f2les_array */
20324 static void
20325 f2les_clip_array (float *src, short *dest, int count, int normalize)
20326 { unsigned char *ucptr ;
20327 float normfact, scaled_value ;
20328 int value ;
20330 normfact = normalize ? (8.0 * 0x10000000) : (1.0 * 0x10000) ;
20331 ucptr = ((unsigned char*) dest) + 2 * count ;
20333 while (count)
20334 { count -- ;
20335 ucptr -= 2 ;
20336 scaled_value = src [count] * normfact ;
20337 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF))
20338 { ucptr [0] = 0xFF ;
20339 ucptr [1] = 0x7F ;
20340 continue ;
20342 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
20343 { ucptr [0] = 0x00 ;
20344 ucptr [1] = 0x80 ;
20345 continue ;
20348 value = lrintf (scaled_value) ;
20349 ucptr [0] = value >> 16 ;
20350 ucptr [1] = value >> 24 ;
20352 } /* f2les_clip_array */
20354 static sf_count_t
20355 pcm_write_f2les (SF_PRIVATE *psf, float *ptr, sf_count_t len)
20356 { void (*convert) (float *, short *t, int, int) ;
20357 int bufferlen, writecount, thiswrite ;
20358 sf_count_t total = 0 ;
20360 convert = (psf->add_clipping) ? f2les_clip_array : f2les_array ;
20361 bufferlen = sizeof (psf->buffer) / sizeof (short) ;
20363 while (len > 0)
20364 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
20365 convert (ptr + total, (short*) (psf->buffer), writecount, psf->norm_float) ;
20366 thiswrite = psf_fwrite (psf->buffer, sizeof (short), writecount, psf) ;
20367 total += thiswrite ;
20368 len -= thiswrite ;
20369 if (thiswrite < writecount)
20370 break ;
20373 return total ;
20374 } /* pcm_write_f2les */
20376 /*==============================================================================
20379 static void
20380 f2let_array (float *src, tribyte *dest, int count, int normalize)
20381 { unsigned char *ucptr ;
20382 float normfact ;
20383 int value ;
20385 normfact = normalize ? (1.0 * 0x7FFFFF) : 1.0 ;
20386 ucptr = ((unsigned char*) dest) + 3 * count ;
20388 while (count)
20389 { count -- ;
20390 ucptr -= 3 ;
20391 value = lrintf (src [count] * normfact) ;
20392 ucptr [0] = value ;
20393 ucptr [1] = value >> 8 ;
20394 ucptr [2] = value >> 16 ;
20396 } /* f2let_array */
20398 static void
20399 f2let_clip_array (float *src, tribyte *dest, int count, int normalize)
20400 { unsigned char *ucptr ;
20401 float normfact, scaled_value ;
20402 int value ;
20404 normfact = normalize ? (8.0 * 0x10000000) : (1.0 * 0x100) ;
20405 ucptr = ((unsigned char*) dest) + 3 * count ;
20407 while (count)
20408 { count -- ;
20409 ucptr -= 3 ;
20410 scaled_value = src [count] * normfact ;
20411 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF))
20412 { ucptr [0] = 0xFF ;
20413 ucptr [1] = 0xFF ;
20414 ucptr [2] = 0x7F ;
20415 continue ;
20417 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
20418 { ucptr [0] = 0x00 ;
20419 ucptr [1] = 0x00 ;
20420 ucptr [2] = 0x80 ;
20421 continue ;
20424 value = lrintf (scaled_value) ;
20425 ucptr [0] = value >> 8 ;
20426 ucptr [1] = value >> 16 ;
20427 ucptr [2] = value >> 24 ;
20429 } /* f2let_clip_array */
20431 static sf_count_t
20432 pcm_write_f2let (SF_PRIVATE *psf, float *ptr, sf_count_t len)
20433 { void (*convert) (float *, tribyte *, int, int) ;
20434 int bufferlen, writecount, thiswrite ;
20435 sf_count_t total = 0 ;
20437 convert = (psf->add_clipping) ? f2let_clip_array : f2let_array ;
20438 bufferlen = sizeof (psf->buffer) / SIZEOF_TRIBYTE ;
20440 while (len > 0)
20441 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
20442 convert (ptr + total, (tribyte*) (psf->buffer), writecount, psf->norm_float) ;
20443 thiswrite = psf_fwrite (psf->buffer, SIZEOF_TRIBYTE, writecount, psf) ;
20444 total += thiswrite ;
20445 len -= thiswrite ;
20446 if (thiswrite < writecount)
20447 break ;
20450 return total ;
20451 } /* pcm_write_f2let */
20453 /*==============================================================================
20456 static void
20457 f2bet_array (float *src, tribyte *dest, int count, int normalize)
20458 { unsigned char *ucptr ;
20459 float normfact ;
20460 int value ;
20462 normfact = normalize ? (1.0 * 0x7FFFFF) : 1.0 ;
20463 ucptr = ((unsigned char*) dest) + 3 * count ;
20465 while (count)
20466 { count -- ;
20467 ucptr -= 3 ;
20468 value = lrintf (src [count] * normfact) ;
20469 ucptr [0] = value >> 16 ;
20470 ucptr [1] = value >> 8 ;
20471 ucptr [2] = value ;
20473 } /* f2bet_array */
20475 static void
20476 f2bet_clip_array (float *src, tribyte *dest, int count, int normalize)
20477 { unsigned char *ucptr ;
20478 float normfact, scaled_value ;
20479 int value ;
20481 normfact = normalize ? (8.0 * 0x10000000) : (1.0 * 0x100) ;
20482 ucptr = ((unsigned char*) dest) + 3 * count ;
20484 while (count)
20485 { count -- ;
20486 ucptr -= 3 ;
20487 scaled_value = src [count] * normfact ;
20488 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF))
20489 { ucptr [0] = 0x7F ;
20490 ucptr [1] = 0xFF ;
20491 ucptr [2] = 0xFF ;
20492 continue ;
20494 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
20495 { ucptr [0] = 0x80 ;
20496 ucptr [1] = 0x00 ;
20497 ucptr [2] = 0x00 ;
20498 continue ;
20501 value = lrint (scaled_value) ;
20502 ucptr [0] = value >> 24 ;
20503 ucptr [1] = value >> 16 ;
20504 ucptr [2] = value >> 8 ;
20506 } /* f2bet_clip_array */
20508 static sf_count_t
20509 pcm_write_f2bet (SF_PRIVATE *psf, float *ptr, sf_count_t len)
20510 { void (*convert) (float *, tribyte *, int, int) ;
20511 int bufferlen, writecount, thiswrite ;
20512 sf_count_t total = 0 ;
20514 convert = (psf->add_clipping) ? f2bet_clip_array : f2bet_array ;
20515 bufferlen = sizeof (psf->buffer) / SIZEOF_TRIBYTE ;
20517 while (len > 0)
20518 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
20519 convert (ptr + total, (tribyte*) (psf->buffer), writecount, psf->norm_float) ;
20520 thiswrite = psf_fwrite (psf->buffer, SIZEOF_TRIBYTE, writecount, psf) ;
20521 total += thiswrite ;
20522 len -= thiswrite ;
20523 if (thiswrite < writecount)
20524 break ;
20527 return total ;
20528 } /* pcm_write_f2bet */
20530 /*==============================================================================
20533 static void
20534 f2bei_array (float *src, int *dest, int count, int normalize)
20535 { unsigned char *ucptr ;
20536 float normfact ;
20537 int value ;
20539 normfact = normalize ? (1.0 * 0x7FFFFFFF) : 1.0 ;
20540 ucptr = ((unsigned char*) dest) + 4 * count ;
20541 while (count)
20542 { count -- ;
20543 ucptr -= 4 ;
20544 value = lrintf (src [count] * normfact) ;
20545 ucptr [0] = value >> 24 ;
20546 ucptr [1] = value >> 16 ;
20547 ucptr [2] = value >> 8 ;
20548 ucptr [3] = value ;
20550 } /* f2bei_array */
20552 static void
20553 f2bei_clip_array (float *src, int *dest, int count, int normalize)
20554 { unsigned char *ucptr ;
20555 float normfact, scaled_value ;
20556 int value ;
20558 normfact = normalize ? (8.0 * 0x10000000) : 1.0 ;
20559 ucptr = ((unsigned char*) dest) + 4 * count ;
20561 while (count)
20562 { count -- ;
20563 ucptr -= 4 ;
20564 scaled_value = src [count] * normfact ;
20565 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= 1.0 * 0x7FFFFFFF)
20566 { ucptr [0] = 0x7F ;
20567 ucptr [1] = 0xFF ;
20568 ucptr [2] = 0xFF ;
20569 ucptr [3] = 0xFF ;
20570 continue ;
20572 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
20573 { ucptr [0] = 0x80 ;
20574 ucptr [1] = 0x00 ;
20575 ucptr [2] = 0x00 ;
20576 ucptr [3] = 0x00 ;
20577 continue ;
20580 value = lrintf (scaled_value) ;
20581 ucptr [0] = value >> 24 ;
20582 ucptr [1] = value >> 16 ;
20583 ucptr [2] = value >> 8 ;
20584 ucptr [3] = value ;
20586 } /* f2bei_clip_array */
20588 static sf_count_t
20589 pcm_write_f2bei (SF_PRIVATE *psf, float *ptr, sf_count_t len)
20590 { void (*convert) (float *, int *, int, int) ;
20591 int bufferlen, writecount, thiswrite ;
20592 sf_count_t total = 0 ;
20594 convert = (psf->add_clipping) ? f2bei_clip_array : f2bei_array ;
20595 bufferlen = sizeof (psf->buffer) / sizeof (int) ;
20597 while (len > 0)
20598 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
20599 convert (ptr + total, (int*) (psf->buffer), writecount, psf->norm_float) ;
20600 thiswrite = psf_fwrite (psf->buffer, sizeof (int), writecount, psf) ;
20601 total += thiswrite ;
20602 len -= thiswrite ;
20603 if (thiswrite < writecount)
20604 break ;
20607 return total ;
20608 } /* pcm_write_f2bei */
20610 /*==============================================================================
20613 static void
20614 f2lei_array (float *src, int *dest, int count, int normalize)
20615 { unsigned char *ucptr ;
20616 float normfact ;
20617 int value ;
20619 normfact = normalize ? (1.0 * 0x7FFFFFFF) : 1.0 ;
20620 ucptr = ((unsigned char*) dest) + 4 * count ;
20622 while (count)
20623 { count -- ;
20624 ucptr -= 4 ;
20625 value = lrintf (src [count] * normfact) ;
20626 ucptr [0] = value ;
20627 ucptr [1] = value >> 8 ;
20628 ucptr [2] = value >> 16 ;
20629 ucptr [3] = value >> 24 ;
20631 } /* f2lei_array */
20633 static void
20634 f2lei_clip_array (float *src, int *dest, int count, int normalize)
20635 { unsigned char *ucptr ;
20636 float normfact, scaled_value ;
20637 int value ;
20639 normfact = normalize ? (8.0 * 0x10000000) : 1.0 ;
20640 ucptr = ((unsigned char*) dest) + 4 * count ;
20642 while (count)
20643 { count -- ;
20644 ucptr -= 4 ;
20645 scaled_value = src [count] * normfact ;
20646 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF))
20647 { ucptr [0] = 0xFF ;
20648 ucptr [1] = 0xFF ;
20649 ucptr [2] = 0xFF ;
20650 ucptr [3] = 0x7F ;
20651 continue ;
20653 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
20654 { ucptr [0] = 0x00 ;
20655 ucptr [1] = 0x00 ;
20656 ucptr [2] = 0x00 ;
20657 ucptr [3] = 0x80 ;
20658 continue ;
20661 value = lrintf (scaled_value) ;
20662 ucptr [0] = value ;
20663 ucptr [1] = value >> 8 ;
20664 ucptr [2] = value >> 16 ;
20665 ucptr [3] = value >> 24 ;
20667 } /* f2lei_clip_array */
20669 static sf_count_t
20670 pcm_write_f2lei (SF_PRIVATE *psf, float *ptr, sf_count_t len)
20671 { void (*convert) (float *, int *, int, int) ;
20672 int bufferlen, writecount, thiswrite ;
20673 sf_count_t total = 0 ;
20675 convert = (psf->add_clipping) ? f2lei_clip_array : f2lei_array ;
20676 bufferlen = sizeof (psf->buffer) / sizeof (int) ;
20678 while (len > 0)
20679 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
20680 convert (ptr + total, (int*) (psf->buffer), writecount, psf->norm_float) ;
20681 thiswrite = psf_fwrite (psf->buffer, sizeof (int), writecount, psf) ;
20682 total += thiswrite ;
20683 len -= thiswrite ;
20684 if (thiswrite < writecount)
20685 break ;
20688 return total ;
20689 } /* pcm_write_f2lei */
20691 /*==============================================================================
20694 static void
20695 d2sc_array (double *src, signed char *dest, int count, int normalize)
20696 { double normfact ;
20698 normfact = normalize ? (1.0 * 0x7F) : 1.0 ;
20700 while (count)
20701 { count -- ;
20702 dest [count] = lrint (src [count] * normfact) ;
20704 } /* d2sc_array */
20706 static void
20707 d2sc_clip_array (double *src, signed char *dest, int count, int normalize)
20708 { double normfact, scaled_value ;
20710 normfact = normalize ? (8.0 * 0x10000000) : (1.0 * 0x1000000) ;
20712 while (count)
20713 { count -- ;
20714 scaled_value = src [count] * normfact ;
20715 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF))
20716 { dest [count] = 127 ;
20717 continue ;
20719 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
20720 { dest [count] = -128 ;
20721 continue ;
20724 dest [count] = lrintf (scaled_value) >> 24 ;
20726 } /* d2sc_clip_array */
20728 static sf_count_t
20729 pcm_write_d2sc (SF_PRIVATE *psf, double *ptr, sf_count_t len)
20730 { void (*convert) (double *, signed char *, int, int) ;
20731 int bufferlen, writecount, thiswrite ;
20732 sf_count_t total = 0 ;
20734 convert = (psf->add_clipping) ? d2sc_clip_array : d2sc_array ;
20735 bufferlen = sizeof (psf->buffer) / sizeof (signed char) ;
20737 while (len > 0)
20738 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
20739 convert (ptr + total, (signed char*) (psf->buffer), writecount, psf->norm_double) ;
20740 thiswrite = psf_fwrite (psf->buffer, sizeof (signed char), writecount, psf) ;
20741 total += thiswrite ;
20742 len -= thiswrite ;
20743 if (thiswrite < writecount)
20744 break ;
20747 return total ;
20748 } /* pcm_write_d2sc */
20750 /*==============================================================================
20753 static void
20754 d2uc_array (double *src, unsigned char *dest, int count, int normalize)
20755 { double normfact ;
20757 normfact = normalize ? (1.0 * 0x7F) : 1.0 ;
20759 while (count)
20760 { count -- ;
20761 dest [count] = lrint (src [count] * normfact) + 128 ;
20763 } /* d2uc_array */
20765 static void
20766 d2uc_clip_array (double *src, unsigned char *dest, int count, int normalize)
20767 { double normfact, scaled_value ;
20769 normfact = normalize ? (8.0 * 0x10000000) : (1.0 * 0x1000000) ;
20771 while (count)
20772 { count -- ;
20773 scaled_value = src [count] * normfact ;
20774 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF))
20775 { dest [count] = 255 ;
20776 continue ;
20778 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
20779 { dest [count] = 0 ;
20780 continue ;
20783 dest [count] = (lrint (src [count] * normfact) >> 24) + 128 ;
20785 } /* d2uc_clip_array */
20788 static sf_count_t
20789 pcm_write_d2uc (SF_PRIVATE *psf, double *ptr, sf_count_t len)
20790 { void (*convert) (double *, unsigned char *, int, int) ;
20791 int bufferlen, writecount, thiswrite ;
20792 sf_count_t total = 0 ;
20794 convert = (psf->add_clipping) ? d2uc_clip_array : d2uc_array ;
20795 bufferlen = sizeof (psf->buffer) / sizeof (unsigned char) ;
20797 while (len > 0)
20798 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
20799 convert (ptr + total, (unsigned char*) (psf->buffer), writecount, psf->norm_double) ;
20800 thiswrite = psf_fwrite (psf->buffer, sizeof (unsigned char), writecount, psf) ;
20801 total += thiswrite ;
20802 len -= thiswrite ;
20803 if (thiswrite < writecount)
20804 break ;
20807 return total ;
20808 } /* pcm_write_d2uc */
20810 /*==============================================================================
20813 static void
20814 d2bes_array (double *src, short *dest, int count, int normalize)
20815 { unsigned char *ucptr ;
20816 short value ;
20817 double normfact ;
20819 normfact = normalize ? (1.0 * 0x7FFF) : 1.0 ;
20820 ucptr = ((unsigned char*) dest) + 2 * count ;
20822 while (count)
20823 { count -- ;
20824 ucptr -= 2 ;
20825 value = lrint (src [count] * normfact) ;
20826 ucptr [1] = value ;
20827 ucptr [0] = value >> 8 ;
20829 } /* d2bes_array */
20831 static void
20832 d2bes_clip_array (double *src, short *dest, int count, int normalize)
20833 { unsigned char *ucptr ;
20834 double normfact, scaled_value ;
20835 int value ;
20837 normfact = normalize ? (8.0 * 0x10000000) : (1.0 * 0x10000) ;
20838 ucptr = ((unsigned char*) dest) + 2 * count ;
20840 while (count)
20841 { count -- ;
20842 ucptr -= 2 ;
20843 scaled_value = src [count] * normfact ;
20844 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF))
20845 { ucptr [1] = 0xFF ;
20846 ucptr [0] = 0x7F ;
20847 continue ;
20849 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
20850 { ucptr [1] = 0x00 ;
20851 ucptr [0] = 0x80 ;
20852 continue ;
20855 value = lrint (scaled_value) ;
20856 ucptr [1] = value >> 16 ;
20857 ucptr [0] = value >> 24 ;
20859 } /* d2bes_clip_array */
20861 static sf_count_t
20862 pcm_write_d2bes (SF_PRIVATE *psf, double *ptr, sf_count_t len)
20863 { void (*convert) (double *, short *, int, int) ;
20864 int bufferlen, writecount, thiswrite ;
20865 sf_count_t total = 0 ;
20867 convert = (psf->add_clipping) ? d2bes_clip_array : d2bes_array ;
20868 bufferlen = sizeof (psf->buffer) / sizeof (short) ;
20870 while (len > 0)
20871 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
20872 convert (ptr + total, (short*) (psf->buffer), writecount, psf->norm_double) ;
20873 thiswrite = psf_fwrite (psf->buffer, sizeof (short), writecount, psf) ;
20874 total += thiswrite ;
20875 len -= thiswrite ;
20876 if (thiswrite < writecount)
20877 break ;
20880 return total ;
20881 } /* pcm_write_d2bes */
20883 /*==============================================================================
20886 static void
20887 d2les_array (double *src, short *dest, int count, int normalize)
20888 { unsigned char *ucptr ;
20889 short value ;
20890 double normfact ;
20892 normfact = normalize ? (1.0 * 0x7FFF) : 1.0 ;
20893 ucptr = ((unsigned char*) dest) + 2 * count ;
20895 while (count)
20896 { count -- ;
20897 ucptr -= 2 ;
20898 value = lrint (src [count] * normfact) ;
20899 ucptr [0] = value ;
20900 ucptr [1] = value >> 8 ;
20902 } /* d2les_array */
20904 static void
20905 d2les_clip_array (double *src, short *dest, int count, int normalize)
20906 { unsigned char *ucptr ;
20907 int value ;
20908 double normfact, scaled_value ;
20910 normfact = normalize ? (8.0 * 0x10000000) : (1.0 * 0x10000) ;
20911 ucptr = ((unsigned char*) dest) + 2 * count ;
20913 while (count)
20914 { count -- ;
20915 ucptr -= 2 ;
20916 scaled_value = src [count] * normfact ;
20917 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF))
20918 { ucptr [0] = 0xFF ;
20919 ucptr [1] = 0x7F ;
20920 continue ;
20922 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
20923 { ucptr [0] = 0x00 ;
20924 ucptr [1] = 0x80 ;
20925 continue ;
20928 value = lrint (scaled_value) ;
20929 ucptr [0] = value >> 16 ;
20930 ucptr [1] = value >> 24 ;
20932 } /* d2les_clip_array */
20934 static sf_count_t
20935 pcm_write_d2les (SF_PRIVATE *psf, double *ptr, sf_count_t len)
20936 { void (*convert) (double *, short *, int, int) ;
20937 int bufferlen, writecount, thiswrite ;
20938 sf_count_t total = 0 ;
20940 convert = (psf->add_clipping) ? d2les_clip_array : d2les_array ;
20941 bufferlen = sizeof (psf->buffer) / sizeof (short) ;
20943 while (len > 0)
20944 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
20945 convert (ptr + total, (short*) (psf->buffer), writecount, psf->norm_double) ;
20946 thiswrite = psf_fwrite (psf->buffer, sizeof (short), writecount, psf) ;
20947 total += thiswrite ;
20948 len -= thiswrite ;
20949 if (thiswrite < writecount)
20950 break ;
20953 return total ;
20954 } /* pcm_write_d2les */
20956 /*==============================================================================
20959 static void
20960 d2let_array (double *src, tribyte *dest, int count, int normalize)
20961 { unsigned char *ucptr ;
20962 int value ;
20963 double normfact ;
20965 normfact = normalize ? (1.0 * 0x7FFFFF) : 1.0 ;
20966 ucptr = ((unsigned char*) dest) + 3 * count ;
20968 while (count)
20969 { count -- ;
20970 ucptr -= 3 ;
20971 value = lrint (src [count] * normfact) ;
20972 ucptr [0] = value ;
20973 ucptr [1] = value >> 8 ;
20974 ucptr [2] = value >> 16 ;
20976 } /* d2let_array */
20978 static void
20979 d2let_clip_array (double *src, tribyte *dest, int count, int normalize)
20980 { unsigned char *ucptr ;
20981 int value ;
20982 double normfact, scaled_value ;
20984 normfact = normalize ? (8.0 * 0x10000000) : (1.0 * 0x100) ;
20985 ucptr = ((unsigned char*) dest) + 3 * count ;
20987 while (count)
20988 { count -- ;
20989 ucptr -= 3 ;
20990 scaled_value = src [count] * normfact ;
20991 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF))
20992 { ucptr [0] = 0xFF ;
20993 ucptr [1] = 0xFF ;
20994 ucptr [2] = 0x7F ;
20995 continue ;
20997 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
20998 { ucptr [0] = 0x00 ;
20999 ucptr [1] = 0x00 ;
21000 ucptr [2] = 0x80 ;
21001 continue ;
21004 value = lrint (scaled_value) ;
21005 ucptr [0] = value >> 8 ;
21006 ucptr [1] = value >> 16 ;
21007 ucptr [2] = value >> 24 ;
21009 } /* d2let_clip_array */
21011 static sf_count_t
21012 pcm_write_d2let (SF_PRIVATE *psf, double *ptr, sf_count_t len)
21013 { void (*convert) (double *, tribyte *, int, int) ;
21014 int bufferlen, writecount, thiswrite ;
21015 sf_count_t total = 0 ;
21017 convert = (psf->add_clipping) ? d2let_clip_array : d2let_array ;
21018 bufferlen = sizeof (psf->buffer) / SIZEOF_TRIBYTE ;
21020 while (len > 0)
21021 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
21022 convert (ptr + total, (tribyte*) (psf->buffer), writecount, psf->norm_double) ;
21023 thiswrite = psf_fwrite (psf->buffer, SIZEOF_TRIBYTE, writecount, psf) ;
21024 total += thiswrite ;
21025 len -= thiswrite ;
21026 if (thiswrite < writecount)
21027 break ;
21030 return total ;
21031 } /* pcm_write_d2let */
21033 /*==============================================================================
21036 static void
21037 d2bet_array (double *src, tribyte *dest, int count, int normalize)
21038 { unsigned char *ucptr ;
21039 int value ;
21040 double normfact ;
21042 normfact = normalize ? (1.0 * 0x7FFFFF) : 1.0 ;
21043 ucptr = ((unsigned char*) dest) + 3 * count ;
21045 while (count)
21046 { count -- ;
21047 ucptr -= 3 ;
21048 value = lrint (src [count] * normfact) ;
21049 ucptr [2] = value ;
21050 ucptr [1] = value >> 8 ;
21051 ucptr [0] = value >> 16 ;
21053 } /* d2bet_array */
21055 static void
21056 d2bet_clip_array (double *src, tribyte *dest, int count, int normalize)
21057 { unsigned char *ucptr ;
21058 int value ;
21059 double normfact, scaled_value ;
21061 normfact = normalize ? (8.0 * 0x10000000) : (1.0 * 0x100) ;
21062 ucptr = ((unsigned char*) dest) + 3 * count ;
21064 while (count)
21065 { count -- ;
21066 ucptr -= 3 ;
21067 scaled_value = src [count] * normfact ;
21068 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF))
21069 { ucptr [2] = 0xFF ;
21070 ucptr [1] = 0xFF ;
21071 ucptr [0] = 0x7F ;
21072 continue ;
21074 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
21075 { ucptr [2] = 0x00 ;
21076 ucptr [1] = 0x00 ;
21077 ucptr [0] = 0x80 ;
21078 continue ;
21081 value = lrint (scaled_value) ;
21082 ucptr [2] = value >> 8 ;
21083 ucptr [1] = value >> 16 ;
21084 ucptr [0] = value >> 24 ;
21086 } /* d2bet_clip_array */
21088 static sf_count_t
21089 pcm_write_d2bet (SF_PRIVATE *psf, double *ptr, sf_count_t len)
21090 { void (*convert) (double *, tribyte *, int, int) ;
21091 int bufferlen, writecount, thiswrite ;
21092 sf_count_t total = 0 ;
21094 convert = (psf->add_clipping) ? d2bet_clip_array : d2bet_array ;
21095 bufferlen = sizeof (psf->buffer) / SIZEOF_TRIBYTE ;
21097 while (len > 0)
21098 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
21099 convert (ptr + total, (tribyte*) (psf->buffer), writecount, psf->norm_double) ;
21100 thiswrite = psf_fwrite (psf->buffer, SIZEOF_TRIBYTE, writecount, psf) ;
21101 total += thiswrite ;
21102 len -= thiswrite ;
21103 if (thiswrite < writecount)
21104 break ;
21107 return total ;
21108 } /* pcm_write_d2bet */
21110 /*==============================================================================
21113 static void
21114 d2bei_array (double *src, int *dest, int count, int normalize)
21115 { unsigned char *ucptr ;
21116 int value ;
21117 double normfact ;
21119 normfact = normalize ? (1.0 * 0x7FFFFFFF) : 1.0 ;
21120 ucptr = ((unsigned char*) dest) + 4 * count ;
21122 while (count)
21123 { count -- ;
21124 ucptr -= 4 ;
21125 value = lrint (src [count] * normfact) ;
21126 ucptr [0] = value >> 24 ;
21127 ucptr [1] = value >> 16 ;
21128 ucptr [2] = value >> 8 ;
21129 ucptr [3] = value ;
21131 } /* d2bei_array */
21133 static void
21134 d2bei_clip_array (double *src, int *dest, int count, int normalize)
21135 { unsigned char *ucptr ;
21136 int value ;
21137 double normfact, scaled_value ;
21139 normfact = normalize ? (8.0 * 0x10000000) : 1.0 ;
21140 ucptr = ((unsigned char*) dest) + 4 * count ;
21142 while (count)
21143 { count -- ;
21144 ucptr -= 4 ;
21145 scaled_value = src [count] * normfact ;
21146 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF))
21147 { ucptr [3] = 0xFF ;
21148 ucptr [2] = 0xFF ;
21149 ucptr [1] = 0xFF ;
21150 ucptr [0] = 0x7F ;
21151 continue ;
21153 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
21154 { ucptr [3] = 0x00 ;
21155 ucptr [2] = 0x00 ;
21156 ucptr [1] = 0x00 ;
21157 ucptr [0] = 0x80 ;
21158 continue ;
21161 value = lrint (scaled_value) ;
21162 ucptr [0] = value >> 24 ;
21163 ucptr [1] = value >> 16 ;
21164 ucptr [2] = value >> 8 ;
21165 ucptr [3] = value ;
21167 } /* d2bei_clip_array */
21169 static sf_count_t
21170 pcm_write_d2bei (SF_PRIVATE *psf, double *ptr, sf_count_t len)
21171 { void (*convert) (double *, int *, int, int) ;
21172 int bufferlen, writecount, thiswrite ;
21173 sf_count_t total = 0 ;
21175 convert = (psf->add_clipping) ? d2bei_clip_array : d2bei_array ;
21176 bufferlen = sizeof (psf->buffer) / sizeof (int) ;
21178 while (len > 0)
21179 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
21180 convert (ptr + total, (int*) (psf->buffer), writecount, psf->norm_double) ;
21181 thiswrite = psf_fwrite (psf->buffer, sizeof (int), writecount, psf) ;
21182 total += thiswrite ;
21183 len -= thiswrite ;
21184 if (thiswrite < writecount)
21185 break ;
21188 return total ;
21189 } /* pcm_write_d2bei */
21191 /*==============================================================================
21194 static void
21195 d2lei_array (double *src, int *dest, int count, int normalize)
21196 { unsigned char *ucptr ;
21197 int value ;
21198 double normfact ;
21200 normfact = normalize ? (1.0 * 0x7FFFFFFF) : 1.0 ;
21201 ucptr = ((unsigned char*) dest) + 4 * count ;
21203 while (count)
21204 { count -- ;
21205 ucptr -= 4 ;
21206 value = lrint (src [count] * normfact) ;
21207 ucptr [0] = value ;
21208 ucptr [1] = value >> 8 ;
21209 ucptr [2] = value >> 16 ;
21210 ucptr [3] = value >> 24 ;
21212 } /* d2lei_array */
21214 static void
21215 d2lei_clip_array (double *src, int *dest, int count, int normalize)
21216 { unsigned char *ucptr ;
21217 int value ;
21218 double normfact, scaled_value ;
21220 normfact = normalize ? (8.0 * 0x10000000) : 1.0 ;
21221 ucptr = ((unsigned char*) dest) + 4 * count ;
21223 while (count)
21224 { count -- ;
21225 ucptr -= 4 ;
21226 scaled_value = src [count] * normfact ;
21227 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF))
21228 { ucptr [0] = 0xFF ;
21229 ucptr [1] = 0xFF ;
21230 ucptr [2] = 0xFF ;
21231 ucptr [3] = 0x7F ;
21232 continue ;
21234 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
21235 { ucptr [0] = 0x00 ;
21236 ucptr [1] = 0x00 ;
21237 ucptr [2] = 0x00 ;
21238 ucptr [3] = 0x80 ;
21239 continue ;
21242 value = lrint (scaled_value) ;
21243 ucptr [0] = value ;
21244 ucptr [1] = value >> 8 ;
21245 ucptr [2] = value >> 16 ;
21246 ucptr [3] = value >> 24 ;
21248 } /* d2lei_clip_array */
21250 static sf_count_t
21251 pcm_write_d2lei (SF_PRIVATE *psf, double *ptr, sf_count_t len)
21252 { void (*convert) (double *, int *, int, int) ;
21253 int bufferlen, writecount, thiswrite ;
21254 sf_count_t total = 0 ;
21256 convert = (psf->add_clipping) ? d2lei_clip_array : d2lei_array ;
21257 bufferlen = sizeof (psf->buffer) / sizeof (int) ;
21259 while (len > 0)
21260 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
21261 convert (ptr + total, (int*) (psf->buffer), writecount, psf->norm_double) ;
21262 thiswrite = psf_fwrite (psf->buffer, sizeof (int), writecount, psf) ;
21263 total += thiswrite ;
21264 len -= thiswrite ;
21265 if (thiswrite < writecount)
21266 break ;
21269 return total ;
21270 } /* pcm_write_d2lei */
21272 /*==============================================================================
21275 static void
21276 sc2s_array (signed char *src, int count, short *dest)
21277 { while (count)
21278 { count -- ;
21279 dest [count] = src [count] << 8 ;
21281 } /* sc2s_array */
21283 static void
21284 uc2s_array (unsigned char *src, int count, short *dest)
21285 { while (count)
21286 { count -- ;
21287 dest [count] = (((short) src [count]) - 0x80) << 8 ;
21289 } /* uc2s_array */
21291 static void
21292 let2s_array (tribyte *src, int count, short *dest)
21293 { unsigned char *ucptr ;
21295 ucptr = ((unsigned char*) src) + 3 * count ;
21296 while (count)
21297 { count -- ;
21298 ucptr -= 3 ;
21299 dest [count] = LET2H_SHORT_PTR (ucptr) ;
21301 } /* let2s_array */
21303 static void
21304 bet2s_array (tribyte *src, int count, short *dest)
21305 { unsigned char *ucptr ;
21307 ucptr = ((unsigned char*) src) + 3 * count ;
21308 while (count)
21309 { count -- ;
21310 ucptr -= 3 ;
21311 dest [count] = BET2H_SHORT_PTR (ucptr) ;
21313 } /* bet2s_array */
21315 static void
21316 lei2s_array (int *src, int count, short *dest)
21317 { unsigned char *ucptr ;
21319 ucptr = ((unsigned char*) src) + 4 * count ;
21320 while (count)
21321 { count -- ;
21322 ucptr -= 4 ;
21323 dest [count] = LEI2H_SHORT_PTR (ucptr) ;
21325 } /* lei2s_array */
21327 static void
21328 bei2s_array (int *src, int count, short *dest)
21329 { unsigned char *ucptr ;
21331 ucptr = ((unsigned char*) src) + 4 * count ;
21332 while (count)
21333 { count -- ;
21334 ucptr -= 4 ;
21335 dest [count] = BEI2H_SHORT_PTR (ucptr) ;
21337 } /* bei2s_array */
21339 /*-----------------------------------------------------------------------------------------------
21342 static void
21343 sc2i_array (signed char *src, int count, int *dest)
21344 { while (count)
21345 { count -- ;
21346 dest [count] = ((int) src [count]) << 24 ;
21348 } /* sc2i_array */
21350 static void
21351 uc2i_array (unsigned char *src, int count, int *dest)
21352 { while (count)
21353 { count -- ;
21354 dest [count] = (((int) src [count]) - 128) << 24 ;
21356 } /* uc2i_array */
21358 static void
21359 bes2i_array (short *src, int count, int *dest)
21360 { unsigned char *ucptr ;
21362 ucptr = ((unsigned char*) src) + 2 * count ;
21363 while (count)
21364 { count -- ;
21365 ucptr -= 2 ;
21366 dest [count] = BES2H_INT_PTR (ucptr) ;
21368 } /* bes2i_array */
21370 static void
21371 les2i_array (short *src, int count, int *dest)
21372 { unsigned char *ucptr ;
21374 ucptr = ((unsigned char*) src) + 2 * count ;
21375 while (count)
21376 { count -- ;
21377 ucptr -= 2 ;
21378 dest [count] = LES2H_INT_PTR (ucptr) ;
21380 } /* les2i_array */
21382 static void
21383 bet2i_array (tribyte *src, int count, int *dest)
21384 { unsigned char *ucptr ;
21386 ucptr = ((unsigned char*) src) + 3 * count ;
21387 while (count)
21388 { count -- ;
21389 ucptr -= 3 ;
21390 dest [count] = BET2H_INT_PTR (ucptr) ;
21392 } /* bet2i_array */
21394 static void
21395 let2i_array (tribyte *src, int count, int *dest)
21396 { unsigned char *ucptr ;
21398 ucptr = ((unsigned char*) src) + 3 * count ;
21399 while (count)
21400 { count -- ;
21401 ucptr -= 3 ;
21402 dest [count] = LET2H_INT_PTR (ucptr) ;
21404 } /* let2i_array */
21406 /*-----------------------------------------------------------------------------------------------
21410 static void
21411 sc2f_array (signed char *src, int count, float *dest, float normfact)
21412 { while (count)
21413 { count -- ;
21414 dest [count] = ((float) src [count]) * normfact ;
21416 } /* sc2f_array */
21418 static void
21419 uc2f_array (unsigned char *src, int count, float *dest, float normfact)
21420 { while (count)
21421 { count -- ;
21422 dest [count] = (((float) src [count]) - 128.0) * normfact ;
21424 } /* uc2f_array */
21426 static void
21427 les2f_array (short *src, int count, float *dest, float normfact)
21428 { short value ;
21430 while (count)
21431 { count -- ;
21432 value = src [count] ;
21433 value = LES2H_SHORT (value) ;
21434 dest [count] = ((float) value) * normfact ;
21436 } /* les2f_array */
21438 static void
21439 bes2f_array (short *src, int count, float *dest, float normfact)
21440 { short value ;
21442 while (count)
21443 { count -- ;
21444 value = src [count] ;
21445 value = BES2H_SHORT (value) ;
21446 dest [count] = ((float) value) * normfact ;
21448 } /* bes2f_array */
21450 static void
21451 let2f_array (tribyte *src, int count, float *dest, float normfact)
21452 { unsigned char *ucptr ;
21453 int value ;
21455 ucptr = ((unsigned char*) src) + 3 * count ;
21456 while (count)
21457 { count -- ;
21458 ucptr -= 3 ;
21459 value = LET2H_INT_PTR (ucptr) ;
21460 dest [count] = ((float) value) * normfact ;
21462 } /* let2f_array */
21464 static void
21465 bet2f_array (tribyte *src, int count, float *dest, float normfact)
21466 { unsigned char *ucptr ;
21467 int value ;
21469 ucptr = ((unsigned char*) src) + 3 * count ;
21470 while (count)
21471 { count -- ;
21472 ucptr -= 3 ;
21473 value = BET2H_INT_PTR (ucptr) ;
21474 dest [count] = ((float) value) * normfact ;
21476 } /* bet2f_array */
21478 static void
21479 lei2f_array (int *src, int count, float *dest, float normfact)
21480 { int value ;
21482 while (count)
21483 { count -- ;
21484 value = src [count] ;
21485 value = LEI2H_INT (value) ;
21486 dest [count] = ((float) value) * normfact ;
21488 } /* lei2f_array */
21490 static void
21491 bei2f_array (int *src, int count, float *dest, float normfact)
21492 { int value ;
21494 while (count)
21495 { count -- ;
21496 value = src [count] ;
21497 value = BEI2H_INT (value) ;
21498 dest [count] = ((float) value) * normfact ;
21500 } /* bei2f_array */
21502 /*-----------------------------------------------------------------------------------------------
21505 static void
21506 sc2d_array (signed char *src, int count, double *dest, double normfact)
21507 { while (count)
21508 { count -- ;
21509 dest [count] = ((double) src [count]) * normfact ;
21511 } /* sc2d_array */
21513 static void
21514 uc2d_array (unsigned char *src, int count, double *dest, double normfact)
21515 { while (count)
21516 { count -- ;
21517 dest [count] = (((double) src [count]) - 128.0) * normfact ;
21519 } /* uc2d_array */
21521 static void
21522 les2d_array (short *src, int count, double *dest, double normfact)
21523 { short value ;
21525 while (count)
21526 { count -- ;
21527 value = src [count] ;
21528 value = LES2H_SHORT (value) ;
21529 dest [count] = ((double) value) * normfact ;
21531 } /* les2d_array */
21533 static void
21534 bes2d_array (short *src, int count, double *dest, double normfact)
21535 { short value ;
21537 while (count)
21538 { count -- ;
21539 value = src [count] ;
21540 value = BES2H_SHORT (value) ;
21541 dest [count] = ((double) value) * normfact ;
21543 } /* bes2d_array */
21545 static void
21546 let2d_array (tribyte *src, int count, double *dest, double normfact)
21547 { unsigned char *ucptr ;
21548 int value ;
21550 ucptr = ((unsigned char*) src) + 3 * count ;
21551 while (count)
21552 { count -- ;
21553 ucptr -= 3 ;
21554 value = LET2H_INT_PTR (ucptr) ;
21555 dest [count] = ((double) value) * normfact ;
21557 } /* let2d_array */
21559 static void
21560 bet2d_array (tribyte *src, int count, double *dest, double normfact)
21561 { unsigned char *ucptr ;
21562 int value ;
21564 ucptr = ((unsigned char*) src) + 3 * count ;
21565 while (count)
21566 { count -- ;
21567 ucptr -= 3 ;
21568 value = (ucptr [0] << 24) | (ucptr [1] << 16) | (ucptr [2] << 8) ;
21569 dest [count] = ((double) value) * normfact ;
21571 } /* bet2d_array */
21573 static void
21574 lei2d_array (int *src, int count, double *dest, double normfact)
21575 { int value ;
21577 while (count)
21578 { count -- ;
21579 value = src [count] ;
21580 value = LEI2H_INT (value) ;
21581 dest [count] = ((double) value) * normfact ;
21583 } /* lei2d_array */
21585 static void
21586 bei2d_array (int *src, int count, double *dest, double normfact)
21587 { int value ;
21589 while (count)
21590 { count -- ;
21591 value = src [count] ;
21592 value = BEI2H_INT (value) ;
21593 dest [count] = ((double) value) * normfact ;
21595 } /* bei2d_array */
21597 /*-----------------------------------------------------------------------------------------------
21600 static void
21601 s2sc_array (short *src, signed char *dest, int count)
21602 { while (count)
21603 { count -- ;
21604 dest [count] = src [count] >> 8 ;
21606 } /* s2sc_array */
21608 static void
21609 s2uc_array (short *src, unsigned char *dest, int count)
21610 { while (count)
21611 { count -- ;
21612 dest [count] = (src [count] >> 8) + 0x80 ;
21614 } /* s2uc_array */
21616 static void
21617 s2let_array (short *src, tribyte *dest, int count)
21618 { unsigned char *ucptr ;
21620 ucptr = ((unsigned char*) dest) + 3 * count ;
21621 while (count)
21622 { count -- ;
21623 ucptr -= 3 ;
21624 ucptr [0] = 0 ;
21625 ucptr [1] = src [count] ;
21626 ucptr [2] = src [count] >> 8 ;
21628 } /* s2let_array */
21630 static void
21631 s2bet_array (short *src, tribyte *dest, int count)
21632 { unsigned char *ucptr ;
21634 ucptr = ((unsigned char*) dest) + 3 * count ;
21635 while (count)
21636 { count -- ;
21637 ucptr -= 3 ;
21638 ucptr [2] = 0 ;
21639 ucptr [1] = src [count] ;
21640 ucptr [0] = src [count] >> 8 ;
21642 } /* s2bet_array */
21644 static void
21645 s2lei_array (short *src, int *dest, int count)
21646 { unsigned char *ucptr ;
21648 ucptr = ((unsigned char*) dest) + 4 * count ;
21649 while (count)
21650 { count -- ;
21651 ucptr -= 4 ;
21652 ucptr [0] = 0 ;
21653 ucptr [1] = 0 ;
21654 ucptr [2] = src [count] ;
21655 ucptr [3] = src [count] >> 8 ;
21657 } /* s2lei_array */
21659 static void
21660 s2bei_array (short *src, int *dest, int count)
21661 { unsigned char *ucptr ;
21663 ucptr = ((unsigned char*) dest) + 4 * count ;
21664 while (count)
21665 { count -- ;
21666 ucptr -= 4 ;
21667 ucptr [0] = src [count] >> 8 ;
21668 ucptr [1] = src [count] ;
21669 ucptr [2] = 0 ;
21670 ucptr [3] = 0 ;
21672 } /* s2bei_array */
21674 /*-----------------------------------------------------------------------------------------------
21677 static void
21678 i2sc_array (int *src, signed char *dest, int count)
21679 { while (count)
21680 { count -- ;
21681 dest [count] = (src [count] >> 24) ;
21683 } /* i2sc_array */
21685 static void
21686 i2uc_array (int *src, unsigned char *dest, int count)
21687 { while (count)
21688 { count -- ;
21689 dest [count] = ((src [count] >> 24) + 128) ;
21691 } /* i2uc_array */
21693 static void
21694 i2bes_array (int *src, short *dest, int count)
21695 { unsigned char *ucptr ;
21697 ucptr = ((unsigned char*) dest) + 2 * count ;
21698 while (count)
21699 { count -- ;
21700 ucptr -= 2 ;
21701 ucptr [0] = src [count] >> 24 ;
21702 ucptr [1] = src [count] >> 16 ;
21704 } /* i2bes_array */
21706 static void
21707 i2les_array (int *src, short *dest, int count)
21708 { unsigned char *ucptr ;
21710 ucptr = ((unsigned char*) dest) + 2 * count ;
21711 while (count)
21712 { count -- ;
21713 ucptr -= 2 ;
21714 ucptr [0] = src [count] >> 16 ;
21715 ucptr [1] = src [count] >> 24 ;
21717 } /* i2les_array */
21719 static void
21720 i2let_array (int *src, tribyte *dest, int count)
21721 { unsigned char *ucptr ;
21722 int value ;
21724 ucptr = ((unsigned char*) dest) + 3 * count ;
21725 while (count)
21726 { count -- ;
21727 ucptr -= 3 ;
21728 value = src [count] >> 8 ;
21729 ucptr [0] = value ;
21730 ucptr [1] = value >> 8 ;
21731 ucptr [2] = value >> 16 ;
21733 } /* i2let_array */
21735 static void
21736 i2bet_array (int *src, tribyte *dest, int count)
21737 { unsigned char *ucptr ;
21738 int value ;
21740 ucptr = ((unsigned char*) dest) + 3 * count ;
21741 while (count)
21742 { count -- ;
21743 ucptr -= 3 ;
21744 value = src [count] >> 8 ;
21745 ucptr [2] = value ;
21746 ucptr [1] = value >> 8 ;
21747 ucptr [0] = value >> 16 ;
21749 } /* i2bet_array */
21751 /*-----------------------------------------------------------------------------------------------
21754 /*-----------------------------------------------------------------------------------------------
21757 /*==============================================================================
21760 ** Do not edit or modify anything in this comment block.
21761 ** The arch-tag line is a file identity tag for the GNU Arch
21762 ** revision control system.
21764 ** arch-tag: d8bc7c0e-1e2f-4ff3-a28f-10ce1fbade3b
21767 * Copyright 1992 by Jutta Degener and Carsten Bormann, Technische
21768 * Universitaet Berlin. See the accompanying file "COPYRIGHT" for
21769 * details. THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE.
21772 #include <stdio.h>
21773 #include <assert.h>
21777 /* 4.2.0 .. 4.2.3 PREPROCESSING SECTION
21779 * After A-law to linear conversion (or directly from the
21780 * Ato D converter) the following scaling is assumed for
21781 * input to the RPE-LTP algorithm:
21783 * in: 0.1.....................12
21784 * S.v.v.v.v.v.v.v.v.v.v.v.v.*.*.*
21786 * Where S is the sign bit, v a valid bit, and * a "don't care" bit.
21787 * The original signal is called sop[..]
21789 * out: 0.1................... 12
21790 * S.S.v.v.v.v.v.v.v.v.v.v.v.v.0.0
21794 void Gsm_Preprocess (
21795 struct gsm_state * S,
21796 word * s,
21797 word * so ) /* [0..159] IN/OUT */
21800 word z1 = S->z1;
21801 longword L_z2 = S->L_z2;
21802 word mp = S->mp;
21804 word s1;
21805 longword L_s2;
21807 longword L_temp;
21809 word msp, lsp;
21810 word SO;
21812 register int k = 160;
21814 while (k--) {
21816 /* 4.2.1 Downscaling of the input signal
21818 SO = SASR_W( *s, 3 ) << 2;
21819 s++;
21821 assert (SO >= -0x4000); /* downscaled by */
21822 assert (SO <= 0x3FFC); /* previous routine. */
21825 /* 4.2.2 Offset compensation
21827 * This part implements a high-pass filter and requires extended
21828 * arithmetic precision for the recursive part of this filter.
21829 * The input of this procedure is the array so[0...159] and the
21830 * output the array sof[ 0...159 ].
21832 /* Compute the non-recursive part
21835 s1 = SO - z1; /* s1 = gsm_sub( *so, z1 ); */
21836 z1 = SO;
21838 assert(s1 != MIN_WORD);
21840 /* Compute the recursive part
21842 L_s2 = s1;
21843 L_s2 <<= 15;
21845 /* Execution of a 31 bv 16 bits multiplication
21848 msp = SASR_L( L_z2, 15 );
21849 lsp = L_z2-((longword)msp<<15); /* gsm_L_sub(L_z2,(msp<<15)); */
21851 L_s2 += GSM_MULT_R( lsp, 32735 );
21852 L_temp = (longword)msp * 32735; /* GSM_L_MULT(msp,32735) >> 1;*/
21853 L_z2 = GSM_L_ADD( L_temp, L_s2 );
21855 /* Compute sof[k] with rounding
21857 L_temp = GSM_L_ADD( L_z2, 16384 );
21859 /* 4.2.3 Preemphasis
21862 msp = GSM_MULT_R( mp, -28180 );
21863 mp = SASR_L( L_temp, 15 );
21864 *so++ = GSM_ADD( mp, msp );
21867 S->z1 = z1;
21868 S->L_z2 = L_z2;
21869 S->mp = mp;
21872 ** Do not edit or modify anything in this comment block.
21873 ** The arch-tag line is a file identity tag for the GNU Arch
21874 ** revision control system.
21876 ** arch-tag: b760b0d9-3a05-4da3-9dc9-441ffb905d87
21880 ** Copyright (C) 2002-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
21882 ** This program is free software; you can redistribute it and/or modify
21883 ** it under the terms of the GNU Lesser General Public License as published by
21884 ** the Free Software Foundation; either version 2.1 of the License, or
21885 ** (at your option) any later version.
21887 ** This program is distributed in the hope that it will be useful,
21888 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
21889 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21890 ** GNU Lesser General Public License for more details.
21892 ** You should have received a copy of the GNU Lesser General Public License
21893 ** along with this program; if not, write to the Free Software
21894 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21897 #include <stdio.h>
21898 #include <fcntl.h>
21899 #include <string.h>
21900 #include <ctype.h>
21903 /*------------------------------------------------------------------------------
21904 ** Macros to handle big/little endian issues.
21907 #define PVF1_MARKER (MAKE_MARKER ('P', 'V', 'F', '1'))
21909 /*------------------------------------------------------------------------------
21910 ** Private static functions.
21913 static int pvf_close (SF_PRIVATE *psf) ;
21915 static int pvf_write_header (SF_PRIVATE *psf, int calc_length) ;
21916 static int pvf_read_header (SF_PRIVATE *psf) ;
21918 /*------------------------------------------------------------------------------
21919 ** Public function.
21923 pvf_open (SF_PRIVATE *psf)
21924 { int subformat ;
21925 int error = 0 ;
21927 if (psf->mode == SFM_READ || (psf->mode == SFM_RDWR && psf->filelength > 0))
21928 { if ((error = pvf_read_header (psf)))
21929 return error ;
21932 subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
21934 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
21935 { if ((psf->sf.format & SF_FORMAT_TYPEMASK) != SF_FORMAT_PVF)
21936 return SFE_BAD_OPEN_FORMAT ;
21938 psf->endian = SF_ENDIAN_BIG ;
21940 if (pvf_write_header (psf, SF_FALSE))
21941 return psf->error ;
21943 psf->write_header = pvf_write_header ;
21946 psf->close = pvf_close ;
21948 psf->blockwidth = psf->bytewidth * psf->sf.channels ;
21950 switch (subformat)
21951 { case SF_FORMAT_PCM_S8 : /* 8-bit linear PCM. */
21952 case SF_FORMAT_PCM_16 : /* 16-bit linear PCM. */
21953 case SF_FORMAT_PCM_32 : /* 32-bit linear PCM. */
21954 error = pcm_init (psf) ;
21955 break ;
21957 default : break ;
21960 return error ;
21961 } /* pvf_open */
21963 /*------------------------------------------------------------------------------
21966 static int
21967 pvf_close (SF_PRIVATE *psf)
21969 psf = psf ;
21971 return 0 ;
21972 } /* pvf_close */
21974 static int
21975 pvf_write_header (SF_PRIVATE *psf, int calc_length)
21976 { sf_count_t current ;
21978 if (psf->pipeoffset > 0)
21979 return 0 ;
21981 calc_length = calc_length ; /* Avoid a compiler warning. */
21983 current = psf_ftell (psf) ;
21985 /* Reset the current header length to zero. */
21986 psf->header [0] = 0 ;
21987 psf->headindex = 0 ;
21989 if (psf->is_pipe == SF_FALSE)
21990 psf_fseek (psf, 0, SEEK_SET) ;
21992 LSF_SNPRINTF ((char*) psf->header, sizeof (psf->header), "PVF1\n%d %d %d\n",
21993 psf->sf.channels, psf->sf.samplerate, psf->bytewidth * 8) ;
21995 psf->headindex = strlen ((char*) psf->header) ;
21997 /* Header construction complete so write it out. */
21998 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
22000 if (psf->error)
22001 return psf->error ;
22003 psf->dataoffset = psf->headindex ;
22005 if (current > 0)
22006 psf_fseek (psf, current, SEEK_SET) ;
22008 return psf->error ;
22009 } /* pvf_write_header */
22011 static int
22012 pvf_read_header (SF_PRIVATE *psf)
22013 { char buffer [32] ;
22014 int marker, channels, samplerate, bitwidth ;
22016 psf_binheader_readf (psf, "pmj", 0, &marker, 1) ;
22017 psf_log_printf (psf, "%M\n", marker) ;
22019 if (marker != PVF1_MARKER)
22020 return SFE_PVF_NO_PVF1 ;
22022 /* Grab characters up until a newline which is replaced by an EOS. */
22023 psf_binheader_readf (psf, "G", buffer, sizeof (buffer)) ;
22025 if (sscanf (buffer, "%d %d %d", &channels, &samplerate, &bitwidth) != 3)
22026 return SFE_PVF_BAD_HEADER ;
22028 psf_log_printf (psf, " Channels : %d\n Sample rate : %d\n Bit width : %d\n",
22029 channels, samplerate, bitwidth) ;
22031 psf->sf.channels = channels ;
22032 psf->sf.samplerate = samplerate ;
22034 switch (bitwidth)
22035 { case 8 :
22036 psf->sf.format = SF_FORMAT_PVF | SF_FORMAT_PCM_S8 ;
22037 psf->bytewidth = 1 ;
22038 break ;
22040 case 16 :
22041 psf->sf.format = SF_FORMAT_PVF | SF_FORMAT_PCM_16 ;
22042 psf->bytewidth = 2 ;
22043 break ;
22044 case 32 :
22045 psf->sf.format = SF_FORMAT_PVF | SF_FORMAT_PCM_32 ;
22046 psf->bytewidth = 4 ;
22047 break ;
22049 default :
22050 return SFE_PVF_BAD_BITWIDTH ;
22053 psf->dataoffset = psf_ftell (psf) ;
22054 psf_log_printf (psf, " Data Offset : %D\n", psf->dataoffset) ;
22056 psf->endian = SF_ENDIAN_BIG ;
22058 psf->datalength = psf->filelength - psf->dataoffset ;
22059 psf->blockwidth = psf->sf.channels * psf->bytewidth ;
22061 psf->close = pvf_close ;
22063 if (! psf->sf.frames && psf->blockwidth)
22064 psf->sf.frames = (psf->filelength - psf->dataoffset) / psf->blockwidth ;
22066 return 0 ;
22067 } /* pvf_read_header */
22069 ** Do not edit or modify anything in this comment block.
22070 ** The arch-tag line is a file identity tag for the GNU Arch
22071 ** revision control system.
22073 ** arch-tag: 20a26761-8bc1-41d7-b1f3-9793bf3d9864
22076 ** Copyright (C) 1999-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
22078 ** This program is free software; you can redistribute it and/or modify
22079 ** it under the terms of the GNU Lesser General Public License as published by
22080 ** the Free Software Foundation; either version 2.1 of the License, or
22081 ** (at your option) any later version.
22083 ** This program is distributed in the hope that it will be useful,
22084 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
22085 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22086 ** GNU Lesser General Public License for more details.
22088 ** You should have received a copy of the GNU Lesser General Public License
22089 ** along with this program; if not, write to the Free Software
22090 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22093 #include <stdio.h>
22096 /*------------------------------------------------------------------------------
22097 ** Public function.
22101 raw_open (SF_PRIVATE *psf)
22102 { int subformat, error = SFE_NO_ERROR ;
22104 subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
22106 psf->endian = psf->sf.format & SF_FORMAT_ENDMASK ;
22108 if (CPU_IS_BIG_ENDIAN && (psf->endian == 0 || psf->endian == SF_ENDIAN_CPU))
22109 psf->endian = SF_ENDIAN_BIG ;
22110 else if (CPU_IS_LITTLE_ENDIAN && (psf->endian == 0 || psf->endian == SF_ENDIAN_CPU))
22111 psf->endian = SF_ENDIAN_LITTLE ;
22113 psf->blockwidth = psf->bytewidth * psf->sf.channels ;
22114 psf->dataoffset = 0 ;
22115 psf->datalength = psf->filelength ;
22116 switch (subformat)
22117 { case SF_FORMAT_PCM_S8 :
22118 error = pcm_init (psf) ;
22119 break ;
22121 case SF_FORMAT_PCM_U8 :
22122 error = pcm_init (psf) ;
22123 break ;
22125 case SF_FORMAT_PCM_16 :
22126 case SF_FORMAT_PCM_24 :
22127 case SF_FORMAT_PCM_32 :
22128 error = pcm_init (psf) ;
22129 break ;
22131 case SF_FORMAT_ULAW :
22132 error = ulaw_init (psf) ;
22133 break ;
22135 case SF_FORMAT_ALAW :
22136 error = alaw_init (psf) ;
22137 break ;
22139 case SF_FORMAT_GSM610 :
22140 error = gsm610_init (psf) ;
22141 break ;
22143 /* Lite remove start */
22144 case SF_FORMAT_FLOAT :
22145 error = float32_init (psf) ;
22146 break ;
22148 case SF_FORMAT_DOUBLE :
22149 error = double64_init (psf) ;
22150 break ;
22152 case SF_FORMAT_DWVW_12 :
22153 error = dwvw_init (psf, 12) ;
22154 break ;
22156 case SF_FORMAT_DWVW_16 :
22157 error = dwvw_init (psf, 16) ;
22158 break ;
22160 case SF_FORMAT_DWVW_24 :
22161 error = dwvw_init (psf, 24) ;
22162 break ;
22164 case SF_FORMAT_VOX_ADPCM :
22165 error = vox_adpcm_init (psf) ;
22166 break ;
22167 /* Lite remove end */
22169 default : return SFE_BAD_OPEN_FORMAT ;
22172 return error ;
22173 } /* raw_open */
22175 ** Do not edit or modify anything in this comment block.
22176 ** The arch-tag line is a file identity tag for the GNU Arch
22177 ** revision control system.
22179 ** arch-tag: f0066de7-d6ce-4f36-a1e0-e475c07d4e1a
22182 * Copyright 1992 by Jutta Degener and Carsten Bormann, Technische
22183 * Universitaet Berlin. See the accompanying file "COPYRIGHT" for
22184 * details. THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE.
22187 #include <stdio.h>
22188 #include <assert.h>
22192 /* 4.2.13 .. 4.2.17 RPE ENCODING SECTION
22195 /* 4.2.13 */
22197 static void Weighting_filter (
22198 register word * e, /* signal [-5..0.39.44] IN */
22199 word * x /* signal [0..39] OUT */
22202 * The coefficients of the weighting filter are stored in a table
22203 * (see table 4.4). The following scaling is used:
22205 * H[0..10] = integer( real_H[ 0..10] * 8192 );
22208 /* word wt[ 50 ]; */
22210 register longword L_result;
22211 register int k /* , i */ ;
22213 /* Initialization of a temporary working array wt[0...49]
22216 /* for (k = 0; k <= 4; k++) wt[k] = 0;
22217 * for (k = 5; k <= 44; k++) wt[k] = *e++;
22218 * for (k = 45; k <= 49; k++) wt[k] = 0;
22220 * (e[-5..-1] and e[40..44] are allocated by the caller,
22221 * are initially zero and are not written anywhere.)
22223 e -= 5;
22225 /* Compute the signal x[0..39]
22227 for (k = 0; k <= 39; k++) {
22229 L_result = 8192 >> 1;
22231 /* for (i = 0; i <= 10; i++) {
22232 * L_temp = GSM_L_MULT( wt[k+i], gsm_H[i] );
22233 * L_result = GSM_L_ADD( L_result, L_temp );
22237 #undef rpeSTEP
22238 #define rpeSTEP( i, H ) (e[ k + i ] * (longword)H)
22240 /* Every one of these multiplications is done twice --
22241 * but I don't see an elegant way to optimize this.
22242 * Do you?
22245 #ifdef STUPID_COMPILER
22246 L_result += rpeSTEP( 0, -134 ) ;
22247 L_result += rpeSTEP( 1, -374 ) ;
22248 /* + rpeSTEP( 2, 0 ) */
22249 L_result += rpeSTEP( 3, 2054 ) ;
22250 L_result += rpeSTEP( 4, 5741 ) ;
22251 L_result += rpeSTEP( 5, 8192 ) ;
22252 L_result += rpeSTEP( 6, 5741 ) ;
22253 L_result += rpeSTEP( 7, 2054 ) ;
22254 /* + rpeSTEP( 8, 0 ) */
22255 L_result += rpeSTEP( 9, -374 ) ;
22256 L_result += rpeSTEP( 10, -134 ) ;
22257 #else
22258 L_result +=
22259 rpeSTEP( 0, -134 )
22260 + rpeSTEP( 1, -374 )
22261 /* + rpeSTEP( 2, 0 ) */
22262 + rpeSTEP( 3, 2054 )
22263 + rpeSTEP( 4, 5741 )
22264 + rpeSTEP( 5, 8192 )
22265 + rpeSTEP( 6, 5741 )
22266 + rpeSTEP( 7, 2054 )
22267 /* + rpeSTEP( 8, 0 ) */
22268 + rpeSTEP( 9, -374 )
22269 + rpeSTEP(10, -134 )
22271 #endif
22273 /* L_result = GSM_L_ADD( L_result, L_result ); (* scaling(x2) *)
22274 * L_result = GSM_L_ADD( L_result, L_result ); (* scaling(x4) *)
22276 * x[k] = SASR( L_result, 16 );
22279 /* 2 adds vs. >>16 => 14, minus one shift to compensate for
22280 * those we lost when replacing L_MULT by '*'.
22283 L_result = SASR_L( L_result, 13 );
22284 x[k] = ( L_result < MIN_WORD ? MIN_WORD
22285 : (L_result > MAX_WORD ? MAX_WORD : L_result ));
22289 /* 4.2.14 */
22291 static void RPE_grid_selection (
22292 word * x, /* [0..39] IN */
22293 word * xM, /* [0..12] OUT */
22294 word * Mc_out /* OUT */
22297 * The signal x[0..39] is used to select the RPE grid which is
22298 * represented by Mc.
22301 /* register word temp1; */
22302 register int /* m, */ i;
22303 register longword L_result, L_temp;
22304 longword EM; /* xxx should be L_EM? */
22305 word Mc;
22307 longword L_common_0_3;
22309 EM = 0;
22310 Mc = 0;
22312 /* for (m = 0; m <= 3; m++) {
22313 * L_result = 0;
22316 * for (i = 0; i <= 12; i++) {
22318 * temp1 = SASR_W( x[m + 3*i], 2 );
22320 * assert(temp1 != MIN_WORD);
22322 * L_temp = GSM_L_MULT( temp1, temp1 );
22323 * L_result = GSM_L_ADD( L_temp, L_result );
22326 * if (L_result > EM) {
22327 * Mc = m;
22328 * EM = L_result;
22333 #undef rpeSTEP
22334 #define rpeSTEP( m, i ) L_temp = SASR_W( x[m + 3 * i], 2 ); \
22335 L_result += L_temp * L_temp;
22337 /* common part of 0 and 3 */
22339 L_result = 0;
22340 rpeSTEP( 0, 1 ); rpeSTEP( 0, 2 ); rpeSTEP( 0, 3 ); rpeSTEP( 0, 4 );
22341 rpeSTEP( 0, 5 ); rpeSTEP( 0, 6 ); rpeSTEP( 0, 7 ); rpeSTEP( 0, 8 );
22342 rpeSTEP( 0, 9 ); rpeSTEP( 0, 10); rpeSTEP( 0, 11); rpeSTEP( 0, 12);
22343 L_common_0_3 = L_result;
22345 /* i = 0 */
22347 rpeSTEP( 0, 0 );
22348 L_result <<= 1; /* implicit in L_MULT */
22349 EM = L_result;
22351 /* i = 1 */
22353 L_result = 0;
22354 rpeSTEP( 1, 0 );
22355 rpeSTEP( 1, 1 ); rpeSTEP( 1, 2 ); rpeSTEP( 1, 3 ); rpeSTEP( 1, 4 );
22356 rpeSTEP( 1, 5 ); rpeSTEP( 1, 6 ); rpeSTEP( 1, 7 ); rpeSTEP( 1, 8 );
22357 rpeSTEP( 1, 9 ); rpeSTEP( 1, 10); rpeSTEP( 1, 11); rpeSTEP( 1, 12);
22358 L_result <<= 1;
22359 if (L_result > EM) {
22360 Mc = 1;
22361 EM = L_result;
22364 /* i = 2 */
22366 L_result = 0;
22367 rpeSTEP( 2, 0 );
22368 rpeSTEP( 2, 1 ); rpeSTEP( 2, 2 ); rpeSTEP( 2, 3 ); rpeSTEP( 2, 4 );
22369 rpeSTEP( 2, 5 ); rpeSTEP( 2, 6 ); rpeSTEP( 2, 7 ); rpeSTEP( 2, 8 );
22370 rpeSTEP( 2, 9 ); rpeSTEP( 2, 10); rpeSTEP( 2, 11); rpeSTEP( 2, 12);
22371 L_result <<= 1;
22372 if (L_result > EM) {
22373 Mc = 2;
22374 EM = L_result;
22377 /* i = 3 */
22379 L_result = L_common_0_3;
22380 rpeSTEP( 3, 12 );
22381 L_result <<= 1;
22382 if (L_result > EM) {
22383 Mc = 3;
22384 EM = L_result;
22387 /**/
22389 /* Down-sampling by a factor 3 to get the selected xM[0..12]
22390 * RPE sequence.
22392 for (i = 0; i <= 12; i ++) xM[i] = x[Mc + 3*i];
22393 *Mc_out = Mc;
22396 /* 4.12.15 */
22398 static void APCM_quantization_xmaxc_to_exp_mant (
22399 word xmaxc, /* IN */
22400 word * expon_out, /* OUT */
22401 word * mant_out ) /* OUT */
22403 word expon, mant;
22405 /* Compute expononent and mantissa of the decoded version of xmaxc
22408 expon = 0;
22409 if (xmaxc > 15) expon = SASR_W(xmaxc, 3) - 1;
22410 mant = xmaxc - (expon << 3);
22412 if (mant == 0) {
22413 expon = -4;
22414 mant = 7;
22416 else {
22417 while (mant <= 7) {
22418 mant = mant << 1 | 1;
22419 expon--;
22421 mant -= 8;
22424 assert( expon >= -4 && expon <= 6 );
22425 assert( mant >= 0 && mant <= 7 );
22427 *expon_out = expon;
22428 *mant_out = mant;
22431 static void APCM_quantization (
22432 word * xM, /* [0..12] IN */
22433 word * xMc, /* [0..12] OUT */
22434 word * mant_out, /* OUT */
22435 word * expon_out, /* OUT */
22436 word * xmaxc_out /* OUT */
22439 int i, itest;
22441 word xmax, xmaxc, temp, temp1, temp2;
22442 word expon, mant;
22445 /* Find the maximum absolute value xmax of xM[0..12].
22448 xmax = 0;
22449 for (i = 0; i <= 12; i++) {
22450 temp = xM[i];
22451 temp = GSM_ABS(temp);
22452 if (temp > xmax) xmax = temp;
22455 /* Qantizing and coding of xmax to get xmaxc.
22458 expon = 0;
22459 temp = SASR_W( xmax, 9 );
22460 itest = 0;
22462 for (i = 0; i <= 5; i++) {
22464 itest |= (temp <= 0);
22465 temp = SASR_W( temp, 1 );
22467 assert(expon <= 5);
22468 if (itest == 0) expon++; /* expon = add (expon, 1) */
22471 assert(expon <= 6 && expon >= 0);
22472 temp = expon + 5;
22474 assert(temp <= 11 && temp >= 0);
22475 xmaxc = gsm_add( SASR_W(xmax, temp), (word) (expon << 3) );
22477 /* Quantizing and coding of the xM[0..12] RPE sequence
22478 * to get the xMc[0..12]
22481 APCM_quantization_xmaxc_to_exp_mant( xmaxc, &expon, &mant );
22483 /* This computation uses the fact that the decoded version of xmaxc
22484 * can be calculated by using the expononent and the mantissa part of
22485 * xmaxc (logarithmic table).
22486 * So, this method avoids any division and uses only a scaling
22487 * of the RPE samples by a function of the expononent. A direct
22488 * multiplication by the inverse of the mantissa (NRFAC[0..7]
22489 * found in table 4.5) gives the 3 bit coded version xMc[0..12]
22490 * of the RPE samples.
22494 /* Direct computation of xMc[0..12] using table 4.5
22497 assert( expon <= 4096 && expon >= -4096);
22498 assert( mant >= 0 && mant <= 7 );
22500 temp1 = 6 - expon; /* normalization by the expononent */
22501 temp2 = gsm_NRFAC[ mant ]; /* inverse mantissa */
22503 for (i = 0; i <= 12; i++) {
22505 assert(temp1 >= 0 && temp1 < 16);
22507 temp = xM[i] << temp1;
22508 temp = GSM_MULT( temp, temp2 );
22509 temp = SASR_W(temp, 12);
22510 xMc[i] = temp + 4; /* see note below */
22513 /* NOTE: This equation is used to make all the xMc[i] positive.
22516 *mant_out = mant;
22517 *expon_out = expon;
22518 *xmaxc_out = xmaxc;
22521 /* 4.2.16 */
22523 static void APCM_inverse_quantization (
22524 register word * xMc, /* [0..12] IN */
22525 word mant,
22526 word expon,
22527 register word * xMp) /* [0..12] OUT */
22529 * This part is for decoding the RPE sequence of coded xMc[0..12]
22530 * samples to obtain the xMp[0..12] array. Table 4.6 is used to get
22531 * the mantissa of xmaxc (FAC[0..7]).
22534 int i;
22535 word temp, temp1, temp2, temp3;
22537 assert( mant >= 0 && mant <= 7 );
22539 temp1 = gsm_FAC[ mant ]; /* see 4.2-15 for mant */
22540 temp2 = gsm_sub( 6, expon ); /* see 4.2-15 for exp */
22541 temp3 = gsm_asl( 1, gsm_sub( temp2, 1 ));
22543 for (i = 13; i--;) {
22545 assert( *xMc <= 7 && *xMc >= 0 ); /* 3 bit unsigned */
22547 /* temp = gsm_sub( *xMc++ << 1, 7 ); */
22548 temp = (*xMc++ << 1) - 7; /* restore sign */
22549 assert( temp <= 7 && temp >= -7 ); /* 4 bit signed */
22551 temp <<= 12; /* 16 bit signed */
22552 temp = GSM_MULT_R( temp1, temp );
22553 temp = GSM_ADD( temp, temp3 );
22554 *xMp++ = gsm_asr( temp, temp2 );
22558 /* 4.2.17 */
22560 static void RPE_grid_positioning (
22561 word Mc, /* grid position IN */
22562 register word * xMp, /* [0..12] IN */
22563 register word * ep /* [0..39] OUT */
22566 * This procedure computes the reconstructed long term residual signal
22567 * ep[0..39] for the LTP analysis filter. The inputs are the Mc
22568 * which is the grid position selection and the xMp[0..12] decoded
22569 * RPE samples which are upsampled by a factor of 3 by inserting zero
22570 * values.
22573 int i = 13;
22575 assert(0 <= Mc && Mc <= 3);
22577 switch (Mc) {
22578 case 3: *ep++ = 0;
22579 case 2: do {
22580 *ep++ = 0;
22581 case 1: *ep++ = 0;
22582 case 0: *ep++ = *xMp++;
22583 } while (--i);
22585 while (++Mc < 4) *ep++ = 0;
22589 int i, k;
22590 for (k = 0; k <= 39; k++) ep[k] = 0;
22591 for (i = 0; i <= 12; i++) {
22592 ep[ Mc + (3*i) ] = xMp[i];
22597 /* 4.2.18 */
22599 /* This procedure adds the reconstructed long term residual signal
22600 * ep[0..39] to the estimated signal dpp[0..39] from the long term
22601 * analysis filter to compute the reconstructed short term residual
22602 * signal dp[-40..-1]; also the reconstructed short term residual
22603 * array dp[-120..-41] is updated.
22606 #if 0 /* Has been inlined in code.c */
22607 void Gsm_Update_of_reconstructed_short_time_residual_signal (
22608 word * dpp, /* [0...39] IN */
22609 word * ep, /* [0...39] IN */
22610 word * dp) /* [-120...-1] IN/OUT */
22612 int k;
22614 for (k = 0; k <= 79; k++)
22615 dp[ -120 + k ] = dp[ -80 + k ];
22617 for (k = 0; k <= 39; k++)
22618 dp[ -40 + k ] = gsm_add( ep[k], dpp[k] );
22620 #endif /* Has been inlined in code.c */
22622 void Gsm_RPE_Encoding (
22623 /*-struct gsm_state * S,-*/
22625 word * e, /* -5..-1][0..39][40..44 IN/OUT */
22626 word * xmaxc, /* OUT */
22627 word * Mc, /* OUT */
22628 word * xMc) /* [0..12] OUT */
22630 word x[40];
22631 word xM[13], xMp[13];
22632 word mant, expon;
22634 Weighting_filter(e, x);
22635 RPE_grid_selection(x, xM, Mc);
22637 APCM_quantization( xM, xMc, &mant, &expon, xmaxc);
22638 APCM_inverse_quantization( xMc, mant, expon, xMp);
22640 RPE_grid_positioning( *Mc, xMp, e );
22644 void Gsm_RPE_Decoding (
22645 /*-struct gsm_state * S,-*/
22647 word xmaxcr,
22648 word Mcr,
22649 word * xMcr, /* [0..12], 3 bits IN */
22650 word * erp /* [0..39] OUT */
22653 word expon, mant;
22654 word xMp[ 13 ];
22656 APCM_quantization_xmaxc_to_exp_mant( xmaxcr, &expon, &mant );
22657 APCM_inverse_quantization( xMcr, mant, expon, xMp );
22658 RPE_grid_positioning( Mcr, xMp, erp );
22662 ** Do not edit or modify anything in this comment block.
22663 ** The arch-tag line is a file identity tag for the GNU Arch
22664 ** revision control system.
22666 ** arch-tag: 82005b9e-1560-4e94-9ddb-00cb14867295
22670 ** Copyright (C) 2001-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
22672 ** This program is free software; you can redistribute it and/or modify
22673 ** it under the terms of the GNU Lesser General Public License as published by
22674 ** the Free Software Foundation; either version 2.1 of the License, or
22675 ** (at your option) any later version.
22677 ** This program is distributed in the hope that it will be useful,
22678 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
22679 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22680 ** GNU Lesser General Public License for more details.
22682 ** You should have received a copy of the GNU Lesser General Public License
22683 ** along with this program; if not, write to the Free Software
22684 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22687 #include <stdio.h>
22688 #include <string.h>
22689 #include <ctype.h>
22690 #include <stdarg.h>
22693 #if (ENABLE_EXPERIMENTAL_CODE == 0)
22696 rx2_open (SF_PRIVATE *psf)
22697 { if (psf)
22698 return SFE_UNIMPLEMENTED ;
22699 return (psf && 0) ;
22700 } /* rx2_open */
22702 #else
22704 /*------------------------------------------------------------------------------
22705 * Macros to handle big/little endian issues.
22708 #define CAT_MARKER (MAKE_MARKER ('C', 'A', 'T', ' '))
22709 #define GLOB_MARKER (MAKE_MARKER ('G', 'L', 'O', 'B'))
22711 #define RECY_MARKER (MAKE_MARKER ('R', 'E', 'C', 'Y'))
22713 #define SLCL_MARKER (MAKE_MARKER ('S', 'L', 'C', 'L'))
22714 #define SLCE_MARKER (MAKE_MARKER ('S', 'L', 'C', 'E'))
22716 #define DEVL_MARKER (MAKE_MARKER ('D', 'E', 'V', 'L'))
22717 #define TRSH_MARKER (MAKE_MARKER ('T', 'R', 'S', 'H'))
22719 #define EQ_MARKER (MAKE_MARKER ('E', 'Q', ' ', ' '))
22720 #define COMP_MARKER (MAKE_MARKER ('C', 'O', 'M', 'P'))
22722 #define SINF_MARKER (MAKE_MARKER ('S', 'I', 'N', 'F'))
22723 #define SDAT_MARKER (MAKE_MARKER ('S', 'D', 'A', 'T'))
22725 /*------------------------------------------------------------------------------
22726 * Typedefs for file chunks.
22730 /*------------------------------------------------------------------------------
22731 * Private static functions.
22733 static int rx2_close (SF_PRIVATE *psf) ;
22735 /*------------------------------------------------------------------------------
22736 ** Public functions.
22740 rx2_open (SF_PRIVATE *psf)
22741 { static char *marker_type [4] =
22742 { "Original Enabled", "Enabled Hidden",
22743 "Additional/PencilTool", "Disabled"
22746 int error, marker, length, glob_offset, slce_count, frames ;
22748 int sdat_length = 0, slce_total = 0 ;
22750 int n_channels ;
22753 /* So far only doing read. */
22755 psf_binheader_readf (psf, "Epm4", 0, &marker, &length) ;
22757 if (marker != CAT_MARKER)
22758 { psf_log_printf (psf, "length : %d\n", length) ;
22759 return -1000 ;
22762 if (length != psf->filelength - 8)
22763 psf_log_printf (psf, "%M : %d (should be %d)\n", marker, length, psf->filelength - 8) ;
22764 else
22765 psf_log_printf (psf, "%M : %d\n", marker, length) ;
22767 /* 'REX2' marker */
22768 psf_binheader_readf (psf, "m", &marker) ;
22769 psf_log_printf (psf, "%M", marker) ;
22771 /* 'HEAD' marker */
22772 psf_binheader_readf (psf, "m", &marker) ;
22773 psf_log_printf (psf, "%M\n", marker) ;
22775 /* Grab 'GLOB' offset. */
22776 psf_binheader_readf (psf, "E4", &glob_offset) ;
22777 glob_offset += 0x14 ; /* Add the current file offset. */
22779 /* Jump to offset 0x30 */
22780 psf_binheader_readf (psf, "p", 0x30) ;
22782 /* Get name length */
22783 length = 0 ;
22784 psf_binheader_readf (psf, "1", &length) ;
22785 if (length >= SIGNED_SIZEOF (psf->buffer))
22786 { psf_log_printf (psf, " Text : %d *** Error : Too sf_count_t!\n") ;
22787 return -1001 ;
22790 memset (psf->buffer, 0, SIGNED_SIZEOF (psf->buffer)) ;
22791 psf_binheader_readf (psf, "b", psf->buffer, length) ;
22792 psf_log_printf (psf, " Text : \"%s\"\n", psf->buffer) ;
22794 /* Jump to GLOB offset position. */
22795 if (glob_offset & 1)
22796 glob_offset ++ ;
22798 psf_binheader_readf (psf, "p", glob_offset) ;
22800 slce_count = 0 ;
22801 /* GLOB */
22802 while (1)
22803 { psf_binheader_readf (psf, "m", &marker) ;
22805 if (marker != SLCE_MARKER && slce_count > 0)
22806 { psf_log_printf (psf, " SLCE count : %d\n", slce_count) ;
22807 slce_count = 0 ;
22809 switch (marker)
22810 { case GLOB_MARKER:
22811 psf_binheader_readf (psf, "E4", &length) ;
22812 psf_log_printf (psf, " %M : %d\n", marker, length) ;
22813 psf_binheader_readf (psf, "j", length) ;
22814 break ;
22816 case RECY_MARKER:
22817 psf_binheader_readf (psf, "E4", &length) ;
22818 psf_log_printf (psf, " %M : %d\n", marker, length) ;
22819 psf_binheader_readf (psf, "j", (length+1) & 0xFFFFFFFE) ; /* ?????? */
22820 break ;
22822 case CAT_MARKER:
22823 psf_binheader_readf (psf, "E4", &length) ;
22824 psf_log_printf (psf, " %M : %d\n", marker, length) ;
22825 /*-psf_binheader_readf (psf, "j", length) ;-*/
22826 break ;
22828 case DEVL_MARKER:
22829 psf_binheader_readf (psf, "mE4", &marker, &length) ;
22830 psf_log_printf (psf, " DEVL%M : %d\n", marker, length) ;
22831 if (length & 1)
22832 length ++ ;
22833 psf_binheader_readf (psf, "j", length) ;
22834 break ;
22836 case EQ_MARKER:
22837 case COMP_MARKER:
22838 psf_binheader_readf (psf, "E4", &length) ;
22839 psf_log_printf (psf, " %M : %d\n", marker, length) ;
22840 /* This is weird!!!! why make this (length - 1) */
22841 if (length & 1)
22842 length ++ ;
22843 psf_binheader_readf (psf, "j", length) ;
22844 break ;
22846 case SLCL_MARKER:
22847 psf_log_printf (psf, " %M\n (Offset, Next Offset, Type)\n", marker) ;
22848 slce_count = 0 ;
22849 break ;
22851 case SLCE_MARKER:
22852 { int len [4], indx ;
22854 psf_binheader_readf (psf, "E4444", &len [0], &len [1], &len [2], &len [3]) ;
22856 indx = ((len [3] & 0x0000FFFF) >> 8) & 3 ;
22858 if (len [2] == 1)
22859 { if (indx != 1)
22860 indx = 3 ; /* 2 cases, where next slice offset = 1 -> disabled & enabled/hidden */
22862 psf_log_printf (psf, " %M : (%6d, ?: 0x%X, %s)\n", marker, len [1], (len [3] & 0xFFFF0000) >> 16, marker_type [indx]) ;
22864 else
22865 { slce_total += len [2] ;
22867 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]) ;
22870 slce_count ++ ;
22872 break ;
22874 case SINF_MARKER:
22875 psf_binheader_readf (psf, "E4", &length) ;
22876 psf_log_printf (psf, " %M : %d\n", marker, length) ;
22878 psf_binheader_readf (psf, "E2", &n_channels) ;
22879 n_channels = (n_channels & 0x0000FF00) >> 8 ;
22880 psf_log_printf (psf, " Channels : %d\n", n_channels) ;
22882 psf_binheader_readf (psf, "E44", &psf->sf.samplerate, &frames) ;
22883 psf->sf.frames = frames ;
22884 psf_log_printf (psf, " Sample Rate : %d\n", psf->sf.samplerate) ;
22885 psf_log_printf (psf, " Frames : %D\n", psf->sf.frames) ;
22887 psf_binheader_readf (psf, "E4", &length) ;
22888 psf_log_printf (psf, " ??????????? : %d\n", length) ;
22890 psf_binheader_readf (psf, "E4", &length) ;
22891 psf_log_printf (psf, " ??????????? : %d\n", length) ;
22892 break ;
22894 case SDAT_MARKER:
22895 psf_binheader_readf (psf, "E4", &length) ;
22897 sdat_length = length ;
22899 /* Get the current offset. */
22900 psf->dataoffset = psf_binheader_readf (psf, NULL) ;
22902 if (psf->dataoffset + length != psf->filelength)
22903 psf_log_printf (psf, " %M : %d (should be %d)\n", marker, length, psf->dataoffset + psf->filelength) ;
22904 else
22905 psf_log_printf (psf, " %M : %d\n", marker, length) ;
22906 break ;
22908 default :
22909 psf_log_printf (psf, "Unknown marker : 0x%X %M", marker, marker) ;
22910 return -1003 ;
22911 break ;
22914 /* SDAT always last marker in file. */
22915 if (marker == SDAT_MARKER)
22916 break ;
22919 puts (psf->logbuffer) ;
22920 puts ("-----------------------------------") ;
22922 printf ("SDAT length : %d\n", sdat_length) ;
22923 printf ("SLCE count : %d\n", slce_count) ;
22925 /* Hack for zero slice count. */
22926 if (slce_count == 0 && slce_total == 1)
22927 slce_total = frames ;
22929 printf ("SLCE samples : %d\n", slce_total) ;
22931 /* Two bytes per sample. */
22932 printf ("Comp Ratio : %f:1\n", (2.0 * slce_total * n_channels) / sdat_length) ;
22934 puts (" ") ;
22936 psf->logbuffer [0] = 0 ;
22938 /* OK, have the header although not too sure what it all means. */
22940 psf->endian = SF_ENDIAN_BIG ;
22942 psf->datalength = psf->filelength - psf->dataoffset ;
22944 if (psf_fseek (psf, psf->dataoffset, SEEK_SET))
22945 return SFE_BAD_SEEK ;
22947 psf->sf.format = (SF_FORMAT_REX2 | SF_FORMAT_DWVW_12) ;
22949 psf->sf.channels = 1 ;
22950 psf->bytewidth = 2 ;
22951 psf->blockwidth = psf->sf.channels * psf->bytewidth ;
22953 if ((error = dwvw_init (psf, 16)))
22954 return error ;
22956 psf->close = rx2_close ;
22958 if (! psf->sf.frames && psf->blockwidth)
22959 psf->sf.frames = psf->datalength / psf->blockwidth ;
22961 /* All done. */
22963 return 0 ;
22964 } /* rx2_open */
22966 /*------------------------------------------------------------------------------
22969 static int
22970 rx2_close (SF_PRIVATE *psf)
22972 if (psf->mode == SFM_WRITE)
22973 { /* Now we know for certain the length of the file we can re-write
22974 ** correct values for the FORM, 8SVX and BODY chunks.
22979 return 0 ;
22980 } /* rx2_close */
22982 #endif
22984 ** Do not edit or modify anything in this comment block.
22985 ** The arch-tag line is a file identity tag for the GNU Arch
22986 ** revision control system.
22988 ** arch-tag: 7366e813-9fee-4d1f-881e-e4a691469370
22991 ** Copyright (C) 2001-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
22993 ** This program is free software; you can redistribute it and/or modify
22994 ** it under the terms of the GNU Lesser General Public License as published by
22995 ** the Free Software Foundation; either version 2.1 of the License, or
22996 ** (at your option) any later version.
22998 ** This program is distributed in the hope that it will be useful,
22999 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
23000 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23001 ** GNU Lesser General Public License for more details.
23003 ** You should have received a copy of the GNU Lesser General Public License
23004 ** along with this program; if not, write to the Free Software
23005 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23008 #include <stdio.h>
23009 #include <string.h>
23013 #if (ENABLE_EXPERIMENTAL_CODE == 0)
23016 sd2_open (SF_PRIVATE *psf)
23017 { if (psf)
23018 return SFE_UNIMPLEMENTED ;
23019 return (psf && 0) ;
23020 } /* sd2_open */
23022 #else
23024 /*------------------------------------------------------------------------------
23025 * Macros to handle big/little endian issues.
23028 #define Sd2f_MARKER MAKE_MARKER ('S', 'd', '2', 'f')
23030 /*------------------------------------------------------------------------------
23031 * Typedefs for file chunks.
23036 /*------------------------------------------------------------------------------
23037 * Private static functions.
23040 static int sd2_close (SF_PRIVATE *psf) ;
23042 /*------------------------------------------------------------------------------
23043 ** Public functions.
23047 sd2_open (SF_PRIVATE *psf)
23048 { int marker, software, rsrc_offset, len ;
23049 int rsrc_data_offset, rsrc_map_offset, rsrc_data_length, rsrc_map_length ;
23050 char slen ;
23051 float srate ;
23053 /* Read only so far. */
23055 psf_binheader_readf (psf, "Epmmj", 0x41, &marker, &software, 14) ;
23057 if (marker != Sd2f_MARKER)
23058 { printf ("Whoops!!!\n") ;
23059 puts (psf->logbuffer) ;
23060 return SFE_INTERNAL ;
23063 psf_log_printf (psf, "Marker : %M\n"
23064 "Software : %M\n",
23065 marker, software) ;
23067 /* This seems to be a constant for binhex files. */
23068 psf->dataoffset = 0x80 ;
23070 /* All SD2 files are big endian. */
23071 psf->endian= SF_ENDIAN_BIG ;
23074 ** Resource header info from:
23075 ** http://developer.apple.com/techpubs/mac/MoreToolbox/MoreToolbox-99.html
23078 rsrc_offset = psf->datalength + psf->dataoffset ;
23079 if (rsrc_offset & 0x7F)
23080 rsrc_offset = rsrc_offset - (rsrc_offset & 0x7F) + psf->dataoffset ;
23082 psf_log_printf (psf, "Resource offset : 0x%X\n", rsrc_offset) ;
23084 /* Jump to the rsrc_offset fork section. */
23085 psf_binheader_readf (psf, "Ep", rsrc_offset) ;
23087 psf_binheader_readf (psf, "E4444", &rsrc_data_offset, &rsrc_map_offset, &rsrc_data_length, &rsrc_map_length) ;
23089 rsrc_data_offset += rsrc_offset ;
23090 rsrc_map_offset += rsrc_offset ;
23092 psf_log_printf (psf, " data offset : 0x%X\n"
23093 " map offset : 0x%X\n"
23094 " data length : 0x%X\n"
23095 " map length : 0x%X\n",
23097 rsrc_data_offset, rsrc_map_offset, rsrc_data_length, rsrc_map_length) ;
23099 if (rsrc_data_offset + rsrc_data_length > rsrc_map_offset || rsrc_map_offset + rsrc_map_length > psf->filelength)
23100 { puts ("##############################") ;
23101 puts (psf->logbuffer) ;
23102 puts ("##############################") ;
23103 return SFE_INTERNAL ;
23106 memset (psf->buffer, 0, sizeof (psf->buffer)) ;
23108 psf_binheader_readf (psf, "Ep41", rsrc_data_offset, &len, &slen) ;
23109 if (slen + 1 == len)
23110 { psf_binheader_readf (psf, "Eb", psf->buffer, len - 1) ;
23111 ((char*) psf->buffer) [len - 1] = 0 ;
23112 if (sscanf ((char*) psf->buffer, "%d", &len) == 1)
23113 psf->bytewidth = len ;
23116 psf_binheader_readf (psf, "E41", &len, &slen) ;
23117 if (slen + 1 == len)
23118 { psf_binheader_readf (psf, "Eb", psf->buffer, len - 1) ;
23119 ((char*) psf->buffer) [len - 1] = 0 ;
23120 if (sscanf ((char*) psf->buffer, "%f", &srate) == 1)
23121 psf->sf.samplerate = srate ;
23124 psf_binheader_readf (psf, "E41", &len, &slen) ;
23125 if (slen + 1 == len)
23126 { psf_binheader_readf (psf, "Eb", psf->buffer, len - 1) ;
23127 ((char*) psf->buffer) [len - 1] = 0 ;
23128 if (sscanf ((char*) psf->buffer, "%d", &len) == 1)
23129 psf->sf.channels = len ;
23132 psf_log_printf (psf, " byte width : %d\n", psf->bytewidth) ;
23133 psf_log_printf (psf, " sample rate : %d\n", psf->sf.samplerate) ;
23134 psf_log_printf (psf, " channels : %d\n", psf->sf.channels) ;
23136 if (psf->bytewidth == 2)
23137 { psf->sf.format = SF_FORMAT_SD2 | SF_FORMAT_PCM_16 ;
23139 psf->blockwidth = psf->bytewidth * psf->sf.channels ;
23141 psf->sf.frames = psf->datalength / psf->blockwidth ;
23144 pcm_init (psf) ;
23146 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
23148 psf->close = sd2_close ;
23150 return 0 ;
23151 } /* sd2_open */
23153 /*------------------------------------------------------------------------------
23156 static int
23157 sd2_close (SF_PRIVATE *psf)
23159 if (psf->mode == SFM_WRITE)
23160 { /* Now we know for certain the audio_length of the file we can re-write
23161 ** correct values for the FORM, 8SVX and BODY chunks.
23166 return 0 ;
23167 } /* sd2_close */
23169 #endif
23171 ** Do not edit or modify anything in this comment block.
23172 ** The arch-tag line is a file identity tag for the GNU Arch
23173 ** revision control system.
23175 ** arch-tag: 1ee183e5-6b9f-4c2c-bd0a-24f35595cefc
23178 ** Copyright (C) 2002-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
23180 ** This program is free software; you can redistribute it and/or modify
23181 ** it under the terms of the GNU Lesser General Public License as published by
23182 ** the Free Software Foundation; either version 2.1 of the License, or
23183 ** (at your option) any later version.
23185 ** This program is distributed in the hope that it will be useful,
23186 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
23187 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23188 ** GNU Lesser General Public License for more details.
23190 ** You should have received a copy of the GNU Lesser General Public License
23191 ** along with this program; if not, write to the Free Software
23192 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23196 #include <stdio.h>
23197 #include <stdlib.h>
23198 #include <fcntl.h>
23199 #include <string.h>
23200 #include <ctype.h>
23203 /*------------------------------------------------------------------------------
23206 #define SDS_DATA_OFFSET 0x15
23207 #define SDS_BLOCK_SIZE 127
23209 #define SDS_AUDIO_BYTES_PER_BLOCK 120
23211 #define SDS_3BYTE_TO_INT_DECODE(x) (((x) & 0x7F) | (((x) & 0x7F00) >> 1) | (((x) & 0x7F0000) >> 2))
23212 #define SDS_INT_TO_3BYTE_ENCODE(x) (((x) & 0x7F) | (((x) << 1) & 0x7F00) | (((x) << 2) & 0x7F0000))
23214 /*------------------------------------------------------------------------------
23215 ** Typedefs.
23218 typedef struct tag_SDS_PRIVATE
23219 { int bitwidth, frames ;
23220 int samplesperblock, total_blocks ;
23222 int (*reader) (SF_PRIVATE *psf, struct tag_SDS_PRIVATE *psds) ;
23223 int (*writer) (SF_PRIVATE *psf, struct tag_SDS_PRIVATE *psds) ;
23225 int read_block, read_count ;
23226 unsigned char read_data [SDS_BLOCK_SIZE] ;
23227 int read_samples [SDS_BLOCK_SIZE / 2] ; /* Maximum samples per block */
23229 int write_block, write_count ;
23230 unsigned char write_data [SDS_BLOCK_SIZE] ;
23231 int write_samples [SDS_BLOCK_SIZE / 2] ; /* Maximum samples per block */
23232 } SDS_PRIVATE ;
23234 /*------------------------------------------------------------------------------
23235 ** Private static functions.
23238 static int sds_close (SF_PRIVATE *psf) ;
23240 static int sds_write_header (SF_PRIVATE *psf, int calc_length) ;
23241 static int sds_read_header (SF_PRIVATE *psf, SDS_PRIVATE *psds) ;
23243 static int sds_init (SF_PRIVATE *psf, SDS_PRIVATE *psds) ;
23245 static sf_count_t sds_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
23246 static sf_count_t sds_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
23247 static sf_count_t sds_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
23248 static sf_count_t sds_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
23250 static sf_count_t sds_write_s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
23251 static sf_count_t sds_write_i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
23252 static sf_count_t sds_write_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
23253 static sf_count_t sds_write_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
23255 static sf_count_t sds_seek (SF_PRIVATE *psf, int mode, sf_count_t offset) ;
23257 static int sds_2byte_read (SF_PRIVATE *psf, SDS_PRIVATE *psds) ;
23258 static int sds_3byte_read (SF_PRIVATE *psf, SDS_PRIVATE *psds) ;
23259 static int sds_4byte_read (SF_PRIVATE *psf, SDS_PRIVATE *psds) ;
23261 static int sds_read (SF_PRIVATE *psf, SDS_PRIVATE *psds, int *iptr, int readcount) ;
23263 static int sds_2byte_write (SF_PRIVATE *psf, SDS_PRIVATE *psds) ;
23264 static int sds_3byte_write (SF_PRIVATE *psf, SDS_PRIVATE *psds) ;
23265 static int sds_4byte_write (SF_PRIVATE *psf, SDS_PRIVATE *psds) ;
23267 static int sds_write (SF_PRIVATE *psf, SDS_PRIVATE *psds, int *iptr, int writecount) ;
23269 /*------------------------------------------------------------------------------
23270 ** Public function.
23274 sds_open (SF_PRIVATE *psf)
23275 { SDS_PRIVATE *psds ;
23276 int subformat, error = 0 ;
23278 /* Hmmmm, need this here to pass update_header_test. */
23279 psf->sf.frames = 0 ;
23281 if (! (psds = calloc (1, sizeof (SDS_PRIVATE))))
23282 return SFE_MALLOC_FAILED ;
23283 psf->fdata = psds ;
23285 if (psf->mode == SFM_READ || (psf->mode == SFM_RDWR && psf->filelength > 0))
23286 { if ((error = sds_read_header (psf, psds)))
23287 return error ;
23290 if ((psf->sf.format & SF_FORMAT_TYPEMASK) != SF_FORMAT_SDS)
23291 return SFE_BAD_OPEN_FORMAT ;
23293 subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
23295 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
23296 { if (sds_write_header (psf, SF_FALSE))
23297 return psf->error ;
23299 psf->write_header = sds_write_header ;
23301 psf_fseek (psf, SDS_DATA_OFFSET, SEEK_SET) ;
23304 if ((error = sds_init (psf, psds)) != 0)
23305 return error ;
23307 psf->seek = sds_seek ;
23308 psf->close = sds_close ;
23310 psf->blockwidth = 0 ;
23312 return error ;
23313 } /* sds_open */
23315 /*------------------------------------------------------------------------------
23318 static int
23319 sds_close (SF_PRIVATE *psf)
23321 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
23322 { SDS_PRIVATE *psds ;
23324 if ((psds = (SDS_PRIVATE *) psf->fdata) == NULL)
23325 { psf_log_printf (psf, "*** Bad psf->fdata ptr.\n") ;
23326 return SFE_INTERNAL ;
23329 if (psds->write_count > 0)
23330 { memset (&(psds->write_data [psds->write_count]), 0, (psds->samplesperblock - psds->write_count) * sizeof (int)) ;
23331 psds->writer (psf, psds) ;
23334 sds_write_header (psf, SF_TRUE) ;
23337 return 0 ;
23338 } /* sds_close */
23340 static int
23341 sds_init (SF_PRIVATE *psf, SDS_PRIVATE *psds)
23343 if (psds->bitwidth < 8 || psds->bitwidth > 28)
23344 return (psf->error = SFE_SDS_BAD_BIT_WIDTH) ;
23346 if (psds->bitwidth < 14)
23347 { psds->reader = sds_2byte_read ;
23348 psds->writer = sds_2byte_write ;
23349 psds->samplesperblock = SDS_AUDIO_BYTES_PER_BLOCK / 2 ;
23351 else if (psds->bitwidth < 21)
23352 { psds->reader = sds_3byte_read ;
23353 psds->writer = sds_3byte_write ;
23354 psds->samplesperblock = SDS_AUDIO_BYTES_PER_BLOCK / 3 ;
23356 else
23357 { psds->reader = sds_4byte_read ;
23358 psds->writer = sds_4byte_write ;
23359 psds->samplesperblock = SDS_AUDIO_BYTES_PER_BLOCK / 4 ;
23362 if (psf->mode == SFM_READ || psf->mode == SFM_RDWR)
23363 { psf->read_short = sds_read_s ;
23364 psf->read_int = sds_read_i ;
23365 psf->read_float = sds_read_f ;
23366 psf->read_double = sds_read_d ;
23368 /* Read first block. */
23369 psds->reader (psf, psds) ;
23372 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
23373 { psf->write_short = sds_write_s ;
23374 psf->write_int = sds_write_i ;
23375 psf->write_float = sds_write_f ;
23376 psf->write_double = sds_write_d ;
23379 return 0 ;
23380 } /* sds_init */
23382 static int
23383 sds_read_header (SF_PRIVATE *psf, SDS_PRIVATE *psds)
23384 { unsigned char channel, bitwidth, loop_type, byte ;
23385 unsigned short sample_no, marker ;
23386 unsigned int samp_period, data_length, sustain_loop_start, sustain_loop_end ;
23387 int bytesread, blockcount ;
23389 /* Set position to start of file to begin reading header. */
23390 bytesread = psf_binheader_readf (psf, "pE211", 0, &marker, &channel, &byte) ;
23392 if (marker != 0xF07E || byte != 0x01)
23393 return SFE_SDS_NOT_SDS ;
23395 psf_log_printf (psf, "Midi Sample Dump Standard (.sds)\nF07E\n Midi Channel : %d\n", channel) ;
23397 bytesread += psf_binheader_readf (psf, "e213", &sample_no, &bitwidth, &samp_period) ;
23399 sample_no = SDS_3BYTE_TO_INT_DECODE (sample_no) ;
23400 samp_period = SDS_3BYTE_TO_INT_DECODE (samp_period) ;
23402 psds->bitwidth = bitwidth ;
23404 psf->sf.samplerate = 1000000000 / samp_period ;
23406 psf_log_printf (psf, " Sample Number : %d\n"
23407 " Bit Width : %d\n"
23408 " Sample Rate : %d\n",
23409 sample_no, psds->bitwidth, psf->sf.samplerate) ;
23411 bytesread += psf_binheader_readf (psf, "e3331", &data_length, &sustain_loop_start, &sustain_loop_end, &loop_type) ;
23413 data_length = SDS_3BYTE_TO_INT_DECODE (data_length) ;
23415 sustain_loop_start = SDS_3BYTE_TO_INT_DECODE (sustain_loop_start) ;
23416 sustain_loop_end = SDS_3BYTE_TO_INT_DECODE (sustain_loop_end) ;
23418 psf_log_printf (psf, " Sustain Loop\n"
23419 " Start : %d\n"
23420 " End : %d\n"
23421 " Loop Type : %d\n",
23422 sustain_loop_start, sustain_loop_end, loop_type) ;
23424 psf->dataoffset = SDS_DATA_OFFSET ;
23425 psf->datalength = psf->filelength - psf->dataoffset ;
23427 if (data_length != psf->filelength - psf->dataoffset)
23428 { psf_log_printf (psf, " Datalength : %d (truncated data??? %d)\n", data_length, psf->filelength - psf->dataoffset) ;
23429 data_length = psf->filelength - psf->dataoffset ;
23431 else
23432 psf_log_printf (psf, " Datalength : %d\n", data_length) ;
23434 bytesread += psf_binheader_readf (psf, "1", &byte) ;
23435 if (byte != 0xF7)
23436 psf_log_printf (psf, "bad end : %X\n", byte & 0xFF) ;
23438 for (blockcount = 0 ; bytesread < psf->filelength ; blockcount++)
23440 bytesread += psf_fread (&marker, 1, 2, psf) ;
23442 if (marker == 0)
23443 break ;
23445 psf_fseek (psf, SDS_BLOCK_SIZE - 2, SEEK_CUR) ;
23446 bytesread += SDS_BLOCK_SIZE - 2 ;
23449 psf_log_printf (psf, "\nBlocks : %d\n", blockcount) ;
23450 psds->total_blocks = blockcount ;
23452 psds->samplesperblock = SDS_AUDIO_BYTES_PER_BLOCK / ((psds->bitwidth + 6) / 7) ;
23453 psf_log_printf (psf, "Samples/Block : %d\n", psds->samplesperblock) ;
23455 psf_log_printf (psf, "Frames : %d\n", blockcount * psds->samplesperblock) ;
23457 psf->sf.frames = blockcount * psds->samplesperblock ;
23458 psds->frames = blockcount * psds->samplesperblock ;
23460 /* Always Mono */
23461 psf->sf.channels = 1 ;
23462 psf->sf.sections = 1 ;
23465 ** Lie to the user about PCM bit width. Always round up to
23466 ** the next multiple of 8.
23468 switch ((psds->bitwidth + 7) / 8)
23469 { case 1 :
23470 psf->sf.format = SF_FORMAT_SDS | SF_FORMAT_PCM_S8 ;
23471 break ;
23473 case 2 :
23474 psf->sf.format = SF_FORMAT_SDS | SF_FORMAT_PCM_16 ;
23475 break ;
23477 case 3 :
23478 psf->sf.format = SF_FORMAT_SDS | SF_FORMAT_PCM_24 ;
23479 break ;
23481 case 4 :
23482 psf->sf.format = SF_FORMAT_SDS | SF_FORMAT_PCM_32 ;
23483 break ;
23485 default :
23486 psf_log_printf (psf, "*** Weird byte width (%d)\n", (psds->bitwidth + 7) / 8) ;
23487 return SFE_SDS_BAD_BIT_WIDTH ;
23490 psf_fseek (psf, SDS_DATA_OFFSET, SEEK_SET) ;
23492 return 0 ;
23493 } /* sds_read_header */
23495 static int
23496 sds_write_header (SF_PRIVATE *psf, int calc_length)
23497 { SDS_PRIVATE *psds ;
23498 sf_count_t current ;
23499 int samp_period, data_length, sustain_loop_start, sustain_loop_end ;
23500 unsigned char loop_type = 0 ;
23502 if ((psds = (SDS_PRIVATE *) psf->fdata) == NULL)
23503 { psf_log_printf (psf, "*** Bad psf->fdata ptr.\n") ;
23504 return SFE_INTERNAL ;
23507 if (psf->pipeoffset > 0)
23508 return 0 ;
23510 current = psf_ftell (psf) ;
23512 if (calc_length)
23513 psf->sf.frames = psds->total_blocks * psds->samplesperblock + psds->write_count ;
23515 if (psds->write_count > 0)
23516 { int current_count = psds->write_count ;
23517 int current_block = psds->write_block ;
23519 psds->writer (psf, psds) ;
23521 psf_fseek (psf, -1 * SDS_BLOCK_SIZE, SEEK_CUR) ;
23523 psds->write_count = current_count ;
23524 psds->write_block = current_block ;
23527 /* Reset the current header length to zero. */
23528 psf->header [0] = 0 ;
23529 psf->headindex = 0 ;
23531 if (psf->is_pipe == SF_FALSE)
23532 psf_fseek (psf, 0, SEEK_SET) ;
23534 psf_binheader_writef (psf, "E211", 0xF07E, 0, 1) ;
23536 switch (psf->sf.format & SF_FORMAT_SUBMASK)
23537 { case SF_FORMAT_PCM_S8 :
23538 psds->bitwidth = 8 ;
23539 break ;
23540 case SF_FORMAT_PCM_16 :
23541 psds->bitwidth = 16 ;
23542 break ;
23543 case SF_FORMAT_PCM_24 :
23544 psds->bitwidth = 24 ;
23545 break ;
23546 default:
23547 return SFE_SDS_BAD_BIT_WIDTH ;
23550 samp_period = SDS_INT_TO_3BYTE_ENCODE (1000000000 / psf->sf.samplerate) ;
23552 psf_binheader_writef (psf, "e213", 0, psds->bitwidth, samp_period) ;
23554 data_length = SDS_INT_TO_3BYTE_ENCODE (psds->total_blocks * SDS_BLOCK_SIZE) ;
23555 sustain_loop_start = SDS_INT_TO_3BYTE_ENCODE (0) ;
23556 sustain_loop_end = SDS_INT_TO_3BYTE_ENCODE (psf->sf.frames) ;
23558 psf_binheader_writef (psf, "e33311", data_length, sustain_loop_start, sustain_loop_end, loop_type, 0xF7) ;
23560 /* Header construction complete so write it out. */
23561 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
23563 if (psf->error)
23564 return psf->error ;
23566 psf->dataoffset = psf->headindex ;
23567 psf->datalength = psds->write_block * SDS_BLOCK_SIZE ;
23569 if (current > 0)
23570 psf_fseek (psf, current, SEEK_SET) ;
23572 return psf->error ;
23573 } /* sds_write_header */
23576 /*------------------------------------------------------------------------------
23579 static int
23580 sds_2byte_read (SF_PRIVATE *psf, SDS_PRIVATE *psds)
23581 { unsigned char *ucptr, checksum ;
23582 unsigned int sample ;
23583 int k ;
23585 psds->read_block ++ ;
23586 psds->read_count = 0 ;
23588 if (psds->read_block * psds->samplesperblock > psds->frames)
23589 { memset (psds->read_samples, 0, psds->samplesperblock * sizeof (int)) ;
23590 return 1 ;
23593 if ((k = psf_fread (psds->read_data, 1, SDS_BLOCK_SIZE, psf)) != SDS_BLOCK_SIZE)
23594 psf_log_printf (psf, "*** Warning : short read (%d != %d).\n", k, SDS_BLOCK_SIZE) ;
23596 if (psds->read_data [0] != 0xF0)
23597 { printf ("Error A : %02X\n", psds->read_data [0] & 0xFF) ;
23600 checksum = psds->read_data [1] ;
23601 if (checksum != 0x7E)
23602 { printf ("Error 1 : %02X\n", checksum & 0xFF) ;
23605 for (k = 2 ; k < SDS_BLOCK_SIZE - 3 ; k ++)
23606 checksum ^= psds->read_data [k] ;
23608 checksum &= 0x7F ;
23610 if (checksum != psds->read_data [SDS_BLOCK_SIZE - 2])
23611 { 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]) ;
23614 ucptr = psds->read_data + 5 ;
23615 for (k = 0 ; k < 120 ; k += 2)
23616 { sample = (ucptr [k] << 25) + (ucptr [k + 1] << 18) ;
23617 psds->read_samples [k / 2] = (int) (sample - 0x80000000) ;
23620 return 1 ;
23621 } /* sds_2byte_read */
23623 static int
23624 sds_3byte_read (SF_PRIVATE *psf, SDS_PRIVATE *psds)
23625 { unsigned char *ucptr, checksum ;
23626 unsigned int sample ;
23627 int k ;
23629 psds->read_block ++ ;
23630 psds->read_count = 0 ;
23632 if (psds->read_block * psds->samplesperblock > psds->frames)
23633 { memset (psds->read_samples, 0, psds->samplesperblock * sizeof (int)) ;
23634 return 1 ;
23637 if ((k = psf_fread (psds->read_data, 1, SDS_BLOCK_SIZE, psf)) != SDS_BLOCK_SIZE)
23638 psf_log_printf (psf, "*** Warning : short read (%d != %d).\n", k, SDS_BLOCK_SIZE) ;
23640 if (psds->read_data [0] != 0xF0)
23641 { printf ("Error A : %02X\n", psds->read_data [0] & 0xFF) ;
23644 checksum = psds->read_data [1] ;
23645 if (checksum != 0x7E)
23646 { printf ("Error 1 : %02X\n", checksum & 0xFF) ;
23649 for (k = 2 ; k < SDS_BLOCK_SIZE - 3 ; k ++)
23650 checksum ^= psds->read_data [k] ;
23652 checksum &= 0x7F ;
23654 if (checksum != psds->read_data [SDS_BLOCK_SIZE - 2])
23655 { 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]) ;
23658 ucptr = psds->read_data + 5 ;
23659 for (k = 0 ; k < 120 ; k += 3)
23660 { sample = (ucptr [k] << 25) + (ucptr [k + 1] << 18) + (ucptr [k + 2] << 11) ;
23661 psds->read_samples [k / 3] = (int) (sample - 0x80000000) ;
23664 return 1 ;
23665 } /* sds_3byte_read */
23667 static int
23668 sds_4byte_read (SF_PRIVATE *psf, SDS_PRIVATE *psds)
23669 { unsigned char *ucptr, checksum ;
23670 unsigned int sample ;
23671 int k ;
23673 psds->read_block ++ ;
23674 psds->read_count = 0 ;
23676 if (psds->read_block * psds->samplesperblock > psds->frames)
23677 { memset (psds->read_samples, 0, psds->samplesperblock * sizeof (int)) ;
23678 return 1 ;
23681 if ((k = psf_fread (psds->read_data, 1, SDS_BLOCK_SIZE, psf)) != SDS_BLOCK_SIZE)
23682 psf_log_printf (psf, "*** Warning : short read (%d != %d).\n", k, SDS_BLOCK_SIZE) ;
23684 if (psds->read_data [0] != 0xF0)
23685 { printf ("Error A : %02X\n", psds->read_data [0] & 0xFF) ;
23688 checksum = psds->read_data [1] ;
23689 if (checksum != 0x7E)
23690 { printf ("Error 1 : %02X\n", checksum & 0xFF) ;
23693 for (k = 2 ; k < SDS_BLOCK_SIZE - 3 ; k ++)
23694 checksum ^= psds->read_data [k] ;
23696 checksum &= 0x7F ;
23698 if (checksum != psds->read_data [SDS_BLOCK_SIZE - 2])
23699 { 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]) ;
23702 ucptr = psds->read_data + 5 ;
23703 for (k = 0 ; k < 120 ; k += 4)
23704 { sample = (ucptr [k] << 25) + (ucptr [k + 1] << 18) + (ucptr [k + 2] << 11) + (ucptr [k + 3] << 4) ;
23705 psds->read_samples [k / 4] = (int) (sample - 0x80000000) ;
23708 return 1 ;
23709 } /* sds_4byte_read */
23712 static sf_count_t
23713 sds_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
23714 { SDS_PRIVATE *psds ;
23715 int *iptr ;
23716 int k, bufferlen, readcount, count ;
23717 sf_count_t total = 0 ;
23719 if (psf->fdata == NULL)
23720 return 0 ;
23721 psds = (SDS_PRIVATE*) psf->fdata ;
23723 iptr = (int*) psf->buffer ;
23724 bufferlen = sizeof (psf->buffer) / sizeof (int) ;
23725 while (len > 0)
23726 { readcount = (len >= bufferlen) ? bufferlen : len ;
23727 count = sds_read (psf, psds, iptr, readcount) ;
23728 for (k = 0 ; k < readcount ; k++)
23729 ptr [total + k] = iptr [k] >> 16 ;
23730 total += count ;
23731 len -= readcount ;
23734 return total ;
23735 } /* sds_read_s */
23737 static sf_count_t
23738 sds_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
23739 { SDS_PRIVATE *psds ;
23740 int total ;
23742 if (psf->fdata == NULL)
23743 return 0 ;
23744 psds = (SDS_PRIVATE*) psf->fdata ;
23746 total = sds_read (psf, psds, ptr, len) ;
23748 return total ;
23749 } /* sds_read_i */
23751 static sf_count_t
23752 sds_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
23753 { SDS_PRIVATE *psds ;
23754 int *iptr ;
23755 int k, bufferlen, readcount, count ;
23756 sf_count_t total = 0 ;
23757 float normfact ;
23759 if (psf->fdata == NULL)
23760 return 0 ;
23761 psds = (SDS_PRIVATE*) psf->fdata ;
23763 if (psf->norm_float == SF_TRUE)
23764 normfact = 1.0 / 0x80000000 ;
23765 else
23766 normfact = 1.0 / (1 << psds->bitwidth) ;
23768 iptr = (int*) psf->buffer ;
23769 bufferlen = sizeof (psf->buffer) / sizeof (int) ;
23770 while (len > 0)
23771 { readcount = (len >= bufferlen) ? bufferlen : len ;
23772 count = sds_read (psf, psds, iptr, readcount) ;
23773 for (k = 0 ; k < readcount ; k++)
23774 ptr [total + k] = normfact * iptr [k] ;
23775 total += count ;
23776 len -= readcount ;
23779 return total ;
23780 } /* sds_read_f */
23782 static sf_count_t
23783 sds_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
23784 { SDS_PRIVATE *psds ;
23785 int *iptr ;
23786 int k, bufferlen, readcount, count ;
23787 sf_count_t total = 0 ;
23788 double normfact ;
23790 if (psf->fdata == NULL)
23791 return 0 ;
23792 psds = (SDS_PRIVATE*) psf->fdata ;
23794 if (psf->norm_double == SF_TRUE)
23795 normfact = 1.0 / 0x80000000 ;
23796 else
23797 normfact = 1.0 / (1 << psds->bitwidth) ;
23799 iptr = (int*) psf->buffer ;
23800 bufferlen = sizeof (psf->buffer) / sizeof (int) ;
23801 while (len > 0)
23802 { readcount = (len >= bufferlen) ? bufferlen : len ;
23803 count = sds_read (psf, psds, iptr, readcount) ;
23804 for (k = 0 ; k < readcount ; k++)
23805 ptr [total + k] = normfact * iptr [k] ;
23806 total += count ;
23807 len -= readcount ;
23810 return total ;
23811 } /* sds_read_d */
23813 static int
23814 sds_read (SF_PRIVATE *psf, SDS_PRIVATE *psds, int *ptr, int len)
23815 { int count, total = 0 ;
23817 while (total < len)
23818 { if (psds->read_block * psds->samplesperblock >= psds->frames)
23819 { memset (&(ptr [total]), 0, (len - total) * sizeof (int)) ;
23820 return total ;
23823 if (psds->read_count >= psds->samplesperblock)
23824 psds->reader (psf, psds) ;
23826 count = (psds->samplesperblock - psds->read_count) ;
23827 count = (len - total > count) ? count : len - total ;
23829 memcpy (&(ptr [total]), &(psds->read_samples [psds->read_count]), count * sizeof (int)) ;
23830 total += count ;
23831 psds->read_count += count ;
23834 return total ;
23835 } /* sds_read */
23837 /*==============================================================================
23840 static sf_count_t
23841 sds_seek (SF_PRIVATE *psf, int mode, sf_count_t seek_from_start)
23842 { SDS_PRIVATE *psds ;
23843 sf_count_t file_offset ;
23844 int newblock, newsample ;
23846 if ((psds = psf->fdata) == NULL)
23847 { psf->error = SFE_INTERNAL ;
23848 return SF_SEEK_ERROR ;
23851 if (psf->datalength < 0 || psf->dataoffset < 0)
23852 { psf->error = SFE_BAD_SEEK ;
23853 return SF_SEEK_ERROR ;
23856 if (seek_from_start < 0 || seek_from_start > psf->sf.frames)
23857 { psf->error = SFE_BAD_SEEK ;
23858 return SF_SEEK_ERROR ;
23861 if (mode == SFM_READ && psds->write_count > 0)
23862 psds->writer (psf, psds) ;
23864 newblock = seek_from_start / psds->samplesperblock ;
23865 newsample = seek_from_start % psds->samplesperblock ;
23867 switch (mode)
23868 { case SFM_READ :
23869 if (newblock > psds->total_blocks)
23870 { psf->error = SFE_BAD_SEEK ;
23871 return SF_SEEK_ERROR ;
23874 file_offset = psf->dataoffset + newblock * SDS_BLOCK_SIZE ;
23876 if (psf_fseek (psf, file_offset, SEEK_SET) != file_offset)
23877 { psf->error = SFE_SEEK_FAILED ;
23878 return SF_SEEK_ERROR ;
23881 psds->read_block = newblock ;
23882 psds->reader (psf, psds) ;
23883 psds->read_count = newsample ;
23884 break ;
23886 case SFM_WRITE :
23887 if (newblock > psds->total_blocks)
23888 { psf->error = SFE_BAD_SEEK ;
23889 return SF_SEEK_ERROR ;
23892 file_offset = psf->dataoffset + newblock * SDS_BLOCK_SIZE ;
23894 if (psf_fseek (psf, file_offset, SEEK_SET) != file_offset)
23895 { psf->error = SFE_SEEK_FAILED ;
23896 return SF_SEEK_ERROR ;
23899 psds->write_block = newblock ;
23900 psds->reader (psf, psds) ;
23901 psds->write_count = newsample ;
23902 break ;
23904 default :
23905 psf->error = SFE_BAD_SEEK ;
23906 return SF_SEEK_ERROR ;
23907 break ;
23910 return seek_from_start ;
23911 } /* sds_seek */
23913 /*==============================================================================
23916 static int
23917 sds_2byte_write (SF_PRIVATE *psf, SDS_PRIVATE *psds)
23918 { unsigned char *ucptr, checksum ;
23919 unsigned int sample ;
23920 int k ;
23922 psds->write_data [0] = 0xF0 ;
23923 psds->write_data [1] = 0x7E ;
23924 psds->write_data [2] = 0 ; /* Channel number */
23925 psds->write_data [3] = psds->write_block & 0x7F ; /* Packet number */
23927 ucptr = psds->write_data + 5 ;
23928 for (k = 0 ; k < 120 ; k += 2)
23929 { sample = psds->write_samples [k / 2] ;
23930 sample += 0x80000000 ;
23931 ucptr [k] = (sample >> 25) & 0x7F ;
23932 ucptr [k + 1] = (sample >> 18) & 0x7F ;
23935 checksum = psds->write_data [1] ;
23936 for (k = 2 ; k < SDS_BLOCK_SIZE - 3 ; k ++)
23937 checksum ^= psds->write_data [k] ;
23938 checksum &= 0x7F ;
23940 psds->write_data [SDS_BLOCK_SIZE - 2] = checksum ;
23941 psds->write_data [SDS_BLOCK_SIZE - 1] = 0xF7 ;
23943 if ((k = psf_fwrite (psds->write_data, 1, SDS_BLOCK_SIZE, psf)) != SDS_BLOCK_SIZE)
23944 psf_log_printf (psf, "*** Warning : psf_fwrite (%d != %d).\n", k, SDS_BLOCK_SIZE) ;
23946 psds->write_block ++ ;
23947 psds->write_count = 0 ;
23949 if (psds->write_block > psds->total_blocks)
23950 psds->total_blocks = psds->write_block ;
23951 psds->frames = psds->total_blocks * psds->samplesperblock ;
23953 return 1 ;
23954 } /* sds_2byte_write */
23956 static int
23957 sds_3byte_write (SF_PRIVATE *psf, SDS_PRIVATE *psds)
23958 { unsigned char *ucptr, checksum ;
23959 unsigned int sample ;
23960 int k ;
23962 psds->write_data [0] = 0xF0 ;
23963 psds->write_data [1] = 0x7E ;
23964 psds->write_data [2] = 0 ; /* Channel number */
23965 psds->write_data [3] = psds->write_block & 0x7F ; /* Packet number */
23967 ucptr = psds->write_data + 5 ;
23968 for (k = 0 ; k < 120 ; k += 3)
23969 { sample = psds->write_samples [k / 3] ;
23970 sample += 0x80000000 ;
23971 ucptr [k] = (sample >> 25) & 0x7F ;
23972 ucptr [k + 1] = (sample >> 18) & 0x7F ;
23973 ucptr [k + 2] = (sample >> 11) & 0x7F ;
23976 checksum = psds->write_data [1] ;
23977 for (k = 2 ; k < SDS_BLOCK_SIZE - 3 ; k ++)
23978 checksum ^= psds->write_data [k] ;
23979 checksum &= 0x7F ;
23981 psds->write_data [SDS_BLOCK_SIZE - 2] = checksum ;
23982 psds->write_data [SDS_BLOCK_SIZE - 1] = 0xF7 ;
23984 if ((k = psf_fwrite (psds->write_data, 1, SDS_BLOCK_SIZE, psf)) != SDS_BLOCK_SIZE)
23985 psf_log_printf (psf, "*** Warning : psf_fwrite (%d != %d).\n", k, SDS_BLOCK_SIZE) ;
23987 psds->write_block ++ ;
23988 psds->write_count = 0 ;
23990 if (psds->write_block > psds->total_blocks)
23991 psds->total_blocks = psds->write_block ;
23992 psds->frames = psds->total_blocks * psds->samplesperblock ;
23994 return 1 ;
23995 } /* sds_3byte_write */
23997 static int
23998 sds_4byte_write (SF_PRIVATE *psf, SDS_PRIVATE *psds)
23999 { unsigned char *ucptr, checksum ;
24000 unsigned int sample ;
24001 int k ;
24003 psds->write_data [0] = 0xF0 ;
24004 psds->write_data [1] = 0x7E ;
24005 psds->write_data [2] = 0 ; /* Channel number */
24006 psds->write_data [3] = psds->write_block & 0x7F ; /* Packet number */
24008 ucptr = psds->write_data + 5 ;
24009 for (k = 0 ; k < 120 ; k += 4)
24010 { sample = psds->write_samples [k / 4] ;
24011 sample += 0x80000000 ;
24012 ucptr [k] = (sample >> 25) & 0x7F ;
24013 ucptr [k + 1] = (sample >> 18) & 0x7F ;
24014 ucptr [k + 2] = (sample >> 11) & 0x7F ;
24015 ucptr [k + 3] = (sample >> 4) & 0x7F ;
24018 checksum = psds->write_data [1] ;
24019 for (k = 2 ; k < SDS_BLOCK_SIZE - 3 ; k ++)
24020 checksum ^= psds->write_data [k] ;
24021 checksum &= 0x7F ;
24023 psds->write_data [SDS_BLOCK_SIZE - 2] = checksum ;
24024 psds->write_data [SDS_BLOCK_SIZE - 1] = 0xF7 ;
24026 if ((k = psf_fwrite (psds->write_data, 1, SDS_BLOCK_SIZE, psf)) != SDS_BLOCK_SIZE)
24027 psf_log_printf (psf, "*** Warning : psf_fwrite (%d != %d).\n", k, SDS_BLOCK_SIZE) ;
24029 psds->write_block ++ ;
24030 psds->write_count = 0 ;
24032 if (psds->write_block > psds->total_blocks)
24033 psds->total_blocks = psds->write_block ;
24034 psds->frames = psds->total_blocks * psds->samplesperblock ;
24036 return 1 ;
24037 } /* sds_4byte_write */
24039 static sf_count_t
24040 sds_write_s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
24041 { SDS_PRIVATE *psds ;
24042 int *iptr ;
24043 int k, bufferlen, writecount, count ;
24044 sf_count_t total = 0 ;
24046 if (psf->fdata == NULL)
24047 return 0 ;
24048 psds = (SDS_PRIVATE*) psf->fdata ;
24050 iptr = (int*) psf->buffer ;
24051 bufferlen = sizeof (psf->buffer) / sizeof (int) ;
24052 while (len > 0)
24053 { writecount = (len >= bufferlen) ? bufferlen : len ;
24054 for (k = 0 ; k < writecount ; k++)
24055 iptr [k] = ptr [total + k] << 16 ;
24056 count = sds_write (psf, psds, iptr, writecount) ;
24057 total += count ;
24058 len -= writecount ;
24061 return total ;
24062 } /* sds_write_s */
24064 static sf_count_t
24065 sds_write_i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
24066 { SDS_PRIVATE *psds ;
24067 int total ;
24069 if (psf->fdata == NULL)
24070 return 0 ;
24071 psds = (SDS_PRIVATE*) psf->fdata ;
24073 total = sds_write (psf, psds, ptr, len) ;
24075 return total ;
24076 } /* sds_write_i */
24078 static sf_count_t
24079 sds_write_f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
24080 { SDS_PRIVATE *psds ;
24081 int *iptr ;
24082 int k, bufferlen, writecount, count ;
24083 sf_count_t total = 0 ;
24084 float normfact ;
24086 if (psf->fdata == NULL)
24087 return 0 ;
24088 psds = (SDS_PRIVATE*) psf->fdata ;
24090 if (psf->norm_float == SF_TRUE)
24091 normfact = 1.0 * 0x80000000 ;
24092 else
24093 normfact = 1.0 * (1 << psds->bitwidth) ;
24095 iptr = (int*) psf->buffer ;
24096 bufferlen = sizeof (psf->buffer) / sizeof (int) ;
24097 while (len > 0)
24098 { writecount = (len >= bufferlen) ? bufferlen : len ;
24099 for (k = 0 ; k < writecount ; k++)
24100 iptr [k] = normfact * ptr [total + k] ;
24101 count = sds_write (psf, psds, iptr, writecount) ;
24102 total += count ;
24103 len -= writecount ;
24106 return total ;
24107 } /* sds_write_f */
24109 static sf_count_t
24110 sds_write_d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
24111 { SDS_PRIVATE *psds ;
24112 int *iptr ;
24113 int k, bufferlen, writecount, count ;
24114 sf_count_t total = 0 ;
24115 double normfact ;
24117 if (psf->fdata == NULL)
24118 return 0 ;
24119 psds = (SDS_PRIVATE*) psf->fdata ;
24121 if (psf->norm_double == SF_TRUE)
24122 normfact = 1.0 * 0x80000000 ;
24123 else
24124 normfact = 1.0 * (1 << psds->bitwidth) ;
24126 iptr = (int*) psf->buffer ;
24127 bufferlen = sizeof (psf->buffer) / sizeof (int) ;
24128 while (len > 0)
24129 { writecount = (len >= bufferlen) ? bufferlen : len ;
24130 for (k = 0 ; k < writecount ; k++)
24131 iptr [k] = normfact * ptr [total + k] ;
24132 count = sds_write (psf, psds, iptr, writecount) ;
24133 total += count ;
24134 len -= writecount ;
24137 return total ;
24138 } /* sds_write_d */
24140 static int
24141 sds_write (SF_PRIVATE *psf, SDS_PRIVATE *psds, int *ptr, int len)
24142 { int count, total = 0 ;
24144 while (total < len)
24145 { count = psds->samplesperblock - psds->write_count ;
24146 if (count > len - total)
24147 count = len - total ;
24149 memcpy (&(psds->write_samples [psds->write_count]), &(ptr [total]), count * sizeof (int)) ;
24150 total += count ;
24151 psds->write_count += count ;
24153 if (psds->write_count >= psds->samplesperblock)
24154 psds->writer (psf, psds) ;
24157 return total ;
24158 } /* sds_write */
24161 ** Do not edit or modify anything in this comment block.
24162 ** The arch-tag line is a file identity tag for the GNU Arch
24163 ** revision control system.
24165 ** arch-tag: d5d26aa3-368c-4ca6-bb85-377e5a2578cc
24168 ** Copyright (C) 1999-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
24170 ** This program is free software; you can redistribute it and/or modify
24171 ** it under the terms of the GNU Lesser General Public License as published by
24172 ** the Free Software Foundation; either version 2.1 of the License, or
24173 ** (at your option) any later version.
24175 ** This program is distributed in the hope that it will be useful,
24176 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
24177 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24178 ** GNU Lesser General Public License for more details.
24180 ** You should have received a copy of the GNU Lesser General Public License
24181 ** along with this program; if not, write to the Free Software
24182 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24186 /*-----------------------------------------------------------------------------------------------
24187 ** Generic functions for performing endian swapping on integer arrays.
24190 void
24191 endswap_short_array (short *ptr, int len)
24194 #if 0
24195 unsigned char *ucptr, temp ;
24197 ucptr = ((unsigned char *) ptr) + 2 * len ;
24198 while (len > 0)
24199 { len -- ;
24200 ucptr -= 2 ;
24201 temp = ucptr [0] ;
24202 ucptr [0] = ucptr [1] ;
24203 ucptr [1] = temp ;
24205 #else
24206 short temp ;
24207 while (len > 0)
24208 { len -- ;
24209 temp = ptr [len] ;
24210 ptr [len] = ENDSWAP_SHORT (temp) ;
24212 #endif
24213 } /* endswap_short_array */
24215 void
24216 endswap_int_array (int *ptr, int len)
24218 #if 0
24219 unsigned char *ucptr, temp ;
24221 ucptr = ((unsigned char *) ptr) + 4 * len ;
24222 while (len > 0)
24223 { len -- ;
24224 ucptr -= 4 ;
24226 temp = ucptr [0] ;
24227 ucptr [0] = ucptr [3] ;
24228 ucptr [3] = temp ;
24230 temp = ucptr [1] ;
24231 ucptr [1] = ucptr [2] ;
24232 ucptr [2] = temp ;
24234 #else
24235 int temp ;
24237 while (len > 0)
24238 { len -- ;
24239 temp = ptr [len] ;
24240 ptr [len] = ENDSWAP_INT (temp) ;
24242 #endif
24243 } /* endswap_int_array */
24245 /* This function assumes that sizeof (long) == 8, but works correctly even
24246 ** is sizeof (long) == 4.
24248 void
24249 endswap_long_array (long *ptr, int len)
24250 { unsigned char *ucptr, temp ;
24252 ucptr = (unsigned char *) ptr + 8 * len ;
24253 while (len > 0)
24254 { len -- ;
24255 ucptr -= 8 ;
24257 temp = ucptr [0] ;
24258 ucptr [0] = ucptr [7] ;
24259 ucptr [7] = temp ;
24261 temp = ucptr [1] ;
24262 ucptr [1] = ucptr [6] ;
24263 ucptr [6] = temp ;
24265 temp = ucptr [2] ;
24266 ucptr [2] = ucptr [5] ;
24267 ucptr [5] = temp ;
24269 temp = ucptr [3] ;
24270 ucptr [3] = ucptr [4] ;
24271 ucptr [4] = temp ;
24273 } /* endswap_long_array */
24275 /*========================================================================================
24278 void
24279 endswap_short_copy (short *dest, short *src, int len)
24281 #if 0
24282 char *psrc, *pdest ;
24284 psrc = ((char *) src) + 2 * len ;
24285 pdest = ((char *) dest) + 2 * len ;
24286 while (len > 0)
24287 { len -- ;
24288 psrc -= 2 ;
24289 pdest -= 2 ;
24291 pdest [0] = psrc [1] ;
24292 pdest [1] = psrc [0] ;
24294 #else
24295 while (len > 0)
24296 { len -- ;
24297 dest [len] = ENDSWAP_SHORT (src [len]) ;
24299 #endif
24300 } /* endswap_short_copy */
24302 void
24303 endswap_int_copy (int *dest, int *src, int len)
24305 #if 0
24306 char *psrc, *pdest ;
24308 psrc = ((char *) src) + 4 * len ;
24309 pdest = ((char *) dest) + 4 * len ;
24310 while (len > 0)
24311 { len -- ;
24312 psrc -= 4 ;
24313 pdest -= 4 ;
24315 pdest [0] = psrc [3] ;
24316 pdest [1] = psrc [2] ;
24317 pdest [2] = psrc [1] ;
24318 pdest [3] = psrc [0] ;
24320 #else
24321 while (len > 0)
24322 { len -- ;
24323 dest [len] = ENDSWAP_INT (src [len]) ;
24325 #endif
24326 } /* endswap_int_copy */
24328 /* This function assumes that sizeof (long) == 8, but works correctly even
24329 ** is sizeof (long) == 4.
24331 void
24332 endswap_long_copy (long *dest, long *src, int len)
24333 { char *psrc, *pdest ;
24335 psrc = (char *) src + 8 * len ;
24336 pdest = (char *) dest + 8 * len ;
24337 while (len > 0)
24338 { len -- ;
24339 psrc -= 8 ;
24340 pdest -= 8 ;
24342 pdest [0] = psrc [7] ;
24343 pdest [1] = psrc [6] ;
24344 pdest [2] = psrc [5] ;
24345 pdest [3] = psrc [4] ;
24346 pdest [4] = psrc [3] ;
24347 pdest [5] = psrc [2] ;
24348 pdest [6] = psrc [1] ;
24349 pdest [7] = psrc [0] ;
24351 } /* endswap_long_copy */
24354 ** Do not edit or modify anything in this comment block.
24355 ** The arch-tag line is a file identity tag for the GNU Arch
24356 ** revision control system.
24358 ** arch-tag: 4cf6cc94-da4e-4ef5-aa4c-03dc6cd16810
24361 * Copyright 1992 by Jutta Degener and Carsten Bormann, Technische
24362 * Universitaet Berlin. See the accompanying file "COPYRIGHT" for
24363 * details. THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE.
24366 #include <stdio.h>
24367 #include <assert.h>
24372 * SHORT TERM ANALYSIS FILTERING SECTION
24375 /* 4.2.8 */
24377 static void Decoding_of_the_coded_Log_Area_Ratios (
24378 word * LARc, /* coded log area ratio [0..7] IN */
24379 word * LARpp) /* out: decoded .. */
24381 register word temp1 /* , temp2 */;
24383 /* This procedure requires for efficient implementation
24384 * two tables.
24386 * INVA[1..8] = integer( (32768 * 8) / real_A[1..8])
24387 * MIC[1..8] = minimum value of the LARc[1..8]
24390 /* Compute the LARpp[1..8]
24393 /* for (i = 1; i <= 8; i++, B++, MIC++, INVA++, LARc++, LARpp++) {
24395 * temp1 = GSM_ADD( *LARc, *MIC ) << 10;
24396 * temp2 = *B << 1;
24397 * temp1 = GSM_SUB( temp1, temp2 );
24399 * assert(*INVA != MIN_WORD);
24401 * temp1 = GSM_MULT_R( *INVA, temp1 );
24402 * *LARpp = GSM_ADD( temp1, temp1 );
24406 #undef short_termSTEP
24407 #define short_termSTEP( B, MIC, INVA ) \
24408 temp1 = GSM_ADD( *LARc++, MIC ) << 10; \
24409 temp1 = GSM_SUB( temp1, B << 1 ); \
24410 temp1 = GSM_MULT_R( INVA, temp1 ); \
24411 *LARpp++ = GSM_ADD( temp1, temp1 );
24413 short_termSTEP( 0, -32, 13107 );
24414 short_termSTEP( 0, -32, 13107 );
24415 short_termSTEP( 2048, -16, 13107 );
24416 short_termSTEP( -2560, -16, 13107 );
24418 short_termSTEP( 94, -8, 19223 );
24419 short_termSTEP( -1792, -8, 17476 );
24420 short_termSTEP( -341, -4, 31454 );
24421 short_termSTEP( -1144, -4, 29708 );
24423 /* NOTE: the addition of *MIC is used to restore
24424 * the sign of *LARc.
24428 /* 4.2.9 */
24429 /* Computation of the quantized reflection coefficients
24432 /* 4.2.9.1 Interpolation of the LARpp[1..8] to get the LARp[1..8]
24436 * Within each frame of 160 analyzed speech samples the short term
24437 * analysis and synthesis filters operate with four different sets of
24438 * coefficients, derived from the previous set of decoded LARs(LARpp(j-1))
24439 * and the actual set of decoded LARs (LARpp(j))
24441 * (Initial value: LARpp(j-1)[1..8] = 0.)
24444 static void Coefficients_0_12 (
24445 register word * LARpp_j_1,
24446 register word * LARpp_j,
24447 register word * LARp)
24449 register int i;
24451 for (i = 1; i <= 8; i++, LARp++, LARpp_j_1++, LARpp_j++) {
24452 *LARp = GSM_ADD( SASR_W( *LARpp_j_1, 2 ), SASR_W( *LARpp_j, 2 ));
24453 *LARp = GSM_ADD( *LARp, SASR_W( *LARpp_j_1, 1));
24457 static void Coefficients_13_26 (
24458 register word * LARpp_j_1,
24459 register word * LARpp_j,
24460 register word * LARp)
24462 register int i;
24463 for (i = 1; i <= 8; i++, LARpp_j_1++, LARpp_j++, LARp++) {
24464 *LARp = GSM_ADD( SASR_W( *LARpp_j_1, 1), SASR_W( *LARpp_j, 1 ));
24468 static void Coefficients_27_39 (
24469 register word * LARpp_j_1,
24470 register word * LARpp_j,
24471 register word * LARp)
24473 register int i;
24475 for (i = 1; i <= 8; i++, LARpp_j_1++, LARpp_j++, LARp++) {
24476 *LARp = GSM_ADD( SASR_W( *LARpp_j_1, 2 ), SASR_W( *LARpp_j, 2 ));
24477 *LARp = GSM_ADD( *LARp, SASR_W( *LARpp_j, 1 ));
24482 static void Coefficients_40_159 (
24483 register word * LARpp_j,
24484 register word * LARp)
24486 register int i;
24488 for (i = 1; i <= 8; i++, LARp++, LARpp_j++)
24489 *LARp = *LARpp_j;
24492 /* 4.2.9.2 */
24494 static void LARp_to_rp (
24495 register word * LARp) /* [0..7] IN/OUT */
24497 * The input of this procedure is the interpolated LARp[0..7] array.
24498 * The reflection coefficients, rp[i], are used in the analysis
24499 * filter and in the synthesis filter.
24502 register int i;
24503 register word temp;
24505 for (i = 1; i <= 8; i++, LARp++) {
24507 /* temp = GSM_ABS( *LARp );
24509 * if (temp < 11059) temp <<= 1;
24510 * else if (temp < 20070) temp += 11059;
24511 * else temp = GSM_ADD( temp >> 2, 26112 );
24513 * *LARp = *LARp < 0 ? -temp : temp;
24516 if (*LARp < 0) {
24517 temp = *LARp == MIN_WORD ? MAX_WORD : -(*LARp);
24518 *LARp = - ((temp < 11059) ? temp << 1
24519 : ((temp < 20070) ? temp + 11059
24520 : GSM_ADD( (word) (temp >> 2), (word) 26112 )));
24521 } else {
24522 temp = *LARp;
24523 *LARp = (temp < 11059) ? temp << 1
24524 : ((temp < 20070) ? temp + 11059
24525 : GSM_ADD( (word) (temp >> 2), (word) 26112 ));
24531 /* 4.2.10 */
24532 static void Short_term_analysis_filtering (
24533 struct gsm_state * S,
24534 register word * rp, /* [0..7] IN */
24535 register int k_n, /* k_end - k_start */
24536 register word * s /* [0..n-1] IN/OUT */
24539 * This procedure computes the short term residual signal d[..] to be fed
24540 * to the RPE-LTP loop from the s[..] signal and from the local rp[..]
24541 * array (quantized reflection coefficients). As the call of this
24542 * procedure can be done in many ways (see the interpolation of the LAR
24543 * coefficient), it is assumed that the computation begins with index
24544 * k_start (for arrays d[..] and s[..]) and stops with index k_end
24545 * (k_start and k_end are defined in 4.2.9.1). This procedure also
24546 * needs to keep the array u[0..7] in memory for each call.
24549 register word * u = S->u;
24550 register int i;
24551 register word di, zzz, ui, sav, rpi;
24553 for (; k_n--; s++) {
24555 di = sav = *s;
24557 for (i = 0; i < 8; i++) { /* YYY */
24559 ui = u[i];
24560 rpi = rp[i];
24561 u[i] = sav;
24563 zzz = GSM_MULT_R(rpi, di);
24564 sav = GSM_ADD( ui, zzz);
24566 zzz = GSM_MULT_R(rpi, ui);
24567 di = GSM_ADD( di, zzz );
24570 *s = di;
24574 #if defined(USE_FLOAT_MUL) && defined(FAST)
24576 static void Fast_Short_term_analysis_filtering (
24577 struct gsm_state * S,
24578 register word * rp, /* [0..7] IN */
24579 register int k_n, /* k_end - k_start */
24580 register word * s /* [0..n-1] IN/OUT */
24583 register word * u = S->u;
24584 register int i;
24586 float uf[8],
24587 rpf[8];
24589 register float scalef = 3.0517578125e-5;
24590 register float sav, di, temp;
24592 for (i = 0; i < 8; ++i) {
24593 uf[i] = u[i];
24594 rpf[i] = rp[i] * scalef;
24596 for (; k_n--; s++) {
24597 sav = di = *s;
24598 for (i = 0; i < 8; ++i) {
24599 register float rpfi = rpf[i];
24600 register float ufi = uf[i];
24602 uf[i] = sav;
24603 temp = rpfi * di + ufi;
24604 di += rpfi * ufi;
24605 sav = temp;
24607 *s = di;
24609 for (i = 0; i < 8; ++i) u[i] = uf[i];
24611 #endif /* ! (defined (USE_FLOAT_MUL) && defined (FAST)) */
24613 static void Short_term_synthesis_filtering (
24614 struct gsm_state * S,
24615 register word * rrp, /* [0..7] IN */
24616 register int k, /* k_end - k_start */
24617 register word * wt, /* [0..k-1] IN */
24618 register word * sr /* [0..k-1] OUT */
24621 register word * v = S->v;
24622 register int i;
24623 register word sri, tmp1, tmp2;
24625 while (k--) {
24626 sri = *wt++;
24627 for (i = 8; i--;) {
24629 /* sri = GSM_SUB( sri, gsm_mult_r( rrp[i], v[i] ) );
24631 tmp1 = rrp[i];
24632 tmp2 = v[i];
24633 tmp2 = ( tmp1 == MIN_WORD && tmp2 == MIN_WORD
24634 ? MAX_WORD
24635 : 0x0FFFF & (( (longword)tmp1 * (longword)tmp2
24636 + 16384) >> 15)) ;
24638 sri = GSM_SUB( sri, tmp2 );
24640 /* v[i+1] = GSM_ADD( v[i], gsm_mult_r( rrp[i], sri ) );
24642 tmp1 = ( tmp1 == MIN_WORD && sri == MIN_WORD
24643 ? MAX_WORD
24644 : 0x0FFFF & (( (longword)tmp1 * (longword)sri
24645 + 16384) >> 15)) ;
24647 v[i+1] = GSM_ADD( v[i], tmp1);
24649 *sr++ = v[0] = sri;
24654 #if defined(FAST) && defined(USE_FLOAT_MUL)
24656 static void Fast_Short_term_synthesis_filtering (
24657 struct gsm_state * S,
24658 register word * rrp, /* [0..7] IN */
24659 register int k, /* k_end - k_start */
24660 register word * wt, /* [0..k-1] IN */
24661 register word * sr /* [0..k-1] OUT */
24664 register word * v = S->v;
24665 register int i;
24667 float va[9], rrpa[8];
24668 register float scalef = 3.0517578125e-5, temp;
24670 for (i = 0; i < 8; ++i) {
24671 va[i] = v[i];
24672 rrpa[i] = (float)rrp[i] * scalef;
24674 while (k--) {
24675 register float sri = *wt++;
24676 for (i = 8; i--;) {
24677 sri -= rrpa[i] * va[i];
24678 if (sri < -32768.) sri = -32768.;
24679 else if (sri > 32767.) sri = 32767.;
24681 temp = va[i] + rrpa[i] * sri;
24682 if (temp < -32768.) temp = -32768.;
24683 else if (temp > 32767.) temp = 32767.;
24684 va[i+1] = temp;
24686 *sr++ = va[0] = sri;
24688 for (i = 0; i < 9; ++i) v[i] = va[i];
24691 #endif /* defined(FAST) && defined(USE_FLOAT_MUL) */
24693 void Gsm_Short_Term_Analysis_Filter (
24695 struct gsm_state * S,
24697 word * LARc, /* coded log area ratio [0..7] IN */
24698 word * s /* signal [0..159] IN/OUT */
24701 word * LARpp_j = S->LARpp[ S->j ];
24702 word * LARpp_j_1 = S->LARpp[ S->j ^= 1 ];
24704 word LARp[8];
24706 #undef FILTER
24707 #if defined(FAST) && defined(USE_FLOAT_MUL)
24708 # define FILTER (* (S->fast \
24709 ? Fast_Short_term_analysis_filtering \
24710 : Short_term_analysis_filtering ))
24712 #else
24713 # define FILTER Short_term_analysis_filtering
24714 #endif
24716 Decoding_of_the_coded_Log_Area_Ratios( LARc, LARpp_j );
24718 Coefficients_0_12( LARpp_j_1, LARpp_j, LARp );
24719 LARp_to_rp( LARp );
24720 FILTER( S, LARp, 13, s);
24722 Coefficients_13_26( LARpp_j_1, LARpp_j, LARp);
24723 LARp_to_rp( LARp );
24724 FILTER( S, LARp, 14, s + 13);
24726 Coefficients_27_39( LARpp_j_1, LARpp_j, LARp);
24727 LARp_to_rp( LARp );
24728 FILTER( S, LARp, 13, s + 27);
24730 Coefficients_40_159( LARpp_j, LARp);
24731 LARp_to_rp( LARp );
24732 FILTER( S, LARp, 120, s + 40);
24735 void Gsm_Short_Term_Synthesis_Filter (
24736 struct gsm_state * S,
24738 word * LARcr, /* received log area ratios [0..7] IN */
24739 word * wt, /* received d [0..159] IN */
24741 word * s /* signal s [0..159] OUT */
24744 word * LARpp_j = S->LARpp[ S->j ];
24745 word * LARpp_j_1 = S->LARpp[ S->j ^=1 ];
24747 word LARp[8];
24749 #undef FILTER
24750 #if defined(FAST) && defined(USE_FLOAT_MUL)
24752 # define FILTER (* (S->fast \
24753 ? Fast_Short_term_synthesis_filtering \
24754 : Short_term_synthesis_filtering ))
24755 #else
24756 # define FILTER Short_term_synthesis_filtering
24757 #endif
24759 Decoding_of_the_coded_Log_Area_Ratios( LARcr, LARpp_j );
24761 Coefficients_0_12( LARpp_j_1, LARpp_j, LARp );
24762 LARp_to_rp( LARp );
24763 FILTER( S, LARp, 13, wt, s );
24765 Coefficients_13_26( LARpp_j_1, LARpp_j, LARp);
24766 LARp_to_rp( LARp );
24767 FILTER( S, LARp, 14, wt + 13, s + 13 );
24769 Coefficients_27_39( LARpp_j_1, LARpp_j, LARp);
24770 LARp_to_rp( LARp );
24771 FILTER( S, LARp, 13, wt + 27, s + 27 );
24773 Coefficients_40_159( LARpp_j, LARp );
24774 LARp_to_rp( LARp );
24775 FILTER(S, LARp, 120, wt + 40, s + 40);
24778 ** Do not edit or modify anything in this comment block.
24779 ** The arch-tag line is a file identity tag for the GNU Arch
24780 ** revision control system.
24782 ** arch-tag: 019ac7ba-c6dd-4540-abf0-8644b6c4a633
24786 ** Copyright (C) 1999-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
24788 ** This program is free software; you can redistribute it and/or modify
24789 ** it under the terms of the GNU Lesser General Public License as published by
24790 ** the Free Software Foundation; either version 2.1 of the License, or
24791 ** (at your option) any later version.
24793 ** This program is distributed in the hope that it will be useful,
24794 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
24795 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24796 ** GNU Lesser General Public License for more details.
24798 ** You should have received a copy of the GNU Lesser General Public License
24799 ** along with this program; if not, write to the Free Software
24800 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24804 #include <stdlib.h>
24805 #include <string.h>
24806 #include <ctype.h>
24809 #define SNDFILE_MAGICK 0x1234C0DE
24811 typedef struct
24812 { int error ;
24813 const char *str ;
24814 } ErrorStruct ;
24816 static
24817 ErrorStruct SndfileErrors [] =
24819 /* Public error values and their associated strings. */
24820 { SF_ERR_NO_ERROR , "No Error." },
24821 { SF_ERR_UNRECOGNISED_FORMAT , "File opened for read. Format not recognised." },
24822 { SF_ERR_SYSTEM , "System error." /* Often replaced. */ },
24824 /* Private error values and their associated strings. */
24825 { SFE_BAD_FILE , "File does not exist or is not a regular file (possibly a pipe?)." },
24826 { SFE_BAD_FILE_READ , "File exists but no data could be read." },
24827 { SFE_OPEN_FAILED , "Could not open file." },
24828 { SFE_BAD_SNDFILE_PTR , "Not a valid SNDFILE* pointer." },
24829 { SFE_BAD_SF_INFO_PTR , "NULL SF_INFO pointer passed to libsndfile." },
24830 { SFE_BAD_SF_INCOMPLETE , "SF_PRIVATE struct incomplete and end of header parsing." },
24831 { SFE_BAD_FILE_PTR , "Bad FILE pointer." },
24832 { SFE_BAD_INT_PTR , "Internal error, Bad pointer." },
24833 { SFE_BAD_STAT_SIZE , "Error : software was misconfigured at compile time (sizeof statbuf.st_size)." },
24835 { SFE_MALLOC_FAILED , "Internal malloc () failed." },
24836 { SFE_UNIMPLEMENTED , "File contains data in an unimplemented format." },
24837 { SFE_BAD_READ_ALIGN , "Attempt to read a non-integer number of channels." },
24838 { SFE_BAD_WRITE_ALIGN , "Attempt to write a non-integer number of channels." },
24839 { SFE_UNKNOWN_FORMAT , "File contains data in an unknown format." },
24840 { SFE_NOT_READMODE , "Read attempted on file currently open for write." },
24841 { SFE_NOT_WRITEMODE , "Write attempted on file currently open for read." },
24842 { SFE_BAD_MODE_RW , "This file format does not support read/write mode." },
24843 { SFE_BAD_SF_INFO , "Internal error : SF_INFO struct incomplete." },
24844 { SFE_BAD_OFFSET , "Error : supplied offset beyond end of file." },
24845 { SFE_NO_EMBED_SUPPORT , "Error : embedding not supported for this file format." },
24846 { SFE_NO_EMBEDDED_RDWR , "Error : cannot open embedded file read/write." },
24847 { SFE_NO_PIPE_WRITE , "Error : this file format does not support pipe write." },
24848 { SFE_BAD_RDWR_FORMAT , "Attempted to open read only format for RDWR." },
24850 { SFE_INTERLEAVE_MODE , "Attempt to write to file with non-interleaved data." },
24851 { SFE_INTERLEAVE_SEEK , "Bad karma in seek during interleave read operation." },
24852 { SFE_INTERLEAVE_READ , "Bad karma in read during interleave read operation." },
24854 { SFE_INTERNAL , "Unspecified internal error." },
24855 { SFE_LOG_OVERRUN , "Log buffer has overrun. File probably broken." },
24856 { SFE_BAD_CONTROL_CMD , "Bad command passed to function sf_command()." },
24857 { SFE_BAD_ENDIAN , "Bad endian-ness. Try default endian-ness" },
24858 { SFE_CHANNEL_COUNT , "Too many channels specified." },
24860 { SFE_BAD_SEEK , "Internal psf_fseek() failed." },
24861 { SFE_NOT_SEEKABLE , "Seek attempted on unseekable file type." },
24862 { SFE_AMBIGUOUS_SEEK , "Error: combination of file open mode and seek command is ambiguous." },
24863 { SFE_WRONG_SEEK , "Error: invalid seek parameters." },
24864 { SFE_SEEK_FAILED , "Error: parameters OK, but psf_seek() failed." },
24866 { SFE_BAD_OPEN_MODE , "Error: bad mode parameter for file open." },
24867 { SFE_OPEN_PIPE_RDWR , "Error: attempt toopen a pipe in read/write mode." },
24868 { SFE_RDWR_POSITION , "Error on RDWR position (cryptic)." },
24870 { SFE_STR_NO_SUPPORT , "Error : File type does not support string data." },
24871 { SFE_STR_MAX_DATA , "Error : Maximum string data storage reached." },
24872 { SFE_STR_MAX_COUNT , "Error : Maximum string data count reached." },
24873 { SFE_STR_BAD_TYPE , "Error : Bad string data type." },
24874 { SFE_STR_NO_ADD_END , "Error : file type does not support strings added at end of file." },
24875 { SFE_STR_BAD_STRING , "Error : bad string." },
24876 { SFE_STR_WEIRD , "Error : Weird string error." },
24877 { SFE_RDWR_BAD_HEADER , "Error : Cannot open file in read/write mode due to string data in header." },
24879 { SFE_WAV_NO_RIFF , "Error in WAV file. No 'RIFF' chunk marker." },
24880 { SFE_WAV_NO_WAVE , "Error in WAV file. No 'WAVE' chunk marker." },
24881 { SFE_WAV_NO_FMT , "Error in WAV file. No 'fmt ' chunk marker." },
24882 { SFE_WAV_FMT_SHORT , "Error in WAV file. Short 'fmt ' chunk." },
24884 { SFE_WAV_FMT_TOO_BIG , "Error in WAV file. 'fmt ' chunk too large." },
24885 { SFE_WAV_BAD_FACT , "Error in WAV file. 'fact' chunk out of place." },
24886 { SFE_WAV_BAD_PEAK , "Error in WAV file. Bad 'PEAK' chunk." },
24887 { SFE_WAV_PEAK_B4_FMT , "Error in WAV file. 'PEAK' chunk found before 'fmt ' chunk." },
24889 { SFE_WAV_BAD_FORMAT , "Error in WAV file. Errors in 'fmt ' chunk." },
24890 { SFE_WAV_BAD_BLOCKALIGN , "Error in WAV file. Block alignment in 'fmt ' chunk is incorrect." },
24891 { SFE_WAV_NO_DATA , "Error in WAV file. No 'data' chunk marker." },
24892 { SFE_WAV_UNKNOWN_CHUNK , "Error in WAV file. File contains an unknown chunk marker." },
24893 { SFE_WAV_WVPK_DATA , "Error in WAV file. Data is in WAVPACK format." },
24895 { SFE_WAV_ADPCM_NOT4BIT , "Error in ADPCM WAV file. Invalid bit width." },
24896 { SFE_WAV_ADPCM_CHANNELS , "Error in ADPCM WAV file. Invalid number of channels." },
24897 { SFE_WAV_GSM610_FORMAT , "Error in GSM610 WAV file. Invalid format chunk." },
24899 { SFE_AIFF_NO_FORM , "Error in AIFF file, bad 'FORM' marker." },
24900 { SFE_AIFF_AIFF_NO_FORM , "Error in AIFF file, 'AIFF' marker without 'FORM'." },
24901 { SFE_AIFF_COMM_NO_FORM , "Error in AIFF file, 'COMM' marker without 'FORM'." },
24902 { SFE_AIFF_SSND_NO_COMM , "Error in AIFF file, 'SSND' marker without 'COMM'." },
24903 { SFE_AIFF_UNKNOWN_CHUNK , "Error in AIFF file, unknown chunk." },
24904 { SFE_AIFF_COMM_CHUNK_SIZE, "Error in AIFF file, bad 'COMM' chunk size." },
24905 { SFE_AIFF_BAD_COMM_CHUNK , "Error in AIFF file, bad 'COMM' chunk." },
24906 { SFE_AIFF_PEAK_B4_COMM , "Error in AIFF file. 'PEAK' chunk found before 'COMM' chunk." },
24907 { SFE_AIFF_BAD_PEAK , "Error in AIFF file. Bad 'PEAK' chunk." },
24908 { SFE_AIFF_NO_SSND , "Error in AIFF file, bad 'SSND' chunk." },
24909 { SFE_AIFF_NO_DATA , "Error in AIFF file, no sound data." },
24910 { SFE_AIFF_RW_SSND_NOT_LAST, "Error in AIFF file, RDWR only possible if SSND chunk at end of file." },
24912 { SFE_AU_UNKNOWN_FORMAT , "Error in AU file, unknown format." },
24913 { SFE_AU_NO_DOTSND , "Error in AU file, missing '.snd' or 'dns.' marker." },
24914 { SFE_AU_EMBED_BAD_LEN , "Embedded AU file with unknown length." },
24916 { SFE_RAW_READ_BAD_SPEC , "Error while opening RAW file for read. Must specify format and channels.\n"
24917 "Possibly trying to open unsupported format."
24919 { SFE_RAW_BAD_BITWIDTH , "Error. RAW file bitwidth must be a multiple of 8." },
24920 { SFE_RAW_BAD_FORMAT , "Error. Bad format field in SF_INFO struct when openning a RAW file for read." },
24922 { SFE_PAF_NO_MARKER , "Error in PAF file, no marker." },
24923 { SFE_PAF_VERSION , "Error in PAF file, bad version." },
24924 { SFE_PAF_UNKNOWN_FORMAT , "Error in PAF file, unknown format." },
24925 { SFE_PAF_SHORT_HEADER , "Error in PAF file. File shorter than minimal header." },
24927 { SFE_SVX_NO_FORM , "Error in 8SVX / 16SV file, no 'FORM' marker." },
24928 { SFE_SVX_NO_BODY , "Error in 8SVX / 16SV file, no 'BODY' marker." },
24929 { SFE_SVX_NO_DATA , "Error in 8SVX / 16SV file, no sound data." },
24930 { SFE_SVX_BAD_COMP , "Error in 8SVX / 16SV file, unsupported compression format." },
24931 { SFE_SVX_BAD_NAME_LENGTH , "Error in 8SVX / 16SV file, NAME chunk too long." },
24933 { SFE_NIST_BAD_HEADER , "Error in NIST file, bad header." },
24934 { SFE_NIST_CRLF_CONVERISON, "Error : NIST file damaged by Windows CR -> CRLF conversion process." },
24935 { SFE_NIST_BAD_ENCODING , "Error in NIST file, unsupported compression format." },
24937 { SFE_VOC_NO_CREATIVE , "Error in VOC file, no 'Creative Voice File' marker." },
24938 { SFE_VOC_BAD_FORMAT , "Error in VOC file, bad format." },
24939 { SFE_VOC_BAD_VERSION , "Error in VOC file, bad version number." },
24940 { SFE_VOC_BAD_MARKER , "Error in VOC file, bad marker in file." },
24941 { SFE_VOC_BAD_SECTIONS , "Error in VOC file, incompatible VOC sections." },
24942 { SFE_VOC_MULTI_SAMPLERATE, "Error in VOC file, more than one sample rate defined." },
24943 { SFE_VOC_MULTI_SECTION , "Unimplemented VOC file feature, file contains multiple sound sections." },
24944 { SFE_VOC_MULTI_PARAM , "Error in VOC file, file contains multiple bit or channel widths." },
24945 { SFE_VOC_SECTION_COUNT , "Error in VOC file, too many sections." },
24946 { SFE_VOC_NO_PIPE , "Error : not able to operate on VOC files over a pipe." },
24948 { SFE_IRCAM_NO_MARKER , "Error in IRCAM file, bad IRCAM marker." },
24949 { SFE_IRCAM_BAD_CHANNELS , "Error in IRCAM file, bad channel count." },
24950 { SFE_IRCAM_UNKNOWN_FORMAT, "Error in IRCAM file, unknow encoding format." },
24952 { SFE_W64_64_BIT , "Error in W64 file, file contains 64 bit offset." },
24954 { SFE_W64_NO_RIFF , "Error in W64 file. No 'riff' chunk marker." },
24955 { SFE_W64_NO_WAVE , "Error in W64 file. No 'wave' chunk marker." },
24956 { SFE_W64_NO_FMT , "Error in W64 file. No 'fmt ' chunk marker." },
24957 { SFE_W64_NO_DATA , "Error in W64 file. No 'data' chunk marker." },
24959 { SFE_W64_FMT_SHORT , "Error in W64 file. Short 'fmt ' chunk." },
24960 { SFE_W64_FMT_TOO_BIG , "Error in W64 file. 'fmt ' chunk too large." },
24962 { SFE_W64_ADPCM_NOT4BIT , "Error in ADPCM W64 file. Invalid bit width." },
24963 { SFE_W64_ADPCM_CHANNELS , "Error in ADPCM W64 file. Invalid number of channels." },
24964 { SFE_W64_GSM610_FORMAT , "Error in GSM610 W64 file. Invalid format chunk." },
24966 { SFE_MAT4_BAD_NAME , "Error in MAT4 file. No variable name." },
24967 { SFE_MAT4_NO_SAMPLERATE , "Error in MAT4 file. No sample rate." },
24968 { SFE_MAT4_ZERO_CHANNELS , "Error in MAT4 file. Channel count is zero." },
24970 { SFE_MAT5_BAD_ENDIAN , "Error in MAT5 file. Not able to determine endian-ness." },
24971 { SFE_MAT5_NO_BLOCK , "Error in MAT5 file. Bad block structure." },
24972 { SFE_MAT5_SAMPLE_RATE , "Error in MAT5 file. Not able to determine sample rate." },
24973 { SFE_MAT5_ZERO_CHANNELS , "Error in MAT5 file. Channel count is zero." },
24975 { SFE_PVF_NO_PVF1 , "Error in PVF file. No PVF1 marker." },
24976 { SFE_PVF_BAD_HEADER , "Error in PVF file. Bad header." },
24977 { SFE_PVF_BAD_BITWIDTH , "Error in PVF file. Bad bit width." },
24979 { SFE_XI_BAD_HEADER , "Error in XI file. Bad header." },
24980 { SFE_XI_EXCESS_SAMPLES , "Error in XI file. Excess samples in file." },
24981 { SFE_XI_NO_PIPE , "Error : not able to operate on XI files over a pipe." },
24983 { SFE_HTK_NO_PIPE , "Error : not able to operate on HTK files over a pipe." },
24985 { SFE_SDS_NOT_SDS , "Error : not an SDS file." },
24986 { SFE_SDS_BAD_BIT_WIDTH , "Error : bad bit width for SDS file." },
24988 { SFE_DWVW_BAD_BITWIDTH , "Error : Bad bit width for DWVW encoding. Must be 12, 16 or 24." },
24989 { SFE_G72X_NOT_MONO , "Error : G72x encoding does not support more than 1 channel." },
24991 { SFE_MAX_ERROR , "Maximum error number." },
24992 { SFE_MAX_ERROR + 1 , NULL }
24995 /*------------------------------------------------------------------------------
24998 static int format_from_extension (const char *filename) ;
24999 static int guess_file_type (SF_PRIVATE *psf, const char *filename) ;
25000 static int validate_sfinfo (SF_INFO *sfinfo) ;
25001 static int validate_psf (SF_PRIVATE *psf) ;
25002 static void save_header_info (SF_PRIVATE *psf) ;
25003 static void copy_filename (SF_PRIVATE *psf, const char *path) ;
25004 static int psf_close (SF_PRIVATE *psf) ;
25005 static int psf_open_file (SF_PRIVATE *psf, int mode, SF_INFO *sfinfo) ;
25007 /*------------------------------------------------------------------------------
25008 ** Private (static) variables.
25011 static int sf_errno = 0 ;
25012 static char sf_logbuffer [SF_BUFFER_LEN] = { 0 } ;
25013 static char sf_syserr [SF_SYSERR_LEN] = { 0 } ;
25015 /*------------------------------------------------------------------------------
25018 #define VALIDATE_SNDFILE_AND_ASSIGN_PSF(a,b,c) \
25019 { if (! (a)) \
25020 { sf_errno = SFE_BAD_SNDFILE_PTR ; \
25021 return 0 ; \
25022 } ; \
25023 (b) = (SF_PRIVATE*) (a) ; \
25024 if (psf_filedes_valid (b) == 0) \
25025 { (b)->error = SFE_BAD_FILE_PTR ; \
25026 return 0 ; \
25027 } ; \
25028 if ((b)->Magick != SNDFILE_MAGICK) \
25029 { (b)->error = SFE_BAD_SNDFILE_PTR ; \
25030 return 0 ; \
25031 } ; \
25032 if (c) (b)->error = 0 ; \
25035 /*------------------------------------------------------------------------------
25036 ** Public functions.
25039 SNDFILE*
25040 sf_open (const char *path, int mode, SF_INFO *sfinfo)
25041 { SF_PRIVATE *psf ;
25042 int error = 0 ;
25044 if ((psf = calloc (1, sizeof (SF_PRIVATE))) == NULL)
25045 { sf_errno = SFE_MALLOC_FAILED ;
25046 return NULL ;
25049 memset (psf, 0, sizeof (SF_PRIVATE)) ;
25051 psf_log_printf (psf, "File : %s\n", path) ;
25053 copy_filename (psf, path) ;
25055 if (strcmp (path, "-") == 0)
25056 error = psf_set_stdio (psf, mode) ;
25057 else
25058 error = psf_fopen (psf, path, mode) ;
25060 if (error == 0)
25061 error = psf_open_file (psf, mode, sfinfo) ;
25063 if (error)
25064 { sf_errno = error ;
25065 if (error == SFE_SYSTEM)
25066 LSF_SNPRINTF (sf_syserr, sizeof (sf_syserr), "%s", psf->syserr) ;
25067 LSF_SNPRINTF (sf_logbuffer, sizeof (sf_logbuffer), "%s", psf->logbuffer) ;
25068 psf_close (psf) ;
25069 return NULL ;
25072 memcpy (sfinfo, &(psf->sf), sizeof (SF_INFO)) ;
25074 return (SNDFILE*) psf ;
25075 } /* sf_open */
25077 SNDFILE*
25078 sf_open_fd (int fd, int mode, SF_INFO *sfinfo, int close_desc)
25079 { SF_PRIVATE *psf ;
25080 int error ;
25082 if ((psf = calloc (1, sizeof (SF_PRIVATE))) == NULL)
25083 { sf_errno = SFE_MALLOC_FAILED ;
25084 return NULL ;
25087 psf_set_file (psf, fd) ;
25088 psf->is_pipe = psf_is_pipe (psf) ;
25089 psf->fileoffset = psf_ftell (psf) ;
25091 if (! close_desc)
25092 psf->do_not_close_descriptor = SF_TRUE ;
25094 error = psf_open_file (psf, mode, sfinfo) ;
25096 if (error)
25097 { sf_errno = error ;
25098 if (error == SFE_SYSTEM)
25099 LSF_SNPRINTF (sf_syserr, sizeof (sf_syserr), "%s", psf->syserr) ;
25100 LSF_SNPRINTF (sf_logbuffer, sizeof (sf_logbuffer), "%s", psf->logbuffer) ;
25101 psf_close (psf) ;
25102 return NULL ;
25105 memcpy (sfinfo, &(psf->sf), sizeof (SF_INFO)) ;
25107 return (SNDFILE*) psf ;
25108 } /* sf_open_fd */
25110 /*------------------------------------------------------------------------------
25114 sf_close (SNDFILE *sndfile)
25115 { SF_PRIVATE *psf ;
25117 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
25119 return psf_close (psf) ;
25120 } /* sf_close */
25122 /*==============================================================================
25125 const char*
25126 sf_error_number (int errnum)
25127 { static const char *bad_errnum =
25128 "No error defined for this error number. This is a bug in libsndfile." ;
25129 int k ;
25131 if (errnum == SFE_MAX_ERROR)
25132 return SndfileErrors [0].str ;
25134 if (errnum < 0 || errnum > SFE_MAX_ERROR)
25135 { /* This really shouldn't happen in release versions. */
25136 printf ("Not a valid error number (%d).\n", errnum) ;
25137 return bad_errnum ;
25140 for (k = 0 ; SndfileErrors [k].str ; k++)
25141 if (errnum == SndfileErrors [k].error)
25142 return SndfileErrors [k].str ;
25144 return bad_errnum ;
25145 } /* sf_error_number */
25147 const char*
25148 sf_strerror (SNDFILE *sndfile)
25149 { SF_PRIVATE *psf = NULL ;
25150 int errnum ;
25152 if (! sndfile)
25153 { errnum = sf_errno ;
25154 if (errnum == SFE_SYSTEM && sf_syserr [0])
25155 return sf_syserr ;
25157 else
25158 { psf = (SF_PRIVATE *) sndfile ;
25160 if (psf->Magick != SNDFILE_MAGICK)
25161 return "sf_strerror : Bad magic number." ;
25163 errnum = psf->error ;
25165 if (errnum == SFE_SYSTEM && psf->syserr [0])
25166 return psf->syserr ;
25169 return sf_error_number (errnum) ;
25170 } /* sf_strerror */
25172 /*------------------------------------------------------------------------------
25176 sf_error (SNDFILE *sndfile)
25177 { SF_PRIVATE *psf ;
25179 if (! sndfile)
25180 { if (sf_error != 0)
25181 return 1 ;
25182 return 0 ;
25185 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 0) ;
25187 if (psf->error)
25188 return 1 ;
25190 return 0 ;
25191 } /* sf_error */
25193 /*------------------------------------------------------------------------------
25197 sf_perror (SNDFILE *sndfile)
25198 { SF_PRIVATE *psf ;
25199 int errnum ;
25201 if (! sndfile)
25202 { errnum = sf_errno ;
25204 else
25205 { VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 0) ;
25206 errnum = psf->error ;
25209 fprintf (stderr, "%s\n", sf_error_number (errnum)) ;
25210 return SFE_NO_ERROR ;
25211 } /* sf_perror */
25214 /*------------------------------------------------------------------------------
25218 sf_error_str (SNDFILE *sndfile, char *str, size_t maxlen)
25219 { SF_PRIVATE *psf ;
25220 int errnum ;
25222 if (! str)
25223 return SFE_INTERNAL ;
25225 if (! sndfile)
25226 errnum = sf_errno ;
25227 else
25228 { VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 0) ;
25229 errnum = psf->error ;
25232 LSF_SNPRINTF (str, maxlen, "%s", sf_error_number (errnum)) ;
25234 return SFE_NO_ERROR ;
25235 } /* sf_error_str */
25237 /*==============================================================================
25241 sf_format_check (const SF_INFO *info)
25242 { int subformat, endian ;
25244 subformat = info->format & SF_FORMAT_SUBMASK ;
25245 endian = info->format & SF_FORMAT_ENDMASK ;
25247 /* This is the place where each file format can check if the suppiled
25248 ** SF_INFO struct is valid.
25249 ** Return 0 on failure, 1 ons success.
25252 if (info->channels < 1 || info->channels > 256)
25253 return 0 ;
25255 if (info->samplerate < 0)
25256 return 0 ;
25258 switch (info->format & SF_FORMAT_TYPEMASK)
25259 { case SF_FORMAT_WAV :
25260 case SF_FORMAT_WAVEX :
25261 /* WAV is strictly little endian. */
25262 if (endian == SF_ENDIAN_BIG || endian == SF_ENDIAN_CPU)
25263 return 0 ;
25264 if (subformat == SF_FORMAT_PCM_U8 || subformat == SF_FORMAT_PCM_16)
25265 return 1 ;
25266 if (subformat == SF_FORMAT_PCM_24 || subformat == SF_FORMAT_PCM_32)
25267 return 1 ;
25268 if ((subformat == SF_FORMAT_IMA_ADPCM || subformat == SF_FORMAT_MS_ADPCM) && info->channels <= 2)
25269 return 1 ;
25270 if (subformat == SF_FORMAT_GSM610 && info->channels == 1)
25271 return 1 ;
25272 if (subformat == SF_FORMAT_ULAW || subformat == SF_FORMAT_ALAW)
25273 return 1 ;
25274 if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE)
25275 return 1 ;
25276 break ;
25278 case SF_FORMAT_AIFF :
25279 /* AIFF does allow both endian-nesses for PCM data.*/
25280 if (subformat == SF_FORMAT_PCM_16 || subformat == SF_FORMAT_PCM_24 || subformat == SF_FORMAT_PCM_32)
25281 return 1 ;
25282 /* Other encodings. Check for endian-ness. */
25283 if (endian == SF_ENDIAN_LITTLE || endian == SF_ENDIAN_CPU)
25284 return 0 ;
25285 if (subformat == SF_FORMAT_PCM_U8 || subformat == SF_FORMAT_PCM_S8)
25286 return 1 ;
25287 if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE)
25288 return 1 ;
25289 if (subformat == SF_FORMAT_ULAW || subformat == SF_FORMAT_ALAW)
25290 return 1 ;
25291 if ((subformat == SF_FORMAT_DWVW_12 || subformat == SF_FORMAT_DWVW_16 ||
25292 subformat == SF_FORMAT_DWVW_24) && info-> channels == 1)
25293 return 1 ;
25294 if (subformat == SF_FORMAT_GSM610 && info->channels == 1)
25295 return 1 ;
25296 if (subformat == SF_FORMAT_IMA_ADPCM && (info->channels == 1 || info->channels == 2))
25297 return 1 ;
25298 break ;
25300 case SF_FORMAT_AU :
25301 if (subformat == SF_FORMAT_PCM_S8 || subformat == SF_FORMAT_PCM_16)
25302 return 1 ;
25303 if (subformat == SF_FORMAT_PCM_24 || subformat == SF_FORMAT_PCM_32)
25304 return 1 ;
25305 if (subformat == SF_FORMAT_ULAW || subformat == SF_FORMAT_ALAW)
25306 return 1 ;
25307 if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE)
25308 return 1 ;
25309 if (subformat == SF_FORMAT_G721_32 && info->channels == 1)
25310 return 1 ;
25311 if (subformat == SF_FORMAT_G723_24 && info->channels == 1)
25312 return 1 ;
25313 if (subformat == SF_FORMAT_G723_40 && info->channels == 1)
25314 return 1 ;
25315 break ;
25317 case SF_FORMAT_RAW :
25318 if (subformat == SF_FORMAT_PCM_U8 || subformat == SF_FORMAT_PCM_S8 || subformat == SF_FORMAT_PCM_16)
25319 return 1 ;
25320 if (subformat == SF_FORMAT_PCM_24 || subformat == SF_FORMAT_PCM_32)
25321 return 1 ;
25322 if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE)
25323 return 1 ;
25324 if (subformat == SF_FORMAT_ALAW || subformat == SF_FORMAT_ULAW)
25325 return 1 ;
25326 if ((subformat == SF_FORMAT_DWVW_12 || subformat == SF_FORMAT_DWVW_16 ||
25327 subformat == SF_FORMAT_DWVW_24) && info-> channels == 1)
25328 return 1 ;
25329 if (subformat == SF_FORMAT_GSM610 && info->channels == 1)
25330 return 1 ;
25331 if (subformat == SF_FORMAT_VOX_ADPCM && info->channels == 1)
25332 return 1 ;
25333 break ;
25335 case SF_FORMAT_PAF :
25336 if (subformat == SF_FORMAT_PCM_S8 || subformat == SF_FORMAT_PCM_16)
25337 return 1 ;
25338 if (subformat == SF_FORMAT_PCM_24 || subformat == SF_FORMAT_PCM_32)
25339 return 1 ;
25340 break ;
25342 case SF_FORMAT_SVX :
25343 /* SVX currently does not support more than one channel for write.
25344 ** Read will allow more than one channel but only allow one here.
25346 if (info->channels != 1)
25347 return 0 ;
25348 /* Always big endian. */
25349 if (endian == SF_ENDIAN_LITTLE || endian == SF_ENDIAN_CPU)
25350 return 0 ;
25352 if ((subformat == SF_FORMAT_PCM_S8 || subformat == SF_FORMAT_PCM_16) && info->channels == 1)
25353 return 1 ;
25354 break ;
25356 case SF_FORMAT_NIST :
25357 if (subformat == SF_FORMAT_PCM_S8 || subformat == SF_FORMAT_PCM_16)
25358 return 1 ;
25359 if (subformat == SF_FORMAT_PCM_24 || subformat == SF_FORMAT_PCM_32)
25360 return 1 ;
25361 if (subformat == SF_FORMAT_ULAW || subformat == SF_FORMAT_ALAW)
25362 return 1 ;
25363 break ;
25365 case SF_FORMAT_IRCAM :
25366 if (subformat == SF_FORMAT_PCM_16 || subformat == SF_FORMAT_PCM_24 || subformat == SF_FORMAT_PCM_32)
25367 return 1 ;
25368 if (subformat == SF_FORMAT_ULAW || subformat == SF_FORMAT_ALAW || subformat == SF_FORMAT_FLOAT)
25369 return 1 ;
25370 break ;
25372 case SF_FORMAT_VOC :
25373 /* VOC is strictly little endian. */
25374 if (endian == SF_ENDIAN_BIG || endian == SF_ENDIAN_CPU)
25375 return 0 ;
25376 if (subformat == SF_FORMAT_PCM_U8 || subformat == SF_FORMAT_PCM_16)
25377 return 1 ;
25378 if (subformat == SF_FORMAT_ULAW || subformat == SF_FORMAT_ALAW)
25379 return 1 ;
25380 break ;
25382 case SF_FORMAT_W64 :
25383 /* W64 is strictly little endian. */
25384 if (endian == SF_ENDIAN_BIG || endian == SF_ENDIAN_CPU)
25385 return 0 ;
25386 if (subformat == SF_FORMAT_PCM_U8 || subformat == SF_FORMAT_PCM_16)
25387 return 1 ;
25388 if (subformat == SF_FORMAT_PCM_24 || subformat == SF_FORMAT_PCM_32)
25389 return 1 ;
25390 if ((subformat == SF_FORMAT_IMA_ADPCM || subformat == SF_FORMAT_MS_ADPCM) && info->channels <= 2)
25391 return 1 ;
25392 if (subformat == SF_FORMAT_GSM610 && info->channels == 1)
25393 return 1 ;
25394 if (subformat == SF_FORMAT_ULAW || subformat == SF_FORMAT_ALAW)
25395 return 1 ;
25396 if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE)
25397 return 1 ;
25398 break ;
25400 case SF_FORMAT_MAT4 :
25401 if (subformat == SF_FORMAT_PCM_16 || subformat == SF_FORMAT_PCM_32)
25402 return 1 ;
25403 if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE)
25404 return 1 ;
25405 break ;
25407 case SF_FORMAT_MAT5 :
25408 if (subformat == SF_FORMAT_PCM_U8 || subformat == SF_FORMAT_PCM_16 || subformat == SF_FORMAT_PCM_32)
25409 return 1 ;
25410 if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE)
25411 return 1 ;
25412 break ;
25414 case SF_FORMAT_PVF :
25415 if (subformat == SF_FORMAT_PCM_S8 || subformat == SF_FORMAT_PCM_16 || subformat == SF_FORMAT_PCM_32)
25416 return 1 ;
25417 break ;
25419 case SF_FORMAT_XI :
25420 if (info->channels != 1)
25421 return 0 ;
25422 if (subformat == SF_FORMAT_DPCM_8 || subformat == SF_FORMAT_DPCM_16)
25423 return 1 ;
25424 break ;
25426 case SF_FORMAT_HTK :
25427 /* HTK is strictly big endian. */
25428 if (endian == SF_ENDIAN_LITTLE || endian == SF_ENDIAN_CPU)
25429 return 0 ;
25430 if (info->channels != 1)
25431 return 0 ;
25432 if (subformat == SF_FORMAT_PCM_16)
25433 return 1 ;
25434 break ;
25436 case SF_FORMAT_SDS :
25437 /* SDS is strictly big endian. */
25438 if (endian == SF_ENDIAN_LITTLE || endian == SF_ENDIAN_CPU)
25439 return 0 ;
25440 if (info->channels != 1)
25441 return 0 ;
25442 if (subformat == SF_FORMAT_PCM_S8 || subformat == SF_FORMAT_PCM_16 || subformat == SF_FORMAT_PCM_24)
25443 return 1 ;
25444 break ;
25446 case SF_FORMAT_AVR :
25447 /* SDS is strictly big endian. */
25448 if (endian == SF_ENDIAN_LITTLE || endian == SF_ENDIAN_CPU)
25449 return 0 ;
25450 if (info->channels < 1 || info->channels > 2)
25451 return 0 ;
25452 if (subformat == SF_FORMAT_PCM_U8 || subformat == SF_FORMAT_PCM_S8 || subformat == SF_FORMAT_PCM_16)
25453 return 1 ;
25454 break ;
25457 case SF_FORMAT_SD2 :
25458 /+* SD2 is strictly big endian. *+/
25459 if (endian == SF_ENDIAN_LITTLE || endian == SF_ENDIAN_CPU)
25460 return 0 ;
25461 if (subformat == SF_FORMAT_PCM_16)
25462 return 1 ;
25463 break ;
25466 default : break ;
25469 return 0 ;
25470 } /* sf_format_check */
25472 /*------------------------------------------------------------------------------
25476 sf_command (SNDFILE *sndfile, int command, void *data, int datasize)
25477 { SF_PRIVATE *psf = NULL ;
25479 /* This set of commands do not need the sndfile parameter. */
25480 switch (command)
25481 { case SFC_GET_LIB_VERSION :
25482 if (data == NULL)
25483 return (psf->error = SFE_BAD_CONTROL_CMD) ;
25484 if (ENABLE_EXPERIMENTAL_CODE)
25485 LSF_SNPRINTF (data, datasize, "%s-%s-exp", PACKAGE_NAME, PACKAGE_VERSION) ;
25486 else
25487 LSF_SNPRINTF (data, datasize, "%s-%s", PACKAGE_NAME, PACKAGE_VERSION) ;
25488 return 0 ;
25490 case SFC_GET_SIMPLE_FORMAT_COUNT :
25491 if (data == NULL || datasize != SIGNED_SIZEOF (int))
25492 return (sf_errno = SFE_BAD_CONTROL_CMD) ;
25493 *((int*) data) = psf_get_format_simple_count () ;
25494 return 0 ;
25496 case SFC_GET_SIMPLE_FORMAT :
25497 if (data == NULL || datasize != SIGNED_SIZEOF (SF_FORMAT_INFO))
25498 return (sf_errno = SFE_BAD_CONTROL_CMD) ;
25499 return psf_get_format_simple (data) ;
25501 case SFC_GET_FORMAT_MAJOR_COUNT :
25502 if (data == NULL || datasize != SIGNED_SIZEOF (int))
25503 return (sf_errno = SFE_BAD_CONTROL_CMD) ;
25504 *((int*) data) = psf_get_format_major_count () ;
25505 return 0 ;
25507 case SFC_GET_FORMAT_MAJOR :
25508 if (data == NULL || datasize != SIGNED_SIZEOF (SF_FORMAT_INFO))
25509 return (sf_errno = SFE_BAD_CONTROL_CMD) ;
25510 return psf_get_format_major (data) ;
25512 case SFC_GET_FORMAT_SUBTYPE_COUNT :
25513 if (data == NULL || datasize != SIGNED_SIZEOF (int))
25514 return (sf_errno = SFE_BAD_CONTROL_CMD) ;
25515 *((int*) data) = psf_get_format_subtype_count () ;
25516 return 0 ;
25518 case SFC_GET_FORMAT_SUBTYPE :
25519 if (data == NULL || datasize != SIGNED_SIZEOF (SF_FORMAT_INFO))
25520 return (sf_errno = SFE_BAD_CONTROL_CMD) ;
25521 return psf_get_format_subtype (data) ;
25523 case SFC_GET_FORMAT_INFO :
25524 if (data == NULL || datasize != SIGNED_SIZEOF (SF_FORMAT_INFO))
25525 return (sf_errno = SFE_BAD_CONTROL_CMD) ;
25526 return psf_get_format_info (data) ;
25529 if (! sndfile && command == SFC_GET_LOG_INFO)
25530 { if (data == NULL)
25531 return (psf->error = SFE_BAD_CONTROL_CMD) ;
25532 LSF_SNPRINTF (data, datasize, "%s", sf_logbuffer) ;
25533 return 0 ;
25536 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
25538 switch (command)
25539 { case SFC_SET_NORM_FLOAT :
25540 psf->norm_float = (datasize) ? SF_TRUE : SF_FALSE ;
25541 return psf->norm_float ;
25543 case SFC_SET_NORM_DOUBLE :
25544 psf->norm_double = (datasize) ? SF_TRUE : SF_FALSE ;
25545 return psf->norm_double ;
25547 case SFC_GET_NORM_FLOAT :
25548 return psf->norm_float ;
25550 case SFC_GET_NORM_DOUBLE :
25551 return psf->norm_double ;
25553 case SFC_SET_ADD_PEAK_CHUNK :
25554 { int format = psf->sf.format & SF_FORMAT_TYPEMASK ;
25556 /* Only WAV and AIFF support the PEAK chunk. */
25557 if (format != SF_FORMAT_WAV && format != SF_FORMAT_WAVEX && format != SF_FORMAT_AIFF)
25558 return SF_FALSE ;
25560 format = psf->sf.format & SF_FORMAT_SUBMASK ;
25562 /* Only files containg the following data types support the PEAK chunk. */
25563 if (format != SF_FORMAT_FLOAT && format != SF_FORMAT_DOUBLE)
25564 return SF_FALSE ;
25567 /* Can only do this is in SFM_WRITE mode. */
25568 if (psf->mode != SFM_WRITE)
25569 return SF_FALSE ;
25570 /* If data has already been written this must fail. */
25571 if (psf->have_written)
25572 return psf->has_peak ;
25573 /* Everything seems OK, so set psf->has_peak and re-write header. */
25574 psf->has_peak = (datasize) ? SF_TRUE : SF_FALSE ;
25575 if (psf->write_header)
25576 psf->write_header (psf, SF_TRUE) ;
25577 return psf->has_peak ;
25579 case SFC_GET_LOG_INFO :
25580 if (data == NULL)
25581 return (psf->error = SFE_BAD_CONTROL_CMD) ;
25582 LSF_SNPRINTF (data, datasize, "%s", psf->logbuffer) ;
25583 break ;
25585 case SFC_CALC_SIGNAL_MAX :
25586 if (data == NULL || datasize != sizeof (double))
25587 return (psf->error = SFE_BAD_CONTROL_CMD) ;
25588 *((double*) data) = psf_calc_signal_max (psf, SF_FALSE) ;
25589 break ;
25591 case SFC_CALC_NORM_SIGNAL_MAX :
25592 if (data == NULL || datasize != sizeof (double))
25593 return (psf->error = SFE_BAD_CONTROL_CMD) ;
25594 *((double*) data) = psf_calc_signal_max (psf, SF_TRUE) ;
25595 break ;
25597 case SFC_CALC_MAX_ALL_CHANNELS :
25598 if (data == NULL || datasize != SIGNED_SIZEOF (double) * psf->sf.channels)
25599 return (psf->error = SFE_BAD_CONTROL_CMD) ;
25600 return psf_calc_max_all_channels (psf, (double*) data, SF_FALSE) ;
25602 case SFC_CALC_NORM_MAX_ALL_CHANNELS :
25603 if (data == NULL || datasize != SIGNED_SIZEOF (double) * psf->sf.channels)
25604 return (psf->error = SFE_BAD_CONTROL_CMD) ;
25605 return psf_calc_max_all_channels (psf, (double*) data, SF_TRUE) ;
25607 case SFC_UPDATE_HEADER_NOW :
25608 if (psf->write_header)
25609 psf->write_header (psf, SF_TRUE) ;
25610 break ;
25612 case SFC_SET_UPDATE_HEADER_AUTO :
25613 psf->auto_header = datasize ? SF_TRUE : SF_FALSE ;
25614 return psf->auto_header ;
25615 break ;
25617 case SFC_SET_ADD_DITHER_ON_WRITE :
25618 case SFC_SET_ADD_DITHER_ON_READ :
25620 ** FIXME !
25621 ** These are obsolete. Just return.
25622 ** Remove some time after version 1.0.8.
25624 break ;
25626 case SFC_SET_DITHER_ON_WRITE :
25627 if (data == NULL || datasize != SIGNED_SIZEOF (SF_DITHER_INFO))
25628 return (psf->error = SFE_BAD_CONTROL_CMD) ;
25629 memcpy (&psf->write_dither, data, sizeof (psf->write_dither)) ;
25630 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
25631 dither_init (psf, SFM_WRITE) ;
25632 break ;
25634 case SFC_SET_DITHER_ON_READ :
25635 if (data == NULL || datasize != SIGNED_SIZEOF (SF_DITHER_INFO))
25636 return (psf->error = SFE_BAD_CONTROL_CMD) ;
25637 memcpy (&psf->read_dither, data, sizeof (psf->read_dither)) ;
25638 if (psf->mode == SFM_READ || psf->mode == SFM_RDWR)
25639 dither_init (psf, SFM_READ) ;
25640 break ;
25642 case SFC_FILE_TRUNCATE :
25643 if (psf->mode != SFM_WRITE && psf->mode != SFM_RDWR)
25644 return SF_TRUE ;
25645 if (datasize != sizeof (sf_count_t))
25646 return SF_TRUE ;
25647 { sf_count_t position ;
25649 position = *((sf_count_t*) data) ;
25651 if (sf_seek (sndfile, position, SEEK_SET) != position)
25652 return SF_TRUE ;
25654 psf->sf.frames = position ;
25656 position = psf_fseek (psf, 0, SEEK_CUR) ;
25658 return psf_ftruncate (psf, position) ;
25660 break ;
25662 case SFC_SET_RAW_START_OFFSET :
25663 if (data == NULL || datasize != sizeof (sf_count_t))
25664 return (psf->error = SFE_BAD_CONTROL_CMD) ;
25665 if ((psf->sf.format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
25666 return (psf->error = SFE_BAD_CONTROL_CMD) ;
25668 psf->dataoffset = *((sf_count_t*) data) ;
25669 sf_seek (sndfile, 0, SEEK_CUR) ;
25670 break ;
25672 case SFC_GET_EMBED_FILE_INFO :
25673 if (data == NULL || datasize != sizeof (SF_EMBED_FILE_INFO))
25674 return (psf->error = SFE_BAD_CONTROL_CMD) ;
25676 ((SF_EMBED_FILE_INFO*) data)->offset = psf->fileoffset ;
25677 ((SF_EMBED_FILE_INFO*) data)->length = psf->filelength ;
25678 break ;
25680 /* Lite remove start */
25681 case SFC_TEST_IEEE_FLOAT_REPLACE :
25682 psf->ieee_replace = (datasize) ? SF_TRUE : SF_FALSE ;
25683 if ((psf->sf.format & SF_FORMAT_SUBMASK) == SF_FORMAT_FLOAT)
25684 float32_init (psf) ;
25685 else if ((psf->sf.format & SF_FORMAT_SUBMASK) == SF_FORMAT_DOUBLE)
25686 double64_init (psf) ;
25687 else
25688 return (psf->error = SFE_BAD_CONTROL_CMD) ;
25689 break ;
25690 /* Lite remove end */
25692 case SFC_SET_CLIPPING :
25693 psf->add_clipping = (datasize) ? SF_TRUE : SF_FALSE ;
25694 return psf->add_clipping ;
25696 case SFC_GET_CLIPPING :
25697 return psf->add_clipping ;
25699 default :
25700 /* Must be a file specific command. Pass it on. */
25701 if (psf->command)
25702 return psf->command (psf, command, data, datasize) ;
25704 psf_log_printf (psf, "*** sf_command : cmd = 0x%X\n", command) ;
25705 return (psf->error = SFE_BAD_CONTROL_CMD) ;
25708 return 0 ;
25709 } /* sf_command */
25711 /*------------------------------------------------------------------------------
25714 sf_count_t
25715 sf_seek (SNDFILE *sndfile, sf_count_t offset, int whence)
25716 { SF_PRIVATE *psf ;
25717 sf_count_t seek_from_start = 0, retval ;
25719 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
25721 if (! psf->sf.seekable)
25722 { psf->error = SFE_NOT_SEEKABLE ;
25723 return ((sf_count_t) -1) ;
25726 /* If the whence parameter has a mode ORed in, check to see that
25727 ** it makes sense.
25729 if (((whence & SFM_MASK) == SFM_WRITE && psf->mode == SFM_READ) ||
25730 ((whence & SFM_MASK) == SFM_WRITE && psf->mode == SFM_WRITE))
25731 { psf->error = SFE_WRONG_SEEK ;
25732 return ((sf_count_t) -1) ;
25735 /* Convert all SEEK_CUR and SEEK_END into seek_from_start to be
25736 ** used with SEEK_SET.
25738 switch (whence)
25739 { /* The SEEK_SET behaviour is independant of mode. */
25740 case SEEK_SET :
25741 case SEEK_SET | SFM_READ :
25742 case SEEK_SET | SFM_WRITE :
25743 case SEEK_SET | SFM_RDWR :
25744 seek_from_start = offset ;
25745 break ;
25747 /* The SEEK_CUR is a little more tricky. */
25748 case SEEK_CUR :
25749 if (offset == 0)
25750 { if (psf->mode == SFM_READ)
25751 return psf->read_current ;
25752 if (psf->mode == SFM_WRITE)
25753 return psf->write_current ;
25755 if (psf->mode == SFM_READ)
25756 seek_from_start = psf->read_current + offset ;
25757 else if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
25758 seek_from_start = psf->write_current + offset ;
25759 else
25760 psf->error = SFE_AMBIGUOUS_SEEK ;
25761 break ;
25763 case SEEK_CUR | SFM_READ :
25764 if (offset == 0)
25765 return psf->read_current ;
25766 seek_from_start = psf->read_current + offset ;
25767 break ;
25769 case SEEK_CUR | SFM_WRITE :
25770 if (offset == 0)
25771 return psf->write_current ;
25772 seek_from_start = psf->write_current + offset ;
25773 break ;
25775 /* The SEEK_END */
25776 case SEEK_END :
25777 case SEEK_END | SFM_READ :
25778 case SEEK_END | SFM_WRITE :
25779 seek_from_start = psf->sf.frames + offset ;
25780 break ;
25782 default :
25783 psf->error = SFE_BAD_SEEK ;
25784 break ;
25787 if (psf->error)
25788 return ((sf_count_t) -1) ;
25790 if (seek_from_start < 0 || seek_from_start > psf->sf.frames)
25791 { psf->error = SFE_BAD_SEEK ;
25792 return ((sf_count_t) -1) ;
25795 if (psf->seek)
25796 { int new_mode = (whence & SFM_MASK) ? (whence & SFM_MASK) : psf->mode ;
25798 retval = psf->seek (psf, new_mode, seek_from_start) ;
25800 switch (new_mode)
25801 { case SFM_READ :
25802 psf->read_current = retval ;
25803 break ;
25804 case SFM_WRITE :
25805 psf->write_current = retval ;
25806 break ;
25807 case SFM_RDWR :
25808 psf->read_current = retval ;
25809 psf->write_current = retval ;
25810 new_mode = SFM_READ ;
25811 break ;
25814 psf->last_op = new_mode ;
25816 return retval ;
25819 psf->error = SFE_AMBIGUOUS_SEEK ;
25820 return ((sf_count_t) -1) ;
25821 } /* sf_seek */
25823 /*------------------------------------------------------------------------------
25826 const char*
25827 sf_get_string (SNDFILE *sndfile, int str_type)
25828 { SF_PRIVATE *psf ;
25830 if ((psf = (SF_PRIVATE*) sndfile) == NULL)
25831 return NULL ;
25832 if (psf->Magick != SNDFILE_MAGICK)
25833 return NULL ;
25835 return psf_get_string (psf, str_type) ;
25836 } /* sf_get_string */
25839 sf_set_string (SNDFILE *sndfile, int str_type, const char* str)
25840 { SF_PRIVATE *psf ;
25842 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
25844 return psf_store_string (psf, str_type, str) ;
25845 } /* sf_get_string */
25847 /*==============================================================================
25850 sf_count_t
25851 sf_read_raw (SNDFILE *sndfile, void *ptr, sf_count_t bytes)
25852 { SF_PRIVATE *psf ;
25853 sf_count_t count ;
25854 int bytewidth, blockwidth ;
25856 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
25858 bytewidth = (psf->bytewidth > 0) ? psf->bytewidth : 1 ;
25859 blockwidth = (psf->blockwidth > 0) ? psf->blockwidth : 1 ;
25861 if (psf->mode == SFM_WRITE)
25862 { psf->error = SFE_NOT_READMODE ;
25863 return 0 ;
25866 if (bytes < 0 || psf->read_current >= psf->datalength)
25867 { psf_memset (ptr, 0, bytes) ;
25868 return 0 ;
25871 if (bytes % (psf->sf.channels * bytewidth))
25872 { psf->error = SFE_BAD_READ_ALIGN ;
25873 return 0 ;
25876 count = psf_fread (ptr, 1, bytes, psf) ;
25878 if (count < bytes)
25879 psf_memset (((char*) ptr) + count, 0, bytes - count) ;
25881 psf->read_current += count / blockwidth ;
25883 psf->last_op = SFM_READ ;
25885 return count ;
25886 } /* sf_read_raw */
25888 /*------------------------------------------------------------------------------
25891 sf_count_t
25892 sf_read_short (SNDFILE *sndfile, short *ptr, sf_count_t len)
25893 { SF_PRIVATE *psf ;
25894 sf_count_t count, extra ;
25896 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
25898 if (psf->mode == SFM_WRITE)
25899 { psf->error = SFE_NOT_READMODE ;
25900 return 0 ;
25903 if (len % psf->sf.channels)
25904 { psf->error = SFE_BAD_READ_ALIGN ;
25905 return 0 ;
25908 if (len <= 0 || psf->read_current >= psf->sf.frames)
25909 { psf_memset (ptr, 0, len * sizeof (short)) ;
25910 return 0 ; /* End of file. */
25913 if (! psf->read_short || psf->seek == NULL)
25914 { psf->error = SFE_UNIMPLEMENTED ;
25915 return 0 ;
25918 if (psf->last_op != SFM_READ)
25919 if (psf->seek (psf, SFM_READ, psf->read_current) < 0)
25920 return 0 ;
25922 count = psf->read_short (psf, ptr, len) ;
25924 if (psf->read_current + count / psf->sf.channels > psf->sf.frames)
25925 { count = (psf->sf.frames - psf->read_current) * psf->sf.channels ;
25926 extra = len - count ;
25927 psf_memset (ptr + count, 0, extra * sizeof (short)) ;
25928 psf->read_current = psf->sf.frames ;
25931 psf->read_current += count / psf->sf.channels ;
25933 psf->last_op = SFM_READ ;
25935 if (psf->read_current > psf->sf.frames)
25936 { count = psf->sf.channels * (psf->read_current - psf->sf.frames) ;
25937 psf->read_current = psf->sf.frames ;
25940 return count ;
25941 } /* sf_read_short */
25943 sf_count_t
25944 sf_readf_short (SNDFILE *sndfile, short *ptr, sf_count_t frames)
25945 { SF_PRIVATE *psf ;
25946 sf_count_t count, extra ;
25948 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
25950 if (psf->mode == SFM_WRITE)
25951 { psf->error = SFE_NOT_READMODE ;
25952 return 0 ;
25955 if (frames <= 0 || psf->read_current >= psf->sf.frames)
25956 { psf_memset (ptr, 0, frames * psf->sf.channels * sizeof (short)) ;
25957 return 0 ; /* End of file. */
25960 if (! psf->read_short || psf->seek == NULL)
25961 { psf->error = SFE_UNIMPLEMENTED ;
25962 return 0 ;
25965 if (psf->last_op != SFM_READ)
25966 if (psf->seek (psf, SFM_READ, psf->read_current) < 0)
25967 return 0 ;
25969 count = psf->read_short (psf, ptr, frames * psf->sf.channels) ;
25971 if (psf->read_current + count / psf->sf.channels > psf->sf.frames)
25972 { count = (psf->sf.frames - psf->read_current) * psf->sf.channels ;
25973 extra = frames * psf->sf.channels - count ;
25974 psf_memset (ptr + count, 0, extra * sizeof (short)) ;
25975 psf->read_current = psf->sf.frames ;
25978 psf->read_current += count / psf->sf.channels ;
25980 psf->last_op = SFM_READ ;
25982 if (psf->read_current > psf->sf.frames)
25983 { count = psf->sf.channels * (psf->read_current - psf->sf.frames) ;
25984 psf->read_current = psf->sf.frames ;
25987 return count / psf->sf.channels ;
25988 } /* sf_readf_short */
25990 /*------------------------------------------------------------------------------
25993 sf_count_t
25994 sf_read_int (SNDFILE *sndfile, int *ptr, sf_count_t len)
25995 { SF_PRIVATE *psf ;
25996 sf_count_t count, extra ;
25998 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
26000 if (psf->mode == SFM_WRITE)
26001 { psf->error = SFE_NOT_READMODE ;
26002 return 0 ;
26005 if (len % psf->sf.channels)
26006 { psf->error = SFE_BAD_READ_ALIGN ;
26007 return 0 ;
26010 if (len <= 0 || psf->read_current >= psf->sf.frames)
26011 { psf_memset (ptr, 0, len * sizeof (int)) ;
26012 return 0 ;
26015 if (! psf->read_int || psf->seek == NULL)
26016 { psf->error = SFE_UNIMPLEMENTED ;
26017 return 0 ;
26020 if (psf->last_op != SFM_READ)
26021 if (psf->seek (psf, SFM_READ, psf->read_current) < 0)
26022 return 0 ;
26024 count = psf->read_int (psf, ptr, len) ;
26026 if (psf->read_current + count / psf->sf.channels > psf->sf.frames)
26027 { count = (psf->sf.frames - psf->read_current) * psf->sf.channels ;
26028 extra = len - count ;
26029 psf_memset (ptr + count, 0, extra * sizeof (int)) ;
26030 psf->read_current = psf->sf.frames ;
26033 psf->read_current += count / psf->sf.channels ;
26035 psf->last_op = SFM_READ ;
26037 if (psf->read_current > psf->sf.frames)
26038 { count = psf->sf.channels * (psf->read_current - psf->sf.frames) ;
26039 psf->read_current = psf->sf.frames ;
26042 return count ;
26043 } /* sf_read_int */
26045 sf_count_t
26046 sf_readf_int (SNDFILE *sndfile, int *ptr, sf_count_t frames)
26047 { SF_PRIVATE *psf ;
26048 sf_count_t count, extra ;
26050 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
26052 if (psf->mode == SFM_WRITE)
26053 { psf->error = SFE_NOT_READMODE ;
26054 return 0 ;
26057 if (frames <= 0 || psf->read_current >= psf->sf.frames)
26058 { psf_memset (ptr, 0, frames * psf->sf.channels * sizeof (int)) ;
26059 return 0 ;
26062 if (! psf->read_int || psf->seek == NULL)
26063 { psf->error = SFE_UNIMPLEMENTED ;
26064 return 0 ;
26067 if (psf->last_op != SFM_READ)
26068 if (psf->seek (psf, SFM_READ, psf->read_current) < 0)
26069 return 0 ;
26071 count = psf->read_int (psf, ptr, frames * psf->sf.channels) ;
26073 if (psf->read_current + count / psf->sf.channels > psf->sf.frames)
26074 { count = (psf->sf.frames - psf->read_current) * psf->sf.channels ;
26075 extra = frames * psf->sf.channels - count ;
26076 psf_memset (ptr + count, 0, extra * sizeof (int)) ;
26077 psf->read_current = psf->sf.frames ;
26080 psf->read_current += count / psf->sf.channels ;
26082 psf->last_op = SFM_READ ;
26084 if (psf->read_current > psf->sf.frames)
26085 { count = psf->sf.channels * (psf->read_current - psf->sf.frames) ;
26086 psf->read_current = psf->sf.frames ;
26089 return count / psf->sf.channels ;
26090 } /* sf_readf_int */
26092 /*------------------------------------------------------------------------------
26095 sf_count_t
26096 sf_read_float (SNDFILE *sndfile, float *ptr, sf_count_t len)
26097 { SF_PRIVATE *psf ;
26098 sf_count_t count, extra ;
26100 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
26102 if (psf->mode == SFM_WRITE)
26103 { psf->error = SFE_NOT_READMODE ;
26104 return 0 ;
26107 if (len % psf->sf.channels)
26108 { psf->error = SFE_BAD_READ_ALIGN ;
26109 return 0 ;
26112 if (len <= 0 || psf->read_current >= psf->sf.frames)
26113 { psf_memset (ptr, 0, len * sizeof (float)) ;
26114 return 0 ;
26117 if (! psf->read_float || psf->seek == NULL)
26118 { psf->error = SFE_UNIMPLEMENTED ;
26119 return 0 ;
26122 if (psf->last_op != SFM_READ)
26123 if (psf->seek (psf, SFM_READ, psf->read_current) < 0)
26124 return 0 ;
26126 count = psf->read_float (psf, ptr, len) ;
26128 if (psf->read_current + count / psf->sf.channels > psf->sf.frames)
26129 { count = (psf->sf.frames - psf->read_current) * psf->sf.channels ;
26130 extra = len - count ;
26131 psf_memset (ptr + count, 0, extra * sizeof (float)) ;
26132 psf->read_current = psf->sf.frames ;
26135 psf->read_current += count / psf->sf.channels ;
26137 psf->last_op = SFM_READ ;
26139 if (psf->read_current > psf->sf.frames)
26140 { count = psf->sf.channels * (psf->read_current - psf->sf.frames) ;
26141 psf->read_current = psf->sf.frames ;
26144 return count ;
26145 } /* sf_read_float */
26147 sf_count_t
26148 sf_readf_float (SNDFILE *sndfile, float *ptr, sf_count_t frames)
26149 { SF_PRIVATE *psf ;
26150 sf_count_t count, extra ;
26152 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
26154 if (psf->mode == SFM_WRITE)
26155 { psf->error = SFE_NOT_READMODE ;
26156 return 0 ;
26159 if (frames <= 0 || psf->read_current >= psf->sf.frames)
26160 { psf_memset (ptr, 0, frames * psf->sf.channels * sizeof (float)) ;
26161 return 0 ;
26164 if (! psf->read_float || psf->seek == NULL)
26165 { psf->error = SFE_UNIMPLEMENTED ;
26166 return 0 ;
26169 if (psf->last_op != SFM_READ)
26170 if (psf->seek (psf, SFM_READ, psf->read_current) < 0)
26171 return 0 ;
26173 count = psf->read_float (psf, ptr, frames * psf->sf.channels) ;
26175 if (psf->read_current + count / psf->sf.channels > psf->sf.frames)
26176 { count = (psf->sf.frames - psf->read_current) * psf->sf.channels ;
26177 extra = frames * psf->sf.channels - count ;
26178 psf_memset (ptr + count, 0, extra * sizeof (float)) ;
26179 psf->read_current = psf->sf.frames ;
26182 psf->read_current += count / psf->sf.channels ;
26184 psf->last_op = SFM_READ ;
26186 if (psf->read_current > psf->sf.frames)
26187 { count = psf->sf.channels * (psf->read_current - psf->sf.frames) ;
26188 psf->read_current = psf->sf.frames ;
26191 return count / psf->sf.channels ;
26192 } /* sf_readf_float */
26194 /*------------------------------------------------------------------------------
26197 sf_count_t
26198 sf_read_double (SNDFILE *sndfile, double *ptr, sf_count_t len)
26199 { SF_PRIVATE *psf ;
26200 sf_count_t count, extra ;
26202 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
26204 if (psf->mode == SFM_WRITE)
26205 { psf->error = SFE_NOT_READMODE ;
26206 return 0 ;
26209 if (len % psf->sf.channels)
26210 { psf->error = SFE_BAD_READ_ALIGN ;
26211 return 0 ;
26214 if (len <= 0 || psf->read_current >= psf->sf.frames)
26215 { psf_memset (ptr, 0, len * sizeof (double)) ;
26216 return 0 ;
26219 if (! psf->read_double || psf->seek == NULL)
26220 { psf->error = SFE_UNIMPLEMENTED ;
26221 return 0 ;
26224 if (psf->last_op != SFM_READ)
26225 if (psf->seek (psf, SFM_READ, psf->read_current) < 0)
26226 return 0 ;
26228 count = psf->read_double (psf, ptr, len) ;
26230 if (psf->read_current + count / psf->sf.channels > psf->sf.frames)
26231 { count = (psf->sf.frames - psf->read_current) * psf->sf.channels ;
26232 extra = len - count ;
26233 psf_memset (ptr + count, 0, extra * sizeof (double)) ;
26234 psf->read_current = psf->sf.frames ;
26237 psf->read_current += count / psf->sf.channels ;
26239 psf->last_op = SFM_READ ;
26241 if (psf->read_current > psf->sf.frames)
26242 { count = psf->sf.channels * (psf->read_current - psf->sf.frames) ;
26243 psf->read_current = psf->sf.frames ;
26246 return count ;
26247 } /* sf_read_double */
26249 sf_count_t
26250 sf_readf_double (SNDFILE *sndfile, double *ptr, sf_count_t frames)
26251 { SF_PRIVATE *psf ;
26252 sf_count_t count, extra ;
26254 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
26256 if (psf->mode == SFM_WRITE)
26257 { psf->error = SFE_NOT_READMODE ;
26258 return 0 ;
26261 if (frames <= 0 || psf->read_current >= psf->sf.frames)
26262 { psf_memset (ptr, 0, frames * psf->sf.channels * sizeof (double)) ;
26263 return 0 ;
26266 if (! psf->read_double || psf->seek == NULL)
26267 { psf->error = SFE_UNIMPLEMENTED ;
26268 return 0 ;
26271 if (psf->last_op != SFM_READ)
26272 if (psf->seek (psf, SFM_READ, psf->read_current) < 0)
26273 return 0 ;
26275 count = psf->read_double (psf, ptr, frames * psf->sf.channels) ;
26277 if (psf->read_current + count / psf->sf.channels > psf->sf.frames)
26278 { count = (psf->sf.frames - psf->read_current) * psf->sf.channels ;
26279 extra = frames * psf->sf.channels - count ;
26280 psf_memset (ptr + count, 0, extra * sizeof (double)) ;
26281 psf->read_current = psf->sf.frames ;
26284 psf->read_current += count / psf->sf.channels ;
26286 psf->last_op = SFM_READ ;
26288 if (psf->read_current > psf->sf.frames)
26289 { count = psf->sf.channels * (psf->read_current - psf->sf.frames) ;
26290 psf->read_current = psf->sf.frames ;
26293 return count / psf->sf.channels ;
26294 } /* sf_readf_double */
26296 /*------------------------------------------------------------------------------
26299 sf_count_t
26300 sf_write_raw (SNDFILE *sndfile, void *ptr, sf_count_t len)
26301 { SF_PRIVATE *psf ;
26302 sf_count_t count ;
26303 int bytewidth, blockwidth ;
26305 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
26307 bytewidth = (psf->bytewidth > 0) ? psf->bytewidth : 1 ;
26308 blockwidth = (psf->blockwidth > 0) ? psf->blockwidth : 1 ;
26310 if (psf->mode == SFM_READ)
26311 { psf->error = SFE_NOT_WRITEMODE ;
26312 return 0 ;
26315 if (len % (psf->sf.channels * bytewidth))
26316 { psf->error = SFE_BAD_WRITE_ALIGN ;
26317 return 0 ;
26320 if (psf->have_written == SF_FALSE && psf->write_header != NULL)
26321 psf->write_header (psf, SF_FALSE) ;
26322 psf->have_written = SF_TRUE ;
26324 count = psf_fwrite (ptr, 1, len, psf) ;
26326 psf->write_current += count / blockwidth ;
26328 if (psf->write_current > psf->sf.frames)
26329 psf->sf.frames = psf->write_current ;
26331 psf->last_op = SFM_WRITE ;
26333 return count ;
26334 } /* sf_write_raw */
26336 /*------------------------------------------------------------------------------
26339 sf_count_t
26340 sf_write_short (SNDFILE *sndfile, short *ptr, sf_count_t len)
26341 { SF_PRIVATE *psf ;
26342 sf_count_t count ;
26344 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
26346 if (psf->mode == SFM_READ)
26347 { psf->error = SFE_NOT_WRITEMODE ;
26348 return 0 ;
26351 if (len % psf->sf.channels)
26352 { psf->error = SFE_BAD_WRITE_ALIGN ;
26353 return 0 ;
26356 if (! psf->write_short || psf->seek == NULL)
26357 { psf->error = SFE_UNIMPLEMENTED ;
26358 return 0 ;
26361 if (psf->last_op != SFM_WRITE)
26362 if (psf->seek (psf, SFM_WRITE, psf->write_current) < 0)
26363 return 0 ;
26365 if (psf->have_written == SF_FALSE && psf->write_header != NULL)
26366 psf->write_header (psf, SF_FALSE) ;
26367 psf->have_written = SF_TRUE ;
26369 count = psf->write_short (psf, ptr, len) ;
26371 psf->write_current += count / psf->sf.channels ;
26373 psf->last_op = SFM_WRITE ;
26375 if (psf->auto_header)
26376 psf->write_header (psf, SF_TRUE) ;
26378 if (psf->write_current > psf->sf.frames)
26379 psf->sf.frames = psf->write_current ;
26381 return count ;
26382 } /* sf_write_short */
26384 sf_count_t
26385 sf_writef_short (SNDFILE *sndfile, short *ptr, sf_count_t frames)
26386 { SF_PRIVATE *psf ;
26387 sf_count_t count ;
26389 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
26391 if (psf->mode == SFM_READ)
26392 { psf->error = SFE_NOT_WRITEMODE ;
26393 return 0 ;
26396 if (! psf->write_short || psf->seek == NULL)
26397 { psf->error = SFE_UNIMPLEMENTED ;
26398 return 0 ;
26401 if (psf->last_op != SFM_WRITE)
26402 if (psf->seek (psf, SFM_WRITE, psf->write_current) < 0)
26403 return 0 ;
26405 if (psf->have_written == SF_FALSE && psf->write_header != NULL)
26406 psf->write_header (psf, SF_FALSE) ;
26407 psf->have_written = SF_TRUE ;
26409 count = psf->write_short (psf, ptr, frames * psf->sf.channels) ;
26411 psf->write_current += count / psf->sf.channels ;
26413 psf->last_op = SFM_WRITE ;
26415 if (psf->auto_header)
26416 psf->write_header (psf, SF_TRUE) ;
26418 if (psf->write_current > psf->sf.frames)
26419 psf->sf.frames = psf->write_current ;
26421 return count / psf->sf.channels ;
26422 } /* sf_writef_short */
26424 /*------------------------------------------------------------------------------
26427 sf_count_t
26428 sf_write_int (SNDFILE *sndfile, int *ptr, sf_count_t len)
26429 { SF_PRIVATE *psf ;
26430 sf_count_t count ;
26432 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
26434 if (psf->mode == SFM_READ)
26435 { psf->error = SFE_NOT_WRITEMODE ;
26436 return 0 ;
26439 if (len % psf->sf.channels)
26440 { psf->error = SFE_BAD_WRITE_ALIGN ;
26441 return 0 ;
26444 if (! psf->write_int || psf->seek == NULL)
26445 { psf->error = SFE_UNIMPLEMENTED ;
26446 return 0 ;
26449 if (psf->last_op != SFM_WRITE)
26450 if (psf->seek (psf, SFM_WRITE, psf->write_current) < 0)
26451 return 0 ;
26453 if (psf->have_written == SF_FALSE && psf->write_header != NULL)
26454 psf->write_header (psf, SF_FALSE) ;
26455 psf->have_written = SF_TRUE ;
26457 count = psf->write_int (psf, ptr, len) ;
26459 psf->write_current += count / psf->sf.channels ;
26461 psf->last_op = SFM_WRITE ;
26463 if (psf->auto_header)
26464 psf->write_header (psf, SF_TRUE) ;
26466 if (psf->write_current > psf->sf.frames)
26467 psf->sf.frames = psf->write_current ;
26469 return count ;
26470 } /* sf_write_int */
26472 sf_count_t
26473 sf_writef_int (SNDFILE *sndfile, int *ptr, sf_count_t frames)
26474 { SF_PRIVATE *psf ;
26475 sf_count_t count ;
26477 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
26479 if (psf->mode == SFM_READ)
26480 { psf->error = SFE_NOT_WRITEMODE ;
26481 return 0 ;
26484 if (! psf->write_int || psf->seek == NULL)
26485 { psf->error = SFE_UNIMPLEMENTED ;
26486 return 0 ;
26489 if (psf->last_op != SFM_WRITE)
26490 if (psf->seek (psf, SFM_WRITE, psf->write_current) < 0)
26491 return 0 ;
26493 if (psf->have_written == SF_FALSE && psf->write_header != NULL)
26494 psf->write_header (psf, SF_FALSE) ;
26495 psf->have_written = SF_TRUE ;
26497 count = psf->write_int (psf, ptr, frames * psf->sf.channels) ;
26499 psf->write_current += count / psf->sf.channels ;
26501 psf->last_op = SFM_WRITE ;
26503 if (psf->auto_header)
26504 psf->write_header (psf, SF_TRUE) ;
26506 if (psf->write_current > psf->sf.frames)
26507 psf->sf.frames = psf->write_current ;
26509 return count / psf->sf.channels ;
26510 } /* sf_writef_int */
26512 /*------------------------------------------------------------------------------
26515 sf_count_t
26516 sf_write_float (SNDFILE *sndfile, float *ptr, sf_count_t len)
26517 { SF_PRIVATE *psf ;
26518 sf_count_t count ;
26520 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
26522 if (psf->mode == SFM_READ)
26523 { psf->error = SFE_NOT_WRITEMODE ;
26524 return 0 ;
26527 if (len % psf->sf.channels)
26528 { psf->error = SFE_BAD_WRITE_ALIGN ;
26529 return 0 ;
26532 if (! psf->write_float || psf->seek == NULL)
26533 { psf->error = SFE_UNIMPLEMENTED ;
26534 return 0 ;
26537 if (psf->last_op != SFM_WRITE)
26538 if (psf->seek (psf, SFM_WRITE, psf->write_current) < 0)
26539 return 0 ;
26541 if (psf->have_written == SF_FALSE && psf->write_header != NULL)
26542 psf->write_header (psf, SF_FALSE) ;
26543 psf->have_written = SF_TRUE ;
26545 count = psf->write_float (psf, ptr, len) ;
26547 psf->write_current += count / psf->sf.channels ;
26549 psf->last_op = SFM_WRITE ;
26551 if (psf->auto_header)
26552 psf->write_header (psf, SF_TRUE) ;
26554 if (psf->write_current > psf->sf.frames)
26555 psf->sf.frames = psf->write_current ;
26557 return count ;
26558 } /* sf_write_float */
26560 sf_count_t
26561 sf_writef_float (SNDFILE *sndfile, float *ptr, sf_count_t frames)
26562 { SF_PRIVATE *psf ;
26563 sf_count_t count ;
26565 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
26567 if (psf->mode == SFM_READ)
26568 { psf->error = SFE_NOT_WRITEMODE ;
26569 return 0 ;
26572 if (! psf->write_float || psf->seek == NULL)
26573 { psf->error = SFE_UNIMPLEMENTED ;
26574 return 0 ;
26577 if (psf->last_op != SFM_WRITE)
26578 if (psf->seek (psf, SFM_WRITE, psf->write_current) < 0)
26579 return 0 ;
26581 if (psf->have_written == SF_FALSE && psf->write_header != NULL)
26582 psf->write_header (psf, SF_FALSE) ;
26583 psf->have_written = SF_TRUE ;
26585 count = psf->write_float (psf, ptr, frames * psf->sf.channels) ;
26587 psf->write_current += count / psf->sf.channels ;
26589 psf->last_op = SFM_WRITE ;
26591 if (psf->auto_header)
26592 psf->write_header (psf, SF_TRUE) ;
26594 if (psf->write_current > psf->sf.frames)
26595 psf->sf.frames = psf->write_current ;
26597 return count / psf->sf.channels ;
26598 } /* sf_writef_float */
26600 /*------------------------------------------------------------------------------
26603 sf_count_t
26604 sf_write_double (SNDFILE *sndfile, double *ptr, sf_count_t len)
26605 { SF_PRIVATE *psf ;
26606 sf_count_t count ;
26608 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
26610 if (psf->mode == SFM_READ)
26611 { psf->error = SFE_NOT_WRITEMODE ;
26612 return 0 ;
26615 if (len % psf->sf.channels)
26616 { psf->error = SFE_BAD_WRITE_ALIGN ;
26617 return 0 ;
26620 if (! psf->write_double || psf->seek == NULL)
26621 { psf->error = SFE_UNIMPLEMENTED ;
26622 return 0 ;
26625 if (psf->last_op != SFM_WRITE)
26626 if (psf->seek (psf, SFM_WRITE, psf->write_current) < 0)
26627 return 0 ;
26629 if (psf->have_written == SF_FALSE && psf->write_header != NULL)
26630 psf->write_header (psf, SF_FALSE) ;
26631 psf->have_written = SF_TRUE ;
26633 count = psf->write_double (psf, ptr, len) ;
26635 psf->write_current += count / psf->sf.channels ;
26637 psf->last_op = SFM_WRITE ;
26639 if (psf->auto_header)
26640 psf->write_header (psf, SF_TRUE) ;
26642 if (psf->write_current > psf->sf.frames)
26643 psf->sf.frames = psf->write_current ;
26645 return count ;
26646 } /* sf_write_double */
26648 sf_count_t
26649 sf_writef_double (SNDFILE *sndfile, double *ptr, sf_count_t frames)
26650 { SF_PRIVATE *psf ;
26651 sf_count_t count ;
26653 VALIDATE_SNDFILE_AND_ASSIGN_PSF (sndfile, psf, 1) ;
26655 if (psf->mode == SFM_READ)
26656 { psf->error = SFE_NOT_WRITEMODE ;
26657 return 0 ;
26660 if (! psf->write_double || psf->seek == NULL)
26661 { psf->error = SFE_UNIMPLEMENTED ;
26662 return 0 ;
26665 if (psf->last_op != SFM_WRITE)
26666 if (psf->seek (psf, SFM_WRITE, psf->write_current) < 0)
26667 return 0 ;
26669 if (psf->have_written == SF_FALSE && psf->write_header != NULL)
26670 psf->write_header (psf, SF_FALSE) ;
26671 psf->have_written = SF_TRUE ;
26673 count = psf->write_double (psf, ptr, frames * psf->sf.channels) ;
26675 psf->write_current += count / psf->sf.channels ;
26677 psf->last_op = SFM_WRITE ;
26679 if (psf->auto_header)
26680 psf->write_header (psf, SF_TRUE) ;
26682 if (psf->write_current > psf->sf.frames)
26683 psf->sf.frames = psf->write_current ;
26685 return count / psf->sf.channels ;
26686 } /* sf_writef_double */
26688 /*=========================================================================
26689 ** Private functions.
26692 static int
26693 format_from_extension (const char *filename)
26694 { char *cptr ;
26695 char buffer [16] ;
26697 if (filename == NULL)
26698 return 0 ;
26700 if ((cptr = strrchr (filename, '.')) == NULL)
26701 return 0 ;
26703 cptr ++ ;
26704 if (strlen (cptr) > sizeof (buffer) - 1)
26705 return 0 ;
26707 strncpy (buffer, cptr, sizeof (buffer)) ;
26708 buffer [sizeof (buffer) - 1] = 0 ;
26710 /* Convert everything in the buffer to lower case. */
26711 cptr = buffer ;
26712 while (*cptr)
26713 { *cptr = tolower (*cptr) ;
26714 cptr ++ ;
26717 cptr = buffer ;
26719 if (strcmp (cptr, "au") == 0)
26720 return SF_FORMAT_AU | SF_FORMAT_ULAW ;
26722 if (strcmp (cptr, "snd") == 0)
26723 return SF_FORMAT_AU | SF_FORMAT_ULAW ;
26725 if (strcmp (cptr, "vox") == 0)
26726 return SF_FORMAT_RAW | SF_FORMAT_VOX_ADPCM ;
26728 return 0 ;
26729 } /* format_from_extension */
26731 static int
26732 guess_file_type (SF_PRIVATE *psf, const char *filename)
26733 { int buffer [3], format ;
26734 unsigned char cptr [0x40] ;
26736 if (psf_binheader_readf (psf, "b", &buffer, SIGNED_SIZEOF (buffer)) != SIGNED_SIZEOF (buffer))
26737 { psf->error = SFE_BAD_FILE_READ ;
26738 return 0 ;
26741 if (buffer [0] == MAKE_MARKER ('R', 'I', 'F', 'F') && buffer [2] == MAKE_MARKER ('W', 'A', 'V', 'E'))
26742 return SF_FORMAT_WAV ;
26744 if (buffer [0] == MAKE_MARKER ('F', 'O', 'R', 'M'))
26745 { if (buffer [2] == MAKE_MARKER ('A', 'I', 'F', 'F') || buffer [2] == MAKE_MARKER ('A', 'I', 'F', 'C'))
26746 return SF_FORMAT_AIFF ;
26747 if (buffer [2] == MAKE_MARKER ('8', 'S', 'V', 'X') || buffer [2] == MAKE_MARKER ('1', '6', 'S', 'V'))
26748 return SF_FORMAT_SVX ;
26749 return 0 ;
26752 if (buffer [0] == MAKE_MARKER ('.', 's', 'n', 'd') || buffer [0] == MAKE_MARKER ('d', 'n', 's', '.'))
26753 return SF_FORMAT_AU ;
26755 if ((buffer [0] == MAKE_MARKER ('f', 'a', 'p', ' ') || buffer [0] == MAKE_MARKER (' ', 'p', 'a', 'f')))
26756 return SF_FORMAT_PAF ;
26758 if (buffer [0] == MAKE_MARKER ('N', 'I', 'S', 'T'))
26759 return SF_FORMAT_NIST ;
26761 if (buffer [0] == MAKE_MARKER ('C', 'r', 'e', 'a') && buffer [1] == MAKE_MARKER ('t', 'i', 'v', 'e'))
26762 return SF_FORMAT_VOC ;
26764 if ((buffer [0] & MAKE_MARKER (0xFF, 0xFF, 0xF8, 0xFF)) == MAKE_MARKER (0x64, 0xA3, 0x00, 0x00) ||
26765 (buffer [0] & MAKE_MARKER (0xFF, 0xF8, 0xFF, 0xFF)) == MAKE_MARKER (0x00, 0x00, 0xA3, 0x64))
26766 return SF_FORMAT_IRCAM ;
26768 if (buffer [0] == MAKE_MARKER ('r', 'i', 'f', 'f'))
26769 return SF_FORMAT_W64 ;
26771 if (buffer [0] == MAKE_MARKER (0, 0, 0x03, 0xE8) && buffer [1] == MAKE_MARKER (0, 0, 0, 1) &&
26772 buffer [2] == MAKE_MARKER (0, 0, 0, 1))
26773 return SF_FORMAT_MAT4 ;
26775 if (buffer [0] == MAKE_MARKER (0, 0, 0, 0) && buffer [1] == MAKE_MARKER (1, 0, 0, 0) &&
26776 buffer [2] == MAKE_MARKER (1, 0, 0, 0))
26777 return SF_FORMAT_MAT4 ;
26779 if (buffer [0] == MAKE_MARKER ('M', 'A', 'T', 'L') && buffer [1] == MAKE_MARKER ('A', 'B', ' ', '5'))
26780 return SF_FORMAT_MAT5 ;
26782 if (buffer [0] == MAKE_MARKER ('P', 'V', 'F', '1'))
26783 return SF_FORMAT_PVF ;
26785 if (buffer [0] == MAKE_MARKER ('E', 'x', 't', 'e') && buffer [1] == MAKE_MARKER ('n', 'd', 'e', 'd') &&
26786 buffer [2] == MAKE_MARKER (' ', 'I', 'n', 's'))
26787 return SF_FORMAT_XI ;
26789 if (ENABLE_EXPERIMENTAL_CODE && buffer [0] == MAKE_MARKER ('O', 'g', 'g', 'S'))
26790 return SF_FORMAT_OGG ;
26792 if (buffer [0] == MAKE_MARKER ('A', 'L', 'a', 'w') && buffer [1] == MAKE_MARKER ('S', 'o', 'u', 'n')
26793 && buffer [2] == MAKE_MARKER ('d', 'F', 'i', 'l'))
26794 return SF_FORMAT_WVE ;
26796 if (buffer [0] == MAKE_MARKER ('D', 'i', 'a', 'm') && buffer [1] == MAKE_MARKER ('o', 'n', 'd', 'W')
26797 && buffer [2] == MAKE_MARKER ('a', 'r', 'e', ' '))
26798 return SF_FORMAT_DWD ;
26800 if (buffer [0] == MAKE_MARKER ('L', 'M', '8', '9') || buffer [0] == MAKE_MARKER ('5', '3', 0, 0))
26801 return SF_FORMAT_TXW ;
26803 if ((buffer [0] & MAKE_MARKER (0xFF, 0xFF, 0x80, 0xFF)) == MAKE_MARKER (0xF0, 0x7E, 0, 0x01))
26804 return SF_FORMAT_SDS ;
26806 if (buffer [0] == MAKE_MARKER ('C', 'A', 'T', ' ') && buffer [2] == MAKE_MARKER ('R', 'E', 'X', '2'))
26807 return SF_FORMAT_REX2 ;
26809 if (buffer [0] == MAKE_MARKER (0x30, 0x26, 0xB2, 0x75) && buffer [1] == MAKE_MARKER (0x8E, 0x66, 0xCF, 0x11))
26810 return 0 /*-SF_FORMAT_WMA-*/ ;
26812 /* HMM (Hidden Markov Model) Tool Kit. */
26813 if (2 * BEI2H_INT (buffer [0]) + 12 == psf->filelength && buffer [2] == MAKE_MARKER (0, 2, 0, 0))
26814 return SF_FORMAT_HTK ;
26816 if (buffer [0] == MAKE_MARKER ('f', 'L', 'a', 'C'))
26817 return 0 /*-SF_FORMAT_FLAC-*/ ;
26819 /* Turtle Beach SMP 16-bit */
26820 if (buffer [0] == MAKE_MARKER ('S', 'O', 'U', 'N') && buffer [1] == MAKE_MARKER ('D', ' ', 'S', 'A'))
26821 return 0 ;
26823 if (buffer [0] == MAKE_MARKER ('S', 'Y', '8', '0') || buffer [0] == MAKE_MARKER ('S', 'Y', '8', '5'))
26824 return 0 ;
26826 if (buffer [0] == MAKE_MARKER ('a', 'j', 'k', 'g'))
26827 return 0 /*-SF_FORMAT_SHN-*/ ;
26829 if (buffer [0] == MAKE_MARKER ('2', 'B', 'I', 'T'))
26830 return SF_FORMAT_AVR ;
26832 if (OS_IS_MACOSX && (format = macos_guess_file_type (psf, filename)) != 0)
26833 return format ;
26835 /* Detect wacky MacOS header stuff. This might be "Sound Designer II". */
26836 memcpy (cptr , buffer, sizeof (buffer)) ;
26837 if (cptr [0] == 0 && cptr [1] > 0 && psf->sf.seekable)
26838 { psf_binheader_readf (psf, "pb", 0, &cptr, SIGNED_SIZEOF (cptr)) ;
26840 if (cptr [1] < (sizeof (cptr) - 3) && cptr [cptr [1] + 2] == 0 && strlen (((char*) cptr) + 2) == cptr [1])
26841 { psf_log_printf (psf, "Weird MacOS Header.\n") ;
26842 psf_binheader_readf (psf, "em", &buffer) ;
26843 if (buffer [0] == MAKE_MARKER (0, 'S', 'd', '2'))
26844 return SF_FORMAT_SD2 ;
26848 /* This must be the last one. */
26849 if ((format = format_from_extension (filename)) != 0)
26850 return format ;
26852 /* Default to header-less RAW PCM file type. */
26853 return SF_FORMAT_RAW ;
26854 } /* guess_file_type */
26857 static int
26858 validate_sfinfo (SF_INFO *sfinfo)
26859 { if (sfinfo->samplerate < 1)
26860 return 0 ;
26861 if (sfinfo->frames < 0)
26862 return 0 ;
26863 if (sfinfo->channels < 1)
26864 return 0 ;
26865 if ((sfinfo->format & SF_FORMAT_TYPEMASK) == 0)
26866 return 0 ;
26867 if ((sfinfo->format & SF_FORMAT_SUBMASK) == 0)
26868 return 0 ;
26869 if (sfinfo->sections < 1)
26870 return 0 ;
26871 return 1 ;
26872 } /* validate_sfinfo */
26874 static int
26875 validate_psf (SF_PRIVATE *psf)
26877 if (psf->datalength < 0)
26878 { psf_log_printf (psf, "Invalid SF_PRIVATE field : datalength == %D.\n", psf->datalength) ;
26879 return 0 ;
26881 if (psf->dataoffset < 0)
26882 { psf_log_printf (psf, "Invalid SF_PRIVATE field : dataoffset == %D.\n", psf->dataoffset) ;
26883 return 0 ;
26885 if (psf->blockwidth && psf->blockwidth != psf->sf.channels * psf->bytewidth)
26886 { psf_log_printf (psf, "Invalid SF_PRIVATE field : channels * bytewidth == %d.\n",
26887 psf->sf.channels * psf->bytewidth) ;
26888 return 0 ;
26890 return 1 ;
26891 } /* validate_psf */
26893 static void
26894 save_header_info (SF_PRIVATE *psf)
26895 { LSF_SNPRINTF (sf_logbuffer, sizeof (sf_logbuffer), "%s", psf->logbuffer) ;
26896 } /* save_header_info */
26898 static void
26899 copy_filename (SF_PRIVATE *psf, const char *path)
26900 { const char *cptr ;
26902 if ((cptr = strrchr (path, '/')) || (cptr = strrchr (path, '\\')))
26903 cptr ++ ;
26904 else
26905 cptr = path ;
26907 memset (psf->filename, 0, SF_FILENAME_LEN) ;
26909 LSF_SNPRINTF (psf->filename, sizeof (psf->filename), "%s", cptr) ;
26911 } /* copy_filename */
26913 /*==============================================================================
26916 static int
26917 psf_close (SF_PRIVATE *psf)
26918 { int error ;
26920 if (psf->close)
26921 error = psf->close (psf) ;
26923 psf_fclose (psf) ;
26925 if (psf->fdata)
26926 free (psf->fdata) ;
26928 if (psf->interleave)
26929 free (psf->interleave) ;
26931 if (psf->dither)
26932 free (psf->dither) ;
26934 if (psf->pchunk)
26935 free (psf->pchunk) ;
26937 if (psf->format_desc)
26938 { memset (psf->format_desc, 0, strlen (psf->format_desc)) ;
26939 free (psf->format_desc) ;
26942 memset (psf, 0, sizeof (SF_PRIVATE)) ;
26943 free (psf) ;
26945 return 0 ;
26946 } /* psf_close */
26948 static int
26949 psf_open_file (SF_PRIVATE *psf, int mode, SF_INFO *sfinfo)
26950 { int error, format ;
26952 if (mode != SFM_READ && mode != SFM_WRITE && mode != SFM_RDWR)
26953 return SFE_BAD_OPEN_MODE ;
26955 if (sfinfo == NULL)
26956 return SFE_BAD_SF_INFO_PTR ;
26958 if (mode == SFM_READ)
26959 { if ((sfinfo->format & SF_FORMAT_TYPEMASK) == SF_FORMAT_RAW)
26960 { if (sf_format_check (sfinfo) == 0)
26961 return SFE_RAW_BAD_FORMAT ;
26963 else
26964 memset (sfinfo, 0, sizeof (SF_INFO)) ;
26967 sf_errno = error = 0 ;
26968 sf_logbuffer [0] = 0 ;
26970 memcpy (&(psf->sf), sfinfo, sizeof (SF_INFO)) ;
26972 psf->Magick = SNDFILE_MAGICK ;
26973 psf->norm_float = SF_TRUE ;
26974 psf->norm_double = SF_TRUE ;
26975 psf->mode = mode ;
26976 psf->dataoffset = -1 ;
26977 psf->datalength = -1 ;
26978 psf->read_current = -1 ;
26979 psf->write_current = -1 ;
26980 psf->auto_header = SF_FALSE ;
26981 psf->rwf_endian = SF_ENDIAN_LITTLE ;
26982 psf->seek = psf_default_seek ;
26984 psf->sf.sections = 1 ;
26986 psf->is_pipe = psf_is_pipe (psf) ;
26988 if (psf->is_pipe)
26989 { psf->sf.seekable = SF_FALSE ;
26990 psf->filelength = SF_COUNT_MAX ;
26992 else
26993 { psf->sf.seekable = SF_TRUE ;
26995 /* File is open, so get the length. */
26996 psf->filelength = psf_get_filelen (psf) ;
26999 if (psf->fileoffset > 0)
27000 { switch (psf->mode)
27001 { case SFM_READ :
27002 if (psf->filelength < 44)
27003 { psf_log_printf (psf, "Short filelength: %D (fileoffset: %D)\n", psf->filelength, psf->fileoffset) ;
27004 return SFE_BAD_OFFSET ;
27006 break ;
27008 case SFM_WRITE :
27009 psf->fileoffset = 0 ;
27010 psf_fseek (psf, 0, SEEK_END) ;
27011 psf->fileoffset = psf_ftell (psf) ;
27012 break ;
27014 case SFM_RDWR :
27015 return SFE_NO_EMBEDDED_RDWR ;
27018 psf_log_printf (psf, "Embedded file offset : %D\n", psf->fileoffset) ;
27021 if (psf->filelength == SF_COUNT_MAX)
27022 psf_log_printf (psf, "Length : unknown\n") ;
27023 else
27024 psf_log_printf (psf, "Length : %D\n", psf->filelength) ;
27026 if (mode == SFM_WRITE || (mode == SFM_RDWR && psf->filelength == 0))
27027 { /* If the file is being opened for write or RDWR and the file is currently
27028 ** empty, then the SF_INFO struct must contain valid data.
27030 if (sf_format_check (&(psf->sf)) == 0)
27031 return SFE_BAD_OPEN_FORMAT ;
27033 else if ((psf->sf.format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW)
27034 { /* If type RAW has not been specified then need to figure out file type. */
27035 psf->sf.format = guess_file_type (psf, psf->filename) ;
27038 /* Prevent unnecessary seeks */
27039 psf->last_op = psf->mode ;
27041 /* Set bytewidth if known. */
27042 switch (psf->sf.format & SF_FORMAT_SUBMASK)
27043 { case SF_FORMAT_PCM_S8 :
27044 case SF_FORMAT_PCM_U8 :
27045 case SF_FORMAT_ULAW :
27046 case SF_FORMAT_ALAW :
27047 case SF_FORMAT_DPCM_8 :
27048 psf->bytewidth = 1 ;
27049 break ;
27051 case SF_FORMAT_PCM_16 :
27052 case SF_FORMAT_DPCM_16 :
27053 psf->bytewidth = 2 ;
27054 break ;
27056 case SF_FORMAT_PCM_24 :
27057 psf->bytewidth = 3 ;
27058 break ;
27060 case SF_FORMAT_PCM_32 :
27061 case SF_FORMAT_FLOAT :
27062 psf->bytewidth = 4 ;
27063 break ;
27065 case SF_FORMAT_DOUBLE :
27066 psf->bytewidth = 8 ;
27067 break ;
27070 /* Call the initialisation function for the relevant file type. */
27071 switch (psf->sf.format & SF_FORMAT_TYPEMASK)
27072 { case SF_FORMAT_WAV :
27073 case SF_FORMAT_WAVEX :
27074 error = wav_open (psf) ;
27075 break ;
27077 case SF_FORMAT_AIFF :
27078 error = aiff_open (psf) ;
27079 break ;
27081 case SF_FORMAT_AU :
27082 error = au_open (psf) ;
27083 break ;
27085 case SF_FORMAT_AU | SF_FORMAT_ULAW :
27086 error = au_nh_open (psf) ;
27087 break ;
27089 case SF_FORMAT_RAW :
27090 error = raw_open (psf) ;
27091 break ;
27093 case SF_FORMAT_W64 :
27094 error = w64_open (psf) ;
27095 break ;
27097 /* Lite remove start */
27098 case SF_FORMAT_PAF :
27099 error = paf_open (psf) ;
27100 break ;
27102 case SF_FORMAT_SVX :
27103 error = svx_open (psf) ;
27104 break ;
27106 case SF_FORMAT_NIST :
27107 error = nist_open (psf) ;
27108 break ;
27110 case SF_FORMAT_IRCAM :
27111 error = ircam_open (psf) ;
27112 break ;
27114 case SF_FORMAT_VOC :
27115 error = voc_open (psf) ;
27116 break ;
27118 case SF_FORMAT_SDS :
27119 error = sds_open (psf) ;
27120 break ;
27122 case SF_FORMAT_OGG :
27123 error = ogg_open (psf) ;
27124 break ;
27126 case SF_FORMAT_TXW :
27127 error = txw_open (psf) ;
27128 break ;
27130 case SF_FORMAT_WVE :
27131 error = wve_open (psf) ;
27132 break ;
27134 case SF_FORMAT_DWD :
27135 error = dwd_open (psf) ;
27136 break ;
27138 case SF_FORMAT_MAT4 :
27139 error = mat4_open (psf) ;
27140 break ;
27142 case SF_FORMAT_MAT5 :
27143 error = mat5_open (psf) ;
27144 break ;
27146 case SF_FORMAT_PVF :
27147 error = pvf_open (psf) ;
27148 break ;
27150 case SF_FORMAT_XI :
27151 error = xi_open (psf) ;
27152 break ;
27154 case SF_FORMAT_HTK :
27155 error = htk_open (psf) ;
27156 break ;
27158 case SF_FORMAT_SD2 :
27159 error = sd2_open (psf) ;
27160 break ;
27162 case SF_FORMAT_REX2 :
27163 error = rx2_open (psf) ;
27164 break ;
27166 case SF_FORMAT_AVR :
27167 error = avr_open (psf) ;
27168 break ;
27170 /* Lite remove end */
27172 default :
27173 error = SFE_UNKNOWN_FORMAT ;
27176 if (error)
27177 return error ;
27179 /* For now, check whether embedding is supported. */
27180 format = psf->sf.format & SF_FORMAT_TYPEMASK ;
27181 if (psf->fileoffset > 0 &&
27182 (format != SF_FORMAT_WAV) && (format != SF_FORMAT_WAVEX) &&
27183 (format != SF_FORMAT_AIFF) && (format != SF_FORMAT_AU)
27185 return SFE_NO_EMBED_SUPPORT ;
27187 if (psf->fileoffset > 0)
27188 psf_log_printf (psf, "Embedded file length : %D\n", psf->filelength) ;
27190 if (mode == SFM_RDWR && sf_format_check (&(psf->sf)) == 0)
27191 return SFE_BAD_RDWR_FORMAT ;
27193 if (validate_sfinfo (&(psf->sf)) == 0)
27194 { psf_log_SF_INFO (psf) ;
27195 save_header_info (psf) ;
27196 return SFE_BAD_SF_INFO ;
27199 if (validate_psf (psf) == 0)
27200 { save_header_info (psf) ;
27201 return SFE_INTERNAL ;
27204 psf->read_current = 0 ;
27205 psf->write_current = (psf->mode == SFM_RDWR) ? psf->sf.frames : 0 ;
27207 memcpy (sfinfo, &(psf->sf), sizeof (SF_INFO)) ;
27209 return 0 ;
27210 } /* psf_open_file */
27213 ** Do not edit or modify anything in this comment block.
27214 ** The arch-tag line is a file identity tag for the GNU Arch
27215 ** revision control system.
27217 ** arch-tag: cd4f9e91-a8ec-4154-9bf6-fe4b8c69a615
27220 ** Copyright (C) 2001-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
27222 ** This program is free software; you can redistribute it and/or modify
27223 ** it under the terms of the GNU Lesser General Public License as published by
27224 ** the Free Software Foundation; either version 2.1 of the License, or
27225 ** (at your option) any later version.
27227 ** This program is distributed in the hope that it will be useful,
27228 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
27229 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27230 ** GNU Lesser General Public License for more details.
27232 ** You should have received a copy of the GNU Lesser General Public License
27233 ** along with this program; if not, write to the Free Software
27234 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27237 #include <stdio.h>
27238 #include <string.h>
27239 #include <math.h>
27242 #define STRINGS_DEBUG 0
27243 #if STRINGS_DEBUG
27244 static void hexdump (void *data, int len) ;
27245 #endif
27248 psf_store_string (SF_PRIVATE *psf, int str_type, const char *str)
27249 { static char lsf_name [] = PACKAGE "-" VERSION ;
27250 static char bracket_name [] = " (" PACKAGE "-" VERSION ")" ;
27251 int k, str_len, len_remaining, str_flags ;
27253 if (str == NULL)
27254 return SFE_STR_BAD_STRING ;
27256 str_len = strlen (str) ;
27258 /* A few extra checks for write mode. */
27259 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
27260 { if ((psf->str_flags & SF_STR_ALLOW_START) == 0)
27261 return SFE_STR_NO_SUPPORT ;
27262 if ((psf->str_flags & SF_STR_ALLOW_END) == 0)
27263 return SFE_STR_NO_SUPPORT ;
27264 /* Only allow zero length strings for software. */
27265 if (str_type != SF_STR_SOFTWARE && str_len == 0)
27266 return SFE_STR_BAD_STRING ;
27269 /* Determine flags */
27270 str_flags = SF_STR_LOCATE_START ;
27271 if (psf->have_written)
27272 { if ((psf->str_flags & SF_STR_ALLOW_END) == 0)
27273 return SFE_STR_NO_ADD_END ;
27274 str_flags = SF_STR_LOCATE_END ;
27277 /* Find next free slot in table. */
27278 for (k = 0 ; k < SF_MAX_STRINGS ; k++)
27279 if (psf->strings [k].type == 0)
27280 break ;
27282 /* More sanity checking. */
27283 if (k >= SF_MAX_STRINGS)
27284 return SFE_STR_MAX_COUNT ;
27286 if (k == 0 && psf->str_end != NULL)
27287 { psf_log_printf (psf, "SFE_STR_WEIRD : k == 0 && psf->str_end != NULL\n") ;
27288 return SFE_STR_WEIRD ;
27291 if (k != 0 && psf->str_end == NULL)
27292 { psf_log_printf (psf, "SFE_STR_WEIRD : k != 0 && psf->str_end == NULL\n") ;
27293 return SFE_STR_WEIRD ;
27296 /* Special case for the first string. */
27297 if (k == 0)
27298 psf->str_end = psf->str_storage ;
27301 #if STRINGS_DEBUG
27302 psf_log_printf (psf, "str_storage : %X\n", (int) psf->str_storage) ;
27303 psf_log_printf (psf, "str_end : %X\n", (int) psf->str_end) ;
27304 psf_log_printf (psf, "sizeof (storage) : %d\n", SIGNED_SIZEOF (psf->str_storage)) ;
27305 #endif
27307 len_remaining = SIGNED_SIZEOF (psf->str_storage) - (psf->str_end - psf->str_storage) ;
27309 if (len_remaining < str_len + 2)
27310 return SFE_STR_MAX_DATA ;
27312 switch (str_type)
27313 { case SF_STR_SOFTWARE :
27314 /* In write mode, want to append libsndfile-version to string. */
27315 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
27316 { psf->strings [k].type = str_type ;
27317 psf->strings [k].str = psf->str_end ;
27318 psf->strings [k].flags = str_flags ;
27320 memcpy (psf->str_end, str, str_len + 1) ;
27321 psf->str_end += str_len ;
27324 ** If the supplied string does not already contain a
27325 ** libsndfile-X.Y.Z component, then add it.
27327 if (strstr (str, PACKAGE) == NULL && len_remaining > (int) (strlen (bracket_name) + str_len + 2))
27328 { if (strlen (str) == 0)
27329 strncat (psf->str_end, lsf_name, len_remaining) ;
27330 else
27331 strncat (psf->str_end, bracket_name, len_remaining) ;
27332 psf->str_end += strlen (psf->str_end) ;
27335 /* Plus one to catch string terminator. */
27336 psf->str_end += 1 ;
27337 break ;
27340 /* Fall though if not write mode. */
27342 case SF_STR_TITLE :
27343 case SF_STR_COPYRIGHT :
27344 case SF_STR_ARTIST :
27345 case SF_STR_COMMENT :
27346 case SF_STR_DATE :
27347 psf->strings [k].type = str_type ;
27348 psf->strings [k].str = psf->str_end ;
27349 psf->strings [k].flags = str_flags ;
27351 /* Plus one to catch string terminator. */
27352 memcpy (psf->str_end, str, str_len + 1) ;
27353 psf->str_end += str_len + 1 ;
27354 break ;
27356 default :
27357 return SFE_STR_BAD_TYPE ;
27360 psf->str_flags |= (psf->have_written) ? SF_STR_LOCATE_END : SF_STR_LOCATE_START ;
27362 #if STRINGS_DEBUG
27363 hexdump (psf->str_storage, 300) ;
27364 #endif
27366 return 0 ;
27367 } /* psf_store_string */
27369 const char*
27370 psf_get_string (SF_PRIVATE *psf, int str_type)
27371 { int k ;
27373 for (k = 0 ; k < SF_MAX_STRINGS ; k++)
27374 if (str_type == psf->strings [k].type)
27375 return psf->strings [k].str ;
27377 return NULL ;
27378 } /* psf_get_string */
27380 #if STRINGS_DEBUG
27382 #include <ctype.h>
27383 static void
27384 hexdump (void *data, int len)
27385 { unsigned char *ptr ;
27386 int k ;
27388 ptr = data ;
27390 puts ("---------------------------------------------------------") ;
27391 while (len >= 16)
27392 { for (k = 0 ; k < 16 ; k++)
27393 printf ("%02X ", ptr [k] & 0xFF) ;
27394 printf (" ") ;
27395 for (k = 0 ; k < 16 ; k++)
27396 printf ("%c", isprint (ptr [k]) ? ptr [k] : '.') ;
27397 puts ("") ;
27398 ptr += 16 ;
27399 len -= 16 ;
27401 } /* hexdump */
27403 #endif
27405 ** Do not edit or modify anything in this comment block.
27406 ** The arch-tag line is a file identity tag for the GNU Arch
27407 ** revision control system.
27409 ** arch-tag: 04393aa1-9389-46fe-baf2-58a7bd544fd6
27412 ** Copyright (C) 1999-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
27414 ** This program is free software; you can redistribute it and/or modify
27415 ** it under the terms of the GNU Lesser General Public License as published by
27416 ** the Free Software Foundation; either version 2.1 of the License, or
27417 ** (at your option) any later version.
27419 ** This program is distributed in the hope that it will be useful,
27420 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
27421 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27422 ** GNU Lesser General Public License for more details.
27424 ** You should have received a copy of the GNU Lesser General Public License
27425 ** along with this program; if not, write to the Free Software
27426 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27430 #include <stdio.h>
27431 #include <string.h>
27432 #include <ctype.h>
27433 #include <stdarg.h>
27437 /*------------------------------------------------------------------------------
27438 * Macros to handle big/little endian issues.
27441 #define FORM_MARKER (MAKE_MARKER ('F', 'O', 'R', 'M'))
27442 #define SVX8_MARKER (MAKE_MARKER ('8', 'S', 'V', 'X'))
27443 #define SV16_MARKER (MAKE_MARKER ('1', '6', 'S', 'V'))
27444 #define VHDR_MARKER (MAKE_MARKER ('V', 'H', 'D', 'R'))
27445 #define BODY_MARKER (MAKE_MARKER ('B', 'O', 'D', 'Y'))
27447 #define ATAK_MARKER (MAKE_MARKER ('A', 'T', 'A', 'K'))
27448 #define RLSE_MARKER (MAKE_MARKER ('R', 'L', 'S', 'E'))
27450 #define c_MARKER (MAKE_MARKER ('(', 'c', ')', ' '))
27451 #define NAME_MARKER (MAKE_MARKER ('N', 'A', 'M', 'E'))
27452 #define AUTH_MARKER (MAKE_MARKER ('A', 'U', 'T', 'H'))
27453 #define ANNO_MARKER (MAKE_MARKER ('A', 'N', 'N', 'O'))
27454 #define CHAN_MARKER (MAKE_MARKER ('C', 'H', 'A', 'N'))
27456 /*------------------------------------------------------------------------------
27457 * Typedefs for file chunks.
27460 typedef struct
27461 { unsigned int oneShotHiSamples, repeatHiSamples, samplesPerHiCycle ;
27462 unsigned short samplesPerSec ;
27463 unsigned char octave, compression ;
27464 unsigned int volume ;
27465 } VHDR_CHUNK ;
27467 enum {
27468 svxHAVE_FORM = 0x01,
27470 HAVE_SVX = 0x02,
27471 HAVE_VHDR = 0x04,
27472 HAVE_BODY = 0x08
27475 /*------------------------------------------------------------------------------
27476 * Private static functions.
27479 static int svx_close (SF_PRIVATE *psf) ;
27480 static int svx_write_header (SF_PRIVATE *psf, int calc_length) ;
27481 static int svx_read_header (SF_PRIVATE *psf) ;
27483 /*------------------------------------------------------------------------------
27484 ** Public function.
27488 svx_open (SF_PRIVATE *psf)
27489 { int error, subformat ;
27491 if (psf->mode == SFM_READ || (psf->mode == SFM_RDWR && psf->filelength > 0))
27492 { if ((error = svx_read_header (psf)))
27493 return error ;
27495 psf->endian = SF_ENDIAN_BIG ; /* All SVX files are big endian. */
27497 psf->blockwidth = psf->sf.channels * psf->bytewidth ;
27498 if (psf->blockwidth)
27499 psf->sf.frames = psf->datalength / psf->blockwidth ;
27501 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
27504 subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
27506 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
27507 { if (psf->is_pipe)
27508 return SFE_NO_PIPE_WRITE ;
27510 if ((psf->sf.format & SF_FORMAT_TYPEMASK) != SF_FORMAT_SVX)
27511 return SFE_BAD_OPEN_FORMAT ;
27513 psf->endian = psf->sf.format & SF_FORMAT_ENDMASK ;
27515 if (psf->endian == SF_ENDIAN_LITTLE || (CPU_IS_LITTLE_ENDIAN && psf->endian == SF_ENDIAN_CPU))
27516 return SFE_BAD_ENDIAN ;
27518 psf->endian = SF_ENDIAN_BIG ; /* All SVX files are big endian. */
27520 error = svx_write_header (psf, SF_FALSE) ;
27521 if (error)
27522 return error ;
27524 psf->write_header = svx_write_header ;
27527 psf->close = svx_close ;
27529 if ((error = pcm_init (psf)))
27530 return error ;
27532 return 0 ;
27533 } /* svx_open */
27535 /*------------------------------------------------------------------------------
27538 static int
27539 svx_read_header (SF_PRIVATE *psf)
27540 { VHDR_CHUNK vhdr ;
27541 unsigned int FORMsize, vhdrsize, dword, marker ;
27542 int filetype = 0, parsestage = 0, done = 0 ;
27543 int bytecount = 0, channels ;
27545 psf_binheader_readf (psf, "p", 0) ;
27547 /* Set default number of channels. */
27548 psf->sf.channels = 1 ;
27550 psf->sf.format = SF_FORMAT_SVX ;
27552 while (! done)
27553 { psf_binheader_readf (psf, "m", &marker) ;
27554 switch (marker)
27555 { case FORM_MARKER :
27556 if (parsestage)
27557 return SFE_SVX_NO_FORM ;
27559 psf_binheader_readf (psf, "E4", &FORMsize) ;
27561 if (FORMsize != psf->filelength - 2 * sizeof (dword))
27562 { dword = psf->filelength - 2 * sizeof (dword) ;
27563 psf_log_printf (psf, "FORM : %d (should be %d)\n", FORMsize, dword) ;
27564 FORMsize = dword ;
27566 else
27567 psf_log_printf (psf, "FORM : %d\n", FORMsize) ;
27568 parsestage |= svxHAVE_FORM ;
27569 break ;
27571 case SVX8_MARKER :
27572 case SV16_MARKER :
27573 if (! (parsestage & svxHAVE_FORM))
27574 return SFE_SVX_NO_FORM ;
27575 filetype = marker ;
27576 psf_log_printf (psf, " %M\n", marker) ;
27577 parsestage |= HAVE_SVX ;
27578 break ;
27580 case VHDR_MARKER :
27581 if (! (parsestage & (svxHAVE_FORM | HAVE_SVX)))
27582 return SFE_SVX_NO_FORM ;
27584 psf_binheader_readf (psf, "E4", &vhdrsize) ;
27586 psf_log_printf (psf, " VHDR : %d\n", vhdrsize) ;
27588 psf_binheader_readf (psf, "E4442114", &(vhdr.oneShotHiSamples), &(vhdr.repeatHiSamples),
27589 &(vhdr.samplesPerHiCycle), &(vhdr.samplesPerSec), &(vhdr.octave), &(vhdr.compression),
27590 &(vhdr.volume)) ;
27592 psf_log_printf (psf, " OneShotHiSamples : %d\n", vhdr.oneShotHiSamples) ;
27593 psf_log_printf (psf, " RepeatHiSamples : %d\n", vhdr.repeatHiSamples) ;
27594 psf_log_printf (psf, " samplesPerHiCycle : %d\n", vhdr.samplesPerHiCycle) ;
27595 psf_log_printf (psf, " Sample Rate : %d\n", vhdr.samplesPerSec) ;
27596 psf_log_printf (psf, " Octave : %d\n", vhdr.octave) ;
27598 psf_log_printf (psf, " Compression : %d => ", vhdr.compression) ;
27600 switch (vhdr.compression)
27601 { case 0 : psf_log_printf (psf, "None.\n") ;
27602 break ;
27603 case 1 : psf_log_printf (psf, "Fibonacci delta\n") ;
27604 break ;
27605 case 2 : psf_log_printf (psf, "Exponential delta\n") ;
27606 break ;
27609 psf_log_printf (psf, " Volume : %d\n", vhdr.volume) ;
27611 psf->sf.samplerate = vhdr.samplesPerSec ;
27613 if (filetype == SVX8_MARKER)
27614 { psf->sf.format |= SF_FORMAT_PCM_S8 ;
27615 psf->bytewidth = 1 ;
27617 else if (filetype == SV16_MARKER)
27618 { psf->sf.format |= SF_FORMAT_PCM_16 ;
27619 psf->bytewidth = 2 ;
27622 parsestage |= HAVE_VHDR ;
27623 break ;
27625 case BODY_MARKER :
27626 if (! (parsestage & HAVE_VHDR))
27627 return SFE_SVX_NO_BODY ;
27629 psf_binheader_readf (psf, "E4", &dword) ;
27630 psf->datalength = dword ;
27632 psf->dataoffset = psf_ftell (psf) ;
27634 if (psf->datalength > psf->filelength - psf->dataoffset)
27635 { psf_log_printf (psf, " BODY : %D (should be %D)\n", psf->datalength, psf->filelength - psf->dataoffset) ;
27636 psf->datalength = psf->filelength - psf->dataoffset ;
27638 else
27639 psf_log_printf (psf, " BODY : %D\n", psf->datalength) ;
27641 parsestage |= HAVE_BODY ;
27643 if (! psf->sf.seekable)
27644 break ;
27646 psf_fseek (psf, psf->datalength, SEEK_CUR) ;
27647 break ;
27649 case NAME_MARKER :
27650 if (! (parsestage & HAVE_SVX))
27651 return SFE_SVX_NO_FORM ;
27653 psf_binheader_readf (psf, "E4", &dword) ;
27655 psf_log_printf (psf, " %M : %d\n", marker, dword) ;
27657 if (strlen (psf->filename) != dword)
27658 { if (dword > sizeof (psf->filename) - 1)
27659 return SFE_SVX_BAD_NAME_LENGTH ;
27661 psf_binheader_readf (psf, "b", psf->filename, dword) ;
27662 psf->filename [dword] = 0 ;
27664 else
27665 psf_binheader_readf (psf, "j", dword) ;
27666 break ;
27668 case ANNO_MARKER :
27669 if (! (parsestage & HAVE_SVX))
27670 return SFE_SVX_NO_FORM ;
27672 psf_binheader_readf (psf, "E4", &dword) ;
27674 psf_log_printf (psf, " %M : %d\n", marker, dword) ;
27676 psf_binheader_readf (psf, "j", dword) ;
27677 break ;
27679 case CHAN_MARKER :
27680 if (! (parsestage & HAVE_SVX))
27681 return SFE_SVX_NO_FORM ;
27683 psf_binheader_readf (psf, "E4", &dword) ;
27685 psf_log_printf (psf, " %M : %d\n", marker, dword) ;
27687 bytecount += psf_binheader_readf (psf, "E4", &channels) ;
27689 psf_log_printf (psf, " Channels : %d => %d\n", channels) ;
27691 psf_binheader_readf (psf, "j", dword - bytecount) ;
27692 break ;
27695 case AUTH_MARKER :
27696 case c_MARKER :
27697 if (! (parsestage & HAVE_SVX))
27698 return SFE_SVX_NO_FORM ;
27700 psf_binheader_readf (psf, "E4", &dword) ;
27702 psf_log_printf (psf, " %M : %d\n", marker, dword) ;
27704 psf_binheader_readf (psf, "j", dword) ;
27705 break ;
27707 default :
27708 if (isprint ((marker >> 24) & 0xFF) && isprint ((marker >> 16) & 0xFF)
27709 && isprint ((marker >> 8) & 0xFF) && isprint (marker & 0xFF))
27710 { psf_binheader_readf (psf, "E4", &dword) ;
27712 psf_log_printf (psf, "%M : %d (unknown marker)\n", marker, dword) ;
27714 psf_binheader_readf (psf, "j", dword) ;
27715 break ;
27717 if ((dword = psf_ftell (psf)) & 0x03)
27718 { psf_log_printf (psf, " Unknown chunk marker at position %d. Resynching.\n", dword - 4) ;
27720 psf_binheader_readf (psf, "j", -3) ;
27721 break ;
27723 psf_log_printf (psf, "*** Unknown chunk marker : %X. Exiting parser.\n", marker) ;
27724 done = 1 ;
27725 } ; /* switch (marker) */
27727 if (! psf->sf.seekable && (parsestage & HAVE_BODY))
27728 break ;
27730 if (psf_ftell (psf) >= psf->filelength - SIGNED_SIZEOF (dword))
27731 break ;
27732 } ; /* while (1) */
27734 if (vhdr.compression)
27735 return SFE_SVX_BAD_COMP ;
27737 if (psf->dataoffset <= 0)
27738 return SFE_SVX_NO_DATA ;
27740 return 0 ;
27741 } /* svx_read_header */
27743 static int
27744 svx_close (SF_PRIVATE *psf)
27746 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
27747 svx_write_header (psf, SF_TRUE) ;
27749 return 0 ;
27750 } /* svx_close */
27752 static int
27753 svx_write_header (SF_PRIVATE *psf, int calc_length)
27754 { static char annotation [] = "libsndfile by Erik de Castro Lopo\0\0\0" ;
27755 sf_count_t current ;
27757 current = psf_ftell (psf) ;
27759 if (calc_length)
27760 { psf->filelength = psf_get_filelen (psf) ;
27762 psf->datalength = psf->filelength - psf->dataoffset ;
27764 if (psf->dataend)
27765 psf->datalength -= psf->filelength - psf->dataend ;
27767 psf->sf.frames = psf->datalength / (psf->bytewidth * psf->sf.channels) ;
27770 psf->header [0] = 0 ;
27771 psf->headindex = 0 ;
27772 psf_fseek (psf, 0, SEEK_SET) ;
27774 /* FORM marker and FORM size. */
27775 psf_binheader_writef (psf, "Etm8", FORM_MARKER, (psf->filelength < 8) ?
27776 psf->filelength * 0 : psf->filelength - 8) ;
27778 psf_binheader_writef (psf, "m", (psf->bytewidth == 1) ? SVX8_MARKER : SV16_MARKER) ;
27780 /* VHDR chunk. */
27781 psf_binheader_writef (psf, "Em4", VHDR_MARKER, sizeof (VHDR_CHUNK)) ;
27782 /* VHDR : oneShotHiSamples, repeatHiSamples, samplesPerHiCycle */
27783 psf_binheader_writef (psf, "E444", psf->sf.frames, 0, 0) ;
27784 /* VHDR : samplesPerSec, octave, compression */
27785 psf_binheader_writef (psf, "E211", psf->sf.samplerate, 1, 0) ;
27786 /* VHDR : volume */
27787 psf_binheader_writef (psf, "E4", (psf->bytewidth == 1) ? 0xFF : 0xFFFF) ;
27789 /* Filename and annotation strings. */
27790 psf_binheader_writef (psf, "Emsms", NAME_MARKER, psf->filename, ANNO_MARKER, annotation) ;
27792 /* BODY marker and size. */
27793 psf_binheader_writef (psf, "Etm8", BODY_MARKER, (psf->datalength < 0) ?
27794 psf->datalength * 0 : psf->datalength) ;
27796 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
27798 if (psf->error)
27799 return psf->error ;
27801 psf->dataoffset = psf->headindex ;
27803 if (current > 0)
27804 psf_fseek (psf, current, SEEK_SET) ;
27806 return psf->error ;
27807 } /* svx_write_header */
27811 ** Do not edit or modify anything in this comment block.
27812 ** The arch-tag line is a file identity tag for the GNU Arch
27813 ** revision control system.
27815 ** arch-tag: a80ab6fb-7d75-4d32-a6b0-0061a3f05d95
27818 * Copyright 1992 by Jutta Degener and Carsten Bormann, Technische
27819 * Universitaet Berlin. See the accompanying file "COPYRIGHT" for
27820 * details. THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE.
27823 /* Most of these tables are inlined at their point of use.
27826 /* 4.4 TABLES USED IN THE FIXED POINT IMPLEMENTATION OF THE RPE-LTP
27827 * CODER AND DECODER
27829 * (Most of them inlined, so watch out.)
27832 #define GSM_TABLE_C
27834 /* Table 4.1 Quantization of the Log.-Area Ratios
27836 /* i 1 2 3 4 5 6 7 8 */
27837 word gsm_A[8] = {20480, 20480, 20480, 20480, 13964, 15360, 8534, 9036};
27838 word gsm_B[8] = { 0, 0, 2048, -2560, 94, -1792, -341, -1144};
27839 word gsm_MIC[8] = { -32, -32, -16, -16, -8, -8, -4, -4 };
27840 word gsm_MAC[8] = { 31, 31, 15, 15, 7, 7, 3, 3 };
27843 /* Table 4.2 Tabulation of 1/A[1..8]
27845 word gsm_INVA[8]={ 13107, 13107, 13107, 13107, 19223, 17476, 31454, 29708 };
27848 /* Table 4.3a Decision level of the LTP gain quantizer
27850 /* bc 0 1 2 3 */
27851 word gsm_DLB[4] = { 6554, 16384, 26214, 32767 };
27854 /* Table 4.3b Quantization levels of the LTP gain quantizer
27856 /* bc 0 1 2 3 */
27857 word gsm_QLB[4] = { 3277, 11469, 21299, 32767 };
27860 /* Table 4.4 Coefficients of the weighting filter
27862 /* i 0 1 2 3 4 5 6 7 8 9 10 */
27863 word gsm_H[11] = {-134, -374, 0, 2054, 5741, 8192, 5741, 2054, 0, -374, -134 };
27866 /* Table 4.5 Normalized inverse mantissa used to compute xM/xmax
27868 /* i 0 1 2 3 4 5 6 7 */
27869 word gsm_NRFAC[8] = { 29128, 26215, 23832, 21846, 20165, 18725, 17476, 16384 };
27872 /* Table 4.6 Normalized direct mantissa used to compute xM/xmax
27874 /* i 0 1 2 3 4 5 6 7 */
27875 word gsm_FAC[8] = { 18431, 20479, 22527, 24575, 26623, 28671, 30719, 32767 };
27877 ** Do not edit or modify anything in this comment block.
27878 ** The arch-tag line is a file identity tag for the GNU Arch
27879 ** revision control system.
27881 ** arch-tag: 8957c531-e6b0-4097-9202-da7ca42729ca
27885 ** Copyright (C) 2002-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
27887 ** This program is free software; you can redistribute it and/or modify
27888 ** it under the terms of the GNU Lesser General Public License as published by
27889 ** the Free Software Foundation; either version 2.1 of the License, or
27890 ** (at your option) any later version.
27892 ** This program is distributed in the hope that it will be useful,
27893 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
27894 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27895 ** GNU Lesser General Public License for more details.
27897 ** You should have received a copy of the GNU Lesser General Public License
27898 ** along with this program; if not, write to the Free Software
27899 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27902 /*===========================================================================
27903 ** Yamaha TX16 Sampler Files.
27905 ** This header parser was written using information from the SoX source code
27906 ** and trial and error experimentation. The code here however is all original.
27909 #include <stdio.h>
27910 #include <fcntl.h>
27911 #include <string.h>
27912 #include <ctype.h>
27915 #if (ENABLE_EXPERIMENTAL_CODE == 0)
27918 txw_open (SF_PRIVATE *psf)
27919 { if (psf)
27920 return SFE_UNIMPLEMENTED ;
27921 return (psf && 0) ;
27922 } /* txw_open */
27924 #else
27926 /*------------------------------------------------------------------------------
27927 ** Markers.
27930 #define TXW_DATA_OFFSET 32
27932 #define TXW_LOOPED 0x49
27933 #define TXW_NO_LOOP 0xC9
27935 /*------------------------------------------------------------------------------
27936 ** Private static functions.
27939 static int txw_read_header (SF_PRIVATE *psf) ;
27941 static sf_count_t txw_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
27942 static sf_count_t txw_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
27943 static sf_count_t txw_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
27944 static sf_count_t txw_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
27946 static sf_count_t txw_seek (SF_PRIVATE *psf, int mode, sf_count_t offset) ;
27948 /*------------------------------------------------------------------------------
27949 ** Public functions.
27953 * ftp://ftp.t0.or.at/pub/sound/tx16w/samples.yamaha
27954 * ftp://ftp.t0.or.at/pub/sound/tx16w/faq/tx16w.tec
27955 * http://www.t0.or.at/~mpakesch/tx16w/
27957 * from tx16w.c sox 12.15: (7-Oct-98) (Mark Lakata and Leigh Smith)
27958 * char filetype[6] "LM8953"
27959 * nulls[10],
27960 * dummy_aeg[6]
27961 * format 0x49 = looped, 0xC9 = non-looped
27962 * sample_rate 1 = 33 kHz, 2 = 50 kHz, 3 = 16 kHz
27963 * atc_length[3] if sample rate 0, [2]&0xfe = 6: 33kHz, 0x10:50, 0xf6: 16,
27964 * depending on [5] but to heck with it
27965 * rpt_length[3] (these are for looped samples, attack and loop lengths)
27966 * unused[2]
27969 typedef struct
27970 { unsigned char format, srate, sr2, sr3 ;
27971 unsigned short srhash ;
27972 unsigned int attacklen, repeatlen ;
27973 } TXW_HEADER ;
27975 #define ERROR_666 666
27978 txw_open (SF_PRIVATE *psf)
27979 { int error ;
27981 if (psf->mode != SFM_READ)
27982 return SFE_UNIMPLEMENTED ;
27984 if ((error = txw_read_header (psf)))
27985 return error ;
27987 if (psf_fseek (psf, psf->dataoffset, SEEK_SET) != psf->dataoffset)
27988 return SFE_BAD_SEEK ;
27990 psf->read_short = txw_read_s ;
27991 psf->read_int = txw_read_i ;
27992 psf->read_float = txw_read_f ;
27993 psf->read_double = txw_read_d ;
27995 psf->seek = txw_seek ;
27997 return 0 ;
27998 } /* txw_open */
28000 /*------------------------------------------------------------------------------
28003 static int
28004 txw_read_header (SF_PRIVATE *psf)
28005 { TXW_HEADER txwh ;
28006 char *strptr ;
28008 memset (&txwh, 0, sizeof (txwh)) ;
28009 memset (psf->buffer, 0, sizeof (psf->buffer)) ;
28010 psf_binheader_readf (psf, "pb", 0, psf->buffer, 16) ;
28012 if (memcmp (psf->buffer, "LM8953\0\0\0\0\0\0\0\0\0\0", 16) != 0)
28013 return ERROR_666 ;
28015 psf_log_printf (psf, "Read only : Yamaha TX-16 Sampler (.txw)\nLM8953\n") ;
28017 /* Jump 6 bytes (dummp_aeg), read format, read sample rate. */
28018 psf_binheader_readf (psf, "j11", 6, &txwh.format, &txwh.srate) ;
28020 /* 8 bytes (atc_length[3], rpt_length[3], unused[2]). */
28021 psf_binheader_readf (psf, "e33j", &txwh.attacklen, &txwh.repeatlen, 2) ;
28022 txwh.sr2 = (txwh.attacklen >> 16) & 0xFE ;
28023 txwh.sr3 = (txwh.repeatlen >> 16) & 0xFE ;
28024 txwh.attacklen &= 0x1FFFF ;
28025 txwh.repeatlen &= 0x1FFFF ;
28027 switch (txwh.format)
28028 { case TXW_LOOPED :
28029 strptr = "looped" ;
28030 break ;
28032 case TXW_NO_LOOP :
28033 strptr = "non-looped" ;
28034 break ;
28036 default :
28037 psf_log_printf (psf, " Format : 0x%02x => ?????\n", txwh.format) ;
28038 return ERROR_666 ;
28041 psf_log_printf (psf, " Format : 0x%02X => %s\n", txwh.format, strptr) ;
28043 strptr = NULL ;
28045 switch (txwh.srate)
28046 { case 1 :
28047 psf->sf.samplerate = 33333 ;
28048 break ;
28050 case 2 :
28051 psf->sf.samplerate = 50000 ;
28052 break ;
28054 case 3 :
28055 psf->sf.samplerate = 16667 ;
28056 break ;
28058 default :
28059 /* This is ugly and braindead. */
28060 txwh.srhash = ((txwh.sr2 & 0xFE) << 8) | (txwh.sr3 & 0xFE) ;
28061 switch (txwh.srhash)
28062 { case ((0x6 << 8) | 0x52) :
28063 psf->sf.samplerate = 33333 ;
28064 break ;
28066 case ((0x10 << 8) | 0x52) :
28067 psf->sf.samplerate = 50000 ;
28068 break ;
28070 case ((0xF6 << 8) | 0x52) :
28071 psf->sf.samplerate = 166667 ;
28072 break ;
28074 default :
28075 strptr = " Sample Rate : Unknown : forcing to 33333\n" ;
28076 psf->sf.samplerate = 33333 ;
28077 break ;
28082 if (strptr)
28083 psf_log_printf (psf, strptr) ;
28084 else if (txwh.srhash)
28085 psf_log_printf (psf, " Sample Rate : %d (0x%X) => %d\n", txwh.srate, txwh.srhash, psf->sf.samplerate) ;
28086 else
28087 psf_log_printf (psf, " Sample Rate : %d => %d\n", txwh.srate, psf->sf.samplerate) ;
28089 if (txwh.format == TXW_LOOPED)
28090 { psf_log_printf (psf, " Attack Len : %d\n", txwh.attacklen) ;
28091 psf_log_printf (psf, " Repeat Len : %d\n", txwh.repeatlen) ;
28094 psf->dataoffset = TXW_DATA_OFFSET ;
28095 psf->datalength = psf->filelength - TXW_DATA_OFFSET ;
28096 psf->sf.frames = 2 * psf->datalength / 3 ;
28099 if (psf->datalength % 3 == 1)
28100 psf_log_printf (psf, "*** File seems to be truncated, %d extra bytes.\n",
28101 (int) (psf->datalength % 3)) ;
28103 if (txwh.attacklen + txwh.repeatlen > psf->sf.frames)
28104 psf_log_printf (psf, "*** File has been truncated.\n") ;
28106 psf->sf.format = SF_FORMAT_TXW | SF_FORMAT_PCM_16 ;
28107 psf->sf.channels = 1 ;
28108 psf->sf.sections = 1 ;
28109 psf->sf.seekable = SF_TRUE ;
28111 return 0 ;
28112 } /* txw_read_header */
28114 static sf_count_t
28115 txw_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
28116 { unsigned char *ucptr ;
28117 short sample ;
28118 int k, bufferlen, readcount, count ;
28119 sf_count_t total = 0 ;
28121 bufferlen = sizeof (psf->buffer) / 3 ;
28122 bufferlen -= (bufferlen & 1) ;
28123 while (len > 0)
28124 { readcount = (len >= bufferlen) ? bufferlen : len ;
28125 count = psf_fread (psf->buffer, 3, readcount, psf) ;
28127 ucptr = (unsigned char *) psf->buffer ;
28128 for (k = 0 ; k < readcount ; k += 2)
28129 { sample = (ucptr [0] << 8) | (ucptr [1] & 0xF0) ;
28130 ptr [total + k] = sample ;
28131 sample = (ucptr [2] << 8) | ((ucptr [1] & 0xF) << 4) ;
28132 ptr [total + k + 1] = sample ;
28133 ucptr += 3 ;
28136 total += count ;
28137 len -= readcount ;
28140 return total ;
28141 } /* txw_read_s */
28143 static sf_count_t
28144 txw_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
28145 { unsigned char *ucptr ;
28146 short sample ;
28147 int k, bufferlen, readcount, count ;
28148 sf_count_t total = 0 ;
28150 bufferlen = sizeof (psf->buffer) / 3 ;
28151 bufferlen -= (bufferlen & 1) ;
28152 while (len > 0)
28153 { readcount = (len >= bufferlen) ? bufferlen : len ;
28154 count = psf_fread (psf->buffer, 3, readcount, psf) ;
28156 ucptr = (unsigned char *) psf->buffer ;
28157 for (k = 0 ; k < readcount ; k += 2)
28158 { sample = (ucptr [0] << 8) | (ucptr [1] & 0xF0) ;
28159 ptr [total + k] = sample << 16 ;
28160 sample = (ucptr [2] << 8) | ((ucptr [1] & 0xF) << 4) ;
28161 ptr [total + k + 1] = sample << 16 ;
28162 ucptr += 3 ;
28165 total += count ;
28166 len -= readcount ;
28169 return total ;
28170 } /* txw_read_i */
28172 static sf_count_t
28173 txw_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
28174 { unsigned char *ucptr ;
28175 short sample ;
28176 int k, bufferlen, readcount, count ;
28177 sf_count_t total = 0 ;
28178 float normfact ;
28180 if (psf->norm_float == SF_TRUE)
28181 normfact = 1.0 / 0x8000 ;
28182 else
28183 normfact = 1.0 / 0x10 ;
28185 bufferlen = sizeof (psf->buffer) / 3 ;
28186 bufferlen -= (bufferlen & 1) ;
28187 while (len > 0)
28188 { readcount = (len >= bufferlen) ? bufferlen : len ;
28189 count = psf_fread (psf->buffer, 3, readcount, psf) ;
28191 ucptr = (unsigned char *) psf->buffer ;
28192 for (k = 0 ; k < readcount ; k += 2)
28193 { sample = (ucptr [0] << 8) | (ucptr [1] & 0xF0) ;
28194 ptr [total + k] = normfact * sample ;
28195 sample = (ucptr [2] << 8) | ((ucptr [1] & 0xF) << 4) ;
28196 ptr [total + k + 1] = normfact * sample ;
28197 ucptr += 3 ;
28200 total += count ;
28201 len -= readcount ;
28204 return total ;
28205 } /* txw_read_f */
28207 static sf_count_t
28208 txw_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
28209 { unsigned char *ucptr ;
28210 short sample ;
28211 int k, bufferlen, readcount, count ;
28212 sf_count_t total = 0 ;
28213 double normfact ;
28215 if (psf->norm_double == SF_TRUE)
28216 normfact = 1.0 / 0x8000 ;
28217 else
28218 normfact = 1.0 / 0x10 ;
28220 bufferlen = sizeof (psf->buffer) / 3 ;
28221 bufferlen -= (bufferlen & 1) ;
28222 while (len > 0)
28223 { readcount = (len >= bufferlen) ? bufferlen : len ;
28224 count = psf_fread (psf->buffer, 3, readcount, psf) ;
28226 ucptr = (unsigned char *) psf->buffer ;
28227 for (k = 0 ; k < readcount ; k += 2)
28228 { sample = (ucptr [0] << 8) | (ucptr [1] & 0xF0) ;
28229 ptr [total + k] = normfact * sample ;
28230 sample = (ucptr [2] << 8) | ((ucptr [1] & 0xF) << 4) ;
28231 ptr [total + k + 1] = normfact * sample ;
28232 ucptr += 3 ;
28235 total += count ;
28236 len -= readcount ;
28239 return total ;
28240 } /* txw_read_d */
28242 static sf_count_t
28243 txw_seek (SF_PRIVATE *psf, int mode, sf_count_t offset)
28244 { if (psf && mode)
28245 return offset ;
28247 return 0 ;
28248 } /* txw_seek */
28250 #endif
28252 ** Do not edit or modify anything in this comment block.
28253 ** The arch-tag line is a file identity tag for the GNU Arch
28254 ** revision control system.
28256 ** arch-tag: 4d0ba7af-b1c5-46b4-a900-7c6f59fd9a89
28259 ** Copyright (C) 1999-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
28261 ** This program is free software; you can redistribute it and/or modify
28262 ** it under the terms of the GNU Lesser General Public License as published by
28263 ** the Free Software Foundation; either version 2.1 of the License, or
28264 ** (at your option) any later version.
28266 ** This program is distributed in the hope that it will be useful,
28267 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
28268 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28269 ** GNU Lesser General Public License for more details.
28271 ** You should have received a copy of the GNU Lesser General Public License
28272 ** along with this program; if not, write to the Free Software
28273 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28277 static sf_count_t ulaw_read_ulaw2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
28278 static sf_count_t ulaw_read_ulaw2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
28279 static sf_count_t ulaw_read_ulaw2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
28280 static sf_count_t ulaw_read_ulaw2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
28282 static sf_count_t ulaw_write_s2ulaw (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
28283 static sf_count_t ulaw_write_i2ulaw (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
28284 static sf_count_t ulaw_write_f2ulaw (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
28285 static sf_count_t ulaw_write_d2ulaw (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
28287 static void ulaw2s_array (unsigned char *buffer, unsigned int count, short *ptr) ;
28288 static void ulaw2i_array (unsigned char *buffer, unsigned int count, int *ptr) ;
28289 static void ulaw2f_array (unsigned char *buffer, unsigned int count, float *ptr, float normfact) ;
28290 static void ulaw2d_array (unsigned char *buffer, unsigned int count, double *ptr, double normfact) ;
28292 static void s2ulaw_array (short *buffer, unsigned int count, unsigned char *ptr) ;
28293 static void i2ulaw_array (int *buffer, unsigned int count, unsigned char *ptr) ;
28294 static void f2ulaw_array (float *buffer, unsigned int count, unsigned char *ptr, float normfact) ;
28295 static void d2ulaw_array (double *buffer, unsigned int count, unsigned char *ptr, double normfact) ;
28299 ulaw_init (SF_PRIVATE *psf)
28301 if (psf->mode == SFM_READ || psf->mode == SFM_RDWR)
28302 { psf->read_short = ulaw_read_ulaw2s ;
28303 psf->read_int = ulaw_read_ulaw2i ;
28304 psf->read_float = ulaw_read_ulaw2f ;
28305 psf->read_double = ulaw_read_ulaw2d ;
28308 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
28309 { psf->write_short = ulaw_write_s2ulaw ;
28310 psf->write_int = ulaw_write_i2ulaw ;
28311 psf->write_float = ulaw_write_f2ulaw ;
28312 psf->write_double = ulaw_write_d2ulaw ;
28315 psf->bytewidth = 1 ;
28316 psf->blockwidth = psf->sf.channels ;
28318 if (psf->filelength > psf->dataoffset)
28319 psf->datalength = (psf->dataend) ? psf->dataend - psf->dataoffset :
28320 psf->filelength - psf->dataoffset ;
28321 else
28322 psf->datalength = 0 ;
28324 psf->sf.frames = psf->datalength / psf->blockwidth ;
28326 return 0 ;
28327 } /* ulaw_init */
28329 static sf_count_t
28330 ulaw_read_ulaw2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
28331 { int bufferlen, readcount, thisread ;
28332 sf_count_t total = 0 ;
28334 bufferlen = sizeof (psf->buffer) / sizeof (char) ;
28336 while (len > 0)
28337 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
28338 thisread = psf_fread (psf->buffer, 1, readcount, psf) ;
28339 ulaw2s_array ((unsigned char*) (psf->buffer), thisread, ptr + total) ;
28340 total += thisread ;
28341 if (thisread < readcount)
28342 break ;
28343 len -= thisread ;
28346 return total ;
28347 } /* ulaw_read_ulaw2s */
28349 static sf_count_t
28350 ulaw_read_ulaw2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
28351 { int bufferlen, readcount, thisread ;
28352 sf_count_t total = 0 ;
28354 bufferlen = sizeof (psf->buffer) / sizeof (char) ;
28356 while (len > 0)
28357 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
28358 thisread = psf_fread (psf->buffer, 1, readcount, psf) ;
28359 ulaw2i_array ((unsigned char*) (psf->buffer), thisread, ptr + total) ;
28360 total += thisread ;
28361 if (thisread < readcount)
28362 break ;
28363 len -= thisread ;
28366 return total ;
28367 } /* ulaw_read_ulaw2i */
28369 static sf_count_t
28370 ulaw_read_ulaw2f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
28371 { int bufferlen, readcount, thisread ;
28372 sf_count_t total = 0 ;
28373 float normfact ;
28375 normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x8000) : 1.0 ;
28377 bufferlen = sizeof (psf->buffer) / sizeof (char) ;
28379 while (len > 0)
28380 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
28381 thisread = psf_fread (psf->buffer, 1, readcount, psf) ;
28382 ulaw2f_array ((unsigned char*) (psf->buffer), thisread, ptr + total, normfact) ;
28383 total += thisread ;
28384 if (thisread < readcount)
28385 break ;
28386 len -= thisread ;
28389 return total ;
28390 } /* ulaw_read_ulaw2f */
28392 static sf_count_t
28393 ulaw_read_ulaw2d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
28394 { int bufferlen, readcount, thisread ;
28395 sf_count_t total = 0 ;
28396 double normfact ;
28398 normfact = (psf->norm_double) ? 1.0 / ((double) 0x8000) : 1.0 ;
28399 bufferlen = sizeof (psf->buffer) / sizeof (char) ;
28401 while (len > 0)
28402 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
28403 thisread = psf_fread (psf->buffer, 1, readcount, psf) ;
28404 ulaw2d_array ((unsigned char*) (psf->buffer), thisread, ptr + total, normfact) ;
28405 total += thisread ;
28406 if (thisread < readcount)
28407 break ;
28408 len -= thisread ;
28411 return total ;
28412 } /* ulaw_read_ulaw2d */
28414 /*=============================================================================================
28417 static sf_count_t
28418 ulaw_write_s2ulaw (SF_PRIVATE *psf, short *ptr, sf_count_t len)
28419 { int bufferlen, writecount, thiswrite ;
28420 sf_count_t total = 0 ;
28422 bufferlen = sizeof (psf->buffer) / sizeof (char) ;
28424 while (len > 0)
28425 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
28426 s2ulaw_array (ptr + total, writecount, (unsigned char*) (psf->buffer)) ;
28427 thiswrite = psf_fwrite (psf->buffer, 1, writecount, psf) ;
28428 total += thiswrite ;
28429 if (thiswrite < writecount)
28430 break ;
28431 len -= thiswrite ;
28434 return total ;
28435 } /* ulaw_write_s2ulaw */
28437 static sf_count_t
28438 ulaw_write_i2ulaw (SF_PRIVATE *psf, int *ptr, sf_count_t len)
28439 { int bufferlen, writecount, thiswrite ;
28440 sf_count_t total = 0 ;
28442 bufferlen = sizeof (psf->buffer) / sizeof (char) ;
28444 while (len > 0)
28445 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
28446 i2ulaw_array (ptr + total, writecount, (unsigned char*) (psf->buffer)) ;
28447 thiswrite = psf_fwrite (psf->buffer, 1, writecount, psf) ;
28448 total += thiswrite ;
28449 if (thiswrite < writecount)
28450 break ;
28451 len -= thiswrite ;
28454 return total ;
28455 } /* ulaw_write_i2ulaw */
28457 static sf_count_t
28458 ulaw_write_f2ulaw (SF_PRIVATE *psf, float *ptr, sf_count_t len)
28459 { int bufferlen, writecount, thiswrite ;
28460 sf_count_t total = 0 ;
28461 float normfact ;
28463 normfact = (psf->norm_float == SF_TRUE) ? (1.0 * 0x7FFF) : 1.0 ;
28465 bufferlen = sizeof (psf->buffer) / sizeof (char) ;
28467 while (len > 0)
28468 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
28469 f2ulaw_array (ptr + total, writecount, (unsigned char*) (psf->buffer), normfact) ;
28470 thiswrite = psf_fwrite (psf->buffer, 1, writecount, psf) ;
28471 total += thiswrite ;
28472 if (thiswrite < writecount)
28473 break ;
28474 len -= thiswrite ;
28477 return total ;
28478 } /* ulaw_write_f2ulaw */
28480 static sf_count_t
28481 ulaw_write_d2ulaw (SF_PRIVATE *psf, double *ptr, sf_count_t len)
28482 { int bufferlen, writecount, thiswrite ;
28483 sf_count_t total = 0 ;
28484 double normfact ;
28486 normfact = (psf->norm_double) ? (1.0 * 0x7FFF) : 1.0 ;
28488 bufferlen = sizeof (psf->buffer) / sizeof (char) ;
28490 while (len > 0)
28491 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
28492 d2ulaw_array (ptr + total, writecount, (unsigned char*) (psf->buffer), normfact) ;
28493 thiswrite = psf_fwrite (psf->buffer, 1, writecount, psf) ;
28494 total += thiswrite ;
28495 if (thiswrite < writecount)
28496 break ;
28497 len -= thiswrite ;
28500 return total ;
28501 } /* ulaw_write_d2ulaw */
28503 /*=============================================================================================
28504 * Private static functions and data.
28507 static short ulaw_decode [128] =
28508 { -32124, -31100, -30076, -29052, -28028, -27004, -25980, -24956,
28509 -23932, -22908, -21884, -20860, -19836, -18812, -17788, -16764,
28510 -15996, -15484, -14972, -14460, -13948, -13436, -12924, -12412,
28511 -11900, -11388, -10876, -10364, -9852, -9340, -8828, -8316,
28512 -7932, -7676, -7420, -7164, -6908, -6652, -6396, -6140,
28513 -5884, -5628, -5372, -5116, -4860, -4604, -4348, -4092,
28514 -3900, -3772, -3644, -3516, -3388, -3260, -3132, -3004,
28515 -2876, -2748, -2620, -2492, -2364, -2236, -2108, -1980,
28516 -1884, -1820, -1756, -1692, -1628, -1564, -1500, -1436,
28517 -1372, -1308, -1244, -1180, -1116, -1052, -988, -924,
28518 -876, -844, -812, -780, -748, -716, -684, -652,
28519 -620, -588, -556, -524, -492, -460, -428, -396,
28520 -372, -356, -340, -324, -308, -292, -276, -260,
28521 -244, -228, -212, -196, -180, -164, -148, -132,
28522 -120, -112, -104, -96, -88, -80, -72, -64,
28523 -56, -48, -40, -32, -24, -16, -8, 0,
28526 static
28527 unsigned char ulaw_encode [8193] =
28528 { 0xFF, 0xFE, 0xFE, 0xFD, 0xFD, 0xFC, 0xFC, 0xFB, 0xFB, 0xFA, 0xFA, 0xF9,
28529 0xF9, 0xF8, 0xF8, 0xF7, 0xF7, 0xF6, 0xF6, 0xF5, 0xF5, 0xF4, 0xF4, 0xF3,
28530 0xF3, 0xF2, 0xF2, 0xF1, 0xF1, 0xF0, 0xF0, 0xEF, 0xEF, 0xEF, 0xEF, 0xEE,
28531 0xEE, 0xEE, 0xEE, 0xED, 0xED, 0xED, 0xED, 0xEC, 0xEC, 0xEC, 0xEC, 0xEB,
28532 0xEB, 0xEB, 0xEB, 0xEA, 0xEA, 0xEA, 0xEA, 0xE9, 0xE9, 0xE9, 0xE9, 0xE8,
28533 0xE8, 0xE8, 0xE8, 0xE7, 0xE7, 0xE7, 0xE7, 0xE6, 0xE6, 0xE6, 0xE6, 0xE5,
28534 0xE5, 0xE5, 0xE5, 0xE4, 0xE4, 0xE4, 0xE4, 0xE3, 0xE3, 0xE3, 0xE3, 0xE2,
28535 0xE2, 0xE2, 0xE2, 0xE1, 0xE1, 0xE1, 0xE1, 0xE0, 0xE0, 0xE0, 0xE0, 0xDF,
28536 0xDF, 0xDF, 0xDF, 0xDF, 0xDF, 0xDF, 0xDF, 0xDE, 0xDE, 0xDE, 0xDE, 0xDE,
28537 0xDE, 0xDE, 0xDE, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDC,
28538 0xDC, 0xDC, 0xDC, 0xDC, 0xDC, 0xDC, 0xDC, 0xDB, 0xDB, 0xDB, 0xDB, 0xDB,
28539 0xDB, 0xDB, 0xDB, 0xDA, 0xDA, 0xDA, 0xDA, 0xDA, 0xDA, 0xDA, 0xDA, 0xD9,
28540 0xD9, 0xD9, 0xD9, 0xD9, 0xD9, 0xD9, 0xD9, 0xD8, 0xD8, 0xD8, 0xD8, 0xD8,
28541 0xD8, 0xD8, 0xD8, 0xD7, 0xD7, 0xD7, 0xD7, 0xD7, 0xD7, 0xD7, 0xD7, 0xD6,
28542 0xD6, 0xD6, 0xD6, 0xD6, 0xD6, 0xD6, 0xD6, 0xD5, 0xD5, 0xD5, 0xD5, 0xD5,
28543 0xD5, 0xD5, 0xD5, 0xD4, 0xD4, 0xD4, 0xD4, 0xD4, 0xD4, 0xD4, 0xD4, 0xD3,
28544 0xD3, 0xD3, 0xD3, 0xD3, 0xD3, 0xD3, 0xD3, 0xD2, 0xD2, 0xD2, 0xD2, 0xD2,
28545 0xD2, 0xD2, 0xD2, 0xD1, 0xD1, 0xD1, 0xD1, 0xD1, 0xD1, 0xD1, 0xD1, 0xD0,
28546 0xD0, 0xD0, 0xD0, 0xD0, 0xD0, 0xD0, 0xD0, 0xCF, 0xCF, 0xCF, 0xCF, 0xCF,
28547 0xCF, 0xCF, 0xCF, 0xCF, 0xCF, 0xCF, 0xCF, 0xCF, 0xCF, 0xCF, 0xCF, 0xCE,
28548 0xCE, 0xCE, 0xCE, 0xCE, 0xCE, 0xCE, 0xCE, 0xCE, 0xCE, 0xCE, 0xCE, 0xCE,
28549 0xCE, 0xCE, 0xCE, 0xCD, 0xCD, 0xCD, 0xCD, 0xCD, 0xCD, 0xCD, 0xCD, 0xCD,
28550 0xCD, 0xCD, 0xCD, 0xCD, 0xCD, 0xCD, 0xCD, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
28551 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCB,
28552 0xCB, 0xCB, 0xCB, 0xCB, 0xCB, 0xCB, 0xCB, 0xCB, 0xCB, 0xCB, 0xCB, 0xCB,
28553 0xCB, 0xCB, 0xCB, 0xCA, 0xCA, 0xCA, 0xCA, 0xCA, 0xCA, 0xCA, 0xCA, 0xCA,
28554 0xCA, 0xCA, 0xCA, 0xCA, 0xCA, 0xCA, 0xCA, 0xC9, 0xC9, 0xC9, 0xC9, 0xC9,
28555 0xC9, 0xC9, 0xC9, 0xC9, 0xC9, 0xC9, 0xC9, 0xC9, 0xC9, 0xC9, 0xC9, 0xC8,
28556 0xC8, 0xC8, 0xC8, 0xC8, 0xC8, 0xC8, 0xC8, 0xC8, 0xC8, 0xC8, 0xC8, 0xC8,
28557 0xC8, 0xC8, 0xC8, 0xC7, 0xC7, 0xC7, 0xC7, 0xC7, 0xC7, 0xC7, 0xC7, 0xC7,
28558 0xC7, 0xC7, 0xC7, 0xC7, 0xC7, 0xC7, 0xC7, 0xC6, 0xC6, 0xC6, 0xC6, 0xC6,
28559 0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0xC6, 0xC5,
28560 0xC5, 0xC5, 0xC5, 0xC5, 0xC5, 0xC5, 0xC5, 0xC5, 0xC5, 0xC5, 0xC5, 0xC5,
28561 0xC5, 0xC5, 0xC5, 0xC4, 0xC4, 0xC4, 0xC4, 0xC4, 0xC4, 0xC4, 0xC4, 0xC4,
28562 0xC4, 0xC4, 0xC4, 0xC4, 0xC4, 0xC4, 0xC4, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3,
28563 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC2,
28564 0xC2, 0xC2, 0xC2, 0xC2, 0xC2, 0xC2, 0xC2, 0xC2, 0xC2, 0xC2, 0xC2, 0xC2,
28565 0xC2, 0xC2, 0xC2, 0xC1, 0xC1, 0xC1, 0xC1, 0xC1, 0xC1, 0xC1, 0xC1, 0xC1,
28566 0xC1, 0xC1, 0xC1, 0xC1, 0xC1, 0xC1, 0xC1, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0,
28567 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xBF,
28568 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF,
28569 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF,
28570 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE,
28571 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE,
28572 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE, 0xBE,
28573 0xBE, 0xBE, 0xBE, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD,
28574 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD,
28575 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBC,
28576 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC,
28577 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC,
28578 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
28579 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
28580 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
28581 0xBB, 0xBB, 0xBB, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA,
28582 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA,
28583 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xBA, 0xB9,
28584 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB9,
28585 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB9,
28586 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB9, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8,
28587 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8,
28588 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8, 0xB8,
28589 0xB8, 0xB8, 0xB8, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7,
28590 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7,
28591 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB7, 0xB6,
28592 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB6,
28593 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB6,
28594 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB6, 0xB5, 0xB5, 0xB5, 0xB5, 0xB5,
28595 0xB5, 0xB5, 0xB5, 0xB5, 0xB5, 0xB5, 0xB5, 0xB5, 0xB5, 0xB5, 0xB5, 0xB5,
28596 0xB5, 0xB5, 0xB5, 0xB5, 0xB5, 0xB5, 0xB5, 0xB5, 0xB5, 0xB5, 0xB5, 0xB5,
28597 0xB5, 0xB5, 0xB5, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4,
28598 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4,
28599 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB4, 0xB3,
28600 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3,
28601 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3,
28602 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2,
28603 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2,
28604 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2, 0xB2,
28605 0xB2, 0xB2, 0xB2, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1,
28606 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1,
28607 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB0,
28608 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0,
28609 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0,
28610 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF,
28611 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 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, 0xAE,
28616 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 0xAE, 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, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD,
28622 0xAD, 0xAD, 0xAD, 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, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC,
28627 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 0xAC, 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, 0xAB,
28632 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 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, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
28638 0xAA, 0xAA, 0xAA, 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, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9,
28643 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 0xA9, 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, 0xA8,
28648 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 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, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7, 0xA7,
28654 0xA7, 0xA7, 0xA7, 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, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6,
28659 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 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, 0xA5,
28664 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 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, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4, 0xA4,
28670 0xA4, 0xA4, 0xA4, 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, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3,
28675 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 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, 0xA2,
28680 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 0xA2, 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, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1, 0xA1,
28686 0xA1, 0xA1, 0xA1, 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, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0,
28691 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 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, 0x9F,
28696 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 0x9F, 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, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E,
28707 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 0x9E, 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, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D, 0x9D,
28718 0x9D, 0x9D, 0x9D, 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, 0x9C,
28728 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 0x9C, 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, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B,
28739 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 0x9B, 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, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A, 0x9A,
28750 0x9A, 0x9A, 0x9A, 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, 0x99,
28760 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 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, 0x98, 0x98, 0x98, 0x98, 0x98,
28771 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 0x98, 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, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97, 0x97,
28782 0x97, 0x97, 0x97, 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, 0x96,
28792 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 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, 0x95, 0x95, 0x95, 0x95, 0x95,
28803 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 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, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94, 0x94,
28814 0x94, 0x94, 0x94, 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, 0x93,
28824 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 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, 0x92, 0x92, 0x92, 0x92, 0x92,
28835 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 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, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91, 0x91,
28846 0x91, 0x91, 0x91, 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, 0x90,
28856 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 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, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F,
28867 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 0x8F, 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, 0x8E,
28888 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 0x8E, 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, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D, 0x8D,
28910 0x8D, 0x8D, 0x8D, 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, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C,
28931 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 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, 0x8B,
28952 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 0x8B, 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, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A, 0x8A,
28974 0x8A, 0x8A, 0x8A, 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, 0x89, 0x89, 0x89, 0x89, 0x89,
28995 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 0x89, 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, 0x88,
29016 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 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, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87, 0x87,
29038 0x87, 0x87, 0x87, 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, 0x86, 0x86, 0x86, 0x86, 0x86,
29059 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 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, 0x85,
29080 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 0x85, 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, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84, 0x84,
29102 0x84, 0x84, 0x84, 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, 0x83, 0x83, 0x83, 0x83, 0x83,
29123 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 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, 0x82,
29144 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 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, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
29166 0x81, 0x81, 0x81, 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, 0x80, 0x80, 0x80, 0x80, 0x80,
29187 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 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, 0x00
29213 static void
29214 ulaw2s_array (unsigned char *buffer, unsigned int count, short *ptr)
29215 { while (count)
29216 { count -- ;
29217 if (buffer [count] & 0x80)
29218 ptr [count] = -1 * ulaw_decode [((int) buffer [count]) & 0x7F] ;
29219 else
29220 ptr [count] = ulaw_decode [((int) buffer [count]) & 0x7F] ;
29222 } /* ulaw2s_array */
29224 static void
29225 ulaw2i_array (unsigned char *buffer, unsigned int count, int *ptr)
29226 { while (count)
29227 { count -- ;
29228 if (buffer [count] & 0x80)
29229 ptr [count] = -1 * ulaw_decode [buffer [count] & 0x7F] << 16 ;
29230 else
29231 ptr [count] = ulaw_decode [buffer [count] & 0x7F] << 16 ;
29233 } /* ulaw2i_array */
29235 static void
29236 ulaw2f_array (unsigned char *buffer, unsigned int count, float *ptr, float normfact)
29237 { while (count)
29238 { count -- ;
29239 if (buffer [count] & 0x80)
29240 ptr [count] = -normfact * ulaw_decode [((int) buffer [count]) & 0x7F] ;
29241 else
29242 ptr [count] = normfact * ulaw_decode [((int) buffer [count]) & 0x7F] ;
29244 } /* ulaw2f_array */
29246 static void
29247 ulaw2d_array (unsigned char *buffer, unsigned int count, double *ptr, double normfact)
29248 { while (count)
29249 { count -- ;
29250 if (buffer [count] & 0x80)
29251 ptr [count] = -normfact * ulaw_decode [((int) buffer [count]) & 0x7F] ;
29252 else
29253 ptr [count] = normfact * ulaw_decode [((int) buffer [count]) & 0x7F] ;
29255 } /* ulaw2d_array */
29257 static void
29258 s2ulaw_array (short *ptr, unsigned int count, unsigned char *buffer)
29259 { while (count)
29260 { count -- ;
29261 if (ptr [count] >= 0)
29262 buffer [count] = ulaw_encode [ptr [count] / 4] ;
29263 else
29264 buffer [count] = 0x7F & ulaw_encode [ptr [count] / -4] ;
29266 } /* s2ulaw_array */
29268 static void
29269 i2ulaw_array (int *ptr, unsigned int count, unsigned char *buffer)
29270 { while (count)
29271 { count -- ;
29272 if (ptr [count] >= 0)
29273 buffer [count] = ulaw_encode [ptr [count] >> (16 + 2)] ;
29274 else
29275 buffer [count] = 0x7F & ulaw_encode [-ptr [count] >> (16 + 2)] ;
29277 } /* i2ulaw_array */
29279 static void
29280 f2ulaw_array (float *ptr, unsigned int count, unsigned char *buffer, float normfact)
29281 { while (count)
29282 { count -- ;
29283 if (ptr [count] >= 0)
29284 buffer [count] = ulaw_encode [(lrintf (normfact * ptr [count])) / 4] ;
29285 else
29286 buffer [count] = 0x7F & ulaw_encode [(lrintf (normfact * ptr [count])) / -4] ;
29288 } /* f2ulaw_array */
29290 static void
29291 d2ulaw_array (double *ptr, unsigned int count, unsigned char *buffer, double normfact)
29292 { while (count)
29293 { count -- ;
29294 if (ptr [count] >= 0)
29295 buffer [count] = ulaw_encode [(lrint (normfact * ptr [count])) / 4] ;
29296 else
29297 buffer [count] = 0x7F & ulaw_encode [(lrint (normfact * ptr [count])) / -4] ;
29299 } /* d2ulaw_array */
29302 ** Do not edit or modify anything in this comment block.
29303 ** The arch-tag line is a file identity tag for the GNU Arch
29304 ** revision control system.
29306 ** arch-tag: 655cc790-f058-45e8-89c9-86967cccc37e
29309 ** Copyright (C) 2001-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
29311 ** This program is free software; you can redistribute it and/or modify
29312 ** it under the terms of the GNU Lesser General Public License as published by
29313 ** the Free Software Foundation; either version 2.1 of the License, or
29314 ** (at your option) any later version.
29316 ** This program is distributed in the hope that it will be useful,
29317 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
29318 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29319 ** GNU Lesser General Public License for more details.
29321 ** You should have received a copy of the GNU Lesser General Public License
29322 ** along with this program; if not, write to the Free Software
29323 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29326 /* RANT:
29327 ** The VOC file format is the most brain damaged format I have yet had to deal
29328 ** with. No one programmer could have bee stupid enough to put this together.
29329 ** Instead it looks like a series of manic, dyslexic assembly language programmers
29330 ** hacked it to fit their needs.
29331 ** Utterly woeful.
29335 #include <stdio.h>
29336 #include <stdlib.h>
29337 #include <string.h>
29341 /*------------------------------------------------------------------------------
29342 * Typedefs for file chunks.
29345 #define VOC_MAX_SECTIONS 200
29347 enum
29348 { VOC_TERMINATOR = 0,
29349 VOC_SOUND_DATA = 1,
29350 VOC_SOUND_CONTINUE = 2,
29351 VOC_SILENCE = 3,
29352 VOC_MARKER = 4,
29353 VOC_ASCII = 5,
29354 VOC_REPEAT = 6,
29355 VOC_END_REPEAT = 7,
29356 VOC_EXTENDED = 8,
29357 VOC_EXTENDED_II = 9
29360 typedef struct
29361 { int samples ;
29362 int offset ; /* Offset of zero => silence. */
29363 } SND_DATA_BLOCKS ;
29365 typedef struct
29366 { unsigned int sections, section_types ;
29367 int samplerate, channels, bitwidth ;
29368 SND_DATA_BLOCKS blocks [VOC_MAX_SECTIONS] ;
29369 } VOC_DATA ;
29371 /*------------------------------------------------------------------------------
29372 * Private static functions.
29375 static int voc_close (SF_PRIVATE *psf) ;
29376 static int voc_write_header (SF_PRIVATE *psf, int calc_length) ;
29377 static int voc_read_header (SF_PRIVATE *psf) ;
29379 static const char* voc_encoding2str (int encoding) ;
29381 #if 0
29383 /* These functions would be required for files with more than one VOC_SOUND_DATA
29384 ** segment. Not sure whether to bother implementing this.
29387 static int voc_multi_init (SF_PRIVATE *psf, VOC_DATA *pvoc) ;
29389 static int voc_multi_read_uc2s (SF_PRIVATE *psf, short *ptr, int len) ;
29390 static int voc_multi_read_les2s (SF_PRIVATE *psf, short *ptr, int len) ;
29392 static int voc_multi_read_uc2i (SF_PRIVATE *psf, int *ptr, int len) ;
29393 static int voc_multi_read_les2i (SF_PRIVATE *psf, int *ptr, int len) ;
29395 static int voc_multi_read_uc2f (SF_PRIVATE *psf, float *ptr, int len) ;
29396 static int voc_multi_read_les2f (SF_PRIVATE *psf, float *ptr, int len) ;
29398 static int voc_multi_read_uc2d (SF_PRIVATE *psf, double *ptr, int len) ;
29399 static int voc_multi_read_les2d (SF_PRIVATE *psf, double *ptr, int len) ;
29400 #endif
29402 /*------------------------------------------------------------------------------
29403 ** Public function.
29407 voc_open (SF_PRIVATE *psf)
29408 { int subformat, error = 0 ;
29410 if (psf->is_pipe)
29411 return SFE_VOC_NO_PIPE ;
29413 if (psf->mode == SFM_READ || (psf->mode == SFM_RDWR && psf->filelength > 0))
29414 { if ((error = voc_read_header (psf)))
29415 return error ;
29418 subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
29420 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
29421 { if ((psf->sf.format & SF_FORMAT_TYPEMASK) != SF_FORMAT_VOC)
29422 return SFE_BAD_OPEN_FORMAT ;
29424 psf->endian = SF_ENDIAN_LITTLE ;
29426 if ((error = voc_write_header (psf, SF_FALSE)))
29427 return error ;
29429 psf->write_header = voc_write_header ;
29432 psf->blockwidth = psf->bytewidth * psf->sf.channels ;
29434 psf->close = voc_close ;
29436 switch (subformat)
29437 { case SF_FORMAT_PCM_U8 :
29438 case SF_FORMAT_PCM_16 :
29439 error = pcm_init (psf) ;
29440 break ;
29442 case SF_FORMAT_ALAW :
29443 error = alaw_init (psf) ;
29444 break ;
29446 case SF_FORMAT_ULAW :
29447 error = ulaw_init (psf) ;
29448 break ;
29450 default : return SFE_UNIMPLEMENTED ;
29453 return error ;
29454 } /* voc_open */
29456 /*------------------------------------------------------------------------------
29459 static int
29460 voc_read_header (SF_PRIVATE *psf)
29461 { VOC_DATA *pvoc ;
29462 char creative [20] ;
29463 unsigned char block_type, rate_byte ;
29464 short version, checksum, encoding, dataoffset ;
29465 int offset ;
29467 /* Set position to start of file to begin reading header. */
29468 offset = psf_binheader_readf (psf, "pb", 0, creative, SIGNED_SIZEOF (creative)) ;
29470 if (creative [sizeof (creative) - 1] != 0x1A)
29471 return SFE_VOC_NO_CREATIVE ;
29473 /* Terminate the string. */
29474 creative [sizeof (creative) - 1] = 0 ;
29476 if (strcmp ("Creative Voice File", creative))
29477 return SFE_VOC_NO_CREATIVE ;
29479 psf_log_printf (psf, "%s\n", creative) ;
29481 offset += psf_binheader_readf (psf, "e222", &dataoffset, &version, &checksum) ;
29483 psf->dataoffset = dataoffset ;
29485 psf_log_printf (psf, "dataoffset : %d\n"
29486 "version : 0x%X\n"
29487 "checksum : 0x%X\n", psf->dataoffset, version, checksum) ;
29489 if (version != 0x010A && version != 0x0114)
29490 return SFE_VOC_BAD_VERSION ;
29492 if (! (psf->fdata = malloc (sizeof (VOC_DATA))))
29493 return SFE_MALLOC_FAILED ;
29495 pvoc = (VOC_DATA*) psf->fdata ;
29497 memset (pvoc, 0, sizeof (VOC_DATA)) ;
29499 /* Set the default encoding now. */
29500 psf->sf.format = SF_FORMAT_VOC ; /* Major format */
29501 encoding = SF_FORMAT_PCM_U8 ; /* Minor format */
29502 psf->endian = SF_ENDIAN_LITTLE ;
29504 while (1)
29505 { offset += psf_binheader_readf (psf, "1", &block_type) ;
29507 switch (block_type)
29508 { case VOC_ASCII :
29509 { int size ;
29511 offset += psf_binheader_readf (psf, "e3", &size) ;
29513 psf_log_printf (psf, " ASCII : %d\n", size) ;
29515 offset += psf_binheader_readf (psf, "b", psf->header, size) ;
29516 psf->header [size] = 0 ;
29517 psf_log_printf (psf, " text : %s\n", psf->header) ;
29519 continue ;
29521 case VOC_SOUND_DATA :
29522 case VOC_EXTENDED :
29523 case VOC_EXTENDED_II :
29524 break ;
29526 default : psf_log_printf (psf, "*** Weird block marker (%d)\n", block_type) ;
29529 break ;
29532 if (block_type == VOC_SOUND_DATA)
29533 { unsigned char compression ;
29534 int size ;
29536 offset += psf_binheader_readf (psf, "e311", &size, &rate_byte, &compression) ;
29538 psf->sf.samplerate = 1000000 / (256 - (rate_byte & 0xFF)) ;
29540 psf_log_printf (psf, " Sound Data : %d\n sr : %d => %dHz\n comp : %d\n",
29541 size, rate_byte, psf->sf.samplerate, compression) ;
29543 if (offset + size - 1 > psf->filelength)
29544 { psf_log_printf (psf, "Seems to be a truncated file.\n") ;
29545 psf_log_printf (psf, "offset: %d size: %d sum: %d filelength: %D\n", offset, size, offset + size, psf->filelength) ;
29546 return SFE_VOC_BAD_SECTIONS ;
29548 else if (offset + size - 1 < psf->filelength)
29549 { psf_log_printf (psf, "Seems to be a multi-segment file (#1).\n") ;
29550 psf_log_printf (psf, "offset: %d size: %d sum: %d filelength: %D\n", offset, size, offset + size, psf->filelength) ;
29551 return SFE_VOC_BAD_SECTIONS ;
29554 psf->dataoffset = offset ;
29555 psf->dataend = psf->filelength - 1 ;
29557 psf->sf.channels = 1 ;
29558 psf->bytewidth = 1 ;
29560 psf->sf.format = SF_FORMAT_VOC | SF_FORMAT_PCM_U8 ;
29562 return 0 ;
29565 if (block_type == VOC_EXTENDED)
29566 { unsigned char pack, stereo, compression ;
29567 unsigned short rate_short ;
29568 int size ;
29570 offset += psf_binheader_readf (psf, "e3211", &size, &rate_short, &pack, &stereo) ;
29572 psf_log_printf (psf, " Extended : %d\n", size) ;
29573 if (size == 4)
29574 psf_log_printf (psf, " size : 4\n") ;
29575 else
29576 psf_log_printf (psf, " size : %d (should be 4)\n", size) ;
29578 psf_log_printf (psf, " pack : %d\n"
29579 " stereo : %s\n", pack, (stereo ? "yes" : "no")) ;
29581 if (stereo)
29582 { psf->sf.channels = 2 ;
29583 psf->sf.samplerate = 128000000 / (65536 - rate_short) ;
29585 else
29586 { psf->sf.channels = 1 ;
29587 psf->sf.samplerate = 256000000 / (65536 - rate_short) ;
29590 psf_log_printf (psf, " sr : %d => %dHz\n", (rate_short & 0xFFFF), psf->sf.samplerate) ;
29592 offset += psf_binheader_readf (psf, "1", &block_type) ;
29594 if (block_type != VOC_SOUND_DATA)
29595 { psf_log_printf (psf, "*** Expecting VOC_SOUND_DATA section.\n") ;
29596 return SFE_VOC_BAD_FORMAT ;
29599 offset += psf_binheader_readf (psf, "e311", &size, &rate_byte, &compression) ;
29601 psf_log_printf (psf, " Sound Data : %d\n"
29602 " sr : %d\n"
29603 " comp : %d\n", size, rate_byte, compression) ;
29606 if (offset + size - 1 > psf->filelength)
29607 { psf_log_printf (psf, "Seems to be a truncated file.\n") ;
29608 psf_log_printf (psf, "offset: %d size: %d sum: %d filelength: %D\n", offset, size, offset + size, psf->filelength) ;
29609 return SFE_VOC_BAD_SECTIONS ;
29611 else if (offset + size - 1 < psf->filelength)
29612 { psf_log_printf (psf, "Seems to be a multi-segment file (#2).\n") ;
29613 psf_log_printf (psf, "offset: %d size: %d sum: %d filelength: %D\n", offset, size, offset + size, psf->filelength) ;
29614 return SFE_VOC_BAD_SECTIONS ;
29617 psf->dataoffset = offset ;
29618 psf->dataend = psf->filelength - 1 ;
29620 psf->bytewidth = 1 ;
29622 psf->sf.format = SF_FORMAT_VOC | SF_FORMAT_PCM_U8 ;
29624 return 0 ;
29627 if (block_type == VOC_EXTENDED_II)
29628 { unsigned char bitwidth, channels ;
29629 int size, fourbytes ;
29631 offset += psf_binheader_readf (psf, "e341124", &size, &psf->sf.samplerate,
29632 &bitwidth, &channels, &encoding, &fourbytes) ;
29634 if (size * 2 == psf->filelength - 39)
29635 { int temp_size = psf->filelength - 31 ;
29637 psf_log_printf (psf, " Extended II : %d (SoX bug: should be %d)\n", size, temp_size) ;
29638 size = temp_size ;
29640 else
29641 psf_log_printf (psf, " Extended II : %d\n", size) ;
29643 psf_log_printf (psf, " sample rate : %d\n"
29644 " bit width : %d\n"
29645 " channels : %d\n", psf->sf.samplerate, bitwidth, channels) ;
29647 if (bitwidth == 16 && encoding == 0)
29648 { encoding = 4 ;
29649 psf_log_printf (psf, " encoding : 0 (SoX bug: should be 4 for 16 bit signed PCM)\n") ;
29651 else
29652 psf_log_printf (psf, " encoding : %d => %s\n", encoding, voc_encoding2str (encoding)) ;
29655 psf_log_printf (psf, " fourbytes : %X\n", fourbytes) ;
29657 psf->sf.channels = channels ;
29659 psf->dataoffset = offset ;
29660 psf->dataend = psf->filelength - 1 ;
29662 if (size + 31 == psf->filelength + 1)
29663 { /* Hack for reading files produced using
29664 ** sf_command (SFC_UPDATE_HEADER_NOW).
29666 psf_log_printf (psf, "Missing zero byte at end of file.\n") ;
29667 size = psf->filelength - 30 ;
29668 psf->dataend = 0 ;
29670 else if (size + 31 > psf->filelength)
29671 { psf_log_printf (psf, "Seems to be a truncated file.\n") ;
29672 size = psf->filelength - 31 ;
29674 else if (size + 31 < psf->filelength)
29675 psf_log_printf (psf, "Seems to be a multi-segment file (#3).\n") ;
29677 switch (encoding)
29678 { case 0 :
29679 psf->sf.format = SF_FORMAT_VOC | SF_FORMAT_PCM_U8 ;
29680 psf->bytewidth = 1 ;
29681 break ;
29683 case 4 :
29684 psf->sf.format = SF_FORMAT_VOC | SF_FORMAT_PCM_16 ;
29685 psf->bytewidth = 2 ;
29686 break ;
29688 case 6 :
29689 psf->sf.format = SF_FORMAT_VOC | SF_FORMAT_ALAW ;
29690 psf->bytewidth = 1 ;
29691 break ;
29693 case 7 :
29694 psf->sf.format = SF_FORMAT_VOC | SF_FORMAT_ULAW ;
29695 psf->bytewidth = 1 ;
29696 break ;
29698 default : /* Unknown */
29699 return SFE_UNKNOWN_FORMAT ;
29700 break ;
29705 return 0 ;
29706 } /* voc_read_header */
29708 /*====================================================================================
29711 static int
29712 voc_write_header (SF_PRIVATE *psf, int calc_length)
29713 { sf_count_t current ;
29714 int rate_const, subformat ;
29716 current = psf_ftell (psf) ;
29718 if (calc_length)
29719 { psf->filelength = psf_get_filelen (psf) ;
29721 psf->datalength = psf->filelength - psf->dataoffset ;
29722 if (psf->dataend)
29723 psf->datalength -= psf->filelength - psf->dataend ;
29725 psf->sf.frames = psf->datalength / (psf->bytewidth * psf->sf.channels) ;
29728 subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
29729 /* Reset the current header length to zero. */
29730 psf->header [0] = 0 ;
29731 psf->headindex = 0 ;
29732 psf_fseek (psf, 0, SEEK_SET) ;
29734 /* VOC marker and 0x1A byte. */
29735 psf_binheader_writef (psf, "eb1", "Creative Voice File", 19, 0x1A) ;
29737 /* Data offset, version and other. */
29738 psf_binheader_writef (psf, "e222", 26, 0x0114, 0x111F) ;
29740 /* Use same logic as SOX.
29741 ** If the file is mono 8 bit data, use VOC_SOUND_DATA.
29742 ** If the file is mono 16 bit data, use VOC_EXTENED.
29743 ** Otherwise use VOC_EXTENED_2.
29746 if (subformat == SF_FORMAT_PCM_U8 && psf->sf.channels == 1)
29747 { /* samplerate = 1000000 / (256 - rate_const) ; */
29748 rate_const = 256 - 1000000 / psf->sf.samplerate ;
29750 /* First type marker, length, rate_const and compression */
29751 psf_binheader_writef (psf, "e1311", VOC_SOUND_DATA, (int) (psf->datalength + 1), rate_const, 0) ;
29753 else if (subformat == SF_FORMAT_PCM_U8 && psf->sf.channels == 2)
29754 { /* sample_rate = 128000000 / (65536 - rate_short) ; */
29755 rate_const = 65536 - 128000000 / psf->sf.samplerate ;
29757 /* First write the VOC_EXTENDED section
29758 ** marker, length, rate_const and compression
29760 psf_binheader_writef (psf, "e13211", VOC_EXTENDED, 4, rate_const, 0, 1) ;
29762 /* samplerate = 1000000 / (256 - rate_const) ; */
29763 rate_const = 256 - 1000000 / psf->sf.samplerate ;
29765 /* Now write the VOC_SOUND_DATA section
29766 ** marker, length, rate_const and compression
29768 psf_binheader_writef (psf, "e1311", VOC_SOUND_DATA, (int) (psf->datalength + 1), rate_const, 0) ;
29770 else
29771 { int length ;
29773 if (psf->sf.channels < 1 || psf->sf.channels > 2)
29774 return SFE_CHANNEL_COUNT ;
29776 switch (subformat)
29777 { case SF_FORMAT_PCM_U8 :
29778 psf->bytewidth = 1 ;
29779 length = psf->sf.frames * psf->sf.channels * psf->bytewidth + 12 ;
29780 /* Marker, length, sample rate, bitwidth, stereo flag, encoding and fourt zero bytes. */
29781 psf_binheader_writef (psf, "e1341124", VOC_EXTENDED_II, length, psf->sf.samplerate, 16, psf->sf.channels, 4, 0) ;
29782 break ;
29784 case SF_FORMAT_PCM_16 :
29785 psf->bytewidth = 2 ;
29786 length = psf->sf.frames * psf->sf.channels * psf->bytewidth + 12 ;
29787 /* Marker, length, sample rate, bitwidth, stereo flag, encoding and fourt zero bytes. */
29788 psf_binheader_writef (psf, "e1341124", VOC_EXTENDED_II, length, psf->sf.samplerate, 16, psf->sf.channels, 4, 0) ;
29789 break ;
29791 case SF_FORMAT_ALAW :
29792 psf->bytewidth = 1 ;
29793 length = psf->sf.frames * psf->sf.channels * psf->bytewidth + 12 ;
29794 psf_binheader_writef (psf, "e1341124", VOC_EXTENDED_II, length, psf->sf.samplerate, 8, psf->sf.channels, 6, 0) ;
29795 break ;
29797 case SF_FORMAT_ULAW :
29798 psf->bytewidth = 1 ;
29799 length = psf->sf.frames * psf->sf.channels * psf->bytewidth + 12 ;
29800 psf_binheader_writef (psf, "e1341124", VOC_EXTENDED_II, length, psf->sf.samplerate, 8, psf->sf.channels, 7, 0) ;
29801 break ;
29803 default : return SFE_UNIMPLEMENTED ;
29807 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
29809 if (psf->error)
29810 return psf->error ;
29812 psf->dataoffset = psf->headindex ;
29814 if (current > 0)
29815 psf_fseek (psf, current, SEEK_SET) ;
29817 return psf->error ;
29818 } /* voc_write_header */
29820 static int
29821 voc_close (SF_PRIVATE *psf)
29823 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
29824 { /* Now we know for certain the length of the file we can re-write
29825 ** correct values for the FORM, 8SVX and BODY chunks.
29827 unsigned byte = VOC_TERMINATOR ;
29830 psf_fseek (psf, 0, SEEK_END) ;
29832 /* Write terminator */
29833 psf_fwrite (&byte, 1, 1, psf) ;
29835 voc_write_header (psf, SF_TRUE) ;
29838 return 0 ;
29839 } /* voc_close */
29841 static const char*
29842 voc_encoding2str (int encoding)
29844 switch (encoding)
29845 { case 0 : return "8 bit unsigned PCM" ;
29846 case 4 : return "16 bit signed PCM" ;
29847 case 6 : return "A-law" ;
29848 case 7 : return "u-law" ;
29849 default : break ;
29851 return "*** Unknown ***" ;
29852 } /* voc_encoding2str */
29854 /*====================================================================================
29857 #if 0
29858 static int
29859 voc_multi_init (SF_PRIVATE *psf, VOC_DATA *pvoc)
29861 psf->sf.frames = 0 ;
29863 if (pvoc->bitwidth == 8)
29864 { psf->read_short = voc_multi_read_uc2s ;
29865 psf->read_int = voc_multi_read_uc2i ;
29866 psf->read_float = voc_multi_read_uc2f ;
29867 psf->read_double = voc_multi_read_uc2d ;
29868 return 0 ;
29871 if (pvoc->bitwidth == 16)
29872 { psf->read_short = voc_multi_read_les2s ;
29873 psf->read_int = voc_multi_read_les2i ;
29874 psf->read_float = voc_multi_read_les2f ;
29875 psf->read_double = voc_multi_read_les2d ;
29876 return 0 ;
29879 psf_log_printf (psf, "Error : bitwith != 8 && bitwidth != 16.\n") ;
29881 return SFE_UNIMPLEMENTED ;
29882 } /* voc_multi_read_int */
29884 /*------------------------------------------------------------------------------------
29887 static int
29888 voc_multi_read_uc2s (SF_PRIVATE *psf, short *ptr, int len)
29891 return 0 ;
29892 } /* voc_multi_read_uc2s */
29894 static int
29895 voc_multi_read_les2s (SF_PRIVATE *psf, short *ptr, int len)
29898 return 0 ;
29899 } /* voc_multi_read_les2s */
29902 static int
29903 voc_multi_read_uc2i (SF_PRIVATE *psf, int *ptr, int len)
29906 return 0 ;
29907 } /* voc_multi_read_uc2i */
29909 static int
29910 voc_multi_read_les2i (SF_PRIVATE *psf, int *ptr, int len)
29913 return 0 ;
29914 } /* voc_multi_read_les2i */
29917 static int
29918 voc_multi_read_uc2f (SF_PRIVATE *psf, float *ptr, int len)
29921 return 0 ;
29922 } /* voc_multi_read_uc2f */
29924 static int
29925 voc_multi_read_les2f (SF_PRIVATE *psf, float *ptr, int len)
29928 return 0 ;
29929 } /* voc_multi_read_les2f */
29932 static int
29933 voc_multi_read_uc2d (SF_PRIVATE *psf, double *ptr, int len)
29936 return 0 ;
29937 } /* voc_multi_read_uc2d */
29939 static int
29940 voc_multi_read_les2d (SF_PRIVATE *psf, double *ptr, int len)
29943 return 0 ;
29944 } /* voc_multi_read_les2d */
29946 #endif
29948 /*------------------------------------------------------------------------------------
29950 Creative Voice (VOC) file format
29951 --------------------------------
29953 ~From: galt@dsd.es.com
29955 (byte numbers are hex!)
29957 HEADER (bytes 00-19)
29958 Series of DATA BLOCKS (bytes 1A+) [Must end w/ Terminator Block]
29960 - ---------------------------------------------------------------
29962 HEADER:
29963 =======
29964 byte # Description
29965 ------ ------------------------------------------
29966 00-12 "Creative Voice File"
29967 13 1A (eof to abort printing of file)
29968 14-15 Offset of first datablock in .voc file (std 1A 00
29969 in Intel Notation)
29970 16-17 Version number (minor,major) (VOC-HDR puts 0A 01)
29971 18-19 1's Comp of Ver. # + 1234h (VOC-HDR puts 29 11)
29973 - ---------------------------------------------------------------
29975 DATA BLOCK:
29976 ===========
29978 Data Block: TYPE(1-byte), SIZE(3-bytes), INFO(0+ bytes)
29979 NOTE: Terminator Block is an exception -- it has only the TYPE byte.
29981 TYPE Description Size (3-byte int) Info
29982 ---- ----------- ----------------- -----------------------
29983 00 Terminator (NONE) (NONE)
29984 01 Sound data 2+length of data *
29985 02 Sound continue length of data Voice Data
29986 03 Silence 3 **
29987 04 Marker 2 Marker# (2 bytes)
29988 05 ASCII length of string null terminated string
29989 06 Repeat 2 Count# (2 bytes)
29990 07 End repeat 0 (NONE)
29991 08 Extended 4 ***
29993 *Sound Info Format:
29994 ---------------------
29995 00 Sample Rate
29996 01 Compression Type
29997 02+ Voice Data
29999 **Silence Info Format:
30000 ----------------------------
30001 00-01 Length of silence - 1
30002 02 Sample Rate
30005 ***Extended Info Format:
30006 ---------------------
30007 00-01 Time Constant: Mono: 65536 - (256000000/sample_rate)
30008 Stereo: 65536 - (25600000/(2*sample_rate))
30009 02 Pack
30010 03 Mode: 0 = mono
30011 1 = stereo
30014 Marker# -- Driver keeps the most recent marker in a status byte
30015 Count# -- Number of repetitions + 1
30016 Count# may be 1 to FFFE for 0 - FFFD repetitions
30017 or FFFF for endless repetitions
30018 Sample Rate -- SR byte = 256-(1000000/sample_rate)
30019 Length of silence -- in units of sampling cycle
30020 Compression Type -- of voice data
30021 8-bits = 0
30022 4-bits = 1
30023 2.6-bits = 2
30024 2-bits = 3
30025 Multi DAC = 3+(# of channels) [interesting--
30026 this isn't in the developer's manual]
30029 ---------------------------------------------------------------------------------
30030 Addendum submitted by Votis Kokavessis:
30032 After some experimenting with .VOC files I found out that there is a Data Block
30033 Type 9, which is not covered in the VOC.TXT file. Here is what I was able to discover
30034 about this block type:
30037 TYPE: 09
30038 SIZE: 12 + length of data
30039 INFO: 12 (twelve) bytes
30041 INFO STRUCTURE:
30043 Bytes 0-1: (Word) Sample Rate (e.g. 44100)
30044 Bytes 2-3: zero (could be that bytes 0-3 are a DWord for Sample Rate)
30045 Byte 4: Sample Size in bits (e.g. 16)
30046 Byte 5: Number of channels (e.g. 1 for mono, 2 for stereo)
30047 Byte 6: Unknown (equal to 4 in all files I examined)
30048 Bytes 7-11: zero
30051 -------------------------------------------------------------------------------------*/
30053 /*=====================================================================================
30054 **=====================================================================================
30055 **=====================================================================================
30056 **=====================================================================================
30059 /*------------------------------------------------------------------------
30060 The following is taken from the Audio File Formats FAQ dated 2-Jan-1995
30061 and submitted by Guido van Rossum <guido@cwi.nl>.
30062 --------------------------------------------------------------------------
30063 Creative Voice (VOC) file format
30064 --------------------------------
30066 From: galt@dsd.es.com
30068 (byte numbers are hex!)
30070 HEADER (bytes 00-19)
30071 Series of DATA BLOCKS (bytes 1A+) [Must end w/ Terminator Block]
30073 - ---------------------------------------------------------------
30075 HEADER:
30076 -------
30077 byte # Description
30078 ------ ------------------------------------------
30079 00-12 "Creative Voice File"
30080 13 1A (eof to abort printing of file)
30081 14-15 Offset of first datablock in .voc file (std 1A 00
30082 in Intel Notation)
30083 16-17 Version number (minor,major) (VOC-HDR puts 0A 01)
30084 18-19 2's Comp of Ver. # + 1234h (VOC-HDR puts 29 11)
30086 - ---------------------------------------------------------------
30088 DATA BLOCK:
30089 -----------
30091 Data Block: TYPE(1-byte), SIZE(3-bytes), INFO(0+ bytes)
30092 NOTE: Terminator Block is an exception -- it has only the TYPE byte.
30094 TYPE Description Size (3-byte int) Info
30095 ---- ----------- ----------------- -----------------------
30096 00 Terminator (NONE) (NONE)
30097 01 Sound data 2+length of data *
30098 02 Sound continue length of data Voice Data
30099 03 Silence 3 **
30100 04 Marker 2 Marker# (2 bytes)
30101 05 ASCII length of string null terminated string
30102 06 Repeat 2 Count# (2 bytes)
30103 07 End repeat 0 (NONE)
30104 08 Extended 4 ***
30106 *Sound Info Format: **Silence Info Format:
30107 --------------------- ----------------------------
30108 00 Sample Rate 00-01 Length of silence - 1
30109 01 Compression Type 02 Sample Rate
30110 02+ Voice Data
30112 ***Extended Info Format:
30113 ---------------------
30114 00-01 Time Constant: Mono: 65536 - (256000000/sample_rate)
30115 Stereo: 65536 - (25600000/(2*sample_rate))
30116 02 Pack
30117 03 Mode: 0 = mono
30118 1 = stereo
30121 Marker# -- Driver keeps the most recent marker in a status byte
30122 Count# -- Number of repetitions + 1
30123 Count# may be 1 to FFFE for 0 - FFFD repetitions
30124 or FFFF for endless repetitions
30125 Sample Rate -- SR byte = 256-(1000000/sample_rate)
30126 Length of silence -- in units of sampling cycle
30127 Compression Type -- of voice data
30128 8-bits = 0
30129 4-bits = 1
30130 2.6-bits = 2
30131 2-bits = 3
30132 Multi DAC = 3+(# of channels) [interesting--
30133 this isn't in the developer's manual]
30135 Detailed description of new data blocks (VOC files version 1.20 and above):
30137 (Source is fax from Barry Boone at Creative Labs, 405/742-6622)
30139 BLOCK 8 - digitized sound attribute extension, must preceed block 1.
30140 Used to define stereo, 8 bit audio
30141 BYTE bBlockID; // = 8
30142 BYTE nBlockLen[3]; // 3 byte length
30143 WORD wTimeConstant; // time constant = same as block 1
30144 BYTE bPackMethod; // same as in block 1
30145 BYTE bVoiceMode; // 0-mono, 1-stereo
30147 Data is stored left, right
30149 BLOCK 9 - data block that supersedes blocks 1 and 8.
30150 Used for stereo, 16 bit.
30152 BYTE bBlockID; // = 9
30153 BYTE nBlockLen[3]; // length 12 plus length of sound
30154 DWORD dwSamplesPerSec; // samples per second, not time const.
30155 BYTE bBitsPerSample; // e.g., 8 or 16
30156 BYTE bChannels; // 1 for mono, 2 for stereo
30157 WORD wFormat; // see below
30158 BYTE reserved[4]; // pad to make block w/o data
30159 // have a size of 16 bytes
30161 Valid values of wFormat are:
30163 0x0000 8-bit unsigned PCM
30164 0x0001 Creative 8-bit to 4-bit ADPCM
30165 0x0002 Creative 8-bit to 3-bit ADPCM
30166 0x0003 Creative 8-bit to 2-bit ADPCM
30167 0x0004 16-bit signed PCM
30168 0x0006 CCITT a-Law
30169 0x0007 CCITT u-Law
30170 0x02000 Creative 16-bit to 4-bit ADPCM
30172 Data is stored left, right
30174 ------------------------------------------------------------------------*/
30176 ** Do not edit or modify anything in this comment block.
30177 ** The arch-tag line is a file identity tag for the GNU Arch
30178 ** revision control system.
30180 ** arch-tag: 40a50167-a81c-463a-9e1d-3282ff84e09d
30183 ** Copyright (C) 2002-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
30185 ** This program is free software; you can redistribute it and/or modify
30186 ** it under the terms of the GNU Lesser General Public License as published by
30187 ** the Free Software Foundation; either version 2.1 of the License, or
30188 ** (at your option) any later version.
30190 ** This program is distributed in the hope that it will be useful,
30191 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
30192 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30193 ** GNU Lesser General Public License for more details.
30195 ** You should have received a copy of the GNU Lesser General Public License
30196 ** along with this program; if not, write to the Free Software
30197 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30201 ** This is the OKI / Dialogic ADPCM encoder/decoder. It converts from
30202 ** 12 bit linear sample data to a 4 bit ADPCM.
30204 ** Implemented from the description found here:
30206 ** http://www.comptek.ru:8100/telephony/tnotes/tt1-13.html
30208 ** and compared against the encoder/decoder found here:
30210 ** http://ibiblio.org/pub/linux/apps/sound/convert/vox.tar.gz
30213 #include <stdio.h>
30214 #include <stdlib.h>
30215 #include <string.h>
30218 #define VOX_DATA_LEN 2048
30219 #define PCM_DATA_LEN (VOX_DATA_LEN *2)
30221 typedef struct
30222 { short last ;
30223 short step_index ;
30225 int vox_bytes, pcm_samples ;
30227 unsigned char vox_data [VOX_DATA_LEN] ;
30228 short pcm_data [PCM_DATA_LEN] ;
30229 } VOX_ADPCM_PRIVATE ;
30231 static int vox_adpcm_encode_block (VOX_ADPCM_PRIVATE *pvox) ;
30232 static int vox_adpcm_decode_block (VOX_ADPCM_PRIVATE *pvox) ;
30234 static short vox_adpcm_decode (char code, VOX_ADPCM_PRIVATE *pvox) ;
30235 static char vox_adpcm_encode (short samp, VOX_ADPCM_PRIVATE *pvox) ;
30237 static sf_count_t vox_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
30238 static sf_count_t vox_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
30239 static sf_count_t vox_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
30240 static sf_count_t vox_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
30242 static sf_count_t vox_write_s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
30243 static sf_count_t vox_write_i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
30244 static sf_count_t vox_write_f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
30245 static sf_count_t vox_write_d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
30247 static int vox_read_block (SF_PRIVATE *psf, VOX_ADPCM_PRIVATE *pvox, short *ptr, int len) ;
30248 static int vox_write_block (SF_PRIVATE *psf, VOX_ADPCM_PRIVATE *pvox, short *ptr, int len) ;
30250 /*============================================================================================
30251 ** Predefined OKI ADPCM encoder/decoder tables.
30254 static short stepvox_adpcm_size_table [49] =
30255 { 16, 17, 19, 21, 23, 25, 28, 31, 34, 37, 41, 45, 50, 55, 60,
30256 66, 73, 80, 88, 97, 107, 118, 130, 143, 157, 173, 190, 209,
30257 230, 253, 279, 307, 337, 371, 408, 449, 494, 544, 598, 658,
30258 724, 796, 876, 963, 1060, 1166, 1282, 1408, 1552
30259 } ; /* stepvox_adpcm_size_table */
30261 static short stepvox_adpcm_adjust_table [8] =
30262 { -1, -1, -1, -1, 2, 4, 6, 8
30263 } ; /* stepvox_adpcm_adjust_table */
30265 /*------------------------------------------------------------------------------
30269 vox_adpcm_init (SF_PRIVATE *psf)
30270 { VOX_ADPCM_PRIVATE *pvox = NULL ;
30272 if (psf->mode == SFM_RDWR)
30273 return SFE_BAD_MODE_RW ;
30275 if (psf->mode == SFM_WRITE && psf->sf.channels != 1)
30276 return SFE_CHANNEL_COUNT ;
30278 if ((pvox = malloc (sizeof (VOX_ADPCM_PRIVATE))) == NULL)
30279 return SFE_MALLOC_FAILED ;
30281 psf->fdata = (void*) pvox ;
30282 memset (pvox, 0, sizeof (VOX_ADPCM_PRIVATE)) ;
30284 if (psf->mode == SFM_WRITE)
30285 { psf->write_short = vox_write_s ;
30286 psf->write_int = vox_write_i ;
30287 psf->write_float = vox_write_f ;
30288 psf->write_double = vox_write_d ;
30290 else
30291 { psf_log_printf (psf, "Header-less OKI Dialogic ADPCM encoded file.\n") ;
30292 psf_log_printf (psf, "Setting up for 8kHz, mono, Vox ADPCM.\n") ;
30294 psf->read_short = vox_read_s ;
30295 psf->read_int = vox_read_i ;
30296 psf->read_float = vox_read_f ;
30297 psf->read_double = vox_read_d ;
30300 /* Standard sample rate chennels etc. */
30301 if (psf->sf.samplerate < 1)
30302 psf->sf.samplerate = 8000 ;
30303 psf->sf.channels = 1 ;
30305 psf->sf.frames = psf->filelength * 2 ;
30307 psf->sf.seekable = SF_FALSE ;
30309 /* Seek back to start of data. */
30310 if (psf_fseek (psf, 0 , SEEK_SET) == -1)
30311 return SFE_BAD_SEEK ;
30313 return 0 ;
30314 } /* vox_adpcm_init */
30316 /*------------------------------------------------------------------------------
30319 static char
30320 vox_adpcm_encode (short samp, VOX_ADPCM_PRIVATE *pvox)
30321 { short code ;
30322 short diff, error, stepsize ;
30324 stepsize = stepvox_adpcm_size_table [pvox->step_index] ;
30325 code = 0 ;
30327 diff = samp - pvox->last ;
30328 if (diff < 0)
30329 { code = 0x08 ;
30330 error = -diff ;
30332 else
30333 error = diff ;
30335 if (error >= stepsize)
30336 { code = code | 0x04 ;
30337 error -= stepsize ;
30340 if (error >= stepsize / 2)
30341 { code = code | 0x02 ;
30342 error -= stepsize / 2 ;
30345 if (error >= stepsize / 4)
30346 code = code | 0x01 ;
30349 ** To close the feedback loop, the deocder is used to set the
30350 ** estimate of last sample and in doing so, also set the step_index.
30352 pvox->last = vox_adpcm_decode (code, pvox) ;
30354 return code ;
30355 } /* vox_adpcm_encode */
30357 static short
30358 vox_adpcm_decode (char code, VOX_ADPCM_PRIVATE *pvox)
30359 { short diff, error, stepsize, samp ;
30361 stepsize = stepvox_adpcm_size_table [pvox->step_index] ;
30363 error = stepsize / 8 ;
30365 if (code & 0x01)
30366 error += stepsize / 4 ;
30368 if (code & 0x02)
30369 error += stepsize / 2 ;
30371 if (code & 0x04)
30372 error += stepsize ;
30374 diff = (code & 0x08) ? -error : error ;
30375 samp = pvox->last + diff ;
30378 ** Apply clipping.
30380 if (samp > 2048)
30381 samp = 2048 ;
30382 if (samp < -2048)
30383 samp = -2048 ;
30385 pvox->last = samp ;
30386 pvox->step_index += stepvox_adpcm_adjust_table [code & 0x7] ;
30388 if (pvox->step_index < 0)
30389 pvox->step_index = 0 ;
30390 if (pvox->step_index > 48)
30391 pvox->step_index = 48 ;
30393 return samp ;
30394 } /* vox_adpcm_decode */
30396 static int
30397 vox_adpcm_encode_block (VOX_ADPCM_PRIVATE *pvox)
30398 { unsigned char code ;
30399 int j, k ;
30401 /* If data_count is odd, add an extra zero valued sample. */
30402 if (pvox->pcm_samples & 1)
30403 pvox->pcm_data [pvox->pcm_samples++] = 0 ;
30405 for (j = k = 0 ; k < pvox->pcm_samples ; j++)
30406 { code = vox_adpcm_encode (pvox->pcm_data [k++] / 16, pvox) << 4 ;
30407 code |= vox_adpcm_encode (pvox->pcm_data [k++] / 16, pvox) ;
30408 pvox->vox_data [j] = code ;
30411 pvox->vox_bytes = j ;
30413 return 0 ;
30414 } /* vox_adpcm_encode_block */
30416 static int
30417 vox_adpcm_decode_block (VOX_ADPCM_PRIVATE *pvox)
30418 { unsigned char code ;
30419 int j, k ;
30421 for (j = k = 0 ; j < pvox->vox_bytes ; j++)
30422 { code = pvox->vox_data [j] ;
30423 pvox->pcm_data [k++] = 16 * vox_adpcm_decode ((code >> 4) & 0x0f, pvox) ;
30424 pvox->pcm_data [k++] = 16 * vox_adpcm_decode (code & 0x0f, pvox) ;
30427 pvox->pcm_samples = k ;
30429 return 0 ;
30430 } /* vox_adpcm_decode_block */
30432 /*==============================================================================
30435 static int
30436 vox_read_block (SF_PRIVATE *psf, VOX_ADPCM_PRIVATE *pvox, short *ptr, int len)
30437 { int indx = 0, k ;
30439 while (indx < len)
30440 { pvox->vox_bytes = (len - indx > PCM_DATA_LEN) ? VOX_DATA_LEN : (len - indx + 1) / 2 ;
30442 if ((k = psf_fread (pvox->vox_data, 1, pvox->vox_bytes, psf)) != pvox->vox_bytes)
30443 { if (psf_ftell (psf) + k != psf->filelength)
30444 psf_log_printf (psf, "*** Warning : short read (%d != %d).\n", k, pvox->vox_bytes) ;
30445 if (k == 0)
30446 break ;
30449 pvox->vox_bytes = k ;
30451 vox_adpcm_decode_block (pvox) ;
30453 memcpy (&(ptr [indx]), pvox->pcm_data, pvox->pcm_samples * sizeof (short)) ;
30454 indx += pvox->pcm_samples ;
30457 return indx ;
30458 } /* vox_read_block */
30461 static sf_count_t
30462 vox_read_s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
30463 { VOX_ADPCM_PRIVATE *pvox ;
30464 int readcount, count ;
30465 sf_count_t total = 0 ;
30467 if (! psf->fdata)
30468 return 0 ;
30469 pvox = (VOX_ADPCM_PRIVATE*) psf->fdata ;
30471 while (len > 0)
30472 { readcount = (len > 0x10000000) ? 0x10000000 : (int) len ;
30474 count = vox_read_block (psf, pvox, ptr, readcount) ;
30476 total += count ;
30477 len -= count ;
30478 if (count != readcount)
30479 break ;
30482 return total ;
30483 } /* vox_read_s */
30485 static sf_count_t
30486 vox_read_i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
30487 { VOX_ADPCM_PRIVATE *pvox ;
30488 short *sptr ;
30489 int k, bufferlen, readcount, count ;
30490 sf_count_t total = 0 ;
30492 if (! psf->fdata)
30493 return 0 ;
30494 pvox = (VOX_ADPCM_PRIVATE*) psf->fdata ;
30496 sptr = (short*) psf->buffer ;
30497 bufferlen = SF_BUFFER_LEN / sizeof (short) ;
30498 while (len > 0)
30499 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
30500 count = vox_read_block (psf, pvox, sptr, readcount) ;
30501 for (k = 0 ; k < readcount ; k++)
30502 ptr [total + k] = ((int) sptr [k]) << 16 ;
30503 total += count ;
30504 len -= readcount ;
30505 if (count != readcount)
30506 break ;
30509 return total ;
30510 } /* vox_read_i */
30512 static sf_count_t
30513 vox_read_f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
30514 { VOX_ADPCM_PRIVATE *pvox ;
30515 short *sptr ;
30516 int k, bufferlen, readcount, count ;
30517 sf_count_t total = 0 ;
30518 float normfact ;
30520 if (! psf->fdata)
30521 return 0 ;
30522 pvox = (VOX_ADPCM_PRIVATE*) psf->fdata ;
30524 normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x8000) : 1.0 ;
30526 sptr = (short*) psf->buffer ;
30527 bufferlen = SF_BUFFER_LEN / sizeof (short) ;
30528 while (len > 0)
30529 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
30530 count = vox_read_block (psf, pvox, sptr, readcount) ;
30531 for (k = 0 ; k < readcount ; k++)
30532 ptr [total + k] = normfact * (float) (sptr [k]) ;
30533 total += count ;
30534 len -= readcount ;
30535 if (count != readcount)
30536 break ;
30539 return total ;
30540 } /* vox_read_f */
30542 static sf_count_t
30543 vox_read_d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
30544 { VOX_ADPCM_PRIVATE *pvox ;
30545 short *sptr ;
30546 int k, bufferlen, readcount, count ;
30547 sf_count_t total = 0 ;
30548 double normfact ;
30550 if (! psf->fdata)
30551 return 0 ;
30552 pvox = (VOX_ADPCM_PRIVATE*) psf->fdata ;
30554 normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x8000) : 1.0 ;
30556 sptr = (short*) psf->buffer ;
30557 bufferlen = SF_BUFFER_LEN / sizeof (short) ;
30558 while (len > 0)
30559 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
30560 count = vox_read_block (psf, pvox, sptr, readcount) ;
30561 for (k = 0 ; k < readcount ; k++)
30562 ptr [total + k] = normfact * (double) (sptr [k]) ;
30563 total += count ;
30564 len -= readcount ;
30565 if (count != readcount)
30566 break ;
30569 return total ;
30570 } /* vox_read_d */
30572 /*------------------------------------------------------------------------------
30575 static int
30576 vox_write_block (SF_PRIVATE *psf, VOX_ADPCM_PRIVATE *pvox, short *ptr, int len)
30577 { int indx = 0, k ;
30579 while (indx < len)
30580 { pvox->pcm_samples = (len - indx > PCM_DATA_LEN) ? PCM_DATA_LEN : len - indx ;
30582 memcpy (pvox->pcm_data, &(ptr [indx]), pvox->pcm_samples * sizeof (short)) ;
30584 vox_adpcm_encode_block (pvox) ;
30586 if ((k = psf_fwrite (pvox->vox_data, 1, pvox->vox_bytes, psf)) != pvox->vox_bytes)
30587 psf_log_printf (psf, "*** Warning : short read (%d != %d).\n", k, pvox->vox_bytes) ;
30589 indx += pvox->pcm_samples ;
30592 return indx ;
30593 } /* vox_write_block */
30595 static sf_count_t
30596 vox_write_s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
30597 { VOX_ADPCM_PRIVATE *pvox ;
30598 int writecount, count ;
30599 sf_count_t total = 0 ;
30601 if (! psf->fdata)
30602 return 0 ;
30603 pvox = (VOX_ADPCM_PRIVATE*) psf->fdata ;
30605 while (len)
30606 { writecount = (len > 0x10000000) ? 0x10000000 : (int) len ;
30608 count = vox_write_block (psf, pvox, ptr, writecount) ;
30610 total += count ;
30611 len -= count ;
30612 if (count != writecount)
30613 break ;
30616 return total ;
30617 } /* vox_write_s */
30619 static sf_count_t
30620 vox_write_i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
30621 { VOX_ADPCM_PRIVATE *pvox ;
30622 short *sptr ;
30623 int k, bufferlen, writecount, count ;
30624 sf_count_t total = 0 ;
30626 if (! psf->fdata)
30627 return 0 ;
30628 pvox = (VOX_ADPCM_PRIVATE*) psf->fdata ;
30630 sptr = (short*) psf->buffer ;
30631 bufferlen = SF_BUFFER_LEN / sizeof (short) ;
30632 while (len > 0)
30633 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
30634 for (k = 0 ; k < writecount ; k++)
30635 sptr [k] = ptr [total + k] >> 16 ;
30636 count = vox_write_block (psf, pvox, sptr, writecount) ;
30637 total += count ;
30638 len -= writecount ;
30639 if (count != writecount)
30640 break ;
30643 return total ;
30644 } /* vox_write_i */
30646 static sf_count_t
30647 vox_write_f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
30648 { VOX_ADPCM_PRIVATE *pvox ;
30649 short *sptr ;
30650 int k, bufferlen, writecount, count ;
30651 sf_count_t total = 0 ;
30652 float normfact ;
30654 if (! psf->fdata)
30655 return 0 ;
30656 pvox = (VOX_ADPCM_PRIVATE*) psf->fdata ;
30658 normfact = (psf->norm_float == SF_TRUE) ? (1.0 * 0x7FFF) : 1.0 ;
30660 sptr = (short*) psf->buffer ;
30661 bufferlen = SF_BUFFER_LEN / sizeof (short) ;
30662 while (len > 0)
30663 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
30664 for (k = 0 ; k < writecount ; k++)
30665 sptr [k] = lrintf (normfact * ptr [total + k]) ;
30666 count = vox_write_block (psf, pvox, sptr, writecount) ;
30667 total += count ;
30668 len -= writecount ;
30669 if (count != writecount)
30670 break ;
30673 return total ;
30674 } /* vox_write_f */
30676 static sf_count_t
30677 vox_write_d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
30678 { VOX_ADPCM_PRIVATE *pvox ;
30679 short *sptr ;
30680 int k, bufferlen, writecount, count ;
30681 sf_count_t total = 0 ;
30682 double normfact ;
30684 if (! psf->fdata)
30685 return 0 ;
30686 pvox = (VOX_ADPCM_PRIVATE*) psf->fdata ;
30688 normfact = (psf->norm_double == SF_TRUE) ? (1.0 * 0x7FFF) : 1.0 ;
30690 sptr = (short*) psf->buffer ;
30691 bufferlen = SF_BUFFER_LEN / sizeof (short) ;
30692 while (len > 0)
30693 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
30694 for (k = 0 ; k < writecount ; k++)
30695 sptr [k] = lrint (normfact * ptr [total + k]) ;
30696 count = vox_write_block (psf, pvox, sptr, writecount) ;
30697 total += count ;
30698 len -= writecount ;
30699 if (count != writecount)
30700 break ;
30703 return total ;
30704 } /* vox_write_d */
30708 ** Do not edit or modify anything in this comment block.
30709 ** The arch-tag line is a file identity tag for the GNU Arch
30710 ** revision control system.
30712 ** arch-tag: e15e97fe-ff9d-4b46-a489-7059fb2d0b1e
30715 ** Copyright (C) 1999-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
30717 ** This program is free software; you can redistribute it and/or modify
30718 ** it under the terms of the GNU Lesser General Public License as published by
30719 ** the Free Software Foundation; either version 2.1 of the License, or
30720 ** (at your option) any later version.
30722 ** This program is distributed in the hope that it will be useful,
30723 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
30724 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30725 ** GNU Lesser General Public License for more details.
30727 ** You should have received a copy of the GNU Lesser General Public License
30728 ** along with this program; if not, write to the Free Software
30729 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30732 #include <stdio.h>
30733 #include <string.h>
30734 #include <ctype.h>
30735 #include <time.h>
30738 /*------------------------------------------------------------------------------
30739 ** W64 files use 16 byte markers as opposed to the four byte marker of
30740 ** WAV files.
30741 ** For comparison purposes, an integer is required, so make an integer
30742 ** hash for the 16 bytes using MAKE_HASH16 macro, but also create a 16
30743 ** byte array containing the complete 16 bytes required when writing the
30744 ** header.
30747 #define MAKE_HASH16(x0,x1,x2,x3,x4,x5,x6,x7,x8,x9,xa,xb,xc,xd,xe,xf) \
30748 ( (x0) ^ ((x1) << 1) ^ ((x2) << 2) ^ ((x3) << 3) ^ \
30749 ((x4) << 4) ^ ((x5) << 5) ^ ((x6) << 6) ^ ((x7) << 7) ^ \
30750 ((x8) << 8) ^ ((x9) << 9) ^ ((xa) << 10) ^ ((xb) << 11) ^ \
30751 ((xc) << 12) ^ ((xd) << 13) ^ ((xe) << 14) ^ ((xf) << 15) )
30753 #define MAKE_MARKER16(name,x0,x1,x2,x3,x4,x5,x6,x7,x8,x9,xa,xb,xc,xd,xe,xf) \
30754 static unsigned char name [16] = { (x0), (x1), (x2), (x3), (x4), (x5), \
30755 (x6), (x7), (x8), (x9), (xa), (xb), (xc), (xd), (xe), (xf) }
30757 #define riff_HASH16 MAKE_HASH16 ('r', 'i', 'f', 'f', 0x2E, 0x91, 0xCF, 0x11, 0xA5, \
30758 0xD6, 0x28, 0xDB, 0x04, 0xC1, 0x00, 0x00)
30760 #define wave_HASH16 MAKE_HASH16 ('w', 'a', 'v', 'e', 0xF3, 0xAC, 0xD3, 0x11, \
30761 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A)
30763 #define fmt_HASH16 MAKE_HASH16 ('f', 'm', 't', ' ', 0xF3, 0xAC, 0xD3, 0x11, \
30764 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A)
30766 #define fact_HASH16 MAKE_HASH16 ('f', 'a', 'c', 't', 0xF3, 0xAC, 0xD3, 0x11, \
30767 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A)
30769 #define data_HASH16 MAKE_HASH16 ('d', 'a', 't', 'a', 0xF3, 0xAC, 0xD3, 0x11, \
30770 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A)
30772 #define ACID_HASH16 MAKE_HASH16 (0x6D, 0x07, 0x1C, 0xEA, 0xA3, 0xEF, 0x78, 0x4C, \
30773 0x90, 0x57, 0x7F, 0x79, 0xEE, 0x25, 0x2A, 0xAE)
30775 MAKE_MARKER16 (riff_MARKER16, 'r', 'i', 'f', 'f', 0x2E, 0x91, 0xCF, 0x11,
30776 0xA5, 0xD6, 0x28, 0xDB, 0x04, 0xC1, 0x00, 0x00) ;
30779 MAKE_MARKER16 (wave_MARKER16, 'w', 'a', 'v', 'e', 0xF3, 0xAC, 0xD3, 0x11,
30780 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A) ;
30782 MAKE_MARKER16 (fmt_MARKER16, 'f', 'm', 't', ' ', 0xF3, 0xAC, 0xD3, 0x11,
30783 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A) ;
30785 MAKE_MARKER16 (fact_MARKER16, 'f', 'a', 'c', 't', 0xF3, 0xAC, 0xD3, 0x11,
30786 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A) ;
30788 MAKE_MARKER16 (data_MARKER16, 'd', 'a', 't', 'a', 0xF3, 0xAC, 0xD3, 0x11,
30789 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A) ;
30791 enum
30792 { HAVE_riff = 0x01,
30793 HAVE_wave = 0x02,
30794 w64HAVE_fmt = 0x04,
30795 w64HAVE_fact = 0x08,
30796 w64HAVE_data = 0x20
30799 /*------------------------------------------------------------------------------
30800 * Private static functions.
30803 static int w64_read_header (SF_PRIVATE *psf, int *blockalign, int *framesperblock) ;
30804 static int w64_write_header (SF_PRIVATE *psf, int calc_length) ;
30805 static int w64_close (SF_PRIVATE *psf) ;
30807 /*------------------------------------------------------------------------------
30808 ** Public function.
30812 w64_open (SF_PRIVATE *psf)
30813 { int subformat, error, blockalign = 0, framesperblock = 0 ;
30815 if (psf->mode == SFM_READ || (psf->mode == SFM_RDWR &&psf->filelength > 0))
30816 { if ((error = w64_read_header (psf, &blockalign, &framesperblock)))
30817 return error ;
30820 if ((psf->sf.format & SF_FORMAT_TYPEMASK) != SF_FORMAT_W64)
30821 return SFE_BAD_OPEN_FORMAT ;
30823 subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
30825 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
30826 { if (psf->is_pipe)
30827 return SFE_NO_PIPE_WRITE ;
30829 psf->endian = SF_ENDIAN_LITTLE ; /* All W64 files are little endian. */
30831 psf->blockwidth = psf->bytewidth * psf->sf.channels ;
30833 if (subformat == SF_FORMAT_IMA_ADPCM || subformat == SF_FORMAT_MS_ADPCM)
30834 { blockalign = wav_w64_srate2blocksize (psf->sf.samplerate * psf->sf.channels) ;
30835 framesperblock = -1 ;
30837 /* FIXME : This block must go */
30838 psf->filelength = SF_COUNT_MAX ;
30839 psf->datalength = psf->filelength ;
30840 if (psf->sf.frames <= 0)
30841 psf->sf.frames = (psf->blockwidth) ? psf->filelength / psf->blockwidth : psf->filelength ;
30842 /* EMXIF : This block must go */
30845 if ((error = w64_write_header (psf, SF_FALSE)))
30846 return error ;
30848 psf->write_header = w64_write_header ;
30851 psf->close = w64_close ;
30853 switch (subformat)
30854 { case SF_FORMAT_PCM_U8 :
30855 error = pcm_init (psf) ;
30856 break ;
30858 case SF_FORMAT_PCM_16 :
30859 case SF_FORMAT_PCM_24 :
30860 case SF_FORMAT_PCM_32 :
30861 error = pcm_init (psf) ;
30862 break ;
30864 case SF_FORMAT_ULAW :
30865 error = ulaw_init (psf) ;
30866 break ;
30868 case SF_FORMAT_ALAW :
30869 error = alaw_init (psf) ;
30870 break ;
30872 /* Lite remove start */
30873 case SF_FORMAT_FLOAT :
30874 error = float32_init (psf) ;
30875 break ;
30877 case SF_FORMAT_DOUBLE :
30878 error = double64_init (psf) ;
30879 break ;
30881 case SF_FORMAT_IMA_ADPCM :
30882 error = wav_w64_ima_init (psf, blockalign, framesperblock) ;
30883 break ;
30885 case SF_FORMAT_MS_ADPCM :
30886 error = wav_w64_msadpcm_init (psf, blockalign, framesperblock) ;
30887 break ;
30888 /* Lite remove end */
30890 case SF_FORMAT_GSM610 :
30891 error = gsm610_init (psf) ;
30892 break ;
30894 default : return SFE_UNIMPLEMENTED ;
30897 return error ;
30898 } /* w64_open */
30900 /*=========================================================================
30901 ** Private functions.
30904 static int
30905 w64_read_header (SF_PRIVATE *psf, int *blockalign, int *framesperblock)
30906 { WAV_FMT wav_fmt ;
30907 int dword = 0, marker, format = 0 ;
30908 sf_count_t chunk_size, bytesread = 0 ;
30909 int parsestage = 0, error, done = 0 ;
30911 /* Set position to start of file to begin reading header. */
30912 psf_binheader_readf (psf, "p", 0) ;
30914 while (! done)
30915 { /* Read the 4 byte marker and jump 12 bytes. */
30916 bytesread += psf_binheader_readf (psf, "h", &marker) ;
30917 chunk_size = 0 ;
30919 switch (marker)
30920 { case riff_HASH16 :
30921 if (parsestage)
30922 return SFE_W64_NO_RIFF ;
30924 bytesread += psf_binheader_readf (psf, "e8", &chunk_size) ;
30926 if (psf->filelength < chunk_size)
30927 psf_log_printf (psf, "riff : %D (should be %D)\n", chunk_size, psf->filelength) ;
30928 else
30929 psf_log_printf (psf, "riff : %D\n", chunk_size) ;
30931 parsestage |= HAVE_riff ;
30932 break ;
30934 case ACID_HASH16:
30935 psf_log_printf (psf, "Looks like an ACID file. Exiting.\n") ;
30936 return SFE_UNIMPLEMENTED ;
30938 case wave_HASH16 :
30939 if ((parsestage & HAVE_riff) != HAVE_riff)
30940 return SFE_W64_NO_WAVE ;
30941 psf_log_printf (psf, "wave\n") ;
30942 parsestage |= HAVE_wave ;
30943 break ;
30945 case fmt_HASH16 :
30946 if ((parsestage & (HAVE_riff | HAVE_wave)) != (HAVE_riff | HAVE_wave))
30947 return SFE_W64_NO_FMT ;
30949 bytesread += psf_binheader_readf (psf, "e8", &chunk_size) ;
30950 psf_log_printf (psf, " fmt : %D\n", chunk_size) ;
30952 /* size of 16 byte marker and 8 byte chunk_size value. */
30953 chunk_size -= 24 ;
30955 if ((error = wav_w64_read_fmt_chunk (psf, &wav_fmt, (int) chunk_size)))
30956 return error ;
30958 if (chunk_size % 8)
30959 psf_binheader_readf (psf, "j", 8 - (chunk_size % 8)) ;
30961 format = wav_fmt.format ;
30962 parsestage |= w64HAVE_fmt ;
30963 break ;
30965 case fact_HASH16:
30966 { sf_count_t frames ;
30968 psf_binheader_readf (psf, "e88", &chunk_size, &frames) ;
30969 psf_log_printf (psf, " fact : %D\n frames : %D\n",
30970 chunk_size, frames) ;
30972 break ;
30975 case data_HASH16 :
30976 if ((parsestage & (HAVE_riff | HAVE_wave | w64HAVE_fmt)) != (HAVE_riff | HAVE_wave | w64HAVE_fmt))
30977 return SFE_W64_NO_DATA ;
30979 psf_binheader_readf (psf, "e8", &chunk_size) ;
30981 psf->dataoffset = psf_ftell (psf) ;
30983 psf->datalength = chunk_size - 24 ;
30985 if (chunk_size % 8)
30986 chunk_size += 8 - (chunk_size % 8) ;
30988 psf_log_printf (psf, "data : %D\n", chunk_size) ;
30990 parsestage |= w64HAVE_data ;
30992 if (! psf->sf.seekable)
30993 break ;
30995 /* Seek past data and continue reading header. */
30996 psf_fseek (psf, chunk_size, SEEK_CUR) ;
30997 break ;
30999 default :
31000 if (psf_ftell (psf) & 0x0F)
31001 { psf_log_printf (psf, " Unknown chunk marker at position %d. Resynching.\n", dword - 4) ;
31002 psf_binheader_readf (psf, "j", -3) ;
31003 break ;
31005 psf_log_printf (psf, "*** Unknown chunk marker : %X. Exiting parser.\n", marker) ;
31006 done = SF_TRUE ;
31007 break ;
31008 } ; /* switch (dword) */
31010 if (psf->sf.seekable == 0 && (parsestage & w64HAVE_data))
31011 break ;
31013 if (psf_ftell (psf) >= (psf->filelength - (2 * SIGNED_SIZEOF (dword))))
31014 break ;
31016 if (psf->logindex >= SIGNED_SIZEOF (psf->logbuffer) - 2)
31017 return SFE_LOG_OVERRUN ;
31018 } ; /* while (1) */
31020 if (! psf->dataoffset)
31021 return SFE_W64_NO_DATA ;
31023 psf->endian = SF_ENDIAN_LITTLE ; /* All WAV files are little endian. */
31025 if (psf_ftell (psf) != psf->dataoffset)
31026 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
31028 psf->close = w64_close ;
31030 if (psf->blockwidth)
31031 { if (psf->filelength - psf->dataoffset < psf->datalength)
31032 psf->sf.frames = (psf->filelength - psf->dataoffset) / psf->blockwidth ;
31033 else
31034 psf->sf.frames = psf->datalength / psf->blockwidth ;
31037 switch (format)
31038 { case WAVE_FORMAT_PCM :
31039 case WAVE_FORMAT_EXTENSIBLE :
31040 /* extensible might be FLOAT, MULAW, etc as well! */
31041 psf->sf.format = SF_FORMAT_W64 | u_bitwidth_to_subformat (psf->bytewidth * 8) ;
31042 break ;
31044 case WAVE_FORMAT_MULAW :
31045 psf->sf.format = (SF_FORMAT_W64 | SF_FORMAT_ULAW) ;
31046 break ;
31048 case WAVE_FORMAT_ALAW :
31049 psf->sf.format = (SF_FORMAT_W64 | SF_FORMAT_ALAW) ;
31050 break ;
31052 case WAVE_FORMAT_MS_ADPCM :
31053 psf->sf.format = (SF_FORMAT_W64 | SF_FORMAT_MS_ADPCM) ;
31054 *blockalign = wav_fmt.msadpcm.blockalign ;
31055 *framesperblock = wav_fmt.msadpcm.samplesperblock ;
31056 break ;
31058 case WAVE_FORMAT_IMA_ADPCM :
31059 psf->sf.format = (SF_FORMAT_W64 | SF_FORMAT_IMA_ADPCM) ;
31060 *blockalign = wav_fmt.ima.blockalign ;
31061 *framesperblock = wav_fmt.ima.samplesperblock ;
31062 break ;
31064 case WAVE_FORMAT_GSM610 :
31065 psf->sf.format = (SF_FORMAT_W64 | SF_FORMAT_GSM610) ;
31066 break ;
31068 case WAVE_FORMAT_IEEE_FLOAT :
31069 psf->sf.format = SF_FORMAT_W64 ;
31070 psf->sf.format |= (psf->bytewidth == 8) ? SF_FORMAT_DOUBLE : SF_FORMAT_FLOAT ;
31071 break ;
31073 default : return SFE_UNIMPLEMENTED ;
31076 return 0 ;
31077 } /* w64_read_header */
31079 static int
31080 w64_write_header (SF_PRIVATE *psf, int calc_length)
31081 { sf_count_t fmt_size, current ;
31082 size_t fmt_pad = 0 ;
31083 int subformat, add_fact_chunk = SF_FALSE ;
31085 current = psf_ftell (psf) ;
31087 if (calc_length)
31088 { psf->filelength = psf_get_filelen (psf) ;
31090 psf->datalength = psf->filelength - psf->dataoffset ;
31091 if (psf->dataend)
31092 psf->datalength -= psf->filelength - psf->dataend ;
31094 if (psf->bytewidth)
31095 psf->sf.frames = psf->datalength / (psf->bytewidth * psf->sf.channels) ;
31098 /* Reset the current header length to zero. */
31099 psf->header [0] = 0 ;
31100 psf->headindex = 0 ;
31101 psf_fseek (psf, 0, SEEK_SET) ;
31103 /* riff marker, length, wave and 'fmt ' markers. */
31104 psf_binheader_writef (psf, "eh8hh", riff_MARKER16, psf->filelength - 8, wave_MARKER16, fmt_MARKER16) ;
31106 subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
31108 switch (subformat)
31109 { case SF_FORMAT_PCM_U8 :
31110 case SF_FORMAT_PCM_16 :
31111 case SF_FORMAT_PCM_24 :
31112 case SF_FORMAT_PCM_32 :
31113 fmt_size = 24 + 2 + 2 + 4 + 4 + 2 + 2 ;
31114 fmt_pad = (size_t) (8 - (fmt_size & 0x7)) ;
31115 fmt_size += fmt_pad ;
31117 /* fmt : format, channels, samplerate */
31118 psf_binheader_writef (psf, "e8224", fmt_size, WAVE_FORMAT_PCM, psf->sf.channels, psf->sf.samplerate) ;
31119 /* fmt : bytespersec */
31120 psf_binheader_writef (psf, "e4", psf->sf.samplerate * psf->bytewidth * psf->sf.channels) ;
31121 /* fmt : blockalign, bitwidth */
31122 psf_binheader_writef (psf, "e22", psf->bytewidth * psf->sf.channels, psf->bytewidth * 8) ;
31123 break ;
31125 case SF_FORMAT_FLOAT :
31126 case SF_FORMAT_DOUBLE :
31127 fmt_size = 24 + 2 + 2 + 4 + 4 + 2 + 2 ;
31128 fmt_pad = (size_t) (8 - (fmt_size & 0x7)) ;
31129 fmt_size += fmt_pad ;
31131 /* fmt : format, channels, samplerate */
31132 psf_binheader_writef (psf, "e8224", fmt_size, WAVE_FORMAT_IEEE_FLOAT, psf->sf.channels, psf->sf.samplerate) ;
31133 /* fmt : bytespersec */
31134 psf_binheader_writef (psf, "e4", psf->sf.samplerate * psf->bytewidth * psf->sf.channels) ;
31135 /* fmt : blockalign, bitwidth */
31136 psf_binheader_writef (psf, "e22", psf->bytewidth * psf->sf.channels, psf->bytewidth * 8) ;
31138 add_fact_chunk = SF_TRUE ;
31139 break ;
31141 case SF_FORMAT_ULAW :
31142 fmt_size = 24 + 2 + 2 + 4 + 4 + 2 + 2 ;
31143 fmt_pad = (size_t) (8 - (fmt_size & 0x7)) ;
31144 fmt_size += fmt_pad ;
31146 /* fmt : format, channels, samplerate */
31147 psf_binheader_writef (psf, "e8224", fmt_size, WAVE_FORMAT_MULAW, psf->sf.channels, psf->sf.samplerate) ;
31148 /* fmt : bytespersec */
31149 psf_binheader_writef (psf, "e4", psf->sf.samplerate * psf->bytewidth * psf->sf.channels) ;
31150 /* fmt : blockalign, bitwidth */
31151 psf_binheader_writef (psf, "e22", psf->bytewidth * psf->sf.channels, 8) ;
31153 add_fact_chunk = SF_TRUE ;
31154 break ;
31156 case SF_FORMAT_ALAW :
31157 fmt_size = 24 + 2 + 2 + 4 + 4 + 2 + 2 ;
31158 fmt_pad = (size_t) (8 - (fmt_size & 0x7)) ;
31159 fmt_size += fmt_pad ;
31161 /* fmt : format, channels, samplerate */
31162 psf_binheader_writef (psf, "e8224", fmt_size, WAVE_FORMAT_ALAW, psf->sf.channels, psf->sf.samplerate) ;
31163 /* fmt : bytespersec */
31164 psf_binheader_writef (psf, "e4", psf->sf.samplerate * psf->bytewidth * psf->sf.channels) ;
31165 /* fmt : blockalign, bitwidth */
31166 psf_binheader_writef (psf, "e22", psf->bytewidth * psf->sf.channels, 8) ;
31168 add_fact_chunk = SF_TRUE ;
31169 break ;
31171 /* Lite remove start */
31172 case SF_FORMAT_IMA_ADPCM :
31173 { int blockalign, framesperblock, bytespersec ;
31175 blockalign = wav_w64_srate2blocksize (psf->sf.samplerate * psf->sf.channels) ;
31176 framesperblock = 2 * (blockalign - 4 * psf->sf.channels) / psf->sf.channels + 1 ;
31177 bytespersec = (psf->sf.samplerate * blockalign) / framesperblock ;
31179 /* fmt chunk. */
31180 fmt_size = 24 + 2 + 2 + 4 + 4 + 2 + 2 + 2 + 2 ;
31181 fmt_pad = (size_t) (8 - (fmt_size & 0x7)) ;
31182 fmt_size += fmt_pad ;
31184 /* fmt : size, WAV format type, channels. */
31185 psf_binheader_writef (psf, "e822", fmt_size, WAVE_FORMAT_IMA_ADPCM, psf->sf.channels) ;
31187 /* fmt : samplerate, bytespersec. */
31188 psf_binheader_writef (psf, "e44", psf->sf.samplerate, bytespersec) ;
31190 /* fmt : blockalign, bitwidth, extrabytes, framesperblock. */
31191 psf_binheader_writef (psf, "e2222", blockalign, 4, 2, framesperblock) ;
31194 add_fact_chunk = SF_TRUE ;
31195 break ;
31197 case SF_FORMAT_MS_ADPCM :
31198 { int blockalign, framesperblock, bytespersec, extrabytes ;
31200 blockalign = wav_w64_srate2blocksize (psf->sf.samplerate * psf->sf.channels) ;
31201 framesperblock = 2 + 2 * (blockalign - 7 * psf->sf.channels) / psf->sf.channels ;
31202 bytespersec = (psf->sf.samplerate * blockalign) / framesperblock ;
31204 /* fmt chunk. */
31205 extrabytes = 2 + 2 + MSADPCM_ADAPT_COEFF_COUNT * (2 + 2) ;
31206 fmt_size = 24 + 2 + 2 + 4 + 4 + 2 + 2 + 2 + extrabytes ;
31207 fmt_pad = (size_t) (8 - (fmt_size & 0x7)) ;
31208 fmt_size += fmt_pad ;
31210 /* fmt : size, W64 format type, channels. */
31211 psf_binheader_writef (psf, "e822", fmt_size, WAVE_FORMAT_MS_ADPCM, psf->sf.channels) ;
31213 /* fmt : samplerate, bytespersec. */
31214 psf_binheader_writef (psf, "e44", psf->sf.samplerate, bytespersec) ;
31216 /* fmt : blockalign, bitwidth, extrabytes, framesperblock. */
31217 psf_binheader_writef (psf, "e22222", blockalign, 4, extrabytes, framesperblock, 7) ;
31219 msadpcm_write_adapt_coeffs (psf) ;
31222 add_fact_chunk = SF_TRUE ;
31223 break ;
31224 /* Lite remove end */
31226 case SF_FORMAT_GSM610 :
31227 { int bytespersec ;
31229 bytespersec = (psf->sf.samplerate * WAV_W64_GSM610_BLOCKSIZE) / WAV_W64_GSM610_SAMPLES ;
31231 /* fmt chunk. */
31232 fmt_size = 24 + 2 + 2 + 4 + 4 + 2 + 2 + 2 + 2 ;
31233 fmt_pad = (size_t) (8 - (fmt_size & 0x7)) ;
31234 fmt_size += fmt_pad ;
31236 /* fmt : size, WAV format type, channels. */
31237 psf_binheader_writef (psf, "e822", fmt_size, WAVE_FORMAT_GSM610, psf->sf.channels) ;
31239 /* fmt : samplerate, bytespersec. */
31240 psf_binheader_writef (psf, "e44", psf->sf.samplerate, bytespersec) ;
31242 /* fmt : blockalign, bitwidth, extrabytes, framesperblock. */
31243 psf_binheader_writef (psf, "e2222", WAV_W64_GSM610_BLOCKSIZE, 0, 2, WAV_W64_GSM610_SAMPLES) ;
31246 add_fact_chunk = SF_TRUE ;
31247 break ;
31249 default : return SFE_UNIMPLEMENTED ;
31252 /* Pad to 8 bytes with zeros. */
31253 if (fmt_pad > 0)
31254 psf_binheader_writef (psf, "z", fmt_pad) ;
31256 if (add_fact_chunk)
31257 psf_binheader_writef (psf, "eh88", fact_MARKER16, 16 + 8 + 8, psf->sf.frames) ;
31259 psf_binheader_writef (psf, "eh8", data_MARKER16, psf->datalength + 24) ;
31260 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
31262 if (psf->error)
31263 return psf->error ;
31265 psf->dataoffset = psf->headindex ;
31267 if (current > 0)
31268 psf_fseek (psf, current, SEEK_SET) ;
31270 return psf->error ;
31271 } /* w64_write_header */
31273 static int
31274 w64_close (SF_PRIVATE *psf)
31276 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
31277 w64_write_header (psf, SF_TRUE) ;
31279 return 0 ;
31280 } /* w64_close */
31284 ** Do not edit or modify anything in this comment block.
31285 ** The arch-tag line is a file identity tag for the GNU Arch
31286 ** revision control system.
31288 ** arch-tag: 9aa4e141-538a-4dd9-99c9-b3f0f2dd4f4a
31291 ** Copyright (C) 1999-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
31292 ** Copyright (C) 2004 David Viens <davidv@plogue.com>
31294 ** This program is free software; you can redistribute it and/or modify
31295 ** it under the terms of the GNU Lesser General Public License as published by
31296 ** the Free Software Foundation; either version 2.1 of the License, or
31297 ** (at your option) any later version.
31299 ** This program is distributed in the hope that it will be useful,
31300 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
31301 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31302 ** GNU Lesser General Public License for more details.
31304 ** You should have received a copy of the GNU Lesser General Public License
31305 ** along with this program; if not, write to the Free Software
31306 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31310 #include <stdio.h>
31311 #include <stdlib.h>
31312 #include <string.h>
31313 #include <ctype.h>
31314 #include <time.h>
31317 /*------------------------------------------------------------------------------
31318 * Macros to handle big/little endian issues.
31321 #define RIFF_MARKER (MAKE_MARKER ('R', 'I', 'F', 'F'))
31322 #define WAVE_MARKER (MAKE_MARKER ('W', 'A', 'V', 'E'))
31323 #define fmt_MARKER (MAKE_MARKER ('f', 'm', 't', ' '))
31324 #define data_MARKER (MAKE_MARKER ('d', 'a', 't', 'a'))
31325 #define fact_MARKER (MAKE_MARKER ('f', 'a', 'c', 't'))
31326 #define PEAK_MARKER (MAKE_MARKER ('P', 'E', 'A', 'K'))
31328 #define cue_MARKER (MAKE_MARKER ('c', 'u', 'e', ' '))
31329 #define LIST_MARKER (MAKE_MARKER ('L', 'I', 'S', 'T'))
31330 #define slnt_MARKER (MAKE_MARKER ('s', 'l', 'n', 't'))
31331 #define wavl_MARKER (MAKE_MARKER ('w', 'a', 'v', 'l'))
31332 #define INFO_MARKER (MAKE_MARKER ('I', 'N', 'F', 'O'))
31333 #define plst_MARKER (MAKE_MARKER ('p', 'l', 's', 't'))
31334 #define adtl_MARKER (MAKE_MARKER ('a', 'd', 't', 'l'))
31335 #define labl_MARKER (MAKE_MARKER ('l', 'a', 'b', 'l'))
31336 #define ltxt_MARKER (MAKE_MARKER ('l', 't', 'x', 't'))
31337 #define note_MARKER (MAKE_MARKER ('n', 'o', 't', 'e'))
31338 #define smpl_MARKER (MAKE_MARKER ('s', 'm', 'p', 'l'))
31339 #define bext_MARKER (MAKE_MARKER ('b', 'e', 'x', 't'))
31340 #define MEXT_MARKER (MAKE_MARKER ('M', 'E', 'X', 'T'))
31341 #define DISP_MARKER (MAKE_MARKER ('D', 'I', 'S', 'P'))
31342 #define acid_MARKER (MAKE_MARKER ('a', 'c', 'i', 'd'))
31343 #define strc_MARKER (MAKE_MARKER ('s', 't', 'r', 'c'))
31344 #define PAD_MARKER (MAKE_MARKER ('P', 'A', 'D', ' '))
31345 #define afsp_MARKER (MAKE_MARKER ('a', 'f', 's', 'p'))
31346 #define clm_MARKER (MAKE_MARKER ('c', 'l', 'm', ' '))
31348 #define ISFT_MARKER (MAKE_MARKER ('I', 'S', 'F', 'T'))
31349 #define ICRD_MARKER (MAKE_MARKER ('I', 'C', 'R', 'D'))
31350 #define ICOP_MARKER (MAKE_MARKER ('I', 'C', 'O', 'P'))
31351 #define IARL_MARKER (MAKE_MARKER ('I', 'A', 'R', 'L'))
31352 #define IART_MARKER (MAKE_MARKER ('I', 'A', 'R', 'T'))
31353 #define INAM_MARKER (MAKE_MARKER ('I', 'N', 'A', 'M'))
31354 #define IENG_MARKER (MAKE_MARKER ('I', 'E', 'N', 'G'))
31355 #define IART_MARKER (MAKE_MARKER ('I', 'A', 'R', 'T'))
31356 #define ICOP_MARKER (MAKE_MARKER ('I', 'C', 'O', 'P'))
31357 #define IPRD_MARKER (MAKE_MARKER ('I', 'P', 'R', 'D'))
31358 #define ISRC_MARKER (MAKE_MARKER ('I', 'S', 'R', 'C'))
31359 #define ISBJ_MARKER (MAKE_MARKER ('I', 'S', 'B', 'J'))
31360 #define ICMT_MARKER (MAKE_MARKER ('I', 'C', 'M', 'T'))
31362 /* Weird WAVPACK marker which can show up at the start of the DATA section. */
31363 #define wvpk_MARKER (MAKE_MARKER ('w', 'v', 'p', 'k'))
31364 #define OggS_MARKER (MAKE_MARKER ('O', 'g', 'g', 'S'))
31366 enum
31367 { HAVE_RIFF = 0x01,
31368 HAVE_WAVE = 0x02,
31369 wavHAVE_fmt = 0x04,
31370 wavHAVE_fact = 0x08,
31371 HAVE_PEAK = 0x10,
31372 wavHAVE_data = 0x20,
31373 HAVE_other = 0x80000000
31378 /* known WAVEFORMATEXTENSIBLE GUIDS */
31379 static const EXT_SUBFORMAT MSGUID_SUBTYPE_PCM =
31380 { 0x00000001, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }
31383 static const EXT_SUBFORMAT MSGUID_SUBTYPE_MS_ADPCM =
31384 { 0x00000002, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }
31387 static const EXT_SUBFORMAT MSGUID_SUBTYPE_IEEE_FLOAT =
31388 { 0x00000003, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }
31391 static const EXT_SUBFORMAT MSGUID_SUBTYPE_ALAW =
31392 { 0x00000006, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }
31395 static const EXT_SUBFORMAT MSGUID_SUBTYPE_MULAW =
31396 { 0x00000007, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }
31399 #if 0
31400 /* maybe interesting one day to read the following through sf_read_raw */
31401 /* http://www.bath.ac.uk/~masrwd/pvocex/pvocex.html */
31402 static const EXT_SUBFORMAT MSGUID_SUBTYPE_PVOCEX =
31403 { 0x8312B9C2, 0x2E6E, 0x11d4, { 0xA8, 0x24, 0xDE, 0x5B, 0x96, 0xC3, 0xAB, 0x21 }
31405 #endif
31407 /*------------------------------------------------------------------------------
31408 ** Private static functions.
31411 static int wav_read_header (SF_PRIVATE *psf, int *blockalign, int *framesperblock) ;
31412 static int wav_write_header (SF_PRIVATE *psf, int calc_length) ;
31414 static void wavex_write_guid (SF_PRIVATE *psf, const EXT_SUBFORMAT * subformat) ;
31415 static int wavex_write_header (SF_PRIVATE *psf, int calc_length) ;
31417 static int wav_write_tailer (SF_PRIVATE *psf) ;
31418 static void wav_write_strings (SF_PRIVATE *psf, int location) ;
31419 static int wav_command (SF_PRIVATE *psf, int command, void *data, int datasize) ;
31420 static int wav_close (SF_PRIVATE *psf) ;
31422 static int wav_subchunk_parse (SF_PRIVATE *psf, int chunk) ;
31423 static int wav_read_smpl_chunk (SF_PRIVATE *psf, unsigned int chunklen) ;
31424 static int wav_read_acid_chunk (SF_PRIVATE *psf, unsigned int chunklen) ;
31426 static int wavex_write_guid_equal (const EXT_SUBFORMAT * first, const EXT_SUBFORMAT * second) ;
31428 /*------------------------------------------------------------------------------
31429 ** Public function.
31433 wav_open (SF_PRIVATE *psf)
31434 { int format, subformat, error, blockalign = 0, framesperblock = 0 ;
31436 if (psf->mode == SFM_READ || (psf->mode == SFM_RDWR && psf->filelength > 0))
31437 { if ((error = wav_read_header (psf, &blockalign, &framesperblock)))
31438 return error ;
31441 subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
31443 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
31444 { if (psf->is_pipe)
31445 return SFE_NO_PIPE_WRITE ;
31447 format = psf->sf.format & SF_FORMAT_TYPEMASK ;
31448 if (format != SF_FORMAT_WAV && format != SF_FORMAT_WAVEX)
31449 return SFE_BAD_OPEN_FORMAT ;
31451 psf->endian = SF_ENDIAN_LITTLE ; /* All WAV files are little endian. */
31453 psf->blockwidth = psf->bytewidth * psf->sf.channels ;
31455 if (psf->mode != SFM_RDWR || psf->filelength < 44)
31456 { psf->filelength = 0 ;
31457 psf->datalength = 0 ;
31458 psf->dataoffset = 0 ;
31459 psf->sf.frames = 0 ;
31462 if (subformat == SF_FORMAT_IMA_ADPCM || subformat == SF_FORMAT_MS_ADPCM)
31463 { blockalign = wav_w64_srate2blocksize (psf->sf.samplerate * psf->sf.channels) ;
31464 framesperblock = -1 ; /* Corrected later. */
31467 psf->str_flags = SF_STR_ALLOW_START | SF_STR_ALLOW_END ;
31469 /* By default, add the peak chunk to floating point files. Default behaviour
31470 ** can be switched off using sf_command (SFC_SET_PEAK_CHUNK, SF_FALSE).
31472 if (psf->mode == SFM_WRITE && (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE))
31473 { psf->pchunk = calloc (1, sizeof (PEAK_CHUNK) * psf->sf.channels * sizeof (PEAK_POS)) ;
31474 if (psf->pchunk == NULL)
31475 return SFE_MALLOC_FAILED ;
31476 psf->has_peak = SF_TRUE ;
31477 psf->peak_loc = SF_PEAK_START ;
31480 psf->write_header = (format == SF_FORMAT_WAV) ? wav_write_header : wavex_write_header ;
31483 psf->close = wav_close ;
31484 psf->command = wav_command ;
31486 switch (subformat)
31487 { case SF_FORMAT_PCM_U8 :
31488 case SF_FORMAT_PCM_16 :
31489 case SF_FORMAT_PCM_24 :
31490 case SF_FORMAT_PCM_32 :
31491 error = pcm_init (psf) ;
31492 break ;
31494 case SF_FORMAT_ULAW :
31495 error = ulaw_init (psf) ;
31496 break ;
31498 case SF_FORMAT_ALAW :
31499 error = alaw_init (psf) ;
31500 break ;
31502 /* Lite remove start */
31503 case SF_FORMAT_FLOAT :
31504 error = float32_init (psf) ;
31505 break ;
31507 case SF_FORMAT_DOUBLE :
31508 error = double64_init (psf) ;
31509 break ;
31511 case SF_FORMAT_IMA_ADPCM :
31512 error = wav_w64_ima_init (psf, blockalign, framesperblock) ;
31513 break ;
31515 case SF_FORMAT_MS_ADPCM :
31516 error = wav_w64_msadpcm_init (psf, blockalign, framesperblock) ;
31517 break ;
31518 /* Lite remove end */
31520 case SF_FORMAT_GSM610 :
31521 error = gsm610_init (psf) ;
31522 break ;
31524 default : return SFE_UNIMPLEMENTED ;
31527 if (psf->mode == SFM_WRITE || (psf->mode == SFM_RDWR && psf->filelength == 0))
31528 return psf->write_header (psf, SF_FALSE) ;
31530 return error ;
31531 } /* wav_open */
31533 /*=========================================================================
31534 ** Private functions.
31537 static int
31538 wav_read_header (SF_PRIVATE *psf, int *blockalign, int *framesperblock)
31539 { WAV_FMT wav_fmt ;
31540 FACT_CHUNK fact_chunk ;
31541 int dword, marker, RIFFsize, done = 0 ;
31542 int parsestage = 0, error, format = 0 ;
31543 char *cptr ;
31545 /* Set position to start of file to begin reading header. */
31546 psf_binheader_readf (psf, "p", 0) ;
31548 while (! done)
31549 { psf_binheader_readf (psf, "m", &marker) ;
31551 switch (marker)
31552 { case RIFF_MARKER :
31553 if (parsestage)
31554 return SFE_WAV_NO_RIFF ;
31556 parsestage |= HAVE_RIFF ;
31558 psf_binheader_readf (psf, "e4", &RIFFsize) ;
31560 if (psf->fileoffset > 0 && psf->filelength > RIFFsize + 8)
31561 { /* Set file length. */
31562 psf->filelength = RIFFsize + 8 ;
31563 psf_log_printf (psf, "RIFF : %u\n", RIFFsize) ;
31565 else if (psf->filelength < RIFFsize + 2 * SIGNED_SIZEOF (dword))
31566 { psf_log_printf (psf, "RIFF : %u (should be %D)\n", RIFFsize, psf->filelength - 2 * SIGNED_SIZEOF (dword)) ;
31567 RIFFsize = dword ;
31569 else
31570 psf_log_printf (psf, "RIFF : %u\n", RIFFsize) ;
31572 break ;
31574 case WAVE_MARKER :
31575 if ((parsestage & HAVE_RIFF) != HAVE_RIFF)
31576 return SFE_WAV_NO_WAVE ;
31577 parsestage |= HAVE_WAVE ;
31579 psf_log_printf (psf, "WAVE\n") ;
31580 break ;
31582 case fmt_MARKER :
31583 if ((parsestage & (HAVE_RIFF | HAVE_WAVE)) != (HAVE_RIFF | HAVE_WAVE))
31584 return SFE_WAV_NO_FMT ;
31586 /* If this file has a SECOND fmt chunk, I don't want to know about it. */
31587 if (parsestage & wavHAVE_fmt)
31588 break ;
31590 parsestage |= wavHAVE_fmt ;
31592 psf_binheader_readf (psf, "e4", &dword) ;
31593 psf_log_printf (psf, "fmt : %d\n", dword) ;
31595 if ((error = wav_w64_read_fmt_chunk (psf, &wav_fmt, dword)))
31596 return error ;
31598 format = wav_fmt.format ;
31599 break ;
31601 case data_MARKER :
31602 if ((parsestage & (HAVE_RIFF | HAVE_WAVE | wavHAVE_fmt)) != (HAVE_RIFF | HAVE_WAVE | wavHAVE_fmt))
31603 return SFE_WAV_NO_DATA ;
31605 if (psf->mode == SFM_RDWR && (parsestage & HAVE_other) != 0)
31606 return SFE_RDWR_BAD_HEADER ;
31608 parsestage |= wavHAVE_data ;
31610 psf_binheader_readf (psf, "e4", &dword) ;
31612 psf->datalength = dword ;
31613 psf->dataoffset = psf_ftell (psf) ;
31615 if (dword == 0 && RIFFsize == 8 && psf->filelength > 44)
31616 { psf_log_printf (psf, "*** Looks like a WAV file which wasn't closed properly. Fixing it.\n") ;
31617 psf->datalength = dword = psf->filelength - psf->dataoffset ;
31620 if (psf->datalength > psf->filelength - psf->dataoffset)
31621 { psf_log_printf (psf, "data : %D (should be %D)\n", psf->datalength, psf->filelength - psf->dataoffset) ;
31622 psf->datalength = psf->filelength - psf->dataoffset ;
31624 else
31625 psf_log_printf (psf, "data : %D\n", psf->datalength) ;
31627 /* Only set dataend if there really is data at the end. */
31628 if (psf->datalength + psf->dataoffset < psf->filelength)
31629 psf->dataend = psf->datalength + psf->dataoffset ;
31631 if (format == WAVE_FORMAT_MS_ADPCM && psf->datalength % 2)
31632 { psf->datalength ++ ;
31633 psf_log_printf (psf, "*** Data length odd. Increasing it by 1.\n") ;
31636 if (! psf->sf.seekable)
31637 break ;
31639 /* Seek past data and continue reading header. */
31640 psf_fseek (psf, psf->datalength, SEEK_CUR) ;
31642 dword = psf_ftell (psf) ;
31643 if (dword != (sf_count_t) (psf->dataoffset + psf->datalength))
31644 psf_log_printf (psf, "*** psf_fseek past end error ***\n", dword, psf->dataoffset + psf->datalength) ;
31645 break ;
31647 case fact_MARKER :
31648 if ((parsestage & (HAVE_RIFF | HAVE_WAVE)) != (HAVE_RIFF | HAVE_WAVE))
31649 return SFE_WAV_BAD_FACT ;
31651 parsestage |= wavHAVE_fact ;
31653 if ((parsestage & wavHAVE_fmt) != wavHAVE_fmt)
31654 psf_log_printf (psf, "*** Should have 'fmt ' chunk before 'fact'\n") ;
31656 psf_binheader_readf (psf, "e44", &dword, & (fact_chunk.frames)) ;
31658 if (dword > SIGNED_SIZEOF (fact_chunk))
31659 psf_binheader_readf (psf, "j", (int) (dword - SIGNED_SIZEOF (fact_chunk))) ;
31661 if (dword)
31662 psf_log_printf (psf, "%M : %d\n", marker, dword) ;
31663 else
31664 psf_log_printf (psf, "%M : %d (should not be zero)\n", marker, dword) ;
31666 psf_log_printf (psf, " frames : %d\n", fact_chunk.frames) ;
31667 break ;
31669 case PEAK_MARKER :
31670 if ((parsestage & (HAVE_RIFF | HAVE_WAVE | wavHAVE_fmt)) != (HAVE_RIFF | HAVE_WAVE | wavHAVE_fmt))
31671 return SFE_WAV_PEAK_B4_FMT ;
31673 parsestage |= HAVE_PEAK ;
31675 psf_binheader_readf (psf, "e4", &dword) ;
31677 psf_log_printf (psf, "%M : %d\n", marker, dword) ;
31678 if (dword != SIGNED_SIZEOF (PEAK_CHUNK) + psf->sf.channels * SIGNED_SIZEOF (PEAK_POS))
31679 { psf_binheader_readf (psf, "j", dword) ;
31680 psf_log_printf (psf, "*** File PEAK chunk size doesn't fit with number of channels.\n") ;
31681 return SFE_WAV_BAD_PEAK ;
31684 psf->pchunk = calloc (1, sizeof (PEAK_CHUNK) * psf->sf.channels * sizeof (PEAK_POS)) ;
31685 if (psf->pchunk == NULL)
31686 return SFE_MALLOC_FAILED ;
31688 /* read in rest of PEAK chunk. */
31689 psf_binheader_readf (psf, "e44", & (psf->pchunk->version), & (psf->pchunk->timestamp)) ;
31691 if (psf->pchunk->version != 1)
31692 psf_log_printf (psf, " version : %d *** (should be version 1)\n", psf->pchunk->version) ;
31693 else
31694 psf_log_printf (psf, " version : %d\n", psf->pchunk->version) ;
31696 psf_log_printf (psf, " time stamp : %d\n", psf->pchunk->timestamp) ;
31697 psf_log_printf (psf, " Ch Position Value\n") ;
31699 cptr = (char *) psf->buffer ;
31700 for (dword = 0 ; dword < psf->sf.channels ; dword++)
31701 { psf_binheader_readf (psf, "ef4", & (psf->pchunk->peaks [dword].value),
31702 & (psf->pchunk->peaks [dword].position)) ;
31704 LSF_SNPRINTF (cptr, sizeof (psf->buffer), " %2d %-12d %g\n",
31705 dword, psf->pchunk->peaks [dword].position, psf->pchunk->peaks [dword].value) ;
31706 cptr [sizeof (psf->buffer) - 1] = 0 ;
31707 psf_log_printf (psf, cptr) ;
31710 psf->has_peak = SF_TRUE ; /* Found PEAK chunk. */
31711 psf->peak_loc = ((parsestage & wavHAVE_data) == 0) ? SF_PEAK_START : SF_PEAK_END ;
31712 break ;
31714 case cue_MARKER :
31715 parsestage |= HAVE_other ;
31717 { int bytesread, cue_count ;
31718 int id, position, chunk_id, chunk_start, block_start, offset ;
31720 bytesread = psf_binheader_readf (psf, "e44", &dword, &cue_count) ;
31721 bytesread -= 4 ; /* Remove bytes for first dword. */
31722 psf_log_printf (psf, "%M : %u\n Count : %d\n", marker, dword, cue_count) ;
31724 while (cue_count)
31725 { bytesread += psf_binheader_readf (psf, "e444444", &id, &position,
31726 &chunk_id, &chunk_start, &block_start, &offset) ;
31727 psf_log_printf (psf, " Cue ID : %2d"
31728 " Pos : %5u Chunk : %M"
31729 " Chk Start : %d Blk Start : %d"
31730 " Offset : %5d\n",
31731 id, position, chunk_id, chunk_start, block_start, offset) ;
31732 cue_count -- ;
31735 if (bytesread != dword)
31736 { psf_log_printf (psf, "**** Chunk size weirdness (%d != %d)\n", dword, bytesread) ;
31737 psf_binheader_readf (psf, "j", dword - bytesread) ;
31740 break ;
31742 case smpl_MARKER :
31743 parsestage |= HAVE_other ;
31745 psf_binheader_readf (psf, "e4", &dword) ;
31746 psf_log_printf (psf, "smpl : %u\n", dword) ;
31748 if ((error = wav_read_smpl_chunk (psf, dword)))
31749 return error ;
31750 break ;
31752 case acid_MARKER :
31753 parsestage |= HAVE_other ;
31755 psf_binheader_readf (psf, "e4", &dword) ;
31756 psf_log_printf (psf, "acid : %u\n", dword) ;
31758 if ((error = wav_read_acid_chunk (psf, dword)))
31759 return error ;
31760 break ;
31762 case INFO_MARKER :
31763 case LIST_MARKER :
31764 parsestage |= HAVE_other ;
31766 if ((error = wav_subchunk_parse (psf, marker)) != 0)
31767 return error ;
31768 break ;
31770 case strc_MARKER : /* Multiple of 32 bytes. */
31772 case afsp_MARKER :
31773 case bext_MARKER :
31774 case clm_MARKER :
31775 case plst_MARKER :
31776 case DISP_MARKER :
31777 case MEXT_MARKER :
31778 case PAD_MARKER :
31779 parsestage |= HAVE_other ;
31781 psf_binheader_readf (psf, "e4", &dword) ;
31782 psf_log_printf (psf, "%M : %u\n", marker, dword) ;
31783 dword += (dword & 1) ;
31784 psf_binheader_readf (psf, "j", dword) ;
31785 break ;
31787 default :
31788 if (isprint ((marker >> 24) & 0xFF) && isprint ((marker >> 16) & 0xFF)
31789 && isprint ((marker >> 8) & 0xFF) && isprint (marker & 0xFF))
31790 { psf_binheader_readf (psf, "e4", &dword) ;
31791 psf_log_printf (psf, "*** %M : %d (unknown marker)\n", marker, dword) ;
31792 psf_binheader_readf (psf, "j", dword) ;
31793 break ;
31795 if (psf_ftell (psf) & 0x03)
31796 { psf_log_printf (psf, " Unknown chunk marker at position %d. Resynching.\n", dword - 4) ;
31797 psf_binheader_readf (psf, "j", -3) ;
31798 break ;
31800 psf_log_printf (psf, "*** Unknown chunk marker : %X. Exiting parser.\n", marker) ;
31801 done = SF_TRUE ;
31802 break ;
31803 } ; /* switch (dword) */
31805 if (! psf->sf.seekable && (parsestage & wavHAVE_data))
31806 break ;
31808 if (psf_ftell (psf) >= psf->filelength - SIGNED_SIZEOF (dword))
31809 { psf_log_printf (psf, "End\n") ;
31810 break ;
31813 if (psf->logindex >= SIGNED_SIZEOF (psf->logbuffer) - 2)
31814 return SFE_LOG_OVERRUN ;
31815 } ; /* while (1) */
31817 if (! psf->dataoffset)
31818 return SFE_WAV_NO_DATA ;
31820 psf->endian = SF_ENDIAN_LITTLE ; /* All WAV files are little endian. */
31822 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
31824 if (psf->is_pipe == 0)
31825 { /*
31826 ** Check for 'wvpk' at the start of the DATA section. Not able to
31827 ** handle this.
31829 psf_binheader_readf (psf, "e4", &marker) ;
31830 if (marker == wvpk_MARKER || marker == OggS_MARKER)
31831 return SFE_WAV_WVPK_DATA ;
31834 /* Seek to start of DATA section. */
31835 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
31837 psf->close = wav_close ;
31839 if (psf->blockwidth)
31840 { if (psf->filelength - psf->dataoffset < psf->datalength)
31841 psf->sf.frames = (psf->filelength - psf->dataoffset) / psf->blockwidth ;
31842 else
31843 psf->sf.frames = psf->datalength / psf->blockwidth ;
31846 switch (format)
31849 case WAVE_FORMAT_EXTENSIBLE :
31850 /* compare GUIDs for known ones */
31851 if (wavex_write_guid_equal (&wav_fmt.ext.esf, &MSGUID_SUBTYPE_PCM))
31852 psf->sf.format = SF_FORMAT_WAVEX | u_bitwidth_to_subformat (psf->bytewidth * 8) ;
31853 else
31854 if (wavex_write_guid_equal (&wav_fmt.ext.esf, &MSGUID_SUBTYPE_MS_ADPCM))
31855 { psf->sf.format = (SF_FORMAT_WAVEX | SF_FORMAT_MS_ADPCM) ;
31856 *blockalign = wav_fmt.msadpcm.blockalign ;
31857 *framesperblock = wav_fmt.msadpcm.samplesperblock ;
31859 else if (wavex_write_guid_equal (&wav_fmt.ext.esf, &MSGUID_SUBTYPE_IEEE_FLOAT))
31860 psf->sf.format = SF_FORMAT_WAVEX | ((psf->bytewidth == 8) ? SF_FORMAT_DOUBLE : SF_FORMAT_FLOAT) ;
31861 else if (wavex_write_guid_equal (&wav_fmt.ext.esf, &MSGUID_SUBTYPE_ALAW))
31862 psf->sf.format = (SF_FORMAT_WAVEX | SF_FORMAT_ALAW) ;
31863 else if (wavex_write_guid_equal (&wav_fmt.ext.esf, &MSGUID_SUBTYPE_MULAW))
31864 psf->sf.format = (SF_FORMAT_WAVEX | SF_FORMAT_ULAW) ;
31865 else
31866 return SFE_UNIMPLEMENTED ;
31867 break ;
31869 case WAVE_FORMAT_PCM :
31870 psf->sf.format = SF_FORMAT_WAV | u_bitwidth_to_subformat (psf->bytewidth * 8) ;
31871 break ;
31873 case WAVE_FORMAT_MULAW :
31874 case IBM_FORMAT_MULAW :
31875 psf->sf.format = (SF_FORMAT_WAV | SF_FORMAT_ULAW) ;
31876 break ;
31878 case WAVE_FORMAT_ALAW :
31879 case IBM_FORMAT_ALAW :
31880 psf->sf.format = (SF_FORMAT_WAV | SF_FORMAT_ALAW) ;
31881 break ;
31883 case WAVE_FORMAT_MS_ADPCM :
31884 psf->sf.format = (SF_FORMAT_WAV | SF_FORMAT_MS_ADPCM) ;
31885 *blockalign = wav_fmt.msadpcm.blockalign ;
31886 *framesperblock = wav_fmt.msadpcm.samplesperblock ;
31887 break ;
31889 case WAVE_FORMAT_IMA_ADPCM :
31890 psf->sf.format = (SF_FORMAT_WAV | SF_FORMAT_IMA_ADPCM) ;
31891 *blockalign = wav_fmt.ima.blockalign ;
31892 *framesperblock = wav_fmt.ima.samplesperblock ;
31893 break ;
31895 case WAVE_FORMAT_GSM610 :
31896 psf->sf.format = (SF_FORMAT_WAV | SF_FORMAT_GSM610) ;
31897 break ;
31899 case WAVE_FORMAT_IEEE_FLOAT :
31900 psf->sf.format = SF_FORMAT_WAV ;
31901 psf->sf.format |= (psf->bytewidth == 8) ? SF_FORMAT_DOUBLE : SF_FORMAT_FLOAT ;
31902 break ;
31904 default : return SFE_UNIMPLEMENTED ;
31907 return 0 ;
31908 } /* wav_read_header */
31910 static int
31911 wav_write_header (SF_PRIVATE *psf, int calc_length)
31912 { sf_count_t current ;
31913 int fmt_size, k, subformat, add_fact_chunk = SF_FALSE ;
31915 current = psf_ftell (psf) ;
31917 if (calc_length)
31918 { psf->filelength = psf_get_filelen (psf) ;
31920 psf->datalength = psf->filelength - psf->dataoffset ;
31922 if (psf->dataend)
31923 psf->datalength -= psf->filelength - psf->dataend ;
31925 if (psf->bytewidth > 0)
31926 psf->sf.frames = psf->datalength / (psf->bytewidth * psf->sf.channels) ;
31929 /* Reset the current header length to zero. */
31930 psf->header [0] = 0 ;
31931 psf->headindex = 0 ;
31932 psf_fseek (psf, 0, SEEK_SET) ;
31934 /* RIFF marker, length, WAVE and 'fmt ' markers. */
31935 if (psf->filelength < 8)
31936 psf_binheader_writef (psf, "etm8", RIFF_MARKER, 8) ;
31937 else
31938 psf_binheader_writef (psf, "etm8", RIFF_MARKER, psf->filelength - 8) ;
31940 /* WAVE and 'fmt ' markers. */
31941 psf_binheader_writef (psf, "emm", WAVE_MARKER, fmt_MARKER) ;
31943 subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
31945 switch (subformat)
31946 { case SF_FORMAT_PCM_U8 :
31947 case SF_FORMAT_PCM_16 :
31948 case SF_FORMAT_PCM_24 :
31949 case SF_FORMAT_PCM_32 :
31950 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 ;
31952 /* fmt : format, channels, samplerate */
31953 psf_binheader_writef (psf, "e4224", fmt_size, WAVE_FORMAT_PCM, psf->sf.channels, psf->sf.samplerate) ;
31954 /* fmt : bytespersec */
31955 psf_binheader_writef (psf, "e4", psf->sf.samplerate * psf->bytewidth * psf->sf.channels) ;
31956 /* fmt : blockalign, bitwidth */
31957 psf_binheader_writef (psf, "e22", psf->bytewidth * psf->sf.channels, psf->bytewidth * 8) ;
31958 break ;
31960 case SF_FORMAT_FLOAT :
31961 case SF_FORMAT_DOUBLE :
31962 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 ;
31964 /* fmt : format, channels, samplerate */
31965 psf_binheader_writef (psf, "e4224", fmt_size, WAVE_FORMAT_IEEE_FLOAT, psf->sf.channels, psf->sf.samplerate) ;
31966 /* fmt : bytespersec */
31967 psf_binheader_writef (psf, "e4", psf->sf.samplerate * psf->bytewidth * psf->sf.channels) ;
31968 /* fmt : blockalign, bitwidth */
31969 psf_binheader_writef (psf, "e22", psf->bytewidth * psf->sf.channels, psf->bytewidth * 8) ;
31971 add_fact_chunk = SF_TRUE ;
31972 break ;
31974 case SF_FORMAT_ULAW :
31975 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 ;
31977 /* fmt : format, channels, samplerate */
31978 psf_binheader_writef (psf, "e4224", fmt_size, WAVE_FORMAT_MULAW, psf->sf.channels, psf->sf.samplerate) ;
31979 /* fmt : bytespersec */
31980 psf_binheader_writef (psf, "e4", psf->sf.samplerate * psf->bytewidth * psf->sf.channels) ;
31981 /* fmt : blockalign, bitwidth */
31982 psf_binheader_writef (psf, "e22", psf->bytewidth * psf->sf.channels, 8) ;
31984 add_fact_chunk = SF_TRUE ;
31985 break ;
31987 case SF_FORMAT_ALAW :
31988 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 ;
31990 /* fmt : format, channels, samplerate */
31991 psf_binheader_writef (psf, "e4224", fmt_size, WAVE_FORMAT_ALAW, psf->sf.channels, psf->sf.samplerate) ;
31992 /* fmt : bytespersec */
31993 psf_binheader_writef (psf, "e4", psf->sf.samplerate * psf->bytewidth * psf->sf.channels) ;
31994 /* fmt : blockalign, bitwidth */
31995 psf_binheader_writef (psf, "e22", psf->bytewidth * psf->sf.channels, 8) ;
31997 add_fact_chunk = SF_TRUE ;
31998 break ;
32000 /* Lite remove start */
32001 case SF_FORMAT_IMA_ADPCM :
32002 { int blockalign, framesperblock, bytespersec ;
32004 blockalign = wav_w64_srate2blocksize (psf->sf.samplerate * psf->sf.channels) ;
32005 framesperblock = 2 * (blockalign - 4 * psf->sf.channels) / psf->sf.channels + 1 ;
32006 bytespersec = (psf->sf.samplerate * blockalign) / framesperblock ;
32008 /* fmt chunk. */
32009 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 + 2 ;
32011 /* fmt : size, WAV format type, channels, samplerate, bytespersec */
32012 psf_binheader_writef (psf, "e42244", fmt_size, WAVE_FORMAT_IMA_ADPCM,
32013 psf->sf.channels, psf->sf.samplerate, bytespersec) ;
32015 /* fmt : blockalign, bitwidth, extrabytes, framesperblock. */
32016 psf_binheader_writef (psf, "e2222", blockalign, 4, 2, framesperblock) ;
32019 add_fact_chunk = SF_TRUE ;
32020 break ;
32022 case SF_FORMAT_MS_ADPCM :
32023 { int blockalign, framesperblock, bytespersec, extrabytes ;
32025 blockalign = wav_w64_srate2blocksize (psf->sf.samplerate * psf->sf.channels) ;
32026 framesperblock = 2 + 2 * (blockalign - 7 * psf->sf.channels) / psf->sf.channels ;
32027 bytespersec = (psf->sf.samplerate * blockalign) / framesperblock ;
32029 /* fmt chunk. */
32030 extrabytes = 2 + 2 + MSADPCM_ADAPT_COEFF_COUNT * (2 + 2) ;
32031 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 + extrabytes ;
32033 /* fmt : size, WAV format type, channels. */
32034 psf_binheader_writef (psf, "e422", fmt_size, WAVE_FORMAT_MS_ADPCM, psf->sf.channels) ;
32036 /* fmt : samplerate, bytespersec. */
32037 psf_binheader_writef (psf, "e44", psf->sf.samplerate, bytespersec) ;
32039 /* fmt : blockalign, bitwidth, extrabytes, framesperblock. */
32040 psf_binheader_writef (psf, "e22222", blockalign, 4, extrabytes, framesperblock, 7) ;
32042 msadpcm_write_adapt_coeffs (psf) ;
32045 add_fact_chunk = SF_TRUE ;
32046 break ;
32047 /* Lite remove end */
32049 case SF_FORMAT_GSM610 :
32050 { int blockalign, framesperblock, bytespersec ;
32052 blockalign = WAV_W64_GSM610_BLOCKSIZE ;
32053 framesperblock = WAV_W64_GSM610_SAMPLES ;
32054 bytespersec = (psf->sf.samplerate * blockalign) / framesperblock ;
32056 /* fmt chunk. */
32057 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 + 2 ;
32059 /* fmt : size, WAV format type, channels. */
32060 psf_binheader_writef (psf, "e422", fmt_size, WAVE_FORMAT_GSM610, psf->sf.channels) ;
32062 /* fmt : samplerate, bytespersec. */
32063 psf_binheader_writef (psf, "e44", psf->sf.samplerate, bytespersec) ;
32065 /* fmt : blockalign, bitwidth, extrabytes, framesperblock. */
32066 psf_binheader_writef (psf, "e2222", blockalign, 0, 2, framesperblock) ;
32069 add_fact_chunk = SF_TRUE ;
32070 break ;
32072 default : return SFE_UNIMPLEMENTED ;
32075 if (add_fact_chunk)
32076 psf_binheader_writef (psf, "etm48", fact_MARKER, 4, psf->sf.frames) ;
32078 if (psf->str_flags & SF_STR_LOCATE_START)
32079 wav_write_strings (psf, SF_STR_LOCATE_START) ;
32081 if (psf->has_peak && psf->peak_loc == SF_PEAK_START)
32082 { psf_binheader_writef (psf, "em4", PEAK_MARKER,
32083 sizeof (PEAK_CHUNK) + psf->sf.channels * sizeof (PEAK_POS)) ;
32084 psf_binheader_writef (psf, "e44", 1, time (NULL)) ;
32085 for (k = 0 ; k < psf->sf.channels ; k++)
32086 psf_binheader_writef (psf, "ef4", psf->pchunk->peaks [k].value, psf->pchunk->peaks [k].position) ;
32089 psf_binheader_writef (psf, "etm8", data_MARKER, psf->datalength) ;
32090 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
32091 if (psf->error)
32092 return psf->error ;
32094 psf->dataoffset = psf->headindex ;
32096 if (current < psf->dataoffset)
32097 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
32098 else if (current > 0)
32099 psf_fseek (psf, current, SEEK_SET) ;
32101 return psf->error ;
32102 } /* wav_write_header */
32106 static int
32107 wavex_write_guid_equal (const EXT_SUBFORMAT * first, const EXT_SUBFORMAT * second)
32108 { return !memcmp (first, second, sizeof (EXT_SUBFORMAT)) ;
32109 } /* wavex_write_guid_equal */
32112 static void
32113 wavex_write_guid (SF_PRIVATE *psf, const EXT_SUBFORMAT * subformat)
32115 psf_binheader_writef (psf, "e422b", subformat->esf_field1,
32116 subformat->esf_field2, subformat->esf_field3,
32117 subformat->esf_field4, 8) ;
32118 } /* wavex_write_guid */
32121 static int
32122 wavex_write_header (SF_PRIVATE *psf, int calc_length)
32123 { sf_count_t current ;
32124 int fmt_size, k, subformat, add_fact_chunk = SF_FALSE ;
32126 current = psf_ftell (psf) ;
32128 if (calc_length)
32129 { psf->filelength = psf_get_filelen (psf) ;
32131 psf->datalength = psf->filelength - psf->dataoffset ;
32133 if (psf->dataend)
32134 psf->datalength -= psf->filelength - psf->dataend ;
32136 if (psf->bytewidth > 0)
32137 psf->sf.frames = psf->datalength / (psf->bytewidth * psf->sf.channels) ;
32141 /* Reset the current header length to zero. */
32142 psf->header [0] = 0 ;
32143 psf->headindex = 0 ;
32144 psf_fseek (psf, 0, SEEK_SET) ;
32146 /* RIFF marker, length, WAVE and 'fmt ' markers. */
32147 if (psf->filelength < 8)
32148 psf_binheader_writef (psf, "etm8", RIFF_MARKER, 8) ;
32149 else
32150 psf_binheader_writef (psf, "etm8", RIFF_MARKER, psf->filelength - 8) ;
32152 /* WAVE and 'fmt ' markers. */
32153 psf_binheader_writef (psf, "emm", WAVE_MARKER, fmt_MARKER) ;
32155 subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
32157 /* initial section (same for all, it appears) */
32158 switch (subformat)
32159 { case SF_FORMAT_PCM_U8 :
32160 case SF_FORMAT_PCM_16 :
32161 case SF_FORMAT_PCM_24 :
32162 case SF_FORMAT_PCM_32 :
32163 case SF_FORMAT_FLOAT :
32164 case SF_FORMAT_DOUBLE :
32165 case SF_FORMAT_ULAW :
32166 case SF_FORMAT_ALAW :
32167 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 + 2 + 4 + 4 + 2 + 2 + 8 ;
32169 /* fmt : format, channels, samplerate */
32170 psf_binheader_writef (psf, "e4224", fmt_size, WAVE_FORMAT_EXTENSIBLE, psf->sf.channels, psf->sf.samplerate) ;
32171 /* fmt : bytespersec */
32172 psf_binheader_writef (psf, "e4", psf->sf.samplerate * psf->bytewidth * psf->sf.channels) ;
32173 /* fmt : blockalign, bitwidth */
32174 psf_binheader_writef (psf, "e22", psf->bytewidth * psf->sf.channels, psf->bytewidth * 8) ;
32176 /* cbSize 22 is sizeof (WAVEFORMATEXTENSIBLE) - sizeof (WAVEFORMATEX) */
32177 psf_binheader_writef (psf, "e2", 22) ;
32179 /* wValidBitsPerSample, for our use same as bitwidth as we use it fully */
32180 psf_binheader_writef (psf, "e2", psf->bytewidth * 8) ;
32182 if (psf->sf.channels == 2)
32183 psf_binheader_writef (psf, "e4", 0x1 | 0x2 ) ; /* dwChannelMask front left and right */
32184 else
32185 psf_binheader_writef (psf, "e4", 0) ; /* dwChannelMask = 0 when in doubt */
32186 break ;
32188 case SF_FORMAT_MS_ADPCM : /* todo, GUID exists might have different header as per wav_write_header */
32189 default : return SFE_UNIMPLEMENTED ;
32192 /* GUI section, different for each */
32194 switch (subformat)
32195 { case SF_FORMAT_PCM_U8 :
32196 case SF_FORMAT_PCM_16 :
32197 case SF_FORMAT_PCM_24 :
32198 case SF_FORMAT_PCM_32 :
32199 wavex_write_guid (psf, &MSGUID_SUBTYPE_PCM) ;
32200 break ;
32202 case SF_FORMAT_FLOAT :
32203 case SF_FORMAT_DOUBLE :
32204 wavex_write_guid (psf, &MSGUID_SUBTYPE_IEEE_FLOAT) ;
32205 add_fact_chunk = SF_TRUE ;
32206 break ;
32208 case SF_FORMAT_ULAW :
32209 wavex_write_guid (psf, &MSGUID_SUBTYPE_MULAW) ;
32210 add_fact_chunk = SF_TRUE ;
32211 break ;
32213 case SF_FORMAT_ALAW :
32214 wavex_write_guid (psf, &MSGUID_SUBTYPE_ALAW) ;
32215 add_fact_chunk = SF_TRUE ;
32216 break ;
32218 case SF_FORMAT_MS_ADPCM : /* todo, GUID exists */
32220 default : return SFE_UNIMPLEMENTED ;
32224 if (add_fact_chunk)
32225 psf_binheader_writef (psf, "etm48", fact_MARKER, 4, psf->sf.frames) ;
32227 if (psf->str_flags & SF_STR_LOCATE_START)
32228 wav_write_strings (psf, SF_STR_LOCATE_START) ;
32230 if (psf->has_peak && psf->peak_loc == SF_PEAK_START)
32231 { psf_binheader_writef (psf, "em4", PEAK_MARKER,
32232 sizeof (PEAK_CHUNK) + psf->sf.channels * sizeof (PEAK_POS)) ;
32233 psf_binheader_writef (psf, "e44", 1, time (NULL)) ;
32234 for (k = 0 ; k < psf->sf.channels ; k++)
32235 psf_binheader_writef (psf, "ef4", psf->pchunk->peaks [k].value, psf->pchunk->peaks [k].position) ;
32238 psf_binheader_writef (psf, "etm8", data_MARKER, psf->datalength) ;
32239 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
32240 if (psf->error)
32241 return psf->error ;
32243 psf->dataoffset = psf->headindex ;
32245 if (current < psf->dataoffset)
32246 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
32247 else if (current > 0)
32248 psf_fseek (psf, current, SEEK_SET) ;
32250 return psf->error ;
32251 } /* wavex_write_header */
32255 static int
32256 wav_write_tailer (SF_PRIVATE *psf)
32257 { int k ;
32259 /* Reset the current header buffer length to zero. */
32260 psf->header [0] = 0 ;
32261 psf->headindex = 0 ;
32263 psf->dataend = psf_fseek (psf, 0, SEEK_END) ;
32265 /* Add a PEAK chunk if requested. */
32266 if (psf->has_peak && psf->peak_loc == SF_PEAK_END)
32267 { psf_binheader_writef (psf, "em4", PEAK_MARKER,
32268 sizeof (PEAK_CHUNK) + psf->sf.channels * sizeof (PEAK_POS)) ;
32269 psf_binheader_writef (psf, "e44", 1, time (NULL)) ;
32270 for (k = 0 ; k < psf->sf.channels ; k++)
32271 psf_binheader_writef (psf, "ef4", psf->pchunk->peaks [k].value, psf->pchunk->peaks [k].position) ;
32274 if (psf->str_flags & SF_STR_LOCATE_END)
32275 wav_write_strings (psf, SF_STR_LOCATE_END) ;
32277 /* Write the tailer. */
32278 if (psf->headindex > 0)
32279 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
32281 return 0 ;
32282 } /* wav_write_tailer */
32284 static void
32285 wav_write_strings (SF_PRIVATE *psf, int location)
32286 { int k, prev_head_index, saved_head_index ;
32288 prev_head_index = psf->headindex + 4 ;
32290 psf_binheader_writef (psf, "em4m", LIST_MARKER, 0xBADBAD, INFO_MARKER) ;
32292 for (k = 0 ; k < SF_MAX_STRINGS ; k++)
32293 { if (psf->strings [k].type == 0)
32294 break ;
32295 if (psf->strings [k].flags != location)
32296 continue ;
32298 switch (psf->strings [k].type)
32299 { case SF_STR_SOFTWARE :
32300 psf_binheader_writef (psf, "ems", ISFT_MARKER, psf->strings [k].str) ;
32301 break ;
32303 case SF_STR_TITLE :
32304 psf_binheader_writef (psf, "ems", INAM_MARKER, psf->strings [k].str) ;
32305 break ;
32307 case SF_STR_COPYRIGHT :
32308 psf_binheader_writef (psf, "ems", ICOP_MARKER, psf->strings [k].str) ;
32309 break ;
32311 case SF_STR_ARTIST :
32312 psf_binheader_writef (psf, "ems", IART_MARKER, psf->strings [k].str) ;
32313 break ;
32315 case SF_STR_COMMENT :
32316 psf_binheader_writef (psf, "ems", ICMT_MARKER, psf->strings [k].str) ;
32317 break ;
32319 case SF_STR_DATE :
32320 psf_binheader_writef (psf, "ems", ICRD_MARKER, psf->strings [k].str) ;
32321 break ;
32325 saved_head_index = psf->headindex ;
32326 psf->headindex = prev_head_index ;
32327 psf_binheader_writef (psf, "e4", saved_head_index - prev_head_index - 4) ;
32328 psf->headindex = saved_head_index ;
32330 } /* wav_write_strings */
32332 static int
32333 wav_close (SF_PRIVATE *psf)
32335 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
32336 { wav_write_tailer (psf) ;
32338 if ((psf->sf.format & SF_FORMAT_TYPEMASK) == SF_FORMAT_WAV)
32339 wav_write_header (psf, SF_TRUE) ;
32340 else
32341 wavex_write_header (psf, SF_TRUE) ;
32344 return 0 ;
32345 } /* wav_close */
32347 static int
32348 wav_command (SF_PRIVATE *psf, int command, void *data, int datasize)
32350 /* Avoid compiler warnings. */
32351 psf = psf ;
32352 data = data ;
32353 datasize = datasize ;
32355 switch (command)
32356 { default : break ;
32359 return 0 ;
32360 } /* wav_command */
32362 static int
32363 wav_subchunk_parse (SF_PRIVATE *psf, int chunk)
32364 { sf_count_t current_pos ;
32365 char *cptr ;
32366 int dword, bytesread, length ;
32368 current_pos = psf_fseek (psf, 0, SEEK_CUR) ;
32370 bytesread = psf_binheader_readf (psf, "e4", &length) ;
32372 if (current_pos + length > psf->filelength)
32373 { psf_log_printf (psf, "%M : %d (should be %d)\n", chunk, length, (int) (psf->filelength - current_pos)) ;
32374 length = psf->filelength - current_pos ;
32376 else
32377 psf_log_printf (psf, "%M : %d\n", chunk, length) ;
32380 while (bytesread < length)
32381 { bytesread += psf_binheader_readf (psf, "m", &chunk) ;
32383 switch (chunk)
32384 { case adtl_MARKER :
32385 case INFO_MARKER :
32386 /* These markers don't contain anything. */
32387 psf_log_printf (psf, " %M\n", chunk) ;
32388 break ;
32390 case data_MARKER:
32391 psf_log_printf (psf, " %M inside a LIST block??? Backing out.\n", chunk) ;
32392 /* Jump back four bytes and return to caller. */
32393 psf_binheader_readf (psf, "j", -4) ;
32394 return 0 ;
32396 case ISFT_MARKER :
32397 case ICOP_MARKER :
32398 case IARL_MARKER :
32399 case IART_MARKER :
32400 case ICMT_MARKER :
32401 case ICRD_MARKER :
32402 case IENG_MARKER :
32404 case INAM_MARKER :
32405 case IPRD_MARKER :
32406 case ISBJ_MARKER :
32407 case ISRC_MARKER :
32408 bytesread += psf_binheader_readf (psf, "e4", &dword) ;
32409 dword += (dword & 1) ;
32410 if (dword > SIGNED_SIZEOF (psf->buffer))
32411 { psf_log_printf (psf, " *** %M : %d (too big)\n", chunk, dword) ;
32412 return SFE_INTERNAL ;
32415 cptr = (char*) psf->buffer ;
32416 psf_binheader_readf (psf, "b", cptr, dword) ;
32417 bytesread += dword ;
32418 cptr [dword - 1] = 0 ;
32419 psf_log_printf (psf, " %M : %s\n", chunk, cptr) ;
32420 break ;
32422 case labl_MARKER :
32423 { int mark_id ;
32425 bytesread += psf_binheader_readf (psf, "e44", &dword, &mark_id) ;
32426 dword -= 4 ;
32427 dword += (dword & 1) ;
32428 if (dword > SIGNED_SIZEOF (psf->buffer))
32429 { psf_log_printf (psf, " *** %M : %d (too big)\n", chunk, dword) ;
32430 return SFE_INTERNAL ;
32433 cptr = (char*) psf->buffer ;
32434 psf_binheader_readf (psf, "b", cptr, dword) ;
32435 bytesread += dword ;
32436 cptr [dword - 1] = 0 ;
32437 psf_log_printf (psf, " %M : %d : %s\n", chunk, mark_id, cptr) ;
32439 break ;
32442 case DISP_MARKER :
32443 case ltxt_MARKER :
32444 case note_MARKER :
32445 bytesread += psf_binheader_readf (psf, "e4", &dword) ;
32446 dword += (dword & 1) ;
32447 psf_binheader_readf (psf, "j", dword) ;
32448 bytesread += dword ;
32449 psf_log_printf (psf, " %M : %d\n", chunk, dword) ;
32450 break ;
32452 default :
32453 psf_binheader_readf (psf, "e4", &dword) ;
32454 bytesread += sizeof (dword) ;
32455 dword += (dword & 1) ;
32456 psf_binheader_readf (psf, "j", dword) ;
32457 bytesread += dword ;
32458 psf_log_printf (psf, " *** %M : %d\n", chunk, dword) ;
32459 if (dword > length)
32460 return 0 ;
32461 break ;
32464 switch (chunk)
32465 { case ISFT_MARKER :
32466 psf_store_string (psf, SF_STR_SOFTWARE, (char*) psf->buffer) ;
32467 break ;
32468 case ICOP_MARKER :
32469 psf_store_string (psf, SF_STR_COPYRIGHT, (char*) psf->buffer) ;
32470 break ;
32471 case INAM_MARKER :
32472 psf_store_string (psf, SF_STR_TITLE, (char*) psf->buffer) ;
32473 break ;
32474 case IART_MARKER :
32475 psf_store_string (psf, SF_STR_ARTIST, (char*) psf->buffer) ;
32476 break ;
32477 case ICMT_MARKER :
32478 psf_store_string (psf, SF_STR_COMMENT, (char*) psf->buffer) ;
32479 break ;
32480 case ICRD_MARKER :
32481 psf_store_string (psf, SF_STR_DATE, (char*) psf->buffer) ;
32482 break ;
32485 if (psf->logindex >= SIGNED_SIZEOF (psf->logbuffer) - 2)
32486 return SFE_LOG_OVERRUN ;
32489 current_pos = psf_fseek (psf, 0, SEEK_CUR) - current_pos ;
32491 if (current_pos - 4 != length)
32492 psf_log_printf (psf, "**** Bad chunk length %d sbould be %D\n", length, current_pos - 4) ;
32494 return 0 ;
32495 } /* wav_subchunk_parse */
32497 static int
32498 wav_read_smpl_chunk (SF_PRIVATE *psf, unsigned int chunklen)
32499 { unsigned int bytesread = 0, dword, sampler_data, loop_count ;
32500 int k ;
32502 chunklen += (chunklen & 1) ;
32504 bytesread += psf_binheader_readf (psf, "e4", &dword) ;
32505 psf_log_printf (psf, " Manufacturer : %X\n", dword) ;
32507 bytesread += psf_binheader_readf (psf, "e4", &dword) ;
32508 psf_log_printf (psf, " Product : %u\n", dword) ;
32510 bytesread += psf_binheader_readf (psf, "e4", &dword) ;
32511 psf_log_printf (psf, " Period : %u nsec\n", dword) ;
32513 bytesread += psf_binheader_readf (psf, "e4", &dword) ;
32514 psf_log_printf (psf, " Midi Note : %u\n", dword) ;
32516 bytesread += psf_binheader_readf (psf, "e4", &dword) ;
32517 if (dword != 0)
32518 { LSF_SNPRINTF ((char*) psf->buffer, sizeof (psf->buffer), "%f",
32519 (1.0 * 0x80000000) / ((unsigned int) dword)) ;
32520 psf_log_printf (psf, " Pitch Fract. : %s\n", (char*) psf->buffer) ;
32522 else
32523 psf_log_printf (psf, " Pitch Fract. : 0\n") ;
32525 bytesread += psf_binheader_readf (psf, "e4", &dword) ;
32526 psf_log_printf (psf, " SMPTE Format : %u\n", dword) ;
32528 bytesread += psf_binheader_readf (psf, "e4", &dword) ;
32529 LSF_SNPRINTF ((char*) psf->buffer, sizeof (psf->buffer), "%02d:%02d:%02d %02d",
32530 (dword >> 24) & 0x7F, (dword >> 16) & 0x7F, (dword >> 8) & 0x7F, dword & 0x7F) ;
32531 psf_log_printf (psf, " SMPTE Offset : %s\n", psf->buffer) ;
32533 bytesread += psf_binheader_readf (psf, "e4", &loop_count) ;
32534 psf_log_printf (psf, " Loop Count : %u\n", loop_count) ;
32536 /* Sampler Data holds the number of data bytes after the CUE chunks which
32537 ** is not actually CUE data. Display value after CUE data.
32539 bytesread += psf_binheader_readf (psf, "e4", &sampler_data) ;
32541 while (loop_count > 0 && chunklen - bytesread >= 24)
32543 bytesread += psf_binheader_readf (psf, "e4", &dword) ;
32544 psf_log_printf (psf, " Cue ID : %2u", dword) ;
32546 bytesread += psf_binheader_readf (psf, "e4", &dword) ;
32547 psf_log_printf (psf, " Type : %2u", dword) ;
32549 bytesread += psf_binheader_readf (psf, "e4", &dword) ;
32550 psf_log_printf (psf, " Start : %5u", dword) ;
32552 bytesread += psf_binheader_readf (psf, "e4", &dword) ;
32553 psf_log_printf (psf, " End : %5u", dword) ;
32555 bytesread += psf_binheader_readf (psf, "e4", &dword) ;
32556 psf_log_printf (psf, " Fraction : %5u", dword) ;
32558 bytesread += psf_binheader_readf (psf, "e4", &dword) ;
32559 psf_log_printf (psf, " Count : %5u\n", dword) ;
32561 loop_count -- ;
32564 if (chunklen - bytesread == 0)
32565 { if (sampler_data != 0)
32566 psf_log_printf (psf, " Sampler Data : %u (should be 0)\n", sampler_data) ;
32567 else
32568 psf_log_printf (psf, " Sampler Data : %u\n", sampler_data) ;
32570 else
32571 { if (sampler_data != chunklen - bytesread)
32572 { psf_log_printf (psf, " Sampler Data : %u (should have been %u)\n", sampler_data, chunklen - bytesread) ;
32573 sampler_data = chunklen - bytesread ;
32575 else
32576 psf_log_printf (psf, " Sampler Data : %u\n", sampler_data) ;
32578 psf_log_printf (psf, " ") ;
32579 for (k = 0 ; k < (int) sampler_data ; k++)
32580 { char ch ;
32582 if (k > 0 && (k % 20) == 0)
32583 psf_log_printf (psf, "\n ") ;
32585 bytesread += psf_binheader_readf (psf, "1", &ch) ;
32586 psf_log_printf (psf, "%02X ", ch & 0xFF) ;
32589 psf_log_printf (psf, "\n") ;
32592 return 0 ;
32593 } /* wav_read_smpl_chunk */
32596 ** The acid chunk goes a little something like this:
32598 ** 4 bytes 'acid'
32599 ** 4 bytes (int) length of chunk
32600 ** 4 bytes (int) ???
32601 ** 2 bytes (short) root note
32602 ** 2 bytes (short) ???
32603 ** 4 bytes (float) ???
32604 ** 4 bytes (int) number of beats
32605 ** 2 bytes (short) meter denominator
32606 ** 2 bytes (short) meter numerator
32607 ** 4 bytes (float) tempo
32611 static int
32612 wav_read_acid_chunk (SF_PRIVATE *psf, unsigned int chunklen)
32613 { unsigned int bytesread = 0 ;
32614 int beats ;
32615 short rootnote, q1, meter_denom, meter_numer ;
32616 float q2, q3, tempo ;
32618 chunklen += (chunklen & 1) ;
32620 bytesread += psf_binheader_readf (psf, "e224f", &rootnote, &q1, &q2, &q3) ;
32621 LSF_SNPRINTF ((char*) psf->buffer, sizeof (psf->buffer), "%f", q2) ;
32622 psf_log_printf (psf, " Root note : %d\n ???? : %d\n ???? : %s\n ???? : %d\n",
32623 rootnote, q1, psf->buffer, q3) ;
32625 bytesread += psf_binheader_readf (psf, "e422f", &beats, &meter_denom, &meter_numer, &tempo) ;
32626 LSF_SNPRINTF ((char*) psf->buffer, sizeof (psf->buffer), "%f", tempo) ;
32627 psf_log_printf (psf, " Beats : %d\n Meter : %d/%d\n Tempo : %s\n",
32628 beats, meter_denom, meter_numer, psf->buffer) ;
32630 psf_binheader_readf (psf, "j", chunklen - bytesread) ;
32632 return 0 ;
32633 } /* wav_read_acid_chunk */
32635 ** Do not edit or modify anything in this comment block.
32636 ** The arch-tag line is a file identity tag for the GNU Arch
32637 ** revision control system.
32639 ** arch-tag: 9c551689-a1d8-4905-9f56-26a204374f18
32642 ** Copyright (C) 1999-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
32644 ** This program is free software; you can redistribute it and/or modify
32645 ** it under the terms of the GNU Lesser General Public License as published by
32646 ** the Free Software Foundation; either version 2.1 of the License, or
32647 ** (at your option) any later version.
32649 ** This program is distributed in the hope that it will be useful,
32650 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
32651 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32652 ** GNU Lesser General Public License for more details.
32654 ** You should have received a copy of the GNU Lesser General Public License
32655 ** along with this program; if not, write to the Free Software
32656 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
32659 #include <stdio.h>
32660 #include <string.h>
32661 #include <ctype.h>
32662 #include <time.h>
32665 /*------------------------------------------------------------------------------
32666 * Private static functions.
32670 wav_w64_read_fmt_chunk (SF_PRIVATE *psf, WAV_FMT *wav_fmt, int structsize)
32671 { int bytesread, k, bytespersec = 0 ;
32673 memset (wav_fmt, 0, sizeof (WAV_FMT)) ;
32675 if (structsize < 16)
32676 return SFE_WAV_FMT_SHORT ;
32677 if (structsize > SIGNED_SIZEOF (WAV_FMT))
32678 return SFE_WAV_FMT_TOO_BIG ;
32680 /* Read the minimal WAV file header here. */
32681 bytesread =
32682 psf_binheader_readf (psf, "e224422", &(wav_fmt->format), &(wav_fmt->min.channels),
32683 &(wav_fmt->min.samplerate), &(wav_fmt->min.bytespersec),
32684 &(wav_fmt->min.blockalign), &(wav_fmt->min.bitwidth)) ;
32686 psf_log_printf (psf, " Format : 0x%X => %s\n", wav_fmt->format, wav_w64_format_str (wav_fmt->format)) ;
32687 psf_log_printf (psf, " Channels : %d\n", wav_fmt->min.channels) ;
32688 psf_log_printf (psf, " Sample Rate : %d\n", wav_fmt->min.samplerate) ;
32689 psf_log_printf (psf, " Block Align : %d\n", wav_fmt->min.blockalign) ;
32691 if (wav_fmt->format == WAVE_FORMAT_PCM && wav_fmt->min.bitwidth == 24 &&
32692 wav_fmt->min.blockalign == 4 * wav_fmt->min.channels)
32694 psf_log_printf (psf, "\nInvalid file generated by Syntrillium's Cooledit!\n"
32695 "Treating as WAVE_FORMAT_IEEE_FLOAT 32 bit floating point file.\n\n") ;
32696 psf_log_printf (psf, " Bit Width : 24 (should be 32)\n") ;
32697 wav_fmt->min.bitwidth = 32 ;
32698 wav_fmt->format = WAVE_FORMAT_IEEE_FLOAT ;
32700 else if (wav_fmt->format == WAVE_FORMAT_GSM610 && wav_fmt->min.bitwidth != 0)
32701 psf_log_printf (psf, " Bit Width : %d (should be 0)\n", wav_fmt->min.bitwidth) ;
32702 else
32703 psf_log_printf (psf, " Bit Width : %d\n", wav_fmt->min.bitwidth) ;
32706 psf->sf.samplerate = wav_fmt->min.samplerate ;
32707 psf->sf.frames = 0 ; /* Correct this when reading data chunk. */
32708 psf->sf.channels = wav_fmt->min.channels ;
32710 switch (wav_fmt->format)
32711 { case WAVE_FORMAT_PCM :
32712 case WAVE_FORMAT_IEEE_FLOAT :
32713 bytespersec = wav_fmt->min.samplerate * wav_fmt->min.blockalign ;
32714 if (wav_fmt->min.bytespersec != (unsigned) bytespersec)
32715 psf_log_printf (psf, " Bytes/sec : %d (should be %d)\n", wav_fmt->min.bytespersec, bytespersec) ;
32716 else
32717 psf_log_printf (psf, " Bytes/sec : %d\n", wav_fmt->min.bytespersec) ;
32719 psf->bytewidth = BITWIDTH2BYTES (wav_fmt->min.bitwidth) ;
32720 break ;
32722 case WAVE_FORMAT_ALAW :
32723 case WAVE_FORMAT_MULAW :
32724 if (wav_fmt->min.bytespersec / wav_fmt->min.blockalign != wav_fmt->min.samplerate)
32725 psf_log_printf (psf, " Bytes/sec : %d (should be %d)\n", wav_fmt->min.bytespersec, wav_fmt->min.samplerate * wav_fmt->min.blockalign) ;
32726 else
32727 psf_log_printf (psf, " Bytes/sec : %d\n", wav_fmt->min.bytespersec) ;
32729 psf->bytewidth = 1 ;
32730 if (structsize >= 18)
32731 { bytesread += psf_binheader_readf (psf, "e2", &(wav_fmt->size20.extrabytes)) ;
32732 psf_log_printf (psf, " Extra Bytes : %d\n", wav_fmt->size20.extrabytes) ;
32734 break ;
32736 case WAVE_FORMAT_IMA_ADPCM :
32737 if (wav_fmt->min.bitwidth != 4)
32738 return SFE_WAV_ADPCM_NOT4BIT ;
32739 if (wav_fmt->min.channels < 1 || wav_fmt->min.channels > 2)
32740 return SFE_WAV_ADPCM_CHANNELS ;
32742 bytesread +=
32743 psf_binheader_readf (psf, "e22", &(wav_fmt->ima.extrabytes), &(wav_fmt->ima.samplesperblock)) ;
32745 bytespersec = (wav_fmt->ima.samplerate * wav_fmt->ima.blockalign) / wav_fmt->ima.samplesperblock ;
32746 if (wav_fmt->ima.bytespersec != (unsigned) bytespersec)
32747 psf_log_printf (psf, " Bytes/sec : %d (should be %d)\n", wav_fmt->ima.bytespersec, bytespersec) ;
32748 else
32749 psf_log_printf (psf, " Bytes/sec : %d\n", wav_fmt->ima.bytespersec) ;
32751 psf->bytewidth = 2 ;
32752 psf_log_printf (psf, " Extra Bytes : %d\n", wav_fmt->ima.extrabytes) ;
32753 psf_log_printf (psf, " Samples/Block : %d\n", wav_fmt->ima.samplesperblock) ;
32754 break ;
32756 case WAVE_FORMAT_MS_ADPCM :
32757 if (wav_fmt->msadpcm.bitwidth != 4)
32758 return SFE_WAV_ADPCM_NOT4BIT ;
32759 if (wav_fmt->msadpcm.channels < 1 || wav_fmt->msadpcm.channels > 2)
32760 return SFE_WAV_ADPCM_CHANNELS ;
32762 bytesread +=
32763 psf_binheader_readf (psf, "e222", &(wav_fmt->msadpcm.extrabytes),
32764 &(wav_fmt->msadpcm.samplesperblock), &(wav_fmt->msadpcm.numcoeffs)) ;
32766 bytespersec = (wav_fmt->min.samplerate * wav_fmt->min.blockalign) / wav_fmt->msadpcm.samplesperblock ;
32767 if (wav_fmt->min.bytespersec == (unsigned) bytespersec)
32768 psf_log_printf (psf, " Bytes/sec : %d\n", wav_fmt->min.bytespersec) ;
32769 else if (wav_fmt->min.bytespersec == (wav_fmt->min.samplerate / wav_fmt->msadpcm.samplesperblock) * wav_fmt->min.blockalign)
32770 psf_log_printf (psf, " Bytes/sec : %d (should be %d (MS BUG!))\n", wav_fmt->min.bytespersec, bytespersec) ;
32771 else
32772 psf_log_printf (psf, " Bytes/sec : %d (should be %d)\n", wav_fmt->min.bytespersec, bytespersec) ;
32775 psf->bytewidth = 2 ;
32776 psf_log_printf (psf, " Extra Bytes : %d\n", wav_fmt->msadpcm.extrabytes) ;
32777 psf_log_printf (psf, " Samples/Block : %d\n", wav_fmt->msadpcm.samplesperblock) ;
32778 if (wav_fmt->msadpcm.numcoeffs > SIGNED_SIZEOF (MS_ADPCM_WAV_FMT) / SIGNED_SIZEOF (int))
32779 { psf_log_printf (psf, " No. of Coeffs : %d ****\n", wav_fmt->msadpcm.numcoeffs) ;
32780 wav_fmt->msadpcm.numcoeffs = SIGNED_SIZEOF (MS_ADPCM_WAV_FMT) / SIGNED_SIZEOF (int) ;
32782 else
32783 psf_log_printf (psf, " No. of Coeffs : %d\n", wav_fmt->msadpcm.numcoeffs) ;
32785 psf_log_printf (psf, " Index Coeffs1 Coeffs2\n") ;
32786 for (k = 0 ; k < wav_fmt->msadpcm.numcoeffs ; k++)
32787 { bytesread +=
32788 psf_binheader_readf (psf, "e22", &(wav_fmt->msadpcm.coeffs [k].coeff1), &(wav_fmt->msadpcm.coeffs [k].coeff2)) ;
32789 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) ;
32790 psf_log_printf (psf, (char*) psf->buffer) ;
32792 break ;
32794 case WAVE_FORMAT_GSM610 :
32795 if (wav_fmt->gsm610.channels != 1 || wav_fmt->gsm610.blockalign != 65)
32796 return SFE_WAV_GSM610_FORMAT ;
32798 bytesread +=
32799 psf_binheader_readf (psf, "e22", &(wav_fmt->gsm610.extrabytes), &(wav_fmt->gsm610.samplesperblock)) ;
32801 if (wav_fmt->gsm610.samplesperblock != 320)
32802 return SFE_WAV_GSM610_FORMAT ;
32804 bytespersec = (wav_fmt->gsm610.samplerate * wav_fmt->gsm610.blockalign) / wav_fmt->gsm610.samplesperblock ;
32805 if (wav_fmt->gsm610.bytespersec != (unsigned) bytespersec)
32806 psf_log_printf (psf, " Bytes/sec : %d (should be %d)\n", wav_fmt->gsm610.bytespersec, bytespersec) ;
32807 else
32808 psf_log_printf (psf, " Bytes/sec : %d\n", wav_fmt->gsm610.bytespersec) ;
32810 psf->bytewidth = 2 ;
32811 psf_log_printf (psf, " Extra Bytes : %d\n", wav_fmt->gsm610.extrabytes) ;
32812 psf_log_printf (psf, " Samples/Block : %d\n", wav_fmt->gsm610.samplesperblock) ;
32813 break ;
32815 case WAVE_FORMAT_EXTENSIBLE :
32816 if (wav_fmt->ext.bytespersec / wav_fmt->ext.blockalign != wav_fmt->ext.samplerate)
32817 psf_log_printf (psf, " Bytes/sec : %d (should be %d)\n", wav_fmt->ext.bytespersec, wav_fmt->ext.samplerate * wav_fmt->ext.blockalign) ;
32818 else
32819 psf_log_printf (psf, " Bytes/sec : %d\n", wav_fmt->ext.bytespersec) ;
32821 bytesread +=
32822 psf_binheader_readf (psf, "e224", &(wav_fmt->ext.extrabytes), &(wav_fmt->ext.validbits),
32823 &(wav_fmt->ext.channelmask)) ;
32825 psf_log_printf (psf, " Valid Bits : %d\n", wav_fmt->ext.validbits) ;
32826 psf_log_printf (psf, " Channel Mask : 0x%X\n", wav_fmt->ext.channelmask) ;
32828 bytesread +=
32829 psf_binheader_readf (psf, "e422", &(wav_fmt->ext.esf.esf_field1), &(wav_fmt->ext.esf.esf_field2),
32830 &(wav_fmt->ext.esf.esf_field3)) ;
32832 /* compare the esf_fields with each known GUID? and print?*/
32833 psf_log_printf (psf, " Subformat\n") ;
32834 psf_log_printf (psf, " esf_field1 : 0x%X\n", wav_fmt->ext.esf.esf_field1) ;
32835 psf_log_printf (psf, " esf_field2 : 0x%X\n", wav_fmt->ext.esf.esf_field2) ;
32836 psf_log_printf (psf, " esf_field3 : 0x%X\n", wav_fmt->ext.esf.esf_field3) ;
32837 psf_log_printf (psf, " esf_field4 : ") ;
32838 for (k = 0 ; k < 8 ; k++)
32839 { bytesread += psf_binheader_readf (psf, "1", &(wav_fmt->ext.esf.esf_field4 [k])) ;
32840 psf_log_printf (psf, "0x%X ", wav_fmt->ext.esf.esf_field4 [k] & 0xFF) ;
32842 psf_log_printf (psf, "\n") ;
32843 psf->bytewidth = BITWIDTH2BYTES (wav_fmt->ext.bitwidth) ;
32844 break ;
32846 default : break ;
32849 if (bytesread > structsize)
32850 { psf_log_printf (psf, "*** wav_w64_read_fmt_chunk (bytesread > structsize)\n") ;
32851 return SFE_W64_FMT_SHORT ;
32853 else
32854 psf_binheader_readf (psf, "j", structsize - bytesread) ;
32856 psf->blockwidth = wav_fmt->min.channels * psf->bytewidth ;
32858 return 0 ;
32859 } /* wav_w64_read_fmt_chunk */
32861 /*==============================================================================
32864 typedef struct
32865 { int ID ;
32866 const char *name ;
32867 } WAV_FORMAT_DESC ;
32869 #define STR(x) #x
32870 #define FORMAT_TYPE(x) { x, STR (x) }
32872 static WAV_FORMAT_DESC wave_descs [] =
32873 { FORMAT_TYPE (WAVE_FORMAT_PCM),
32874 FORMAT_TYPE (WAVE_FORMAT_MS_ADPCM),
32875 FORMAT_TYPE (WAVE_FORMAT_IEEE_FLOAT),
32876 FORMAT_TYPE (WAVE_FORMAT_VSELP),
32877 FORMAT_TYPE (WAVE_FORMAT_IBM_CVSD),
32878 FORMAT_TYPE (WAVE_FORMAT_ALAW),
32879 FORMAT_TYPE (WAVE_FORMAT_MULAW),
32880 FORMAT_TYPE (WAVE_FORMAT_OKI_ADPCM),
32881 FORMAT_TYPE (WAVE_FORMAT_IMA_ADPCM),
32882 FORMAT_TYPE (WAVE_FORMAT_MEDIASPACE_ADPCM),
32883 FORMAT_TYPE (WAVE_FORMAT_SIERRA_ADPCM),
32884 FORMAT_TYPE (WAVE_FORMAT_G723_ADPCM),
32885 FORMAT_TYPE (WAVE_FORMAT_DIGISTD),
32886 FORMAT_TYPE (WAVE_FORMAT_DIGIFIX),
32887 FORMAT_TYPE (WAVE_FORMAT_DIALOGIC_OKI_ADPCM),
32888 FORMAT_TYPE (WAVE_FORMAT_MEDIAVISION_ADPCM),
32889 FORMAT_TYPE (WAVE_FORMAT_CU_CODEC),
32890 FORMAT_TYPE (WAVE_FORMAT_YAMAHA_ADPCM),
32891 FORMAT_TYPE (WAVE_FORMAT_SONARC),
32892 FORMAT_TYPE (WAVE_FORMAT_DSPGROUP_TRUESPEECH),
32893 FORMAT_TYPE (WAVE_FORMAT_ECHOSC1),
32894 FORMAT_TYPE (WAVE_FORMAT_AUDIOFILE_AF36),
32895 FORMAT_TYPE (WAVE_FORMAT_APTX),
32896 FORMAT_TYPE (WAVE_FORMAT_AUDIOFILE_AF10),
32897 FORMAT_TYPE (WAVE_FORMAT_PROSODY_1612),
32898 FORMAT_TYPE (WAVE_FORMAT_LRC),
32899 FORMAT_TYPE (WAVE_FORMAT_DOLBY_AC2),
32900 FORMAT_TYPE (WAVE_FORMAT_GSM610),
32901 FORMAT_TYPE (WAVE_FORMAT_MSNAUDIO),
32902 FORMAT_TYPE (WAVE_FORMAT_ANTEX_ADPCME),
32903 FORMAT_TYPE (WAVE_FORMAT_CONTROL_RES_VQLPC),
32904 FORMAT_TYPE (WAVE_FORMAT_DIGIREAL),
32905 FORMAT_TYPE (WAVE_FORMAT_DIGIADPCM),
32906 FORMAT_TYPE (WAVE_FORMAT_CONTROL_RES_CR10),
32907 FORMAT_TYPE (WAVE_FORMAT_NMS_VBXADPCM),
32908 FORMAT_TYPE (WAVE_FORMAT_ROLAND_RDAC),
32909 FORMAT_TYPE (WAVE_FORMAT_ECHOSC3),
32910 FORMAT_TYPE (WAVE_FORMAT_ROCKWELL_ADPCM),
32911 FORMAT_TYPE (WAVE_FORMAT_ROCKWELL_DIGITALK),
32912 FORMAT_TYPE (WAVE_FORMAT_XEBEC),
32913 FORMAT_TYPE (WAVE_FORMAT_G721_ADPCM),
32914 FORMAT_TYPE (WAVE_FORMAT_G728_CELP),
32915 FORMAT_TYPE (WAVE_FORMAT_MSG723),
32916 FORMAT_TYPE (WAVE_FORMAT_MPEG),
32917 FORMAT_TYPE (WAVE_FORMAT_RT24),
32918 FORMAT_TYPE (WAVE_FORMAT_PAC),
32919 FORMAT_TYPE (WAVE_FORMAT_MPEGLAYER3),
32920 FORMAT_TYPE (WAVE_FORMAT_LUCENT_G723),
32921 FORMAT_TYPE (WAVE_FORMAT_CIRRUS),
32922 FORMAT_TYPE (WAVE_FORMAT_ESPCM),
32923 FORMAT_TYPE (WAVE_FORMAT_VOXWARE),
32924 FORMAT_TYPE (WAVE_FORMAT_CANOPUS_ATRAC),
32925 FORMAT_TYPE (WAVE_FORMAT_G726_ADPCM),
32926 FORMAT_TYPE (WAVE_FORMAT_G722_ADPCM),
32927 FORMAT_TYPE (WAVE_FORMAT_DSAT),
32928 FORMAT_TYPE (WAVE_FORMAT_DSAT_DISPLAY),
32929 FORMAT_TYPE (WAVE_FORMAT_VOXWARE_BYTE_ALIGNED),
32930 FORMAT_TYPE (WAVE_FORMAT_VOXWARE_AC8),
32931 FORMAT_TYPE (WAVE_FORMAT_VOXWARE_AC10),
32932 FORMAT_TYPE (WAVE_FORMAT_VOXWARE_AC16),
32933 FORMAT_TYPE (WAVE_FORMAT_VOXWARE_AC20),
32934 FORMAT_TYPE (WAVE_FORMAT_VOXWARE_RT24),
32935 FORMAT_TYPE (WAVE_FORMAT_VOXWARE_RT29),
32936 FORMAT_TYPE (WAVE_FORMAT_VOXWARE_RT29HW),
32937 FORMAT_TYPE (WAVE_FORMAT_VOXWARE_VR12),
32938 FORMAT_TYPE (WAVE_FORMAT_VOXWARE_VR18),
32939 FORMAT_TYPE (WAVE_FORMAT_VOXWARE_TQ40),
32940 FORMAT_TYPE (WAVE_FORMAT_SOFTSOUND),
32941 FORMAT_TYPE (WAVE_FORMAT_VOXARE_TQ60),
32942 FORMAT_TYPE (WAVE_FORMAT_MSRT24),
32943 FORMAT_TYPE (WAVE_FORMAT_G729A),
32944 FORMAT_TYPE (WAVE_FORMAT_MVI_MV12),
32945 FORMAT_TYPE (WAVE_FORMAT_DF_G726),
32946 FORMAT_TYPE (WAVE_FORMAT_DF_GSM610),
32947 FORMAT_TYPE (WAVE_FORMAT_ONLIVE),
32948 FORMAT_TYPE (WAVE_FORMAT_SBC24),
32949 FORMAT_TYPE (WAVE_FORMAT_DOLBY_AC3_SPDIF),
32950 FORMAT_TYPE (WAVE_FORMAT_ZYXEL_ADPCM),
32951 FORMAT_TYPE (WAVE_FORMAT_PHILIPS_LPCBB),
32952 FORMAT_TYPE (WAVE_FORMAT_PACKED),
32953 FORMAT_TYPE (WAVE_FORMAT_RHETOREX_ADPCM),
32954 FORMAT_TYPE (IBM_FORMAT_MULAW),
32955 FORMAT_TYPE (IBM_FORMAT_ALAW),
32956 FORMAT_TYPE (IBM_FORMAT_ADPCM),
32957 FORMAT_TYPE (WAVE_FORMAT_VIVO_G723),
32958 FORMAT_TYPE (WAVE_FORMAT_VIVO_SIREN),
32959 FORMAT_TYPE (WAVE_FORMAT_DIGITAL_G723),
32960 FORMAT_TYPE (WAVE_FORMAT_CREATIVE_ADPCM),
32961 FORMAT_TYPE (WAVE_FORMAT_CREATIVE_FASTSPEECH8),
32962 FORMAT_TYPE (WAVE_FORMAT_CREATIVE_FASTSPEECH10),
32963 FORMAT_TYPE (WAVE_FORMAT_QUARTERDECK),
32964 FORMAT_TYPE (WAVE_FORMAT_FM_TOWNS_SND),
32965 FORMAT_TYPE (WAVE_FORMAT_BZV_DIGITAL),
32966 FORMAT_TYPE (WAVE_FORMAT_VME_VMPCM),
32967 FORMAT_TYPE (WAVE_FORMAT_OLIGSM),
32968 FORMAT_TYPE (WAVE_FORMAT_OLIADPCM),
32969 FORMAT_TYPE (WAVE_FORMAT_OLICELP),
32970 FORMAT_TYPE (WAVE_FORMAT_OLISBC),
32971 FORMAT_TYPE (WAVE_FORMAT_OLIOPR),
32972 FORMAT_TYPE (WAVE_FORMAT_LH_CODEC),
32973 FORMAT_TYPE (WAVE_FORMAT_NORRIS),
32974 FORMAT_TYPE (WAVE_FORMAT_SOUNDSPACE_MUSICOMPRESS),
32975 FORMAT_TYPE (WAVE_FORMAT_DVM),
32976 FORMAT_TYPE (WAVE_FORMAT_INTERWAV_VSC112),
32977 FORMAT_TYPE (WAVE_FORMAT_EXTENSIBLE),
32980 char const*
32981 wav_w64_format_str (int k)
32982 { int lower, upper, mid ;
32984 lower = -1 ;
32985 upper = sizeof (wave_descs) / sizeof (WAV_FORMAT_DESC) ;
32987 /* binary search */
32988 if ((wave_descs [0].ID <= k) & (k <= wave_descs [upper - 1].ID))
32990 while (lower + 1 < upper)
32991 { mid = (upper + lower) / 2 ;
32993 if (k == wave_descs [mid].ID)
32994 return wave_descs [mid].name ;
32995 if (k < wave_descs [mid].ID)
32996 upper = mid ;
32997 else
32998 lower = mid ;
33002 return "Unknown format" ;
33003 } /* wav_w64_format_str */
33006 wav_w64_srate2blocksize (int srate_chan_product)
33007 { if (srate_chan_product < 12000)
33008 return 256 ;
33009 if (srate_chan_product < 23000)
33010 return 512 ;
33011 if (srate_chan_product < 44000)
33012 return 1024 ;
33013 return 2048 ;
33014 } /* srate2blocksize */
33016 ** Do not edit or modify anything in this comment block.
33017 ** The arch-tag line is a file identity tag for the GNU Arch
33018 ** revision control system.
33020 ** arch-tag: 43c1b1dd-8abd-43da-a8cd-44da914b64a5
33023 ** Copyright (C) 2002-2004 Erik de Castro Lopo <erikd@mega-nerd.com>
33025 ** This program is free software; you can redistribute it and/or modify
33026 ** it under the terms of the GNU Lesser General Public License as published by
33027 ** the Free Software Foundation; either version 2.1 of the License, or
33028 ** (at your option) any later version.
33030 ** This program is distributed in the hope that it will be useful,
33031 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
33032 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33033 ** GNU Lesser General Public License for more details.
33035 ** You should have received a copy of the GNU Lesser General Public License
33036 ** along with this program; if not, write to the Free Software
33037 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
33040 #include <stdio.h>
33041 #include <fcntl.h>
33042 #include <string.h>
33043 #include <ctype.h>
33046 #if (ENABLE_EXPERIMENTAL_CODE == 0)
33049 wve_open (SF_PRIVATE *psf)
33050 { if (psf)
33051 return SFE_UNIMPLEMENTED ;
33052 return (psf && 0) ;
33053 } /* wve_open */
33055 #else
33057 #define SFE_WVE_NOT_WVE 666
33059 /*------------------------------------------------------------------------------
33060 ** Macros to handle big/little endian issues.
33063 #define ALAW_MARKER MAKE_MARKER ('A', 'L', 'a', 'w')
33064 #define SOUN_MARKER MAKE_MARKER ('S', 'o', 'u', 'n')
33065 #define DFIL_MARKER MAKE_MARKER ('d', 'F', 'i', 'l')
33067 /*------------------------------------------------------------------------------
33068 ** Private static functions.
33071 static int wve_read_header (SF_PRIVATE *psf) ;
33073 /*------------------------------------------------------------------------------
33074 ** Public function.
33078 wve_open (SF_PRIVATE *psf)
33079 { int subformat, error = 0 ;
33081 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
33082 return SFE_UNIMPLEMENTED ;
33084 if ((error = wve_read_header (psf)))
33085 return error ;
33087 if ((psf->sf.format & SF_FORMAT_TYPEMASK) != SF_FORMAT_WVE)
33088 return SFE_BAD_OPEN_FORMAT ;
33090 subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
33092 return error ;
33093 } /* wve_open */
33095 /*------------------------------------------------------------------------------
33098 static int
33099 wve_read_header (SF_PRIVATE *psf)
33100 { int marker ;
33102 /* Set position to start of file to begin reading header. */
33103 psf_binheader_readf (psf, "pm", 0, &marker) ;
33104 if (marker != ALAW_MARKER)
33105 return SFE_WVE_NOT_WVE ;
33107 psf_binheader_readf (psf, "m", &marker) ;
33108 if (marker != SOUN_MARKER)
33109 return SFE_WVE_NOT_WVE ;
33111 psf_binheader_readf (psf, "m", &marker) ;
33112 if (marker != DFIL_MARKER)
33113 return SFE_WVE_NOT_WVE ;
33115 psf_log_printf (psf, "Read only : Psion Palmtop Alaw (.wve)\n"
33116 " Sample Rate : 8000\n"
33117 " Channels : 1\n"
33118 " Encoding : A-law\n") ;
33120 psf->dataoffset = 0x20 ;
33121 psf->datalength = psf->filelength - psf->dataoffset ;
33123 psf->sf.format = SF_FORMAT_WVE | SF_FORMAT_ALAW ;
33124 psf->sf.samplerate = 8000 ;
33125 psf->sf.frames = psf->datalength ;
33126 psf->sf.channels = 1 ;
33128 return alaw_init (psf) ;
33129 } /* wve_read_header */
33131 /*------------------------------------------------------------------------------
33134 #endif
33136 ** Do not edit or modify anything in this comment block.
33137 ** The arch-tag line is a file identity tag for the GNU Arch
33138 ** revision control system.
33140 ** arch-tag: ba368cb5-523f-45e4-98c1-5b99a102f73f
33143 ** Copyright (C) 2003,2004 Erik de Castro Lopo <erikd@mega-nerd.com>
33145 ** This program is free software; you can redistribute it and/or modify
33146 ** it under the terms of the GNU Lesser General Public License as published by
33147 ** the Free Software Foundation; either version 2.1 of the License, or
33148 ** (at your option) any later version.
33150 ** This program is distributed in the hope that it will be useful,
33151 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
33152 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33153 ** GNU Lesser General Public License for more details.
33155 ** You should have received a copy of the GNU Lesser General Public License
33156 ** along with this program; if not, write to the Free Software
33157 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
33161 #include <stdio.h>
33162 #include <stdlib.h>
33163 #include <fcntl.h>
33164 #include <string.h>
33165 #include <ctype.h>
33168 #define MAX_XI_SAMPLES 16
33170 /*------------------------------------------------------------------------------
33171 ** Private static functions and tyepdefs.
33174 typedef struct
33175 { /* Warning, this filename is NOT nul terminated. */
33176 char filename [22] ;
33177 char software [20] ;
33178 char sample_name [22] ;
33180 int loop_begin, loop_end ;
33181 int sample_flags ;
33183 /* Data for encoder and decoder. */
33184 short last_16 ;
33185 } XI_PRIVATE ;
33187 static int xi_close (SF_PRIVATE *psf) ;
33188 static int xi_write_header (SF_PRIVATE *psf, int calc_length) ;
33189 static int xi_read_header (SF_PRIVATE *psf) ;
33190 static int dpcm_init (SF_PRIVATE *psf) ;
33193 static sf_count_t dpcm_seek (SF_PRIVATE *psf, int mode, sf_count_t offset) ;
33195 /*------------------------------------------------------------------------------
33196 ** Public function.
33200 xi_open (SF_PRIVATE *psf)
33201 { XI_PRIVATE *pxi ;
33202 int subformat, error = 0 ;
33204 if (psf->is_pipe)
33205 return SFE_XI_NO_PIPE ;
33207 if (psf->fdata)
33208 pxi = psf->fdata ;
33209 else if ((pxi = calloc (1, sizeof (XI_PRIVATE))) == NULL)
33210 return SFE_MALLOC_FAILED ;
33212 psf->fdata = pxi ;
33214 if (psf->mode == SFM_READ || (psf->mode == SFM_RDWR && psf->filelength > 0))
33215 { if ((error = xi_read_header (psf)))
33216 return error ;
33219 subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
33221 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
33222 { if ((psf->sf.format & SF_FORMAT_TYPEMASK) != SF_FORMAT_XI)
33223 return SFE_BAD_OPEN_FORMAT ;
33225 psf->endian = SF_ENDIAN_LITTLE ;
33226 psf->sf.channels = 1 ; /* Always mono */
33227 psf->sf.samplerate = 44100 ; /* Always */
33229 /* Set up default instrument and software name. */
33230 memcpy (pxi->filename, "Default Name ", sizeof (pxi->filename)) ;
33231 memcpy (pxi->software, PACKAGE "-" VERSION " ", sizeof (pxi->software)) ;
33233 memset (pxi->sample_name, 0, sizeof (pxi->sample_name)) ;
33234 LSF_SNPRINTF (pxi->sample_name, sizeof (pxi->sample_name), "%s", "Sample #1") ;
33236 pxi->sample_flags = (subformat == SF_FORMAT_DPCM_16) ? 16 : 0 ;
33238 if (xi_write_header (psf, SF_FALSE))
33239 return psf->error ;
33241 psf->write_header = xi_write_header ;
33244 psf->close = xi_close ;
33245 psf->seek = dpcm_seek ;
33247 psf->sf.seekable = SF_FALSE ;
33249 psf->blockwidth = psf->bytewidth * psf->sf.channels ;
33251 switch (subformat)
33252 { case SF_FORMAT_DPCM_8 : /* 8-bit differential PCM. */
33253 case SF_FORMAT_DPCM_16 : /* 16-bit differential PCM. */
33254 error = dpcm_init (psf) ;
33255 break ;
33257 default : break ;
33260 return error ;
33261 } /* xi_open */
33263 /*------------------------------------------------------------------------------
33266 static int
33267 xi_close (SF_PRIVATE *psf)
33269 psf = psf ;
33271 return 0 ;
33272 } /* xi_close */
33274 /*==============================================================================
33277 static sf_count_t dpcm_read_dsc2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
33278 static sf_count_t dpcm_read_dsc2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
33279 static sf_count_t dpcm_read_dsc2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
33280 static sf_count_t dpcm_read_dsc2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
33282 static sf_count_t dpcm_write_s2dsc (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
33283 static sf_count_t dpcm_write_i2dsc (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
33284 static sf_count_t dpcm_write_f2dsc (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
33285 static sf_count_t dpcm_write_d2dsc (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
33287 static sf_count_t dpcm_read_dles2s (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
33288 static sf_count_t dpcm_read_dles2i (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
33289 static sf_count_t dpcm_read_dles2f (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
33290 static sf_count_t dpcm_read_dles2d (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
33292 static sf_count_t dpcm_write_s2dles (SF_PRIVATE *psf, short *ptr, sf_count_t len) ;
33293 static sf_count_t dpcm_write_i2dles (SF_PRIVATE *psf, int *ptr, sf_count_t len) ;
33294 static sf_count_t dpcm_write_f2dles (SF_PRIVATE *psf, float *ptr, sf_count_t len) ;
33295 static sf_count_t dpcm_write_d2dles (SF_PRIVATE *psf, double *ptr, sf_count_t len) ;
33297 static int
33298 dpcm_init (SF_PRIVATE *psf)
33299 { if (psf->bytewidth == 0 || psf->sf.channels == 0)
33300 return SFE_INTERNAL ;
33302 psf->blockwidth = psf->bytewidth * psf->sf.channels ;
33304 if (psf->mode == SFM_READ || psf->mode == SFM_RDWR)
33305 { switch (psf->bytewidth)
33306 { case 1 :
33307 psf->read_short = dpcm_read_dsc2s ;
33308 psf->read_int = dpcm_read_dsc2i ;
33309 psf->read_float = dpcm_read_dsc2f ;
33310 psf->read_double = dpcm_read_dsc2d ;
33311 break ;
33312 case 2 :
33313 psf->read_short = dpcm_read_dles2s ;
33314 psf->read_int = dpcm_read_dles2i ;
33315 psf->read_float = dpcm_read_dles2f ;
33316 psf->read_double = dpcm_read_dles2d ;
33317 break ;
33318 default :
33319 psf_log_printf (psf, "dpcm_init() returning SFE_UNIMPLEMENTED\n") ;
33320 return SFE_UNIMPLEMENTED ;
33324 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
33325 { switch (psf->bytewidth)
33326 { case 1 :
33327 psf->write_short = dpcm_write_s2dsc ;
33328 psf->write_int = dpcm_write_i2dsc ;
33329 psf->write_float = dpcm_write_f2dsc ;
33330 psf->write_double = dpcm_write_d2dsc ;
33331 break ;
33332 case 2 :
33333 psf->write_short = dpcm_write_s2dles ;
33334 psf->write_int = dpcm_write_i2dles ;
33335 psf->write_float = dpcm_write_f2dles ;
33336 psf->write_double = dpcm_write_d2dles ;
33337 break ;
33338 default :
33339 psf_log_printf (psf, "dpcm_init() returning SFE_UNIMPLEMENTED\n") ;
33340 return SFE_UNIMPLEMENTED ;
33344 psf->filelength = psf_get_filelen (psf) ;
33345 psf->datalength = (psf->dataend) ? psf->dataend - psf->dataoffset :
33346 psf->filelength - psf->dataoffset ;
33347 psf->sf.frames = psf->datalength / psf->blockwidth ;
33349 return 0 ;
33350 } /* dpcm_init */
33352 /*==============================================================================
33355 static sf_count_t
33356 dpcm_seek (SF_PRIVATE *psf, int mode, sf_count_t offset)
33357 { XI_PRIVATE *pxi ;
33358 int total, bufferlen, len ;
33360 if ((pxi = psf->fdata) == NULL)
33361 return SFE_INTERNAL ;
33363 if (psf->datalength < 0 || psf->dataoffset < 0)
33364 { psf->error = SFE_BAD_SEEK ;
33365 return ((sf_count_t) -1) ;
33368 if (offset == 0)
33369 { psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
33370 pxi->last_16 = 0 ;
33371 return 0 ;
33374 if (offset < 0 || offset > psf->sf.frames)
33375 { psf->error = SFE_BAD_SEEK ;
33376 return ((sf_count_t) -1) ;
33379 if (mode != SFM_READ)
33380 { /* What to do about write??? */
33381 psf->error = SFE_BAD_SEEK ;
33382 return ((sf_count_t) -1) ;
33385 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
33387 if ((psf->sf.format & SF_FORMAT_SUBMASK) == SF_FORMAT_DPCM_16)
33388 { total = offset ;
33389 bufferlen = sizeof (psf->buffer) / sizeof (short) ;
33390 while (total > 0)
33391 { len = (total > bufferlen) ? bufferlen : total ;
33392 total -= dpcm_read_dles2s (psf, (short *) psf->buffer, len) ;
33395 else
33396 { total = offset ;
33397 bufferlen = sizeof (psf->buffer) / sizeof (short) ;
33398 while (total > 0)
33399 { len = (total > bufferlen) ? bufferlen : total ;
33400 total -= dpcm_read_dsc2s (psf, (short *) psf->buffer, len) ;
33404 return offset ;
33405 } /* dpcm_seek */
33408 static int
33409 xi_write_header (SF_PRIVATE *psf, int calc_length)
33410 { XI_PRIVATE *pxi ;
33411 sf_count_t current ;
33412 const char *string ;
33414 if ((pxi = psf->fdata) == NULL)
33415 return SFE_INTERNAL ;
33417 calc_length = calc_length ; /* Avoid a compiler warning. */
33419 current = psf_ftell (psf) ;
33421 /* Reset the current header length to zero. */
33422 psf->header [0] = 0 ;
33423 psf->headindex = 0 ;
33424 psf_fseek (psf, 0, SEEK_SET) ;
33426 string = "Extended Instrument: " ;
33427 psf_binheader_writef (psf, "b", string, strlen (string)) ;
33428 psf_binheader_writef (psf, "b1", pxi->filename, sizeof (pxi->filename), 0x1A) ;
33430 /* Write software version and two byte XI version. */
33431 psf_binheader_writef (psf, "eb2", pxi->software, sizeof (pxi->software), (1 << 8) + 2) ;
33434 ** Jump note numbers (96), volume envelope (48), pan envelope (48),
33435 ** volume points (1), pan points (1)
33437 psf_binheader_writef (psf, "z", (size_t) (96 + 48 + 48 + 1 + 1)) ;
33439 /* Jump volume loop (3 bytes), pan loop (3), envelope flags (3), vibrato (3)
33440 ** fade out (2), 22 unknown bytes, and then write sample_count (2 bytes).
33442 psf_binheader_writef (psf, "ez2z2", (size_t) (4 * 3), 0x1234, (size_t) 22, 1) ;
33444 psf->sf.frames = 12 ;
33445 pxi->loop_begin = 0 ;
33446 pxi->loop_end = 0 ;
33448 psf_binheader_writef (psf, "et844", psf->sf.frames, pxi->loop_begin, pxi->loop_end) ;
33450 /* volume, fine tune, flags, pan, note, namelen */
33451 psf_binheader_writef (psf, "111111", 128, 0, pxi->sample_flags, 128, 0, strlen (pxi->sample_name)) ;
33453 psf_binheader_writef (psf, "b", pxi->sample_name, sizeof (pxi->sample_name)) ;
33459 /* Header construction complete so write it out. */
33460 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
33462 if (psf->error)
33463 return psf->error ;
33465 psf->dataoffset = psf->headindex ;
33467 if (current > 0)
33468 psf_fseek (psf, current, SEEK_SET) ;
33470 return psf->error ;
33471 } /* xi_write_header */
33473 static int
33474 xi_read_header (SF_PRIVATE *psf)
33475 { char buffer [64], name [32] ;
33476 short version, fade_out, sample_count ;
33477 int k, loop_begin, loop_end ;
33478 int sample_sizes [MAX_XI_SAMPLES] ;
33480 psf_binheader_readf (psf, "pb", 0, buffer, 21) ;
33482 memset (sample_sizes, 0, sizeof (sample_sizes)) ;
33484 buffer [20] = 0 ;
33485 if (strcmp (buffer, "Extended Instrument:") != 0)
33486 return SFE_XI_BAD_HEADER ;
33488 memset (buffer, 0, sizeof (buffer)) ;
33489 psf_binheader_readf (psf, "b", buffer, 23) ;
33491 if (buffer [22] != 0x1A)
33492 return SFE_XI_BAD_HEADER ;
33494 buffer [22] = 0 ;
33495 psf_log_printf (psf, "Extended Instrument : %s\n", buffer) ;
33497 psf_binheader_readf (psf, "be2", buffer, 20, &version) ;
33498 buffer [19] = 0 ;
33499 psf_log_printf (psf, "Software : %s\nVersion : %d.%02d\n", buffer, version / 256, version % 256) ;
33501 /* Jump note numbers (96), volume envelope (48), pan envelope (48),
33502 ** volume points (1), pan points (1)
33504 psf_binheader_readf (psf, "j", 96 + 48 + 48 + 1 + 1) ;
33506 psf_binheader_readf (psf, "b", buffer, 12) ;
33507 psf_log_printf (psf, "Volume Loop\n sustain : %u\n begin : %u\n end : %u\n",
33508 buffer [0], buffer [1], buffer [2]) ;
33509 psf_log_printf (psf, "Pan Loop\n sustain : %u\n begin : %u\n end : %u\n",
33510 buffer [3], buffer [4], buffer [5]) ;
33511 psf_log_printf (psf, "Envelope Flags\n volume : 0x%X\n pan : 0x%X\n",
33512 buffer [6] & 0xFF, buffer [7] & 0xFF) ;
33514 psf_log_printf (psf, "Vibrato\n type : %u\n sweep : %u\n depth : %u\n rate : %u\n",
33515 buffer [8], buffer [9], buffer [10], buffer [11]) ;
33518 ** Read fade_out then jump reserved (2 bytes) and ???? (20 bytes) and
33519 ** sample_count.
33521 psf_binheader_readf (psf, "e2j2", &fade_out, 2 + 20, &sample_count) ;
33522 psf_log_printf (psf, "Fade out : %d\n", fade_out) ;
33524 /* XI file can contain up to 16 samples. */
33525 if (sample_count > MAX_XI_SAMPLES)
33526 return SFE_XI_EXCESS_SAMPLES ;
33528 /* Log all data for each sample. */
33529 for (k = 0 ; k < sample_count ; k++)
33530 { psf_binheader_readf (psf, "e444", &(sample_sizes [k]), &loop_begin, &loop_end) ;
33532 /* Read 5 know bytes, 1 unknown byte and 22 name bytes. */
33533 psf_binheader_readf (psf, "bb", buffer, 6, name, 22) ;
33534 name [21] = 0 ;
33536 psf_log_printf (psf, "Sample #%d\n name : %s\n size : %d\n", k + 1, name, sample_sizes [k]) ;
33537 psf_log_printf (psf, " loop\n begin : %d\n end : %d\n", loop_begin, loop_end) ;
33539 psf_log_printf (psf, " volume : %u\n f. tune : %d\n flags : 0x%02X ",
33540 buffer [0] & 0xFF, buffer [1] & 0xFF, buffer [2] & 0xFF) ;
33542 psf_log_printf (psf, " (") ;
33543 if (buffer [2] & 1)
33544 psf_log_printf (psf, " Loop") ;
33545 if (buffer [2] & 2)
33546 psf_log_printf (psf, " PingPong") ;
33547 psf_log_printf (psf, (buffer [2] & 16) ? " 16bit" : " 8bit") ;
33548 psf_log_printf (psf, " )\n") ;
33550 psf_log_printf (psf, " pan : %u\n note : %d\n namelen : %d\n",
33551 buffer [3] & 0xFF, buffer [4], buffer [5]) ;
33553 if (k != 0)
33554 continue ;
33556 if (buffer [2] & 16)
33557 { psf->sf.format = SF_FORMAT_XI | SF_FORMAT_DPCM_16 ;
33558 psf->bytewidth = 2 ;
33560 else
33561 { psf->sf.format = SF_FORMAT_XI | SF_FORMAT_DPCM_8 ;
33562 psf->bytewidth = 1 ;
33566 while (sample_count > 1 && sample_sizes [sample_count - 1] == 0)
33567 sample_count -- ;
33569 /* Currently, we can only handle 1 sample per file. */
33571 if (sample_count > 2)
33572 { psf_log_printf (psf, "*** Sample count is less than 16 but more than 1.\n") ;
33573 psf_log_printf (psf, " sample count : %d sample_sizes [%d] : %d\n",
33574 sample_count, sample_count - 1, sample_sizes [sample_count - 1]) ;
33575 return SFE_XI_EXCESS_SAMPLES ;
33578 psf->dataoffset = psf_fseek (psf, 0, SEEK_CUR) ;
33579 psf_log_printf (psf, "Data Offset : %D\n", psf->dataoffset) ;
33581 psf->datalength = sample_sizes [0] ;
33583 if (psf->dataoffset + psf->datalength > psf->filelength)
33584 { psf_log_printf (psf, "*** File seems to be truncated. Should be at least %D bytes long.\n",
33585 psf->dataoffset + sample_sizes [0]) ;
33586 psf->datalength = psf->filelength - psf->dataoffset ;
33589 if (psf_fseek (psf, psf->dataoffset, SEEK_SET) != psf->dataoffset)
33590 return SFE_BAD_SEEK ;
33592 psf->close = xi_close ;
33594 psf->endian = SF_ENDIAN_LITTLE ;
33595 psf->sf.channels = 1 ; /* Always mono */
33596 psf->sf.samplerate = 44100 ; /* Always */
33598 psf->blockwidth = psf->sf.channels * psf->bytewidth ;
33600 if (! psf->sf.frames && psf->blockwidth)
33601 psf->sf.frames = (psf->filelength - psf->dataoffset) / psf->blockwidth ;
33603 return 0 ;
33604 } /* xi_read_header */
33606 /*==============================================================================
33609 static void dsc2s_array (XI_PRIVATE *pxi, signed char *src, int count, short *dest) ;
33610 static void dsc2i_array (XI_PRIVATE *pxi, signed char *src, int count, int *dest) ;
33611 static void dsc2f_array (XI_PRIVATE *pxi, signed char *src, int count, float *dest, float normfact) ;
33612 static void dsc2d_array (XI_PRIVATE *pxi, signed char *src, int count, double *dest, double normfact) ;
33614 static void dles2s_array (XI_PRIVATE *pxi, short *src, int count, short *dest) ;
33615 static void dles2i_array (XI_PRIVATE *pxi, short *src, int count, int *dest) ;
33616 static void dles2f_array (XI_PRIVATE *pxi, short *src, int count, float *dest, float normfact) ;
33617 static void dles2d_array (XI_PRIVATE *pxi, short *src, int count, double *dest, double normfact) ;
33619 static sf_count_t
33620 dpcm_read_dsc2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
33621 { XI_PRIVATE *pxi ;
33622 int bufferlen, readcount, thisread ;
33623 sf_count_t total = 0 ;
33625 if ((pxi = psf->fdata) == NULL)
33626 return 0 ;
33628 bufferlen = sizeof (psf->buffer) / sizeof (signed char) ;
33630 while (len > 0)
33631 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
33632 thisread = psf_fread (psf->buffer, sizeof (signed char), readcount, psf) ;
33633 dsc2s_array (pxi, (signed char*) (psf->buffer), thisread, ptr + total) ;
33634 total += thisread ;
33635 len -= thisread ;
33636 if (thisread < readcount)
33637 break ;
33640 return total ;
33641 } /* dpcm_read_dsc2s */
33643 static sf_count_t
33644 dpcm_read_dsc2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
33645 { XI_PRIVATE *pxi ;
33646 int bufferlen, readcount, thisread ;
33647 sf_count_t total = 0 ;
33649 if ((pxi = psf->fdata) == NULL)
33650 return 0 ;
33652 bufferlen = sizeof (psf->buffer) / sizeof (signed char) ;
33654 while (len > 0)
33655 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
33656 thisread = psf_fread (psf->buffer, sizeof (signed char), readcount, psf) ;
33657 dsc2i_array (pxi, (signed char*) (psf->buffer), thisread, ptr + total) ;
33658 total += thisread ;
33659 len -= thisread ;
33660 if (thisread < readcount)
33661 break ;
33664 return total ;
33665 } /* dpcm_read_dsc2i */
33667 static sf_count_t
33668 dpcm_read_dsc2f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
33669 { XI_PRIVATE *pxi ;
33670 int bufferlen, readcount, thisread ;
33671 sf_count_t total = 0 ;
33672 float normfact ;
33674 if ((pxi = psf->fdata) == NULL)
33675 return 0 ;
33677 normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x80) : 1.0 ;
33679 bufferlen = sizeof (psf->buffer) / sizeof (signed char) ;
33681 while (len > 0)
33682 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
33683 thisread = psf_fread (psf->buffer, sizeof (signed char), readcount, psf) ;
33684 dsc2f_array (pxi, (signed char*) (psf->buffer), thisread, ptr + total, normfact) ;
33685 total += thisread ;
33686 len -= thisread ;
33687 if (thisread < readcount)
33688 break ;
33691 return total ;
33692 } /* dpcm_read_dsc2f */
33694 static sf_count_t
33695 dpcm_read_dsc2d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
33696 { XI_PRIVATE *pxi ;
33697 int bufferlen, readcount, thisread ;
33698 sf_count_t total = 0 ;
33699 double normfact ;
33701 if ((pxi = psf->fdata) == NULL)
33702 return 0 ;
33704 normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x80) : 1.0 ;
33706 bufferlen = sizeof (psf->buffer) / sizeof (signed char) ;
33708 while (len > 0)
33709 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
33710 thisread = psf_fread (psf->buffer, sizeof (signed char), readcount, psf) ;
33711 dsc2d_array (pxi, (signed char*) (psf->buffer), thisread, ptr + total, normfact) ;
33712 total += thisread ;
33713 len -= thisread ;
33714 if (thisread < readcount)
33715 break ;
33718 return total ;
33719 } /* dpcm_read_dsc2d */
33721 /*------------------------------------------------------------------------------
33724 static sf_count_t
33725 dpcm_read_dles2s (SF_PRIVATE *psf, short *ptr, sf_count_t len)
33726 { XI_PRIVATE *pxi ;
33727 int bufferlen, readcount, thisread ;
33728 sf_count_t total = 0 ;
33730 if ((pxi = psf->fdata) == NULL)
33731 return 0 ;
33733 bufferlen = sizeof (psf->buffer) / sizeof (short) ;
33735 while (len > 0)
33736 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
33737 thisread = psf_fread (psf->buffer, sizeof (short), readcount, psf) ;
33738 dles2s_array (pxi, (short*) (psf->buffer), thisread, ptr + total) ;
33739 total += thisread ;
33740 len -= thisread ;
33741 if (thisread < readcount)
33742 break ;
33745 return total ;
33746 } /* dpcm_read_dles2s */
33748 static sf_count_t
33749 dpcm_read_dles2i (SF_PRIVATE *psf, int *ptr, sf_count_t len)
33750 { XI_PRIVATE *pxi ;
33751 int bufferlen, readcount, thisread ;
33752 sf_count_t total = 0 ;
33754 if ((pxi = psf->fdata) == NULL)
33755 return 0 ;
33757 bufferlen = sizeof (psf->buffer) / sizeof (short) ;
33759 while (len > 0)
33760 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
33761 thisread = psf_fread (psf->buffer, sizeof (short), readcount, psf) ;
33762 dles2i_array (pxi, (short*) (psf->buffer), thisread, ptr + total) ;
33763 total += thisread ;
33764 len -= thisread ;
33765 if (thisread < readcount)
33766 break ;
33769 return total ;
33770 } /* dpcm_read_dles2i */
33772 static sf_count_t
33773 dpcm_read_dles2f (SF_PRIVATE *psf, float *ptr, sf_count_t len)
33774 { XI_PRIVATE *pxi ;
33775 int bufferlen, readcount, thisread ;
33776 sf_count_t total = 0 ;
33777 float normfact ;
33779 if ((pxi = psf->fdata) == NULL)
33780 return 0 ;
33782 normfact = (psf->norm_float == SF_TRUE) ? 1.0 / ((float) 0x8000) : 1.0 ;
33784 bufferlen = sizeof (psf->buffer) / sizeof (short) ;
33786 while (len > 0)
33787 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
33788 thisread = psf_fread (psf->buffer, sizeof (short), readcount, psf) ;
33789 dles2f_array (pxi, (short*) (psf->buffer), thisread, ptr + total, normfact) ;
33790 total += thisread ;
33791 len -= thisread ;
33792 if (thisread < readcount)
33793 break ;
33796 return total ;
33797 } /* dpcm_read_dles2f */
33799 static sf_count_t
33800 dpcm_read_dles2d (SF_PRIVATE *psf, double *ptr, sf_count_t len)
33801 { XI_PRIVATE *pxi ;
33802 int bufferlen, readcount, thisread ;
33803 sf_count_t total = 0 ;
33804 double normfact ;
33806 if ((pxi = psf->fdata) == NULL)
33807 return 0 ;
33809 normfact = (psf->norm_double == SF_TRUE) ? 1.0 / ((double) 0x8000) : 1.0 ;
33811 bufferlen = sizeof (psf->buffer) / sizeof (short) ;
33813 while (len > 0)
33814 { readcount = (len >= bufferlen) ? bufferlen : (int) len ;
33815 thisread = psf_fread (psf->buffer, sizeof (short), readcount, psf) ;
33816 dles2d_array (pxi, (short*) (psf->buffer), thisread, ptr + total, normfact) ;
33817 total += thisread ;
33818 len -= thisread ;
33819 if (thisread < readcount)
33820 break ;
33823 return total ;
33824 } /* dpcm_read_dles2d */
33826 /*==============================================================================
33829 static void s2dsc_array (XI_PRIVATE *pxi, short *src, signed char *dest, int count) ;
33830 static void i2dsc_array (XI_PRIVATE *pxi, int *src, signed char *dest, int count) ;
33831 static void f2dsc_array (XI_PRIVATE *pxi, float *src, signed char *dest, int count, float normfact) ;
33832 static void d2dsc_array (XI_PRIVATE *pxi, double *src, signed char *dest, int count, double normfact) ;
33834 static void s2dles_array (XI_PRIVATE *pxi, short *src, short *dest, int count) ;
33835 static void i2dles_array (XI_PRIVATE *pxi, int *src, short *dest, int count) ;
33836 static void f2dles_array (XI_PRIVATE *pxi, float *src, short *dest, int count, float normfact) ;
33837 static void d2dles_array (XI_PRIVATE *pxi, double *src, short *dest, int count, double normfact) ;
33840 static sf_count_t
33841 dpcm_write_s2dsc (SF_PRIVATE *psf, short *ptr, sf_count_t len)
33842 { XI_PRIVATE *pxi ;
33843 int bufferlen, writecount, thiswrite ;
33844 sf_count_t total = 0 ;
33846 if ((pxi = psf->fdata) == NULL)
33847 return 0 ;
33849 bufferlen = sizeof (psf->buffer) / sizeof (signed char) ;
33851 while (len > 0)
33852 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
33853 s2dsc_array (pxi, ptr + total, (signed char*) (psf->buffer), writecount) ;
33854 thiswrite = psf_fwrite (psf->buffer, sizeof (signed char), writecount, psf) ;
33855 total += thiswrite ;
33856 len -= thiswrite ;
33857 if (thiswrite < writecount)
33858 break ;
33861 return total ;
33862 } /* dpcm_write_s2dsc */
33864 static sf_count_t
33865 dpcm_write_i2dsc (SF_PRIVATE *psf, int *ptr, sf_count_t len)
33866 { XI_PRIVATE *pxi ;
33867 int bufferlen, writecount, thiswrite ;
33868 sf_count_t total = 0 ;
33870 if ((pxi = psf->fdata) == NULL)
33871 return 0 ;
33873 bufferlen = sizeof (psf->buffer) / sizeof (signed char) ;
33875 while (len > 0)
33876 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
33877 i2dsc_array (pxi, ptr + total, (signed char*) (psf->buffer), writecount) ;
33878 thiswrite = psf_fwrite (psf->buffer, sizeof (signed char), writecount, psf) ;
33879 total += thiswrite ;
33880 len -= thiswrite ;
33881 if (thiswrite < writecount)
33882 break ;
33885 return total ;
33886 } /* dpcm_write_i2dsc */
33888 static sf_count_t
33889 dpcm_write_f2dsc (SF_PRIVATE *psf, float *ptr, sf_count_t len)
33890 { XI_PRIVATE *pxi ;
33891 int bufferlen, writecount, thiswrite ;
33892 sf_count_t total = 0 ;
33893 float normfact ;
33895 if ((pxi = psf->fdata) == NULL)
33896 return 0 ;
33898 normfact = (psf->norm_float == SF_TRUE) ? (1.0 * 0x7F) : 1.0 ;
33900 bufferlen = sizeof (psf->buffer) / sizeof (signed char) ;
33902 while (len > 0)
33903 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
33904 f2dsc_array (pxi, ptr + total, (signed char*) (psf->buffer), writecount, normfact) ;
33905 thiswrite = psf_fwrite (psf->buffer, sizeof (signed char), writecount, psf) ;
33906 total += thiswrite ;
33907 len -= thiswrite ;
33908 if (thiswrite < writecount)
33909 break ;
33912 return total ;
33913 } /* dpcm_write_f2dsc */
33915 static sf_count_t
33916 dpcm_write_d2dsc (SF_PRIVATE *psf, double *ptr, sf_count_t len)
33917 { XI_PRIVATE *pxi ;
33918 int bufferlen, writecount, thiswrite ;
33919 sf_count_t total = 0 ;
33920 double normfact ;
33922 if ((pxi = psf->fdata) == NULL)
33923 return 0 ;
33925 normfact = (psf->norm_double == SF_TRUE) ? (1.0 * 0x7F) : 1.0 ;
33927 bufferlen = sizeof (psf->buffer) / sizeof (signed char) ;
33929 while (len > 0)
33930 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
33931 d2dsc_array (pxi, ptr + total, (signed char*) (psf->buffer), writecount, normfact) ;
33932 thiswrite = psf_fwrite (psf->buffer, sizeof (signed char), writecount, psf) ;
33933 total += thiswrite ;
33934 len -= thiswrite ;
33935 if (thiswrite < writecount)
33936 break ;
33939 return total ;
33940 } /* dpcm_write_d2dsc */
33943 static sf_count_t
33944 dpcm_write_s2dles (SF_PRIVATE *psf, short *ptr, sf_count_t len)
33945 { XI_PRIVATE *pxi ;
33946 int bufferlen, writecount, thiswrite ;
33947 sf_count_t total = 0 ;
33949 if ((pxi = psf->fdata) == NULL)
33950 return 0 ;
33952 bufferlen = sizeof (psf->buffer) / sizeof (short) ;
33954 while (len > 0)
33955 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
33956 s2dles_array (pxi, ptr + total, (short*) (psf->buffer), writecount) ;
33957 thiswrite = psf_fwrite (psf->buffer, sizeof (short), writecount, psf) ;
33958 total += thiswrite ;
33959 len -= thiswrite ;
33960 if (thiswrite < writecount)
33961 break ;
33964 return total ;
33965 } /* dpcm_write_s2dles */
33967 static sf_count_t
33968 dpcm_write_i2dles (SF_PRIVATE *psf, int *ptr, sf_count_t len)
33969 { XI_PRIVATE *pxi ;
33970 int bufferlen, writecount, thiswrite ;
33971 sf_count_t total = 0 ;
33973 if ((pxi = psf->fdata) == NULL)
33974 return 0 ;
33976 bufferlen = sizeof (psf->buffer) / sizeof (short) ;
33978 while (len > 0)
33979 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
33980 i2dles_array (pxi, ptr + total, (short*) (psf->buffer), writecount) ;
33981 thiswrite = psf_fwrite (psf->buffer, sizeof (short), writecount, psf) ;
33982 total += thiswrite ;
33983 len -= thiswrite ;
33984 if (thiswrite < writecount)
33985 break ;
33988 return total ;
33989 } /* dpcm_write_i2dles */
33991 static sf_count_t
33992 dpcm_write_f2dles (SF_PRIVATE *psf, float *ptr, sf_count_t len)
33993 { XI_PRIVATE *pxi ;
33994 int bufferlen, writecount, thiswrite ;
33995 sf_count_t total = 0 ;
33996 float normfact ;
33998 if ((pxi = psf->fdata) == NULL)
33999 return 0 ;
34001 normfact = (psf->norm_float == SF_TRUE) ? (1.0 * 0x7FFF) : 1.0 ;
34003 bufferlen = sizeof (psf->buffer) / sizeof (short) ;
34005 while (len > 0)
34006 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
34007 f2dles_array (pxi, ptr + total, (short*) (psf->buffer), writecount, normfact) ;
34008 thiswrite = psf_fwrite (psf->buffer, sizeof (short), writecount, psf) ;
34009 total += thiswrite ;
34010 len -= thiswrite ;
34011 if (thiswrite < writecount)
34012 break ;
34015 return total ;
34016 } /* dpcm_write_f2dles */
34018 static sf_count_t
34019 dpcm_write_d2dles (SF_PRIVATE *psf, double *ptr, sf_count_t len)
34020 { XI_PRIVATE *pxi ;
34021 int bufferlen, writecount, thiswrite ;
34022 sf_count_t total = 0 ;
34023 double normfact ;
34025 if ((pxi = psf->fdata) == NULL)
34026 return 0 ;
34028 normfact = (psf->norm_double == SF_TRUE) ? (1.0 * 0x7FFF) : 1.0 ;
34030 bufferlen = sizeof (psf->buffer) / sizeof (short) ;
34032 while (len > 0)
34033 { writecount = (len >= bufferlen) ? bufferlen : (int) len ;
34034 d2dles_array (pxi, ptr + total, (short*) (psf->buffer), writecount, normfact) ;
34035 thiswrite = psf_fwrite (psf->buffer, sizeof (short), writecount, psf) ;
34036 total += thiswrite ;
34037 len -= thiswrite ;
34038 if (thiswrite < writecount)
34039 break ;
34042 return total ;
34043 } /* dpcm_write_d2dles */
34046 /*==============================================================================
34049 static void
34050 dsc2s_array (XI_PRIVATE *pxi, signed char *src, int count, short *dest)
34051 { signed char last_val ;
34052 int k ;
34054 last_val = pxi->last_16 >> 8 ;
34056 for (k = 0 ; k < count ; k++)
34057 { last_val += src [k] ;
34058 dest [k] = last_val << 8 ;
34061 pxi->last_16 = last_val << 8 ;
34062 } /* dsc2s_array */
34064 static void
34065 dsc2i_array (XI_PRIVATE *pxi, signed char *src, int count, int *dest)
34066 { signed char last_val ;
34067 int k ;
34069 last_val = pxi->last_16 >> 8 ;
34071 for (k = 0 ; k < count ; k++)
34072 { last_val += src [k] ;
34073 dest [k] = last_val << 24 ;
34076 pxi->last_16 = last_val << 8 ;
34077 } /* dsc2i_array */
34079 static void
34080 dsc2f_array (XI_PRIVATE *pxi, signed char *src, int count, float *dest, float normfact)
34081 { signed char last_val ;
34082 int k ;
34084 last_val = pxi->last_16 >> 8 ;
34086 for (k = 0 ; k < count ; k++)
34087 { last_val += src [k] ;
34088 dest [k] = last_val * normfact ;
34091 pxi->last_16 = last_val << 8 ;
34092 } /* dsc2f_array */
34094 static void
34095 dsc2d_array (XI_PRIVATE *pxi, signed char *src, int count, double *dest, double normfact)
34096 { signed char last_val ;
34097 int k ;
34099 last_val = pxi->last_16 >> 8 ;
34101 for (k = 0 ; k < count ; k++)
34102 { last_val += src [k] ;
34103 dest [k] = last_val * normfact ;
34106 pxi->last_16 = last_val << 8 ;
34107 } /* dsc2d_array */
34109 /*------------------------------------------------------------------------------
34112 static void
34113 s2dsc_array (XI_PRIVATE *pxi, short *src, signed char *dest, int count)
34114 { signed char last_val, current ;
34115 int k ;
34117 last_val = pxi->last_16 >> 8 ;
34119 for (k = 0 ; k < count ; k++)
34120 { current = src [k] >> 8 ;
34121 dest [k] = current - last_val ;
34122 last_val = current ;
34125 pxi->last_16 = last_val << 8 ;
34126 } /* s2dsc_array */
34128 static void
34129 i2dsc_array (XI_PRIVATE *pxi, int *src, signed char *dest, int count)
34130 { signed char last_val, current ;
34131 int k ;
34133 last_val = pxi->last_16 >> 8 ;
34135 for (k = 0 ; k < count ; k++)
34136 { current = src [k] >> 24 ;
34137 dest [k] = current - last_val ;
34138 last_val = current ;
34141 pxi->last_16 = last_val << 8 ;
34142 } /* i2dsc_array */
34144 static void
34145 f2dsc_array (XI_PRIVATE *pxi, float *src, signed char *dest, int count, float normfact)
34146 { signed char last_val, current ;
34147 int k ;
34149 last_val = pxi->last_16 >> 8 ;
34151 for (k = 0 ; k < count ; k++)
34152 { current = lrintf (src [k] * normfact) ;
34153 dest [k] = current - last_val ;
34154 last_val = current ;
34157 pxi->last_16 = last_val << 8 ;
34158 } /* f2dsc_array */
34160 static void
34161 d2dsc_array (XI_PRIVATE *pxi, double *src, signed char *dest, int count, double normfact)
34162 { signed char last_val, current ;
34163 int k ;
34165 last_val = pxi->last_16 >> 8 ;
34167 for (k = 0 ; k < count ; k++)
34168 { current = lrint (src [k] * normfact) ;
34169 dest [k] = current - last_val ;
34170 last_val = current ;
34173 pxi->last_16 = last_val << 8 ;
34174 } /* d2dsc_array */
34176 /*==============================================================================
34179 static void
34180 dles2s_array (XI_PRIVATE *pxi, short *src, int count, short *dest)
34181 { short last_val ;
34182 int k ;
34184 last_val = pxi->last_16 ;
34186 for (k = 0 ; k < count ; k++)
34187 { last_val += LES2H_SHORT (src [k]) ;
34188 dest [k] = last_val ;
34191 pxi->last_16 = last_val ;
34192 } /* dles2s_array */
34194 static void
34195 dles2i_array (XI_PRIVATE *pxi, short *src, int count, int *dest)
34196 { short last_val ;
34197 int k ;
34199 last_val = pxi->last_16 ;
34201 for (k = 0 ; k < count ; k++)
34202 { last_val += LES2H_SHORT (src [k]) ;
34203 dest [k] = last_val << 16 ;
34206 pxi->last_16 = last_val ;
34207 } /* dles2i_array */
34209 static void
34210 dles2f_array (XI_PRIVATE *pxi, short *src, int count, float *dest, float normfact)
34211 { short last_val ;
34212 int k ;
34214 last_val = pxi->last_16 ;
34216 for (k = 0 ; k < count ; k++)
34217 { last_val += LES2H_SHORT (src [k]) ;
34218 dest [k] = last_val * normfact ;
34221 pxi->last_16 = last_val ;
34222 } /* dles2f_array */
34224 static void
34225 dles2d_array (XI_PRIVATE *pxi, short *src, int count, double *dest, double normfact)
34226 { short last_val ;
34227 int k ;
34229 last_val = pxi->last_16 ;
34231 for (k = 0 ; k < count ; k++)
34232 { last_val += LES2H_SHORT (src [k]) ;
34233 dest [k] = last_val * normfact ;
34236 pxi->last_16 = last_val ;
34237 } /* dles2d_array */
34239 /*------------------------------------------------------------------------------
34242 static void
34243 s2dles_array (XI_PRIVATE *pxi, short *src, short *dest, int count)
34244 { short diff, last_val ;
34245 int k ;
34247 last_val = pxi->last_16 ;
34249 for (k = 0 ; k < count ; k++)
34250 { diff = src [k] - last_val ;
34251 dest [k] = LES2H_SHORT (diff) ;
34252 last_val = src [k] ;
34255 pxi->last_16 = last_val ;
34256 } /* s2dles_array */
34258 static void
34259 i2dles_array (XI_PRIVATE *pxi, int *src, short *dest, int count)
34260 { short diff, last_val ;
34261 int k ;
34263 last_val = pxi->last_16 ;
34265 for (k = 0 ; k < count ; k++)
34266 { diff = (src [k] >> 16) - last_val ;
34267 dest [k] = LES2H_SHORT (diff) ;
34268 last_val = src [k] >> 16 ;
34271 pxi->last_16 = last_val ;
34272 } /* i2dles_array */
34274 static void
34275 f2dles_array (XI_PRIVATE *pxi, float *src, short *dest, int count, float normfact)
34276 { short diff, last_val, current ;
34277 int k ;
34279 last_val = pxi->last_16 ;
34281 for (k = 0 ; k < count ; k++)
34282 { current = lrintf (src [k] * normfact) ;
34283 diff = current - last_val ;
34284 dest [k] = LES2H_SHORT (diff) ;
34285 last_val = current ;
34288 pxi->last_16 = last_val ;
34289 } /* f2dles_array */
34291 static void
34292 d2dles_array (XI_PRIVATE *pxi, double *src, short *dest, int count, double normfact)
34293 { short diff, last_val, current ;
34294 int k ;
34296 last_val = pxi->last_16 ;
34298 for (k = 0 ; k < count ; k++)
34299 { current = lrint (src [k] * normfact) ;
34300 diff = current - last_val ;
34301 dest [k] = LES2H_SHORT (diff) ;
34302 last_val = current ;
34305 pxi->last_16 = last_val ;
34306 } /* d2dles_array */
34309 ** Do not edit or modify anything in this comment block.
34310 ** The arch-tag line is a file identity tag for the GNU Arch
34311 ** revision control system.
34313 ** arch-tag: 1ab2dbe0-29af-4d80-9c6f-cb21b67521bc