-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhushtype.py
More file actions
738 lines (615 loc) · 24 KB
/
Copy pathhushtype.py
File metadata and controls
738 lines (615 loc) · 24 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
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
"""hushtype -- Real-time voice dictation for Windows.
GPU-accelerated speech-to-text using OpenAI Whisper, with voice commands,
smart auto-spacing, terminal-aware paste, and a visual status indicator.
Runs system-wide as an input method -- dictate into any application.
Usage:
python hushtype.py Start dictation (Ctrl+Alt+V to toggle)
python hushtype.py --help Show all options
https://github.com/turqoisehex/hushtype
"""
import os
import sys
# Ensure print() works in PyInstaller frozen exe (unbuffered stdout)
if getattr(sys, 'frozen', False):
try:
sys.stdout.reconfigure(line_buffering=True)
sys.stderr.reconfigure(line_buffering=True)
except Exception:
pass
import multiprocessing
import argparse
# Suppress noisy hf_xet warning about optional download accelerator
import warnings
warnings.filterwarnings("ignore", message=".*hf_xet.*")
from RealtimeSTT import AudioToTextRecorder
import numpy as np
import pyautogui
import pyaudio
import re
import time
import threading
import logging
import ctypes
import winsound
import tkinter as tk
import win32clipboard
pyautogui.FAILSAFE = False
__version__ = "1.1.0"
# ---------------------------------------------------------------------------
# Logging setup -- truncated each session
# ---------------------------------------------------------------------------
LOG_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "hushtype.log")
_file_handler = logging.FileHandler(LOG_FILE, mode='w', encoding='utf-8')
_file_handler.setFormatter(
logging.Formatter('%(asctime)s %(levelname)s %(name)s: %(message)s'))
logging.getLogger().addHandler(_file_handler)
logging.getLogger().setLevel(logging.WARNING)
class _QuietFilter(logging.Filter):
"""Suppress noisy 'latency limit' messages from console (still logged to file)."""
def filter(self, record):
return "latency limit" not in record.getMessage()
for _h in logging.getLogger().handlers:
if isinstance(_h, logging.StreamHandler) and not isinstance(_h, logging.FileHandler):
_h.addFilter(_QuietFilter())
logging.getLogger("realtimestt").addFilter(_QuietFilter())
# ---------------------------------------------------------------------------
# Global state
# ---------------------------------------------------------------------------
listening = False
recorder = None
last_toggle_time = 0
need_space_before = False
status_window = None
status_label = None
# CLI overrides -- populated by parse_args(), read by recorder_loop()
_cli_config = {
'model': 'turbo', 'silence': 3.0, 'sensitivity': 0.4, 'channel': 1,
}
# Known Whisper hallucination phrases (appear on silence/noise).
HALLUCINATION_PHRASES = {
"thank you", "thanks for watching", "i'm sorry",
"you", "bye", "the end",
}
# Terminal window classes that need Ctrl+Shift+V instead of Ctrl+V
TERMINAL_CLASSES = {
b"ConsoleWindowClass", b"CASCADIA_HOSTING_WINDOW_CLASS",
b"mintty", b"VirtualConsoleClass", b"PuTTY",
}
user32 = ctypes.windll.user32
# ---------------------------------------------------------------------------
# Hotkey polling via GetAsyncKeyState (no keyboard library dependency)
# ---------------------------------------------------------------------------
VK_CONTROL = 0x11
VK_MENU = 0x12 # Alt
VK_V = 0x56
VK_RETURN = 0x0D
GetAsyncKeyState = user32.GetAsyncKeyState
GetAsyncKeyState.restype = ctypes.c_short
GetAsyncKeyState.argtypes = [ctypes.c_int]
_hotkey_was_pressed = False
_enter_was_pressed = False
def poll_hotkey():
"""Poll Ctrl+Alt+V and Enter via GetAsyncKeyState every 50 ms."""
global _hotkey_was_pressed, _enter_was_pressed, need_space_before
ctrl = GetAsyncKeyState(VK_CONTROL) & 0x8000
alt = GetAsyncKeyState(VK_MENU) & 0x8000
v_key = GetAsyncKeyState(VK_V) & 0x8000
hotkey_down = bool(ctrl and alt and v_key)
if hotkey_down and not _hotkey_was_pressed:
toggle()
_hotkey_was_pressed = hotkey_down
enter_down = bool(GetAsyncKeyState(VK_RETURN) & 0x8000)
if enter_down and not _enter_was_pressed:
need_space_before = False
_enter_was_pressed = enter_down
if status_window:
status_window.after(50, poll_hotkey)
# ---------------------------------------------------------------------------
# Audio device discovery
# ---------------------------------------------------------------------------
def find_input_device(name_substring):
"""Find audio input device index by name substring, or None."""
p = pyaudio.PyAudio()
try:
for i in range(p.get_device_count()):
info = p.get_device_info_by_index(i)
if (info['maxInputChannels'] > 0
and name_substring.lower() in info['name'].lower()):
print(f"[OK] Using device {i}: {info['name']}")
return i
finally:
p.terminate()
return None
# ---------------------------------------------------------------------------
# Audio feed thread -- reads from mic, feeds to recorder
# ---------------------------------------------------------------------------
def audio_feed(device_index):
"""Read int16 mono audio from device and feed to recorder via feed_audio.
Opens at 16 kHz mono int16 (proven to work on NVIDIA Broadcast).
If mono fails, opens with native channels and extracts selected channel.
"""
global recorder
RATE = 16000
CHUNK = 1024
ch_select = _cli_config.get('channel', 1) - 1 # 1-indexed CLI -> 0-indexed
while True:
p = pyaudio.PyAudio()
stream = None
mono = True
try:
try:
stream = p.open(
format=pyaudio.paInt16, channels=1, rate=RATE,
input=True, input_device_index=device_index,
frames_per_buffer=CHUNK)
except Exception:
info = p.get_device_info_by_index(device_index)
ch = int(info['maxInputChannels'])
mono = False
stream = p.open(
format=pyaudio.paInt16, channels=ch, rate=RATE,
input=True, input_device_index=device_index,
frames_per_buffer=CHUNK)
while True:
data = stream.read(CHUNK, exception_on_overflow=False)
if mono:
r = recorder
if r is not None:
r.feed_audio(data)
else:
samples = np.frombuffer(data, dtype=np.int16)
ch_idx = min(ch_select, ch - 1)
selected = samples[ch_idx::ch].copy()
r = recorder
if r is not None:
r.feed_audio(selected)
except Exception as e:
logging.warning("Audio feed error: %s", e)
print(f"[!] Audio feed error: {e}")
time.sleep(2)
finally:
if stream is not None:
try:
stream.stop_stream()
stream.close()
except Exception:
pass
p.terminate()
# ---------------------------------------------------------------------------
# Clipboard paste (terminal-aware)
# ---------------------------------------------------------------------------
def is_terminal_foreground():
hwnd = user32.GetForegroundWindow()
class_name = ctypes.create_string_buffer(256)
user32.GetClassNameA(hwnd, class_name, 256)
return class_name.value in TERMINAL_CLASSES
def paste_from_clipboard():
"""Send Ctrl+V or Ctrl+Shift+V depending on whether target is a terminal."""
if is_terminal_foreground():
pyautogui.hotkey('ctrl', 'shift', 'v')
else:
pyautogui.hotkey('ctrl', 'v')
# ---------------------------------------------------------------------------
# Visual indicator
# ---------------------------------------------------------------------------
drag_start_x = 0
drag_start_y = 0
def create_status_window():
global status_window, status_label
status_window = tk.Tk()
status_window.title("")
status_window.attributes('-topmost', True)
status_window.attributes('-alpha', 0.85)
status_window.overrideredirect(True)
sw = status_window.winfo_screenwidth()
sh = status_window.winfo_screenheight()
status_window.geometry(f"120x30+{sw - 140}+{sh - 75}")
status_label = tk.Label(
status_window, text="PAUSED",
font=("Segoe UI", 11, "bold"),
fg="white", bg="#666666", padx=10, pady=5)
status_label.pack(fill=tk.BOTH, expand=True)
status_label.bind('<Button-1>', _start_drag)
status_label.bind('<B1-Motion>', _on_drag)
return status_window
def _start_drag(event):
global drag_start_x, drag_start_y
drag_start_x = event.x
drag_start_y = event.y
def _on_drag(event):
x = status_window.winfo_x() + event.x - drag_start_x
y = status_window.winfo_y() + event.y - drag_start_y
status_window.geometry(f"+{x}+{y}")
def update_status(state):
if status_label is None:
return
def do_update():
if state == "paused":
status_label.config(text="PAUSED", bg="#666666")
elif state == "listening":
status_label.config(text="LISTENING", bg="#2e7d32")
elif state == "transcribing":
status_label.config(text="WORKING", bg="#f57c00")
if status_window:
status_window.after(0, do_update)
# ---------------------------------------------------------------------------
# Recorder callbacks
# ---------------------------------------------------------------------------
def on_recording_start():
if listening:
update_status("listening")
def on_recording_stop():
if listening:
update_status("transcribing")
# ---------------------------------------------------------------------------
# Voice commands
# ---------------------------------------------------------------------------
def clean_command(text):
"""Remove punctuation and extra whitespace for command matching."""
return re.sub(r'[^\w\s]', '', text).strip().lower()
VOICE_COMMANDS = {
# Correction
"scratch that": lambda: pyautogui.hotkey('ctrl', 'z'),
"delete that": lambda: pyautogui.hotkey('ctrl', 'z'),
"delete last": lambda: pyautogui.hotkey('ctrl', 'z'),
"select that": lambda: pyautogui.hotkey('ctrl', 'shift', 'left'),
"select last": lambda: pyautogui.hotkey('ctrl', 'shift', 'left'),
"select word": lambda: pyautogui.hotkey('ctrl', 'shift', 'left'),
"select": lambda: pyautogui.hotkey('ctrl', 'shift', 'left'),
"select all": lambda: pyautogui.hotkey('ctrl', 'a'),
"undo": lambda: pyautogui.hotkey('ctrl', 'z'),
"undo that": lambda: pyautogui.hotkey('ctrl', 'z'),
"redo": lambda: pyautogui.hotkey('ctrl', 'y'),
"redo that": lambda: pyautogui.hotkey('ctrl', 'y'),
"delete word": lambda: pyautogui.hotkey('ctrl', 'backspace'),
"backspace word": lambda: pyautogui.hotkey('ctrl', 'backspace'),
# Navigation
"new line": lambda: pyautogui.press('enter'),
"newline": lambda: pyautogui.press('enter'),
"next line": lambda: pyautogui.press('enter'),
"enter": lambda: pyautogui.press('enter'),
"go to start": lambda: pyautogui.hotkey('ctrl', 'home'),
"go to beginning": lambda: pyautogui.hotkey('ctrl', 'home'),
"go to end": lambda: pyautogui.hotkey('ctrl', 'end'),
"go up": lambda: pyautogui.press('up'),
"go down": lambda: pyautogui.press('down'),
# Clipboard
"copy": lambda: pyautogui.hotkey('ctrl', 'c'),
"copy that": lambda: pyautogui.hotkey('ctrl', 'c'),
"paste": paste_from_clipboard,
"paste that": paste_from_clipboard,
"cut": lambda: pyautogui.hotkey('ctrl', 'x'),
"cut that": lambda: pyautogui.hotkey('ctrl', 'x'),
# Formatting
"tab": lambda: pyautogui.press('tab'),
"indent": lambda: pyautogui.press('tab'),
"outdent": lambda: pyautogui.hotkey('shift', 'tab'),
"unindent": lambda: pyautogui.hotkey('shift', 'tab'),
# Special
"save": lambda: pyautogui.hotkey('ctrl', 's'),
"save file": lambda: pyautogui.hotkey('ctrl', 's'),
"save that": lambda: pyautogui.hotkey('ctrl', 's'),
}
_MULTI_STEP = {
"select line": [('press', 'home'), ('hotkey', 'shift', 'end')],
"delete line": [('press', 'home'), ('hotkey', 'shift', 'end'), ('press', 'delete')],
"clear line": [('press', 'home'), ('hotkey', 'shift', 'end'), ('press', 'delete')],
"new paragraph": [('press', 'enter'), ('press', 'enter')],
"next paragraph": [('press', 'enter'), ('press', 'enter')],
}
# Commands that don't change cursor/text state -- preserve need_space_before.
_NO_RESET_CMDS = {"copy", "copy that", "save", "save file", "save that"}
def _dispatch_command(cmd):
"""Execute a voice command. Returns True if handled."""
if cmd in VOICE_COMMANDS:
VOICE_COMMANDS[cmd]()
return True
if cmd in _MULTI_STEP:
for step in _MULTI_STEP[cmd]:
action, *args = step
getattr(pyautogui, action)(*args)
return True
if cmd in ("stop listening", "pause", "stop"):
toggle()
return True
return False
# ---------------------------------------------------------------------------
# Text processing
# ---------------------------------------------------------------------------
def process(text):
global need_space_before
t = text.strip()
if not t:
return
if not listening:
return
if time.time() - last_toggle_time < 0.5:
return
update_status("listening")
if t.lower().rstrip('.!?,') in HALLUCINATION_PHRASES:
return
cmd = clean_command(t)
# Voice command dispatch
if _dispatch_command(cmd):
if cmd not in _NO_RESET_CMDS:
need_space_before = False
return
# Default: type the text via clipboard
prefix = " " if need_space_before else ""
old_clip = None
try:
win32clipboard.OpenClipboard()
try:
old_clip = win32clipboard.GetClipboardData(
win32clipboard.CF_UNICODETEXT)
except TypeError:
pass
finally:
win32clipboard.CloseClipboard()
except Exception:
pass
try:
win32clipboard.OpenClipboard()
try:
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardData(
win32clipboard.CF_UNICODETEXT, prefix + t)
finally:
win32clipboard.CloseClipboard()
except Exception:
return # clipboard locked -- skip silently
need_space_before = True
paste_from_clipboard()
time.sleep(0.03)
if old_clip is not None:
try:
win32clipboard.OpenClipboard()
try:
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardData(
win32clipboard.CF_UNICODETEXT, old_clip)
finally:
win32clipboard.CloseClipboard()
except Exception:
pass
# ---------------------------------------------------------------------------
# Toggle listening
# ---------------------------------------------------------------------------
def toggle():
global listening, last_toggle_time, need_space_before
if time.time() - last_toggle_time < 0.3:
return
listening = not listening
last_toggle_time = time.time()
if listening:
need_space_before = False
clear_audio_buffer()
print("\n[LISTENING]")
update_status("listening")
threading.Thread(target=lambda: winsound.Beep(523, 80),
daemon=True).start()
else:
print("\n[PAUSED]")
update_status("paused")
threading.Thread(target=lambda: winsound.Beep(440, 80),
daemon=True).start()
if recorder:
try:
recorder.interrupt_stop_event.set()
except AttributeError:
pass
# ---------------------------------------------------------------------------
# Audio buffer management
# ---------------------------------------------------------------------------
def clear_audio_buffer():
"""Drain any audio that accumulated in the recorder while paused."""
if recorder is None:
return
try:
if hasattr(recorder, 'audio_queue'):
while not recorder.audio_queue.empty():
try:
recorder.audio_queue.get_nowait()
except Exception:
break
if hasattr(recorder, 'frames'):
recorder.frames.clear()
except Exception:
pass
# ---------------------------------------------------------------------------
# Recorder management
# ---------------------------------------------------------------------------
def create_recorder(input_device_index):
"""Create a fresh AudioToTextRecorder instance."""
return AudioToTextRecorder(
model=_cli_config['model'],
language="en",
device="cuda",
compute_type="auto",
use_microphone=False, # we feed audio ourselves
silero_sensitivity=_cli_config['sensitivity'],
silero_use_onnx=True,
post_speech_silence_duration=_cli_config['silence'],
min_length_of_recording=0.5,
early_transcription_on_silence=800,
normalize_audio=True,
spinner=False,
on_recording_start=on_recording_start,
on_recording_stop=on_recording_stop,
)
def _restart_recorder(device_index, reason):
"""Shutdown and recreate the recorder. Returns True on success."""
global recorder
_file_handler.setLevel(logging.CRITICAL)
try:
recorder.shutdown()
except Exception:
pass
time.sleep(2)
try:
recorder = create_recorder(device_index)
_file_handler.setLevel(logging.WARNING)
print("[OK] Recorder restarted.\n")
logging.warning("Recorder restarted after: %s", reason)
return True
except Exception as e:
_file_handler.setLevel(logging.WARNING)
logging.exception("Recorder restart failed")
print(f"[ERR] Restart failed: {e}")
print(" Retrying in 5 seconds...")
time.sleep(5)
return False
# ---------------------------------------------------------------------------
# Recorder loop
# ---------------------------------------------------------------------------
def recorder_loop(device_index):
global recorder
print("[...] Loading Whisper model (GPU)...")
# Device selection: --device > NVIDIA Broadcast > system default
input_device = device_index
if _cli_config.get('device'):
explicit = find_input_device(_cli_config['device'])
if explicit is not None:
input_device = explicit
else:
print(f"[!] Device matching '{_cli_config['device']}' not found")
recorder = create_recorder(input_device)
print("[OK] Model loaded. Ready.\n")
# Start audio feed thread
threading.Thread(target=audio_feed, args=(input_device,),
daemon=True).start()
was_listening = False
error_count = 0
while True:
try:
if listening:
if not was_listening:
clear_audio_buffer()
was_listening = True
recorder.text(process)
error_count = 0
else:
was_listening = False
time.sleep(0.1)
except (BrokenPipeError, ConnectionError, EOFError, OSError) as e:
logging.exception("Recorder connection lost")
print(f"\n[!] Recorder connection lost: {e}")
if _restart_recorder(input_device, str(e)):
error_count = 0
was_listening = False
except Exception as e:
error_count += 1
logging.warning("Recorder error (%d/5): %s", error_count, e)
if error_count >= 5:
logging.exception("Too many errors, restarting recorder")
print(f"\n[!] Too many errors ({e}) -- restarting...")
if _restart_recorder(input_device, str(e)):
error_count = 0
was_listening = False
else:
print(f"[!] Error: {e}")
time.sleep(0.5)
# ---------------------------------------------------------------------------
# CLI
# ---------------------------------------------------------------------------
def parse_args():
parser = argparse.ArgumentParser(
prog="hushtype",
description=(
"Real-time voice dictation for Windows. "
"GPU-accelerated speech-to-text powered by OpenAI Whisper, "
"with 40+ voice commands, smart auto-spacing, terminal-aware "
"paste, and a visual status indicator."
),
)
parser.add_argument(
"--version", action="version", version=f"%(prog)s {__version__}",
)
parser.add_argument(
"--model", default="turbo",
help="Whisper model size: tiny, base, small, medium, large-v3, "
"turbo (default: turbo)",
)
parser.add_argument(
"--silence", type=float, default=2.5,
help="Seconds of silence before finalizing speech (default: 2.5)",
)
parser.add_argument(
"--sensitivity", type=float, default=0.55,
help="VAD sensitivity 0.0-1.0, higher = more sensitive (default: 0.55)",
)
parser.add_argument(
"--device",
help="Preferred mic name substring (e.g. --device ZOOM). "
"Overrides NVIDIA Broadcast.",
)
parser.add_argument(
"--channel", type=int, default=1,
help="Audio channel on multi-channel devices, 1-indexed (default: 1)",
)
parser.add_argument(
"--verbose", action="store_true",
help="Show detailed debug logging from the speech recognizer",
)
parser.add_argument(
"--offline", action="store_true",
help="Disable all network access (model must already be cached)",
)
parser.add_argument(
"--cache-dir",
help="HuggingFace cache directory for Whisper models",
)
return parser.parse_args()
# ---------------------------------------------------------------------------
# Entry point
# ---------------------------------------------------------------------------
def main():
args = parse_args()
print("=" * 50)
print(f"hushtype v{__version__} -- Voice Dictation")
print("=" * 50)
print("\nPress Ctrl+Alt+V to toggle listening")
print("Press Ctrl+C to quit")
print("\nCommands: scratch that, delete word, select, new line,")
print(" undo, redo, copy, paste, save, stop listening\n")
if args.offline:
os.environ["HF_HUB_OFFLINE"] = "1"
if args.cache_dir:
os.environ["HF_HOME"] = args.cache_dir
_cli_config['model'] = args.model
_cli_config['silence'] = args.silence
_cli_config['sensitivity'] = args.sensitivity
_cli_config['device'] = args.device
_cli_config['channel'] = args.channel
_cli_config['verbose'] = args.verbose
if args.verbose:
logging.getLogger("realtimestt").setLevel(logging.DEBUG)
_console = logging.StreamHandler()
_console.setFormatter(
logging.Formatter('%(levelname)s: %(message)s'))
logging.getLogger("realtimestt").addHandler(_console)
# Find default device before starting
device = find_input_device("NVIDIA Broadcast")
if device is None:
print("[!] NVIDIA Broadcast not found, using system default")
root = create_status_window()
# Start hotkey polling (runs inside tkinter main loop)
poll_hotkey()
# Start recorder in background thread
recorder_thread = threading.Thread(
target=recorder_loop, args=(device,), daemon=True)
recorder_thread.start()
try:
root.mainloop()
except KeyboardInterrupt:
pass
finally:
print("\nExiting")
if recorder:
try:
recorder.shutdown()
except Exception:
pass
if __name__ == '__main__':
multiprocessing.freeze_support()
main()