How to make animation react to sound?

edited February 2016 in Library Questions

Hi, I'm a music student but we're learning processing and our lecturer wants us to make and animation which reacts to sound.

I'm trying to make an animation which creates spheres. The brightness( cooler colors for loq freq, brighter for high) and the number and size of the spheres increase with amplitude. Can anyone help me with how to go about doing that?

This is my code so far:

int c = 255; float op = random(0, 255);

void setup() { size(960, 500, P3D); background(0); frameRate(25); noStroke(); rectMode(CENTER); }

void draw() { fill(c, random(100));

float sz = random(200); float big = random(1, 200); float placex = random(10, 960); float placey = random(10, 500);

fill(random(255), random(255), random(255)); ellipse(random(width), random(height), sz, sz); noStroke(); lights(); translate(placex, placey); sphere(big);

if (frameCount % 200 == 0) { c = 255 - c; // 255 0 255 0 255 0 .. } saveFrame("frame-####.tif");

if (frameCount > 500) { // 20 seconds * 25 fps = 500 noLoop(); } }

Answers

  • edited February 2016

    @rudeboi===

    • please format your code

    • use minim lib

    • get audioline input in
    • make FFT forward and use the values to index them for your needs.
  • edited February 2016

    @akenaton I appreciate the response but I have no idea what any of that means?

  • edited February 2016

    Deleted.

  • edited February 2016

    I'll explain:

    please format your code
    

    go back edit your post

    format your code right

    How to post code

    when you post your code:

    • in the processing editor hit ctrl-t to auto format

    • copy it

    • paste it in the browser

    • leave 2 empty lines before and after it

    • mark the code (without the empty lines)

    • press C in the small command bar.

    • save post

    use minim lib
    get audioline input in
    make FFT forward and use the values to index them for your needs.
    

    there is a section librarys where you can read about those - a library brings you new commands. Minim is not in there.

    ok, read the examples on FFT for minim:

    http://code.compartmental.net/tools/minim/

    then in processing, install the lib in the menu Sketch (import lib | add lib) and look at the examples (Menu File|examples|contributed libraries). When you get get audioline input in you can listen to the sound, make FFT forward and use the values to index them for your needs.

    show your code again when you are this far.

  • edited February 2016

    @Chrisir=== thanks to explain; i am not an "administrator" && not enough time left to...i work..i am a developper so i give guidelines is it well???

  • @akenaton, it is fine that you helped him, thank you.

    I was just explaining a little more since he said he wasn't sure what you meant.

    Best, Chrisir :)

  • here is one of the examples

    you need a file named test.mp3 (a song)

    //
    
    
    /**
     * Get Line In
     * by Damien Di Fede.
     *  
     * This sketch demonstrates how to use the getLineIn (or other) method of 
     * Minim. This method returns an AudioInput object. 
    
     * An <code>AudioInput</code> represents a connection to the computer's current 
     * record source (usually the line-in) and is used to monitor audio coming 
     * from an external source. There are five versions of <code>getLineIn</code>:
    
     * <pre>
     * getLineIn()
     * getLineIn(int type) 
     * getLineIn(int type, int bufferSize) 
     * getLineIn(int type, int bufferSize, float sampleRate) 
     * getLineIn(int type, int bufferSize, float sampleRate, int bitDepth)  
     * </pre>
     * The value you can use for <code>type</code> is either <code>Minim.MONO</code> 
     * or <code>Minim.STEREO</code>. <code>bufferSize</code> specifies how large 
     * you want the sample buffer to be, <code>sampleRate</code> specifies the 
     * sample rate you want to monitor at, and <code>bitDepth</code> specifies what 
     * bit depth you want to monitor at. <code>type</code> defaults to <code>Minim.STEREO</code>,
     * <code>bufferSize</code> defaults to 1024, <code>sampleRate</code> defaults to 
     * 44100, and <code>bitDepth</code> defaults to 16. If an <code>AudioInput</code> 
     * cannot be created with the properties you request, <code>Minim</code> will report 
     * an error and return <code>null</code>.
     * 
     * When you run your sketch as an applet you will need to sign it in order to get an input. 
     * 
     * Before you exit your sketch make sure you call the <code>close</code> method 
     * of any <code>AudioInput</code>'s you have received from <code>getLineIn</code>.
     */
    
    import ddf.minim.*;
    
    Minim minim;
    // AudioInput in;
    
    int cellsize = 2; // Dimensions of each cell in the grid
    
    AudioPlayer in;
    
    int i=0; 
    
    // ----------------------------------
    
    void setup() {
    
      size(800, 600); 
      println( "Start of setup():" );
    
    
      minim = new Minim(this);
      //  minim.debugOn();
    
      // get a line in from Minim, default bit depth is 16
      // in = minim.getLineIn(Minim.STEREO, 800);
      in = minim.loadFile("test.mp3", 2048);
      in.loop();
      println( "End of setup()." );
    }
    
    void draw() {
    
      //background(0);
    
      // Begin loop for columns
    
      int x ; // x position
      int y ; // y position
    
      color c = color(random(255)); // the color
    
      // use map()
      float val1 = map (abs(in.left.get(i)), 0, 5, 0, width);      
      float val2 = map (abs(in.right.get(i)), -4, 4, 0, height);
    
      // Calculate a position 
      x = int(val1); // 
      y = int(val2); // 
    
      // Translate to the location, set fill and stroke, and draw the rect
      pushMatrix();
      translate(x+220, y);
      fill(c, 204);
      noStroke();
      rectMode(CENTER);
      rect(0, 0, cellsize*3, cellsize*3);
      popMatrix();
    
      i++;
    } // func 
    
    void stop()
    {
      // always close Minim audio classes when you are done with them
      in.close();
      minim.stop();
    
      super.stop();
    }
    //
    
Sign In or Register to comment.