Amazon Polly examples using SDK for Rust - AWS SDK for Rust

Amazon Polly examples using SDK for Rust

The following code examples show you how to perform actions and implement common scenarios by using the AWS SDK for Rust with Amazon Polly.

Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios.

Scenarios are code examples that show you how to accomplish specific tasks by calling multiple functions within a service or combined with other AWS services.

Each example includes a link to the complete source code, where you can find instructions on how to set up and run the code in context.

Actions

The following code example shows how to use DescribeVoices.

SDK for Rust
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

async fn list_voices(client: &Client) -> Result<(), Error> { let resp = client.describe_voices().send().await?; println!("Voices:"); let voices = resp.voices(); for voice in voices { println!(" Name: {}", voice.name().unwrap_or("No name!")); println!( " Language: {}", voice.language_name().unwrap_or("No language!") ); println!(); } println!("Found {} voices", voices.len()); Ok(()) }
  • For API details, see DescribeVoices in AWS SDK for Rust API reference.

The following code example shows how to use ListLexicons.

SDK for Rust
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

async fn show_lexicons(client: &Client) -> Result<(), Error> { let resp = client.list_lexicons().send().await?; println!("Lexicons:"); let lexicons = resp.lexicons(); for lexicon in lexicons { println!(" Name: {}", lexicon.name().unwrap_or_default()); println!( " Language: {:?}\n", lexicon .attributes() .as_ref() .map(|attrib| attrib .language_code .as_ref() .expect("languages must have language codes")) .expect("languages must have attributes") ); } println!(); println!("Found {} lexicons.", lexicons.len()); println!(); Ok(()) }
  • For API details, see ListLexicons in AWS SDK for Rust API reference.

The following code example shows how to use PutLexicon.

SDK for Rust
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

async fn make_lexicon(client: &Client, name: &str, from: &str, to: &str) -> Result<(), Error> { let content = format!("<?xml version=\"1.0\" encoding=\"UTF-8\"?> <lexicon version=\"1.0\" xmlns=\"http://www.w3.org/2005/01/pronunciation-lexicon\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.w3.org/2005/01/pronunciation-lexicon http://www.w3.org/TR/2007/CR-pronunciation-lexicon-20071212/pls.xsd\" alphabet=\"ipa\" xml:lang=\"en-US\"> <lexeme><grapheme>{}</grapheme><alias>{}</alias></lexeme> </lexicon>", from, to); client .put_lexicon() .name(name) .content(content) .send() .await?; println!("Added lexicon"); Ok(()) }
  • For API details, see PutLexicon in AWS SDK for Rust API reference.

The following code example shows how to use SynthesizeSpeech.

SDK for Rust
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

async fn synthesize(client: &Client, filename: &str) -> Result<(), Error> { let content = fs::read_to_string(filename); let resp = client .synthesize_speech() .output_format(OutputFormat::Mp3) .text(content.unwrap()) .voice_id(VoiceId::Joanna) .send() .await?; // Get MP3 data from response and save it let mut blob = resp .audio_stream .collect() .await .expect("failed to read data"); let parts: Vec<&str> = filename.split('.').collect(); let out_file = format!("{}{}", String::from(parts[0]), ".mp3"); let mut file = tokio::fs::File::create(out_file) .await .expect("failed to create file"); file.write_all_buf(&mut blob) .await .expect("failed to write to file"); Ok(()) }

Scenarios

The following code example shows how to:

  • Use Amazon Polly to synthesize a plain text (UTF-8) input file to an audio file.

  • Upload the audio file to an Amazon S3 bucket.

  • Use Amazon Transcribe to convert the audio file to text.

  • Display the text.

SDK for Rust

Use Amazon Polly to synthesize a plain text (UTF-8) input file to an audio file, upload the audio file to an Amazon S3 bucket, use Amazon Transcribe to convert that audio file to text, and display the text.

For complete source code and instructions on how to set up and run, see the full example on GitHub.

Services used in this example
  • Amazon Polly

  • Amazon S3

  • Amazon Transcribe