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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
//!
//! Selection of AI Voice Names:
//!
//! (This is not a comprehensive list. You may find all available endpoints fetched from WestUS region in [`VoiceName`][crate::azure::VoiceName])
//!
//! - [`VoiceName::yue_CN_YunSongNeural`][crate::azure::VoiceName::yue_CN_YunSongNeural]: A 25-year-old young man's voice (deeper than Yunfeng).
//! - [`VoiceName::yue_CN_XiaoMinNeural`][crate::azure::VoiceName::yue_CN_XiaoMinNeural]: Sounds like a 20-year-old young lady's voice.
//! - [`VoiceName::zh_CN_YunfengNeural`][crate::azure::VoiceName::zh_CN_YunfengNeural]: A 22-25 years' young man's voice.
//! - [`VoiceName::zh_CN_YunhaoNeural`][crate::azure::VoiceName::zh_CN_YunhaoNeural]: A 25-year-old young man's voice (deeper than Yunfeng).
//! - [`VoiceName::zh_CN_YunjianNeural`][crate::azure::VoiceName::zh_CN_YunjianNeural]: A 30 to 35-year-old man's voice (deeper and powerful).
//! - [`VoiceName::zh_CN_YunxiNeural`][crate::azure::VoiceName::zh_CN_YunxiNeural]: A 14 to 17-year-old boy student's voice.
//! - [`VoiceName::zh_CN_YunxiaNeural`][crate::azure::VoiceName::zh_CN_YunxiaNeural]: A 10-year-old boy's voice.
//! - [`VoiceName::zh_CN_YunyangNeural`][crate::azure::VoiceName::zh_CN_YunyangNeural]: A 35-year-old man's voice (announcer like).
//! - [`VoiceName::zh_CN_YunyeNeural`][crate::azure::VoiceName::zh_CN_YunyeNeural]: A 35 to 40-year-old man's voice (documentary voice actor like).
//! - [`VoiceName::zh_CN_YunzeNeural`][crate::azure::VoiceName::zh_CN_YunzeNeural]: A 45 to 50-year-old man's voice (kind and deep voice).
//! - [`VoiceName::zh_CN_XiaochenNeural`][crate::azure::VoiceName::zh_CN_XiaochenNeural]: Sounds like a 19-year-old college girl.
//! - [`VoiceName::zh_CN_XiaohanNeural`][crate::azure::VoiceName::zh_CN_XiaohanNeural]: Sounds like a 27-year-old young woman.
//! - [`VoiceName::zh_CN_XiaomengNeural`][crate::azure::VoiceName::zh_CN_XiaomengNeural]: Sounds like a 23-year-old young lady.
//! - [`VoiceName::zh_CN_XiaomoNeural`][crate::azure::VoiceName::zh_CN_XiaomoNeural]: Sounds like a 25-year-old young lady with intellectual voice.
//! - [`VoiceName::zh_CN_XiaoqiuNeural`][crate::azure::VoiceName::zh_CN_XiaoqiuNeural]: Sounds like a 35 to 40-year-old female's voice (announcer like).
//! - [`VoiceName::zh_CN_XiaoruiNeural`][crate::azure::VoiceName::zh_CN_XiaoruiNeural]: Sounds like a 50-year-old female's voice (kind and slow).
//! - [`VoiceName::zh_CN_XiaoshuangNeural`][crate::azure::VoiceName::zh_CN_XiaoshuangNeural]: Sounds like a 10-year-old little girl.
//! - [`VoiceName::zh_CN_XiaoxiaoNeural`][crate::azure::VoiceName::zh_CN_XiaoxiaoNeural]: Sounds like a 20-year-old young lady with sweet voice.
//! - [`VoiceName::zh_CN_XiaoxuanNeural`][crate::azure::VoiceName::zh_CN_XiaoxuanNeural]: Sounds like a 27-year-old young woman with cold voice.
//! - [`VoiceName::zh_CN_XiaoyanNeural`][crate::azure::VoiceName::zh_CN_XiaoyanNeural]: Sounds like a 30-year-old woman's voice (normal).
//! - [`VoiceName::zh_CN_XiaoyiNeural`][crate::azure::VoiceName::zh_CN_XiaoyiNeural]: Sounds like a 19-year-old college girl's voice.
//! - [`VoiceName::zh_CN_XiaoyouNeural`][crate::azure::VoiceName::zh_CN_XiaoyouNeural]: Sounds like a 7-year-old little girl's voice (cute and sweet).
//! - [`VoiceName::zh_CN_XiaozhenNeural`][crate::azure::VoiceName::zh_CN_XiaozhenNeural]: Sounds like a 22-year-old young lady's voice (full of careness).
//!
//! For use of styles and roles, see [docs/azure-voices-n-roles.md](https://github.com/dongsxyz/rust-ai/blob/master/docs/azure-voices-n-roles.md).

