How to change background color with sound

edited November 2013 in How To...
import ddf.minim.*;

Minim minim;
AudioInput in;
AudioRecorder recorder;


  void setup() {
  size(800, 600);  // size always goes first!
  if (frame != null) {
    frame.setResizable(true);
  }


  minim = new Minim(this);

  in = minim.getLineIn();
  recorder = minim.createRecorder(in, "myrecording.wav", true);

  textFont(createFont("Arial", 12));
}

void draw()
{
  background(0); 
  stroke(255);
  for(int i = 0; i < in.bufferSize() - 1; i++)
  {
    line(i, 150 + in.left.get(i)*50, i+1, 150 + in.left.get(i+1)*50);
    line(200 + in.left.get(i)*50, i, 200 + in.left.get(i+1)*50, i+1);
    line(i, 450 + in.right.get(i)*50, i+1, 450 + in.right.get(i+1)*50);
        line(600 + in.right.get(i)*50, i, 600 + in.right.get(i+1)*50, i+1);
  }

  if ( recorder.isRecording() )
  {
    text("Currently recording...", 5, 15);
  }
  else
  {
    text("Not recording.", 5, 15);
  }
}

void keyReleased()
{
  if ( key == 'r' ) 
  {

    if ( recorder.isRecording() ) 
    {
      recorder.endRecord();
    }
    else 
    {
      recorder.beginRecord();
    }
  }
  if ( key == 's' )
  {

    recorder.save();
    println("Done saving.");
  }
}

how can i make the background color change with the sound...?

Tagged:

Answers

  • edited November 2013

    Try something like this:

    import ddf.minim.analysis.*;
    import ddf.minim.*;
    Minim       minim;
    AudioPlayer jingle;
    FFT         fft;
    void setup()
    {
      size(512, 200);
      minim = new Minim(this);
      jingle = minim.loadFile("yourFileName.wav", 1024);
      // loop the file indefinitely
      jingle.loop();
      fft = new FFT( jingle.bufferSize(), jingle.sampleRate() );
    }
    
    void draw()
    { 
      fft.forward( jingle.mix );
    
      for(int i = 0; i < fft.specSize(); i++)
      {
        background(fft.getBand(i)*1000);
      }
    }
    
Sign In or Register to comment.