forked from graniet/llm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtts_rodio_example.rs
More file actions
49 lines (37 loc) · 1.45 KB
/
tts_rodio_example.rs
File metadata and controls
49 lines (37 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//! Example demonstrating text-to-speech synthesis using OpenAI
//!
//! This example shows how to:
//! 1. Initialize the OpenAI text-to-speech provider
//! 2. Generate speech from text
//! 3. Save the audio output to a file
use std::{fs::File, io::BufReader};
use llm::builder::{LLMBackend, LLMBuilder};
use rodio::{Decoder, OutputStream, Sink};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Get API key from environment variable or use test key
let api_key = std::env::var("OPENAI_API_KEY").unwrap_or("test_key".into());
// Initialize OpenAI text-to-speech provider
let tts = LLMBuilder::new()
.backend(LLMBackend::OpenAI)
.api_key(api_key)
.model("tts-1")
.voice("ash")
.build()?;
// Text to convert to speech
let text = "Hello! This is an example of text-to-speech synthesis using OpenAI with LLM crates and rodio in Rust.";
// Generate speech
let audio_data = tts.speech(text).await?;
// Save the audio to a file
std::fs::write("output-speech.mp3", audio_data)?;
// Play the audio
let (_stream, stream_handle) = OutputStream::try_default().unwrap();
let sink = Sink::try_new(&stream_handle).unwrap();
let file = File::open("output-speech.mp3").unwrap();
let source = Decoder::new(BufReader::new(file)).unwrap();
// Play audio
sink.append(source);
// Block until the sound has finished
sink.sleep_until_end();
Ok(())
}