File size: 1,441 Bytes
0b9d85f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()