add libsndfile directory
[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                                         parsestage |= HAVE_other ;
543
544                                         psf_binheader_readf (psf, "4", &dword) ;
545                                         if (dword < WAV_BEXT_CHUNK_SIZE)
546                                                 psf_log_printf (psf, "bext : %u (should be >= %d)\n", dword, WAV_BEXT_CHUNK_SIZE) ;
547                                         else
548                                                 psf_log_printf (psf, "bext : %u\n", dword) ;
549
550                                         if ((error = wav_read_bext_chunk (psf, dword)))
551                                                 return error ;
552                                         break ;
553
554                         case strc_MARKER : /* Multiple of 32 bytes. */
555
556                         case afsp_MARKER :
557                         case clm_MARKER :
558                         case elmo_MARKER :
559                         case levl_MARKER :
560                         case plst_MARKER :
561                         case DISP_MARKER :
562                         case MEXT_MARKER :
563                         case PAD_MARKER :
564                                         parsestage |= HAVE_other ;
565
566                                         psf_binheader_readf (psf, "4", &dword) ;
567                                         psf_log_printf (psf, "%M : %u\n", marker, dword) ;
568                                         dword += (dword & 1) ;
569                                         psf_binheader_readf (psf, "j", dword) ;
570                                         break ;
571
572                         default :
573                                         parsestage |= HAVE_other ;
574                                         if (isprint ((marker >> 24) & 0xFF) && isprint ((marker >> 16) & 0xFF)
575                                                 && isprint ((marker >> 8) & 0xFF) && isprint (marker & 0xFF))
576                                         {       psf_binheader_readf (psf, "4", &dword) ;
577                                                 psf_log_printf (psf, "*** %M : %d (unknown marker)\n", marker, dword) ;
578                                                 psf_binheader_readf (psf, "j", dword) ;
579                                                 break ;
580                                                 } ;
581                                         if (psf_ftell (psf) & 0x03)
582                                         {       psf_log_printf (psf, "  Unknown chunk marker at position %d. Resynching.\n", dword - 4) ;
583                                                 psf_binheader_readf (psf, "j", -3) ;
584                                                 break ;
585                                                 } ;
586                                         psf_log_printf (psf, "*** Unknown chunk marker (%X) at position %D. Exiting parser.\n", marker, psf_ftell (psf) - 4) ;
587                                         done = SF_TRUE ;
588                                         break ;
589                         } ;     /* switch (dword) */
590
591                 if (! psf->sf.seekable && (parsestage & HAVE_data))
592                         break ;
593
594                 if (psf_ftell (psf) >= psf->filelength - SIGNED_SIZEOF (dword))
595                 {       psf_log_printf (psf, "End\n") ;
596                         break ;
597                         } ;
598                 } ; /* while (1) */
599
600         if (! psf->dataoffset)
601                 return SFE_WAV_NO_DATA ;
602
603         /* WAVs can be little or big endian */
604         psf->endian = psf->rwf_endian ;
605
606         psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
607
608         if (psf->is_pipe == 0)
609         {       /*
610                 ** Check for 'wvpk' at the start of the DATA section. Not able to
611                 ** handle this.
612                 */
613                 psf_binheader_readf (psf, "4", &marker) ;
614                 if (marker == wvpk_MARKER || marker == OggS_MARKER)
615                         return SFE_WAV_WVPK_DATA ;
616                 } ;
617
618         /* Seek to start of DATA section. */
619         psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
620
621         if (psf->blockwidth)
622         {       if (psf->filelength - psf->dataoffset < psf->datalength)
623                         psf->sf.frames = (psf->filelength - psf->dataoffset) / psf->blockwidth ;
624                 else
625                         psf->sf.frames = psf->datalength / psf->blockwidth ;
626                 } ;
627
628         switch (format)
629         {       case WAVE_FORMAT_EXTENSIBLE :
630                         if (psf->sf.format == (SF_FORMAT_WAVEX | SF_FORMAT_MS_ADPCM))
631                         {       *blockalign = wav_fmt.msadpcm.blockalign ;
632                                 *framesperblock = wav_fmt.msadpcm.samplesperblock ;
633                                 } ;
634                         break ;
635
636                 case WAVE_FORMAT_PCM :
637                                         psf->sf.format = SF_FORMAT_WAV | u_bitwidth_to_subformat (psf->bytewidth * 8) ;
638                                         break ;
639
640                 case WAVE_FORMAT_MULAW :
641                 case IBM_FORMAT_MULAW :
642                                         psf->sf.format = (SF_FORMAT_WAV | SF_FORMAT_ULAW) ;
643                                         break ;
644
645                 case WAVE_FORMAT_ALAW :
646                 case IBM_FORMAT_ALAW :
647                                         psf->sf.format = (SF_FORMAT_WAV | SF_FORMAT_ALAW) ;
648                                         break ;
649
650                 case WAVE_FORMAT_MS_ADPCM :
651                                         psf->sf.format = (SF_FORMAT_WAV | SF_FORMAT_MS_ADPCM) ;
652                                         *blockalign = wav_fmt.msadpcm.blockalign ;
653                                         *framesperblock = wav_fmt.msadpcm.samplesperblock ;
654                                         break ;
655
656                 case WAVE_FORMAT_IMA_ADPCM :
657                                         psf->sf.format = (SF_FORMAT_WAV | SF_FORMAT_IMA_ADPCM) ;
658                                         *blockalign = wav_fmt.ima.blockalign ;
659                                         *framesperblock = wav_fmt.ima.samplesperblock ;
660                                         break ;
661
662                 case WAVE_FORMAT_GSM610 :
663                                         psf->sf.format = (SF_FORMAT_WAV | SF_FORMAT_GSM610) ;
664                                         break ;
665
666                 case WAVE_FORMAT_IEEE_FLOAT :
667                                         psf->sf.format = SF_FORMAT_WAV ;
668                                         psf->sf.format |= (psf->bytewidth == 8) ? SF_FORMAT_DOUBLE : SF_FORMAT_FLOAT ;
669                                         break ;
670
671                 case WAVE_FORMAT_G721_ADPCM :
672                                         psf->sf.format = SF_FORMAT_WAV | SF_FORMAT_G721_32 ;
673                                         break ;
674
675                 default : return SFE_UNIMPLEMENTED ;
676                 } ;
677
678         /* Only set the format endian-ness if its non-standard big-endian. */
679         if (psf->endian == SF_ENDIAN_BIG)
680                 psf->sf.format |= SF_ENDIAN_BIG ;
681
682         return 0 ;
683 } /* wav_read_header */
684
685 static int
686 wav_write_header (SF_PRIVATE *psf, int calc_length)
687 {       sf_count_t      current ;
688         int             fmt_size, k, subformat, add_fact_chunk = SF_FALSE ;
689
690         current = psf_ftell (psf) ;
691
692         if (calc_length)
693         {       psf->filelength = psf_get_filelen (psf) ;
694
695                 psf->datalength = psf->filelength - psf->dataoffset ;
696
697                 if (psf->dataend)
698                         psf->datalength -= psf->filelength - psf->dataend ;
699
700                 if (psf->bytewidth > 0)
701                         psf->sf.frames = psf->datalength / (psf->bytewidth * psf->sf.channels) ;
702                 } ;
703
704         /* Reset the current header length to zero. */
705         psf->header [0] = 0 ;
706         psf->headindex = 0 ;
707         psf_fseek (psf, 0, SEEK_SET) ;
708
709         /*
710         ** RIFX signifies big-endian format for all header and data.
711         ** To prevent lots of code copying here, we'll set the psf->rwf_endian flag
712         ** once here, and never specify endian-ness for all other header operations.
713         */
714
715         /* RIFF/RIFX marker, length, WAVE and 'fmt ' markers. */
716
717         if (psf->endian == SF_ENDIAN_LITTLE)
718                 psf_binheader_writef (psf, "etm8", RIFF_MARKER, (psf->filelength < 8) ? 8 : psf->filelength - 8) ;
719         else
720                 psf_binheader_writef (psf, "Etm8", RIFX_MARKER, (psf->filelength < 8) ? 8 : psf->filelength - 8) ;
721
722         /* WAVE and 'fmt ' markers. */
723         psf_binheader_writef (psf, "mm", WAVE_MARKER, fmt_MARKER) ;
724
725         subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
726
727         switch (subformat)
728         {       case SF_FORMAT_PCM_U8 :
729                 case SF_FORMAT_PCM_16 :
730                 case SF_FORMAT_PCM_24 :
731                 case SF_FORMAT_PCM_32 :
732                                         fmt_size = 2 + 2 + 4 + 4 + 2 + 2 ;
733
734                                         /* fmt : format, channels, samplerate */
735                                         psf_binheader_writef (psf, "4224", fmt_size, WAVE_FORMAT_PCM, psf->sf.channels, psf->sf.samplerate) ;
736                                         /*  fmt : bytespersec */
737                                         psf_binheader_writef (psf, "4", psf->sf.samplerate * psf->bytewidth * psf->sf.channels) ;
738                                         /*  fmt : blockalign, bitwidth */
739                                         psf_binheader_writef (psf, "22", psf->bytewidth * psf->sf.channels, psf->bytewidth * 8) ;
740                                         break ;
741
742                 case SF_FORMAT_FLOAT :
743                 case SF_FORMAT_DOUBLE :
744                                         fmt_size = 2 + 2 + 4 + 4 + 2 + 2 ;
745
746                                         /* fmt : format, channels, samplerate */
747                                         psf_binheader_writef (psf, "4224", fmt_size, WAVE_FORMAT_IEEE_FLOAT, psf->sf.channels, psf->sf.samplerate) ;
748                                         /*  fmt : bytespersec */
749                                         psf_binheader_writef (psf, "4", psf->sf.samplerate * psf->bytewidth * psf->sf.channels) ;
750                                         /*  fmt : blockalign, bitwidth */
751                                         psf_binheader_writef (psf, "22", psf->bytewidth * psf->sf.channels, psf->bytewidth * 8) ;
752
753                                         add_fact_chunk = SF_TRUE ;
754                                         break ;
755
756                 case SF_FORMAT_ULAW :
757                                         fmt_size = 2 + 2 + 4 + 4 + 2 + 2 ;
758
759                                         /* fmt : format, channels, samplerate */
760                                         psf_binheader_writef (psf, "4224", fmt_size, WAVE_FORMAT_MULAW, psf->sf.channels, psf->sf.samplerate) ;
761                                         /*  fmt : bytespersec */
762                                         psf_binheader_writef (psf, "4", psf->sf.samplerate * psf->bytewidth * psf->sf.channels) ;
763                                         /*  fmt : blockalign, bitwidth */
764                                         psf_binheader_writef (psf, "22", psf->bytewidth * psf->sf.channels, 8) ;
765
766                                         add_fact_chunk = SF_TRUE ;
767                                         break ;
768
769                 case SF_FORMAT_ALAW :
770                                         fmt_size = 2 + 2 + 4 + 4 + 2 + 2 ;
771
772                                         /* fmt : format, channels, samplerate */
773                                         psf_binheader_writef (psf, "4224", fmt_size, WAVE_FORMAT_ALAW, psf->sf.channels, psf->sf.samplerate) ;
774                                         /*  fmt : bytespersec */
775                                         psf_binheader_writef (psf, "4", psf->sf.samplerate * psf->bytewidth * psf->sf.channels) ;
776                                         /*  fmt : blockalign, bitwidth */
777                                         psf_binheader_writef (psf, "22", psf->bytewidth * psf->sf.channels, 8) ;
778
779                                         add_fact_chunk = SF_TRUE ;
780                                         break ;
781
782                 /* Lite remove start */
783                 case SF_FORMAT_IMA_ADPCM :
784                                         {       int blockalign, framesperblock, bytespersec ;
785
786                                                 blockalign              = wav_w64_srate2blocksize (psf->sf.samplerate * psf->sf.channels) ;
787                                                 framesperblock  = 2 * (blockalign - 4 * psf->sf.channels) / psf->sf.channels + 1 ;
788                                                 bytespersec             = (psf->sf.samplerate * blockalign) / framesperblock ;
789
790                                                 /* fmt chunk. */
791                                                 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 + 2 ;
792
793                                                 /* fmt : size, WAV format type, channels, samplerate, bytespersec */
794                                                 psf_binheader_writef (psf, "42244", fmt_size, WAVE_FORMAT_IMA_ADPCM,
795                                                                         psf->sf.channels, psf->sf.samplerate, bytespersec) ;
796
797                                                 /* fmt : blockalign, bitwidth, extrabytes, framesperblock. */
798                                                 psf_binheader_writef (psf, "2222", blockalign, 4, 2, framesperblock) ;
799                                                 } ;
800
801                                         add_fact_chunk = SF_TRUE ;
802                                         break ;
803
804                 case SF_FORMAT_MS_ADPCM :
805                                         {       int     blockalign, framesperblock, bytespersec, extrabytes ;
806
807                                                 blockalign              = wav_w64_srate2blocksize (psf->sf.samplerate * psf->sf.channels) ;
808                                                 framesperblock  = 2 + 2 * (blockalign - 7 * psf->sf.channels) / psf->sf.channels ;
809                                                 bytespersec             = (psf->sf.samplerate * blockalign) / framesperblock ;
810
811                                                 /* fmt chunk. */
812                                                 extrabytes      = 2 + 2 + MSADPCM_ADAPT_COEFF_COUNT * (2 + 2) ;
813                                                 fmt_size        = 2 + 2 + 4 + 4 + 2 + 2 + 2 + extrabytes ;
814
815                                                 /* fmt : size, WAV format type, channels. */
816                                                 psf_binheader_writef (psf, "422", fmt_size, WAVE_FORMAT_MS_ADPCM, psf->sf.channels) ;
817
818                                                 /* fmt : samplerate, bytespersec. */
819                                                 psf_binheader_writef (psf, "44", psf->sf.samplerate, bytespersec) ;
820
821                                                 /* fmt : blockalign, bitwidth, extrabytes, framesperblock. */
822                                                 psf_binheader_writef (psf, "22222", blockalign, 4, extrabytes, framesperblock, 7) ;
823
824                                                 msadpcm_write_adapt_coeffs (psf) ;
825                                                 } ;
826
827                                         add_fact_chunk = SF_TRUE ;
828                                         break ;
829
830
831                 case SF_FORMAT_G721_32 :
832                                         /* fmt chunk. */
833                                         fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 + 2 ;
834
835                                         /* fmt : size, WAV format type, channels, samplerate, bytespersec */
836                                         psf_binheader_writef (psf, "42244", fmt_size, WAVE_FORMAT_G721_ADPCM,
837                                                                 psf->sf.channels, psf->sf.samplerate, psf->sf.samplerate * psf->sf.channels / 2) ;
838
839                                         /* fmt : blockalign, bitwidth, extrabytes, auxblocksize. */
840                                         psf_binheader_writef (psf, "2222", 64, 4, 2, 0) ;
841
842                                         add_fact_chunk = SF_TRUE ;
843                                         break ;
844
845                 /* Lite remove end */
846
847                 case SF_FORMAT_GSM610 :
848                                         {       int     blockalign, framesperblock, bytespersec ;
849
850                                                 blockalign              = WAV_W64_GSM610_BLOCKSIZE ;
851                                                 framesperblock  = WAV_W64_GSM610_SAMPLES ;
852                                                 bytespersec             = (psf->sf.samplerate * blockalign) / framesperblock ;
853
854                                                 /* fmt chunk. */
855                                                 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 + 2 ;
856
857                                                 /* fmt : size, WAV format type, channels. */
858                                                 psf_binheader_writef (psf, "422", fmt_size, WAVE_FORMAT_GSM610, psf->sf.channels) ;
859
860                                                 /* fmt : samplerate, bytespersec. */
861                                                 psf_binheader_writef (psf, "44", psf->sf.samplerate, bytespersec) ;
862
863                                                 /* fmt : blockalign, bitwidth, extrabytes, framesperblock. */
864                                                 psf_binheader_writef (psf, "2222", blockalign, 0, 2, framesperblock) ;
865                                                 } ;
866
867                                         add_fact_chunk = SF_TRUE ;
868                                         break ;
869
870                 default :       return SFE_UNIMPLEMENTED ;
871                 } ;
872
873         if (add_fact_chunk)
874                 psf_binheader_writef (psf, "tm48", fact_MARKER, 4, psf->sf.frames) ;
875
876         if (psf->str_flags & SF_STR_LOCATE_START)
877                 wav_write_strings (psf, SF_STR_LOCATE_START) ;
878
879         if (psf->peak_info != NULL && psf->peak_info->peak_loc == SF_PEAK_START)
880         {       psf_binheader_writef (psf, "m4", PEAK_MARKER, WAV_PEAK_CHUNK_SIZE (psf->sf.channels)) ;
881                 psf_binheader_writef (psf, "44", 1, time (NULL)) ;
882                 for (k = 0 ; k < psf->sf.channels ; k++)
883                         psf_binheader_writef (psf, "ft8", (float) psf->peak_info->peaks [k].value, psf->peak_info->peaks [k].position) ;
884                 } ;
885
886         if (psf->broadcast_info != NULL)
887                 wav_write_bext_chunk (psf) ;
888
889         if (psf->instrument != NULL)
890         {       int             tmp ;
891                 double  dtune = (double) (0x40000000) / 25.0 ;
892
893                 psf_binheader_writef (psf, "m4", smpl_MARKER, 9 * 4 + psf->instrument->loop_count * 6 * 4) ;
894                 psf_binheader_writef (psf, "44", 0, 0) ; /* Manufacturer zero is everyone */
895                 tmp = (int) (1.0e9 / psf->sf.samplerate) ; /* Sample period in nano seconds */
896                 psf_binheader_writef (psf, "44", tmp, psf->instrument->basenote) ;
897                 tmp = (unsigned int) (psf->instrument->detune * dtune + 0.5) ;
898                 psf_binheader_writef (psf, "4", tmp) ;
899                 psf_binheader_writef (psf, "44", 0, 0) ; /* SMTPE format */
900                 psf_binheader_writef (psf, "44", psf->instrument->loop_count, 0) ;
901
902                 for (tmp = 0 ; tmp < psf->instrument->loop_count ; tmp++)
903                 {       int type ;
904
905                         type = psf->instrument->loops [tmp].mode ;
906                         type = (type == SF_LOOP_FORWARD ? 0 : type==SF_LOOP_BACKWARD ? 2 : type == SF_LOOP_ALTERNATING ? 1 : 32) ;
907
908                         psf_binheader_writef (psf, "44", tmp, type) ;
909                         psf_binheader_writef (psf, "44", psf->instrument->loops [tmp].start, psf->instrument->loops [tmp].end) ;
910                         psf_binheader_writef (psf, "44", 0, psf->instrument->loops [tmp].count) ;
911                         } ;
912                 } ;
913
914         psf_binheader_writef (psf, "tm8", data_MARKER, psf->datalength) ;
915         psf_fwrite (psf->header, psf->headindex, 1, psf) ;
916         if (psf->error)
917                 return psf->error ;
918
919         psf->dataoffset = psf->headindex ;
920
921         if (current < psf->dataoffset)
922                 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
923         else if (current > 0)
924                 psf_fseek (psf, current, SEEK_SET) ;
925
926         return psf->error ;
927 } /* wav_write_header */
928
929
930
931 static int
932 wavex_write_header (SF_PRIVATE *psf, int calc_length)
933 {       sf_count_t      current ;
934         int             fmt_size, k, subformat, add_fact_chunk = SF_FALSE ;
935
936         current = psf_ftell (psf) ;
937
938         if (calc_length)
939         {       psf->filelength = psf_get_filelen (psf) ;
940
941                 psf->datalength = psf->filelength - psf->dataoffset ;
942
943                 if (psf->dataend)
944                         psf->datalength -= psf->filelength - psf->dataend ;
945
946                 if (psf->bytewidth > 0)
947                         psf->sf.frames = psf->datalength / (psf->bytewidth * psf->sf.channels) ;
948                 } ;
949
950
951         /* Reset the current header length to zero. */
952         psf->header [0] = 0 ;
953         psf->headindex = 0 ;
954         psf_fseek (psf, 0, SEEK_SET) ;
955
956         /* RIFX signifies big-endian format for all header and data
957         ** to prevent lots of code copying here, we'll set the psf->rwf_endian
958         ** flag once here, and never specify endian-ness for all other header ops
959         */
960
961         /* RIFF marker, length, WAVE and 'fmt ' markers. */
962
963         if (psf->endian == SF_ENDIAN_LITTLE)
964         {       if (psf->filelength < 8)
965                         psf_binheader_writef (psf, "tm8", RIFF_MARKER, 8) ;
966                 else
967                         psf_binheader_writef (psf, "tm8", RIFF_MARKER, psf->filelength - 8) ;
968                 }
969         else
970         {       if (psf->filelength < 8)
971                         psf_binheader_writef (psf, "Etm8", RIFX_MARKER, 8) ;
972                 else
973                         psf_binheader_writef (psf, "Etm8", RIFX_MARKER, psf->filelength - 8) ;
974                 } ;
975
976         /* WAVE and 'fmt ' markers. */
977         psf_binheader_writef (psf, "mm", WAVE_MARKER, fmt_MARKER) ;
978
979         subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
980
981         /* initial section (same for all, it appears) */
982         switch (subformat)
983         {       case SF_FORMAT_PCM_U8 :
984                 case SF_FORMAT_PCM_16 :
985                 case SF_FORMAT_PCM_24 :
986                 case SF_FORMAT_PCM_32 :
987                 case SF_FORMAT_FLOAT :
988                 case SF_FORMAT_DOUBLE :
989                 case SF_FORMAT_ULAW :
990                 case SF_FORMAT_ALAW :
991                         fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 + 2 + 4 + 4 + 2 + 2 + 8 ;
992
993                         /* fmt : format, channels, samplerate */
994                         psf_binheader_writef (psf, "4224", fmt_size, WAVE_FORMAT_EXTENSIBLE, psf->sf.channels, psf->sf.samplerate) ;
995                         /*  fmt : bytespersec */
996                         psf_binheader_writef (psf, "4", psf->sf.samplerate * psf->bytewidth * psf->sf.channels) ;
997                         /*  fmt : blockalign, bitwidth */
998                         psf_binheader_writef (psf, "22", psf->bytewidth * psf->sf.channels, psf->bytewidth * 8) ;
999
1000                         /* cbSize 22 is sizeof (WAVEFORMATEXTENSIBLE) - sizeof (WAVEFORMATEX) */
1001                         psf_binheader_writef (psf, "2", 22) ;
1002
1003                         /* wValidBitsPerSample, for our use same as bitwidth as we use it fully */
1004                         psf_binheader_writef (psf, "2", psf->bytewidth * 8) ;
1005
1006                         /*
1007                         ** Ok some liberty is taken here to use the most commonly used channel masks
1008                         ** instead of "no mapping". If you really want to use "no mapping" for 8 channels and less
1009                         ** please don't use wavex. (otherwise we'll have to create a new SF_COMMAND)
1010                         */
1011                         switch (psf->sf.channels)
1012                         {       case 1 :        /* center channel mono */
1013                                         psf_binheader_writef (psf, "4", 0x4) ;
1014                                         break ;
1015
1016                                 case 2 :        /* front left and right */
1017                                         psf_binheader_writef (psf, "4", 0x1 | 0x2) ;
1018                                         break ;
1019
1020                                 case 4 :        /* Quad */
1021                                         psf_binheader_writef (psf, "4", 0x1 | 0x2 | 0x10 | 0x20) ;
1022                                         break ;
1023
1024                                 case 6 :        /* 5.1 */
1025                                         psf_binheader_writef (psf, "4", 0x1 | 0x2 | 0x4 | 0x8 | 0x10 | 0x20) ;
1026                                         break ;
1027
1028                                 case 8 :        /* 7.1 */
1029                                         psf_binheader_writef (psf, "4", 0x1 | 0x2 | 0x4 | 0x8 | 0x10 | 0x20 | 0x40 | 0x80) ;
1030                                         break ;
1031
1032                                 default :       /* 0 when in doubt , use direct out, ie NO mapping*/
1033                                         psf_binheader_writef (psf, "4", 0x0) ;
1034                                         break ;
1035                                 }
1036
1037                         break ;
1038
1039                 case SF_FORMAT_MS_ADPCM : /* Todo, GUID exists might have different header as per wav_write_header */
1040                 default :
1041                         return SFE_UNIMPLEMENTED ;
1042                 } ;
1043
1044         /* GUID section, different for each */
1045
1046         switch (subformat)
1047         {       case SF_FORMAT_PCM_U8 :
1048                 case SF_FORMAT_PCM_16 :
1049                 case SF_FORMAT_PCM_24 :
1050                 case SF_FORMAT_PCM_32 :
1051                         wavex_write_guid (psf, &MSGUID_SUBTYPE_PCM) ;
1052                         break ;
1053
1054                 case SF_FORMAT_FLOAT :
1055                 case SF_FORMAT_DOUBLE :
1056                         wavex_write_guid (psf, &MSGUID_SUBTYPE_IEEE_FLOAT) ;
1057                         add_fact_chunk = SF_TRUE ;
1058                         break ;
1059
1060                 case SF_FORMAT_ULAW :
1061                         wavex_write_guid (psf, &MSGUID_SUBTYPE_MULAW) ;
1062                         add_fact_chunk = SF_TRUE ;
1063                         break ;
1064
1065                 case SF_FORMAT_ALAW :
1066                         wavex_write_guid (psf, &MSGUID_SUBTYPE_ALAW) ;
1067                         add_fact_chunk = SF_TRUE ;
1068                         break ;
1069
1070                 case SF_FORMAT_MS_ADPCM : /* todo, GUID exists */
1071
1072                 default : return SFE_UNIMPLEMENTED ;
1073                 } ;
1074
1075         if (add_fact_chunk)
1076                 psf_binheader_writef (psf, "tm48", fact_MARKER, 4, psf->sf.frames) ;
1077
1078         if (psf->str_flags & SF_STR_LOCATE_START)
1079                 wav_write_strings (psf, SF_STR_LOCATE_START) ;
1080
1081         if (psf->peak_info != NULL && psf->peak_info->peak_loc == SF_PEAK_START)
1082         {       psf_binheader_writef (psf, "m4", PEAK_MARKER, WAV_PEAK_CHUNK_SIZE (psf->sf.channels)) ;
1083                 psf_binheader_writef (psf, "44", 1, time (NULL)) ;
1084                 for (k = 0 ; k < psf->sf.channels ; k++)
1085                         psf_binheader_writef (psf, "ft8", (float) psf->peak_info->peaks [k].value, psf->peak_info->peaks [k].position) ;
1086                 } ;
1087
1088         psf_binheader_writef (psf, "tm8", data_MARKER, psf->datalength) ;
1089         psf_fwrite (psf->header, psf->headindex, 1, psf) ;
1090         if (psf->error)
1091                 return psf->error ;
1092
1093         psf->dataoffset = psf->headindex ;
1094
1095         if (current < psf->dataoffset)
1096                 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
1097         else if (current > 0)
1098                 psf_fseek (psf, current, SEEK_SET) ;
1099
1100         return psf->error ;
1101 } /* wavex_write_header */
1102
1103
1104
1105 static int
1106 wav_write_tailer (SF_PRIVATE *psf)
1107 {       int             k ;
1108
1109         /* Reset the current header buffer length to zero. */
1110         psf->header [0] = 0 ;
1111         psf->headindex = 0 ;
1112
1113         psf->dataend = psf_fseek (psf, 0, SEEK_END) ;
1114
1115         /* Add a PEAK chunk if requested. */
1116         if (psf->peak_info != NULL && psf->peak_info->peak_loc == SF_PEAK_END)
1117         {       psf_binheader_writef (psf, "m4", PEAK_MARKER, WAV_PEAK_CHUNK_SIZE (psf->sf.channels)) ;
1118                 psf_binheader_writef (psf, "44", 1, time (NULL)) ;
1119                 for (k = 0 ; k < psf->sf.channels ; k++)
1120                         psf_binheader_writef (psf, "f4", psf->peak_info->peaks [k].value, psf->peak_info->peaks [k].position) ;
1121                 } ;
1122
1123         if (psf->str_flags & SF_STR_LOCATE_END)
1124                 wav_write_strings (psf, SF_STR_LOCATE_END) ;
1125
1126         /* Write the tailer. */
1127         if (psf->headindex > 0)
1128                 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
1129
1130         return 0 ;
1131 } /* wav_write_tailer */
1132
1133 static void
1134 wav_write_strings (SF_PRIVATE *psf, int location)
1135 {       int     k, prev_head_index, saved_head_index ;
1136
1137         prev_head_index = psf->headindex + 4 ;
1138
1139         psf_binheader_writef (psf, "m4m", LIST_MARKER, 0xBADBAD, INFO_MARKER) ;
1140
1141         for (k = 0 ; k < SF_MAX_STRINGS ; k++)
1142         {       if (psf->strings [k].type == 0)
1143                         break ;
1144                 if (psf->strings [k].flags != location)
1145                         continue ;
1146
1147                 switch (psf->strings [k].type)
1148                 {       case SF_STR_SOFTWARE :
1149                                 psf_binheader_writef (psf, "ms", ISFT_MARKER, psf->strings [k].str) ;
1150                                 break ;
1151
1152                         case SF_STR_TITLE :
1153                                 psf_binheader_writef (psf, "ms", INAM_MARKER, psf->strings [k].str) ;
1154                                 break ;
1155
1156                         case SF_STR_COPYRIGHT :
1157                                 psf_binheader_writef (psf, "ms", ICOP_MARKER, psf->strings [k].str) ;
1158                                 break ;
1159
1160                         case SF_STR_ARTIST :
1161                                 psf_binheader_writef (psf, "ms", IART_MARKER, psf->strings [k].str) ;
1162                                 break ;
1163
1164                         case SF_STR_COMMENT :
1165                                 psf_binheader_writef (psf, "ms", ICMT_MARKER, psf->strings [k].str) ;
1166                                 break ;
1167
1168                         case SF_STR_DATE :
1169                                 psf_binheader_writef (psf, "ms", ICRD_MARKER, psf->strings [k].str) ;
1170                                 break ;
1171                         } ;
1172                 } ;
1173
1174         saved_head_index = psf->headindex ;
1175         psf->headindex = prev_head_index ;
1176         psf_binheader_writef (psf, "4", saved_head_index - prev_head_index - 4) ;
1177         psf->headindex = saved_head_index ;
1178
1179 } /* wav_write_strings */
1180
1181 static int
1182 wav_close (SF_PRIVATE *psf)
1183 {
1184         if (psf->mode == SFM_WRITE || psf->mode == SFM_RDWR)
1185         {       wav_write_tailer (psf) ;
1186
1187                 psf->write_header (psf, SF_TRUE) ;
1188                 } ;
1189
1190         return 0 ;
1191 } /* wav_close */
1192
1193 static int
1194 wav_command (SF_PRIVATE *psf, int command, void *data, int datasize)
1195 {
1196         /* Avoid compiler warnings. */
1197         psf = psf ;
1198         data = data ;
1199         datasize = datasize ;
1200
1201         switch (command)
1202         {       default : break ;
1203                 } ;
1204
1205         return 0 ;
1206 } /* wav_command */
1207
1208 static int
1209 wav_subchunk_parse (SF_PRIVATE *psf, int chunk)
1210 {       sf_count_t      current_pos ;
1211         char            *cptr ;
1212         int             dword, bytesread, length ;
1213
1214         current_pos = psf_fseek (psf, 0, SEEK_CUR) ;
1215
1216         bytesread = psf_binheader_readf (psf, "4", &length) ;
1217
1218         if (length <= 8)
1219         {       /* This case is for broken files generated by PEAK. */
1220                 psf_log_printf (psf, "%M : %d (weird length)\n", chunk, length) ;
1221                 psf_binheader_readf (psf, "mj", &chunk, length - 4) ;
1222                 psf_log_printf (psf, "  %M\n", chunk) ;
1223                 return 0 ;
1224                 } ;
1225
1226         if (psf->headindex + length > SIGNED_SIZEOF (psf->header))
1227         {       psf_log_printf (psf, "%M : %d (too long)\n", chunk, length) ;
1228                 psf_binheader_readf (psf, "j", length) ;
1229                 return 0 ;
1230                 } ;
1231
1232         if (current_pos + length > psf->filelength)
1233         {       psf_log_printf (psf, "%M : %d (should be %d)\n", chunk, length, (int) (psf->filelength - current_pos)) ;
1234                 length = psf->filelength - current_pos ;
1235                 }
1236         else
1237                 psf_log_printf (psf, "%M : %d\n", chunk, length) ;
1238
1239         while (bytesread < length)
1240         {       bytesread += psf_binheader_readf (psf, "m", &chunk) ;
1241
1242                 switch (chunk)
1243                 {       case adtl_MARKER :
1244                         case INFO_MARKER :
1245                                         /* These markers don't contain anything. */
1246                                         psf_log_printf (psf, "  %M\n", chunk) ;
1247                                         break ;
1248
1249                         case data_MARKER:
1250                                         psf_log_printf (psf, "  %M inside a LIST block??? Backing out.\n", chunk) ;
1251                                         /* Jump back four bytes and return to caller. */
1252                                         psf_binheader_readf (psf, "j", -4) ;
1253                                         return 0 ;
1254
1255                         case ISFT_MARKER :
1256                         case ICOP_MARKER :
1257                         case IARL_MARKER :
1258                         case IART_MARKER :
1259                         case ICMT_MARKER :
1260                         case ICRD_MARKER :
1261                         case IENG_MARKER :
1262
1263                         case INAM_MARKER :
1264                         case IPRD_MARKER :
1265                         case ISBJ_MARKER :
1266                         case ISRC_MARKER :
1267                                         bytesread += psf_binheader_readf (psf, "4", &dword) ;
1268                                         dword += (dword & 1) ;
1269                                         if (dword < 0 || dword > SIGNED_SIZEOF (psf->u.cbuf))
1270                                         {       psf_log_printf (psf, "  *** %M : %d (too big)\n", chunk, dword) ;
1271                                                 psf_binheader_readf (psf, "j", dword) ;
1272                                                 break ;
1273                                                 } ;
1274
1275                                         cptr = psf->u.cbuf ;
1276                                         psf_binheader_readf (psf, "b", cptr, dword) ;
1277                                         bytesread += dword ;
1278                                         cptr [dword - 1] = 0 ;
1279                                         psf_log_printf (psf, "    %M : %s\n", chunk, cptr) ;
1280                                         break ;
1281
1282                         case labl_MARKER :
1283                                         {       int mark_id ;
1284
1285                                                 bytesread += psf_binheader_readf (psf, "44", &dword, &mark_id) ;
1286                                                 dword -= 4 ;
1287                                                 dword += (dword & 1) ;
1288                                                 if (dword < 1 || dword > SIGNED_SIZEOF (psf->u.cbuf))
1289                                                 {       psf_log_printf (psf, "  *** %M : %d (too big)\n", chunk, dword) ;
1290                                                         psf_binheader_readf (psf, "j", dword) ;
1291                                                         break ;
1292                                                         } ;
1293
1294                                                 cptr = psf->u.cbuf ;
1295                                                 psf_binheader_readf (psf, "b", cptr, dword) ;
1296                                                 bytesread += dword ;
1297                                                 cptr [dword - 1] = 0 ;
1298                                                 psf_log_printf (psf, "    %M : %d : %s\n", chunk, mark_id, cptr) ;
1299                                                 } ;
1300                                         break ;
1301
1302
1303                         case DISP_MARKER :
1304                         case ltxt_MARKER :
1305                         case note_MARKER :
1306                                         bytesread += psf_binheader_readf (psf, "4", &dword) ;
1307                                         dword += (dword & 1) ;
1308                                         psf_binheader_readf (psf, "j", dword) ;
1309                                         bytesread += dword ;
1310                                         psf_log_printf (psf, "    %M : %d\n", chunk, dword) ;
1311                                         break ;
1312
1313                         default :
1314                                         psf_binheader_readf (psf, "4", &dword) ;
1315                                         bytesread += sizeof (dword) ;
1316                                         dword += (dword & 1) ;
1317                                         psf_binheader_readf (psf, "j", dword) ;
1318                                         bytesread += dword ;
1319                                         psf_log_printf (psf, "    *** %M : %d\n", chunk, dword) ;
1320                                         if (dword > length)
1321                                                 return 0 ;
1322                                         break ;
1323                         } ;
1324
1325                 switch (chunk)
1326                 {       case ISFT_MARKER :
1327                                         psf_store_string (psf, SF_STR_SOFTWARE, psf->u.cbuf) ;
1328                                         break ;
1329                         case ICOP_MARKER :
1330                                         psf_store_string (psf, SF_STR_COPYRIGHT, psf->u.cbuf) ;
1331                                         break ;
1332                         case INAM_MARKER :
1333                                         psf_store_string (psf, SF_STR_TITLE, psf->u.cbuf) ;
1334                                         break ;
1335                         case IART_MARKER :
1336                                         psf_store_string (psf, SF_STR_ARTIST, psf->u.cbuf) ;
1337                                         break ;
1338                         case ICMT_MARKER :
1339                                         psf_store_string (psf, SF_STR_COMMENT, psf->u.cbuf) ;
1340                                         break ;
1341                         case ICRD_MARKER :
1342                                         psf_store_string (psf, SF_STR_DATE, psf->u.cbuf) ;
1343                                         break ;
1344                         } ;
1345                 } ;
1346
1347         current_pos = psf_fseek (psf, 0, SEEK_CUR) - current_pos ;
1348
1349         if (current_pos - 4 != length)
1350                 psf_log_printf (psf, "**** Bad chunk length %d sbould be %D\n", length, current_pos - 4) ;
1351
1352         return 0 ;
1353 } /* wav_subchunk_parse */
1354
1355 static int
1356 wav_read_smpl_chunk (SF_PRIVATE *psf, unsigned int chunklen)
1357 {       unsigned int bytesread = 0, dword, sampler_data, loop_count ;
1358         unsigned int note, start, end, type = -1, count ;
1359         int j, k ;
1360
1361         chunklen += (chunklen & 1) ;
1362
1363         bytesread += psf_binheader_readf (psf, "4", &dword) ;
1364         psf_log_printf (psf, "  Manufacturer : %X\n", dword) ;
1365
1366         bytesread += psf_binheader_readf (psf, "4", &dword) ;
1367         psf_log_printf (psf, "  Product      : %u\n", dword) ;
1368
1369         bytesread += psf_binheader_readf (psf, "4", &dword) ;
1370         psf_log_printf (psf, "  Period       : %u nsec\n", dword) ;
1371
1372         bytesread += psf_binheader_readf (psf, "4", &note) ;
1373         psf_log_printf (psf, "  Midi Note    : %u\n", note) ;
1374
1375         bytesread += psf_binheader_readf (psf, "4", &dword) ;
1376         if (dword != 0)
1377         {       LSF_SNPRINTF (psf->u.cbuf, sizeof (psf->u.cbuf), "%f",
1378                                  (1.0 * 0x80000000) / ((unsigned int) dword)) ;
1379                 psf_log_printf (psf, "  Pitch Fract. : %s\n", psf->u.cbuf) ;
1380                 }
1381         else
1382                 psf_log_printf (psf, "  Pitch Fract. : 0\n") ;
1383
1384         bytesread += psf_binheader_readf (psf, "4", &dword) ;
1385         psf_log_printf (psf, "  SMPTE Format : %u\n", dword) ;
1386
1387         bytesread += psf_binheader_readf (psf, "4", &dword) ;
1388         LSF_SNPRINTF (psf->u.cbuf, sizeof (psf->u.cbuf), "%02d:%02d:%02d %02d",
1389                  (dword >> 24) & 0x7F, (dword >> 16) & 0x7F, (dword >> 8) & 0x7F, dword & 0x7F) ;
1390         psf_log_printf (psf, "  SMPTE Offset : %s\n", psf->u.cbuf) ;
1391
1392         bytesread += psf_binheader_readf (psf, "4", &loop_count) ;
1393         psf_log_printf (psf, "  Loop Count   : %u\n", loop_count) ;
1394
1395         /* Sampler Data holds the number of data bytes after the CUE chunks which
1396         ** is not actually CUE data. Display value after CUE data.
1397         */
1398         bytesread += psf_binheader_readf (psf, "4", &sampler_data) ;
1399
1400         if ((psf->instrument = psf_instrument_alloc ()) == NULL)
1401                 return SFE_MALLOC_FAILED ;
1402
1403         psf->instrument->loop_count = loop_count ;
1404
1405         for (j = 0 ; loop_count > 0 && chunklen - bytesread >= 24 ; j ++)
1406         {       bytesread += psf_binheader_readf (psf, "4", &dword) ;
1407                 psf_log_printf (psf, "    Cue ID : %2u", dword) ;
1408
1409                 bytesread += psf_binheader_readf (psf, "4", &type) ;
1410                 psf_log_printf (psf, "  Type : %2u", type) ;
1411
1412                 bytesread += psf_binheader_readf (psf, "4", &start) ;
1413                 psf_log_printf (psf, "  Start : %5u", start) ;
1414
1415                 bytesread += psf_binheader_readf (psf, "4", &end) ;
1416                 psf_log_printf (psf, "  End : %5u", end) ;
1417
1418                 bytesread += psf_binheader_readf (psf, "4", &dword) ;
1419                 psf_log_printf (psf, "  Fraction : %5u", dword) ;
1420
1421                 bytesread += psf_binheader_readf (psf, "4", &count) ;
1422                 psf_log_printf (psf, "  Count : %5u\n", count) ;
1423
1424                 if (j < ARRAY_LEN (psf->instrument->loops))
1425                 {       psf->instrument->loops [j].start = start ;
1426                         psf->instrument->loops [j].end = end ;
1427                         psf->instrument->loops [j].count = count ;
1428
1429                         switch (type)
1430                         {       case 0 :
1431                                         psf->instrument->loops [j].mode = SF_LOOP_FORWARD ;
1432                                         break ;
1433                                 case 1 :
1434                                         psf->instrument->loops [j].mode = SF_LOOP_ALTERNATING ;
1435                                         break ;
1436                                 case 2 :
1437                                         psf->instrument->loops [j].mode = SF_LOOP_BACKWARD ;
1438                                         break ;
1439                                 default:
1440                                         psf->instrument->loops [j].mode = SF_LOOP_NONE ;
1441                                         break ;
1442                                 } ;
1443                         } ;
1444
1445                 loop_count -- ;
1446                 } ;
1447
1448         if (chunklen - bytesread == 0)
1449         {       if (sampler_data != 0)
1450                         psf_log_printf (psf, "  Sampler Data : %u (should be 0)\n", sampler_data) ;
1451                 else
1452                         psf_log_printf (psf, "  Sampler Data : %u\n", sampler_data) ;
1453                 }
1454         else
1455         {       if (sampler_data != chunklen - bytesread)
1456                 {       psf_log_printf (psf, "  Sampler Data : %u (should have been %u)\n", sampler_data, chunklen - bytesread) ;
1457                         sampler_data = chunklen - bytesread ;
1458                         }
1459                 else
1460                         psf_log_printf (psf, "  Sampler Data : %u\n", sampler_data) ;
1461
1462                 psf_log_printf (psf, "      ") ;
1463                 for (k = 0 ; k < (int) sampler_data ; k++)
1464                 {       char ch ;
1465
1466                         if (k > 0 && (k % 20) == 0)
1467                                 psf_log_printf (psf, "\n      ") ;
1468
1469                         bytesread += psf_binheader_readf (psf, "1", &ch) ;
1470                         psf_log_printf (psf, "%02X ", ch & 0xFF) ;
1471                         } ;
1472
1473                 psf_log_printf (psf, "\n") ;
1474                 } ;
1475
1476         psf->instrument->basenote = note ;
1477         psf->instrument->gain = 1 ;
1478         psf->instrument->velocity_lo = psf->instrument->key_lo = 0 ;
1479         psf->instrument->velocity_hi = psf->instrument->key_hi = 127 ;
1480
1481         return 0 ;
1482 } /* wav_read_smpl_chunk */
1483
1484 /*
1485 ** The acid chunk goes a little something like this:
1486 **
1487 ** 4 bytes          'acid'
1488 ** 4 bytes (int)     length of chunk starting at next byte
1489 **
1490 ** 4 bytes (int)     type of file:
1491 **        this appears to be a bit mask,however some combinations
1492 **        are probably impossible and/or qualified as "errors"
1493 **
1494 **        0x01 On: One Shot         Off: Loop
1495 **        0x02 On: Root note is Set Off: No root
1496 **        0x04 On: Stretch is On,   Off: Strech is OFF
1497 **        0x08 On: Disk Based       Off: Ram based
1498 **        0x10 On: ??????????       Off: ????????? (Acidizer puts that ON)
1499 **
1500 ** 2 bytes (short)      root note
1501 **        if type 0x10 is OFF : [C,C#,(...),B] -> [0x30 to 0x3B]
1502 **        if type 0x10 is ON  : [C,C#,(...),B] -> [0x3C to 0x47]
1503 **         (both types fit on same MIDI pitch albeit different octaves, so who cares)
1504 **
1505 ** 2 bytes (short)      ??? always set to 0x8000
1506 ** 4 bytes (float)      ??? seems to be always 0
1507 ** 4 bytes (int)        number of beats
1508 ** 2 bytes (short)      meter denominator   //always 4 in SF/ACID
1509 ** 2 bytes (short)      meter numerator     //always 4 in SF/ACID
1510 **                      //are we sure about the order?? usually its num/denom
1511 ** 4 bytes (float)      tempo
1512 **
1513 */
1514
1515 static int
1516 wav_read_acid_chunk (SF_PRIVATE *psf, unsigned int chunklen)
1517 {       unsigned int bytesread = 0 ;
1518         int     beats, flags ;
1519         short rootnote, q1, meter_denom, meter_numer ;
1520         float q2, tempo ;
1521
1522         chunklen += (chunklen & 1) ;
1523
1524         bytesread += psf_binheader_readf (psf, "422f", &flags, &rootnote, &q1, &q2) ;
1525
1526         LSF_SNPRINTF (psf->u.cbuf, sizeof (psf->u.cbuf), "%f", q2) ;
1527
1528         psf_log_printf (psf, "  Flags     : 0x%04x (%s,%s,%s,%s,%s)\n", flags,
1529                         (flags & 0x01) ? "OneShot" : "Loop",
1530                         (flags & 0x02) ? "RootNoteValid" : "RootNoteInvalid",
1531                         (flags & 0x04) ? "StretchOn" : "StretchOff",
1532                         (flags & 0x08) ? "DiskBased" : "RAMBased",
1533                         (flags & 0x10) ? "??On" : "??Off") ;
1534
1535         psf_log_printf (psf, "  Root note : 0x%x\n  ????      : 0x%04x\n  ????      : %s\n",
1536                                 rootnote, q1, psf->u.cbuf) ;
1537
1538         bytesread += psf_binheader_readf (psf, "422f", &beats, &meter_denom, &meter_numer, &tempo) ;
1539         LSF_SNPRINTF (psf->u.cbuf, sizeof (psf->u.cbuf), "%f", tempo) ;
1540         psf_log_printf (psf, "  Beats     : %d\n  Meter     : %d/%d\n  Tempo     : %s\n",
1541                                 beats, meter_numer, meter_denom, psf->u.cbuf) ;
1542
1543         psf_binheader_readf (psf, "j", chunklen - bytesread) ;
1544
1545         if ((psf->loop_info = calloc (1, sizeof (SF_LOOP_INFO))) == NULL)
1546                 return SFE_MALLOC_FAILED ;
1547
1548         psf->loop_info->time_sig_num    = meter_numer ;
1549         psf->loop_info->time_sig_den    = meter_denom ;
1550         psf->loop_info->loop_mode               = (flags & 0x01) ? SF_LOOP_NONE : SF_LOOP_FORWARD ;
1551         psf->loop_info->num_beats               = beats ;
1552         psf->loop_info->bpm                             = tempo ;
1553         psf->loop_info->root_key                = (flags & 0x02) ? rootnote : -1 ;
1554
1555         return 0 ;
1556 } /* wav_read_acid_chunk */
1557
1558 int
1559 wav_read_bext_chunk (SF_PRIVATE *psf, unsigned int chunksize)
1560 {
1561         SF_BROADCAST_INFO* b ;
1562
1563         if ((psf->broadcast_info = calloc (1, sizeof (SF_BROADCAST_INFO))) == NULL)
1564         {       psf->error = SFE_MALLOC_FAILED ;
1565                 return -1 ;
1566                 } ;
1567
1568         b = psf->broadcast_info ;
1569
1570         psf_binheader_readf (psf, "b", b->description, sizeof (b->description)) ;
1571         psf_binheader_readf (psf, "b", b->originator, sizeof (b->originator)) ;
1572         psf_binheader_readf (psf, "b", b->originator_reference, sizeof (b->originator_reference)) ;
1573         psf_binheader_readf (psf, "b", b->origination_date, sizeof (b->origination_date)) ;
1574         psf_binheader_readf (psf, "b", b->origination_time, sizeof (b->origination_time)) ;
1575         psf_binheader_readf (psf, "442", &b->time_reference_low, &b->time_reference_high, &b->version) ;
1576         psf_binheader_readf (psf, "bj", &b->umid, sizeof (b->umid), 190) ;
1577
1578         if (chunksize > WAV_BEXT_CHUNK_SIZE)
1579         {       /* File has coding history data. */
1580
1581                 b->coding_history_size = chunksize - WAV_BEXT_CHUNK_SIZE ;
1582
1583                 if (b->coding_history_size > SIGNED_SIZEOF (b->coding_history))
1584                 {       free (psf->broadcast_info) ;
1585                         psf->broadcast_info = NULL ;
1586                         psf->error = SFE_MALLOC_FAILED ;
1587                         return -1 ;
1588                         } ;
1589
1590                 /* We do not parse the coding history */
1591                 psf_binheader_readf (psf, "b", b->coding_history, b->coding_history_size) ;
1592                 b->coding_history [sizeof (b->coding_history) - 1] = 0 ;
1593                 } ;
1594
1595         return 0 ;
1596 } /* wav_read_bext_chunk */
1597
1598 static int
1599 wav_write_bext_chunk (SF_PRIVATE *psf)
1600 {       SF_BROADCAST_INFO *b ;
1601
1602         if ((b = psf->broadcast_info) == NULL)
1603                 return -1 ;
1604
1605         psf_binheader_writef (psf, "m4", bext_MARKER, WAV_BEXT_CHUNK_SIZE + b->coding_history_size) ;
1606
1607         /*
1608         **      Note that it is very important the the field widths of the SF_BROADCAST_INFO
1609         **      struct match those for the bext chunk fields.
1610         */
1611
1612         psf_binheader_writef (psf, "b", b->description, sizeof (b->description)) ;
1613         psf_binheader_writef (psf, "b", b->originator, sizeof (b->originator)) ;
1614         psf_binheader_writef (psf, "b", b->originator_reference, sizeof (b->originator_reference)) ;
1615         psf_binheader_writef (psf, "b", b->origination_date, sizeof (b->origination_date)) ;
1616         psf_binheader_writef (psf, "b", b->origination_time, sizeof (b->origination_time)) ;
1617         psf_binheader_writef (psf, "442", b->time_reference_low, b->time_reference_high, b->version) ;
1618         psf_binheader_writef (psf, "b", b->umid, sizeof (b->umid)) ;
1619         psf_binheader_writef (psf, "z", make_size_t (190)) ;
1620
1621         if (b->coding_history_size > 0)
1622                 psf_binheader_writef (psf, "b", b->coding_history, b->coding_history_size) ;
1623
1624         return 0 ;
1625 } /* wav_write_bext_chunk */
1626
1627 /*
1628 ** Do not edit or modify anything in this comment block.
1629 ** The arch-tag line is a file identity tag for the GNU Arch
1630 ** revision control system.
1631 **
1632 ** arch-tag: 9c551689-a1d8-4905-9f56-26a204374f18
1633 */