2 ** Copyright (C) 1999-2006 Erik de Castro Lopo <erikd@mega-nerd.com>
3 ** Copyright (C) 2004-2005 David Viens <davidv@plogue.com>
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU Lesser General Public License as published by
7 ** the Free Software Foundation; either version 2.1 of the License, or
8 ** (at your option) any later version.
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU Lesser General Public License for more details.
15 ** You should have received a copy of the GNU Lesser General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
33 /*------------------------------------------------------------------------------
34 * Macros to handle big/little endian issues.
37 #define RIFF_MARKER (MAKE_MARKER ('R', 'I', 'F', 'F'))
38 #define RIFX_MARKER (MAKE_MARKER ('R', 'I', 'F', 'X'))
39 #define WAVE_MARKER (MAKE_MARKER ('W', 'A', 'V', 'E'))
40 #define fmt_MARKER (MAKE_MARKER ('f', 'm', 't', ' '))
41 #define data_MARKER (MAKE_MARKER ('d', 'a', 't', 'a'))
42 #define fact_MARKER (MAKE_MARKER ('f', 'a', 'c', 't'))
43 #define PEAK_MARKER (MAKE_MARKER ('P', 'E', 'A', 'K'))
45 #define cue_MARKER (MAKE_MARKER ('c', 'u', 'e', ' '))
46 #define LIST_MARKER (MAKE_MARKER ('L', 'I', 'S', 'T'))
47 #define slnt_MARKER (MAKE_MARKER ('s', 'l', 'n', 't'))
48 #define wavl_MARKER (MAKE_MARKER ('w', 'a', 'v', 'l'))
49 #define INFO_MARKER (MAKE_MARKER ('I', 'N', 'F', 'O'))
50 #define plst_MARKER (MAKE_MARKER ('p', 'l', 's', 't'))
51 #define adtl_MARKER (MAKE_MARKER ('a', 'd', 't', 'l'))
52 #define labl_MARKER (MAKE_MARKER ('l', 'a', 'b', 'l'))
53 #define ltxt_MARKER (MAKE_MARKER ('l', 't', 'x', 't'))
54 #define note_MARKER (MAKE_MARKER ('n', 'o', 't', 'e'))
55 #define smpl_MARKER (MAKE_MARKER ('s', 'm', 'p', 'l'))
56 #define bext_MARKER (MAKE_MARKER ('b', 'e', 'x', 't'))
57 #define levl_MARKER (MAKE_MARKER ('l', 'e', 'v', 'l'))
58 #define MEXT_MARKER (MAKE_MARKER ('M', 'E', 'X', 'T'))
59 #define DISP_MARKER (MAKE_MARKER ('D', 'I', 'S', 'P'))
60 #define acid_MARKER (MAKE_MARKER ('a', 'c', 'i', 'd'))
61 #define strc_MARKER (MAKE_MARKER ('s', 't', 'r', 'c'))
62 #define PAD_MARKER (MAKE_MARKER ('P', 'A', 'D', ' '))
63 #define afsp_MARKER (MAKE_MARKER ('a', 'f', 's', 'p'))
64 #define clm_MARKER (MAKE_MARKER ('c', 'l', 'm', ' '))
65 #define elmo_MARKER (MAKE_MARKER ('e', 'l', 'm', 'o'))
67 #define ISFT_MARKER (MAKE_MARKER ('I', 'S', 'F', 'T'))
68 #define ICRD_MARKER (MAKE_MARKER ('I', 'C', 'R', 'D'))
69 #define ICOP_MARKER (MAKE_MARKER ('I', 'C', 'O', 'P'))
70 #define IARL_MARKER (MAKE_MARKER ('I', 'A', 'R', 'L'))
71 #define IART_MARKER (MAKE_MARKER ('I', 'A', 'R', 'T'))
72 #define INAM_MARKER (MAKE_MARKER ('I', 'N', 'A', 'M'))
73 #define IENG_MARKER (MAKE_MARKER ('I', 'E', 'N', 'G'))
74 #define IART_MARKER (MAKE_MARKER ('I', 'A', 'R', 'T'))
75 #define ICOP_MARKER (MAKE_MARKER ('I', 'C', 'O', 'P'))
76 #define IPRD_MARKER (MAKE_MARKER ('I', 'P', 'R', 'D'))
77 #define ISRC_MARKER (MAKE_MARKER ('I', 'S', 'R', 'C'))
78 #define ISBJ_MARKER (MAKE_MARKER ('I', 'S', 'B', 'J'))
79 #define ICMT_MARKER (MAKE_MARKER ('I', 'C', 'M', 'T'))
81 /* Weird WAVPACK marker which can show up at the start of the DATA section. */
82 #define wvpk_MARKER (MAKE_MARKER ('w', 'v', 'p', 'k'))
83 #define OggS_MARKER (MAKE_MARKER ('O', 'g', 'g', 'S'))
85 #define WAV_PEAK_CHUNK_SIZE(ch) (2 * sizeof (int) + ch * (sizeof (float) + sizeof (int)))
86 #define WAV_BEXT_CHUNK_SIZE 602
95 HAVE_other = 0x80000000
100 /* known WAVEFORMATEXTENSIBLE GUIDS */
101 static const EXT_SUBFORMAT MSGUID_SUBTYPE_PCM =
102 { 0x00000001, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }
105 static const EXT_SUBFORMAT MSGUID_SUBTYPE_MS_ADPCM =
106 { 0x00000002, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }
109 static const EXT_SUBFORMAT MSGUID_SUBTYPE_IEEE_FLOAT =
110 { 0x00000003, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }
113 static const EXT_SUBFORMAT MSGUID_SUBTYPE_ALAW =
114 { 0x00000006, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }
117 static const EXT_SUBFORMAT MSGUID_SUBTYPE_MULAW =
118 { 0x00000007, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }
122 ** the next two are from
123 ** http://dream.cs.bath.ac.uk/researchdev/wave-ex/bformat.html
125 static const EXT_SUBFORMAT MSGUID_SUBTYPE_AMBISONIC_B_FORMAT_PCM =
126 { 0x00000001, 0x0721, 0x11d3, { 0x86, 0x44, 0xC8, 0xC1, 0xCA, 0x00, 0x00, 0x00 }
129 static const EXT_SUBFORMAT MSGUID_SUBTYPE_AMBISONIC_B_FORMAT_IEEE_FLOAT =
130 { 0x00000003, 0x0721, 0x11d3, { 0x86, 0x44, 0xC8, 0xC1, 0xCA, 0x00, 0x00, 0x00 }
135 /* maybe interesting one day to read the following through sf_read_raw */
136 /* http://www.bath.ac.uk/~masrwd/pvocex/pvocex.html */
137 static const EXT_SUBFORMAT MSGUID_SUBTYPE_PVOCEX =
138 { 0x8312B9C2, 0x2E6E, 0x11d4, { 0xA8, 0x24, 0xDE, 0x5B, 0x96, 0xC3, 0xAB, 0x21 }
142 /*------------------------------------------------------------------------------
143 ** Private static functions.
146 static int wav_read_header (SF_PRIVATE *psf, int *blockalign, int *framesperblock) ;
147 static int wav_write_header (SF_PRIVATE *psf, int calc_length) ;
149 static int wavex_write_header (SF_PRIVATE *psf, int calc_length) ;
151 static int wav_write_tailer (SF_PRIVATE *psf) ;
152 static void wav_write_strings (SF_PRIVATE *psf, int location) ;
153 static int wav_command (SF_PRIVATE *psf, int command, void *data, int datasize) ;
154 static int wav_close (SF_PRIVATE *psf) ;
156 static int wav_subchunk_parse (SF_PRIVATE *psf, int chunk) ;
157 static int wav_read_smpl_chunk (SF_PRIVATE *psf, unsigned int chunklen) ;
158 static int wav_read_acid_chunk (SF_PRIVATE *psf, unsigned int chunklen) ;
159 static int wav_read_bext_chunk (SF_PRIVATE *psf, unsigned int chunklen) ;
160 static int wav_write_bext_chunk (SF_PRIVATE *psf) ;
162 /*------------------------------------------------------------------------------
167 wav_open (SF_PRIVATE *psf)
168 { int format, subformat, error, blockalign = 0, framesperblock = 0 ;
170 if (psf->mode == SFM_READ || (psf->mode == SFM_RDWR && psf->filelength > 0))
171 { if ((error = wav_read_header (psf, &blockalign, &framesperblock)))
175 subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
177 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
179 return SFE_NO_PIPE_WRITE ;
181 format = psf->sf.format & SF_FORMAT_TYPEMASK ;
182 if (format != SF_FORMAT_WAV && format != SF_FORMAT_WAVEX)
183 return SFE_BAD_OPEN_FORMAT ;
185 psf->blockwidth = psf->bytewidth * psf->sf.channels ;
187 /* RIFF WAVs are little-endian, RIFX WAVs are big-endian, default to little */
188 psf->endian = psf->sf.format & SF_FORMAT_ENDMASK ;
189 if (CPU_IS_BIG_ENDIAN && psf->endian == SF_ENDIAN_CPU)
190 psf->endian = SF_ENDIAN_BIG ;
191 else if (psf->endian != SF_ENDIAN_BIG)
192 psf->endian = SF_ENDIAN_LITTLE ;
194 if (psf->mode != SFM_RDWR || psf->filelength < 44)
195 { psf->filelength = 0 ;
196 psf->datalength = 0 ;
197 psf->dataoffset = 0 ;
201 if (subformat == SF_FORMAT_IMA_ADPCM || subformat == SF_FORMAT_MS_ADPCM)
202 { blockalign = wav_w64_srate2blocksize (psf->sf.samplerate * psf->sf.channels) ;
203 framesperblock = -1 ; /* Corrected later. */
206 psf->str_flags = SF_STR_ALLOW_START | SF_STR_ALLOW_END ;
208 /* By default, add the peak chunk to floating point files. Default behaviour
209 ** can be switched off using sf_command (SFC_SET_PEAK_CHUNK, SF_FALSE).
211 if (psf->mode == SFM_WRITE && (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE))
212 { if ((psf->peak_info = peak_info_calloc (psf->sf.channels)) == NULL)
213 return SFE_MALLOC_FAILED ;
214 psf->peak_info->peak_loc = SF_PEAK_START ;
217 psf->write_header = (format == SF_FORMAT_WAV) ? wav_write_header : wavex_write_header ;
220 psf->container_close = wav_close ;
221 psf->command = wav_command ;
224 { case SF_FORMAT_PCM_U8 :
225 case SF_FORMAT_PCM_16 :
226 case SF_FORMAT_PCM_24 :
227 case SF_FORMAT_PCM_32 :
228 error = pcm_init (psf) ;
231 case SF_FORMAT_ULAW :
232 error = ulaw_init (psf) ;
235 case SF_FORMAT_ALAW :
236 error = alaw_init (psf) ;
239 /* Lite remove start */
240 case SF_FORMAT_FLOAT :
241 error = float32_init (psf) ;
244 case SF_FORMAT_DOUBLE :
245 error = double64_init (psf) ;
248 case SF_FORMAT_IMA_ADPCM :
249 error = wav_w64_ima_init (psf, blockalign, framesperblock) ;
252 case SF_FORMAT_MS_ADPCM :
253 error = wav_w64_msadpcm_init (psf, blockalign, framesperblock) ;
256 case SF_FORMAT_G721_32 :
257 error = g72x_init (psf) ;
259 /* Lite remove end */
261 case SF_FORMAT_GSM610 :
262 error = gsm610_init (psf) ;
265 default : return SFE_UNIMPLEMENTED ;
268 if (psf->mode == SFM_WRITE || (psf->mode == SFM_RDWR && psf->filelength == 0))
269 return psf->write_header (psf, SF_FALSE) ;
274 /*=========================================================================
275 ** Private functions.
279 wav_read_header (SF_PRIVATE *psf, int *blockalign, int *framesperblock)
281 FACT_CHUNK fact_chunk ;
282 unsigned dword = 0, marker, RIFFsize, done = 0 ;
283 int parsestage = 0, error, format = 0 ;
286 memset (&wav_fmt, 0, sizeof (wav_fmt)) ;
287 /* Set position to start of file to begin reading header. */
288 psf_binheader_readf (psf, "p", 0) ;
291 { psf_binheader_readf (psf, "m", &marker) ;
297 return SFE_WAV_NO_RIFF ;
299 parsestage |= HAVE_RIFF ;
301 /* RIFX signifies big-endian format for all header and data
302 ** to prevent lots of code copying here, we'll set the psf->rwf_endian
303 ** flag once here, and never specify endian-ness for all other header ops
305 if (marker == RIFF_MARKER)
306 psf->rwf_endian = SF_ENDIAN_LITTLE ;
308 psf->rwf_endian = SF_ENDIAN_BIG ;
310 psf_binheader_readf (psf, "4", &RIFFsize) ;
312 if (psf->fileoffset > 0 && psf->filelength > RIFFsize + 8)
313 { /* Set file length. */
314 psf->filelength = RIFFsize + 8 ;
315 if (marker == RIFF_MARKER)
316 psf_log_printf (psf, "RIFF : %u\n", RIFFsize) ;
318 psf_log_printf (psf, "RIFX : %u\n", RIFFsize) ;
320 else if (psf->filelength < RIFFsize + 2 * SIGNED_SIZEOF (dword))
321 { if (marker == RIFF_MARKER)
322 psf_log_printf (psf, "RIFF : %u (should be %D)\n", RIFFsize, psf->filelength - 2 * SIGNED_SIZEOF (dword)) ;
324 psf_log_printf (psf, "RIFX : %u (should be %D)\n", RIFFsize, psf->filelength - 2 * SIGNED_SIZEOF (dword)) ;
329 { if (marker == RIFF_MARKER)
330 psf_log_printf (psf, "RIFF : %u\n", RIFFsize) ;
332 psf_log_printf (psf, "RIFX : %u\n", RIFFsize) ;
337 if ((parsestage & HAVE_RIFF) != HAVE_RIFF)
338 return SFE_WAV_NO_WAVE ;
339 parsestage |= HAVE_WAVE ;
341 psf_log_printf (psf, "WAVE\n") ;
345 if ((parsestage & (HAVE_RIFF | HAVE_WAVE)) != (HAVE_RIFF | HAVE_WAVE))
346 return SFE_WAV_NO_FMT ;
348 /* If this file has a SECOND fmt chunk, I don't want to know about it. */
349 if (parsestage & HAVE_fmt)
352 parsestage |= HAVE_fmt ;
354 psf_binheader_readf (psf, "4", &dword) ;
355 psf_log_printf (psf, "fmt : %d\n", dword) ;
357 if ((error = wav_w64_read_fmt_chunk (psf, &wav_fmt, dword)))
360 format = wav_fmt.format ;
364 if ((parsestage & (HAVE_RIFF | HAVE_WAVE | HAVE_fmt)) != (HAVE_RIFF | HAVE_WAVE | HAVE_fmt))
365 return SFE_WAV_NO_DATA ;
367 if (psf->mode == SFM_RDWR && (parsestage & HAVE_other) != 0)
368 return SFE_RDWR_BAD_HEADER ;
370 parsestage |= HAVE_data ;
372 psf_binheader_readf (psf, "4", &dword) ;
374 psf->datalength = dword ;
375 psf->dataoffset = psf_ftell (psf) ;
377 if (dword == 0 && RIFFsize == 8 && psf->filelength > 44)
378 { psf_log_printf (psf, "*** Looks like a WAV file which wasn't closed properly. Fixing it.\n") ;
379 psf->datalength = dword = psf->filelength - psf->dataoffset ;
382 if (psf->datalength > psf->filelength - psf->dataoffset)
383 { psf_log_printf (psf, "data : %D (should be %D)\n", psf->datalength, psf->filelength - psf->dataoffset) ;
384 psf->datalength = psf->filelength - psf->dataoffset ;
387 psf_log_printf (psf, "data : %D\n", psf->datalength) ;
389 /* Only set dataend if there really is data at the end. */
390 if (psf->datalength + psf->dataoffset < psf->filelength)
391 psf->dataend = psf->datalength + psf->dataoffset ;
393 if (format == WAVE_FORMAT_MS_ADPCM && psf->datalength % 2)
394 { psf->datalength ++ ;
395 psf_log_printf (psf, "*** Data length odd. Increasing it by 1.\n") ;
398 if (! psf->sf.seekable)
401 /* Seek past data and continue reading header. */
402 psf_fseek (psf, psf->datalength, SEEK_CUR) ;
404 dword = psf_ftell (psf) ;
405 if (dword != (sf_count_t) (psf->dataoffset + psf->datalength))
406 psf_log_printf (psf, "*** psf_fseek past end error ***\n", dword, psf->dataoffset + psf->datalength) ;
410 if ((parsestage & (HAVE_RIFF | HAVE_WAVE)) != (HAVE_RIFF | HAVE_WAVE))
411 return SFE_WAV_BAD_FACT ;
413 parsestage |= HAVE_fact ;
415 if ((parsestage & HAVE_fmt) != HAVE_fmt)
416 psf_log_printf (psf, "*** Should have 'fmt ' chunk before 'fact'\n") ;
418 psf_binheader_readf (psf, "44", &dword, & (fact_chunk.frames)) ;
420 if (dword > SIGNED_SIZEOF (fact_chunk))
421 psf_binheader_readf (psf, "j", (int) (dword - SIGNED_SIZEOF (fact_chunk))) ;
424 psf_log_printf (psf, "%M : %d\n", marker, dword) ;
426 psf_log_printf (psf, "%M : %d (should not be zero)\n", marker, dword) ;
428 psf_log_printf (psf, " frames : %d\n", fact_chunk.frames) ;
432 if ((parsestage & (HAVE_RIFF | HAVE_WAVE | HAVE_fmt)) != (HAVE_RIFF | HAVE_WAVE | HAVE_fmt))
433 return SFE_WAV_PEAK_B4_FMT ;
435 parsestage |= HAVE_PEAK ;
437 psf_binheader_readf (psf, "4", &dword) ;
439 psf_log_printf (psf, "%M : %d\n", marker, dword) ;
440 if (dword != WAV_PEAK_CHUNK_SIZE (psf->sf.channels))
441 { psf_binheader_readf (psf, "j", dword) ;
442 psf_log_printf (psf, "*** File PEAK chunk size doesn't fit with number of channels (%d).\n", psf->sf.channels) ;
443 return SFE_WAV_BAD_PEAK ;
446 if ((psf->peak_info = peak_info_calloc (psf->sf.channels)) == NULL)
447 return SFE_MALLOC_FAILED ;
449 /* read in rest of PEAK chunk. */
450 psf_binheader_readf (psf, "44", & (psf->peak_info->version), & (psf->peak_info->timestamp)) ;
452 if (psf->peak_info->version != 1)
453 psf_log_printf (psf, " version : %d *** (should be version 1)\n", psf->peak_info->version) ;
455 psf_log_printf (psf, " version : %d\n", psf->peak_info->version) ;
457 psf_log_printf (psf, " time stamp : %d\n", psf->peak_info->timestamp) ;
458 psf_log_printf (psf, " Ch Position Value\n") ;
461 for (dword = 0 ; dword < (unsigned) psf->sf.channels ; dword++)
463 unsigned int position ;
464 psf_binheader_readf (psf, "f4", &value, &position) ;
465 psf->peak_info->peaks [dword].value = value ;
466 psf->peak_info->peaks [dword].position = position ;
468 LSF_SNPRINTF (cptr, sizeof (psf->u.cbuf), " %2d %-12ld %g\n",
469 dword, (long) psf->peak_info->peaks [dword].position, psf->peak_info->peaks [dword].value) ;
470 cptr [sizeof (psf->u.cbuf) - 1] = 0 ;
471 psf_log_printf (psf, cptr) ;
474 psf->peak_info->peak_loc = ((parsestage & HAVE_data) == 0) ? SF_PEAK_START : SF_PEAK_END ;
478 parsestage |= HAVE_other ;
480 { unsigned bytesread, cue_count ;
481 int id, position, chunk_id, chunk_start, block_start, offset ;
483 bytesread = psf_binheader_readf (psf, "44", &dword, &cue_count) ;
484 bytesread -= 4 ; /* Remove bytes for first dword. */
485 psf_log_printf (psf, "%M : %u\n", marker, dword) ;
488 { psf_log_printf (psf, " Count : %d (skipping)\n", cue_count) ;
489 psf_binheader_readf (psf, "j", cue_count * 24) ;
493 psf_log_printf (psf, " Count : %d\n", cue_count) ;
496 { bytesread += psf_binheader_readf (psf, "444444", &id, &position,
497 &chunk_id, &chunk_start, &block_start, &offset) ;
498 psf_log_printf (psf, " Cue ID : %2d"
499 " Pos : %5u Chunk : %M"
500 " Chk Start : %d Blk Start : %d"
502 id, position, chunk_id, chunk_start, block_start, offset) ;
506 if (bytesread != dword)
507 { psf_log_printf (psf, "**** Chunk size weirdness (%d != %d)\n", dword, bytesread) ;
508 psf_binheader_readf (psf, "j", dword - bytesread) ;
514 parsestage |= HAVE_other ;
516 psf_binheader_readf (psf, "4", &dword) ;
517 psf_log_printf (psf, "smpl : %u\n", dword) ;
519 if ((error = wav_read_smpl_chunk (psf, dword)))
524 parsestage |= HAVE_other ;
526 psf_binheader_readf (psf, "4", &dword) ;
527 psf_log_printf (psf, "acid : %u\n", dword) ;
529 if ((error = wav_read_acid_chunk (psf, dword)))
535 parsestage |= HAVE_other ;
537 if ((error = wav_subchunk_parse (psf, marker)) != 0)
543 psf_binheader_readf (psf, "4", &dword) ;
544 if (dword < WAV_BEXT_CHUNK_SIZE)
545 psf_log_printf (psf, "bext : %u (should be >= %d)\n", dword, WAV_BEXT_CHUNK_SIZE) ;
547 psf_log_printf (psf, "bext : %u\n", dword) ;
549 if ((error = wav_read_bext_chunk (psf, dword)))
553 case strc_MARKER : /* Multiple of 32 bytes. */
563 parsestage |= HAVE_other ;
565 psf_binheader_readf (psf, "4", &dword) ;
566 psf_log_printf (psf, "%M : %u\n", marker, dword) ;
567 dword += (dword & 1) ;
568 psf_binheader_readf (psf, "j", dword) ;
572 parsestage |= HAVE_other ;
573 if (isprint ((marker >> 24) & 0xFF) && isprint ((marker >> 16) & 0xFF)
574 && isprint ((marker >> 8) & 0xFF) && isprint (marker & 0xFF))
575 { psf_binheader_readf (psf, "4", &dword) ;
576 psf_log_printf (psf, "*** %M : %d (unknown marker)\n", marker, dword) ;
577 psf_binheader_readf (psf, "j", dword) ;
580 if (psf_ftell (psf) & 0x03)
581 { psf_log_printf (psf, " Unknown chunk marker at position %d. Resynching.\n", dword - 4) ;
582 psf_binheader_readf (psf, "j", -3) ;
585 psf_log_printf (psf, "*** Unknown chunk marker (%X) at position %D. Exiting parser.\n", marker, psf_ftell (psf) - 4) ;
588 } ; /* switch (dword) */
590 if (! psf->sf.seekable && (parsestage & HAVE_data))
593 if (psf_ftell (psf) >= psf->filelength - SIGNED_SIZEOF (dword))
594 { psf_log_printf (psf, "End\n") ;
599 if (! psf->dataoffset)
600 return SFE_WAV_NO_DATA ;
602 /* WAVs can be little or big endian */
603 psf->endian = psf->rwf_endian ;
605 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
607 if (psf->is_pipe == 0)
609 ** Check for 'wvpk' at the start of the DATA section. Not able to
612 psf_binheader_readf (psf, "4", &marker) ;
613 if (marker == wvpk_MARKER || marker == OggS_MARKER)
614 return SFE_WAV_WVPK_DATA ;
617 /* Seek to start of DATA section. */
618 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
621 { if (psf->filelength - psf->dataoffset < psf->datalength)
622 psf->sf.frames = (psf->filelength - psf->dataoffset) / psf->blockwidth ;
624 psf->sf.frames = psf->datalength / psf->blockwidth ;
628 { case WAVE_FORMAT_EXTENSIBLE :
629 if (psf->sf.format == (SF_FORMAT_WAVEX | SF_FORMAT_MS_ADPCM))
630 { *blockalign = wav_fmt.msadpcm.blockalign ;
631 *framesperblock = wav_fmt.msadpcm.samplesperblock ;
635 case WAVE_FORMAT_PCM :
636 psf->sf.format = SF_FORMAT_WAV | u_bitwidth_to_subformat (psf->bytewidth * 8) ;
639 case WAVE_FORMAT_MULAW :
640 case IBM_FORMAT_MULAW :
641 psf->sf.format = (SF_FORMAT_WAV | SF_FORMAT_ULAW) ;
644 case WAVE_FORMAT_ALAW :
645 case IBM_FORMAT_ALAW :
646 psf->sf.format = (SF_FORMAT_WAV | SF_FORMAT_ALAW) ;
649 case WAVE_FORMAT_MS_ADPCM :
650 psf->sf.format = (SF_FORMAT_WAV | SF_FORMAT_MS_ADPCM) ;
651 *blockalign = wav_fmt.msadpcm.blockalign ;
652 *framesperblock = wav_fmt.msadpcm.samplesperblock ;
655 case WAVE_FORMAT_IMA_ADPCM :
656 psf->sf.format = (SF_FORMAT_WAV | SF_FORMAT_IMA_ADPCM) ;
657 *blockalign = wav_fmt.ima.blockalign ;
658 *framesperblock = wav_fmt.ima.samplesperblock ;
661 case WAVE_FORMAT_GSM610 :
662 psf->sf.format = (SF_FORMAT_WAV | SF_FORMAT_GSM610) ;
665 case WAVE_FORMAT_IEEE_FLOAT :
666 psf->sf.format = SF_FORMAT_WAV ;
667 psf->sf.format |= (psf->bytewidth == 8) ? SF_FORMAT_DOUBLE : SF_FORMAT_FLOAT ;
670 case WAVE_FORMAT_G721_ADPCM :
671 psf->sf.format = SF_FORMAT_WAV | SF_FORMAT_G721_32 ;
674 default : return SFE_UNIMPLEMENTED ;
677 /* Only set the format endian-ness if its non-standard big-endian. */
678 if (psf->endian == SF_ENDIAN_BIG)
679 psf->sf.format |= SF_ENDIAN_BIG ;
682 } /* wav_read_header */
685 wav_write_header (SF_PRIVATE *psf, int calc_length)
686 { sf_count_t current ;
687 int fmt_size, k, subformat, add_fact_chunk = SF_FALSE ;
689 current = psf_ftell (psf) ;
692 { psf->filelength = psf_get_filelen (psf) ;
694 psf->datalength = psf->filelength - psf->dataoffset ;
697 psf->datalength -= psf->filelength - psf->dataend ;
699 if (psf->bytewidth > 0)
700 psf->sf.frames = psf->datalength / (psf->bytewidth * psf->sf.channels) ;
703 /* Reset the current header length to zero. */
704 psf->header [0] = 0 ;
706 psf_fseek (psf, 0, SEEK_SET) ;
709 ** RIFX signifies big-endian format for all header and data.
710 ** To prevent lots of code copying here, we'll set the psf->rwf_endian flag
711 ** once here, and never specify endian-ness for all other header operations.
714 /* RIFF/RIFX marker, length, WAVE and 'fmt ' markers. */
716 if (psf->endian == SF_ENDIAN_LITTLE)
717 psf_binheader_writef (psf, "etm8", RIFF_MARKER, (psf->filelength < 8) ? 8 : psf->filelength - 8) ;
719 psf_binheader_writef (psf, "Etm8", RIFX_MARKER, (psf->filelength < 8) ? 8 : psf->filelength - 8) ;
721 /* WAVE and 'fmt ' markers. */
722 psf_binheader_writef (psf, "mm", WAVE_MARKER, fmt_MARKER) ;
724 subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
727 { case SF_FORMAT_PCM_U8 :
728 case SF_FORMAT_PCM_16 :
729 case SF_FORMAT_PCM_24 :
730 case SF_FORMAT_PCM_32 :
731 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 ;
733 /* fmt : format, channels, samplerate */
734 psf_binheader_writef (psf, "4224", fmt_size, WAVE_FORMAT_PCM, psf->sf.channels, psf->sf.samplerate) ;
735 /* fmt : bytespersec */
736 psf_binheader_writef (psf, "4", psf->sf.samplerate * psf->bytewidth * psf->sf.channels) ;
737 /* fmt : blockalign, bitwidth */
738 psf_binheader_writef (psf, "22", psf->bytewidth * psf->sf.channels, psf->bytewidth * 8) ;
741 case SF_FORMAT_FLOAT :
742 case SF_FORMAT_DOUBLE :
743 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 ;
745 /* fmt : format, channels, samplerate */
746 psf_binheader_writef (psf, "4224", fmt_size, WAVE_FORMAT_IEEE_FLOAT, psf->sf.channels, psf->sf.samplerate) ;
747 /* fmt : bytespersec */
748 psf_binheader_writef (psf, "4", psf->sf.samplerate * psf->bytewidth * psf->sf.channels) ;
749 /* fmt : blockalign, bitwidth */
750 psf_binheader_writef (psf, "22", psf->bytewidth * psf->sf.channels, psf->bytewidth * 8) ;
752 add_fact_chunk = SF_TRUE ;
755 case SF_FORMAT_ULAW :
756 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 ;
758 /* fmt : format, channels, samplerate */
759 psf_binheader_writef (psf, "4224", fmt_size, WAVE_FORMAT_MULAW, psf->sf.channels, psf->sf.samplerate) ;
760 /* fmt : bytespersec */
761 psf_binheader_writef (psf, "4", psf->sf.samplerate * psf->bytewidth * psf->sf.channels) ;
762 /* fmt : blockalign, bitwidth */
763 psf_binheader_writef (psf, "22", psf->bytewidth * psf->sf.channels, 8) ;
765 add_fact_chunk = SF_TRUE ;
768 case SF_FORMAT_ALAW :
769 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 ;
771 /* fmt : format, channels, samplerate */
772 psf_binheader_writef (psf, "4224", fmt_size, WAVE_FORMAT_ALAW, psf->sf.channels, psf->sf.samplerate) ;
773 /* fmt : bytespersec */
774 psf_binheader_writef (psf, "4", psf->sf.samplerate * psf->bytewidth * psf->sf.channels) ;
775 /* fmt : blockalign, bitwidth */
776 psf_binheader_writef (psf, "22", psf->bytewidth * psf->sf.channels, 8) ;
778 add_fact_chunk = SF_TRUE ;
781 /* Lite remove start */
782 case SF_FORMAT_IMA_ADPCM :
783 { int blockalign, framesperblock, bytespersec ;
785 blockalign = wav_w64_srate2blocksize (psf->sf.samplerate * psf->sf.channels) ;
786 framesperblock = 2 * (blockalign - 4 * psf->sf.channels) / psf->sf.channels + 1 ;
787 bytespersec = (psf->sf.samplerate * blockalign) / framesperblock ;
790 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 + 2 ;
792 /* fmt : size, WAV format type, channels, samplerate, bytespersec */
793 psf_binheader_writef (psf, "42244", fmt_size, WAVE_FORMAT_IMA_ADPCM,
794 psf->sf.channels, psf->sf.samplerate, bytespersec) ;
796 /* fmt : blockalign, bitwidth, extrabytes, framesperblock. */
797 psf_binheader_writef (psf, "2222", blockalign, 4, 2, framesperblock) ;
800 add_fact_chunk = SF_TRUE ;
803 case SF_FORMAT_MS_ADPCM :
804 { int blockalign, framesperblock, bytespersec, extrabytes ;
806 blockalign = wav_w64_srate2blocksize (psf->sf.samplerate * psf->sf.channels) ;
807 framesperblock = 2 + 2 * (blockalign - 7 * psf->sf.channels) / psf->sf.channels ;
808 bytespersec = (psf->sf.samplerate * blockalign) / framesperblock ;
811 extrabytes = 2 + 2 + MSADPCM_ADAPT_COEFF_COUNT * (2 + 2) ;
812 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 + extrabytes ;
814 /* fmt : size, WAV format type, channels. */
815 psf_binheader_writef (psf, "422", fmt_size, WAVE_FORMAT_MS_ADPCM, psf->sf.channels) ;
817 /* fmt : samplerate, bytespersec. */
818 psf_binheader_writef (psf, "44", psf->sf.samplerate, bytespersec) ;
820 /* fmt : blockalign, bitwidth, extrabytes, framesperblock. */
821 psf_binheader_writef (psf, "22222", blockalign, 4, extrabytes, framesperblock, 7) ;
823 msadpcm_write_adapt_coeffs (psf) ;
826 add_fact_chunk = SF_TRUE ;
830 case SF_FORMAT_G721_32 :
832 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 + 2 ;
834 /* fmt : size, WAV format type, channels, samplerate, bytespersec */
835 psf_binheader_writef (psf, "42244", fmt_size, WAVE_FORMAT_G721_ADPCM,
836 psf->sf.channels, psf->sf.samplerate, psf->sf.samplerate * psf->sf.channels / 2) ;
838 /* fmt : blockalign, bitwidth, extrabytes, auxblocksize. */
839 psf_binheader_writef (psf, "2222", 64, 4, 2, 0) ;
841 add_fact_chunk = SF_TRUE ;
844 /* Lite remove end */
846 case SF_FORMAT_GSM610 :
847 { int blockalign, framesperblock, bytespersec ;
849 blockalign = WAV_W64_GSM610_BLOCKSIZE ;
850 framesperblock = WAV_W64_GSM610_SAMPLES ;
851 bytespersec = (psf->sf.samplerate * blockalign) / framesperblock ;
854 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 + 2 ;
856 /* fmt : size, WAV format type, channels. */
857 psf_binheader_writef (psf, "422", fmt_size, WAVE_FORMAT_GSM610, psf->sf.channels) ;
859 /* fmt : samplerate, bytespersec. */
860 psf_binheader_writef (psf, "44", psf->sf.samplerate, bytespersec) ;
862 /* fmt : blockalign, bitwidth, extrabytes, framesperblock. */
863 psf_binheader_writef (psf, "2222", blockalign, 0, 2, framesperblock) ;
866 add_fact_chunk = SF_TRUE ;
869 default : return SFE_UNIMPLEMENTED ;
873 psf_binheader_writef (psf, "tm48", fact_MARKER, 4, psf->sf.frames) ;
875 if (psf->str_flags & SF_STR_LOCATE_START)
876 wav_write_strings (psf, SF_STR_LOCATE_START) ;
878 if (psf->peak_info != NULL && psf->peak_info->peak_loc == SF_PEAK_START)
879 { psf_binheader_writef (psf, "m4", PEAK_MARKER, WAV_PEAK_CHUNK_SIZE (psf->sf.channels)) ;
880 psf_binheader_writef (psf, "44", 1, time (NULL)) ;
881 for (k = 0 ; k < psf->sf.channels ; k++)
882 psf_binheader_writef (psf, "ft8", (float) psf->peak_info->peaks [k].value, psf->peak_info->peaks [k].position) ;
885 if (psf->broadcast_info != NULL)
886 wav_write_bext_chunk (psf) ;
888 if (psf->instrument != NULL)
890 double dtune = (double) (0x40000000) / 25.0 ;
892 psf_binheader_writef (psf, "m4", smpl_MARKER, 9 * 4 + psf->instrument->loop_count * 6 * 4) ;
893 psf_binheader_writef (psf, "44", 0, 0) ; /* Manufacturer zero is everyone */
894 tmp = (int) (1.0e9 / psf->sf.samplerate) ; /* Sample period in nano seconds */
895 psf_binheader_writef (psf, "44", tmp, psf->instrument->basenote) ;
896 tmp = (unsigned int) (psf->instrument->detune * dtune + 0.5) ;
897 psf_binheader_writef (psf, "4", tmp) ;
898 psf_binheader_writef (psf, "44", 0, 0) ; /* SMTPE format */
899 psf_binheader_writef (psf, "44", psf->instrument->loop_count, 0) ;
901 for (tmp = 0 ; tmp < psf->instrument->loop_count ; tmp++)
904 type = psf->instrument->loops [tmp].mode ;
905 type = (type == SF_LOOP_FORWARD ? 0 : type==SF_LOOP_BACKWARD ? 2 : type == SF_LOOP_ALTERNATING ? 1 : 32) ;
907 psf_binheader_writef (psf, "44", tmp, type) ;
908 psf_binheader_writef (psf, "44", psf->instrument->loops [tmp].start, psf->instrument->loops [tmp].end) ;
909 psf_binheader_writef (psf, "44", 0, psf->instrument->loops [tmp].count) ;
913 psf_binheader_writef (psf, "tm8", data_MARKER, psf->datalength) ;
914 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
918 psf->dataoffset = psf->headindex ;
920 if (current < psf->dataoffset)
921 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
922 else if (current > 0)
923 psf_fseek (psf, current, SEEK_SET) ;
926 } /* wav_write_header */
931 wavex_write_header (SF_PRIVATE *psf, int calc_length)
932 { sf_count_t current ;
933 int fmt_size, k, subformat, add_fact_chunk = SF_FALSE ;
935 current = psf_ftell (psf) ;
938 { psf->filelength = psf_get_filelen (psf) ;
940 psf->datalength = psf->filelength - psf->dataoffset ;
943 psf->datalength -= psf->filelength - psf->dataend ;
945 if (psf->bytewidth > 0)
946 psf->sf.frames = psf->datalength / (psf->bytewidth * psf->sf.channels) ;
950 /* Reset the current header length to zero. */
951 psf->header [0] = 0 ;
953 psf_fseek (psf, 0, SEEK_SET) ;
955 /* RIFX signifies big-endian format for all header and data
956 ** to prevent lots of code copying here, we'll set the psf->rwf_endian
957 ** flag once here, and never specify endian-ness for all other header ops
960 /* RIFF marker, length, WAVE and 'fmt ' markers. */
962 if (psf->endian == SF_ENDIAN_LITTLE)
963 { if (psf->filelength < 8)
964 psf_binheader_writef (psf, "tm8", RIFF_MARKER, 8) ;
966 psf_binheader_writef (psf, "tm8", RIFF_MARKER, psf->filelength - 8) ;
969 { if (psf->filelength < 8)
970 psf_binheader_writef (psf, "Etm8", RIFX_MARKER, 8) ;
972 psf_binheader_writef (psf, "Etm8", RIFX_MARKER, psf->filelength - 8) ;
975 /* WAVE and 'fmt ' markers. */
976 psf_binheader_writef (psf, "mm", WAVE_MARKER, fmt_MARKER) ;
978 subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
980 /* initial section (same for all, it appears) */
982 { case SF_FORMAT_PCM_U8 :
983 case SF_FORMAT_PCM_16 :
984 case SF_FORMAT_PCM_24 :
985 case SF_FORMAT_PCM_32 :
986 case SF_FORMAT_FLOAT :
987 case SF_FORMAT_DOUBLE :
988 case SF_FORMAT_ULAW :
989 case SF_FORMAT_ALAW :
990 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 + 2 + 4 + 4 + 2 + 2 + 8 ;
992 /* fmt : format, channels, samplerate */
993 psf_binheader_writef (psf, "4224", fmt_size, WAVE_FORMAT_EXTENSIBLE, psf->sf.channels, psf->sf.samplerate) ;
994 /* fmt : bytespersec */
995 psf_binheader_writef (psf, "4", psf->sf.samplerate * psf->bytewidth * psf->sf.channels) ;
996 /* fmt : blockalign, bitwidth */
997 psf_binheader_writef (psf, "22", psf->bytewidth * psf->sf.channels, psf->bytewidth * 8) ;
999 /* cbSize 22 is sizeof (WAVEFORMATEXTENSIBLE) - sizeof (WAVEFORMATEX) */
1000 psf_binheader_writef (psf, "2", 22) ;
1002 /* wValidBitsPerSample, for our use same as bitwidth as we use it fully */
1003 psf_binheader_writef (psf, "2", psf->bytewidth * 8) ;
1006 ** Ok some liberty is taken here to use the most commonly used channel masks
1007 ** instead of "no mapping". If you really want to use "no mapping" for 8 channels and less
1008 ** please don't use wavex. (otherwise we'll have to create a new SF_COMMAND)
1010 switch (psf->sf.channels)
1011 { case 1 : /* center channel mono */
1012 psf_binheader_writef (psf, "4", 0x4) ;
1015 case 2 : /* front left and right */
1016 psf_binheader_writef (psf, "4", 0x1 | 0x2) ;
1020 psf_binheader_writef (psf, "4", 0x1 | 0x2 | 0x10 | 0x20) ;
1024 psf_binheader_writef (psf, "4", 0x1 | 0x2 | 0x4 | 0x8 | 0x10 | 0x20) ;
1028 psf_binheader_writef (psf, "4", 0x1 | 0x2 | 0x4 | 0x8 | 0x10 | 0x20 | 0x40 | 0x80) ;
1031 default : /* 0 when in doubt , use direct out, ie NO mapping*/
1032 psf_binheader_writef (psf, "4", 0x0) ;
1038 case SF_FORMAT_MS_ADPCM : /* Todo, GUID exists might have different header as per wav_write_header */
1040 return SFE_UNIMPLEMENTED ;
1043 /* GUID section, different for each */
1046 { case SF_FORMAT_PCM_U8 :
1047 case SF_FORMAT_PCM_16 :
1048 case SF_FORMAT_PCM_24 :
1049 case SF_FORMAT_PCM_32 :
1050 wavex_write_guid (psf, &MSGUID_SUBTYPE_PCM) ;
1053 case SF_FORMAT_FLOAT :
1054 case SF_FORMAT_DOUBLE :
1055 wavex_write_guid (psf, &MSGUID_SUBTYPE_IEEE_FLOAT) ;
1056 add_fact_chunk = SF_TRUE ;
1059 case SF_FORMAT_ULAW :
1060 wavex_write_guid (psf, &MSGUID_SUBTYPE_MULAW) ;
1061 add_fact_chunk = SF_TRUE ;
1064 case SF_FORMAT_ALAW :
1065 wavex_write_guid (psf, &MSGUID_SUBTYPE_ALAW) ;
1066 add_fact_chunk = SF_TRUE ;
1069 case SF_FORMAT_MS_ADPCM : /* todo, GUID exists */
1071 default : return SFE_UNIMPLEMENTED ;
1075 psf_binheader_writef (psf, "tm48", fact_MARKER, 4, psf->sf.frames) ;
1077 if (psf->str_flags & SF_STR_LOCATE_START)
1078 wav_write_strings (psf, SF_STR_LOCATE_START) ;
1080 if (psf->peak_info != NULL && psf->peak_info->peak_loc == SF_PEAK_START)
1081 { psf_binheader_writef (psf, "m4", PEAK_MARKER, WAV_PEAK_CHUNK_SIZE (psf->sf.channels)) ;
1082 psf_binheader_writef (psf, "44", 1, time (NULL)) ;
1083 for (k = 0 ; k < psf->sf.channels ; k++)
1084 psf_binheader_writef (psf, "ft8", (float) psf->peak_info->peaks [k].value, psf->peak_info->peaks [k].position) ;
1087 psf_binheader_writef (psf, "tm8", data_MARKER, psf->datalength) ;
1088 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
1092 psf->dataoffset = psf->headindex ;
1094 if (current < psf->dataoffset)
1095 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
1096 else if (current > 0)
1097 psf_fseek (psf, current, SEEK_SET) ;
1100 } /* wavex_write_header */
1105 wav_write_tailer (SF_PRIVATE *psf)
1108 /* Reset the current header buffer length to zero. */
1109 psf->header [0] = 0 ;
1110 psf->headindex = 0 ;
1112 psf->dataend = psf_fseek (psf, 0, SEEK_END) ;
1114 /* Add a PEAK chunk if requested. */
1115 if (psf->peak_info != NULL && psf->peak_info->peak_loc == SF_PEAK_END)
1116 { psf_binheader_writef (psf, "m4", PEAK_MARKER, WAV_PEAK_CHUNK_SIZE (psf->sf.channels)) ;
1117 psf_binheader_writef (psf, "44", 1, time (NULL)) ;
1118 for (k = 0 ; k < psf->sf.channels ; k++)
1119 psf_binheader_writef (psf, "f4", psf->peak_info->peaks [k].value, psf->peak_info->peaks [k].position) ;
1122 if (psf->str_flags & SF_STR_LOCATE_END)
1123 wav_write_strings (psf, SF_STR_LOCATE_END) ;
1125 /* Write the tailer. */
1126 if (psf->headindex > 0)
1127 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
1130 } /* wav_write_tailer */
1133 wav_write_strings (SF_PRIVATE *psf, int location)
1134 { int k, prev_head_index, saved_head_index ;
1136 prev_head_index = psf->headindex + 4 ;
1138 psf_binheader_writef (psf, "m4m", LIST_MARKER, 0xBADBAD, INFO_MARKER) ;
1140 for (k = 0 ; k < SF_MAX_STRINGS ; k++)
1141 { if (psf->strings [k].type == 0)
1143 if (psf->strings [k].flags != location)
1146 switch (psf->strings [k].type)
1147 { case SF_STR_SOFTWARE :
1148 psf_binheader_writef (psf, "ms", ISFT_MARKER, psf->strings [k].str) ;
1152 psf_binheader_writef (psf, "ms", INAM_MARKER, psf->strings [k].str) ;
1155 case SF_STR_COPYRIGHT :
1156 psf_binheader_writef (psf, "ms", ICOP_MARKER, psf->strings [k].str) ;
1159 case SF_STR_ARTIST :
1160 psf_binheader_writef (psf, "ms", IART_MARKER, psf->strings [k].str) ;
1163 case SF_STR_COMMENT :
1164 psf_binheader_writef (psf, "ms", ICMT_MARKER, psf->strings [k].str) ;
1168 psf_binheader_writef (psf, "ms", ICRD_MARKER, psf->strings [k].str) ;
1173 saved_head_index = psf->headindex ;
1174 psf->headindex = prev_head_index ;
1175 psf_binheader_writef (psf, "4", saved_head_index - prev_head_index - 4) ;
1176 psf->headindex = saved_head_index ;
1178 } /* wav_write_strings */
1181 wav_close (SF_PRIVATE *psf)
1183 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
1184 { wav_write_tailer (psf) ;
1186 psf->write_header (psf, SF_TRUE) ;
1193 wav_command (SF_PRIVATE *psf, int command, void *data, int datasize)
1195 /* Avoid compiler warnings. */
1198 datasize = datasize ;
1208 wav_subchunk_parse (SF_PRIVATE *psf, int chunk)
1209 { sf_count_t current_pos ;
1211 int dword, bytesread, length ;
1213 current_pos = psf_fseek (psf, 0, SEEK_CUR) ;
1215 bytesread = psf_binheader_readf (psf, "4", &length) ;
1218 { /* This case is for broken files generated by PEAK. */
1219 psf_log_printf (psf, "%M : %d (weird length)\n", chunk, length) ;
1220 psf_binheader_readf (psf, "mj", &chunk, length - 4) ;
1221 psf_log_printf (psf, " %M\n", chunk) ;
1225 if (psf->headindex + length > SIGNED_SIZEOF (psf->header))
1226 { psf_log_printf (psf, "%M : %d (too long)\n", chunk, length) ;
1227 psf_binheader_readf (psf, "j", length) ;
1231 if (current_pos + length > psf->filelength)
1232 { psf_log_printf (psf, "%M : %d (should be %d)\n", chunk, length, (int) (psf->filelength - current_pos)) ;
1233 length = psf->filelength - current_pos ;
1236 psf_log_printf (psf, "%M : %d\n", chunk, length) ;
1238 while (bytesread < length)
1239 { bytesread += psf_binheader_readf (psf, "m", &chunk) ;
1242 { case adtl_MARKER :
1244 /* These markers don't contain anything. */
1245 psf_log_printf (psf, " %M\n", chunk) ;
1249 psf_log_printf (psf, " %M inside a LIST block??? Backing out.\n", chunk) ;
1250 /* Jump back four bytes and return to caller. */
1251 psf_binheader_readf (psf, "j", -4) ;
1266 bytesread += psf_binheader_readf (psf, "4", &dword) ;
1267 dword += (dword & 1) ;
1268 if (dword < 0 || dword > SIGNED_SIZEOF (psf->u.cbuf))
1269 { psf_log_printf (psf, " *** %M : %d (too big)\n", chunk, dword) ;
1270 psf_binheader_readf (psf, "j", dword) ;
1274 cptr = psf->u.cbuf ;
1275 psf_binheader_readf (psf, "b", cptr, dword) ;
1276 bytesread += dword ;
1277 cptr [dword - 1] = 0 ;
1278 psf_log_printf (psf, " %M : %s\n", chunk, cptr) ;
1284 bytesread += psf_binheader_readf (psf, "44", &dword, &mark_id) ;
1286 dword += (dword & 1) ;
1287 if (dword < 1 || dword > SIGNED_SIZEOF (psf->u.cbuf))
1288 { psf_log_printf (psf, " *** %M : %d (too big)\n", chunk, dword) ;
1289 psf_binheader_readf (psf, "j", dword) ;
1293 cptr = psf->u.cbuf ;
1294 psf_binheader_readf (psf, "b", cptr, dword) ;
1295 bytesread += dword ;
1296 cptr [dword - 1] = 0 ;
1297 psf_log_printf (psf, " %M : %d : %s\n", chunk, mark_id, cptr) ;
1305 bytesread += psf_binheader_readf (psf, "4", &dword) ;
1306 dword += (dword & 1) ;
1307 psf_binheader_readf (psf, "j", dword) ;
1308 bytesread += dword ;
1309 psf_log_printf (psf, " %M : %d\n", chunk, dword) ;
1313 psf_binheader_readf (psf, "4", &dword) ;
1314 bytesread += sizeof (dword) ;
1315 dword += (dword & 1) ;
1316 psf_binheader_readf (psf, "j", dword) ;
1317 bytesread += dword ;
1318 psf_log_printf (psf, " *** %M : %d\n", chunk, dword) ;
1325 { case ISFT_MARKER :
1326 psf_store_string (psf, SF_STR_SOFTWARE, psf->u.cbuf) ;
1329 psf_store_string (psf, SF_STR_COPYRIGHT, psf->u.cbuf) ;
1332 psf_store_string (psf, SF_STR_TITLE, psf->u.cbuf) ;
1335 psf_store_string (psf, SF_STR_ARTIST, psf->u.cbuf) ;
1338 psf_store_string (psf, SF_STR_COMMENT, psf->u.cbuf) ;
1341 psf_store_string (psf, SF_STR_DATE, psf->u.cbuf) ;
1346 current_pos = psf_fseek (psf, 0, SEEK_CUR) - current_pos ;
1348 if (current_pos - 4 != length)
1349 psf_log_printf (psf, "**** Bad chunk length %d sbould be %D\n", length, current_pos - 4) ;
1352 } /* wav_subchunk_parse */
1355 wav_read_smpl_chunk (SF_PRIVATE *psf, unsigned int chunklen)
1356 { unsigned int bytesread = 0, dword, sampler_data, loop_count ;
1357 unsigned int note, start, end, type = -1, count ;
1360 chunklen += (chunklen & 1) ;
1362 bytesread += psf_binheader_readf (psf, "4", &dword) ;
1363 psf_log_printf (psf, " Manufacturer : %X\n", dword) ;
1365 bytesread += psf_binheader_readf (psf, "4", &dword) ;
1366 psf_log_printf (psf, " Product : %u\n", dword) ;
1368 bytesread += psf_binheader_readf (psf, "4", &dword) ;
1369 psf_log_printf (psf, " Period : %u nsec\n", dword) ;
1371 bytesread += psf_binheader_readf (psf, "4", ¬e) ;
1372 psf_log_printf (psf, " Midi Note : %u\n", note) ;
1374 bytesread += psf_binheader_readf (psf, "4", &dword) ;
1376 { LSF_SNPRINTF (psf->u.cbuf, sizeof (psf->u.cbuf), "%f",
1377 (1.0 * 0x80000000) / ((unsigned int) dword)) ;
1378 psf_log_printf (psf, " Pitch Fract. : %s\n", psf->u.cbuf) ;
1381 psf_log_printf (psf, " Pitch Fract. : 0\n") ;
1383 bytesread += psf_binheader_readf (psf, "4", &dword) ;
1384 psf_log_printf (psf, " SMPTE Format : %u\n", dword) ;
1386 bytesread += psf_binheader_readf (psf, "4", &dword) ;
1387 LSF_SNPRINTF (psf->u.cbuf, sizeof (psf->u.cbuf), "%02d:%02d:%02d %02d",
1388 (dword >> 24) & 0x7F, (dword >> 16) & 0x7F, (dword >> 8) & 0x7F, dword & 0x7F) ;
1389 psf_log_printf (psf, " SMPTE Offset : %s\n", psf->u.cbuf) ;
1391 bytesread += psf_binheader_readf (psf, "4", &loop_count) ;
1392 psf_log_printf (psf, " Loop Count : %u\n", loop_count) ;
1394 /* Sampler Data holds the number of data bytes after the CUE chunks which
1395 ** is not actually CUE data. Display value after CUE data.
1397 bytesread += psf_binheader_readf (psf, "4", &sampler_data) ;
1399 if ((psf->instrument = psf_instrument_alloc ()) == NULL)
1400 return SFE_MALLOC_FAILED ;
1402 psf->instrument->loop_count = loop_count ;
1404 for (j = 0 ; loop_count > 0 && chunklen - bytesread >= 24 ; j ++)
1405 { bytesread += psf_binheader_readf (psf, "4", &dword) ;
1406 psf_log_printf (psf, " Cue ID : %2u", dword) ;
1408 bytesread += psf_binheader_readf (psf, "4", &type) ;
1409 psf_log_printf (psf, " Type : %2u", type) ;
1411 bytesread += psf_binheader_readf (psf, "4", &start) ;
1412 psf_log_printf (psf, " Start : %5u", start) ;
1414 bytesread += psf_binheader_readf (psf, "4", &end) ;
1415 psf_log_printf (psf, " End : %5u", end) ;
1417 bytesread += psf_binheader_readf (psf, "4", &dword) ;
1418 psf_log_printf (psf, " Fraction : %5u", dword) ;
1420 bytesread += psf_binheader_readf (psf, "4", &count) ;
1421 psf_log_printf (psf, " Count : %5u\n", count) ;
1423 if (j < ARRAY_LEN (psf->instrument->loops))
1424 { psf->instrument->loops [j].start = start ;
1425 psf->instrument->loops [j].end = end ;
1426 psf->instrument->loops [j].count = count ;
1430 psf->instrument->loops [j].mode = SF_LOOP_FORWARD ;
1433 psf->instrument->loops [j].mode = SF_LOOP_ALTERNATING ;
1436 psf->instrument->loops [j].mode = SF_LOOP_BACKWARD ;
1439 psf->instrument->loops [j].mode = SF_LOOP_NONE ;
1447 if (chunklen - bytesread == 0)
1448 { if (sampler_data != 0)
1449 psf_log_printf (psf, " Sampler Data : %u (should be 0)\n", sampler_data) ;
1451 psf_log_printf (psf, " Sampler Data : %u\n", sampler_data) ;
1454 { if (sampler_data != chunklen - bytesread)
1455 { psf_log_printf (psf, " Sampler Data : %u (should have been %u)\n", sampler_data, chunklen - bytesread) ;
1456 sampler_data = chunklen - bytesread ;
1459 psf_log_printf (psf, " Sampler Data : %u\n", sampler_data) ;
1461 psf_log_printf (psf, " ") ;
1462 for (k = 0 ; k < (int) sampler_data ; k++)
1465 if (k > 0 && (k % 20) == 0)
1466 psf_log_printf (psf, "\n ") ;
1468 bytesread += psf_binheader_readf (psf, "1", &ch) ;
1469 psf_log_printf (psf, "%02X ", ch & 0xFF) ;
1472 psf_log_printf (psf, "\n") ;
1475 psf->instrument->basenote = note ;
1476 psf->instrument->gain = 1 ;
1477 psf->instrument->velocity_lo = psf->instrument->key_lo = 0 ;
1478 psf->instrument->velocity_hi = psf->instrument->key_hi = 127 ;
1481 } /* wav_read_smpl_chunk */
1484 ** The acid chunk goes a little something like this:
1487 ** 4 bytes (int) length of chunk starting at next byte
1489 ** 4 bytes (int) type of file:
1490 ** this appears to be a bit mask,however some combinations
1491 ** are probably impossible and/or qualified as "errors"
1493 ** 0x01 On: One Shot Off: Loop
1494 ** 0x02 On: Root note is Set Off: No root
1495 ** 0x04 On: Stretch is On, Off: Strech is OFF
1496 ** 0x08 On: Disk Based Off: Ram based
1497 ** 0x10 On: ?????????? Off: ????????? (Acidizer puts that ON)
1499 ** 2 bytes (short) root note
1500 ** if type 0x10 is OFF : [C,C#,(...),B] -> [0x30 to 0x3B]
1501 ** if type 0x10 is ON : [C,C#,(...),B] -> [0x3C to 0x47]
1502 ** (both types fit on same MIDI pitch albeit different octaves, so who cares)
1504 ** 2 bytes (short) ??? always set to 0x8000
1505 ** 4 bytes (float) ??? seems to be always 0
1506 ** 4 bytes (int) number of beats
1507 ** 2 bytes (short) meter denominator //always 4 in SF/ACID
1508 ** 2 bytes (short) meter numerator //always 4 in SF/ACID
1509 ** //are we sure about the order?? usually its num/denom
1510 ** 4 bytes (float) tempo
1515 wav_read_acid_chunk (SF_PRIVATE *psf, unsigned int chunklen)
1516 { unsigned int bytesread = 0 ;
1518 short rootnote, q1, meter_denom, meter_numer ;
1521 chunklen += (chunklen & 1) ;
1523 bytesread += psf_binheader_readf (psf, "422f", &flags, &rootnote, &q1, &q2) ;
1525 LSF_SNPRINTF (psf->u.cbuf, sizeof (psf->u.cbuf), "%f", q2) ;
1527 psf_log_printf (psf, " Flags : 0x%04x (%s,%s,%s,%s,%s)\n", flags,
1528 (flags & 0x01) ? "OneShot" : "Loop",
1529 (flags & 0x02) ? "RootNoteValid" : "RootNoteInvalid",
1530 (flags & 0x04) ? "StretchOn" : "StretchOff",
1531 (flags & 0x08) ? "DiskBased" : "RAMBased",
1532 (flags & 0x10) ? "??On" : "??Off") ;
1534 psf_log_printf (psf, " Root note : 0x%x\n ???? : 0x%04x\n ???? : %s\n",
1535 rootnote, q1, psf->u.cbuf) ;
1537 bytesread += psf_binheader_readf (psf, "422f", &beats, &meter_denom, &meter_numer, &tempo) ;
1538 LSF_SNPRINTF (psf->u.cbuf, sizeof (psf->u.cbuf), "%f", tempo) ;
1539 psf_log_printf (psf, " Beats : %d\n Meter : %d/%d\n Tempo : %s\n",
1540 beats, meter_numer, meter_denom, psf->u.cbuf) ;
1542 psf_binheader_readf (psf, "j", chunklen - bytesread) ;
1544 if ((psf->loop_info = calloc (1, sizeof (SF_LOOP_INFO))) == NULL)
1545 return SFE_MALLOC_FAILED ;
1547 psf->loop_info->time_sig_num = meter_numer ;
1548 psf->loop_info->time_sig_den = meter_denom ;
1549 psf->loop_info->loop_mode = (flags & 0x01) ? SF_LOOP_NONE : SF_LOOP_FORWARD ;
1550 psf->loop_info->num_beats = beats ;
1551 psf->loop_info->bpm = tempo ;
1552 psf->loop_info->root_key = (flags & 0x02) ? rootnote : -1 ;
1555 } /* wav_read_acid_chunk */
1558 wav_read_bext_chunk (SF_PRIVATE *psf, unsigned int chunksize)
1560 SF_BROADCAST_INFO* b ;
1562 if ((psf->broadcast_info = calloc (1, sizeof (SF_BROADCAST_INFO))) == NULL)
1563 { psf->error = SFE_MALLOC_FAILED ;
1567 b = psf->broadcast_info ;
1569 psf_binheader_readf (psf, "b", b->description, sizeof (b->description)) ;
1570 psf_binheader_readf (psf, "b", b->originator, sizeof (b->originator)) ;
1571 psf_binheader_readf (psf, "b", b->originator_reference, sizeof (b->originator_reference)) ;
1572 psf_binheader_readf (psf, "b", b->origination_date, sizeof (b->origination_date)) ;
1573 psf_binheader_readf (psf, "b", b->origination_time, sizeof (b->origination_time)) ;
1574 psf_binheader_readf (psf, "442", &b->time_reference_low, &b->time_reference_high, &b->version) ;
1575 psf_binheader_readf (psf, "bj", &b->umid, sizeof (b->umid), 190) ;
1577 if (chunksize > WAV_BEXT_CHUNK_SIZE)
1578 { /* File has coding history data. */
1580 b->coding_history_size = chunksize - WAV_BEXT_CHUNK_SIZE ;
1582 if (b->coding_history_size > SIGNED_SIZEOF (b->coding_history))
1583 { free (psf->broadcast_info) ;
1584 psf->broadcast_info = NULL ;
1585 psf->error = SFE_MALLOC_FAILED ;
1589 /* We do not parse the coding history */
1590 psf_binheader_readf (psf, "b", b->coding_history, b->coding_history_size) ;
1591 b->coding_history [sizeof (b->coding_history) - 1] = 0 ;
1595 } /* wav_read_bext_chunk */
1598 wav_write_bext_chunk (SF_PRIVATE *psf)
1599 { SF_BROADCAST_INFO *b ;
1601 if ((b = psf->broadcast_info) == NULL)
1604 psf_binheader_writef (psf, "m4", bext_MARKER, WAV_BEXT_CHUNK_SIZE + b->coding_history_size) ;
1607 ** Note that it is very important the the field widths of the SF_BROADCAST_INFO
1608 ** struct match those for the bext chunk fields.
1611 psf_binheader_writef (psf, "b", b->description, sizeof (b->description)) ;
1612 psf_binheader_writef (psf, "b", b->originator, sizeof (b->originator)) ;
1613 psf_binheader_writef (psf, "b", b->originator_reference, sizeof (b->originator_reference)) ;
1614 psf_binheader_writef (psf, "b", b->origination_date, sizeof (b->origination_date)) ;
1615 psf_binheader_writef (psf, "b", b->origination_time, sizeof (b->origination_time)) ;
1616 psf_binheader_writef (psf, "442", b->time_reference_low, b->time_reference_high, b->version) ;
1617 psf_binheader_writef (psf, "b", b->umid, sizeof (b->umid)) ;
1618 psf_binheader_writef (psf, "z", make_size_t (190)) ;
1620 if (b->coding_history_size > 0)
1621 psf_binheader_writef (psf, "b", b->coding_history, b->coding_history_size) ;
1624 } /* wav_write_bext_chunk */
1627 ** Do not edit or modify anything in this comment block.
1628 ** The arch-tag line is a file identity tag for the GNU Arch
1629 ** revision control system.
1631 ** arch-tag: 9c551689-a1d8-4905-9f56-26a204374f18