Skip to content

Commit 38666a5

Browse files
committed
feat(TextToSpeech): add example for text to speech synthesize
1 parent 3889bc8 commit 38666a5

File tree

4 files changed

+457
-0
lines changed

4 files changed

+457
-0
lines changed

Examples/ExampleTextToSpeechV1.cs

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/**
2+
* Copyright 2020 IBM Corp. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
using IBM.Watson.TextToSpeech.V1;
19+
using IBM.Watson.TextToSpeech.V1.Model;
20+
using IBM.Cloud.SDK.Utilities;
21+
using IBM.Cloud.SDK.Authentication;
22+
using IBM.Cloud.SDK.Authentication.Iam;
23+
using System.Collections;
24+
using System.Collections.Generic;
25+
using UnityEngine;
26+
using IBM.Cloud.SDK;
27+
28+
29+
namespace IBM.Watson.Examples
30+
{
31+
public class ExampleTextToSpeechV1 : MonoBehaviour
32+
{
33+
#region PLEASE SET THESE VARIABLES IN THE INSPECTOR
34+
[Space(10)]
35+
[Tooltip("The IAM apikey.")]
36+
[SerializeField]
37+
private string iamApikey;
38+
[Tooltip("The service URL (optional). This defaults to \"https://gateway.watsonplatform.net/text-to-speech/api\"")]
39+
[SerializeField]
40+
private string serviceUrl;
41+
private TextToSpeechService service;
42+
private string allisionVoice = "en-US_AllisonVoice";
43+
private string synthesizeText = "Hello, welcome to the Watson Unity SDK!";
44+
private string synthesizeMimeType = "audio/wav";
45+
#endregion
46+
47+
#region PlayClip
48+
private void PlayClip(AudioClip clip)
49+
{
50+
if (Application.isPlaying && clip != null)
51+
{
52+
GameObject audioObject = new GameObject("AudioObject");
53+
AudioSource source = audioObject.AddComponent<AudioSource>();
54+
source.spatialBlend = 0.0f;
55+
source.loop = false;
56+
source.clip = clip;
57+
source.Play();
58+
59+
GameObject.Destroy(audioObject, clip.length);
60+
}
61+
}
62+
#endregion
63+
64+
private void Start()
65+
{
66+
LogSystem.InstallDefaultReactors();
67+
Runnable.Run(CreateService());
68+
}
69+
70+
private IEnumerator CreateService()
71+
{
72+
if (string.IsNullOrEmpty(iamApikey))
73+
{
74+
throw new IBMException("Please add IAM ApiKey to the Iam Apikey field in the inspector.");
75+
}
76+
77+
IamAuthenticator authenticator = new IamAuthenticator(apikey: iamApikey);
78+
79+
while (!authenticator.CanAuthenticate())
80+
{
81+
yield return null;
82+
}
83+
84+
service = new TextToSpeechService(authenticator);
85+
if (!string.IsNullOrEmpty(serviceUrl))
86+
{
87+
service.SetServiceUrl(serviceUrl);
88+
}
89+
90+
Runnable.Run(ExampleSynthesize());
91+
}
92+
93+
#region Synthesize
94+
private IEnumerator ExampleSynthesize()
95+
{
96+
byte[] synthesizeResponse = null;
97+
AudioClip clip = null;
98+
service.Synthesize(
99+
callback: (DetailedResponse<byte[]> response, IBMError error) =>
100+
{
101+
synthesizeResponse = response.Result;
102+
Log.Debug("ExampleTextToSpeechV1", "Synthesize done!");
103+
clip = WaveFile.ParseWAV("myClip", synthesizeResponse);
104+
PlayClip(clip);
105+
},
106+
text: synthesizeText,
107+
voice: allisionVoice,
108+
accept: synthesizeMimeType
109+
);
110+
111+
while (synthesizeResponse == null)
112+
yield return null;
113+
114+
yield return new WaitForSeconds(clip.length);
115+
}
116+
#endregion
117+
}
118+
}

Examples/ExampleTextToSpeechV1.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)