-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathicarus.lua
268 lines (249 loc) · 6.47 KB
/
icarus.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
-- icarus v1.3.1
--
-- I warn you, fly a middle
-- course: go too low & water
-- will weigh you down;
-- go too high and the sun's
-- fire will burn you.
-- keep to the middle way.
--
-- (plug in midi keyboard first)
-- E1 = time
-- E2 = filter
-- E3 = feedback
-- K1,2,3 bounce
-- speeding up time more easily
-- destroys the sun
engine.name="Icarus"
icarus=include("icarus/lib/icarus")
local MusicUtil=require "musicutil"
local Formatters=require 'formatters'
local feedback_temp=0
local vol_current=0
local vol_target=0
local time_change=0
local time_button=false
local filter_change=0
local filter_button=false
local current_time=0
function init()
skeys=icarus:new()
setup_midi()
-- osc input
osc.event=osc_in
clock.run(redraw_clock)
end
function setup_midi()
-- get list of devices
local mididevice={}
local mididevice_list={"none"}
midi_channels={"all"}
for i=1,16 do
table.insert(midi_channels,i)
end
for _,dev in pairs(midi.devices) do
if dev.port~=nil then
local name=string.lower(dev.name)
table.insert(mididevice_list,name)
print("adding "..name.." to port "..dev.port)
mididevice[name]={
name=name,
port=dev.port,
midi=midi.connect(dev.port),
active=false,
}
mididevice[name].midi.event=function(data)
if mididevice[name].active==false then
do return end
end
local d=midi.to_msg(data)
--if d.type~="clock" then
-- tab.print(d)
--end
if d.ch~=midi_channels[params:get("midichannel")] and params:get("midichannel")>1 then
do return end
end
if d.type=="note_on" then
skeys:on(d.note)
elseif d.type=="note_off" then
skeys:off(d.note)
elseif d.type=="pitchbend" then
local bend_st = (util.round(d.val / 2)) / 8192 * 2 -1 -- Convert to -1 to 1
engine.bend(bend_st * params:get("bend_range"))
end
end
end
end
tab.print(mididevice_list)
params:add{type="option",id="midi",name="midi in",options=mididevice_list,default=1}
params:set_action("midi",function(v)
if v==1 then
do return end
end
for name,_ in pairs(mididevice) do
mididevice[name].active=false
end
mididevice[mididevice_list[v]].active=true
end)
params:add{type="option",id="midichannel",name="midi ch",options=midi_channels,default=1}
params:add_number("bend_range", "bend range", 0, 48, 2)
if #mididevice_list>1 then
params:set("midi",2)
end
end
function enc(k,d)
if k==1 then
params:delta("delaytime",d)
if params:get("delaytime")<0.25 then
params:delta("destruction",d)
-- params:set("pressdisablesfeedback",2)
else
-- params:set("pressdisablesfeedback",1)
end
elseif k==2 then
params:delta("lpf",-1*sign(d))
elseif k==3 then
params:delta("feedback",d)
end
end
function key(k,z)
if k==1 then
time_button=z==1
elseif k==2 then
filter_button=z==1
elseif k==3 then
if z==1 then
feedback_temp=params:get("feedback")
params:set("feedback",0.85)
else
params:set("feedback",feedback_temp)
end
end
end
function redraw_clock() -- our grid redraw clock
while true do -- while it's running...
-- if time button is on, do some shifting
if time_button then
time_change=time_change-1
params:delta("delaytime",3)
elseif time_change<0 then
time_change=time_change+1
params:delta("delaytime",-3)
end
if filter_button then
filter_change=filter_change-1
params:delta("lpf",-1)
elseif filter_change<0 then
filter_change=filter_change+1
params:delta("lpf",1)
end
-- have this clock move target volume to current volume
if math.abs(vol_target-vol_current)>0.005 then
vol_current=vol_current+sign(vol_target-vol_current)/200
end
-- print(vol_current,vol_target)
clock.sleep(1/20) -- refresh
current_time=current_time+1/20
redraw()
end
end
function redraw()
screen.clear()
-- make the sun curve in the sky based on delay time
local delay_range={0.05,0.5}
local rdelay=util.linlin(delay_range[1],delay_range[2],270,90,params:get("delaytime"))
local center={64,32}
local rpos={center[1]+40*math.sin(math.rad(rdelay)),center[2]+40*math.cos(math.rad(rdelay))}
local rfeedback=util.linlin(0.9,1.5,4,20,params:get("feedback"))
local rvolume=util.linlin(0,1,0,32,vol_current)
local rlow=rfeedback
local rhigh=rfeedback+rvolume
for i=rhigh,rlow,-1 do
local ll=math.floor(util.linlin(rlow,rhigh,14,1,i))
screen.level(ll)
i=i*math.pow(1.5,1/ll)
screen.circle(rpos[1],rpos[2]+10,i)
screen.fill()
end
screen.level(15)
screen.circle(rpos[1],rpos[2]+10,rfeedback)
screen.fill()
--screen.update()
-- the ocean
local rfilter=util.linlin(0,18000,20,62,params:get("lpf"))
-- screen.level(0)
-- screen.rect(0,rfilter,129,65)
-- screen.fill()
-- -- draw reflection of the sun in the water
-- screen.level(1)
-- screen.move(0,rfilter)
-- screen.line(129,rfilter)
-- screen.stroke()
-- screen.level(10)
-- for i=1,10 do
-- screen.level(11-i)
-- screen.move(rpos[1]-rfeedback/i*0.8,rfilter+i*2)
-- screen.line(rpos[1]+rfeedback/i*0.8,rfilter+i*2)
-- screen.stroke()
-- end
local horizon=math.floor(rfilter)
--cls()
-- circfill(30,22,15,7)
math.randomseed(4)
screen.level(0)
screen.rect(0,rfilter,129,65)
screen.fill()
for y=0,64 do
local z=64/(y+1)
for i=0,z*5 do
x=(rnd(160)+current_time*160/z)%150-16
w=cos(rnd()+current_time)*12/z
if (w>0) then
local s = screen.peek(math.floor(x)%128+1,math.floor(horizon-1-y/2)%64+1, math.floor(x+1)%128+1,math.floor(horizon-y/2)%64+1)
if s ~= nil and s~='' then
local pgot = util.clamp(string.byte(s,1),1,15)
screen.level(pgot+1)
screen.move(x-w,y+horizon)
screen.line(x+w,y+horizon)
screen.stroke()
end
end
end
end
screen.update()
end
--- Cos of value
-- Value is expected to be between 0..1 (instead of 0..360)
-- @param x value
function cos(x)
return math.cos(math.rad(x * 360))
end
function rnd(x)
if x == 0 then
return 0
end
if (not x) then
x = 1
end
x = x * 100
x = math.random(x) / 100
return x
end
function rerun()
norns.script.load(norns.state.script)
end
function sign(x)
if x>0 then
return 1
elseif x<0 then
return-1
else
return 0
end
end
--
-- osc
--
function osc_in(path,args,from)
vol_target=util.linlin(0,0.15,0,1,args[2])
end