public void Process(float[,] workingBuffer, int startIndex, int endIndex)
{
if (inUse)
{
//quick checks to do before we go through our main loop
if (synth.Channels == 2 && pan != synth.PanPositions[channel])
this.setPan(synth.PanPositions[channel]);
//set sampleRate for tune
variableSampleRate = synth.SampleRate * Math.Pow(2.0, (synth.TunePositions[channel] * -1.0) / 12.0);
//main loop
for (int i = startIndex; i < endIndex; i++)
{
//manage states and calculate volume level
switch (state)
{
case VoiceState.Attack:
fadeCounter--;
if (fadeCounter <= 0)
{
state = VoiceState.Sustain;
fadeMultiplier = 1.0f;
}
else
{
fadeMultiplier = 1.0f - (fadeCounter / (float)attack);
}
break;
case VoiceState.Sustain:
decayCounter--;
if (decayCounter <= 0)
{
state = VoiceState.None;
inUse = false;
fadeMultiplier = 0.0f;
}
else
{
fadeMultiplier = decayCounter / (float)decay;
}
break;
case VoiceState.Hold:
fadeCounter--;//not used for volume
decayCounter--;
if (decayCounter <= 0)
{
state = VoiceState.None;
inUse = false;
fadeMultiplier = 0.0f;
}
else if (fadeCounter <= 0)
{
state = VoiceState.Release;
fadeCounter = release;
}
else
{
fadeMultiplier = decayCounter / (float)decay;
}
break;
case VoiceState.Release:
fadeCounter--;
if (fadeCounter <= 0)
{
state = VoiceState.None;
inUse = false;
}
else
{//Multiply decay with fadeout so volume doesn't suddenly rise when releasing notes
fadeMultiplier = (decayCounter / (float)decay) * (fadeCounter / (float)release);
}
break;
}
//end of state management
//Decide how to sample based on channels available
//mono output
if (synth.Channels == 1)
{
float sample = inst.getSampleAtTime(note, 0, synth.SampleRate, ref time);
sample = sample * (velocity / 127.0f) * synth.VolPositions[channel];
workingBuffer[0, i] += (sample * fadeMultiplier * gainControl);
}
//mono sample to stereo output
else if (synth.Channels == 2 && inst.allSamplesSupportDualChannel() == false)
{
float sample = inst.getSampleAtTime(note, 0, synth.SampleRate, ref time);
sample = sample * (velocity / 127.0f) * synth.VolPositions[channel];
workingBuffer[0, i] += (sample * fadeMultiplier * leftpan * gainControl);
workingBuffer[1, i] += (sample * fadeMultiplier * rightpan * gainControl);
}
//both support stereo
else
{
}
time += 1.0 / variableSampleRate;
//bailout of the loop if there is no reason to continue.
if (inUse == false)
return;
}
}
}