Vivek6041/Sound_Segregator
3
1import streamlit as st2import os3import soundfile as sf4import librosa5 6# Title of the app7st.title("Sound Segregator")8 9# File uploader for multiple files10uploaded_files = st.file_uploader("Choose audio files", type=["wav", "mp3", "flac"], accept_multiple_files=True)11 12# Output directory13output_dir = "output"14os.makedirs(output_dir, exist_ok=True)15 16# Output file format options17file_format = st.selectbox("Choose output file format", ["wav", "mp3"])18 19# Advanced settings on main screen20st.header("Advanced Settings")21fft_size = st.slider("FFT Size", 256, 8192, 2048)22hop_length = st.slider("Hop Length", 64, 1024, 512)23 24# Function to process a single file25def process_file(uploaded_file):26 # Save the uploaded file temporarily27 input_path = os.path.join(output_dir, uploaded_file.name)28 with open(input_path, "wb") as f:29 f.write(uploaded_file.getbuffer())30 31 # Load audio file32 y, sr = librosa.load(input_path, sr=None)33 34 # Segregate audio with advanced settings35 harmonic, percussive = librosa.effects.hpss(y, margin=(1.0, 1.0), kernel_size=fft_size, hop_length=hop_length)36 37 # Display input file name38 st.subheader(f"Results for {uploaded_file.name}")39 40 # Save and display download links for each component41 component_names = ['harmonic', 'percussive']42 components = [harmonic, percussive]43 for i, component in enumerate(components):44 output_filename = os.path.join(output_dir, f"{os.path.splitext(uploaded_file.name)[0]}_{component_names[i]}.{file_format}")45 sf.write(output_filename, component, sr, format=file_format)46 st.audio(output_filename, format=f"audio/{file_format}")47 st.download_button(48 label=f"Download {component_names[i]}",49 data=open(output_filename, "rb").read(),50 file_name=output_filename51 )52 53# Segregation button54if st.button("Start Segregation"):55 if uploaded_files is not None:56 # Process each uploaded file57 for uploaded_file in uploaded_files:58 process_file(uploaded_file)59 