2 ** Copyright (C) 2008-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
3 ** Copyright (C) 2009 Uli Franke <cls@nebadje.org>
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.
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.
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.
21 ** This format documented at:
22 ** http://www.sr.se/utveckling/tu/bwf/prog/RF_64v1_4.pdf
24 ** But this may be a better reference:
25 ** http://www.ebu.ch/CMSimages/en/tec_doc_t3306-2007_tcm6-42570.pdf
40 /*------------------------------------------------------------------------------
41 ** Macros to handle big/little endian issues.
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')
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')
55 /*------------------------------------------------------------------------------
59 /*------------------------------------------------------------------------------
60 ** Private static functions.
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
) ;
68 /*------------------------------------------------------------------------------
73 rf64_open (SF_PRIVATE
*psf
)
74 { WAV_PRIVATE
*wpriv
;
75 int subformat
, error
= 0 ;
76 int blockalign
, framesperblock
;
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
;
83 /* All RF64 files are little endian. */
84 psf
->endian
= SF_ENDIAN_LITTLE
;
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)
92 if ((psf
->sf
.format
& SF_FORMAT_TYPEMASK
) != SF_FORMAT_RF64
)
93 return SFE_BAD_OPEN_FORMAT
;
95 subformat
= psf
->sf
.format
& SF_FORMAT_SUBMASK
;
97 if (psf
->file
.mode
== SFM_WRITE
|| psf
->file
.mode
== SFM_RDWR
)
99 return SFE_NO_PIPE_WRITE
;
101 psf
->blockwidth
= psf
->bytewidth
* psf
->sf
.channels
;
103 if ((error
= rf64_write_header (psf
, SF_FALSE
)))
106 psf
->write_header
= rf64_write_header
;
109 psf
->container_close
= rf64_close
;
110 psf
->command
= rf64_command
;
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
) ;
120 case SF_FORMAT_ULAW
:
121 error
= ulaw_init (psf
) ;
124 case SF_FORMAT_ALAW
:
125 error
= alaw_init (psf
) ;
128 /* Lite remove start */
129 case SF_FORMAT_FLOAT
:
130 error
= float32_init (psf
) ;
133 case SF_FORMAT_DOUBLE
:
134 error
= double64_init (psf
) ;
137 /* Lite remove end */
139 default : return SFE_UNIMPLEMENTED
;
145 /*------------------------------------------------------------------------------
154 #define HAVE_CHUNK(CHUNK) ((parsestage & CHUNK) != 0)
157 rf64_read_header (SF_PRIVATE
*psf
, int *blockalign
, int *framesperblock
)
158 { WAV_PRIVATE
*wpriv
;
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 ;
164 if ((wpriv
= psf
->container_data
) == NULL
)
165 return SFE_INTERNAL
;
166 wav_fmt
= &wpriv
->wav_fmt
;
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
;
173 if (marks
[0] == FFFF_MARKER
)
174 psf_log_printf (psf
, "%M\n %M\n", RF64_MARKER
, WAVE_MARKER
) ;
176 psf_log_printf (psf
, "%M : 0x%x (should be 0xFFFFFFFF)\n %M\n", RF64_MARKER
, WAVE_MARKER
) ;
179 { psf_binheader_readf (psf
, "em4", &marker
, &size32
) ;
183 { unsigned int table_len
, bytesread
;
185 /* Read ds64 sizes (3 8-byte words). */
186 bytesread
= psf_binheader_readf (psf
, "888", &riff_size
, &ds64_datalength
, &frame_count
) ;
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
) ;
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) ;
203 { psf_log_printf (psf
, "%M : %u\n", marker
, size32
) ;
204 psf_binheader_readf (psf
, "j", size32
- bytesread
- 4) ;
208 if (psf
->filelength
!= riff_size
+ 8)
209 psf_log_printf (psf
, " Riff size : %D (should be %D)\n", riff_size
, psf
->filelength
- 8) ;
211 psf_log_printf (psf
, " Riff size : %D\n", riff_size
) ;
213 psf_log_printf (psf
, " Data size : %D\n", ds64_datalength
) ;
215 psf_log_printf (psf
, " Frames : %D\n", frame_count
) ;
216 psf_log_printf (psf
, " Table length : %u\n", table_len
) ;
219 parsestage
|= HAVE_ds64
;
223 psf_log_printf (psf
, "%M : %u\n", marker
, size32
) ;
224 if ((error
= wav_w64_read_fmt_chunk (psf
, size32
)) != 0)
226 format
= wav_fmt
->format
;
227 parsestage
|= HAVE_fmt
;
231 if ((error
= wav_read_bext_chunk (psf
, size32
)) != 0)
233 parsestage
|= HAVE_bext
;
237 /* see wav for more sophisticated parsing -> implement state machine with parsestage */
239 if (HAVE_CHUNK (HAVE_ds64
))
240 { if (size32
== 0xffffffff)
241 psf_log_printf (psf
, "%M : 0x%x\n", marker
, size32
) ;
243 psf_log_printf (psf
, "%M : 0x%x (should be 0xffffffff\n", marker
, size32
) ;
244 psf
->datalength
= ds64_datalength
;
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") ;
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
;
257 psf
->dataoffset
= psf_ftell (psf
) ;
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
;
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
;
269 if (NOT (psf
->sf
.seekable
) || psf
->dataoffset
< 0)
272 /* Seek past data and continue reading header. */
273 psf_fseek (psf
, psf
->datalength
, SEEK_CUR
) ;
275 if (psf_ftell (psf
) != psf
->datalength
+ psf
->dataoffset
)
276 psf_log_printf (psf
, " *** psf_fseek past end error ***\n") ;
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
) ;
287 psf_binheader_readf (psf
, "j", size32
) ;
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) ;
295 psf_log_printf (psf
, "*** Unknown chunk marker (0x%X) at position 0x%X. Exiting parser.\n", marker
, psf_ftell (psf
) - 4) ;
298 } ; /* switch (marker) */
300 if (psf_ftell (psf
) >= psf
->filelength
- SIGNED_SIZEOF (marker
))
301 { psf_log_printf (psf
, "End\n") ;
306 if (psf
->dataoffset
<= 0)
307 return SFE_WAV_NO_DATA
;
309 /* WAVs can be little or big endian */
310 psf
->endian
= psf
->rwf_endian
;
312 psf_fseek (psf
, psf
->dataoffset
, SEEK_SET
) ;
314 if (psf
->is_pipe
== 0)
316 ** Check for 'wvpk' at the start of the DATA section. Not able to
319 psf_binheader_readf (psf
, "4", &marker
) ;
320 if (marker
== wvpk_MARKER
|| marker
== OggS_MARKER
)
321 return SFE_WAV_WVPK_DATA
;
324 /* Seek to start of DATA section. */
325 psf_fseek (psf
, psf
->dataoffset
, SEEK_SET
) ;
328 { if (psf
->filelength
- psf
->dataoffset
< psf
->datalength
)
329 psf
->sf
.frames
= (psf
->filelength
- psf
->dataoffset
) / psf
->blockwidth
;
331 psf
->sf
.frames
= psf
->datalength
/ psf
->blockwidth
;
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
) ;
339 case WAVE_FORMAT_EXTENSIBLE
:
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
;
344 if (psf
->sf
.format
== (SF_FORMAT_WAVEX
| SF_FORMAT_MS_ADPCM
))
345 { *blockalign
= wav_fmt
->msadpcm
.blockalign
;
346 *framesperblock
= wav_fmt
->msadpcm
.samplesperblock
;
350 case WAVE_FORMAT_PCM
:
351 psf
->sf
.format
= SF_FORMAT_RF64
| u_bitwidth_to_subformat (psf
->bytewidth
* 8) ;
354 case WAVE_FORMAT_MULAW
:
355 case IBM_FORMAT_MULAW
:
356 psf
->sf
.format
= (SF_FORMAT_RF64
| SF_FORMAT_ULAW
) ;
359 case WAVE_FORMAT_ALAW
:
360 case IBM_FORMAT_ALAW
:
361 psf
->sf
.format
= (SF_FORMAT_RF64
| SF_FORMAT_ALAW
) ;
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
;
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
;
376 case WAVE_FORMAT_GSM610
:
377 psf
->sf
.format
= (SF_FORMAT_RF64
| SF_FORMAT_GSM610
) ;
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
;
385 case WAVE_FORMAT_G721_ADPCM
:
386 psf
->sf
.format
= SF_FORMAT_RF64
| SF_FORMAT_G721_32
;
389 default : return SFE_UNIMPLEMENTED
;
392 if (wpriv
->fmt_is_broken
)
393 wav_w64_analyze (psf
) ;
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
;
400 } /* rf64_read_header */
402 /* known WAVEFORMATEXTENSIBLE GUIDS */
403 static const EXT_SUBFORMAT MSGUID_SUBTYPE_PCM
=
404 { 0x00000001, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }
407 static const EXT_SUBFORMAT MSGUID_SUBTYPE_MS_ADPCM
=
408 { 0x00000002, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }
411 static const EXT_SUBFORMAT MSGUID_SUBTYPE_IEEE_FLOAT
=
412 { 0x00000003, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }
415 static const EXT_SUBFORMAT MSGUID_SUBTYPE_ALAW
=
416 { 0x00000006, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }
419 static const EXT_SUBFORMAT MSGUID_SUBTYPE_MULAW
=
420 { 0x00000007, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }
424 ** the next two are from
425 ** http://dream.cs.bath.ac.uk/researchdev/wave-ex/bformat.html
427 static const EXT_SUBFORMAT MSGUID_SUBTYPE_AMBISONIC_B_FORMAT_PCM
=
428 { 0x00000001, 0x0721, 0x11d3, { 0x86, 0x44, 0xC8, 0xC1, 0xCA, 0x00, 0x00, 0x00 }
431 static const EXT_SUBFORMAT MSGUID_SUBTYPE_AMBISONIC_B_FORMAT_IEEE_FLOAT
=
432 { 0x00000003, 0x0721, 0x11d3, { 0x86, 0x44, 0xC8, 0xC1, 0xCA, 0x00, 0x00, 0x00 }
437 rf64_write_fmt_chunk (SF_PRIVATE
*psf
)
438 { WAV_PRIVATE
*wpriv
;
439 int subformat
, fmt_size
;
441 if ((wpriv
= psf
->container_data
) == NULL
)
442 return SFE_INTERNAL
;
444 subformat
= psf
->sf
.format
& SF_FORMAT_SUBMASK
;
446 /* initial section (same for all, it appears) */
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 ;
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) ;
465 /* cbSize 22 is sizeof (WAVEFORMATEXTENSIBLE) - sizeof (WAVEFORMATEX) */
466 psf_binheader_writef (psf
, "2", 22) ;
468 /* wValidBitsPerSample, for our use same as bitwidth as we use it fully */
469 psf_binheader_writef (psf
, "2", psf
->bytewidth
* 8) ;
471 /* For an Ambisonic file set the channel mask to zero.
472 ** Otherwise use a default based on the channel count.
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
) ;
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)
484 switch (psf
->sf
.channels
)
485 { case 1 : /* center channel mono */
486 psf_binheader_writef (psf
, "4", 0x4) ;
489 case 2 : /* front left and right */
490 psf_binheader_writef (psf
, "4", 0x1 | 0x2) ;
494 psf_binheader_writef (psf
, "4", 0x1 | 0x2 | 0x10 | 0x20) ;
498 psf_binheader_writef (psf
, "4", 0x1 | 0x2 | 0x4 | 0x8 | 0x10 | 0x20) ;
502 psf_binheader_writef (psf
, "4", 0x1 | 0x2 | 0x4 | 0x8 | 0x10 | 0x20 | 0x40 | 0x80) ;
505 default : /* 0 when in doubt , use direct out, ie NO mapping*/
506 psf_binheader_writef (psf
, "4", 0x0) ;
512 case SF_FORMAT_MS_ADPCM
: /* Todo, GUID exists might have different header as per wav_write_header */
514 return SFE_UNIMPLEMENTED
;
517 /* GUID section, different for each */
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
) ;
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
) ;
534 case SF_FORMAT_ULAW
:
535 wavex_write_guid (psf
, &MSGUID_SUBTYPE_MULAW
) ;
538 case SF_FORMAT_ALAW
:
539 wavex_write_guid (psf
, &MSGUID_SUBTYPE_ALAW
) ;
542 default : return SFE_UNIMPLEMENTED
;
546 } /* rf64_write_fmt_chunk */
550 rf64_write_header (SF_PRIVATE
*psf
, int calc_length
)
551 { sf_count_t current
;
552 int error
= 0, has_data
= SF_FALSE
;
554 current
= psf_ftell (psf
) ;
556 if (psf
->dataoffset
> 0 && current
> psf
->dataoffset
)
560 { psf
->filelength
= psf_get_filelen (psf
) ;
561 psf
->datalength
= psf
->filelength
- psf
->dataoffset
;
564 psf
->datalength
-= psf
->filelength
- psf
->dataend
;
566 if (psf
->bytewidth
> 0)
567 psf
->sf
.frames
= psf
->datalength
/ (psf
->bytewidth
* psf
->sf
.channels
) ;
570 /* Reset the current header length to zero. */
571 psf
->header
[0] = 0 ;
573 psf_fseek (psf
, 0, SEEK_SET
) ;
575 psf_binheader_writef (psf
, "em4m", RF64_MARKER
, 0xffffffff, WAVE_MARKER
) ;
577 /* Currently no table. */
578 psf_binheader_writef (psf
, "m48884", ds64_MARKER
, 28, psf
->filelength
- 8, psf
->datalength
, psf
->sf
.frames
, 0) ;
580 /* WAVE and 'fmt ' markers. */
581 psf_binheader_writef (psf
, "m", fmt_MARKER
) ;
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
;
590 case SF_FORMAT_WAVEX
:
591 case SF_FORMAT_RF64
:
592 if ((error
= rf64_write_fmt_chunk (psf
)) != 0)
597 return SFE_UNIMPLEMENTED
;
600 if (psf
->broadcast_16k
!= NULL
)
601 wav_write_bext_chunk (psf
) ;
604 /* The LIST/INFO chunk. */
605 if (psf
->str_flags
& SF_STR_LOCATE_START
)
606 wav_write_strings (psf
, SF_STR_LOCATE_START
) ;
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
) ;
615 // if (psf->broadcast_info != NULL)
616 // wav_write_bext_chunk (psf) ;
618 if (psf
->instrument
!= NULL
)
620 double dtune
= (double) (0x40000000) / 25.0 ;
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) ;
631 for (tmp
= 0 ; tmp
< psf
->instrument
->loop_count
; tmp
++)
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) ;
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
) ;
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
)) ;
651 psf_binheader_writef (psf
, "m4", data_MARKER
, 0xffffffff) ;
653 psf_fwrite (psf
->header
, psf
->headindex
, 1, psf
) ;
657 if (has_data
&& psf
->dataoffset
!= psf
->headindex
)
658 { printf ("Oooops : has_data && psf->dataoffset != psf->headindex\n") ;
659 return psf
->error
= SFE_INTERNAL
;
662 psf
->dataoffset
= psf
->headindex
;
665 psf_fseek (psf
, psf
->dataoffset
, SEEK_SET
) ;
666 else if (current
> 0)
667 psf_fseek (psf
, current
, SEEK_SET
) ;
670 } /* rf64_write_header */
673 rf64_close (SF_PRIVATE
*psf
)
675 if (psf
->file
.mode
== SFM_WRITE
|| psf
->file
.mode
== SFM_RDWR
)
676 { // rf64_write_tailer (psf) ;
678 psf
->write_header (psf
, SF_TRUE
) ;
685 rf64_command (SF_PRIVATE
*psf
, int command
, void * UNUSED (data
), int datasize
)
686 { WAV_PRIVATE
*wpriv
;
688 if ((wpriv
= psf
->container_data
) == NULL
)
689 return SFE_INTERNAL
;
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
;
701 return wpriv
->wavex_ambisonic
;
703 case SFC_WAVEX_GET_AMBISONIC
:
704 return wpriv
->wavex_ambisonic
;
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) ;