2016-05-03 01:16:26 +00:00
|
|
|
#include "amuse/Voice.hpp"
|
2016-05-07 22:10:57 +00:00
|
|
|
#include "amuse/Submix.hpp"
|
2016-05-03 01:16:26 +00:00
|
|
|
#include "amuse/IBackendVoice.hpp"
|
2016-05-11 21:30:45 +00:00
|
|
|
#include "amuse/IBackendVoiceAllocator.hpp"
|
2016-05-11 04:48:08 +00:00
|
|
|
#include "amuse/AudioGroup.hpp"
|
2016-05-11 21:30:45 +00:00
|
|
|
#include "amuse/Common.hpp"
|
|
|
|
#include "amuse/Engine.hpp"
|
2016-05-28 02:28:59 +00:00
|
|
|
#include "amuse/DSPCodec.h"
|
|
|
|
#include "amuse/N64MusyXCodec.h"
|
2016-05-11 21:30:45 +00:00
|
|
|
#include <cmath>
|
2016-05-11 04:48:08 +00:00
|
|
|
#include <string.h>
|
2016-05-03 01:16:26 +00:00
|
|
|
|
|
|
|
namespace amuse
|
|
|
|
{
|
2016-05-22 08:35:55 +00:00
|
|
|
extern "C" const float VolumeLUT[];
|
2016-05-03 01:16:26 +00:00
|
|
|
|
2016-05-06 21:22:39 +00:00
|
|
|
void Voice::_destroy()
|
|
|
|
{
|
|
|
|
Entity::_destroy();
|
2016-05-14 06:33:21 +00:00
|
|
|
|
|
|
|
for (std::shared_ptr<Voice>& vox : m_childVoices)
|
|
|
|
vox->_destroy();
|
2016-05-07 22:10:57 +00:00
|
|
|
}
|
|
|
|
|
2016-05-15 21:56:23 +00:00
|
|
|
Voice::~Voice()
|
|
|
|
{
|
2016-05-16 20:20:29 +00:00
|
|
|
//fprintf(stderr, "DEALLOC %d\n", m_vid);
|
2016-05-15 21:56:23 +00:00
|
|
|
}
|
2016-05-14 22:38:37 +00:00
|
|
|
|
2016-05-15 21:56:23 +00:00
|
|
|
Voice::Voice(Engine& engine, const AudioGroup& group, int groupId, int vid, bool emitter, Submix* smx)
|
|
|
|
: Entity(engine, group, groupId), m_vid(vid), m_emitter(emitter), m_submix(smx)
|
2016-05-07 22:10:57 +00:00
|
|
|
{
|
2016-05-16 20:20:29 +00:00
|
|
|
//fprintf(stderr, "ALLOC %d\n", m_vid);
|
2016-05-07 22:10:57 +00:00
|
|
|
}
|
|
|
|
|
2016-05-15 21:56:23 +00:00
|
|
|
Voice::Voice(Engine& engine, const AudioGroup& group, int groupId, ObjectId oid, int vid, bool emitter, Submix* smx)
|
|
|
|
: Entity(engine, group, groupId, oid), m_vid(vid), m_emitter(emitter), m_submix(smx)
|
2016-05-07 22:10:57 +00:00
|
|
|
{
|
2016-05-16 20:20:29 +00:00
|
|
|
//fprintf(stderr, "ALLOC %d\n", m_vid);
|
2016-05-06 21:22:39 +00:00
|
|
|
}
|
|
|
|
|
2016-05-16 02:40:18 +00:00
|
|
|
void Voice::_macroSampleEnd()
|
|
|
|
{
|
|
|
|
if (m_sampleEndTrap.macroId != 0xffff)
|
|
|
|
{
|
|
|
|
if (m_sampleEndTrap.macroId == m_state.m_header.m_macroId)
|
|
|
|
{
|
|
|
|
m_state.m_pc.back().second = m_sampleEndTrap.macroStep;
|
|
|
|
m_state.m_inWait = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
loadSoundObject(m_sampleEndTrap.macroId, m_sampleEndTrap.macroStep,
|
|
|
|
m_state.m_ticksPerSec, m_state.m_initKey,
|
|
|
|
m_state.m_initVel, m_state.m_initMod);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
m_state.sampleEndNotify(*this);
|
|
|
|
}
|
|
|
|
|
2016-05-28 02:28:59 +00:00
|
|
|
bool Voice::_checkSamplePos(bool& looped)
|
2016-05-03 01:16:26 +00:00
|
|
|
{
|
2016-05-28 02:28:59 +00:00
|
|
|
looped = false;
|
2016-05-16 20:20:29 +00:00
|
|
|
if (!m_curSample)
|
|
|
|
return true;
|
|
|
|
|
2016-05-11 04:48:08 +00:00
|
|
|
if (m_curSamplePos >= m_lastSamplePos)
|
|
|
|
{
|
|
|
|
if (m_curSample->first.m_loopLengthSamples)
|
|
|
|
{
|
|
|
|
/* Turn over looped sample */
|
|
|
|
m_curSamplePos = m_curSample->first.m_loopStartSample;
|
2016-05-29 04:31:58 +00:00
|
|
|
if (m_curFormat == SampleFormat::DSP)
|
|
|
|
{
|
|
|
|
m_prev1 = m_curSample->second.dsp.m_hist1;
|
|
|
|
m_prev2 = m_curSample->second.dsp.m_hist2;
|
|
|
|
}
|
2016-05-28 02:28:59 +00:00
|
|
|
looped = true;
|
2016-05-11 04:48:08 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Notify sample end */
|
2016-05-16 02:40:18 +00:00
|
|
|
_macroSampleEnd();
|
2016-05-11 04:48:08 +00:00
|
|
|
m_curSample = nullptr;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2016-05-15 21:56:23 +00:00
|
|
|
|
|
|
|
/* Looped samples issue sample end when ADSR envelope complete */
|
|
|
|
if (m_volAdsr.isComplete())
|
|
|
|
{
|
2016-05-16 02:40:18 +00:00
|
|
|
_macroSampleEnd();
|
2016-05-15 21:56:23 +00:00
|
|
|
m_curSample = nullptr;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-11 04:48:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Voice::_doKeyOff()
|
|
|
|
{
|
2016-06-02 02:28:48 +00:00
|
|
|
if (m_state.m_inWait && m_state.m_keyoffWait)
|
|
|
|
{
|
|
|
|
if (m_volAdsr.isAdsrSet())
|
|
|
|
m_volAdsr.keyOff();
|
|
|
|
if (m_pitchAdsr.isAdsrSet())
|
|
|
|
m_pitchAdsr.keyOff();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_volAdsr.keyOff();
|
|
|
|
m_pitchAdsr.keyOff();
|
|
|
|
}
|
2016-05-14 22:38:37 +00:00
|
|
|
m_state.keyoffNotify(*this);
|
2016-05-11 21:30:45 +00:00
|
|
|
}
|
|
|
|
|
2016-05-17 03:47:12 +00:00
|
|
|
void Voice::_setTotalPitch(int32_t cents, bool slew)
|
2016-05-11 21:30:45 +00:00
|
|
|
{
|
2016-05-16 20:20:29 +00:00
|
|
|
//fprintf(stderr, "PITCH %d\n", cents);
|
2016-05-11 21:30:45 +00:00
|
|
|
int32_t interval = cents - m_curSample->first.m_pitch * 100;
|
2016-05-11 22:39:24 +00:00
|
|
|
double ratio = std::exp2(interval / 1200.0);
|
2016-05-11 21:30:45 +00:00
|
|
|
m_sampleRate = m_curSample->first.m_sampleRate * ratio;
|
2016-05-17 03:47:12 +00:00
|
|
|
m_backendVoice->setPitchRatio(ratio, slew);
|
2016-05-11 21:30:45 +00:00
|
|
|
}
|
|
|
|
|
2016-05-15 06:48:26 +00:00
|
|
|
bool Voice::_isRecursivelyDead()
|
|
|
|
{
|
|
|
|
if (m_voxState != VoiceState::Dead)
|
|
|
|
return false;
|
|
|
|
for (std::shared_ptr<Voice>& vox : m_childVoices)
|
|
|
|
if (!vox->_isRecursivelyDead())
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-14 04:46:39 +00:00
|
|
|
void Voice::_bringOutYourDead()
|
2016-05-11 21:30:45 +00:00
|
|
|
{
|
2016-05-14 04:46:39 +00:00
|
|
|
for (auto it = m_childVoices.begin() ; it != m_childVoices.end() ;)
|
|
|
|
{
|
|
|
|
Voice* vox = it->get();
|
|
|
|
vox->_bringOutYourDead();
|
2016-05-15 06:48:26 +00:00
|
|
|
if (vox->_isRecursivelyDead())
|
2016-05-14 04:46:39 +00:00
|
|
|
{
|
2016-05-31 20:40:51 +00:00
|
|
|
it = _destroyVoice(it);
|
2016-05-14 04:46:39 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
++it;
|
|
|
|
}
|
2016-05-11 21:30:45 +00:00
|
|
|
}
|
|
|
|
|
2016-05-14 06:33:21 +00:00
|
|
|
std::shared_ptr<Voice> Voice::_findVoice(int vid, std::weak_ptr<Voice> thisPtr)
|
|
|
|
{
|
|
|
|
if (m_vid == vid)
|
|
|
|
return thisPtr.lock();
|
|
|
|
|
|
|
|
for (std::shared_ptr<Voice>& vox : m_childVoices)
|
|
|
|
{
|
|
|
|
std::shared_ptr<Voice> ret = vox->_findVoice(vid, vox);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2016-05-15 21:56:23 +00:00
|
|
|
std::unique_ptr<int8_t[]>& Voice::_ensureCtrlVals()
|
|
|
|
{
|
|
|
|
if (m_ctrlValsSelf)
|
|
|
|
return m_ctrlValsSelf;
|
|
|
|
m_ctrlValsSelf.reset(new int8_t[128]);
|
|
|
|
memset(m_ctrlValsSelf.get(), 0, 128);
|
|
|
|
return m_ctrlValsSelf;
|
|
|
|
}
|
|
|
|
|
2016-05-31 20:40:51 +00:00
|
|
|
std::list<std::shared_ptr<Voice>>::iterator Voice::_allocateVoice(double sampleRate, bool dynamicPitch)
|
2016-05-14 04:46:39 +00:00
|
|
|
{
|
|
|
|
auto it = m_childVoices.emplace(m_childVoices.end(), new Voice(m_engine, m_audioGroup,
|
2016-05-15 21:56:23 +00:00
|
|
|
m_groupId, m_engine.m_nextVid++, m_emitter, m_submix));
|
2016-05-31 05:15:20 +00:00
|
|
|
if (m_submix)
|
|
|
|
m_childVoices.back()->m_backendVoice =
|
|
|
|
m_submix->m_backendSubmix->allocateVoice(*m_childVoices.back(), sampleRate, dynamicPitch);
|
|
|
|
else
|
|
|
|
m_childVoices.back()->m_backendVoice =
|
|
|
|
m_engine.getBackend().allocateVoice(*m_childVoices.back(), sampleRate, dynamicPitch);
|
2016-05-31 20:40:51 +00:00
|
|
|
return it;
|
2016-05-14 04:46:39 +00:00
|
|
|
}
|
|
|
|
|
2016-05-31 20:40:51 +00:00
|
|
|
std::list<std::shared_ptr<Voice>>::iterator Voice::_destroyVoice(std::list<std::shared_ptr<Voice>>::iterator it)
|
2016-05-11 21:30:45 +00:00
|
|
|
{
|
2016-05-31 20:40:51 +00:00
|
|
|
if ((*it)->m_destroyed)
|
2016-05-16 22:13:24 +00:00
|
|
|
return m_childVoices.begin();
|
|
|
|
|
2016-05-31 20:40:51 +00:00
|
|
|
(*it)->_destroy();
|
|
|
|
return m_childVoices.erase(it);
|
2016-05-11 21:30:45 +00:00
|
|
|
}
|
|
|
|
|
2016-05-21 01:15:31 +00:00
|
|
|
static void ApplyVolume(float vol, int16_t& samp)
|
|
|
|
{
|
|
|
|
/* -10dB to 0dB mapped to full volume range */
|
2016-05-22 08:35:55 +00:00
|
|
|
samp *= VolumeLUT[int(vol * 65536)];
|
2016-05-21 01:15:31 +00:00
|
|
|
}
|
|
|
|
|
2016-05-16 20:20:29 +00:00
|
|
|
bool Voice::_advanceSample(int16_t& samp, int32_t& newPitch)
|
2016-05-11 21:30:45 +00:00
|
|
|
{
|
2016-05-22 08:35:55 +00:00
|
|
|
double dt;
|
|
|
|
|
|
|
|
/* Block linearized will use a larger `dt` for amplitude sampling;
|
|
|
|
* significantly reducing the processing expense */
|
|
|
|
switch (m_engine.m_ampMode)
|
|
|
|
{
|
|
|
|
case AmplitudeMode::PerSample:
|
|
|
|
m_voiceSamples += 1;
|
|
|
|
dt = 1.0 / m_sampleRate;
|
|
|
|
break;
|
|
|
|
case AmplitudeMode::BlockLinearized:
|
|
|
|
{
|
|
|
|
uint32_t rem = m_voiceSamples % 160;
|
|
|
|
m_voiceSamples += 1;
|
|
|
|
if (rem != 0)
|
|
|
|
{
|
|
|
|
/* Lerp within 160-sample block */
|
|
|
|
float t = rem / 160.f;
|
|
|
|
float l = m_lastLevel * (1.f - t) + m_nextLevel * t;
|
|
|
|
|
|
|
|
/* Apply total volume to sample using decibel scale */
|
|
|
|
ApplyVolume(l, samp);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
dt = 160.0 / m_sampleRate;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-11 21:30:45 +00:00
|
|
|
m_voiceTime += dt;
|
|
|
|
bool refresh = false;
|
|
|
|
|
|
|
|
/* Process active envelope */
|
|
|
|
if (m_envelopeTime >= 0.0)
|
|
|
|
{
|
|
|
|
m_envelopeTime += dt;
|
2016-05-31 10:16:52 +00:00
|
|
|
float start = m_envelopeStart;
|
|
|
|
float end = m_envelopeEnd;
|
|
|
|
float t = std::max(0.f, std::min(1.f, float(m_envelopeTime / m_envelopeDur)));
|
2016-05-11 21:30:45 +00:00
|
|
|
if (m_envelopeCurve)
|
|
|
|
t = (*m_envelopeCurve)[int(t*127.f)] / 127.f;
|
2016-05-31 10:16:52 +00:00
|
|
|
m_curVol = clamp(0.f, (start * (1.0f - t)) + (end * t), 1.f);
|
2016-06-02 02:28:48 +00:00
|
|
|
|
|
|
|
//printf("%d %f\n", m_vid, m_curVol);
|
2016-05-11 21:30:45 +00:00
|
|
|
|
|
|
|
/* Done with envelope */
|
|
|
|
if (m_envelopeTime > m_envelopeDur)
|
|
|
|
m_envelopeTime = -1.f;
|
|
|
|
}
|
|
|
|
|
2016-06-02 02:28:48 +00:00
|
|
|
/* Dynamically evaluate per-sample SoundMacro parameters */
|
|
|
|
float evalVol = m_state.m_volumeSel ? ((m_state.m_volumeSel.evaluate(*this, m_state) / 127.f) * m_curVol) : m_curVol;
|
|
|
|
|
|
|
|
bool panDirty = false;
|
|
|
|
if (m_state.m_panSel)
|
|
|
|
{
|
|
|
|
float evalPan = (m_state.m_panSel.evaluate(*this, m_state) - 64.f) / 64.f;
|
|
|
|
if (evalPan != m_curPan)
|
|
|
|
{
|
|
|
|
m_curPan = evalPan;
|
|
|
|
panDirty = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (m_state.m_spanSel)
|
|
|
|
{
|
|
|
|
float evalSpan = (m_state.m_spanSel.evaluate(*this, m_state) - 64.f) / 64.f;
|
|
|
|
if (evalSpan != m_curSpan)
|
|
|
|
{
|
|
|
|
m_curSpan = evalSpan;
|
|
|
|
panDirty = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (m_state.m_reverbSel)
|
|
|
|
{
|
|
|
|
float evalRev = m_state.m_reverbSel.evaluate(*this, m_state) / 127.f;
|
|
|
|
if (evalRev != m_curReverbVol)
|
|
|
|
{
|
|
|
|
m_curReverbVol = evalRev;
|
|
|
|
panDirty = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (panDirty)
|
|
|
|
_setPan(m_curPan);
|
|
|
|
|
|
|
|
if (m_state.m_pitchWheelSel)
|
|
|
|
setPitchWheel(m_state.m_pitchWheelSel.evaluate(*this, m_state) / 127.f);
|
|
|
|
|
2016-05-11 21:30:45 +00:00
|
|
|
/* Factor in ADSR envelope state */
|
2016-05-22 08:35:55 +00:00
|
|
|
float adsr = m_volAdsr.advance(dt);
|
|
|
|
m_lastLevel = m_nextLevel;
|
2016-06-02 02:28:48 +00:00
|
|
|
m_nextLevel = m_userVol * evalVol * adsr * (m_state.m_curVel / 127.f);
|
2016-05-11 21:30:45 +00:00
|
|
|
|
|
|
|
/* Apply tremolo */
|
|
|
|
if (m_state.m_tremoloSel && (m_tremoloScale || m_tremoloModScale))
|
|
|
|
{
|
|
|
|
float t = m_state.m_tremoloSel.evaluate(*this, m_state);
|
|
|
|
if (m_tremoloScale && m_tremoloModScale)
|
|
|
|
{
|
|
|
|
float fac = (1.0f - t) + (m_tremoloScale * t);
|
|
|
|
float modT = getModWheel() / 127.f;
|
|
|
|
float modFac = (1.0f - modT) + (m_tremoloModScale * modT);
|
2016-05-22 08:35:55 +00:00
|
|
|
m_nextLevel *= fac * modFac;
|
2016-05-11 21:30:45 +00:00
|
|
|
}
|
|
|
|
else if (m_tremoloScale)
|
|
|
|
{
|
|
|
|
float fac = (1.0f - t) + (m_tremoloScale * t);
|
2016-05-22 08:35:55 +00:00
|
|
|
m_nextLevel *= fac;
|
2016-05-11 21:30:45 +00:00
|
|
|
}
|
|
|
|
else if (m_tremoloModScale)
|
|
|
|
{
|
|
|
|
float modT = getModWheel() / 127.f;
|
|
|
|
float modFac = (1.0f - modT) + (m_tremoloModScale * modT);
|
2016-05-22 08:35:55 +00:00
|
|
|
m_nextLevel *= modFac;
|
2016-05-11 21:30:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-22 08:35:55 +00:00
|
|
|
m_nextLevel = ClampFull<float>(m_nextLevel);
|
|
|
|
|
|
|
|
/* Apply total volume to sample using decibel scale */
|
|
|
|
ApplyVolume(m_nextLevel, samp);
|
2016-05-11 21:30:45 +00:00
|
|
|
|
|
|
|
/* Process active pan-sweep */
|
|
|
|
if (m_panningTime >= 0.f)
|
|
|
|
{
|
|
|
|
m_panningTime += dt;
|
|
|
|
float start = (m_panPos - 64) / 64.f;
|
|
|
|
float end = (m_panPos + m_panWidth - 64) / 64.f;
|
|
|
|
float t = std::max(0.f, std::min(1.f, m_panningTime / m_panningDur));
|
2016-05-21 21:40:03 +00:00
|
|
|
_setPan((start * (1.0f - t)) + (end * t));
|
2016-05-11 21:30:45 +00:00
|
|
|
refresh = true;
|
|
|
|
|
|
|
|
/* Done with panning */
|
|
|
|
if (m_panningTime > m_panningDur)
|
|
|
|
m_panningTime = -1.f;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Process active span-sweep */
|
|
|
|
if (m_spanningTime >= 0.f)
|
|
|
|
{
|
|
|
|
m_spanningTime += dt;
|
|
|
|
float start = (m_spanPos - 64) / 64.f;
|
|
|
|
float end = (m_spanPos + m_spanWidth - 64) / 64.f;
|
|
|
|
float t = std::max(0.f, std::min(1.f, m_spanningTime / m_spanningDur));
|
2016-05-21 21:40:03 +00:00
|
|
|
_setSurroundPan((start * (1.0f - t)) + (end * t));
|
2016-05-11 21:30:45 +00:00
|
|
|
refresh = true;
|
|
|
|
|
|
|
|
/* Done with spanning */
|
|
|
|
if (m_spanningTime > m_spanningDur)
|
|
|
|
m_spanningTime = -1.f;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Calculate total pitch */
|
2016-05-16 20:20:29 +00:00
|
|
|
newPitch = m_curPitch;
|
|
|
|
refresh |= m_pitchDirty;
|
2016-05-11 21:30:45 +00:00
|
|
|
m_pitchDirty = false;
|
2016-06-01 21:17:16 +00:00
|
|
|
if (m_portamentoTime >= 0.f)
|
|
|
|
{
|
|
|
|
m_portamentoTime += dt;
|
|
|
|
float t = std::max(0.f, std::min(1.f, m_portamentoTime / m_state.m_portamentoTime));
|
|
|
|
|
|
|
|
newPitch = (m_curPitch * (1.0f - t)) + (m_portamentoTarget * t);
|
|
|
|
refresh = true;
|
|
|
|
|
|
|
|
/* Done with portamento */
|
|
|
|
if (m_portamentoTime > m_state.m_portamentoTime)
|
|
|
|
{
|
|
|
|
m_portamentoTime = -1.f;
|
|
|
|
m_curPitch = m_portamentoTarget;
|
|
|
|
}
|
|
|
|
}
|
2016-05-11 21:30:45 +00:00
|
|
|
if (m_pitchEnv)
|
|
|
|
{
|
2016-06-01 21:17:16 +00:00
|
|
|
newPitch *= m_pitchAdsr.advance(dt);
|
2016-05-16 20:20:29 +00:00
|
|
|
refresh = true;
|
2016-05-11 21:30:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Process pitch sweep 1 */
|
2016-05-17 03:23:35 +00:00
|
|
|
if (m_pitchSweep1It < m_pitchSweep1Times)
|
2016-05-11 21:30:45 +00:00
|
|
|
{
|
2016-05-17 03:23:35 +00:00
|
|
|
++m_pitchSweep1It;
|
|
|
|
m_pitchSweep1 = m_pitchSweep1Add * m_pitchSweep1It / m_pitchSweep1Times;
|
2016-05-16 20:20:29 +00:00
|
|
|
refresh = true;
|
2016-05-11 21:30:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Process pitch sweep 2 */
|
2016-05-17 03:23:35 +00:00
|
|
|
if (m_pitchSweep2It < m_pitchSweep2Times)
|
2016-05-11 21:30:45 +00:00
|
|
|
{
|
2016-05-17 03:23:35 +00:00
|
|
|
++m_pitchSweep2It;
|
|
|
|
m_pitchSweep2 = m_pitchSweep2Add * m_pitchSweep2It / m_pitchSweep2Times;
|
2016-05-11 21:30:45 +00:00
|
|
|
refresh = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* True if backend voice needs reconfiguration before next sample */
|
|
|
|
return refresh;
|
2016-05-11 04:48:08 +00:00
|
|
|
}
|
|
|
|
|
2016-05-28 02:28:59 +00:00
|
|
|
uint32_t Voice::_GetBlockSampleCount(SampleFormat fmt)
|
|
|
|
{
|
|
|
|
switch (fmt)
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
return 1;
|
|
|
|
case Voice::SampleFormat::DSP:
|
|
|
|
return 14;
|
|
|
|
case Voice::SampleFormat::N64:
|
|
|
|
return 64;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-11 04:48:08 +00:00
|
|
|
size_t Voice::supplyAudio(size_t samples, int16_t* data)
|
|
|
|
{
|
|
|
|
uint32_t samplesRem = samples;
|
2016-05-11 21:30:45 +00:00
|
|
|
size_t samplesProc = 0;
|
2016-05-14 22:38:37 +00:00
|
|
|
|
2016-05-31 05:15:20 +00:00
|
|
|
/* Process SoundMacro; bootstrapping sample if needed */
|
|
|
|
bool dead = m_state.advance(*this, samples / m_sampleRate);
|
2016-05-14 22:38:37 +00:00
|
|
|
|
2016-06-02 02:28:48 +00:00
|
|
|
/* Process per-block evaluators here */
|
|
|
|
if (m_state.m_pedalSel)
|
|
|
|
{
|
|
|
|
bool pedal = m_state.m_pedalSel.evaluate(*this, m_state) >= 64;
|
|
|
|
if (pedal != m_sustained)
|
|
|
|
setPedal(pedal);
|
|
|
|
}
|
|
|
|
|
2016-05-11 04:48:08 +00:00
|
|
|
if (m_curSample)
|
|
|
|
{
|
2016-05-28 02:28:59 +00:00
|
|
|
uint32_t blockSampleCount = _GetBlockSampleCount(m_curFormat);
|
|
|
|
uint32_t block;
|
2016-05-16 20:20:29 +00:00
|
|
|
int32_t curPitch = m_curPitch;
|
2016-05-28 02:28:59 +00:00
|
|
|
bool refresh = false;
|
2016-05-16 20:20:29 +00:00
|
|
|
|
2016-05-28 02:28:59 +00:00
|
|
|
bool looped = true;
|
|
|
|
while (looped && samplesRem)
|
2016-05-16 02:40:18 +00:00
|
|
|
{
|
2016-05-28 02:28:59 +00:00
|
|
|
block = m_curSamplePos / blockSampleCount;
|
|
|
|
uint32_t rem = m_curSamplePos % blockSampleCount;
|
2016-05-16 02:40:18 +00:00
|
|
|
|
2016-05-28 02:28:59 +00:00
|
|
|
if (rem)
|
2016-05-16 02:40:18 +00:00
|
|
|
{
|
2016-05-28 02:28:59 +00:00
|
|
|
uint32_t remCount = std::min(samplesRem, m_lastSamplePos - block * blockSampleCount);
|
|
|
|
uint32_t decSamples;
|
2016-05-16 02:40:18 +00:00
|
|
|
|
2016-05-28 02:28:59 +00:00
|
|
|
switch (m_curFormat)
|
|
|
|
{
|
|
|
|
case SampleFormat::DSP:
|
|
|
|
{
|
|
|
|
decSamples = DSPDecompressFrameRanged(data, m_curSampleData + 8 * block,
|
2016-05-29 04:31:58 +00:00
|
|
|
m_curSample->second.dsp.m_coefs,
|
2016-05-28 02:28:59 +00:00
|
|
|
&m_prev1, &m_prev2, rem, remCount);
|
|
|
|
break;
|
|
|
|
}
|
2016-05-29 04:31:58 +00:00
|
|
|
case SampleFormat::N64:
|
|
|
|
{
|
|
|
|
decSamples = N64MusyXDecompressFrameRanged(data, m_curSampleData + 256 + 40 * block,
|
|
|
|
m_curSample->second.vadpcm.m_coefs,
|
|
|
|
rem, remCount);
|
|
|
|
break;
|
|
|
|
}
|
2016-05-28 02:28:59 +00:00
|
|
|
case SampleFormat::PCM:
|
|
|
|
{
|
|
|
|
const int16_t* pcm = reinterpret_cast<const int16_t*>(m_curSampleData);
|
|
|
|
remCount = std::min(samplesRem, m_lastSamplePos - m_curSamplePos);
|
|
|
|
for (uint32_t i=0 ; i<remCount ; ++i)
|
|
|
|
data[i] = SBig(pcm[m_curSamplePos+i]);
|
|
|
|
decSamples = remCount;
|
|
|
|
break;
|
|
|
|
}
|
2016-05-29 04:31:58 +00:00
|
|
|
case SampleFormat::PCM_PC:
|
2016-05-28 02:28:59 +00:00
|
|
|
{
|
2016-05-29 04:31:58 +00:00
|
|
|
const int16_t* pcm = reinterpret_cast<const int16_t*>(m_curSampleData);
|
|
|
|
remCount = std::min(samplesRem, m_lastSamplePos - m_curSamplePos);
|
|
|
|
memcpy(data, pcm + m_curSamplePos, remCount * sizeof(int16_t));
|
|
|
|
decSamples = remCount;
|
2016-05-28 02:28:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
memset(data, 0, sizeof(int16_t) * samples);
|
|
|
|
return samples;
|
|
|
|
}
|
2016-05-16 02:40:18 +00:00
|
|
|
|
2016-05-28 02:28:59 +00:00
|
|
|
/* Per-sample processing */
|
|
|
|
for (uint32_t i=0 ; i<decSamples ; ++i)
|
|
|
|
{
|
|
|
|
++samplesProc;
|
|
|
|
++m_curSamplePos;
|
|
|
|
refresh |= _advanceSample(data[i], curPitch);
|
|
|
|
}
|
2016-05-16 02:40:18 +00:00
|
|
|
|
2016-05-28 02:28:59 +00:00
|
|
|
samplesRem -= decSamples;
|
|
|
|
data += decSamples;
|
2016-05-16 02:40:18 +00:00
|
|
|
}
|
2016-05-28 02:28:59 +00:00
|
|
|
|
|
|
|
if (_checkSamplePos(looped))
|
2016-05-16 02:40:18 +00:00
|
|
|
{
|
2016-05-28 02:28:59 +00:00
|
|
|
if (samplesRem)
|
|
|
|
memset(data, 0, sizeof(int16_t) * samplesRem);
|
2016-05-16 20:20:29 +00:00
|
|
|
return samples;
|
2016-05-16 02:40:18 +00:00
|
|
|
}
|
2016-05-28 02:28:59 +00:00
|
|
|
if (looped)
|
|
|
|
continue;
|
2016-05-11 21:30:45 +00:00
|
|
|
|
2016-05-28 02:28:59 +00:00
|
|
|
while (samplesRem)
|
2016-05-11 21:30:45 +00:00
|
|
|
{
|
2016-05-28 02:28:59 +00:00
|
|
|
block = m_curSamplePos / blockSampleCount;
|
|
|
|
uint32_t remCount = std::min(samplesRem, m_lastSamplePos - block * blockSampleCount);
|
|
|
|
uint32_t decSamples;
|
2016-05-11 21:30:45 +00:00
|
|
|
|
2016-05-28 02:28:59 +00:00
|
|
|
switch (m_curFormat)
|
|
|
|
{
|
|
|
|
case SampleFormat::DSP:
|
|
|
|
{
|
|
|
|
decSamples = DSPDecompressFrame(data, m_curSampleData + 8 * block,
|
2016-05-29 04:31:58 +00:00
|
|
|
m_curSample->second.dsp.m_coefs,
|
2016-05-28 02:28:59 +00:00
|
|
|
&m_prev1, &m_prev2, remCount);
|
|
|
|
break;
|
|
|
|
}
|
2016-05-29 04:31:58 +00:00
|
|
|
case SampleFormat::N64:
|
|
|
|
{
|
|
|
|
decSamples = N64MusyXDecompressFrame(data, m_curSampleData + 256 + 40 * block,
|
|
|
|
m_curSample->second.vadpcm.m_coefs,
|
|
|
|
remCount);
|
|
|
|
break;
|
|
|
|
}
|
2016-05-28 02:28:59 +00:00
|
|
|
case SampleFormat::PCM:
|
|
|
|
{
|
|
|
|
const int16_t* pcm = reinterpret_cast<const int16_t*>(m_curSampleData);
|
|
|
|
remCount = std::min(samplesRem, m_lastSamplePos - m_curSamplePos);
|
|
|
|
for (uint32_t i=0 ; i<remCount ; ++i)
|
|
|
|
data[i] = SBig(pcm[m_curSamplePos+i]);
|
|
|
|
decSamples = remCount;
|
|
|
|
break;
|
|
|
|
}
|
2016-05-29 04:31:58 +00:00
|
|
|
case SampleFormat::PCM_PC:
|
2016-05-28 02:28:59 +00:00
|
|
|
{
|
2016-05-29 04:31:58 +00:00
|
|
|
const int16_t* pcm = reinterpret_cast<const int16_t*>(m_curSampleData);
|
|
|
|
remCount = std::min(samplesRem, m_lastSamplePos - m_curSamplePos);
|
|
|
|
memcpy(data, pcm + m_curSamplePos, remCount * sizeof(int16_t));
|
|
|
|
decSamples = remCount;
|
2016-05-28 02:28:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
memset(data, 0, sizeof(int16_t) * samples);
|
|
|
|
return samples;
|
|
|
|
}
|
2016-05-16 02:40:18 +00:00
|
|
|
|
2016-05-28 02:28:59 +00:00
|
|
|
/* Per-sample processing */
|
|
|
|
for (uint32_t i=0 ; i<decSamples ; ++i)
|
|
|
|
{
|
|
|
|
++samplesProc;
|
|
|
|
++m_curSamplePos;
|
|
|
|
refresh |= _advanceSample(data[i], curPitch);
|
|
|
|
}
|
|
|
|
|
|
|
|
samplesRem -= decSamples;
|
|
|
|
data += decSamples;
|
|
|
|
|
|
|
|
if (_checkSamplePos(looped))
|
|
|
|
{
|
|
|
|
if (samplesRem)
|
|
|
|
memset(data, 0, sizeof(int16_t) * samplesRem);
|
|
|
|
return samples;
|
|
|
|
}
|
|
|
|
if (looped)
|
|
|
|
break;
|
2016-05-11 04:48:08 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-16 20:20:29 +00:00
|
|
|
|
|
|
|
if (refresh)
|
2016-05-17 03:47:12 +00:00
|
|
|
_setTotalPitch(curPitch + m_pitchSweep1 + m_pitchSweep2 + m_pitchWheelVal, true);
|
2016-05-11 04:48:08 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
memset(data, 0, sizeof(int16_t) * samples);
|
|
|
|
|
2016-05-31 05:15:20 +00:00
|
|
|
if (dead && (!m_curSample || m_voxState == VoiceState::KeyOff) &&
|
2016-05-16 02:40:18 +00:00
|
|
|
m_sampleEndTrap.macroId == 0xffff &&
|
|
|
|
m_messageTrap.macroId == 0xffff &&
|
2016-05-19 11:03:38 +00:00
|
|
|
(!m_curSample || (m_curSample && m_volAdsr.isComplete())))
|
2016-05-14 22:38:37 +00:00
|
|
|
{
|
2016-05-22 08:35:55 +00:00
|
|
|
m_curSample = nullptr;
|
2016-05-14 22:38:37 +00:00
|
|
|
m_voxState = VoiceState::Dead;
|
2016-05-22 08:35:55 +00:00
|
|
|
m_backendVoice->stop();
|
2016-05-14 22:38:37 +00:00
|
|
|
}
|
2016-05-11 04:48:08 +00:00
|
|
|
return samples;
|
2016-05-03 01:16:26 +00:00
|
|
|
}
|
|
|
|
|
2016-05-11 21:30:45 +00:00
|
|
|
int Voice::maxVid() const
|
|
|
|
{
|
|
|
|
int maxVid = m_vid;
|
2016-05-14 04:46:39 +00:00
|
|
|
for (const std::shared_ptr<Voice>& vox : m_childVoices)
|
|
|
|
maxVid = std::max(maxVid, vox->maxVid());
|
2016-05-11 21:30:45 +00:00
|
|
|
return maxVid;
|
|
|
|
}
|
|
|
|
|
2016-05-15 06:48:26 +00:00
|
|
|
std::shared_ptr<Voice> Voice::_startChildMacro(ObjectId macroId, int macroStep, double ticksPerSec,
|
|
|
|
uint8_t midiKey, uint8_t midiVel, uint8_t midiMod, bool pushPc)
|
2016-05-03 05:16:37 +00:00
|
|
|
{
|
2016-05-31 20:40:51 +00:00
|
|
|
std::list<std::shared_ptr<Voice>>::iterator vox = _allocateVoice(32000.0, true);
|
|
|
|
if (!(*vox)->loadSoundObject(macroId, macroStep, ticksPerSec, midiKey,
|
|
|
|
midiVel, midiMod, pushPc))
|
2016-05-14 06:33:21 +00:00
|
|
|
{
|
2016-05-31 20:40:51 +00:00
|
|
|
_destroyVoice(vox);
|
2016-05-14 06:33:21 +00:00
|
|
|
return {};
|
|
|
|
}
|
2016-05-31 20:40:51 +00:00
|
|
|
(*vox)->setVolume(m_userVol);
|
|
|
|
(*vox)->setPan(m_userPan);
|
|
|
|
(*vox)->setSurroundPan(m_userSpan);
|
|
|
|
return *vox;
|
2016-05-03 05:16:37 +00:00
|
|
|
}
|
|
|
|
|
2016-05-15 06:48:26 +00:00
|
|
|
std::shared_ptr<Voice> Voice::startChildMacro(int8_t addNote, ObjectId macroId, int macroStep)
|
2016-05-03 05:16:37 +00:00
|
|
|
{
|
2016-05-15 06:48:26 +00:00
|
|
|
return _startChildMacro(macroId, macroStep, 1000.0, m_state.m_initKey + addNote,
|
|
|
|
m_state.m_initVel, m_state.m_initMod);
|
|
|
|
}
|
2016-05-11 21:30:45 +00:00
|
|
|
|
2016-05-15 06:48:26 +00:00
|
|
|
bool Voice::_loadSoundMacro(const unsigned char* macroData, int macroStep, double ticksPerSec,
|
|
|
|
uint8_t midiKey, uint8_t midiVel, uint8_t midiMod, bool pushPc)
|
|
|
|
{
|
2016-05-11 21:30:45 +00:00
|
|
|
if (m_state.m_pc.empty())
|
2016-05-28 02:28:59 +00:00
|
|
|
m_state.initialize(macroData, macroStep, ticksPerSec, midiKey, midiVel, midiMod,
|
|
|
|
m_audioGroup.getDataFormat() != DataFormat::PC);
|
2016-05-11 21:30:45 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!pushPc)
|
|
|
|
m_state.m_pc.clear();
|
|
|
|
m_state.m_pc.push_back({macroData, macroStep});
|
|
|
|
m_state.m_header = *reinterpret_cast<const SoundMacroState::Header*>(macroData);
|
2016-05-28 02:28:59 +00:00
|
|
|
if (m_audioGroup.getDataFormat() != DataFormat::PC)
|
|
|
|
m_state.m_header.swapBig();
|
2016-05-11 21:30:45 +00:00
|
|
|
}
|
|
|
|
|
2016-05-14 04:46:39 +00:00
|
|
|
m_voxState = VoiceState::Playing;
|
2016-05-14 22:38:37 +00:00
|
|
|
m_backendVoice->start();
|
2016-05-11 21:30:45 +00:00
|
|
|
return true;
|
2016-05-03 05:16:37 +00:00
|
|
|
}
|
|
|
|
|
2016-05-15 06:48:26 +00:00
|
|
|
bool Voice::_loadKeymap(const Keymap* keymap, int macroStep, double ticksPerSec,
|
|
|
|
uint8_t midiKey, uint8_t midiVel, uint8_t midiMod, bool pushPc)
|
|
|
|
{
|
|
|
|
const Keymap& km = keymap[midiKey];
|
2016-05-28 02:28:59 +00:00
|
|
|
ObjectId oid = (m_audioGroup.getDataFormat() == DataFormat::PC) ? km.objectId : SBig(km.objectId);
|
2016-05-15 06:48:26 +00:00
|
|
|
midiKey += km.transpose;
|
2016-05-28 02:28:59 +00:00
|
|
|
bool ret = loadSoundObject(oid, macroStep, ticksPerSec, midiKey, midiVel, midiMod, pushPc);
|
2016-05-21 21:40:03 +00:00
|
|
|
m_curVol = 1.f;
|
|
|
|
_setPan((km.pan - 64) / 64.f);
|
|
|
|
_setSurroundPan(-1.f);
|
|
|
|
return ret;
|
2016-05-15 06:48:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Voice::_loadLayer(const std::vector<const LayerMapping*>& layer, int macroStep, double ticksPerSec,
|
|
|
|
uint8_t midiKey, uint8_t midiVel, uint8_t midiMod, bool pushPc)
|
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
for (const LayerMapping* mapping : layer)
|
|
|
|
{
|
|
|
|
if (midiKey >= mapping->keyLo && midiKey <= mapping->keyHi)
|
|
|
|
{
|
2016-05-28 02:28:59 +00:00
|
|
|
ObjectId oid = (m_audioGroup.getDataFormat() == DataFormat::PC) ? mapping->objectId : SBig(mapping->objectId);
|
2016-05-16 02:40:18 +00:00
|
|
|
uint8_t mappingKey = midiKey + mapping->transpose;
|
2016-05-15 06:48:26 +00:00
|
|
|
if (m_voxState != VoiceState::Playing)
|
2016-05-21 21:40:03 +00:00
|
|
|
{
|
2016-05-28 02:28:59 +00:00
|
|
|
ret |= loadSoundObject(oid, macroStep, ticksPerSec,
|
2016-05-16 02:40:18 +00:00
|
|
|
mappingKey, midiVel, midiMod, pushPc);
|
2016-05-21 21:40:03 +00:00
|
|
|
m_curVol = mapping->volume / 127.f;
|
|
|
|
_setPan((mapping->pan - 64) / 64.f);
|
|
|
|
_setSurroundPan((mapping->span - 64) / 64.f);
|
|
|
|
}
|
2016-05-15 06:48:26 +00:00
|
|
|
else
|
2016-05-21 21:40:03 +00:00
|
|
|
{
|
|
|
|
std::shared_ptr<Voice> vox =
|
2016-05-28 02:28:59 +00:00
|
|
|
_startChildMacro(oid, macroStep, ticksPerSec,
|
2016-05-21 21:40:03 +00:00
|
|
|
mappingKey, midiVel, midiMod, pushPc);
|
|
|
|
if (vox)
|
|
|
|
{
|
|
|
|
vox->m_curVol = mapping->volume / 127.f;
|
|
|
|
vox->_setPan((mapping->pan - 64) / 64.f);
|
|
|
|
vox->_setSurroundPan((mapping->span - 64) / 64.f);
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
}
|
2016-05-15 06:48:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Voice::loadSoundObject(ObjectId objectId, int macroStep, double ticksPerSec,
|
|
|
|
uint8_t midiKey, uint8_t midiVel, uint8_t midiMod,
|
|
|
|
bool pushPc)
|
|
|
|
{
|
|
|
|
const unsigned char* macroData = m_audioGroup.getPool().soundMacro(objectId);
|
|
|
|
if (macroData)
|
|
|
|
return _loadSoundMacro(macroData, macroStep, ticksPerSec, midiKey, midiVel, midiMod, pushPc);
|
|
|
|
|
|
|
|
const Keymap* keymap = m_audioGroup.getPool().keymap(objectId);
|
|
|
|
if (keymap)
|
|
|
|
return _loadKeymap(keymap, macroStep, ticksPerSec, midiKey, midiVel, midiMod, pushPc);
|
|
|
|
|
|
|
|
const std::vector<const LayerMapping*>* layer = m_audioGroup.getPool().layer(objectId);
|
|
|
|
if (layer)
|
|
|
|
return _loadLayer(*layer, macroStep, ticksPerSec, midiKey, midiVel, midiMod, pushPc);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-05-16 02:40:18 +00:00
|
|
|
void Voice::_macroKeyOff()
|
2016-05-09 07:22:58 +00:00
|
|
|
{
|
2016-05-16 20:20:29 +00:00
|
|
|
if (m_voxState == VoiceState::Playing)
|
|
|
|
{
|
|
|
|
if (m_sustained)
|
|
|
|
m_sustainKeyOff = true;
|
|
|
|
else
|
|
|
|
_doKeyOff();
|
|
|
|
m_voxState = VoiceState::KeyOff;
|
|
|
|
}
|
2016-05-09 07:22:58 +00:00
|
|
|
}
|
|
|
|
|
2016-05-16 02:40:18 +00:00
|
|
|
void Voice::keyOff()
|
|
|
|
{
|
|
|
|
if (m_keyoffTrap.macroId != 0xffff)
|
|
|
|
{
|
|
|
|
if (m_keyoffTrap.macroId == m_state.m_header.m_macroId)
|
|
|
|
{
|
|
|
|
m_state.m_pc.back().second = m_keyoffTrap.macroStep;
|
|
|
|
m_state.m_inWait = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
loadSoundObject(m_keyoffTrap.macroId, m_keyoffTrap.macroStep,
|
|
|
|
m_state.m_ticksPerSec, m_state.m_initKey,
|
|
|
|
m_state.m_initVel, m_state.m_initMod);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
_macroKeyOff();
|
2016-05-19 05:27:39 +00:00
|
|
|
|
|
|
|
for (const std::shared_ptr<Voice>& vox : m_childVoices)
|
|
|
|
vox->keyOff();
|
2016-05-16 02:40:18 +00:00
|
|
|
}
|
|
|
|
|
2016-05-09 07:22:58 +00:00
|
|
|
void Voice::message(int32_t val)
|
|
|
|
{
|
2016-05-16 02:40:18 +00:00
|
|
|
m_messageQueue.push_back(val);
|
|
|
|
|
|
|
|
if (m_messageTrap.macroId != 0xffff)
|
|
|
|
{
|
|
|
|
if (m_messageTrap.macroId == m_state.m_header.m_macroId)
|
|
|
|
m_state.m_pc.back().second = m_messageTrap.macroStep;
|
|
|
|
else
|
|
|
|
loadSoundObject(m_messageTrap.macroId, m_messageTrap.macroStep,
|
|
|
|
m_state.m_ticksPerSec, m_state.m_initKey,
|
|
|
|
m_state.m_initVel, m_state.m_initMod);
|
|
|
|
}
|
2016-05-09 07:22:58 +00:00
|
|
|
}
|
|
|
|
|
2016-05-11 04:48:08 +00:00
|
|
|
void Voice::startSample(int16_t sampId, int32_t offset)
|
|
|
|
{
|
|
|
|
m_curSample = m_audioGroup.getSample(sampId);
|
|
|
|
if (m_curSample)
|
|
|
|
{
|
2016-05-11 21:30:45 +00:00
|
|
|
m_sampleRate = m_curSample->first.m_sampleRate;
|
|
|
|
m_curPitch = m_curSample->first.m_pitch;
|
2016-06-02 02:28:48 +00:00
|
|
|
m_pitchDirty = true;
|
2016-05-16 20:20:29 +00:00
|
|
|
setPitchWheel(m_curPitchWheel);
|
2016-05-11 04:48:08 +00:00
|
|
|
m_backendVoice->resetSampleRate(m_curSample->first.m_sampleRate);
|
2016-05-17 01:25:50 +00:00
|
|
|
|
|
|
|
int32_t numSamples = m_curSample->first.m_numSamples & 0xffffff;
|
|
|
|
if (offset)
|
|
|
|
{
|
|
|
|
if (m_curSample->first.m_loopLengthSamples)
|
|
|
|
{
|
2016-05-21 21:40:03 +00:00
|
|
|
if (offset > int32_t(m_curSample->first.m_loopStartSample))
|
2016-05-17 01:25:50 +00:00
|
|
|
offset = ((offset - m_curSample->first.m_loopStartSample) %
|
|
|
|
m_curSample->first.m_loopLengthSamples) +
|
|
|
|
m_curSample->first.m_loopStartSample;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
offset = clamp(0, offset, numSamples);
|
|
|
|
}
|
2016-05-11 04:48:08 +00:00
|
|
|
m_curSamplePos = offset;
|
|
|
|
m_curSampleData = m_audioGroup.getSampleData(m_curSample->first.m_sampleOff);
|
|
|
|
m_prev1 = 0;
|
|
|
|
m_prev2 = 0;
|
2016-05-31 05:15:20 +00:00
|
|
|
|
2016-05-29 04:31:58 +00:00
|
|
|
if (m_audioGroup.getDataFormat() == DataFormat::PC)
|
|
|
|
m_curFormat = SampleFormat::PCM_PC;
|
|
|
|
else
|
|
|
|
m_curFormat = SampleFormat(m_curSample->first.m_numSamples >> 24);
|
2016-05-31 05:15:20 +00:00
|
|
|
|
|
|
|
if (m_curFormat == SampleFormat::DSP_DRUM)
|
|
|
|
m_curFormat = SampleFormat::DSP;
|
|
|
|
|
2016-05-11 04:48:08 +00:00
|
|
|
m_lastSamplePos = m_curSample->first.m_loopLengthSamples ?
|
2016-05-14 04:46:39 +00:00
|
|
|
(m_curSample->first.m_loopStartSample + m_curSample->first.m_loopLengthSamples) : numSamples;
|
|
|
|
|
2016-05-28 02:28:59 +00:00
|
|
|
bool looped;
|
|
|
|
_checkSamplePos(looped);
|
2016-05-13 01:46:41 +00:00
|
|
|
|
|
|
|
/* Seek DSPADPCM state if needed */
|
2016-05-17 01:25:50 +00:00
|
|
|
if (m_curSample && m_curSamplePos && m_curFormat == SampleFormat::DSP)
|
2016-05-13 01:46:41 +00:00
|
|
|
{
|
|
|
|
uint32_t block = m_curSamplePos / 14;
|
|
|
|
uint32_t rem = m_curSamplePos % 14;
|
|
|
|
for (uint32_t b = 0 ; b < block ; ++b)
|
2016-05-29 04:31:58 +00:00
|
|
|
DSPDecompressFrameStateOnly(m_curSampleData + 8 * b, m_curSample->second.dsp.m_coefs,
|
2016-05-13 01:46:41 +00:00
|
|
|
&m_prev1, &m_prev2, 14);
|
|
|
|
|
|
|
|
if (rem)
|
2016-05-29 04:31:58 +00:00
|
|
|
DSPDecompressFrameStateOnly(m_curSampleData + 8 * block, m_curSample->second.dsp.m_coefs,
|
2016-05-13 01:46:41 +00:00
|
|
|
&m_prev1, &m_prev2, rem);
|
|
|
|
}
|
2016-05-11 04:48:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Voice::stopSample()
|
|
|
|
{
|
|
|
|
m_curSample = nullptr;
|
|
|
|
}
|
|
|
|
|
2016-05-09 07:22:58 +00:00
|
|
|
void Voice::setVolume(float vol)
|
|
|
|
{
|
2016-05-16 20:20:29 +00:00
|
|
|
m_userVol = clamp(0.f, vol, 1.f);
|
2016-05-16 22:13:24 +00:00
|
|
|
for (std::shared_ptr<Voice>& vox : m_childVoices)
|
|
|
|
vox->setVolume(vol);
|
2016-05-11 21:30:45 +00:00
|
|
|
}
|
|
|
|
|
2016-05-21 21:40:03 +00:00
|
|
|
void Voice::_setPan(float pan)
|
2016-05-11 21:30:45 +00:00
|
|
|
{
|
2016-05-16 20:20:29 +00:00
|
|
|
m_curPan = clamp(-1.f, pan, 1.f);
|
2016-05-21 21:40:03 +00:00
|
|
|
float totalPan = clamp(-1.f, m_curPan + m_userPan, 1.f);
|
|
|
|
float totalSpan = clamp(-1.f, m_curSpan + m_userSpan, 1.f);
|
|
|
|
float coefs[8];
|
|
|
|
|
|
|
|
/* Left */
|
|
|
|
coefs[0] = (totalPan <= 0.f) ? 1.f : (1.f - totalPan);
|
|
|
|
coefs[0] *= (totalSpan <= 0.f) ? 1.f : (1.f - totalSpan);
|
|
|
|
|
|
|
|
/* Right */
|
|
|
|
coefs[1] = (totalPan >= 0.f) ? 1.f : (1.f + totalPan);
|
|
|
|
coefs[1] *= (totalSpan <= 0.f) ? 1.f : (1.f - totalSpan);
|
|
|
|
|
|
|
|
/* Back Left */
|
|
|
|
coefs[2] = (totalPan <= 0.f) ? 1.f : (1.f - totalPan);
|
|
|
|
coefs[2] *= (totalSpan >= 0.f) ? 1.f : (1.f + totalSpan);
|
|
|
|
|
|
|
|
/* Back Right */
|
|
|
|
coefs[3] = (totalPan >= 0.f) ? 1.f : (1.f + totalPan);
|
|
|
|
coefs[3] *= (totalSpan >= 0.f) ? 1.f : (1.f + totalSpan);
|
|
|
|
|
|
|
|
/* Center */
|
|
|
|
coefs[4] = 1.f - std::fabs(totalPan);
|
|
|
|
|
|
|
|
/* LFE */
|
|
|
|
coefs[5] = 1.f;
|
|
|
|
|
|
|
|
/* Side Left */
|
|
|
|
coefs[6] = (totalPan <= 0.f) ? 1.f : (1.f - totalPan);
|
|
|
|
coefs[6] *= 1.f - std::fabs(totalSpan);
|
|
|
|
|
|
|
|
/* Side Right */
|
|
|
|
coefs[7] = (totalPan >= 0.f) ? 1.f : (1.f + totalPan);
|
|
|
|
coefs[7] *= 1.f - std::fabs(totalSpan);
|
|
|
|
|
|
|
|
m_backendVoice->setMatrixCoefficients(coefs, true);
|
2016-05-31 05:15:20 +00:00
|
|
|
|
|
|
|
for (int i=0 ; i<8 ; ++i)
|
|
|
|
coefs[i] *= m_curReverbVol;
|
|
|
|
m_backendVoice->setSubmixMatrixCoefficients(coefs, true);
|
2016-05-21 21:40:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Voice::setPan(float pan)
|
|
|
|
{
|
|
|
|
m_userPan = pan;
|
|
|
|
_setPan(m_curPan);
|
2016-05-16 22:13:24 +00:00
|
|
|
for (std::shared_ptr<Voice>& vox : m_childVoices)
|
|
|
|
vox->setPan(pan);
|
2016-05-09 07:22:58 +00:00
|
|
|
}
|
|
|
|
|
2016-05-21 21:40:03 +00:00
|
|
|
void Voice::_setSurroundPan(float span)
|
2016-05-09 07:22:58 +00:00
|
|
|
{
|
2016-05-16 20:20:29 +00:00
|
|
|
m_curSpan = clamp(-1.f, span, 1.f);
|
2016-05-21 21:40:03 +00:00
|
|
|
_setPan(m_curPan);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Voice::setSurroundPan(float span)
|
|
|
|
{
|
|
|
|
m_userSpan = span;
|
|
|
|
_setSurroundPan(m_curSpan);
|
2016-05-16 22:13:24 +00:00
|
|
|
for (std::shared_ptr<Voice>& vox : m_childVoices)
|
|
|
|
vox->setSurroundPan(span);
|
2016-05-09 07:22:58 +00:00
|
|
|
}
|
|
|
|
|
2016-05-11 21:30:45 +00:00
|
|
|
void Voice::startEnvelope(double dur, float vol, const Curve* envCurve)
|
2016-05-11 04:48:08 +00:00
|
|
|
{
|
2016-05-31 10:16:52 +00:00
|
|
|
m_envelopeTime = 0.f;
|
2016-05-11 21:30:45 +00:00
|
|
|
m_envelopeDur = dur;
|
2016-05-16 20:20:29 +00:00
|
|
|
m_envelopeStart = clamp(0.f, m_curVol, 1.f);
|
|
|
|
m_envelopeEnd = clamp(0.f, vol, 1.f);
|
2016-05-11 21:30:45 +00:00
|
|
|
m_envelopeCurve = envCurve;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Voice::startFadeIn(double dur, float vol, const Curve* envCurve)
|
|
|
|
{
|
2016-05-31 10:16:52 +00:00
|
|
|
m_envelopeTime = 0.f;
|
2016-05-11 21:30:45 +00:00
|
|
|
m_envelopeDur = dur;
|
|
|
|
m_envelopeStart = 0.f;
|
2016-05-21 21:40:03 +00:00
|
|
|
m_envelopeEnd = clamp(0.f, vol, 1.f);
|
2016-05-11 21:30:45 +00:00
|
|
|
m_envelopeCurve = envCurve;
|
|
|
|
}
|
|
|
|
|
2016-05-21 21:40:03 +00:00
|
|
|
void Voice::startPanning(double dur, uint8_t panPos, int8_t panWidth)
|
2016-05-11 21:30:45 +00:00
|
|
|
{
|
2016-06-01 21:17:16 +00:00
|
|
|
m_panningTime = 0.f;
|
2016-05-11 21:30:45 +00:00
|
|
|
m_panningDur = dur;
|
|
|
|
m_panPos = panPos;
|
|
|
|
m_panWidth = panWidth;
|
|
|
|
}
|
|
|
|
|
2016-05-21 21:40:03 +00:00
|
|
|
void Voice::startSpanning(double dur, uint8_t spanPos, int8_t spanWidth)
|
2016-05-11 21:30:45 +00:00
|
|
|
{
|
2016-06-01 21:17:16 +00:00
|
|
|
m_spanningTime = 0.f;
|
2016-05-11 21:30:45 +00:00
|
|
|
m_spanningDur = dur;
|
|
|
|
m_spanPos = spanPos;
|
|
|
|
m_spanWidth = spanWidth;
|
2016-05-11 04:48:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Voice::setPitchKey(int32_t cents)
|
|
|
|
{
|
2016-05-11 21:30:45 +00:00
|
|
|
m_curPitch = cents;
|
|
|
|
m_pitchDirty = true;
|
2016-05-11 04:48:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Voice::setPedal(bool pedal)
|
|
|
|
{
|
|
|
|
if (m_sustained && !pedal && m_sustainKeyOff)
|
|
|
|
{
|
|
|
|
m_sustainKeyOff = false;
|
|
|
|
_doKeyOff();
|
|
|
|
}
|
|
|
|
m_sustained = pedal;
|
2016-05-14 04:46:39 +00:00
|
|
|
|
|
|
|
for (std::shared_ptr<Voice>& vox : m_childVoices)
|
|
|
|
vox->setPedal(pedal);
|
2016-05-11 04:48:08 +00:00
|
|
|
}
|
|
|
|
|
2016-05-21 21:40:03 +00:00
|
|
|
void Voice::setDoppler(float)
|
2016-05-11 04:48:08 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-05-11 21:30:45 +00:00
|
|
|
void Voice::setVibrato(int32_t level, int32_t modLevel, float period)
|
|
|
|
{
|
|
|
|
m_vibratoLevel = level;
|
|
|
|
m_vibratoModLevel = modLevel;
|
|
|
|
m_vibratoPeriod = period;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Voice::setMod2VibratoRange(int32_t modLevel)
|
|
|
|
{
|
|
|
|
m_vibratoModLevel = modLevel;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Voice::setTremolo(float tremoloScale, float tremoloModScale)
|
|
|
|
{
|
|
|
|
m_tremoloScale = tremoloScale;
|
|
|
|
m_tremoloModScale = tremoloModScale;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Voice::setPitchSweep1(uint8_t times, int16_t add)
|
|
|
|
{
|
|
|
|
m_pitchSweep1 = 0;
|
2016-05-17 03:23:35 +00:00
|
|
|
m_pitchSweep1It = 0;
|
|
|
|
m_pitchSweep1Times = times * 160;
|
2016-05-11 21:30:45 +00:00
|
|
|
m_pitchSweep1Add = add;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Voice::setPitchSweep2(uint8_t times, int16_t add)
|
|
|
|
{
|
|
|
|
m_pitchSweep2 = 0;
|
2016-05-17 03:23:35 +00:00
|
|
|
m_pitchSweep2It = 0;
|
|
|
|
m_pitchSweep2Times = times * 160;
|
2016-05-11 21:30:45 +00:00
|
|
|
m_pitchSweep2Add = add;
|
|
|
|
}
|
|
|
|
|
2016-05-11 04:48:08 +00:00
|
|
|
void Voice::setReverbVol(float rvol)
|
|
|
|
{
|
2016-05-31 05:15:20 +00:00
|
|
|
m_curReverbVol = clamp(0.f, rvol, 1.f);
|
|
|
|
_setPan(m_curPan);
|
|
|
|
for (std::shared_ptr<Voice>& vox : m_childVoices)
|
|
|
|
vox->setReverbVol(rvol);
|
2016-05-11 04:48:08 +00:00
|
|
|
}
|
|
|
|
|
2016-05-16 20:20:29 +00:00
|
|
|
void Voice::setAdsr(ObjectId adsrId, bool dls)
|
2016-05-11 04:48:08 +00:00
|
|
|
{
|
2016-05-16 20:20:29 +00:00
|
|
|
if (dls)
|
|
|
|
{
|
|
|
|
const ADSRDLS* adsr = m_audioGroup.getPool().tableAsAdsrDLS(adsrId);
|
|
|
|
if (adsr)
|
2016-05-19 05:27:39 +00:00
|
|
|
{
|
2016-05-16 20:20:29 +00:00
|
|
|
m_volAdsr.reset(adsr, m_state.m_initKey, m_state.m_initVel);
|
2016-05-19 05:27:39 +00:00
|
|
|
if (m_voxState == VoiceState::KeyOff)
|
|
|
|
m_volAdsr.keyOff();
|
|
|
|
}
|
2016-05-16 20:20:29 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const ADSR* adsr = m_audioGroup.getPool().tableAsAdsr(adsrId);
|
|
|
|
if (adsr)
|
2016-05-19 05:27:39 +00:00
|
|
|
{
|
2016-05-16 20:20:29 +00:00
|
|
|
m_volAdsr.reset(adsr);
|
2016-05-19 05:27:39 +00:00
|
|
|
if (m_voxState == VoiceState::KeyOff)
|
|
|
|
m_volAdsr.keyOff();
|
|
|
|
}
|
2016-05-16 20:20:29 +00:00
|
|
|
}
|
2016-05-11 04:48:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Voice::setPitchFrequency(uint32_t hz, uint16_t fine)
|
|
|
|
{
|
2016-05-11 21:30:45 +00:00
|
|
|
m_sampleRate = hz + fine / 65536.0;
|
2016-05-17 03:47:12 +00:00
|
|
|
m_backendVoice->setPitchRatio(1.0, false);
|
2016-05-11 21:30:45 +00:00
|
|
|
m_backendVoice->resetSampleRate(m_sampleRate);
|
2016-05-11 04:48:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Voice::setPitchAdsr(ObjectId adsrId, int32_t cents)
|
|
|
|
{
|
2016-05-16 20:20:29 +00:00
|
|
|
const ADSRDLS* adsr = m_audioGroup.getPool().tableAsAdsrDLS(adsrId);
|
2016-05-14 22:38:37 +00:00
|
|
|
if (adsr)
|
|
|
|
{
|
2016-05-16 20:20:29 +00:00
|
|
|
m_pitchAdsr.reset(adsr, m_state.m_initKey, m_state.m_initVel);
|
2016-05-14 22:38:37 +00:00
|
|
|
m_pitchEnvRange = cents;
|
|
|
|
m_pitchEnv = true;
|
|
|
|
}
|
2016-05-11 04:48:08 +00:00
|
|
|
}
|
|
|
|
|
2016-05-15 06:48:26 +00:00
|
|
|
void Voice::setPitchWheel(float pitchWheel)
|
|
|
|
{
|
|
|
|
m_curPitchWheel = amuse::clamp(-1.f, pitchWheel, 1.f);
|
|
|
|
if (pitchWheel > 0.f)
|
2016-05-17 03:23:35 +00:00
|
|
|
m_pitchWheelVal = m_pitchWheelUp * m_curPitchWheel;
|
2016-05-15 06:48:26 +00:00
|
|
|
else if (pitchWheel < 0.f)
|
2016-05-17 03:23:35 +00:00
|
|
|
m_pitchWheelVal = m_pitchWheelDown * m_curPitchWheel;
|
2016-05-15 06:48:26 +00:00
|
|
|
else
|
|
|
|
m_pitchWheelVal = 0;
|
|
|
|
m_pitchDirty = true;
|
2016-05-16 22:13:24 +00:00
|
|
|
|
|
|
|
for (std::shared_ptr<Voice>& vox : m_childVoices)
|
|
|
|
vox->setPitchWheel(pitchWheel);
|
2016-05-15 06:48:26 +00:00
|
|
|
}
|
|
|
|
|
2016-05-11 04:48:08 +00:00
|
|
|
void Voice::setPitchWheelRange(int8_t up, int8_t down)
|
|
|
|
{
|
2016-05-15 06:48:26 +00:00
|
|
|
m_pitchWheelUp = up * 100;
|
|
|
|
m_pitchWheelDown = down * 100;
|
|
|
|
setPitchWheel(m_curPitchWheel);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Voice::setAftertouch(uint8_t aftertouch)
|
|
|
|
{
|
|
|
|
m_curAftertouch = aftertouch;
|
2016-05-16 22:13:24 +00:00
|
|
|
for (std::shared_ptr<Voice>& vox : m_childVoices)
|
|
|
|
vox->setAftertouch(aftertouch);
|
2016-05-15 06:48:26 +00:00
|
|
|
}
|
|
|
|
|
2016-06-01 21:17:16 +00:00
|
|
|
bool Voice::doPortamento(uint8_t newNote)
|
|
|
|
{
|
|
|
|
bool pState;
|
|
|
|
switch (m_state.m_portamentoMode)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
default:
|
|
|
|
pState = false;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
pState = true;
|
|
|
|
break;
|
|
|
|
case 2:
|
2016-06-02 02:28:48 +00:00
|
|
|
pState = (m_state.m_portamentoSel ? m_state.m_portamentoSel.evaluate(*this, m_state) : getCtrlValue(65)) >= 64;
|
2016-06-01 21:17:16 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pState)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
m_portamentoTime = 0.f;
|
|
|
|
m_portamentoTarget = newNote * 100;
|
|
|
|
m_state.m_initKey = newNote;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-21 21:40:03 +00:00
|
|
|
void Voice::_notifyCtrlChange(uint8_t ctrl, int8_t val)
|
2016-05-16 20:20:29 +00:00
|
|
|
{
|
2016-05-31 10:16:52 +00:00
|
|
|
if (ctrl == 0x40)
|
2016-05-16 20:20:29 +00:00
|
|
|
{
|
2016-05-31 10:16:52 +00:00
|
|
|
if (val >= 0x40)
|
2016-05-16 20:20:29 +00:00
|
|
|
setPedal(true);
|
|
|
|
else
|
|
|
|
setPedal(false);
|
|
|
|
}
|
2016-05-31 05:15:20 +00:00
|
|
|
else if (ctrl == 0x5b)
|
2016-05-31 10:16:52 +00:00
|
|
|
{
|
2016-05-31 05:15:20 +00:00
|
|
|
setReverbVol(val / 127.f);
|
2016-05-31 10:16:52 +00:00
|
|
|
}
|
2016-05-16 22:13:24 +00:00
|
|
|
|
|
|
|
for (std::shared_ptr<Voice>& vox : m_childVoices)
|
2016-05-21 21:40:03 +00:00
|
|
|
vox->_notifyCtrlChange(ctrl, val);
|
2016-05-16 20:20:29 +00:00
|
|
|
}
|
|
|
|
|
2016-05-15 06:48:26 +00:00
|
|
|
size_t Voice::getTotalVoices() const
|
|
|
|
{
|
|
|
|
size_t ret = 1;
|
|
|
|
for (const std::shared_ptr<Voice>& vox : m_childVoices)
|
|
|
|
ret += vox->getTotalVoices();
|
|
|
|
return ret;
|
2016-05-11 04:48:08 +00:00
|
|
|
}
|
|
|
|
|
2016-05-31 20:40:51 +00:00
|
|
|
void Voice::kill()
|
|
|
|
{
|
|
|
|
m_voxState = VoiceState::Dead;
|
|
|
|
for (const std::shared_ptr<Voice>& vox : m_childVoices)
|
|
|
|
vox->kill();
|
|
|
|
}
|
|
|
|
|
2016-05-03 01:16:26 +00:00
|
|
|
}
|