-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice.py
More file actions
508 lines (443 loc) · 19.4 KB
/
Copy pathservice.py
File metadata and controls
508 lines (443 loc) · 19.4 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
# -*- coding: utf8 -*-
# Voice keyboard service — runs at Kodi startup, manages voice input lifecycle
import json
import os
import subprocess
import sys
import threading
import time
# Ensure the addon directory is on sys.path so lazy imports from worker/GLib
# threads can resolve lib.* modules.
_addon_dir = os.path.dirname(os.path.abspath(__file__))
if _addon_dir not in sys.path:
sys.path.insert(0, _addon_dir)
try:
import xbmc
import xbmcaddon
import xbmcgui
_KODI_AVAILABLE = True
except ImportError:
_KODI_AVAILABLE = False
from lib.audio_capture import get_audio_backend
from lib.rate_limiter import RateLimiter
from lib.stt import get_stt_provider
from lib.stt.gemini import GeminiSTTProvider # noqa: F401 — pre-import for thread access
DEBOUNCE_SECONDS = 0.5
class VoiceService(object):
"""Polls for voice activation, captures audio, and injects transcribed text."""
_STATE_IDLE = "idle"
_STATE_LISTENING = "listening"
_STATE_PROCESSING = "processing"
def __init__(self):
self._state = self._STATE_IDLE
self._lock = threading.Lock()
self._last_activation_time = 0.0
self._rate_limiter = RateLimiter()
self._ble_backend = None
self._stt_provider = None
self._mic_button = None
def _get_state(self):
with self._lock:
return self._state
def _set_state(self, state):
with self._lock:
self._state = state
def _check_activation(self):
"""Return True if the voice_keyboard_activate property is set (and clear it)."""
now = time.time()
if now - self._last_activation_time < DEBOUNCE_SECONDS:
return False
window = xbmcgui.Window(10000)
prop = window.getProperty("voice_keyboard_activate")
if prop:
window.clearProperty("voice_keyboard_activate")
self._last_activation_time = now
return True
return False
def _keyboard_visible(self):
"""Return True if the virtual keyboard is currently on screen."""
return bool(xbmc.getCondVisibility("Window.IsVisible(virtualkeyboard)"))
def _inject_text(self, text):
"""Inject text via Input.SendText JSON-RPC. done=False keeps keyboard open."""
payload = json.dumps(
{
"jsonrpc": "2.0",
"method": "Input.SendText",
"params": {"text": text, "done": False},
"id": 1,
}
)
xbmc.executeJSONRPC(payload)
def _worker(self):
"""Worker thread: capture → transcribe → inject. Always returns state to idle."""
progress = None
try:
provider_name = xbmcaddon.Addon().getSetting("stt_provider") or "gemini"
# Gate Gemini calls against free-tier rate limits before recording.
if provider_name == "gemini":
if not self._rate_limiter.can_call():
reason = self._rate_limiter.get_limit_reason()
if reason == "rpm":
msg = "Rate limit reached — try again in a moment"
else:
msg = "Daily rate limit reached — try again tomorrow"
xbmc.log(
"Voice keyboard rate limited: {}".format(reason),
xbmc.LOGWARNING,
)
xbmcgui.Dialog().notification(
"Voice Input", msg, xbmcgui.NOTIFICATION_WARNING, 5000
)
return
# Persistent indicator: stays up while listening, flips to
# "Transcribing..." after capture, closed in the finally below —
# so there is never silent dead air between speech and result.
progress = xbmcgui.DialogProgressBG()
progress.create("Voice Input", "Listening...")
backend = self._ble_backend if self._ble_backend else get_audio_backend()
if backend is None:
xbmcgui.Dialog().notification(
"Voice Input",
"No microphone detected",
xbmcgui.NOTIFICATION_ERROR,
5000,
)
return
try:
# BLE backend with shared btmon: audio is already being fed
# externally, so use external_feed=True to skip spawning
# a second btmon process.
if backend is self._ble_backend:
backend.start_recording(external_feed=True)
else:
backend.start_recording()
# BLE backend buffers asynchronously; wait for silence or timeout.
if hasattr(backend, "wait_for_silence"):
backend.wait_for_silence()
# Tell the remote the session is over the moment capture ends
# (turns its LED off) — before the WAV build, which can take
# a second on this hardware. Covers every stop reason.
if getattr(self, "_ble_control_char_path", None):
from lib.audio_capture.ble import _send_mic_close
_send_mic_close(self._ble_control_char_path)
wav_bytes = backend.stop_recording()
# Debug aid: keep the last capture on disk so audio-quality
# problems can be inspected (this is what STT actually gets).
if wav_bytes:
try:
with open(
"/storage/.kodi/temp/voice_last.wav", "wb"
) as dump:
dump.write(wav_bytes)
except OSError:
pass
except Exception as exc:
xbmc.log(
"Voice keyboard microphone error: {}".format(exc), xbmc.LOGWARNING
)
xbmcgui.Dialog().notification(
"Voice Input",
"Microphone error — check connection",
xbmcgui.NOTIFICATION_ERROR,
5000,
)
return
if not wav_bytes:
xbmcgui.Dialog().notification(
"Voice Input",
"No speech detected",
xbmcgui.NOTIFICATION_ERROR,
5000,
)
return
self._set_state(self._STATE_PROCESSING)
progress.update(75, message="Transcribing...")
provider = self._stt_provider if self._stt_provider else get_stt_provider()
try:
candidates = provider.transcribe_candidates(wav_bytes)
except RuntimeError as exc:
xbmc.log(
"Voice keyboard transcription failed: {}".format(exc),
xbmc.LOGWARNING,
)
xbmcgui.Dialog().notification(
"Voice Input",
"Transcription failed: {}".format(exc),
xbmcgui.NOTIFICATION_ERROR,
5000,
)
return
if provider_name == "gemini":
self._rate_limiter.record_call()
xbmc.log(
"Voice keyboard STT heard: {!r} (candidates: {})".format(
candidates[0] if candidates else "", candidates
),
xbmc.LOGINFO,
)
# Close the busy indicator before showing results (or the picker).
progress.close()
progress = None
if not any(c.strip() for c in candidates):
xbmcgui.Dialog().notification(
"Voice Input",
"Heard nothing usable — try again",
xbmcgui.NOTIFICATION_ERROR,
5000,
)
return
if len(candidates) <= 1:
result = candidates[0] if candidates else ""
else:
idx = xbmcgui.Dialog().select("Voice Input — pick a title", candidates)
if idx < 0:
return # user cancelled
result = candidates[idx]
xbmc.log(
"Voice keyboard: injecting text {!r}".format(result), xbmc.LOGINFO
)
self._inject_text(result)
except Exception as exc:
xbmc.log("Voice keyboard worker error: {}".format(exc), xbmc.LOGWARNING)
xbmcgui.Dialog().notification(
"Voice Input",
"Unexpected error — check log",
xbmcgui.NOTIFICATION_ERROR,
5000,
)
finally:
if progress is not None:
try:
progress.close()
except Exception:
pass
self._set_state(self._STATE_IDLE)
def _send_ble_ack(self):
"""Send ack to control characteristic — fire-and-forget.
Uses Popen (no wait) so it doesn't block the btmon reader thread.
"""
if not (
hasattr(self, "_ble_control_char_path") and self._ble_control_char_path
):
return
from lib.audio_capture.ble import _send_voice_ack_fast
_send_voice_ack_fast(self._ble_control_char_path)
def _on_ble_voice_start(self):
"""Callback from btmon reader when mic button is pressed.
NOTE: This is called from the btmon reader thread. We only do
thread-safe checks here (state, debounce) and defer the keyboard
visibility check + worker launch to a new thread where Kodi API
calls are safe.
MIC_OPEN is only sent when a new session actually starts: every
MIC_OPEN resets the remote's ~17s internal mic timer, so re-sending
it on the clone firmware's ff retries needlessly extends the
session (and how long the remote's LED stays lit).
"""
now = time.time()
elapsed = now - self._last_activation_time
if elapsed < DEBOUNCE_SECONDS:
return
state = self._get_state()
if state != self._STATE_IDLE:
return
# Open the remote's mic for this new session.
self._send_ble_ack()
# Set state immediately to prevent double-triggers
self._set_state(self._STATE_LISTENING)
self._last_activation_time = now
if _KODI_AVAILABLE:
xbmc.log(
"Voice keyboard: mic button pressed, launching worker", xbmc.LOGINFO
)
def _check_and_run():
if not self._keyboard_visible():
if _KODI_AVAILABLE:
xbmc.log(
"Voice keyboard: keyboard not visible, ignoring",
xbmc.LOGINFO,
)
self._set_state(self._STATE_IDLE)
return
self._worker()
t = threading.Thread(target=_check_and_run)
t.daemon = True
t.start()
def _start_ble_monitor(self):
"""Start BLE voice monitor if audio_source is BLE and remote is available.
Sets up D-Bus notifications on status, audio, and control characteristics,
then starts a single shared btmon subprocess that handles both mic button
detection and audio data capture.
"""
from lib.audio_capture import _get_audio_source
if _get_audio_source() != "ble":
return False
try:
from lib.audio_capture.ble import (
BLEAudioCapture,
_find_char_path,
_start_notify,
VOICE_STATUS_UUID,
VOICE_DATA_UUID,
VOICE_CONTROL_UUID,
)
self._ble_backend = BLEAudioCapture()
self._stt_provider = get_stt_provider()
# Find and enable notifications on status char (mic button)
status_path = _find_char_path(VOICE_STATUS_UUID)
if status_path is None:
xbmc.log(
"Voice keyboard BLE: no status characteristic found",
xbmc.LOGWARNING,
)
return False
_start_notify(status_path)
# Find and enable notifications on audio data char
audio_path = _find_char_path(VOICE_DATA_UUID)
if audio_path:
_start_notify(audio_path)
# Find control char and pre-enable voice mode.
# Writing 0x01 at startup tells the remote the host is ready
# for voice data, so it enters voice mode immediately on button
# press instead of doing a quick press/release cycle.
control_path = _find_char_path(VOICE_CONTROL_UUID)
self._ble_control_char_path = control_path
if control_path:
from lib.audio_capture.ble import _send_get_caps
_send_get_caps(control_path)
xbmc.log(
"Voice keyboard BLE: sent ATVV GET_CAPS handshake",
xbmc.LOGINFO,
)
xbmc.log(
"Voice keyboard BLE: monitoring for mic button press", xbmc.LOGINFO
)
return True
except Exception as exc:
xbmc.log(
"Voice keyboard BLE monitor failed: {}".format(exc),
xbmc.LOGWARNING,
)
return False
def _start_btmon_watcher(self):
"""Start a single btmon subprocess for both mic detection and audio capture.
btmon captures raw HCI traffic below the D-Bus layer, bypassing
Kodi's GLib main loop. This shared process handles:
- Handle 0x0042 (status): mic button press/release detection
- Handle 0x003f (audio): voice data capture → fed to BLE backend
"""
self._btmon_proc = subprocess.Popen(
["btmon"],
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
)
self._btmon_thread = threading.Thread(
target=self._btmon_reader_safe, daemon=True
)
self._btmon_thread.start()
xbmc.log("Voice keyboard: btmon watcher thread started", xbmc.LOGINFO)
def _btmon_reader_safe(self):
"""Wrapper that catches and logs exceptions from _btmon_reader."""
try:
self._btmon_reader()
except Exception as exc:
if _KODI_AVAILABLE:
xbmc.log(
"Voice keyboard: btmon reader crashed: {}".format(exc),
xbmc.LOGWARNING,
)
def _btmon_reader(self):
"""Read btmon output, detect mic button and capture audio data.
Unified reader for the single shared btmon process. Handles:
- Handle 0x0042 + Data[1]: ff → mic button press → _on_ble_voice_start()
- Handle 0x003f + Data[N]: hex → audio data → BLE backend feed
Also auto-triggers voice start when audio data appears on 0x003f
while idle — this catches cases where the status notification (0x0042)
was too brief for btmon to capture.
"""
import re
import select as _select
data_re = re.compile(r"Data\[(\d+)\]:\s*([0-9a-fA-F]+)")
prev_line = ""
while self._btmon_proc and self._btmon_proc.poll() is None:
r, _, _ = _select.select([self._btmon_proc.stdout], [], [], 0.05)
if not r:
continue
raw = self._btmon_proc.stdout.readline()
if not raw:
continue
line = raw.decode("utf-8", errors="replace").rstrip()
# Voice button (ATVV): Data[1]: 08 = START_SEARCH; clone
# firmwares (UR02) retry with ff while waiting for MIC_OPEN.
if "Handle: 0x0042" in prev_line and (
"Data[1]: ff" in line or "Data[1]: 08" in line
):
if _KODI_AVAILABLE:
xbmc.log(
"Voice keyboard BLE: mic button detected via btmon",
xbmc.LOGINFO,
)
self._on_ble_voice_start()
# ATVV AUDIO_END / button release (0x00). Two interaction modes:
# - short press (<2s): keep listening until silence is detected
# (ignore the release that immediately follows the press)
# - held >=2s: releasing the button is the stop signal
# The remote's own end-of-stream 0x00 also arrives well past 2s,
# so it stops the capture through the same path.
elif "Handle: 0x0042" in prev_line and "Data[1]: 00" in line:
backend = self._ble_backend
if backend is not None and getattr(backend, "_recording", False):
if time.time() - self._last_activation_time >= 2.0:
backend.signal_stream_end()
# Audio data: Handle 0x003f, Data[N]: hex
elif "Handle: 0x003f" in prev_line:
m = data_re.search(line)
if m:
# If audio data arrives while idle, auto-trigger voice start.
# This catches the case where the status ff notification was
# too brief for btmon to output before it was replaced by 00.
if self._get_state() == self._STATE_IDLE:
if _KODI_AVAILABLE:
xbmc.log(
"Voice keyboard BLE: audio data triggered voice start",
xbmc.LOGINFO,
)
self._on_ble_voice_start()
if self._ble_backend:
self._ble_backend.feed_audio_data(m.group(2))
prev_line = line
def run(self):
"""Main service loop. Polls for activation until Kodi requests abort."""
monitor = xbmc.Monitor()
xbmc.log("Voice keyboard service started")
ble_active = self._start_ble_monitor()
if ble_active:
xbmc.log("Voice keyboard: BLE mic button activation enabled")
self._start_btmon_watcher()
ble_retry_counter = 0
while not monitor.abortRequested():
# Retry BLE monitor setup if it failed at startup (remote may
# not have been connected yet).
if not ble_active:
ble_retry_counter += 1
if ble_retry_counter >= 20: # Every ~10 seconds
ble_retry_counter = 0
ble_active = self._start_ble_monitor()
if ble_active:
xbmc.log("Voice keyboard: BLE connected on retry", xbmc.LOGINFO)
self._start_btmon_watcher()
# Poll window property for activation (works in both BLE and non-BLE
# modes). In BLE mode this enables on-screen mic button activation
# alongside the remote button via RunScript.
if self._get_state() == self._STATE_IDLE:
if self._check_activation():
if self._keyboard_visible():
self._set_state(self._STATE_LISTENING)
t = threading.Thread(target=self._worker)
t.daemon = True
t.start()
monitor.waitForAbort(0.5)
if hasattr(self, "_btmon_proc") and self._btmon_proc:
self._btmon_proc.terminate()
xbmc.log("Voice keyboard service stopped")
if _KODI_AVAILABLE:
VoiceService().run()