-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecord.py
More file actions
93 lines (74 loc) · 2.78 KB
/
record.py
File metadata and controls
93 lines (74 loc) · 2.78 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python3
"""
Record from the Mac’s default microphone while the space bar is held down
and save the result to capture.wav.
"""
import numpy as np
import sounddevice as sd
import soundfile as sf
from pynput import keyboard
import mlx_whisper
SAMPLERATE = 44_100 # 44.1 kHz
CHANNELS = 1 # mono
_stream = None # active audio stream
_buffers = [] # list of NumPy chunks
_is_recording = False
global_text = "snuh kabob"
# ------------------------------------------------------------ audio helpers
def _start_recording() -> None:
"""Open an input stream and push incoming audio into _buffers."""
global _stream, _buffers
_buffers = [] # reset buffer for every take
def _callback(indata, frames, time, status):
if status:
print(status)
_buffers.append(indata.copy())
_stream = sd.InputStream(samplerate=SAMPLERATE,
channels=CHANNELS,
callback=_callback)
_stream.start()
print("Recording… (hold SPACE)")
def _stop_recording() -> None:
"""Close the input stream and write buffered audio to capture.wav."""
global _stream
if _stream:
_stream.stop()
_stream.close()
_stream = None
text = ""
if _buffers: # anything captured?
audio = np.concatenate(_buffers, axis=0)
sf.write('capture.wav', audio, SAMPLERATE)
seconds = len(audio) / SAMPLERATE
print(f"Saved {seconds:0.2f} s to capture.wav")
# convert the audio to text here
text=mlx_whisper.transcribe('capture.wav')['text']
else:
print("No audio captured.")
return text
# ------------------------------------------------------------ keyboard hooks
def _on_press(key):
global _is_recording
if key == keyboard.Key.space and not _is_recording:
_is_recording = True
_start_recording()
def _on_release(key):
global global_text
# always return false to stop listener
global _is_recording
if key == keyboard.Key.space and _is_recording:
_is_recording = False
global_text = _stop_recording()
print("global text is ", global_text)
return False # to stop the listener
# optional: allow exiting with Esc
if key == keyboard.Key.esc:
return False # stops the listener / exits program
# ------------------------------------------------------------ main entry
def offline_main() -> None:
print("Hold SPACE to record, release to stop. Esc quits.")
with keyboard.Listener(on_press=_on_press,
on_release=_on_release) as listener:
listener.join()
if __name__ == "__main__":
offline_main()#!/usr/bin/env python3