Daily Creative Coding

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

Minimでビットクラッシャー・エフェクトをかける

f:id:aa_debdeb:20151204231041j:plain
/**
* Bit Crusher Effect
*
* @author aa_debdeb
* @date 2015/12/17
*/

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

Minim minim;
AudioOutput out;
Oscil sine;
BitCrush bitCrush;

void setup(){
  size(512, 200);
  minim = new Minim(this);
  out = minim.getLineOut();
  sine = new Oscil(440, 0.5, Waves.SINE);
  bitCrush = new BitCrush(16, out.sampleRate());
  sine.patch(bitCrush).patch(out);
}

void draw(){
  background(0);
  
  stroke(60);
  strokeWeight(1);
  line(mouseX, 0, mouseX, height);
  line(0, mouseY, width, mouseY);

  stroke(255);
  strokeWeight(1);
  for(int i = 0; i < out.bufferSize() - 1; i++){
    line(i, 50 + out.left.get(i) * 50, i + 1, 50 + out.left.get(i + 1) * 50);
    line(i, 150 + out.right.get(i) * 50, i + 1, 150 + out.right.get(i + 1) * 50);
  }
  
  text("bitRes: " + bitCrush.bitRes.getLastValue(), 15, 15);
  text("bitRate: " + bitCrush.bitRate.getLastValue(), 15, 30);  
}

void mouseMoved(){
  float bitRes = map(mouseX, 0, width, 2, 16);
  float bitRate = map(mouseY, 0, height, 0, 20000);
  bitCrush.bitRes.setLastValue(bitRes);
  bitCrush.bitRate.setLastValue(bitRate);
}