-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsequencer.eel
546 lines (491 loc) · 10.8 KB
/
sequencer.eel
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
////////////////////////////////////////////////
// sequencer.eel - EELSynth SynthAPI Sequencer
// Copyright (C) 2010-2011 David Olofson
////////////////////////////////////////////////
module sequencer;
import "math";
import "synthapi" as S;
import "io" as io;
export constant EV_CONTROL = 0;
export constant EV_DETACH = 1;
export constant EV_INSTRUMENT = 2;
export constant EV_CCONTROL = 3;
procedure checkpq(pq)
{
for local i = 0, sizeof pq - 2
if pq[i][0] > pq[i + 1][0]
{
print("ERROR! Out of order events in the priority queue!\n");
for local j = 0, sizeof pq - 1
{
if j == i or j == i + 1
print(">>>");
else
print(" ");
print(j, ":\t", pq[j][0], ", ", pq[j][1], "\n");
}
print("---------\n");
}
}
function pqinsert(pq, ev)
{
local when = ev[0];
/* local max = sizeof pq - 1;
if max < 1
{
if not max
if when <= pq[0][0]
{
insert(pq, 0, ev);
return 0;
}
pq.+ ev;
return sizeof pq - 1;
}
if when >= pq[max][0]
{
pq.+ ev;
return sizeof pq - 1;
}
local min, local pivot = 0;
while max != min
{
pivot = (integer)((min + max) / 2);
local ts = pq[pivot][0];
if ts == when
{
insert(pq, pivot, ev); // Shortcut!
return pivot;
}
else if when > ts and pivot != min
min = pivot;
else
max = pivot;
}
if when >= pq[pivot][0]
pivot += 1;
insert(pq, pivot, ev);
return pivot;
*/
for local i = 0, sizeof pq - 1
if pq[i][0] > when
{
insert(pq, i, ev);
return i;
}
pq.+ ev;
return sizeof pq - 1;
}
function trackSerialize(self, indent)
{
local b = dstring [];
local s = self.sequence;
local lastt = nil;
local ic = self.icontrols;
if sizeof ic
{
b.+ indent;
b.+ "IC(";
for local i = 0, sizeof ic - 1
b.+ (string)key(ic, i) + "," +
(string)index(ic, i) + ", ";
b.+ ");\n";
}
if sizeof s
lastt = s[0][0];
b.+ indent;
for local i = 0, sizeof s - 1
{
local ev = s[i];
if ev[0] != lastt // Different timestamp ==> new line!
{
lastt = ev[0];
b.+ "\n";
b.+ indent;
}
switch ev[1]
case EV_CONTROL
b.+ "C(";
case EV_DETACH
b.+ "D(";
case EV_INSTRUMENT
b.+ "I(";
case EV_CCONTROL
b.+ "CC(";
b.+ (string)ev[0];
b.+ ",";
switch ev[1]
case EV_CONTROL
b.+ (string)ev[2] + "," +
(string)ev[3] + "," +
(string)ev[4];
case EV_DETACH
b.+ (string)ev[2];
case EV_INSTRUMENT
b.+ "\"" + ev[2] + "\",\"" +
(string)ev[3] + "\"";
case EV_CCONTROL
b.+ (string)ev[2] + "," +
(string)ev[3];
/* if ev[0] != lastt
{
// Different timestamp ==> new line!
lastt = ev[0];
b.+ ");\n";
b.+ indent;
}
else*/
b.+ "); ";
}
b.+ "\n";
return b;
}
// Add a sequencer track
function AddTrack(seq)
{
local tr = table [
.target nil,
.play true,
.sequence [], // Event database
.now -1, // Current time (pulses)
.playpos 0, // Playback position (index)
.icontrols table [],
// Connect to control interface 'target'.
procedure Connect(self, target)
{
self.target = target;
}
// Move play position to 'time' (pulses).
procedure Skip(self, time)
{
//FIXME: Smarter algorithm...
self.now = time;
self.playpos = 0;
local s = self.sequence;
for local pp = 0, sizeof s - 1
if s[pp][0] >= time
{
self.playpos = pp;
break;
}
if not self.now
self:InitControls();
}
// Apply initial control values
procedure InitControls(self)
{
local ic = self.icontrols;
for local i = 0, sizeof ic - 1
self.target.Control(-1, key(ic, i),
index(ic, i));
}
// UnGATE and detach any playing voices!
procedure StopAllNotes(self)
{
self.target:Control(-1, S.GATE, 0.);
self.target:Detach(-1);
}
// Play from .now to 'time' (pulses).
procedure Play(self, time)
{
local s = self.sequence;
local pp = self.playpos;
while pp < sizeof s
{
local ev = s[pp];
if ev[0] >= time
break;
if self.play
self:Issue(ev);
pp += 1;
}
self.now = time;
self.playpos = pp;
}
//TODO: Use a selection table instead, with various selection tools + various
//TODO: operations to perform on the selection - GIMP/PhotoShop style!
procedure Delete(self)[pos, to]
{
if not sizeof self.sequence
return;
if specified pos
local i0, local i1 = pos;
else
i0, i1 = 0, sizeof self.sequence;
if specified to
i1 = to;
i0 = 0 >| i0 |< sizeof self.sequence;
i1 = i0 >| i1 |< sizeof self.sequence;
delete(self.sequence, i0, i1 - i0);
//FIXME: Untested...
if self.playpos > i0
{
if self.playpos < i1
self.playpos = i0;
else
self.playpos -= i1 - i0;
}
}
// Pass events to the target
procedure Issue(self, ev)
{
local tt = self.target;
switch ev[1]
case EV_CONTROL
tt:Control(ev[2, 3, 4]);
case EV_DETACH
tt:Detach(ev[2]);
case EV_INSTRUMENT
tt:Instrument(ev[2, 3]);
case EV_CCONTROL
tt:CControl(ev[2, 3]);
}
// Handle incoming events
procedure Feed(self, ev)
{
local e = ev;
local s = self.sequencer;
// Feed live evenst through to synths
if s.thru
self:Issue(e);
// Recording?
if not s.record
return; // Nope, we're done!
// Event filters
if e[1] < sizeof s.efilter
if s.efilter[e[1]]
return;
if e[1] == EV_CONTROL
if e[3] < sizeof s.cfilter
if s.cfilter[e[3]]
return;
// Recording but not playing? Set initial controls!
// NOTE:
// Voice controls become channel wide, ie "-1"!
if not s.play
{
if e[1] != EV_CONTROL
return;
self.icontrols[e[3]] = e[4];
return;
}
// Quantization
if s.quantize
{
e = clone e;
e[0] = floor((e[0] + s.quantize / 2) /
s.quantize) * s.quantize;
}
// Looping
if s.loop
{
e[0] -= s.start;
e[0] %= s.loop - s.start;
e[0] += s.start;
}
// Insert in track!
self:Insert(e);
}
procedure Insert(self, ev)
{
if pqinsert(self.sequence, ev) < self.playpos
self.playpos += 1;
}
// SynthAPI interface to Feed()
procedure Control(self, vvoice, cport, value)
{
self:Feed([self.sequencer.now,
EV_CONTROL, vvoice, cport, value]);
}
procedure Detach(self, vvoice)
{
self:Feed([self.sequencer.now,
EV_DETACH, vvoice]);
}
procedure Instrument(self, name)[preset]
{
if specified preset
local pres = preset;
else
pres = "Default";
self:Feed([self.sequencer.now,
EV_INSTRUMENT, name, pres]);
}
procedure CControl(self, cport, value)
{
self:Feed([self.sequencer.now,
EV_CCONTROL, cport, value]);
}
// File/data I/O
.Serialize trackSerialize
];
tr.sequencer (=) seq;
seq.tracks.+ tr;
return tr;
}
///////////////////////////////////////////////////////////////
// Indonesian synth project demo song
///////////////////////////////////////////////////////////////
procedure SongStep(seq)
{
local st = seq.tracks;
for local i = 0, sizeof st - 1
st[i].play = false;
switch seq.songpos
case 0
st[0, 2].play = true;
case 1
st[0, 1, 2].play = true;
case 2
st[0, 1, 2, 3].play = true;
case 3
st[0, 1, 2, 5].play = true;
case 4
st[0, 1, 4, 6].play = true;
case 5
st[0, 1, 2].play = true;
case 6
st[0, 1, 6].play = true;
seq.songpos += 1;
}
///////////////////////////////////////////////////////////////
export function New
{
local t = table [
// Settings
.record true, // Recording?
.play true, // Playing back?
.thru true, // Pass events through to target?
.tempo 1000, // Pulses per second
.beat 480, // Pulses per beat
.measure 1920, // Pulses per measure
.start 0, // Start/loop start time
.loop 0, // Loop end time (exclusive), or 0
.loopreset false, // Stop all notes when restarting loop
/////////////////////////////////////
//.songpos 0,
//.SongStep SongStep,
/////////////////////////////////////
.quantize 120, // Record time quantization
// Record filter
.efilter vector_u8 [],
.cfilter vector_u8 [],
.metro 480, // Metronome period
.metrotick 0, // Set by Process() if there was a
// metronome tick. 1 for normal, 2 for
// first tick in measure.
.tracks [], // Sequencer tracks (interfaces + state)
// State
.now -1, // Playback timestamp
.AddTrack AddTrack,
// Move play position to 'time' (pulses).
procedure Skip(self, time)
{
local tr = self.tracks;
for local i = 0, sizeof tr - 1
tr[i]:Skip(time);
self.now = time;
}
// Play from .now to 'time' (pulses).
procedure Play(self, time)
{
if self.metro
{
local mt = self.metro;
local ms = self.measure;
local lasttime = self.now;
if floor(time / mt) != floor(lasttime / mt)
if floor(time / ms) !=
floor(lasttime / ms)
self.metrotick = 2;
else
self.metrotick = 1;
}
local tr = self.tracks;
for local i = 0, sizeof tr - 1
tr[i]:Play(time);
self.now = time;
}
// Stop (unGATE and Detach()) all playing notes in all tracks.
procedure StopAllNotes(self)
{
local tr = self.tracks;
for local i = 0, sizeof tr - 1
tr[i]:StopAllNotes();
}
// Process all tracks for 'dt' seconds!
procedure Process(self, dt)
{
self.metrotick = 0;
if not self.play
return;
local time = self.now + self.tempo * dt;
if self.loop
{
time -= self.start;
time %= self.loop - self.start;
time += self.start;
}
if time < self.now
{
// Loop!
self:Play(self.loop); // Last few...
if self.loopreset
self:StopAllNotes();
self:Skip(self.start);
///////////////////////////////////////////////////////////////
// self:SongStep();
///////////////////////////////////////////////////////////////
}
self:Play(time);
}
// Convert pulses to bar:beat:pulse notation
function Pulses2BBP(self, t)
{
local bar = floor(t / self.measure);
t -= bar * self.measure;
local beat = floor(t / self.beat);
t -= beat * self.beat;
local pulse = floor(t + .5);
local buf = dstring [];
buf.+ (dstring)bar;
buf.+ ':';
buf.+ (dstring)(beat + 1);
buf.+ ':';
buf.+ (dstring)pulse;
return buf;
}
function Serialize(self)
{
local b = dstring [];
b.+ "Generator(\"EELSynth 0.1\");\n";
b.+ "Format(\"ESS\", 0);\n";
b.+ "Tempo(" + (dstring)self.tempo + ");\n";
b.+ "TimeSig(" + (dstring)self.beat + "," +
(dstring)self.measure + ");\n";
b.+ "Loop(" + (dstring)self.start + "," +
(dstring)self.loop + ");\n";
for local i = 0, sizeof self.tracks - 1
{
if not sizeof self.tracks[i].sequence
continue;
b.+ "\nTrack(", (dstring)i, ");\n";
b.+ self.tracks[i]:Serialize("\t");
}
return b;
}
procedure Deserialize(self, song)
{
local b = dstring [];
local f = file ["sequencer_ESS_0.eel", "rb"];
//FIXME: Somehow extract and check the Format() item, to
//FIXME: pick the right header for parsing the file.
b.+ io.read(f, sizeof f);
b.+ song;
b.+ "}\n";
local sm = compile(b);
sm.Process(self);
}
];
return t;
}