2017-03-10 20:34:42 +00:00
|
|
|
/*
|
|
|
|
** music_timiditypp_mididevice.cpp
|
|
|
|
** Provides access to timidity.exe
|
|
|
|
**
|
|
|
|
**---------------------------------------------------------------------------
|
|
|
|
** Copyright 2001-2017 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.
|
|
|
|
**---------------------------------------------------------------------------
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
|
2019-09-28 14:50:00 +00:00
|
|
|
#include "mididevice.h"
|
2017-09-01 17:22:33 +00:00
|
|
|
#include <vector>
|
2017-03-10 15:12:52 +00:00
|
|
|
|
2018-02-20 19:50:01 +00:00
|
|
|
#include "timiditypp/timidity.h"
|
|
|
|
#include "timiditypp/instrum.h"
|
2018-02-20 20:52:45 +00:00
|
|
|
#include "timiditypp/playmidi.h"
|
2016-03-01 15:47:10 +00:00
|
|
|
|
2019-09-23 11:58:16 +00:00
|
|
|
|
2019-09-29 20:01:19 +00:00
|
|
|
TimidityConfig timidityConfig;
|
2019-09-23 11:58:16 +00:00
|
|
|
|
2018-02-20 00:23:56 +00:00
|
|
|
class TimidityPPMIDIDevice : public SoftSynthMIDIDevice
|
2017-03-10 15:12:52 +00:00
|
|
|
{
|
2019-09-27 20:19:00 +00:00
|
|
|
std::shared_ptr<TimidityPlus::Instruments> instruments;
|
2017-03-10 15:12:52 +00:00
|
|
|
public:
|
2019-09-29 20:01:19 +00:00
|
|
|
TimidityPPMIDIDevice(int samplerate);
|
2017-03-10 15:12:52 +00:00
|
|
|
~TimidityPPMIDIDevice();
|
|
|
|
|
2019-09-29 23:51:53 +00:00
|
|
|
int OpenRenderer() override;
|
|
|
|
void PrecacheInstruments(const uint16_t *instruments, int count) override;
|
2019-09-28 14:50:00 +00:00
|
|
|
//std::string GetStats();
|
2018-02-20 00:23:56 +00:00
|
|
|
int GetDeviceType() const override { return MDEV_TIMIDITY; }
|
2017-03-10 15:12:52 +00:00
|
|
|
|
2018-02-21 18:05:14 +00:00
|
|
|
double test[3] = { 0, 0, 0 };
|
|
|
|
|
2017-03-10 15:12:52 +00:00
|
|
|
protected:
|
2018-02-20 20:52:45 +00:00
|
|
|
TimidityPlus::Player *Renderer;
|
2017-03-10 15:12:52 +00:00
|
|
|
|
2019-09-29 23:51:53 +00:00
|
|
|
void HandleEvent(int status, int parm1, int parm2) override;
|
|
|
|
void HandleLongEvent(const uint8_t *data, int len) override;
|
|
|
|
void ComputeOutput(float *buffer, int len) override;
|
2019-09-29 20:01:19 +00:00
|
|
|
void LoadInstruments();
|
2018-02-20 00:23:56 +00:00
|
|
|
};
|
2016-03-01 15:47:10 +00:00
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
2019-09-27 20:19:00 +00:00
|
|
|
//
|
2016-03-01 15:47:10 +00:00
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2019-09-29 20:01:19 +00:00
|
|
|
void TimidityPPMIDIDevice::LoadInstruments()
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2019-09-29 20:01:19 +00:00
|
|
|
if (timidityConfig.reader)
|
2018-02-20 19:50:01 +00:00
|
|
|
{
|
2019-09-29 20:01:19 +00:00
|
|
|
timidityConfig.loadedConfig = timidityConfig.readerName;
|
|
|
|
timidityConfig.instruments.reset(new TimidityPlus::Instruments());
|
|
|
|
bool success = timidityConfig.instruments->load(timidityConfig.reader);
|
|
|
|
timidityConfig.reader = nullptr;
|
2018-03-06 21:41:27 +00:00
|
|
|
|
2019-09-27 20:19:00 +00:00
|
|
|
if (!success)
|
2018-02-23 17:33:54 +00:00
|
|
|
{
|
2019-09-29 20:01:19 +00:00
|
|
|
timidityConfig.instruments.reset();
|
|
|
|
timidityConfig.loadedConfig = "";
|
2019-09-27 20:19:00 +00:00
|
|
|
throw std::runtime_error("Unable to initialize instruments for Timidity++ MIDI device");
|
2018-02-23 17:33:54 +00:00
|
|
|
}
|
2018-02-20 00:23:56 +00:00
|
|
|
}
|
2019-09-29 20:01:19 +00:00
|
|
|
else if (timidityConfig.instruments == nullptr)
|
2017-09-06 21:48:43 +00:00
|
|
|
{
|
2019-09-27 20:19:00 +00:00
|
|
|
throw std::runtime_error("No instruments set for Timidity++ device");
|
2017-09-06 21:48:43 +00:00
|
|
|
}
|
2019-09-29 20:01:19 +00:00
|
|
|
instruments = timidityConfig.instruments;
|
2019-09-27 20:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// TimidityPPMIDIDevice Constructor
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2019-09-29 20:01:19 +00:00
|
|
|
TimidityPPMIDIDevice::TimidityPPMIDIDevice(int samplerate)
|
2019-09-27 22:10:39 +00:00
|
|
|
:SoftSynthMIDIDevice(samplerate, 4000, 65000)
|
2019-09-27 20:19:00 +00:00
|
|
|
{
|
|
|
|
TimidityPlus::set_playback_rate(SampleRate);
|
2019-09-29 20:01:19 +00:00
|
|
|
LoadInstruments();
|
2019-09-27 20:19:00 +00:00
|
|
|
Renderer = new TimidityPlus::Player(instruments.get());
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// TimidityPPMIDIDevice Destructor
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
TimidityPPMIDIDevice::~TimidityPPMIDIDevice ()
|
|
|
|
{
|
2018-02-20 00:23:56 +00:00
|
|
|
Close();
|
|
|
|
if (Renderer != nullptr)
|
2017-11-04 11:15:53 +00:00
|
|
|
{
|
2018-02-20 00:23:56 +00:00
|
|
|
delete Renderer;
|
2017-11-04 11:15:53 +00:00
|
|
|
}
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// TimidityPPMIDIDevice :: Open
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2019-09-28 08:00:22 +00:00
|
|
|
int TimidityPPMIDIDevice::OpenRenderer()
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2019-09-28 08:00:22 +00:00
|
|
|
Renderer->playmidi_stream_init();
|
|
|
|
return 0;
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
2018-02-20 00:23:56 +00:00
|
|
|
// TimidityPPMIDIDevice :: PrecacheInstruments
|
2016-03-01 15:47:10 +00:00
|
|
|
//
|
2018-02-20 00:23:56 +00:00
|
|
|
// 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
|
2016-03-01 15:47:10 +00:00
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2018-02-20 22:36:59 +00:00
|
|
|
void TimidityPPMIDIDevice::PrecacheInstruments(const uint16_t *instrumentlist, int count)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2018-02-21 01:02:29 +00:00
|
|
|
if (instruments != nullptr)
|
|
|
|
instruments->PrecacheInstruments(instrumentlist, count);
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
2018-02-20 00:23:56 +00:00
|
|
|
// TimidityPPMIDIDevice :: HandleEvent
|
2016-03-01 15:47:10 +00:00
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2018-02-20 00:23:56 +00:00
|
|
|
void TimidityPPMIDIDevice::HandleEvent(int status, int parm1, int parm2)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2018-02-21 01:02:29 +00:00
|
|
|
if (Renderer != nullptr)
|
2018-02-21 19:44:51 +00:00
|
|
|
Renderer->send_event(status, parm1, parm2);
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
2018-02-20 00:23:56 +00:00
|
|
|
// TimidityPPMIDIDevice :: HandleLongEvent
|
2016-03-01 15:47:10 +00:00
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2018-02-20 00:23:56 +00:00
|
|
|
void TimidityPPMIDIDevice::HandleLongEvent(const uint8_t *data, int len)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2018-02-21 01:02:29 +00:00
|
|
|
if (Renderer != nullptr)
|
2018-02-21 19:44:51 +00:00
|
|
|
Renderer->send_long_event(data, len);
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
2018-02-20 00:23:56 +00:00
|
|
|
// TimidityPPMIDIDevice :: ComputeOutput
|
2016-03-01 15:47:10 +00:00
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2018-02-20 00:23:56 +00:00
|
|
|
void TimidityPPMIDIDevice::ComputeOutput(float *buffer, int len)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2018-02-21 01:02:29 +00:00
|
|
|
if (Renderer != nullptr)
|
2018-02-21 16:35:44 +00:00
|
|
|
Renderer->compute_data(buffer, len);
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
2018-02-23 16:27:36 +00:00
|
|
|
//
|
2016-03-01 15:47:10 +00:00
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2019-09-29 20:01:19 +00:00
|
|
|
bool Timidity_SetupConfig(const char* args)
|
|
|
|
{
|
|
|
|
if (*args == 0) args = timidityConfig.timidity_config.c_str();
|
|
|
|
if (stricmp(timidityConfig.loadedConfig.c_str(), args) == 0) return false; // aleady loaded
|
|
|
|
|
2019-09-29 23:51:53 +00:00
|
|
|
MusicIO::SoundFontReaderInterface* reader = nullptr;
|
2019-09-29 20:01:19 +00:00
|
|
|
if (musicCallbacks.OpenSoundFont)
|
|
|
|
{
|
|
|
|
reader = musicCallbacks.OpenSoundFont(args, SF_GUS | SF_SF2);
|
|
|
|
}
|
|
|
|
else if (MusicIO::fileExists(args))
|
|
|
|
{
|
|
|
|
reader = new MusicIO::FileSystemSoundFontReader(args, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (reader == nullptr)
|
|
|
|
{
|
|
|
|
char error[80];
|
|
|
|
snprintf(error, 80, "Timidity++: %s: Unable to load sound font\n", args);
|
|
|
|
throw std::runtime_error(error);
|
|
|
|
}
|
|
|
|
timidityConfig.reader = reader;
|
|
|
|
timidityConfig.readerName = args;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
MIDIDevice *CreateTimidityPPMIDIDevice(const char *Args, int samplerate)
|
2017-03-10 15:12:52 +00:00
|
|
|
{
|
2019-09-29 20:01:19 +00:00
|
|
|
Timidity_SetupConfig(Args);
|
|
|
|
return new TimidityPPMIDIDevice(samplerate);
|
2017-03-10 15:12:52 +00:00
|
|
|
}
|
2018-02-20 19:50:01 +00:00
|
|
|
|
2019-09-27 20:19:00 +00:00
|
|
|
|
2018-02-20 22:36:59 +00:00
|
|
|
void TimidityPP_Shutdown()
|
|
|
|
{
|
2018-02-21 20:11:31 +00:00
|
|
|
TimidityPlus::free_gauss_table();
|
2018-02-21 00:29:39 +00:00
|
|
|
TimidityPlus::free_global_mblock();
|
2018-02-20 22:36:59 +00:00
|
|
|
}
|