File size: 2,445 Bytes
782d9c8 330b230 782d9c8 cb6e2f4 782d9c8 330b230 782d9c8 330b230 782d9c8 cb6e2f4 782d9c8 cb6e2f4 782d9c8 |
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 66 67 68 |
import streamlit as st
from pyaudio import paInt16, PyAudio
import wave
def record_Audio(filename, duration):
"""
A audio-recording helping function Using PyAudio
"""
if not filename:
raise ValueError("Filename not specified. Please provide a filename!")
CHUNK = 1024
FORMAT = paInt16
CHANNELS = 1
RATE = 16000
RECORD_TIME = duration
recording_state = st.session_state.get("recording_state", False)
recording_info_placeholder = st.empty()
stop_button_placeholder = st.empty()
if recording_state:
recording_info_placeholder.info("Recording... ")
with wave.open(filename, 'wb') as f:
p = PyAudio()
f.setnchannels(CHANNELS)
f.setsampwidth(p.get_sample_size(FORMAT))
f.setframerate(RATE)
default_output_device_index = p.get_default_output_device_info()['index']
defualt_input_device_index = p.get_default_input_device_info()['index']
print("Input - ", p.get_default_input_device_info(), defualt_input_device_index)
print(default_output_device_index, default_output_device_index)
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
output=True,
input_device_index=defualt_input_device_index,
output_device_index=default_output_device_index)
if recording_state:
stop_button = st.button("Stop Recording")
else:
stop_button_placeholder.empty()
for _ in range(0, RATE // CHUNK * RECORD_TIME):
f.writeframes(stream.read(CHUNK))
if stop_button:
stop_button = st.empty()
st.session_state["recording_done"] = True
recording_info_placeholder.info("Recording Stopped")
stream.close()
p.terminate()
recording_info_placeholder.success("Recording Completed\nThese are the results:")
st.session_state["recording_done"] = True
stream.close()
p.terminate()
|