2 ** Copyright (C) 1999-2017 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 #define WAVLIKE_PEAK_CHUNK_SIZE(ch) (2 * sizeof (int) + ch * (sizeof (float) + sizeof (int)))
86 HAVE_other = 0x80000000
91 /* known WAVEFORMATEXTENSIBLE GUIDS */
92 static const EXT_SUBFORMAT MSGUID_SUBTYPE_PCM =
93 { 0x00000001, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }
97 static const EXT_SUBFORMAT MSGUID_SUBTYPE_MS_ADPCM =
98 { 0x00000002, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }
102 static const EXT_SUBFORMAT MSGUID_SUBTYPE_IEEE_FLOAT =
103 { 0x00000003, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }
106 static const EXT_SUBFORMAT MSGUID_SUBTYPE_ALAW =
107 { 0x00000006, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }
110 static const EXT_SUBFORMAT MSGUID_SUBTYPE_MULAW =
111 { 0x00000007, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }
115 ** the next two are from
116 ** http://dream.cs.bath.ac.uk/researchdev/wave-ex/bformat.html
118 static const EXT_SUBFORMAT MSGUID_SUBTYPE_AMBISONIC_B_FORMAT_PCM =
119 { 0x00000001, 0x0721, 0x11d3, { 0x86, 0x44, 0xC8, 0xC1, 0xCA, 0x00, 0x00, 0x00 }
122 static const EXT_SUBFORMAT MSGUID_SUBTYPE_AMBISONIC_B_FORMAT_IEEE_FLOAT =
123 { 0x00000003, 0x0721, 0x11d3, { 0x86, 0x44, 0xC8, 0xC1, 0xCA, 0x00, 0x00, 0x00 }
128 /* maybe interesting one day to read the following through sf_read_raw */
129 /* http://www.bath.ac.uk/~masrwd/pvocex/pvocex.html */
130 static const EXT_SUBFORMAT MSGUID_SUBTYPE_PVOCEX =
131 { 0x8312B9C2, 0x2E6E, 0x11d4, { 0xA8, 0x24, 0xDE, 0x5B, 0x96, 0xC3, 0xAB, 0x21 }
135 /*------------------------------------------------------------------------------
136 ** Private static functions.
139 static int wav_read_header (SF_PRIVATE *psf, int *blockalign, int *framesperblock) ;
140 static int wav_write_header (SF_PRIVATE *psf, int calc_length) ;
142 static int wav_write_tailer (SF_PRIVATE *psf) ;
143 static int wav_command (SF_PRIVATE *psf, int command, void *data, int datasize) ;
144 static int wav_close (SF_PRIVATE *psf) ;
146 static int wav_read_smpl_chunk (SF_PRIVATE *psf, uint32_t chunklen) ;
147 static int wav_read_acid_chunk (SF_PRIVATE *psf, uint32_t chunklen) ;
149 static int wav_set_chunk (SF_PRIVATE *psf, const SF_CHUNK_INFO * chunk_info) ;
150 static SF_CHUNK_ITERATOR * wav_next_chunk_iterator (SF_PRIVATE *psf, SF_CHUNK_ITERATOR * iterator) ;
151 static int wav_get_chunk_size (SF_PRIVATE *psf, const SF_CHUNK_ITERATOR * iterator, SF_CHUNK_INFO * chunk_info) ;
152 static int wav_get_chunk_data (SF_PRIVATE *psf, const SF_CHUNK_ITERATOR * iterator, SF_CHUNK_INFO * chunk_info) ;
154 /*------------------------------------------------------------------------------
159 wav_open (SF_PRIVATE *psf)
160 { WAVLIKE_PRIVATE * wpriv ;
161 int format, subformat, error, blockalign = 0, framesperblock = 0 ;
163 if ((wpriv = calloc (1, sizeof (WAVLIKE_PRIVATE))) == NULL)
164 return SFE_MALLOC_FAILED ;
165 psf->container_data = wpriv ;
167 wpriv->wavex_ambisonic = SF_AMBISONIC_NONE ;
168 psf->strings.flags = SF_STR_ALLOW_START | SF_STR_ALLOW_END ;
170 if (psf->file.mode == SFM_READ || (psf->file.mode == SFM_RDWR && psf->filelength > 0))
171 { if ((error = wav_read_header (psf, &blockalign, &framesperblock)))
174 psf->next_chunk_iterator = wav_next_chunk_iterator ;
175 psf->get_chunk_size = wav_get_chunk_size ;
176 psf->get_chunk_data = wav_get_chunk_data ;
179 subformat = SF_CODEC (psf->sf.format) ;
181 if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
183 return SFE_NO_PIPE_WRITE ;
185 wpriv->wavex_ambisonic = SF_AMBISONIC_NONE ;
187 format = SF_CONTAINER (psf->sf.format) ;
188 if (format != SF_FORMAT_WAV && format != SF_FORMAT_WAVEX)
189 return SFE_BAD_OPEN_FORMAT ;
191 psf->blockwidth = psf->bytewidth * psf->sf.channels ;
193 /* RIFF WAVs are little-endian, RIFX WAVs are big-endian, default to little */
194 psf->endian = SF_ENDIAN (psf->sf.format) ;
195 if (CPU_IS_BIG_ENDIAN && psf->endian == SF_ENDIAN_CPU)
196 psf->endian = SF_ENDIAN_BIG ;
197 else if (psf->endian != SF_ENDIAN_BIG)
198 psf->endian = SF_ENDIAN_LITTLE ;
200 if (psf->file.mode != SFM_RDWR || psf->filelength < 44)
201 { psf->filelength = 0 ;
202 psf->datalength = 0 ;
203 psf->dataoffset = 0 ;
207 if (subformat == SF_FORMAT_IMA_ADPCM || subformat == SF_FORMAT_MS_ADPCM)
208 { blockalign = wavlike_srate2blocksize (psf->sf.samplerate * psf->sf.channels) ;
209 framesperblock = -1 ; /* Corrected later. */
212 /* By default, add the peak chunk to floating point files. Default behaviour
213 ** can be switched off using sf_command (SFC_SET_PEAK_CHUNK, SF_FALSE).
215 if (psf->file.mode == SFM_WRITE && (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE))
216 { if ((psf->peak_info = peak_info_calloc (psf->sf.channels)) == NULL)
217 return SFE_MALLOC_FAILED ;
218 psf->peak_info->peak_loc = SF_PEAK_START ;
221 psf->write_header = wav_write_header ;
222 psf->set_chunk = wav_set_chunk ;
225 psf->container_close = wav_close ;
226 psf->command = wav_command ;
229 { case SF_FORMAT_PCM_U8 :
230 case SF_FORMAT_PCM_16 :
231 case SF_FORMAT_PCM_24 :
232 case SF_FORMAT_PCM_32 :
233 error = pcm_init (psf) ;
236 case SF_FORMAT_ULAW :
237 error = ulaw_init (psf) ;
240 case SF_FORMAT_ALAW :
241 error = alaw_init (psf) ;
244 /* Lite remove start */
245 case SF_FORMAT_FLOAT :
246 error = float32_init (psf) ;
249 case SF_FORMAT_DOUBLE :
250 error = double64_init (psf) ;
253 case SF_FORMAT_IMA_ADPCM :
254 error = wavlike_ima_init (psf, blockalign, framesperblock) ;
257 case SF_FORMAT_MS_ADPCM :
258 error = wavlike_msadpcm_init (psf, blockalign, framesperblock) ;
261 /* Lite remove end */
263 default : return SFE_UNIMPLEMENTED ;
266 if (psf->file.mode == SFM_WRITE || (psf->file.mode == SFM_RDWR && psf->filelength == 0))
267 return psf->write_header (psf, SF_FALSE) ;
272 /*=========================================================================
273 ** Private functions.
277 wav_read_header (SF_PRIVATE *psf, int *blockalign, int *framesperblock)
278 { WAVLIKE_PRIVATE *wpriv ;
280 FACT_CHUNK fact_chunk ;
281 uint32_t marker, chunk_size = 0, RIFFsize = 0, done = 0 ;
282 int parsestage = 0, error, format = 0 ;
284 if (psf->is_pipe == 0 && psf->filelength > SF_PLATFORM_S64 (0xffffffff))
285 psf_log_printf (psf, "Warning : filelength > 0xffffffff. This is bad!!!!\n") ;
287 if ((wpriv = psf->container_data) == NULL)
288 return SFE_INTERNAL ;
289 wav_fmt = &wpriv->wav_fmt ;
291 /* Set position to start of file to begin reading header. */
292 psf_binheader_readf (psf, "pmj", 0, &marker, -4) ;
293 psf->header.indx = 0 ;
295 /* RIFX signifies big-endian format for all header and data to prevent
296 ** lots of code copying here, we'll set the psf->rwf_endian flag once here,
297 ** and never specify endian-ness for all other header ops/
299 psf->rwf_endian = (marker == RIFF_MARKER) ? SF_ENDIAN_LITTLE : SF_ENDIAN_BIG ;
302 { size_t jump = chunk_size & 1 ;
304 marker = chunk_size = 0 ;
305 psf_binheader_readf (psf, "jm4", jump, &marker, &chunk_size) ;
307 { sf_count_t pos = psf_ftell (psf) ;
308 psf_log_printf (psf, "Have 0 marker at position %D (0x%x).\n", pos, pos) ;
312 psf_store_read_chunk_u32 (&psf->rchunks, marker, psf_ftell (psf), chunk_size) ;
318 return SFE_WAV_NO_RIFF ;
320 parsestage |= HAVE_RIFF ;
322 RIFFsize = chunk_size ;
324 if (psf->fileoffset > 0 && psf->filelength > RIFFsize + 8)
325 { /* Set file length. */
326 psf->filelength = RIFFsize + 8 ;
327 if (marker == RIFF_MARKER)
328 psf_log_printf (psf, "RIFF : %u\n", RIFFsize) ;
330 psf_log_printf (psf, "RIFX : %u\n", RIFFsize) ;
332 else if (psf->filelength < RIFFsize + 2 * SIGNED_SIZEOF (marker))
333 { if (marker == RIFF_MARKER)
334 psf_log_printf (psf, "RIFF : %u (should be %D)\n", RIFFsize, psf->filelength - 2 * SIGNED_SIZEOF (marker)) ;
336 psf_log_printf (psf, "RIFX : %u (should be %D)\n", RIFFsize, psf->filelength - 2 * SIGNED_SIZEOF (marker)) ;
338 RIFFsize = psf->filelength - 2 * SIGNED_SIZEOF (RIFFsize) ;
341 { if (marker == RIFF_MARKER)
342 psf_log_printf (psf, "RIFF : %u\n", RIFFsize) ;
344 psf_log_printf (psf, "RIFX : %u\n", RIFFsize) ;
347 psf_binheader_readf (psf, "m", &marker) ;
348 if (marker != WAVE_MARKER)
349 return SFE_WAV_NO_WAVE ;
350 parsestage |= HAVE_WAVE ;
351 psf_log_printf (psf, "WAVE\n") ;
356 if ((parsestage & (HAVE_RIFF | HAVE_WAVE)) != (HAVE_RIFF | HAVE_WAVE))
357 return SFE_WAV_NO_FMT ;
359 /* If this file has a SECOND fmt chunk, I don't want to know about it. */
360 if (parsestage & HAVE_fmt)
363 parsestage |= HAVE_fmt ;
365 psf_log_printf (psf, "fmt : %d\n", chunk_size) ;
367 if ((error = wavlike_read_fmt_chunk (psf, chunk_size)))
370 format = wav_fmt->format ;
374 if ((parsestage & (HAVE_RIFF | HAVE_WAVE | HAVE_fmt)) != (HAVE_RIFF | HAVE_WAVE | HAVE_fmt))
375 return SFE_WAV_NO_DATA ;
377 if (psf->file.mode == SFM_RDWR && (parsestage & HAVE_other) != 0)
378 return SFE_RDWR_BAD_HEADER ;
380 parsestage |= HAVE_data ;
382 psf->datalength = chunk_size ;
383 if (psf->datalength & 1)
384 psf_log_printf (psf, "*** 'data' chunk should be an even number of bytes in length.\n") ;
386 psf->dataoffset = psf_ftell (psf) ;
388 if (psf->dataoffset > 0)
389 { if (chunk_size == 0 && RIFFsize == 8 && psf->filelength > 44)
390 { psf_log_printf (psf, "*** Looks like a WAV file which wasn't closed properly. Fixing it.\n") ;
391 psf->datalength = psf->filelength - psf->dataoffset ;
394 if (psf->datalength > psf->filelength - psf->dataoffset)
395 { psf_log_printf (psf, "data : %D (should be %D)\n", psf->datalength, psf->filelength - psf->dataoffset) ;
396 psf->datalength = psf->filelength - psf->dataoffset ;
399 psf_log_printf (psf, "data : %D\n", psf->datalength) ;
401 /* Only set dataend if there really is data at the end. */
402 if (psf->datalength + psf->dataoffset < psf->filelength)
403 psf->dataend = psf->datalength + psf->dataoffset ;
405 psf->datalength += chunk_size & 1 ;
409 if (! psf->sf.seekable || psf->dataoffset < 0)
412 /* Seek past data and continue reading header. */
413 psf_fseek (psf, psf->datalength, SEEK_CUR) ;
415 if (psf_ftell (psf) != psf->datalength + psf->dataoffset)
416 psf_log_printf (psf, "*** psf_fseek past end error ***\n") ;
420 if ((parsestage & (HAVE_RIFF | HAVE_WAVE)) != (HAVE_RIFF | HAVE_WAVE))
421 return SFE_WAV_BAD_FACT ;
423 parsestage |= HAVE_fact ;
425 if ((parsestage & HAVE_fmt) != HAVE_fmt)
426 psf_log_printf (psf, "*** Should have 'fmt ' chunk before 'fact'\n") ;
428 psf_binheader_readf (psf, "4", & (fact_chunk.frames)) ;
430 if (chunk_size > SIGNED_SIZEOF (fact_chunk))
431 psf_binheader_readf (psf, "j", (int) (chunk_size - SIGNED_SIZEOF (fact_chunk))) ;
434 psf_log_printf (psf, "%M : %u\n", marker, chunk_size) ;
436 psf_log_printf (psf, "%M : %u (should not be zero)\n", marker, chunk_size) ;
438 psf_log_printf (psf, " frames : %d\n", fact_chunk.frames) ;
442 if ((parsestage & (HAVE_RIFF | HAVE_WAVE | HAVE_fmt)) != (HAVE_RIFF | HAVE_WAVE | HAVE_fmt))
443 return SFE_WAV_PEAK_B4_FMT ;
445 parsestage |= HAVE_PEAK ;
447 psf_log_printf (psf, "%M : %u\n", marker, chunk_size) ;
448 if ((error = wavlike_read_peak_chunk (psf, chunk_size)) != 0)
450 psf->peak_info->peak_loc = ((parsestage & HAVE_data) == 0) ? SF_PEAK_START : SF_PEAK_END ;
454 parsestage |= HAVE_other ;
456 { uint32_t thisread, bytesread, cue_count, position, offset ;
457 int id, chunk_id, chunk_start, block_start, cue_index ;
459 bytesread = psf_binheader_readf (psf, "4", &cue_count) ;
460 psf_log_printf (psf, "%M : %u\n", marker, chunk_size) ;
462 if (cue_count > 1000)
463 { psf_log_printf (psf, " Count : %u (skipping)\n", cue_count) ;
464 psf_binheader_readf (psf, "j", (cue_count > 20 ? 20 : cue_count) * 24) ;
468 psf_log_printf (psf, " Count : %d\n", cue_count) ;
470 if ((psf->cues = psf_cues_alloc (cue_count)) == NULL)
471 return SFE_MALLOC_FAILED ;
477 if ((thisread = psf_binheader_readf (psf, "e44m444", &id, &position, &chunk_id, &chunk_start, &block_start, &offset)) == 0)
479 bytesread += thisread ;
481 psf_log_printf (psf, " Cue ID : %2d"
482 " Pos : %5u Chunk : %M"
483 " Chk Start : %d Blk Start : %d"
485 id, position, chunk_id, chunk_start, block_start, offset) ;
486 psf->cues->cue_points [cue_index].indx = id ;
487 psf->cues->cue_points [cue_index].position = position ;
488 psf->cues->cue_points [cue_index].fcc_chunk = chunk_id ;
489 psf->cues->cue_points [cue_index].chunk_start = chunk_start ;
490 psf->cues->cue_points [cue_index].block_start = block_start ;
491 psf->cues->cue_points [cue_index].sample_offset = offset ;
492 psf->cues->cue_points [cue_index].name [0] = '\0' ;
497 if (bytesread != chunk_size)
498 { psf_log_printf (psf, "**** Chunk size weirdness (%d != %d)\n", chunk_size, bytesread) ;
499 psf_binheader_readf (psf, "j", chunk_size - bytesread) ;
505 parsestage |= HAVE_other ;
507 psf_log_printf (psf, "smpl : %u\n", chunk_size) ;
509 if ((error = wav_read_smpl_chunk (psf, chunk_size)))
514 parsestage |= HAVE_other ;
516 psf_log_printf (psf, "acid : %u\n", chunk_size) ;
518 if ((error = wav_read_acid_chunk (psf, chunk_size)))
524 parsestage |= HAVE_other ;
526 if ((error = wavlike_subchunk_parse (psf, marker, chunk_size)) != 0)
532 The 'bext' chunk can actually be updated, so don't need to set this.
533 parsestage |= HAVE_other ;
535 if ((error = wavlike_read_bext_chunk (psf, chunk_size)))
541 We can eat into a 'PAD ' chunk if we need to.
542 parsestage |= HAVE_other ;
544 psf_log_printf (psf, "%M : %u\n", marker, chunk_size) ;
545 psf_binheader_readf (psf, "j", chunk_size) ;
549 if ((error = wavlike_read_cart_chunk (psf, chunk_size)))
553 case iXML_MARKER : /* See http://en.wikipedia.org/wiki/IXML */
554 case strc_MARKER : /* Multiple of 32 bytes. */
574 psf_log_printf (psf, "%M : %u\n", marker, chunk_size) ;
575 psf_binheader_readf (psf, "j", chunk_size) ;
579 if (chunk_size >= 0xffff0000)
581 psf_log_printf (psf, "*** Unknown chunk marker (%X) at position %D with length %u. Exiting parser.\n", marker, psf_ftell (psf) - 8, chunk_size) ;
585 if (psf_isprint ((marker >> 24) & 0xFF) && psf_isprint ((marker >> 16) & 0xFF)
586 && psf_isprint ((marker >> 8) & 0xFF) && psf_isprint (marker & 0xFF))
587 { psf_log_printf (psf, "*** %M : %u (unknown marker)\n", marker, chunk_size) ;
588 psf_binheader_readf (psf, "j", chunk_size) ;
591 if (psf_ftell (psf) & 0x03)
592 { psf_log_printf (psf, " Unknown chunk marker at position %D. Resynching.\n", psf_ftell (psf) - 8) ;
593 psf_binheader_readf (psf, "j", -3) ;
594 /* File is too messed up so we prevent editing in RDWR mode here. */
595 parsestage |= HAVE_other ;
598 psf_log_printf (psf, "*** Unknown chunk marker (%X) at position %D. Exiting parser.\n", marker, psf_ftell (psf) - 8) ;
601 } ; /* switch (marker) */
603 if (chunk_size >= psf->filelength)
604 { psf_log_printf (psf, "*** Chunk size %u > file length %D. Exiting parser.\n", chunk_size, psf->filelength) ;
608 if (! psf->sf.seekable && (parsestage & HAVE_data))
611 if (psf_ftell (psf) >= psf->filelength - SIGNED_SIZEOF (chunk_size))
612 { psf_log_printf (psf, "End\n") ;
617 if (psf->dataoffset <= 0)
618 return SFE_WAV_NO_DATA ;
620 if (psf->sf.channels < 1)
621 return SFE_CHANNEL_COUNT_ZERO ;
623 if (psf->sf.channels >= SF_MAX_CHANNELS)
624 return SFE_CHANNEL_COUNT ;
626 if (format != WAVE_FORMAT_PCM && (parsestage & HAVE_fact) == 0)
627 psf_log_printf (psf, "**** All non-PCM format files should have a 'fact' chunk.\n") ;
629 /* WAVs can be little or big endian */
630 psf->endian = psf->rwf_endian ;
632 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
634 if (psf->is_pipe == 0)
636 ** Check for 'wvpk' at the start of the DATA section. Not able to
639 psf_binheader_readf (psf, "4", &marker) ;
640 if (marker == wvpk_MARKER || marker == OggS_MARKER)
641 return SFE_WAV_WVPK_DATA ;
644 /* Seek to start of DATA section. */
645 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
648 { if (psf->filelength - psf->dataoffset < psf->datalength)
649 psf->sf.frames = (psf->filelength - psf->dataoffset) / psf->blockwidth ;
651 psf->sf.frames = psf->datalength / psf->blockwidth ;
655 { case WAVE_FORMAT_EXTENSIBLE :
656 if (psf->sf.format == (SF_FORMAT_WAVEX | SF_FORMAT_MS_ADPCM))
657 { *blockalign = wav_fmt->msadpcm.blockalign ;
658 *framesperblock = wav_fmt->msadpcm.samplesperblock ;
662 case WAVE_FORMAT_PCM :
663 psf->sf.format = SF_FORMAT_WAV | u_bitwidth_to_subformat (psf->bytewidth * 8) ;
666 case WAVE_FORMAT_MULAW :
667 case IBM_FORMAT_MULAW :
668 psf->sf.format = (SF_FORMAT_WAV | SF_FORMAT_ULAW) ;
671 case WAVE_FORMAT_ALAW :
672 case IBM_FORMAT_ALAW :
673 psf->sf.format = (SF_FORMAT_WAV | SF_FORMAT_ALAW) ;
676 case WAVE_FORMAT_MS_ADPCM :
677 psf->sf.format = (SF_FORMAT_WAV | SF_FORMAT_MS_ADPCM) ;
678 *blockalign = wav_fmt->msadpcm.blockalign ;
679 *framesperblock = wav_fmt->msadpcm.samplesperblock ;
682 case WAVE_FORMAT_IMA_ADPCM :
683 psf->sf.format = (SF_FORMAT_WAV | SF_FORMAT_IMA_ADPCM) ;
684 *blockalign = wav_fmt->ima.blockalign ;
685 *framesperblock = wav_fmt->ima.samplesperblock ;
688 case WAVE_FORMAT_GSM610 :
689 psf->sf.format = (SF_FORMAT_WAV | SF_FORMAT_GSM610) ;
692 case WAVE_FORMAT_IEEE_FLOAT :
693 psf->sf.format = SF_FORMAT_WAV ;
694 psf->sf.format |= (psf->bytewidth == 8) ? SF_FORMAT_DOUBLE : SF_FORMAT_FLOAT ;
697 case WAVE_FORMAT_G721_ADPCM :
698 psf->sf.format = SF_FORMAT_WAV | SF_FORMAT_G721_32 ;
701 default : return SFE_UNIMPLEMENTED ;
704 if (wpriv->fmt_is_broken)
705 wavlike_analyze (psf) ;
707 /* Only set the format endian-ness if its non-standard big-endian. */
708 if (psf->endian == SF_ENDIAN_BIG)
709 psf->sf.format |= SF_ENDIAN_BIG ;
712 } /* wav_read_header */
715 wav_write_fmt_chunk (SF_PRIVATE *psf)
716 { int subformat, fmt_size, add_fact_chunk = 0 ;
718 subformat = SF_CODEC (psf->sf.format) ;
721 { case SF_FORMAT_PCM_U8 :
722 case SF_FORMAT_PCM_16 :
723 case SF_FORMAT_PCM_24 :
724 case SF_FORMAT_PCM_32 :
725 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 ;
727 /* fmt : format, channels, samplerate */
728 psf_binheader_writef (psf, "4224", BHW4 (fmt_size), BHW2 (WAVE_FORMAT_PCM), BHW2 (psf->sf.channels), BHW4 (psf->sf.samplerate)) ;
729 /* fmt : bytespersec */
730 psf_binheader_writef (psf, "4", BHW4 (psf->sf.samplerate * psf->bytewidth * psf->sf.channels)) ;
731 /* fmt : blockalign, bitwidth */
732 psf_binheader_writef (psf, "22", BHW2 (psf->bytewidth * psf->sf.channels), BHW2 (psf->bytewidth * 8)) ;
735 case SF_FORMAT_FLOAT :
736 case SF_FORMAT_DOUBLE :
737 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 ;
739 /* fmt : format, channels, samplerate */
740 psf_binheader_writef (psf, "4224", BHW4 (fmt_size), BHW2 (WAVE_FORMAT_IEEE_FLOAT), BHW2 (psf->sf.channels), BHW4 (psf->sf.samplerate)) ;
741 /* fmt : bytespersec */
742 psf_binheader_writef (psf, "4", BHW4 (psf->sf.samplerate * psf->bytewidth * psf->sf.channels)) ;
743 /* fmt : blockalign, bitwidth */
744 psf_binheader_writef (psf, "22", BHW2 (psf->bytewidth * psf->sf.channels), BHW2 (psf->bytewidth * 8)) ;
746 add_fact_chunk = SF_TRUE ;
749 case SF_FORMAT_ULAW :
750 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 ;
752 /* fmt : format, channels, samplerate */
753 psf_binheader_writef (psf, "4224", BHW4 (fmt_size), BHW2 (WAVE_FORMAT_MULAW), BHW2 (psf->sf.channels), BHW4 (psf->sf.samplerate)) ;
754 /* fmt : bytespersec */
755 psf_binheader_writef (psf, "4", BHW4 (psf->sf.samplerate * psf->bytewidth * psf->sf.channels)) ;
756 /* fmt : blockalign, bitwidth, extrabytes */
757 psf_binheader_writef (psf, "222", BHW2 (psf->bytewidth * psf->sf.channels), BHW2 (8), BHW2 (0)) ;
759 add_fact_chunk = SF_TRUE ;
762 case SF_FORMAT_ALAW :
763 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 ;
765 /* fmt : format, channels, samplerate */
766 psf_binheader_writef (psf, "4224", BHW4 (fmt_size), BHW2 (WAVE_FORMAT_ALAW), BHW2 (psf->sf.channels), BHW4 (psf->sf.samplerate)) ;
767 /* fmt : bytespersec */
768 psf_binheader_writef (psf, "4", BHW4 (psf->sf.samplerate * psf->bytewidth * psf->sf.channels)) ;
769 /* fmt : blockalign, bitwidth, extrabytes */
770 psf_binheader_writef (psf, "222", BHW2 (psf->bytewidth * psf->sf.channels), BHW2 (8), BHW2 (0)) ;
772 add_fact_chunk = SF_TRUE ;
775 /* Lite remove start */
776 case SF_FORMAT_IMA_ADPCM :
777 { int blockalign, framesperblock, bytespersec ;
779 blockalign = wavlike_srate2blocksize (psf->sf.samplerate * psf->sf.channels) ;
780 framesperblock = 2 * (blockalign - 4 * psf->sf.channels) / psf->sf.channels + 1 ;
781 bytespersec = (psf->sf.samplerate * blockalign) / framesperblock ;
784 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 + 2 ;
786 /* fmt : size, WAV format type, channels, samplerate, bytespersec */
787 psf_binheader_writef (psf, "42244", BHW4 (fmt_size), BHW2 (WAVE_FORMAT_IMA_ADPCM),
788 BHW2 (psf->sf.channels), BHW4 (psf->sf.samplerate), BHW4 (bytespersec)) ;
790 /* fmt : blockalign, bitwidth, extrabytes, framesperblock. */
791 psf_binheader_writef (psf, "2222", BHW2 (blockalign), BHW2 (4), BHW2 (2), BHW2 (framesperblock)) ;
794 add_fact_chunk = SF_TRUE ;
797 case SF_FORMAT_MS_ADPCM :
798 { int blockalign, framesperblock, bytespersec, extrabytes ;
800 blockalign = wavlike_srate2blocksize (psf->sf.samplerate * psf->sf.channels) ;
801 framesperblock = 2 + 2 * (blockalign - 7 * psf->sf.channels) / psf->sf.channels ;
802 bytespersec = (psf->sf.samplerate * blockalign) / framesperblock ;
805 extrabytes = 2 + 2 + WAVLIKE_MSADPCM_ADAPT_COEFF_COUNT * (2 + 2) ;
806 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 + extrabytes ;
808 /* fmt : size, WAV format type, channels. */
809 psf_binheader_writef (psf, "422", BHW4 (fmt_size), BHW2 (WAVE_FORMAT_MS_ADPCM), BHW2 (psf->sf.channels)) ;
811 /* fmt : samplerate, bytespersec. */
812 psf_binheader_writef (psf, "44", BHW4 (psf->sf.samplerate), BHW4 (bytespersec)) ;
814 /* fmt : blockalign, bitwidth, extrabytes, framesperblock. */
815 psf_binheader_writef (psf, "22222", BHW2 (blockalign), BHW2 (4), BHW2 (extrabytes), BHW2 (framesperblock), BHW2 (7)) ;
817 wavlike_msadpcm_write_adapt_coeffs (psf) ;
820 add_fact_chunk = SF_TRUE ;
824 case SF_FORMAT_G721_32 :
826 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 + 2 ;
828 /* fmt : size, WAV format type, channels, samplerate, bytespersec */
829 psf_binheader_writef (psf, "42244", BHW4 (fmt_size), BHW2 (WAVE_FORMAT_G721_ADPCM),
830 BHW2 (psf->sf.channels), BHW4 (psf->sf.samplerate), BHW4 (psf->sf.samplerate * psf->sf.channels / 2)) ;
832 /* fmt : blockalign, bitwidth, extrabytes, auxblocksize. */
833 psf_binheader_writef (psf, "2222", BHW2 (64), BHW2 (4), BHW2 (2), BHW2 (0)) ;
835 add_fact_chunk = SF_TRUE ;
838 /* Lite remove end */
840 case SF_FORMAT_GSM610 :
841 { int blockalign, framesperblock, bytespersec ;
843 blockalign = WAVLIKE_GSM610_BLOCKSIZE ;
844 framesperblock = WAVLIKE_GSM610_SAMPLES ;
845 bytespersec = (psf->sf.samplerate * blockalign) / framesperblock ;
848 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 + 2 ;
850 /* fmt : size, WAV format type, channels. */
851 psf_binheader_writef (psf, "422", BHW4 (fmt_size), BHW2 (WAVE_FORMAT_GSM610), BHW2 (psf->sf.channels)) ;
853 /* fmt : samplerate, bytespersec. */
854 psf_binheader_writef (psf, "44", BHW4 (psf->sf.samplerate), BHW4 (bytespersec)) ;
856 /* fmt : blockalign, bitwidth, extrabytes, framesperblock. */
857 psf_binheader_writef (psf, "2222", BHW2 (blockalign), BHW2 (0), BHW2 (2), BHW2 (framesperblock)) ;
860 add_fact_chunk = SF_TRUE ;
863 default : return SFE_UNIMPLEMENTED ;
867 psf_binheader_writef (psf, "tm48", BHWm (fact_MARKER), BHW4 (4), BHW8 (psf->sf.frames)) ;
870 } /* wav_write_fmt_chunk */
873 wavex_write_fmt_chunk (SF_PRIVATE *psf)
874 { WAVLIKE_PRIVATE *wpriv ;
875 int subformat, fmt_size ;
877 if ((wpriv = psf->container_data) == NULL)
878 return SFE_INTERNAL ;
880 subformat = SF_CODEC (psf->sf.format) ;
882 /* initial section (same for all, it appears) */
884 { case SF_FORMAT_PCM_U8 :
885 case SF_FORMAT_PCM_16 :
886 case SF_FORMAT_PCM_24 :
887 case SF_FORMAT_PCM_32 :
888 case SF_FORMAT_FLOAT :
889 case SF_FORMAT_DOUBLE :
890 case SF_FORMAT_ULAW :
891 case SF_FORMAT_ALAW :
892 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 + 2 + 4 + 4 + 2 + 2 + 8 ;
894 /* fmt : format, channels, samplerate */
895 psf_binheader_writef (psf, "4224", BHW4 (fmt_size), BHW2 (WAVE_FORMAT_EXTENSIBLE), BHW2 (psf->sf.channels), BHW4 (psf->sf.samplerate)) ;
896 /* fmt : bytespersec */
897 psf_binheader_writef (psf, "4", BHW4 (psf->sf.samplerate * psf->bytewidth * psf->sf.channels)) ;
898 /* fmt : blockalign, bitwidth */
899 psf_binheader_writef (psf, "22", BHW2 (psf->bytewidth * psf->sf.channels), BHW2 (psf->bytewidth * 8)) ;
901 /* cbSize 22 is sizeof (WAVEFORMATEXTENSIBLE) - sizeof (WAVEFORMATEX) */
902 psf_binheader_writef (psf, "2", BHW2 (22)) ;
904 /* wValidBitsPerSample, for our use same as bitwidth as we use it fully */
905 psf_binheader_writef (psf, "2", BHW2 (psf->bytewidth * 8)) ;
907 /* For an Ambisonic file set the channel mask to zero.
908 ** Otherwise use a default based on the channel count.
910 if (wpriv->wavex_ambisonic != SF_AMBISONIC_NONE)
911 psf_binheader_writef (psf, "4", BHW4 (0)) ;
912 else if (wpriv->wavex_channelmask != 0)
913 psf_binheader_writef (psf, "4", BHW4 (wpriv->wavex_channelmask)) ;
916 ** Ok some liberty is taken here to use the most commonly used channel masks
917 ** instead of "no mapping". If you really want to use "no mapping" for 8 channels and less
918 ** please don't use wavex. (otherwise we'll have to create a new SF_COMMAND)
920 switch (psf->sf.channels)
921 { case 1 : /* center channel mono */
922 psf_binheader_writef (psf, "4", BHW4 (0x4)) ;
925 case 2 : /* front left and right */
926 psf_binheader_writef (psf, "4", BHW4 (0x1 | 0x2)) ;
930 psf_binheader_writef (psf, "4", BHW4 (0x1 | 0x2 | 0x10 | 0x20)) ;
934 psf_binheader_writef (psf, "4", BHW4 (0x1 | 0x2 | 0x4 | 0x8 | 0x10 | 0x20)) ;
938 psf_binheader_writef (psf, "4", BHW4 (0x1 | 0x2 | 0x4 | 0x8 | 0x10 | 0x20 | 0x40 | 0x80)) ;
941 default : /* 0 when in doubt , use direct out, ie NO mapping*/
942 psf_binheader_writef (psf, "4", BHW4 (0x0)) ;
948 case SF_FORMAT_MS_ADPCM : /* Todo, GUID exists might have different header as per wav_write_header */
950 return SFE_UNIMPLEMENTED ;
953 /* GUID section, different for each */
956 { case SF_FORMAT_PCM_U8 :
957 case SF_FORMAT_PCM_16 :
958 case SF_FORMAT_PCM_24 :
959 case SF_FORMAT_PCM_32 :
960 wavlike_write_guid (psf, wpriv->wavex_ambisonic == SF_AMBISONIC_NONE ?
961 &MSGUID_SUBTYPE_PCM : &MSGUID_SUBTYPE_AMBISONIC_B_FORMAT_PCM) ;
964 case SF_FORMAT_FLOAT :
965 case SF_FORMAT_DOUBLE :
966 wavlike_write_guid (psf, wpriv->wavex_ambisonic == SF_AMBISONIC_NONE ?
967 &MSGUID_SUBTYPE_IEEE_FLOAT : &MSGUID_SUBTYPE_AMBISONIC_B_FORMAT_IEEE_FLOAT) ;
970 case SF_FORMAT_ULAW :
971 wavlike_write_guid (psf, &MSGUID_SUBTYPE_MULAW) ;
974 case SF_FORMAT_ALAW :
975 wavlike_write_guid (psf, &MSGUID_SUBTYPE_ALAW) ;
979 /* This is dead code due to return in previous switch statement. */
980 case SF_FORMAT_MS_ADPCM : /* todo, GUID exists */
981 wavlike_write_guid (psf, &MSGUID_SUBTYPE_MS_ADPCM) ;
983 return SFE_UNIMPLEMENTED ;
986 default : return SFE_UNIMPLEMENTED ;
989 psf_binheader_writef (psf, "tm48", BHWm (fact_MARKER), BHW4 (4), BHW8 (psf->sf.frames)) ;
992 } /* wavex_write_fmt_chunk */
996 wav_write_header (SF_PRIVATE *psf, int calc_length)
997 { sf_count_t current ;
998 int error, has_data = SF_FALSE ;
1000 current = psf_ftell (psf) ;
1002 if (current > psf->dataoffset)
1003 has_data = SF_TRUE ;
1006 { psf->filelength = psf_get_filelen (psf) ;
1008 psf->datalength = psf->filelength - psf->dataoffset ;
1011 psf->datalength -= psf->filelength - psf->dataend ;
1012 else if (psf->bytewidth > 0 && psf->sf.seekable == SF_TRUE)
1013 psf->datalength = psf->sf.frames * psf->bytewidth * psf->sf.channels ;
1016 /* Reset the current header length to zero. */
1017 psf->header.ptr [0] = 0 ;
1018 psf->header.indx = 0 ;
1019 psf_fseek (psf, 0, SEEK_SET) ;
1022 ** RIFX signifies big-endian format for all header and data.
1023 ** To prevent lots of code copying here, we'll set the psf->rwf_endian flag
1024 ** once here, and never specify endian-ness for all other header operations.
1027 /* RIFF/RIFX marker, length, WAVE and 'fmt ' markers. */
1029 if (psf->endian == SF_ENDIAN_LITTLE)
1030 psf_binheader_writef (psf, "etm8", BHWm (RIFF_MARKER), BHW8 ((psf->filelength < 8) ? 8 : psf->filelength - 8)) ;
1032 psf_binheader_writef (psf, "Etm8", BHWm (RIFX_MARKER), BHW8 ((psf->filelength < 8) ? 8 : psf->filelength - 8)) ;
1034 /* WAVE and 'fmt ' markers. */
1035 psf_binheader_writef (psf, "mm", BHWm (WAVE_MARKER), BHWm (fmt_MARKER)) ;
1037 /* Write the 'fmt ' chunk. */
1038 switch (SF_CONTAINER (psf->sf.format))
1039 { case SF_FORMAT_WAV :
1040 if ((error = wav_write_fmt_chunk (psf)) != 0)
1044 case SF_FORMAT_WAVEX :
1045 if ((error = wavex_write_fmt_chunk (psf)) != 0)
1050 return SFE_UNIMPLEMENTED ;
1053 /* The LIST/INFO chunk. */
1054 if (psf->strings.flags & SF_STR_LOCATE_START)
1055 wavlike_write_strings (psf, SF_STR_LOCATE_START) ;
1057 if (psf->peak_info != NULL && psf->peak_info->peak_loc == SF_PEAK_START)
1058 wavlike_write_peak_chunk (psf) ;
1060 if (psf->broadcast_16k != NULL)
1061 wavlike_write_bext_chunk (psf) ;
1063 if (psf->cart_16k != NULL)
1064 wavlike_write_cart_chunk (psf) ;
1066 if (psf->cues != NULL)
1069 psf_binheader_writef (psf, "em44", BHWm (cue_MARKER), BHW4 (4 + psf->cues->cue_count * 6 * 4), BHW4 (psf->cues->cue_count)) ;
1071 for (k = 0 ; k < psf->cues->cue_count ; k++)
1072 psf_binheader_writef (psf, "e44m444", BHW4 (psf->cues->cue_points [k].indx), BHW4 (psf->cues->cue_points [k].position),
1073 BHWm (psf->cues->cue_points [k].fcc_chunk), BHW4 (psf->cues->cue_points [k].chunk_start),
1074 BHW4 (psf->cues->cue_points [k].block_start), BHW4 (psf->cues->cue_points [k].sample_offset)) ;
1077 if (psf->instrument != NULL)
1079 double dtune = (double) (0x40000000) / 25.0 ;
1081 psf_binheader_writef (psf, "m4", BHWm (smpl_MARKER), BHW4 (9 * 4 + psf->instrument->loop_count * 6 * 4)) ;
1082 psf_binheader_writef (psf, "44", BHW4 (0), BHW4 (0)) ; /* Manufacturer zero is everyone */
1083 tmp = (int) (1.0e9 / psf->sf.samplerate) ; /* Sample period in nano seconds */
1084 psf_binheader_writef (psf, "44", BHW4 (tmp), BHW4 (psf->instrument->basenote)) ;
1085 tmp = (uint32_t) (psf->instrument->detune * dtune + 0.5) ;
1086 psf_binheader_writef (psf, "4", BHW4 (tmp)) ;
1087 psf_binheader_writef (psf, "44", BHW4 (0), BHW4 (0)) ; /* SMTPE format */
1088 psf_binheader_writef (psf, "44", BHW4 (psf->instrument->loop_count), BHW4 (0)) ;
1090 for (tmp = 0 ; tmp < psf->instrument->loop_count ; tmp++)
1093 type = psf->instrument->loops [tmp].mode ;
1094 type = (type == SF_LOOP_FORWARD ? 0 : type == SF_LOOP_BACKWARD ? 2 : type == SF_LOOP_ALTERNATING ? 1 : 32) ;
1096 psf_binheader_writef (psf, "44", BHW4 (tmp), BHW4 (type)) ;
1097 psf_binheader_writef (psf, "44", BHW4 (psf->instrument->loops [tmp].start), BHW4 (psf->instrument->loops [tmp].end - 1)) ;
1098 psf_binheader_writef (psf, "44", BHW4 (0), BHW4 (psf->instrument->loops [tmp].count)) ;
1102 /* Write custom headers. */
1103 if (psf->wchunks.used > 0)
1104 wavlike_write_custom_chunks (psf) ;
1106 if (psf->header.indx + 16 < psf->dataoffset)
1107 { /* Add PAD data if necessary. */
1108 size_t k = psf->dataoffset - (psf->header.indx + 16) ;
1109 psf_binheader_writef (psf, "m4z", BHWm (PAD_MARKER), BHW4 (k), BHWz (k)) ;
1112 psf_binheader_writef (psf, "tm8", BHWm (data_MARKER), BHW8 (psf->datalength)) ;
1113 psf_fwrite (psf->header.ptr, psf->header.indx, 1, psf) ;
1117 if (has_data && psf->dataoffset != psf->header.indx)
1118 { psf_log_printf (psf, "Oooops : has_data && psf->dataoffset != psf->header.indx\n") ;
1119 return psf->error = SFE_INTERNAL ;
1122 psf->dataoffset = psf->header.indx ;
1125 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
1126 else if (current > 0)
1127 psf_fseek (psf, current, SEEK_SET) ;
1130 } /* wav_write_header */
1134 wav_write_tailer (SF_PRIVATE *psf)
1136 /* Reset the current header buffer length to zero. */
1137 psf->header.ptr [0] = 0 ;
1138 psf->header.indx = 0 ;
1140 if (psf->bytewidth > 0 && psf->sf.seekable == SF_TRUE)
1141 { psf->datalength = psf->sf.frames * psf->bytewidth * psf->sf.channels ;
1142 psf->dataend = psf->dataoffset + psf->datalength ;
1145 if (psf->dataend > 0)
1146 psf_fseek (psf, psf->dataend, SEEK_SET) ;
1148 psf->dataend = psf_fseek (psf, 0, SEEK_END) ;
1150 if (psf->dataend & 1)
1151 psf_binheader_writef (psf, "z", BHWz (1)) ;
1153 /* Add a PEAK chunk if requested. */
1154 if (psf->peak_info != NULL && psf->peak_info->peak_loc == SF_PEAK_END)
1155 wavlike_write_peak_chunk (psf) ;
1157 if (psf->strings.flags & SF_STR_LOCATE_END)
1158 wavlike_write_strings (psf, SF_STR_LOCATE_END) ;
1160 /* Write the tailer. */
1161 if (psf->header.indx > 0)
1162 psf_fwrite (psf->header.ptr, psf->header.indx, 1, psf) ;
1165 } /* wav_write_tailer */
1168 wav_close (SF_PRIVATE *psf)
1170 if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
1171 { wav_write_tailer (psf) ;
1173 if (psf->file.mode == SFM_RDWR)
1174 { sf_count_t current = psf_ftell (psf) ;
1177 ** If the mode is RDWR and the current position is less than the
1178 ** filelength, truncate the file.
1181 if (current < psf->filelength)
1182 { psf_ftruncate (psf, current) ;
1183 psf->filelength = current ;
1187 psf->write_header (psf, SF_TRUE) ;
1194 wav_command (SF_PRIVATE *psf, int command, void * UNUSED (data), int datasize)
1195 { WAVLIKE_PRIVATE *wpriv ;
1197 if ((wpriv = psf->container_data) == NULL)
1198 return SFE_INTERNAL ;
1201 { case SFC_WAVEX_SET_AMBISONIC :
1202 if ((SF_CONTAINER (psf->sf.format)) == SF_FORMAT_WAVEX)
1203 { if (datasize == SF_AMBISONIC_NONE)
1204 wpriv->wavex_ambisonic = SF_AMBISONIC_NONE ;
1205 else if (datasize == SF_AMBISONIC_B_FORMAT)
1206 wpriv->wavex_ambisonic = SF_AMBISONIC_B_FORMAT ;
1210 return wpriv->wavex_ambisonic ;
1212 case SFC_WAVEX_GET_AMBISONIC :
1213 return wpriv->wavex_ambisonic ;
1215 case SFC_SET_CHANNEL_MAP_INFO :
1216 wpriv->wavex_channelmask = wavlike_gen_channel_mask (psf->channel_map, psf->sf.channels) ;
1217 return (wpriv->wavex_channelmask != 0) ;
1227 wav_read_smpl_chunk (SF_PRIVATE *psf, uint32_t chunklen)
1228 { char buffer [512] ;
1229 uint32_t thisread, bytesread = 0, dword, sampler_data, loop_count ;
1230 uint32_t note, pitch, start, end, type = -1, count ;
1233 chunklen += (chunklen & 1) ;
1235 bytesread += psf_binheader_readf (psf, "4", &dword) ;
1236 psf_log_printf (psf, " Manufacturer : %X\n", dword) ;
1238 bytesread += psf_binheader_readf (psf, "4", &dword) ;
1239 psf_log_printf (psf, " Product : %u\n", dword) ;
1241 bytesread += psf_binheader_readf (psf, "4", &dword) ;
1242 psf_log_printf (psf, " Period : %u nsec\n", dword) ;
1244 bytesread += psf_binheader_readf (psf, "4", ¬e) ;
1245 psf_log_printf (psf, " Midi Note : %u\n", note) ;
1247 bytesread += psf_binheader_readf (psf, "4", &pitch) ;
1249 { snprintf (buffer, sizeof (buffer), "%f",
1250 (1.0 * 0x80000000) / ((uint32_t) pitch)) ;
1251 psf_log_printf (psf, " Pitch Fract. : %s\n", buffer) ;
1254 psf_log_printf (psf, " Pitch Fract. : 0\n") ;
1256 bytesread += psf_binheader_readf (psf, "4", &dword) ;
1257 psf_log_printf (psf, " SMPTE Format : %u\n", dword) ;
1259 bytesread += psf_binheader_readf (psf, "4", &dword) ;
1260 snprintf (buffer, sizeof (buffer), "%02d:%02d:%02d %02d",
1261 (dword >> 24) & 0x7F, (dword >> 16) & 0x7F, (dword >> 8) & 0x7F, dword & 0x7F) ;
1262 psf_log_printf (psf, " SMPTE Offset : %s\n", buffer) ;
1264 bytesread += psf_binheader_readf (psf, "4", &loop_count) ;
1265 psf_log_printf (psf, " Loop Count : %u\n", loop_count) ;
1267 if (loop_count == 0 && chunklen == bytesread)
1270 /* Sampler Data holds the number of data bytes after the CUE chunks which
1271 ** is not actually CUE data. Display value after CUE data.
1273 bytesread += psf_binheader_readf (psf, "4", &sampler_data) ;
1275 if ((psf->instrument = psf_instrument_alloc ()) == NULL)
1276 return SFE_MALLOC_FAILED ;
1278 psf->instrument->loop_count = loop_count ;
1280 for (j = 0 ; loop_count > 0 && chunklen - bytesread >= 24 ; j ++)
1281 { if ((thisread = psf_binheader_readf (psf, "4", &dword)) == 0)
1283 bytesread += thisread ;
1284 psf_log_printf (psf, " Cue ID : %2u", dword) ;
1286 bytesread += psf_binheader_readf (psf, "4", &type) ;
1287 psf_log_printf (psf, " Type : %2u", type) ;
1289 bytesread += psf_binheader_readf (psf, "4", &start) ;
1290 psf_log_printf (psf, " Start : %5u", start) ;
1292 bytesread += psf_binheader_readf (psf, "4", &end) ;
1293 psf_log_printf (psf, " End : %5u", end) ;
1295 bytesread += psf_binheader_readf (psf, "4", &dword) ;
1296 psf_log_printf (psf, " Fraction : %5u", dword) ;
1298 bytesread += psf_binheader_readf (psf, "4", &count) ;
1299 psf_log_printf (psf, " Count : %5u\n", count) ;
1301 if (j < ARRAY_LEN (psf->instrument->loops))
1302 { psf->instrument->loops [j].start = start ;
1303 psf->instrument->loops [j].end = end + 1 ;
1304 psf->instrument->loops [j].count = count ;
1308 psf->instrument->loops [j].mode = SF_LOOP_FORWARD ;
1311 psf->instrument->loops [j].mode = SF_LOOP_ALTERNATING ;
1314 psf->instrument->loops [j].mode = SF_LOOP_BACKWARD ;
1317 psf->instrument->loops [j].mode = SF_LOOP_NONE ;
1325 if (chunklen - bytesread == 0)
1326 { if (sampler_data != 0)
1327 psf_log_printf (psf, " Sampler Data : %u (should be 0)\n", sampler_data) ;
1329 psf_log_printf (psf, " Sampler Data : %u\n", sampler_data) ;
1332 { if (sampler_data != chunklen - bytesread)
1333 { psf_log_printf (psf, " Sampler Data : %u (should have been %u)\n", sampler_data, chunklen - bytesread) ;
1334 sampler_data = chunklen - bytesread ;
1337 psf_log_printf (psf, " Sampler Data : %u\n", sampler_data) ;
1339 psf_log_printf (psf, " ") ;
1340 for (k = 0 ; k < (int) sampler_data ; k++)
1343 if (k > 0 && (k % 20) == 0)
1344 psf_log_printf (psf, "\n ") ;
1346 if ((thisread = psf_binheader_readf (psf, "1", &ch)) == 0)
1348 bytesread += thisread ;
1349 psf_log_printf (psf, "%02X ", ch & 0xFF) ;
1352 psf_log_printf (psf, "\n") ;
1355 psf->instrument->basenote = note ;
1356 psf->instrument->detune = (int8_t)(pitch / (0x40000000 / 25.0) + 0.5) ;
1357 psf->instrument->gain = 1 ;
1358 psf->instrument->velocity_lo = psf->instrument->key_lo = 0 ;
1359 psf->instrument->velocity_hi = psf->instrument->key_hi = 127 ;
1362 } /* wav_read_smpl_chunk */
1365 ** The acid chunk goes a little something like this:
1368 ** 4 bytes (int) length of chunk starting at next byte
1370 ** 4 bytes (int) type of file:
1371 ** this appears to be a bit mask,however some combinations
1372 ** are probably impossible and/or qualified as "errors"
1374 ** 0x01 On: One Shot Off: Loop
1375 ** 0x02 On: Root note is Set Off: No root
1376 ** 0x04 On: Stretch is On, Off: Strech is OFF
1377 ** 0x08 On: Disk Based Off: Ram based
1378 ** 0x10 On: ?????????? Off: ????????? (Acidizer puts that ON)
1380 ** 2 bytes (short) root note
1381 ** if type 0x10 is OFF : [C,C#,(...),B] -> [0x30 to 0x3B]
1382 ** if type 0x10 is ON : [C,C#,(...),B] -> [0x3C to 0x47]
1383 ** (both types fit on same MIDI pitch albeit different octaves, so who cares)
1385 ** 2 bytes (short) ??? always set to 0x8000
1386 ** 4 bytes (float) ??? seems to be always 0
1387 ** 4 bytes (int) number of beats
1388 ** 2 bytes (short) meter denominator //always 4 in SF/ACID
1389 ** 2 bytes (short) meter numerator //always 4 in SF/ACID
1390 ** //are we sure about the order?? usually its num/denom
1391 ** 4 bytes (float) tempo
1396 wav_read_acid_chunk (SF_PRIVATE *psf, uint32_t chunklen)
1397 { char buffer [512] ;
1398 uint32_t bytesread = 0 ;
1400 short rootnote, q1, meter_denom, meter_numer ;
1403 chunklen += (chunklen & 1) ;
1405 bytesread += psf_binheader_readf (psf, "422f", &flags, &rootnote, &q1, &q2) ;
1407 snprintf (buffer, sizeof (buffer), "%f", q2) ;
1409 psf_log_printf (psf, " Flags : 0x%04x (%s,%s,%s,%s,%s)\n", flags,
1410 (flags & 0x01) ? "OneShot" : "Loop",
1411 (flags & 0x02) ? "RootNoteValid" : "RootNoteInvalid",
1412 (flags & 0x04) ? "StretchOn" : "StretchOff",
1413 (flags & 0x08) ? "DiskBased" : "RAMBased",
1414 (flags & 0x10) ? "??On" : "??Off") ;
1416 psf_log_printf (psf, " Root note : 0x%x\n ???? : 0x%04x\n ???? : %s\n",
1417 rootnote, q1, buffer) ;
1419 bytesread += psf_binheader_readf (psf, "422f", &beats, &meter_denom, &meter_numer, &tempo) ;
1420 snprintf (buffer, sizeof (buffer), "%f", tempo) ;
1421 psf_log_printf (psf, " Beats : %d\n Meter : %d/%d\n Tempo : %s\n",
1422 beats, meter_numer, meter_denom, buffer) ;
1424 psf_binheader_readf (psf, "j", chunklen - bytesread) ;
1426 if ((psf->loop_info = calloc (1, sizeof (SF_LOOP_INFO))) == NULL)
1427 return SFE_MALLOC_FAILED ;
1429 psf->loop_info->time_sig_num = meter_numer ;
1430 psf->loop_info->time_sig_den = meter_denom ;
1431 psf->loop_info->loop_mode = (flags & 0x01) ? SF_LOOP_NONE : SF_LOOP_FORWARD ;
1432 psf->loop_info->num_beats = beats ;
1433 psf->loop_info->bpm = tempo ;
1434 psf->loop_info->root_key = (flags & 0x02) ? rootnote : -1 ;
1437 } /* wav_read_acid_chunk */
1439 /*==============================================================================
1443 wav_set_chunk (SF_PRIVATE *psf, const SF_CHUNK_INFO * chunk_info)
1444 { return psf_save_write_chunk (&psf->wchunks, chunk_info) ;
1445 } /* wav_set_chunk */
1447 static SF_CHUNK_ITERATOR *
1448 wav_next_chunk_iterator (SF_PRIVATE *psf, SF_CHUNK_ITERATOR * iterator)
1449 { return psf_next_chunk_iterator (&psf->rchunks, iterator) ;
1450 } /* wav_next_chunk_iterator */
1453 wav_get_chunk_size (SF_PRIVATE *psf, const SF_CHUNK_ITERATOR * iterator, SF_CHUNK_INFO * chunk_info)
1456 if ((indx = psf_find_read_chunk_iterator (&psf->rchunks, iterator)) < 0)
1457 return SFE_UNKNOWN_CHUNK ;
1459 chunk_info->datalen = psf->rchunks.chunks [indx].len ;
1461 return SFE_NO_ERROR ;
1462 } /* wav_get_chunk_size */
1465 wav_get_chunk_data (SF_PRIVATE *psf, const SF_CHUNK_ITERATOR * iterator, SF_CHUNK_INFO * chunk_info)
1469 if ((indx = psf_find_read_chunk_iterator (&psf->rchunks, iterator)) < 0)
1470 return SFE_UNKNOWN_CHUNK ;
1472 if (chunk_info->data == NULL)
1473 return SFE_BAD_CHUNK_DATA_PTR ;
1475 chunk_info->id_size = psf->rchunks.chunks [indx].id_size ;
1476 memcpy (chunk_info->id, psf->rchunks.chunks [indx].id, sizeof (chunk_info->id) / sizeof (*chunk_info->id)) ;
1478 pos = psf_ftell (psf) ;
1479 psf_fseek (psf, psf->rchunks.chunks [indx].offset, SEEK_SET) ;
1480 psf_fread (chunk_info->data, SF_MIN (chunk_info->datalen, psf->rchunks.chunks [indx].len), 1, psf) ;
1481 psf_fseek (psf, pos, SEEK_SET) ;
1483 return SFE_NO_ERROR ;
1484 } /* wav_get_chunk_data */