-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_debug_logging.py
More file actions
101 lines (67 loc) · 8.26 KB
/
add_debug_logging.py
File metadata and controls
101 lines (67 loc) · 8.26 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
"""
Add detailed debug logging to beat_studio_professional.py to pinpoint the broadcasting error
"""
import re
def add_debug_logging():
"""Add detailed logging to track array shapes during audio generation."""
# Read the current file
with open('beat_studio_professional.py', 'r', encoding='utf-8') as f:
content = f.read()
# Add imports for logging if not already there
if 'import logging' not in content:
content = content.replace('import numpy as np', 'import numpy as np\nimport logging')
# Add debug level logging configuration
if 'logger = logging.getLogger(__name__)' in content:
log_setup = """
# Configure detailed logging
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler('beat_studio_debug.log'),
logging.StreamHandler()
]
)
"""
content = content.replace('logger = logging.getLogger(__name__)', 'logger = logging.getLogger(__name__)' + log_setup)
# Add logging to the drum rendering section
drum_render_pattern = r'(\s+# Render drums\s+for note in drum_pattern\.notes:.*?sound \*= note\.velocity\s+end_sample = min\(start_sample \+ len\(sound\), total_samples\))'
drum_render_replacement = r'\1\n logger.debug(f"Drum note: start_sample={start_sample}, sound.shape={sound.shape}, drum_track[{start_sample}:{end_sample}].shape={drum_track[start_sample:end_sample].shape}")'
content = re.sub(drum_render_pattern, drum_render_replacement, content, flags=re.DOTALL)
# Add logging to the melody rendering section
melody_render_pattern = r'(\s+# Render melody\s+for note in melody_pattern\.notes:.*?sound = self\.synthesizer\.generate_note\(.*?\)\s+end_sample = min\(start_sample \+ len\(sound\), total_samples\))'
melody_render_replacement = r'\1\n logger.debug(f"Melody note: start_sample={start_sample}, sound.shape={sound.shape}, melody_track[{start_sample}:{end_sample}].shape={melody_track[start_sample:end_sample].shape}")'
content = re.sub(melody_render_pattern, melody_render_replacement, content, flags=re.DOTALL)
# Add logging to the bass rendering section
bass_render_pattern = r'(\s+# Render bass\s+for note in bass_pattern\.notes:.*?sound = self\.synthesizer\.generate_note\(.*?\)\s+end_sample = min\(start_sample \+ len\(sound\), total_samples\))'
bass_render_replacement = r'\1\n logger.debug(f"Bass note: start_sample={start_sample}, sound.shape={sound.shape}, bass_track[{start_sample}:{end_sample}].shape={bass_track[start_sample:end_sample].shape}")'
content = re.sub(bass_render_pattern, bass_render_replacement, content, flags=re.DOTALL)
# Add logging to the mixing section
mixing_pattern = r'(\s+# Mix tracks\s+final_mix = \(.*?\) \* AudioConstants\.MASTER_VOLUME)'
mixing_replacement = r' logger.debug(f"Before mixing - drum_track.shape={drum_track.shape}, melody_track.shape={melody_track.shape}, bass_track.shape={bass_track.shape}")\n\1'
content = re.sub(mixing_pattern, mixing_replacement, content, flags=re.DOTALL)
# Add logging to the kick drum generation
kick_pattern = r'(@staticmethod\s+def kick\(duration: float = 0\.5\) -> np\.ndarray:.*?return np\.tanh\(kick \* 2\) \* 0\.8 # Soft clipping)'
kick_replacement = r'\g<0>\n logger.debug(f"Generated kick drum with shape={np.tanh(kick * 2).shape}")'
content = re.sub(kick_pattern, kick_replacement, content, flags=re.DOTALL)
# Add logging to the envelope apply method
envelope_pattern = r'(def apply\(self, signal: np\.ndarray, note_duration: float,.*?return signal \* envelope)'
envelope_replacement = r'\g<0>\n logger.debug(f"Envelope applied - input signal.shape={signal.shape}, envelope.shape={envelope.shape}, output.shape={(signal * envelope).shape}")'
content = re.sub(envelope_pattern, envelope_replacement, content, flags=re.DOTALL)
# Add a direct fix for the most likely broadcasting issue in drum rendering
drum_track_add_pattern = r'(drum_track\[start_sample:end_sample\] \+= sound\[:end_sample - start_sample\])'
drum_track_add_replacement = r'try:\n sound_slice = sound[:end_sample - start_sample]\n if sound_slice.shape != drum_track[start_sample:end_sample].shape:\n logger.warning(f"Shape mismatch! Reshaping sound_slice from {sound_slice.shape} to {drum_track[start_sample:end_sample].shape}")\n # Ensure shapes match by truncating or padding\n target_shape = drum_track[start_sample:end_sample].shape\n if len(sound_slice) > len(drum_track[start_sample:end_sample]):\n sound_slice = sound_slice[:len(drum_track[start_sample:end_sample])]\n elif len(sound_slice) < len(drum_track[start_sample:end_sample]):\n pad_length = len(drum_track[start_sample:end_sample]) - len(sound_slice)\n sound_slice = np.pad(sound_slice, (0, pad_length), "constant")\n drum_track[start_sample:end_sample] += sound_slice\n except Exception as e:\n logger.error(f"Error adding drum sound to track: {e}")'
content = content.replace(drum_track_add_pattern, drum_track_add_replacement)
# Apply similar fixes to melody and bass rendering
melody_track_add_pattern = r'(melody_track\[start_sample:end_sample\] \+= sound\[:end_sample - start_sample\])'
melody_track_add_replacement = r'try:\n sound_slice = sound[:end_sample - start_sample]\n if sound_slice.shape != melody_track[start_sample:end_sample].shape:\n logger.warning(f"Shape mismatch! Reshaping sound_slice from {sound_slice.shape} to {melody_track[start_sample:end_sample].shape}")\n # Ensure shapes match by truncating or padding\n target_shape = melody_track[start_sample:end_sample].shape\n if len(sound_slice) > len(melody_track[start_sample:end_sample]):\n sound_slice = sound_slice[:len(melody_track[start_sample:end_sample])]\n elif len(sound_slice) < len(melody_track[start_sample:end_sample]):\n pad_length = len(melody_track[start_sample:end_sample]) - len(sound_slice)\n sound_slice = np.pad(sound_slice, (0, pad_length), "constant")\n melody_track[start_sample:end_sample] += sound_slice\n except Exception as e:\n logger.error(f"Error adding melody sound to track: {e}")'
content = content.replace(melody_track_add_pattern, melody_track_add_replacement)
bass_track_add_pattern = r'(bass_track\[start_sample:end_sample\] \+= sound\[:end_sample - start_sample\])'
bass_track_add_replacement = r'try:\n sound_slice = sound[:end_sample - start_sample]\n if sound_slice.shape != bass_track[start_sample:end_sample].shape:\n logger.warning(f"Shape mismatch! Reshaping sound_slice from {sound_slice.shape} to {bass_track[start_sample:end_sample].shape}")\n # Ensure shapes match by truncating or padding\n target_shape = bass_track[start_sample:end_sample].shape\n if len(sound_slice) > len(bass_track[start_sample:end_sample]):\n sound_slice = sound_slice[:len(bass_track[start_sample:end_sample])]\n elif len(sound_slice) < len(bass_track[start_sample:end_sample]):\n pad_length = len(bass_track[start_sample:end_sample]) - len(sound_slice)\n sound_slice = np.pad(sound_slice, (0, pad_length), "constant")\n bass_track[start_sample:end_sample] += sound_slice\n except Exception as e:\n logger.error(f"Error adding bass sound to track: {e}")'
content = content.replace(bass_track_add_pattern, bass_track_add_replacement)
# Write back the modified content
with open('beat_studio_professional.py', 'w', encoding='utf-8') as f:
f.write(content)
print("✅ Debug logging added and shape mismatch fixes applied!")
if __name__ == "__main__":
add_debug_logging()