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 case SF_FORMAT_GSM610 :
363 error = gsm610_init (psf) ;
364 if (psf->sf.frames > comm_fmt.numSampleFrames)
365 psf->sf.frames = comm_fmt.numSampleFrames ;
368 default : return SFE_UNIMPLEMENTED ;
371 if (psf->file.mode != SFM_WRITE && psf->sf.frames - comm_fmt.numSampleFrames != 0)
372 { psf_log_printf (psf,
373 "*** Frame count read from 'COMM' chunk (%u) not equal to frame count\n"
374 "*** calculated from length of 'SSND' chunk (%u).\n",
375 comm_fmt.numSampleFrames, (uint32_t) psf->sf.frames) ;
381 /*==========================================================================================
382 ** Private functions.
385 /* This function ought to check size */
387 marker_to_position (const MARK_ID_POS *m, uint16_t n, int marksize)
390 for (i = 0 ; i < marksize ; i++)
391 if (m [i].markerID == n)
392 return m [i].position ;
394 } /* marker_to_position */
397 aiff_read_header (SF_PRIVATE *psf, COMM_CHUNK *comm_fmt)
398 { SSND_CHUNK ssnd_fmt ;
399 AIFF_PRIVATE *paiff ;
401 uint32_t chunk_size = 0, FORMsize, SSNDsize, bytesread, mark_count = 0 ;
402 int k, found_chunk = 0, done = 0, error = 0 ;
404 int instr_found = 0, mark_found = 0 ;
406 if (psf->filelength > SF_PLATFORM_S64 (0xffffffff))
407 psf_log_printf (psf, "Warning : filelength > 0xffffffff. This is bad!!!!\n") ;
409 if ((paiff = psf->container_data) == NULL)
410 return SFE_INTERNAL ;
412 paiff->comm_offset = 0 ;
413 paiff->ssnd_offset = 0 ;
415 /* Set position to start of file to begin reading header. */
416 psf_binheader_readf (psf, "p", 0) ;
418 memset (comm_fmt, 0, sizeof (COMM_CHUNK)) ;
420 /* Until recently AIF* file were all BIG endian. */
421 psf->endian = SF_ENDIAN_BIG ;
423 /* AIFF files can apparently have their chunks in any order. However, they
424 ** must have a FORM chunk. Approach here is to read all the chunks one by
425 ** one and then check for the mandatory chunks at the end.
429 size_t jump = chunk_size & 1 ;
431 marker = chunk_size = 0 ;
432 psf_binheader_readf (psf, "Ejm4", jump, &marker, &chunk_size) ;
434 { sf_count_t pos = psf_ftell (psf) ;
435 psf_log_printf (psf, "Have 0 marker at position %D (0x%x).\n", pos, pos) ;
439 if (psf->file.mode == SFM_RDWR && (found_chunk & HAVE_SSND))
440 return SFE_AIFF_RW_SSND_NOT_LAST ;
442 psf_store_read_chunk_u32 (&psf->rchunks, marker, psf_ftell (psf), chunk_size) ;
447 return SFE_AIFF_NO_FORM ;
449 FORMsize = chunk_size ;
451 found_chunk |= HAVE_FORM ;
452 psf_binheader_readf (psf, "m", &marker) ;
456 found_chunk |= (marker == AIFC_MARKER) ? (HAVE_AIFC | HAVE_AIFF) : HAVE_AIFF ;
462 if (psf->fileoffset > 0 && psf->filelength > FORMsize + 8)
463 { /* Set file length. */
464 psf->filelength = FORMsize + 8 ;
465 psf_log_printf (psf, "FORM : %u\n %M\n", FORMsize, marker) ;
467 else if (FORMsize != psf->filelength - 2 * SIGNED_SIZEOF (chunk_size))
468 { chunk_size = psf->filelength - 2 * sizeof (chunk_size) ;
469 psf_log_printf (psf, "FORM : %u (should be %u)\n %M\n", FORMsize, chunk_size, marker) ;
470 FORMsize = chunk_size ;
473 psf_log_printf (psf, "FORM : %u\n %M\n", FORMsize, marker) ;
474 /* Set this to 0, so we don't jump a byte when parsing the next marker. */
480 paiff->comm_offset = psf_ftell (psf) - 8 ;
481 chunk_size += chunk_size & 1 ;
482 comm_fmt->size = chunk_size ;
483 if ((error = aiff_read_comm_chunk (psf, comm_fmt)) != 0)
486 found_chunk |= HAVE_COMM ;
490 /* Must have COMM chunk before PEAK chunk. */
491 if ((found_chunk & (HAVE_FORM | HAVE_AIFF | HAVE_COMM)) != (HAVE_FORM | HAVE_AIFF | HAVE_COMM))
492 return SFE_AIFF_PEAK_B4_COMM ;
494 psf_log_printf (psf, "%M : %d\n", marker, chunk_size) ;
495 if (chunk_size != AIFF_PEAK_CHUNK_SIZE (psf->sf.channels))
496 { psf_binheader_readf (psf, "j", chunk_size) ;
497 psf_log_printf (psf, "*** File PEAK chunk too big.\n") ;
498 return SFE_WAV_BAD_PEAK ;
501 if ((psf->peak_info = peak_info_calloc (psf->sf.channels)) == NULL)
502 return SFE_MALLOC_FAILED ;
504 /* read in rest of PEAK chunk. */
505 psf_binheader_readf (psf, "E44", &(psf->peak_info->version), &(psf->peak_info->timestamp)) ;
507 if (psf->peak_info->version != 1)
508 psf_log_printf (psf, " version : %d *** (should be version 1)\n", psf->peak_info->version) ;
510 psf_log_printf (psf, " version : %d\n", psf->peak_info->version) ;
512 psf_log_printf (psf, " time stamp : %d\n", psf->peak_info->timestamp) ;
513 psf_log_printf (psf, " Ch Position Value\n") ;
516 for (k = 0 ; k < psf->sf.channels ; k++)
520 psf_binheader_readf (psf, "Ef4", &value, &position) ;
521 psf->peak_info->peaks [k].value = value ;
522 psf->peak_info->peaks [k].position = position ;
524 snprintf (cptr, sizeof (ubuf.scbuf), " %2d %-12" PRId64 " %g\n",
525 k, psf->peak_info->peaks [k].position, psf->peak_info->peaks [k].value) ;
526 cptr [sizeof (ubuf.scbuf) - 1] = 0 ;
527 psf_log_printf (psf, "%s", cptr) ;
530 psf->peak_info->peak_loc = ((found_chunk & HAVE_SSND) == 0) ? SF_PEAK_START : SF_PEAK_END ;
534 if ((found_chunk & HAVE_AIFC) && (found_chunk & HAVE_FVER) == 0)
535 psf_log_printf (psf, "*** Valid AIFC files should have an FVER chunk.\n") ;
537 paiff->ssnd_offset = psf_ftell (psf) - 8 ;
538 SSNDsize = chunk_size ;
539 psf_binheader_readf (psf, "E44", &(ssnd_fmt.offset), &(ssnd_fmt.blocksize)) ;
541 psf->datalength = SSNDsize - sizeof (ssnd_fmt) ;
542 psf->dataoffset = psf_ftell (psf) ;
544 if (psf->datalength > psf->filelength - psf->dataoffset || psf->datalength < 0)
545 { psf_log_printf (psf, " SSND : %u (should be %D)\n", SSNDsize, psf->filelength - psf->dataoffset + sizeof (SSND_CHUNK)) ;
546 psf->datalength = psf->filelength - psf->dataoffset ;
549 psf_log_printf (psf, " SSND : %u\n", SSNDsize) ;
551 if (ssnd_fmt.offset == 0 || psf->dataoffset + ssnd_fmt.offset == ssnd_fmt.blocksize)
552 { psf_log_printf (psf, " Offset : %u\n", ssnd_fmt.offset) ;
553 psf_log_printf (psf, " Block Size : %u\n", ssnd_fmt.blocksize) ;
555 psf->dataoffset += ssnd_fmt.offset ;
556 psf->datalength -= ssnd_fmt.offset ;
559 { psf_log_printf (psf, " Offset : %u\n", ssnd_fmt.offset) ;
560 psf_log_printf (psf, " Block Size : %u ???\n", ssnd_fmt.blocksize) ;
561 psf->dataoffset += ssnd_fmt.offset ;
562 psf->datalength -= ssnd_fmt.offset ;
565 /* Only set dataend if there really is data at the end. */
566 if (psf->datalength + psf->dataoffset < psf->filelength)
567 psf->dataend = psf->datalength + psf->dataoffset ;
569 found_chunk |= HAVE_SSND ;
571 if (! psf->sf.seekable)
574 /* Seek to end of SSND chunk. */
575 psf_fseek (psf, psf->dataoffset + psf->datalength, SEEK_SET) ;
581 if (chunk_size >= SIGNED_SIZEOF (ubuf.scbuf))
582 { psf_log_printf (psf, " %M : %d (too big)\n", marker, chunk_size) ;
583 return SFE_INTERNAL ;
587 psf_binheader_readf (psf, "b", cptr, chunk_size + (chunk_size & 1)) ;
588 cptr [chunk_size] = 0 ;
590 psf_sanitize_string (cptr, chunk_size) ;
592 psf_log_printf (psf, " %M : %s\n", marker, cptr) ;
593 psf_store_string (psf, SF_STR_COPYRIGHT, cptr) ;
594 chunk_size += chunk_size & 1 ;
600 if (chunk_size >= SIGNED_SIZEOF (ubuf.scbuf) - 1)
601 { psf_log_printf (psf, " %M : %d (too big)\n", marker, chunk_size) ;
602 return SFE_INTERNAL ;
606 psf_binheader_readf (psf, "b", cptr, chunk_size + (chunk_size & 1)) ;
607 cptr [chunk_size] = 0 ;
608 psf_log_printf (psf, " %M : %s\n", marker, cptr) ;
609 psf_store_string (psf, SF_STR_ARTIST, cptr) ;
610 chunk_size += chunk_size & 1 ;
614 { uint16_t count, id, len ;
615 uint32_t timestamp, bytes ;
620 bytes -= psf_binheader_readf (psf, "E2", &count) ;
621 psf_log_printf (psf, " %M : %d\n count : %d\n", marker, chunk_size, count) ;
623 for (k = 0 ; k < count ; k++)
624 { bytes -= psf_binheader_readf (psf, "E422", ×tamp, &id, &len) ;
625 psf_log_printf (psf, " time : 0x%x\n marker : %x\n length : %d\n", timestamp, id, len) ;
627 if (len + 1 > SIGNED_SIZEOF (ubuf.scbuf))
628 { psf_log_printf (psf, "\nError : string length (%d) too big.\n", len) ;
629 return SFE_INTERNAL ;
633 bytes -= psf_binheader_readf (psf, "b", cptr, len) ;
635 psf_log_printf (psf, " string : %s\n", cptr) ;
639 psf_binheader_readf (psf, "j", bytes) ;
644 { unsigned appl_marker ;
648 if (chunk_size >= SIGNED_SIZEOF (ubuf.scbuf) - 1)
649 { psf_log_printf (psf, " %M : %u (too big, skipping)\n", marker, chunk_size) ;
650 psf_binheader_readf (psf, "j", chunk_size + (chunk_size & 1)) ;
655 { psf_log_printf (psf, " %M : %d (too small, skipping)\n", marker, chunk_size) ;
656 psf_binheader_readf (psf, "j", chunk_size + (chunk_size & 1)) ;
661 psf_binheader_readf (psf, "mb", &appl_marker, cptr, chunk_size + (chunk_size & 1) - 4) ;
662 cptr [chunk_size] = 0 ;
664 for (k = 0 ; k < (int) chunk_size ; k++)
665 if (! psf_isprint (cptr [k]))
670 psf_log_printf (psf, " %M : %d\n AppSig : %M\n Name : %s\n", marker, chunk_size, appl_marker, cptr) ;
671 psf_store_string (psf, SF_STR_SOFTWARE, cptr) ;
672 chunk_size += chunk_size & 1 ;
679 if (chunk_size >= SIGNED_SIZEOF (ubuf.scbuf) - 2)
680 { psf_log_printf (psf, " %M : %d (too big)\n", marker, chunk_size) ;
681 return SFE_INTERNAL ;
685 psf_binheader_readf (psf, "b", cptr, chunk_size + (chunk_size & 1)) ;
686 cptr [chunk_size] = 0 ;
687 psf_log_printf (psf, " %M : %s\n", marker, cptr) ;
688 psf_store_string (psf, SF_STR_TITLE, cptr) ;
689 chunk_size += chunk_size & 1 ;
695 if (chunk_size >= SIGNED_SIZEOF (ubuf.scbuf) - 2)
696 { psf_log_printf (psf, " %M : %d (too big)\n", marker, chunk_size) ;
697 return SFE_INTERNAL ;
701 psf_binheader_readf (psf, "b", cptr, chunk_size + (chunk_size & 1)) ;
702 cptr [chunk_size] = 0 ;
703 psf_log_printf (psf, " %M : %s\n", marker, cptr) ;
704 psf_store_string (psf, SF_STR_COMMENT, cptr) ;
705 chunk_size += chunk_size & 1 ;
709 if (chunk_size != SIZEOF_INST_CHUNK)
710 { psf_log_printf (psf, " %M : %d (should be %d)\n", marker, chunk_size, SIZEOF_INST_CHUNK) ;
711 psf_binheader_readf (psf, "j", chunk_size) ;
714 psf_log_printf (psf, " %M : %d\n", marker, chunk_size) ;
715 { uint8_t bytes [6] ;
718 if (psf->instrument == NULL && (psf->instrument = psf_instrument_alloc ()) == NULL)
719 return SFE_MALLOC_FAILED ;
721 psf_binheader_readf (psf, "b", bytes, 6) ;
722 psf_log_printf (psf, " Base Note : %u\n Detune : %u\n"
723 " Low Note : %u\n High Note : %u\n"
724 " Low Vel. : %u\n High Vel. : %u\n",
725 bytes [0], bytes [1], bytes [2], bytes [3], bytes [4], bytes [5]) ;
726 psf->instrument->basenote = bytes [0] ;
727 psf->instrument->detune = bytes [1] ;
728 psf->instrument->key_lo = bytes [2] ;
729 psf->instrument->key_hi = bytes [3] ;
730 psf->instrument->velocity_lo = bytes [4] ;
731 psf->instrument->velocity_hi = bytes [5] ;
732 psf_binheader_readf (psf, "E2", &gain) ;
733 psf->instrument->gain = gain ;
734 psf_log_printf (psf, " Gain (dB) : %d\n", gain) ;
736 { int16_t mode ; /* 0 - no loop, 1 - forward looping, 2 - backward looping */
737 const char *loop_mode ;
738 uint16_t begin, end ;
740 psf_binheader_readf (psf, "E222", &mode, &begin, &end) ;
741 loop_mode = get_loop_mode_str (mode) ;
742 mode = get_loop_mode (mode) ;
743 if (mode == SF_LOOP_NONE)
744 { psf->instrument->loop_count = 0 ;
745 psf->instrument->loops [0].mode = SF_LOOP_NONE ;
748 { psf->instrument->loop_count = 1 ;
749 psf->instrument->loops [0].mode = SF_LOOP_FORWARD ;
750 psf->instrument->loops [0].start = begin ;
751 psf->instrument->loops [0].end = end ;
752 psf->instrument->loops [0].count = 0 ;
754 psf_log_printf (psf, " Sustain\n mode : %d => %s\n begin : %u\n end : %u\n",
755 mode, loop_mode, begin, end) ;
756 psf_binheader_readf (psf, "E222", &mode, &begin, &end) ;
757 loop_mode = get_loop_mode_str (mode) ;
758 mode = get_loop_mode (mode) ;
759 if (mode == SF_LOOP_NONE)
760 psf->instrument->loops [1].mode = SF_LOOP_NONE ;
762 { psf->instrument->loop_count += 1 ;
763 psf->instrument->loops [1].mode = SF_LOOP_FORWARD ;
764 psf->instrument->loops [1].start = begin ;
765 psf->instrument->loops [1].end = end ;
766 psf->instrument->loops [1].count = 0 ;
768 psf_log_printf (psf, " Release\n mode : %d => %s\n begin : %u\n end : %u\n",
769 mode, loop_mode, begin, end) ;
775 psf_log_printf (psf, " basc : %u\n", chunk_size) ;
777 if ((error = aiff_read_basc_chunk (psf, chunk_size)))
782 psf_log_printf (psf, " %M : %d\n", marker, chunk_size) ;
783 { uint16_t mark_id, n = 0 ;
786 bytesread = psf_binheader_readf (psf, "E2", &n) ;
788 psf_log_printf (psf, " Count : %u\n", mark_count) ;
789 if (paiff->markstr != NULL)
790 { psf_log_printf (psf, "*** Second MARK chunk found. Throwing away the first.\n") ;
791 free (paiff->markstr) ;
793 paiff->markstr = calloc (mark_count, sizeof (MARK_ID_POS)) ;
794 if (paiff->markstr == NULL)
795 return SFE_MALLOC_FAILED ;
797 if (mark_count > 1000)
798 { psf_log_printf (psf, " More than 1000 markers, skipping!\n") ;
799 psf_binheader_readf (psf, "j", chunk_size - bytesread) ;
803 if ((psf->cues = psf_cues_alloc (mark_count)) == NULL)
804 return SFE_MALLOC_FAILED ;
806 for (n = 0 ; n < mark_count && bytesread < chunk_size ; n++)
807 { uint32_t pstr_len ;
810 bytesread += psf_binheader_readf (psf, "E241", &mark_id, &position, &ch) ;
811 psf_log_printf (psf, " Mark ID : %u\n Position : %u\n", mark_id, position) ;
813 psf->cues->cue_points [n].indx = mark_id ;
814 psf->cues->cue_points [n].position = 0 ;
815 psf->cues->cue_points [n].fcc_chunk = MAKE_MARKER ('d', 'a', 't', 'a') ; /* always data */
816 psf->cues->cue_points [n].chunk_start = 0 ;
817 psf->cues->cue_points [n].block_start = 0 ;
818 psf->cues->cue_points [n].sample_offset = position ;
820 pstr_len = (ch & 1) ? ch : ch + 1 ;
822 if (pstr_len < sizeof (ubuf.scbuf) - 1)
823 { bytesread += psf_binheader_readf (psf, "b", ubuf.scbuf, pstr_len) ;
824 ubuf.scbuf [pstr_len] = 0 ;
827 { uint32_t read_len = pstr_len - (sizeof (ubuf.scbuf) - 1) ;
828 bytesread += psf_binheader_readf (psf, "bj", ubuf.scbuf, read_len, pstr_len - read_len) ;
829 ubuf.scbuf [sizeof (ubuf.scbuf) - 1] = 0 ;
832 psf_log_printf (psf, " Name : %s\n", ubuf.scbuf) ;
834 psf_strlcpy (psf->cues->cue_points [n].name, sizeof (psf->cues->cue_points [n].name), ubuf.cbuf) ;
836 paiff->markstr [n].markerID = mark_id ;
837 paiff->markstr [n].position = position ;
839 ** TODO if ubuf.scbuf is equal to
840 ** either Beg_loop, Beg loop or beg loop and spam
841 ** if (psf->instrument == NULL && (psf->instrument = psf_instrument_alloc ()) == NULL)
842 ** return SFE_MALLOC_FAILED ;
847 psf_binheader_readf (psf, "j", chunk_size - bytesread) ;
851 found_chunk |= HAVE_FVER ;
852 /* Fall through to next case. */
855 psf_log_printf (psf, " %M : %d\n", marker, chunk_size) ;
856 psf_binheader_readf (psf, "j", chunk_size) ;
860 /* Fix for broken AIFC files with incorrect COMM chunk length. */
861 chunk_size = (chunk_size >> 24) - 3 ;
862 psf_log_printf (psf, " %M : %d\n", marker, chunk_size) ;
863 psf_binheader_readf (psf, "j", make_size_t (chunk_size)) ;
868 { psf_log_printf (psf, " %M : %d (should be >= 12)\n", marker, chunk_size) ;
869 psf_binheader_readf (psf, "j", chunk_size) ;
873 psf_log_printf (psf, " %M : %d\n", marker, chunk_size) ;
875 if ((error = aiff_read_chanmap (psf, chunk_size)))
880 if (chunk_size >= 0xffff0000)
882 psf_log_printf (psf, "*** Unknown chunk marker (%X) at position %D with length %u. Exiting parser.\n", marker, psf_ftell (psf) - 8, chunk_size) ;
886 if (psf_isprint ((marker >> 24) & 0xFF) && psf_isprint ((marker >> 16) & 0xFF)
887 && psf_isprint ((marker >> 8) & 0xFF) && psf_isprint (marker & 0xFF))
888 { psf_log_printf (psf, " %M : %u (unknown marker)\n", marker, chunk_size) ;
890 psf_binheader_readf (psf, "j", chunk_size) ;
894 if (psf_ftell (psf) & 0x03)
895 { psf_log_printf (psf, " Unknown chunk marker at position %D. Resynching.\n", psf_ftell (psf) - 8) ;
896 psf_binheader_readf (psf, "j", -3) ;
899 psf_log_printf (psf, "*** Unknown chunk marker %X at position %D. Exiting parser.\n", marker, psf_ftell (psf)) ;
902 } ; /* switch (marker) */
904 if (chunk_size >= psf->filelength)
905 { psf_log_printf (psf, "*** Chunk size %u > file length %D. Exiting parser.\n", chunk_size, psf->filelength) ;
909 if ((! psf->sf.seekable) && (found_chunk & HAVE_SSND))
912 if (psf_ftell (psf) >= psf->filelength - (2 * SIGNED_SIZEOF (int32_t)))
916 if (instr_found && mark_found)
917 { int ji, str_index ;
918 /* Next loop will convert markers to loop positions for internal handling */
919 for (ji = 0 ; ji < psf->instrument->loop_count ; ji ++)
920 { if (ji < ARRAY_LEN (psf->instrument->loops))
921 { psf->instrument->loops [ji].start = marker_to_position (paiff->markstr, psf->instrument->loops [ji].start, mark_count) ;
922 psf->instrument->loops [ji].end = marker_to_position (paiff->markstr, psf->instrument->loops [ji].end, mark_count) ;
923 psf->instrument->loops [ji].mode = SF_LOOP_FORWARD ;
927 /* The markers that correspond to loop positions can now be removed from cues struct */
928 if (psf->cues->cue_count > (uint32_t) (psf->instrument->loop_count * 2))
931 for (j = 0 ; j < psf->cues->cue_count - (uint32_t) (psf->instrument->loop_count * 2) ; j ++)
932 { /* This simply copies the information in cues above loop positions and writes it at current count instead */
933 psf->cues->cue_points [j].indx = psf->cues->cue_points [j + psf->instrument->loop_count * 2].indx ;
934 psf->cues->cue_points [j].position = psf->cues->cue_points [j + psf->instrument->loop_count * 2].position ;
935 psf->cues->cue_points [j].fcc_chunk = psf->cues->cue_points [j + psf->instrument->loop_count * 2].fcc_chunk ;
936 psf->cues->cue_points [j].chunk_start = psf->cues->cue_points [j + psf->instrument->loop_count * 2].chunk_start ;
937 psf->cues->cue_points [j].block_start = psf->cues->cue_points [j + psf->instrument->loop_count * 2].block_start ;
938 psf->cues->cue_points [j].sample_offset = psf->cues->cue_points [j + psf->instrument->loop_count * 2].sample_offset ;
939 for (str_index = 0 ; str_index < 256 ; str_index++)
940 psf->cues->cue_points [j].name [str_index] = psf->cues->cue_points [j + psf->instrument->loop_count * 2].name [str_index] ;
942 psf->cues->cue_count -= psf->instrument->loop_count * 2 ;
944 { /* All the cues were in fact loop positions so we can actually remove the cues altogether */
950 if (psf->sf.channels < 1)
951 return SFE_CHANNEL_COUNT_ZERO ;
953 if (psf->sf.channels >= SF_MAX_CHANNELS)
954 return SFE_CHANNEL_COUNT ;
956 if (! (found_chunk & HAVE_FORM))
957 return SFE_AIFF_NO_FORM ;
959 if (! (found_chunk & HAVE_AIFF))
960 return SFE_AIFF_COMM_NO_FORM ;
962 if (! (found_chunk & HAVE_COMM))
963 return SFE_AIFF_SSND_NO_COMM ;
965 if (! psf->dataoffset)
966 return SFE_AIFF_NO_DATA ;
969 } /* aiff_read_header */
972 aiff_close (SF_PRIVATE *psf)
973 { AIFF_PRIVATE *paiff = psf->container_data ;
975 if (paiff != NULL && paiff->markstr != NULL)
976 { free (paiff->markstr) ;
977 paiff->markstr = NULL ;
980 if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
981 { aiff_write_tailer (psf) ;
982 aiff_write_header (psf, SF_TRUE) ;
989 aiff_read_comm_chunk (SF_PRIVATE *psf, COMM_CHUNK *comm_fmt)
991 int subformat, samplerate ;
995 /* The COMM chunk has an int aligned to an odd word boundary. Some
996 ** procesors are not able to deal with this (ie bus fault) so we have
997 ** to take special care.
1000 psf_binheader_readf (psf, "E242b", &(comm_fmt->numChannels), &(comm_fmt->numSampleFrames),
1001 &(comm_fmt->sampleSize), &(comm_fmt->sampleRate), SIGNED_SIZEOF (comm_fmt->sampleRate)) ;
1003 if (comm_fmt->size > 0x10000 && (comm_fmt->size & 0xffff) == 0)
1004 { psf_log_printf (psf, " COMM : %d (0x%x) *** should be ", comm_fmt->size, comm_fmt->size) ;
1005 comm_fmt->size = ENDSWAP_32 (comm_fmt->size) ;
1006 psf_log_printf (psf, "%d (0x%x)\n", comm_fmt->size, comm_fmt->size) ;
1009 psf_log_printf (psf, " COMM : %d\n", comm_fmt->size) ;
1011 if (comm_fmt->size == SIZEOF_AIFF_COMM)
1012 comm_fmt->encoding = NONE_MARKER ;
1013 else if (comm_fmt->size == SIZEOF_AIFC_COMM_MIN)
1014 psf_binheader_readf (psf, "Em", &(comm_fmt->encoding)) ;
1015 else if (comm_fmt->size >= SIZEOF_AIFC_COMM)
1016 { uint8_t encoding_len ;
1019 psf_binheader_readf (psf, "Em1", &(comm_fmt->encoding), &encoding_len) ;
1021 comm_fmt->size = SF_MIN (sizeof (ubuf.scbuf), make_size_t (comm_fmt->size)) ;
1022 memset (ubuf.scbuf, 0, comm_fmt->size) ;
1023 read_len = comm_fmt->size - SIZEOF_AIFC_COMM + 1 ;
1024 psf_binheader_readf (psf, "b", ubuf.scbuf, read_len) ;
1025 ubuf.scbuf [read_len + 1] = 0 ;
1028 samplerate = tenbytefloat2int (comm_fmt->sampleRate) ;
1030 psf_log_printf (psf, " Sample Rate : %d\n", samplerate) ;
1031 psf_log_printf (psf, " Frames : %u%s\n", comm_fmt->numSampleFrames, (comm_fmt->numSampleFrames == 0 && psf->filelength > 104) ? " (Should not be 0)" : "") ;
1033 if (comm_fmt->numChannels < 1 || comm_fmt->numChannels >= SF_MAX_CHANNELS)
1034 { psf_log_printf (psf, " Channels : %d (should be >= 1 and < %d)\n", comm_fmt->numChannels, SF_MAX_CHANNELS) ;
1035 return SFE_CHANNEL_COUNT_BAD ;
1038 psf_log_printf (psf, " Channels : %d\n", comm_fmt->numChannels) ;
1040 /* Found some broken 'fl32' files with comm.samplesize == 16. Fix it here. */
1041 if ((comm_fmt->encoding == fl32_MARKER || comm_fmt->encoding == FL32_MARKER) && comm_fmt->sampleSize != 32)
1042 { psf_log_printf (psf, " Sample Size : %d (should be 32)\n", comm_fmt->sampleSize) ;
1043 comm_fmt->sampleSize = 32 ;
1045 else if ((comm_fmt->encoding == fl64_MARKER || comm_fmt->encoding == FL64_MARKER) && comm_fmt->sampleSize != 64)
1046 { psf_log_printf (psf, " Sample Size : %d (should be 64)\n", comm_fmt->sampleSize) ;
1047 comm_fmt->sampleSize = 64 ;
1050 psf_log_printf (psf, " Sample Size : %d\n", comm_fmt->sampleSize) ;
1052 subformat = s_bitwidth_to_subformat (comm_fmt->sampleSize) ;
1054 psf->sf.samplerate = samplerate ;
1055 psf->sf.frames = comm_fmt->numSampleFrames ;
1056 psf->sf.channels = comm_fmt->numChannels ;
1057 psf->bytewidth = BITWIDTH2BYTES (comm_fmt->sampleSize) ;
1059 psf->endian = SF_ENDIAN_BIG ;
1061 switch (comm_fmt->encoding)
1062 { case NONE_MARKER :
1063 psf->sf.format = (SF_FORMAT_AIFF | subformat) ;
1069 psf->sf.format = (SF_ENDIAN_BIG | SF_FORMAT_AIFF | subformat) ;
1075 psf->endian = SF_ENDIAN_LITTLE ;
1076 psf->sf.format = (SF_ENDIAN_LITTLE | SF_FORMAT_AIFF | subformat) ;
1081 psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_FLOAT) ;
1086 psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_ULAW) ;
1091 psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_ALAW) ;
1096 psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_DOUBLE) ;
1100 psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_PCM_U8) ;
1104 psf->sf.format = SF_FORMAT_AIFF ;
1105 switch (comm_fmt->sampleSize)
1107 psf->sf.format |= SF_FORMAT_DWVW_12 ;
1110 psf->sf.format |= SF_FORMAT_DWVW_16 ;
1113 psf->sf.format |= SF_FORMAT_DWVW_24 ;
1117 psf->sf.format |= SF_FORMAT_DWVW_N ;
1123 psf->sf.format = SF_FORMAT_AIFF ;
1124 psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_GSM610) ;
1129 psf->endian = SF_ENDIAN_BIG ;
1130 psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_IMA_ADPCM) ;
1134 psf_log_printf (psf, "AIFC : Unimplemented format : %M\n", comm_fmt->encoding) ;
1135 return SFE_UNIMPLEMENTED ;
1138 if (! ubuf.scbuf [0])
1139 psf_log_printf (psf, " Encoding : %M\n", comm_fmt->encoding) ;
1141 psf_log_printf (psf, " Encoding : %M => %s\n", comm_fmt->encoding, ubuf.scbuf) ;
1144 } /* aiff_read_comm_chunk */
1147 /*==========================================================================================
1151 aiff_rewrite_header (SF_PRIVATE *psf)
1153 /* Assuming here that the header has already been written and just
1154 ** needs to be corrected for new data length. That means that we
1155 ** only change the length fields of the FORM and SSND chunks ;
1156 ** everything else can be skipped over.
1158 int k, ch, comm_size, comm_frames ;
1160 psf_fseek (psf, 0, SEEK_SET) ;
1161 psf_fread (psf->header.ptr, psf->dataoffset, 1, psf) ;
1163 psf->header.indx = 0 ;
1166 psf_binheader_writef (psf, "Etm8", FORM_MARKER, psf->filelength - 8) ;
1169 if ((k = psf_find_read_chunk_m32 (&psf->rchunks, COMM_MARKER)) >= 0)
1170 { psf->header.indx = psf->rchunks.chunks [k].offset - 8 ;
1171 comm_frames = psf->sf.frames ;
1172 comm_size = psf->rchunks.chunks [k].len ;
1173 psf_binheader_writef (psf, "Em42t4", COMM_MARKER, comm_size, psf->sf.channels, comm_frames) ;
1177 if ((k = psf_find_read_chunk_m32 (&psf->rchunks, PEAK_MARKER)) >= 0)
1178 { psf->header.indx = psf->rchunks.chunks [k].offset - 8 ;
1179 psf_binheader_writef (psf, "Em4", PEAK_MARKER, AIFF_PEAK_CHUNK_SIZE (psf->sf.channels)) ;
1180 psf_binheader_writef (psf, "E44", 1, time (NULL)) ;
1181 for (ch = 0 ; ch < psf->sf.channels ; ch++)
1182 psf_binheader_writef (psf, "Eft8", (float) psf->peak_info->peaks [ch].value, psf->peak_info->peaks [ch].position) ;
1187 if ((k = psf_find_read_chunk_m32 (&psf->rchunks, SSND_MARKER)) >= 0)
1188 { psf->header.indx = psf->rchunks.chunks [k].offset - 8 ;
1189 psf_binheader_writef (psf, "Etm8", SSND_MARKER, psf->datalength + SIZEOF_SSND_CHUNK) ;
1192 /* Header mangling complete so write it out. */
1193 psf_fseek (psf, 0, SEEK_SET) ;
1194 psf_fwrite (psf->header.ptr, psf->header.indx, 1, psf) ;
1197 } /* aiff_rewrite_header */
1200 convert_loop_mode (int type_mode)
1201 { switch (type_mode)
1202 { case SF_LOOP_NONE:
1204 case SF_LOOP_FORWARD :
1206 case SF_LOOP_ALTERNATING :
1212 } /* convert_loop_mode */
1215 aiff_write_header (SF_PRIVATE *psf, int calc_length)
1216 { sf_count_t current ;
1217 AIFF_PRIVATE *paiff ;
1218 uint8_t comm_sample_rate [10], comm_zero_bytes [2] = { 0, 0 } ;
1219 uint32_t comm_type, comm_size, comm_encoding, comm_frames = 0, uk ;
1220 int k, endian, has_data = SF_FALSE ;
1223 if ((paiff = psf->container_data) == NULL)
1224 return SFE_INTERNAL ;
1226 current = psf_ftell (psf) ;
1228 if (current > psf->dataoffset)
1229 has_data = SF_TRUE ;
1232 { psf->filelength = psf_get_filelen (psf) ;
1234 psf->datalength = psf->filelength - psf->dataoffset ;
1236 psf->datalength -= psf->filelength - psf->dataend ;
1238 if (psf->bytewidth > 0)
1239 psf->sf.frames = psf->datalength / (psf->bytewidth * psf->sf.channels) ;
1242 if (psf->file.mode == SFM_RDWR && psf->dataoffset > 0 && psf->rchunks.count > 0)
1243 { aiff_rewrite_header (psf) ;
1245 psf_fseek (psf, current, SEEK_SET) ;
1249 endian = SF_ENDIAN (psf->sf.format) ;
1250 if (CPU_IS_LITTLE_ENDIAN && endian == SF_ENDIAN_CPU)
1251 endian = SF_ENDIAN_LITTLE ;
1253 /* Standard value here. */
1254 bit_width = psf->bytewidth * 8 ;
1255 comm_frames = (psf->sf.frames > 0xFFFFFFFF) ? 0xFFFFFFFF : psf->sf.frames ;
1257 switch (SF_CODEC (psf->sf.format) | endian)
1258 { case SF_FORMAT_PCM_S8 | SF_ENDIAN_BIG :
1259 psf->endian = SF_ENDIAN_BIG ;
1260 comm_type = AIFC_MARKER ;
1261 comm_size = SIZEOF_AIFC_COMM ;
1262 comm_encoding = twos_MARKER ;
1265 case SF_FORMAT_PCM_S8 | SF_ENDIAN_LITTLE :
1266 psf->endian = SF_ENDIAN_LITTLE ;
1267 comm_type = AIFC_MARKER ;
1268 comm_size = SIZEOF_AIFC_COMM ;
1269 comm_encoding = sowt_MARKER ;
1272 case SF_FORMAT_PCM_16 | SF_ENDIAN_BIG :
1273 psf->endian = SF_ENDIAN_BIG ;
1274 comm_type = AIFC_MARKER ;
1275 comm_size = SIZEOF_AIFC_COMM ;
1276 comm_encoding = twos_MARKER ;
1279 case SF_FORMAT_PCM_16 | SF_ENDIAN_LITTLE :
1280 psf->endian = SF_ENDIAN_LITTLE ;
1281 comm_type = AIFC_MARKER ;
1282 comm_size = SIZEOF_AIFC_COMM ;
1283 comm_encoding = sowt_MARKER ;
1286 case SF_FORMAT_PCM_24 | SF_ENDIAN_BIG :
1287 psf->endian = SF_ENDIAN_BIG ;
1288 comm_type = AIFC_MARKER ;
1289 comm_size = SIZEOF_AIFC_COMM ;
1290 comm_encoding = in24_MARKER ;
1293 case SF_FORMAT_PCM_24 | SF_ENDIAN_LITTLE :
1294 psf->endian = SF_ENDIAN_LITTLE ;
1295 comm_type = AIFC_MARKER ;
1296 comm_size = SIZEOF_AIFC_COMM ;
1297 comm_encoding = ni24_MARKER ;
1300 case SF_FORMAT_PCM_32 | SF_ENDIAN_BIG :
1301 psf->endian = SF_ENDIAN_BIG ;
1302 comm_type = AIFC_MARKER ;
1303 comm_size = SIZEOF_AIFC_COMM ;
1304 comm_encoding = in32_MARKER ;
1307 case SF_FORMAT_PCM_32 | SF_ENDIAN_LITTLE :
1308 psf->endian = SF_ENDIAN_LITTLE ;
1309 comm_type = AIFC_MARKER ;
1310 comm_size = SIZEOF_AIFC_COMM ;
1311 comm_encoding = ni32_MARKER ;
1314 case SF_FORMAT_PCM_S8 : /* SF_ENDIAN_FILE */
1315 case SF_FORMAT_PCM_16 :
1316 case SF_FORMAT_PCM_24 :
1317 case SF_FORMAT_PCM_32 :
1318 psf->endian = SF_ENDIAN_BIG ;
1319 comm_type = AIFF_MARKER ;
1320 comm_size = SIZEOF_AIFF_COMM ;
1324 case SF_FORMAT_FLOAT : /* Big endian floating point. */
1325 psf->endian = SF_ENDIAN_BIG ;
1326 comm_type = AIFC_MARKER ;
1327 comm_size = SIZEOF_AIFC_COMM ;
1328 comm_encoding = FL32_MARKER ; /* Use 'FL32' because its easier to read. */
1331 case SF_FORMAT_DOUBLE : /* Big endian double precision floating point. */
1332 psf->endian = SF_ENDIAN_BIG ;
1333 comm_type = AIFC_MARKER ;
1334 comm_size = SIZEOF_AIFC_COMM ;
1335 comm_encoding = FL64_MARKER ; /* Use 'FL64' because its easier to read. */
1338 case SF_FORMAT_ULAW :
1339 psf->endian = SF_ENDIAN_BIG ;
1340 comm_type = AIFC_MARKER ;
1341 comm_size = SIZEOF_AIFC_COMM ;
1342 comm_encoding = ulaw_MARKER ;
1345 case SF_FORMAT_ALAW :
1346 psf->endian = SF_ENDIAN_BIG ;
1347 comm_type = AIFC_MARKER ;
1348 comm_size = SIZEOF_AIFC_COMM ;
1349 comm_encoding = alaw_MARKER ;
1352 case SF_FORMAT_PCM_U8 :
1353 psf->endian = SF_ENDIAN_BIG ;
1354 comm_type = AIFC_MARKER ;
1355 comm_size = SIZEOF_AIFC_COMM ;
1356 comm_encoding = raw_MARKER ;
1359 case SF_FORMAT_DWVW_12 :
1360 psf->endian = SF_ENDIAN_BIG ;
1361 comm_type = AIFC_MARKER ;
1362 comm_size = SIZEOF_AIFC_COMM ;
1363 comm_encoding = DWVW_MARKER ;
1365 /* Override standard value here.*/
1369 case SF_FORMAT_DWVW_16 :
1370 psf->endian = SF_ENDIAN_BIG ;
1371 comm_type = AIFC_MARKER ;
1372 comm_size = SIZEOF_AIFC_COMM ;
1373 comm_encoding = DWVW_MARKER ;
1375 /* Override standard value here.*/
1379 case SF_FORMAT_DWVW_24 :
1380 psf->endian = SF_ENDIAN_BIG ;
1381 comm_type = AIFC_MARKER ;
1382 comm_size = SIZEOF_AIFC_COMM ;
1383 comm_encoding = DWVW_MARKER ;
1385 /* Override standard value here.*/
1389 case SF_FORMAT_GSM610 :
1390 psf->endian = SF_ENDIAN_BIG ;
1391 comm_type = AIFC_MARKER ;
1392 comm_size = SIZEOF_AIFC_COMM ;
1393 comm_encoding = GSM_MARKER ;
1395 /* Override standard value here.*/
1399 case SF_FORMAT_IMA_ADPCM :
1400 psf->endian = SF_ENDIAN_BIG ;
1401 comm_type = AIFC_MARKER ;
1402 comm_size = SIZEOF_AIFC_COMM ;
1403 comm_encoding = ima4_MARKER ;
1405 /* Override standard value here.*/
1407 comm_frames = psf->sf.frames / AIFC_IMA4_SAMPLES_PER_BLOCK ;
1410 default : return SFE_BAD_OPEN_FORMAT ;
1413 /* Reset the current header length to zero. */
1414 psf->header.ptr [0] = 0 ;
1415 psf->header.indx = 0 ;
1416 psf_fseek (psf, 0, SEEK_SET) ;
1418 psf_binheader_writef (psf, "Etm8", FORM_MARKER, psf->filelength - 8) ;
1420 /* Write AIFF/AIFC marker and COM chunk. */
1421 if (comm_type == AIFC_MARKER)
1422 /* AIFC must have an FVER chunk. */
1423 psf_binheader_writef (psf, "Emm44", comm_type, FVER_MARKER, 4, 0xA2805140) ;
1425 psf_binheader_writef (psf, "Em", comm_type) ;
1427 paiff->comm_offset = psf->header.indx - 8 ;
1429 memset (comm_sample_rate, 0, sizeof (comm_sample_rate)) ;
1430 uint2tenbytefloat (psf->sf.samplerate, comm_sample_rate) ;
1432 psf_binheader_writef (psf, "Em42t42", COMM_MARKER, comm_size, psf->sf.channels, comm_frames, bit_width) ;
1433 psf_binheader_writef (psf, "b", comm_sample_rate, sizeof (comm_sample_rate)) ;
1435 /* AIFC chunks have some extra data. */
1436 if (comm_type == AIFC_MARKER)
1437 psf_binheader_writef (psf, "mb", comm_encoding, comm_zero_bytes, sizeof (comm_zero_bytes)) ;
1439 if (psf->channel_map && paiff->chanmap_tag)
1440 psf_binheader_writef (psf, "Em4444", CHAN_MARKER, 12, paiff->chanmap_tag, 0, 0) ;
1442 /* Check if there's a INST chunk to write */
1443 if (psf->instrument != NULL && psf->cues != NULL)
1444 { /* Both loops and cues exist */
1445 uint16_t sustainLoopMode, releaseLoopMode ;
1446 uint32_t idx, sLoopStart = 0, sLoopEnd = 0, rLoopStart = 0, rLoopEnd = 0 ;
1447 int totalStringLength = 0, stringLength ;
1449 /* Here we count how many bytes will the pascal strings need */
1450 for (idx = 0 ; idx < psf->cues->cue_count ; idx++)
1451 { stringLength = strlen (psf->cues->cue_points [idx].name) + 1 ; /* We'll count the first byte also of every pascal string */
1452 if (stringLength % 2 == 0)
1453 totalStringLength += stringLength ;
1455 totalStringLength += (stringLength + 1) ; /* The pascal string must have an even count */
1458 /* First we check which loops are active and create the necessary MARK chunk for markers */
1459 /* The first written markers will be references from loop points then comes the real markers */
1460 if (psf->instrument->loops [0].mode != SF_LOOP_NONE && psf->instrument->loops [1].mode != SF_LOOP_NONE)
1461 { /* There's both a sustain loop and a release loop */
1462 psf_binheader_writef (psf, "Em42 241b 241b 241b 241b",
1463 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,
1464 1, psf->instrument->loops [0].start, 18, "sustain loop start", make_size_t (19),
1465 2, psf->instrument->loops [0].end, 16, "sustain loop end", make_size_t (17),
1466 3, psf->instrument->loops [1].start, 18, "release loop start", make_size_t (19),
1467 4, psf->instrument->loops [1].end, 16, "release loop end", make_size_t (17)) ;
1468 /* Now comes true markers from cues struct */
1469 for (idx = 0 ; idx < psf->cues->cue_count ; idx++)
1470 psf_binheader_writef (psf, "E24p", 5 + idx, psf->cues->cue_points [idx].sample_offset, psf->cues->cue_points [idx].name) ;
1472 /* Change the loops to be references to the markers */
1478 else if (psf->instrument->loops [0].mode != SF_LOOP_NONE && psf->instrument->loops [1].mode == SF_LOOP_NONE)
1479 { /* There's a sustain loop but no release loop */
1480 psf_binheader_writef (psf, "Em42241b241b",
1481 MARK_MARKER, 2 + (2 + 4 + 1 + 19) + (2 + 4 + 1 + 17) + psf->cues->cue_count * (2 + 4) + totalStringLength, 2 + psf->cues->cue_count,
1482 1, psf->instrument->loops [0].start, 18, "sustain loop start", make_size_t (19),
1483 2, psf->instrument->loops [0].end, 16, "sustain loop end", make_size_t (17)) ;
1484 /* Now comes true markers from cues struct */
1485 for (idx = 0 ; idx < psf->cues->cue_count ; idx++)
1486 psf_binheader_writef (psf, "E24p", 3 + idx, psf->cues->cue_points [idx].sample_offset, psf->cues->cue_points [idx].name) ;
1488 /* Change the loops to be references to the markers */
1494 else if (psf->instrument->loops [0].mode == SF_LOOP_NONE && psf->instrument->loops [1].mode != SF_LOOP_NONE)
1495 { /* There's a release loop but no sustain loop! Strange indeed! */
1496 psf_binheader_writef (psf, "Em42241b241b",
1497 MARK_MARKER, 2 + (2 + 4 + 1 + 19) + (2 + 4 + 1 + 17) + psf->cues->cue_count * (2 + 4) + totalStringLength, 2 + psf->cues->cue_count,
1498 1, psf->instrument->loops [1].start, 18, "release loop start", make_size_t (19),
1499 2, psf->instrument->loops [1].end, 16, "release loop end", make_size_t (17)) ;
1500 /* Now comes true markers from cues struct */
1501 for (idx = 0 ; idx < psf->cues->cue_count ; idx++)
1502 psf_binheader_writef (psf, "E24p", 3 + idx, psf->cues->cue_points [idx].sample_offset, psf->cues->cue_points [idx].name) ;
1504 /* Change the loops to be references to the markers */
1511 /* First convert loop modes to aiff standard */
1512 sustainLoopMode = convert_loop_mode (psf->instrument->loops [0].mode) ;
1513 releaseLoopMode = convert_loop_mode (psf->instrument->loops [1].mode) ;
1515 /* Now we finally write the actual INST chunk */
1516 psf_binheader_writef (psf, "Em4111111", INST_MARKER, SIZEOF_INST_CHUNK, psf->instrument->basenote, psf->instrument->detune,
1517 psf->instrument->key_lo, psf->instrument->key_hi, psf->instrument->velocity_lo, psf->instrument->velocity_hi) ;
1518 psf_binheader_writef (psf, "E2222222", (short) psf->instrument->gain,
1519 sustainLoopMode, sLoopStart, sLoopEnd,
1520 releaseLoopMode, rLoopStart, rLoopEnd) ;
1523 else if (psf->instrument != NULL && psf->cues == NULL)
1524 { /* There are loops but no cues */
1525 uint16_t sustainLoopMode, releaseLoopMode ;
1526 uint32_t sLoopStart = 0, sLoopEnd = 0, rLoopStart = 0, rLoopEnd = 0 ;
1528 /* First we check which loops are active and create the necessary MARK chunk for markers */
1529 if (psf->instrument->loops [0].mode != SF_LOOP_NONE && psf->instrument->loops [1].mode != SF_LOOP_NONE)
1530 { /* There's both a sustain loop and a release loop */
1531 psf_binheader_writef (psf, "Em42 241b 241b 241b 241b",
1532 MARK_MARKER, 2 + 2 * (2 + 4 + 1 + 19) + 2 * (2 + 4 + 1 + 17), 4,
1533 1, psf->instrument->loops [0].start, 18, "sustain loop start", make_size_t (19),
1534 2, psf->instrument->loops [0].end, 16, "sustain loop end", make_size_t (17),
1535 3, psf->instrument->loops [1].start, 18, "release loop start", make_size_t (19),
1536 4, psf->instrument->loops [1].end, 16, "release loop end", make_size_t (17)) ;
1537 /* Change the loops to be references to the markers */
1543 else if (psf->instrument->loops [0].mode != SF_LOOP_NONE && psf->instrument->loops [1].mode == SF_LOOP_NONE)
1544 { /* There's a sustain loop but no release loop */
1545 psf_binheader_writef (psf, "Em42241b241b",
1546 MARK_MARKER, 2 + (2 + 4 + 1 + 19) + (2 + 4 + 1 + 17), 2,
1547 1, psf->instrument->loops [0].start, 18, "sustain loop start", make_size_t (19),
1548 2, psf->instrument->loops [0].end, 16, "sustain loop end", make_size_t (17)) ;
1549 /* Change the loops to be references to the markers */
1555 else if (psf->instrument->loops [0].mode == SF_LOOP_NONE && psf->instrument->loops [1].mode != SF_LOOP_NONE)
1556 { /* There's a release loop but no sustain loop! Strange indeed! */
1557 psf_binheader_writef (psf, "Em42241b241b",
1558 MARK_MARKER, 2 + (2 + 4 + 1 + 19) + (2 + 4 + 1 + 17), 2,
1559 1, psf->instrument->loops [1].start, 18, "release loop start", make_size_t (19),
1560 2, psf->instrument->loops [1].end, 16, "release loop end", make_size_t (17)) ;
1561 /* Change the loops to be references to the markers */
1568 /* First convert loop modes to aiff standard */
1569 sustainLoopMode = convert_loop_mode (psf->instrument->loops [0].mode) ;
1570 releaseLoopMode = convert_loop_mode (psf->instrument->loops [1].mode) ;
1572 /* Now we finally write the actual INST chunk */
1573 psf_binheader_writef (psf, "Em4111111", INST_MARKER, SIZEOF_INST_CHUNK, psf->instrument->basenote, psf->instrument->detune,
1574 psf->instrument->key_lo, psf->instrument->key_hi, psf->instrument->velocity_lo, psf->instrument->velocity_hi) ;
1575 psf_binheader_writef (psf, "E2222222", (short) psf->instrument->gain,
1576 sustainLoopMode, sLoopStart, sLoopEnd,
1577 releaseLoopMode, rLoopStart, rLoopEnd) ;
1580 else if (psf->instrument == NULL && psf->cues != NULL)
1581 { /* There are cues but no loops */
1583 int totalStringLength = 0, stringLength ;
1585 /* Here we count how many bytes will the pascal strings need */
1586 for (idx = 0 ; idx < psf->cues->cue_count ; idx++)
1587 { stringLength = strlen (psf->cues->cue_points [idx].name) + 1 ; /* We'll count the first byte also of every pascal string */
1588 totalStringLength += stringLength + (stringLength % 2 == 0 ? 0 : 1) ;
1591 psf_binheader_writef (psf, "Em42",
1592 MARK_MARKER, 2 + psf->cues->cue_count * (2 + 4) + totalStringLength, psf->cues->cue_count) ;
1594 for (idx = 0 ; idx < psf->cues->cue_count ; idx++)
1595 psf_binheader_writef (psf, "E24p", psf->cues->cue_points [idx].indx, psf->cues->cue_points [idx].sample_offset, psf->cues->cue_points [idx].name) ;
1598 if (psf->strings.flags & SF_STR_LOCATE_START)
1599 aiff_write_strings (psf, SF_STR_LOCATE_START) ;
1601 if (psf->peak_info != NULL && psf->peak_info->peak_loc == SF_PEAK_START)
1602 { psf_binheader_writef (psf, "Em4", PEAK_MARKER, AIFF_PEAK_CHUNK_SIZE (psf->sf.channels)) ;
1603 psf_binheader_writef (psf, "E44", 1, time (NULL)) ;
1604 for (k = 0 ; k < psf->sf.channels ; k++)
1605 psf_binheader_writef (psf, "Eft8", (float) psf->peak_info->peaks [k].value, psf->peak_info->peaks [k].position) ;
1608 /* Write custom headers. */
1609 for (uk = 0 ; uk < psf->wchunks.used ; uk++)
1610 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)) ;
1612 /* Write SSND chunk. */
1613 paiff->ssnd_offset = psf->header.indx ;
1614 psf_binheader_writef (psf, "Etm844", SSND_MARKER, psf->datalength + SIZEOF_SSND_CHUNK, 0, 0) ;
1616 /* Header construction complete so write it out. */
1617 psf_fwrite (psf->header.ptr, psf->header.indx, 1, psf) ;
1622 if (has_data && psf->dataoffset != psf->header.indx)
1623 return psf->error = SFE_INTERNAL ;
1625 psf->dataoffset = psf->header.indx ;
1628 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
1629 else if (current > 0)
1630 psf_fseek (psf, current, SEEK_SET) ;
1633 } /* aiff_write_header */
1636 aiff_write_tailer (SF_PRIVATE *psf)
1639 /* Reset the current header length to zero. */
1640 psf->header.ptr [0] = 0 ;
1641 psf->header.indx = 0 ;
1643 psf->dataend = psf_fseek (psf, 0, SEEK_END) ;
1645 /* Make sure tailer data starts at even byte offset. Pad if necessary. */
1646 if (psf->dataend % 2 == 1)
1647 { psf_fwrite (psf->header.ptr, 1, 1, psf) ;
1651 if (psf->peak_info != NULL && psf->peak_info->peak_loc == SF_PEAK_END)
1652 { psf_binheader_writef (psf, "Em4", PEAK_MARKER, AIFF_PEAK_CHUNK_SIZE (psf->sf.channels)) ;
1653 psf_binheader_writef (psf, "E44", 1, time (NULL)) ;
1654 for (k = 0 ; k < psf->sf.channels ; k++)
1655 psf_binheader_writef (psf, "Eft8", (float) psf->peak_info->peaks [k].value, psf->peak_info->peaks [k].position) ;
1658 if (psf->strings.flags & SF_STR_LOCATE_END)
1659 aiff_write_strings (psf, SF_STR_LOCATE_END) ;
1661 /* Write the tailer. */
1662 if (psf->header.indx > 0)
1663 psf_fwrite (psf->header.ptr, psf->header.indx, 1, psf) ;
1666 } /* aiff_write_tailer */
1669 aiff_write_strings (SF_PRIVATE *psf, int location)
1672 for (k = 0 ; k < SF_MAX_STRINGS ; k++)
1673 { if (psf->strings.data [k].type == 0)
1676 if (psf->strings.data [k].flags != location)
1679 switch (psf->strings.data [k].type)
1680 { case SF_STR_SOFTWARE :
1681 slen = strlen (psf->strings.storage + psf->strings.data [k].offset) ;
1682 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))) ;
1686 psf_binheader_writef (psf, "EmS", NAME_MARKER, psf->strings.storage + psf->strings.data [k].offset) ;
1689 case SF_STR_COPYRIGHT :
1690 psf_binheader_writef (psf, "EmS", c_MARKER, psf->strings.storage + psf->strings.data [k].offset) ;
1693 case SF_STR_ARTIST :
1694 psf_binheader_writef (psf, "EmS", AUTH_MARKER, psf->strings.storage + psf->strings.data [k].offset) ;
1697 case SF_STR_COMMENT :
1698 psf_binheader_writef (psf, "EmS", ANNO_MARKER, psf->strings.storage + psf->strings.data [k].offset) ;
1703 psf_binheader_writef (psf, "Ems", ICRD_MARKER, psf->strings.data [k].str) ;
1710 } /* aiff_write_strings */
1713 aiff_command (SF_PRIVATE * psf, int command, void * UNUSED (data), int UNUSED (datasize))
1714 { AIFF_PRIVATE *paiff ;
1716 if ((paiff = psf->container_data) == NULL)
1717 return SFE_INTERNAL ;
1720 { case SFC_SET_CHANNEL_MAP_INFO :
1721 paiff->chanmap_tag = aiff_caf_find_channel_layout_tag (psf->channel_map, psf->sf.channels) ;
1722 return (paiff->chanmap_tag != 0) ;
1729 } /* aiff_command */
1732 get_loop_mode_str (int16_t mode)
1734 { case 0 : return "none" ;
1735 case 1 : return "forward" ;
1736 case 2 : return "backward" ;
1739 return "*** unknown" ;
1740 } /* get_loop_mode_str */
1743 get_loop_mode (int16_t mode)
1745 { case 0 : return SF_LOOP_NONE ;
1746 case 1 : return SF_LOOP_FORWARD ;
1747 case 2 : return SF_LOOP_BACKWARD ;
1750 return SF_LOOP_NONE ;
1751 } /* get_loop_mode */
1753 /*==========================================================================================
1754 ** Rough hack at converting from 80 bit IEEE float in AIFF header to an int and
1755 ** back again. It assumes that all sample rates are between 1 and 800MHz, which
1756 ** should be OK as other sound file formats use a 32 bit integer to store sample
1758 ** There is another (probably better) version in the source code to the SoX but it
1759 ** has a copyright which probably prevents it from being allowable as GPL/LGPL.
1763 tenbytefloat2int (uint8_t *bytes)
1766 if (bytes [0] & 0x80) /* Negative number. */
1769 if (bytes [0] <= 0x3F) /* Less than 1. */
1772 if (bytes [0] > 0x40) /* Way too big. */
1775 if (bytes [0] == 0x40 && bytes [1] > 0x1C) /* Too big. */
1778 /* Ok, can handle it. */
1780 val = (bytes [2] << 23) | (bytes [3] << 15) | (bytes [4] << 7) | (bytes [5] >> 1) ;
1782 val >>= (29 - bytes [1]) ;
1785 } /* tenbytefloat2int */
1788 uint2tenbytefloat (uint32_t num, uint8_t *bytes)
1789 { uint32_t mask = 0x40000000 ;
1793 { bytes [0] = 0x3F ;
1802 { bytes [1] = 0x1D ;
1806 for (count = 0 ; count < 32 ; count ++)
1812 num = count < 31 ? num << (count + 1) : 0 ;
1813 bytes [1] = 29 - count ;
1814 bytes [2] = (num >> 24) & 0xFF ;
1815 bytes [3] = (num >> 16) & 0xFF ;
1816 bytes [4] = (num >> 8) & 0xFF ;
1817 bytes [5] = num & 0xFF ;
1819 } /* uint2tenbytefloat */
1822 aiff_read_basc_chunk (SF_PRIVATE * psf, int datasize)
1823 { const char * type_str ;
1827 count = psf_binheader_readf (psf, "E442", &bc.version, &bc.numBeats, &bc.rootNote) ;
1828 count += psf_binheader_readf (psf, "E222", &bc.scaleType, &bc.sigNumerator, &bc.sigDenominator) ;
1829 count += psf_binheader_readf (psf, "E2j", &bc.loopType, datasize - sizeof (bc)) ;
1831 psf_log_printf (psf, " Version ? : %u\n Num Beats : %u\n Root Note : 0x%x\n",
1832 bc.version, bc.numBeats, bc.rootNote) ;
1834 switch (bc.scaleType)
1835 { case basc_SCALE_MINOR :
1836 type_str = "MINOR" ;
1838 case basc_SCALE_MAJOR :
1839 type_str = "MAJOR" ;
1841 case basc_SCALE_NEITHER :
1842 type_str = "NEITHER" ;
1844 case basc_SCALE_BOTH :
1848 type_str = "!!WRONG!!" ;
1852 psf_log_printf (psf, " ScaleType : 0x%x (%s)\n", bc.scaleType, type_str) ;
1853 psf_log_printf (psf, " Time Sig : %d/%d\n", bc.sigNumerator, bc.sigDenominator) ;
1855 switch (bc.loopType)
1856 { case basc_TYPE_ONE_SHOT :
1857 type_str = "One Shot" ;
1859 case basc_TYPE_LOOP :
1863 type_str = "!!WRONG!!" ;
1867 psf_log_printf (psf, " Loop Type : 0x%x (%s)\n", bc.loopType, type_str) ;
1869 if ((psf->loop_info = calloc (1, sizeof (SF_LOOP_INFO))) == NULL)
1870 return SFE_MALLOC_FAILED ;
1872 psf->loop_info->time_sig_num = bc.sigNumerator ;
1873 psf->loop_info->time_sig_den = bc.sigDenominator ;
1874 psf->loop_info->loop_mode = (bc.loopType == basc_TYPE_ONE_SHOT) ? SF_LOOP_NONE : SF_LOOP_FORWARD ;
1875 psf->loop_info->num_beats = bc.numBeats ;
1877 /* Can always be recalculated from other known fields. */
1878 psf->loop_info->bpm = (1.0 / psf->sf.frames) * psf->sf.samplerate
1879 * ((bc.numBeats * 4.0) / bc.sigDenominator) * 60.0 ;
1880 psf->loop_info->root_key = bc.rootNote ;
1882 if (count < datasize)
1883 psf_binheader_readf (psf, "j", datasize - count) ;
1886 } /* aiff_read_basc_chunk */
1890 aiff_read_chanmap (SF_PRIVATE * psf, unsigned dword)
1891 { const AIFF_CAF_CHANNEL_MAP * map_info ;
1892 unsigned channel_bitmap, channel_decriptions, bytesread ;
1895 bytesread = psf_binheader_readf (psf, "444", &layout_tag, &channel_bitmap, &channel_decriptions) ;
1897 if ((map_info = aiff_caf_of_channel_layout_tag (layout_tag)) == NULL)
1900 psf_log_printf (psf, " Tag : %x\n", layout_tag) ;
1902 psf_log_printf (psf, " Layout : %s\n", map_info->name) ;
1904 if (bytesread < dword)
1905 psf_binheader_readf (psf, "j", dword - bytesread) ;
1907 if (map_info->channel_map != NULL)
1908 { size_t chanmap_size = psf->sf.channels * sizeof (psf->channel_map [0]) ;
1910 free (psf->channel_map) ;
1912 if ((psf->channel_map = malloc (chanmap_size)) == NULL)
1913 return SFE_MALLOC_FAILED ;
1915 memcpy (psf->channel_map, map_info->channel_map, chanmap_size) ;
1919 } /* aiff_read_chanmap */
1921 /*==============================================================================
1925 aiff_set_chunk (SF_PRIVATE *psf, const SF_CHUNK_INFO * chunk_info)
1926 { return psf_save_write_chunk (&psf->wchunks, chunk_info) ;
1927 } /* aiff_set_chunk */
1929 static SF_CHUNK_ITERATOR *
1930 aiff_next_chunk_iterator (SF_PRIVATE *psf, SF_CHUNK_ITERATOR * iterator)
1931 { return psf_next_chunk_iterator (&psf->rchunks, iterator) ;
1932 } /* aiff_next_chunk_iterator */
1935 aiff_get_chunk_size (SF_PRIVATE *psf, const SF_CHUNK_ITERATOR * iterator, SF_CHUNK_INFO * chunk_info)
1938 if ((indx = psf_find_read_chunk_iterator (&psf->rchunks, iterator)) < 0)
1939 return SFE_UNKNOWN_CHUNK ;
1941 chunk_info->datalen = psf->rchunks.chunks [indx].len ;
1943 return SFE_NO_ERROR ;
1944 } /* aiff_get_chunk_size */
1947 aiff_get_chunk_data (SF_PRIVATE *psf, const SF_CHUNK_ITERATOR * iterator, SF_CHUNK_INFO * chunk_info)
1951 if ((indx = psf_find_read_chunk_iterator (&psf->rchunks, iterator)) < 0)
1952 return SFE_UNKNOWN_CHUNK ;
1954 if (chunk_info->data == NULL)
1955 return SFE_BAD_CHUNK_DATA_PTR ;
1957 chunk_info->id_size = psf->rchunks.chunks [indx].id_size ;
1958 memcpy (chunk_info->id, psf->rchunks.chunks [indx].id, sizeof (chunk_info->id) / sizeof (*chunk_info->id)) ;
1960 pos = psf_ftell (psf) ;
1961 psf_fseek (psf, psf->rchunks.chunks [indx].offset, SEEK_SET) ;
1962 psf_fread (chunk_info->data, SF_MIN (chunk_info->datalen, psf->rchunks.chunks [indx].len), 1, psf) ;
1963 psf_fseek (psf, pos, SEEK_SET) ;
1965 return SFE_NO_ERROR ;
1966 } /* aiff_get_chunk_data */