Coverage Report

Created: 2026-03-21 06:29

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