async_dashscope/operation/audio/
mod.rs

1use crate::{
2    Client,
3    error::DashScopeError,
4    operation::audio::{tts::output::TextToSpeechOutputStream},
5};
6#[cfg(feature = "websocket")]
7use crate::operation::audio::asr::customization::Customization;
8use crate::{error::Result, operation::audio::tts::output::TextToSpeechOutput};
9pub use tts::param::{
10    Input as TextToSpeechInput, InputBuilder as TextToSpeechInputBuilder, TextToSpeechParam,
11    TextToSpeechParamBuilder,
12};
13#[cfg(feature = "websocket")]
14pub mod asr;
15pub mod tts;
16#[cfg(feature = "websocket")]
17pub mod ws;
18
19const AUDIO_PATH: &str = "/services/aigc/multimodal-generation/generation";
20
21pub struct Audio<'a> {
22    client: &'a Client,
23}
24
25impl<'a> Audio<'a> {
26    pub fn new(client: &'a Client) -> Self {
27        Self { client }
28    }
29
30    /// 执行文本转语音(TTS)转换
31    ///
32    /// 此异步方法向指定端点发送 POST 请求,将文本转换为语音输出
33    ///
34    /// # 参数
35    /// * `request` - TTS 转换参数配置,包含文本内容、语音模型等设置
36    pub async fn tts(&self, request: TextToSpeechParam) -> Result<TextToSpeechOutput> {
37        // 检查请求是否明确设置为非流式,如果是,则返回错误。
38        if request.stream == Some(true) {
39            return Err(DashScopeError::InvalidArgument(
40                "When stream is true, use Audio::call_stream".into(),
41            ));
42        }
43        self.client.post(AUDIO_PATH, request).await
44    }
45
46    pub async fn tts_stream(&self, request: TextToSpeechParam) -> Result<TextToSpeechOutputStream> {
47        // 检查请求是否明确设置为非流式,如果是,则返回错误。
48        if request.stream == Some(false) {
49            return Err(DashScopeError::InvalidArgument(
50                "When stream is false, use Audio::call".into(),
51            ));
52        }
53        self.client.post_stream(AUDIO_PATH, request).await
54    }
55
56    #[cfg(feature = "websocket")]
57    pub async fn asr_ws(&self) -> Result<ws::WebsocketInference> {
58        use crate::operation::ws_client::WsClient;
59
60        let ws = WsClient::into_ws_client(self.client.clone()).await?;
61        Ok(ws::WebsocketInference::new(ws))
62    }
63
64    #[cfg(feature = "websocket")]
65    pub async fn tts_ws(&self) -> Result<ws::WebsocketInference> {
66        use crate::operation::ws_client::WsClient;
67
68        let ws = WsClient::into_ws_client(self.client.clone()).await?;
69        Ok(ws::WebsocketInference::new(ws))
70    }
71
72    #[cfg(feature = "websocket")]
73    pub async fn asr_customization(&self) -> Result<Customization> {
74        Ok(Customization::new(self.client.clone()))
75    }
76}