-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgtr.lua
More file actions
228 lines (171 loc) · 4.3 KB
/
Copy pathgtr.lua
File metadata and controls
228 lines (171 loc) · 4.3 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
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
-- GTR
-- based on earthsea + GTR engine
-- guitar kind of thing
engine.name = 'gtr'
local MusicUtil = require "musicutil"
local g = grid.connect()
local note_list = {}
local midi_in_device
local screen_framerate = 15
local screen_refresh_metro
local MAX_NUM_VOICES = 16
local buttons_down = {false,false,false}
local vel_pattern = { 7 , 5 , 5 , 5 , 1 , 5 , 5 , 5 }
local vp_len = 5
local vel_ptr = 0
local clockdiv = 2
local vel = 1.0
local notelist = {}
local noteidx = 1
function init()
midi_in_device = midi.connect(1)
midi_in_device.event = midi_event
-- Add params
params:add{type = "number", id = "midi_device", name = "MIDI Device", min = 1, max = 4, default = 1, action = function(value)
midi_in_device.event = nil
midi_in_device = midi.connect(value)
midi_in_device.event = midi_event
end}
local channels = {"All"}
for i = 1, 16 do table.insert(channels, i) end
params:add{type = "option", id = "midi_channel", name = "MIDI Channel", options = channels}
if g.device then gridredraw() end
screen_refresh_metro = metro.init{
event = function(stage)
gridredraw()
redraw()
end ,
time = 1 / screen_framerate }
screen_refresh_metro:start()
clock.run(do_bar)
end
function do_bar()
clock.sync(4)
clock.run(do_step)
end
function do_step()
while true do
vel_ptr = vel_ptr + 1
if vel_ptr > vp_len then
vel_ptr = 1
end
if noteidx > #notelist then
noteidx = 1
end
if #notelist > 0 then
vel = (vel_pattern[vel_ptr] - 1.0) / 6.0
if vel > 0 then
note_on(notelist[noteidx].note, 0 )
end
noteidx = noteidx + 1
end
clock.sync(1/clockdiv)
end
end
-- MIDI input
function midi_event(data)
local msg = midi.to_msg(data)
local channel_param = params:get("midi_channel")
if channel_param == 1 or (channel_param > 1 and msg.ch == channel_param - 1) then
-- Note off
if msg.type == "note_off" then
-- note_off(msg.note)
for idx = 1,#notelist do
if notelist[idx].note == msg.note then
table.remove(notelist,idx)
break
end
end
--table.insert(notelist,#notelist + 1, { note=msg.note, vel=msg.vel / 127 })
-- Note on
elseif msg.type == "note_on" then
-- note_on(msg.note, msg.vel / 127)
table.insert(notelist,#notelist + 1, { note=msg.note, vel=msg.vel / 127 })
-- Key pressure
-- elseif msg.type == "key_pressure" then
-- set_pressure(msg.note, msg.val / 127)
-- Channel pressure
-- elseif msg.type == "channel_pressure" then
-- set_pressure_all(msg.val / 127)
-- Pitch bend
elseif msg.type == "pitchbend" then
local bend_st = (util.round(msg.val / 2)) / 8192 * 2 -1 -- Convert to -1 to 1
local bend_range = params:get("bend_range")
-- set_pitch_bend_all(bend_st * bend_range)
-- CC
elseif msg.type == "cc" then
-- Mod wheel
if msg.cc == 1 then
-- set_timbre_all(msg.val / 127)
end
end
end
end
function note_on(note,vel)
local hz = MusicUtil.note_num_to_freq(note)
engine.note(hz, vel * 0.2 )
end
function note_off(note)
end
function g.key(x, y, z)
--print("key",x,y,z)
if x == 16 and z == 1 then
clockdiv = 9 - y
elseif y == 8 and z == 1 and x <= 8 then
vp_len = x
elseif y < 8 and z == 1 and x <= 8 then
vel_pattern[x] = 8 - y
end
-- play_notes(x,y,z)
gridredraw()
end
function gridredraw()
g:all(0)
draw_vel_pattern()
g:led(16,9 - clockdiv,8)
g:refresh()
end
function enc(n,delta)
if n == 1 then
mix:delta("output", delta)
end
end
function key(n,z)
end
function redraw()
screen.clear()
screen.aa(0)
screen.line_width(1)
screen.move(100,10)
screen.text("play")
screen_playnotes()
screen.update()
end
function screen_playnotes()
end
function draw_vel_pattern()
g:led(vp_len,8,5)
g:led(vel_ptr,8,10)
for idx = 1,8 do
for ydx = 1,vel_pattern[idx] do
if idx <= vp_len then
g:led(idx,8 - ydx , 4 )
else
g:led(idx,8 - ydx , 2 )
end
end
end
end
function play_notes(x,y,z)
end
function makenote(state,id,hz,x,y,n)
end
function log2(n)
return math.log(n) / math.log(2)
end
function grid_note(e)
gridredraw()
end
function cleanup()
print("Done")
end