- Google Cloud Speech Recognition [VR\AR\Desktop\Desktop] を買って、シーン内にGCSpeechRecognitionというPrefabを入れる(Assets/FrostweepGames/GCSpeechRecognition/Prefabs/にある)。
-
Prefabの設定のApi Keyに、Googleから取得した音声認識用のAPI Keyを入れる。
-
Prefabの設定のConfigsの中のDefault Languageを設定(スクリプト内で設定も可能)
-
GCSpeechRecognitionオブジェクトに、以下のスクリプト(GoogleVoiceRecognition.cs)を追加。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using FrostweepGames.Plugins.GoogleCloud.SpeechRecognition;
public class GoogleVoiceRecognition : MonoBehaviour
{
public delegate void onRecognized(SpeechRecognitionResult [] speechRecognitionResults);
private GCSpeechRecognition speechRecognition;
private bool bRuntimeDetection = false;
// Start is called before the first frame update
void Start()
{
speechRecognition = GCSpeechRecognition.Instance;
speechRecognition.RecognitionSuccessEvent += RecognitionSuccessEventHandler;
speechRecognition.NetworkRequestFailedEvent += SpeechRecognizedFailedEventHandler;
speechRecognition.LongRecognitionSuccessEvent += LongRecognitionSuccessEventHandler;
// 言語を入れ替える
//speechRecognition.SetLanguage(Enumerators.LanguageCode.ja_JP);
}
private void OnDestroy()
{
speechRecognition.RecognitionSuccessEvent -= RecognitionSuccessEventHandler;
speechRecognition.NetworkRequestFailedEvent -= SpeechRecognizedFailedEventHandler;
speechRecognition.LongRecognitionSuccessEvent -= LongRecognitionSuccessEventHandler;
}
public void StartRecord(bool _bRuntimeDetection=false)
{
bRuntimeDetection = _bRuntimeDetection;
speechRecognition.StartRecord(bRuntimeDetection); // true: runtime voice detection on (Does not seem to work
onRecognizedCallback = null;
}
onRecognized onRecognizedCallback = null;
public void StopRecord(onRecognized _onRecognizedCallback)
{
onRecognizedCallback = null;
if (_onRecognizedCallback == null)
{
Debug.Log("Please supply callback!");
}
onRecognizedCallback = _onRecognizedCallback;
speechRecognition.StopRecord();
}
private void SpeechRecognizedFailedEventHandler(string obj, long requestIndex)
{
Debug.Log("Error:" + obj);
if (onRecognizedCallback != null)
{
onRecognizedCallback(null);
}
}
private void RecognitionSuccessEventHandler(RecognitionResponse obj, long requestIndex)
{
Debug.Log("Recognition success");
if (obj != null && obj.results.Length > 0)
{
if (onRecognizedCallback != null)
{
onRecognizedCallback(obj.results);
}
}
else
{
Debug.Log("Speech Recognition succeeded! Words are no detected.");
if (onRecognizedCallback != null)
{
onRecognizedCallback(null);
}
}
}
private void LongRecognitionSuccessEventHandler(OperationResponse operation, long index)
{
Debug.Log("Recognition success");
if (operation != null && operation.response.results.Length > 0)
{
if (onRecognizedCallback != null)
{
onRecognizedCallback(operation.response.results);
}
}
else
{
Debug.Log("Error:Speech Recognition succeeded! Words are no detected.");
if (onRecognizedCallback != null)
{
onRecognizedCallback(null);
}
}
}
}
-
使う側のスクリプトを用意
- 必要ライブラリのインポート
using FrostweepGames.Plugins.GoogleCloud.SpeechRecognition;
- 認識オブジェクトの取得
GameObject gvrObj = GameObject.Find("GCSpeechRecognition"); GoogleVoiceRecognition googleVoiceRecognition = gvrObj.GetComponent<GoogleVoiceRecognition>();
- 録音開始
googleVoiceRecognition.StartRecord();
- 録音停止&認識開始。認識結果はコールバックにて受け取る。
googleVoiceRecognition.StopRecord( (SpeechRecognitionResult[] srr)=> { if( srr == null ){ Debug.Log("Recognition failed."); return; } foreach (var result in srr) { foreach (var alternative in result.alternatives) { Debug.Log("Transcripted:" + alternative.transcript); } } } );