Spaces:
Build error
Build error
Update app.py
Browse files12 unmodified lines13141516171819202122232425262728293031324 unmodified lines37383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091924 unmodified lines97989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715812 unmodified linesimport matplotlibmatplotlib.use('Agg')def update_output_visibility(choice):if "2 Stems" in choice:return {vocals_output: gr.update(visible=True),drums_output: gr.update(visible=False),bass_output: gr.update(visible=False),other_output: gr.update(visible=True, label="Instrumental (No Vocals)")}elif "4 Stems" in choice:return {vocals_output: gr.update(visible=True),drums_output: gr.update(visible=True),bass_output: gr.update(visible=True),other_output: gr.update(visible=True, label="Other")}async def separate_stems(audio_file_path, stem_choice, progress=gr.Progress(track_tqdm=True)):4 unmodified linesoriginal_filename_base = os.path.basename(audio_file_path).rsplit('.', 1)[0]stable_input_path = f"stable_input_{original_filename_base}.wav"shutil.copy(audio_file_path, stable_input_path)model_arg = "--two-stems=vocals" if "2 Stems" in stem_choice else ""output_dir = "separated"if os.path.exists(output_dir): shutil.rmtree(output_dir)command = f"python3 -m demucs {model_arg} -o \"{output_dir}\" \"{stable_input_path}\""progress(0.2, desc="Running Demucs (this can take a minute)...")process = await asyncio.create_subprocess_shell(command,stdout=asyncio.subprocess.PIPE,stderr=asyncio.subprocess.PIPE)stdout, stderr = await process.communicate()if process.returncode != 0:raise gr.Error(f"Demucs failed to run. Error: {stderr.decode()[:500]}")progress(0.8, desc="Locating separated stem files...")stable_filename_base = os.path.basename(stable_input_path).rsplit('.', 1)[0]model_folder_name = next(os.walk(output_dir))[1][0]stems_path = os.path.join(output_dir, model_folder_name, stable_filename_base)if not os.path.exists(stems_path):raise gr.Error(f"Demucs finished, but the output directory was not found!")vocals_path = os.path.join(stems_path, "vocals.wav") if os.path.exists(os.path.join(stems_path, "vocals.wav")) else Nonedrums_path = os.path.join(stems_path, "drums.wav") if os.path.exists(os.path.join(stems_path, "drums.wav")) else Nonebass_path = os.path.join(stems_path, "bass.wav") if os.path.exists(os.path.join(stems_path, "bass.wav")) else Noneother_filename = "no_vocals.wav" if "2 Stems" in stem_choice else "other.wav"other_path = os.path.join(stems_path, other_filename) if os.path.exists(os.path.join(stems_path, other_filename)) else Noneos.remove(stable_input_path)return vocals_path, drums_path, bass_path, other_pathexcept Exception as e:print(f"An error occurred: {e}")raise gr.Error(str(e))def visualize_slices(stem_audio_data, progress=gr.Progress(track_tqdm=True)):if stem_audio_data is None:gr.Warning("This stem is empty. Cannot visualize.")return None, None, Nonesample_rate, y_int = stem_audio_datay = librosa.util.buf_to_float(y_int)progress(0.3, desc="Finding transients...")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)onset_times = librosa.frames_to_time(onset_frames, sr=sample_rate)progress(0.7, desc="Generating waveform plot...")fig, ax = plt.subplots(figsize=(10, 3))fig.patch.set_facecolor('#1f2937')4 unmodified linesax.tick_params(colors='gray'); ax.xaxis.label.set_color('gray'); ax.yaxis.label.set_color('gray')ax.set_xlabel("Time (s)"); ax.set_ylabel("Amplitude"); ax.set_title("Detected Slices", color='white')plt.tight_layout()progress(1, desc="Done!")return fig, onset_times, stem_audio_datadef preview_slice(active_stem_audio, onset_times, evt: gr.SelectData):if active_stem_audio is None or onset_times is None: return Nonesample_rate, y = active_stem_audio; clicked_time = evt.index[0]start_time = 0; end_time = len(y) / sample_ratefor i, t in enumerate(onset_times):if t > clicked_time:end_time = t; breakstart_time = tstart_sample = librosa.time_to_samples(start_time, sr=sample_rate)end_sample = librosa.time_to_samples(end_time, sr=sample_rate)sliced_audio = y[start_sample:end_sample]return (sample_rate, sliced_audio)with gr.Blocks(theme=gr.themes.Default(primary_hue="blue", secondary_hue="red")) as demo:gr.Markdown("# 🎵 Loop Architect")onset_times_state = gr.State(value=None)active_stem_state = gr.State(value=None)with gr.Row():with gr.Column(scale=1):gr.Markdown("### 1. Separate Stems")audio_input = gr.Audio(type="filepath", label="Upload a Track")stem_options = gr.Radio(["4 Stems (Vocals, Drums, Bass, Other)", "2 Stems (Vocals + Instrumental)"], label="Separation Type", value="4 Stems (Vocals, Drums, Bass, Other)")submit_button = gr.Button("Separate Stems")with gr.Column(scale=2):with gr.Accordion("Separated Stems", open=True):with gr.Row():vocals_output = gr.Audio(label="Vocals", scale=4)slice_vocals_btn = gr.Button("Visualize Slices", scale=1)with gr.Row():drums_output = gr.Audio(label="Drums", scale=4)slice_drums_btn = gr.Button("Visualize Slices", scale=1)with gr.Row():bass_output = gr.Audio(label="Bass", scale=4)slice_bass_btn = gr.Button("Visualize Slices", scale=1)with gr.Row():other_output = gr.Audio(label="Other / Instrumental", scale=4)slice_other_btn = gr.Button("Visualize Slices", scale=1)gr.Markdown("### Slice Editor")slice_plot = gr.Plot(label="Click a region on the waveform to preview a slice")preview_player = gr.Audio(label="Slice Preview")submit_button.click(fn=separate_stems, inputs=[audio_input, stem_options], outputs=[vocals_output, drums_output, bass_output, other_output])stem_options.change(fn=update_output_visibility, inputs=stem_options, outputs=[vocals_output, drums_output, bass_output, other_output])slice_vocals_btn.click(fn=visualize_slices, inputs=vocals_output, outputs=[slice_plot, onset_times_state, active_stem_state])slice_drums_btn.click(fn=visualize_slices, inputs=drums_output, outputs=[slice_plot, onset_times_state, active_stem_state])slice_bass_btn.click(fn=visualize_slices, inputs=bass_output, outputs=[slice_plot, onset_times_state, active_stem_state])slice_other_btn.click(fn=visualize_slices, inputs=other_output, outputs=[slice_plot, onset_times_state, active_stem_state])slice_plot.click(fn=preview_slice, inputs=[active_stem_state, onset_times_state], outputs=preview_player)demo.launch()12 unmodified lines13141516171819202122232425262728293031323334353637383940414 unmodified lines4647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221234 unmodified lines12812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438512 unmodified linesimport matplotlibmatplotlib.use('Agg')temp_files = []def cleanup_temp_files():global temp_filesfor file_path in temp_files:if os.path.exists(file_path):os.remove(file_path)temp_files = []def update_output_visibility(choice):if "2 Stems" in choice:return {vocals_output: gr.update(visible=True),drums_output: gr.update(visible=False),bass_output: gr.update(visible=False),other_output: gr.update(visible=True, label="Instrumental (No Vocals)")}elif "4 Stems" in choice:return {vocals_output: gr.update(visible=True),drums_output: gr.update(visible=True),bass_output: gr.update(visible=True),other_output: gr.update(visible=True, label="Other")}async def separate_stems(audio_file_path, stem_choice, progress=gr.Progress(track_tqdm=True)):4 unmodified linesoriginal_filename_base = os.path.basename(audio_file_path).rsplit('.', 1)[0]stable_input_path = f"stable_input_{original_filename_base}.wav"shutil.copy(audio_file_path, stable_input_path)model_arg = "--two-stems=vocals" if "2 Stems" in stem_choice else ""output_dir = "separated"if os.path.exists(output_dir): shutil.rmtree(output_dir)command = f"python3 -m demucs {model_arg} -o \"{output_dir}\" \"{stable_input_path}\""progress(0.2, desc="Running Demucs (this can take a minute)...")process = await asyncio.create_subprocess_shell(command,stdout=asyncio.subprocess.PIPE,stderr=asyncio.subprocess.PIPE)stdout, stderr = await process.communicate()if process.returncode != 0:raise gr.Error(f"Demucs failed to run. Error: {stderr.decode()[:500]}")progress(0.8, desc="Locating separated stem files...")stable_filename_base = os.path.basename(stable_input_path).rsplit('.', 1)[0]model_folder_name = next(os.walk(output_dir))[1][0]stems_path = os.path.join(output_dir, model_folder_name, stable_filename_base)if not os.path.exists(stems_path):raise gr.Error(f"Demucs finished, but the output directory was not found!")vocals_path = os.path.join(stems_path, "vocals.wav") if os.path.exists(os.path.join(stems_path, "vocals.wav")) else Nonedrums_path = os.path.join(stems_path, "drums.wav") if os.path.exists(os.path.join(stems_path, "drums.wav")) else Nonebass_path = os.path.join(stems_path, "bass.wav") if os.path.exists(os.path.join(stems_path, "bass.wav")) else Noneother_filename = "no_vocals.wav" if "2 Stems" in stem_choice else "other.wav"other_path = os.path.join(stems_path, other_filename) if os.path.exists(os.path.join(stems_path, other_filename)) else Noneos.remove(stable_input_path)# Detect bars for each stem after separationvocals_bar_times = Nonedrums_bar_times = Nonebass_bar_times = Noneother_bar_times = Noneif vocals_path:vocals_audio_data = sf.read(vocals_path)_, _, vocals_bar_times = detect_bars(vocals_audio_data)if drums_path:drums_audio_data = sf.read(drums_path)_, _, drums_bar_times = detect_bars(drums_audio_data)if bass_path:bass_audio_data = sf.read(bass_path)_, _, bass_bar_times = detect_bars(bass_audio_data)if other_path:other_audio_data = sf.read(other_path)_, _, other_bar_times = detect_bars(other_audio_data)return vocals_path, drums_path, bass_path, other_path, vocals_bar_times, drums_bar_times, bass_bar_times, other_bar_timesexcept Exception as e:print(f"An error occurred: {e}")raise gr.Error(str(e))def visualize_slices(stem_audio_data, sensitivity, progress=gr.Progress(track_tqdm=True)):if stem_audio_data is None:gr.Warning("This stem is empty. Cannot visualize.")return None, None, Nonesample_rate, y_int = stem_audio_datay = librosa.util.buf_to_float(y_int)progress(0.3, desc="Finding transients...")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=sensitivity)onset_times = librosa.frames_to_time(onset_frames, sr=sample_rate)progress(0.7, desc="Generating waveform plot...")fig, ax = plt.subplots(figsize=(10, 3))fig.patch.set_facecolor('#1f2937')4 unmodified linesax.tick_params(colors='gray'); ax.xaxis.label.set_color('gray'); ax.yaxis.label.set_color('gray')ax.set_xlabel("Time (s)"); ax.set_ylabel("Amplitude"); ax.set_title("Detected Slices", color='white')plt.tight_layout()progress(1, desc="Done!")return fig, onset_times, stem_audio_datadef preview_slice(active_stem_audio, onset_times, evt: gr.SelectData):if active_stem_audio is None or onset_times is None: return Nonesample_rate, y = active_stem_audio# Convert click event coordinates to time# evt.index[0] is the x-coordinate of the click in pixels# evt.target[0] is the width of the plot in pixels# len(y) / sample_rate is the total duration of the audio in secondsclicked_time = evt.index[0] * (len(y) / sample_rate) / evt.target[0]start_time = 0end_time = len(y) / sample_rate# Find the closest onset time before the clicked timeonsets_before = onset_times[onset_times <= clicked_time]if len(onsets_before) > 0:start_time = onsets_before[-1]# Find the closest onset time after the clicked timeonsets_after = onset_times[onset_times > clicked_time]if len(onsets_after) > 0:end_time = onsets_after[0]else:# If no onset after the clicked time, slice to the end of the audioend_time = len(y) / sample_ratestart_sample = librosa.time_to_samples(start_time, sr=sample_rate)end_sample = librosa.time_to_samples(end_time, sr=sample_rate)# Ensure start_sample is less than end_sampleif start_sample >= end_sample:# If the click is exactly on or after the last onset, preview a small segment at the endif len(onset_times) > 0:start_sample = librosa.time_to_samples(onset_times[-1], sr=sample_rate)end_sample = len(y)else:# If no onsets detected, slice the whole audiostart_sample = 0end_sample = len(y)sliced_audio = y[start_sample:end_sample]return (sample_rate, sliced_audio)def download_slice(sliced_audio_data):if sliced_audio_data is None:gr.Warning("No slice preview available to download.")return Nonesample_rate, y = sliced_audio_datawith tempfile.NamedTemporaryFile(suffix=".wav", delete=False, prefix="slice_") as tmp_file:sf.write(tmp_file.name, y, sample_rate)global temp_filestemp_files.append(tmp_file.name)return tmp_file.namedef detect_bars(stem_audio_data):if stem_audio_data is None:return None, None, Nonesample_rate, y_int = stem_audio_datay = librosa.util.buf_to_float(y_int)y_mono = librosa.to_mono(y.T) if y.ndim > 1 else y# Estimate tempotempo, beat_frames = librosa.beat.beat_track(y=y_mono, sr=sample_rate)# Convert beat frames to beat timesbeat_times = librosa.frames_to_time(beat_frames, sr=sample_rate)# Calculate bar times (assuming 4 beats per bar)bar_times = beat_times[::4]return tempo, beat_times, bar_timesdef create_loop(stem_audio_data, bar_times, loop_length):if stem_audio_data is None or bar_times is None or len(bar_times) < 2:gr.Warning("Insufficient data to create a loop.")return Nonesample_rate, y_int = stem_audio_datay = librosa.util.buf_to_float(y_int)y_mono = librosa.to_mono(y.T) if y.ndim > 1 else y# Parse loop lengthnum_bars = int(loop_length.split(" ")[0])# Find the start of the first full bar (assuming bar_times[0] is the start of the first bar)# If we want to start from the beginning of the audio, we can use 0 as the start time.# For now, let's assume we start from the first detected bar.start_time = bar_times[0]# Calculate the duration of one barbar_duration = bar_times[1] - bar_times[0] if len(bar_times) > 1 else 0# Calculate the end time for the loopend_time = start_time + (num_bars * bar_duration)# Ensure the end time does not exceed the audio durationaudio_duration = len(y) / sample_rateend_time = min(end_time, audio_duration)# Convert times to samplesstart_sample = librosa.time_to_samples(start_time, sr=sample_rate)end_sample = librosa.time_to_samples(end_time, sr=sample_rate)# Extract the loop segmentlooped_audio = y_mono[start_sample:end_sample]# Save the looped audio to a temporary filewith tempfile.NamedTemporaryFile(suffix=".wav", delete=False, prefix="loop_") as tmp_file:sf.write(tmp_file.name, looped_audio, sample_rate)global temp_filestemp_files.append(tmp_file.name)return tmp_file.namedef cut_all_oneshots(stem_audio_data, onset_times):if stem_audio_data is None or onset_times is None or len(onset_times) < 1:gr.Warning("Insufficient data or onsets detected to cut one-shots.")return Nonesample_rate, y_int = stem_audio_datay = librosa.util.buf_to_float(y_int)y_mono = librosa.to_mono(y.T) if y.ndim > 1 else yoneshot_files = []audio_duration = len(y_mono) / sample_ratefor i in range(len(onset_times)):start_time = onset_times[i]end_time = onset_times[i+1] if i < len(onset_times) - 1 else audio_durationstart_sample = librosa.time_to_samples(start_time, sr=sample_rate)end_sample = librosa.time_to_samples(end_time, sr=sample_rate)# Ensure start_sample is less than end_sample, add a small buffer if necessaryif start_sample >= end_sample:end_sample = start_sample + int(0.01 * sample_rate) # Add 10ms buffer if start is equal to or after endif end_sample > len(y_mono):end_sample = len(y_mono)segment = y_mono[start_sample:end_sample]# Save each segment to a temporary filewith tempfile.NamedTemporaryFile(suffix=f"_{i}.wav", delete=False, prefix="oneshot_") as tmp_file:sf.write(tmp_file.name, segment, sample_rate)oneshot_files.append(tmp_file.name)if not oneshot_files:gr.Warning("No one-shots were successfully cut.")return None# Create a zip archive of the temporary one-shot fileswith tempfile.NamedTemporaryFile(suffix=".zip", delete=False, prefix="oneshots_archive_") as zip_file:with zipfile.ZipFile(zip_file.name, 'w') as zipf:for file_path in oneshot_files:zipf.write(file_path, os.path.basename(file_path))# Add the zip file and individual oneshot files to the temp_files list for cleanupglobal temp_filestemp_files.extend(oneshot_files)temp_files.append(zip_file.name)return zip_file.namewith gr.Blocks(theme=gr.themes.Default(primary_hue="blue", secondary_hue="red")) as demo:gr.Markdown("# 🎵 Loop Architect")onset_times_state = gr.State(value=None)active_stem_state = gr.State(value=None)vocals_bar_times_state = gr.State(value=None)drums_bar_times_state = gr.State(value=None)bass_bar_times_state = gr.State(value=None)other_bar_times_state = gr.State(value=None)with gr.Row():with gr.Column(scale=1):gr.Markdown("### 1. Separate Stems")audio_input = gr.Audio(type="filepath", label="Upload a Track")stem_options = gr.Radio(["4 Stems (Vocals, Drums, Bass, Other)", "2 Stems (Vocals + Instrumental)"], label="Separation Type", value="4 Stems (Vocals, Drums, Bass, Other)")submit_button = gr.Button("Separate Stems")with gr.Column(scale=2):with gr.Accordion("Separated Stems", open=True):with gr.Row():vocals_output = gr.Audio(label="Vocals", scale=2)with gr.Column(scale=1):slice_vocals_btn = gr.Button("Visualize Slices")vocals_loop_length = gr.Dropdown(choices=["4 Bars", "8 Bars", "16 Bars"], label="Loop Length", value="4 Bars")create_vocals_loop_btn = gr.Button("Create Loop")vocals_loop_output = gr.Audio(label="Vocals Loop", visible=False, scale=2)vocals_loop_download_btn = gr.DownloadButton(value="Download Loop", visible=False)with gr.Row():drums_output = gr.Audio(label="Drums", scale=2)with gr.Column(scale=1):slice_drums_btn = gr.Button("Visualize Slices")drums_loop_length = gr.Dropdown(choices=["4 Bars", "8 Bars", "16 Bars"], label="Loop Length", value="4 Bars")create_drums_loop_btn = gr.Button("Create Loop")drums_loop_output = gr.Audio(label="Drums Loop", visible=False, scale=2)drums_loop_download_btn = gr.DownloadButton(value="Download Loop", visible=False)with gr.Row():bass_output = gr.Audio(label="Bass", scale=2)with gr.Column(scale=1):slice_bass_btn = gr.Button("Visualize Slices")bass_loop_length = gr.Dropdown(choices=["4 Bars", "8 Bars", "16 Bars"], label="Loop Length", value="4 Bars")create_bass_loop_btn = gr.Button("Create Loop")bass_loop_output = gr.Audio(label="Bass Loop", visible=False, scale=2)bass_loop_download_btn = gr.DownloadButton(value="Download Loop", visible=False)with gr.Row():other_output = gr.Audio(label="Other / Instrumental", scale=2)with gr.Column(scale=1):slice_other_btn = gr.Button("Visualize Slices")other_loop_length = gr.Dropdown(choices=["4 Bars", "8 Bars", "16 Bars"], label="Loop Length", value="4 Bars")create_other_loop_btn = gr.Button("Create Loop")other_loop_output = gr.Audio(label="Other Loop", visible=False, scale=2)other_loop_download_btn = gr.DownloadButton(value="Download Loop", visible=False)gr.Markdown("### Slice Editor")sensitivity_slider = gr.Slider(minimum=0, maximum=1, value=0.5, label="Onset Sensitivity")slice_plot = gr.Image(label="Click a region on the waveform to preview a slice")preview_player = gr.Audio(label="Slice Preview")download_slice_btn = gr.DownloadButton(value="Download Slice", visible=False)cut_all_oneshots_btn = gr.Button(value="Cut All Oneshots")cut_oneshots_download_btn = gr.DownloadButton(value="Download All Oneshots", visible=False)audio_input.change(fn=cleanup_temp_files)submit_button.click(fn=separate_stems, inputs=[audio_input, stem_options], outputs=[vocals_output, drums_output, bass_output, other_output, vocals_bar_times_state, drums_bar_times_state, bass_bar_times_state, other_bar_times_state])stem_options.change(fn=update_output_visibility, inputs=stem_options, outputs=[vocals_output, drums_output, bass_output, other_output])slice_vocals_btn.click(fn=visualize_slices, inputs=[vocals_output, sensitivity_slider], outputs=[slice_plot, onset_times_state, active_stem_state])slice_drums_btn.click(fn=visualize_slices, inputs=[drums_output, sensitivity_slider], outputs=[slice_plot, onset_times_state, active_stem_state])slice_bass_btn.click(fn=visualize_slices, inputs=[bass_output, sensitivity_slider], outputs=[slice_plot, onset_times_state, active_stem_state])slice_other_btn.click(fn=visualize_slices, inputs=[other_output, sensitivity_slider], outputs=[slice_plot, onset_times_state, active_stem_state])slice_plot.select(fn=preview_slice, inputs=[active_stem_state, onset_times_state], outputs=preview_player).then(lambda: gr.update(visible=True), outputs=download_slice_btn)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])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])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])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])download_slice_btn.click(fn=download_slice, inputs=preview_player, outputs=download_slice_btn)cut_all_oneshots_btn.click(fn=cut_all_oneshots, inputs=[active_stem_state, onset_times_state], outputs=cut_oneshots_download_btn)demo.launch()
app.py+283-56