Rust
Rust
IBM Cloud - Text to Speech - Synthesize Audio (GET)
See more IBM Text to Speech Examples
Synthesizes text to audio that is spoken in the specified voice.Chilkat Rust Downloads
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let http = chilkat::Http::new();
http.set_login("apikey");
http.set_password("my_apikey");
http.set_basic_auth(true);
// Use your base URL shown in the credentials web page (below your apikey)
let my_base_url = "https://api.us-south.text-to-speech.watson.cloud.ibm.com/instances/31941e96-7b89-4d56-8993-9cd8f18ec2d8".to_string();
let sb_url = chilkat::StringBuilder::new();
let _ = sb_url.append(&my_base_url);
let _ = sb_url.append("/v1/synthesize?accept=ACCEPT_TYPE&voice=CHOSEN_VOICE&text=TEXT_TO_SYNTHESIZE");
// Choose "audio/wav" for the output file type.
let mut num_replaced = sb_url.replace("ACCEPT_TYPE", &http.url_encode("audio/wav").unwrap_or_default());
// See the choices for voices at https://cloud.ibm.com/apidocs/text-to-speech#synthesize-audio-get
num_replaced = sb_url.replace("CHOSEN_VOICE", "en-US_MichaelVoice");
let text_to_synthesize = "When life gives you lemons, order the lobster tail.".to_string();
num_replaced = sb_url.replace("TEXT_TO_SYNTHESIZE", &http.url_encode(&text_to_synthesize).unwrap_or_default());
// Send the GET to synthesize the voice.
// The response file will be contained in bdResponseBody
let bd_response_body = chilkat::BinData::new();
if http.quick_get_bd(&sb_url.get_as_string().unwrap_or_default(), &bd_response_body).is_err() {
println!("{}", http.last_error_text());
return;
}
let resp_status_code = http.last_status();
println!("response status code = {}", resp_status_code);
if resp_status_code == 200 {
let _ = bd_response_body.write_file("qa_output/lobster.wav").is_ok();
println!("Success!");
} else {
println!("{}", bd_response_body.get_string("utf-8").unwrap_or_default());
}