-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathutil.py
69 lines (55 loc) · 2.02 KB
/
util.py
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
import curses
import time
import numpy as np
import numpy.typing as npt
import sounddevice as sd
def _record_audio(screen: curses.window) -> npt.NDArray[np.float32]:
screen.nodelay(True) # Non-blocking input
screen.clear()
screen.addstr(
"Press <spacebar> to start recording. Press <spacebar> again to stop recording.\n"
)
screen.refresh()
recording = False
audio_buffer: list[npt.NDArray[np.float32]] = []
def _audio_callback(indata, frames, time_info, status):
if status:
screen.addstr(f"Status: {status}\n")
screen.refresh()
if recording:
audio_buffer.append(indata.copy())
# Open the audio stream with the callback.
with sd.InputStream(samplerate=24000, channels=1, dtype=np.float32, callback=_audio_callback):
while True:
key = screen.getch()
if key == ord(" "):
recording = not recording
if recording:
screen.addstr("Recording started...\n")
else:
screen.addstr("Recording stopped.\n")
break
screen.refresh()
time.sleep(0.01)
# Combine recorded audio chunks.
if audio_buffer:
audio_data = np.concatenate(audio_buffer, axis=0)
else:
audio_data = np.empty((0,), dtype=np.float32)
return audio_data
def record_audio():
# Using curses to record audio in a way that:
# - doesn't require accessibility permissions on macos
# - doesn't block the terminal
audio_data = curses.wrapper(_record_audio)
return audio_data
class AudioPlayer:
def __enter__(self):
self.stream = sd.OutputStream(samplerate=24000, channels=1, dtype=np.int16)
self.stream.start()
return self
def __exit__(self, exc_type, exc_value, traceback):
self.stream.stop() # wait for the stream to finish
self.stream.close()
def add_audio(self, audio_data: npt.NDArray[np.int16]):
self.stream.write(audio_data)