2 ** Copyright (C) 1999-2019 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.
34 /*------------------------------------------------------------------------------
35 * Macros to handle big/little endian issues.
38 #define RIFF_MARKER (MAKE_MARKER ('R', 'I', 'F', 'F'))
39 #define RIFX_MARKER (MAKE_MARKER ('R', 'I', 'F', 'X'))
40 #define WAVE_MARKER (MAKE_MARKER ('W', 'A', 'V', 'E'))
41 #define fmt_MARKER (MAKE_MARKER ('f', 'm', 't', ' '))
42 #define fact_MARKER (MAKE_MARKER ('f', 'a', 'c', 't'))
44 #define cue_MARKER (MAKE_MARKER ('c', 'u', 'e', ' '))
45 #define slnt_MARKER (MAKE_MARKER ('s', 'l', 'n', 't'))
46 #define wavl_MARKER (MAKE_MARKER ('w', 'a', 'v', 'l'))
47 #define plst_MARKER (MAKE_MARKER ('p', 'l', 's', 't'))
48 #define smpl_MARKER (MAKE_MARKER ('s', 'm', 'p', 'l'))
49 #define iXML_MARKER (MAKE_MARKER ('i', 'X', 'M', 'L'))
50 #define levl_MARKER (MAKE_MARKER ('l', 'e', 'v', 'l'))
51 #define MEXT_MARKER (MAKE_MARKER ('M', 'E', 'X', 'T'))
52 #define acid_MARKER (MAKE_MARKER ('a', 'c', 'i', 'd'))
53 #define strc_MARKER (MAKE_MARKER ('s', 't', 'r', 'c'))
54 #define afsp_MARKER (MAKE_MARKER ('a', 'f', 's', 'p'))
55 #define clm_MARKER (MAKE_MARKER ('c', 'l', 'm', ' '))
56 #define elmo_MARKER (MAKE_MARKER ('e', 'l', 'm', 'o'))
57 #define FLLR_MARKER (MAKE_MARKER ('F', 'L', 'L', 'R'))
59 #define minf_MARKER (MAKE_MARKER ('m', 'i', 'n', 'f'))
60 #define elm1_MARKER (MAKE_MARKER ('e', 'l', 'm', '1'))
61 #define regn_MARKER (MAKE_MARKER ('r', 'e', 'g', 'n'))
62 #define ovwf_MARKER (MAKE_MARKER ('o', 'v', 'w', 'f'))
63 #define umid_MARKER (MAKE_MARKER ('u', 'm', 'i', 'd'))
64 #define SyLp_MARKER (MAKE_MARKER ('S', 'y', 'L', 'p'))
65 #define Cr8r_MARKER (MAKE_MARKER ('C', 'r', '8', 'r'))
66 #define JUNK_MARKER (MAKE_MARKER ('J', 'U', 'N', 'K'))
67 #define PMX_MARKER (MAKE_MARKER ('_', 'P', 'M', 'X'))
68 #define inst_MARKER (MAKE_MARKER ('i', 'n', 's', 't'))
69 #define AFAn_MARKER (MAKE_MARKER ('A', 'F', 'A', 'n'))
72 /* Weird WAVPACK marker which can show up at the start of the DATA section. */
73 #define wvpk_MARKER (MAKE_MARKER ('w', 'v', 'p', 'k'))
74 #define OggS_MARKER (MAKE_MARKER ('O', 'g', 'g', 'S'))
76 /* ID3v1 trailer which can show up at the end and erronerously look like a chunk. */
77 #define TAG__MARKER (MAKE_MARKER ('T', 'A', 'G', 0))
78 #define TAG__MARKER_MASK (MAKE_MARKER (0xff, 0xff, 0xff, 0))
80 #define WAVLIKE_PEAK_CHUNK_SIZE(ch) (2 * sizeof (int) + ch * (sizeof (float) + sizeof (int)))
94 /* known WAVEFORMATEXTENSIBLE GUIDS */
95 static const EXT_SUBFORMAT MSGUID_SUBTYPE_PCM =
96 { 0x00000001, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }
100 static const EXT_SUBFORMAT MSGUID_SUBTYPE_MS_ADPCM =
101 { 0x00000002, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }
105 static const EXT_SUBFORMAT MSGUID_SUBTYPE_IEEE_FLOAT =
106 { 0x00000003, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }
109 static const EXT_SUBFORMAT MSGUID_SUBTYPE_ALAW =
110 { 0x00000006, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }
113 static const EXT_SUBFORMAT MSGUID_SUBTYPE_MULAW =
114 { 0x00000007, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }
118 ** the next two are from
119 ** http://dream.cs.bath.ac.uk/researchdev/wave-ex/bformat.html
121 static const EXT_SUBFORMAT MSGUID_SUBTYPE_AMBISONIC_B_FORMAT_PCM =
122 { 0x00000001, 0x0721, 0x11d3, { 0x86, 0x44, 0xC8, 0xC1, 0xCA, 0x00, 0x00, 0x00 }
125 static const EXT_SUBFORMAT MSGUID_SUBTYPE_AMBISONIC_B_FORMAT_IEEE_FLOAT =
126 { 0x00000003, 0x0721, 0x11d3, { 0x86, 0x44, 0xC8, 0xC1, 0xCA, 0x00, 0x00, 0x00 }
131 /* maybe interesting one day to read the following through sf_read_raw */
132 /* http://www.bath.ac.uk/~masrwd/pvocex/pvocex.html */
133 static const EXT_SUBFORMAT MSGUID_SUBTYPE_PVOCEX =
134 { 0x8312B9C2, 0x2E6E, 0x11d4, { 0xA8, 0x24, 0xDE, 0x5B, 0x96, 0xC3, 0xAB, 0x21 }
138 /*------------------------------------------------------------------------------
139 ** Private static functions.
142 static int wav_read_header (SF_PRIVATE *psf, int *blockalign, int *framesperblock) ;
143 static int wav_write_header (SF_PRIVATE *psf, int calc_length) ;
145 static int wav_write_tailer (SF_PRIVATE *psf) ;
146 static int wav_command (SF_PRIVATE *psf, int command, void *data, int datasize) ;
147 static int wav_close (SF_PRIVATE *psf) ;
149 static int wav_read_smpl_chunk (SF_PRIVATE *psf, uint32_t chunklen) ;
150 static int wav_read_acid_chunk (SF_PRIVATE *psf, uint32_t chunklen) ;
152 static int wav_set_chunk (SF_PRIVATE *psf, const SF_CHUNK_INFO * chunk_info) ;
153 static SF_CHUNK_ITERATOR * wav_next_chunk_iterator (SF_PRIVATE *psf, SF_CHUNK_ITERATOR * iterator) ;
154 static int wav_get_chunk_size (SF_PRIVATE *psf, const SF_CHUNK_ITERATOR * iterator, SF_CHUNK_INFO * chunk_info) ;
155 static int wav_get_chunk_data (SF_PRIVATE *psf, const SF_CHUNK_ITERATOR * iterator, SF_CHUNK_INFO * chunk_info) ;
157 /*------------------------------------------------------------------------------
162 wav_open (SF_PRIVATE *psf)
163 { WAVLIKE_PRIVATE * wpriv ;
164 int format, subformat, error, blockalign = 0, framesperblock = 0 ;
166 if ((wpriv = calloc (1, sizeof (WAVLIKE_PRIVATE))) == NULL)
167 return SFE_MALLOC_FAILED ;
168 psf->container_data = wpriv ;
170 wpriv->wavex_ambisonic = SF_AMBISONIC_NONE ;
171 psf->strings.flags = SF_STR_ALLOW_START | SF_STR_ALLOW_END ;
173 if (psf->file.mode == SFM_READ || (psf->file.mode == SFM_RDWR && psf->filelength > 0))
174 { if ((error = wav_read_header (psf, &blockalign, &framesperblock)))
177 psf->next_chunk_iterator = wav_next_chunk_iterator ;
178 psf->get_chunk_size = wav_get_chunk_size ;
179 psf->get_chunk_data = wav_get_chunk_data ;
182 subformat = SF_CODEC (psf->sf.format) ;
184 if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
186 return SFE_NO_PIPE_WRITE ;
188 wpriv->wavex_ambisonic = SF_AMBISONIC_NONE ;
190 format = SF_CONTAINER (psf->sf.format) ;
191 if (format != SF_FORMAT_WAV && format != SF_FORMAT_WAVEX)
192 return SFE_BAD_OPEN_FORMAT ;
194 psf->blockwidth = psf->bytewidth * psf->sf.channels ;
196 /* RIFF WAVs are little-endian, RIFX WAVs are big-endian, default to little */
197 psf->endian = SF_ENDIAN (psf->sf.format) ;
198 if (CPU_IS_BIG_ENDIAN && psf->endian == SF_ENDIAN_CPU)
199 psf->endian = SF_ENDIAN_BIG ;
200 else if (psf->endian != SF_ENDIAN_BIG)
201 psf->endian = SF_ENDIAN_LITTLE ;
203 if (psf->file.mode != SFM_RDWR || psf->filelength < 44)
204 { psf->filelength = 0 ;
205 psf->datalength = 0 ;
206 psf->dataoffset = 0 ;
210 #if (ENABLE_EXPERIMENTAL_CODE == 0)
211 /* For now, don't support writing MPEGLAYER3 WAVs, as we can't guarentee that
212 ** such a file written by libsndfile would have the same length when opened again.
214 if (subformat == SF_FORMAT_MPEG_LAYER_III)
215 return SFE_UNSUPPORTED_ENCODING ;
218 if (subformat == SF_FORMAT_IMA_ADPCM || subformat == SF_FORMAT_MS_ADPCM)
219 { blockalign = wavlike_srate2blocksize (psf->sf.samplerate * psf->sf.channels) ;
220 framesperblock = -1 ; /* Corrected later. */
223 /* By default, add the peak chunk to floating point files. Default behaviour
224 ** can be switched off using sf_command (SFC_SET_PEAK_CHUNK, SF_FALSE).
226 if (psf->file.mode == SFM_WRITE && (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE))
227 { if ((psf->peak_info = peak_info_calloc (psf->sf.channels)) == NULL)
228 return SFE_MALLOC_FAILED ;
229 psf->peak_info->peak_loc = SF_PEAK_START ;
232 psf->write_header = wav_write_header ;
233 psf->set_chunk = wav_set_chunk ;
236 psf->container_close = wav_close ;
237 psf->command = wav_command ;
240 { case SF_FORMAT_PCM_U8 :
241 case SF_FORMAT_PCM_16 :
242 case SF_FORMAT_PCM_24 :
243 case SF_FORMAT_PCM_32 :
244 error = pcm_init (psf) ;
247 case SF_FORMAT_ULAW :
248 error = ulaw_init (psf) ;
251 case SF_FORMAT_ALAW :
252 error = alaw_init (psf) ;
255 /* Lite remove start */
256 case SF_FORMAT_FLOAT :
257 error = float32_init (psf) ;
260 case SF_FORMAT_DOUBLE :
261 error = double64_init (psf) ;
264 case SF_FORMAT_IMA_ADPCM :
265 error = wavlike_ima_init (psf, blockalign, framesperblock) ;
268 case SF_FORMAT_MS_ADPCM :
269 error = wavlike_msadpcm_init (psf, blockalign, framesperblock) ;
272 case SF_FORMAT_NMS_ADPCM_16 :
273 case SF_FORMAT_NMS_ADPCM_24 :
274 case SF_FORMAT_NMS_ADPCM_32 :
275 error = nms_adpcm_init (psf) ;
278 /* Lite remove end */
280 case SF_FORMAT_MPEG_LAYER_III :
281 error = mpeg_init (psf, SF_BITRATE_MODE_CONSTANT, SF_FALSE) ;
284 default : return SFE_UNIMPLEMENTED ;
287 if (psf->file.mode == SFM_WRITE || (psf->file.mode == SFM_RDWR && psf->filelength == 0))
288 return psf->write_header (psf, SF_FALSE) ;
293 /*=========================================================================
294 ** Private functions.
298 wav_read_header (SF_PRIVATE *psf, int *blockalign, int *framesperblock)
299 { WAVLIKE_PRIVATE *wpriv ;
301 FACT_CHUNK fact_chunk ;
302 uint32_t marker, chunk_size = 0, RIFFsize = 0, done = 0 ;
303 int parsestage = 0, error, format = 0 ;
305 if (psf->is_pipe == 0 && psf->filelength > 0xFFFFFFFFLL)
306 psf_log_printf (psf, "Warning : filelength > 0xffffffff. This is bad!!!!\n") ;
308 if ((wpriv = psf->container_data) == NULL)
309 return SFE_INTERNAL ;
310 wav_fmt = &wpriv->wav_fmt ;
312 /* Set position to start of file to begin reading header. */
313 psf_binheader_readf (psf, "pmj", 0, &marker, -4) ;
314 psf->header.indx = 0 ;
316 /* RIFX signifies big-endian format for all header and data to prevent
317 ** lots of code copying here, we'll set the psf->rwf_endian flag once here,
318 ** and never specify endian-ness for all other header ops/
320 psf->rwf_endian = (marker == RIFF_MARKER) ? SF_ENDIAN_LITTLE : SF_ENDIAN_BIG ;
323 { size_t jump = chunk_size & 1 ;
325 marker = chunk_size = 0 ;
326 psf_binheader_readf (psf, "jm4", jump, &marker, &chunk_size) ;
328 { sf_count_t pos = psf_ftell (psf) ;
329 psf_log_printf (psf, "Have 0 marker at position %D (0x%x).\n", pos, pos) ;
333 psf_store_read_chunk_u32 (&psf->rchunks, marker, psf_ftell (psf), chunk_size) ;
339 return SFE_WAV_NO_RIFF ;
341 parsestage |= HAVE_RIFF ;
343 RIFFsize = chunk_size ;
345 if (psf->fileoffset > 0 && psf->filelength > RIFFsize + 8)
346 { /* Set file length. */
347 psf->filelength = RIFFsize + 8 ;
348 if (marker == RIFF_MARKER)
349 psf_log_printf (psf, "RIFF : %u\n", RIFFsize) ;
351 psf_log_printf (psf, "RIFX : %u\n", RIFFsize) ;
353 else if (psf->filelength < RIFFsize + 2 * SIGNED_SIZEOF (marker))
354 { if (marker == RIFF_MARKER)
355 psf_log_printf (psf, "RIFF : %u (should be %D)\n", RIFFsize, psf->filelength - 2 * SIGNED_SIZEOF (marker)) ;
357 psf_log_printf (psf, "RIFX : %u (should be %D)\n", RIFFsize, psf->filelength - 2 * SIGNED_SIZEOF (marker)) ;
359 RIFFsize = psf->filelength - 2 * SIGNED_SIZEOF (RIFFsize) ;
362 { if (marker == RIFF_MARKER)
363 psf_log_printf (psf, "RIFF : %u\n", RIFFsize) ;
365 psf_log_printf (psf, "RIFX : %u\n", RIFFsize) ;
368 psf_binheader_readf (psf, "m", &marker) ;
369 if (marker != WAVE_MARKER)
370 return SFE_WAV_NO_WAVE ;
371 parsestage |= HAVE_WAVE ;
372 psf_log_printf (psf, "WAVE\n") ;
377 if ((parsestage & (HAVE_RIFF | HAVE_WAVE)) != (HAVE_RIFF | HAVE_WAVE))
378 return SFE_WAV_NO_FMT ;
380 /* If this file has a SECOND fmt chunk, I don't want to know about it. */
381 if (parsestage & HAVE_fmt)
384 parsestage |= HAVE_fmt ;
386 psf_log_printf (psf, "fmt : %d\n", chunk_size) ;
388 if ((error = wavlike_read_fmt_chunk (psf, chunk_size)))
391 format = wav_fmt->format ;
395 if ((parsestage & (HAVE_RIFF | HAVE_WAVE | HAVE_fmt)) != (HAVE_RIFF | HAVE_WAVE | HAVE_fmt))
396 return SFE_WAV_NO_DATA ;
398 if (psf->file.mode == SFM_RDWR && (parsestage & HAVE_other) != 0)
399 return SFE_RDWR_BAD_HEADER ;
401 parsestage |= HAVE_data ;
403 psf->datalength = chunk_size ;
404 if (psf->datalength & 1)
405 psf_log_printf (psf, "*** 'data' chunk should be an even number of bytes in length.\n") ;
407 psf->dataoffset = psf_ftell (psf) ;
409 if (psf->dataoffset > 0)
410 { if (chunk_size == 0 && RIFFsize == 8 && psf->filelength > 44)
411 { psf_log_printf (psf, "*** Looks like a WAV file which wasn't closed properly. Fixing it.\n") ;
412 psf->datalength = psf->filelength - psf->dataoffset ;
415 if (psf->datalength > psf->filelength - psf->dataoffset)
416 { psf_log_printf (psf, "data : %D (should be %D)\n", psf->datalength, psf->filelength - psf->dataoffset) ;
417 psf->datalength = psf->filelength - psf->dataoffset ;
420 psf_log_printf (psf, "data : %D\n", psf->datalength) ;
422 /* Only set dataend if there really is data at the end. */
423 if (psf->datalength + psf->dataoffset < psf->filelength)
424 psf->dataend = psf->datalength + psf->dataoffset ;
426 psf->datalength += chunk_size & 1 ;
430 if (! psf->sf.seekable || psf->dataoffset < 0)
433 /* Seek past data and continue reading header. */
434 psf_fseek (psf, psf->datalength, SEEK_CUR) ;
436 if (psf_ftell (psf) != psf->datalength + psf->dataoffset)
437 psf_log_printf (psf, "*** psf_fseek past end error ***\n") ;
441 if ((parsestage & (HAVE_RIFF | HAVE_WAVE)) != (HAVE_RIFF | HAVE_WAVE))
442 return SFE_WAV_BAD_FACT ;
444 parsestage |= HAVE_fact ;
446 if ((parsestage & HAVE_fmt) != HAVE_fmt)
447 psf_log_printf (psf, "*** Should have 'fmt ' chunk before 'fact'\n") ;
449 psf_binheader_readf (psf, "4", & (fact_chunk.frames)) ;
451 if (chunk_size > SIGNED_SIZEOF (fact_chunk))
452 psf_binheader_readf (psf, "j", (int) (chunk_size - SIGNED_SIZEOF (fact_chunk))) ;
455 psf_log_printf (psf, "%M : %u\n", marker, chunk_size) ;
457 psf_log_printf (psf, "%M : %u (should not be zero)\n", marker, chunk_size) ;
459 psf_log_printf (psf, " frames : %d\n", fact_chunk.frames) ;
463 if ((parsestage & (HAVE_RIFF | HAVE_WAVE | HAVE_fmt)) != (HAVE_RIFF | HAVE_WAVE | HAVE_fmt))
464 return SFE_WAV_PEAK_B4_FMT ;
466 parsestage |= HAVE_PEAK ;
468 psf_log_printf (psf, "%M : %u\n", marker, chunk_size) ;
469 if ((error = wavlike_read_peak_chunk (psf, chunk_size)) != 0)
471 psf->peak_info->peak_loc = ((parsestage & HAVE_data) == 0) ? SF_PEAK_START : SF_PEAK_END ;
475 parsestage |= HAVE_other ;
477 { uint32_t thisread, bytesread, cue_count, position, offset ;
478 int id, chunk_id, chunk_start, block_start, cue_index ;
480 bytesread = psf_binheader_readf (psf, "4", &cue_count) ;
481 psf_log_printf (psf, "%M : %u\n", marker, chunk_size) ;
483 if (cue_count > 2500) /* 2500 is close to the largest number of cues possible because of block sizes */
484 { psf_log_printf (psf, " Count : %u (skipping)\n", cue_count) ;
485 psf_binheader_readf (psf, "j", chunk_size - bytesread) ;
489 psf_log_printf (psf, " Count : %d\n", cue_count) ;
496 if ((psf->cues = psf_cues_alloc (cue_count)) == NULL)
497 return SFE_MALLOC_FAILED ;
503 if ((thisread = psf_binheader_readf (psf, "e44m444", &id, &position, &chunk_id, &chunk_start, &block_start, &offset)) == 0)
505 bytesread += thisread ;
507 if (cue_index < 10) /* avoid swamping log buffer with cues */
508 psf_log_printf (psf, " Cue ID : %2d"
509 " Pos : %5u Chunk : %M"
510 " Chk Start : %d Blk Start : %d"
512 id, position, chunk_id, chunk_start, block_start, offset) ;
513 else if (cue_index == 10)
514 psf_log_printf (psf, " (Skipping)\n") ;
516 psf->cues->cue_points [cue_index].indx = id ;
517 psf->cues->cue_points [cue_index].position = position ;
518 psf->cues->cue_points [cue_index].fcc_chunk = chunk_id ;
519 psf->cues->cue_points [cue_index].chunk_start = chunk_start ;
520 psf->cues->cue_points [cue_index].block_start = block_start ;
521 psf->cues->cue_points [cue_index].sample_offset = offset ;
522 psf->cues->cue_points [cue_index].name [0] = '\0' ;
527 if (bytesread != chunk_size)
528 { psf_log_printf (psf, "**** Chunk size weirdness (%d != %d)\n", chunk_size, bytesread) ;
529 psf_binheader_readf (psf, "j", chunk_size - bytesread) ;
535 parsestage |= HAVE_other ;
537 psf_log_printf (psf, "smpl : %u\n", chunk_size) ;
539 if ((error = wav_read_smpl_chunk (psf, chunk_size)))
544 parsestage |= HAVE_other ;
546 psf_log_printf (psf, "acid : %u\n", chunk_size) ;
548 if ((error = wav_read_acid_chunk (psf, chunk_size)))
554 parsestage |= HAVE_other ;
556 if ((error = wavlike_subchunk_parse (psf, marker, chunk_size)) != 0)
562 The 'bext' chunk can actually be updated, so don't need to set this.
563 parsestage |= HAVE_other ;
565 if ((error = wavlike_read_bext_chunk (psf, chunk_size)))
571 We can eat into a 'PAD ' chunk if we need to.
572 parsestage |= HAVE_other ;
574 psf_log_printf (psf, "%M : %u\n", marker, chunk_size) ;
575 psf_binheader_readf (psf, "j", chunk_size) ;
579 if ((error = wavlike_read_cart_chunk (psf, chunk_size)))
583 case iXML_MARKER : /* See http://en.wikipedia.org/wiki/IXML */
584 case strc_MARKER : /* Multiple of 32 bytes. */
604 psf_log_printf (psf, "%M : %u\n", marker, chunk_size) ;
605 psf_binheader_readf (psf, "j", chunk_size) ;
609 if (chunk_size >= 0xffff0000)
611 psf_log_printf (psf, "*** Unknown chunk marker (%X) at position %D with length %u. Exiting parser.\n", marker, psf_ftell (psf) - 8, chunk_size) ;
615 if ((marker & TAG__MARKER_MASK) == TAG__MARKER &&
616 psf_ftell (psf) - 8 + 128 == psf->filelength)
617 { psf_log_printf (psf, "*** Hit ID3v1 trailer. Exiting parser.\n") ;
620 parsestage |= HAVE_other ;
624 if (psf_isprint ((marker >> 24) & 0xFF) && psf_isprint ((marker >> 16) & 0xFF)
625 && psf_isprint ((marker >> 8) & 0xFF) && psf_isprint (marker & 0xFF))
626 { psf_log_printf (psf, "*** %M : %u (unknown marker)\n", marker, chunk_size) ;
627 psf_binheader_readf (psf, "j", chunk_size) ;
630 if (psf_ftell (psf) & 0x03)
631 { psf_log_printf (psf, " Unknown chunk marker at position %D. Resynching.\n", psf_ftell (psf) - 8) ;
632 psf_binheader_readf (psf, "j", -3) ;
633 /* File is too messed up so we prevent editing in RDWR mode here. */
634 parsestage |= HAVE_other ;
637 psf_log_printf (psf, "*** Unknown chunk marker (%X) at position %D. Exiting parser.\n", marker, psf_ftell (psf) - 8) ;
640 } ; /* switch (marker) */
642 if (chunk_size >= psf->filelength)
643 { psf_log_printf (psf, "*** Chunk size %u > file length %D. Exiting parser.\n", chunk_size, psf->filelength) ;
647 if (! psf->sf.seekable && (parsestage & HAVE_data))
650 if (psf_ftell (psf) >= psf->filelength - SIGNED_SIZEOF (chunk_size))
651 { psf_log_printf (psf, "End\n") ;
656 if (psf->dataoffset <= 0)
657 return SFE_WAV_NO_DATA ;
659 if (psf->sf.channels < 1)
660 return SFE_CHANNEL_COUNT_ZERO ;
662 if (psf->sf.channels > SF_MAX_CHANNELS)
663 return SFE_CHANNEL_COUNT ;
665 if (format != WAVE_FORMAT_PCM && (parsestage & HAVE_fact) == 0)
666 psf_log_printf (psf, "**** All non-PCM format files should have a 'fact' chunk.\n") ;
668 /* WAVs can be little or big endian */
669 psf->endian = psf->rwf_endian ;
671 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
673 if (psf->is_pipe == 0)
675 ** Check for 'wvpk' at the start of the DATA section. Not able to
678 psf_binheader_readf (psf, "4", &marker) ;
679 if (marker == wvpk_MARKER || marker == OggS_MARKER)
680 return SFE_WAV_WVPK_DATA ;
683 /* Seek to start of DATA section. */
684 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
687 { if (psf->filelength - psf->dataoffset < psf->datalength)
688 psf->sf.frames = (psf->filelength - psf->dataoffset) / psf->blockwidth ;
690 psf->sf.frames = psf->datalength / psf->blockwidth ;
694 { case WAVE_FORMAT_EXTENSIBLE :
695 if (psf->sf.format == (SF_FORMAT_WAVEX | SF_FORMAT_MS_ADPCM))
696 { *blockalign = wav_fmt->msadpcm.blockalign ;
697 *framesperblock = wav_fmt->msadpcm.samplesperblock ;
701 case WAVE_FORMAT_NMS_VBXADPCM :
702 switch (wav_fmt->min.bitwidth)
704 psf->sf.format = SF_FORMAT_WAV | SF_FORMAT_NMS_ADPCM_16 ;
707 psf->sf.format = SF_FORMAT_WAV | SF_FORMAT_NMS_ADPCM_24 ;
710 psf->sf.format = SF_FORMAT_WAV | SF_FORMAT_NMS_ADPCM_32 ;
714 return SFE_UNIMPLEMENTED ;
718 case WAVE_FORMAT_PCM :
719 psf->sf.format = SF_FORMAT_WAV | u_bitwidth_to_subformat (psf->bytewidth * 8) ;
722 case WAVE_FORMAT_MULAW :
723 case IBM_FORMAT_MULAW :
724 psf->sf.format = (SF_FORMAT_WAV | SF_FORMAT_ULAW) ;
727 case WAVE_FORMAT_ALAW :
728 case IBM_FORMAT_ALAW :
729 psf->sf.format = (SF_FORMAT_WAV | SF_FORMAT_ALAW) ;
732 case WAVE_FORMAT_MS_ADPCM :
733 psf->sf.format = (SF_FORMAT_WAV | SF_FORMAT_MS_ADPCM) ;
734 *blockalign = wav_fmt->msadpcm.blockalign ;
735 *framesperblock = wav_fmt->msadpcm.samplesperblock ;
738 case WAVE_FORMAT_IMA_ADPCM :
739 psf->sf.format = (SF_FORMAT_WAV | SF_FORMAT_IMA_ADPCM) ;
740 *blockalign = wav_fmt->ima.blockalign ;
741 *framesperblock = wav_fmt->ima.samplesperblock ;
744 case WAVE_FORMAT_GSM610 :
745 psf->sf.format = (SF_FORMAT_WAV | SF_FORMAT_GSM610) ;
748 case WAVE_FORMAT_IEEE_FLOAT :
749 psf->sf.format = SF_FORMAT_WAV ;
750 psf->sf.format |= (psf->bytewidth == 8) ? SF_FORMAT_DOUBLE : SF_FORMAT_FLOAT ;
753 case WAVE_FORMAT_G721_ADPCM :
754 psf->sf.format = SF_FORMAT_WAV | SF_FORMAT_G721_32 ;
757 case WAVE_FORMAT_MPEGLAYER3 :
758 psf->sf.format = SF_FORMAT_WAV | SF_FORMAT_MPEG_LAYER_III ;
759 if (parsestage & HAVE_fact)
760 psf->sf.frames = fact_chunk.frames ;
763 default : return SFE_UNIMPLEMENTED ;
766 if (wpriv->fmt_is_broken)
767 wavlike_analyze (psf) ;
769 /* Only set the format endian-ness if its non-standard big-endian. */
770 if (psf->endian == SF_ENDIAN_BIG)
771 psf->sf.format |= SF_ENDIAN_BIG ;
774 } /* wav_read_header */
777 wav_write_fmt_chunk (SF_PRIVATE *psf)
778 { int subformat, fmt_size, add_fact_chunk = 0 ;
780 subformat = SF_CODEC (psf->sf.format) ;
783 { case SF_FORMAT_PCM_U8 :
784 case SF_FORMAT_PCM_16 :
785 case SF_FORMAT_PCM_24 :
786 case SF_FORMAT_PCM_32 :
787 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 ;
789 /* fmt : format, channels, samplerate */
790 psf_binheader_writef (psf, "4224", BHW4 (fmt_size), BHW2 (WAVE_FORMAT_PCM), BHW2 (psf->sf.channels), BHW4 (psf->sf.samplerate)) ;
791 /* fmt : bytespersec */
792 psf_binheader_writef (psf, "4", BHW4 (psf->sf.samplerate * psf->bytewidth * psf->sf.channels)) ;
793 /* fmt : blockalign, bitwidth */
794 psf_binheader_writef (psf, "22", BHW2 (psf->bytewidth * psf->sf.channels), BHW2 (psf->bytewidth * 8)) ;
797 case SF_FORMAT_FLOAT :
798 case SF_FORMAT_DOUBLE :
799 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 ;
801 /* fmt : format, channels, samplerate */
802 psf_binheader_writef (psf, "4224", BHW4 (fmt_size), BHW2 (WAVE_FORMAT_IEEE_FLOAT), BHW2 (psf->sf.channels), BHW4 (psf->sf.samplerate)) ;
803 /* fmt : bytespersec */
804 psf_binheader_writef (psf, "4", BHW4 (psf->sf.samplerate * psf->bytewidth * psf->sf.channels)) ;
805 /* fmt : blockalign, bitwidth */
806 psf_binheader_writef (psf, "22", BHW2 (psf->bytewidth * psf->sf.channels), BHW2 (psf->bytewidth * 8)) ;
808 add_fact_chunk = SF_TRUE ;
811 case SF_FORMAT_ULAW :
812 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 ;
814 /* fmt : format, channels, samplerate */
815 psf_binheader_writef (psf, "4224", BHW4 (fmt_size), BHW2 (WAVE_FORMAT_MULAW), BHW2 (psf->sf.channels), BHW4 (psf->sf.samplerate)) ;
816 /* fmt : bytespersec */
817 psf_binheader_writef (psf, "4", BHW4 (psf->sf.samplerate * psf->bytewidth * psf->sf.channels)) ;
818 /* fmt : blockalign, bitwidth, extrabytes */
819 psf_binheader_writef (psf, "222", BHW2 (psf->bytewidth * psf->sf.channels), BHW2 (8), BHW2 (0)) ;
821 add_fact_chunk = SF_TRUE ;
824 case SF_FORMAT_ALAW :
825 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 ;
827 /* fmt : format, channels, samplerate */
828 psf_binheader_writef (psf, "4224", BHW4 (fmt_size), BHW2 (WAVE_FORMAT_ALAW), BHW2 (psf->sf.channels), BHW4 (psf->sf.samplerate)) ;
829 /* fmt : bytespersec */
830 psf_binheader_writef (psf, "4", BHW4 (psf->sf.samplerate * psf->bytewidth * psf->sf.channels)) ;
831 /* fmt : blockalign, bitwidth, extrabytes */
832 psf_binheader_writef (psf, "222", BHW2 (psf->bytewidth * psf->sf.channels), BHW2 (8), BHW2 (0)) ;
834 add_fact_chunk = SF_TRUE ;
837 /* Lite remove start */
838 case SF_FORMAT_IMA_ADPCM :
839 { int blockalign, framesperblock, bytespersec ;
841 blockalign = wavlike_srate2blocksize (psf->sf.samplerate * psf->sf.channels) ;
842 framesperblock = 2 * (blockalign - 4 * psf->sf.channels) / psf->sf.channels + 1 ;
843 bytespersec = (psf->sf.samplerate * blockalign) / framesperblock ;
846 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 + 2 ;
848 /* fmt : size, WAV format type, channels, samplerate, bytespersec */
849 psf_binheader_writef (psf, "42244", BHW4 (fmt_size), BHW2 (WAVE_FORMAT_IMA_ADPCM),
850 BHW2 (psf->sf.channels), BHW4 (psf->sf.samplerate), BHW4 (bytespersec)) ;
852 /* fmt : blockalign, bitwidth, extrabytes, framesperblock. */
853 psf_binheader_writef (psf, "2222", BHW2 (blockalign), BHW2 (4), BHW2 (2), BHW2 (framesperblock)) ;
856 add_fact_chunk = SF_TRUE ;
859 case SF_FORMAT_MS_ADPCM :
860 { int blockalign, framesperblock, bytespersec, extrabytes ;
862 blockalign = wavlike_srate2blocksize (psf->sf.samplerate * psf->sf.channels) ;
863 framesperblock = 2 + 2 * (blockalign - 7 * psf->sf.channels) / psf->sf.channels ;
864 bytespersec = (psf->sf.samplerate * blockalign) / framesperblock ;
867 extrabytes = 2 + 2 + WAVLIKE_MSADPCM_ADAPT_COEFF_COUNT * (2 + 2) ;
868 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 + extrabytes ;
870 /* fmt : size, WAV format type, channels. */
871 psf_binheader_writef (psf, "422", BHW4 (fmt_size), BHW2 (WAVE_FORMAT_MS_ADPCM), BHW2 (psf->sf.channels)) ;
873 /* fmt : samplerate, bytespersec. */
874 psf_binheader_writef (psf, "44", BHW4 (psf->sf.samplerate), BHW4 (bytespersec)) ;
876 /* fmt : blockalign, bitwidth, extrabytes, framesperblock. */
877 psf_binheader_writef (psf, "22222", BHW2 (blockalign), BHW2 (4), BHW2 (extrabytes), BHW2 (framesperblock), BHW2 (7)) ;
879 wavlike_msadpcm_write_adapt_coeffs (psf) ;
882 add_fact_chunk = SF_TRUE ;
886 case SF_FORMAT_G721_32 :
888 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 + 2 ;
890 /* fmt : size, WAV format type, channels, samplerate, bytespersec */
891 psf_binheader_writef (psf, "42244", BHW4 (fmt_size), BHW2 (WAVE_FORMAT_G721_ADPCM),
892 BHW2 (psf->sf.channels), BHW4 (psf->sf.samplerate), BHW4 (psf->sf.samplerate * psf->sf.channels / 2)) ;
894 /* fmt : blockalign, bitwidth, extrabytes, auxblocksize. */
895 psf_binheader_writef (psf, "2222", BHW2 (64), BHW2 (4), BHW2 (2), BHW2 (0)) ;
897 add_fact_chunk = SF_TRUE ;
900 case SF_FORMAT_NMS_ADPCM_16 :
901 case SF_FORMAT_NMS_ADPCM_24 :
902 case SF_FORMAT_NMS_ADPCM_32 :
903 { int bytespersec, blockalign, bitwidth ;
905 bitwidth = subformat == SF_FORMAT_NMS_ADPCM_16 ? 2 : subformat == SF_FORMAT_NMS_ADPCM_24 ? 3 : 4 ;
906 blockalign = 20 * bitwidth + 2 ;
907 bytespersec = psf->sf.samplerate * blockalign / 160 ;
910 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 ;
912 /* fmt : format, channels, samplerate */
913 psf_binheader_writef (psf, "4224", BHW4 (fmt_size), BHW2 (WAVE_FORMAT_NMS_VBXADPCM),
914 BHW2 (psf->sf.channels), BHW4 (psf->sf.samplerate)) ;
915 /* fmt : bytespersec, blockalign, bitwidth */
916 psf_binheader_writef (psf, "422", BHW4 (bytespersec), BHW2 (blockalign), BHW2 (bitwidth)) ;
918 add_fact_chunk = SF_TRUE ;
922 /* Lite remove end */
924 case SF_FORMAT_GSM610 :
925 { int blockalign, framesperblock, bytespersec ;
927 blockalign = WAVLIKE_GSM610_BLOCKSIZE ;
928 framesperblock = WAVLIKE_GSM610_SAMPLES ;
929 bytespersec = (psf->sf.samplerate * blockalign) / framesperblock ;
932 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 + 2 ;
934 /* fmt : size, WAV format type, channels. */
935 psf_binheader_writef (psf, "422", BHW4 (fmt_size), BHW2 (WAVE_FORMAT_GSM610), BHW2 (psf->sf.channels)) ;
937 /* fmt : samplerate, bytespersec. */
938 psf_binheader_writef (psf, "44", BHW4 (psf->sf.samplerate), BHW4 (bytespersec)) ;
940 /* fmt : blockalign, bitwidth, extrabytes, framesperblock. */
941 psf_binheader_writef (psf, "2222", BHW2 (blockalign), BHW2 (0), BHW2 (2), BHW2 (framesperblock)) ;
944 add_fact_chunk = SF_TRUE ;
947 #if (ENABLE_EXPERIMENTAL_CODE == 0)
948 case SF_FORMAT_MPEG_LAYER_III :
949 { int bytespersec, blockalign, flags, blocksize, samplesperblock, codecdelay ;
951 /* Intended to be set as the average sample rate.
952 ** TODO: Maybe re-write this on close with final average
954 bytespersec = psf->byterate (psf) ;
956 /* Average block size. Info only I think. */
957 blocksize = (1152 * bytespersec) / psf->sf.samplerate ;
959 /* Can be set to block size IFF the block size is
960 ** constant, set to 1 otherwise. Constant sized
961 ** MPEG block streams are uncommon (CBR @ 32kHz and
962 ** 48kHz only. Meh. */
965 /* TODO: Only flags defined are padding-type. I /think/
966 ** Lame does ISO style padding by default, which has a
971 /* Should only vary per MPEG 1.0/2.0 vs '2.5'.
972 ** TODO: Move this out to MPEG specific place? */
973 samplesperblock = psf->sf.samplerate >= 32000 ? 1152 : 576 ;
975 /* Set as 0 if unknown.
976 ** TODO: Plumb this cleanly from Lame.
981 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 + 2 + 4 + 2 + 2 + 2 ;
983 /* fmt : size, WAV format type, channels. */
984 psf_binheader_writef (psf, "422", BHW4 (fmt_size), BHW2 (WAVE_FORMAT_MPEGLAYER3), BHW2 (psf->sf.channels)) ;
986 /* fmt : samplerate, bytespersec. */
987 psf_binheader_writef (psf, "44", BHW4 (psf->sf.samplerate), BHW4 (bytespersec)) ;
989 /* fmt : blockalign, bitwidth, extrabytes, id. */
990 psf_binheader_writef (psf, "2222", BHW2 (blockalign), BHW2 (0), BHW2 (12), BHW2 (1)) ;
992 /* fmt : flags, blocksize, samplesperblock, codecdelay */
993 psf_binheader_writef (psf, "4222", BHW4 (flags), BHW2 (blocksize), BHW2 (samplesperblock), BHW2 (codecdelay)) ;
996 add_fact_chunk = SF_TRUE ;
1000 default : return SFE_UNIMPLEMENTED ;
1004 psf_binheader_writef (psf, "tm48", BHWm (fact_MARKER), BHW4 (4), BHW8 (psf->sf.frames)) ;
1007 } /* wav_write_fmt_chunk */
1010 wavex_write_fmt_chunk (SF_PRIVATE *psf)
1011 { WAVLIKE_PRIVATE *wpriv ;
1012 int subformat, fmt_size ;
1014 if ((wpriv = psf->container_data) == NULL)
1015 return SFE_INTERNAL ;
1017 subformat = SF_CODEC (psf->sf.format) ;
1019 /* initial section (same for all, it appears) */
1021 { case SF_FORMAT_PCM_U8 :
1022 case SF_FORMAT_PCM_16 :
1023 case SF_FORMAT_PCM_24 :
1024 case SF_FORMAT_PCM_32 :
1025 case SF_FORMAT_FLOAT :
1026 case SF_FORMAT_DOUBLE :
1027 case SF_FORMAT_ULAW :
1028 case SF_FORMAT_ALAW :
1029 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 + 2 + 4 + 4 + 2 + 2 + 8 ;
1031 /* fmt : format, channels, samplerate */
1032 psf_binheader_writef (psf, "4224", BHW4 (fmt_size), BHW2 (WAVE_FORMAT_EXTENSIBLE), BHW2 (psf->sf.channels), BHW4 (psf->sf.samplerate)) ;
1033 /* fmt : bytespersec */
1034 psf_binheader_writef (psf, "4", BHW4 (psf->sf.samplerate * psf->bytewidth * psf->sf.channels)) ;
1035 /* fmt : blockalign, bitwidth */
1036 psf_binheader_writef (psf, "22", BHW2 (psf->bytewidth * psf->sf.channels), BHW2 (psf->bytewidth * 8)) ;
1038 /* cbSize 22 is sizeof (WAVEFORMATEXTENSIBLE) - sizeof (WAVEFORMATEX) */
1039 psf_binheader_writef (psf, "2", BHW2 (22)) ;
1041 /* wValidBitsPerSample, for our use same as bitwidth as we use it fully */
1042 psf_binheader_writef (psf, "2", BHW2 (psf->bytewidth * 8)) ;
1044 /* For an Ambisonic file set the channel mask to zero.
1045 ** Otherwise use a default based on the channel count.
1047 if (wpriv->wavex_ambisonic != SF_AMBISONIC_NONE)
1048 psf_binheader_writef (psf, "4", BHW4 (0)) ;
1049 else if (wpriv->wavex_channelmask != 0)
1050 psf_binheader_writef (psf, "4", BHW4 (wpriv->wavex_channelmask)) ;
1053 ** Ok some liberty is taken here to use the most commonly used channel masks
1054 ** instead of "no mapping". If you really want to use "no mapping" for 8 channels and less
1055 ** please don't use wavex. (otherwise we'll have to create a new SF_COMMAND)
1057 switch (psf->sf.channels)
1058 { case 1 : /* center channel mono */
1059 psf_binheader_writef (psf, "4", BHW4 (0x4)) ;
1062 case 2 : /* front left and right */
1063 psf_binheader_writef (psf, "4", BHW4 (0x1 | 0x2)) ;
1067 psf_binheader_writef (psf, "4", BHW4 (0x1 | 0x2 | 0x10 | 0x20)) ;
1071 psf_binheader_writef (psf, "4", BHW4 (0x1 | 0x2 | 0x4 | 0x8 | 0x10 | 0x20)) ;
1075 psf_binheader_writef (psf, "4", BHW4 (0x1 | 0x2 | 0x4 | 0x8 | 0x10 | 0x20 | 0x40 | 0x80)) ;
1078 default : /* 0 when in doubt , use direct out, ie NO mapping*/
1079 psf_binheader_writef (psf, "4", BHW4 (0x0)) ;
1085 case SF_FORMAT_MS_ADPCM : /* Todo, GUID exists might have different header as per wav_write_header */
1087 return SFE_UNIMPLEMENTED ;
1090 /* GUID section, different for each */
1093 { case SF_FORMAT_PCM_U8 :
1094 case SF_FORMAT_PCM_16 :
1095 case SF_FORMAT_PCM_24 :
1096 case SF_FORMAT_PCM_32 :
1097 wavlike_write_guid (psf, wpriv->wavex_ambisonic == SF_AMBISONIC_NONE ?
1098 &MSGUID_SUBTYPE_PCM : &MSGUID_SUBTYPE_AMBISONIC_B_FORMAT_PCM) ;
1101 case SF_FORMAT_FLOAT :
1102 case SF_FORMAT_DOUBLE :
1103 wavlike_write_guid (psf, wpriv->wavex_ambisonic == SF_AMBISONIC_NONE ?
1104 &MSGUID_SUBTYPE_IEEE_FLOAT : &MSGUID_SUBTYPE_AMBISONIC_B_FORMAT_IEEE_FLOAT) ;
1107 case SF_FORMAT_ULAW :
1108 wavlike_write_guid (psf, &MSGUID_SUBTYPE_MULAW) ;
1111 case SF_FORMAT_ALAW :
1112 wavlike_write_guid (psf, &MSGUID_SUBTYPE_ALAW) ;
1116 /* This is dead code due to return in previous switch statement. */
1117 case SF_FORMAT_MS_ADPCM : /* todo, GUID exists */
1118 wavlike_write_guid (psf, &MSGUID_SUBTYPE_MS_ADPCM) ;
1120 return SFE_UNIMPLEMENTED ;
1123 default : return SFE_UNIMPLEMENTED ;
1126 psf_binheader_writef (psf, "tm48", BHWm (fact_MARKER), BHW4 (4), BHW8 (psf->sf.frames)) ;
1129 } /* wavex_write_fmt_chunk */
1133 wav_write_header (SF_PRIVATE *psf, int calc_length)
1134 { sf_count_t current ;
1135 int error, has_data = SF_FALSE ;
1137 current = psf_ftell (psf) ;
1139 if (current > psf->dataoffset)
1140 has_data = SF_TRUE ;
1143 { psf->filelength = psf_get_filelen (psf) ;
1145 psf->datalength = psf->filelength - psf->dataoffset ;
1148 psf->datalength -= psf->filelength - psf->dataend ;
1149 else if (psf->bytewidth > 0 && psf->sf.seekable == SF_TRUE)
1150 psf->datalength = psf->sf.frames * psf->bytewidth * psf->sf.channels ;
1153 /* Reset the current header length to zero. */
1154 psf->header.ptr [0] = 0 ;
1155 psf->header.indx = 0 ;
1156 psf_fseek (psf, 0, SEEK_SET) ;
1159 ** RIFX signifies big-endian format for all header and data.
1160 ** To prevent lots of code copying here, we'll set the psf->rwf_endian flag
1161 ** once here, and never specify endian-ness for all other header operations.
1164 /* RIFF/RIFX marker, length, WAVE and 'fmt ' markers. */
1166 if (psf->endian == SF_ENDIAN_LITTLE)
1167 psf_binheader_writef (psf, "etm8", BHWm (RIFF_MARKER), BHW8 ((psf->filelength < 8) ? 8 : psf->filelength - 8)) ;
1169 psf_binheader_writef (psf, "Etm8", BHWm (RIFX_MARKER), BHW8 ((psf->filelength < 8) ? 8 : psf->filelength - 8)) ;
1171 /* WAVE and 'fmt ' markers. */
1172 psf_binheader_writef (psf, "mm", BHWm (WAVE_MARKER), BHWm (fmt_MARKER)) ;
1174 /* Write the 'fmt ' chunk. */
1175 switch (SF_CONTAINER (psf->sf.format))
1176 { case SF_FORMAT_WAV :
1177 if ((error = wav_write_fmt_chunk (psf)) != 0)
1181 case SF_FORMAT_WAVEX :
1182 if ((error = wavex_write_fmt_chunk (psf)) != 0)
1187 return SFE_UNIMPLEMENTED ;
1190 /* The LIST/INFO chunk. */
1191 if (psf->strings.flags & SF_STR_LOCATE_START)
1192 wavlike_write_strings (psf, SF_STR_LOCATE_START) ;
1194 if (psf->peak_info != NULL && psf->peak_info->peak_loc == SF_PEAK_START)
1195 wavlike_write_peak_chunk (psf) ;
1197 if (psf->broadcast_16k != NULL)
1198 wavlike_write_bext_chunk (psf) ;
1200 if (psf->cart_16k != NULL)
1201 wavlike_write_cart_chunk (psf) ;
1203 if (psf->cues != NULL)
1206 psf_binheader_writef (psf, "em44", BHWm (cue_MARKER), BHW4 (4 + psf->cues->cue_count * 6 * 4), BHW4 (psf->cues->cue_count)) ;
1208 for (k = 0 ; k < psf->cues->cue_count ; k++)
1209 psf_binheader_writef (psf, "e44m444", BHW4 (psf->cues->cue_points [k].indx), BHW4 (psf->cues->cue_points [k].position),
1210 BHWm (psf->cues->cue_points [k].fcc_chunk), BHW4 (psf->cues->cue_points [k].chunk_start),
1211 BHW4 (psf->cues->cue_points [k].block_start), BHW4 (psf->cues->cue_points [k].sample_offset)) ;
1214 if (psf->instrument != NULL)
1216 double dtune = (double) (0x40000000) / 25.0 ;
1218 psf_binheader_writef (psf, "m4", BHWm (smpl_MARKER), BHW4 (9 * 4 + psf->instrument->loop_count * 6 * 4)) ;
1219 psf_binheader_writef (psf, "44", BHW4 (0), BHW4 (0)) ; /* Manufacturer zero is everyone */
1220 tmp = (int) (1.0e9 / psf->sf.samplerate) ; /* Sample period in nano seconds */
1221 psf_binheader_writef (psf, "44", BHW4 (tmp), BHW4 (psf->instrument->basenote)) ;
1222 tmp = (uint32_t) (psf->instrument->detune * dtune + 0.5) ;
1223 psf_binheader_writef (psf, "4", BHW4 (tmp)) ;
1224 psf_binheader_writef (psf, "44", BHW4 (0), BHW4 (0)) ; /* SMTPE format */
1225 psf_binheader_writef (psf, "44", BHW4 (psf->instrument->loop_count), BHW4 (0)) ;
1227 /* Make sure we don't read past the loops array end. */
1228 if (psf->instrument->loop_count > ARRAY_LEN (psf->instrument->loops))
1229 psf->instrument->loop_count = ARRAY_LEN (psf->instrument->loops) ;
1231 for (tmp = 0 ; tmp < psf->instrument->loop_count ; tmp++)
1234 type = psf->instrument->loops [tmp].mode ;
1235 type = (type == SF_LOOP_FORWARD ? 0 : type == SF_LOOP_BACKWARD ? 2 : type == SF_LOOP_ALTERNATING ? 1 : 32) ;
1237 psf_binheader_writef (psf, "44", BHW4 (tmp), BHW4 (type)) ;
1238 psf_binheader_writef (psf, "44", BHW4 (psf->instrument->loops [tmp].start), BHW4 (psf->instrument->loops [tmp].end - 1)) ;
1239 psf_binheader_writef (psf, "44", BHW4 (0), BHW4 (psf->instrument->loops [tmp].count)) ;
1243 /* Write custom headers. */
1244 if (psf->wchunks.used > 0)
1245 wavlike_write_custom_chunks (psf) ;
1247 if (psf->header.indx + 16 < psf->dataoffset)
1248 { /* Add PAD data if necessary. */
1249 size_t k = psf->dataoffset - (psf->header.indx + 16) ;
1250 psf_binheader_writef (psf, "m4z", BHWm (PAD_MARKER), BHW4 (k), BHWz (k)) ;
1253 psf_binheader_writef (psf, "tm8", BHWm (data_MARKER), BHW8 (psf->datalength)) ;
1254 psf_fwrite (psf->header.ptr, psf->header.indx, 1, psf) ;
1258 if (has_data && psf->dataoffset != psf->header.indx)
1259 { psf_log_printf (psf, "Oooops : has_data && psf->dataoffset != psf->header.indx\n") ;
1260 return psf->error = SFE_INTERNAL ;
1263 psf->dataoffset = psf->header.indx ;
1266 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
1267 else if (current > 0)
1268 psf_fseek (psf, current, SEEK_SET) ;
1271 } /* wav_write_header */
1275 wav_write_tailer (SF_PRIVATE *psf)
1277 /* Reset the current header buffer length to zero. */
1278 psf->header.ptr [0] = 0 ;
1279 psf->header.indx = 0 ;
1281 if (psf->bytewidth > 0 && psf->sf.seekable == SF_TRUE)
1282 { psf->datalength = psf->sf.frames * psf->bytewidth * psf->sf.channels ;
1283 psf->dataend = psf->dataoffset + psf->datalength ;
1286 if (psf->dataend > 0)
1287 psf_fseek (psf, psf->dataend, SEEK_SET) ;
1289 psf->dataend = psf_fseek (psf, 0, SEEK_END) ;
1291 if (psf->dataend & 1)
1292 psf_binheader_writef (psf, "z", BHWz (1)) ;
1294 /* Add a PEAK chunk if requested. */
1295 if (psf->peak_info != NULL && psf->peak_info->peak_loc == SF_PEAK_END)
1296 wavlike_write_peak_chunk (psf) ;
1298 if (psf->strings.flags & SF_STR_LOCATE_END)
1299 wavlike_write_strings (psf, SF_STR_LOCATE_END) ;
1301 /* Write the tailer. */
1302 if (psf->header.indx > 0)
1303 psf_fwrite (psf->header.ptr, psf->header.indx, 1, psf) ;
1306 } /* wav_write_tailer */
1309 wav_close (SF_PRIVATE *psf)
1311 if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
1312 { wav_write_tailer (psf) ;
1314 if (psf->file.mode == SFM_RDWR)
1315 { sf_count_t current = psf_ftell (psf) ;
1318 ** If the mode is RDWR and the current position is less than the
1319 ** filelength, truncate the file.
1322 if (current < psf->filelength)
1323 { psf_ftruncate (psf, current) ;
1324 psf->filelength = current ;
1328 psf->write_header (psf, SF_TRUE) ;
1335 wav_command (SF_PRIVATE *psf, int command, void * UNUSED (data), int datasize)
1336 { WAVLIKE_PRIVATE *wpriv ;
1338 if ((wpriv = psf->container_data) == NULL)
1339 return SFE_INTERNAL ;
1342 { case SFC_WAVEX_SET_AMBISONIC :
1343 if ((SF_CONTAINER (psf->sf.format)) == SF_FORMAT_WAVEX)
1344 { if (datasize == SF_AMBISONIC_NONE)
1345 wpriv->wavex_ambisonic = SF_AMBISONIC_NONE ;
1346 else if (datasize == SF_AMBISONIC_B_FORMAT)
1347 wpriv->wavex_ambisonic = SF_AMBISONIC_B_FORMAT ;
1351 return wpriv->wavex_ambisonic ;
1353 case SFC_WAVEX_GET_AMBISONIC :
1354 return wpriv->wavex_ambisonic ;
1356 case SFC_SET_CHANNEL_MAP_INFO :
1357 wpriv->wavex_channelmask = wavlike_gen_channel_mask (psf->channel_map, psf->sf.channels) ;
1358 return (wpriv->wavex_channelmask != 0) ;
1368 wav_read_smpl_chunk (SF_PRIVATE *psf, uint32_t chunklen)
1369 { char buffer [512] ;
1370 uint32_t thisread, bytesread = 0, dword, sampler_data, loop_count, actually_loop_count = 0 ;
1371 uint32_t note, pitch, start, end, type = -1, count ;
1374 chunklen += (chunklen & 1) ;
1376 bytesread += psf_binheader_readf (psf, "4", &dword) ;
1377 psf_log_printf (psf, " Manufacturer : %X\n", dword) ;
1379 bytesread += psf_binheader_readf (psf, "4", &dword) ;
1380 psf_log_printf (psf, " Product : %u\n", dword) ;
1382 bytesread += psf_binheader_readf (psf, "4", &dword) ;
1383 psf_log_printf (psf, " Period : %u nsec\n", dword) ;
1385 bytesread += psf_binheader_readf (psf, "4", ¬e) ;
1386 psf_log_printf (psf, " Midi Note : %u\n", note) ;
1388 bytesread += psf_binheader_readf (psf, "4", &pitch) ;
1390 { snprintf (buffer, sizeof (buffer), "%f",
1391 (1.0 * 0x80000000) / ((uint32_t) pitch)) ;
1392 psf_log_printf (psf, " Pitch Fract. : %s\n", buffer) ;
1395 psf_log_printf (psf, " Pitch Fract. : 0\n") ;
1397 bytesread += psf_binheader_readf (psf, "4", &dword) ;
1398 psf_log_printf (psf, " SMPTE Format : %u\n", dword) ;
1400 bytesread += psf_binheader_readf (psf, "4", &dword) ;
1401 snprintf (buffer, sizeof (buffer), "%02"PRIu32 ":%02"PRIu32 ":%02"PRIu32
1402 " %02"PRIu32 "", (dword >> 24) & 0x7F, (dword >> 16) & 0x7F,
1403 (dword >> 8) & 0x7F, dword & 0x7F) ;
1404 psf_log_printf (psf, " SMPTE Offset : %s\n", buffer) ;
1406 bytesread += psf_binheader_readf (psf, "4", &loop_count) ;
1407 psf_log_printf (psf, " Loop Count : %u\n", loop_count) ;
1409 if (loop_count == 0 && chunklen == bytesread)
1412 /* Sampler Data holds the number of data bytes after the CUE chunks which
1413 ** is not actually CUE data. Display value after CUE data.
1415 bytesread += psf_binheader_readf (psf, "4", &sampler_data) ;
1417 if (psf->instrument)
1418 { psf_log_printf (psf, " Found more than one SMPL chunk, using last one.\n") ;
1419 free (psf->instrument) ;
1420 psf->instrument = NULL ;
1422 if ((psf->instrument = psf_instrument_alloc ()) == NULL)
1423 return SFE_MALLOC_FAILED ;
1425 psf->instrument->loop_count = loop_count ;
1427 for (j = 0 ; loop_count > 0 && chunklen - bytesread >= 24 ; j ++)
1428 { if ((thisread = psf_binheader_readf (psf, "4", &dword)) == 0)
1430 bytesread += thisread ;
1431 psf_log_printf (psf, " Cue ID : %2u", dword) ;
1433 bytesread += psf_binheader_readf (psf, "4", &type) ;
1434 psf_log_printf (psf, " Type : %2u", type) ;
1436 bytesread += psf_binheader_readf (psf, "4", &start) ;
1437 psf_log_printf (psf, " Start : %5u", start) ;
1439 bytesread += psf_binheader_readf (psf, "4", &end) ;
1440 psf_log_printf (psf, " End : %5u", end) ;
1442 bytesread += psf_binheader_readf (psf, "4", &dword) ;
1443 psf_log_printf (psf, " Fraction : %5u", dword) ;
1445 bytesread += psf_binheader_readf (psf, "4", &count) ;
1446 psf_log_printf (psf, " Count : %5u\n", count) ;
1448 if (j < ARRAY_LEN (psf->instrument->loops))
1449 { psf->instrument->loops [j].start = start ;
1450 psf->instrument->loops [j].end = end + 1 ;
1451 psf->instrument->loops [j].count = count ;
1455 psf->instrument->loops [j].mode = SF_LOOP_FORWARD ;
1458 psf->instrument->loops [j].mode = SF_LOOP_ALTERNATING ;
1461 psf->instrument->loops [j].mode = SF_LOOP_BACKWARD ;
1464 psf->instrument->loops [j].mode = SF_LOOP_NONE ;
1468 actually_loop_count ++ ;
1471 if (actually_loop_count > ARRAY_LEN (psf->instrument->loops))
1473 psf_log_printf (psf, "*** Warning, actual Loop Points count exceeds %u, changing Loop Count from %u to %u\n", ARRAY_LEN (psf->instrument->loops), loop_count, ARRAY_LEN (psf->instrument->loops)) ;
1474 psf->instrument->loop_count = ARRAY_LEN (psf->instrument->loops) ;
1476 else if (loop_count != actually_loop_count)
1477 { psf_log_printf (psf, "*** Warning, actual Loop Points count != Loop Count, changing Loop Count from %u to %u\n", loop_count, actually_loop_count) ;
1478 psf->instrument->loop_count = actually_loop_count ;
1481 if (chunklen - bytesread == 0)
1482 { if (sampler_data != 0)
1483 psf_log_printf (psf, " Sampler Data : %u (should be 0)\n", sampler_data) ;
1485 psf_log_printf (psf, " Sampler Data : %u\n", sampler_data) ;
1488 { if (sampler_data != chunklen - bytesread)
1489 { psf_log_printf (psf, " Sampler Data : %u (should have been %u)\n", sampler_data, chunklen - bytesread) ;
1490 sampler_data = chunklen - bytesread ;
1493 psf_log_printf (psf, " Sampler Data : %u\n", sampler_data) ;
1495 psf_log_printf (psf, " ") ;
1496 for (k = 0 ; k < (int) sampler_data ; k++)
1499 if (k > 0 && (k % 20) == 0)
1500 psf_log_printf (psf, "\n ") ;
1502 if ((thisread = psf_binheader_readf (psf, "1", &ch)) == 0)
1504 bytesread += thisread ;
1505 psf_log_printf (psf, "%02X ", ch & 0xFF) ;
1508 psf_log_printf (psf, "\n") ;
1511 psf->instrument->basenote = note ;
1512 psf->instrument->detune = (int8_t) (pitch / (0x40000000 / 25.0) + 0.5) ;
1513 psf->instrument->gain = 1 ;
1514 psf->instrument->velocity_lo = psf->instrument->key_lo = 0 ;
1515 psf->instrument->velocity_hi = psf->instrument->key_hi = 127 ;
1518 } /* wav_read_smpl_chunk */
1521 ** The acid chunk goes a little something like this:
1524 ** 4 bytes (int) length of chunk starting at next byte
1526 ** 4 bytes (int) type of file:
1527 ** this appears to be a bit mask,however some combinations
1528 ** are probably impossible and/or qualified as "errors"
1530 ** 0x01 On: One Shot Off: Loop
1531 ** 0x02 On: Root note is Set Off: No root
1532 ** 0x04 On: Stretch is On, Off: Strech is OFF
1533 ** 0x08 On: Disk Based Off: Ram based
1534 ** 0x10 On: ?????????? Off: ????????? (Acidizer puts that ON)
1536 ** 2 bytes (short) root note
1537 ** if type 0x10 is OFF : [C,C#,(...),B] -> [0x30 to 0x3B]
1538 ** if type 0x10 is ON : [C,C#,(...),B] -> [0x3C to 0x47]
1539 ** (both types fit on same MIDI pitch albeit different octaves, so who cares)
1541 ** 2 bytes (short) ??? always set to 0x8000
1542 ** 4 bytes (float) ??? seems to be always 0
1543 ** 4 bytes (int) number of beats
1544 ** 2 bytes (short) meter denominator //always 4 in SF/ACID
1545 ** 2 bytes (short) meter numerator //always 4 in SF/ACID
1546 ** //are we sure about the order?? usually its num/denom
1547 ** 4 bytes (float) tempo
1552 wav_read_acid_chunk (SF_PRIVATE *psf, uint32_t chunklen)
1553 { char buffer [512] ;
1554 uint32_t bytesread = 0 ;
1556 short rootnote, q1, meter_denom, meter_numer ;
1559 chunklen += (chunklen & 1) ;
1561 bytesread += psf_binheader_readf (psf, "422f", &flags, &rootnote, &q1, &q2) ;
1563 snprintf (buffer, sizeof (buffer), "%f", q2) ;
1565 psf_log_printf (psf, " Flags : 0x%04x (%s,%s,%s,%s,%s)\n", flags,
1566 (flags & 0x01) ? "OneShot" : "Loop",
1567 (flags & 0x02) ? "RootNoteValid" : "RootNoteInvalid",
1568 (flags & 0x04) ? "StretchOn" : "StretchOff",
1569 (flags & 0x08) ? "DiskBased" : "RAMBased",
1570 (flags & 0x10) ? "??On" : "??Off") ;
1572 psf_log_printf (psf, " Root note : 0x%x\n ???? : 0x%04x\n ???? : %s\n",
1573 rootnote, q1, buffer) ;
1575 bytesread += psf_binheader_readf (psf, "422f", &beats, &meter_denom, &meter_numer, &tempo) ;
1576 snprintf (buffer, sizeof (buffer), "%f", tempo) ;
1577 psf_log_printf (psf, " Beats : %d\n Meter : %d/%d\n Tempo : %s\n",
1578 beats, meter_numer, meter_denom, buffer) ;
1580 psf_binheader_readf (psf, "j", chunklen - bytesread) ;
1583 { psf_log_printf (psf, " Found existing loop info, using last one.\n") ;
1584 free (psf->loop_info) ;
1585 psf->loop_info = NULL ;
1587 if ((psf->loop_info = calloc (1, sizeof (SF_LOOP_INFO))) == NULL)
1588 return SFE_MALLOC_FAILED ;
1590 psf->loop_info->time_sig_num = meter_numer ;
1591 psf->loop_info->time_sig_den = meter_denom ;
1592 psf->loop_info->loop_mode = (flags & 0x01) ? SF_LOOP_NONE : SF_LOOP_FORWARD ;
1593 psf->loop_info->num_beats = beats ;
1594 psf->loop_info->bpm = tempo ;
1595 psf->loop_info->root_key = (flags & 0x02) ? rootnote : -1 ;
1598 } /* wav_read_acid_chunk */
1600 /*==============================================================================
1604 wav_set_chunk (SF_PRIVATE *psf, const SF_CHUNK_INFO * chunk_info)
1605 { return psf_save_write_chunk (&psf->wchunks, chunk_info) ;
1606 } /* wav_set_chunk */
1608 static SF_CHUNK_ITERATOR *
1609 wav_next_chunk_iterator (SF_PRIVATE *psf, SF_CHUNK_ITERATOR * iterator)
1610 { return psf_next_chunk_iterator (&psf->rchunks, iterator) ;
1611 } /* wav_next_chunk_iterator */
1614 wav_get_chunk_size (SF_PRIVATE *psf, const SF_CHUNK_ITERATOR * iterator, SF_CHUNK_INFO * chunk_info)
1617 if ((indx = psf_find_read_chunk_iterator (&psf->rchunks, iterator)) < 0)
1618 return SFE_UNKNOWN_CHUNK ;
1620 chunk_info->datalen = psf->rchunks.chunks [indx].len ;
1622 return SFE_NO_ERROR ;
1623 } /* wav_get_chunk_size */
1626 wav_get_chunk_data (SF_PRIVATE *psf, const SF_CHUNK_ITERATOR * iterator, SF_CHUNK_INFO * chunk_info)
1630 if ((indx = psf_find_read_chunk_iterator (&psf->rchunks, iterator)) < 0)
1631 return SFE_UNKNOWN_CHUNK ;
1633 if (chunk_info->data == NULL)
1634 return SFE_BAD_CHUNK_DATA_PTR ;
1636 chunk_info->id_size = psf->rchunks.chunks [indx].id_size ;
1637 memcpy (chunk_info->id, psf->rchunks.chunks [indx].id, sizeof (chunk_info->id) / sizeof (*chunk_info->id)) ;
1639 pos = psf_ftell (psf) ;
1640 psf_fseek (psf, psf->rchunks.chunks [indx].offset, SEEK_SET) ;
1641 psf_fread (chunk_info->data, SF_MIN (chunk_info->datalen, psf->rchunks.chunks [indx].len), 1, psf) ;
1642 psf_fseek (psf, pos, SEEK_SET) ;
1644 return SFE_NO_ERROR ;
1645 } /* wav_get_chunk_data */