Multiple Sound Support (#377)

* Initial support for the game playing multiple sounds at once.

* Make better use of our channels and use the remaining channels.
This commit is contained in:
Prince Frizzy 2023-05-03 03:51:27 -04:00 committed by GitHub
parent 0d42098b4e
commit 677f4591dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 190 additions and 79 deletions

View File

@ -24,6 +24,12 @@
.byte (\b - sequence_start) >> 8, (\b - sequence_start) & 0xff
.endm
.macro seq_startchannel_extended a, b
.byte 0xb0
.byte \a
.byte (\b - sequence_start) >> 8, (\b - sequence_start) & 0xff
.endm
.macro seq_subtract a
.byte 0xc8
.byte \a
@ -80,12 +86,12 @@
.macro seq_disablechannels_extended a
.byte 0xc0
.byte \a >> 24, \a >> 16, \a >> 8, \a & 0xff
.byte (\a >> 56) & 0xff, (\a >> 48) & 0xff, (\a >> 40) & 0xff, (\a >> 32) & 0xff, (\a >> 24) & 0xff, (\a >> 16) & 0xff, (\a >> 8) & 0xff, \a & 0xff
.endm
.macro seq_initchannels_extended a
.byte 0xc1
.byte \a >> 24, \a >> 16, \a >> 8, \a & 0xff
.byte (\a >> 56) & 0xff, (\a >> 48) & 0xff, (\a >> 40) & 0xff, (\a >> 32) & 0xff, (\a >> 24) & 0xff, (\a >> 16) & 0xff, (\a >> 8) & 0xff, \a & 0xff
.endm
.macro seq_changevol a

View File

@ -18,20 +18,96 @@ seq_setmutescale 0
seq_setvol 127
.endif
seq_settempo 120
#seq_initchannels 0xfff
seq_initchannels_extended 0xfff
seq_startchannel 0, .channel0
seq_startchannel 1, .channel1
seq_startchannel 2, .channel2
seq_startchannel 3, .channel38
seq_startchannel 4, .channel4
seq_startchannel 5, .channel59
seq_startchannel 6, .channel6
seq_startchannel 7, .channel7
seq_startchannel 8, .channel38
seq_startchannel 9, .channel59
seq_startchannel 10, .channel10
seq_startchannel 11, .channel11
seq_initchannels_extended 0xffffffffffffffff
# SOUND_BANK_ACTION
seq_startchannel_extended 0, .channel0
seq_startchannel_extended 1, .channel0
seq_startchannel_extended 2, .channel0
seq_startchannel_extended 3, .channel0
seq_startchannel_extended 4, .channel0
seq_startchannel_extended 5, .channel0
seq_startchannel_extended 6, .channel0
seq_startchannel_extended 7, .channel0
# SOUND_BANK_MOVING
seq_startchannel_extended 8, .channel1
seq_startchannel_extended 9, .channel1
seq_startchannel_extended 10, .channel1
seq_startchannel_extended 11, .channel1
seq_startchannel_extended 12, .channel1
seq_startchannel_extended 13, .channel1
seq_startchannel_extended 14, .channel1
seq_startchannel_extended 15, .channel1
# SOUND_BANK_MARIO_VOICE
seq_startchannel_extended 16, .channel2
seq_startchannel_extended 17, .channel2
seq_startchannel_extended 18, .channel2
seq_startchannel_extended 19, .channel2
seq_startchannel_extended 20, .channel2
seq_startchannel_extended 21, .channel2
seq_startchannel_extended 22, .channel2
seq_startchannel_extended 23, .channel2
# SOUND_BANK_GENERAL
seq_startchannel_extended 24, .channel38
seq_startchannel_extended 25, .channel38
seq_startchannel_extended 26, .channel38
seq_startchannel_extended 27, .channel38
# SOUND_BANK_ENV
seq_startchannel_extended 28, .channel4
seq_startchannel_extended 29, .channel4
seq_startchannel_extended 30, .channel4
seq_startchannel_extended 31, .channel4
# SOUND_BANK_OBJ
seq_startchannel_extended 32, .channel59
seq_startchannel_extended 33, .channel59
seq_startchannel_extended 34, .channel59
seq_startchannel_extended 35, .channel59
# SOUND_BANK_AIR
seq_startchannel_extended 36, .channel6
seq_startchannel_extended 37, .channel6
seq_startchannel_extended 38, .channel6
seq_startchannel_extended 39, .channel6
# SOUND_BANK_MENU
seq_startchannel_extended 40, .channel7
# SOUND_BANK_GENERAL2
seq_startchannel_extended 41, .channel38
seq_startchannel_extended 42, .channel38
seq_startchannel_extended 43, .channel38
seq_startchannel_extended 44, .channel38
# SOUND_BANK_OBJ2
seq_startchannel_extended 45, .channel59
seq_startchannel_extended 46, .channel59
seq_startchannel_extended 47, .channel59
# SOUND_BANK_LUIGI_VOICE
seq_startchannel_extended 48, .channel10
seq_startchannel_extended 49, .channel10
seq_startchannel_extended 50, .channel10
seq_startchannel_extended 51, .channel10
seq_startchannel_extended 52, .channel10
seq_startchannel_extended 53, .channel10
seq_startchannel_extended 54, .channel10
seq_startchannel_extended 55, .channel10
# SOUND_BANK_WARIO_VOICE
seq_startchannel_extended 56, .channel11
seq_startchannel_extended 57, .channel11
seq_startchannel_extended 58, .channel11
seq_startchannel_extended 59, .channel11
seq_startchannel_extended 60, .channel11
seq_startchannel_extended 61, .channel11
seq_startchannel_extended 62, .channel11
seq_startchannel_extended 63, .channel11
.seq_loop:
seq_delay 20000
seq_jump .seq_loop

View File

@ -25,7 +25,7 @@
// N.B. sound banks are different from the audio banks referred to in other
// files. We should really fix our naming to be less ambiguous...
#define MAX_BACKGROUND_MUSIC_QUEUE_SIZE 6
#define MAX_CHANNELS_PER_SOUND_BANK 1
#define MAX_CHANNELS_PER_SOUND_BANK 8
#define SOUND_INDEX_COUNT 40
#define SEQUENCE_NONE 0xFF
@ -393,9 +393,9 @@ STATIC_ASSERT(ARRAY_COUNT(sBackgroundMusicDefaultVolume) == 64,
u8 sCurrentBackgroundMusicSeqId = SEQUENCE_NONE;
u8 sMusicDynamicDelay = 0;
u8 sSoundBankUsedListBack[SOUND_BANK_COUNT] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
u8 sSoundBankFreeListFront[SOUND_BANK_COUNT] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
u8 sSoundBankFreeListFront[SOUND_BANK_COUNT] = { 8, 8, 8, 4, 4, 4, 4, 1, 4, 3, 8, 8 };
u8 sNumSoundsInBank[SOUND_BANK_COUNT] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; // only used for debugging
u8 sMaxChannelsForSoundBank[SOUND_BANK_COUNT] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
u8 sMaxChannelsForSoundBank[SOUND_BANK_COUNT] = { 8, 8, 8, 4, 4, 4, 4, 1, 4, 3, 8, 8 };
// Banks 2 and 7 both grew from 0x30 sounds to 0x40 in size in US.
#ifdef VERSION_JP
@ -452,10 +452,9 @@ s8 D_SH_80343E48_pad[0x8];
#endif
struct Sound sSoundRequests[0x100];
// Curiously, this has size 3, despite SEQUENCE_PLAYERS == 4 on EU
struct ChannelVolumeScaleFade D_80360928[3][CHANNELS_MAX];
u8 sUsedChannelsForSoundBank[SOUND_BANK_COUNT];
u8 sCurrentSound[SOUND_BANK_COUNT][MAX_CHANNELS_PER_SOUND_BANK]; // index into sSoundBanks
struct ChannelVolumeScaleFade D_80360928[SEQUENCE_PLAYERS][CHANNELS_MAX] = { 0 };
u8 sUsedChannelsForSoundBank[SOUND_BANK_COUNT] = { 0 };
u8 sCurrentSound[SOUND_BANK_COUNT][MAX_CHANNELS_PER_SOUND_BANK] = { 0 }; // index into sSoundBanks
/**
* For each sound bank, a pool containing the currently active sounds for that bank.
@ -1401,7 +1400,7 @@ static void update_game_sound(void) {
for (bank = 0; bank < SOUND_BANK_COUNT; bank++) {
select_current_sounds(bank);
for (i = 0; i < MAX_CHANNELS_PER_SOUND_BANK; i++) {
for (i = 0; i < sUsedChannelsForSoundBank[bank]; i++) {
soundIndex = sCurrentSound[bank][i];
if (soundIndex < SOUND_INDEX_COUNT && sSoundBanks[bank][soundIndex].soundStatus != SOUND_STATUS_STOPPED) {
@ -1750,11 +1749,12 @@ static void update_game_sound(void) {
// Increment to the next channel that this bank owns
channelIndex++;
}
// Increment to the first channel index of the next bank
// (In practice sUsedChannelsForSoundBank[i] = sMaxChannelsForSoundBank[i] = 1, so this
// doesn't do anything)
channelIndex += sMaxChannelsForSoundBank[bank] - sUsedChannelsForSoundBank[bank];
}
}

View File

@ -7,11 +7,11 @@
#if defined(VERSION_EU) || defined(VERSION_SH)
#define SEQUENCE_PLAYERS 4
#define SEQUENCE_CHANNELS 48
#define SEQUENCE_CHANNELS 128
#define SEQUENCE_LAYERS 64
#else
#define SEQUENCE_PLAYERS 3
#define SEQUENCE_CHANNELS 32
#define SEQUENCE_CHANNELS 129
#ifdef VERSION_JP
#define SEQUENCE_LAYERS 48
#else
@ -20,7 +20,7 @@
#endif
#define LAYERS_MAX 8
#define CHANNELS_MAX 32
#define CHANNELS_MAX 64
#define NO_LAYER ((struct SequenceChannelLayer *)(-1))

View File

@ -38,9 +38,9 @@ struct Note *gNotes;
static u8 pad[4];
#endif
struct SequencePlayer gSequencePlayers[SEQUENCE_PLAYERS];
struct SequenceChannel gSequenceChannels[SEQUENCE_CHANNELS];
struct SequenceChannelLayer gSequenceLayers[SEQUENCE_LAYERS];
struct SequencePlayer gSequencePlayers[SEQUENCE_PLAYERS] = { 0 };
struct SequenceChannel gSequenceChannels[SEQUENCE_CHANNELS] = { 0 };
struct SequenceChannelLayer gSequenceLayers[SEQUENCE_LAYERS] = { 0 };
struct SequenceChannel gSequenceChannelNone;
struct AudioListItem gLayerFreeList;

View File

@ -1,6 +1,6 @@
#include <PR/ultratypes.h>
#if defined(AUDIO_DEVELOPMENT) || defined(DEVELOPMENT)
#if defined(DEVELOPMENT)
#include <stdio.h>
#include <assert.h>
#endif
@ -11,6 +11,7 @@
#include "heap.h"
#include "load.h"
#include "seqplayer.h"
#include "pc/debuglog.h"
#define PORTAMENTO_IS_SPECIAL(x) ((x).mode & 0x80)
#define PORTAMENTO_MODE(x) ((x).mode & ~0x80)
@ -188,8 +189,7 @@ void seq_channel_layer_free(struct SequenceChannel *seqChannel, s32 layerIndex)
}
void sequence_channel_disable(struct SequenceChannel *seqChannel) {
s32 i;
for (i = 0; i < LAYERS_MAX; i++) {
for (s32 i = 0; i < LAYERS_MAX; i++) {
seq_channel_layer_free(seqChannel, i);
}
@ -199,8 +199,7 @@ void sequence_channel_disable(struct SequenceChannel *seqChannel) {
}
struct SequenceChannel *allocate_sequence_channel(void) {
s32 i;
for (i = 0; i < ARRAY_COUNT(gSequenceChannels); i++) {
for (u32 i = 0; i < ARRAY_COUNT(gSequenceChannels); i++) {
if (gSequenceChannels[i].seqPlayer == NULL) {
#if defined(VERSION_EU) || defined(VERSION_SH)
return &gSequenceChannels[i];
@ -209,16 +208,15 @@ struct SequenceChannel *allocate_sequence_channel(void) {
#endif
}
}
LOG_ERROR("RAN OUT OF SEQUENCE CHANNELS FOR ALLOCATION!");
return &gSequenceChannelNone;
}
void sequence_player_init_channels(struct SequencePlayer *seqPlayer, u16 channelBits) {
struct SequenceChannel *seqChannel;
s32 i;
for (i = 0; i < CHANNELS_MAX; i++) {
for (u32 i = 0; i < CHANNELS_MAX; i++) {
if (channelBits & 1) {
seqChannel = seqPlayer->channels[i];
struct SequenceChannel *seqChannel = seqPlayer->channels[i];
if (IS_SEQUENCE_CHANNEL_VALID(seqChannel) == TRUE && seqChannel->seqPlayer == seqPlayer) {
sequence_channel_disable(seqChannel);
seqChannel->seqPlayer = NULL;
@ -246,13 +244,10 @@ void sequence_player_init_channels(struct SequencePlayer *seqPlayer, u16 channel
}
void sequence_player_disable_channels(struct SequencePlayer *seqPlayer, u16 channelBits) {
struct SequenceChannel *seqChannel;
s32 i;
eu_stubbed_printf_0("SUBTRACK DIM\n");
for (i = 0; i < CHANNELS_MAX; i++) {
for (u32 i = 0; i < CHANNELS_MAX; i++) {
if (channelBits & 1) {
seqChannel = seqPlayer->channels[i];
struct SequenceChannel *seqChannel = seqPlayer->channels[i];
if (IS_SEQUENCE_CHANNEL_VALID(seqChannel) == TRUE) {
if (seqChannel->seqPlayer == seqPlayer) {
sequence_channel_disable(seqChannel);
@ -276,27 +271,22 @@ void sequence_player_disable_channels(struct SequencePlayer *seqPlayer, u16 chan
}
}
void sequence_player_init_channels_extended(struct SequencePlayer* seqPlayer, u32 channelBits) {
struct SequenceChannel* seqChannel;
s32 i;
#ifdef AUDIO_DEVELOPMENT
printf("debug: Enabling channels (extended) with corresponding bits %X\n", channelBits);
#endif
for (i = 0; i < CHANNELS_MAX; i++) {
void sequence_player_init_channels_extended(struct SequencePlayer* seqPlayer, u64 channelBits) {
LOG_DEBUG("Enabling channels (extended) with corresponding bits %llX", channelBits);
for (u32 i = 0; i < CHANNELS_MAX; i++) {
if (channelBits & 1) {
seqChannel = seqPlayer->channels[i];
struct SequenceChannel* seqChannel = seqPlayer->channels[i];
if (IS_SEQUENCE_CHANNEL_VALID(seqChannel) == TRUE && seqChannel->seqPlayer == seqPlayer) {
sequence_channel_disable(seqChannel);
seqChannel->seqPlayer = NULL;
}
}
seqChannel = allocate_sequence_channel();
if (IS_SEQUENCE_CHANNEL_VALID(seqChannel) == FALSE) {
eu_stubbed_printf_0("Audio:Track:Warning: No Free Notetrack\n");
gAudioErrorFlags = i + 0x10000;
seqPlayer->channels[i] = seqChannel;
}
else {
} else {
sequence_channel_init(seqChannel);
seqPlayer->channels[i] = seqChannel;
seqChannel->seqPlayer = seqPlayer;
@ -305,14 +295,10 @@ void sequence_player_init_channels_extended(struct SequencePlayer* seqPlayer, u3
seqChannel->noteAllocPolicy = seqPlayer->noteAllocPolicy;
}
#ifdef AUDIO_DEVELOPMENT
printf("debug: Tried to enable channel (extended) %i with result of validity %u.\n", i, IS_SEQUENCE_CHANNEL_VALID(seqChannel));
#endif
LOG_DEBUG("Tried to enable channel (extended) %i with result of validity %u.", i, IS_SEQUENCE_CHANNEL_VALID(seqChannel));
}
#ifdef AUDIO_DEVELOPMENT
printf("debug: Checked channel (extended) %i for enable with bit %u.\n", i, channelBits & 1);
#endif
LOG_DEBUG("Checked channel (extended) %i for enable with bit %llu.", i, channelBits & 1);
#ifdef VERSION_EU
channelBits = channelBits >> 1;
@ -322,17 +308,12 @@ void sequence_player_init_channels_extended(struct SequencePlayer* seqPlayer, u3
}
}
void sequence_player_disable_channels_extended(struct SequencePlayer* seqPlayer, u32 channelBits) {
struct SequenceChannel* seqChannel;
s32 i;
void sequence_player_disable_channels_extended(struct SequencePlayer* seqPlayer, u64 channelBits) {
LOG_DEBUG("Disabling channels (extended) with corresponding bits %llX", channelBits);
#ifdef AUDIO_DEVELOPMENT
printf("debug: Disabling channels (extended) with corresponding bits %X\n", channelBits);
#endif
for (i = 0; i < CHANNELS_MAX; i++) {
for (u32 i = 0; i < CHANNELS_MAX; i++) {
if (channelBits & 1) {
seqChannel = seqPlayer->channels[i];
struct SequenceChannel* seqChannel = seqPlayer->channels[i];
if (IS_SEQUENCE_CHANNEL_VALID(seqChannel) == TRUE) {
if (seqChannel->seqPlayer == seqPlayer) {
sequence_channel_disable(seqChannel);
@ -349,6 +330,27 @@ void sequence_player_disable_channels_extended(struct SequencePlayer* seqPlayer,
}
}
void sequence_player_disable_all_channels(struct SequencePlayer *seqPlayer) {
eu_stubbed_printf_0("SUBTRACK DIM\n");
for (u32 i = 0; i < CHANNELS_MAX; i++) {
struct SequenceChannel *seqChannel = seqPlayer->channels[i];
if (IS_SEQUENCE_CHANNEL_VALID(seqChannel) == TRUE) {
if (seqChannel->seqPlayer == seqPlayer) {
sequence_channel_disable(seqChannel);
seqChannel->seqPlayer = NULL;
}
#if defined(VERSION_EU) || defined(VERSION_SH)
else {
#ifdef VERSION_EU
stubbed_printf("Audio:Track: Warning SUBTRACK PARENT CHANGED\n");
#endif
}
#endif
seqPlayer->channels[i] = &gSequenceChannelNone;
}
}
}
void sequence_channel_enable(struct SequencePlayer *seqPlayer, u8 channelIndex, void *script) {
struct SequenceChannel *seqChannel = seqPlayer->channels[channelIndex];
s32 i;
@ -378,11 +380,15 @@ void sequence_channel_enable(struct SequencePlayer *seqPlayer, u8 channelIndex,
seq_channel_layer_free(seqChannel, i);
}
}
LOG_DEBUG("Enabled sequence channel %d with script entry of %p", channelIndex, script);
}
}
void sequence_player_disable(struct SequencePlayer *seqPlayer) {
sequence_player_disable_channels(seqPlayer, 0xffff);
LOG_DEBUG("Disabling sequence player %p", seqPlayer);
sequence_player_disable_all_channels(seqPlayer);
note_pool_clear(&seqPlayer->notePool);
seqPlayer->finished = TRUE;
seqPlayer->enabled = FALSE;
@ -519,6 +525,22 @@ s32 m64_read_s32(struct M64ScriptState* state) {
return ret;
}
s64 m64_read_s64(struct M64ScriptState* state) {
#ifdef DEVELOPMENT
assert(state != NULL);
assert(state->pc != NULL);
#endif
s64 ret = *(state->pc++) << 56;
ret = (*(state->pc++) << 48) | ret;
ret = (*(state->pc++) << 40) | ret;
ret = (*(state->pc++) << 32) | ret;
ret = (*(state->pc++) << 24) | ret;
ret = (*(state->pc++) << 16) | ret;
ret = (*(state->pc++) << 8) | ret;
ret = *(state->pc++) | ret;
return ret;
}
#if defined(VERSION_SH)
void seq_channel_layer_process_script(struct SequenceChannelLayer *layer) {
s32 cmd;
@ -2341,8 +2363,10 @@ void sequence_player_process_sequence(struct SequencePlayer *seqPlayer) {
u8 temp;
s32 value = 0;
s32 i;
u8 u8v;
u16 u16v;
u32 u32v;
u64 u64v;
u8 *tempPtr;
struct M64ScriptState *state;
#if defined(VERSION_EU) || defined(VERSION_SH)
@ -2707,13 +2731,13 @@ void sequence_player_process_sequence(struct SequencePlayer *seqPlayer) {
break;
case 0xc1: // seq_initchannels_extended
u32v = m64_read_s32(state);
sequence_player_init_channels_extended(seqPlayer, u32v);
u64v = m64_read_s64(state);
sequence_player_init_channels_extended(seqPlayer, u64v);
break;
case 0xc0: // seq_disablechannels_extended
u32v = m64_read_s32(state);
sequence_player_disable_channels_extended(seqPlayer, u32v);
u64v = m64_read_s64(state);
sequence_player_disable_channels_extended(seqPlayer, u64v);
break;
case 0xd5: // seq_setmutescale
@ -2823,6 +2847,11 @@ void sequence_player_process_sequence(struct SequencePlayer *seqPlayer) {
break;
case 0xa0:
break;
case 0xb0: // seq_startchannel_extended
u8v = m64_read_u8(state);
u16v = m64_read_s16(state);
sequence_channel_enable(seqPlayer, u8v, seqPlayer->seqData + u16v);
break;
#if !defined(VERSION_EU) && !defined(VERSION_SH)
case 0xd8: // (this makes no sense)
break;