Rhueue commited on
Commit
cb6c13a
1 Parent(s): 609febe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -18
app.py CHANGED
@@ -1,10 +1,10 @@
1
  import streamlit as st
2
- import noisereduce as nr
3
  import soundfile as sf
4
  import io
5
  import numpy as np
 
 
6
  from pydub import AudioSegment
7
- from pydub.silence import split_on_silence, detect_silence
8
 
9
  # Define a Streamlit app
10
  st.title("Audio Processing App")
@@ -12,33 +12,52 @@ st.title("Audio Processing App")
12
  # Upload the input audio file
13
  uploaded_audio = st.file_uploader("Upload an audio file", type=["mp3", "wav", "ogg", "flac", "wma", "m4a"])
14
 
 
 
 
15
  if uploaded_audio is not None:
16
  audio_bytes = uploaded_audio.read()
17
 
18
- # Convert audio file to numpy array
19
  audio, sample_rate = sf.read(io.BytesIO(audio_bytes))
20
 
21
- # Apply noise reduction
22
- st.write("Applying noise reduction...")
23
- reduced_audio_data = nr.reduce_noise(y=audio, sr=sample_rate)
24
-
25
- # Create an AudioSegment from the reduced audio data
26
- reduced_audio = AudioSegment(
27
- reduced_audio_data.tobytes(),
28
  frame_rate=sample_rate,
29
- sample_width=reduced_audio_data.dtype.itemsize,
30
  channels=1
31
  )
32
 
33
- # Split audio on silences
34
- st.write("Inserting small silences...")
35
- silence_segments = detect_silence(reduced_audio, min_silence_len=100, silence_thresh=-36)
36
- silenced_audio = AudioSegment.empty()
37
- for start, end in silence_segments:
38
- silenced_audio += reduced_audio[start:end]
39
 
40
  # Provide a link to download the processed audio
41
- st.audio(silenced_audio.export(format="wav").read(), format="audio/wav")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
  # Run the Streamlit app
44
  if __name__ == "__main__":
 
1
  import streamlit as st
 
2
  import soundfile as sf
3
  import io
4
  import numpy as np
5
+ import pyaudio
6
+ import wave
7
  from pydub import AudioSegment
 
8
 
9
  # Define a Streamlit app
10
  st.title("Audio Processing App")
 
12
  # Upload the input audio file
13
  uploaded_audio = st.file_uploader("Upload an audio file", type=["mp3", "wav", "ogg", "flac", "wma", "m4a"])
14
 
15
+ # Speed factor input
16
+ speed_factor = st.slider("Playback Speed", min_value=0.1, max_value=2.0, step=0.1, value=1.0)
17
+
18
  if uploaded_audio is not None:
19
  audio_bytes = uploaded_audio.read()
20
 
21
+ # Convert audio file to numpy array using soundfile
22
  audio, sample_rate = sf.read(io.BytesIO(audio_bytes))
23
 
24
+ # Create an AudioSegment from the audio data
25
+ audio_segment = AudioSegment(
26
+ audio.tobytes(),
 
 
 
 
27
  frame_rate=sample_rate,
28
+ sample_width=2,
29
  channels=1
30
  )
31
 
32
+ # Slow down the audio based on user's input speed factor
33
+ st.write(f"Slowing down audio to {speed_factor}x speed...")
34
+ slowed_audio = audio_segment.speedup(playback_speed=1/speed_factor)
 
 
 
35
 
36
  # Provide a link to download the processed audio
37
+ st.audio(slowed_audio.export(format="wav").read(), format="audio/wav")
38
+
39
+ # Play the modified audio
40
+ st.audio(slowed_audio.export(format="mp3").read(), format="audio/mp3")
41
+
42
+ # PyAudio code for capturing and playing audio
43
+ p = pyaudio.PyAudio()
44
+ stream_out = p.open(format=pyaudio.paInt16,
45
+ channels=1,
46
+ rate=int(sample_rate * speed_factor),
47
+ output=True)
48
+
49
+ stream_in = p.open(format=pyaudio.paInt16,
50
+ channels=1,
51
+ rate=sample_rate,
52
+ input=True)
53
+
54
+ for _ in range(1):
55
+ data = stream_in.read(int(len(slowed_audio) / sample_rate * speed_factor) * 2)
56
+ stream_out.write(data)
57
+
58
+ stream_out.stop_stream()
59
+ stream_out.close()
60
+ p.terminate()
61
 
62
  # Run the Streamlit app
63
  if __name__ == "__main__":