File size: 1,991 Bytes
1b95475
6447be5
136ed2d
ff25f09
cb6c13a
 
6447be5
1b95475
 
6447be5
1b95475
 
 
 
cb6c13a
 
 
1b95475
 
 
cb6c13a
6447be5
 
cb6c13a
 
 
6447be5
cb6c13a
6447be5
 
1b95475
cb6c13a
 
 
1b95475
6447be5
cb6c13a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b95475
 
 
ff25f09
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
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.")