-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(TextToSpeech): add example for text to speech synthesize
- Loading branch information
1 parent
3889bc8
commit 38666a5
Showing
4 changed files
with
457 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/** | ||
* 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 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_AllisonVoice"; | ||
private string synthesizeText = "Hello, welcome to the Watson Unity SDK!"; | ||
private string synthesizeMimeType = "audio/wav"; | ||
#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 | ||
|
||
private void Start() | ||
{ | ||
LogSystem.InstallDefaultReactors(); | ||
Runnable.Run(CreateService()); | ||
} | ||
|
||
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); | ||
} | ||
|
||
Runnable.Run(ExampleSynthesize()); | ||
} | ||
|
||
#region Synthesize | ||
private IEnumerator ExampleSynthesize() | ||
{ | ||
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: synthesizeText, | ||
voice: allisionVoice, | ||
accept: synthesizeMimeType | ||
); | ||
|
||
while (synthesizeResponse == null) | ||
yield return null; | ||
|
||
yield return new WaitForSeconds(clip.length); | ||
} | ||
#endregion | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.