-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScratchOSC.pde
225 lines (190 loc) · 6.54 KB
/
ScratchOSC.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
/**
* ScratchOSC Android Client
* Renders Realtime ScratchOSC Data via OSC from a Turntable running ScratchOSC
* This was developed as Part of the ArtHackDay at 319 Scholes, Brooklyn, NY
* January 26-28, 2012
* Written By: Daniel Moore
*/
//Copyright (C) 2012 Daniel Moore
//
//Permission is hereby granted, free of charge, to any person obtaining a copy of
//this software and associated documentation files (the "Software"), to deal in
//the Software without restriction, including without limitation the rights to
//use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
//of the Software, and to permit persons to whom the Software is furnished to do
//so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in all
//copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE.
import android.app.Notification;
import android.app.NotificationManager;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorManager;
import android.hardware.SensorEventListener;
import java.net.*;
import oscP5.*;
import netP5.*;
// Setup vibration globals:
NotificationManager gNotificationManager;
Notification gNotification;
long[] gVibrate = {
0, 250, 50, 125, 50, 62
};
//-----------------------------------------------------------------------------------------
// Screen Data:
float sw, sh;
// Font Data:
String[] fontList;
PFont androidFont;
// Setup variables for the SensorManager, the SensorEventListeners,
// the Sensors, and the arrays to hold the resultant sensor values:
LockScreen scLock;
OscP5 oscP5;
NetAddress myRemoteLocation;
float rad;
float step, frame_R, frame_L, progressL, progressR, rotation_L, rotation_R, fade;
void setup() {
scLock = new LockScreen(this.getApplicationContext());
size(screenWidth, screenHeight);
sw = screenWidth;
sh = screenHeight;
rad = sh/2;
// Set this so the sketch won't reset as the phone is rotated:
orientation(LANDSCAPE);
fade = 1.0;
scLock.Start();
frameRate(20);
/* starts oscP5, listening for incoming messages at port 12000
* this should automatically grab the IP of your device
* please note you need to be connected to WiFi
*/
oscP5 = new OscP5(this, getLocalIpAddress(), 8319);
/* 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.
*/
myRemoteLocation = new NetAddress(getLocalIpAddress(), 8319);
smooth();
}
void draw() {
float X = sw / 4.0;
float Y = sh / 2.0;
step = 100;
// Fill canvas grey
background(255);
// Draw L circle
fill(230, 230, 230);
noStroke();
ellipse( X, Y, rad, rad );
// Draw R circle
fill(230, 230, 230);
noStroke();
ellipse( 3*X, Y, rad, rad );
progressL = (frameCount*step)/300000;
stroke(220);
strokeWeight(20);
arc(X, Y, 350, 350, PI+HALF_PI, 4*PI);
stroke(180);
arc(X, Y, 350, 350, PI+HALF_PI, (2*PI*progressL) + PI+HALF_PI);
progressR = (frameCount*step)/300000; //
stroke(220);
strokeWeight(20);
arc(3*X, Y, 350, 350, PI+HALF_PI, 4*PI);
stroke(180);
arc(3*X, Y, 350, 350, PI+HALF_PI, (2*PI*progressR) + PI+HALF_PI);
/*turns on the device vibrator if the fader it hit */
if(fade == 0.0){
gNotificationManager.notify(1, gNotification);
fade = 1.0;
}
stroke(50, 50, 250);
strokeWeight(25);
frame_L = frame_R;
rotation_R = map(frame_R, 0, 1800, 0, TWO_PI);
rotation_L = map(frame_L, 0, 1800, 0, TWO_PI);
line(X, Y, cos(rotation_L) * (X*0.92) + X, sin(rotation_L) * (Y*0.92) + Y);
line(3*X, Y, cos(rotation_R) * (X*0.92) + 3*X, sin(rotation_R) * (Y*0.92) + Y);
}
/*
* ScratchML OSC Message Format
*
* /scratch/record/right - Right Turntable float pos
* /scratch/record/left - Left Turntable float pos
* /scratch/mixer/fader - Crossfader int 0 = left
* 1 = right
*
*/
void oscEvent(OscMessage theOscMessage) {
if (theOscMessage.checkAddrPattern("/scratch/record/right")==true) {
float firstValue = theOscMessage.get(0).floatValue();
frame_R = firstValue;
return;
}
if (theOscMessage.checkAddrPattern("/scratch/record/left")==true) {
float firstValue = theOscMessage.get(0).floatValue();
frame_L = firstValue;
//println("left"+frame_L);
return;
}
if (theOscMessage.checkAddrPattern("/scratch/mixer/fader")==true) {
float firstValue = theOscMessage.get(0).floatValue();
fade = firstValue;
return;
}
}
void onResume() {
super.onResume();
gNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Create our Notification that will do the vibration:
gNotification = new Notification();
// Set the vibration:
gNotification.vibrate = gVibrate;
}
public String getLocalIpAddress() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
return inetAddress.getHostAddress().toString();
}
}
}
}
catch (SocketException ex) {
// Log.e(LOG_TAG, ex.toString());
}
return null;
}
class LockScreen {
PowerManager pm;
Context context;
WakeLock wl;
public LockScreen(Context parent) {
this.context = parent;
pm =(PowerManager) parent.getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
}
public void Start() {
wl.acquire();
}
public void Stop() {
wl.release();
}
}