2016-03-01 15:47:10 +00:00
|
|
|
/*
|
|
|
|
** music_opl_mididevice.cpp
|
|
|
|
** Provides an emulated OPL implementation of a MIDI output 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.
|
|
|
|
**---------------------------------------------------------------------------
|
|
|
|
**
|
2019-09-26 23:01:52 +00:00
|
|
|
**
|
2016-03-01 15:47:10 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
// HEADER FILES ------------------------------------------------------------
|
|
|
|
|
2020-01-02 11:43:37 +00:00
|
|
|
#include "zmusic/zmusic_internal.h"
|
2019-09-28 14:50:00 +00:00
|
|
|
#include "mididevice.h"
|
2019-09-28 16:32:25 +00:00
|
|
|
#include "zmusic/mus2midi.h"
|
2020-01-02 11:43:37 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_OPL
|
2019-09-26 18:06:39 +00:00
|
|
|
#include "oplsynth/opl.h"
|
2019-09-28 14:50:00 +00:00
|
|
|
#include "oplsynth/opl_mus_player.h"
|
2016-03-01 15:47:10 +00:00
|
|
|
|
|
|
|
// MACROS ------------------------------------------------------------------
|
|
|
|
|
|
|
|
// EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
|
|
|
|
|
|
|
|
// PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
|
|
|
|
|
|
|
|
// PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
|
|
|
|
|
|
|
|
// EXTERNAL DATA DECLARATIONS ----------------------------------------------
|
|
|
|
|
|
|
|
// PRIVATE DATA DEFINITIONS ------------------------------------------------
|
|
|
|
|
2019-09-29 20:01:19 +00:00
|
|
|
OPLConfig oplConfig;
|
|
|
|
|
2019-09-26 23:01:52 +00:00
|
|
|
// OPL implementation of a MIDI output device -------------------------------
|
|
|
|
|
|
|
|
class OPLMIDIDevice : public SoftSynthMIDIDevice, protected OPLmusicBlock
|
|
|
|
{
|
|
|
|
public:
|
2019-09-29 20:01:19 +00:00
|
|
|
OPLMIDIDevice(int core);
|
2019-09-29 23:51:53 +00:00
|
|
|
int OpenRenderer() override;
|
|
|
|
void Close() override;
|
|
|
|
int GetTechnology() const override;
|
2019-09-28 14:50:00 +00:00
|
|
|
std::string GetStats() override;
|
2019-09-26 23:01:52 +00:00
|
|
|
|
|
|
|
protected:
|
2019-09-29 23:51:53 +00:00
|
|
|
void CalcTickRate() override;
|
|
|
|
int PlayTick() override;
|
|
|
|
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;
|
|
|
|
bool ServiceStream(void *buff, int numbytes) override;
|
2019-09-26 23:01:52 +00:00
|
|
|
int GetDeviceType() const override { return MDEV_OPL; }
|
|
|
|
};
|
|
|
|
|
2016-03-01 15:47:10 +00:00
|
|
|
|
2019-09-26 23:01:52 +00:00
|
|
|
// PUBLIC DATA DEFINITIONS -------------------------------------------------
|
2016-03-01 15:47:10 +00:00
|
|
|
|
|
|
|
// CODE --------------------------------------------------------------------
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// OPLMIDIDevice Contructor
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2019-09-29 20:01:19 +00:00
|
|
|
OPLMIDIDevice::OPLMIDIDevice(int core)
|
|
|
|
: SoftSynthMIDIDevice((int)OPL_SAMPLE_RATE), OPLmusicBlock(core, oplConfig.numchips)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2019-09-29 20:01:19 +00:00
|
|
|
FullPan = oplConfig.fullpan;
|
|
|
|
memcpy(OPLinstruments, oplConfig.OPLinstruments, sizeof(OPLinstruments));
|
2019-09-28 08:00:22 +00:00
|
|
|
StreamBlockSize = 14;
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// OPLMIDIDevice :: Open
|
|
|
|
//
|
|
|
|
// Returns 0 on success.
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2019-09-28 08:00:22 +00:00
|
|
|
int OPLMIDIDevice::OpenRenderer()
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2019-09-26 23:01:52 +00:00
|
|
|
if (io == NULL || 0 == (NumChips = io->Init(currentCore, NumChips, FullPan, true)))
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
2019-09-28 08:00:22 +00:00
|
|
|
isMono = !FullPan && !io->IsOPL3;
|
|
|
|
stopAllVoices();
|
|
|
|
resetAllControllers(100);
|
|
|
|
return 0;
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// OPLMIDIDevice :: Close
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
void OPLMIDIDevice::Close()
|
|
|
|
{
|
|
|
|
SoftSynthMIDIDevice::Close();
|
2017-04-15 23:36:53 +00:00
|
|
|
io->Reset();
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// OPLMIDIDevice :: GetTechnology
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
int OPLMIDIDevice::GetTechnology() const
|
|
|
|
{
|
2017-03-10 11:39:23 +00:00
|
|
|
return MIDIDEV_FMSYNTH;
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// OPLMIDIDevice :: CalcTickRate
|
|
|
|
//
|
|
|
|
// Tempo is the number of microseconds per quarter note.
|
|
|
|
// Division is the number of ticks per quarter note.
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
void OPLMIDIDevice::CalcTickRate()
|
|
|
|
{
|
|
|
|
SoftSynthMIDIDevice::CalcTickRate();
|
|
|
|
io->SetClockRate(OPLmusicBlock::SamplesPerTick = SoftSynthMIDIDevice::SamplesPerTick);
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// OPLMIDIDevice :: PlayTick
|
|
|
|
//
|
|
|
|
// We derive from two base classes that both define PlayTick(), so we need
|
|
|
|
// to be unambiguous about which one to use.
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
int OPLMIDIDevice::PlayTick()
|
|
|
|
{
|
|
|
|
return SoftSynthMIDIDevice::PlayTick();
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// OPLMIDIDevice :: HandleEvent
|
|
|
|
//
|
|
|
|
// Processes a normal MIDI event.
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
void OPLMIDIDevice::HandleEvent(int status, int parm1, int parm2)
|
|
|
|
{
|
|
|
|
int command = status & 0xF0;
|
|
|
|
int channel = status & 0x0F;
|
|
|
|
|
2017-04-15 23:36:53 +00:00
|
|
|
// Swap voices 9 and 15, because their roles are reversed
|
2016-03-01 15:47:10 +00:00
|
|
|
// in MUS and MIDI formats.
|
|
|
|
if (channel == 9)
|
|
|
|
{
|
|
|
|
channel = 15;
|
|
|
|
}
|
|
|
|
else if (channel == 15)
|
|
|
|
{
|
|
|
|
channel = 9;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (command)
|
|
|
|
{
|
|
|
|
case MIDI_NOTEOFF:
|
|
|
|
playingcount--;
|
2017-04-15 23:36:53 +00:00
|
|
|
noteOff(channel, parm1);
|
2016-03-01 15:47:10 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MIDI_NOTEON:
|
|
|
|
playingcount++;
|
2017-04-15 23:36:53 +00:00
|
|
|
noteOn(channel, parm1, parm2);
|
2016-03-01 15:47:10 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MIDI_POLYPRESS:
|
2019-09-26 23:01:52 +00:00
|
|
|
//DEBUGOUT("Unhandled note aftertouch: Channel %d, note %d, value %d\n", channel, parm1, parm2);
|
2016-03-01 15:47:10 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MIDI_CTRLCHANGE:
|
|
|
|
switch (parm1)
|
|
|
|
{
|
2017-04-15 23:36:53 +00:00
|
|
|
// some controllers here get passed on but are not handled by the player.
|
|
|
|
//case 0: changeBank(channel, parm2); break;
|
|
|
|
case 1: changeModulation(channel, parm2); break;
|
|
|
|
case 6: changeExtended(channel, ctrlDataEntryHi, parm2); break;
|
|
|
|
case 7: changeVolume(channel, parm2, false); break;
|
|
|
|
case 10: changePanning(channel, parm2); break;
|
|
|
|
case 11: changeVolume(channel, parm2, true); break;
|
|
|
|
case 38: changeExtended(channel, ctrlDataEntryLo, parm2); break;
|
|
|
|
case 64: changeSustain(channel, parm2); break;
|
|
|
|
//case 67: changeSoftPedal(channel, parm2); break;
|
|
|
|
//case 91: changeReverb(channel, parm2); break;
|
|
|
|
//case 93: changeChorus(channel, parm2); break;
|
|
|
|
case 98: changeExtended(channel, ctrlNRPNLo, parm2); break;
|
|
|
|
case 99: changeExtended(channel, ctrlNRPNHi, parm2); break;
|
|
|
|
case 100: changeExtended(channel, ctrlRPNLo, parm2); break;
|
|
|
|
case 101: changeExtended(channel, ctrlRPNHi, parm2); break;
|
|
|
|
case 120: allNotesOff(channel, parm2); break;
|
|
|
|
case 121: resetControllers(channel, 100); break;
|
|
|
|
case 123: notesOff(channel, parm2); break;
|
|
|
|
//case 126: changeMono(channel, parm2); break;
|
|
|
|
//case 127: changePoly(channel, parm2); break;
|
2016-03-01 15:47:10 +00:00
|
|
|
default:
|
2019-09-26 23:01:52 +00:00
|
|
|
//DEBUGOUT("Unhandled controller: Channel %d, controller %d, value %d\n", channel, parm1, parm2);
|
2016-03-01 15:47:10 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MIDI_PRGMCHANGE:
|
2017-04-15 23:36:53 +00:00
|
|
|
programChange(channel, parm1);
|
2016-03-01 15:47:10 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MIDI_CHANPRESS:
|
2019-09-26 23:01:52 +00:00
|
|
|
//DEBUGOUT("Unhandled channel aftertouch: Channel %d, value %d\n", channel, parm1, 0);
|
2016-03-01 15:47:10 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MIDI_PITCHBEND:
|
2017-04-15 23:36:53 +00:00
|
|
|
changePitch(channel, parm1, parm2);
|
2016-03-01 15:47:10 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// OPLMIDIDevice :: HandleLongEvent
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2017-03-08 17:47:52 +00:00
|
|
|
void OPLMIDIDevice::HandleLongEvent(const uint8_t *data, int len)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// OPLMIDIDevice :: ComputeOutput
|
|
|
|
//
|
|
|
|
// We override ServiceStream, so this function is never actually called.
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
void OPLMIDIDevice::ComputeOutput(float *buffer, int len)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// OPLMIDIDevice :: ServiceStream
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
bool OPLMIDIDevice::ServiceStream(void *buff, int numbytes)
|
|
|
|
{
|
|
|
|
return OPLmusicBlock::ServiceStream(buff, numbytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// OPLMIDIDevice :: GetStats
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2019-09-28 14:50:00 +00:00
|
|
|
std::string OPLMIDIDevice::GetStats()
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2019-09-28 14:50:00 +00:00
|
|
|
std::string out;
|
2017-04-15 23:36:53 +00:00
|
|
|
for (uint32_t i = 0; i < io->NumChannels; ++i)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2019-09-26 23:01:52 +00:00
|
|
|
char star = '*';
|
2017-04-23 13:40:03 +00:00
|
|
|
if (voices[i].index == ~0u)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2019-09-26 23:01:52 +00:00
|
|
|
star = '/';
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
2017-04-15 23:36:53 +00:00
|
|
|
else if (voices[i].sustained)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2019-09-26 23:01:52 +00:00
|
|
|
star = '-';
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
2017-04-15 23:36:53 +00:00
|
|
|
else if (voices[i].current_instr_voice == &voices[i].current_instr->voices[1])
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2019-09-26 23:01:52 +00:00
|
|
|
star = '\\';
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-09-26 23:01:52 +00:00
|
|
|
star = '+';
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
2019-09-26 23:01:52 +00:00
|
|
|
out += star;
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
2019-09-26 23:01:52 +00:00
|
|
|
|
2019-09-29 20:01:19 +00:00
|
|
|
|
|
|
|
MIDIDevice* CreateOplMIDIDevice(const char *Args)
|
2019-09-26 23:01:52 +00:00
|
|
|
{
|
2019-09-29 20:01:19 +00:00
|
|
|
if (!oplConfig.genmidiset) throw std::runtime_error("Cannot play OPL without GENMIDI data");
|
|
|
|
int core = oplConfig.core;
|
|
|
|
if (Args != NULL && *Args >= '0' && *Args < '4') core = *Args - '0';
|
|
|
|
return new OPLMIDIDevice(core);
|
2019-09-26 23:01:52 +00:00
|
|
|
}
|
2020-01-02 11:43:37 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
MIDIDevice* CreateOplMIDIDevice(const char* Args)
|
|
|
|
{
|
|
|
|
throw std::runtime_error("OPL device not supported in this configuration");
|
|
|
|
}
|
|
|
|
#endif
|