-
Notifications
You must be signed in to change notification settings - Fork 1
/
editor.py
1131 lines (1053 loc) · 39 KB
/
editor.py
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import pyxel as px
import json
import os
import copy
import glob
from system import util
from system import sounds
class App:
# ===============================================
# Pyxelベース機能
# ===============================================
def __init__(self):
self.outpath = os.path.abspath("./")
px.init(256, 256, title="Pyxel Tracker", quit_key=px.KEY_NONE)
self.loading = False
with open("./help.txt", "rt", encoding="utf-8") as fin:
self.help_texts = fin.read().split("\n")
try:
fin = open("./user/tones.json", "rt", encoding="utf-8")
except:
fin = open("./system/tones.json", "rt", encoding="utf-8")
finally:
self.tones = json.loads(fin.read())
try:
fin = open("./user/patterns.json", "rt", encoding="utf-8")
except:
fin = open("./system/patterns.json", "rt", encoding="utf-8")
finally:
self.patterns = json.loads(fin.read())
self.pool = []
self.redo_items = []
self.music = []
self.is_file_load = False
self.is_file_save = False
self.is_playing = False
self.is_help_mode = False
self.files = []
self.file_cursol = 0
self.file_pos = 0
self.playing_row = 0
self.playing_start = None
self.is_tone_edit = False
self.piano_octave = 2
self.piano_tone = 0
self.piano_key = None
self.cx1 = 0
self.crow1 = 0
self.cx2 = 0
self.crow2 = 0
self.pos = 0
self.is_range_mode = False
self.params_cx = None
self.params_x = 0
self.params_y = 0
self.params_width = 0
self.params_height = 0
self.params_cursol = 0
self.tone_cursol = 0
self.numstock = 0
self.confirm_action = None
self.confirm_txt = None
self.confirm_cursol = None
self.buffer = None
self.flash_pat = False
self.init_items()
self.message = "Press [esc] to show help."
px.run(self.update, self.draw)
def update(self):
self.is_cmd = px.btn(px.KEY_GUI) or px.btn(px.KEY_CTRL)
if self.confirm_action:
return self.manage_confirm()
if self.is_help_mode:
return self.update_help()
if self.is_file_load or self.is_file_save:
return self.manage_files()
if not self.params_cx is None:
return self.edit_params()
self.manage_player()
self.play_piano()
self.manage_system()
if self.is_tone_edit:
self.edit_tone()
else:
self.edit_notes()
def draw(self):
px.cls(0)
self.flash_pat = px.frame_count % 30 < 15
if self.is_help_mode:
return self.draw_help()
if self.is_file_load or self.is_file_save:
return self.draw_files()
if self.message:
px.text(20, 216, self.message, 7)
self.draw_piano()
if self.is_tone_edit:
self.draw_tone()
else:
self.draw_notes()
if not self.params_cx is None:
self.draw_params()
if self.confirm_txt:
self.draw_confirm()
# ===============================================
# システム機能
# ===============================================
def manage_system(self):
if not self.is_cmd:
return
if px.btnp(px.KEY_RETURN):
self.playing_start = (
None if self.playing_start == self.crow1 else self.crow1
)
if self.is_playing:
return
if px.btnp(px.KEY_N):
self.set_confirm("Are you sure you want to initialize this project?", "new")
if px.btnp(px.KEY_L):
self.set_files()
self.is_file_load = True
if px.btnp(px.KEY_E) and self.project:
try:
sounds.make_midi(self.items, f"{self.outpath}/midi/{self.project}.mid")
self.message = "Exported midi file."
except:
self.message = "Failed export midi file."
if px.btnp(px.KEY_Z):
if self.pool:
self.redo_items.append(self.items)
self.items = self.pool.pop()
self.message = "Undoed."
self.add_crow(0)
else:
self.message = "Cannot undo."
if px.btnp(px.KEY_Y):
if self.redo_items:
self.pool.append(self.items)
self.items = self.redo_items.pop()
self.message = "Redoed."
self.add_crow(0)
else:
self.message = "Cannot redo."
if px.btnp(px.KEY_C):
(x1, x2, y1, y2) = self.get_x12y12()
buffer = []
idx = 0
while True:
col1 = get_col(x1)
col2 = get_col(x2 + 1)
buffer.append(self.items[y1 + idx][col1:col2])
idx += 1
if y1 + idx > y2 or y1 + idx >= len(self.items):
break
self.buffer = buffer
self.is_range_mode = False
self.message = "Copied."
if px.btnp(px.KEY_V):
if self.buffer is None:
self.message = "No copied data."
return
self.push_pool()
is_col_all = len(self.buffer[0]) > 4
if is_col_all:
base_col = 0
elif self.cx1 == 0:
self.message = "Cannot Paste."
return
else:
base_col = get_col(self.cx1)
for y, buffer in enumerate(self.buffer):
for col, value in enumerate(buffer):
self.set_item(self.crow1 + y, base_col + col, value)
self.add_crow(len(self.buffer))
self.message = "Pasted."
if px.btnp(px.KEY_O, 10, 2):
self.transpose(1)
if px.btnp(px.KEY_I, 10, 2):
self.transpose(-1)
def set_files(self):
files = glob.glob("./projects/*.json")
self.files = [file.split("/")[-1] for file in files]
self.files.sort()
def push_pool(self):
self.pool.append(copy.deepcopy(self.items))
self.redo_items = []
if len(self.pool) > 30:
self.pool.pop(0)
def transpose(self, dist):
(x1, x2, y1, y2) = self.get_x12y12()
row = y1
while True:
for idx in range(x2 - x1 + 1):
if x1 + idx > 0:
col = get_col(x1 + idx) + 3
value = self.items[row][col]
if type(value) is int and value >= 0:
new_value = util.range(value, dist, 59, 0)
self.set_item(row, col, new_value)
row += 1
if row > y2 or row >= len(self.items):
break
# ===============================================
# ヘルプ
# ===============================================
def update_help(self):
if px.btnp(px.KEY_ESCAPE) or px.btnp(px.KEY_RETURN):
self.is_help_mode = False
def draw_help(self):
for y, text in enumerate(self.help_texts):
px.text(2, 2 + y * 8, text, 7)
# ===============================================
# ファイルセレクタ
# ===============================================
def manage_files(self):
if self.is_file_save:
if px.btnp(px.KEY_ESCAPE):
self.is_file_save = False
if px.btnp(px.KEY_RETURN):
if self.project:
self.init_play()
self.is_file_save = False
tmp_len = len(self.project)
for key, value in dict_input.items():
if px.btnp(key, 10, 2) and tmp_len < 15:
self.project += value
if px.btnp(px.KEY_BACKSPACE, 10, 2) and tmp_len > 0:
self.project = self.project[0 : tmp_len - 1]
if self.is_file_load:
if px.btnp(px.KEY_ESCAPE):
self.is_file_load = False
if px.btnp(px.KEY_RETURN):
self.project = (
self.files[self.file_cursol]
.replace(".json", "")
.replace("projects\\", "")
)
self.crow1 = 0
self.pos = 0
with open(
f"./projects/{self.project}.json", "rt", encoding="utf-8"
) as fin:
self.items = json.loads(fin.read())
self.set_locs()
self.message = "File loaded."
self.is_file_load = False
keep_x = None
idx = self.file_cursol
udkey = util.udkey()
if not udkey is None:
keep_x = self.file_cursol % 4
idx += udkey * 4
rlkey = util.rlkey()
if not rlkey is None:
idx += rlkey
if idx >= len(self.files):
idx = 0 if keep_x is None else keep_x
elif idx < 0:
idx = len(self.files) - 1
if not keep_x is None:
add_x = (4 + keep_x - len(self.files)) % 4
idx = len(self.files) - 4 + add_x
self.file_cursol = idx
def draw_files(self):
idx = self.file_pos
while idx < len(self.files):
x, y = self.get_files_xy(idx)
px.text(x + 2, y + 2, self.files[idx].replace(".json", ""), 7)
idx += 1
if self.is_file_load:
px.text(2, 2, "Select project file", 7)
x, y = self.get_files_xy(self.file_cursol)
c = 7 if self.flash_pat else 12
px.rectb(x, y, 63, 8, c)
if self.is_file_save:
px.text(2, 2, "Enter project name > " + self.project, 7)
if len(self.project) < 15 and self.flash_pat:
px.rect(2 + 21 * 4 + len(self.project) * 4, 2, 4, 5, 7)
def get_files_xy(self, idx):
return (idx % 4) * 4 * 16, (idx // 4) * 8 + 16
# ===============================================
# プレイヤー
# ===============================================
def manage_player(self):
pressed = px.btnp(px.KEY_RETURN) and not self.is_cmd
if self.is_playing:
pos = px.play_pos(0) or px.play_pos(1) or px.play_pos(2) or px.play_pos(3)
if pos is None:
self.start_play(True)
elif pressed:
self.message = None
px.stop()
self.add_crow(self.playing_row - self.crow1)
self.is_playing = False
else:
while self.items_tick[self.playing_row + 1] / 48 <= pos[1]:
self.playing_row += 1
elif pressed:
self.music = sounds.compile(self.items, self.tones, self.patterns)
if not self.project:
self.set_files()
self.is_file_save = True
return
self.init_play()
def init_play(self):
with open(
f"{self.outpath}/projects/{self.project}.json", "wt", encoding="utf-8"
) as fout:
fout.write(json.dumps(self.items))
with open(
f"{self.outpath}/musics/{self.project}.json", "wt", encoding="utf-8"
) as fout:
fout.write(json.dumps(self.music))
with open(f"{self.outpath}/user/tones.json", "wt", encoding="utf-8") as fout:
fout.write(json.dumps(self.tones))
self.message = "Saved."
for ch, sound in enumerate(self.music):
px.sounds[ch].set(*sound)
self.start_play()
self.is_playing = True
self.is_range_mode = False
def start_play(self, is_loop=False):
if is_loop:
row = 0 if self.playing_start is None else self.playing_start
else:
row = self.crow1 % len(self.items)
tick = int(self.items_tick[row] / 48)
self.playing_row = row
for ch in range(4):
px.play(ch, [ch], tick)
# ===============================================
# ピアノ
# ===============================================
def play_piano(self):
if self.is_cmd or self.is_range_mode or self.is_playing:
return
for key, value in dict_playkey.items():
if px.btnp(key):
octave = self.piano_octave + value[1]
if octave >= 0 and octave <= 4:
note = octave * 12 + value[5]
self.play_piano_note(key, note, None)
(key, value) = util.numkey()
if not key is None:
for pattern in self.patterns:
if pattern["key"] == ":" + str(value):
self.play_piano_note(key, None, pattern)
if not self.piano_key is None and px.btnr(self.piano_key):
px.play(0, [0], tick=480)
self.piano_key = None
rest_pressed = px.btnp(px.KEY_R) or px.btnp(px.KEY_MINUS)
channel = self.cx1 - 1
if not self.is_tone_edit and channel >= 0 and rest_pressed:
self.set_note(channel, -1)
if px.btn(px.KEY_ALT):
self.piano_octave = util.range(self.piano_octave, util.rlkey(), 4, 0)
def draw_piano(self):
project = (
f"[{self.project.replace('.json', '')}]" if self.project else "(no name)"
)
px.text(184, 232, project, 12)
px.rect(20, 240, 209, 16, 7)
for x in range(34):
px.line(25 + x * 6, 240, 25 + x * 6, 255, 0)
for o in range(5):
px.rect(23 + o * 42, 240, 5, 9, 0)
px.rect(29 + o * 42, 240, 5, 9, 0)
px.rect(41 + o * 42, 240, 5, 9, 0)
px.rect(47 + o * 42, 240, 5, 9, 0)
px.rect(53 + o * 42, 240, 5, 9, 0)
base_x = 21 + self.piano_octave * 42
for key, value in dict_playkey.items():
octave = self.piano_octave + value[1]
if octave >= 0 and octave <= 4:
x = base_x + value[2]
y = 240 + value[3]
if key == self.piano_key:
px.rect(x, y, 3, 5, 11)
else:
px.text(x, y, value[4], 11)
for idx, pattern in enumerate(self.patterns):
x = 20 + idx * 20
px.text(x, 232, pattern["abbr"] + pattern["key"], 11)
c_rs = 13 if self.piano_octave >= 4 else 11
c_ls = 13 if self.piano_octave <= 0 else 11
px.text(234, 244, ">>", c_rs)
px.text(8, 244, "<<", c_ls)
def play_piano_note(self, key, note, pattern):
state = {
"note_cnt": 1,
"tone": self.piano_tone,
"volume": 7,
"quantize": 0.5,
"duration": 0,
"note": note,
"is_rest": False,
"pattern": pattern,
"tick": 0,
}
result = {}
sounds.putNotes(960 * 48, state, self.tones, result)
px.sounds[0].set(
result["note"],
result["tone"],
result["volume"],
result["effect"],
1,
)
px.play(0, [0])
self.piano_key = key
channel = self.cx1 - 1
if not self.is_tone_edit and channel >= 0:
value = note if not note is None else pattern["key"]
self.set_note(channel, value)
# ===============================================
# 音色
# ===============================================
def edit_tone(self):
if px.btnp(px.KEY_TAB) or px.btnp(px.KEY_ESCAPE):
self.is_tone_edit = False
udkey = util.udkey()
if not udkey is None:
self.tone_cursol = util.loop(self.tone_cursol, udkey, 7)
self.numstock = 0
tone = self.tones[self.piano_tone]
tone_key = list_parm[self.tone_cursol]
if px.btnp(px.KEY_BACKSPACE):
tone[tone_key] = 0
self.numstock = 0
rlkey = util.rlkey()
if not rlkey is None:
tone = self.tones[self.piano_tone]
self.numstock = 0
if self.tone_cursol == 0:
self.piano_tone = util.loop(self.piano_tone, rlkey, len(self.tones))
elif self.tone_cursol == 1:
idx_wave = list_wave.index(tone["wave"])
idx_wave = util.loop(idx_wave, rlkey, len(list_wave))
self.update_tone(list_wave[idx_wave])
else:
self.update_tone(tone[tone_key] + rlkey)
if self.tone_cursol > 0:
(_, value) = util.numkey()
if not value is None:
self.update_tone(self.numstock * 10 + value)
self.numstock = tone[tone_key]
else:
tmp_len = len(tone["name"])
for key, value in dict_input.items():
if px.btnp(key, 10, 2) and tmp_len < 15:
tone["name"] += value
if px.btnp(px.KEY_BACKSPACE, 10, 2) and tmp_len > 0:
tone["name"] = tone["name"][0 : tmp_len - 1]
def draw_tone(self):
draw_window(16, 16, 224, 152)
tone = None
for idx, tmp_tone in enumerate(self.tones):
y = 24 + idx * 8
c = 13
if self.piano_tone == idx:
tone = tmp_tone
c = 7
px.text(24, y, str(idx).ljust(2) + ":" + tmp_tone["name"], c)
y_base = 24
x_base = 104
px.text(x_base, y_base, str(self.piano_tone).ljust(2) + ":" + tone["name"], 7)
px.text(x_base, y_base + 8, "tone [PSTN]", 6)
px.text(x_base, y_base + 16, "attack ticks", 6)
px.text(x_base, y_base + 24, "decay ticks", 6)
px.text(x_base, y_base + 32, "sustain %", 6)
px.text(x_base, y_base + 40, "release ticks", 6)
px.text(x_base, y_base + 48, "vibrato ticks", 6)
for idx, parm in enumerate(list_parm):
if not parm is None:
px.text(x_base + 32, y_base + idx * 8, str(tone[parm]), 7)
# ADSRグラフ
attack = tone["attack"]
decay = tone["decay"]
sustain = tone["sustain"] / 100
release = tone["release"]
vibrato = tone["vibrato"]
len_sus = max(vibrato + 60 - attack - decay, 60)
scale = max(attack + decay + len_sus + release, vibrato)
if vibrato > 0:
x = vibrato * 112 / scale + x_base
y = 160 - sustain * 64
px.line(x, 96, x, 160, 9)
px.text(x, 162, "vib >>", 9)
if scale > 60 or sustain > 0:
draw_adsr(0, attack, 0, 1, scale, 7)
draw_adsr(attack, decay, 1, sustain, scale, 7)
draw_adsr(attack + decay, len_sus, sustain, sustain, scale, 11, "sus")
draw_adsr(attack + decay + len_sus, release, sustain, 0, scale, 3)
# カーソル
self.draw_cursol(x_base - 4, 22 + self.tone_cursol * 8, 80)
# 音色名入力ロケータ
if self.tone_cursol == 0:
if len(tone["name"]) < 15 and self.flash_pat:
px.rect(x_base + (3 + len(tone["name"])) * 4, y_base, 4, 5, 7)
def update_tone(self, value):
tone = self.tones[self.piano_tone]
key = list_parm[self.tone_cursol]
if key == "attack" or key == "decay" or key == "release" or key == "vibrato":
tone[key] = max(min(value, 240), 0)
elif key == "sustain":
tone[key] = max(min(value, 100), 0)
else:
tone[key] = value
# ===============================================
# ノート編集
# ===============================================
def edit_notes(self):
if px.btnp(px.KEY_ESCAPE):
self.is_help_mode = True
if px.btnp(px.KEY_AT):
self.open_params()
if px.btnp(px.KEY_TAB):
self.numstock = 0
self.is_tone_edit = True
if px.btnp(px.KEY_SPACE, 10, 2):
self.set_note(self.cx1 - 1, None)
if px.btnp(px.KEY_BACKSPACE, 10, 2):
if self.is_range_mode:
self.push_pool()
(x1, x2, y1, y2) = self.get_x12y12()
idx = 0
print(x1, x2, y1, y2)
while True:
if y1 + idx > y2 or y1 + idx >= len(self.items):
break
col1 = get_col(x1)
col2 = get_col(x2 + 1)
while col1 < col2:
self.set_item(y1 + idx, col1, None)
col1 += 1
idx += 1
self.auto_delete_rows()
# self.add_crow(-1, True)
return
if self.cx1 == 0 and self.crow1 == 0:
return
col1 = get_col(self.cx1)
col2 = get_col(self.cx1 + 1) - 1
is_deleted = False
while col1 <= col2:
if self.crow1 >= len(self.items):
break
if not is_deleted and not self.items[self.crow1][col1] is None:
is_deleted = True
self.push_pool()
self.set_item(self.crow1, col1, None)
col1 += 1
self.auto_delete_rows()
self.add_crow(-1, True)
self.is_range_mode = False
if px.btnp(px.KEY_SHIFT):
self.is_range_mode = not self.is_range_mode
ud_key = util.udkey()
if not ud_key is None:
if self.is_cmd:
if ud_key < 0:
dist_row = self.get_prev_loc(self.crow1 - 1)
elif self.crow1 >= len(self.items):
dist_row = 0
else:
dist_row = self.get_next_loc(self.crow1) + 1
self.add_crow(dist_row - self.crow1)
else:
self.add_crow(ud_key)
if not px.btn(px.KEY_ALT):
self.cx1 = util.loop(self.cx1, util.rlkey(), 5)
wheel = px.mouse_wheel
if wheel != 0:
self.add_crow(-wheel, True)
if self.is_range_mode:
self.cx2 = self.cx1 if self.cx1 > 0 else 4
else:
self.cx2 = self.cx1
self.crow2 = self.crow1
if self.cx1 > 0:
self.piano_tone = self.piano_tones[self.crow1][self.cx1 - 1]
def draw_notes(self):
# 再生インジケータ
if self.is_playing:
play_row = self.playing_row - self.pos
if play_row > 12:
self.pos += play_row - 12
play_row = self.playing_row - self.pos
elif play_row < 0:
self.pos += play_row
play_row = self.playing_row - self.pos
px.rect(0, base_y + (play_row + 1) * 8, 256, 8, 1)
# 枠
last_row = min(len(self.items) - self.pos, 25)
last_y = base_y + (last_row + 1) * 8 - 1
for x in tpl_vline_p:
px.line(x, base_y, x, last_y, 5)
for x in tpl_vline_s:
px.line(x, base_y, x, last_y, 1)
# データ
loc = 0
tick = 0
saved_item = copy.deepcopy(item_empty)
for item_idx in range(len(self.items)):
item = self.items[item_idx]
for i, data in enumerate(item):
if not data is None:
saved_item[i] = item[i]
loc_size = saved_item[1]
if not saved_item[2] is None:
tick += saved_item[2]
if tick == loc_size:
tick = 0
pos = item_idx - self.pos
if pos >= 0 and pos < 25:
y = base_y + 9 + 8 * pos
c = 9
if loc != self.locs[item_idx]:
loc = self.locs[item_idx]
c = 10
s = "<<" if item_idx == self.playing_start else str(loc)
px.text(tpl_cx[19] * 4 + 1, y, s, c)
for i, data in enumerate(item):
if pos == 0:
self.draw_item(y, i, saved_item[i], not data is None)
else:
self.draw_item(y, i, data, True)
beat_size = dict_beat[loc_size]
c = 1
if tick == 0:
c = 10
elif tick % (loc_size // beat_size) == 0:
c = 5
y = base_y + 7 + pos * 8 + 8
px.line(0, y, 255, y, c)
# カーソル
if not self.is_playing:
(x1, x2, y1, y2) = self.get_x12y12()
x, y = self.get_xy(x1, y1)
w = tpl_cx[get_col(x2 + 1)] * 4 - x - 2
h = (y2 - y1 + 1) * 8 - 1
if not self.is_range_mode or px.frame_count % 10 < 8:
px.rectb(x, y, w, h, 7)
# 固定ヘッダ
px.rect(0, base_y, 256, 8, 1)
px.text(1, base_y + 1, "BPM x/x Tick", 11)
px.text(53, base_y + 1, "@1 V Qu Nte @2 V Qu Nte @3 V Qu Nte @4 V Qu Nte", 6)
px.text(245, base_y + 1, "Loc", 10)
# ===============================================
# パラメータ編集
# ===============================================
def open_params(self):
x, y = self.get_xy(self.cx1, self.crow1)
self.params_width = 160 if self.cx1 > 0 else 64
self.params_height = 30
self.params_x = x + 2
if self.params_x + self.params_width >= 254:
self.params_x = 254 - self.params_width
self.params_y = y + 10
if self.params_y + self.params_height >= 254:
self.params_y = y - 4 - self.params_height
self.params_saved = repr(self.items)
self.push_pool()
self.params_cursol = 1 if self.cx1 > 0 else 2
self.numstock = 0
self.params_cx = self.cx1
def close_params(self):
self.params_cx = None
self.auto_add_rows(len(self.items) - 1)
self.auto_delete_rows()
def edit_params(self):
if px.btnp(px.KEY_RETURN) or px.btnp(px.KEY_AT) or px.btnp(px.KEY_ESCAPE):
if repr(self.items) == self.params_saved:
self.pool.pop(len(self.pool) - 1)
self.close_params()
if px.btnp(px.KEY_TAB):
self.numstock = 0
self.close_params()
self.is_tone_edit = True
if px.btnp(px.KEY_BACKSPACE):
self.set_item(self.crow1, self.get_params_col(), None)
self.auto_delete_rows()
self.numstock = 0
udkey = util.udkey()
if not udkey is None:
self.params_cursol = util.loop(self.params_cursol, udkey, 3)
self.numstock = 0
if self.cx1 == 0:
self.set_params_base(util.rlkey())
else:
self.set_params_channel(util.rlkey(), util.numkey()[1])
def draw_params(self):
x = self.params_x
y = self.params_y
item = self.get_item(self.crow1)
draw_window(x, y, self.params_width, self.params_height)
self.draw_cursol(x + 2, y + 2 + self.params_cursol * 8, self.params_width - 4)
col = get_col(self.cx1)
if self.cx1 == 0:
px.text(x + 4, y + 4, "BPM =", 6)
px.text(x + 32, y + 4, get_bpm(item[col]), 6)
c_beat = 6 if self.is_col_first() else 13
px.text(x + 4, y + 12, "Beat =", c_beat)
px.text(x + 32, y + 12, get_beat(item[col + 1]), c_beat)
px.text(x + 4, y + 20, "Tick =", 6)
px.text(x + 32, y + 20, get_tick(item[col + 2]), 6)
else:
px.text(x + 4, y + 4, "Instrument = (0-15)", 6)
if not item[col] is None:
px.text(x + 56, y + 4, str(item[col]), 6)
tone = self.tones[item[col]]
px.text(x + 96, y + 4, tone["name"], 6)
px.text(x + 4, y + 12, "Volume = (1-7)", 6)
px.text(x + 56, y + 12, str(item[col + 1] or ""), 6)
px.text(x + 4, y + 20, "Quantize = (1-16)", 6)
px.text(x + 56, y + 20, str(item[col + 2] or ""), 6)
def set_params_base(self, dist):
if dist is None:
return
item = self.get_item(self.crow1)
col = self.get_params_col()
value = item[col]
if self.params_cursol == 0:
new_value = util.range(value, -dist * 8, 432, 120)
elif self.params_cursol == 1:
if not self.is_col_first():
return
old_value = list_beat[0] if value is None else list_beat.index(value)
new_idx = util.loop(old_value, dist, len(list_beat))
new_value = list_beat[new_idx]
else:
old_value = list_tick[0] if value is None else list_tick.index(value)
new_idx = util.loop(old_value, dist, len(list_tick))
new_value = list_tick[new_idx]
self.set_item(self.crow1, col, new_value)
def set_params_channel(self, dist, num):
if dist is None and num is None:
return
item = self.get_item(self.crow1)
col = self.get_params_col()
value = item[col]
max_val = (15, 7, 16)[self.params_cursol]
min_val = (0, 1, 1)[self.params_cursol]
if not dist is None:
new_value = util.range(value, dist, max_val, min_val)
self.numstock = 0
if not num is None:
self.numstock = self.numstock * 10 + num
new_value = min(self.numstock, max_val)
self.numstock = new_value
self.set_item(self.crow1, col, new_value)
def get_params_col(self):
return get_col(self.cx1) + self.params_cursol
def is_col_first(self):
return self.crow1 == 0 or self.locs[self.crow1] > self.locs[self.crow1 - 1]
# ===============================================
# confirmウィンドウ
# ===============================================
def manage_confirm(self):
if self.confirm_txt is None:
if self.confirm_action == "new":
self.push_pool()
self.init_items()
self.message = "Data initialized."
self.confirm_action = None
else:
self.confirm_cursol = util.loop(self.confirm_cursol, util.rlkey(), 2)
if px.btnp(px.KEY_RETURN):
if self.confirm_cursol == 1:
self.confirm_action = None
self.confirm_txt = None
def draw_confirm(self):
width = len(self.confirm_txt) * 4 + 8
height = 3 * 8 + 8
x = 128 - width / 2
y = 128 - height / 2
draw_window(x, y, width, height)
px.text(x + 4, y + 4, self.confirm_txt, 7)
px.text(128 - 16, y + 20, "yes", 7)
px.text(128 + 18, y + 20, "no", 7)
self.draw_cursol(128 - 18 + self.confirm_cursol * 32, y + 19, 16)
def set_confirm(self, txt, action):
self.message = None
self.confirm_cursol = 0
self.confirm_txt = txt
self.confirm_action = action
# ===============================================
# 汎用
# ===============================================
def add_crow(self, dist, no_loop=False):
if dist is None:
return
value = self.crow1
max_value = len(self.items)
if no_loop:
new_value = util.range(value, dist, max_value, 0)
else:
new_value = util.loop(value, dist, max_value + 1, 0)
self.crow1 = new_value
pos = new_value - self.pos
if pos < 0:
self.pos += pos
if pos >= 24:
self.pos += pos - 24
def get_xy(self, cx, cy):
x = tpl_cx[get_col(cx)] * 4
y = base_y + 8 + (cy - self.pos) * 8
return x, y
def get_x12y12(self):
if self.cx1 <= self.cx2:
x1 = self.cx1
x2 = self.cx2
else:
x1 = self.cx2
x2 = self.cx1
if self.crow1 <= self.crow2:
y1 = self.crow1
y2 = self.crow2
else:
y1 = self.crow2
y2 = self.crow1
return (x1, x2, y1, y2)
def get_next_loc(self, row):
dist_row = row
self.set_locs()
loc = self.locs[row]
while self.locs[dist_row + 1] == loc:
dist_row += 1
return dist_row
def get_prev_loc(self, row):
dist_row = row
loc = self.locs[row]
while dist_row > 0 and self.locs[dist_row - 1] == loc:
dist_row -= 1
return dist_row
def set_locs(self):
speed = 0
loc = 1
tick = 0
tick_total = 0
loc_size = 0
tick_size = 0
idx = 0
locs = []
items_tick = []
piano_tones = []
current_tones = [0, 0, 0, 0]
while True:
item = self.get_item(idx)
items_tick.append(tick_total)
locs.append(loc)
current_tones = copy.deepcopy(current_tones)
if not item[0] is None:
speed = item[0]
if not item[1] is None:
loc_size = item[1]
if not item[2] is None:
tick_size = item[2]
for ch in range(4):
tone = item[3 + ch * 4]
if not tone is None:
current_tones[ch] = tone
piano_tones.append(current_tones)
tick += tick_size
tick_total += speed * tick_size
if tick >= loc_size:
tick -= loc_size
loc += 1
if len(self.items) <= idx:
locs.append(loc)
break
idx += 1
items_tick.append(tick_total)
self.piano_tones = piano_tones
self.items_tick = items_tick
self.locs = locs
def init_items(self):
items = []
items.append(copy.deepcopy(item_empty))
items[0][0] = 240
items[0][1] = 48
items[0][2] = 6
self.project = ""
self.items = items
self.crow1 = 0
self.pos = 0
self.set_item(0, 3, None)
self.set_locs()
def get_item(self, row):
return self.items[row] if row < len(self.items) else copy.deepcopy(item_empty)
def set_item(self, row, col, data):
if row == 0 and col <= 2 and data is None:
return
self.message = None
self.auto_add_rows(row)
self.items[row][col] = data
self.set_locs()
def auto_add_rows(self, row):
dist_row = self.get_next_loc(row)
while dist_row > len(self.items) - 1:
self.items.append(copy.deepcopy(item_empty))
def auto_delete_rows(self):
if not self.params_cx is None:
return
loop = True
while loop:
loc = self.get_prev_loc(len(self.items) - 1)
next_loc = self.get_next_loc(loc) + 1
locs = next_loc - loc
while loc < next_loc:
for data in self.get_item(loc):
if not data is None:
loop = False
break
else:
loc += 1
continue
loop = False
break
else:
del self.items[-locs:]
def draw_item(self, y, i, data, is_real):
if not data is None:
x = tpl_cx[i] * 4 + 1
if i == 0:
txt = get_bpm(data)
elif i == 1:
txt = get_beat(data)
elif i == 2:
txt = get_tick(data)
elif i % 4 == 2:
if type(data) == int:
txt = "-" if data < 0 else tpl_note[data % 12] + str(data // 12)
else:
for pattern in self.patterns:
if pattern["key"] == data:
txt = pattern["abbr"]
else:
txt = str(data)
if i <= 2:
c = 11 if is_real else 3
else:
c = 6 if is_real else 12
px.text(x, y, txt, c)