Skip to content

Commit b5341bf

Browse files
committed
Add MOD volume control
1 parent 9195544 commit b5341bf

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

src/mips/modplayer/modplayer.c

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,11 @@ static void SPUUploadInstruments(uint32_t SpuAddr, const uint8_t* data, uint32_t
133133

134134
static void SPUUnMute() { SPU_CTRL = 0xc000; }
135135

136-
static void SPUSetVoiceVolume(int voiceID, uint16_t left, uint16_t right) {
137-
SPU_VOICES[voiceID].volumeLeft = left >> 2;
138-
SPU_VOICES[voiceID].volumeRight = right >> 2;
136+
static uint32_t s_masterVolume = 16384;
137+
138+
static void SPUSetVoiceVolume(int voiceID, uint32_t left, uint32_t right) {
139+
SPU_VOICES[voiceID].volumeLeft = (left * s_masterVolume) >> 16;
140+
SPU_VOICES[voiceID].volumeRight = (right * s_masterVolume) >> 16;
139141
}
140142

141143
static void SPUSetStartAddress(int voiceID, uint32_t spuAddr) { SPU_VOICES[voiceID].sampleStartAddr = spuAddr >> 3; }
@@ -804,3 +806,18 @@ void MOD_PlayNote(unsigned channel, unsigned sampleID, unsigned note, int16_t vo
804806
int32_t newPeriod = channelData->period = MOD_PeriodTable[note];
805807
SETVOICESAMPLERATE(channel, newPeriod);
806808
}
809+
810+
void MOD_PlaySoundEffect(unsigned channel, unsigned sampleID, unsigned note, int16_t volume) {
811+
uint32_t s_prevVolume = s_masterVolume;
812+
s_masterVolume = 16384;
813+
MOD_PlayNote( channel, sampleID, note, volume);
814+
s_masterVolume = s_prevVolume;
815+
}
816+
817+
void MOD_SetMusicVolume(uint32_t musicVolume) {
818+
s_masterVolume = musicVolume;
819+
const unsigned channels = MOD_Channels;
820+
for (unsigned channel = 0; channel < MOD_Channels; channel++) {
821+
SETVOICEVOLUME(channel, s_channelData[channel].volume);
822+
}
823+
}

src/mips/modplayer/modplayer.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,5 +146,12 @@ void MOD_Relocate(uint8_t* buffer);
146146
// setting the volume of the voice to 0.
147147
void MOD_PlayNote(unsigned voiceID, unsigned sampleID, unsigned note, int16_t volume);
148148

149+
// Plays a sound effect
150+
// As opposed to MOD_PlayNote(), MOD_PlaySoundEffect()'s volume is absolute. 0 == mute, 63 == max SPU voice volume
151+
void MOD_PlaySoundEffect(unsigned channel, unsigned sampleID, unsigned note, int16_t volume);
152+
149153
// Added API to reset the SPU and silence everything.
150154
void MOD_Silence();
155+
156+
// Set MOD Volume to musicVolume, where musicVolume is between 0 and 65535.
157+
void MOD_SetMusicVolume(uint32_t musicVolume);

0 commit comments

Comments
 (0)