Skip to content

Commit

Permalink
MIDI synth (Fluidsynth): Add code to recognize Roland GS MASTER VOLUM…
Browse files Browse the repository at this point in the history
…E sysex. Now PC-98 games can "fade out" the MIDI music using GS commands (Seiyoku Gakuen Seraphita)
  • Loading branch information
joncampbell123 committed Jan 11, 2025
1 parent c109f80 commit 4035992
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/midi.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ struct DB_Midi {
DB_Midi() {}
};

extern bool roland_gs_sysex;
extern DB_Midi midi;

#endif
25 changes: 25 additions & 0 deletions src/gui/midi_synth.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
static MixerChannel *synthchan = NULL;
static fluid_synth_t *synth_soft = NULL;
static int synthsamplerate = 0;
static uint8_t master_volume = 128;

static void synth_log(int level,
#if !defined (FLUIDSYNTH_VERSION_MAJOR) || FLUIDSYNTH_VERSION_MAJOR >= 2 // Let version 2.x be the default
Expand Down Expand Up @@ -61,6 +62,11 @@ static void synth_log(int level,
static void synth_CallBack(Bitu len) {
if (synth_soft != NULL) {
fluid_synth_write_s16(synth_soft, (int)len, MixTemp, 0, 2, MixTemp, 1, 2);
if (master_volume < 128) {
for (unsigned int i=0;i < (len*2);i++) {
((int16_t*)MixTemp)[i] = (int16_t)(((((int16_t*)MixTemp)[i]) * master_volume) >> 7);
}
}
synthchan->AddSamples_s16(len,(int16_t *)MixTemp);
}
}
Expand All @@ -82,6 +88,23 @@ class MidiHandler_synth: public MidiHandler {
void PlayEvent(uint8_t *msg, Bitu len) {
uint8_t event = msg[0], channel, p1, p2;

if (roland_gs_sysex) {
if (msg[1] == 0x41/*Roland*/ && msg[3] == 0x42/*GS*/ && msg[4] == 0x12/*Send*/ && len >= 9) {
const uint32_t addr =
((uint32_t)msg[5] << 16) +
((uint32_t)msg[6] << 8) +
(uint32_t)msg[7];

if (addr == 0x400004) { /* MASTER VOLUME */
/* Fluidsynth doesn't appear to support this message, so we have to handle it ourself. */
master_volume = msg[8];
if (master_volume >= 127) master_volume = 128;
LOG_MSG("MIDI synth: MASTER VOLUME %u",master_volume);
return;
}
}
}

switch (event) {
case 0xf0:
case 0xf7:
Expand All @@ -92,6 +115,7 @@ class MidiHandler_synth: public MidiHandler {
LOG(LOG_MISC,LOG_DEBUG)("SYNTH: midi tick");
return;
case 0xff:
master_volume = 128;
LOG(LOG_MISC,LOG_DEBUG)("SYNTH: system reset");
fluid_synth_system_reset(synth_soft);
return;
Expand Down Expand Up @@ -224,6 +248,7 @@ class MidiHandler_synth: public MidiHandler {
sffile=sf;
fsinfo="Sound font: "+sf;

master_volume = 128;
synthchan = MIXER_AddChannel(synth_CallBack, (unsigned int)synthsamplerate, "SYNTH");
synthchan->Enable(false);
isOpen = true;
Expand Down

0 comments on commit 4035992

Please sign in to comment.