0113ba651315095e359ee2252eb6b1a99173e925
[Faustine.git] / interpretor / libsndfile-1.0.25 / src / aiff.c
1 /*
2 ** Copyright (C) 1999-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
3 ** Copyright (C) 2005 David Viens <davidv@plogue.com>
4 **
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.
9 **
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.
14 **
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.
18 */
19
20 #include "sfconfig.h"
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <time.h>
26 #include <ctype.h>
27 #include <inttypes.h>
28
29 #include "sndfile.h"
30 #include "sfendian.h"
31 #include "common.h"
32 #include "chanmap.h"
33
34 /*------------------------------------------------------------------------------
35 * Macros to handle big/little endian issues.
36 */
37
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'))
47
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', '!'))
55
56 #define PEAK_MARKER (MAKE_MARKER ('P', 'E', 'A', 'K'))
57 #define basc_MARKER (MAKE_MARKER ('b', 'a', 's', 'c'))
58
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'))
68
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'))
73
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'))
78
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'))
82
83 /*
84 ** This value is officially assigned to Mega Nerd Pty Ltd by Apple
85 ** Corportation as the Application marker for libsndfile.
86 **
87 ** See : http://developer.apple.com/faq/datatype.html
88 */
89 #define m3ga_MARKER (MAKE_MARKER ('m', '3', 'g', 'a'))
90
91 /* Unsupported AIFC encodings.*/
92
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'))
96
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
103
104 /* Is it constant? */
105
106 /* AIFC/IMA4 defines. */
107 #define AIFC_IMA4_BLOCK_LEN 34
108 #define AIFC_IMA4_SAMPLES_PER_BLOCK 64
109
110 #define AIFF_PEAK_CHUNK_SIZE(ch) (2 * sizeof (int) + ch * (sizeof (float) + sizeof (int)))
111
112 /*------------------------------------------------------------------------------
113 * Typedefs for file chunks.
114 */
115
116 enum
117 { HAVE_FORM = 0x01,
118 HAVE_AIFF = 0x02,
119 HAVE_AIFC = 0x04,
120 HAVE_FVER = 0x08,
121 HAVE_COMM = 0x10,
122 HAVE_SSND = 0x20
123 } ;
124
125 typedef struct
126 { unsigned int size ;
127 short numChannels ;
128 unsigned int numSampleFrames ;
129 short sampleSize ;
130 unsigned char sampleRate [10] ;
131 unsigned int encoding ;
132 char zero_bytes [2] ;
133 } COMM_CHUNK ;
134
135 typedef struct
136 { unsigned int offset ;
137 unsigned int blocksize ;
138 } SSND_CHUNK ;
139
140 typedef struct
141 { short playMode ;
142 unsigned short beginLoop ;
143 unsigned short endLoop ;
144 } INST_LOOP ;
145
146 typedef struct
147 { char baseNote ; /* all notes are MIDI note numbers */
148 char detune ; /* cents off, only -50 to +50 are significant */
149 char lowNote ;
150 char highNote ;
151 char lowVelocity ; /* 1 to 127 */
152 char highVelocity ; /* 1 to 127 */
153 short gain ; /* in dB, 0 is normal */
154 INST_LOOP sustain_loop ;
155 INST_LOOP release_loop ;
156 } INST_CHUNK ;
157
158
159 enum
160 { basc_SCALE_MINOR = 1,
161 basc_SCALE_MAJOR,
162 basc_SCALE_NEITHER,
163 basc_SCALE_BOTH
164 } ;
165
166 enum
167 { basc_TYPE_LOOP = 0,
168 basc_TYPE_ONE_SHOT
169 } ;
170
171
172 typedef struct
173 { unsigned int version ;
174 unsigned int numBeats ;
175 unsigned short rootNote ;
176 unsigned short scaleType ;
177 unsigned short sigNumerator ;
178 unsigned short sigDenominator ;
179 unsigned short loopType ;
180 } basc_CHUNK ;
181
182 typedef struct
183 { unsigned short markerID ;
184 unsigned int position ;
185 } MARK_ID_POS ;
186
187 typedef struct
188 { PRIV_CHUNK4 chunk4 ;
189
190 sf_count_t comm_offset ;
191 sf_count_t ssnd_offset ;
192
193 int chanmap_tag ;
194
195 MARK_ID_POS *markstr ;
196 } AIFF_PRIVATE ;
197
198 /*------------------------------------------------------------------------------
199 * Private static functions.
200 */
201
202 static int aiff_close (SF_PRIVATE *psf) ;
203
204 static int tenbytefloat2int (unsigned char *bytes) ;
205 static void uint2tenbytefloat (unsigned int num, unsigned char *bytes) ;
206
207 static int aiff_read_comm_chunk (SF_PRIVATE *psf, COMM_CHUNK *comm_fmt) ;
208
209 static int aiff_read_header (SF_PRIVATE *psf, COMM_CHUNK *comm_fmt) ;
210
211 static int aiff_write_header (SF_PRIVATE *psf, int calc_length) ;
212 static int aiff_write_tailer (SF_PRIVATE *psf) ;
213 static void aiff_write_strings (SF_PRIVATE *psf, int location) ;
214
215 static int aiff_command (SF_PRIVATE *psf, int command, void *data, int datasize) ;
216
217 static const char *get_loop_mode_str (short mode) ;
218
219 static short get_loop_mode (short mode) ;
220
221 static int aiff_read_basc_chunk (SF_PRIVATE * psf, int) ;
222
223 static int aiff_read_chanmap (SF_PRIVATE * psf, unsigned dword) ;
224
225 static unsigned int marker_to_position (const MARK_ID_POS *m, unsigned short n, int marksize) ;
226
227 /*------------------------------------------------------------------------------
228 ** Public function.
229 */
230
231 int
232 aiff_open (SF_PRIVATE *psf)
233 { COMM_CHUNK comm_fmt ;
234 int error, subformat ;
235
236 memset (&comm_fmt, 0, sizeof (comm_fmt)) ;
237
238 subformat = SF_CODEC (psf->sf.format) ;
239
240 if ((psf->container_data = calloc (1, sizeof (AIFF_PRIVATE))) == NULL)
241 return SFE_MALLOC_FAILED ;
242
243 if (psf->file.mode == SFM_READ || (psf->file.mode == SFM_RDWR && psf->filelength > 0))
244 { if ((error = aiff_read_header (psf, &comm_fmt)))
245 return error ;
246 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
247 } ;
248
249 if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
250 { if (psf->is_pipe)
251 return SFE_NO_PIPE_WRITE ;
252
253 if ((SF_CONTAINER (psf->sf.format)) != SF_FORMAT_AIFF)
254 return SFE_BAD_OPEN_FORMAT ;
255
256 if (psf->file.mode == SFM_WRITE && (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE))
257 { if ((psf->peak_info = peak_info_calloc (psf->sf.channels)) == NULL)
258 return SFE_MALLOC_FAILED ;
259 psf->peak_info->peak_loc = SF_PEAK_START ;
260 } ;
261
262 if (psf->file.mode != SFM_RDWR || psf->filelength < 40)
263 { psf->filelength = 0 ;
264 psf->datalength = 0 ;
265 psf->dataoffset = 0 ;
266 psf->sf.frames = 0 ;
267 } ;
268
269 psf->str_flags = SF_STR_ALLOW_START | SF_STR_ALLOW_END ;
270
271 if ((error = aiff_write_header (psf, SF_FALSE)))
272 return error ;
273
274 psf->write_header = aiff_write_header ;
275 } ;
276
277 psf->container_close = aiff_close ;
278 psf->command = aiff_command ;
279
280 switch (SF_CODEC (psf->sf.format))
281 { case SF_FORMAT_PCM_U8 :
282 error = pcm_init (psf) ;
283 break ;
284
285 case SF_FORMAT_PCM_S8 :
286 error = pcm_init (psf) ;
287 break ;
288
289 case SF_FORMAT_PCM_16 :
290 case SF_FORMAT_PCM_24 :
291 case SF_FORMAT_PCM_32 :
292 error = pcm_init (psf) ;
293 break ;
294
295 case SF_FORMAT_ULAW :
296 error = ulaw_init (psf) ;
297 break ;
298
299 case SF_FORMAT_ALAW :
300 error = alaw_init (psf) ;
301 break ;
302
303 /* Lite remove start */
304 case SF_FORMAT_FLOAT :
305 error = float32_init (psf) ;
306 break ;
307
308 case SF_FORMAT_DOUBLE :
309 error = double64_init (psf) ;
310 break ;
311
312 case SF_FORMAT_DWVW_12 :
313 error = dwvw_init (psf, 12) ;
314 break ;
315
316 case SF_FORMAT_DWVW_16 :
317 error = dwvw_init (psf, 16) ;
318 break ;
319
320 case SF_FORMAT_DWVW_24 :
321 error = dwvw_init (psf, 24) ;
322 break ;
323
324 case SF_FORMAT_DWVW_N :
325 if (psf->file.mode != SFM_READ)
326 { error = SFE_DWVW_BAD_BITWIDTH ;
327 break ;
328 } ;
329 if (comm_fmt.sampleSize >= 8 && comm_fmt.sampleSize < 24)
330 { error = dwvw_init (psf, comm_fmt.sampleSize) ;
331 psf->sf.frames = comm_fmt.numSampleFrames ;
332 break ;
333 } ;
334 psf_log_printf (psf, "AIFC/DWVW : Bad bitwidth %d\n", comm_fmt.sampleSize) ;
335 error = SFE_DWVW_BAD_BITWIDTH ;
336 break ;
337
338 case SF_FORMAT_IMA_ADPCM :
339 /*
340 ** IMA ADPCM encoded AIFF files always have a block length
341 ** of 34 which decodes to 64 samples.
342 */
343 error = aiff_ima_init (psf, AIFC_IMA4_BLOCK_LEN, AIFC_IMA4_SAMPLES_PER_BLOCK) ;
344 break ;
345 /* Lite remove end */
346
347 case SF_FORMAT_GSM610 :
348 error = gsm610_init (psf) ;
349 break ;
350
351 default : return SFE_UNIMPLEMENTED ;
352 } ;
353
354
355 return error ;
356 } /* aiff_open */
357
358 /*==========================================================================================
359 ** Private functions.
360 */
361
362 /* This function ought to check size */
363 static unsigned int
364 marker_to_position (const MARK_ID_POS *m, unsigned short n, int marksize)
365 { int i ;
366
367 for (i = 0 ; i < marksize ; i++)
368 if (m [i].markerID == n)
369 return m [i].position ;
370 return 0 ;
371 } /* marker_to_position */
372
373 static int
374 aiff_read_header (SF_PRIVATE *psf, COMM_CHUNK *comm_fmt)
375 { SSND_CHUNK ssnd_fmt ;
376 AIFF_PRIVATE *paiff ;
377 unsigned marker, dword, FORMsize, SSNDsize, bytesread ;
378 int k, found_chunk = 0, done = 0, error = 0 ;
379 char *cptr ;
380 int instr_found = 0, mark_found = 0, mark_count = 0 ;
381
382 if (psf->filelength > SF_PLATFORM_S64 (0xffffffff))
383 psf_log_printf (psf, "Warning : filelength > 0xffffffff. This is bad!!!!\n") ;
384
385 if ((paiff = psf->container_data) == NULL)
386 return SFE_INTERNAL ;
387
388 paiff->comm_offset = 0 ;
389 paiff->ssnd_offset = 0 ;
390
391 /* Set position to start of file to begin reading header. */
392 psf_binheader_readf (psf, "p", 0) ;
393
394 memset (comm_fmt, 0, sizeof (COMM_CHUNK)) ;
395
396 /* Until recently AIF* file were all BIG endian. */
397 psf->endian = SF_ENDIAN_BIG ;
398
399 /* AIFF files can apparently have their chunks in any order. However, they
400 ** must have a FORM chunk. Approach here is to read all the chunks one by
401 ** one and then check for the mandatory chunks at the end.
402 */
403 while (! done)
404 { psf_binheader_readf (psf, "m", &marker) ;
405
406 if (psf->file.mode == SFM_RDWR && (found_chunk & HAVE_SSND))
407 return SFE_AIFF_RW_SSND_NOT_LAST ;
408
409 switch (marker)
410 { case FORM_MARKER :
411 if (found_chunk)
412 return SFE_AIFF_NO_FORM ;
413
414 psf_binheader_readf (psf, "E4", &FORMsize) ;
415 pchk4_store (&paiff->chunk4, marker, psf->headindex - 8, FORMsize) ;
416
417 if (psf->fileoffset > 0 && psf->filelength > FORMsize + 8)
418 { /* Set file length. */
419 psf->filelength = FORMsize + 8 ;
420 psf_log_printf (psf, "FORM : %u\n", FORMsize) ;
421 }
422 else if (FORMsize != psf->filelength - 2 * SIGNED_SIZEOF (dword))
423 { dword = psf->filelength - 2 * sizeof (dword) ;
424 psf_log_printf (psf, "FORM : %u (should be %u)\n", FORMsize, dword) ;
425 FORMsize = dword ;
426 }
427 else
428 psf_log_printf (psf, "FORM : %u\n", FORMsize) ;
429 found_chunk |= HAVE_FORM ;
430 break ;
431
432 case AIFC_MARKER :
433 case AIFF_MARKER :
434 if ((found_chunk & HAVE_FORM) == 0)
435 return SFE_AIFF_AIFF_NO_FORM ;
436 psf_log_printf (psf, " %M\n", marker) ;
437 found_chunk |= (marker == AIFC_MARKER) ? (HAVE_AIFC | HAVE_AIFF) : HAVE_AIFF ;
438 break ;
439
440 case COMM_MARKER :
441 paiff->comm_offset = psf_ftell (psf) - 4 ;
442 error = aiff_read_comm_chunk (psf, comm_fmt) ;
443 pchk4_store (&paiff->chunk4, marker, paiff->comm_offset, comm_fmt->size) ;
444
445
446 psf->sf.samplerate = tenbytefloat2int (comm_fmt->sampleRate) ;
447 psf->sf.frames = comm_fmt->numSampleFrames ;
448 psf->sf.channels = comm_fmt->numChannels ;
449 psf->bytewidth = BITWIDTH2BYTES (comm_fmt->sampleSize) ;
450
451 if (error)
452 return error ;
453
454 found_chunk |= HAVE_COMM ;
455 break ;
456
457 case PEAK_MARKER :
458 /* Must have COMM chunk before PEAK chunk. */
459 if ((found_chunk & (HAVE_FORM | HAVE_AIFF | HAVE_COMM)) != (HAVE_FORM | HAVE_AIFF | HAVE_COMM))
460 return SFE_AIFF_PEAK_B4_COMM ;
461
462 psf_binheader_readf (psf, "E4", &dword) ;
463 pchk4_store (&paiff->chunk4, marker, psf->headindex - 8, dword) ;
464
465 psf_log_printf (psf, "%M : %d\n", marker, dword) ;
466 if (dword != AIFF_PEAK_CHUNK_SIZE (psf->sf.channels))
467 { psf_binheader_readf (psf, "j", dword) ;
468 psf_log_printf (psf, "*** File PEAK chunk too big.\n") ;
469 return SFE_WAV_BAD_PEAK ;
470 } ;
471
472 if ((psf->peak_info = peak_info_calloc (psf->sf.channels)) == NULL)
473 return SFE_MALLOC_FAILED ;
474
475 /* read in rest of PEAK chunk. */
476 psf_binheader_readf (psf, "E44", &(psf->peak_info->version), &(psf->peak_info->timestamp)) ;
477
478 if (psf->peak_info->version != 1)
479 psf_log_printf (psf, " version : %d *** (should be version 1)\n", psf->peak_info->version) ;
480 else
481 psf_log_printf (psf, " version : %d\n", psf->peak_info->version) ;
482
483 psf_log_printf (psf, " time stamp : %d\n", psf->peak_info->timestamp) ;
484 psf_log_printf (psf, " Ch Position Value\n") ;
485
486 cptr = psf->u.cbuf ;
487 for (dword = 0 ; dword < (unsigned) psf->sf.channels ; dword++)
488 { float value ;
489 unsigned int position ;
490
491 psf_binheader_readf (psf, "Ef4", &value, &position) ;
492 psf->peak_info->peaks [dword].value = value ;
493 psf->peak_info->peaks [dword].position = position ;
494
495 snprintf (cptr, sizeof (psf->u.scbuf), " %2d %-12" PRId64 " %g\n",
496 dword, psf->peak_info->peaks [dword].position, psf->peak_info->peaks [dword].value) ;
497 cptr [sizeof (psf->u.scbuf) - 1] = 0 ;
498 psf_log_printf (psf, "%s", cptr) ;
499 } ;
500
501 psf->peak_info->peak_loc = ((found_chunk & HAVE_SSND) == 0) ? SF_PEAK_START : SF_PEAK_END ;
502 break ;
503
504 case SSND_MARKER :
505 if ((found_chunk & HAVE_AIFC) && (found_chunk & HAVE_FVER) == 0)
506 psf_log_printf (psf, "*** Valid AIFC files should have an FVER chunk.\n") ;
507
508 paiff->ssnd_offset = psf_ftell (psf) - 4 ;
509 psf_binheader_readf (psf, "E444", &SSNDsize, &(ssnd_fmt.offset), &(ssnd_fmt.blocksize)) ;
510 pchk4_store (&paiff->chunk4, marker, paiff->ssnd_offset, SSNDsize) ;
511
512 psf->datalength = SSNDsize - sizeof (ssnd_fmt) ;
513 psf->dataoffset = psf_ftell (psf) ;
514
515 if (psf->datalength > psf->filelength - psf->dataoffset || psf->datalength < 0)
516 { psf_log_printf (psf, " SSND : %u (should be %D)\n", SSNDsize, psf->filelength - psf->dataoffset + sizeof (SSND_CHUNK)) ;
517 psf->datalength = psf->filelength - psf->dataoffset ;
518 }
519 else
520 psf_log_printf (psf, " SSND : %u\n", SSNDsize) ;
521
522 if (ssnd_fmt.offset == 0 || psf->dataoffset + ssnd_fmt.offset == ssnd_fmt.blocksize)
523 { psf_log_printf (psf, " Offset : %u\n", ssnd_fmt.offset) ;
524 psf_log_printf (psf, " Block Size : %u\n", ssnd_fmt.blocksize) ;
525
526 psf->dataoffset += ssnd_fmt.offset ;
527 psf->datalength -= ssnd_fmt.offset ;
528 }
529 else
530 { psf_log_printf (psf, " Offset : %u\n", ssnd_fmt.offset) ;
531 psf_log_printf (psf, " Block Size : %u ???\n", ssnd_fmt.blocksize) ;
532 psf->dataoffset += ssnd_fmt.offset ;
533 psf->datalength -= ssnd_fmt.offset ;
534 } ;
535
536 /* Only set dataend if there really is data at the end. */
537 if (psf->datalength + psf->dataoffset < psf->filelength)
538 psf->dataend = psf->datalength + psf->dataoffset ;
539
540 found_chunk |= HAVE_SSND ;
541
542 if (! psf->sf.seekable)
543 break ;
544
545 /* Seek to end of SSND chunk. */
546 psf_fseek (psf, psf->dataoffset + psf->datalength + (SSNDsize & 1), SEEK_SET) ;
547 break ;
548
549 case c_MARKER :
550 psf_binheader_readf (psf, "E4", &dword) ;
551 pchk4_store (&paiff->chunk4, marker, psf_ftell (psf) - 8, dword) ;
552 if (dword == 0)
553 break ;
554 if (dword >= SIGNED_SIZEOF (psf->u.scbuf))
555 { psf_log_printf (psf, " %M : %d (too big)\n", marker, dword) ;
556 return SFE_INTERNAL ;
557 } ;
558
559 cptr = psf->u.cbuf ;
560 psf_binheader_readf (psf, "b", cptr, dword + (dword & 1)) ;
561 cptr [dword] = 0 ;
562
563 psf_sanitize_string (cptr, dword) ;
564
565 psf_log_printf (psf, " %M : %s\n", marker, cptr) ;
566 psf_store_string (psf, SF_STR_COPYRIGHT, cptr) ;
567 break ;
568
569 case AUTH_MARKER :
570 psf_binheader_readf (psf, "E4", &dword) ;
571 pchk4_store (&paiff->chunk4, marker, psf_ftell (psf) - 8, dword) ;
572 if (dword == 0)
573 break ;
574 if (dword >= SIGNED_SIZEOF (psf->u.scbuf) - 1)
575 { psf_log_printf (psf, " %M : %d (too big)\n", marker, dword) ;
576 return SFE_INTERNAL ;
577 } ;
578
579 cptr = psf->u.cbuf ;
580 psf_binheader_readf (psf, "b", cptr, dword + (dword & 1)) ;
581 cptr [dword] = 0 ;
582 psf_log_printf (psf, " %M : %s\n", marker, cptr) ;
583 psf_store_string (psf, SF_STR_ARTIST, cptr) ;
584 break ;
585
586 case COMT_MARKER :
587 { unsigned short count, id, len ;
588 unsigned int timestamp ;
589
590 psf_binheader_readf (psf, "E42", &dword, &count) ;
591 pchk4_store (&paiff->chunk4, marker, psf_ftell (psf) - 8, dword) ;
592 psf_log_printf (psf, " %M : %d\n count : %d\n", marker, dword, count) ;
593 dword += (dword & 1) ;
594 if (dword == 0)
595 break ;
596 dword -= 2 ;
597
598 for (k = 0 ; k < count ; k++)
599 { dword -= psf_binheader_readf (psf, "E422", &timestamp, &id, &len) ;
600 psf_log_printf (psf, " time : 0x%x\n marker : %x\n length : %d\n", timestamp, id, len) ;
601
602 if (len + 1 > SIGNED_SIZEOF (psf->u.scbuf))
603 { psf_log_printf (psf, "\nError : string length (%d) too big.\n", len) ;
604 return SFE_INTERNAL ;
605 } ;
606
607 cptr = psf->u.cbuf ;
608 dword -= psf_binheader_readf (psf, "b", cptr, len) ;
609 cptr [len] = 0 ;
610 psf_log_printf (psf, " string : %s\n", cptr) ;
611 } ;
612
613 if (dword > 0)
614 psf_binheader_readf (psf, "j", dword) ;
615 } ;
616 break ;
617
618 case APPL_MARKER :
619 { unsigned appl_marker ;
620
621 psf_binheader_readf (psf, "E4", &dword) ;
622 pchk4_store (&paiff->chunk4, marker, psf_ftell (psf) - 8, dword) ;
623 if (dword == 0)
624 break ;
625 if (dword >= SIGNED_SIZEOF (psf->u.scbuf) - 1)
626 { psf_log_printf (psf, " %M : %d (too big, skipping)\n", marker, dword) ;
627 psf_binheader_readf (psf, "j", dword + (dword & 1)) ;
628 break ;
629 } ;
630
631 if (dword < 4)
632 { psf_log_printf (psf, " %M : %d (too small, skipping)\n", marker, dword) ;
633 psf_binheader_readf (psf, "j", dword + (dword & 1)) ;
634 break ;
635 } ;
636
637 cptr = psf->u.cbuf ;
638 psf_binheader_readf (psf, "mb", &appl_marker, cptr, dword + (dword & 1) - 4) ;
639 cptr [dword] = 0 ;
640
641 for (k = 0 ; k < (int) dword ; k++)
642 if (! psf_isprint (cptr [k]))
643 { cptr [k] = 0 ;
644 break ;
645 } ;
646
647 psf_log_printf (psf, " %M : %d\n AppSig : %M\n Name : %s\n", marker, dword, appl_marker, cptr) ;
648 psf_store_string (psf, SF_STR_SOFTWARE, cptr) ;
649 } ;
650 break ;
651
652 case NAME_MARKER :
653 psf_binheader_readf (psf, "E4", &dword) ;
654 pchk4_store (&paiff->chunk4, marker, psf_ftell (psf) - 8, dword) ;
655 if (dword == 0)
656 break ;
657 if (dword >= SIGNED_SIZEOF (psf->u.scbuf) - 2)
658 { psf_log_printf (psf, " %M : %d (too big)\n", marker, dword) ;
659 return SFE_INTERNAL ;
660 } ;
661
662 cptr = psf->u.cbuf ;
663 psf_binheader_readf (psf, "b", cptr, dword + (dword & 1)) ;
664 cptr [dword] = 0 ;
665 psf_log_printf (psf, " %M : %s\n", marker, cptr) ;
666 psf_store_string (psf, SF_STR_TITLE, cptr) ;
667 break ;
668
669 case ANNO_MARKER :
670 psf_binheader_readf (psf, "E4", &dword) ;
671 pchk4_store (&paiff->chunk4, marker, psf_ftell (psf) - 8, dword) ;
672 if (dword == 0)
673 break ;
674 if (dword >= SIGNED_SIZEOF (psf->u.scbuf) - 2)
675 { psf_log_printf (psf, " %M : %d (too big)\n", marker, dword) ;
676 return SFE_INTERNAL ;
677 } ;
678
679 cptr = psf->u.cbuf ;
680 psf_binheader_readf (psf, "b", cptr, dword + (dword & 1)) ;
681 cptr [dword] = 0 ;
682 psf_log_printf (psf, " %M : %s\n", marker, cptr) ;
683 psf_store_string (psf, SF_STR_COMMENT, cptr) ;
684 break ;
685
686 case INST_MARKER :
687 psf_binheader_readf (psf, "E4", &dword) ;
688 pchk4_store (&paiff->chunk4, marker, psf_ftell (psf) - 8, dword) ;
689 if (dword != SIZEOF_INST_CHUNK)
690 { psf_log_printf (psf, " %M : %d (should be %d)\n", marker, dword, SIZEOF_INST_CHUNK) ;
691 psf_binheader_readf (psf, "j", dword) ;
692 break ;
693 } ;
694 psf_log_printf (psf, " %M : %d\n", marker, dword) ;
695 { unsigned char bytes [6] ;
696 short gain ;
697
698 if (psf->instrument == NULL && (psf->instrument = psf_instrument_alloc ()) == NULL)
699 return SFE_MALLOC_FAILED ;
700
701 psf_binheader_readf (psf, "b", bytes, 6) ;
702 psf_log_printf (psf, " Base Note : %u\n Detune : %u\n"
703 " Low Note : %u\n High Note : %u\n"
704 " Low Vel. : %u\n High Vel. : %u\n",
705 bytes [0], bytes [1], bytes [2], bytes [3], bytes [4], bytes [5]) ;
706 psf->instrument->basenote = bytes [0] ;
707 psf->instrument->detune = bytes [1] ;
708 psf->instrument->key_lo = bytes [2] ;
709 psf->instrument->key_hi = bytes [3] ;
710 psf->instrument->velocity_lo = bytes [4] ;
711 psf->instrument->velocity_hi = bytes [5] ;
712 psf_binheader_readf (psf, "E2", &gain) ;
713 psf->instrument->gain = gain ;
714 psf_log_printf (psf, " Gain (dB) : %d\n", gain) ;
715 } ;
716 { short mode ; /* 0 - no loop, 1 - forward looping, 2 - backward looping */
717 const char *loop_mode ;
718 unsigned short begin, end ;
719
720 psf_binheader_readf (psf, "E222", &mode, &begin, &end) ;
721 loop_mode = get_loop_mode_str (mode) ;
722 mode = get_loop_mode (mode) ;
723 if (mode == SF_LOOP_NONE)
724 { psf->instrument->loop_count = 0 ;
725 psf->instrument->loops [0].mode = SF_LOOP_NONE ;
726 }
727 else
728 { psf->instrument->loop_count = 1 ;
729 psf->instrument->loops [0].mode = SF_LOOP_FORWARD ;
730 psf->instrument->loops [0].start = begin ;
731 psf->instrument->loops [0].end = end ;
732 psf->instrument->loops [0].count = 0 ;
733 } ;
734 psf_log_printf (psf, " Sustain\n mode : %d => %s\n begin : %u\n end : %u\n",
735 mode, loop_mode, begin, end) ;
736 psf_binheader_readf (psf, "E222", &mode, &begin, &end) ;
737 loop_mode = get_loop_mode_str (mode) ;
738 mode = get_loop_mode (mode) ;
739 if (mode == SF_LOOP_NONE)
740 psf->instrument->loops [1].mode = SF_LOOP_NONE ;
741 else
742 { psf->instrument->loop_count += 1 ;
743 psf->instrument->loops [1].mode = SF_LOOP_FORWARD ;
744 psf->instrument->loops [1].start = begin ;
745 psf->instrument->loops [1].end = end ;
746 psf->instrument->loops [1].count = 0 ;
747 } ;
748 psf_log_printf (psf, " Release\n mode : %d => %s\n begin : %u\n end : %u\n",
749 mode, loop_mode, begin, end) ;
750 } ;
751 instr_found++ ;
752 break ;
753
754 case basc_MARKER :
755 psf_binheader_readf (psf, "E4", &dword) ;
756 pchk4_store (&paiff->chunk4, marker, psf_ftell (psf) - 8, dword) ;
757 psf_log_printf (psf, " basc : %u\n", dword) ;
758
759 if ((error = aiff_read_basc_chunk (psf, dword)))
760 return error ;
761 break ;
762
763 case MARK_MARKER :
764 psf_binheader_readf (psf, "E4", &dword) ;
765 pchk4_store (&paiff->chunk4, marker, psf_ftell (psf) - 8, dword) ;
766 psf_log_printf (psf, " %M : %d\n", marker, dword) ;
767 { unsigned short mark_id, n = 0 ;
768 unsigned int position ;
769
770 bytesread = psf_binheader_readf (psf, "E2", &n) ;
771 mark_count = n ;
772 psf_log_printf (psf, " Count : %d\n", mark_count) ;
773 if (paiff->markstr != NULL)
774 { psf_log_printf (psf, "*** Second MARK chunk found. Throwing away the first.\n") ;
775 free (paiff->markstr) ;
776 } ;
777 paiff->markstr = calloc (mark_count, sizeof (MARK_ID_POS)) ;
778 if (paiff->markstr == NULL)
779 return SFE_MALLOC_FAILED ;
780
781 for (n = 0 ; n < mark_count && bytesread < dword ; n++)
782 { unsigned int pstr_len ;
783 unsigned char ch ;
784
785 bytesread += psf_binheader_readf (psf, "E241", &mark_id, &position, &ch) ;
786 psf_log_printf (psf, " Mark ID : %u\n Position : %u\n", mark_id, position) ;
787
788 pstr_len = (ch & 1) ? ch : ch + 1 ;
789
790 if (pstr_len < sizeof (psf->u.scbuf) - 1)
791 { bytesread += psf_binheader_readf (psf, "b", psf->u.scbuf, pstr_len) ;
792 psf->u.scbuf [pstr_len] = 0 ;
793 }
794 else
795 { unsigned int read_len = pstr_len - (sizeof (psf->u.scbuf) - 1) ;
796 bytesread += psf_binheader_readf (psf, "bj", psf->u.scbuf, read_len, pstr_len - read_len) ;
797 psf->u.scbuf [sizeof (psf->u.scbuf) - 1] = 0 ;
798 }
799
800 psf_log_printf (psf, " Name : %s\n", psf->u.scbuf) ;
801
802 paiff->markstr [n].markerID = mark_id ;
803 paiff->markstr [n].position = position ;
804 /*
805 ** TODO if psf->u.scbuf is equal to
806 ** either Beg_loop, Beg loop or beg loop and spam
807 ** if (psf->instrument == NULL && (psf->instrument = psf_instrument_alloc ()) == NULL)
808 ** return SFE_MALLOC_FAILED ;
809 */
810 } ;
811 } ;
812 mark_found++ ;
813 psf_binheader_readf (psf, "j", dword - bytesread) ;
814 break ;
815
816 case FVER_MARKER :
817 found_chunk |= HAVE_FVER ;
818 /* Fall through to next case. */
819
820 case SFX_MARKER :
821 psf_binheader_readf (psf, "E4", &dword) ;
822 pchk4_store (&paiff->chunk4, marker, psf_ftell (psf) - 8, dword) ;
823 psf_log_printf (psf, " %M : %d\n", marker, dword) ;
824
825 psf_binheader_readf (psf, "j", dword) ;
826 break ;
827
828 case NONE_MARKER :
829 /* Fix for broken AIFC files with incorrect COMM chunk length. */
830 { unsigned char byte ;
831 psf_binheader_readf (psf, "1", &byte) ;
832 dword = byte ;
833 psf_binheader_readf (psf, "j", dword) ;
834 }
835 break ;
836
837 case CHAN_MARKER :
838 psf_binheader_readf (psf, "E4", &dword) ;
839 pchk4_store (&paiff->chunk4, marker, psf_ftell (psf) - 8, dword) ;
840
841 if (dword < 12)
842 { psf_log_printf (psf, " %M : %d (should be >= 12)\n", marker, dword) ;
843 psf_binheader_readf (psf, "j", dword) ;
844 break ;
845 }
846
847 psf_log_printf (psf, " %M : %d\n", marker, dword) ;
848
849 if ((error = aiff_read_chanmap (psf, dword)))
850 return error ;
851 break ;
852
853 default :
854 if (psf_isprint ((marker >> 24) & 0xFF) && psf_isprint ((marker >> 16) & 0xFF)
855 && psf_isprint ((marker >> 8) & 0xFF) && psf_isprint (marker & 0xFF))
856 { psf_binheader_readf (psf, "E4", &dword) ;
857 psf_log_printf (psf, " %M : %d (unknown marker)\n", marker, dword) ;
858
859 psf_binheader_readf (psf, "j", dword) ;
860 break ;
861 } ;
862 if ((dword = psf_ftell (psf)) & 0x03)
863 { psf_log_printf (psf, " Unknown chunk marker %X at position %d. Resyncing.\n", marker, dword - 4) ;
864
865 psf_binheader_readf (psf, "j", -3) ;
866 break ;
867 } ;
868 psf_log_printf (psf, "*** Unknown chunk marker %X at position %D. Exiting parser.\n", marker, psf_ftell (psf)) ;
869 done = 1 ;
870 break ;
871 } ; /* switch (marker) */
872
873 if ((! psf->sf.seekable) && (found_chunk & HAVE_SSND))
874 break ;
875
876 if (psf_ftell (psf) >= psf->filelength - (2 * SIGNED_SIZEOF (dword)))
877 break ;
878 } ; /* while (1) */
879
880 if (instr_found && mark_found)
881 { int j ;
882
883 for (j = 0 ; j<psf->instrument->loop_count ; j ++)
884 { if (j < ARRAY_LEN (psf->instrument->loops))
885 { psf->instrument->loops [j].start = marker_to_position (paiff->markstr, psf->instrument->loops [j].start, mark_count) ;
886 psf->instrument->loops [j].end = marker_to_position (paiff->markstr, psf->instrument->loops [j].end, mark_count) ;
887 psf->instrument->loops [j].mode = SF_LOOP_FORWARD ;
888 } ;
889 } ;
890 } ;
891
892 if (! (found_chunk & HAVE_FORM))
893 return SFE_AIFF_NO_FORM ;
894
895 if (! (found_chunk & HAVE_AIFF))
896 return SFE_AIFF_COMM_NO_FORM ;
897
898 if (! (found_chunk & HAVE_COMM))
899 return SFE_AIFF_SSND_NO_COMM ;
900
901 if (! psf->dataoffset)
902 return SFE_AIFF_NO_DATA ;
903
904 return 0 ;
905 } /* aiff_read_header */
906
907 static int
908 aiff_close (SF_PRIVATE *psf)
909 { AIFF_PRIVATE *paiff = psf->container_data ;
910
911 if (paiff != NULL && paiff->markstr != NULL)
912 { free (paiff->markstr) ;
913 paiff->markstr = NULL ;
914 } ;
915
916 if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
917 { aiff_write_tailer (psf) ;
918 aiff_write_header (psf, SF_TRUE) ;
919 } ;
920
921 return 0 ;
922 } /* aiff_close */
923
924 static int
925 aiff_read_comm_chunk (SF_PRIVATE *psf, COMM_CHUNK *comm_fmt)
926 { int error = 0, bytesread, subformat ;
927
928 psf->u.scbuf [0] = 0 ;
929
930 bytesread = psf_binheader_readf (psf, "E4", &(comm_fmt->size)) ;
931
932 /* The COMM chunk has an int aligned to an odd word boundary. Some
933 ** procesors are not able to deal with this (ie bus fault) so we have
934 ** to take special care.
935 */
936 comm_fmt->size += comm_fmt->size & 1 ;
937
938 bytesread +=
939 psf_binheader_readf (psf, "E242b", &(comm_fmt->numChannels), &(comm_fmt->numSampleFrames),
940 &(comm_fmt->sampleSize), &(comm_fmt->sampleRate), SIGNED_SIZEOF (comm_fmt->sampleRate)) ;
941
942 if (comm_fmt->size > 0x10000 && (comm_fmt->size & 0xffff) == 0)
943 { psf_log_printf (psf, " COMM : %d (0x%x) *** should be ", comm_fmt->size, comm_fmt->size) ;
944 comm_fmt->size = ENDSWAP_INT (comm_fmt->size) ;
945 psf_log_printf (psf, "%d (0x%x)\n", comm_fmt->size, comm_fmt->size) ;
946 }
947 else
948 psf_log_printf (psf, " COMM : %d\n", comm_fmt->size) ;
949
950 if (comm_fmt->size == SIZEOF_AIFF_COMM)
951 comm_fmt->encoding = NONE_MARKER ;
952 else if (comm_fmt->size == SIZEOF_AIFC_COMM_MIN)
953 bytesread += psf_binheader_readf (psf, "Em", &(comm_fmt->encoding)) ;
954 else if (comm_fmt->size >= SIZEOF_AIFC_COMM)
955 { unsigned char encoding_len ;
956 unsigned read_len ;
957
958 bytesread += psf_binheader_readf (psf, "Em1", &(comm_fmt->encoding), &encoding_len) ;
959
960 comm_fmt->size = SF_MIN (sizeof (psf->u.scbuf), make_size_t (comm_fmt->size)) ;
961 memset (psf->u.scbuf, 0, comm_fmt->size) ;
962 read_len = comm_fmt->size - SIZEOF_AIFC_COMM + 1 ;
963 bytesread += psf_binheader_readf (psf, "b", psf->u.scbuf, read_len) ;
964 psf->u.scbuf [read_len + 1] = 0 ;
965 } ;
966
967 psf_log_printf (psf, " Sample Rate : %d\n", tenbytefloat2int (comm_fmt->sampleRate)) ;
968 psf_log_printf (psf, " Frames : %u%s\n", comm_fmt->numSampleFrames, (comm_fmt->numSampleFrames == 0 && psf->filelength > 104) ? " (Should not be 0)" : "") ;
969 psf_log_printf (psf, " Channels : %d\n", comm_fmt->numChannels) ;
970
971 /* Found some broken 'fl32' files with comm.samplesize == 16. Fix it here. */
972 if ((comm_fmt->encoding == fl32_MARKER || comm_fmt->encoding == FL32_MARKER) && comm_fmt->sampleSize != 32)
973 { psf_log_printf (psf, " Sample Size : %d (should be 32)\n", comm_fmt->sampleSize) ;
974 comm_fmt->sampleSize = 32 ;
975 }
976 else if ((comm_fmt->encoding == fl64_MARKER || comm_fmt->encoding == FL64_MARKER) && comm_fmt->sampleSize != 64)
977 { psf_log_printf (psf, " Sample Size : %d (should be 64)\n", comm_fmt->sampleSize) ;
978 comm_fmt->sampleSize = 64 ;
979 }
980 else
981 psf_log_printf (psf, " Sample Size : %d\n", comm_fmt->sampleSize) ;
982
983 subformat = s_bitwidth_to_subformat (comm_fmt->sampleSize) ;
984
985 psf->endian = SF_ENDIAN_BIG ;
986
987 switch (comm_fmt->encoding)
988 { case NONE_MARKER :
989 psf->sf.format = (SF_FORMAT_AIFF | subformat) ;
990 break ;
991
992 case twos_MARKER :
993 case in24_MARKER :
994 case in32_MARKER :
995 psf->sf.format = (SF_ENDIAN_BIG | SF_FORMAT_AIFF | subformat) ;
996 break ;
997
998 case sowt_MARKER :
999 case ni24_MARKER :
1000 case ni32_MARKER :
1001 psf->endian = SF_ENDIAN_LITTLE ;
1002 psf->sf.format = (SF_ENDIAN_LITTLE | SF_FORMAT_AIFF | subformat) ;
1003 break ;
1004
1005 case fl32_MARKER :
1006 case FL32_MARKER :
1007 psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_FLOAT) ;
1008 break ;
1009
1010 case ulaw_MARKER :
1011 case ULAW_MARKER :
1012 psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_ULAW) ;
1013 break ;
1014
1015 case alaw_MARKER :
1016 case ALAW_MARKER :
1017 psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_ALAW) ;
1018 break ;
1019
1020 case fl64_MARKER :
1021 case FL64_MARKER :
1022 psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_DOUBLE) ;
1023 break ;
1024
1025 case raw_MARKER :
1026 psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_PCM_U8) ;
1027 break ;
1028
1029 case DWVW_MARKER :
1030 psf->sf.format = SF_FORMAT_AIFF ;
1031 switch (comm_fmt->sampleSize)
1032 { case 12 :
1033 psf->sf.format |= SF_FORMAT_DWVW_12 ;
1034 break ;
1035 case 16 :
1036 psf->sf.format |= SF_FORMAT_DWVW_16 ;
1037 break ;
1038 case 24 :
1039 psf->sf.format |= SF_FORMAT_DWVW_24 ;
1040 break ;
1041
1042 default :
1043 psf->sf.format |= SF_FORMAT_DWVW_N ;
1044 break ;
1045 } ;
1046 break ;
1047
1048 case GSM_MARKER :
1049 psf->sf.format = SF_FORMAT_AIFF ;
1050 psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_GSM610) ;
1051 break ;
1052
1053
1054 case ima4_MARKER :
1055 psf->endian = SF_ENDIAN_BIG ;
1056 psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_IMA_ADPCM) ;
1057 break ;
1058
1059 default :
1060 psf_log_printf (psf, "AIFC : Unimplemented format : %M\n", comm_fmt->encoding) ;
1061 error = SFE_UNIMPLEMENTED ;
1062 } ;
1063
1064 if (! psf->u.scbuf [0])
1065 psf_log_printf (psf, " Encoding : %M\n", comm_fmt->encoding) ;
1066 else
1067 psf_log_printf (psf, " Encoding : %M => %s\n", comm_fmt->encoding, psf->u.scbuf) ;
1068
1069 return error ;
1070 } /* aiff_read_comm_chunk */
1071
1072
1073 /*==========================================================================================
1074 */
1075
1076 static int
1077 aiff_rewrite_header (SF_PRIVATE *psf, AIFF_PRIVATE * paiff)
1078 {
1079 /* Assuming here that the header has already been written and just
1080 ** needs to be corrected for new data length. That means that we
1081 ** only change the length fields of the FORM and SSND chunks ;
1082 ** everything else can be skipped over.
1083 */
1084 int k, ch ;
1085 int comm_size, comm_frames ;
1086
1087 psf_fseek (psf, 0, SEEK_SET) ;
1088 psf_fread (psf->header, psf->dataoffset, 1, psf) ;
1089
1090 psf->headindex = 0 ;
1091
1092 for (k = 0 ; k < paiff->chunk4.count ; k++)
1093 { switch (paiff->chunk4.l [k].chunk)
1094 { case FORM_MARKER :
1095 psf_binheader_writef (psf, "Etm8", FORM_MARKER, psf->filelength - 8) ;
1096 break ;
1097
1098 case COMM_MARKER :
1099 psf->headindex = paiff->chunk4.l [k].offset ;
1100 comm_frames = psf->sf.frames ;
1101 comm_size = paiff->chunk4.l [k].len ;
1102 psf_binheader_writef (psf, "Em42t4", COMM_MARKER, comm_size, psf->sf.channels, comm_frames) ;
1103 break ;
1104
1105 case SSND_MARKER :
1106 psf->headindex = paiff->chunk4.l [k].offset ;
1107 psf_binheader_writef (psf, "Etm8", SSND_MARKER, psf->datalength + SIZEOF_SSND_CHUNK) ;
1108 break ;
1109
1110 case PEAK_MARKER :
1111 psf->headindex = paiff->chunk4.l [k].offset ;
1112 psf_binheader_writef (psf, "Em4", PEAK_MARKER, AIFF_PEAK_CHUNK_SIZE (psf->sf.channels)) ;
1113 psf_binheader_writef (psf, "E44", 1, time (NULL)) ;
1114 for (ch = 0 ; ch < psf->sf.channels ; ch++)
1115 psf_binheader_writef (psf, "Eft8", (float) psf->peak_info->peaks [ch].value, psf->peak_info->peaks [ch].position) ;
1116 break ;
1117
1118 default :
1119 /* There are a whole bunch of chunks we should just ignore. */
1120 break ;
1121 } ;
1122 } ;
1123
1124 /* Header mangling complete so write it out. */
1125 psf_fseek (psf, 0, SEEK_SET) ;
1126 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
1127
1128 return 0 ;
1129 } /* aiff_rewrite_header */
1130
1131 static int
1132 aiff_write_header (SF_PRIVATE *psf, int calc_length)
1133 { sf_count_t current ;
1134 AIFF_PRIVATE *paiff ;
1135 unsigned char comm_sample_rate [10], comm_zero_bytes [2] = { 0, 0 } ;
1136 unsigned int comm_type, comm_size, comm_encoding, comm_frames = 0 ;
1137 int k, endian, has_data = SF_FALSE ;
1138 short bit_width ;
1139
1140 if ((paiff = psf->container_data) == NULL)
1141 return SFE_INTERNAL ;
1142
1143 current = psf_ftell (psf) ;
1144
1145 if (current > psf->dataoffset)
1146 has_data = SF_TRUE ;
1147
1148 if (calc_length)
1149 { psf->filelength = psf_get_filelen (psf) ;
1150
1151 psf->datalength = psf->filelength - psf->dataoffset ;
1152 if (psf->dataend)
1153 psf->datalength -= psf->filelength - psf->dataend ;
1154
1155 if (psf->bytewidth > 0)
1156 psf->sf.frames = psf->datalength / (psf->bytewidth * psf->sf.channels) ;
1157 } ;
1158
1159 if (psf->file.mode == SFM_RDWR && psf->dataoffset > 0 && paiff->chunk4.count > 0)
1160 { int err = aiff_rewrite_header (psf, paiff) ;
1161 if (current > 0)
1162 psf_fseek (psf, current, SEEK_SET) ;
1163 return err ;
1164 } ;
1165
1166 endian = SF_ENDIAN (psf->sf.format) ;
1167 if (CPU_IS_LITTLE_ENDIAN && endian == SF_ENDIAN_CPU)
1168 endian = SF_ENDIAN_LITTLE ;
1169
1170 /* Standard value here. */
1171 bit_width = psf->bytewidth * 8 ;
1172 comm_frames = (psf->sf.frames > 0xFFFFFFFF) ? 0xFFFFFFFF : psf->sf.frames ;
1173
1174 switch (SF_CODEC (psf->sf.format) | endian)
1175 { case SF_FORMAT_PCM_S8 | SF_ENDIAN_BIG :
1176 psf->endian = SF_ENDIAN_BIG ;
1177 comm_type = AIFC_MARKER ;
1178 comm_size = SIZEOF_AIFC_COMM ;
1179 comm_encoding = twos_MARKER ;
1180 break ;
1181
1182 case SF_FORMAT_PCM_S8 | SF_ENDIAN_LITTLE :
1183 psf->endian = SF_ENDIAN_LITTLE ;
1184 comm_type = AIFC_MARKER ;
1185 comm_size = SIZEOF_AIFC_COMM ;
1186 comm_encoding = sowt_MARKER ;
1187 break ;
1188
1189 case SF_FORMAT_PCM_16 | SF_ENDIAN_BIG :
1190 psf->endian = SF_ENDIAN_BIG ;
1191 comm_type = AIFC_MARKER ;
1192 comm_size = SIZEOF_AIFC_COMM ;
1193 comm_encoding = twos_MARKER ;
1194 break ;
1195
1196 case SF_FORMAT_PCM_16 | SF_ENDIAN_LITTLE :
1197 psf->endian = SF_ENDIAN_LITTLE ;
1198 comm_type = AIFC_MARKER ;
1199 comm_size = SIZEOF_AIFC_COMM ;
1200 comm_encoding = sowt_MARKER ;
1201 break ;
1202
1203 case SF_FORMAT_PCM_24 | SF_ENDIAN_BIG :
1204 psf->endian = SF_ENDIAN_BIG ;
1205 comm_type = AIFC_MARKER ;
1206 comm_size = SIZEOF_AIFC_COMM ;
1207 comm_encoding = in24_MARKER ;
1208 break ;
1209
1210 case SF_FORMAT_PCM_24 | SF_ENDIAN_LITTLE :
1211 psf->endian = SF_ENDIAN_LITTLE ;
1212 comm_type = AIFC_MARKER ;
1213 comm_size = SIZEOF_AIFC_COMM ;
1214 comm_encoding = ni24_MARKER ;
1215 break ;
1216
1217 case SF_FORMAT_PCM_32 | SF_ENDIAN_BIG :
1218 psf->endian = SF_ENDIAN_BIG ;
1219 comm_type = AIFC_MARKER ;
1220 comm_size = SIZEOF_AIFC_COMM ;
1221 comm_encoding = in32_MARKER ;
1222 break ;
1223
1224 case SF_FORMAT_PCM_32 | SF_ENDIAN_LITTLE :
1225 psf->endian = SF_ENDIAN_LITTLE ;
1226 comm_type = AIFC_MARKER ;
1227 comm_size = SIZEOF_AIFC_COMM ;
1228 comm_encoding = ni32_MARKER ;
1229 break ;
1230
1231 case SF_FORMAT_PCM_S8 : /* SF_ENDIAN_FILE */
1232 case SF_FORMAT_PCM_16 :
1233 case SF_FORMAT_PCM_24 :
1234 case SF_FORMAT_PCM_32 :
1235 psf->endian = SF_ENDIAN_BIG ;
1236 comm_type = AIFF_MARKER ;
1237 comm_size = SIZEOF_AIFF_COMM ;
1238 comm_encoding = 0 ;
1239 break ;
1240
1241 case SF_FORMAT_FLOAT : /* Big endian floating point. */
1242 psf->endian = SF_ENDIAN_BIG ;
1243 comm_type = AIFC_MARKER ;
1244 comm_size = SIZEOF_AIFC_COMM ;
1245 comm_encoding = FL32_MARKER ; /* Use 'FL32' because its easier to read. */
1246 break ;
1247
1248 case SF_FORMAT_DOUBLE : /* Big endian double precision floating point. */
1249 psf->endian = SF_ENDIAN_BIG ;
1250 comm_type = AIFC_MARKER ;
1251 comm_size = SIZEOF_AIFC_COMM ;
1252 comm_encoding = FL64_MARKER ; /* Use 'FL64' because its easier to read. */
1253 break ;
1254
1255 case SF_FORMAT_ULAW :
1256 psf->endian = SF_ENDIAN_BIG ;
1257 comm_type = AIFC_MARKER ;
1258 comm_size = SIZEOF_AIFC_COMM ;
1259 comm_encoding = ulaw_MARKER ;
1260 break ;
1261
1262 case SF_FORMAT_ALAW :
1263 psf->endian = SF_ENDIAN_BIG ;
1264 comm_type = AIFC_MARKER ;
1265 comm_size = SIZEOF_AIFC_COMM ;
1266 comm_encoding = alaw_MARKER ;
1267 break ;
1268
1269 case SF_FORMAT_PCM_U8 :
1270 psf->endian = SF_ENDIAN_BIG ;
1271 comm_type = AIFC_MARKER ;
1272 comm_size = SIZEOF_AIFC_COMM ;
1273 comm_encoding = raw_MARKER ;
1274 break ;
1275
1276 case SF_FORMAT_DWVW_12 :
1277 psf->endian = SF_ENDIAN_BIG ;
1278 comm_type = AIFC_MARKER ;
1279 comm_size = SIZEOF_AIFC_COMM ;
1280 comm_encoding = DWVW_MARKER ;
1281
1282 /* Override standard value here.*/
1283 bit_width = 12 ;
1284 break ;
1285
1286 case SF_FORMAT_DWVW_16 :
1287 psf->endian = SF_ENDIAN_BIG ;
1288 comm_type = AIFC_MARKER ;
1289 comm_size = SIZEOF_AIFC_COMM ;
1290 comm_encoding = DWVW_MARKER ;
1291
1292 /* Override standard value here.*/
1293 bit_width = 16 ;
1294 break ;
1295
1296 case SF_FORMAT_DWVW_24 :
1297 psf->endian = SF_ENDIAN_BIG ;
1298 comm_type = AIFC_MARKER ;
1299 comm_size = SIZEOF_AIFC_COMM ;
1300 comm_encoding = DWVW_MARKER ;
1301
1302 /* Override standard value here.*/
1303 bit_width = 24 ;
1304 break ;
1305
1306 case SF_FORMAT_GSM610 :
1307 psf->endian = SF_ENDIAN_BIG ;
1308 comm_type = AIFC_MARKER ;
1309 comm_size = SIZEOF_AIFC_COMM ;
1310 comm_encoding = GSM_MARKER ;
1311
1312 /* Override standard value here.*/
1313 bit_width = 16 ;
1314 break ;
1315
1316 case SF_FORMAT_IMA_ADPCM :
1317 psf->endian = SF_ENDIAN_BIG ;
1318 comm_type = AIFC_MARKER ;
1319 comm_size = SIZEOF_AIFC_COMM ;
1320 comm_encoding = ima4_MARKER ;
1321
1322 /* Override standard value here.*/
1323 bit_width = 16 ;
1324 comm_frames = psf->sf.frames / AIFC_IMA4_SAMPLES_PER_BLOCK ;
1325 break ;
1326
1327 default : return SFE_BAD_OPEN_FORMAT ;
1328 } ;
1329
1330 /* Reset the current header length to zero. */
1331 psf->header [0] = 0 ;
1332 psf->headindex = 0 ;
1333 psf_fseek (psf, 0, SEEK_SET) ;
1334
1335 psf_binheader_writef (psf, "Etm8", FORM_MARKER, psf->filelength - 8) ;
1336
1337 /* Write AIFF/AIFC marker and COM chunk. */
1338 if (comm_type == AIFC_MARKER)
1339 /* AIFC must have an FVER chunk. */
1340 psf_binheader_writef (psf, "Emm44", comm_type, FVER_MARKER, 4, 0xA2805140) ;
1341 else
1342 psf_binheader_writef (psf, "Em", comm_type) ;
1343
1344 paiff->comm_offset = psf->headindex - 8 ;
1345
1346 memset (comm_sample_rate, 0, sizeof (comm_sample_rate)) ;
1347 uint2tenbytefloat (psf->sf.samplerate, comm_sample_rate) ;
1348
1349 psf_binheader_writef (psf, "Em42t42", COMM_MARKER, comm_size, psf->sf.channels, comm_frames, bit_width) ;
1350 psf_binheader_writef (psf, "b", comm_sample_rate, sizeof (comm_sample_rate)) ;
1351
1352 /* AIFC chunks have some extra data. */
1353 if (comm_type == AIFC_MARKER)
1354 psf_binheader_writef (psf, "mb", comm_encoding, comm_zero_bytes, sizeof (comm_zero_bytes)) ;
1355
1356 if (psf->channel_map && paiff->chanmap_tag)
1357 psf_binheader_writef (psf, "Em4444", CHAN_MARKER, 12, paiff->chanmap_tag, 0, 0) ;
1358
1359 if (psf->instrument != NULL)
1360 { MARK_ID_POS m [4] ;
1361 INST_CHUNK ch ;
1362 unsigned short ct = 0 ;
1363
1364 memset (m, 0, sizeof (m)) ;
1365 memset (&ch, 0, sizeof (ch)) ;
1366
1367 ch.baseNote = psf->instrument->basenote ;
1368 ch.detune = psf->instrument->detune ;
1369 ch.lowNote = psf->instrument->key_lo ;
1370 ch.highNote = psf->instrument->key_hi ;
1371 ch.lowVelocity = psf->instrument->velocity_lo ;
1372 ch.highVelocity = psf->instrument->velocity_hi ;
1373 ch.gain = psf->instrument->gain ;
1374 if (psf->instrument->loops [0].mode != SF_LOOP_NONE)
1375 { ch.sustain_loop.playMode = 1 ;
1376 ch.sustain_loop.beginLoop = ct ;
1377 m [0].markerID = ct++ ;
1378 m [0].position = psf->instrument->loops [0].start ;
1379 ch.sustain_loop.endLoop = ct ;
1380 m [1].markerID = ct++ ;
1381 m [1].position = psf->instrument->loops [0].end ;
1382 } ;
1383 if (psf->instrument->loops [1].mode != SF_LOOP_NONE)
1384 { ch.release_loop.playMode = 1 ;
1385 ch.release_loop.beginLoop = ct ;
1386 m [2].markerID = ct++ ;
1387 m [2].position = psf->instrument->loops [1].start ;
1388 ch.release_loop.endLoop = ct ;
1389 m [3].markerID = ct++ ;
1390 m [3].position = psf->instrument->loops [1].end ;
1391 }
1392 else
1393 { ch.release_loop.playMode = 0 ;
1394 ch.release_loop.beginLoop = 0 ;
1395 ch.release_loop.endLoop = 0 ;
1396 } ;
1397
1398 psf_binheader_writef (psf, "Em4111111", INST_MARKER, SIZEOF_INST_CHUNK, ch.baseNote, ch.detune,
1399 ch.lowNote, ch.highNote, ch.lowVelocity, ch.highVelocity) ;
1400 psf_binheader_writef (psf, "2222222", ch.gain, ch.sustain_loop.playMode,
1401 ch.sustain_loop.beginLoop, ch.sustain_loop.endLoop, ch.release_loop.playMode,
1402 ch.release_loop.beginLoop, ch.release_loop.endLoop) ;
1403
1404 if (ct == 2)
1405 psf_binheader_writef (psf, "Em42241b241b",
1406 MARK_MARKER, 2 + 2 * (2 + 4 + 1 + 9), 2,
1407 m [0].markerID, m [0].position, 8, "beg loop", make_size_t (9),
1408 m [1].markerID, m [1].position, 8, "end loop", make_size_t (9)) ;
1409 else if (ct == 4)
1410 psf_binheader_writef (psf, "Em42 241b 241b 241b 241b",
1411 MARK_MARKER, 2 + 4 * (2 + 4 + 1 + 9), 4,
1412 m [0].markerID, m [0].position, 8, "beg loop", make_size_t (9),
1413 m [1].markerID, m [1].position, 8, "end loop", make_size_t (9),
1414 m [2].markerID, m [2].position, 8, "beg loop", make_size_t (9),
1415 m [3].markerID, m [3].position, 8, "end loop", make_size_t (9)) ;
1416 } ;
1417
1418 if (psf->str_flags & SF_STR_LOCATE_START)
1419 aiff_write_strings (psf, SF_STR_LOCATE_START) ;
1420
1421 if (psf->peak_info != NULL && psf->peak_info->peak_loc == SF_PEAK_START)
1422 { psf_binheader_writef (psf, "Em4", PEAK_MARKER, AIFF_PEAK_CHUNK_SIZE (psf->sf.channels)) ;
1423 psf_binheader_writef (psf, "E44", 1, time (NULL)) ;
1424 for (k = 0 ; k < psf->sf.channels ; k++)
1425 psf_binheader_writef (psf, "Eft8", (float) psf->peak_info->peaks [k].value, psf->peak_info->peaks [k].position) ;
1426 } ;
1427
1428 /* Write SSND chunk. */
1429 paiff->ssnd_offset = psf->headindex ;
1430 psf_binheader_writef (psf, "Etm844", SSND_MARKER, psf->datalength + SIZEOF_SSND_CHUNK, 0, 0) ;
1431
1432 /* Header construction complete so write it out. */
1433 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
1434
1435 if (psf->error)
1436 return psf->error ;
1437
1438 if (has_data && psf->dataoffset != psf->headindex)
1439 return psf->error = SFE_INTERNAL ;
1440
1441 psf->dataoffset = psf->headindex ;
1442
1443 if (! has_data)
1444 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
1445 else if (current > 0)
1446 psf_fseek (psf, current, SEEK_SET) ;
1447
1448 return psf->error ;
1449 } /* aiff_write_header */
1450
1451 static int
1452 aiff_write_tailer (SF_PRIVATE *psf)
1453 { int k ;
1454
1455 /* Reset the current header length to zero. */
1456 psf->header [0] = 0 ;
1457 psf->headindex = 0 ;
1458
1459 psf->dataend = psf_fseek (psf, 0, SEEK_END) ;
1460
1461 /* Make sure tailer data starts at even byte offset. Pad if necessary. */
1462 if (psf->dataend % 2 == 1)
1463 { psf_fwrite (psf->header, 1, 1, psf) ;
1464 psf->dataend ++ ;
1465 } ;
1466
1467 if (psf->peak_info != NULL && psf->peak_info->peak_loc == SF_PEAK_END)
1468 { psf_binheader_writef (psf, "Em4", PEAK_MARKER, AIFF_PEAK_CHUNK_SIZE (psf->sf.channels)) ;
1469 psf_binheader_writef (psf, "E44", 1, time (NULL)) ;
1470 for (k = 0 ; k < psf->sf.channels ; k++)
1471 psf_binheader_writef (psf, "Eft8", (float) psf->peak_info->peaks [k].value, psf->peak_info->peaks [k].position) ;
1472 } ;
1473
1474 if (psf->str_flags & SF_STR_LOCATE_END)
1475 aiff_write_strings (psf, SF_STR_LOCATE_END) ;
1476
1477 /* Write the tailer. */
1478 if (psf->headindex > 0)
1479 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
1480
1481 return 0 ;
1482 } /* aiff_write_tailer */
1483
1484 static void
1485 aiff_write_strings (SF_PRIVATE *psf, int location)
1486 { int k, slen ;
1487
1488 for (k = 0 ; k < SF_MAX_STRINGS ; k++)
1489 { if (psf->strings [k].type == 0)
1490 break ;
1491
1492 if (psf->strings [k].flags != location)
1493 continue ;
1494
1495 switch (psf->strings [k].type)
1496 { case SF_STR_SOFTWARE :
1497 slen = strlen (psf->strings [k].str) ;
1498 psf_binheader_writef (psf, "Em4mb", APPL_MARKER, slen + 4, m3ga_MARKER, psf->strings [k].str, make_size_t (slen + (slen & 1))) ;
1499 break ;
1500
1501 case SF_STR_TITLE :
1502 psf_binheader_writef (psf, "EmS", NAME_MARKER, psf->strings [k].str) ;
1503 break ;
1504
1505 case SF_STR_COPYRIGHT :
1506 psf_binheader_writef (psf, "EmS", c_MARKER, psf->strings [k].str) ;
1507 break ;
1508
1509 case SF_STR_ARTIST :
1510 psf_binheader_writef (psf, "EmS", AUTH_MARKER, psf->strings [k].str) ;
1511 break ;
1512
1513 case SF_STR_COMMENT :
1514 psf_binheader_writef (psf, "EmS", ANNO_MARKER, psf->strings [k].str) ;
1515 break ;
1516
1517 /*
1518 case SF_STR_DATE :
1519 psf_binheader_writef (psf, "Ems", ICRD_MARKER, psf->strings [k].str) ;
1520 break ;
1521 */
1522 } ;
1523 } ;
1524
1525 return ;
1526 } /* aiff_write_strings */
1527
1528 static int
1529 aiff_command (SF_PRIVATE * psf, int command, void * UNUSED (data), int UNUSED (datasize))
1530 { AIFF_PRIVATE *paiff ;
1531
1532 if ((paiff = psf->container_data) == NULL)
1533 return SFE_INTERNAL ;
1534
1535 switch (command)
1536 { case SFC_SET_CHANNEL_MAP_INFO :
1537 paiff->chanmap_tag = aiff_caf_find_channel_layout_tag (psf->channel_map, psf->sf.channels) ;
1538 return (paiff->chanmap_tag != 0) ;
1539
1540 default :
1541 break ;
1542 } ;
1543
1544
1545
1546 return 0 ;
1547 } /* aiff_command */
1548
1549 static const char*
1550 get_loop_mode_str (short mode)
1551 { switch (mode)
1552 { case 0 : return "none" ;
1553 case 1 : return "forward" ;
1554 case 2 : return "backward" ;
1555 } ;
1556
1557 return "*** unknown" ;
1558 } /* get_loop_mode_str */
1559
1560 static short
1561 get_loop_mode (short mode)
1562 { switch (mode)
1563 { case 0 : return SF_LOOP_NONE ;
1564 case 1 : return SF_LOOP_FORWARD ;
1565 case 2 : return SF_LOOP_BACKWARD ;
1566 } ;
1567
1568 return SF_LOOP_NONE ;
1569 } /* get_loop_mode */
1570
1571 /*==========================================================================================
1572 ** Rough hack at converting from 80 bit IEEE float in AIFF header to an int and
1573 ** back again. It assumes that all sample rates are between 1 and 800MHz, which
1574 ** should be OK as other sound file formats use a 32 bit integer to store sample
1575 ** rate.
1576 ** There is another (probably better) version in the source code to the SoX but it
1577 ** has a copyright which probably prevents it from being allowable as GPL/LGPL.
1578 */
1579
1580 static int
1581 tenbytefloat2int (unsigned char *bytes)
1582 { int val = 3 ;
1583
1584 if (bytes [0] & 0x80) /* Negative number. */
1585 return 0 ;
1586
1587 if (bytes [0] <= 0x3F) /* Less than 1. */
1588 return 1 ;
1589
1590 if (bytes [0] > 0x40) /* Way too big. */
1591 return 0x4000000 ;
1592
1593 if (bytes [0] == 0x40 && bytes [1] > 0x1C) /* Too big. */
1594 return 800000000 ;
1595
1596 /* Ok, can handle it. */
1597
1598 val = (bytes [2] << 23) | (bytes [3] << 15) | (bytes [4] << 7) | (bytes [5] >> 1) ;
1599
1600 val >>= (29 - bytes [1]) ;
1601
1602 return val ;
1603 } /* tenbytefloat2int */
1604
1605 static void
1606 uint2tenbytefloat (unsigned int num, unsigned char *bytes)
1607 { unsigned int mask = 0x40000000 ;
1608 int count ;
1609
1610 if (num <= 1)
1611 { bytes [0] = 0x3F ;
1612 bytes [1] = 0xFF ;
1613 bytes [2] = 0x80 ;
1614 return ;
1615 } ;
1616
1617 bytes [0] = 0x40 ;
1618
1619 if (num >= mask)
1620 { bytes [1] = 0x1D ;
1621 return ;
1622 } ;
1623
1624 for (count = 0 ; count <= 32 ; count ++)
1625 { if (num & mask)
1626 break ;
1627 mask >>= 1 ;
1628 } ;
1629
1630 num <<= count + 1 ;
1631 bytes [1] = 29 - count ;
1632 bytes [2] = (num >> 24) & 0xFF ;
1633 bytes [3] = (num >> 16) & 0xFF ;
1634 bytes [4] = (num >> 8) & 0xFF ;
1635 bytes [5] = num & 0xFF ;
1636
1637 } /* uint2tenbytefloat */
1638
1639 static int
1640 aiff_read_basc_chunk (SF_PRIVATE * psf, int datasize)
1641 { const char * type_str ;
1642 basc_CHUNK bc ;
1643 int count ;
1644
1645 count = psf_binheader_readf (psf, "E442", &bc.version, &bc.numBeats, &bc.rootNote) ;
1646 count += psf_binheader_readf (psf, "E222", &bc.scaleType, &bc.sigNumerator, &bc.sigDenominator) ;
1647 count += psf_binheader_readf (psf, "E2j", &bc.loopType, datasize - sizeof (bc)) ;
1648
1649 psf_log_printf (psf, " Version ? : %u\n Num Beats : %u\n Root Note : 0x%x\n",
1650 bc.version, bc.numBeats, bc.rootNote) ;
1651
1652 switch (bc.scaleType)
1653 { case basc_SCALE_MINOR :
1654 type_str = "MINOR" ;
1655 break ;
1656 case basc_SCALE_MAJOR :
1657 type_str = "MAJOR" ;
1658 break ;
1659 case basc_SCALE_NEITHER :
1660 type_str = "NEITHER" ;
1661 break ;
1662 case basc_SCALE_BOTH :
1663 type_str = "BOTH" ;
1664 break ;
1665 default :
1666 type_str = "!!WRONG!!" ;
1667 break ;
1668 } ;
1669
1670 psf_log_printf (psf, " ScaleType : 0x%x (%s)\n", bc.scaleType, type_str) ;
1671 psf_log_printf (psf, " Time Sig : %d/%d\n", bc.sigNumerator, bc.sigDenominator) ;
1672
1673 switch (bc.loopType)
1674 { case basc_TYPE_ONE_SHOT :
1675 type_str = "One Shot" ;
1676 break ;
1677 case basc_TYPE_LOOP :
1678 type_str = "Loop" ;
1679 break ;
1680 default:
1681 type_str = "!!WRONG!!" ;
1682 break ;
1683 } ;
1684
1685 psf_log_printf (psf, " Loop Type : 0x%x (%s)\n", bc.loopType, type_str) ;
1686
1687 if ((psf->loop_info = calloc (1, sizeof (SF_LOOP_INFO))) == NULL)
1688 return SFE_MALLOC_FAILED ;
1689
1690 psf->loop_info->time_sig_num = bc.sigNumerator ;
1691 psf->loop_info->time_sig_den = bc.sigDenominator ;
1692 psf->loop_info->loop_mode = (bc.loopType == basc_TYPE_ONE_SHOT) ? SF_LOOP_NONE : SF_LOOP_FORWARD ;
1693 psf->loop_info->num_beats = bc.numBeats ;
1694
1695 /* Can always be recalculated from other known fields. */
1696 psf->loop_info->bpm = (1.0 / psf->sf.frames) * psf->sf.samplerate
1697 * ((bc.numBeats * 4.0) / bc.sigDenominator) * 60.0 ;
1698 psf->loop_info->root_key = bc.rootNote ;
1699
1700 if (count < datasize)
1701 psf_binheader_readf (psf, "j", datasize - count) ;
1702
1703 return 0 ;
1704 } /* aiff_read_basc_chunk */
1705
1706
1707 static int
1708 aiff_read_chanmap (SF_PRIVATE * psf, unsigned dword)
1709 { const AIFF_CAF_CHANNEL_MAP * map_info ;
1710 unsigned channel_bitmap, channel_decriptions, bytesread ;
1711 int layout_tag ;
1712
1713 bytesread = psf_binheader_readf (psf, "444", &layout_tag, &channel_bitmap, &channel_decriptions) ;
1714
1715 if ((map_info = aiff_caf_of_channel_layout_tag (layout_tag)) == NULL)
1716 return 0 ;
1717
1718 psf_log_printf (psf, " Tag : %x\n", layout_tag) ;
1719 if (map_info)
1720 psf_log_printf (psf, " Layout : %s\n", map_info->name) ;
1721
1722 if (bytesread < dword)
1723 psf_binheader_readf (psf, "j", dword - bytesread) ;
1724
1725 if (map_info->channel_map != NULL)
1726 { size_t chanmap_size = psf->sf.channels * sizeof (psf->channel_map [0]) ;
1727
1728 free (psf->channel_map) ;
1729
1730 if ((psf->channel_map = malloc (chanmap_size)) == NULL)
1731 return SFE_MALLOC_FAILED ;
1732
1733 memcpy (psf->channel_map, map_info->channel_map, chanmap_size) ;
1734 } ;
1735
1736 return 0 ;
1737 } /* aiff_read_chanmap */
1738