- 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)
#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
#endif // __DOOMDEF_H__

View file

@ -23,8 +23,9 @@
#include <stdio.h>
#include <stdlib.h>
#include <exception>
#include "timidity.h"
#include "doomerrors.h"
namespace Timidity
{
@ -34,10 +35,12 @@ namespace Timidity
/* This'll allocate memory or die. */
void *safe_malloc(size_t count)
{
char buffer[80];
void *p;
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)))
{
@ -45,7 +48,8 @@ void *safe_malloc(size_t count)
}
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.
}

View file

@ -30,7 +30,7 @@
#include <memory>
#include "timidity.h"
#include "templates.h"
//#include "templates.h"
#include "gf1patch.h"
#include "i_soundfont.h"
@ -165,14 +165,14 @@ static Instrument *load_instrument(Renderer *song, const char *name, int percuss
if (!fp.isOpen())
{
/* Try with various extensions */
FString tmp = name;
std::string tmp = name;
tmp += ".pat";
fp = gus_sfreader->LookupFile(tmp).first;
fp = gus_sfreader->LookupFile(tmp.c_str()).first;
if (!fp.isOpen())
{
#ifdef __unix__ // Windows isn't case-sensitive.
tmp.ToUpper();
fp = gus_sfreader->LookupFile(tmp).first;
#ifndef _WIN32 // Windows isn't case-sensitive.
std::transform(tmp.begin(), tmp.end(), tmp.begin(), [](unsigned char c){ return toupper(c); } );
fp = gus_sfreader->LookupFile(tmp.c_str()).first;
if (!fp.isOpen())
#endif
{
@ -401,7 +401,7 @@ fail:
{
sp->envelope.gf1.rate[j] = patch_data.EnvelopeRate[j];
/* [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 */

View file

@ -25,8 +25,17 @@
#include <string.h>
#include <math.h>
#include "m_swap.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)
@ -785,7 +794,7 @@ static const char *SourceToString(USHORT usSource)
case CONN_SRC_CC93:
return "CC93";
default:
mysnprintf(unknown, countof(unknown), "UNKNOWN (0x%04x)", usSource);
snprintf(unknown, sizeof(unknown), "UNKNOWN (0x%04x)", usSource);
return unknown;
}
}
@ -803,7 +812,7 @@ static const char *TransformToString(USHORT usTransform)
case CONN_TRN_SWITCH:
return "SWITCH";
default:
mysnprintf(unknown, countof(unknown), "UNKNOWN (0x%04x)", usTransform);
snprintf(unknown, sizeof(unknown), "UNKNOWN (0x%04x)", usTransform);
return unknown;
}
}
@ -877,7 +886,7 @@ static const char *DestinationToString(USHORT usDestination)
case CONN_DST_FILTER_Q:
return "FILTER_Q";
default:
mysnprintf(unknown, countof(unknown), "UNKNOWN (0x%04x)", usDestination);
snprintf(unknown, sizeof(unknown), "UNKNOWN (0x%04x)", usDestination);
return unknown;
}
}

View file

@ -33,7 +33,7 @@ FontFile *font_find(const char *filename)
{
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;
}
@ -108,7 +108,7 @@ Instrument *load_instrument_font_order(struct Renderer *song, int order, int dru
return NULL;
}
FontFile::FontFile(FString filename)
FontFile::FontFile(const char *filename)
: Filename(filename)
{
Next = Fonts;

View file

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

View file

@ -270,7 +270,7 @@ struct SFPerc
struct SFFile : public Timidity::FontFile
{
SFFile(FString filename);
SFFile(const char * filename);
~SFFile();
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);

View file

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

View file

@ -20,8 +20,10 @@
#ifndef TIMIDITY_H
#define TIMIDITY_H
#include "doomtype.h"
#include "files.h"
#include <stdint.h>
#include <string>
class FileReader; // this needs to go away.
namespace Timidity
{
@ -185,7 +187,7 @@ enum
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
{
public:
FontFile(FString filename);
FontFile(const char *filename);
virtual ~FontFile();
FString Filename;
std::string Filename;
FontFile *Next;
virtual Instrument *LoadInstrument(struct Renderer *song, int drum, int bank, int program) = 0;