void getNextAudioBlock (const AudioSourceChannelInfo& bufferToFill) override
{
for (int channel = 0; channel < bufferToFill.buffer->getNumChannels(); ++channel)
{
// Get a pointer to the start sample in the buffer for this audio output channel
float* const buffer = bufferToFill.buffer->getWritePointer (channel, bufferToFill.startSample);
// Fill the required number of samples with noise betweem -0.125 and +0.125
for (int sample = 0; sample < bufferToFill.numSamples; ++sample)
buffer[sample] = random.nextFloat() * 0.25f - 0.125f;
}
}
xcode was complaining about random.nextFloat() with the error message:
Member reference base type ‘long ()’ is not a structure or union
Adding this line right before the for statement fixed the issue (and caused the application window to open and output white noise when ran in xcode).
Random random (0);
I’m very new to C++, so let me know if this doesn’t make sense.
If you initialize random locally, it will always output the same data for each block. You can use it without initialisation in which case it will automatically create a random seed:
void getNextAudioBlock (const AudioSourceChannelInfo& bufferToFill) override
{
Random random;
for (int channel = 0; channel < bufferToFill.buffer->getNumChannels(); ++channel)
[...]
}
Even better would be to declare random as class member:
…which is exactly what the code in the tutorial has:
...
void getNextAudioBlock (const AudioSourceChannelInfo& bufferToFill) override
{
for (int channel = 0; channel < bufferToFill.buffer->getNumChannels(); ++channel)
{
// Get a pointer to the start sample in the buffer for this audio output channel
float* const buffer = bufferToFill.buffer->getWritePointer (channel, bufferToFill.startSample);
// Fill the required number of samples with noise betweem -0.125 and +0.125
for (int sample = 0; sample < bufferToFill.numSamples; ++sample)
buffer[sample] = random.nextFloat() * 0.25f - 0.125f;
}
}
private:
Random random; ///<<<<<---- !!!
...
Ah–missed the downloadable zip! I was building the code up in my own project, using the blocks provided in the body of the tutorial. I’ve amended the thread title to state this was resolved, and will update the first post with an indication, thanks!