①スケッチの準備
Processingを起動して、空のスケッチウィンドウに以下のコードをコピー&ペーストする。
import oscP5.*; //oscP5ライブラリの読み込み
import netP5.*;
import rwmidi.*; //RWMIDIライブラリの読み込み
OscP5 oscP5;
MidiOutput output;
int ch = 0; //Midi Chの設定 ここ変えるとKontakt Playeyの別のラックの音出せる
int note_on = 60; //NoteOnナンバー(変数)
int note_off = 60; //NoteOffナンバー(変数)
int pitch = 8192; //ピッチベンド
int vel = 100; //Velocity(音の強さ)の設定0〜127
int program = 1; //プログラムチェンジ(音色)の設定
int dev = 0; //音源の設定 Kontaktをport a(=0)にした時はJavaは2
int devLength = 0; //デバイスの数
int port = 12000; //ポートナンバー
int lock = 0; //マウスの連打を防止するため
void setup () {
size (500, 300); //ウィンドウサイズ
frameRate (30);
oscP5 = new OscP5 (this, port); //受信アドレスとポート,thisはDHCPで自動に振られたアドレスになる
devLength = RWMidi.getOutputDevices ().length; //デバイスの数
output = RWMidi.getOutputDevices () [dev].createOutput(); //デバイスの設定
output.sendProgramChange (program); //プログラムチェンジの設定
//デバイスリストの表示
for (int i = 0; i < devLength; i++) {
println ("Output Device " + i + " : " + RWMidi.getOutputDevices () [i].getName() );
}
}
void draw () {
background (164, 0, 0);
text ("Device Name: " + output.getName (), 15, 20);
text ("Program Change: " + program, 15, 40);
text ("Push Keys or Send OSC! (port: " + port + ")", 15, 80);
}
void keyPressed () { //キーを押した時
output.sendPitchBend( ch, 8192 );
if(lock==0){
switch(key) {
case'z': note_on = 41; break;
case'x': note_on = 43; break;
case'c': note_on = 45; break;
case'v': note_on = 47; break;
case'b': note_on = 48; break;
case'n': note_on = 50; break;
case'm': note_on = 52; break;
case',': note_on = 53; break;
case'.': note_on = 55; break;
case'/': note_on = 57; break;
case'_': note_on = 59; break;
case'a': note_on = 60; break;
case's': note_on = 62; break;
case'd': note_on = 64; break;
case'f': note_on = 65; break;
case'g': note_on = 67; break;
case'h': note_on = 69; break;
case'j': note_on = 71; break;
case'k': note_on = 72; break;
case'l': note_on = 74; break;
case';': note_on = 76; break;
case':': note_on = 77; break;
case']': note_on = 79; break;
default: note_on = 0; break;
}
output.sendNoteOn ( ch, note_on, vel ); //NoteOnの送信
println ( "Ch: " + ch + " Note: " + note_on + " Vel: " + vel); //デバッッグ
lock=1;
}
}
void keyReleased () { //キーを離した時
switch(key) {
case'z': note_off = 41; break;
case'x': note_off = 43; break;
case'c': note_off = 45; break;
case'v': note_off = 47; break;
case'b': note_off = 48; break;
case'n': note_off = 50; break;
case'm': note_off = 52; break;
case',': note_off = 53; break;
case'.': note_off = 55; break;
case'/': note_off = 57; break;
case'_': note_off = 59; break;
case'a': note_off = 60; break;
case's': note_off = 62; break;
case'd': note_off = 64; break;
case'f': note_off = 65; break;
case'g': note_off = 67; break;
case'h': note_off = 69; break;
case'j': note_off = 71; break;
case'k': note_off = 72; break;
case'l': note_off = 74; break;
case';': note_off = 76; break;
case':': note_off = 77; break;
case']': note_off = 79; break;
default: note_off = 0; break;
}
output.sendNoteOff(ch, note_off, vel); //NoteOffの送信
lock=0;
}
void mouseMoved ()
{
output.sendPitchBend(0, (int) map(mouseX, 0, 800, 0, 16382)); //ピッチベンドの送信
}
//OSC受信処理
void oscEvent (OscMessage theOscMessage) {
//OSCで/noteonを受信した時
if(theOscMessage.checkAddrPattern("/noteon")) {
if (theOscMessage.checkTypetag("i")) { //受信データのint判定
println("/noteon: " + theOscMessage.get(0).intValue());
note_on = theOscMessage.get(0).intValue();
output.sendNoteOn( ch, note_on, vel );
}
output.sendPitchBend( ch, 8192 ); //ピッチベンドのリセット
}
//OSCで/noteoffを受信した時
if(theOscMessage.checkAddrPattern("/noteoff")) { //NoteOff
if (theOscMessage.checkTypetag("i")) { //受信データのint判定
println("/noteoff: " + theOscMessage.get(0).intValue());
note_off = theOscMessage.get(0).intValue();
output.sendNoteOff(ch, note_off, vel);
}
}
//OSCで/pitchを受信した時 ピッチベンドは0〜16383
if(theOscMessage.checkAddrPattern("/pitch")) {
if (theOscMessage.checkTypetag("i")) { //受信データのint判定
println("/pitch: " + theOscMessage.get(0).intValue());
pitch = theOscMessage.get(0).intValue();
output.sendPitchBend( ch, pitch );
}
}
}
②スケッチの実行(キーボードとマウスで演奏)
Runボタンを押して実行すると赤いウィンドウが表示される。

キーボードの下図のキーを押すことで演奏することができる。

キーを押したまま、マウスを左右に振ることで音高を変化させることもできる。
③音色の変更
スケッチの12行目のprogramの引数を変更して音色を変えてみよう。
int program = 1; //プログラムチェンジ(音色)の設定