Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pyaudio
|
3 |
+
import wave
|
4 |
+
import threading
|
5 |
+
import numpy as np
|
6 |
+
|
7 |
+
def record_audio():
|
8 |
+
global RECORDING, frames
|
9 |
+
FORMAT = pyaudio.paInt16
|
10 |
+
CHANNELS = 1
|
11 |
+
RATE = 44100
|
12 |
+
CHUNK = 1024
|
13 |
+
frames = []
|
14 |
+
p = pyaudio.PyAudio()
|
15 |
+
stream = p.open(format=FORMAT,
|
16 |
+
channels=CHANNELS,
|
17 |
+
rate=RATE,
|
18 |
+
input=True,
|
19 |
+
frames_per_buffer=CHUNK)
|
20 |
+
|
21 |
+
while RECORDING:
|
22 |
+
data = stream.read(CHUNK)
|
23 |
+
frames.append(data)
|
24 |
+
|
25 |
+
stream.stop_stream()
|
26 |
+
stream.close()
|
27 |
+
p.terminate()
|
28 |
+
|
29 |
+
def main():
|
30 |
+
global RECORDING, frames
|
31 |
+
st.title('Audio Recorder')
|
32 |
+
RECORDING = False
|
33 |
+
|
34 |
+
if st.button('Start Recording'):
|
35 |
+
global recording_thread
|
36 |
+
RECORDING = True
|
37 |
+
frames = [] # Clear previous recording data
|
38 |
+
recording_thread = threading.Thread(target=record_audio)
|
39 |
+
recording_thread.start()
|
40 |
+
st.write('Recording...')
|
41 |
+
|
42 |
+
if st.button('Stop Recording'):
|
43 |
+
global recorded_audio
|
44 |
+
RECORDING = False
|
45 |
+
recording_thread.join()
|
46 |
+
recorded_audio = np.frombuffer(b''.join(frames), dtype=np.int16)
|
47 |
+
st.audio(recorded_audio, format='audio/wav')
|
48 |
+
|
49 |
+
# Use 'recorded_audio' variable in your program as needed
|
50 |
+
# For example, you can pass it to a function for further processing
|
51 |
+
# YourFunction(recorded_audio)
|
52 |
+
|
53 |
+
if __name__ == '__main__':
|
54 |
+
main()
|