-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcurses_fs_instruments.py
More file actions
443 lines (387 loc) · 13 KB
/
Copy pathcurses_fs_instruments.py
File metadata and controls
443 lines (387 loc) · 13 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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
import subprocess
PORT = 9988
import os
os.chdir(os.path.dirname(os.path.realpath(__file__))) # set cwd to where the script is
import curses_fs_fonts
import time
import curses
from curses import wrapper
def concat(*args, sep=' '):
return sep.join(str(arg) for arg in args)
channel = 0
def get_inst_list(basic: bool = False, fnt: int=1) -> list[str]|dict[str,list[str]]:
instruments = subprocess.check_output(f"echo 'inst {fnt}' | nc -q 0 localhost {PORT}", shell=True).decode().split('\n')
if basic:
return instruments
insts: dict[str,list[str]] = {}
for inst in instruments:
if not inst:
continue
inst = inst.split(' ',1)
insts[inst[1]] = inst[0].split('-',1)
return insts
def try_search(stdscr, options: list, font_id):
stdscr.clear()
insts = get_inst_list(False, font_id)
search = text_input(stdscr, (1,6))
if search == "cancel":
cursable(stdscr)
return
#search = input("Enter instrument: ")
options = []
if not search:
for n, inst in enumerate(insts):
options.append(inst)
#print(n,inst)
opts = ["Back", "Search", *options]
sel = select_loop(stdscr, opts, " > 3 Inst", False)
if sel == "Search":
try_search(stdscr, options, font_id)
elif sel == "Back":
cursable(stdscr)
return
else:
return select(sel, font_id)
else:
#alias a bunch of em
count = 0
opts_list = []
for inst in insts:
if search.lower() in inst.lower():
options.append(inst)
opts_list.append((count,inst))
count += 1
if len(options) == 1:
return select(options[0], font_id)
else:
opts = ["Back", "Search", *options]
sel = select_loop(stdscr, opts, " > 3 Inst", False)
if sel == "Search":
try_search(stdscr, options, font_id)
elif sel == "Back":
cursable(stdscr)
return
else:
return select(sel, font_id)
# insts = get_inst_list(False, font_id)
# search = text_input(stdscr, (1,6))
# if search == "cancel":
# cursable(stdscr)
# return
# try:
# search_num = int(search)
# # fail on purpose if int is larger than list
# grinch = 1 / max(len(options)-search_num, 0)
# except:
# options = []
# if not search:
# for n, inst in enumerate(insts):
# options.append(inst)
# print(n,inst)
# else:
# #alias a bunch of em
# count = 0
# opts_list = []
# for inst in insts:
# if search.lower() in inst.lower():
# options.append(inst)
# opts_list.append((count,inst))
# print(count, inst)
# count += 1
# if len(options) == 1:
# select(options[0], font_id)
# else:
# try_search(stdscr, options, font_id)
# else:
# select(options[search_num], font_id)
def get_recent_channel() -> int:
try:
with open("current.sav") as save_file:
save_data = save_file.readlines()
except:
pass
else:
for line in save_data:
if line.startswith("chansel"):
return int(line.split()[1])
return 1
def get_recent_font() -> int:
try:
with open("current.sav") as save_file:
save_data = save_file.readlines()
except:
pass
else:
for line in save_data:
if line.startswith("fntsel"):
return int(line.split()[1])
return 1
def select(instrument: str, font_id):
insts: dict[str,list[str]] = get_inst_list(False, font_id) # type: ignore
print(f"Switching to {instrument}")
sel = insts[instrument]
try:
subprocess.check_output(f"echo 'select {channel-1} {font_id} {sel[0]} {sel[1]}' | nc -q 0 localhost {PORT}", shell=True)
except subprocess.CalledProcessError:
return "Process error"
else:
save_inst(sel, font_id)
save_chan(channel)
save_font(font_id)
return f"Switching channel {channel} to {instrument}"
def save_inst(sel: list[str], font_id) -> None:
known_instruments = []
other_data = []
track = None
try:
with open("current.sav") as save_file:
save_data = save_file.readlines()
except:
pass
else:
for line in save_data:
if line.startswith("inst"):
known_instruments.append(line.split())
else:
other_data.append(line)
for i, inst in enumerate(known_instruments):
if int(inst[1]) == channel:
track = i
break
if track is not None:
known_instruments.pop(track)
known_instruments.append( ["inst", str(channel), str(font_id), str(sel[0]), str(sel[1])] )
save_data = other_data
for inst in known_instruments:
save_data.append(" ".join(inst)+'\n')
with open("current.sav", 'w') as save_file:
save_file.writelines(save_data)
def save_chan(sel: int) -> None:
final_data = []
try:
with open("current.sav") as save_file:
save_data = save_file.readlines()
except:
pass
else:
for line in save_data:
if not line.startswith("chansel"):
final_data.append(line)
final_data.append( f"chansel {sel}\n" )
with open("current.sav", 'w') as save_file:
save_file.writelines(final_data)
def save_font(sel: int) -> None:
final_data = []
try:
with open("current.sav") as save_file:
save_data = save_file.readlines()
except:
pass
else:
for line in save_data:
if not line.startswith("fntsel"):
final_data.append(line)
final_data.append( f"fntsel {sel}\n" )
with open("current.sav", 'w') as save_file:
save_file.writelines(final_data)
def reset_insts() -> None:
for channel in range(16):
subprocess.check_output(f"echo 'select {channel} 1 000 000' | nc -q 0 localhost {PORT}", shell=True)
def find_name(bnk: int, prg: int, fnt: int) -> str:
instruments = get_inst_list(True, fnt)
bnkname = format(bnk,"03d")
prgname = format(prg,"03d")
for inst in instruments:
if inst.startswith(f"{bnkname}-{prgname}"):
return inst.split(' ',1)[1]
return "This should never happen"
def set_inst(channel: int, bnk: int, prg: int, fnt: int) -> None:
output = subprocess.check_output(f"echo 'select {channel-1} {fnt} {bnk} {prg}' | nc -q 0 localhost {PORT}", shell=True)
if output != b'':
print( output )
print(f"Set channel {channel} to {find_name(bnk, prg, fnt)}")
def num_input(stdscr, position: tuple, opts: list) -> str:
y, x = position[0], position[1]
final_text = []
cycle = ["Ent", *opts, "Esc"]
i = 0 # switch to specific selection and while loop
key = "primed"
while True:
run = False
#stdscr.clear()
# stdscr.addstr( 12, 32, "L", curses.A_REVERSE)
# stdscr.addstr( 0,0, f"Main > Sets > Rt > Aud{stage}")
if key == "primed": # interpret key inputs
pass
elif key == curses.KEY_RIGHT:
i += 1
elif key == curses.KEY_LEFT:
i -= 1
elif key == ord('\n'):
run = True
if i < 0: # loop
i = len(cycle)-1
if i >= len(cycle):
i = 0
stdscr.addstr(y,x, ' ')
stdscr.addstr(y,x, cycle[i], curses.A_REVERSE)
if run: # wrap run action in conditional
return cycle[i]
stdscr.refresh()
key = stdscr.getch() #refresh in loop, don't handle end feedback separately
def select_loop(stdscr, entries: list, stage: str='', return_index: bool=False):
curses.start_color()
bw = curses.color_pair(0) # white on black if we need it
curses.curs_set(0)
def display(*args, sep=' '):
stdscr.addstr( 1,0, concat( *args, sep ) )
i = 1 # switch to specific selection and while loop
key = "primed"
while True:
run = False
stdscr.clear()
stdscr.addstr( 12, 32, "L", curses.A_REVERSE)
stdscr.addstr(0,0, f"Main > MIDI > Ins{stage}")
if key == "primed": # interpret key inputs
pass
elif key == curses.KEY_RIGHT:
i += 1
elif key == curses.KEY_LEFT:
i -= 1
elif key == ord('\n'):
run = True
if i < 0: # loop
i = len(entries)-1
if i >= len(entries):
i = 0
display(entries[i])
if run: # wrap run action in conditional
if return_index:
return i
else:
return entries[i]
stdscr.refresh()
key = stdscr.getch() #refresh in loop, don't handle end feedback separately
def text_input(stdscr, position: tuple) -> str|int:
y, x = position[0], position[1]
final_text = []
cycle = ["Ent", *"abcdefghijklmnopqrstuvwxyz 1234567890 ", "Esc","<X"]
stdscr.clear()
def choose_char(pos=0):
i = pos # switch to specific selection and while loop
key = "primed"
while True:
run = False
#stdscr.clear()
stdscr.addstr(0,0, "Main > MIDI > Ins > 3 Inst")
stdscr.addstr(1,0, "Inst:")
if key == "primed": # interpret key inputs
pass
elif key == curses.KEY_RIGHT:
i += 1
elif key == curses.KEY_LEFT:
i -= 1
elif key == ord('\n'):
run = True
if i < 0: # loop
i = len(cycle)-1
if i >= len(cycle):
i = 0
stdscr.addstr(y,x, ' ')
stdscr.addstr(y,x, cycle[i], curses.A_REVERSE)
if run: # wrap run action in conditional
return cycle[i],i
stdscr.refresh()
key = stdscr.getch() #refresh in loop, don't handle end feedback separately
next_char, next_pos = choose_char()
while not next_char in ["Esc", "Ent"]:
if next_char == "<X":
try:
final_text.pop(-1)
x -= 1
except:
pass
else:
final_text.append(next_char)
x += 1
stdscr.addstr(*position, ''.join(final_text))
next_char, next_pos = choose_char(next_pos)
if next_char == "Ent":
return ''.join(final_text)
elif next_char == "Esc":
return 'cancel'
def cursable(stdscr):
def display(*args, sep=' '):
stdscr.addstr( 1,0, concat( *args, sep ) )
stdscr.addstr( 12, 32, "L", curses.A_REVERSE)
stdscr.addstr(0,0, "Main > MIDI > Ins > 1 Ch ")
stdscr.addstr(1,0, "Channel ")
#8
global channel
channel = num_input(stdscr, (1,8), list(str(i+1) for i in range(16)))
if channel == "Ent":
channel = get_recent_channel()
stdscr.addstr(1,0, f"Using channel {channel}")
stdscr.refresh()
time.sleep(1)
elif channel == "Esc":
return
else:
channel = int(channel)
fonts = curses_fs_fonts.list_loaded(False)
fonts_show = []
for i in fonts:
fonts_show.append(i[2])
opts = ["Back","Soundfonts:",*fonts_show]
stdscr.addstr(0,0, "Main > MIDI > Ins > 2 Sft ")
sel = select_loop(stdscr, opts, " > 2 Sft", True)
while sel == 1:
sel = select_loop(stdscr, opts, " > 2 Sft", True)
if sel == 0:
cursable(stdscr)
return
font_id = sel-1
stdscr.clear()
insts = get_inst_list(False, font_id)
search = text_input(stdscr, (1,6))
if search == "cancel":
cursable(stdscr)
return
#search = input("Enter instrument: ")
options = []
if not search:
for n, inst in enumerate(insts):
options.append(inst)
#print(n,inst)
opts = ["Back", "Search", *options]
sel = select_loop(stdscr, opts, " > 3 Inst", False)
if sel == "Search":
try_search(stdscr, options, font_id)
elif sel == "Back":
cursable(stdscr)
return
else:
return select(sel, font_id)
else:
#alias a bunch of em
count = 0
opts_list = []
for inst in insts:
if search.lower() in inst.lower():
options.append(inst)
opts_list.append((count,inst))
count += 1
if len(options) == 1:
return select(options[0], font_id)
else:
opts = ["Back", "Search", *options]
sel = select_loop(stdscr, opts, " > 3 Inst", False)
if sel == "Search":
try_search(stdscr, options, font_id)
elif sel == "Back":
cursable(stdscr)
return
else:
return select(sel, font_id)
if __name__ == '__main__':
print(wrapper(cursable))