use std::collections::HashMap;

use log::{error, warn};
use reqwest::header::HeaderMap;

use crate::azure::{
    endpoint::{
        request_get_endpoint, request_post_endpoint, request_post_endpoint_ssml,
        SpeechServiceEndpoint,
    },
    types::{
        common::{MicrosoftOutputFormat, ResponseExpectation, ResponseType},
        speech::{
            entity::EntityReference,
            file::{File, FileKind, FileType, PaginatedFiles},
            filter::FilterOperator,
            health::ServiceHealth,
            transcription::{
                Status, Transcription, TranscriptionProperties, TranscriptionReport,
                TranscriptionResult,
            },
            ErrorResponse,
        },
        tts::Voice,
        SSML,
    },
    Locale,
};

/// The Speech service allows you to convert text into synthesized speech and
/// get a list of supported voices for a region by using a REST API.
///
/// Source: <https://learn.microsoft.com/en-us/azure/cognitive-services/speech-service/rest-text-to-speech>
pub struct Speech {
    ssml: SSML,
    output_format: MicrosoftOutputFormat,
}

impl Default for Speech {
    fn default() -> Self {
        Self {
            ssml: SSML::default(),
            output_format: MicrosoftOutputFormat::Audio_24khz_48kbitrate_Mono_Mp3,
        }
    }
}

impl From<SSML> for Speech {
    /// Convert SSML document into Speech endpoint instance directly.
    fn from(value: SSML) -> Self {
        Self {
            ssml: value,
            output_format: MicrosoftOutputFormat::Audio_24khz_48kbitrate_Mono_Mp3,
        }
    }
}

impl Speech {
    pub fn new_transcription(display_name: String) -> Transcription {
        Transcription::default().display_name(display_name)
    }

    pub fn format(self, f: MicrosoftOutputFormat) -> Self {
        Self {
            output_format: f,
            ..self
        }
    }

    pub fn ssml(self, ssml: SSML) -> Self {
        Self { ssml, ..self }
    }

    /// Get a full list of voices for a specific region or endpoint. Prefix the
    /// voices list endpoint with a region to get a list of voices for that
    /// region. This is preconfigured in your `config.yml`.
    ///
    /// Voices and styles in preview are only available in three service
    /// regions: East US, West Europe, and Southeast Asia.
    ///
    /// Source: <https://learn.microsoft.com/en-us/azure/cognitive-services/speech-service/rest-text-to-speech>
    pub async fn voice_list() -> Result<Vec<Voice>, Box<dyn std::error::Error>> {
        let text =
            request_get_endpoint(&SpeechServiceEndpoint::Get_List_of_Voices, None, None).await?;
        match serde_json::from_str::<Vec<Voice>>(&text) {
            Ok(voices) => Ok(voices),
            Err(e) => {
                warn!(target: "azure", "Error parsing response: {:?}", e);
                Err("Unable to parse voice list, check log for details".into())
            }
        }
    }

    /// Health status provides insights about the overall health of the service
    /// and sub-components.
    ///
    /// V3.1 API supported only.
    pub async fn health_check() -> Result<ServiceHealth, Box<dyn std::error::Error>> {
        let text = request_get_endpoint(
            &SpeechServiceEndpoint::Get_Speech_to_Text_Health_Status_v3_1,
            None,
            None,
        )
        .await?;

        match serde_json::from_str::<ServiceHealth>(&text) {
            Ok(status) => Ok(status),
            Err(e) => {
                warn!(target: "azure", "Error parsing response: {:?}", e);
                Err("Unable to parse health status of speech cognitive services, check log for details".into())
            }
        }
    }

