voice_vox_api 0.13.0

VoiceVox Engine openapi rust binding.
Documentation
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
//! definition of VoiceVox openapi schema section.
#![allow(dead_code)]

use std::convert::{TryFrom, TryInto};

use serde::ser::SerializeStruct;
use serde::{Deserialize, Serialize, Serializer};

/// this is Used in all around.
///
///
#[allow(non_snake_case)]
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq)]
pub struct AudioQuery {
    pub accent_phrases: Vec<AccentPhrase>,
    pub speedScale: f32,
    pub pitchScale: f32,
    pub intonationScale: f32,
    pub volumeScale: f32,
    pub prePhonemeLength: f32,
    pub postPhonemeLength: f32,
    pub outputSamplingRate: i32,
    pub outputStereo: bool,
    pub kana: Option<String>,
}

/// this is used in AudioItem.
///
///
#[allow(non_snake_case)]
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq)]
pub struct AudioQueryInProject {
    pub accentPhrases: Vec<AccentPhraseInProject>,
    pub speedScale: f32,
    pub pitchScale: f32,
    pub intonationScale: f32,
    pub volumeScale: f32,
    pub prePhonemeLength: f32,
    pub postPhonemeLength: f32,
    pub outputSamplingRate: i32,
    pub outputStereo: bool,
    pub kana: Option<String>,
}

impl From<AudioQuery> for AudioQueryInProject {
    fn from(aq: AudioQuery) -> Self {
        Self {
            accentPhrases: aq
                .accent_phrases
                .iter()
                .map(|ap| ap.clone().into())
                .collect(),
            speedScale: aq.speedScale,
            pitchScale: aq.pitchScale,
            intonationScale: aq.intonationScale,
            volumeScale: aq.volumeScale,
            prePhonemeLength: aq.prePhonemeLength,
            postPhonemeLength: aq.postPhonemeLength,
            outputSamplingRate: aq.outputSamplingRate,
            outputStereo: aq.outputStereo,
            kana: aq.kana,
        }
    }
}

impl From<AudioQueryInProject> for AudioQuery {
    fn from(aq: AudioQueryInProject) -> Self {
        Self {
            accent_phrases: aq
                .accentPhrases
                .iter()
                .map(|ap| ap.clone().into())
                .collect(),
            speedScale: aq.speedScale,
            pitchScale: aq.pitchScale,
            intonationScale: aq.intonationScale,
            volumeScale: aq.volumeScale,
            prePhonemeLength: aq.prePhonemeLength,
            postPhonemeLength: aq.postPhonemeLength,
            outputSamplingRate: aq.outputSamplingRate,
            outputStereo: aq.outputStereo,
            kana: aq.kana,
        }
    }
}

#[derive(Deserialize, Serialize, Debug, Clone, PartialEq)]
pub struct AccentPhrase {
    pub moras: Vec<Mora>,
    pub accent: i32,
    pub pause_mora: Option<Mora>,
    pub is_interrogative: Option<bool>,
}

#[allow(non_snake_case)]
#[derive(Deserialize, Debug, Clone, PartialEq)]
pub struct AccentPhraseInProject {
    pub moras: Vec<MoraInProject>,
    pub accent: i32,
    pub pause_mora: Option<MoraInProject>,
    pub isInterrogative: Option<bool>,
}

impl Serialize for AccentPhraseInProject {
    fn serialize<S>(&self, serializer: S) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
    where
        S: Serializer,
    {
        let mut s = serializer.serialize_struct("AccentPhrase", 4)?;
        s.serialize_field("moras", &self.moras)?;
        s.serialize_field("accent", &self.accent)?;
        if let Some(pause_mora) = &self.pause_mora {
            s.serialize_field("pause_mora", pause_mora)?;
        } else {
            s.skip_field("pause_mora")?;
        }
        s.serialize_field("isInterrogative", &self.isInterrogative.unwrap_or(false))?;
        s.end()
    }
}

