Page updated Jan 16, 2024

Text-to-speech

Set up the backend

If you haven't already done so, run amplify init inside your project and then amplify add auth (we recommend selecting the default configuration).

Run amplify add predictions, then use the following answers:

1? Please select from one of the categories below
2 Identify
3❯ Convert
4 Interpret
5 Infer
6 Learn More
7
8? What would you like to convert?
9 Translate text into a different language
10❯ Generate speech audio from text
11 Transcribe text from audio
12
13? Provide a friendly name for your resource
14 <Enter a friendly name here>
15
16? What is the source language? (Use arrow keys)
17 <Select your default source language>
18
19? Select a speaker (Use arrow keys)
20 <Select your default speaker voice>
21
22? Who should have access?
23 Auth users only
24❯ Auth and Guest users

Run amplify push to create the resources in the cloud

Working with the API

Here is an example of converting text to speech. In order to override any choices you made while adding this resource using the Amplify CLI, you can pass in a voice in the options object as shown below.

1import Amplify
2import AWSPredictionsPlugin
3import AVFoundation
4
5//...
6
7var player: AVAudioPlayer?
8
9//...
10
11func textToSpeech() async throws {
12 let result = try await Amplify.Predictions.convert(
13 .textToSpeech("Hello, world!"),
14 options: .init(voice: .englishFemaleIvy)
15 )
16 print("TextToSpeech result: \(result)")
17 self.player = try AVAudioPlayer(data: result.audioData)
18 player?.play()
19}
1import Amplify
2import AWSPredictionsPlugin
3import AVFoundation
4
5//...
6
7var player: AVAudioPlayer?
8var textToSpeechSink: AnyCancellable?
9
10//...
11
12func textToSpeech() {
13 textToSpeechSink = Amplify.Publisher.create {
14 try await Amplify.Predictions.convert(
15 .textToSpeech("Hello, world!"),
16 options: .init(voice: .englishFemaleIvy)
17 )
18 }
19 .sink(receiveCompletion: { completion in
20 if case let .failure(error) = completion {
21 print("Error converting text to speech: \(error)")
22 }
23 }, receiveValue: { result in
24 print("TextToSpeech result: \(result)")
25 self.player = try? AVAudioPlayer(data: result.audioData)
26 self.player?.play()
27 })
28}

As a result of running this code, you will hear audio of the text being emitted from your device.