Repository navigation
Expand file tree
/
Copy pathPXM.lua
More file actions
323 lines (295 loc) · 8.05 KB
/
Copy pathPXM.lua
File metadata and controls
323 lines (295 loc) · 8.05 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
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
-- Prophet X Patch Librarian
-- K1 to reconnect
-- K2 to erase
-- K3 to rename
local textentry = require 'textentry'
local midi_channel = 2
local sysex_request = {0xF0, 0x01, 0x30, 0x06, 0xF7}
local sysex_timeout_secs = 2
local patch_name_start = 0x000001E2
local patch_name_length = 20
local forbidden_positions = {0x000001E5, 0x000001ED, 0x000001F5}
local midi_dev = nil
local grid_dev = nil
local patches = {}
local press_times = {}
local awaiting_patch_from = {}
local sysex_buffer = {}
local flash_state = {}
local sysex_active = false
local last_pressed_index = nil
local timeout_clock = nil
local k2_held = false
local renaming_mode = false
local last_displayed_name = ""
local function contains(tbl, val)
for _, v in ipairs(tbl) do
if v == val then return true end
end
return false
end
-- strips non-printable characters from a string
local function clean_name(str)
local out = ""
for i = 1, #str do
local char = string.sub(str, i, i)
if string.byte(char) >= 32 and string.byte(char) <= 126 then
out = out .. char
end
end
return out
end
local function index_to_xy(index)
local x = ((index - 1) % 16) + 1
local y = math.floor((index - 1) / 16) + 1
return x, y
end
local function grid_led(index, level)
if not grid_dev then return end
local x, y = index_to_xy(index)
grid_dev:led(x, y, level)
end
local function flash_indicator(index)
while awaiting_patch_from[index] do
grid_led(index, flash_state[index] and 15 or 0)
grid_dev:refresh()
flash_state[index] = not flash_state[index]
clock.sleep(0.1)
end
end
local function patch_filename(index)
return norns.state.data .. "patch_" .. index .. ".syx"
end
local function save_patch(index, data)
local file = io.open(patch_filename(index), "wb")
if file then
for _, byte in ipairs(data) do
file:write(string.char(byte))
end
file:close()
end
end
-- reads the patch name from raw sysex data, skipping forbidden byte positions
local function extract_patch_name(sysex_data)
if not sysex_data or #sysex_data < patch_name_start + patch_name_length then
return "Unnamed"
end
local name = ""
local pos = patch_name_start
local chars_read = 0
while chars_read < patch_name_length do
if not contains(forbidden_positions, pos) then
name = name .. string.char(sysex_data[pos])
chars_read = chars_read + 1
end
pos = pos + 1
end
return name
end
local function get_patch_name(index)
if not patches[index] then return "Unnamed" end
return clean_name(extract_patch_name(patches[index]))
end
-- writes a new name into the sysex data for a stored patch and saves to disk.
-- note: patch_name_start is the 0-based address from the Prophet X spec;
-- +1 is applied when writing to account for lua's 1-based table indexing.
local function update_patch_name(index, new_name)
if not patches[index] then return end
local sysex_data = patches[index]
new_name = new_name:sub(1, patch_name_length)
local name_index = 1
local written = 0
local pos = patch_name_start + 1
while written < patch_name_length do
if not contains(forbidden_positions, pos) then
if name_index <= #new_name then
sysex_data[pos] = string.byte(new_name, name_index)
name_index = name_index + 1
else
sysex_data[pos] = 0x20
end
written = written + 1
end
pos = pos + 1
end
save_patch(index, sysex_data)
last_displayed_name = new_name
redraw("Patch renamed!")
end
local function send_sysex(data)
if midi_dev then midi_dev:send(data) end
end
local function sysex_timeout()
clock.sleep(sysex_timeout_secs)
if sysex_active then
redraw("Timeout!")
clock.sleep(1)
sysex_active = false
awaiting_patch_from[last_pressed_index] = nil
last_pressed_index = nil
redraw()
end
end
local function erase_all_patches()
for i = 1, 128 do
patches[i] = nil
os.remove(patch_filename(i))
end
redraw("All patches erased!")
end
local function start_text_input(index)
local trimmed = get_patch_name(index):match("^(.-)%s*$") or "Unnamed"
textentry.enter(function(new_name)
if new_name then update_patch_name(index, new_name) end
renaming_mode = false
redraw()
end, trimmed, "Enter new patch name:")
end
function init_midi()
midi_dev = nil
for _, dev in pairs(midi.devices) do
if dev.port and dev.name and dev.name:find("Prophet") then
midi_dev = midi.connect(dev.port)
midi_dev.event = midi_event
midi_dev.channel = midi_channel
redraw("Reconnected!")
clock.run(function() clock.sleep(0.5); redraw() end)
return
end
end
redraw("Connection Failed")
clock.run(function() clock.sleep(0.5); redraw() end)
end
function init_grid()
grid_dev = grid.connect()
if grid_dev then grid_dev.key = grid_key end
end
function load_patches()
for i = 1, 128 do
local file = io.open(patch_filename(i), "rb")
if file then
patches[i] = {}
for byte in file:read("*a"):gmatch(".") do
table.insert(patches[i], string.byte(byte))
end
file:close()
end
end
end
function midi_event(data)
if not data or #data == 0 then return end
if sysex_active then
if data[1] == 0xF0 then
sysex_buffer = data
elseif data[#data] == 0xF7 then
for _, byte in ipairs(data) do table.insert(sysex_buffer, byte) end
if last_pressed_index then
patches[last_pressed_index] = sysex_buffer
save_patch(last_pressed_index, sysex_buffer)
awaiting_patch_from[last_pressed_index] = nil
flash_state[last_pressed_index] = nil
if timeout_clock then
clock.cancel(timeout_clock)
timeout_clock = nil
end
last_displayed_name = get_patch_name(last_pressed_index)
redraw()
end
sysex_active = false
sysex_buffer = {}
else
for _, byte in ipairs(data) do table.insert(sysex_buffer, byte) end
end
end
end
function redraw(message)
screen.clear()
if k2_held then
screen.move(64, 32)
screen.text_center("Erase")
screen.move(64, 42)
screen.text_center("K3: Erase All")
else
if last_displayed_name ~= "" then
screen.move(64, 32)
screen.text_center(last_displayed_name)
end
if last_pressed_index then
screen.move(120, 60)
screen.text_right(tostring(last_pressed_index))
end
end
if message then
screen.move(64, 52)
screen.text_center(message)
end
screen.update()
if grid_dev then
grid_dev:all(0)
for i = 1, 128 do
if patches[i] then
grid_led(i, i == last_pressed_index and 15 or 5)
end
end
grid_dev:refresh()
end
end
function grid_key(x, y, z)
if sysex_active or not x or not y then return end
local index = (y - 1) * 16 + x
if renaming_mode and not k2_held then
if z == 1 then start_text_input(index) end
return
end
if k2_held then
if z == 1 then
patches[index] = nil
os.remove(patch_filename(index))
redraw("Patch deleted")
end
return
end
if z == 1 then
press_times[index] = util.time()
last_pressed_index = index
if patches[index] then
send_sysex(patches[index])
last_displayed_name = get_patch_name(index)
redraw()
else
awaiting_patch_from[index] = true
flash_state[index] = true
clock.run(flash_indicator, index)
sysex_active = true
redraw("Receiving...")
if timeout_clock then clock.cancel(timeout_clock) end
timeout_clock = clock.run(sysex_timeout)
send_sysex(sysex_request)
end
end
end
function key(n, z)
if n == 1 and z == 1 then
redraw("Reconnecting...")
init_midi()
elseif n == 2 then
k2_held = (z == 1)
redraw()
elseif n == 3 and z == 1 then
if k2_held then
erase_all_patches()
elseif patches[last_pressed_index] then
renaming_mode = true
start_text_input(last_pressed_index)
else
redraw("No patch selected!")
end
end
end
function enc(n, delta) end
function init()
init_midi()
init_grid()
load_patches()
redraw("Patch Librarian")
end