/
ferrous_wheel.pde
executable file
·436 lines (355 loc) · 12.9 KB
/
ferrous_wheel.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/**
* Ferrous Wheel
* By Matt Mets
* Sponsored by the Children's Museum of Pittsburgh 2009
*
* Feb 2013: Modified a bit by Gregory Witt of said Museum
* to accomodate a few years worth of changes to Processing,
* some new hardware, and a different OS (Windows) (sorry).
* See "windows_notes/notes.txt" for installation details.
*
*/
// Libraries
import codeanticode.gsvideo.*; // GSVideo for motion capture
import themidibus.*; // MIDI Bus for output
import proxml.*; // XML settings file
import java.io.*;
import java.util.*;
// Constants
int windowHeight = 240;
int windowWidth = 320;
color black = color(0);
color white = color(255);
int numPixels;
// Configuration options
// White balance correction factors for red and blue channels
// determine experimentally by measuring distance from green
float redCorrection = .88;
float blueCorrection = 1.13;
int threshold = 104; // Set the brightness threshold value
// Start and end points for the detection line
int startX = 195;
int startY = 87;
int endX = 83;
int endY = 137;
// MIDI info
int midiChannel = 0; // MIDI channel to play notes on
int midiVelocity = 127; // Velocity (change this based on blob size you say?)
int maxNotes = 9; // Max # of notes allowed (don't overload the synth) (not implemented correctly)
// Use a pentatonic scale
int midiNotes[] = {42, 44, 46, 49, 51,
54, 56, 58, 61, 63,
66, 68 };
// Global objects
GSCapture video; // The video source
MidiBus myBus; // MIDI output device
int lineData [ ]; // Data along the detection line
String logFileName; // Name of the log file
java.util.List blobList = new LinkedList();
// Representation of a found magnet
class blob {
public blob(int center_, int width_, int pitch_) {
center = center_;
width = width_;
pitch = pitch_;
current = true;
}
int center;
int width;
int pitch;
boolean current;
}
PrintWriter appendWriter(String filename) {
try {
File file = saveFile(filename);
OutputStream output = new FileOutputStream(file, true);
return createWriter(output);
}
catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("Couldn't create a writer for " +
filename);
}
}
// Log a message to both the console and a file
void log(String message) {
println(message);
PrintWriter file = appendWriter(logFileName);
file.print(millis() + " " + message + "\n");
file.flush();
file.close();
}
// If possible, read in the settings file.
void readSettings(){
proxml.XMLInOut xmlInOut = new XMLInOut(this);
try{
xmlInOut.loadElement("settings.xml");
}catch(Exception e){
log("LOAD_SETTINGS_FAIL text=\"" + e + "\"");
}
}
// Stupid xml callback, launched by readSettings()
void xmlEvent(proxml.XMLElement _x) {
proxml.XMLElement settings = _x;
proxml.XMLElement setting;
for(int i = 0; i < settings.countChildren();i++){
setting = settings.getChild(i);
if( setting.getElement().equals("sense_line")) {
startX = setting.getIntAttribute("startX");
startY = setting.getIntAttribute("startY");
endX = setting.getIntAttribute("endX");
endY = setting.getIntAttribute("endY");
log("LOAD_SETTINGS startX=" + startX + " startY=" + startY + " endX=" + endX + " endY=" + endY);
}
else if( setting.getElement().equals("thresholding")) {
threshold = setting.getIntAttribute("threshold");
blueCorrection = setting.getFloatAttribute("blueCorrection");
redCorrection = setting.getFloatAttribute("redCorrection");
log("LOAD_SETTINGS threshold=" + threshold + " blueCorrection=" + blueCorrection + " redCorrection=" + redCorrection);
}
}
}
// Write out the settings file based on the global variables
void writeSettings(){
// xml element to store and load the configuration settings
proxml.XMLElement settings = new proxml.XMLElement("settings");
proxml.XMLInOut xmlInOut = new XMLInOut(this);
// Sense line: Where to look in the image
proxml.XMLElement sense_line = new proxml.XMLElement("sense_line");
sense_line.addAttribute("startX", startX);
sense_line.addAttribute("startY", startY);
sense_line.addAttribute("endX", endX);
sense_line.addAttribute("endY", endY);
settings.addChild(sense_line);
// Thresholding: How to interpret brightness levels
proxml.XMLElement thresholding = new proxml.XMLElement("thresholding");
thresholding.addAttribute("threshold", threshold);
thresholding.addAttribute("redCorrection", redCorrection);
thresholding.addAttribute("blueCorrection", blueCorrection);
settings.addChild(thresholding);
xmlInOut.saveElement(settings,"settings.xml");
}
void setup() {
// Construct a name for the current log file
logFileName = "logs/"
+ year() + "." + month() + "." + day()
+ "." +hour() + ":" + minute() + ".txt";
log("START date=" + year() + "/" + month() + "/" + day()
+ " time=" + hour() + ":" + minute() + ":" + second());
// Set up the window
size(windowWidth, windowHeight);
strokeWeight(5);
// Try to load the settings file
readSettings();
// Uses the default video input, see the reference if this causes an error
// For the installation, the on-board camera is disabled in the BIOS so it can't
// get in the way.
video = new GSCapture(this, width, height); // was ,24 fps but my camera no support
video.start();
// Calculate the total number of pixels on the screen
numPixels = video.width * video.height;
// List available MIDI devices, use no input and loopMIDI for output.
// This isn't 'right' but it should work as long as loopMIDI shows up as #3 in the list.
MidiBus.list();
myBus = new MidiBus(this, -1, 3); // Create a new MIDI device
// Tell me you're working
myBus.sendNoteOn(midiChannel, 64, midiVelocity); // Send a Midi noteOn
delay(200);
myBus.sendNoteOff(midiChannel, 64, midiVelocity); // Send a Midi noteOff
// using-the-mousewheel-scrollwheel-in-processing taken from:
// http://processinghacks.com/hacks:using-the-mousewheel-scrollwheel-in-processing
// @author Rick Companje
addMouseWheelListener(new java.awt.event.MouseWheelListener() {
public void mouseWheelMoved(java.awt.event.MouseWheelEvent evt) {
mouseWheel(evt.getWheelRotation());
}});
}
// http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
void getLine(int x0, int y0, int x1, int y1) {
int Dx = x1 - x0;
int Dy = y1 - y0;
boolean steep = (abs(Dy) >= abs(Dx));
if (steep) {
int temp = x0;
x0 = y0;
y0 = temp;
temp = x1;
x1 = y1;
y1 = temp;
// recompute Dx, Dy after swap
Dx = x1 - x0;
Dy = y1 - y0;
}
int xstep = 1;
if (Dx < 0) {
xstep = -1;
Dx = -Dx;
}
int ystep = 1;
if (Dy < 0) {
ystep = -1;
Dy = -Dy;
}
int TwoDy = 2*Dy;
int TwoDyTwoDx = TwoDy - 2*Dx; // 2*Dy - 2*Dx
int E = TwoDy - Dx; //2*Dy - Dx
int y = y0;
int xDraw, yDraw;
lineData = new int [ abs(x1 - x0) ];
int n = 0;
for (int x = x0; x != x1; x += xstep) {
if (steep) {
xDraw = y;
yDraw = x;
} else {
xDraw = x;
yDraw = y;
}
// Grab the data from the line
color pixelA = pixels[xDraw*windowWidth + yDraw + 1];
color pixelB = pixels[(xDraw + 1)*windowWidth + yDraw];
color pixelC = pixels[xDraw*windowWidth + yDraw];
color pixelD = pixels[(xDraw + 1)*windowWidth + yDraw + 1];
lineData[n] = color((red(pixelA) + red(pixelB) + red(pixelC) + red(pixelD))/4,
(green(pixelA) + green(pixelB) + green(pixelC) + green(pixelD))/4,
(blue(pixelA) + blue(pixelB) + blue(pixelC) + blue(pixelD))/4);
// Draw the line in
pixels[xDraw*windowWidth + yDraw] = color(255,0,0);
// next
if (E > 0) {
E += TwoDyTwoDx; //E += 2*Dy - 2*Dx;
y = y + ystep;
} else {
E += TwoDy; //E += 2*Dy;
}
n++;
}
}
void draw() {
try {
if (video.available()) {
video.read(); // Grab a frame of video
image(video,0,0); // Copy the video to the display screen
// Load the pixel array
loadPixels();
// Grab the current sense line out of the image
getLine(startY, startX, endY, endX);
// Look for colored magnets and black ledger lines
for (int i = 0; i < lineData.length; i++) {
// Grab the color data
color pix = lineData[i];
float p_red = red(pix)*redCorrection;
float p_green = green(pix);
float p_blue = blue(pix)*blueCorrection;
// Look for things that have color and might be a magnet
// We define that as having a difference between any two channels (red, green or blue)
// greater than a set threshold. This was designed as a first-pass filter against noisy
// pixel data, but a better approach would be to average larger areas and then operate on that.
if( ( abs(p_red - p_green) > threshold ||
abs(p_red - p_blue) > threshold ||
abs(p_green - p_blue) > threshold )
&& (brightness(pix) > 10)
&& (brightness(pix) < 245))
{
lineData[i] = white;
}
else {
lineData[i] = black;
}
// Look for things that might be the black ledger line
// Just look for something that is sufficiently dark.
if( brightness(pix) < 30) {
lineData[i] = color(255,0,0);
}
}
// Draw the thresholded line at the bottom of the screen, just for visual identification.
for(int i = 0; i < lineData.length; i++) {
pixels[i+1+windowWidth*(windowHeight - 4)] = lineData[i];
pixels[i+1+windowWidth*(windowHeight - 3)] = lineData[i];
pixels[i+1+windowWidth*(windowHeight - 2)] = lineData[i];
}
// Refresh the blob list by marking everything old and then searching for current/new ones
for (Iterator it = blobList.iterator(); it.hasNext(); ) {
blob currentBlob = (blob)it.next();
currentBlob.current = false;
}
// Now, find the center of each block of darkness, and use that as a MIDI note:
int noteStart = 0;
boolean counting = false;
for(int i = 0; i < lineData.length; i++) {
if (counting && lineData[i] == black) {
// Done counting
counting = false;
int center = (i - noteStart)/2 + noteStart;
int width = i - noteStart;
if (width > 2)
{
boolean found = false;
// If blob part of list, mark it current.
for (Iterator it = blobList.iterator(); it.hasNext(); ) {
blob currentBlob = (blob)it.next();
if( abs(currentBlob.center - center) <= 2 ) {
currentBlob.current = true;
found = true;
}
}
// Otherwise, add it to the list.
if( found == false) {
// Limit the max number of notes in flight to 9
if ( blobList.size() > maxNotes ) {
log("DROPPED_NOTE center=" + center + " width=" + width);
}
else {
int pitch = midiNotes[(int)( ((float)center/lineData.length)*midiNotes.length )];
blobList.add(new blob(center, width, pitch));
myBus.sendNoteOn(midiChannel, pitch, midiVelocity); // Send a Midi noteOn
log("NOTE_ON center=" + center + " width=" + width + " pitch=" + pitch);
}
}
}
}
// TODO: hack here
else if (!counting && lineData[i] == white) {
// Start counting
counting = true;
noteStart = i;
}
}
updatePixels();
// Search through the list of known blobs, and remove ones that have disappeared
for (Iterator it = blobList.iterator(); it.hasNext(); ) {
blob currentBlob = (blob)it.next();
if( currentBlob.current == false ) {
log("NOTE_OFF center=" + currentBlob.center + " width=" + currentBlob.width + " pitch=" + currentBlob.pitch);
it.remove();
myBus.sendNoteOff(midiChannel, currentBlob.pitch, midiVelocity); // Send a Midi nodeOff
}
}
}
}
catch (Exception e)
{
log("EXCEPTION text=\"" + e + "\"");
}
}
// Right-click to set start of line, left-click to set end
void mousePressed()
{
if (mouseButton == LEFT) {
startX = mouseX;
startY = mouseY;
log("UPDATE_SETTINGS startX=" + startX + " startY=" + startY);
writeSettings();
} else if (mouseButton == RIGHT) {
endX = mouseX;
endY = mouseY;
log("UPDAT_SETTINGS endX=" + endX + " endY=" + endY);
writeSettings();
}
}
// Update the brighness threshold when the mouse wheel is spun
void mouseWheel(int delta) {
threshold += delta;
log("UPDATE_SETTINGS threshold=" + threshold);
writeSettings();
}