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