shabda 1.1.0

shabda — Grapheme-to-phoneme (G2P) conversion: text to phoneme sequences for vocal synthesis
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
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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
//! The G2P engine — ties normalization, dictionary, rules, and prosody together.

use alloc::{string::ToString, vec::Vec};
use serde::{Deserialize, Serialize};
use tracing::{trace, warn};

use svara::phoneme::Phoneme;
use svara::sequence::PhonemeEvent;

use crate::dictionary::PronunciationDict;
use crate::error::{Result, ShabdaError};
use crate::normalize;
use crate::prosody;
use crate::rules;

/// Supported languages for G2P conversion.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub enum Language {
    /// English (General American).
    English,
}

/// Detects the most likely language for the given text based on script analysis.
///
/// Uses varna's script Unicode range data to identify which writing system
/// the text uses, then maps that to a supported `Language`. Returns `None`
/// if the text uses a script not associated with any supported language.
///
/// Currently only English (Latin script) is supported. As more languages are
/// added to shabda, this function will detect them automatically.
///
/// # Examples
///
/// ```
/// use shabda::engine::detect_language;
///
/// assert_eq!(detect_language("hello world"), Some(shabda::engine::Language::English));
/// assert_eq!(detect_language(""), None);
/// ```
#[cfg(feature = "varna")]
#[must_use]
pub fn detect_language(text: &str) -> Option<Language> {
    if text.trim().is_empty() {
        return None;
    }

    // Count codepoints belonging to each known script
    let scripts = [
        ("Latn", Language::English),
        // Future: ("Deva", Language::Hindi), ("Arab", Language::Arabic), etc.
    ];

    let mut best: Option<(Language, usize)> = None;

    for (script_code, language) in &scripts {
        if let Some(script) = varna::script::by_code(script_code) {
            let count = text
                .chars()
                .filter(|c| script.contains_codepoint(u32::from(*c)))
                .count();
            if count > 0 {
                match best {
                    Some((_, best_count)) if count > best_count => {
                        best = Some((*language, count));
                    }
                    None => {
                        best = Some((*language, count));
                    }
                    _ => {}
                }
            }
        }
    }

    best.map(|(lang, _)| lang)
}

/// Options for controlling G2P conversion behavior.
///
/// Used with [`G2PEngine::convert_with`] to enable emphasis detection,
/// speaking rate control, and other prosody features. The default options
/// produce the same output as [`G2PEngine::convert`].
///
/// # Examples
///
/// ```
/// use shabda::engine::ConvertOptions;
///
/// let opts = ConvertOptions::new()
///     .with_emphasis(true)
///     .with_speaking_rate(120.0);
/// ```
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ConvertOptions {
    /// Enable emphasis detection from CAPS and *asterisks*.
    ///
    /// When `true`, ALL-CAPS words (3+ chars) receive emphatic stress,
    /// and `*wrapped*` words receive focus stress.
    #[serde(default)]
    pub emphasis: bool,

    /// Target speaking rate in words per minute.
    ///
    /// `None` uses the default rate (~150 WPM). Lower values produce
    /// slower, more deliberate speech; higher values produce faster speech.
    /// Clamped to 50–300 WPM.
    #[serde(default)]
    pub speaking_rate_wpm: Option<f32>,

    /// Phoneme-level timing profile for fine-grained duration control.
    ///
    /// `None` uses default durations from svara. Scales are multiplicative
    /// (1.0 = no change, 2.0 = double duration, 0.5 = half duration).
    #[serde(default)]
    pub timing: Option<TimingProfile>,
}

/// Phoneme-level timing control.
///
/// Allows independent scaling of vowel, consonant, and pause durations
/// for fine-grained control over speech rhythm.
///
/// # Examples
///
/// ```
/// use shabda::engine::TimingProfile;
///
/// // Crisp speech: shorter vowels, normal consonants
/// let crisp = TimingProfile::new(0.8, 1.0, 0.7);
///
/// // Deliberate speech: longer vowels, longer pauses
/// let deliberate = TimingProfile::new(1.3, 1.0, 1.5);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct TimingProfile {
    /// Scale factor for vowel/diphthong durations (default 1.0).
    pub vowel_scale: f32,
    /// Scale factor for consonant durations (default 1.0).
    pub consonant_scale: f32,
    /// Scale factor for pause/silence durations (default 1.0).
    pub pause_scale: f32,
}

