Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,75 +1,158 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
with gr.Blocks(theme=gr.themes.Default(primary_hue="blue", secondary_hue="red")) as demo:
|
| 2 |
gr.Markdown("# 🎵 Loop Architect")
|
| 3 |
onset_times_state = gr.State(value=None)
|
| 4 |
active_stem_state = gr.State(value=None)
|
| 5 |
-
|
| 6 |
-
drums_bar_times_state = gr.State(value=None)
|
| 7 |
-
bass_bar_times_state = gr.State(value=None)
|
| 8 |
-
other_bar_times_state = gr.State(value=None)
|
| 9 |
-
|
| 10 |
-
|
| 11 |
with gr.Row():
|
| 12 |
with gr.Column(scale=1):
|
| 13 |
gr.Markdown("### 1. Separate Stems")
|
| 14 |
audio_input = gr.Audio(type="filepath", label="Upload a Track")
|
| 15 |
stem_options = gr.Radio(["4 Stems (Vocals, Drums, Bass, Other)", "2 Stems (Vocals + Instrumental)"], label="Separation Type", value="4 Stems (Vocals, Drums, Bass, Other)")
|
| 16 |
submit_button = gr.Button("Separate Stems")
|
| 17 |
-
|
| 18 |
with gr.Column(scale=2):
|
| 19 |
with gr.Accordion("Separated Stems", open=True):
|
| 20 |
-
with gr.Row():
|
| 21 |
-
vocals_output = gr.Audio(label="Vocals", scale=
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
create_drums_loop_btn = gr.Button("Create Loop")
|
| 34 |
-
drums_loop_output = gr.Audio(label="Drums Loop", visible=False, scale=2)
|
| 35 |
-
drums_loop_download_btn = gr.DownloadButton(value="Download Loop", visible=False)
|
| 36 |
-
with gr.Row():
|
| 37 |
-
bass_output = gr.Audio(label="Bass", scale=2)
|
| 38 |
-
with gr.Column(scale=1):
|
| 39 |
-
slice_bass_btn = gr.Button("Visualize Slices")
|
| 40 |
-
bass_loop_length = gr.Dropdown(choices=["4 Bars", "8 Bars", "16 Bars"], label="Loop Length", value="4 Bars")
|
| 41 |
-
create_bass_loop_btn = gr.Button("Create Loop")
|
| 42 |
-
bass_loop_output = gr.Audio(label="Bass Loop", visible=False, scale=2)
|
| 43 |
-
bass_loop_download_btn = gr.DownloadButton(value="Download Loop", visible=False)
|
| 44 |
-
with gr.Row():
|
| 45 |
-
other_output = gr.Audio(label="Other / Instrumental", scale=2)
|
| 46 |
-
with gr.Column(scale=1):
|
| 47 |
-
slice_other_btn = gr.Button("Visualize Slices")
|
| 48 |
-
other_loop_length = gr.Dropdown(choices=["4 Bars", "8 Bars", "16 Bars"], label="Loop Length", value="4 Bars")
|
| 49 |
-
create_other_loop_btn = gr.Button("Create Loop")
|
| 50 |
-
other_loop_output = gr.Audio(label="Other Loop", visible=False, scale=2)
|
| 51 |
-
other_loop_download_btn = gr.DownloadButton(value="Download Loop", visible=False)
|
| 52 |
-
|
| 53 |
-
|
| 54 |
gr.Markdown("### Slice Editor")
|
| 55 |
-
|
| 56 |
-
slice_plot = gr.Image(label="Click a region on the waveform to preview a slice")
|
| 57 |
preview_player = gr.Audio(label="Slice Preview")
|
| 58 |
|
| 59 |
-
submit_button.click(fn=separate_stems, inputs=[audio_input, stem_options], outputs=[vocals_output, drums_output, bass_output, other_output
|
| 60 |
stem_options.change(fn=update_output_visibility, inputs=stem_options, outputs=[vocals_output, drums_output, bass_output, other_output])
|
| 61 |
-
|
| 62 |
-
slice_vocals_btn.click(fn=visualize_slices, inputs=
|
| 63 |
-
slice_drums_btn.click(fn=visualize_slices, inputs=
|
| 64 |
-
slice_bass_btn.click(fn=visualize_slices, inputs=
|
| 65 |
-
slice_other_btn.click(fn=visualize_slices, inputs=
|
| 66 |
-
|
| 67 |
slice_plot.select(fn=preview_slice, inputs=[active_stem_state, onset_times_state], outputs=preview_player)
|
| 68 |
|
| 69 |
-
create_vocals_loop_btn.click(fn=create_loop, inputs=[vocals_output, vocals_bar_times_state, vocals_loop_length], outputs=[vocals_loop_output, vocals_loop_download_btn])
|
| 70 |
-
create_drums_loop_btn.click(fn=create_loop, inputs=[drums_output, drums_bar_times_state, drums_loop_length], outputs=[drums_loop_output, drums_loop_download_btn])
|
| 71 |
-
create_bass_loop_btn.click(fn=create_loop, inputs=[bass_output, bass_bar_times_state, bass_loop_length], outputs=[bass_loop_output, bass_loop_download_btn])
|
| 72 |
-
create_other_loop_btn.click(fn=create_loop, inputs=[other_output, other_bar_times_state, other_loop_length], outputs=[other_loop_output, other_loop_download_btn])
|
| 73 |
-
|
| 74 |
-
|
| 75 |
demo.launch()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import shutil
|
| 4 |
+
import asyncio
|
| 5 |
+
import librosa
|
| 6 |
+
import librosa.display
|
| 7 |
+
import soundfile as sf
|
| 8 |
+
import numpy as np
|
| 9 |
+
import time
|
| 10 |
+
import zipfile
|
| 11 |
+
import tempfile
|
| 12 |
+
import matplotlib.pyplot as plt
|
| 13 |
+
import matplotlib
|
| 14 |
+
matplotlib.use('Agg')
|
| 15 |
+
|
| 16 |
+
def update_output_visibility(choice):
|
| 17 |
+
if "2 Stems" in choice:
|
| 18 |
+
return {
|
| 19 |
+
vocals_output: gr.update(visible=True),
|
| 20 |
+
drums_output: gr.update(visible=False),
|
| 21 |
+
bass_output: gr.update(visible=False),
|
| 22 |
+
other_output: gr.update(visible=True, label="Instrumental (No Vocals)")
|
| 23 |
+
}
|
| 24 |
+
elif "4 Stems" in choice:
|
| 25 |
+
return {
|
| 26 |
+
vocals_output: gr.update(visible=True),
|
| 27 |
+
drums_output: gr.update(visible=True),
|
| 28 |
+
bass_output: gr.update(visible=True),
|
| 29 |
+
other_output: gr.update(visible=True, label="Other")
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
async def separate_stems(audio_file_path, stem_choice, progress=gr.Progress(track_tqdm=True)):
|
| 33 |
+
if audio_file_path is None: raise gr.Error("No audio file uploaded!")
|
| 34 |
+
progress(0, desc="Starting...")
|
| 35 |
+
try:
|
| 36 |
+
progress(0.05, desc="Preparing audio file...")
|
| 37 |
+
original_filename_base = os.path.basename(audio_file_path).rsplit('.', 1)[0]
|
| 38 |
+
stable_input_path = f"stable_input_{original_filename_base}.wav"
|
| 39 |
+
shutil.copy(audio_file_path, stable_input_path)
|
| 40 |
+
|
| 41 |
+
model_arg = "--two-stems=vocals" if "2 Stems" in stem_choice else ""
|
| 42 |
+
output_dir = "separated"
|
| 43 |
+
if os.path.exists(output_dir): shutil.rmtree(output_dir)
|
| 44 |
+
|
| 45 |
+
command = f"python3 -m demucs {model_arg} -o \"{output_dir}\" \"{stable_input_path}\""
|
| 46 |
+
progress(0.2, desc="Running Demucs (this can take a minute)...")
|
| 47 |
+
|
| 48 |
+
process = await asyncio.create_subprocess_shell(
|
| 49 |
+
command,
|
| 50 |
+
stdout=asyncio.subprocess.PIPE,
|
| 51 |
+
stderr=asyncio.subprocess.PIPE)
|
| 52 |
+
|
| 53 |
+
stdout, stderr = await process.communicate()
|
| 54 |
+
|
| 55 |
+
if process.returncode != 0:
|
| 56 |
+
raise gr.Error(f"Demucs failed to run. Error: {stderr.decode()[:500]}")
|
| 57 |
+
|
| 58 |
+
progress(0.8, desc="Locating separated stem files...")
|
| 59 |
+
stable_filename_base = os.path.basename(stable_input_path).rsplit('.', 1)[0]
|
| 60 |
+
model_folder_name = next(os.walk(output_dir))[1][0]
|
| 61 |
+
stems_path = os.path.join(output_dir, model_folder_name, stable_filename_base)
|
| 62 |
+
|
| 63 |
+
if not os.path.exists(stems_path):
|
| 64 |
+
raise gr.Error(f"Demucs finished, but the output directory was not found!")
|
| 65 |
+
|
| 66 |
+
vocals_path = os.path.join(stems_path, "vocals.wav") if os.path.exists(os.path.join(stems_path, "vocals.wav")) else None
|
| 67 |
+
drums_path = os.path.join(stems_path, "drums.wav") if os.path.exists(os.path.join(stems_path, "drums.wav")) else None
|
| 68 |
+
bass_path = os.path.join(stems_path, "bass.wav") if os.path.exists(os.path.join(stems_path, "bass.wav")) else None
|
| 69 |
+
other_filename = "no_vocals.wav" if "2 Stems" in stem_choice else "other.wav"
|
| 70 |
+
other_path = os.path.join(stems_path, other_filename) if os.path.exists(os.path.join(stems_path, other_filename)) else None
|
| 71 |
+
|
| 72 |
+
os.remove(stable_input_path)
|
| 73 |
+
return vocals_path, drums_path, bass_path, other_path
|
| 74 |
+
except Exception as e:
|
| 75 |
+
print(f"An error occurred: {e}")
|
| 76 |
+
raise gr.Error(str(e))
|
| 77 |
+
|
| 78 |
+
def visualize_slices(stem_audio_data, progress=gr.Progress(track_tqdm=True)):
|
| 79 |
+
if stem_audio_data is None:
|
| 80 |
+
gr.Warning("This stem is empty. Cannot visualize.")
|
| 81 |
+
return None, None, None
|
| 82 |
+
|
| 83 |
+
sample_rate, y_int = stem_audio_data
|
| 84 |
+
y = librosa.util.buf_to_float(y_int)
|
| 85 |
+
|
| 86 |
+
progress(0.3, desc="Finding transients...")
|
| 87 |
+
onset_frames = librosa.onset.onset_detect(y=librosa.to_mono(y.T) if y.ndim > 1 else y, sr=sample_rate, wait=1, pre_avg=1, post_avg=1, post_max=1, delta=0.05)
|
| 88 |
+
onset_times = librosa.frames_to_time(onset_frames, sr=sample_rate)
|
| 89 |
+
|
| 90 |
+
progress(0.7, desc="Generating waveform plot...")
|
| 91 |
+
fig, ax = plt.subplots(figsize=(10, 3))
|
| 92 |
+
fig.patch.set_facecolor('#1f2937')
|
| 93 |
+
ax.set_facecolor('#111827')
|
| 94 |
+
librosa.display.waveshow(y, sr=sample_rate, ax=ax, color='#32f6ff', alpha=0.7)
|
| 95 |
+
for t in onset_times:
|
| 96 |
+
ax.axvline(x=t, color='#ff3b3b', linestyle='--', linewidth=1)
|
| 97 |
+
ax.tick_params(colors='gray'); ax.xaxis.label.set_color('gray'); ax.yaxis.label.set_color('gray')
|
| 98 |
+
ax.set_xlabel("Time (s)"); ax.set_ylabel("Amplitude"); ax.set_title("Detected Slices", color='white')
|
| 99 |
+
plt.tight_layout()
|
| 100 |
+
|
| 101 |
+
progress(1, desc="Done!")
|
| 102 |
+
return fig, onset_times, stem_audio_data
|
| 103 |
+
|
| 104 |
+
def preview_slice(active_stem_audio, onset_times, evt: gr.SelectData):
|
| 105 |
+
if active_stem_audio is None or onset_times is None: return None
|
| 106 |
+
sample_rate, y = active_stem_audio; clicked_time = evt.index[0]
|
| 107 |
+
start_time = 0; end_time = len(y) / sample_rate
|
| 108 |
+
for i, t in enumerate(onset_times):
|
| 109 |
+
if t > clicked_time:
|
| 110 |
+
end_time = t; break
|
| 111 |
+
start_time = t
|
| 112 |
+
start_sample = librosa.time_to_samples(start_time, sr=sample_rate)
|
| 113 |
+
end_sample = librosa.time_to_samples(end_time, sr=sample_rate)
|
| 114 |
+
sliced_audio = y[start_sample:end_sample]
|
| 115 |
+
return (sample_rate, sliced_audio)
|
| 116 |
+
|
| 117 |
with gr.Blocks(theme=gr.themes.Default(primary_hue="blue", secondary_hue="red")) as demo:
|
| 118 |
gr.Markdown("# 🎵 Loop Architect")
|
| 119 |
onset_times_state = gr.State(value=None)
|
| 120 |
active_stem_state = gr.State(value=None)
|
| 121 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
with gr.Row():
|
| 123 |
with gr.Column(scale=1):
|
| 124 |
gr.Markdown("### 1. Separate Stems")
|
| 125 |
audio_input = gr.Audio(type="filepath", label="Upload a Track")
|
| 126 |
stem_options = gr.Radio(["4 Stems (Vocals, Drums, Bass, Other)", "2 Stems (Vocals + Instrumental)"], label="Separation Type", value="4 Stems (Vocals, Drums, Bass, Other)")
|
| 127 |
submit_button = gr.Button("Separate Stems")
|
| 128 |
+
|
| 129 |
with gr.Column(scale=2):
|
| 130 |
with gr.Accordion("Separated Stems", open=True):
|
| 131 |
+
with gr.Row():
|
| 132 |
+
vocals_output = gr.Audio(label="Vocals", scale=4)
|
| 133 |
+
slice_vocals_btn = gr.Button("Visualize Slices", scale=1)
|
| 134 |
+
with gr.Row():
|
| 135 |
+
drums_output = gr.Audio(label="Drums", scale=4)
|
| 136 |
+
slice_drums_btn = gr.Button("Visualize Slices", scale=1)
|
| 137 |
+
with gr.Row():
|
| 138 |
+
bass_output = gr.Audio(label="Bass", scale=4)
|
| 139 |
+
slice_bass_btn = gr.Button("Visualize Slices", scale=1)
|
| 140 |
+
with gr.Row():
|
| 141 |
+
other_output = gr.Audio(label="Other / Instrumental", scale=4)
|
| 142 |
+
slice_other_btn = gr.Button("Visualize Slices", scale=1)
|
| 143 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
gr.Markdown("### Slice Editor")
|
| 145 |
+
slice_plot = gr.Plot(label="Click a region on the waveform to preview a slice")
|
|
|
|
| 146 |
preview_player = gr.Audio(label="Slice Preview")
|
| 147 |
|
| 148 |
+
submit_button.click(fn=separate_stems, inputs=[audio_input, stem_options], outputs=[vocals_output, drums_output, bass_output, other_output])
|
| 149 |
stem_options.change(fn=update_output_visibility, inputs=stem_options, outputs=[vocals_output, drums_output, bass_output, other_output])
|
| 150 |
+
|
| 151 |
+
slice_vocals_btn.click(fn=visualize_slices, inputs=vocals_output, outputs=[slice_plot, onset_times_state, active_stem_state])
|
| 152 |
+
slice_drums_btn.click(fn=visualize_slices, inputs=drums_output, outputs=[slice_plot, onset_times_state, active_stem_state])
|
| 153 |
+
slice_bass_btn.click(fn=visualize_slices, inputs=bass_output, outputs=[slice_plot, onset_times_state, active_stem_state])
|
| 154 |
+
slice_other_btn.click(fn=visualize_slices, inputs=other_output, outputs=[slice_plot, onset_times_state, active_stem_state])
|
| 155 |
+
|
| 156 |
slice_plot.select(fn=preview_slice, inputs=[active_stem_state, onset_times_state], outputs=preview_player)
|
| 157 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
demo.launch()
|