Skip to content

Commit

Permalink
Refactoring, obsolete code cleanup and extra comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ActiveNick committed Aug 13, 2019
1 parent c3c1a6c commit 1865e9a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 32 deletions.
45 changes: 13 additions & 32 deletions Assets/Scripts/SpeechManager.cs
Expand Up @@ -14,10 +14,19 @@
using Microsoft.CognitiveServices.Speech;
using Microsoft.CognitiveServices.Speech.Audio;

// IMPORTANT: THIS CODE ONLY WORKS WITH THE .NET 4.6 SCRIPTING RUNTIME

/// <summary>
/// SpeechManager provides two paths for Speech Synthesis using the
/// Cognitive Services Unified Speech service:
/// 1. REST API (old method)
/// 2. Official SDK plugin for Unity (recommended)
/// IMPORTANT: THIS CODE ONLY WORKS WITH THE .NET 4.6 SCRIPTING RUNTIME
/// </summary>
public class SpeechManager : MonoBehaviour {

// FOR MORE INFO ON AUTHENTICATION AND HOW TO GET YOUR API KEY, PLEASE VISIT
// https://docs.microsoft.com/en-us/azure/cognitive-services/speech/how-to/how-to-authentication
// The free tier gives you 5,000 free API transactions / month.
// Set credentials in the Inspector
[Tooltip("Cognitive Services Speech API Key")]
[SecretValue("SpeechService_APIKey")]
public string SpeechServiceAPIKey = string.Empty;
Expand All @@ -38,23 +47,6 @@ public class SpeechManager : MonoBehaviour {
[HideInInspector]
public bool isReady = false;

#if UNITY_EDITOR || !UNITY_WSA
/// <summary>
/// This class is required to circumvent a TLS bug in Unity, otherwise Unity will throw
/// an error stating the certificate is invalid. This is supposed to be fixed in Unity
/// 2018.2. Note that UWP doesn't have this bug, only Mono, hence the conditional code.
/// </summary>
//private class CustomCertificatePolicy : ICertificatePolicy
//{
// public bool CheckValidationResult(ServicePoint sp,
// X509Certificate certificate, WebRequest request, int error)
// {
// // We force Unity to always validate the certificate as "true".
// return true;
// }
//}
#endif

private void Awake()
{
// Attempt to load API secrets
Expand All @@ -68,21 +60,10 @@ private void Awake()

// Use this for initialization
void Start () {
#if UNITY_EDITOR || !UNITY_WSA
// Unity will complain that the following statement is deprecated, however it's still working :)
//ServicePointManager.CertificatePolicy = new CustomCertificatePolicy();

// This 'workaround' seems to work for the .NET Storage SDK, but not here. Leaving this for clarity
//ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
#endif

// FOR MORE INFO ON AUTHENTICATION AND HOW TO GET YOUR API KEY, PLEASE VISIT
// https://docs.microsoft.com/en-us/azure/cognitive-services/speech/how-to/how-to-authentication
// The Authentication code below is only for the REST API version
Authentication auth = new Authentication();
Task<string> authenticating = auth.Authenticate($"https://{SpeechServiceRegion}.api.cognitive.microsoft.com/sts/v1.0/issueToken",
SpeechServiceAPIKey); // INSERT-YOUR-SPEECH-API-KEY-HERE
// Don't use the key above, it's mine and I reserve the right to invalidate it if/when I want,
// use the link above and go get your own. The free tier gives you 5,000 free API transactions / month.
SpeechServiceAPIKey);

// Since the authentication process needs to run asynchronously, we run the code in a coroutine to
// avoid blocking the main Unity thread.
Expand Down
8 changes: 8 additions & 0 deletions Assets/Scripts/UIManager.cs
Expand Up @@ -14,6 +14,7 @@ public class UIManager : MonoBehaviour {
public Toggle useSDK;
public Dropdown voicelist;
public GameObject shape;

private void Start()
{
pitch.text = "0";
Expand All @@ -27,12 +28,17 @@ private void Start()
voicelist.value = (int)VoiceName.enUSJessaRUS;
}

// The spinning cube is only used to verify that speech synthesis doesn't introduce
// game loop blocking code.
public void Update()
{
if (shape != null)
shape.transform.Rotate(Vector3.up, 1);
}

/// <summary>
/// Speech synthesis can be called via REST API or Speech Service SDK plugin for Unity
/// </summary>
public async void SpeechPlayback()
{
if (speech.isReady)
Expand All @@ -42,10 +48,12 @@ public async void SpeechPlayback()
speech.VoicePitch = int.Parse(pitch.text);
if (useSDK.isOn)
{
// Required to insure non-blocking code in the main Unity UI thread.
await Task.Run(() => speech.SpeakWithSDKPlugin(msg));
}
else
{
// This code is non-blocking by default, no need to run in background
speech.SpeakWithRESTAPI(msg);
}
} else
Expand Down

0 comments on commit 1865e9a

Please sign in to comment.