-
Notifications
You must be signed in to change notification settings - Fork 3
/
audio.test.lua
221 lines (186 loc) · 4.81 KB
/
audio.test.lua
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
local draw2D = require "draw2D"
local audio = require "audio"
local ffi = require "ffi"
local C = ffi.C
local util = require "util"
local conflat = util.conflat
local template = util.template
local win = require "window"
win:setdim(100, 50)
local concat = table.concat
local format = string.format
local pi = math.pi
local twopi = pi * 2
local id = 0
local gensym = function(name)
id = id + 1
return format("%s_%d", name, id)
end
local set_mt = {}
set_mt.__index = set_mt
function set_mt:add(s, more, ...)
if not self[s] then
self[#self+1] = s
self[s] = true
end
if more then self:add(more, ...) end
return self
end
function set(initial, ...)
local s = setmetatable({}, set_mt)
if initial then s:add(initial, ...) end
return s
end
local rope_mt = {}
rope_mt.__index = rope_mt
function rope_mt:write(fmt, ...)
self[#self+1] = format(fmt, ...)
return self
end
function rope(...)
return setmetatable({...}, rope_mt)
end
local synthcode = template [[
local system = ...
local voice = assert(system.voices[$id], "voice does not exist")
$locals
$objects
voice.perform = function(self, out, frames)
local lbuf, rbuf = unpack(out)
$pre
for i = 0, frames-1 do
$statements
lbuf[i] = lbuf[i] + $left
rbuf[i] = rbuf[i] + $right
end
$post
end
]]
local generator = {}
-- TODO: this doesn't handle multi-channel properly
local function visit(self, v)
if type(v) == "table" then
if self.memo[v] then
return self.memo[v]
elseif v.op then
-- parse inputs first:
local gen = assert(generator[v.op], "missing gen"..tostring(v.op))
local out = gen(self, v)
self.memo[v] = out
return out
else
local args = {}
for i, input in ipairs(v) do
args[i] = visit(self, input)
end
return unpack(args)
end
else
return tostring(v)
end
end
function generator:Mul(node)
end
function generator:SinOsc(node)
self.locals:add("local sin = math.sin")
local p = self:member(gensym("phase"), visit(self, node[2]))
local pincr = gensym("pincr")
local o = gensym("sinosc")
self.statements:write("local %s = %s * %f", pincr, visit(self, node[1]), twopi / audio.driver.samplerate)
self.statements:write("%s = %s + %s", p, p, pincr)
self.statements:write("local %s = sin(%s)", o, p)
return o
end
function generator:Param(node)
local name = self:member(node[1], visit(self, node[2]))
return name
end
local Expr = {}
Expr.__index = Expr
local binops = {
{ op = "Add", name = "add", meta = "__add", infix = "+" },
{ op = "Sub", name = "sub", meta = "__sub", infix = "-" },
{ op = "Mul", name = "mul", meta = "__mul", infix = "*" },
{ op = "Div", name = "div", meta = "__div", infix = "/" },
{ op = "Pow", name = "pow", meta = "__pow", infix = "^" },
{ op = "Mod", name = "mod", meta = "__mod", infix = "%" },
}
for i, v in ipairs(binops) do
Expr[v.meta] = function(a, b)
return setmetatable({ op = v.op, a, b }, Expr)
end
generator[v.op] = function(self, node)
local o = gensym(v.name)
self.statements:write("local %s = %s %s %s", o, visit(self, node[1]), v.infix, visit(self, node[2]))
return o
end
end
local function Param(name, default)
return setmetatable({ op="Param", name or gensym("param"), default or 0 }, Expr)
end
local function SinOsc(frequency, initialphase)
return setmetatable({ op="SinOsc", frequency or 440, initialphase or 0 }, Expr)
end
local kernel_mt = {}
kernel_mt.__index = kernel_mt
function kernel_mt:member(name, init)
local id = #self.objects + 1
self.objects[name] = id
self.objects:write("voice.data[%d] = %s", id, init)
self.pre:write("local %s = self[%d]", name, id)
self.post:write("self[%d] = %s", id, name)
return name
end
local function Def(t)
-- send to server as a re-usable def now?
return function(init)
local kernel = setmetatable({
memo = {},
locals = set(), -- max 60 upvalues
objects = rope(), -- max table constructor items?
statements = rope(), -- max 200 locals
pre = rope(),
post = rope(),
}, kernel_mt)
local l, r = visit(kernel, t)
kernel.left = l or 0
kernel.right = r or kernel.left
local id = audio.add()
kernel.id = id
local code = synthcode(kernel)
--print(code)
audio.setcode(code)
if init then
for k, v in pairs(init) do
audio.setparam(id, kernel.objects[k], v)
end
end
return kernel
end
end
local wobble = Def{
SinOsc((110 * Param("register", 4)) + SinOsc(Param("rate")) * Param("width", 10)) * 0.01
}
local synths = {}
for i = 1, 50 do
local v = wobble{
width = math.random()*100,
rate = math.random(10),
}
synths[v] = true
end
function draw()
for x = 0, 1, 0.01 do
local y = math.random() * 0.5
draw2D.line(x, 0.5 + y, x, 0.5 - y)
end
for synth in pairs(synths) do
if math.random() < 0.01 then
audio.setparam(synth.id, synth.objects.register, math.random(10))
end
if math.random() < 0.0001 then
audio.remove(synth.id)
synths[synth] = nil
end
end
end