gpt4 book ai didi

c++ - 简单的 VST 合成器以随机间隔改变音高

转载 作者:太空宇宙 更新时间:2023-11-04 13:53:50 27 4
gpt4 key购买 nike

我正在学习如何使用 Steinberg VST 2.4 SDK(或者更确切地说,是 3.6.0 版本附带的 2.x 部分)。我创建了一个简单的合成器,旨在在其生命周期内以恒定频率播放正弦波。这是这个合成器的代码:

static const float TAU = 3.14159f * 2;

AudioEffect* createEffectInstance(audioMasterCallback audioMaster) {
return new VSTTest(audioMaster);
}

VSTTest::VSTTest(audioMasterCallback audioMaster) : AudioEffectX(audioMaster, 0, NUM_PARAMS), //NUM_PARAMS is 0
fDeltaTime(1.0f / getSampleRate()), //time per sample (1.0 / 44100, presumably)
fFrequency(100.0f), //frequency of the wave (100 Hz)
fAmplitude(0.5f), //amplitude of the wave
fPos(0.0f) { //position of the wave in the x direction
setNumInputs(0);
setNumOutputs(2);
canProcessReplacing();
isSynth();
}

VSTTest::~VSTTest(void) {

}

void VSTTest::processReplacing(float** input, float** output, VstInt32 numFrames) {
for (VstInt32 i = 0; i < numFrames; i++) {
output[0][i] = fAmplitude * sin(fPos);
output[1][i] = fAmplitude * sin(fPos);

fPos += fFrequency * TAU * fDeltaTime;

if (fPos >= TAU) {
fPos = fmod(fPos, TAU);
}
}
}

void VSTTest::setSampleRate(float fSampleRate) {
fDeltaTime = 1.0f / fSampleRate;
}

问题是,当 VST 在 FL Studio 中作为一个 channel 加载时,我可以听到(并看到)它在大约 20 秒内多次改变音高,直到它稳定在一个甚至不正确的最终音高上(偏离约 10 赫兹)。为什么会这样?

最佳答案

您正在向 fPos 添加一个巨大的数字,它应该只增加 1。您要查找的公式是:

sinf(2.0 * M_PI * fPos++ * fFrequency / fSampleRate);

关于c++ - 简单的 VST 合成器以随机间隔改变音高,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22425619/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com