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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
|
local sndfile = require "sndfile"
local bit = require "bit"
local ffi = require "ffi"
-- According to LuaJIT docs, it makes sense to cache
-- the FFI namespace
local C = ffi.C
-- Make table.new() available (a LuaJIT extension)
require "table.new"
--
-- Define C functions for benchmarking (POSIX libc)
--
ffi.cdef[[
typedef long time_t;
struct timespec {
time_t tv_sec; /* seconds */
long tv_nsec; /* nanoseconds */
};
typedef enum {
CLOCK_REALTIME = 0,
CLOCK_MONOTONIC = 1,
CLOCK_PROCESS_CPUTIME_ID = 2,
CLOCK_THREAD_CPUTIME_ID = 3,
CLOCK_MONOTONIC_RAW = 4,
CLOCK_REALTIME_COARSE = 5,
CLOCK_MONOTONIC_COARSE = 6,
CLOCK_BOOTTIME = 7
} clockid_t;
int clock_gettime(clockid_t clk_id, struct timespec *tp);
]]
-- measure time required to execute fnc()
function benchmark(fnc)
local t1 = ffi.new("struct timespec[1]")
local t2 = ffi.new("struct timespec[1]")
C.clock_gettime("CLOCK_PROCESS_CPUTIME_ID", t1)
fnc()
C.clock_gettime("CLOCK_PROCESS_CPUTIME_ID", t2)
local t1_ms = t1[0].tv_sec*1000 + t1[0].tv_nsec/1000000
local t2_ms = t2[0].tv_sec*1000 + t2[0].tv_nsec/1000000
print("Elapsed CPU time: "..(t2_ms - t1_ms).."ms")
end
--
-- Define the Lua FFI part of Applause's C core.
-- These functions and types are defined in applause.c
--
ffi.cdef[[
enum applause_audio_state {
APPLAUSE_AUDIO_OK = 0,
APPLAUSE_AUDIO_INTERRUPTED,
APPLAUSE_AUDIO_XRUN
};
enum applause_audio_state applause_push_sample(double sample_double);
int applause_midi_velocity_getvalue(int note, int channel);
int applause_midi_note_getvalue(int channel);
int applause_midi_cc_getvalue(int control, int channel);
]]
-- Sample rate
-- This is overwritten by the C core
samplerate = 44100
-- Time units: Convert between time and sample numbers
-- These are functions, so we can round the result
-- automatically
function sec(x) return math.floor(samplerate*(x or 1)) end
function msec(x) return sec((x or 1)/1000) end
-- The clock signal:
-- In order to support (re)using the same stream more than
-- once in a complex stream without recalculating everything,
-- tick closures must be shared among all usages of a stream.
-- A clock signal is necessary to "trigger" the recalculation
-- of a stream's current sample.
-- The clock signal is a boolean oscillating between true and
-- false.
local clock_signal = false
-- FIXME: Inconsistent naming. Use all-lower case for functions
-- and methods?
function DeriveClass(base)
local class = {base = base}
if base then
-- we cannot derive metamethod tables, so we
-- copy all relevant metamethods
for _, m in pairs{"len", "tostring",
"add", "sub", "mul", "div",
"mod", "pow", "unm",
"concat", "lt", "le", "gc"} do
class["__"..m] = base["__"..m]
end
end
-- Metamethods should work even on root class tables
-- However, this way we cannot use metatables to track the
-- class inheritance. This is done using the `base` field.
setmetatable(class, class)
function class:new(...)
-- Try to call the parent constructor
local obj = base and base:new() or {}
obj.base = self
setmetatable(obj, self)
-- Allow constructors to return something else
-- than an instance of the class.
return obj.ctor and obj:ctor(...) or obj
end
-- The call metamethod is synonymous to :new()
class.__call = class.new
-- All objects have the class table as their metatable,
-- so it must look into the class and possibly invoke metamethods
-- on the base class.
-- A simple `class.__index = base` does not work since
-- we want indexing to create IndexStreams.
-- NOTE: __index methods get the original table looked up
-- as their self argument (e.g. the stream object).
function class:__index(key)
if type(key) == "string" then
return rawget(class, key) or (base and base[key])
end
-- non-string keys create IndexStreams
return IndexStream:new(self, key)
end
-- Checks whether object is instance of other_class.
-- Will work with class templates as well.
-- This is not using getmetatable() since class metatables
-- point to themselves (see above).
function class:instanceof(other_class)
repeat
-- Better use rawequal() in case we support the
-- __eq metamethod someday.
if rawequal(self, other_class) then return true end
self = self.base
until not self
return false
end
return class
end
-- Stream base class
Stream = DeriveClass()
function Stream:ctor(value)
self.value = tonumber(value) or 0
end
-- There is Stream:instanceof(), but testing Stream.is_a_stream
-- is sometimes faster (for generator functions) and can be done
-- without knowing that the table at hand is an object
Stream.is_a_stream = true
-- A stream, produces an infinite number of the same value by default
-- (eternal quietness by default)
function Stream:tick()
return function()
return self.value
end
end
Stream.streams = {}
function Stream:reset()
for i = 1, #self.streams do
self.streams[i]:reset()
end
end
-- Explicitly clock-sync a stream.
-- FIXME: This should be done automatically by an optimizer stage.
function Stream:sync()
return SyncedStream:new(self)
end
function Stream:rep(repeats)
return RepeatStream:new(self, repeats)
end
function Stream:map(fnc)
return MapStream:new(self, fnc)
end
-- Register all unary functions from the math package
-- as stream operations/methods (creates a stream that calls
-- the function on every sample)
for _, fnc in pairs{"abs", "acos", "asin", "atan",
"ceil", "cos", "cosh", "deg",
"exp", "floor", "log", "log10",
"rad", "sin", "sinh", "sqrt",
"tan", "tanh"} do
Stream[fnc] = function(self)
return self:map(math[fnc])
end
end
function Stream:bnot()
return self:map(bit.bnot)
end
-- Register all binary operators of the "bit" module
for _, name in pairs{"bor", "band", "bxor",
"lshift", "rshift", "arshift",
"rol", "ror"} do
local fnc = bit[name]
Stream[name] = function(self, v)
return self:map(function(x) return fnc(x, v) end)
end
end
-- Scalar operations
-- In contrast to stream operations (based on ZipStream),
-- these work only with scalars and do not
-- extend the stream length
function Stream:add(n)
return self:map(function(x) return x+n end)
end
function Stream:sub(n)
return self:map(function(x) return x-n end)
end
function Stream:mul(n)
return self:map(function(x) return x*n end)
end
Stream.gain = Stream.mul
function Stream:div(n)
return self:map(function(x) return x/n end)
end
function Stream:mod(n)
return self:map(function(x) return x%n end)
end
function Stream:pow(n)
return self:map(function(x) return x^n end)
end
function Stream:clip(min, max)
min = min or -1
max = max or 1
local math_min = math.min
local math_max = math.max
return self:map(function(x)
return math_min(math_max(x, min), max)
end)
end
-- Scale [-1,+1] signal to [lower,upper]
-- lower is optional and defaults to 0
function Stream:scale(v1, v2)
local lower = v2 and v1 or 0
local upper = v2 or v1
return self:map(function(x)
return (x + 1)*(upper - lower)/2 + lower
end)
end
function Stream:scan(fnc)
return ScanStream:new(self, fnc)
end
function Stream:fold(fnc)
return FoldStream:new(self, fnc)
end
function Stream:zip(fnc, ...)
return ZipStream:new(fnc, self, ...)
end
function Stream:sub(i, j)
return SubStream:new(self, i, j)
end
function Stream:ravel()
return RavelStream:new(self)
end
-- This is a linear resampler thanks to the
-- semantics of IndexStream
function Stream:resample(factor)
return self[iota(math.floor(self:len() * factor)):div(factor)]
end
--
-- Wave forms with names derived from ChucK:
-- Can be written freq:SawOsc() or Stream.SawOsc(freq)
-- depending on the use case. The latter form may
-- be useful for constant frequencies.
--
-- Ramp from 0 to 1
function Stream.Phasor(freq)
return ScanStream:new(freq, function(accu, f)
return ((accu or 0) + f/samplerate) % 1
end)
end
-- Saw tooth wave from -1 to 1
function Stream.SawOsc(freq)
return ScanStream:new(freq, function(accu, f)
return ((accu or 1) + 2*f/samplerate) % 2
end):sub(1)
end
function Stream.SinOsc(freq)
return Stream.Phasor(freq):mul(2*math.pi):sin()
end
-- Pulse between 0 and 1 in half a period (width = 0.5)
function Stream.PulseOsc(freq)
return Stream.Phasor(freq):map(function(x)
return x < 0.5 and 1 or 0
end)
end
function Stream.SqrOsc(freq)
return Stream.Phasor(freq):map(function(x)
return x < 0.5 and 1 or -1
end)
end
function Stream.TriOsc(freq)
local abs = math.abs
return Stream.SawOsc(freq):map(function(x)
return abs(x)*2 - 1
end)
end
--
-- Filter shortcuts.
-- They have their own classes
--
function Stream:LPF(freq)
return LPFStream:new(self, freq)
end
function Stream:HPF(freq)
return HPFStream:new(self, freq)
end
function Stream:BPF(freq, quality)
return BPFStream:new(self, freq, quality)
end
function Stream:BRF(freq, quality)
return BRFStream:new(self, freq, quality)
end
-- Bit crusher effect
function Stream:crush(bits)
bits = bits or 8
local floor = math.floor
return self:map(function(x)
return floor(x * 2^bits + 0.5) / 2^bits
end)
end
-- The len() method is the main way to get a stream's
-- length (at least in this code) and classes should overwrite
-- this method.
-- The __len metamethod is also defined but it currently cannot
-- work since Lua 5.1 does not consider a table's metamethod when
-- evaluating the length (#) operator.
function Stream:len()
return math.huge -- infinity
end
function Stream:play()
self:reset()
local tick = self:tick()
-- Make sure JIT compilation is turned on for the generator function
-- and all subfunctions.
-- This should not be necessary theoretically.
jit.on(true, true)
jit.on(tick, true)
-- Perform garbage collection cycle and tweak it
-- to be more realtime friendly.
-- FIXME: Since every stream that does not lag will have
-- times when it is idle, it may be clever to stop the
-- garbage collector and step it manually whenever
-- the Jack sample queue is full. However, how to guarantee
-- that we step it fast enough to prevent leaks?
collectgarbage("collect")
local old_pause = collectgarbage("setpause", 100)
local old_stepmul = collectgarbage("setstepmul", 100)
local state
repeat
-- Advance clock
clock_signal = not clock_signal
local sample = tick()
if not sample then break end
-- FIXME: What if the sample is not a number,
-- perhaps we should check that here
state = C.applause_push_sample(sample)
-- React to buffer underruns.
-- This is done here instead of in the realtime thread
-- even though it is already overloaded, so as not to
-- affect other applications in the Jack graph.
if state == C.APPLAUSE_AUDIO_XRUN then
io.stderr:write("WARNING: Buffer underrun detected\n")
end
until state == C.APPLAUSE_AUDIO_INTERRUPTED
collectgarbage("setpause", old_pause)
collectgarbage("setstepmul", old_stepmul)
if state == C.APPLAUSE_AUDIO_INTERRUPTED then
error("SIGINT received", 2)
end
end
-- implemented in applause.c
function Stream:fork()
error("C function not registered!")
end
function Stream:foreach(fnc)
self:reset()
local tick = self:tick()
while true do
clock_signal = not clock_signal
local sample = tick()
if not sample then break end
fnc(sample)
end
end
-- TODO: Use a buffer to improve perfomance (e.g. 1024 samples)
function Stream:save(filename, format)
if self:len() == math.huge then
error("Cannot save infinite stream")
end
local hnd = sndfile:new(filename, "SFM_WRITE",
samplerate, 1, format)
self:foreach(function(sample)
-- FIXME: What if the sample is not a number,
-- perhaps we should check that here
hnd:write(sample)
end)
hnd:close()
end
function Stream:totable()
if self:len() == math.huge then
error("Cannot serialize infinite stream")
end
local vector = table.new(self:len(), 0)
self:foreach(function(sample)
vector[#vector + 1] = sample
end)
return vector
end
-- Effectively eager-evaluates the stream returning
-- an array-backed stream.
function Stream:eval()
return VectorStream:new(self:totable())
end
function Stream:toplot(rows, cols)
rows = rows or 25
cols = cols or 80
local scaled = self:resample(cols / self:len())
:add(1):mul(rows/2):floor():totable()
local plot = {}
for i = 1, #scaled do
plot[i] = {}
for j = 1, rows do plot[i][j] = " " end
-- middle line (represents 0)
plot[i][math.ceil(rows/2)] = "-"
plot[i][scaled[i]] = "+" -- data point
-- connect with last data point
if i > 1 then
if scaled[i-1] < scaled[i] then
for j = scaled[i-1]+1, scaled[i]-1 do
plot[i][j] = "|"
end
elseif scaled[i-1] > scaled[i] then
for j = scaled[i-1]-1, scaled[i]+1, -1 do
plot[i][j] = "|"
end
end
end
end
local str = ""
for j = rows, 1, -1 do
for i = 1, cols do str = str..plot[i][j] end
str = str.."\n"
end
return str
end
function Stream:pipe(prog, vbufmode, vbufsize)
local hnd = io.popen(prog, "w")
hnd:setvbuf(vbufmode or "full", vbufsize)
self:foreach(function(sample)
hnd:write(sample, "\n")
end)
hnd:close()
end
function Stream:gnuplot()
if self:len() == math.huge then
error("Cannot plot infinite stream")
end
-- NOTE: We're not using Stream:pipe() here, so we can
-- efficiently calculate a time index.
-- FIXME: Using something like libplplot would be more
-- efficient
local hnd = io.popen("feedgnuplot --exit --lines --ymin -1 --ymax 1 --domain", "w")
hnd:setvbuf("full")
local second = sec()
local i = 1
self:foreach(function(sample)
hnd:write(i/second, " ", sample, "\n")
i = i + 1
end)
hnd:close()
end
-- Stream metamethods
-- NOTE: Currently non-functional since Lua 5.1 does not
-- consider metamethods when evaluating the length operator.
function Stream:__len() return self:len() end
function Stream:__tostring()
local t
if self:len() > 1024 then
t = self:sub(1, 1024):totable()
table.insert(t, "...")
else
t = self:totable()
end
for i = 1, #t do t[i] = tostring(t[i]) end
return "{"..table.concat(t, ", ").."}"
end
-- NOTE: Named addOp() and similar functions below
-- are necessary instead of lambdas so consecutive
-- operations can be collapsed by ZipStream (which
-- tests for function equivalence)
local function addOp(x1, x2) return x1+x2 end
function Stream.__add(op1, op2)
return ZipStream:new(addOp, op1, op2)
end
local function subOp(x1, x2) return x1-x2 end
function Stream.__sub(op1, op2)
return ZipStream:new(subOp, op1, op2)
end
local function mulOp(x1, x2) return x1*x2 end
function Stream.__mul(op1, op2)
return ZipStream:new(mulOp, op1, op2)
end
local function divOp(x1, x2) return x1/x2 end
function Stream.__div(op1, op2)
return ZipStream:new(divOp, op1, op2)
end
local function modOp(x1, x2) return x1%x2 end
function Stream.__mod(op1, op2)
return ZipStream:new(modOp, op1, op2)
end
local function powOp(x1, x2) return x1^x2 end
function Stream.__pow(op1, op2)
return ZipStream:new(powOp, op1, op2)
end
function Stream:__unm() return self:mul(-1) end
function Stream.__concat(op1, op2)
return ConcatStream:new(op1, op2)
end
-- FIXME: Length comparisions can already be written
-- elegantly - perhaps these operators should have
-- more APLish semantics instead?
-- However Lua practically demands these metamethods
-- (as well as __eq) to return booleans.
function Stream.__lt(op1, op2)
return op1:len() < op2:len()
end
function Stream.__le(op1, op2)
return op1:len() <= op2:len()
end
SyncedStream = DeriveClass(Stream)
function SyncedStream:ctor(stream)
self.streams = {stream}
end
function SyncedStream:reset()
self.syncedTick = nil
Stream.reset(self)
end
function SyncedStream:tick()
if not self.syncedTick then
local last_clock
local last_sample
local tick = self.streams[1]:tick()
self.syncedTick = function()
if clock_signal ~= last_clock then
last_clock = clock_signal
last_sample = tick()
end
return last_sample
end
end
return self.syncedTick
end
VectorStream = DeriveClass(Stream)
function VectorStream:ctor(vector)
self.vector = vector
end
function VectorStream:tick()
local vector = self.vector
local i = 0
return function()
i = i + 1
return vector[i]
end
end
function VectorStream:len()
return #self.vector
end
-- NOTE: A SndfileStream itself cannot currently be reused within
-- one high-level stream (i.e. UGen graph).
-- SndfileStream:sync() must be called to wrap it in a
-- synced stream manually.
-- FIXME: This will no longer be necessary when syncing
-- streams automatically in an optimization phase.
SndfileStream = DeriveClass(Stream)
function SndfileStream:ctor(filename)
-- FIXME: This fails if the file is not at the
-- correct sample rate. Need to resample...
self.handle = sndfile:new(filename, "SFM_READ")
end
function SndfileStream:reset()
self.handle:seek(0)
Stream.reset(self)
end
function SndfileStream:tick()
local handle = self.handle
return function()
return handle:read()
end
end
function SndfileStream:len()
return tonumber(self.handle.info.frames)
end
-- Sometimes it may be useful to explicitly close the file
-- handle behind a SndfileStream
function SndfileStream:close()
self.handle:close()
end
ConcatStream = DeriveClass(Stream)
function ConcatStream:ctor(...)
self.streams = {}
for _, v in ipairs{...} do
v = tostream(v)
if v:instanceof(ConcatStream) then
-- Optimization: Avoid redundant
-- ConcatStream objects
for _, s in ipairs(v.streams) do
table.insert(self.streams, s)
end
else
table.insert(self.streams, v)
end
end
-- all but the last stream must be finite
-- (it makes no sense to append something to
-- an infinite stream)
for i = 1, #self.streams - 1 do
if self.streams[i]:len() == math.huge then
error("Stream "..i.." is infinite")
end
end
end
function ConcatStream:tick()
local i = 1
local ticks = {}
for k = 1, #self.streams do
ticks[k] = self.streams[k]:tick()
end
return function()
while i <= #ticks do
local sample = ticks[i]()
if sample then return sample end
-- try next stream
i = i + 1
end
end
end
function ConcatStream:len()
local len = 0
-- if last stream is infinite, len will also be infinite
for _, stream in pairs(self.streams) do
len = len + stream:len()
end
return len
end
RepeatStream = DeriveClass(Stream)
function RepeatStream:ctor(stream, repeats)
self.streams = {tostream(stream)}
self.repeats = repeats or math.huge
end
function RepeatStream:tick()
local i = 1
local stream_tick = self.streams[1]:tick()
local repeats = self.repeats
return function()
while i <= repeats do
local sample = stream_tick()
if sample then return sample end
-- next iteration
i = i + 1
-- FIXME: The tick() method itself may be too
-- inefficient for realtime purposes.
-- Also, we may slowly leak memory.
stream_tick = self.streams[1]:tick()
end
end
end
function RepeatStream:len()
return self.streams[1]:len() * self.repeats
end
-- Ravel operation inspired by APL.
-- This removes one level of nesting from nested streams
-- (e.g. streams of streams), and is semantically similar
-- to folding the stream with the Concat operation.
RavelStream = DeriveClass(Stream)
function RavelStream:ctor(stream)
self.streams = {tostream(stream)}
end
function RavelStream:tick()
local stream_tick = self.streams[1]:tick()
local current_tick = nil
return function()
while true do
if current_tick then
local value = current_tick()
if value then return value end
current_tick = nil
end
local value = stream_tick()
-- NOTE: We don't use instanceof() here for performance
-- reasons
if type(value) == "table" and value.is_a_stream then
current_tick = value:tick()
else
return value
end
end
end
end
function RavelStream:len()
if self.streams[1]:len() == math.huge then
-- FIXME: Actually, it is possible that the stream
-- is infinite but consists only of empty streams.
-- In this case, tick() will be stuck in an infinite loop...
return math.huge
end
local len = 0
local t = self.streams[1]:totable()
for i = 1, #t do
len = len + (type(t[i]) == "table" and t[i].is_a_stream and
t[i]:len() or 1)
end
return len
end
IotaStream = DeriveClass(Stream)
function IotaStream:ctor(v1, v2)
if not v2 then
self.from = 1
self.to = v1 or math.huge
else
self.from = v1
self.to = v2
end
if self.from < 1 or self.to < 1 or
self.from > self.to then
error("Invalid iota range ["..self.from..","..self.to.."]")
end
end
function IotaStream:tick()
local i = self.from-1
return function()
if i >= self.to then return end
i = i + 1
return i
end
end
function IotaStream:len()
return self.to == math.huge and math.huge or
self.to - self.from + 1
end
-- i and j have the same semantics as in string.sub()
SubStream = DeriveClass(Stream)
function SubStream:ctor(stream, i, j)
self.streams = {tostream(stream)}
self.i = i
self.j = j or -1
local stream_len = self.streams[1]:len()
if self.i < 0 then self.i = self.i + stream_len + 1 end
if self.j < 0 then self.j = self.j + stream_len + 1 end
if self.i > stream_len or self.j > stream_len or
self.i > self.j then
error("Invalid sub-stream range ["..self.i..","..self.j.."]")
end
end
function SubStream:tick()
local tick = self.streams[1]:tick()
-- OPTIMIZE: Perhaps ask stream to skip the first
-- self.i-1 samples
for _ = 1, self.i-1 do tick() end
local i = self.i
return function()
if i > self.j then return end
i = i + 1
return tick()
end
end
function SubStream:len()
return self.j == math.huge and math.huge or
self.j - self.i + 1
end
-- FIXME: Will not work for non-samlpe streams
-- This should be split into a generic (index) and
-- sample-only (interpolate) operation
IndexStream = DeriveClass(Stream)
function IndexStream:ctor(stream, index_stream)
-- NOTE: For stream resetting to work and to simplify
-- future optimization passes, all streams are in the streams array
self.streams = {tostream(stream), tostream(index_stream)}
end
function IndexStream:tick()
local stream_tick = self.streams[1]:tick()
local index_tick = self.streams[2]:tick()
local stream_len = self.streams[1]:len()
-- avoid math table lookup at sample rate
local huge = math.huge
local floor = math.floor
local ceil = math.ceil
-- cache of samples generated by stream
local cache = {}
return function()
local index_sample = index_tick()
if not index_sample then return end
if index_sample < 1 or index_sample > stream_len or
index_sample == huge then
error("Index "..index_sample.." out of range")
end
local index_floor, index_ceil = floor(index_sample),
ceil(index_sample)
while #cache < index_ceil do
table.insert(cache, stream_tick())
end
-- applies linear interpolation if index_sample is
-- not an integer
return cache[index_floor] +
(cache[index_ceil] - cache[index_floor])*
(index_sample - index_floor)
end
end
function IndexStream:len()
-- Length of the indexing stream
return self.streams[2]:len()
end
MapStream = DeriveClass(Stream)
function MapStream:ctor(stream, fnc)
self.streams = {tostream(stream)}
self.fnc = fnc
end
function MapStream:tick()
local tick = self.streams[1]:tick()
return function()
local sample = tick()
return sample and self.fnc(sample)
end
end
function MapStream:len()
return self.streams[1]:len()
end
ScanStream = DeriveClass(Stream)
function ScanStream:ctor(stream, fnc)
self.streams = {tostream(stream)}
self.fnc = fnc
end
function ScanStream:tick()
local tick = self.streams[1]:tick()
local last_sample = nil
return function()
local sample = tick()
if not sample then return end
last_sample = self.fnc(last_sample, sample)
return last_sample
end
end
function ScanStream:len()
return self.streams[1]:len()
end
FoldStream = DeriveClass(Stream)
function FoldStream:ctor(stream, fnc)
self.streams = {tostream(stream)}
self.fnc = fnc
end
function FoldStream:tick()
local tick = self.streams[1]:tick()
return function()
local l, r
while true do
r = tick()
if not r then break end
l = l and self.fnc(l, r) or r
end
return l
end
end
function FoldStream:len()
return self.streams[1]:len() > 0 and 1 or 0
end
-- ZipStream combines any number of streams into a single
-- stream using a function. This is the basis of the "+"
-- and "*" operations.
ZipStream = DeriveClass(Stream)
function ZipStream:ctor(fnc, ...)
self.fnc = fnc
self.streams = {}
for _, v in ipairs{...} do
v = tostream(v)
if v:instanceof(ZipStream) and v.fnc == fnc then
-- Optimization: Avoid redundant
-- ZipStream objects
for _, s in ipairs(v.streams) do
table.insert(self.streams, s)
end
else
table.insert(self.streams, v)
end
end
end
function ZipStream:tick()
local running = true
local ticks = {}
for i = 1, #self.streams do
ticks[i] = self.streams[i]:tick()
end
if #ticks == 2 then
-- 2 streams are common, so use an unrolled
-- version here
return function()
if not running then return end
local sample1, sample2 = ticks[1](), ticks[2]()
if not sample1 then
running = sample2
return sample2
elseif not sample2 then
-- have sample1, keep running
return sample1
end
return self.fnc(sample1, sample2)
end
else
return function()
if not running then return end
local result = nil
for i = 1, #ticks do
local sample = ticks[i]()
if sample then
result = result and self.fnc(result, sample)
or sample
end
end
-- if all streams have ended, `result` will be nil
running = result
return result
end
end
end
function ZipStream:len()
local max = 0
for _, stream in pairs(self.streams) do
max = math.max(max, stream:len())
end
return max
end
NoiseStream = DeriveClass(Stream)
function NoiseStream:tick()
local random = math.random
return function()
return random()*2 - 1
end
end
--
-- MIDI Support
--
-- Velocity of NOTE ON for a specific note on a channel
MIDIVelocityStream = DeriveClass(Stream)
function MIDIVelocityStream:ctor(note, channel)
self.note = note
assert(0 <= self.note and self.note <= 127,
"MIDI note out of range (0 <= x <= 127)")
self.channel = channel or 1
assert(1 <= self.channel and self.channel <= 16,
"MIDI channel out of range (1 <= x <= 16)")
end
-- This is for calling from external code (e.g. from
-- streams supporting MIDI natively)
function MIDIVelocityStream.getValue(note, channel)
-- NOTE: The native function assert() for invalid
-- notes or channels to avoid segfaults
assert(0 <= note and note <= 127,
"MIDI note out of range (0 <= x <= 127)")
assert(1 <= channel and channel <= 16,
"MIDI channel out of range (1 <= x <= 16)")
return C.applause_midi_velocity_getvalue(note, channel)
end
function MIDIVelocityStream:tick()
local note = self.note
local channel = self.channel
return function()
return C.applause_midi_velocity_getvalue(note, channel)
end
end
-- Stream of integer words representing the last MIDI note
-- triggered on a channel with its corresponding velocity
-- (of the NOTE ON message).
-- The MIDI note is the lower byte and the velocity the
-- upper byte of the word.
MIDINoteStream = DeriveClass(Stream)
function MIDINoteStream:ctor(channel)
self.channel = channel or 1
assert(1 <= self.channel and self.channel <= 16,
"MIDI channel out of range (1 <= x <= 16)")
end
-- This is for calling from external code (e.g. from
-- streams supporting MIDI natively)
function MIDINoteStream.getValue(channel)
-- NOTE: The native function assert() for invalid
-- notes or channels to avoid segfaults
assert(1 <= channel and channel <= 16,
"MIDI channel out of range (1 <= x <= 16)")
return C.applause_midi_note_getvalue(channel)
end
function MIDINoteStream:tick()
local channel = self.channel
return function()
return C.applause_midi_note_getvalue(channel)
end
end
MIDICCStream = DeriveClass(Stream)
function MIDICCStream:ctor(control, channel)
self.control = control
self.channel = channel or 1
assert(0 <= self.control and self.control <= 127,
"MIDI control number out of range (0 <= x <= 127)")
assert(1 <= self.channel and self.channel <= 16,
"MIDI channel out of range (1 <= x <= 16)")
end
-- This is for calling from external code (e.g. from
-- streams supporting MIDI natively)
function MIDICCStream.getValue(control, channel)
-- NOTE: The native function assert() for invalid
-- notes or channels to avoid segfaults
assert(0 <= control and control <= 127,
"MIDI control number out of range (0 <= x <= 127)")
assert(1 <= channel and channel <= 16,
"MIDI channel out of range (1 <= x <= 16)")
return C.applause_midi_cc_getvalue(control, channel)
end
function MIDICCStream:tick()
local control = self.control
local channel = self.channel
return function()
return C.applause_midi_cc_getvalue(control, channel)
end
end
-- MIDI primitives
-- There are only 128 possible MIDI notes,
-- so their frequencies can and should be cached.
-- We do this once instead of on-demand, so the lookup
-- table consists of consecutive numbers.
local mtof_cache = table.new(128, 0)
for note = 0, 127 do
-- MIDI NOTE 69 corresponds to 440 Hz
mtof_cache[note] = 440*math.pow(2, (note - 69)/12)
end
-- Convert from MIDI note to frequency
-- NOTE: mtof() can handle the words as generated by MIDINoteStream
function mtof(note)
return mtof_cache[bit.band(note, 0xFF)]
end
function Stream:mtof() return self:map(mtof) end
-- Convert from frequency to closest MIDI note
function ftom(freq)
-- NOTE: math.log/2 is a LuaJIT extension
return math.floor(12*math.log(freq/440, 2) + 0.5)+69
end
function Stream:ftom() return self:map(ftom) end
-- primitives
function tostream(v)
if type(v) == "table" then
if v.is_a_stream then return v end
-- assume to be vector
return VectorStream:new(v)
else
return Stream:new(v)
end
end
function iota(...) return IotaStream:new(...) end
--
-- Filters
--
--[==[
--
-- Non-working FIR filters (FIXME)
--
-- Normalized Sinc function
local function Sinc(x)
return x == 0 and 1 or
math.sin(2*math.pi*x)/(2*math.pi*x)
end
local function Hamming(n, window)
local alpha = 0.54
return alpha - (1-alpha)*math.cos((2*math.pi*n)/(window-1))
end
local function Blackman(n, window)
local alpha = 0.16
return (1-alpha)/2 -
0.5*math.cos((2*math.pi*n)/(window-1)) +
alpha*0.5*math.cos((4*math.pi*n)/(window-1))
end
FIRStream = DeriveClass(Stream)
function FIRStream:ctor(stream, freq_stream)
-- NOTE: For stream resetting to work and to simplify
-- future optimization passes, all streams are in the streams array
self.streams = {tostream(stream), tostream(freq_stream)}
end
function FIRStream:tick()
local window = {}
-- window size (max. 1024 samples)
-- this is the max. latency introduced by the filter
-- since the window must be filled before we can generate
-- (filtered) samples
local window_size = math.min(1024, self.streams[1]:len())
local window_p = window_size-1
local accu = 0
local blackman = {}
for i = 1, window_size do blackman[i] = Blackman(i-1, window_size) end
local tick = self.streams[1]:tick()
local freq_tick = self.streams[2]:tick()
return function()
-- fill buffer (initial)
while #window < window_size-1 do
table.insert(window, tick())
end
window[window_p+1] = tick()
window_p = (window_p + 1) % window_size
local period = freq_tick()/samplerate
local sample = 0
local i = window_p
repeat
-- FIXME
sample = sample + window[(i % window_size)+1] *
Sinc((i-window_p - window_size/2)/period) *
blackman[i-window_p+1]
i = i + 1
until (i % window_size) == window_p
return sample
end
end
function FIRStream:len()
return self.streams[1]:len()
end
]==]
--
-- General-purpose IIR filters:
-- These are direct translations of ChucK's LPF, HPF, BPF and BRF
-- ugens which are in turn adapted from SuperCollider 3.
--
-- De-denormalize function adapted from ChucK.
-- Not quite sure why this is needed - properly to make the
-- IIR filters numerically more stable.
local function ddn(f)
return f >= 0 and (f > 1e-15 and f < 1e15 and f or 0) or
(f < -1e-15 and f > -1e15 and f or 0)
end
LPFStream = DeriveClass(Stream)
function LPFStream:ctor(stream, freq)
-- NOTE: For stream resetting to work and to simplify
-- future optimization passes, all streams are in the streams array
self.streams = {tostream(stream), tostream(freq)}
end
function LPFStream:tick()
local a0, b1, b2
local y1, y2 = 0, 0
-- some cached constants
local radians_per_sample = (2*math.pi)/samplerate
local sqrt2 = math.sqrt(2)
-- some cached math table lookups
local tan = math.tan
local tick = self.streams[1]:tick()
local freq_tick = self.streams[2]:tick()
local cur_freq = nil
return function()
local sample = tick()
local freq = freq_tick()
if sample == nil or freq == nil then
-- don't filter if we run out of frequency samples
return sample
elseif freq ~= cur_freq then
-- calculate filter coefficients
-- avoid recalculation for constant frequencies
cur_freq = freq
local pfreq = cur_freq * radians_per_sample * 0.5
local C = 1/tan(pfreq)
local C2 = C*C
local sqrt2C = C * sqrt2
a0 = 1/(1 + sqrt2C + C2)
b1 = -2.0 * (1.0 - C2) * a0
b2 = -(1.0 - sqrt2C + C2) * a0
end
local y0 = sample + b1*y1 + b2*y2
local result = a0 * (y0 + 2*y1 + y2)
y2 = ddn(y1)
y1 = ddn(y0)
return result
end
end
function LPFStream:len()
return self.streams[1]:len()
end
HPFStream = DeriveClass(Stream)
function HPFStream:ctor(stream, freq)
-- NOTE: For stream resetting to work and to simplify
-- future optimization passes, all streams are in the streams array
self.streams = {tostream(stream), tostream(freq)}
end
function HPFStream:tick()
local a0, b1, b2
local y1, y2 = 0, 0
-- some cached constants
local radians_per_sample = (2*math.pi)/samplerate
local sqrt2 = math.sqrt(2)
-- some cached math table lookups
local tan = math.tan
local tick = self.streams[1]:tick()
local freq_tick = self.streams[2]:tick()
local cur_freq = nil
-- NOTE: Very similar to LPFStream.tick()
-- Can we factor out the similarity without sacrificing
-- too much performance?
return function()
local sample = tick()
local freq = freq_tick()
if sample == nil or freq == nil then
-- don't filter if we run out of frequency samples
return sample
elseif freq ~= cur_freq then
-- calculate filter coefficients
-- avoid recalculation for constant frequencies
cur_freq = freq
local pfreq = cur_freq * radians_per_sample * 0.5
local C = tan(pfreq)
local C2 = C*C
local sqrt2C = C * sqrt2
a0 = 1/(1 + sqrt2C + C2)
b1 = 2.0 * (1.0 - C2) * a0
b2 = -(1.0 - sqrt2C + C2) * a0
end
local sample = tick()
local y0 = sample + b1*y1 + b2*y2
local result = a0 * (y0 - 2*y1 + y2)
y2 = ddn(y1)
y1 = ddn(y0)
return result
end
end
function HPFStream:len()
return self.streams[1]:len()
end
-- NOTE: The quality factor, indirectly proportional
-- to the passband width
BPFStream = DeriveClass(Stream)
function BPFStream:ctor(stream, freq, quality)
-- NOTE: For stream resetting to work and to simplify
-- future optimization passes, all streams are in the streams array
self.streams = {tostream(stream), tostream(freq)}
-- FIXME: Does this make sense to be a stream?
self.quality = quality
end
function BPFStream:tick()
local a0, b1, b2
local y1, y2 = 0, 0
-- some cached constants
local radians_per_sample = (2*math.pi)/samplerate
local sqrt2 = math.sqrt(2)
-- some cached math table lookups
local tan = math.tan
local cos = math.cos
local tick = self.streams[1]:tick()
local freq_tick = self.streams[2]:tick()
local cur_freq = nil
return function()
local sample = tick()
local freq = freq_tick()
if sample == nil or freq == nil then
-- don't filter if we run out of frequency samples
return sample
elseif freq ~= cur_freq then
-- calculate filter coefficients
-- avoid recalculation for constant frequencies
cur_freq = freq
local pfreq = cur_freq * radians_per_sample
local pbw = 1 / self.quality*pfreq*0.5
local C = 1/tan(pbw)
local D = 2*cos(pfreq);
a0 = 1/(1 + C)
b1 = C*D*a0
b2 = (1 - C)*a0
end
local sample = tick()
local y0 = sample + b1*y1 + b2*y2
local result = a0 * (y0 - y2)
y2 = ddn(y1)
y1 = ddn(y0)
return result
end
end
function BPFStream:len()
return self.streams[1]:len()
end
-- NOTE: The quality factor, indirectly proportional
-- to the passband width
BRFStream = DeriveClass(Stream)
function BRFStream:ctor(stream, freq, quality)
self.streams = {tostream(stream), tostream(freq)}
-- FIXME: Does this make sense to be a stream?
self.quality = quality
end
function BRFStream:tick()
local a0, b1, b2
local y1, y2 = 0, 0
-- some cached constants
local radians_per_sample = (2*math.pi)/samplerate
local sqrt2 = math.sqrt(2)
-- some cached math table lookups
local tan = math.tan
local cos = math.cos
local tick = self.streams[1]:tick()
local freq_tick = self.streams[2]:tick()
local cur_freq = nil
-- NOTE: Very similar to BPFStream.tick()
return function()
local sample = tick()
local freq = freq_tick()
if sample == nil or freq == nil then
-- don't filter if we run out of frequency samples
return sample
elseif freq ~= cur_freq then
-- calculate filter coefficients
-- avoid recalculation for constant frequencies
cur_freq = freq
local pfreq = cur_freq * radians_per_sample
local pbw = 1 / self.quality*pfreq*0.5
local C = tan(pbw)
local D = 2*cos(pfreq);
a0 = 1/(1 + C)
b1 = -D*a0
b2 = (1 - C)*a0
end
local sample = tick()
local y0 = sample - b1*y1 - b2*y2
local result = a0 * (y0 + y2) + b1*y1
y2 = ddn(y1)
y1 = ddn(y0)
return result
end
end
function BRFStream:len()
return self.streams[1]:len()
end
--
-- Jack client abstractions. This passes low level signals
-- and works only with clients created via Stream.fork()
--
ffi.cdef[[
int kill(int pid, int sig);
]]
Client = DeriveClass()
function Client:ctor(pid)
self.pid = pid
end
function Client:play()
C.kill(self.pid, 10); -- SIGUSR1
end
function Client:stop()
C.kill(self.pid, 12); -- SIGUSR2
end
function Client:kill()
C.kill(self.pid, 15); -- SIGTERM
end
Client.__gc = Client.kill
|