impl TimingProfile {
    /// Creates a new timing profile with the given scale factors.
    #[must_use]
    pub fn new(vowel_scale: f32, consonant_scale: f32, pause_scale: f32) -> Self {
        Self {
            vowel_scale,
            consonant_scale,
            pause_scale,
        }
    }
}

impl Default for TimingProfile {
    fn default() -> Self {
        Self {
            vowel_scale: 1.0,
            consonant_scale: 1.0,
            pause_scale: 1.0,
        }
    }
}

impl ConvertOptions {
    /// Creates default conversion options.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Enables or disables emphasis detection.
    #[must_use]
    pub fn with_emphasis(mut self, emphasis: bool) -> Self {
        self.emphasis = emphasis;
        self
    }

    /// Sets the target speaking rate in words per minute.
    #[must_use]
    pub fn with_speaking_rate(mut self, wpm: f32) -> Self {
        self.speaking_rate_wpm = Some(wpm);
        self
    }

    /// Sets a timing profile for phoneme-level duration control.
    #[must_use]
    pub fn with_timing(mut self, timing: TimingProfile) -> Self {
        self.timing = Some(timing);
        self
    }
}

/// The grapheme-to-phoneme engine.
///
/// Converts text to svara `PhonemeEvent` sequences using dictionary lookup
/// with rule-based fallback and automatic prosody assignment.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct G2PEngine {
    /// Active language.
    language: Language,
    /// Pronunciation dictionary.
    dictionary: PronunciationDict,
}

impl G2PEngine {
    /// Creates a new G2P engine for the given language.
    #[must_use]
    pub fn new(language: Language) -> Self {
        let dictionary = match language {
            Language::English => PronunciationDict::english(),
        };
        Self {
            language,
            dictionary,
        }
    }

    /// Returns the active language.
    #[must_use]
    pub fn language(&self) -> Language {
        self.language
    }

    /// Returns a reference to the pronunciation dictionary.
    #[must_use]
    pub fn dictionary(&self) -> &PronunciationDict {
        &self.dictionary
    }

    /// Returns a mutable reference to the dictionary for adding custom entries.
    pub fn dictionary_mut(&mut self) -> &mut PronunciationDict {
        &mut self.dictionary
    }

    /// Converts text to a sequence of phoneme events with default options.
    ///
    /// Equivalent to `convert_with(text, &ConvertOptions::default())`.
    ///
    /// # Errors
    ///
    /// Returns `ShabdaError::InvalidInput` if the text is empty.
    ///
    /// # Examples
    ///
    /// ```
    /// use shabda::prelude::*;
    ///
    /// let g2p = G2PEngine::new(Language::English);
    /// let events = g2p.convert("hello world").unwrap();
    /// assert!(!events.is_empty());
    /// ```
    pub fn convert(&self, text: &str) -> Result<Vec<PhonemeEvent>> {
        self.convert_with(text, &ConvertOptions::default())
    }

