async_dashscope/operation/
audio.rs

1use crate::{error::DashScopeError, operation::audio::tts::output::TextToSpeechOutputStream, Client};
2use crate::{error::Result, operation::audio::tts::output::TextToSpeechOutput};
3pub use tts::param::{
4    Input as TextToSpeechInput, InputBuilder as TextToSpeechInputBuilder, TextToSpeechParam,
5    TextToSpeechParamBuilder,
6};
7mod tts;
8
9const AUDIO_PATH: &str = "/services/aigc/multimodal-generation/generation";
10
11pub struct Audio<'a> {
12    client: &'a Client,
13}
14
15impl<'a> Audio<'a> {
16    pub fn new(client: &'a Client) -> Self {
17        Self { client }
18    }
19
20    /// 执行文本转语音(TTS)转换
21    ///
22    /// 此异步方法向指定端点发送 POST 请求,将文本转换为语音输出
23    ///
24    /// # 参数
25    /// * `request` - TTS 转换参数配置,包含文本内容、语音模型等设置
26    pub async fn tts(&self, request: TextToSpeechParam) -> Result<TextToSpeechOutput> {
27        // 检查请求是否明确设置为非流式,如果是,则返回错误。
28        if request.stream == Some(true) {
29            return Err(DashScopeError::InvalidArgument(
30                "When stream is true, use Audio::call_stream".into(),
31            ));
32        }
33        self.client.post(AUDIO_PATH, request).await
34    }
35
36    pub async fn tts_stream(&self, request: TextToSpeechParam) -> Result<TextToSpeechOutputStream> {
37        // 检查请求是否明确设置为非流式,如果是,则返回错误。
38        if request.stream == Some(false) {
39            return Err(DashScopeError::InvalidArgument(
40                "When stream is false, use Audio::call".into(),
41            ));
42        }
43        self.client.post_stream(AUDIO_PATH, request).await
44    }
45}