回答編集履歴
2
内容追記
test
CHANGED
@@ -41,3 +41,353 @@
|
|
41
41
|
|
42
42
|
|
43
43
|
途中でわからないことがあれば言ってください。アドバイスできると思います。
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
---
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
追記:Azure Cognitive Speech SDK 導入方法
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
自分のアカウントのAzure上で Cognitive Services を立てている前提です。
|
56
|
+
|
57
|
+
立て方は上記のQiita記事を参考にしてください。
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
1, Microsoftさん公式のUnityのサンプルプロジェクトをgit cloneする。
|
62
|
+
|
63
|
+
[https://github.com/Azure-Samples/cognitive-services-speech-sdk/tree/master/quickstart/csharp/unity/from-microphone](https://github.com/Azure-Samples/cognitive-services-speech-sdk/tree/master/quickstart/csharp/unity/from-microphone)
|
64
|
+
|
65
|
+
※これをzipでDLして quickstart/csharp/unity/from-microphone でもよい。
|
66
|
+
|
67
|
+
[https://github.com/Azure-Samples/cognitive-services-speech-sdk](https://github.com/Azure-Samples/cognitive-services-speech-sdk)
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
2, `Microsoft.CognitiveServices.Speech.xxx.unitypackage` をDLしてインポートする[https://aka.ms/csspeech/unitypackage](https://aka.ms/csspeech/unitypackage)
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
3, Assets/Scripts/`HelloWorld.cs` を下の内容に書き換える。
|
76
|
+
|
77
|
+
変更点は、キー等の情報をスクリプトに直接記入するようになっていたので[SerializeField]でEditor上から入力できるようにした。あと日本語対応。
|
78
|
+
|
79
|
+
```C#
|
80
|
+
|
81
|
+
//
|
82
|
+
|
83
|
+
// Copyright (c) Microsoft. All rights reserved.
|
84
|
+
|
85
|
+
// Licensed under the MIT license. See LICENSE.md file in the project root for full license information.
|
86
|
+
|
87
|
+
//
|
88
|
+
|
89
|
+
// <code>
|
90
|
+
|
91
|
+
using UnityEngine;
|
92
|
+
|
93
|
+
using UnityEngine.UI;
|
94
|
+
|
95
|
+
using Microsoft.CognitiveServices.Speech;
|
96
|
+
|
97
|
+
#if PLATFORM_ANDROID
|
98
|
+
|
99
|
+
using UnityEngine.Android;
|
100
|
+
|
101
|
+
#endif
|
102
|
+
|
103
|
+
#if PLATFORM_IOS
|
104
|
+
|
105
|
+
using UnityEngine.iOS;
|
106
|
+
|
107
|
+
using System.Collections;
|
108
|
+
|
109
|
+
#endif
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
public class HelloWorld : MonoBehaviour
|
114
|
+
|
115
|
+
{
|
116
|
+
|
117
|
+
// Hook up the two properties below with a Text and Button object in your UI.
|
118
|
+
|
119
|
+
public Text outputText;
|
120
|
+
|
121
|
+
public Button startRecoButton;
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
private object threadLocker = new object();
|
126
|
+
|
127
|
+
private bool waitingForReco;
|
128
|
+
|
129
|
+
private string message;
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
private bool micPermissionGranted = false;
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
#if PLATFORM_ANDROID || PLATFORM_IOS
|
138
|
+
|
139
|
+
// Required to manifest microphone permission, cf.
|
140
|
+
|
141
|
+
// https://docs.unity3d.com/Manual/android-manifest.html
|
142
|
+
|
143
|
+
private Microphone mic;
|
144
|
+
|
145
|
+
#endif
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
[SerializeField] string subscriptionKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
|
150
|
+
|
151
|
+
[SerializeField] string serviceRegion = "japaneast";
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
public async void ButtonClick()
|
156
|
+
|
157
|
+
{
|
158
|
+
|
159
|
+
// Creates an instance of a speech config with specified subscription key and service region.
|
160
|
+
|
161
|
+
// Replace with your own subscription key and service region (e.g., "westus").
|
162
|
+
|
163
|
+
var config = SpeechConfig.FromSubscription(subscriptionKey, serviceRegion);
|
164
|
+
|
165
|
+
var lang = SourceLanguageConfig.FromLanguage("ja-JP");
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
// Make sure to dispose the recognizer after use!
|
170
|
+
|
171
|
+
using (var recognizer = new SpeechRecognizer(config, lang))
|
172
|
+
|
173
|
+
{
|
174
|
+
|
175
|
+
lock (threadLocker)
|
176
|
+
|
177
|
+
{
|
178
|
+
|
179
|
+
waitingForReco = true;
|
180
|
+
|
181
|
+
}
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
// Starts speech recognition, and returns after a single utterance is recognized. The end of a
|
186
|
+
|
187
|
+
// single utterance is determined by listening for silence at the end or until a maximum of 15
|
188
|
+
|
189
|
+
// seconds of audio is processed. The task returns the recognition text as result.
|
190
|
+
|
191
|
+
// Note: Since RecognizeOnceAsync() returns only a single utterance, it is suitable only for single
|
192
|
+
|
193
|
+
// shot recognition like command or query.
|
194
|
+
|
195
|
+
// For long-running multi-utterance recognition, use StartContinuousRecognitionAsync() instead.
|
196
|
+
|
197
|
+
var result = await recognizer.RecognizeOnceAsync().ConfigureAwait(false);
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
// Checks result.
|
202
|
+
|
203
|
+
string newMessage = string.Empty;
|
204
|
+
|
205
|
+
if (result.Reason == ResultReason.RecognizedSpeech)
|
206
|
+
|
207
|
+
{
|
208
|
+
|
209
|
+
newMessage = result.Text;
|
210
|
+
|
211
|
+
}
|
212
|
+
|
213
|
+
else if (result.Reason == ResultReason.NoMatch)
|
214
|
+
|
215
|
+
{
|
216
|
+
|
217
|
+
newMessage = "NOMATCH: Speech could not be recognized.";
|
218
|
+
|
219
|
+
}
|
220
|
+
|
221
|
+
else if (result.Reason == ResultReason.Canceled)
|
222
|
+
|
223
|
+
{
|
224
|
+
|
225
|
+
var cancellation = CancellationDetails.FromResult(result);
|
226
|
+
|
227
|
+
newMessage = $"CANCELED: Reason={cancellation.Reason} ErrorDetails={cancellation.ErrorDetails}";
|
228
|
+
|
229
|
+
}
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
lock (threadLocker)
|
234
|
+
|
235
|
+
{
|
236
|
+
|
237
|
+
message = newMessage;
|
238
|
+
|
239
|
+
waitingForReco = false;
|
240
|
+
|
241
|
+
}
|
242
|
+
|
243
|
+
}
|
244
|
+
|
245
|
+
}
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
void Start()
|
250
|
+
|
251
|
+
{
|
252
|
+
|
253
|
+
if (outputText == null)
|
254
|
+
|
255
|
+
{
|
256
|
+
|
257
|
+
UnityEngine.Debug.LogError("outputText property is null! Assign a UI Text element to it.");
|
258
|
+
|
259
|
+
}
|
260
|
+
|
261
|
+
else if (startRecoButton == null)
|
262
|
+
|
263
|
+
{
|
264
|
+
|
265
|
+
message = "startRecoButton property is null! Assign a UI Button to it.";
|
266
|
+
|
267
|
+
UnityEngine.Debug.LogError(message);
|
268
|
+
|
269
|
+
}
|
270
|
+
|
271
|
+
else
|
272
|
+
|
273
|
+
{
|
274
|
+
|
275
|
+
// Continue with normal initialization, Text and Button objects are present.
|
276
|
+
|
277
|
+
#if PLATFORM_ANDROID
|
278
|
+
|
279
|
+
// Request to use the microphone, cf.
|
280
|
+
|
281
|
+
// https://docs.unity3d.com/Manual/android-RequestingPermissions.html
|
282
|
+
|
283
|
+
message = "Waiting for mic permission";
|
284
|
+
|
285
|
+
if (!Permission.HasUserAuthorizedPermission(Permission.Microphone))
|
286
|
+
|
287
|
+
{
|
288
|
+
|
289
|
+
Permission.RequestUserPermission(Permission.Microphone);
|
290
|
+
|
291
|
+
}
|
292
|
+
|
293
|
+
#elif PLATFORM_IOS
|
294
|
+
|
295
|
+
if (!Application.HasUserAuthorization(UserAuthorization.Microphone))
|
296
|
+
|
297
|
+
{
|
298
|
+
|
299
|
+
Application.RequestUserAuthorization(UserAuthorization.Microphone);
|
300
|
+
|
301
|
+
}
|
302
|
+
|
303
|
+
#else
|
304
|
+
|
305
|
+
micPermissionGranted = true;
|
306
|
+
|
307
|
+
message = "Click button to recognize speech";
|
308
|
+
|
309
|
+
#endif
|
310
|
+
|
311
|
+
startRecoButton.onClick.AddListener(ButtonClick);
|
312
|
+
|
313
|
+
}
|
314
|
+
|
315
|
+
}
|
316
|
+
|
317
|
+
|
318
|
+
|
319
|
+
void Update()
|
320
|
+
|
321
|
+
{
|
322
|
+
|
323
|
+
#if PLATFORM_ANDROID
|
324
|
+
|
325
|
+
if (!micPermissionGranted && Permission.HasUserAuthorizedPermission(Permission.Microphone))
|
326
|
+
|
327
|
+
{
|
328
|
+
|
329
|
+
micPermissionGranted = true;
|
330
|
+
|
331
|
+
message = "Click button to recognize speech";
|
332
|
+
|
333
|
+
}
|
334
|
+
|
335
|
+
#elif PLATFORM_IOS
|
336
|
+
|
337
|
+
if (!micPermissionGranted && Application.HasUserAuthorization(UserAuthorization.Microphone))
|
338
|
+
|
339
|
+
{
|
340
|
+
|
341
|
+
micPermissionGranted = true;
|
342
|
+
|
343
|
+
message = "Click button to recognize speech";
|
344
|
+
|
345
|
+
}
|
346
|
+
|
347
|
+
#endif
|
348
|
+
|
349
|
+
|
350
|
+
|
351
|
+
lock (threadLocker)
|
352
|
+
|
353
|
+
{
|
354
|
+
|
355
|
+
if (startRecoButton != null)
|
356
|
+
|
357
|
+
{
|
358
|
+
|
359
|
+
startRecoButton.interactable = !waitingForReco && micPermissionGranted;
|
360
|
+
|
361
|
+
}
|
362
|
+
|
363
|
+
if (outputText != null)
|
364
|
+
|
365
|
+
{
|
366
|
+
|
367
|
+
outputText.text = message;
|
368
|
+
|
369
|
+
}
|
370
|
+
|
371
|
+
}
|
372
|
+
|
373
|
+
}
|
374
|
+
|
375
|
+
}
|
376
|
+
|
377
|
+
// </code>
|
378
|
+
|
379
|
+
|
380
|
+
|
381
|
+
```
|
382
|
+
|
383
|
+
|
384
|
+
|
385
|
+
4, 作成済みのリソースから `キー1` を `subscriptionKey` に張り付ける。また `場所` が `serviceRegion` に相当するので japaneast でなければ書き換える。
|
386
|
+
|
387
|
+
![イメージ説明](2d0868f322cff3abb9cc4176eb76a664.png)
|
388
|
+
|
389
|
+
|
390
|
+
|
391
|
+
5, Build Settings の Target Platform を Android に変更してビルド。実機で動作確認。(Windows でも iOS でもOKです。)
|
392
|
+
|
393
|
+
![イメージ説明](f42c63ce85e8268f8bf7115cf7062f29.jpeg)
|
1
書式修正
test
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
>音声認識APIを用いて音声をテキスト化し、switch文の中で照合させて合致したcase内の処理によってアニメーションを変えるという流れなのでしょうか
|
1
|
+
> 音声認識APIを用いて音声をテキスト化し、switch文の中で照合させて合致したcase内の処理によってアニメーションを変えるという流れなのでしょうか
|
2
2
|
|
3
3
|
|
4
4
|
|
@@ -8,7 +8,7 @@
|
|
8
8
|
|
9
9
|
|
10
10
|
|
11
|
-
>『そもそもAPIをどの様に用いれば上記の処理が出来るのか』が分からない
|
11
|
+
> 『そもそもAPIをどの様に用いれば上記の処理が出来るのか』が分からない
|
12
12
|
|
13
13
|
|
14
14
|
|