Adjusted Timidity++ to soundfint manager

This commit is contained in:
Christoph Oelckers 2018-02-23 08:54:01 +01:00
parent f9893a700a
commit 6618656e7c
8 changed files with 47 additions and 177 deletions

View File

@ -30,7 +30,7 @@ protected:
// This is only doable for loose config files that get set as sound fonts. All other cases read from a contained environment where this does not apply. // This is only doable for loose config files that get set as sound fonts. All other cases read from a contained environment where this does not apply.
bool mAllowAbsolutePaths = false; bool mAllowAbsolutePaths = false;
// This has only meaning if being run on a platform with a case sensitive file system and loose files. // This has only meaning if being run on a platform with a case sensitive file system and loose files.
// When reading from an archive it will always be case insensitive, just like the lump manager (since it repurposes the same implementation.) // When reading from an archive it will always be case insensitive, just like the lump manager.
bool mCaseSensitivePaths = false; bool mCaseSensitivePaths = false;
TArray<FString> mPaths; TArray<FString> mPaths;
@ -62,7 +62,7 @@ class FSF2Reader : public FSoundFontReader
FString mFilename; FString mFilename;
public: public:
FSF2Reader(const char *filename); FSF2Reader(const char *filename);
virtual FileReader *FSF2Reader::OpenMainConfigFile() override; virtual FileReader *OpenMainConfigFile() override;
virtual FileReader *OpenFile(const char *name) override; virtual FileReader *OpenFile(const char *name) override;
}; };
@ -108,7 +108,7 @@ public:
//========================================================================== //==========================================================================
// //
// This one gets ugly... //
// //
//========================================================================== //==========================================================================

View File

@ -132,134 +132,26 @@ double flt_rand()
return (int)pr_rnd.GenRand_Real1(); return (int)pr_rnd.GenRand_Real1();
} }
PathList::PathList() struct timidity_file *open_file(const char *name, FSoundFontReader *sfreader)
{ {
} FileReader *fr;
FString filename;
/* This adds a directory to the path list */ if (name == nullptr)
void PathList::addPath(const char *str) {
{ fr = sfreader->OpenMainConfigFile();
if (*str == 0) return; filename = sfreader->basePath() + "timidity.cfg";
for (size_t i = 0; i < paths.size(); i++) }
{ else
if (pathcmp(paths[i].c_str(), str, 0) == 0) {
{ auto res = sfreader->LookupFile(name);
// move string to the back. fr = res.first;
std::string ss = paths[i]; filename = res.second;
paths.erase(paths.begin() + i); }
paths.push_back(ss); if (fr == nullptr) return nullptr;
return;
}
}
paths.push_back(str);
}
int PathList::pathcmp(const char *p1, const char *p2, int ignore_case)
{
int c1, c2;
#ifdef _WIN32
ignore_case = 1; /* Always ignore the case */
#endif
do {
c1 = *p1++ & 0xff;
c2 = *p2++ & 0xff;
if (ignore_case)
{
c1 = tolower(c1);
c2 = tolower(c2);
}
if (c1 == '/') c1 = *p1 ? 0x100 : 0;
if (c1 == '/') c2 = *p2 ? 0x100 : 0;
} while (c1 == c2 && c1 /* && c2 */);
return c1 - c2;
}
FileReader *PathList::tryOpenPath(const char *name, bool ismain)
{
FileReader *fp;
int lumpnum;
if (ismain)
{
tppPathExpander.openmode = PathExpander::OM_FILEORLUMP;
tppPathExpander.clearPathlist();
#ifdef _WIN32
tppPathExpander.addToPathlist("C:\\TIMIDITY");
tppPathExpander.addToPathlist("\\TIMIDITY");
tppPathExpander.addToPathlist(progdir);
#else
tppPathExpander.addToPathlist("/usr/local/lib/timidity");
tppPathExpander.addToPathlist("/etc/timidity");
tppPathExpander.addToPathlist("/etc");
#endif
}
if (!(fp = tppPathExpander.openFileReader(name, &lumpnum)))
return NULL;
if (ismain)
{
if (lumpnum > 0)
{
tppPathExpander.openmode = PathExpander::OM_LUMP;
tppPathExpander.clearPathlist(); // when reading from a PK3 we don't want to use any external path
}
else
{
tppPathExpander.openmode = PathExpander::OM_FILE;
}
}
return fp;
}
std::pair<FileReader *, std::string> PathList::openFile(const char *name, bool ismainfile)
{
if (name && *name)
{
/* First try the given name */
if (!IsAbsPath(name))
{
for (int i = (int)paths.size() - 1; i >= 0; i--)
{
std::string s = paths[i];
auto c = s.at(s.length() - 1);
if (c != '/' && c != '#' && name[0] != '#')
{
s += '/';
}
s += name;
auto fr = tryOpenPath(s.c_str(), ismainfile);
if (fr!= nullptr) return std::make_pair(fr, s);
}
auto fr = tryOpenPath(name, ismainfile);
if (fr != nullptr) return std::make_pair(fr, name);
}
else
{
// an absolute path is never looked up.
FileReader *fr = new FileReader;
if (fr->Open(name))
{
return std::make_pair(fr, std::string(name));
}
delete fr;
}
}
return std::make_pair(nullptr, std::string());
}
struct timidity_file *open_file(const char *name, bool ismainfile, PathList &pathList)
{
auto file = pathList.openFile(name, ismainfile);
if (!file.first) return nullptr;
auto tf = new timidity_file; auto tf = new timidity_file;
tf->url = file.first; tf->url = fr;
tf->filename = file.second; tf->filename = filename.GetChars();
return tf; return tf;
} }

