Daily Creative Coding

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

Minimでホワイトノイズにローパス・ハイパス・バンドパスフィルターをかける

f:id:aa_debdeb:20151204214628j:plain

キーボードからフィルターを変更可能.

  • 1: ローパスフィルター
  • 2: ハイパスフルター
  • 3: バンドパスフィルター

マウスのx座標で周波数,y座標でレゾナンスを決定する. 表示画面は上が波形で,下がFFTの結果を表している.

/**
* Low Pass / High Pass / Band Pass Filters
*
* @author aa_debdeb
* @date 2015/12/16
*/

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;
FFT fft;
Noise noise;
MoogFilter moog;
UGen currentUGen;

void setup(){
  size(512, 550);
  minim = new Minim(this);
  out = minim.getLineOut();
  
  noise = new Noise(0.5, Noise.Tint.WHITE);
  moog = new MoogFilter(440, 0.5, MoogFilter.Type.LP);
  noise.patch(moog).patch(out);
  
  fft = new FFT(out.bufferSize(), out.sampleRate());
}

void draw(){
  background(0);
  
  stroke(128, 100);
  strokeWeight(1);
  line(mouseX, 0, mouseX, height);
  line(0, mouseY, width, mouseY);
  
  stroke(255, 140, 0);
  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);
  }
  
  fft.forward(out.mix);
  noStroke();
  fill(255, 140, 0);
  for(int i = 0; i < fft.specSize() / 2; i += 5){
    float rectWidth = fft.getBand(i) * 3;
    rect(width / 2 - rectWidth, 250 + i, rectWidth * 2, 5); 
  }
  
  textSize(12);
  text("type: " + moog.type, 30, 210);
  text("frequency: " + moog.frequency.getLastValue(), 30, 225);
  text("resonance: " + moog.resonance.getLastValue(), 30, 240);
}

void mouseMoved(){
  float freq = map(mouseX, 0, width, 100, 15000);
  float rez = map(mouseY, 0, height, 0, 1);
  
  moog.frequency.setLastValue(freq);
  moog.resonance.setLastValue(rez);
}

void keyPressed(){
  switch(key){
    case '1':
      moog.type = MoogFilter.Type.LP;
      break;
    case '2':
      moog.type = MoogFilter.Type.HP;
      break;
    case '3':
      moog.type = MoogFilter.Type.BP;
      break;
  }
}