C++ 局部变量的赋值导致音频在JUCE中停止处理

C++ 局部变量的赋值导致音频在JUCE中停止处理,c++,audio,variable-assignment,local-variables,juce,C++,Audio,Variable Assignment,Local Variables,Juce,EDIT:这是一个未初始化的变量,造成了混乱的行为。请参阅关于获取更多JUCE编译器警告的信息 我试图创建一个基本的合成器,当我仅仅试图给一个新声明的变量赋值时,我很快就遇到了一个荒谬的问题。 在跟随JUCE simple sine合成教程之后,我遇到了这个问题。这是我的getNextAudioBlock()函数产生白噪声时的基本代码。请注意如何在整个过程中声明和分配四个整数: const int numChannels = bufferToFill.buffer->getNumChann

EDIT:这是一个未初始化的变量,造成了混乱的行为。请参阅关于获取更多JUCE编译器警告的信息

我试图创建一个基本的合成器,当我仅仅试图给一个新声明的变量赋值时,我很快就遇到了一个荒谬的问题。 在跟随JUCE simple sine合成教程之后,我遇到了这个问题。这是我的
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);
    }
}
这项工作:

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;
我唯一的想法是在音频线程上分配新内存会导致错误,但我在其他在线资源中看到过声明和分配,甚至在我的NumChannel、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;
        }
    }
  }
void MainContentComponent::getNextAudioBlock(const AudioSourceChannelInfo&bufferToFill)
{
const int numChannels=bufferToFill.buffer->getNumChannels();
const int numSamples=bufferToFill.numSamples;
对于(int通道=0;通道getWritePointer(通道,bufferToFill.startSample);
对于(int-sample;sample
我在Projucer中创建了一个新的AudioApplication项目,并将这段代码粘贴到
getNextAudioBlock()
方法中(在此处引用时添加合理的成员变量)

编译器立即指出问题:循环变量<代码>样本< /C> >未初始化(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;sample

查看将其更改为(int-sample=0;
for(int-sample=0;numChannels;channel++)的
for(int-channel=0;numChannels;channel++)中的条件
for(int-channel=0;numChannels;channel++)
是否有意为您解决问题。

可能不是问题的解决方案,但是
中的条件
numChannels
是否有意为(int-channel=0;numChannels(…;channelbuffer[sample]=(randomGen.nextFloat()*2.0f-1.0f;
甚至不会编译,因为缺少关闭
。或者实际提供。我添加了确切的代码,并链接到FirstSynth和AdditiveManual,这两个项目只有在变量为全局变量时才起作用members@brenthompson2--如果您是付费许可证持有人,并且live build不适合您,那么您应该在JUCE论坛上发布,该论坛由JUCE开发团队监控。谢谢您非常好。这很有意义。我明天早上会试试。你用的是什么编译器?Xcode 8,但我希望这里的行为在任何地方都是不可预测的——看看这个答案:这就是解决方案。它还修复了我的问题,即音频没有同时输出到两个通道。现在如果可以的话