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
|
#include <dolphin/ai.h>
#include <dolphin/dtk.h>
static DVDCommandBlock __block_for_run_callback;
static DVDCommandBlock __block_for_prep_callback;
static DVDCommandBlock __block_for_stream_status;
static DVDCommandBlock __block_for_ais_isr;
static DVDCommandBlock __block_for_flushtracks;
static DVDCommandBlock __block_for_set_state;
static DVDCommandBlock __block_for_next_track;
static DTKTrack* __DTKCurrentTrack;
static DTKTrack* __DTKPlayListHead;
static DTKTrack* __DTKPlayListTail;
static vu32 __DTKState;
static vu32 __DTKTempState;
static vu32 __DTKRepeatMode;
static vu32 __DTKPosition;
static vu32 __DTKInterruptFrequency;
static vu8 __DTKVolumeL;
static vu8 __DTKVolumeR;
static volatile u32 __DTKShutdownFlag;
static volatile u32 __DTKTrackEnded;
static DTKFlushCallback __DTKFlushCallback;
static void __DTKStartAi() {
AISetStreamVolLeft(__DTKVolumeL);
AISetStreamVolRight(__DTKVolumeR);
AIResetStreamSampleCount();
AISetStreamTrigger(__DTKInterruptFrequency);
AISetStreamPlayState(1);
}
static void __DTKStopAi() {
AISetStreamVolLeft(0);
AISetStreamVolRight(0);
AISetStreamPlayState(0);
}
static void __DTKCheckUserCallback(DTKTrack* track, u32 event) {
if (track == NULL) {
return;
}
if (track->callback != NULL && (track->eventMask & event) != 0) {
track->callback(track->eventMask & event);
}
}
static void __DTKForward() {
BOOL enabled;
enabled = OSDisableInterrupts();
if (__DTKCurrentTrack != NULL && __DTKCurrentTrack->next != nullptr) {
__DTKCurrentTrack = __DTKCurrentTrack->next;
}
OSRestoreInterrupts(enabled);
}
static void __DTKBackward() {
BOOL enabled;
enabled = OSDisableInterrupts();
if (__DTKCurrentTrack != NULL && __DTKCurrentTrack->prev != nullptr) {
__DTKCurrentTrack = __DTKCurrentTrack->prev;
}
OSRestoreInterrupts(enabled);
}
static void __DTKCallbackForStreamStatus(s32 result, DVDCommandBlock* block) {
if ((u8)result) {
return;
}
__DTKTrackEnded = TRUE;
__DTKPosition = 0;
}
static void __DTKCallbackForRun(s32 result, DVDFileInfo* fileInfo) {
__DTKStartAi();
DVDStopStreamAtEndAsync(&__block_for_run_callback, NULL);
__DTKState = 1;
__DTKCheckUserCallback(__DTKCurrentTrack, 1);
}
static void __DTKCallbackForPreparePaused(s32 result, DVDFileInfo* fileInfo) {
__DTKStopAi();
DVDStopStreamAtEndAsync(&__block_for_prep_callback, NULL);
__DTKState = 2;
__DTKCheckUserCallback(__DTKCurrentTrack, 32);
}
static void __DTKPrepareCurrentTrack(DVDCallback callback) {
DVDPrepareStreamAsync(&__DTKCurrentTrack->dvdFileInfo, 0, 0, callback);
}
static void __DTKCallbackForPlaylist(s32 result, DVDCommandBlock* block) {
__DTKPosition = result;
if (__DTKTrackEnded != FALSE) {
__DTKTrackEnded = FALSE;
__DTKCheckUserCallback(__DTKCurrentTrack, 16);
__DTKState = 3;
switch (__DTKRepeatMode) {
case 0:
if (__DTKCurrentTrack == NULL) {
break;
}
if (__DTKCurrentTrack->next != NULL) {
__DTKCurrentTrack = __DTKCurrentTrack->next;
__DTKStopAi();
__DTKPrepareCurrentTrack(__DTKCallbackForRun);
} else {
__DTKCurrentTrack = __DTKPlayListHead;
__DTKStopAi();
__DTKState = 0;
}
break;
case 1: {
if (__DTKCurrentTrack == NULL) {
break;
}
if (__DTKCurrentTrack->next != NULL) {
__DTKCurrentTrack = __DTKCurrentTrack->next;
__DTKStopAi();
__DTKPrepareCurrentTrack(__DTKCallbackForRun);
} else {
__DTKCurrentTrack = __DTKPlayListHead;
__DTKStopAi();
__DTKPrepareCurrentTrack(__DTKCallbackForRun);
}
break;
}
case 2: {
if (__DTKCurrentTrack == NULL) {
break;
}
__DTKStopAi();
__DTKPrepareCurrentTrack(__DTKCallbackForRun);
break;
}
}
return;
}
DVDGetStreamErrorStatusAsync(&__block_for_stream_status, __DTKCallbackForStreamStatus);
}
static void __DTKCallbackForAIInterrupt(u32 result) {
AISetStreamTrigger(result + __DTKInterruptFrequency);
if (__DTKCurrentTrack != NULL) {
DVDGetStreamPlayAddrAsync(&__block_for_ais_isr, __DTKCallbackForPlaylist);
}
}
static void __DTKCallbackForFlush(s32 result, DVDCommandBlock* block) {
DTKTrack* iter;
AISetStreamPlayState(0);
for (iter = __DTKPlayListHead; iter != NULL; iter = iter->next) {
DVDClose(&iter->dvdFileInfo);
}
__DTKPlayListHead = NULL;
__DTKPlayListTail = NULL;
__DTKCurrentTrack = NULL;
__DTKState = 0;
if (__DTKFlushCallback != NULL) {
__DTKFlushCallback();
__DTKFlushCallback = NULL;
}
__DTKState = 0;
__DTKShutdownFlag = 0;
}
static void __DTKCallbackForStop() {
__DTKCheckUserCallback(__DTKCurrentTrack, 2);
__DTKState = 0;
}
static void __DTKCallbackForNextTrack() {
AISetStreamPlayState(0);
__DTKForward();
__DTKState = 0;
DTKSetState(__DTKTempState);
}
static void __DTKCallbackForPrevTrack() {
AISetStreamPlayState(0);
__DTKBackward();
__DTKState = 0;
DTKSetState(__DTKTempState);
}
void DTKInit() {
__DTKCurrentTrack = NULL;
__DTKPlayListHead = NULL;
__DTKPlayListTail = NULL;
__DTKState = 0;
__DTKRepeatMode = 0;
__DTKPosition = 0;
__DTKInterruptFrequency = 48000;
__DTKVolumeL = 255;
__DTKVolumeR = 255;
AISetStreamVolLeft(0);
AISetStreamVolRight(0);
AIRegisterStreamCallback(__DTKCallbackForAIInterrupt);
AIResetStreamSampleCount();
AISetStreamPlayState(0);
}
u32 DTKQueueTrack(char* fileName, DTKTrack* track, u32 eventMask, DTKCallback callback) {
BOOL enabled;
u32 prepareTrack = FALSE;
if (DVDOpen(fileName, &track->dvdFileInfo) == 0) {
return 1;
}
enabled = OSDisableInterrupts();
track->fileName = fileName;
track->eventMask = eventMask;
track->callback = callback;
if (__DTKPlayListHead == NULL) {
__DTKPlayListHead = track;
__DTKPlayListTail = track;
track->prev = NULL;
track->next = NULL;
if (__DTKState == 1) {
prepareTrack = TRUE;
}
} else {
__DTKPlayListTail->next = track;
track->prev = __DTKPlayListTail;
__DTKPlayListTail = track;
track->next = NULL;
}
if (__DTKCurrentTrack == NULL) {
__DTKCurrentTrack = track;
}
OSRestoreInterrupts(enabled);
__DTKCheckUserCallback(track, 8);
if (prepareTrack != 0) {
__DTKState = 3;
__DTKPrepareCurrentTrack(__DTKCallbackForRun);
}
return 0;
}
void DTKFlushTracks(DTKFlushCallback callback) {
u32 oldState;
if (__DTKState != 3) {
oldState = __DTKState;
__DTKState = 3;
__DTKFlushCallback = callback;
if (oldState == 1) {
DVDCancelStreamAsync(&__block_for_flushtracks, __DTKCallbackForFlush);
} else {
__DTKCallbackForFlush(0, NULL);
}
}
}
void DTKSetSampleRate(u32 rate) {}
void DTKSetRepeatMode(u32 repeat) { __DTKRepeatMode = repeat; }
void DTKSetState(u32 state) {
if (__DTKState == state || __DTKState == 3) {
return;
}
switch (state) {
case 0: {
if (__DTKCurrentTrack == NULL) {
break;
}
__DTKState = 3;
AISetStreamVolLeft(0);
AISetStreamVolRight(0);
AISetStreamPlayState(0);
DVDCancelStreamAsync(&__block_for_set_state, __DTKCallbackForStop);
break;
}
case 1: {
if (__DTKState == 2) {
__DTKStartAi();
__DTKState = 1;
if (__DTKCurrentTrack != NULL) {
__DTKCheckUserCallback(__DTKCurrentTrack, 1);
}
} else if (__DTKCurrentTrack != NULL) {
__DTKState = 3;
__DTKPrepareCurrentTrack(__DTKCallbackForRun);
} else {
__DTKState = 1;
}
__DTKTrackEnded = 0;
break;
}
case 4: {
if (__DTKState != 0) {
break;
}
if (__DTKCurrentTrack != NULL) {
__DTKState = 3;
__DTKPrepareCurrentTrack(__DTKCallbackForPreparePaused);
}
__DTKTrackEnded = FALSE;
break;
}
case 2: {
AISetStreamPlayState(0);
if (__DTKState == 1) {
__DTKState = 2;
}
__DTKCheckUserCallback(__DTKCurrentTrack, 4);
break;
}
}
}
void DTKNextTrack() {
if (__DTKState != 3 && __DTKCurrentTrack != NULL) {
__DTKTempState = __DTKState;
__DTKState = 3;
if (__DTKTempState == 1) {
AISetStreamVolLeft(0);
AISetStreamVolRight(0);
DVDCancelStreamAsync(&__block_for_next_track, __DTKCallbackForNextTrack);
} else {
__DTKForward();
__DTKState = __DTKTempState;
}
}
}
u32 DTKGetState() { return __DTKState; }
void DTKSetVolume(u8 left, u8 right) {
__DTKVolumeL = left;
__DTKVolumeR = right;
if (__DTKState == 1) {
AISetStreamVolLeft(left);
AISetStreamVolRight(right);
}
}
|