Bug Summary

File:libs/libsndfile/src/aiff.c
Location:line 457, column 7
Description:Value stored to 'FORMsize' is never read

Annotated Source Code

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