Audio Processing Basics (part 1)

This is the first post where I try to write something about audio processing in C++. This will be on a very basic level and I will add some links below if you want to know more.

Digital Signal Processing, DSP, is when you have a digital signal and manipulates it mathematically. The digital signal can be a lot of things: audio, voice, video, temperature, etc. The signal can come from an analog source that has been converted to digital, or it can be generated, synthesized.

Audio data format

When it comes to audio processing, most common is that the audio data is formatted to be in the -1.0 to +1.0 range. If you start coding plugins etc., you will see that the data is in that range and the data buffers will be arrays of floats or doubles. If the bit depth is, for example, 16 bit per sample, each sample will be in the range of 32767 and -32768. This is easily converted to a fractional value in the -1.0 to +1.0 range. In an N-bit audio system, where N is the bit depth (for example 16):

\(fraction= \frac{integer}{2^N}\)

Audio data manipulation, some examples

The normalized audio data can easily be manipulated. Here are two examples.

To increase/decrease gain of audio data, just multiply the samples with a gain factor.

for (int i = 0; i < numberOfSamplesInInputBuffer; i++)
{
    // Get input sample
    float sample = inputBuffer[i];        
    // Lower the gain and copy it to the output buffer
    float gain = 0.5;
    outputBuffer[i] = gain * sample;
}

To mix two audio data signals, add the samples together:

for (int i = 0; i < numberOfSamplesInInputBuffers; i++)
{
    // Mix contributions from two sources
    float outSample = 0.0;
    outSample += inputBufferOne[i];
    outSample += inputBufferTwo[i];
    // To change gain, just multiply gain factor with 
    // the samples, example: 
    // outSample += 0.5 * inputBufferOne[i];

    // Write the mix to the output buffer
    outputBuffer[i] = outSample;
}

Resources

To get some background, I can recommend taking a look at The Scientist and Engineer’s Guide to Digital Signal Processing by Steven W. Smith, Ph.D.

At Musicdsp.org there is a collection of algorithms, thoughts, ideas and code snippets.

The Synthesis ToolKit in C++ (STK) is a set of open source audio signal processing and algorithmic synthesis classes written in C++. Source code also available at GitHub.

Comments are closed.