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