Unverified Commit bc511ec7 authored by Russell Cohen's avatar Russell Cohen Committed by GitHub
Browse files

Add two Amazon Polly examples (#286)



* Add to Amazon Polly examples

* Fix copy-paste error

* Update aws/sdk/examples/polly-helloworld/src/main.rs

Co-authored-by: default avatarDavid Barsky <dbarsky@amazon.com>

* Update aws/sdk/examples/polly-helloworld/src/main.rs

Co-authored-by: default avatarDavid Barsky <dbarsky@amazon.com>

* Some cleanups

* Rerun precommit

* CR Feedback

* Fix clippy lint

* Small cleanup

Co-authored-by: default avatarDavid Barsky <dbarsky@amazon.com>
parent 6b605892
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
audio.mp3
+11 −0
Original line number Diff line number Diff line
[package]
name = "polly-generate-speech"
version = "0.1.0"
authors = ["Russell Cohen <rcoh@amazon.com>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
polly = { path = "../../build/aws-sdk/polly"}
tokio = { version = "1", features = ["full"] }
+25 −0
Original line number Diff line number Diff line
use polly::model::{Engine, OutputFormat, VoiceId};
use std::error::Error;
use tokio::fs::File;
use tokio::io::AsyncWriteExt;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
    let client = polly::fluent::Client::from_env();
    let resp = client
        .synthesize_speech()
        .voice_id(VoiceId::Emma)
        .engine(Engine::Neural)
        .output_format(OutputFormat::Mp3)
        .text("Hello, I am polly!")
        .send()
        .await?;
    let audio = resp.audio_stream.expect("data should be included");
    let mut file = File::create("audio.mp3").await?;
    file.write_all(audio.as_ref()).await?;
    println!(
        "Audio written to audio.mp3 ({} bytes)",
        audio.as_ref().len()
    );
    Ok(())
}
+11 −0
Original line number Diff line number Diff line
[package]
name = "polly-helloworld"
version = "0.1.0"
authors = ["Russell Cohen <rcoh@amazon.com>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
polly = { path = "../../build/aws-sdk/polly"}
tokio = { version = "1", features = ["full"] }
+43 −0
Original line number Diff line number Diff line
use polly::model::{Engine, Voice};
use std::error::Error;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
    let client = polly::fluent::Client::from_env();
    let mut tok = None;
    let mut voices: Vec<Voice> = vec![];
    // Below is an an example of how pagination can be implemented manually.
    loop {
        let mut req = client.describe_voices();
        if let Some(tok) = tok {
            req = req.next_token(tok);
        }
        let resp = req.send().await?;
        for voice in resp.voices.unwrap_or_default() {
            println!(
                "I can speak as: {} in {:?}",
                voice.name.as_ref().unwrap(),
                voice.language_name.as_ref().unwrap()
            );
            voices.push(voice);
        }
        tok = match resp.next_token {
            Some(next) => Some(next),
            None => break,
        };
    }
    let neural_voices = voices
        .iter()
        .filter(|voice| {
            voice
                .supported_engines
                .as_deref()
                .unwrap_or_default()
                .contains(&Engine::Neural)
        })
        .map(|voice| voice.id.as_ref().unwrap())
        .collect::<Vec<_>>();

    println!("Voices supporting a neural engine: {:?}", neural_voices);
    Ok(())
}
Loading