Last active
April 8, 2019 02:24
-
-
Save nickgs/03b54ba6ad690f3f61a9425f5bae0644 to your computer and use it in GitHub Desktop.
Soundboard from class on 04/3/19
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 osc, env, analyzer; | |
let notes = [220, 330, 120]; | |
let pad_size = 100; | |
let pad1; | |
let pad2; | |
function setup() { | |
createCanvas(710, 200); | |
pad1 = new Pad(220, 100, 100); | |
pad2 = new Pad(330, 200, 200); | |
} | |
function draw() { | |
background("black"); | |
pad1.draw(); | |
pad2.draw(); | |
} | |
function keyPressed() { | |
//Q key | |
if(keyCode == 81) { | |
pad1.play(); | |
} | |
if(keyCode == 87) { | |
pad2.play(); | |
} | |
} | |
// Define our pad. | |
class Pad { | |
constructor(_freq, _x, _y) { | |
this.freq = _freq; | |
this.x = _x; | |
this.y = _y; | |
this.osc = new p5.Oscillator(); | |
this.osc.amp(0); | |
this.osc.start(); | |
this.osc.setType('sine'); | |
this.width = pad_size; | |
this.height = pad_size; | |
this.env = new p5.Env(); | |
// set attackTime, decayTime, sustainRatio, releaseTime | |
this.env.setADSR(0.001, 0.1, 0.2, 0.1); | |
// set attackLevel, releaseLevel | |
this.env.setRange(3, 0); | |
// p5.Amplitude will analyze all sound in the sketch | |
// unless the setInput() method is used to specify an input. | |
this.analyzer = new p5.Amplitude(); | |
this.analyzer.setInput(this.env); | |
} | |
draw(){ | |
// get volume reading from the p5.Amplitude analyzer | |
let level = this.analyzer.getLevel(); | |
// use level to draw a green rectangle | |
let levelHeight = map(level, 0, 0.4, 0, height); | |
fill(100, 250, 100); | |
rect(this.x, this.y, this.width, -levelHeight); | |
} | |
play(){ | |
this.osc.freq(random(notes)); | |
this.env.play(this.osc); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment