forked from mtszb/libaiff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aifx.c
320 lines (280 loc) · 7.34 KB
/
aifx.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/* $Id: aifx.c,v 1.26 2008/08/23 15:48:18 toad32767 Exp $ */
/*-
* Copyright (c) 2005, 2006 Marco Trillo
*
* Permission is hereby granted, free of charge, to any
* person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the
* Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice
* shall be included in all copies or substantial portions of
* the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
* KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
* PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
* OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "private.h"
int
init_aifx(AIFF_Ref r)
{
int bps, wSegmentSize;
double sRate;
uint8_t buffer[10];
uint32_t len;
CommonChunk p;
IFFType aFmt;
if (!find_iff_chunk(AIFF_COMM, r, &len))
return (-1);
if (len < 18)
return (-1);
if (fread(&(p.numChannels), 1, 2, r->fd) < 2 ||
fread(&(p.numSampleFrames), 1, 4, r->fd) < 4 ||
fread(&(p.sampleSize), 1, 2, r->fd) < 2 ||
fread(buffer, 1, 10, r->fd) < 10)
return (-1);
p.numChannels = ARRANGE_BE16(p.numChannels);
p.numSampleFrames = ARRANGE_BE32(p.numSampleFrames);
p.sampleSize = ARRANGE_BE16(p.sampleSize);
sRate = ieee754_read_extended(buffer);
r->nSamples = p.numSampleFrames;
r->nChannels = p.numChannels;
r->samplingRate = sRate;
r->bitsPerSample = p.sampleSize;
bps = p.sampleSize;
wSegmentSize = (bps + 7) >> 3;
r->segmentSize = wSegmentSize;
if (len >= 22 && r->format == AIFF_TYPE_AIFC) {
if (fread(&aFmt, 1, 4, r->fd) < 4)
return (-1);
switch (aFmt) {
case AUDIO_FORMAT_LPCM: /* 'NONE' */
case AUDIO_FORMAT_lpcm: /* 'lpcm' (not standard) */
case AUDIO_FORMAT_twos: /* 'twos' */
r->audioFormat = AUDIO_FORMAT_LPCM;
r->flags |= LPCM_BIG_ENDIAN;
break;
case AUDIO_FORMAT_ULAW: /* 'ULAW' */
case AUDIO_FORMAT_ulaw: /* 'ulaw' */
r->audioFormat = AUDIO_FORMAT_ULAW;
r->segmentSize = 2;
r->bitsPerSample = 14;
break;
case AUDIO_FORMAT_ALAW: /* 'ALAW' */
case AUDIO_FORMAT_alaw: /* 'alaw' */
r->audioFormat = AUDIO_FORMAT_ALAW;
r->segmentSize = 2;
r->bitsPerSample = 13;
break;
case AUDIO_FORMAT_sowt: /* 'sowt' */
r->audioFormat = AUDIO_FORMAT_LPCM;
r->flags |= LPCM_LTE_ENDIAN;
break;
case AUDIO_FORMAT_FL32: /* 'FL32' */
case AUDIO_FORMAT_fl32: /* 'fl32' */
r->audioFormat = AUDIO_FORMAT_FL32;
r->segmentSize = 4;
r->bitsPerSample = 32;
r->flags |= LPCM_BIG_ENDIAN;
break;
default:
r->audioFormat = AUDIO_FORMAT_UNKNOWN;
}
/*
* Read the description string if
* the F_NOTSEEKABLE flag is set
*/
if (len > 22 && (r->flags & F_NOTSEEKABLE)) {
int count = PASCALInGetLength(r->fd);
while (count-- > 0) {
if (getc(r->fd) < 0)
return (-1);
}
}
} else {
r->audioFormat = AUDIO_FORMAT_LPCM;
r->flags |= LPCM_BIG_ENDIAN;
}
return (1);
}
int
read_aifx_marker(AIFF_Ref r, int *id, uint64_t * position, char **name)
{
uint16_t nMarkers;
uint32_t cklen;
int n;
Marker m;
if (r->stat != 2) {
if (!find_iff_chunk(AIFF_MARK, r, &cklen))
return (0);
if (cklen < 2)
return (-1);
if (fread(&nMarkers, 1, 2, r->fd) < 2)
return (-1);
nMarkers = ARRANGE_BE16(nMarkers);
r->nMarkers = nMarkers;
r->markerPos = 0;
r->stat = 2;
}
n = r->nMarkers;
if (r->markerPos >= n) {
r->stat = 0;
return (0);
}
if (fread(&(m.id), 1, 2, r->fd) < 2 ||
fread(&(m.position), 1, 4, r->fd) < 4)
return (-1);
m.id = ARRANGE_BE16(m.id);
m.position = ARRANGE_BE32(m.position);
if (name != NULL) {
int l;
*name = PASCALInRead(r->fd, &l);
} else {
int l = PASCALInGetLength(r->fd);
if (!(r->flags & F_NOTSEEKABLE)) {
if (fseek(r->fd, (long) l, SEEK_CUR) < 0)
return (-1);
} else {
while (l-- > 0) {
if (getc(r->fd) < 0)
return (-1);
}
}
}
*id = m.id;
*position = m.position;
r->markerPos++;
return (1);
}
int
get_aifx_instrument(AIFF_Ref r, Instrument * inpi)
{
int i;
uint32_t cklen;
int8_t buffer[6];
int16_t gain;
AIFFLoop sustainLoop, releaseLoop;
int ids[4];
int id;
uint32_t pos;
uint32_t positions[4];
char *name;
if (!find_iff_chunk(AIFF_INST, r, &cklen))
return (0);
if (cklen != 20)
return (0);
if (fread(buffer, 1, 6, r->fd) < 6)
return (0);
if (fread(&gain, 1, 2, r->fd) < 2)
return (0);
if (fread(&sustainLoop, 1, 6, r->fd) < 6)
return (0);
if (fread(&releaseLoop, 1, 6, r->fd) < 6)
return (0);
inpi->baseNote = buffer[0];
inpi->detune = buffer[1];
inpi->lowNote = buffer[2];
inpi->highNote = buffer[3];
inpi->lowVelocity = buffer[4];
inpi->highVelocity = buffer[5];
inpi->gain = ARRANGE_BE16(gain);
inpi->sustainLoop.playMode = ARRANGE_BE16(sustainLoop.playMode);
inpi->releaseLoop.playMode = ARRANGE_BE16(releaseLoop.playMode);
/* Read the MarkerId`s for the positions */
ids[0] = ARRANGE_BE16(sustainLoop.beginLoop);
ids[1] = ARRANGE_BE16(sustainLoop.endLoop);
ids[2] = ARRANGE_BE16(releaseLoop.beginLoop);
ids[3] = ARRANGE_BE16(releaseLoop.endLoop);
/* Read the positions */
memset(positions, 0, 16 /* 4*4 */ ); /* by default set them to 0 */
for (;;) {
uint64_t p;
if (read_aifx_marker(r, &id, &p, &name) < 1) {
break;
}
pos = p;
if (name)
free(name);
for (i = 0; i < 4; ++i) {
if (id == ids[i])
positions[i] = pos;
}
}
inpi->sustainLoop.beginLoop = positions[0];
inpi->sustainLoop.endLoop = positions[1];
inpi->releaseLoop.beginLoop = positions[2];
inpi->releaseLoop.endLoop = positions[3];
return (1);
}
int
do_aifx_prepare(AIFF_Ref r)
{
uint32_t clen;
SoundChunk s;
long of;
assert(sizeof(SoundChunk) == 8);
if (!find_iff_chunk(AIFF_SSND, r, &clen))
return (-1);
if (clen < 8)
return (-1);
clen -= 8;
r->soundLen = clen;
r->pos = 0;
if (fread(&s, 1, 8, r->fd) < 8) {
return (-1);
}
s.offset = ARRANGE_BE32(s.offset);
if (s.offset)
r->soundLen -= s.offset;
of = s.offset;
/*
* FIXME: What is s.blockSize?
*/
if (of > 0) {
if (!(r->flags & F_NOTSEEKABLE)) {
if (fseek(r->fd, of, SEEK_CUR) < 0)
return (-1);
} else {
while (of-- > 0) {
if (getc(r->fd) < 0)
return (-1);
}
}
}
return (1);
}
struct s_enc_name {
IFFType enc;
const char *name;
};
#define kNumEncs 6
static struct s_enc_name encNames[kNumEncs] = {
{AUDIO_FORMAT_LPCM, "Signed integer (big-endian) linear PCM"},
{AUDIO_FORMAT_twos, "Signed integer (big-endian) linear PCM"},
{AUDIO_FORMAT_sowt, "Signed integer (little-endian) linear PCM"},
{AUDIO_FORMAT_FL32, "Signed IEEE-754 single precision (big-endian) "
"floating point PCM"},
{AUDIO_FORMAT_ULAW, "Signed 8-bit mu-Law floating point PCM"},
{AUDIO_FORMAT_ALAW, "Signed 8-bit A-Law floating point PCM"}
};
const char *
get_aifx_enc_name(IFFType enc)
{
int i;
struct s_enc_name* e = encNames;
for (i = 0; i < kNumEncs; ++i) {
if (e[i].enc == enc)
return (e[i].name);
}
return (NULL);
}