sound.c
12.2 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
/*====================================================================
* sound.c
*
* Synopsis:
*
* 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 <assert.h>
#include <stdlib.h>
#include "ic.h"
#define BLOCK_SIZE 1024
#define IFF_ID_FORM 'FORM'
#define IFF_ID_AIFC 'AIFC'
#define IFF_ID_COMM 'COMM'
#define IFF_ID_SSND 'SSND'
#define IFF_ID_INST 'INST'
#define IFF_ID_APPL 'APPL'
#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 long compressionType; /* note: string follows */
} CommonChunk;
typedef short MarkerID;
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;
ICSound *curSound=0;
void soundNew(ICSound *s)
{
s->decl.declclass = ICSOUND;
s->decl.id = objectID++;
s->decl.offset = (void *)-1; /* not allocated yet */
s->decl.size = sizeof(ALSound);
s->keymap = 0;
s->samplePan = 0;
s->sampleVolume = 127;
s->sound = 0;
s->wave = 0;
}
void soundSetPan(ICSound *s, int pan)
{
s->samplePan = pan;
}
void soundSetVol(ICSound *s, int vol)
{
s->sampleVolume = vol;
}
static void extendedToDouble(char *ps, double *pd)
{
register unsigned long top2bits;
register IEEE_DBL *p_dbl;
register SANE_EXT *p_ext;
p_dbl = (IEEE_DBL *) pd;
p_ext = (SANE_EXT *) ps;
top2bits = p_ext->l1 & 0xc0000000;
p_dbl->hi = ((p_ext->l1 << 4) & 0x3ff00000) | top2bits;
p_dbl->hi |= (p_ext->l1 << 5) & 0xffff0;
p_dbl->hi |= (p_ext->l2 >> 27) & 0x1f;
p_dbl->lo = (p_ext->l2 << 5) & 0xffffffe0;
p_dbl->lo |= (unsigned long) ((p_ext->s1 >> 11) & 0x1f);
}
void soundFromFile(ICSound *sound, char *file)
{
FILE *infile;
char temp_sampleRate[10];
double srate;
ChunkHeader Header;
Chunk FormChunk;
CommonChunk CommChunk;
SoundDataChunk SndDChunk;
Aloop LpChunk;
InstrumentChunk inst;
int num;
int done=0;
char *start;
char *end;
char *nfile;
long offset;
ICWave *icw;
ICKeyMap *ick;
ICLoop *icl;
ICBook *icb;
int waveLen;
int chunkLen;
int bytes;
char *ptr;
long skip;
long ts;
short nloops;
short version;
char *ChunkName;
int i;
int j;
int k;
int n;
int bookSize;
short order;
short npredictors;
icw = calloc(1,sizeof(ICWave)); /* ### should probably declare this */
assert(icw);
waveNew(icw);
listAppend(objectList, icw);
ick = calloc(1,sizeof(ICKeyMap)); /* ### should probably declare this */
assert(ick);
keyNew(ick);
listAppend(objectList, ick);
start = strchr(file, '"');
end = strrchr(file, '"');
start++; /* ditch first quote */
nfile = malloc(end-start);
strncpy(nfile, start, end-start);
nfile[end-start] = 0;
infile = fopen(nfile, "r");
if (infile == 0) {
printf("error: bad filename %s\n", nfile);
exit(1);
}
fread(&FormChunk, sizeof(Chunk), 1, infile);
if ((FormChunk.ckID != IFF_ID_FORM) ||
(FormChunk.formType != IFF_ID_AIFC)) {
printf("this is not an AIFF-C File\n");
return;
}
while (!done) {
num = fread(&Header, sizeof(ChunkHeader), 1, infile);
if (num < 1) {
done = 1;
break;
}
skip = (Header.ckSize + 1) & ~1; /* account for pad byte */
switch (Header.ckID) {
case (IFF_ID_COMM):
offset = ftell(infile);
num = fread (&CommChunk, sizeof(CommonChunk), 1, infile);
if (num < 1) {
printf("error parsing file %s\n", file);
done = 1;
}
extendedToDouble(CommChunk.sampleRate, &srate);
waveSetRate(icw, (int)srate);
waveSetSize(icw, CommChunk.sampleSize);
waveSetChannels(icw, CommChunk.numChannels);
fseek(infile, offset + skip, SEEK_SET);
break;
case (IFF_ID_SSND):
offset = ftell(infile);
fread(&SndDChunk, sizeof (SndDChunk), 1, infile);
assert(SndDChunk.offset == 0);
assert(SndDChunk.blockSize == 0);
/*
* ### FIXME expects CommChunk to be first.
* ### not a valid assumption
*/
waveLen = Header.ckSize - 8;
chunkLen = ((waveLen + 0x7) & ~0x7) - waveLen;
waveSetTable(icw, (char *)ftell(tblFile), waveLen);
ptr = malloc(BLOCK_SIZE);
assert(ptr);
while(waveLen > 0) {
bytes = MIN(waveLen, BLOCK_SIZE);
fread(ptr, sizeof(char), bytes, infile);
fwrite(ptr, sizeof(char), bytes, tblFile);
waveLen -= bytes;
}
/*
* Pad tbl file if necessary
*/
if (chunkLen > 0)
fwrite(ptr, sizeof(char), chunkLen - waveLen, tblFile);
fseek(infile, offset + skip, SEEK_SET);
break;
case (IFF_ID_INST):
offset = ftell(infile);
fread(&inst, sizeof(InstrumentChunk), 1, infile);
keySetKeyMin(ick, inst.lowNote);
keySetKeyMax(ick, inst.highNote);
keySetKeyBase(ick, inst.baseNote);
keySetVelMin(ick, inst.lowVelocity);
keySetVelMax(ick, inst.highVelocity);
keySetDetune(ick, inst.detune);
fseek(infile, offset + skip, SEEK_SET);
break;
case (IFF_ID_APPL):
offset = ftell(infile);
fread(&ts, sizeof(long), 1, infile);
if (ts == 'stoc'){
ChunkName = ReadPString(infile);
if (!strcmp(LOOP_NAME, ChunkName)){
fread(&version, sizeof(short), 1, infile);
if (version != VERSION)
fprintf(stderr,"Non-identical loop chunk versions\n");
fread(&nloops, sizeof(short), 1, infile);
if (nloops > 1){
fprintf(stderr, "Found %d loops, only 1 supported\n");
nloops = 1;
}
for (i=0; i<nloops; i++)
fread(&LpChunk, sizeof(Aloop), 1, infile);
icl = calloc(1,sizeof(ICLoop));
loopNew(icl);
listAppend(objectList, icl);
loopSetPoints(icl, LpChunk.start, LpChunk.end, LpChunk.count,
LpChunk.state);
icw->loop = icl;
} else
if (!strcmp(CODE_NAME, ChunkName)){
fread(&version, sizeof(short), 1, infile);
if (version != VERSION)
fprintf(stderr,"Non-identical codebook chunk versions\n");
fread(&order, sizeof(short), 1, infile);
fread(&npredictors, sizeof(short), 1, infile);
bookSize = npredictors*order*ADPCMVSIZE;
icb = calloc(1,sizeof(ICBook) + bookSize*sizeof(short) - 2);
bookNew(icb, order, npredictors);
listAppend(objectList, icb);
for (i=0, n=0; i<npredictors; i++){
for (j=0; j<order; j++)
for (k=0; k<ADPCMVSIZE; k++)
fread(icb->book + n++, sizeof(short), 1, infile);
}
icw->book = icb;
}
}
fseek(infile, offset + Header.ckSize, SEEK_SET);
break;
default:
/* skip chunks we don't know about */
fseek(infile, skip, SEEK_CUR);
break;
}
}
if (!sound->keymap) {
sound->keymap = ick;
}
if (!sound->wave) {
sound->wave = icw;
}
fclose(infile);
}
void soundAddKeyMap(ICSound *s, ICKeyMap *map)
{
s->keymap = map;
}
void soundAddEnvelope(ICSound *s, ICEnvelope *env)
{
s->envelope = env;
}
void soundLayOut(ICSound *ics)
{
ALSound *s;
ics->decl.size = declSize((Decl *)ics);
s = calloc(1, ics->decl.size);
assert(s);
s->samplePan = ics->samplePan;
s->sampleVolume = ics->sampleVolume;
s->keyMap = ics->keymap->decl.offset;
s->envelope = ics->envelope->decl.offset;
s->waveCount = 1;
s->wavetable[0] = ics->wave->decl.offset;
ics->sound = s;
}
void soundPrint(ICSound *s)
{
ALSound *ss = s->sound;
Id *name;
name = findname((Decl *)s);
printf("Sound: %s\n", name->name);
declPrint((Decl *)s);
printf("\twaveCount:\t= %x\n", ss->waveCount);
printf("\twavetable:\t= %x\n", ss->wavetable);
printf("\tenvelope:\t= %x\n", ss->envelope);
printf("\tkey map:\t= %x\n", ss->keyMap);
printf("\tsamplePan:\t= %x\n", ss->samplePan);
printf("\tsampleVolume:\t= %x\n", ss->sampleVolume);
printf("\n");
}
void soundWrite(ICSound *ics, FILE *file)
{
fwrite(ics->sound, ics->decl.size, 1, file);
}