    /// The text-to-speech REST API supports neural text-to-speech voices, which
    /// support specific languages and dialects that are identified by locale. Each
    /// available endpoint is associated with a region. A Speech resource key for
    /// the endpoint or region that you plan to use is required.
    ///
    /// Source: <https://learn.microsoft.com/en-us/azure/cognitive-services/speech-service/rest-text-to-speech>
    pub async fn text_to_speech(self) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
        let mut headers = HeaderMap::new();
        headers.insert("X-Microsoft-OutputFormat", self.output_format.into());
        match request_post_endpoint_ssml(
            &SpeechServiceEndpoint::Post_Text_to_Speech_v1,
            self.ssml,
            ResponseExpectation::Bytes,
            Some(headers),
        )
        .await
        {
            Ok(ResponseType::Bytes(bytes)) => Ok(bytes),
            Err(e) => Err(e),
            _ => Err("Incorrect response type".into()),
        }
    }

    /// Same as `text_to_speech`.
    pub async fn tts(self) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
        Ok(self.text_to_speech().await?)
    }
}

impl Transcription {
    pub fn skip(self, skip: usize) -> Self {
        Self {
            skip: Some(skip),
            ..self
        }
    }
    pub fn top(self, top: usize) -> Self {
        Self {
            top: Some(top),
            ..self
        }
    }
    pub fn filter(self, filter: FilterOperator) -> Self {
        Self {
            filter: Some(filter),
            ..self
        }
    }

    pub fn sas_validity_in_seconds(self, sec: u32) -> Self {
        Self {
            sas_validity_in_seconds: Some(sec),
            ..self
        }
    }

    pub fn model(self, model: String) -> Self {
        Self {
            model: Some(EntityReference::from(model)),
            ..self
        }
    }

    pub fn content_container_url(self, url: String) -> Self {
        Self {
            content_container_url: Some(url),
            ..self
        }
    }
    pub fn content_urls(self, urls: Vec<String>) -> Self {
        Self {
            content_urls: Some(urls),
            ..self
        }
    }

    pub fn project(self, project: String) -> Self {
        Self {
            project: Some(EntityReference::from(project)),
            ..self
        }
    }

    pub fn set_self(self, _self: String) -> Self {
        Self {
            _self: Some(_self),
            ..self
        }
    }

    /// Change display name of current transcription job.
    ///
    /// No effect after transcription submitted.
    pub fn display_name(self, display_name: String) -> Self {
        Self {
            display_name,
            ..self
        }
    }

    /// Change default locale of this transcription job.
    ///
    /// No effect after transcription submitted.
    pub fn locale(self, locale: Locale) -> Self {
        Self { locale, ..self }
    }

    /// Pass in a closure to set the inner properties.
    pub fn properties<F>(self, mut f: F) -> Self
    where
        F: FnMut(Option<TranscriptionProperties>) -> TranscriptionProperties,
    {
        Self {
            properties: Some(f(self.properties)),
            ..self
        }
    }

    /// [Custom Speech]
    /// Gets the list of custom models for the authenticated subscription.
    ///
    /// TODO: implement this.
    pub async fn models(self) -> Result<(), Box<dyn std::error::Error>> {
        todo!("Test with custom models");
        // let mut params = HashMap::<String, String>::new();

        // if let Some(skip) = self.skip {
        //     params.insert("skip".into(), skip.to_string());
        // }
        // if let Some(top) = self.top {
        //     params.insert("top".into(), top.to_string());
        // }
        // if let Some(filter) = self.filter {
        //     params.insert("filter".into(), filter.to_string());
        // }

        // let text = request_get_endpoint(
        //     &SpeechServiceEndpoint::Get_List_of_Models_v3_1,
        //     Some(params),
        // )
        // .await?;

        // println!("{}", text);

        // match serde_json::from_str::<ServiceHealthResponse>(&text) {
        //     Ok(status) => Ok(status),
        //     Err(e) => {
        //         warn!(target: "azure", "Error parsing response: {:?}", e);
        //         Err("Unable to parse health status of speech cognitive services, check log for details".into())
        //     }
        // }

        // Ok(())
    }

    /// Create a new audio transcription job.
    pub async fn create(&self) -> Result<Transcription, Box<dyn std::error::Error>> {
        return if let ResponseType::Text(text) = request_post_endpoint(
            &SpeechServiceEndpoint::Post_Create_Transcription_v3_1,
            self.clone(),
            ResponseExpectation::Text,
            None,
        )
        .await?
        {
            return match serde_json::from_str::<Transcription>(&text) {
                Ok(trans) => Ok(trans),
                Err(e) => {
                    warn!(target: "azure", "Unable to parse transcription creation result: `{:#?}`", e);
                    match serde_json::from_str::<ErrorResponse>(&text) {
                        Ok(e) => {
                            error!(target: "azure", "Error from Azure: `{:?}`", e);
                            Err(Box::new(e))
                        }
                        Err(e) => {
                            error!(target: "azure", "Unable to parse error response: `{:?}`", e);
                            Err(Box::new(e))
                        }
                    }
                }
            };
        } else {
            Err("Unable to load output from Azure speech service endpoint".into())
        };
    }

