-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbeat_studio_integration.py
More file actions
1075 lines (916 loc) · 41.8 KB
/
beat_studio_integration.py
File metadata and controls
1075 lines (916 loc) · 41.8 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
"""
beat_studio_integration.py - Professional Beat Studio Integration for CodedSwitch
This module integrates the advanced beat generation features into your existing GUI
"""
import os
import sys
import logging
logger = logging.getLogger(__name__) # early logger setup
import numpy as np
import pygame
import threading
import tempfile
from scipy.io import wavfile
from scipy import signal
import json
from typing import Dict, Optional, Tuple, List, Any
from dataclasses import dataclass
import tkinter as tk
# ---------------------------------------------------------------------------
# SafeDoubleVar – Defensive wrapper around tk.DoubleVar
# ---------------------------------------------------------------------------
class _SafeDoubleVar(tk.DoubleVar):
"""A drop-in replacement for ``tk.DoubleVar`` that verifies the value
passed to ``set()`` is numeric (or castable to float). If an invalid
value is detected it is **not** applied and we log the incident instead
of letting Tkinter raise a cryptic ``_tkinter.TclError: expected
floating-point number but got ...`` error. This helps track down the
root cause of mysterious *getdouble()* crashes without killing the GUI.
"""
def set(self, value): # type: ignore[override]
try:
# Accept ints, floats and anything that can cleanly cast to float
if not isinstance(value, (int, float)):
value = float(value)
super().set(float(value))
except Exception:
import traceback, sys
tb = ''.join(traceback.format_stack(limit=10))
msg = (
f"Attempted to set DoubleVar '{getattr(self, '_name', 'unknown')}' "
f"with non-numeric value: {value!r} ({type(value).__name__})\n{tb}"
)
# Send to logger *and* stderr so it shows in console even if logging isn't configured
logger.error(msg)
print(msg, file=sys.stderr)
# Monkey-patch globally so every subsequent ``tk.DoubleVar`` usage picks up the
# safe version without invasive refactors.
tk.DoubleVar = _SafeDoubleVar # type: ignore[attr-defined]
# ---------------------------------------------------------------------------
# Patch low-level ``_tkinter.getdouble`` so it gracefully handles tuple/list
# ---------------------------------------------------------------------------
try:
import _tkinter # type: ignore
_orig_getdouble = getattr(_tkinter, "getdouble", None)
if _orig_getdouble is not None:
def _safe_getdouble(val): # type: ignore[override]
"""Return the first element if a tuple/list is passed to getdouble()."""
if isinstance(val, (tuple, list)) and val:
val = val[0]
return _orig_getdouble(val)
_tkinter.getdouble = _safe_getdouble # type: ignore[attr-defined]
logger.debug("_tkinter.getdouble patched with tuple-safe version")
else:
logger.debug("_tkinter.getdouble not present; skipping patch")
except Exception as _patch_err: # pragma: no cover
logger.debug("Could not patch _tkinter.getdouble: %s", _patch_err)
from tkinter import ttk, messagebox
import ttkbootstrap as ttk
from datetime import datetime
# Default availability flag (updated after import attempt below)
BEAT_STUDIO_AVAILABLE = False
# Try to import the beat studio modules
try:
from beat_studio import (
BeatStudioEngine, PresetManager, EffectsProcessor,
create_beat_from_lyrics, AudioConstants, Scale,
DrumSynthesizer, Synthesizer, MelodyGenerator, Sequencer,
Pattern, Note
)
BEAT_STUDIO_AVAILABLE = True
logger.info("✅ Beat Studio modules loaded successfully!")
except Exception as e:
# Second attempt: try the stand-alone professional module
try:
import beat_studio_professional as beat_studio
BeatStudioEngine = beat_studio.BeatStudioEngine
PresetManager = beat_studio.PresetManager
EffectsProcessor = beat_studio.EffectsProcessor
create_beat_from_lyrics = beat_studio.create_beat_from_lyrics
AudioConstants = beat_studio.AudioConstants
Scale = beat_studio.Scale
DrumSynthesizer = beat_studio.DrumSynthesizer
Synthesizer = beat_studio.Synthesizer
MelodyGenerator = beat_studio.MelodyGenerator
Sequencer = beat_studio.Sequencer
Pattern = beat_studio.Pattern
Note = beat_studio.Note
BEAT_STUDIO_AVAILABLE = True
logger.info("✅ beat_studio_professional loaded as fallback!")
except ImportError:
logger.warning(f"⚠️ Beat Studio modules not available: {e}")
BEAT_STUDIO_AVAILABLE = False
# Create minimal fallback classes to satisfy type references
@dataclass
class Note:
pitch: int = 60
velocity: float = 1.0
duration: float = 1.0
start_time: float = 0.0
@dataclass
class Pattern:
notes: list = None
length: float = 0.0
channel: str = "default"
class BeatStudioEngine:
pass
class PresetManager:
def __init__(self):
self.presets = {}
class EffectsProcessor:
def apply_effects(self, audio, settings=None):
return audio
class AudioConstants:
SAMPLE_RATE = 44100
CHANNELS = 2
BUFFER_SIZE = 512
# ============================================================================
# BEAT STUDIO INTEGRATION CLASS
# ============================================================================
class BeatStudioIntegration:
DRUM_SAMPLE_DIR = os.path.join(os.path.dirname(__file__), "assets", "hq_drums") # kick.wav, snare.wav, hihat.wav, openhat.wav
USE_FLUIDSYNTH = False # toggled via UI
_sample_cache: Dict[str, pygame.mixer.Sound] = {}
"""Integrates professional beat generation into CodedSwitch GUI."""
def __init__(self):
global BEAT_STUDIO_AVAILABLE
self.engine = None
self.preset_manager = None
self.effects_processor = None
self.current_beat = None
self.current_audio_file = None
self.is_playing = False
# Initialize components if available
if BEAT_STUDIO_AVAILABLE:
try:
self.engine = BeatStudioEngine()
self.preset_manager = PresetManager()
self.effects_processor = EffectsProcessor()
logger.info("🎵 Beat Studio components initialized successfully!")
# Pre-load HQ drum samples, but do not fail if audio device is unavailable
try:
self._load_drum_samples()
except Exception as sample_err:
logger.warning(f"Could not load HQ drum samples (continuing without them): {sample_err}")
except Exception as e:
logger.error(f"Failed to initialize Beat Studio components: {e}")
BEAT_STUDIO_AVAILABLE = False
def is_available(self) -> bool:
"""Check if Beat Studio is available and initialized."""
return BEAT_STUDIO_AVAILABLE and self.engine is not None
def add_beat_studio_tab(self, notebook: ttk.Notebook, parent_gui) -> Optional[ttk.Frame]:
"""Add Beat Studio tab to the main notebook."""
if not self.is_available():
logger.warning("Beat Studio not available - tab not added")
return None
# Create the Beat Studio tab
beat_tab = ttk.Frame(notebook)
notebook.add(beat_tab, text=" 🎵 Beat Studio ")
# Set up the Beat Studio interface
self._setup_beat_studio_tab(beat_tab, parent_gui)
logger.info("🎵 Beat Studio tab added successfully!")
return beat_tab
def _setup_beat_studio_tab(self, parent: ttk.Frame, parent_gui):
"""Set up the complete Beat Studio interface.
The UI for Beat Studio is quite tall; on smaller screens the bottom
playback buttons can get clipped by the application status-bar.
Wrapping the entire tab in a scroll-able canvas keeps the layout the
same while letting users scroll to anything that would otherwise be
hidden.
"""
# ------------------------------------------------------------------
# Scrollable container
# ------------------------------------------------------------------
# Create an outer frame that will hold the Canvas + scrollbar.
container = ttk.Frame(parent)
container.pack(fill=tk.BOTH, expand=True)
# Canvas for scrolling and the vertical scrollbar.
canvas = tk.Canvas(container, highlightthickness=0)
vscroll = ttk.Scrollbar(container, orient="vertical", command=canvas.yview)
canvas.configure(yscrollcommand=vscroll.set)
# Geometry management
canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
vscroll.pack(side=tk.RIGHT, fill=tk.Y)
# ------------------------------------------------------------------
# Interior "main_frame" that will host the actual Beat-Studio widgets
# ------------------------------------------------------------------
main_frame = ttk.Frame(canvas, padding=10)
# Add the frame to the canvas window so it becomes scrollable.
canvas_window = canvas.create_window((0, 0), window=main_frame, anchor="nw")
# Make the embedded frame match the full canvas width so unused space disappears
def _on_canvas_config(event):
"""Keep the inner frame width in sync with the canvas."""
canvas.itemconfig(canvas_window, width=event.width)
canvas.bind("<Configure>", _on_canvas_config)
# Whenever the size of main_frame changes, update the scrollregion.
def _on_frame_config(event):
canvas.configure(scrollregion=canvas.bbox("all"))
main_frame.bind("<Configure>", _on_frame_config)
# Enable mouse-wheel scrolling (Windows & Linux). Mac uses different delta.
def _on_mousewheel(event):
# event.delta is negative when scrolling down on Windows
canvas.yview_scroll(int(-1 * (event.delta / 120)), "units")
canvas.bind_all("<MouseWheel>", _on_mousewheel)
# ------------------------------------------------------------------
# Build the regular Beat-Studio UI inside main_frame
# ------------------------------------------------------------------
# Header
header_frame = ttk.Frame(main_frame)
header_frame.pack(fill=tk.X, pady=(0, 15))
ttk.Label(
header_frame,
text="🎵 Professional Beat Studio - AI-Powered Beat Generation",
font=("Segoe UI", 14, "bold")
).pack(side=tk.LEFT)
# Store references
self.parent_gui = parent_gui
# Create main sections
self._create_lyrics_section(main_frame)
self._create_generation_controls(main_frame)
self._create_pattern_editor(main_frame)
self._create_playback_controls(main_frame)
def _create_lyrics_section(self, parent):
"""Create lyrics input section."""
lyrics_frame = ttk.LabelFrame(parent, text="📝 Lyrics for Beat Generation", padding=10)
lyrics_frame.pack(fill=tk.BOTH, expand=True, pady=(0, 10))
# Top controls
controls_frame = ttk.Frame(lyrics_frame)
controls_frame.pack(fill=tk.X, pady=(0, 5))
ttk.Button(
controls_frame,
text="📋 Import from Lyric Lab",
command=self._import_from_lyric_lab,
bootstyle="info"
).pack(side=tk.LEFT, padx=(0, 5))
ttk.Button(
controls_frame,
text="🎯 Analyze Mood",
command=self._analyze_lyrics_mood,
bootstyle="primary"
).pack(side=tk.LEFT, padx=5)
# Lyrics text area
from ttkbootstrap.scrolled import ScrolledText
self.beat_lyrics_text = ScrolledText(
lyrics_frame,
height=6,
wrap=tk.WORD,
font=("Consolas", 10)
)
self.beat_lyrics_text.pack(fill=tk.BOTH, expand=True)
def _create_generation_controls(self, parent):
"""Create beat generation controls."""
control_frame = ttk.LabelFrame(parent, text="🎛️ Beat Generation Controls", padding=10)
control_frame.pack(fill=tk.X, pady=(0, 10))
# Row 1: Presets
preset_frame = ttk.Frame(control_frame)
preset_frame.pack(fill=tk.X, pady=(0, 10))
ttk.Label(preset_frame, text="Preset:", font=("Segoe UI", 10, "bold")).pack(side=tk.LEFT, padx=(0, 5))
self.preset_var = tk.StringVar(value="Custom")
preset_values = ["Custom"] + list(self.preset_manager.presets.keys()) if self.preset_manager else ["Custom"]
preset_combo = ttk.Combobox(
preset_frame,
textvariable=self.preset_var,
values=preset_values,
state="readonly",
width=20
)
preset_combo.pack(side=tk.LEFT, padx=(0, 20))
preset_combo.bind("<<ComboboxSelected>>", self._on_preset_change)
# BPM Control
ttk.Label(preset_frame, text="BPM:", font=("Segoe UI", 10, "bold")).pack(side=tk.LEFT, padx=(0, 5))
self.bpm_var = tk.IntVar(value=120)
bpm_scale = ttk.Scale(
preset_frame,
from_=60,
to=200,
variable=self.bpm_var,
length=150,
orient=tk.HORIZONTAL
)
bpm_scale.pack(side=tk.LEFT, padx=(0, 5))
self.bpm_label = ttk.Label(preset_frame, text="120")
self.bpm_label.pack(side=tk.LEFT, padx=(0, 20))
bpm_scale.configure(command=lambda v: self.bpm_label.config(text=str(int(float(v)))))
# Energy Level
ttk.Label(preset_frame, text="Energy:", font=("Segoe UI", 10, "bold")).pack(side=tk.LEFT, padx=(0, 5))
self.energy_var = tk.IntVar(value=7)
energy_scale = ttk.Scale(
preset_frame,
from_=1,
to=10,
variable=self.energy_var,
length=100,
orient=tk.HORIZONTAL
)
energy_scale.pack(side=tk.LEFT, padx=(0, 5))
self.energy_label = ttk.Label(preset_frame, text="7")
self.energy_label.pack(side=tk.LEFT)
energy_scale.configure(command=lambda v: self.energy_label.config(text=str(int(float(v)))))
# Row 2: Style controls
style_frame = ttk.Frame(control_frame)
style_frame.pack(fill=tk.X)
ttk.Label(style_frame, text="Pattern:", font=("Segoe UI", 10, "bold")).pack(side=tk.LEFT, padx=(0, 5))
self.pattern_var = tk.StringVar(value="trap")
pattern_combo = ttk.Combobox(
style_frame,
textvariable=self.pattern_var,
values=["basic", "trap", "hiphop", "drill", "experimental"],
state="readonly",
width=15
)
pattern_combo.pack(side=tk.LEFT, padx=(0, 20))
ttk.Label(style_frame, text="Scale:", font=("Segoe UI", 10, "bold")).pack(side=tk.LEFT, padx=(0, 5))
self.scale_var = tk.StringVar(value="MINOR")
scale_combo = ttk.Combobox(
style_frame,
textvariable=self.scale_var,
values=["MAJOR", "MINOR", "PENTATONIC", "BLUES"],
state="readonly",
width=15
)
scale_combo.pack(side=tk.LEFT, padx=(0, 20))
# Generate button
self.generate_btn = ttk.Button(
style_frame,
text="🎵 Generate Beat",
command=self._generate_beat,
bootstyle="success",
width=20
)
self.generate_btn.pack(side=tk.RIGHT)
def _create_pattern_editor(self, parent):
"""Create visual pattern editor."""
pattern_frame = ttk.LabelFrame(parent, text="🎹 Pattern Editor", padding=10)
pattern_frame.pack(fill=tk.X, pady=(0, 10))
# Pattern grid header
header_frame = ttk.Frame(pattern_frame)
header_frame.pack(fill=tk.X, pady=(0, 5))
ttk.Label(header_frame, text="", width=10).pack(side=tk.LEFT) # Spacer for instrument names
for i in range(1, 17):
ttk.Label(
header_frame,
text=str(i),
width=3,
font=("Courier", 8, "bold" if i % 4 == 1 else "normal")
).pack(side=tk.LEFT)
# Initialize patterns
self.patterns = {
'kick': [1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0],
'snare': [0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0],
'hihat': [1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0],
'openhat': [0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1]
}
self.pattern_buttons = {}
# Create pattern rows
instruments = [
("🥁 Kick", "kick", "#FF5722"),
("🥁 Snare", "snare", "#2196F3"),
("🎩 Hi-Hat", "hihat", "#4CAF50"),
("🎩 Open Hat", "openhat", "#FFC107")
]
for inst_name, inst_key, color in instruments:
row_frame = ttk.Frame(pattern_frame)
row_frame.pack(fill=tk.X, pady=2)
ttk.Label(row_frame, text=inst_name, width=10).pack(side=tk.LEFT)
self.pattern_buttons[inst_key] = []
for i in range(16):
btn = tk.Button(
row_frame,
text="●" if self.patterns[inst_key][i] else "○",
width=2,
height=1,
font=("Arial", 8),
bg=color if self.patterns[inst_key][i] else "gray80",
fg="white",
relief=tk.FLAT,
command=lambda k=inst_key, idx=i: self._toggle_pattern(k, idx, color)
)
btn.pack(side=tk.LEFT, padx=1)
self.pattern_buttons[inst_key].append(btn)
# Drum source selector
source_frame = ttk.Frame(parent)
source_frame.pack(fill=tk.X, pady=(5, 0))
ttk.Label(source_frame, text="Drum Source:", font=("Segoe UI", 10, "bold")).pack(side=tk.LEFT, padx=(0, 5))
self.drum_source_var = tk.StringVar(value="Synth")
ttk.Combobox(source_frame,
textvariable=self.drum_source_var,
values=["Synth", "HQ Samples"],
state="readonly",
width=12).pack(side=tk.LEFT)
def _create_playback_controls(self, parent):
"""Create playback and export controls."""
playback_frame = ttk.LabelFrame(parent, text="🎧 Playback & Export", padding=10)
playback_frame.pack(fill=tk.X)
# Status label
self.status_label = ttk.Label(
playback_frame,
text="Ready to generate beats!",
font=("Segoe UI", 10)
)
self.status_label.pack(pady=(0, 10))
# Progress bar
self.progress_var = tk.DoubleVar()
self.progress_bar = ttk.Progressbar(
playback_frame,
variable=self.progress_var,
maximum=100,
mode='determinate'
)
self.progress_bar.pack(fill=tk.X, pady=(0, 10))
# Control buttons
button_frame = ttk.Frame(playback_frame)
button_frame.pack()
self.play_btn = ttk.Button(
button_frame,
text="▶️ Play",
command=self._play_beat,
bootstyle="primary",
state=tk.DISABLED,
width=12
)
self.play_btn.pack(side=tk.LEFT, padx=5)
self.stop_btn = ttk.Button(
button_frame,
text="⏹️ Stop",
command=self._stop_beat,
state=tk.DISABLED,
width=12
)
self.stop_btn.pack(side=tk.LEFT, padx=5)
ttk.Button(
button_frame,
text="💾 Save WAV",
command=self._save_beat,
bootstyle="info",
width=12
).pack(side=tk.LEFT, padx=5)
ttk.Button(
button_frame,
text="📤 Export Project",
command=self._export_project,
bootstyle="warning",
width=12
).pack(side=tk.LEFT, padx=5)
# Effects controls
effects_frame = ttk.Frame(playback_frame)
effects_frame.pack(fill=tk.X, pady=(10, 0))
ttk.Label(effects_frame, text="Effects:", font=("Segoe UI", 10, "bold")).pack(side=tk.LEFT, padx=(0, 10))
self.reverb_var = tk.BooleanVar(value=True)
ttk.Checkbutton(effects_frame, text="Reverb", variable=self.reverb_var).pack(side=tk.LEFT, padx=5)
self.compression_var = tk.BooleanVar(value=True)
ttk.Checkbutton(effects_frame, text="Compression", variable=self.compression_var).pack(side=tk.LEFT, padx=5)
ttk.Label(effects_frame, text="Master Volume:", font=("Segoe UI", 10, "bold")).pack(side=tk.LEFT, padx=(20, 5))
self.volume_var = tk.DoubleVar(value=0.8)
volume_scale = ttk.Scale(
effects_frame,
from_=0.0,
to=1.0,
variable=self.volume_var,
length=100,
orient=tk.HORIZONTAL
)
volume_scale.pack(side=tk.LEFT)
# ============================================================================
# FUNCTIONALITY METHODS
# ============================================================================
def _import_from_lyric_lab(self):
"""Import lyrics from the Lyric Lab tab."""
try:
if hasattr(self.parent_gui, 'lyric_editor'):
lyrics = self.parent_gui.lyric_editor.get("1.0", tk.END).strip()
if lyrics:
self.beat_lyrics_text.delete("1.0", tk.END)
self.beat_lyrics_text.insert("1.0", lyrics)
self.status_label.config(text="✅ Lyrics imported from Lyric Lab!")
else:
messagebox.showwarning("No Lyrics", "No lyrics found in Lyric Lab!")
else:
messagebox.showinfo("Import", "Switch to Lyric Lab tab and create some lyrics first!")
except Exception as e:
logger.error(f"Failed to import lyrics: {e}")
messagebox.showerror("Import Error", f"Failed to import lyrics: {str(e)}")
def _analyze_lyrics_mood(self):
"""Analyze lyrics mood and adjust settings."""
lyrics = self.beat_lyrics_text.get("1.0", tk.END).strip()
if not lyrics:
messagebox.showwarning("No Lyrics", "Enter some lyrics first!")
return
try:
# Use the engine's mood analysis
params = self.engine.analyze_lyrics_mood(lyrics)
# Update UI with suggested parameters
self.bpm_var.set(params['tempo'])
self.scale_var.set(params['scale'].name)
self.pattern_var.set(params['drum_pattern'])
self.status_label.config(text=f"🎯 Mood analyzed! Suggested: {params['tempo']} BPM, {params['scale'].name} scale")
except Exception as e:
logger.error(f"Mood analysis failed: {e}")
self.status_label.config(text="❌ Mood analysis failed")
def _on_preset_change(self, event=None):
"""Handle preset selection change."""
preset_name = self.preset_var.get()
if preset_name != "Custom" and preset_name in self.preset_manager.presets:
preset = self.preset_manager.presets[preset_name]
# Apply preset settings
if 'tempo' in preset:
self.bpm_var.set(preset['tempo'])
if 'drum_pattern' in preset:
self.pattern_var.set(preset['drum_pattern'])
if 'scale' in preset:
self.scale_var.set(preset['scale'].upper())
self.status_label.config(text=f"✅ Loaded preset: {preset_name}")
def _toggle_pattern(self, instrument: str, index: int, color: str):
"""Toggle a beat in the pattern."""
self.patterns[instrument][index] = 1 - self.patterns[instrument][index]
btn = self.pattern_buttons[instrument][index]
is_active = self.patterns[instrument][index]
btn.config(
text="●" if is_active else "○",
bg=color if is_active else "gray80"
)
# Play preview sound when activating a step
try:
if is_active and self.engine and hasattr(self.engine, 'preview_drum'):
if self.drum_source_var.get() == "HQ Samples" and instrument in self._sample_cache:
self._sample_cache[instrument].play()
else:
self.engine.preview_drum(instrument)
except Exception as e:
logger.debug(f"Preview error: {e}")
def _convert_pattern_to_notes(self, channel: str) -> List[Note]:
"""Convert UI pattern to Note objects."""
notes = []
# Map UI patterns to drum sounds
drum_mapping = {
'kick': 36, # MIDI kick drum
'snare': 38, # MIDI snare
'hihat': 42, # MIDI closed hi-hat
'openhat': 46 # MIDI open hi-hat
}
for instrument, pattern in self.patterns.items():
if instrument in drum_mapping:
for i, active in enumerate(pattern):
if active:
notes.append(Note(
pitch=drum_mapping[instrument],
velocity=0.8,
duration=0.1,
start_time=i * 0.25 # 16th notes
))
return notes
def _update_progress(self, value: float, message: str):
"""Thread-safe progress bar + status update."""
def _apply():
try:
self.progress_var.set(value)
self.status_label.config(text=message)
except Exception as e:
logger.debug(f"Progress update error: {e}")
# Run on main thread via Tk's event loop
root = getattr(self.parent_gui, 'root', None) if hasattr(self, 'parent_gui') else None
if root and callable(getattr(root, 'after', None)):
root.after(0, _apply)
else:
# Fallback (non-GUI context) – just call directly
_apply()
def _play_beat(self):
"""Play the generated beat."""
if not self.current_audio_file or not os.path.exists(self.current_audio_file):
messagebox.showwarning("No Beat", "Generate a beat first!")
return
try:
self.engine.play_audio(self.current_beat)
self.play_btn.config(state=tk.DISABLED)
self.stop_btn.config(state=tk.NORMAL)
self.status_label.config(text="🎵 Playing beat...")
self.is_playing = True
# Re-enable play button after playback
def on_playback_complete():
self.play_btn.config(state=tk.NORMAL)
self.stop_btn.config(state=tk.DISABLED)
self.status_label.config(text="✅ Playback complete")
self.is_playing = False
# Check playback status in background
thread = threading.Thread(target=self._monitor_playback, args=(on_playback_complete,))
thread.daemon = True
thread.start()
except Exception as e:
logger.error(f"Playback error: {e}")
messagebox.showerror("Playback Error", f"Failed to play beat: {str(e)}")
def _monitor_playback(self, callback):
"""Monitor playback status."""
while pygame.mixer.get_busy():
pygame.time.Clock().tick(10)
callback()
def _stop_beat(self):
"""Stop beat playback."""
try:
self.engine.stop_playback()
self.play_btn.config(state=tk.NORMAL)
self.stop_btn.config(state=tk.DISABLED)
self.status_label.config(text="⏹️ Stopped")
self.is_playing = False
except Exception as e:
logger.error(f"Stop error: {e}")
def _save_beat(self):
"""Save the beat as WAV file."""
if self.current_beat is None:
messagebox.showwarning("No Beat", "Generate a beat first!")
return
from tkinter import filedialog
filename = filedialog.asksaveasfilename(
defaultextension=".wav",
filetypes=[("WAV files", "*.wav"), ("All files", "*.*")],
title="Save Beat As"
)
if filename:
try:
self.engine.save_audio(self.current_beat, filename)
messagebox.showinfo("Success", f"Beat saved to {filename}")
self.status_label.config(text=f"💾 Saved: {os.path.basename(filename)}")
except Exception as e:
logger.error(f"Save error: {e}")
messagebox.showerror("Save Error", f"Failed to save beat: {str(e)}")
def _export_project(self):
"""Export complete beat project."""
if self.current_beat is None:
messagebox.showwarning("No Beat", "Generate a beat first!")
return
from tkinter import filedialog
filename = filedialog.asksaveasfilename(
defaultextension=".json",
filetypes=[("JSON files", "*.json"), ("All files", "*.*")],
title="Export Beat Project"
)
if filename:
try:
project_data = {
"lyrics": self.beat_lyrics_text.get("1.0", tk.END).strip(),
"settings": {
"bpm": self.bpm_var.get(),
"pattern": self.pattern_var.get(),
"scale": self.scale_var.get(),
"energy": self.energy_var.get(),
"preset": self.preset_var.get()
},
"patterns": self.patterns,
"effects": {
"reverb": self.reverb_var.get(),
"compression": self.compression_var.get(),
"volume": self.volume_var.get()
},
"metadata": {
"created": datetime.now().isoformat(),
"version": "2.1"
}
}
with open(filename, 'w', encoding='utf-8') as f:
json.dump(project_data, f, indent=2)
messagebox.showinfo("Success", f"Project exported to {filename}")
self.status_label.config(text=f"📤 Exported: {os.path.basename(filename)}")
except Exception as e:
logger.error(f"Export error: {e}")
messagebox.showerror("Export Error", f"Failed to export project: {str(e)}")
# ============================================================================
# PUBLIC API METHODS
# ============================================================================
def create_beat_from_lyrics(self, lyrics: str, preset_name: Optional[str] = None) -> Optional[np.ndarray]:
"""Create a beat from lyrics - public API method."""
if not self.is_available():
logger.warning("Beat Studio not available")
return None
try:
# Apply preset if specified
if preset_name and preset_name in self.preset_manager.presets:
preset = self.preset_manager.presets[preset_name]
# Apply preset parameters
tempo = preset.get('tempo', 120)
pattern = preset.get('drum_pattern', 'basic')
else:
tempo = 120
pattern = 'basic'
# Generate beat
self.engine.sequencer.tempo = tempo
audio_data = self.engine.generate_beat_from_lyrics(lyrics, duration=16.0)
# Apply default effects
audio_data = self.effects_processor.reverb(audio_data, 0.3)
audio_data = self.effects_processor.compressor(audio_data)
self.current_beat = audio_data
return audio_data
except Exception as e:
logger.error(f"Failed to create beat: {e}")
return None
def play_current_beat(self) -> bool:
"""Play the current beat."""
if self.current_beat is not None:
try:
self.engine.play_audio(self.current_beat)
return True
except Exception as e:
logger.error(f"Playback error: {e}")
return False
return False
def stop_beat(self):
"""Stop beat playback."""
try:
self.engine.stop_playback()
except Exception as e:
logger.error(f"Stop error: {e}")
def save_beat(self, filename: str) -> bool:
"""Save the current beat to file."""
if self.current_beat is not None:
try:
self.engine.save_audio(self.current_beat, filename)
return True
except Exception as e:
logger.error(f"Save error: {e}")
return False
return False
# ----------------------------------------------------------------------------
# Stand-alone wrapper for Generate Beat button
# ----------------------------------------------------------------------------
def _generate_beat(self):
"""Handle Generate Beat button click – starts background generation."""
lyrics = self.beat_lyrics_text.get("1.0", tk.END).strip()
if not lyrics:
messagebox.showwarning("No Lyrics", "Enter some lyrics to generate a beat!")
return
# Disable controls while generating
self.generate_btn.config(state=tk.DISABLED, text="🎵 Generating...")
self.progress_var.set(0)
self.status_label.config(text="🎵 Generating your beat...")
# Run generation in background thread
thread = threading.Thread(target=self._generate_beat_thread, args=(lyrics,))
thread.daemon = True
thread.start()
# ----------------------------------------------------------------------------
# Stand-alone background thread for beat generation (module level)
# ----------------------------------------------------------------------------
def _generate_beat_thread(self, lyrics: str):
"""Background thread that carries out beat generation end-to-end."""
try:
self._update_progress(20, "Analyzing lyrics…")
# Tempo
self.engine.sequencer.tempo = self.bpm_var.get()
# Apply pattern from UI
drum_pattern = Pattern(
notes=self._convert_pattern_to_notes('drums'),
length=4.0,
channel='drums'
)
self.engine.sequencer.add_pattern('drums', drum_pattern)
self._update_progress(40, "Generating beat…")
audio_data = self.engine.generate_beat_from_lyrics(lyrics, duration=16.0)
self._update_progress(60, "Applying effects…")
if getattr(self, 'reverb_var', None) and self.reverb_var.get():
audio_data = self.effects_processor.reverb(audio_data, 0.3)
if getattr(self, 'compression_var', None) and self.compression_var.get():
audio_data = self.effects_processor.compressor(audio_data)
audio_data *= getattr(self, 'volume_var', tk.DoubleVar(value=1.0)).get()
self._update_progress(80, "Finalising…")
tmp = tempfile.mktemp(suffix='.wav')
self.engine.save_audio(audio_data, tmp)
self.current_audio_file = tmp
self.current_beat = audio_data
self._update_progress(100, "✅ Beat generated successfully!")
self.play_btn.config(state=tk.NORMAL)
except Exception as e:
logger.error(f"Beat generation failed: {e}")
self.status_label.config(text=f"❌ Generation failed: {str(e)}")
finally:
self.generate_btn.config(state=tk.NORMAL, text="🎵 Generate Beat")
# ----------------------------------------------------------------------------
# Stand-alone helper to save the beat
# ----------------------------------------------------------------------------
def _save_beat(self):
"""Prompt user to save current beat as WAV."""
if self.current_beat is None:
messagebox.showwarning("No Beat", "Generate a beat first!")
return
from tkinter import filedialog
filename = filedialog.asksaveasfilename(
defaultextension=".wav",
filetypes=[("WAV files", "*.wav"), ("All files", "*.*")],
title="Save Beat As"
)
if filename:
try:
self.engine.save_audio(self.current_beat, filename)
messagebox.showinfo("Success", f"Beat saved to {filename}")
self.status_label.config(text=f"💾 Saved: {os.path.basename(filename)}")
except Exception as e:
logger.error(f"Save error: {e}")
messagebox.showerror("Save Error", str(e))
# ----------------------------------------------------------------------------
# Stand-alone helper to export the full project (lyrics + settings + wav path)
# ----------------------------------------------------------------------------
def _export_project(self):
"""Export the current project (lyrics, settings, and beat file) to JSON."""
if self.current_audio_file is None or not os.path.exists(self.current_audio_file):
messagebox.showwarning("No Beat", "Generate a beat first!")
return
from tkinter import filedialog
filename = filedialog.asksaveasfilename(
defaultextension=".json",
filetypes=[("JSON", "*.json"), ("All files", "*.*")],
title="Export Beat Project"
)
if not filename:
return
try:
project = {
"lyrics": self.beat_lyrics_text.get("1.0", tk.END).strip() if hasattr(self, 'beat_lyrics_text') else "",
"bpm": getattr(self, 'bpm_var', tk.IntVar(value=120)).get(),
"volume": getattr(self, 'volume_var', tk.DoubleVar(value=1.0)).get(),
"reverb": getattr(self, 'reverb_var', tk.BooleanVar(value=False)).get() if hasattr(self, 'reverb_var') else False,
"compression": getattr(self, 'compression_var', tk.BooleanVar(value=False)).get() if hasattr(self, 'compression_var') else False,
"audio_file": self.current_audio_file
}
with open(filename, 'w', encoding='utf-8') as fp:
json.dump(project, fp, indent=2)
messagebox.showinfo("Success", f"Project exported to {filename}")
self.status_label.config(text=f"📤 Exported project: {os.path.basename(filename)}")
except Exception as e:
logger.error(f"Export error: {e}")
messagebox.showerror("Export Error", str(e))
# ----------------------------------------------------------------------------
# Stand-alone wrapper for Stop Beat button
# ----------------------------------------------------------------------------
def _stop_beat(self):
"""Handle Stop button click – stops playback."""
try:
self.engine.stop_playback()
self.play_btn.config(state=tk.NORMAL)
self.stop_btn.config(state=tk.DISABLED)
self.status_label.config(text="⏹️ Stopped")
self.is_playing = False
except Exception as e:
logger.error(f"Stop error: {e}")