rust-tts-wrapper 1.4.0

Cross-platform TTS wrapper with C API — mirrors js-tts-wrapper / SwiftTTSWrapper
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
//! Azure AI Speech Personal Voices.
//!
//! Three-step REST under the Speech resource (`Ocp-Apim-Subscription-Key`):
//!
//! 1. **Consent** — `PUT {endpoint}/customvoice/consents/{id}` (multipart:
//!    projectId, voiceTalentName, companyName, locale, audiodata). The
//!    verbal statement must be Azure's fixed per-locale script, and the
//!    spoken names must match the fields — the service verifies the
//!    speaker.
//! 2. **Personal voice** — `POST {endpoint}/customvoice/personalvoices/{id}`
//!    (multipart: projectId, consentId, audiodata WAV 5–90 s). Returns
//!    `Operation-Location` for a long-running operation.
//! 3. **Poll** the operation until `Succeeded`; the created resource
//!    carries the `speakerProfileId` used for synthesis.
//!
//! Synthesis needs SSML `<mstts:ttsembedding speakerProfileId=…>` on a
//! base model voice (`DragonLatestNeural`…) — the azure engine accepts a
//! `"{base_model}/{speaker_profile_id}"` voice string for that.
//!
//! **Intake-gated**: Personal Voice API access requires Microsoft's
//! registration form (aka.ms/customneural). Code works; un-gated
//! subscriptions fail at consent creation. Request shapes verified
//! against the official REST docs 2026-09-26 (not exercised live — no
//! gated subscription available).

use super::{
    select_clips, wav_bytes, CloneHandle, CloneOutcome, CloningMode, ConsentRecording, ConsentSpec,
    VoiceCloning, VoiceIdentity,
};
use crate::types::{TtsError, TtsResult};
use std::collections::HashMap;

/// Azure prompt audio: 5–90 s.
const AZURE_TARGET_SECS: u32 = 60;

/// en-US consent script (verbatim — Azure verifies the statement).
pub(crate) const AZURE_CONSENT_SCRIPT_EN_US: &str = "I [state your first and last name] am aware \
     that recordings of my voice will be used by [state the name of the company] to create and \
     use a synthetic version of my voice.";

pub(crate) struct AzureCloner {
    api_key: String,
    region: String,
    credentials: HashMap<String, String>,
    client: reqwest::blocking::Client,
}

impl AzureCloner {
    pub(crate) fn new(credentials: &HashMap<String, String>) -> Self {
        Self {
            api_key: credentials
                .get("apiKey")
                .or_else(|| credentials.get("subscriptionKey"))
                .cloned()
                .unwrap_or_default(),
            region: credentials
                .get("region")
                .cloned()
                .unwrap_or_else(|| "eastus".into()),
            credentials: credentials.clone(),
            client: reqwest::blocking::Client::new(),
        }
    }

    fn base(&self) -> String {
        format!("https://{}.api.cognitive.microsoft.com", self.region)
    }

    fn api_version(&self) -> String {
        self.credentials
            .get("apiVersion")
            .cloned()
            .unwrap_or_else(|| "2026-01-01".into())
    }

    fn consent_for(identity: &VoiceIdentity) -> Option<&ConsentRecording> {
        identity.consent.iter().find(|c| c.engine == "azure")
    }

    fn auth(&self) -> (&'static str, String) {
        ("Ocp-Apim-Subscription-Key", self.api_key.clone())
    }