    /// Check transcription status
    ///
    /// This will only succeed when you've submitted the initial batch create
    /// request to Azure endpoint.
    ///
    /// It requires a mutable Transcription instance because each [`status()`]
    /// call. So you can use while loops to check for status updates
    /// periodically.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// use std::time::Duration;
    /// use rust_ai::azure::{
    ///     types::speech::Status,
    ///     Speech,
    /// };
    /// 
    /// #[tokio::main]
    /// async fn main() -> tokio::io::Result<()> {
    ///   let mut trans = Speech::new_transcription("Test".into()).create().await.unwrap();
    ///   // Check transcription job status.
    ///   while [Status::Running, Status::NotStarted].contains(&trans.status().await.unwrap().status.clone().unwrap())
    ///   {
    ///       std::thread::sleep(Duration::from_secs(2));
    ///   }
    ///   Ok(())
    /// }
    /// ```
    pub async fn status(&mut self) -> Result<Transcription, Box<dyn std::error::Error>> {
        let text = request_get_endpoint(
            &SpeechServiceEndpoint::Get_Transcription_v3_1,
            None,
            Some(self.transcription_id().unwrap()),
        )
        .await?;

        return match serde_json::from_str::<Transcription>(&text) {
            Ok(trans) => {
                self.last_action_date_time = trans.last_action_date_time.clone();
                self.status = trans.status.clone();
                Ok(trans)
            }
            Err(e) => {
                warn!(target: "azure", "Unable to parse transcription status result: `{:#?}`", e);
                match serde_json::from_str::<ErrorResponse>(&text) {
                    Ok(e) => {
                        error!(target: "azure", "Error from Azure: `{:?}`", e);
                        Err(Box::new(e))
                    }
                    Err(e) => {
                        error!(target: "azure", "Unable to parse error response: `{:?}`", e);
                        Err(Box::new(e))
                    }
                }
            }
        };
    }

    /// Get batch transcription result from Azure endpoint
    pub async fn files(&self) -> Result<PaginatedFiles, Box<dyn std::error::Error>> {
        if let None = self.status.clone() {
            return Err("You should submit the create request first.".into());
        } else {
            match self.status.clone().unwrap() {
                Status::Succeeded => (),
                Status::Failed => {
                    return Err("The transcription failed, thus no results available.".into())
                }
                Status::NotStarted => return Err("Please wait for transcription to start.".into()),
                Status::Running => return Err("Please wait until results available.".into()),
            }
        }

        let mut params = HashMap::<String, String>::new();
        if let Some(sas) = self.sas_validity_in_seconds.clone() {
            params.insert("sasValidityInSeconds".into(), sas.to_string());
        }
        if let Some(skip) = self.skip.clone() {
            params.insert("skip".into(), skip.to_string());
        }
        if let Some(top) = self.top.clone() {
            params.insert("top".into(), top.to_string());
        }
        if let Some(filter) = self.filter.clone() {
            params.insert("filter".into(), filter.to_string());
        }

        let text = request_get_endpoint(
            &SpeechServiceEndpoint::Get_Transcription_Files_v3_1,
            Some(params),
            Some(format!("{}/files", self.transcription_id().unwrap())),
        )
        .await?;

        return match serde_json::from_str::<PaginatedFiles>(&text) {
            Ok(files) => Ok(files),
            Err(e) => {
                warn!(target: "azure", "Unable to parse transcription files list result: `{:#?}`", e);
                match serde_json::from_str::<ErrorResponse>(&text) {
                    Ok(e) => {
                        error!(target: "azure", "Error from Azure: `{:?}`", e);
                        Err(Box::new(e))
                    }
                    Err(e) => {
                        error!(target: "azure", "Unable to parse error response: `{:?}`", e);
                        Err(Box::new(e))
                    }
                }
            }
        };
    }
}

impl PaginatedFiles {
    /// Load more results from Azure endpoint.
    ///
    /// TODO: Note, this method is not implemented yet!
    pub async fn more(self) -> Result<Option<PaginatedFiles>, Box<dyn std::error::Error>> {
        if let Some(_next_page_url) = self.next_link {
            todo!("Unimplemented method");
        } else {
            Ok(None)
        }
    }

