-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path6_input.lua
More file actions
146 lines (128 loc) · 2.89 KB
/
6_input.lua
File metadata and controls
146 lines (128 loc) · 2.89 KB
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
--
-- ////\\\\
-- ////\\\\ TUTORIAL
-- ////\\\\ PART 6
-- \\\\////
-- \\\\//// INPUT
-- \\\\////
--
engine.name = 'InputTutorial'
local viewport = { width = 128, height = 64 }
local signal = { amp_in_l = 0, amp_out_l = 0, amp_in_l_max = 0, amp_out_l_max = 0 }
local controls = { amp = 1.0 }
local refresh_rate = 1.0 / 15
local p_amp_in
local p_amp_out
function init()
-- Render Style
screen.level(15)
screen.aa(0)
screen.line_width(1)
-- Render
redraw()
-- Listen
audio.monitor_mono()
engine.amp(1.0)
-- Poll in
p_amp_in = poll.set("amp_in_l")
p_amp_in.time = refresh_rate
p_amp_in.callback = function(val)
signal.amp_in_l = val
if signal.amp_in_l > signal.amp_in_l_max then
signal.amp_in_l_max = signal.amp_in_l
end
end
-- Poll out
p_amp_out = poll.set("amp_out_l")
p_amp_out.time = refresh_rate
p_amp_out.callback = function(val)
signal.amp_out_l = val
if signal.amp_out_l > signal.amp_out_l_max then
signal.amp_out_l_max = signal.amp_out_l
end
end
end
function update()
engine.amp(controls.amp)
redraw()
end
function repoll()
p_amp_in:update()
p_amp_out:update()
end
-- Controls
function key(id,state)
if state == 0 then return end
if id == 2 or id == 3 then
if controls.amp == 1 then
controls.amp = 0
else
controls.amp = 1
end
end
update()
end
function enc(id,delta)
controls.amp = clamp(controls.amp + (delta/10), 0.1, 1)
update()
end
-- Render
function draw_frame()
screen.level(15)
screen.rect(1, 1, viewport.width-1, viewport.height-1)
screen.stroke()
end
function draw_uv(value,maximum,offset)
size = {width = 4, height = viewport.height - 4}
pos = {x = viewport.width - size.width - offset, y = 2}
ratio = value/maximum
activity = clamp(size.height - (ratio * size.height),3,size.height)
screen.line_width(size.width)
-- Draw
screen.level(1)
screen.move(pos.x,pos.y)
screen.line(pos.x,pos.y + size.height)
screen.stroke()
screen.level(15)
screen.move(pos.x,pos.y + size.height)
screen.line(pos.x,activity)
screen.stroke()
screen.line_width(1)
end
function draw_controls()
x = viewport.width - 16
y = math.floor(viewport.height-(controls.amp * (viewport.height-5)) - 2)
-- Draw
screen.level(15)
screen.line_width(1)
screen.move(x,y)
screen.line(x + 4,y)
screen.stroke()
end
function draw_label()
line_height = 8
screen.move(5,viewport.height - (line_height * 1))
screen.text(controls.amp..'amp')
end
function redraw()
screen.clear()
draw_frame()
draw_controls()
draw_label()
draw_uv(signal.amp_in_l,signal.amp_in_l_max,0)
draw_uv(signal.amp_out_l,signal.amp_out_l_max,5)
screen.stroke()
screen.update()
end
-- Utils
function clamp(val,min,max)
return val < min and min or val > max and max or val
end
-- Interval
re = metro.init()
re.time = refresh_rate
re.event = function()
repoll()
redraw()
end
re:start()