    /// PUT the consent resource (multipart per Consents_Post).
    fn put_consent(
        &self,
        consent: &ConsentRecording,
        resource_id: &str,
        project_id: &str,
    ) -> TtsResult<()> {
        let talent = consent
            .metadata
            .get("voiceTalentName")
            .ok_or_else(|| TtsError("azure cloning: consent needs voiceTalentName".into()))?;
        let company = consent
            .metadata
            .get("companyName")
            .ok_or_else(|| TtsError("azure cloning: consent needs companyName".into()))?;
        let locale = consent
            .metadata
            .get("locale")
            .ok_or_else(|| TtsError("azure cloning: consent needs locale".into()))?;
        let form = reqwest::blocking::multipart::Form::new()
            .text("projectId", project_id.to_string())
            .text("voiceTalentName", talent.clone())
            .text("companyName", company.clone())
            .text("locale", locale.clone())
            .part(
                "audiodata",
                reqwest::blocking::multipart::Part::bytes(wav_bytes(
                    &consent.pcm,
                    consent.sample_rate,
                ))
                .file_name("consent.wav")
                .mime_str("audio/wav")
                .map_err(|e| TtsError(format!("mime: {e}")))?,
            );
        let (header, value) = self.auth();
        let url = format!(
            "{}/customvoice/consents/{}?api-version={}",
            self.base(),
            resource_id,
            self.api_version()
        );
        let resp = self
            .client
            .put(&url)
            .header(header, value)
            .multipart(form)
            .send()
            .map_err(|e| TtsError(format!("azure consent: {e}")))?;
        let status = resp.status();
        // 201 created / 200 existing-and-identical are both fine.
        if !status.is_success() {
            let body = resp.text().unwrap_or_default();
            return Err(TtsError(format!("azure consent {status}: {body}")));
        }
        Ok(())
    }

    /// POST the personal voice (multipart per PersonalVoices_Post);
    /// returns the Operation-Location URL.
    fn post_personal_voice(
        &self,
        resource_id: &str,
        project_id: &str,
        consent_id: &str,
        prompt_pcm: &[u8],
        prompt_rate: u32,
    ) -> TtsResult<String> {
        let rate = prompt_rate;
        // 5–90 s window (lower bound validated by the caller).
        #[allow(clippy::cast_possible_truncation)]
        let max_pcm = (rate as usize).saturating_mul(2 * 90);
        let pcm = if prompt_pcm.len() > max_pcm {
            prompt_pcm[..max_pcm].to_vec()
        } else {
            prompt_pcm.to_vec()
        };
        let form = reqwest::blocking::multipart::Form::new()
            .text("projectId", project_id.to_string())
            .text("consentId", consent_id.to_string())
            .part(
                "audiodata",
                reqwest::blocking::multipart::Part::bytes(wav_bytes(&pcm, rate))
                    .file_name("prompt.wav")
                    .mime_str("audio/wav")
                    .map_err(|e| TtsError(format!("mime: {e}")))?,
            );
        let (header, value) = self.auth();
        let url = format!(
            "{}/customvoice/personalvoices/{}?api-version={}",
            self.base(),
            resource_id,
            self.api_version()
        );
        let resp = self
            .client
            .post(&url)
            .header(header, value)
            .multipart(form)
            .send()
            .map_err(|e| TtsError(format!("azure personal voice: {e}")))?;
        let status = resp.status();
        if !status.is_success() {
            let body = resp.text().unwrap_or_default();
            return Err(TtsError(format!("azure personal voice {status}: {body}")));
        }
        let op = resp
            .headers()
            .get("Operation-Location")
            .and_then(|v| v.to_str().ok())
            .map(str::to_string)
            .ok_or_else(|| TtsError("azure personal voice: no Operation-Location header".into()))?;
        Ok(op)
    }

    /// Poll the LRO once; Ok(Some(handle)) on success, Ok(None) while
    /// running, Err on failure.
    fn poll_once(&self, operation_url: &str) -> TtsResult<Option<CloneHandle>> {
        let (header, value) = self.auth();
        let resp = self
            .client
            .get(operation_url)
            .header(header, value)
            .send()
            .map_err(|e| TtsError(format!("azure poll: {e}")))?;
        let status = resp.status();
        let json: serde_json::Value = resp
            .json()
            .map_err(|e| TtsError(format!("azure poll parse ({status}): {e}")))?;
        let op_status = json
            .get("status")
            .and_then(|v| v.as_str())
            .unwrap_or_default();
        match op_status {
            "Succeeded" => {
                let profile = json
                    .pointer("/properties/result/speakerProfileId")
                    .or_else(|| json.pointer("/result/speakerProfileId"))
                    .and_then(|v| v.as_str())
                    .ok_or_else(|| {
                        TtsError(format!(
                            "azure poll: succeeded but no speakerProfileId: {json}"
                        ))
                    })?;
                Ok(Some(CloneHandle {
                    engine: "azure".into(),
                    // Synthesis voice string: "{base_model}/{profile}" —
                    // the azure engine splits this for ttsembedding.
                    voice_id: format!("DragonLatestNeural/{profile}"),
                    model: Some("DragonLatestNeural".into()),
                }))
            }
            "Failed" => {
                let err = json
                    .pointer("/error/message")
                    .and_then(|v| v.as_str())
                    .unwrap_or("unknown error");
                Err(TtsError(format!("azure personal voice failed: {err}")))
            }
            _ => Ok(None),
        }
    }
}

