<?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>Programming - Glenn Wehmeyer</title>
	<atom:link href="https://glennwehmeyer.com/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>https://glennwehmeyer.com</link>
	<description></description>
	<lastBuildDate>Mon, 28 Jan 2019 13:24:49 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.7.1</generator>
	<item>
		<title>Creating a text to speech server w/ Python</title>
		<link>https://glennwehmeyer.com/2019/01/11/creating-text-to-speech-server-w-python/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=creating-text-to-speech-server-w-python</link>
					<comments>https://glennwehmeyer.com/2019/01/11/creating-text-to-speech-server-w-python/#respond</comments>
		
		<dc:creator><![CDATA[Glenn W.]]></dc:creator>
		<pubDate>Fri, 11 Jan 2019 07:51:54 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<guid isPermaLink="false">http://glennwehmeyer.com/?p=241</guid>

					<description><![CDATA[<p>A short little article on using Python and MQTT server to send messages to a text to speech server on my MacBook [&#8230;]</p>
<p>The post <a href="https://glennwehmeyer.com/2019/01/11/creating-text-to-speech-server-w-python/">Creating a text to speech server w/ Python</a> first appeared on <a href="https://glennwehmeyer.com">Glenn Wehmeyer</a>.</p>]]></description>
										<content:encoded><![CDATA[<p class="has-drop-cap has-medium-font-size">A short little article on using Python and MQTT server to send messages to a text to speech server on my MacBook Air.</p>



<p>I was looking for a nice sounding text to speech agent one day and started to do a little research.  I tried Amazon Polly and Google Text to Speech.  They were ok to use but they require me to connect to their services.  Then I realized that the voices that Apple includes in all copies of MacOS X.   The command on MacOS was super simple &#8211; &#8220;say -v {voice}  {string to say}&#8221;.   After playing with the <strong>say</strong> command I found the right voice &#8211; A nice <g class="gr_ gr_268 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="268" data-gr-id="268">aussie</g> voice named Karen.  </p>



<figure class="wp-block-embed is-type-rich is-provider-embed-handler"><div class="wp-block-embed__wrapper">
<!--[if lt IE 9]><script>document.createElement('audio');</script><![endif]-->
<audio class="wp-audio-shortcode" id="audio-241-1" preload="none" style="width: 100%;" controls="controls"><source type="audio/mpeg" src="https://glennwehmeyer.com/wp-content/uploads/2019/01/youfreaks.mp3?_=1" /><a href="https://glennwehmeyer.com/wp-content/uploads/2019/01/youfreaks.mp3">https://glennwehmeyer.com/wp-content/uploads/2019/01/youfreaks.mp3</a></audio>
</div><figcaption>A sample of Karen&#8217;s voice</figcaption></figure>



<p>Now it was time to put together some Python code to read from the MQTT server on my local <g class="gr_ gr_3 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="3" data-gr-id="3">linux</g> server and play the message on my MacBook Air.</p>



<pre class="wp-block-preformatted">"""<br> my TTS Server version 1.5<br> Description: A Text to Speech server for Mac<br> Revision notes: 1.5 is implementing mqtt client for text to speak<br> """<br> import paho.mqtt.client as mqtt<br> import os<br> import sys<br> import signal<br> def signal_handler(signal, frame):<br>     global interrupted<br>     interrupted = True<br> signal.signal(signal.SIGINT, signal_handler)<br> def on_connect(client,userdata,flags,rc):<br>     print "We connected - " + str(rc)<br>     <code>client.subscribe("myTTS/message")</code><br> def on_message(client,userdata,msg):<br>     print msg.topic+" "+str(msg.payload)<br>     os.system('say -v Karen "'+str(msg.payload)+'"')<br> def say_message(themsg):<br>     print themsg<br>     os.system('say -v Karen "'+str(themsg)+'"')<br> print("MyTTS Server 1.5 w/ MQTT starting up")<br> """os.system('say -v Karen "TTS Server 1.5 Starting up!"')<br> """<br> say_message("TTS Server 1.5 Starting up!")<br> interrupted = False<br> client = mqtt.Client()<br> client.on_connect = on_connect<br> client.on_message = on_message<br> client.connect("192.168.0.200", 1883, 60)<br> while True:<br>     client.loop()<br>     <code>if interrupted:     </code><br><code>           print "TTS Server is shutting down"     </code><br><code>           client.disconnect()     </code><br><code>           break</code></pre>



<p>The python library for communicating with <g class="gr_ gr_11 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar multiReplace" id="11" data-gr-id="11">a MQTT</g> server is called <g class="gr_ gr_5 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="5" data-gr-id="5">paho</g>&#8211;<g class="gr_ gr_6 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling" id="6" data-gr-id="6">mqtt</g> and you can install it via <strong>pip</strong>. The next thing you notice is that I imported <strong>signal</strong> class.  I want to use <g class="gr_ gr_12 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" id="12" data-gr-id="12">signal</g> to catch a Ctrl-C (or signal from OS) and shut down the server gracefully.  The on_connect function deals with subscribing to the correct message queue to receive messages from.. in this case &#8220;myTTS/message&#8221;.  The on_message takes a message that was put in the message queue and uses the system function to call say with the message.  The main body of the server takes care of starting the <g class="gr_ gr_725 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="725" data-gr-id="725">mqtt</g> client and starting a loop to listen for messages.</p>



<p>I used <g class="gr_ gr_5 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="5" data-gr-id="5">mosquitto</g> for my MQTT server.  It came as a package for Centos 7, so it was easy to install.  You can also find it here with setup instructions: https://mosquitto.org/download/<br><br>Next time I&#8217;ll show another python app that put random messages (like the time)  in the queue for the TTS server to say.</p><p>The post <a href="https://glennwehmeyer.com/2019/01/11/creating-text-to-speech-server-w-python/">Creating a text to speech server w/ Python</a> first appeared on <a href="https://glennwehmeyer.com">Glenn Wehmeyer</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://glennwehmeyer.com/2019/01/11/creating-text-to-speech-server-w-python/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		<enclosure url="http://glennwehmeyer.com/wp-content/uploads/2019/01/youfreaks.mp3" length="12396" type="audio/mpeg" />

			</item>
	</channel>
</rss>
