-
Notifications
You must be signed in to change notification settings - Fork 0
/
TuioDemo.pde
339 lines (276 loc) · 10.7 KB
/
TuioDemo.pde
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
//<>// //<>//
import java.util.concurrent.ConcurrentHashMap;
import java.util.Iterator;
import themidibus.*; //Import the library //<>//
//import java.util.Map;
import controlP5.*;
ControlP5 cp5;
Panel panel;
MidiBus mainBus, perfBus;
ConcurrentHashMap<Integer, MuObject> muObjectMap = new ConcurrentHashMap<Integer, MuObject>();
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);
}
boolean calibrationMode = false;
int beatValue = 0;
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);
beatValue = value;
}
void delay(int time) {
int current = millis();
while (millis () < current+time) Thread.yield();
}
// import the TUIO library
import TUIO.*;
// declare a TuioProcessing client
TuioProcessing tuioClient;
// these are some helper variables which are used
// to create scalable graphical feedback
float cursor_size = 15;
float object_size = 60;
float table_size = 760;
float scale_factor = 1;
PFont font;
float obj_size = object_size*scale_factor;
float cur_size = cursor_size*scale_factor;
PImage calibrationBg;
PImage[] gates = new PImage[36];
boolean verbose = false; // print console debug messages
boolean callback = true; // updates only after callbacks
void setup()
{
calibrationBg = loadImage("calibration.png");
calibrationBg.resize(displayWidth, displayHeight);
for(int i=0; i <= 35; i++)
gates[i] = loadImage("gates/gate" + i + ".png");
// GUI setup
//noCursor();
size(displayWidth, displayHeight, JAVA2D);
noStroke();
fill(0);
//MidiBus.list(); // List all available Midi devices on STDOUT. This will show each device's index and name.
// Parent In Out
// | | |
mainBus = new MidiBus(this, -1, "loopMIDI Port", "PSYN"); // Create a new MidiBus using the device index to select the Midi input and output devices respectively.
perfBus = new MidiBus(this, "loopMIDI Port Out", "loopMIDI Port Performance", "PSYN"); // Create a new MidiBus using the device index to select the Midi input and output devices respectively.
// periodic updates
//if (!callback) {
frameRate(60);
// loop();
//} else noLoop(); // or callback updates
font = createFont("Arial", 18);
scale_factor = height/table_size;
// finally we create an instance of the TuioProcessing client
// since we add "this" class as an argument the TuioProcessing class expects
// an implementation of the TUIO callback methods in this class (see below)
tuioClient = new TuioProcessing(this);
MuMusic.initMinor();
//MuMusic.initMajor();
// MuMusic.initPentatonic();
cp5 = new ControlP5(this);
panel = new Panel();
createKnobs();
createGroupKnobs();
}
// within the draw method we retrieve an ArrayList of type <TuioObject>, <TuioCursor> or <TuioBlob>
// from the TuioProcessing client and then loops over all lists to draw the graphical feedback.
//Iterator<Map.Entry<Integer,MuObject>> it;
Iterator it;
void draw()
{
if (calibrationMode == true)
background(calibrationBg);
else background(255);
textFont(font, 18*scale_factor);
try {
it = muObjectMap.keySet().iterator();
while (it.hasNext ()) {
muObjectMap.get(it.next()).display();
}
}
catch (Exception e) {
print(e);
}
ArrayList<TuioCursor> tuioCursorList = tuioClient.getTuioCursorList();
for (int i=0; i<tuioCursorList.size (); i++) {
TuioCursor tcur = tuioCursorList.get(i);
int pit = tcur.getScreenX(width)/7;
//mainBus.sendNoteOn(channel, pit, velocity); // Send a Midi noteOn
ArrayList<TuioPoint> pointList = tcur.getPath();
if (pointList.size()>0) {
stroke(0, 0, 255);
TuioPoint start_point = pointList.get(0);
for (int j=0; j<pointList.size (); j++) {
TuioPoint end_point = pointList.get(j);
line(start_point.getScreenX(width), start_point.getScreenY(height), end_point.getScreenX(width), end_point.getScreenY(height));
start_point = end_point;
}
stroke(192, 192, 192);
fill(192, 192, 192);
ellipse( tcur.getScreenX(width), tcur.getScreenY(height), cur_size, cur_size);
fill(0);
text(""+ tcur.getCursorID(), tcur.getScreenX(width)-5, tcur.getScreenY(height)+5);
}
}
ArrayList<TuioBlob> tuioBlobList = tuioClient.getTuioBlobList();
for (int i=0; i<tuioBlobList.size (); i++) {
TuioBlob tblb = tuioBlobList.get(i);
stroke(0);
fill(0);
pushMatrix();
translate(tblb.getScreenX(width), tblb.getScreenY(height));
rotate(tblb.getAngle());
ellipse(-1*tblb.getScreenWidth(width)/2, -1*tblb.getScreenHeight(height)/2, tblb.getScreenWidth(width), tblb.getScreenWidth(width));
popMatrix();
fill(255);
text(""+tblb.getBlobID(), tblb.getScreenX(width), tblb.getScreenX(width));
}
}
// --------------------------------------------------------------
// these callback methods are called whenever a TUIO event occurs
// there are three callbacks for add/set/del events for each object/cursor/blob type
// the final refresh callback marks the end of each TUIO frame
// called when an object is added to the scene
void addTuioObject(TuioObject tobj) {
if (verbose) println("add obj "+tobj.getSymbolID()+" ("+tobj.getSessionID()+") "+tobj.getX()+" "+tobj.getY()+" "+tobj.getAngle());
if (muObjectMap.containsKey(tobj.getSymbolID()) == false)
muObjectMap.put(tobj.getSymbolID(), createMuObject(tobj));
}
// called when an object is moved
void updateTuioObject (TuioObject tobj) {
if (verbose) println("set obj "+tobj.getSymbolID()+" ("+tobj.getSessionID()+") "+tobj.getX()+" "+tobj.getY()+" "+tobj.getAngle()
+" "+tobj.getMotionSpeed()+" "+tobj.getRotationSpeed()+" "+tobj.getMotionAccel()+" "+tobj.getRotationAccel());
try {
muObjectMap.get(tobj.getSymbolID()).update();
}
catch (Exception e) {
print("Exception!!");
}
}
// called when an object is removed from the scene
void removeTuioObject(TuioObject tobj) {
if (verbose) println("del obj "+tobj.getSymbolID()+" ("+tobj.getSessionID()+")");
muObjectMap.get(tobj.getSymbolID()).dismiss();
muObjectMap.remove(tobj.getSymbolID());
}
// --------------------------------------------------------------
// called when a cursor is added to the scene
void addTuioCursor(TuioCursor tcur) {
if (verbose) println("add cur "+tcur.getCursorID()+" ("+tcur.getSessionID()+ ") " +tcur.getX()+" "+tcur.getY());
//redraw();
playNote(tcur);
}
int[] notesOn = new int[10];
// called when a cursor is moved
void updateTuioCursor (TuioCursor tcur) {
if (verbose) println("set cur "+tcur.getCursorID()+" ("+tcur.getSessionID()+ ") " +tcur.getX()+" "+tcur.getY()
+" "+tcur.getMotionSpeed()+" "+tcur.getMotionAccel());
//redraw();
playNote(tcur);
}
void playNote(TuioCursor tcur) {
float sliderX = panel.slider.x(panel.slider.getPosition());
float sliderY = panel.slider.y(panel.slider.getPosition());
if (tcur.getScreenX(width) >= sliderX && tcur.getScreenX(width) <= sliderX + panel.slider.getWidth() &&
tcur.getScreenY(height) >= sliderY && tcur.getScreenY(height) <= sliderY + panel.slider.getHeight()) {
float pit = -(sliderX - tcur.getScreenX(width)) / 2;
float velocity = (sliderY - tcur.getScreenY(height) + 100);
float[] pos = {
pit, 100- velocity
};
panel.slider.setArrayValue(pos);
if((int)pit >= 0 && (int)pit < MuMusic.currentScale.length)
pit = MuMusic.currentScale[(int)pit];
//mainBus.sendNoteOff(0, notesOn[tcur.getCursorID()], 100);
int lastNote = notesOn[tcur.getCursorID()];
notesOn[tcur.getCursorID()] = (int)pit;
mainBus.sendNoteOn(0, (int)pit, (int)velocity);
mainBus.sendNoteOff(0, lastNote, 100);
} else {
mainBus.sendNoteOff(0, notesOn[tcur.getCursorID()], 10);
}
}
// called when a cursor is removed from the scene
void removeTuioCursor(TuioCursor tcur) {
if (verbose) println("del cur "+tcur.getCursorID()+" ("+tcur.getSessionID()+")");
//int pit = tcur.getScreenX(width)/7;
mainBus.sendNoteOff(0, notesOn[tcur.getCursorID()], 10);
}
// --------------------------------------------------------------
// called when a blob is added to the scene
void addTuioBlob(TuioBlob tblb) {
if (verbose) println("add blb "+tblb.getBlobID()+" ("+tblb.getSessionID()+") "+tblb.getX()+" "+tblb.getY()+" "+tblb.getAngle()+" "+tblb.getWidth()+" "+tblb.getHeight()+" "+tblb.getArea());
//redraw();
}
// called when a blob is moved
void updateTuioBlob (TuioBlob tblb) {
if (verbose) println("set blb "+tblb.getBlobID()+" ("+tblb.getSessionID()+") "+tblb.getX()+" "+tblb.getY()+" "+tblb.getAngle()+" "+tblb.getWidth()+" "+tblb.getHeight()+" "+tblb.getArea()
+" "+tblb.getMotionSpeed()+" "+tblb.getRotationSpeed()+" "+tblb.getMotionAccel()+" "+tblb.getRotationAccel());
//redraw()
}
// called when a blob is removed from the scene
void removeTuioBlob(TuioBlob tblb) {
if (verbose) println("del blb "+tblb.getBlobID()+" ("+tblb.getSessionID()+")");
//redraw()
}
// --------------------------------------------------------------
// called at the end of each TUIO frame
void refresh(TuioTime frameTime) {
if (verbose) println("frame #"+frameTime.getFrameID()+" ("+frameTime.getTotalMilliseconds()+")");
if (callback) redraw();
}
void keyPressed() {
if (key == 'c') {
calibrationMode = !calibrationMode;
}
if (key == 'p') {
//int status_byte = 0xC0;
int status_byte = 192;
// This is the status byte for a program change
int channel = 0;
// We'll use channel 0
int byte1 = 2;
// This will be the preset you are sending with your program change
int byte2 = 0;
// This is not used for program change so ignore it and set it to 0
mainBus.sendMessage(status_byte, channel, byte1, byte2);
//Send the custom message
//mainBus.sendMessage(new byte[]{(byte)240,(byte)127,(byte)127,(byte)1,(byte)1,(byte)0,(byte)0,(byte)4,(byte)0,(byte)247});
}
if (key == 'r') {
mainBus.sendMessage(176, 0, 117, 127);
mainBus.sendMessage(176, 0, 117, 0);
}
}
void polygon(float x, float y, float radius, int npoints) {
float angle = TWO_PI / npoints;
beginShape();
for (float a = 0; a < TWO_PI; a += angle) {
float sx = x + cos(a) * radius;
float sy = y + sin(a) * radius;
vertex(sx, sy);
}
endShape(CLOSE);
}