xxxxxxxxxx
69
/* A line is drawn from the bottom-center of the canvas.
The y-coordinate of the end of the line is mapped
to the current amplitude each frame.
The x-coordinate of the end of the line is randomly
generated using Perlin Noise.
*/
let mic, fft, spectrum, amp, vol;
let micOn = true;
let volHist = [];
let t = 0;
function setup() {
createCanvas(400, 600);
mic = new p5.AudioIn(print("mic detected"),
function() {
print("no mic detected")
});
mic.start(print("mic on"),
function() {
print("mic off")
});
amp = new p5.Amplitude();
amp.setInput(mic);
frameRate(5);
}
function draw() {
background(220);
vol = amp.getLevel();
volHist.push(vol);
print(vol);
t += 0.1;
x = width/2;
x += (noise(t) - 0.5) * 20;
seedling(x, height);
}
function seedling(x, y) {
this.x = x;
this.y = y;
for (i = 0; i < volHist.length; i++) {
this.y -= map(volHist[i], 0, 1, 0, 20);
stroke("green");
fill("green");
line(width / 2, height, this.x, this.y);
}
}
// click the mouse to turn the mic on and off
function mouseClicked() {
if (micOn == true) {
micOn = false
mic.stop();
print("mic off");
} else {
micOn = true;
mic.start();
print("mic on");
}
}