- removed some ZDoomd dependencies from Timidity(GUS) backend

* use std::string instead of FString
* replaced the single use of clamp with std::min/std::max.
* copied MAKE_ID macro into the source.
* use snprintf instead of mysnprintf
* use std::runtime_error instead of I_Error to abort on failed memory allocations.
This commit is contained in:
Christoph Oelckers 2019-09-24 11:07:32 +02:00
parent 7ba485f632
commit fea0f77905
9 changed files with 44 additions and 27 deletions

View file

@ -372,10 +372,12 @@ enum
#define BLINKTHRESHOLD (4*32) #define BLINKTHRESHOLD (4*32)
#ifndef MAKE_ID
#ifndef __BIG_ENDIAN__ #ifndef __BIG_ENDIAN__
#define MAKE_ID(a,b,c,d) ((uint32_t)((a)|((b)<<8)|((c)<<16)|((d)<<24))) #define MAKE_ID(a,b,c,d) ((uint32_t)((a)|((b)<<8)|((c)<<16)|((d)<<24)))
#else #else
#define MAKE_ID(a,b,c,d) ((uint32_t)((d)|((c)<<8)|((b)<<16)|((a)<<24))) #define MAKE_ID(a,b,c,d) ((uint32_t)((d)|((c)<<8)|((b)<<16)|((a)<<24)))
#endif #endif
#endif
#endif // __DOOMDEF_H__ #endif // __DOOMDEF_H__

View file

@ -23,8 +23,9 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <exception>
#include "timidity.h" #include "timidity.h"
#include "doomerrors.h"
namespace Timidity namespace Timidity
{ {
@ -34,10 +35,12 @@ namespace Timidity
/* This'll allocate memory or die. */ /* This'll allocate memory or die. */
void *safe_malloc(size_t count) void *safe_malloc(size_t count)
{ {
char buffer[80];
void *p; void *p;
if (count > (1 << 21)) if (count > (1 << 21))
{ {
I_Error("Timidity: Tried allocating %zu bytes. This must be a bug.", count); snprintf(buffer, 80, "Timidity: Tried allocating %zu bytes. This must be a bug.", count);
throw std::runtime_error(buffer);
} }
else if ((p = malloc(count))) else if ((p = malloc(count)))
{ {
@ -45,7 +48,8 @@ void *safe_malloc(size_t count)
} }
else else
{ {
I_Error("Timidity: Couldn't malloc %zu bytes.", count); snprintf(buffer, 80, "Timidity: Couldn't malloc %zu bytes.", count);
throw std::runtime_error(buffer);
} }
return 0; // Unreachable. return 0; // Unreachable.
} }

View file

@ -30,7 +30,7 @@
#include <memory> #include <memory>
#include "timidity.h" #include "timidity.h"
#include "templates.h" //#include "templates.h"
#include "gf1patch.h" #include "gf1patch.h"
#include "i_soundfont.h" #include "i_soundfont.h"
@ -165,14 +165,14 @@ static Instrument *load_instrument(Renderer *song, const char *name, int percuss
if (!fp.isOpen()) if (!fp.isOpen())
{ {
/* Try with various extensions */ /* Try with various extensions */
FString tmp = name; std::string tmp = name;
tmp += ".pat"; tmp += ".pat";
fp = gus_sfreader->LookupFile(tmp).first; fp = gus_sfreader->LookupFile(tmp.c_str()).first;
if (!fp.isOpen()) if (!fp.isOpen())
{ {
#ifdef __unix__ // Windows isn't case-sensitive. #ifndef _WIN32 // Windows isn't case-sensitive.
tmp.ToUpper(); std::transform(tmp.begin(), tmp.end(), tmp.begin(), [](unsigned char c){ return toupper(c); } );
fp = gus_sfreader->LookupFile(tmp).first; fp = gus_sfreader->LookupFile(tmp.c_str()).first;
if (!fp.isOpen()) if (!fp.isOpen())
#endif #endif
{ {
@ -401,7 +401,7 @@ fail:
{ {
sp->envelope.gf1.rate[j] = patch_data.EnvelopeRate[j]; sp->envelope.gf1.rate[j] = patch_data.EnvelopeRate[j];
/* [RH] GF1NEW clamps the offsets to the range [5,251], so we do too. */ /* [RH] GF1NEW clamps the offsets to the range [5,251], so we do too. */
sp->envelope.gf1.offset[j] = clamp<uint8_t>(patch_data.EnvelopeOffset[j], 5, 251); sp->envelope.gf1.offset[j] = std::max<uint8_t>(5, std::min<uint8_t>(251, patch_data.EnvelopeOffset[j]));
} }
/* Then read the sample data */ /* Then read the sample data */

View file

@ -25,8 +25,17 @@
#include <string.h> #include <string.h>
#include <math.h> #include <math.h>
#include "m_swap.h"
#include "timidity.h" #include "timidity.h"
#include "doomdef.h"
#ifndef MAKE_ID
#ifndef __BIG_ENDIAN__
#define MAKE_ID(a,b,c,d) ((uint32_t)((a)|((b)<<8)|((c)<<16)|((d)<<24)))
#else
#define MAKE_ID(a,b,c,d) ((uint32_t)((d)|((c)<<8)|((b)<<16)|((a)<<24)))
#endif
#endif
#define __Sound_SetError(x) #define __Sound_SetError(x)
@ -785,7 +794,7 @@ static const char *SourceToString(USHORT usSource)
case CONN_SRC_CC93: case CONN_SRC_CC93:
return "CC93"; return "CC93";
default: default:
mysnprintf(unknown, countof(unknown), "UNKNOWN (0x%04x)", usSource); snprintf(unknown, sizeof(unknown), "UNKNOWN (0x%04x)", usSource);
return unknown; return unknown;
} }
} }
@ -803,7 +812,7 @@ static const char *TransformToString(USHORT usTransform)
case CONN_TRN_SWITCH: case CONN_TRN_SWITCH:
return "SWITCH"; return "SWITCH";
default: default:
mysnprintf(unknown, countof(unknown), "UNKNOWN (0x%04x)", usTransform); snprintf(unknown, sizeof(unknown), "UNKNOWN (0x%04x)", usTransform);
return unknown; return unknown;
} }
} }
@ -877,7 +886,7 @@ static const char *DestinationToString(USHORT usDestination)
case CONN_DST_FILTER_Q: case CONN_DST_FILTER_Q:
return "FILTER_Q"; return "FILTER_Q";
default: default:
mysnprintf(unknown, countof(unknown), "UNKNOWN (0x%04x)", usDestination); snprintf(unknown, sizeof(unknown), "UNKNOWN (0x%04x)", usDestination);
return unknown; return unknown;
} }
} }

