Speed up BGM playback by storing XA locations on startup

This commit is contained in:
Lucas S. Vieira 2024-12-24 19:45:14 -03:00
parent ef1c154df0
commit b99c30ef04
4 changed files with 50 additions and 14 deletions

View file

@ -65,8 +65,11 @@ uint32_t sound_upload_vag(const uint32_t *data, uint32_t size);
void sound_play_vag(SoundEffect sfx, uint8_t loops);
uint32_t sound_get_cd_status(void);
CdlLOC sound_find_xa(const char *filename);
void sound_play_xa(const char *filename, int double_speed,
uint8_t channel, uint32_t loopback_sector);
void sound_play_xa_immediate(CdlLOC *loc, int double_speed,
uint8_t channel, uint32_t loopback_sector);
void sound_stop_xa(void);
int sound_xa_requested_play();
void sound_xa_set_channel(uint8_t channel);
@ -102,6 +105,7 @@ typedef struct {
uint16_t stop_sector;
} BGMTableEntry;
void sound_bgm_init();
void sound_bgm_play(BGMOption);
const BGMTableEntry *sound_bgm_get_data(BGMOption);
void sound_bgm_check_stop(BGMOption);

View file

@ -56,8 +56,8 @@ main(void)
{
// Engine initialization
setup_context();
sound_init();
CdInit();
sound_init();
pad_init();
timer_init();
fastalloc_init();

View file

@ -65,6 +65,8 @@ sound_init(void)
sound_reset_mem();
_sound_reset_channels();
sound_xa_set_volume(XA_DEFAULT_VOLUME);
sound_bgm_init();
}
void
@ -163,28 +165,47 @@ sound_play_vag(SoundEffect sfx, uint8_t loops)
void _xacd_event_callback(CdlIntrResult, uint8_t *);
CdlLOC
sound_find_xa(const char *filename)
{
CdlFILE file = { 0 };
if(!CdSearchFile(&file, filename)) {
printf("WARNING: Could not find .XA file %s.\n", filename);
return (CdlLOC){ 0 };
}
return file.pos;
}
void
sound_play_xa(const char *filename, int double_speed,
uint8_t channel, uint32_t loopback_sector)
{
CdlFILE file;
sound_stop_xa();
CdlLOC loc = sound_find_xa(filename);
/* int sectorn = CdPosToInt(&loc); */
/* int numsectors = (file.size + 2047) / 2048; */
/* printf("%s: Sector %d (size: %d => %d sectors).\n", */
/* filename, sectorn, file.size, numsectors); */
sound_play_xa_immediate(&loc, double_speed, channel, loopback_sector);
}
void
sound_play_xa_immediate(
CdlLOC *loc, int double_speed,
uint8_t channel, uint32_t loopback_sector)
{
CdlFILTER filter;
// Stop sound if playing. We'll need the CD right now
sound_stop_xa();
if(!CdSearchFile(&file, filename)) {
printf("Could not find .XA file %s.\n", filename);
return;
}
int sectorn = CdPosToInt(&file.pos);
int numsectors = (file.size + 2047) / 2048;
printf("%s: Sector %d (size: %d => %d sectors).\n",
filename, sectorn, file.size, numsectors);
/* int sectorn = CdPosToInt(&file.pos); */
/* int numsectors = (file.size + 2047) / 2048; */
/* printf("%s: Sector %d (size: %d => %d sectors).\n", */
/* filename, sectorn, file.size, numsectors); */
// Save current file location
_xa_loc = file.pos;
_xa_loc = *loc;
// Hook .XA callback for auto stop/loop
cd_set_callbacks(PLAYBACK_XA);

View file

@ -24,11 +24,22 @@ const BGMTableEntry bgm_table[] = {
{"\\BGM\\BGM003.XA;1", 1, 2830, 0}, // EZ
};
static CdlLOC bgm_table_locs[BGM_NUM_SONGS];
void
sound_bgm_init()
{
// Find location of all songs on bgm table and store them
for(int i = 0; i < BGM_NUM_SONGS; i++) {
bgm_table_locs[i] = sound_find_xa(bgm_table[i].filename);
}
}
void
sound_bgm_play(BGMOption t)
{
sound_play_xa(
bgm_table[t].filename,
sound_play_xa_immediate(
&bgm_table_locs[t],
0,
bgm_table[t].channel,
bgm_table[t].loopback_sector);