Audio encoding question

Hi All,

If I have understood correctly the processBlock function gets an AudioBuffer that has a number of samples for each channell.
If the sample rate is 48k, then there are 48k samples per second. It seems the samples have a gain value.

All well and good if I want to change gain, add distortion etc.

But how do I get to the actual sample ‘gain’ value at a particular frequency or frequency range for a given sample snapshot?

I get the feeling I don’t understand how the audio is encoded :slight_smile:

A sample has an amplitude, which is a float number between 1.0 and -1.0. During processing it is allowed to exceed this range, but when handed over, it is expected to stay below that limits.

An audio buffer is merely a wrapper around an array (not literally an array, can be a vector, raw memory, etc.) of float numbers, so it is accessed via a float* (float pointer).

An example is already in the boilerplate, if you look at the processBlock:

    // This is the place where you'd normally do the guts of your plugin's
    // audio processing...
    // Make sure to reset the state if your inner loop is processing
    // the samples and the outer loop is handling the channels.
    // Alternatively, you can process the samples with the channels
    // interleaved by keeping the same state.
    for (int channel = 0; channel < totalNumInputChannels; ++channel)
    {
        auto* channelData = buffer.getWritePointer (channel);

        // ..do something to the data...
    }

channelData is now a pointer to your audio, each sample is one amplitude.

e.g.

    for (int channel = 0; channel < totalNumInputChannels; ++channel)
    {
        auto* channelData = buffer.getWritePointer (channel);
        for (int i = 0; i < buffer.getNumSamples(); ++i)
            channelData [i] *= 0.5;  // halves the amplitude (apply a gain of 0.5)
    }

Have a look into the tutorials

Good luck

1 Like

Audio is always sampled over time, that means if you record a signal from e.g. a microphone at 48kHz you take 48k “snapshots” of the signal state per second. So you could calculate a timestamp for each sample that maps the time when this sample was recorded to each sample value. This is sometimes called the time-domain.

The information on which frequencies are actually contained in the signal are not directly stored into the audio buffer. If you need this information you need to compute it by transforming the time-domain data into the frequency-domain. To do this, you usually use the Fourier Transform. As the Fourier Transform is computationally a bit heavy, some smart people found out that if you compute such a transform on buffer sizes of 2^n with n being an integer number, it can be optimized quite excessive. Algorithms that do this are called Fast Fourier Transforms or short FFT. Juce has a ready-to-use FFT class in the DSP module.

However if you want to manipulate the signal in the frequency domain like boosting or cutting some frequencies you don’t necessarily need to compute the FFT to do so. For simple equalizer-like applications you compute all you need directly in the time domain with so called IIR and FIR filters (also contained in the DSP module).

This should just give you a light overlook about the topic, if you plan to use any of those you are better off having a detailed read on all those topics, however this might give you some terms to google for :slight_smile:

1 Like

It’s analogous (no pun intended) to the waveform the speakers end up reproducing and what reaches your ears. The only information you can easily get and manipulate in code from the waveform is the amplitude values. It gets much more complicated very quickly when you want to do more complicated things. But for example standard filters/equalisers don’t need anything else to work but manipulations of the amplitude values.

Thank you both for the speedy and helpful responses.

I had completely misunderstood how signals were sampled.

I now understand and I see what I need to do.

For my own use, and maybe commercially -depending on how fugly I make it :smiley: - I want to create some vibey limiting. Should be fun!