@@ -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 */
2484class Compressor extends Effect {
2585 constructor ( ) {
0 commit comments