xxxxxxxxxx
75
/* 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;
let xHist = [];
let yHist = [];
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);
t += 0.1;
xOffset = width / 2;
y = height;
xOffset += (noise(t) - 0.5) * 20;
xHist.push(xOffset);
yHist.push(y);
beginShape(LINES);
for (i = 0; i < volHist.length; i++) {
y -= map(volHist[i], 0, 1, 0, 20);
stroke("green");
// fill("green");
// strokeWeight(5);
vertex(xHist[i], yHist[i]);
vertex(xOffset, y);
}
endShape();
print(vol);
}
// 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");
}
}