# Pink noise generator

Hi everyone,
In the tutorial:Tutorial: Build a white noise generator, it generates white noise signal in real time.How could generate pink noise signal in real time in this way?

`````` void getNextAudioBlock (const AudioSourceChannelInfo& bufferToFill) override
{
for (auto channel = 0; channel < bufferToFill.buffer->getNumChannels(); ++channel)
{
// Get a pointer to the start sample in the buffer for this audio output channel
auto* buffer = bufferToFill.buffer->getWritePointer (channel, bufferToFill.startSample);

// Fill the required number of samples with noise between -0.125 and +0.125
for (auto sample = 0; sample < bufferToFill.numSamples; ++sample)
buffer[sample] = random.nextFloat() * 0.25f - 0.125f;
}
``````

}

Best regards!

there are several different methods.

you can approximate a -3db/octave â€śpinking filterâ€ť using an FIR, as shown in this snippet posted by paul kellet on musicdsp.org

``````Filter to make pink noise from white  (updated March 2000)
------------------------------------

This is an approximation to a -10dB/decade filter using a weighted sum
of first order filters. It is accurate to within +/-0.05dB above 9.2Hz
(44100Hz sampling rate). Unity gain is at Nyquist, but can be adjusted
by scaling the numbers at the end of each line.

If 'white' consists of uniform random numbers, such as those generated
by the rand() function, 'pink' will have an almost gaussian level
distribution.

b0 = 0.99886 * b0 + white * 0.0555179;
b1 = 0.99332 * b1 + white * 0.0750759;
b2 = 0.96900 * b2 + white * 0.1538520;
b3 = 0.86650 * b3 + white * 0.3104856;
b4 = 0.55000 * b4 + white * 0.5329522;
b5 = -0.7616 * b5 - white * 0.0168980;
pink = b0 + b1 + b2 + b3 + b4 + b5 + b6 + white * 0.5362;
b6 = white * 0.115926;

An 'economy' version with accuracy of +/-0.5dB is also available.

b0 = 0.99765 * b0 + white * 0.0990460;
b1 = 0.96300 * b1 + white * 0.2965164;
b2 = 0.57000 * b2 + white * 1.0526913;
pink = b0 + b1 + b2 + white * 0.1848;

---
paul.kellett@maxim.abel.co.uk
http://www.abel.co.uk/~maxim/

``````

and there is the â€śvossâ€ť or â€śgardner/vossâ€ť or â€śvoss/mccartneyâ€ť algorithm, which involves summing white noise generated at successively lower octaves. (mccartneyâ€™s innovation was to stagger the calculation of each octave, making the CPU load constant.) Robin Whittle has a good overview of these methods here.

this method from stefan stenzel innovates on the white-noise-octaves structure by upsampling the octaves with linear interpolation, in a highly efficient implementation. to my ears, the results are excellent. (downside is that it is a little harder to grok what is actually going on - at least for me.)

2 Likes

Thank you !!!

Any specific reason you properly capitalize all names but mine ?

Anyway, I once wrote a description that is still online:
http://stenzel.waldorfmusic.de

Only the Web Audio is so outdated that it is no longer working.

But seems you are not targeted, Paul Kellet faces the same fateâ€¦

1 Like

nope! here, iâ€™ve updated the post for consistency.

(i think i added caps because â€śmccartneyâ€ť looked strange otherwise.)

anyways, many thanks for sharing your algorithm. my comment about the opacity of the implementation was more about the heavy use of bitwise operations on floats, macros, that sort of thing; not a criticism of the writeup which is very clear!

It might be easier to first look at the integer version without float hacks, basically just 10 lines of code where it all happens.

1 Like