forked from watson-developer-cloud/unity-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExampleTextToSpeechV1.cs
More file actions
137 lines (122 loc) · 4.51 KB
/
Copy pathExampleTextToSpeechV1.cs
File metadata and controls
137 lines (122 loc) · 4.51 KB
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
/**
* Copyright 2020 IBM Corp. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
using IBM.Watson.TextToSpeech.V1;
using IBM.Watson.TextToSpeech.V1.Model;
using IBM.Cloud.SDK.Utilities;
using IBM.Cloud.SDK.Authentication;
using IBM.Cloud.SDK.Authentication.Iam;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using IBM.Cloud.SDK;
namespace IBM.Watson.Examples
{
public class ExampleTextToSpeechV1 : MonoBehaviour
{
#region PLEASE SET THESE VARIABLES IN THE INSPECTOR
[Space(10)]
[Tooltip("The IAM apikey.")]
[SerializeField]
private string iamApikey;
[Tooltip("The service URL (optional). This defaults to \"https://gateway.watsonplatform.net/text-to-speech/api\"")]
[SerializeField]
private string serviceUrl;
private TextToSpeechService service;
private string allisionVoice = "en-US_AllisonV3Voice";
private string synthesizeText = "Hello, welcome to the Watson Unity SDK!";
private string placeholderText = "Please type text here and press enter.";
private string waitingText = "Watson Text to Speech service is synthesizing the audio!";
private string synthesizeMimeType = "audio/wav";
public InputField textInput;
private bool _textEntered = false;
private AudioClip _recording = null;
private byte[] audioStream = null;
#endregion
private void Start()
{
LogSystem.InstallDefaultReactors();
Runnable.Run(CreateService());
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Return))
{
Runnable.Run(ExampleSynthesize(textInput.text));
}
}
private IEnumerator CreateService()
{
if (string.IsNullOrEmpty(iamApikey))
{
throw new IBMException("Please add IAM ApiKey to the Iam Apikey field in the inspector.");
}
IamAuthenticator authenticator = new IamAuthenticator(apikey: iamApikey);
while (!authenticator.CanAuthenticate())
{
yield return null;
}
service = new TextToSpeechService(authenticator);
if (!string.IsNullOrEmpty(serviceUrl))
{
service.SetServiceUrl(serviceUrl);
}
}
#region Synthesize Example
private IEnumerator ExampleSynthesize(string text)
{
if (string.IsNullOrEmpty(text))
{
text = synthesizeText;
Log.Debug("ExampleTextToSpeechV1", "Using default text, please enter your own text in dialog box!");
}
byte[] synthesizeResponse = null;
AudioClip clip = null;
service.Synthesize(
callback: (DetailedResponse<byte[]> response, IBMError error) =>
{
synthesizeResponse = response.Result;
Log.Debug("ExampleTextToSpeechV1", "Synthesize done!");
clip = WaveFile.ParseWAV("myClip", synthesizeResponse);
PlayClip(clip);
},
text: text,
voice: allisionVoice,
accept: synthesizeMimeType
);
while (synthesizeResponse == null)
yield return null;
yield return new WaitForSeconds(clip.length);
}
#endregion
#region PlayClip
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);
}
}
#endregion
}
}