impl VoiceCloning for AzureCloner {
    fn engine_id(&self) -> &'static str {
        "azure"
    }

    fn cloning_mode(&self) -> CloningMode {
        CloningMode::Job
    }

    fn consent_spec(&self) -> Option<ConsentSpec> {
        Some(ConsentSpec {
            script: AZURE_CONSENT_SCRIPT_EN_US.into(),
            locale: "en-US".into(),
            metadata_keys: vec!["voiceTalentName", "companyName", "locale"],
        })
    }

    fn clone_voice(&self, identity: &VoiceIdentity) -> TtsResult<CloneOutcome> {
        let project_id = self
            .credentials
            .get("projectId")
            .filter(|p| !p.is_empty())
            .ok_or_else(|| {
                TtsError(
                    "azure cloning: projectId credential required (a Custom Voice \
                     project from Speech Studio)"
                        .into(),
                )
            })?;
        let Some(consent) = Self::consent_for(identity) else {
            return Err(TtsError(format!(
                "azure cloning: consent recording required — the user must read \
                 the {AZURE_CONSENT_SCRIPT_EN_US:?} script aloud, with metadata \
                 voiceTalentName/companyName/locale"
            )));
        };
        // Validate everything BEFORE any network call: a failure after
        // put_consent would orphan a server-side consent resource.
        let talent = consent
            .metadata
            .get("voiceTalentName")
            .ok_or_else(|| TtsError("azure cloning: consent needs voiceTalentName".into()))?;
        let company = consent
            .metadata
            .get("companyName")
            .ok_or_else(|| TtsError("azure cloning: consent needs companyName".into()))?;
        let locale = consent
            .metadata
            .get("locale")
            .ok_or_else(|| TtsError("azure cloning: consent needs locale".into()))?;
        // Only the en-US script is pinned; a different locale would be
        // verified against that locale's script we cannot supply.
        if locale != "en-US" {
            return Err(TtsError(format!(
                "azure cloning: only the en-US consent script is pinned \
                 (requested {locale})"
            )));
        }
        if identity.clips.is_empty() {
            return Err(TtsError("azure cloning: identity has no clips".into()));
        }
        let picked = select_clips(&identity.clips, AZURE_TARGET_SECS);
        let (prompt_pcm, prompt_rate) = super::concat_clips(&picked, 300)?;
        if (prompt_pcm.len() as u64) < u64::from(prompt_rate) * 2 * 5 {
            return Err(TtsError("azure cloning: prompt audio must be ≥5 s".into()));
        }
        let _ = (talent, company);
        let suffix = uuid::Uuid::new_v4().simple().to_string();
        let consent_id = format!("rust-tts-consent-{suffix}");
        let voice_id = format!("rust-tts-pv-{suffix}");
        self.put_consent(consent, &consent_id, project_id)?;
        let operation =
            self.post_personal_voice(&voice_id, project_id, &consent_id, &prompt_pcm, prompt_rate)?;
        // First poll immediately — training is documented as <5 s.
        if let Some(handle) = self.poll_once(&operation)? {
            return Ok(CloneOutcome::Ready(handle));
        }
        Ok(CloneOutcome::Pending {
            engine: "azure".into(),
            job_id: operation,
        })
    }

    fn poll_clone(&self, job_id: &str) -> TtsResult<CloneOutcome> {
        match self.poll_once(job_id)? {
            Some(handle) => Ok(CloneOutcome::Ready(handle)),
            None => Ok(CloneOutcome::Pending {
                engine: "azure".into(),
                job_id: job_id.to_string(),
            }),
        }
    }

    fn list_cloned(&self) -> TtsResult<Vec<CloneHandle>> {
        let (header, value) = self.auth();
        let url = format!(
            "{}/customvoice/personalvoices?api-version={}",
            self.base(),
            self.api_version()
        );
        let resp = self
            .client
            .get(&url)
            .header(header, value)
            .send()
            .map_err(|e| TtsError(format!("azure list: {e}")))?;
        let status = resp.status();
        if !status.is_success() {
            // Gated subscriptions land here (401 from customvoice even
            // with a key that works for plain synthesis) — surface it,
            // never fold it into an empty list.
            let body = resp.text().unwrap_or_default();
            return Err(TtsError(format!("azure list {status}: {body}")));
        }
        let json: serde_json::Value = resp
            .json()
            .map_err(|e| TtsError(format!("azure list parse ({status}): {e}")))?;
        let Some(values) = json.get("value").and_then(|v| v.as_array()) else {
            return Ok(Vec::new());
        };
        Ok(values
            .iter()
            .filter_map(|v| {
                let profile = v.get("speakerProfileId").and_then(|x| x.as_str())?;
                Some(CloneHandle {
                    engine: "azure".into(),
                    voice_id: format!("DragonLatestNeural/{profile}"),
                    model: Some("DragonLatestNeural".into()),
                })
            })
            .collect())
    }

    fn delete_cloned(&self, handle: &CloneHandle) -> TtsResult<()> {
        // The personal voice resource id is needed for DELETE; handles
        // carry speakerProfileId instead. Resolve via list.
        let (header, value) = self.auth();
        let list_url = format!(
            "{}/customvoice/personalvoices?api-version={}",
            self.base(),
            self.api_version()
        );
        let resp = self
            .client
            .get(&list_url)
            .header(header, value.clone())
            .send()
            .map_err(|e| TtsError(format!("azure delete lookup: {e}")))?;
        let status = resp.status();
        if !status.is_success() {
            let body = resp.text().unwrap_or_default();
            return Err(TtsError(format!("azure delete lookup {status}: {body}")));
        }
        let json: serde_json::Value = resp
            .json()
            .map_err(|e| TtsError(format!("azure delete lookup parse: {e}")))?;
        let wanted = handle
            .voice_id
            .rsplit('/')
            .next()
            .unwrap_or(&handle.voice_id);
        let Some(values) = json.get("value").and_then(|v| v.as_array()) else {
            return Err(TtsError("azure delete: voice not found".into()));
        };
        let Some(entry) = values
            .iter()
            .find(|v| v.get("speakerProfileId").and_then(|x| x.as_str()) == Some(wanted))
        else {
            return Err(TtsError("azure delete: voice not found".into()));
        };
        let Some(id) = entry.get("id").and_then(|x| x.as_str()) else {
            return Err(TtsError("azure delete: resource has no id".into()));
        };
        let url = format!(
            "{}/customvoice/personalvoices/{}?api-version={}",
            self.base(),
            id,
            self.api_version()
        );
        let resp = self
            .client
            .delete(&url)
            .header(header, value)
            .send()
            .map_err(|e| TtsError(format!("azure delete: {e}")))?;
        let status = resp.status();
        if !status.is_success() {
            let body = resp.text().unwrap_or_default();
            return Err(TtsError(format!("azure delete {status}: {body}")));
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn consent_script_is_pinned() {
        assert!(AZURE_CONSENT_SCRIPT_EN_US.contains("synthetic version of my voice"));
    }

    #[test]
    fn consent_gate_and_project_required() {
        let cloner = AzureCloner::new(&HashMap::new());
        let identity = VoiceIdentity::default();
        let err = cloner.clone_voice(&identity).unwrap_err().to_string();
        assert!(err.contains("projectId"), "{err}");

        let mut creds = HashMap::new();
        creds.insert("projectId".to_string(), "p1".to_string());
        let cloner = AzureCloner::new(&creds);
        let err = cloner.clone_voice(&identity).unwrap_err().to_string();
        assert!(err.contains("consent recording required"), "{err}");
    }

    #[test]
    fn job_mode_with_consent_spec() {
        let cloner = AzureCloner::new(&HashMap::new());
        assert_eq!(cloner.cloning_mode(), CloningMode::Job);
        let spec = cloner.consent_spec().expect("gated");
        assert!(spec.metadata_keys.contains(&"voiceTalentName"));
    }
}