Coverage Report

Created: 2025-09-05 06:58

/src/libsndfile/src/aiff.c
Line
Count
Source (jump to first uncovered line)
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
3.05k
#define FORM_MARKER   (MAKE_MARKER ('F', 'O', 'R', 'M'))
39
3.05k
#define AIFF_MARKER   (MAKE_MARKER ('A', 'I', 'F', 'F'))
40
3.93k
#define AIFC_MARKER   (MAKE_MARKER ('A', 'I', 'F', 'C'))
41
4.50k
#define COMM_MARKER   (MAKE_MARKER ('C', 'O', 'M', 'M'))
42
1.12k
#define SSND_MARKER   (MAKE_MARKER ('S', 'S', 'N', 'D'))
43
437
#define MARK_MARKER   (MAKE_MARKER ('M', 'A', 'R', 'K'))
44
782
#define INST_MARKER   (MAKE_MARKER ('I', 'N', 'S', 'T'))
45
654
#define APPL_MARKER   (MAKE_MARKER ('A', 'P', 'P', 'L'))
46
1.01k
#define CHAN_MARKER   (MAKE_MARKER ('C', 'H', 'A', 'N'))
47
48
623
#define c_MARKER    (MAKE_MARKER ('(', 'c', ')', ' '))
49
181
#define NAME_MARKER   (MAKE_MARKER ('N', 'A', 'M', 'E'))
50
914
#define AUTH_MARKER   (MAKE_MARKER ('A', 'U', 'T', 'H'))
51
153
#define ANNO_MARKER   (MAKE_MARKER ('A', 'N', 'N', 'O'))
52
380
#define COMT_MARKER   (MAKE_MARKER ('C', 'O', 'M', 'T'))
53
76
#define FVER_MARKER   (MAKE_MARKER ('F', 'V', 'E', 'R'))
54
153
#define SFX_MARKER    (MAKE_MARKER ('S', 'F', 'X', '!'))
55
56
221
#define PEAK_MARKER   (MAKE_MARKER ('P', 'E', 'A', 'K'))
57
331
#define basc_MARKER   (MAKE_MARKER ('b', 'a', 's', 'c'))
58
59
/* Supported AIFC encodings.*/
60
1.50k
#define NONE_MARKER   (MAKE_MARKER ('N', 'O', 'N', 'E'))
61
45
#define sowt_MARKER   (MAKE_MARKER ('s', 'o', 'w', 't'))
62
18
#define twos_MARKER   (MAKE_MARKER ('t', 'w', 'o', 's'))
63
240
#define raw_MARKER    (MAKE_MARKER ('r', 'a', 'w', ' '))
64
52
#define in24_MARKER   (MAKE_MARKER ('i', 'n', '2', '4'))
65
79
#define ni24_MARKER   (MAKE_MARKER ('4', '2', 'n', '1'))
66
118
#define in32_MARKER   (MAKE_MARKER ('i', 'n', '3', '2'))
67
97
#define ni32_MARKER   (MAKE_MARKER ('2', '3', 'n', 'i'))
68
69
8.82k
#define fl32_MARKER   (MAKE_MARKER ('f', 'l', '3', '2'))
70
4.82k
#define FL32_MARKER   (MAKE_MARKER ('F', 'L', '3', '2'))
71
8.40k
#define fl64_MARKER   (MAKE_MARKER ('f', 'l', '6', '4'))
72
4.23k
#define FL64_MARKER   (MAKE_MARKER ('F', 'L', '6', '4'))
73
74
44
#define ulaw_MARKER   (MAKE_MARKER ('u', 'l', 'a', 'w'))
75
78
#define ULAW_MARKER   (MAKE_MARKER ('U', 'L', 'A', 'W'))
76
443
#define alaw_MARKER   (MAKE_MARKER ('a', 'l', 'a', 'w'))
77
477
#define ALAW_MARKER   (MAKE_MARKER ('A', 'L', 'A', 'W'))
78
79
593
#define DWVW_MARKER   (MAKE_MARKER ('D', 'W', 'V', 'W'))
80
290
#define GSM_MARKER    (MAKE_MARKER ('G', 'S', 'M', ' '))
81
154
#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
4.50k
#define SIZEOF_AIFF_COMM    18
99
4.03k
#define SIZEOF_AIFC_COMM_MIN  22
100
5.36k
#define SIZEOF_AIFC_COMM    24
101
0
#define SIZEOF_SSND_CHUNK   8
102
1.02k
#define SIZEOF_INST_CHUNK   20
103
104
/* Is it constant? */
105
106
/* AIFC/IMA4 defines. */
107
126
#define AIFC_IMA4_BLOCK_LEN       34
108
126
#define AIFC_IMA4_SAMPLES_PER_BLOCK   64
109
110
220
#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
3.05k
{ COMM_CHUNK comm_fmt ;
237
3.05k
  int error = 0, subformat ;
238
239
3.05k
  memset (&comm_fmt, 0, sizeof (comm_fmt)) ;
240
241
3.05k
  subformat = SF_CODEC (psf->sf.format) ;
242
243
3.05k
  if ((psf->container_data = calloc (1, sizeof (AIFF_PRIVATE))) == NULL)
244
0
    return SFE_MALLOC_FAILED ;
245
246
3.05k
  psf->container_close = aiff_close ;
247
248
3.05k
  if (psf->file.mode == SFM_READ || (psf->file.mode == SFM_RDWR && psf->filelength > 0))
249
3.05k
  { if ((error = aiff_read_header (psf, &comm_fmt)))
250
2.14k
      return error ;
251
252
908
    psf->next_chunk_iterator = aiff_next_chunk_iterator ;
253
908
    psf->get_chunk_size = aiff_get_chunk_size ;
254
908
    psf->get_chunk_data = aiff_get_chunk_data ;
255
256
908
    psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
257
908
    } ;
258
259
908
  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
908
    } ;
287
288
908
  psf->command = aiff_command ;
289
290
908
  switch (SF_CODEC (psf->sf.format))
