elevenlabs_api/apis/
tts.rs

1// Converts text into speech using a voice of your choice and returns audio.
2// See: https://docs.elevenlabs.io/api-reference/text-to-speech
3
4//! TTS API
5
6use crate::{ApiResult, Elevenlabs};
7use serde::{Deserialize, Serialize};
8
9use super::{requests::Requests, TEXT_TO_SPEECH};
10
11#[derive(Debug, Serialize, Deserialize)]
12pub struct TtsBody {
13    /// Identifier of the model that will be used.
14    /// Defaults to "eleven_monolingual_v1"
15    #[serde(skip_serializing_if = "Option::is_none")]
16    pub model_id: Option<String>,
17    /// The text that will get converted into speech.
18    pub text: String,
19    /// Voice settings overriding stored setttings for the given voice.
20    /// They are applied only on the given TTS request.
21    /// Defaults to None
22    #[serde(skip_serializing_if = "Option::is_none")]
23    pub voice_settings: Option<VoiceSettings>,
24}
25
26#[derive(Debug, Serialize, Deserialize)]
27pub struct VoiceSettings {
28    /// Similarity Boost
29    pub stability: f32,
30    /// Stability
31    pub similarity_boost: f32,
32}
33
34pub trait TtsApi {
35    /// Creates a bytes array containing the audio of the text.
36    fn tts(&self, voice_settings: &TtsBody, voice_id: impl AsRef<str>) -> ApiResult<Vec<u8>>;
37}
38
39impl TtsApi for Elevenlabs {
40    fn tts(&self, tts_body: &TtsBody, voice_id: impl AsRef<str>) -> ApiResult<Vec<u8>> {
41        let request_body = serde_json::to_value(tts_body).unwrap();
42        self.post(
43            format!("{}/{}", TEXT_TO_SPEECH, voice_id.as_ref()).as_str(),
44            request_body,
45        )
46    }
47}
48
49#[cfg(test)]
50mod tests {
51    use crate::tts::*;
52    use crate::*;
53
54    #[test]
55    fn test_tts() {
56        let auth = Auth::from_env().unwrap();
57        let elevenlabs = Elevenlabs::new(auth, "https://api.elevenlabs.io/v1/");
58
59        let tts_body = TtsBody {
60            model_id: None,
61            text: "Hello world".to_string(),
62            voice_settings: None,
63        };
64
65        let tts_result = elevenlabs.tts(&tts_body, "yoZ06aMxZJJ28mfd3POQ");
66        let bytes = tts_result.unwrap();
67        assert!(bytes.len() > 0);
68    }
69}