-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWakeWordListener.cs
More file actions
420 lines (363 loc) · 12.4 KB
/
Copy pathWakeWordListener.cs
File metadata and controls
420 lines (363 loc) · 12.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
using System.Text;
namespace PrimeDictate;
/// <summary>
/// Opt-in idle mic watcher: short rolling in-memory PCM buffer transcribed looking only for the
/// wake phrase. Consecutive attempts overlap so a multi-word phrase can span slides. Audio is
/// never written to disk.
/// </summary>
internal sealed class WakeWordListener : IAsyncDisposable
{
/// <summary>How much recent PCM to retain and send to STT each attempt (~2s loop).</summary>
private static readonly TimeSpan RollingBufferDuration = TimeSpan.FromSeconds(2);
/// <summary>
/// Time between transcription attempts while speech is present. Kept below
/// <see cref="RollingBufferDuration"/> so consecutive windows overlap for phrases like
/// "okay computer".
/// </summary>
private static readonly TimeSpan SlideInterval = TimeSpan.FromSeconds(1);
private static readonly TimeSpan PollInterval = TimeSpan.FromMilliseconds(300);
private static readonly TimeSpan MinAudioForAttempt = TimeSpan.FromMilliseconds(700);
private static readonly TimeSpan[] MicStartRetryDelays =
[
TimeSpan.FromMilliseconds(250),
TimeSpan.FromMilliseconds(500),
TimeSpan.FromMilliseconds(1_000),
TimeSpan.FromMilliseconds(1_500),
TimeSpan.FromMilliseconds(2_500),
];
private const double MinSpeechRmsThreshold = 0.0018;
private readonly DefaultMicrophoneRecorder recorder = new();
private readonly Func<PcmAudioBuffer, CancellationToken, ValueTask<string>> transcribeAsync;
private readonly object sync = new();
private bool enabled;
private string wakePhrase = AppSettings.DefaultWakeWordPhrase;
private CancellationTokenSource? loopCts;
private Task? loopTask;
private int detectGate;
public WakeWordListener(Func<PcmAudioBuffer, CancellationToken, ValueTask<string>> transcribeAsync)
{
this.transcribeAsync = transcribeAsync ?? throw new ArgumentNullException(nameof(transcribeAsync));
}
public event Action? WakeDetected;
public bool IsRunning
{
get
{
lock (this.sync)
{
return this.loopTask is { IsCompleted: false };
}
}
}
public void ApplyConfiguration(
bool enableWakeWord,
string? wakeWordPhrase,
string? selectedInputDeviceId,
double inputGainMultiplier)
{
var phrase = NormalizePhrase(wakeWordPhrase);
lock (this.sync)
{
this.enabled = enableWakeWord;
this.wakePhrase = phrase;
}
if (!this.recorder.IsRecording)
{
this.recorder.UpdateInputDevice(selectedInputDeviceId);
this.recorder.UpdateInputGain(inputGainMultiplier);
}
}
/// <summary>
/// Opens the shared-mode mic and starts the listen loop, retrying briefly on open failure
/// (common right after exclusive-mode dictation releases the device).
/// </summary>
/// <param name="stillWanted">
/// Optional gate checked between retries; when it returns false, start is abandoned without error.
/// </param>
/// <returns>
/// true when the listen loop is running; false when abandoned because wake is no longer wanted.
/// </returns>
public async Task<bool> StartAsync(Func<bool>? stillWanted = null)
{
for (var attempt = 0; ; attempt++)
{
if (stillWanted is not null && !stillWanted())
{
return false;
}
try
{
return this.TryStartOnce();
}
catch (Exception ex)
{
if (attempt >= MicStartRetryDelays.Length)
{
AppLog.Error($"Wake word listener failed to start after {attempt + 1} attempts: {ex.Message}");
throw;
}
AppLog.Info(
$"Wake word mic start failed (attempt {attempt + 1}/{MicStartRetryDelays.Length + 1}): {ex.Message}. Retrying...");
await Task.Delay(MicStartRetryDelays[attempt]).ConfigureAwait(false);
lock (this.sync)
{
if (!this.enabled)
{
return false;
}
if (this.loopTask is { IsCompleted: false })
{
return true;
}
}
}
}
}
/// <returns>true when the listen loop is running after this call.</returns>
private bool TryStartOnce()
{
lock (this.sync)
{
if (this.loopTask is { IsCompleted: false })
{
return true;
}
if (!this.enabled)
{
return false;
}
if (!this.recorder.IsRecording)
{
this.recorder.Start(exclusiveMode: false);
}
this.loopCts = new CancellationTokenSource();
var token = this.loopCts.Token;
this.loopTask = Task.Run(() => this.ListenLoopAsync(token), CancellationToken.None);
AppLog.Info(
$"Wake word listener started (phrase \"{this.wakePhrase}\", " +
$"rolling ~{RollingBufferDuration.TotalSeconds:0.#}s buffer, " +
$"~{SlideInterval.TotalMilliseconds:0}ms slide).");
return true;
}
}
public async Task StopAsync()
{
CancellationTokenSource? cts;
Task? loop;
lock (this.sync)
{
cts = this.loopCts;
loop = this.loopTask;
this.loopCts = null;
this.loopTask = null;
}
if (cts is not null)
{
cts.Cancel();
}
if (loop is not null)
{
try
{
await loop.ConfigureAwait(false);
}
catch (OperationCanceledException)
{
}
}
cts?.Dispose();
if (this.recorder.IsRecording)
{
_ = await this.recorder.StopAsync().ConfigureAwait(false);
}
}
public async ValueTask DisposeAsync()
{
await this.StopAsync().ConfigureAwait(false);
this.recorder.Dispose();
}
private async Task ListenLoopAsync(CancellationToken cancellationToken)
{
var nextSlideAfterUtc = DateTime.MinValue;
while (!cancellationToken.IsCancellationRequested)
{
try
{
await Task.Delay(PollInterval, cancellationToken).ConfigureAwait(false);
}
catch (OperationCanceledException)
{
break;
}
// Keep a bounded rolling ring of recent PCM; do not clear between slides.
this.recorder.TrimCapturedBuffer(RollingBufferDuration);
var nowUtc = DateTime.UtcNow;
if (nowUtc < nextSlideAfterUtc)
{
continue;
}
if (!this.recorder.TryGetPcm16KhzMonoSnapshot(out var snap, out _, RollingBufferDuration) ||
snap.IsEmpty ||
snap.Duration < MinAudioForAttempt ||
!ContainsLikelySpeech(snap))
{
continue;
}
// Advance the slide clock from attempt start so STT latency does not stretch the hop
// and shrink effective overlap between consecutive windows.
nextSlideAfterUtc = nowUtc + SlideInterval;
string transcript;
try
{
transcript = await this.transcribeAsync(snap, cancellationToken).ConfigureAwait(false);
}
catch (OperationCanceledException)
{
break;
}
catch (Exception ex)
{
AppLog.Error($"Wake word transcription failed: {ex.Message}");
continue;
}
string phrase;
lock (this.sync)
{
phrase = this.wakePhrase;
}
if (!MatchesWakePhrase(transcript, phrase))
{
continue;
}
if (Interlocked.CompareExchange(ref this.detectGate, 1, 0) != 0)
{
continue;
}
try
{
AppLog.Info($"Wake phrase detected: \"{phrase}\".");
// Release the mic before dictation starts (exclusive mode especially).
if (this.recorder.IsRecording)
{
_ = await this.recorder.StopAsync().ConfigureAwait(false);
}
this.WakeDetected?.Invoke();
}
finally
{
Interlocked.Exchange(ref this.detectGate, 0);
}
break;
}
}
private static string NormalizePhrase(string? phrase)
{
var normalized = CollapseWhitespace(phrase ?? string.Empty).Trim();
return string.IsNullOrWhiteSpace(normalized)
? AppSettings.DefaultWakeWordPhrase
: normalized;
}
private static bool MatchesWakePhrase(string transcript, string phrase)
{
var text = NormalizeForMatch(transcript);
var target = NormalizeForMatch(phrase);
if (string.IsNullOrEmpty(text) || string.IsNullOrEmpty(target))
{
return false;
}
if (text.Contains(target, StringComparison.Ordinal))
{
return true;
}
var altTarget = target.Replace("okay", "ok", StringComparison.Ordinal);
var altText = text.Replace("okay", "ok", StringComparison.Ordinal);
return altText.Contains(target, StringComparison.Ordinal) ||
altText.Contains(altTarget, StringComparison.Ordinal) ||
text.Contains(altTarget, StringComparison.Ordinal);
}
private static string NormalizeForMatch(string value)
{
if (string.IsNullOrWhiteSpace(value))
{
return string.Empty;
}
var builder = new StringBuilder(value.Length);
var pendingSpace = false;
foreach (var ch in value.Trim().ToLowerInvariant())
{
if (char.IsLetterOrDigit(ch))
{
if (pendingSpace && builder.Length > 0)
{
builder.Append(' ');
}
pendingSpace = false;
builder.Append(ch);
}
else
{
pendingSpace = true;
}
}
return builder.ToString();
}
private static string CollapseWhitespace(string value)
{
var builder = new StringBuilder(value.Length);
var pendingSpace = false;
foreach (var ch in value)
{
if (char.IsWhiteSpace(ch))
{
pendingSpace = true;
continue;
}
if (pendingSpace && builder.Length > 0)
{
builder.Append(' ');
}
pendingSpace = false;
builder.Append(ch);
}
return builder.ToString();
}
private static bool ContainsLikelySpeech(PcmAudioBuffer audio)
{
var bytes = audio.Pcm16KhzMono;
if (bytes.Length < 4)
{
return false;
}
const int frameSampleCount = 1_600;
const int requiredSpeechFrames = 2;
var sampleCount = bytes.Length / 2;
var speechFrames = 0;
for (var frameStart = 0; frameStart < sampleCount; frameStart += frameSampleCount)
{
var frameLength = Math.Min(frameSampleCount, sampleCount - frameStart);
if (frameLength <= 0)
{
continue;
}
double sumSquares = 0;
var byteStart = frameStart * 2;
for (var i = 0; i < frameLength; i++)
{
var sample = BitConverter.ToInt16(bytes, byteStart + (i * 2));
var normalized = sample / 32768.0;
sumSquares += normalized * normalized;
}
var rms = Math.Sqrt(sumSquares / frameLength);
if (rms < MinSpeechRmsThreshold)
{
continue;
}
speechFrames++;
if (speechFrames >= requiredSpeechFrames)
{
return true;
}
}
return false;
}
}