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

Daily Creative Coding

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

パーリンノイズで音程と音量を制御する(Minim)

f:id:aa_debdeb:20160622110551j:plain
/**
* sound conrolled by perlin noise
*
* @author aa_debdeb
* @date 2016/06/26
*/

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;
AudioOutput out;
Oscil wave;
float freqNoise, ampNoise;
int state = 1;

void setup(){
  size(512, 200);
  minim = new Minim(this);
  out = minim.getLineOut();
  wave = new Oscil(440, 0.5, Waves.SINE);
  wave.patch(out);
  freqNoise = random(10000);
  ampNoise = random(10000);
}

void draw(){
  background(0);
  stroke(255);
  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);
  }
  float freq = map(noise(freqNoise + frameCount * 0.01), 0, 1, 20, 3000);
  wave.setFrequency(freq);
  float amp = map(noise(ampNoise + frameCount * 0.01), 0, 1, 0.1, 1.0);
  wave.setAmplitude(amp);
}

void mousePressed(){
  state += 1;
  if(state == 7){state = 1;}
  switch(state){
    case 1:
      wave.setWaveform(Waves.SINE);
      break;
    case 2:
      wave.setWaveform(Waves.TRIANGLE);
      break;
    case 3:
      wave.setWaveform(Waves.SAW);
      break;
    case 4:
      wave.setWaveform(Waves.SQUARE);
      break;
    case 5:
      wave.setWaveform(Waves.QUARTERPULSE);
      break;
    case 6:
      wave.setWaveform(Waves.PHASOR);
      break;
  }
}