impl From<AccentPhrase> for AccentPhraseInProject {
    fn from(ap: AccentPhrase) -> Self {
        Self {
            moras: ap.moras.iter().map(|mora| mora.clone().into()).collect(),
            accent: ap.accent,
            pause_mora: ap.pause_mora.map(|mora| mora.clone().into()),
            isInterrogative: ap.is_interrogative,
        }
    }
}

impl From<AccentPhraseInProject> for AccentPhrase {
    fn from(ap: AccentPhraseInProject) -> Self {
        Self {
            moras: ap.moras.iter().map(|mora| mora.clone().into()).collect(),
            accent: ap.accent,
            pause_mora: ap.pause_mora.map(|mora| mora.clone().into()),
            is_interrogative: ap.isInterrogative,
        }
    }
}

#[derive(Deserialize, Serialize, Debug, Clone, PartialEq)]
pub struct Mora {
    pub text: String,
    pub consonant: Option<String>,
    pub consonant_length: Option<f32>,
    pub vowel: String,
    pub vowel_length: f32,
    pub pitch: f32,
}

#[allow(non_snake_case)]
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq)]
pub struct MoraInProject {
    pub text: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(default)]
    pub consonant: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(default)]
    pub consonantLength: Option<f32>,
    pub vowel: String,
    pub vowelLength: f32,
    pub pitch: f32,
}

impl From<Mora> for MoraInProject {
    fn from(mora: Mora) -> Self {
        Self {
            text: mora.text,
            consonant: mora.consonant,
            consonantLength: mora.consonant_length,
            vowel: mora.vowel,
            vowelLength: mora.vowel_length,
            pitch: mora.pitch,
        }
    }
}

impl From<MoraInProject> for Mora {
    fn from(mora: MoraInProject) -> Self {
        Self {
            text: mora.text.clone(),
            consonant: mora.consonant.clone(),
            consonant_length: mora.consonantLength,
            vowel: mora.vowel.clone(),
            vowel_length: mora.vowelLength,
            pitch: mora.pitch,
        }
    }
}

#[allow(non_snake_case)]
#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct HttpValidationError {
    pub detail: Vec<ValidationError>,
}

#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct ValidationError {
    ///Location
    pub loc: Vec<String>,
    ///Message
    pub msg: String,
    ///Error Type
    #[serde(rename = "type")]
    pub _type: String,
}

#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct AccentPhrasesResponse {
    pub accent_phrases: Vec<AccentPhrase>,
}

#[derive(Deserialize, Serialize, Debug)]
pub struct ParseKanaBadRequest {
    pub text: String,
    pub error_name: String,
    pub error_args: String,
}

pub enum ErrorName {
    UnknownText,
    AccentTop,
    AccentTwice,
    AccentNotFound,
    EmptyPhrase,
    InterrogationMarkNotAtEnd,
    InfiniteLoop,
}

#[allow(non_snake_case, unused_variables)]
#[derive(Deserialize, Serialize, Debug)]
pub struct Preset {
    pub id: i32,
    pub name: String,
    pub speaker_uuid: String,
    pub style_id: i32,
    pub speedScale: f32,
    pub pitchScale: f32,
    pub intonationScale: f32,
    pub volumeScale: f32,
    pub prePhonemeLength: f32,
    pub postPhonemeLength: f32,
}

#[derive(Deserialize, Serialize, Debug)]
pub struct Speaker {
    /// character name
    pub name: String,
    /// used to call SpeakerInfo.
    pub speaker_uuid: String,
    /// collection of emotion style.
    pub styles: Vec<SpeakerStyle>,
    pub version: Option<String>,
}

#[derive(Deserialize, Serialize, Debug)]
pub struct SpeakerStyle {
    /// emotion style.
    pub name: String,
    /// style_id or speaker same as [StyleInfo.id]
    pub id: i32,
}

