-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcrowns.lua
299 lines (253 loc) · 5.93 KB
/
crowns.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
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
-- scriptname: crowns
-- v0.0.0 @catfact / @zebra
engine.name = 'Zsins'
nob = dofile(_path.code .. 'zebra/lib/rnob.lua')
local hz_in = -1
local OscState = {}
OscState.__index = OscState
OscState.new = function()
local s = {}
s.f = 220 -- ui linear freq
s.ri = 390 -- fixme: magic!
s.a = 0 -- ui linear amp
s.am = 1 -- DC amp modulator
s.pan = 0 -- ui pan
s.metaTable = OscState
return s
end
local UiState = {}
UiState.__index = UiState
UiState.new = function()
local s = {}
s.fn = nil -- ui function array
s.v = 1 -- voice select
s.e1 = 'amp' -- encoder 1 function
s.e2 = 'ratio' -- encoder 2 function
s.mod = false -- mod switch flag
s.setHz = false -- setting-frequency flag
s.togAmp = false -- toggling-amplitude flag
s.metaTable = UiState
return s
end
local o = {} -- array of oscillator state
local u = UiState.new() -- ui state
local numSines = 32
function cleanup()
-- nothing to do?
end
----------------
--- helper functions
local function update_hz()
engine.hz(u.v, o[u.v].f * nob[o[u.v].ri][3])
end
local function inc_amp(inc)
o[u.v].a = o[u.v].a + inc
if o[u.v].a < 0 then o[u.v].a = 0 end
if o[u.v].a > 0.5 then o[u.v].a = 0.5 end
engine.amp(u.v, o[u.v].a)
end
local function inc_ratio(inc)
o[u.v].ri = o[u.v].ri + inc
if o[u.v].ri < 1 then o[u.v].ri = 1
elseif o[u.v].ri > #nob then o[u.v].ri = #nob
end
update_hz()
end
local function inc_pan(inc)
o[u.v].pan = o[u.v].pan + inc
if o[u.v].pan < -1 then o[u.v].pan = -1 end
if o[u.v].pan > 1 then o[u.v].pan = 1 end
engine.pan(u.v, o[u.v].pan)
redraw()
end
local function inc_voice(inc)
u.v = u.v + inc
if u.v < 1 then u.v = 1 end
if u.v > numSines then u.v = numSines end
end
local function tog_amp()
if o[u.v].am > 0 then o[u.v].am = 0
else o[u.v].am = 1 end
engine.am_add(u.v, o[u.v].am)
redraw()
end
local function round_hz (hz)
return math.floor(hz * 100) * 0.01
end
local function grab_hz()
print(hz_in)
if hz_in ~= -1 then
o[u.v].f = round_hz(hz_in)
update_hz()
redraw()
end
end
---------------------
--- global UI glue
function enc(n,z)
if n > 1 then
--- call current handler function for keys 2,3
u.fn.enc[n-1](z)
else
--- key 1 selects voice or scrubs hz grab
if z > 0 then inc_voice(1) else inc_voice(-1) end
if u.setHz then grab_hz() end
if u.togAmp then tog_amp() end
end
redraw()
end
function key(n,z)
-- print("key",n,z)
if n>1 then u.fn.key[n-1](z)
else u.mod = z > 0 end
redraw()
end
function redraw() u.fn.draw() end
--------------------
--- local UI
local uifn = {
none = {
enc={function(z) end, function(z) end},
key={function(z) end, function(z) end},
draw=function() end
},
one = {
enc= {
function(z)
if u.e1 == 'freq' then
o[u.v].f = o[u.v].f + z
update_hz()
else
inc_amp(z * 0.01)
end
end,
function(z)
if u.e2 == 'ratio' then
inc_ratio(z)
else
inc_pan(z * 0.05)
end
end
},
key={
--- key 1: select enc1 function
function(z)
if u.mod then
if z > 0 then -- press
u.setHz = true
grab_hz()
else -- lift
u.setHz = false
u.e1 = 'amp'
end
else -- no mod
print("key 1, no mod: "..z)
if z > 0 then -- press
u.e1 = 'freq'
else -- lift
u.e1 = 'amp'
end
end
end, -- end mod check
-- key 2: select enc2 function
function(z)
if u.mod then
if z > 0 then -- press
u.togAmp = true
tog_amp()
else -- lift
u.togAmp = false
u.e2 = 'ratio'
end
else -- no mod
if z > 0 then -- press
u.e2 = 'pan'
else -- lift
u.e2 = 'ratio'
end
end -- end mod check
end
},
draw=function()
screen.clear()
screen.level(15)
--------------
-- draw numbers
screen.aa(0)
screen.aa(0)
screen.aa(0)
screen.font_face(40)
screen.font_size(12)
screen.move(1, 8)
screen.text("A= "..o[u.v].a)
screen.move(54, 8)
screen.text("R= "..nob[o[u.v].ri][1].."/"..nob[o[u.v].ri][2])
screen.move(1, 20)
screen.text("F= "..o[u.v].f)
screen.move(64, 20)
screen.text("P= "..o[u.v].pan)
-----------
-- draw dots
local ampStr = ""
local selStr = ""
for i=1,numSines do
if o[i].a > 0.25 then
if o[i].am > 0 then
ampStr = ampStr.."#"
else
ampStr = ampStr.."-"
end
elseif o[i].a > 0 then
if o[i].am > 0 then
ampStr = ampStr.."|"
else
ampStr = ampStr.."-"
end
else
ampStr = ampStr.."."
end
if i == u.v then
selStr = selStr.."^"
else
selStr = selStr.." "
end
end
-- tiny monospace font
screen.font_face(25)
screen.font_size(6)
screen.move(96, 32)
screen.text("V= "..u.v)
screen.move(1, 48)
screen.text(ampStr)
screen.move(1, 58)
screen.text(selStr)
screen.update()
end
}
}
-------
-- grid stuff
local g = grid.connect()
local gc = dofile(_path.code .. 'zebra/lib/grid_cut.lua')
function g.key(x, y, z)
gc.handle_grid_key(x, y, z)
end
function gridredraw()
gc.draw_grid(g)
end
------------
---- INIT
function init()
u.fn = uifn.one
for i=1,numSines do
o[i] = OscState.new()
engine.am_mul(i, 0)
engine.am_add(i, 1)
engine.amp_atk(i, 5.0)
engine.amp_atk(i, 5.0)
engine.hz_lag(i, 1.0)
end
local p_pitch = poll.set('pitch_in_l', function(hz) hz_in = hz end)
p_pitch:start()
gc.init()
end