import streamlit as st import pyaudio import wave import threading import numpy as np def record_audio(): global RECORDING, frames FORMAT = pyaudio.paInt16 CHANNELS = 1 RATE = 44100 CHUNK = 1024 frames = [] p = pyaudio.PyAudio() stream = p.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK) while RECORDING: data = stream.read(CHUNK) frames.append(data) stream.stop_stream() stream.close() p.terminate() def main(): global RECORDING, frames st.title('Audio Recorder') RECORDING = False if st.button('Start Recording'): global recording_thread RECORDING = True frames = [] # Clear previous recording data recording_thread = threading.Thread(target=record_audio) recording_thread.start() st.write('Recording...') if st.button('Stop Recording'): global recorded_audio RECORDING = False recording_thread.join() recorded_audio = np.frombuffer(b''.join(frames), dtype=np.int16) st.audio(recorded_audio, format='audio/wav') # Use 'recorded_audio' variable in your program as needed # For example, you can pass it to a function for further processing # YourFunction(recorded_audio) if __name__ == '__main__': main()