Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -27,39 +27,7 @@ import soundfile as sf
|
|
| 27 |
# Suppress warnings
|
| 28 |
warnings.filterwarnings("ignore")
|
| 29 |
|
| 30 |
-
# ===
|
| 31 |
-
effect_map = {
|
| 32 |
-
"Noise Reduction": apply_noise_reduction,
|
| 33 |
-
"Compress Dynamic Range": apply_compression,
|
| 34 |
-
"Add Reverb": apply_reverb,
|
| 35 |
-
"Pitch Shift": lambda x: apply_pitch_shift(x),
|
| 36 |
-
"Echo": apply_echo,
|
| 37 |
-
"Stereo Widening": apply_stereo_widen,
|
| 38 |
-
"Bass Boost": apply_bass_boost,
|
| 39 |
-
"Treble Boost": apply_treble_boost,
|
| 40 |
-
"Normalize": apply_normalize,
|
| 41 |
-
"Noise Gate": lambda x: apply_noise_gate(x, threshold=-50.0),
|
| 42 |
-
"Limiter": lambda x: apply_limiter(x, limit_dB=-1),
|
| 43 |
-
"Bitcrusher": lambda x: apply_bitcrush(x, bit_depth=8),
|
| 44 |
-
"Auto Gain": lambda x: apply_auto_gain(x, target_dB=-20),
|
| 45 |
-
"Vocal Distortion": lambda x: apply_vocal_distortion(x),
|
| 46 |
-
"Harmony": lambda x: apply_harmony(x),
|
| 47 |
-
"Stage Mode": apply_stage_mode
|
| 48 |
-
}
|
| 49 |
-
|
| 50 |
-
# === Helper Functions ===
|
| 51 |
-
def audiosegment_to_array(audio):
|
| 52 |
-
return np.array(audio.get_array_of_samples()), audio.frame_rate
|
| 53 |
-
|
| 54 |
-
def array_to_audiosegment(samples, frame_rate, channels=1):
|
| 55 |
-
return AudioSegment(
|
| 56 |
-
samples.tobytes(),
|
| 57 |
-
frame_rate=int(frame_rate),
|
| 58 |
-
sample_width=samples.dtype.itemsize,
|
| 59 |
-
channels=channels
|
| 60 |
-
)
|
| 61 |
-
|
| 62 |
-
# === Effect Functions ===
|
| 63 |
def apply_normalize(audio):
|
| 64 |
return audio.normalize()
|
| 65 |
|
|
@@ -135,6 +103,18 @@ def apply_bitcrush(audio, bit_depth=8):
|
|
| 135 |
downsampled = np.round(samples / (32768 / max_val)).astype(np.int16)
|
| 136 |
return array_to_audiosegment(downsampled, audio.frame_rate // 2, channels=audio.channels)
|
| 137 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
# === Loudness Matching (EBU R128) ===
|
| 139 |
try:
|
| 140 |
import pyloudnorm as pyln
|
|
@@ -244,6 +224,22 @@ def apply_vocal_isolation(audio_path):
|
|
| 244 |
save_track(out_path, vocal_track, model.samplerate)
|
| 245 |
return out_path
|
| 246 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 247 |
# === Process Audio Function โ Fully Featured ===
|
| 248 |
def process_audio(audio_file, selected_effects, isolate_vocals, preset_name, export_format):
|
| 249 |
status = "๐ Loading audio..."
|
|
@@ -266,7 +262,6 @@ def process_audio(audio_file, selected_effects, isolate_vocals, preset_name, exp
|
|
| 266 |
"Bitcrusher": lambda x: apply_bitcrush(x, bit_depth=8),
|
| 267 |
"Auto Gain": lambda x: apply_auto_gain(x, target_dB=-20),
|
| 268 |
"Vocal Distortion": lambda x: apply_vocal_distortion(x),
|
| 269 |
-
"Harmony": lambda x: apply_harmony(x),
|
| 270 |
"Stage Mode": apply_stage_mode
|
| 271 |
}
|
| 272 |
|
|
@@ -406,7 +401,7 @@ def key_to_semitone(key="C"):
|
|
| 406 |
"F#": 6, "G": 7, "G#": 8, "A": 9, "A#": 10, "B": 11}
|
| 407 |
return keys.get(key, 0)
|
| 408 |
|
| 409 |
-
# === AI Remastering Tab โ
|
| 410 |
def ai_remaster(audio_path):
|
| 411 |
try:
|
| 412 |
# Step 1: Noise reduction
|
|
@@ -484,7 +479,7 @@ def live_effect_preview(stream, selected_effect):
|
|
| 484 |
except Exception as e:
|
| 485 |
return None
|
| 486 |
|
| 487 |
-
# === Voice Command Parsing
|
| 488 |
def voice_to_effect(command):
|
| 489 |
command = command.lower()
|
| 490 |
if "noise reduction" in command or "remove noise" in command:
|
|
@@ -535,14 +530,23 @@ def harmonic_saturation(audio, saturation_type="Tube", intensity=0.2):
|
|
| 535 |
|
| 536 |
return array_to_audiosegment(saturated.astype(np.int16), audio.frame_rate, channels=audio.channels)
|
| 537 |
|
| 538 |
-
#
|
| 539 |
-
def
|
| 540 |
-
|
| 541 |
-
|
| 542 |
-
|
| 543 |
-
|
| 544 |
-
|
| 545 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 546 |
|
| 547 |
# === Save/Load Mix Session (.aiproj) โ Added Back ===
|
| 548 |
def save_project(audio, preset, effects):
|
|
@@ -580,13 +584,6 @@ with gr.Blocks(css="""
|
|
| 580 |
animation: float 3s ease-in-out infinite;
|
| 581 |
}
|
| 582 |
|
| 583 |
-
.studio-header h3 {
|
| 584 |
-
font-size: 18px;
|
| 585 |
-
color: #9ca3af;
|
| 586 |
-
margin-top: -5px;
|
| 587 |
-
font-style: italic;
|
| 588 |
-
}
|
| 589 |
-
|
| 590 |
@keyframes float {
|
| 591 |
0%, 100% { transform: translateY(0); }
|
| 592 |
50% { transform: translateY(-10px); }
|
|
@@ -600,7 +597,6 @@ with gr.Blocks(css="""
|
|
| 600 |
font-weight: bold;
|
| 601 |
box-shadow: 0 0 10px #2563eb44;
|
| 602 |
border: none;
|
| 603 |
-
font-size: 16px;
|
| 604 |
}
|
| 605 |
|
| 606 |
.gr-button:hover {
|
|
@@ -660,7 +656,7 @@ with gr.Blocks(css="""
|
|
| 660 |
}
|
| 661 |
""") as demo:
|
| 662 |
|
| 663 |
-
# Header
|
| 664 |
gr.HTML('''
|
| 665 |
<div class="studio-header">
|
| 666 |
<img src="logo.png" width="400" />
|
|
@@ -716,7 +712,7 @@ with gr.Blocks(css="""
|
|
| 716 |
with gr.Row():
|
| 717 |
with gr.Column(min_width=200):
|
| 718 |
input_audio_remix = gr.Audio(label="Upload Music Track", type="filepath")
|
| 719 |
-
split_button = gr.Button("Split
|
| 720 |
with gr.Column(min_width=400):
|
| 721 |
stem_outputs = [
|
| 722 |
gr.File(label="Vocals"),
|
|
@@ -724,6 +720,7 @@ with gr.Blocks(css="""
|
|
| 724 |
gr.File(label="Bass"),
|
| 725 |
gr.File(label="Other")
|
| 726 |
]
|
|
|
|
| 727 |
split_button.click(fn=stem_split, inputs=[input_audio_remix], outputs=stem_outputs)
|
| 728 |
|
| 729 |
gr.Markdown("#### Apply Effects to Individual Stems")
|
|
@@ -778,7 +775,7 @@ with gr.Blocks(css="""
|
|
| 778 |
output_other = gr.Audio(label="Processed Other", type="filepath")
|
| 779 |
btn_other.click(fn=apply_effects_to_stem, inputs=[other_path, other_effects], outputs=output_other)
|
| 780 |
|
| 781 |
-
# --- AI Remastering Tab โ
|
| 782 |
with gr.Tab("๐ฎ AI Remastering"):
|
| 783 |
gr.Interface(
|
| 784 |
fn=ai_remaster,
|
|
@@ -799,8 +796,7 @@ with gr.Blocks(css="""
|
|
| 799 |
],
|
| 800 |
outputs=gr.Audio(label="Warm Output", type="filepath"),
|
| 801 |
title="Add Analog-Style Warmth",
|
| 802 |
-
description="Enhance clarity and presence using saturation styles like Tube or Tape."
|
| 803 |
-
allow_flagging="never"
|
| 804 |
)
|
| 805 |
|
| 806 |
# --- Vocal Doubler / Harmonizer โ Added ===
|
|
@@ -831,8 +827,7 @@ with gr.Blocks(css="""
|
|
| 831 |
title="Batch Audio Processor",
|
| 832 |
description="Upload multiple files, apply effects in bulk, and download all results in a single ZIP.",
|
| 833 |
flagging_mode="never",
|
| 834 |
-
submit_btn="Process All Files"
|
| 835 |
-
clear_btn=None
|
| 836 |
)
|
| 837 |
|
| 838 |
# --- Vocal Pitch Correction โ Auto-Tune Style ===
|
|
@@ -849,23 +844,6 @@ with gr.Blocks(css="""
|
|
| 849 |
)
|
| 850 |
|
| 851 |
# --- Frequency Spectrum Tab โ Real-time Visualizer ===
|
| 852 |
-
def visualize_spectrum(audio_path):
|
| 853 |
-
y, sr = torchaudio.load(audio_path)
|
| 854 |
-
y_np = y.numpy().flatten()
|
| 855 |
-
stft = librosa.stft(y_np)
|
| 856 |
-
db = librosa.amplitude_to_db(abs(stft))
|
| 857 |
-
|
| 858 |
-
plt.figure(figsize=(10, 4))
|
| 859 |
-
img = librosa.display.specshow(db, sr=sr, x_axis="time", y_axis="hz", cmap="magma")
|
| 860 |
-
plt.colorbar(img, format="%+2.0f dB")
|
| 861 |
-
plt.title("Frequency Spectrum")
|
| 862 |
-
plt.tight_layout()
|
| 863 |
-
buf = BytesIO()
|
| 864 |
-
plt.savefig(buf, format="png")
|
| 865 |
-
plt.close()
|
| 866 |
-
buf.seek(0)
|
| 867 |
-
return Image.open(buf)
|
| 868 |
-
|
| 869 |
with gr.Tab("๐ Frequency Spectrum"):
|
| 870 |
gr.Interface(
|
| 871 |
fn=visualize_spectrum,
|
|
@@ -967,11 +945,28 @@ with gr.Blocks(css="""
|
|
| 967 |
fn=live_effect_preview,
|
| 968 |
inputs=[
|
| 969 |
gr.Audio(sources=["microphone"], streaming=True, label="Live Mic Input"),
|
| 970 |
-
gr.Dropdown(choices=["Normalize", "Reverb", "Pitch Shift", "Saturation"]
|
| 971 |
],
|
| 972 |
outputs=gr.Audio(label="Live Output", type="filepath"),
|
| 973 |
title="Preview Effects in Real-Time",
|
| 974 |
-
description="Try effects live before exporting"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 975 |
)
|
| 976 |
|
| 977 |
# --- AI Suggest Effects Based on Genre ===
|
|
|
|
| 27 |
# Suppress warnings
|
| 28 |
warnings.filterwarnings("ignore")
|
| 29 |
|
| 30 |
+
# === Effect Map (Global) ===
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
def apply_normalize(audio):
|
| 32 |
return audio.normalize()
|
| 33 |
|
|
|
|
| 103 |
downsampled = np.round(samples / (32768 / max_val)).astype(np.int16)
|
| 104 |
return array_to_audiosegment(downsampled, audio.frame_rate // 2, channels=audio.channels)
|
| 105 |
|
| 106 |
+
# === Helper Functions ===
|
| 107 |
+
def audiosegment_to_array(audio):
|
| 108 |
+
return np.array(audio.get_array_of_samples()), audio.frame_rate
|
| 109 |
+
|
| 110 |
+
def array_to_audiosegment(samples, frame_rate, channels=1):
|
| 111 |
+
return AudioSegment(
|
| 112 |
+
samples.tobytes(),
|
| 113 |
+
frame_rate=int(frame_rate),
|
| 114 |
+
sample_width=samples.dtype.itemsize,
|
| 115 |
+
channels=channels
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
# === Loudness Matching (EBU R128) ===
|
| 119 |
try:
|
| 120 |
import pyloudnorm as pyln
|
|
|
|
| 224 |
save_track(out_path, vocal_track, model.samplerate)
|
| 225 |
return out_path
|
| 226 |
|
| 227 |
+
# === Stem Splitting (Drums, Bass, Other, Vocals) โ Now Defined! ===
|
| 228 |
+
def stem_split(audio_path):
|
| 229 |
+
model = pretrained.get_model(name='htdemucs')
|
| 230 |
+
wav = load_track_local(audio_path, model.samplerate, channels=2)
|
| 231 |
+
sources = apply_model(model, wav[None])[0]
|
| 232 |
+
|
| 233 |
+
output_dir = tempfile.mkdtemp()
|
| 234 |
+
stem_paths = []
|
| 235 |
+
|
| 236 |
+
for i, name in enumerate(['drums', 'bass', 'other', 'vocals']):
|
| 237 |
+
path = os.path.join(output_dir, f"{name}.wav")
|
| 238 |
+
save_track(path, sources[i].cpu(), model.samplerate)
|
| 239 |
+
stem_paths.append(gr.File(value=path))
|
| 240 |
+
|
| 241 |
+
return stem_paths
|
| 242 |
+
|
| 243 |
# === Process Audio Function โ Fully Featured ===
|
| 244 |
def process_audio(audio_file, selected_effects, isolate_vocals, preset_name, export_format):
|
| 245 |
status = "๐ Loading audio..."
|
|
|
|
| 262 |
"Bitcrusher": lambda x: apply_bitcrush(x, bit_depth=8),
|
| 263 |
"Auto Gain": lambda x: apply_auto_gain(x, target_dB=-20),
|
| 264 |
"Vocal Distortion": lambda x: apply_vocal_distortion(x),
|
|
|
|
| 265 |
"Stage Mode": apply_stage_mode
|
| 266 |
}
|
| 267 |
|
|
|
|
| 401 |
"F#": 6, "G": 7, "G#": 8, "A": 9, "A#": 10, "B": 11}
|
| 402 |
return keys.get(key, 0)
|
| 403 |
|
| 404 |
+
# === AI Remastering Tab โ New Feature (Without SpeechBrain) ===
|
| 405 |
def ai_remaster(audio_path):
|
| 406 |
try:
|
| 407 |
# Step 1: Noise reduction
|
|
|
|
| 479 |
except Exception as e:
|
| 480 |
return None
|
| 481 |
|
| 482 |
+
# === Voice Command Parsing ===
|
| 483 |
def voice_to_effect(command):
|
| 484 |
command = command.lower()
|
| 485 |
if "noise reduction" in command or "remove noise" in command:
|
|
|
|
| 530 |
|
| 531 |
return array_to_audiosegment(saturated.astype(np.int16), audio.frame_rate, channels=audio.channels)
|
| 532 |
|
| 533 |
+
# === Visualizers ===
|
| 534 |
+
def visualize_spectrum(audio_path):
|
| 535 |
+
y, sr = torchaudio.load(audio_path)
|
| 536 |
+
y_np = y.numpy().flatten()
|
| 537 |
+
stft = librosa.stft(y_np)
|
| 538 |
+
db = librosa.amplitude_to_db(abs(stft))
|
| 539 |
+
|
| 540 |
+
plt.figure(figsize=(10, 4))
|
| 541 |
+
img = librosa.display.specshow(db, sr=sr, x_axis="time", y_axis="hz", cmap="magma")
|
| 542 |
+
plt.colorbar(img, format="%+2.0f dB")
|
| 543 |
+
plt.title("Frequency Spectrum")
|
| 544 |
+
plt.tight_layout()
|
| 545 |
+
buf = BytesIO()
|
| 546 |
+
plt.savefig(buf, format="png")
|
| 547 |
+
plt.close()
|
| 548 |
+
buf.seek(0)
|
| 549 |
+
return Image.open(buf)
|
| 550 |
|
| 551 |
# === Save/Load Mix Session (.aiproj) โ Added Back ===
|
| 552 |
def save_project(audio, preset, effects):
|
|
|
|
| 584 |
animation: float 3s ease-in-out infinite;
|
| 585 |
}
|
| 586 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 587 |
@keyframes float {
|
| 588 |
0%, 100% { transform: translateY(0); }
|
| 589 |
50% { transform: translateY(-10px); }
|
|
|
|
| 597 |
font-weight: bold;
|
| 598 |
box-shadow: 0 0 10px #2563eb44;
|
| 599 |
border: none;
|
|
|
|
| 600 |
}
|
| 601 |
|
| 602 |
.gr-button:hover {
|
|
|
|
| 656 |
}
|
| 657 |
""") as demo:
|
| 658 |
|
| 659 |
+
# Header
|
| 660 |
gr.HTML('''
|
| 661 |
<div class="studio-header">
|
| 662 |
<img src="logo.png" width="400" />
|
|
|
|
| 712 |
with gr.Row():
|
| 713 |
with gr.Column(min_width=200):
|
| 714 |
input_audio_remix = gr.Audio(label="Upload Music Track", type="filepath")
|
| 715 |
+
split_button = gr.Button("Split Into Drums, Bass, Vocals, etc.")
|
| 716 |
with gr.Column(min_width=400):
|
| 717 |
stem_outputs = [
|
| 718 |
gr.File(label="Vocals"),
|
|
|
|
| 720 |
gr.File(label="Bass"),
|
| 721 |
gr.File(label="Other")
|
| 722 |
]
|
| 723 |
+
|
| 724 |
split_button.click(fn=stem_split, inputs=[input_audio_remix], outputs=stem_outputs)
|
| 725 |
|
| 726 |
gr.Markdown("#### Apply Effects to Individual Stems")
|
|
|
|
| 775 |
output_other = gr.Audio(label="Processed Other", type="filepath")
|
| 776 |
btn_other.click(fn=apply_effects_to_stem, inputs=[other_path, other_effects], outputs=output_other)
|
| 777 |
|
| 778 |
+
# --- AI Remastering Tab โ New Feature (Fixed) ===
|
| 779 |
with gr.Tab("๐ฎ AI Remastering"):
|
| 780 |
gr.Interface(
|
| 781 |
fn=ai_remaster,
|
|
|
|
| 796 |
],
|
| 797 |
outputs=gr.Audio(label="Warm Output", type="filepath"),
|
| 798 |
title="Add Analog-Style Warmth",
|
| 799 |
+
description="Enhance clarity and presence using saturation styles like Tube or Tape."
|
|
|
|
| 800 |
)
|
| 801 |
|
| 802 |
# --- Vocal Doubler / Harmonizer โ Added ===
|
|
|
|
| 827 |
title="Batch Audio Processor",
|
| 828 |
description="Upload multiple files, apply effects in bulk, and download all results in a single ZIP.",
|
| 829 |
flagging_mode="never",
|
| 830 |
+
submit_btn="Process All Files"
|
|
|
|
| 831 |
)
|
| 832 |
|
| 833 |
# --- Vocal Pitch Correction โ Auto-Tune Style ===
|
|
|
|
| 844 |
)
|
| 845 |
|
| 846 |
# --- Frequency Spectrum Tab โ Real-time Visualizer ===
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 847 |
with gr.Tab("๐ Frequency Spectrum"):
|
| 848 |
gr.Interface(
|
| 849 |
fn=visualize_spectrum,
|
|
|
|
| 945 |
fn=live_effect_preview,
|
| 946 |
inputs=[
|
| 947 |
gr.Audio(sources=["microphone"], streaming=True, label="Live Mic Input"),
|
| 948 |
+
gr.Dropdown(choices=["Normalize", "Reverb", "Pitch Shift", "Saturation"]),
|
| 949 |
],
|
| 950 |
outputs=gr.Audio(label="Live Output", type="filepath"),
|
| 951 |
title="Preview Effects in Real-Time",
|
| 952 |
+
description="Try effects live before exporting",
|
| 953 |
+
allow_flagging="never"
|
| 954 |
+
)
|
| 955 |
+
|
| 956 |
+
# --- Custom EQ Editor ===
|
| 957 |
+
with gr.Tab("๐ Custom EQ Editor"):
|
| 958 |
+
gr.Interface(
|
| 959 |
+
fn=auto_eq,
|
| 960 |
+
inputs=[
|
| 961 |
+
gr.Audio(label="Upload Track", type="filepath"),
|
| 962 |
+
gr.Dropdown(choices=["Pop", "EDM", "Rock", "Hip-Hop", "Acoustic", "Metal", "Trap", "LoFi",
|
| 963 |
+
"Jazz", "Classical", "Chillhop", "Ambient", "Jazz Piano", "Trap EDM",
|
| 964 |
+
"Indie Rock", "Lo-Fi Jazz", "R&B", "Soul", "Funk"],
|
| 965 |
+
label="Genre", value="Pop")
|
| 966 |
+
],
|
| 967 |
+
outputs=gr.Audio(label="EQ-Enhanced Output", type="filepath"),
|
| 968 |
+
title="Custom EQ by Genre",
|
| 969 |
+
description="Apply custom EQ based on genre"
|
| 970 |
)
|
| 971 |
|
| 972 |
# --- AI Suggest Effects Based on Genre ===
|