piper_plus/phonemize/
mod.rs1use std::collections::HashMap;
7
8use crate::config::PhonemeIdMap;
9use crate::error::PiperError;
10
11pub use piper_plus_g2p::G2pError;
13pub use piper_plus_g2p::PhonemeIdMap as G2pPhonemeIdMap;
14
15pub mod adapter;
16pub mod chinese;
17pub use piper_plus_g2p::custom_dict;
18pub mod english;
19pub mod french;
20#[cfg(feature = "japanese")]
21pub mod japanese;
22pub mod korean;
23pub use piper_plus_g2p::multilingual;
24pub mod portuguese;
25pub mod spanish;
26pub use piper_plus_g2p::token_map;
27
28#[derive(Debug, Clone, Copy)]
30pub struct ProsodyInfo {
31 pub a1: i32,
32 pub a2: i32,
33 pub a3: i32,
34}
35
36pub type ProsodyFeature = [i32; 3];
38
39pub trait Phonemizer: Send + Sync {
41 fn phonemize_with_prosody(
43 &self,
44 text: &str,
45 ) -> Result<(Vec<String>, Vec<Option<ProsodyInfo>>), PiperError>;
46
47 fn get_phoneme_id_map(&self) -> Option<&PhonemeIdMap>;
49
50 fn post_process_ids(
52 &self,
53 ids: Vec<i64>,
54 prosody: Vec<Option<ProsodyFeature>>,
55 id_map: &PhonemeIdMap,
56 ) -> (Vec<i64>, Vec<Option<ProsodyFeature>>);
57
58 fn language_code(&self) -> &str;
60
61 fn detect_primary_language(&self, _text: &str) -> &str {
66 self.language_code()
67 }
68}
69
70pub struct PhonemizerRegistry {
72 registry: HashMap<String, Box<dyn Phonemizer>>,
73}
74
75impl PhonemizerRegistry {
76 pub fn new() -> Self {
77 Self {
78 registry: HashMap::new(),
79 }
80 }
81
82 pub fn register(&mut self, lang_code: &str, phonemizer: Box<dyn Phonemizer>) {
83 self.registry.insert(lang_code.to_string(), phonemizer);
84 }
85
86 pub fn get(&self, lang_code: &str) -> Option<&dyn Phonemizer> {
87 self.registry.get(lang_code).map(|p| p.as_ref())
88 }
89
90 pub fn available_languages(&self) -> Vec<&str> {
91 self.registry.keys().map(|s| s.as_str()).collect()
92 }
93}
94
95impl Default for PhonemizerRegistry {
96 fn default() -> Self {
97 Self::new()
98 }
99}