3 |
* LinuxSampler - modular, streaming capable sampler * |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
* * |
5 |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
* Copyright (C) 2005, 2006 Christian Schoenebeck * |
* Copyright (C) 2005 - 2009 Christian Schoenebeck * |
7 |
* * |
* * |
8 |
* This program is free software; you can redistribute it and/or modify * |
* This program is free software; you can redistribute it and/or modify * |
9 |
* it under the terms of the GNU General Public License as published by * |
* it under the terms of the GNU General Public License as published by * |
24 |
#include "../../common/Features.h" |
#include "../../common/Features.h" |
25 |
#include "Synthesizer.h" |
#include "Synthesizer.h" |
26 |
#include "Profiler.h" |
#include "Profiler.h" |
27 |
|
#include "Engine.h" |
28 |
|
#include "EngineChannel.h" |
29 |
|
|
30 |
#include "Voice.h" |
#include "Voice.h" |
31 |
|
|
32 |
namespace LinuxSampler { namespace gig { |
namespace LinuxSampler { namespace gig { |
33 |
|
|
|
const float Voice::FILTER_CUTOFF_COEFF(CalculateFilterCutoffCoeff()); |
|
|
|
|
|
float Voice::CalculateFilterCutoffCoeff() { |
|
|
return log(CONFIG_FILTER_CUTOFF_MAX / CONFIG_FILTER_CUTOFF_MIN); |
|
|
} |
|
|
|
|
34 |
Voice::Voice() { |
Voice::Voice() { |
35 |
pEngine = NULL; |
pEngine = NULL; |
36 |
pDiskThread = NULL; |
pDiskThread = NULL; |
40 |
pLFO3 = new LFOSigned(1200.0f); // pitch EG (-1200..+1200 range) |
pLFO3 = new LFOSigned(1200.0f); // pitch EG (-1200..+1200 range) |
41 |
KeyGroup = 0; |
KeyGroup = 0; |
42 |
SynthesisMode = 0; // set all mode bits to 0 first |
SynthesisMode = 0; // set all mode bits to 0 first |
43 |
// select synthesis implementation (currently either pure C++ or MMX+SSE(1)) |
// select synthesis implementation (asm core is not supported ATM) |
44 |
#if CONFIG_ASM && ARCH_X86 |
#if 0 // CONFIG_ASM && ARCH_X86 |
45 |
SYNTHESIS_MODE_SET_IMPLEMENTATION(SynthesisMode, Features::supportsMMX() && Features::supportsSSE()); |
SYNTHESIS_MODE_SET_IMPLEMENTATION(SynthesisMode, Features::supportsMMX() && Features::supportsSSE()); |
46 |
#else |
#else |
47 |
SYNTHESIS_MODE_SET_IMPLEMENTATION(SynthesisMode, false); |
SYNTHESIS_MODE_SET_IMPLEMENTATION(SynthesisMode, false); |
58 |
if (pLFO3) delete pLFO3; |
if (pLFO3) delete pLFO3; |
59 |
} |
} |
60 |
|
|
61 |
void Voice::SetEngine(Engine* pEngine) { |
void Voice::SetEngine(LinuxSampler::Engine* pEngine) { |
62 |
this->pEngine = pEngine; |
Engine* engine = static_cast<Engine*>(pEngine); |
63 |
this->pDiskThread = pEngine->pDiskThread; |
this->pEngine = engine; |
64 |
|
this->pDiskThread = engine->pDiskThread; |
65 |
dmsg(6,("Voice::SetEngine()\n")); |
dmsg(6,("Voice::SetEngine()\n")); |
66 |
} |
} |
67 |
|
|
82 |
int Voice::Trigger(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOnEvent, int PitchBend, ::gig::DimensionRegion* pDimRgn, type_t VoiceType, int iKeyGroup) { |
int Voice::Trigger(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOnEvent, int PitchBend, ::gig::DimensionRegion* pDimRgn, type_t VoiceType, int iKeyGroup) { |
83 |
this->pEngineChannel = pEngineChannel; |
this->pEngineChannel = pEngineChannel; |
84 |
this->pDimRgn = pDimRgn; |
this->pDimRgn = pDimRgn; |
85 |
|
Orphan = false; |
86 |
|
|
87 |
#if CONFIG_DEVMODE |
#if CONFIG_DEVMODE |
88 |
if (itNoteOnEvent->FragmentPos() > pEngine->MaxSamplesPerCycle) { // just a sanity check for debugging |
if (itNoteOnEvent->FragmentPos() > pEngine->MaxSamplesPerCycle) { // just a sanity check for debugging |
102 |
// calculate volume |
// calculate volume |
103 |
const double velocityAttenuation = pDimRgn->GetVelocityAttenuation(itNoteOnEvent->Param.Note.Velocity); |
const double velocityAttenuation = pDimRgn->GetVelocityAttenuation(itNoteOnEvent->Param.Note.Velocity); |
104 |
|
|
105 |
float volume = velocityAttenuation / 32768.0f; // we downscale by 32768 to convert from int16 value range to DSP value range (which is -1.0..1.0) |
// For 16 bit samples, we downscale by 32768 to convert from |
106 |
|
// int16 value range to DSP value range (which is |
107 |
|
// -1.0..1.0). For 24 bit, we downscale from int32. |
108 |
|
float volume = velocityAttenuation / (pSample->BitDepth == 16 ? 32768.0f : 32768.0f * 65536.0f); |
109 |
|
|
110 |
volume *= pDimRgn->SampleAttenuation; |
volume *= pDimRgn->SampleAttenuation * pEngineChannel->GlobalVolume * GLOBAL_VOLUME; |
111 |
|
|
112 |
// the volume of release triggered samples depends on note length |
// the volume of release triggered samples depends on note length |
113 |
if (Type == type_release_trigger) { |
if (Type == type_release_trigger) { |
120 |
|
|
121 |
// select channel mode (mono or stereo) |
// select channel mode (mono or stereo) |
122 |
SYNTHESIS_MODE_SET_CHANNELS(SynthesisMode, pSample->Channels == 2); |
SYNTHESIS_MODE_SET_CHANNELS(SynthesisMode, pSample->Channels == 2); |
123 |
|
// select bit depth (16 or 24) |
124 |
|
SYNTHESIS_MODE_SET_BITDEPTH24(SynthesisMode, pSample->BitDepth == 24); |
125 |
|
|
126 |
// get starting crossfade volume level |
// get starting crossfade volume level |
127 |
float crossfadeVolume; |
float crossfadeVolume; |
128 |
switch (pDimRgn->AttenuationController.type) { |
switch (pDimRgn->AttenuationController.type) { |
129 |
case ::gig::attenuation_ctrl_t::type_channelaftertouch: |
case ::gig::attenuation_ctrl_t::type_channelaftertouch: |
130 |
crossfadeVolume = 1.0f; //TODO: aftertouch not supported yet |
crossfadeVolume = Engine::CrossfadeCurve[CrossfadeAttenuation(pEngineChannel->ControllerTable[128])]; |
131 |
break; |
break; |
132 |
case ::gig::attenuation_ctrl_t::type_velocity: |
case ::gig::attenuation_ctrl_t::type_velocity: |
133 |
crossfadeVolume = Engine::CrossfadeCurve[CrossfadeAttenuation(itNoteOnEvent->Param.Note.Velocity)]; |
crossfadeVolume = Engine::CrossfadeCurve[CrossfadeAttenuation(itNoteOnEvent->Param.Note.Velocity)]; |
145 |
|
|
146 |
float subfragmentRate = pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE; |
float subfragmentRate = pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE; |
147 |
CrossfadeSmoother.trigger(crossfadeVolume, subfragmentRate); |
CrossfadeSmoother.trigger(crossfadeVolume, subfragmentRate); |
148 |
VolumeSmoother.trigger(pEngineChannel->GlobalVolume, subfragmentRate); |
VolumeSmoother.trigger(pEngineChannel->MidiVolume, subfragmentRate); |
149 |
PanLeftSmoother.trigger(pEngineChannel->GlobalPanLeft, subfragmentRate); |
PanLeftSmoother.trigger(pEngineChannel->GlobalPanLeft, subfragmentRate); |
150 |
PanRightSmoother.trigger(pEngineChannel->GlobalPanRight, subfragmentRate); |
PanRightSmoother.trigger(pEngineChannel->GlobalPanRight, subfragmentRate); |
151 |
|
|
156 |
long cachedsamples = pSample->GetCache().Size / pSample->FrameSize; |
long cachedsamples = pSample->GetCache().Size / pSample->FrameSize; |
157 |
DiskVoice = cachedsamples < pSample->SamplesTotal; |
DiskVoice = cachedsamples < pSample->SamplesTotal; |
158 |
|
|
159 |
|
const DLS::sample_loop_t& loopinfo = pDimRgn->pSampleLoops[0]; |
160 |
|
|
161 |
if (DiskVoice) { // voice to be streamed from disk |
if (DiskVoice) { // voice to be streamed from disk |
162 |
MaxRAMPos = cachedsamples - (pEngine->MaxSamplesPerCycle << CONFIG_MAX_PITCH) / pSample->Channels; //TODO: this calculation is too pessimistic and may better be moved to Render() method, so it calculates MaxRAMPos dependent to the current demand of sample points to be rendered (e.g. in case of JACK) |
if (cachedsamples > (pEngine->MaxSamplesPerCycle << CONFIG_MAX_PITCH)) { |
163 |
|
MaxRAMPos = cachedsamples - (pEngine->MaxSamplesPerCycle << CONFIG_MAX_PITCH) / pSample->Channels; //TODO: this calculation is too pessimistic and may better be moved to Render() method, so it calculates MaxRAMPos dependent to the current demand of sample points to be rendered (e.g. in case of JACK) |
164 |
|
} else { |
165 |
|
// The cache is too small to fit a max sample buffer. |
166 |
|
// Setting MaxRAMPos to 0 will probably cause a click |
167 |
|
// in the audio, but it's better than not handling |
168 |
|
// this case at all, which would have caused the |
169 |
|
// unsigned MaxRAMPos to be set to a negative number. |
170 |
|
MaxRAMPos = 0; |
171 |
|
} |
172 |
|
|
173 |
// check if there's a loop defined which completely fits into the cached (RAM) part of the sample |
// check if there's a loop defined which completely fits into the cached (RAM) part of the sample |
174 |
RAMLoop = (pSample->Loops && pSample->LoopEnd <= MaxRAMPos); |
RAMLoop = (pDimRgn->SampleLoops && (loopinfo.LoopStart + loopinfo.LoopLength) <= MaxRAMPos); |
175 |
|
|
176 |
if (pDiskThread->OrderNewStream(&DiskStreamRef, pSample, MaxRAMPos, !RAMLoop) < 0) { |
if (pDiskThread->OrderNewStream(&DiskStreamRef, pDimRgn, MaxRAMPos, !RAMLoop) < 0) { |
177 |
dmsg(1,("Disk stream order failed!\n")); |
dmsg(1,("Disk stream order failed!\n")); |
178 |
KillImmediately(); |
KillImmediately(); |
179 |
return -1; |
return -1; |
182 |
} |
} |
183 |
else { // RAM only voice |
else { // RAM only voice |
184 |
MaxRAMPos = cachedsamples; |
MaxRAMPos = cachedsamples; |
185 |
RAMLoop = (pSample->Loops != 0); |
RAMLoop = (pDimRgn->SampleLoops != 0); |
186 |
dmsg(4,("RAM only voice launched (Looping: %s)\n", (RAMLoop) ? "yes" : "no")); |
dmsg(4,("RAM only voice launched (Looping: %s)\n", (RAMLoop) ? "yes" : "no")); |
187 |
} |
} |
188 |
if (RAMLoop) { |
if (RAMLoop) { |
189 |
loop.uiTotalCycles = pSample->LoopPlayCount; |
loop.uiTotalCycles = pSample->LoopPlayCount; |
190 |
loop.uiCyclesLeft = pSample->LoopPlayCount; |
loop.uiCyclesLeft = pSample->LoopPlayCount; |
191 |
loop.uiStart = pSample->LoopStart; |
loop.uiStart = loopinfo.LoopStart; |
192 |
loop.uiEnd = pSample->LoopEnd; |
loop.uiEnd = loopinfo.LoopStart + loopinfo.LoopLength; |
193 |
loop.uiSize = pSample->LoopSize; |
loop.uiSize = loopinfo.LoopLength; |
194 |
} |
} |
195 |
|
|
196 |
// calculate initial pitch value |
// calculate initial pitch value |
197 |
{ |
{ |
198 |
double pitchbasecents = pDimRgn->FineTune + (int) pEngine->ScaleTuning[MIDIKey % 12]; |
double pitchbasecents = pEngineChannel->pInstrument->FineTune + pDimRgn->FineTune + pEngine->ScaleTuning[MIDIKey % 12]; |
199 |
if (pDimRgn->PitchTrack) pitchbasecents += (MIDIKey - (int) pDimRgn->UnityNote) * 100; |
|
200 |
this->PitchBase = RTMath::CentsToFreqRatio(pitchbasecents) * (double(pSample->SamplesPerSecond) / double(pEngine->SampleRate)); |
// GSt behaviour: maximum transpose up is 40 semitones. If |
201 |
this->PitchBend = RTMath::CentsToFreqRatio(((double) PitchBend / 8192.0) * 200.0); // pitchbend wheel +-2 semitones = 200 cents |
// MIDI key is more than 40 semitones above unity note, |
202 |
|
// the transpose is not done. |
203 |
|
if (pDimRgn->PitchTrack && (MIDIKey - (int) pDimRgn->UnityNote) < 40) pitchbasecents += (MIDIKey - (int) pDimRgn->UnityNote) * 100; |
204 |
|
|
205 |
|
this->PitchBase = RTMath::CentsToFreqRatioUnlimited(pitchbasecents) * (double(pSample->SamplesPerSecond) / double(pEngine->SampleRate)); |
206 |
|
this->PitchBendRange = 1.0 / 8192.0 * 100.0 * pEngineChannel->pInstrument->PitchbendRange; |
207 |
|
this->PitchBend = RTMath::CentsToFreqRatio(PitchBend * PitchBendRange); |
208 |
} |
} |
209 |
|
|
210 |
// the length of the decay and release curves are dependent on the velocity |
// the length of the decay and release curves are dependent on the velocity |
219 |
eg1controllervalue = 0; |
eg1controllervalue = 0; |
220 |
break; |
break; |
221 |
case ::gig::eg1_ctrl_t::type_channelaftertouch: |
case ::gig::eg1_ctrl_t::type_channelaftertouch: |
222 |
eg1controllervalue = 0; // TODO: aftertouch not yet supported |
eg1controllervalue = pEngineChannel->ControllerTable[128]; |
223 |
break; |
break; |
224 |
case ::gig::eg1_ctrl_t::type_velocity: |
case ::gig::eg1_ctrl_t::type_velocity: |
225 |
eg1controllervalue = itNoteOnEvent->Param.Note.Velocity; |
eg1controllervalue = itNoteOnEvent->Param.Note.Velocity; |
260 |
else |
else |
261 |
#else |
#else |
262 |
{ |
{ |
263 |
float finalVolume = pEngineChannel->GlobalVolume * crossfadeVolume * EG1.getLevel(); |
float finalVolume = pEngineChannel->MidiVolume * crossfadeVolume * EG1.getLevel(); |
264 |
|
|
265 |
finalSynthesisParameters.fFinalVolumeLeft = finalVolume * VolumeLeft * pEngineChannel->GlobalPanLeft; |
finalSynthesisParameters.fFinalVolumeLeft = finalVolume * VolumeLeft * pEngineChannel->GlobalPanLeft; |
266 |
finalSynthesisParameters.fFinalVolumeRight = finalVolume * VolumeRight * pEngineChannel->GlobalPanRight; |
finalSynthesisParameters.fFinalVolumeRight = finalVolume * VolumeRight * pEngineChannel->GlobalPanRight; |
277 |
eg2controllervalue = 0; |
eg2controllervalue = 0; |
278 |
break; |
break; |
279 |
case ::gig::eg2_ctrl_t::type_channelaftertouch: |
case ::gig::eg2_ctrl_t::type_channelaftertouch: |
280 |
eg2controllervalue = 0; // TODO: aftertouch not yet supported |
eg2controllervalue = pEngineChannel->ControllerTable[128]; |
281 |
break; |
break; |
282 |
case ::gig::eg2_ctrl_t::type_velocity: |
case ::gig::eg2_ctrl_t::type_velocity: |
283 |
eg2controllervalue = itNoteOnEvent->Param.Note.Velocity; |
eg2controllervalue = itNoteOnEvent->Param.Note.Velocity; |
357 |
} |
} |
358 |
if (bLFO1Enabled) { |
if (bLFO1Enabled) { |
359 |
pLFO1->trigger(pDimRgn->LFO1Frequency, |
pLFO1->trigger(pDimRgn->LFO1Frequency, |
360 |
start_level_max, |
start_level_min, |
361 |
lfo1_internal_depth, |
lfo1_internal_depth, |
362 |
pDimRgn->LFO1ControlDepth, |
pDimRgn->LFO1ControlDepth, |
363 |
pDimRgn->LFO1FlipPhase, |
pDimRgn->LFO1FlipPhase, |
429 |
break; |
break; |
430 |
case ::gig::lfo3_ctrl_aftertouch: |
case ::gig::lfo3_ctrl_aftertouch: |
431 |
lfo3_internal_depth = 0; |
lfo3_internal_depth = 0; |
432 |
pLFO3->ExtController = 0; // TODO: aftertouch not implemented yet |
pLFO3->ExtController = 128; |
433 |
bLFO3Enabled = false; // see TODO comment in line above |
bLFO3Enabled = true; |
434 |
break; |
break; |
435 |
case ::gig::lfo3_ctrl_internal_modwheel: |
case ::gig::lfo3_ctrl_internal_modwheel: |
436 |
lfo3_internal_depth = pDimRgn->LFO3InternalDepth; |
lfo3_internal_depth = pDimRgn->LFO3InternalDepth; |
439 |
break; |
break; |
440 |
case ::gig::lfo3_ctrl_internal_aftertouch: |
case ::gig::lfo3_ctrl_internal_aftertouch: |
441 |
lfo3_internal_depth = pDimRgn->LFO3InternalDepth; |
lfo3_internal_depth = pDimRgn->LFO3InternalDepth; |
442 |
pLFO1->ExtController = 0; // TODO: aftertouch not implemented yet |
pLFO1->ExtController = 128; |
443 |
bLFO3Enabled = (lfo3_internal_depth > 0 /*|| pDimRgn->LFO3ControlDepth > 0*/); // see TODO comment in line above |
bLFO3Enabled = (lfo3_internal_depth > 0 || pDimRgn->LFO3ControlDepth > 0); |
444 |
break; |
break; |
445 |
default: |
default: |
446 |
lfo3_internal_depth = 0; |
lfo3_internal_depth = 0; |
497 |
case ::gig::vcf_cutoff_ctrl_genpurpose8: |
case ::gig::vcf_cutoff_ctrl_genpurpose8: |
498 |
VCFCutoffCtrl.controller = 83; |
VCFCutoffCtrl.controller = 83; |
499 |
break; |
break; |
500 |
case ::gig::vcf_cutoff_ctrl_aftertouch: //TODO: not implemented yet |
case ::gig::vcf_cutoff_ctrl_aftertouch: |
501 |
|
VCFCutoffCtrl.controller = 128; |
502 |
|
break; |
503 |
case ::gig::vcf_cutoff_ctrl_none: |
case ::gig::vcf_cutoff_ctrl_none: |
504 |
default: |
default: |
505 |
VCFCutoffCtrl.controller = 0; |
VCFCutoffCtrl.controller = 0; |
533 |
finalSynthesisParameters.filterLeft.SetType(pDimRgn->VCFType); |
finalSynthesisParameters.filterLeft.SetType(pDimRgn->VCFType); |
534 |
finalSynthesisParameters.filterRight.SetType(pDimRgn->VCFType); |
finalSynthesisParameters.filterRight.SetType(pDimRgn->VCFType); |
535 |
#else // override filter type |
#else // override filter type |
536 |
FilterLeft.SetType(CONFIG_OVERRIDE_FILTER_TYPE); |
finalSynthesisParameters.filterLeft.SetType(CONFIG_OVERRIDE_FILTER_TYPE); |
537 |
FilterRight.SetType(CONFIG_OVERRIDE_FILTER_TYPE); |
finalSynthesisParameters.filterRight.SetType(CONFIG_OVERRIDE_FILTER_TYPE); |
538 |
#endif // CONFIG_OVERRIDE_FILTER_TYPE |
#endif // CONFIG_OVERRIDE_FILTER_TYPE |
539 |
|
|
540 |
VCFCutoffCtrl.value = pEngineChannel->ControllerTable[VCFCutoffCtrl.controller]; |
VCFCutoffCtrl.value = pEngineChannel->ControllerTable[VCFCutoffCtrl.controller]; |
557 |
else { |
else { |
558 |
cvalue = pDimRgn->VCFCutoff; |
cvalue = pDimRgn->VCFCutoff; |
559 |
} |
} |
560 |
cutoff *= float(cvalue) * 0.00787402f; // (1 / 127) |
cutoff *= float(cvalue); |
561 |
if (cutoff > 1.0) cutoff = 1.0; |
if (cutoff > 127.0f) cutoff = 127.0f; |
|
cutoff = (cutoff < 0.5 ? cutoff * 4826 - 1 : cutoff * 5715 - 449); |
|
|
if (cutoff < 1.0) cutoff = 1.0; |
|
562 |
|
|
563 |
// calculate resonance |
// calculate resonance |
564 |
float resonance = (float) (VCFResonanceCtrl.controller ? VCFResonanceCtrl.value : pDimRgn->VCFResonance) * 0.00787f; // 0.0..1.0 |
float resonance = (float) (VCFResonanceCtrl.controller ? VCFResonanceCtrl.value : pDimRgn->VCFResonance); |
565 |
|
|
566 |
VCFCutoffCtrl.fvalue = cutoff - 1.0; |
VCFCutoffCtrl.fvalue = cutoff; |
567 |
VCFResonanceCtrl.fvalue = resonance; |
VCFResonanceCtrl.fvalue = resonance; |
568 |
} |
} |
569 |
else { |
else { |
640 |
} |
} |
641 |
} |
} |
642 |
|
|
643 |
sample_t* ptr = DiskStreamRef.pStream->GetReadPtr(); // get the current read_ptr within the ringbuffer where we read the samples from |
sample_t* ptr = (sample_t*)DiskStreamRef.pStream->GetReadPtr(); // get the current read_ptr within the ringbuffer where we read the samples from |
644 |
|
|
645 |
// render current audio fragment |
// render current audio fragment |
646 |
Synthesize(Samples, ptr, Delay); |
Synthesize(Samples, ptr, Delay); |
738 |
CrossfadeSmoother.update(Engine::CrossfadeCurve[CrossfadeAttenuation(itEvent->Param.CC.Value)]); |
CrossfadeSmoother.update(Engine::CrossfadeCurve[CrossfadeAttenuation(itEvent->Param.CC.Value)]); |
739 |
} |
} |
740 |
if (itEvent->Param.CC.Controller == 7) { // volume |
if (itEvent->Param.CC.Controller == 7) { // volume |
741 |
VolumeSmoother.update(Engine::VolumeCurve[itEvent->Param.CC.Value] * CONFIG_GLOBAL_ATTENUATION); |
VolumeSmoother.update(Engine::VolumeCurve[itEvent->Param.CC.Value]); |
742 |
} else if (itEvent->Param.CC.Controller == 10) { // panpot |
} else if (itEvent->Param.CC.Controller == 10) { // panpot |
743 |
PanLeftSmoother.update(Engine::PanCurve[128 - itEvent->Param.CC.Value]); |
PanLeftSmoother.update(Engine::PanCurve[128 - itEvent->Param.CC.Value]); |
744 |
PanRightSmoother.update(Engine::PanCurve[itEvent->Param.CC.Value]); |
PanRightSmoother.update(Engine::PanCurve[itEvent->Param.CC.Value]); |
750 |
} |
} |
751 |
|
|
752 |
void Voice::processPitchEvent(RTList<Event>::Iterator& itEvent) { |
void Voice::processPitchEvent(RTList<Event>::Iterator& itEvent) { |
753 |
const float pitch = RTMath::CentsToFreqRatio(((double) itEvent->Param.Pitch.Pitch / 8192.0) * 200.0); // +-two semitones = +-200 cents |
PitchBend = RTMath::CentsToFreqRatio(itEvent->Param.Pitch.Pitch * PitchBendRange); |
|
finalSynthesisParameters.fFinalPitch *= pitch; |
|
|
PitchBend = pitch; |
|
754 |
} |
} |
755 |
|
|
756 |
void Voice::processCutoffEvent(RTList<Event>::Iterator& itEvent) { |
void Voice::processCutoffEvent(RTList<Event>::Iterator& itEvent) { |
759 |
VCFCutoffCtrl.value == ccvalue; |
VCFCutoffCtrl.value == ccvalue; |
760 |
if (pDimRgn->VCFCutoffControllerInvert) ccvalue = 127 - ccvalue; |
if (pDimRgn->VCFCutoffControllerInvert) ccvalue = 127 - ccvalue; |
761 |
if (ccvalue < pDimRgn->VCFVelocityScale) ccvalue = pDimRgn->VCFVelocityScale; |
if (ccvalue < pDimRgn->VCFVelocityScale) ccvalue = pDimRgn->VCFVelocityScale; |
762 |
float cutoff = CutoffBase * float(ccvalue) * 0.00787402f; // (1 / 127) |
float cutoff = CutoffBase * float(ccvalue); |
763 |
if (cutoff > 1.0) cutoff = 1.0; |
if (cutoff > 127.0f) cutoff = 127.0f; |
|
cutoff = (cutoff < 0.5 ? cutoff * 4826 - 1 : cutoff * 5715 - 449); |
|
|
if (cutoff < 1.0) cutoff = 1.0; |
|
764 |
|
|
765 |
VCFCutoffCtrl.fvalue = cutoff - 1.0; // needed for initialization of fFinalCutoff next time |
VCFCutoffCtrl.fvalue = cutoff; // needed for initialization of fFinalCutoff next time |
766 |
fFinalCutoff = cutoff; |
fFinalCutoff = cutoff; |
767 |
} |
} |
768 |
|
|
770 |
// convert absolute controller value to differential |
// convert absolute controller value to differential |
771 |
const int ctrldelta = itEvent->Param.CC.Value - VCFResonanceCtrl.value; |
const int ctrldelta = itEvent->Param.CC.Value - VCFResonanceCtrl.value; |
772 |
VCFResonanceCtrl.value = itEvent->Param.CC.Value; |
VCFResonanceCtrl.value = itEvent->Param.CC.Value; |
773 |
const float resonancedelta = (float) ctrldelta * 0.00787f; // 0.0..1.0 |
const float resonancedelta = (float) ctrldelta; |
774 |
fFinalResonance += resonancedelta; |
fFinalResonance += resonancedelta; |
775 |
// needed for initialization of parameter |
// needed for initialization of parameter |
776 |
VCFResonanceCtrl.fvalue = itEvent->Param.CC.Value * 0.00787f; |
VCFResonanceCtrl.fvalue = itEvent->Param.CC.Value; |
777 |
} |
} |
778 |
|
|
779 |
/** |
/** |
785 |
* @param Skip - number of sample points to skip in output buffer |
* @param Skip - number of sample points to skip in output buffer |
786 |
*/ |
*/ |
787 |
void Voice::Synthesize(uint Samples, sample_t* pSrc, uint Skip) { |
void Voice::Synthesize(uint Samples, sample_t* pSrc, uint Skip) { |
788 |
finalSynthesisParameters.pOutLeft = &pEngineChannel->pOutputLeft[Skip]; |
finalSynthesisParameters.pOutLeft = &pEngineChannel->pChannelLeft->Buffer()[Skip]; |
789 |
finalSynthesisParameters.pOutRight = &pEngineChannel->pOutputRight[Skip]; |
finalSynthesisParameters.pOutRight = &pEngineChannel->pChannelRight->Buffer()[Skip]; |
790 |
finalSynthesisParameters.pSrc = pSrc; |
finalSynthesisParameters.pSrc = pSrc; |
791 |
|
|
792 |
RTList<Event>::Iterator itCCEvent = pEngineChannel->pEvents->first(); |
RTList<Event>::Iterator itCCEvent = pEngineChannel->pEvents->first(); |
793 |
RTList<Event>::Iterator itNoteEvent = pEngineChannel->pMIDIKeyInfo[MIDIKey].pEvents->first(); |
RTList<Event>::Iterator itNoteEvent = pEngineChannel->pMIDIKeyInfo[MIDIKey].pEvents->first(); |
794 |
|
|
795 |
if (Skip) { // skip events that happened before this voice was triggered |
if (itTriggerEvent) { // skip events that happened before this voice was triggered |
796 |
while (itCCEvent && itCCEvent->FragmentPos() <= Skip) ++itCCEvent; |
while (itCCEvent && itCCEvent->FragmentPos() <= Skip) ++itCCEvent; |
797 |
while (itNoteEvent && itNoteEvent->FragmentPos() <= Skip) ++itNoteEvent; |
// we can't simply compare the timestamp here, because note events |
798 |
|
// might happen on the same time stamp, so we have to deal on the |
799 |
|
// actual sequence the note events arrived instead (see bug #112) |
800 |
|
for (; itNoteEvent; ++itNoteEvent) { |
801 |
|
if (itTriggerEvent == itNoteEvent) { |
802 |
|
++itNoteEvent; |
803 |
|
break; |
804 |
|
} |
805 |
|
} |
806 |
} |
} |
807 |
|
|
808 |
uint killPos; |
uint killPos; |
809 |
if (itKillEvent) killPos = RTMath::Min(itKillEvent->FragmentPos(), pEngine->MaxFadeOutPos); |
if (itKillEvent) { |
810 |
|
int maxFadeOutPos = Samples - pEngine->MinFadeOutSamples; |
811 |
|
if (maxFadeOutPos < 0) { |
812 |
|
// There's not enough space in buffer to do a fade out |
813 |
|
// from max volume (this can only happen for audio |
814 |
|
// drivers that use Samples < MaxSamplesPerCycle). |
815 |
|
// End the EG1 here, at pos 0, with a shorter max fade |
816 |
|
// out time. |
817 |
|
EG1.enterFadeOutStage(Samples / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); |
818 |
|
itKillEvent = Pool<Event>::Iterator(); |
819 |
|
} else { |
820 |
|
killPos = RTMath::Min(itKillEvent->FragmentPos(), maxFadeOutPos); |
821 |
|
} |
822 |
|
} |
823 |
|
|
824 |
uint i = Skip; |
uint i = Skip; |
825 |
while (i < Samples) { |
while (i < Samples) { |
826 |
int iSubFragmentEnd = RTMath::Min(i + CONFIG_DEFAULT_SUBFRAGMENT_SIZE, Samples); |
int iSubFragmentEnd = RTMath::Min(i + CONFIG_DEFAULT_SUBFRAGMENT_SIZE, Samples); |
827 |
|
|
828 |
// initialize all final synthesis parameters |
// initialize all final synthesis parameters |
|
finalSynthesisParameters.fFinalPitch = PitchBase * PitchBend; |
|
829 |
fFinalCutoff = VCFCutoffCtrl.fvalue; |
fFinalCutoff = VCFCutoffCtrl.fvalue; |
830 |
fFinalResonance = VCFResonanceCtrl.fvalue; |
fFinalResonance = VCFResonanceCtrl.fvalue; |
831 |
|
|
832 |
// process MIDI control change and pitchbend events for this subfragment |
// process MIDI control change and pitchbend events for this subfragment |
833 |
processCCEvents(itCCEvent, iSubFragmentEnd); |
processCCEvents(itCCEvent, iSubFragmentEnd); |
834 |
|
|
835 |
|
finalSynthesisParameters.fFinalPitch = PitchBase * PitchBend; |
836 |
float fFinalVolume = VolumeSmoother.render() * CrossfadeSmoother.render(); |
float fFinalVolume = VolumeSmoother.render() * CrossfadeSmoother.render(); |
837 |
#ifdef CONFIG_PROCESS_MUTED_CHANNELS |
#ifdef CONFIG_PROCESS_MUTED_CHANNELS |
838 |
if (pEngineChannel->GetMute()) fFinalVolume = 0; |
if (pEngineChannel->GetMute()) fFinalVolume = 0; |
841 |
// process transition events (note on, note off & sustain pedal) |
// process transition events (note on, note off & sustain pedal) |
842 |
processTransitionEvents(itNoteEvent, iSubFragmentEnd); |
processTransitionEvents(itNoteEvent, iSubFragmentEnd); |
843 |
|
|
844 |
// if the voice was killed in this subfragment switch EG1 to fade out stage |
// if the voice was killed in this subfragment, or if the |
845 |
if (itKillEvent && killPos <= iSubFragmentEnd) { |
// filter EG is finished, switch EG1 to fade out stage |
846 |
|
if ((itKillEvent && killPos <= iSubFragmentEnd) || |
847 |
|
(SYNTHESIS_MODE_GET_FILTER(SynthesisMode) && |
848 |
|
EG2.getSegmentType() == EGADSR::segment_end)) { |
849 |
EG1.enterFadeOutStage(); |
EG1.enterFadeOutStage(); |
850 |
itKillEvent = Pool<Event>::Iterator(); |
itKillEvent = Pool<Event>::Iterator(); |
851 |
} |
} |
876 |
if (EG3.active()) finalSynthesisParameters.fFinalPitch *= EG3.render(); |
if (EG3.active()) finalSynthesisParameters.fFinalPitch *= EG3.render(); |
877 |
|
|
878 |
// process low frequency oscillators |
// process low frequency oscillators |
879 |
if (bLFO1Enabled) fFinalVolume *= pLFO1->render(); |
if (bLFO1Enabled) fFinalVolume *= (1.0f - pLFO1->render()); |
880 |
if (bLFO2Enabled) fFinalCutoff *= pLFO2->render(); |
if (bLFO2Enabled) fFinalCutoff *= pLFO2->render(); |
881 |
if (bLFO3Enabled) finalSynthesisParameters.fFinalPitch *= RTMath::CentsToFreqRatio(pLFO3->render()); |
if (bLFO3Enabled) finalSynthesisParameters.fFinalPitch *= RTMath::CentsToFreqRatio(pLFO3->render()); |
882 |
|
|
883 |
|
// limit the pitch so we don't read outside the buffer |
884 |
|
finalSynthesisParameters.fFinalPitch = RTMath::Min(finalSynthesisParameters.fFinalPitch, float(1 << CONFIG_MAX_PITCH)); |
885 |
|
|
886 |
// if filter enabled then update filter coefficients |
// if filter enabled then update filter coefficients |
887 |
if (SYNTHESIS_MODE_GET_FILTER(SynthesisMode)) { |
if (SYNTHESIS_MODE_GET_FILTER(SynthesisMode)) { |
888 |
finalSynthesisParameters.filterLeft.SetParameters(fFinalCutoff + 1.0, fFinalResonance, pEngine->SampleRate); |
finalSynthesisParameters.filterLeft.SetParameters(fFinalCutoff, fFinalResonance, pEngine->SampleRate); |
889 |
finalSynthesisParameters.filterRight.SetParameters(fFinalCutoff + 1.0, fFinalResonance, pEngine->SampleRate); |
finalSynthesisParameters.filterRight.SetParameters(fFinalCutoff, fFinalResonance, pEngine->SampleRate); |
890 |
} |
} |
891 |
|
|
892 |
// do we need resampling? |
// do we need resampling? |
923 |
if (EG1.active()) { |
if (EG1.active()) { |
924 |
|
|
925 |
// if sample has a loop and loop start has been reached in this subfragment, send a special event to EG1 to let it finish the attack hold stage |
// if sample has a loop and loop start has been reached in this subfragment, send a special event to EG1 to let it finish the attack hold stage |
926 |
if (pSample->Loops && Pos <= pSample->LoopStart && pSample->LoopStart < newPos) { |
if (pDimRgn->SampleLoops && Pos <= pDimRgn->pSampleLoops[0].LoopStart && pDimRgn->pSampleLoops[0].LoopStart < newPos) { |
927 |
EG1.update(EGADSR::event_hold_end, pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); |
EG1.update(EGADSR::event_hold_end, pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); |
928 |
} |
} |
929 |
|
|
961 |
* fading down the volume level to avoid clicks and regular processing |
* fading down the volume level to avoid clicks and regular processing |
962 |
* until the kill event actually occured! |
* until the kill event actually occured! |
963 |
* |
* |
964 |
* @see Kill() |
* If it's necessary to know when the voice's disk stream was actually |
965 |
|
* deleted, then one can set the optional @a bRequestNotification |
966 |
|
* parameter and this method will then return the handle of the disk |
967 |
|
* stream (unique identifier) and one can use this handle to poll the |
968 |
|
* disk thread if this stream has been deleted. In any case this method |
969 |
|
* will return immediately and will not block until the stream actually |
970 |
|
* was deleted. |
971 |
|
* |
972 |
|
* @param bRequestNotification - (optional) whether the disk thread shall |
973 |
|
* provide a notification once it deleted |
974 |
|
* the respective disk stream |
975 |
|
* (default=false) |
976 |
|
* @returns handle to the voice's disk stream or @c Stream::INVALID_HANDLE |
977 |
|
* if the voice did not use a disk stream at all |
978 |
|
* @see Kill() |
979 |
*/ |
*/ |
980 |
void Voice::KillImmediately() { |
Stream::Handle Voice::KillImmediately(bool bRequestNotification) { |
981 |
|
Stream::Handle hStream = Stream::INVALID_HANDLE; |
982 |
if (DiskVoice && DiskStreamRef.State != Stream::state_unused) { |
if (DiskVoice && DiskStreamRef.State != Stream::state_unused) { |
983 |
pDiskThread->OrderDeletionOfStream(&DiskStreamRef); |
pDiskThread->OrderDeletionOfStream(&DiskStreamRef, bRequestNotification); |
984 |
|
hStream = DiskStreamRef.hStream; |
985 |
} |
} |
986 |
Reset(); |
Reset(); |
987 |
|
return hStream; |
988 |
} |
} |
989 |
|
|
990 |
/** |
/** |