@@ -70,6 +70,69 @@ const _brownNoiseBuffer = (function () {
70
70
* @constructor
71
71
* @param {String } type Type of noise can be 'white' (default),
72
72
* '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>
73
136
*/
74
137
class Noise extends Oscillator {
75
138
constructor ( type ) {
@@ -94,7 +157,7 @@ class Noise extends Oscillator {
94
157
* White is the default.
95
158
*
96
159
* @method setType
97
- * @param {String } [ type] 'white', 'pink' or 'brown'
160
+ * @param {String } type 'white', 'pink' or 'brown'
98
161
*/
99
162
setType ( type ) {
100
163
switch ( type ) {
@@ -117,9 +180,23 @@ class Noise extends Oscillator {
117
180
}
118
181
}
119
182
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
+ */
120
190
getType ( ) {
121
191
return this . buffer . type ;
122
192
}
193
+
194
+ /**
195
+ * Starts playing the noise.
196
+ *
197
+ * @method start
198
+ * @for p5.Noise
199
+ */
123
200
start ( ) {
124
201
if ( this . started ) {
125
202
this . stop ( ) ;
@@ -133,6 +210,12 @@ class Noise extends Oscillator {
133
210
this . started = true ;
134
211
}
135
212
213
+ /**
214
+ * Stops playing the noise.
215
+ *
216
+ * @method stop
217
+ * @for p5.Noise
218
+ */
136
219
stop ( ) {
137
220
var now = p5sound . audiocontext . currentTime ;
138
221
if ( this . noise ) {
@@ -141,6 +224,12 @@ class Noise extends Oscillator {
141
224
}
142
225
}
143
226
227
+ /**
228
+ * Get rid of the Noise object and free up its resources / memory.
229
+ *
230
+ * @method dispose
231
+ * @for p5.Noise
232
+ */
144
233
dispose ( ) {
145
234
var now = p5sound . audiocontext . currentTime ;
146
235
0 commit comments