2017年12月11日月曜日

人間の耳で音の位相は聞き取れるのか?

「人間の耳で音の位相は聞き取れるのか?」問題のテスト。音痴な自分でも結構分かる気はする...(特にphase=0と0.5の差は相当顕著)。
import ddf.minim.*;    
import ddf.minim.ugens.*;    

Minim minim;
AudioOutput out;    
Oscil sin1, sin2;
float f=440;
float A=1.0;
float trig=0.5;
void setup() {

  size(512, 200);
  smooth();
  minim = new Minim(this);
  out = minim.getLineOut(Minim.MONO, 1024);

  sin1=new Oscil(f, A, Waves.SINE);
  sin2=new Oscil(f*2, A*0.5, Waves.SINE);
  sin1.setPhase(0);
  sin2.setPhase(0);

  sin1.patch(out);
  sin2.patch(out);
}


void draw() {
  background(255);
  stroke(0);
  float x=0, Amp_old=999;
  int flag=0;
  for (int i = 0; i < out.bufferSize()-1; i++) {
    float Amp=out.left.get(i);
    if ((Amp_old=trig)||flag==1) {
      point(x, height/2 + Amp*height/4);
      flag=1;
      x+=2;
    }
    Amp_old=Amp;
  }
}

void mouseMoved() {
  float phase=map(mouseX, 0, width, 0, 1);
  sin2.setPhase(phase);
}


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