seqpLoop.c
5.44 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
#include <assert.h>
#include <libaudio.h>
#include "UniversalSP.h"
#include "audiotest.h"
#include "audio.h"
#include "seqpTest.h"
#include "seqpLoop.h"
extern ALHeap gHeapRec;
extern ALBankFile * gBankFile;
extern ALUSPlayer * gSeqp;
extern ALUSeq * gSeq;
extern u8 * gSeqData;
static ALUSMarker startMkr, endMkr;
static void _StartLoopTest (ALUSPlayer *seqp, short testID);
static void _GetLoopTestSequence (void);
static u32 _qnotes2ticks (ALUSeq *seq, u32 qnotes);
static s32 _DivS32ByF32 (s32 i, f32 f);
void
SeqpLoop_Init (void)
{
ALSeqpConfig seqc;
PRINTF("Test loops and markers\n");
/* Setup this test's retrace proc and data. */
seqpTest.retraceProc = SeqpLoop_RetraceProc;
seqpTest.buttonProc = SeqpLoop_ButtonProc;
seqpTest.retraceData = (TRetraceData)gSeqp;
/* Get the loop test sequence. */
_GetLoopTestSequence ();
/* Setup the loop test sequence player. */
seqc.maxVoices = 8;
seqc.maxEvents = MAX_SEQP_EVENTS;
seqc.maxChannels = 16;
seqc.heap = &gHeapRec;
seqc.initOsc = 0;
seqc.updateOsc = 0;
seqc.stopOsc = 0;
#ifdef _DEBUG
seqc.debugFlags = NO_VOICE_ERR_MASK |NOTE_OFF_ERR_MASK | NO_SOUND_ERR_MASK;
#endif
alUSPNew (gSeqp, &seqc);
alUSPSetSeq (gSeqp, gSeq);
alUSPSetBank (gSeqp, gBankFile->bankArray[0]);
}
void
SeqpLoop_Fini (void)
{
alUSPDelete (gSeqp);
}
void
SeqpLoop_ButtonProc(u16 oldButtons, u16 newButtons)
{
u16 button;
button = oldButtons^newButtons; /* get the ones that have changed */
button &= newButtons; /* get only button downs */
if (button & CONT_START)
TestDone ();
}
void
SeqpLoop_RetraceProc (TRetraceData data)
{
static short sLoopTestNum = 0;
static char sCheckForEOS = TRUE;
ALUSPlayer * seqp = (ALUSPlayer *)data;
if (sCheckForEOS && alUSPGetState(seqp) == AL_STOPPED)
{
sCheckForEOS = FALSE;
_StartLoopTest (seqp, sLoopTestNum++);
}
if (!sCheckForEOS && alUSPGetState(seqp) == AL_PLAYING)
sCheckForEOS = TRUE;
}
static void
_StartLoopTest (ALUSPlayer *seqp, short testID)
{
ALUSeq * seq = alUSPGetSeq (seqp);
ALUSMarker zeroMkr;
u32 startTick;
u32 endTick;
s32 numLoops = 3;
switch (testID)
{
case 0:
startTick = 0;
endTick = 0x7fffffff;
break;
case 1:
startTick = 0;
endTick = _qnotes2ticks(seq, 3) + 1;
break;
case 2:
startTick = 0;
endTick = _qnotes2ticks(seq, 2) + 1;
break;
case 3:
startTick = 0;
endTick = _qnotes2ticks(seq, 1) + 1;
break;
case 4:
startTick = 0;
endTick = 0;
break;
case 5:
startTick = _qnotes2ticks(seq, 1) + 1;
endTick = 0x7fffffff;
break;
case 6:
startTick = _qnotes2ticks(seq, 2) + 1;
endTick = 0x7fffffff;
break;
case 7:
startTick = _qnotes2ticks(seq, 3) + 1;
endTick = 0x7fffffff;
break;
case 8:
startTick = _qnotes2ticks(seq, 1) + 1;
endTick = _qnotes2ticks(seq, 3) + 1;
break;
case 9:
startTick = _qnotes2ticks(seq, 3) + 1;
endTick = _qnotes2ticks(seq, 1) + 1;
break;
case 10:
startTick = _qnotes2ticks(seq, 1) + 1;
endTick = _qnotes2ticks(seq, 3) + 1;
numLoops = 16;
break;
case 11:
startTick = 0;
endTick = 0x7fffffff;
numLoops = -1;
break;
default:
PRINTF("\tdone\n");
return;
}
alUSNewMarker (seq, &zeroMkr, 0);
alUSNewMarker (seq, &startMkr, startTick);
alUSNewMarker (seq, &endMkr, endTick);
alUSSetLoc (seq, &zeroMkr);
alSeqpLoop (seqp, &startMkr, &endMkr, numLoops);
PRINTF("\tloop %d times from %d to %d ticks, mkrs [%d, %d)\n",
numLoops, startTick, endTick, startMkr.lastTicks, endMkr.curTicks);
alUSPPlay (seqp);
}
static u32
_qnotes2ticks (ALUSeq *seq, u32 qnotes)
{
return _DivS32ByF32 (qnotes, seq->qnpt);
}
/*
This routine safely divides a signed 32-bit integer
by a floating point value. It avoids overflow by using
a double to store the result and then before truncating
to an integer it compares the result to the limit and
limits it on overflow. Underflow is handled automatically
by the CPU which limits the value to zero.
Presently this routine is used to divide a time in usecs
by a pitch ratio. Since the time could be a very large number,
very small pitch ratios can cause the reult to overflow,
causing a floating point exception.
*/
static
s32 _DivS32ByF32 (s32 i, f32 f)
{
#define INT_MAX 2147483647 /* Should be in a limits.h file. */
f64 rd;
int ri;
assert(f!=0); /* Caller must make sure we do not divide by zero! */
rd = i/f; /* Store result as a double to avoid overflow. */
if (rd > INT_MAX) /* Limit the value if necessary. */
ri = INT_MAX;
else
ri = rd;
return ri;
}
static void
_GetLoopTestSequence (void)
{
#define LOOP_SEQ_NUM 0
ALSeqFile * testSeqFile;
u8 * romData;
s32 len;
/**** Load the sequence file header and data from ROM ****/
testSeqFile = alHeapAlloc(&gHeapRec, 1, 4);
romCopy(_testseqSegmentRomStart, (u8 *)testSeqFile, 4);
len = 4 + testSeqFile->seqCount*sizeof(ALSeqData);
testSeqFile = alHeapAlloc(&gHeapRec, 1, len);
romCopy(_testseqSegmentRomStart, (u8 *)testSeqFile, len);
alSeqFileNew(testSeqFile, (u8 *) _testseqSegmentRomStart);
romData = testSeqFile->seqArray[LOOP_SEQ_NUM].offset;
len = testSeqFile->seqArray[LOOP_SEQ_NUM].len;
if (len & 0x1)
len++;
romCopy (romData, gSeqData, len);
alUSNew (gSeq, gSeqData, len);
}