    /// Converts text to a sequence of phoneme events with the given options.
    ///
    /// The pipeline:
    /// 1. Expand numbers to words and normalize text
    /// 2. Detect sentence intonation from punctuation
    /// 3. For each word: dictionary lookup → rule-based fallback
    /// 4. Syllabify and assign stress based on syllable weight
    /// 5. Apply emphasis markers (if enabled)
    /// 6. Apply speaking rate scaling (if set)
    /// 7. Insert phrase pauses at commas (150ms) and periods (300ms)
    /// 8. Insert word-boundary silence (40ms) between words
    ///
    /// # Errors
    ///
    /// Returns `ShabdaError::InvalidInput` if the text is empty.
    ///
    /// # Examples
    ///
    /// ```
    /// use shabda::prelude::*;
    ///
    /// let g2p = G2PEngine::new(Language::English);
    /// let opts = ConvertOptions::new()
    ///     .with_emphasis(true)
    ///     .with_speaking_rate(120.0);
    /// let events = g2p.convert_with("HELLO world", &opts).unwrap();
    /// assert!(!events.is_empty());
    /// ```
    pub fn convert_with(&self, text: &str, options: &ConvertOptions) -> Result<Vec<PhonemeEvent>> {
        if text.trim().is_empty() {
            return Err(ShabdaError::InvalidInput("empty text".to_string()));
        }

        #[cfg(feature = "varna")]
        let varna_inventory = crate::validate::inventory_for(self.language);

        let intonation = normalize::detect_intonation(text);
        let normalized = if options.emphasis {
            normalize::normalize_with_emphasis(text)
        } else {
            normalize::normalize(text)
        };

        trace!(
            input = text,
            normalized = normalized.as_str(),
            ?intonation,
            emphasis = options.emphasis,
            rate = ?options.speaking_rate_wpm,
            "converting text to phonemes"
        );

        let words: Vec<&str> = normalized.split_whitespace().collect();
        let mut events = Vec::new();
        let mut emphasis_active = false;

        for (i, word) in words.iter().enumerate() {
            // Handle emphasis markers
            if *word == normalize::EMPHASIS_START {
                emphasis_active = true;
                continue;
            }
            if *word == normalize::EMPHASIS_END {
                emphasis_active = false;
                continue;
            }

            // Handle phrase boundary markers
            if *word == normalize::COMMA_PAUSE {
                events.push(PhonemeEvent::new(
                    Phoneme::Silence,
                    0.15,
                    svara::prosody::Stress::Unstressed,
                ));
                continue;
            }
            if *word == normalize::PERIOD_PAUSE {
                events.push(PhonemeEvent::new(
                    Phoneme::Silence,
                    0.30,
                    svara::prosody::Stress::Unstressed,
                ));
                continue;
            }

            // Collect preceding content words for heteronym context
            let preceding: Vec<&str> = words[..i]
                .iter()
                .rev()
                .filter(|w| {
                    **w != normalize::COMMA_PAUSE
                        && **w != normalize::PERIOD_PAUSE
                        && **w != normalize::EMPHASIS_START
                        && **w != normalize::EMPHASIS_END
                })
                .take(3)
                .copied()
                .collect();

            // Look up in dictionary first, fall back to rules
            let phonemes: Vec<Phoneme> = if let Some(rule) = crate::heteronym::lookup(word) {
                // Heteronym: select variant based on context
                if let Some(prons) = self.dictionary.lookup_all(word) {
                    trace!(word, variant_count = prons.len(), "heteronym lookup");
                    crate::heteronym::select_phonemes(rule, &preceding, prons).to_vec()
                } else if let Some(dict_entry) = self.dictionary.lookup(word) {
                    dict_entry.to_vec()
                } else {
                    match self.language {
                        Language::English => rules::english_rules(word),
                    }
                }
            } else if let Some(dict_entry) = self.dictionary.lookup(word) {
                trace!(word, phoneme_count = dict_entry.len(), "dictionary hit");
                dict_entry.to_vec()
            } else if normalize::is_foreign_word(word) {
                // Foreign word: strip diacritics and try rules
                trace!(word, "foreign word detected, stripping diacritics");
                let stripped = normalize::strip_diacritics(word);
                if let Some(dict_entry) = self.dictionary.lookup(&stripped) {
                    dict_entry.to_vec()
                } else {
                    match self.language {
                        Language::English => rules::english_rules(&stripped),
                    }
                }
            } else {
                trace!(word, "dictionary miss, falling back to rules");
                match self.language {
                    Language::English => rules::english_rules(word),
                }
            };

            // Validate phoneme output against varna inventory in debug builds
            #[cfg(feature = "varna")]
            {
                let invalid = crate::validate::validate_phonemes(&phonemes, &varna_inventory);
                debug_assert!(
                    invalid.is_empty(),
                    "word {word:?} produced phonemes not in varna inventory: {invalid:?}"
                );
            }

            if phonemes.is_empty() {
                warn!(word, "no phonemes produced, skipping word");
                continue;
            }

            // Syllabify and assign stress based on syllable weight
            let is_content = prosody::is_content_word(word);
            let syllables = crate::syllable::syllabify(&phonemes);
            let mut word_events = if syllables.is_empty() {
                trace!(word, "no syllables (consonant-only), using simple stress");
                prosody::assign_stress(&phonemes, is_content)
            } else {
                trace!(
                    word,
                    syllable_count = syllables.len(),
                    is_content,
                    "syllabified"
                );
                prosody::assign_stress_syllabic(&syllables, is_content)
            };

            // Apply emphasis if active
            if emphasis_active {
                prosody::apply_emphasis(&mut word_events);
            }

            events.extend(word_events);

            // Insert short silence between words (not after last word)
            if i < words.len() - 1 {
                events.push(PhonemeEvent::new(
                    Phoneme::Silence,
                    0.04,
                    svara::prosody::Stress::Unstressed,
                ));
            }
        }

        // Apply speaking rate scaling
        if let Some(wpm) = options.speaking_rate_wpm {
            prosody::apply_rate(&mut events, wpm);
        }

        // Apply timing profile
        if let Some(ref timing) = options.timing {
            prosody::apply_timing(&mut events, timing);
        }

        Ok(events)
    }

