ProcessingでMIDIコントローラーを使用する方法を紹介します。具体的にはMIDIコントローラーのツマミを動かして、そのControl Changeの値を活用します。
上記の動画がすごい参考になったのですが、英語なので日本の方向けに書き直します。
まずProcessingでMIDIを扱うためのライブラリとしてThe MidiBusというものが用意されています。このライブラリをインストールします。
次にexampleからBasicをロードします。
以下はExampleです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
//ライブラリをインポート import themidibus.*; //Import the library //MidiBusクラスの変数「myBus」を作成。複数MIDIコンを使いたい場合には、myBus1,myBus2等、複数作れば良い…はず。 MidiBus myBus; // The MidiBus void setup() { size(400, 400); background(0); //つながっているMIDI関係機器を表示。このリストの番号で、後ほどのIn Outの設定をする。 MidiBus.list(); // List all available Midi devices on STDOUT. This will show each device's index and name. // // Either you can // Parent In Out // | | | //myBus = new MidiBus(this, 0, 1); // Create a new MidiBus using the device index to select the Midi input and output devices respectively. // or you can ... // Parent In Out // | | | //myBus = new MidiBus(this, "IncomingDeviceName", "OutgoingDeviceName"); // Create a new MidiBus using the device names to select the Midi input and output devices respectively. //先ほど表示したリストから番号か、もしくは名前で指定。毎回同じ機材を使うのであれば名前で指定したほうがいいのか? //In Outのうち使わないところには-1を指定すれば良い模様。 // or for testing you could ... // Parent In Out // | | | myBus = new MidiBus(this, -1, "Java Sound Synthesizer"); // Create a new MidiBus with no input device and the default Java Sound Synthesizer as the output device. } void draw() { int channel = 0; int pitch = 64; int velocity = 127; //ノートを送る。 myBus.sendNoteOn(channel, pitch, velocity); // Send a Midi noteOn delay(200); myBus.sendNoteOff(channel, pitch, velocity); // Send a Midi nodeOff int number = 0; int value = 90; //コントロールチェンジを送る myBus.sendControllerChange(channel, number, value); // Send a controllerChange delay(2000); } //ノートオンが来たときに起きる関数 void noteOn(int channel, int pitch, int velocity) { // Receive a noteOn println(); println("Note On:"); println("--------"); println("Channel:"+channel); println("Pitch:"+pitch); println("Velocity:"+velocity); } //ノートオフが来たときに起きる関数 void noteOff(int channel, int pitch, int velocity) { // Receive a noteOff println(); println("Note Off:"); println("--------"); println("Channel:"+channel); println("Pitch:"+pitch); println("Velocity:"+velocity); } //コントロールチェンジが来たときに起きる関数 void controllerChange(int channel, int number, int value) { // Receive a controllerChange println(); println("Controller Change:"); println("--------"); println("Channel:"+channel); println("Number:"+number); println("Value:"+value); } void delay(int time) { int current = millis(); while (millis () < current+time) Thread.yield(); } |
MIDIコンのツマミを使うのであれば通常MIDI CCを受信することになるので以下の部分を使うことになります。
1 2 3 4 5 6 7 8 9 10 |
//コントロールチェンジが来たときに起きる関数 void controllerChange(int channel, int number, int value) { // Receive a controllerChange println(); println("Controller Change:"); println("--------"); println("Channel:"+channel); println("Number:"+number); println("Value:"+value); } |
基本的には値Valueが欲しいのでこれを変数に入れてあげて、processingの方で活用すればいいわけです。
1 2 3 4 5 6 7 |
//コントロールチェンジが来たときに起きる関数 int ccValue; void controllerChange(int channel, int number, int value) { ccValue =+value); } |
しかしこのままだとvalueは0〜127になってしまうので、使いにくいです。map()関数を使って、MIDIコンのセンターより左側に回すとマイナスの値に、右側に回すとプラスの値にしてみました。
1 2 3 4 |
int max=100; int min=-100; //ccValueに入ってくる0〜127の値をmin-100〜max100に変更してあげて使いやすくする。 float m = map(ccValue, 0, 127, min, max); |
One comment