1 /*
2 ** Copyright (C) 1999-2018 Erik de Castro Lopo <erikd@mega-nerd.com>
3 **
4 ** This program is free software; you can redistribute it and/or modify
5 ** it under the terms of the GNU Lesser General Public License as published by
6 ** the Free Software Foundation; either version 2.1 of the License, or
7 ** (at your option) any later version.
8 **
9 ** This program is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ** GNU Lesser General Public License for more details.
13 **
14 ** You should have received a copy of the GNU Lesser General Public License
15 ** along with this program; if not, write to the Free Software
16 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 #include "sfconfig.h"
20
21 #include <stdio.h>
22 #include <string.h>
23 #include <ctype.h>
24 #include <time.h>
25
26 #include "sndfile.h"
27 #include "sfendian.h"
28 #include "common.h"
29 #include "wavlike.h"
30
31 /*------------------------------------------------------------------------------
32 ** W64 files use 16 byte markers as opposed to the four byte marker of
33 ** WAV files.
34 ** For comparison purposes, an integer is required, so make an integer
35 ** hash for the 16 bytes using MAKE_HASH16 macro, but also create a 16
36 ** byte array containing the complete 16 bytes required when writing the
37 ** header.
38 */
39
40 #define MAKE_HASH16(x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, xa, xb, xc, xd, xe, xf) \
41 ( (x0) ^ ((x1) << 1) ^ ((x2) << 2) ^ ((x3) << 3) ^ \
42 ((x4) << 4) ^ ((x5) << 5) ^ ((x6) << 6) ^ ((x7) << 7) ^ \
43 ((x8) << 8) ^ ((x9) << 9) ^ ((xa) << 10) ^ ((xb) << 11) ^ \
44 ((xc) << 12) ^ ((xd) << 13) ^ ((xe) << 14) ^ ((xf) << 15) )
45
46 #define MAKE_MARKER16(name, x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, xa, xb, xc, xd, xe, xf) \
47 static unsigned char name [16] = { (x0), (x1), (x2), (x3), (x4), (x5), \
48 (x6), (x7), (x8), (x9), (xa), (xb), (xc), (xd), (xe), (xf) }
49
50 #define riff_HASH16 MAKE_HASH16 ('r', 'i', 'f', 'f', 0x2E, 0x91, 0xCF, 0x11, \
51 0xA5, 0xD6, 0x28, 0xDB, 0x04, 0xC1, 0x00, 0x00)
52
53 #define wave_HASH16 MAKE_HASH16 ('w', 'a', 'v', 'e', 0xF3, 0xAC, 0xD3, 0x11, \
54 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A)
55
56 #define fmt_HASH16 MAKE_HASH16 ('f', 'm', 't', ' ', 0xF3, 0xAC, 0xD3, 0x11, \
57 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A)
58
59 #define fact_HASH16 MAKE_HASH16 ('f', 'a', 'c', 't', 0xF3, 0xAC, 0xD3, 0x11, \
60 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A)
61
62 #define data_HASH16 MAKE_HASH16 ('d', 'a', 't', 'a', 0xF3, 0xAC, 0xD3, 0x11, \
63 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A)
64
65 #define ACID_HASH16 MAKE_HASH16 (0x6D, 0x07, 0x1C, 0xEA, 0xA3, 0xEF, 0x78, 0x4C, \
66 0x90, 0x57, 0x7F, 0x79, 0xEE, 0x25, 0x2A, 0xAE)
67
68 #define levl_HASH16 MAKE_HASH16 (0x6c, 0x65, 0x76, 0x6c, 0xf3, 0xac, 0xd3, 0x11, \
69 0xd1, 0x8c, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A)
70
71 #define list_HASH16 MAKE_HASH16 (0x6C, 0x69, 0x73, 0x74, 0x2F, 0x91, 0xCF, 0x11, \
72 0xA5, 0xD6, 0x28, 0xDB, 0x04, 0xC1, 0x00, 0x00)
73
74 #define junk_HASH16 MAKE_HASH16 (0x6A, 0x75, 0x6E, 0x6b, 0xF3, 0xAC, 0xD3, 0x11, \
75 0x8C, 0xD1, 0x00, 0xC0, 0x4f, 0x8E, 0xDB, 0x8A)
76
77 #define bext_HASH16 MAKE_HASH16 (0x62, 0x65, 0x78, 0x74, 0xf3, 0xac, 0xd3, 0xaa, \
78 0xd1, 0x8c, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A)
79
80 #define MARKER_HASH16 MAKE_HASH16 (0x56, 0x62, 0xf7, 0xab, 0x2d, 0x39, 0xd2, 0x11, \
81 0x86, 0xc7, 0x00, 0xc0, 0x4f, 0x8e, 0xdb, 0x8a)
82
83 #define SUMLIST_HASH16 MAKE_HASH16 (0xBC, 0x94, 0x5F, 0x92, 0x5A, 0x52, 0xD2, 0x11, \
84 0x86, 0xDC, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A)
85
86
87 MAKE_MARKER16 (riff_MARKER16, 'r', 'i', 'f', 'f', 0x2E, 0x91, 0xCF, 0x11,
88 0xA5, 0xD6, 0x28, 0xDB, 0x04, 0xC1, 0x00, 0x00) ;
89
90
91 MAKE_MARKER16 (wave_MARKER16, 'w', 'a', 'v', 'e', 0xF3, 0xAC, 0xD3, 0x11,
92 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A) ;
93
94 MAKE_MARKER16 (fmt_MARKER16, 'f', 'm', 't', ' ', 0xF3, 0xAC, 0xD3, 0x11,
95 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A) ;
96
97 MAKE_MARKER16 (fact_MARKER16, 'f', 'a', 'c', 't', 0xF3, 0xAC, 0xD3, 0x11,
98 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A) ;
99
100 MAKE_MARKER16 (data_MARKER16, 'd', 'a', 't', 'a', 0xF3, 0xAC, 0xD3, 0x11,
101 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A) ;
102
103 enum
104 { HAVE_riff = 0x01,
105 HAVE_wave = 0x02,
106 HAVE_fmt = 0x04,
107 HAVE_fact = 0x08,
108 HAVE_data = 0x20
109 } ;
110
111 /*------------------------------------------------------------------------------
112 * Private static functions.
113 */
114
115 static int w64_read_header (SF_PRIVATE *psf, int *blockalign, int *framesperblock) ;
116 static int w64_write_header (SF_PRIVATE *psf, int calc_length) ;
117 static int w64_close (SF_PRIVATE *psf) ;
118
119 /*------------------------------------------------------------------------------
120 ** Public function.
121 */
122
123 int
w64_open(SF_PRIVATE * psf)124 w64_open (SF_PRIVATE *psf)
125 { WAVLIKE_PRIVATE * wpriv ;
126 int subformat, error, blockalign = 0, framesperblock = 0 ;
127
128 if ((wpriv = calloc (1, sizeof (WAVLIKE_PRIVATE))) == NULL)
129 return SFE_MALLOC_FAILED ;
130 psf->container_data = wpriv ;
131
132 if (psf->file.mode == SFM_READ || (psf->file.mode == SFM_RDWR &&psf->filelength > 0))
133 { if ((error = w64_read_header (psf, &blockalign, &framesperblock)))
134 return error ;
135 } ;
136
137 if ((SF_CONTAINER (psf->sf.format)) != SF_FORMAT_W64)
138 return SFE_BAD_OPEN_FORMAT ;
139
140 subformat = SF_CODEC (psf->sf.format) ;
141
142 if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
143 { if (psf->is_pipe)
144 return SFE_NO_PIPE_WRITE ;
145
146 psf->endian = SF_ENDIAN_LITTLE ; /* All W64 files are little endian. */
147
148 psf->blockwidth = psf->bytewidth * psf->sf.channels ;
149
150 if (subformat == SF_FORMAT_IMA_ADPCM || subformat == SF_FORMAT_MS_ADPCM)
151 { blockalign = wavlike_srate2blocksize (psf->sf.samplerate * psf->sf.channels) ;
152 framesperblock = -1 ;
153
154 /*
155 ** At this point we don't know the file length so set it stupidly high, but not
156 ** so high that it triggers undefined behaviour whan something is added to it.
157 */
158 psf->filelength = SF_COUNT_MAX - 10000 ;
159 psf->datalength = psf->filelength ;
160 if (psf->sf.frames <= 0)
161 psf->sf.frames = (psf->blockwidth) ? psf->filelength / psf->blockwidth : psf->filelength ;
162 } ;
163
164 if ((error = w64_write_header (psf, SF_FALSE)))
165 return error ;
166
167 psf->write_header = w64_write_header ;
168 } ;
169
170 psf->container_close = w64_close ;
171
172 switch (subformat)
173 { case SF_FORMAT_PCM_U8 :
174 error = pcm_init (psf) ;
175 break ;
176
177 case SF_FORMAT_PCM_16 :
178 case SF_FORMAT_PCM_24 :
179 case SF_FORMAT_PCM_32 :
180 error = pcm_init (psf) ;
181 break ;
182
183 case SF_FORMAT_ULAW :
184 error = ulaw_init (psf) ;
185 break ;
186
187 case SF_FORMAT_ALAW :
188 error = alaw_init (psf) ;
189 break ;
190
191 /* Lite remove start */
192 case SF_FORMAT_FLOAT :
193 error = float32_init (psf) ;
194 break ;
195
196 case SF_FORMAT_DOUBLE :
197 error = double64_init (psf) ;
198 break ;
199
200 case SF_FORMAT_IMA_ADPCM :
201 error = wavlike_ima_init (psf, blockalign, framesperblock) ;
202 break ;
203
204 case SF_FORMAT_MS_ADPCM :
205 error = wavlike_msadpcm_init (psf, blockalign, framesperblock) ;
206 break ;
207 /* Lite remove end */
208
209 case SF_FORMAT_GSM610 :
210 error = gsm610_init (psf) ;
211 break ;
212
213 default : return SFE_UNIMPLEMENTED ;
214 } ;
215
216 return error ;
217 } /* w64_open */
218
219 /*=========================================================================
220 ** Private functions.
221 */
222
223 static int
w64_read_header(SF_PRIVATE * psf,int * blockalign,int * framesperblock)224 w64_read_header (SF_PRIVATE *psf, int *blockalign, int *framesperblock)
225 { WAVLIKE_PRIVATE *wpriv ;
226 WAV_FMT *wav_fmt ;
227 int dword = 0, marker, format = 0 ;
228 sf_count_t chunk_size, bytesread = 0 ;
229 int parsestage = 0, error, done = 0 ;
230
231 if ((wpriv = psf->container_data) == NULL)
232 return SFE_INTERNAL ;
233 wav_fmt = &wpriv->wav_fmt ;
234
235 /* Set position to start of file to begin reading header. */
236 psf_binheader_readf (psf, "p", 0) ;
237
238 while (! done)
239 { /* Each new chunk must start on an 8 byte boundary, so jump if needed. */
240 if (psf->header.indx & 0x7)
241 psf_binheader_readf (psf, "j", 8 - (psf->header.indx & 0x7)) ;
242
243 /* Generate hash of 16 byte marker. */
244 marker = chunk_size = 0 ;
245 bytesread = psf_binheader_readf (psf, "eh8", &marker, &chunk_size) ;
246 if (bytesread == 0)
247 break ;
248 switch (marker)
249 { case riff_HASH16 :
250 if (parsestage)
251 return SFE_W64_NO_RIFF ;
252
253 if (psf->filelength != chunk_size)
254 psf_log_printf (psf, "riff : %D (should be %D)\n", chunk_size, psf->filelength) ;
255 else
256 psf_log_printf (psf, "riff : %D\n", chunk_size) ;
257
258 parsestage |= HAVE_riff ;
259
260 bytesread += psf_binheader_readf (psf, "h", &marker) ;
261 if (marker == wave_HASH16)
262 { if ((parsestage & HAVE_riff) != HAVE_riff)
263 return SFE_W64_NO_WAVE ;
264 psf_log_printf (psf, "wave\n") ;
265 parsestage |= HAVE_wave ;
266 } ;
267 chunk_size = 0 ;
268 break ;
269
270 case ACID_HASH16:
271 psf_log_printf (psf, "Looks like an ACID file. Exiting.\n") ;
272 return SFE_UNIMPLEMENTED ;
273
274 case fmt_HASH16 :
275 if ((parsestage & (HAVE_riff | HAVE_wave)) != (HAVE_riff | HAVE_wave))
276 return SFE_WAV_NO_FMT ;
277
278 psf_log_printf (psf, " fmt : %D\n", chunk_size) ;
279
280 /* size of 16 byte marker and 8 byte chunk_size value. */
281 chunk_size -= 24 ;
282
283 if ((error = wavlike_read_fmt_chunk (psf, (int) chunk_size)))
284 return error ;
285
286 if (chunk_size % 8)
287 psf_binheader_readf (psf, "j", 8 - (chunk_size % 8)) ;
288
289 format = wav_fmt->format ;
290 parsestage |= HAVE_fmt ;
291 chunk_size = 0 ;
292 break ;
293
294 case fact_HASH16:
295 { sf_count_t frames ;
296
297 psf_binheader_readf (psf, "e8", &frames) ;
298 psf_log_printf (psf, "fact : %D\n frames : %D\n",
299 chunk_size, frames) ;
300 } ;
301 chunk_size = 0 ;
302 break ;
303
304
305 case data_HASH16 :
306 if ((parsestage & (HAVE_riff | HAVE_wave | HAVE_fmt)) != (HAVE_riff | HAVE_wave | HAVE_fmt))
307 return SFE_W64_NO_DATA ;
308
309 psf->dataoffset = psf_ftell (psf) ;
310 psf->datalength = SF_MIN (chunk_size - 24, psf->filelength - psf->dataoffset) ;
311
312 if (chunk_size % 8)
313 chunk_size += 8 - (chunk_size % 8) ;
314
315 psf_log_printf (psf, "data : %D\n", chunk_size) ;
316
317 parsestage |= HAVE_data ;
318
319 if (! psf->sf.seekable)
320 break ;
321
322 /* Seek past data and continue reading header. */
323 psf_fseek (psf, chunk_size, SEEK_CUR) ;
324 chunk_size = 0 ;
325 break ;
326
327 case levl_HASH16 :
328 psf_log_printf (psf, "levl : %D\n", chunk_size) ;
329 break ;
330
331 case list_HASH16 :
332 psf_log_printf (psf, "list : %D\n", chunk_size) ;
333 break ;
334
335 case junk_HASH16 :
336 psf_log_printf (psf, "junk : %D\n", chunk_size) ;
337 break ;
338
339 case bext_HASH16 :
340 psf_log_printf (psf, "bext : %D\n", chunk_size) ;
341 break ;
342
343 case MARKER_HASH16 :
344 psf_log_printf (psf, "marker : %D\n", chunk_size) ;
345 break ;
346
347 case SUMLIST_HASH16 :
348 psf_log_printf (psf, "summary list : %D\n", chunk_size) ;
349 break ;
350
351 default :
352 psf_log_printf (psf, "*** Unknown chunk marker (%X) at position %D with length %D. Skipping and continuing.\n", marker, psf_ftell (psf) - 8, chunk_size) ;
353 break ;
354 } ; /* switch (dword) */
355
356 if (chunk_size >= psf->filelength)
357 { psf_log_printf (psf, "*** Chunk size %u > file length %D. Exiting parser.\n", chunk_size, psf->filelength) ;
358 break ;
359 } ;
360
361 if (psf->sf.seekable == 0 && (parsestage & HAVE_data))
362 break ;
363
364 if (psf_ftell (psf) >= (psf->filelength - (2 * SIGNED_SIZEOF (dword))))
365 break ;
366
367 if (chunk_size > 0 && chunk_size < 0xffff0000)
368 { dword = chunk_size ;
369 psf_binheader_readf (psf, "j", dword - 24) ;
370 } ;
371 } ; /* while (1) */
372
373 if (psf->dataoffset <= 0)
374 return SFE_W64_NO_DATA ;
375
376 if (psf->sf.channels < 1)
377 return SFE_CHANNEL_COUNT_ZERO ;
378
379 if (psf->sf.channels > SF_MAX_CHANNELS)
380 return SFE_CHANNEL_COUNT ;
381
382 psf->endian = SF_ENDIAN_LITTLE ; /* All W64 files are little endian. */
383
384 if (psf_ftell (psf) != psf->dataoffset)
385 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
386
387 if (psf->blockwidth)
388 { if (psf->filelength - psf->dataoffset < psf->datalength)
389 psf->sf.frames = (psf->filelength - psf->dataoffset) / psf->blockwidth ;
390 else
391 psf->sf.frames = psf->datalength / psf->blockwidth ;
392 } ;
393
394 switch (format)
395 { case WAVE_FORMAT_PCM :
396 case WAVE_FORMAT_EXTENSIBLE :
397 /* extensible might be FLOAT, MULAW, etc as well! */
398 psf->sf.format = SF_FORMAT_W64 | u_bitwidth_to_subformat (psf->bytewidth * 8) ;
399 break ;
400
401 case WAVE_FORMAT_MULAW :
402 psf->sf.format = (SF_FORMAT_W64 | SF_FORMAT_ULAW) ;
403 break ;
404
405 case WAVE_FORMAT_ALAW :
406 psf->sf.format = (SF_FORMAT_W64 | SF_FORMAT_ALAW) ;
407 break ;
408
409 case WAVE_FORMAT_MS_ADPCM :
410 psf->sf.format = (SF_FORMAT_W64 | SF_FORMAT_MS_ADPCM) ;
411 *blockalign = wav_fmt->msadpcm.blockalign ;
412 *framesperblock = wav_fmt->msadpcm.samplesperblock ;
413 break ;
414
415 case WAVE_FORMAT_IMA_ADPCM :
416 psf->sf.format = (SF_FORMAT_W64 | SF_FORMAT_IMA_ADPCM) ;
417 *blockalign = wav_fmt->ima.blockalign ;
418 *framesperblock = wav_fmt->ima.samplesperblock ;
419 break ;
420
421 case WAVE_FORMAT_GSM610 :
422 psf->sf.format = (SF_FORMAT_W64 | SF_FORMAT_GSM610) ;
423 break ;
424
425 case WAVE_FORMAT_IEEE_FLOAT :
426 psf->sf.format = SF_FORMAT_W64 ;
427 psf->sf.format |= (psf->bytewidth == 8) ? SF_FORMAT_DOUBLE : SF_FORMAT_FLOAT ;
428 break ;
429
430 default : return SFE_UNIMPLEMENTED ;
431 } ;
432
433 return 0 ;
434 } /* w64_read_header */
435
436 static int
w64_write_header(SF_PRIVATE * psf,int calc_length)437 w64_write_header (SF_PRIVATE *psf, int calc_length)
438 { sf_count_t fmt_size, current ;
439 size_t fmt_pad = 0 ;
440 int subformat, add_fact_chunk = SF_FALSE ;
441
442 current = psf_ftell (psf) ;
443
444 if (calc_length)
445 { psf->filelength = psf_get_filelen (psf) ;
446
447 psf->datalength = psf->filelength - psf->dataoffset ;
448 if (psf->dataend)
449 psf->datalength -= psf->filelength - psf->dataend ;
450
451 if (psf->bytewidth)
452 psf->sf.frames = psf->datalength / (psf->bytewidth * psf->sf.channels) ;
453 } ;
454
455 /* Reset the current header length to zero. */
456 psf->header.ptr [0] = 0 ;
457 psf->header.indx = 0 ;
458 psf_fseek (psf, 0, SEEK_SET) ;
459
460 /* riff marker, length, wave and 'fmt ' markers. */
461 psf_binheader_writef (psf, "eh8hh", BHWh (riff_MARKER16), BHW8 (psf->filelength), BHWh (wave_MARKER16), BHWh (fmt_MARKER16)) ;
462
463 subformat = SF_CODEC (psf->sf.format) ;
464
465 switch (subformat)
466 { case SF_FORMAT_PCM_U8 :
467 case SF_FORMAT_PCM_16 :
468 case SF_FORMAT_PCM_24 :
469 case SF_FORMAT_PCM_32 :
470 fmt_size = 24 + 2 + 2 + 4 + 4 + 2 + 2 ;
471 fmt_pad = (size_t) ((fmt_size & 0x7) ? 8 - (fmt_size & 0x7) : 0) ;
472 fmt_size += fmt_pad ;
473
474 /* fmt : format, channels, samplerate */
475 psf_binheader_writef (psf, "e8224", BHW8 (fmt_size), BHW2 (WAVE_FORMAT_PCM), BHW2 (psf->sf.channels), BHW4 (psf->sf.samplerate)) ;
476 /* fmt : bytespersec */
477 psf_binheader_writef (psf, "e4", BHW4 (psf->sf.samplerate * psf->bytewidth * psf->sf.channels)) ;
478 /* fmt : blockalign, bitwidth */
479 psf_binheader_writef (psf, "e22", BHW2 (psf->bytewidth * psf->sf.channels), BHW2 (psf->bytewidth * 8)) ;
480 break ;
481
482 case SF_FORMAT_FLOAT :
483 case SF_FORMAT_DOUBLE :
484 fmt_size = 24 + 2 + 2 + 4 + 4 + 2 + 2 ;
485 fmt_pad = (size_t) ((fmt_size & 0x7) ? 8 - (fmt_size & 0x7) : 0) ;
486 fmt_size += fmt_pad ;
487
488 /* fmt : format, channels, samplerate */
489 psf_binheader_writef (psf, "e8224", BHW8 (fmt_size), BHW2 (WAVE_FORMAT_IEEE_FLOAT), BHW2 (psf->sf.channels), BHW4 (psf->sf.samplerate)) ;
490 /* fmt : bytespersec */
491 psf_binheader_writef (psf, "e4", BHW4 (psf->sf.samplerate * psf->bytewidth * psf->sf.channels)) ;
492 /* fmt : blockalign, bitwidth */
493 psf_binheader_writef (psf, "e22", BHW2 (psf->bytewidth * psf->sf.channels), BHW2 (psf->bytewidth * 8)) ;
494
495 add_fact_chunk = SF_TRUE ;
496 break ;
497
498 case SF_FORMAT_ULAW :
499 fmt_size = 24 + 2 + 2 + 4 + 4 + 2 + 2 ;
500 fmt_pad = (size_t) ((fmt_size & 0x7) ? 8 - (fmt_size & 0x7) : 0) ;
501 fmt_size += fmt_pad ;
502
503 /* fmt : format, channels, samplerate */
504 psf_binheader_writef (psf, "e8224", BHW8 (fmt_size), BHW2 (WAVE_FORMAT_MULAW), BHW2 (psf->sf.channels), BHW4 (psf->sf.samplerate)) ;
505 /* fmt : bytespersec */
506 psf_binheader_writef (psf, "e4", BHW4 (psf->sf.samplerate * psf->bytewidth * psf->sf.channels)) ;
507 /* fmt : blockalign, bitwidth */
508 psf_binheader_writef (psf, "e22", BHW2 (psf->bytewidth * psf->sf.channels), BHW2 (8)) ;
509
510 add_fact_chunk = SF_TRUE ;
511 break ;
512
513 case SF_FORMAT_ALAW :
514 fmt_size = 24 + 2 + 2 + 4 + 4 + 2 + 2 ;
515 fmt_pad = (size_t) ((fmt_size & 0x7) ? 8 - (fmt_size & 0x7) : 0) ;
516 fmt_size += fmt_pad ;
517
518 /* fmt : format, channels, samplerate */
519 psf_binheader_writef (psf, "e8224", BHW8 (fmt_size), BHW2 (WAVE_FORMAT_ALAW), BHW2 (psf->sf.channels), BHW4 (psf->sf.samplerate)) ;
520 /* fmt : bytespersec */
521 psf_binheader_writef (psf, "e4", BHW4 (psf->sf.samplerate * psf->bytewidth * psf->sf.channels)) ;
522 /* fmt : blockalign, bitwidth */
523 psf_binheader_writef (psf, "e22", BHW2 (psf->bytewidth * psf->sf.channels), BHW2 (8)) ;
524
525 add_fact_chunk = SF_TRUE ;
526 break ;
527
528 /* Lite remove start */
529 case SF_FORMAT_IMA_ADPCM :
530 { int blockalign, framesperblock, bytespersec ;
531
532 blockalign = wavlike_srate2blocksize (psf->sf.samplerate * psf->sf.channels) ;
533 framesperblock = 2 * (blockalign - 4 * psf->sf.channels) / psf->sf.channels + 1 ;
534 bytespersec = (psf->sf.samplerate * blockalign) / framesperblock ;
535
536 /* fmt chunk. */
537 fmt_size = 24 + 2 + 2 + 4 + 4 + 2 + 2 + 2 + 2 ;
538 fmt_pad = (size_t) ((fmt_size & 0x7) ? 8 - (fmt_size & 0x7) : 0) ;
539 fmt_size += fmt_pad ;
540
541 /* fmt : size, WAV format type, channels. */
542 psf_binheader_writef (psf, "e822", BHW8 (fmt_size), BHW2 (WAVE_FORMAT_IMA_ADPCM), BHW2 (psf->sf.channels)) ;
543
544 /* fmt : samplerate, bytespersec. */
545 psf_binheader_writef (psf, "e44", BHW4 (psf->sf.samplerate), BHW4 (bytespersec)) ;
546
547 /* fmt : blockalign, bitwidth, extrabytes, framesperblock. */
548 psf_binheader_writef (psf, "e2222", BHW2 (blockalign), BHW2 (4), BHW2 (2), BHW2 (framesperblock)) ;
549 } ;
550
551 add_fact_chunk = SF_TRUE ;
552 break ;
553
554 case SF_FORMAT_MS_ADPCM :
555 { int blockalign, framesperblock, bytespersec, extrabytes ;
556
557 blockalign = wavlike_srate2blocksize (psf->sf.samplerate * psf->sf.channels) ;
558 framesperblock = 2 + 2 * (blockalign - 7 * psf->sf.channels) / psf->sf.channels ;
559 bytespersec = (psf->sf.samplerate * blockalign) / framesperblock ;
560
561 /* fmt chunk. */
562 extrabytes = 2 + 2 + WAVLIKE_MSADPCM_ADAPT_COEFF_COUNT * (2 + 2) ;
563 fmt_size = 24 + 2 + 2 + 4 + 4 + 2 + 2 + 2 + extrabytes ;
564 fmt_pad = (size_t) ((fmt_size & 0x7) ? 8 - (fmt_size & 0x7) : 0) ;
565 fmt_size += fmt_pad ;
566
567 /* fmt : size, W64 format type, channels. */
568 psf_binheader_writef (psf, "e822", BHW8 (fmt_size), BHW2 (WAVE_FORMAT_MS_ADPCM), BHW2 (psf->sf.channels)) ;
569
570 /* fmt : samplerate, bytespersec. */
571 psf_binheader_writef (psf, "e44", BHW4 (psf->sf.samplerate), BHW4 (bytespersec)) ;
572
573 /* fmt : blockalign, bitwidth, extrabytes, framesperblock. */
574 psf_binheader_writef (psf, "e22222", BHW2 (blockalign), BHW2 (4), BHW2 (extrabytes), BHW2 (framesperblock), BHW2 (7)) ;
575
576 wavlike_msadpcm_write_adapt_coeffs (psf) ;
577 } ;
578
579 add_fact_chunk = SF_TRUE ;
580 break ;
581 /* Lite remove end */
582
583 case SF_FORMAT_GSM610 :
584 { int bytespersec ;
585
586 bytespersec = (psf->sf.samplerate * WAVLIKE_GSM610_BLOCKSIZE) / WAVLIKE_GSM610_SAMPLES ;
587
588 /* fmt chunk. */
589 fmt_size = 24 + 2 + 2 + 4 + 4 + 2 + 2 + 2 + 2 ;
590 fmt_pad = (size_t) ((fmt_size & 0x7) ? 8 - (fmt_size & 0x7) : 0) ;
591 fmt_size += fmt_pad ;
592
593 /* fmt : size, WAV format type, channels. */
594 psf_binheader_writef (psf, "e822", BHW8 (fmt_size), BHW2 (WAVE_FORMAT_GSM610), BHW2 (psf->sf.channels)) ;
595
596 /* fmt : samplerate, bytespersec. */
597 psf_binheader_writef (psf, "e44", BHW4 (psf->sf.samplerate), BHW4 (bytespersec)) ;
598
599 /* fmt : blockalign, bitwidth, extrabytes, framesperblock. */
600 psf_binheader_writef (psf, "e2222", BHW2 (WAVLIKE_GSM610_BLOCKSIZE), BHW2 (0), BHW2 (2), BHW2 (WAVLIKE_GSM610_SAMPLES)) ;
601 } ;
602
603 add_fact_chunk = SF_TRUE ;
604 break ;
605
606 default : return SFE_UNIMPLEMENTED ;
607 } ;
608
609 /* Pad to 8 bytes with zeros. */
610 if (fmt_pad > 0)
611 psf_binheader_writef (psf, "z", BHWz (fmt_pad)) ;
612
613 if (add_fact_chunk)
614 psf_binheader_writef (psf, "eh88", BHWh (fact_MARKER16), BHW8 ((sf_count_t) (16 + 8 + 8)), BHW8 (psf->sf.frames)) ;
615
616 psf_binheader_writef (psf, "eh8", BHWh (data_MARKER16), BHW8 (psf->datalength + 24)) ;
617 psf_fwrite (psf->header.ptr, psf->header.indx, 1, psf) ;
618
619 if (psf->error)
620 return psf->error ;
621
622 psf->dataoffset = psf->header.indx ;
623
624 if (current > 0)
625 psf_fseek (psf, current, SEEK_SET) ;
626
627 return psf->error ;
628 } /* w64_write_header */
629
630 static int
w64_close(SF_PRIVATE * psf)631 w64_close (SF_PRIVATE *psf)
632 {
633 if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
634 w64_write_header (psf, SF_TRUE) ;
635
636 return 0 ;
637 } /* w64_close */
638
639