View file

@ -33,7 +33,7 @@ FontFile *font_find(const char *filename)
{ {
for (FontFile *font = Fonts; font != NULL; font = font->Next) for (FontFile *font = Fonts; font != NULL; font = font->Next)
{ {
if (stricmp(filename, font->Filename) == 0) if (stricmp(filename, font->Filename.c_str()) == 0)
{ {
return font; return font;
} }
@ -108,7 +108,7 @@ Instrument *load_instrument_font_order(struct Renderer *song, int order, int dru
return NULL; return NULL;
} }
FontFile::FontFile(FString filename) FontFile::FontFile(const char *filename)
: Filename(filename) : Filename(filename)
{ {
Next = Fonts; Next = Fonts;

View file

@ -763,7 +763,7 @@ SFFile *ReadSF2(const char *filename, FileReader &f)
return NULL; return NULL;
} }
SFFile::SFFile(FString filename) SFFile::SFFile(const char *filename)
: FontFile(filename) : FontFile(filename)
{ {
Presets = NULL; Presets = NULL;
@ -1507,7 +1507,7 @@ void SFFile::ApplyGeneratorsToRegion(SFGenComposite *gen, SFSample *sfsamp, Rend
void SFFile::LoadSample(SFSample *sample) void SFFile::LoadSample(SFSample *sample)
{ {
FileReader fp = gus_sfreader->LookupFile(Filename).first; FileReader fp = gus_sfreader->LookupFile(Filename.c_str()).first;
uint32_t i; uint32_t i;
if (!fp.isOpen()) if (!fp.isOpen())

View file

@ -270,7 +270,7 @@ struct SFPerc
struct SFFile : public Timidity::FontFile struct SFFile : public Timidity::FontFile
{ {
SFFile(FString filename); SFFile(const char * filename);
~SFFile(); ~SFFile();
Timidity::Instrument *LoadInstrument(struct Timidity::Renderer *song, int drum, int bank, int program); Timidity::Instrument *LoadInstrument(struct Timidity::Renderer *song, int drum, int bank, int program);
Timidity::Instrument *LoadInstrumentOrder(struct Timidity::Renderer *song, int order, int drum, int bank, int program); Timidity::Instrument *LoadInstrumentOrder(struct Timidity::Renderer *song, int order, int drum, int bank, int program);

View file

@ -65,7 +65,7 @@ namespace Timidity
ToneBank *tonebank[MAXBANK], *drumset[MAXBANK]; ToneBank *tonebank[MAXBANK], *drumset[MAXBANK];
static FString def_instr_name; static std::string def_instr_name;
std::unique_ptr<FSoundFontReader> gus_sfreader; std::unique_ptr<FSoundFontReader> gus_sfreader;
static bool InitReader(const char *config_file) static bool InitReader(const char *config_file)
@ -733,8 +733,8 @@ Renderer::Renderer(float sample_rate, const char *args)
default_instrument = NULL; default_instrument = NULL;
default_program = DEFAULT_PROGRAM; default_program = DEFAULT_PROGRAM;
if (def_instr_name.IsNotEmpty()) if (def_instr_name.length() > 0)
set_default_instrument(def_instr_name); set_default_instrument(def_instr_name.c_str());
voices = MAX(*midi_voices, 16); voices = MAX(*midi_voices, 16);
voice = new Voice[voices]; voice = new Voice[voices];

View file

@ -20,8 +20,10 @@
#ifndef TIMIDITY_H #ifndef TIMIDITY_H
#define TIMIDITY_H #define TIMIDITY_H
#include "doomtype.h" #include <stdint.h>
#include "files.h" #include <string>
class FileReader; // this needs to go away.
namespace Timidity namespace Timidity
{ {
@ -185,7 +187,7 @@ enum
VERB_DEBUG VERB_DEBUG
}; };
void cmsg(int type, int verbosity_level, const char *fmt, ...) GCCPRINTF(3,4); void cmsg(int type, int verbosity_level, const char *fmt, ...);
/* /*
@ -338,10 +340,10 @@ instrum_font.cpp
class FontFile class FontFile
{ {
public: public:
FontFile(FString filename); FontFile(const char *filename);
virtual ~FontFile(); virtual ~FontFile();
FString Filename; std::string Filename;
FontFile *Next; FontFile *Next;
virtual Instrument *LoadInstrument(struct Renderer *song, int drum, int bank, int program) = 0; virtual Instrument *LoadInstrument(struct Renderer *song, int drum, int bank, int program) = 0;