Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions emuscd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
4 changes: 4 additions & 0 deletions emuscd/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
71 changes: 64 additions & 7 deletions emuscd/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
35 changes: 25 additions & 10 deletions emuscd/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ class EmuscdDaemon {
std::unique_ptr<EmuSC::Synth> synth;
std::string device_name;
std::string rom_dir;
ResetMode reset_mode;

std::atomic<bool> running{true};
std::atomic<bool> device_change_requested{false};
Expand All @@ -179,10 +180,12 @@ class EmuscdDaemon {
unsigned int requested_rate, unsigned int latency_ms,
unsigned int block, unsigned int threads,
const std::vector<unsigned>& 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);
Expand Down Expand Up @@ -321,16 +324,14 @@ class EmuscdDaemon {
}

std::unique_ptr<EmuSC::Synth> 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
Expand All @@ -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.
Expand All @@ -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;
}

Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -574,13 +579,23 @@ 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"
<< "Usage: emuscd [options]\n"
<< "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"
Expand Down Expand Up @@ -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;
Expand Down
26 changes: 21 additions & 5 deletions emuscd/main_winmidi.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -178,9 +183,9 @@ class WinMidiDaemon {
}

std::unique_ptr<EmuSC::Synth> 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
Expand All @@ -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;
}

Expand Down Expand Up @@ -478,6 +486,7 @@ class WinMidiDaemon {

unsigned _sampleRate, _blockFrames;
std::string _romDir;
ResetMode _resetMode;
float _gainLin = 1.0f;
std::vector<float> _rendered;
std::atomic<bool> _running{false};
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -550,6 +560,12 @@ int main(int argc, char **argv) {
else if (a == "--block") block = static_cast<unsigned>(std::stoul(need("--block")));
else if (a == "--latency") latency = static_cast<unsigned>(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;
Expand All @@ -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;
}
Loading
Loading