Trim / app.py
Rhueue's picture
Update app.py
cb6c13a
raw
history blame contribute delete
No virus
1.99 kB
import streamlit as st
import soundfile as sf
import io
import numpy as np
import pyaudio
import wave
from pydub import AudioSegment
# Define a Streamlit app
st.title("Audio Processing App")
# Upload the input audio file
uploaded_audio = st.file_uploader("Upload an audio file", type=["mp3", "wav", "ogg", "flac", "wma", "m4a"])
# Speed factor input
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()
# Convert audio file to numpy array using soundfile
audio, sample_rate = sf.read(io.BytesIO(audio_bytes))
# Create an AudioSegment from the audio data
audio_segment = AudioSegment(
audio.tobytes(),
frame_rate=sample_rate,
sample_width=2,
channels=1
)
# Slow down the audio based on user's input speed factor
st.write(f"Slowing down audio to {speed_factor}x speed...")
slowed_audio = audio_segment.speedup(playback_speed=1/speed_factor)
# Provide a link to download the processed audio
st.audio(slowed_audio.export(format="wav").read(), format="audio/wav")
# Play the modified audio
st.audio(slowed_audio.export(format="mp3").read(), format="audio/mp3")
# PyAudio code for capturing and playing audio
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()
# Run the Streamlit app
if __name__ == "__main__":
st.write("Upload an audio file to process.")