- Home /
Google text-to-speech via HTTP
Hi,
i've been experimenting with the TTS functionality of Google Translate which you can access via HTTP in a browser. I have read it uses WAV as audiotype. My code compiles but nothing plays; I can see in the Inspector that the AudioSource Audio Clip is set when the script runs, but when I bring up the Select Audio Clip, it says "Can not show PCM data for this file" and there are 0 bits etc.
Am I missing something here? Would be great to get this working as it would provide many languages for TTS
This is my code, which runs in an Init() function when the app starts.
var www : WWW = new WWW ("http://translate.google.com/translate_tts?tl=en&q=hello");
yield www;
VoiceAudio.clip = www.GetAudioClip(false, true, AudioType.WAV);
VoiceAudio.Play();
Note; this was developed in 2013, before Google blocked this way of using the TTS
Answer by JeffersonReis · Sep 13, 2014 at 06:01 PM
using UnityEngine;
using System.Collections;
using System.Text.RegularExpressions;
/// <summary>
/// <author>Jefferson Reis</author>
/// <explanation>Works only on Android. To test, change the platform to Android.</explanation>
/// </summary>
public class GoogleTextToSpeech : MonoBehaviour
{
public string words = "Hello";
IEnumerator Start ()
{
// Remove the "spaces" in excess
Regex rgx = new Regex ("\\s+");
// Replace the "spaces" with "% 20" for the link Can be interpreted
string result = rgx.Replace (words, "%20");
string url = "http://translate.google.com/translate_tts?tl=en&q=" + result;
WWW www = new WWW (url);
yield return www;
audio.clip = www.GetAudioClip (false, false, AudioType.MPEG);
audio.Play ();
}
void OnGUI ()
{
words = GUI.TextField (new Rect (Screen.width / 2 - 200 / 2, 10, 200, 30), words);
if (GUI.Button (new Rect (Screen.width / 2 - 150 / 2, 40, 150, 50), "Speak")) {
StartCoroutine (Start ());
}
}
}//closes the class
Answer by terraformer · Jan 05, 2013 at 12:36 PM
Thanks Adam,
Just curious, have you tried this yourself? Anyone else?
I tried (with streaming)
VoiceClip = www.GetAudioClip(false, true, AudioType.MPEG);
and (without streaming)
VoiceClip = www.GetAudioClip(false, false, AudioType.MPEG);
but both gives this error: Streaming of '' on this platform is not supported
I also tried to build it as a webplayer, if the 'platform' refers to the runtime environment?
I finally tried to upload the webplayer to a webserver but same thing there.
I develop on MacOSX with Unity 3.5.5
Best regards, T.
I seem to have found the answer why this does not work: Unity uses the file extension to deter$$anonymous$$e the file type ins$$anonymous$$d of $$anonymous$$I$$anonymous$$E (why?)
in this post: http://forum.unity3d.com/threads/77442-Unity-3.2-Strea$$anonymous$$g-of-on-this-platform-is-not-supported-UnityEngine.WWW-get_audio vaughan writes: "http:// 123.akamai. net/123/1_342342_32.mp3?auth$$anonymous$$ey=1234567&dummy=fake.mp3 ... was all that was needed to make it work. So as dreamora pointed out, a file extension doesn't change your file type, but as far as Unity is concerned, it seems to only play nice if you have the right extension, even if the mime-type response is mp3. "
i.e. he added &dummy=fake.mp3 to the URL which can't be done with Google TTS, from what I have seen in my tests
just for kicks I opened Adobe Director, created a Quicktime cast member with the Google TTS url, put it on stage, pressed play: Worked, without a line of script :)
and by the way, Director has had TTS built-in since 2002 (but I would not use it for game dev today)
also tried a the Ter$$anonymous$$al, which creates a mp3 file, it is really odd that Unity can't do this
curl -A "$$anonymous$$ozilla"
"http://translate.google.com/translate_tts?tl=en&q=hello+world"
> hello.mp3
% Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 102 4896 102 4896 0 0 10392 0 --:--:-- --:--:-- --:--:-- 75323
actually, when I do add &dummy=hello.mp3 in the URL the error message in Unity changes from Strea$$anonymous$$g of '' on this platform is not supported into Strea$$anonymous$$g of 'mp3' on this platform is not supported
It seems everything after the dot . is taken as the name
I tried to use the HT$$anonymous$$L URL Encoding of . which is %2E but that doesn't help
I'm about to give this up
That seems a little odd, that is using file type ins$$anonymous$$d of mime, but I'll have a play around now and see what I can get working.
Answer by fuyangli · Mar 01, 2013 at 11:15 AM
Have you guys found how to make it work?
A colleague of $$anonymous$$e made a PHP script which takes the Google TTS URL with text and converts the mp3 into an OGG file, and saves it locally. So at least that is the first step. I haven't had so much time to take it from there, but I will make an effort hopefully during the next week to implement it in Unity.
Amazing. It would be awesome if we could stream it from the internet, but, it won't be avaible for offline play. Thanks for the reply, and keep coding!
Solved! Here is a demo; right now it just says "Google is talking" when the app starts but I will work more on this to say directions (to make Unity games accessible for blind, along with a sound compass); I'll soon publish the PHP and UnityScript in a Github project where the code can be downloaded. Demo: http://people.dsv.su.se/~thomasw/unity_access/soundcompass/ Github project: https://sites.google.com/site/unityaccess/visual
That's what I wanted to do: Games for Blind people. Terraformer, do you have any kind of contact? Add me in facebook.com/fuyangli so we can talk about what you're doing! It'd be great if we can work together on this!
Hi, I don't use Facebook but you can contact me by e-mail at thomasw@dsv.su.se /$$anonymous$$
Answer by urfx · Oct 31, 2014 at 10:00 PM
what platform were you trying to build for?
TO FIX this issue in you Unity build settings change from web to android or ios... that should of worked.
p.s. I am also looking for collaborators on plug-ins and or games for blind / VI please PM me.
Your answer
Follow this Question
Related Questions
WWW Resolving host timed out 2 Answers
Why do I get Parse error (code 400) from Google Ftiness API in Unity? 0 Answers
Stuck with Importing Google Packages 1 Answer
MMO Timer? 1 Answer
Google Drive Plugin - full access from android device 1 Answer