-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAccelController.ck
158 lines (145 loc) · 3.21 KB
/
AccelController.ck
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
// a few global variables
// same as rate serial communication speed in max (48000/9600=5)
5::samp => dur Tupdate;
5 => int channel;
0.0 => float baseFreq;
1.0 => float amp;
5000.0 => float filt;
float sinDist;
float ppp;
int currC;
int currX;
int prevX;
int currY;
int prevY;
int currZ;
int prevZ;
int currV;
int prevV;
// argument sets which midi channel to listen to
if (me.args() ) {
me.arg(0) => Std.atoi => channel;
}
// Oscillators and mix
/*TriOsc s => */Gain g => LPF f => Pan2 p => dac;
p => Gain g2 => NRev r => dac;
// CC object
CC cc;
cc.start();
GAMELAN gam;
gam.connect(g);
// level to determine when shred exits
0.08 => float outGain;
// other levels
20000 => f.freq;
0.08 => g.gain;
1.0 => g2.gain;
1.0 => r.gain;
//0.0 => s.gain;
1.0 => p.pan;
3.2::second => dur T;
T - (now%T) => now;
// important arrays for scale, note durations, octaves and 'meter stretch'
[0, 2, 3, 7, 7, 9, 10, 12] @=> int scale[];
[0.25, .5, .75, 1.0, 1.25] @=> float durs[];
[2.0, 4.0, 4.0, 8.0, 8.0, 16.0] @=> float octaves[];
[1.0, 2.0, 4.0] @=> float durMult[];
/*fun void noController()
{
octaves[Std.rand2(0,octaves.cap()-1)] => float oct;
durMult[Std.rand2(0, durMult.cap()-1)] => float durMod;
0 => int counter;
0.0 => float fadeIn;
2.0 => float oscGain;
0.0 => s.gain;
while ((oscGain - 1) > outGain)
{
durs[Std.rand2(0, durs.cap()-1)]*durMod::second => T;
Std.mtof(scale[Std.rand2(0, scale.cap()-1)]+36)*oct => s.freq;
Std.rand2f(-1.0, 1.0) => p.pan;
T => now;
if (counter < 40) {
counter++;
0.025 +=> fadeIn;
fadeIn => s.gain;
//<<<fadeIn>>>;
} else {
Math.pow(oscGain, 0.96) => oscGain;
oscGain - 1 => s.gain;
//<<<oscGain - 1>>>;
}
}
}*/
fun void doIt()
{
while (true)
{
if (amp > 10)
{
gam.generic(baseFreq, amp, amp/2);
Tupdate => T;
} else {
Tupdate => T;
}
T => now;
}
}
fun void listen()
{
int avX;
int avY;
int avZ;
int avV;
while (true)
{
cc.getChan() => currC;
if (currC == channel)
{
// get accelerometer data
cc.getAccX() => currX;
cc.getAccY() => currY;
cc.getAccZ() => currZ;
cc.getDist() => currV;
//<<<currC, currX, currY, currZ, currV>>>;
((currX+prevX)/2) => avX;
((currY+prevY)/2) => avY;
((currZ+prevZ)/2) => avZ;
((currV+prevV)/2) => avV;
// Y tilt determines filter frequency
Math.min(Std.fabs(((avY / 127.0)*20000)), 19900.0) => filt;
Math.max(filt, baseFreq*1.25) => filt => f.freq;
// X tilt determines panning
((avX / 127.0)*1650+110) => baseFreq;
//velocity from accelerometer causes slight pitch bend
(Std.fabs(avZ - 60.0)) => amp;
//baseFreq *(1 + bend) => s.freq;
// distance determines gain
if (amp > 10) gam.generic(baseFreq, amp, amp/10);
Math.pow(((avV - 40) / 128.0), 2.0) => sinDist => g.gain;
//<<<bend, baseFreq, filt, ppp, sinDist>>>;
currX => prevX;
currY => prevY;
currZ => prevZ;
currV => prevV;
}
Tupdate => now;
}
}
fun void play()
{
if (channel == 5) {
me.exit();
//noController();
} else {
spork ~ listen();
doIt();
}
}
// fill variables with real values before messing with them
cc.getAccX() => currX => prevX;
cc.getAccY() => currY => prevY;
cc.getAccZ() => currZ => prevZ;
cc.getDist() => currV => prevV;
// start the fun
play();
//me.exit();