291
908
  { case SF_FORMAT_PCM_U8 :
292
41
        error = pcm_init (psf) ;
293
41
        break ;
294
295
1
    case SF_FORMAT_PCM_S8 :
296
1
        error = pcm_init (psf) ;
297
1
        break ;
298
299
9
    case SF_FORMAT_PCM_16 :
300
12
    case SF_FORMAT_PCM_24 :
301
15
    case SF_FORMAT_PCM_32 :
302
15
        error = pcm_init (psf) ;
303
15
        break ;
304
305
13
    case SF_FORMAT_ULAW :
306
13
        error = ulaw_init (psf) ;
307
13
        break ;
308
309
20
    case SF_FORMAT_ALAW :
310
20
        error = alaw_init (psf) ;
311
20
        break ;
312
313
    /* Lite remove start */
314
23
    case SF_FORMAT_FLOAT :
315
23
        error = float32_init (psf) ;
316
23
        break ;
317
318
30
    case SF_FORMAT_DOUBLE :
319
30
        error = double64_init (psf) ;
320
30
        break ;
321
322
7
    case SF_FORMAT_DWVW_12 :
323
7
        if (psf->sf.frames > comm_fmt.numSampleFrames)
324
0
          psf->sf.frames = comm_fmt.numSampleFrames ;
325
7
        break ;
326
327
69
    case SF_FORMAT_DWVW_16 :
328
69
        error = dwvw_init (psf, 16) ;
329
69
        if (psf->sf.frames > comm_fmt.numSampleFrames)
330
12
          psf->sf.frames = comm_fmt.numSampleFrames ;
331
69
        break ;
332
333
14
    case SF_FORMAT_DWVW_24 :
334
14
        error = dwvw_init (psf, 24) ;
335
14
        if (psf->sf.frames > comm_fmt.numSampleFrames)
336
5
          psf->sf.frames = comm_fmt.numSampleFrames ;
337
14
        break ;
338
339
225
    case SF_FORMAT_DWVW_N :
340
225
        if (psf->file.mode != SFM_READ)
341
0
        { error = SFE_DWVW_BAD_BITWIDTH ;
342
0
          break ;
343
225
          } ;
344
225
        if (comm_fmt.sampleSize >= 8 && comm_fmt.sampleSize < 24)
345
211
        { error = dwvw_init (psf, comm_fmt.sampleSize) ;
346
211
          if (psf->sf.frames > comm_fmt.numSampleFrames)
347
15
            psf->sf.frames = comm_fmt.numSampleFrames ;
348
211
          break ;
349
211
          } ;
350
14
        psf_log_printf (psf, "AIFC/DWVW : Bad bitwidth %d\n", comm_fmt.sampleSize) ;
351
14
        error = SFE_DWVW_BAD_BITWIDTH ;
352
14
        break ;
353
354
126
    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
126
        error = aiff_ima_init (psf, AIFC_IMA4_BLOCK_LEN, AIFC_IMA4_SAMPLES_PER_BLOCK) ;
360
126
        break ;
361
    /* Lite remove end */
362
363
220
    case SF_FORMAT_GSM610 :
364
220
        error = gsm610_init (psf) ;
365
220
        if (psf->sf.frames > comm_fmt.numSampleFrames)
366
24
          psf->sf.frames = comm_fmt.numSampleFrames ;
367
220
        break ;
368
369
104
    default : return SFE_UNIMPLEMENTED ;
370
908
    } ;
371
372
804
  if (psf->file.mode != SFM_WRITE && psf->sf.frames - comm_fmt.numSampleFrames != 0)
373
664
  { psf_log_printf (psf,
374
664
      "*** Frame count read from 'COMM' chunk (%u) not equal to frame count\n"
375
664
      "*** calculated from length of 'SSND' chunk (%u).\n",
376
664
      comm_fmt.numSampleFrames, (uint32_t) psf->sf.frames) ;
377
664
    } ;
378
379
804
  return error ;
