sequence.c++
10.3 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
#include <assert.h>
#include "libbank.h"
#include <PR/libaudio.h>
#include <stdio.h>
#include <strings.h>
#include <stdlib.h>
#define IFF_FILE_HDR 'MThd'
#define IFF_TRACK_HDR 'MTrk'
int ICSeq::SetSeq(int /*number*/)
{
/* empty in base class, each sub class should handle this */
return 0;
}
int ICSeq::NextSeqNote(void)
{
return 0;
}
void ICSeq::loadFile(char *fname)
{
long fileSize;
int rv;
if((seqFile = fopen(fname, "r")) == NULL){
fprintf(stderr,"Could not open file %s\n",fname);
exit(-1);
}
seqFname = fname; /* save name for error messages */
fseek(seqFile, 0, SEEK_END);
fileSize = ftell(seqFile);
rewind(seqFile);
seqBank = (ALSeqFile*)malloc(fileSize);
if(!seqBank)
{
fprintf(stderr,"Unable to allocate memory for sequence data\n");
exit(-1);
}
rv = fread (seqBank, fileSize, 1, seqFile);
if (rv != 1)
fprintf(stderr,"Could not read file %s",fname);
if(seqBank->revision != AL_SEQBANK_VERSION)
{
fprintf(stderr,"Sequence bank file %s is not of a supported version\n",
seqFname);
exit(-1);
}
}
void ICSeq::ResetChanMap(void)
{
int i;
for(i=0;i<16;i++)
ChanProgMap[i] = 0;
ChanProgMap[9] = 128;
}
/**********************************************************************
*
* Compact Midi Sequence class
*
*********************************************************************/
int gSeq;
int ICCSeq::SetSeq(int number)
{
int offset,i,flagTmp;
gSeq = number;
if(number >= seqBank->seqCount)
return FALSE; // no such sequence return FALSE for failure
offset = (int)seqBank->seqArray[number].offset;
base = (ALCMidiHdr*)((char*)seqBank + offset);
validTracks = 0;
for(i=0;i<16;i++)
{
curBUPtr[i] = 0;
curBULen[i] = 0;
lastStatus[i] = 0;
offset = base->trackOffset[i];
if(offset)
{
flagTmp = 1 << i;
validTracks |= flagTmp;
curLoc[i] = (char*)((int)base + offset);
}
else
curLoc[i] = 0;
}
return TRUE;
}
int ICCSeq::NextSeqNote(char *program, char *key)
{
int trackFlags,track;
int deltaTicks,duration;
char status,type,byte1;
while(validTracks)
{
track = -1;
trackFlags = validTracks;
while(trackFlags)
{
track++;
trackFlags >>= 1;
}
deltaTicks = readVarLen(track); /* read the delta time */
status = getTrackByte(track); /* read the status byte */
if (status == AL_MIDI_Meta) /* running status not allowed on meta events!! */
{
type = getTrackByte(track);
if (type == AL_MIDI_META_TEMPO)
{
getTrackByte(track);
getTrackByte(track);
getTrackByte(track);
lastStatus[track] = 0; /* lastStatus not supported after meta */
}
else if (type == AL_MIDI_META_EOT)
{
u32 flagMask;
flagMask = 0x01 << track;
validTracks = validTracks ^ flagMask;
lastStatus[track] = 0; /* lastStatus not supported after meta */
}
}
else if (type == AL_CMIDI_LOOPSTART_CODE)
{
getTrackByte(track); /* get next two bytes, ignore them */
getTrackByte(track);
lastStatus[track] = 0;
}
else if (type == AL_CMIDI_LOOPEND_CODE)
{
getTrackByte(track); /* get next six bytes, ignore them */
getTrackByte(track);
getTrackByte(track);
getTrackByte(track);
getTrackByte(track);
getTrackByte(track);
lastStatus[track] = 0;
}
else
{
if (status & 0x80) /* if high byte is set, then new status */
{
byte1 = getTrackByte(track);
lastStatus[track] = status;
}
else /* running status */
{
byte1 = status;
status = lastStatus[track];
}
switch (status & 0xF0)
{
case AL_MIDI_ProgramChange:
ChanProgMap[track] = byte1;
break;
case AL_MIDI_NoteOn:
getTrackByte(track); /* get velocity */
duration = readVarLen(track); /* get duration */
*program = ChanProgMap[track];
*key = byte1;
return TRUE;
case AL_MIDI_NoteOff:
case AL_MIDI_PolyKeyPressure:
case AL_MIDI_ControlChange:
case AL_MIDI_PitchBendChange:
getTrackByte(track);
break;
}
}
}
return FALSE;
}
int ICCSeq::readVarLen(int track)
{
int value;
int c;
value = (int)getTrackByte(track);
if(value&0x00000080)
{
value &= 0x7F;
do
{
c = (int)getTrackByte(track);
value = (value << 7) + (c & 0x7F);
} while(c & 0x80);
}
return(value);
}
char ICCSeq::getTrackByte(int track)
{
char theByte;
if(curBULen[track])
{
theByte = *curBUPtr[track];
curBUPtr[track]++;
curBULen[track]--;
}
else /* need to handle backup mode */
{
theByte = *curLoc[track];
curLoc[track]++;
if(theByte == AL_CMIDI_BLOCK_CODE)
{
char loBackUp,hiBackUp,theLen,nextByte;
int backup;
nextByte = *curLoc[track];
curLoc[track]++;
if(nextByte == AL_CMIDI_BLOCK_CODE)
theByte = nextByte;
else
{
/* if here, then got a backup section. get the amount of
backup, and the len of the section. Subtract the amount of
backup from the curLoc ptr, and subtract four more, since
curLoc has been advanced by four while reading the codes. */
hiBackUp = nextByte;
loBackUp = *curLoc[track];
curLoc[track]++;
theLen = *curLoc[track];
curLoc[track]++;
backup = (int)hiBackUp;
backup = backup << 8;
backup += loBackUp;
curBUPtr[track] = curLoc[track] - (backup + 4);
curBULen[track] = (int)theLen;
/* now get the byte */
theByte = *curBUPtr[track];
curBUPtr[track]++;
curBULen[track]--;
}
}
}
return theByte;
}
/*************************************************
*
* Midi type 0 class
*
*************************************************/
int ICMSeq::SetSeq(int number)
{
int offset;
if(number >= seqBank->seqCount)
return FALSE; // no such sequence return FALSE for failure
offset = (int)seqBank->seqArray[number].offset;
seqPtr = ((char*)seqBank + offset);
lastStatus = 0;
if (read32() != IFF_FILE_HDR)
{
fprintf(stderr,"Sequence #%d Error in the file header\n",
number);
return FALSE;
}
read32(); /* skip the length field */
if (read16() != 0)
{
fprintf(stderr,"Sequence #%d isn't a type 0 midi\n",
number);
return FALSE;
}
if (read16() != 1)
{
fprintf(stderr,"Sequence #%d incorrect number of tracks\n",
number);
return FALSE;
}
read16(); /* skip division */
if (read32() != IFF_TRACK_HDR)
{
fprintf(stderr,"Sequence #%d, Track header not correct\n",
number);
return FALSE;
}
read32(); /* skip the length */
validSeq = TRUE;
return TRUE;
}
int ICMSeq::NextSeqNote(char *program, char *key)
{
char status,byte1;
int deltaTicks;
int len,i;
while(validSeq)
{
deltaTicks = readVarLen();
status = read8();
if ((status == 0xf0) || (status == 0xf7))
{
len = readVarLen();
for (i = 0; i < len; i++)
read8();
lastStatus = status;
}
else if (status == AL_MIDI_Meta)
{
byte1 = read8();
len = readVarLen();
for (i = 0; i < len; i++)
read8();
lastStatus = status;
if(byte1 == AL_MIDI_META_EOT)
validSeq = FALSE;
}
else
{
if (status & 0x80)
byte1 = read8();
else /* running status */
{
byte1 = status;
status = lastStatus;
if(status == 0)
printf("Bad running status status!\n");
}
lastStatus = status;
switch(status & 0xF0)
{
case AL_MIDI_NoteOff:
case AL_MIDI_PolyKeyPressure:
case AL_MIDI_ControlChange:
case AL_MIDI_PitchBendChange:
read8();
break;
case AL_MIDI_NoteOn:
read8();
*program = ChanProgMap[status & 0x0F];
*key = byte1;
return TRUE;
case AL_MIDI_ProgramChange:
ChanProgMap[status & 0x0F] = byte1;
break;
}
}
}
return FALSE;
}
/* non-aligned byte reading routines */
char ICMSeq::read8(void)
{
return *seqPtr++;
}
short ICMSeq::read16(void)
{
short tmp;
tmp = *seqPtr++ << 8;
tmp |= *seqPtr++;
return tmp;
}
int ICMSeq::read32(void)
{
int tmp;
tmp = *seqPtr++ << 24;
tmp |= *seqPtr++ << 16;
tmp |= *seqPtr++ << 8;
tmp |= *seqPtr++;
return tmp;
}
int ICMSeq::readVarLen(void)
{
int value;
char c;
c = *seqPtr++;
value = (int)c;
if ( c & 0x80 ) {
value &= 0x7f;
do {
c = *seqPtr++;
value = (value << 7) + (c & 0x7f);
} while (c & 0x80);
}
return (value);
}