Integrating the (unofficial) Google Text To Speech

Hey!

Has anyone managed to successfully integrate Google’s text to speech in a Corona app?

I came across this post: http://forums.coronalabs.com/topic/36383-text-to-speech-in-corona-sdk/ It demonstrates a (unofficial) way to use google text to speech in Corona. I’ve implemented it and it works fine in the simulator, but when running on an Android device no voice files are received.

It works fine on the device if I use any URL other than google’s and download some random mp3, so it must be that Google is protecting theirs. I came accross some posts pointing out that the headers for the request should contain an empty referer, which I also tried, as well as faking the user agent.

Any ideas?

Thanks!

local function downloadSpeech(language, text, filename, instant) local function networkListener(event) if ( event.isError ) then print( "Network error - download failed" ) elseif ( event.phase == "began" ) then print( "Progress Phase: began" ) elseif ( event.phase == "ended" ) then print( "Done" ) if language == "spanish" then currentSpanish = audio.loadSound(language .. ".mp3", system.TemporaryDirectory) else currentEnglish = audio.loadSound(language .. ".mp3", system.TemporaryDirectory) end if instant then play(language) end end end local lan = (language == "spanish" and "es") or "en" local params = {} local headers = {} headers["Content-Type"] = "audio/mpeg" headers["Content-Transfer-Encoding"] = "binary" headers["Pragma"] = "no-cache" headers["User-Agent"] = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)" headers["Referer"] = "\r\n" params.headers = headers params.progress = "download" params.response = { filename = filename .. ".mp3", baseDirectory = system.TemporaryDirectory } network.request("http://www.translate.google.com/translate\_tts?tl=" .. lan .. "&q=" .. text, "GET", networkListener, params ) end

Have you considered putting in some additional print statements in the call back to printout the event.response and maybe see what error you’re getting?

Rob

ah I was expecting an error event if the response was not as expected, thanks Rob

so it does return a html string for a 400 ‘bad request’ error, which probably means there is something wrong with the headers

what I don’t understand is that it works fine in the simulator, are there any important differences between network requests in the simulator and those on the device?

Shouldn’t that be a post request? As you are posting data and then recieving a response

no I’m sure it’s a simple GET, you can copy paste the URL in your webbrowser and it’ll give you the mp3

https://www.drupal.org/node/1799502

https://cloud.google.com/translate/v2/using_rest

I would try post and see

thanks, I tried it but it didn’t work

the information you posted is related to the official translate API which is a paid service, I’m trying to use the free ‘unofficial’ api which is not really intended for public use, hence it’s protected somewhat and checks if for example the request header has an empty referer

it’s also limited in some ways, you can only translate up to about 50 chars I believe, which is good enough for me

Ah, i see. I’m not at my computer at the moment, so I won’t be of much help to you.

Best of luck in figuring this out :slight_smile:

Hi.

Did you manage to solve the issue text-to-speech issue? I have run into similar problems.

Hey,

Sadly I haven’t, I tried changing a bunch of things in the request header but its pretty much trial and error as no official doc is available. It works in the simulator but not on the device. I was going to have a look at it again at some point in the future.

Let me know if you have any success!

Have you considered putting in some additional print statements in the call back to printout the event.response and maybe see what error you’re getting?

Rob

ah I was expecting an error event if the response was not as expected, thanks Rob

so it does return a html string for a 400 ‘bad request’ error, which probably means there is something wrong with the headers

what I don’t understand is that it works fine in the simulator, are there any important differences between network requests in the simulator and those on the device?

Shouldn’t that be a post request? As you are posting data and then recieving a response

no I’m sure it’s a simple GET, you can copy paste the URL in your webbrowser and it’ll give you the mp3

https://www.drupal.org/node/1799502

https://cloud.google.com/translate/v2/using_rest

I would try post and see

thanks, I tried it but it didn’t work

the information you posted is related to the official translate API which is a paid service, I’m trying to use the free ‘unofficial’ api which is not really intended for public use, hence it’s protected somewhat and checks if for example the request header has an empty referer

it’s also limited in some ways, you can only translate up to about 50 chars I believe, which is good enough for me

Ah, i see. I’m not at my computer at the moment, so I won’t be of much help to you.

Best of luck in figuring this out :slight_smile:

Hi.

Did you manage to solve the issue text-to-speech issue? I have run into similar problems.

Hey,

Sadly I haven’t, I tried changing a bunch of things in the request header but its pretty much trial and error as no official doc is available. It works in the simulator but not on the device. I was going to have a look at it again at some point in the future.

Let me know if you have any success!

Did you figure this out?