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 - 2007 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 |
|
|
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 |
|
|
159 |
const DLS::sample_loop_t& loopinfo = pDimRgn->pSampleLoops[0]; |
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 = (pDimRgn->SampleLoops && (loopinfo.LoopStart + loopinfo.LoopLength) <= MaxRAMPos); |
RAMLoop = (pDimRgn->SampleLoops && (loopinfo.LoopStart + loopinfo.LoopLength) <= MaxRAMPos); |
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 |
|
|
200 |
// GSt behaviour: maximum transpose up is 40 semitones. If |
// GSt behaviour: maximum transpose up is 40 semitones. If |
201 |
// MIDI key is more than 40 semitones above unity note, |
// MIDI key is more than 40 semitones above unity note, |
202 |
// the transpose is not done. |
// the transpose is not done. |
203 |
if (pDimRgn->PitchTrack && (MIDIKey - (int) pDimRgn->UnityNote) < 40) pitchbasecents += (MIDIKey - (int) pDimRgn->UnityNote) * 100; |
if (pDimRgn->PitchTrack && (MIDIKey - (int) pDimRgn->UnityNote) < 40) pitchbasecents += (MIDIKey - (int) pDimRgn->UnityNote) * 100; |
204 |
|
|
205 |
this->PitchBase = RTMath::CentsToFreqRatio(pitchbasecents) * (double(pSample->SamplesPerSecond) / double(pEngine->SampleRate)); |
this->PitchBase = RTMath::CentsToFreqRatioUnlimited(pitchbasecents) * (double(pSample->SamplesPerSecond) / double(pEngine->SampleRate)); |
206 |
this->PitchBend = RTMath::CentsToFreqRatio(((double) PitchBend / 8192.0) * 200.0); // pitchbend wheel +-2 semitones = 200 cents |
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 |
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) { |
794 |
|
|
795 |
if (itTriggerEvent) { // 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; |
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; |
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, fFinalResonance, pEngine->SampleRate); |
finalSynthesisParameters.filterLeft.SetParameters(fFinalCutoff, fFinalResonance, pEngine->SampleRate); |