Skip to content

Commit 83297b9

Browse files
authored
Merge pull request #719 from KevinGrajeda/noise-docs
add example and documentation to Noise
2 parents 62226f2 + 7b2fc72 commit 83297b9

File tree

1 file changed

+90
-1
lines changed

1 file changed

+90
-1
lines changed

src/noise.js

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,69 @@ const _brownNoiseBuffer = (function () {
7070
* @constructor
7171
* @param {String} type Type of noise can be 'white' (default),
7272
* 'brown' or 'pink'.
73+
* @example
74+
* <div><code>
75+
* let noise,fft;
76+
* let playing = false;
77+
*
78+
* function setup() {
79+
* let cnv = createCanvas(100, 100);
80+
* cnv.mousePressed(playNoise);
81+
* cnv.mouseReleased(stopNoise);
82+
* noStroke();
83+
* fill(255,0,255);
84+
*
85+
* // Create a new p5.Noise object
86+
* noise = new p5.Noise();
87+
* // Create a new p5.FFT object
88+
* fft = new p5.FFT();
89+
* }
90+
*
91+
* function draw() {
92+
* background(220);
93+
* // Display the current type of noise or "Tap to play"
94+
* textAlign(CENTER, CENTER);
95+
* if (playing) {
96+
* text('Noise type: '+noise.getType(), width / 2, 20);
97+
* } else {
98+
* text('Tap to play', width / 2, 20);
99+
* }
100+
* drawSpectrum();
101+
* }
102+
*
103+
* function playNoise() {
104+
* noise.start();
105+
* playing = true;
106+
* }
107+
*
108+
* function stopNoise() {
109+
* noise.stop();
110+
* playing = false;
111+
*
112+
* // Change the type of noise
113+
* if (noise.getType() === 'white') {
114+
* noise.setType('pink');
115+
* } else if (noise.getType() === 'pink'){
116+
* noise.setType('brown');
117+
* } else {
118+
* noise.setType('white');
119+
* }
120+
* }
121+
*
122+
* function drawSpectrum() {
123+
* // Get and draw the frequency spectrum of the noise
124+
* let spectrum = fft.analyze();
125+
* beginShape();
126+
* vertex(0, height);
127+
* for (let i = 0; i < spectrum.length; i++) {
128+
* let x = map(i, 0, spectrum.length, 0, width);
129+
* let h = map(spectrum[i], 0, 255, height, 0);
130+
* vertex(x, h);
131+
* }
132+
* vertex(width, height);
133+
* endShape();
134+
* }
135+
* </code> </div>
73136
*/
74137
class Noise extends Oscillator {
75138
constructor(type) {
@@ -94,7 +157,7 @@ class Noise extends Oscillator {
94157
* White is the default.
95158
*
96159
* @method setType
97-
* @param {String} [type] 'white', 'pink' or 'brown'
160+
* @param {String} type 'white', 'pink' or 'brown'
98161
*/
99162
setType(type) {
100163
switch (type) {
@@ -117,9 +180,23 @@ class Noise extends Oscillator {
117180
}
118181
}
119182

183+
/**
184+
* Returns current type of noise eg. 'white', 'pink' or 'brown'.
185+
*
186+
* @method getType
187+
* @for p5.Noise
188+
* @returns {String} type of noise eg. 'white', 'pink' or 'brown'.
189+
*/
120190
getType() {
121191
return this.buffer.type;
122192
}
193+
194+
/**
195+
* Starts playing the noise.
196+
*
197+
* @method start
198+
* @for p5.Noise
199+
*/
123200
start() {
124201
if (this.started) {
125202
this.stop();
@@ -133,6 +210,12 @@ class Noise extends Oscillator {
133210
this.started = true;
134211
}
135212

213+
/**
214+
* Stops playing the noise.
215+
*
216+
* @method stop
217+
* @for p5.Noise
218+
*/
136219
stop() {
137220
var now = p5sound.audiocontext.currentTime;
138221
if (this.noise) {
@@ -141,6 +224,12 @@ class Noise extends Oscillator {
141224
}
142225
}
143226

227+
/**
228+
* Get rid of the Noise object and free up its resources / memory.
229+
*
230+
* @method dispose
231+
* @for p5.Noise
232+
*/
144233
dispose() {
145234
var now = p5sound.audiocontext.currentTime;
146235

0 commit comments

Comments
 (0)