Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,1164 +1,611 @@
|
|
| 1 |
-
#!/usr/bin/env python3
|
| 2 |
-
"""
|
| 3 |
-
Live Audio Singing Helper - Production Grade
|
| 4 |
-
Advanced audio processing tool for singers and musicians
|
| 5 |
-
Author: Lead Developer
|
| 6 |
-
Version: 2.0.0
|
| 7 |
-
"""
|
| 8 |
-
|
| 9 |
import gradio as gr
|
| 10 |
import librosa
|
| 11 |
import numpy as np
|
| 12 |
import soundfile as sf
|
| 13 |
-
from spleeter.separator import Separator
|
| 14 |
import os
|
| 15 |
-
import sys
|
| 16 |
-
import shutil
|
| 17 |
import tempfile
|
| 18 |
-
import
|
| 19 |
-
import matplotlib.pyplot as plt
|
| 20 |
-
import traceback
|
| 21 |
-
import logging
|
| 22 |
-
import gc
|
| 23 |
from pathlib import Path
|
| 24 |
-
from typing import Tuple, Optional, Dict, Any, List, Union
|
| 25 |
-
from dataclasses import dataclass
|
| 26 |
-
from contextlib import contextmanager
|
| 27 |
import warnings
|
| 28 |
warnings.filterwarnings("ignore")
|
| 29 |
|
| 30 |
-
#
|
| 31 |
-
|
| 32 |
-
from
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
"""Structured result container for audio processing operations"""
|
| 48 |
-
success: bool
|
| 49 |
-
message: str
|
| 50 |
-
data: Optional[Dict[str, Any]] = None
|
| 51 |
-
error: Optional[str] = None
|
| 52 |
-
|
| 53 |
-
class AudioProcessorPro:
|
| 54 |
-
"""Professional-grade audio processor with comprehensive error handling and optimization"""
|
| 55 |
|
| 56 |
def __init__(self):
|
| 57 |
-
self.
|
| 58 |
-
self.
|
| 59 |
-
self.temp_dir = None
|
| 60 |
-
self.session_id = None
|
| 61 |
-
self._initialize_session()
|
| 62 |
-
|
| 63 |
-
def _initialize_session(self):
|
| 64 |
-
"""Initialize processing session with proper cleanup"""
|
| 65 |
-
try:
|
| 66 |
-
self.session_id = f"{TEMP_DIR_PREFIX}{np.random.randint(100000)}"
|
| 67 |
-
self.temp_dir = tempfile.mkdtemp(prefix=self.session_id)
|
| 68 |
-
logger.info(f"Session initialized: {self.session_id}")
|
| 69 |
-
except Exception as e:
|
| 70 |
-
logger.error(f"Session initialization failed: {e}")
|
| 71 |
-
raise
|
| 72 |
-
|
| 73 |
-
@contextmanager
|
| 74 |
-
def _safe_processing(self, operation_name: str):
|
| 75 |
-
"""Context manager for safe processing with automatic cleanup"""
|
| 76 |
-
logger.info(f"Starting {operation_name}")
|
| 77 |
-
try:
|
| 78 |
-
yield
|
| 79 |
-
logger.info(f"Completed {operation_name}")
|
| 80 |
-
except Exception as e:
|
| 81 |
-
logger.error(f"Error in {operation_name}: {e}")
|
| 82 |
-
raise
|
| 83 |
-
finally:
|
| 84 |
-
gc.collect() # Force garbage collection
|
| 85 |
|
| 86 |
-
def
|
| 87 |
-
"""
|
| 88 |
try:
|
| 89 |
-
|
| 90 |
-
|
| 91 |
|
| 92 |
-
#
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
return ProcessingResult(False, f"File too large. Max size: {MAX_FILE_SIZE//1024//1024}MB")
|
| 96 |
|
| 97 |
-
#
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
-
# Check audio properties
|
| 103 |
-
try:
|
| 104 |
-
y, sr = librosa.load(audio_path, duration=1.0) # Load first second for validation
|
| 105 |
-
duration = librosa.get_duration(filename=audio_path)
|
| 106 |
-
|
| 107 |
-
if duration > MAX_DURATION:
|
| 108 |
-
return ProcessingResult(False, f"Audio too long. Max duration: {MAX_DURATION//60} minutes")
|
| 109 |
-
|
| 110 |
-
if sr < 8000:
|
| 111 |
-
return ProcessingResult(False, "Sample rate too low (minimum 8kHz)")
|
| 112 |
-
|
| 113 |
-
return ProcessingResult(True, "File validation passed", {
|
| 114 |
-
'duration': duration,
|
| 115 |
-
'sample_rate': sr,
|
| 116 |
-
'file_size': file_size
|
| 117 |
-
})
|
| 118 |
-
|
| 119 |
-
except Exception as e:
|
| 120 |
-
return ProcessingResult(False, f"Invalid audio file: {str(e)}")
|
| 121 |
-
|
| 122 |
except Exception as e:
|
| 123 |
-
return
|
| 124 |
|
| 125 |
-
def
|
| 126 |
-
"""
|
|
|
|
|
|
|
|
|
|
| 127 |
try:
|
| 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 |
-
duration = librosa.get_duration(y=y, sr=sr)
|
| 154 |
-
|
| 155 |
-
# Core features
|
| 156 |
-
tempo, beats = librosa.beat.beat_track(y=y, sr=sr)
|
| 157 |
-
|
| 158 |
-
# Spectral features
|
| 159 |
-
spectral_centroids = librosa.feature.spectral_centroid(y=y, sr=sr)[0]
|
| 160 |
-
spectral_rolloff = librosa.feature.spectral_rolloff(y=y, sr=sr)[0]
|
| 161 |
-
spectral_bandwidth = librosa.feature.spectral_bandwidth(y=y, sr=sr)[0]
|
| 162 |
-
zero_crossing_rate = librosa.feature.zero_crossing_rate(y)[0]
|
| 163 |
-
|
| 164 |
-
# Timbral features
|
| 165 |
-
mfccs = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=13)
|
| 166 |
-
chroma = librosa.feature.chroma_stft(y=y, sr=sr)
|
| 167 |
-
|
| 168 |
-
# Dynamic features
|
| 169 |
-
rms = librosa.feature.rms(y=y)[0]
|
| 170 |
-
|
| 171 |
-
# Pitch estimation
|
| 172 |
-
pitches, magnitudes = librosa.piptrack(y=y, sr=sr)
|
| 173 |
-
pitch_values = []
|
| 174 |
-
for t in range(pitches.shape[1]):
|
| 175 |
-
index = magnitudes[:, t].argmax()
|
| 176 |
-
pitch = pitches[index, t]
|
| 177 |
-
if pitch > 0:
|
| 178 |
-
pitch_values.append(pitch)
|
| 179 |
-
|
| 180 |
-
features = {
|
| 181 |
-
# Basic properties
|
| 182 |
-
'duration': round(duration, 2),
|
| 183 |
-
'sample_rate': sr,
|
| 184 |
-
'file_size': validation.data['file_size'],
|
| 185 |
-
|
| 186 |
-
# Rhythm and tempo
|
| 187 |
-
'tempo': round(tempo, 1),
|
| 188 |
-
'num_beats': len(beats),
|
| 189 |
-
'rhythm_regularity': round(np.std(np.diff(beats)), 3),
|
| 190 |
-
|
| 191 |
-
# Spectral characteristics
|
| 192 |
-
'spectral_centroid_mean': round(np.mean(spectral_centroids), 2),
|
| 193 |
-
'spectral_centroid_std': round(np.std(spectral_centroids), 2),
|
| 194 |
-
'spectral_rolloff_mean': round(np.mean(spectral_rolloff), 2),
|
| 195 |
-
'spectral_bandwidth_mean': round(np.mean(spectral_bandwidth), 2),
|
| 196 |
-
'zero_crossing_rate_mean': round(np.mean(zero_crossing_rate), 4),
|
| 197 |
-
|
| 198 |
-
# Dynamic properties
|
| 199 |
-
'rms_energy_mean': round(np.mean(rms), 4),
|
| 200 |
-
'rms_energy_std': round(np.std(rms), 4),
|
| 201 |
-
'dynamic_range': round(np.max(rms) - np.min(rms), 4),
|
| 202 |
-
|
| 203 |
-
# Pitch information
|
| 204 |
-
'pitch_mean': round(np.mean(pitch_values), 2) if pitch_values else 0,
|
| 205 |
-
'pitch_std': round(np.std(pitch_values), 2) if pitch_values else 0,
|
| 206 |
-
'pitch_range': round(max(pitch_values) - min(pitch_values), 2) if len(pitch_values) > 1 else 0,
|
| 207 |
-
|
| 208 |
-
# Timbral features (for advanced analysis)
|
| 209 |
-
'mfcc_mean': np.round(np.mean(mfccs, axis=1), 3).tolist(),
|
| 210 |
-
'chroma_mean': np.round(np.mean(chroma, axis=1), 3).tolist(),
|
| 211 |
-
|
| 212 |
-
# Quality metrics
|
| 213 |
-
'signal_to_noise_ratio': round(20 * np.log10(np.mean(rms) / (np.std(rms) + 1e-10)), 2)
|
| 214 |
}
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
if progress_callback:
|
| 232 |
-
progress_callback(0.1, "Initializing separator...")
|
| 233 |
-
|
| 234 |
-
separator = self.get_separator(stems)
|
| 235 |
-
|
| 236 |
-
if progress_callback:
|
| 237 |
-
progress_callback(0.3, "Loading audio...")
|
| 238 |
-
|
| 239 |
-
# Create unique output directory
|
| 240 |
-
output_dir = os.path.join(self.temp_dir, f"separation_{np.random.randint(10000)}")
|
| 241 |
-
os.makedirs(output_dir, exist_ok=True)
|
| 242 |
-
|
| 243 |
-
if progress_callback:
|
| 244 |
-
progress_callback(0.5, "Separating audio sources...")
|
| 245 |
-
|
| 246 |
-
# Perform separation
|
| 247 |
-
separator.separate_to_file(audio_path, output_dir)
|
| 248 |
-
|
| 249 |
-
if progress_callback:
|
| 250 |
-
progress_callback(0.8, "Processing results...")
|
| 251 |
-
|
| 252 |
-
# Get separated files
|
| 253 |
-
base_name = os.path.splitext(os.path.basename(audio_path))[0]
|
| 254 |
-
result_dir = os.path.join(output_dir, base_name)
|
| 255 |
-
|
| 256 |
-
separated_files = {}
|
| 257 |
-
if stems == 2:
|
| 258 |
-
separated_files = {
|
| 259 |
-
'vocals': os.path.join(result_dir, "vocals.wav"),
|
| 260 |
-
'accompaniment': os.path.join(result_dir, "accompaniment.wav")
|
| 261 |
-
}
|
| 262 |
-
else: # 4 stems
|
| 263 |
-
separated_files = {
|
| 264 |
-
'vocals': os.path.join(result_dir, "vocals.wav"),
|
| 265 |
-
'drums': os.path.join(result_dir, "drums.wav"),
|
| 266 |
-
'bass': os.path.join(result_dir, "bass.wav"),
|
| 267 |
-
'other': os.path.join(result_dir, "other.wav")
|
| 268 |
-
}
|
| 269 |
-
|
| 270 |
-
# Verify all files exist
|
| 271 |
-
missing_files = [k for k, v in separated_files.items() if not os.path.exists(v)]
|
| 272 |
-
if missing_files:
|
| 273 |
-
return ProcessingResult(False, f"Separation incomplete. Missing: {missing_files}")
|
| 274 |
-
|
| 275 |
-
if progress_callback:
|
| 276 |
-
progress_callback(1.0, "Separation complete!")
|
| 277 |
-
|
| 278 |
-
return ProcessingResult(True, f"β
{stems}-stem separation successful!", separated_files)
|
| 279 |
-
|
| 280 |
-
except Exception as e:
|
| 281 |
-
logger.error(f"Audio separation failed: {e}")
|
| 282 |
-
return ProcessingResult(False, f"Separation failed: {str(e)}")
|
| 283 |
-
|
| 284 |
-
def apply_vocal_effects(self, audio_path: str, effects_config: Dict[str, float]) -> ProcessingResult:
|
| 285 |
-
"""Apply vocal effects with comprehensive options"""
|
| 286 |
-
with self._safe_processing("vocal_effects"):
|
| 287 |
-
try:
|
| 288 |
-
validation = self.validate_audio_file(audio_path)
|
| 289 |
-
if not validation.success:
|
| 290 |
-
return validation
|
| 291 |
-
|
| 292 |
-
y, sr = librosa.load(audio_path, sr=None)
|
| 293 |
-
original_y = y.copy()
|
| 294 |
-
|
| 295 |
-
# Apply pitch shifting
|
| 296 |
-
pitch_shift = effects_config.get('pitch_shift', 0)
|
| 297 |
-
if pitch_shift != 0:
|
| 298 |
-
y = librosa.effects.pitch_shift(y, sr=sr, n_steps=pitch_shift)
|
| 299 |
-
|
| 300 |
-
# Apply reverb
|
| 301 |
-
reverb_amount = effects_config.get('reverb', 0)
|
| 302 |
-
if reverb_amount > 0:
|
| 303 |
-
reverb_length = int(0.5 * sr)
|
| 304 |
-
impulse = np.random.randn(reverb_length) * np.exp(-np.arange(reverb_length) / (sr * 0.1))
|
| 305 |
-
impulse *= reverb_amount
|
| 306 |
-
y = scipy.signal.convolve(y, impulse, mode='same')
|
| 307 |
-
|
| 308 |
-
# Apply chorus effect
|
| 309 |
-
chorus_amount = effects_config.get('chorus', 0)
|
| 310 |
-
if chorus_amount > 0:
|
| 311 |
-
delay_samples = int(0.02 * sr) # 20ms delay
|
| 312 |
-
delayed = np.pad(original_y, (delay_samples, 0), mode='constant')[:len(y)]
|
| 313 |
-
y = y + chorus_amount * delayed
|
| 314 |
-
|
| 315 |
-
# Apply compression
|
| 316 |
-
compression = effects_config.get('compression', 0)
|
| 317 |
-
if compression > 0:
|
| 318 |
-
threshold = 0.1
|
| 319 |
-
ratio = 1 + compression * 9 # 1:1 to 10:1 ratio
|
| 320 |
-
mask = np.abs(y) > threshold
|
| 321 |
-
y[mask] = np.sign(y[mask]) * (threshold + (np.abs(y[mask]) - threshold) / ratio)
|
| 322 |
-
|
| 323 |
-
# Normalize to prevent clipping
|
| 324 |
-
if np.max(np.abs(y)) > 0:
|
| 325 |
-
y = y / np.max(np.abs(y)) * 0.95
|
| 326 |
-
|
| 327 |
-
# Save processed audio
|
| 328 |
-
output_path = os.path.join(self.temp_dir, f"processed_{np.random.randint(10000)}.wav")
|
| 329 |
-
sf.write(output_path, y, sr)
|
| 330 |
-
|
| 331 |
-
effects_applied = [k for k, v in effects_config.items() if v != 0]
|
| 332 |
-
|
| 333 |
-
return ProcessingResult(True, f"Effects applied: {', '.join(effects_applied)}", {
|
| 334 |
-
'output_path': output_path,
|
| 335 |
-
'effects_applied': effects_applied
|
| 336 |
-
})
|
| 337 |
-
|
| 338 |
-
except Exception as e:
|
| 339 |
-
logger.error(f"Effects processing failed: {e}")
|
| 340 |
-
return ProcessingResult(False, f"Effects processing failed: {str(e)}")
|
| 341 |
-
|
| 342 |
-
def cleanup_session(self):
|
| 343 |
-
"""Clean up temporary files and release resources"""
|
| 344 |
-
try:
|
| 345 |
-
if self.temp_dir and os.path.exists(self.temp_dir):
|
| 346 |
-
shutil.rmtree(self.temp_dir)
|
| 347 |
-
logger.info(f"Cleaned up session: {self.session_id}")
|
| 348 |
except Exception as e:
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
class StyleCoachingEngine:
|
| 352 |
-
"""Advanced vocal style coaching system"""
|
| 353 |
|
| 354 |
-
def
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
def extract_vocal_features(self, audio_path: str) -> ProcessingResult:
|
| 358 |
-
"""Extract detailed vocal-specific features"""
|
| 359 |
try:
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
else:
|
| 380 |
-
vibrato_extent = 0
|
| 381 |
-
|
| 382 |
-
# Vocal effort estimation
|
| 383 |
-
spectral_centroid = librosa.feature.spectral_centroid(y=y, sr=sr)
|
| 384 |
-
vocal_effort = np.mean(spectral_centroid) / 1000 # Normalized measure
|
| 385 |
-
|
| 386 |
-
features = {
|
| 387 |
-
'fundamental_frequency_mean': np.nanmean(f0_clean) if len(f0_clean) > 0 else 0,
|
| 388 |
-
'fundamental_frequency_std': np.nanstd(f0_clean) if len(f0_clean) > 0 else 0,
|
| 389 |
-
'pitch_range': np.nanmax(f0_clean) - np.nanmin(f0_clean) if len(f0_clean) > 0 else 0,
|
| 390 |
-
'vibrato_extent': vibrato_extent,
|
| 391 |
-
'vocal_effort': vocal_effort,
|
| 392 |
-
'voiced_percentage': np.mean(voiced_flag) * 100,
|
| 393 |
-
'mfccs': mfccs,
|
| 394 |
-
'duration': librosa.get_duration(y=y, sr=sr)
|
| 395 |
-
}
|
| 396 |
-
|
| 397 |
-
return ProcessingResult(True, "Vocal features extracted", features)
|
| 398 |
-
|
| 399 |
except Exception as e:
|
| 400 |
-
|
| 401 |
-
return ProcessingResult(False, f"Vocal feature extraction failed: {str(e)}")
|
| 402 |
|
| 403 |
-
def
|
| 404 |
-
"""
|
| 405 |
try:
|
| 406 |
-
|
| 407 |
-
return ProcessingResult(False, "Need at least 2 reference tracks")
|
| 408 |
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 412 |
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
for key in valid_features[0].keys():
|
| 416 |
-
if key != 'mfccs': # Handle MFCCs separately
|
| 417 |
-
values = [f[key] for f in valid_features if key in f and f[key] is not None]
|
| 418 |
-
if values:
|
| 419 |
-
profile[key] = np.mean(values)
|
| 420 |
-
profile[f'{key}_std'] = np.std(values)
|
| 421 |
|
| 422 |
-
#
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
|
| 427 |
-
|
|
|
|
| 428 |
|
| 429 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 430 |
|
| 431 |
except Exception as e:
|
| 432 |
-
|
| 433 |
-
return ProcessingResult(False, f"Style profile creation failed: {str(e)}")
|
| 434 |
|
| 435 |
-
def
|
| 436 |
-
"""Compare user
|
|
|
|
|
|
|
|
|
|
| 437 |
try:
|
| 438 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 439 |
|
| 440 |
-
#
|
| 441 |
-
pitch_diff = abs(user_features
|
| 442 |
-
|
| 443 |
-
|
|
|
|
| 444 |
|
| 445 |
-
#
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 450 |
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
comparison['vibrato_match'] = max(0, 100 - vibrato_diff * 50)
|
| 456 |
|
| 457 |
-
|
| 458 |
-
comparison['overall_similarity'] = np.mean([
|
| 459 |
-
comparison['pitch_accuracy'],
|
| 460 |
-
comparison['range_match'],
|
| 461 |
-
comparison['vibrato_match']
|
| 462 |
-
])
|
| 463 |
|
| 464 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 465 |
|
| 466 |
except Exception as e:
|
| 467 |
-
|
| 468 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 469 |
|
| 470 |
-
# Global
|
| 471 |
-
|
| 472 |
-
style_coach = StyleCoachingEngine(processor)
|
| 473 |
|
| 474 |
-
def
|
| 475 |
"""Format analysis results for display"""
|
| 476 |
-
if not
|
| 477 |
-
return "
|
| 478 |
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
π΅ **Basic Properties**
|
| 482 |
-
β’ Duration: {features.get('duration', 'N/A')} seconds
|
| 483 |
-
β’ Sample Rate: {features.get('sample_rate', 'N/A')} Hz
|
| 484 |
-
β’ File Size: {features.get('file_size', 0) / 1024:.1f} KB
|
| 485 |
|
| 486 |
-
|
| 487 |
-
β’
|
| 488 |
-
β’
|
| 489 |
-
β’
|
| 490 |
|
| 491 |
-
π
|
| 492 |
-
β’
|
| 493 |
-
β’ Spectral
|
| 494 |
-
β’ Zero Crossing Rate: {
|
|
|
|
| 495 |
|
| 496 |
-
|
| 497 |
-
β’ Average
|
| 498 |
-
β’
|
| 499 |
-
β’
|
| 500 |
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
β’ Pitch Variation: {features.get('pitch_std', 'N/A')} Hz
|
| 504 |
-
β’ Pitch Range: {features.get('pitch_range', 'N/A')} Hz"""
|
| 505 |
-
|
| 506 |
-
return text
|
| 507 |
-
|
| 508 |
-
def process_audio_separation(audio_file, stems_mode, pitch_shift, reverb, chorus, compression):
|
| 509 |
-
"""Main audio separation processing function"""
|
| 510 |
if not audio_file:
|
| 511 |
-
return
|
| 512 |
-
|
| 513 |
-
|
| 514 |
-
|
| 515 |
-
)
|
| 516 |
|
| 517 |
try:
|
| 518 |
-
#
|
| 519 |
-
|
| 520 |
-
|
| 521 |
-
def progress_callback(progress, message):
|
| 522 |
-
progress_updates.append(f"[{progress*100:.0f}%] {message}")
|
| 523 |
-
|
| 524 |
-
# Analyze features first
|
| 525 |
-
feature_result = processor.extract_comprehensive_features(audio_file)
|
| 526 |
-
if not feature_result.success:
|
| 527 |
-
return (
|
| 528 |
-
f"β {feature_result.message}",
|
| 529 |
-
None, None, None, None,
|
| 530 |
-
feature_result.error or "Analysis failed"
|
| 531 |
-
)
|
| 532 |
-
|
| 533 |
-
analysis_text = format_analysis_text(feature_result.data)
|
| 534 |
|
| 535 |
# Separate audio
|
| 536 |
-
|
| 537 |
-
separation_result =
|
| 538 |
-
|
| 539 |
-
if not separation_result.success:
|
| 540 |
-
return (
|
| 541 |
-
f"β {separation_result.message}",
|
| 542 |
-
None, None, None, None,
|
| 543 |
-
analysis_text
|
| 544 |
-
)
|
| 545 |
-
|
| 546 |
-
separated_files = separation_result.data
|
| 547 |
|
| 548 |
-
|
| 549 |
-
|
| 550 |
-
'pitch_shift': pitch_shift,
|
| 551 |
-
'reverb': reverb,
|
| 552 |
-
'chorus': chorus,
|
| 553 |
-
'compression': compression
|
| 554 |
-
}
|
| 555 |
|
| 556 |
-
|
| 557 |
-
if vocals_path and any(v != 0 for v in effects_config.values()):
|
| 558 |
-
effects_result = processor.apply_vocal_effects(vocals_path, effects_config)
|
| 559 |
-
if effects_result.success:
|
| 560 |
-
vocals_path = effects_result.data['output_path']
|
| 561 |
-
separation_result.message += f" | {effects_result.message}"
|
| 562 |
-
|
| 563 |
-
# Prepare outputs based on stems
|
| 564 |
-
if stems == 2:
|
| 565 |
return (
|
| 566 |
-
|
| 567 |
-
|
| 568 |
-
|
| 569 |
-
None,
|
|
|
|
| 570 |
analysis_text
|
| 571 |
)
|
| 572 |
else:
|
| 573 |
return (
|
| 574 |
-
|
| 575 |
-
|
| 576 |
-
|
| 577 |
-
|
| 578 |
-
|
| 579 |
analysis_text
|
| 580 |
)
|
| 581 |
|
| 582 |
except Exception as e:
|
| 583 |
-
|
| 584 |
-
return (
|
| 585 |
-
f"β Processing failed: {str(e)}",
|
| 586 |
-
None, None, None, None,
|
| 587 |
-
"Analysis failed due to processing error"
|
| 588 |
-
)
|
| 589 |
|
| 590 |
-
def
|
| 591 |
-
"""
|
| 592 |
if not audio_file:
|
| 593 |
-
return
|
| 594 |
-
"β Please record audio first",
|
| 595 |
-
None,
|
| 596 |
-
"No analysis available"
|
| 597 |
-
)
|
| 598 |
|
| 599 |
try:
|
| 600 |
-
# Analyze
|
| 601 |
-
|
| 602 |
-
|
| 603 |
-
return (
|
| 604 |
-
f"β {feature_result.message}",
|
| 605 |
-
None,
|
| 606 |
-
feature_result.error or "Analysis failed"
|
| 607 |
-
)
|
| 608 |
-
|
| 609 |
-
analysis_text = format_analysis_text(feature_result.data)
|
| 610 |
|
| 611 |
# Apply effects
|
| 612 |
-
|
| 613 |
-
'pitch_shift': pitch_shift,
|
| 614 |
-
'reverb': reverb,
|
| 615 |
-
'chorus': chorus,
|
| 616 |
-
'compression': compression
|
| 617 |
-
}
|
| 618 |
|
| 619 |
-
|
| 620 |
-
|
| 621 |
-
return (
|
| 622 |
-
f"β {effects_result.message}",
|
| 623 |
-
None,
|
| 624 |
-
analysis_text
|
| 625 |
-
)
|
| 626 |
|
| 627 |
-
|
| 628 |
-
|
| 629 |
-
|
| 630 |
-
|
| 631 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 632 |
|
| 633 |
except Exception as e:
|
| 634 |
-
|
| 635 |
-
return (
|
| 636 |
-
f"β Processing failed: {str(e)}",
|
| 637 |
-
None,
|
| 638 |
-
"Analysis failed due to processing error"
|
| 639 |
-
)
|
| 640 |
|
| 641 |
def process_style_coaching(reference_files, user_audio):
|
| 642 |
-
"""
|
| 643 |
if not reference_files or len(reference_files) < 2:
|
| 644 |
-
return
|
| 645 |
-
"β Please upload at least 2 reference tracks",
|
| 646 |
-
"No references processed",
|
| 647 |
-
"Upload reference tracks to get personalized coaching feedback"
|
| 648 |
-
)
|
| 649 |
|
| 650 |
if not user_audio:
|
| 651 |
-
return
|
| 652 |
-
|
| 653 |
-
|
| 654 |
-
|
| 655 |
-
)
|
| 656 |
|
| 657 |
try:
|
| 658 |
# Process reference tracks
|
| 659 |
ref_features = []
|
| 660 |
ref_status = []
|
| 661 |
|
| 662 |
-
for i, ref_file in enumerate(reference_files[:5]):
|
| 663 |
-
|
| 664 |
-
|
| 665 |
-
|
| 666 |
-
|
| 667 |
-
|
| 668 |
-
|
| 669 |
-
|
| 670 |
-
|
| 671 |
-
if vocal_result.success:
|
| 672 |
-
ref_features.append(vocal_result.data)
|
| 673 |
-
ref_status.append(f"β
Reference {i+1}: Processed successfully")
|
| 674 |
-
else:
|
| 675 |
-
ref_status.append(f"β Reference {i+1}: Feature extraction failed")
|
| 676 |
-
else:
|
| 677 |
-
ref_status.append(f"β Reference {i+1}: Vocal separation failed")
|
| 678 |
else:
|
| 679 |
-
ref_status.append(f"β Reference {i+1}:
|
| 680 |
-
|
| 681 |
-
ref_status.append(f"β Reference {i+1}:
|
| 682 |
|
| 683 |
if len(ref_features) < 2:
|
| 684 |
-
return (
|
| 685 |
-
"β Failed to process enough reference tracks",
|
| 686 |
-
"\n".join(ref_status),
|
| 687 |
-
"Need at least 2 valid reference tracks for style analysis"
|
| 688 |
-
)
|
| 689 |
-
|
| 690 |
-
# Build style profile
|
| 691 |
-
profile_result = style_coach.build_style_profile(ref_features)
|
| 692 |
-
if not profile_result.success:
|
| 693 |
-
return (
|
| 694 |
-
f"β {profile_result.message}",
|
| 695 |
-
"\n".join(ref_status),
|
| 696 |
-
"Style profile creation failed"
|
| 697 |
-
)
|
| 698 |
|
| 699 |
# Process user audio
|
| 700 |
-
user_separation =
|
| 701 |
-
if not user_separation
|
| 702 |
-
return (
|
| 703 |
-
f"β Failed to process your audio: {user_separation.message}",
|
| 704 |
-
"\n".join(ref_status),
|
| 705 |
-
"Could not separate vocals from your performance"
|
| 706 |
-
)
|
| 707 |
|
| 708 |
-
|
| 709 |
-
if not
|
| 710 |
-
return (
|
| 711 |
-
"β Could not extract vocals from your performance",
|
| 712 |
-
"\n".join(ref_status),
|
| 713 |
-
"Vocal separation failed"
|
| 714 |
-
)
|
| 715 |
|
| 716 |
-
#
|
| 717 |
-
|
| 718 |
-
if not
|
| 719 |
-
return (
|
| 720 |
-
f"β {user_vocal_result.message}",
|
| 721 |
-
"\n".join(ref_status),
|
| 722 |
-
"Could not analyze your vocal characteristics"
|
| 723 |
-
)
|
| 724 |
-
|
| 725 |
-
# Compare performance
|
| 726 |
-
comparison_result = style_coach.compare_performance(
|
| 727 |
-
user_vocal_result.data,
|
| 728 |
-
profile_result.data
|
| 729 |
-
)
|
| 730 |
|
| 731 |
-
|
| 732 |
-
|
| 733 |
-
f"β {comparison_result.message}",
|
| 734 |
-
"\n".join(ref_status),
|
| 735 |
-
"Performance comparison failed"
|
| 736 |
-
)
|
| 737 |
-
|
| 738 |
-
# Generate feedback
|
| 739 |
-
comparison = comparison_result.data
|
| 740 |
-
feedback = f"""π― **Style Coaching Analysis**
|
| 741 |
-
|
| 742 |
-
π **Performance Scores**
|
| 743 |
-
β’ Pitch Accuracy: {comparison['pitch_accuracy']:.1f}/100
|
| 744 |
-
β’ Range Match: {comparison['range_match']:.1f}/100
|
| 745 |
-
β’ Vibrato Control: {comparison['vibrato_match']:.1f}/100
|
| 746 |
-
β’ **Overall Similarity: {comparison['overall_similarity']:.1f}/100**
|
| 747 |
|
| 748 |
-
|
| 749 |
|
| 750 |
-
|
| 751 |
-
{
|
| 752 |
-
"β οΈ Work on pitch accuracy. Practice scales and interval training." if comparison['pitch_accuracy'] > 60 else
|
| 753 |
-
"β Significant pitch issues. Focus on basic pitch matching exercises."}
|
| 754 |
|
| 755 |
-
|
| 756 |
-
|
| 757 |
-
|
| 758 |
-
|
|
|
|
| 759 |
|
| 760 |
-
|
| 761 |
-
{"
|
| 762 |
-
"
|
| 763 |
-
"
|
| 764 |
|
| 765 |
-
|
| 766 |
-
{f"β’ Continue practicing - you're very close to the target style!" if comparison['overall_similarity'] > 80 else
|
| 767 |
-
f"β’ Focus on the areas scoring below 70 points" if comparison['overall_similarity'] > 60 else
|
| 768 |
-
f"β’ Start with basic vocal technique exercises before style matching"}
|
| 769 |
-
|
| 770 |
-
π **Progress Tracking:**
|
| 771 |
-
Analyzed {len(ref_features)} reference tracks
|
| 772 |
-
Overall performance: {"Advanced" if comparison['overall_similarity'] > 80 else "Intermediate" if comparison['overall_similarity'] > 60 else "Beginner"}
|
| 773 |
-
"""
|
| 774 |
-
|
| 775 |
-
final_status = f"β
Style coaching complete! Analyzed {len(ref_features)} references and generated personalized feedback."
|
| 776 |
|
| 777 |
-
return (
|
| 778 |
-
final_status,
|
| 779 |
-
"\n".join(ref_status),
|
| 780 |
-
feedback
|
| 781 |
-
)
|
| 782 |
|
| 783 |
except Exception as e:
|
| 784 |
-
|
| 785 |
-
return (
|
| 786 |
-
f"β Style coaching failed: {str(e)}",
|
| 787 |
-
"Processing error occurred",
|
| 788 |
-
"An error occurred during analysis. Please try again."
|
| 789 |
-
)
|
| 790 |
|
| 791 |
-
# Create
|
| 792 |
-
def
|
| 793 |
-
"""Create the main professional Gradio interface"""
|
| 794 |
-
|
| 795 |
-
# Custom CSS for professional styling
|
| 796 |
-
custom_css = """
|
| 797 |
-
.gradio-container {
|
| 798 |
-
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
| 799 |
-
max-width: 1200px !important;
|
| 800 |
-
margin: auto;
|
| 801 |
-
}
|
| 802 |
-
.header-text {
|
| 803 |
-
text-align: center;
|
| 804 |
-
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 805 |
-
color: white;
|
| 806 |
-
padding: 2rem;
|
| 807 |
-
border-radius: 10px;
|
| 808 |
-
margin-bottom: 2rem;
|
| 809 |
-
}
|
| 810 |
-
.tab-nav {
|
| 811 |
-
border-radius: 10px 10px 0 0;
|
| 812 |
-
}
|
| 813 |
-
.output-text {
|
| 814 |
-
font-family: 'Courier New', monospace;
|
| 815 |
-
}
|
| 816 |
-
"""
|
| 817 |
|
| 818 |
-
with gr.Blocks(
|
| 819 |
-
|
| 820 |
gr.HTML("""
|
| 821 |
-
<div
|
| 822 |
-
<h1>π€
|
| 823 |
-
<p>Professional
|
| 824 |
-
<p><strong>Version 2.0.0</strong> | Advanced source separation, vocal effects, and AI-powered style coaching</p>
|
| 825 |
</div>
|
| 826 |
""")
|
| 827 |
|
| 828 |
with gr.Tabs():
|
| 829 |
-
|
| 830 |
-
|
| 831 |
-
|
| 832 |
-
###
|
| 833 |
-
Upload your audio files to separate vocals from instruments with state-of-the-art AI models.
|
| 834 |
-
Apply professional vocal effects and get detailed audio analysis.
|
| 835 |
-
""")
|
| 836 |
|
| 837 |
with gr.Row():
|
| 838 |
-
with gr.Column(
|
| 839 |
-
|
| 840 |
-
|
| 841 |
-
label="π Upload Audio File",
|
| 842 |
-
sources=["upload"],
|
| 843 |
-
show_download_button=True
|
| 844 |
-
)
|
| 845 |
-
|
| 846 |
-
stems_mode = gr.Dropdown(
|
| 847 |
choices=["2-stem (Vocals + Instrumental)", "4-stem (Vocals + Drums + Bass + Other)"],
|
| 848 |
value="2-stem (Vocals + Instrumental)",
|
| 849 |
-
label="
|
| 850 |
-
info="Choose the complexity of separation"
|
| 851 |
-
)
|
| 852 |
-
|
| 853 |
-
with gr.Group():
|
| 854 |
-
gr.Markdown("**ποΈ Vocal Effects**")
|
| 855 |
-
with gr.Row():
|
| 856 |
-
pitch_shift = gr.Slider(
|
| 857 |
-
minimum=-12, maximum=12, value=0, step=0.5,
|
| 858 |
-
label="Pitch Shift (semitones)",
|
| 859 |
-
info="Transpose vocals up or down"
|
| 860 |
-
)
|
| 861 |
-
reverb = gr.Slider(
|
| 862 |
-
minimum=0, maximum=0.5, value=0, step=0.05,
|
| 863 |
-
label="Reverb Amount",
|
| 864 |
-
info="Add spatial depth"
|
| 865 |
-
)
|
| 866 |
-
with gr.Row():
|
| 867 |
-
chorus = gr.Slider(
|
| 868 |
-
minimum=0, maximum=0.3, value=0, step=0.05,
|
| 869 |
-
label="Chorus Effect",
|
| 870 |
-
info="Add vocal thickness"
|
| 871 |
-
)
|
| 872 |
-
compression = gr.Slider(
|
| 873 |
-
minimum=0, maximum=1, value=0, step=0.1,
|
| 874 |
-
label="Compression",
|
| 875 |
-
info="Even out dynamics"
|
| 876 |
-
)
|
| 877 |
-
|
| 878 |
-
process_btn = gr.Button(
|
| 879 |
-
"π Process Audio",
|
| 880 |
-
variant="primary",
|
| 881 |
-
size="lg"
|
| 882 |
)
|
|
|
|
| 883 |
|
| 884 |
-
with gr.Column(
|
| 885 |
-
|
| 886 |
-
|
| 887 |
-
interactive=False,
|
| 888 |
-
lines=3
|
| 889 |
-
)
|
| 890 |
-
|
| 891 |
-
analysis_output = gr.Textbox(
|
| 892 |
-
label="π Audio Analysis",
|
| 893 |
-
interactive=False,
|
| 894 |
-
lines=20,
|
| 895 |
-
elem_classes="output-text"
|
| 896 |
-
)
|
| 897 |
|
| 898 |
-
# Output audio files
|
| 899 |
with gr.Row():
|
| 900 |
-
|
| 901 |
-
|
| 902 |
-
show_download_button=True
|
| 903 |
-
)
|
| 904 |
-
audio_output2 = gr.Audio(
|
| 905 |
-
label="πΌ Instrumental/Drums",
|
| 906 |
-
show_download_button=True
|
| 907 |
-
)
|
| 908 |
|
| 909 |
with gr.Row():
|
| 910 |
-
|
| 911 |
-
|
| 912 |
-
show_download_button=True
|
| 913 |
-
)
|
| 914 |
-
audio_output4 = gr.Audio(
|
| 915 |
-
label="πΉ Other (4-stem only)",
|
| 916 |
-
show_download_button=True
|
| 917 |
-
)
|
| 918 |
|
| 919 |
-
#
|
| 920 |
-
with gr.Tab("
|
| 921 |
-
gr.Markdown(""
|
| 922 |
-
### Real-time Recording & Vocal Processing
|
| 923 |
-
Record your voice directly and apply professional vocal effects in real-time.
|
| 924 |
-
Perfect for vocal practice and experimentation.
|
| 925 |
-
""")
|
| 926 |
|
| 927 |
with gr.Row():
|
| 928 |
-
with gr.Column(
|
| 929 |
-
|
| 930 |
-
|
| 931 |
-
|
| 932 |
-
|
| 933 |
-
show_download_button=True
|
| 934 |
-
)
|
| 935 |
-
|
| 936 |
-
with gr.Group():
|
| 937 |
-
gr.Markdown("**ποΈ Real-time Effects**")
|
| 938 |
-
with gr.Row():
|
| 939 |
-
live_pitch = gr.Slider(
|
| 940 |
-
minimum=-12, maximum=12, value=0, step=0.5,
|
| 941 |
-
label="Pitch Correction",
|
| 942 |
-
info="Real-time pitch adjustment"
|
| 943 |
-
)
|
| 944 |
-
live_reverb = gr.Slider(
|
| 945 |
-
minimum=0, maximum=0.5, value=0, step=0.05,
|
| 946 |
-
label="Studio Reverb",
|
| 947 |
-
info="Professional reverb effect"
|
| 948 |
-
)
|
| 949 |
-
with gr.Row():
|
| 950 |
-
live_chorus = gr.Slider(
|
| 951 |
-
minimum=0, maximum=0.3, value=0, step=0.05,
|
| 952 |
-
label="Vocal Doubling",
|
| 953 |
-
info="Thicken your voice"
|
| 954 |
-
)
|
| 955 |
-
live_compression = gr.Slider(
|
| 956 |
-
minimum=0, maximum=1, value=0, step=0.1,
|
| 957 |
-
label="Dynamic Control",
|
| 958 |
-
info="Professional compression"
|
| 959 |
-
)
|
| 960 |
-
|
| 961 |
-
live_process_btn = gr.Button(
|
| 962 |
-
"π΅ Process Recording",
|
| 963 |
-
variant="primary",
|
| 964 |
-
size="lg"
|
| 965 |
-
)
|
| 966 |
|
| 967 |
-
with gr.Column(
|
| 968 |
-
|
| 969 |
-
|
| 970 |
-
interactive=False,
|
| 971 |
-
lines=3
|
| 972 |
-
)
|
| 973 |
-
|
| 974 |
-
live_analysis = gr.Textbox(
|
| 975 |
-
label="π Recording Analysis",
|
| 976 |
-
interactive=False,
|
| 977 |
-
lines=15,
|
| 978 |
-
elem_classes="output-text"
|
| 979 |
-
)
|
| 980 |
|
| 981 |
-
|
| 982 |
-
label="π§ Processed Recording",
|
| 983 |
-
show_download_button=True
|
| 984 |
-
)
|
| 985 |
|
| 986 |
-
#
|
| 987 |
-
with gr.Tab("
|
| 988 |
-
gr.Markdown(""
|
| 989 |
-
### Professional Vocal Style Analysis & Coaching
|
| 990 |
-
Upload reference tracks from artists you want to emulate, then record your performance.
|
| 991 |
-
Get detailed AI-powered feedback on how to improve your vocal style.
|
| 992 |
-
""")
|
| 993 |
|
| 994 |
with gr.Row():
|
| 995 |
-
with gr.Column(
|
| 996 |
-
|
| 997 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 998 |
file_count="multiple",
|
| 999 |
-
file_types=["audio"]
|
| 1000 |
-
info="Upload songs from artists whose style you want to learn"
|
| 1001 |
-
)
|
| 1002 |
-
|
| 1003 |
-
user_performance = gr.Audio(
|
| 1004 |
-
type="filepath",
|
| 1005 |
-
label="π€ Your Performance",
|
| 1006 |
-
sources=["upload", "microphone"],
|
| 1007 |
-
show_download_button=True,
|
| 1008 |
-
info="Record or upload your singing"
|
| 1009 |
)
|
| 1010 |
-
|
| 1011 |
-
|
| 1012 |
-
"
|
| 1013 |
-
|
| 1014 |
-
size="lg"
|
| 1015 |
)
|
|
|
|
| 1016 |
|
| 1017 |
-
with gr.Column(
|
| 1018 |
-
|
| 1019 |
-
|
| 1020 |
-
interactive=False,
|
| 1021 |
-
lines=4
|
| 1022 |
-
)
|
| 1023 |
-
|
| 1024 |
-
reference_status = gr.Textbox(
|
| 1025 |
-
label="π Reference Processing",
|
| 1026 |
-
interactive=False,
|
| 1027 |
-
lines=8
|
| 1028 |
-
)
|
| 1029 |
|
| 1030 |
-
|
| 1031 |
-
label="π― Personalized Coaching Feedback",
|
| 1032 |
-
interactive=False,
|
| 1033 |
-
lines=25,
|
| 1034 |
-
elem_classes="output-text"
|
| 1035 |
-
)
|
| 1036 |
|
| 1037 |
-
#
|
| 1038 |
-
with gr.Tab("βΉοΈ Help
|
| 1039 |
-
gr.Markdown(
|
| 1040 |
-
#
|
| 1041 |
-
|
| 1042 |
-
**Version:** {VERSION}
|
| 1043 |
-
**Author:** Lead Developer Team
|
| 1044 |
-
**Last Updated:** 2024
|
| 1045 |
-
|
| 1046 |
-
## π Features Overview
|
| 1047 |
-
|
| 1048 |
-
### π΅ Audio Separation & Analysis
|
| 1049 |
-
- **Source Separation**: Advanced AI-powered vocal isolation using Spleeter
|
| 1050 |
-
- **Multi-stem Options**: 2-stem (vocals/instrumental) or 4-stem (vocals/drums/bass/other)
|
| 1051 |
-
- **Professional Effects**: Pitch shifting, reverb, chorus, and compression
|
| 1052 |
-
- **Detailed Analysis**: Comprehensive audio feature extraction and visualization
|
| 1053 |
-
|
| 1054 |
-
### ποΈ Live Recording & Effects
|
| 1055 |
-
- **Real-time Recording**: Direct microphone input with instant processing
|
| 1056 |
-
- **Professional Effects Chain**: Studio-quality vocal processing
|
| 1057 |
-
- **Live Analysis**: Instant feedback on your recording characteristics
|
| 1058 |
-
|
| 1059 |
-
### π AI Style Coaching
|
| 1060 |
-
- **Reference-based Learning**: Upload tracks from artists you want to emulate
|
| 1061 |
-
- **AI-powered Analysis**: Advanced vocal characteristic comparison
|
| 1062 |
-
- **Personalized Feedback**: Specific recommendations for improvement
|
| 1063 |
-
- **Progress Tracking**: Monitor your vocal development over time
|
| 1064 |
-
|
| 1065 |
-
## π Supported Formats
|
| 1066 |
-
- **Input**: MP3, WAV, FLAC, M4A, OGG, AAC
|
| 1067 |
-
- **Output**: High-quality WAV files
|
| 1068 |
-
- **Maximum File Size**: 50MB per file
|
| 1069 |
-
- **Maximum Duration**: 10 minutes per track
|
| 1070 |
|
| 1071 |
-
##
|
| 1072 |
|
| 1073 |
-
###
|
| 1074 |
-
|
| 1075 |
-
2
|
| 1076 |
-
|
| 1077 |
-
4. **Record in a quiet environment** for live recording
|
| 1078 |
-
5. **Choose similar genres** for reference tracks in style coaching
|
| 1079 |
|
| 1080 |
-
###
|
| 1081 |
-
-
|
| 1082 |
-
-
|
| 1083 |
-
-
|
| 1084 |
|
| 1085 |
-
##
|
| 1086 |
-
-
|
| 1087 |
-
-
|
| 1088 |
-
-
|
| 1089 |
-
- **Effects Processing**: Professional-grade audio effects
|
| 1090 |
|
| 1091 |
-
##
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1092 |
|
| 1093 |
-
##
|
| 1094 |
-
- Use separated vocals to practice harmonies
|
| 1095 |
-
- Apply pitch correction to hear your target pitch
|
| 1096 |
-
- Use compression to understand dynamic control
|
| 1097 |
|
| 1098 |
-
|
| 1099 |
-
-
|
| 1100 |
-
-
|
| 1101 |
-
-
|
| 1102 |
|
| 1103 |
-
##
|
| 1104 |
-
-
|
| 1105 |
-
-
|
| 1106 |
-
- Use effects subtly for natural-sounding results
|
| 1107 |
|
| 1108 |
-
##
|
| 1109 |
-
|
|
|
|
| 1110 |
|
| 1111 |
---
|
| 1112 |
-
|
| 1113 |
-
**β οΈ Important Notes:**
|
| 1114 |
-
- This tool is for educational and practice purposes
|
| 1115 |
-
- Respect copyright when using reference tracks
|
| 1116 |
-
- Results may vary based on audio quality and complexity
|
| 1117 |
-
- Processing times depend on file size and server load
|
| 1118 |
""")
|
| 1119 |
|
| 1120 |
-
#
|
| 1121 |
-
|
| 1122 |
-
|
| 1123 |
-
inputs=[
|
| 1124 |
-
outputs=[
|
| 1125 |
-
show_progress=True
|
| 1126 |
)
|
| 1127 |
|
| 1128 |
-
|
| 1129 |
-
|
| 1130 |
-
inputs=[
|
| 1131 |
-
outputs=[
|
| 1132 |
-
show_progress=True
|
| 1133 |
)
|
| 1134 |
|
| 1135 |
-
|
| 1136 |
-
|
| 1137 |
-
inputs=[
|
| 1138 |
-
outputs=[
|
| 1139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1140 |
)
|
| 1141 |
|
| 1142 |
-
return
|
| 1143 |
|
| 1144 |
if __name__ == "__main__":
|
| 1145 |
-
|
| 1146 |
-
|
| 1147 |
-
demo = create_main_interface()
|
| 1148 |
-
demo.launch(
|
| 1149 |
-
server_name="0.0.0.0",
|
| 1150 |
-
server_port=7860,
|
| 1151 |
-
show_api=True,
|
| 1152 |
-
show_error=True,
|
| 1153 |
-
quiet=False
|
| 1154 |
-
)
|
| 1155 |
-
except Exception as e:
|
| 1156 |
-
logger.error(f"Failed to launch application: {e}")
|
| 1157 |
-
traceback.print_exc()
|
| 1158 |
-
sys.exit(1)
|
| 1159 |
-
finally:
|
| 1160 |
-
# Cleanup on exit
|
| 1161 |
-
try:
|
| 1162 |
-
processor.cleanup_session()
|
| 1163 |
-
except:
|
| 1164 |
-
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import librosa
|
| 3 |
import numpy as np
|
| 4 |
import soundfile as sf
|
|
|
|
| 5 |
import os
|
|
|
|
|
|
|
| 6 |
import tempfile
|
| 7 |
+
import shutil
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
| 9 |
import warnings
|
| 10 |
warnings.filterwarnings("ignore")
|
| 11 |
|
| 12 |
+
# Import for advanced features
|
| 13 |
+
try:
|
| 14 |
+
from spleeter.separator import Separator
|
| 15 |
+
SPLEETER_AVAILABLE = True
|
| 16 |
+
except ImportError:
|
| 17 |
+
SPLEETER_AVAILABLE = False
|
| 18 |
+
print("Spleeter not available - source separation disabled")
|
| 19 |
|
| 20 |
+
try:
|
| 21 |
+
import scipy.signal
|
| 22 |
+
from scipy.spatial.distance import euclidean
|
| 23 |
+
from dtw import dtw
|
| 24 |
+
ADVANCED_FEATURES = True
|
| 25 |
+
except ImportError:
|
| 26 |
+
ADVANCED_FEATURES = False
|
| 27 |
+
print("Advanced features not available")
|
| 28 |
|
| 29 |
+
class AudioEngine:
|
| 30 |
+
"""Clean, professional audio processing engine"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
def __init__(self):
|
| 33 |
+
self.temp_dir = tempfile.mkdtemp()
|
| 34 |
+
self.separators = {} # Cache for Spleeter models
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
def analyze_audio(self, audio_path):
|
| 37 |
+
"""Extract comprehensive audio features"""
|
| 38 |
try:
|
| 39 |
+
# Load audio
|
| 40 |
+
y, sr = librosa.load(audio_path)
|
| 41 |
|
| 42 |
+
# Basic properties
|
| 43 |
+
duration = len(y) / sr
|
| 44 |
+
tempo, beats = librosa.beat.beat_track(y=y, sr=sr)
|
|
|
|
| 45 |
|
| 46 |
+
# Spectral features
|
| 47 |
+
spectral_centroid = np.mean(librosa.feature.spectral_centroid(y=y, sr=sr))
|
| 48 |
+
spectral_rolloff = np.mean(librosa.feature.spectral_rolloff(y=y, sr=sr))
|
| 49 |
+
zero_crossing_rate = np.mean(librosa.feature.zero_crossing_rate(y))
|
| 50 |
+
|
| 51 |
+
# Energy features
|
| 52 |
+
rms_energy = np.mean(librosa.feature.rms(y=y))
|
| 53 |
+
|
| 54 |
+
# Pitch estimation
|
| 55 |
+
pitches, magnitudes = librosa.piptrack(y=y, sr=sr)
|
| 56 |
+
pitch_values = []
|
| 57 |
+
for t in range(pitches.shape[1]):
|
| 58 |
+
index = magnitudes[:, t].argmax()
|
| 59 |
+
pitch = pitches[index, t]
|
| 60 |
+
if pitch > 0:
|
| 61 |
+
pitch_values.append(pitch)
|
| 62 |
+
|
| 63 |
+
avg_pitch = np.mean(pitch_values) if pitch_values else 0
|
| 64 |
+
|
| 65 |
+
return {
|
| 66 |
+
'success': True,
|
| 67 |
+
'duration': round(duration, 2),
|
| 68 |
+
'tempo': round(tempo, 1),
|
| 69 |
+
'sample_rate': sr,
|
| 70 |
+
'spectral_centroid': round(spectral_centroid, 2),
|
| 71 |
+
'spectral_rolloff': round(spectral_rolloff, 2),
|
| 72 |
+
'zero_crossing_rate': round(zero_crossing_rate, 4),
|
| 73 |
+
'rms_energy': round(rms_energy, 4),
|
| 74 |
+
'average_pitch': round(avg_pitch, 2),
|
| 75 |
+
'pitch_count': len(pitch_values),
|
| 76 |
+
'beats_detected': len(beats)
|
| 77 |
+
}
|
| 78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
except Exception as e:
|
| 80 |
+
return {'success': False, 'error': str(e)}
|
| 81 |
|
| 82 |
+
def separate_vocals(self, audio_path, model_type="2stems"):
|
| 83 |
+
"""Separate vocals using Spleeter"""
|
| 84 |
+
if not SPLEETER_AVAILABLE:
|
| 85 |
+
return {'success': False, 'error': 'Spleeter not available'}
|
| 86 |
+
|
| 87 |
try:
|
| 88 |
+
# Load or create separator
|
| 89 |
+
if model_type not in self.separators:
|
| 90 |
+
self.separators[model_type] = Separator(f'spleeter:{model_type}-16kHz')
|
| 91 |
+
|
| 92 |
+
separator = self.separators[model_type]
|
| 93 |
+
|
| 94 |
+
# Create output directory
|
| 95 |
+
output_dir = os.path.join(self.temp_dir, f"separation_{np.random.randint(10000)}")
|
| 96 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 97 |
+
|
| 98 |
+
# Separate
|
| 99 |
+
separator.separate_to_file(audio_path, output_dir)
|
| 100 |
+
|
| 101 |
+
# Get results
|
| 102 |
+
audio_name = Path(audio_path).stem
|
| 103 |
+
result_dir = os.path.join(output_dir, audio_name)
|
| 104 |
+
|
| 105 |
+
if model_type == "2stems":
|
| 106 |
+
vocals_path = os.path.join(result_dir, "vocals.wav")
|
| 107 |
+
accompaniment_path = os.path.join(result_dir, "accompaniment.wav")
|
| 108 |
+
|
| 109 |
+
return {
|
| 110 |
+
'success': True,
|
| 111 |
+
'vocals': vocals_path if os.path.exists(vocals_path) else None,
|
| 112 |
+
'accompaniment': accompaniment_path if os.path.exists(accompaniment_path) else None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
}
|
| 114 |
+
|
| 115 |
+
elif model_type == "4stems":
|
| 116 |
+
vocals_path = os.path.join(result_dir, "vocals.wav")
|
| 117 |
+
drums_path = os.path.join(result_dir, "drums.wav")
|
| 118 |
+
bass_path = os.path.join(result_dir, "bass.wav")
|
| 119 |
+
other_path = os.path.join(result_dir, "other.wav")
|
| 120 |
+
|
| 121 |
+
return {
|
| 122 |
+
'success': True,
|
| 123 |
+
'vocals': vocals_path if os.path.exists(vocals_path) else None,
|
| 124 |
+
'drums': drums_path if os.path.exists(drums_path) else None,
|
| 125 |
+
'bass': bass_path if os.path.exists(bass_path) else None,
|
| 126 |
+
'other': other_path if os.path.exists(other_path) else None
|
| 127 |
+
}
|
| 128 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
except Exception as e:
|
| 130 |
+
return {'success': False, 'error': str(e)}
|
|
|
|
|
|
|
|
|
|
| 131 |
|
| 132 |
+
def apply_effects(self, audio_path, pitch_shift=0, reverb=0):
|
| 133 |
+
"""Apply vocal effects"""
|
|
|
|
|
|
|
|
|
|
| 134 |
try:
|
| 135 |
+
y, sr = librosa.load(audio_path)
|
| 136 |
+
|
| 137 |
+
# Apply pitch shift
|
| 138 |
+
if pitch_shift != 0:
|
| 139 |
+
y = librosa.effects.pitch_shift(y, sr=sr, n_steps=pitch_shift)
|
| 140 |
+
|
| 141 |
+
# Apply reverb (simple convolution)
|
| 142 |
+
if reverb > 0 and ADVANCED_FEATURES:
|
| 143 |
+
reverb_length = int(0.5 * sr)
|
| 144 |
+
impulse = np.random.randn(reverb_length) * np.exp(-np.arange(reverb_length) / (sr * 0.1))
|
| 145 |
+
y = scipy.signal.convolve(y, impulse * reverb, mode='same')
|
| 146 |
+
y = y / np.max(np.abs(y)) # Normalize
|
| 147 |
+
|
| 148 |
+
# Save processed audio
|
| 149 |
+
output_path = os.path.join(self.temp_dir, f"processed_{np.random.randint(10000)}.wav")
|
| 150 |
+
sf.write(output_path, y, sr)
|
| 151 |
+
|
| 152 |
+
return {'success': True, 'output': output_path}
|
| 153 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
except Exception as e:
|
| 155 |
+
return {'success': False, 'error': str(e)}
|
|
|
|
| 156 |
|
| 157 |
+
def extract_vocal_features(self, audio_path):
|
| 158 |
+
"""Extract features for style coaching"""
|
| 159 |
try:
|
| 160 |
+
y, sr = librosa.load(audio_path)
|
|
|
|
| 161 |
|
| 162 |
+
# Pitch analysis
|
| 163 |
+
pitches, magnitudes = librosa.piptrack(y=y, sr=sr)
|
| 164 |
+
pitch_values = []
|
| 165 |
+
for t in range(pitches.shape[1]):
|
| 166 |
+
index = magnitudes[:, t].argmax()
|
| 167 |
+
pitch = pitches[index, t]
|
| 168 |
+
if pitch > 0:
|
| 169 |
+
pitch_values.append(pitch)
|
| 170 |
|
| 171 |
+
if not pitch_values:
|
| 172 |
+
return {'success': False, 'error': 'No pitch detected'}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
|
| 174 |
+
# Basic vocal metrics
|
| 175 |
+
mean_pitch = np.mean(pitch_values)
|
| 176 |
+
pitch_std = np.std(pitch_values)
|
| 177 |
+
pitch_range = max(pitch_values) - min(pitch_values)
|
| 178 |
|
| 179 |
+
# Tempo
|
| 180 |
+
tempo, _ = librosa.beat.beat_track(y=y, sr=sr)
|
| 181 |
|
| 182 |
+
# Spectral features
|
| 183 |
+
spectral_centroid = np.mean(librosa.feature.spectral_centroid(y=y, sr=sr))
|
| 184 |
+
|
| 185 |
+
# Energy
|
| 186 |
+
rms_energy = np.mean(librosa.feature.rms(y=y))
|
| 187 |
+
|
| 188 |
+
return {
|
| 189 |
+
'success': True,
|
| 190 |
+
'mean_pitch': mean_pitch,
|
| 191 |
+
'pitch_std': pitch_std,
|
| 192 |
+
'pitch_range': pitch_range,
|
| 193 |
+
'tempo': tempo,
|
| 194 |
+
'spectral_centroid': spectral_centroid,
|
| 195 |
+
'rms_energy': rms_energy
|
| 196 |
+
}
|
| 197 |
|
| 198 |
except Exception as e:
|
| 199 |
+
return {'success': False, 'error': str(e)}
|
|
|
|
| 200 |
|
| 201 |
+
def compare_vocal_styles(self, user_features, reference_features_list):
|
| 202 |
+
"""Compare user vocals to reference style"""
|
| 203 |
+
if not ADVANCED_FEATURES:
|
| 204 |
+
return {'success': False, 'error': 'Advanced features not available'}
|
| 205 |
+
|
| 206 |
try:
|
| 207 |
+
# Average reference features
|
| 208 |
+
ref_avg = {}
|
| 209 |
+
for key in ['mean_pitch', 'pitch_std', 'pitch_range', 'tempo', 'spectral_centroid', 'rms_energy']:
|
| 210 |
+
values = [ref[key] for ref in reference_features_list if key in ref]
|
| 211 |
+
ref_avg[key] = np.mean(values) if values else 0
|
| 212 |
|
| 213 |
+
# Calculate differences
|
| 214 |
+
pitch_diff = abs(user_features['mean_pitch'] - ref_avg['mean_pitch'])
|
| 215 |
+
tempo_diff = abs(user_features['tempo'] - ref_avg['tempo'])
|
| 216 |
+
timbre_diff = abs(user_features['spectral_centroid'] - ref_avg['spectral_centroid'])
|
| 217 |
+
energy_diff = abs(user_features['rms_energy'] - ref_avg['rms_energy'])
|
| 218 |
|
| 219 |
+
# Generate feedback
|
| 220 |
+
feedback = []
|
| 221 |
+
|
| 222 |
+
if pitch_diff > 50:
|
| 223 |
+
feedback.append(f"π΅ Pitch: Your average pitch differs by {pitch_diff:.1f} Hz. Practice matching the reference key.")
|
| 224 |
+
else:
|
| 225 |
+
feedback.append("π΅ Pitch: Good pitch accuracy!")
|
| 226 |
+
|
| 227 |
+
if tempo_diff > 10:
|
| 228 |
+
feedback.append(f"β±οΈ Tempo: Your tempo differs by {tempo_diff:.1f} BPM. Work on timing consistency.")
|
| 229 |
+
else:
|
| 230 |
+
feedback.append("β±οΈ Tempo: Good timing!")
|
| 231 |
+
|
| 232 |
+
if timbre_diff > 500:
|
| 233 |
+
feedback.append("π£οΈ Timbre: Try adjusting your vocal tone to match the reference style.")
|
| 234 |
+
else:
|
| 235 |
+
feedback.append("π£οΈ Timbre: Good vocal tone match!")
|
| 236 |
|
| 237 |
+
if energy_diff > 0.1:
|
| 238 |
+
feedback.append("π Energy: Adjust your vocal intensity to match the reference.")
|
| 239 |
+
else:
|
| 240 |
+
feedback.append("π Energy: Good energy level!")
|
|
|
|
| 241 |
|
| 242 |
+
overall_score = max(0, 100 - (pitch_diff/2 + tempo_diff + timbre_diff/10 + energy_diff*100))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
|
| 244 |
+
return {
|
| 245 |
+
'success': True,
|
| 246 |
+
'score': round(overall_score, 1),
|
| 247 |
+
'feedback': feedback,
|
| 248 |
+
'metrics': {
|
| 249 |
+
'pitch_diff': round(pitch_diff, 1),
|
| 250 |
+
'tempo_diff': round(tempo_diff, 1),
|
| 251 |
+
'timbre_diff': round(timbre_diff, 1),
|
| 252 |
+
'energy_diff': round(energy_diff, 3)
|
| 253 |
+
}
|
| 254 |
+
}
|
| 255 |
|
| 256 |
except Exception as e:
|
| 257 |
+
return {'success': False, 'error': str(e)}
|
| 258 |
+
|
| 259 |
+
def cleanup(self):
|
| 260 |
+
"""Clean up temporary files"""
|
| 261 |
+
try:
|
| 262 |
+
if os.path.exists(self.temp_dir):
|
| 263 |
+
shutil.rmtree(self.temp_dir)
|
| 264 |
+
except Exception:
|
| 265 |
+
pass
|
| 266 |
|
| 267 |
+
# Global engine instance
|
| 268 |
+
engine = AudioEngine()
|
|
|
|
| 269 |
|
| 270 |
+
def format_analysis_results(analysis):
|
| 271 |
"""Format analysis results for display"""
|
| 272 |
+
if not analysis['success']:
|
| 273 |
+
return f"β Analysis failed: {analysis['error']}"
|
| 274 |
|
| 275 |
+
return f"""π Audio Analysis Results
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 276 |
|
| 277 |
+
π΅ Basic Properties:
|
| 278 |
+
β’ Duration: {analysis['duration']} seconds
|
| 279 |
+
β’ Sample Rate: {analysis['sample_rate']} Hz
|
| 280 |
+
β’ Tempo: {analysis['tempo']} BPM
|
| 281 |
|
| 282 |
+
π Audio Characteristics:
|
| 283 |
+
β’ Spectral Centroid: {analysis['spectral_centroid']} Hz
|
| 284 |
+
β’ Spectral Rolloff: {analysis['spectral_rolloff']} Hz
|
| 285 |
+
β’ Zero Crossing Rate: {analysis['zero_crossing_rate']}
|
| 286 |
+
β’ RMS Energy: {analysis['rms_energy']}
|
| 287 |
|
| 288 |
+
π€ Vocal Information:
|
| 289 |
+
β’ Average Pitch: {analysis['average_pitch']} Hz
|
| 290 |
+
β’ Pitch Points Detected: {analysis['pitch_count']}
|
| 291 |
+
β’ Beats Detected: {analysis['beats_detected']}"""
|
| 292 |
|
| 293 |
+
def process_audio_separation(audio_file, separation_mode):
|
| 294 |
+
"""Main audio separation function"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 295 |
if not audio_file:
|
| 296 |
+
return "β Please upload an audio file", None, None, None, None, ""
|
| 297 |
+
|
| 298 |
+
if not SPLEETER_AVAILABLE:
|
| 299 |
+
return "β Spleeter not available for source separation", None, None, None, None, ""
|
|
|
|
| 300 |
|
| 301 |
try:
|
| 302 |
+
# Analyze audio first
|
| 303 |
+
analysis = engine.analyze_audio(audio_file)
|
| 304 |
+
analysis_text = format_analysis_results(analysis)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 305 |
|
| 306 |
# Separate audio
|
| 307 |
+
model_type = "2stems" if "2-stem" in separation_mode else "4stems"
|
| 308 |
+
separation_result = engine.separate_vocals(audio_file, model_type)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 309 |
|
| 310 |
+
if not separation_result['success']:
|
| 311 |
+
return f"β Separation failed: {separation_result['error']}", None, None, None, None, analysis_text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 312 |
|
| 313 |
+
if model_type == "2stems":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 314 |
return (
|
| 315 |
+
"β
2-stem separation completed successfully!",
|
| 316 |
+
separation_result.get('vocals'),
|
| 317 |
+
separation_result.get('accompaniment'),
|
| 318 |
+
None,
|
| 319 |
+
None,
|
| 320 |
analysis_text
|
| 321 |
)
|
| 322 |
else:
|
| 323 |
return (
|
| 324 |
+
"β
4-stem separation completed successfully!",
|
| 325 |
+
separation_result.get('vocals'),
|
| 326 |
+
separation_result.get('drums'),
|
| 327 |
+
separation_result.get('bass'),
|
| 328 |
+
separation_result.get('other'),
|
| 329 |
analysis_text
|
| 330 |
)
|
| 331 |
|
| 332 |
except Exception as e:
|
| 333 |
+
return f"β Processing error: {str(e)}", None, None, None, None, ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 334 |
|
| 335 |
+
def process_vocal_effects(audio_file, pitch_shift, reverb_amount):
|
| 336 |
+
"""Apply vocal effects to audio"""
|
| 337 |
if not audio_file:
|
| 338 |
+
return "β Please upload an audio file", None, ""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 339 |
|
| 340 |
try:
|
| 341 |
+
# Analyze original
|
| 342 |
+
analysis = engine.analyze_audio(audio_file)
|
| 343 |
+
analysis_text = format_analysis_results(analysis)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 344 |
|
| 345 |
# Apply effects
|
| 346 |
+
effects_result = engine.apply_effects(audio_file, pitch_shift, reverb_amount)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 347 |
|
| 348 |
+
if not effects_result['success']:
|
| 349 |
+
return f"β Effects failed: {effects_result['error']}", None, analysis_text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 350 |
|
| 351 |
+
effects_applied = []
|
| 352 |
+
if pitch_shift != 0:
|
| 353 |
+
effects_applied.append(f"Pitch: {pitch_shift:+.1f} semitones")
|
| 354 |
+
if reverb_amount > 0:
|
| 355 |
+
effects_applied.append(f"Reverb: {reverb_amount:.2f}")
|
| 356 |
+
|
| 357 |
+
status = f"β
Effects applied: {', '.join(effects_applied)}" if effects_applied else "β
Audio processed (no effects)"
|
| 358 |
+
|
| 359 |
+
return status, effects_result['output'], analysis_text
|
| 360 |
|
| 361 |
except Exception as e:
|
| 362 |
+
return f"β Processing error: {str(e)}", None, ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 363 |
|
| 364 |
def process_style_coaching(reference_files, user_audio):
|
| 365 |
+
"""Style coaching analysis"""
|
| 366 |
if not reference_files or len(reference_files) < 2:
|
| 367 |
+
return "β Upload at least 2 reference tracks", "", ""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 368 |
|
| 369 |
if not user_audio:
|
| 370 |
+
return "β Please record or upload your performance", "", ""
|
| 371 |
+
|
| 372 |
+
if not SPLEETER_AVAILABLE or not ADVANCED_FEATURES:
|
| 373 |
+
return "β Style coaching requires advanced features", "", ""
|
|
|
|
| 374 |
|
| 375 |
try:
|
| 376 |
# Process reference tracks
|
| 377 |
ref_features = []
|
| 378 |
ref_status = []
|
| 379 |
|
| 380 |
+
for i, ref_file in enumerate(reference_files[:5]):
|
| 381 |
+
# Separate vocals
|
| 382 |
+
separation_result = engine.separate_vocals(ref_file.name, "2stems")
|
| 383 |
+
if separation_result['success'] and separation_result.get('vocals'):
|
| 384 |
+
# Extract features
|
| 385 |
+
features = engine.extract_vocal_features(separation_result['vocals'])
|
| 386 |
+
if features['success']:
|
| 387 |
+
ref_features.append(features)
|
| 388 |
+
ref_status.append(f"β
Reference {i+1}: Processed")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 389 |
else:
|
| 390 |
+
ref_status.append(f"β Reference {i+1}: Feature extraction failed")
|
| 391 |
+
else:
|
| 392 |
+
ref_status.append(f"β Reference {i+1}: Vocal separation failed")
|
| 393 |
|
| 394 |
if len(ref_features) < 2:
|
| 395 |
+
return "β Need at least 2 valid reference tracks", "\n".join(ref_status), ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 396 |
|
| 397 |
# Process user audio
|
| 398 |
+
user_separation = engine.separate_vocals(user_audio, "2stems")
|
| 399 |
+
if not user_separation['success'] or not user_separation.get('vocals'):
|
| 400 |
+
return "β Could not separate vocals from your performance", "\n".join(ref_status), ""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 401 |
|
| 402 |
+
user_features = engine.extract_vocal_features(user_separation['vocals'])
|
| 403 |
+
if not user_features['success']:
|
| 404 |
+
return "β Could not analyze your vocal features", "\n".join(ref_status), ""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 405 |
|
| 406 |
+
# Compare styles
|
| 407 |
+
comparison = engine.compare_vocal_styles(user_features, ref_features)
|
| 408 |
+
if not comparison['success']:
|
| 409 |
+
return f"β Style comparison failed: {comparison['error']}", "\n".join(ref_status), ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 410 |
|
| 411 |
+
# Format feedback
|
| 412 |
+
feedback_text = f"""π― Vocal Style Coaching Results
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 413 |
|
| 414 |
+
π Overall Score: {comparison['score']}/100
|
| 415 |
|
| 416 |
+
π΅ Detailed Feedback:
|
| 417 |
+
{chr(10).join(comparison['feedback'])}
|
|
|
|
|
|
|
| 418 |
|
| 419 |
+
π Technical Metrics:
|
| 420 |
+
β’ Pitch Difference: {comparison['metrics']['pitch_diff']} Hz
|
| 421 |
+
β’ Tempo Difference: {comparison['metrics']['tempo_diff']} BPM
|
| 422 |
+
β’ Timbre Difference: {comparison['metrics']['timbre_diff']} Hz
|
| 423 |
+
β’ Energy Difference: {comparison['metrics']['energy_diff']}
|
| 424 |
|
| 425 |
+
π― Recommendations:
|
| 426 |
+
{f"π₯ Excellent! You're very close to the target style." if comparison['score'] > 80 else
|
| 427 |
+
f"π Good progress! Focus on the areas mentioned above." if comparison['score'] > 60 else
|
| 428 |
+
f"πͺ Keep practicing! Work on basic vocal technique first."}
|
| 429 |
|
| 430 |
+
References analyzed: {len(ref_features)}/5"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 431 |
|
| 432 |
+
return f"β
Style coaching complete! Score: {comparison['score']}/100", "\n".join(ref_status), feedback_text
|
|
|
|
|
|
|
|
|
|
|
|
|
| 433 |
|
| 434 |
except Exception as e:
|
| 435 |
+
return f"β Coaching failed: {str(e)}", "", ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 436 |
|
| 437 |
+
# Create main interface
|
| 438 |
+
def create_app():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 439 |
|
| 440 |
+
with gr.Blocks(title="Audio Singing Helper", theme=gr.themes.Soft()) as app:
|
| 441 |
+
|
| 442 |
gr.HTML("""
|
| 443 |
+
<div style="text-align: center; padding: 20px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; border-radius: 10px; margin-bottom: 20px;">
|
| 444 |
+
<h1>π€ Audio Singing Helper</h1>
|
| 445 |
+
<p>Professional audio processing for singers and musicians</p>
|
|
|
|
| 446 |
</div>
|
| 447 |
""")
|
| 448 |
|
| 449 |
with gr.Tabs():
|
| 450 |
+
|
| 451 |
+
# Audio Separation Tab
|
| 452 |
+
with gr.Tab("π΅ Audio Separation"):
|
| 453 |
+
gr.Markdown("### Separate vocals from instrumental tracks")
|
|
|
|
|
|
|
|
|
|
| 454 |
|
| 455 |
with gr.Row():
|
| 456 |
+
with gr.Column():
|
| 457 |
+
sep_audio_input = gr.Audio(type="filepath", label="Upload Audio File")
|
| 458 |
+
sep_mode = gr.Dropdown(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 459 |
choices=["2-stem (Vocals + Instrumental)", "4-stem (Vocals + Drums + Bass + Other)"],
|
| 460 |
value="2-stem (Vocals + Instrumental)",
|
| 461 |
+
label="Separation Mode"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 462 |
)
|
| 463 |
+
sep_button = gr.Button("π― Separate Audio", variant="primary")
|
| 464 |
|
| 465 |
+
with gr.Column():
|
| 466 |
+
sep_status = gr.Textbox(label="Status", lines=2)
|
| 467 |
+
sep_analysis = gr.Textbox(label="Audio Analysis", lines=12)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 468 |
|
|
|
|
| 469 |
with gr.Row():
|
| 470 |
+
sep_vocals = gr.Audio(label="π€ Vocals")
|
| 471 |
+
sep_instrumental = gr.Audio(label="πΌ Instrumental/Drums")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 472 |
|
| 473 |
with gr.Row():
|
| 474 |
+
sep_bass = gr.Audio(label="πΈ Bass")
|
| 475 |
+
sep_other = gr.Audio(label="πΉ Other")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 476 |
|
| 477 |
+
# Vocal Effects Tab
|
| 478 |
+
with gr.Tab("ποΈ Vocal Effects"):
|
| 479 |
+
gr.Markdown("### Apply professional vocal effects")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 480 |
|
| 481 |
with gr.Row():
|
| 482 |
+
with gr.Column():
|
| 483 |
+
fx_audio_input = gr.Audio(type="filepath", label="Upload Audio File")
|
| 484 |
+
fx_pitch = gr.Slider(-12, 12, 0, step=0.5, label="Pitch Shift (semitones)")
|
| 485 |
+
fx_reverb = gr.Slider(0, 0.5, 0, step=0.05, label="Reverb Amount")
|
| 486 |
+
fx_button = gr.Button("π΅ Apply Effects", variant="primary")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 487 |
|
| 488 |
+
with gr.Column():
|
| 489 |
+
fx_status = gr.Textbox(label="Status", lines=2)
|
| 490 |
+
fx_analysis = gr.Textbox(label="Audio Analysis", lines=10)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 491 |
|
| 492 |
+
fx_output = gr.Audio(label="π§ Processed Audio")
|
|
|
|
|
|
|
|
|
|
| 493 |
|
| 494 |
+
# Live Recording Tab
|
| 495 |
+
with gr.Tab("ποΈ Live Recording"):
|
| 496 |
+
gr.Markdown("### Record and process your voice in real-time")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 497 |
|
| 498 |
with gr.Row():
|
| 499 |
+
with gr.Column():
|
| 500 |
+
live_audio = gr.Audio(type="filepath", sources=["microphone"], label="Record Your Voice")
|
| 501 |
+
live_pitch = gr.Slider(-12, 12, 0, step=0.5, label="Pitch Correction")
|
| 502 |
+
live_reverb = gr.Slider(0, 0.5, 0, step=0.05, label="Reverb")
|
| 503 |
+
live_button = gr.Button("π€ Process Recording", variant="primary")
|
| 504 |
+
|
| 505 |
+
with gr.Column():
|
| 506 |
+
live_status = gr.Textbox(label="Status", lines=2)
|
| 507 |
+
live_analysis = gr.Textbox(label="Recording Analysis", lines=10)
|
| 508 |
+
|
| 509 |
+
live_output = gr.Audio(label="π§ Processed Recording")
|
| 510 |
+
|
| 511 |
+
# Style Coaching Tab
|
| 512 |
+
with gr.Tab("π Style Coaching"):
|
| 513 |
+
gr.Markdown("### Get personalized vocal coaching feedback")
|
| 514 |
+
|
| 515 |
+
with gr.Row():
|
| 516 |
+
with gr.Column():
|
| 517 |
+
coach_refs = gr.File(
|
| 518 |
+
label="Reference Tracks (2-5 files)",
|
| 519 |
file_count="multiple",
|
| 520 |
+
file_types=["audio"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 521 |
)
|
| 522 |
+
coach_user = gr.Audio(
|
| 523 |
+
type="filepath",
|
| 524 |
+
label="Your Performance",
|
| 525 |
+
sources=["upload", "microphone"]
|
|
|
|
| 526 |
)
|
| 527 |
+
coach_button = gr.Button("π― Get Coaching", variant="primary")
|
| 528 |
|
| 529 |
+
with gr.Column():
|
| 530 |
+
coach_status = gr.Textbox(label="Status", lines=3)
|
| 531 |
+
coach_refs_status = gr.Textbox(label="Reference Processing", lines=8)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 532 |
|
| 533 |
+
coach_feedback = gr.Textbox(label="π― Coaching Feedback", lines=15)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 534 |
|
| 535 |
+
# Help Tab
|
| 536 |
+
with gr.Tab("βΉοΈ Help"):
|
| 537 |
+
gr.Markdown("""
|
| 538 |
+
# π€ Audio Singing Helper - User Guide
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 539 |
|
| 540 |
+
## Features
|
| 541 |
|
| 542 |
+
### π΅ Audio Separation
|
| 543 |
+
- Upload any song to separate vocals from instruments
|
| 544 |
+
- Choose 2-stem (vocals + instrumental) or 4-stem (vocals + drums + bass + other)
|
| 545 |
+
- Get detailed audio analysis of your tracks
|
|
|
|
|
|
|
| 546 |
|
| 547 |
+
### ποΈ Vocal Effects
|
| 548 |
+
- Apply pitch shifting (-12 to +12 semitones)
|
| 549 |
+
- Add reverb for spatial depth
|
| 550 |
+
- Process any audio file with professional effects
|
| 551 |
|
| 552 |
+
### ποΈ Live Recording
|
| 553 |
+
- Record directly from your microphone
|
| 554 |
+
- Apply real-time pitch correction and reverb
|
| 555 |
+
- Perfect for vocal practice and experimentation
|
|
|
|
| 556 |
|
| 557 |
+
### π Style Coaching
|
| 558 |
+
- Upload 2-5 reference tracks from artists you want to emulate
|
| 559 |
+
- Record or upload your performance
|
| 560 |
+
- Get AI-powered feedback on pitch, timing, and vocal characteristics
|
| 561 |
+
- Receive a score and specific improvement suggestions
|
| 562 |
|
| 563 |
+
## Tips for Best Results
|
|
|
|
|
|
|
|
|
|
| 564 |
|
| 565 |
+
- **Use high-quality audio files** - better input = better results
|
| 566 |
+
- **Keep files under 5 minutes** for faster processing
|
| 567 |
+
- **For style coaching**: Choose references from similar genres
|
| 568 |
+
- **Record in quiet environments** for best analysis
|
| 569 |
|
| 570 |
+
## Supported Formats
|
| 571 |
+
- Input: MP3, WAV, FLAC, M4A, OGG
|
| 572 |
+
- Output: High-quality WAV files
|
|
|
|
| 573 |
|
| 574 |
+
## Technical Requirements
|
| 575 |
+
- Some features require additional dependencies
|
| 576 |
+
- Processing time varies based on file length and complexity
|
| 577 |
|
| 578 |
---
|
| 579 |
+
Built for singers and musicians worldwide π
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 580 |
""")
|
| 581 |
|
| 582 |
+
# Connect all the event handlers
|
| 583 |
+
sep_button.click(
|
| 584 |
+
process_audio_separation,
|
| 585 |
+
inputs=[sep_audio_input, sep_mode],
|
| 586 |
+
outputs=[sep_status, sep_vocals, sep_instrumental, sep_bass, sep_other, sep_analysis]
|
|
|
|
| 587 |
)
|
| 588 |
|
| 589 |
+
fx_button.click(
|
| 590 |
+
process_vocal_effects,
|
| 591 |
+
inputs=[fx_audio_input, fx_pitch, fx_reverb],
|
| 592 |
+
outputs=[fx_status, fx_output, fx_analysis]
|
|
|
|
| 593 |
)
|
| 594 |
|
| 595 |
+
live_button.click(
|
| 596 |
+
process_vocal_effects,
|
| 597 |
+
inputs=[live_audio, live_pitch, live_reverb],
|
| 598 |
+
outputs=[live_status, live_output, live_analysis]
|
| 599 |
+
)
|
| 600 |
+
|
| 601 |
+
coach_button.click(
|
| 602 |
+
process_style_coaching,
|
| 603 |
+
inputs=[coach_refs, coach_user],
|
| 604 |
+
outputs=[coach_status, coach_refs_status, coach_feedback]
|
| 605 |
)
|
| 606 |
|
| 607 |
+
return app
|
| 608 |
|
| 609 |
if __name__ == "__main__":
|
| 610 |
+
app = create_app()
|
| 611 |
+
app.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|