Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,44 +1,27 @@
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
-
import
|
4 |
-
import threading
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
-
def callback(indata, frames, time, status):
|
10 |
-
# This function is called for each audio block
|
11 |
-
if status:
|
12 |
-
print(status)
|
13 |
-
sd.play(indata, samplerate=44100) # Play the audio data in real-time
|
14 |
-
|
15 |
-
def start_stream():
|
16 |
-
global stop_streaming
|
17 |
-
stop_streaming = False
|
18 |
-
with sd.InputStream(callback=callback):
|
19 |
-
while not stop_streaming:
|
20 |
-
sd.sleep(100) # Sleep for a short duration to simulate real-time processing
|
21 |
-
|
22 |
-
def stop_stream():
|
23 |
-
global stop_streaming
|
24 |
-
stop_streaming = True
|
25 |
-
|
26 |
-
def start_recording():
|
27 |
-
thread = threading.Thread(target=start_stream)
|
28 |
-
thread.start()
|
29 |
-
return "Recording started..."
|
30 |
-
|
31 |
-
def stop_recording():
|
32 |
-
stop_stream()
|
33 |
-
return "Recording stopped."
|
34 |
|
35 |
with gr.Blocks() as demo:
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
|
|
39 |
|
40 |
-
start_button.click(start_recording, outputs=status)
|
41 |
-
stop_button.click(stop_recording, outputs=status)
|
42 |
|
43 |
if __name__ == "__main__":
|
44 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
+
import time
|
|
|
4 |
|
5 |
+
def add_to_stream(audio, instream):
|
6 |
+
time.sleep(1)
|
7 |
+
if audio is None:
|
8 |
+
return gr.update(), instream
|
9 |
+
if instream is None:
|
10 |
+
ret = audio
|
11 |
+
else:
|
12 |
+
ret = (audio[0], np.concatenate((instream[1], audio[1])))
|
13 |
+
return ret, ret
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
with gr.Blocks() as demo:
|
17 |
+
inp = gr.Audio(source="microphone")
|
18 |
+
out = gr.Audio()
|
19 |
+
stream = gr.State()
|
20 |
+
clear = gr.Button("Clear")
|
21 |
+
|
22 |
+
inp.stream(add_to_stream, [inp, stream], [out, stream])
|
23 |
+
clear.click(lambda: [None, None, None], None, [inp, out, stream])
|
24 |
|
|
|
|
|
25 |
|
26 |
if __name__ == "__main__":
|
27 |
+
demo.launch()
|