Skip to content

Instantly share code, notes, and snippets.

@nickgs
Last active May 5, 2020 14:34
Show Gist options
  • Save nickgs/46a112cb6adbbec9f219e00f83b7033f to your computer and use it in GitHub Desktop.
Save nickgs/46a112cb6adbbec9f219e00f83b7033f to your computer and use it in GitHub Desktop.
Soundboard Pad
// Class that defines an object that can play a simple sound given a frequency.
class Pad {
constructor(x, y, freq) {
this.x = x;
this.y = y;
this.width = 100;
this.height = 100;
this.freq = freq;
// Oscillator, Envelope, Analyzer
this.osc = new p5.Oscillator();
this.osc.amp(0);
this.osc.setType('sine');
this.osc.start();
this.env = new p5.Env();
// attackTime, decayTime, sustainRatio, releaseTime
this.env.setADSR(0.001, 0.1, 0.2, 0.1);
this.env.setRange(3, 0);
this.analyzer = new p5.Amplitude();
this.analyzer.setInput(this.env);
}
draw() {
let level = this.analyzer.getLevel();
let levelHeight = map(level, 0, 0.4, 0, this.height)
rect(this.x, this.y, this.width, -levelHeight);
}
play() {
this.osc.freq(this.freq);
this.env.play(this.osc);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment