gzdoom/src/sound/mididevices/music_timidity_mididevice.cpp

341 lines
9.8 KiB
C++
Raw Normal View History

2016-03-01 15:47:10 +00:00
/*
** music_timidity_mididevice.cpp
** Provides access to TiMidity as a generic MIDI device.
**
**---------------------------------------------------------------------------
** Copyright 2008 Randy Heit
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions
** are met:
**
** 1. Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** 2. Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in the
** documentation and/or other materials provided with the distribution.
** 3. The name of the author may not be used to endorse or promote products
** derived from this software without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**---------------------------------------------------------------------------
**
*/
// HEADER FILES ------------------------------------------------------------
#include "i_musicinterns.h"
#include "v_text.h"
#include "timidity/timidity.h"
#include "timidity/playmidi.h"
#include "timidity/instrum.h"
#include "i_soundfont.h"
#include "doomerrors.h"
2016-03-01 15:47:10 +00:00
// MACROS ------------------------------------------------------------------
// TYPES -------------------------------------------------------------------
// EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
// PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
// PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
// EXTERNAL DATA DECLARATIONS ----------------------------------------------
// PRIVATE DATA DEFINITIONS ------------------------------------------------
// CVARS for this device - all of them require a device reset --------------------------------------------
static void CheckRestart()
{
if (currSong != nullptr && currSong->GetDeviceType() == MDEV_GUS)
{
MIDIDeviceChanged(-1, true);
}
}
CUSTOM_CVAR(String, midi_config, "gzdoom", CVAR_ARCHIVE | CVAR_GLOBALCONFIG)
{
CheckRestart();
}
CUSTOM_CVAR(Bool, midi_dmxgus, false, CVAR_ARCHIVE | CVAR_GLOBALCONFIG) // This was 'true' but since it requires special setup that's not such a good idea.
{
CheckRestart();
}
CUSTOM_CVAR(String, gus_patchdir, "", CVAR_ARCHIVE | CVAR_GLOBALCONFIG)
{
CheckRestart();
}
CUSTOM_CVAR(Int, midi_voices, 32, CVAR_ARCHIVE | CVAR_GLOBALCONFIG)
{
CheckRestart();
}
CUSTOM_CVAR(Int, gus_memsize, 0, CVAR_ARCHIVE | CVAR_GLOBALCONFIG)
{
CheckRestart();
}
//==========================================================================
//
// Error printing override to redirect to the internal console instead of stdout.
//
//==========================================================================
static void gzdoom_ctl_cmsg(int type, int verbosity_level, const char* fmt, ...)
{
if (verbosity_level >= Timidity::VERB_DEBUG) return; // Don't waste time on diagnostics.
va_list args;
va_start(args, fmt);
FString msg;
msg.VFormat(fmt, args);
va_end(args);
switch (type)
{
case Timidity::CMSG_ERROR:
Printf(TEXTCOLOR_RED "%s\n", msg.GetChars());
break;
case Timidity::CMSG_WARNING:
Printf(TEXTCOLOR_YELLOW "%s\n", msg.GetChars());
break;
case Timidity::CMSG_INFO:
DPrintf(DMSG_SPAMMY, "%s\n", msg.GetChars());
break;
}
}
//==========================================================================
//
// The actual device.
//
//==========================================================================
namespace Timidity { struct Renderer; }
class TimidityMIDIDevice : public SoftSynthMIDIDevice
{
public:
TimidityMIDIDevice(const char *args, int samplerate);
~TimidityMIDIDevice();
int Open(MidiCallback, void *userdata);
void PrecacheInstruments(const uint16_t *instruments, int count);
int GetDeviceType() const override { return MDEV_GUS; }
protected:
Timidity::Renderer *Renderer;
void HandleEvent(int status, int parm1, int parm2);
void HandleLongEvent(const uint8_t *data, int len);
void ComputeOutput(float *buffer, int len);
void LoadConfig(const char *);
};
// DATA DEFINITIONS -------------------------------------------------
static FString currentConfig;
static Timidity::Instruments* instruments;
2016-03-01 15:47:10 +00:00
// CODE --------------------------------------------------------------------
void TimidityMIDIDevice::LoadConfig(const char *config)
{
if ((midi_dmxgus && *config == 0) || !stricmp(config, "DMXGUS"))
{
if (currentConfig.CompareNoCase("DMXGUS") == 0) return; // aleady loaded
int lump = Wads.CheckNumForName("DMXGUS");
if (lump == -1) lump = Wads.CheckNumForName("DMXGUSC");
if (lump >= 0)
{
auto data = Wads.OpenLumpReader(lump);
if (data.GetLength() > 0)
{
// Check if we got some GUS data before using it.
FString ultradir = getenv("ULTRADIR");
if (ultradir.IsNotEmpty() || *(*gus_patchdir) != 0)
{
auto psreader = new FPatchSetReader(data);
// The GUS put its patches in %ULTRADIR%/MIDI so we can try that
if (ultradir.IsNotEmpty())
{
ultradir += "/midi";
psreader->AddPath(ultradir);
}
if (instruments) delete instruments;
instruments = new Timidity::Instruments(psreader);
// Load DMXGUS lump and patches from gus_patchdir
if (*(*gus_patchdir) != 0) psreader->AddPath(gus_patchdir);
if (instruments->LoadDMXGUS(gus_memsize) < 0)
{
delete instruments;
instruments = nullptr;
I_Error("Unable to initialize instruments for GUS MIDI device");
}
currentConfig = "DMXGUS";
}
}
}
}
if (*config == 0) config = midi_config;
if (currentConfig.CompareNoCase(config) == 0) return;
if (instruments) delete instruments;
instruments = nullptr;
auto reader = sfmanager.OpenSoundFont(config, SF_GUS | SF_SF2);
if (reader == nullptr)
{
I_Error("GUS: %s: Unable to load sound font\n",config);
}
instruments = new Timidity::Instruments(reader);
if (instruments->LoadConfig() < 0)
{
delete instruments;
instruments = nullptr;
I_Error("Unable to initialize instruments for GUS MIDI device");
}
currentConfig = config;
}
2016-03-01 15:47:10 +00:00
//==========================================================================
//
// TimidityMIDIDevice Constructor
//
//==========================================================================
TimidityMIDIDevice::TimidityMIDIDevice(const char *args, int samplerate)
: SoftSynthMIDIDevice(samplerate, 11025, 65535)
2016-03-01 15:47:10 +00:00
{
LoadConfig(args);
Timidity::printMessage = gzdoom_ctl_cmsg;
Renderer = new Timidity::Renderer((float)SampleRate, midi_voices, instruments);
2016-03-01 15:47:10 +00:00
}
//==========================================================================
//
// TimidityMIDIDevice Destructor
//
//==========================================================================
TimidityMIDIDevice::~TimidityMIDIDevice()
{
Close();
if (Renderer != nullptr)
2016-03-01 15:47:10 +00:00
{
delete Renderer;
}
}
//==========================================================================
//
// TimidityMIDIDevice :: Open
//
// Returns 0 on success.
//
//==========================================================================
int TimidityMIDIDevice::Open(MidiCallback callback, void *userdata)
2016-03-01 15:47:10 +00:00
{
int ret = OpenStream(2, 0, callback, userdata);
if (ret == 0)
{
Renderer->Reset();
}
return ret;
}
//==========================================================================
//
// TimidityMIDIDevice :: PrecacheInstruments
//
// Each entry is packed as follows:
// Bits 0- 6: Instrument number
// Bits 7-13: Bank number
// Bit 14: Select drum set if 1, tone bank if 0
//
//==========================================================================
2017-03-08 14:52:09 +00:00
void TimidityMIDIDevice::PrecacheInstruments(const uint16_t *instruments, int count)
2016-03-01 15:47:10 +00:00
{
for (int i = 0; i < count; ++i)
{
Renderer->MarkInstrument((instruments[i] >> 7) & 127, instruments[i] >> 14, instruments[i] & 127);
}
Renderer->load_missing_instruments();
}
//==========================================================================
//
// TimidityMIDIDevice :: HandleEvent
//
//==========================================================================
void TimidityMIDIDevice::HandleEvent(int status, int parm1, int parm2)
{
Renderer->HandleEvent(status, parm1, parm2);
}
//==========================================================================
//
// TimidityMIDIDevice :: HandleLongEvent
//
//==========================================================================
void TimidityMIDIDevice::HandleLongEvent(const uint8_t *data, int len)
2016-03-01 15:47:10 +00:00
{
Renderer->HandleLongMessage(data, len);
}
//==========================================================================
//
// TimidityMIDIDevice :: ComputeOutput
//
//==========================================================================
void TimidityMIDIDevice::ComputeOutput(float *buffer, int len)
{
Renderer->ComputeOutput(buffer, len);
for (int i = 0; i < len * 2; i++) buffer[i] *= 0.7f;
2016-03-01 15:47:10 +00:00
}
//==========================================================================
//
//
//
//==========================================================================
MIDIDevice *CreateTimidityMIDIDevice(const char *args, int samplerate)
{
return new TimidityMIDIDevice(args, samplerate);
}
void Timidity_Shutdown()
{
if (instruments) delete instruments;
instruments = nullptr;
}