|
import streamlit as st |
|
import soundfile as sf |
|
import io |
|
import numpy as np |
|
import pyaudio |
|
import wave |
|
from pydub import AudioSegment |
|
|
|
|
|
st.title("Audio Processing App") |
|
|
|
|
|
uploaded_audio = st.file_uploader("Upload an audio file", type=["mp3", "wav", "ogg", "flac", "wma", "m4a"]) |
|
|
|
|
|
speed_factor = st.slider("Playback Speed", min_value=0.1, max_value=2.0, step=0.1, value=1.0) |
|
|
|
if uploaded_audio is not None: |
|
audio_bytes = uploaded_audio.read() |
|
|
|
|
|
audio, sample_rate = sf.read(io.BytesIO(audio_bytes)) |
|
|
|
|
|
audio_segment = AudioSegment( |
|
audio.tobytes(), |
|
frame_rate=sample_rate, |
|
sample_width=2, |
|
channels=1 |
|
) |
|
|
|
|
|
st.write(f"Slowing down audio to {speed_factor}x speed...") |
|
slowed_audio = audio_segment.speedup(playback_speed=1/speed_factor) |
|
|
|
|
|
st.audio(slowed_audio.export(format="wav").read(), format="audio/wav") |
|
|
|
|
|
st.audio(slowed_audio.export(format="mp3").read(), format="audio/mp3") |
|
|
|
|
|
p = pyaudio.PyAudio() |
|
stream_out = p.open(format=pyaudio.paInt16, |
|
channels=1, |
|
rate=int(sample_rate * speed_factor), |
|
output=True) |
|
|
|
stream_in = p.open(format=pyaudio.paInt16, |
|
channels=1, |
|
rate=sample_rate, |
|
input=True) |
|
|
|
for _ in range(1): |
|
data = stream_in.read(int(len(slowed_audio) / sample_rate * speed_factor) * 2) |
|
stream_out.write(data) |
|
|
|
stream_out.stop_stream() |
|
stream_out.close() |
|
p.terminate() |
|
|
|
|
|
if __name__ == "__main__": |
|
st.write("Upload an audio file to process.") |
|
|