Files
sunny9898/task8/wk9/wk9_example_5b/wk9_example_5b.pde
louiscklaw 5637fbf94f update,
2025-02-01 02:07:58 +08:00

55 lines
1.2 KiB
Plaintext

import processing.sound.*;
AudioIn input;
Amplitude amp;
int scale;
int threshold = 50;
color c0 = color(random(255),random(255),random(255), random(20,100));
Circle[] myCircles = new Circle[0];
void setup() {
size(1024, 768);
background(255);
input = new AudioIn(this, 0);
//input.start(); // capture only
input.play(); // capture and play
amp = new Amplitude(this);
amp.input(input);
noStroke();
fill(c0);
}
void draw() {
background(255);
scale = int(map(amp.analyze(), 0, 0.2, 0, threshold));
fill(c0);
ellipse(width/2, height/2, scale, scale);
if (scale>threshold)
addCircle(scale, c0);
drawCircles();
}
void addCircle(int s, color c) {
// Create a new instant of circle
Circle tempCircle = new Circle(s, c);
// Add the new circle to Circle array
myCircles = (Circle[])append(myCircles, tempCircle);
println("Num of circles: "+myCircles.length);
// generate a new color for the next circle
c0 = color(random(255), random(255), random(255), random(20, 100));
}
void drawCircles() {
for (int i = 0; i < myCircles.length; i++) {
myCircles[i].move();
myCircles[i].display();
}
}
void keyPressed() {
println("Clear all");
myCircles = new Circle[0];
}