Skip to content

Commit 86e246f

Browse files
committed
add example to noise
1 parent c17425a commit 86e246f

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

src/noise.js

Lines changed: 63 additions & 0 deletions
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) {

0 commit comments

Comments
 (0)