2 ** Copyright (C) 1999-2016 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.
127 int16_t numChannels ;
128 uint32_t numSampleFrames ;
130 uint8_t sampleRate [10] ;
132 char zero_bytes [2] ;
147 { int8_t baseNote ; /* all notes are MIDI note numbers */
148 int8_t detune ; /* cents off, only -50 to +50 are significant */
151 int8_t lowVelocity ; /* 1 to 127 */
152 int8_t highVelocity ; /* 1 to 127 */
153 int16_t 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,
177 uint16_t sigNumerator ;
178 uint16_t sigDenominator ;
183 { uint16_t markerID ;
188 { sf_count_t comm_offset ;
189 sf_count_t ssnd_offset ;
191 int32_t chanmap_tag ;
193 MARK_ID_POS *markstr ;
196 /*------------------------------------------------------------------------------
197 * Private static functions.
200 static int aiff_close (SF_PRIVATE *psf) ;
202 static int tenbytefloat2int (uint8_t *bytes) ;
203 static void uint2tenbytefloat (uint32_t num, uint8_t *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 (int16_t mode) ;
217 static int16_t get_loop_mode (int16_t 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 uint32_t marker_to_position (const MARK_ID_POS *m, uint16_t 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 if (psf->sf.frames > comm_fmt.numSampleFrames)
323 psf->sf.frames = comm_fmt.numSampleFrames ;
326 case SF_FORMAT_DWVW_16 :
327 error = dwvw_init (psf, 16) ;
328 if (psf->sf.frames > comm_fmt.numSampleFrames)
329 psf->sf.frames = comm_fmt.numSampleFrames ;
332 case SF_FORMAT_DWVW_24 :
333 error = dwvw_init (psf, 24) ;
334 if (psf->sf.frames > comm_fmt.numSampleFrames)
335 psf->sf.frames = comm_fmt.numSampleFrames ;
338 case SF_FORMAT_DWVW_N :
339 if (psf->file.mode != SFM_READ)
340 { error = SFE_DWVW_BAD_BITWIDTH ;
343 if (comm_fmt.sampleSize >= 8 && comm_fmt.sampleSize < 24)
344 { error = dwvw_init (psf, comm_fmt.sampleSize) ;
345 if (psf->sf.frames > comm_fmt.numSampleFrames)
346 psf->sf.frames = comm_fmt.numSampleFrames ;
349 psf_log_printf (psf, "AIFC/DWVW : Bad bitwidth %d\n", comm_fmt.sampleSize) ;
350 error = SFE_DWVW_BAD_BITWIDTH ;
353 case SF_FORMAT_IMA_ADPCM :
355 ** IMA ADPCM encoded AIFF files always have a block length
356 ** of 34 which decodes to 64 samples.
358 error = aiff_ima_init (psf, AIFC_IMA4_BLOCK_LEN, AIFC_IMA4_SAMPLES_PER_BLOCK) ;
360 /* Lite remove end */
362 default : return SFE_UNIMPLEMENTED ;
365 if (psf->file.mode != SFM_WRITE && psf->sf.frames - comm_fmt.numSampleFrames != 0)
366 { psf_log_printf (psf,
367 "*** Frame count read from 'COMM' chunk (%u) not equal to frame count\n"
368 "*** calculated from length of 'SSND' chunk (%u).\n",
369 comm_fmt.numSampleFrames, (uint32_t) psf->sf.frames) ;
375 /*==========================================================================================
376 ** Private functions.
379 /* This function ought to check size */
381 marker_to_position (const MARK_ID_POS *m, uint16_t n, int marksize)
384 for (i = 0 ; i < marksize ; i++)
385 if (m [i].markerID == n)
386 return m [i].position ;
388 } /* marker_to_position */
391 aiff_read_header (SF_PRIVATE *psf, COMM_CHUNK *comm_fmt)
392 { SSND_CHUNK ssnd_fmt ;
393 AIFF_PRIVATE *paiff ;
395 uint32_t chunk_size = 0, FORMsize, SSNDsize, bytesread, mark_count = 0 ;
396 int k, found_chunk = 0, done = 0, error = 0 ;
398 int instr_found = 0, mark_found = 0 ;
400 if (psf->filelength > SF_PLATFORM_S64 (0xffffffff))
401 psf_log_printf (psf, "Warning : filelength > 0xffffffff. This is bad!!!!\n") ;
403 if ((paiff = psf->container_data) == NULL)
404 return SFE_INTERNAL ;
406 paiff->comm_offset = 0 ;
407 paiff->ssnd_offset = 0 ;
409 /* Set position to start of file to begin reading header. */
410 psf_binheader_readf (psf, "p", 0) ;
412 memset (comm_fmt, 0, sizeof (COMM_CHUNK)) ;
414 /* Until recently AIF* file were all BIG endian. */
415 psf->endian = SF_ENDIAN_BIG ;
417 /* AIFF files can apparently have their chunks in any order. However, they
418 ** must have a FORM chunk. Approach here is to read all the chunks one by
419 ** one and then check for the mandatory chunks at the end.
423 size_t jump = chunk_size & 1 ;
425 marker = chunk_size = 0 ;
426 psf_binheader_readf (psf, "Ejm4", jump, &marker, &chunk_size) ;
428 { sf_count_t pos = psf_ftell (psf) ;
429 psf_log_printf (psf, "Have 0 marker at position %D (0x%x).\n", pos, pos) ;
433 if (psf->file.mode == SFM_RDWR && (found_chunk & HAVE_SSND))
434 return SFE_AIFF_RW_SSND_NOT_LAST ;
436 psf_store_read_chunk_u32 (&psf->rchunks, marker, psf_ftell (psf), chunk_size) ;
441 return SFE_AIFF_NO_FORM ;
443 FORMsize = chunk_size ;
445 found_chunk |= HAVE_FORM ;
446 psf_binheader_readf (psf, "m", &marker) ;
450 found_chunk |= (marker == AIFC_MARKER) ? (HAVE_AIFC | HAVE_AIFF) : HAVE_AIFF ;
456 if (psf->fileoffset > 0 && psf->filelength > FORMsize + 8)
457 { /* Set file length. */
458 psf->filelength = FORMsize + 8 ;
459 psf_log_printf (psf, "FORM : %u\n %M\n", FORMsize, marker) ;
461 else if (FORMsize != psf->filelength - 2 * SIGNED_SIZEOF (chunk_size))
462 { chunk_size = psf->filelength - 2 * sizeof (chunk_size) ;
463 psf_log_printf (psf, "FORM : %u (should be %u)\n %M\n", FORMsize, chunk_size, marker) ;
464 FORMsize = chunk_size ;
467 psf_log_printf (psf, "FORM : %u\n %M\n", FORMsize, marker) ;
468 /* Set this to 0, so we don't jump a byte when parsing the next marker. */
474 paiff->comm_offset = psf_ftell (psf) - 8 ;
475 chunk_size += chunk_size & 1 ;
476 comm_fmt->size = chunk_size ;
477 if ((error = aiff_read_comm_chunk (psf, comm_fmt)) != 0)
480 found_chunk |= HAVE_COMM ;
484 /* Must have COMM chunk before PEAK chunk. */
485 if ((found_chunk & (HAVE_FORM | HAVE_AIFF | HAVE_COMM)) != (HAVE_FORM | HAVE_AIFF | HAVE_COMM))
486 return SFE_AIFF_PEAK_B4_COMM ;
488 psf_log_printf (psf, "%M : %d\n", marker, chunk_size) ;
489 if (chunk_size != AIFF_PEAK_CHUNK_SIZE (psf->sf.channels))
490 { psf_binheader_readf (psf, "j", chunk_size) ;
491 psf_log_printf (psf, "*** File PEAK chunk too big.\n") ;
492 return SFE_WAV_BAD_PEAK ;
495 if ((psf->peak_info = peak_info_calloc (psf->sf.channels)) == NULL)
496 return SFE_MALLOC_FAILED ;
498 /* read in rest of PEAK chunk. */
499 psf_binheader_readf (psf, "E44", &(psf->peak_info->version), &(psf->peak_info->timestamp)) ;
501 if (psf->peak_info->version != 1)
502 psf_log_printf (psf, " version : %d *** (should be version 1)\n", psf->peak_info->version) ;
504 psf_log_printf (psf, " version : %d\n", psf->peak_info->version) ;
506 psf_log_printf (psf, " time stamp : %d\n", psf->peak_info->timestamp) ;
507 psf_log_printf (psf, " Ch Position Value\n") ;
510 for (k = 0 ; k < psf->sf.channels ; k++)
514 psf_binheader_readf (psf, "Ef4", &value, &position) ;
515 psf->peak_info->peaks [k].value = value ;
516 psf->peak_info->peaks [k].position = position ;
518 snprintf (cptr, sizeof (ubuf.scbuf), " %2d %-12" PRId64 " %g\n",
519 k, psf->peak_info->peaks [k].position, psf->peak_info->peaks [k].value) ;
520 cptr [sizeof (ubuf.scbuf) - 1] = 0 ;
521 psf_log_printf (psf, "%s", cptr) ;
524 psf->peak_info->peak_loc = ((found_chunk & HAVE_SSND) == 0) ? SF_PEAK_START : SF_PEAK_END ;
528 if ((found_chunk & HAVE_AIFC) && (found_chunk & HAVE_FVER) == 0)
529 psf_log_printf (psf, "*** Valid AIFC files should have an FVER chunk.\n") ;
531 paiff->ssnd_offset = psf_ftell (psf) - 8 ;
532 SSNDsize = chunk_size ;
533 psf_binheader_readf (psf, "E44", &(ssnd_fmt.offset), &(ssnd_fmt.blocksize)) ;
535 psf->datalength = SSNDsize - sizeof (ssnd_fmt) ;
536 psf->dataoffset = psf_ftell (psf) ;
538 if (psf->datalength > psf->filelength - psf->dataoffset || psf->datalength < 0)
539 { psf_log_printf (psf, " SSND : %u (should be %D)\n", SSNDsize, psf->filelength - psf->dataoffset + sizeof (SSND_CHUNK)) ;
540 psf->datalength = psf->filelength - psf->dataoffset ;
543 psf_log_printf (psf, " SSND : %u\n", SSNDsize) ;
545 if (ssnd_fmt.offset == 0 || psf->dataoffset + ssnd_fmt.offset == ssnd_fmt.blocksize)
546 { psf_log_printf (psf, " Offset : %u\n", ssnd_fmt.offset) ;
547 psf_log_printf (psf, " Block Size : %u\n", ssnd_fmt.blocksize) ;
549 psf->dataoffset += ssnd_fmt.offset ;
550 psf->datalength -= ssnd_fmt.offset ;
553 { psf_log_printf (psf, " Offset : %u\n", ssnd_fmt.offset) ;
554 psf_log_printf (psf, " Block Size : %u ???\n", ssnd_fmt.blocksize) ;
555 psf->dataoffset += ssnd_fmt.offset ;
556 psf->datalength -= ssnd_fmt.offset ;
559 /* Only set dataend if there really is data at the end. */
560 if (psf->datalength + psf->dataoffset < psf->filelength)
561 psf->dataend = psf->datalength + psf->dataoffset ;
563 found_chunk |= HAVE_SSND ;
565 if (! psf->sf.seekable)
568 /* Seek to end of SSND chunk. */
569 psf_fseek (psf, psf->dataoffset + psf->datalength, SEEK_SET) ;
575 if (chunk_size >= SIGNED_SIZEOF (ubuf.scbuf))
576 { psf_log_printf (psf, " %M : %d (too big)\n", marker, chunk_size) ;
577 return SFE_INTERNAL ;
581 psf_binheader_readf (psf, "b", cptr, chunk_size + (chunk_size & 1)) ;
582 cptr [chunk_size] = 0 ;
584 psf_sanitize_string (cptr, chunk_size) ;
586 psf_log_printf (psf, " %M : %s\n", marker, cptr) ;
587 psf_store_string (psf, SF_STR_COPYRIGHT, cptr) ;
588 chunk_size += chunk_size & 1 ;
594 if (chunk_size >= SIGNED_SIZEOF (ubuf.scbuf) - 1)
595 { psf_log_printf (psf, " %M : %d (too big)\n", marker, chunk_size) ;
596 return SFE_INTERNAL ;
600 psf_binheader_readf (psf, "b", cptr, chunk_size + (chunk_size & 1)) ;
601 cptr [chunk_size] = 0 ;
602 psf_log_printf (psf, " %M : %s\n", marker, cptr) ;
603 psf_store_string (psf, SF_STR_ARTIST, cptr) ;
604 chunk_size += chunk_size & 1 ;
608 { uint16_t count, id, len ;
609 uint32_t timestamp, bytes ;
614 bytes -= psf_binheader_readf (psf, "E2", &count) ;
615 psf_log_printf (psf, " %M : %d\n count : %d\n", marker, chunk_size, count) ;
617 for (k = 0 ; k < count ; k++)
618 { bytes -= psf_binheader_readf (psf, "E422", ×tamp, &id, &len) ;
619 psf_log_printf (psf, " time : 0x%x\n marker : %x\n length : %d\n", timestamp, id, len) ;
621 if (len + 1 > SIGNED_SIZEOF (ubuf.scbuf))
622 { psf_log_printf (psf, "\nError : string length (%d) too big.\n", len) ;
623 return SFE_INTERNAL ;
627 bytes -= psf_binheader_readf (psf, "b", cptr, len) ;
629 psf_log_printf (psf, " string : %s\n", cptr) ;
633 psf_binheader_readf (psf, "j", bytes) ;
638 { unsigned appl_marker ;
642 if (chunk_size >= SIGNED_SIZEOF (ubuf.scbuf) - 1)
643 { psf_log_printf (psf, " %M : %u (too big, skipping)\n", marker, chunk_size) ;
644 psf_binheader_readf (psf, "j", chunk_size + (chunk_size & 1)) ;
649 { psf_log_printf (psf, " %M : %d (too small, skipping)\n", marker, chunk_size) ;
650 psf_binheader_readf (psf, "j", chunk_size + (chunk_size & 1)) ;
655 psf_binheader_readf (psf, "mb", &appl_marker, cptr, chunk_size + (chunk_size & 1) - 4) ;
656 cptr [chunk_size] = 0 ;
658 for (k = 0 ; k < (int) chunk_size ; k++)
659 if (! psf_isprint (cptr [k]))
664 psf_log_printf (psf, " %M : %d\n AppSig : %M\n Name : %s\n", marker, chunk_size, appl_marker, cptr) ;
665 psf_store_string (psf, SF_STR_SOFTWARE, cptr) ;
666 chunk_size += chunk_size & 1 ;
673 if (chunk_size >= SIGNED_SIZEOF (ubuf.scbuf) - 2)
674 { psf_log_printf (psf, " %M : %d (too big)\n", marker, chunk_size) ;
675 return SFE_INTERNAL ;
679 psf_binheader_readf (psf, "b", cptr, chunk_size + (chunk_size & 1)) ;
680 cptr [chunk_size] = 0 ;
681 psf_log_printf (psf, " %M : %s\n", marker, cptr) ;
682 psf_store_string (psf, SF_STR_TITLE, cptr) ;
683 chunk_size += chunk_size & 1 ;
689 if (chunk_size >= SIGNED_SIZEOF (ubuf.scbuf) - 2)
690 { psf_log_printf (psf, " %M : %d (too big)\n", marker, chunk_size) ;
691 return SFE_INTERNAL ;
695 psf_binheader_readf (psf, "b", cptr, chunk_size + (chunk_size & 1)) ;
696 cptr [chunk_size] = 0 ;
697 psf_log_printf (psf, " %M : %s\n", marker, cptr) ;
698 psf_store_string (psf, SF_STR_COMMENT, cptr) ;
699 chunk_size += chunk_size & 1 ;
703 if (chunk_size != SIZEOF_INST_CHUNK)
704 { psf_log_printf (psf, " %M : %d (should be %d)\n", marker, chunk_size, SIZEOF_INST_CHUNK) ;
705 psf_binheader_readf (psf, "j", chunk_size) ;
708 psf_log_printf (psf, " %M : %d\n", marker, chunk_size) ;
709 { uint8_t bytes [6] ;
712 if (psf->instrument == NULL && (psf->instrument = psf_instrument_alloc ()) == NULL)
713 return SFE_MALLOC_FAILED ;
715 psf_binheader_readf (psf, "b", bytes, 6) ;
716 psf_log_printf (psf, " Base Note : %u\n Detune : %u\n"
717 " Low Note : %u\n High Note : %u\n"
718 " Low Vel. : %u\n High Vel. : %u\n",
719 bytes [0], bytes [1], bytes [2], bytes [3], bytes [4], bytes [5]) ;
720 psf->instrument->basenote = bytes [0] ;
721 psf->instrument->detune = bytes [1] ;
722 psf->instrument->key_lo = bytes [2] ;
723 psf->instrument->key_hi = bytes [3] ;
724 psf->instrument->velocity_lo = bytes [4] ;
725 psf->instrument->velocity_hi = bytes [5] ;
726 psf_binheader_readf (psf, "E2", &gain) ;
727 psf->instrument->gain = gain ;
728 psf_log_printf (psf, " Gain (dB) : %d\n", gain) ;
730 { int16_t mode ; /* 0 - no loop, 1 - forward looping, 2 - backward looping */
731 const char *loop_mode ;
732 uint16_t begin, end ;
734 psf_binheader_readf (psf, "E222", &mode, &begin, &end) ;
735 loop_mode = get_loop_mode_str (mode) ;
736 mode = get_loop_mode (mode) ;
737 if (mode == SF_LOOP_NONE)
738 { psf->instrument->loop_count = 0 ;
739 psf->instrument->loops [0].mode = SF_LOOP_NONE ;
742 { psf->instrument->loop_count = 1 ;
743 psf->instrument->loops [0].mode = SF_LOOP_FORWARD ;
744 psf->instrument->loops [0].start = begin ;
745 psf->instrument->loops [0].end = end ;
746 psf->instrument->loops [0].count = 0 ;
748 psf_log_printf (psf, " Sustain\n mode : %d => %s\n begin : %u\n end : %u\n",
749 mode, loop_mode, begin, end) ;
750 psf_binheader_readf (psf, "E222", &mode, &begin, &end) ;
751 loop_mode = get_loop_mode_str (mode) ;
752 mode = get_loop_mode (mode) ;
753 if (mode == SF_LOOP_NONE)
754 psf->instrument->loops [1].mode = SF_LOOP_NONE ;
756 { psf->instrument->loop_count += 1 ;
757 psf->instrument->loops [1].mode = SF_LOOP_FORWARD ;
758 psf->instrument->loops [1].start = begin ;
759 psf->instrument->loops [1].end = end ;
760 psf->instrument->loops [1].count = 0 ;
762 psf_log_printf (psf, " Release\n mode : %d => %s\n begin : %u\n end : %u\n",
763 mode, loop_mode, begin, end) ;
769 psf_log_printf (psf, " basc : %u\n", chunk_size) ;
771 if ((error = aiff_read_basc_chunk (psf, chunk_size)))
776 psf_log_printf (psf, " %M : %d\n", marker, chunk_size) ;
777 { uint16_t mark_id, n = 0 ;
780 bytesread = psf_binheader_readf (psf, "E2", &n) ;
782 psf_log_printf (psf, " Count : %u\n", mark_count) ;
783 if (paiff->markstr != NULL)
784 { psf_log_printf (psf, "*** Second MARK chunk found. Throwing away the first.\n") ;
785 free (paiff->markstr) ;
787 paiff->markstr = calloc (mark_count, sizeof (MARK_ID_POS)) ;
788 if (paiff->markstr == NULL)
789 return SFE_MALLOC_FAILED ;
791 if (mark_count > 1000)
792 { psf_log_printf (psf, " More than 1000 markers, skipping!\n") ;
793 psf_binheader_readf (psf, "j", chunk_size - bytesread) ;
797 if ((psf->cues = psf_cues_alloc (mark_count)) == NULL)
798 return SFE_MALLOC_FAILED ;
800 for (n = 0 ; n < mark_count && bytesread < chunk_size ; n++)
801 { uint32_t pstr_len ;
804 bytesread += psf_binheader_readf (psf, "E241", &mark_id, &position, &ch) ;
805 psf_log_printf (psf, " Mark ID : %u\n Position : %u\n", mark_id, position) ;
807 psf->cues->cue_points [n].indx = mark_id ;
808 psf->cues->cue_points [n].position = 0 ;
809 psf->cues->cue_points [n].fcc_chunk = MAKE_MARKER ('d', 'a', 't', 'a') ; /* always data */
810 psf->cues->cue_points [n].chunk_start = 0 ;
811 psf->cues->cue_points [n].block_start = 0 ;
812 psf->cues->cue_points [n].sample_offset = position ;
814 pstr_len = (ch & 1) ? ch : ch + 1 ;
816 if (pstr_len < sizeof (ubuf.scbuf) - 1)
817 { bytesread += psf_binheader_readf (psf, "b", ubuf.scbuf, pstr_len) ;
818 ubuf.scbuf [pstr_len] = 0 ;
821 { uint32_t read_len = pstr_len - (sizeof (ubuf.scbuf) - 1) ;
822 bytesread += psf_binheader_readf (psf, "bj", ubuf.scbuf, read_len, pstr_len - read_len) ;
823 ubuf.scbuf [sizeof (ubuf.scbuf) - 1] = 0 ;
826 psf_log_printf (psf, " Name : %s\n", ubuf.scbuf) ;
828 psf_strlcpy (psf->cues->cue_points [n].name, sizeof (psf->cues->cue_points [n].name), ubuf.cbuf) ;
830 paiff->markstr [n].markerID = mark_id ;
831 paiff->markstr [n].position = position ;
833 ** TODO if ubuf.scbuf is equal to
834 ** either Beg_loop, Beg loop or beg loop and spam
835 ** if (psf->instrument == NULL && (psf->instrument = psf_instrument_alloc ()) == NULL)
836 ** return SFE_MALLOC_FAILED ;
841 psf_binheader_readf (psf, "j", chunk_size - bytesread) ;
845 found_chunk |= HAVE_FVER ;
846 /* Fall through to next case. */
849 psf_log_printf (psf, " %M : %d\n", marker, chunk_size) ;
850 psf_binheader_readf (psf, "j", chunk_size) ;
854 /* Fix for broken AIFC files with incorrect COMM chunk length. */
855 chunk_size = (chunk_size >> 24) - 3 ;
856 psf_log_printf (psf, " %M : %d\n", marker, chunk_size) ;
857 psf_binheader_readf (psf, "j", make_size_t (chunk_size)) ;
862 { psf_log_printf (psf, " %M : %d (should be >= 12)\n", marker, chunk_size) ;
863 psf_binheader_readf (psf, "j", chunk_size) ;
867 psf_log_printf (psf, " %M : %d\n", marker, chunk_size) ;
869 if ((error = aiff_read_chanmap (psf, chunk_size)))
874 if (chunk_size >= 0xffff0000)
876 psf_log_printf (psf, "*** Unknown chunk marker (%X) at position %D with length %u. Exiting parser.\n", marker, psf_ftell (psf) - 8, chunk_size) ;
880 if (psf_isprint ((marker >> 24) & 0xFF) && psf_isprint ((marker >> 16) & 0xFF)
881 && psf_isprint ((marker >> 8) & 0xFF) && psf_isprint (marker & 0xFF))
882 { psf_log_printf (psf, " %M : %u (unknown marker)\n", marker, chunk_size) ;
884 psf_binheader_readf (psf, "j", chunk_size) ;
888 if (psf_ftell (psf) & 0x03)
889 { psf_log_printf (psf, " Unknown chunk marker at position %D. Resynching.\n", psf_ftell (psf) - 8) ;
890 psf_binheader_readf (psf, "j", -3) ;
893 psf_log_printf (psf, "*** Unknown chunk marker %X at position %D. Exiting parser.\n", marker, psf_ftell (psf)) ;
896 } ; /* switch (marker) */
898 if (chunk_size >= psf->filelength)
899 { psf_log_printf (psf, "*** Chunk size %u > file length %D. Exiting parser.\n", chunk_size, psf->filelength) ;
903 if ((! psf->sf.seekable) && (found_chunk & HAVE_SSND))
906 if (psf_ftell (psf) >= psf->filelength - (2 * SIGNED_SIZEOF (int32_t)))
910 if (instr_found && mark_found)
911 { int ji, str_index ;
912 /* Next loop will convert markers to loop positions for internal handling */
913 for (ji = 0 ; ji < psf->instrument->loop_count ; ji ++)
914 { if (ji < ARRAY_LEN (psf->instrument->loops))
915 { psf->instrument->loops [ji].start = marker_to_position (paiff->markstr, psf->instrument->loops [ji].start, mark_count) ;
916 psf->instrument->loops [ji].end = marker_to_position (paiff->markstr, psf->instrument->loops [ji].end, mark_count) ;
917 psf->instrument->loops [ji].mode = SF_LOOP_FORWARD ;
921 /* The markers that correspond to loop positions can now be removed from cues struct */
922 if (psf->cues->cue_count > (uint32_t) (psf->instrument->loop_count * 2))
925 for (j = 0 ; j < psf->cues->cue_count - (uint32_t) (psf->instrument->loop_count * 2) ; j ++)
926 { /* This simply copies the information in cues above loop positions and writes it at current count instead */
927 psf->cues->cue_points [j].indx = psf->cues->cue_points [j + psf->instrument->loop_count * 2].indx ;
928 psf->cues->cue_points [j].position = psf->cues->cue_points [j + psf->instrument->loop_count * 2].position ;
929 psf->cues->cue_points [j].fcc_chunk = psf->cues->cue_points [j + psf->instrument->loop_count * 2].fcc_chunk ;
930 psf->cues->cue_points [j].chunk_start = psf->cues->cue_points [j + psf->instrument->loop_count * 2].chunk_start ;
931 psf->cues->cue_points [j].block_start = psf->cues->cue_points [j + psf->instrument->loop_count * 2].block_start ;
932 psf->cues->cue_points [j].sample_offset = psf->cues->cue_points [j + psf->instrument->loop_count * 2].sample_offset ;
933 for (str_index = 0 ; str_index < 256 ; str_index++)
934 psf->cues->cue_points [j].name [str_index] = psf->cues->cue_points [j + psf->instrument->loop_count * 2].name [str_index] ;
936 psf->cues->cue_count -= psf->instrument->loop_count * 2 ;
938 { /* All the cues were in fact loop positions so we can actually remove the cues altogether */
944 if (psf->sf.channels < 1)
945 return SFE_CHANNEL_COUNT_ZERO ;
947 if (psf->sf.channels >= SF_MAX_CHANNELS)
948 return SFE_CHANNEL_COUNT ;
950 if (! (found_chunk & HAVE_FORM))
951 return SFE_AIFF_NO_FORM ;
953 if (! (found_chunk & HAVE_AIFF))
954 return SFE_AIFF_COMM_NO_FORM ;
956 if (! (found_chunk & HAVE_COMM))
957 return SFE_AIFF_SSND_NO_COMM ;
959 if (! psf->dataoffset)
960 return SFE_AIFF_NO_DATA ;
963 } /* aiff_read_header */
966 aiff_close (SF_PRIVATE *psf)
967 { AIFF_PRIVATE *paiff = psf->container_data ;
969 if (paiff != NULL && paiff->markstr != NULL)
970 { free (paiff->markstr) ;
971 paiff->markstr = NULL ;
974 if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
975 { aiff_write_tailer (psf) ;
976 aiff_write_header (psf, SF_TRUE) ;
983 aiff_read_comm_chunk (SF_PRIVATE *psf, COMM_CHUNK *comm_fmt)
985 int subformat, samplerate ;
989 /* The COMM chunk has an int aligned to an odd word boundary. Some
990 ** procesors are not able to deal with this (ie bus fault) so we have
991 ** to take special care.
994 psf_binheader_readf (psf, "E242b", &(comm_fmt->numChannels), &(comm_fmt->numSampleFrames),
995 &(comm_fmt->sampleSize), &(comm_fmt->sampleRate), SIGNED_SIZEOF (comm_fmt->sampleRate)) ;
997 if (comm_fmt->size > 0x10000 && (comm_fmt->size & 0xffff) == 0)
998 { psf_log_printf (psf, " COMM : %d (0x%x) *** should be ", comm_fmt->size, comm_fmt->size) ;
999 comm_fmt->size = ENDSWAP_32 (comm_fmt->size) ;
1000 psf_log_printf (psf, "%d (0x%x)\n", comm_fmt->size, comm_fmt->size) ;
1003 psf_log_printf (psf, " COMM : %d\n", comm_fmt->size) ;
1005 if (comm_fmt->size == SIZEOF_AIFF_COMM)
1006 comm_fmt->encoding = NONE_MARKER ;
1007 else if (comm_fmt->size == SIZEOF_AIFC_COMM_MIN)
1008 psf_binheader_readf (psf, "Em", &(comm_fmt->encoding)) ;
1009 else if (comm_fmt->size >= SIZEOF_AIFC_COMM)
1010 { uint8_t encoding_len ;
1013 psf_binheader_readf (psf, "Em1", &(comm_fmt->encoding), &encoding_len) ;
1015 comm_fmt->size = SF_MIN (sizeof (ubuf.scbuf), make_size_t (comm_fmt->size)) ;
1016 memset (ubuf.scbuf, 0, comm_fmt->size) ;
1017 read_len = comm_fmt->size - SIZEOF_AIFC_COMM + 1 ;
1018 psf_binheader_readf (psf, "b", ubuf.scbuf, read_len) ;
1019 ubuf.scbuf [read_len + 1] = 0 ;
1022 samplerate = tenbytefloat2int (comm_fmt->sampleRate) ;
1024 psf_log_printf (psf, " Sample Rate : %d\n", samplerate) ;
1025 psf_log_printf (psf, " Frames : %u%s\n", comm_fmt->numSampleFrames, (comm_fmt->numSampleFrames == 0 && psf->filelength > 104) ? " (Should not be 0)" : "") ;
1027 if (comm_fmt->numChannels < 1 || comm_fmt->numChannels >= SF_MAX_CHANNELS)
1028 { psf_log_printf (psf, " Channels : %d (should be >= 1 and < %d)\n", comm_fmt->numChannels, SF_MAX_CHANNELS) ;
1029 return SFE_CHANNEL_COUNT_BAD ;
1032 psf_log_printf (psf, " Channels : %d\n", comm_fmt->numChannels) ;
1034 /* Found some broken 'fl32' files with comm.samplesize == 16. Fix it here. */
1035 if ((comm_fmt->encoding == fl32_MARKER || comm_fmt->encoding == FL32_MARKER) && comm_fmt->sampleSize != 32)
1036 { psf_log_printf (psf, " Sample Size : %d (should be 32)\n", comm_fmt->sampleSize) ;
1037 comm_fmt->sampleSize = 32 ;
1039 else if ((comm_fmt->encoding == fl64_MARKER || comm_fmt->encoding == FL64_MARKER) && comm_fmt->sampleSize != 64)
1040 { psf_log_printf (psf, " Sample Size : %d (should be 64)\n", comm_fmt->sampleSize) ;
1041 comm_fmt->sampleSize = 64 ;
1044 psf_log_printf (psf, " Sample Size : %d\n", comm_fmt->sampleSize) ;
1046 subformat = s_bitwidth_to_subformat (comm_fmt->sampleSize) ;
1048 psf->sf.samplerate = samplerate ;
1049 psf->sf.frames = comm_fmt->numSampleFrames ;
1050 psf->sf.channels = comm_fmt->numChannels ;
1051 psf->bytewidth = BITWIDTH2BYTES (comm_fmt->sampleSize) ;
1053 psf->endian = SF_ENDIAN_BIG ;
1055 switch (comm_fmt->encoding)
1056 { case NONE_MARKER :
1057 psf->sf.format = (SF_FORMAT_AIFF | subformat) ;
1063 psf->sf.format = (SF_ENDIAN_BIG | SF_FORMAT_AIFF | subformat) ;
1069 psf->endian = SF_ENDIAN_LITTLE ;
1070 psf->sf.format = (SF_ENDIAN_LITTLE | SF_FORMAT_AIFF | subformat) ;
1075 psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_FLOAT) ;
1080 psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_ULAW) ;
1085 psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_ALAW) ;
1090 psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_DOUBLE) ;
1094 psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_PCM_U8) ;
1098 psf->sf.format = SF_FORMAT_AIFF ;
1099 switch (comm_fmt->sampleSize)
1101 psf->sf.format |= SF_FORMAT_DWVW_12 ;
1104 psf->sf.format |= SF_FORMAT_DWVW_16 ;
1107 psf->sf.format |= SF_FORMAT_DWVW_24 ;
1111 psf->sf.format |= SF_FORMAT_DWVW_N ;
1117 psf->sf.format = SF_FORMAT_AIFF ;
1118 psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_GSM610) ;
1123 psf->endian = SF_ENDIAN_BIG ;
1124 psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_IMA_ADPCM) ;
1128 psf_log_printf (psf, "AIFC : Unimplemented format : %M\n", comm_fmt->encoding) ;
1129 return SFE_UNIMPLEMENTED ;
1132 if (! ubuf.scbuf [0])
1133 psf_log_printf (psf, " Encoding : %M\n", comm_fmt->encoding) ;
1135 psf_log_printf (psf, " Encoding : %M => %s\n", comm_fmt->encoding, ubuf.scbuf) ;
1138 } /* aiff_read_comm_chunk */
1141 /*==========================================================================================
1145 aiff_rewrite_header (SF_PRIVATE *psf)
1147 /* Assuming here that the header has already been written and just
1148 ** needs to be corrected for new data length. That means that we
1149 ** only change the length fields of the FORM and SSND chunks ;
1150 ** everything else can be skipped over.
1152 int k, ch, comm_size, comm_frames ;
1154 psf_fseek (psf, 0, SEEK_SET) ;
1155 psf_fread (psf->header.ptr, psf->dataoffset, 1, psf) ;
1157 psf->header.indx = 0 ;
1160 psf_binheader_writef (psf, "Etm8", BHWm (FORM_MARKER), BHW8 (psf->filelength - 8)) ;
1163 if ((k = psf_find_read_chunk_m32 (&psf->rchunks, COMM_MARKER)) >= 0)
1164 { psf->header.indx = psf->rchunks.chunks [k].offset - 8 ;
1165 comm_frames = psf->sf.frames ;
1166 comm_size = psf->rchunks.chunks [k].len ;
1167 psf_binheader_writef (psf, "Em42t4", BHWm (COMM_MARKER), BHW4 (comm_size), BHW2 (psf->sf.channels), BHW4 (comm_frames)) ;
1171 if ((k = psf_find_read_chunk_m32 (&psf->rchunks, PEAK_MARKER)) >= 0)
1172 { psf->header.indx = psf->rchunks.chunks [k].offset - 8 ;
1173 psf_binheader_writef (psf, "Em4", BHWm (PEAK_MARKER), BHW4 (AIFF_PEAK_CHUNK_SIZE (psf->sf.channels))) ;
1174 psf_binheader_writef (psf, "E44", BHW4 (1), BHW4 (time (NULL))) ;
1175 for (ch = 0 ; ch < psf->sf.channels ; ch++)
1176 psf_binheader_writef (psf, "Eft8", BHWf ((float) psf->peak_info->peaks [ch].value), BHW8 (psf->peak_info->peaks [ch].position)) ;
1181 if ((k = psf_find_read_chunk_m32 (&psf->rchunks, SSND_MARKER)) >= 0)
1182 { psf->header.indx = psf->rchunks.chunks [k].offset - 8 ;
1183 psf_binheader_writef (psf, "Etm8", BHWm (SSND_MARKER), BHW8 (psf->datalength + SIZEOF_SSND_CHUNK)) ;
1186 /* Header mangling complete so write it out. */
1187 psf_fseek (psf, 0, SEEK_SET) ;
1188 psf_fwrite (psf->header.ptr, psf->header.indx, 1, psf) ;
1191 } /* aiff_rewrite_header */
1194 convert_loop_mode (int type_mode)
1195 { switch (type_mode)
1196 { case SF_LOOP_NONE:
1198 case SF_LOOP_FORWARD :
1200 case SF_LOOP_ALTERNATING :
1206 } /* convert_loop_mode */
1209 aiff_write_header (SF_PRIVATE *psf, int calc_length)
1210 { sf_count_t current ;
1211 AIFF_PRIVATE *paiff ;
1212 uint8_t comm_sample_rate [10], comm_zero_bytes [2] = { 0, 0 } ;
1213 uint32_t comm_type, comm_size, comm_encoding, comm_frames = 0, uk ;
1214 int k, endian, has_data = SF_FALSE ;
1217 if ((paiff = psf->container_data) == NULL)
1218 return SFE_INTERNAL ;
1220 current = psf_ftell (psf) ;
1222 if (current > psf->dataoffset)
1223 has_data = SF_TRUE ;
1226 { psf->filelength = psf_get_filelen (psf) ;
1228 psf->datalength = psf->filelength - psf->dataoffset ;
1230 psf->datalength -= psf->filelength - psf->dataend ;
1232 if (psf->bytewidth > 0)
1233 psf->sf.frames = psf->datalength / (psf->bytewidth * psf->sf.channels) ;
1236 if (psf->file.mode == SFM_RDWR && psf->dataoffset > 0 && psf->rchunks.count > 0)
1237 { aiff_rewrite_header (psf) ;
1239 psf_fseek (psf, current, SEEK_SET) ;
1243 endian = SF_ENDIAN (psf->sf.format) ;
1244 if (CPU_IS_LITTLE_ENDIAN && endian == SF_ENDIAN_CPU)
1245 endian = SF_ENDIAN_LITTLE ;
1247 /* Standard value here. */
1248 bit_width = psf->bytewidth * 8 ;
1249 comm_frames = (psf->sf.frames > 0xFFFFFFFF) ? 0xFFFFFFFF : psf->sf.frames ;
1251 switch (SF_CODEC (psf->sf.format) | endian)
1252 { case SF_FORMAT_PCM_S8 | SF_ENDIAN_BIG :
1253 psf->endian = SF_ENDIAN_BIG ;
1254 comm_type = AIFC_MARKER ;
1255 comm_size = SIZEOF_AIFC_COMM ;
1256 comm_encoding = twos_MARKER ;
1259 case SF_FORMAT_PCM_S8 | SF_ENDIAN_LITTLE :
1260 psf->endian = SF_ENDIAN_LITTLE ;
1261 comm_type = AIFC_MARKER ;
1262 comm_size = SIZEOF_AIFC_COMM ;
1263 comm_encoding = sowt_MARKER ;
1266 case SF_FORMAT_PCM_16 | SF_ENDIAN_BIG :
1267 psf->endian = SF_ENDIAN_BIG ;
1268 comm_type = AIFC_MARKER ;
1269 comm_size = SIZEOF_AIFC_COMM ;
1270 comm_encoding = twos_MARKER ;
1273 case SF_FORMAT_PCM_16 | SF_ENDIAN_LITTLE :
1274 psf->endian = SF_ENDIAN_LITTLE ;
1275 comm_type = AIFC_MARKER ;
1276 comm_size = SIZEOF_AIFC_COMM ;
1277 comm_encoding = sowt_MARKER ;
1280 case SF_FORMAT_PCM_24 | SF_ENDIAN_BIG :
1281 psf->endian = SF_ENDIAN_BIG ;
1282 comm_type = AIFC_MARKER ;
1283 comm_size = SIZEOF_AIFC_COMM ;
1284 comm_encoding = in24_MARKER ;
1287 case SF_FORMAT_PCM_24 | SF_ENDIAN_LITTLE :
1288 psf->endian = SF_ENDIAN_LITTLE ;
1289 comm_type = AIFC_MARKER ;
1290 comm_size = SIZEOF_AIFC_COMM ;
1291 comm_encoding = ni24_MARKER ;
1294 case SF_FORMAT_PCM_32 | SF_ENDIAN_BIG :
1295 psf->endian = SF_ENDIAN_BIG ;
1296 comm_type = AIFC_MARKER ;
1297 comm_size = SIZEOF_AIFC_COMM ;
1298 comm_encoding = in32_MARKER ;
1301 case SF_FORMAT_PCM_32 | SF_ENDIAN_LITTLE :
1302 psf->endian = SF_ENDIAN_LITTLE ;
1303 comm_type = AIFC_MARKER ;
1304 comm_size = SIZEOF_AIFC_COMM ;
1305 comm_encoding = ni32_MARKER ;
1308 case SF_FORMAT_PCM_S8 : /* SF_ENDIAN_FILE */
1309 case SF_FORMAT_PCM_16 :
1310 case SF_FORMAT_PCM_24 :
1311 case SF_FORMAT_PCM_32 :
1312 psf->endian = SF_ENDIAN_BIG ;
1313 comm_type = AIFF_MARKER ;
1314 comm_size = SIZEOF_AIFF_COMM ;
1318 case SF_FORMAT_FLOAT : /* Big endian floating point. */
1319 psf->endian = SF_ENDIAN_BIG ;
1320 comm_type = AIFC_MARKER ;
1321 comm_size = SIZEOF_AIFC_COMM ;
1322 comm_encoding = FL32_MARKER ; /* Use 'FL32' because its easier to read. */
1325 case SF_FORMAT_DOUBLE : /* Big endian double precision floating point. */
1326 psf->endian = SF_ENDIAN_BIG ;
1327 comm_type = AIFC_MARKER ;
1328 comm_size = SIZEOF_AIFC_COMM ;
1329 comm_encoding = FL64_MARKER ; /* Use 'FL64' because its easier to read. */
1332 case SF_FORMAT_ULAW :
1333 psf->endian = SF_ENDIAN_BIG ;
1334 comm_type = AIFC_MARKER ;
1335 comm_size = SIZEOF_AIFC_COMM ;
1336 comm_encoding = ulaw_MARKER ;
1339 case SF_FORMAT_ALAW :
1340 psf->endian = SF_ENDIAN_BIG ;
1341 comm_type = AIFC_MARKER ;
1342 comm_size = SIZEOF_AIFC_COMM ;
1343 comm_encoding = alaw_MARKER ;
1346 case SF_FORMAT_PCM_U8 :
1347 psf->endian = SF_ENDIAN_BIG ;
1348 comm_type = AIFC_MARKER ;
1349 comm_size = SIZEOF_AIFC_COMM ;
1350 comm_encoding = raw_MARKER ;
1353 case SF_FORMAT_DWVW_12 :
1354 psf->endian = SF_ENDIAN_BIG ;
1355 comm_type = AIFC_MARKER ;
1356 comm_size = SIZEOF_AIFC_COMM ;
1357 comm_encoding = DWVW_MARKER ;
1359 /* Override standard value here.*/
1363 case SF_FORMAT_DWVW_16 :
1364 psf->endian = SF_ENDIAN_BIG ;
1365 comm_type = AIFC_MARKER ;
1366 comm_size = SIZEOF_AIFC_COMM ;
1367 comm_encoding = DWVW_MARKER ;
1369 /* Override standard value here.*/
1373 case SF_FORMAT_DWVW_24 :
1374 psf->endian = SF_ENDIAN_BIG ;
1375 comm_type = AIFC_MARKER ;
1376 comm_size = SIZEOF_AIFC_COMM ;
1377 comm_encoding = DWVW_MARKER ;
1379 /* Override standard value here.*/
1383 case SF_FORMAT_GSM610 :
1384 psf->endian = SF_ENDIAN_BIG ;
1385 comm_type = AIFC_MARKER ;
1386 comm_size = SIZEOF_AIFC_COMM ;
1387 comm_encoding = GSM_MARKER ;
1389 /* Override standard value here.*/
1393 case SF_FORMAT_IMA_ADPCM :
1394 psf->endian = SF_ENDIAN_BIG ;
1395 comm_type = AIFC_MARKER ;
1396 comm_size = SIZEOF_AIFC_COMM ;
1397 comm_encoding = ima4_MARKER ;
1399 /* Override standard value here.*/
1401 comm_frames = psf->sf.frames / AIFC_IMA4_SAMPLES_PER_BLOCK ;
1404 default : return SFE_BAD_OPEN_FORMAT ;
1407 /* Reset the current header length to zero. */
1408 psf->header.ptr [0] = 0 ;
1409 psf->header.indx = 0 ;
1410 psf_fseek (psf, 0, SEEK_SET) ;
1412 psf_binheader_writef (psf, "Etm8", BHWm (FORM_MARKER), BHW8 (psf->filelength - 8)) ;
1414 /* Write AIFF/AIFC marker and COM chunk. */
1415 if (comm_type == AIFC_MARKER)
1416 /* AIFC must have an FVER chunk. */
1417 psf_binheader_writef (psf, "Emm44", BHWm (comm_type), BHWm (FVER_MARKER), BHW4 (4), BHW4 (0xA2805140)) ;
1419 psf_binheader_writef (psf, "Em", BHWm (comm_type)) ;
1421 paiff->comm_offset = psf->header.indx - 8 ;
1423 memset (comm_sample_rate, 0, sizeof (comm_sample_rate)) ;
1424 uint2tenbytefloat (psf->sf.samplerate, comm_sample_rate) ;
1426 psf_binheader_writef (psf, "Em42t42", BHWm (COMM_MARKER), BHW4 (comm_size), BHW2 (psf->sf.channels), BHW4 (comm_frames), BHW2 (bit_width)) ;
1427 psf_binheader_writef (psf, "b", BHWv (comm_sample_rate), BHWz (sizeof (comm_sample_rate))) ;
1429 /* AIFC chunks have some extra data. */
1430 if (comm_type == AIFC_MARKER)
1431 psf_binheader_writef (psf, "mb", BHWm (comm_encoding), BHWv (comm_zero_bytes), BHWz (sizeof (comm_zero_bytes))) ;
1433 if (psf->channel_map && paiff->chanmap_tag)
1434 psf_binheader_writef (psf, "Em4444", BHWm (CHAN_MARKER), BHW4 (12), BHW4 (paiff->chanmap_tag), BHW4 (0), BHW4 (0)) ;
1436 /* Check if there's a INST chunk to write */
1437 if (psf->instrument != NULL && psf->cues != NULL)
1438 { /* Both loops and cues exist */
1439 uint16_t sustainLoopMode, releaseLoopMode ;
1440 uint32_t idx, sLoopStart = 0, sLoopEnd = 0, rLoopStart = 0, rLoopEnd = 0 ;
1441 int totalStringLength = 0, stringLength ;
1443 /* Here we count how many bytes will the pascal strings need */
1444 for (idx = 0 ; idx < psf->cues->cue_count ; idx++)
1445 { stringLength = strlen (psf->cues->cue_points [idx].name) + 1 ; /* We'll count the first byte also of every pascal string */
1446 if (stringLength % 2 == 0)
1447 totalStringLength += stringLength ;
1449 totalStringLength += (stringLength + 1) ; /* The pascal string must have an even count */
1452 /* First we check which loops are active and create the necessary MARK chunk for markers */
1453 /* The first written markers will be references from loop points then comes the real markers */
1454 if (psf->instrument->loops [0].mode != SF_LOOP_NONE && psf->instrument->loops [1].mode != SF_LOOP_NONE)
1455 { /* There's both a sustain loop and a release loop */
1456 psf_binheader_writef (psf, "Em42 241b 241b 241b 241b",
1457 MARK_MARKER, 2 + 2 * (2 + 4 + 1 + 19) + 2 * (2 + 4 + 1 + 17) + psf->cues->cue_count * (2 + 4) + totalStringLength, 4 + psf->cues->cue_count,
1458 1, psf->instrument->loops [0].start, 18, "sustain loop start", make_size_t (19),
1459 2, psf->instrument->loops [0].end, 16, "sustain loop end", make_size_t (17),
1460 3, psf->instrument->loops [1].start, 18, "release loop start", make_size_t (19),
1461 4, psf->instrument->loops [1].end, 16, "release loop end", make_size_t (17)) ;
1462 /* Now comes true markers from cues struct */
1463 for (idx = 0 ; idx < psf->cues->cue_count ; idx++)
1464 psf_binheader_writef (psf, "E24p", 5 + idx, psf->cues->cue_points [idx].sample_offset, psf->cues->cue_points [idx].name) ;
1466 /* Change the loops to be references to the markers */
1472 else if (psf->instrument->loops [0].mode != SF_LOOP_NONE && psf->instrument->loops [1].mode == SF_LOOP_NONE)
1473 { /* There's a sustain loop but no release loop */
1474 psf_binheader_writef (psf, "Em42241b241b",
1475 MARK_MARKER, 2 + (2 + 4 + 1 + 19) + (2 + 4 + 1 + 17) + psf->cues->cue_count * (2 + 4) + totalStringLength, 2 + psf->cues->cue_count,
1476 1, psf->instrument->loops [0].start, 18, "sustain loop start", make_size_t (19),
1477 2, psf->instrument->loops [0].end, 16, "sustain loop end", make_size_t (17)) ;
1478 /* Now comes true markers from cues struct */
1479 for (idx = 0 ; idx < psf->cues->cue_count ; idx++)
1480 psf_binheader_writef (psf, "E24p", 3 + idx, psf->cues->cue_points [idx].sample_offset, psf->cues->cue_points [idx].name) ;
1482 /* Change the loops to be references to the markers */
1488 else if (psf->instrument->loops [0].mode == SF_LOOP_NONE && psf->instrument->loops [1].mode != SF_LOOP_NONE)
1489 { /* There's a release loop but no sustain loop! Strange indeed! */
1490 psf_binheader_writef (psf, "Em42241b241b",
1491 MARK_MARKER, 2 + (2 + 4 + 1 + 19) + (2 + 4 + 1 + 17) + psf->cues->cue_count * (2 + 4) + totalStringLength, 2 + psf->cues->cue_count,
1492 1, psf->instrument->loops [1].start, 18, "release loop start", make_size_t (19),
1493 2, psf->instrument->loops [1].end, 16, "release loop end", make_size_t (17)) ;
1494 /* Now comes true markers from cues struct */
1495 for (idx = 0 ; idx < psf->cues->cue_count ; idx++)
1496 psf_binheader_writef (psf, "E24p", 3 + idx, psf->cues->cue_points [idx].sample_offset, psf->cues->cue_points [idx].name) ;
1498 /* Change the loops to be references to the markers */
1505 /* First convert loop modes to aiff standard */
1506 sustainLoopMode = convert_loop_mode (psf->instrument->loops [0].mode) ;
1507 releaseLoopMode = convert_loop_mode (psf->instrument->loops [1].mode) ;
1509 /* Now we finally write the actual INST chunk */
1510 psf_binheader_writef (psf, "Em4111111", INST_MARKER, SIZEOF_INST_CHUNK, psf->instrument->basenote, psf->instrument->detune,
1511 psf->instrument->key_lo, psf->instrument->key_hi, psf->instrument->velocity_lo, psf->instrument->velocity_hi) ;
1512 psf_binheader_writef (psf, "E2222222", (short) psf->instrument->gain,
1513 sustainLoopMode, sLoopStart, sLoopEnd,
1514 releaseLoopMode, rLoopStart, rLoopEnd) ;
1517 else if (psf->instrument != NULL && psf->cues == NULL)
1518 { /* There are loops but no cues */
1519 uint16_t sustainLoopMode, releaseLoopMode ;
1520 uint32_t sLoopStart = 0, sLoopEnd = 0, rLoopStart = 0, rLoopEnd = 0 ;
1522 /* First we check which loops are active and create the necessary MARK chunk for markers */
1523 if (psf->instrument->loops [0].mode != SF_LOOP_NONE && psf->instrument->loops [1].mode != SF_LOOP_NONE)
1524 { /* There's both a sustain loop and a release loop */
1525 psf_binheader_writef (psf, "Em42 241b 241b 241b 241b",
1526 MARK_MARKER, 2 + 2 * (2 + 4 + 1 + 19) + 2 * (2 + 4 + 1 + 17), 4,
1527 1, psf->instrument->loops [0].start, 18, "sustain loop start", make_size_t (19),
1528 2, psf->instrument->loops [0].end, 16, "sustain loop end", make_size_t (17),
1529 3, psf->instrument->loops [1].start, 18, "release loop start", make_size_t (19),
1530 4, psf->instrument->loops [1].end, 16, "release loop end", make_size_t (17)) ;
1531 /* Change the loops to be references to the markers */
1537 else if (psf->instrument->loops [0].mode != SF_LOOP_NONE && psf->instrument->loops [1].mode == SF_LOOP_NONE)
1538 { /* There's a sustain loop but no release loop */
1539 psf_binheader_writef (psf, "Em42241b241b",
1540 MARK_MARKER, 2 + (2 + 4 + 1 + 19) + (2 + 4 + 1 + 17), 2,
1541 1, psf->instrument->loops [0].start, 18, "sustain loop start", make_size_t (19),
1542 2, psf->instrument->loops [0].end, 16, "sustain loop end", make_size_t (17)) ;
1543 /* Change the loops to be references to the markers */
1549 else if (psf->instrument->loops [0].mode == SF_LOOP_NONE && psf->instrument->loops [1].mode != SF_LOOP_NONE)
1550 { /* There's a release loop but no sustain loop! Strange indeed! */
1551 psf_binheader_writef (psf, "Em42241b241b",
1552 MARK_MARKER, 2 + (2 + 4 + 1 + 19) + (2 + 4 + 1 + 17), 2,
1553 1, psf->instrument->loops [1].start, 18, "release loop start", make_size_t (19),
1554 2, psf->instrument->loops [1].end, 16, "release loop end", make_size_t (17)) ;
1555 /* Change the loops to be references to the markers */
1562 /* First convert loop modes to aiff standard */
1563 sustainLoopMode = convert_loop_mode (psf->instrument->loops [0].mode) ;
1564 releaseLoopMode = convert_loop_mode (psf->instrument->loops [1].mode) ;
1566 /* Now we finally write the actual INST chunk */
1567 psf_binheader_writef (psf, "Em4111111", INST_MARKER, SIZEOF_INST_CHUNK, psf->instrument->basenote, psf->instrument->detune,
1568 psf->instrument->key_lo, psf->instrument->key_hi, psf->instrument->velocity_lo, psf->instrument->velocity_hi) ;
1569 psf_binheader_writef (psf, "E2222222", (short) psf->instrument->gain,
1570 sustainLoopMode, sLoopStart, sLoopEnd,
1571 releaseLoopMode, rLoopStart, rLoopEnd) ;
1574 else if (psf->instrument == NULL && psf->cues != NULL)
1575 { /* There are cues but no loops */
1577 int totalStringLength = 0, stringLength ;
1579 /* Here we count how many bytes will the pascal strings need */
1580 for (idx = 0 ; idx < psf->cues->cue_count ; idx++)
1581 { stringLength = strlen (psf->cues->cue_points [idx].name) + 1 ; /* We'll count the first byte also of every pascal string */
1582 totalStringLength += stringLength + (stringLength % 2 == 0 ? 0 : 1) ;
1585 psf_binheader_writef (psf, "Em42",
1586 BHWm (MARK_MARKER), BHW4 (2 + psf->cues->cue_count * (2 + 4) + totalStringLength), BHW2 (psf->cues->cue_count)) ;
1588 for (idx = 0 ; idx < psf->cues->cue_count ; idx++)
1589 psf_binheader_writef (psf, "E24p", BHW2 (psf->cues->cue_points [idx].indx), BHW4 (psf->cues->cue_points [idx].sample_offset), BHWp (psf->cues->cue_points [idx].name)) ;
1592 if (psf->strings.flags & SF_STR_LOCATE_START)
1593 aiff_write_strings (psf, SF_STR_LOCATE_START) ;
1595 if (psf->peak_info != NULL && psf->peak_info->peak_loc == SF_PEAK_START)
1596 { psf_binheader_writef (psf, "Em4", BHWm (PEAK_MARKER), BHW4 (AIFF_PEAK_CHUNK_SIZE (psf->sf.channels))) ;
1597 psf_binheader_writef (psf, "E44", BHW4 (1), BHW4 (time (NULL))) ;
1598 for (k = 0 ; k < psf->sf.channels ; k++)
1599 psf_binheader_writef (psf, "Eft8", BHWf ((float) psf->peak_info->peaks [k].value), BHW8 (psf->peak_info->peaks [k].position)) ;
1602 /* Write custom headers. */
1603 for (uk = 0 ; uk < psf->wchunks.used ; uk++)
1604 psf_binheader_writef (psf, "Em4b", BHWm (psf->wchunks.chunks [uk].mark32), BHW4 (psf->wchunks.chunks [uk].len), BHWv (psf->wchunks.chunks [uk].data), BHWz (psf->wchunks.chunks [uk].len)) ;
1606 /* Write SSND chunk. */
1607 paiff->ssnd_offset = psf->header.indx ;
1608 psf_binheader_writef (psf, "Etm844", BHWm (SSND_MARKER), BHW8 (psf->datalength + SIZEOF_SSND_CHUNK), BHW4 (0), BHW4 (0)) ;
1610 /* Header construction complete so write it out. */
1611 psf_fwrite (psf->header.ptr, psf->header.indx, 1, psf) ;
1616 if (has_data && psf->dataoffset != psf->header.indx)
1617 return psf->error = SFE_INTERNAL ;
1619 psf->dataoffset = psf->header.indx ;
1622 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
1623 else if (current > 0)
1624 psf_fseek (psf, current, SEEK_SET) ;
1627 } /* aiff_write_header */
1630 aiff_write_tailer (SF_PRIVATE *psf)
1633 /* Reset the current header length to zero. */
1634 psf->header.ptr [0] = 0 ;
1635 psf->header.indx = 0 ;
1637 psf->dataend = psf_fseek (psf, 0, SEEK_END) ;
1639 /* Make sure tailer data starts at even byte offset. Pad if necessary. */
1640 if (psf->dataend % 2 == 1)
1641 { psf_fwrite (psf->header.ptr, 1, 1, psf) ;
1645 if (psf->peak_info != NULL && psf->peak_info->peak_loc == SF_PEAK_END)
1646 { psf_binheader_writef (psf, "Em4", BHWm (PEAK_MARKER), BHW4 (AIFF_PEAK_CHUNK_SIZE (psf->sf.channels))) ;
1647 psf_binheader_writef (psf, "E44", BHW4 (1), BHW4 (time (NULL))) ;
1648 for (k = 0 ; k < psf->sf.channels ; k++)
1649 psf_binheader_writef (psf, "Eft8", BHWf ((float) psf->peak_info->peaks [k].value), BHW8 (psf->peak_info->peaks [k].position)) ;
1652 if (psf->strings.flags & SF_STR_LOCATE_END)
1653 aiff_write_strings (psf, SF_STR_LOCATE_END) ;
1655 /* Write the tailer. */
1656 if (psf->header.indx > 0)
1657 psf_fwrite (psf->header.ptr, psf->header.indx, 1, psf) ;
1660 } /* aiff_write_tailer */
1663 aiff_write_strings (SF_PRIVATE *psf, int location)
1666 for (k = 0 ; k < SF_MAX_STRINGS ; k++)
1667 { if (psf->strings.data [k].type == 0)
1670 if (psf->strings.data [k].flags != location)
1673 switch (psf->strings.data [k].type)
1674 { case SF_STR_SOFTWARE :
1675 slen = strlen (psf->strings.storage + psf->strings.data [k].offset) ;
1676 psf_binheader_writef (psf, "Em4mb", BHWm (APPL_MARKER), BHW4 (slen + 4), BHWm (m3ga_MARKER), BHWv (psf->strings.storage + psf->strings.data [k].offset), BHWz (slen + (slen & 1))) ;
1680 psf_binheader_writef (psf, "EmS", BHWm (NAME_MARKER), BHWS (psf->strings.storage + psf->strings.data [k].offset)) ;
1683 case SF_STR_COPYRIGHT :
1684 psf_binheader_writef (psf, "EmS", BHWm (c_MARKER), BHWS (psf->strings.storage + psf->strings.data [k].offset)) ;
1687 case SF_STR_ARTIST :
1688 psf_binheader_writef (psf, "EmS", BHWm (AUTH_MARKER), BHWS (psf->strings.storage + psf->strings.data [k].offset)) ;
1691 case SF_STR_COMMENT :
1692 psf_binheader_writef (psf, "EmS", BHWm (ANNO_MARKER), BHWS (psf->strings.storage + psf->strings.data [k].offset)) ;
1697 psf_binheader_writef (psf, "Ems", BHWm (ICRD_MARKER), BHWs (psf->strings.data [k].str)) ;
1704 } /* aiff_write_strings */
1707 aiff_command (SF_PRIVATE * psf, int command, void * UNUSED (data), int UNUSED (datasize))
1708 { AIFF_PRIVATE *paiff ;
1710 if ((paiff = psf->container_data) == NULL)
1711 return SFE_INTERNAL ;
1714 { case SFC_SET_CHANNEL_MAP_INFO :
1715 paiff->chanmap_tag = aiff_caf_find_channel_layout_tag (psf->channel_map, psf->sf.channels) ;
1716 return (paiff->chanmap_tag != 0) ;
1723 } /* aiff_command */
1726 get_loop_mode_str (int16_t mode)
1728 { case 0 : return "none" ;
1729 case 1 : return "forward" ;
1730 case 2 : return "backward" ;
1733 return "*** unknown" ;
1734 } /* get_loop_mode_str */
1737 get_loop_mode (int16_t mode)
1739 { case 0 : return SF_LOOP_NONE ;
1740 case 1 : return SF_LOOP_FORWARD ;
1741 case 2 : return SF_LOOP_BACKWARD ;
1744 return SF_LOOP_NONE ;
1745 } /* get_loop_mode */
1747 /*==========================================================================================
1748 ** Rough hack at converting from 80 bit IEEE float in AIFF header to an int and
1749 ** back again. It assumes that all sample rates are between 1 and 800MHz, which
1750 ** should be OK as other sound file formats use a 32 bit integer to store sample
1752 ** There is another (probably better) version in the source code to the SoX but it
1753 ** has a copyright which probably prevents it from being allowable as GPL/LGPL.
1757 tenbytefloat2int (uint8_t *bytes)
1760 if (bytes [0] & 0x80) /* Negative number. */
1763 if (bytes [0] <= 0x3F) /* Less than 1. */
1766 if (bytes [0] > 0x40) /* Way too big. */
1769 if (bytes [0] == 0x40 && bytes [1] > 0x1C) /* Too big. */
1772 /* Ok, can handle it. */
1774 val = (bytes [2] << 23) | (bytes [3] << 15) | (bytes [4] << 7) | (bytes [5] >> 1) ;
1776 val >>= (29 - bytes [1]) ;
1779 } /* tenbytefloat2int */
1782 uint2tenbytefloat (uint32_t num, uint8_t *bytes)
1783 { uint32_t mask = 0x40000000 ;
1787 { bytes [0] = 0x3F ;
1796 { bytes [1] = 0x1D ;
1800 for (count = 0 ; count < 32 ; count ++)
1806 num = count < 31 ? num << (count + 1) : 0 ;
1807 bytes [1] = 29 - count ;
1808 bytes [2] = (num >> 24) & 0xFF ;
1809 bytes [3] = (num >> 16) & 0xFF ;
1810 bytes [4] = (num >> 8) & 0xFF ;
1811 bytes [5] = num & 0xFF ;
1813 } /* uint2tenbytefloat */
1816 aiff_read_basc_chunk (SF_PRIVATE * psf, int datasize)
1817 { const char * type_str ;
1821 count = psf_binheader_readf (psf, "E442", &bc.version, &bc.numBeats, &bc.rootNote) ;
1822 count += psf_binheader_readf (psf, "E222", &bc.scaleType, &bc.sigNumerator, &bc.sigDenominator) ;
1823 count += psf_binheader_readf (psf, "E2j", &bc.loopType, datasize - sizeof (bc)) ;
1825 psf_log_printf (psf, " Version ? : %u\n Num Beats : %u\n Root Note : 0x%x\n",
1826 bc.version, bc.numBeats, bc.rootNote) ;
1828 switch (bc.scaleType)
1829 { case basc_SCALE_MINOR :
1830 type_str = "MINOR" ;
1832 case basc_SCALE_MAJOR :
1833 type_str = "MAJOR" ;
1835 case basc_SCALE_NEITHER :
1836 type_str = "NEITHER" ;
1838 case basc_SCALE_BOTH :
1842 type_str = "!!WRONG!!" ;
1846 psf_log_printf (psf, " ScaleType : 0x%x (%s)\n", bc.scaleType, type_str) ;
1847 psf_log_printf (psf, " Time Sig : %d/%d\n", bc.sigNumerator, bc.sigDenominator) ;
1849 switch (bc.loopType)
1850 { case basc_TYPE_ONE_SHOT :
1851 type_str = "One Shot" ;
1853 case basc_TYPE_LOOP :
1857 type_str = "!!WRONG!!" ;
1861 psf_log_printf (psf, " Loop Type : 0x%x (%s)\n", bc.loopType, type_str) ;
1863 if ((psf->loop_info = calloc (1, sizeof (SF_LOOP_INFO))) == NULL)
1864 return SFE_MALLOC_FAILED ;
1866 psf->loop_info->time_sig_num = bc.sigNumerator ;
1867 psf->loop_info->time_sig_den = bc.sigDenominator ;
1868 psf->loop_info->loop_mode = (bc.loopType == basc_TYPE_ONE_SHOT) ? SF_LOOP_NONE : SF_LOOP_FORWARD ;
1869 psf->loop_info->num_beats = bc.numBeats ;
1871 /* Can always be recalculated from other known fields. */
1872 psf->loop_info->bpm = (1.0 / psf->sf.frames) * psf->sf.samplerate
1873 * ((bc.numBeats * 4.0) / bc.sigDenominator) * 60.0 ;
1874 psf->loop_info->root_key = bc.rootNote ;
1876 if (count < datasize)
1877 psf_binheader_readf (psf, "j", datasize - count) ;
1880 } /* aiff_read_basc_chunk */
1884 aiff_read_chanmap (SF_PRIVATE * psf, unsigned dword)
1885 { const AIFF_CAF_CHANNEL_MAP * map_info ;
1886 unsigned channel_bitmap, channel_decriptions, bytesread ;
1889 bytesread = psf_binheader_readf (psf, "444", &layout_tag, &channel_bitmap, &channel_decriptions) ;
1891 if ((map_info = aiff_caf_of_channel_layout_tag (layout_tag)) == NULL)
1894 psf_log_printf (psf, " Tag : %x\n", layout_tag) ;
1896 psf_log_printf (psf, " Layout : %s\n", map_info->name) ;
1898 if (bytesread < dword)
1899 psf_binheader_readf (psf, "j", dword - bytesread) ;
1901 if (map_info->channel_map != NULL)
1902 { size_t chanmap_size = SF_MIN (psf->sf.channels, layout_tag & 0xffff) * sizeof (psf->channel_map [0]) ;
1904 free (psf->channel_map) ;
1906 if ((psf->channel_map = malloc (chanmap_size)) == NULL)
1907 return SFE_MALLOC_FAILED ;
1909 memcpy (psf->channel_map, map_info->channel_map, chanmap_size) ;
1913 } /* aiff_read_chanmap */
1915 /*==============================================================================
1919 aiff_set_chunk (SF_PRIVATE *psf, const SF_CHUNK_INFO * chunk_info)
1920 { return psf_save_write_chunk (&psf->wchunks, chunk_info) ;
1921 } /* aiff_set_chunk */
1923 static SF_CHUNK_ITERATOR *
1924 aiff_next_chunk_iterator (SF_PRIVATE *psf, SF_CHUNK_ITERATOR * iterator)
1925 { return psf_next_chunk_iterator (&psf->rchunks, iterator) ;
1926 } /* aiff_next_chunk_iterator */
1929 aiff_get_chunk_size (SF_PRIVATE *psf, const SF_CHUNK_ITERATOR * iterator, SF_CHUNK_INFO * chunk_info)
1932 if ((indx = psf_find_read_chunk_iterator (&psf->rchunks, iterator)) < 0)
1933 return SFE_UNKNOWN_CHUNK ;
1935 chunk_info->datalen = psf->rchunks.chunks [indx].len ;
1937 return SFE_NO_ERROR ;
1938 } /* aiff_get_chunk_size */
1941 aiff_get_chunk_data (SF_PRIVATE *psf, const SF_CHUNK_ITERATOR * iterator, SF_CHUNK_INFO * chunk_info)
1945 if ((indx = psf_find_read_chunk_iterator (&psf->rchunks, iterator)) < 0)
1946 return SFE_UNKNOWN_CHUNK ;
1948 if (chunk_info->data == NULL)
1949 return SFE_BAD_CHUNK_DATA_PTR ;
1951 chunk_info->id_size = psf->rchunks.chunks [indx].id_size ;
1952 memcpy (chunk_info->id, psf->rchunks.chunks [indx].id, sizeof (chunk_info->id) / sizeof (*chunk_info->id)) ;
1954 pos = psf_ftell (psf) ;
1955 psf_fseek (psf, psf->rchunks.chunks [indx].offset, SEEK_SET) ;
1956 psf_fread (chunk_info->data, SF_MIN (chunk_info->datalen, psf->rchunks.chunks [indx].len), 1, psf) ;
1957 psf_fseek (psf, pos, SEEK_SET) ;
1959 return SFE_NO_ERROR ;
1960 } /* aiff_get_chunk_data */