<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/text-to-speech-with-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://shirinsplayground.netlify.com/2018/06/text_to_speech_r/"> Shirin's playgRound</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Computers started talking to us! They do this with so called Text-to-Speech (TTS) systems. With neural nets, deep learning and lots of training data, these systems have gotten a whole lot better in recent years. In some cases, they are so good that you can’t distinguish between human and machine voice.</p>
<p>In one of our recent <a rel="nofollow" href="https://blog.codecentric.de/2018/04/kuenstliche-intelligenz-codecentric_ai/" target="_blank">codecentric.AI</a> <a rel="nofollow" href="https://youtu.be/2EEMSsVBE8w" target="_blank">videos</a>, we compared <a rel="nofollow" href="https://youtu.be/2EEMSsVBE8w" target="_blank">different Text-to-Speech systems</a> (the video is in German, though – but the text snippets and their voice recordings we show in the video are a mix of German and English). In this video, we had a small contest between Polly, Alexa, Siri And Co to find out who best speaks different tongue twisters.</p>
<p> <iframe width="450" allowfullscreen src="https://www.youtube.com/embed/2EEMSsVBE8w"></iframe></p>
<p>Here, I want to find out what’s possible with R and Text-to-Speech packages.</p>
<div>
<h2>How does TTS work?</h2>
<p>Challenges for good TTS systems are the complexity of the human language: we intone words differently, depending on where they are in a sentence, what we want to convey with that sentence, how our mood is, and so on. AI-based TTS systems can take phonemes and intonation into account.</p>
<p>There are different ways to artificially produce speech. A very important method is Unit Selection synthesis. With this method, text is first normalized and divided into smaller entities that represent sentences, syllables, words, phonemes, etc. The structure (e.g. the pronunciation) of these entities is then learned in context. We call this part Natural Language Processing (NLP). Usually, these learned segments are stored in a database (either as human voice recordings or synthetically generated) that can be searched to find suitable speech parts (Unit Selection). This search is often done with decision trees, neural nets or Hidden-Markov-Models.</p>
<p>If the speech has been generated by a computer, this is called formant synthesis. It offers more flexibility because the collection of words isn’t limited to what has been pre-recorded by a human. Even imaginary or new words can easily be produced and the voices can be readily exchanged. Until recently, this synthetic voice did not sound anything like a human recorded voice; you could definitely hear that it was “fake”. Most of the TTS systems today still suffer from this, but this is in the process of changing: there are already a few artificial TTS systems that do sound very human.</p>
</div>
<div>
<h2>What TTS systems are there?</h2>
<p>We already find TTS systems in many digital devices, like computers, smart phones, etc. Most of the “big players” offer TTS-as-a-service, but there are also many “smaller” and free programs for TTS. Many can be downloaded as software or used from a web browser or as an API. Here is an incomplete list:</p>
<ul>
<li>Microsoft/Windows: includes <a rel="nofollow" href="https://support.microsoft.com/de-de/help/22798/windows-10-narrator-get-started" target="_blank"><strong>Narrator</strong></a> and <a rel="nofollow" href="https://www.microsoft.com/en-us/download/details.aspx?id=10121" target="_blank"><strong>Microsoft Speech API</strong></a></li>
<li>Mac: <a rel="nofollow" href="https://www.apple.com/de/accessibility/mac/vision/" target="_blank"><strong>VoiceOver</strong></a></li>
<li>Linux: different software can be installed, e.g. <a rel="nofollow" href="https://sourceforge.net/projects/espeak/files/espeak/" target="_blank"><strong>eSpeak</strong></a></li>
<li><a rel="nofollow" href="https://www.ibm.com/watson/services/text-to-speech/" target="_blank">IBM Watson</a></li>
<li><a rel="nofollow" href="https://cloud.google.com/text-to-speech/" target="_blank">Google Cloud</a></li>
<li><a rel="nofollow" href="https://azure.microsoft.com/de-de/services/cognitive-services/speech/" target="_blank">Microsoft Azure</a></li>
<li><a rel="nofollow" href="https://developer.amazon.com/de/alexa" target="_blank">Amazon Alexa</a></li>
<li><a rel="nofollow" href="https://9to5mac.com/2017/03/10/how-to-get-siri-to-read-articles-to-you-on-ios-macos/" target="_blank">Siri on iPhone</a></li>
<li><a rel="nofollow" href="https://aws.amazon.com/de/polly/" target="_blank">Polly on Amazon AWS</a></li>
<li><a rel="nofollow" href="https://support.microsoft.com/de-de/help/17214/windows-10-what-is" target="_blank">Microsoft Cortana</a></li>
<li><a rel="nofollow" href="https://freetts.sourceforge.io/docs/" target="_blank">FreeTTS</a></li>
<li><a rel="nofollow" href="https://www.ispeech.org/text.to.speech" target="_blank">iSpeech</a></li>
<li><a rel="nofollow" href="https://www.naturalreaders.com/online/" target="_blank">Natural Readers</a></li>
<li><a rel="nofollow" href="http://www.cross-plus-a.com/balabolka.htm" target="_blank">Balabolka</a></li>
<li><a rel="nofollow" href="https://www.panopreter.com/en/products/pb/download.php" target="_blank">Panopreter</a></li>
<li><a rel="nofollow" href="https://www.text2speech.org/" target="_blank">text2speech.org</a></li>
<li><a rel="nofollow" href="http://text-to-speech-translator.paralink.com/" target="_blank">text-to-speech-translator.paralink.com/</a></li>
</ul>
</div>
<div>
<h2>Text-to-Speech in R</h2>
<p>The only package for TTS I found was <code>Rtts</code>. It doesn’t seem very comprehensive but it does the job of converting text to speech. The only API that works right now is **ITRI (<a rel="nofollow" href="http://tts.itri.org.tw)**/" target="_blank">http://tts.itri.org.tw)**</a>. And it only supports English and Chinese.</p>
<p>Let’s try it out!</p>
<pre><code>library(Rtts)</code></pre>
<pre><code>## Lade nötiges Paket: RCurl</code></pre>
<pre><code>## Lade nötiges Paket: bitops</code></pre>
<p>Here, I’ll be using a quote from <strong>DOUGLAS ADAMS’ THE HITCHHIKER’S GUIDE TO THE GALAXY</strong>:</p>
<pre><code>content &lt;- &quot;A common mistake that people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools.&quot;</code></pre>
<p>The main TTS function is <code>tts_ITRI()</code> and I’m going to loop over the different voice options.</p>
<pre><code>speakers = c(&quot;Bruce&quot;, &quot;Theresa&quot;, &quot;Angela&quot;, &quot;MCHEN_Bruce&quot;, &quot;MCHEN_Joddess&quot;, &quot;ENG_Bob&quot;, &quot;ENG_Alice&quot;, &quot;ENG_Tracy&quot;)
lapply(speakers, function(x) tts_ITRI(content, speaker = x,
         destfile = paste0(&quot;audio_tts_&quot;, x, &quot;.mp3&quot;)))</code></pre>
<p>I uploaded the results to Soundcloud for you to hear: – <a rel="nofollow" href="https://soundcloud.com/shirin-glander-729692416/audio-tts-bruce/s-iZC6u?in=shirin-glander-729692416/sets/text-to-speech-blogpost/s-lXYea" target="_blank">audio-tts-bruce</a> – <a rel="nofollow" href="https://soundcloud.com/shirin-glander-729692416/audio-tts-theresa/s-lYt0R?in=shirin-glander-729692416/sets/text-to-speech-blogpost/s-lXYea" target="_blank">audio-tts-theresa</a> – <a rel="nofollow" href="https://soundcloud.com/shirin-glander-729692416/audio-tts-angela/s-KVUMS?in=shirin-glander-729692416/sets/text-to-speech-blogpost/s-lXYea" target="_blank">audio-tts-angela</a> – <a rel="nofollow" href="https://soundcloud.com/shirin-glander-729692416/audio-tts-mchen-bruce/s-KVDeb?in=shirin-glander-729692416/sets/text-to-speech-blogpost/s-lXYea" target="_blank">audio-tts-mchen-bruce</a> – <a rel="nofollow" href="https://soundcloud.com/shirin-glander-729692416/audio-tts-mchen-joddess/s-mDdik?in=shirin-glander-729692416/sets/text-to-speech-blogpost/s-lXYea" target="_blank">audio-tts-mchen-joddess</a> – <a rel="nofollow" href="https://soundcloud.com/shirin-glander-729692416/audio-tts-eng-bob/s-520Y2?in=shirin-glander-729692416/sets/text-to-speech-blogpost/s-lXYea" target="_blank">audio-tts-eng-bob</a> – <a rel="nofollow" href="https://soundcloud.com/shirin-glander-729692416/audio-tts-eng-alice/s-BKTpj?in=shirin-glander-729692416/sets/text-to-speech-blogpost/s-lXYea" target="_blank">audio-tts-eng-alice</a> – <a rel="nofollow" href="https://soundcloud.com/shirin-glander-729692416/audio-tts-eng-tracy/s-SKVDm?in=shirin-glander-729692416/sets/text-to-speech-blogpost/s-lXYea" target="_blank">audio-tts-eng-tracy</a></p>
<p>As you can hear, it sounds quite wonky. There are many better alternatives out there, but most of them aren’t free and/or can’t be used (as easily) from R. Noam Ross tried <a rel="nofollow" href="https://rpubs.com/noamross/153216" target="_blank">IBM Watson’s TTS API in this post</a>, which would be a very good solution. Or you could access the <a rel="nofollow" href="https://cloud.google.com/text-to-speech/" target="_blank">Google Cloud</a> API from within R.</p>
<p>The most convenient solution for me was to use <a rel="nofollow" href="https://sourceforge.net/projects/espeak/files/espeak/" target="_blank"><strong>eSpeak</strong></a> from the command line. The output sounds relatively good, it is free and offers many languages and voices with lots of parameters to tweak. This is how you would produce audio from text with eSpeak:</p>
<ul>
<li>English US</li>
</ul>
<pre><code>espeak -v english-us -s 150 -w '/Users/shiringlander/Documents/Github/audio_tts_espeak_en_us.wav' &quot;A common mistake that people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools.&quot;</code></pre>
<ul>
<li>just for fun: English Scottish</li>
</ul>
<pre><code>espeak -v en-scottish -s 150 -w '/Users/shiringlander/Documents/Github/audio_tts_espeak_en-scottish.wav' &quot;A common mistake that people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools.&quot;</code></pre>
<ul>
<li>even funnier: German</li>
</ul>
<pre><code>espeak -v german -s 150 -w '/Users/shiringlander/Documents/Github/audio_tts_espeak_german.wav' &quot;A common mistake that people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools.&quot;</code></pre>
<p>The <a rel="nofollow" href="https://soundcloud.com/shirin-glander-729692416/sets/text-to-speech-blogpost/s-lXYea" target="_blank">playlist</a> contains all audio files I generated in this post.</p>
<hr>
<pre><code>sessionInfo()</code></pre>
<pre><code>## R version 3.5.0 (2018-04-23)
## Platform: x86_64-apple-darwin15.6.0 (64-bit)
## Running under: macOS High Sierra 10.13.5
## 
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib
## 
## locale:
## [1] de_DE.UTF-8/de_DE.UTF-8/de_DE.UTF-8/C/de_DE.UTF-8/de_DE.UTF-8
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] Rtts_0.3.3      RCurl_1.95-4.10 bitops_1.0-6   
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_0.12.17    bookdown_0.7    digest_0.6.15   rprojroot_1.3-2
##  [5] backports_1.1.2 magrittr_1.5    evaluate_0.10.1 blogdown_0.6   
##  [9] stringi_1.2.3   rmarkdown_1.10  tools_3.5.0     stringr_1.3.1  
## [13] xfun_0.2        yaml_2.1.19     compiler_3.5.0  htmltools_0.3.6
## [17] knitr_1.20</code></pre>
</div>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://shirinsplayground.netlify.com/2018/06/text_to_speech_r/"> Shirin's playgRound</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/tj4Ggu4VjA0" height="1"></a></div>