2 ** Copyright (C) 1999-2015 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 { sf_count_t comm_offset ;
189 sf_count_t ssnd_offset ;
193 MARK_ID_POS *markstr ;
196 /*------------------------------------------------------------------------------
197 * Private static functions.
200 static int aiff_close (SF_PRIVATE *psf) ;
202 static int tenbytefloat2int (unsigned char *bytes) ;
203 static void uint2tenbytefloat (unsigned int num, unsigned char *bytes) ;
205 static int aiff_read_comm_chunk (SF_PRIVATE *psf, COMM_CHUNK *comm_fmt) ;
207 static int aiff_read_header (SF_PRIVATE *psf, COMM_CHUNK *comm_fmt) ;
209 static int aiff_write_header (SF_PRIVATE *psf, int calc_length) ;
210 static int aiff_write_tailer (SF_PRIVATE *psf) ;
211 static void aiff_write_strings (SF_PRIVATE *psf, int location) ;
213 static int aiff_command (SF_PRIVATE *psf, int command, void *data, int datasize) ;
215 static const char *get_loop_mode_str (short mode) ;
217 static short get_loop_mode (short mode) ;
219 static int aiff_read_basc_chunk (SF_PRIVATE * psf, int) ;
221 static int aiff_read_chanmap (SF_PRIVATE * psf, unsigned dword) ;
223 static unsigned int marker_to_position (const MARK_ID_POS *m, unsigned short n, int marksize) ;
225 static int aiff_set_chunk (SF_PRIVATE *psf, const SF_CHUNK_INFO * chunk_info) ;
226 static SF_CHUNK_ITERATOR * aiff_next_chunk_iterator (SF_PRIVATE *psf, SF_CHUNK_ITERATOR * iterator) ;
227 static int aiff_get_chunk_size (SF_PRIVATE *psf, const SF_CHUNK_ITERATOR * iterator, SF_CHUNK_INFO * chunk_info) ;
228 static int aiff_get_chunk_data (SF_PRIVATE *psf, const SF_CHUNK_ITERATOR * iterator, SF_CHUNK_INFO * chunk_info) ;
230 /*------------------------------------------------------------------------------
235 aiff_open (SF_PRIVATE *psf)
236 { COMM_CHUNK comm_fmt ;
237 int error, subformat ;
239 memset (&comm_fmt, 0, sizeof (comm_fmt)) ;
241 subformat = SF_CODEC (psf->sf.format) ;
243 if ((psf->container_data = calloc (1, sizeof (AIFF_PRIVATE))) == NULL)
244 return SFE_MALLOC_FAILED ;
246 if (psf->file.mode == SFM_READ || (psf->file.mode == SFM_RDWR && psf->filelength > 0))
247 { if ((error = aiff_read_header (psf, &comm_fmt)))
250 psf->next_chunk_iterator = aiff_next_chunk_iterator ;
251 psf->get_chunk_size = aiff_get_chunk_size ;
252 psf->get_chunk_data = aiff_get_chunk_data ;
254 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
257 if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
259 return SFE_NO_PIPE_WRITE ;
261 if ((SF_CONTAINER (psf->sf.format)) != SF_FORMAT_AIFF)
262 return SFE_BAD_OPEN_FORMAT ;
264 if (psf->file.mode == SFM_WRITE && (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE))
265 { if ((psf->peak_info = peak_info_calloc (psf->sf.channels)) == NULL)
266 return SFE_MALLOC_FAILED ;
267 psf->peak_info->peak_loc = SF_PEAK_START ;
270 if (psf->file.mode != SFM_RDWR || psf->filelength < 40)
271 { psf->filelength = 0 ;
272 psf->datalength = 0 ;
273 psf->dataoffset = 0 ;
277 psf->strings.flags = SF_STR_ALLOW_START | SF_STR_ALLOW_END ;
279 if ((error = aiff_write_header (psf, SF_FALSE)))
282 psf->write_header = aiff_write_header ;
283 psf->set_chunk = aiff_set_chunk ;
286 psf->container_close = aiff_close ;
287 psf->command = aiff_command ;
289 switch (SF_CODEC (psf->sf.format))
290 { case SF_FORMAT_PCM_U8 :
291 error = pcm_init (psf) ;
294 case SF_FORMAT_PCM_S8 :
295 error = pcm_init (psf) ;
298 case SF_FORMAT_PCM_16 :
299 case SF_FORMAT_PCM_24 :
300 case SF_FORMAT_PCM_32 :
301 error = pcm_init (psf) ;
304 case SF_FORMAT_ULAW :
305 error = ulaw_init (psf) ;
308 case SF_FORMAT_ALAW :
309 error = alaw_init (psf) ;
312 /* Lite remove start */
313 case SF_FORMAT_FLOAT :
314 error = float32_init (psf) ;
317 case SF_FORMAT_DOUBLE :
318 error = double64_init (psf) ;
321 case SF_FORMAT_DWVW_12 :
322 error = dwvw_init (psf, 12) ;
325 case SF_FORMAT_DWVW_16 :
326 error = dwvw_init (psf, 16) ;
329 case SF_FORMAT_DWVW_24 :
330 error = dwvw_init (psf, 24) ;
333 case SF_FORMAT_DWVW_N :
334 if (psf->file.mode != SFM_READ)
335 { error = SFE_DWVW_BAD_BITWIDTH ;
338 if (comm_fmt.sampleSize >= 8 && comm_fmt.sampleSize < 24)
339 { error = dwvw_init (psf, comm_fmt.sampleSize) ;
340 psf->sf.frames = comm_fmt.numSampleFrames ;
343 psf_log_printf (psf, "AIFC/DWVW : Bad bitwidth %d\n", comm_fmt.sampleSize) ;
344 error = SFE_DWVW_BAD_BITWIDTH ;
347 case SF_FORMAT_IMA_ADPCM :
349 ** IMA ADPCM encoded AIFF files always have a block length
350 ** of 34 which decodes to 64 samples.
352 error = aiff_ima_init (psf, AIFC_IMA4_BLOCK_LEN, AIFC_IMA4_SAMPLES_PER_BLOCK) ;
354 /* Lite remove end */
356 case SF_FORMAT_GSM610 :
357 error = gsm610_init (psf) ;
360 default : return SFE_UNIMPLEMENTED ;
367 /*==========================================================================================
368 ** Private functions.
371 /* This function ought to check size */
373 marker_to_position (const MARK_ID_POS *m, unsigned short n, int marksize)
376 for (i = 0 ; i < marksize ; i++)
377 if (m [i].markerID == n)
378 return m [i].position ;
380 } /* marker_to_position */
383 aiff_read_header (SF_PRIVATE *psf, COMM_CHUNK *comm_fmt)
384 { SSND_CHUNK ssnd_fmt ;
385 AIFF_PRIVATE *paiff ;
387 unsigned chunk_size = 0, FORMsize, SSNDsize, bytesread ;
388 int k, found_chunk = 0, done = 0, error = 0 ;
390 int instr_found = 0, mark_found = 0, mark_count = 0 ;
392 if (psf->filelength > SF_PLATFORM_S64 (0xffffffff))
393 psf_log_printf (psf, "Warning : filelength > 0xffffffff. This is bad!!!!\n") ;
395 if ((paiff = psf->container_data) == NULL)
396 return SFE_INTERNAL ;
398 paiff->comm_offset = 0 ;
399 paiff->ssnd_offset = 0 ;
401 /* Set position to start of file to begin reading header. */
402 psf_binheader_readf (psf, "p", 0) ;
404 memset (comm_fmt, 0, sizeof (COMM_CHUNK)) ;
406 /* Until recently AIF* file were all BIG endian. */
407 psf->endian = SF_ENDIAN_BIG ;
409 /* AIFF files can apparently have their chunks in any order. However, they
410 ** must have a FORM chunk. Approach here is to read all the chunks one by
411 ** one and then check for the mandatory chunks at the end.
415 size_t jump = chunk_size & 1 ;
417 marker = chunk_size = 0 ;
418 psf_binheader_readf (psf, "Ejm4", jump, &marker, &chunk_size) ;
420 { sf_count_t pos = psf_ftell (psf) ;
421 psf_log_printf (psf, "Have 0 marker at position %D (0x%x).\n", pos, pos) ;
425 if (psf->file.mode == SFM_RDWR && (found_chunk & HAVE_SSND))
426 return SFE_AIFF_RW_SSND_NOT_LAST ;
428 psf_store_read_chunk_u32 (&psf->rchunks, marker, psf_ftell (psf), chunk_size) ;
433 return SFE_AIFF_NO_FORM ;
435 FORMsize = chunk_size ;
437 found_chunk |= HAVE_FORM ;
438 psf_binheader_readf (psf, "m", &marker) ;
442 found_chunk |= (marker == AIFC_MARKER) ? (HAVE_AIFC | HAVE_AIFF) : HAVE_AIFF ;
448 if (psf->fileoffset > 0 && psf->filelength > FORMsize + 8)
449 { /* Set file length. */
450 psf->filelength = FORMsize + 8 ;
451 psf_log_printf (psf, "FORM : %u\n %M\n", FORMsize, marker) ;
453 else if (FORMsize != psf->filelength - 2 * SIGNED_SIZEOF (chunk_size))
454 { chunk_size = psf->filelength - 2 * sizeof (chunk_size) ;
455 psf_log_printf (psf, "FORM : %u (should be %u)\n %M\n", FORMsize, chunk_size, marker) ;
456 FORMsize = chunk_size ;
459 psf_log_printf (psf, "FORM : %u\n %M\n", FORMsize, marker) ;
460 /* Set this to 0, so we don't jump a byte when parsing the next marker. */
466 paiff->comm_offset = psf_ftell (psf) - 8 ;
467 chunk_size += chunk_size & 1 ;
468 comm_fmt->size = chunk_size ;
469 if ((error = aiff_read_comm_chunk (psf, comm_fmt)) != 0)
472 found_chunk |= HAVE_COMM ;
476 /* Must have COMM chunk before PEAK chunk. */
477 if ((found_chunk & (HAVE_FORM | HAVE_AIFF | HAVE_COMM)) != (HAVE_FORM | HAVE_AIFF | HAVE_COMM))
478 return SFE_AIFF_PEAK_B4_COMM ;
480 psf_log_printf (psf, "%M : %d\n", marker, chunk_size) ;
481 if (chunk_size != AIFF_PEAK_CHUNK_SIZE (psf->sf.channels))
482 { psf_binheader_readf (psf, "j", chunk_size) ;
483 psf_log_printf (psf, "*** File PEAK chunk too big.\n") ;
484 return SFE_WAV_BAD_PEAK ;
487 if ((psf->peak_info = peak_info_calloc (psf->sf.channels)) == NULL)
488 return SFE_MALLOC_FAILED ;
490 /* read in rest of PEAK chunk. */
491 psf_binheader_readf (psf, "E44", &(psf->peak_info->version), &(psf->peak_info->timestamp)) ;
493 if (psf->peak_info->version != 1)
494 psf_log_printf (psf, " version : %d *** (should be version 1)\n", psf->peak_info->version) ;
496 psf_log_printf (psf, " version : %d\n", psf->peak_info->version) ;
498 psf_log_printf (psf, " time stamp : %d\n", psf->peak_info->timestamp) ;
499 psf_log_printf (psf, " Ch Position Value\n") ;
502 for (k = 0 ; k < psf->sf.channels ; k++)
504 unsigned int position ;
506 psf_binheader_readf (psf, "Ef4", &value, &position) ;
507 psf->peak_info->peaks [k].value = value ;
508 psf->peak_info->peaks [k].position = position ;
510 snprintf (cptr, sizeof (ubuf.scbuf), " %2d %-12" PRId64 " %g\n",
511 k, psf->peak_info->peaks [k].position, psf->peak_info->peaks [k].value) ;
512 cptr [sizeof (ubuf.scbuf) - 1] = 0 ;
513 psf_log_printf (psf, "%s", cptr) ;
516 psf->peak_info->peak_loc = ((found_chunk & HAVE_SSND) == 0) ? SF_PEAK_START : SF_PEAK_END ;
520 if ((found_chunk & HAVE_AIFC) && (found_chunk & HAVE_FVER) == 0)
521 psf_log_printf (psf, "*** Valid AIFC files should have an FVER chunk.\n") ;
523 paiff->ssnd_offset = psf_ftell (psf) - 8 ;
524 SSNDsize = chunk_size ;
525 psf_binheader_readf (psf, "E44", &(ssnd_fmt.offset), &(ssnd_fmt.blocksize)) ;
527 psf->datalength = SSNDsize - sizeof (ssnd_fmt) ;
528 psf->dataoffset = psf_ftell (psf) ;
530 if (psf->datalength > psf->filelength - psf->dataoffset || psf->datalength < 0)
531 { psf_log_printf (psf, " SSND : %u (should be %D)\n", SSNDsize, psf->filelength - psf->dataoffset + sizeof (SSND_CHUNK)) ;
532 psf->datalength = psf->filelength - psf->dataoffset ;
535 psf_log_printf (psf, " SSND : %u\n", SSNDsize) ;
537 if (ssnd_fmt.offset == 0 || psf->dataoffset + ssnd_fmt.offset == ssnd_fmt.blocksize)
538 { psf_log_printf (psf, " Offset : %u\n", ssnd_fmt.offset) ;
539 psf_log_printf (psf, " Block Size : %u\n", ssnd_fmt.blocksize) ;
541 psf->dataoffset += ssnd_fmt.offset ;
542 psf->datalength -= ssnd_fmt.offset ;
545 { psf_log_printf (psf, " Offset : %u\n", ssnd_fmt.offset) ;
546 psf_log_printf (psf, " Block Size : %u ???\n", ssnd_fmt.blocksize) ;
547 psf->dataoffset += ssnd_fmt.offset ;
548 psf->datalength -= ssnd_fmt.offset ;
551 /* Only set dataend if there really is data at the end. */
552 if (psf->datalength + psf->dataoffset < psf->filelength)
553 psf->dataend = psf->datalength + psf->dataoffset ;
555 found_chunk |= HAVE_SSND ;
557 if (! psf->sf.seekable)
560 /* Seek to end of SSND chunk. */
561 psf_fseek (psf, psf->dataoffset + psf->datalength, SEEK_SET) ;
567 if (chunk_size >= SIGNED_SIZEOF (ubuf.scbuf))
568 { psf_log_printf (psf, " %M : %d (too big)\n", marker, chunk_size) ;
569 return SFE_INTERNAL ;
573 psf_binheader_readf (psf, "b", cptr, chunk_size + (chunk_size & 1)) ;
574 cptr [chunk_size] = 0 ;
576 psf_sanitize_string (cptr, chunk_size) ;
578 psf_log_printf (psf, " %M : %s\n", marker, cptr) ;
579 psf_store_string (psf, SF_STR_COPYRIGHT, cptr) ;
580 chunk_size += chunk_size & 1 ;
586 if (chunk_size >= SIGNED_SIZEOF (ubuf.scbuf) - 1)
587 { psf_log_printf (psf, " %M : %d (too big)\n", marker, chunk_size) ;
588 return SFE_INTERNAL ;
592 psf_binheader_readf (psf, "b", cptr, chunk_size + (chunk_size & 1)) ;
593 cptr [chunk_size] = 0 ;
594 psf_log_printf (psf, " %M : %s\n", marker, cptr) ;
595 psf_store_string (psf, SF_STR_ARTIST, cptr) ;
596 chunk_size += chunk_size & 1 ;
600 { unsigned short count, id, len ;
601 unsigned int timestamp, bytes ;
606 bytes -= psf_binheader_readf (psf, "E2", &count) ;
607 psf_log_printf (psf, " %M : %d\n count : %d\n", marker, chunk_size, count) ;
609 for (k = 0 ; k < count ; k++)
610 { bytes -= psf_binheader_readf (psf, "E422", ×tamp, &id, &len) ;
611 psf_log_printf (psf, " time : 0x%x\n marker : %x\n length : %d\n", timestamp, id, len) ;
613 if (len + 1 > SIGNED_SIZEOF (ubuf.scbuf))
614 { psf_log_printf (psf, "\nError : string length (%d) too big.\n", len) ;
615 return SFE_INTERNAL ;
619 bytes -= psf_binheader_readf (psf, "b", cptr, len) ;
621 psf_log_printf (psf, " string : %s\n", cptr) ;
625 psf_binheader_readf (psf, "j", bytes) ;
630 { unsigned appl_marker ;
634 if (chunk_size >= SIGNED_SIZEOF (ubuf.scbuf) - 1)
635 { psf_log_printf (psf, " %M : %u (too big, skipping)\n", marker, chunk_size) ;
636 psf_binheader_readf (psf, "j", chunk_size + (chunk_size & 1)) ;
641 { psf_log_printf (psf, " %M : %d (too small, skipping)\n", marker, chunk_size) ;
642 psf_binheader_readf (psf, "j", chunk_size + (chunk_size & 1)) ;
647 psf_binheader_readf (psf, "mb", &appl_marker, cptr, chunk_size + (chunk_size & 1) - 4) ;
648 cptr [chunk_size] = 0 ;
650 for (k = 0 ; k < (int) chunk_size ; k++)
651 if (! psf_isprint (cptr [k]))
656 psf_log_printf (psf, " %M : %d\n AppSig : %M\n Name : %s\n", marker, chunk_size, appl_marker, cptr) ;
657 psf_store_string (psf, SF_STR_SOFTWARE, cptr) ;
658 chunk_size += chunk_size & 1 ;
665 if (chunk_size >= SIGNED_SIZEOF (ubuf.scbuf) - 2)
666 { psf_log_printf (psf, " %M : %d (too big)\n", marker, chunk_size) ;
667 return SFE_INTERNAL ;
671 psf_binheader_readf (psf, "b", cptr, chunk_size + (chunk_size & 1)) ;
672 cptr [chunk_size] = 0 ;
673 psf_log_printf (psf, " %M : %s\n", marker, cptr) ;
674 psf_store_string (psf, SF_STR_TITLE, cptr) ;
675 chunk_size += chunk_size & 1 ;
681 if (chunk_size >= SIGNED_SIZEOF (ubuf.scbuf) - 2)
682 { psf_log_printf (psf, " %M : %d (too big)\n", marker, chunk_size) ;
683 return SFE_INTERNAL ;
687 psf_binheader_readf (psf, "b", cptr, chunk_size + (chunk_size & 1)) ;
688 cptr [chunk_size] = 0 ;
689 psf_log_printf (psf, " %M : %s\n", marker, cptr) ;
690 psf_store_string (psf, SF_STR_COMMENT, cptr) ;
691 chunk_size += chunk_size & 1 ;
695 if (chunk_size != SIZEOF_INST_CHUNK)
696 { psf_log_printf (psf, " %M : %d (should be %d)\n", marker, chunk_size, SIZEOF_INST_CHUNK) ;
697 psf_binheader_readf (psf, "j", chunk_size) ;
700 psf_log_printf (psf, " %M : %d\n", marker, chunk_size) ;
701 { unsigned char bytes [6] ;
704 if (psf->instrument == NULL && (psf->instrument = psf_instrument_alloc ()) == NULL)
705 return SFE_MALLOC_FAILED ;
707 psf_binheader_readf (psf, "b", bytes, 6) ;
708 psf_log_printf (psf, " Base Note : %u\n Detune : %u\n"
709 " Low Note : %u\n High Note : %u\n"
710 " Low Vel. : %u\n High Vel. : %u\n",
711 bytes [0], bytes [1], bytes [2], bytes [3], bytes [4], bytes [5]) ;
712 psf->instrument->basenote = bytes [0] ;
713 psf->instrument->detune = bytes [1] ;
714 psf->instrument->key_lo = bytes [2] ;
715 psf->instrument->key_hi = bytes [3] ;
716 psf->instrument->velocity_lo = bytes [4] ;
717 psf->instrument->velocity_hi = bytes [5] ;
718 psf_binheader_readf (psf, "E2", &gain) ;
719 psf->instrument->gain = gain ;
720 psf_log_printf (psf, " Gain (dB) : %d\n", gain) ;
722 { short mode ; /* 0 - no loop, 1 - forward looping, 2 - backward looping */
723 const char *loop_mode ;
724 unsigned short begin, end ;
726 psf_binheader_readf (psf, "E222", &mode, &begin, &end) ;
727 loop_mode = get_loop_mode_str (mode) ;
728 mode = get_loop_mode (mode) ;
729 if (mode == SF_LOOP_NONE)
730 { psf->instrument->loop_count = 0 ;
731 psf->instrument->loops [0].mode = SF_LOOP_NONE ;
734 { psf->instrument->loop_count = 1 ;
735 psf->instrument->loops [0].mode = SF_LOOP_FORWARD ;
736 psf->instrument->loops [0].start = begin ;
737 psf->instrument->loops [0].end = end ;
738 psf->instrument->loops [0].count = 0 ;
740 psf_log_printf (psf, " Sustain\n mode : %d => %s\n begin : %u\n end : %u\n",
741 mode, loop_mode, begin, end) ;
742 psf_binheader_readf (psf, "E222", &mode, &begin, &end) ;
743 loop_mode = get_loop_mode_str (mode) ;
744 mode = get_loop_mode (mode) ;
745 if (mode == SF_LOOP_NONE)
746 psf->instrument->loops [1].mode = SF_LOOP_NONE ;
748 { psf->instrument->loop_count += 1 ;
749 psf->instrument->loops [1].mode = SF_LOOP_FORWARD ;
750 psf->instrument->loops [1].start = begin ;
751 psf->instrument->loops [1].end = end ;
752 psf->instrument->loops [1].count = 0 ;
754 psf_log_printf (psf, " Release\n mode : %d => %s\n begin : %u\n end : %u\n",
755 mode, loop_mode, begin, end) ;
761 psf_log_printf (psf, " basc : %u\n", chunk_size) ;
763 if ((error = aiff_read_basc_chunk (psf, chunk_size)))
768 psf_log_printf (psf, " %M : %d\n", marker, chunk_size) ;
769 { unsigned short mark_id, n = 0 ;
770 unsigned int position ;
772 bytesread = psf_binheader_readf (psf, "E2", &n) ;
774 psf_log_printf (psf, " Count : %d\n", mark_count) ;
775 if (paiff->markstr != NULL)
776 { psf_log_printf (psf, "*** Second MARK chunk found. Throwing away the first.\n") ;
777 free (paiff->markstr) ;
779 paiff->markstr = calloc (mark_count, sizeof (MARK_ID_POS)) ;
780 if (paiff->markstr == NULL)
781 return SFE_MALLOC_FAILED ;
783 for (n = 0 ; n < mark_count && bytesread < chunk_size ; n++)
784 { unsigned int pstr_len ;
787 bytesread += psf_binheader_readf (psf, "E241", &mark_id, &position, &ch) ;
788 psf_log_printf (psf, " Mark ID : %u\n Position : %u\n", mark_id, position) ;
790 pstr_len = (ch & 1) ? ch : ch + 1 ;
792 if (pstr_len < sizeof (ubuf.scbuf) - 1)
793 { bytesread += psf_binheader_readf (psf, "b", ubuf.scbuf, pstr_len) ;
794 ubuf.scbuf [pstr_len] = 0 ;
797 { unsigned int read_len = pstr_len - (sizeof (ubuf.scbuf) - 1) ;
798 bytesread += psf_binheader_readf (psf, "bj", ubuf.scbuf, read_len, pstr_len - read_len) ;
799 ubuf.scbuf [sizeof (ubuf.scbuf) - 1] = 0 ;
802 psf_log_printf (psf, " Name : %s\n", ubuf.scbuf) ;
804 paiff->markstr [n].markerID = mark_id ;
805 paiff->markstr [n].position = position ;
807 ** TODO if ubuf.scbuf is equal to
808 ** either Beg_loop, Beg loop or beg loop and spam
809 ** if (psf->instrument == NULL && (psf->instrument = psf_instrument_alloc ()) == NULL)
810 ** return SFE_MALLOC_FAILED ;
815 psf_binheader_readf (psf, "j", chunk_size - bytesread) ;
819 found_chunk |= HAVE_FVER ;
820 /* Fall through to next case. */
823 psf_log_printf (psf, " %M : %d\n", marker, chunk_size) ;
824 psf_binheader_readf (psf, "j", chunk_size) ;
828 /* Fix for broken AIFC files with incorrect COMM chunk length. */
829 chunk_size = (chunk_size >> 24) - 3 ;
830 psf_log_printf (psf, " %M : %d\n", marker, chunk_size) ;
831 psf_binheader_readf (psf, "j", make_size_t (chunk_size)) ;
836 { psf_log_printf (psf, " %M : %d (should be >= 12)\n", marker, chunk_size) ;
837 psf_binheader_readf (psf, "j", chunk_size) ;
841 psf_log_printf (psf, " %M : %d\n", marker, chunk_size) ;
843 if ((error = aiff_read_chanmap (psf, chunk_size)))
848 if (chunk_size >= 0xffff0000)
850 psf_log_printf (psf, "*** Unknown chunk marker (%X) at position %D with length %u. Exiting parser.\n", marker, psf_ftell (psf) - 8, chunk_size) ;
854 if (psf_isprint ((marker >> 24) & 0xFF) && psf_isprint ((marker >> 16) & 0xFF)
855 && psf_isprint ((marker >> 8) & 0xFF) && psf_isprint (marker & 0xFF))
856 { psf_log_printf (psf, " %M : %u (unknown marker)\n", marker, chunk_size) ;
858 psf_binheader_readf (psf, "j", chunk_size) ;
862 if (psf_ftell (psf) & 0x03)
863 { psf_log_printf (psf, " Unknown chunk marker at position %D. Resynching.\n", psf_ftell (psf) - 8) ;
864 psf_binheader_readf (psf, "j", -3) ;
867 psf_log_printf (psf, "*** Unknown chunk marker %X at position %D. Exiting parser.\n", marker, psf_ftell (psf)) ;
870 } ; /* switch (marker) */
872 if (chunk_size >= psf->filelength)
873 { psf_log_printf (psf, "*** Chunk size %u > file length %D. Exiting parser.\n", chunk_size, psf->filelength) ;
877 if ((! psf->sf.seekable) && (found_chunk & HAVE_SSND))
880 if (psf_ftell (psf) >= psf->filelength - (2 * SIGNED_SIZEOF (int32_t)))
884 if (instr_found && mark_found)
887 for (j = 0 ; j < psf->instrument->loop_count ; j ++)
888 { if (j < ARRAY_LEN (psf->instrument->loops))
889 { psf->instrument->loops [j].start = marker_to_position (paiff->markstr, psf->instrument->loops [j].start, mark_count) ;
890 psf->instrument->loops [j].end = marker_to_position (paiff->markstr, psf->instrument->loops [j].end, mark_count) ;
891 psf->instrument->loops [j].mode = SF_LOOP_FORWARD ;
896 if (psf->sf.channels < 1)
897 return SFE_CHANNEL_COUNT_ZERO ;
899 if (psf->sf.channels >= SF_MAX_CHANNELS)
900 return SFE_CHANNEL_COUNT ;
902 if (! (found_chunk & HAVE_FORM))
903 return SFE_AIFF_NO_FORM ;
905 if (! (found_chunk & HAVE_AIFF))
906 return SFE_AIFF_COMM_NO_FORM ;
908 if (! (found_chunk & HAVE_COMM))
909 return SFE_AIFF_SSND_NO_COMM ;
911 if (! psf->dataoffset)
912 return SFE_AIFF_NO_DATA ;
915 } /* aiff_read_header */
918 aiff_close (SF_PRIVATE *psf)
919 { AIFF_PRIVATE *paiff = psf->container_data ;
921 if (paiff != NULL && paiff->markstr != NULL)
922 { free (paiff->markstr) ;
923 paiff->markstr = NULL ;
926 if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
927 { aiff_write_tailer (psf) ;
928 aiff_write_header (psf, SF_TRUE) ;
935 aiff_read_comm_chunk (SF_PRIVATE *psf, COMM_CHUNK *comm_fmt)
937 int subformat, samplerate ;
941 /* The COMM chunk has an int aligned to an odd word boundary. Some
942 ** procesors are not able to deal with this (ie bus fault) so we have
943 ** to take special care.
946 psf_binheader_readf (psf, "E242b", &(comm_fmt->numChannels), &(comm_fmt->numSampleFrames),
947 &(comm_fmt->sampleSize), &(comm_fmt->sampleRate), SIGNED_SIZEOF (comm_fmt->sampleRate)) ;
949 if (comm_fmt->size > 0x10000 && (comm_fmt->size & 0xffff) == 0)
950 { psf_log_printf (psf, " COMM : %d (0x%x) *** should be ", comm_fmt->size, comm_fmt->size) ;
951 comm_fmt->size = ENDSWAP_32 (comm_fmt->size) ;
952 psf_log_printf (psf, "%d (0x%x)\n", comm_fmt->size, comm_fmt->size) ;
955 psf_log_printf (psf, " COMM : %d\n", comm_fmt->size) ;
957 if (comm_fmt->size == SIZEOF_AIFF_COMM)
958 comm_fmt->encoding = NONE_MARKER ;
959 else if (comm_fmt->size == SIZEOF_AIFC_COMM_MIN)
960 psf_binheader_readf (psf, "Em", &(comm_fmt->encoding)) ;
961 else if (comm_fmt->size >= SIZEOF_AIFC_COMM)
962 { unsigned char encoding_len ;
965 psf_binheader_readf (psf, "Em1", &(comm_fmt->encoding), &encoding_len) ;
967 comm_fmt->size = SF_MIN (sizeof (ubuf.scbuf), make_size_t (comm_fmt->size)) ;
968 memset (ubuf.scbuf, 0, comm_fmt->size) ;
969 read_len = comm_fmt->size - SIZEOF_AIFC_COMM + 1 ;
970 psf_binheader_readf (psf, "b", ubuf.scbuf, read_len) ;
971 ubuf.scbuf [read_len + 1] = 0 ;
974 samplerate = tenbytefloat2int (comm_fmt->sampleRate) ;
976 psf_log_printf (psf, " Sample Rate : %d\n", samplerate) ;
977 psf_log_printf (psf, " Frames : %u%s\n", comm_fmt->numSampleFrames, (comm_fmt->numSampleFrames == 0 && psf->filelength > 104) ? " (Should not be 0)" : "") ;
978 psf_log_printf (psf, " Channels : %d\n", comm_fmt->numChannels) ;
980 /* Found some broken 'fl32' files with comm.samplesize == 16. Fix it here. */
981 if ((comm_fmt->encoding == fl32_MARKER || comm_fmt->encoding == FL32_MARKER) && comm_fmt->sampleSize != 32)
982 { psf_log_printf (psf, " Sample Size : %d (should be 32)\n", comm_fmt->sampleSize) ;
983 comm_fmt->sampleSize = 32 ;
985 else if ((comm_fmt->encoding == fl64_MARKER || comm_fmt->encoding == FL64_MARKER) && comm_fmt->sampleSize != 64)
986 { psf_log_printf (psf, " Sample Size : %d (should be 64)\n", comm_fmt->sampleSize) ;
987 comm_fmt->sampleSize = 64 ;
990 psf_log_printf (psf, " Sample Size : %d\n", comm_fmt->sampleSize) ;
992 subformat = s_bitwidth_to_subformat (comm_fmt->sampleSize) ;
994 psf->sf.samplerate = samplerate ;
995 psf->sf.frames = comm_fmt->numSampleFrames ;
996 psf->sf.channels = comm_fmt->numChannels ;
997 psf->bytewidth = BITWIDTH2BYTES (comm_fmt->sampleSize) ;
999 psf->endian = SF_ENDIAN_BIG ;
1001 switch (comm_fmt->encoding)
1002 { case NONE_MARKER :
1003 psf->sf.format = (SF_FORMAT_AIFF | subformat) ;
1009 psf->sf.format = (SF_ENDIAN_BIG | SF_FORMAT_AIFF | subformat) ;
1015 psf->endian = SF_ENDIAN_LITTLE ;
1016 psf->sf.format = (SF_ENDIAN_LITTLE | SF_FORMAT_AIFF | subformat) ;
1021 psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_FLOAT) ;
1026 psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_ULAW) ;
1031 psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_ALAW) ;
1036 psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_DOUBLE) ;
1040 psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_PCM_U8) ;
1044 psf->sf.format = SF_FORMAT_AIFF ;
1045 switch (comm_fmt->sampleSize)
1047 psf->sf.format |= SF_FORMAT_DWVW_12 ;
1050 psf->sf.format |= SF_FORMAT_DWVW_16 ;
1053 psf->sf.format |= SF_FORMAT_DWVW_24 ;
1057 psf->sf.format |= SF_FORMAT_DWVW_N ;
1063 psf->sf.format = SF_FORMAT_AIFF ;
1064 psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_GSM610) ;
1069 psf->endian = SF_ENDIAN_BIG ;
1070 psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_IMA_ADPCM) ;
1074 psf_log_printf (psf, "AIFC : Unimplemented format : %M\n", comm_fmt->encoding) ;
1075 return SFE_UNIMPLEMENTED ;
1078 if (! ubuf.scbuf [0])
1079 psf_log_printf (psf, " Encoding : %M\n", comm_fmt->encoding) ;
1081 psf_log_printf (psf, " Encoding : %M => %s\n", comm_fmt->encoding, ubuf.scbuf) ;
1084 } /* aiff_read_comm_chunk */
1087 /*==========================================================================================
1091 aiff_rewrite_header (SF_PRIVATE *psf)
1093 /* Assuming here that the header has already been written and just
1094 ** needs to be corrected for new data length. That means that we
1095 ** only change the length fields of the FORM and SSND chunks ;
1096 ** everything else can be skipped over.
1098 int k, ch, comm_size, comm_frames ;
1100 psf_fseek (psf, 0, SEEK_SET) ;
1101 psf_fread (psf->header, psf->dataoffset, 1, psf) ;
1103 psf->headindex = 0 ;
1106 psf_binheader_writef (psf, "Etm8", FORM_MARKER, psf->filelength - 8) ;
1109 if ((k = psf_find_read_chunk_m32 (&psf->rchunks, COMM_MARKER)) >= 0)
1110 { psf->headindex = psf->rchunks.chunks [k].offset - 8 ;
1111 comm_frames = psf->sf.frames ;
1112 comm_size = psf->rchunks.chunks [k].len ;
1113 psf_binheader_writef (psf, "Em42t4", COMM_MARKER, comm_size, psf->sf.channels, comm_frames) ;
1117 if ((k = psf_find_read_chunk_m32 (&psf->rchunks, PEAK_MARKER)) >= 0)
1118 { psf->headindex = psf->rchunks.chunks [k].offset - 8 ;
1119 psf_binheader_writef (psf, "Em4", PEAK_MARKER, AIFF_PEAK_CHUNK_SIZE (psf->sf.channels)) ;
1120 psf_binheader_writef (psf, "E44", 1, time (NULL)) ;
1121 for (ch = 0 ; ch < psf->sf.channels ; ch++)
1122 psf_binheader_writef (psf, "Eft8", (float) psf->peak_info->peaks [ch].value, psf->peak_info->peaks [ch].position) ;
1127 if ((k = psf_find_read_chunk_m32 (&psf->rchunks, SSND_MARKER)) >= 0)
1128 { psf->headindex = psf->rchunks.chunks [k].offset - 8 ;
1129 psf_binheader_writef (psf, "Etm8", SSND_MARKER, psf->datalength + SIZEOF_SSND_CHUNK) ;
1132 /* Header mangling complete so write it out. */
1133 psf_fseek (psf, 0, SEEK_SET) ;
1134 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
1137 } /* aiff_rewrite_header */
1140 aiff_write_header (SF_PRIVATE *psf, int calc_length)
1141 { sf_count_t current ;
1142 AIFF_PRIVATE *paiff ;
1143 unsigned char comm_sample_rate [10], comm_zero_bytes [2] = { 0, 0 } ;
1144 unsigned int comm_type, comm_size, comm_encoding, comm_frames = 0, uk ;
1145 int k, endian, has_data = SF_FALSE ;
1148 if ((paiff = psf->container_data) == NULL)
1149 return SFE_INTERNAL ;
1151 current = psf_ftell (psf) ;
1153 if (current > psf->dataoffset)
1154 has_data = SF_TRUE ;
1157 { psf->filelength = psf_get_filelen (psf) ;
1159 psf->datalength = psf->filelength - psf->dataoffset ;
1161 psf->datalength -= psf->filelength - psf->dataend ;
1163 if (psf->bytewidth > 0)
1164 psf->sf.frames = psf->datalength / (psf->bytewidth * psf->sf.channels) ;
1167 if (psf->file.mode == SFM_RDWR && psf->dataoffset > 0 && psf->rchunks.count > 0)
1168 { aiff_rewrite_header (psf) ;
1170 psf_fseek (psf, current, SEEK_SET) ;
1174 endian = SF_ENDIAN (psf->sf.format) ;
1175 if (CPU_IS_LITTLE_ENDIAN && endian == SF_ENDIAN_CPU)
1176 endian = SF_ENDIAN_LITTLE ;
1178 /* Standard value here. */
1179 bit_width = psf->bytewidth * 8 ;
1180 comm_frames = (psf->sf.frames > 0xFFFFFFFF) ? 0xFFFFFFFF : psf->sf.frames ;
1182 switch (SF_CODEC (psf->sf.format) | endian)
1183 { case SF_FORMAT_PCM_S8 | SF_ENDIAN_BIG :
1184 psf->endian = SF_ENDIAN_BIG ;
1185 comm_type = AIFC_MARKER ;
1186 comm_size = SIZEOF_AIFC_COMM ;
1187 comm_encoding = twos_MARKER ;
1190 case SF_FORMAT_PCM_S8 | SF_ENDIAN_LITTLE :
1191 psf->endian = SF_ENDIAN_LITTLE ;
1192 comm_type = AIFC_MARKER ;
1193 comm_size = SIZEOF_AIFC_COMM ;
1194 comm_encoding = sowt_MARKER ;
1197 case SF_FORMAT_PCM_16 | SF_ENDIAN_BIG :
1198 psf->endian = SF_ENDIAN_BIG ;
1199 comm_type = AIFC_MARKER ;
1200 comm_size = SIZEOF_AIFC_COMM ;
1201 comm_encoding = twos_MARKER ;
1204 case SF_FORMAT_PCM_16 | SF_ENDIAN_LITTLE :
1205 psf->endian = SF_ENDIAN_LITTLE ;
1206 comm_type = AIFC_MARKER ;
1207 comm_size = SIZEOF_AIFC_COMM ;
1208 comm_encoding = sowt_MARKER ;
1211 case SF_FORMAT_PCM_24 | SF_ENDIAN_BIG :
1212 psf->endian = SF_ENDIAN_BIG ;
1213 comm_type = AIFC_MARKER ;
1214 comm_size = SIZEOF_AIFC_COMM ;
1215 comm_encoding = in24_MARKER ;
1218 case SF_FORMAT_PCM_24 | SF_ENDIAN_LITTLE :
1219 psf->endian = SF_ENDIAN_LITTLE ;
1220 comm_type = AIFC_MARKER ;
1221 comm_size = SIZEOF_AIFC_COMM ;
1222 comm_encoding = ni24_MARKER ;
1225 case SF_FORMAT_PCM_32 | SF_ENDIAN_BIG :
1226 psf->endian = SF_ENDIAN_BIG ;
1227 comm_type = AIFC_MARKER ;
1228 comm_size = SIZEOF_AIFC_COMM ;
1229 comm_encoding = in32_MARKER ;
1232 case SF_FORMAT_PCM_32 | SF_ENDIAN_LITTLE :
1233 psf->endian = SF_ENDIAN_LITTLE ;
1234 comm_type = AIFC_MARKER ;
1235 comm_size = SIZEOF_AIFC_COMM ;
1236 comm_encoding = ni32_MARKER ;
1239 case SF_FORMAT_PCM_S8 : /* SF_ENDIAN_FILE */
1240 case SF_FORMAT_PCM_16 :
1241 case SF_FORMAT_PCM_24 :
1242 case SF_FORMAT_PCM_32 :
1243 psf->endian = SF_ENDIAN_BIG ;
1244 comm_type = AIFF_MARKER ;
1245 comm_size = SIZEOF_AIFF_COMM ;
1249 case SF_FORMAT_FLOAT : /* Big endian floating point. */
1250 psf->endian = SF_ENDIAN_BIG ;
1251 comm_type = AIFC_MARKER ;
1252 comm_size = SIZEOF_AIFC_COMM ;
1253 comm_encoding = FL32_MARKER ; /* Use 'FL32' because its easier to read. */
1256 case SF_FORMAT_DOUBLE : /* Big endian double precision floating point. */
1257 psf->endian = SF_ENDIAN_BIG ;
1258 comm_type = AIFC_MARKER ;
1259 comm_size = SIZEOF_AIFC_COMM ;
1260 comm_encoding = FL64_MARKER ; /* Use 'FL64' because its easier to read. */
1263 case SF_FORMAT_ULAW :
1264 psf->endian = SF_ENDIAN_BIG ;
1265 comm_type = AIFC_MARKER ;
1266 comm_size = SIZEOF_AIFC_COMM ;
1267 comm_encoding = ulaw_MARKER ;
1270 case SF_FORMAT_ALAW :
1271 psf->endian = SF_ENDIAN_BIG ;
1272 comm_type = AIFC_MARKER ;
1273 comm_size = SIZEOF_AIFC_COMM ;
1274 comm_encoding = alaw_MARKER ;
1277 case SF_FORMAT_PCM_U8 :
1278 psf->endian = SF_ENDIAN_BIG ;
1279 comm_type = AIFC_MARKER ;
1280 comm_size = SIZEOF_AIFC_COMM ;
1281 comm_encoding = raw_MARKER ;
1284 case SF_FORMAT_DWVW_12 :
1285 psf->endian = SF_ENDIAN_BIG ;
1286 comm_type = AIFC_MARKER ;
1287 comm_size = SIZEOF_AIFC_COMM ;
1288 comm_encoding = DWVW_MARKER ;
1290 /* Override standard value here.*/
1294 case SF_FORMAT_DWVW_16 :
1295 psf->endian = SF_ENDIAN_BIG ;
1296 comm_type = AIFC_MARKER ;
1297 comm_size = SIZEOF_AIFC_COMM ;
1298 comm_encoding = DWVW_MARKER ;
1300 /* Override standard value here.*/
1304 case SF_FORMAT_DWVW_24 :
1305 psf->endian = SF_ENDIAN_BIG ;
1306 comm_type = AIFC_MARKER ;
1307 comm_size = SIZEOF_AIFC_COMM ;
1308 comm_encoding = DWVW_MARKER ;
1310 /* Override standard value here.*/
1314 case SF_FORMAT_GSM610 :
1315 psf->endian = SF_ENDIAN_BIG ;
1316 comm_type = AIFC_MARKER ;
1317 comm_size = SIZEOF_AIFC_COMM ;
1318 comm_encoding = GSM_MARKER ;
1320 /* Override standard value here.*/
1324 case SF_FORMAT_IMA_ADPCM :
1325 psf->endian = SF_ENDIAN_BIG ;
1326 comm_type = AIFC_MARKER ;
1327 comm_size = SIZEOF_AIFC_COMM ;
1328 comm_encoding = ima4_MARKER ;
1330 /* Override standard value here.*/
1332 comm_frames = psf->sf.frames / AIFC_IMA4_SAMPLES_PER_BLOCK ;
1335 default : return SFE_BAD_OPEN_FORMAT ;
1338 /* Reset the current header length to zero. */
1339 psf->header [0] = 0 ;
1340 psf->headindex = 0 ;
1341 psf_fseek (psf, 0, SEEK_SET) ;
1343 psf_binheader_writef (psf, "Etm8", FORM_MARKER, psf->filelength - 8) ;
1345 /* Write AIFF/AIFC marker and COM chunk. */
1346 if (comm_type == AIFC_MARKER)
1347 /* AIFC must have an FVER chunk. */
1348 psf_binheader_writef (psf, "Emm44", comm_type, FVER_MARKER, 4, 0xA2805140) ;
1350 psf_binheader_writef (psf, "Em", comm_type) ;
1352 paiff->comm_offset = psf->headindex - 8 ;
1354 memset (comm_sample_rate, 0, sizeof (comm_sample_rate)) ;
1355 uint2tenbytefloat (psf->sf.samplerate, comm_sample_rate) ;
1357 psf_binheader_writef (psf, "Em42t42", COMM_MARKER, comm_size, psf->sf.channels, comm_frames, bit_width) ;
1358 psf_binheader_writef (psf, "b", comm_sample_rate, sizeof (comm_sample_rate)) ;
1360 /* AIFC chunks have some extra data. */
1361 if (comm_type == AIFC_MARKER)
1362 psf_binheader_writef (psf, "mb", comm_encoding, comm_zero_bytes, sizeof (comm_zero_bytes)) ;
1364 if (psf->channel_map && paiff->chanmap_tag)
1365 psf_binheader_writef (psf, "Em4444", CHAN_MARKER, 12, paiff->chanmap_tag, 0, 0) ;
1367 if (psf->instrument != NULL)
1368 { MARK_ID_POS m [4] ;
1370 unsigned short ct = 0 ;
1372 memset (m, 0, sizeof (m)) ;
1373 memset (&ch, 0, sizeof (ch)) ;
1375 ch.baseNote = psf->instrument->basenote ;
1376 ch.detune = psf->instrument->detune ;
1377 ch.lowNote = psf->instrument->key_lo ;
1378 ch.highNote = psf->instrument->key_hi ;
1379 ch.lowVelocity = psf->instrument->velocity_lo ;
1380 ch.highVelocity = psf->instrument->velocity_hi ;
1381 ch.gain = psf->instrument->gain ;
1382 if (psf->instrument->loops [0].mode != SF_LOOP_NONE)
1383 { ch.sustain_loop.playMode = 1 ;
1384 ch.sustain_loop.beginLoop = ct ;
1385 m [0].markerID = ct++ ;
1386 m [0].position = psf->instrument->loops [0].start ;
1387 ch.sustain_loop.endLoop = ct ;
1388 m [1].markerID = ct++ ;
1389 m [1].position = psf->instrument->loops [0].end ;
1391 if (psf->instrument->loops [1].mode != SF_LOOP_NONE)
1392 { ch.release_loop.playMode = 1 ;
1393 ch.release_loop.beginLoop = ct ;
1394 m [2].markerID = ct++ ;
1395 m [2].position = psf->instrument->loops [1].start ;
1396 ch.release_loop.endLoop = ct ;
1397 m [3].markerID = ct++ ;
1398 m [3].position = psf->instrument->loops [1].end ;
1401 { ch.release_loop.playMode = 0 ;
1402 ch.release_loop.beginLoop = 0 ;
1403 ch.release_loop.endLoop = 0 ;
1406 psf_binheader_writef (psf, "Em4111111", INST_MARKER, SIZEOF_INST_CHUNK, ch.baseNote, ch.detune,
1407 ch.lowNote, ch.highNote, ch.lowVelocity, ch.highVelocity) ;
1408 psf_binheader_writef (psf, "2222222", ch.gain, ch.sustain_loop.playMode,
1409 ch.sustain_loop.beginLoop, ch.sustain_loop.endLoop, ch.release_loop.playMode,
1410 ch.release_loop.beginLoop, ch.release_loop.endLoop) ;
1413 psf_binheader_writef (psf, "Em42241b241b",
1414 MARK_MARKER, 2 + 2 * (2 + 4 + 1 + 9), 2,
1415 m [0].markerID, m [0].position, 8, "beg loop", make_size_t (9),
1416 m [1].markerID, m [1].position, 8, "end loop", make_size_t (9)) ;
1418 psf_binheader_writef (psf, "Em42 241b 241b 241b 241b",
1419 MARK_MARKER, 2 + 4 * (2 + 4 + 1 + 9), 4,
1420 m [0].markerID, m [0].position, 8, "beg loop", make_size_t (9),
1421 m [1].markerID, m [1].position, 8, "end loop", make_size_t (9),
1422 m [2].markerID, m [2].position, 8, "beg loop", make_size_t (9),
1423 m [3].markerID, m [3].position, 8, "end loop", make_size_t (9)) ;
1426 if (psf->strings.flags & SF_STR_LOCATE_START)
1427 aiff_write_strings (psf, SF_STR_LOCATE_START) ;
1429 if (psf->peak_info != NULL && psf->peak_info->peak_loc == SF_PEAK_START)
1430 { psf_binheader_writef (psf, "Em4", PEAK_MARKER, AIFF_PEAK_CHUNK_SIZE (psf->sf.channels)) ;
1431 psf_binheader_writef (psf, "E44", 1, time (NULL)) ;
1432 for (k = 0 ; k < psf->sf.channels ; k++)
1433 psf_binheader_writef (psf, "Eft8", (float) psf->peak_info->peaks [k].value, psf->peak_info->peaks [k].position) ;
1436 /* Write custom headers. */
1437 for (uk = 0 ; uk < psf->wchunks.used ; uk++)
1438 psf_binheader_writef (psf, "Em4b", psf->wchunks.chunks [uk].mark32, psf->wchunks.chunks [uk].len, psf->wchunks.chunks [uk].data, make_size_t (psf->wchunks.chunks [uk].len)) ;
1440 /* Write SSND chunk. */
1441 paiff->ssnd_offset = psf->headindex ;
1442 psf_binheader_writef (psf, "Etm844", SSND_MARKER, psf->datalength + SIZEOF_SSND_CHUNK, 0, 0) ;
1444 /* Header construction complete so write it out. */
1445 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
1450 if (has_data && psf->dataoffset != psf->headindex)
1451 return psf->error = SFE_INTERNAL ;
1453 psf->dataoffset = psf->headindex ;
1456 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
1457 else if (current > 0)
1458 psf_fseek (psf, current, SEEK_SET) ;
1461 } /* aiff_write_header */
1464 aiff_write_tailer (SF_PRIVATE *psf)
1467 /* Reset the current header length to zero. */
1468 psf->header [0] = 0 ;
1469 psf->headindex = 0 ;
1471 psf->dataend = psf_fseek (psf, 0, SEEK_END) ;
1473 /* Make sure tailer data starts at even byte offset. Pad if necessary. */
1474 if (psf->dataend % 2 == 1)
1475 { psf_fwrite (psf->header, 1, 1, psf) ;
1479 if (psf->peak_info != NULL && psf->peak_info->peak_loc == SF_PEAK_END)
1480 { psf_binheader_writef (psf, "Em4", PEAK_MARKER, AIFF_PEAK_CHUNK_SIZE (psf->sf.channels)) ;
1481 psf_binheader_writef (psf, "E44", 1, time (NULL)) ;
1482 for (k = 0 ; k < psf->sf.channels ; k++)
1483 psf_binheader_writef (psf, "Eft8", (float) psf->peak_info->peaks [k].value, psf->peak_info->peaks [k].position) ;
1486 if (psf->strings.flags & SF_STR_LOCATE_END)
1487 aiff_write_strings (psf, SF_STR_LOCATE_END) ;
1489 /* Write the tailer. */
1490 if (psf->headindex > 0)
1491 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
1494 } /* aiff_write_tailer */
1497 aiff_write_strings (SF_PRIVATE *psf, int location)
1500 for (k = 0 ; k < SF_MAX_STRINGS ; k++)
1501 { if (psf->strings.data [k].type == 0)
1504 if (psf->strings.data [k].flags != location)
1507 switch (psf->strings.data [k].type)
1508 { case SF_STR_SOFTWARE :
1509 slen = strlen (psf->strings.storage + psf->strings.data [k].offset) ;
1510 psf_binheader_writef (psf, "Em4mb", APPL_MARKER, slen + 4, m3ga_MARKER, psf->strings.storage + psf->strings.data [k].offset, make_size_t (slen + (slen & 1))) ;
1514 psf_binheader_writef (psf, "EmS", NAME_MARKER, psf->strings.storage + psf->strings.data [k].offset) ;
1517 case SF_STR_COPYRIGHT :
1518 psf_binheader_writef (psf, "EmS", c_MARKER, psf->strings.storage + psf->strings.data [k].offset) ;
1521 case SF_STR_ARTIST :
1522 psf_binheader_writef (psf, "EmS", AUTH_MARKER, psf->strings.storage + psf->strings.data [k].offset) ;
1525 case SF_STR_COMMENT :
1526 psf_binheader_writef (psf, "EmS", ANNO_MARKER, psf->strings.storage + psf->strings.data [k].offset) ;
1531 psf_binheader_writef (psf, "Ems", ICRD_MARKER, psf->strings.data [k].str) ;
1538 } /* aiff_write_strings */
1541 aiff_command (SF_PRIVATE * psf, int command, void * UNUSED (data), int UNUSED (datasize))
1542 { AIFF_PRIVATE *paiff ;
1544 if ((paiff = psf->container_data) == NULL)
1545 return SFE_INTERNAL ;
1548 { case SFC_SET_CHANNEL_MAP_INFO :
1549 paiff->chanmap_tag = aiff_caf_find_channel_layout_tag (psf->channel_map, psf->sf.channels) ;
1550 return (paiff->chanmap_tag != 0) ;
1557 } /* aiff_command */
1560 get_loop_mode_str (short mode)
1562 { case 0 : return "none" ;
1563 case 1 : return "forward" ;
1564 case 2 : return "backward" ;
1567 return "*** unknown" ;
1568 } /* get_loop_mode_str */
1571 get_loop_mode (short mode)
1573 { case 0 : return SF_LOOP_NONE ;
1574 case 1 : return SF_LOOP_FORWARD ;
1575 case 2 : return SF_LOOP_BACKWARD ;
1578 return SF_LOOP_NONE ;
1579 } /* get_loop_mode */
1581 /*==========================================================================================
1582 ** Rough hack at converting from 80 bit IEEE float in AIFF header to an int and
1583 ** back again. It assumes that all sample rates are between 1 and 800MHz, which
1584 ** should be OK as other sound file formats use a 32 bit integer to store sample
1586 ** There is another (probably better) version in the source code to the SoX but it
1587 ** has a copyright which probably prevents it from being allowable as GPL/LGPL.
1591 tenbytefloat2int (unsigned char *bytes)
1594 if (bytes [0] & 0x80) /* Negative number. */
1597 if (bytes [0] <= 0x3F) /* Less than 1. */
1600 if (bytes [0] > 0x40) /* Way too big. */
1603 if (bytes [0] == 0x40 && bytes [1] > 0x1C) /* Too big. */
1606 /* Ok, can handle it. */
1608 val = (bytes [2] << 23) | (bytes [3] << 15) | (bytes [4] << 7) | (bytes [5] >> 1) ;
1610 val >>= (29 - bytes [1]) ;
1613 } /* tenbytefloat2int */
1616 uint2tenbytefloat (unsigned int num, unsigned char *bytes)
1617 { unsigned int mask = 0x40000000 ;
1621 { bytes [0] = 0x3F ;
1630 { bytes [1] = 0x1D ;
1634 for (count = 0 ; count < 32 ; count ++)
1641 bytes [1] = 29 - count ;
1642 bytes [2] = (num >> 24) & 0xFF ;
1643 bytes [3] = (num >> 16) & 0xFF ;
1644 bytes [4] = (num >> 8) & 0xFF ;
1645 bytes [5] = num & 0xFF ;
1647 } /* uint2tenbytefloat */
1650 aiff_read_basc_chunk (SF_PRIVATE * psf, int datasize)
1651 { const char * type_str ;
1655 count = psf_binheader_readf (psf, "E442", &bc.version, &bc.numBeats, &bc.rootNote) ;
1656 count += psf_binheader_readf (psf, "E222", &bc.scaleType, &bc.sigNumerator, &bc.sigDenominator) ;
1657 count += psf_binheader_readf (psf, "E2j", &bc.loopType, datasize - sizeof (bc)) ;
1659 psf_log_printf (psf, " Version ? : %u\n Num Beats : %u\n Root Note : 0x%x\n",
1660 bc.version, bc.numBeats, bc.rootNote) ;
1662 switch (bc.scaleType)
1663 { case basc_SCALE_MINOR :
1664 type_str = "MINOR" ;
1666 case basc_SCALE_MAJOR :
1667 type_str = "MAJOR" ;
1669 case basc_SCALE_NEITHER :
1670 type_str = "NEITHER" ;
1672 case basc_SCALE_BOTH :
1676 type_str = "!!WRONG!!" ;
1680 psf_log_printf (psf, " ScaleType : 0x%x (%s)\n", bc.scaleType, type_str) ;
1681 psf_log_printf (psf, " Time Sig : %d/%d\n", bc.sigNumerator, bc.sigDenominator) ;
1683 switch (bc.loopType)
1684 { case basc_TYPE_ONE_SHOT :
1685 type_str = "One Shot" ;
1687 case basc_TYPE_LOOP :
1691 type_str = "!!WRONG!!" ;
1695 psf_log_printf (psf, " Loop Type : 0x%x (%s)\n", bc.loopType, type_str) ;
1697 if ((psf->loop_info = calloc (1, sizeof (SF_LOOP_INFO))) == NULL)
1698 return SFE_MALLOC_FAILED ;
1700 psf->loop_info->time_sig_num = bc.sigNumerator ;
1701 psf->loop_info->time_sig_den = bc.sigDenominator ;
1702 psf->loop_info->loop_mode = (bc.loopType == basc_TYPE_ONE_SHOT) ? SF_LOOP_NONE : SF_LOOP_FORWARD ;
1703 psf->loop_info->num_beats = bc.numBeats ;
1705 /* Can always be recalculated from other known fields. */
1706 psf->loop_info->bpm = (1.0 / psf->sf.frames) * psf->sf.samplerate
1707 * ((bc.numBeats * 4.0) / bc.sigDenominator) * 60.0 ;
1708 psf->loop_info->root_key = bc.rootNote ;
1710 if (count < datasize)
1711 psf_binheader_readf (psf, "j", datasize - count) ;
1714 } /* aiff_read_basc_chunk */
1718 aiff_read_chanmap (SF_PRIVATE * psf, unsigned dword)
1719 { const AIFF_CAF_CHANNEL_MAP * map_info ;
1720 unsigned channel_bitmap, channel_decriptions, bytesread ;
1723 bytesread = psf_binheader_readf (psf, "444", &layout_tag, &channel_bitmap, &channel_decriptions) ;
1725 if ((map_info = aiff_caf_of_channel_layout_tag (layout_tag)) == NULL)
1728 psf_log_printf (psf, " Tag : %x\n", layout_tag) ;
1730 psf_log_printf (psf, " Layout : %s\n", map_info->name) ;
1732 if (bytesread < dword)
1733 psf_binheader_readf (psf, "j", dword - bytesread) ;
1735 if (map_info->channel_map != NULL)
1736 { size_t chanmap_size = psf->sf.channels * sizeof (psf->channel_map [0]) ;
1738 free (psf->channel_map) ;
1740 if ((psf->channel_map = malloc (chanmap_size)) == NULL)
1741 return SFE_MALLOC_FAILED ;
1743 memcpy (psf->channel_map, map_info->channel_map, chanmap_size) ;
1747 } /* aiff_read_chanmap */
1749 /*==============================================================================
1753 aiff_set_chunk (SF_PRIVATE *psf, const SF_CHUNK_INFO * chunk_info)
1754 { return psf_save_write_chunk (&psf->wchunks, chunk_info) ;
1755 } /* aiff_set_chunk */
1757 static SF_CHUNK_ITERATOR *
1758 aiff_next_chunk_iterator (SF_PRIVATE *psf, SF_CHUNK_ITERATOR * iterator)
1759 { return psf_next_chunk_iterator (&psf->rchunks, iterator) ;
1760 } /* aiff_next_chunk_iterator */
1763 aiff_get_chunk_size (SF_PRIVATE *psf, const SF_CHUNK_ITERATOR * iterator, SF_CHUNK_INFO * chunk_info)
1766 if ((indx = psf_find_read_chunk_iterator (&psf->rchunks, iterator)) < 0)
1767 return SFE_UNKNOWN_CHUNK ;
1769 chunk_info->datalen = psf->rchunks.chunks [indx].len ;
1771 return SFE_NO_ERROR ;
1772 } /* aiff_get_chunk_size */
1775 aiff_get_chunk_data (SF_PRIVATE *psf, const SF_CHUNK_ITERATOR * iterator, SF_CHUNK_INFO * chunk_info)
1779 if ((indx = psf_find_read_chunk_iterator (&psf->rchunks, iterator)) < 0)
1780 return SFE_UNKNOWN_CHUNK ;
1782 if (chunk_info->data == NULL)
1783 return SFE_BAD_CHUNK_DATA_PTR ;
1785 chunk_info->id_size = psf->rchunks.chunks [indx].id_size ;
1786 memcpy (chunk_info->id, psf->rchunks.chunks [indx].id, sizeof (chunk_info->id) / sizeof (*chunk_info->id)) ;
1788 pos = psf_ftell (psf) ;
1789 psf_fseek (psf, psf->rchunks.chunks [indx].offset, SEEK_SET) ;
1790 psf_fread (chunk_info->data, SF_MIN (chunk_info->datalen, psf->rchunks.chunks [indx].len), 1, psf) ;
1791 psf_fseek (psf, pos, SEEK_SET) ;
1793 return SFE_NO_ERROR ;
1794 } /* aiff_get_chunk_data */