-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVoiceListenerStatusCenter.cs
More file actions
107 lines (96 loc) · 2.96 KB
/
VoiceListenerStatusCenter.cs
File metadata and controls
107 lines (96 loc) · 2.96 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
using System;
namespace TimeTask
{
internal sealed class VoiceRecognitionRecord
{
public string Text { get; set; }
public float Confidence { get; set; }
public string Source { get; set; }
public DateTime CapturedAtUtc { get; set; }
public byte[] AudioPcm16Mono { get; set; }
}
internal enum VoiceListenerState
{
Unknown = 0,
Installing = 1,
Loading = 2,
Unavailable = 3,
Ready = 4,
Recognizing = 5
}
internal sealed class VoiceListenerStatus
{
public VoiceListenerState State { get; set; }
public string Message { get; set; }
public DateTime UpdatedAtUtc { get; set; }
}
internal static class VoiceListenerStatusCenter
{
private static readonly object Sync = new object();
private static VoiceListenerStatus _current = new VoiceListenerStatus
{
State = VoiceListenerState.Unknown,
Message = "语音状态未知",
UpdatedAtUtc = DateTime.UtcNow
};
public static event EventHandler<VoiceListenerStatus> StatusChanged;
public static event EventHandler<VoiceRecognitionRecord> RecognitionCaptured;
public static VoiceListenerStatus Current
{
get
{
lock (Sync)
{
return new VoiceListenerStatus
{
State = _current.State,
Message = _current.Message,
UpdatedAtUtc = _current.UpdatedAtUtc
};
}
}
}
public static void Publish(VoiceListenerState state, string message)
{
VoiceListenerStatus snapshot;
lock (Sync)
{
_current = new VoiceListenerStatus
{
State = state,
Message = message ?? string.Empty,
UpdatedAtUtc = DateTime.UtcNow
};
snapshot = new VoiceListenerStatus
{
State = _current.State,
Message = _current.Message,
UpdatedAtUtc = _current.UpdatedAtUtc
};
}
try
{
StatusChanged?.Invoke(null, snapshot);
}
catch
{
// Keep status center fire-and-forget to avoid breaking runtime logic.
}
}
public static void PublishRecognition(VoiceRecognitionRecord record)
{
if (record == null || string.IsNullOrWhiteSpace(record.Text))
{
return;
}
try
{
RecognitionCaptured?.Invoke(null, record);
}
catch
{
// Keep fire-and-forget to avoid impacting recognition loop.
}
}
}
}