Make R speak with the Bing Speech API

David Smith
Aug 16, 2018 · 3 min read

Ever wanted to make R talk to you? Now you can, with the mscstts package by John Muschelli. It provides an interface to the Microsoft Cognitive Services Text-to-Speech API (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.

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 Try Cognitive Services page, selecting “Speech APIs”, and clicking “Get API Key” for “Bing Speech”:

No credit card is needed; all you need is a Microsoft, Facebook, LinkedIn or Github account. (If you need permanent keys, you can create an Azure account here which you can use for 5000 free Speech API calls per month, and also get $200 in free credits for use with any Azure service.)

Once you have your key (you’ll actually get two, but you can use either one) you will call the function 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 script, 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 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.)

Saving the data to a file and then invoking a player got a bit tedious for me, so I created the function in the script below to automate the process. Let's see it in action:

Note that you can choose from a number of accents and spoken languages (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 SSML standard. (This does mean you can’t use characters recognized as SSML in your text, which is why the function below filters out and first.)

The mscstts package is available for download now from your favorite CRAN mirror, and you can find the latest development version on Github. Many thanks to John Muschelli for putting this handy package together!

Originally published at blog.revolutionanalytics.com on August 16, 2018.

Microsoft Azure

Any language. Any platform. Our team is focused on making the world more amazing for developers and IT operations communities with the best that Microsoft Azure can provide. If you want to contribute in this journey with us, contact us at medium@microsoft.com

David Smith

Written by

Developer Advocate at Microsoft focused on AI, Data Science, and the R language.

Microsoft Azure

Any language. Any platform. Our team is focused on making the world more amazing for developers and IT operations communities with the best that Microsoft Azure can provide. If you want to contribute in this journey with us, contact us at medium@microsoft.com