Audio Processing Basics (part 3) – Making noise

Here is an example how to generate (kind of) white noise. White noise is a random signal having equal intensity at different frequencies. I write “kind of”, since this noise isn’t randomly generated. But it’s good enough in most cases when you only need a white noise sound. I’ve taken the algorithm from the Fast Whitenoise Generator at musicdsp.org.

The following function creates noise and writes it to a float* array:

// Create pseudorandom noise
// Algorithm from https://www.musicdsp.org/en/latest/Synthesis/216-fast-whitenoise-generator.html
// buf - array to write samples to
// numFrames - number of samples, also minimum size of buffer

void createNoise(float* buf, int numFrames, float level = 0.30f)
{
    float scale = 2.0f / 0xffffffff;
    int x1 = 0x67452301;
    int x2 = 0xefcdab89;
    level *= scale;

    while (numFrames--)
    {
        x1 ^= x2;
        *buf++ = x2 * level;
        x2 += x1;
     }
}

I have some working code that runs this function and writes it to a WAV file. Check my GitHub account tgranat in the repository DSPbasics.

If you haven’t, check Part 2 where I create a sine wave tone.

Comments are closed.