View File

@ -28,45 +28,18 @@
#include <vector> #include <vector>
#include <stdint.h> #include <stdint.h>
#include "files.h" #include "files.h"
#include "i_soundfont.h"
namespace TimidityPlus namespace TimidityPlus
{ {
class PathList
{
std::vector<std::string> paths;
FileReader *tryOpenPath(const char *name, bool ismain);
public:
PathList();
void addPath(const char *str);
void clear()
{
paths.resize(1);
}
static int pathcmp(const char *p1, const char *p2, int ignore_case);
std::pair<FileReader *, std::string> openFile(const char *name, bool ismainfile);
};
struct timidity_file struct timidity_file
{ {
FileReader *url; FileReader *url;
std::string filename; std::string filename;
}; };
/* Noise modes for open_file */ extern struct timidity_file *open_file(const char *name, FSoundFontReader *);
enum
{
OF_SILENT = 0,
OF_NORMAL = 1,
OF_VERBOSE = 2,
};
extern struct timidity_file *open_file(const char *name, bool, PathList &);
extern void close_file(struct timidity_file *tf); extern void close_file(struct timidity_file *tf);
extern void skip(struct timidity_file *tf, size_t len); extern void skip(struct timidity_file *tf, size_t len);
extern char *tf_gets(char *buff, int n, struct timidity_file *tf); extern char *tf_gets(char *buff, int n, struct timidity_file *tf);
@ -76,7 +49,6 @@ extern long tf_seek(struct timidity_file *tf, long offset, int whence);
extern long tf_tell(struct timidity_file *tf); extern long tf_tell(struct timidity_file *tf);
extern int int_rand(int n); /* random [0..n-1] */ extern int int_rand(int n); /* random [0..n-1] */
double flt_rand(); double flt_rand();
extern int check_file_extension(char *filename, char *ext, int decompress);
extern void *safe_malloc(size_t count); extern void *safe_malloc(size_t count);
extern void *safe_realloc(void *old_ptr, size_t new_size); extern void *safe_realloc(void *old_ptr, size_t new_size);

View File

@ -30,6 +30,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#include "instrum.h" #include "instrum.h"
#include "quantity.h" #include "quantity.h"
#include "cmdlib.h" #include "cmdlib.h"
#include "i_soundfont.h"
@ -618,7 +619,7 @@ char *Instruments::expand_variables(char *string, MBlockList *varbuf, const char
} }
int Instruments::read_config_file(const char *name, int self, int allow_missing_file, bool ismainfile) int Instruments::read_config_file(const char *name, int self, int allow_missing_file)
{ {
struct timidity_file *tf; struct timidity_file *tf;
char buf[1024], *tmp, *w[MAXWORDS + 1], *cp; char buf[1024], *tmp, *w[MAXWORDS + 1], *cp;
@ -638,7 +639,7 @@ int Instruments::read_config_file(const char *name, int self, int allow_missing_
return READ_CONFIG_RECURSION; return READ_CONFIG_RECURSION;
} }
tf = open_file(name, ismainfile, pathlist); tf = open_file(name, sfreader);
if (tf == NULL) if (tf == NULL)
return allow_missing_file ? READ_CONFIG_FILE_NOT_FOUND : return allow_missing_file ? READ_CONFIG_FILE_NOT_FOUND :
READ_CONFIG_ERROR; READ_CONFIG_ERROR;
@ -1401,7 +1402,7 @@ int Instruments::read_config_file(const char *name, int self, int allow_missing_
continue; continue;
} }
for (i = 1; i < words; i++) for (i = 1; i < words; i++)
pathlist.addPath(w[i]); sfreader->AddPath(w[i]);
} }
else if (!strcmp(w[0], "source") || !strcmp(w[0], "trysource")) else if (!strcmp(w[0], "source") || !strcmp(w[0], "trysource"))
{ {
@ -1613,4 +1614,4 @@ int Instruments::read_config_file(const char *name, int self, int allow_missing_
return (errcnt == 0) ? READ_CONFIG_SUCCESS : READ_CONFIG_ERROR; return (errcnt == 0) ? READ_CONFIG_SUCCESS : READ_CONFIG_ERROR;
} }
} }

