Rhueue commited on
Commit
609febe
·
1 Parent(s): 136ed2d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -11
app.py CHANGED
@@ -1,8 +1,10 @@
1
  import streamlit as st
 
2
  import soundfile as sf
3
  import io
4
  import numpy as np
5
  from pydub import AudioSegment
 
6
 
7
  # Define a Streamlit app
8
  st.title("Audio Processing App")
@@ -13,26 +15,30 @@ uploaded_audio = st.file_uploader("Upload an audio file", type=["mp3", "wav", "o
13
  if uploaded_audio is not None:
14
  audio_bytes = uploaded_audio.read()
15
 
16
- # Convert audio file to numpy array using soundfile
17
  audio, sample_rate = sf.read(io.BytesIO(audio_bytes))
18
 
19
- # Convert the audio to 16-bit to avoid unsupported 24-bit formats
20
- audio = (audio * 32767).astype(np.int16)
 
21
 
22
- # Create an AudioSegment from the audio data
23
- audio_segment = AudioSegment(
24
- audio.tobytes(),
25
  frame_rate=sample_rate,
26
- sample_width=2,
27
  channels=1
28
  )
29
 
30
- # Slow down the audio based on user's input speed factor
31
- st.write("Slowing down audio...")
32
- slowed_audio = audio_segment.speedup(playback_speed=0.7)
 
 
 
33
 
34
  # Provide a link to download the processed audio
35
- st.audio(slowed_audio.export(format="wav").read(), format="audio/wav")
36
 
37
  # Run the Streamlit app
38
  if __name__ == "__main__":
 
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")
 
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__":