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