此Vst Synth示例的说明

时间:2013-11-05 15:33:47

标签: c++ signal-processing vst

我无法理解Steinberg VST Synth示例中的特定代码区域

在此功能中:


void VstXSynth::processReplacing (float** inputs, float** outputs, VstInt32 sampleFrames)
{
    float* out1 = outputs[0];
    float* out2 = outputs[1];

if (noteIsOn) { float baseFreq = freqtab[currentNote & 0x7f] * fScaler; float freq1 = baseFreq + fFreq1; // not really linear... float freq2 = baseFreq + fFreq2; float* wave1 = (fWaveform1 < .5) ? sawtooth : pulse; float* wave2 = (fWaveform2 < .5) ? sawtooth : pulse; float wsf = (float)kWaveSize; float vol = (float)(fVolume * (double)currentVelocity * midiScaler); VstInt32 mask = kWaveSize - 1;

    if (currentDelta > 0)
    {
        if (currentDelta >= sampleFrames)   // future
        {
            currentDelta -= sampleFrames;
            return;
        }
        memset (out1, 0, currentDelta * sizeof (float));
        memset (out2, 0, currentDelta * sizeof (float));
        out1 += currentDelta;
        out2 += currentDelta;
        sampleFrames -= currentDelta;
        currentDelta = 0;
    }

    // loop
    while (--sampleFrames >= 0)
    {
        // this is all very raw, there is no means of interpolation,
        // and we will certainly get aliasing due to non-bandlimited
        // waveforms. don't use this for serious projects...
        (*out1++) = wave1[(VstInt32)fPhase1 & mask] * fVolume1 * vol;
        (*out2++) = wave2[(VstInt32)fPhase2 & mask] * fVolume2 * vol;
        fPhase1 += freq1;
        fPhase2 += freq2;
    }
}                       
else
{
    memset (out1, 0, sampleFrames * sizeof (float));
    memset (out2, 0, sampleFrames * sizeof (float));
}

}

我理解该函数的方法是,如果当前正在打开midi音符,我们需要将波表复制到输出数组中以传递回VstHost。我不明白的是

    if (currentDelta > 0)
    {
        if (currentDelta >= sampleFrames)   // future
        {
            currentDelta -= sampleFrames;
            return;
        }
        memset (out1, 0, currentDelta * sizeof (float));
        memset (out2, 0, currentDelta * sizeof (float));
        out1 += currentDelta;
        out2 += currentDelta;
        sampleFrames -= currentDelta;
        currentDelta = 0;
    }

    // loop
    while (--sampleFrames >= 0)
    {
        // this is all very raw, there is no means of interpolation,
        // and we will certainly get aliasing due to non-bandlimited
        // waveforms. don't use this for serious projects...
        (*out1++) = wave1[(VstInt32)fPhase1 & mask] * fVolume1 * vol;
        (*out2++) = wave2[(VstInt32)fPhase2 & mask] * fVolume2 * vol;
        fPhase1 += freq1;
        fPhase2 += freq2;
    }
}                       
else
{
    memset (out1, 0, sampleFrames * sizeof (float));
    memset (out2, 0, sampleFrames * sizeof (float));
}
条件块中的区域正在做什么。它似乎只是在输出数组中写入零...

可以在http://pastebin.com/SdAXkRyW

找到该文件的完整版本

1 个答案:

答案 0 :(得分:1)

接收的MIDI NoteOn事件可以相对于您收到的缓冲区的开头有一个偏移量(称为deltaFrames)。 currentDelta跟踪相对于接收到的缓冲区的开始应该播放音符的时间。

所以如果currentDelta&gt; sampleFrames,表示该音符不应该在此周期(未来)中播放 - 提前退出。

如果currentDelta在这个周期的范围内,则记忆被清除到音符应该产生输出的时刻(memset)并且操纵指针使其看起来像缓冲区在声音应该发出的位置开始play - length -sampleFrames-也会调整。

然后在循环中产生声音。

希望它有所帮助。 马克