0

编辑:结果证明这是一个未初始化的变量,会产生混乱的行为。请参阅这篇关于获取更多 JUCE 编译器警告的帖子

我试图创建一个基本的合成器,但在尝试为新声明的变量赋值时很快遇到了一个荒谬的问题。在遵循 JUCE 简单的正弦合成教程之后,我遇到了问题。这是我的getNextAudioBlock()函数在产生白噪声时的基本代码。请注意如何在整个过程中声明和分配四个整数:

const int numChannels = bufferToFill.buffer->getNumChannels();
const int numSamples = bufferToFill.numSamples;
for (int channel = 0; channel < numChannels; channel++){
    float* const buffer = bufferToFill.buffer -> getWritePointer(channel, bufferToFill.startSample);
    for (int sample; sample < numSamples; sample++){
        buffer[sample] = (randomGen.nextFloat() * 2.0f - 1.0f);
    }
}

但是,一旦我尝试添加另一个 int,我就再也听不到声音了。只需简单地int unusedVariable = 0;在函数中的任何位置添加该行,getNextAudioBlock()但在buffer[sample]分配立即从函数返回之前,因此它不会产生音频。

如果我只是简单地声明新变量 ( int unusedVariable;),那么它仍然有效。只有特别是分配部分导致错误。此外,如果我将变量声明为全局成员,那么函数内的赋值就可以正常工作。

重申一下,这有效:

buffer[sample] = (randomGen.nextFloat() * 2.0f - 1.0f;

这有效:

int unusedVariable;
buffer[sample] = (randomGen.nextFloat() * 2.0f - 1.0f;

但这不会:

int unusedVariable = 0;
buffer[sample] = (randomGen.nextFloat() * 2.0f - 1.0f;

我唯一的想法是在音频线程上分配新内存会导致错误,但我已经看到在其他在线资源中完成声明和分配,甚至在我完全相同的函数中使用 numChannels、numSamples、channel 和 sample 都分配和分配得很好。我还认为它与使用 Random 类有关,但即使它生成正弦波,我也会遇到同样的问题。

编辑:这是从项目复制的确切代码。这里 nextSample 是全局声明的,因为在本地声明缓冲区时缓冲区不会被填充

  void MainContentComponent::getNextAudioBlock (const AudioSourceChannelInfo& bufferToFill)
  {
    const int numChannels = bufferToFill.buffer->getNumChannels();
    const int numSamples = bufferToFill.numSamples;
    for (int channel = 0; channel < numChannels; channel++){
        float* const buffer = bufferToFill.buffer -> getWritePointer (channel, bufferToFill.startSample);
        for (int sample; sample < numSamples; sample++){
            // nextSample = (randomGen.nextFloat() * 2.0f - 1.0f); // For Randomly generated White Noise
            nextSample = (float) std::sin (currentAngle);
            currentAngle += angleDelta;
            buffer[sample] = nextSample * volumeLevel;
        }
    }
  }
4

1 回答 1

1

我在 Projucer 中创建了一个新的 AudioApplication 项目,并将这段代码粘贴到getNextAudioBlock()方法中(在您在此处引用它们时添加合理的成员变量)。

编译器立即指出了问题——sample下面的循环变量没有初始化(并且 C++ 不会默认为你初始化它),所以如果该变量使用的内存恰好包含一个小于缓冲区的值大小,您将生成一些音频;如果不是,则传递给此函数的缓冲区不受影响,因为循环永远不会运行。

    for (int sample; sample < numSamples; sample++){
        nextSample = (randomGen.nextFloat() * 2.0f - 1.0f); // For Randomly generated White Noise
        //nextSample = (float) std::sin (currentAngle);
        //currentAngle += angleDelta;
        buffer[sample] = nextSample * volumeLevel;
    }

看看是否将其更改为for (int sample=0;不会为您解决问题。

于 2017-08-24T01:00:53.327 回答