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)
542 parsestage |= HAVE_other ;
544 psf_binheader_readf (psf, "4", &dword) ;
545 if (dword < WAV_BEXT_CHUNK_SIZE)
546 psf_log_printf (psf, "bext : %u (should be >= %d)\n", dword, WAV_BEXT_CHUNK_SIZE) ;
548 psf_log_printf (psf, "bext : %u\n", dword) ;
550 if ((error = wav_read_bext_chunk (psf, dword)))
554 case strc_MARKER : /* Multiple of 32 bytes. */
564 parsestage |= HAVE_other ;
566 psf_binheader_readf (psf, "4", &dword) ;
567 psf_log_printf (psf, "%M : %u\n", marker, dword) ;
568 dword += (dword & 1) ;
569 psf_binheader_readf (psf, "j", dword) ;
573 parsestage |= HAVE_other ;
574 if (isprint ((marker >> 24) & 0xFF) && isprint ((marker >> 16) & 0xFF)
575 && isprint ((marker >> 8) & 0xFF) && isprint (marker & 0xFF))
576 { psf_binheader_readf (psf, "4", &dword) ;
577 psf_log_printf (psf, "*** %M : %d (unknown marker)\n", marker, dword) ;
578 psf_binheader_readf (psf, "j", dword) ;
581 if (psf_ftell (psf) & 0x03)
582 { psf_log_printf (psf, " Unknown chunk marker at position %d. Resynching.\n", dword - 4) ;
583 psf_binheader_readf (psf, "j", -3) ;
586 psf_log_printf (psf, "*** Unknown chunk marker (%X) at position %D. Exiting parser.\n", marker, psf_ftell (psf) - 4) ;
589 } ; /* switch (dword) */
591 if (! psf->sf.seekable && (parsestage & HAVE_data))
594 if (psf_ftell (psf) >= psf->filelength - SIGNED_SIZEOF (dword))
595 { psf_log_printf (psf, "End\n") ;
600 if (! psf->dataoffset)
601 return SFE_WAV_NO_DATA ;
603 /* WAVs can be little or big endian */
604 psf->endian = psf->rwf_endian ;
606 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
608 if (psf->is_pipe == 0)
610 ** Check for 'wvpk' at the start of the DATA section. Not able to
613 psf_binheader_readf (psf, "4", &marker) ;
614 if (marker == wvpk_MARKER || marker == OggS_MARKER)
615 return SFE_WAV_WVPK_DATA ;
618 /* Seek to start of DATA section. */
619 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
622 { if (psf->filelength - psf->dataoffset < psf->datalength)
623 psf->sf.frames = (psf->filelength - psf->dataoffset) / psf->blockwidth ;
625 psf->sf.frames = psf->datalength / psf->blockwidth ;
629 { case WAVE_FORMAT_EXTENSIBLE :
630 if (psf->sf.format == (SF_FORMAT_WAVEX | SF_FORMAT_MS_ADPCM))
631 { *blockalign = wav_fmt.msadpcm.blockalign ;
632 *framesperblock = wav_fmt.msadpcm.samplesperblock ;
636 case WAVE_FORMAT_PCM :
637 psf->sf.format = SF_FORMAT_WAV | u_bitwidth_to_subformat (psf->bytewidth * 8) ;
640 case WAVE_FORMAT_MULAW :
641 case IBM_FORMAT_MULAW :
642 psf->sf.format = (SF_FORMAT_WAV | SF_FORMAT_ULAW) ;
645 case WAVE_FORMAT_ALAW :
646 case IBM_FORMAT_ALAW :
647 psf->sf.format = (SF_FORMAT_WAV | SF_FORMAT_ALAW) ;
650 case WAVE_FORMAT_MS_ADPCM :
651 psf->sf.format = (SF_FORMAT_WAV | SF_FORMAT_MS_ADPCM) ;
652 *blockalign = wav_fmt.msadpcm.blockalign ;
653 *framesperblock = wav_fmt.msadpcm.samplesperblock ;
656 case WAVE_FORMAT_IMA_ADPCM :
657 psf->sf.format = (SF_FORMAT_WAV | SF_FORMAT_IMA_ADPCM) ;
658 *blockalign = wav_fmt.ima.blockalign ;
659 *framesperblock = wav_fmt.ima.samplesperblock ;
662 case WAVE_FORMAT_GSM610 :
663 psf->sf.format = (SF_FORMAT_WAV | SF_FORMAT_GSM610) ;
666 case WAVE_FORMAT_IEEE_FLOAT :
667 psf->sf.format = SF_FORMAT_WAV ;
668 psf->sf.format |= (psf->bytewidth == 8) ? SF_FORMAT_DOUBLE : SF_FORMAT_FLOAT ;
671 case WAVE_FORMAT_G721_ADPCM :
672 psf->sf.format = SF_FORMAT_WAV | SF_FORMAT_G721_32 ;
675 default : return SFE_UNIMPLEMENTED ;
678 /* Only set the format endian-ness if its non-standard big-endian. */
679 if (psf->endian == SF_ENDIAN_BIG)
680 psf->sf.format |= SF_ENDIAN_BIG ;
683 } /* wav_read_header */
686 wav_write_header (SF_PRIVATE *psf, int calc_length)
687 { sf_count_t current ;
688 int fmt_size, k, subformat, add_fact_chunk = SF_FALSE ;
690 current = psf_ftell (psf) ;
693 { psf->filelength = psf_get_filelen (psf) ;
695 psf->datalength = psf->filelength - psf->dataoffset ;
698 psf->datalength -= psf->filelength - psf->dataend ;
700 if (psf->bytewidth > 0)
701 psf->sf.frames = psf->datalength / (psf->bytewidth * psf->sf.channels) ;
704 /* Reset the current header length to zero. */
705 psf->header [0] = 0 ;
707 psf_fseek (psf, 0, SEEK_SET) ;
710 ** RIFX signifies big-endian format for all header and data.
711 ** To prevent lots of code copying here, we'll set the psf->rwf_endian flag
712 ** once here, and never specify endian-ness for all other header operations.
715 /* RIFF/RIFX marker, length, WAVE and 'fmt ' markers. */
717 if (psf->endian == SF_ENDIAN_LITTLE)
718 psf_binheader_writef (psf, "etm8", RIFF_MARKER, (psf->filelength < 8) ? 8 : psf->filelength - 8) ;
720 psf_binheader_writef (psf, "Etm8", RIFX_MARKER, (psf->filelength < 8) ? 8 : psf->filelength - 8) ;
722 /* WAVE and 'fmt ' markers. */
723 psf_binheader_writef (psf, "mm", WAVE_MARKER, fmt_MARKER) ;
725 subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
728 { case SF_FORMAT_PCM_U8 :
729 case SF_FORMAT_PCM_16 :
730 case SF_FORMAT_PCM_24 :
731 case SF_FORMAT_PCM_32 :
732 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 ;
734 /* fmt : format, channels, samplerate */
735 psf_binheader_writef (psf, "4224", fmt_size, WAVE_FORMAT_PCM, psf->sf.channels, psf->sf.samplerate) ;
736 /* fmt : bytespersec */
737 psf_binheader_writef (psf, "4", psf->sf.samplerate * psf->bytewidth * psf->sf.channels) ;
738 /* fmt : blockalign, bitwidth */
739 psf_binheader_writef (psf, "22", psf->bytewidth * psf->sf.channels, psf->bytewidth * 8) ;
742 case SF_FORMAT_FLOAT :
743 case SF_FORMAT_DOUBLE :
744 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 ;
746 /* fmt : format, channels, samplerate */
747 psf_binheader_writef (psf, "4224", fmt_size, WAVE_FORMAT_IEEE_FLOAT, psf->sf.channels, psf->sf.samplerate) ;
748 /* fmt : bytespersec */
749 psf_binheader_writef (psf, "4", psf->sf.samplerate * psf->bytewidth * psf->sf.channels) ;
750 /* fmt : blockalign, bitwidth */
751 psf_binheader_writef (psf, "22", psf->bytewidth * psf->sf.channels, psf->bytewidth * 8) ;
753 add_fact_chunk = SF_TRUE ;
756 case SF_FORMAT_ULAW :
757 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 ;
759 /* fmt : format, channels, samplerate */
760 psf_binheader_writef (psf, "4224", fmt_size, WAVE_FORMAT_MULAW, psf->sf.channels, psf->sf.samplerate) ;
761 /* fmt : bytespersec */
762 psf_binheader_writef (psf, "4", psf->sf.samplerate * psf->bytewidth * psf->sf.channels) ;
763 /* fmt : blockalign, bitwidth */
764 psf_binheader_writef (psf, "22", psf->bytewidth * psf->sf.channels, 8) ;
766 add_fact_chunk = SF_TRUE ;
769 case SF_FORMAT_ALAW :
770 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 ;
772 /* fmt : format, channels, samplerate */
773 psf_binheader_writef (psf, "4224", fmt_size, WAVE_FORMAT_ALAW, psf->sf.channels, psf->sf.samplerate) ;
774 /* fmt : bytespersec */
775 psf_binheader_writef (psf, "4", psf->sf.samplerate * psf->bytewidth * psf->sf.channels) ;
776 /* fmt : blockalign, bitwidth */
777 psf_binheader_writef (psf, "22", psf->bytewidth * psf->sf.channels, 8) ;
779 add_fact_chunk = SF_TRUE ;
782 /* Lite remove start */
783 case SF_FORMAT_IMA_ADPCM :
784 { int blockalign, framesperblock, bytespersec ;
786 blockalign = wav_w64_srate2blocksize (psf->sf.samplerate * psf->sf.channels) ;
787 framesperblock = 2 * (blockalign - 4 * psf->sf.channels) / psf->sf.channels + 1 ;
788 bytespersec = (psf->sf.samplerate * blockalign) / framesperblock ;
791 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 + 2 ;
793 /* fmt : size, WAV format type, channels, samplerate, bytespersec */
794 psf_binheader_writef (psf, "42244", fmt_size, WAVE_FORMAT_IMA_ADPCM,
795 psf->sf.channels, psf->sf.samplerate, bytespersec) ;
797 /* fmt : blockalign, bitwidth, extrabytes, framesperblock. */
798 psf_binheader_writef (psf, "2222", blockalign, 4, 2, framesperblock) ;
801 add_fact_chunk = SF_TRUE ;
804 case SF_FORMAT_MS_ADPCM :
805 { int blockalign, framesperblock, bytespersec, extrabytes ;
807 blockalign = wav_w64_srate2blocksize (psf->sf.samplerate * psf->sf.channels) ;
808 framesperblock = 2 + 2 * (blockalign - 7 * psf->sf.channels) / psf->sf.channels ;
809 bytespersec = (psf->sf.samplerate * blockalign) / framesperblock ;
812 extrabytes = 2 + 2 + MSADPCM_ADAPT_COEFF_COUNT * (2 + 2) ;
813 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 + extrabytes ;
815 /* fmt : size, WAV format type, channels. */
816 psf_binheader_writef (psf, "422", fmt_size, WAVE_FORMAT_MS_ADPCM, psf->sf.channels) ;
818 /* fmt : samplerate, bytespersec. */
819 psf_binheader_writef (psf, "44", psf->sf.samplerate, bytespersec) ;
821 /* fmt : blockalign, bitwidth, extrabytes, framesperblock. */
822 psf_binheader_writef (psf, "22222", blockalign, 4, extrabytes, framesperblock, 7) ;
824 msadpcm_write_adapt_coeffs (psf) ;
827 add_fact_chunk = SF_TRUE ;
831 case SF_FORMAT_G721_32 :
833 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 + 2 ;
835 /* fmt : size, WAV format type, channels, samplerate, bytespersec */
836 psf_binheader_writef (psf, "42244", fmt_size, WAVE_FORMAT_G721_ADPCM,
837 psf->sf.channels, psf->sf.samplerate, psf->sf.samplerate * psf->sf.channels / 2) ;
839 /* fmt : blockalign, bitwidth, extrabytes, auxblocksize. */
840 psf_binheader_writef (psf, "2222", 64, 4, 2, 0) ;
842 add_fact_chunk = SF_TRUE ;
845 /* Lite remove end */
847 case SF_FORMAT_GSM610 :
848 { int blockalign, framesperblock, bytespersec ;
850 blockalign = WAV_W64_GSM610_BLOCKSIZE ;
851 framesperblock = WAV_W64_GSM610_SAMPLES ;
852 bytespersec = (psf->sf.samplerate * blockalign) / framesperblock ;
855 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 + 2 ;
857 /* fmt : size, WAV format type, channels. */
858 psf_binheader_writef (psf, "422", fmt_size, WAVE_FORMAT_GSM610, psf->sf.channels) ;
860 /* fmt : samplerate, bytespersec. */
861 psf_binheader_writef (psf, "44", psf->sf.samplerate, bytespersec) ;
863 /* fmt : blockalign, bitwidth, extrabytes, framesperblock. */
864 psf_binheader_writef (psf, "2222", blockalign, 0, 2, framesperblock) ;
867 add_fact_chunk = SF_TRUE ;
870 default : return SFE_UNIMPLEMENTED ;
874 psf_binheader_writef (psf, "tm48", fact_MARKER, 4, psf->sf.frames) ;
876 if (psf->str_flags & SF_STR_LOCATE_START)
877 wav_write_strings (psf, SF_STR_LOCATE_START) ;
879 if (psf->peak_info != NULL && psf->peak_info->peak_loc == SF_PEAK_START)
880 { psf_binheader_writef (psf, "m4", PEAK_MARKER, WAV_PEAK_CHUNK_SIZE (psf->sf.channels)) ;
881 psf_binheader_writef (psf, "44", 1, time (NULL)) ;
882 for (k = 0 ; k < psf->sf.channels ; k++)
883 psf_binheader_writef (psf, "ft8", (float) psf->peak_info->peaks [k].value, psf->peak_info->peaks [k].position) ;
886 if (psf->broadcast_info != NULL)
887 wav_write_bext_chunk (psf) ;
889 if (psf->instrument != NULL)
891 double dtune = (double) (0x40000000) / 25.0 ;
893 psf_binheader_writef (psf, "m4", smpl_MARKER, 9 * 4 + psf->instrument->loop_count * 6 * 4) ;
894 psf_binheader_writef (psf, "44", 0, 0) ; /* Manufacturer zero is everyone */
895 tmp = (int) (1.0e9 / psf->sf.samplerate) ; /* Sample period in nano seconds */
896 psf_binheader_writef (psf, "44", tmp, psf->instrument->basenote) ;
897 tmp = (unsigned int) (psf->instrument->detune * dtune + 0.5) ;
898 psf_binheader_writef (psf, "4", tmp) ;
899 psf_binheader_writef (psf, "44", 0, 0) ; /* SMTPE format */
900 psf_binheader_writef (psf, "44", psf->instrument->loop_count, 0) ;
902 for (tmp = 0 ; tmp < psf->instrument->loop_count ; tmp++)
905 type = psf->instrument->loops [tmp].mode ;
906 type = (type == SF_LOOP_FORWARD ? 0 : type==SF_LOOP_BACKWARD ? 2 : type == SF_LOOP_ALTERNATING ? 1 : 32) ;
908 psf_binheader_writef (psf, "44", tmp, type) ;
909 psf_binheader_writef (psf, "44", psf->instrument->loops [tmp].start, psf->instrument->loops [tmp].end) ;
910 psf_binheader_writef (psf, "44", 0, psf->instrument->loops [tmp].count) ;
914 psf_binheader_writef (psf, "tm8", data_MARKER, psf->datalength) ;
915 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
919 psf->dataoffset = psf->headindex ;
921 if (current < psf->dataoffset)
922 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
923 else if (current > 0)
924 psf_fseek (psf, current, SEEK_SET) ;
927 } /* wav_write_header */
932 wavex_write_header (SF_PRIVATE *psf, int calc_length)
933 { sf_count_t current ;
934 int fmt_size, k, subformat, add_fact_chunk = SF_FALSE ;
936 current = psf_ftell (psf) ;
939 { psf->filelength = psf_get_filelen (psf) ;
941 psf->datalength = psf->filelength - psf->dataoffset ;
944 psf->datalength -= psf->filelength - psf->dataend ;
946 if (psf->bytewidth > 0)
947 psf->sf.frames = psf->datalength / (psf->bytewidth * psf->sf.channels) ;
951 /* Reset the current header length to zero. */
952 psf->header [0] = 0 ;
954 psf_fseek (psf, 0, SEEK_SET) ;
956 /* RIFX signifies big-endian format for all header and data
957 ** to prevent lots of code copying here, we'll set the psf->rwf_endian
958 ** flag once here, and never specify endian-ness for all other header ops
961 /* RIFF marker, length, WAVE and 'fmt ' markers. */
963 if (psf->endian == SF_ENDIAN_LITTLE)
964 { if (psf->filelength < 8)
965 psf_binheader_writef (psf, "tm8", RIFF_MARKER, 8) ;
967 psf_binheader_writef (psf, "tm8", RIFF_MARKER, psf->filelength - 8) ;
970 { if (psf->filelength < 8)
971 psf_binheader_writef (psf, "Etm8", RIFX_MARKER, 8) ;
973 psf_binheader_writef (psf, "Etm8", RIFX_MARKER, psf->filelength - 8) ;
976 /* WAVE and 'fmt ' markers. */
977 psf_binheader_writef (psf, "mm", WAVE_MARKER, fmt_MARKER) ;
979 subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
981 /* initial section (same for all, it appears) */
983 { case SF_FORMAT_PCM_U8 :
984 case SF_FORMAT_PCM_16 :
985 case SF_FORMAT_PCM_24 :
986 case SF_FORMAT_PCM_32 :
987 case SF_FORMAT_FLOAT :
988 case SF_FORMAT_DOUBLE :
989 case SF_FORMAT_ULAW :
990 case SF_FORMAT_ALAW :
991 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 + 2 + 4 + 4 + 2 + 2 + 8 ;
993 /* fmt : format, channels, samplerate */
994 psf_binheader_writef (psf, "4224", fmt_size, WAVE_FORMAT_EXTENSIBLE, psf->sf.channels, psf->sf.samplerate) ;
995 /* fmt : bytespersec */
996 psf_binheader_writef (psf, "4", psf->sf.samplerate * psf->bytewidth * psf->sf.channels) ;
997 /* fmt : blockalign, bitwidth */
998 psf_binheader_writef (psf, "22", psf->bytewidth * psf->sf.channels, psf->bytewidth * 8) ;
1000 /* cbSize 22 is sizeof (WAVEFORMATEXTENSIBLE) - sizeof (WAVEFORMATEX) */
1001 psf_binheader_writef (psf, "2", 22) ;
1003 /* wValidBitsPerSample, for our use same as bitwidth as we use it fully */
1004 psf_binheader_writef (psf, "2", psf->bytewidth * 8) ;
1007 ** Ok some liberty is taken here to use the most commonly used channel masks
1008 ** instead of "no mapping". If you really want to use "no mapping" for 8 channels and less
1009 ** please don't use wavex. (otherwise we'll have to create a new SF_COMMAND)
1011 switch (psf->sf.channels)
1012 { case 1 : /* center channel mono */
1013 psf_binheader_writef (psf, "4", 0x4) ;
1016 case 2 : /* front left and right */
1017 psf_binheader_writef (psf, "4", 0x1 | 0x2) ;
1021 psf_binheader_writef (psf, "4", 0x1 | 0x2 | 0x10 | 0x20) ;
1025 psf_binheader_writef (psf, "4", 0x1 | 0x2 | 0x4 | 0x8 | 0x10 | 0x20) ;
1029 psf_binheader_writef (psf, "4", 0x1 | 0x2 | 0x4 | 0x8 | 0x10 | 0x20 | 0x40 | 0x80) ;
1032 default : /* 0 when in doubt , use direct out, ie NO mapping*/
1033 psf_binheader_writef (psf, "4", 0x0) ;
1039 case SF_FORMAT_MS_ADPCM : /* Todo, GUID exists might have different header as per wav_write_header */
1041 return SFE_UNIMPLEMENTED ;
1044 /* GUID section, different for each */
1047 { case SF_FORMAT_PCM_U8 :
1048 case SF_FORMAT_PCM_16 :
1049 case SF_FORMAT_PCM_24 :
1050 case SF_FORMAT_PCM_32 :
1051 wavex_write_guid (psf, &MSGUID_SUBTYPE_PCM) ;
1054 case SF_FORMAT_FLOAT :
1055 case SF_FORMAT_DOUBLE :
1056 wavex_write_guid (psf, &MSGUID_SUBTYPE_IEEE_FLOAT) ;
1057 add_fact_chunk = SF_TRUE ;
1060 case SF_FORMAT_ULAW :
1061 wavex_write_guid (psf, &MSGUID_SUBTYPE_MULAW) ;
1062 add_fact_chunk = SF_TRUE ;
1065 case SF_FORMAT_ALAW :
1066 wavex_write_guid (psf, &MSGUID_SUBTYPE_ALAW) ;
1067 add_fact_chunk = SF_TRUE ;
1070 case SF_FORMAT_MS_ADPCM : /* todo, GUID exists */
1072 default : return SFE_UNIMPLEMENTED ;
1076 psf_binheader_writef (psf, "tm48", fact_MARKER, 4, psf->sf.frames) ;
1078 if (psf->str_flags & SF_STR_LOCATE_START)
1079 wav_write_strings (psf, SF_STR_LOCATE_START) ;
1081 if (psf->peak_info != NULL && psf->peak_info->peak_loc == SF_PEAK_START)
1082 { psf_binheader_writef (psf, "m4", PEAK_MARKER, WAV_PEAK_CHUNK_SIZE (psf->sf.channels)) ;
1083 psf_binheader_writef (psf, "44", 1, time (NULL)) ;
1084 for (k = 0 ; k < psf->sf.channels ; k++)
1085 psf_binheader_writef (psf, "ft8", (float) psf->peak_info->peaks [k].value, psf->peak_info->peaks [k].position) ;
1088 psf_binheader_writef (psf, "tm8", data_MARKER, psf->datalength) ;
1089 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
1093 psf->dataoffset = psf->headindex ;
1095 if (current < psf->dataoffset)
1096 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
1097 else if (current > 0)
1098 psf_fseek (psf, current, SEEK_SET) ;
1101 } /* wavex_write_header */
1106 wav_write_tailer (SF_PRIVATE *psf)
1109 /* Reset the current header buffer length to zero. */
1110 psf->header [0] = 0 ;
1111 psf->headindex = 0 ;
1113 psf->dataend = psf_fseek (psf, 0, SEEK_END) ;
1115 /* Add a PEAK chunk if requested. */
1116 if (psf->peak_info != NULL && psf->peak_info->peak_loc == SF_PEAK_END)
1117 { psf_binheader_writef (psf, "m4", PEAK_MARKER, WAV_PEAK_CHUNK_SIZE (psf->sf.channels)) ;
1118 psf_binheader_writef (psf, "44", 1, time (NULL)) ;
1119 for (k = 0 ; k < psf->sf.channels ; k++)
1120 psf_binheader_writef (psf, "f4", psf->peak_info->peaks [k].value, psf->peak_info->peaks [k].position) ;
1123 if (psf->str_flags & SF_STR_LOCATE_END)
1124 wav_write_strings (psf, SF_STR_LOCATE_END) ;
1126 /* Write the tailer. */
1127 if (psf->headindex > 0)
1128 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
1131 } /* wav_write_tailer */
1134 wav_write_strings (SF_PRIVATE *psf, int location)
1135 { int k, prev_head_index, saved_head_index ;
1137 prev_head_index = psf->headindex + 4 ;
1139 psf_binheader_writef (psf, "m4m", LIST_MARKER, 0xBADBAD, INFO_MARKER) ;
1141 for (k = 0 ; k < SF_MAX_STRINGS ; k++)
1142 { if (psf->strings [k].type == 0)
1144 if (psf->strings [k].flags != location)
1147 switch (psf->strings [k].type)
1148 { case SF_STR_SOFTWARE :
1149 psf_binheader_writef (psf, "ms", ISFT_MARKER, psf->strings [k].str) ;
1153 psf_binheader_writef (psf, "ms", INAM_MARKER, psf->strings [k].str) ;
1156 case SF_STR_COPYRIGHT :
1157 psf_binheader_writef (psf, "ms", ICOP_MARKER, psf->strings [k].str) ;
1160 case SF_STR_ARTIST :
1161 psf_binheader_writef (psf, "ms", IART_MARKER, psf->strings [k].str) ;
1164 case SF_STR_COMMENT :
1165 psf_binheader_writef (psf, "ms", ICMT_MARKER, psf->strings [k].str) ;
1169 psf_binheader_writef (psf, "ms", ICRD_MARKER, psf->strings [k].str) ;
1174 saved_head_index = psf->headindex ;
1175 psf->headindex = prev_head_index ;
1176 psf_binheader_writef (psf, "4", saved_head_index - prev_head_index - 4) ;
1177 psf->headindex = saved_head_index ;
1179 } /* wav_write_strings */
1182 wav_close (SF_PRIVATE *psf)
1184 if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
1185 { wav_write_tailer (psf) ;
1187 psf->write_header (psf, SF_TRUE) ;
1194 wav_command (SF_PRIVATE *psf, int command, void *data, int datasize)
1196 /* Avoid compiler warnings. */
1199 datasize = datasize ;
1209 wav_subchunk_parse (SF_PRIVATE *psf, int chunk)
1210 { sf_count_t current_pos ;
1212 int dword, bytesread, length ;
1214 current_pos = psf_fseek (psf, 0, SEEK_CUR) ;
1216 bytesread = psf_binheader_readf (psf, "4", &length) ;
1219 { /* This case is for broken files generated by PEAK. */
1220 psf_log_printf (psf, "%M : %d (weird length)\n", chunk, length) ;
1221 psf_binheader_readf (psf, "mj", &chunk, length - 4) ;
1222 psf_log_printf (psf, " %M\n", chunk) ;
1226 if (psf->headindex + length > SIGNED_SIZEOF (psf->header))
1227 { psf_log_printf (psf, "%M : %d (too long)\n", chunk, length) ;
1228 psf_binheader_readf (psf, "j", length) ;
1232 if (current_pos + length > psf->filelength)
1233 { psf_log_printf (psf, "%M : %d (should be %d)\n", chunk, length, (int) (psf->filelength - current_pos)) ;
1234 length = psf->filelength - current_pos ;
1237 psf_log_printf (psf, "%M : %d\n", chunk, length) ;
1239 while (bytesread < length)
1240 { bytesread += psf_binheader_readf (psf, "m", &chunk) ;
1243 { case adtl_MARKER :
1245 /* These markers don't contain anything. */
1246 psf_log_printf (psf, " %M\n", chunk) ;
1250 psf_log_printf (psf, " %M inside a LIST block??? Backing out.\n", chunk) ;
1251 /* Jump back four bytes and return to caller. */
1252 psf_binheader_readf (psf, "j", -4) ;
1267 bytesread += psf_binheader_readf (psf, "4", &dword) ;
1268 dword += (dword & 1) ;
1269 if (dword < 0 || dword > SIGNED_SIZEOF (psf->u.cbuf))
1270 { psf_log_printf (psf, " *** %M : %d (too big)\n", chunk, dword) ;
1271 psf_binheader_readf (psf, "j", dword) ;
1275 cptr = psf->u.cbuf ;
1276 psf_binheader_readf (psf, "b", cptr, dword) ;
1277 bytesread += dword ;
1278 cptr [dword - 1] = 0 ;
1279 psf_log_printf (psf, " %M : %s\n", chunk, cptr) ;
1285 bytesread += psf_binheader_readf (psf, "44", &dword, &mark_id) ;
1287 dword += (dword & 1) ;
1288 if (dword < 1 || dword > SIGNED_SIZEOF (psf->u.cbuf))
1289 { psf_log_printf (psf, " *** %M : %d (too big)\n", chunk, dword) ;
1290 psf_binheader_readf (psf, "j", dword) ;
1294 cptr = psf->u.cbuf ;
1295 psf_binheader_readf (psf, "b", cptr, dword) ;
1296 bytesread += dword ;
1297 cptr [dword - 1] = 0 ;
1298 psf_log_printf (psf, " %M : %d : %s\n", chunk, mark_id, cptr) ;
1306 bytesread += psf_binheader_readf (psf, "4", &dword) ;
1307 dword += (dword & 1) ;
1308 psf_binheader_readf (psf, "j", dword) ;
1309 bytesread += dword ;
1310 psf_log_printf (psf, " %M : %d\n", chunk, dword) ;
1314 psf_binheader_readf (psf, "4", &dword) ;
1315 bytesread += sizeof (dword) ;
1316 dword += (dword & 1) ;
1317 psf_binheader_readf (psf, "j", dword) ;
1318 bytesread += dword ;
1319 psf_log_printf (psf, " *** %M : %d\n", chunk, dword) ;
1326 { case ISFT_MARKER :
1327 psf_store_string (psf, SF_STR_SOFTWARE, psf->u.cbuf) ;
1330 psf_store_string (psf, SF_STR_COPYRIGHT, psf->u.cbuf) ;
1333 psf_store_string (psf, SF_STR_TITLE, psf->u.cbuf) ;
1336 psf_store_string (psf, SF_STR_ARTIST, psf->u.cbuf) ;
1339 psf_store_string (psf, SF_STR_COMMENT, psf->u.cbuf) ;
1342 psf_store_string (psf, SF_STR_DATE, psf->u.cbuf) ;
1347 current_pos = psf_fseek (psf, 0, SEEK_CUR) - current_pos ;
1349 if (current_pos - 4 != length)
1350 psf_log_printf (psf, "**** Bad chunk length %d sbould be %D\n", length, current_pos - 4) ;
1353 } /* wav_subchunk_parse */
1356 wav_read_smpl_chunk (SF_PRIVATE *psf, unsigned int chunklen)
1357 { unsigned int bytesread = 0, dword, sampler_data, loop_count ;
1358 unsigned int note, start, end, type = -1, count ;
1361 chunklen += (chunklen & 1) ;
1363 bytesread += psf_binheader_readf (psf, "4", &dword) ;
1364 psf_log_printf (psf, " Manufacturer : %X\n", dword) ;
1366 bytesread += psf_binheader_readf (psf, "4", &dword) ;
1367 psf_log_printf (psf, " Product : %u\n", dword) ;
1369 bytesread += psf_binheader_readf (psf, "4", &dword) ;
1370 psf_log_printf (psf, " Period : %u nsec\n", dword) ;
1372 bytesread += psf_binheader_readf (psf, "4", ¬e) ;
1373 psf_log_printf (psf, " Midi Note : %u\n", note) ;
1375 bytesread += psf_binheader_readf (psf, "4", &dword) ;
1377 { LSF_SNPRINTF (psf->u.cbuf, sizeof (psf->u.cbuf), "%f",
1378 (1.0 * 0x80000000) / ((unsigned int) dword)) ;
1379 psf_log_printf (psf, " Pitch Fract. : %s\n", psf->u.cbuf) ;
1382 psf_log_printf (psf, " Pitch Fract. : 0\n") ;
1384 bytesread += psf_binheader_readf (psf, "4", &dword) ;
1385 psf_log_printf (psf, " SMPTE Format : %u\n", dword) ;
1387 bytesread += psf_binheader_readf (psf, "4", &dword) ;
1388 LSF_SNPRINTF (psf->u.cbuf, sizeof (psf->u.cbuf), "%02d:%02d:%02d %02d",
1389 (dword >> 24) & 0x7F, (dword >> 16) & 0x7F, (dword >> 8) & 0x7F, dword & 0x7F) ;
1390 psf_log_printf (psf, " SMPTE Offset : %s\n", psf->u.cbuf) ;
1392 bytesread += psf_binheader_readf (psf, "4", &loop_count) ;
1393 psf_log_printf (psf, " Loop Count : %u\n", loop_count) ;
1395 /* Sampler Data holds the number of data bytes after the CUE chunks which
1396 ** is not actually CUE data. Display value after CUE data.
1398 bytesread += psf_binheader_readf (psf, "4", &sampler_data) ;
1400 if ((psf->instrument = psf_instrument_alloc ()) == NULL)
1401 return SFE_MALLOC_FAILED ;
1403 psf->instrument->loop_count = loop_count ;
1405 for (j = 0 ; loop_count > 0 && chunklen - bytesread >= 24 ; j ++)
1406 { bytesread += psf_binheader_readf (psf, "4", &dword) ;
1407 psf_log_printf (psf, " Cue ID : %2u", dword) ;
1409 bytesread += psf_binheader_readf (psf, "4", &type) ;
1410 psf_log_printf (psf, " Type : %2u", type) ;
1412 bytesread += psf_binheader_readf (psf, "4", &start) ;
1413 psf_log_printf (psf, " Start : %5u", start) ;
1415 bytesread += psf_binheader_readf (psf, "4", &end) ;
1416 psf_log_printf (psf, " End : %5u", end) ;
1418 bytesread += psf_binheader_readf (psf, "4", &dword) ;
1419 psf_log_printf (psf, " Fraction : %5u", dword) ;
1421 bytesread += psf_binheader_readf (psf, "4", &count) ;
1422 psf_log_printf (psf, " Count : %5u\n", count) ;
1424 if (j < ARRAY_LEN (psf->instrument->loops))
1425 { psf->instrument->loops [j].start = start ;
1426 psf->instrument->loops [j].end = end ;
1427 psf->instrument->loops [j].count = count ;
1431 psf->instrument->loops [j].mode = SF_LOOP_FORWARD ;
1434 psf->instrument->loops [j].mode = SF_LOOP_ALTERNATING ;
1437 psf->instrument->loops [j].mode = SF_LOOP_BACKWARD ;
1440 psf->instrument->loops [j].mode = SF_LOOP_NONE ;
1448 if (chunklen - bytesread == 0)
1449 { if (sampler_data != 0)
1450 psf_log_printf (psf, " Sampler Data : %u (should be 0)\n", sampler_data) ;
1452 psf_log_printf (psf, " Sampler Data : %u\n", sampler_data) ;
1455 { if (sampler_data != chunklen - bytesread)
1456 { psf_log_printf (psf, " Sampler Data : %u (should have been %u)\n", sampler_data, chunklen - bytesread) ;
1457 sampler_data = chunklen - bytesread ;
1460 psf_log_printf (psf, " Sampler Data : %u\n", sampler_data) ;
1462 psf_log_printf (psf, " ") ;
1463 for (k = 0 ; k < (int) sampler_data ; k++)
1466 if (k > 0 && (k % 20) == 0)
1467 psf_log_printf (psf, "\n ") ;
1469 bytesread += psf_binheader_readf (psf, "1", &ch) ;
1470 psf_log_printf (psf, "%02X ", ch & 0xFF) ;
1473 psf_log_printf (psf, "\n") ;
1476 psf->instrument->basenote = note ;
1477 psf->instrument->gain = 1 ;
1478 psf->instrument->velocity_lo = psf->instrument->key_lo = 0 ;
1479 psf->instrument->velocity_hi = psf->instrument->key_hi = 127 ;
1482 } /* wav_read_smpl_chunk */
1485 ** The acid chunk goes a little something like this:
1488 ** 4 bytes (int) length of chunk starting at next byte
1490 ** 4 bytes (int) type of file:
1491 ** this appears to be a bit mask,however some combinations
1492 ** are probably impossible and/or qualified as "errors"
1494 ** 0x01 On: One Shot Off: Loop
1495 ** 0x02 On: Root note is Set Off: No root
1496 ** 0x04 On: Stretch is On, Off: Strech is OFF
1497 ** 0x08 On: Disk Based Off: Ram based
1498 ** 0x10 On: ?????????? Off: ????????? (Acidizer puts that ON)
1500 ** 2 bytes (short) root note
1501 ** if type 0x10 is OFF : [C,C#,(...),B] -> [0x30 to 0x3B]
1502 ** if type 0x10 is ON : [C,C#,(...),B] -> [0x3C to 0x47]
1503 ** (both types fit on same MIDI pitch albeit different octaves, so who cares)
1505 ** 2 bytes (short) ??? always set to 0x8000
1506 ** 4 bytes (float) ??? seems to be always 0
1507 ** 4 bytes (int) number of beats
1508 ** 2 bytes (short) meter denominator //always 4 in SF/ACID
1509 ** 2 bytes (short) meter numerator //always 4 in SF/ACID
1510 ** //are we sure about the order?? usually its num/denom
1511 ** 4 bytes (float) tempo
1516 wav_read_acid_chunk (SF_PRIVATE *psf, unsigned int chunklen)
1517 { unsigned int bytesread = 0 ;
1519 short rootnote, q1, meter_denom, meter_numer ;
1522 chunklen += (chunklen & 1) ;
1524 bytesread += psf_binheader_readf (psf, "422f", &flags, &rootnote, &q1, &q2) ;
1526 LSF_SNPRINTF (psf->u.cbuf, sizeof (psf->u.cbuf), "%f", q2) ;
1528 psf_log_printf (psf, " Flags : 0x%04x (%s,%s,%s,%s,%s)\n", flags,
1529 (flags & 0x01) ? "OneShot" : "Loop",
1530 (flags & 0x02) ? "RootNoteValid" : "RootNoteInvalid",
1531 (flags & 0x04) ? "StretchOn" : "StretchOff",
1532 (flags & 0x08) ? "DiskBased" : "RAMBased",
1533 (flags & 0x10) ? "??On" : "??Off") ;
1535 psf_log_printf (psf, " Root note : 0x%x\n ???? : 0x%04x\n ???? : %s\n",
1536 rootnote, q1, psf->u.cbuf) ;
1538 bytesread += psf_binheader_readf (psf, "422f", &beats, &meter_denom, &meter_numer, &tempo) ;
1539 LSF_SNPRINTF (psf->u.cbuf, sizeof (psf->u.cbuf), "%f", tempo) ;
1540 psf_log_printf (psf, " Beats : %d\n Meter : %d/%d\n Tempo : %s\n",
1541 beats, meter_numer, meter_denom, psf->u.cbuf) ;
1543 psf_binheader_readf (psf, "j", chunklen - bytesread) ;
1545 if ((psf->loop_info = calloc (1, sizeof (SF_LOOP_INFO))) == NULL)
1546 return SFE_MALLOC_FAILED ;
1548 psf->loop_info->time_sig_num = meter_numer ;
1549 psf->loop_info->time_sig_den = meter_denom ;
1550 psf->loop_info->loop_mode = (flags & 0x01) ? SF_LOOP_NONE : SF_LOOP_FORWARD ;
1551 psf->loop_info->num_beats = beats ;
1552 psf->loop_info->bpm = tempo ;
1553 psf->loop_info->root_key = (flags & 0x02) ? rootnote : -1 ;
1556 } /* wav_read_acid_chunk */
1559 wav_read_bext_chunk (SF_PRIVATE *psf, unsigned int chunksize)
1561 SF_BROADCAST_INFO* b ;
1563 if ((psf->broadcast_info = calloc (1, sizeof (SF_BROADCAST_INFO))) == NULL)
1564 { psf->error = SFE_MALLOC_FAILED ;
1568 b = psf->broadcast_info ;
1570 psf_binheader_readf (psf, "b", b->description, sizeof (b->description)) ;
1571 psf_binheader_readf (psf, "b", b->originator, sizeof (b->originator)) ;
1572 psf_binheader_readf (psf, "b", b->originator_reference, sizeof (b->originator_reference)) ;
1573 psf_binheader_readf (psf, "b", b->origination_date, sizeof (b->origination_date)) ;
1574 psf_binheader_readf (psf, "b", b->origination_time, sizeof (b->origination_time)) ;
1575 psf_binheader_readf (psf, "442", &b->time_reference_low, &b->time_reference_high, &b->version) ;
1576 psf_binheader_readf (psf, "bj", &b->umid, sizeof (b->umid), 190) ;
1578 if (chunksize > WAV_BEXT_CHUNK_SIZE)
1579 { /* File has coding history data. */
1581 b->coding_history_size = chunksize - WAV_BEXT_CHUNK_SIZE ;
1583 if (b->coding_history_size > SIGNED_SIZEOF (b->coding_history))
1584 { free (psf->broadcast_info) ;
1585 psf->broadcast_info = NULL ;
1586 psf->error = SFE_MALLOC_FAILED ;
1590 /* We do not parse the coding history */
1591 psf_binheader_readf (psf, "b", b->coding_history, b->coding_history_size) ;
1592 b->coding_history [sizeof (b->coding_history) - 1] = 0 ;
1596 } /* wav_read_bext_chunk */
1599 wav_write_bext_chunk (SF_PRIVATE *psf)
1600 { SF_BROADCAST_INFO *b ;
1602 if ((b = psf->broadcast_info) == NULL)
1605 psf_binheader_writef (psf, "m4", bext_MARKER, WAV_BEXT_CHUNK_SIZE + b->coding_history_size) ;
1608 ** Note that it is very important the the field widths of the SF_BROADCAST_INFO
1609 ** struct match those for the bext chunk fields.
1612 psf_binheader_writef (psf, "b", b->description, sizeof (b->description)) ;
1613 psf_binheader_writef (psf, "b", b->originator, sizeof (b->originator)) ;
1614 psf_binheader_writef (psf, "b", b->originator_reference, sizeof (b->originator_reference)) ;
1615 psf_binheader_writef (psf, "b", b->origination_date, sizeof (b->origination_date)) ;
1616 psf_binheader_writef (psf, "b", b->origination_time, sizeof (b->origination_time)) ;
1617 psf_binheader_writef (psf, "442", b->time_reference_low, b->time_reference_high, b->version) ;
1618 psf_binheader_writef (psf, "b", b->umid, sizeof (b->umid)) ;
1619 psf_binheader_writef (psf, "z", make_size_t (190)) ;
1621 if (b->coding_history_size > 0)
1622 psf_binheader_writef (psf, "b", b->coding_history, b->coding_history_size) ;
1625 } /* wav_write_bext_chunk */
1628 ** Do not edit or modify anything in this comment block.
1629 ** The arch-tag line is a file identity tag for the GNU Arch
1630 ** revision control system.
1632 ** arch-tag: 9c551689-a1d8-4905-9f56-26a204374f18