本教程基于Unity2017.2及Visual Studio 2017
本教程编写时间:2017年12月8日
本文内容提要
- 设计语音命令并针对Hololens语音引擎优化
- 让用户知道可以用什么语音命令
- 听到用户的语音命令后给出反馈
- 使用Dictation Recognizer理解用户在说什么
- 使用Grammar Recognizer监听基于SRGS(Speech Recognition Grammar Specification,语音识别文法规范)文件的命令
本文使用了官方教程的资源
原地址
如果下载有困难,百度云地址
App
文件夹并选择该文件夹Microphone权限必须开启,本工程已经设置开启了该权限,你自己创建工程时,务必要牢记
1. Edit > Project Settings > Player
2. 切换到Universal Windows Platform
3. “Publishing Settings > Capabilities”区域中,选中Microphone capability
// TODO: 2.a Delete the following two lines:
RecordButton.SetActive(false);
MessageUIRenderer.gameObject.SetActive(false);
在本章中,我们将使用听写识别器为宇航员创建一条消息。使用听写识别器时,请记住:
Microphone权限必须开启,本工程已经设置开启了该权限,你自己创建工程时,务必要牢记
1. Edit > Project Settings > Player
2. 切换到Universal Windows Platform
3. “Publishing Settings > Capabilities”区域中,选中Microphone capability
我们要编辑MicrophoneManager.cs来使用听写识别器。这是我们要补充的:
1. 当按下Record按钮时,我们将启动DictationRecognizer。
2. 显示DictationRecognizer理解的假设。
3. 锁定DictationRecognizer理解的结果。
4. 检查DictationRecognizer的超时。
5. 当按下“ 停止”按钮或麦克风会话超时时,请停止DictationRecognizer。
6. 重新启动KeywordRecognizer,它将侦听Send Message命令。
现在,在MicrophoneManager.cs中完成注释为3.a的所有编码练习,下面为最终完整代码:
using Academy.HoloToolkit.Unity;
using System.Collections;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Windows.Speech;
public class MicrophoneManager : MonoBehaviour
{
[Tooltip("A text area for the recognizer to display the recognized strings.")]
public Text DictationDisplay;
private DictationRecognizer dictationRecognizer;
// Use this string to cache the text currently displayed in the text box.
private StringBuilder textSoFar;
// Using an empty string specifies the default microphone.
private static string deviceName = string.Empty;
private int samplingRate;
private const int messageLength = 10;
// Use this to reset the UI once the Microphone is done recording after it was started.
private bool hasRecordingStarted;
void Awake()
{
/* TODO: DEVELOPER CODING EXERCISE 3.a */
// 3.a: Create a new DictationRecognizer and assign it to dictationRecognizer variable.
dictationRecognizer = new DictationRecognizer();
// 3.a: Register for dictationRecognizer.DictationHypothesis and implement DictationHypothesis below
// This event is fired while the user is talking. As the recognizer listens, it provides text of what it's heard so far.
dictationRecognizer.DictationHypothesis += DictationRecognizer_DictationHypothesis;
// 3.a: Register for dictationRecognizer.DictationResult and implement DictationResult below
// This event is fired after the user pauses, typically at the end of a sentence. The full recognized string is returned here.
dictationRecognizer.DictationResult += DictationRecognizer_DictationResult;
// 3.a: Register for dictationRecognizer.DictationComplete and implement DictationComplete below
// This event is fired when the recognizer stops, whether from Stop() being called, a timeout occurring, or some other error.
dictationRecognizer.DictationComplete += DictationRecognizer_DictationComplete;
// 3.a: Register for dictationRecognizer.DictationError and implement DictationError below
// This event is fired when an error occurs.
dictationRecognizer.DictationError += DictationRecognizer_DictationError;
// Query the maximum frequency of the default microphone. Use 'unused' to ignore the minimum frequency.
int unused;
Microphone.GetDeviceCaps(deviceName, out unused, out samplingRate);
// Use this string to cache the text currently displayed in the text box.
textSoFar = new StringBuilder();
// Use this to reset the UI once the Microphone is done recording after it was started.
hasRecordingStarted = false;
}
void Update()
{
// 3.a: Add condition to check if dictationRecognizer.Status is Running
if (hasRecordingStarted && !Microphone.IsRecording(deviceName) && dictationRecognizer.Status == SpeechSystemStatus.Running)
{
// Reset the flag now that we're cleaning up the UI.
hasRecordingStarted = false;
// This acts like pressing the Stop button and sends the message to the Communicator.
// If the microphone stops as a result of timing out, make sure to manually stop the dictation recognizer.
// Look at the StopRecording function.
SendMessage("RecordStop");
}
}
///
/// Turns on the dictation recognizer and begins recording audio from the default microphone.
///
/// The audio clip recorded from the microphone.
public AudioClip StartRecording()
{
// 3.a Shutdown the PhraseRecognitionSystem. This controls the KeywordRecognizers
PhraseRecognitionSystem.Shutdown();
// 3.a: Start dictationRecognizer
dictationRecognizer.Start();
// 3.a Uncomment this line
DictationDisplay.text = "Dictation is starting. It may take time to display your text the first time, but begin speaking now...";
// Set the flag that we've started recording.
hasRecordingStarted = true;
// Start recording from the microphone for 10 seconds.
return Microphone.Start(deviceName, false, messageLength, samplingRate);
}
///
/// Ends the recording session.
///
public void StopRecording()
{
// 3.a: Check if dictationRecognizer.Status is Running and stop it if so
if (dictationRecognizer.Status == SpeechSystemStatus.Running)
{
dictationRecognizer.Stop();
}
Microphone.End(deviceName);
}
///
/// This event is fired while the user is talking. As the recognizer listens, it provides text of what it's heard so far.
///
/// The currently hypothesized recognition.
private void DictationRecognizer_DictationHypothesis(string text)
{
// 3.a: Set DictationDisplay text to be textSoFar and new hypothesized text
// We don't want to append to textSoFar yet, because the hypothesis may have changed on the next event
DictationDisplay.text = textSoFar.ToString() + " " + text + "...";
}
///
/// This event is fired after the user pauses, typically at the end of a sentence. The full recognized string is returned here.
///
/// The text that was heard by the recognizer.
/// A representation of how confident (rejected, low, medium, high) the recognizer is of this recognition.
private void DictationRecognizer_DictationResult(string text, ConfidenceLevel confidence)
{
// 3.a: Append textSoFar with latest text
textSoFar.Append(text + ". ");
// 3.a: Set DictationDisplay text to be textSoFar
DictationDisplay.text = textSoFar.ToString();
}
///
/// This event is fired when the recognizer stops, whether from Stop() being called, a timeout occurring, or some other error.
/// Typically, this will simply return "Complete". In this case, we check to see if the recognizer timed out.
///
/// An enumerated reason for the session completing.
private void DictationRecognizer_DictationComplete(DictationCompletionCause cause)
{
// If Timeout occurs, the user has been silent for too long.
// With dictation, the default timeout after a recognition is 20 seconds.
// The default timeout with initial silence is 5 seconds.
if (cause == DictationCompletionCause.TimeoutExceeded)
{
Microphone.End(deviceName);
DictationDisplay.text = "Dictation has timed out. Please press the record button again.";
SendMessage("ResetAfterTimeout");
}
}
///
/// This event is fired when an error occurs.
///
/// The string representation of the error reason.
/// The int representation of the hresult.
private void DictationRecognizer_DictationError(string error, int hresult)
{
// 3.a: Set DictationDisplay text to be the error string
DictationDisplay.text = error + "\nHRESULT: " + hresult;
}
private IEnumerator RestartSpeechSystem(KeywordManager keywordToStart)
{
while (dictationRecognizer != null && dictationRecognizer.Status == SpeechSystemStatus.Running)
{
yield return null;
}
keywordToStart.StartKeywordRecognizer();
}
}
使用语法识别器根据SRGS或语音识别语法规范文件识别用户的语音。
Microphone权限必须开启,本工程已经设置开启了该权限,你自己创建工程时,务必要牢记
1. Edit > Project Settings > Player
2. 切换到Universal Windows Platform
3. “Publishing Settings > Capabilities”区域中,选中Microphone capability
SRGS设计规范可以在W3C网站上找到。
洪流学堂,最科学的Unity3d学习路线,让你快人一步掌握Unity3d开发核心技术!