Problem with creating Object from Sketch
in
Programming Questions
•
1 year ago
Hey all,
I was messing around with making music visualizers in Processing (using the Minim library) and came up with this neat sketch:
- import ddf.minim.analysis.*;
- import ddf.minim.*;
- import processing.video.*;
- Minim minim;
- AudioInput input;
- FFT fft;
- void setup() {
- size(1024,640);
- smooth();
- background(0);
- minim = new Minim(this);
- input = minim.getLineIn(Minim.STEREO, 512);
- fft = new FFT(input.bufferSize(), input.sampleRate());
- fft.linAverages(64);
- }
- void draw() {
- //clear screen
- fill(0,12);
- rect(0, 0, width, height);
- noFill();
- //move the FFT forward
- fft.forward(input.mix);
- //compute some parameters
- int w = int(fft.specSize()/64);
- //draw everything using the FFT data
- for (int i = 0; i < fft.avgSize(); i++){
- //stroke(0, 0, fft.getAvg(i)*50, 225);
- stroke(fft.getAvg(i)*25, fft.getAvg(i)*25, fft.getAvg(i)*25 + 50);
- ellipse(width/2 + fft.getAvg(w)*50, height/2 + fft.getAvg(i)*50, i, i);
- ellipse(width/2 + fft.getAvg(w)*50, height/2 - fft.getAvg(i)*50, i, i);
- ellipse(width/2 - fft.getAvg(w)*50, height/2 + fft.getAvg(i)*50, i, i);
- ellipse(width/2 - fft.getAvg(w)*50, height/2 - fft.getAvg(i)*50, i, i);
- }
- }
- void stop(){
- input.close();
- minim.stop();
- super.stop();
- }
It looks pretty neat, but I wanted to be able to put multiple visualizations into a single file and be able to switch between them without closing the window. I figured the best way to do this would be with classes, so I tried to turn this sketch into a class:
- import ddf.minim.analysis.*;
- import ddf.minim.*;
- import processing.video.*;
- Minim minim;
- AudioInput input;
- FFT fft;
- DualCircles circ;
- void setup() {
- size(1024, 640);
- smooth();
- background(0);
- minim = new Minim(this);
- input = minim.getLineIn(Minim.STEREO, 512);
- fft = new FFT(input.bufferSize(), input.sampleRate());
- fft.linAverages(64);
- rectMode(CORNERS);
- DualCircles circ = new DualCircles(color(25,25,25),75);
- }
- void draw() {
- fill(0, 16);
- rect(0, 0, width, height);
- noFill();
- //move the FFT forward
- fft.forward(input.mix);
- //pass the updated FFT to draw() to update the sketch
- circ.draw(fft);
- }
- void stop() {
- input.close();
- minim.stop();
- super.stop();
- }
- class DualCircles {
- color strokeColor;
- float sensitivity;
- float bassFactor;
- DualCircles(color strokeColorValue, float sensitivityValue){
- strokeColor = strokeColorValue;
- sensitivity = sensitivityValue;
- }
- void draw(FFT fft){
- int bassFactor = int(fft.specSize()/64);
- for (int i = 0; i < fft.avgSize(); i++) {
- stroke(fft.getAvg(i)*red(strokeColor), fft.getAvg(i)*green(strokeColor), fft.getAvg(i)*blue(strokeColor) + 50);
- ellipse(width/2 + fft.getAvg(bassFactor)*sensitivity, height/2 + fft.getAvg(i)*50, i, i);
- ellipse(width/2 + fft.getAvg(bassFactor)*sensitivity, height/2 - fft.getAvg(i)*50, i, i);
- ellipse(width/2 - fft.getAvg(bassFactor)*sensitivity, height/2 + fft.getAvg(i)*50, i, i);
- ellipse(width/2 - fft.getAvg(bassFactor)*sensitivity, height/2 - fft.getAvg(i)*50, i, i);
- }
- }
- }
However, this seems to throw an error at the call to draw(). I get a NullPointerException, and the following gets printed out:
Exception in thread "Animation Thread" java.lang.NullPointerException
at circlesandsquaresTEST.draw(circlesandsquaresTEST.java:50)
at processing.core.PApplet.handleDraw(Unknown Source)
at processing.core.PApplet.run(Unknown Source)
at java.lang.Thread.run(Thread.java:662)
I can decode that to understand that there's an error in draw(), but I can't narrow it down any further. Any thoughts?
1