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() 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) stream = p.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True) if recording_state: stop_button = st.button("Stop Recording") for _ in range(0, RATE // CHUNK * RECORD_TIME): f.writeframes(stream.read(CHUNK)) if stop_button: break recording_info_placeholder.success("Recording Completed\nThese are the results:") st.session_state["recording_done"] = True stream.close() p.terminate()