Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -23,42 +23,11 @@ import base64
|
|
| 23 |
import pickle
|
| 24 |
import json
|
| 25 |
import soundfile as sf
|
| 26 |
-
import wave
|
| 27 |
-
import struct
|
| 28 |
|
| 29 |
# Suppress warnings
|
| 30 |
warnings.filterwarnings("ignore")
|
| 31 |
|
| 32 |
-
# ===
|
| 33 |
-
effect_map = {
|
| 34 |
-
"Noise Reduction": "apply_noise_reduction",
|
| 35 |
-
"Compress Dynamic Range": "apply_compression",
|
| 36 |
-
"Add Reverb": "apply_reverb",
|
| 37 |
-
"Pitch Shift": "apply_pitch_shift",
|
| 38 |
-
"Echo": "apply_echo",
|
| 39 |
-
"Stereo Widening": "apply_stereo_widen",
|
| 40 |
-
"Bass Boost": "apply_bass_boost",
|
| 41 |
-
"Treble Boost": "apply_treble_boost",
|
| 42 |
-
"Normalize": "apply_normalize",
|
| 43 |
-
"Limiter": "apply_limiter",
|
| 44 |
-
"Auto Gain": "apply_auto_gain",
|
| 45 |
-
"Vocal Distortion": "apply_vocal_distortion",
|
| 46 |
-
"Stage Mode": "apply_stage_mode"
|
| 47 |
-
}
|
| 48 |
-
|
| 49 |
-
# === Helper Functions ===
|
| 50 |
-
def audiosegment_to_array(audio):
|
| 51 |
-
return np.array(audio.get_array_of_samples()), audio.frame_rate
|
| 52 |
-
|
| 53 |
-
def array_to_audiosegment(samples, frame_rate, channels=1):
|
| 54 |
-
return AudioSegment(
|
| 55 |
-
samples.tobytes(),
|
| 56 |
-
frame_rate=int(frame_rate),
|
| 57 |
-
sample_width=samples.dtype.itemsize,
|
| 58 |
-
channels=channels
|
| 59 |
-
)
|
| 60 |
-
|
| 61 |
-
# === Effect Functions ===
|
| 62 |
def apply_normalize(audio):
|
| 63 |
return audio.normalize()
|
| 64 |
|
|
@@ -124,6 +93,18 @@ def apply_bitcrush(audio, bit_depth=8):
|
|
| 124 |
downsampled = np.round(samples / (32768 / max_val)).astype(np.int16)
|
| 125 |
return array_to_audiosegment(downsampled, audio.frame_rate // 2, channels=audio.channels)
|
| 126 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
# === Loudness Matching (EBU R128) ===
|
| 128 |
try:
|
| 129 |
import pyloudnorm as pyln
|
|
@@ -185,7 +166,7 @@ def auto_eq(audio, genre="Pop"):
|
|
| 185 |
|
| 186 |
return array_to_audiosegment(samples.astype(np.int16), sr, channels=audio.channels)
|
| 187 |
|
| 188 |
-
# ===
|
| 189 |
def load_track_local(path, sample_rate, channels=2):
|
| 190 |
sig, rate = torchaudio.load(path)
|
| 191 |
if rate != sample_rate:
|
|
@@ -198,6 +179,7 @@ def save_track(path, wav, sample_rate):
|
|
| 198 |
path = Path(path)
|
| 199 |
torchaudio.save(str(path), wav, sample_rate)
|
| 200 |
|
|
|
|
| 201 |
def apply_vocal_isolation(audio_path):
|
| 202 |
model = pretrained.get_model(name='htdemucs')
|
| 203 |
wav = load_track_local(audio_path, model.samplerate, channels=2)
|
|
@@ -211,6 +193,22 @@ def apply_vocal_isolation(audio_path):
|
|
| 211 |
save_track(out_path, vocal_track, model.samplerate)
|
| 212 |
return out_path
|
| 213 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
# === Process Audio Function โ Fully Featured ===
|
| 215 |
def process_audio(audio_file, selected_effects, isolate_vocals, preset_name, export_format):
|
| 216 |
status = "๐ Loading audio..."
|
|
@@ -256,7 +254,7 @@ def process_audio(audio_file, selected_effects, isolate_vocals, preset_name, exp
|
|
| 256 |
|
| 257 |
waveform_image = show_waveform(output_path)
|
| 258 |
genre = detect_genre(output_path)
|
| 259 |
-
session_log = generate_session_log(audio_file,
|
| 260 |
|
| 261 |
status = "๐ Done!"
|
| 262 |
return output_path, waveform_image, session_log, genre, status, history
|
|
@@ -437,7 +435,7 @@ def live_effect_preview(stream, selected_effect):
|
|
| 437 |
except Exception as e:
|
| 438 |
return None
|
| 439 |
|
| 440 |
-
# === Voice Command Parsing
|
| 441 |
def voice_to_effect(command):
|
| 442 |
command = command.lower()
|
| 443 |
if "noise reduction" in command or "remove noise" in command:
|
|
@@ -488,13 +486,17 @@ def harmonic_saturation(audio, saturation_type="Tube", intensity=0.2):
|
|
| 488 |
|
| 489 |
return array_to_audiosegment(saturated.astype(np.int16), audio.frame_rate, channels=audio.channels)
|
| 490 |
|
| 491 |
-
#
|
| 492 |
-
def
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 498 |
return out_path
|
| 499 |
|
| 500 |
# === Save/Load Mix Session (.aiproj) โ Added Back ===
|
|
@@ -518,30 +520,9 @@ def load_project(project_file):
|
|
| 518 |
def process_prompt(audio, prompt):
|
| 519 |
return apply_noise_reduction(audio)
|
| 520 |
|
| 521 |
-
# === A/B Compare Two Tracks ===
|
| 522 |
-
def compare_ab(track1_path, track2_path):
|
| 523 |
-
return track1_path, track2_path
|
| 524 |
-
|
| 525 |
-
# === Loop Section ===
|
| 526 |
-
def loop_section(audio_path, start_ms, end_ms, loops=2):
|
| 527 |
-
audio = AudioSegment.from_file(audio_path)
|
| 528 |
-
section = audio[start_ms:end_ms]
|
| 529 |
-
looped = section * loops
|
| 530 |
-
out_path = os.path.join(tempfile.gettempdir(), "looped_output.wav")
|
| 531 |
-
looped.export(out_path, format="wav")
|
| 532 |
-
return out_path
|
| 533 |
-
|
| 534 |
-
# === Share Effect Chain ===
|
| 535 |
-
def share_effect_chain(selected_effects):
|
| 536 |
-
return json.dumps(selected_effects)
|
| 537 |
-
|
| 538 |
-
def load_effect_chain(chain_json):
|
| 539 |
-
return json.loads(chain_json)
|
| 540 |
-
|
| 541 |
# === Vocal Formant Correction (AI Voice Tuning) ===
|
| 542 |
def formant_correct(audio, shift=1.0):
|
| 543 |
samples, sr = audiosegment_to_array(audio)
|
| 544 |
-
# Simulate formant shifting using librosa
|
| 545 |
corrected = librosa.effects.pitch_shift(samples, sr=sr, n_steps=shift)
|
| 546 |
return array_to_audiosegment(corrected.astype(np.int16), sr, channels=audio.channels)
|
| 547 |
|
|
@@ -733,7 +714,7 @@ with gr.Blocks(css="""
|
|
| 733 |
output_other = gr.Audio(label="Processed Other", type="filepath")
|
| 734 |
btn_other.click(fn=apply_effects_to_stem, inputs=[other_path, other_effects], outputs=output_other)
|
| 735 |
|
| 736 |
-
# --- AI Remastering Tab โ New Feature (
|
| 737 |
with gr.Tab("๐ฎ AI Remastering"):
|
| 738 |
gr.Interface(
|
| 739 |
fn=ai_remaster,
|
|
@@ -975,8 +956,7 @@ with gr.Blocks(css="""
|
|
| 975 |
gr.Audio(label="Version B", type="filepath")
|
| 976 |
],
|
| 977 |
title="Compare Two Versions",
|
| 978 |
-
description="Hear two mixes side-by-side"
|
| 979 |
-
allow_flagging="never"
|
| 980 |
)
|
| 981 |
|
| 982 |
# --- Loop Section ===
|
|
@@ -991,7 +971,7 @@ with gr.Blocks(css="""
|
|
| 991 |
],
|
| 992 |
outputs=gr.Audio(label="Looped Output", type="filepath"),
|
| 993 |
title="Repeat a Section",
|
| 994 |
-
description="Useful for editing
|
| 995 |
)
|
| 996 |
|
| 997 |
# --- Share Effect Chain ===
|
|
@@ -1001,7 +981,7 @@ with gr.Blocks(css="""
|
|
| 1001 |
inputs=gr.CheckboxGroup(choices=preset_choices["Default"]),
|
| 1002 |
outputs=gr.Textbox(label="Share Code", lines=2),
|
| 1003 |
title="Copy/Paste Effect Chain",
|
| 1004 |
-
description="Share your setup
|
| 1005 |
)
|
| 1006 |
|
| 1007 |
with gr.Tab("๐ฅ Load Shared Chain"):
|
|
@@ -1019,14 +999,14 @@ with gr.Blocks(css="""
|
|
| 1019 |
### Keyboard Controls
|
| 1020 |
- `Ctrl + Z`: Undo last effect
|
| 1021 |
- `Ctrl + Y`: Redo
|
| 1022 |
-
- `Spacebar`: Play/Stop
|
| 1023 |
-
- `Ctrl + S`: Save
|
| 1024 |
- `Ctrl + O`: Open session
|
| 1025 |
- `Ctrl + C`: Copy effect chain
|
| 1026 |
- `Ctrl + V`: Paste effect chain
|
| 1027 |
- `Ctrl + Shift + L`: Loop selection
|
| 1028 |
-
- `Ctrl + Shift + P`: Export
|
| 1029 |
-
- `Ctrl + Shift + M`: Export full mix ZIP
|
| 1030 |
""")
|
| 1031 |
|
| 1032 |
# --- Vocal Formant Correction ===
|
|
@@ -1039,35 +1019,7 @@ with gr.Blocks(css="""
|
|
| 1039 |
],
|
| 1040 |
outputs=gr.Audio(label="Natural-Sounding Vocal", type="filepath"),
|
| 1041 |
title="Preserve Vocal Quality During Pitch Shift",
|
| 1042 |
-
description="Make pitch-shifted vocals sound more
|
| 1043 |
)
|
| 1044 |
|
| 1045 |
-
# --- Effect History โ Undo/Redo Stack ===
|
| 1046 |
-
effect_history = []
|
| 1047 |
-
|
| 1048 |
-
def undo_last_effect(history):
|
| 1049 |
-
if len(history) > 1:
|
| 1050 |
-
return history[-2]
|
| 1051 |
-
return history[0]
|
| 1052 |
-
|
| 1053 |
-
def redo_last_effect(history):
|
| 1054 |
-
return history[-1]
|
| 1055 |
-
|
| 1056 |
-
with gr.Tab("๐ Undo/Redo"):
|
| 1057 |
-
history_state = gr.State([])
|
| 1058 |
-
with gr.Row():
|
| 1059 |
-
input_audio_undo = gr.Audio(label="Upload Track", type="filepath")
|
| 1060 |
-
effect_checkbox_undo = gr.CheckboxGroup(choices=preset_choices["Default"], label="Apply Effects")
|
| 1061 |
-
process_btn_undo = gr.Button("Apply")
|
| 1062 |
-
undo_btn = gr.Button("Undo Last")
|
| 1063 |
-
redo_btn = gr.Button("Redo Last")
|
| 1064 |
-
output_audio_undo = gr.Audio(label="Processed Audio", type="filepath")
|
| 1065 |
-
|
| 1066 |
-
process_btn_undo.click(fn=lambda a, e: process_audio(a, e, False, "Default", "WAV")[0],
|
| 1067 |
-
inputs=[input_audio_undo, effect_checkbox_undo],
|
| 1068 |
-
outputs=[output_audio_undo, history_state])
|
| 1069 |
-
|
| 1070 |
-
undo_btn.click(fn=undo_last_effect, inputs=history_state, outputs=output_audio_undo)
|
| 1071 |
-
redo_btn.click(fn=redo_last_effect, inputs=history_state, outputs=output_audio_undo)
|
| 1072 |
-
|
| 1073 |
demo.launch()
|
|
|
|
| 23 |
import pickle
|
| 24 |
import json
|
| 25 |
import soundfile as sf
|
|
|
|
|
|
|
| 26 |
|
| 27 |
# Suppress warnings
|
| 28 |
warnings.filterwarnings("ignore")
|
| 29 |
|
| 30 |
+
# === Effect Map (Global) ===
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
def apply_normalize(audio):
|
| 32 |
return audio.normalize()
|
| 33 |
|
|
|
|
| 93 |
downsampled = np.round(samples / (32768 / max_val)).astype(np.int16)
|
| 94 |
return array_to_audiosegment(downsampled, audio.frame_rate // 2, channels=audio.channels)
|
| 95 |
|
| 96 |
+
# === Helper Functions ===
|
| 97 |
+
def audiosegment_to_array(audio):
|
| 98 |
+
return np.array(audio.get_array_of_samples()), audio.frame_rate
|
| 99 |
+
|
| 100 |
+
def array_to_audiosegment(samples, frame_rate, channels=1):
|
| 101 |
+
return AudioSegment(
|
| 102 |
+
samples.tobytes(),
|
| 103 |
+
frame_rate=int(frame_rate),
|
| 104 |
+
sample_width=samples.dtype.itemsize,
|
| 105 |
+
channels=channels
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
# === Loudness Matching (EBU R128) ===
|
| 109 |
try:
|
| 110 |
import pyloudnorm as pyln
|
|
|
|
| 166 |
|
| 167 |
return array_to_audiosegment(samples.astype(np.int16), sr, channels=audio.channels)
|
| 168 |
|
| 169 |
+
# === Load Track Helpers ===
|
| 170 |
def load_track_local(path, sample_rate, channels=2):
|
| 171 |
sig, rate = torchaudio.load(path)
|
| 172 |
if rate != sample_rate:
|
|
|
|
| 179 |
path = Path(path)
|
| 180 |
torchaudio.save(str(path), wav, sample_rate)
|
| 181 |
|
| 182 |
+
# === Vocal Isolation Helpers ===
|
| 183 |
def apply_vocal_isolation(audio_path):
|
| 184 |
model = pretrained.get_model(name='htdemucs')
|
| 185 |
wav = load_track_local(audio_path, model.samplerate, channels=2)
|
|
|
|
| 193 |
save_track(out_path, vocal_track, model.samplerate)
|
| 194 |
return out_path
|
| 195 |
|
| 196 |
+
# === Stem Splitting Function (Now Added Back!) ===
|
| 197 |
+
def stem_split(audio_path):
|
| 198 |
+
model = pretrained.get_model(name='htdemucs')
|
| 199 |
+
wav = load_track_local(audio_path, model.samplerate, channels=2)
|
| 200 |
+
sources = apply_model(model, wav[None])[0]
|
| 201 |
+
|
| 202 |
+
output_dir = tempfile.mkdtemp()
|
| 203 |
+
stem_paths = []
|
| 204 |
+
|
| 205 |
+
for i, name in enumerate(['drums', 'bass', 'other', 'vocals']):
|
| 206 |
+
path = os.path.join(output_dir, f"{name}.wav")
|
| 207 |
+
save_track(path, sources[i].cpu(), model.samplerate)
|
| 208 |
+
stem_paths.append(gr.File(value=path))
|
| 209 |
+
|
| 210 |
+
return stem_paths
|
| 211 |
+
|
| 212 |
# === Process Audio Function โ Fully Featured ===
|
| 213 |
def process_audio(audio_file, selected_effects, isolate_vocals, preset_name, export_format):
|
| 214 |
status = "๐ Loading audio..."
|
|
|
|
| 254 |
|
| 255 |
waveform_image = show_waveform(output_path)
|
| 256 |
genre = detect_genre(output_path)
|
| 257 |
+
session_log = generate_session_log(audio_file, effects_to_apply, isolate_vocals, export_format, genre)
|
| 258 |
|
| 259 |
status = "๐ Done!"
|
| 260 |
return output_path, waveform_image, session_log, genre, status, history
|
|
|
|
| 435 |
except Exception as e:
|
| 436 |
return None
|
| 437 |
|
| 438 |
+
# === Voice Command Parsing ===
|
| 439 |
def voice_to_effect(command):
|
| 440 |
command = command.lower()
|
| 441 |
if "noise reduction" in command or "remove noise" in command:
|
|
|
|
| 486 |
|
| 487 |
return array_to_audiosegment(saturated.astype(np.int16), audio.frame_rate, channels=audio.channels)
|
| 488 |
|
| 489 |
+
# === A/B Compare Two Tracks ===
|
| 490 |
+
def compare_ab(track1_path, track2_path):
|
| 491 |
+
return track1_path, track2_path
|
| 492 |
+
|
| 493 |
+
# === Loop Section ===
|
| 494 |
+
def loop_section(audio_path, start_ms, end_ms, loops=2):
|
| 495 |
+
audio = AudioSegment.from_file(audio_path)
|
| 496 |
+
section = audio[start_ms:end_ms]
|
| 497 |
+
looped = section * loops
|
| 498 |
+
out_path = os.path.join(tempfile.gettempdir(), "looped_output.wav")
|
| 499 |
+
looped.export(out_path, format="wav")
|
| 500 |
return out_path
|
| 501 |
|
| 502 |
# === Save/Load Mix Session (.aiproj) โ Added Back ===
|
|
|
|
| 520 |
def process_prompt(audio, prompt):
|
| 521 |
return apply_noise_reduction(audio)
|
| 522 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 523 |
# === Vocal Formant Correction (AI Voice Tuning) ===
|
| 524 |
def formant_correct(audio, shift=1.0):
|
| 525 |
samples, sr = audiosegment_to_array(audio)
|
|
|
|
| 526 |
corrected = librosa.effects.pitch_shift(samples, sr=sr, n_steps=shift)
|
| 527 |
return array_to_audiosegment(corrected.astype(np.int16), sr, channels=audio.channels)
|
| 528 |
|
|
|
|
| 714 |
output_other = gr.Audio(label="Processed Other", type="filepath")
|
| 715 |
btn_other.click(fn=apply_effects_to_stem, inputs=[other_path, other_effects], outputs=output_other)
|
| 716 |
|
| 717 |
+
# --- AI Remastering Tab โ New Feature (Fixed) ===
|
| 718 |
with gr.Tab("๐ฎ AI Remastering"):
|
| 719 |
gr.Interface(
|
| 720 |
fn=ai_remaster,
|
|
|
|
| 956 |
gr.Audio(label="Version B", type="filepath")
|
| 957 |
],
|
| 958 |
title="Compare Two Versions",
|
| 959 |
+
description="Hear two mixes side-by-side"
|
|
|
|
| 960 |
)
|
| 961 |
|
| 962 |
# --- Loop Section ===
|
|
|
|
| 971 |
],
|
| 972 |
outputs=gr.Audio(label="Looped Output", type="filepath"),
|
| 973 |
title="Repeat a Section",
|
| 974 |
+
description="Useful for editing specific parts"
|
| 975 |
)
|
| 976 |
|
| 977 |
# --- Share Effect Chain ===
|
|
|
|
| 981 |
inputs=gr.CheckboxGroup(choices=preset_choices["Default"]),
|
| 982 |
outputs=gr.Textbox(label="Share Code", lines=2),
|
| 983 |
title="Copy/Paste Effect Chain",
|
| 984 |
+
description="Share your setup via link/code"
|
| 985 |
)
|
| 986 |
|
| 987 |
with gr.Tab("๐ฅ Load Shared Chain"):
|
|
|
|
| 999 |
### Keyboard Controls
|
| 1000 |
- `Ctrl + Z`: Undo last effect
|
| 1001 |
- `Ctrl + Y`: Redo
|
| 1002 |
+
- `Spacebar`: Play/Stop
|
| 1003 |
+
- `Ctrl + S`: Save session
|
| 1004 |
- `Ctrl + O`: Open session
|
| 1005 |
- `Ctrl + C`: Copy effect chain
|
| 1006 |
- `Ctrl + V`: Paste effect chain
|
| 1007 |
- `Ctrl + Shift + L`: Loop selection
|
| 1008 |
+
- `Ctrl + Shift + P`: Export PDF report
|
| 1009 |
+
- `Ctrl + Shift + M`: Export full mix ZIP
|
| 1010 |
""")
|
| 1011 |
|
| 1012 |
# --- Vocal Formant Correction ===
|
|
|
|
| 1019 |
],
|
| 1020 |
outputs=gr.Audio(label="Natural-Sounding Vocal", type="filepath"),
|
| 1021 |
title="Preserve Vocal Quality During Pitch Shift",
|
| 1022 |
+
description="Make pitch-shifted vocals sound more natural"
|
| 1023 |
)
|
| 1024 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1025 |
demo.launch()
|