Skip to content

Commit aeba38a

Browse files
authored
Merge pull request #729 from KevinGrajeda/compressor-example
add compressor example
2 parents 5b2bf5c + 4ce5785 commit aeba38a

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

src/compressor.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,68 @@ import Effect from './effect';
1818
* @class p5.Compressor
1919
* @constructor
2020
* @extends p5.Effect
21+
* @example
22+
* <div><code>
23+
* let sound, compressor, playing;
2124
*
25+
* function preload() {
26+
* sound = loadSound('assets/beat.mp3');
27+
* }
2228
*
29+
* function setup() {
30+
* let cnv = createCanvas(100, 100);
31+
* cnv.mouseClicked(togglePlay);
32+
* sound.disconnect();
33+
* compressor = new p5.Compressor();
34+
* compressor.process(sound);
35+
*
36+
* textAlign(CENTER, CENTER);
37+
* fft = new p5.FFT();
38+
* }
39+
*
40+
* function draw() {
41+
* background(220);
42+
* // Constrain mouse Y position between 0 and -100
43+
* let threshold = -constrain(mouseY, 0, 100);
44+
* compressor.threshold(threshold);
45+
*
46+
* // Draw a rectangle based on the compressor reduction
47+
* fill(255, 0, 255, 70);
48+
* rect(0, 0, width, -compressor.reduction());
49+
*
50+
* fill(0);
51+
* if (playing) {
52+
* text('Threshold: ' + round(threshold), width / 2, 20);
53+
* } else {
54+
* text('Tap to play', width / 2, 20);
55+
* }
56+
* // Draw a line to indicate the threshold
57+
* stroke(0);
58+
* line(0, mouseY, width, mouseY);
59+
* drawSpectrum();
60+
* }
61+
*
62+
* function togglePlay() {
63+
* if (playing) {
64+
* playing = false;
65+
* sound.pause();
66+
* } else {
67+
* playing = true;
68+
* sound.loop();
69+
* }
70+
* }
71+
*
72+
* function drawSpectrum() {
73+
* let spectrum = fft.analyze();
74+
* noStroke();
75+
* fill(255, 0, 255);
76+
* for (let i = 0; i < spectrum.length; i++){
77+
* let x = map(i, 0, spectrum.length, 0, width);
78+
* let h = -height + map(spectrum[i], 0, 255, height, 0);
79+
* rect(x, height, width / spectrum.length, h);
80+
* }
81+
* }
82+
* </code></div>
2383
*/
2484
class Compressor extends Effect {
2585
constructor() {

0 commit comments

Comments
 (0)