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