{"id":857,"date":"2012-05-07T20:53:42","date_gmt":"2012-05-07T18:53:42","guid":{"rendered":"https:\/\/porzelt.net\/?p=857"},"modified":"2019-10-04T16:23:19","modified_gmt":"2019-10-04T16:23:19","slug":"die-inoffizielle-google-text-to-speech-api","status":"publish","type":"post","link":"https:\/\/porzelt.net\/en\/die-inoffizielle-google-text-to-speech-api","title":{"rendered":"Die inoffizielle Google Text-To-Speech API"},"content":{"rendered":"<p>The fact that Googles Translation-Application <a href=\"http:\/\/translate.google.com\/\"> translate.google.com <!-- a--> offers the possibility to readout an inserted text (TTS text to speech), is nothing new. The specialty, however, are the possibilities that arise through this feature.<br \/>\n&nbsp;<br \/>\nThe data for the audio output is in fact called by a simple HTTP GET (REST) \u200b\u200brequest:<br \/>\n&nbsp;<br \/>\n<code>http:\/\/translate.google.com\/translate_tts?tl=de&q=<strong>text<\/strong><\/code><br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\nBy this request, which provides an MP3 file, can easily realize TTS-applications and works, thanks to the multilingualism of Google&#8217;s service, also for many other languages\u200b\u200b. The only drawback to this method is a limitation which allows only a maximum of 100 characters per request.<br \/>\n&nbsp;<br \/>\nThe main URL parameters in this case, the two for the selected language and speaking to the text:<\/a><\/p>\n<p><a href=\"http:\/\/translate.google.com\/\"><\/p>\n<ul><code>tl= <-Language-><br \/>\nq= <-Text-><\/code><\/ul>\n<p>&nbsp;<br \/>\nIn ActionScript 3 the corresponding code would look like this:<br \/>\n&nbsp;<\/p>\n<div class=\"actionscript3\">\n<ol>\n<li class=\"li1\">\n<div class=\"de1\"><span class=\"kw2\">var<\/span> sayWhat<span class=\"sy0\">:<\/span><span class=\"kw5\">String<\/span> = <span class=\"st0\">&#8220;Hallo, wie geht es dir? &#8220;<\/span><span class=\"sy0\">;<\/span><\/div>\n<\/li>\n<li class=\"li1\">\n<div class=\"de1\"><span class=\"kw2\">var<\/span> <span class=\"kw7\">lang<\/span><span class=\"sy0\">:<\/span><span class=\"kw5\">String<\/span> = <span class=\"st0\">&#8220;de&#8221;<\/span><span class=\"sy0\">;<\/span><\/div>\n<\/li>\n<li class=\"li1\">\n<div class=\"de1\"><\/div>\n<\/li>\n<li class=\"li1\">\n<div class=\"de1\"><span class=\"co1\">\/\/ sayWhat = &#8220;Hello, how are you?&#8221;;<\/span><\/div>\n<\/li>\n<li class=\"li1\">\n<div class=\"de1\"><span class=\"co1\">\/\/ lang = &#8220;en&#8221;;<\/span><\/div>\n<\/li>\n<li class=\"li1\">\n<div class=\"de1\"><\/div>\n<\/li>\n<li class=\"li1\">\n<div class=\"de1\"><span class=\"kw2\">var<\/span> req<span class=\"sy0\">:<\/span><span class=\"kw5\">URLRequest<\/span>= <span class=\"kw1\">new<\/span> <span class=\"kw5\">URLRequest<\/span><span class=\"br0\">(<\/span><span class=\"st0\">&#8220;http:\/\/translate.google.com\/translate_tts?tl=&#8221;<\/span><span class=\"sy0\"> +<\/span> lang <span class=\"sy0\">+<\/span> <span class=\"st0\">&#8220;&#038;q=&#8221;<\/span> <span class=\"sy0\">+<\/span> <span class=\"kw7\">encodeURI<\/span><span class=\"br0\">(<\/span>sayWhat<span class=\"br0\">)<\/span><span class=\"br0\">)<\/span><\/div>\n<\/li>\n<li class=\"li1\">\n<div class=\"de1\"><\/div>\n<\/li>\n<li class=\"li1\">\n<div class=\"de1\"><span class=\"kw2\">var<\/span> snd<span class=\"sy0\">:<\/span><span class=\"kw5\">Sound<\/span> = <span class=\"kw1\">new<\/span> <span class=\"kw5\">Sound<\/span><span class=\"br0\">(<\/span>req<span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span><\/div>\n<\/li>\n<li class=\"li1\">\n<div class=\"de2\"><\/div>\n<\/li>\n<li class=\"li1\">\n<div class=\"de1\"><span class=\"kw1\">if<\/span> <span class=\"br0\">(<\/span>sayWhat<span class=\"sy0\">.<\/span><span class=\"kw7\">length<\/span> <span class=\"sy0\">><\/span> <span class=\"nu0\">100<\/span><span class=\"br0\">)<\/span><span class=\"br0\">{<\/span><\/div>\n<\/li>\n<li class=\"li1\">\n<div class=\"de1\"><span class=\"kw1\">throw<\/span> <span class=\"kw1\">new<\/span> <span class=\"kw5\">Error<\/span><span class=\"br0\">(<\/span><span class=\"st0\">&#8220;Only phrases < 100 charakters are supported.\"<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span><\/div>\n<\/li>\n<li class=\"li1\">\n<div class=\"de1\"><span class=\"br0\">}<\/span> <span class=\"kw1\">else<\/span> <span class=\"br0\">{<\/span><\/div>\n<\/li>\n<li class=\"li1\">\n<div class=\"de1\">snd<span class=\"sy0\">.<\/span><span class=\"kw7\">play<\/span><span class=\"br0\">(<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span><\/div>\n<\/li>\n<li class=\"li1\">\n<div class=\"de2\"><span class=\"br0\">}<\/span><\/div>\n<\/li>\n<\/ol>\n<\/div>\n<p>&nbsp;<\/p>\n<p>A URLRequest calls to the generated MP3 file, which is as usual in AS3 is received and played.<br \/>\nFor a practical application the code had to convert the umlauts &#8220;\u00e4, \u00fc, \u00f6&#8221; into &#8220;ae, ue, oe&#8221;, because Google&#8217;s TTS service that does not recognize them. Also Texts larger than 100 characters have to be divided into smaller sections which are requested and played one after another.<br \/>\n&nbsp;<br \/>\nWhile testing the function I noticed that Google had a latency between 600ms up to 1.5seconds. So sentences, which had to be separated because of the length, were played with a large gap in between some Words. An optimization would be if the new MP3-pard would load while the previous part is played.<br \/>\n&nbsp;<br \/>\nDemo-App soon&#8230;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The fact that Googles Translation-Application translate.google.com offers the possibility to readout an inserted text (TTS text to speech), is nothing new. The specialty, however, are the possibilities that arise through this feature. &nbsp; The data for the audio output is in fact called by a simple HTTP GET (REST) \u200b\u200brequest: &nbsp; http:\/\/translate.google.com\/translate_tts?tl=de&#038;q=text &nbsp; &nbsp; By this request, which provides an MP3 file, can easily realize TTS-applications and works, thanks to the multilingualism of Google&#8217;s service, also for many other languages\u200b\u200b. The only drawback to this method is a limitation which allows only a maximum of 100 characters per request. &nbsp; The main URL parameters in this case, the two for [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":868,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4,7],"tags":[15,20,103,183,206,215,238,247,248,249,251],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/porzelt.net\/en\/wp-json\/wp\/v2\/posts\/857"}],"collection":[{"href":"https:\/\/porzelt.net\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/porzelt.net\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/porzelt.net\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/porzelt.net\/en\/wp-json\/wp\/v2\/comments?post=857"}],"version-history":[{"count":1,"href":"https:\/\/porzelt.net\/en\/wp-json\/wp\/v2\/posts\/857\/revisions"}],"predecessor-version":[{"id":1252,"href":"https:\/\/porzelt.net\/en\/wp-json\/wp\/v2\/posts\/857\/revisions\/1252"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/porzelt.net\/en\/wp-json\/wp\/v2\/media\/868"}],"wp:attachment":[{"href":"https:\/\/porzelt.net\/en\/wp-json\/wp\/v2\/media?parent=857"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/porzelt.net\/en\/wp-json\/wp\/v2\/categories?post=857"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/porzelt.net\/en\/wp-json\/wp\/v2\/tags?post=857"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}