/** * oscP5sendreceive by andreas schlegel * example shows how to send and receive osc messages. * oscP5 website at http://www.sojamo.de/oscP5 */ import oscP5.*; import netP5.*; OscP5 oscP5; NetAddress myIPhone; NetAddress mySooperLooper; public int loopcount; public float loop1_len; public float loop2_len; public float loop3_len; public float loop4_len; void setup() { size(400,400); frameRate(1); /* start oscP5, listening for incoming messages at port 12000 */ //set port to SL output oscP5 = new OscP5(this,12000); /* myRemoteLocation is a NetAddress. a NetAddress takes 2 parameters, * an ip address and a port number. myRemoteLocation is used as parameter in * oscP5.send() when sending osc packets to another computer, device, * application. usage see below. for testing purposes the listening port * and the port of the remote location address are the same, hence you will * send messages back to this sketch. */ // myIPhone = new NetAddress("192.168.2.6",12001); //myIPhone = new NetAddress("flyawayphone.local",12001); mySooperLooper = new NetAddress("127.0.0.1",10051); //9951 //plugs intercept commands that come in on the third parameter and send them to the function that has the name of the second parameter oscP5.plug(this,"selected_loop_num_reader","/sl/selected_loop_num"); oscP5.plug(this,"loop_pos_reader","/sl/0/loop_pos"); oscP5.plug(this,"loop_pos_reader","/sl/1/loop_pos"); oscP5.plug(this,"loop_pos_reader","/sl/2/loop_pos"); oscP5.plug(this,"loop_pos_reader","/sl/3/loop_pos"); oscP5.plug(this,"loop_len_reader","/sl/0/loop_len"); oscP5.plug(this,"loop_len_reader","/sl/1/loop_len"); oscP5.plug(this,"loop_len_reader","/sl/2/loop_len"); oscP5.plug(this,"loop_len_reader","/sl/3/loop_len"); oscP5.plug(this,"ping_reader","/sl/ping"); //ping SL to get number of loops pingSL(); //clear out any left over registrations from improper shutdown deregisterLoopPositions(); //wait for variable to be filled with ping plug while(loopcount==0); { // // println("Waiting for SL"); } //register event in sooperlooper // // println("LoopCount:" + loopcount); int i; for(i=1;i<=loopcount;i++) { registerLoopPosition(i-1); // // println(i-1); } //register to recieve looplength from SL int j; for(j=1;j<=loopcount;j++) { registerLoopLength(j-1); // // println(j-1); } // Register to get updates of selected_loop_num registerSelectedLoop(); } void draw() { background(0); } //This works with the plug statements in the setup() and processes the selected loop and sends the data to the IPhone public void selected_loop_num_reader(int myint ,String mystring, float activeButtonNumber) { // // println("got a message"); // // println("Selected Loop: " + str(activeButtonNumber)); int i; for(i=1;i<=4;i++) //shut them all off { // // println("enter for"); toggleIPhoneButton(i,0); } toggleIPhoneButton(activeButtonNumber + 1,1); } //Works with the selected_loop_num_reader to make things a little cleaner void toggleIPhoneButton(float buttonnumber, float control_value) { String control_path; float derived_len; control_path="/1/toggle"+ str(int(buttonnumber)); // // println(control_path); OscMessage myMessage = new OscMessage(control_path); myMessage.add(control_value); /* send the message */ oscP5.send(myMessage, myIPhone); } //works with plug to send loop position to iphone. public void loop_pos_reader(int loopindex, String ctrl, float control_value) { // // println("loop info received" + loopindex + "," + ctrl + "," + control_value); sendPosToIPhone(loopindex,control_value); } //Don't know if there is a better way to do this than a case... public void loop_len_reader(int loopindex, String ctrl, float control_value) { switch(loopindex) { case 0: loop1_len = control_value; break; case 1: loop2_len = control_value; break; case 2: loop3_len = control_value; break; case 3: loop4_len = control_value; break; } // // println("loop len received" + loopindex + "," + ctrl + "," + control_value); } //This ping fires only when the processing script is fired up - so you need all your loops set up at the beginning. //It puts the number of loops into a variable called...loopcount. public void ping_reader(String hosturl , String version, int sl_loopcount) { // s:hosturl s:version i:loopcount // // println(hosturl); // // println(version); // // println(sl_loopcount); loopcount = sl_loopcount; } //This sends loop position to the iphone void sendPosToIPhone(int loopindex, float control_value) { String control_path; float derived_len; control_path = "/1/fader" + str(loopindex + 1); /* // // println("------->>>" + str(loopindex)); // // println(loop1_len); // // println(control_value); */ switch(loopindex){ case 0 : control_value = control_value / loop1_len; break; case 1 : control_value = control_value / loop2_len; break; case 2 : control_value = control_value / loop3_len; break; case 3 : control_value = control_value / loop4_len; break; } OscMessage myMessage = new OscMessage(control_path); myMessage.add(control_value); /* send the message */ oscP5.send(myMessage, myIPhone); } void mousePressed() { // } //deregister and quit when you press the q key. void keyPressed() { if(key =='q') { deregisterLoopPositions(); deregisterLoopLengths(); deregisterSelectedLoop(); exit(); } } void deregisterLoopPositions(){ int i; for(i=1;i<=loopcount;i++) { deregisterLoopPosition(i-1); // // println("deregistered" + str(i-1)); } } void deregisterLoopLengths(){ int i; for(i=1;i<=loopcount;i++) { deregisterLoopLength(i-1); //// // println(i-1); } } void deregisterLoopPosition(int loopindex) { String registerThis; String returnPath; ///sl/#/unregister_auto_update s:ctrl s:returl s:retpath registerThis = "/sl/"+ loopindex + "/unregister_auto_update"; returnPath = "/sl/" + loopindex + "/loop_pos"; /* in the following different ways of creating osc messages are shown by example */ OscMessage myMessage = new OscMessage(registerThis); // /sl/#/register_auto_update i:ms_interval s:ctrl s:returl s:retpath myMessage.add("loop_pos"); myMessage.add("osc.udp://127.0.0.1:12000"); /* add an int to the osc message */ myMessage.add(returnPath); /* send the message */ oscP5.send(myMessage, mySooperLooper); } void deregisterLoopLength(int loopindex) { String registerThis; String returnPath; ///sl/#/unregister_auto_update s:ctrl s:returl s:retpath registerThis = "/sl/"+ loopindex + "/unregister_auto_update"; returnPath = "/sl/" + loopindex + "/loop_len"; /* in the following different ways of creating osc messages are shown by example */ OscMessage myMessage = new OscMessage(registerThis); // /sl/#/register_auto_update i:ms_interval s:ctrl s:returl s:retpath myMessage.add("loop_len"); myMessage.add("osc.udp://127.0.0.1:12000"); /* add an int to the osc message */ myMessage.add(returnPath); /* send the message */ oscP5.send(myMessage, mySooperLooper); } void registerLoopPosition(int loopindex) { String registerThis; String returnPath; registerThis = "/sl/"+ loopindex + "/register_auto_update"; returnPath = "/sl/" + loopindex + "/loop_pos"; /* in the following different ways of creating osc messages are shown by example */ OscMessage myMessage = new OscMessage(registerThis); // /sl/#/register_auto_update i:ms_interval s:ctrl s:returl s:retpath myMessage.add("loop_pos"); myMessage.add(100); myMessage.add("osc.udp://127.0.0.1:12000"); /* add an int to the osc message */ myMessage.add(returnPath); /* send the message */ oscP5.send(myMessage, mySooperLooper); } void registerLoopLength(int loopindex) { String registerThis; String returnPath; registerThis = "/sl/"+ loopindex + "/register_auto_update"; returnPath = "/sl/" + loopindex + "/loop_len"; /* in the following different ways of creating osc messages are shown by example */ OscMessage myMessage = new OscMessage(registerThis); // /sl/#/register_auto_update i:ms_interval s:ctrl s:returl s:retpath myMessage.add("loop_len"); myMessage.add(100); myMessage.add("osc.udp://127.0.0.1:12000"); /* add an int to the osc message */ myMessage.add(returnPath); /* send the message */ oscP5.send(myMessage, mySooperLooper); } void registerSelectedLoop() { //String registerThis; String returnPath; returnPath = "/sl/selected_loop_num"; /* in the following different ways of creating osc messages are shown by example */ OscMessage myMessage = new OscMessage("/register_auto_update"); // /register_auto_update s:ctrl i:ms_interval s:returl s:retpath myMessage.add("selected_loop_num"); myMessage.add(100); myMessage.add("osc.udp://127.0.0.1:12000"); /* add an int to the osc message */ myMessage.add(returnPath); /* send the message */ oscP5.send(myMessage, mySooperLooper); } void deregisterSelectedLoop() { String registerThis; String returnPath; returnPath = "/sl/selected_loop_num"; /* in the following different ways of creating osc messages are shown by example */ OscMessage myMessage = new OscMessage("/unregister_auto_update"); // /register_auto_update s:ctrl i:ms_interval s:returl s:retpath myMessage.add("selected_loop_num"); myMessage.add("osc.udp://127.0.0.1:12000"); /* add an int to the osc message */ myMessage.add(returnPath); /* send the message */ oscP5.send(myMessage, mySooperLooper); } void pingSL() { /* in the following different ways of creating osc messages are shown by example */ OscMessage myMessage = new OscMessage("/ping"); myMessage.add("osc.udp://127.0.0.1:12000"); /* add an int to the osc message */ myMessage.add("/sl/ping"); /* send the message */ oscP5.send(myMessage, mySooperLooper); } /* incoming osc message are forwarded to the oscEvent method. */ //uncomment all the below stuff if you want to see all messages in the output window. void oscEvent(OscMessage theOscMessage) { /* // print the address pattern and the typetag of the received OscMessage */ //// print("### received an osc message."); //// print(" addrpattern: "+theOscMessage.addrPattern()); // // println(" typetag: "+theOscMessage.typetag()); }