Create class to represent sound spectrum with Minim

Hey, I create this "dancing block" that represent the low frequency of a sample, then I tried to create a class called "block" but it don't works properly.

So the question is what I am doing wrong with the class block.

I would like to listen your opinion about create a class block vs create a function block. Please tell me also aaaany sugestion about anything (first discussion).

Thanks in advance.

Here is the normal "dancing block":


import ddf.minim.*;
import ddf.minim.analysis.*;

Minim minim;
AudioPlayer sample;
FFT fftLog;

void setup(){
  size(500,500,P3D);
  minim=new Minim(this);
  sample=minim.loadFile("sample.mp3",1024); 
  
  sample.play();
  
  fftLog=new FFT(sample.bufferSize(),sample.sampleRate()); 
  fftLog.logAverages(22,1);

  
  
}
void draw(){
  background(0);
  fftLog.forward(sample.mix);
  camera(300,-300,300,0,0,0,0,1,0);
  
  box(40,fftLog.getAvg(0),40);
  
}

You will need to add a mp3 file called "sample" to test the program (it works).

And here's the OOP program where I try to create a block class (the block don't dance):


class Block{
  int bandNumber;
  float baseSize;
  float level;
  color fill;
  color stroke;   
  FFT fftLog;
  AudioPlayer sample;
   
  Block(int _bandNumber,float _baseSize,float _level,color _fill,color _stroke,FFT _fftLog,String _sampleName){ 
    bandNumber=_bandNumber;
    baseSize=_baseSize;
    level=_level;
    fill=_fill;
    stroke=_stroke;
    fftLog=_fftLog;
    sample=minim.loadFile(_sampleName,1024); 
  }
  void display(){
    fftLog.forward(sample.mix);
    translate(0,-fftLog.getAvg(bandNumber)/2,0);
    stroke(stroke);
    fill(fill);
    box(baseSize,level*fftLog.getAvg(bandNumber),baseSize);
  }
}

//And the MAIN

import ddf.minim.*;
import ddf.minim.analysis.*;

Minim minim;
AudioPlayer sample;
FFT fftLog;
Block b;
void setup() {
  size(500, 500, P3D);

  //inicilizate minim
  minim=new Minim(this);

  //To hear the music
  sample=minim.loadFile("sample.mp3", 1024); 
  sample.play();

  fftLog=new FFT(sample.bufferSize(), sample.sampleRate());
  fftLog.logAverages(22, 10);

  //inicializate block
  b=new Block(2, 50, 2, 255, 255, fftLog, "sample.mp3");
}
void draw() {
  background(0);
  camera(300, -300, 300, 0, 0, 0, 0, 1, 0);

  b.display();
}
  

Answers

  • Don't know if you can use fill and stroke as variaiables.. try to give them other names.

  • I give them other names but the box don't move. The problem is that the method getAvg() don't work in the class block and I don't know why.

  • Answer ✓

    @spiderdab::

    1) interest of creating 1 oop class: try to suppose that you want to display 100 cubes like yours, each of them with different fill por stroke or.... 2) your class code does not work because you declare twice AudioPlayer sample && fft:: try that (of course there are useless parameters in the constructor and it could be rigtht to change that in the main ui but it does not matter for the moment; leave your main class as it is && change your class with the code below

            class Block{
              int bandNumber;
              float baseSize;
              float level;
              color fill;
              color stroke;   
              //FFT fftLog;
             // AudioPlayer sample;
    
              Block(int _bandNumber,float _baseSize,float _level,color _fill,color _stroke,FFT _fftLog,String _sampleName){ 
                bandNumber=_bandNumber;
                baseSize=_baseSize;
                level=_level;
                fill=_fill;
                stroke=_stroke;
                fftLog=_fftLog;
                //sample=minim.loadFile(_sampleName,1024); 
              }
              void display(){
                fftLog.forward(sample.mix);
                translate(0,-fftLog.getAvg(bandNumber)/2,0);
                stroke(stroke);
                fill(fill);
                box(baseSize,level*fftLog.getAvg(bandNumber),baseSize);
              }
            }
    
  • Thank you!! :x

Sign In or Register to comment.