380
908
} /* 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
118
{ int i ;
390
391
608k
  for (i = 0 ; i < marksize ; i++)
392
608k
    if (m [i].markerID == n)
393
54
      return m [i].position ;
394
64
  return 0 ;
395
118
} /* marker_to_position */
396
397
static int
398
aiff_read_header (SF_PRIVATE *psf, COMM_CHUNK *comm_fmt)
399
3.05k
{ SSND_CHUNK  ssnd_fmt ;
400
3.05k
  AIFF_PRIVATE *paiff ;
401
3.05k
  BUF_UNION ubuf ;
402
3.05k
  uint32_t  chunk_size = 0, FORMsize, SSNDsize, bytesread, mark_count = 0 ;
403
3.05k
  int     k, found_chunk = 0, done = 0, error = 0 ;
404
3.05k
  char    *cptr ;
405
3.05k
  int     instr_found = 0, mark_found = 0 ;
406
407
3.05k
  if (psf->filelength > 0xFFFFFFFFLL)
408
0
    psf_log_printf (psf, "Warning : filelength > 0xffffffff. This is bad!!!!\n") ;
409
410
3.05k
  if ((paiff = psf->container_data) == NULL)
411
0
    return SFE_INTERNAL ;
412
413
3.05k
  paiff->comm_offset = 0 ;
414
3.05k
  paiff->ssnd_offset = 0 ;
415
416
  /* Set position to start of file to begin reading header. */
417
3.05k
  psf_binheader_readf (psf, "p", 0) ;
418
419
3.05k
  memset (comm_fmt, 0, sizeof (COMM_CHUNK)) ;
420
421
  /* Until recently AIF* file were all BIG endian. */
422
3.05k
  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
18.5k
  while (! done)
429
18.5k
  { unsigned  marker ;
430
18.5k
    size_t jump = chunk_size & 1 ;
431
432
18.5k
    marker = chunk_size = 0 ;
433
18.5k
    psf_binheader_readf (psf, "Ejm4", jump, &marker, &chunk_size) ;
434
18.5k
    if (marker == 0)
435
7
    { sf_count_t pos = psf_ftell (psf) ;
436
7
      psf_log_printf (psf, "Have 0 marker at position %D (0x%x).\n", pos, pos) ;
437
7
      break ;
438
18.5k
      } ;
439
440
18.5k
    if (psf->file.mode == SFM_RDWR && (found_chunk & HAVE_SSND))
441
0
      return SFE_AIFF_RW_SSND_NOT_LAST ;
442
443
18.5k
    psf_store_read_chunk_u32 (&psf->rchunks, marker, psf_ftell (psf), chunk_size) ;
444
445
18.5k
    switch (marker)
446
18.5k
    { case FORM_MARKER :
447
3.05k
          if (found_chunk)
448
1
            return SFE_AIFF_NO_FORM ;
449
450
3.05k
          FORMsize = chunk_size ;
451
452
3.05k
          found_chunk |= HAVE_FORM ;
453
3.05k
          psf_binheader_readf (psf, "m", &marker) ;
454
3.05k
          switch (marker)
455
3.05k
          { case AIFC_MARKER :
456
3.05k
            case AIFF_MARKER :
457
3.05k
              found_chunk |= (marker == AIFC_MARKER) ? (HAVE_AIFC | HAVE_AIFF) : HAVE_AIFF ;
458
3.05k
              break ;
459
0
            default :
460
0
              break ;
461
3.05k
            } ;
462
463
3.05k
          if (psf->fileoffset > 0 && psf->filelength > FORMsize + 8)
464
49
          { /* Set file length. */
465
49
            psf->filelength = FORMsize + 8 ;
466
49
            psf_log_printf (psf, "FORM : %u\n %M\n", FORMsize, marker) ;
467
49
            }
468
3.00k
          else if (FORMsize != psf->filelength - 2 * SIGNED_SIZEOF (chunk_size))
469
3.00k
          { chunk_size = psf->filelength - 2 * sizeof (chunk_size) ;
470
3.00k
            psf_log_printf (psf, "FORM : %u (should be %u)\n %M\n", FORMsize, chunk_size, marker) ;
471
3.00k
            FORMsize = chunk_size ;
472
3.00k
            }
473
1
          else
474
1
            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
3.05k
          chunk_size = 0 ;
477
3.05k
          break ;
478
479
480
4.50k
      case COMM_MARKER :
481
4.50k
          paiff->comm_offset = psf_ftell (psf) - 8 ;
482
4.50k
          chunk_size += chunk_size & 1 ;
483
4.50k
          comm_fmt->size = chunk_size ;
484
4.50k
          if ((error = aiff_read_comm_chunk (psf, comm_fmt)) != 0)
485
512
            return error ;
486
487
3.99k
          found_chunk |= HAVE_COMM ;
488
3.99k
          break ;
489
490
221
      case PEAK_MARKER :
491
          /* Must have COMM chunk before PEAK chunk. */
492
221
          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
220
          psf_log_printf (psf, "%M : %d\n", marker, chunk_size) ;
496
220
          if (chunk_size != AIFF_PEAK_CHUNK_SIZE (psf->sf.channels))
497
9
          { psf_binheader_readf (psf, "j", chunk_size) ;
498
9
            psf_log_printf (psf, "*** File PEAK chunk too big.\n") ;
499
9
            return SFE_WAV_BAD_PEAK ;
500
211
            } ;
501
502
211
          if (psf->peak_info)
503
92
          { psf_log_printf (psf, "*** Found existing peak info, using last one.\n") ;
504
92
            free (psf->peak_info) ;
505
92
            psf->peak_info = NULL ;
506
92
            } ;
507
211
          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
211
          psf_binheader_readf (psf, "E44", &(psf->peak_info->version), &(psf->peak_info->timestamp)) ;
512
513
211
          if (psf->peak_info->version != 1)
514
170
            psf_log_printf (psf, "  version    : %d *** (should be version 1)\n", psf->peak_info->version) ;
515
41
          else
516
41
            psf_log_printf (psf, "  version    : %d\n", psf->peak_info->version) ;
517
518
211
          psf_log_printf (psf, "  time stamp : %d\n", psf->peak_info->timestamp) ;
519
211
          psf_log_printf (psf, "    Ch   Position       Value\n") ;
520
521
211
          cptr = ubuf.cbuf ;
522
3.06k
          for (k = 0 ; k < psf->sf.channels ; k++)
523
2.85k
          { float value ;
524
2.85k
            uint32_t position ;
525
526
2.85k
            psf_binheader_readf (psf, "Ef4", &value, &position) ;
527
2.85k
            psf->peak_info->peaks [k].value = value ;
528
2.85k
            psf->peak_info->peaks [k].position = position ;
529
530
2.85k
            snprintf (cptr, sizeof (ubuf.scbuf), "    %2d   %-12" PRId64 "   %g\n",
531
2.85k
                k, psf->peak_info->peaks [k].position, psf->peak_info->peaks [k].value) ;
532
2.85k
            cptr [sizeof (ubuf.scbuf) - 1] = 0 ;
533
2.85k
            psf_log_printf (psf, "%s", cptr) ;
534
2.85k
            } ;
535
536
211
          psf->peak_info->peak_loc = ((found_chunk & HAVE_SSND) == 0) ? SF_PEAK_START : SF_PEAK_END ;
537
211
          break ;
538
539
1.12k
      case SSND_MARKER :
540
1.12k
          if ((found_chunk & HAVE_AIFC) && (found_chunk & HAVE_FVER) == 0)
541
316
            psf_log_printf (psf, "*** Valid AIFC files should have an FVER chunk.\n") ;
542
543
1.12k
          paiff->ssnd_offset = psf_ftell (psf) - 8 ;
544
1.12k
          SSNDsize = chunk_size ;
545
1.12k
          psf_binheader_readf (psf, "E44", &(ssnd_fmt.offset), &(ssnd_fmt.blocksize)) ;
546
547
1.12k
          psf->datalength = SSNDsize - sizeof (ssnd_fmt) ;
548
1.12k
          psf->dataoffset = psf_ftell (psf) ;
549
550
1.12k
          if (psf->datalength > psf->filelength - psf->dataoffset || psf->datalength < 0)
551
327
          { psf_log_printf (psf, " SSND : %u (should be %D)\n", SSNDsize, psf->filelength - psf->dataoffset + sizeof (SSND_CHUNK)) ;
552
327
            psf->datalength = psf->filelength - psf->dataoffset ;
553
327
            }
554
800
          else
555
800
            psf_log_printf (psf, " SSND : %u\n", SSNDsize) ;
556
557
1.12k
          if (ssnd_fmt.offset == 0 || psf->dataoffset + ssnd_fmt.offset == ssnd_fmt.blocksize)
558
395
          { psf_log_printf (psf, "  Offset     : %u\n", ssnd_fmt.offset) ;
559
395
            psf_log_printf (psf, "  Block Size : %u\n", ssnd_fmt.blocksize) ;
560
561
395
            psf->dataoffset += ssnd_fmt.offset ;
562
395
            psf->datalength -= ssnd_fmt.offset ;
563
395
            }
564
732
          else
565
732
          { psf_log_printf (psf, "  Offset     : %u\n", ssnd_fmt.offset) ;
566
732
            psf_log_printf (psf, "  Block Size : %u ???\n", ssnd_fmt.blocksize) ;
567
732
            psf->dataoffset += ssnd_fmt.offset ;
568
732
            psf->datalength -= ssnd_fmt.offset ;
569
732
            } ;
570
571
          /* Only set dataend if there really is data at the end. */
572
1.12k
          if (psf->datalength + psf->dataoffset < psf->filelength)
573
792
            psf->dataend = psf->datalength + psf->dataoffset ;
574
575
1.12k
          found_chunk |= HAVE_SSND ;
576
577
1.12k
          if (! psf->sf.seekable)
578
0
            break ;
579
580
          /* Seek to end of SSND chunk. */
581
1.12k
          psf_fseek (psf, psf->dataoffset + psf->datalength, SEEK_SET) ;
582
1.12k
          break ;
583
584
623
      case c_MARKER :
585
623
          if (chunk_size == 0)
586
176
            break ;
587
447
          if (chunk_size >= SIGNED_SIZEOF (ubuf.scbuf))
588
40
          { psf_log_printf (psf, " %M : %d (too big)\n", marker, chunk_size) ;
589
40
            return SFE_INTERNAL ;
590
407
            } ;
591
592
407
          cptr = ubuf.cbuf ;
593
407
          psf_binheader_readf (psf, "b", cptr, chunk_size + (chunk_size & 1)) ;
594
407
          cptr [chunk_size] = 0 ;
595
596
407
          psf_sanitize_string (cptr, chunk_size) ;
597
598
407
          psf_log_printf (psf, " %M : %s\n", marker, cptr) ;
599
407
          psf_store_string (psf, SF_STR_COPYRIGHT, cptr) ;
600
407
          chunk_size += chunk_size & 1 ;
601
407
          break ;
602
603
914
      case AUTH_MARKER :
604
914
          if (chunk_size == 0)
605
809
            break ;
606
105
          if (chunk_size >= SIGNED_SIZEOF (ubuf.scbuf) - 1)
607
31
          { psf_log_printf (psf, " %M : %d (too big)\n", marker, chunk_size) ;
608
31
            return SFE_INTERNAL ;
609
74
            } ;
610
611
74
          cptr = ubuf.cbuf ;
612
74
          psf_binheader_readf (psf, "b", cptr, chunk_size + (chunk_size & 1)) ;
613
74
          cptr [chunk_size] = 0 ;
614
74
          psf_log_printf (psf, " %M : %s\n", marker, cptr) ;
615
74
          psf_store_string (psf, SF_STR_ARTIST, cptr) ;
616
74
          chunk_size += chunk_size & 1 ;
617
74
          break ;
618
619
380
      case COMT_MARKER :
620
380
        { uint16_t count, id, len ;
621
380
          uint32_t timestamp, bytes ;
622
623
380
          if (chunk_size == 0)
624
29
            break ;
625
351
          bytes = chunk_size ;
626
351
          bytes -= psf_binheader_readf (psf, "E2", &count) ;
627
351
          psf_log_printf (psf, " %M : %d\n  count  : %d\n", marker, chunk_size, count) ;
628
629
1.04M
          for (k = 0 ; k < count ; k++)
630
1.04M
          { bytes -= psf_binheader_readf (psf, "E422", &timestamp, &id, &len) ;
631
1.04M
            psf_log_printf (psf, "   time   : 0x%x\n   marker : %x\n   length : %d\n", timestamp, id, len) ;
632
633
1.04M
            if (len + 1 > SIGNED_SIZEOF (ubuf.scbuf))
634
13
            { psf_log_printf (psf, "\nError : string length (%d) too big.\n", len) ;
635
13
              return SFE_INTERNAL ;
636
1.04M
              } ;
637
638
1.04M
            cptr = ubuf.cbuf ;
639
1.04M
            bytes -= psf_binheader_readf (psf, "b", cptr, len) ;
640
1.04M
            cptr [len] = 0 ;
641
1.04M
            psf_log_printf (psf, "   string : %s\n", cptr) ;
642
1.04M
            } ;
643
644
338
          if (bytes > 0)
645
118
            psf_binheader_readf (psf, "j", bytes) ;
646
338
          } ;
647
338
          break ;
648
649
654
      case APPL_MARKER :
650
654
        { unsigned appl_marker ;
651
652
654
          if (chunk_size == 0)
653
35
            break ;
654
619
          if (chunk_size >= SIGNED_SIZEOF (ubuf.scbuf) - 1)
655
59
          { psf_log_printf (psf, " %M : %u (too big, skipping)\n", marker, chunk_size) ;
656
59
            psf_binheader_readf (psf, "j", chunk_size + (chunk_size & 1)) ;
657
59
            break ;
658
560
            } ;
659
660
560
          if (chunk_size < 4)
661
18
          { psf_log_printf (psf, " %M : %d (too small, skipping)\n", marker, chunk_size) ;
662
18
            psf_binheader_readf (psf, "j", chunk_size + (chunk_size & 1)) ;
663
18
            break ;
664
542
            } ;
665
666
542
          cptr = ubuf.cbuf ;
667
542
          psf_binheader_readf (psf, "mb", &appl_marker, cptr, chunk_size + (chunk_size & 1) - 4) ;
668
542
          cptr [chunk_size] = 0 ;
669
670
2.39k
          for (k = 0 ; k < (int) chunk_size ; k++)
671
2.00k
            if (! psf_isprint (cptr [k]))
672
148
            { cptr [k] = 0 ;
673
148
              break ;
674
148
              } ;
675
676
542
          psf_log_printf (psf, " %M : %d\n  AppSig : %M\n  Name   : %s\n", marker, chunk_size, appl_marker, cptr) ;
677
542
          psf_store_string (psf, SF_STR_SOFTWARE, cptr) ;
678
542
          chunk_size += chunk_size & 1 ;
679
542
          } ;
680
542
          break ;
681
682
181
      case NAME_MARKER :
683
181
          if (chunk_size == 0)
684
68
            break ;
685
113
          if (chunk_size >= SIGNED_SIZEOF (ubuf.scbuf) - 2)
686
29
          { psf_log_printf (psf, " %M : %d (too big)\n", marker, chunk_size) ;
687
29
            return SFE_INTERNAL ;
688
84
            } ;
689
690
84
          cptr = ubuf.cbuf ;
691
84
          psf_binheader_readf (psf, "b", cptr, chunk_size + (chunk_size & 1)) ;
692
84
          cptr [chunk_size] = 0 ;
693
84
          psf_log_printf (psf, " %M : %s\n", marker, cptr) ;
694
84
          psf_store_string (psf, SF_STR_TITLE, cptr) ;
695
84
          chunk_size += chunk_size & 1 ;
696
84
          break ;
697
698
153
      case ANNO_MARKER :
699
153
          if (chunk_size == 0)
700
80
            break ;
701
73
          if (chunk_size >= SIGNED_SIZEOF (ubuf.scbuf) - 2)
702
33
          { psf_log_printf (psf, " %M : %d (too big)\n", marker, chunk_size) ;
703
33
            return SFE_INTERNAL ;
704
40
            } ;
705
706
40
          cptr = ubuf.cbuf ;
707
40
          psf_binheader_readf (psf, "b", cptr, chunk_size + (chunk_size & 1)) ;
708
40
          cptr [chunk_size] = 0 ;
709
40
          psf_log_printf (psf, " %M : %s\n", marker, cptr) ;
710
40
          psf_store_string (psf, SF_STR_COMMENT, cptr) ;
711
40
          chunk_size += chunk_size & 1 ;
712
40
          break ;
713
714
782
      case INST_MARKER :
715
782
          if (chunk_size != SIZEOF_INST_CHUNK)
716
241
          { psf_log_printf (psf, " %M : %d (should be %d)\n", marker, chunk_size, SIZEOF_INST_CHUNK) ;
717
241
            psf_binheader_readf (psf, "j", chunk_size) ;
718
241
            break ;
719
541
            } ;
720
541
          psf_log_printf (psf, " %M : %d\n", marker, chunk_size) ;
721
541
          { uint8_t bytes [6] ;
722
541
            int16_t gain ;
723
724
541
            if (psf->instrument == NULL && (psf->instrument = psf_instrument_alloc ()) == NULL)
725
0
              return SFE_MALLOC_FAILED ;
726
727
541
            psf_binheader_readf (psf, "b", bytes, 6) ;
728
541
            psf_log_printf (psf, "  Base Note : %u\n  Detune    : %u\n"
729
541
                      "  Low  Note : %u\n  High Note : %u\n"
730
541
                      "  Low  Vel. : %u\n  High Vel. : %u\n",
731
541
                      bytes [0], bytes [1], bytes [2], bytes [3], bytes [4], bytes [5]) ;
732
541
            psf->instrument->basenote = bytes [0] ;
733
541
            psf->instrument->detune = bytes [1] ;
734
541
            psf->instrument->key_lo = bytes [2] ;
735
541
            psf->instrument->key_hi = bytes [3] ;
736
541
            psf->instrument->velocity_lo = bytes [4] ;
737
541
            psf->instrument->velocity_hi = bytes [5] ;
738
541
            psf_binheader_readf (psf, "E2", &gain) ;
739
541
            psf->instrument->gain = gain ;
740
541
            psf_log_printf (psf, "  Gain (dB) : %d\n", gain) ;
741
541
            } ;
742
541
          { int16_t mode ; /* 0 - no loop, 1 - forward looping, 2 - backward looping */
743
541
            const char  *loop_mode ;
744
541
            uint16_t begin, end ;
745
746
541
            psf_binheader_readf (psf, "E222", &mode, &begin, &end) ;
747
541
            loop_mode = get_loop_mode_str (mode) ;
748
541
            mode = get_loop_mode (mode) ;
749
541
            if (mode == SF_LOOP_NONE)
750
302
            { psf->instrument->loop_count = 0 ;
751
302
              psf->instrument->loops [0].mode = SF_LOOP_NONE ;
752
302
              }
753
239
            else
754
239
            { psf->instrument->loop_count = 1 ;
755
239
              psf->instrument->loops [0].mode = SF_LOOP_FORWARD ;
756
239
              psf->instrument->loops [0].start = begin ;
757
239
              psf->instrument->loops [0].end = end ;
758
239
              psf->instrument->loops [0].count = 0 ;
759
239
              } ;
760
541
            psf_log_printf (psf, "  Sustain\n   mode  : %d => %s\n   begin : %u\n   end   : %u\n",
761
541
                      mode, loop_mode, begin, end) ;
762
541
            psf_binheader_readf (psf, "E222", &mode, &begin, &end) ;
763
541
            loop_mode = get_loop_mode_str (mode) ;
764
541
            mode = get_loop_mode (mode) ;
765
541
            if (mode == SF_LOOP_NONE)
766
276
              psf->instrument->loops [1].mode = SF_LOOP_NONE ;
767
265
            else
768
265
            { psf->instrument->loop_count += 1 ;
769
265
              psf->instrument->loops [1].mode = SF_LOOP_FORWARD ;
770
265
              psf->instrument->loops [1].start = begin ;
771
265
              psf->instrument->loops [1].end = end ;
772
265
              psf->instrument->loops [1].count = 0 ;
773
265
              } ;
774
541
            psf_log_printf (psf, "  Release\n   mode  : %d => %s\n   begin : %u\n   end   : %u\n",
775
541
                    mode, loop_mode, begin, end) ;
776
541
            } ;
777
541
          instr_found++ ;
778
541
          break ;
779
780
331
      case basc_MARKER :
781
331
          psf_log_printf (psf, " basc : %u\n", chunk_size) ;
782
783
331
          if ((error = aiff_read_basc_chunk (psf, chunk_size)))
784
0
            return error ;
785
331
          break ;
786
787
437
      case MARK_MARKER :
788
437
          psf_log_printf (psf, " %M : %d\n", marker, chunk_size) ;
789
437
          { uint16_t mark_id, n = 0 ;
790
437
            uint32_t position ;
791
792
437
            bytesread = psf_binheader_readf (psf, "E2", &n) ;
793
437
            mark_count = n ;
794
437
            psf_log_printf (psf, "  Count : %u\n", mark_count) ;
795
437
            if (paiff->markstr != NULL)
796
291
            { psf_log_printf (psf, "*** Second MARK chunk found. Throwing away the first.\n") ;
797
291
              free (paiff->markstr) ;
798
291
              } ;
799
437
            paiff->markstr = calloc (mark_count, sizeof (MARK_ID_POS)) ;
800
437
            if (paiff->markstr == NULL)
801
0
              return SFE_MALLOC_FAILED ;
802
803
437
            if (mark_count > 2500) /* 2500 is close to the largest number of cues possible because of block sizes */
804
164
            { psf_log_printf (psf, "  More than 2500 markers, skipping!\n") ;
805
164
              psf_binheader_readf (psf, "j", chunk_size - bytesread) ;
806
164
              break ;
807
273
            } ;
808
809
273
            if (psf->cues)
810
156
            { free (psf->cues) ;
811
156
              psf->cues = NULL ;
812
156
              } ;
813
273
            if ((psf->cues = psf_cues_alloc (mark_count)) == NULL)
814
0
              return SFE_MALLOC_FAILED ;
815
816
33.0k
            for (n = 0 ; n < mark_count && bytesread < chunk_size ; n++)
817
32.8k
            { uint32_t pstr_len ;
818
32.8k
              uint8_t ch ;
819
820
32.8k
              bytesread += psf_binheader_readf (psf, "E241", &mark_id, &position, &ch) ;
821
32.8k
              psf_log_printf (psf, "   Mark ID  : %u\n   Position : %u\n", mark_id, position) ;
822
823
32.8k
              psf->cues->cue_points [n].indx = mark_id ;
824
32.8k
              psf->cues->cue_points [n].position = 0 ;
825
32.8k
              psf->cues->cue_points [n].fcc_chunk = MAKE_MARKER ('d', 'a', 't', 'a') ; /* always data */
826
32.8k
              psf->cues->cue_points [n].chunk_start = 0 ;
827
32.8k
              psf->cues->cue_points [n].block_start = 0 ;
828
32.8k
              psf->cues->cue_points [n].sample_offset = position ;
829
830
32.8k
              pstr_len = (ch & 1) ? ch : ch + 1 ;
831
832
32.8k
              if (pstr_len < sizeof (ubuf.scbuf) - 1)
833
32.8k
              { bytesread += psf_binheader_readf (psf, "b", ubuf.scbuf, pstr_len) ;
834
32.8k
                ubuf.scbuf [pstr_len] = 0 ;
835
32.8k
                }
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
32.8k
              psf_log_printf (psf, "   Name     : %s\n", ubuf.scbuf) ;
843
844
32.8k
              psf_strlcpy (psf->cues->cue_points [n].name, sizeof (psf->cues->cue_points [n].name), ubuf.cbuf) ;
845
846
32.8k
              paiff->markstr [n].markerID = mark_id ;
847
32.8k
              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
32.8k
              } ;
855
273
            } ;
856
273
          mark_found++ ;
857
273
          psf_binheader_readf (psf, "j", chunk_size - bytesread) ;
858
273
          break ;
859
860
76
      case FVER_MARKER :
861
76
          found_chunk |= HAVE_FVER ;
862
          /* Falls through. */
863
864
153
      case SFX_MARKER :
865
153
          psf_log_printf (psf, " %M : %d\n", marker, chunk_size) ;
866
153
          psf_binheader_readf (psf, "j", chunk_size) ;
867
153
          break ;
868
869
197
      case NONE_MARKER :
870
          /* Fix for broken AIFC files with incorrect COMM chunk length. */
871
197
          chunk_size = (chunk_size >> 24) - 3 ;
872
197
          psf_log_printf (psf, " %M : %d\n", marker, chunk_size) ;
873
197
          psf_binheader_readf (psf, "j", make_size_t (chunk_size)) ;
874
197
          break ;
875
876
1.01k
      case CHAN_MARKER :
877
1.01k
          if (chunk_size < 12)
878
20
          { psf_log_printf (psf, " %M : %d (should be >= 12)\n", marker, chunk_size) ;
879
20
            psf_binheader_readf (psf, "j", chunk_size) ;
880
20
            break ;
881
20
            }
882
883
996
          psf_log_printf (psf, " %M : %d\n", marker, chunk_size) ;
884
885
996
          if ((error = aiff_read_chanmap (psf, chunk_size)))
886
0
            return error ;
887
996
          break ;
888
889
3.85k
      default :
890
3.85k
          if (chunk_size >= 0xffff0000)
891
56
          { done = SF_TRUE ;
892
56
            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
56
            break ;
894
3.80k
            } ;
895
896
3.80k
          if (psf_isprint ((marker >> 24) & 0xFF) && psf_isprint ((marker >> 16) & 0xFF)
897
3.80k
            && psf_isprint ((marker >> 8) & 0xFF) && psf_isprint (marker & 0xFF))
898
1.02k
          { psf_log_printf (psf, " %M : %u (unknown marker)\n", marker, chunk_size) ;
899
900
1.02k
            psf_binheader_readf (psf, "j", chunk_size) ;
901
1.02k
            break ;
902
2.77k
            } ;
903
904
2.77k
          if (psf_ftell (psf) & 0x03)
905
2.42k
          { psf_log_printf (psf, "  Unknown chunk marker at position %D. Resynching.\n", psf_ftell (psf) - 8) ;
906
2.42k
            psf_binheader_readf (psf, "j", -3) ;
907
2.42k
            break ;
908
2.42k
            } ;
909
356
          psf_log_printf (psf, "*** Unknown chunk marker %X at position %D. Exiting parser.\n", marker, psf_ftell (psf)) ;
910
356
          done = SF_TRUE ;
911
356
          break ;
912
18.5k
      } ; /* switch (marker) */
913
914
17.9k
    if (chunk_size >= psf->filelength)
915
1.77k
    { psf_log_printf (psf, "*** Chunk size %u > file length %D. Exiting parser.\n", chunk_size, psf->filelength) ;
916
1.77k
      break ;
917
16.1k
      } ;
918
919
16.1k
    if ((! psf->sf.seekable) && (found_chunk & HAVE_SSND))
920
0
      break ;
921
922
16.1k
    if (psf_ftell (psf) >= psf->filelength - (2 * SIGNED_SIZEOF (int32_t)))
923
592
      break ;
924
16.1k
    } ; /* while (1) */
925
926
2.38k
  if (instr_found && mark_found)
927
69
  { int ji, str_index ;
928
    /* Next loop will convert markers to loop positions for internal handling */
929
128
    for (ji = 0 ; ji < psf->instrument->loop_count ; ji ++)
930
59
    { if (ji < ARRAY_LEN (psf->instrument->loops))
931
59
      { psf->instrument->loops [ji].start = marker_to_position (paiff->markstr, psf->instrument->loops [ji].start, mark_count) ;
932
59
        psf->instrument->loops [ji].end = marker_to_position (paiff->markstr, psf->instrument->loops [ji].end, mark_count) ;
933
59
        psf->instrument->loops [ji].mode = SF_LOOP_FORWARD ;
934
59
        } ;
935
59
      } ;
936
937
    /* The markers that correspond to loop positions can now be removed from cues struct */
938
69
    if (psf->cues->cue_count > (uint32_t) (psf->instrument->loop_count * 2))
939
64
    { uint32_t j ;
940
941
45.8k
      for (j = 0 ; j < psf->cues->cue_count - (uint32_t) (psf->instrument->loop_count * 2) ; j ++)
942
45.8k
      { /* This simply copies the information in cues above loop positions and writes it at current count instead */
943
45.8k
        psf->cues->cue_points [j].indx = psf->cues->cue_points [j + psf->instrument->loop_count * 2].indx ;
944
45.8k
        psf->cues->cue_points [j].position = psf->cues->cue_points [j + psf->instrument->loop_count * 2].position ;
945
45.8k
        psf->cues->cue_points [j].fcc_chunk = psf->cues->cue_points [j + psf->instrument->loop_count * 2].fcc_chunk ;
946
45.8k
        psf->cues->cue_points [j].chunk_start = psf->cues->cue_points [j + psf->instrument->loop_count * 2].chunk_start ;
947
45.8k
        psf->cues->cue_points [j].block_start = psf->cues->cue_points [j + psf->instrument->loop_count * 2].block_start ;
948
45.8k
        psf->cues->cue_points [j].sample_offset = psf->cues->cue_points [j + psf->instrument->loop_count * 2].sample_offset ;
949
11.7M
        for (str_index = 0 ; str_index < 256 ; str_index++)
950
11.7M
          psf->cues->cue_points [j].name [str_index] = psf->cues->cue_points [j + psf->instrument->loop_count * 2].name [str_index] ;
951
45.8k
        } ;
952
64
      psf->cues->cue_count -= psf->instrument->loop_count * 2 ;
953
64
      } else
954
5
      { /* All the cues were in fact loop positions so we can actually remove the cues altogether */
955
5
        free (psf->cues) ;
956
5
        psf->cues = NULL ;
957
5
        }
958
69
    } ;
959
960
2.38k
  if (psf->sf.channels < 1)
961
1.47k
    return SFE_CHANNEL_COUNT_ZERO ;
962
963
908
  if (psf->sf.channels > SF_MAX_CHANNELS)
964
0
    return SFE_CHANNEL_COUNT ;
965
966
908
  if (! (found_chunk & HAVE_FORM))
967
0
    return SFE_AIFF_NO_FORM ;
968
969
908
  if (! (found_chunk & HAVE_AIFF))
970
0
    return SFE_AIFF_COMM_NO_FORM ;
971
972
908
  if (! (found_chunk & HAVE_COMM))
973
0
    return SFE_AIFF_SSND_NO_COMM ;
974
975
908
  if (! psf->dataoffset)
976
0
    return SFE_AIFF_NO_DATA ;
977
978
908
  return 0 ;
979
908
} /* aiff_read_header */
980
981
static int
982
aiff_close (SF_PRIVATE *psf)
983
3.05k
{ AIFF_PRIVATE *paiff = psf->container_data ;
984
985
3.05k
  if (paiff != NULL && paiff->markstr != NULL)
986
146
  { free (paiff->markstr) ;
987
146
    paiff->markstr = NULL ;
988
146
    } ;
989
990
3.05k
  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
3.05k
  return 0 ;
996
3.05k
} /* aiff_close */
997
998
static int
999
aiff_read_comm_chunk (SF_PRIVATE *psf, COMM_CHUNK *comm_fmt)
1000
4.50k
{ BUF_UNION ubuf ;
1001
4.50k
  int subformat, samplerate ;
1002
1003
4.50k
  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
4.50k
  psf_binheader_readf (psf, "E242b", &(comm_fmt->numChannels), &(comm_fmt->numSampleFrames),
1011
4.50k
        &(comm_fmt->sampleSize), &(comm_fmt->sampleRate), SIGNED_SIZEOF (comm_fmt->sampleRate)) ;
1012
1013
4.50k
  if (comm_fmt->size > 0x10000 && (comm_fmt->size & 0xffff) == 0)
1014
180
  { psf_log_printf (psf, " COMM : %d (0x%x) *** should be ", comm_fmt->size, comm_fmt->size) ;
1015
180
    comm_fmt->size = ENDSWAP_32 (comm_fmt->size) ;
1016
180
    psf_log_printf (psf, "%d (0x%x)\n", comm_fmt->size, comm_fmt->size) ;
1017
180
    }
1018
4.32k
  else
1019
4.32k
    psf_log_printf (psf, " COMM : %d\n", comm_fmt->size) ;
1020
1021
4.50k
  if (comm_fmt->size == SIZEOF_AIFF_COMM)
1022
475
    comm_fmt->encoding = NONE_MARKER ;
1023
4.03k
  else if (comm_fmt->size == SIZEOF_AIFC_COMM_MIN)
1024
215
    psf_binheader_readf (psf, "Em", &(comm_fmt->encoding)) ;
1025
3.81k
  else if (comm_fmt->size >= SIZEOF_AIFC_COMM)
1026
1.54k
  { uint8_t encoding_len ;
1027
1.54k
    unsigned read_len ;
1028
1029
1.54k
    psf_binheader_readf (psf, "Em1", &(comm_fmt->encoding), &encoding_len) ;
1030
1031
1.54k
    comm_fmt->size = SF_MIN (sizeof (ubuf.scbuf), make_size_t (comm_fmt->size)) ;
1032
1.54k
    memset (ubuf.scbuf, 0, comm_fmt->size) ;
1033
1.54k
    read_len = comm_fmt->size - SIZEOF_AIFC_COMM + 1 ;
1034
1.54k
    psf_binheader_readf (psf, "b", ubuf.scbuf, read_len) ;
1035
1.54k
    ubuf.scbuf [read_len + 1] = 0 ;
1036
1.54k
    } ;
1037
1038
4.50k
  samplerate = tenbytefloat2int (comm_fmt->sampleRate) ;
1039
1040
4.50k
  psf_log_printf (psf, "  Sample Rate : %d\n", samplerate) ;
1041
4.50k
  psf_log_printf (psf, "  Frames      : %u%s\n", comm_fmt->numSampleFrames, (comm_fmt->numSampleFrames == 0 && psf->filelength > 104) ? " (Should not be 0)" : "") ;
1042
1043
4.50k
  if (comm_fmt->numChannels < 1 || comm_fmt->numChannels > SF_MAX_CHANNELS)
1044
129
  { psf_log_printf (psf, "  Channels    : %d (should be >= 1 and < %d)\n", comm_fmt->numChannels, SF_MAX_CHANNELS) ;
1045
129
    return SFE_CHANNEL_COUNT_BAD ;
1046
4.37k
    } ;
1047
1048
4.37k
  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
4.37k
  if ((comm_fmt->encoding == fl32_MARKER || comm_fmt->encoding == FL32_MARKER) && comm_fmt->sampleSize != 32)
1052
363
  { psf_log_printf (psf, "  Sample Size : %d (should be 32)\n", comm_fmt->sampleSize) ;
1053
363
    comm_fmt->sampleSize = 32 ;
1054
363
    }
1055
4.01k
  else if ((comm_fmt->encoding == fl64_MARKER || comm_fmt->encoding == FL64_MARKER) && comm_fmt->sampleSize != 64)
1056
415
  { psf_log_printf (psf, "  Sample Size : %d (should be 64)\n", comm_fmt->sampleSize) ;
1057
415
    comm_fmt->sampleSize = 64 ;
1058
415
    }
1059
3.59k
  else
1060
3.59k
    psf_log_printf (psf, "  Sample Size : %d\n", comm_fmt->sampleSize) ;
1061
1062
1063
4.37k
  if ((psf->sf.channels != comm_fmt->numChannels) && psf->peak_info)
1064
79
  { psf_log_printf (psf, "  *** channel count changed, discarding existing PEAK chunk\n") ;
1065
79
    free (psf->peak_info) ;
1066
79
    psf->peak_info = NULL ;
1067
79
    } ;
1068
1069
4.37k
  subformat = s_bitwidth_to_subformat (comm_fmt->sampleSize) ;
1070
1071
4.37k
  psf->sf.samplerate = samplerate ;
1072
4.37k
  psf->sf.frames = comm_fmt->numSampleFrames ;
1073
4.37k
  psf->sf.channels = comm_fmt->numChannels ;
1074
4.37k
  psf->bytewidth = BITWIDTH2BYTES (comm_fmt->sampleSize) ;
1075
1076
4.37k
  psf->endian = SF_ENDIAN_BIG ;
1077
1078
4.37k
  switch (comm_fmt->encoding)
1079
4.37k
  { case NONE_MARKER :
1080
830
        psf->sf.format = (SF_FORMAT_AIFF | subformat) ;
1081
830
        break ;
1082
1083
18
    case twos_MARKER :
1084
52
    case in24_MARKER :
1085
118
    case in32_MARKER :
1086
118
        psf->sf.format = (SF_ENDIAN_BIG | SF_FORMAT_AIFF | subformat) ;
1087
118
        break ;
1088
1089
45
    case sowt_MARKER :
1090
79
    case ni24_MARKER :
1091
97
    case ni32_MARKER :
1092
97
        psf->endian = SF_ENDIAN_LITTLE ;
1093
97
        psf->sf.format = (SF_ENDIAN_LITTLE | SF_FORMAT_AIFF | subformat) ;
1094
97
        break ;
1095
1096
68
    case fl32_MARKER :
1097
520
    case FL32_MARKER :
1098
520
        psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_FLOAT) ;
1099
520
        break ;
1100
1101
44
    case ulaw_MARKER :
1102
78
    case ULAW_MARKER :
1103
78
        psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_ULAW) ;
