-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPicovoiceHandler.cs
104 lines (97 loc) · 3.35 KB
/
PicovoiceHandler.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using Pv;
public class PicovoiceHandler
{
private readonly string _accessKey;
private readonly int _led1Pin;
private readonly int _led2Pin;
// private readonly GpioController _gpioController;
private Picovoice? _picovoice;
private readonly List<string> _keywordPaths;
private readonly List<float> _sensitivities;
private readonly int _audioDeviceIndex;
private bool _isRunning;
public event Func<Task> WakeWordDetected;
public PicovoiceHandler(string accessKey, int led1Pin, int led2Pin, List<string> keywordPaths, List<float> sensitivities, int audioDeviceIndex)
{
_accessKey = accessKey;
_led1Pin = led1Pin;
_led2Pin = led2Pin;
_keywordPaths = keywordPaths;
_sensitivities = sensitivities;
_audioDeviceIndex = audioDeviceIndex;
}
public void Start()
{
_isRunning = true;
Task.Run(() => ListenForWakeWord());
}
public void Stop()
{
_isRunning = false;
}
private async Task ListenForWakeWord()
{
using (Porcupine porcupine = Porcupine.FromKeywordPaths(_accessKey, _keywordPaths, sensitivities: _sensitivities))
{
using (PvRecorder recorder = PvRecorder.Create(frameLength: porcupine.FrameLength, deviceIndex: _audioDeviceIndex))
{
recorder.Start();
while (_isRunning)
{
short[] frame = recorder.Read();
int result = porcupine.Process(frame);
if (result >= 0)
{
Console.WriteLine($"[{DateTime.Now.ToLongTimeString()}] Detected '{Path.GetFileNameWithoutExtension(_keywordPaths[result])}'");
if (WakeWordDetected != null)
{
await WakeWordDetected.Invoke();
}
}
Thread.Yield();
}
recorder.Stop();
}
}
}
// private async Task OnWakeWordDetected()
// {
// // Simulate wake word detection event
// Console.WriteLine("Wake word detected.");
// if (WakeWordDetected != null)
// {
// await WakeWordDetected.Invoke();
// }
// }
// public async Task FadeLedsAsync()
// {
// if (_pwmController1 == null || _pwmController2 == null)
// {
// _pwmController1 = new PwmController(_gpioController, _led1Pin);
// _pwmController2 = new PwmController(_gpioController, _led2Pin);
// }
// // Perform fade out
// await _pwmController1.FadeAsync(2000); // Fade duration in milliseconds
// await _pwmController2.FadeAsync(2000); // Fade duration in milliseconds
// }
// public void Stop()
// {
// if (_cancellationTokenSource != null)
// {
// _cancellationTokenSource.Cancel();
// if (_listeningTask != null)
// {
// _listeningTask.Wait();
// }
// _cancellationTokenSource.Dispose();
// _cancellationTokenSource = null!;
// _listeningTask = null;
// }
// Console.WriteLine("PicovoiceHandler stopped.");
// }
}