alex buz commited on
Commit
b994852
·
1 Parent(s): de0e969
Files changed (3) hide show
  1. app.py +52 -13
  2. push.bat +1 -1
  3. requirements.txt +5 -1
app.py CHANGED
@@ -1,20 +1,59 @@
1
  import gradio as gr
 
 
 
 
 
 
2
 
3
- def save_audio(audio):
4
- """
5
- Function to save the recorded audio.
6
- For demonstration, this function just returns the audio file.
7
- """
8
- return audio
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
- # Create a Gradio interface for recording audio
11
  iface = gr.Interface(
12
- fn=save_audio,
13
- inputs=gr.inputs.Audio(source="microphone", type="filepath"),
14
- outputs="audio",
15
- title="Simple Voice Recorder",
16
- description="Record your voice and play it back!"
17
  )
18
 
19
- # Run the Gradio app
20
  iface.launch()
 
1
  import gradio as gr
2
+ import sounddevice as sd
3
+ import numpy as np
4
+ import scipy.io.wavfile as wav
5
+ from io import BytesIO
6
+ import threading
7
+ import time
8
 
9
+ class Recorder:
10
+ def __init__(self):
11
+ self.recording = False
12
+ self.audio_data = None
13
+
14
+ def toggle_recording(self, _):
15
+ if self.recording:
16
+ self.recording = False
17
+ else:
18
+ self.recording = True
19
+ self.record()
20
+
21
+ def record(self):
22
+ def record_thread():
23
+ sr = 16000 # Sample rate
24
+ duration = 10 # Max duration in seconds
25
+ myrecording = sd.rec(int(duration * sr), samplerate=sr, channels=2, dtype='float64')
26
+ sd.wait() # Wait until recording is finished
27
+ self.audio_data = (sr, myrecording)
28
+
29
+ if not self.recording:
30
+ return
31
+
32
+ thread = threading.Thread(target=record_thread)
33
+ thread.start()
34
+
35
+ def get_audio(self):
36
+ if self.audio_data is not None:
37
+ sr, audio = self.audio_data
38
+ wav_file = BytesIO()
39
+ wav.write(wav_file, sr, np.int16(audio * 32767))
40
+ wav_file.seek(0)
41
+ return wav_file
42
+ return None
43
+
44
+ # Create an instance of the recorder
45
+ recorder = Recorder()
46
+
47
+ def update_output(_):
48
+ time.sleep(0.5) # Small delay to allow recording to stop/start
49
+ return recorder.get_audio()
50
 
 
51
  iface = gr.Interface(
52
+ fn=update_output,
53
+ inputs=[gr.Button("Toggle Recording")],
54
+ outputs=gr.Audio(type="file", label="Recorded Audio"),
55
+ title="Voice Recorder",
56
+ description="Click the button to start or stop recording."
57
  )
58
 
 
59
  iface.launch()
push.bat CHANGED
@@ -1,3 +1,3 @@
1
  git add .
2
  git commit -m "%1"
3
- git push -u origin voice_recorder
 
1
  git add .
2
  git commit -m "%1"
3
+ git push
requirements.txt CHANGED
@@ -1 +1,5 @@
1
- huggingface_hub==0.22.2
 
 
 
 
 
1
+ huggingface_hub==0.22.2
2
+ gradio
3
+ sounddevice
4
+ numpy
5
+ scipy