How should I edit the codes to make my codes respond to sound?
in
Core Library Questions
•
1 year ago
I already have the code here..
Right now, the particles are responding to mouseX, mouseY positions and speed.
I want the particles to respond to the music,
But I have no idea which codes to use or modify.
I am totally lost..
I was thinking something like..
The faster the beat, faster the movement of the circle..
or...the higher the wavelength's height, the more vibrant the color...
Can someone help me out?
D;
- import ddf.minim.*;
- import ddf.minim.signals.*;
- import ddf.minim.analysis.*;
- import ddf.minim.effects.*;
- Minim minim;
- AudioPlayer groove;
- ParticleSystem ps;
- void setup() {
- size(700, 700);
- //colorMode(RGB, 255, 255, 255, 100);
- background(240);
- ps = new ParticleSystem(1, new PVector(width/2,height/2,0));
- smooth();
- minim = new Minim(this);
- groove = minim.loadFile("takuya.wav",0400);
- groove.loop();
- }
- void draw() {
- ps.run();
- ps.addParticle(mouseX,mouseY);
- //how can I change the framerate so that the background
- //is covered less often?
- frameRate(40);
- fill(0,0,0,10);
- ellipseMode(CENTER);
- ellipse(mouseX,mouseY,20,20);
- strokeWeight(2);
- noStroke();
- rect(0,0,700,700);
- fill(255,10);
- }
- class Particle {
- PVector loc;
- PVector vel;
- PVector acc;
- float r;
- float timer;
- Particle(PVector l) {
- acc = new PVector(0,0.05,0);
- vel = new PVector(random(-1,5),random(-8,0),3);
- loc = l.get();
- r = random(3);
- timer = 30.0;
- }
- void run() {
- update();
- render();
- }
- void update() {
- vel.add(acc);
- loc.sub(vel);
- timer += 0.5;
- }
- void render() {
- ellipseMode(CENTER);
- stroke(255,50);
- //later, I could change the colors to change according to the sound
- fill(abs(mouseX-pmouseX), abs(mouseY-pmouseY), abs(mouseX-pmouseY),20);
- ellipse(loc.x,loc.y,abs(mouseY-pmouseY), abs(mouseY-pmouseY));
- displayVector(vel,loc.x,loc.y,5);
- }
- boolean dead() {
- if (timer <= .01) {
- return true;
- }
- else {
- return false;
- }
- }
- void displayVector(PVector v, float x, float y, float scayl) {
- pushMatrix();
- float arrowsize = random(30);
- translate(x,y);
- stroke(255,200);
- //direction
- rotate(v.heading2D());
- //len means length
- float len = v.mag()/scayl;
- line(0,0,5,0);
- popMatrix();
- }
- }
- class ParticleSystem {
- ArrayList particles;
- PVector origin;
- ParticleSystem(int num, PVector v) {
- particles = new ArrayList();
- origin = v.get();
- for (int i = 0; i < num; i++) {
- particles.add(new Particle(origin));
- }
- }
- void run() {
- for (int i = particles.size()-4; i >= 0; i--) {
- Particle p = (Particle) particles.get(i);
- p.run();
- if (p.dead()) {
- particles.remove(i);
- }
- }
- }
- void addParticle() {
- particles.add(new Particle(origin));
- }
- void addParticle(float x, float y) {
- particles.add(new Particle(new PVector(mouseX,mouseY)));
- }
- void addParticle(Particle p) {
- particles.add(p);
- }
- boolean dead() {
- if (particles.isEmpty()) {
- return true;
- }
- else {
- return false;
- }
- }
- void stop() {
- groove.close();
- minim.stop();
- // super.stop();
- println(groove);
- }
- }
1