- moved the instrument set maintenance out of the Timidity++ library into the player class.

This removes the dependency on the sound font manager from the low level library, reducing the direct dependencies to FileReader and SoundFontReader.
This commit is contained in:
Christoph Oelckers 2019-09-23 11:27:57 +02:00
parent df7a4bb0d9
commit 2cf8cc47df
7 changed files with 27 additions and 23 deletions

View file

@ -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)

View file

@ -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<FileReader , FString> LookupFile(const char *name);
void AddPath(const char *str);

View file

@ -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
{

View file

@ -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);

View file

@ -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

View file

@ -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;

View file

@ -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;
}
};