soundfile.c++
12.9 KB
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
//====================================================================
// soundfile.c++
//
// Copyright 1993, Silicon Graphics, Inc.
// All Rights Reserved.
//
// This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
// the contents of this file may not be disclosed to third parties, copied or
// duplicated in any form, in whole or in part, without the prior written
// permission of Silicon Graphics, Inc.
//
// RESTRICTED RIGHTS LEGEND:
// Use, duplication or disclosure by the Government is subject to restrictions
// as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
// and Computer Software clause at DFARS 252.227-7013, and/or in similar or
// successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
// rights reserved under the Copyright Laws of the United States.
//====================================================================
#include <stdlib.h>
#include <bstring.h>
#include <string.h>
#include "libbank.h"
#define BLOCK_SIZE 1024
#define IFF_ID_FORM 'FORM'
#define IFF_ID_AIFC 'AIFC'
#define IFF_ID_AIFF 'AIFF'
#define IFF_ID_COMM 'COMM'
#define IFF_ID_SSND 'SSND'
#define IFF_ID_INST 'INST'
#define IFF_ID_APPL 'APPL'
#define IFF_ID_MARK 'MARK'
#define CODE_NAME "VADPCMCODES"
#define LOOP_NAME "VADPCMLOOPS"
#define VERSION 1
#define NoLooping 0
#define ForwardLooping 1
#define ForwardBackwardLooping 2
typedef struct {
unsigned long ckID;
long ckSize;
} ChunkHeader;
typedef struct {
unsigned long ckID;
long ckSize;
unsigned long formType;
} Chunk;
typedef struct {
short numChannels;
unsigned short numFramesH;
unsigned short numFramesL;
short sampleSize;
char sampleRate[10];
unsigned short compressionTypeH; // Separated into HL to prevent alignment
unsigned short compressionTypeL; /* note: string follows */
} CommonChunk;
typedef short MarkerID;
typedef struct {
MarkerID id;
unsigned short positionH; // Separated into HL to prevent alignment
unsigned short positionL; /* note: pstring follows */
} Marker;
int lookupMarker(long *sample, MarkerID loopPoint, Marker *markers, int nmarkers);
typedef struct {
short playMode;
MarkerID beginLoop;
MarkerID endLoop;
} Loop;
typedef struct {
char baseNote;
char detune;
char lowNote;
char highNote;
char lowVelocity;
char highVelocity;
short gain;
Loop sustainLoop;
Loop releaseLoop;
} InstrumentChunk;
typedef struct {
unsigned long offset;
unsigned long blockSize;
} SoundDataChunk;
typedef struct {
unsigned long hi;
unsigned long lo;
} IEEE_DBL;
typedef struct {
unsigned long l1;
unsigned long l2;
unsigned short s1;
} SANE_EXT;
ICSoundFile::ICSoundFile(char *fname)
{
long waveLen;
long offset;
int i;
Chunk FormChunk;
CommonChunk comm;
int done=0;
long skip;
ChunkHeader Header;
long ts;
int num;
char *ChunkName;
ICLoop *icl;
ICBook *icb;
ALADPCMloop LpChunk;
short order;
short npredictors;
short version;
short nloops;
short nMarkers;
long start;
long end;
Marker *markers;
SoundDataChunk data;
InstrumentChunk inst;
int bookSize;
short *bookp;
char msg[256];
unsigned long compressionType;
declclass = ICSOUNDFILE;
size = 0; /* don't know yet */
dataOffset = 0;
wave = new ICWave();
FailIf(!wave, IC_INTERNAL_ERR);
keymap = new ICKeyMap();
FailIf(!keymap, IC_INTERNAL_ERR);
icb = new ICBook();
FailIf(!icb, IC_INTERNAL_ERR);
sound = new (ICSound);
FailIf(!sound, IC_INTERNAL_ERR);
sound->SetKeyMap(keymap);
sound->SetWaveTable(wave);
wave->SetName(fname);
sourceFile = fopen(fname, "r");
FailIfMsg(!sourceFile, IC_FILE_ERR,
icErrStr(msg, "can't open file ", fname));
fileName = (char *)malloc(strlen(fname) + 1);
FailIf(!fileName, IC_INTERNAL_ERR);
strcpy(fileName, fname);
fread(&FormChunk, sizeof(FormChunk), 1, sourceFile);
if ((FormChunk.ckID != IFF_ID_FORM) ||
((FormChunk.formType != IFF_ID_AIFC) &&
(FormChunk.formType != IFF_ID_AIFF))) {
printf("this is not an AIFF-C File\n");
return;
}
while (!done) {
num = fread(&Header, sizeof(ChunkHeader), 1, sourceFile);
if (num < 1) {
done = 1;
break;
}
skip = (Header.ckSize + 1) & ~1; /* account for pad byte */
switch (Header.ckID) {
case (IFF_ID_COMM):
{
offset = ftell(sourceFile);
num = fread (&comm, sizeof(CommonChunk), 1, sourceFile);
if (num < 1) {
printf("error parsing file %s\n", sourceFile);
done = 1;
}
if (FormChunk.formType != IFF_ID_AIFF)
compressionType = comm.compressionTypeH<<16 | comm.compressionTypeL;
else
compressionType = 'NONE';
fseek(sourceFile, offset + skip, SEEK_SET);
}
break;
case (IFF_ID_MARK):
offset = ftell(sourceFile);
fread(&nMarkers, sizeof(short), 1, sourceFile);
markers = (Marker *) calloc(nMarkers, sizeof(Marker));
for (i=0; i<nMarkers; i++){
fread(&markers[i], sizeof(Marker), 1, sourceFile);
ChunkName = ReadPString(sourceFile);
free(ChunkName); // Junk the marker name
}
fseek(sourceFile, offset + skip, SEEK_SET);
break;
case (IFF_ID_SSND):
offset = ftell(sourceFile);
fread(&data, sizeof (data), 1, sourceFile);
FailIfMsg(data.offset != 0, IC_MISC_ERR,
"AIFC SndD offset != 0");
FailIfMsg(data.blockSize != 0, IC_MISC_ERR,
"AIFC SndD block size != 0");
dataOffset = ftell(sourceFile);
waveLen = Header.ckSize - 8;
wave->SetLen((int)waveLen);
this->size = (int)waveLen;
fseek(sourceFile, offset + skip, SEEK_SET);
break;
case (IFF_ID_INST):
offset = ftell(sourceFile);
fread(&inst, sizeof(InstrumentChunk), 1, sourceFile);
keymap->SetKeyMin(inst.lowNote);
keymap->SetKeyMax(inst.highNote);
keymap->SetKeyBase(inst.baseNote);
keymap->SetVelMin(inst.lowVelocity);
keymap->SetVelMax(inst.highVelocity);
keymap->SetDetune(inst.detune);
fseek(sourceFile, offset + skip, SEEK_SET);
break;
case (IFF_ID_APPL):
{
offset = ftell(sourceFile);
fread(&ts, sizeof(ts), 1, sourceFile);
if (ts == 'stoc'){
ChunkName = ReadPString(sourceFile);
if (!strcmp(LOOP_NAME, ChunkName)){
fread(&version, sizeof(short), 1, sourceFile);
if (version != VERSION) {
printf("Non-identical loop chunk versions\n");
}
fread(&nloops, sizeof(short), 1, sourceFile);
if (nloops > 1){
printf("Found %d loops, only 1 supported\n");
nloops = 1;
}
for (i=0; i<nloops; i++) {
fread(&LpChunk, sizeof(ALADPCMloop),1, sourceFile);
}
icl = new ICLoop();
FailIf(!icl, IC_INTERNAL_ERR);
icl->SetPoints(LpChunk.start, LpChunk.end,
LpChunk.count, LpChunk.state);
// Must be an ADPCM file to have this chunk
wave->SetType(AL_ADPCM_WAVE);
wave->SetLoop(icl);
} else {
if (!strcmp(CODE_NAME, ChunkName)){
fread(&version, sizeof(short), 1, sourceFile);
if (version != VERSION)
printf("Bad codebook chunk version\n");
fread(&order, sizeof(short), 1, sourceFile);
fread(&npredictors, sizeof(short), 1, sourceFile);
bookSize = npredictors*order*ADPCMVSIZE;
bookp = (short *)malloc(bookSize*sizeof(short));
FailIf(!bookp, IC_INTERNAL_ERR);
fread(bookp,sizeof(short), bookSize, sourceFile);
icb->SetBookData(bookp, order, npredictors);
free(bookp);
wave->SetBook(icb);
}
}
}
fseek(sourceFile, offset + Header.ckSize, SEEK_SET);
}
break;
default:
/* skip chunks we don't know about */
fseek(sourceFile, skip, SEEK_CUR);
break;
}
}
wave->SetType(AL_ADPCM_WAVE);
if (compressionType == 'NONE'){
// Set the type according to the file type and bits/sample
switch (comm.sampleSize){
case (16):
wave->SetType(AL_RAW16_WAVE);
break;
default:
printf("format not supported\n");
break;
}
// Set up the loop point from the Marker definitions.
// Note: only pay attention to a Sustain loop in forward mode
if (inst.sustainLoop.playMode == ForwardLooping){
if (lookupMarker(&start, inst.sustainLoop.beginLoop, markers, nMarkers))
printf("Begin loop marker not found\n");
if (lookupMarker(&end, inst.sustainLoop.endLoop, markers, nMarkers))
printf("End loop marker not found\n");
// Make it an infinitely repeating loop
icl = new ICLoop();
FailIf(!icl, IC_INTERNAL_ERR);
icl->SetPoints(start, end, 0xffffffff, 0);
wave->SetLoop(icl);
}
}
fclose(sourceFile);
}
void ICSoundFile::Assemble()
{
size = wave->GetLen();
size = Size();
wave->SetBase((char *)offset);
}
void ICSoundFile::Prune()
{
// do nothing, we want this to be pruned from the list
}
void ICSoundFile::Print()
{}
void ICSoundFile::Write(FILE *file)
{
char *ptr = new char[BLOCK_SIZE];
FailIf(!ptr, IC_INTERNAL_ERR);
FailIf((int)offset != ftell(file), IC_INTERNAL_ERR);
FailIfMsg(!dataOffset, IC_INTERNAL_ERR, "no offset for sound file data");
sourceFile = fopen(fileName, "r");
FailIfMsg(!sourceFile, IC_INTERNAL_ERR, "couldn't open sound file");
int rv = fseek(sourceFile, dataOffset, SEEK_SET);
FailIfMsg(rv, IC_INTERNAL_ERR, "fseek failed");
int waveLen = wave->GetLen();
while(waveLen > 0) {
int bytes = MIN(waveLen, BLOCK_SIZE);
fread(ptr, sizeof(char), bytes, sourceFile);
fwrite(ptr, sizeof(char), bytes, file);
waveLen -= bytes;
}
// Pad tbl file if necessary
waveLen = wave->GetLen();
if ((size - waveLen) > 0) {
bzero(ptr, size-waveLen);
fwrite(ptr, sizeof(char), size - waveLen, file);
}
fclose(sourceFile);
}
int lookupMarker(long *sample, MarkerID loopPoint, Marker *markers, int nmarkers)
{
for (int i=0; i<nmarkers; i++)
if (loopPoint == markers[i].id){
*sample = (markers[i].positionH<<16) + markers[i].positionL;
return 0;
}
return 1;
}