diff --git a/src/sound/mididevices/music_timiditypp_mididevice.cpp b/src/sound/mididevices/music_timiditypp_mididevice.cpp index 381551d3a..e7a0c56cc 100644 --- a/src/sound/mididevices/music_timiditypp_mididevice.cpp +++ b/src/sound/mididevices/music_timiditypp_mididevice.cpp @@ -45,6 +45,7 @@ class TimidityPPMIDIDevice : public SoftSynthMIDIDevice { static TimidityPlus::Instruments *instruments; + static FString configName; int sampletime; public: TimidityPPMIDIDevice(const char *args, int samplerate); @@ -70,6 +71,7 @@ protected: void ComputeOutput(float *buffer, int len); }; TimidityPlus::Instruments *TimidityPPMIDIDevice::instruments; +FString TimidityPPMIDIDevice::configName; // Config file to use CUSTOM_CVAR(String, timidity_config, "gzdoom", CVAR_ARCHIVE | CVAR_GLOBALCONFIG) @@ -95,7 +97,7 @@ TimidityPPMIDIDevice::TimidityPPMIDIDevice(const char *args, int samplerate) if (args == NULL || *args == 0) args = timidity_config; Renderer = nullptr; - if (instruments != nullptr && !instruments->checkConfig(args)) + if (instruments != nullptr && configName.CompareNoCase(args)) // Only load instruments if they have changed from the last played song. { delete instruments; instruments = nullptr; @@ -104,11 +106,16 @@ TimidityPPMIDIDevice::TimidityPPMIDIDevice(const char *args, int samplerate) if (instruments == nullptr) { - instruments = new TimidityPlus::Instruments; - if (!instruments->load(args)) + + auto sfreader = sfmanager.OpenSoundFont(args, SF_SF2 | SF_GUS); + if (sfreader != nullptr) { - delete instruments; - instruments = nullptr; + instruments = new TimidityPlus::Instruments; + if (!instruments->load(sfreader)) + { + delete instruments; + instruments = nullptr; + } } } if (instruments != nullptr) diff --git a/src/sound/music/i_soundfont.h b/src/sound/music/i_soundfont.h index 681558a86..9206e11ea 100644 --- a/src/sound/music/i_soundfont.h +++ b/src/sound/music/i_soundfont.h @@ -44,6 +44,11 @@ public: virtual ~FSoundFontReader() {} virtual FileReader OpenMainConfigFile() = 0; // this is special because it needs to be synthesized for .sf files and set some restrictions for patch sets + virtual FString MainConfigFileName() + { + return basePath() + "timidity.cfg"; + } + virtual FileReader OpenFile(const char *name) = 0; std::pair LookupFile(const char *name); void AddPath(const char *str); diff --git a/src/sound/timiditypp/common.cpp b/src/sound/timiditypp/common.cpp index e9b912f79..8e642d9c9 100644 --- a/src/sound/timiditypp/common.cpp +++ b/src/sound/timiditypp/common.cpp @@ -127,14 +127,14 @@ double flt_rand() return (int)GenRand_Real1(); } -struct timidity_file *open_file(const char *name, FSoundFontReader *sfreader) +struct timidity_file *open_file(const char *name, SoundFontReaderInterface *sfreader) { FileReader fr; FString filename; if (name == nullptr) { fr = sfreader->OpenMainConfigFile(); - filename = sfreader->basePath() + "timidity.cfg"; + filename = sfreader->MainConfigFileName(); } else { diff --git a/src/sound/timiditypp/common.h b/src/sound/timiditypp/common.h index 74c7070df..ea3867429 100644 --- a/src/sound/timiditypp/common.h +++ b/src/sound/timiditypp/common.h @@ -32,6 +32,8 @@ namespace TimidityPlus { + //class SoundFontReaderInterface; + using SoundFontReaderInterface = FSoundFontReader; struct timidity_file { @@ -39,7 +41,7 @@ struct timidity_file std::string filename; }; -extern struct timidity_file *open_file(const char *name, FSoundFontReader *); +extern struct timidity_file *open_file(const char *name, SoundFontReaderInterface *); extern void tf_close(struct timidity_file *tf); extern void skip(struct timidity_file *tf, size_t len); extern char *tf_gets(char *buff, int n, struct timidity_file *tf); diff --git a/src/sound/timiditypp/configfile.cpp b/src/sound/timiditypp/configfile.cpp index 445a0985b..92ccfc6ed 100644 --- a/src/sound/timiditypp/configfile.cpp +++ b/src/sound/timiditypp/configfile.cpp @@ -643,7 +643,7 @@ int Instruments::read_config_file(const char *name, int self, int allow_missing_ { char *c = strdup_mblock(&varbuf, tf->filename.c_str()); basedir = c; - for (; c; c++) if (*c == '\\') *c = '/'; + for (; *c; c++) if (*c == '\\') *c = '/'; sep = (char*)strrchr(basedir, '/'); } else diff --git a/src/sound/timiditypp/instrum.cpp b/src/sound/timiditypp/instrum.cpp index 554f01317..ba9f5878e 100644 --- a/src/sound/timiditypp/instrum.cpp +++ b/src/sound/timiditypp/instrum.cpp @@ -52,16 +52,13 @@ Instruments::Instruments() memcpy(layer_items, static_layer_items, sizeof(layer_items)); } -bool Instruments::load(const char *config) +bool Instruments::load(SoundFontReaderInterface *sf) { - sfreader = sfmanager.OpenSoundFont(config, SF_SF2 | SF_GUS); - if (sfreader == nullptr) return false; - + sfreader = sf; if (read_config_file(nullptr, 0, 0) == RC_OK) { init_load_soundfont(); set_default_instrument(); - configFileName = config; return true; } return false; diff --git a/src/sound/timiditypp/instrum.h b/src/sound/timiditypp/instrum.h index 0119159b3..6b96a0a3a 100644 --- a/src/sound/timiditypp/instrum.h +++ b/src/sound/timiditypp/instrum.h @@ -31,7 +31,6 @@ #include "sflayer.h" #include "sfitem.h" -class FSoundFontReader; namespace TimidityPlus { @@ -233,7 +232,7 @@ struct SampleImporter; class Instruments { std::string configFileName; - FSoundFontReader *sfreader; + SoundFontReaderInterface *sfreader; ToneBank standard_tonebank, standard_drumset; @@ -462,7 +461,7 @@ class Instruments public: Instruments(); - bool load(const char *config); + bool load(SoundFontReaderInterface *); ~Instruments(); const ToneBank *toneBank(int i) const @@ -556,12 +555,6 @@ public: { set_default_instrument(def_instr_name); } - - bool checkConfig(const char *filename) - { - return configFileName.compare(filename) == 0; - } - };