いるかアイス日記

いるかアイスの日記

Traktor Pro 2とProcessingの連携

はじめに

TraktorのツマミをいじったらProcessingでインタラクティブになにか描画してくれるような事をやりたかったのでやりました。 Traktorの操作はMIDI信号としてとれるので、ProcessingではMIDIを扱うライブラリなど利用してうまくやりました。

この際しっかり理解しておかないといけないのが""MIDIの構造""なので、この辺を参考に軽く理解しました。

Pump Up the DJ • Traktorマッピング虎の巻(2)MIDI編

MIDIメッセージの種類には、コントロール・チェンジ、Note On、Pitch Bendの3つあって、音階とか一番よく使われるのがNote Onという感じかな...。(雑)

実際にやってみる

Traktor のI/O設定

  1. Traktor画面でPreferencesを開く(⌘+,)。
  2. Device を addして Generic MIDI を選択
  3. In portとOut portをTraktor Virtualに選択

f:id:irucaice:20151222125344p:plain

Processingの設定

  1. promidi というProcessingのライブラリの準備 ここのライブラリを落としておきます。 http://creativecomputing.cc/p5libs/promidi/

  2. exampleのpromidiを動かしてみる

import promidi.*;

MidiIO midiIO;

void setup(){
  size(128*5,128*5);
  smooth();
  background(0);
  
  //get an instance of MidiIO
  midiIO = MidiIO.getInstance(this);
  println("printPorts of midiIO");
  
  //print a list of all available devices
  midiIO.printDevices();
  
  //open the first midi channel of the first device
  midiIO.openInput(2,0);
}

void draw(){
  //nothing to do here
}

void noteOn(Note note, int device, int channel){
  int vel = note.getVelocity();
  int pit = note.getPitch();
  
  fill(255,vel*2,pit*2,vel*2);
  stroke(255,vel);
  ellipse(vel*5,pit*5,30,30);
}

void noteOff(Note note, int device, int channel){
  int pit = note.getPitch();
  
  fill(255,pit*2,pit*2,pit*2);
  stroke(255,pit);
  ellipse(pit*5,pit*5,30,30);
}

void controllerIn(Controller controller, int device, int channel){
  int num = controller.getNumber();
  int val = controller.getValue();
  
  fill(255,num*2,val*2,num*2);
  stroke(255,num);
  ellipse(num*5,val*5,30,30);
}

void programChange(ProgramChange programChange, int device, int channel){
  int num = programChange.getNumber();
  
  fill(255,num*2,num*2,num*2);
  stroke(255,num);
  ellipse(num*5,num*5,30,30);
}

これを実行し、コンソール画面に「Traktor Virtual Output/Input」が表示されていれば認識されています。

f:id:irucaice:20151222124908p:plain

midiIO.openInput(2,0);
ここで利用するMIDI機器を指定します。
1つ目の引数がMIDI機器の番号、2つ目がチャンネルの番号になる。
MIDI機器の番号はprintDevices();で表示した際にinputの横に表示された番号を使用し、チャンネル番号は0で問題ない。

Traktor マッピングの設定

  1. 再びTraktor画面でPreferencesを開く(⌘+,)。
  2. Assignment TableでAdd outを押してcontrolを追加。今回はEQのHigh・Mid・Lowをマッピングします。
  3. Device Mappingでこの操作をどの信号に割り当てるか指定します。今回はCh01.Note.C1, C#1, D1にマッピング
  4. LED Options のController Rangeの0.0〜1.0をMidi Rangeの0〜127に割り当てる。

f:id:irucaice:20151222141934p:plain

これでプログラムを実行し、つまみを動かすと、インタラクティブに円が表示されます。 f:id:irucaice:20151222161809p:plain おわり。