読者です 読者をやめる 読者になる 読者になる

Daily Creative Coding

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

Minimでノコギリ波,矩形波,ホワイトノイズを合成する

サウンド
f:id:aa_debdeb:20151209184329j:plain
/**
* Synthesis of Saw Wave, Square Wave, White Noise
*
* @author aa_debdeb
* @date 2015/12/19
*/

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;
Summer summer;

void setup(){
  size(512, 200);
  minim = new Minim(this);
  out = minim.getLineOut();
  
  summer = getSawWave(440, 0.5, 50);
  summer.patch(out);
  
}

void draw(){
  background(0);
  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);
  }
}

void keyPressed(){
  switch(key){
    case '1':
      summer.unpatch(out);
      summer = getSawWave(440, 0.5, 50);
      summer.patch(out);
      break;
    case '2':
      summer.unpatch(out);
      summer = getSquareWave(440, 0.5, 50);
      summer.patch(out);
      break;
    case '3':
      summer.unpatch(out);
      summer = getWhiteNoise(0.5, 50);
      summer.patch(out);
      break;
  }
}

Summer getSawWave(float freq, float amp, int n){
  Summer summer = new Summer();
  for(int i = 0; i < n; i++){
    Oscil wave = new Oscil(freq * i, amp / (i + 1), Waves.SINE);
    wave.patch(summer);
  }
  return summer;
}

Summer getSquareWave(float freq, float amp, int n){
  Summer summer = new Summer();
  for(int i = 0; i < n; i++){
    Oscil wave = new Oscil(freq * (i * 2 + 1), amp / (i * 2 + 1), Waves.SINE);
    wave.patch(summer);
  }
  return summer;
}

Summer getWhiteNoise(float amp, int n){
  Summer summer = new Summer();
  for(int i = 0; i < n; i++){
    Oscil wave = new Oscil(random(20000), amp * 0.1, Waves.SINE);
    wave.patch(summer);
  }
  return summer;
}