Tutorial: Simple synth (noise)


#1

I am happily working through the JUCE tutorials and have a quick question about a bit of code. This is probably a C++ thing that I need to learn (I am coming from java).

In the code below, buffer is set up as a pointer (to a location/offset in bufferToFill?) but is then used as the name of an array in the for loop that creates to noise data. Is buffer a pointer or an array … or both? It compiles and runs OK so I can use as a template, but I would just like to understand how this bit of code works.

Thanks

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;
    }
}

#2

yes, it is both. It is a pointer which has writing access to the audio buffer. In cpp pointers can be arrays.

ex: pointer[0] would be the first position in the array pointer. Might see it instantiated like:

float *pointer[100]

I think if you don’t specify an index it might just return the first position in the array.


#3

The compiler interprets buffer[sample] as a shorthand for

*(buffer + sample * sizeof(float))

Google for “C pointers vs arrays” and try to understand the distinction between the two. This is one of the key concepts in C/C++.


#4

Great, thanks for your fast replies! I will go and google now :grinning: