Skip to content

Instantly share code, notes, and snippets.

@miparnisari
Created April 6, 2018 03:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miparnisari/f7a4895d20c25508f00b202c81ac4245 to your computer and use it in GitHub Desktop.
Save miparnisari/f7a4895d20c25508f00b202c81ac4245 to your computer and use it in GitHub Desktop.
using System;
using System.Collections;
using System.Xml.Linq;
using UnityEngine;
using UnityEngine.Networking;
public class TextToSpeech : MonoBehaviour {
public static readonly string accessUri = "https://api.cognitive.microsoft.com/sts/v1.0/issueToken";
public static readonly string synthesizeUri = "https://speech.platform.bing.com/synthesize";
public string textToSpeak = "Oh yeah! Finally Dorot is speaking.";
private static string accessToken;
private static readonly string apiKey = "MY-SUPER-DUPER-KEY";
public TextToSpeech()
{
}
public void Speak()
{
Debug.Log("Speaking...");
StartCoroutine(RequestToken());
}
private string GenerateSsml()
{
var ssmlDoc = new XDocument(
new XElement("speak",
new XAttribute("version", "1.0"),
new XAttribute(XNamespace.Xml + "lang", "en-US"),
new XElement("voice",
new XAttribute(XNamespace.Xml + "lang", "en-US"),
new XAttribute(XNamespace.Xml + "gender", "Male"),
new XAttribute("name", "Microsoft Server Speech Text to Speech Voice (en-US, BenjaminRUS)"),
this.textToSpeak)));
return ssmlDoc.ToString();
}
public IEnumerator RequestToken()
{
var tokenRequest = UnityWebRequest.Post(accessUri, "data");
tokenRequest.SetRequestHeader("Ocp-Apim-Subscription-Key", apiKey);
var tokenResponse = tokenRequest.SendWebRequest();
yield return tokenResponse;
if (tokenRequest.isHttpError)
{
Debug.LogError("HTTP Error: " + tokenRequest.error + " Code: " + tokenRequest.responseCode);
}
else
{
var postStringData = GenerateSsml();
accessToken = tokenRequest.downloadHandler.text;
Debug.LogError("Access token: " + accessToken);
Debug.LogError("postStringData: " + postStringData);
StartCoroutine(Synthesize(postStringData, accessToken));
}
}
public IEnumerator Synthesize(string text, string token)
{
var synReq = UnityWebRequest.Post(synthesizeUri, text);
synReq.SetRequestHeader("Content-Type", "application/ssml+xml");
synReq.SetRequestHeader("X-Microsoft-OutputFormat", "riff-16khz-16bit-mono-pcm");
synReq.SetRequestHeader("X-Search-AppId", "07D3234E49CE426DAA29772419F436CA");
synReq.SetRequestHeader("X-Search-ClientID", "1ECFAE91408841A480F00935DC390960");
synReq.SetRequestHeader("User-Agent", "Dorot");
synReq.SetRequestHeader("Authorization", "Bearer " + token);
var synRes = synReq.SendWebRequest();
yield return synRes;
if (synReq.isHttpError)
{
Debug.LogError("HTTP Error: " + synReq.error + " Code: " + synReq.responseCode + " isNetworkError: " + synReq.isNetworkError + " isDone: " + synReq.isDone);
}
else
{
Debug.LogError("done!");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment