Watson Unity TextToSpeech

標準

元記事>「Watson×Unity!初心者でもできる、VR 空間で Unity ちゃんとおしゃべりアプリ!」

https://www.ibm.com/developerworks/jp/cloud/library/unity/

に対し、Unity-SDKのバージョンが変わったため、SampleTextToSpeech.csの書き換えが必要になった。

(SpeechToTextに関しては、前記事参照)

こんな感じでどうでしょう?

※サービス資格情報は、Unityのほうのインスペクターで入力します

//SampleTextToSpeech.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using IBM.Watson.DeveloperCloud.Services.TextToSpeech.v1;
using IBM.Watson.DeveloperCloud.Connection;
using IBM.Watson.DeveloperCloud.Utilities;

public class SampleTextToSpeech : MonoBehaviour {

#region PLEASE SET THESE VARIABLES IN THE INSPECTOR
 [SerializeField]
 private string _username;
 [SerializeField]
 private string _password;
 [SerializeField]
 private string _url;
 #endregion

TextToSpeech m_textToSpeech;
 string m_TestString = "おはようございます。漢字も読めます。";

private bool m_synthesizeTested = false;
 private bool m_getVoicesTested = false;
 private bool m_getVoiceTested = false;


 // Use this for initialization

void Start()
 {
 // Create credential and instantiate service
 Credentials credentials = new Credentials(_username, _password, _url);

m_textToSpeech = new TextToSpeech(credentials);
 Runnable.Run(Examples());
 }

private IEnumerator Examples()
 {

// Synthesize
 Debug.Log("Attempting synthesize.");
// m_textToSpeech.Voice = VoiceType.en_US_Allison;
 m_textToSpeech.Voice = VoiceType.ja_JP_Emi;

m_textToSpeech.ToSpeech(HandleToSpeechCallback, OnFail, m_TestString, true);
 while (!m_synthesizeTested)
 yield return null;

// Get Voices
 Debug.Log("Attempting to get voices.");
 m_textToSpeech.GetVoices(OnGetVoices, OnFail);
 while (!m_getVoicesTested)
 yield return null;

// Get Voice
 Debug.Log("Attempting to get voice {0}.");
 m_textToSpeech.GetVoice(OnGetVoice, OnFail, VoiceType.ja_JP_Emi);

while (!m_getVoiceTested)
 yield return null;
 
 }

void HandleToSpeechCallback(AudioClip clip, Dictionary<string, object> customData = null)
 {
 PlayClip(clip);
 }

private void PlayClip(AudioClip clip)
 {
 if (Application.isPlaying && clip != null)
 {
 GameObject audioObject = new GameObject("AudioObject");
 AudioSource source = audioObject.AddComponent<AudioSource>();
 source.spatialBlend = 0.0f;
 source.loop = false;
 source.clip = clip;
 source.Play();

GameObject.Destroy(audioObject, clip.length);
 }
 }

private void OnGetVoices(Voices voices, Dictionary<string, object> customData = null)
 {
 Debug.Log("Text to Speech - Get voices response: {0}");
 m_getVoicesTested = true;
 }

private void OnGetVoice(Voice voice, Dictionary<string, object> customData = null)
 {
 Debug.Log("Text to Speech - Get voice response: {0}");
 m_getVoiceTested = true;
 }

private void OnFail(RESTConnector.Error error, Dictionary<string, object> customData)
 {
 Debug.Log("Error received: {0}");
 }

// Update is called once per frame
 void Update () {

}

}
広告

コメントは受け付けていません。