AeInstFile.c++
10.6 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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <bstring.h>
#include <PR/libaudio.h>
#include <PR/audiotools.h>
#include <iostream.h>
#include <fstream.h>
#include <iomanip.h>
#include "AeAsset.h"
#include "AeFile.h"
#include "AeInstFile.h"
#include "AeWaveFile.h"
#include "AeAssetBase.h"
#include "AeAssetBin.h"
#include "y.tab.h"
#define kNInstTypes 5
static TAssetType writeOrderTable[] =
{ kEnvType, kKeymapType, kSoundType, kInstType, kBankType };
AeInstFile * instFile; // global shared with y.tab.c
AeInstFile::AeInstFile (char * name)
: AeAssetFile (name)
{
curBankFile = NULL;
curBank = NULL;
curInstrument = NULL;
curSound = NULL;
curKeyMap = NULL;
curEnvelope = NULL;
hash = NULL;
list = NULL;
}
AeInstFile::~AeInstFile (void)
{
}
AeErr
AeInstFile::Read (AeAsset *& asset)
{
AeErr err;
hash = new AeInstHash;
list = new AeInstList;
if ((err = read ()) == kAeNoErr)
asset = curBank;
else
ReportError (err);
if (hash) delete hash;
if (list) delete list;
return err;
}
AeErr
AeInstFile::ReadList (GList<AeAsset *> *& assetList)
{
AeErr err;
hash = new AeInstHash;
list = new AeInstList;
if ((err = read ()) == kAeNoErr)
{
// copy over the asset declarations into the assetList
GList<AeInstDecl *> * instList = list->GetList ();
AeInstDecl * decl;
int i;
assetList = new GList<AeAsset *>;
instList = list->GetList ();
instList->Start (i);
while (instList->Next (i, decl))
assetList->Append (decl->asset);
}
else
ReportError (err);
if (hash) delete hash;
if (list) delete list;
return err;
}
//
// This routine creates a temporary file and writes
// the appropriate asset base data into the file in
// proper order. If everything is successful, it
// backs up the old file and swaps the temporary
// file to be the current one.
//
AeErr
AeInstFile::Write (AeAssetBase * base)
{
ofstream fOut;
char * tmpName = tmpnam (NULL);
int i;
AeErr err = kAeNoErr;
assert (base);
fOut.open (tmpName);
if (fOut)
{
// write out each asset type in correct order
for (i=0; i<kNInstTypes; i++)
{
if (err = write (fOut, base, writeOrderTable[i]))
break;
}
// close the stream
fOut.close ();
// if successful, then make the file real
if (!err)
SwapTmpFile (tmpName);
else
ReportError (err);
}
return err;
}
//
// This routine gets the asset bin associated with 'type'
// and writes each of its assets to fOut.
// It aborts and returns an error if any of the assets
// fail to write.
//
AeErr
AeInstFile::write (ofstream & fOut, AeAssetBase * base, TAssetType type)
{
AeAssetBin * bin;
GList<AeAsset *> * assetList;
AeAsset * asset;
int i;
AeErr err = kAeNoErr;
if (base->FindTypedBin (type, bin))
{
assetList = bin->GetAssetList ();
assetList->Start (i);
while (assetList->Next (i, asset))
{
if (err = writeAsset (fOut, asset))
break;
}
}
return err;
}
AeErr
AeInstFile::writeAsset (ofstream & fOut, AeAsset * asset)
{
AeErr err;
assert (asset);
switch (asset->GetType ())
{
case kBankType:
err = writeBank (fOut, (AeBankAsset *)asset);
break;
case kInstType:
err = writeInst (fOut, (AeInstAsset *)asset);
break;
case kSoundType:
err = writeSound (fOut, (AeSoundAsset *)asset);
break;
case kEnvType:
err = writeEnv (fOut, (AeEnvAsset *)asset);
break;
case kKeymapType:
err = writeKeymap (fOut, (AeKeymapAsset *)asset);
break;
default:
err = kAeNoErr;
break;
}
return err;
}
AeErr
AeInstFile::writeBank (ofstream & fOut, AeBankAsset * asset)
{
GList<AeAsset *> * childList;
AeAsset * child;
int i;
char * prefix = MakeFilePrefix ();
AeErr err = kAeNoErr;
fOut << "bank " << prefix << "\n{\n";
if (child = asset->GetPercussion())
fOut << "\tpercussionDefault = " << child->GetName() << ";\n";
fOut << "\tsampleRate = " << asset->GetSampleRate() << ";\n\n";
if (childList = asset->GetChildList ())
{
childList->Start (i);
while (childList->Next (i, child))
{
fOut << "\tinstrument [" << i-1 << "] = " << child->GetName() << ";\n";
}
}
fOut << "}\n\n";
if (prefix) free (prefix);
if (!fOut)
err = kAeFileBadWrite;
return err;
}
AeErr
AeInstFile::writeInst (ofstream & fOut, AeInstAsset * asset)
{
GList<AeAsset *> * childList;
AeAsset * child;
int i;
AeErr err = kAeNoErr;
fOut << "instrument " << asset->GetName() << "\n{\n";
fOut << "\tvolume = " << asset->GetVolume() << ";\n";
fOut << "\tpan = " << asset->GetPan() << ";\n";
fOut << "\tpriority = " << asset->GetPriority() << ";\n";
fOut << "\tbendRange = " << asset->GetBendRange() << ";\n\n";
if (asset->GetTremType ())
{
fOut << "\ttremoloType = " << asset->GetTremType() << ";\n";
fOut << "\ttremoloRate = " << asset->GetTremRate() << ";\n";
fOut << "\ttremoloDepth = " << asset->GetTremDepth() << ";\n";
fOut << "\ttremoloDelay = " << asset->GetTremDelay() << ";\n\n";
}
if (asset->GetVibType ())
{
fOut << "\tvibratoType = " << asset->GetVibType() << ";\n";
fOut << "\tvibratoRate = " << asset->GetVibRate() << ";\n";
fOut << "\tvibratoDepth = " << asset->GetVibDepth() << ";\n";
fOut << "\tvibratoDelay = " << asset->GetVibDelay() << ";\n\n";
}
if (childList = asset->GetChildList ())
{
childList->Start (i);
while (childList->Next (i, child))
{
fOut << "\tsound = " << child->GetName() << ";\n";
}
}
fOut << "}\n\n";
if (!fOut)
err = kAeFileBadWrite;
return err;
}
AeErr
AeInstFile::writeSound (ofstream & fOut, AeSoundAsset * asset)
{
AeAsset * refAsset;
AeWaveFile * file;
char fileName[256];
AeErr err = kAeNoErr;
*fileName = '\0';
if (refAsset = asset->GetWave ())
{
if (file = ((AeWaveAsset *)refAsset)->GetFile ())
strcpy (fileName, file->GetFullName ());
}
fOut << "sound " << asset->GetName() << "\n{\n";
fOut << "\tuse (\"" << fileName << "\");\n";
fOut << "\tvolume = " << asset->GetVolume() << ";\n";
fOut << "\tpan = " << asset->GetPan() << ";\n";
if (refAsset = asset->GetKeymap ())
fOut << "\tkeymap = " << refAsset->GetName() << ";\n";
if (refAsset = asset->GetEnvelope ())
fOut << "\tenvelope = " << refAsset->GetName() << ";\n";
fOut << "}\n\n";
if (!fOut)
err = kAeFileBadWrite;
return err;
}
AeErr
AeInstFile::writeEnv (ofstream & fOut, AeEnvAsset * asset)
{
AeErr err = kAeNoErr;
fOut << "envelope " << asset->GetName() << "\n{\n";
fOut << "\tattackTime = " << asset->GetAttackTime() << ";\n";
fOut << "\tattackVolume = " << asset->GetAttackLevel() << ";\n";
fOut << "\tdecayTime = " << asset->GetDecayTime() << ";\n";
fOut << "\tdecayVolume = " << asset->GetDecayLevel() << ";\n";
fOut << "\treleaseTime = " << asset->GetReleaseTime() << ";\n";
fOut << "}\n\n";
if (!fOut)
err = kAeFileBadWrite;
return err;
}
AeErr
AeInstFile::writeKeymap (ofstream & fOut, AeKeymapAsset * asset)
{
AeErr err = kAeNoErr;
fOut << "keymap " << asset->GetName() << "\n{\n";
fOut << "\tvelocityMin = " << asset->GetVelMin() << ";\n";
fOut << "\tvelocityMax = " << asset->GetVelMax() << ";\n";
fOut << "\tkeyMin = " << asset->GetKeyMin() << ";\n";
fOut << "\tkeyMax = " << asset->GetKeyMax() << ";\n";
fOut << "\tkeyBase = " << asset->GetKeyBase() << ";\n";
fOut << "\tdetune = " << asset->GetDetune() << ";\n";
fOut << "}\n\n";
if (!fOut)
err = kAeFileBadWrite;
return err;
}
AeErr
AeInstFile::read (void)
{
FILE * fileRef;
AeErr err = kAeNoErr;
assert (fFullName);
instFile = this;
if (fileRef = freopen (fFullName, "r", stdin))
{
initLex ();
initReserved ();
if (yyparse ())
err = kAeFileBadRead;
fclose (fileRef);
}
else
err = kAeFileCantOpen;
return err;
}
void
AeInstFile::initReserved (void)
{
/* enter the key words in the symbol table to reserve them */
hash->Enter (use, "use", 3);
hash->Enter (sampleRate, "sampleRate", strlen("sampleRate"));
hash->Enter (priority, "priority", strlen("priority"));
hash->Enter (bank, "bank", 4);
hash->Enter (instrument, "instrument", 10);
hash->Enter (sound, "sound", 5);
hash->Enter (wavetable, "wavetable", 9);
hash->Enter (volume, "volume", 6);
hash->Enter (pan, "pan", 3);
hash->Enter (keymap, "keymap", 6);
hash->Enter (keyMin, "keyMin", 6);
hash->Enter (keyMax, "keyMax", 6);
hash->Enter (keyBase, "keyBase", 7);
hash->Enter (velocityMin, "velocityMin", 11);
hash->Enter (velocityMax, "velocityMax", 11);
hash->Enter (detune, "detune", 6);
hash->Enter (envelope, "envelope", 8);
hash->Enter (attackTime, "attackTime", 10);
hash->Enter (attackVolume, "attackVolume", 12);
hash->Enter (decayTime, "decayTime", 9);
hash->Enter (decayVolume, "decayVolume", 11);
hash->Enter (releaseTime, "releaseTime", 11);
hash->Enter (releaseVolume, "releaseVolume", 13);
hash->Enter (percussionDefault, "percussionDefault", 17);
hash->Enter (vibratoType, "vibratoType", 11);
hash->Enter (vibratoRate, "vibratoRate", 11);
hash->Enter (vibratoDepth, "vibratoDepth", 12);
hash->Enter (vibratoDelay, "vibratoDelay", 12);
hash->Enter (tremoloType, "tremoloType", 11);
hash->Enter (tremoloRate, "tremoloRate", 11);
hash->Enter (tremoloDepth, "tremoloDepth", 12);
hash->Enter (tremoloDelay, "tremoloDelay", 12);
// maintain backward compatibility with misspelled reserved words
hash->Enter (tremoloType, "tremeloType", 11);
hash->Enter (tremoloRate, "tremeloRate", 11);
hash->Enter (tremoloDepth, "tremeloDepth", 12);
hash->Enter (tremoloDelay, "tremeloDelay", 12);
hash->Enter (bendRange, "bendRange", strlen("bendRange"));
}
//
// this routine strips the quotes off the end of a string and appends a
// null character.
//
char *
AeInstFile::MakeUnquotedStr (char * src)
{
char * dest;
char * start = strchr(src, '"');
char * end = strrchr(src, '"');
start++; // ditch first quote
if (dest = (char *)malloc (end-start+1))
{
strncpy (dest, start, end-start);
dest[end-start] = 0;
}
return dest;
}
void pline(void)
{
fprintf(stdout, "error: line %d: ", yylineno);
}
void yyerror(char *s)
{
pline();
fprintf(stdout, "%s: ", s);
fprintf(stdout, "last token was %s\n", yytext);
}