diff --git a/README.md b/README.md index d13a990..f89ca1c 100644 --- a/README.md +++ b/README.md @@ -478,6 +478,8 @@ Common options: ``` --device NAME Device to emulate (default: sc88); sc55, sc55mkii, sc88, jv880, jv1080 + --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) --midi-in PORT Also connect from an existing MIDI input: index, or name or part of one --list-midi-in List MIDI input ports and exit @@ -539,6 +541,8 @@ Common options: ``` --device NAME Device to emulate (default: sc88); sc55, sc55mkii, sc88, jv880, jv1080 --midi-in PORT MIDI input device: index, or name or part of one (default: 0) + --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/CMakeLists.txt b/emuscd/CMakeLists.txt index 5c05a55..73ed37c 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/README.md b/emuscd/README.md index 0448756..98bfa83 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: 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) --midi-in PORT Also connect from an existing MIDI input: index, or name or part of one --list-midi-in List MIDI input ports and exit @@ -150,6 +152,8 @@ emusc-winmidi.exe --device sc88 --midi-in "loopMIDI Port" ``` --device NAME Device to emulate (default: sc88); sc55, sc55mkii, sc88, jv880, jv1080 --midi-in PORT MIDI input device: index, or name or part of one (default: 0) +--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 64712b4..d619ad5 100644 --- a/emuscd/common.h +++ b/emuscd/common.h @@ -21,13 +21,70 @@ 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 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; } + if (s == "gm") { out = ResetMode::GM; return true; } + return false; +} + +// 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_gs_mode()/has_gm_mode() nothing to ask. +// +// 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; + 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(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 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) { + 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) { for (const char *d : SUPPORTED_DEVICES) diff --git a/emuscd/main.cc b/emuscd/main.cc index c6f0e15..581fa51 100644 --- a/emuscd/main.cc +++ b/emuscd/main.cc @@ -166,6 +166,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}; @@ -179,10 +180,12 @@ 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, const std::string& midi_in) + double gain_db, const std::string& midi_in, + 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); if (!midi_in.empty()) connect_midi_in(midi_in); @@ -321,16 +324,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 @@ -342,7 +343,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. @@ -354,6 +355,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; } @@ -520,6 +524,7 @@ int main(int argc, char* argv[]) { bool stats = false; int rt_priority = 0; double gain_db = 0.0; + ResetMode reset_mode = ResetMode::None; for (int i = 1; i < argc; i++) { std::string arg = argv[i]; @@ -574,6 +579,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" @@ -581,6 +591,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: 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" << " --midi-in PORT Also connect from an existing MIDI input, such\n" << " as a USB keyboard: an index, or a name or any\n" @@ -664,7 +679,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, midi_in); + gain_db, midi_in, 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 3a505cd..d2d9e14 100644 --- a/emuscd/main_winmidi.cc +++ b/emuscd/main_winmidi.cc @@ -43,6 +43,11 @@ const char *USAGE = " Supported: sc55, sc55mkii, sc88, jv880, jv1080\n" " --midi-in PORT MIDI input device: an index, or a name or any part\n" " of one, any case (default: 0). See --list-midi-in\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" @@ -122,9 +127,9 @@ class WinMidiDaemon { WinMidiDaemon(const std::string &dev, const std::string &romDir, const std::string &midiIn, 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)) { @@ -178,9 +183,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 @@ -193,6 +198,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; } @@ -478,6 +486,7 @@ class WinMidiDaemon { unsigned _sampleRate, _blockFrames; std::string _romDir; + ResetMode _resetMode; float _gainLin = 1.0f; std::vector _rendered; std::atomic _running{false}; @@ -520,6 +529,7 @@ int main(int argc, char **argv) { unsigned block = 256; unsigned latency = 20; double gainDb = 0.0; + ResetMode resetMode = ResetMode::None; for (int i = 1; i < argc; i++) { std::string a = argv[i]; @@ -550,6 +560,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; @@ -573,7 +589,7 @@ int main(int argc, char **argv) { if (romDir.empty()) romDir = default_rom_dir(); WinMidiDaemon daemon(device, romDir, midiIn, waveOutId, rate, block, latency, gainDb, - audioApi, dsoundOutId); + audioApi, dsoundOutId, resetMode); daemon.run(); return 0; } diff --git a/emuscd/tests/power_on_test.cc b/emuscd/tests/power_on_test.cc new file mode 100644 index 0000000..3273baf --- /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 f8b08c7..2bb1fa6 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_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) + if(MSVC) + target_compile_options(synth_mode_lock_test PRIVATE /UNDEBUG) + else() + target_compile_options(synth_mode_lock_test PRIVATE -UNDEBUG) + endif() + 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 bdb3e60..96083f6 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,52 @@ 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; + /* @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); + 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; + } + return true; +} + +bool device_has_gm_mode(const Device *device) +{ + if (!device || !device->initialized) return false; - return device->voice_ops->gm_system_on(device->voice_state); + 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) +{ + return device && device->initialized && + xp_profile(&device->renderer.rom)->sysexModelId == 0x42; } bool device_host_reset_enters_gm(const Device *device) @@ -867,6 +952,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: @@ -967,11 +1057,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; @@ -1088,6 +1195,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: @@ -1245,6 +1355,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; @@ -1260,13 +1373,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 f6e79b5..4594f2e 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,10 +242,20 @@ 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); +/* 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. */ +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/engines/xp/devices/profile.h b/libemusc/src/engines/xp/devices/profile.h index 9e9c9ae..6ff00df 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 5fe78c0..aeee539 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/src/settings.cc b/libemusc/src/settings.cc index 625c7dc..ae8423e 100644 --- a/libemusc/src/settings.cc +++ b/libemusc/src/settings.cc @@ -668,6 +668,20 @@ 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; +} + + +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 @@ -683,12 +697,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 19acad2..5b1c97a 100644 --- a/libemusc/src/settings.h +++ b/libemusc/src/settings.h @@ -118,6 +118,10 @@ 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; + // 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 9016088..8c301c7 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; } @@ -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 (_modeLock == ModeLock::GM) + _settings->set_gm_mode(); + } + midiMutex.unlock(); } @@ -951,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(); @@ -1013,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(); @@ -1207,6 +1213,55 @@ 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()) + return Xp::device_has_gm_mode(_xpDevice); + return _settings->has_gm_mode(); +} + + +void Synth::set_mode_lock(ModeLock 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 640484d..2b48c05 100644 --- a/libemusc/src/synth.h +++ b/libemusc/src/synth.h @@ -139,6 +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. + // 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; + + // 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, 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; } + // 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 +232,8 @@ class Synth private: Settings *_settings = nullptr; + 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 // scheduler clock. When the loaded ROM is an SC-88 this holds that engine and @@ -325,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/device_test.cc b/libemusc/tests/device_test.cc index a0ee33e..18ec801 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); diff --git a/libemusc/tests/synth_mode_lock_test.cc b/libemusc/tests/synth_mode_lock_test.cc new file mode 100644 index 0000000..99d061a --- /dev/null +++ b/libemusc/tests/synth_mode_lock_test.cc @@ -0,0 +1,375 @@ +/* SPDX-License-Identifier: CC0-1.0 */ + +/* 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 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. + * + * 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, SC-88 and JV-1080. + * + * 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 + * and the SC-88's the test reports skipped. + */ + +#include "synth.h" +#include "control_rom.h" +#include "wave_rom.h" + +#include +#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; +using ModeLock = EmuSC::Synth::ModeLock; +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, 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_mode_lock(lock); + run(*s); + return s; +} + +void test_sc55mkii(Roms &r) +{ + { + 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 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); + + 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 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, ModeLock::GM); + assert(s->mode_lock() == ModeLock::GM); + 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)); + + s->set_mode_lock(ModeLock::None); + sysex(*s, kGsReset); + assert(in_gs(*s)); + assert(bank_after_select(*s) == 8); + } +} + +/* 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; + 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_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++; +} + +} // namespace + +int main(void) +{ + Roms mk2, sc88; + const bool haveMk2 = load("SC55MKII", mk2); + const bool haveSc88 = load("SC88", sc88); + if (!haveMk2 && !haveSc88) + return 77; + 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, true, 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; +}