Spaces:
Running
Running
import streamlit as st | |
import noisereduce as nr | |
import soundfile as sf | |
from pedalboard import NoiseGate, Compressor | |
import numpy as np | |
from io import BytesIO | |
# Function to remove background noise and process audio | |
def process_audio(input_file): | |
# Load the audio file | |
data, sr = sf.read(input_file) | |
# Reduce stationary noise using noisereduce | |
reduced_noise = nr.reduce_noise(y=data, sr=sr) | |
# Create a noise gate to further reduce noise using pedalboard | |
noise_gate = NoiseGate(threshold=0.02, attack=10, release=100) | |
# Create a compressor to enhance the audio | |
compressor = Compressor(threshold_db=-25, ratio=3, | |
attack_ms=10, release_ms=200) | |
# Create a pedalboard and add the noise gate and compressor to it | |
board = noise_gate >> compressor | |
# Process the audio through the pedalboard | |
processed_audio = board(np.array([reduced_noise])) | |
return processed_audio, sr | |
def main(): | |
st.title("Background Noise Reduction") | |
# Upload audio file | |
uploaded_file = st.file_uploader("Upload audio file", type=["wav"]) | |
if uploaded_file is not None: | |
# Process the audio | |
st.write("Processing audio...") | |
processed_audio, sr = process_audio(uploaded_file) | |
# Display processing progress | |
with st.spinner('Processing audio...'): | |
processed_audio_bytes = BytesIO() | |
sf.write(processed_audio_bytes, | |
processed_audio[0], sr, format='wav') | |
st.write("Audio processed successfully!") | |
# Allow user to play original and edited audio | |
st.subheader("Play Audio") | |
st.audio(uploaded_file, format='audio/wav', | |
start_time=0, caption='Original Audio') | |
st.audio(processed_audio_bytes, format='audio/wav', | |
start_time=0, caption='Processed Audio') | |
# Allow user to download processed audio | |
st.subheader("Download Processed Audio") | |
st.download_button("Download", processed_audio_bytes.getvalue( | |
), file_name="processed_audio.wav", mime='audio/wav') | |
if __name__ == "__main__": | |
main() | |