2016-05-19 02:19:43 +00:00
|
|
|
#include "amuse/SongState.hpp"
|
|
|
|
#include "amuse/Common.hpp"
|
|
|
|
#include "amuse/Sequencer.hpp"
|
2016-05-19 05:56:45 +00:00
|
|
|
#include <cmath>
|
2016-05-19 02:19:43 +00:00
|
|
|
|
|
|
|
namespace amuse
|
|
|
|
{
|
|
|
|
|
|
|
|
static uint32_t DecodeRLE(const unsigned char*& data)
|
|
|
|
{
|
|
|
|
uint32_t ret = 0;
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
uint32_t thisPart = *data & 0x7f;
|
|
|
|
if (*data & 0x80)
|
|
|
|
{
|
|
|
|
++data;
|
|
|
|
thisPart = thisPart * 256 + *data;
|
|
|
|
if (thisPart == 0)
|
2016-07-02 21:50:38 +00:00
|
|
|
{
|
|
|
|
++data;
|
2016-05-19 02:19:43 +00:00
|
|
|
return -1;
|
2016-07-02 21:50:38 +00:00
|
|
|
}
|
2016-05-19 02:19:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (thisPart == 32767)
|
|
|
|
{
|
|
|
|
ret += 32767;
|
|
|
|
data += 2;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret += thisPart;
|
|
|
|
data += 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int32_t DecodeContinuousRLE(const unsigned char*& data)
|
|
|
|
{
|
|
|
|
int32_t ret = int32_t(DecodeRLE(data));
|
|
|
|
if (ret >= 16384)
|
|
|
|
return ret - 32767;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t DecodeTimeRLE(const unsigned char*& data)
|
|
|
|
{
|
|
|
|
uint32_t ret = 0;
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
uint16_t thisPart = SBig(*reinterpret_cast<const uint16_t*>(data));
|
|
|
|
if (thisPart == 0xffff)
|
|
|
|
{
|
|
|
|
ret += 65535;
|
|
|
|
data += 4;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret += thisPart;
|
|
|
|
data += 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SongState::Header::swapBig()
|
|
|
|
{
|
2016-06-20 03:35:57 +00:00
|
|
|
m_trackIdxOff = SBig(m_trackIdxOff);
|
|
|
|
m_regionIdxOff = SBig(m_regionIdxOff);
|
2016-05-19 02:19:43 +00:00
|
|
|
m_chanMapOff = SBig(m_chanMapOff);
|
|
|
|
m_tempoTableOff = SBig(m_tempoTableOff);
|
|
|
|
m_initialTempo = SBig(m_initialTempo);
|
|
|
|
m_unkOff = SBig(m_unkOff);
|
|
|
|
}
|
|
|
|
|
2016-06-27 22:43:04 +00:00
|
|
|
bool SongState::TrackRegion::indexValid(bool bigEndian) const
|
2016-05-19 02:19:43 +00:00
|
|
|
{
|
2016-06-27 22:43:04 +00:00
|
|
|
return (bigEndian ? SBig(m_regionIndex) : m_regionIndex) >= 0;
|
2016-05-19 02:19:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SongState::TempoChange::swapBig()
|
|
|
|
{
|
|
|
|
m_tick = SBig(m_tick);
|
|
|
|
m_tempo = SBig(m_tempo);
|
|
|
|
}
|
|
|
|
|
2016-06-20 08:08:32 +00:00
|
|
|
void SongState::Track::Header::swapBig()
|
2016-05-19 02:19:43 +00:00
|
|
|
{
|
|
|
|
m_type = SBig(m_type);
|
|
|
|
m_pitchOff = SBig(m_pitchOff);
|
|
|
|
m_modOff = SBig(m_modOff);
|
|
|
|
}
|
|
|
|
|
2016-06-20 08:08:32 +00:00
|
|
|
SongState::Track::Track(SongState& parent, uint8_t midiChan, const TrackRegion* regions)
|
2016-06-20 03:35:57 +00:00
|
|
|
: m_parent(parent), m_midiChan(midiChan), m_curRegion(nullptr), m_nextRegion(regions)
|
2016-06-20 08:08:32 +00:00
|
|
|
{
|
2016-07-14 04:54:46 +00:00
|
|
|
for (int i = 0; i < 128; ++i)
|
2017-06-18 02:48:52 +00:00
|
|
|
m_remNoteLengths[i] = std::numeric_limits<decltype(m_remNoteLengths)::value_type>::min();
|
2016-06-20 08:08:32 +00:00
|
|
|
}
|
2016-06-20 03:35:57 +00:00
|
|
|
|
2016-06-21 06:40:13 +00:00
|
|
|
void SongState::Track::setRegion(Sequencer* seq, const TrackRegion* region)
|
2016-05-19 02:19:43 +00:00
|
|
|
{
|
2016-06-20 03:35:57 +00:00
|
|
|
m_curRegion = region;
|
2016-07-14 04:54:46 +00:00
|
|
|
uint32_t regionIdx = (m_parent.m_bigEndian ? SBig(m_curRegion->m_regionIndex) : m_curRegion->m_regionIndex);
|
2016-06-20 03:35:57 +00:00
|
|
|
m_nextRegion = &m_curRegion[1];
|
2016-05-19 02:19:43 +00:00
|
|
|
|
2016-07-14 04:54:46 +00:00
|
|
|
m_data = m_parent.m_songData +
|
|
|
|
(m_parent.m_bigEndian ? SBig(m_parent.m_regionIdx[regionIdx]) : m_parent.m_regionIdx[regionIdx]);
|
2016-06-20 03:35:57 +00:00
|
|
|
|
|
|
|
Header header = *reinterpret_cast<const Header*>(m_data);
|
2016-06-21 06:40:13 +00:00
|
|
|
if (m_parent.m_bigEndian)
|
|
|
|
header.swapBig();
|
2016-06-20 03:35:57 +00:00
|
|
|
m_data += 12;
|
2016-05-19 02:19:43 +00:00
|
|
|
|
|
|
|
if (header.m_pitchOff)
|
2016-06-20 03:35:57 +00:00
|
|
|
m_pitchWheelData = m_parent.m_songData + header.m_pitchOff;
|
2016-05-19 02:19:43 +00:00
|
|
|
if (header.m_modOff)
|
2016-06-20 03:35:57 +00:00
|
|
|
m_modWheelData = m_parent.m_songData + header.m_modOff;
|
|
|
|
|
|
|
|
m_eventWaitCountdown = 0;
|
|
|
|
m_lastPitchTick = m_parent.m_curTick;
|
|
|
|
m_lastPitchVal = 0;
|
|
|
|
m_lastModTick = m_parent.m_curTick;
|
|
|
|
m_lastModVal = 0;
|
2016-06-21 06:40:13 +00:00
|
|
|
if (seq)
|
|
|
|
{
|
|
|
|
seq->setPitchWheel(m_midiChan, clamp(-1.f, m_lastPitchVal / 32768.f, 1.f));
|
|
|
|
seq->setCtrlValue(m_midiChan, 1, clamp(0, m_lastModVal * 128 / 16384, 127));
|
|
|
|
}
|
2016-07-02 21:50:38 +00:00
|
|
|
if (m_parent.m_sngVersion == 1)
|
2016-06-20 03:35:57 +00:00
|
|
|
m_eventWaitCountdown = int32_t(DecodeTimeRLE(m_data));
|
|
|
|
else
|
|
|
|
{
|
2016-07-14 04:54:46 +00:00
|
|
|
int32_t absTick = (m_parent.m_bigEndian ? SBig(*reinterpret_cast<const int32_t*>(m_data))
|
|
|
|
: *reinterpret_cast<const int32_t*>(m_data));
|
2016-06-20 03:35:57 +00:00
|
|
|
m_eventWaitCountdown = absTick;
|
|
|
|
m_lastN64EventTick = absTick;
|
|
|
|
m_data += 4;
|
|
|
|
}
|
|
|
|
}
|
2016-05-19 02:19:43 +00:00
|
|
|
|
2016-07-14 04:54:46 +00:00
|
|
|
void SongState::Track::advanceRegion(Sequencer* seq) { setRegion(seq, m_nextRegion); }
|
2016-05-19 02:19:43 +00:00
|
|
|
|
2016-07-02 21:50:38 +00:00
|
|
|
int SongState::DetectVersion(const unsigned char* ptr, bool& isBig)
|
|
|
|
{
|
|
|
|
isBig = ptr[0] == 0;
|
|
|
|
Header header = *reinterpret_cast<const Header*>(ptr);
|
|
|
|
if (isBig)
|
|
|
|
header.swapBig();
|
|
|
|
const uint32_t* trackIdx = reinterpret_cast<const uint32_t*>(ptr + header.m_trackIdxOff);
|
|
|
|
const uint32_t* regionIdxTable = reinterpret_cast<const uint32_t*>(ptr + header.m_regionIdxOff);
|
|
|
|
|
|
|
|
/* First determine maximum index of MIDI regions across all tracks */
|
|
|
|
uint32_t maxRegionIdx = 0;
|
2016-07-14 04:54:46 +00:00
|
|
|
for (int i = 0; i < 64; ++i)
|
2016-07-02 21:50:38 +00:00
|
|
|
{
|
|
|
|
if (trackIdx[i])
|
|
|
|
{
|
|
|
|
const TrackRegion* region = nullptr;
|
2016-07-14 04:54:46 +00:00
|
|
|
const TrackRegion* nextRegion =
|
|
|
|
reinterpret_cast<const TrackRegion*>(ptr + (isBig ? SBig(trackIdx[i]) : trackIdx[i]));
|
2016-07-02 21:50:38 +00:00
|
|
|
|
|
|
|
/* Iterate all regions */
|
|
|
|
while (nextRegion->indexValid(isBig))
|
|
|
|
{
|
|
|
|
region = nextRegion;
|
2016-07-14 04:54:46 +00:00
|
|
|
uint32_t regionIdx = (isBig ? SBig(region->m_regionIndex) : region->m_regionIndex);
|
2016-07-02 21:50:38 +00:00
|
|
|
maxRegionIdx = std::max(maxRegionIdx, regionIdx);
|
|
|
|
nextRegion = ®ion[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Perform 2 trials, first assuming revised format (more likely) */
|
2016-07-14 04:54:46 +00:00
|
|
|
int v = 1;
|
|
|
|
for (; v >= 0; --v)
|
2016-07-02 21:50:38 +00:00
|
|
|
{
|
|
|
|
bool bad = false;
|
|
|
|
|
|
|
|
/* Validate all tracks */
|
2016-07-14 04:54:46 +00:00
|
|
|
for (int i = 0; i < 64; ++i)
|
2016-07-02 21:50:38 +00:00
|
|
|
{
|
|
|
|
if (trackIdx[i])
|
|
|
|
{
|
|
|
|
const TrackRegion* region = nullptr;
|
2016-07-14 04:54:46 +00:00
|
|
|
const TrackRegion* nextRegion =
|
|
|
|
reinterpret_cast<const TrackRegion*>(ptr + (isBig ? SBig(trackIdx[i]) : trackIdx[i]));
|
2016-07-02 21:50:38 +00:00
|
|
|
|
|
|
|
/* Iterate all regions */
|
|
|
|
while (nextRegion->indexValid(isBig))
|
|
|
|
{
|
|
|
|
region = nextRegion;
|
2016-07-14 04:54:46 +00:00
|
|
|
uint32_t regionIdx = (isBig ? SBig(region->m_regionIndex) : region->m_regionIndex);
|
2016-07-02 21:50:38 +00:00
|
|
|
nextRegion = ®ion[1];
|
|
|
|
|
2016-07-14 04:54:46 +00:00
|
|
|
const unsigned char* data =
|
|
|
|
ptr + (isBig ? SBig(regionIdxTable[regionIdx]) : regionIdxTable[regionIdx]);
|
2016-07-02 21:50:38 +00:00
|
|
|
|
|
|
|
/* Can't reliably validate final region */
|
|
|
|
if (regionIdx == maxRegionIdx)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Expected end pointer (next region) */
|
2016-07-14 04:54:46 +00:00
|
|
|
const unsigned char* expectedEnd =
|
|
|
|
ptr + (isBig ? SBig(regionIdxTable[regionIdx + 1]) : regionIdxTable[regionIdx + 1]);
|
2016-07-02 21:50:38 +00:00
|
|
|
|
|
|
|
Track::Header header = *reinterpret_cast<const Track::Header*>(data);
|
|
|
|
if (isBig)
|
|
|
|
header.swapBig();
|
|
|
|
data += 12;
|
|
|
|
|
|
|
|
/* continuous pitch data */
|
|
|
|
if (header.m_pitchOff)
|
|
|
|
{
|
|
|
|
const unsigned char* dptr = ptr + header.m_pitchOff;
|
2016-07-14 04:54:46 +00:00
|
|
|
while (DecodeRLE(dptr) != 0xffffffff)
|
|
|
|
{
|
|
|
|
DecodeContinuousRLE(dptr);
|
|
|
|
}
|
2016-07-02 21:50:38 +00:00
|
|
|
if (dptr >= (expectedEnd - 4) && (dptr <= expectedEnd))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* continuous modulation data */
|
|
|
|
if (header.m_modOff)
|
|
|
|
{
|
|
|
|
const unsigned char* dptr = ptr + header.m_modOff;
|
2016-07-14 04:54:46 +00:00
|
|
|
while (DecodeRLE(dptr) != 0xffffffff)
|
|
|
|
{
|
|
|
|
DecodeContinuousRLE(dptr);
|
|
|
|
}
|
2016-07-02 21:50:38 +00:00
|
|
|
if (dptr >= (expectedEnd - 4) && (dptr <= expectedEnd))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Loop through as many commands as we can for this time period */
|
|
|
|
if (v == 1)
|
|
|
|
{
|
|
|
|
/* Revised */
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
/* Delta time */
|
|
|
|
DecodeTimeRLE(data);
|
|
|
|
|
|
|
|
/* Load next command */
|
|
|
|
if (*reinterpret_cast<const uint16_t*>(data) == 0xffff)
|
|
|
|
{
|
|
|
|
/* End of channel */
|
|
|
|
data += 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (data[0] & 0x80 && data[1] & 0x80)
|
|
|
|
{
|
|
|
|
/* Control change */
|
|
|
|
data += 2;
|
|
|
|
}
|
|
|
|
else if (data[0] & 0x80)
|
|
|
|
{
|
|
|
|
/* Program change */
|
|
|
|
data += 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Note */
|
|
|
|
data += 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Legacy */
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
/* Delta-time */
|
|
|
|
data += 4;
|
|
|
|
|
|
|
|
/* Load next command */
|
|
|
|
if (*reinterpret_cast<const uint16_t*>(&data[2]) == 0xffff)
|
|
|
|
{
|
|
|
|
/* End of channel */
|
|
|
|
data += 4;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ((data[2] & 0x80) != 0x80)
|
|
|
|
{
|
|
|
|
/* Note */
|
|
|
|
}
|
|
|
|
else if (data[2] & 0x80 && data[3] & 0x80)
|
|
|
|
{
|
|
|
|
/* Control change */
|
|
|
|
}
|
|
|
|
else if (data[2] & 0x80)
|
|
|
|
{
|
|
|
|
/* Program change */
|
|
|
|
}
|
|
|
|
data += 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data < (expectedEnd - 4) || (data > expectedEnd))
|
|
|
|
{
|
|
|
|
bad = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (bad)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (bad)
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SongState::initialize(const unsigned char* ptr)
|
2016-05-19 02:19:43 +00:00
|
|
|
{
|
2016-07-02 21:50:38 +00:00
|
|
|
m_sngVersion = DetectVersion(ptr, m_bigEndian);
|
|
|
|
if (m_sngVersion < 0)
|
|
|
|
return false;
|
|
|
|
|
2016-06-20 03:35:57 +00:00
|
|
|
m_songData = ptr;
|
2016-05-19 02:19:43 +00:00
|
|
|
m_header = *reinterpret_cast<const Header*>(ptr);
|
2016-06-21 06:40:13 +00:00
|
|
|
if (m_bigEndian)
|
|
|
|
m_header.swapBig();
|
2016-06-20 03:35:57 +00:00
|
|
|
const uint32_t* trackIdx = reinterpret_cast<const uint32_t*>(ptr + m_header.m_trackIdxOff);
|
|
|
|
m_regionIdx = reinterpret_cast<const uint32_t*>(ptr + m_header.m_regionIdxOff);
|
|
|
|
const uint8_t* chanMap = reinterpret_cast<const uint8_t*>(ptr + m_header.m_chanMapOff);
|
2016-05-19 02:19:43 +00:00
|
|
|
|
2016-06-20 08:08:32 +00:00
|
|
|
/* Initialize all tracks */
|
2016-07-14 04:54:46 +00:00
|
|
|
for (int i = 0; i < 64; ++i)
|
2016-05-19 02:19:43 +00:00
|
|
|
{
|
2016-06-20 03:35:57 +00:00
|
|
|
if (trackIdx[i])
|
2016-05-19 02:19:43 +00:00
|
|
|
{
|
2016-07-14 04:54:46 +00:00
|
|
|
const TrackRegion* region =
|
|
|
|
reinterpret_cast<const TrackRegion*>(ptr + (m_bigEndian ? SBig(trackIdx[i]) : trackIdx[i]));
|
2016-06-20 08:08:32 +00:00
|
|
|
m_tracks[i].emplace(*this, chanMap[i], region);
|
2016-05-19 02:19:43 +00:00
|
|
|
}
|
|
|
|
else
|
2016-06-20 08:08:32 +00:00
|
|
|
m_tracks[i] = std::experimental::nullopt;
|
2016-05-19 02:19:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize tempo */
|
|
|
|
if (m_header.m_tempoTableOff)
|
|
|
|
m_tempoPtr = reinterpret_cast<const TempoChange*>(ptr + m_header.m_tempoTableOff);
|
|
|
|
else
|
|
|
|
m_tempoPtr = nullptr;
|
|
|
|
|
2016-07-07 19:17:30 +00:00
|
|
|
m_tempo = m_header.m_initialTempo & 0x7fffffff;
|
2016-05-19 02:19:43 +00:00
|
|
|
m_curTick = 0;
|
|
|
|
m_songState = SongPlayState::Playing;
|
2016-07-02 21:50:38 +00:00
|
|
|
|
|
|
|
return true;
|
2016-05-19 02:19:43 +00:00
|
|
|
}
|
|
|
|
|
2016-06-20 08:08:32 +00:00
|
|
|
bool SongState::Track::advance(Sequencer& seq, int32_t ticks)
|
2016-05-19 02:19:43 +00:00
|
|
|
{
|
|
|
|
int32_t endTick = m_parent.m_curTick + ticks;
|
|
|
|
|
2016-06-20 03:35:57 +00:00
|
|
|
/* Advance region if needed */
|
2016-06-27 22:43:04 +00:00
|
|
|
while (m_nextRegion->indexValid(m_parent.m_bigEndian))
|
2016-06-20 03:35:57 +00:00
|
|
|
{
|
2016-07-14 04:54:46 +00:00
|
|
|
uint32_t nextRegTick = (m_parent.m_bigEndian ? SBig(m_nextRegion->m_startTick) : m_nextRegion->m_startTick);
|
2016-12-11 01:52:42 +00:00
|
|
|
if (uint32_t(endTick) > nextRegTick)
|
2016-06-21 06:40:13 +00:00
|
|
|
advanceRegion(&seq);
|
2016-06-20 03:35:57 +00:00
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-06-20 08:08:32 +00:00
|
|
|
/* Stop finished notes */
|
2016-07-14 04:54:46 +00:00
|
|
|
for (int i = 0; i < 128; ++i)
|
2016-06-20 08:08:32 +00:00
|
|
|
{
|
2017-06-18 02:48:52 +00:00
|
|
|
constexpr decltype(m_remNoteLengths)::value_type MIN = std::numeric_limits<decltype(MIN)>::min();
|
|
|
|
if (m_remNoteLengths[i] != MIN)
|
2016-06-20 08:08:32 +00:00
|
|
|
{
|
|
|
|
m_remNoteLengths[i] -= ticks;
|
|
|
|
if (m_remNoteLengths[i] <= 0)
|
|
|
|
{
|
|
|
|
seq.keyOff(m_midiChan, i, 0);
|
2017-06-18 02:48:52 +00:00
|
|
|
m_remNoteLengths[i] = MIN;
|
2016-06-20 08:08:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-20 03:35:57 +00:00
|
|
|
if (!m_data)
|
2016-06-27 22:43:04 +00:00
|
|
|
return !m_nextRegion->indexValid(m_parent.m_bigEndian);
|
2016-06-20 03:35:57 +00:00
|
|
|
|
2016-05-19 02:19:43 +00:00
|
|
|
/* Update continuous pitch data */
|
|
|
|
if (m_pitchWheelData)
|
|
|
|
{
|
|
|
|
int32_t pitchTick = m_parent.m_curTick;
|
|
|
|
int32_t remPitchTicks = ticks;
|
|
|
|
while (pitchTick < endTick)
|
|
|
|
{
|
|
|
|
/* See if there's an upcoming pitch change in this interval */
|
|
|
|
const unsigned char* ptr = m_pitchWheelData;
|
|
|
|
uint32_t deltaTicks = DecodeRLE(ptr);
|
2016-05-21 23:41:45 +00:00
|
|
|
if (deltaTicks != 0xffffffff)
|
2016-05-19 02:19:43 +00:00
|
|
|
{
|
2016-05-21 23:41:45 +00:00
|
|
|
int32_t nextTick = m_lastPitchTick + deltaTicks;
|
2016-05-19 02:19:43 +00:00
|
|
|
if (pitchTick + remPitchTicks > nextTick)
|
|
|
|
{
|
|
|
|
/* Update pitch */
|
|
|
|
int32_t pitchDelta = DecodeContinuousRLE(ptr);
|
|
|
|
m_lastPitchVal += pitchDelta;
|
|
|
|
m_pitchWheelData = ptr;
|
|
|
|
m_lastPitchTick = nextTick;
|
|
|
|
remPitchTicks -= (nextTick - pitchTick);
|
|
|
|
pitchTick = nextTick;
|
2016-05-19 10:12:32 +00:00
|
|
|
seq.setPitchWheel(m_midiChan, clamp(-1.f, m_lastPitchVal / 32768.f, 1.f));
|
2016-05-19 02:19:43 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
remPitchTicks -= (nextTick - pitchTick);
|
|
|
|
pitchTick = nextTick;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Update continuous modulation data */
|
|
|
|
if (m_modWheelData)
|
|
|
|
{
|
|
|
|
int32_t modTick = m_parent.m_curTick;
|
|
|
|
int32_t remModTicks = ticks;
|
|
|
|
while (modTick < endTick)
|
|
|
|
{
|
|
|
|
/* See if there's an upcoming modulation change in this interval */
|
|
|
|
const unsigned char* ptr = m_modWheelData;
|
|
|
|
uint32_t deltaTicks = DecodeRLE(ptr);
|
2016-05-21 23:41:45 +00:00
|
|
|
if (deltaTicks != 0xffffffff)
|
2016-05-19 02:19:43 +00:00
|
|
|
{
|
2016-05-21 23:41:45 +00:00
|
|
|
int32_t nextTick = m_lastModTick + deltaTicks;
|
2016-05-19 02:19:43 +00:00
|
|
|
if (modTick + remModTicks > nextTick)
|
|
|
|
{
|
|
|
|
/* Update modulation */
|
|
|
|
int32_t modDelta = DecodeContinuousRLE(ptr);
|
|
|
|
m_lastModVal += modDelta;
|
|
|
|
m_modWheelData = ptr;
|
|
|
|
m_lastModTick = nextTick;
|
|
|
|
remModTicks -= (nextTick - modTick);
|
|
|
|
modTick = nextTick;
|
2016-06-20 03:35:57 +00:00
|
|
|
seq.setCtrlValue(m_midiChan, 1, clamp(0, m_lastModVal * 128 / 16384, 127));
|
2016-05-19 02:19:43 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
remModTicks -= (nextTick - modTick);
|
|
|
|
modTick = nextTick;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Loop through as many commands as we can for this time period */
|
2016-07-02 21:50:38 +00:00
|
|
|
if (m_parent.m_sngVersion == 1)
|
2016-05-19 02:19:43 +00:00
|
|
|
{
|
2016-07-02 21:50:38 +00:00
|
|
|
/* Revision */
|
2016-06-20 03:35:57 +00:00
|
|
|
while (true)
|
2016-05-19 02:19:43 +00:00
|
|
|
{
|
2016-06-20 03:35:57 +00:00
|
|
|
/* Advance wait timer if active, returning if waiting */
|
|
|
|
if (m_eventWaitCountdown)
|
|
|
|
{
|
|
|
|
m_eventWaitCountdown -= ticks;
|
|
|
|
ticks = 0;
|
|
|
|
if (m_eventWaitCountdown > 0)
|
|
|
|
return false;
|
|
|
|
}
|
2016-05-19 02:19:43 +00:00
|
|
|
|
2016-06-20 03:35:57 +00:00
|
|
|
/* Load next command */
|
|
|
|
if (*reinterpret_cast<const uint16_t*>(m_data) == 0xffff)
|
|
|
|
{
|
|
|
|
/* End of channel */
|
|
|
|
m_data = nullptr;
|
2016-06-27 22:43:04 +00:00
|
|
|
return !m_nextRegion->indexValid(m_parent.m_bigEndian);
|
2016-06-20 03:35:57 +00:00
|
|
|
}
|
2016-06-22 04:18:28 +00:00
|
|
|
else if (m_data[0] & 0x80 && m_data[1] & 0x80)
|
2016-06-20 03:35:57 +00:00
|
|
|
{
|
|
|
|
/* Control change */
|
|
|
|
uint8_t val = m_data[0] & 0x7f;
|
|
|
|
uint8_t ctrl = m_data[1] & 0x7f;
|
|
|
|
seq.setCtrlValue(m_midiChan, ctrl, val);
|
|
|
|
m_data += 2;
|
|
|
|
}
|
2016-06-22 04:18:28 +00:00
|
|
|
else if (m_data[0] & 0x80)
|
|
|
|
{
|
|
|
|
/* Program change */
|
|
|
|
uint8_t prog = m_data[0] & 0x7f;
|
|
|
|
seq.setChanProgram(m_midiChan, prog);
|
|
|
|
m_data += 2;
|
|
|
|
}
|
2016-06-20 03:35:57 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Note */
|
|
|
|
uint8_t note = m_data[0] & 0x7f;
|
|
|
|
uint8_t vel = m_data[1] & 0x7f;
|
2016-07-14 04:54:46 +00:00
|
|
|
uint16_t length = (m_parent.m_bigEndian ? SBig(*reinterpret_cast<const uint16_t*>(m_data + 2))
|
|
|
|
: *reinterpret_cast<const uint16_t*>(m_data + 2));
|
2016-06-20 03:35:57 +00:00
|
|
|
seq.keyOn(m_midiChan, note, vel);
|
|
|
|
m_remNoteLengths[note] = length;
|
|
|
|
m_data += 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set next delta-time */
|
|
|
|
m_eventWaitCountdown += int32_t(DecodeTimeRLE(m_data));
|
2016-05-19 02:19:43 +00:00
|
|
|
}
|
2016-06-20 03:35:57 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-07-02 21:50:38 +00:00
|
|
|
/* Legacy */
|
2016-06-20 03:35:57 +00:00
|
|
|
while (true)
|
2016-05-19 02:19:43 +00:00
|
|
|
{
|
2016-06-20 03:35:57 +00:00
|
|
|
/* Advance wait timer if active, returning if waiting */
|
|
|
|
if (m_eventWaitCountdown)
|
|
|
|
{
|
|
|
|
m_eventWaitCountdown -= ticks;
|
|
|
|
ticks = 0;
|
|
|
|
if (m_eventWaitCountdown > 0)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Load next command */
|
2016-06-27 22:43:04 +00:00
|
|
|
if (*reinterpret_cast<const uint16_t*>(&m_data[2]) == 0xffff)
|
2016-06-20 03:35:57 +00:00
|
|
|
{
|
|
|
|
/* End of channel */
|
|
|
|
m_data = nullptr;
|
2016-06-27 22:43:04 +00:00
|
|
|
return !m_nextRegion->indexValid(m_parent.m_bigEndian);
|
2016-06-20 03:35:57 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ((m_data[2] & 0x80) != 0x80)
|
|
|
|
{
|
|
|
|
/* Note */
|
2016-07-14 04:54:46 +00:00
|
|
|
uint16_t length = (m_parent.m_bigEndian ? SBig(*reinterpret_cast<const uint16_t*>(m_data))
|
|
|
|
: *reinterpret_cast<const uint16_t*>(m_data));
|
2016-06-20 03:35:57 +00:00
|
|
|
uint8_t note = m_data[2] & 0x7f;
|
|
|
|
uint8_t vel = m_data[3] & 0x7f;
|
|
|
|
seq.keyOn(m_midiChan, note, vel);
|
|
|
|
m_remNoteLengths[note] = length;
|
|
|
|
}
|
2016-06-23 00:28:02 +00:00
|
|
|
else if (m_data[2] & 0x80 && m_data[3] & 0x80)
|
|
|
|
{
|
|
|
|
/* Control change */
|
|
|
|
uint8_t val = m_data[2] & 0x7f;
|
|
|
|
uint8_t ctrl = m_data[3] & 0x7f;
|
|
|
|
seq.setCtrlValue(m_midiChan, ctrl, val);
|
|
|
|
}
|
|
|
|
else if (m_data[2] & 0x80)
|
|
|
|
{
|
|
|
|
/* Program change */
|
|
|
|
uint8_t prog = m_data[2] & 0x7f;
|
|
|
|
seq.setChanProgram(m_midiChan, prog);
|
|
|
|
}
|
2016-06-20 03:35:57 +00:00
|
|
|
m_data += 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set next delta-time */
|
2016-07-14 04:54:46 +00:00
|
|
|
int32_t absTick = (m_parent.m_bigEndian ? SBig(*reinterpret_cast<const int32_t*>(m_data))
|
|
|
|
: *reinterpret_cast<const int32_t*>(m_data));
|
2016-06-20 03:35:57 +00:00
|
|
|
m_eventWaitCountdown += absTick - m_lastN64EventTick;
|
|
|
|
m_lastN64EventTick = absTick;
|
2016-05-19 02:19:43 +00:00
|
|
|
m_data += 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SongState::advance(Sequencer& seq, double dt)
|
|
|
|
{
|
|
|
|
/* Stopped */
|
|
|
|
if (m_songState == SongPlayState::Stopped)
|
|
|
|
return true;
|
|
|
|
|
2016-05-19 05:27:39 +00:00
|
|
|
bool done = false;
|
|
|
|
m_curDt += dt;
|
|
|
|
while (m_curDt > 0.0)
|
2016-05-19 02:19:43 +00:00
|
|
|
{
|
2016-05-19 05:27:39 +00:00
|
|
|
done = true;
|
|
|
|
|
2016-05-19 02:19:43 +00:00
|
|
|
/* Compute ticks to compute based on current tempo */
|
2016-05-19 05:27:39 +00:00
|
|
|
double ticksPerSecond = m_tempo * 384 / 60;
|
|
|
|
int32_t remTicks = std::ceil(m_curDt * ticksPerSecond);
|
|
|
|
if (!remTicks)
|
|
|
|
break;
|
2016-05-19 02:19:43 +00:00
|
|
|
|
|
|
|
/* See if there's an upcoming tempo change in this interval */
|
|
|
|
if (m_tempoPtr && m_tempoPtr->m_tick != 0xffffffff)
|
|
|
|
{
|
|
|
|
TempoChange change = *m_tempoPtr;
|
2016-06-21 06:40:13 +00:00
|
|
|
if (m_bigEndian)
|
|
|
|
change.swapBig();
|
2016-05-19 02:19:43 +00:00
|
|
|
|
|
|
|
if (m_curTick + remTicks > change.m_tick)
|
|
|
|
remTicks = change.m_tick - m_curTick;
|
|
|
|
|
|
|
|
if (remTicks <= 0)
|
|
|
|
{
|
|
|
|
/* Turn over tempo */
|
2016-07-07 19:17:30 +00:00
|
|
|
m_tempo = change.m_tempo & 0x7fffffff;
|
2016-06-01 21:32:48 +00:00
|
|
|
seq.setTempo(m_tempo * 384 / 60);
|
2016-05-19 02:19:43 +00:00
|
|
|
++m_tempoPtr;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-20 08:08:32 +00:00
|
|
|
/* Advance all tracks */
|
|
|
|
for (std::experimental::optional<Track>& trk : m_tracks)
|
|
|
|
if (trk)
|
|
|
|
done &= trk->advance(seq, remTicks);
|
2016-05-19 02:19:43 +00:00
|
|
|
|
|
|
|
m_curTick += remTicks;
|
|
|
|
|
|
|
|
if (m_tempo == 0)
|
2016-05-19 05:27:39 +00:00
|
|
|
m_curDt = 0.0;
|
2016-05-19 02:19:43 +00:00
|
|
|
else
|
2016-05-19 05:27:39 +00:00
|
|
|
m_curDt -= remTicks / ticksPerSecond;
|
2016-05-19 02:19:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (done)
|
|
|
|
m_songState = SongPlayState::Stopped;
|
|
|
|
return done;
|
|
|
|
}
|
|
|
|
}
|