#[derive(Deserialize, Debug, Serialize)]
pub struct StyleInfoRaw {
    pub(crate) id: i32,
    /// base64
    pub(crate) icon: String,
    /// base64
    pub(crate) voice_samples: Vec<String>,
}

impl TryFrom<StyleInfoRaw> for StyleInfo {
    type Error = TryFromRawError;
    fn try_from(raw: StyleInfoRaw) -> Result<Self, <Self as TryFrom<StyleInfoRaw>>::Error> {
        Ok(Self {
            id: raw.id,
            icon: base64::decode(raw.icon).map_err(|_| TryFromRawError::Base64Decode)?,
            voice_samples: raw
                .voice_samples
                .iter()
                .filter_map(|b64| base64::decode(b64).ok())
                .collect(),
        })
    }
}

impl From<StyleInfo> for StyleInfoRaw {
    fn from(mut si: StyleInfo) -> Self {
        Self {
            id: si.id,
            icon: base64::encode(si.icon),
            voice_samples: si
                .voice_samples
                .drain(..)
                .map(|bytes| base64::encode(bytes))
                .collect(),
        }
    }
}

#[derive(Debug)]
pub struct SpeakerInfo {
    /// markdown format.
    pub policy: String,
    /// png file.
    pub portrait: Vec<u8>,
    pub style_infos: Vec<StyleInfo>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct SpeakerInfoRaw {
    pub(crate) policy: String,
    /// base64
    pub(crate) portrait: String,

    pub(crate) style_infos: Vec<StyleInfoRaw>,
}

pub enum TryFromRawError {
    Base64Decode,
}

impl TryFrom<SpeakerInfoRaw> for SpeakerInfo {
    type Error = TryFromRawError;
    fn try_from(mut raw: SpeakerInfoRaw) -> Result<Self, Self::Error> {
        Ok(Self {
            policy: raw.policy,
            portrait: base64::decode(raw.portrait).map_err(|_| TryFromRawError::Base64Decode)?,
            style_infos: raw
                .style_infos
                .drain(..)
                .filter_map(|si_raw| si_raw.try_into().ok())
                .collect(),
        })
    }
}

impl From<SpeakerInfo> for SpeakerInfoRaw {
    fn from(mut si: SpeakerInfo) -> Self {
        Self {
            policy: si.policy,
            portrait: base64::encode(&si.portrait),
            style_infos: si.style_infos.drain(..).map(|si| si.into()).collect(),
        }
    }
}

#[derive(Debug)]
pub struct StyleInfo {
    /// style_id or speaker. you can put into below API fields.
    /// * AudioQuery.speaker
    /// * AccentPhrases.speaker
    /// * MoraData.speaker
    /// * MoraPitch.speaker
    /// * MoraLength.speaker
    /// * Synthesis.speaker
    /// * CancellableSynthesis.speaker
    /// * MultiSynthesis.speaker
    /// * SynthesisMorphing.base_speaker
    /// * SynthesisMorphing.target_speaker
    pub id: i32,
    ///png file
    pub icon: Vec<u8>,
    ///wav file
    pub voice_samples: Vec<Vec<u8>>,
}

#[derive(Deserialize, Serialize, Debug)]
pub struct SupportedDevices {
    /// always support
    pub cpu: bool,
    /// if enabled when Nvidia gpu + 3GiB VRam
    pub cuda: bool,
    /// if enabled when DirectML supported by engine.
    pub dml: Option<bool>,
}

#[derive(Debug)]
pub struct DownloadableLibraries {
    pub download_url: String,
    pub bytes: usize,
    pub speaker: Speaker,
    pub speaker_info: SpeakerInfo,
}

#[derive(Deserialize, Serialize, Debug)]
pub struct DownloadableLibrariesRaw {
    download_url: String,
    bytes: usize,
    speaker: Speaker,
    speaker_info: SpeakerInfoRaw,
}

impl TryFrom<DownloadableLibrariesRaw> for DownloadableLibraries {
    type Error = TryFromRawError;