1104
78
        break ;
1105
1106
443
    case alaw_MARKER :
1107
477
    case ALAW_MARKER :
1108
477
        psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_ALAW) ;
1109
477
        break ;
1110
1111
378
    case fl64_MARKER :
1112
596
    case FL64_MARKER :
1113
596
        psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_DOUBLE) ;
1114
596
        break ;
1115
1116
240
    case raw_MARKER :
1117
240
        psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_PCM_U8) ;
1118
240
        break ;
1119
1120
593
    case DWVW_MARKER :
1121
593
        psf->sf.format = SF_FORMAT_AIFF ;
1122
593
        switch (comm_fmt->sampleSize)
1123
593
        { case 12 :
1124
13
            psf->sf.format |= SF_FORMAT_DWVW_12 ;
1125
13
            break ;
1126
170
          case 16 :
1127
170
            psf->sf.format |= SF_FORMAT_DWVW_16 ;
1128
170
            break ;
1129
27
          case 24 :
1130
27
            psf->sf.format |= SF_FORMAT_DWVW_24 ;
1131
27
            break ;
1132
1133
383
          default :
1134
383
            psf->sf.format |= SF_FORMAT_DWVW_N ;
1135
383
            break ;
1136
593
          } ;
1137
593
        break ;
1138
1139
290
    case GSM_MARKER :
