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

30 min. Processing

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

MinimとControlP5でリズムマシン

サウンド GUI
f:id:aa_debdeb:20161030095545j:plain
/**
* rhythm machine by minim and conrolP5 
*
* @author aa_debdeb
* @date 2016/11/08
*/

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;
AudioPlayer kick, snare, hihat;
ControlP5 cp5;
Toggle[] kickSequncer, snareSequncer, hihatSequncer;

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

void setup(){
  size(800, 500);
  frameRate(desiredFrameRate);
  minim = new Minim(this);
  kick = minim.loadFile("kick.mp3");
  snare = minim.loadFile("snare.mp3");
  hihat = minim.loadFile("hihat.mp3");
  cp5 = new ControlP5(this);
  float yStep = height / 5;
  cp5.addKnob("bpm")
     .setRange(60, 200)
     .setValue(120)
     .setRadius(30)
     .setPosition(width / 2 - 30, yStep * 1 - 30);
  kickSequncer = new Toggle[16];
  snareSequncer = new Toggle[16];
  hihatSequncer = new Toggle[16];
  PVector toggleSize = new PVector(20, 15);
  for(int i = 0; i < 16; i++){
    float x = (i - 8) * 40 + width / 2 - toggleSize.x / 2;
    kickSequncer[i] =  cp5.addToggle("kick" + i)
                       .setSize(int(toggleSize.x), int(toggleSize.y))
                       .setPosition(x, yStep * 2 - toggleSize.y / 2)
                       .setValue(false);
    snareSequncer[i] =  cp5.addToggle("snare" + i)
                       .setSize(int(toggleSize.x), int(toggleSize.y))
                       .setPosition(x, yStep * 3 - toggleSize.y / 2)
                       .setValue(false);
    hihatSequncer[i] =  cp5.addToggle("hihat" + i)
                       .setSize(int(toggleSize.x), int(toggleSize.y))
                       .setPosition(x, yStep * 4 - toggleSize.y / 2)
                       .setValue(false);                      
  }
}

void draw(){
  if(frameCount % int((desiredFrameRate / (bpm / 60.0)) / 4.0) == 0){
    if(kickSequncer[count].getBooleanValue()){
      kick.play();
      kick.rewind();
    }
    if(snareSequncer[count].getBooleanValue()){
      snare.play();
      snare.rewind();
    }
    if(hihatSequncer[count].getBooleanValue()){
      hihat.play();
      hihat.rewind();
    }
    count++;
    if(count == 16) count = 0;
  }
}