akshansh36 commited on
Commit
6b64681
·
verified ·
1 Parent(s): 7e493ec

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -35
app.py CHANGED
@@ -1,44 +1,27 @@
1
  import gradio as gr
2
  import numpy as np
3
- import sounddevice as sd
4
- import threading
5
 
6
- # Global variable to stop the streaming
7
- stop_streaming = False
 
 
 
 
 
 
 
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
- start_button = gr.Button("Start Recording")
37
- stop_button = gr.Button("Stop Recording")
38
- status = gr.Textbox()
 
 
 
 
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()