# Manual: Noise

The two classes in Minim that can be used to generate noise are `WhiteNoise` and `PinkNoise`. White noise is a random signal (or process) with a flat power spectral density. In other words, the signal’s power spectral density has equal power in any band, at any centre frequency, having a given bandwidth. White noise is considered analogous to white light which contains all frequencies. Pink noise or 1/f noise is a signal or process with a frequency spectrum such that the power spectral density is proportional to the reciprocal of the frequency. In layman’s terms, white noise sounds brighter than pink noise.

```// Constructs a white noise generator with an amplitude of 1.
WhiteNoise()
// Constructs a white noise generator with the given amplitude.
WhiteNoise(float amp)
// Constructs a pink noise signal with an amplitude of 1.
PinkNoise()
// Constructs a pink noise signal with an amplitude of amp.
PinkNoise(float amp)```

White noise and pink noise can be created with the default amplitude of 1, or you can specify an amplitude. You can then manipulate the noise with the following methods:

```// Sets the amplitude of the signal to a.
void setAmp(float a)
// Sets the pan of the signal to p.
void setPan(float p)
// Fills signal with values in the range of [-1, 1].
void generate(float[] signal)
// Fills left and right with values in the range of [-1, 1].
void generate(float[] left, float[] right)```

The amplitude can be set between 0 and 1, where 0 is silent and 1 is full volume. The noise classes are MONO so they can be panned. The pan values can be between -1 and 1, where -1 is “hard left” and 0 is “hard right”. Note that this is different than setting the volume and pan controls of the `AudioOutput` that you add the noise signals to. You might construct a white noise object with an amplitude of 0.7 and then add it to an output whose volume is set to 0.9, resulting in a final amplitude of 0.63.

The `generate` methods are required by the `AudioSignal` interface and will generally be called by the `AudioOutput` that you add the noise to. However, you can also call them yourself if you need to generate noise to write to a file or wherever.

Code Sample (online example)

```/**
* This sketch demonstrates the difference between pink noise and white noise.<br />
* Move the mouse up and down to change the amplitude of the noise, left and right to change the panning.<br />
* Type 'w' to toggle between white noise and pink noise. The sketch starts playing pink noise.
*/

import ddf.minim.*;
import ddf.minim.signals.*;

Minim minim;
AudioOutput out;
PinkNoise pn;
WhiteNoise wn;
boolean useWhite;

void setup()
{
size(512, 200);

minim = new Minim(this);
out = minim.getLineOut();
// make a pink noise signal with an amplitude of 0.5
pn = new PinkNoise(0.5);
// make a white noise signal with an amplitude of 0.5
wn = new WhiteNoise(0.5);
// add the pink noise signal to the output
// add the white noise signal to the output
// and disable it
out.disableSignal(wn);
// boolean used to toggle between pink and white noise
useWhite = false;

textFont(createFont("Arial", 12));
}

void draw()
{
background(0);
stroke(255);
// draw the waveforms
for(int i = 0; i < out.bufferSize()-1; i++)
{
float x1 = map(i, 0, out.bufferSize(), 0, width);
float x2 = map(i+1, 0, out.bufferSize(), 0, width);
line(x1, 50 + out.left.get(i)*50, x2, 50 + out.left.get(i+1)*50);
line(x1, 150 + out.right.get(i)*50, x2, 150 + out.right.get(i+1)*50);
}
// draw 0 lines
stroke(255, 0, 0);
line(0, 50, width, 50);
line(0, 150, width, 150);

if ( out.isEnabled(wn) )
{
text("White noise.", 5, 15);
}
else
{
text("Pink noise.", 5, 15);
}
}

void mouseMoved()
{
float amp = map(mouseY, 0, height, 1, 0);
float pan = map(mouseX, 0, width, -1, 1);
wn.setAmp(amp);
wn.setPan(pan);
pn.setAmp(amp);
pn.setPan(pan);
}

void keyReleased()
{
// here you can see how it is possible to interactively add and remove signals from an AudioOutput
if ( key == 'w' )
{
useWhite = !useWhite;
println("Use white: " + useWhite);
if ( useWhite )
{
out.disableSignal(pn);
out.enableSignal(wn);
}
else
{
out.disableSignal(wn);
out.enableSignal(pn);
}
}
}

void stop()
{
// always close Minim audio classes when you are done with them
out.close();
// always stop Minim before exiting
minim.stop();

super.stop();
}```

## 2 thoughts on “Manual: Noise”

1. Dear DDF,

Thank you for minim, it’s very nice for teaching the basics of computer-based sound.

It would be really useful if AudioSignal implemented Effectable. It’s pretty common to want to run WhiteNoise or a SquareWave through a BandPass filter, for example. But as far as I can tell that’s not currently possible since AudioSignals are not Effectable.

best,
douglas