-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranscribe_microphone.py
366 lines (307 loc) · 11.5 KB
/
transcribe_microphone.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import sounddevice as sd
import numpy as np
from faster_whisper import WhisperModel
import threading
import queue
from pynput import keyboard
import logging
import pulsectl
import time
import curses
import json
from dataclasses import dataclass
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
try:
with open("available_models_languages.json") as f:
config = json.load(f)
except (FileNotFoundError, json.JSONDecodeError) as e:
logger.error(f"Configuration error: {e}")
raise
accepted_models = config.get("accepted_models", [])
accepted_languages = config.get("accepted_languages", [])
accepted_compute_types = ["float16", "int8"]
accepted_devices = ["cpu", "cuda"]
SETTINGS_FILE = "transcriber_settings.json"
ENGLISH_ONLY_MODELS = {
"tiny.en",
"small.en",
"base.en",
"medium.en",
"distil-medium.en",
"distil-small.en",
}
@dataclass
class Settings:
device_name: str
model_size: str
compute_type: str
device: str
language: str
def save_settings(settings: dict):
try:
with open(SETTINGS_FILE, "w") as f:
json.dump(settings, f)
except IOError as e:
logger.error(f"Failed to save settings: {e}")
def load_settings() -> Settings | None:
try:
with open(SETTINGS_FILE) as f:
data = json.load(f)
return Settings(**data)
except (FileNotFoundError, json.JSONDecodeError) as e:
logger.warning(f"Failed to load settings: {e}")
return None
def curses_menu(stdscr, title: str, options: list, message: str = "", initial_idx=0):
current_row = initial_idx
h, w = stdscr.getmaxyx()
def draw_menu():
stdscr.clear()
max_visible = min(h - 2, len(options))
start = max(0, current_row - (max_visible // 2))
end = min(start + max_visible, len(options))
if message:
lines = message.split("\n")
for i, line in enumerate(lines):
x = w // 2 - len(line) // 2
y = h // 4 - len(lines) + i
stdscr.addstr(y, x, line[: w - 1])
for i in range(start, end):
text = options[i]
x = w // 2 - len(text) // 2
y = h // 2 - (max_visible // 2) + (i - start)
if i == current_row:
stdscr.attron(curses.color_pair(1))
stdscr.addstr(y, x, text[: w - 1])
stdscr.attroff(curses.color_pair(1))
else:
stdscr.addstr(y, x, text[: w - 1])
if max_visible < len(options):
ratio = (current_row + 1) / len(options)
y = h - 2
x_start = w // 4
length = w // 2
stdscr.addstr(y, x_start, "[")
end_pos = int(ratio * (length - 2)) + x_start + 1
stdscr.addstr(y, x_start + 1, " " * (length - 2))
stdscr.addstr(y, end_pos, "█")
stdscr.addstr(y, x_start + length - 1, "]")
stdscr.refresh()
curses.curs_set(0)
curses.start_color()
curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE)
draw_menu()
while True:
key = stdscr.getch()
if key == curses.KEY_UP and current_row > 0:
current_row -= 1
elif key == curses.KEY_DOWN and current_row < len(options) - 1:
current_row += 1
elif key in [curses.KEY_ENTER, 10, 13]:
return options[current_row]
elif key == 27:
return None
if curses.is_term_resized(h, w):
h, w = stdscr.getmaxyx()
draw_menu()
def get_initial_choice(stdscr):
options = ["Use Last Settings", "Choose New Settings"]
selected = curses_menu(stdscr, "", options)
return selected
class MicrophoneTranscriber:
def __init__(self, settings: Settings):
self.model = WhisperModel(
settings.model_size,
device=settings.device,
compute_type=settings.compute_type,
)
self.text_queue = queue.Queue()
self.stop_event = threading.Event()
self.is_recording = False
self.sample_rate = 16000
self.device_name = settings.device_name
self.audio_buffer = []
self.buffer_time = 40
self.max_buffer_samples = self.sample_rate * self.buffer_time
self.segment_number = 0
self.keyboard_controller = keyboard.Controller()
self.language = settings.language
def set_default_audio_source(self):
with pulsectl.Pulse("set-default-source") as pulse:
for source in pulse.source_list():
if source.name == self.device_name:
pulse.source_default_set(source)
logger.info(f"Default source set to: {source.name}")
return
logger.warning(f"Source '{self.device_name}' not found")
def audio_callback(self, indata, frames, time, status):
if status:
logger.warning(f"Status: {status}")
audio_data = (
np.mean(indata, axis=1)
if indata.ndim > 1 and indata.shape[1] == 2
else indata.flatten()
).astype(np.float32)
if not np.isclose(audio_data.max(), 0):
audio_data /= np.abs(audio_data).max()
self.audio_buffer.extend(audio_data)
if len(self.audio_buffer) > self.max_buffer_samples:
excess = len(self.audio_buffer) - self.max_buffer_samples
self.audio_buffer = self.audio_buffer[excess:]
def transcribe_and_send(self, audio_data):
try:
segments, _ = self.model.transcribe(
audio_data,
beam_size=5,
condition_on_previous_text=False,
language=self.language if self.language != "auto" else None,
)
transcribed_text = " ".join(segment.text for segment in segments)
if transcribed_text.strip():
for char in transcribed_text:
self.keyboard_controller.press(char)
self.keyboard_controller.release(char)
time.sleep(0.001)
logger.info(f"Transcribed text: {transcribed_text}")
except Exception as e:
logger.error(f"Transcription error: {e}")
def start_recording(self):
if not self.is_recording:
logger.info("Starting recording...")
self.stop_event.clear()
self.is_recording = True
self.stream = sd.InputStream(
callback=self.audio_callback,
channels=1,
samplerate=self.sample_rate,
blocksize=4000,
device="default",
)
self.stream.start()
def stop_recording_and_transcribe(self):
if self.is_recording:
logger.info("Stopping recording and starting transcription...")
self.stop_event.set()
self.is_recording = False
self.stream.stop()
self.stream.close()
if self.audio_buffer:
threading.Thread(
target=self.transcribe_and_send,
args=(np.array(self.audio_buffer, dtype=np.float32),),
daemon=True,
).start()
self.audio_buffer.clear()
def on_press(self, key):
try:
if key == keyboard.Key.pause and not self.is_recording:
self.start_recording()
except AttributeError:
pass
def on_release(self, key):
try:
if key == keyboard.Key.pause and self.is_recording:
self.stop_recording_and_transcribe()
except AttributeError:
pass
def run(self):
self.set_default_audio_source()
with keyboard.Listener(
on_press=self.on_press, on_release=self.on_release
) as listener:
logger.info("Press PAUSE to start/stop recording. Press Ctrl+C to exit.")
try:
listener.join()
except KeyboardInterrupt:
if self.is_recording:
self.stop_recording_and_transcribe()
logger.info("Program terminated by user")
def main():
while True:
try:
initial_choice = curses.wrapper(get_initial_choice)
if initial_choice not in ["Use Last Settings", "Choose New Settings"]:
continue
if initial_choice == "Use Last Settings":
settings = load_settings()
if not settings:
logger.info(
"No previous settings found. Proceeding with new settings."
)
initial_choice = "Choose New Settings"
if initial_choice == "Choose New Settings":
device_name = curses.wrapper(
lambda stdscr: curses_menu(
stdscr, "", [src.name for src in pulsectl.Pulse().source_list()]
)
)
model_size = curses.wrapper(
lambda stdscr: curses_menu(stdscr, "", accepted_models)
)
english_only = model_size in ENGLISH_ONLY_MODELS
device = curses.wrapper(
lambda stdscr: curses_menu(stdscr, "", accepted_devices)
)
if device == "cpu":
available_compute_types = ["int8"]
compute_type_message = (
"float16 is not supported on CPU: only int8 is available"
)
else:
available_compute_types = accepted_compute_types
compute_type_message = ""
if english_only:
compute_type_message += (
"\n\nLanguage selection skipped for this English-only model."
)
compute_type = curses.wrapper(
lambda stdscr: curses_menu(
stdscr,
"",
available_compute_types,
message=compute_type_message,
)
)
if not english_only:
language = curses.wrapper(
lambda stdscr: curses_menu(stdscr, "", accepted_languages)
)
else:
language = "en"
if any(
[
not x
for x in [
device_name,
model_size,
compute_type,
device,
language,
]
]
):
continue
save_settings(
{
"device_name": device_name,
"model_size": model_size,
"compute_type": compute_type,
"device": device,
"language": language,
}
)
settings = Settings(
device_name, model_size, compute_type, device, language
)
transcriber = MicrophoneTranscriber(settings)
try:
transcriber.run()
break
except Exception as e:
logger.error(f"Error: {e}")
continue
except KeyboardInterrupt:
logger.info("Program terminated by user")
break
if __name__ == "__main__":
main()