From a5740253732e2c6b8aea0781a69b7826c2779f03 Mon Sep 17 00:00:00 2001 From: Wohlstand Date: Tue, 29 Sep 2020 01:57:08 +0300 Subject: [PATCH] OPNMIDI: Fixed an inability to load a custom bank --- .../mididevices/music_opnmidi_mididevice.cpp | 50 +++++++++++++------ 1 file changed, 35 insertions(+), 15 deletions(-) diff --git a/libraries/zmusic/mididevices/music_opnmidi_mididevice.cpp b/libraries/zmusic/mididevices/music_opnmidi_mididevice.cpp index 967b16976..3d539e4b8 100644 --- a/libraries/zmusic/mididevices/music_opnmidi_mididevice.cpp +++ b/libraries/zmusic/mididevices/music_opnmidi_mididevice.cpp @@ -46,7 +46,7 @@ class OPNMIDIDevice : public SoftSynthMIDIDevice { struct OPN2_MIDIPlayer *Renderer; public: - OPNMIDIDevice(const char *bank); + OPNMIDIDevice(const OpnConfig *config); ~OPNMIDIDevice(); @@ -59,7 +59,7 @@ protected: void ComputeOutput(float *buffer, int len) override; private: - int LoadCustomBank(const char *bankfile); + int LoadCustomBank(const OpnConfig *config); }; @@ -82,25 +82,25 @@ enum //========================================================================== #include "data/xg.h" -OPNMIDIDevice::OPNMIDIDevice(const char *bank) +OPNMIDIDevice::OPNMIDIDevice(const OpnConfig *config) :SoftSynthMIDIDevice(44100) { Renderer = opn2_init(44100); // todo: make it configurable if (Renderer != nullptr) { - if (!opnConfig.opn_use_custom_bank || !LoadCustomBank(opnConfig.opn_custom_bank.c_str())) + if (!LoadCustomBank(config)) { - if(opnConfig.default_bank.size() == 0) + if(config->default_bank.size() == 0) { opn2_openBankData(Renderer, xg_default, sizeof(xg_default)); } - else opn2_openBankData(Renderer, opnConfig.default_bank.data(), (long)opnConfig.default_bank.size()); + else opn2_openBankData(Renderer, config->default_bank.data(), (long)config->default_bank.size()); } - opn2_switchEmulator(Renderer, (int)opnConfig.opn_emulator_id); - opn2_setRunAtPcmRate(Renderer, (int)opnConfig.opn_run_at_pcm_rate); - opn2_setNumChips(Renderer, opnConfig.opn_chips_count); - opn2_setSoftPanEnabled(Renderer, (int)opnConfig.opn_fullpan); + opn2_switchEmulator(Renderer, (int)config->opn_emulator_id); + opn2_setRunAtPcmRate(Renderer, (int)config->opn_run_at_pcm_rate); + opn2_setNumChips(Renderer, config->opn_chips_count); + opn2_setSoftPanEnabled(Renderer, (int)config->opn_fullpan); } else { @@ -133,9 +133,12 @@ OPNMIDIDevice::~OPNMIDIDevice() //========================================================================== -int OPNMIDIDevice::LoadCustomBank(const char *bankfile) +int OPNMIDIDevice::LoadCustomBank(const OpnConfig *config) { - if(!bankfile || !*bankfile) + const char *bankfile = config->opn_custom_bank.c_str(); + if(!config->opn_use_custom_bank) + return 0; + if(!*bankfile) return 0; return (opn2_openBankFile(Renderer, bankfile) == 0); } @@ -235,17 +238,34 @@ void OPNMIDIDevice::ComputeOutput(float *buffer, int len) MIDIDevice *CreateOPNMIDIDevice(const char *Args) { + OpnConfig config = opnConfig; + const char* bank = Args && *Args ? Args : opnConfig.opn_use_custom_bank ? opnConfig.opn_custom_bank.c_str() : nullptr; if (bank && *bank) { + const char* info; if (musicCallbacks.PathForSoundfont) { - auto info = musicCallbacks.PathForSoundfont(bank, SF_WOPN); - if (info != nullptr) bank = info; + info = musicCallbacks.PathForSoundfont(bank, SF_WOPN); + } + else + { + info = bank; + } + + if(info == nullptr) + { + config.opn_custom_bank = ""; + config.opn_use_custom_bank = false; + } + else + { + config.opn_custom_bank = info; + config.opn_use_custom_bank = true; } } - return new OPNMIDIDevice(bank); + return new OPNMIDIDevice(&config); } #else