2 ** Copyright (C) 1999-2016 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 case SF_FORMAT_G721_32 :
262 error = g72x_init (psf) ;
264 /* Lite remove end */
266 case SF_FORMAT_GSM610 :
267 error = gsm610_init (psf) ;
270 default : return SFE_UNIMPLEMENTED ;
273 if (psf->file.mode == SFM_WRITE || (psf->file.mode == SFM_RDWR && psf->filelength == 0))
274 return psf->write_header (psf, SF_FALSE) ;
279 /*=========================================================================
280 ** Private functions.
284 wav_read_header (SF_PRIVATE *psf, int *blockalign, int *framesperblock)
285 { WAVLIKE_PRIVATE *wpriv ;
287 FACT_CHUNK fact_chunk ;
288 uint32_t marker, chunk_size = 0, RIFFsize = 0, done = 0 ;
289 int parsestage = 0, error, format = 0 ;
291 if (psf->is_pipe == 0 && psf->filelength > SF_PLATFORM_S64 (0xffffffff))
292 psf_log_printf (psf, "Warning : filelength > 0xffffffff. This is bad!!!!\n") ;
294 if ((wpriv = psf->container_data) == NULL)
295 return SFE_INTERNAL ;
296 wav_fmt = &wpriv->wav_fmt ;
298 /* Set position to start of file to begin reading header. */
299 psf_binheader_readf (psf, "pmj", 0, &marker, -4) ;
300 psf->header.indx = 0 ;
302 /* RIFX signifies big-endian format for all header and data to prevent
303 ** lots of code copying here, we'll set the psf->rwf_endian flag once here,
304 ** and never specify endian-ness for all other header ops/
306 psf->rwf_endian = (marker == RIFF_MARKER) ? SF_ENDIAN_LITTLE : SF_ENDIAN_BIG ;
309 { size_t jump = chunk_size & 1 ;
311 marker = chunk_size = 0 ;
312 psf_binheader_readf (psf, "jm4", jump, &marker, &chunk_size) ;
314 { sf_count_t pos = psf_ftell (psf) ;
315 psf_log_printf (psf, "Have 0 marker at position %D (0x%x).\n", pos, pos) ;
319 psf_store_read_chunk_u32 (&psf->rchunks, marker, psf_ftell (psf), chunk_size) ;
325 return SFE_WAV_NO_RIFF ;
327 parsestage |= HAVE_RIFF ;
329 RIFFsize = chunk_size ;
331 if (psf->fileoffset > 0 && psf->filelength > RIFFsize + 8)
332 { /* Set file length. */
333 psf->filelength = RIFFsize + 8 ;
334 if (marker == RIFF_MARKER)
335 psf_log_printf (psf, "RIFF : %u\n", RIFFsize) ;
337 psf_log_printf (psf, "RIFX : %u\n", RIFFsize) ;
339 else if (psf->filelength < RIFFsize + 2 * SIGNED_SIZEOF (marker))
340 { if (marker == RIFF_MARKER)
341 psf_log_printf (psf, "RIFF : %u (should be %D)\n", RIFFsize, psf->filelength - 2 * SIGNED_SIZEOF (marker)) ;
343 psf_log_printf (psf, "RIFX : %u (should be %D)\n", RIFFsize, psf->filelength - 2 * SIGNED_SIZEOF (marker)) ;
345 RIFFsize = psf->filelength - 2 * SIGNED_SIZEOF (RIFFsize) ;
348 { if (marker == RIFF_MARKER)
349 psf_log_printf (psf, "RIFF : %u\n", RIFFsize) ;
351 psf_log_printf (psf, "RIFX : %u\n", RIFFsize) ;
354 psf_binheader_readf (psf, "m", &marker) ;
355 if (marker != WAVE_MARKER)
356 return SFE_WAV_NO_WAVE ;
357 parsestage |= HAVE_WAVE ;
358 psf_log_printf (psf, "WAVE\n") ;
363 if ((parsestage & (HAVE_RIFF | HAVE_WAVE)) != (HAVE_RIFF | HAVE_WAVE))
364 return SFE_WAV_NO_FMT ;
366 /* If this file has a SECOND fmt chunk, I don't want to know about it. */
367 if (parsestage & HAVE_fmt)
370 parsestage |= HAVE_fmt ;
372 psf_log_printf (psf, "fmt : %d\n", chunk_size) ;
374 if ((error = wavlike_read_fmt_chunk (psf, chunk_size)))
377 format = wav_fmt->format ;
381 if ((parsestage & (HAVE_RIFF | HAVE_WAVE | HAVE_fmt)) != (HAVE_RIFF | HAVE_WAVE | HAVE_fmt))
382 return SFE_WAV_NO_DATA ;
384 if (psf->file.mode == SFM_RDWR && (parsestage & HAVE_other) != 0)
385 return SFE_RDWR_BAD_HEADER ;
387 parsestage |= HAVE_data ;
389 psf->datalength = chunk_size ;
390 if (psf->datalength & 1)
391 psf_log_printf (psf, "*** 'data' chunk should be an even number of bytes in length.\n") ;
393 psf->dataoffset = psf_ftell (psf) ;
395 if (psf->dataoffset > 0)
396 { if (chunk_size == 0 && RIFFsize == 8 && psf->filelength > 44)
397 { psf_log_printf (psf, "*** Looks like a WAV file which wasn't closed properly. Fixing it.\n") ;
398 psf->datalength = psf->filelength - psf->dataoffset ;
401 if (psf->datalength > psf->filelength - psf->dataoffset)
402 { psf_log_printf (psf, "data : %D (should be %D)\n", psf->datalength, psf->filelength - psf->dataoffset) ;
403 psf->datalength = psf->filelength - psf->dataoffset ;
406 psf_log_printf (psf, "data : %D\n", psf->datalength) ;
408 /* Only set dataend if there really is data at the end. */
409 if (psf->datalength + psf->dataoffset < psf->filelength)
410 psf->dataend = psf->datalength + psf->dataoffset ;
412 psf->datalength += chunk_size & 1 ;
416 if (! psf->sf.seekable || psf->dataoffset < 0)
419 /* Seek past data and continue reading header. */
420 psf_fseek (psf, psf->datalength, SEEK_CUR) ;
422 if (psf_ftell (psf) != psf->datalength + psf->dataoffset)
423 psf_log_printf (psf, "*** psf_fseek past end error ***\n") ;
427 if ((parsestage & (HAVE_RIFF | HAVE_WAVE)) != (HAVE_RIFF | HAVE_WAVE))
428 return SFE_WAV_BAD_FACT ;
430 parsestage |= HAVE_fact ;
432 if ((parsestage & HAVE_fmt) != HAVE_fmt)
433 psf_log_printf (psf, "*** Should have 'fmt ' chunk before 'fact'\n") ;
435 psf_binheader_readf (psf, "4", & (fact_chunk.frames)) ;
437 if (chunk_size > SIGNED_SIZEOF (fact_chunk))
438 psf_binheader_readf (psf, "j", (int) (chunk_size - SIGNED_SIZEOF (fact_chunk))) ;
441 psf_log_printf (psf, "%M : %u\n", marker, chunk_size) ;
443 psf_log_printf (psf, "%M : %u (should not be zero)\n", marker, chunk_size) ;
445 psf_log_printf (psf, " frames : %d\n", fact_chunk.frames) ;
449 if ((parsestage & (HAVE_RIFF | HAVE_WAVE | HAVE_fmt)) != (HAVE_RIFF | HAVE_WAVE | HAVE_fmt))
450 return SFE_WAV_PEAK_B4_FMT ;
452 parsestage |= HAVE_PEAK ;
454 psf_log_printf (psf, "%M : %u\n", marker, chunk_size) ;
455 if ((error = wavlike_read_peak_chunk (psf, chunk_size)) != 0)
457 psf->peak_info->peak_loc = ((parsestage & HAVE_data) == 0) ? SF_PEAK_START : SF_PEAK_END ;
461 parsestage |= HAVE_other ;
463 { uint32_t thisread, bytesread, cue_count, position, offset ;
464 int id, chunk_id, chunk_start, block_start, cue_index ;
466 bytesread = psf_binheader_readf (psf, "4", &cue_count) ;
467 psf_log_printf (psf, "%M : %u\n", marker, chunk_size) ;
469 if (cue_count > 1000)
470 { psf_log_printf (psf, " Count : %u (skipping)\n", cue_count) ;
471 psf_binheader_readf (psf, "j", (cue_count > 20 ? 20 : cue_count) * 24) ;
475 psf_log_printf (psf, " Count : %d\n", cue_count) ;
477 if ((psf->cues = psf_cues_alloc (cue_count)) == NULL)
478 return SFE_MALLOC_FAILED ;
484 if ((thisread = psf_binheader_readf (psf, "e44m444", &id, &position, &chunk_id, &chunk_start, &block_start, &offset)) == 0)
486 bytesread += thisread ;
488 psf_log_printf (psf, " Cue ID : %2d"
489 " Pos : %5u Chunk : %M"
490 " Chk Start : %d Blk Start : %d"
492 id, position, chunk_id, chunk_start, block_start, offset) ;
493 psf->cues->cue_points [cue_index].indx = id ;
494 psf->cues->cue_points [cue_index].position = position ;
495 psf->cues->cue_points [cue_index].fcc_chunk = chunk_id ;
496 psf->cues->cue_points [cue_index].chunk_start = chunk_start ;
497 psf->cues->cue_points [cue_index].block_start = block_start ;
498 psf->cues->cue_points [cue_index].sample_offset = offset ;
499 psf->cues->cue_points [cue_index].name [0] = '\0' ;
504 if (bytesread != chunk_size)
505 { psf_log_printf (psf, "**** Chunk size weirdness (%d != %d)\n", chunk_size, bytesread) ;
506 psf_binheader_readf (psf, "j", chunk_size - bytesread) ;
512 parsestage |= HAVE_other ;
514 psf_log_printf (psf, "smpl : %u\n", chunk_size) ;
516 if ((error = wav_read_smpl_chunk (psf, chunk_size)))
521 parsestage |= HAVE_other ;
523 psf_log_printf (psf, "acid : %u\n", chunk_size) ;
525 if ((error = wav_read_acid_chunk (psf, chunk_size)))
531 parsestage |= HAVE_other ;
533 if ((error = wavlike_subchunk_parse (psf, marker, chunk_size)) != 0)
539 The 'bext' chunk can actually be updated, so don't need to set this.
540 parsestage |= HAVE_other ;
542 if ((error = wavlike_read_bext_chunk (psf, chunk_size)))
548 We can eat into a 'PAD ' chunk if we need to.
549 parsestage |= HAVE_other ;
551 psf_log_printf (psf, "%M : %u\n", marker, chunk_size) ;
552 psf_binheader_readf (psf, "j", chunk_size) ;
556 if ((error = wavlike_read_cart_chunk (psf, chunk_size)))
560 case iXML_MARKER : /* See http://en.wikipedia.org/wiki/IXML */
561 case strc_MARKER : /* Multiple of 32 bytes. */
581 psf_log_printf (psf, "%M : %u\n", marker, chunk_size) ;
582 psf_binheader_readf (psf, "j", chunk_size) ;
586 if (chunk_size >= 0xffff0000)
588 psf_log_printf (psf, "*** Unknown chunk marker (%X) at position %D with length %u. Exiting parser.\n", marker, psf_ftell (psf) - 8, chunk_size) ;
592 if (psf_isprint ((marker >> 24) & 0xFF) && psf_isprint ((marker >> 16) & 0xFF)
593 && psf_isprint ((marker >> 8) & 0xFF) && psf_isprint (marker & 0xFF))
594 { psf_log_printf (psf, "*** %M : %u (unknown marker)\n", marker, chunk_size) ;
595 psf_binheader_readf (psf, "j", chunk_size) ;
598 if (psf_ftell (psf) & 0x03)
599 { psf_log_printf (psf, " Unknown chunk marker at position %D. Resynching.\n", psf_ftell (psf) - 8) ;
600 psf_binheader_readf (psf, "j", -3) ;
601 /* File is too messed up so we prevent editing in RDWR mode here. */
602 parsestage |= HAVE_other ;
605 psf_log_printf (psf, "*** Unknown chunk marker (%X) at position %D. Exiting parser.\n", marker, psf_ftell (psf) - 8) ;
608 } ; /* switch (marker) */
610 if (chunk_size >= psf->filelength)
611 { psf_log_printf (psf, "*** Chunk size %u > file length %D. Exiting parser.\n", chunk_size, psf->filelength) ;
615 if (! psf->sf.seekable && (parsestage & HAVE_data))
618 if (psf_ftell (psf) >= psf->filelength - SIGNED_SIZEOF (chunk_size))
619 { psf_log_printf (psf, "End\n") ;
624 if (psf->dataoffset <= 0)
625 return SFE_WAV_NO_DATA ;
627 if (psf->sf.channels < 1)
628 return SFE_CHANNEL_COUNT_ZERO ;
630 if (psf->sf.channels >= SF_MAX_CHANNELS)
631 return SFE_CHANNEL_COUNT ;
633 if (format != WAVE_FORMAT_PCM && (parsestage & HAVE_fact) == 0)
634 psf_log_printf (psf, "**** All non-PCM format files should have a 'fact' chunk.\n") ;
636 /* WAVs can be little or big endian */
637 psf->endian = psf->rwf_endian ;
639 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
641 if (psf->is_pipe == 0)
643 ** Check for 'wvpk' at the start of the DATA section. Not able to
646 psf_binheader_readf (psf, "4", &marker) ;
647 if (marker == wvpk_MARKER || marker == OggS_MARKER)
648 return SFE_WAV_WVPK_DATA ;
651 /* Seek to start of DATA section. */
652 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
655 { if (psf->filelength - psf->dataoffset < psf->datalength)
656 psf->sf.frames = (psf->filelength - psf->dataoffset) / psf->blockwidth ;
658 psf->sf.frames = psf->datalength / psf->blockwidth ;
662 { case WAVE_FORMAT_EXTENSIBLE :
663 if (psf->sf.format == (SF_FORMAT_WAVEX | SF_FORMAT_MS_ADPCM))
664 { *blockalign = wav_fmt->msadpcm.blockalign ;
665 *framesperblock = wav_fmt->msadpcm.samplesperblock ;
669 case WAVE_FORMAT_PCM :
670 psf->sf.format = SF_FORMAT_WAV | u_bitwidth_to_subformat (psf->bytewidth * 8) ;
673 case WAVE_FORMAT_MULAW :
674 case IBM_FORMAT_MULAW :
675 psf->sf.format = (SF_FORMAT_WAV | SF_FORMAT_ULAW) ;
678 case WAVE_FORMAT_ALAW :
679 case IBM_FORMAT_ALAW :
680 psf->sf.format = (SF_FORMAT_WAV | SF_FORMAT_ALAW) ;
683 case WAVE_FORMAT_MS_ADPCM :
684 psf->sf.format = (SF_FORMAT_WAV | SF_FORMAT_MS_ADPCM) ;
685 *blockalign = wav_fmt->msadpcm.blockalign ;
686 *framesperblock = wav_fmt->msadpcm.samplesperblock ;
689 case WAVE_FORMAT_IMA_ADPCM :
690 psf->sf.format = (SF_FORMAT_WAV | SF_FORMAT_IMA_ADPCM) ;
691 *blockalign = wav_fmt->ima.blockalign ;
692 *framesperblock = wav_fmt->ima.samplesperblock ;
695 case WAVE_FORMAT_GSM610 :
696 psf->sf.format = (SF_FORMAT_WAV | SF_FORMAT_GSM610) ;
699 case WAVE_FORMAT_IEEE_FLOAT :
700 psf->sf.format = SF_FORMAT_WAV ;
701 psf->sf.format |= (psf->bytewidth == 8) ? SF_FORMAT_DOUBLE : SF_FORMAT_FLOAT ;
704 case WAVE_FORMAT_G721_ADPCM :
705 psf->sf.format = SF_FORMAT_WAV | SF_FORMAT_G721_32 ;
708 default : return SFE_UNIMPLEMENTED ;
711 if (wpriv->fmt_is_broken)
712 wavlike_analyze (psf) ;
714 /* Only set the format endian-ness if its non-standard big-endian. */
715 if (psf->endian == SF_ENDIAN_BIG)
716 psf->sf.format |= SF_ENDIAN_BIG ;
719 } /* wav_read_header */
722 wav_write_fmt_chunk (SF_PRIVATE *psf)
723 { int subformat, fmt_size, add_fact_chunk = 0 ;
725 subformat = SF_CODEC (psf->sf.format) ;
728 { case SF_FORMAT_PCM_U8 :
729 case SF_FORMAT_PCM_16 :
730 case SF_FORMAT_PCM_24 :
731 case SF_FORMAT_PCM_32 :
732 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 ;
734 /* fmt : format, channels, samplerate */
735 psf_binheader_writef (psf, "4224", fmt_size, WAVE_FORMAT_PCM, psf->sf.channels, psf->sf.samplerate) ;
736 /* fmt : bytespersec */
737 psf_binheader_writef (psf, "4", psf->sf.samplerate * psf->bytewidth * psf->sf.channels) ;
738 /* fmt : blockalign, bitwidth */
739 psf_binheader_writef (psf, "22", psf->bytewidth * psf->sf.channels, psf->bytewidth * 8) ;
742 case SF_FORMAT_FLOAT :
743 case SF_FORMAT_DOUBLE :
744 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 ;
746 /* fmt : format, channels, samplerate */
747 psf_binheader_writef (psf, "4224", fmt_size, WAVE_FORMAT_IEEE_FLOAT, psf->sf.channels, psf->sf.samplerate) ;
748 /* fmt : bytespersec */
749 psf_binheader_writef (psf, "4", psf->sf.samplerate * psf->bytewidth * psf->sf.channels) ;
750 /* fmt : blockalign, bitwidth */
751 psf_binheader_writef (psf, "22", psf->bytewidth * psf->sf.channels, psf->bytewidth * 8) ;
753 add_fact_chunk = SF_TRUE ;
756 case SF_FORMAT_ULAW :
757 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 ;
759 /* fmt : format, channels, samplerate */
760 psf_binheader_writef (psf, "4224", fmt_size, WAVE_FORMAT_MULAW, psf->sf.channels, psf->sf.samplerate) ;
761 /* fmt : bytespersec */
762 psf_binheader_writef (psf, "4", psf->sf.samplerate * psf->bytewidth * psf->sf.channels) ;
763 /* fmt : blockalign, bitwidth, extrabytes */
764 psf_binheader_writef (psf, "222", psf->bytewidth * psf->sf.channels, 8, 0) ;
766 add_fact_chunk = SF_TRUE ;
769 case SF_FORMAT_ALAW :
770 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 ;
772 /* fmt : format, channels, samplerate */
773 psf_binheader_writef (psf, "4224", fmt_size, WAVE_FORMAT_ALAW, psf->sf.channels, psf->sf.samplerate) ;
774 /* fmt : bytespersec */
775 psf_binheader_writef (psf, "4", psf->sf.samplerate * psf->bytewidth * psf->sf.channels) ;
776 /* fmt : blockalign, bitwidth, extrabytes */
777 psf_binheader_writef (psf, "222", psf->bytewidth * psf->sf.channels, 8, 0) ;
779 add_fact_chunk = SF_TRUE ;
782 /* Lite remove start */
783 case SF_FORMAT_IMA_ADPCM :
784 { int blockalign, framesperblock, bytespersec ;
786 blockalign = wavlike_srate2blocksize (psf->sf.samplerate * psf->sf.channels) ;
787 framesperblock = 2 * (blockalign - 4 * psf->sf.channels) / psf->sf.channels + 1 ;
788 bytespersec = (psf->sf.samplerate * blockalign) / framesperblock ;
791 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 + 2 ;
793 /* fmt : size, WAV format type, channels, samplerate, bytespersec */
794 psf_binheader_writef (psf, "42244", fmt_size, WAVE_FORMAT_IMA_ADPCM,
795 psf->sf.channels, psf->sf.samplerate, bytespersec) ;
797 /* fmt : blockalign, bitwidth, extrabytes, framesperblock. */
798 psf_binheader_writef (psf, "2222", blockalign, 4, 2, framesperblock) ;
801 add_fact_chunk = SF_TRUE ;
804 case SF_FORMAT_MS_ADPCM :
805 { int blockalign, framesperblock, bytespersec, extrabytes ;
807 blockalign = wavlike_srate2blocksize (psf->sf.samplerate * psf->sf.channels) ;
808 framesperblock = 2 + 2 * (blockalign - 7 * psf->sf.channels) / psf->sf.channels ;
809 bytespersec = (psf->sf.samplerate * blockalign) / framesperblock ;
812 extrabytes = 2 + 2 + WAVLIKE_MSADPCM_ADAPT_COEFF_COUNT * (2 + 2) ;
813 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 + extrabytes ;
815 /* fmt : size, WAV format type, channels. */
816 psf_binheader_writef (psf, "422", fmt_size, WAVE_FORMAT_MS_ADPCM, psf->sf.channels) ;
818 /* fmt : samplerate, bytespersec. */
819 psf_binheader_writef (psf, "44", psf->sf.samplerate, bytespersec) ;
821 /* fmt : blockalign, bitwidth, extrabytes, framesperblock. */
822 psf_binheader_writef (psf, "22222", blockalign, 4, extrabytes, framesperblock, 7) ;
824 wavlike_msadpcm_write_adapt_coeffs (psf) ;
827 add_fact_chunk = SF_TRUE ;
831 case SF_FORMAT_G721_32 :
833 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 + 2 ;
835 /* fmt : size, WAV format type, channels, samplerate, bytespersec */
836 psf_binheader_writef (psf, "42244", fmt_size, WAVE_FORMAT_G721_ADPCM,
837 psf->sf.channels, psf->sf.samplerate, psf->sf.samplerate * psf->sf.channels / 2) ;
839 /* fmt : blockalign, bitwidth, extrabytes, auxblocksize. */
840 psf_binheader_writef (psf, "2222", 64, 4, 2, 0) ;
842 add_fact_chunk = SF_TRUE ;
845 /* Lite remove end */
847 case SF_FORMAT_GSM610 :
848 { int blockalign, framesperblock, bytespersec ;
850 blockalign = WAVLIKE_GSM610_BLOCKSIZE ;
851 framesperblock = WAVLIKE_GSM610_SAMPLES ;
852 bytespersec = (psf->sf.samplerate * blockalign) / framesperblock ;
855 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 + 2 ;
857 /* fmt : size, WAV format type, channels. */
858 psf_binheader_writef (psf, "422", fmt_size, WAVE_FORMAT_GSM610, psf->sf.channels) ;
860 /* fmt : samplerate, bytespersec. */
861 psf_binheader_writef (psf, "44", psf->sf.samplerate, bytespersec) ;
863 /* fmt : blockalign, bitwidth, extrabytes, framesperblock. */
864 psf_binheader_writef (psf, "2222", blockalign, 0, 2, framesperblock) ;
867 add_fact_chunk = SF_TRUE ;
870 default : return SFE_UNIMPLEMENTED ;
874 psf_binheader_writef (psf, "tm48", fact_MARKER, 4, psf->sf.frames) ;
877 } /* wav_write_fmt_chunk */
880 wavex_write_fmt_chunk (SF_PRIVATE *psf)
881 { WAVLIKE_PRIVATE *wpriv ;
882 int subformat, fmt_size ;
884 if ((wpriv = psf->container_data) == NULL)
885 return SFE_INTERNAL ;
887 subformat = SF_CODEC (psf->sf.format) ;
889 /* initial section (same for all, it appears) */
891 { case SF_FORMAT_PCM_U8 :
892 case SF_FORMAT_PCM_16 :
893 case SF_FORMAT_PCM_24 :
894 case SF_FORMAT_PCM_32 :
895 case SF_FORMAT_FLOAT :
896 case SF_FORMAT_DOUBLE :
897 case SF_FORMAT_ULAW :
898 case SF_FORMAT_ALAW :
899 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 + 2 + 4 + 4 + 2 + 2 + 8 ;
901 /* fmt : format, channels, samplerate */
902 psf_binheader_writef (psf, "4224", fmt_size, WAVE_FORMAT_EXTENSIBLE, psf->sf.channels, psf->sf.samplerate) ;
903 /* fmt : bytespersec */
904 psf_binheader_writef (psf, "4", psf->sf.samplerate * psf->bytewidth * psf->sf.channels) ;
905 /* fmt : blockalign, bitwidth */
906 psf_binheader_writef (psf, "22", psf->bytewidth * psf->sf.channels, psf->bytewidth * 8) ;
908 /* cbSize 22 is sizeof (WAVEFORMATEXTENSIBLE) - sizeof (WAVEFORMATEX) */
909 psf_binheader_writef (psf, "2", 22) ;
911 /* wValidBitsPerSample, for our use same as bitwidth as we use it fully */
912 psf_binheader_writef (psf, "2", psf->bytewidth * 8) ;
914 /* For an Ambisonic file set the channel mask to zero.
915 ** Otherwise use a default based on the channel count.
917 if (wpriv->wavex_ambisonic != SF_AMBISONIC_NONE)
918 psf_binheader_writef (psf, "4", 0) ;
919 else if (wpriv->wavex_channelmask != 0)
920 psf_binheader_writef (psf, "4", wpriv->wavex_channelmask) ;
923 ** Ok some liberty is taken here to use the most commonly used channel masks
924 ** instead of "no mapping". If you really want to use "no mapping" for 8 channels and less
925 ** please don't use wavex. (otherwise we'll have to create a new SF_COMMAND)
927 switch (psf->sf.channels)
928 { case 1 : /* center channel mono */
929 psf_binheader_writef (psf, "4", 0x4) ;
932 case 2 : /* front left and right */
933 psf_binheader_writef (psf, "4", 0x1 | 0x2) ;
937 psf_binheader_writef (psf, "4", 0x1 | 0x2 | 0x10 | 0x20) ;
941 psf_binheader_writef (psf, "4", 0x1 | 0x2 | 0x4 | 0x8 | 0x10 | 0x20) ;
945 psf_binheader_writef (psf, "4", 0x1 | 0x2 | 0x4 | 0x8 | 0x10 | 0x20 | 0x40 | 0x80) ;
948 default : /* 0 when in doubt , use direct out, ie NO mapping*/
949 psf_binheader_writef (psf, "4", 0x0) ;
955 case SF_FORMAT_MS_ADPCM : /* Todo, GUID exists might have different header as per wav_write_header */
957 return SFE_UNIMPLEMENTED ;
960 /* GUID section, different for each */
963 { case SF_FORMAT_PCM_U8 :
964 case SF_FORMAT_PCM_16 :
965 case SF_FORMAT_PCM_24 :
966 case SF_FORMAT_PCM_32 :
967 wavlike_write_guid (psf, wpriv->wavex_ambisonic == SF_AMBISONIC_NONE ?
968 &MSGUID_SUBTYPE_PCM : &MSGUID_SUBTYPE_AMBISONIC_B_FORMAT_PCM) ;
971 case SF_FORMAT_FLOAT :
972 case SF_FORMAT_DOUBLE :
973 wavlike_write_guid (psf, wpriv->wavex_ambisonic == SF_AMBISONIC_NONE ?
974 &MSGUID_SUBTYPE_IEEE_FLOAT : &MSGUID_SUBTYPE_AMBISONIC_B_FORMAT_IEEE_FLOAT) ;
977 case SF_FORMAT_ULAW :
978 wavlike_write_guid (psf, &MSGUID_SUBTYPE_MULAW) ;
981 case SF_FORMAT_ALAW :
982 wavlike_write_guid (psf, &MSGUID_SUBTYPE_ALAW) ;
986 /* This is dead code due to return in previous switch statement. */
987 case SF_FORMAT_MS_ADPCM : /* todo, GUID exists */
988 wavlike_write_guid (psf, &MSGUID_SUBTYPE_MS_ADPCM) ;
990 return SFE_UNIMPLEMENTED ;
993 default : return SFE_UNIMPLEMENTED ;
996 psf_binheader_writef (psf, "tm48", fact_MARKER, 4, psf->sf.frames) ;
999 } /* wavex_write_fmt_chunk */
1003 wav_write_header (SF_PRIVATE *psf, int calc_length)
1004 { sf_count_t current ;
1005 int error, has_data = SF_FALSE ;
1007 current = psf_ftell (psf) ;
1009 if (current > psf->dataoffset)
1010 has_data = SF_TRUE ;
1013 { psf->filelength = psf_get_filelen (psf) ;
1015 psf->datalength = psf->filelength - psf->dataoffset ;
1018 psf->datalength -= psf->filelength - psf->dataend ;
1019 else if (psf->bytewidth > 0 && psf->sf.seekable == SF_TRUE)
1020 psf->datalength = psf->sf.frames * psf->bytewidth * psf->sf.channels ;
1023 /* Reset the current header length to zero. */
1024 psf->header.ptr [0] = 0 ;
1025 psf->header.indx = 0 ;
1026 psf_fseek (psf, 0, SEEK_SET) ;
1029 ** RIFX signifies big-endian format for all header and data.
1030 ** To prevent lots of code copying here, we'll set the psf->rwf_endian flag
1031 ** once here, and never specify endian-ness for all other header operations.
1034 /* RIFF/RIFX marker, length, WAVE and 'fmt ' markers. */
1036 if (psf->endian == SF_ENDIAN_LITTLE)
1037 psf_binheader_writef (psf, "etm8", RIFF_MARKER, (psf->filelength < 8) ? 8 : psf->filelength - 8) ;
1039 psf_binheader_writef (psf, "Etm8", RIFX_MARKER, (psf->filelength < 8) ? 8 : psf->filelength - 8) ;
1041 /* WAVE and 'fmt ' markers. */
1042 psf_binheader_writef (psf, "mm", WAVE_MARKER, fmt_MARKER) ;
1044 /* Write the 'fmt ' chunk. */
1045 switch (SF_CONTAINER (psf->sf.format))
1046 { case SF_FORMAT_WAV :
1047 if ((error = wav_write_fmt_chunk (psf)) != 0)
1051 case SF_FORMAT_WAVEX :
1052 if ((error = wavex_write_fmt_chunk (psf)) != 0)
1057 return SFE_UNIMPLEMENTED ;
1060 /* The LIST/INFO chunk. */
1061 if (psf->strings.flags & SF_STR_LOCATE_START)
1062 wavlike_write_strings (psf, SF_STR_LOCATE_START) ;
1064 if (psf->peak_info != NULL && psf->peak_info->peak_loc == SF_PEAK_START)
1065 wavlike_write_peak_chunk (psf) ;
1067 if (psf->broadcast_16k != NULL)
1068 wavlike_write_bext_chunk (psf) ;
1070 if (psf->cart_16k != NULL)
1071 wavlike_write_cart_chunk (psf) ;
1073 if (psf->cues != NULL)
1076 psf_binheader_writef (psf, "em44", cue_MARKER, 4 + psf->cues->cue_count * 6 * 4, psf->cues->cue_count) ;
1078 for (k = 0 ; k < psf->cues->cue_count ; k++)
1079 psf_binheader_writef (psf, "e44m444", psf->cues->cue_points [k].indx, psf->cues->cue_points [k].position,
1080 psf->cues->cue_points [k].fcc_chunk, psf->cues->cue_points [k].chunk_start,
1081 psf->cues->cue_points [k].block_start, psf->cues->cue_points [k].sample_offset) ;
1084 if (psf->instrument != NULL)
1086 double dtune = (double) (0x40000000) / 25.0 ;
1088 psf_binheader_writef (psf, "m4", smpl_MARKER, 9 * 4 + psf->instrument->loop_count * 6 * 4) ;
1089 psf_binheader_writef (psf, "44", 0, 0) ; /* Manufacturer zero is everyone */
1090 tmp = (int) (1.0e9 / psf->sf.samplerate) ; /* Sample period in nano seconds */
1091 psf_binheader_writef (psf, "44", tmp, psf->instrument->basenote) ;
1092 tmp = (uint32_t) (psf->instrument->detune * dtune + 0.5) ;
1093 psf_binheader_writef (psf, "4", tmp) ;
1094 psf_binheader_writef (psf, "44", 0, 0) ; /* SMTPE format */
1095 psf_binheader_writef (psf, "44", psf->instrument->loop_count, 0) ;
1097 for (tmp = 0 ; tmp < psf->instrument->loop_count ; tmp++)
1100 type = psf->instrument->loops [tmp].mode ;
1101 type = (type == SF_LOOP_FORWARD ? 0 : type == SF_LOOP_BACKWARD ? 2 : type == SF_LOOP_ALTERNATING ? 1 : 32) ;
1103 psf_binheader_writef (psf, "44", tmp, type) ;
1104 psf_binheader_writef (psf, "44", psf->instrument->loops [tmp].start, psf->instrument->loops [tmp].end - 1) ;
1105 psf_binheader_writef (psf, "44", 0, psf->instrument->loops [tmp].count) ;
1109 /* Write custom headers. */
1110 if (psf->wchunks.used > 0)
1111 wavlike_write_custom_chunks (psf) ;
1113 if (psf->header.indx + 16 < psf->dataoffset)
1114 { /* Add PAD data if necessary. */
1115 size_t k = psf->dataoffset - (psf->header.indx + 16) ;
1116 psf_binheader_writef (psf, "m4z", PAD_MARKER, k, k) ;
1119 psf_binheader_writef (psf, "tm8", data_MARKER, psf->datalength) ;
1120 psf_fwrite (psf->header.ptr, psf->header.indx, 1, psf) ;
1124 if (has_data && psf->dataoffset != psf->header.indx)
1125 { psf_log_printf (psf, "Oooops : has_data && psf->dataoffset != psf->header.indx\n") ;
1126 return psf->error = SFE_INTERNAL ;
1129 psf->dataoffset = psf->header.indx ;
1132 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
1133 else if (current > 0)
1134 psf_fseek (psf, current, SEEK_SET) ;
1137 } /* wav_write_header */
1141 wav_write_tailer (SF_PRIVATE *psf)
1143 /* Reset the current header buffer length to zero. */
1144 psf->header.ptr [0] = 0 ;
1145 psf->header.indx = 0 ;
1147 if (psf->bytewidth > 0 && psf->sf.seekable == SF_TRUE)
1148 { psf->datalength = psf->sf.frames * psf->bytewidth * psf->sf.channels ;
1149 psf->dataend = psf->dataoffset + psf->datalength ;
1152 if (psf->dataend > 0)
1153 psf_fseek (psf, psf->dataend, SEEK_SET) ;
1155 psf->dataend = psf_fseek (psf, 0, SEEK_END) ;
1157 if (psf->dataend & 1)
1158 psf_binheader_writef (psf, "z", 1) ;
1160 /* Add a PEAK chunk if requested. */
1161 if (psf->peak_info != NULL && psf->peak_info->peak_loc == SF_PEAK_END)
1162 wavlike_write_peak_chunk (psf) ;
1164 if (psf->strings.flags & SF_STR_LOCATE_END)
1165 wavlike_write_strings (psf, SF_STR_LOCATE_END) ;
1167 /* Write the tailer. */
1168 if (psf->header.indx > 0)
1169 psf_fwrite (psf->header.ptr, psf->header.indx, 1, psf) ;
1172 } /* wav_write_tailer */
1175 wav_close (SF_PRIVATE *psf)
1177 if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
1178 { wav_write_tailer (psf) ;
1180 if (psf->file.mode == SFM_RDWR)
1181 { sf_count_t current = psf_ftell (psf) ;
1184 ** If the mode is RDWR and the current position is less than the
1185 ** filelength, truncate the file.
1188 if (current < psf->filelength)
1189 { psf_ftruncate (psf, current) ;
1190 psf->filelength = current ;
1194 psf->write_header (psf, SF_TRUE) ;
1201 wav_command (SF_PRIVATE *psf, int command, void * UNUSED (data), int datasize)
1202 { WAVLIKE_PRIVATE *wpriv ;
1204 if ((wpriv = psf->container_data) == NULL)
1205 return SFE_INTERNAL ;
1208 { case SFC_WAVEX_SET_AMBISONIC :
1209 if ((SF_CONTAINER (psf->sf.format)) == SF_FORMAT_WAVEX)
1210 { if (datasize == SF_AMBISONIC_NONE)
1211 wpriv->wavex_ambisonic = SF_AMBISONIC_NONE ;
1212 else if (datasize == SF_AMBISONIC_B_FORMAT)
1213 wpriv->wavex_ambisonic = SF_AMBISONIC_B_FORMAT ;
1217 return wpriv->wavex_ambisonic ;
1219 case SFC_WAVEX_GET_AMBISONIC :
1220 return wpriv->wavex_ambisonic ;
1222 case SFC_SET_CHANNEL_MAP_INFO :
1223 wpriv->wavex_channelmask = wavlike_gen_channel_mask (psf->channel_map, psf->sf.channels) ;
1224 return (wpriv->wavex_channelmask != 0) ;
1234 wav_read_smpl_chunk (SF_PRIVATE *psf, uint32_t chunklen)
1235 { char buffer [512] ;
1236 uint32_t thisread, bytesread = 0, dword, sampler_data, loop_count ;
1237 uint32_t note, start, end, type = -1, count ;
1240 chunklen += (chunklen & 1) ;
1242 bytesread += psf_binheader_readf (psf, "4", &dword) ;
1243 psf_log_printf (psf, " Manufacturer : %X\n", dword) ;
1245 bytesread += psf_binheader_readf (psf, "4", &dword) ;
1246 psf_log_printf (psf, " Product : %u\n", dword) ;
1248 bytesread += psf_binheader_readf (psf, "4", &dword) ;
1249 psf_log_printf (psf, " Period : %u nsec\n", dword) ;
1251 bytesread += psf_binheader_readf (psf, "4", ¬e) ;
1252 psf_log_printf (psf, " Midi Note : %u\n", note) ;
1254 bytesread += psf_binheader_readf (psf, "4", &dword) ;
1256 { snprintf (buffer, sizeof (buffer), "%f",
1257 (1.0 * 0x80000000) / ((uint32_t) dword)) ;
1258 psf_log_printf (psf, " Pitch Fract. : %s\n", buffer) ;
1261 psf_log_printf (psf, " Pitch Fract. : 0\n") ;
1263 bytesread += psf_binheader_readf (psf, "4", &dword) ;
1264 psf_log_printf (psf, " SMPTE Format : %u\n", dword) ;
1266 bytesread += psf_binheader_readf (psf, "4", &dword) ;
1267 snprintf (buffer, sizeof (buffer), "%02d:%02d:%02d %02d",
1268 (dword >> 24) & 0x7F, (dword >> 16) & 0x7F, (dword >> 8) & 0x7F, dword & 0x7F) ;
1269 psf_log_printf (psf, " SMPTE Offset : %s\n", buffer) ;
1271 bytesread += psf_binheader_readf (psf, "4", &loop_count) ;
1272 psf_log_printf (psf, " Loop Count : %u\n", loop_count) ;
1274 if (loop_count == 0 && chunklen == bytesread)
1277 /* Sampler Data holds the number of data bytes after the CUE chunks which
1278 ** is not actually CUE data. Display value after CUE data.
1280 bytesread += psf_binheader_readf (psf, "4", &sampler_data) ;
1282 if ((psf->instrument = psf_instrument_alloc ()) == NULL)
1283 return SFE_MALLOC_FAILED ;
1285 psf->instrument->loop_count = loop_count ;
1287 for (j = 0 ; loop_count > 0 && chunklen - bytesread >= 24 ; j ++)
1288 { if ((thisread = psf_binheader_readf (psf, "4", &dword)) == 0)
1290 bytesread += thisread ;
1291 psf_log_printf (psf, " Cue ID : %2u", dword) ;
1293 bytesread += psf_binheader_readf (psf, "4", &type) ;
1294 psf_log_printf (psf, " Type : %2u", type) ;
1296 bytesread += psf_binheader_readf (psf, "4", &start) ;
1297 psf_log_printf (psf, " Start : %5u", start) ;
1299 bytesread += psf_binheader_readf (psf, "4", &end) ;
1300 psf_log_printf (psf, " End : %5u", end) ;
1302 bytesread += psf_binheader_readf (psf, "4", &dword) ;
1303 psf_log_printf (psf, " Fraction : %5u", dword) ;
1305 bytesread += psf_binheader_readf (psf, "4", &count) ;
1306 psf_log_printf (psf, " Count : %5u\n", count) ;
1308 if (j < ARRAY_LEN (psf->instrument->loops))
1309 { psf->instrument->loops [j].start = start ;
1310 psf->instrument->loops [j].end = end + 1 ;
1311 psf->instrument->loops [j].count = count ;
1315 psf->instrument->loops [j].mode = SF_LOOP_FORWARD ;
1318 psf->instrument->loops [j].mode = SF_LOOP_ALTERNATING ;
1321 psf->instrument->loops [j].mode = SF_LOOP_BACKWARD ;
1324 psf->instrument->loops [j].mode = SF_LOOP_NONE ;
1332 if (chunklen - bytesread == 0)
1333 { if (sampler_data != 0)
1334 psf_log_printf (psf, " Sampler Data : %u (should be 0)\n", sampler_data) ;
1336 psf_log_printf (psf, " Sampler Data : %u\n", sampler_data) ;
1339 { if (sampler_data != chunklen - bytesread)
1340 { psf_log_printf (psf, " Sampler Data : %u (should have been %u)\n", sampler_data, chunklen - bytesread) ;
1341 sampler_data = chunklen - bytesread ;
1344 psf_log_printf (psf, " Sampler Data : %u\n", sampler_data) ;
1346 psf_log_printf (psf, " ") ;
1347 for (k = 0 ; k < (int) sampler_data ; k++)
1350 if (k > 0 && (k % 20) == 0)
1351 psf_log_printf (psf, "\n ") ;
1353 if ((thisread = psf_binheader_readf (psf, "1", &ch)) == 0)
1355 bytesread += thisread ;
1356 psf_log_printf (psf, "%02X ", ch & 0xFF) ;
1359 psf_log_printf (psf, "\n") ;
1362 psf->instrument->basenote = note ;
1363 psf->instrument->gain = 1 ;
1364 psf->instrument->velocity_lo = psf->instrument->key_lo = 0 ;
1365 psf->instrument->velocity_hi = psf->instrument->key_hi = 127 ;
1368 } /* wav_read_smpl_chunk */
1371 ** The acid chunk goes a little something like this:
1374 ** 4 bytes (int) length of chunk starting at next byte
1376 ** 4 bytes (int) type of file:
1377 ** this appears to be a bit mask,however some combinations
1378 ** are probably impossible and/or qualified as "errors"
1380 ** 0x01 On: One Shot Off: Loop
1381 ** 0x02 On: Root note is Set Off: No root
1382 ** 0x04 On: Stretch is On, Off: Strech is OFF
1383 ** 0x08 On: Disk Based Off: Ram based
1384 ** 0x10 On: ?????????? Off: ????????? (Acidizer puts that ON)
1386 ** 2 bytes (short) root note
1387 ** if type 0x10 is OFF : [C,C#,(...),B] -> [0x30 to 0x3B]
1388 ** if type 0x10 is ON : [C,C#,(...),B] -> [0x3C to 0x47]
1389 ** (both types fit on same MIDI pitch albeit different octaves, so who cares)
1391 ** 2 bytes (short) ??? always set to 0x8000
1392 ** 4 bytes (float) ??? seems to be always 0
1393 ** 4 bytes (int) number of beats
1394 ** 2 bytes (short) meter denominator //always 4 in SF/ACID
1395 ** 2 bytes (short) meter numerator //always 4 in SF/ACID
1396 ** //are we sure about the order?? usually its num/denom
1397 ** 4 bytes (float) tempo
1402 wav_read_acid_chunk (SF_PRIVATE *psf, uint32_t chunklen)
1403 { char buffer [512] ;
1404 uint32_t bytesread = 0 ;
1406 short rootnote, q1, meter_denom, meter_numer ;
1409 chunklen += (chunklen & 1) ;
1411 bytesread += psf_binheader_readf (psf, "422f", &flags, &rootnote, &q1, &q2) ;
1413 snprintf (buffer, sizeof (buffer), "%f", q2) ;
1415 psf_log_printf (psf, " Flags : 0x%04x (%s,%s,%s,%s,%s)\n", flags,
1416 (flags & 0x01) ? "OneShot" : "Loop",
1417 (flags & 0x02) ? "RootNoteValid" : "RootNoteInvalid",
1418 (flags & 0x04) ? "StretchOn" : "StretchOff",
1419 (flags & 0x08) ? "DiskBased" : "RAMBased",
1420 (flags & 0x10) ? "??On" : "??Off") ;
1422 psf_log_printf (psf, " Root note : 0x%x\n ???? : 0x%04x\n ???? : %s\n",
1423 rootnote, q1, buffer) ;
1425 bytesread += psf_binheader_readf (psf, "422f", &beats, &meter_denom, &meter_numer, &tempo) ;
1426 snprintf (buffer, sizeof (buffer), "%f", tempo) ;
1427 psf_log_printf (psf, " Beats : %d\n Meter : %d/%d\n Tempo : %s\n",
1428 beats, meter_numer, meter_denom, buffer) ;
1430 psf_binheader_readf (psf, "j", chunklen - bytesread) ;
1432 if ((psf->loop_info = calloc (1, sizeof (SF_LOOP_INFO))) == NULL)
1433 return SFE_MALLOC_FAILED ;
1435 psf->loop_info->time_sig_num = meter_numer ;
1436 psf->loop_info->time_sig_den = meter_denom ;
1437 psf->loop_info->loop_mode = (flags & 0x01) ? SF_LOOP_NONE : SF_LOOP_FORWARD ;
1438 psf->loop_info->num_beats = beats ;
1439 psf->loop_info->bpm = tempo ;
1440 psf->loop_info->root_key = (flags & 0x02) ? rootnote : -1 ;
1443 } /* wav_read_acid_chunk */
1445 /*==============================================================================
1449 wav_set_chunk (SF_PRIVATE *psf, const SF_CHUNK_INFO * chunk_info)
1450 { return psf_save_write_chunk (&psf->wchunks, chunk_info) ;
1451 } /* wav_set_chunk */
1453 static SF_CHUNK_ITERATOR *
1454 wav_next_chunk_iterator (SF_PRIVATE *psf, SF_CHUNK_ITERATOR * iterator)
1455 { return psf_next_chunk_iterator (&psf->rchunks, iterator) ;
1456 } /* wav_next_chunk_iterator */
1459 wav_get_chunk_size (SF_PRIVATE *psf, const SF_CHUNK_ITERATOR * iterator, SF_CHUNK_INFO * chunk_info)
1462 if ((indx = psf_find_read_chunk_iterator (&psf->rchunks, iterator)) < 0)
1463 return SFE_UNKNOWN_CHUNK ;
1465 chunk_info->datalen = psf->rchunks.chunks [indx].len ;
1467 return SFE_NO_ERROR ;
1468 } /* wav_get_chunk_size */
1471 wav_get_chunk_data (SF_PRIVATE *psf, const SF_CHUNK_ITERATOR * iterator, SF_CHUNK_INFO * chunk_info)
1475 if ((indx = psf_find_read_chunk_iterator (&psf->rchunks, iterator)) < 0)
1476 return SFE_UNKNOWN_CHUNK ;
1478 if (chunk_info->data == NULL)
1479 return SFE_BAD_CHUNK_DATA_PTR ;
1481 chunk_info->id_size = psf->rchunks.chunks [indx].id_size ;
1482 memcpy (chunk_info->id, psf->rchunks.chunks [indx].id, sizeof (chunk_info->id) / sizeof (*chunk_info->id)) ;
1484 pos = psf_ftell (psf) ;
1485 psf_fseek (psf, psf->rchunks.chunks [indx].offset, SEEK_SET) ;
1486 psf_fread (chunk_info->data, SF_MIN (chunk_info->datalen, psf->rchunks.chunks [indx].len), 1, psf) ;
1487 psf_fseek (psf, pos, SEEK_SET) ;
1489 return SFE_NO_ERROR ;
1490 } /* wav_get_chunk_data */