|
import streamlit as st |
|
import noisereduce as nr |
|
import soundfile as sf |
|
import io |
|
import numpy as np |
|
from pydub import AudioSegment |
|
from pydub.silence import split_on_silence, detect_silence |
|
|
|
|
|
st.title("Audio Processing App") |
|
|
|
|
|
uploaded_audio = st.file_uploader("Upload an audio file", type=["mp3", "wav", "ogg", "flac", "wma", "m4a"]) |
|
|
|
if uploaded_audio is not None: |
|
audio_bytes = uploaded_audio.read() |
|
|
|
|
|
audio, sample_rate = sf.read(io.BytesIO(audio_bytes)) |
|
|
|
|
|
st.write("Applying noise reduction...") |
|
reduced_audio_data = nr.reduce_noise(y=audio, sr=sample_rate) |
|
|
|
|
|
reduced_audio = AudioSegment( |
|
reduced_audio_data.tobytes(), |
|
frame_rate=sample_rate, |
|
sample_width=reduced_audio_data.dtype.itemsize, |
|
channels=1 |
|
) |
|
|
|
|
|
st.write("Inserting small silences...") |
|
silence_segments = detect_silence(reduced_audio, min_silence_len=100, silence_thresh=-36) |
|
silenced_audio = AudioSegment.empty() |
|
for start, end in silence_segments: |
|
silenced_audio += reduced_audio[start:end] |
|
|
|
|
|
st.audio(silenced_audio.export(format="wav").read(), format="audio/wav") |
|
|
|
|
|
if __name__ == "__main__": |
|
st.write("Upload an audio file to process.") |
|
|