View File

@ -36,6 +36,7 @@
#include "filter.h" #include "filter.h"
#include "quantity.h" #include "quantity.h"
#include "freq.h" #include "freq.h"
#include "i_soundfont.h"
namespace TimidityPlus namespace TimidityPlus
{ {
@ -54,7 +55,10 @@ Instruments::Instruments()
bool Instruments::load(const char *config) bool Instruments::load(const char *config)
{ {
if (read_config_file(config, 0, 0, true) == RC_OK) sfreader = sfmanager.OpenSoundFont(config, SF_SF2 | SF_GUS);
if (sfreader == nullptr) return false;
if (read_config_file(nullptr, 0, 0) == RC_OK)
{ {
init_load_soundfont(); init_load_soundfont();
set_default_instrument(); set_default_instrument();
@ -616,7 +620,7 @@ Instrument *Instruments::load_gus_instrument(char *name, ToneBank *bank, int dr,
return ip; return ip;
} }
/* Open patch file */ /* Open patch file */
if (!(tf = open_file(name, false, pathlist))) { if (!(tf = open_file(name, sfreader))) {
int name_len, ext_len; int name_len, ext_len;
static const char *patch_ext[] = { ".pat", 0 }; static const char *patch_ext[] = { ".pat", 0 };
@ -631,7 +635,7 @@ Instrument *Instruments::load_gus_instrument(char *name, ToneBank *bank, int dr,
continue; /* duplicated ext. */ continue; /* duplicated ext. */
strcpy((char *)tmp, name); strcpy((char *)tmp, name);
strcat((char *)tmp, patch_ext[i]); strcat((char *)tmp, patch_ext[i]);
if ((tf = open_file((char *)tmp, false, pathlist))) if ((tf = open_file((char *)tmp, sfreader)))
{ {
noluck = 0; noluck = 0;
break; break;
@ -2039,4 +2043,4 @@ void Instruments::recompute_userdrum_altassign(int bank, int group)
} }

View File

@ -31,6 +31,7 @@
#include "sflayer.h" #include "sflayer.h"
#include "sfitem.h" #include "sfitem.h"
class FSoundFontReader;
namespace TimidityPlus namespace TimidityPlus
{ {
@ -232,7 +233,7 @@ struct SampleImporter;
class Instruments class Instruments
{ {
std::string configFileName; std::string configFileName;
PathList pathlist; FSoundFontReader *sfreader;
ToneBank standard_tonebank, standard_drumset; ToneBank standard_tonebank, standard_drumset;
@ -549,7 +550,7 @@ public:
void PrecacheInstruments(const uint16_t *instruments, int count); void PrecacheInstruments(const uint16_t *instruments, int count);
int read_config_file(const char *name, int self, int allow_missing_file, bool ismainfile = false); int read_config_file(const char *name, int self, int allow_missing_file);
void set_default_instrument() void set_default_instrument()
{ {

View File

@ -278,7 +278,7 @@ int Instruments::import_wave_discriminant(char *sample_file)
struct timidity_file *tf; struct timidity_file *tf;
char buf[12]; char buf[12];
if ((tf = open_file(sample_file, false, pathlist)) == NULL) if ((tf = open_file(sample_file, sfreader)) == NULL)
return 1; return 1;
if (tf_read(buf, 12, 1, tf) != 1 if (tf_read(buf, 12, 1, tf) != 1
|| memcmp(&buf[0], "RIFF", 4) != 0 || memcmp(&buf[8], "WAVE", 4) != 0) || memcmp(&buf[0], "RIFF", 4) != 0 || memcmp(&buf[8], "WAVE", 4) != 0)
@ -309,7 +309,7 @@ int Instruments::import_wave_load(char *sample_file, Instrument *inst)
WAVSamplerChunk samplerc = {0,}; WAVSamplerChunk samplerc = {0,};
GeneralInstrumentInfo instc; GeneralInstrumentInfo instc;
if ((tf = open_file(sample_file, false, pathlist)) == NULL) if ((tf = open_file(sample_file, sfreader)) == NULL)
return 1; return 1;
if (tf_read(buf, 12, 1, tf) != 1 if (tf_read(buf, 12, 1, tf) != 1
|| memcmp(&buf[0], "RIFF", 4) != 0 || memcmp(&buf[8], "WAVE", 4) != 0) || memcmp(&buf[0], "RIFF", 4) != 0 || memcmp(&buf[8], "WAVE", 4) != 0)
@ -551,7 +551,7 @@ int Instruments::import_aiff_discriminant(char *sample_file)
struct timidity_file *tf; struct timidity_file *tf;
char buf[12]; char buf[12];
if ((tf = open_file(sample_file, false, pathlist)) == NULL) if ((tf = open_file(sample_file, sfreader)) == NULL)
return 1; return 1;
if (tf_read(buf, 12, 1, tf) != 1 if (tf_read(buf, 12, 1, tf) != 1
|| memcmp(&buf[0], "FORM", 4) != 0 || memcmp(&buf[8], "AIF", 3) != 0 || memcmp(&buf[0], "FORM", 4) != 0 || memcmp(&buf[8], "AIF", 3) != 0
@ -591,7 +591,7 @@ int Instruments::import_aiff_load(char *sample_file, Instrument *inst)
AIFFLoopInfo loop_info = {0,0,0}; AIFFLoopInfo loop_info = {0,0,0};
AIFFMarkerData *marker_data; AIFFMarkerData *marker_data;
if ((tf = open_file(sample_file, false, pathlist)) == NULL) if ((tf = open_file(sample_file, sfreader)) == NULL)
return 1; return 1;
if (tf_read(buf, 12, 1, tf) != 1 if (tf_read(buf, 12, 1, tf) != 1
|| memcmp(&buf[0], "FORM", 4) != 0 || memcmp(&buf[8], "AIF", 3) != 0 || memcmp(&buf[0], "FORM", 4) != 0 || memcmp(&buf[8], "AIF", 3) != 0
@ -1222,4 +1222,4 @@ static double ConvertFromIeeeExtended(const char *bytes)
else else
return f; return f;
} }
} }

View File

@ -265,7 +265,7 @@ void Instruments::init_sf(SFInsts *rec)
SFInfo sfinfo; SFInfo sfinfo;
int i; int i;
if ((rec->tf = open_file(rec->fname, false, pathlist)) == NULL) { if ((rec->tf = open_file(rec->fname, sfreader)) == NULL) {
ctl_cmsg(CMSG_ERROR, VERB_NORMAL, ctl_cmsg(CMSG_ERROR, VERB_NORMAL,
"Can't open soundfont file %s", rec->fname); "Can't open soundfont file %s", rec->fname);
end_soundfont(rec); end_soundfont(rec);
@ -362,7 +362,7 @@ Instrument *Instruments::try_load_soundfont(SFInsts *rec, int order, int bank,in
if (rec->tf == NULL) { if (rec->tf == NULL) {
if (rec->fname == NULL) if (rec->fname == NULL)
return NULL; return NULL;
if ((rec->tf = open_file(rec->fname, false, pathlist)) == NULL) if ((rec->tf = open_file(rec->fname, sfreader)) == NULL)
{ {
ctl_cmsg(CMSG_ERROR, VERB_NORMAL, ctl_cmsg(CMSG_ERROR, VERB_NORMAL,
"Can't open soundfont file %s", rec->fname); "Can't open soundfont file %s", rec->fname);
@ -1512,4 +1512,4 @@ void Instruments::convert_vibrato(SampleList *vp, LayerTable *tbl)
to_msec(tbl->val[SF_delayLfo2]) * 0.001; to_msec(tbl->val[SF_delayLfo2]) * 0.001;
} }
} }