    /// Converts text and renders directly to audio samples.
    ///
    /// Convenience method that combines G2P conversion with svara rendering.
    ///
    /// # Errors
    ///
    /// Returns errors from either G2P conversion or audio synthesis.
    ///
    /// # Examples
    ///
    /// ```
    /// use shabda::prelude::*;
    ///
    /// let g2p = G2PEngine::new(Language::English);
    /// let voice = svara::voice::VoiceProfile::new_male();
    /// let samples = g2p.speak("hello", &voice, 44100.0).unwrap();
    /// assert!(!samples.is_empty());
    /// ```
    pub fn speak(
        &self,
        text: &str,
        voice: &svara::voice::VoiceProfile,
        sample_rate: f32,
    ) -> Result<Vec<f32>> {
        self.speak_with(text, voice, sample_rate, &ConvertOptions::default())
    }

    /// Converts text and renders to audio samples with the given options.
    ///
    /// # Errors
    ///
    /// Returns errors from either G2P conversion or audio synthesis.
    pub fn speak_with(
        &self,
        text: &str,
        voice: &svara::voice::VoiceProfile,
        sample_rate: f32,
        options: &ConvertOptions,
    ) -> Result<Vec<f32>> {
        let events = self.convert_with(text, options)?;

        let mut seq = svara::sequence::PhonemeSequence::new();
        for event in events {
            seq.push(event);
        }

        seq.render(voice, sample_rate)
            .map_err(|e| ShabdaError::RuleError(alloc::format!("audio synthesis failed: {e}")))
    }

    /// Converts SSML-formatted text to a sequence of phoneme events.
    ///
    /// Parses the SSML markup and applies `<break>`, `<emphasis>`, and
    /// `<prosody>` elements to control the G2P pipeline.
    ///
    /// # Errors
    ///
    /// Returns `ShabdaError::InvalidInput` if the SSML is malformed or empty.
    ///
    /// # Examples
    ///
    /// ```
    /// use shabda::prelude::*;
    ///
    /// let g2p = G2PEngine::new(Language::English);
    /// let events = g2p.convert_ssml(
    ///     "Hello <break time=\"300ms\"/> <emphasis level=\"strong\">world</emphasis>"
    /// ).unwrap();
    /// assert!(!events.is_empty());
    /// ```
    pub fn convert_ssml(&self, ssml: &str) -> Result<Vec<PhonemeEvent>> {
        if ssml.trim().is_empty() {
            return Err(ShabdaError::InvalidInput("empty SSML".to_string()));
        }

        let nodes = crate::ssml::parse(ssml)
            .map_err(|e| ShabdaError::InvalidInput(alloc::format!("SSML parse error: {e}")))?;

        let mut events = Vec::new();
        self.render_ssml_nodes(&nodes, &ConvertOptions::default(), &mut events)?;
        Ok(events)
    }

