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 below2 Identify3❯ Convert4 Interpret5 Infer6 Learn More7 8? What would you like to convert?9 Translate text into a different language10❯ Generate speech audio from text11 Transcribe text from audio12
13? Provide a friendly name for your resource14 <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 only24❯ 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 Amplify2import AWSPredictionsPlugin3import AVFoundation4
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 Amplify2import AWSPredictionsPlugin3import AVFoundation4
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 in20 if case let .failure(error) = completion {21 print("Error converting text to speech: \(error)")22 }23 }, receiveValue: { result in24 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.