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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
|
/*
* LibPSn00b Example Programs
*
* CD-XA Audio Example
* 2019 - 2021 Meido-Tek Productions / PSn00bSDK Project
*
* Demonstrates playback and looping of CD-XA audio using the
* new libpsxcd library.
*
*
* Since there is not yet an open source XA audio encoder yet, the
* only way to create XA audio data is by using the official tools.
* You will have to provide your own XA file to get this example
* to work.
*
* You will also need MKPSXISO (https://github.com/lameguy64/mkpsxiso)
* to build an ISO image with your XA audio data.
*
*
* Theory of operation:
*
* CD-XA playback is accomplished by first locating the XA file in
* the ISO9660 file system using CdSearchFile(). CD mode is then set
* using the CdlSetmode command with CdlModeSpeed, CdlModeRT and
* CdlModeSF flags to configure the CD controller for XA audio
* streaming. Trying to play XA audio without CdlModeSF, which
* enables filtering, will feed all XA channels to the SPU resulting
* in a stuttery cacophony of sounds.
*
* The XA channel for playback is selected using the CdlSetfilter
* command and CdlFILTER struct. XA audio is usually comprised of
* multiple audio streams interleaved together and this command
* sets which channel to filter in from all the other channels.
* CdlModeSF enables the filtering feature and is required when
* playing back XA audio streams with multiple interleaved sound
* channels.
*
* Playback is initiated by issuing CdlReadS with the XA file's
* location specified as a parameter, which is internally issued
* to the CD controller as the seek target before CdlReadS is
* actually issued.
*
* Playback can be stopped by simply issuing CdlPause during playback.
* It is not recommended to use CdlStop as it will stop the disc spinning
* and result to much slower response once the disc has stopped.
*
* The most effective method of determining the end of an XA stream is
* by hooking a callback routine with CdReadyCallback(), which is
* triggered whenever a data sector has been read and checking the
* header of the received sector if it belongs to the channel currently
* being played.
*
*
* Tips:
*
* - For best efficiency, it is recommended to have all XA tracks in
* a single XA file have roughly the same length to one another,
* otherwise tracks that end short will be padded with empty sectors
* which wastes potentially usable disc space.
*
* - The CD filter can be changed during playback and the switchover is
* completely seamless. Use this trick to accomplish dynamic music
* effects that change seamlessly based on events during gameplay.
*
* - With custom tools, it is possible to interleave data sectors with
* a single XA audio stream which would permit music playback during
* loading sessions (aside from using sequenced music). Alternatively,
* CdlReadN can be used to begin playback but with more reliable data
* reading. This is also how streaming FMV sequences are accomplished.
*
*
* Pros over CD audio:
*
* - Does not require changing disc speed when switching between data
* access and audio playback.
*
* - Permits switching between streams during playback without restart.
*
* - Near 1:4 audio compression ratio.
*
* - Data sectors can be interleaved alongside XA audio.
*
*
* Cons compared to CD audio:
*
* - Skips more often on a poor condition disc or optical pick-up.
*
* - Cannot be played with any CD player.
*
* - Lower audio sample rate (37.8KHz whereas CD audio is 44.1KHz, not
* good for audiophools).
*
*
* Controls:
*
* Up/Down - Select channel.
* Cross - Play selected channel.
* Circle - Stop selected channel.
* Right - Switch channel (without restarting playback).
*
*
* Example by Lameguy64
* (TODO: clean up/rewrite this example)
*
* Changelog:
*
* May 10, 2021 - Variable types updated for psxgpu.h changes.
*
* November 22, 2019 - Initial version
*
*/
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <psxetc.h>
#include <psxgte.h>
#include <psxgpu.h>
#include <psxapi.h>
#include <psxpad.h>
#include <psxsio.h>
#include <psxspu.h>
#include <psxcd.h>
#define MAX_BALLS 1536 /* Number of balls to display */
#define OT_LEN 8 /* Ordering table length */
/* Screen coordinates */
#define SCREEN_XRES 320
#define SCREEN_YRES 240
#define CENTER_X SCREEN_XRES/2
#define CENTER_Y SCREEN_YRES/2
/* Display and drawing environments */
DISPENV disp[2];
DRAWENV draw[2];
char pribuff[2][65536]; /* Primitive packet buffers */
uint32_t ot[2][OT_LEN]; /* Ordering tables */
char *nextpri; /* Pointer to next packet buffer offset */
int db = 0; /* Double buffer index */
/* Ball struct and array */
typedef struct BALL_TYPE
{
short x,y;
short xdir,ydir;
unsigned char r,g,b,p;
} BALL_TYPE;
BALL_TYPE balls[MAX_BALLS];
/* Ball texture reference */
extern const uint32_t ball16c[];
/* TIM image parameters for loading the ball texture and drawing sprites */
TIM_IMAGE tim;
/* XA audio handling stuff */
volatile int num_loops=0; /* Loop counter */
volatile int xa_play_channel; /* Currently playing channel number */
CdlLOC xa_loc; /* XA data start location */
/* Pad input buffer*/
char padbuff[2][34];
// https://problemkaputt.de/psx-spx.htm#cdromxasubheaderfilechannelinterleave
typedef struct {
uint8_t file, channel;
uint8_t submode, coding_info;
} XA_Header;
typedef enum {
XA_END_OF_RECORD = 1 << 0,
XA_TYPE_VIDEO = 1 << 1,
XA_TYPE_AUDIO = 1 << 2,
XA_TYPE_DATA = 1 << 3,
XA_TRIGGER = 1 << 4,
XA_FORM2 = 1 << 5,
XA_REAL_TIME = 1 << 6,
XA_END_OF_FILE = 1 << 7
} XA_SubmodeFlag;
// https://problemkaputt.de/psx-spx.htm#cdromsectorencoding
typedef struct {
CdlLOC pos;
XA_Header xa_header[2];
uint8_t data[2048];
uint32_t edc;
uint8_t ecc[276];
} Sector;
// This buffer is used by cd_event_handler() as a temporary area for sectors
// read from the CD. Due to DMA limitations it can't be allocated on the stack
// (especially not in the interrupt callbacks' stack, whose size is very
// limited).
Sector sector;
void cd_event_handler(CdlIntrResult event, uint8_t *payload) {
// Ignore all events other than a sector being ready.
if (event != CdlDataReady)
return;
// Fetch the sector and check if it is part of the audio file by checking
// its XA flags. If it isn't an audio sector, restart playback.
CdGetSector(§or, sizeof(Sector) / 4);
if (
!(sector.xa_header[0].submode & XA_TYPE_AUDIO) &&
!(sector.xa_header[1].submode & XA_TYPE_AUDIO)
) {
// Seek back to the beginning of the file.
CdControlF(CdlReadS, &xa_loc);
num_loops++;
}
}
void init()
{
int i;
/* Uncomment to direct tty messages to serial */
//AddSIO(115200);
/* Reset GPU (also installs event handler for VSync) */
printf("Init GPU... ");
ResetGraph( 0 );
printf("Done.\n");
/* Initialize SPU and CD-ROM */
printf("Initializing CD-ROM... ");
SpuInit();
CdInit();
printf("Done.\n");
/* Set display and draw environment parameters */
SetDefDispEnv(&disp[0], 0, 0, SCREEN_XRES, SCREEN_YRES);
SetDefDispEnv(&disp[1], 0, SCREEN_YRES, SCREEN_XRES, SCREEN_YRES);
SetDefDrawEnv(&draw[0], 0, SCREEN_YRES, SCREEN_XRES, SCREEN_YRES);
SetDefDrawEnv(&draw[1], 0, 0, SCREEN_XRES, SCREEN_YRES);
/* Set clear color, area clear and dither processing */
setRGB0(&draw[0], 63, 0, 127);
draw[0].isbg = 1;
draw[0].dtd = 1;
setRGB0(&draw[1], 63, 0, 127);
draw[1].isbg = 1;
draw[1].dtd = 1;
/* Load and open font stream */
FntLoad(960, 0);
FntOpen(32, 32, 256, 176, 2, 200);
/* Upload the ball texture */
GetTimInfo(ball16c, &tim); /* Get TIM parameters */
LoadImage(tim.prect, tim.paddr); /* Upload texture to VRAM */
if( tim.mode & 0x8 )
{
LoadImage(tim.crect, tim.caddr); /* Upload CLUT if present */
}
/* Calculate ball positions */
printf("Calculating balls... ");
for(i=0; i<MAX_BALLS; i++)
{
balls[i].x = (rand()%304);
balls[i].y = (rand()%224);
balls[i].xdir = 1-(rand()%3);
balls[i].ydir = 1-(rand()%3);
if( !balls[i].xdir )
balls[i].xdir = 1;
if( !balls[i].ydir )
balls[i].ydir = 1;
balls[i].xdir *= 2;
balls[i].ydir *= 2;
balls[i].r = (rand()%256);
balls[i].g = (rand()%256);
balls[i].b = (rand()%256);
}
printf("Done.\n");
/* Initialize pad */
InitPAD(padbuff[0], 34, padbuff[1], 34);
StartPAD();
ChangeClearPAD(0);
}
int main(int argc, const char* argv[])
{
SPRT *spr;
SPRT_16 *sprt;
DR_TPAGE *tpri;
PADTYPE *pad;
CdlFILE file;
CdlFILTER filter;
int i,counter=0;
int sel_channel=0;
int p_up=0,p_down=0,p_right=0,p_cross=0,p_circle=0;
/* Init graphics and stuff before doing anything else */
init();
/* Locate the XA file */
if( !CdSearchFile(&file, "\\XASAMPLE.XA") )
{
printf("Unable to find file.\n");
return 0;
}
else
{
int sec;
sec = CdPosToInt(&file.pos);
printf("XA located at sector %d size %d.\n", sec, file.size);
}
/* Save file location as XA location */
xa_loc = file.pos;
/* Hook XA callback function to CdReadyCallback (for auto stop/loop */
EnterCriticalSection();
CdReadyCallback(&cd_event_handler);
ExitCriticalSection();
/* Set CD mode for XA streaming (2x speed, send XA to SPU, enable filter */
i = CdlModeSpeed|CdlModeRT|CdlModeSF;
CdControl(CdlSetmode, &i, 0);
/* Set file 1 on filter for channels 0-31 */
filter.file = 1;
/* Main loop */
printf("Entering loop...\n");
while(1) {
pad = ((PADTYPE*)padbuff[0]);
if( pad->stat == 0 )
{
if(( pad->type == 0x4 )||( pad->type == 0x5 )||( pad->type == 0x7 ))
{
/* Menu selection controls */
if( !(pad->btn&PAD_UP) )
{
if( !p_up )
{
if( sel_channel > 0 )
{
sel_channel--;
}
p_up = 1;
}
}
else
{
p_up = 0;
}
if( !(pad->btn&PAD_DOWN) )
{
if( !p_down )
{
if( sel_channel < 31 )
{
sel_channel++;
}
p_down = 1;
}
}
else
{
p_down = 0;
}
/* Play selected XA channel from start */
if( !(pad->btn&PAD_CROSS) )
{
if( !p_cross )
{
filter.chan = sel_channel;
CdControl(CdlSetfilter, &filter, 0);
CdControl(CdlReadS, &xa_loc, 0);
xa_play_channel = sel_channel;
p_cross = 1;
}
}
else
{
p_cross = 0;
}
/* Stop playback */
if( !(pad->btn&PAD_CIRCLE) )
{
if( !p_circle )
{
CdControl(CdlPause, 0, 0);
p_circle = 1;
}
}
else
{
p_circle = 0;
}
/* Change XA channel */
if( !(pad->btn&PAD_RIGHT) )
{
if( !p_right )
{
filter.chan = sel_channel;
CdControl(CdlSetfilter, &filter, 0);
xa_play_channel = sel_channel;
p_right = 1;
}
}
else
{
p_right = 0;
}
}
}
/* Display information */
FntPrint(-1, "\n PSN00BSDK XA AUDIO EXAMPLE\n\n");
FntPrint(-1, " CHANNEL:\n");
for(i=0; i<8; i++)
{
if( i == sel_channel )
{
FntPrint(-1, " -->%d\n", i);
}
else
{
FntPrint(-1, " %d\n", i);
}
}
FntPrint(-1, "\n CURRENT=%d STATUS=%x LOOPS=%d\n",
xa_play_channel, CdStatus(), num_loops);
FntPrint(-1, "\n <X>-PLAY (START) <O>-STOP\n <R>-SET CHANNEL\n");
/* Clear ordering table and set start address of primitive buffer */
ClearOTagR(ot[db], OT_LEN);
nextpri = pribuff[db];
/* Sort the balls */
sprt = (SPRT_16*)nextpri;
for( i=0; i<MAX_BALLS; i++ ) {
setSprt16(sprt);
setXY0(sprt, balls[i].x, balls[i].y);
setRGB0(sprt, balls[i].r, balls[i].g, balls[i].b);
setUV0(sprt, 0, 0);
setClut(sprt, tim.crect->x, tim.crect->y);
addPrim(ot[db]+(OT_LEN-1), sprt);
sprt++;
balls[i].x += balls[i].xdir;
balls[i].y += balls[i].ydir;
if( ( balls[i].x+16 ) > SCREEN_XRES ) {
balls[i].xdir = -2;
} else if( balls[i].x < 0 ) {
balls[i].xdir = 2;
}
if( ( balls[i].y+16 ) > SCREEN_YRES ) {
balls[i].ydir = -2;
} else if( balls[i].y < 0 ) {
balls[i].ydir = 2;
}
}
nextpri = (char*)sprt;
/* Sort a TPage primitive so the sprites will draw pixels from */
/* the correct texture page in VRAM */
tpri = (DR_TPAGE*)nextpri;
setDrawTPage(tpri, 0, 0, getTPage(0, 0, tim.prect->x, tim.prect->y));
addPrim(ot[db]+(OT_LEN-1), tpri);
nextpri += sizeof(DR_TPAGE);
/* Draw font */
FntFlush(-1);
/* Wait for GPU and VSync */
DrawSync(0);
VSync(0);
/* Since draw.isbg is non-zero this clears the screen */
PutDispEnv(&disp[db]);
PutDrawEnv(&draw[db]);
SetDispMask(1);
/* Begin drawing the new frame */
DrawOTag( ot[db]+(OT_LEN-1) );
/* Alternate to the next buffer */
db = !db;
/* Periodically issue CdlNop every second to update CdStatus() */
counter++;
if( (counter%60) == 59 )
{
CdControl(CdlNop, 0, 0);
}
}
return 0;
}
|