Buffer size - getNumSamples


#1

Hello,
I am sorry for stupid question but I am beginner and sometimes it’s still difficult for me to find help in https://juce.com/doc/classes
It’s also question in music technology at all.
getNumSamples returns for me 512. I understand it means in the buffer there are 512 samples. But I can’t find the answer how long (in time) is that buffer? I mean, what is actual sample rate of that buffer? How many samples it generates in one second? Is there any command that I can easy check it or even change?
For any help thanks in advance


#2

The AudioBuffer class doesn’t deal with sample rate. The sample rate is determined elsewhere. If you are doing a plugin you can call getSampleRate in the AudioProcessor::processBlock method. (Apparently it’s not reliable to be called from other methods…)

If you are doing a stand alone application, you can get the sample rate from the current AudioIODevice your AudioDeviceManager is using.


#3

Hey,
great thanks for your reply.
Actually I’ve just made that tutorial: https://www.youtube.com/watch?v=GjNeYI6-uNE&feature=youtu.be

And guy generates simple noise with that code:

void getNextAudioBlock (const AudioSourceChannelInfo& bufferToFill) override
{
    for (int channel = 0; channel < bufferToFill.buffer->getNumChannels(); ++channel)
    {
        float* const buffer = bufferToFill.buffer->getWritePointer(channel, bufferToFill.startSample);
        for (int sample = 0; sample < bufferToFill.numSamples; ++sample)
        {
            buffer[sample] = ((random.nextFloat() * 2) - 1) * 0.25f;
        }
    }
}

And now I made a challange for me, to replace buffer[sample] random function with simple sine wave function. So I think it should be something like that:

buffer[sample] = sin((sample / XXX) * 3.14) * 0.25f;

And now I wonder what should be XXX to get for example 60 Hz wave. But to solve that I need to know what is the frequency of those 512 bufferToFill.numSamples. Am I right or not? If yes, how to find that frequency (sample rate)?

Best Regards


#4

Did you check out the example plug-in?

Rail


#5

The sample rate is set in prepareToPlay if you need to set it anywhere for any classes inside your plug-in.

Otherwise you can just call getSampleRate()

The calculation would be

double step = F / getSampleRate();
counter += step; 
float y = sin (counter * 2.0 * double_Pi); 

if (counter >= ) 
    counter -= 1.0; 

Where counter is a member variable initialized to zero. You need to use a counter instead of sample number or else you will get pops in between buffers as the phase isn’t guaranteed to be preserved.


#6

Ok,
but I can’t just call getSampleRate()

When I execute something like that:
std::cout << getSampleRate() << std::endl;

I get error: Use of undeclared identifier ‘getSampleRate’

So maybe I should #include anything in the header?


#7

getSampleRate() is a method of AudioProcessor.
In C++ (any OO language anyway) you need the object to call a method.

It is not a matter of including something, it means, you can call it inside your processBlock() easily, but no point to call it e.g. in MainComponent or anything similar…