From 2dd6c1486594fa9ab843c99532fdbc7f78c42bf0 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Mon, 23 Sep 2019 13:53:28 +0200 Subject: [PATCH] - implemented an abstract sound font reader interface for Timidity++. The only dependency left on the main GZDoom code are the CVars, which will be dealt with next. # Conflicts: # src/sound/mididevices/music_timiditypp_mididevice.cpp --- .../music_timiditypp_mididevice.cpp | 3 +- src/sound/music/i_soundfont.cpp | 34 +++++++++++++++++-- src/sound/music/i_soundfont.h | 13 +++---- src/sound/timidity/timidity.cpp | 1 + src/sound/timiditypp/common.cpp | 27 ++------------- src/sound/timiditypp/common.h | 32 ++++++++++------- src/sound/timiditypp/configfile.cpp | 2 +- src/sound/timiditypp/effect.h | 1 - src/sound/timiditypp/filter.cpp | 1 + src/sound/timiditypp/mix.cpp | 7 +--- src/sound/timiditypp/playmidi.cpp | 7 ++++ 11 files changed, 73 insertions(+), 55 deletions(-) diff --git a/src/sound/mididevices/music_timiditypp_mididevice.cpp b/src/sound/mididevices/music_timiditypp_mididevice.cpp index 16b25d21f..f7c8ae47b 100644 --- a/src/sound/mididevices/music_timiditypp_mididevice.cpp +++ b/src/sound/mididevices/music_timiditypp_mididevice.cpp @@ -42,7 +42,8 @@ #include "version.h" #include "m_misc.h" #include "v_text.h" -#include "i_system.h" +#include "doomerrors.h" +#include "i_soundfont.h" #include "timiditypp/timidity.h" #include "timiditypp/instrum.h" diff --git a/src/sound/music/i_soundfont.cpp b/src/sound/music/i_soundfont.cpp index abf6c4ed3..0cb48e132 100644 --- a/src/sound/music/i_soundfont.cpp +++ b/src/sound/music/i_soundfont.cpp @@ -36,6 +36,7 @@ #include #include #include "i_soundfont.h" +#include "i_soundinternal.h" #include "cmdlib.h" #include "w_wad.h" #include "i_system.h" @@ -45,6 +46,33 @@ FSoundFontManager sfmanager; +struct timidity_file_FileReader : public TimidityPlus::timidity_file +{ + FileReader fr; + + char* gets(char* buff, int n) override + { + if (!fr.isOpen()) return nullptr; + return fr.Gets(buff, n); + } + long read(void* buff, int32_t size, int32_t nitems) override + { + if (!fr.isOpen()) return 0; + return (long)fr.Read(buff, size * nitems) / size; + } + long seek(long offset, int whence) override + { + if (!fr.isOpen()) return 0; + return (long)fr.Seek(offset, (FileReader::ESeek)whence); + } + long tell() override + { + if (!fr.isOpen()) return 0; + return (long)fr.Tell(); + } + +}; + //========================================================================== // // returns both a file reader and the full name of the looked up file @@ -124,15 +152,15 @@ FileReader FSoundFontReader::Open(const char *name, std::string& filename) // //========================================================================== -struct TimidityPlus::timidity_file* FSoundFontReader::open_timidity_file(const char* name) +struct TimidityPlus::timidity_file* FSoundFontReader::open_timidityplus_file(const char* name) { std::string filename; FileReader fr = Open(name, filename); if (!fr.isOpen()) return nullptr; - auto tf = new TimidityPlus::timidity_file; - tf->url = std::move(fr); + auto tf = new timidity_file_FileReader; + tf->fr = std::move(fr); tf->filename = std::move(filename); return tf; } diff --git a/src/sound/music/i_soundfont.h b/src/sound/music/i_soundfont.h index 80a20486e..f100ff94f 100644 --- a/src/sound/music/i_soundfont.h +++ b/src/sound/music/i_soundfont.h @@ -3,6 +3,7 @@ #include "doomtype.h" #include "w_wad.h" #include "files.h" +#include "timiditypp/timidity_file.h" enum { @@ -20,17 +21,13 @@ struct FSoundFontInfo int type; }; -namespace TimidityPlus -{ - struct timidity_file; -} //========================================================================== // // // //========================================================================== -class FSoundFontReader +class FSoundFontReader : public TimidityPlus::SoundFontReaderInterface { 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. @@ -62,7 +59,11 @@ public: } virtual FileReader Open(const char* name, std::string &filename); - virtual struct TimidityPlus::timidity_file* open_timidity_file(const char* name); + virtual struct TimidityPlus::timidity_file* open_timidityplus_file(const char* name); + virtual void timidityplus_add_path(const char* name) + { + return AddPath(name); + } }; //========================================================================== diff --git a/src/sound/timidity/timidity.cpp b/src/sound/timidity/timidity.cpp index a21596ade..d1064db44 100644 --- a/src/sound/timidity/timidity.cpp +++ b/src/sound/timidity/timidity.cpp @@ -35,6 +35,7 @@ #include "i_musicinterns.h" #include "v_text.h" #include "timidity.h" +#include "i_soundfont.h" CUSTOM_CVAR(String, midi_config, "gzdoom", CVAR_ARCHIVE | CVAR_GLOBALCONFIG) { diff --git a/src/sound/timiditypp/common.cpp b/src/sound/timiditypp/common.cpp index b73894e7f..1fc78045b 100644 --- a/src/sound/timiditypp/common.cpp +++ b/src/sound/timiditypp/common.cpp @@ -132,48 +132,27 @@ double flt_rand() struct timidity_file *open_file(const char *name, SoundFontReaderInterface *sfreader) { - return sfreader->open_timidity_file(name); + return sfreader->open_timidityplus_file(name); } /* This closes files opened with open_file */ void tf_close(struct timidity_file *tf) { - tf->url.Close(); delete tf; } /* This is meant for skipping a few bytes. */ void skip(struct timidity_file *tf, size_t len) { - tf->url.Seek((long)len, FileReader::SeekCur); -} - -char *tf_gets(char *buff, int n, struct timidity_file *tf) -{ - return tf->url.Gets(buff, n); + tf_seek(tf, (long)len, SEEK_CUR); } int tf_getc(struct timidity_file *tf) { unsigned char c; - auto read = tf->url.Read(&c, 1); + auto read = tf_read(&c, 1, 1, tf); return read == 0 ? EOF : c; } -long tf_read(void *buff, int32_t size, int32_t nitems, struct timidity_file *tf) -{ - return (long)tf->url.Read(buff, size * nitems) / size; -} - -long tf_seek(struct timidity_file *tf, long offset, int whence) -{ - - return (long)tf->url.Seek(offset, (FileReader::ESeek)whence); -} - -long tf_tell(struct timidity_file *tf) -{ - return (long)tf->url.Tell(); -} } diff --git a/src/sound/timiditypp/common.h b/src/sound/timiditypp/common.h index ea3867429..a999f5041 100644 --- a/src/sound/timiditypp/common.h +++ b/src/sound/timiditypp/common.h @@ -27,28 +27,34 @@ #include #include #include -#include "files.h" -#include "i_soundfont.h" +#include "timidity_file.h" namespace TimidityPlus { - //class SoundFontReaderInterface; - using SoundFontReaderInterface = FSoundFontReader; - -struct timidity_file +inline char* tf_gets(char* buff, int n, struct timidity_file* tf) { - FileReader url; - std::string filename; -}; + return tf->gets(buff, n); +} + +inline long tf_read(void* buff, int32_t size, int32_t nitems, struct timidity_file* tf) +{ + return (long)tf->read(buff, size, nitems); +} + +inline long tf_seek(struct timidity_file* tf, long offset, int whence) +{ + return (long)tf->seek(offset, whence); +} + +inline long tf_tell(struct timidity_file* tf) +{ + return (long)tf->tell(); +} 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); int tf_getc(struct timidity_file *tf); -extern long tf_read(void *buff, int32_t size, int32_t nitems, struct timidity_file *tf); -extern long tf_seek(struct timidity_file *tf, long offset, int whence); -extern long tf_tell(struct timidity_file *tf); extern int int_rand(int n); /* random [0..n-1] */ double flt_rand(); diff --git a/src/sound/timiditypp/configfile.cpp b/src/sound/timiditypp/configfile.cpp index 92ccfc6ed..a3874e4b2 100644 --- a/src/sound/timiditypp/configfile.cpp +++ b/src/sound/timiditypp/configfile.cpp @@ -1397,7 +1397,7 @@ int Instruments::read_config_file(const char *name, int self, int allow_missing_ continue; } for (i = 1; i < words; i++) - sfreader->AddPath(w[i]); + sfreader->timidityplus_add_path(w[i]); } else if (!strcmp(w[0], "source") || !strcmp(w[0], "trysource")) { diff --git a/src/sound/timiditypp/effect.h b/src/sound/timiditypp/effect.h index 5cace9dd5..6d65d8e20 100644 --- a/src/sound/timiditypp/effect.h +++ b/src/sound/timiditypp/effect.h @@ -1,7 +1,6 @@ #pragma once #include -#include "m_random.h" #include "timidity.h" diff --git a/src/sound/timiditypp/filter.cpp b/src/sound/timiditypp/filter.cpp index 7e3cb37ea..9afbf26e7 100644 --- a/src/sound/timiditypp/filter.cpp +++ b/src/sound/timiditypp/filter.cpp @@ -38,6 +38,7 @@ #include "common.h" #include "instrum.h" #include "filter.h" +#include "sysdep.h" namespace TimidityPlus { diff --git a/src/sound/timiditypp/mix.cpp b/src/sound/timiditypp/mix.cpp index b4b19e2ea..204dc46ed 100644 --- a/src/sound/timiditypp/mix.cpp +++ b/src/sound/timiditypp/mix.cpp @@ -32,15 +32,10 @@ #include "resample.h" #include "mix.h" #include "optcode.h" -#include "c_cvars.h" - -CUSTOM_CVAR(Float, min_sustain_time, 5000, CVAR_ARCHIVE | CVAR_GLOBALCONFIG) -{ - if (self < 0) self = 0; -} namespace TimidityPlus { +extern float min_sustain_time; #define FROM_FINAL_VOLUME(a) (a) diff --git a/src/sound/timiditypp/playmidi.cpp b/src/sound/timiditypp/playmidi.cpp index fa4bd43c1..2f134c13b 100644 --- a/src/sound/timiditypp/playmidi.cpp +++ b/src/sound/timiditypp/playmidi.cpp @@ -59,6 +59,7 @@ namespace TimidityPlus float timidity_drum_power = 1.f; int timidity_key_adjust = 0; float timidity_tempo_adjust = 1.f; + float min_sustain_time = 5000; // The following options have no generic use and are only meaningful for some SYSEX events not normally found in common MIDIs. // For now they are kept as unchanging global variables @@ -196,6 +197,12 @@ CUSTOM_CVAR(Float, timidity_tempo_adjust, 1, CVAR_ARCHIVE | CVAR_GLOBALCONFIG) ChangeVarSync(TimidityPlus::timidity_tempo_adjust, *self); } +CUSTOM_CVAR(Float, min_sustain_time, 5000, CVAR_ARCHIVE | CVAR_GLOBALCONFIG) +{ + if (self < 0) self = 0; + ChangeVarSync(TimidityPlus::min_sustain_time, *self); +} + namespace TimidityPlus {