    /// Recursively renders SSML nodes into phoneme events.
    fn render_ssml_nodes(
        &self,
        nodes: &[crate::ssml::SsmlNode],
        base_options: &ConvertOptions,
        events: &mut Vec<PhonemeEvent>,
    ) -> Result<()> {
        use crate::ssml::SsmlNode;

        for node in nodes {
            match node {
                SsmlNode::Text(text) => {
                    if !text.trim().is_empty() {
                        let mut text_events = self.convert_with(text, base_options)?;
                        events.append(&mut text_events);
                    }
                }
                SsmlNode::Break { duration_ms } => {
                    let duration_secs = *duration_ms as f32 / 1000.0;
                    events.push(PhonemeEvent::new(
                        Phoneme::Silence,
                        duration_secs,
                        svara::prosody::Stress::Unstressed,
                    ));
                }
                SsmlNode::Emphasis { level, children } => {
                    let emphasis_opts = ConvertOptions {
                        emphasis: true,
                        ..base_options.clone()
                    };
                    // Convert children with emphasis, then boost based on level
                    let start_idx = events.len();
                    self.render_ssml_nodes(children, &emphasis_opts, events)?;
                    let emphasis_events = &mut events[start_idx..];
                    // Apply emphasis — convert_with already does this for emphasis=true,
                    // but we also scale by level
                    match level {
                        crate::ssml::EmphasisLevel::Strong => {
                            prosody::apply_emphasis(emphasis_events);
                        }
                        crate::ssml::EmphasisLevel::Moderate => {
                            // Moderate: lighter emphasis already handled by emphasis=true
                        }
                        crate::ssml::EmphasisLevel::Reduced => {
                            // De-stress: set all to unstressed
                            for event in emphasis_events.iter_mut() {
                                event.stress = svara::prosody::Stress::Unstressed;
                            }
                        }
                    }
                }
                SsmlNode::Prosody { rate, children } => {
                    let prosody_opts = if let Some(r) = rate {
                        ConvertOptions {
                            speaking_rate_wpm: Some(r.wpm()),
                            ..base_options.clone()
                        }
                    } else {
                        base_options.clone()
                    };
                    self.render_ssml_nodes(children, &prosody_opts, events)?;
                }
            }
        }
        Ok(())
    }

    /// Converts text word-by-word, calling a callback after each word.
    ///
    /// Useful for real-time or streaming applications where phoneme events
    /// should be processed incrementally rather than buffered.
    ///
    /// The callback receives `(word, phoneme_events)` for each content word.
    /// Pause markers are delivered as words with silence events.
    ///
    /// # Errors
    ///
    /// Returns `ShabdaError::InvalidInput` if the text is empty, or propagates
    /// any error from the callback (via `ShabdaError::RuleError`).
    ///
    /// # Examples
    ///
    /// ```
    /// use shabda::prelude::*;
    ///
    /// let g2p = G2PEngine::new(Language::English);
    /// let mut word_count = 0;
    /// g2p.convert_streaming("hello world", |_word, _events| {
    ///     word_count += 1;
    /// }).unwrap();
    /// assert!(word_count >= 2);
    /// ```
    pub fn convert_streaming<F>(&self, text: &str, mut callback: F) -> Result<()>
    where
        F: FnMut(&str, &[PhonemeEvent]),
    {
        if text.trim().is_empty() {
            return Err(ShabdaError::InvalidInput("empty text".to_string()));
        }

        let normalized = normalize::normalize(text);
        let words: Vec<&str> = normalized.split_whitespace().collect();

        for word in &words {
            if *word == normalize::COMMA_PAUSE {
                let events = [PhonemeEvent::new(
                    Phoneme::Silence,
                    0.15,
                    svara::prosody::Stress::Unstressed,
                )];
                callback(word, &events);
                continue;
            }
            if *word == normalize::PERIOD_PAUSE {
                let events = [PhonemeEvent::new(
                    Phoneme::Silence,
                    0.30,
                    svara::prosody::Stress::Unstressed,
                )];
                callback(word, &events);
                continue;
            }

            let phonemes: Vec<Phoneme> = if let Some(dict_entry) = self.dictionary.lookup(word) {
                dict_entry.to_vec()
            } else {
                match self.language {
                    Language::English => rules::english_rules(word),
                }
            };

            if phonemes.is_empty() {
                continue;
            }

            let is_content = prosody::is_content_word(word);
            let syllables = crate::syllable::syllabify(&phonemes);
            let word_events = if syllables.is_empty() {
                prosody::assign_stress(&phonemes, is_content)
            } else {
                prosody::assign_stress_syllabic(&syllables, is_content)
            };

            callback(word, &word_events);
        }

        Ok(())
    }
}