<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/make-r-speak/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://blog.revolutionanalytics.com/2018/08/make-r-speak.html"> Revolutions</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>
<p>Every wanted to make R talk to you? Now you can, with the <a rel="nofollow" href="https://mran.microsoft.com/package/mscstts" target="_blank">mscstts package</a> by John Muschelli. It provides an interface to the Microsoft Cognitive Services <a rel="nofollow" href="https://docs.microsoft.com/azure/cognitive-services/speech/home?WT.mc_id=Revolutions-blog-davidsmi" target="_blank">Text-to-Speech API</a> (hence the name) in Azure, and you can use it to convert any short piece of text to a playable audio file, rendering it as speech using a number of different voices.</p>
<p>Before you can generate speech yourself, you'll need a Bing Speech API key. If you don't already have an Azure account, you can generate a free 7-day API key in seconds by visiting the <a rel="nofollow" href="https://azure.microsoft.com/try/cognitive-services/?WT.mc_id=Revolutions-blog-davidsmi" target="_blank">Try Cognitive Services page</a>, selecting &quot;Speech APIs&quot;, and clicking &quot;Get API Key&quot; for &quot;Bing Speech&quot;:</p>
<p><a rel="nofollow" href="https://azure.microsoft.com/try/cognitive-services/?WT.mc_id=Revolutions-blog-davidsmi" target="_blank"><img border="0" alt="Bing speech" title="Bing speech" src="http://revolution-computing.typepad.com/.a/6a010534b1db25970b022ad389eec7200d-800wi"></a></p>
<p>No credit card is needed; all you need is a Microsoft, Facebook, LinkedIn or Github account. (If you need permanent keys, you can <a rel="nofollow" href="https://azure.microsoft.com/free/?WT.mc_id=Revolutions-blog-davidsmi" target="_blank">create an Azure account here</a> which you can use for 5000 free Speech API calls per month, and <em>also </em>get $200 in free credits for use with any Azure service.)</p>
<p>Once you have your key (you'll actually get two, but you can use either one) you will call the <a rel="nofollow" href="https://www.rdocumentation.org/packages/mscstts/versions/0.2.2/topics/ms_synthesize?tap_a=5644-dce66f&tap_s=10907-287229" target="_blank"><code>ms_synthesize</code> function</a> to convert text up to 1,000 characters or so into mp3 data, which you can then save to a file. (See lines 8-10 in the <a rel="nofollow" href="https://gist.github.com/revodavid/d650bd6b31f5ec5b3ae633695aedb276" target="_blank"><code>speakR.R</code> script</a>, below.) Then, you can play the file with any MP3 player on your system. On Windows, the easiest way to do that is to call <code>start</code> on the MP3 file itself, which will use your system default. (You'll need to modify line 11 of the script to work non-Windows systems.)</p>
<p>Saving the data to a file and then invoking a player got a bit tedious for me, so I created the <code>say</code> function in the script below to automate the process. Let's see it in action:</p>
<p><iframe allowfullscreen="" src="https://www.youtube.com/embed/vG3rEH4gba8" width="450" height="283"></iframe></p>
<p>Note that you can choose from a number of <a rel="nofollow" href="https://docs.microsoft.com/azure/cognitive-services/speech/api-reference-rest/bingvoiceoutput?WT.mc_id=Revolutions-blog-davidsmi#SupLocales" target="_blank">accents and spoken languages</a> (including British English, Canadian French, Chinese and Japanese), and the gender of the voice (though both female and male voices aren't available for all languages).  You can even modify volume, pitch, speaking rate, and even the pronunciation of individual words using the <a rel="nofollow" href="https://docs.microsoft.com/azure/cognitive-services/speech/api-reference-rest/bingvoiceoutput?WT.mc_id=Revolutions-blog-davidsmi#ChangeSSML" target="_blank">SSML standard</a>. (This does mean you can't use characters recognized as SSML in your text, which is why the <code>say</code> function below filters out <code>&lt; ></code> and <code>/</code> first.) </p>
<p>The mscstts package is available for download now from your favorite CRAN mirror, and you can find the latest <a rel="nofollow" href="https://github.com/muschellij2/mscstts" target="_blank">development version on Github</a>. Many thanks to John Muschelli for putting this handy package together!</p>
<p></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="http://blog.revolutionanalytics.com/2018/08/make-r-speak.html"> Revolutions</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/cEPoclZlfNU" height="1"></a></div>