Ok, your attempts show improvement. So that's good!
Some suggestions:
Some suggestions:
- Set the number of objects in the array.
- Initialize the array in setup (this was causing your null pointer exception).
- Everything minim-related can be done ONCE. Each object can use the global result via dampedVolume.
Adapted Code
- import ddf.minim.*;
- import ddf.minim.analysis.*;
- int numBalloons = 10;
- Balloon[] balloons = new Balloon[numBalloons];
- Minim minim;
- AudioInput voice;
- FFT fft;
- float dampedVolume;
- float damping = 0.6;
- float filtering = 100;
- void setup() {
- size(600, 600);
- smooth();
- minim = new Minim(this);
- voice = minim.getLineIn(Minim.STEREO, 2048);
- fft = new FFT(voice.bufferSize(), voice.sampleRate());
- for (int i=0; i<numBalloons; i++) {
- balloons[i] = new Balloon(random(width), random(height));
- }
- }
- void draw() {
- background(255);
- analyseSound(); // done once, end result = new dampedVolume value
- for (int i=0; i<balloons.length; i++) {
- balloons[i].display();
- }
- }
- void analyseSound() {
- fft.forward(voice.mix);
- float volume = 0;
- for (int i=0; i<filtering; i++) {
- volume += fft.getBand(i);
- }
- volume *= 0.025;
- dampedVolume = dampedVolume + (volume - dampedVolume)*damping;
- }
- void stop() {
- voice.close();
- minim.stop();
- super.stop();
- }
- class Balloon {
- float x, y;
- color c;
- float scaler;
- Balloon(float x, float y) {
- this.x = x;
- this.y = y;
- c = color(random(255), random(255), random(255));
- scaler = random(0.5, 2);
- }
- void display() {
- fill(c);
- ellipse(x, y, dampedVolume*scaler, dampedVolume*scaler);
- }
- }