Tonic commited on
Commit
31459da
·
1 Parent(s): 94c8f06

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -13
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import streamlit as st
2
- import pyaudio
3
- import wave
4
  import numpy as np
 
5
  import whisper
6
  import os
7
  import streamlit.components.v1 as components
@@ -37,27 +37,28 @@ record_audio = st.checkbox("Record Audio")
37
  if record_audio:
38
  audio_frames = []
39
 
40
- p = pyaudio.PyAudio()
41
- stream = p.open(format=pyaudio.paInt16, channels=1, rate=44100, input=True, frames_per_buffer=1024)
 
 
 
42
 
43
- st.text("Recording audio. Click 'Stop Recording' when finished.")
44
- while st.button("Stop Recording"):
45
- data = stream.read(1024)
46
- audio_frames.append(data)
47
 
48
- stream.stop_stream()
49
- stream.close()
50
- p.terminate()
51
 
52
  st.success("Recording stopped")
53
 
54
  if audio_frames:
55
- audio_data = b''.join(audio_frames)
56
  with wave.open("recorded_audio.wav", "wb") as wf:
57
  wf.setnchannels(1)
58
  wf.setsampwidth(2)
59
  wf.setframerate(44100)
60
- wf.writeframes(audio_data)
61
 
62
  # Moved the submit_button check here
63
  if 'submit_button' in st.session_state:
 
1
  import streamlit as st
2
+ import sounddevice as sd
 
3
  import numpy as np
4
+ import wave
5
  import whisper
6
  import os
7
  import streamlit.components.v1 as components
 
37
  if record_audio:
38
  audio_frames = []
39
 
40
+ def audio_callback(indata, frames, time, status):
41
+ if status:
42
+ print(status, flush=True)
43
+ if any(indata):
44
+ audio_frames.append(indata.copy())
45
 
46
+ if st.button("Stop Recording"):
47
+ sd.stop()
 
 
48
 
49
+ with st.spinner("Recording..."):
50
+ with sd.InputStream(callback=audio_callback):
51
+ st.text("Recording audio. Click 'Stop Recording' when finished.")
52
 
53
  st.success("Recording stopped")
54
 
55
  if audio_frames:
56
+ audio_data = np.concatenate(audio_frames, axis=0)
57
  with wave.open("recorded_audio.wav", "wb") as wf:
58
  wf.setnchannels(1)
59
  wf.setsampwidth(2)
60
  wf.setframerate(44100)
61
+ wf.writeframes(audio_data.tobytes())
62
 
63
  # Moved the submit_button check here
64
  if 'submit_button' in st.session_state: