Spaces:
Running
Running
Update rhythma.py
Browse files- rhythma.py +406 -286
rhythma.py
CHANGED
|
@@ -1,291 +1,411 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import gradio as gr
|
| 3 |
import numpy as np
|
| 4 |
-
import matplotlib
|
| 5 |
-
matplotlib.use('Agg') # Set backend BEFORE importing pyplot
|
| 6 |
import matplotlib.pyplot as plt
|
|
|
|
|
|
|
| 7 |
from PIL import Image
|
|
|
|
|
|
|
| 8 |
import soundfile as sf
|
| 9 |
-
import
|
| 10 |
-
import
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
print("
|
| 19 |
-
|
| 20 |
-
print(" Groq LLM analysis and audio transcription features will be disabled.")
|
| 21 |
-
print(" Falling back to local analysis methods.")
|
| 22 |
-
print("*"*40)
|
| 23 |
-
else:
|
| 24 |
-
print("✅ GROQ_API_KEY found. Enabling Groq features.")
|
| 25 |
-
# --- End Environment Variable Check ---
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
# --- Initialize Core Components ---
|
| 29 |
try:
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
except
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import numpy as np
|
|
|
|
|
|
|
| 2 |
import matplotlib.pyplot as plt
|
| 3 |
+
from scipy import signal
|
| 4 |
+
# import pandas as pd # pandas wasn't used, commented out
|
| 5 |
from PIL import Image
|
| 6 |
+
import io
|
| 7 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 8 |
import soundfile as sf
|
| 9 |
+
import os
|
| 10 |
+
import traceback # For better error logging
|
| 11 |
+
|
| 12 |
+
# --- Optional Dependency Handling ---
|
| 13 |
+
try:
|
| 14 |
+
from groq import Groq
|
| 15 |
+
GROQ_AVAILABLE = True
|
| 16 |
+
except ImportError:
|
| 17 |
+
GROQ_AVAILABLE = False
|
| 18 |
+
print("⚠️ Groq package not installed. LLM analysis and transcription disabled.")
|
| 19 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
try:
|
| 21 |
+
from sentence_transformers import SentenceTransformer
|
| 22 |
+
SENTENCE_TRANSFORMER_AVAILABLE = True
|
| 23 |
+
except ImportError:
|
| 24 |
+
SENTENCE_TRANSFORMER_AVAILABLE = False
|
| 25 |
+
print("⚠️ SentenceTransformer not installed. Falling back to simple text matching for analysis.")
|
| 26 |
+
# --- End Optional Dependency Handling ---
|
| 27 |
+
|
| 28 |
+
class RhythmaModulationEngine:
|
| 29 |
+
"""
|
| 30 |
+
Rhythma: The Living Modulation Engine
|
| 31 |
+
A dynamic rhythm-based audio modulation system that creates responsive
|
| 32 |
+
sound experiences based on rhythm patterns and emotional states.
|
| 33 |
+
"""
|
| 34 |
+
|
| 35 |
+
def __init__(self, base_freq=None, modulation_type="sine", rhythm_pattern=None, emotional_state=None):
|
| 36 |
+
"""
|
| 37 |
+
Initialize the RhythmaModulationEngine.
|
| 38 |
+
|
| 39 |
+
Args:
|
| 40 |
+
base_freq (float, optional): The base frequency in Hz. Overridden by emotional_state if provided.
|
| 41 |
+
modulation_type (str): Type of modulation (sine, pulse, chirp). Defaults to "sine".
|
| 42 |
+
rhythm_pattern (str, optional): Pattern type (calm, active, focused, relaxed). Defaults to "calm".
|
| 43 |
+
emotional_state (str, optional): Emotional state (anxious, stressed, calm, etc.). Maps to specific frequencies.
|
| 44 |
+
"""
|
| 45 |
+
self.modulation_type = modulation_type
|
| 46 |
+
self.sample_rate = 44100 # Standard audio sample rate
|
| 47 |
+
|
| 48 |
+
# Define frequency mappings for emotional states (Example frequencies)
|
| 49 |
+
self.emotional_frequencies = {
|
| 50 |
+
"anxious": 396,
|
| 51 |
+
"stressed": 528,
|
| 52 |
+
"calm": 741,
|
| 53 |
+
"sad": 417,
|
| 54 |
+
"angry": 852,
|
| 55 |
+
"fearful": 639,
|
| 56 |
+
"confused": 285,
|
| 57 |
+
"happy": 432,
|
| 58 |
+
"neutral": 440, # Added neutral state
|
| 59 |
+
"focused": 639, # Example mapping for focus intention
|
| 60 |
+
"relaxed": 741, # Example mapping for relax intention
|
| 61 |
+
"active": 528, # Example mapping for active intention
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
# Detailed information about emotional states/frequencies
|
| 65 |
+
self.emotional_info = {
|
| 66 |
+
"anxious": {"name": "Liberating Guilt and Fear", "advice": "The 396 Hz frequency may help release fear and guilt."},
|
| 67 |
+
"stressed": {"name": "Transformation and Miracles", "advice": "The 528 Hz frequency is associated with transformation."},
|
| 68 |
+
"calm": {"name": "Awakening Intuition", "advice": "The 741 Hz frequency is associated with awakening intuition."},
|
| 69 |
+
"sad": {"name": "Facilitating Change", "advice": "The 417 Hz frequency is linked to facilitating change."},
|
| 70 |
+
"angry": {"name": "Returning to Spiritual Order", "advice": "The 852 Hz frequency may aid in returning to inner strength."},
|
| 71 |
+
"fearful": {"name": "Connecting Relationships", "advice": "The 639 Hz frequency is associated with connecting relationships."},
|
| 72 |
+
"confused": {"name": "Quantum Cognition", "advice": "The 285 Hz frequency is believed to influence energy fields."},
|
| 73 |
+
"happy": {"name": "Harmonizing Vibrations", "advice": "The 432 Hz frequency is associated with natural harmony."},
|
| 74 |
+
"neutral": {"name": "Grounded Presence", "advice": "The 440 Hz frequency provides a stable reference point."},
|
| 75 |
+
"focused": {"name": "Clarity and Connection", "advice": "The 639 Hz frequency may support focus and understanding."},
|
| 76 |
+
"relaxed": {"name": "Intuitive Calm", "advice": "The 741 Hz frequency is linked to intuitive states and problem-solving."},
|
| 77 |
+
"active": {"name": "Dynamic Energy", "advice": "The 528 Hz frequency is associated with positive transformation."},
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
# Configure rhythm patterns
|
| 81 |
+
self.rhythm_configs = {
|
| 82 |
+
"calm": {"mod_depth": 0.15, "mod_freq": 0.5, "pulse_width": 0.7, "phase_shift": 0.1, "harmonics": [1.0, 0.5, 0.25, 0.125]},
|
| 83 |
+
"active": {"mod_depth": 0.4, "mod_freq": 2.5, "pulse_width": 0.3, "phase_shift": 0.3, "harmonics": [1.0, 0.7, 0.5, 0.3]},
|
| 84 |
+
"focused": {"mod_depth": 0.25, "mod_freq": 1.5, "pulse_width": 0.5, "phase_shift": 0.2, "harmonics": [1.0, 0.6, 0.3, 0.15]},
|
| 85 |
+
"relaxed": {"mod_depth": 0.2, "mod_freq": 0.3, "pulse_width": 0.8, "phase_shift": 0.05, "harmonics": [1.0, 0.4, 0.2, 0.1]}
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
# Symbolic mapping for rhythm patterns
|
| 89 |
+
self.symbolic_mapping = {
|
| 90 |
+
"calm": "Resonating in the Circle Archetype: completion, wholeness, presence",
|
| 91 |
+
"active": "Resonating in the Spiral Archetype: flow, transition, emergence",
|
| 92 |
+
"focused": "Resonating in the Triangle Archetype: clarity, direction, purpose",
|
| 93 |
+
"relaxed": "Resonating in the Wave Archetype: fluidity, acceptance, surrender"
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
# Determine emotional state and base frequency
|
| 97 |
+
valid_emotional_state = emotional_state if emotional_state and emotional_state in self.emotional_frequencies else None
|
| 98 |
+
self.emotional_state = valid_emotional_state
|
| 99 |
+
|
| 100 |
+
if self.emotional_state:
|
| 101 |
+
self.base_freq = self.emotional_frequencies[self.emotional_state]
|
| 102 |
+
elif base_freq and base_freq > 0: # Check if base_freq override is valid
|
| 103 |
+
self.base_freq = base_freq
|
| 104 |
+
# Try to find a state close to the frequency for info purposes
|
| 105 |
+
min_diff = float('inf')
|
| 106 |
+
closest_state = None
|
| 107 |
+
for state, freq in self.emotional_frequencies.items():
|
| 108 |
+
diff = abs(freq - base_freq)
|
| 109 |
+
if diff < min_diff:
|
| 110 |
+
min_diff = diff
|
| 111 |
+
closest_state = state
|
| 112 |
+
# Only assign if reasonably close (e.g., within 10 Hz)
|
| 113 |
+
if min_diff <= 10:
|
| 114 |
+
self.emotional_state = closest_state # Use for info display only
|
| 115 |
+
else:
|
| 116 |
+
self.emotional_state = None # No specific emotional state tied
|
| 117 |
+
else:
|
| 118 |
+
self.emotional_state = "neutral" # Default state if no emotion/freq provided
|
| 119 |
+
self.base_freq = self.emotional_frequencies[self.emotional_state]
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
# Set rhythm pattern
|
| 123 |
+
valid_rhythm_pattern = rhythm_pattern if rhythm_pattern and rhythm_pattern in self.rhythm_configs else None
|
| 124 |
+
self.rhythm_pattern = valid_rhythm_pattern or "calm" # Default to calm if not provided or invalid
|
| 125 |
+
|
| 126 |
+
# Get current rhythm config
|
| 127 |
+
self.config = self.rhythm_configs.get(self.rhythm_pattern, self.rhythm_configs["calm"])
|
| 128 |
+
|
| 129 |
+
def _generate_base_wave(self, duration):
|
| 130 |
+
"""Generate the base carrier wave"""
|
| 131 |
+
t = np.linspace(0, duration, int(self.sample_rate * duration), endpoint=False)
|
| 132 |
+
# Initial simple sine wave
|
| 133 |
+
base_wave = np.sin(2 * np.pi * self.base_freq * t)
|
| 134 |
+
|
| 135 |
+
# Apply harmonics for richer base sound *before* modulation
|
| 136 |
+
harmonics = self.config["harmonics"]
|
| 137 |
+
rich_wave = np.zeros_like(base_wave)
|
| 138 |
+
for i, harmonic_amp in enumerate(harmonics):
|
| 139 |
+
harmonic_freq = self.base_freq * (i + 1)
|
| 140 |
+
# Ensure harmonic frequency does not exceed Nyquist limit
|
| 141 |
+
if harmonic_freq < self.sample_rate / 2:
|
| 142 |
+
rich_wave += harmonic_amp * np.sin(2 * np.pi * harmonic_freq * t)
|
| 143 |
+
|
| 144 |
+
# Normalize the rich base wave before modulation
|
| 145 |
+
if np.max(np.abs(rich_wave)) > 0:
|
| 146 |
+
rich_wave = rich_wave / np.max(np.abs(rich_wave))
|
| 147 |
+
else:
|
| 148 |
+
rich_wave = base_wave # Fallback if harmonics resulted in zero
|
| 149 |
+
|
| 150 |
+
return t, rich_wave
|
| 151 |
+
|
| 152 |
+
|
| 153 |
+
def _apply_sine_modulation(self, t, carrier):
|
| 154 |
+
"""Apply sine wave amplitude modulation"""
|
| 155 |
+
mod_freq = self.config["mod_freq"]
|
| 156 |
+
mod_depth = self.config["mod_depth"]
|
| 157 |
+
mod_env = 1.0 + mod_depth * np.sin(2 * np.pi * mod_freq * t + self.config["phase_shift"])
|
| 158 |
+
return carrier * mod_env
|
| 159 |
+
|
| 160 |
+
def _apply_pulse_modulation(self, t, carrier):
|
| 161 |
+
"""Apply pulse wave amplitude modulation"""
|
| 162 |
+
mod_freq = self.config["mod_freq"]
|
| 163 |
+
mod_depth = self.config["mod_depth"]
|
| 164 |
+
pulse_width = self.config["pulse_width"]
|
| 165 |
+
pulse = 0.5 * (signal.square(2 * np.pi * mod_freq * t, duty=pulse_width) + 1) # 0 to 1
|
| 166 |
+
mod_env = 1.0 - mod_depth + mod_depth * pulse # Modulates between (1-depth) and 1
|
| 167 |
+
return carrier * mod_env
|
| 168 |
+
|
| 169 |
+
def _apply_chirp_modulation(self, t, carrier):
|
| 170 |
+
"""Apply frequency chirp modulation (applied differently)"""
|
| 171 |
+
# Chirp modulation modifies frequency directly, not amplitude envelope
|
| 172 |
+
# This implementation is more complex and might replace the base wave generation
|
| 173 |
+
# For simplicity, let's keep amplitude modulation for 'chirp' but with a varying mod freq
|
| 174 |
+
|
| 175 |
+
# Simple approach: vary the *modulation frequency* over time (like a siren)
|
| 176 |
+
start_mod_freq = max(0.1, self.config["mod_freq"] / 2) # Avoid 0 Hz
|
| 177 |
+
end_mod_freq = self.config["mod_freq"] * 2
|
| 178 |
+
instantaneous_mod_freq = np.linspace(start_mod_freq, end_mod_freq, len(t))
|
| 179 |
+
|
| 180 |
+
mod_depth = self.config["mod_depth"]
|
| 181 |
+
# Integrate frequency to get phase: 2 * pi * integral(f(t) dt)
|
| 182 |
+
phase = 2 * np.pi * np.cumsum(instantaneous_mod_freq) / self.sample_rate
|
| 183 |
+
mod_env = 1.0 + mod_depth * np.sin(phase + self.config["phase_shift"])
|
| 184 |
+
return carrier * mod_env
|
| 185 |
+
|
| 186 |
+
def generate_modulated_wave(self, duration):
|
| 187 |
+
"""
|
| 188 |
+
Generate modulated audio wave based on current settings.
|
| 189 |
+
Applies harmonics to the base wave first, then applies modulation.
|
| 190 |
+
"""
|
| 191 |
+
t, base_carrier = self._generate_base_wave(duration) # Base carrier now includes harmonics
|
| 192 |
+
|
| 193 |
+
# Apply the selected amplitude modulation type
|
| 194 |
+
if self.modulation_type == "sine":
|
| 195 |
+
modulated = self._apply_sine_modulation(t, base_carrier)
|
| 196 |
+
elif self.modulation_type == "pulse":
|
| 197 |
+
modulated = self._apply_pulse_modulation(t, base_carrier)
|
| 198 |
+
elif self.modulation_type == "chirp":
|
| 199 |
+
# Using the amplitude modulation with varying frequency approach
|
| 200 |
+
modulated = self._apply_chirp_modulation(t, base_carrier)
|
| 201 |
+
else:
|
| 202 |
+
modulated = base_carrier # Default to unmodulated rich carrier
|
| 203 |
+
|
| 204 |
+
# Final normalization to prevent clipping
|
| 205 |
+
max_amp = np.max(np.abs(modulated))
|
| 206 |
+
if max_amp > 0:
|
| 207 |
+
normalized = 0.9 * modulated / max_amp # Use 0.9 to leave headroom
|
| 208 |
+
else:
|
| 209 |
+
normalized = modulated # Avoid division by zero if signal is silent
|
| 210 |
+
|
| 211 |
+
return normalized
|
| 212 |
+
|
| 213 |
+
def save_audio(self, duration, file_path=None):
|
| 214 |
+
"""Generate and save audio to a file"""
|
| 215 |
+
audio = self.generate_modulated_wave(duration)
|
| 216 |
+
file_path = file_path or f"rhythma_{self.base_freq}Hz_{self.rhythm_pattern}.wav"
|
| 217 |
+
try:
|
| 218 |
+
sf.write(file_path, audio, self.sample_rate)
|
| 219 |
+
print(f"Audio saved to: {file_path}")
|
| 220 |
+
return file_path
|
| 221 |
+
except Exception as e:
|
| 222 |
+
print(f"Error saving audio file: {e}")
|
| 223 |
+
traceback.print_exc()
|
| 224 |
+
return None # Return None if saving fails
|
| 225 |
+
|
| 226 |
+
|
| 227 |
+
def visualize_waveform(self, duration):
|
| 228 |
+
"""Generate visualization of the modulated waveform"""
|
| 229 |
+
# Generate a shorter segment for visualization consistency
|
| 230 |
+
vis_duration = min(duration, 0.5) # Shorter duration for clearer plot
|
| 231 |
+
plot_samples = int(self.sample_rate * vis_duration)
|
| 232 |
+
|
| 233 |
+
t = np.linspace(0, vis_duration, plot_samples, endpoint=False)
|
| 234 |
+
modulated = self.generate_modulated_wave(vis_duration) # Generate specific duration
|
| 235 |
+
|
| 236 |
+
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(10, 6), gridspec_kw={'height_ratios': [1, 1]})
|
| 237 |
+
|
| 238 |
+
# Plot time domain (zoom in on a small section for detail)
|
| 239 |
+
zoom_samples = min(plot_samples, 2000) # Show max ~45ms
|
| 240 |
+
ax1.plot(t[:zoom_samples], modulated[:zoom_samples])
|
| 241 |
+
title = f'Rhythma Waveform: {self.rhythm_pattern.capitalize()} ({self.modulation_type.capitalize()})'
|
| 242 |
+
if self.emotional_state:
|
| 243 |
+
title += f' - {self.emotional_state.capitalize()} ({self.base_freq} Hz)'
|
| 244 |
+
else:
|
| 245 |
+
title += f' - {self.base_freq} Hz'
|
| 246 |
+
ax1.set_title(title)
|
| 247 |
+
ax1.set_xlabel('Time (s)')
|
| 248 |
+
ax1.set_ylabel('Amplitude')
|
| 249 |
+
ax1.grid(True)
|
| 250 |
+
|
| 251 |
+
# Plot frequency domain (spectrogram)
|
| 252 |
+
try:
|
| 253 |
+
# Use the full generated segment for spectrogram if possible
|
| 254 |
+
full_wave = self.generate_modulated_wave(duration)
|
| 255 |
+
f, t_spec, Sxx = signal.spectrogram(full_wave, self.sample_rate, nperseg=1024)
|
| 256 |
+
# Limit frequency display range for clarity (e.g., up to 2kHz)
|
| 257 |
+
freq_limit_idx = np.where(f >= 2000)[0]
|
| 258 |
+
if len(freq_limit_idx) > 0:
|
| 259 |
+
f = f[:freq_limit_idx[0]]
|
| 260 |
+
Sxx = Sxx[:freq_limit_idx[0], :]
|
| 261 |
+
else: # Handle cases where max freq is below limit
|
| 262 |
+
pass
|
| 263 |
+
# Use logarithmic scale for power if needed
|
| 264 |
+
pcm = ax2.pcolormesh(t_spec, f, 10 * np.log10(Sxx + 1e-9), shading='gouraud', cmap='viridis') # Log scale power
|
| 265 |
+
fig.colorbar(pcm, ax=ax2, label='Power (dB)') # Add colorbar
|
| 266 |
+
ax2.set_ylabel('Frequency (Hz)')
|
| 267 |
+
ax2.set_xlabel('Time (s)')
|
| 268 |
+
ax2.set_title('Spectrogram')
|
| 269 |
+
|
| 270 |
+
except Exception as e:
|
| 271 |
+
print(f"Error generating spectrogram: {e}")
|
| 272 |
+
ax2.set_title('Spectrogram (Error)')
|
| 273 |
+
ax2.text(0.5, 0.5, 'Could not generate spectrogram', horizontalalignment='center', verticalalignment='center', transform=ax2.transAxes)
|
| 274 |
+
|
| 275 |
+
|
| 276 |
+
plt.tight_layout(rect=[0, 0.05, 1, 1]) # Adjust layout to prevent overlap, leave space at bottom
|
| 277 |
+
|
| 278 |
+
# Add symbolic interpretation below plots
|
| 279 |
+
fig_text = self.get_symbolic_interpretation()
|
| 280 |
+
emotion_info = self.emotional_info.get(self.emotional_state, {})
|
| 281 |
+
if emotion_info:
|
| 282 |
+
fig_text += f"\n{self.base_freq} Hz - {emotion_info.get('name', '')}: {emotion_info.get('advice', '')}"
|
| 283 |
+
elif not self.emotional_state: # Case where only base_freq was set
|
| 284 |
+
fig_text += f"\nBase Frequency: {self.base_freq} Hz"
|
| 285 |
+
|
| 286 |
+
|
| 287 |
+
fig.text(0.5, 0.01, fig_text, ha='center', va='bottom', fontsize=9, style='italic', wrap=True)
|
| 288 |
+
|
| 289 |
+
return fig
|
| 290 |
+
|
| 291 |
+
|
| 292 |
+
def get_waveform_image(self):
|
| 293 |
+
"""Generate a simple waveform image as a PIL Image"""
|
| 294 |
+
# Generate a short, clear representation of the *base* frequency wave
|
| 295 |
+
duration = 0.05 # Very short duration for visualization
|
| 296 |
+
t = np.linspace(0, duration, int(self.sample_rate * duration), False)
|
| 297 |
+
# Use the base frequency determined in __init__
|
| 298 |
+
tone = np.sin(2 * np.pi * self.base_freq * t)
|
| 299 |
+
|
| 300 |
+
plt.figure(figsize=(6, 2)) # Smaller figure for simple image
|
| 301 |
+
plt.plot(t, tone)
|
| 302 |
+
# plt.title(f"Base Tone: {self.base_freq} Hz") # Title might clutter small image
|
| 303 |
+
plt.xlabel("Time (s)")
|
| 304 |
+
plt.ylabel("Amplitude")
|
| 305 |
+
plt.ylim(-1.1, 1.1)
|
| 306 |
+
plt.grid(True)
|
| 307 |
+
plt.tight_layout()
|
| 308 |
+
|
| 309 |
+
buf = io.BytesIO()
|
| 310 |
+
plt.savefig(buf, format='png', bbox_inches='tight')
|
| 311 |
+
buf.seek(0)
|
| 312 |
+
plt.close() # Close the plot to free memory
|
| 313 |
+
|
| 314 |
+
return Image.open(buf)
|
| 315 |
+
|
| 316 |
+
|
| 317 |
+
def get_symbolic_interpretation(self):
|
| 318 |
+
"""Return the symbolic interpretation of the current rhythm pattern"""
|
| 319 |
+
return self.symbolic_mapping.get(self.rhythm_pattern, "Pattern Interpretation: Default")
|
| 320 |
+
|
| 321 |
+
def get_emotional_advice(self):
|
| 322 |
+
"""Get advice based on emotional state if available"""
|
| 323 |
+
if not self.emotional_state:
|
| 324 |
+
return "No specific emotional state identified."
|
| 325 |
+
|
| 326 |
+
emotion_info = self.emotional_info.get(self.emotional_state, {})
|
| 327 |
+
return emotion_info.get('advice', "General well-being advice applies.")
|
| 328 |
+
|
| 329 |
+
|
| 330 |
+
def get_complete_analysis(self):
|
| 331 |
+
"""Get a complete analysis including emotional and rhythmic information"""
|
| 332 |
+
analysis = []
|
| 333 |
+
|
| 334 |
+
if self.emotional_state:
|
| 335 |
+
emotion_info = self.emotional_info.get(self.emotional_state, {})
|
| 336 |
+
analysis.append(f"Detected State/Intention: {self.emotional_state.capitalize()}")
|
| 337 |
+
analysis.append(f"Resonant Frequency: {self.base_freq} Hz - {emotion_info.get('name', 'Frequency Information')}")
|
| 338 |
+
analysis.append(f"Guidance: {emotion_info.get('advice', 'Focus on the sound.')}")
|
| 339 |
+
else:
|
| 340 |
+
# This case happens if only override_freq was used and it didn't map closely to a state
|
| 341 |
+
analysis.append(f"Using Manual Frequency: {self.base_freq} Hz")
|
| 342 |
+
analysis.append("Guidance: Tune into the custom frequency.")
|
| 343 |
+
|
| 344 |
+
analysis.append(f"Rhythm Pattern: {self.rhythm_pattern.capitalize()}")
|
| 345 |
+
analysis.append(f"Symbolic Interpretation: {self.get_symbolic_interpretation()}")
|
| 346 |
+
analysis.append(f"Modulation Type: {self.modulation_type.capitalize()}")
|
| 347 |
+
|
| 348 |
+
return "\n\n".join(analysis)
|
| 349 |
+
|
| 350 |
+
|
| 351 |
+
class RhythmaSymphAICore:
|
| 352 |
+
"""
|
| 353 |
+
SymphAI Core - Interprets input to determine emotional state and rhythm pattern.
|
| 354 |
+
Handles text and audio input, utilizing Groq LLM and Sentence Transformers if available.
|
| 355 |
+
"""
|
| 356 |
+
|
| 357 |
+
def __init__(self, use_groq=True):
|
| 358 |
+
"""Initialize the SymphAI Core"""
|
| 359 |
+
# Expanded emotional states / intentions
|
| 360 |
+
self.emotional_states = [
|
| 361 |
+
"anxious", "stressed", "calm", "sad",
|
| 362 |
+
"angry", "fearful", "confused", "happy",
|
| 363 |
+
"neutral", "focused", "relaxed", "active", # Added intentions
|
| 364 |
+
]
|
| 365 |
+
|
| 366 |
+
# Default rhythm patterns
|
| 367 |
+
self.rhythm_patterns = list(RhythmaModulationEngine().rhythm_configs.keys()) # Get from engine
|
| 368 |
+
|
| 369 |
+
# Initialize Groq client if available and requested
|
| 370 |
+
self.groq_client = None
|
| 371 |
+
self.use_groq = use_groq and GROQ_AVAILABLE
|
| 372 |
+
if self.use_groq:
|
| 373 |
+
api_key = os.environ.get("GROQ_API_KEY")
|
| 374 |
+
if api_key:
|
| 375 |
+
try:
|
| 376 |
+
self.groq_client = Groq(api_key=api_key)
|
| 377 |
+
print("✅ Groq client initialized successfully.")
|
| 378 |
+
except Exception as e:
|
| 379 |
+
print(f"⚠️ Failed to initialize Groq client: {str(e)}")
|
| 380 |
+
self.use_groq = False # Disable Groq if init fails
|
| 381 |
+
else:
|
| 382 |
+
print("⚠️ GROQ_API_KEY environment variable not found. Groq features disabled.")
|
| 383 |
+
self.use_groq = False
|
| 384 |
+
|
| 385 |
+
# Initialize sentence transformer for semantic matching if available
|
| 386 |
+
self.embedding_model = None
|
| 387 |
+
self.emotional_embeddings = {}
|
| 388 |
+
self.rhythm_embeddings = {}
|
| 389 |
+
if SENTENCE_TRANSFORMER_AVAILABLE:
|
| 390 |
+
try:
|
| 391 |
+
# Using a common, effective model
|
| 392 |
+
self.embedding_model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 393 |
+
# Pre-compute embeddings for faster lookup
|
| 394 |
+
self.emotional_embeddings = {
|
| 395 |
+
state: self.embedding_model.encode([state])[0] # Get the 1D array
|
| 396 |
+
for state in self.emotional_states
|
| 397 |
+
}
|
| 398 |
+
self.rhythm_embeddings = {
|
| 399 |
+
pattern: self.embedding_model.encode([pattern])[0] # Get the 1D array
|
| 400 |
+
for pattern in self.rhythm_patterns
|
| 401 |
+
}
|
| 402 |
+
print("✅ SentenceTransformer initialized successfully.")
|
| 403 |
+
except Exception as e:
|
| 404 |
+
print(f"⚠️ Failed to initialize SentenceTransformer: {str(e)}. Using basic text matching.")
|
| 405 |
+
self.embedding_model = None # Ensure it's None if init fails
|
| 406 |
+
else:
|
| 407 |
+
print("ℹ️ SentenceTransformer not installed. Using basic text matching.")
|
| 408 |
+
|
| 409 |
+
|
| 410 |
+
def detect_emotion_with_groq(self, input_text):
|
| 411 |
+
|