File size: 2,143 Bytes
0acc83a
41fb2f1
 
 
 
 
0acc83a
41fb2f1
0acc83a
41fb2f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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()