    /// Only obtain the report file from Azure container.
    pub async fn report(&self) -> Result<Option<TranscriptionReport>, Box<dyn std::error::Error>> {
        if self.values.len() == 0 {
            return Ok(None);
        }

        let mut report = self
            .values
            .iter()
            .filter(|file| file.kind == FileKind::TranscriptionReport);

        if let Some(report) = report.next().clone() {
            let text = request_get_endpoint(
                &SpeechServiceEndpoint::None,
                None,
                Some(report.links.content_url.clone()),
            )
            .await?;

            return match serde_json::from_str::<TranscriptionReport>(&text) {
                Ok(report) => Ok(Some(report)),
                Err(e) => {
                    warn!(target: "azure", "Unable to parse transcription result file: `{:#?}`", e);
                    match serde_json::from_str::<ErrorResponse>(&text) {
                        Ok(e) => {
                            error!(target: "azure", "Error from Azure: `{:?}`", e);
                            Err(Box::new(e))
                        }
                        Err(e) => {
                            error!(target: "azure", "Unable to parse error response: `{:?}`", e);
                            Err(Box::new(e))
                        }
                    }
                }
            };
        } else {
            Ok(None)
        }
    }
}

impl File {
    /// Get file info from Azure transcription endpoint.
    pub async fn file(&self) -> Result<File, Box<dyn std::error::Error>> {
        let (trans_id, file_id) = self.file_id()?;

        let mut params = HashMap::<String, String>::new();
        if let Some(sas) = self.sas_validity_in_seconds.clone() {
            params.insert("sasValidityInSeconds".into(), sas.to_string());
        }

        let text = request_get_endpoint(
            &SpeechServiceEndpoint::Get_Transcription_File_v3_1,
            Some(params),
            Some(format!("{}/files/{}", trans_id, file_id)),
        )
        .await?;

        return match serde_json::from_str::<File>(&text) {
            Ok(file) => Ok(file),
            Err(e) => {
                warn!(target: "azure", "Unable to parse transcription result file: `{:#?}`", e);
                match serde_json::from_str::<ErrorResponse>(&text) {
                    Ok(e) => {
                        error!(target: "azure", "Error from Azure: `{:?}`", e);
                        Err(Box::new(e))
                    }
                    Err(e) => {
                        error!(target: "azure", "Unable to parse error response: `{:?}`", e);
                        Err(Box::new(e))
                    }
                }
            }
        };
    }

    /// Obtain transcription result from Azure container.
    ///
    /// The result file is wrapped inside [`FileType`] and thus have multiple
    /// variants.
    pub async fn details(&self) -> Result<FileType, Box<dyn std::error::Error>> {
        let text = request_get_endpoint(
            &SpeechServiceEndpoint::None,
            None,
            Some(self.links.content_url.clone()),
        )
        .await?;

        return match self.kind {
            FileKind::TranscriptionReport => {
                match serde_json::from_str::<TranscriptionReport>(&text) {
                    Ok(report) => Ok(FileType::TranscriptionReport(report)),
                    Err(e) => {
                        warn!(target: "azure", "Unable to parse transcription result file: `{:#?}`", e);
                        match serde_json::from_str::<ErrorResponse>(&text) {
                            Ok(e) => {
                                error!(target: "azure", "Error from Azure: `{:?}`", e);
                                Err(Box::new(e))
                            }
                            Err(e) => {
                                error!(target: "azure", "Unable to parse error response: `{:?}`", e);
                                Err(Box::new(e))
                            }
                        }
                    }
                }
            }
            FileKind::Transcription => match serde_json::from_str::<TranscriptionResult>(&text) {
                Ok(report) => Ok(FileType::Transcription(report)),
                Err(e) => {
                    warn!(target: "azure", "Unable to parse transcription result file: `{:#?}`", e);
                    match serde_json::from_str::<ErrorResponse>(&text) {
                        Ok(e) => {
                            error!(target: "azure", "Error from Azure: `{:?}`", e);
                            Err(Box::new(e))
                        }
                        Err(e) => {
                            error!(target: "azure", "Unable to parse error response: `{:?}`", e);
                            Err(Box::new(e))
                        }
                    }
                }
            },
            _ => {
                todo!(
                    "Not yet supported file type `{}`",
                    Into::<String>::into(self.kind.clone())
                );
            }
        };
    }
}