<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Alex Sleat &#187; mp3</title>
	<atom:link href="http://alexsleat.co.uk/tag/mp3/feed/" rel="self" type="application/rss+xml" />
	<link>http://alexsleat.co.uk</link>
	<description>/home/alex</description>
	<lastBuildDate>Sat, 04 Feb 2012 22:11:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Python &#8211; Making Use of Google&#8217;s Text To Speech Translation Tool</title>
		<link>http://alexsleat.co.uk/2011/11/14/python-making-use-of-googles-text-to-speech-translation-tool/</link>
		<comments>http://alexsleat.co.uk/2011/11/14/python-making-use-of-googles-text-to-speech-translation-tool/#comments</comments>
		<pubDate>Mon, 14 Nov 2011 00:46:22 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[blog1]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[how to]]></category>
		<category><![CDATA[mp3]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[source code]]></category>
		<category><![CDATA[stream]]></category>
		<category><![CDATA[text to speech]]></category>
		<category><![CDATA[translate]]></category>
		<category><![CDATA[tts]]></category>
		<category><![CDATA[voice]]></category>

		<guid isPermaLink="false">http://alexsleat.co.uk/?p=23542</guid>
		<description><![CDATA[<br/><p>Text to speech (tts) is a difficult task to get right and there are quite a few packages that show just how hard with their terrible tinny voices, these are also usually only limited to English, which for the larger part than most people would like to believe don&#8217;t find it very useful. Nevermind, [...]]]></description>
			<content:encoded><![CDATA[<br/><p>Text to speech (tts) is a difficult task to get right and there are quite a few packages that show just how hard with their terrible tinny voices, these are also usually only limited to English, which for the larger part than most people would like to believe don&#8217;t find it very useful. Nevermind, Google to the rescue, their <a href="http://translate.google.com">Translation tool</a> has had the ability to also voice translations for a while now. So wouldn&#8217;t it be awesome if we could utilize this, stable, constantly developing and ultimately free utlilty? Yes? Well luckly for you it&#8217;s possible. It&#8217;s been done before with the use of JavaScript, like <a href="http://weston.ruter.net/projects/google-tts/">here</a> but those more used to scripting and desktop programming don&#8217;t worry, you can use it too.</p>
<p>Basically it works by sending a request to the Google translate servers which then, pretty much instantly, reply with an MP3 file which contains the requested words in their most sexy of robot voices.</p>
<p>Check this URL for an example of said voice &#8211; <a href="http://translate.google.com/translate_tts?q=check+out+my+sexy+robot+voice">http://translate.google.com/translate_tts?q=check+out+my+sexy+robot+voice</a> (if it doesn&#8217;t load in your browser, open VLC, CTRL+N and paste the URL in)</p>
<h6>First, we want to be able to get python to stream an MP3 file off the web:</h6>
<p>More info on this can be found <a href="http://codeboje.de/playing-mp3-stream-python/">here</a>, the following code was is a stripped down version of the code found at codeboje.de.</p>
<script src="http://gist.github.com/1362973.js?file=playMP3stream.py"></script><noscript><link rel="stylesheet" href="https://gist.github.com/stylesheets/gist/embed.css"><div id="gist-1362973" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="c">#!/usr/bin/python</span></div><div class='line' id='LC2'><br/></div><div class='line' id='LC3'><span class="kn">import</span> <span class="nn">time</span>		<span class="c">#for delay</span></div><div class='line' id='LC4'><span class="kn">import</span> <span class="nn">pygst</span>		<span class="c">#for playing mp3 stream</span></div><div class='line' id='LC5'><span class="kn">import</span> <span class="nn">gst</span>		<span class="c"># &quot; &quot;</span></div><div class='line' id='LC6'><br/></div><div class='line' id='LC7'><span class="c">#Play an .mp3 file from the internet</span></div><div class='line' id='LC8'><span class="n">music_stream_uri</span> <span class="o">=</span> <span class="s">&#39;http://www.sample-url.com/file.mp3&#39;</span></div><div class='line' id='LC9'><span class="n">player</span> <span class="o">=</span> <span class="n">gst</span><span class="o">.</span><span class="n">element_factory_make</span><span class="p">(</span><span class="s">&quot;playbin&quot;</span><span class="p">,</span> <span class="s">&quot;player&quot;</span><span class="p">)</span></div><div class='line' id='LC10'><span class="n">player</span><span class="o">.</span><span class="n">set_property</span><span class="p">(</span><span class="s">&#39;uri&#39;</span><span class="p">,</span> <span class="n">music_stream_uri</span><span class="p">)</span></div><div class='line' id='LC11'><span class="n">player</span><span class="o">.</span><span class="n">set_state</span><span class="p">(</span><span class="n">gst</span><span class="o">.</span><span class="n">STATE_PLAYING</span><span class="p">)</span></div><div class='line' id='LC12'><br/></div><div class='line' id='LC13'><span class="c">#requires a delay, if the py process closes before the mp3 has finished it will be cut off.</span></div><div class='line' id='LC14'><span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">12</span><span class="p">)</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1362973/bc68a8dd1807586944708fbb24fe09716c8153ae/playMP3stream.py" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1362973#file_play_mp3stream.py" style="float:right;margin-right:10px;color:#666">playMP3stream.py</a>
            <a href="https://gist.github.com/1362973">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>
</noscript>
<h6>Now we want to make a string to send :</h6>
<p>We&#8217;re going to use command line arguments as the string which wants to be read by the Google TTS engine, the following code will grab those arguments and concatenate them into a string. It also has to be in a format like a usual Google URL, these usually replace white space for the &#8216;+&#8217; sign, that&#8217;s easy to do.</p>
<script src="http://gist.github.com/1362973.js?file=argvURLfriendly.py"></script><noscript><link rel="stylesheet" href="https://gist.github.com/stylesheets/gist/embed.css"><div id="gist-1362973" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="c">#!/usr/bin/python</span></div><div class='line' id='LC2'><br/></div><div class='line' id='LC3'><span class="kn">import</span> <span class="nn">sys</span>		<span class="c">#for cmd line argv</span></div><div class='line' id='LC4'><br/></div><div class='line' id='LC5'><span class="c">#take command line args as the input string</span></div><div class='line' id='LC6'><span class="n">input_string</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span></div><div class='line' id='LC7'><span class="c">#remove the program name from the argv list</span></div><div class='line' id='LC8'><span class="n">input_string</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span></div><div class='line' id='LC9'><br/></div><div class='line' id='LC10'><span class="c">#convert to google friendly url (with + replacing spaces)</span></div><div class='line' id='LC11'><span class="n">tts_string</span> <span class="o">=</span> <span class="s">&#39;+&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">input_string</span><span class="p">)</span></div><div class='line' id='LC12'><br/></div><div class='line' id='LC13'><span class="k">print</span> <span class="n">tts_string</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1362973/f3480729df33a3ff5cf17899a0a6354b49d86ad1/argvURLfriendly.py" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1362973#file_argv_ur_lfriendly.py" style="float:right;margin-right:10px;color:#666">argvURLfriendly.py</a>
            <a href="https://gist.github.com/1362973">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>
</noscript>
<p>Now let&#8217;s just pass this URL to the bit we wrote to stream an MP3 and watch it go:</p>
<h6>Full Source Code:</h6>
<p>(Uses command line arguments as it&#8217;s input, run like &#8216;<em>python ttsString.py Hello World</em>&#8216;)<br />
<script src="http://gist.github.com/1362973.js?file=ttsString.py"></script><noscript><link rel="stylesheet" href="https://gist.github.com/stylesheets/gist/embed.css"><div id="gist-1362973" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="c">#!/usr/bin/python</span></div><div class='line' id='LC2'><br/></div><div class='line' id='LC3'><span class="kn">import</span> <span class="nn">sys</span>		<span class="c">#for cmd line argv</span></div><div class='line' id='LC4'><span class="kn">import</span> <span class="nn">time</span>		<span class="c">#for delay</span></div><div class='line' id='LC5'><span class="kn">import</span> <span class="nn">pygst</span>		<span class="c">#for playing mp3 stream</span></div><div class='line' id='LC6'><span class="kn">import</span> <span class="nn">gst</span>		<span class="c"># &quot; &quot;</span></div><div class='line' id='LC7'><br/></div><div class='line' id='LC8'><span class="c">#take command line args as the input string</span></div><div class='line' id='LC9'><span class="n">input_string</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span></div><div class='line' id='LC10'><span class="c">#remove the program name from the argv list</span></div><div class='line' id='LC11'><span class="n">input_string</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span></div><div class='line' id='LC12'><br/></div><div class='line' id='LC13'><span class="c">#convert to google friendly url (with + replacing spaces)</span></div><div class='line' id='LC14'><span class="n">tts_string</span> <span class="o">=</span> <span class="s">&#39;+&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">input_string</span><span class="p">)</span></div><div class='line' id='LC15'><br/></div><div class='line' id='LC16'><span class="k">print</span> <span class="n">tts_string</span></div><div class='line' id='LC17'><br/></div><div class='line' id='LC18'><span class="c">#use string in combination with the translate url as the stream to be played</span></div><div class='line' id='LC19'><span class="n">music_stream_uri</span> <span class="o">=</span> <span class="s">&#39;http://translate.google.com/translate_tts?q=&#39;</span> <span class="o">+</span> <span class="n">tts_string</span></div><div class='line' id='LC20'><span class="n">player</span> <span class="o">=</span> <span class="n">gst</span><span class="o">.</span><span class="n">element_factory_make</span><span class="p">(</span><span class="s">&quot;playbin&quot;</span><span class="p">,</span> <span class="s">&quot;player&quot;</span><span class="p">)</span></div><div class='line' id='LC21'><span class="n">player</span><span class="o">.</span><span class="n">set_property</span><span class="p">(</span><span class="s">&#39;uri&#39;</span><span class="p">,</span> <span class="n">music_stream_uri</span><span class="p">)</span></div><div class='line' id='LC22'><span class="n">player</span><span class="o">.</span><span class="n">set_state</span><span class="p">(</span><span class="n">gst</span><span class="o">.</span><span class="n">STATE_PLAYING</span><span class="p">)</span></div><div class='line' id='LC23'><br/></div><div class='line' id='LC24'><span class="c">#requires a delay, if the py process closes before the mp3 has finished it will be cut off.</span></div><div class='line' id='LC25'><span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">12</span><span class="p">)</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1362973/2616de7115260a45b45ec55d84050a4dc39e8613/ttsString.py" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1362973#file_tts_string.py" style="float:right;margin-right:10px;color:#666">ttsString.py</a>
            <a href="https://gist.github.com/1362973">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>
</noscript></p>
<p>That&#8217;s pretty much how easy it is to use Google&#8217;s free online TTS engine, one main thing to watch out for is the 100 character limit to the use of this service in this way along with the API call limits which apply to each IP address. There are loads of cool things this can be used for and I&#8217;d love to see what anyone comes up with, keep me posted and have fun!</p>
]]></content:encoded>
			<wfw:commentRss>http://alexsleat.co.uk/2011/11/14/python-making-use-of-googles-text-to-speech-translation-tool/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
<enclosure url="http://translate.google.com/translate_tts?q=check+out+my+sexy+robot+voice" length="10272" type="audio/mpeg" />
		</item>
	</channel>
</rss>

