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: David Barsky <dbarsky@amazon.com>

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

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

* Some cleanups

* Rerun precommit

* CR Feedback

* Fix clippy lint

* Small cleanup

Co-authored-by: David Barsky <dbarsky@amazon.com>
This commit is contained in:
Russell Cohen 2021-04-01 21:49:57 -04:00 committed by GitHub
parent 6b605892c7
commit bc511ec77f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 94 additions and 4 deletions

View File

@ -0,0 +1 @@
audio.mp3

View File

@ -0,0 +1,11 @@
[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"] }

View File

@ -0,0 +1,25 @@
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(())
}

View File

@ -0,0 +1,11 @@
[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"] }

View File

@ -0,0 +1,43 @@
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(())
}

View File

@ -101,4 +101,4 @@ structure EnumQueryInput {
@httpLabel
@required
enum: StringEnum
}
}

View File

@ -80,7 +80,7 @@ class CombinedErrorGenerator(
rust(
"""
/// An unexpected error, eg. invalid JSON returned by the service or an unknown error code
Unhandled(Box<dyn #T>),
Unhandled(Box<dyn #T + Send + Sync + 'static>),
""",
RuntimeType.StdError
)
@ -125,7 +125,7 @@ class CombinedErrorGenerator(
Self { kind, meta }
}
pub fn unhandled(err: impl Into<Box<dyn #{std_error}>>) -> Self {
pub fn unhandled(err: impl Into<Box<dyn #{std_error} + Send + Sync + 'static>>) -> Self {
Self {
kind: ${symbol.name}Kind::Unhandled(err.into()),
meta: Default::default()

View File

@ -3,4 +3,3 @@
- [Http Operations](./operation.md)
- [Endpoint Resolution](./endpoint.md)
- [HTTP middleware](./middleware.md)