2 ** Copyright (C) 1999-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
3 ** Copyright (C) 2005 David Viens <davidv@plogue.com>
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.
34 /*------------------------------------------------------------------------------
35 * Macros to handle big/little endian issues.
38 #define FORM_MARKER (MAKE_MARKER ('F', 'O', 'R', 'M'))
39 #define AIFF_MARKER (MAKE_MARKER ('A', 'I', 'F', 'F'))
40 #define AIFC_MARKER (MAKE_MARKER ('A', 'I', 'F', 'C'))
41 #define COMM_MARKER (MAKE_MARKER ('C', 'O', 'M', 'M'))
42 #define SSND_MARKER (MAKE_MARKER ('S', 'S', 'N', 'D'))
43 #define MARK_MARKER (MAKE_MARKER ('M', 'A', 'R', 'K'))
44 #define INST_MARKER (MAKE_MARKER ('I', 'N', 'S', 'T'))
45 #define APPL_MARKER (MAKE_MARKER ('A', 'P', 'P', 'L'))
46 #define CHAN_MARKER (MAKE_MARKER ('C', 'H', 'A', 'N'))
48 #define c_MARKER (MAKE_MARKER ('(', 'c', ')', ' '))
49 #define NAME_MARKER (MAKE_MARKER ('N', 'A', 'M', 'E'))
50 #define AUTH_MARKER (MAKE_MARKER ('A', 'U', 'T', 'H'))
51 #define ANNO_MARKER (MAKE_MARKER ('A', 'N', 'N', 'O'))
52 #define COMT_MARKER (MAKE_MARKER ('C', 'O', 'M', 'T'))
53 #define FVER_MARKER (MAKE_MARKER ('F', 'V', 'E', 'R'))
54 #define SFX_MARKER (MAKE_MARKER ('S', 'F', 'X', '!'))
56 #define PEAK_MARKER (MAKE_MARKER ('P', 'E', 'A', 'K'))
57 #define basc_MARKER (MAKE_MARKER ('b', 'a', 's', 'c'))
59 /* Supported AIFC encodings.*/
60 #define NONE_MARKER (MAKE_MARKER ('N', 'O', 'N', 'E'))
61 #define sowt_MARKER (MAKE_MARKER ('s', 'o', 'w', 't'))
62 #define twos_MARKER (MAKE_MARKER ('t', 'w', 'o', 's'))
63 #define raw_MARKER (MAKE_MARKER ('r', 'a', 'w', ' '))
64 #define in24_MARKER (MAKE_MARKER ('i', 'n', '2', '4'))
65 #define ni24_MARKER (MAKE_MARKER ('4', '2', 'n', '1'))
66 #define in32_MARKER (MAKE_MARKER ('i', 'n', '3', '2'))
67 #define ni32_MARKER (MAKE_MARKER ('2', '3', 'n', 'i'))
69 #define fl32_MARKER (MAKE_MARKER ('f', 'l', '3', '2'))
70 #define FL32_MARKER (MAKE_MARKER ('F', 'L', '3', '2'))
71 #define fl64_MARKER (MAKE_MARKER ('f', 'l', '6', '4'))
72 #define FL64_MARKER (MAKE_MARKER ('F', 'L', '6', '4'))
74 #define ulaw_MARKER (MAKE_MARKER ('u', 'l', 'a', 'w'))
75 #define ULAW_MARKER (MAKE_MARKER ('U', 'L', 'A', 'W'))
76 #define alaw_MARKER (MAKE_MARKER ('a', 'l', 'a', 'w'))
77 #define ALAW_MARKER (MAKE_MARKER ('A', 'L', 'A', 'W'))
79 #define DWVW_MARKER (MAKE_MARKER ('D', 'W', 'V', 'W'))
80 #define GSM_MARKER (MAKE_MARKER ('G', 'S', 'M', ' '))
81 #define ima4_MARKER (MAKE_MARKER ('i', 'm', 'a', '4'))
84 ** This value is officially assigned to Mega Nerd Pty Ltd by Apple
85 ** Corportation as the Application marker for libsndfile.
87 ** See : http://developer.apple.com/faq/datatype.html
89 #define m3ga_MARKER (MAKE_MARKER ('m', '3', 'g', 'a'))
91 /* Unsupported AIFC encodings.*/
93 #define MAC3_MARKER (MAKE_MARKER ('M', 'A', 'C', '3'))
94 #define MAC6_MARKER (MAKE_MARKER ('M', 'A', 'C', '6'))
95 #define ADP4_MARKER (MAKE_MARKER ('A', 'D', 'P', '4'))
97 /* Predfined chunk sizes. */
98 #define SIZEOF_AIFF_COMM 18
99 #define SIZEOF_AIFC_COMM_MIN 22
100 #define SIZEOF_AIFC_COMM 24
101 #define SIZEOF_SSND_CHUNK 8
102 #define SIZEOF_INST_CHUNK 20
104 /* Is it constant? */
106 /* AIFC/IMA4 defines. */
107 #define AIFC_IMA4_BLOCK_LEN 34
108 #define AIFC_IMA4_SAMPLES_PER_BLOCK 64
110 #define AIFF_PEAK_CHUNK_SIZE(ch) (2 * sizeof (int) + ch * (sizeof (float) + sizeof (int)))
112 /*------------------------------------------------------------------------------
113 * Typedefs for file chunks.
126 { unsigned int size
;
128 unsigned int numSampleFrames
;
130 unsigned char sampleRate
[10] ;
131 unsigned int encoding
;
132 char zero_bytes
[2] ;
136 { unsigned int offset
;
137 unsigned int blocksize
;
142 unsigned short beginLoop
;
143 unsigned short endLoop
;
147 { char baseNote
; /* all notes are MIDI note numbers */
148 char detune
; /* cents off, only -50 to +50 are significant */
151 char lowVelocity
; /* 1 to 127 */
152 char highVelocity
; /* 1 to 127 */
153 short gain
; /* in dB, 0 is normal */
154 INST_LOOP sustain_loop
;
155 INST_LOOP release_loop
;
160 { basc_SCALE_MINOR
= 1,
167 { basc_TYPE_LOOP
= 0,
173 { unsigned int version
;
174 unsigned int numBeats
;
175 unsigned short rootNote
;
176 unsigned short scaleType
;
177 unsigned short sigNumerator
;
178 unsigned short sigDenominator
;
179 unsigned short loopType
;
183 { unsigned short markerID
;
184 unsigned int position
;
188 { PRIV_CHUNK4 chunk4
;
190 sf_count_t comm_offset
;
191 sf_count_t ssnd_offset
;
195 MARK_ID_POS
*markstr
;
198 /*------------------------------------------------------------------------------
199 * Private static functions.
202 static int aiff_close (SF_PRIVATE
*psf
) ;
204 static int tenbytefloat2int (unsigned char *bytes
) ;
205 static void uint2tenbytefloat (unsigned int num
, unsigned char *bytes
) ;
207 static int aiff_read_comm_chunk (SF_PRIVATE
*psf
, COMM_CHUNK
*comm_fmt
) ;
209 static int aiff_read_header (SF_PRIVATE
*psf
, COMM_CHUNK
*comm_fmt
) ;
211 static int aiff_write_header (SF_PRIVATE
*psf
, int calc_length
) ;
212 static int aiff_write_tailer (SF_PRIVATE
*psf
) ;
213 static void aiff_write_strings (SF_PRIVATE
*psf
, int location
) ;
215 static int aiff_command (SF_PRIVATE
*psf
, int command
, void *data
, int datasize
) ;
217 static const char *get_loop_mode_str (short mode
) ;
219 static short get_loop_mode (short mode
) ;
221 static int aiff_read_basc_chunk (SF_PRIVATE
* psf
, int) ;
223 static int aiff_read_chanmap (SF_PRIVATE
* psf
, unsigned dword
) ;
225 static unsigned int marker_to_position (const MARK_ID_POS
*m
, unsigned short n
, int marksize
) ;
227 /*------------------------------------------------------------------------------
232 aiff_open (SF_PRIVATE
*psf
)
233 { COMM_CHUNK comm_fmt
;
234 int error
, subformat
;
236 memset (&comm_fmt
, 0, sizeof (comm_fmt
)) ;
238 subformat
= SF_CODEC (psf
->sf
.format
) ;
240 if ((psf
->container_data
= calloc (1, sizeof (AIFF_PRIVATE
))) == NULL
)
241 return SFE_MALLOC_FAILED
;
243 if (psf
->file
.mode
== SFM_READ
|| (psf
->file
.mode
== SFM_RDWR
&& psf
->filelength
> 0))
244 { if ((error
= aiff_read_header (psf
, &comm_fmt
)))
246 psf_fseek (psf
, psf
->dataoffset
, SEEK_SET
) ;
249 if (psf
->file
.mode
== SFM_WRITE
|| psf
->file
.mode
== SFM_RDWR
)
251 return SFE_NO_PIPE_WRITE
;
253 if ((SF_CONTAINER (psf
->sf
.format
)) != SF_FORMAT_AIFF
)
254 return SFE_BAD_OPEN_FORMAT
;
256 if (psf
->file
.mode
== SFM_WRITE
&& (subformat
== SF_FORMAT_FLOAT
|| subformat
== SF_FORMAT_DOUBLE
))
257 { if ((psf
->peak_info
= peak_info_calloc (psf
->sf
.channels
)) == NULL
)
258 return SFE_MALLOC_FAILED
;
259 psf
->peak_info
->peak_loc
= SF_PEAK_START
;
262 if (psf
->file
.mode
!= SFM_RDWR
|| psf
->filelength
< 40)
263 { psf
->filelength
= 0 ;
264 psf
->datalength
= 0 ;
265 psf
->dataoffset
= 0 ;
269 psf
->str_flags
= SF_STR_ALLOW_START
| SF_STR_ALLOW_END
;
271 if ((error
= aiff_write_header (psf
, SF_FALSE
)))
274 psf
->write_header
= aiff_write_header
;
277 psf
->container_close
= aiff_close
;
278 psf
->command
= aiff_command
;
280 switch (SF_CODEC (psf
->sf
.format
))
281 { case SF_FORMAT_PCM_U8
:
282 error
= pcm_init (psf
) ;
285 case SF_FORMAT_PCM_S8
:
286 error
= pcm_init (psf
) ;
289 case SF_FORMAT_PCM_16
:
290 case SF_FORMAT_PCM_24
:
291 case SF_FORMAT_PCM_32
:
292 error
= pcm_init (psf
) ;
295 case SF_FORMAT_ULAW
:
296 error
= ulaw_init (psf
) ;
299 case SF_FORMAT_ALAW
:
300 error
= alaw_init (psf
) ;
303 /* Lite remove start */
304 case SF_FORMAT_FLOAT
:
305 error
= float32_init (psf
) ;
308 case SF_FORMAT_DOUBLE
:
309 error
= double64_init (psf
) ;
312 case SF_FORMAT_DWVW_12
:
313 error
= dwvw_init (psf
, 12) ;
316 case SF_FORMAT_DWVW_16
:
317 error
= dwvw_init (psf
, 16) ;
320 case SF_FORMAT_DWVW_24
:
321 error
= dwvw_init (psf
, 24) ;
324 case SF_FORMAT_DWVW_N
:
325 if (psf
->file
.mode
!= SFM_READ
)
326 { error
= SFE_DWVW_BAD_BITWIDTH
;
329 if (comm_fmt
.sampleSize
>= 8 && comm_fmt
.sampleSize
< 24)
330 { error
= dwvw_init (psf
, comm_fmt
.sampleSize
) ;
331 psf
->sf
.frames
= comm_fmt
.numSampleFrames
;
334 psf_log_printf (psf
, "AIFC/DWVW : Bad bitwidth %d\n", comm_fmt
.sampleSize
) ;
335 error
= SFE_DWVW_BAD_BITWIDTH
;
338 case SF_FORMAT_IMA_ADPCM
:
340 ** IMA ADPCM encoded AIFF files always have a block length
341 ** of 34 which decodes to 64 samples.
343 error
= aiff_ima_init (psf
, AIFC_IMA4_BLOCK_LEN
, AIFC_IMA4_SAMPLES_PER_BLOCK
) ;
345 /* Lite remove end */
347 case SF_FORMAT_GSM610
:
348 error
= gsm610_init (psf
) ;
351 default : return SFE_UNIMPLEMENTED
;
358 /*==========================================================================================
359 ** Private functions.
362 /* This function ought to check size */
364 marker_to_position (const MARK_ID_POS
*m
, unsigned short n
, int marksize
)
367 for (i
= 0 ; i
< marksize
; i
++)
368 if (m
[i
].markerID
== n
)
369 return m
[i
].position
;
371 } /* marker_to_position */
374 aiff_read_header (SF_PRIVATE
*psf
, COMM_CHUNK
*comm_fmt
)
375 { SSND_CHUNK ssnd_fmt
;
376 AIFF_PRIVATE
*paiff
;
377 unsigned marker
, dword
, FORMsize
, SSNDsize
, bytesread
;
378 int k
, found_chunk
= 0, done
= 0, error
= 0 ;
380 int instr_found
= 0, mark_found
= 0, mark_count
= 0 ;
382 if (psf
->filelength
> SF_PLATFORM_S64 (0xffffffff))
383 psf_log_printf (psf
, "Warning : filelength > 0xffffffff. This is bad!!!!\n") ;
385 if ((paiff
= psf
->container_data
) == NULL
)
386 return SFE_INTERNAL
;
388 paiff
->comm_offset
= 0 ;
389 paiff
->ssnd_offset
= 0 ;
391 /* Set position to start of file to begin reading header. */
392 psf_binheader_readf (psf
, "p", 0) ;
394 memset (comm_fmt
, 0, sizeof (COMM_CHUNK
)) ;
396 /* Until recently AIF* file were all BIG endian. */
397 psf
->endian
= SF_ENDIAN_BIG
;
399 /* AIFF files can apparently have their chunks in any order. However, they
400 ** must have a FORM chunk. Approach here is to read all the chunks one by
401 ** one and then check for the mandatory chunks at the end.
404 { psf_binheader_readf (psf
, "m", &marker
) ;
406 if (psf
->file
.mode
== SFM_RDWR
&& (found_chunk
& HAVE_SSND
))
407 return SFE_AIFF_RW_SSND_NOT_LAST
;
412 return SFE_AIFF_NO_FORM
;
414 psf_binheader_readf (psf
, "E4", &FORMsize
) ;
415 pchk4_store (&paiff
->chunk4
, marker
, psf
->headindex
- 8, FORMsize
) ;
417 if (psf
->fileoffset
> 0 && psf
->filelength
> FORMsize
+ 8)
418 { /* Set file length. */
419 psf
->filelength
= FORMsize
+ 8 ;
420 psf_log_printf (psf
, "FORM : %u\n", FORMsize
) ;
422 else if (FORMsize
!= psf
->filelength
- 2 * SIGNED_SIZEOF (dword
))
423 { dword
= psf
->filelength
- 2 * sizeof (dword
) ;
424 psf_log_printf (psf
, "FORM : %u (should be %u)\n", FORMsize
, dword
) ;
428 psf_log_printf (psf
, "FORM : %u\n", FORMsize
) ;
429 found_chunk
|= HAVE_FORM
;
434 if ((found_chunk
& HAVE_FORM
) == 0)
435 return SFE_AIFF_AIFF_NO_FORM
;
436 psf_log_printf (psf
, " %M\n", marker
) ;
437 found_chunk
|= (marker
== AIFC_MARKER
) ? (HAVE_AIFC
| HAVE_AIFF
) : HAVE_AIFF
;
441 paiff
->comm_offset
= psf_ftell (psf
) - 4 ;
442 error
= aiff_read_comm_chunk (psf
, comm_fmt
) ;
443 pchk4_store (&paiff
->chunk4
, marker
, paiff
->comm_offset
, comm_fmt
->size
) ;
446 psf
->sf
.samplerate
= tenbytefloat2int (comm_fmt
->sampleRate
) ;
447 psf
->sf
.frames
= comm_fmt
->numSampleFrames
;
448 psf
->sf
.channels
= comm_fmt
->numChannels
;
449 psf
->bytewidth
= BITWIDTH2BYTES (comm_fmt
->sampleSize
) ;
454 found_chunk
|= HAVE_COMM
;
458 /* Must have COMM chunk before PEAK chunk. */
459 if ((found_chunk
& (HAVE_FORM
| HAVE_AIFF
| HAVE_COMM
)) != (HAVE_FORM
| HAVE_AIFF
| HAVE_COMM
))
460 return SFE_AIFF_PEAK_B4_COMM
;
462 psf_binheader_readf (psf
, "E4", &dword
) ;
463 pchk4_store (&paiff
->chunk4
, marker
, psf
->headindex
- 8, dword
) ;
465 psf_log_printf (psf
, "%M : %d\n", marker
, dword
) ;
466 if (dword
!= AIFF_PEAK_CHUNK_SIZE (psf
->sf
.channels
))
467 { psf_binheader_readf (psf
, "j", dword
) ;
468 psf_log_printf (psf
, "*** File PEAK chunk too big.\n") ;
469 return SFE_WAV_BAD_PEAK
;
472 if ((psf
->peak_info
= peak_info_calloc (psf
->sf
.channels
)) == NULL
)
473 return SFE_MALLOC_FAILED
;
475 /* read in rest of PEAK chunk. */
476 psf_binheader_readf (psf
, "E44", &(psf
->peak_info
->version
), &(psf
->peak_info
->timestamp
)) ;
478 if (psf
->peak_info
->version
!= 1)
479 psf_log_printf (psf
, " version : %d *** (should be version 1)\n", psf
->peak_info
->version
) ;
481 psf_log_printf (psf
, " version : %d\n", psf
->peak_info
->version
) ;
483 psf_log_printf (psf
, " time stamp : %d\n", psf
->peak_info
->timestamp
) ;
484 psf_log_printf (psf
, " Ch Position Value\n") ;
487 for (dword
= 0 ; dword
< (unsigned) psf
->sf
.channels
; dword
++)
489 unsigned int position
;
491 psf_binheader_readf (psf
, "Ef4", &value
, &position
) ;
492 psf
->peak_info
->peaks
[dword
].value
= value
;
493 psf
->peak_info
->peaks
[dword
].position
= position
;
495 snprintf (cptr
, sizeof (psf
->u
.scbuf
), " %2d %-12" PRId64
" %g\n",
496 dword
, psf
->peak_info
->peaks
[dword
].position
, psf
->peak_info
->peaks
[dword
].value
) ;
497 cptr
[sizeof (psf
->u
.scbuf
) - 1] = 0 ;
498 psf_log_printf (psf
, "%s", cptr
) ;
501 psf
->peak_info
->peak_loc
= ((found_chunk
& HAVE_SSND
) == 0) ? SF_PEAK_START
: SF_PEAK_END
;
505 if ((found_chunk
& HAVE_AIFC
) && (found_chunk
& HAVE_FVER
) == 0)
506 psf_log_printf (psf
, "*** Valid AIFC files should have an FVER chunk.\n") ;
508 paiff
->ssnd_offset
= psf_ftell (psf
) - 4 ;
509 psf_binheader_readf (psf
, "E444", &SSNDsize
, &(ssnd_fmt
.offset
), &(ssnd_fmt
.blocksize
)) ;
510 pchk4_store (&paiff
->chunk4
, marker
, paiff
->ssnd_offset
, SSNDsize
) ;
512 psf
->datalength
= SSNDsize
- sizeof (ssnd_fmt
) ;
513 psf
->dataoffset
= psf_ftell (psf
) ;
515 if (psf
->datalength
> psf
->filelength
- psf
->dataoffset
|| psf
->datalength
< 0)
516 { psf_log_printf (psf
, " SSND : %u (should be %D)\n", SSNDsize
, psf
->filelength
- psf
->dataoffset
+ sizeof (SSND_CHUNK
)) ;
517 psf
->datalength
= psf
->filelength
- psf
->dataoffset
;
520 psf_log_printf (psf
, " SSND : %u\n", SSNDsize
) ;
522 if (ssnd_fmt
.offset
== 0 || psf
->dataoffset
+ ssnd_fmt
.offset
== ssnd_fmt
.blocksize
)
523 { psf_log_printf (psf
, " Offset : %u\n", ssnd_fmt
.offset
) ;
524 psf_log_printf (psf
, " Block Size : %u\n", ssnd_fmt
.blocksize
) ;
526 psf
->dataoffset
+= ssnd_fmt
.offset
;
527 psf
->datalength
-= ssnd_fmt
.offset
;
530 { psf_log_printf (psf
, " Offset : %u\n", ssnd_fmt
.offset
) ;
531 psf_log_printf (psf
, " Block Size : %u ???\n", ssnd_fmt
.blocksize
) ;
532 psf
->dataoffset
+= ssnd_fmt
.offset
;
533 psf
->datalength
-= ssnd_fmt
.offset
;
536 /* Only set dataend if there really is data at the end. */
537 if (psf
->datalength
+ psf
->dataoffset
< psf
->filelength
)
538 psf
->dataend
= psf
->datalength
+ psf
->dataoffset
;
540 found_chunk
|= HAVE_SSND
;
542 if (! psf
->sf
.seekable
)
545 /* Seek to end of SSND chunk. */
546 psf_fseek (psf
, psf
->dataoffset
+ psf
->datalength
+ (SSNDsize
& 1), SEEK_SET
) ;
550 psf_binheader_readf (psf
, "E4", &dword
) ;
551 pchk4_store (&paiff
->chunk4
, marker
, psf_ftell (psf
) - 8, dword
) ;
554 if (dword
>= SIGNED_SIZEOF (psf
->u
.scbuf
))
555 { psf_log_printf (psf
, " %M : %d (too big)\n", marker
, dword
) ;
556 return SFE_INTERNAL
;
560 psf_binheader_readf (psf
, "b", cptr
, dword
+ (dword
& 1)) ;
563 psf_sanitize_string (cptr
, dword
) ;
565 psf_log_printf (psf
, " %M : %s\n", marker
, cptr
) ;
566 psf_store_string (psf
, SF_STR_COPYRIGHT
, cptr
) ;
570 psf_binheader_readf (psf
, "E4", &dword
) ;
571 pchk4_store (&paiff
->chunk4
, marker
, psf_ftell (psf
) - 8, dword
) ;
574 if (dword
>= SIGNED_SIZEOF (psf
->u
.scbuf
) - 1)
575 { psf_log_printf (psf
, " %M : %d (too big)\n", marker
, dword
) ;
576 return SFE_INTERNAL
;
580 psf_binheader_readf (psf
, "b", cptr
, dword
+ (dword
& 1)) ;
582 psf_log_printf (psf
, " %M : %s\n", marker
, cptr
) ;
583 psf_store_string (psf
, SF_STR_ARTIST
, cptr
) ;
587 { unsigned short count
, id
, len
;
588 unsigned int timestamp
;
590 psf_binheader_readf (psf
, "E42", &dword
, &count
) ;
591 pchk4_store (&paiff
->chunk4
, marker
, psf_ftell (psf
) - 8, dword
) ;
592 psf_log_printf (psf
, " %M : %d\n count : %d\n", marker
, dword
, count
) ;
593 dword
+= (dword
& 1) ;
598 for (k
= 0 ; k
< count
; k
++)
599 { dword
-= psf_binheader_readf (psf
, "E422", ×tamp
, &id
, &len
) ;
600 psf_log_printf (psf
, " time : 0x%x\n marker : %x\n length : %d\n", timestamp
, id
, len
) ;
602 if (len
+ 1 > SIGNED_SIZEOF (psf
->u
.scbuf
))
603 { psf_log_printf (psf
, "\nError : string length (%d) too big.\n", len
) ;
604 return SFE_INTERNAL
;
608 dword
-= psf_binheader_readf (psf
, "b", cptr
, len
) ;
610 psf_log_printf (psf
, " string : %s\n", cptr
) ;
614 psf_binheader_readf (psf
, "j", dword
) ;
619 { unsigned appl_marker
;
621 psf_binheader_readf (psf
, "E4", &dword
) ;
622 pchk4_store (&paiff
->chunk4
, marker
, psf_ftell (psf
) - 8, dword
) ;
625 if (dword
>= SIGNED_SIZEOF (psf
->u
.scbuf
) - 1)
626 { psf_log_printf (psf
, " %M : %d (too big, skipping)\n", marker
, dword
) ;
627 psf_binheader_readf (psf
, "j", dword
+ (dword
& 1)) ;
632 { psf_log_printf (psf
, " %M : %d (too small, skipping)\n", marker
, dword
) ;
633 psf_binheader_readf (psf
, "j", dword
+ (dword
& 1)) ;
638 psf_binheader_readf (psf
, "mb", &appl_marker
, cptr
, dword
+ (dword
& 1) - 4) ;
641 for (k
= 0 ; k
< (int) dword
; k
++)
642 if (! psf_isprint (cptr
[k
]))
647 psf_log_printf (psf
, " %M : %d\n AppSig : %M\n Name : %s\n", marker
, dword
, appl_marker
, cptr
) ;
648 psf_store_string (psf
, SF_STR_SOFTWARE
, cptr
) ;
653 psf_binheader_readf (psf
, "E4", &dword
) ;
654 pchk4_store (&paiff
->chunk4
, marker
, psf_ftell (psf
) - 8, dword
) ;
657 if (dword
>= SIGNED_SIZEOF (psf
->u
.scbuf
) - 2)
658 { psf_log_printf (psf
, " %M : %d (too big)\n", marker
, dword
) ;
659 return SFE_INTERNAL
;
663 psf_binheader_readf (psf
, "b", cptr
, dword
+ (dword
& 1)) ;
665 psf_log_printf (psf
, " %M : %s\n", marker
, cptr
) ;
666 psf_store_string (psf
, SF_STR_TITLE
, cptr
) ;
670 psf_binheader_readf (psf
, "E4", &dword
) ;
671 pchk4_store (&paiff
->chunk4
, marker
, psf_ftell (psf
) - 8, dword
) ;
674 if (dword
>= SIGNED_SIZEOF (psf
->u
.scbuf
) - 2)
675 { psf_log_printf (psf
, " %M : %d (too big)\n", marker
, dword
) ;
676 return SFE_INTERNAL
;
680 psf_binheader_readf (psf
, "b", cptr
, dword
+ (dword
& 1)) ;
682 psf_log_printf (psf
, " %M : %s\n", marker
, cptr
) ;
683 psf_store_string (psf
, SF_STR_COMMENT
, cptr
) ;
687 psf_binheader_readf (psf
, "E4", &dword
) ;
688 pchk4_store (&paiff
->chunk4
, marker
, psf_ftell (psf
) - 8, dword
) ;
689 if (dword
!= SIZEOF_INST_CHUNK
)
690 { psf_log_printf (psf
, " %M : %d (should be %d)\n", marker
, dword
, SIZEOF_INST_CHUNK
) ;
691 psf_binheader_readf (psf
, "j", dword
) ;
694 psf_log_printf (psf
, " %M : %d\n", marker
, dword
) ;
695 { unsigned char bytes
[6] ;
698 if (psf
->instrument
== NULL
&& (psf
->instrument
= psf_instrument_alloc ()) == NULL
)
699 return SFE_MALLOC_FAILED
;
701 psf_binheader_readf (psf
, "b", bytes
, 6) ;
702 psf_log_printf (psf
, " Base Note : %u\n Detune : %u\n"
703 " Low Note : %u\n High Note : %u\n"
704 " Low Vel. : %u\n High Vel. : %u\n",
705 bytes
[0], bytes
[1], bytes
[2], bytes
[3], bytes
[4], bytes
[5]) ;
706 psf
->instrument
->basenote
= bytes
[0] ;
707 psf
->instrument
->detune
= bytes
[1] ;
708 psf
->instrument
->key_lo
= bytes
[2] ;
709 psf
->instrument
->key_hi
= bytes
[3] ;
710 psf
->instrument
->velocity_lo
= bytes
[4] ;
711 psf
->instrument
->velocity_hi
= bytes
[5] ;
712 psf_binheader_readf (psf
, "E2", &gain
) ;
713 psf
->instrument
->gain
= gain
;
714 psf_log_printf (psf
, " Gain (dB) : %d\n", gain
) ;
716 { short mode
; /* 0 - no loop, 1 - forward looping, 2 - backward looping */
717 const char *loop_mode
;
718 unsigned short begin
, end
;
720 psf_binheader_readf (psf
, "E222", &mode
, &begin
, &end
) ;
721 loop_mode
= get_loop_mode_str (mode
) ;
722 mode
= get_loop_mode (mode
) ;
723 if (mode
== SF_LOOP_NONE
)
724 { psf
->instrument
->loop_count
= 0 ;
725 psf
->instrument
->loops
[0].mode
= SF_LOOP_NONE
;
728 { psf
->instrument
->loop_count
= 1 ;
729 psf
->instrument
->loops
[0].mode
= SF_LOOP_FORWARD
;
730 psf
->instrument
->loops
[0].start
= begin
;
731 psf
->instrument
->loops
[0].end
= end
;
732 psf
->instrument
->loops
[0].count
= 0 ;
734 psf_log_printf (psf
, " Sustain\n mode : %d => %s\n begin : %u\n end : %u\n",
735 mode
, loop_mode
, begin
, end
) ;
736 psf_binheader_readf (psf
, "E222", &mode
, &begin
, &end
) ;
737 loop_mode
= get_loop_mode_str (mode
) ;
738 mode
= get_loop_mode (mode
) ;
739 if (mode
== SF_LOOP_NONE
)
740 psf
->instrument
->loops
[1].mode
= SF_LOOP_NONE
;
742 { psf
->instrument
->loop_count
+= 1 ;
743 psf
->instrument
->loops
[1].mode
= SF_LOOP_FORWARD
;
744 psf
->instrument
->loops
[1].start
= begin
;
745 psf
->instrument
->loops
[1].end
= end
;
746 psf
->instrument
->loops
[1].count
= 0 ;
748 psf_log_printf (psf
, " Release\n mode : %d => %s\n begin : %u\n end : %u\n",
749 mode
, loop_mode
, begin
, end
) ;
755 psf_binheader_readf (psf
, "E4", &dword
) ;
756 pchk4_store (&paiff
->chunk4
, marker
, psf_ftell (psf
) - 8, dword
) ;
757 psf_log_printf (psf
, " basc : %u\n", dword
) ;
759 if ((error
= aiff_read_basc_chunk (psf
, dword
)))
764 psf_binheader_readf (psf
, "E4", &dword
) ;
765 pchk4_store (&paiff
->chunk4
, marker
, psf_ftell (psf
) - 8, dword
) ;
766 psf_log_printf (psf
, " %M : %d\n", marker
, dword
) ;
767 { unsigned short mark_id
, n
= 0 ;
768 unsigned int position
;
770 bytesread
= psf_binheader_readf (psf
, "E2", &n
) ;
772 psf_log_printf (psf
, " Count : %d\n", mark_count
) ;
773 if (paiff
->markstr
!= NULL
)
774 { psf_log_printf (psf
, "*** Second MARK chunk found. Throwing away the first.\n") ;
775 free (paiff
->markstr
) ;
777 paiff
->markstr
= calloc (mark_count
, sizeof (MARK_ID_POS
)) ;
778 if (paiff
->markstr
== NULL
)
779 return SFE_MALLOC_FAILED
;
781 for (n
= 0 ; n
< mark_count
&& bytesread
< dword
; n
++)
782 { unsigned int pstr_len
;
785 bytesread
+= psf_binheader_readf (psf
, "E241", &mark_id
, &position
, &ch
) ;
786 psf_log_printf (psf
, " Mark ID : %u\n Position : %u\n", mark_id
, position
) ;
788 pstr_len
= (ch
& 1) ? ch
: ch
+ 1 ;
790 if (pstr_len
< sizeof (psf
->u
.scbuf
) - 1)
791 { bytesread
+= psf_binheader_readf (psf
, "b", psf
->u
.scbuf
, pstr_len
) ;
792 psf
->u
.scbuf
[pstr_len
] = 0 ;
795 { unsigned int read_len
= pstr_len
- (sizeof (psf
->u
.scbuf
) - 1) ;
796 bytesread
+= psf_binheader_readf (psf
, "bj", psf
->u
.scbuf
, read_len
, pstr_len
- read_len
) ;
797 psf
->u
.scbuf
[sizeof (psf
->u
.scbuf
) - 1] = 0 ;
800 psf_log_printf (psf
, " Name : %s\n", psf
->u
.scbuf
) ;
802 paiff
->markstr
[n
].markerID
= mark_id
;
803 paiff
->markstr
[n
].position
= position
;
805 ** TODO if psf->u.scbuf is equal to
806 ** either Beg_loop, Beg loop or beg loop and spam
807 ** if (psf->instrument == NULL && (psf->instrument = psf_instrument_alloc ()) == NULL)
808 ** return SFE_MALLOC_FAILED ;
813 psf_binheader_readf (psf
, "j", dword
- bytesread
) ;
817 found_chunk
|= HAVE_FVER
;
818 /* Fall through to next case. */
821 psf_binheader_readf (psf
, "E4", &dword
) ;
822 pchk4_store (&paiff
->chunk4
, marker
, psf_ftell (psf
) - 8, dword
) ;
823 psf_log_printf (psf
, " %M : %d\n", marker
, dword
) ;
825 psf_binheader_readf (psf
, "j", dword
) ;
829 /* Fix for broken AIFC files with incorrect COMM chunk length. */
830 { unsigned char byte
;
831 psf_binheader_readf (psf
, "1", &byte
) ;
833 psf_binheader_readf (psf
, "j", dword
) ;
838 psf_binheader_readf (psf
, "E4", &dword
) ;
839 pchk4_store (&paiff
->chunk4
, marker
, psf_ftell (psf
) - 8, dword
) ;
842 { psf_log_printf (psf
, " %M : %d (should be >= 12)\n", marker
, dword
) ;
843 psf_binheader_readf (psf
, "j", dword
) ;
847 psf_log_printf (psf
, " %M : %d\n", marker
, dword
) ;
849 if ((error
= aiff_read_chanmap (psf
, dword
)))
854 if (psf_isprint ((marker
>> 24) & 0xFF) && psf_isprint ((marker
>> 16) & 0xFF)
855 && psf_isprint ((marker
>> 8) & 0xFF) && psf_isprint (marker
& 0xFF))
856 { psf_binheader_readf (psf
, "E4", &dword
) ;
857 psf_log_printf (psf
, " %M : %d (unknown marker)\n", marker
, dword
) ;
859 psf_binheader_readf (psf
, "j", dword
) ;
862 if ((dword
= psf_ftell (psf
)) & 0x03)
863 { psf_log_printf (psf
, " Unknown chunk marker %X at position %d. Resyncing.\n", marker
, dword
- 4) ;
865 psf_binheader_readf (psf
, "j", -3) ;
868 psf_log_printf (psf
, "*** Unknown chunk marker %X at position %D. Exiting parser.\n", marker
, psf_ftell (psf
)) ;
871 } ; /* switch (marker) */
873 if ((! psf
->sf
.seekable
) && (found_chunk
& HAVE_SSND
))
876 if (psf_ftell (psf
) >= psf
->filelength
- (2 * SIGNED_SIZEOF (dword
)))
880 if (instr_found
&& mark_found
)
883 for (j
= 0 ; j
<psf
->instrument
->loop_count
; j
++)
884 { if (j
< ARRAY_LEN (psf
->instrument
->loops
))
885 { psf
->instrument
->loops
[j
].start
= marker_to_position (paiff
->markstr
, psf
->instrument
->loops
[j
].start
, mark_count
) ;
886 psf
->instrument
->loops
[j
].end
= marker_to_position (paiff
->markstr
, psf
->instrument
->loops
[j
].end
, mark_count
) ;
887 psf
->instrument
->loops
[j
].mode
= SF_LOOP_FORWARD
;
892 if (! (found_chunk
& HAVE_FORM
))
893 return SFE_AIFF_NO_FORM
;
895 if (! (found_chunk
& HAVE_AIFF
))
896 return SFE_AIFF_COMM_NO_FORM
;
898 if (! (found_chunk
& HAVE_COMM
))
899 return SFE_AIFF_SSND_NO_COMM
;
901 if (! psf
->dataoffset
)
902 return SFE_AIFF_NO_DATA
;
905 } /* aiff_read_header */
908 aiff_close (SF_PRIVATE
*psf
)
909 { AIFF_PRIVATE
*paiff
= psf
->container_data
;
911 if (paiff
!= NULL
&& paiff
->markstr
!= NULL
)
912 { free (paiff
->markstr
) ;
913 paiff
->markstr
= NULL
;
916 if (psf
->file
.mode
== SFM_WRITE
|| psf
->file
.mode
== SFM_RDWR
)
917 { aiff_write_tailer (psf
) ;
918 aiff_write_header (psf
, SF_TRUE
) ;
925 aiff_read_comm_chunk (SF_PRIVATE
*psf
, COMM_CHUNK
*comm_fmt
)
926 { int error
= 0, bytesread
, subformat
;
928 psf
->u
.scbuf
[0] = 0 ;
930 bytesread
= psf_binheader_readf (psf
, "E4", &(comm_fmt
->size
)) ;
932 /* The COMM chunk has an int aligned to an odd word boundary. Some
933 ** procesors are not able to deal with this (ie bus fault) so we have
934 ** to take special care.
936 comm_fmt
->size
+= comm_fmt
->size
& 1 ;
939 psf_binheader_readf (psf
, "E242b", &(comm_fmt
->numChannels
), &(comm_fmt
->numSampleFrames
),
940 &(comm_fmt
->sampleSize
), &(comm_fmt
->sampleRate
), SIGNED_SIZEOF (comm_fmt
->sampleRate
)) ;
942 if (comm_fmt
->size
> 0x10000 && (comm_fmt
->size
& 0xffff) == 0)
943 { psf_log_printf (psf
, " COMM : %d (0x%x) *** should be ", comm_fmt
->size
, comm_fmt
->size
) ;
944 comm_fmt
->size
= ENDSWAP_INT (comm_fmt
->size
) ;
945 psf_log_printf (psf
, "%d (0x%x)\n", comm_fmt
->size
, comm_fmt
->size
) ;
948 psf_log_printf (psf
, " COMM : %d\n", comm_fmt
->size
) ;
950 if (comm_fmt
->size
== SIZEOF_AIFF_COMM
)
951 comm_fmt
->encoding
= NONE_MARKER
;
952 else if (comm_fmt
->size
== SIZEOF_AIFC_COMM_MIN
)
953 bytesread
+= psf_binheader_readf (psf
, "Em", &(comm_fmt
->encoding
)) ;
954 else if (comm_fmt
->size
>= SIZEOF_AIFC_COMM
)
955 { unsigned char encoding_len
;
958 bytesread
+= psf_binheader_readf (psf
, "Em1", &(comm_fmt
->encoding
), &encoding_len
) ;
960 comm_fmt
->size
= SF_MIN (sizeof (psf
->u
.scbuf
), make_size_t (comm_fmt
->size
)) ;
961 memset (psf
->u
.scbuf
, 0, comm_fmt
->size
) ;
962 read_len
= comm_fmt
->size
- SIZEOF_AIFC_COMM
+ 1 ;
963 bytesread
+= psf_binheader_readf (psf
, "b", psf
->u
.scbuf
, read_len
) ;
964 psf
->u
.scbuf
[read_len
+ 1] = 0 ;
967 psf_log_printf (psf
, " Sample Rate : %d\n", tenbytefloat2int (comm_fmt
->sampleRate
)) ;
968 psf_log_printf (psf
, " Frames : %u%s\n", comm_fmt
->numSampleFrames
, (comm_fmt
->numSampleFrames
== 0 && psf
->filelength
> 104) ? " (Should not be 0)" : "") ;
969 psf_log_printf (psf
, " Channels : %d\n", comm_fmt
->numChannels
) ;
971 /* Found some broken 'fl32' files with comm.samplesize == 16. Fix it here. */
972 if ((comm_fmt
->encoding
== fl32_MARKER
|| comm_fmt
->encoding
== FL32_MARKER
) && comm_fmt
->sampleSize
!= 32)
973 { psf_log_printf (psf
, " Sample Size : %d (should be 32)\n", comm_fmt
->sampleSize
) ;
974 comm_fmt
->sampleSize
= 32 ;
976 else if ((comm_fmt
->encoding
== fl64_MARKER
|| comm_fmt
->encoding
== FL64_MARKER
) && comm_fmt
->sampleSize
!= 64)
977 { psf_log_printf (psf
, " Sample Size : %d (should be 64)\n", comm_fmt
->sampleSize
) ;
978 comm_fmt
->sampleSize
= 64 ;
981 psf_log_printf (psf
, " Sample Size : %d\n", comm_fmt
->sampleSize
) ;
983 subformat
= s_bitwidth_to_subformat (comm_fmt
->sampleSize
) ;
985 psf
->endian
= SF_ENDIAN_BIG
;
987 switch (comm_fmt
->encoding
)
989 psf
->sf
.format
= (SF_FORMAT_AIFF
| subformat
) ;
995 psf
->sf
.format
= (SF_ENDIAN_BIG
| SF_FORMAT_AIFF
| subformat
) ;
1001 psf
->endian
= SF_ENDIAN_LITTLE
;
1002 psf
->sf
.format
= (SF_ENDIAN_LITTLE
| SF_FORMAT_AIFF
| subformat
) ;
1007 psf
->sf
.format
= (SF_FORMAT_AIFF
| SF_FORMAT_FLOAT
) ;
1012 psf
->sf
.format
= (SF_FORMAT_AIFF
| SF_FORMAT_ULAW
) ;
1017 psf
->sf
.format
= (SF_FORMAT_AIFF
| SF_FORMAT_ALAW
) ;
1022 psf
->sf
.format
= (SF_FORMAT_AIFF
| SF_FORMAT_DOUBLE
) ;
1026 psf
->sf
.format
= (SF_FORMAT_AIFF
| SF_FORMAT_PCM_U8
) ;
1030 psf
->sf
.format
= SF_FORMAT_AIFF
;
1031 switch (comm_fmt
->sampleSize
)
1033 psf
->sf
.format
|= SF_FORMAT_DWVW_12
;
1036 psf
->sf
.format
|= SF_FORMAT_DWVW_16
;
1039 psf
->sf
.format
|= SF_FORMAT_DWVW_24
;
1043 psf
->sf
.format
|= SF_FORMAT_DWVW_N
;
1049 psf
->sf
.format
= SF_FORMAT_AIFF
;
1050 psf
->sf
.format
= (SF_FORMAT_AIFF
| SF_FORMAT_GSM610
) ;
1055 psf
->endian
= SF_ENDIAN_BIG
;
1056 psf
->sf
.format
= (SF_FORMAT_AIFF
| SF_FORMAT_IMA_ADPCM
) ;
1060 psf_log_printf (psf
, "AIFC : Unimplemented format : %M\n", comm_fmt
->encoding
) ;
1061 error
= SFE_UNIMPLEMENTED
;
1064 if (! psf
->u
.scbuf
[0])
1065 psf_log_printf (psf
, " Encoding : %M\n", comm_fmt
->encoding
) ;
1067 psf_log_printf (psf
, " Encoding : %M => %s\n", comm_fmt
->encoding
, psf
->u
.scbuf
) ;
1070 } /* aiff_read_comm_chunk */
1073 /*==========================================================================================
1077 aiff_rewrite_header (SF_PRIVATE
*psf
, AIFF_PRIVATE
* paiff
)
1079 /* Assuming here that the header has already been written and just
1080 ** needs to be corrected for new data length. That means that we
1081 ** only change the length fields of the FORM and SSND chunks ;
1082 ** everything else can be skipped over.
1085 int comm_size
, comm_frames
;
1087 psf_fseek (psf
, 0, SEEK_SET
) ;
1088 psf_fread (psf
->header
, psf
->dataoffset
, 1, psf
) ;
1090 psf
->headindex
= 0 ;
1092 for (k
= 0 ; k
< paiff
->chunk4
.count
; k
++)
1093 { switch (paiff
->chunk4
.l
[k
].chunk
)
1094 { case FORM_MARKER
:
1095 psf_binheader_writef (psf
, "Etm8", FORM_MARKER
, psf
->filelength
- 8) ;
1099 psf
->headindex
= paiff
->chunk4
.l
[k
].offset
;
1100 comm_frames
= psf
->sf
.frames
;
1101 comm_size
= paiff
->chunk4
.l
[k
].len
;
1102 psf_binheader_writef (psf
, "Em42t4", COMM_MARKER
, comm_size
, psf
->sf
.channels
, comm_frames
) ;
1106 psf
->headindex
= paiff
->chunk4
.l
[k
].offset
;
1107 psf_binheader_writef (psf
, "Etm8", SSND_MARKER
, psf
->datalength
+ SIZEOF_SSND_CHUNK
) ;
1111 psf
->headindex
= paiff
->chunk4
.l
[k
].offset
;
1112 psf_binheader_writef (psf
, "Em4", PEAK_MARKER
, AIFF_PEAK_CHUNK_SIZE (psf
->sf
.channels
)) ;
1113 psf_binheader_writef (psf
, "E44", 1, time (NULL
)) ;
1114 for (ch
= 0 ; ch
< psf
->sf
.channels
; ch
++)
1115 psf_binheader_writef (psf
, "Eft8", (float) psf
->peak_info
->peaks
[ch
].value
, psf
->peak_info
->peaks
[ch
].position
) ;
1119 /* There are a whole bunch of chunks we should just ignore. */
1124 /* Header mangling complete so write it out. */
1125 psf_fseek (psf
, 0, SEEK_SET
) ;
1126 psf_fwrite (psf
->header
, psf
->headindex
, 1, psf
) ;
1129 } /* aiff_rewrite_header */
1132 aiff_write_header (SF_PRIVATE
*psf
, int calc_length
)
1133 { sf_count_t current
;
1134 AIFF_PRIVATE
*paiff
;
1135 unsigned char comm_sample_rate
[10], comm_zero_bytes
[2] = { 0, 0 } ;
1136 unsigned int comm_type
, comm_size
, comm_encoding
, comm_frames
= 0 ;
1137 int k
, endian
, has_data
= SF_FALSE
;
1140 if ((paiff
= psf
->container_data
) == NULL
)
1141 return SFE_INTERNAL
;
1143 current
= psf_ftell (psf
) ;
1145 if (current
> psf
->dataoffset
)
1146 has_data
= SF_TRUE
;
1149 { psf
->filelength
= psf_get_filelen (psf
) ;
1151 psf
->datalength
= psf
->filelength
- psf
->dataoffset
;
1153 psf
->datalength
-= psf
->filelength
- psf
->dataend
;
1155 if (psf
->bytewidth
> 0)
1156 psf
->sf
.frames
= psf
->datalength
/ (psf
->bytewidth
* psf
->sf
.channels
) ;
1159 if (psf
->file
.mode
== SFM_RDWR
&& psf
->dataoffset
> 0 && paiff
->chunk4
.count
> 0)
1160 { int err
= aiff_rewrite_header (psf
, paiff
) ;
1162 psf_fseek (psf
, current
, SEEK_SET
) ;
1166 endian
= SF_ENDIAN (psf
->sf
.format
) ;
1167 if (CPU_IS_LITTLE_ENDIAN
&& endian
== SF_ENDIAN_CPU
)
1168 endian
= SF_ENDIAN_LITTLE
;
1170 /* Standard value here. */
1171 bit_width
= psf
->bytewidth
* 8 ;
1172 comm_frames
= (psf
->sf
.frames
> 0xFFFFFFFF) ? 0xFFFFFFFF : psf
->sf
.frames
;
1174 switch (SF_CODEC (psf
->sf
.format
) | endian
)
1175 { case SF_FORMAT_PCM_S8
| SF_ENDIAN_BIG
:
1176 psf
->endian
= SF_ENDIAN_BIG
;
1177 comm_type
= AIFC_MARKER
;
1178 comm_size
= SIZEOF_AIFC_COMM
;
1179 comm_encoding
= twos_MARKER
;
1182 case SF_FORMAT_PCM_S8
| SF_ENDIAN_LITTLE
:
1183 psf
->endian
= SF_ENDIAN_LITTLE
;
1184 comm_type
= AIFC_MARKER
;
1185 comm_size
= SIZEOF_AIFC_COMM
;
1186 comm_encoding
= sowt_MARKER
;
1189 case SF_FORMAT_PCM_16
| SF_ENDIAN_BIG
:
1190 psf
->endian
= SF_ENDIAN_BIG
;
1191 comm_type
= AIFC_MARKER
;
1192 comm_size
= SIZEOF_AIFC_COMM
;
1193 comm_encoding
= twos_MARKER
;
1196 case SF_FORMAT_PCM_16
| SF_ENDIAN_LITTLE
:
1197 psf
->endian
= SF_ENDIAN_LITTLE
;
1198 comm_type
= AIFC_MARKER
;
1199 comm_size
= SIZEOF_AIFC_COMM
;
1200 comm_encoding
= sowt_MARKER
;
1203 case SF_FORMAT_PCM_24
| SF_ENDIAN_BIG
:
1204 psf
->endian
= SF_ENDIAN_BIG
;
1205 comm_type
= AIFC_MARKER
;
1206 comm_size
= SIZEOF_AIFC_COMM
;
1207 comm_encoding
= in24_MARKER
;
1210 case SF_FORMAT_PCM_24
| SF_ENDIAN_LITTLE
:
1211 psf
->endian
= SF_ENDIAN_LITTLE
;
1212 comm_type
= AIFC_MARKER
;
1213 comm_size
= SIZEOF_AIFC_COMM
;
1214 comm_encoding
= ni24_MARKER
;
1217 case SF_FORMAT_PCM_32
| SF_ENDIAN_BIG
:
1218 psf
->endian
= SF_ENDIAN_BIG
;
1219 comm_type
= AIFC_MARKER
;
1220 comm_size
= SIZEOF_AIFC_COMM
;
1221 comm_encoding
= in32_MARKER
;
1224 case SF_FORMAT_PCM_32
| SF_ENDIAN_LITTLE
:
1225 psf
->endian
= SF_ENDIAN_LITTLE
;
1226 comm_type
= AIFC_MARKER
;
1227 comm_size
= SIZEOF_AIFC_COMM
;
1228 comm_encoding
= ni32_MARKER
;
1231 case SF_FORMAT_PCM_S8
: /* SF_ENDIAN_FILE */
1232 case SF_FORMAT_PCM_16
:
1233 case SF_FORMAT_PCM_24
:
1234 case SF_FORMAT_PCM_32
:
1235 psf
->endian
= SF_ENDIAN_BIG
;
1236 comm_type
= AIFF_MARKER
;
1237 comm_size
= SIZEOF_AIFF_COMM
;
1241 case SF_FORMAT_FLOAT
: /* Big endian floating point. */
1242 psf
->endian
= SF_ENDIAN_BIG
;
1243 comm_type
= AIFC_MARKER
;
1244 comm_size
= SIZEOF_AIFC_COMM
;
1245 comm_encoding
= FL32_MARKER
; /* Use 'FL32' because its easier to read. */
1248 case SF_FORMAT_DOUBLE
: /* Big endian double precision floating point. */
1249 psf
->endian
= SF_ENDIAN_BIG
;
1250 comm_type
= AIFC_MARKER
;
1251 comm_size
= SIZEOF_AIFC_COMM
;
1252 comm_encoding
= FL64_MARKER
; /* Use 'FL64' because its easier to read. */
1255 case SF_FORMAT_ULAW
:
1256 psf
->endian
= SF_ENDIAN_BIG
;
1257 comm_type
= AIFC_MARKER
;
1258 comm_size
= SIZEOF_AIFC_COMM
;
1259 comm_encoding
= ulaw_MARKER
;
1262 case SF_FORMAT_ALAW
:
1263 psf
->endian
= SF_ENDIAN_BIG
;
1264 comm_type
= AIFC_MARKER
;
1265 comm_size
= SIZEOF_AIFC_COMM
;
1266 comm_encoding
= alaw_MARKER
;
1269 case SF_FORMAT_PCM_U8
:
1270 psf
->endian
= SF_ENDIAN_BIG
;
1271 comm_type
= AIFC_MARKER
;
1272 comm_size
= SIZEOF_AIFC_COMM
;
1273 comm_encoding
= raw_MARKER
;
1276 case SF_FORMAT_DWVW_12
:
1277 psf
->endian
= SF_ENDIAN_BIG
;
1278 comm_type
= AIFC_MARKER
;
1279 comm_size
= SIZEOF_AIFC_COMM
;
1280 comm_encoding
= DWVW_MARKER
;
1282 /* Override standard value here.*/
1286 case SF_FORMAT_DWVW_16
:
1287 psf
->endian
= SF_ENDIAN_BIG
;
1288 comm_type
= AIFC_MARKER
;
1289 comm_size
= SIZEOF_AIFC_COMM
;
1290 comm_encoding
= DWVW_MARKER
;
1292 /* Override standard value here.*/
1296 case SF_FORMAT_DWVW_24
:
1297 psf
->endian
= SF_ENDIAN_BIG
;
1298 comm_type
= AIFC_MARKER
;
1299 comm_size
= SIZEOF_AIFC_COMM
;
1300 comm_encoding
= DWVW_MARKER
;
1302 /* Override standard value here.*/
1306 case SF_FORMAT_GSM610
:
1307 psf
->endian
= SF_ENDIAN_BIG
;
1308 comm_type
= AIFC_MARKER
;
1309 comm_size
= SIZEOF_AIFC_COMM
;
1310 comm_encoding
= GSM_MARKER
;
1312 /* Override standard value here.*/
1316 case SF_FORMAT_IMA_ADPCM
:
1317 psf
->endian
= SF_ENDIAN_BIG
;
1318 comm_type
= AIFC_MARKER
;
1319 comm_size
= SIZEOF_AIFC_COMM
;
1320 comm_encoding
= ima4_MARKER
;
1322 /* Override standard value here.*/
1324 comm_frames
= psf
->sf
.frames
/ AIFC_IMA4_SAMPLES_PER_BLOCK
;
1327 default : return SFE_BAD_OPEN_FORMAT
;
1330 /* Reset the current header length to zero. */
1331 psf
->header
[0] = 0 ;
1332 psf
->headindex
= 0 ;
1333 psf_fseek (psf
, 0, SEEK_SET
) ;
1335 psf_binheader_writef (psf
, "Etm8", FORM_MARKER
, psf
->filelength
- 8) ;
1337 /* Write AIFF/AIFC marker and COM chunk. */
1338 if (comm_type
== AIFC_MARKER
)
1339 /* AIFC must have an FVER chunk. */
1340 psf_binheader_writef (psf
, "Emm44", comm_type
, FVER_MARKER
, 4, 0xA2805140) ;
1342 psf_binheader_writef (psf
, "Em", comm_type
) ;
1344 paiff
->comm_offset
= psf
->headindex
- 8 ;
1346 memset (comm_sample_rate
, 0, sizeof (comm_sample_rate
)) ;
1347 uint2tenbytefloat (psf
->sf
.samplerate
, comm_sample_rate
) ;
1349 psf_binheader_writef (psf
, "Em42t42", COMM_MARKER
, comm_size
, psf
->sf
.channels
, comm_frames
, bit_width
) ;
1350 psf_binheader_writef (psf
, "b", comm_sample_rate
, sizeof (comm_sample_rate
)) ;
1352 /* AIFC chunks have some extra data. */
1353 if (comm_type
== AIFC_MARKER
)
1354 psf_binheader_writef (psf
, "mb", comm_encoding
, comm_zero_bytes
, sizeof (comm_zero_bytes
)) ;
1356 if (psf
->channel_map
&& paiff
->chanmap_tag
)
1357 psf_binheader_writef (psf
, "Em4444", CHAN_MARKER
, 12, paiff
->chanmap_tag
, 0, 0) ;
1359 if (psf
->instrument
!= NULL
)
1360 { MARK_ID_POS m
[4] ;
1362 unsigned short ct
= 0 ;
1364 memset (m
, 0, sizeof (m
)) ;
1365 memset (&ch
, 0, sizeof (ch
)) ;
1367 ch
.baseNote
= psf
->instrument
->basenote
;
1368 ch
.detune
= psf
->instrument
->detune
;
1369 ch
.lowNote
= psf
->instrument
->key_lo
;
1370 ch
.highNote
= psf
->instrument
->key_hi
;
1371 ch
.lowVelocity
= psf
->instrument
->velocity_lo
;
1372 ch
.highVelocity
= psf
->instrument
->velocity_hi
;
1373 ch
.gain
= psf
->instrument
->gain
;
1374 if (psf
->instrument
->loops
[0].mode
!= SF_LOOP_NONE
)
1375 { ch
.sustain_loop
.playMode
= 1 ;
1376 ch
.sustain_loop
.beginLoop
= ct
;
1377 m
[0].markerID
= ct
++ ;
1378 m
[0].position
= psf
->instrument
->loops
[0].start
;
1379 ch
.sustain_loop
.endLoop
= ct
;
1380 m
[1].markerID
= ct
++ ;
1381 m
[1].position
= psf
->instrument
->loops
[0].end
;
1383 if (psf
->instrument
->loops
[1].mode
!= SF_LOOP_NONE
)
1384 { ch
.release_loop
.playMode
= 1 ;
1385 ch
.release_loop
.beginLoop
= ct
;
1386 m
[2].markerID
= ct
++ ;
1387 m
[2].position
= psf
->instrument
->loops
[1].start
;
1388 ch
.release_loop
.endLoop
= ct
;
1389 m
[3].markerID
= ct
++ ;
1390 m
[3].position
= psf
->instrument
->loops
[1].end
;
1393 { ch
.release_loop
.playMode
= 0 ;
1394 ch
.release_loop
.beginLoop
= 0 ;
1395 ch
.release_loop
.endLoop
= 0 ;
1398 psf_binheader_writef (psf
, "Em4111111", INST_MARKER
, SIZEOF_INST_CHUNK
, ch
.baseNote
, ch
.detune
,
1399 ch
.lowNote
, ch
.highNote
, ch
.lowVelocity
, ch
.highVelocity
) ;
1400 psf_binheader_writef (psf
, "2222222", ch
.gain
, ch
.sustain_loop
.playMode
,
1401 ch
.sustain_loop
.beginLoop
, ch
.sustain_loop
.endLoop
, ch
.release_loop
.playMode
,
1402 ch
.release_loop
.beginLoop
, ch
.release_loop
.endLoop
) ;
1405 psf_binheader_writef (psf
, "Em42241b241b",
1406 MARK_MARKER
, 2 + 2 * (2 + 4 + 1 + 9), 2,
1407 m
[0].markerID
, m
[0].position
, 8, "beg loop", make_size_t (9),
1408 m
[1].markerID
, m
[1].position
, 8, "end loop", make_size_t (9)) ;
1410 psf_binheader_writef (psf
, "Em42 241b 241b 241b 241b",
1411 MARK_MARKER
, 2 + 4 * (2 + 4 + 1 + 9), 4,
1412 m
[0].markerID
, m
[0].position
, 8, "beg loop", make_size_t (9),
1413 m
[1].markerID
, m
[1].position
, 8, "end loop", make_size_t (9),
1414 m
[2].markerID
, m
[2].position
, 8, "beg loop", make_size_t (9),
1415 m
[3].markerID
, m
[3].position
, 8, "end loop", make_size_t (9)) ;
1418 if (psf
->str_flags
& SF_STR_LOCATE_START
)
1419 aiff_write_strings (psf
, SF_STR_LOCATE_START
) ;
1421 if (psf
->peak_info
!= NULL
&& psf
->peak_info
->peak_loc
== SF_PEAK_START
)
1422 { psf_binheader_writef (psf
, "Em4", PEAK_MARKER
, AIFF_PEAK_CHUNK_SIZE (psf
->sf
.channels
)) ;
1423 psf_binheader_writef (psf
, "E44", 1, time (NULL
)) ;
1424 for (k
= 0 ; k
< psf
->sf
.channels
; k
++)
1425 psf_binheader_writef (psf
, "Eft8", (float) psf
->peak_info
->peaks
[k
].value
, psf
->peak_info
->peaks
[k
].position
) ;
1428 /* Write SSND chunk. */
1429 paiff
->ssnd_offset
= psf
->headindex
;
1430 psf_binheader_writef (psf
, "Etm844", SSND_MARKER
, psf
->datalength
+ SIZEOF_SSND_CHUNK
, 0, 0) ;
1432 /* Header construction complete so write it out. */
1433 psf_fwrite (psf
->header
, psf
->headindex
, 1, psf
) ;
1438 if (has_data
&& psf
->dataoffset
!= psf
->headindex
)
1439 return psf
->error
= SFE_INTERNAL
;
1441 psf
->dataoffset
= psf
->headindex
;
1444 psf_fseek (psf
, psf
->dataoffset
, SEEK_SET
) ;
1445 else if (current
> 0)
1446 psf_fseek (psf
, current
, SEEK_SET
) ;
1449 } /* aiff_write_header */
1452 aiff_write_tailer (SF_PRIVATE
*psf
)
1455 /* Reset the current header length to zero. */
1456 psf
->header
[0] = 0 ;
1457 psf
->headindex
= 0 ;
1459 psf
->dataend
= psf_fseek (psf
, 0, SEEK_END
) ;
1461 /* Make sure tailer data starts at even byte offset. Pad if necessary. */
1462 if (psf
->dataend
% 2 == 1)
1463 { psf_fwrite (psf
->header
, 1, 1, psf
) ;
1467 if (psf
->peak_info
!= NULL
&& psf
->peak_info
->peak_loc
== SF_PEAK_END
)
1468 { psf_binheader_writef (psf
, "Em4", PEAK_MARKER
, AIFF_PEAK_CHUNK_SIZE (psf
->sf
.channels
)) ;
1469 psf_binheader_writef (psf
, "E44", 1, time (NULL
)) ;
1470 for (k
= 0 ; k
< psf
->sf
.channels
; k
++)
1471 psf_binheader_writef (psf
, "Eft8", (float) psf
->peak_info
->peaks
[k
].value
, psf
->peak_info
->peaks
[k
].position
) ;
1474 if (psf
->str_flags
& SF_STR_LOCATE_END
)
1475 aiff_write_strings (psf
, SF_STR_LOCATE_END
) ;
1477 /* Write the tailer. */
1478 if (psf
->headindex
> 0)
1479 psf_fwrite (psf
->header
, psf
->headindex
, 1, psf
) ;
1482 } /* aiff_write_tailer */
1485 aiff_write_strings (SF_PRIVATE
*psf
, int location
)
1488 for (k
= 0 ; k
< SF_MAX_STRINGS
; k
++)
1489 { if (psf
->strings
[k
].type
== 0)
1492 if (psf
->strings
[k
].flags
!= location
)
1495 switch (psf
->strings
[k
].type
)
1496 { case SF_STR_SOFTWARE
:
1497 slen
= strlen (psf
->strings
[k
].str
) ;
1498 psf_binheader_writef (psf
, "Em4mb", APPL_MARKER
, slen
+ 4, m3ga_MARKER
, psf
->strings
[k
].str
, make_size_t (slen
+ (slen
& 1))) ;
1502 psf_binheader_writef (psf
, "EmS", NAME_MARKER
, psf
->strings
[k
].str
) ;
1505 case SF_STR_COPYRIGHT
:
1506 psf_binheader_writef (psf
, "EmS", c_MARKER
, psf
->strings
[k
].str
) ;
1509 case SF_STR_ARTIST
:
1510 psf_binheader_writef (psf
, "EmS", AUTH_MARKER
, psf
->strings
[k
].str
) ;
1513 case SF_STR_COMMENT
:
1514 psf_binheader_writef (psf
, "EmS", ANNO_MARKER
, psf
->strings
[k
].str
) ;
1519 psf_binheader_writef (psf, "Ems", ICRD_MARKER, psf->strings [k].str) ;
1526 } /* aiff_write_strings */
1529 aiff_command (SF_PRIVATE
* psf
, int command
, void * UNUSED (data
), int UNUSED (datasize
))
1530 { AIFF_PRIVATE
*paiff
;
1532 if ((paiff
= psf
->container_data
) == NULL
)
1533 return SFE_INTERNAL
;
1536 { case SFC_SET_CHANNEL_MAP_INFO
:
1537 paiff
->chanmap_tag
= aiff_caf_find_channel_layout_tag (psf
->channel_map
, psf
->sf
.channels
) ;
1538 return (paiff
->chanmap_tag
!= 0) ;
1547 } /* aiff_command */
1550 get_loop_mode_str (short mode
)
1552 { case 0 : return "none" ;
1553 case 1 : return "forward" ;
1554 case 2 : return "backward" ;
1557 return "*** unknown" ;
1558 } /* get_loop_mode_str */
1561 get_loop_mode (short mode
)
1563 { case 0 : return SF_LOOP_NONE
;
1564 case 1 : return SF_LOOP_FORWARD
;
1565 case 2 : return SF_LOOP_BACKWARD
;
1568 return SF_LOOP_NONE
;
1569 } /* get_loop_mode */
1571 /*==========================================================================================
1572 ** Rough hack at converting from 80 bit IEEE float in AIFF header to an int and
1573 ** back again. It assumes that all sample rates are between 1 and 800MHz, which
1574 ** should be OK as other sound file formats use a 32 bit integer to store sample
1576 ** There is another (probably better) version in the source code to the SoX but it
1577 ** has a copyright which probably prevents it from being allowable as GPL/LGPL.
1581 tenbytefloat2int (unsigned char *bytes
)
1584 if (bytes
[0] & 0x80) /* Negative number. */
1587 if (bytes
[0] <= 0x3F) /* Less than 1. */
1590 if (bytes
[0] > 0x40) /* Way too big. */
1593 if (bytes
[0] == 0x40 && bytes
[1] > 0x1C) /* Too big. */
1596 /* Ok, can handle it. */
1598 val
= (bytes
[2] << 23) | (bytes
[3] << 15) | (bytes
[4] << 7) | (bytes
[5] >> 1) ;
1600 val
>>= (29 - bytes
[1]) ;
1603 } /* tenbytefloat2int */
1606 uint2tenbytefloat (unsigned int num
, unsigned char *bytes
)
1607 { unsigned int mask
= 0x40000000 ;
1611 { bytes
[0] = 0x3F ;
1620 { bytes
[1] = 0x1D ;
1624 for (count
= 0 ; count
<= 32 ; count
++)
1631 bytes
[1] = 29 - count
;
1632 bytes
[2] = (num
>> 24) & 0xFF ;
1633 bytes
[3] = (num
>> 16) & 0xFF ;
1634 bytes
[4] = (num
>> 8) & 0xFF ;
1635 bytes
[5] = num
& 0xFF ;
1637 } /* uint2tenbytefloat */
1640 aiff_read_basc_chunk (SF_PRIVATE
* psf
, int datasize
)
1641 { const char * type_str
;
1645 count
= psf_binheader_readf (psf
, "E442", &bc
.version
, &bc
.numBeats
, &bc
.rootNote
) ;
1646 count
+= psf_binheader_readf (psf
, "E222", &bc
.scaleType
, &bc
.sigNumerator
, &bc
.sigDenominator
) ;
1647 count
+= psf_binheader_readf (psf
, "E2j", &bc
.loopType
, datasize
- sizeof (bc
)) ;
1649 psf_log_printf (psf
, " Version ? : %u\n Num Beats : %u\n Root Note : 0x%x\n",
1650 bc
.version
, bc
.numBeats
, bc
.rootNote
) ;
1652 switch (bc
.scaleType
)
1653 { case basc_SCALE_MINOR
:
1654 type_str
= "MINOR" ;
1656 case basc_SCALE_MAJOR
:
1657 type_str
= "MAJOR" ;
1659 case basc_SCALE_NEITHER
:
1660 type_str
= "NEITHER" ;
1662 case basc_SCALE_BOTH
:
1666 type_str
= "!!WRONG!!" ;
1670 psf_log_printf (psf
, " ScaleType : 0x%x (%s)\n", bc
.scaleType
, type_str
) ;
1671 psf_log_printf (psf
, " Time Sig : %d/%d\n", bc
.sigNumerator
, bc
.sigDenominator
) ;
1673 switch (bc
.loopType
)
1674 { case basc_TYPE_ONE_SHOT
:
1675 type_str
= "One Shot" ;
1677 case basc_TYPE_LOOP
:
1681 type_str
= "!!WRONG!!" ;
1685 psf_log_printf (psf
, " Loop Type : 0x%x (%s)\n", bc
.loopType
, type_str
) ;
1687 if ((psf
->loop_info
= calloc (1, sizeof (SF_LOOP_INFO
))) == NULL
)
1688 return SFE_MALLOC_FAILED
;
1690 psf
->loop_info
->time_sig_num
= bc
.sigNumerator
;
1691 psf
->loop_info
->time_sig_den
= bc
.sigDenominator
;
1692 psf
->loop_info
->loop_mode
= (bc
.loopType
== basc_TYPE_ONE_SHOT
) ? SF_LOOP_NONE
: SF_LOOP_FORWARD
;
1693 psf
->loop_info
->num_beats
= bc
.numBeats
;
1695 /* Can always be recalculated from other known fields. */
1696 psf
->loop_info
->bpm
= (1.0 / psf
->sf
.frames
) * psf
->sf
.samplerate
1697 * ((bc
.numBeats
* 4.0) / bc
.sigDenominator
) * 60.0 ;
1698 psf
->loop_info
->root_key
= bc
.rootNote
;
1700 if (count
< datasize
)
1701 psf_binheader_readf (psf
, "j", datasize
- count
) ;
1704 } /* aiff_read_basc_chunk */
1708 aiff_read_chanmap (SF_PRIVATE
* psf
, unsigned dword
)
1709 { const AIFF_CAF_CHANNEL_MAP
* map_info
;
1710 unsigned channel_bitmap
, channel_decriptions
, bytesread
;
1713 bytesread
= psf_binheader_readf (psf
, "444", &layout_tag
, &channel_bitmap
, &channel_decriptions
) ;
1715 if ((map_info
= aiff_caf_of_channel_layout_tag (layout_tag
)) == NULL
)
1718 psf_log_printf (psf
, " Tag : %x\n", layout_tag
) ;
1720 psf_log_printf (psf
, " Layout : %s\n", map_info
->name
) ;
1722 if (bytesread
< dword
)
1723 psf_binheader_readf (psf
, "j", dword
- bytesread
) ;
1725 if (map_info
->channel_map
!= NULL
)
1726 { size_t chanmap_size
= psf
->sf
.channels
* sizeof (psf
->channel_map
[0]) ;
1728 free (psf
->channel_map
) ;
1730 if ((psf
->channel_map
= malloc (chanmap_size
)) == NULL
)
1731 return SFE_MALLOC_FAILED
;
1733 memcpy (psf
->channel_map
, map_info
->channel_map
, chanmap_size
) ;
1737 } /* aiff_read_chanmap */