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.
|
|
|
|
**---------------------------------------------------------------------------
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
|
2017-03-10 15:12:52 +00:00
|
|
|
#include "i_midi_win32.h"
|
|
|
|
|
2017-09-01 17:22:33 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2017-03-10 15:12:52 +00:00
|
|
|
|
2016-03-01 15:47:10 +00:00
|
|
|
#include "i_musicinterns.h"
|
|
|
|
#include "c_cvars.h"
|
|
|
|
#include "cmdlib.h"
|
|
|
|
#include "templates.h"
|
|
|
|
#include "version.h"
|
2017-11-04 11:15:53 +00:00
|
|
|
#include "tmpfileplus.h"
|
2017-12-02 16:33:43 +00:00
|
|
|
#include "m_misc.h"
|
2018-02-20 19:50:01 +00:00
|
|
|
#include "v_text.h"
|
|
|
|
|
|
|
|
#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
|
|
|
|
2018-02-20 00:23:56 +00:00
|
|
|
class TimidityPPMIDIDevice : public SoftSynthMIDIDevice
|
2017-03-10 15:12:52 +00:00
|
|
|
{
|
2018-02-20 00:23:56 +00:00
|
|
|
TArray<uint8_t> midi;
|
2018-02-20 19:50:01 +00:00
|
|
|
static TimidityPlus::Instruments *instruments;
|
2017-03-10 15:12:52 +00:00
|
|
|
public:
|
|
|
|
TimidityPPMIDIDevice(const char *args);
|
|
|
|
~TimidityPPMIDIDevice();
|
|
|
|
|
|
|
|
int Open(MidiCallback, void *userdata);
|
2018-02-20 00:23:56 +00:00
|
|
|
void PrecacheInstruments(const uint16_t *instruments, int count);
|
|
|
|
//FString GetStats();
|
|
|
|
int GetDeviceType() const override { return MDEV_TIMIDITY; }
|
2017-03-10 15:12:52 +00:00
|
|
|
bool Preprocess(MIDIStreamer *song, bool looping);
|
|
|
|
void TimidityVolumeChanged();
|
|
|
|
|
|
|
|
protected:
|
2018-02-20 20:52:45 +00:00
|
|
|
TimidityPlus::Player *Renderer;
|
2017-03-10 15:12:52 +00:00
|
|
|
|
2018-02-20 00:23:56 +00:00
|
|
|
void HandleEvent(int status, int parm1, int parm2);
|
|
|
|
void HandleLongEvent(const uint8_t *data, int len);
|
|
|
|
void ComputeOutput(float *buffer, int len);
|
|
|
|
};
|
2018-02-20 19:50:01 +00:00
|
|
|
TimidityPlus::Instruments *TimidityPPMIDIDevice::instruments;
|
2017-03-10 15:12:52 +00:00
|
|
|
|
2018-02-20 00:23:56 +00:00
|
|
|
// Config file to use
|
2018-02-20 19:50:01 +00:00
|
|
|
CVAR(String, timidity_config, "timidity.cfg", CVAR_ARCHIVE | CVAR_GLOBALCONFIG)
|
2016-03-01 15:47:10 +00:00
|
|
|
|
|
|
|
// added because Timidity's output is rather loud.
|
|
|
|
CUSTOM_CVAR (Float, timidity_mastervolume, 1.0f, CVAR_ARCHIVE|CVAR_GLOBALCONFIG)
|
|
|
|
{
|
|
|
|
if (self < 0.f)
|
|
|
|
self = 0.f;
|
|
|
|
else if (self > 4.f)
|
|
|
|
self = 4.f;
|
|
|
|
if (currSong != NULL)
|
|
|
|
currSong->TimidityVolumeChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CUSTOM_CVAR (Int, timidity_frequency, 44100, CVAR_ARCHIVE|CVAR_GLOBALCONFIG)
|
|
|
|
{ // Clamp frequency to Timidity's limits
|
|
|
|
if (self < 4000)
|
|
|
|
self = 4000;
|
|
|
|
else if (self > 65000)
|
|
|
|
self = 65000;
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// TimidityPPMIDIDevice Constructor
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2018-02-20 00:23:56 +00:00
|
|
|
TimidityPPMIDIDevice::TimidityPPMIDIDevice(const char *args)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2018-02-20 00:23:56 +00:00
|
|
|
if (args == NULL || *args == 0) args = timidity_config;
|
|
|
|
|
2018-02-20 19:50:01 +00:00
|
|
|
if (instruments != nullptr && !instruments->checkConfig(args))
|
|
|
|
{
|
|
|
|
delete instruments;
|
|
|
|
}
|
|
|
|
instruments = new TimidityPlus::Instruments;
|
|
|
|
if (!instruments->load(args))
|
2018-02-20 00:23:56 +00:00
|
|
|
{
|
2018-02-20 19:50:01 +00:00
|
|
|
delete instruments;
|
|
|
|
instruments = nullptr;
|
2018-02-20 00:23:56 +00:00
|
|
|
}
|
2018-02-20 19:50:01 +00:00
|
|
|
if (instruments != nullptr)
|
2017-09-06 21:48:43 +00:00
|
|
|
{
|
2018-02-20 20:52:45 +00:00
|
|
|
Renderer = new TimidityPlus::Player(timidity_frequency, instruments);
|
2017-09-06 21:48:43 +00:00
|
|
|
}
|
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
|
|
|
}
|
2018-02-20 00:23:56 +00:00
|
|
|
*/
|
2017-11-04 11:15:53 +00:00
|
|
|
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// TimidityPPMIDIDevice :: Preprocess
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2018-02-20 19:50:01 +00:00
|
|
|
namespace TimidityPlus
|
|
|
|
{
|
2018-02-20 20:52:45 +00:00
|
|
|
int load_midi_file(FileReader *fr, TimidityPlus::Player *p);
|
2018-02-20 19:50:01 +00:00
|
|
|
void run_midi(int samples);
|
|
|
|
void timidity_close();
|
|
|
|
}
|
|
|
|
|
2016-03-01 15:47:10 +00:00
|
|
|
bool TimidityPPMIDIDevice::Preprocess(MIDIStreamer *song, bool looping)
|
|
|
|
{
|
|
|
|
// Write MIDI song to temporary file
|
|
|
|
song->CreateSMF(midi, looping ? 0 : 1);
|
2018-02-20 19:50:01 +00:00
|
|
|
MemoryReader fr((char*)&midi[0], midi.Size());
|
2018-02-20 20:52:45 +00:00
|
|
|
return !TimidityPlus::load_midi_file(&fr, Renderer);
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// TimidityPPMIDIDevice :: Open
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2018-02-20 00:23:56 +00:00
|
|
|
int TimidityPPMIDIDevice::Open(MidiCallback callback, void *userdata)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2018-02-20 19:50:01 +00:00
|
|
|
// No instruments loaded means we cannot play...
|
|
|
|
if (instruments == nullptr) return 0;
|
|
|
|
|
2018-02-20 00:23:56 +00:00
|
|
|
int ret = OpenStream(2, 0, callback, userdata);
|
|
|
|
if (ret == 0)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2018-02-20 00:23:56 +00:00
|
|
|
//Renderer->Reset();
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
2018-02-20 00:23:56 +00:00
|
|
|
return ret;
|
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 00:23:56 +00:00
|
|
|
void TimidityPPMIDIDevice::PrecacheInstruments(const uint16_t *instruments, int count)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2018-02-20 00:23:56 +00:00
|
|
|
for (int i = 0; i < count; ++i)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2018-02-20 00:23:56 +00:00
|
|
|
//Renderer->MarkInstrument((instruments[i] >> 7) & 127, instruments[i] >> 14, instruments[i] & 127);
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
2018-02-20 00:23:56 +00:00
|
|
|
//Renderer->load_missing_instruments();
|
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-20 00:23:56 +00:00
|
|
|
//Renderer->HandleEvent(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-20 00:23:56 +00:00
|
|
|
//Renderer->HandleLongMessage(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-20 00:23:56 +00:00
|
|
|
TimidityPlus::run_midi(len / 8); // bytes to samples
|
|
|
|
memset(buffer, len, 0); // to do
|
|
|
|
|
|
|
|
//Renderer->ComputeOutput(buffer, len);
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
2018-02-20 00:23:56 +00:00
|
|
|
// TimidityPPMIDIDevice :: TimidityVolumeChanged
|
2016-03-01 15:47:10 +00:00
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2018-02-20 00:23:56 +00:00
|
|
|
void TimidityPPMIDIDevice::TimidityVolumeChanged()
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2018-02-20 00:23:56 +00:00
|
|
|
if (Stream != NULL)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2018-02-20 00:23:56 +00:00
|
|
|
Stream->SetVolume(timidity_mastervolume);
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-10 15:12:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
MIDIDevice *CreateTimidityPPMIDIDevice(const char *args)
|
|
|
|
{
|
|
|
|
return new TimidityPPMIDIDevice(args);
|
|
|
|
}
|
2018-02-20 19:50:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
void TimidityPlus::ctl_cmsg(int type, int verbosity_level, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
if (verbosity_level >= 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 CMSG_ERROR:
|
|
|
|
Printf(TEXTCOLOR_RED "%s\n", msg.GetChars());
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CMSG_WARNING:
|
|
|
|
Printf(TEXTCOLOR_YELLOW "%s\n", msg.GetChars());
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CMSG_INFO:
|
|
|
|
DPrintf(DMSG_SPAMMY, "%s\n", msg.GetChars());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|