Last active
November 7, 2020 20:08
-
-
Save nickgs/202550f47397af1d840e14fb534d5f17 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let value = 0; | |
let pad1; | |
let pad2; | |
function setup() { | |
createCanvas(windowWidth, windowHeight); | |
background("black"); | |
pad1 = new Pad(10, 100, 232); | |
pad2 = new Pad(110, 100, 400); | |
} | |
function draw() { | |
background("black"); | |
pad1.draw(); | |
pad2.draw(); | |
} | |
function keyPressed() { | |
if(keyCode == 68) { // the D key | |
pad1.play(); | |
} | |
if(keyCode == 70) { // the D key | |
pad2.play(); | |
} | |
} | |
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.start(); | |
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