2 ** Copyright (C) 1999-2015 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 data_MARKER (MAKE_MARKER ('d', 'a', 't', 'a'))
43 #define fact_MARKER (MAKE_MARKER ('f', 'a', 'c', 't'))
44 #define PEAK_MARKER (MAKE_MARKER ('P', 'E', 'A', 'K'))
46 #define cue_MARKER (MAKE_MARKER ('c', 'u', 'e', ' '))
47 #define LIST_MARKER (MAKE_MARKER ('L', 'I', 'S', 'T'))
48 #define slnt_MARKER (MAKE_MARKER ('s', 'l', 'n', 't'))
49 #define wavl_MARKER (MAKE_MARKER ('w', 'a', 'v', 'l'))
50 #define INFO_MARKER (MAKE_MARKER ('I', 'N', 'F', 'O'))
51 #define plst_MARKER (MAKE_MARKER ('p', 'l', 's', 't'))
52 #define adtl_MARKER (MAKE_MARKER ('a', 'd', 't', 'l'))
53 #define labl_MARKER (MAKE_MARKER ('l', 'a', 'b', 'l'))
54 #define ltxt_MARKER (MAKE_MARKER ('l', 't', 'x', 't'))
55 #define note_MARKER (MAKE_MARKER ('n', 'o', 't', 'e'))
56 #define smpl_MARKER (MAKE_MARKER ('s', 'm', 'p', 'l'))
57 #define bext_MARKER (MAKE_MARKER ('b', 'e', 'x', 't'))
58 #define iXML_MARKER (MAKE_MARKER ('i', 'X', 'M', 'L'))
59 #define levl_MARKER (MAKE_MARKER ('l', 'e', 'v', 'l'))
60 #define MEXT_MARKER (MAKE_MARKER ('M', 'E', 'X', 'T'))
61 #define DISP_MARKER (MAKE_MARKER ('D', 'I', 'S', 'P'))
62 #define acid_MARKER (MAKE_MARKER ('a', 'c', 'i', 'd'))
63 #define strc_MARKER (MAKE_MARKER ('s', 't', 'r', 'c'))
64 #define PAD_MARKER (MAKE_MARKER ('P', 'A', 'D', ' '))
65 #define afsp_MARKER (MAKE_MARKER ('a', 'f', 's', 'p'))
66 #define clm_MARKER (MAKE_MARKER ('c', 'l', 'm', ' '))
67 #define elmo_MARKER (MAKE_MARKER ('e', 'l', 'm', 'o'))
68 #define cart_MARKER (MAKE_MARKER ('c', 'a', 'r', 't'))
69 #define FLLR_MARKER (MAKE_MARKER ('F', 'L', 'L', 'R'))
71 #define exif_MARKER (MAKE_MARKER ('e', 'x', 'i', 'f'))
72 #define ever_MARKER (MAKE_MARKER ('e', 'v', 'e', 'r'))
73 #define etim_MARKER (MAKE_MARKER ('e', 't', 'i', 'm'))
74 #define ecor_MARKER (MAKE_MARKER ('e', 'c', 'o', 'r'))
75 #define emdl_MARKER (MAKE_MARKER ('e', 'm', 'd', 'l'))
76 #define emnt_MARKER (MAKE_MARKER ('e', 'm', 'n', 't'))
77 #define erel_MARKER (MAKE_MARKER ('e', 'r', 'e', 'l'))
78 #define eucm_MARKER (MAKE_MARKER ('e', 'u', 'c', 'm'))
79 #define olym_MARKER (MAKE_MARKER ('o', 'l', 'y', 'm'))
80 #define minf_MARKER (MAKE_MARKER ('m', 'i', 'n', 'f'))
81 #define elm1_MARKER (MAKE_MARKER ('e', 'l', 'm', '1'))
82 #define regn_MARKER (MAKE_MARKER ('r', 'e', 'g', 'n'))
83 #define ovwf_MARKER (MAKE_MARKER ('o', 'v', 'w', 'f'))
84 #define umid_MARKER (MAKE_MARKER ('u', 'm', 'i', 'd'))
85 #define SyLp_MARKER (MAKE_MARKER ('S', 'y', 'L', 'p'))
86 #define Cr8r_MARKER (MAKE_MARKER ('C', 'r', '8', 'r'))
87 #define JUNK_MARKER (MAKE_MARKER ('J', 'U', 'N', 'K'))
88 #define PMX_MARKER (MAKE_MARKER ('_', 'P', 'M', 'X'))
89 #define inst_MARKER (MAKE_MARKER ('i', 'n', 's', 't'))
90 #define AFAn_MARKER (MAKE_MARKER ('A', 'F', 'A', 'n'))
93 #define ISFT_MARKER (MAKE_MARKER ('I', 'S', 'F', 'T'))
94 #define ICRD_MARKER (MAKE_MARKER ('I', 'C', 'R', 'D'))
95 #define ICOP_MARKER (MAKE_MARKER ('I', 'C', 'O', 'P'))
96 #define IARL_MARKER (MAKE_MARKER ('I', 'A', 'R', 'L'))
97 #define IART_MARKER (MAKE_MARKER ('I', 'A', 'R', 'T'))
98 #define INAM_MARKER (MAKE_MARKER ('I', 'N', 'A', 'M'))
99 #define IENG_MARKER (MAKE_MARKER ('I', 'E', 'N', 'G'))
100 #define IGNR_MARKER (MAKE_MARKER ('I', 'G', 'N', 'R'))
101 #define ICOP_MARKER (MAKE_MARKER ('I', 'C', 'O', 'P'))
102 #define IPRD_MARKER (MAKE_MARKER ('I', 'P', 'R', 'D'))
103 #define ISRC_MARKER (MAKE_MARKER ('I', 'S', 'R', 'C'))
104 #define ISBJ_MARKER (MAKE_MARKER ('I', 'S', 'B', 'J'))
105 #define ICMT_MARKER (MAKE_MARKER ('I', 'C', 'M', 'T'))
106 #define IAUT_MARKER (MAKE_MARKER ('I', 'A', 'U', 'T'))
107 #define ITRK_MARKER (MAKE_MARKER ('I', 'T', 'R', 'K'))
109 /* Weird WAVPACK marker which can show up at the start of the DATA section. */
110 #define wvpk_MARKER (MAKE_MARKER ('w', 'v', 'p', 'k'))
111 #define OggS_MARKER (MAKE_MARKER ('O', 'g', 'g', 'S'))
113 #define WAV_PEAK_CHUNK_SIZE(ch) (2 * sizeof (int) + ch * (sizeof (float) + sizeof (int)))
114 #define WAV_BEXT_MIN_CHUNK_SIZE 602
115 #define WAV_BEXT_MAX_CHUNK_SIZE (10 * 1024)
117 #define WAV_CART_MIN_CHUNK_SIZE 2048
118 #define WAV_CART_MAX_CHUNK_SIZE 0xffffffff
128 HAVE_other = 0x80000000
133 /* known WAVEFORMATEXTENSIBLE GUIDS */
134 static const EXT_SUBFORMAT MSGUID_SUBTYPE_PCM =
135 { 0x00000001, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }
138 static const EXT_SUBFORMAT MSGUID_SUBTYPE_MS_ADPCM =
139 { 0x00000002, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }
142 static const EXT_SUBFORMAT MSGUID_SUBTYPE_IEEE_FLOAT =
143 { 0x00000003, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }
146 static const EXT_SUBFORMAT MSGUID_SUBTYPE_ALAW =
147 { 0x00000006, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }
150 static const EXT_SUBFORMAT MSGUID_SUBTYPE_MULAW =
151 { 0x00000007, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }
155 ** the next two are from
156 ** http://dream.cs.bath.ac.uk/researchdev/wave-ex/bformat.html
158 static const EXT_SUBFORMAT MSGUID_SUBTYPE_AMBISONIC_B_FORMAT_PCM =
159 { 0x00000001, 0x0721, 0x11d3, { 0x86, 0x44, 0xC8, 0xC1, 0xCA, 0x00, 0x00, 0x00 }
162 static const EXT_SUBFORMAT MSGUID_SUBTYPE_AMBISONIC_B_FORMAT_IEEE_FLOAT =
163 { 0x00000003, 0x0721, 0x11d3, { 0x86, 0x44, 0xC8, 0xC1, 0xCA, 0x00, 0x00, 0x00 }
168 /* maybe interesting one day to read the following through sf_read_raw */
169 /* http://www.bath.ac.uk/~masrwd/pvocex/pvocex.html */
170 static const EXT_SUBFORMAT MSGUID_SUBTYPE_PVOCEX =
171 { 0x8312B9C2, 0x2E6E, 0x11d4, { 0xA8, 0x24, 0xDE, 0x5B, 0x96, 0xC3, 0xAB, 0x21 }
175 /*------------------------------------------------------------------------------
176 ** Private static functions.
179 static int wav_read_header (SF_PRIVATE *psf, int *blockalign, int *framesperblock) ;
180 static int wav_write_header (SF_PRIVATE *psf, int calc_length) ;
182 static int wav_write_tailer (SF_PRIVATE *psf) ;
183 static void wav_write_strings (SF_PRIVATE *psf, int location) ;
184 static int wav_command (SF_PRIVATE *psf, int command, void *data, int datasize) ;
185 static int wav_close (SF_PRIVATE *psf) ;
187 static int wav_subchunk_parse (SF_PRIVATE *psf, int chunk, uint32_t length) ;
188 static int exif_subchunk_parse (SF_PRIVATE *psf, uint32_t length) ;
189 static int wav_read_smpl_chunk (SF_PRIVATE *psf, uint32_t chunklen) ;
190 static int wav_read_acid_chunk (SF_PRIVATE *psf, uint32_t chunklen) ;
192 static int wav_set_chunk (SF_PRIVATE *psf, const SF_CHUNK_INFO * chunk_info) ;
193 static SF_CHUNK_ITERATOR * wav_next_chunk_iterator (SF_PRIVATE *psf, SF_CHUNK_ITERATOR * iterator) ;
194 static int wav_get_chunk_size (SF_PRIVATE *psf, const SF_CHUNK_ITERATOR * iterator, SF_CHUNK_INFO * chunk_info) ;
195 static int wav_get_chunk_data (SF_PRIVATE *psf, const SF_CHUNK_ITERATOR * iterator, SF_CHUNK_INFO * chunk_info) ;
197 /*------------------------------------------------------------------------------
202 wav_open (SF_PRIVATE *psf)
203 { WAV_PRIVATE * wpriv ;
204 int format, subformat, error, blockalign = 0, framesperblock = 0 ;
206 if ((wpriv = calloc (1, sizeof (WAV_PRIVATE))) == NULL)
207 return SFE_MALLOC_FAILED ;
208 psf->container_data = wpriv ;
210 wpriv->wavex_ambisonic = SF_AMBISONIC_NONE ;
211 psf->strings.flags = SF_STR_ALLOW_START | SF_STR_ALLOW_END ;
213 if (psf->file.mode == SFM_READ || (psf->file.mode == SFM_RDWR && psf->filelength > 0))
214 { if ((error = wav_read_header (psf, &blockalign, &framesperblock)))
217 psf->next_chunk_iterator = wav_next_chunk_iterator ;
218 psf->get_chunk_size = wav_get_chunk_size ;
219 psf->get_chunk_data = wav_get_chunk_data ;
222 subformat = SF_CODEC (psf->sf.format) ;
224 if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
226 return SFE_NO_PIPE_WRITE ;
228 wpriv->wavex_ambisonic = SF_AMBISONIC_NONE ;
230 format = SF_CONTAINER (psf->sf.format) ;
231 if (format != SF_FORMAT_WAV && format != SF_FORMAT_WAVEX)
232 return SFE_BAD_OPEN_FORMAT ;
234 psf->blockwidth = psf->bytewidth * psf->sf.channels ;
236 /* RIFF WAVs are little-endian, RIFX WAVs are big-endian, default to little */
237 psf->endian = SF_ENDIAN (psf->sf.format) ;
238 if (CPU_IS_BIG_ENDIAN && psf->endian == SF_ENDIAN_CPU)
239 psf->endian = SF_ENDIAN_BIG ;
240 else if (psf->endian != SF_ENDIAN_BIG)
241 psf->endian = SF_ENDIAN_LITTLE ;
243 if (psf->file.mode != SFM_RDWR || psf->filelength < 44)
244 { psf->filelength = 0 ;
245 psf->datalength = 0 ;
246 psf->dataoffset = 0 ;
250 if (subformat == SF_FORMAT_IMA_ADPCM || subformat == SF_FORMAT_MS_ADPCM)
251 { blockalign = wav_w64_srate2blocksize (psf->sf.samplerate * psf->sf.channels) ;
252 framesperblock = -1 ; /* Corrected later. */
255 /* By default, add the peak chunk to floating point files. Default behaviour
256 ** can be switched off using sf_command (SFC_SET_PEAK_CHUNK, SF_FALSE).
258 if (psf->file.mode == SFM_WRITE && (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE))
259 { if ((psf->peak_info = peak_info_calloc (psf->sf.channels)) == NULL)
260 return SFE_MALLOC_FAILED ;
261 psf->peak_info->peak_loc = SF_PEAK_START ;
264 psf->write_header = wav_write_header ;
265 psf->set_chunk = wav_set_chunk ;
268 psf->container_close = wav_close ;
269 psf->command = wav_command ;
272 { case SF_FORMAT_PCM_U8 :
273 case SF_FORMAT_PCM_16 :
274 case SF_FORMAT_PCM_24 :
275 case SF_FORMAT_PCM_32 :
276 error = pcm_init (psf) ;
279 case SF_FORMAT_ULAW :
280 error = ulaw_init (psf) ;
283 case SF_FORMAT_ALAW :
284 error = alaw_init (psf) ;
287 /* Lite remove start */
288 case SF_FORMAT_FLOAT :
289 error = float32_init (psf) ;
292 case SF_FORMAT_DOUBLE :
293 error = double64_init (psf) ;
296 case SF_FORMAT_IMA_ADPCM :
297 error = wav_w64_ima_init (psf, blockalign, framesperblock) ;
300 case SF_FORMAT_MS_ADPCM :
301 error = wav_w64_msadpcm_init (psf, blockalign, framesperblock) ;
304 case SF_FORMAT_G721_32 :
305 error = g72x_init (psf) ;
307 /* Lite remove end */
309 case SF_FORMAT_GSM610 :
310 error = gsm610_init (psf) ;
313 default : return SFE_UNIMPLEMENTED ;
316 if (psf->file.mode == SFM_WRITE || (psf->file.mode == SFM_RDWR && psf->filelength == 0))
317 return psf->write_header (psf, SF_FALSE) ;
322 /*=========================================================================
323 ** Private functions.
327 wav_read_header (SF_PRIVATE *psf, int *blockalign, int *framesperblock)
328 { WAV_PRIVATE *wpriv ;
330 FACT_CHUNK fact_chunk ;
331 uint32_t marker, chunk_size = 0, RIFFsize = 0, done = 0, uk ;
332 int parsestage = 0, error, format = 0 ;
335 if (psf->is_pipe == 0 && psf->filelength > SF_PLATFORM_S64 (0xffffffff))
336 psf_log_printf (psf, "Warning : filelength > 0xffffffff. This is bad!!!!\n") ;
338 if ((wpriv = psf->container_data) == NULL)
339 return SFE_INTERNAL ;
340 wav_fmt = &wpriv->wav_fmt ;
342 /* Set position to start of file to begin reading header. */
343 psf_binheader_readf (psf, "pmj", 0, &marker, -4) ;
346 /* RIFX signifies big-endian format for all header and data to prevent
347 ** lots of code copying here, we'll set the psf->rwf_endian flag once here,
348 ** and never specify endian-ness for all other header ops/
350 psf->rwf_endian = (marker == RIFF_MARKER) ? SF_ENDIAN_LITTLE : SF_ENDIAN_BIG ;
353 { size_t jump = chunk_size & 1 ;
355 marker = chunk_size = 0 ;
356 psf_binheader_readf (psf, "jm4", jump, &marker, &chunk_size) ;
358 { sf_count_t pos = psf_ftell (psf) ;
359 psf_log_printf (psf, "Have 0 marker at position %D (0x%x).\n", pos, pos) ;
363 psf_store_read_chunk_u32 (&psf->rchunks, marker, psf_ftell (psf), chunk_size) ;
369 return SFE_WAV_NO_RIFF ;
371 parsestage |= HAVE_RIFF ;
373 RIFFsize = chunk_size ;
375 if (psf->fileoffset > 0 && psf->filelength > RIFFsize + 8)
376 { /* Set file length. */
377 psf->filelength = RIFFsize + 8 ;
378 if (marker == RIFF_MARKER)
379 psf_log_printf (psf, "RIFF : %u\n", RIFFsize) ;
381 psf_log_printf (psf, "RIFX : %u\n", RIFFsize) ;
383 else if (psf->filelength < RIFFsize + 2 * SIGNED_SIZEOF (marker))
384 { if (marker == RIFF_MARKER)
385 psf_log_printf (psf, "RIFF : %u (should be %D)\n", RIFFsize, psf->filelength - 2 * SIGNED_SIZEOF (marker)) ;
387 psf_log_printf (psf, "RIFX : %u (should be %D)\n", RIFFsize, psf->filelength - 2 * SIGNED_SIZEOF (marker)) ;
389 RIFFsize = psf->filelength - 2 * SIGNED_SIZEOF (RIFFsize) ;
392 { if (marker == RIFF_MARKER)
393 psf_log_printf (psf, "RIFF : %u\n", RIFFsize) ;
395 psf_log_printf (psf, "RIFX : %u\n", RIFFsize) ;
398 psf_binheader_readf (psf, "m", &marker) ;
399 if (marker != WAVE_MARKER)
400 return SFE_WAV_NO_WAVE ;
401 parsestage |= HAVE_WAVE ;
402 psf_log_printf (psf, "WAVE\n") ;
407 if ((parsestage & (HAVE_RIFF | HAVE_WAVE)) != (HAVE_RIFF | HAVE_WAVE))
408 return SFE_WAV_NO_FMT ;
410 /* If this file has a SECOND fmt chunk, I don't want to know about it. */
411 if (parsestage & HAVE_fmt)
414 parsestage |= HAVE_fmt ;
416 psf_log_printf (psf, "fmt : %d\n", chunk_size) ;
418 if ((error = wav_w64_read_fmt_chunk (psf, chunk_size)))
421 format = wav_fmt->format ;
425 if ((parsestage & (HAVE_RIFF | HAVE_WAVE | HAVE_fmt)) != (HAVE_RIFF | HAVE_WAVE | HAVE_fmt))
426 return SFE_WAV_NO_DATA ;
428 if (psf->file.mode == SFM_RDWR && (parsestage & HAVE_other) != 0)
429 return SFE_RDWR_BAD_HEADER ;
431 parsestage |= HAVE_data ;
433 psf->datalength = chunk_size ;
434 if (psf->datalength & 1)
435 psf_log_printf (psf, "*** 'data' chunk should be an even number of bytes in length.\n") ;
437 psf->dataoffset = psf_ftell (psf) ;
439 if (psf->dataoffset > 0)
440 { if (chunk_size == 0 && RIFFsize == 8 && psf->filelength > 44)
441 { psf_log_printf (psf, "*** Looks like a WAV file which wasn't closed properly. Fixing it.\n") ;
442 psf->datalength = psf->filelength - psf->dataoffset ;
445 if (psf->datalength > psf->filelength - psf->dataoffset)
446 { psf_log_printf (psf, "data : %D (should be %D)\n", psf->datalength, psf->filelength - psf->dataoffset) ;
447 psf->datalength = psf->filelength - psf->dataoffset ;
450 psf_log_printf (psf, "data : %D\n", psf->datalength) ;
452 /* Only set dataend if there really is data at the end. */
453 if (psf->datalength + psf->dataoffset < psf->filelength)
454 psf->dataend = psf->datalength + psf->dataoffset ;
456 psf->datalength += chunk_size & 1 ;
460 if (! psf->sf.seekable || psf->dataoffset < 0)
463 /* Seek past data and continue reading header. */
464 psf_fseek (psf, psf->datalength, SEEK_CUR) ;
466 if (psf_ftell (psf) != psf->datalength + psf->dataoffset)
467 psf_log_printf (psf, "*** psf_fseek past end error ***\n") ;
471 if ((parsestage & (HAVE_RIFF | HAVE_WAVE)) != (HAVE_RIFF | HAVE_WAVE))
472 return SFE_WAV_BAD_FACT ;
474 parsestage |= HAVE_fact ;
476 if ((parsestage & HAVE_fmt) != HAVE_fmt)
477 psf_log_printf (psf, "*** Should have 'fmt ' chunk before 'fact'\n") ;
479 psf_binheader_readf (psf, "4", & (fact_chunk.frames)) ;
481 if (chunk_size > SIGNED_SIZEOF (fact_chunk))
482 psf_binheader_readf (psf, "j", (int) (chunk_size - SIGNED_SIZEOF (fact_chunk))) ;
485 psf_log_printf (psf, "%M : %u\n", marker, chunk_size) ;
487 psf_log_printf (psf, "%M : %u (should not be zero)\n", marker, chunk_size) ;
489 psf_log_printf (psf, " frames : %d\n", fact_chunk.frames) ;
493 if ((parsestage & (HAVE_RIFF | HAVE_WAVE | HAVE_fmt)) != (HAVE_RIFF | HAVE_WAVE | HAVE_fmt))
494 return SFE_WAV_PEAK_B4_FMT ;
496 parsestage |= HAVE_PEAK ;
498 psf_log_printf (psf, "%M : %u\n", marker, chunk_size) ;
499 if (chunk_size != WAV_PEAK_CHUNK_SIZE (psf->sf.channels))
500 { psf_binheader_readf (psf, "j", chunk_size) ;
501 psf_log_printf (psf, "*** File PEAK chunk size doesn't fit with number of channels (%d).\n", psf->sf.channels) ;
502 return SFE_WAV_BAD_PEAK ;
505 if ((psf->peak_info = peak_info_calloc (psf->sf.channels)) == NULL)
506 return SFE_MALLOC_FAILED ;
508 /* read in rest of PEAK chunk. */
509 psf_binheader_readf (psf, "44", & (psf->peak_info->version), & (psf->peak_info->timestamp)) ;
511 if (psf->peak_info->version != 1)
512 psf_log_printf (psf, " version : %d *** (should be version 1)\n", psf->peak_info->version) ;
514 psf_log_printf (psf, " version : %d\n", psf->peak_info->version) ;
516 psf_log_printf (psf, " time stamp : %d\n", psf->peak_info->timestamp) ;
517 psf_log_printf (psf, " Ch Position Value\n") ;
519 for (uk = 0 ; uk < (uint32_t) psf->sf.channels ; uk++)
523 psf_binheader_readf (psf, "f4", &value, &position) ;
524 psf->peak_info->peaks [uk].value = value ;
525 psf->peak_info->peaks [uk].position = position ;
527 snprintf (buffer, sizeof (buffer), " %2d %-12" PRId64 " %g\n",
528 uk, psf->peak_info->peaks [uk].position, psf->peak_info->peaks [uk].value) ;
529 buffer [sizeof (buffer) - 1] = 0 ;
530 psf_log_printf (psf, "%s", buffer) ;
533 psf->peak_info->peak_loc = ((parsestage & HAVE_data) == 0) ? SF_PEAK_START : SF_PEAK_END ;
537 parsestage |= HAVE_other ;
539 { uint32_t thisread, bytesread, cue_count ;
540 int id, position, chunk_id, chunk_start, block_start, offset ;
542 bytesread = psf_binheader_readf (psf, "4", &cue_count) ;
543 psf_log_printf (psf, "%M : %u\n", marker, chunk_size) ;
546 { psf_log_printf (psf, " Count : %u (skipping)\n", cue_count) ;
547 psf_binheader_readf (psf, "j", (cue_count > 20 ? 20 : cue_count) * 24) ;
551 psf_log_printf (psf, " Count : %d\n", cue_count) ;
555 if ((thisread = psf_binheader_readf (psf, "444444", &id, &position, &chunk_id, &chunk_start, &block_start, &offset)) == 0)
557 bytesread += thisread ;
559 psf_log_printf (psf, " Cue ID : %2d"
560 " Pos : %5u Chunk : %M"
561 " Chk Start : %d Blk Start : %d"
563 id, position, chunk_id, chunk_start, block_start, offset) ;
567 if (bytesread != chunk_size)
568 { psf_log_printf (psf, "**** Chunk size weirdness (%d != %d)\n", chunk_size, bytesread) ;
569 psf_binheader_readf (psf, "j", chunk_size - bytesread) ;
575 parsestage |= HAVE_other ;
577 psf_log_printf (psf, "smpl : %u\n", chunk_size) ;
579 if ((error = wav_read_smpl_chunk (psf, chunk_size)))
584 parsestage |= HAVE_other ;
586 psf_log_printf (psf, "acid : %u\n", chunk_size) ;
588 if ((error = wav_read_acid_chunk (psf, chunk_size)))
594 parsestage |= HAVE_other ;
596 if ((error = wav_subchunk_parse (psf, marker, chunk_size)) != 0)
602 The 'bext' chunk can actually be updated, so don't need to set this.
603 parsestage |= HAVE_other ;
605 if ((error = wav_read_bext_chunk (psf, chunk_size)))
611 We can eat into a 'PAD ' chunk if we need to.
612 parsestage |= HAVE_other ;
614 psf_log_printf (psf, "%M : %u\n", marker, chunk_size) ;
615 psf_binheader_readf (psf, "j", chunk_size) ;
619 if ((error = wav_read_cart_chunk (psf, chunk_size)))
623 case iXML_MARKER : /* See http://en.wikipedia.org/wiki/IXML */
624 case strc_MARKER : /* Multiple of 32 bytes. */
644 psf_log_printf (psf, "%M : %u\n", marker, chunk_size) ;
645 psf_binheader_readf (psf, "j", chunk_size) ;
649 if (chunk_size >= 0xffff0000)
651 psf_log_printf (psf, "*** Unknown chunk marker (%X) at position %D with length %u. Exiting parser.\n", marker, psf_ftell (psf) - 8, chunk_size) ;
655 if (psf_isprint ((marker >> 24) & 0xFF) && psf_isprint ((marker >> 16) & 0xFF)
656 && psf_isprint ((marker >> 8) & 0xFF) && psf_isprint (marker & 0xFF))
657 { psf_log_printf (psf, "*** %M : %u (unknown marker)\n", marker, chunk_size) ;
658 psf_binheader_readf (psf, "j", chunk_size) ;
661 if (psf_ftell (psf) & 0x03)
662 { psf_log_printf (psf, " Unknown chunk marker at position %D. Resynching.\n", psf_ftell (psf) - 8) ;
663 psf_binheader_readf (psf, "j", -3) ;
664 /* File is too messed up so we prevent editing in RDWR mode here. */
665 parsestage |= HAVE_other ;
668 psf_log_printf (psf, "*** Unknown chunk marker (%X) at position %D. Exiting parser.\n", marker, psf_ftell (psf) - 8) ;
671 } ; /* switch (marker) */
673 if (chunk_size >= psf->filelength)
674 { psf_log_printf (psf, "*** Chunk size %u > file length %D. Exiting parser.\n", chunk_size, psf->filelength) ;
678 if (! psf->sf.seekable && (parsestage & HAVE_data))
681 if (psf_ftell (psf) >= psf->filelength - SIGNED_SIZEOF (chunk_size))
682 { psf_log_printf (psf, "End\n") ;
687 if (psf->dataoffset <= 0)
688 return SFE_WAV_NO_DATA ;
690 if (psf->sf.channels < 1)
691 return SFE_CHANNEL_COUNT_ZERO ;
693 if (psf->sf.channels >= SF_MAX_CHANNELS)
694 return SFE_CHANNEL_COUNT ;
696 if (format != WAVE_FORMAT_PCM && (parsestage & HAVE_fact) == 0)
697 psf_log_printf (psf, "**** All non-PCM format files should have a 'fact' chunk.\n") ;
699 /* WAVs can be little or big endian */
700 psf->endian = psf->rwf_endian ;
702 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
704 if (psf->is_pipe == 0)
706 ** Check for 'wvpk' at the start of the DATA section. Not able to
709 psf_binheader_readf (psf, "4", &marker) ;
710 if (marker == wvpk_MARKER || marker == OggS_MARKER)
711 return SFE_WAV_WVPK_DATA ;
714 /* Seek to start of DATA section. */
715 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
718 { if (psf->filelength - psf->dataoffset < psf->datalength)
719 psf->sf.frames = (psf->filelength - psf->dataoffset) / psf->blockwidth ;
721 psf->sf.frames = psf->datalength / psf->blockwidth ;
725 { case WAVE_FORMAT_EXTENSIBLE :
726 if (psf->sf.format == (SF_FORMAT_WAVEX | SF_FORMAT_MS_ADPCM))
727 { *blockalign = wav_fmt->msadpcm.blockalign ;
728 *framesperblock = wav_fmt->msadpcm.samplesperblock ;
732 case WAVE_FORMAT_PCM :
733 psf->sf.format = SF_FORMAT_WAV | u_bitwidth_to_subformat (psf->bytewidth * 8) ;
736 case WAVE_FORMAT_MULAW :
737 case IBM_FORMAT_MULAW :
738 psf->sf.format = (SF_FORMAT_WAV | SF_FORMAT_ULAW) ;
741 case WAVE_FORMAT_ALAW :
742 case IBM_FORMAT_ALAW :
743 psf->sf.format = (SF_FORMAT_WAV | SF_FORMAT_ALAW) ;
746 case WAVE_FORMAT_MS_ADPCM :
747 psf->sf.format = (SF_FORMAT_WAV | SF_FORMAT_MS_ADPCM) ;
748 *blockalign = wav_fmt->msadpcm.blockalign ;
749 *framesperblock = wav_fmt->msadpcm.samplesperblock ;
752 case WAVE_FORMAT_IMA_ADPCM :
753 psf->sf.format = (SF_FORMAT_WAV | SF_FORMAT_IMA_ADPCM) ;
754 *blockalign = wav_fmt->ima.blockalign ;
755 *framesperblock = wav_fmt->ima.samplesperblock ;
758 case WAVE_FORMAT_GSM610 :
759 psf->sf.format = (SF_FORMAT_WAV | SF_FORMAT_GSM610) ;
762 case WAVE_FORMAT_IEEE_FLOAT :
763 psf->sf.format = SF_FORMAT_WAV ;
764 psf->sf.format |= (psf->bytewidth == 8) ? SF_FORMAT_DOUBLE : SF_FORMAT_FLOAT ;
767 case WAVE_FORMAT_G721_ADPCM :
768 psf->sf.format = SF_FORMAT_WAV | SF_FORMAT_G721_32 ;
771 default : return SFE_UNIMPLEMENTED ;
774 if (wpriv->fmt_is_broken)
775 wav_w64_analyze (psf) ;
777 /* Only set the format endian-ness if its non-standard big-endian. */
778 if (psf->endian == SF_ENDIAN_BIG)
779 psf->sf.format |= SF_ENDIAN_BIG ;
782 } /* wav_read_header */
785 wav_write_fmt_chunk (SF_PRIVATE *psf)
786 { int subformat, fmt_size, add_fact_chunk = 0 ;
788 subformat = SF_CODEC (psf->sf.format) ;
791 { case SF_FORMAT_PCM_U8 :
792 case SF_FORMAT_PCM_16 :
793 case SF_FORMAT_PCM_24 :
794 case SF_FORMAT_PCM_32 :
795 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 ;
797 /* fmt : format, channels, samplerate */
798 psf_binheader_writef (psf, "4224", fmt_size, WAVE_FORMAT_PCM, psf->sf.channels, psf->sf.samplerate) ;
799 /* fmt : bytespersec */
800 psf_binheader_writef (psf, "4", psf->sf.samplerate * psf->bytewidth * psf->sf.channels) ;
801 /* fmt : blockalign, bitwidth */
802 psf_binheader_writef (psf, "22", psf->bytewidth * psf->sf.channels, psf->bytewidth * 8) ;
805 case SF_FORMAT_FLOAT :
806 case SF_FORMAT_DOUBLE :
807 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 ;
809 /* fmt : format, channels, samplerate */
810 psf_binheader_writef (psf, "4224", fmt_size, WAVE_FORMAT_IEEE_FLOAT, psf->sf.channels, psf->sf.samplerate) ;
811 /* fmt : bytespersec */
812 psf_binheader_writef (psf, "4", psf->sf.samplerate * psf->bytewidth * psf->sf.channels) ;
813 /* fmt : blockalign, bitwidth */
814 psf_binheader_writef (psf, "22", psf->bytewidth * psf->sf.channels, psf->bytewidth * 8) ;
816 add_fact_chunk = SF_TRUE ;
819 case SF_FORMAT_ULAW :
820 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 ;
822 /* fmt : format, channels, samplerate */
823 psf_binheader_writef (psf, "4224", fmt_size, WAVE_FORMAT_MULAW, psf->sf.channels, psf->sf.samplerate) ;
824 /* fmt : bytespersec */
825 psf_binheader_writef (psf, "4", psf->sf.samplerate * psf->bytewidth * psf->sf.channels) ;
826 /* fmt : blockalign, bitwidth, extrabytes */
827 psf_binheader_writef (psf, "222", psf->bytewidth * psf->sf.channels, 8, 0) ;
829 add_fact_chunk = SF_TRUE ;
832 case SF_FORMAT_ALAW :
833 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 ;
835 /* fmt : format, channels, samplerate */
836 psf_binheader_writef (psf, "4224", fmt_size, WAVE_FORMAT_ALAW, psf->sf.channels, psf->sf.samplerate) ;
837 /* fmt : bytespersec */
838 psf_binheader_writef (psf, "4", psf->sf.samplerate * psf->bytewidth * psf->sf.channels) ;
839 /* fmt : blockalign, bitwidth, extrabytes */
840 psf_binheader_writef (psf, "222", psf->bytewidth * psf->sf.channels, 8, 0) ;
842 add_fact_chunk = SF_TRUE ;
845 /* Lite remove start */
846 case SF_FORMAT_IMA_ADPCM :
847 { int blockalign, framesperblock, bytespersec ;
849 blockalign = wav_w64_srate2blocksize (psf->sf.samplerate * psf->sf.channels) ;
850 framesperblock = 2 * (blockalign - 4 * psf->sf.channels) / psf->sf.channels + 1 ;
851 bytespersec = (psf->sf.samplerate * blockalign) / framesperblock ;
854 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 + 2 ;
856 /* fmt : size, WAV format type, channels, samplerate, bytespersec */
857 psf_binheader_writef (psf, "42244", fmt_size, WAVE_FORMAT_IMA_ADPCM,
858 psf->sf.channels, psf->sf.samplerate, bytespersec) ;
860 /* fmt : blockalign, bitwidth, extrabytes, framesperblock. */
861 psf_binheader_writef (psf, "2222", blockalign, 4, 2, framesperblock) ;
864 add_fact_chunk = SF_TRUE ;
867 case SF_FORMAT_MS_ADPCM :
868 { int blockalign, framesperblock, bytespersec, extrabytes ;
870 blockalign = wav_w64_srate2blocksize (psf->sf.samplerate * psf->sf.channels) ;
871 framesperblock = 2 + 2 * (blockalign - 7 * psf->sf.channels) / psf->sf.channels ;
872 bytespersec = (psf->sf.samplerate * blockalign) / framesperblock ;
875 extrabytes = 2 + 2 + MSADPCM_ADAPT_COEFF_COUNT * (2 + 2) ;
876 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 + extrabytes ;
878 /* fmt : size, WAV format type, channels. */
879 psf_binheader_writef (psf, "422", fmt_size, WAVE_FORMAT_MS_ADPCM, psf->sf.channels) ;
881 /* fmt : samplerate, bytespersec. */
882 psf_binheader_writef (psf, "44", psf->sf.samplerate, bytespersec) ;
884 /* fmt : blockalign, bitwidth, extrabytes, framesperblock. */
885 psf_binheader_writef (psf, "22222", blockalign, 4, extrabytes, framesperblock, 7) ;
887 msadpcm_write_adapt_coeffs (psf) ;
890 add_fact_chunk = SF_TRUE ;
894 case SF_FORMAT_G721_32 :
896 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 + 2 ;
898 /* fmt : size, WAV format type, channels, samplerate, bytespersec */
899 psf_binheader_writef (psf, "42244", fmt_size, WAVE_FORMAT_G721_ADPCM,
900 psf->sf.channels, psf->sf.samplerate, psf->sf.samplerate * psf->sf.channels / 2) ;
902 /* fmt : blockalign, bitwidth, extrabytes, auxblocksize. */
903 psf_binheader_writef (psf, "2222", 64, 4, 2, 0) ;
905 add_fact_chunk = SF_TRUE ;
908 /* Lite remove end */
910 case SF_FORMAT_GSM610 :
911 { int blockalign, framesperblock, bytespersec ;
913 blockalign = WAV_W64_GSM610_BLOCKSIZE ;
914 framesperblock = WAV_W64_GSM610_SAMPLES ;
915 bytespersec = (psf->sf.samplerate * blockalign) / framesperblock ;
918 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 + 2 ;
920 /* fmt : size, WAV format type, channels. */
921 psf_binheader_writef (psf, "422", fmt_size, WAVE_FORMAT_GSM610, psf->sf.channels) ;
923 /* fmt : samplerate, bytespersec. */
924 psf_binheader_writef (psf, "44", psf->sf.samplerate, bytespersec) ;
926 /* fmt : blockalign, bitwidth, extrabytes, framesperblock. */
927 psf_binheader_writef (psf, "2222", blockalign, 0, 2, framesperblock) ;
930 add_fact_chunk = SF_TRUE ;
933 default : return SFE_UNIMPLEMENTED ;
937 psf_binheader_writef (psf, "tm48", fact_MARKER, 4, psf->sf.frames) ;
940 } /* wav_write_fmt_chunk */
943 wavex_write_fmt_chunk (SF_PRIVATE *psf)
944 { WAV_PRIVATE *wpriv ;
945 int subformat, fmt_size ;
947 if ((wpriv = psf->container_data) == NULL)
948 return SFE_INTERNAL ;
950 subformat = SF_CODEC (psf->sf.format) ;
952 /* initial section (same for all, it appears) */
954 { case SF_FORMAT_PCM_U8 :
955 case SF_FORMAT_PCM_16 :
956 case SF_FORMAT_PCM_24 :
957 case SF_FORMAT_PCM_32 :
958 case SF_FORMAT_FLOAT :
959 case SF_FORMAT_DOUBLE :
960 case SF_FORMAT_ULAW :
961 case SF_FORMAT_ALAW :
962 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 + 2 + 4 + 4 + 2 + 2 + 8 ;
964 /* fmt : format, channels, samplerate */
965 psf_binheader_writef (psf, "4224", fmt_size, WAVE_FORMAT_EXTENSIBLE, psf->sf.channels, psf->sf.samplerate) ;
966 /* fmt : bytespersec */
967 psf_binheader_writef (psf, "4", psf->sf.samplerate * psf->bytewidth * psf->sf.channels) ;
968 /* fmt : blockalign, bitwidth */
969 psf_binheader_writef (psf, "22", psf->bytewidth * psf->sf.channels, psf->bytewidth * 8) ;
971 /* cbSize 22 is sizeof (WAVEFORMATEXTENSIBLE) - sizeof (WAVEFORMATEX) */
972 psf_binheader_writef (psf, "2", 22) ;
974 /* wValidBitsPerSample, for our use same as bitwidth as we use it fully */
975 psf_binheader_writef (psf, "2", psf->bytewidth * 8) ;
977 /* For an Ambisonic file set the channel mask to zero.
978 ** Otherwise use a default based on the channel count.
980 if (wpriv->wavex_ambisonic != SF_AMBISONIC_NONE)
981 psf_binheader_writef (psf, "4", 0) ;
982 else if (wpriv->wavex_channelmask != 0)
983 psf_binheader_writef (psf, "4", wpriv->wavex_channelmask) ;
986 ** Ok some liberty is taken here to use the most commonly used channel masks
987 ** instead of "no mapping". If you really want to use "no mapping" for 8 channels and less
988 ** please don't use wavex. (otherwise we'll have to create a new SF_COMMAND)
990 switch (psf->sf.channels)
991 { case 1 : /* center channel mono */
992 psf_binheader_writef (psf, "4", 0x4) ;
995 case 2 : /* front left and right */
996 psf_binheader_writef (psf, "4", 0x1 | 0x2) ;
1000 psf_binheader_writef (psf, "4", 0x1 | 0x2 | 0x10 | 0x20) ;
1004 psf_binheader_writef (psf, "4", 0x1 | 0x2 | 0x4 | 0x8 | 0x10 | 0x20) ;
1008 psf_binheader_writef (psf, "4", 0x1 | 0x2 | 0x4 | 0x8 | 0x10 | 0x20 | 0x40 | 0x80) ;
1011 default : /* 0 when in doubt , use direct out, ie NO mapping*/
1012 psf_binheader_writef (psf, "4", 0x0) ;
1018 case SF_FORMAT_MS_ADPCM : /* Todo, GUID exists might have different header as per wav_write_header */
1020 return SFE_UNIMPLEMENTED ;
1023 /* GUID section, different for each */
1026 { case SF_FORMAT_PCM_U8 :
1027 case SF_FORMAT_PCM_16 :
1028 case SF_FORMAT_PCM_24 :
1029 case SF_FORMAT_PCM_32 :
1030 wavex_write_guid (psf, wpriv->wavex_ambisonic == SF_AMBISONIC_NONE ?
1031 &MSGUID_SUBTYPE_PCM : &MSGUID_SUBTYPE_AMBISONIC_B_FORMAT_PCM) ;
1034 case SF_FORMAT_FLOAT :
1035 case SF_FORMAT_DOUBLE :
1036 wavex_write_guid (psf, wpriv->wavex_ambisonic == SF_AMBISONIC_NONE ?
1037 &MSGUID_SUBTYPE_IEEE_FLOAT : &MSGUID_SUBTYPE_AMBISONIC_B_FORMAT_IEEE_FLOAT) ;
1040 case SF_FORMAT_ULAW :
1041 wavex_write_guid (psf, &MSGUID_SUBTYPE_MULAW) ;
1044 case SF_FORMAT_ALAW :
1045 wavex_write_guid (psf, &MSGUID_SUBTYPE_ALAW) ;
1049 /* This is dead code due to return in previous switch statement. */
1050 case SF_FORMAT_MS_ADPCM : /* todo, GUID exists */
1051 wavex_write_guid (psf, &MSGUID_SUBTYPE_MS_ADPCM) ;
1053 return SFE_UNIMPLEMENTED ;
1056 default : return SFE_UNIMPLEMENTED ;
1059 psf_binheader_writef (psf, "tm48", fact_MARKER, 4, psf->sf.frames) ;
1062 } /* wavex_write_fmt_chunk */
1066 wav_write_header (SF_PRIVATE *psf, int calc_length)
1067 { sf_count_t current ;
1069 int k, error, has_data = SF_FALSE ;
1071 current = psf_ftell (psf) ;
1073 if (current > psf->dataoffset)
1074 has_data = SF_TRUE ;
1077 { psf->filelength = psf_get_filelen (psf) ;
1079 psf->datalength = psf->filelength - psf->dataoffset ;
1082 psf->datalength -= psf->filelength - psf->dataend ;
1083 else if (psf->bytewidth > 0 && psf->sf.seekable == SF_TRUE)
1084 psf->datalength = psf->sf.frames * psf->bytewidth * psf->sf.channels ;
1087 /* Reset the current header length to zero. */
1088 psf->header [0] = 0 ;
1089 psf->headindex = 0 ;
1090 psf_fseek (psf, 0, SEEK_SET) ;
1093 ** RIFX signifies big-endian format for all header and data.
1094 ** To prevent lots of code copying here, we'll set the psf->rwf_endian flag
1095 ** once here, and never specify endian-ness for all other header operations.
1098 /* RIFF/RIFX marker, length, WAVE and 'fmt ' markers. */
1100 if (psf->endian == SF_ENDIAN_LITTLE)
1101 psf_binheader_writef (psf, "etm8", RIFF_MARKER, (psf->filelength < 8) ? 8 : psf->filelength - 8) ;
1103 psf_binheader_writef (psf, "Etm8", RIFX_MARKER, (psf->filelength < 8) ? 8 : psf->filelength - 8) ;
1105 /* WAVE and 'fmt ' markers. */
1106 psf_binheader_writef (psf, "mm", WAVE_MARKER, fmt_MARKER) ;
1108 /* Write the 'fmt ' chunk. */
1109 switch (SF_CONTAINER (psf->sf.format))
1110 { case SF_FORMAT_WAV :
1111 if ((error = wav_write_fmt_chunk (psf)) != 0)
1115 case SF_FORMAT_WAVEX :
1116 if ((error = wavex_write_fmt_chunk (psf)) != 0)
1121 return SFE_UNIMPLEMENTED ;
1124 /* The LIST/INFO chunk. */
1125 if (psf->strings.flags & SF_STR_LOCATE_START)
1126 wav_write_strings (psf, SF_STR_LOCATE_START) ;
1128 if (psf->peak_info != NULL && psf->peak_info->peak_loc == SF_PEAK_START)
1129 { psf_binheader_writef (psf, "m4", PEAK_MARKER, WAV_PEAK_CHUNK_SIZE (psf->sf.channels)) ;
1130 psf_binheader_writef (psf, "44", 1, time (NULL)) ;
1131 for (k = 0 ; k < psf->sf.channels ; k++)
1132 psf_binheader_writef (psf, "ft8", (float) psf->peak_info->peaks [k].value, psf->peak_info->peaks [k].position) ;
1135 if (psf->broadcast_16k != NULL)
1136 wav_write_bext_chunk (psf) ;
1138 if (psf->cart_16k != NULL)
1139 wav_write_cart_chunk (psf) ;
1141 if (psf->instrument != NULL)
1143 double dtune = (double) (0x40000000) / 25.0 ;
1145 psf_binheader_writef (psf, "m4", smpl_MARKER, 9 * 4 + psf->instrument->loop_count * 6 * 4) ;
1146 psf_binheader_writef (psf, "44", 0, 0) ; /* Manufacturer zero is everyone */
1147 tmp = (int) (1.0e9 / psf->sf.samplerate) ; /* Sample period in nano seconds */
1148 psf_binheader_writef (psf, "44", tmp, psf->instrument->basenote) ;
1149 tmp = (uint32_t) (psf->instrument->detune * dtune + 0.5) ;
1150 psf_binheader_writef (psf, "4", tmp) ;
1151 psf_binheader_writef (psf, "44", 0, 0) ; /* SMTPE format */
1152 psf_binheader_writef (psf, "44", psf->instrument->loop_count, 0) ;
1154 for (tmp = 0 ; tmp < psf->instrument->loop_count ; tmp++)
1157 type = psf->instrument->loops [tmp].mode ;
1158 type = (type == SF_LOOP_FORWARD ? 0 : type == SF_LOOP_BACKWARD ? 2 : type == SF_LOOP_ALTERNATING ? 1 : 32) ;
1160 psf_binheader_writef (psf, "44", tmp, type) ;
1161 psf_binheader_writef (psf, "44", psf->instrument->loops [tmp].start, psf->instrument->loops [tmp].end - 1) ;
1162 psf_binheader_writef (psf, "44", 0, psf->instrument->loops [tmp].count) ;
1166 /* Write custom headers. */
1167 for (uk = 0 ; uk < psf->wchunks.used ; uk++)
1168 psf_binheader_writef (psf, "m4b", (int) psf->wchunks.chunks [uk].mark32, psf->wchunks.chunks [uk].len, psf->wchunks.chunks [uk].data, make_size_t (psf->wchunks.chunks [uk].len)) ;
1170 if (psf->headindex + 16 < psf->dataoffset)
1171 { /* Add PAD data if necessary. */
1172 k = psf->dataoffset - (psf->headindex + 16) ;
1173 psf_binheader_writef (psf, "m4z", PAD_MARKER, k, make_size_t (k)) ;
1176 psf_binheader_writef (psf, "tm8", data_MARKER, psf->datalength) ;
1177 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
1181 if (has_data && psf->dataoffset != psf->headindex)
1182 { psf_log_printf (psf, "Oooops : has_data && psf->dataoffset != psf->headindex\n") ;
1183 return psf->error = SFE_INTERNAL ;
1186 psf->dataoffset = psf->headindex ;
1189 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
1190 else if (current > 0)
1191 psf_fseek (psf, current, SEEK_SET) ;
1194 } /* wav_write_header */
1198 wav_write_tailer (SF_PRIVATE *psf)
1201 /* Reset the current header buffer length to zero. */
1202 psf->header [0] = 0 ;
1203 psf->headindex = 0 ;
1205 if (psf->bytewidth > 0 && psf->sf.seekable == SF_TRUE)
1206 { psf->datalength = psf->sf.frames * psf->bytewidth * psf->sf.channels ;
1207 psf->dataend = psf->dataoffset + psf->datalength ;
1210 if (psf->dataend > 0)
1211 psf_fseek (psf, psf->dataend, SEEK_SET) ;
1213 psf->dataend = psf_fseek (psf, 0, SEEK_END) ;
1215 if (psf->dataend & 1)
1216 psf_binheader_writef (psf, "z", 1) ;
1218 /* Add a PEAK chunk if requested. */
1219 if (psf->peak_info != NULL && psf->peak_info->peak_loc == SF_PEAK_END)
1220 { psf_binheader_writef (psf, "m4", PEAK_MARKER, WAV_PEAK_CHUNK_SIZE (psf->sf.channels)) ;
1221 psf_binheader_writef (psf, "44", 1, time (NULL)) ;
1222 for (k = 0 ; k < psf->sf.channels ; k++)
1223 psf_binheader_writef (psf, "f4", psf->peak_info->peaks [k].value, psf->peak_info->peaks [k].position) ;
1226 if (psf->strings.flags & SF_STR_LOCATE_END)
1227 wav_write_strings (psf, SF_STR_LOCATE_END) ;
1229 /* Write the tailer. */
1230 if (psf->headindex > 0)
1231 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
1234 } /* wav_write_tailer */
1237 wav_write_strings (SF_PRIVATE *psf, int location)
1238 { int k, prev_head_index, saved_head_index ;
1240 if (psf_location_string_count (psf, location) == 0)
1243 prev_head_index = psf->headindex + 4 ;
1245 psf_binheader_writef (psf, "m4m", LIST_MARKER, 0xBADBAD, INFO_MARKER) ;
1247 for (k = 0 ; k < SF_MAX_STRINGS ; k++)
1248 { if (psf->strings.data [k].type == 0)
1250 if (psf->strings.data [k].type < 0 || psf->strings.data [k].flags != location)
1253 switch (psf->strings.data [k].type)
1254 { case SF_STR_SOFTWARE :
1255 psf_binheader_writef (psf, "ms", ISFT_MARKER, psf->strings.storage + psf->strings.data [k].offset) ;
1259 psf_binheader_writef (psf, "ms", INAM_MARKER, psf->strings.storage + psf->strings.data [k].offset) ;
1262 case SF_STR_COPYRIGHT :
1263 psf_binheader_writef (psf, "ms", ICOP_MARKER, psf->strings.storage + psf->strings.data [k].offset) ;
1266 case SF_STR_ARTIST :
1267 psf_binheader_writef (psf, "ms", IART_MARKER, psf->strings.storage + psf->strings.data [k].offset) ;
1270 case SF_STR_COMMENT :
1271 psf_binheader_writef (psf, "ms", ICMT_MARKER, psf->strings.storage + psf->strings.data [k].offset) ;
1275 psf_binheader_writef (psf, "ms", ICRD_MARKER, psf->strings.storage + psf->strings.data [k].offset) ;
1279 psf_binheader_writef (psf, "ms", IGNR_MARKER, psf->strings.storage + psf->strings.data [k].offset) ;
1283 psf_binheader_writef (psf, "ms", IPRD_MARKER, psf->strings.storage + psf->strings.data [k].offset) ;
1286 case SF_STR_TRACKNUMBER :
1287 psf_binheader_writef (psf, "ms", ITRK_MARKER, psf->strings.storage + psf->strings.data [k].offset) ;
1295 saved_head_index = psf->headindex ;
1296 psf->headindex = prev_head_index ;
1297 psf_binheader_writef (psf, "4", saved_head_index - prev_head_index - 4) ;
1298 psf->headindex = saved_head_index ;
1300 } /* wav_write_strings */
1303 wav_close (SF_PRIVATE *psf)
1305 if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
1306 { wav_write_tailer (psf) ;
1308 if (psf->file.mode == SFM_RDWR)
1309 { sf_count_t current = psf_ftell (psf) ;
1312 ** If the mode is RDWR and the current position is less than the
1313 ** filelength, truncate the file.
1316 if (current < psf->filelength)
1317 { psf_ftruncate (psf, current) ;
1318 psf->filelength = current ;
1322 psf->write_header (psf, SF_TRUE) ;
1329 wav_command (SF_PRIVATE *psf, int command, void * UNUSED (data), int datasize)
1330 { WAV_PRIVATE *wpriv ;
1332 if ((wpriv = psf->container_data) == NULL)
1333 return SFE_INTERNAL ;
1336 { case SFC_WAVEX_SET_AMBISONIC :
1337 if ((SF_CONTAINER (psf->sf.format)) == SF_FORMAT_WAVEX)
1338 { if (datasize == SF_AMBISONIC_NONE)
1339 wpriv->wavex_ambisonic = SF_AMBISONIC_NONE ;
1340 else if (datasize == SF_AMBISONIC_B_FORMAT)
1341 wpriv->wavex_ambisonic = SF_AMBISONIC_B_FORMAT ;
1345 return wpriv->wavex_ambisonic ;
1347 case SFC_WAVEX_GET_AMBISONIC :
1348 return wpriv->wavex_ambisonic ;
1350 case SFC_SET_CHANNEL_MAP_INFO :
1351 wpriv->wavex_channelmask = wavex_gen_channel_mask (psf->channel_map, psf->sf.channels) ;
1352 return (wpriv->wavex_channelmask != 0) ;
1362 wav_subchunk_parse (SF_PRIVATE *psf, int chunk, uint32_t chunk_length)
1363 { sf_count_t current_pos ;
1365 uint32_t chunk_size, bytesread ;
1367 current_pos = psf_fseek (psf, 0, SEEK_CUR) - 4 ;
1369 bytesread = sizeof (chunk_length) ;
1371 if (chunk_length <= 8)
1372 { /* This case is for broken files generated by PEAK. */
1373 psf_log_printf (psf, "%M : %u (weird length)\n", chunk, chunk_length) ;
1374 psf_binheader_readf (psf, "mj", &chunk, chunk_length - 4) ;
1375 psf_log_printf (psf, " %M\n", chunk) ;
1379 if (psf->headindex + chunk_length > SIGNED_SIZEOF (psf->header))
1380 { psf_log_printf (psf, "%M : %u (too long)\n", chunk, chunk_length) ;
1381 psf_binheader_readf (psf, "j", chunk_length) ;
1385 if (current_pos + chunk_length > psf->filelength)
1386 { psf_log_printf (psf, "%M : %u (should be %d)\n", chunk, chunk_length, (int) (psf->filelength - current_pos)) ;
1387 chunk_length = psf->filelength - current_pos ;
1390 psf_log_printf (psf, "%M : %u\n", chunk, chunk_length) ;
1392 while (bytesread < chunk_length)
1393 { uint32_t thisread ;
1395 if ((thisread = psf_binheader_readf (psf, "m", &chunk)) == 0)
1397 bytesread += thisread ;
1400 { case adtl_MARKER :
1402 /* These markers don't contain anything, not even a chunk lebgth. */
1403 psf_log_printf (psf, " %M\n", chunk) ;
1407 psf_log_printf (psf, " %M\n", chunk) ;
1408 bytesread += exif_subchunk_parse (psf, chunk_length - bytesread) ;
1412 psf_log_printf (psf, " %M inside a LIST block??? Backing out.\n", chunk) ;
1413 /* Jump back four bytes and return to caller. */
1414 psf_binheader_readf (psf, "j", -4) ;
1419 ** Four zero bytes where a marker was expected. Assume this means
1420 ** the rest of the chunk is garbage.
1422 psf_log_printf (psf, " *** Found weird-ass zero marker. Jumping to end of chunk.\n") ;
1423 if (bytesread < chunk_length)
1424 bytesread += psf_binheader_readf (psf, "j", chunk_length - bytesread + 4) ;
1425 psf_log_printf (psf, " *** Offset is now : 0x%X\n", psf_fseek (psf, 0, SEEK_CUR)) ;
1433 { case ISFT_MARKER :
1447 bytesread += psf_binheader_readf (psf, "4", &chunk_size) ;
1448 chunk_size += (chunk_size & 1) ;
1449 if (chunk_size >= SIGNED_SIZEOF (buffer) || chunk_size >= chunk_length)
1450 { psf_log_printf (psf, " *** %M : %u (too big)\n", chunk, chunk_size) ;
1451 psf_binheader_readf (psf, "j", chunk_length) ;
1455 bytesread += psf_binheader_readf (psf, "b", buffer, chunk_size) ;
1456 buffer [chunk_size] = 0 ;
1457 psf_log_printf (psf, " %M : %s\n", chunk, buffer) ;
1463 bytesread += psf_binheader_readf (psf, "44", &chunk_size, &mark_id) ;
1465 chunk_size += (chunk_size & 1) ;
1466 if (chunk_size < 1 || chunk_size >= SIGNED_SIZEOF (buffer) || chunk_size >= chunk_length)
1467 { psf_log_printf (psf, " *** %M : %u (too big)\n", chunk, chunk_size) ;
1468 psf_binheader_readf (psf, "j", chunk_length) ;
1472 bytesread += psf_binheader_readf (psf, "b", buffer, chunk_size) ;
1473 buffer [chunk_size] = 0 ;
1474 psf_log_printf (psf, " %M : %u : %s\n", chunk, mark_id, buffer) ;
1482 bytesread += psf_binheader_readf (psf, "4", &chunk_size) ;
1483 chunk_size += (chunk_size & 1) ;
1484 if (chunk_size >= SIGNED_SIZEOF (buffer) || chunk_size >= chunk_length)
1485 { psf_log_printf (psf, " *** %M : %u (too big)\n", chunk, chunk_size) ;
1486 psf_binheader_readf (psf, "j", chunk_length) ;
1490 bytesread += psf_binheader_readf (psf, "j", chunk_size) ;
1491 psf_log_printf (psf, " %M : %u\n", chunk, chunk_size) ;
1495 bytesread += psf_binheader_readf (psf, "4", &chunk_size) ;
1496 chunk_size += (chunk_size & 1) ;
1497 psf_log_printf (psf, " *** %M : %u\n", chunk, chunk_size) ;
1498 if (bytesread + chunk_size > chunk_length)
1499 { bytesread += psf_binheader_readf (psf, "j", chunk_length - bytesread + 4) ;
1503 bytesread += psf_binheader_readf (psf, "j", chunk_size) ;
1505 if (chunk_size >= chunk_length)
1511 { case ISFT_MARKER :
1512 psf_store_string (psf, SF_STR_SOFTWARE, buffer) ;
1515 psf_store_string (psf, SF_STR_COPYRIGHT, buffer) ;
1518 psf_store_string (psf, SF_STR_TITLE, buffer) ;
1521 psf_store_string (psf, SF_STR_ARTIST, buffer) ;
1524 psf_store_string (psf, SF_STR_COMMENT, buffer) ;
1527 psf_store_string (psf, SF_STR_DATE, buffer) ;
1530 psf_store_string (psf, SF_STR_GENRE, buffer) ;
1533 psf_store_string (psf, SF_STR_ALBUM, buffer) ;
1536 psf_store_string (psf, SF_STR_TRACKNUMBER, buffer) ;
1541 current_pos = psf_fseek (psf, 0, SEEK_CUR) - current_pos ;
1543 if (current_pos - 4 != chunk_length)
1544 psf_log_printf (psf, "**** Bad chunk length %d sbould be %D\n", chunk_length, current_pos - 4) ;
1547 } /* wav_subchunk_parse */
1550 wav_read_smpl_chunk (SF_PRIVATE *psf, uint32_t chunklen)
1551 { char buffer [512] ;
1552 uint32_t thisread, bytesread = 0, dword, sampler_data, loop_count ;
1553 uint32_t note, start, end, type = -1, count ;
1556 chunklen += (chunklen & 1) ;
1558 bytesread += psf_binheader_readf (psf, "4", &dword) ;
1559 psf_log_printf (psf, " Manufacturer : %X\n", dword) ;
1561 bytesread += psf_binheader_readf (psf, "4", &dword) ;
1562 psf_log_printf (psf, " Product : %u\n", dword) ;
1564 bytesread += psf_binheader_readf (psf, "4", &dword) ;
1565 psf_log_printf (psf, " Period : %u nsec\n", dword) ;
1567 bytesread += psf_binheader_readf (psf, "4", ¬e) ;
1568 psf_log_printf (psf, " Midi Note : %u\n", note) ;
1570 bytesread += psf_binheader_readf (psf, "4", &dword) ;
1572 { snprintf (buffer, sizeof (buffer), "%f",
1573 (1.0 * 0x80000000) / ((uint32_t) dword)) ;
1574 psf_log_printf (psf, " Pitch Fract. : %s\n", buffer) ;
1577 psf_log_printf (psf, " Pitch Fract. : 0\n") ;
1579 bytesread += psf_binheader_readf (psf, "4", &dword) ;
1580 psf_log_printf (psf, " SMPTE Format : %u\n", dword) ;
1582 bytesread += psf_binheader_readf (psf, "4", &dword) ;
1583 snprintf (buffer, sizeof (buffer), "%02d:%02d:%02d %02d",
1584 (dword >> 24) & 0x7F, (dword >> 16) & 0x7F, (dword >> 8) & 0x7F, dword & 0x7F) ;
1585 psf_log_printf (psf, " SMPTE Offset : %s\n", buffer) ;
1587 bytesread += psf_binheader_readf (psf, "4", &loop_count) ;
1588 psf_log_printf (psf, " Loop Count : %u\n", loop_count) ;
1590 if (loop_count == 0 && chunklen == bytesread)
1593 /* Sampler Data holds the number of data bytes after the CUE chunks which
1594 ** is not actually CUE data. Display value after CUE data.
1596 bytesread += psf_binheader_readf (psf, "4", &sampler_data) ;
1598 if ((psf->instrument = psf_instrument_alloc ()) == NULL)
1599 return SFE_MALLOC_FAILED ;
1601 psf->instrument->loop_count = loop_count ;
1603 for (j = 0 ; loop_count > 0 && chunklen - bytesread >= 24 ; j ++)
1604 { if ((thisread = psf_binheader_readf (psf, "4", &dword)) == 0)
1606 bytesread += thisread ;
1607 psf_log_printf (psf, " Cue ID : %2u", dword) ;
1609 bytesread += psf_binheader_readf (psf, "4", &type) ;
1610 psf_log_printf (psf, " Type : %2u", type) ;
1612 bytesread += psf_binheader_readf (psf, "4", &start) ;
1613 psf_log_printf (psf, " Start : %5u", start) ;
1615 bytesread += psf_binheader_readf (psf, "4", &end) ;
1616 psf_log_printf (psf, " End : %5u", end) ;
1618 bytesread += psf_binheader_readf (psf, "4", &dword) ;
1619 psf_log_printf (psf, " Fraction : %5u", dword) ;
1621 bytesread += psf_binheader_readf (psf, "4", &count) ;
1622 psf_log_printf (psf, " Count : %5u\n", count) ;
1624 if (j < ARRAY_LEN (psf->instrument->loops))
1625 { psf->instrument->loops [j].start = start ;
1626 psf->instrument->loops [j].end = end + 1 ;
1627 psf->instrument->loops [j].count = count ;
1631 psf->instrument->loops [j].mode = SF_LOOP_FORWARD ;
1634 psf->instrument->loops [j].mode = SF_LOOP_ALTERNATING ;
1637 psf->instrument->loops [j].mode = SF_LOOP_BACKWARD ;
1640 psf->instrument->loops [j].mode = SF_LOOP_NONE ;
1648 if (chunklen - bytesread == 0)
1649 { if (sampler_data != 0)
1650 psf_log_printf (psf, " Sampler Data : %u (should be 0)\n", sampler_data) ;
1652 psf_log_printf (psf, " Sampler Data : %u\n", sampler_data) ;
1655 { if (sampler_data != chunklen - bytesread)
1656 { psf_log_printf (psf, " Sampler Data : %u (should have been %u)\n", sampler_data, chunklen - bytesread) ;
1657 sampler_data = chunklen - bytesread ;
1660 psf_log_printf (psf, " Sampler Data : %u\n", sampler_data) ;
1662 psf_log_printf (psf, " ") ;
1663 for (k = 0 ; k < (int) sampler_data ; k++)
1666 if (k > 0 && (k % 20) == 0)
1667 psf_log_printf (psf, "\n ") ;
1669 if ((thisread = psf_binheader_readf (psf, "1", &ch)) == 0)
1671 bytesread += thisread ;
1672 psf_log_printf (psf, "%02X ", ch & 0xFF) ;
1675 psf_log_printf (psf, "\n") ;
1678 psf->instrument->basenote = note ;
1679 psf->instrument->gain = 1 ;
1680 psf->instrument->velocity_lo = psf->instrument->key_lo = 0 ;
1681 psf->instrument->velocity_hi = psf->instrument->key_hi = 127 ;
1684 } /* wav_read_smpl_chunk */
1687 ** The acid chunk goes a little something like this:
1690 ** 4 bytes (int) length of chunk starting at next byte
1692 ** 4 bytes (int) type of file:
1693 ** this appears to be a bit mask,however some combinations
1694 ** are probably impossible and/or qualified as "errors"
1696 ** 0x01 On: One Shot Off: Loop
1697 ** 0x02 On: Root note is Set Off: No root
1698 ** 0x04 On: Stretch is On, Off: Strech is OFF
1699 ** 0x08 On: Disk Based Off: Ram based
1700 ** 0x10 On: ?????????? Off: ????????? (Acidizer puts that ON)
1702 ** 2 bytes (short) root note
1703 ** if type 0x10 is OFF : [C,C#,(...),B] -> [0x30 to 0x3B]
1704 ** if type 0x10 is ON : [C,C#,(...),B] -> [0x3C to 0x47]
1705 ** (both types fit on same MIDI pitch albeit different octaves, so who cares)
1707 ** 2 bytes (short) ??? always set to 0x8000
1708 ** 4 bytes (float) ??? seems to be always 0
1709 ** 4 bytes (int) number of beats
1710 ** 2 bytes (short) meter denominator //always 4 in SF/ACID
1711 ** 2 bytes (short) meter numerator //always 4 in SF/ACID
1712 ** //are we sure about the order?? usually its num/denom
1713 ** 4 bytes (float) tempo
1718 wav_read_acid_chunk (SF_PRIVATE *psf, uint32_t chunklen)
1719 { char buffer [512] ;
1720 uint32_t bytesread = 0 ;
1722 short rootnote, q1, meter_denom, meter_numer ;
1725 chunklen += (chunklen & 1) ;
1727 bytesread += psf_binheader_readf (psf, "422f", &flags, &rootnote, &q1, &q2) ;
1729 snprintf (buffer, sizeof (buffer), "%f", q2) ;
1731 psf_log_printf (psf, " Flags : 0x%04x (%s,%s,%s,%s,%s)\n", flags,
1732 (flags & 0x01) ? "OneShot" : "Loop",
1733 (flags & 0x02) ? "RootNoteValid" : "RootNoteInvalid",
1734 (flags & 0x04) ? "StretchOn" : "StretchOff",
1735 (flags & 0x08) ? "DiskBased" : "RAMBased",
1736 (flags & 0x10) ? "??On" : "??Off") ;
1738 psf_log_printf (psf, " Root note : 0x%x\n ???? : 0x%04x\n ???? : %s\n",
1739 rootnote, q1, buffer) ;
1741 bytesread += psf_binheader_readf (psf, "422f", &beats, &meter_denom, &meter_numer, &tempo) ;
1742 snprintf (buffer, sizeof (buffer), "%f", tempo) ;
1743 psf_log_printf (psf, " Beats : %d\n Meter : %d/%d\n Tempo : %s\n",
1744 beats, meter_numer, meter_denom, buffer) ;
1746 psf_binheader_readf (psf, "j", chunklen - bytesread) ;
1748 if ((psf->loop_info = calloc (1, sizeof (SF_LOOP_INFO))) == NULL)
1749 return SFE_MALLOC_FAILED ;
1751 psf->loop_info->time_sig_num = meter_numer ;
1752 psf->loop_info->time_sig_den = meter_denom ;
1753 psf->loop_info->loop_mode = (flags & 0x01) ? SF_LOOP_NONE : SF_LOOP_FORWARD ;
1754 psf->loop_info->num_beats = beats ;
1755 psf->loop_info->bpm = tempo ;
1756 psf->loop_info->root_key = (flags & 0x02) ? rootnote : -1 ;
1759 } /* wav_read_acid_chunk */
1762 wav_read_bext_chunk (SF_PRIVATE *psf, uint32_t chunksize)
1764 SF_BROADCAST_INFO_16K * b ;
1765 uint32_t bytes = 0 ;
1767 if (chunksize < WAV_BEXT_MIN_CHUNK_SIZE)
1768 { psf_log_printf (psf, "bext : %u (should be >= %d)\n", chunksize, WAV_BEXT_MIN_CHUNK_SIZE) ;
1769 psf_binheader_readf (psf, "j", chunksize) ;
1773 if (chunksize > WAV_BEXT_MAX_CHUNK_SIZE)
1774 { psf_log_printf (psf, "bext : %u (should be < %d)\n", chunksize, WAV_BEXT_MAX_CHUNK_SIZE) ;
1775 psf_binheader_readf (psf, "j", chunksize) ;
1779 if (chunksize >= sizeof (SF_BROADCAST_INFO_16K))
1780 { psf_log_printf (psf, "bext : %u too big to be handled\n", chunksize) ;
1781 psf_binheader_readf (psf, "j", chunksize) ;
1785 psf_log_printf (psf, "bext : %u\n", chunksize) ;
1787 if ((psf->broadcast_16k = broadcast_var_alloc ()) == NULL)
1788 { psf->error = SFE_MALLOC_FAILED ;
1792 b = psf->broadcast_16k ;
1794 bytes += psf_binheader_readf (psf, "b", b->description, sizeof (b->description)) ;
1795 bytes += psf_binheader_readf (psf, "b", b->originator, sizeof (b->originator)) ;
1796 bytes += psf_binheader_readf (psf, "b", b->originator_reference, sizeof (b->originator_reference)) ;
1797 bytes += psf_binheader_readf (psf, "b", b->origination_date, sizeof (b->origination_date)) ;
1798 bytes += psf_binheader_readf (psf, "b", b->origination_time, sizeof (b->origination_time)) ;
1799 bytes += psf_binheader_readf (psf, "442", &b->time_reference_low, &b->time_reference_high, &b->version) ;
1800 bytes += psf_binheader_readf (psf, "bj", &b->umid, sizeof (b->umid), 190) ;
1802 if (chunksize > WAV_BEXT_MIN_CHUNK_SIZE)
1803 { /* File has coding history data. */
1805 b->coding_history_size = chunksize - WAV_BEXT_MIN_CHUNK_SIZE ;
1807 /* We do not parse the coding history */
1808 bytes += psf_binheader_readf (psf, "b", b->coding_history, b->coding_history_size) ;
1811 if (bytes < chunksize)
1812 psf_binheader_readf (psf, "j", chunksize - bytes) ;
1815 } /* wav_read_bext_chunk */
1818 wav_write_bext_chunk (SF_PRIVATE *psf)
1819 { SF_BROADCAST_INFO_16K *b ;
1821 if (psf->broadcast_16k == NULL)
1824 b = psf->broadcast_16k ;
1826 psf_binheader_writef (psf, "m4", bext_MARKER, WAV_BEXT_MIN_CHUNK_SIZE + b->coding_history_size) ;
1829 ** Note that it is very important that the field widths of the SF_BROADCAST_INFO
1830 ** struct match those of the bext chunk fields.
1833 psf_binheader_writef (psf, "b", b->description, sizeof (b->description)) ;
1834 psf_binheader_writef (psf, "b", b->originator, sizeof (b->originator)) ;
1835 psf_binheader_writef (psf, "b", b->originator_reference, sizeof (b->originator_reference)) ;
1836 psf_binheader_writef (psf, "b", b->origination_date, sizeof (b->origination_date)) ;
1837 psf_binheader_writef (psf, "b", b->origination_time, sizeof (b->origination_time)) ;
1838 psf_binheader_writef (psf, "442", b->time_reference_low, b->time_reference_high, b->version) ;
1839 psf_binheader_writef (psf, "b", b->umid, sizeof (b->umid)) ;
1840 psf_binheader_writef (psf, "z", make_size_t (190)) ;
1842 if (b->coding_history_size > 0)
1843 psf_binheader_writef (psf, "b", b->coding_history, make_size_t (b->coding_history_size)) ;
1846 } /* wav_write_bext_chunk */
1849 wav_read_cart_chunk (SF_PRIVATE *psf, uint32_t chunksize)
1850 { SF_CART_INFO_16K *c ;
1851 uint32_t bytes = 0 ;
1854 if (chunksize < WAV_CART_MIN_CHUNK_SIZE)
1855 { psf_log_printf (psf, "cart : %u (should be >= %d)\n", chunksize, WAV_CART_MIN_CHUNK_SIZE) ;
1856 psf_binheader_readf (psf, "j", chunksize) ;
1859 if (chunksize > WAV_CART_MAX_CHUNK_SIZE)
1860 { psf_log_printf (psf, "cart : %u (should be < %d)\n", chunksize, WAV_CART_MAX_CHUNK_SIZE) ;
1861 psf_binheader_readf (psf, "j", chunksize) ;
1865 if (chunksize >= sizeof (SF_CART_INFO_16K))
1866 { psf_log_printf (psf, "cart : %u too big to be handled\n", chunksize) ;
1867 psf_binheader_readf (psf, "j", chunksize) ;
1871 psf_log_printf (psf, "cart : %u\n", chunksize) ;
1873 if ((psf->cart_16k = cart_var_alloc ()) == NULL)
1874 { psf->error = SFE_MALLOC_FAILED ;
1879 bytes += psf_binheader_readf (psf, "b", c->version, sizeof (c->version)) ;
1880 bytes += psf_binheader_readf (psf, "b", c->title, sizeof (c->title)) ;
1881 bytes += psf_binheader_readf (psf, "b", c->artist, sizeof (c->artist)) ;
1882 bytes += psf_binheader_readf (psf, "b", c->cut_id, sizeof (c->cut_id)) ;
1883 bytes += psf_binheader_readf (psf, "b", c->client_id, sizeof (c->client_id)) ;
1884 bytes += psf_binheader_readf (psf, "b", c->category, sizeof (c->category)) ;
1885 bytes += psf_binheader_readf (psf, "b", c->classification, sizeof (c->classification)) ;
1886 bytes += psf_binheader_readf (psf, "b", c->out_cue, sizeof (c->out_cue)) ;
1887 bytes += psf_binheader_readf (psf, "b", c->start_date, sizeof (c->start_date)) ;
1888 bytes += psf_binheader_readf (psf, "b", c->start_time, sizeof (c->start_time)) ;
1889 bytes += psf_binheader_readf (psf, "b", c->end_date, sizeof (c->end_date)) ;
1890 bytes += psf_binheader_readf (psf, "b", c->end_time, sizeof (c->end_time)) ;
1891 bytes += psf_binheader_readf (psf, "b", c->producer_app_id, sizeof (c->producer_app_id)) ;
1892 bytes += psf_binheader_readf (psf, "b", c->producer_app_version, sizeof (c->producer_app_version)) ;
1893 bytes += psf_binheader_readf (psf, "b", c->user_def, sizeof (c->user_def)) ;
1894 bytes += psf_binheader_readf (psf, "e4", &c->level_reference, sizeof (c->level_reference)) ;
1896 for (k = 0 ; k < ARRAY_LEN (c->post_timers) ; k++)
1897 bytes += psf_binheader_readf (psf, "b4", &c->post_timers [k].usage, make_size_t (4), &c->post_timers [k].value) ;
1899 bytes += psf_binheader_readf (psf, "b", c->reserved, sizeof (c->reserved)) ;
1900 bytes += psf_binheader_readf (psf, "b", c->url, sizeof (c->url)) ;
1902 if (chunksize > WAV_CART_MIN_CHUNK_SIZE)
1903 { /* File has tag text. */
1904 c->tag_text_size = chunksize - WAV_CART_MIN_CHUNK_SIZE ;
1905 bytes += psf_binheader_readf (psf, "b", c->tag_text, make_size_t (c->tag_text_size)) ;
1909 } /* wav_read_cart_chunk */
1912 wav_write_cart_chunk (SF_PRIVATE *psf)
1913 { SF_CART_INFO_16K *c ;
1916 if (psf->cart_16k == NULL)
1920 psf_binheader_writef (psf, "m4", cart_MARKER, WAV_CART_MIN_CHUNK_SIZE + c->tag_text_size) ;
1922 ** Note that it is very important that the field widths of the SF_CART_INFO
1923 ** struct match those of the cart chunk fields.
1925 psf_binheader_writef (psf, "b", c->version, sizeof (c->version)) ;
1926 psf_binheader_writef (psf, "b", c->title, sizeof (c->title)) ;
1927 psf_binheader_writef (psf, "b", c->artist, sizeof (c->artist)) ;
1928 psf_binheader_writef (psf, "b", c->cut_id, sizeof (c->cut_id)) ;
1929 psf_binheader_writef (psf, "b", c->client_id, sizeof (c->client_id)) ;
1930 psf_binheader_writef (psf, "b", c->category, sizeof (c->category)) ;
1931 psf_binheader_writef (psf, "b", c->classification, sizeof (c->classification)) ;
1932 psf_binheader_writef (psf, "b", c->out_cue, sizeof (c->out_cue)) ;
1933 psf_binheader_writef (psf, "b", c->start_date, sizeof (c->start_date)) ;
1934 psf_binheader_writef (psf, "b", c->start_time, sizeof (c->start_time)) ;
1935 psf_binheader_writef (psf, "b", c->end_date, sizeof (c->end_date)) ;
1936 psf_binheader_writef (psf, "b", c->end_time, sizeof (c->end_time)) ;
1937 psf_binheader_writef (psf, "b", c->producer_app_id, sizeof (c->producer_app_id)) ;
1938 psf_binheader_writef (psf, "b", c->producer_app_version, sizeof (c->producer_app_version)) ;
1939 psf_binheader_writef (psf, "b", c->user_def, sizeof (c->user_def)) ;
1940 psf_binheader_writef (psf, "4", c->level_reference, sizeof (c->level_reference)) ;
1942 for (k = 0 ; k < ARRAY_LEN (c->post_timers) ; k++)
1943 psf_binheader_writef (psf, "b4", c->post_timers [k].usage, make_size_t (4), c->post_timers [k].value) ;
1945 psf_binheader_writef (psf, "z", sizeof (c->reserved)) ; // just write zeros, we don't have any other use for it
1946 psf_binheader_writef (psf, "b", c->url, sizeof (c->url)) ;
1948 if (c->tag_text_size > 0)
1949 psf_binheader_writef (psf, "b", c->tag_text, make_size_t (c->tag_text_size)) ;
1952 } /* wav_write_cart_chunk */
1955 exif_fill_and_sink (SF_PRIVATE *psf, char* buf, size_t bufsz, size_t toread)
1957 size_t bytesread = 0 ;
1963 bytesread = psf_binheader_readf (psf, "b", buf, bufsz) ;
1966 if (bytesread == bufsz && toread > bufsz)
1967 bytesread += psf_binheader_readf (psf, "j", toread - bufsz) ;
1970 } /* exif_fill_and_sink */
1973 ** Exif specification for audio files, at JEITA CP-3451 Exif 2.2 section 5
1974 ** (Exif Audio File Specification) http://www.exif.org/Exif2-2.PDF
1977 exif_subchunk_parse (SF_PRIVATE *psf, uint32_t length)
1978 { uint32_t marker, dword, vmajor = -1, vminor = -1, bytesread = 0 ;
1982 while (bytesread < length)
1984 if ((thisread = psf_binheader_readf (psf, "m", &marker)) == 0)
1986 bytesread += thisread ;
1990 case 0 : /* camera padding? */
1994 bytesread += psf_binheader_readf (psf, "j4", 4, &dword) ;
1995 vmajor = 10 * (((dword >> 24) & 0xff) - '0') + (((dword >> 16) & 0xff) - '0') ;
1996 vminor = 10 * (((dword >> 8) & 0xff) - '0') + ((dword & 0xff) - '0') ;
1997 psf_log_printf (psf, " EXIF Version : %u.%02u\n", vmajor, vminor) ;
2001 bytesread += psf_binheader_readf (psf, "4", &dword) ;
2002 psf_log_printf (psf, "%M : %u\n", marker, dword) ;
2003 if (bytesread + dword > length)
2005 dword += (dword & 1) ;
2006 bytesread += psf_binheader_readf (psf, "j", dword) ;
2009 case emnt_MARKER : /* design information: null-terminated string */
2010 case emdl_MARKER : /* model name ; null-terminated string */
2011 case ecor_MARKER : /* manufacturer: null-terminated string */
2012 case etim_MARKER : /* creation time: null-terminated string in the format "hour:minute:second.subsecond" */
2013 case erel_MARKER : /* relation info: null-terminated string (filename) */
2014 case eucm_MARKER : /* user comment: 4-byte size follows, then possibly unicode data */
2015 bytesread += psf_binheader_readf (psf, "4", &dword) ;
2016 bytesread += sizeof (dword) ;
2017 dword += (dword & 1) ;
2019 if (dword >= sizeof (buf))
2020 { psf_log_printf (psf, "*** Marker '%M' is too big %u\n\n", marker, dword) ;
2024 bytesread += exif_fill_and_sink (psf, buf, sizeof (buf), dword) ;
2026 /* BAD - don't know what's going on here -- maybe a bug in the camera */
2027 /* field should be NULL-terminated but there's no room for it with the reported number */
2028 /* example output: emdl : 8 (EX-Z1050) */
2029 if (marker == emdl_MARKER && dword == strlen (buf) /* should be >= strlen+1*/)
2030 { psf_log_printf (psf, " *** field size too small for string (sinking 2 bytes)\n") ;
2031 bytesread += psf_binheader_readf (psf, "j", 2) ;
2034 psf_log_printf (psf, " %M : %u (%s)\n", marker, dword, buf) ;
2040 psf_log_printf (psf, " *** %M (%u): -- ignored --\n", marker, marker) ;
2046 } /* exif_subchunk_parse */
2048 /*==============================================================================
2052 wav_set_chunk (SF_PRIVATE *psf, const SF_CHUNK_INFO * chunk_info)
2053 { return psf_save_write_chunk (&psf->wchunks, chunk_info) ;
2054 } /* wav_set_chunk */
2056 static SF_CHUNK_ITERATOR *
2057 wav_next_chunk_iterator (SF_PRIVATE *psf, SF_CHUNK_ITERATOR * iterator)
2058 { return psf_next_chunk_iterator (&psf->rchunks, iterator) ;
2059 } /* wav_next_chunk_iterator */
2062 wav_get_chunk_size (SF_PRIVATE *psf, const SF_CHUNK_ITERATOR * iterator, SF_CHUNK_INFO * chunk_info)
2065 if ((indx = psf_find_read_chunk_iterator (&psf->rchunks, iterator)) < 0)
2066 return SFE_UNKNOWN_CHUNK ;
2068 chunk_info->datalen = psf->rchunks.chunks [indx].len ;
2070 return SFE_NO_ERROR ;
2071 } /* wav_get_chunk_size */
2074 wav_get_chunk_data (SF_PRIVATE *psf, const SF_CHUNK_ITERATOR * iterator, SF_CHUNK_INFO * chunk_info)
2078 if ((indx = psf_find_read_chunk_iterator (&psf->rchunks, iterator)) < 0)
2079 return SFE_UNKNOWN_CHUNK ;
2081 if (chunk_info->data == NULL)
2082 return SFE_BAD_CHUNK_DATA_PTR ;
2084 chunk_info->id_size = psf->rchunks.chunks [indx].id_size ;
2085 memcpy (chunk_info->id, psf->rchunks.chunks [indx].id, sizeof (chunk_info->id) / sizeof (*chunk_info->id)) ;
2087 pos = psf_ftell (psf) ;
2088 psf_fseek (psf, psf->rchunks.chunks [indx].offset, SEEK_SET) ;
2089 psf_fread (chunk_info->data, SF_MIN (chunk_info->datalen, psf->rchunks.chunks [indx].len), 1, psf) ;
2090 psf_fseek (psf, pos, SEEK_SET) ;
2092 return SFE_NO_ERROR ;
2093 } /* wav_get_chunk_data */