From 6984936c7431e2bdd5d2c80c6b2905965a5a4597 Mon Sep 17 00:00:00 2001 From: Marc Lainez Date: Mon, 19 Oct 2026 09:23:03 +0200 Subject: [PATCH 1/5] libemusc: add Synth::has_gm_mode() and a GM lock The lock keeps a device in GM mode whatever SysEx it is sent, for hosts that offer a GM-only mode. Assisted-by: Claude:claude-opus-5-5 --- libemusc/src/CMakeLists.txt | 14 ++ libemusc/src/engines/xp/device.cc | 6 + libemusc/src/engines/xp/device.h | 2 + libemusc/src/settings.cc | 18 +- libemusc/src/settings.h | 2 + libemusc/src/synth.cc | 22 ++- libemusc/src/synth.h | 18 ++ libemusc/tests/synth_gm_lock_test.cc | 257 +++++++++++++++++++++++++++ 8 files changed, 332 insertions(+), 7 deletions(-) create mode 100644 libemusc/tests/synth_gm_lock_test.cc diff --git a/libemusc/src/CMakeLists.txt b/libemusc/src/CMakeLists.txt index f8b08c77..718ff10d 100644 --- a/libemusc/src/CMakeLists.txt +++ b/libemusc/src/CMakeLists.txt @@ -456,4 +456,18 @@ if(BUILD_TESTING) set_target_properties(synth_rx_roland_only_test PROPERTIES CXX_EXTENSIONS OFF) add_test(NAME synth_rx_roland_only_test COMMAND synth_rx_roland_only_test) set_property(TEST synth_rx_roland_only_test PROPERTY SKIP_RETURN_CODE 77) + + # has_gm_mode() and the GM lock. Needs the SC-55mkII ROMs, and checks + # has_gm_mode() on each other device whose ROMs are given. + add_executable(synth_gm_lock_test ../tests/synth_gm_lock_test.cc) + target_include_directories(synth_gm_lock_test PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}") + target_link_libraries(synth_gm_lock_test PRIVATE emusc_static) + if(MSVC) + target_compile_options(synth_gm_lock_test PRIVATE /UNDEBUG) + else() + target_compile_options(synth_gm_lock_test PRIVATE -UNDEBUG) + endif() + set_target_properties(synth_gm_lock_test PROPERTIES CXX_EXTENSIONS OFF) + add_test(NAME synth_gm_lock_test COMMAND synth_gm_lock_test) + set_property(TEST synth_gm_lock_test PROPERTY SKIP_RETURN_CODE 77) endif() diff --git a/libemusc/src/engines/xp/device.cc b/libemusc/src/engines/xp/device.cc index bdb3e60c..c629cf66 100644 --- a/libemusc/src/engines/xp/device.cc +++ b/libemusc/src/engines/xp/device.cc @@ -733,6 +733,12 @@ bool device_gm_system_on(Device *device) return device->voice_ops->gm_system_on(device->voice_state); } +bool device_has_gm_mode(const Device *device) +{ + return device && device->initialized && device->voice_ops && + device->voice_ops->gm_system_on; +} + bool device_host_reset_enters_gm(const Device *device) { if (!device || !device->initialized) diff --git a/libemusc/src/engines/xp/device.h b/libemusc/src/engines/xp/device.h index f6e79b57..85118bda 100644 --- a/libemusc/src/engines/xp/device.h +++ b/libemusc/src/engines/xp/device.h @@ -236,6 +236,8 @@ void device_reset_controllers(Device *device); * message had arrived. False, with nothing changed, on a device that has * no GM mode - which includes every device on the shared firmware port. */ bool device_gm_system_on(Device *device); +/* Whether device_gm_system_on() would act: the device has a GM mode. */ +bool device_has_gm_mode(const Device *device); /* True on a device whose profile has a host reset enter GM mode; see * XpDeviceProfile::hostResetEntersGm. */ bool device_host_reset_enters_gm(const Device *device); diff --git a/libemusc/src/settings.cc b/libemusc/src/settings.cc index 625c7dc2..0d8aeee8 100644 --- a/libemusc/src/settings.cc +++ b/libemusc/src/settings.cc @@ -668,6 +668,14 @@ void Settings::_initialize_drumSet_params(void) } +bool Settings::has_gm_mode(void) const +{ + // Why the SC-55 and a Roland-only receiver have none: see set_gm_mode(). + return _ctrlRom.generation() != ControlRom::SynthGen::SC55 && + !_ctrlRom.device()->rolandSysExOnly; +} + + void Settings::set_gm_mode(void) { // "Turn General MIDI System On" sets every internal parameter to the @@ -683,12 +691,10 @@ void Settings::set_gm_mode(void) // SC-55 a bank-selected variation tone and an NRPN both still take effect // after the message, while on the SC-55mkII neither does (P-0092). So the // SC-55 reaches plain GS defaults here and nothing more. - if (_ctrlRom.generation() == ControlRom::SynthGen::SC55) - return; - - // A device that cannot receive GM System On has no GM mode: a GS_GM host - // reset leaves it exactly as a GS one does, receive switches included. - if (_ctrlRom.device()->rolandSysExOnly) + // + // A device that cannot receive GM System On has no GM mode either: a GS_GM + // host reset leaves it exactly as a GS one does, receive switches included. + if (!has_gm_mode()) return; for (int p = 0; p < 16; p ++) { // TODO: Support SC-88 with 32 parts diff --git a/libemusc/src/settings.h b/libemusc/src/settings.h index 19acad21..4a9d4eb2 100644 --- a/libemusc/src/settings.h +++ b/libemusc/src/settings.h @@ -118,6 +118,8 @@ class Settings // Reset all settings to default GS mode void reset(); void set_gm_mode(void); + // Whether set_gm_mode() leaves anything different from a GS reset. + bool has_gm_mode(void) const; void set_map_mt32(void); // Temporary solution diff --git a/libemusc/src/synth.cc b/libemusc/src/synth.cc index 90160882..41675932 100644 --- a/libemusc/src/synth.cc +++ b/libemusc/src/synth.cc @@ -920,9 +920,15 @@ void Synth::_apply_midi_sysex(uint8_t *data, uint16_t length) // _midi_input_sysex_RQ1(&data[5], length - 5 - 2); // Add reply data buffer // Data set 1 (DT1) - if (data[4] == 0x12) + if (data[4] == 0x12) { _midi_input_sysex_DT1(data[3], &data[5], length - 5 - 2); + // GM mode is GS with some receive switches off, and both a GS Reset and + // a DT1 to those switches turn them back on: put them back. + if (_gmLocked) + _settings->set_gm_mode(); + } + midiMutex.unlock(); } @@ -1207,6 +1213,20 @@ void Synth::panic(void) } +bool Synth::has_gm_mode(void) const +{ + if (_ctrlRom.uses_xp_engine()) + return Xp::device_has_gm_mode(_xpDevice); + return _settings->has_gm_mode(); +} + + +void Synth::set_gm_locked(bool locked) +{ + _gmLocked = locked; +} + + void Synth::set_part_instrument(uint8_t partId, uint8_t index, uint8_t bank) { _parts[partId].set_program(index, bank, true); diff --git a/libemusc/src/synth.h b/libemusc/src/synth.h index 640484dc..e86c7bbc 100644 --- a/libemusc/src/synth.h +++ b/libemusc/src/synth.h @@ -139,6 +139,22 @@ class Synth void reset(SoundMap sm, bool resetParts = false); + // Whether the loaded device has a GM mode that differs from its GS one, + // i.e. whether reset(SoundMap::GS_GM) leaves it anywhere a GS reset does + // not. Asks the engine, which for the SC-88 and JV-1080 exists only from + // set_audio_format() on: call it after that. + bool has_gm_mode(void) const; + + // While locked, the device stays in GM mode whatever SysEx it receives: a + // GS reset resets it into GM instead of GS, and a parameter write cannot + // turn GM's receive switches back on. For a host that offers a GM-only + // mode. It does not itself enter GM: reset(SoundMap::GS_GM) does that. + // Off by default, and meaningless on a device without has_gm_mode(). The + // XP engine has no SysEx path out of GM mode, so only the GP engine needs + // and reads it. + void set_gm_locked(bool locked); + bool gm_locked(void) const { return _gmLocked; } + // Silences the device whatever it is and whatever it was sent: queued MIDI // is dropped, every voice of every part is cut, pedals are released and // controllers reset. Programs, patches and effects are kept. Not a MIDI @@ -211,6 +227,8 @@ class Synth private: Settings *_settings = nullptr; + bool _gmLocked = false; + // The SC-88 is not rendered by the Part and Note path: its engine reads the // control ROM itself and renders a stereo frame at a time off its own // scheduler clock. When the loaded ROM is an SC-88 this holds that engine and diff --git a/libemusc/tests/synth_gm_lock_test.cc b/libemusc/tests/synth_gm_lock_test.cc new file mode 100644 index 00000000..7598e6a6 --- /dev/null +++ b/libemusc/tests/synth_gm_lock_test.cc @@ -0,0 +1,257 @@ +/* SPDX-License-Identifier: CC0-1.0 */ + +/* Synth::has_gm_mode() and Synth::set_gm_locked(). + * + * On the SC-55mkII, GM mode is GS with Rx.BankSelect and Rx.NRPN off on every + * part (Settings::set_gm_mode()), which shows as a bank select that no longer + * reaches the tone. A GS Reset turns both back on, and so can a GS DT1 to a + * part's Rx.BankSelect. Under the GM lock neither may: the GS Reset resets + * into GM instead, and the switch stays off. Each locked case is run unlocked + * first, so the test shows the lock, not the message, is what holds GM. + * + * has_gm_mode() is checked on every device whose ROMs are given: true on the + * SC-55mkII and JV-1080, false on the SC-55, JV-880 and SC-88. + * + * SC55MKII_CONTROL_ROM, SC55MKII_CPU_ROM, SC55MKII_WAVE_ROMS + * SC55_CONTROL_ROM, SC55_CPU_ROM, SC55_WAVE_ROMS + * JV880_CONTROL_ROM, JV880_WAVE_ROMS + * SC88_CONTROL_ROM, SC88_WAVE_ROMS + * JV1080_CONTROL_ROM, JV1080_WAVE_ROMS + * + * The wave ROMs are comma separated, in chip order. Without the SC-55mkII's + * the test reports skipped. + */ + +#include "synth.h" +#include "control_rom.h" +#include "wave_rom.h" + +#include +#ifdef NDEBUG +#error "this test is assertion-driven; NDEBUG compiles it away" +#endif +#include +#include +#include +#include +#include + +namespace { + +using SoundMap = EmuSC::Synth::SoundMap; +const uint32_t kRate = 32000; + +std::vector split_commas(const std::string &s) +{ + std::vector out; + std::string::size_type start = 0; + while (start <= s.size()) { + std::string::size_type comma = s.find(',', start); + if (comma == std::string::npos) { + out.push_back(s.substr(start)); + break; + } + out.push_back(s.substr(start, comma - start)); + start = comma + 1; + } + return out; +} + +struct Roms { + std::unique_ptr ctrl; + std::unique_ptr wave; +}; + +/* Loads _CONTROL_ROM, _CPU_ROM (if set) and _WAVE_ROMS; false when + they are not given. */ +bool load(const std::string &prefix, Roms &r) +{ + const char *control = getenv((prefix + "_CONTROL_ROM").c_str()); + const char *cpu = getenv((prefix + "_CPU_ROM").c_str()); + const char *wave = getenv((prefix + "_WAVE_ROMS").c_str()); + if (!control || !wave) + return false; + try { + r.ctrl.reset(new EmuSC::ControlRom(control, cpu ? cpu : "")); + r.wave.reset(new EmuSC::WaveRom(split_commas(wave), *r.ctrl)); + } catch (const std::string &e) { + std::cerr << prefix << " ROM load failed: " << e << std::endl; + return false; + } + return true; +} + +/* Events are queued and applied as frames are rendered. */ +void run(EmuSC::Synth &synth, unsigned frames = kRate / 10) +{ + float l, r; + for (unsigned i = 0; i < frames; i++) + synth.get_next_frame(l, r); +} + +void sysex(EmuSC::Synth &synth, std::vector msg) +{ + synth.midi_input_sysex(msg.data(), (uint16_t) msg.size()); + run(synth); +} + +/* A GS (model 0x42) DT1 to address a0 a1 a2, checksum appended. */ +std::vector gs_dt1(uint8_t a0, uint8_t a1, uint8_t a2, + const std::vector &data) +{ + std::vector m = { 0xf0, 0x41, 0x10, 0x42, 0x12, a0, a1, a2 }; + int sum = a0 + a1 + a2; + for (uint8_t d : data) { + m.push_back(d); + sum += d; + } + m.push_back((uint8_t) ((128 - (sum & 0x7f)) & 0x7f)); + m.push_back(0xf7); + return m; +} + +const std::vector kGsReset = + { 0xf0, 0x41, 0x10, 0x42, 0x12, 0x40, 0x00, 0x7f, 0x00, 0x41, 0xf7 }; +const std::vector kGmOn = { 0xf0, 0x7e, 0x7f, 0x09, 0x01, 0xf7 }; +/* Rx.BankSelect on, part 1 (40 11 23). */ +const std::vector kRxBankSelectOn = gs_dt1(0x40, 0x11, 0x23, { 0x01 }); + +/* CC0 8, PC 0 on channel 1, then the bank the part ended up on. */ +uint8_t bank_after_select(EmuSC::Synth &synth) +{ + synth.midi_input(0xb0, 0, 8); + synth.midi_input(0xc0, 0, 0); + run(synth); + return synth.get_param(EmuSC::PatchParam::ToneNumber, 0); +} + +bool rx_on(EmuSC::Synth &s, EmuSC::PatchParam p) +{ + for (int part = 0; part < 16; part++) + if (s.get_param(p, part) == 0) + return false; + return true; +} + +bool rx_off(EmuSC::Synth &s, EmuSC::PatchParam p) +{ + for (int part = 0; part < 16; part++) + if (s.get_param(p, part) != 0) + return false; + return true; +} + +bool in_gm(EmuSC::Synth &s) +{ + return rx_off(s, EmuSC::PatchParam::RxBankSelect) && + rx_off(s, EmuSC::PatchParam::RxNRPN); +} + +bool in_gs(EmuSC::Synth &s) +{ + return rx_on(s, EmuSC::PatchParam::RxBankSelect) && + rx_on(s, EmuSC::PatchParam::RxNRPN); +} + +std::unique_ptr power_on(Roms &r, SoundMap map, bool lock) +{ + std::unique_ptr s(new EmuSC::Synth(*r.ctrl, *r.wave)); + s->set_audio_format(kRate, 2); + s->reset(map, true); + s->set_gm_locked(lock); + run(*s); + return s; +} + +void test_sc55mkii(Roms &r) +{ + { + std::unique_ptr s = power_on(r, SoundMap::GS, false); + assert(s->has_gm_mode()); + assert(!s->gm_locked()); + assert(in_gs(*s)); + assert(bank_after_select(*s) == 8); + } + + /* Unlocked: GM mode holds until a GS Reset or a DT1 undoes it. */ + { + std::unique_ptr s = power_on(r, SoundMap::GS_GM, false); + assert(in_gm(*s)); + assert(bank_after_select(*s) == 0); + + sysex(*s, kGsReset); + assert(in_gs(*s)); + assert(bank_after_select(*s) == 8); + + sysex(*s, kGmOn); + assert(in_gm(*s)); + sysex(*s, kRxBankSelectOn); + assert(s->get_param(EmuSC::PatchParam::RxBankSelect, 0) == 1); + assert(bank_after_select(*s) == 8); + } + + /* Locked: neither does. */ + { + std::unique_ptr s = power_on(r, SoundMap::GS_GM, true); + assert(s->gm_locked()); + assert(in_gm(*s)); + + sysex(*s, kGsReset); + assert(in_gm(*s)); + assert(bank_after_select(*s) == 0); + + sysex(*s, kRxBankSelectOn); + assert(in_gm(*s)); + assert(bank_after_select(*s) == 0); + + /* The GS Reset still resets: a part level written before it is back at + its default after it. */ + const uint8_t level = s->get_param(EmuSC::PatchParam::PartLevel, 0); + sysex(*s, gs_dt1(0x40, 0x11, 0x19, { (uint8_t) (level ^ 0x40) })); + assert(s->get_param(EmuSC::PatchParam::PartLevel, 0) == (level ^ 0x40)); + sysex(*s, kGsReset); + assert(s->get_param(EmuSC::PatchParam::PartLevel, 0) == level); + assert(in_gm(*s)); + + /* Unlocking hands GS back to the stream. */ + s->set_gm_locked(false); + sysex(*s, kGsReset); + assert(in_gs(*s)); + assert(bank_after_select(*s) == 8); + } +} + +void check_has_gm_mode(const char *prefix, bool expected, int &checked) +{ + Roms r; + if (!load(prefix, r)) + return; + EmuSC::Synth s(*r.ctrl, *r.wave); + s.set_audio_format(kRate, 2); + s.reset(SoundMap::GS, true); + if (s.has_gm_mode() != expected) { + std::cerr << prefix << ": has_gm_mode() is " << s.has_gm_mode() + << ", expected " << expected << std::endl; + assert(false); + } + checked++; +} + +} // namespace + +int main(void) +{ + Roms mk2; + if (!load("SC55MKII", mk2)) + return 77; + test_sc55mkii(mk2); + + int checked = 0; + check_has_gm_mode("SC55", false, checked); + check_has_gm_mode("JV880", false, checked); + check_has_gm_mode("SC88", false, checked); + check_has_gm_mode("JV1080", true, checked); + std::cout << "has_gm_mode() checked on " << checked + << " more device(s)" << std::endl; + return 0; +} From a88bd96856259c2c2833c6010a8c580ac1ca14d3 Mon Sep 17 00:00:00 2001 From: Marc Lainez Date: Mon, 19 Oct 2026 09:23:03 +0200 Subject: [PATCH 2/5] emuscd, emusc-winmidi: add --reset gs|gm gm powers every device that has a GM mode on in it and locks it there for as long as the daemon runs, across device switches; a device without one runs in GS. Assisted-by: Claude:claude-opus-5-5 --- README.md | 4 ++++ emuscd/README.md | 4 ++++ emuscd/common.h | 54 ++++++++++++++++++++++++++++++++++++------ emuscd/main.cc | 33 ++++++++++++++++++-------- emuscd/main_winmidi.cc | 25 +++++++++++++++---- 5 files changed, 98 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 1e883ea0..2ea9df4a 100644 --- a/README.md +++ b/README.md @@ -475,6 +475,8 @@ Common options: ``` --device NAME Device to emulate (default: sc88); sc55, sc55mkii, sc88, jv880, jv1080 + --reset MODE gs or gm (default: gs); gm keeps a device with a GM mode in it + whatever SysEx it is sent, a device without one runs in GS --name NAME ALSA MIDI port name (default: emuscd) --pcm DEVICE ALSA PCM output device (default: default) --list-pcm List ALSA PCM devices and exit @@ -532,6 +534,8 @@ Common options: ``` --device NAME Device to emulate (default: sc88); sc55, sc55mkii, sc88, jv880, jv1080 --midi-in N MIDI input device index (default: 0) + --reset MODE gs or gm (default: gs); gm keeps a device with a GM mode in it + whatever SysEx it is sent, a device without one runs in GS --wave-out N Wave output device index (default: system default) --list-midi-in List MIDI input devices and exit --list-wave-out List wave output devices and exit diff --git a/emuscd/README.md b/emuscd/README.md index e19328a6..c7b995d8 100644 --- a/emuscd/README.md +++ b/emuscd/README.md @@ -48,6 +48,8 @@ emuscd --device sc88 --name "My Synth" ``` --device NAME Device to emulate (default: sc88); sc55, sc55mkii, sc88, jv880, jv1080 +--reset MODE gs or gm (default: gs); gm keeps a device with a GM mode in it + whatever SysEx it is sent, a device without one runs in GS --name NAME ALSA MIDI port name (default: emuscd) --pcm DEVICE ALSA PCM output device (default: default) --list-pcm List ALSA PCM devices and exit @@ -146,6 +148,8 @@ emusc-winmidi.exe --device sc88 --midi-in 1 ``` --device NAME Device to emulate (default: sc88); sc55, sc55mkii, sc88, jv880, jv1080 --midi-in N MIDI input device index (default: 0) +--reset MODE gs or gm (default: gs); gm keeps a device with a GM mode in it + whatever SysEx it is sent, a device without one runs in GS --audio-api API Audio output API: auto, winmm or dsound (default: auto, DirectSound with WinMM fallback) --wave-out N WinMM wave output device index (default: system default) diff --git a/emuscd/common.h b/emuscd/common.h index 64712b48..e858c1e5 100644 --- a/emuscd/common.h +++ b/emuscd/common.h @@ -21,13 +21,53 @@ using emusc_tools::default_rom_dir; using emusc_tools::gain_db_to_linear; using emusc_tools::apply_gain; -// The sound map emuscd runs in. Named once so that the value handed to the -// Synth constructor and the value handed to its power-on reset cannot drift -// apart: reset(sm, ...) re-applies the map, so passing a different one there -// would silently switch the device into another mode. GS is the Synth default -// and the mode every supported device is addressed in; emuscd exposes no -// option to change it. -inline const EmuSC::Synth::SoundMap SOUND_MAP = EmuSC::Synth::SoundMap::GS; +// --reset: the mode every device this daemon loads is powered on in, kept +// for as long as it runs, across device switches. +enum class ResetMode { GS, GM }; + +inline bool parse_reset_mode(const std::string &s, ResetMode &out) { + if (s == "gs") { out = ResetMode::GS; return true; } + if (s == "gm") { out = ResetMode::GM; return true; } + return false; +} + +// What power_on() left the device in. +enum class PowerOnState { GS, GMLocked, GMUnavailable }; + +// The power-on reset, which is what the hardware does when it is switched on. +// It must follow set_audio_format(), which is where the parts are +// instantiated and the SC-88 and JV-1080 engines are opened: before that the +// reset has nothing to act on, and has_gm_mode() nothing to ask. +// +// GM asks for a GM-only device: the reset enters GM mode and the lock keeps it +// there whatever SysEx arrives. A device without a GM mode is reset into GS +// instead, its default. +inline PowerOnState power_on(EmuSC::Synth &synth, ResetMode mode) { + using SoundMap = EmuSC::Synth::SoundMap; + if (mode == ResetMode::GM && synth.has_gm_mode()) { + synth.reset(SoundMap::GS_GM, true); + synth.set_gm_locked(true); + return PowerOnState::GMLocked; + } + synth.reset(SoundMap::GS, true); + return mode == ResetMode::GM ? PowerOnState::GMUnavailable : PowerOnState::GS; +} + +// The line a daemon prints after power_on(), empty when there is nothing to +// say. `prog` names the daemon, `dev` the device. +inline std::string power_on_message(PowerOnState state, const std::string &prog, + const std::string &dev) { + switch (state) { + case PowerOnState::GMLocked: + return prog + ": " + dev + " is in GM mode and stays in it while " + prog + + " runs, whatever SysEx it is sent"; + case PowerOnState::GMUnavailable: + return prog + ": no GM mode available on " + dev + + ", so GS was selected as the default"; + default: + return ""; + } +} inline bool device_supported(const std::string &dev) { for (const char *d : SUPPORTED_DEVICES) diff --git a/emuscd/main.cc b/emuscd/main.cc index ac507463..6d0f9b98 100644 --- a/emuscd/main.cc +++ b/emuscd/main.cc @@ -115,6 +115,7 @@ class EmuscdDaemon { std::unique_ptr synth; std::string device_name; std::string rom_dir; + ResetMode reset_mode; std::atomic running{true}; std::atomic device_change_requested{false}; @@ -128,10 +129,11 @@ class EmuscdDaemon { unsigned int requested_rate, unsigned int latency_ms, unsigned int block, unsigned int threads, const std::vector& workers, bool stats, - double gain_db) + double gain_db, ResetMode reset) : sample_rate(requested_rate), block_frames(block), render_threads(threads), worker_cpus(workers), show_stats(stats), - gain_lin(gain_db_to_linear(gain_db)), rom_dir(rom_directory) { + gain_lin(gain_db_to_linear(gain_db)), rom_dir(rom_directory), + reset_mode(reset) { init_alsa_midi(port_name); init_alsa_audio(pcm_device, latency_ms); if (!load_device(dev)) { @@ -227,16 +229,14 @@ class EmuscdDaemon { } std::unique_ptr new_synth( - new EmuSC::Synth(*new_ctrl, *new_wave, SOUND_MAP)); + new EmuSC::Synth(*new_ctrl, *new_wave)); new_synth->set_audio_format(sample_rate, 2); new_synth->set_render_threads(render_threads, worker_cpus); - // Power-on reset, which is what the hardware does when it is switched on - // and what a GS reset in the stream asks for later. It must follow - // set_audio_format(), which is where the 16 parts are instantiated: called - // before that, the resetParts loop has nothing to walk. + // Power-on reset (see power_on()), which is what the hardware does when it + // is switched on and what a GS reset in the stream asks for later. // - // On a Sound Canvas the reset is redundant at startup - Settings::reset() + // On a Sound Canvas a GS reset is redundant at startup - Settings::reset() // runs exactly the four _initialize_*/_apply_device_performance calls the // Settings constructor runs, so a freshly built Synth already holds the // same defaults, bank 0 program 0 on every part and Drum1 on part 10. It @@ -248,7 +248,7 @@ class EmuscdDaemon { // PerformanceLayout::resetSelector) it is not redundant: it is what takes // the machine out of its layered boot Performance and onto the channels a // multitimbral stream expects. - new_synth->reset(SOUND_MAP, true); + const PowerOnState state = power_on(*new_synth, reset_mode); // Destroy the old Synth before its ControlRom/WaveRom, which it holds by // reference, are replaced out from under it. @@ -260,6 +260,9 @@ class EmuscdDaemon { std::cout << "Loaded device: " << dev << " (" << ctrl_rom->model() << " v" << ctrl_rom->version() << ")" << std::endl; + const std::string msg = power_on_message(state, "emuscd", dev); + if (!msg.empty()) + std::cout << msg << std::endl; return true; } @@ -425,6 +428,7 @@ int main(int argc, char* argv[]) { bool stats = false; int rt_priority = 0; double gain_db = 0.0; + ResetMode reset_mode = ResetMode::GS; for (int i = 1; i < argc; i++) { std::string arg = argv[i]; @@ -474,6 +478,11 @@ int main(int argc, char* argv[]) { } } else if (arg == "--gain-db") { gain_db = std::stod(need("--gain-db")); + } else if (arg == "--reset") { + if (!parse_reset_mode(need("--reset"), reset_mode)) { + std::cerr << "emuscd: --reset must be gs or gm" << std::endl; + return 1; + } } else if (arg == "--help" || arg == "-h") { std::cout << "emuscd - Roland Sound Canvas daemon\n" @@ -481,6 +490,10 @@ int main(int argc, char* argv[]) { << "Options:\n" << " --device NAME Device to emulate (default: sc88)\n" << " Supported: sc55, sc55mkii, sc88, jv880, jv1080\n" + << " --reset MODE gs or gm (default: gs). gm keeps every device\n" + << " that has a GM mode in it while emuscd runs,\n" + << " whatever SysEx it is sent; a device without\n" + << " one runs in GS\n" << " --name NAME ALSA MIDI port name (default: emuscd)\n" << " --pcm DEVICE ALSA PCM output device (default: default)\n" << " --list-pcm List ALSA PCM devices and exit\n" @@ -560,7 +573,7 @@ int main(int argc, char* argv[]) { try { EmuscdDaemon daemon(device, port_name, pcm_device, rom_dir, rate, latency_ms, block, threads, worker_cpus, stats, - gain_db); + gain_db, reset_mode); daemon.run(); } catch (const std::exception& e) { std::cerr << "Fatal error: " << e.what() << std::endl; diff --git a/emuscd/main_winmidi.cc b/emuscd/main_winmidi.cc index 1de0ff39..58a07ae1 100644 --- a/emuscd/main_winmidi.cc +++ b/emuscd/main_winmidi.cc @@ -41,6 +41,10 @@ const char *USAGE = " --device NAME Device to emulate (default: sc88)\n" " Supported: sc55, sc55mkii, sc88, jv880, jv1080\n" " --midi-in N MIDI input device index (default: 0)\n" +" --reset MODE gs or gm (default: gs). gm keeps every device that\n" +" has a GM mode in it while emusc-winmidi runs,\n" +" whatever SysEx it is sent; a device without one\n" +" runs in GS\n" " --audio-api API Audio output API: auto, winmm or dsound (default:\n" " auto - DirectSound, falling back to WinMM if it\n" " cannot be opened; winmm/dsound force one, and a\n" @@ -119,9 +123,9 @@ class WinMidiDaemon { WinMidiDaemon(const std::string &dev, const std::string &romDir, int midiInId, int waveOutId, unsigned rate, unsigned block, unsigned latencyMs, double gainDb, AudioApi api, - int dsoundOutId) + int dsoundOutId, ResetMode resetMode) : _sampleRate(rate), _blockFrames(block), _romDir(romDir), - _gainLin(gain_db_to_linear(gainDb)) { + _resetMode(resetMode), _gainLin(gain_db_to_linear(gainDb)) { InitializeCriticalSection(&_midiLock); InitializeCriticalSection(&_sysexLock); if (!load_device(dev)) { @@ -175,9 +179,9 @@ class WinMidiDaemon { } std::unique_ptr new_synth( - new EmuSC::Synth(*new_ctrl, *new_wave, SOUND_MAP)); + new EmuSC::Synth(*new_ctrl, *new_wave)); new_synth->set_audio_format(_sampleRate, 2); - new_synth->reset(SOUND_MAP, true); + const PowerOnState state = power_on(*new_synth, _resetMode); // Only the main loop (run()) ever touches _synth after construction, on // both the fill/pump and the device-switch path below, so there's no @@ -190,6 +194,9 @@ class WinMidiDaemon { std::fprintf(stderr, "emusc-winmidi: loaded device %s (%s v%s)\n", dev.c_str(), _ctrlRom->model().c_str(), _ctrlRom->version().c_str()); + const std::string msg = power_on_message(state, "emusc-winmidi", dev); + if (!msg.empty()) + std::fprintf(stderr, "%s\n", msg.c_str()); return true; } @@ -455,6 +462,7 @@ class WinMidiDaemon { unsigned _sampleRate, _blockFrames; std::string _romDir; + ResetMode _resetMode; float _gainLin = 1.0f; std::vector _rendered; std::atomic _running{false}; @@ -497,6 +505,7 @@ int main(int argc, char **argv) { unsigned block = 256; unsigned latency = 20; double gainDb = 0.0; + ResetMode resetMode = ResetMode::GS; for (int i = 1; i < argc; i++) { std::string a = argv[i]; @@ -527,6 +536,12 @@ int main(int argc, char **argv) { else if (a == "--block") block = static_cast(std::stoul(need("--block"))); else if (a == "--latency") latency = static_cast(std::stoul(need("--latency"))); else if (a == "--gain-db") gainDb = std::stod(need("--gain-db")); + else if (a == "--reset") { + if (!parse_reset_mode(need("--reset"), resetMode)) { + std::fprintf(stderr, "emusc-winmidi: --reset must be gs or gm\n"); + return 1; + } + } else if (a == "--list-midi-in") { list_midi_in_devices(); return 0; } else if (a == "--list-wave-out") { list_wave_out_devices(); return 0; } else if (a == "--list-dsound-out") return DSoundOut::list_devices() ? 0 : 1; @@ -550,7 +565,7 @@ int main(int argc, char **argv) { if (romDir.empty()) romDir = default_rom_dir(); WinMidiDaemon daemon(device, romDir, midiInId, waveOutId, rate, block, latency, gainDb, - audioApi, dsoundOutId); + audioApi, dsoundOutId, resetMode); daemon.run(); return 0; } From cf039e3090ce56f00710f3a309a7e6b7dd768118 Mon Sep 17 00:00:00 2001 From: Marc Lainez Date: Mon, 19 Oct 2026 09:29:29 +0200 Subject: [PATCH 3/5] Make --reset gs a lock as well Locked in GS, a GM System On resets the device into GS. Without --reset the stream keeps control of the mode. A device without the mode asked for is locked in the other one, and a device with neither runs in its own default; each case prints a notice. has_gs_mode() joins has_gm_mode() so the daemons can tell which lock a device can take. Assisted-by: Claude:claude-opus-5-5 --- README.md | 8 +- emuscd/README.md | 8 +- emuscd/common.h | 71 +++++++++------- emuscd/main.cc | 11 +-- emuscd/main_winmidi.cc | 11 +-- libemusc/src/CMakeLists.txt | 20 ++--- libemusc/src/engines/xp/device.cc | 6 ++ libemusc/src/engines/xp/device.h | 2 + libemusc/src/settings.cc | 6 ++ libemusc/src/settings.h | 2 + libemusc/src/synth.cc | 16 +++- libemusc/src/synth.h | 30 ++++--- ...m_lock_test.cc => synth_mode_lock_test.cc} | 80 ++++++++++++------- 13 files changed, 170 insertions(+), 101 deletions(-) rename libemusc/tests/{synth_gm_lock_test.cc => synth_mode_lock_test.cc} (71%) diff --git a/README.md b/README.md index 2ea9df4a..b085e817 100644 --- a/README.md +++ b/README.md @@ -475,8 +475,8 @@ Common options: ``` --device NAME Device to emulate (default: sc88); sc55, sc55mkii, sc88, jv880, jv1080 - --reset MODE gs or gm (default: gs); gm keeps a device with a GM mode in it - whatever SysEx it is sent, a device without one runs in GS + --reset MODE gs or gm: keep every device in that mode whatever SysEx it is + sent; one without it is kept in the other (default: neither) --name NAME ALSA MIDI port name (default: emuscd) --pcm DEVICE ALSA PCM output device (default: default) --list-pcm List ALSA PCM devices and exit @@ -534,8 +534,8 @@ Common options: ``` --device NAME Device to emulate (default: sc88); sc55, sc55mkii, sc88, jv880, jv1080 --midi-in N MIDI input device index (default: 0) - --reset MODE gs or gm (default: gs); gm keeps a device with a GM mode in it - whatever SysEx it is sent, a device without one runs in GS + --reset MODE gs or gm: keep every device in that mode whatever SysEx it is + sent; one without it is kept in the other (default: neither) --wave-out N Wave output device index (default: system default) --list-midi-in List MIDI input devices and exit --list-wave-out List wave output devices and exit diff --git a/emuscd/README.md b/emuscd/README.md index c7b995d8..bdd78014 100644 --- a/emuscd/README.md +++ b/emuscd/README.md @@ -48,8 +48,8 @@ emuscd --device sc88 --name "My Synth" ``` --device NAME Device to emulate (default: sc88); sc55, sc55mkii, sc88, jv880, jv1080 ---reset MODE gs or gm (default: gs); gm keeps a device with a GM mode in it - whatever SysEx it is sent, a device without one runs in GS +--reset MODE gs or gm: keep every device in that mode whatever SysEx it is + sent; one without it is kept in the other (default: neither) --name NAME ALSA MIDI port name (default: emuscd) --pcm DEVICE ALSA PCM output device (default: default) --list-pcm List ALSA PCM devices and exit @@ -148,8 +148,8 @@ emusc-winmidi.exe --device sc88 --midi-in 1 ``` --device NAME Device to emulate (default: sc88); sc55, sc55mkii, sc88, jv880, jv1080 --midi-in N MIDI input device index (default: 0) ---reset MODE gs or gm (default: gs); gm keeps a device with a GM mode in it - whatever SysEx it is sent, a device without one runs in GS +--reset MODE gs or gm: keep every device in that mode whatever SysEx it is + sent; one without it is kept in the other (default: neither) --audio-api API Audio output API: auto, winmm or dsound (default: auto, DirectSound with WinMM fallback) --wave-out N WinMM wave output device index (default: system default) diff --git a/emuscd/common.h b/emuscd/common.h index e858c1e5..d619ad54 100644 --- a/emuscd/common.h +++ b/emuscd/common.h @@ -21,9 +21,10 @@ using emusc_tools::default_rom_dir; using emusc_tools::gain_db_to_linear; using emusc_tools::apply_gain; -// --reset: the mode every device this daemon loads is powered on in, kept -// for as long as it runs, across device switches. -enum class ResetMode { GS, GM }; +// --reset: the mode every device this daemon loads is powered on in and +// locked to, for as long as it runs, across device switches. None, without +// --reset, powers it on in GS and leaves the mode to the stream. +enum class ResetMode { None, GS, GM }; inline bool parse_reset_mode(const std::string &s, ResetMode &out) { if (s == "gs") { out = ResetMode::GS; return true; } @@ -31,42 +32,58 @@ inline bool parse_reset_mode(const std::string &s, ResetMode &out) { return false; } -// What power_on() left the device in. -enum class PowerOnState { GS, GMLocked, GMUnavailable }; +// What power_on() did: which mode the device is locked in, if any, and +// whether that is the one --reset asked for. +struct PowerOnState { + ResetMode asked = ResetMode::None; + ResetMode locked = ResetMode::None; +}; // The power-on reset, which is what the hardware does when it is switched on. // It must follow set_audio_format(), which is where the parts are // instantiated and the SC-88 and JV-1080 engines are opened: before that the -// reset has nothing to act on, and has_gm_mode() nothing to ask. +// reset has nothing to act on, and has_gs_mode()/has_gm_mode() nothing to ask. // -// GM asks for a GM-only device: the reset enters GM mode and the lock keeps it -// there whatever SysEx arrives. A device without a GM mode is reset into GS -// instead, its default. +// A device without the mode asked for is locked in the other one instead, and +// a device with neither runs unlocked in the state a GS reset leaves it in - +// its own default. inline PowerOnState power_on(EmuSC::Synth &synth, ResetMode mode) { using SoundMap = EmuSC::Synth::SoundMap; - if (mode == ResetMode::GM && synth.has_gm_mode()) { - synth.reset(SoundMap::GS_GM, true); - synth.set_gm_locked(true); - return PowerOnState::GMLocked; + using ModeLock = EmuSC::Synth::ModeLock; + PowerOnState st; + st.asked = mode; + if (mode != ResetMode::None) { + const bool gs = synth.has_gs_mode(), gm = synth.has_gm_mode(); + if (mode == ResetMode::GM) + st.locked = gm ? ResetMode::GM : gs ? ResetMode::GS : ResetMode::None; + else + st.locked = gs ? ResetMode::GS : gm ? ResetMode::GM : ResetMode::None; } - synth.reset(SoundMap::GS, true); - return mode == ResetMode::GM ? PowerOnState::GMUnavailable : PowerOnState::GS; + synth.reset(st.locked == ResetMode::GM ? SoundMap::GS_GM : SoundMap::GS, true); + synth.set_mode_lock(st.locked == ResetMode::GM ? ModeLock::GM + : st.locked == ResetMode::GS ? ModeLock::GS + : ModeLock::None); + return st; } -// The line a daemon prints after power_on(), empty when there is nothing to -// say. `prog` names the daemon, `dev` the device. -inline std::string power_on_message(PowerOnState state, const std::string &prog, +// The notice a daemon prints after power_on(), empty when --reset was not +// given. `prog` names the daemon, `dev` the device. +inline std::string power_on_message(const PowerOnState &st, + const std::string &prog, const std::string &dev) { - switch (state) { - case PowerOnState::GMLocked: - return prog + ": " + dev + " is in GM mode and stays in it while " + prog + - " runs, whatever SysEx it is sent"; - case PowerOnState::GMUnavailable: - return prog + ": no GM mode available on " + dev + - ", so GS was selected as the default"; - default: + auto name = [](ResetMode m) { return m == ResetMode::GM ? "GM" : "GS"; }; + if (st.asked == ResetMode::None) return ""; - } + if (st.locked == ResetMode::None) + return prog + ": neither GS nor GM mode is available on " + dev + + ", so it runs in its own default mode"; + const std::string stays = " stays in " + std::string(name(st.locked)) + + " mode while " + prog + + " runs, whatever SysEx it is sent"; + if (st.locked == st.asked) + return prog + ": " + dev + stays; + return prog + ": no " + name(st.asked) + " mode available on " + dev + + ", so " + name(st.locked) + " was selected as the default; it" + stays; } inline bool device_supported(const std::string &dev) { diff --git a/emuscd/main.cc b/emuscd/main.cc index 6d0f9b98..65826dc9 100644 --- a/emuscd/main.cc +++ b/emuscd/main.cc @@ -428,7 +428,7 @@ int main(int argc, char* argv[]) { bool stats = false; int rt_priority = 0; double gain_db = 0.0; - ResetMode reset_mode = ResetMode::GS; + ResetMode reset_mode = ResetMode::None; for (int i = 1; i < argc; i++) { std::string arg = argv[i]; @@ -490,10 +490,11 @@ int main(int argc, char* argv[]) { << "Options:\n" << " --device NAME Device to emulate (default: sc88)\n" << " Supported: sc55, sc55mkii, sc88, jv880, jv1080\n" - << " --reset MODE gs or gm (default: gs). gm keeps every device\n" - << " that has a GM mode in it while emuscd runs,\n" - << " whatever SysEx it is sent; a device without\n" - << " one runs in GS\n" + << " --reset MODE gs or gm: keep every device in that mode while\n" + << " emuscd runs, whatever SysEx it is sent; a\n" + << " device without it is kept in the other one.\n" + << " Without it, devices start in GS and the MIDI\n" + << " stream may switch them\n" << " --name NAME ALSA MIDI port name (default: emuscd)\n" << " --pcm DEVICE ALSA PCM output device (default: default)\n" << " --list-pcm List ALSA PCM devices and exit\n" diff --git a/emuscd/main_winmidi.cc b/emuscd/main_winmidi.cc index 58a07ae1..8f484284 100644 --- a/emuscd/main_winmidi.cc +++ b/emuscd/main_winmidi.cc @@ -41,10 +41,11 @@ const char *USAGE = " --device NAME Device to emulate (default: sc88)\n" " Supported: sc55, sc55mkii, sc88, jv880, jv1080\n" " --midi-in N MIDI input device index (default: 0)\n" -" --reset MODE gs or gm (default: gs). gm keeps every device that\n" -" has a GM mode in it while emusc-winmidi runs,\n" -" whatever SysEx it is sent; a device without one\n" -" runs in GS\n" +" --reset MODE gs or gm: keep every device in that mode while\n" +" emusc-winmidi runs, whatever SysEx it is sent; a\n" +" device without it is kept in the other one.\n" +" Without it, devices start in GS and the MIDI\n" +" stream may switch them\n" " --audio-api API Audio output API: auto, winmm or dsound (default:\n" " auto - DirectSound, falling back to WinMM if it\n" " cannot be opened; winmm/dsound force one, and a\n" @@ -505,7 +506,7 @@ int main(int argc, char **argv) { unsigned block = 256; unsigned latency = 20; double gainDb = 0.0; - ResetMode resetMode = ResetMode::GS; + ResetMode resetMode = ResetMode::None; for (int i = 1; i < argc; i++) { std::string a = argv[i]; diff --git a/libemusc/src/CMakeLists.txt b/libemusc/src/CMakeLists.txt index 718ff10d..e5ce3a02 100644 --- a/libemusc/src/CMakeLists.txt +++ b/libemusc/src/CMakeLists.txt @@ -457,17 +457,17 @@ if(BUILD_TESTING) add_test(NAME synth_rx_roland_only_test COMMAND synth_rx_roland_only_test) set_property(TEST synth_rx_roland_only_test PROPERTY SKIP_RETURN_CODE 77) - # has_gm_mode() and the GM lock. Needs the SC-55mkII ROMs, and checks - # has_gm_mode() on each other device whose ROMs are given. - add_executable(synth_gm_lock_test ../tests/synth_gm_lock_test.cc) - target_include_directories(synth_gm_lock_test PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}") - target_link_libraries(synth_gm_lock_test PRIVATE emusc_static) + # has_gs_mode(), has_gm_mode() and the mode lock. Needs the SC-55mkII ROMs, + # and checks both on each other device whose ROMs are given. + add_executable(synth_mode_lock_test ../tests/synth_mode_lock_test.cc) + target_include_directories(synth_mode_lock_test PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}") + target_link_libraries(synth_mode_lock_test PRIVATE emusc_static) if(MSVC) - target_compile_options(synth_gm_lock_test PRIVATE /UNDEBUG) + target_compile_options(synth_mode_lock_test PRIVATE /UNDEBUG) else() - target_compile_options(synth_gm_lock_test PRIVATE -UNDEBUG) + target_compile_options(synth_mode_lock_test PRIVATE -UNDEBUG) endif() - set_target_properties(synth_gm_lock_test PROPERTIES CXX_EXTENSIONS OFF) - add_test(NAME synth_gm_lock_test COMMAND synth_gm_lock_test) - set_property(TEST synth_gm_lock_test PROPERTY SKIP_RETURN_CODE 77) + set_target_properties(synth_mode_lock_test PROPERTIES CXX_EXTENSIONS OFF) + add_test(NAME synth_mode_lock_test COMMAND synth_mode_lock_test) + set_property(TEST synth_mode_lock_test PROPERTY SKIP_RETURN_CODE 77) endif() diff --git a/libemusc/src/engines/xp/device.cc b/libemusc/src/engines/xp/device.cc index c629cf66..c1d54b07 100644 --- a/libemusc/src/engines/xp/device.cc +++ b/libemusc/src/engines/xp/device.cc @@ -739,6 +739,12 @@ bool device_has_gm_mode(const Device *device) device->voice_ops->gm_system_on; } +bool device_has_gs_mode(const Device *device) +{ + return device && device->initialized && + xp_profile(&device->renderer.rom)->sysexModelId == 0x42; +} + bool device_host_reset_enters_gm(const Device *device) { if (!device || !device->initialized) diff --git a/libemusc/src/engines/xp/device.h b/libemusc/src/engines/xp/device.h index 85118bda..538b5f1a 100644 --- a/libemusc/src/engines/xp/device.h +++ b/libemusc/src/engines/xp/device.h @@ -238,6 +238,8 @@ void device_reset_controllers(Device *device); bool device_gm_system_on(Device *device); /* Whether device_gm_system_on() would act: the device has a GM mode. */ bool device_has_gm_mode(const Device *device); +/* Whether the device's own SysEx is GS: its model ID is GS's, 42H. */ +bool device_has_gs_mode(const Device *device); /* True on a device whose profile has a host reset enter GM mode; see * XpDeviceProfile::hostResetEntersGm. */ bool device_host_reset_enters_gm(const Device *device); diff --git a/libemusc/src/settings.cc b/libemusc/src/settings.cc index 0d8aeee8..ae8423e7 100644 --- a/libemusc/src/settings.cc +++ b/libemusc/src/settings.cc @@ -676,6 +676,12 @@ bool Settings::has_gm_mode(void) const } +bool Settings::has_gs_mode(void) const +{ + return !_ctrlRom.device()->gsScaleTuningOnly; +} + + void Settings::set_gm_mode(void) { // "Turn General MIDI System On" sets every internal parameter to the diff --git a/libemusc/src/settings.h b/libemusc/src/settings.h index 4a9d4eb2..5b1c97ab 100644 --- a/libemusc/src/settings.h +++ b/libemusc/src/settings.h @@ -120,6 +120,8 @@ class Settings void set_gm_mode(void); // Whether set_gm_mode() leaves anything different from a GS reset. bool has_gm_mode(void) const; + // Whether the device receives the GS Reset and the GS parameter map. + bool has_gs_mode(void) const; void set_map_mt32(void); // Temporary solution diff --git a/libemusc/src/synth.cc b/libemusc/src/synth.cc index 41675932..efa0a9c9 100644 --- a/libemusc/src/synth.cc +++ b/libemusc/src/synth.cc @@ -823,7 +823,7 @@ void Synth::_apply_midi_sysex(uint8_t *data, uint16_t length) return; midiMutex.lock(); - reset(SoundMap::GS_GM, true); + reset(_modeLock == ModeLock::GS ? SoundMap::GS : SoundMap::GS_GM, true); midiMutex.unlock(); return; } @@ -925,7 +925,7 @@ void Synth::_apply_midi_sysex(uint8_t *data, uint16_t length) // GM mode is GS with some receive switches off, and both a GS Reset and // a DT1 to those switches turn them back on: put them back. - if (_gmLocked) + if (_modeLock == ModeLock::GM) _settings->set_gm_mode(); } @@ -1213,6 +1213,14 @@ void Synth::panic(void) } +bool Synth::has_gs_mode(void) const +{ + if (_ctrlRom.uses_xp_engine()) + return Xp::device_has_gs_mode(_xpDevice); + return _settings->has_gs_mode(); +} + + bool Synth::has_gm_mode(void) const { if (_ctrlRom.uses_xp_engine()) @@ -1221,9 +1229,9 @@ bool Synth::has_gm_mode(void) const } -void Synth::set_gm_locked(bool locked) +void Synth::set_mode_lock(ModeLock lock) { - _gmLocked = locked; + _modeLock = lock; } diff --git a/libemusc/src/synth.h b/libemusc/src/synth.h index e86c7bbc..dae86c89 100644 --- a/libemusc/src/synth.h +++ b/libemusc/src/synth.h @@ -139,21 +139,27 @@ class Synth void reset(SoundMap sm, bool resetParts = false); + // Whether the loaded device has a GS mode: whether it receives the GS + // Reset and the GS parameter map. + bool has_gs_mode(void) const; // Whether the loaded device has a GM mode that differs from its GS one, // i.e. whether reset(SoundMap::GS_GM) leaves it anywhere a GS reset does - // not. Asks the engine, which for the SC-88 and JV-1080 exists only from - // set_audio_format() on: call it after that. + // not. + // Both ask the engine, which for the SC-88 and JV-1080 exists only from + // set_audio_format() on: call them after that. bool has_gm_mode(void) const; - // While locked, the device stays in GM mode whatever SysEx it receives: a - // GS reset resets it into GM instead of GS, and a parameter write cannot - // turn GM's receive switches back on. For a host that offers a GM-only - // mode. It does not itself enter GM: reset(SoundMap::GS_GM) does that. - // Off by default, and meaningless on a device without has_gm_mode(). The - // XP engine has no SysEx path out of GM mode, so only the GP engine needs - // and reads it. - void set_gm_locked(bool locked); - bool gm_locked(void) const { return _gmLocked; } + // A mode lock keeps the device in one mode whatever SysEx it receives, for + // a host that offers a GS-only or GM-only mode. Locked in GS, a GM System + // On resets the device into GS. Locked in GM, a GS Reset resets it into + // GM, and a parameter write cannot turn GM's receive switches back on. It + // does not itself enter the mode: reset() does that. None, the default, + // leaves the mode to the stream. A lock on a mode the device does not have + // is meaningless. The XP engine has no SysEx path from one of its modes to + // the other, so only the GP engine needs and reads it. + enum class ModeLock { None, GS, GM }; + void set_mode_lock(ModeLock lock); + ModeLock mode_lock(void) const { return _modeLock; } // Silences the device whatever it is and whatever it was sent: queued MIDI // is dropped, every voice of every part is cut, pedals are released and @@ -227,7 +233,7 @@ class Synth private: Settings *_settings = nullptr; - bool _gmLocked = false; + ModeLock _modeLock = ModeLock::None; // The SC-88 is not rendered by the Part and Note path: its engine reads the // control ROM itself and renders a stereo frame at a time off its own diff --git a/libemusc/tests/synth_gm_lock_test.cc b/libemusc/tests/synth_mode_lock_test.cc similarity index 71% rename from libemusc/tests/synth_gm_lock_test.cc rename to libemusc/tests/synth_mode_lock_test.cc index 7598e6a6..54dff00a 100644 --- a/libemusc/tests/synth_gm_lock_test.cc +++ b/libemusc/tests/synth_mode_lock_test.cc @@ -1,16 +1,18 @@ /* SPDX-License-Identifier: CC0-1.0 */ -/* Synth::has_gm_mode() and Synth::set_gm_locked(). +/* Synth::has_gs_mode(), Synth::has_gm_mode() and Synth::set_mode_lock(). * * On the SC-55mkII, GM mode is GS with Rx.BankSelect and Rx.NRPN off on every * part (Settings::set_gm_mode()), which shows as a bank select that no longer - * reaches the tone. A GS Reset turns both back on, and so can a GS DT1 to a - * part's Rx.BankSelect. Under the GM lock neither may: the GS Reset resets - * into GM instead, and the switch stays off. Each locked case is run unlocked - * first, so the test shows the lock, not the message, is what holds GM. + * reaches the tone. A GM System On enters it; a GS Reset leaves it, and so + * can a GS DT1 to a part's Rx.BankSelect. Locked in GS, the GM System On may + * not enter it; locked in GM, neither of the others may leave it. Each locked + * case is run unlocked first, so the test shows the lock, not the message, is + * what holds the mode. * - * has_gm_mode() is checked on every device whose ROMs are given: true on the - * SC-55mkII and JV-1080, false on the SC-55, JV-880 and SC-88. + * Both queries are checked on every device whose ROMs are given: + * has_gs_mode() true on the SC-55, SC-55mkII and SC-88; has_gm_mode() true on + * the SC-55mkII and JV-1080. * * SC55MKII_CONTROL_ROM, SC55MKII_CPU_ROM, SC55MKII_WAVE_ROMS * SC55_CONTROL_ROM, SC55_CPU_ROM, SC55_WAVE_ROMS @@ -39,6 +41,7 @@ namespace { using SoundMap = EmuSC::Synth::SoundMap; +using ModeLock = EmuSC::Synth::ModeLock; const uint32_t kRate = 32000; std::vector split_commas(const std::string &s) @@ -153,12 +156,12 @@ bool in_gs(EmuSC::Synth &s) rx_on(s, EmuSC::PatchParam::RxNRPN); } -std::unique_ptr power_on(Roms &r, SoundMap map, bool lock) +std::unique_ptr power_on(Roms &r, SoundMap map, ModeLock lock) { std::unique_ptr s(new EmuSC::Synth(*r.ctrl, *r.wave)); s->set_audio_format(kRate, 2); s->reset(map, true); - s->set_gm_locked(lock); + s->set_mode_lock(lock); run(*s); return s; } @@ -166,16 +169,14 @@ std::unique_ptr power_on(Roms &r, SoundMap map, bool lock) void test_sc55mkii(Roms &r) { { - std::unique_ptr s = power_on(r, SoundMap::GS, false); - assert(s->has_gm_mode()); - assert(!s->gm_locked()); + std::unique_ptr s = power_on(r, SoundMap::GS, ModeLock::None); + assert(s->has_gs_mode() && s->has_gm_mode()); + assert(s->mode_lock() == ModeLock::None); assert(in_gs(*s)); assert(bank_after_select(*s) == 8); - } - /* Unlocked: GM mode holds until a GS Reset or a DT1 undoes it. */ - { - std::unique_ptr s = power_on(r, SoundMap::GS_GM, false); + /* Unlocked: GM System On enters GM, a GS Reset or a DT1 leaves it. */ + sysex(*s, kGmOn); assert(in_gm(*s)); assert(bank_after_select(*s) == 0); @@ -190,10 +191,29 @@ void test_sc55mkii(Roms &r) assert(bank_after_select(*s) == 8); } - /* Locked: neither does. */ + /* Locked in GS: GM System On resets, but into GS. */ + { + std::unique_ptr s = power_on(r, SoundMap::GS, ModeLock::GS); + assert(s->mode_lock() == ModeLock::GS); + + const uint8_t level = s->get_param(EmuSC::PatchParam::PartLevel, 0); + sysex(*s, gs_dt1(0x40, 0x11, 0x19, { (uint8_t) (level ^ 0x40) })); + assert(s->get_param(EmuSC::PatchParam::PartLevel, 0) == (level ^ 0x40)); + sysex(*s, kGmOn); + assert(s->get_param(EmuSC::PatchParam::PartLevel, 0) == level); + assert(in_gs(*s)); + assert(bank_after_select(*s) == 8); + + /* Unlocking hands the mode back to the stream. */ + s->set_mode_lock(ModeLock::None); + sysex(*s, kGmOn); + assert(in_gm(*s)); + } + + /* Locked in GM: neither the GS Reset nor the DT1 leaves it. */ { - std::unique_ptr s = power_on(r, SoundMap::GS_GM, true); - assert(s->gm_locked()); + std::unique_ptr s = power_on(r, SoundMap::GS_GM, ModeLock::GM); + assert(s->mode_lock() == ModeLock::GM); assert(in_gm(*s)); sysex(*s, kGsReset); @@ -213,15 +233,14 @@ void test_sc55mkii(Roms &r) assert(s->get_param(EmuSC::PatchParam::PartLevel, 0) == level); assert(in_gm(*s)); - /* Unlocking hands GS back to the stream. */ - s->set_gm_locked(false); + s->set_mode_lock(ModeLock::None); sysex(*s, kGsReset); assert(in_gs(*s)); assert(bank_after_select(*s) == 8); } } -void check_has_gm_mode(const char *prefix, bool expected, int &checked) +void check_modes(const char *prefix, bool gs, bool gm, int &checked) { Roms r; if (!load(prefix, r)) @@ -229,9 +248,10 @@ void check_has_gm_mode(const char *prefix, bool expected, int &checked) EmuSC::Synth s(*r.ctrl, *r.wave); s.set_audio_format(kRate, 2); s.reset(SoundMap::GS, true); - if (s.has_gm_mode() != expected) { - std::cerr << prefix << ": has_gm_mode() is " << s.has_gm_mode() - << ", expected " << expected << std::endl; + if (s.has_gs_mode() != gs || s.has_gm_mode() != gm) { + std::cerr << prefix << ": has_gs_mode() " << s.has_gs_mode() + << ", has_gm_mode() " << s.has_gm_mode() << "; expected " + << gs << ", " << gm << std::endl; assert(false); } checked++; @@ -247,11 +267,11 @@ int main(void) test_sc55mkii(mk2); int checked = 0; - check_has_gm_mode("SC55", false, checked); - check_has_gm_mode("JV880", false, checked); - check_has_gm_mode("SC88", false, checked); - check_has_gm_mode("JV1080", true, checked); - std::cout << "has_gm_mode() checked on " << checked + check_modes("SC55", true, false, checked); + check_modes("JV880", false, false, checked); + check_modes("SC88", true, false, checked); + check_modes("JV1080", false, true, checked); + std::cout << "has_gs_mode(), has_gm_mode() checked on " << checked << " more device(s)" << std::endl; return 0; } From a740e6c513502d3c769ad90d36959702796c969a Mon Sep 17 00:00:00 2001 From: Marc Lainez Date: Mon, 19 Oct 2026 14:16:04 +0200 Subject: [PATCH 4/5] libemusc: give the SC-88 its GM mode and Rx.Bank/Rx.NRPN switches GM System On was refused on the shared firmware port, so the SC-88 had no GM mode. The firmware's handler runs the GS Reset's module reset and then turns Rx.Bank Select off where the GS Reset turns Rx.NRPN on; both switches, and Rx.Bank Select LSB, now gate CC0, CC32 and the NRPN numbers and are writable at 40 1x 23, 24 and 0a. Which devices have the mode is a profile field. Provenance: P-xxxx. Assisted-by: Claude:claude-opus-5-5 --- libemusc/src/engines/xp/device.cc | 106 ++++++++++++++++++++-- libemusc/src/engines/xp/device.h | 17 +++- libemusc/src/engines/xp/devices/profile.h | 7 ++ libemusc/src/engines/xp/devices/sc88.cc | 5 + libemusc/tests/device_test.cc | 88 ++++++++++++++++++ 5 files changed, 214 insertions(+), 9 deletions(-) diff --git a/libemusc/src/engines/xp/device.cc b/libemusc/src/engines/xp/device.cc index c1d54b07..de22b07a 100644 --- a/libemusc/src/engines/xp/device.cc +++ b/libemusc/src/engines/xp/device.cc @@ -388,6 +388,17 @@ bool sysexWrite(Device *device, uint8_t port, uint32_t address, which of them the device keeps is not established. */ for (unsigned part = 0; part < XP_ENGINE_PART_COUNT; ++part) engine_all_sound_off(&device->engine, (uint8_t)part); + /* @provenance class=FW-STRUCT devices=SC-88 ref=0x37a8,0x12a10,0x129dd,0x13184 + Both candidates for this address's handler run the module reset, + which copies the part image at 0x13184 with +18 = 00, so Rx.Bank + Select and its LSB come back on, as the manual says (om-125). What + they leave Rx.NRPN at differs - `0x12a10` turns it on, `0x129dd` + off again afterwards - and which one this address reaches is not + established, so Rx.NRPN is left as it was. */ + for (unsigned part = 0; part < XP_ENGINE_PART_COUNT; ++part) { + device->channels[part].rx_bank_select = true; + device->channels[part].rx_bank_select_lsb = true; + } return true; } if (address == 0x40007f) { @@ -586,6 +597,38 @@ bool sysexWrite(Device *device, uint8_t port, uint32_t address, blockPart((uint8_t)((address >> 8) & 0x0f), port, &part)) { ChannelState *state = device->channels + part; switch (address & 0xffu) { + case 0x0a: + /* @provenance class=FW-EXACT devices=SC-88 ref=0x13854,0x456b,0x360b + Rx.NRPN: entry 0a of the `40 1x` table at 0x13804 names the + switch-word writer `456b` and bit 15. Turning it off also runs + `360b`, which leaves no NRPN selected. The handler takes any + nonzero value as on; values above the manual's 00..01 are refused + here, as this file refuses other out-of-range writes. */ + if (value > 1) + return false; + state->rx_nrpn = value != 0; + if (!state->rx_nrpn) { + state->nrpn_msb = 127; + state->nrpn_lsb = 127; + } + return true; + case 0x23: + case 0x24: + /* @provenance class=FW-EXACT devices=SC-88 ref=0x1391c,0x13924,0x4825 + Rx.Bank Select and Rx.Bank Select LSB. Both table entries name + `4825`, which sets the part's receive-off bit for 00 and clears + it otherwise; setting either bit also clears the stored CC0 + value at `d860 + 2*part`. Values above 01 are refused, as for + Rx.NRPN. */ + if (value > 1) + return false; + if ((address & 0xffu) == 0x23u) + state->rx_bank_select = value != 0; + else + state->rx_bank_select_lsb = value != 0; + if (value == 0) + state->variation = 0; + return true; case 0x14: if (value > 2) return false; @@ -727,10 +770,29 @@ void device_destroy(Device *device) bool device_gm_system_on(Device *device) { - if (!device || !device->initialized || !device->voice_ops || - !device->voice_ops->gm_system_on) + if (!device || !device->initialized) + return false; + if (device->voice_ops) { + if (!device->voice_ops->gm_system_on) + return false; + return device->voice_ops->gm_system_on(device->voice_state); + } + if (!xp_profile(&device->renderer.rom)->firmwarePortGmMode) return false; - return device->voice_ops->gm_system_on(device->voice_state); + /* @provenance class=FW-EXACT devices=SC-88 ref=0x128c6,0x128fb,0x1291a,0x129a1,0x129c0,0x13184 + GM System On runs the same module reset a GS Reset does (`0x12aa1`, + `0x12ac5`, from the part image at 0x13184) and differs from it in two + bits per part: it sets +18 bit 0, Rx.Bank Select off, where the GS + Reset instead sets +2 bit 15, Rx.NRPN on, which the image leaves off. + Rx.Bank Select LSB stays on, but CC32 is refused while bit 0 is set. + The firmware's reset reaches one module in MODE-2 and both in MODE-1; + this reset, like the GS Reset here, reaches every part. */ + device_reset_controllers(device); + for (unsigned part = 0; part < XP_ENGINE_PART_COUNT; ++part) { + device->channels[part].rx_bank_select = false; + device->channels[part].rx_nrpn = false; + } + return true; } bool device_has_gm_mode(const Device *device) @@ -879,6 +941,11 @@ void device_reset_controllers(Device *device) channel->rpn_lsb = 127; channel->nrpn_msb = 127; channel->nrpn_lsb = 127; + /* The part image at 0x13184 has +18 = 00, both bank switches on; the + GS Reset's `0x129a1`/`0x129c0` then set Rx.NRPN, +2 bit 15. */ + channel->rx_bank_select = true; + channel->rx_bank_select_lsb = true; + channel->rx_nrpn = true; channel->same_note_mode = XP_SAME_NOTE_LIMITED_MULTI; /* GS puts the rhythm part on MIDI channel 10, i.e. part 9 of each port, and a GS reset restores exactly that. It plays from drum setup MAP1: @@ -979,11 +1046,28 @@ bool device_sysex(Device *device, uint8_t port, if (device->voice_ops && size == 4u && data[0] == 0x7e && data[1] == 0x7f && data[2] == 0x09 && data[3] == 0x01) return device_gm_system_on(device); + const struct XpDeviceProfile *profile = xp_profile(&device->renderer.rom); + /* @provenance class=FW-EXACT devices=SC-88 ref=0x0e70,0x17e2,0x128c6,0x1296c + The same message on a device the firmware port serves, when its + firmware has a GM mode. `0x0e70` compares 7F and 09 and nothing else + of the header, so here too only the broadcast ID is received; `0x17e2` + then acts on two sub-IDs: 01, GM System On, and 02, which runs the + GS Reset's own module reset (`0x1296c`). Both are gated by the panel's + Rx GM On switch, which this device does not model and leaves on. */ + if (!device->voice_ops && profile->firmwarePortGmMode && size == 4u && + data[0] == 0x7e && data[1] == 0x7f && data[2] == 0x09) { + if (data[3] == 0x01) + return device_gm_system_on(device); + if (data[3] == 0x02) { + device_reset_controllers(device); + return true; + } + return false; + } /* 41 dev 12, the device's own address bytes, at least one data byte, checksum. The model id and the address width are the device's, so they come from its profile: three bytes on a GS device, four on this family's JV member. */ - const struct XpDeviceProfile *profile = xp_profile(&device->renderer.rom); const size_t addressBytes = profile->sysexAddressBytes; if (addressBytes < 1u || addressBytes > 4u) return false; @@ -1100,6 +1184,9 @@ bool device_midi(Device *device, uint8_t port, uint8_t status, case 0xb0: switch (data1) { case 0: + /* `3172`: refused while Rx.Bank Select is off. */ + if (!state->rx_bank_select) + return true; state->variation = data2; return true; case 1: @@ -1257,6 +1344,9 @@ bool device_midi(Device *device, uint8_t port, uint8_t status, * resolve no tone at all, which is a silenced part rather than a * changed one; this refuses the message instead, and no corpus file * sends one. */ + /* `317c`: refused while either bank switch is off. */ + if (!state->rx_bank_select || !state->rx_bank_select_lsb) + return true; if (data2 > 2) return false; state->tone_map_forced = data2; @@ -1272,13 +1362,17 @@ bool device_midi(Device *device, uint8_t port, uint8_t status, case 66: engine_sostenuto(&device->engine, part, data2 >= 64); return true; + /* `3253` and `3261` leave RPN unselected whatever Rx.NRPN says, and + store the NRPN number only while it is on. */ case 98: - state->nrpn_lsb = data2; + if (state->rx_nrpn) + state->nrpn_lsb = data2; state->rpn_msb = 127; state->rpn_lsb = 127; return true; case 99: - state->nrpn_msb = data2; + if (state->rx_nrpn) + state->nrpn_msb = data2; state->rpn_msb = 127; state->rpn_lsb = 127; return true; diff --git a/libemusc/src/engines/xp/device.h b/libemusc/src/engines/xp/device.h index 538b5f1a..da432807 100644 --- a/libemusc/src/engines/xp/device.h +++ b/libemusc/src/engines/xp/device.h @@ -138,6 +138,16 @@ struct ChannelState { uint8_t rpn_lsb; uint8_t nrpn_msb; uint8_t nrpn_lsb; + /* The part's Rx.Bank Select, Rx.Bank Select LSB and Rx.NRPN switches + (`40 1x 23`, `24`, `0a`). The firmware keeps the first two inverted, + as receive-off bits 0 and 1 of part-structure +18, and the third as + receive-on bit 15 of the switch word at +2. CC0 is stored only with + bit 0 clear (`3172`), CC32 only with both clear (`317c`), and the + NRPN numbers only with bit 15 set (`3253`, `3261`). GM System On is + what turns bank select and NRPN off. */ + bool rx_bank_select; + bool rx_bank_select_lsb; + bool rx_nrpn; /* The controller destination matrix, `40 2x ss`: six source groups in the published order, eleven depths each. Only the cutoff column is applied so far; the rest are held so a song's settings are not lost, @@ -232,9 +242,10 @@ bool device_init_decoded( enum xp_fractional_wrap wrap); void device_destroy(Device *device); void device_reset_controllers(Device *device); -/* The GM System On a device with its own voice path acts on, as if the - * message had arrived. False, with nothing changed, on a device that has - * no GM mode - which includes every device on the shared firmware port. */ +/* GM System On, as if the message had arrived: through the device's own + * voice path when it has one, or through the shared firmware port when the + * profile's firmwarePortGmMode says the device has a GM mode there. False, + * with nothing changed, on a device that has no GM mode. */ bool device_gm_system_on(Device *device); /* Whether device_gm_system_on() would act: the device has a GM mode. */ bool device_has_gm_mode(const Device *device); diff --git a/libemusc/src/engines/xp/devices/profile.h b/libemusc/src/engines/xp/devices/profile.h index 9e9c9ae1..6ff00df6 100644 --- a/libemusc/src/engines/xp/devices/profile.h +++ b/libemusc/src/engines/xp/devices/profile.h @@ -780,6 +780,13 @@ struct XpDeviceProfile { uint8_t sysexModelId; uint8_t sysexAddressBytes; + /* True on a device the shared firmware-port engine serves whose firmware + has a GM mode: the universal GM System On, 7E 7F 09 01, is received + and puts every part in it. A device with its own voice path says the + same through XpVoiceEngineOps::gm_system_on instead, and leaves this + false. */ + bool firmwarePortGmMode; + /* How far one voice sits below the mix's full scale. A device that sums many voices into a fixed-width mixer needs headroom for their sum, and on a device whose mixer is not recovered this is the one number that diff --git a/libemusc/src/engines/xp/devices/sc88.cc b/libemusc/src/engines/xp/devices/sc88.cc index 5fe78c01..aeee5396 100644 --- a/libemusc/src/engines/xp/devices/sc88.cc +++ b/libemusc/src/engines/xp/devices/sc88.cc @@ -308,6 +308,11 @@ const struct XpDeviceProfile SC88_PROFILE = { .sysexModelId = 0x42u, .sysexAddressBytes = 3u, + /* The universal non-real-time handler at 0x0e70 takes 7E 7F 09 and + hands the sub-ID on; 0x17e2 sends 01 to the GM System On at 0x128c6. + scdb 03_disassembly/sysex_handlers.md. */ + .firmwarePortGmMode = true, + /* This device's voice path is the shared firmware port in engine.h and renderer.h, which is what a null here selects. */ .voiceEngine = nullptr, diff --git a/libemusc/tests/device_test.cc b/libemusc/tests/device_test.cc index a0ee33e4..18ec8019 100644 --- a/libemusc/tests/device_test.cc +++ b/libemusc/tests/device_test.cc @@ -448,6 +448,94 @@ int main(void) sizeof selected_bad)); assert(device.channels[0].tone_map_selected == XP_TONE_MAP_SC55); } + /* GM System On: bank select and NRPN stop being received, a GS Reset + brings both back, and the part's selected tone map - SC-55 since the + block above - survives every one of these resets. */ + { + static const uint8_t gm_on[] = { 0xf0, 0x7e, 0x7f, 0x09, 0x01, 0xf7 }; + static const uint8_t gm_on_own_id[] = { 0x7e, 0x10, 0x09, 0x01 }; + static const uint8_t gm_off[] = { 0x7e, 0x7f, 0x09, 0x02 }; + EmuSC::Xp::ChannelState *ch = &device.channels[0]; + const uint8_t cutoff_reset = 64; + + /* What a part receives before: CC0, CC32 and an NRPN cutoff. */ + assert(EmuSC::Xp::device_midi(&device, 0, 0xb0, 0, 5)); + assert(ch->variation == 5); + assert(EmuSC::Xp::device_midi(&device, 0, 0xb0, 99, 0x01)); + assert(EmuSC::Xp::device_midi(&device, 0, 0xb0, 98, 0x20)); + assert(EmuSC::Xp::device_midi(&device, 0, 0xb0, 6, 10)); + assert(ch->cutoff == 10); + + /* `0x0e70` compares the device ID with 7F only. */ + assert(!EmuSC::Xp::device_sysex(&device, 0, gm_on_own_id, + sizeof gm_on_own_id)); + assert(ch->rx_bank_select && ch->variation == 5); + + assert(EmuSC::Xp::device_sysex(&device, 0, gm_on, sizeof gm_on)); + assert(!ch->rx_bank_select && ch->rx_bank_select_lsb && !ch->rx_nrpn); + assert(!device.channels[31].rx_bank_select && !device.channels[31].rx_nrpn); + assert(ch->variation == 0 && ch->cutoff == cutoff_reset); + assert(ch->tone_map_selected == XP_TONE_MAP_SC55); + assert(device.engine.parts[0].tone_map == XP_TONE_MAP_SC55); + + assert(EmuSC::Xp::device_midi(&device, 0, 0xb0, 0, 8)); + assert(ch->variation == 0); + assert(EmuSC::Xp::device_midi(&device, 0, 0xb0, 32, 2)); + assert(ch->tone_map_forced == 0); + assert(device.engine.parts[0].tone_map == XP_TONE_MAP_SC55); + assert(EmuSC::Xp::device_midi(&device, 0, 0xb0, 99, 0x01)); + assert(EmuSC::Xp::device_midi(&device, 0, 0xb0, 98, 0x20)); + assert(ch->nrpn_msb == 127 && ch->nrpn_lsb == 127); + EmuSC::Xp::device_midi(&device, 0, 0xb0, 6, 10); + assert(ch->cutoff == cutoff_reset); + + /* GS Reset turns both switches back on. */ + assert(dt1(&device, 0x40, 0x00, 0x7f, 0x00)); + assert(ch->rx_bank_select && ch->rx_bank_select_lsb && ch->rx_nrpn); + assert(ch->tone_map_selected == XP_TONE_MAP_SC55); + assert(EmuSC::Xp::device_midi(&device, 0, 0xb0, 0, 8)); + assert(ch->variation == 8); + assert(EmuSC::Xp::device_midi(&device, 0, 0xb0, 32, 2)); + assert(device.engine.parts[0].tone_map == XP_TONE_MAP_SC88); + assert(EmuSC::Xp::device_midi(&device, 0, 0xb0, 99, 0x01)); + assert(EmuSC::Xp::device_midi(&device, 0, 0xb0, 98, 0x20)); + assert(EmuSC::Xp::device_midi(&device, 0, 0xb0, 6, 10)); + assert(ch->cutoff == 10); + + /* The same mode reached without the message, and left by sub-ID 02, + which `0x17e2` sends to the GS Reset's own module reset. */ + assert(EmuSC::Xp::device_gm_system_on(&device)); + assert(!ch->rx_bank_select && !ch->rx_nrpn); + assert(EmuSC::Xp::device_sysex(&device, 0, gm_off, sizeof gm_off)); + assert(ch->rx_bank_select && ch->rx_nrpn); + + /* The switches themselves, `40 1x 23`, `24` and `0a`; block 1 is + part 1. Turning a bank switch off also drops the stored CC0. */ + assert(EmuSC::Xp::device_midi(&device, 0, 0xb0, 0, 8)); + assert(dt1(&device, 0x40, 0x11, 0x24, 0x00)); + assert(ch->rx_bank_select && !ch->rx_bank_select_lsb); + assert(ch->variation == 0); + assert(EmuSC::Xp::device_midi(&device, 0, 0xb0, 0, 8)); + assert(ch->variation == 8); + assert(EmuSC::Xp::device_midi(&device, 0, 0xb0, 32, 1)); + assert(ch->tone_map_forced == 0); + assert(dt1(&device, 0x40, 0x11, 0x24, 0x01)); + assert(dt1(&device, 0x40, 0x11, 0x23, 0x00)); + assert(!ch->rx_bank_select && ch->variation == 0); + assert(dt1(&device, 0x40, 0x11, 0x23, 0x01)); + assert(ch->rx_bank_select); + assert(EmuSC::Xp::device_midi(&device, 0, 0xb0, 99, 0x01)); + assert(dt1(&device, 0x40, 0x11, 0x0a, 0x00)); + assert(!ch->rx_nrpn && ch->nrpn_msb == 127); + assert(dt1(&device, 0x40, 0x11, 0x0a, 0x01)); + assert(ch->rx_nrpn); + + /* System Mode Set turns the bank switches back on. */ + assert(EmuSC::Xp::device_gm_system_on(&device)); + assert(dt1(&device, 0x00, 0x00, 0x7f, 0x00)); + assert(ch->rx_bank_select && ch->rx_bank_select_lsb); + assert(dt1(&device, 0x40, 0x00, 0x7f, 0x00)); + } EmuSC::Xp::device_destroy(&device); free(chip); free(control); From 7788745042118261b2576879b62fbe02655b24b1 Mon Sep 17 00:00:00 2001 From: Marc Lainez Date: Mon, 19 Oct 2026 14:30:47 +0200 Subject: [PATCH 5/5] libemusc: report the SC-88's GM mode and hold its mode lock device_has_gm_mode() now follows the profile's firmwarePortGmMode, so Synth::has_gm_mode() is true on the SC-88 and --reset gm enters GM there instead of falling back to GS. With both modes reachable by SysEx, the lock has to hold on the XP path too: locked in GS a GM System On does a GS Reset, locked in GM Rx.Bank Select and Rx.NRPN are turned off again after every message. Provenance: P-xxxx. Assisted-by: Claude:claude-opus-5-5 --- emuscd/CMakeLists.txt | 8 ++ emuscd/tests/power_on_test.cc | 128 +++++++++++++++++++++++++ libemusc/src/CMakeLists.txt | 4 +- libemusc/src/engines/xp/device.cc | 15 ++- libemusc/src/engines/xp/device.h | 5 + libemusc/src/synth.cc | 31 +++++- libemusc/src/synth.h | 4 +- libemusc/tests/synth_mode_lock_test.cc | 110 +++++++++++++++++++-- 8 files changed, 291 insertions(+), 14 deletions(-) create mode 100644 emuscd/tests/power_on_test.cc diff --git a/emuscd/CMakeLists.txt b/emuscd/CMakeLists.txt index 5c05a552..73ed37c1 100644 --- a/emuscd/CMakeLists.txt +++ b/emuscd/CMakeLists.txt @@ -22,4 +22,12 @@ if(BUILD_TESTING) set_target_properties(midi_port_match_test PROPERTIES CXX_STANDARD 17 CXX_EXTENSIONS OFF) target_compile_options(midi_port_match_test PRIVATE -UNDEBUG) add_test(NAME midi_port_match_test COMMAND midi_port_match_test) + + # power_on() on the SC-88: --reset gm enters GM mode. Needs the SC-88 ROMs. + add_executable(power_on_test tests/power_on_test.cc) + target_link_libraries(power_on_test PRIVATE emusc_static) + set_target_properties(power_on_test PROPERTIES CXX_STANDARD 17 CXX_EXTENSIONS OFF) + target_compile_options(power_on_test PRIVATE -UNDEBUG) + add_test(NAME power_on_test COMMAND power_on_test) + set_property(TEST power_on_test PROPERTY SKIP_RETURN_CODE 77) endif() diff --git a/emuscd/tests/power_on_test.cc b/emuscd/tests/power_on_test.cc new file mode 100644 index 00000000..3273bafd --- /dev/null +++ b/emuscd/tests/power_on_test.cc @@ -0,0 +1,128 @@ +/* SPDX-License-Identifier: CC0-1.0 */ + +/* power_on(), what emuscd and emusc-winmidi do with --reset, on the SC-88: + * --reset gm locks it in GM and enters GM, --reset gs locks it in GS. In GM + * mode Rx.NRPN is off, so an NRPN no longer changes the sound. + * + * SC88_CONTROL_ROM the control ROM + * SC88_WAVE_ROMS the four wave chips, comma separated, in chip order + * + * Without them the test reports skipped. + */ +#include "../common.h" +#include "../../libemusc/src/control_rom.h" +#include "../../libemusc/src/wave_rom.h" + +#include +#ifdef NDEBUG +#error "this test is assertion-driven; NDEBUG compiles it away" +#endif +#include +#include +#include +#include +#include +#include + +using namespace emuscd; + +namespace { + +const uint32_t kRate = 32000; + +void run(EmuSC::Synth &synth, unsigned frames) +{ + float l, r; + for (unsigned i = 0; i < frames; i++) + synth.get_next_frame(l, r); +} + +/* Energy of a note on channel 1 over its first 8192 frames. */ +double note_energy(EmuSC::Synth &synth) +{ + float l, r; + double e = 0.0; + synth.midi_input(0x90, 60, 100); + for (unsigned i = 0; i < 8192; i++) { + synth.get_next_frame(l, r); + e += std::fabs(l) + std::fabs(r); + } + synth.midi_input(0x80, 60, 0); + run(synth, kRate); + return e; +} + +/* NRPN 01 63, the part's attack time, on channel 1. */ +void nrpn_attack(EmuSC::Synth &synth, uint8_t value) +{ + synth.midi_input(0xb0, 99, 0x01); + synth.midi_input(0xb0, 98, 0x63); + synth.midi_input(0xb0, 6, value); + synth.midi_input(0xb0, 101, 127); + synth.midi_input(0xb0, 100, 127); + run(synth, kRate / 10); +} + +/* Whether part 1 receives NRPN: the slowest attack leaves the note's opening + well below what it is at the default. */ +bool rx_nrpn(EmuSC::Synth &synth) +{ + const double normal = note_energy(synth); + assert(normal > 0.0); + nrpn_attack(synth, 127); + const double slow = note_energy(synth); + nrpn_attack(synth, 64); + return slow < 0.5 * normal; +} + +std::vector split_commas(const std::string &s) +{ + std::vector out; + std::string::size_type start = 0; + for (;;) { + std::string::size_type comma = s.find(',', start); + out.push_back(s.substr(start, comma == std::string::npos + ? std::string::npos : comma - start)); + if (comma == std::string::npos) + return out; + start = comma + 1; + } +} + +} // namespace + +int main(void) +{ + const char *control = getenv("SC88_CONTROL_ROM"); + const char *wave = getenv("SC88_WAVE_ROMS"); + if (!control || !wave) + return 77; + EmuSC::ControlRom ctrl(control, ""); + EmuSC::WaveRom waves(split_commas(wave), ctrl); + + { + EmuSC::Synth synth(ctrl, waves); + synth.set_audio_format(kRate, 2); + const PowerOnState st = power_on(synth, ResetMode::GM); + assert(st.locked == ResetMode::GM); + assert(synth.mode_lock() == EmuSC::Synth::ModeLock::GM); + assert(power_on_message(st, "emuscd", "sc88") == + "emuscd: sc88 stays in GM mode while emuscd runs, whatever SysEx " + "it is sent"); + run(synth, kRate / 10); + assert(!rx_nrpn(synth)); + } + + { + EmuSC::Synth synth(ctrl, waves); + synth.set_audio_format(kRate, 2); + const PowerOnState st = power_on(synth, ResetMode::GS); + assert(st.locked == ResetMode::GS); + assert(synth.mode_lock() == EmuSC::Synth::ModeLock::GS); + run(synth, kRate / 10); + assert(rx_nrpn(synth)); + } + + std::cout << "--reset gm and --reset gs checked on the SC-88" << std::endl; + return 0; +} diff --git a/libemusc/src/CMakeLists.txt b/libemusc/src/CMakeLists.txt index e5ce3a02..2bb1fa63 100644 --- a/libemusc/src/CMakeLists.txt +++ b/libemusc/src/CMakeLists.txt @@ -457,8 +457,8 @@ if(BUILD_TESTING) add_test(NAME synth_rx_roland_only_test COMMAND synth_rx_roland_only_test) set_property(TEST synth_rx_roland_only_test PROPERTY SKIP_RETURN_CODE 77) - # has_gs_mode(), has_gm_mode() and the mode lock. Needs the SC-55mkII ROMs, - # and checks both on each other device whose ROMs are given. + # has_gs_mode(), has_gm_mode() and the mode lock. Needs the SC-55mkII or + # SC-88 ROMs, and checks both on each other device whose ROMs are given. add_executable(synth_mode_lock_test ../tests/synth_mode_lock_test.cc) target_include_directories(synth_mode_lock_test PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}") target_link_libraries(synth_mode_lock_test PRIVATE emusc_static) diff --git a/libemusc/src/engines/xp/device.cc b/libemusc/src/engines/xp/device.cc index de22b07a..96083f67 100644 --- a/libemusc/src/engines/xp/device.cc +++ b/libemusc/src/engines/xp/device.cc @@ -788,6 +788,14 @@ bool device_gm_system_on(Device *device) The firmware's reset reaches one module in MODE-2 and both in MODE-1; this reset, like the GS Reset here, reaches every part. */ device_reset_controllers(device); + return device_gm_receive_off(device); +} + +bool device_gm_receive_off(Device *device) +{ + if (!device || !device->initialized || device->voice_ops || + !xp_profile(&device->renderer.rom)->firmwarePortGmMode) + return false; for (unsigned part = 0; part < XP_ENGINE_PART_COUNT; ++part) { device->channels[part].rx_bank_select = false; device->channels[part].rx_nrpn = false; @@ -797,8 +805,11 @@ bool device_gm_system_on(Device *device) bool device_has_gm_mode(const Device *device) { - return device && device->initialized && device->voice_ops && - device->voice_ops->gm_system_on; + if (!device || !device->initialized) + return false; + if (device->voice_ops) + return device->voice_ops->gm_system_on != nullptr; + return xp_profile(&device->renderer.rom)->firmwarePortGmMode; } bool device_has_gs_mode(const Device *device) diff --git a/libemusc/src/engines/xp/device.h b/libemusc/src/engines/xp/device.h index da432807..4594f2e7 100644 --- a/libemusc/src/engines/xp/device.h +++ b/libemusc/src/engines/xp/device.h @@ -247,6 +247,11 @@ void device_reset_controllers(Device *device); * profile's firmwarePortGmMode says the device has a GM mode there. False, * with nothing changed, on a device that has no GM mode. */ bool device_gm_system_on(Device *device); +/* The receive switches GM System On turns off on the firmware port, Rx.Bank + * Select and Rx.NRPN on every part, turned off without the reset that comes + * before them. False, with nothing changed, on a device without a GM mode + * there. */ +bool device_gm_receive_off(Device *device); /* Whether device_gm_system_on() would act: the device has a GM mode. */ bool device_has_gm_mode(const Device *device); /* Whether the device's own SysEx is GS: its model ID is GS's, 42H. */ diff --git a/libemusc/src/synth.cc b/libemusc/src/synth.cc index efa0a9c9..8c301c70 100644 --- a/libemusc/src/synth.cc +++ b/libemusc/src/synth.cc @@ -957,7 +957,7 @@ int Synth::get_next_frame(float &lOut, float &rOut) _eventQueue.front().applyAt <= _framesDelivered) { PendingEvent &e = _eventQueue.front(); if (e.isSysEx) - Xp::device_sysex(_xpDevice, e.port, e.sysex.data(), e.sysex.size()); + _xp_sysex(e.port, e.sysex.data(), e.sysex.size()); else Xp::device_midi(_xpDevice, e.port, e.status, e.data1, e.data2); _eventQueue.pop_front(); @@ -1019,7 +1019,7 @@ int Synth::get_next_frames(float *out, size_t frames) _eventQueue.front().applyAt <= _framesDelivered) { PendingEvent &e = _eventQueue.front(); if (e.isSysEx) - Xp::device_sysex(_xpDevice, e.port, e.sysex.data(), e.sysex.size()); + _xp_sysex(e.port, e.sysex.data(), e.sysex.size()); else Xp::device_midi(_xpDevice, e.port, e.status, e.data1, e.data2); _eventQueue.pop_front(); @@ -1235,6 +1235,33 @@ void Synth::set_mode_lock(ModeLock lock) } +// A SysEx to the XP engine, under the mode lock. Only a device with both modes +// has a SysEx path from one to the other, and on it GM mode is GS with Rx.Bank +// Select and Rx.NRPN off, as on the GP engine. Locked in GS, a GM System On +// does what a GS Reset does; locked in GM, whatever a message turned back on +// is turned off again, so a GS Reset resets into GM. +void Synth::_xp_sysex(uint8_t port, const uint8_t *data, size_t size) +{ + const bool bothModes = _modeLock != ModeLock::None && + Xp::device_has_gs_mode(_xpDevice) && + Xp::device_has_gm_mode(_xpDevice); + // The message with or without its framing bytes, as Xp::device_sysex() + // takes it. + const uint8_t *body = data; + size_t n = size; + if (n && body[0] == 0xf0) { ++body; --n; } + if (n && body[n - 1] == 0xf7) --n; + if (bothModes && _modeLock == ModeLock::GS && n == 4 && body[0] == 0x7e && + body[1] == 0x7f && body[2] == 0x09 && body[3] == 0x01) { + Xp::device_reset_controllers(_xpDevice); + return; + } + Xp::device_sysex(_xpDevice, port, data, size); + if (bothModes && _modeLock == ModeLock::GM) + Xp::device_gm_receive_off(_xpDevice); +} + + void Synth::set_part_instrument(uint8_t partId, uint8_t index, uint8_t bank) { _parts[partId].set_program(index, bank, true); diff --git a/libemusc/src/synth.h b/libemusc/src/synth.h index dae86c89..2b48c05d 100644 --- a/libemusc/src/synth.h +++ b/libemusc/src/synth.h @@ -155,8 +155,7 @@ class Synth // GM, and a parameter write cannot turn GM's receive switches back on. It // does not itself enter the mode: reset() does that. None, the default, // leaves the mode to the stream. A lock on a mode the device does not have - // is meaningless. The XP engine has no SysEx path from one of its modes to - // the other, so only the GP engine needs and reads it. + // is meaningless, so only a device with both modes reads it. enum class ModeLock { None, GS, GM }; void set_mode_lock(ModeLock lock); ModeLock mode_lock(void) const { return _modeLock; } @@ -349,6 +348,7 @@ class Synth void _apply_midi(uint8_t status, uint8_t data1, uint8_t data2, int startDelay); void _apply_midi_sysex(uint8_t *data, uint16_t length); + void _xp_sysex(uint8_t port, const uint8_t *data, size_t size); int _partials_in_use(void); int _steal_partials(Gp::Part &requester); int _steal_partial_jv(Gp::Part &requester); diff --git a/libemusc/tests/synth_mode_lock_test.cc b/libemusc/tests/synth_mode_lock_test.cc index 54dff00a..99d061ac 100644 --- a/libemusc/tests/synth_mode_lock_test.cc +++ b/libemusc/tests/synth_mode_lock_test.cc @@ -10,9 +10,14 @@ * case is run unlocked first, so the test shows the lock, not the message, is * what holds the mode. * + * On the SC-88 GM mode is the same two switches off, held in its own engine, + * where a parameter read cannot reach them; an NRPN that no longer changes + * the sound shows Rx.NRPN off. The same messages and locks are run there, + * with a DT1 to Rx.NRPN in place of the one to Rx.BankSelect. + * * Both queries are checked on every device whose ROMs are given: * has_gs_mode() true on the SC-55, SC-55mkII and SC-88; has_gm_mode() true on - * the SC-55mkII and JV-1080. + * the SC-55mkII, SC-88 and JV-1080. * * SC55MKII_CONTROL_ROM, SC55MKII_CPU_ROM, SC55MKII_WAVE_ROMS * SC55_CONTROL_ROM, SC55_CPU_ROM, SC55_WAVE_ROMS @@ -21,7 +26,7 @@ * JV1080_CONTROL_ROM, JV1080_WAVE_ROMS * * The wave ROMs are comma separated, in chip order. Without the SC-55mkII's - * the test reports skipped. + * and the SC-88's the test reports skipped. */ #include "synth.h" @@ -29,6 +34,7 @@ #include "wave_rom.h" #include +#include #ifdef NDEBUG #error "this test is assertion-driven; NDEBUG compiles it away" #endif @@ -240,6 +246,93 @@ void test_sc55mkii(Roms &r) } } +/* Energy of a note on channel 1 over its first 8192 frames, released and + rendered out. */ +double note_energy(EmuSC::Synth &synth) +{ + float l, r; + double e = 0.0; + synth.midi_input(0x90, 60, 100); + for (unsigned i = 0; i < 8192; i++) { + synth.get_next_frame(l, r); + e += std::fabs(l) + std::fabs(r); + } + synth.midi_input(0x80, 60, 0); + run(synth, kRate); + return e; +} + +/* NRPN 01 63, the part's attack time, on channel 1. */ +void nrpn_attack(EmuSC::Synth &synth, uint8_t value) +{ + synth.midi_input(0xb0, 99, 0x01); + synth.midi_input(0xb0, 98, 0x63); + synth.midi_input(0xb0, 6, value); + synth.midi_input(0xb0, 101, 127); + synth.midi_input(0xb0, 100, 127); + run(synth); +} + +/* Whether the SC-88 receives NRPN on part 1: the slowest attack leaves the + note's opening well below what it is at the default. The attack is put + back afterwards. */ +bool sc88_rx_nrpn(EmuSC::Synth &synth) +{ + const double normal = note_energy(synth); + assert(normal > 0.0); + nrpn_attack(synth, 127); + const double slow = note_energy(synth); + nrpn_attack(synth, 64); + return slow < 0.5 * normal; +} + +void test_sc88(Roms &r) +{ + { + std::unique_ptr s = power_on(r, SoundMap::GS, ModeLock::None); + assert(s->has_gs_mode() && s->has_gm_mode()); + assert(sc88_rx_nrpn(*s)); + + /* Unlocked: GM System On enters GM, a GS Reset leaves it. */ + sysex(*s, kGmOn); + assert(!sc88_rx_nrpn(*s)); + sysex(*s, kGsReset); + assert(sc88_rx_nrpn(*s)); + } + + /* A GM reset from the host enters it, which is what --reset gm does. */ + { + std::unique_ptr s = power_on(r, SoundMap::GS_GM, ModeLock::None); + assert(!sc88_rx_nrpn(*s)); + } + + /* Locked in GS: GM System On resets, but into GS. */ + { + std::unique_ptr s = power_on(r, SoundMap::GS, ModeLock::GS); + sysex(*s, kGmOn); + assert(sc88_rx_nrpn(*s)); + + s->set_mode_lock(ModeLock::None); + sysex(*s, kGmOn); + assert(!sc88_rx_nrpn(*s)); + } + + /* Locked in GM: neither a GS Reset nor a DT1 to Rx.NRPN (40 11 0A) turns + it back on. */ + { + std::unique_ptr s = power_on(r, SoundMap::GS_GM, ModeLock::GM); + assert(!sc88_rx_nrpn(*s)); + sysex(*s, kGsReset); + assert(!sc88_rx_nrpn(*s)); + sysex(*s, gs_dt1(0x40, 0x11, 0x0a, { 0x01 })); + assert(!sc88_rx_nrpn(*s)); + + s->set_mode_lock(ModeLock::None); + sysex(*s, kGsReset); + assert(sc88_rx_nrpn(*s)); + } +} + void check_modes(const char *prefix, bool gs, bool gm, int &checked) { Roms r; @@ -261,15 +354,20 @@ void check_modes(const char *prefix, bool gs, bool gm, int &checked) int main(void) { - Roms mk2; - if (!load("SC55MKII", mk2)) + Roms mk2, sc88; + const bool haveMk2 = load("SC55MKII", mk2); + const bool haveSc88 = load("SC88", sc88); + if (!haveMk2 && !haveSc88) return 77; - test_sc55mkii(mk2); + if (haveMk2) + test_sc55mkii(mk2); + if (haveSc88) + test_sc88(sc88); int checked = 0; check_modes("SC55", true, false, checked); check_modes("JV880", false, false, checked); - check_modes("SC88", true, false, checked); + check_modes("SC88", true, true, checked); check_modes("JV1080", false, true, checked); std::cout << "has_gs_mode(), has_gm_mode() checked on " << checked << " more device(s)" << std::endl;