1140
290
        psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_GSM610) ;
1141
290
        break ;
1142
1143
1144
154
    case ima4_MARKER :
1145
154
        psf->endian = SF_ENDIAN_BIG ;
1146
154
        psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_IMA_ADPCM) ;
1147
154
        break ;
1148
1149
383
    default :
1150
383
      psf_log_printf (psf, "AIFC : Unimplemented format : %M\n", comm_fmt->encoding) ;
1151
383
      return SFE_UNIMPLEMENTED ;
1152
4.37k
    } ;
1153
1154
3.99k
  if (! ubuf.scbuf [0])
1155
3.38k
    psf_log_printf (psf, "  Encoding    : %M\n", comm_fmt->encoding) ;
1156
607
  else
1157
607
    psf_log_printf (psf, "  Encoding    : %M => %s\n", comm_fmt->encoding, ubuf.scbuf) ;
1158
1159
3.99k
  return 0 ;
1160
4.37k
} /* 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
1.08k
{ switch (mode)
1614
1.08k
  { case 0 : return "none" ;
1615
83
    case 1 : return "forward" ;
1616
421
    case 2 : return "backward" ;
1617
1.08k
    } ;
1618
1619
360
  return "*** unknown" ;
1620
1.08k
} /* get_loop_mode_str */
1621
1622
static int16_t
1623
get_loop_mode (int16_t mode)
1624
1.08k
{ switch (mode)
1625
1.08k
  { case 0 : return SF_LOOP_NONE ;
1626
83
    case 1 : return SF_LOOP_FORWARD ;
1627
421
    case 2 : return SF_LOOP_BACKWARD ;
1628
1.08k
    } ;
1629
1630
360
  return SF_LOOP_NONE ;
1631
1.08k
} /* 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
4.50k
{ int val = 3 ;
1645
1646
4.50k
  if (bytes [0] & 0x80)  /* Negative number. */
