-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAudioCaptureService.cs
More file actions
397 lines (355 loc) · 14.8 KB
/
AudioCaptureService.cs
File metadata and controls
397 lines (355 loc) · 14.8 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
using System;
using System.IO;
using System.Speech.Recognition;
using System.Timers;
using NAudio.Wave;
using NAudio.CoreAudioApi;
namespace TimeTask
{
/// <summary>
/// 后台智能人声录音服务:
/// - 持续监听麦克风。
/// - 当识别到“有意义的句子”(语音识别置信度阈值)时开始录音。
/// - 当超过指定静默时长(默认30秒)未检测到人声讲话时停止录音。
/// - 音频保存为 WAV 文件到 Recordings/ 目录。
/// </summary>
public sealed class AudioCaptureService : IDisposable
{
private readonly object _lock = new object();
private readonly TimeSpan _silenceTimeout;
private readonly float _confidenceThreshold;
private SpeechRecognitionEngine _recognizer;
private DateTime _lastSpeechTime = DateTime.MinValue;
private Timer _silenceTimer;
private WaveInEvent _waveIn;
private WasapiCapture _wasapi;
private WaveFileWriter _writer;
private string _currentFilePath;
private bool _enabled;
// 简单 VAD 与去抖动参数
private readonly double _energyThresholdDb = -35.0; // 能量阈值(dBFS),越高越“严格”
private readonly int _minStartMs = 400; // 连续高能量达到此时长才开始录音
private readonly int _hangoverMs = 1200; // 录音进行中,连续低能量达到此时长才停止
private int _consecAboveMs = 0;
private int _consecBelowMs = 0;
private DateTime _lastAudioStateSpeech = DateTime.MinValue;
public AudioCaptureService(TimeSpan? silenceTimeout = null, float confidenceThreshold = 0.4f)
{
_silenceTimeout = silenceTimeout ?? TimeSpan.FromSeconds(30);
_confidenceThreshold = confidenceThreshold;
}
/// <summary>
/// 启动持续监听。
/// </summary>
public void Start()
{
lock (_lock)
{
if (_enabled) return;
_enabled = true;
// 初始化语音识别(使用系统离线识别器 + 自由听写语法)
_recognizer = new SpeechRecognitionEngine();
_recognizer.SetInputToDefaultAudioDevice();
_recognizer.LoadGrammar(new DictationGrammar());
// 可选:适当放宽杂音/静音设置,提升连贯监听体验
TryUpdateRecognizerSetting("BabbleTimeout", TimeSpan.FromSeconds(2));
TryUpdateRecognizerSetting("InitialSilenceTimeout", TimeSpan.FromSeconds(5));
_recognizer.SpeechRecognized += OnSpeechRecognized;
_recognizer.SpeechHypothesized += OnSpeechHypothesized;
_recognizer.AudioStateChanged += OnAudioStateChanged;
_recognizer.RecognizeAsync(RecognizeMode.Multiple);
_silenceTimer = new Timer(1000);
_silenceTimer.Elapsed += OnSilenceTick;
_silenceTimer.AutoReset = true;
_silenceTimer.Start();
}
}
/// <summary>
/// 停止监听并清理资源。
/// </summary>
public void Stop()
{
lock (_lock)
{
_enabled = false;
try { _silenceTimer?.Stop(); } catch { }
if (_recognizer != null)
{
try { _recognizer.RecognizeAsyncCancel(); } catch { }
try { _recognizer.RecognizeAsyncStop(); } catch { }
_recognizer.SpeechRecognized -= OnSpeechRecognized;
_recognizer.SpeechHypothesized -= OnSpeechHypothesized;
_recognizer.AudioStateChanged -= OnAudioStateChanged;
_recognizer.Dispose();
_recognizer = null;
}
StopRecordingInternal();
_silenceTimer?.Dispose();
_silenceTimer = null;
}
}
private void OnSpeechHypothesized(object sender, SpeechHypothesizedEventArgs e)
{
// 任何假设事件都视为存在讲话,刷新说话时间(避免短暂停顿导致过早停止)
_lastSpeechTime = DateTime.UtcNow;
}
private void OnAudioStateChanged(object sender, AudioStateChangedEventArgs e)
{
if (e.AudioState == AudioState.Speech)
{
// 仅记录最近的语音状态时间,用于与能量判定共同决定是否开录
_lastAudioStateSpeech = DateTime.UtcNow;
}
}
private void OnSpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
if (e == null || e.Result == null) return;
var confidence = e.Result.Confidence;
var text = e.Result.Text;
// 认为是“有意义的句子”的简单规则:长度>=3,且置信度达到阈值
if (!string.IsNullOrWhiteSpace(text) && text.Trim().Length >= 3 && confidence >= _confidenceThreshold)
{
_lastSpeechTime = DateTime.UtcNow;
// 确保录音已启动
EnsureRecording();
}
}
private void OnSilenceTick(object sender, ElapsedEventArgs e)
{
if (!_enabled) return;
var now = DateTime.UtcNow;
// 超过静默阈值则停止录音
if (_writer != null && now - _lastSpeechTime > _silenceTimeout)
{
lock (_lock)
{
if (_writer != null && now - _lastSpeechTime > _silenceTimeout)
{
StopRecordingInternal();
}
}
}
}
private void EnsureRecording()
{
lock (_lock)
{
if (_writer != null) return;
Directory.CreateDirectory(GetRecordingDir());
_currentFilePath = Path.Combine(GetRecordingDir(),
$"recording_{DateTime.Now:yyyyMMdd_HHmmss}.wav");
// 首选 MME (WaveIn),采样率 44100Hz 单声道 16bit,兼容性更好
try
{
_waveIn = new WaveInEvent
{
WaveFormat = new WaveFormat(44100, 16, 1),
BufferMilliseconds = 200
};
_waveIn.DataAvailable += OnWaveData;
_waveIn.RecordingStopped += OnRecordingStopped;
_writer = new WaveFileWriter(_currentFilePath, _waveIn.WaveFormat);
_waveIn.StartRecording();
return;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"WaveIn start failed, fallback to WASAPI: {ex.Message}");
try
{
if (_waveIn != null)
{
try { _waveIn.DataAvailable -= OnWaveData; } catch { }
try { _waveIn.RecordingStopped -= OnRecordingStopped; } catch { }
_waveIn.Dispose();
}
}
catch { }
_waveIn = null;
}
// 回退:WASAPI 共享模式,使用系统默认录音设备
try
{
var enumerator = new MMDeviceEnumerator();
var device = enumerator.GetDefaultAudioEndpoint(DataFlow.Capture, Role.Multimedia);
_wasapi = new WasapiCapture(device);
_wasapi.ShareMode = AudioClientShareMode.Shared;
_wasapi.DataAvailable += OnWaveData;
_wasapi.RecordingStopped += OnRecordingStopped;
_writer = new WaveFileWriter(_currentFilePath, _wasapi.WaveFormat);
_wasapi.StartRecording();
}
catch (Exception ex2)
{
System.Diagnostics.Debug.WriteLine($"WASAPI start failed: {ex2}");
// 启动失败,清理写入器
if (_writer != null) { try { _writer.Dispose(); } catch { } _writer = null; }
_currentFilePath = null;
}
}
}
private void StopRecordingInternal()
{
try
{
if (_waveIn != null)
{
try { _waveIn.StopRecording(); } catch { }
}
if (_wasapi != null)
{
try { _wasapi.StopRecording(); } catch { }
}
}
catch { }
}
private void OnRecordingStopped(object sender, StoppedEventArgs e)
{
lock (_lock)
{
try
{
if (_waveIn != null)
{
_waveIn.DataAvailable -= OnWaveData;
_waveIn.RecordingStopped -= OnRecordingStopped;
}
if (_wasapi != null)
{
_wasapi.DataAvailable -= OnWaveData;
_wasapi.RecordingStopped -= OnRecordingStopped;
}
}
catch { }
_waveIn?.Dispose();
_waveIn = null;
_wasapi?.Dispose();
_wasapi = null;
if (_writer != null)
{
try { _writer.Flush(); } catch { }
try { _writer.Dispose(); } catch { }
_writer = null;
}
_currentFilePath = null;
}
}
private void OnWaveData(object sender, WaveInEventArgs e)
{
try
{
ProcessAudioBuffer(e.Buffer, e.BytesRecorded, (_waveIn != null) ? _waveIn.WaveFormat : (_wasapi != null ? _wasapi.WaveFormat : null));
}
catch
{
StopRecordingInternal();
}
}
private void ProcessAudioBuffer(byte[] buffer, int bytesRecorded, WaveFormat format)
{
if (format == null || bytesRecorded <= 0) return;
// 计算帧时长(毫秒)
int bytesPerMs = (format.AverageBytesPerSecond / 1000);
int frameMs = Math.Max(1, bytesRecorded / Math.Max(1, bytesPerMs));
// 计算当前缓冲的 dBFS
double db = ComputeDbfs(buffer, bytesRecorded, format);
bool above = db >= _energyThresholdDb;
if (above)
{
_consecAboveMs += frameMs;
_consecBelowMs = 0;
_lastSpeechTime = DateTime.UtcNow; // 只要高能量即刷新“最后讲话时间”
}
else
{
_consecBelowMs += frameMs;
_consecAboveMs = 0;
}
bool recentAudioSpeech = (DateTime.UtcNow - _lastAudioStateSpeech) < TimeSpan.FromSeconds(1.0);
// 开始录音条件:最近识别到语音状态 且 高能量持续达到阈值 或者 识别结果强置信度文本(由 OnSpeechRecognized 触发 EnsureRecording)
if (_writer == null)
{
if (recentAudioSpeech && _consecAboveMs >= _minStartMs)
{
EnsureRecording();
}
}
else
{
// 停止录音条件:持续低能量达到挂起时间(更快停录),或由 30s 静默定时器兜底
if (_consecBelowMs >= _hangoverMs)
{
StopRecordingInternal();
}
}
// 若正在录音,写入缓冲
if (_writer != null)
{
try
{
_writer.Write(buffer, 0, bytesRecorded);
}
catch { StopRecordingInternal(); }
}
}
private static double ComputeDbfs(byte[] buffer, int bytes, WaveFormat format)
{
try
{
if (format.Encoding == WaveFormatEncoding.IeeeFloat && format.BitsPerSample == 32)
{
int samples = bytes / 4; // 32-bit float
if (samples == 0) return double.NegativeInfinity;
double sumSq = 0;
for (int i = 0; i < bytes; i += 4)
{
float sample = BitConverter.ToSingle(buffer, i);
sumSq += (double)sample * sample;
}
double rms = Math.Sqrt(sumSq / samples);
double dbfs = 20.0 * Math.Log10(rms + 1e-12);
return (!double.IsNaN(dbfs) && !double.IsInfinity(dbfs)) ? dbfs : double.NegativeInfinity;
}
else if (format.BitsPerSample == 16)
{
int samples = bytes / 2;
if (samples == 0) return double.NegativeInfinity;
double sumSq = 0;
for (int i = 0; i < bytes; i += 2)
{
short s = BitConverter.ToInt16(buffer, i);
double norm = s / 32768.0; // -1..1
sumSq += norm * norm;
}
double rms = Math.Sqrt(sumSq / samples);
double dbfs = 20.0 * Math.Log10(rms + 1e-12);
return (!double.IsNaN(dbfs) && !double.IsInfinity(dbfs)) ? dbfs : double.NegativeInfinity;
}
else
{
// 其它位深简单回退:按字节归一
double sum = 0;
for (int i = 0; i < bytes; i++) sum += Math.Abs(buffer[i] - 128);
double avg = sum / bytes / 128.0; // 0..1
double dbfs = 20.0 * Math.Log10(avg + 1e-12);
return (!double.IsNaN(dbfs) && !double.IsInfinity(dbfs)) ? dbfs : double.NegativeInfinity;
}
}
catch { return double.NegativeInfinity; }
}
private static string GetRecordingDir()
{
var baseDir = AppDomain.CurrentDomain.BaseDirectory;
return Path.Combine(baseDir, "Recordings");
}
private void TryUpdateRecognizerSetting(string name, TimeSpan value)
{
try { _recognizer.UpdateRecognizerSetting(name, (int)value.TotalMilliseconds); }
catch { /* 某些设置可能不被实现,忽略 */ }
}
public void Dispose()
{
Stop();
}
}
}