Audio Processing Basics (part 2) – Generate a Tone

Time for some code. We will create a tone by sampling a sine wave. This is not the most efficient way to generate a sine wave, since we are calculating sine for each sampling point, but it works and is simple.

A sine wave as a function of time (t):

\(
y(t) = A sin(2{\pi}ft+\varphi)
\)
where:
\(A\), amplitude
\(f\), frequency (Hertz)
\(\varphi\), phase

The formula to get the sine wave samples looks like this. We assume that phase is 0, so we ignore it.

\(
y_i = Asin(\frac{2{\pi}fi}{f_s})
\)
where:
\(A\), amplitude
\(i\), sampling point
\(f\), frequency (Hertz)
\(f_s\), sample rate (samples/second)

Here is a function that generates the sine wave an writes the samples to a float* buffer. If we want some harmonics, we can double and triple etc. the frequency and add it while lowering the amplitude (it’s in comments in the code below).

#define _USE_MATH_DEFINES
#include <cmath>

// Create a sine tone
// buf - array to write samples to
// frequency - in Hertz, for example 440
// numFrames - number of samples, also minimum size of buffer
// sampleRate - in samples/second, for example 44100

void createTone(float* buf, int frequency, int numFrames, int sampleRate)
{
    for (int i = 0; i < numFrames; i++)
    {
        float sample = 0.0;
        // Generate a sample value. Samples are normalized [-1.0, 1.0]
        // Multiplication with 0.30 descreases the amplitude      
        sample += 0.3 * sin(2 * M_PI * i * frequency / sampleRate);

        // Here we can add som harmonics if we want.
        // Double the frequency
        //sample += 0.25 * sin(2 * M_PI * i * 2 * frequency / sampleRate);
        // Triple the frequency
        //sample += 0.20 * sin(2 * M_PI * i * 3 * frequency / sampleRate);
        // Etc.
        //sample += 0.15 * sin(2 * M_PI * i * 4 * frequency / sampleRate);
        //sample += 0.10 * sin(2 * M_PI * i * 5 * frequency / sampleRate);
         
        // Write sample to the buffer
        *buf++ = sample; 
    }
}

I have some working code that generates a sine wave and writes it to a WAV file. Check my GitHub account tgranat in the repository DSPbasics.

I you haven’t, check Part 1 with some C++ code snippets to manipulate audio data.

Comments are closed.