Skip to content

Instantly share code, notes, and snippets.

@coreygo
Created March 7, 2016 23:40

Revisions

  1. coreygo created this gist Mar 7, 2016.
    138 changes: 138 additions & 0 deletions SpiralOnAudioInputBeatListen.pde
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,138 @@
    import ddf.minim.*;
    import ddf.minim.analysis.*;
    import ddf.minim.signals.*;

    Minim minim;
    AudioInput in;
    //AudioPlayer player;
    BeatDetect beat;
    BeatListener bl;

    ArrayList pos = new ArrayList(0);
    //float[] playerBuffer;
    float[] inputBuffer;

    float A, X, Y;
    float g = (sqrt(7) - 1) / 2.0;
    float L = 1;

    float theta;
    int counter;

    void setup() {
    size(1920, 1080, P3D);
    // bug: required for proper alpha opacity layering in P3D
    hint(DISABLE_OPTIMIZED_STROKE);
    smooth();
    frameRate(60); // overly optimistic, we're aiming for 60
    colorMode(HSB, 255, 255, 255);
    minim = new Minim(this);
    //player = minim.loadFile("InTheLightOfKashmir.mp3", 2048);
    //playerBuffer = new float[player.bufferSize()];
    in = minim.getLineIn(Minim.STEREO, 2048);
    inputBuffer = new float[in.bufferSize()];

    X = Y = 0;
    beat = new BeatDetect();
    bl = new BeatListener(beat, in);
    beat.setSensitivity(50);
    counter = 1337*3;
    }

    void keyPressed()
    {
    if (in.isMonitoring())
    {
    in.disableMonitoring();
    } else
    {
    in.enableMonitoring();
    }
    }

    void draw() {
    background(0);
    spiral(width/2, height/2, 0, counter);
    //lissajous();
    println("FPS: " + frameRate); // debug to show current framerate
    }

    void lissajous() {
    // Lissajous + Bowditch
    strokeWeight(1.5);
    beginShape();
    for (int i = 0; i < in.bufferSize() - 1; i++)
    {
    //stroke((i*.15), 0, 255, 255/32);
    stroke(0, 0, 255, 255/32);
    line(width/2+in.left.get(i+1)*1280, height/2-in.right.get(i+1)*720, width/2, height/2);
    //point(width/2+in.left.get(i+1)*720, height/2-in.right.get(i+1)*720);
    }
    endShape();
    }

    void spiral(float x, float y, float start, int many) {
    // Fibonacci
    beat.detect(in.mix);
    X = x;
    Y = y;
    L = start;
    //if ( L >= 10) { L += start; }

    theta = map(counter, 0, width/2, 0, PI/4);
    counter += ((in.mix.level()*60)/2);
    if (beat.isOnset()) {
    counter += 1337/2;
    }
    // prevents madness though there's room to improve counter reduction
    // this is currently necessary but isn't smoothly implemented
    // try commenting out this if statement to see the results
    if (counter > 1337*3) {
    counter -= 1337/1.85;
    }

    println("Counter: " + counter); // debug to show number of current lines in fib sequence

    pushMatrix();
    for (int i = 0; i < many; i++) {

    translate(X, Y);
    rotate(theta);

    strokeWeight(1);
    stroke(255, 127/1.5);
    line(0, L, 0, 0);

    rotate(radians(noise((frameCount - i) * 0.001 * g) * 30.0 + 600));
    stroke((i * 0.22), 255, 255, 127);
    line(L, 0, 0, 0);

    translate(-X, -Y);
    L += g;
    X += L;
    }
    popMatrix();
    }

    class BeatListener implements AudioListener
    {
    private BeatDetect beat;
    private AudioInput in;

    BeatListener(BeatDetect beat, AudioInput source)
    {
    this.in = source;
    this.in.addListener(this);
    this.beat = beat;
    }

    void samples(float[] samps)
    {
    beat.detect(in.mix);
    }

    void samples(float[] sampsL, float[] sampsR)
    {
    beat.detect(in.mix);
    }
    }