Yesterday, I was setting up a new VOIP phone number and I decided that I wanted a professional sounding voicemail greeting for prompting people to leave a message. In the past, I’ve used services that enable you to pay a voice actor to read your voicemail greeting. But hiring a human to read some text seems perverse in this new era of artificial intelligence.
In this blog post, I show you can use the OpenAI Text to Speech endpoint to generate a voicemail greeting. Here’s what the final recording sounds like:
This whole project should take you less than 15 minutes.
Setting Everything Up
Ensure that you have your OpenAI project setup correctly by following the instructions in my previous blog post:
Calling the OpenAI Text to Speech API and Generating an MP3 File
Here’s the code that I used to generate the voicemail greeting:
All of the work happens in the main() function. This function does two things. First, it calls the openai.audio.speech.create()
method to convert the text into an audio file:
In the code above, the following three arguments are passed to this method:
model
– The name of an OpenAI Text to Speech model. You can use either the tts-1 or the tts-1-hd model. The tts-1-hd produces higher definition audio output but it is slower.voice
– The name of the voice to use. The options arealloy
,echo
,fable
,onyx
,nova
, andshimmer
.input
– The text to convert to audio. The text cannot be longer than 4,096 characters. In the code above, the backtick character ` is used to wrap the text across multiple lines.
Because the openai.audio.speech.create()
method calls an API behind the scenes, the method is asynchronous and it returns a promise instead of the actual audio file. The following code is used to take the audio data being streamed back from the API call and write the data to a file on your hard drive:
Summary
The goal of this blog post was to illustrate how you can easily convert text to speech by using the OpenAI Speech endpoint. This was a really simple application of this amazing technology but I hope that this introduction was enough to get you started on more advanced applications.