osctools/oscplayer/oscplayer.pde
2021-11-11 11:21:52 +01:00

119 lines
2.3 KiB
Text

String baseaddress="/oscplayer/";
import oscP5.*;
import netP5.*;
OscP5 oscP5;
NetAddress myRemoteLocation;
Table data;
TableRow datarow;
int rowid=0;
long time=0;
long starttime=0;
public enum Playmode {
STOP, LOOP;
}
Playmode playmode=Playmode.LOOP;
boolean play=false;
void setup() {
size(400,400);
frameRate(60);
data = loadTable("data.csv", "header, csv");
println(data.getRowCount() + " total rows in data");
println("Header:");
for (String title : data.getColumnTitles()) {
print(title+", ");
}
println();
datarow = data.getRow(rowid); //get first row
oscP5 = new OscP5(this,12000); //Port for input OSC Messages
myRemoteLocation = new NetAddress("127.0.0.1",7000); //connect to remote, IP, Port
restart(); //start automatically
}
void draw() {
time=millis()-starttime;
datarow = data.getRow(rowid);
background(0);
if (play){
text("Playing",10,10);
}else{
text("Stopped",10,10);
}
text("Time="+time+" ms",100,10);
text("row="+rowid,200,10);
switch(playmode) {
case STOP:
text("Mode=Stop",10,30);
break;
case LOOP:
text("Mode=Loop",10,30);
break;
}
if (play)
{
long rowtime=datarow.getInt("time"); //time of the currently loaded row (next to send)
if (time>=rowtime) //time to send next row
{
//Send all columns
for (String title : data.getColumnTitles()) {
sendOSC(title,datarow.getFloat(title));
}
rowid++; //load next row
}
if (rowid>=data.getRowCount()) { //reached end
switch(playmode) {
case STOP:
play=false;
break;
case LOOP:
restart();
break;
}
}
}
}
void restart() {
play=true;
starttime=millis();
rowid=0;
}
void sendOSC(String name,float data) {
OscMessage myMessage = new OscMessage(baseaddress+name);
//println("Sending name="+name+" with data="+data);
myMessage.add(data);
oscP5.send(myMessage, myRemoteLocation);
}
/* incoming osc message are forwarded to the oscEvent method. */
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());
}