Daily Creative Coding

元「30 min. Processing」。毎日、Creative Codingします。

音量を色で可視化する

f:id:aa_debdeb:20151118084429j:plain
/**
* Sound Volume Meter
*
* @author aa_debdeb
* @date 2015/11/22
*/

import ddf.minim.spi.*;
import ddf.minim.signals.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.ugens.*;
import ddf.minim.effects.*;

Minim minim;
AudioInput in;

float cWidth = 10;
float cGap = 3;

void setup(){
  size(500, 300);
  smooth();
  minim = new Minim(this);
  in = minim.getLineIn(Minim.STEREO, 512);
}

void draw(){
  colorMode(RGB);
  background(30);
  colorMode(HSB, 360, 100, 100);
  noStroke();
  for(float x = cGap; x < width - cWidth; x += (cWidth + cGap)){
    float hue = 240 - map(x, 0, width, 0, 240);
    float birghtness = in.mix.level() > map(x, 0, width, 0, 1) * 0.1 ? 80: 20;
    fill(hue, 100, birghtness);
    rect(x, 5, cWidth, height - 10);
  }
}

void stop(){
  in.close();
  minim.stop();
  super.stop();
}