1647
603
    return 0 ;
1648
1649
3.90k
  if (bytes [0] <= 0x3F)  /* Less than 1. */
1650
2.63k
    return 1 ;
1651
1652
1.26k
  if (bytes [0] > 0x40)  /* Way too big. */
1653
882
    return 0x4000000 ;
1654
1655
386
  if (bytes [0] == 0x40 && bytes [1] > 0x1C) /* Too big. */
1656
200
    return 800000000 ;
1657
1658
  /* Ok, can handle it. */
1659
1660
186
  val = (bytes [2] << 23) | (bytes [3] << 15) | (bytes [4] << 7) | (bytes [5] >> 1) ;
1661
1662
186
  val >>= (29 - bytes [1]) ;
1663
1664
186
  return val ;
1665
386
} /* 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
331
{ const char * type_str ;
1704
331
  basc_CHUNK bc ;
1705
331
  sf_count_t count ;
1706
1707
331
  count = psf_binheader_readf (psf, "E442", &bc.version, &bc.numBeats, &bc.rootNote) ;
1708
331
  count += psf_binheader_readf (psf, "E222", &bc.scaleType, &bc.sigNumerator, &bc.sigDenominator) ;
1709
331
  count += psf_binheader_readf (psf, "E2j", &bc.loopType, datasize - sizeof (bc)) ;
1710
1711
331
  psf_log_printf (psf, "  Version ? : %u\n  Num Beats : %u\n  Root Note : 0x%x\n",
1712
331
            bc.version, bc.numBeats, bc.rootNote) ;
1713
1714
331
  switch (bc.scaleType)
1715
331
  { case basc_SCALE_MINOR :
1716
9
        type_str = "MINOR" ;
1717
9
        break ;
1718
1
    case basc_SCALE_MAJOR :
1719
1
        type_str = "MAJOR" ;
1720
1
        break ;
1721
82
    case basc_SCALE_NEITHER :
1722
82
        type_str = "NEITHER" ;
1723
82
        break ;
1724
113
    case basc_SCALE_BOTH :
1725
113
        type_str = "BOTH" ;
1726
113
        break ;
1727
126
    default :
1728
126
        type_str = "!!WRONG!!" ;
1729
126
        break ;
1730
331
    } ;
1731
1732
331
  psf_log_printf (psf, "  ScaleType : 0x%x (%s)\n", bc.scaleType, type_str) ;
1733
331
  psf_log_printf (psf, "  Time Sig  : %d/%d\n", bc.sigNumerator, bc.sigDenominator) ;
1734
1735
331
  switch (bc.loopType)
1736
331
  { case basc_TYPE_ONE_SHOT :
1737
2
        type_str = "One Shot" ;
1738
2
        break ;
1739
46
    case basc_TYPE_LOOP :
1740
46
        type_str = "Loop" ;
1741
46
        break ;
1742
283
    default:
1743
283
        type_str = "!!WRONG!!" ;
1744
283
        break ;
1745
331
    } ;
1746
1747
331
  psf_log_printf (psf, "  Loop Type : 0x%x (%s)\n", bc.loopType, type_str) ;
1748
1749
331
  if (psf->loop_info)
1750
279
  { psf_log_printf (psf, "  Found existing loop info, using last one.\n") ;
1751
279
    free (psf->loop_info) ;
1752
279
    psf->loop_info = NULL ;
1753
279
    } ;
1754
331
  if ((psf->loop_info = calloc (1, sizeof (SF_LOOP_INFO))) == NULL)
1755
0
    return SFE_MALLOC_FAILED ;
1756
1757
331
  psf->loop_info->time_sig_num  = bc.sigNumerator ;
1758
331
  psf->loop_info->time_sig_den  = bc.sigDenominator ;
1759
331
  psf->loop_info->loop_mode   = (bc.loopType == basc_TYPE_ONE_SHOT) ? SF_LOOP_NONE : SF_LOOP_FORWARD ;
1760
331
  psf->loop_info->num_beats   = bc.numBeats ;
1761
1762
  /* Can always be recalculated from other known fields. */
