How make your computer speak using Amazon Polly
By: Steve Janco
Tags: Amazon Web Services, Polly, R, Text To Speech

Table of Contents

Introduction Setting up Polly in R Synthesizing text

1. Introduction

This article is going to show you how you can convert written text to audio files and save them down to your computer. All you need is a text file and some basic knowledge of Amazon Web Services.

This my first article on Medium and it’s a part of an assignment for my graduate studies. It intended as a general guide for Amazon Polly and the packages it uses in R to convert written text to speech. So without further hesitation, let’s start the review.

Polly is part of the AI/ML service family available on Amazon Web Services (AWS) page. It is able to transform plain text objects to speech. The service creates very detailed audio from the input text in a number of voices. The punctuation in the text is used by the service, as indication for pauses and breaks. The text can be converted either using Amazon Polly in the AWS Console, through the AWS Command Line Interface (CLI) or an API provided by the AWS Software Development Kit and R code. Let’s have a look at the latter and how to connect to AWS and set up R.

2. Setting up Polly in R

The first step is set up to access to Amazon Polly. You would need an AWS account for that. It can be created in about 10 minutes. After the account is all set, an access key needs to be generated on Amazon’s Identity and Access Management (IAM) Console (see the image below). The key will allow for secure communication between your machine and the API. Once the access key is created, make sure to save it to your R project’s folder.

Creating an access key on Amazon IAM

The second step is to fire up your R console and connect it to Amazon’s API. Luckily, the aws.polly is a handy R’s package that does that for you. You can check out its mechanics on this Git page. Install the polly package along with the tuneR pack, that will let you playback the synthesized text. In addition, we need to provide the generated AWS access keys to aws.polly in order to log in to the service.

Setting up AWS Polly: link

3. Synthesizing text

Once you are logged in to the service, you can call the aws.polly package. There is a list of vices that can be used. It can be examined by using the list_voices() command. There are 61 different male and female voices, including 29 languages. The full list is also available here. After you’ve chosen your favorite voice and the text you want to synthesize, you’ll need to use the synthesize command and assign the outcome to a variable.

Synthesizing text: link

In order to make your computer to playback the synthesized text, we need to call the tuneR package and in case you are using a Mac computer set the player using the setWavPlayer(“afplay”). Execute the play() command and there you go, you’ve synthesized your first text! You can check out the results below.

The text can also be saved it down to your computer by using the writeWave() command. Just like that you’re now able to turn text into audio. One thing to note however, is the character limit of 1500 used by Polly. Unfortunately, in case of synthesizing bigger texts (above 1500 characters), you’ll have to do it in pieces. Hopefully this post provided a good overview of AWS Polly and its main features and will encourage you to try to experiment with it.