-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_window.py
More file actions
5445 lines (4589 loc) · 242 KB
/
main_window.py
File metadata and controls
5445 lines (4589 loc) · 242 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
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
"""
Main GUI Window for Pythonic
Visual interface closely matching the original application
"""
import tkinter as tk
from tkinter import ttk, filedialog, messagebox
import threading
import numpy as np
import json
import os
import time
import random
import copy
from collections import deque
# Import our synthesizer
import sys
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from pythonic.synthesizer import PythonicSynthesizer
from pythonic.oscillator import WaveformType, PitchModMode
from pythonic.noise import NoiseFilterMode, NoiseEnvelopeMode
from pythonic.preset_manager import PresetManager
from pythonic.preferences_manager import PreferencesManager
from pythonic.pattern_manager import PatternManager
from pythonic.lfo import (
LFOWaveform, LFORetrigger, LFOPolarity, SyncDivision,
ModTarget, MOD_TARGET_GROUPS, MOD_TARGET_LABELS,
)
from gui.widgets import (
RotaryKnob, VerticalSlider, ChannelButton,
WaveformSelector, ModeSelector, ToggleButton, PatternEditor, MatrixEditor
)
from gui.po32_transfer import PO32TransferDialog
from gui.po32_import_dialog import PO32ImportDialog
from gui.drum_generator_dialog import DrumGeneratorDialog
from pythonic.drum_generator import infer_drum_type
from pythonic.pattern_generator import PatternGenerator
from pythonic.preset_manager import channel_to_raw_patch
try:
import sounddevice as sd
AUDIO_AVAILABLE = True
except ImportError:
AUDIO_AVAILABLE = False
print("Warning: sounddevice not available. Audio playback disabled.")
try:
import mido
MIDI_AVAILABLE = True
except ImportError:
MIDI_AVAILABLE = False
print("Warning: mido not available. MIDI export disabled.")
# Import MIDI input manager
from pythonic.midi_manager import MidiManager
from pythonic.morph_manager import MorphManager
class PythonicGUI:
"""
Main GUI application for Pythonic
"""
# Color scheme matching Pythonic
COLORS = {
'bg_dark': '#2a2a3a',
'bg_medium': '#3a3a4a',
'bg_light': '#4a4a5a',
'accent': '#5566aa',
'accent_light': '#7788cc',
'text': '#ccccee',
'text_dim': '#8888aa',
'highlight': '#4488ff',
'led_on': '#44ff88',
'led_off': '#333344',
'orange': '#ffaa44',
}
def __init__(self):
# Create main window
self.root = tk.Tk()
self.root.title("Pythonic Drum Synthesizer")
self.root.configure(bg=self.COLORS['bg_dark'])
self.root.resizable(True, True)
self.root.minsize(800, 480) # Compact minimum window size
self.root.geometry("960x600") # Compact default window size
# Initialize preferences first (needed for sample rate)
self.preferences_manager = PreferencesManager()
# Audio output rate (must match device) and internal synthesis rate
self.sample_rate = self.preferences_manager.get('audio_sample_rate', 44100)
self.synth_sample_rate = self.preferences_manager.get('synth_sample_rate', 44100)
# Synth rate should not exceed output rate (would waste CPU)
if self.synth_sample_rate > self.sample_rate:
self.synth_sample_rate = self.sample_rate
self._resample_ratio = self.sample_rate / self.synth_sample_rate
# Initialize synthesizer at the internal synthesis rate
self.synth = PythonicSynthesizer(self.synth_sample_rate,
parallel_channel_processing=True)
# Apply mono mode
mono_mode = self.preferences_manager.get('audio_mono', False)
self.synth.set_mono(mono_mode)
# Apply saved parameter smoothing time to all channels
smoothing_ms = self.preferences_manager.get('param_smoothing_ms', 30.0)
for channel in self.synth.channels:
channel.set_smoothing_time(smoothing_ms)
# Preset manager
self.preset_manager = PresetManager(self.synth)
# Pattern manager
self.pattern_manager = PatternManager(num_channels=8, pattern_length=16)
# MIDI input manager
self.midi_manager = MidiManager()
self._midi_activity_time = 0 # For activity indicator
self._midi_learn_target = None # Parameter name being learned
self._init_midi()
# Morph manager
self.morph_manager = MorphManager(self.synth)
# Audio state
self.audio_stream = None
self.audio_buffer = np.zeros((2048, 2), dtype=np.float32)
self.buffer_lock = threading.Lock()
# UI state
self.selected_channel = 0
self.updating_ui = False # Prevent feedback loops
self.edit_all_mode = False # When True, knob changes affect all unmuted channels
# Undo/redo state management
self._undo_stack = [] # List of (synth_data, pattern_data) snapshots
self._redo_stack = []
self._max_undo = 50 # Maximum undo levels
self._undo_pending = False # Debounce flag for coalescing rapid changes
# Parameter registry for MIDI CC mapping
# Maps parameter_name -> (widget, min_val, max_val, setter_function)
self._cc_parameter_registry = {}
# Pitch bend state tracking
self._pitchbend_original_value = None # Original param value before pitch bend
self._pitchbend_active = False # True when pitch bend is away from center
self._pitchbend_center_threshold = 0.02 # Consider centered if within this range
# Playback state (thread-safe)
self.last_triggered_step = -1 # Track last triggered step to avoid double triggers
self.frames_since_last_step = 0
self.button_flash_state = False # For flashing playing pattern button
self.current_play_position = 0 # Atomic position for UI updates
self.position_lock = threading.Lock() # Protect position updates
self.ui_update_timer = None # Timer for UI updates
# Fill tracking: list of (frames_until_trigger, channel_id, velocity)
# Fills are triggered at sub-step intervals within the current step
self.pending_fills = []
# Performance monitoring
self.callback_times = deque(maxlen=100) # Last 100 callback times
self.trigger_times = deque(maxlen=100) # Time spent triggering
self.process_times = deque(maxlen=100) # Time spent processing audio
self.underrun_count = 0
self.callback_count = 0
self.last_perf_report = time.time()
# Audio buffer size from preferences
buffer_ms = self.preferences_manager.get('audio_buffer_ms', 23.8)
self._audio_block_size = max(64, int(round(buffer_ms / 1000.0 * self.sample_rate)))
self._buffer_time_ms = (self._audio_block_size / self.sample_rate) * 1000
# Adaptive processing - drop samples to prevent underruns
self.enable_sample_dropping = True # Enable adaptive processing
self.dropped_callback_count = 0
self._last_good_audio = np.zeros((self._audio_block_size, 2), dtype=np.float32)
# Resampling state (for synth_rate != output_rate)
self._resample_out_frames = 0
self._resample_in_frames = 0
self._resample_x_out = None
self._resample_x_in = None
self._resample_buffer = None
# Build the interface
self._build_ui()
# Register parameters for MIDI CC control
self._register_cc_parameters()
# Register undo/redo on all knobs and sliders
self._register_undo_on_widgets()
# Bind keyboard events
self.root.bind('<Key>', self._on_key_press)
self.root.bind('<Control-z>', lambda e: self._on_undo())
self.root.bind('<Control-y>', lambda e: self._on_redo())
# Start audio if available
if AUDIO_AVAILABLE:
self._start_audio()
# Initialize synth BPM from pattern manager (for tempo-synced delay)
self.synth.set_bpm(self.pattern_manager.bpm)
# Update UI with current channel
self._update_ui_from_channel()
self._update_morph_ui()
# Start button state updates
self.root.after(250, self._toggle_button_flash)
# Start UI position update timer (separate from audio thread)
self._start_ui_update_timer()
# Load the last preset if available
self._load_last_preset()
def _build_ui(self):
"""Build the complete user interface
Layout follows this structure (top to bottom):
1. Toolbar (program selector, morph, undo/redo, options)
2. Preset Section (preset name, channel buttons 1-8 in 2 rows, mute buttons)
3. Drum Patch Section (mixing, oscillator, noise, velocity controls)
4. Pattern Section (pattern buttons A-L, step editor, play controls)
5. Global Section (transport, swing, fill rate, master volume)
"""
# Scrollable main container
outer_frame = tk.Frame(self.root, bg=self.COLORS['bg_dark'])
outer_frame.pack(fill='both', expand=True)
self._canvas = tk.Canvas(outer_frame, bg=self.COLORS['bg_dark'],
highlightthickness=0)
self._vscrollbar = tk.Scrollbar(outer_frame, orient='vertical',
command=self._canvas.yview)
self._canvas.configure(yscrollcommand=self._vscrollbar.set)
self._vscrollbar.pack(side='right', fill='y')
self._canvas.pack(side='left', fill='both', expand=True)
main_frame = tk.Frame(self._canvas, bg=self.COLORS['bg_dark'])
self._canvas_window = self._canvas.create_window((0, 0), window=main_frame,
anchor='nw')
# Resize canvas scroll region when content changes
def _on_frame_configure(event):
self._canvas.configure(scrollregion=self._canvas.bbox('all'))
main_frame.bind('<Configure>', _on_frame_configure)
# Make canvas width follow window width
def _on_canvas_configure(event):
self._canvas.itemconfig(self._canvas_window, width=event.width)
self._canvas.bind('<Configure>', _on_canvas_configure)
# Bind mouse wheel to scroll
def _on_mousewheel(event):
self._canvas.yview_scroll(int(-1 * (event.delta / 120)), 'units')
self._canvas.bind_all('<MouseWheel>', _on_mousewheel)
# Toolbar (top bar with program selector, morph slider)
self._build_toolbar(main_frame)
# Preset section (preset name, channel buttons in 2 rows)
self._build_preset_section(main_frame)
# Drum Patch section (main controls)
self._build_drum_patch_section(main_frame)
# Pattern section
self._build_pattern_section(main_frame)
# Global section (transport, master volume)
self._build_global_section(main_frame)
def _build_toolbar(self, parent):
"""Build toolbar with program selector, sound morph, and options
Toolbar layout:
- Left: Program selector (1-16)
- Center: Sound Morph slider
- Right: Undo/Redo, option buttons, master volume
"""
toolbar = tk.Frame(parent, bg=self.COLORS['bg_medium'], height=32)
toolbar.pack(fill='x', pady=(0, 2))
toolbar.pack_propagate(False)
# Left side: Program selector
program_frame = tk.Frame(toolbar, bg=self.COLORS['bg_medium'])
program_frame.pack(side='left', padx=3, pady=2)
tk.Label(program_frame, text="program:",
font=('Segoe UI', 7), fg=self.COLORS['text_dim'],
bg=self.COLORS['bg_medium']).pack(side='left', padx=(0, 3))
self.program_var = tk.StringVar(value="1")
self.program_combo = ttk.Combobox(program_frame, textvariable=self.program_var,
values=[str(i) for i in range(1, 17)],
width=3, state='readonly')
self.program_combo.pack(side='left')
self.program_combo.bind('<<ComboboxSelected>>', self._on_program_select)
# Center: Sound Morph slider with A/B learn buttons
morph_frame = tk.Frame(toolbar, bg=self.COLORS['bg_medium'])
morph_frame.pack(side='left', padx=10, pady=2, expand=True)
tk.Label(morph_frame, text="sound morph:",
font=('Segoe UI', 7), fg=self.COLORS['text_dim'],
bg=self.COLORS['bg_medium']).pack(side='left', padx=(0, 3))
# Learn A button (small square)
self.morph_learn_a_btn = tk.Button(
morph_frame, text="A", width=2, height=1,
font=('Segoe UI', 7, 'bold'),
bg='#444455', fg=self.COLORS['text_dim'],
activebackground='#555566',
relief='flat', bd=1,
command=self._on_morph_learn_a)
self.morph_learn_a_btn.pack(side='left', padx=(0, 2))
self.morph_slider = tk.Scale(morph_frame, from_=0, to=100,
orient='horizontal', length=120,
bg=self.COLORS['bg_medium'],
fg=self.COLORS['text'],
highlightthickness=0,
troughcolor=self.COLORS['bg_dark'],
command=self._on_morph_change)
self.morph_slider.pack(side='left')
# Learn B button (small square)
self.morph_learn_b_btn = tk.Button(
morph_frame, text="B", width=2, height=1,
font=('Segoe UI', 7, 'bold'),
bg='#444455', fg=self.COLORS['text_dim'],
activebackground='#555566',
relief='flat', bd=1,
command=self._on_morph_learn_b)
self.morph_learn_b_btn.pack(side='left', padx=(2, 0))
# Right side: Undo/Redo, options, master volume
right_frame = tk.Frame(toolbar, bg=self.COLORS['bg_medium'])
right_frame.pack(side='right', padx=3, pady=2)
# Undo/Redo buttons
self.undo_btn = tk.Button(right_frame, text="↶", width=2, height=1,
font=('Segoe UI', 10),
bg=self.COLORS['bg_light'],
fg=self.COLORS['text'],
state='disabled',
command=self._on_undo)
self.undo_btn.pack(side='left', padx=1)
self.redo_btn = tk.Button(right_frame, text="↷", width=2, height=1,
font=('Segoe UI', 10),
bg=self.COLORS['bg_light'],
fg=self.COLORS['text'],
state='disabled',
command=self._on_redo)
self.redo_btn.pack(side='left', padx=1)
# Separator
tk.Frame(right_frame, width=10, bg=self.COLORS['bg_medium']).pack(side='left')
# Master volume
tk.Label(right_frame, text="master",
font=('Segoe UI', 7), fg=self.COLORS['text_dim'],
bg=self.COLORS['bg_medium']).pack(side='left', padx=(5, 2))
self.master_knob = RotaryKnob(right_frame, size=28,
min_val=-60, max_val=10, default=0,
command=self._on_master_volume_change)
self.master_knob.pack(side='left')
# MIDI activity indicator
tk.Frame(right_frame, width=5, bg=self.COLORS['bg_medium']).pack(side='left')
self.midi_indicator = tk.Canvas(right_frame, width=12, height=12,
bg=self.COLORS['bg_medium'],
highlightthickness=0)
self.midi_indicator.pack(side='left', padx=(5, 2))
self._midi_indicator_id = self.midi_indicator.create_oval(
2, 2, 10, 10, fill=self.COLORS['led_off'], outline='')
# Bind click to open MIDI preferences
self.midi_indicator.bind('<Button-1>', lambda e: self._show_midi_preferences())
def _build_header(self, parent):
"""Build header with title and program selector - DEPRECATED, use _build_toolbar"""
# This method kept for backwards compatibility but not used
pass
def _build_preset_section(self, parent):
"""Build the preset/channel selection section
Toolbar layout:
- Left: Logo "PYTHONIC"
- Center: Preset name display with prev/next buttons
- Right: Channel buttons 1-8 in SINGLE row with small mute buttons, patch name below
"""
preset_section = tk.Frame(parent, bg=self.COLORS['bg_medium'])
preset_section.pack(fill='x', pady=(0, 2))
# Left: Logo/Title
logo_frame = tk.Frame(preset_section, bg=self.COLORS['bg_medium'])
logo_frame.pack(side='left', padx=5, pady=2)
tk.Label(logo_frame, text="PYTHONIC",
font=('Segoe UI', 11, 'bold'), fg=self.COLORS['accent_light'],
bg=self.COLORS['bg_medium']).pack()
# PO-32 Transfer button
self.po32_btn = tk.Button(logo_frame, text="PO-32",
font=('Segoe UI', 7, 'bold'),
bg=self.COLORS['bg_light'],
fg=self.COLORS['orange'],
activebackground=self.COLORS['bg_medium'],
width=5, height=1,
command=self._show_po32_transfer,
relief='raised', bd=1)
self.po32_btn.pack(pady=(2, 0))
# Center-left: Preset name display with navigation
preset_nav_frame = tk.Frame(preset_section, bg=self.COLORS['bg_medium'])
preset_nav_frame.pack(side='left', padx=5, pady=2)
tk.Button(preset_nav_frame, text="◀", width=2, height=1,
font=('Segoe UI', 8),
bg=self.COLORS['bg_light'],
fg=self.COLORS['text'],
command=self._on_preset_prev).pack(side='left', padx=1)
self.preset_combo = ttk.Combobox(preset_nav_frame, width=25, state='readonly')
self.preset_combo.pack(side='left', padx=2)
self.preset_combo.bind('<<ComboboxSelected>>', self._on_preset_combo_select)
tk.Button(preset_nav_frame, text="▶", width=2, height=1,
font=('Segoe UI', 8),
bg=self.COLORS['bg_light'],
fg=self.COLORS['text'],
command=self._on_preset_next).pack(side='left', padx=1)
tk.Button(preset_nav_frame, text="▼", width=2, height=1,
font=('Segoe UI', 7),
bg=self.COLORS['accent'],
fg=self.COLORS['text'],
command=self._on_preset_menu).pack(side='left', padx=2)
self._refresh_preset_list()
# Right side: Channel buttons 1-8 in SINGLE ROW
channels_container = tk.Frame(preset_section, bg=self.COLORS['bg_medium'])
channels_container.pack(side='right', padx=5, pady=2)
# Patch name display ABOVE channels
self.patch_name_label = tk.Label(channels_container,
text="SC BD Schmack",
font=('Segoe UI', 8),
fg=self.COLORS['text'],
bg=self.COLORS['bg_dark'],
width=18, anchor='center',
relief='sunken', padx=3)
self.patch_name_label.pack(pady=(0, 1))
# Single row of channels 1-8 with number, button, mute
channels_row = tk.Frame(channels_container, bg=self.COLORS['bg_medium'])
channels_row.pack()
self.channel_buttons = []
self.mute_buttons = []
self.channel_type_labels = []
for i in range(8):
ch_frame = tk.Frame(channels_row, bg=self.COLORS['bg_medium'])
ch_frame.pack(side='left', padx=1)
# Channel number label on top
tk.Label(ch_frame, text=str(i + 1), font=('Segoe UI', 7),
fg=self.COLORS['text_dim'],
bg=self.COLORS['bg_medium']).pack()
# Channel button and mute in a row
btn_row = tk.Frame(ch_frame, bg=self.COLORS['bg_medium'])
btn_row.pack()
btn = ChannelButton(btn_row, i, size=24,
command=self._on_channel_select)
btn.pack(side='left')
self.channel_buttons.append(btn)
# Small mute button
mute_btn = ToggleButton(btn_row, text="m", width=14, height=14,
command=lambda en, ch=i: self._on_mute_toggle(ch, en))
mute_btn.pack(side='left', padx=1)
self.mute_buttons.append(mute_btn)
# Drum type label under channel (TR-8 style)
type_lbl = tk.Label(ch_frame, text="", font=('Segoe UI', 6),
fg=self.COLORS['orange'],
bg=self.COLORS['bg_medium'],
width=5, anchor='center')
type_lbl.pack()
self.channel_type_labels.append(type_lbl)
self.channel_buttons[0].set_selected(True)
def _build_pattern_section(self, parent):
"""Build the pattern editor section
Left side: Pattern buttons (A-L), matrix toggle, chain controls
Right side: Pattern editor (trig/acc/fill/len lanes)
"""
pattern_frame = tk.LabelFrame(parent, text="pattern",
font=('Segoe UI', 7),
fg=self.COLORS['text_dim'],
bg=self.COLORS['bg_medium'],
labelanchor='nw')
pattern_frame.pack(fill='x', pady=(0, 2))
# Main horizontal layout: controls on left, editor on right
main_row = tk.Frame(pattern_frame, bg=self.COLORS['bg_medium'])
main_row.pack(fill='both', expand=True, padx=3, pady=2)
# LEFT SIDE: Pattern controls
left_panel = tk.Frame(main_row, bg=self.COLORS['bg_medium'])
left_panel.pack(side='left', fill='y', padx=(0, 10))
# Matrix Editor toggle button
self.matrix_toggle_btn = tk.Button(left_panel, text="⊞", width=2, height=1,
font=('Segoe UI', 8),
bg=self.COLORS['bg_light'],
fg=self.COLORS['text'],
command=self._on_matrix_toggle)
self.matrix_toggle_btn.pack(anchor='w', pady=2)
# Pattern selection buttons (A-L) in 3 rows of 4
btn_container = tk.Frame(left_panel, bg=self.COLORS['bg_medium'])
btn_container.pack(pady=5)
self.pattern_buttons = []
for row in range(3):
row_frame = tk.Frame(btn_container, bg=self.COLORS['bg_medium'])
row_frame.pack()
for col in range(4):
idx = row * 4 + col
name = PatternManager.PATTERN_NAMES[idx]
btn = tk.Button(row_frame, text=name, width=2, height=1,
font=('Segoe UI', 7),
bg=self.COLORS['bg_light'],
fg=self.COLORS['text'],
command=lambda i=idx: self._on_pattern_select(i))
btn.pack(side='left', padx=1, pady=1)
btn.bind('<Button-3>', lambda e, i=idx: self._on_pattern_right_click(i, e))
self.pattern_buttons.append(btn)
self.pattern_buttons[0].config(bg=self.COLORS['highlight'])
# Chain controls
chain_frame = tk.Frame(left_panel, bg=self.COLORS['bg_medium'])
chain_frame.pack(pady=5)
tk.Label(chain_frame, text="- chain -",
font=('Segoe UI', 6), fg=self.COLORS['text_dim'],
bg=self.COLORS['bg_medium']).pack()
chain_btn_frame = tk.Frame(chain_frame, bg=self.COLORS['bg_medium'])
chain_btn_frame.pack()
tk.Button(chain_btn_frame, text="◀◀", width=3,
font=('Segoe UI', 7),
bg=self.COLORS['bg_light'],
fg=self.COLORS['text_dim'],
command=self._on_chain_previous).pack(side='left', padx=1)
tk.Button(chain_btn_frame, text="▶▶", width=3,
font=('Segoe UI', 7),
bg=self.COLORS['bg_light'],
fg=self.COLORS['text_dim'],
command=self._on_chain_next).pack(side='left', padx=1)
# RIGHT SIDE: Pattern editor and controls
right_panel = tk.Frame(main_row, bg=self.COLORS['bg_medium'])
right_panel.pack(side='left', fill='both', expand=True)
# Top row: Menu/Copy/Paste only (fill rate, step rate, swing are in bottom bar)
top_controls = tk.Frame(right_panel, bg=self.COLORS['bg_medium'])
top_controls.pack(fill='x', pady=(0, 5))
# Menu/Copy/Paste (right side)
clipboard_frame = tk.Frame(top_controls, bg=self.COLORS['bg_medium'])
clipboard_frame.pack(side='right', padx=2)
tk.Button(clipboard_frame, text="Menu", width=4,
font=('Segoe UI', 7),
bg=self.COLORS['accent'],
fg=self.COLORS['text'],
command=self._on_pattern_menu).pack(side='left', padx=1)
tk.Button(clipboard_frame, text="Copy", width=4,
font=('Segoe UI', 7),
bg=self.COLORS['bg_light'],
fg=self.COLORS['text_dim'],
command=self._on_pattern_copy).pack(side='left', padx=1)
tk.Button(clipboard_frame, text="Paste", width=4,
font=('Segoe UI', 7),
bg=self.COLORS['bg_light'],
fg=self.COLORS['text_dim'],
command=self._on_pattern_paste).pack(side='left', padx=1)
# Probability mode toggle button
self.prob_mode_btn = tk.Button(clipboard_frame, text="Prob", width=4,
font=('Segoe UI', 7),
bg=self.COLORS['bg_light'],
fg=self.COLORS['text_dim'],
command=self._on_toggle_prob_mode)
self.prob_mode_btn.pack(side='left', padx=1)
self.probability_mode_active = False
# Pattern editor area
self.editors_container = tk.Frame(right_panel, bg=self.COLORS['bg_dark'])
self.editors_container.pack(fill='both', expand=True, pady=2)
# Single channel pattern editor frame
self.single_editor_frame = tk.Frame(self.editors_container, bg=self.COLORS['bg_dark'])
self.single_editor_frame.pack(fill='both', expand=True)
# Channel label + pattern editor
self.current_editor_frame = tk.Frame(self.single_editor_frame, bg=self.COLORS['bg_dark'])
self.current_editor_frame.pack(fill='both', expand=True)
self.pattern_channel_label = tk.Label(self.current_editor_frame, text="ch1",
font=('Segoe UI', 8), fg=self.COLORS['text_dim'],
bg=self.COLORS['bg_dark'], anchor='w')
self.pattern_channel_label.pack(side='left', padx=2)
# Create pattern editors for all 8 channels
self.pattern_editors = []
for ch in range(8):
editor = PatternEditor(self.current_editor_frame, channel_id=ch, pattern_length=16,
num_steps=16,
command=self._on_pattern_edit,
all_channels_command=self._on_pattern_edit_all,
length_change_callback=self._on_pattern_length_change)
self.pattern_editors.append(editor)
# Show first channel's editor
self.pattern_editors[0].pack(side='left', fill='both', expand=True)
self.current_pattern_editor_index = 0
# Matrix editor (hidden by default)
self.matrix_editor = MatrixEditor(self.editors_container, num_channels=8,
num_steps=16,
command=self._on_matrix_edit)
self.matrix_view_active = False
def _build_drum_patch_section(self, parent):
"""Build the main drum patch editing section"""
patch_frame = tk.Frame(parent, bg=self.COLORS['bg_medium'])
patch_frame.pack(fill='x', expand=True, pady=(0, 2))
# Seven subsections — lfo1, lfo2, pump racks to the right of vel
self._build_mixing_section(patch_frame)
self._build_oscillator_section(patch_frame)
self._build_noise_section(patch_frame)
self._build_fx_section(patch_frame)
self._build_velocity_section(patch_frame)
self._build_modulation_section(patch_frame)
def _build_mixing_section(self, parent):
"""Build the mixing controls section"""
section = tk.LabelFrame(parent, text="mixing",
font=('Segoe UI', 7),
fg=self.COLORS['text_dim'],
bg=self.COLORS['bg_medium'],
labelanchor='n')
section.pack(side='left', padx=2, pady=2, fill='both', expand=True)
# Row 1: osc/noise HORIZONTAL mix slider
mix_row = tk.Frame(section, bg=self.COLORS['bg_medium'])
mix_row.pack(pady=2, fill='x')
tk.Label(mix_row, text="osc", font=('Segoe UI', 7),
fg=self.COLORS['text_dim'],
bg=self.COLORS['bg_medium']).pack(side='left', padx=2)
# Horizontal mix slider
# Show oscillator on the left and noise on the right.
# Internal parameter semantics remain 0=noise, 100=oscillator.
self.mix_slider = tk.Scale(mix_row, from_=100, to=0,
orient='horizontal', length=60,
showvalue=False,
bg=self.COLORS['bg_medium'],
fg=self.COLORS['text'],
highlightthickness=0,
troughcolor=self.COLORS['bg_dark'],
command=self._on_mix_change)
self.mix_slider.set(50)
self.mix_slider.pack(side='left', padx=2)
tk.Label(mix_row, text="noise", font=('Segoe UI', 7),
fg=self.COLORS['text_dim'],
bg=self.COLORS['bg_medium']).pack(side='left', padx=2)
# Row 2: EQ Freq knob with frequency labels
freq_row = tk.Frame(section, bg=self.COLORS['bg_medium'])
freq_row.pack(pady=1)
tk.Label(freq_row, text="20Hz", font=('Segoe UI', 6),
fg=self.COLORS['text_dim'],
bg=self.COLORS['bg_medium']).pack(side='left')
self.eq_freq_knob = RotaryKnob(freq_row, size=35,
min_val=20, max_val=20000, default=632,
label="eq freq",
logarithmic=True,
command=self._on_eq_freq_change)
self.eq_freq_knob.pack(side='left', padx=2)
tk.Label(freq_row, text="20kHz", font=('Segoe UI', 6),
fg=self.COLORS['text_dim'],
bg=self.COLORS['bg_medium']).pack(side='left')
# Edit All button
self.edit_all_btn = ToggleButton(section, text="edit all", width=45, height=16,
command=self._on_edit_all_toggle)
self.edit_all_btn.pack(pady=1)
# Row 3: Distortion and EQ Gain
row3 = tk.Frame(section, bg=self.COLORS['bg_medium'])
row3.pack(pady=1)
self.distort_knob = RotaryKnob(row3, size=32,
min_val=0, max_val=100, default=0,
label="distort",
command=self._on_distort_change)
self.distort_knob.pack(side='left', padx=2)
self.eq_gain_knob = RotaryKnob(row3, size=32,
min_val=-40, max_val=40, default=0,
label="eq gain",
command=self._on_eq_gain_change)
self.eq_gain_knob.pack(side='left', padx=2)
# Row 4: Level and Pan
row4 = tk.Frame(section, bg=self.COLORS['bg_medium'])
row4.pack(pady=1)
self.level_knob = RotaryKnob(row4, size=32,
min_val=-60, max_val=10, default=0,
label="level",
command=self._on_level_change)
self.level_knob.pack(side='left', padx=2)
self.pan_knob = RotaryKnob(row4, size=32,
min_val=-100, max_val=100, default=0,
label="pan",
command=self._on_pan_change)
self.pan_knob.pack(side='left', padx=2)
# Row 5: Choke and Output A/B
row5 = tk.Frame(section, bg=self.COLORS['bg_medium'])
row5.pack(pady=1)
self.choke_btn = ToggleButton(row5, text="choke", width=40, height=16,
command=self._on_choke_toggle)
self.choke_btn.pack(side='left', padx=2)
self.output_selector = ModeSelector(row5, options=['A', 'B'], width=45,
command=self._on_output_change)
self.output_selector.pack(side='left', padx=2)
def _build_oscillator_section(self, parent):
"""Build the oscillator controls section"""
section = tk.LabelFrame(parent, text="oscillator",
font=('Segoe UI', 7),
fg=self.COLORS['text_dim'],
bg=self.COLORS['bg_medium'],
labelanchor='n')
section.pack(side='left', padx=2, pady=2, fill='both', expand=True)
# Waveform selector with label
waveform_frame = tk.Frame(section, bg=self.COLORS['bg_medium'])
waveform_frame.pack(pady=1)
tk.Label(waveform_frame, text="waveform", font=('Segoe UI', 7),
fg=self.COLORS['text_dim'],
bg=self.COLORS['bg_medium']).pack()
self.waveform_selector = WaveformSelector(waveform_frame,
command=self._on_waveform_change)
self.waveform_selector.pack()
# Oscillator Frequency with labels
freq_frame = tk.Frame(section, bg=self.COLORS['bg_medium'])
freq_frame.pack(pady=1)
tk.Label(freq_frame, text="20Hz", font=('Segoe UI', 6),
fg=self.COLORS['text_dim'],
bg=self.COLORS['bg_medium']).pack(side='left')
self.osc_freq_knob = RotaryKnob(freq_frame, size=38,
min_val=20, max_val=20000, default=440,
label="osc freq",
logarithmic=True,
command=self._on_osc_freq_change)
self.osc_freq_knob.pack(side='left', padx=2)
tk.Label(freq_frame, text="20kHz", font=('Segoe UI', 6),
fg=self.COLORS['text_dim'],
bg=self.COLORS['bg_medium']).pack(side='left')
# Pitch (tune) offset in semitones
pitch_frame = tk.Frame(section, bg=self.COLORS['bg_medium'])
pitch_frame.pack(pady=1)
self.pitch_knob = RotaryKnob(pitch_frame, size=35,
min_val=-24, max_val=24, default=0,
label="pitch", unit="st",
command=self._on_pitch_change)
self.pitch_knob.pack(side='left', padx=2)
# Pitch modulation mode
pitch_mod_frame = tk.Frame(section, bg=self.COLORS['bg_medium'])
pitch_mod_frame.pack(pady=1)
tk.Label(pitch_mod_frame, text="pitch mod", font=('Segoe UI', 7),
fg=self.COLORS['text_dim'],
bg=self.COLORS['bg_medium']).pack()
self.pitch_mod_mode = ModeSelector(pitch_mod_frame,
options=['Decay', 'Sine', 'Rand'],
command=self._on_pitch_mod_mode_change)
self.pitch_mod_mode.pack()
# Pitch mod amount and rate
pitch_knobs_frame = tk.Frame(section, bg=self.COLORS['bg_medium'])
pitch_knobs_frame.pack(pady=1)
self.pitch_amount_knob = RotaryKnob(pitch_knobs_frame, size=32,
min_val=-120, max_val=120, default=0,
label="amount",
command=self._on_pitch_amount_change)
self.pitch_amount_knob.pack(side='left', padx=2)
self.pitch_rate_knob = RotaryKnob(pitch_knobs_frame, size=32,
min_val=1, max_val=2000, default=100,
label="rate",
logarithmic=True,
command=self._on_pitch_rate_change)
self.pitch_rate_knob.pack(side='left', padx=2)
# Attack and Decay knobs
env_frame = tk.Frame(section, bg=self.COLORS['bg_medium'])
env_frame.pack(pady=1)
self.osc_attack_knob = RotaryKnob(env_frame, size=32,
min_val=0, max_val=10000, default=0,
label="attack",
logarithmic=True,
command=self._on_osc_attack_change)
self.osc_attack_knob.pack(side='left', padx=2)
self.osc_decay_knob = RotaryKnob(env_frame, size=32,
min_val=10, max_val=10000, default=316,
label="decay",
logarithmic=True,
command=self._on_osc_decay_change)
self.osc_decay_knob.pack(side='left', padx=2)
def _build_noise_section(self, parent):
"""Build the noise generator controls section"""
section = tk.LabelFrame(parent, text="noise",
font=('Segoe UI', 7),
fg=self.COLORS['text_dim'],
bg=self.COLORS['bg_medium'],
labelanchor='n')
section.pack(side='left', padx=2, pady=2, fill='both', expand=True)
# Filter mode selector (LP/BP/HP)
filter_mode_frame = tk.Frame(section, bg=self.COLORS['bg_medium'])
filter_mode_frame.pack(pady=1)
tk.Label(filter_mode_frame, text="filter mode", font=('Segoe UI', 7),
fg=self.COLORS['text_dim'],
bg=self.COLORS['bg_medium']).pack()
self.noise_filter_mode = ModeSelector(filter_mode_frame,
options=['LP', 'BP', 'HP'],
command=self._on_noise_filter_mode_change)
self.noise_filter_mode.pack()
# Filter freq with frequency labels
freq_frame = tk.Frame(section, bg=self.COLORS['bg_medium'])
freq_frame.pack(pady=1)
tk.Label(freq_frame, text="20Hz", font=('Segoe UI', 6),
fg=self.COLORS['text_dim'],
bg=self.COLORS['bg_medium']).pack(side='left')
self.noise_freq_knob = RotaryKnob(freq_frame, size=38,
min_val=20, max_val=20000, default=20000,
label="filter freq",
logarithmic=True,
command=self._on_noise_freq_change)
self.noise_freq_knob.pack(side='left', padx=2)
tk.Label(freq_frame, text="20kHz", font=('Segoe UI', 6),
fg=self.COLORS['text_dim'],
bg=self.COLORS['bg_medium']).pack(side='left')
# Filter Q knob
q_frame = tk.Frame(section, bg=self.COLORS['bg_medium'])
q_frame.pack(pady=1)
self.noise_q_knob = RotaryKnob(q_frame, size=32,
min_val=0.5, max_val=20.0, default=0.707,
label="filter q",
logarithmic=True,
command=self._on_noise_q_change)
self.noise_q_knob.pack(side='left', padx=2)
# Stereo toggle button
self.stereo_btn = ToggleButton(q_frame, text="stereo", width=40, height=16,
command=self._on_stereo_toggle)
self.stereo_btn.pack(side='left', padx=2)
# Envelope mode selector
env_mode_frame = tk.Frame(section, bg=self.COLORS['bg_medium'])
env_mode_frame.pack(pady=1)
tk.Label(env_mode_frame, text="envelope", font=('Segoe UI', 7),
fg=self.COLORS['text_dim'],
bg=self.COLORS['bg_medium']).pack()
self.noise_env_mode = ModeSelector(env_mode_frame,
options=['Exp', 'Lin', 'Mod'],
command=self._on_noise_env_mode_change)
self.noise_env_mode.pack()
# Attack and Decay as VERTICAL SLIDERS
env_sliders_frame = tk.Frame(section, bg=self.COLORS['bg_medium'])
env_sliders_frame.pack(pady=1)
self.noise_attack_slider = VerticalSlider(env_sliders_frame, width=22, height=55,
min_val=0, max_val=10000, default=0,
label="attack",
logarithmic=True,
command=self._on_noise_attack_change)
self.noise_attack_slider.pack(side='left', padx=3)
self.noise_decay_slider = VerticalSlider(env_sliders_frame, width=22, height=55,
min_val=10, max_val=10000, default=316,
label="decay",
logarithmic=True,
command=self._on_noise_decay_change)
self.noise_decay_slider.pack(side='left', padx=3)
def _build_velocity_section(self, parent):
"""Build the velocity sensitivity section"""
section = tk.LabelFrame(parent, text="vel",
font=('Segoe UI', 7),
fg=self.COLORS['text_dim'],
bg=self.COLORS['bg_medium'],
labelanchor='n')
section.pack(side='left', padx=2, pady=2, fill='both', expand=True)
# Oscillator velocity
self.osc_vel_slider = VerticalSlider(section, width=22, height=55,
min_val=0, max_val=200, default=0,
label="osc",
command=self._on_osc_vel_change)
self.osc_vel_slider.pack(pady=2)
# Noise velocity
self.noise_vel_slider = VerticalSlider(section, width=22, height=55,
min_val=0, max_val=200, default=0,
label="noise",
command=self._on_noise_vel_change)
self.noise_vel_slider.pack(pady=2)
# Mod velocity
self.mod_vel_slider = VerticalSlider(section, width=22, height=55,
min_val=0, max_val=200, default=0,
label="mod",
command=self._on_mod_vel_change)
self.mod_vel_slider.pack(pady=2)
def _build_modulation_section(self, parent):
"""Build three vertical modulation racks (lfo 1, lfo 2, pump) side-by-side,
packed to the right of the velocity section."""
# Build destination option list once (short labels for narrow comboboxes)
self._mod_target_options = ['Off']
self._mod_target_values = [ModTarget.NONE]
for group_name, targets in MOD_TARGET_GROUPS.items():
for t in targets:
self._mod_target_options.append(MOD_TARGET_LABELS[t])
self._mod_target_values.append(t)