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

30 min. Processing

毎日30分、Processingで何かを作る

MinimとControlP5でメトロノーム

サウンド GUI
f:id:aa_debdeb:20161026212607p:plain
/**
* metronome by minim and controlP5
*
* @author aa_debdeb
* @date 2016/11/01
*/

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

Minim minim;
AudioOutput out;
ControlP5 cp5;

int bpm;
int desiredFrameRate = 60; 
int count = 0;

void setup(){
  size(300, 300);
  frameRate(desiredFrameRate);
  minim = new Minim(this);
  out = minim.getLineOut();
  cp5 = new ControlP5(this);
  cp5.addKnob("bpm")
     .setRange(60, 200)
     .setValue(120)
     .setPosition(100, 100)
     .setRadius(50);
}

void draw(){
  if(frameCount % int(desiredFrameRate / (bpm / 60.0)) == 0){
    if(count == 0){
      out.playNote(0.0, 0.3, new MyInstrument(880)); 
    } else {
      out.playNote(0.0, 0.3, new MyInstrument(440));     
    }
    count++;
    if(count == 4) count = 0;
  }
}

class MyInstrument implements Instrument {
  Oscil osc; 
  Line ampEnv;
  MyInstrument(float frequency){
    osc = new Oscil(frequency, 0.0, Waves.SINE);
    ampEnv = new Line();
    ampEnv.patch(osc.amplitude);
  }
  
  void noteOn(float duration){
    ampEnv.activate(duration, 0.5, 0);
    osc.patch(out);
  }
  
  void noteOff(){
    osc.unpatch(out);
  }
}