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 ------------------------------------------------------------
|
|
|
|
|
2019-09-28 14:50:00 +00:00
|
|
|
#include "mididevice.h"
|
2016-03-01 15:47:10 +00:00
|
|
|
#include "timidity/timidity.h"
|
2019-09-24 21:08:56 +00:00
|
|
|
#include "timidity/playmidi.h"
|
|
|
|
#include "timidity/instrum.h"
|
2019-09-28 14:50:00 +00:00
|
|
|
#define USE_BASE_INTERFACE
|
|
|
|
#include "timidity/timidity_file.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 ------------------------------------------------
|
|
|
|
|
2019-09-24 21:08:56 +00:00
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// The actual device.
|
|
|
|
//
|
|
|
|
//==========================================================================
|
2019-06-27 07:56:08 +00:00
|
|
|
|
|
|
|
namespace Timidity { struct Renderer; }
|
|
|
|
|
|
|
|
class TimidityMIDIDevice : public SoftSynthMIDIDevice
|
|
|
|
{
|
2019-09-27 00:31:27 +00:00
|
|
|
void LoadInstruments(GUSConfig *config);
|
2019-06-27 07:56:08 +00:00
|
|
|
public:
|
2019-09-27 00:31:27 +00:00
|
|
|
TimidityMIDIDevice(GUSConfig *config, int samplerate);
|
2019-06-27 07:56:08 +00:00
|
|
|
~TimidityMIDIDevice();
|
|
|
|
|
2019-09-28 08:00:22 +00:00
|
|
|
int OpenRenderer();
|
2019-06-27 07:56:08 +00:00
|
|
|
void PrecacheInstruments(const uint16_t *instruments, int count);
|
|
|
|
int GetDeviceType() const override { return MDEV_GUS; }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
Timidity::Renderer *Renderer;
|
2019-09-27 20:19:00 +00:00
|
|
|
std::shared_ptr<Timidity::Instruments> instruments; // The device needs to hold a reference to this while the renderer is in use.
|
2019-06-27 07:56:08 +00:00
|
|
|
|
|
|
|
void HandleEvent(int status, int parm1, int parm2);
|
|
|
|
void HandleLongEvent(const uint8_t *data, int len);
|
|
|
|
void ComputeOutput(float *buffer, int len);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-03-01 15:47:10 +00:00
|
|
|
// CODE --------------------------------------------------------------------
|
|
|
|
|
2019-09-27 00:31:27 +00:00
|
|
|
|
|
|
|
void TimidityMIDIDevice::LoadInstruments(GUSConfig *config)
|
2019-09-24 21:08:56 +00:00
|
|
|
{
|
2019-09-27 00:31:27 +00:00
|
|
|
if (config->dmxgus.size())
|
2019-09-24 21:08:56 +00:00
|
|
|
{
|
2019-09-27 00:31:27 +00:00
|
|
|
// Check if we got some GUS data before using it.
|
2019-09-28 16:32:25 +00:00
|
|
|
std::string ultradir = getenv("ULTRADIR");
|
|
|
|
if (ultradir.length() || config->gus_patchdir.length() != 0)
|
2019-09-24 21:08:56 +00:00
|
|
|
{
|
2019-09-28 14:50:00 +00:00
|
|
|
auto psreader = new Timidity::BaseSoundFontReader;
|
2019-09-27 00:31:27 +00:00
|
|
|
|
|
|
|
// The GUS put its patches in %ULTRADIR%/MIDI so we can try that
|
2019-09-28 16:32:25 +00:00
|
|
|
if (ultradir.length())
|
2019-09-27 00:31:27 +00:00
|
|
|
{
|
|
|
|
ultradir += "/midi";
|
2019-09-28 16:32:25 +00:00
|
|
|
psreader->timidity_add_path(ultradir.c_str());
|
2019-09-27 00:31:27 +00:00
|
|
|
}
|
|
|
|
// Load DMXGUS lump and patches from gus_patchdir
|
|
|
|
if (config->gus_patchdir.length() != 0) psreader->timidity_add_path(config->gus_patchdir.c_str());
|
|
|
|
|
|
|
|
config->instruments.reset(new Timidity::Instruments(psreader));
|
2019-09-28 14:50:00 +00:00
|
|
|
bool success = config->instruments->LoadDMXGUS(config->gus_memsize, (const char*)config->dmxgus.data(), config->dmxgus.size()) >= 0;
|
2019-09-27 00:31:27 +00:00
|
|
|
|
|
|
|
config->dmxgus.clear();
|
|
|
|
|
|
|
|
if (success)
|
2019-09-24 21:08:56 +00:00
|
|
|
{
|
2019-09-27 00:31:27 +00:00
|
|
|
config->loadedConfig = "DMXGUS";
|
|
|
|
return;
|
2019-09-24 21:08:56 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-27 00:31:27 +00:00
|
|
|
config->loadedConfig = "";
|
|
|
|
config->instruments.reset();
|
|
|
|
throw std::runtime_error("Unable to initialize DMXGUS for GUS MIDI device");
|
2019-09-24 21:08:56 +00:00
|
|
|
}
|
2019-09-27 00:31:27 +00:00
|
|
|
else if (config->reader)
|
2019-09-24 21:08:56 +00:00
|
|
|
{
|
2019-09-27 00:31:27 +00:00
|
|
|
config->loadedConfig = config->readerName;
|
|
|
|
config->instruments.reset(new Timidity::Instruments(config->reader));
|
|
|
|
bool err = config->instruments->LoadConfig() < 0;
|
|
|
|
config->reader = nullptr;
|
|
|
|
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
config->instruments.reset();
|
|
|
|
config->loadedConfig = "";
|
|
|
|
throw std::runtime_error("Unable to initialize instruments for GUS MIDI device");
|
|
|
|
}
|
2019-09-24 21:08:56 +00:00
|
|
|
}
|
2019-09-27 00:31:27 +00:00
|
|
|
else if (config->instruments == nullptr)
|
2019-09-24 21:08:56 +00:00
|
|
|
{
|
2019-09-27 00:31:27 +00:00
|
|
|
throw std::runtime_error("No instruments set for GUS device");
|
2019-09-24 21:08:56 +00:00
|
|
|
}
|
2019-09-27 20:19:00 +00:00
|
|
|
instruments = config->instruments;
|
2019-09-24 21:08:56 +00:00
|
|
|
}
|
|
|
|
|
2016-03-01 15:47:10 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// TimidityMIDIDevice Constructor
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2019-09-27 00:31:27 +00:00
|
|
|
TimidityMIDIDevice::TimidityMIDIDevice(GUSConfig *config, int samplerate)
|
2018-03-06 21:41:27 +00:00
|
|
|
: SoftSynthMIDIDevice(samplerate, 11025, 65535)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2019-09-27 00:31:27 +00:00
|
|
|
LoadInstruments(config);
|
2019-09-27 20:19:00 +00:00
|
|
|
Renderer = new Timidity::Renderer((float)SampleRate, config->midi_voices, instruments.get());
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// TimidityMIDIDevice Destructor
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
TimidityMIDIDevice::~TimidityMIDIDevice()
|
|
|
|
{
|
|
|
|
Close();
|
2017-12-02 11:01:19 +00:00
|
|
|
if (Renderer != nullptr)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
|
|
|
delete Renderer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// TimidityMIDIDevice :: Open
|
|
|
|
//
|
|
|
|
// Returns 0 on success.
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2019-09-28 08:00:22 +00:00
|
|
|
int TimidityMIDIDevice::OpenRenderer()
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2019-09-28 08:00:22 +00:00
|
|
|
Renderer->Reset();
|
2019-09-28 08:04:09 +00:00
|
|
|
return 0;
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2017-03-09 18:54:41 +00:00
|
|
|
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);
|
2018-03-24 17:29:12 +00:00
|
|
|
for (int i = 0; i < len * 2; i++) buffer[i] *= 0.7f;
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
|
2019-06-27 07:56:08 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2019-09-27 00:31:27 +00:00
|
|
|
MIDIDevice *CreateTimidityMIDIDevice(GUSConfig *config, int samplerate)
|
2019-09-24 21:08:56 +00:00
|
|
|
{
|
2019-09-27 00:31:27 +00:00
|
|
|
return new TimidityMIDIDevice(config, samplerate);
|
2019-09-24 21:08:56 +00:00
|
|
|
}
|