n_seq.c
5.78 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
/*====================================================================
*
* 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 <libaudio.h>
#include "n_libaudio.h"
#include <os_internal.h>
#include <ultraerror.h>
#include "seq.h"
#define IFF_FILE_HDR 'MThd'
#define IFF_TRACK_HDR 'MTrk'
static s32 readVarLen(ALSeq *s);
static u8 read8(ALSeq *s);
static s16 read16(ALSeq *s);
static s32 read32(ALSeq *s);
void n_alSeqNextEvent(ALSeq *seq, N_ALEvent *event)
{
u8 status;
s16 delta;
s32 len;
s32 deltaTicks;
s32 i;
#ifdef _DEBUG
/* sct 1/17/96 - Warn if curPtr is beyond the end of sequence. */
if (seq->curPtr >= seq->base + seq->len)
__osError(ERR_ALSEQOVERRUN, 0);
#endif
deltaTicks = readVarLen(seq); /* read the delta time */
seq->lastTicks += deltaTicks;
status = read8(seq);
#if _DEBUG
/*
* System exclusives are not supported, so just skip them and read
* the next event
*/
if ((status == 0xf0) || (status == 0xf7)) {
__osError(ERR_ALSEQSYSEX, 0);
len = readVarLen(seq);
for (i = 0; i < len; i++) {
read8(seq);
}
n_alSeqNextEvent(seq,event);
return;
}
#endif
if (status == AL_MIDI_Meta) {
u8 type = read8(seq);
if (type == AL_MIDI_META_TEMPO) {
event->type = AL_TEMPO_EVT;
event->msg.tempo.ticks = deltaTicks;
event->msg.tempo.status = status;
event->msg.tempo.type = type;
event->msg.tempo.len = read8(seq);
event->msg.tempo.byte1 = read8(seq);
event->msg.tempo.byte2 = read8(seq);
event->msg.tempo.byte3 = read8(seq);
} else if (type == AL_MIDI_META_EOT) {
event->type = AL_SEQ_END_EVT;
event->msg.end.ticks = deltaTicks;
event->msg.end.status = status;
event->msg.end.type = type;
event->msg.end.len = read8(seq);
} else {
#ifdef _DEBUG
__osError(ERR_ALSEQMETA, 1, type);
len = readVarLen(seq);
for (i = 0; i < len; i++) {
read8(seq);
}
n_alSeqNextEvent(seq,event);
return;
#endif
}
seq->lastStatus = 0;
} else {
event->type = AL_SEQ_MIDI_EVT;
event->msg.midi.ticks = deltaTicks;
if (status & 0x80) {
event->msg.midi.status = status;
event->msg.midi.byte1 = read8(seq);
seq->lastStatus = status;
} else {
/* running status */
event->msg.midi.status = seq->lastStatus;
event->msg.midi.byte1 = status;
}
if (((event->msg.midi.status & 0xf0) != AL_MIDI_ProgramChange) &&
((event->msg.midi.status & 0xf0) != AL_MIDI_ChannelPressure)) {
event->msg.midi.byte2 = read8(seq);
} else {
event->msg.midi.byte2 = 0;
}
}
}
void n_alSeqNewMarker(ALSeq *seq, ALSeqMarker *m, u32 ticks)
{
N_ALEvent evt;
u8 *savePtr, *lastPtr;
s32 saveTicks, lastTicks;
s16 saveStatus, lastStatus;
/* does not check that ticks is within bounds */
if (ticks == 0) { /* common case */
m->curPtr = seq->trackStart;
m->lastStatus = 0;
m->lastTicks = 0;
m->curTicks = 0;
return;
} else {
savePtr = seq->curPtr;
saveStatus = seq->lastStatus;
saveTicks = seq->lastTicks;
seq->curPtr = seq->trackStart;
seq->lastStatus = 0;
seq->lastTicks = 0;
do {
lastPtr = seq->curPtr;
lastStatus = seq->lastStatus;
lastTicks = seq->lastTicks;
n_alSeqNextEvent(seq, &evt);
if (evt.type == AL_SEQ_END_EVT)
{
lastPtr = seq->curPtr;
lastStatus = seq->lastStatus;
lastTicks = seq->lastTicks;
break;
}
} while (seq->lastTicks < ticks);
m->curPtr = lastPtr;
m->lastStatus = lastStatus;
m->lastTicks = lastTicks;
m->curTicks = seq->lastTicks; /* Used by test loop condition. */
seq->curPtr = savePtr;
seq->lastStatus = saveStatus;
seq->lastTicks = saveTicks;
}
}
/* non-aligned byte reading routines */
static u8 read8(ALSeq *seq)
{
return *seq->curPtr++;
}
static s16 read16(ALSeq *seq)
{
s16 tmp;
tmp = *seq->curPtr++ << 8;
tmp |= *seq->curPtr++;
return tmp;
}
static s32 read32(ALSeq *seq)
{
s32 tmp;
tmp = *seq->curPtr++ << 24;
tmp |= *seq->curPtr++ << 16;
tmp |= *seq->curPtr++ << 8;
tmp |= *seq->curPtr++;
return tmp;
}
static s32 readVarLen(ALSeq *seq)
{
s32 value;
s32 c;
c = *seq->curPtr++;
value = c;
if ( c & 0x80 ) {
value &= 0x7f;
do {
c = *seq->curPtr++;
value = (value << 7) + (c & 0x7f);
} while (c & 0x80);
}
return (value);
}