This repository has been archived by the owner on Dec 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmlr.ck
288 lines (253 loc) · 4.91 KB
/
mlr.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
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
public class Mlr extends Launchpad
{
120 => float bpm;
8 => int length; // sequence's length
4 => int quantize; // number of ticks per beat in the sequence
Track track[8];
0 => int learn; // are we in learn mode ?
0 => int currentStep;
0 => int currentQuanta;
time timeOfCurrentQuanta;
fun void init(float bpm, int length, int quantize)
{
bpm => this.bpm;
length => this.length;
quantize => this.quantize;
for (0 => int i; i<8; i++){
track[i].init(this);
}
pitch();
}
fun void sequencer()
{
while (1) {
for (0 => currentQuanta; currentQuanta < quantize ; currentQuanta++) {
if (currentQuanta == 0) {
setColor(-1,7,127);
} else {
setColor(-1,7,0);
}
now => timeOfCurrentQuanta;
// for each track, play the current quanta's event if there is one
for (0 => int i; i<8; i++) {
track[i].sequence[currentStep][currentQuanta] => int ev;
if (track[i].isLoaded() && track[i].isPlaying() && ev != -1) {
track[i].play(ev);
}
}
quantaLength() => now;
}
(currentStep+1) % length => currentStep;
}
}
fun dur quantaLength()
{
return 60::second / bpm / quantize;
}
fun void pitch()
{
for (0 => int i; i<8; i++)
{
track[i].pitch();
}
}
fun void load(int t, string fname, int beats, int group, float gain, UGen output)
{
track[t].load(fname, beats, group, t, gain, output);
}
fun void start(int port)
{
connect(port);
spork ~ sequencer();
spork ~ displayShred();
listen();
}
fun void keyEvent(int row, int col, int press)
{
// discard if release event
if (!press) return;
// control event
if (row == -1) {
// (un)toggle learn mode
if (col == 0) {
!learn => learn;
setColor(-1,0,127*learn);
} else if (col == 7) {
length-1 => currentStep;
quantize-1 => currentQuanta;
}
return;
}
track[row] @=> Track t;
// discard if the track is not loaded
if (!t.isLoaded()) return;
// scene event: start/stop/clear
if (col == 8) {
if (learn) {
// clear sequence
t.clearSequence();
} else {
// start/stop
if (t.isPlaying()) {
t.stop();
} else {
groupStop(row);
t.play(0);
}
}
return;
}
// matrix event
// record if in learn mode
if (learn) {
// quantize to the closest quanta
currentStep => int s;
currentQuanta => int q;
if (now - timeOfCurrentQuanta > quantaLength()/2) {
// quantize in the next quanta
if (q == quantize-1) {
(s+1) % length => s;
}
(q+1) % quantize => q;
}
col => t.sequence[s][q];
}
groupStop(row);
t.play(col);
}
fun void groupStop(int t)
{
track[t].group => int g;
for (0 => int i; i<8; i++)
{
if (i != t && track[i].group == g)
{
track[i].stop();
rowOff(i);
}
}
}
// turn off all the lights for one row
fun void rowOff(int row)
{
for (0 => int i; i<9; i++)
{
setColor(row,i,0);
}
}
// turn the current step on + the scene button
fun void rowOn(int row, int col)
{
for (0 => int i; i<8; i++)
{
if (i == col) {
setColor(row,i,127);
} else {
setColor(row,i,0);
}
}
setColor(row,8,127);
}
fun void displayShred()
{
Track @ tr;
while (1) {
for (0 => int r; r<8; r++) {
track[r] @=> tr;
if (tr.playing) {
rowOn(r,tr.currentStep);
if (tr.nextUpdate <= now) {
tr.nextUpdate + tr.stepLength() => tr.nextUpdate;
(tr.currentStep + 1) % 8 => tr.currentStep;
}
} else {
rowOff(r);
}
}
10::ms => now;
}
}
}
class Track
{
Mlr @ parent;
UGen @ output;
SndBuf buf;
int beats;
int group;
int row;
int currentStep;
time nextUpdate;
int playing;
int sequence[][];
fun void init(Mlr parent)
{
parent @=> this.parent;
int seq[parent.length][parent.quantize] @=> sequence;
clearSequence();
}
fun void clearSequence()
{
for (0 => int s; s < parent.length; s++) {
for (0=>int q; q<parent.quantize; q++) {
-1 => sequence[s][q];
}
}
}
// load a loop
fun void load(string fname, int beats, int group, int row, float gain, UGen output)
{
fname => buf.read;
true => buf.loop;
beats => this.beats;
group => this.group;
row => this.row;
gain => buf.gain;
output @=> this.output;
0 => playing;
pitch();
}
fun int isLoaded()
{
return buf.samples() != 0;
}
fun int isPlaying()
{
return playing;
}
fun dur trackLength()
{
return (beats / parent.bpm * 60)::second;
}
fun dur stepLength()
{
return trackLength() / 8;
}
// pitch the buffer so it has the right duration
fun void pitch()
{
if (!isLoaded()) return;
buf.length() / trackLength() => buf.rate;
}
fun void play(int step)
{
step % 8 => currentStep;
step * buf.samples() / 8 => buf.pos;
now + stepLength() => nextUpdate;
if (!playing) {
buf => output;
true => playing;
}
}
fun void stop()
{
if (playing) {
buf !=> output;
false => playing;
}
}
fun void setOutput(UGen u)
{
u @=> output;
}
}