allow libsndfile to open BWF files; fix -Woverload-virtuals being in CCFLAGS instead...
[ardour.git] / libs / libsndfile / src / wav.c
1 /*
2 ** Copyright (C) 1999-2006 Erik de Castro Lopo <erikd@mega-nerd.com>
3 ** Copyright (C) 2004-2005 David Viens <davidv@plogue.com>
4 **
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.
9 **
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.
14 **
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.
18 */
19
20 #include        "sfconfig.h"
21
22 #include        <stdio.h>
23 #include        <stdlib.h>
24 #include        <string.h>
25 #include        <ctype.h>
26 #include        <time.h>
27
28 #include        "sndfile.h"
29 #include        "sfendian.h"
30 #include        "common.h"
31 #include        "wav_w64.h"
32
33 /*------------------------------------------------------------------------------
34  * Macros to handle big/little endian issues.
35  */
36
37 #define RIFF_MARKER      (MAKE_MARKER ('R', 'I', 'F', 'F'))
38 #define RIFX_MARKER      (MAKE_MARKER ('R', 'I', 'F', 'X'))
39 #define WAVE_MARKER      (MAKE_MARKER ('W', 'A', 'V', 'E'))
40 #define fmt_MARKER       (MAKE_MARKER ('f', 'm', 't', ' '))
41 #define data_MARKER      (MAKE_MARKER ('d', 'a', 't', 'a'))
42 #define fact_MARKER      (MAKE_MARKER ('f', 'a', 'c', 't'))
43 #define PEAK_MARKER      (MAKE_MARKER ('P', 'E', 'A', 'K'))
44
45 #define cue_MARKER       (MAKE_MARKER ('c', 'u', 'e', ' '))
46 #define LIST_MARKER      (MAKE_MARKER ('L', 'I', 'S', 'T'))
47 #define slnt_MARKER      (MAKE_MARKER ('s', 'l', 'n', 't'))
48 #define wavl_MARKER      (MAKE_MARKER ('w', 'a', 'v', 'l'))
49 #define INFO_MARKER      (MAKE_MARKER ('I', 'N', 'F', 'O'))
50 #define plst_MARKER      (MAKE_MARKER ('p', 'l', 's', 't'))
51 #define adtl_MARKER      (MAKE_MARKER ('a', 'd', 't', 'l'))
52 #define labl_MARKER      (MAKE_MARKER ('l', 'a', 'b', 'l'))
53 #define ltxt_MARKER      (MAKE_MARKER ('l', 't', 'x', 't'))
54 #define note_MARKER      (MAKE_MARKER ('n', 'o', 't', 'e'))
55 #define smpl_MARKER      (MAKE_MARKER ('s', 'm', 'p', 'l'))
56 #define bext_MARKER      (MAKE_MARKER ('b', 'e', 'x', 't'))
57 #define levl_MARKER      (MAKE_MARKER ('l', 'e', 'v', 'l'))
58 #define MEXT_MARKER      (MAKE_MARKER ('M', 'E', 'X', 'T'))
59 #define DISP_MARKER      (MAKE_MARKER ('D', 'I', 'S', 'P'))
60 #define acid_MARKER      (MAKE_MARKER ('a', 'c', 'i', 'd'))
61 #define strc_MARKER      (MAKE_MARKER ('s', 't', 'r', 'c'))
62 #define PAD_MARKER       (MAKE_MARKER ('P', 'A', 'D', ' '))
63 #define afsp_MARKER      (MAKE_MARKER ('a', 'f', 's', 'p'))
64 #define clm_MARKER       (MAKE_MARKER ('c', 'l', 'm', ' '))
65 #define elmo_MARKER      (MAKE_MARKER ('e', 'l', 'm', 'o'))
66
67 #define ISFT_MARKER      (MAKE_MARKER ('I', 'S', 'F', 'T'))
68 #define ICRD_MARKER      (MAKE_MARKER ('I', 'C', 'R', 'D'))
69 #define ICOP_MARKER      (MAKE_MARKER ('I', 'C', 'O', 'P'))
70 #define IARL_MARKER      (MAKE_MARKER ('I', 'A', 'R', 'L'))
71 #define IART_MARKER      (MAKE_MARKER ('I', 'A', 'R', 'T'))
72 #define INAM_MARKER      (MAKE_MARKER ('I', 'N', 'A', 'M'))
73 #define IENG_MARKER      (MAKE_MARKER ('I', 'E', 'N', 'G'))
74 #define IART_MARKER      (MAKE_MARKER ('I', 'A', 'R', 'T'))
75 #define ICOP_MARKER      (MAKE_MARKER ('I', 'C', 'O', 'P'))
76 #define IPRD_MARKER      (MAKE_MARKER ('I', 'P', 'R', 'D'))
77 #define ISRC_MARKER      (MAKE_MARKER ('I', 'S', 'R', 'C'))
78 #define ISBJ_MARKER      (MAKE_MARKER ('I', 'S', 'B', 'J'))
79 #define ICMT_MARKER      (MAKE_MARKER ('I', 'C', 'M', 'T'))
80
81 /* Weird WAVPACK marker which can show up at the start of the DATA section. */
82 #define wvpk_MARKER (MAKE_MARKER ('w', 'v', 'p', 'k'))
83 #define OggS_MARKER (MAKE_MARKER ('O', 'g', 'g', 'S'))
84
85 #define WAV_PEAK_CHUNK_SIZE(ch)         (2 * sizeof (int) + ch * (sizeof (float) + sizeof (int)))
86 #define WAV_BEXT_CHUNK_SIZE                     602
87
88 enum
89 {       HAVE_RIFF       = 0x01,
90         HAVE_WAVE       = 0x02,
91         HAVE_fmt        = 0x04,
92         HAVE_fact       = 0x08,
93         HAVE_PEAK       = 0x10,
94         HAVE_data       = 0x20,
95         HAVE_other      = 0x80000000
96 } ;
97
98
99
100 /*  known WAVEFORMATEXTENSIBLE GUIDS  */
101 static const EXT_SUBFORMAT MSGUID_SUBTYPE_PCM =
102 {       0x00000001, 0x0000, 0x0010, {   0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }
103 } ;
104
105 static const EXT_SUBFORMAT MSGUID_SUBTYPE_MS_ADPCM =
106 {       0x00000002, 0x0000, 0x0010, {   0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }
107 } ;
108
109 static const EXT_SUBFORMAT MSGUID_SUBTYPE_IEEE_FLOAT =
110 {       0x00000003, 0x0000, 0x0010, {   0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }
111 } ;
112
113 static const EXT_SUBFORMAT MSGUID_SUBTYPE_ALAW =
114 {       0x00000006, 0x0000, 0x0010, {   0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }
115 } ;
116
117 static const EXT_SUBFORMAT MSGUID_SUBTYPE_MULAW =
118 {       0x00000007, 0x0000, 0x0010, {   0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }
119 } ;
120
121 /*
122 ** the next two are from
123 ** http://dream.cs.bath.ac.uk/researchdev/wave-ex/bformat.html
124 */
125 static const EXT_SUBFORMAT MSGUID_SUBTYPE_AMBISONIC_B_FORMAT_PCM =
126 {       0x00000001, 0x0721, 0x11d3, {   0x86, 0x44, 0xC8, 0xC1, 0xCA, 0x00, 0x00, 0x00 }
127 } ;
128
129 static const EXT_SUBFORMAT MSGUID_SUBTYPE_AMBISONIC_B_FORMAT_IEEE_FLOAT =
130 {       0x00000003, 0x0721, 0x11d3, {   0x86, 0x44, 0xC8, 0xC1, 0xCA, 0x00, 0x00, 0x00 }
131 } ;
132
133
134 #if 0
135 /* maybe interesting one day to read the following through sf_read_raw */
136 /* http://www.bath.ac.uk/~masrwd/pvocex/pvocex.html */
137 static const EXT_SUBFORMAT MSGUID_SUBTYPE_PVOCEX =
138 {       0x8312B9C2, 0x2E6E, 0x11d4, {   0xA8, 0x24, 0xDE, 0x5B, 0x96, 0xC3, 0xAB, 0x21 }
139 } ;
140 #endif
141
142 /*------------------------------------------------------------------------------
143 ** Private static functions.
144 */
145
146 static int      wav_read_header  (SF_PRIVATE *psf, int *blockalign, int *framesperblock) ;
147 static int      wav_write_header (SF_PRIVATE *psf, int calc_length) ;
148
149 static int      wavex_write_header (SF_PRIVATE *psf, int calc_length) ;
150
151 static int      wav_write_tailer (SF_PRIVATE *psf) ;
152 static void wav_write_strings (SF_PRIVATE *psf, int location) ;
153 static int      wav_command (SF_PRIVATE *psf, int command, void *data, int datasize) ;
154 static int      wav_close (SF_PRIVATE *psf) ;
155
156 static int      wav_subchunk_parse       (SF_PRIVATE *psf, int chunk) ;
157 static int      wav_read_smpl_chunk (SF_PRIVATE *psf, unsigned int chunklen) ;
158 static int      wav_read_acid_chunk (SF_PRIVATE *psf, unsigned int chunklen) ;
159 static int      wav_read_bext_chunk (SF_PRIVATE *psf, unsigned int chunklen) ;
160 static int      wav_write_bext_chunk (SF_PRIVATE *psf) ;
161
162 /*------------------------------------------------------------------------------
163 ** Public function.
164 */
165
166 int
167 wav_open         (SF_PRIVATE *psf)
168 {       int     format, subformat, error, blockalign = 0, framesperblock = 0 ;
169
170         if (psf->mode == SFM_READ || (psf->mode == SFM_RDWR && psf->filelength > 0))
171         {       if ((error = wav_read_header (psf, &blockalign, &framesperblock)))
172                         return error ;
173                 } ;
174
175         subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
176
177         if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
178         {       if (psf->is_pipe)
179                         return SFE_NO_PIPE_WRITE ;
180
181                 format = psf->sf.format & SF_FORMAT_TYPEMASK ;
182                 if (format != SF_FORMAT_WAV && format != SF_FORMAT_WAVEX)
183                         return  SFE_BAD_OPEN_FORMAT ;
184
185                 psf->blockwidth = psf->bytewidth * psf->sf.channels ;
186
187                 /* RIFF WAVs are little-endian, RIFX WAVs are big-endian, default to little */
188                 psf->endian = psf->sf.format & SF_FORMAT_ENDMASK ;
189                 if (CPU_IS_BIG_ENDIAN && psf->endian == SF_ENDIAN_CPU)
190                         psf->endian = SF_ENDIAN_BIG ;
191                 else if (psf->endian != SF_ENDIAN_BIG)
192                         psf->endian = SF_ENDIAN_LITTLE ;
193
194                 if (psf->mode != SFM_RDWR || psf->filelength < 44)
195                 {       psf->filelength = 0 ;
196                         psf->datalength = 0 ;
197                         psf->dataoffset = 0 ;
198                         psf->sf.frames = 0 ;
199                         } ;
200
201                 if (subformat == SF_FORMAT_IMA_ADPCM || subformat == SF_FORMAT_MS_ADPCM)
202                 {       blockalign = wav_w64_srate2blocksize (psf->sf.samplerate * psf->sf.channels) ;
203                         framesperblock = -1 ; /* Corrected later. */
204                         } ;
205
206                 psf->str_flags = SF_STR_ALLOW_START | SF_STR_ALLOW_END ;
207
208                 /* By default, add the peak chunk to floating point files. Default behaviour
209                 ** can be switched off using sf_command (SFC_SET_PEAK_CHUNK, SF_FALSE).
210                 */
211                 if (psf->mode == SFM_WRITE && (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE))
212                 {       if ((psf->peak_info = peak_info_calloc (psf->sf.channels)) == NULL)
213                                 return SFE_MALLOC_FAILED ;
214                         psf->peak_info->peak_loc = SF_PEAK_START ;
215                         } ;
216
217                 psf->write_header = (format == SF_FORMAT_WAV) ? wav_write_header : wavex_write_header ;
218                 } ;
219
220         psf->container_close = wav_close ;
221         psf->command = wav_command ;
222
223         switch (subformat)
224         {       case SF_FORMAT_PCM_U8 :
225                 case SF_FORMAT_PCM_16 :
226                 case SF_FORMAT_PCM_24 :
227                 case SF_FORMAT_PCM_32 :
228                                         error = pcm_init (psf) ;
229                                         break ;
230
231                 case SF_FORMAT_ULAW :
232                                         error = ulaw_init (psf) ;
233                                         break ;
234
235                 case SF_FORMAT_ALAW :
236                                         error = alaw_init (psf) ;
237                                         break ;
238
239                 /* Lite remove start */
240                 case SF_FORMAT_FLOAT :
241                                         error = float32_init (psf) ;
242                                         break ;
243
244                 case SF_FORMAT_DOUBLE :
245                                         error = double64_init (psf) ;
246                                         break ;
247
248                 case SF_FORMAT_IMA_ADPCM :
249                                         error = wav_w64_ima_init (psf, blockalign, framesperblock) ;
250                                         break ;
251
252                 case SF_FORMAT_MS_ADPCM :
253                                         error = wav_w64_msadpcm_init (psf, blockalign, framesperblock) ;
254                                         break ;
255
256                 case SF_FORMAT_G721_32 :
257                                         error = g72x_init (psf) ;
258                                         break ;
259                 /* Lite remove end */
260
261                 case SF_FORMAT_GSM610 :
262                                         error = gsm610_init (psf) ;
263                                         break ;
264
265                 default :       return SFE_UNIMPLEMENTED ;
266                 } ;
267
268         if (psf->mode == SFM_WRITE || (psf->mode == SFM_RDWR && psf->filelength == 0))
269                 return psf->write_header (psf, SF_FALSE) ;
270
271         return error ;
272 } /* wav_open */
273
274 /*=========================================================================
275 ** Private functions.
276 */
277
278 static int
279 wav_read_header  (SF_PRIVATE *psf, int *blockalign, int *framesperblock)
280 {       WAV_FMT         wav_fmt ;
281         FACT_CHUNK      fact_chunk ;
282         unsigned        dword = 0, marker, RIFFsize, done = 0 ;
283         int                     parsestage = 0, error, format = 0 ;
284         char            *cptr ;
285
286         memset (&wav_fmt, 0, sizeof (wav_fmt)) ;
287         /* Set position to start of file to begin reading header. */
288         psf_binheader_readf (psf, "p", 0) ;
289
290         while (! done)
291         {       psf_binheader_readf (psf, "m", &marker) ;
292
293                 switch (marker)
294                 {       case RIFF_MARKER :
295                         case RIFX_MARKER :
296                                         if (parsestage)
297                                                 return SFE_WAV_NO_RIFF ;
298
299                                         parsestage |= HAVE_RIFF ;
300
301                                         /* RIFX signifies big-endian format for all header and data
302                                         ** to prevent lots of code copying here, we'll set the psf->rwf_endian
303                                         ** flag once here, and never specify endian-ness for all other header ops
304                                         */
305                                         if (marker == RIFF_MARKER)
306                                                 psf->rwf_endian = SF_ENDIAN_LITTLE ;
307                                         else
308                                                 psf->rwf_endian = SF_ENDIAN_BIG ;
309
310                                         psf_binheader_readf (psf, "4", &RIFFsize) ;
311
312                                         if (psf->fileoffset > 0 && psf->filelength > RIFFsize + 8)
313                                         {       /* Set file length. */
314                                                 psf->filelength = RIFFsize + 8 ;
315                                                 if (marker == RIFF_MARKER)
316                                                         psf_log_printf (psf, "RIFF : %u\n", RIFFsize) ;
317                                                 else
318                                                         psf_log_printf (psf, "RIFX : %u\n", RIFFsize) ;
319                                                 }
320                                         else if (psf->filelength < RIFFsize + 2 * SIGNED_SIZEOF (dword))
321                                         {       if (marker == RIFF_MARKER)
322                                                         psf_log_printf (psf, "RIFF : %u (should be %D)\n", RIFFsize, psf->filelength - 2 * SIGNED_SIZEOF (dword)) ;
323                                                 else
324                                                         psf_log_printf (psf, "RIFX : %u (should be %D)\n", RIFFsize, psf->filelength - 2 * SIGNED_SIZEOF (dword)) ;
325
326                                                 RIFFsize = dword ;
327                                                 }
328                                         else
329                                         {       if (marker == RIFF_MARKER)
330                                                         psf_log_printf (psf, "RIFF : %u\n", RIFFsize) ;
331                                                 else
332                                                         psf_log_printf (psf, "RIFX : %u\n", RIFFsize) ;
333                                         } ;
334                                         break ;
335
336                         case WAVE_MARKER :
337                                         if ((parsestage & HAVE_RIFF) != HAVE_RIFF)
338                                                 return SFE_WAV_NO_WAVE ;
339                                         parsestage |= HAVE_WAVE ;
340
341                                         psf_log_printf (psf, "WAVE\n") ;
342                                         break ;
343
344                         case fmt_MARKER :
345                                         if ((parsestage & (HAVE_RIFF | HAVE_WAVE)) != (HAVE_RIFF | HAVE_WAVE))
346                                                 return SFE_WAV_NO_FMT ;
347
348                                         /* If this file has a SECOND fmt chunk, I don't want to know about it. */
349                                         if (parsestage & HAVE_fmt)
350                                                 break ;
351
352                                         parsestage |= HAVE_fmt ;
353
354                                         psf_binheader_readf (psf, "4", &dword) ;
355                                         psf_log_printf (psf, "fmt  : %d\n", dword) ;
356
357                                         if ((error = wav_w64_read_fmt_chunk (psf, &wav_fmt, dword)))
358                                                 return error ;
359
360                                         format = wav_fmt.format ;
361                                         break ;
362
363                         case data_MARKER :
364                                         if ((parsestage & (HAVE_RIFF | HAVE_WAVE | HAVE_fmt)) != (HAVE_RIFF | HAVE_WAVE | HAVE_fmt))
365                                                 return SFE_WAV_NO_DATA ;
366
367                                         if (psf->mode == SFM_RDWR && (parsestage & HAVE_other) != 0)
368                                                 return SFE_RDWR_BAD_HEADER ;
369
370                                         parsestage |= HAVE_data ;
371
372                                         psf_binheader_readf (psf, "4", &dword) ;
373
374                                         psf->datalength = dword ;
375                                         psf->dataoffset = psf_ftell (psf) ;
376
377                                         if (dword == 0 && RIFFsize == 8 && psf->filelength > 44)
378                                         {       psf_log_printf (psf, "*** Looks like a WAV file which wasn't closed properly. Fixing it.\n") ;
379                                                 psf->datalength = dword = psf->filelength - psf->dataoffset ;
380                                                 } ;
381
382                                         if (psf->datalength > psf->filelength - psf->dataoffset)
383                                         {       psf_log_printf (psf, "data : %D (should be %D)\n", psf->datalength, psf->filelength - psf->dataoffset) ;
384                                                 psf->datalength = psf->filelength - psf->dataoffset ;
385                                                 }
386                                         else
387                                                 psf_log_printf (psf, "data : %D\n", psf->datalength) ;
388
389                                         /* Only set dataend if there really is data at the end. */
390                                         if (psf->datalength + psf->dataoffset < psf->filelength)
391                                                 psf->dataend = psf->datalength + psf->dataoffset ;
392
393                                         if (format == WAVE_FORMAT_MS_ADPCM && psf->datalength % 2)
394                                         {       psf->datalength ++ ;
395                                                 psf_log_printf (psf, "*** Data length odd. Increasing it by 1.\n") ;
396                                                 } ;
397
398                                         if (! psf->sf.seekable)
399                                                 break ;
400
401                                         /* Seek past data and continue reading header. */
402                                         psf_fseek (psf, psf->datalength, SEEK_CUR) ;
403
404                                         dword = psf_ftell (psf) ;
405                                         if (dword != (sf_count_t) (psf->dataoffset + psf->datalength))
406                                                 psf_log_printf (psf, "*** psf_fseek past end error ***\n", dword, psf->dataoffset + psf->datalength) ;
407                                         break ;
408
409                         case fact_MARKER :
410                                         if ((parsestage & (HAVE_RIFF | HAVE_WAVE)) != (HAVE_RIFF | HAVE_WAVE))
411                                                 return SFE_WAV_BAD_FACT ;
412
413                                         parsestage |= HAVE_fact ;
414
415                                         if ((parsestage & HAVE_fmt) != HAVE_fmt)
416                                                 psf_log_printf (psf, "*** Should have 'fmt ' chunk before 'fact'\n") ;
417
418                                         psf_binheader_readf (psf, "44", &dword, & (fact_chunk.frames)) ;
419
420                                         if (dword > SIGNED_SIZEOF (fact_chunk))
421                                                 psf_binheader_readf (psf, "j", (int) (dword - SIGNED_SIZEOF (fact_chunk))) ;
422
423                                         if (dword)
424                                                 psf_log_printf (psf, "%M : %d\n", marker, dword) ;
425                                         else
426                                                 psf_log_printf (psf, "%M : %d (should not be zero)\n", marker, dword) ;
427
428                                         psf_log_printf (psf, "  frames  : %d\n", fact_chunk.frames) ;
429                                         break ;
430
431                         case PEAK_MARKER :
432                                         if ((parsestage & (HAVE_RIFF | HAVE_WAVE | HAVE_fmt)) != (HAVE_RIFF | HAVE_WAVE | HAVE_fmt))
433                                                 return SFE_WAV_PEAK_B4_FMT ;
434
435                                         parsestage |= HAVE_PEAK ;
436
437                                         psf_binheader_readf (psf, "4", &dword) ;
438
439                                         psf_log_printf (psf, "%M : %d\n", marker, dword) ;
440                                         if (dword != WAV_PEAK_CHUNK_SIZE (psf->sf.channels))
441                                         {       psf_binheader_readf (psf, "j", dword) ;
442                                                 psf_log_printf (psf, "*** File PEAK chunk size doesn't fit with number of channels (%d).\n", psf->sf.channels) ;
443                                                 return SFE_WAV_BAD_PEAK ;
444                                                 } ;
445
446                                         if ((psf->peak_info = peak_info_calloc (psf->sf.channels)) == NULL)
447                                                 return SFE_MALLOC_FAILED ;
448
449                                         /* read in rest of PEAK chunk. */
450                                         psf_binheader_readf (psf, "44", & (psf->peak_info->version), & (psf->peak_info->timestamp)) ;
451
452                                         if (psf->peak_info->version != 1)
453                                                 psf_log_printf (psf, "  version    : %d *** (should be version 1)\n", psf->peak_info->version) ;
454                                         else
455                                                 psf_log_printf (psf, "  version    : %d\n", psf->peak_info->version) ;
456
457                                         psf_log_printf (psf, "  time stamp : %d\n", psf->peak_info->timestamp) ;
458                                         psf_log_printf (psf, "    Ch   Position       Value\n") ;
459
460                                         cptr = psf->u.cbuf ;
461                                         for (dword = 0 ; dword < (unsigned) psf->sf.channels ; dword++)
462                                         {       float value ;
463                                                 unsigned int position ;
464                                                 psf_binheader_readf (psf, "f4", &value, &position) ;
465                                                 psf->peak_info->peaks [dword].value = value ;
466                                                 psf->peak_info->peaks [dword].position = position ;
467
468                                                 LSF_SNPRINTF (cptr, sizeof (psf->u.cbuf), "    %2d   %-12ld   %g\n",
469                                                                 dword, (long) psf->peak_info->peaks [dword].position, psf->peak_info->peaks [dword].value) ;
470                                                 cptr [sizeof (psf->u.cbuf) - 1] = 0 ;
471                                                 psf_log_printf (psf, cptr) ;
472                                                 } ;
473
474                                         psf->peak_info->peak_loc = ((parsestage & HAVE_data) == 0) ? SF_PEAK_START : SF_PEAK_END ;
475                                         break ;
476
477                         case cue_MARKER :
478                                         parsestage |= HAVE_other ;
479
480                                         {       unsigned bytesread, cue_count ;
481                                                 int id, position, chunk_id, chunk_start, block_start, offset ;
482
483                                                 bytesread = psf_binheader_readf (psf, "44", &dword, &cue_count) ;
484                                                 bytesread -= 4 ; /* Remove bytes for first dword. */
485                                                 psf_log_printf (psf, "%M : %u\n", marker, dword) ;
486
487                                                 if (cue_count > 10)
488                                                 {       psf_log_printf (psf, "  Count : %d (skipping)\n", cue_count) ;
489                                                         psf_binheader_readf (psf, "j", cue_count * 24) ;
490                                                         break ;
491                                                         } ;
492
493                                                 psf_log_printf (psf, "  Count : %d\n", cue_count) ;
494
495                                                 while (cue_count)
496                                                 {       bytesread += psf_binheader_readf (psf, "444444", &id, &position,
497                                                                         &chunk_id, &chunk_start, &block_start, &offset) ;
498                                                         psf_log_printf (psf, "   Cue ID : %2d"
499                                                                                                  "  Pos : %5u  Chunk : %M"
500                                                                                                  "  Chk Start : %d  Blk Start : %d"
501                                                                                                  "  Offset : %5d\n",
502                                                                         id, position, chunk_id, chunk_start, block_start, offset) ;
503                                                         cue_count -- ;
504                                                         } ;
505
506                                                 if (bytesread != dword)
507                                                 {       psf_log_printf (psf, "**** Chunk size weirdness (%d != %d)\n", dword, bytesread) ;
508                                                         psf_binheader_readf (psf, "j", dword - bytesread) ;
509                                                         } ;
510                                                 } ;
511                                         break ;
512
513                         case smpl_MARKER :
514                                         parsestage |= HAVE_other ;
515
516                                         psf_binheader_readf (psf, "4", &dword) ;
517                                         psf_log_printf (psf, "smpl : %u\n", dword) ;
518
519                                         if ((error = wav_read_smpl_chunk (psf, dword)))
520                                                 return error ;
521                                         break ;
522
523                         case acid_MARKER :
524                                         parsestage |= HAVE_other ;
525
526                                         psf_binheader_readf (psf, "4", &dword) ;
527                                         psf_log_printf (psf, "acid : %u\n", dword) ;
528
529                                         if ((error = wav_read_acid_chunk (psf, dword)))
530                                                 return error ;
531                                         break ;
532
533                         case INFO_MARKER :
534                         case LIST_MARKER :
535                                         parsestage |= HAVE_other ;
536
537                                         if ((error = wav_subchunk_parse (psf, marker)) != 0)
538                                                 return error ;
539                                         break ;
540
541                         case bext_MARKER :
542
543                                         psf_binheader_readf (psf, "4", &dword) ;
544                                         if (dword < WAV_BEXT_CHUNK_SIZE)
545                                                 psf_log_printf (psf, "bext : %u (should be >= %d)\n", dword, WAV_BEXT_CHUNK_SIZE) ;
546                                         else
547                                                 psf_log_printf (psf, "bext : %u\n", dword) ;
548
549                                         if ((error = wav_read_bext_chunk (psf, dword)))
550                                                 return error ;
551                                         break ;
552
553                         case strc_MARKER : /* Multiple of 32 bytes. */
554
555                         case afsp_MARKER :
556                         case clm_MARKER :
557                         case elmo_MARKER :
558                         case levl_MARKER :
559                         case plst_MARKER :
560                         case DISP_MARKER :
561                         case MEXT_MARKER :
562                         case PAD_MARKER :
563                                         parsestage |= HAVE_other ;
564
565                                         psf_binheader_readf (psf, "4", &dword) ;
566                                         psf_log_printf (psf, "%M : %u\n", marker, dword) ;
567                                         dword += (dword & 1) ;
568                                         psf_binheader_readf (psf, "j", dword) ;
569                                         break ;
570
571                         default :
572                                         parsestage |= HAVE_other ;
573                                         if (isprint ((marker >> 24) & 0xFF) && isprint ((marker >> 16) & 0xFF)
574                                                 && isprint ((marker >> 8) & 0xFF) && isprint (marker & 0xFF))
575                                         {       psf_binheader_readf (psf, "4", &dword) ;
576                                                 psf_log_printf (psf, "*** %M : %d (unknown marker)\n", marker, dword) ;
577                                                 psf_binheader_readf (psf, "j", dword) ;
578                                                 break ;
579                                                 } ;
580                                         if (psf_ftell (psf) & 0x03)
581                                         {       psf_log_printf (psf, "  Unknown chunk marker at position %d. Resynching.\n", dword - 4) ;
582                                                 psf_binheader_readf (psf, "j", -3) ;
583                                                 break ;
584                                                 } ;
585                                         psf_log_printf (psf, "*** Unknown chunk marker (%X) at position %D. Exiting parser.\n", marker, psf_ftell (psf) - 4) ;
586                                         done = SF_TRUE ;
587                                         break ;
588                         } ;     /* switch (dword) */
589
590                 if (! psf->sf.seekable && (parsestage & HAVE_data))
591                         break ;
592
593                 if (psf_ftell (psf) >= psf->filelength - SIGNED_SIZEOF (dword))
594                 {       psf_log_printf (psf, "End\n") ;
595                         break ;
596                         } ;
597                 } ; /* while (1) */
598
599         if (! psf->dataoffset)
600                 return SFE_WAV_NO_DATA ;
601
602         /* WAVs can be little or big endian */
603         psf->endian = psf->rwf_endian ;
604
605         psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
606
607         if (psf->is_pipe == 0)
608         {       /*
609                 ** Check for 'wvpk' at the start of the DATA section. Not able to
610                 ** handle this.
611                 */
612                 psf_binheader_readf (psf, "4", &marker) ;
613                 if (marker == wvpk_MARKER || marker == OggS_MARKER)
614                         return SFE_WAV_WVPK_DATA ;
615                 } ;
616
617         /* Seek to start of DATA section. */
618         psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
619
620         if (psf->blockwidth)
621         {       if (psf->filelength - psf->dataoffset < psf->datalength)
622                         psf->sf.frames = (psf->filelength - psf->dataoffset) / psf->blockwidth ;
623                 else
624                         psf->sf.frames = psf->datalength / psf->blockwidth ;
625                 } ;
626
627         switch (format)
628         {       case WAVE_FORMAT_EXTENSIBLE :
629                         if (psf->sf.format == (SF_FORMAT_WAVEX | SF_FORMAT_MS_ADPCM))
630                         {       *blockalign = wav_fmt.msadpcm.blockalign ;
631                                 *framesperblock = wav_fmt.msadpcm.samplesperblock ;
632                                 } ;
633                         break ;
634
635                 case WAVE_FORMAT_PCM :
636                                         psf->sf.format = SF_FORMAT_WAV | u_bitwidth_to_subformat (psf->bytewidth * 8) ;
637                                         break ;
638
639                 case WAVE_FORMAT_MULAW :
640                 case IBM_FORMAT_MULAW :
641                                         psf->sf.format = (SF_FORMAT_WAV | SF_FORMAT_ULAW) ;
642                                         break ;
643
644                 case WAVE_FORMAT_ALAW :
645                 case IBM_FORMAT_ALAW :
646                                         psf->sf.format = (SF_FORMAT_WAV | SF_FORMAT_ALAW) ;
647                                         break ;
648
649                 case WAVE_FORMAT_MS_ADPCM :
650                                         psf->sf.format = (SF_FORMAT_WAV | SF_FORMAT_MS_ADPCM) ;
651                                         *blockalign = wav_fmt.msadpcm.blockalign ;
652                                         *framesperblock = wav_fmt.msadpcm.samplesperblock ;
653                                         break ;
654
655                 case WAVE_FORMAT_IMA_ADPCM :
656                                         psf->sf.format = (SF_FORMAT_WAV | SF_FORMAT_IMA_ADPCM) ;
657                                         *blockalign = wav_fmt.ima.blockalign ;
658                                         *framesperblock = wav_fmt.ima.samplesperblock ;
659                                         break ;
660
661                 case WAVE_FORMAT_GSM610 :
662                                         psf->sf.format = (SF_FORMAT_WAV | SF_FORMAT_GSM610) ;
663                                         break ;
664
665                 case WAVE_FORMAT_IEEE_FLOAT :
666                                         psf->sf.format = SF_FORMAT_WAV ;
667                                         psf->sf.format |= (psf->bytewidth == 8) ? SF_FORMAT_DOUBLE : SF_FORMAT_FLOAT ;
668                                         break ;
669
670                 case WAVE_FORMAT_G721_ADPCM :
671                                         psf->sf.format = SF_FORMAT_WAV | SF_FORMAT_G721_32 ;
672                                         break ;
673
674                 default : return SFE_UNIMPLEMENTED ;
675                 } ;
676
677         /* Only set the format endian-ness if its non-standard big-endian. */
678         if (psf->endian == SF_ENDIAN_BIG)
679                 psf->sf.format |= SF_ENDIAN_BIG ;
680
681         return 0 ;
682 } /* wav_read_header */
683
684 static int
685 wav_write_header (SF_PRIVATE *psf, int calc_length)
686 {       sf_count_t      current ;
687         int             fmt_size, k, subformat, add_fact_chunk = SF_FALSE ;
688
689         current = psf_ftell (psf) ;
690
691         if (calc_length)
692         {       psf->filelength = psf_get_filelen (psf) ;
693
694                 psf->datalength = psf->filelength - psf->dataoffset ;
695
696                 if (psf->dataend)
697                         psf->datalength -= psf->filelength - psf->dataend ;
698
699                 if (psf->bytewidth > 0)
700                         psf->sf.frames = psf->datalength / (psf->bytewidth * psf->sf.channels) ;
701                 } ;
702
703         /* Reset the current header length to zero. */
704         psf->header [0] = 0 ;
705         psf->headindex = 0 ;
706         psf_fseek (psf, 0, SEEK_SET) ;
707
708         /*
709         ** RIFX signifies big-endian format for all header and data.
710         ** To prevent lots of code copying here, we'll set the psf->rwf_endian flag
711         ** once here, and never specify endian-ness for all other header operations.
712         */
713
714         /* RIFF/RIFX marker, length, WAVE and 'fmt ' markers. */
715
716         if (psf->endian == SF_ENDIAN_LITTLE)
717                 psf_binheader_writef (psf, "etm8", RIFF_MARKER, (psf->filelength < 8) ? 8 : psf->filelength - 8) ;
718         else
719                 psf_binheader_writef (psf, "Etm8", RIFX_MARKER, (psf->filelength < 8) ? 8 : psf->filelength - 8) ;
720
721         /* WAVE and 'fmt ' markers. */
722         psf_binheader_writef (psf, "mm", WAVE_MARKER, fmt_MARKER) ;
723
724         subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
725
726         switch (subformat)
727         {       case SF_FORMAT_PCM_U8 :
728                 case SF_FORMAT_PCM_16 :
729                 case SF_FORMAT_PCM_24 :
730                 case SF_FORMAT_PCM_32 :
731                                         fmt_size = 2 + 2 + 4 + 4 + 2 + 2 ;
732
733                                         /* fmt : format, channels, samplerate */
734                                         psf_binheader_writef (psf, "4224", fmt_size, WAVE_FORMAT_PCM, psf->sf.channels, psf->sf.samplerate) ;
735                                         /*  fmt : bytespersec */
736                                         psf_binheader_writef (psf, "4", psf->sf.samplerate * psf->bytewidth * psf->sf.channels) ;
737                                         /*  fmt : blockalign, bitwidth */
738                                         psf_binheader_writef (psf, "22", psf->bytewidth * psf->sf.channels, psf->bytewidth * 8) ;
739                                         break ;
740
741                 case SF_FORMAT_FLOAT :
742                 case SF_FORMAT_DOUBLE :
743                                         fmt_size = 2 + 2 + 4 + 4 + 2 + 2 ;
744
745                                         /* fmt : format, channels, samplerate */
746                                         psf_binheader_writef (psf, "4224", fmt_size, WAVE_FORMAT_IEEE_FLOAT, psf->sf.channels, psf->sf.samplerate) ;
747                                         /*  fmt : bytespersec */
748                                         psf_binheader_writef (psf, "4", psf->sf.samplerate * psf->bytewidth * psf->sf.channels) ;
749                                         /*  fmt : blockalign, bitwidth */
750                                         psf_binheader_writef (psf, "22", psf->bytewidth * psf->sf.channels, psf->bytewidth * 8) ;
751
752                                         add_fact_chunk = SF_TRUE ;
753                                         break ;
754
755                 case SF_FORMAT_ULAW :
756                                         fmt_size = 2 + 2 + 4 + 4 + 2 + 2 ;
757
758                                         /* fmt : format, channels, samplerate */
759                                         psf_binheader_writef (psf, "4224", fmt_size, WAVE_FORMAT_MULAW, psf->sf.channels, psf->sf.samplerate) ;
760                                         /*  fmt : bytespersec */
761                                         psf_binheader_writef (psf, "4", psf->sf.samplerate * psf->bytewidth * psf->sf.channels) ;
762                                         /*  fmt : blockalign, bitwidth */
763                                         psf_binheader_writef (psf, "22", psf->bytewidth * psf->sf.channels, 8) ;
764
765                                         add_fact_chunk = SF_TRUE ;
766                                         break ;
767
768                 case SF_FORMAT_ALAW :
769                                         fmt_size = 2 + 2 + 4 + 4 + 2 + 2 ;
770
771                                         /* fmt : format, channels, samplerate */
772                                         psf_binheader_writef (psf, "4224", fmt_size, WAVE_FORMAT_ALAW, psf->sf.channels, psf->sf.samplerate) ;
773                                         /*  fmt : bytespersec */
774                                         psf_binheader_writef (psf, "4", psf->sf.samplerate * psf->bytewidth * psf->sf.channels) ;
775                                         /*  fmt : blockalign, bitwidth */
776                                         psf_binheader_writef (psf, "22", psf->bytewidth * psf->sf.channels, 8) ;
777
778                                         add_fact_chunk = SF_TRUE ;
779                                         break ;
780
781                 /* Lite remove start */
782                 case SF_FORMAT_IMA_ADPCM :
783                                         {       int blockalign, framesperblock, bytespersec ;
784
785                                                 blockalign              = wav_w64_srate2blocksize (psf->sf.samplerate * psf->sf.channels) ;
786                                                 framesperblock  = 2 * (blockalign - 4 * psf->sf.channels) / psf->sf.channels + 1 ;
787                                                 bytespersec             = (psf->sf.samplerate * blockalign) / framesperblock ;
788
789                                                 /* fmt chunk. */
790                                                 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 + 2 ;
791
792                                                 /* fmt : size, WAV format type, channels, samplerate, bytespersec */
793                                                 psf_binheader_writef (psf, "42244", fmt_size, WAVE_FORMAT_IMA_ADPCM,
794                                                                         psf->sf.channels, psf->sf.samplerate, bytespersec) ;
795
796                                                 /* fmt : blockalign, bitwidth, extrabytes, framesperblock. */
797                                                 psf_binheader_writef (psf, "2222", blockalign, 4, 2, framesperblock) ;
798                                                 } ;
799
800                                         add_fact_chunk = SF_TRUE ;
801                                         break ;
802
803                 case SF_FORMAT_MS_ADPCM :
804                                         {       int     blockalign, framesperblock, bytespersec, extrabytes ;
805
806                                                 blockalign              = wav_w64_srate2blocksize (psf->sf.samplerate * psf->sf.channels) ;
807                                                 framesperblock  = 2 + 2 * (blockalign - 7 * psf->sf.channels) / psf->sf.channels ;
808                                                 bytespersec             = (psf->sf.samplerate * blockalign) / framesperblock ;
809
810                                                 /* fmt chunk. */
811                                                 extrabytes      = 2 + 2 + MSADPCM_ADAPT_COEFF_COUNT * (2 + 2) ;
812                                                 fmt_size        = 2 + 2 + 4 + 4 + 2 + 2 + 2 + extrabytes ;
813
814                                                 /* fmt : size, WAV format type, channels. */
815                                                 psf_binheader_writef (psf, "422", fmt_size, WAVE_FORMAT_MS_ADPCM, psf->sf.channels) ;
816
817                                                 /* fmt : samplerate, bytespersec. */
818                                                 psf_binheader_writef (psf, "44", psf->sf.samplerate, bytespersec) ;
819
820                                                 /* fmt : blockalign, bitwidth, extrabytes, framesperblock. */
821                                                 psf_binheader_writef (psf, "22222", blockalign, 4, extrabytes, framesperblock, 7) ;
822
823                                                 msadpcm_write_adapt_coeffs (psf) ;
824                                                 } ;
825
826                                         add_fact_chunk = SF_TRUE ;
827                                         break ;
828
829
830                 case SF_FORMAT_G721_32 :
831                                         /* fmt chunk. */
832                                         fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 + 2 ;
833
834                                         /* fmt : size, WAV format type, channels, samplerate, bytespersec */
835                                         psf_binheader_writef (psf, "42244", fmt_size, WAVE_FORMAT_G721_ADPCM,
836                                                                 psf->sf.channels, psf->sf.samplerate, psf->sf.samplerate * psf->sf.channels / 2) ;
837
838                                         /* fmt : blockalign, bitwidth, extrabytes, auxblocksize. */
839                                         psf_binheader_writef (psf, "2222", 64, 4, 2, 0) ;
840
841                                         add_fact_chunk = SF_TRUE ;
842                                         break ;
843
844                 /* Lite remove end */
845
846                 case SF_FORMAT_GSM610 :
847                                         {       int     blockalign, framesperblock, bytespersec ;
848
849                                                 blockalign              = WAV_W64_GSM610_BLOCKSIZE ;
850                                                 framesperblock  = WAV_W64_GSM610_SAMPLES ;
851                                                 bytespersec             = (psf->sf.samplerate * blockalign) / framesperblock ;
852
853                                                 /* fmt chunk. */
854                                                 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 + 2 ;
855
856                                                 /* fmt : size, WAV format type, channels. */
857                                                 psf_binheader_writef (psf, "422", fmt_size, WAVE_FORMAT_GSM610, psf->sf.channels) ;
858
859                                                 /* fmt : samplerate, bytespersec. */
860                                                 psf_binheader_writef (psf, "44", psf->sf.samplerate, bytespersec) ;
861
862                                                 /* fmt : blockalign, bitwidth, extrabytes, framesperblock. */
863                                                 psf_binheader_writef (psf, "2222", blockalign, 0, 2, framesperblock) ;
864                                                 } ;
865
866                                         add_fact_chunk = SF_TRUE ;
867                                         break ;
868
869                 default :       return SFE_UNIMPLEMENTED ;
870                 } ;
871
872         if (add_fact_chunk)
873                 psf_binheader_writef (psf, "tm48", fact_MARKER, 4, psf->sf.frames) ;
874
875         if (psf->str_flags & SF_STR_LOCATE_START)
876                 wav_write_strings (psf, SF_STR_LOCATE_START) ;
877
878         if (psf->peak_info != NULL && psf->peak_info->peak_loc == SF_PEAK_START)
879         {       psf_binheader_writef (psf, "m4", PEAK_MARKER, WAV_PEAK_CHUNK_SIZE (psf->sf.channels)) ;
880                 psf_binheader_writef (psf, "44", 1, time (NULL)) ;
881                 for (k = 0 ; k < psf->sf.channels ; k++)
882                         psf_binheader_writef (psf, "ft8", (float) psf->peak_info->peaks [k].value, psf->peak_info->peaks [k].position) ;
883                 } ;
884
885         if (psf->broadcast_info != NULL)
886                 wav_write_bext_chunk (psf) ;
887
888         if (psf->instrument != NULL)
889         {       int             tmp ;
890                 double  dtune = (double) (0x40000000) / 25.0 ;
891
892                 psf_binheader_writef (psf, "m4", smpl_MARKER, 9 * 4 + psf->instrument->loop_count * 6 * 4) ;
893                 psf_binheader_writef (psf, "44", 0, 0) ; /* Manufacturer zero is everyone */
894                 tmp = (int) (1.0e9 / psf->sf.samplerate) ; /* Sample period in nano seconds */
895                 psf_binheader_writef (psf, "44", tmp, psf->instrument->basenote) ;
896                 tmp = (unsigned int) (psf->instrument->detune * dtune + 0.5) ;
897                 psf_binheader_writef (psf, "4", tmp) ;
898                 psf_binheader_writef (psf, "44", 0, 0) ; /* SMTPE format */
899                 psf_binheader_writef (psf, "44", psf->instrument->loop_count, 0) ;
900
901                 for (tmp = 0 ; tmp < psf->instrument->loop_count ; tmp++)
902                 {       int type ;
903
904                         type = psf->instrument->loops [tmp].mode ;
905                         type = (type == SF_LOOP_FORWARD ? 0 : type==SF_LOOP_BACKWARD ? 2 : type == SF_LOOP_ALTERNATING ? 1 : 32) ;
906
907                         psf_binheader_writef (psf, "44", tmp, type) ;
908                         psf_binheader_writef (psf, "44", psf->instrument->loops [tmp].start, psf->instrument->loops [tmp].end) ;
909                         psf_binheader_writef (psf, "44", 0, psf->instrument->loops [tmp].count) ;
910                         } ;
911                 } ;
912
913         psf_binheader_writef (psf, "tm8", data_MARKER, psf->datalength) ;
914         psf_fwrite (psf->header, psf->headindex, 1, psf) ;
915         if (psf->error)
916                 return psf->error ;
917
918         psf->dataoffset = psf->headindex ;
919
920         if (current < psf->dataoffset)
921                 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
922         else if (current > 0)
923                 psf_fseek (psf, current, SEEK_SET) ;
924
925         return psf->error ;
926 } /* wav_write_header */
927
928
929
930 static int
931 wavex_write_header (SF_PRIVATE *psf, int calc_length)
932 {       sf_count_t      current ;
933         int             fmt_size, k, subformat, add_fact_chunk = SF_FALSE ;
934
935         current = psf_ftell (psf) ;
936
937         if (calc_length)
938         {       psf->filelength = psf_get_filelen (psf) ;
939
940                 psf->datalength = psf->filelength - psf->dataoffset ;
941
942                 if (psf->dataend)
943                         psf->datalength -= psf->filelength - psf->dataend ;
944
945                 if (psf->bytewidth > 0)
946                         psf->sf.frames = psf->datalength / (psf->bytewidth * psf->sf.channels) ;
947                 } ;
948
949
950         /* Reset the current header length to zero. */
951         psf->header [0] = 0 ;
952         psf->headindex = 0 ;
953         psf_fseek (psf, 0, SEEK_SET) ;
954
955         /* RIFX signifies big-endian format for all header and data
956         ** to prevent lots of code copying here, we'll set the psf->rwf_endian
957         ** flag once here, and never specify endian-ness for all other header ops
958         */
959
960         /* RIFF marker, length, WAVE and 'fmt ' markers. */
961
962         if (psf->endian == SF_ENDIAN_LITTLE)
963         {       if (psf->filelength < 8)
964                         psf_binheader_writef (psf, "tm8", RIFF_MARKER, 8) ;
965                 else
966                         psf_binheader_writef (psf, "tm8", RIFF_MARKER, psf->filelength - 8) ;
967                 }
968         else
969         {       if (psf->filelength < 8)
970                         psf_binheader_writef (psf, "Etm8", RIFX_MARKER, 8) ;
971                 else
972                         psf_binheader_writef (psf, "Etm8", RIFX_MARKER, psf->filelength - 8) ;
973                 } ;
974
975         /* WAVE and 'fmt ' markers. */
976         psf_binheader_writef (psf, "mm", WAVE_MARKER, fmt_MARKER) ;
977
978         subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
979
980         /* initial section (same for all, it appears) */
981         switch (subformat)
982         {       case SF_FORMAT_PCM_U8 :
983                 case SF_FORMAT_PCM_16 :
984                 case SF_FORMAT_PCM_24 :
985                 case SF_FORMAT_PCM_32 :
986                 case SF_FORMAT_FLOAT :
987                 case SF_FORMAT_DOUBLE :
988                 case SF_FORMAT_ULAW :
989                 case SF_FORMAT_ALAW :
990                         fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 + 2 + 4 + 4 + 2 + 2 + 8 ;
991
992                         /* fmt : format, channels, samplerate */
993                         psf_binheader_writef (psf, "4224", fmt_size, WAVE_FORMAT_EXTENSIBLE, psf->sf.channels, psf->sf.samplerate) ;
994                         /*  fmt : bytespersec */
995                         psf_binheader_writef (psf, "4", psf->sf.samplerate * psf->bytewidth * psf->sf.channels) ;
996                         /*  fmt : blockalign, bitwidth */
997                         psf_binheader_writef (psf, "22", psf->bytewidth * psf->sf.channels, psf->bytewidth * 8) ;
998
999                         /* cbSize 22 is sizeof (WAVEFORMATEXTENSIBLE) - sizeof (WAVEFORMATEX) */
1000                         psf_binheader_writef (psf, "2", 22) ;
1001
1002                         /* wValidBitsPerSample, for our use same as bitwidth as we use it fully */
1003                         psf_binheader_writef (psf, "2", psf->bytewidth * 8) ;
1004
1005                         /*
1006                         ** Ok some liberty is taken here to use the most commonly used channel masks
1007                         ** instead of "no mapping". If you really want to use "no mapping" for 8 channels and less
1008                         ** please don't use wavex. (otherwise we'll have to create a new SF_COMMAND)
1009                         */
1010                         switch (psf->sf.channels)
1011                         {       case 1 :        /* center channel mono */
1012                                         psf_binheader_writef (psf, "4", 0x4) ;
1013                                         break ;
1014
1015                                 case 2 :        /* front left and right */
1016                                         psf_binheader_writef (psf, "4", 0x1 | 0x2) ;
1017                                         break ;
1018
1019                                 case 4 :        /* Quad */
1020                                         psf_binheader_writef (psf, "4", 0x1 | 0x2 | 0x10 | 0x20) ;
1021                                         break ;
1022
1023                                 case 6 :        /* 5.1 */
1024                                         psf_binheader_writef (psf, "4", 0x1 | 0x2 | 0x4 | 0x8 | 0x10 | 0x20) ;
1025                                         break ;
1026
1027                                 case 8 :        /* 7.1 */
1028                                         psf_binheader_writef (psf, "4", 0x1 | 0x2 | 0x4 | 0x8 | 0x10 | 0x20 | 0x40 | 0x80) ;
1029                                         break ;
1030
1031                                 default :       /* 0 when in doubt , use direct out, ie NO mapping*/
1032                                         psf_binheader_writef (psf, "4", 0x0) ;
1033                                         break ;
1034                                 }
1035
1036                         break ;
1037
1038                 case SF_FORMAT_MS_ADPCM : /* Todo, GUID exists might have different header as per wav_write_header */
1039                 default :
1040                         return SFE_UNIMPLEMENTED ;
1041                 } ;
1042
1043         /* GUID section, different for each */
1044
1045         switch (subformat)
1046         {       case SF_FORMAT_PCM_U8 :
1047                 case SF_FORMAT_PCM_16 :
1048                 case SF_FORMAT_PCM_24 :
1049                 case SF_FORMAT_PCM_32 :
1050                         wavex_write_guid (psf, &MSGUID_SUBTYPE_PCM) ;
1051                         break ;
1052
1053                 case SF_FORMAT_FLOAT :
1054                 case SF_FORMAT_DOUBLE :
1055                         wavex_write_guid (psf, &MSGUID_SUBTYPE_IEEE_FLOAT) ;
1056                         add_fact_chunk = SF_TRUE ;
1057                         break ;
1058
1059                 case SF_FORMAT_ULAW :
1060                         wavex_write_guid (psf, &MSGUID_SUBTYPE_MULAW) ;
1061                         add_fact_chunk = SF_TRUE ;
1062                         break ;
1063
1064                 case SF_FORMAT_ALAW :
1065                         wavex_write_guid (psf, &MSGUID_SUBTYPE_ALAW) ;
1066                         add_fact_chunk = SF_TRUE ;
1067                         break ;
1068
1069                 case SF_FORMAT_MS_ADPCM : /* todo, GUID exists */
1070
1071                 default : return SFE_UNIMPLEMENTED ;
1072                 } ;
1073
1074         if (add_fact_chunk)
1075                 psf_binheader_writef (psf, "tm48", fact_MARKER, 4, psf->sf.frames) ;
1076
1077         if (psf->str_flags & SF_STR_LOCATE_START)
1078                 wav_write_strings (psf, SF_STR_LOCATE_START) ;
1079
1080         if (psf->peak_info != NULL && psf->peak_info->peak_loc == SF_PEAK_START)
1081         {       psf_binheader_writef (psf, "m4", PEAK_MARKER, WAV_PEAK_CHUNK_SIZE (psf->sf.channels)) ;
1082                 psf_binheader_writef (psf, "44", 1, time (NULL)) ;
1083                 for (k = 0 ; k < psf->sf.channels ; k++)
1084                         psf_binheader_writef (psf, "ft8", (float) psf->peak_info->peaks [k].value, psf->peak_info->peaks [k].position) ;
1085                 } ;
1086
1087         psf_binheader_writef (psf, "tm8", data_MARKER, psf->datalength) ;
1088         psf_fwrite (psf->header, psf->headindex, 1, psf) ;
1089         if (psf->error)
1090                 return psf->error ;
1091
1092         psf->dataoffset = psf->headindex ;
1093
1094         if (current < psf->dataoffset)
1095                 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
1096         else if (current > 0)
1097                 psf_fseek (psf, current, SEEK_SET) ;
1098
1099         return psf->error ;
1100 } /* wavex_write_header */
1101
1102
1103
1104 static int
1105 wav_write_tailer (SF_PRIVATE *psf)
1106 {       int             k ;
1107
1108         /* Reset the current header buffer length to zero. */
1109         psf->header [0] = 0 ;
1110         psf->headindex = 0 ;
1111
1112         psf->dataend = psf_fseek (psf, 0, SEEK_END) ;
1113
1114         /* Add a PEAK chunk if requested. */
1115         if (psf->peak_info != NULL && psf->peak_info->peak_loc == SF_PEAK_END)
1116         {       psf_binheader_writef (psf, "m4", PEAK_MARKER, WAV_PEAK_CHUNK_SIZE (psf->sf.channels)) ;
1117                 psf_binheader_writef (psf, "44", 1, time (NULL)) ;
1118                 for (k = 0 ; k < psf->sf.channels ; k++)
1119                         psf_binheader_writef (psf, "f4", psf->peak_info->peaks [k].value, psf->peak_info->peaks [k].position) ;
1120                 } ;
1121
1122         if (psf->str_flags & SF_STR_LOCATE_END)
1123                 wav_write_strings (psf, SF_STR_LOCATE_END) ;
1124
1125         /* Write the tailer. */
1126         if (psf->headindex > 0)
1127                 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
1128
1129         return 0 ;
1130 } /* wav_write_tailer */
1131
1132 static void
1133 wav_write_strings (SF_PRIVATE *psf, int location)
1134 {       int     k, prev_head_index, saved_head_index ;
1135
1136         prev_head_index = psf->headindex + 4 ;
1137
1138         psf_binheader_writef (psf, "m4m", LIST_MARKER, 0xBADBAD, INFO_MARKER) ;
1139
1140         for (k = 0 ; k < SF_MAX_STRINGS ; k++)
1141         {       if (psf->strings [k].type == 0)
1142                         break ;
1143                 if (psf->strings [k].flags != location)
1144                         continue ;
1145
1146                 switch (psf->strings [k].type)
1147                 {       case SF_STR_SOFTWARE :
1148                                 psf_binheader_writef (psf, "ms", ISFT_MARKER, psf->strings [k].str) ;
1149                                 break ;
1150
1151                         case SF_STR_TITLE :
1152                                 psf_binheader_writef (psf, "ms", INAM_MARKER, psf->strings [k].str) ;
1153                                 break ;
1154
1155                         case SF_STR_COPYRIGHT :
1156                                 psf_binheader_writef (psf, "ms", ICOP_MARKER, psf->strings [k].str) ;
1157                                 break ;
1158
1159                         case SF_STR_ARTIST :
1160                                 psf_binheader_writef (psf, "ms", IART_MARKER, psf->strings [k].str) ;
1161                                 break ;
1162
1163                         case SF_STR_COMMENT :
1164                                 psf_binheader_writef (psf, "ms", ICMT_MARKER, psf->strings [k].str) ;
1165                                 break ;
1166
1167                         case SF_STR_DATE :
1168                                 psf_binheader_writef (psf, "ms", ICRD_MARKER, psf->strings [k].str) ;
1169                                 break ;
1170                         } ;
1171                 } ;
1172
1173         saved_head_index = psf->headindex ;
1174         psf->headindex = prev_head_index ;
1175         psf_binheader_writef (psf, "4", saved_head_index - prev_head_index - 4) ;
1176         psf->headindex = saved_head_index ;
1177
1178 } /* wav_write_strings */
1179
1180 static int
1181 wav_close (SF_PRIVATE *psf)
1182 {
1183         if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
1184         {       wav_write_tailer (psf) ;
1185
1186                 psf->write_header (psf, SF_TRUE) ;
1187                 } ;
1188
1189         return 0 ;
1190 } /* wav_close */
1191
1192 static int
1193 wav_command (SF_PRIVATE *psf, int command, void *data, int datasize)
1194 {
1195         /* Avoid compiler warnings. */
1196         psf = psf ;
1197         data = data ;
1198         datasize = datasize ;
1199
1200         switch (command)
1201         {       default : break ;
1202                 } ;
1203
1204         return 0 ;
1205 } /* wav_command */
1206
1207 static int
1208 wav_subchunk_parse (SF_PRIVATE *psf, int chunk)
1209 {       sf_count_t      current_pos ;
1210         char            *cptr ;
1211         int             dword, bytesread, length ;
1212
1213         current_pos = psf_fseek (psf, 0, SEEK_CUR) ;
1214
1215         bytesread = psf_binheader_readf (psf, "4", &length) ;
1216
1217         if (length <= 8)
1218         {       /* This case is for broken files generated by PEAK. */
1219                 psf_log_printf (psf, "%M : %d (weird length)\n", chunk, length) ;
1220                 psf_binheader_readf (psf, "mj", &chunk, length - 4) ;
1221                 psf_log_printf (psf, "  %M\n", chunk) ;
1222                 return 0 ;
1223                 } ;
1224
1225         if (psf->headindex + length > SIGNED_SIZEOF (psf->header))
1226         {       psf_log_printf (psf, "%M : %d (too long)\n", chunk, length) ;
1227                 psf_binheader_readf (psf, "j", length) ;
1228                 return 0 ;
1229                 } ;
1230
1231         if (current_pos + length > psf->filelength)
1232         {       psf_log_printf (psf, "%M : %d (should be %d)\n", chunk, length, (int) (psf->filelength - current_pos)) ;
1233                 length = psf->filelength - current_pos ;
1234                 }
1235         else
1236                 psf_log_printf (psf, "%M : %d\n", chunk, length) ;
1237
1238         while (bytesread < length)
1239         {       bytesread += psf_binheader_readf (psf, "m", &chunk) ;
1240
1241                 switch (chunk)
1242                 {       case adtl_MARKER :
1243                         case INFO_MARKER :
1244                                         /* These markers don't contain anything. */
1245                                         psf_log_printf (psf, "  %M\n", chunk) ;
1246                                         break ;
1247
1248                         case data_MARKER:
1249                                         psf_log_printf (psf, "  %M inside a LIST block??? Backing out.\n", chunk) ;
1250                                         /* Jump back four bytes and return to caller. */
1251                                         psf_binheader_readf (psf, "j", -4) ;
1252                                         return 0 ;
1253
1254                         case ISFT_MARKER :
1255                         case ICOP_MARKER :
1256                         case IARL_MARKER :
1257                         case IART_MARKER :
1258                         case ICMT_MARKER :
1259                         case ICRD_MARKER :
1260                         case IENG_MARKER :
1261
1262                         case INAM_MARKER :
1263                         case IPRD_MARKER :
1264                         case ISBJ_MARKER :
1265                         case ISRC_MARKER :
1266                                         bytesread += psf_binheader_readf (psf, "4", &dword) ;
1267                                         dword += (dword & 1) ;
1268                                         if (dword < 0 || dword > SIGNED_SIZEOF (psf->u.cbuf))
1269                                         {       psf_log_printf (psf, "  *** %M : %d (too big)\n", chunk, dword) ;
1270                                                 psf_binheader_readf (psf, "j", dword) ;
1271                                                 break ;
1272                                                 } ;
1273
1274                                         cptr = psf->u.cbuf ;
1275                                         psf_binheader_readf (psf, "b", cptr, dword) ;
1276                                         bytesread += dword ;
1277                                         cptr [dword - 1] = 0 ;
1278                                         psf_log_printf (psf, "    %M : %s\n", chunk, cptr) ;
1279                                         break ;
1280
1281                         case labl_MARKER :
1282                                         {       int mark_id ;
1283
1284                                                 bytesread += psf_binheader_readf (psf, "44", &dword, &mark_id) ;
1285                                                 dword -= 4 ;
1286                                                 dword += (dword & 1) ;
1287                                                 if (dword < 1 || dword > SIGNED_SIZEOF (psf->u.cbuf))
1288                                                 {       psf_log_printf (psf, "  *** %M : %d (too big)\n", chunk, dword) ;
1289                                                         psf_binheader_readf (psf, "j", dword) ;
1290                                                         break ;
1291                                                         } ;
1292
1293                                                 cptr = psf->u.cbuf ;
1294                                                 psf_binheader_readf (psf, "b", cptr, dword) ;
1295                                                 bytesread += dword ;
1296                                                 cptr [dword - 1] = 0 ;
1297                                                 psf_log_printf (psf, "    %M : %d : %s\n", chunk, mark_id, cptr) ;
1298                                                 } ;
1299                                         break ;
1300
1301
1302                         case DISP_MARKER :
1303                         case ltxt_MARKER :
1304                         case note_MARKER :
1305                                         bytesread += psf_binheader_readf (psf, "4", &dword) ;
1306                                         dword += (dword & 1) ;
1307                                         psf_binheader_readf (psf, "j", dword) ;
1308                                         bytesread += dword ;
1309                                         psf_log_printf (psf, "    %M : %d\n", chunk, dword) ;
1310                                         break ;
1311
1312                         default :
1313                                         psf_binheader_readf (psf, "4", &dword) ;
1314                                         bytesread += sizeof (dword) ;
1315                                         dword += (dword & 1) ;
1316                                         psf_binheader_readf (psf, "j", dword) ;
1317                                         bytesread += dword ;
1318                                         psf_log_printf (psf, "    *** %M : %d\n", chunk, dword) ;
1319                                         if (dword > length)
1320                                                 return 0 ;
1321                                         break ;
1322                         } ;
1323
1324                 switch (chunk)
1325                 {       case ISFT_MARKER :
1326                                         psf_store_string (psf, SF_STR_SOFTWARE, psf->u.cbuf) ;
1327                                         break ;
1328                         case ICOP_MARKER :
1329                                         psf_store_string (psf, SF_STR_COPYRIGHT, psf->u.cbuf) ;
1330                                         break ;
1331                         case INAM_MARKER :
1332                                         psf_store_string (psf, SF_STR_TITLE, psf->u.cbuf) ;
1333                                         break ;
1334                         case IART_MARKER :
1335                                         psf_store_string (psf, SF_STR_ARTIST, psf->u.cbuf) ;
1336                                         break ;
1337                         case ICMT_MARKER :
1338                                         psf_store_string (psf, SF_STR_COMMENT, psf->u.cbuf) ;
1339                                         break ;
1340                         case ICRD_MARKER :
1341                                         psf_store_string (psf, SF_STR_DATE, psf->u.cbuf) ;
1342                                         break ;
1343                         } ;
1344                 } ;
1345
1346         current_pos = psf_fseek (psf, 0, SEEK_CUR) - current_pos ;
1347
1348         if (current_pos - 4 != length)
1349                 psf_log_printf (psf, "**** Bad chunk length %d sbould be %D\n", length, current_pos - 4) ;
1350
1351         return 0 ;
1352 } /* wav_subchunk_parse */
1353
1354 static int
1355 wav_read_smpl_chunk (SF_PRIVATE *psf, unsigned int chunklen)
1356 {       unsigned int bytesread = 0, dword, sampler_data, loop_count ;
1357         unsigned int note, start, end, type = -1, count ;
1358         int j, k ;
1359
1360         chunklen += (chunklen & 1) ;
1361
1362         bytesread += psf_binheader_readf (psf, "4", &dword) ;
1363         psf_log_printf (psf, "  Manufacturer : %X\n", dword) ;
1364
1365         bytesread += psf_binheader_readf (psf, "4", &dword) ;
1366         psf_log_printf (psf, "  Product      : %u\n", dword) ;
1367
1368         bytesread += psf_binheader_readf (psf, "4", &dword) ;
1369         psf_log_printf (psf, "  Period       : %u nsec\n", dword) ;
1370
1371         bytesread += psf_binheader_readf (psf, "4", &note) ;
1372         psf_log_printf (psf, "  Midi Note    : %u\n", note) ;
1373
1374         bytesread += psf_binheader_readf (psf, "4", &dword) ;
1375         if (dword != 0)
1376         {       LSF_SNPRINTF (psf->u.cbuf, sizeof (psf->u.cbuf), "%f",
1377                                  (1.0 * 0x80000000) / ((unsigned int) dword)) ;
1378                 psf_log_printf (psf, "  Pitch Fract. : %s\n", psf->u.cbuf) ;
1379                 }
1380         else
1381                 psf_log_printf (psf, "  Pitch Fract. : 0\n") ;
1382
1383         bytesread += psf_binheader_readf (psf, "4", &dword) ;
1384         psf_log_printf (psf, "  SMPTE Format : %u\n", dword) ;
1385
1386         bytesread += psf_binheader_readf (psf, "4", &dword) ;
1387         LSF_SNPRINTF (psf->u.cbuf, sizeof (psf->u.cbuf), "%02d:%02d:%02d %02d",
1388                  (dword >> 24) & 0x7F, (dword >> 16) & 0x7F, (dword >> 8) & 0x7F, dword & 0x7F) ;
1389         psf_log_printf (psf, "  SMPTE Offset : %s\n", psf->u.cbuf) ;
1390
1391         bytesread += psf_binheader_readf (psf, "4", &loop_count) ;
1392         psf_log_printf (psf, "  Loop Count   : %u\n", loop_count) ;
1393
1394         /* Sampler Data holds the number of data bytes after the CUE chunks which
1395         ** is not actually CUE data. Display value after CUE data.
1396         */
1397         bytesread += psf_binheader_readf (psf, "4", &sampler_data) ;
1398
1399         if ((psf->instrument = psf_instrument_alloc ()) == NULL)
1400                 return SFE_MALLOC_FAILED ;
1401
1402         psf->instrument->loop_count = loop_count ;
1403
1404         for (j = 0 ; loop_count > 0 && chunklen - bytesread >= 24 ; j ++)
1405         {       bytesread += psf_binheader_readf (psf, "4", &dword) ;
1406                 psf_log_printf (psf, "    Cue ID : %2u", dword) ;
1407
1408                 bytesread += psf_binheader_readf (psf, "4", &type) ;
1409                 psf_log_printf (psf, "  Type : %2u", type) ;
1410
1411                 bytesread += psf_binheader_readf (psf, "4", &start) ;
1412                 psf_log_printf (psf, "  Start : %5u", start) ;
1413
1414                 bytesread += psf_binheader_readf (psf, "4", &end) ;
1415                 psf_log_printf (psf, "  End : %5u", end) ;
1416
1417                 bytesread += psf_binheader_readf (psf, "4", &dword) ;
1418                 psf_log_printf (psf, "  Fraction : %5u", dword) ;
1419
1420                 bytesread += psf_binheader_readf (psf, "4", &count) ;
1421                 psf_log_printf (psf, "  Count : %5u\n", count) ;
1422
1423                 if (j < ARRAY_LEN (psf->instrument->loops))
1424                 {       psf->instrument->loops [j].start = start ;
1425                         psf->instrument->loops [j].end = end ;
1426                         psf->instrument->loops [j].count = count ;
1427
1428                         switch (type)
1429                         {       case 0 :
1430                                         psf->instrument->loops [j].mode = SF_LOOP_FORWARD ;
1431                                         break ;
1432                                 case 1 :
1433                                         psf->instrument->loops [j].mode = SF_LOOP_ALTERNATING ;
1434                                         break ;
1435                                 case 2 :
1436                                         psf->instrument->loops [j].mode = SF_LOOP_BACKWARD ;
1437                                         break ;
1438                                 default:
1439                                         psf->instrument->loops [j].mode = SF_LOOP_NONE ;
1440                                         break ;
1441                                 } ;
1442                         } ;
1443
1444                 loop_count -- ;
1445                 } ;
1446
1447         if (chunklen - bytesread == 0)
1448         {       if (sampler_data != 0)
1449                         psf_log_printf (psf, "  Sampler Data : %u (should be 0)\n", sampler_data) ;
1450                 else
1451                         psf_log_printf (psf, "  Sampler Data : %u\n", sampler_data) ;
1452                 }
1453         else
1454         {       if (sampler_data != chunklen - bytesread)
1455                 {       psf_log_printf (psf, "  Sampler Data : %u (should have been %u)\n", sampler_data, chunklen - bytesread) ;
1456                         sampler_data = chunklen - bytesread ;
1457                         }
1458                 else
1459                         psf_log_printf (psf, "  Sampler Data : %u\n", sampler_data) ;
1460
1461                 psf_log_printf (psf, "      ") ;
1462                 for (k = 0 ; k < (int) sampler_data ; k++)
1463                 {       char ch ;
1464
1465                         if (k > 0 && (k % 20) == 0)
1466                                 psf_log_printf (psf, "\n      ") ;
1467
1468                         bytesread += psf_binheader_readf (psf, "1", &ch) ;
1469                         psf_log_printf (psf, "%02X ", ch & 0xFF) ;
1470                         } ;
1471
1472                 psf_log_printf (psf, "\n") ;
1473                 } ;
1474
1475         psf->instrument->basenote = note ;
1476         psf->instrument->gain = 1 ;
1477         psf->instrument->velocity_lo = psf->instrument->key_lo = 0 ;
1478         psf->instrument->velocity_hi = psf->instrument->key_hi = 127 ;
1479
1480         return 0 ;
1481 } /* wav_read_smpl_chunk */
1482
1483 /*
1484 ** The acid chunk goes a little something like this:
1485 **
1486 ** 4 bytes          'acid'
1487 ** 4 bytes (int)     length of chunk starting at next byte
1488 **
1489 ** 4 bytes (int)     type of file:
1490 **        this appears to be a bit mask,however some combinations
1491 **        are probably impossible and/or qualified as "errors"
1492 **
1493 **        0x01 On: One Shot         Off: Loop
1494 **        0x02 On: Root note is Set Off: No root
1495 **        0x04 On: Stretch is On,   Off: Strech is OFF
1496 **        0x08 On: Disk Based       Off: Ram based
1497 **        0x10 On: ??????????       Off: ????????? (Acidizer puts that ON)
1498 **
1499 ** 2 bytes (short)      root note
1500 **        if type 0x10 is OFF : [C,C#,(...),B] -> [0x30 to 0x3B]
1501 **        if type 0x10 is ON  : [C,C#,(...),B] -> [0x3C to 0x47]
1502 **         (both types fit on same MIDI pitch albeit different octaves, so who cares)
1503 **
1504 ** 2 bytes (short)      ??? always set to 0x8000
1505 ** 4 bytes (float)      ??? seems to be always 0
1506 ** 4 bytes (int)        number of beats
1507 ** 2 bytes (short)      meter denominator   //always 4 in SF/ACID
1508 ** 2 bytes (short)      meter numerator     //always 4 in SF/ACID
1509 **                      //are we sure about the order?? usually its num/denom
1510 ** 4 bytes (float)      tempo
1511 **
1512 */
1513
1514 static int
1515 wav_read_acid_chunk (SF_PRIVATE *psf, unsigned int chunklen)
1516 {       unsigned int bytesread = 0 ;
1517         int     beats, flags ;
1518         short rootnote, q1, meter_denom, meter_numer ;
1519         float q2, tempo ;
1520
1521         chunklen += (chunklen & 1) ;
1522
1523         bytesread += psf_binheader_readf (psf, "422f", &flags, &rootnote, &q1, &q2) ;
1524
1525         LSF_SNPRINTF (psf->u.cbuf, sizeof (psf->u.cbuf), "%f", q2) ;
1526
1527         psf_log_printf (psf, "  Flags     : 0x%04x (%s,%s,%s,%s,%s)\n", flags,
1528                         (flags & 0x01) ? "OneShot" : "Loop",
1529                         (flags & 0x02) ? "RootNoteValid" : "RootNoteInvalid",
1530                         (flags & 0x04) ? "StretchOn" : "StretchOff",
1531                         (flags & 0x08) ? "DiskBased" : "RAMBased",
1532                         (flags & 0x10) ? "??On" : "??Off") ;
1533
1534         psf_log_printf (psf, "  Root note : 0x%x\n  ????      : 0x%04x\n  ????      : %s\n",
1535                                 rootnote, q1, psf->u.cbuf) ;
1536
1537         bytesread += psf_binheader_readf (psf, "422f", &beats, &meter_denom, &meter_numer, &tempo) ;
1538         LSF_SNPRINTF (psf->u.cbuf, sizeof (psf->u.cbuf), "%f", tempo) ;
1539         psf_log_printf (psf, "  Beats     : %d\n  Meter     : %d/%d\n  Tempo     : %s\n",
1540                                 beats, meter_numer, meter_denom, psf->u.cbuf) ;
1541
1542         psf_binheader_readf (psf, "j", chunklen - bytesread) ;
1543
1544         if ((psf->loop_info = calloc (1, sizeof (SF_LOOP_INFO))) == NULL)
1545                 return SFE_MALLOC_FAILED ;
1546
1547         psf->loop_info->time_sig_num    = meter_numer ;
1548         psf->loop_info->time_sig_den    = meter_denom ;
1549         psf->loop_info->loop_mode               = (flags & 0x01) ? SF_LOOP_NONE : SF_LOOP_FORWARD ;
1550         psf->loop_info->num_beats               = beats ;
1551         psf->loop_info->bpm                             = tempo ;
1552         psf->loop_info->root_key                = (flags & 0x02) ? rootnote : -1 ;
1553
1554         return 0 ;
1555 } /* wav_read_acid_chunk */
1556
1557 int
1558 wav_read_bext_chunk (SF_PRIVATE *psf, unsigned int chunksize)
1559 {
1560         SF_BROADCAST_INFO* b ;
1561
1562         if ((psf->broadcast_info = calloc (1, sizeof (SF_BROADCAST_INFO))) == NULL)
1563         {       psf->error = SFE_MALLOC_FAILED ;
1564                 return -1 ;
1565                 } ;
1566
1567         b = psf->broadcast_info ;
1568
1569         psf_binheader_readf (psf, "b", b->description, sizeof (b->description)) ;
1570         psf_binheader_readf (psf, "b", b->originator, sizeof (b->originator)) ;
1571         psf_binheader_readf (psf, "b", b->originator_reference, sizeof (b->originator_reference)) ;
1572         psf_binheader_readf (psf, "b", b->origination_date, sizeof (b->origination_date)) ;
1573         psf_binheader_readf (psf, "b", b->origination_time, sizeof (b->origination_time)) ;
1574         psf_binheader_readf (psf, "442", &b->time_reference_low, &b->time_reference_high, &b->version) ;
1575         psf_binheader_readf (psf, "bj", &b->umid, sizeof (b->umid), 190) ;
1576
1577         if (chunksize > WAV_BEXT_CHUNK_SIZE)
1578         {       /* File has coding history data. */
1579
1580                 b->coding_history_size = chunksize - WAV_BEXT_CHUNK_SIZE ;
1581
1582                 if (b->coding_history_size > SIGNED_SIZEOF (b->coding_history))
1583                 {       free (psf->broadcast_info) ;
1584                         psf->broadcast_info = NULL ;
1585                         psf->error = SFE_MALLOC_FAILED ;
1586                         return -1 ;
1587                         } ;
1588
1589                 /* We do not parse the coding history */
1590                 psf_binheader_readf (psf, "b", b->coding_history, b->coding_history_size) ;
1591                 b->coding_history [sizeof (b->coding_history) - 1] = 0 ;
1592                 } ;
1593
1594         return 0 ;
1595 } /* wav_read_bext_chunk */
1596
1597 static int
1598 wav_write_bext_chunk (SF_PRIVATE *psf)
1599 {       SF_BROADCAST_INFO *b ;
1600
1601         if ((b = psf->broadcast_info) == NULL)
1602                 return -1 ;
1603
1604         psf_binheader_writef (psf, "m4", bext_MARKER, WAV_BEXT_CHUNK_SIZE + b->coding_history_size) ;
1605
1606         /*
1607         **      Note that it is very important the the field widths of the SF_BROADCAST_INFO
1608         **      struct match those for the bext chunk fields.
1609         */
1610
1611         psf_binheader_writef (psf, "b", b->description, sizeof (b->description)) ;
1612         psf_binheader_writef (psf, "b", b->originator, sizeof (b->originator)) ;
1613         psf_binheader_writef (psf, "b", b->originator_reference, sizeof (b->originator_reference)) ;
1614         psf_binheader_writef (psf, "b", b->origination_date, sizeof (b->origination_date)) ;
1615         psf_binheader_writef (psf, "b", b->origination_time, sizeof (b->origination_time)) ;
1616         psf_binheader_writef (psf, "442", b->time_reference_low, b->time_reference_high, b->version) ;
1617         psf_binheader_writef (psf, "b", b->umid, sizeof (b->umid)) ;
1618         psf_binheader_writef (psf, "z", make_size_t (190)) ;
1619
1620         if (b->coding_history_size > 0)
1621                 psf_binheader_writef (psf, "b", b->coding_history, b->coding_history_size) ;
1622
1623         return 0 ;
1624 } /* wav_write_bext_chunk */
1625
1626 /*
1627 ** Do not edit or modify anything in this comment block.
1628 ** The arch-tag line is a file identity tag for the GNU Arch
1629 ** revision control system.
1630 **
1631 ** arch-tag: 9c551689-a1d8-4905-9f56-26a204374f18
1632 */