    fn try_from(value: DownloadableLibrariesRaw) -> Result<Self, Self::Error> {
        let speaker_info = value.speaker_info.try_into()?;
        Ok(Self {
            download_url: value.download_url,
            bytes: value.bytes,
            speaker: value.speaker,
            speaker_info,
        })
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct EngineManifestRaw {
    manifest_version: String,
    name: String,
    uuid: String,
    url: String,
    icon: String,
    default_sampling_rate: i32,
    term_of_service: String,
    update_infos: Vec<UpdateInfo>,
    dependency_licenses: Vec<LicenseInfo>,
    downloadable_libraries_path: String,
    downloadable_libraries_url: String,
}

pub struct EngineManifest {
    pub manifest_version: String,
    pub name: String,
    pub uuid: String,
    pub url: String,
    pub icon: Vec<u8>,
    pub default_sampling_rate: i32,
    pub term_of_service: String,
    pub update_infos: Vec<UpdateInfo>,
    pub dependency_licenses: Vec<LicenseInfo>,
    pub downloadable_libraries_path: String,
    pub downloadable_libraries_url: String,
}

impl TryFrom<EngineManifestRaw> for EngineManifest {
    type Error = TryFromRawError;
    fn try_from(raw: EngineManifestRaw) -> Result<Self, Self::Error> {
        Ok(Self {
            manifest_version: raw.manifest_version,
            name: raw.name,
            uuid: raw.uuid,
            url: raw.url,
            icon: base64::decode(raw.icon).map_err(|_| TryFromRawError::Base64Decode)?,
            default_sampling_rate: raw.default_sampling_rate,
            term_of_service: raw.term_of_service,
            update_infos: raw.update_infos,
            dependency_licenses: raw.dependency_licenses,
            downloadable_libraries_path: raw.downloadable_libraries_path,
            downloadable_libraries_url: raw.downloadable_libraries_url,
        })
    }
}

impl From<EngineManifest> for EngineManifestRaw {
    fn from(raw: EngineManifest) -> Self {
        Self {
            manifest_version: raw.manifest_version,
            name: raw.name,
            uuid: raw.uuid,
            url: raw.url,
            icon: base64::encode(raw.icon),
            default_sampling_rate: raw.default_sampling_rate,
            term_of_service: raw.term_of_service,
            update_infos: raw.update_infos,
            dependency_licenses: raw.dependency_licenses,
            downloadable_libraries_path: raw.downloadable_libraries_path,
            downloadable_libraries_url: raw.downloadable_libraries_url,
        }
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct UpdateInfo {
    version: String,
    descriptions: Vec<String>,
    contributers: Vec<String>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct LicenseInfo {
    name: String,
    version: String,
    license: String,
    text: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct UserDictWord {
    surface: String,
    priority: i32,
    context_id: Option<i32>,
    part_of_speech: String,
    part_of_speech_detail_1: String,
    part_of_speech_detail_2: String,
    part_of_speech_detail_3: String,
    inflectional_type: String,
    inflectional_form: String,
    stem: String,
    yomi: String,
    pronunciation: String,
    accent_type: i32,
    mora_count: Option<i32>,
    accent_associative_rule: String,
}

#[derive(Clone, Copy, Debug)]
pub enum WordType {
    ProperNoun,
    CommonNoun,
    Verb,
    Adjective,
    Suffix,
}

impl ToString for WordType {
    fn to_string(&self) -> String {
        match self {
            WordType::ProperNoun => "PROPER_NOUN",
            WordType::CommonNoun => "COMMON_NOUN",
            WordType::Verb => "VERB",
            WordType::Adjective => "ADJECTIVE",
            WordType::Suffix => "SUFFIX",
        }
        .to_owned()
    }
}