1763
331
  psf->loop_info->bpm = (1.0 / psf->sf.frames) * psf->sf.samplerate
1764
331
              * ((bc.numBeats * 4.0) / bc.sigDenominator) * 60.0 ;
1765
331
  psf->loop_info->root_key = bc.rootNote ;
1766
1767
331
  if (count < datasize)
1768
322
    psf_binheader_readf (psf, "j", datasize - count) ;
1769
1770
331
  return 0 ;
1771
331
} /* aiff_read_basc_chunk */
1772
1773
1774
static int
1775
aiff_read_chanmap (SF_PRIVATE * psf, unsigned dword)
1776
996
{ const AIFF_CAF_CHANNEL_MAP * map_info ;
1777
996
  unsigned channel_bitmap, channel_decriptions, bytesread ;
1778
996
  int layout_tag ;
1779
1780
996
  bytesread = psf_binheader_readf (psf, "444", &layout_tag, &channel_bitmap, &channel_decriptions) ;
1781
1782
996
  if ((map_info = aiff_caf_of_channel_layout_tag (layout_tag)) == NULL)
1783
438
    return 0 ;
1784
1785
558
  psf_log_printf (psf, "  Tag    : %x\n", layout_tag) ;
1786
558
  if (map_info)
1787
558
    psf_log_printf (psf, "  Layout : %s\n", map_info->name) ;
1788
1789
558
  if (bytesread < dword)
1790
158
    psf_binheader_readf (psf, "j", dword - bytesread) ;
1791
1792
558
  if (map_info->channel_map != NULL)
1793
108
  { size_t chanmap_size = SF_MIN (psf->sf.channels, layout_tag & 0xffff) * sizeof (psf->channel_map [0]) ;
1794
1795
108
    free (psf->channel_map) ;
1796
1797
108
    if ((psf->channel_map = malloc (chanmap_size)) == NULL)
1798
0
      return SFE_MALLOC_FAILED ;
1799
1800
108
    memcpy (psf->channel_map, map_info->channel_map, chanmap_size) ;
1801
558
    } ;
1802
1803
558
  return 0 ;
1804
558
} /* 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 */