/
Speechs.razor
282 lines (251 loc) · 9.92 KB
/
Speechs.razor
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
@page "/Speechs"
@attribute [TabItemOption(Text = "语音识别/合成")]
@inherits AppComponentBase
@using System.ComponentModel
<a href="https://www.nuget.org/packages/BootstrapBlazor.WebApi#readme-body-tab"><h3>语音识别/合成</h3></a>
<hr />
<a href="https://github.com/densen2014/Densen.Extensions/blob/master/Demo/DemoShared/Pages/Speechs.razor"><h5>页面源码</h5></a>
<div class="row g-3 mb-3">
<h4>语音识别</h4>
<div class="col-12">
<BootstrapInputGroup>
<Button Text="普通话" OnClick="SpeechRecognition" IsAsync />
<Button Text="粤语" OnClick="SpeechRecognitionHK" IsAsync />
<Button Text="英文" OnClick="SpeechRecognitionEN" IsAsync />
<Button Text="西文" OnClick="SpeechRecognitionES" IsAsync />
<Button Text="停止" OnClick="SpeechRecognitionStop" Icon="fa-fw fa-solid fa-stop" IsAsync />
</BootstrapInputGroup>
</div>
<div>
<Checkbox TValue="bool" ShowLabel="true" @bind-Value="@Options.Continuous" />
</div>
<div>
<Checkbox TValue="bool" ShowLabel="true" @bind-Value="@Options.InterimResults" />
</div>
<div style="width: 160px;">
<BootstrapInputNumber ShowLabel="true" ShowButton="true" @bind-Value="@Options.MaxAlternatives" Max="10" Min="1" />
</div>
<div class="col-12">
<Textarea Value="@Result" />
</div>
</div>
@if (WebSpeech != null && WebSpeech.IsBusy)
{
<Spinner />
}
<h4>语音颜色选择游戏</h4>
<div class="col-12">
<BootstrapInputGroup>
<Button Text="普通话" OnClick="SpeechRecognitionDemo" IsAsync />
<Button Text="粤语" OnClick="SpeechRecognitionHKDemo" IsAsync />
<Button Text="英文" OnClick="SpeechRecognitionENDemo" IsAsync />
<Button Text="西文" OnClick="SpeechRecognitionESDemo" IsAsync />
<Button Text="停止" OnClick="SpeechRecognitionStop" Icon="fa-fw fa-solid fa-stop" IsAsync />
</BootstrapInputGroup>
</div>
<p class="hints"></p>
<div>
<p class="output"><em>...</em></p>
</div>
<div class="row g-3 mb-3">
<h4>语音合成</h4>
<div class="col-12">
<BootstrapInputGroup>
<Button Text="普通话" OnClick="SpeechSynthesis" IsAsync />
<Button Text="粤语" OnClick="SpeechSynthesisHK" IsAsync />
<Button Text="英文" OnClick="SpeechSynthesisEN" IsAsync />
<Button Text="西文" OnClick="SpeechSynthesisES" IsAsync />
<Button Text="停止" OnClick="SpeechStop" Icon="fa-fw fa-solid fa-stop" IsAsync />
</BootstrapInputGroup>
</div>
<div class="col-12">
<WebSpeech @ref="WebSpeech" OnResult="@OnResult" OnIsBusy="@OnIsBusy" OnStatus="@OnStatus" OnError="@OnError" />
</div>
<p>
@Message
</p>
</div>
<div class="row g-3 mb-3">
<h4>语音合成自定义</h4>
<div class="col-12">
@if (WebVoiceList != null && WebVoiceList.Any())
{
<select id="voiceSelect" class="form-select" @onchange="((e)=>OnChange(e))" style="width:70vw; max-width: 400px;">
@foreach (var voice in WebVoiceList)
{
<option value="@voice.VoiceURI">@($"{(voice.LocalService ? "" : "*")}{voice.Name} ({voice.Lang})")</option>
}
</select>
}
</div>
<div class="col-10">
速率
<input type="range" min="0.1" max="10" step="0.1" style="width:70vw; max-width: 400px;" @bind-value="Options2.Rate" />
</div>
<div class="col-10">
音高
<input type="range" min="0" max="2" step="0.1" style="width: 70vw; max-width: 400px; " @bind-value="Options2.Picth">
</div>
<div class="col-10">
音量
<input type="range" min="0" max="1" step="0.01" style="width: 70vw; max-width: 400px; " @bind-value="Options2.Volume" />
</div>
<div class="col-12">
<Textarea @bind-Value="SpeakText" />
</div>
<div class="col-12">
<BootstrapInputGroup>
<Button Text="测试" OnClick="SpeechSynthesisDIY" Icon="fa-fw fa-solid fa-play" IsAsync />
<Button Text="停止" OnClick="SpeechStop" Icon="fa-fw fa-solid fa-stop" IsAsync />
<Button Text="语音列表" OnClick="GetVoiceList" IsAsync />
</BootstrapInputGroup>
</div>
</div>
<AttributeTable Items="@GetAttributes()" />
@code{
[NotNull]
WebSpeech? WebSpeech { get; set; }
[DisplayName("识别结果")]
string? Result { get; set; } = "";
new string? Message { get; set; } = "";
[DisplayName("内容")]
private string SpeakText { get; set; } = "我们一直与Blazor同行";
private string? SelectLang { get; set; }
SpeechRecognitionOption Options { get; set; } = new SpeechRecognitionOption();
SpeechSynthesisOption Options2 { get; set; } = new SpeechSynthesisOption();
async Task SpeechRecognition() => Message = await WebSpeech.SpeechRecognition(option: Options);
async Task SpeechRecognitionHK() => Message = await WebSpeech.SpeechRecognition("zh-HK", Options);
async Task SpeechRecognitionEN() => Message = await WebSpeech.SpeechRecognition("en-US", Options);
async Task SpeechRecognitionES() => Message = await WebSpeech.SpeechRecognition("es-ES", Options);
async Task SpeechRecognitionStop() => await WebSpeech.SpeechRecognitionStop();
async Task SpeechRecognitionDemo() => Message = await WebSpeech.SpeechRecognitionDemo();
async Task SpeechRecognitionHKDemo() => Message = await WebSpeech.SpeechRecognitionDemo("zh-HK");
async Task SpeechRecognitionENDemo() => Message = await WebSpeech.SpeechRecognitionDemo("en-US");
async Task SpeechRecognitionESDemo() => Message = await WebSpeech.SpeechRecognitionDemo("es-ES");
async Task SpeechRecognitionStopDemo() => await WebSpeech.SpeechRecognitionStop();
async Task SpeechSynthesis() => await WebSpeech.SpeechSynthesis("你好 blazor,现在是" + NowString());
async Task SpeechSynthesisHK() => await WebSpeech.SpeechSynthesis("早晨 blazor,依家系 " + NowString(), "zh-HK");
async Task SpeechSynthesisEN() => await WebSpeech.SpeechSynthesis("Hello blazor,now is " + NowString(), "en-US");
async Task SpeechSynthesisES() => await WebSpeech.SpeechSynthesis("Hola blazor,ahora es " + NowString(), "es-ES");
async Task SpeechSynthesisDIY() => await WebSpeech.SpeechSynthesis(SpeakText, Options2, "", SelectLang ?? WebVoiceList?.FirstOrDefault()?.VoiceURI);
async Task SpeechStop() => await WebSpeech.SpeechStop();
string NowString() => DateTime.Now.ToShortTimeString();
List<WebVoice>? WebVoiceList { get; set; }
async Task GetVoiceList()
{
WebVoiceList = await WebSpeech.GetVoiceList();
if (WebVoiceList != null && WebVoiceList.Any()) StateHasChanged();
}
private Task OnIsBusy(bool flag)
{
StateHasChanged();
return Task.CompletedTask;
}
private void OnChange(ChangeEventArgs val)
{
if (val?.Value != null) SelectLang = val.Value.ToString();
}
private Task OnResult(string message)
{
Result = message;
StateHasChanged();
return Task.CompletedTask;
}
private async Task OnStatus(string message)
{
Message = message;
await ShowBottomMessage(message);
}
private async Task OnError(string message)
{
Message = message;
await ShowBottomMessage(message, true);
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
await Task.Delay(500);
await Task.Delay(1500);
while (WebVoiceList == null || !WebVoiceList.Any())
{
await Task.Delay(100);
await GetVoiceList();
if (WebSpeech.SpeechUndefined)
{
return;
}
}
}
}
/// <summary>
/// 获得属性方法
/// </summary>
/// <returns></returns>
protected IEnumerable<AttributeItem> GetAttributes() => new AttributeItem[]
{
new AttributeItem() {
Name = "SpeechRecognition",
Description = "语音识别",
Type = "Task<string>",
ValueList = "",
DefaultValue = "-"
},
new AttributeItem() {
Name = "SpeechRecognitionStop",
Description = "停止语音识别",
Type = "Task",
ValueList = "",
DefaultValue = "-"
},
new AttributeItem() {
Name = "SpeechSynthesis",
Description = "语音合成",
Type = "Task",
ValueList = "",
DefaultValue = "-"
},
new AttributeItem() {
Name = "SpeechStop",
Description = "停止语音合成",
Type = "Task",
ValueList = "",
DefaultValue = "-"
},
new AttributeItem() {
Name = "OnResult",
Description = "识别完成回调方法",
Type = "Func<string, Task>?",
ValueList = "-",
DefaultValue = "-"
},
new AttributeItem() {
Name = "OnIsBusy",
Description = "工作状态回调方法",
Type = "Func<bool, Task>?",
ValueList = "-",
DefaultValue = "-"
},
new AttributeItem() {
Name = "OnStatus",
Description = "状态信息回调方法",
Type = "Func<string, Task>?",
ValueList = "-",
DefaultValue = "-"
},
new AttributeItem() {
Name = "OnError",
Description = "错误回调方法",
Type = "Func<string, Task>?",
ValueList = "-",
DefaultValue = "-"
},
new AttributeItem() {
Name = "Element",
Description = "UI界面元素的引用对象,为空则使用整个页面",
Type = "ElementReference",
ValueList = "-",
DefaultValue = "-"
},
};
}