d40bfab84296fc47fb4c2834bb2abf70c3bf9535
[Faustine.git] / interpretor / lib / src / libsndfile-1.0.25 / src / rf64.c
1 /*
2 ** Copyright (C) 2008-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
3 ** Copyright (C) 2009 Uli Franke <cls@nebadje.org>
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 /*
21 ** This format documented at:
22 ** http://www.sr.se/utveckling/tu/bwf/prog/RF_64v1_4.pdf
23 **
24 ** But this may be a better reference:
25 ** http://www.ebu.ch/CMSimages/en/tec_doc_t3306-2007_tcm6-42570.pdf
26 */
27
28 #include "sfconfig.h"
29
30 #include <stdio.h>
31 #include <fcntl.h>
32 #include <string.h>
33 #include <ctype.h>
34
35 #include "sndfile.h"
36 #include "sfendian.h"
37 #include "common.h"
38 #include "wav_w64.h"
39
40 /*------------------------------------------------------------------------------
41 ** Macros to handle big/little endian issues.
42 */
43 #define RF64_MARKER MAKE_MARKER ('R', 'F', '6', '4')
44 #define FFFF_MARKER MAKE_MARKER (0xff, 0xff, 0xff, 0xff)
45 #define WAVE_MARKER MAKE_MARKER ('W', 'A', 'V', 'E')
46 #define ds64_MARKER MAKE_MARKER ('d', 's', '6', '4')
47 #define fmt_MARKER MAKE_MARKER ('f', 'm', 't', ' ')
48 #define fact_MARKER MAKE_MARKER ('f', 'a', 'c', 't')
49 #define data_MARKER MAKE_MARKER ('d', 'a', 't', 'a')
50
51 #define bext_MARKER MAKE_MARKER ('b', 'e', 'x', 't')
52 #define OggS_MARKER MAKE_MARKER ('O', 'g', 'g', 'S')
53 #define wvpk_MARKER MAKE_MARKER ('w', 'v', 'p', 'k')
54
55 /*------------------------------------------------------------------------------
56 ** Typedefs.
57 */
58
59 /*------------------------------------------------------------------------------
60 ** Private static functions.
61 */
62
63 static int rf64_read_header (SF_PRIVATE *psf, int *blockalign, int *framesperblock) ;
64 static int rf64_write_header (SF_PRIVATE *psf, int calc_length) ;
65 static int rf64_close (SF_PRIVATE *psf) ;
66 static int rf64_command (SF_PRIVATE *psf, int command, void * UNUSED (data), int datasize) ;
67
68 /*------------------------------------------------------------------------------
69 ** Public function.
70 */
71
72 int
73 rf64_open (SF_PRIVATE *psf)
74 { WAV_PRIVATE *wpriv ;
75 int subformat, error = 0 ;
76 int blockalign, framesperblock ;
77
78 if ((wpriv = calloc (1, sizeof (WAV_PRIVATE))) == NULL)
79 return SFE_MALLOC_FAILED ;
80 psf->container_data = wpriv ;
81 wpriv->wavex_ambisonic = SF_AMBISONIC_NONE ;
82
83 /* All RF64 files are little endian. */
84 psf->endian = SF_ENDIAN_LITTLE ;
85
86
87 if (psf->file.mode == SFM_READ || (psf->file.mode == SFM_RDWR && psf->filelength > 0))
88 { if ((error = rf64_read_header (psf, &blockalign, &framesperblock)) != 0)
89 return error ;
90 } ;
91
92 if ((psf->sf.format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RF64)
93 return SFE_BAD_OPEN_FORMAT ;
94
95 subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
96
97 if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
98 { if (psf->is_pipe)
99 return SFE_NO_PIPE_WRITE ;
100
101 psf->blockwidth = psf->bytewidth * psf->sf.channels ;
102
103 if ((error = rf64_write_header (psf, SF_FALSE)))
104 return error ;
105
106 psf->write_header = rf64_write_header ;
107 } ;
108
109 psf->container_close = rf64_close ;
110 psf->command = rf64_command ;
111
112 switch (subformat)
113 { case SF_FORMAT_PCM_U8 :
114 case SF_FORMAT_PCM_16 :
115 case SF_FORMAT_PCM_24 :
116 case SF_FORMAT_PCM_32 :
117 error = pcm_init (psf) ;
118 break ;
119
120 case SF_FORMAT_ULAW :
121 error = ulaw_init (psf) ;
122 break ;
123
124 case SF_FORMAT_ALAW :
125 error = alaw_init (psf) ;
126 break ;
127
128 /* Lite remove start */
129 case SF_FORMAT_FLOAT :
130 error = float32_init (psf) ;
131 break ;
132
133 case SF_FORMAT_DOUBLE :
134 error = double64_init (psf) ;
135 break ;
136
137 /* Lite remove end */
138
139 default : return SFE_UNIMPLEMENTED ;
140 } ;
141
142 return error ;
143 } /* rf64_open */
144
145 /*------------------------------------------------------------------------------
146 */
147 enum
148 { HAVE_ds64 = 0x01,
149 HAVE_fmt = 0x02,
150 HAVE_bext = 0x04,
151 HAVE_data = 0x08
152 } ;
153
154 #define HAVE_CHUNK(CHUNK) ((parsestage & CHUNK) != 0)
155
156 static int
157 rf64_read_header (SF_PRIVATE *psf, int *blockalign, int *framesperblock)
158 { WAV_PRIVATE *wpriv ;
159 WAV_FMT *wav_fmt ;
160 sf_count_t riff_size = 0, frame_count = 0, ds64_datalength = 0 ;
161 unsigned int size32, parsestage = 0 ;
162 int marker, marks [2], error, done = 0, format = 0 ;
163
164 if ((wpriv = psf->container_data) == NULL)
165 return SFE_INTERNAL ;
166 wav_fmt = &wpriv->wav_fmt ;
167
168 /* Set position to start of file to begin reading header. */
169 psf_binheader_readf (psf, "pmmm", 0, &marker, marks, marks + 1) ;
170 if (marker != RF64_MARKER || marks [1] != WAVE_MARKER)
171 return SFE_RF64_NOT_RF64 ;
172
173 if (marks [0] == FFFF_MARKER)
174 psf_log_printf (psf, "%M\n %M\n", RF64_MARKER, WAVE_MARKER) ;
175 else
176 psf_log_printf (psf, "%M : 0x%x (should be 0xFFFFFFFF)\n %M\n", RF64_MARKER, WAVE_MARKER) ;
177
178 while (NOT (done))
179 { psf_binheader_readf (psf, "em4", &marker, &size32) ;
180
181 switch (marker)
182 { case ds64_MARKER :
183 { unsigned int table_len, bytesread ;
184
185 /* Read ds64 sizes (3 8-byte words). */
186 bytesread = psf_binheader_readf (psf, "888", &riff_size, &ds64_datalength, &frame_count) ;
187
188 /* Read table length. */
189 bytesread += psf_binheader_readf (psf, "4", &table_len) ;
190 /* Skip table for now. (this was "table_len + 4", why?) */
191 bytesread += psf_binheader_readf (psf, "j", table_len) ;
192
193 if (size32 == bytesread)
194 psf_log_printf (psf, "%M : %u\n", marker, size32) ;
195 else if (size32 >= bytesread + 4)
196 { unsigned int next ;
197 psf_binheader_readf (psf, "m", &next) ;
198 if (next == fmt_MARKER)
199 { psf_log_printf (psf, "%M : %u (should be %u)\n", marker, size32, bytesread) ;
200 psf_binheader_readf (psf, "j", -4) ;
201 }
202 else
203 { psf_log_printf (psf, "%M : %u\n", marker, size32) ;
204 psf_binheader_readf (psf, "j", size32 - bytesread - 4) ;
205 } ;
206 } ;
207
208 if (psf->filelength != riff_size + 8)
209 psf_log_printf (psf, " Riff size : %D (should be %D)\n", riff_size, psf->filelength - 8) ;
210 else
211 psf_log_printf (psf, " Riff size : %D\n", riff_size) ;
212
213 psf_log_printf (psf, " Data size : %D\n", ds64_datalength) ;
214
215 psf_log_printf (psf, " Frames : %D\n", frame_count) ;
216 psf_log_printf (psf, " Table length : %u\n", table_len) ;
217
218 } ;
219 parsestage |= HAVE_ds64 ;
220 break ;
221
222 case fmt_MARKER:
223 psf_log_printf (psf, "%M : %u\n", marker, size32) ;
224 if ((error = wav_w64_read_fmt_chunk (psf, size32)) != 0)
225 return error ;
226 format = wav_fmt->format ;
227 parsestage |= HAVE_fmt ;
228 break ;
229
230 case bext_MARKER :
231 if ((error = wav_read_bext_chunk (psf, size32)) != 0)
232 return error ;
233 parsestage |= HAVE_bext ;
234 break ;
235
236 case data_MARKER :
237 /* see wav for more sophisticated parsing -> implement state machine with parsestage */
238
239 if (HAVE_CHUNK (HAVE_ds64))
240 { if (size32 == 0xffffffff)
241 psf_log_printf (psf, "%M : 0x%x\n", marker, size32) ;
242 else
243 psf_log_printf (psf, "%M : 0x%x (should be 0xffffffff\n", marker, size32) ;
244 psf->datalength = ds64_datalength ;
245 }
246 else
247 { if (size32 == 0xffffffff)
248 { psf_log_printf (psf, "%M : 0x%x\n", marker, size32) ;
249 psf_log_printf (psf, " *** Data length not specified no 'ds64' chunk.\n") ;
250 }
251 else
252 { psf_log_printf (psf, "%M : 0x%x\n**** Weird, RF64 file without a 'ds64' chunk and no valid 'data' size.\n", marker, size32) ;
253 psf->datalength = size32 ;
254 } ;
255 } ;
256
257 psf->dataoffset = psf_ftell (psf) ;
258
259 if (psf->dataoffset > 0)
260 { if (size32 == 0 && riff_size == 8 && psf->filelength > 44)
261 { psf_log_printf (psf, " *** Looks like a WAV file which wasn't closed properly. Fixing it.\n") ;
262 psf->datalength = psf->filelength - psf->dataoffset ;
263 } ;
264
265 /* Only set dataend if there really is data at the end. */
266 if (psf->datalength + psf->dataoffset < psf->filelength)
267 psf->dataend = psf->datalength + psf->dataoffset ;
268
269 if (NOT (psf->sf.seekable) || psf->dataoffset < 0)
270 break ;
271
272 /* Seek past data and continue reading header. */
273 psf_fseek (psf, psf->datalength, SEEK_CUR) ;
274
275 if (psf_ftell (psf) != psf->datalength + psf->dataoffset)
276 psf_log_printf (psf, " *** psf_fseek past end error ***\n") ;
277 break ;
278 } ;
279 break ;
280
281 default :
282 if (isprint ((marker >> 24) & 0xFF) && isprint ((marker >> 16) & 0xFF)
283 && isprint ((marker >> 8) & 0xFF) && isprint (marker & 0xFF))
284 { psf_log_printf (psf, "*** %M : %d (unknown marker)\n", marker, size32) ;
285 if (size32 < 8)
286 done = SF_TRUE ;
287 psf_binheader_readf (psf, "j", size32) ;
288 break ;
289 } ;
290 if (psf_ftell (psf) & 0x03)
291 { psf_log_printf (psf, " Unknown chunk marker at position 0x%x. Resynching.\n", size32 - 4) ;
292 psf_binheader_readf (psf, "j", -3) ;
293 break ;
294 } ;
295 psf_log_printf (psf, "*** Unknown chunk marker (0x%X) at position 0x%X. Exiting parser.\n", marker, psf_ftell (psf) - 4) ;
296 done = SF_TRUE ;
297 break ;
298 } ; /* switch (marker) */
299
300 if (psf_ftell (psf) >= psf->filelength - SIGNED_SIZEOF (marker))
301 { psf_log_printf (psf, "End\n") ;
302 break ;
303 } ;
304 } ;
305
306 if (psf->dataoffset <= 0)
307 return SFE_WAV_NO_DATA ;
308
309 /* WAVs can be little or big endian */
310 psf->endian = psf->rwf_endian ;
311
312 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
313
314 if (psf->is_pipe == 0)
315 { /*
316 ** Check for 'wvpk' at the start of the DATA section. Not able to
317 ** handle this.
318 */
319 psf_binheader_readf (psf, "4", &marker) ;
320 if (marker == wvpk_MARKER || marker == OggS_MARKER)
321 return SFE_WAV_WVPK_DATA ;
322 } ;
323
324 /* Seek to start of DATA section. */
325 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
326
327 if (psf->blockwidth)
328 { if (psf->filelength - psf->dataoffset < psf->datalength)
329 psf->sf.frames = (psf->filelength - psf->dataoffset) / psf->blockwidth ;
330 else
331 psf->sf.frames = psf->datalength / psf->blockwidth ;
332 } ;
333
334 if (frame_count != psf->sf.frames)
335 psf_log_printf (psf, "*** Calculated frame count %d does not match value from 'ds64' chunk of %d.\n", psf->sf.frames, frame_count) ;
336
337 switch (format)
338 {
339 case WAVE_FORMAT_EXTENSIBLE :
340
341 /* with WAVE_FORMAT_EXTENSIBLE the psf->sf.format field is already set. We just have to set the major to rf64 */
342 psf->sf.format = (psf->sf.format & ~SF_FORMAT_TYPEMASK ) | SF_FORMAT_RF64 ;
343
344 if (psf->sf.format == (SF_FORMAT_WAVEX | SF_FORMAT_MS_ADPCM))
345 { *blockalign = wav_fmt->msadpcm.blockalign ;
346 *framesperblock = wav_fmt->msadpcm.samplesperblock ;
347 } ;
348 break ;
349
350 case WAVE_FORMAT_PCM :
351 psf->sf.format = SF_FORMAT_RF64 | u_bitwidth_to_subformat (psf->bytewidth * 8) ;
352 break ;
353
354 case WAVE_FORMAT_MULAW :
355 case IBM_FORMAT_MULAW :
356 psf->sf.format = (SF_FORMAT_RF64 | SF_FORMAT_ULAW) ;
357 break ;
358
359 case WAVE_FORMAT_ALAW :
360 case IBM_FORMAT_ALAW :
361 psf->sf.format = (SF_FORMAT_RF64 | SF_FORMAT_ALAW) ;
362 break ;
363
364 case WAVE_FORMAT_MS_ADPCM :
365 psf->sf.format = (SF_FORMAT_RF64 | SF_FORMAT_MS_ADPCM) ;
366 *blockalign = wav_fmt->msadpcm.blockalign ;
367 *framesperblock = wav_fmt->msadpcm.samplesperblock ;
368 break ;
369
370 case WAVE_FORMAT_IMA_ADPCM :
371 psf->sf.format = (SF_FORMAT_RF64 | SF_FORMAT_IMA_ADPCM) ;
372 *blockalign = wav_fmt->ima.blockalign ;
373 *framesperblock = wav_fmt->ima.samplesperblock ;
374 break ;
375
376 case WAVE_FORMAT_GSM610 :
377 psf->sf.format = (SF_FORMAT_RF64 | SF_FORMAT_GSM610) ;
378 break ;
379
380 case WAVE_FORMAT_IEEE_FLOAT :
381 psf->sf.format = SF_FORMAT_RF64 ;
382 psf->sf.format |= (psf->bytewidth == 8) ? SF_FORMAT_DOUBLE : SF_FORMAT_FLOAT ;
383 break ;
384
385 case WAVE_FORMAT_G721_ADPCM :
386 psf->sf.format = SF_FORMAT_RF64 | SF_FORMAT_G721_32 ;
387 break ;
388
389 default : return SFE_UNIMPLEMENTED ;
390 } ;
391
392 if (wpriv->fmt_is_broken)
393 wav_w64_analyze (psf) ;
394
395 /* Only set the format endian-ness if its non-standard big-endian. */
396 if (psf->endian == SF_ENDIAN_BIG)
397 psf->sf.format |= SF_ENDIAN_BIG ;
398
399 return 0 ;
400 } /* rf64_read_header */
401
402 /* known WAVEFORMATEXTENSIBLE GUIDS */
403 static const EXT_SUBFORMAT MSGUID_SUBTYPE_PCM =
404 { 0x00000001, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }
405 } ;
406
407 static const EXT_SUBFORMAT MSGUID_SUBTYPE_MS_ADPCM =
408 { 0x00000002, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }
409 } ;
410
411 static const EXT_SUBFORMAT MSGUID_SUBTYPE_IEEE_FLOAT =
412 { 0x00000003, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }
413 } ;
414
415 static const EXT_SUBFORMAT MSGUID_SUBTYPE_ALAW =
416 { 0x00000006, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }
417 } ;
418
419 static const EXT_SUBFORMAT MSGUID_SUBTYPE_MULAW =
420 { 0x00000007, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }
421 } ;
422
423 /*
424 ** the next two are from
425 ** http://dream.cs.bath.ac.uk/researchdev/wave-ex/bformat.html
426 */
427 static const EXT_SUBFORMAT MSGUID_SUBTYPE_AMBISONIC_B_FORMAT_PCM =
428 { 0x00000001, 0x0721, 0x11d3, { 0x86, 0x44, 0xC8, 0xC1, 0xCA, 0x00, 0x00, 0x00 }
429 } ;
430
431 static const EXT_SUBFORMAT MSGUID_SUBTYPE_AMBISONIC_B_FORMAT_IEEE_FLOAT =
432 { 0x00000003, 0x0721, 0x11d3, { 0x86, 0x44, 0xC8, 0xC1, 0xCA, 0x00, 0x00, 0x00 }
433 } ;
434
435
436 static int
437 rf64_write_fmt_chunk (SF_PRIVATE *psf)
438 { WAV_PRIVATE *wpriv ;
439 int subformat, fmt_size ;
440
441 if ((wpriv = psf->container_data) == NULL)
442 return SFE_INTERNAL ;
443
444 subformat = psf->sf.format & SF_FORMAT_SUBMASK ;
445
446 /* initial section (same for all, it appears) */
447 switch (subformat)
448 { case SF_FORMAT_PCM_U8 :
449 case SF_FORMAT_PCM_16 :
450 case SF_FORMAT_PCM_24 :
451 case SF_FORMAT_PCM_32 :
452 case SF_FORMAT_FLOAT :
453 case SF_FORMAT_DOUBLE :
454 case SF_FORMAT_ULAW :
455 case SF_FORMAT_ALAW :
456 fmt_size = 2 + 2 + 4 + 4 + 2 + 2 + 2 + 2 + 4 + 4 + 2 + 2 + 8 ;
457
458 /* fmt : format, channels, samplerate */
459 psf_binheader_writef (psf, "4224", fmt_size, WAVE_FORMAT_EXTENSIBLE, psf->sf.channels, psf->sf.samplerate) ;
460 /* fmt : bytespersec */
461 psf_binheader_writef (psf, "4", psf->sf.samplerate * psf->bytewidth * psf->sf.channels) ;
462 /* fmt : blockalign, bitwidth */
463 psf_binheader_writef (psf, "22", psf->bytewidth * psf->sf.channels, psf->bytewidth * 8) ;
464
465 /* cbSize 22 is sizeof (WAVEFORMATEXTENSIBLE) - sizeof (WAVEFORMATEX) */
466 psf_binheader_writef (psf, "2", 22) ;
467
468 /* wValidBitsPerSample, for our use same as bitwidth as we use it fully */
469 psf_binheader_writef (psf, "2", psf->bytewidth * 8) ;
470
471 /* For an Ambisonic file set the channel mask to zero.
472 ** Otherwise use a default based on the channel count.
473 */
474 if (wpriv->wavex_ambisonic != SF_AMBISONIC_NONE)
475 psf_binheader_writef (psf, "4", 0) ;
476 else if (wpriv->wavex_channelmask != 0)
477 psf_binheader_writef (psf, "4", wpriv->wavex_channelmask) ;
478 else
479 { /*
480 ** Ok some liberty is taken here to use the most commonly used channel masks
481 ** instead of "no mapping". If you really want to use "no mapping" for 8 channels and less
482 ** please don't use wavex. (otherwise we'll have to create a new SF_COMMAND)
483 */
484 switch (psf->sf.channels)
485 { case 1 : /* center channel mono */
486 psf_binheader_writef (psf, "4", 0x4) ;
487 break ;
488
489 case 2 : /* front left and right */
490 psf_binheader_writef (psf, "4", 0x1 | 0x2) ;
491 break ;
492
493 case 4 : /* Quad */
494 psf_binheader_writef (psf, "4", 0x1 | 0x2 | 0x10 | 0x20) ;
495 break ;
496
497 case 6 : /* 5.1 */
498 psf_binheader_writef (psf, "4", 0x1 | 0x2 | 0x4 | 0x8 | 0x10 | 0x20) ;
499 break ;
500
501 case 8 : /* 7.1 */
502 psf_binheader_writef (psf, "4", 0x1 | 0x2 | 0x4 | 0x8 | 0x10 | 0x20 | 0x40 | 0x80) ;
503 break ;
504
505 default : /* 0 when in doubt , use direct out, ie NO mapping*/
506 psf_binheader_writef (psf, "4", 0x0) ;
507 break ;
508 } ;
509 } ;
510 break ;
511
512 case SF_FORMAT_MS_ADPCM : /* Todo, GUID exists might have different header as per wav_write_header */
513 default :
514 return SFE_UNIMPLEMENTED ;
515 } ;
516
517 /* GUID section, different for each */
518
519 switch (subformat)
520 { case SF_FORMAT_PCM_U8 :
521 case SF_FORMAT_PCM_16 :
522 case SF_FORMAT_PCM_24 :
523 case SF_FORMAT_PCM_32 :
524 wavex_write_guid (psf, wpriv->wavex_ambisonic == SF_AMBISONIC_NONE ?
525 &MSGUID_SUBTYPE_PCM : &MSGUID_SUBTYPE_AMBISONIC_B_FORMAT_PCM) ;
526 break ;
527
528 case SF_FORMAT_FLOAT :
529 case SF_FORMAT_DOUBLE :
530 wavex_write_guid (psf, wpriv->wavex_ambisonic == SF_AMBISONIC_NONE ?
531 &MSGUID_SUBTYPE_IEEE_FLOAT : &MSGUID_SUBTYPE_AMBISONIC_B_FORMAT_IEEE_FLOAT) ;
532 break ;
533
534 case SF_FORMAT_ULAW :
535 wavex_write_guid (psf, &MSGUID_SUBTYPE_MULAW) ;
536 break ;
537
538 case SF_FORMAT_ALAW :
539 wavex_write_guid (psf, &MSGUID_SUBTYPE_ALAW) ;
540 break ;
541
542 default : return SFE_UNIMPLEMENTED ;
543 } ;
544
545 return 0 ;
546 } /* rf64_write_fmt_chunk */
547
548
549 static int
550 rf64_write_header (SF_PRIVATE *psf, int calc_length)
551 { sf_count_t current ;
552 int error = 0, has_data = SF_FALSE ;
553
554 current = psf_ftell (psf) ;
555
556 if (psf->dataoffset > 0 && current > psf->dataoffset)
557 has_data = SF_TRUE ;
558
559 if (calc_length)
560 { psf->filelength = psf_get_filelen (psf) ;
561 psf->datalength = psf->filelength - psf->dataoffset ;
562
563 if (psf->dataend)
564 psf->datalength -= psf->filelength - psf->dataend ;
565
566 if (psf->bytewidth > 0)
567 psf->sf.frames = psf->datalength / (psf->bytewidth * psf->sf.channels) ;
568 } ;
569
570 /* Reset the current header length to zero. */
571 psf->header [0] = 0 ;
572 psf->headindex = 0 ;
573 psf_fseek (psf, 0, SEEK_SET) ;
574
575 psf_binheader_writef (psf, "em4m", RF64_MARKER, 0xffffffff, WAVE_MARKER) ;
576
577 /* Currently no table. */
578 psf_binheader_writef (psf, "m48884", ds64_MARKER, 28, psf->filelength - 8, psf->datalength, psf->sf.frames, 0) ;
579
580 /* WAVE and 'fmt ' markers. */
581 psf_binheader_writef (psf, "m", fmt_MARKER) ;
582
583 /* Write the 'fmt ' chunk. */
584 switch (psf->sf.format & SF_FORMAT_TYPEMASK)
585 { case SF_FORMAT_WAV :
586 psf_log_printf (psf, "ooops SF_FORMAT_WAV\n") ;
587 return SFE_UNIMPLEMENTED ;
588 break ;
589
590 case SF_FORMAT_WAVEX :
591 case SF_FORMAT_RF64 :
592 if ((error = rf64_write_fmt_chunk (psf)) != 0)
593 return error ;
594 break ;
595
596 default :
597 return SFE_UNIMPLEMENTED ;
598 } ;
599
600 if (psf->broadcast_16k != NULL)
601 wav_write_bext_chunk (psf) ;
602
603 #if 0
604 /* The LIST/INFO chunk. */
605 if (psf->str_flags & SF_STR_LOCATE_START)
606 wav_write_strings (psf, SF_STR_LOCATE_START) ;
607
608 if (psf->peak_info != NULL && psf->peak_info->peak_loc == SF_PEAK_START)
609 { psf_binheader_writef (psf, "m4", PEAK_MARKER, WAV_PEAK_CHUNK_SIZE (psf->sf.channels)) ;
610 psf_binheader_writef (psf, "44", 1, time (NULL)) ;
611 for (k = 0 ; k < psf->sf.channels ; k++)
612 psf_binheader_writef (psf, "ft8", (float) psf->peak_info->peaks [k].value, psf->peak_info->peaks [k].position) ;
613 } ;
614
615 // if (psf->broadcast_info != NULL)
616 // wav_write_bext_chunk (psf) ;
617
618 if (psf->instrument != NULL)
619 { int tmp ;
620 double dtune = (double) (0x40000000) / 25.0 ;
621
622 psf_binheader_writef (psf, "m4", smpl_MARKER, 9 * 4 + psf->instrument->loop_count * 6 * 4) ;
623 psf_binheader_writef (psf, "44", 0, 0) ; /* Manufacturer zero is everyone */
624 tmp = (int) (1.0e9 / psf->sf.samplerate) ; /* Sample period in nano seconds */
625 psf_binheader_writef (psf, "44", tmp, psf->instrument->basenote) ;
626 tmp = (unsigned int) (psf->instrument->detune * dtune + 0.5) ;
627 psf_binheader_writef (psf, "4", tmp) ;
628 psf_binheader_writef (psf, "44", 0, 0) ; /* SMTPE format */
629 psf_binheader_writef (psf, "44", psf->instrument->loop_count, 0) ;
630
631 for (tmp = 0 ; tmp < psf->instrument->loop_count ; tmp++)
632 { int type ;
633
634 type = psf->instrument->loops [tmp].mode ;
635 type = (type == SF_LOOP_FORWARD ? 0 : type==SF_LOOP_BACKWARD ? 2 : type == SF_LOOP_ALTERNATING ? 1 : 32) ;
636
637 psf_binheader_writef (psf, "44", tmp, type) ;
638 psf_binheader_writef (psf, "44", psf->instrument->loops [tmp].start, psf->instrument->loops [tmp].end) ;
639 psf_binheader_writef (psf, "44", 0, psf->instrument->loops [tmp].count) ;
640 } ;
641 } ;
642
643 if (psf->headindex + 8 < psf->dataoffset)
644 { /* Add PAD data if necessary. */
645 k = psf->dataoffset - 16 - psf->headindex ;
646 psf_binheader_writef (psf, "m4z", PAD_MARKER, k, make_size_t (k)) ;
647 } ;
648
649 #endif
650
651 psf_binheader_writef (psf, "m4", data_MARKER, 0xffffffff) ;
652
653 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
654 if (psf->error)
655 return psf->error ;
656
657 if (has_data && psf->dataoffset != psf->headindex)
658 { printf ("Oooops : has_data && psf->dataoffset != psf->headindex\n") ;
659 return psf->error = SFE_INTERNAL ;
660 } ;
661
662 psf->dataoffset = psf->headindex ;
663
664 if (NOT (has_data))
665 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
666 else if (current > 0)
667 psf_fseek (psf, current, SEEK_SET) ;
668
669 return psf->error ;
670 } /* rf64_write_header */
671
672 static int
673 rf64_close (SF_PRIVATE *psf)
674 {
675 if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
676 { // rf64_write_tailer (psf) ;
677
678 psf->write_header (psf, SF_TRUE) ;
679 } ;
680
681 return 0 ;
682 } /* rf64_close */
683
684 static int
685 rf64_command (SF_PRIVATE *psf, int command, void * UNUSED (data), int datasize)
686 { WAV_PRIVATE *wpriv ;
687
688 if ((wpriv = psf->container_data) == NULL)
689 return SFE_INTERNAL ;
690
691 switch (command)
692 { case SFC_WAVEX_SET_AMBISONIC :
693 if ((SF_CONTAINER (psf->sf.format)) == SF_FORMAT_WAVEX)
694 { if (datasize == SF_AMBISONIC_NONE)
695 wpriv->wavex_ambisonic = SF_AMBISONIC_NONE ;
696 else if (datasize == SF_AMBISONIC_B_FORMAT)
697 wpriv->wavex_ambisonic = SF_AMBISONIC_B_FORMAT ;
698 else
699 return 0 ;
700 } ;
701 return wpriv->wavex_ambisonic ;
702
703 case SFC_WAVEX_GET_AMBISONIC :
704 return wpriv->wavex_ambisonic ;
705
706 case SFC_SET_CHANNEL_MAP_INFO :
707 wpriv->wavex_channelmask = wavex_gen_channel_mask (psf->channel_map, psf->sf.channels) ;
708 return (wpriv->wavex_channelmask != 0) ;
709
710 default :
711 break ;
712 } ;
713
714 return 0 ;
715 } /* rf64_command */
716