-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsynth_gui.py
More file actions
290 lines (241 loc) · 9.48 KB
/
Copy pathsynth_gui.py
File metadata and controls
290 lines (241 loc) · 9.48 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
import tkinter as tk
from realtime_synth import RealtimeSynth
from inputs import KeyboardInput, MIDIInput, VirtualMIDIInput
class SynthGUI:
def __init__(self):
self.synth = RealtimeSynth(sample_rate=48000, block_size=256)
self.keyboard_input = KeyboardInput(
self.synth,
on_note_callback=self._on_note_event
)
self.midi_input = MIDIInput(
self.synth,
on_note_callback=self._on_note_event
)
self.virtual_midi_input = VirtualMIDIInput(
self.synth,
port_name="KarplusStrong Virtual In",
on_note_callback=self._on_note_event
)
self.input_mode = 'keyboard' # 'keyboard', 'midi', or 'virtual'
self.root = tk.Tk()
self.root.title("Inharmonic Karplus-Strong Synthesizer")
self.root.geometry("550x520")
self._build_gui()
self.root.bind('<KeyPress>', self._on_key_press)
self.root.focus_set()
self.synth.start()
def _build_gui(self):
"""Build all GUI elements."""
# ========== Input Mode Selection ==========
input_frame = tk.LabelFrame(self.root, text="Input Mode", padx=10, pady=10)
input_frame.pack(pady=10, padx=10, fill='x')
self.mode_var = tk.StringVar(value='keyboard')
tk.Radiobutton(
input_frame,
text="Computer Keyboard",
variable=self.mode_var,
value='keyboard',
command=self._on_mode_change
).pack(side=tk.LEFT, padx=5)
tk.Radiobutton(
input_frame,
text="MIDI Hardware",
variable=self.mode_var,
value='midi',
command=self._on_mode_change
).pack(side=tk.LEFT, padx=5)
tk.Radiobutton(
input_frame,
text="Virtual MIDI",
variable=self.mode_var,
value='virtual',
command=self._on_mode_change
).pack(side=tk.LEFT, padx=5)
# ========== MIDI Connection (only visible in MIDI mode) ==========
self.midi_frame = tk.Frame(self.root)
tk.Label(
self.midi_frame,
text="MIDI Port:",
font=("Arial", 9)
).pack(side=tk.LEFT, padx=5)
# MIDI port dropdown
available_ports = self.midi_input.get_available_ports()
if not available_ports:
available_ports = ["No MIDI devices found"]
self.midi_port_var = tk.StringVar(value=available_ports[0])
self.midi_dropdown = tk.OptionMenu(
self.midi_frame,
self.midi_port_var,
*available_ports
)
self.midi_dropdown.pack(side=tk.LEFT, padx=5)
self.midi_connect_btn = tk.Button(
self.midi_frame,
text="Connect",
command=self._on_midi_connect
)
self.midi_connect_btn.pack(side=tk.LEFT, padx=5)
self.midi_status_label = tk.Label(
self.midi_frame,
text="Not connected",
fg="gray"
)
self.midi_status_label.pack(side=tk.LEFT, padx=5)
# ========== Virtual MIDI Status (only visible in virtual mode) ==========
self.virtual_midi_frame = tk.Frame(self.root)
self.virtual_status_label = tk.Label(
self.virtual_midi_frame,
text="Virtual port not open",
fg="gray",
font=("Arial", 9)
)
self.virtual_status_label.pack(pady=5)
# ========== Info Display ==========
info_frame = tk.LabelFrame(self.root, text="Controls", padx=10, pady=10)
info_frame.pack(pady=10, padx=10, fill='x')
self.info_text = tk.Label(
info_frame,
text=self.keyboard_input.get_help_text(),
justify=tk.LEFT,
font=("Courier", 9)
)
self.info_text.pack()
# Octave display (keyboard mode only)
self.octave_frame = tk.Frame(info_frame)
self.octave_frame.pack(pady=5)
tk.Label(
self.octave_frame,
text="Octave:",
font=("Arial", 10)
).pack(side=tk.LEFT, padx=5)
self.octave_label = tk.Label(
self.octave_frame,
text=str(self.keyboard_input.get_octave()),
font=("Arial", 12, "bold")
)
self.octave_label.pack(side=tk.LEFT)
# ========== Parameter Controls ==========
param_frame = tk.LabelFrame(self.root, text="Synthesis Parameters", padx=10, pady=10)
param_frame.pack(pady=10, padx=10, fill='both', expand=True)
# Inharmonicity
tk.Label(param_frame, text="Inharmonicity (B)").pack(anchor='w')
self.inharmonicity_slider = tk.Scale(
param_frame,
from_=0.0, to=0.025,
resolution=0.00001,
orient='horizontal',
length=450,
command=lambda v: self.synth.set_inharmonicity(float(v))
)
self.inharmonicity_slider.set(self.synth.current_inharmonicity)
self.inharmonicity_slider.pack(fill='x', pady=2)
# Damping
tk.Label(param_frame, text="Damping (IIR a1, higher = more damping)").pack(anchor='w')
self.damping_slider = tk.Scale(
param_frame,
from_=0.0, to=0.9,
resolution=0.01,
orient='horizontal',
length=450,
command=lambda v: self.synth.set_damping(float(v))
)
self.damping_slider.set(self.synth.current_damping)
self.damping_slider.pack(fill='x', pady=2)
# Decay
tk.Label(param_frame, text="Decay (Feedback gain)").pack(anchor='w')
self.decay_slider = tk.Scale(
param_frame,
from_=0.9, to=1.0,
resolution=0.001,
orient='horizontal',
length=450,
command=lambda v: self.synth.set_decay(float(v))
)
self.decay_slider.set(self.synth.current_decay)
self.decay_slider.pack(fill='x', pady=2)
# Update UI for initial mode
self._update_mode_ui()
def _on_mode_change(self):
"""Handle input mode change."""
new_mode = self.mode_var.get()
if new_mode == self.input_mode:
return
# Disconnect previous mode
if self.input_mode == 'midi':
self.midi_input.disconnect()
elif self.input_mode == 'virtual':
self.virtual_midi_input.close_port()
self.input_mode = new_mode
self._update_mode_ui()
def _update_mode_ui(self):
"""Update UI elements based on current mode."""
if self.input_mode == 'keyboard':
# Show keyboard info
self.midi_frame.pack_forget()
self.virtual_midi_frame.pack_forget()
self.octave_frame.pack(pady=5)
self.info_text.config(text=self.keyboard_input.get_help_text())
elif self.input_mode == 'midi':
# Show MIDI info
self.octave_frame.pack_forget()
self.virtual_midi_frame.pack_forget()
self.midi_frame.pack(pady=5, padx=10, fill='x')
self.info_text.config(text=self.midi_input.get_help_text())
# Auto-connect if available
available = self.midi_input.get_available_ports()
if available and available[0] != "No MIDI devices found":
self._on_midi_connect()
else: # virtual
# Show virtual MIDI info
self.octave_frame.pack_forget()
self.midi_frame.pack_forget()
self.virtual_midi_frame.pack(pady=5, padx=10, fill='x')
self.info_text.config(text=self.virtual_midi_input.get_help_text())
# Auto-open virtual port
if self.virtual_midi_input.open_port():
port_name = self.virtual_midi_input.get_port_name()
self.virtual_status_label.config(
text=f"✓ Virtual port open: '{port_name}'",
fg="green"
)
else:
self.virtual_status_label.config(
text="✗ Failed to open virtual port",
fg="red"
)
def _on_midi_connect(self):
"""Handle MIDI connect button."""
port_name = self.midi_port_var.get()
if port_name == "No MIDI devices found":
self.midi_status_label.config(text="✗ No devices", fg="red")
return
if self.midi_input.connect(port_name):
self.midi_status_label.config(
text=f"✓ Connected",
fg="green"
)
else:
self.midi_status_label.config(text="✗ Failed", fg="red")
def _on_key_press(self, event):
"""Handle keyboard key press."""
if self.input_mode != 'keyboard':
return
result = self.keyboard_input.handle_key_press(event.char)
if result and result['action'] == 'octave_change':
self.octave_label.config(text=str(result['octave']))
def _on_note_event(self, midi_note, velocity):
"""Callback for note events (for potential future use)."""
# Could update a note display here if desired
pass
def run(self):
"""Start the GUI main loop."""
print("✓ Synthesizer GUI ready!")
print(f" Sample rate: {self.synth.fs} Hz")
print(f" Dispersion sections: {self.synth.synth.dispersion.active_sections}")
try:
self.root.mainloop()
finally:
self.midi_input.disconnect()
self.virtual_midi_input.close_port()
self.synth.stop()