Assets/Watson/Scripts/Services/TextToSpeech/TextToSpeech.cs
/// <summary>
/// Converts the given text into an AudioClip that can be played.
/// </summary>
/// <param name="text">The text to synthesis into speech.</param>
/// <param name="callback">The callback to invoke with the AudioClip.</param>
/// <param name="usePost">If true, then we use post instead of get, this allows for text that exceeds the 5k limit.</param>
/// <returns>Returns true if the request is sent.</returns>
Converts the given text into an AudioClip that can be played.
Converts the given text into an AudioClip that can be played .
public bool ToSpeech(string text, ToSpeechCallback callback, bool usePost = false)
        {
            if (string.IsNullOrEmpty(text))
                throw new ArgumentNullException("text");
            if (callback == null)
                throw new ArgumentNullException("callback");
            if (!m_AudioFormats.ContainsKey(m_AudioFormat))
            {
                Log.Error("TextToSpeech", "Unsupported audio format: {0}", m_AudioFormat.ToString());
                return false;
            }
            if (!m_VoiceTypes.ContainsKey(m_Voice))
            {
                Log.Error("TextToSpeech", "Unsupported voice: {0}", m_Voice.ToString());
                return false;
            }
            string textId = Utility.GetMD5(text);
            if (!DisableCache)
            {
                if (m_SpeechCache == null)
                    m_SpeechCache = new DataCache("TextToSpeech_" + m_VoiceTypes[m_Voice]);
                byte[] data = m_SpeechCache.Find(textId);
                if (data != null)
                {
                    AudioClip clip = ProcessResponse(textId, data);
                    callback(clip);
                    return true;
                }
            }
            RESTConnector connector = RESTConnector.GetConnector(SERVICE_ID, "/v1/synthesize");
            if (connector == null)
            {
                Log.Error("TextToSpeech", "Failed to get connector.");
                return false;
            }
            ToSpeechRequest req = new ToSpeechRequest();
            req.TextId = textId;
            req.Text = text;
            req.Callback = callback;
            req.Parameters["accept"] = m_AudioFormats[m_AudioFormat];
            req.Parameters["voice"] = m_VoiceTypes[m_Voice];
            req.OnResponse = ToSpeechResponse;
            if (usePost)
            {
                Dictionary<string, string> upload = new Dictionary<string, string>();
                upload["text"] = text;
                req.Send = Encoding.UTF8.GetBytes(Json.Serialize(upload));
                req.Headers["Content-Type"] = "application/json";
            }
            else
            {
                req.Parameters["text"] = text;
            }
            return connector.Send(req);
        }
public bool ToSpeech ( string text , ToSpeechCallback callback , bool usePost = false ) { if ( string . IsNullOrEmpty ( text ) ) throw new ArgumentNullException ( " " ) ; if ( callback == null ) throw new ArgumentNullException ( " " ) ; if ( ! m_AudioFormats . ContainsKey ( m_AudioFormat ) ) { Log . Error ( " " , " " , m_AudioFormat . ToString ( ) ) ; return false ; } if ( ! m_VoiceTypes . ContainsKey ( m_Voice ) ) { Log . Error ( " " , " " , m_Voice . ToString ( ) ) ; return false ; } string textId = Utility . GetMD5 ( text ) ; if ( ! DisableCache ) { if ( m_SpeechCache == null ) m_SpeechCache = new DataCache ( " " + m_VoiceTypes [ m_Voice ] ) ; byte [ ] data = m_SpeechCache . Find ( textId ) ; if ( data != null ) { AudioClip clip = ProcessResponse ( textId , data ) ; callback ( clip ) ; return true ; } } RESTConnector connector = RESTConnector . GetConnector ( SERVICE_ID , " " ) ; if ( connector == null ) { Log . Error ( " " , " " ) ; return false ; } ToSpeechRequest req = new ToSpeechRequest ( ) ; req . TextId = textId ; req . Text = text ; req . Callback = callback ; req . Parameters [ " " ] = m_AudioFormats [ m_AudioFormat ] ; req . Parameters [ " " ] = m_VoiceTypes [ m_Voice ] ; req . OnResponse = ToSpeechResponse ; if ( usePost ) { Dictionary < string , string > upload = new Dictionary < string , string > ( ) ; upload [ " " ] = text ; req . Send = Encoding . UTF8 . GetBytes ( Json . Serialize ( upload ) ) ; req . Headers [ " " ] = " " ; } else { req . Parameters [ " " ] = text ; } return connector . Send ( req ) ; }
Converts the given text into an AudioClip that can be played.
Converts the given text into an AudioClip that can be played .
Returns true if the request is sent.
Returns true if the request is sent .
text
string
The text to synthesis into speech.
The text to synthesis into speech .
callback
ToSpeechCallback
The callback to invoke with the AudioClip.
The callback to invoke with the AudioClip .
usePost
bool
If true, then we use post instead of get, this allows for text that exceeds the 5k limit.
If true then we use post instead of get this allows for text that exceeds the 5k limit .