voirs-sdk 0.1.0-rc.1

Unified SDK and public API for VoiRS speech 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
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
//! Result caching system for synthesis results and intermediate outputs.

use crate::{
    audio::AudioBuffer,
    error::{Result, VoirsError},
    traits::CacheStats,
    types::{MelSpectrogram, Phoneme},
};
use parking_lot::RwLock;
use serde::{Deserialize, Serialize};
use std::{
    collections::{HashMap, VecDeque},
    hash::{Hash, Hasher},
    path::PathBuf,
    sync::Arc,
    time::{Duration, SystemTime},
};
use tokio::{fs, io::AsyncWriteExt};
use tracing::{debug, info};

/// Advanced synthesis result cache with intelligent storage and retrieval
pub struct SynthesisResultCache {
    /// In-memory cache storage
    memory_cache: Arc<RwLock<HashMap<String, CachedSynthesisResult>>>,

    /// LRU tracking queue
    lru_queue: Arc<RwLock<VecDeque<String>>>,

    /// Disk cache directory
    disk_cache_dir: Option<PathBuf>,

    /// Cache configuration
    config: ResultCacheConfig,

    /// Current memory usage
    current_memory_usage: Arc<RwLock<usize>>,

    /// Cache statistics
    stats: Arc<RwLock<ResultCacheStats>>,

    /// Access frequency tracking
    access_frequency: Arc<RwLock<HashMap<String, AccessInfo>>>,

    /// Text similarity index for related results
    similarity_index: Arc<RwLock<HashMap<String, Vec<String>>>>,

    /// Quality metrics cache
    quality_cache: Arc<RwLock<HashMap<String, QualityMetrics>>>,
}

/// Configuration for result caching
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResultCacheConfig {
    /// Memory cache size in MB
    pub memory_cache_size_mb: usize,

    /// Disk cache size in MB
    pub disk_cache_size_mb: usize,

    /// Default TTL for results
    pub default_ttl_seconds: u64,

    /// TTL for high-quality results
    pub high_quality_ttl_seconds: u64,

    /// TTL for low-quality results
    pub low_quality_ttl_seconds: u64,

    /// Enable disk caching
    pub enable_disk_cache: bool,

    /// Enable compression for cached results
    pub enable_compression: bool,

    /// Enable similarity-based caching
    pub enable_similarity_cache: bool,

    /// Text similarity threshold (0.0-1.0)
    pub similarity_threshold: f64,

    /// Maximum similar results to track
    pub max_similar_results: usize,

    /// Enable background cleanup
    pub enable_background_cleanup: bool,

    /// Cleanup interval in seconds
    pub cleanup_interval_seconds: u64,

    /// Quality threshold for retention
    pub quality_retention_threshold: f64,

    /// Enable result validation
    pub enable_validation: bool,

    /// Cache partitioning by language
    pub partition_by_language: bool,

    /// Maximum text length for caching
    pub max_text_length: usize,
}

impl Default for ResultCacheConfig {
    fn default() -> Self {
        Self {
            memory_cache_size_mb: 512,
            disk_cache_size_mb: 2048,
            default_ttl_seconds: 86400,       // 24 hours
            high_quality_ttl_seconds: 604800, // 7 days
            low_quality_ttl_seconds: 3600,    // 1 hour
            enable_disk_cache: true,
            enable_compression: true,
            enable_similarity_cache: true,
            similarity_threshold: 0.85,
            max_similar_results: 10,
            enable_background_cleanup: true,
            cleanup_interval_seconds: 3600, // 1 hour
            quality_retention_threshold: 0.7,
            enable_validation: true,
            partition_by_language: true,
            max_text_length: 10000,
        }
    }
}

/// Cached synthesis result with metadata
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct CachedSynthesisResult {
    /// Original text
    pub text: String,

    /// Text language
    pub language: String,

    /// Configuration hash used for synthesis
    pub config_hash: u64,

    /// Generated phonemes
    pub phonemes: Vec<Phoneme>,

    /// Generated mel spectrogram
    pub mel_spectrogram: MelSpectrogram,

    /// Generated audio buffer
    pub audio_buffer: AudioBuffer,

    /// Synthesis metadata
    pub metadata: SynthesisMetadata,

    /// Cache metadata
    pub cache_metadata: CacheMetadata,

    /// Quality metrics
    pub quality_metrics: QualityMetrics,

    /// Size in bytes (estimated)
    pub size_bytes: usize,
}

/// Synthesis metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SynthesisMetadata {
    /// Voice used for synthesis
    pub voice_id: String,

    /// Model versions used
    pub model_versions: HashMap<String, String>,

    /// Synthesis duration in milliseconds
    pub synthesis_duration_ms: u64,

    /// Audio duration in seconds
    pub audio_duration_seconds: f64,

    /// Sample rate
    pub sample_rate: u32,

    /// Bit depth
    pub bit_depth: u16,

    /// Number of channels
    pub channels: u16,

    /// Synthesis parameters
    pub parameters: HashMap<String, f64>,

    /// Processing stages timing
    pub stage_timings: HashMap<String, u64>,
}

/// Cache metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CacheMetadata {
    /// When the result was cached
    pub cached_at: SystemTime,

    /// When the result expires
    pub expires_at: SystemTime,

    /// Last access time
    pub last_accessed: SystemTime,

    /// Access count
    pub access_count: u64,

    /// Cache hit count
    pub hit_count: u64,

    /// Result priority
    pub priority: ResultPriority,

    /// Whether result is pinned
    pub pinned: bool,

    /// Cache source
    pub source: CacheSource,

    /// Checksum for integrity
    pub checksum: String,
}

/// Result priority levels
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ResultPriority {
    /// Critical results (never evict)
    Critical,
    /// High priority
    High,
    /// Normal priority
    Normal,
    /// Low priority (evict first)
    Low,
}

impl Default for ResultPriority {
    fn default() -> Self {
        Self::Normal
    }
}

/// Cache source
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum CacheSource {
    /// Generated fresh
    Fresh,
    /// Loaded from memory cache
    Memory,
    /// Loaded from disk cache
    Disk,
    /// Similarity match
    Similar(String),
}

/// Quality metrics for synthesis results
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QualityMetrics {
    /// Overall quality score (0.0-1.0)
    pub overall_score: f64,

    /// Audio quality metrics
    pub audio_quality: AudioQualityMetrics,

    /// Pronunciation accuracy
    pub pronunciation_accuracy: f64,

    /// Naturalness score
    pub naturalness_score: f64,

    /// Intelligibility score
    pub intelligibility_score: f64,

    /// Prosody quality
    pub prosody_quality: f64,

    /// Synthesis confidence
    pub confidence_score: f64,

    /// Error metrics
    pub error_metrics: ErrorMetrics,
}

/// Audio quality metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AudioQualityMetrics {
    /// Signal-to-noise ratio
    pub snr_db: f64,

    /// Total harmonic distortion
    pub thd_percent: f64,

    /// Frequency response quality
    pub frequency_response_score: f64,

    /// Dynamic range
    pub dynamic_range_db: f64,

    /// Spectral quality
    pub spectral_quality_score: f64,
}

/// Error metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ErrorMetrics {
    /// Pronunciation errors
    pub pronunciation_errors: u32,

    /// Timing errors
    pub timing_errors: u32,

    /// Spectral artifacts
    pub spectral_artifacts: u32,

    /// Discontinuities
    pub discontinuities: u32,
}

/// Parameters for caching synthesis results
#[derive(Debug)]
pub struct SynthesisCacheParams {
    pub text: String,
    pub language: String,
    pub config_hash: u64,
    pub phonemes: Vec<Phoneme>,
    pub mel_spectrogram: MelSpectrogram,
    pub audio_buffer: AudioBuffer,
    pub metadata: SynthesisMetadata,
    pub quality_metrics: QualityMetrics,
}

/// Access information for frequency tracking
#[derive(Debug, Clone)]
pub struct AccessInfo {
    /// Total access count
    pub access_count: u64,

    /// Last access time
    pub last_access: SystemTime,

    /// Access frequency (accesses per hour)
    pub frequency: f64,

    /// Access history
    pub access_history: VecDeque<SystemTime>,
}

/// Result cache statistics
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ResultCacheStats {
    /// Basic cache stats
    pub basic_stats: CacheStats,

    /// Results generated
    pub results_generated: u64,

    /// Results served from cache
    pub results_served_from_cache: u64,

    /// Cache misses
    pub cache_misses: u64,

    /// Similarity matches
    pub similarity_matches: u64,

    /// Average synthesis time (ms)
    pub avg_synthesis_time_ms: f64,

    /// Average quality score
    pub avg_quality_score: f64,

    /// Quality distribution
    pub quality_distribution: QualityDistribution,

    /// Language distribution
    pub language_distribution: HashMap<String, u64>,

    /// Text length distribution
    pub text_length_distribution: HashMap<String, u64>,

    /// Disk cache usage
    pub disk_usage_bytes: u64,

    /// Compression ratio
    pub compression_ratio: f64,

    /// Results expired
    pub results_expired: u64,

    /// Results evicted
    pub results_evicted: u64,
}

/// Quality score distribution
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct QualityDistribution {
    /// Excellent quality (0.9-1.0)
    pub excellent: u64,

    /// Good quality (0.7-0.9)
    pub good: u64,

    /// Fair quality (0.5-0.7)
    pub fair: u64,

    /// Poor quality (0.0-0.5)
    pub poor: u64,
}

impl SynthesisResultCache {
    /// Create new synthesis result cache
    pub fn new(config: ResultCacheConfig, disk_cache_dir: Option<PathBuf>) -> Result<Self> {
        // Create disk cache directory if specified
        if let Some(ref dir) = disk_cache_dir {
            if config.enable_disk_cache {
                std::fs::create_dir_all(dir).map_err(|e| {
                    VoirsError::cache_error(format!("Failed to create cache directory: {e}"))
                })?;
            }
        }

        Ok(Self {
            memory_cache: Arc::new(RwLock::new(HashMap::new())),
            lru_queue: Arc::new(RwLock::new(VecDeque::new())),
            disk_cache_dir,
            config,
            current_memory_usage: Arc::new(RwLock::new(0)),
            stats: Arc::new(RwLock::new(ResultCacheStats::default())),
            access_frequency: Arc::new(RwLock::new(HashMap::new())),
            similarity_index: Arc::new(RwLock::new(HashMap::new())),
            quality_cache: Arc::new(RwLock::new(HashMap::new())),
        })
    }

    /// Get cached synthesis result
    pub async fn get_synthesis_result(
        &self,
        text: &str,
        language: &str,
        config_hash: u64,
    ) -> Option<CachedSynthesisResult> {
        let cache_key = self.make_cache_key(text, language, config_hash);

        // Try exact match first
        if let Some(result) = self.get_exact_match(&cache_key).await {
            self.update_access_info(&cache_key).await;
            self.update_hit_stats().await;
            return Some(result);
        }

        // Try similarity match if enabled
        if self.config.enable_similarity_cache {
            if let Some(result) = self.get_similarity_match(text, language, config_hash).await {
                self.update_hit_stats().await;
                return Some(result);
            }
        }

        // Update miss stats
        self.update_miss_stats().await;
        None
    }

    /// Get exact cache match
    async fn get_exact_match(&self, cache_key: &str) -> Option<CachedSynthesisResult> {
        let cache = self.memory_cache.read();

        if let Some(result) = cache.get(cache_key) {
            // Check expiration
            if result.cache_metadata.expires_at > SystemTime::now() {
                return Some(result.clone());
            }
        }

        None
    }

    /// Get similarity-based match
    async fn get_similarity_match(
        &self,
        text: &str,
        _language: &str,
        _config_hash: u64,
    ) -> Option<CachedSynthesisResult> {
        // Collect candidate keys without holding the lock
        let candidate_keys = {
            let similarity_index = self.similarity_index.read();
            let mut candidates = Vec::new();

            for (cached_text, similar_keys) in similarity_index.iter() {
                let similarity = self.calculate_text_similarity(text, cached_text);

                if similarity >= self.config.similarity_threshold {
                    candidates.extend(similar_keys.iter().cloned());
                }
            }

            candidates
        };

        // Now search for valid cached results without holding locks during await
        for similar_key in candidate_keys {
            if let Some(result) = self.get_exact_match(&similar_key).await {
                // Update stats for similarity match
                {
                    let mut stats = self.stats.write();
                    stats.similarity_matches += 1;
                }

                return Some(result);
            }
        }

        None
    }

    /// Calculate text similarity (simple implementation)
    fn calculate_text_similarity(&self, text1: &str, text2: &str) -> f64 {
        // Simple Levenshtein distance-based similarity
        let distance = self.levenshtein_distance(text1, text2);
        let max_len = text1.len().max(text2.len());

        if max_len == 0 {
            1.0
        } else {
            1.0 - (distance as f64 / max_len as f64)
        }
    }

    /// Calculate Levenshtein distance
    #[allow(clippy::needless_range_loop)]
    fn levenshtein_distance(&self, text1: &str, text2: &str) -> usize {
        let chars1: Vec<char> = text1.chars().collect();
        let chars2: Vec<char> = text2.chars().collect();
        let len1 = chars1.len();
        let len2 = chars2.len();

        let mut matrix = vec![vec![0; len2 + 1]; len1 + 1];

        for i in 0..=len1 {
            matrix[i][0] = i;
        }
        for j in 0..=len2 {
            matrix[0][j] = j;
        }

        for i in 1..=len1 {
            for j in 1..=len2 {
                let cost = if chars1[i - 1] == chars2[j - 1] { 0 } else { 1 };
                matrix[i][j] = (matrix[i - 1][j] + 1)
                    .min(matrix[i][j - 1] + 1)
                    .min(matrix[i - 1][j - 1] + cost);
            }
        }

        matrix[len1][len2]
    }

    /// Cache synthesis result
    pub async fn put_synthesis_result(&self, params: SynthesisCacheParams) -> Result<()> {
        // Check text length limit
        if params.text.len() > self.config.max_text_length {
            return Err(VoirsError::cache_error(format!(
                "Text too long for caching: {} > {}",
                params.text.len(),
                self.config.max_text_length
            )));
        }

        let cache_key = self.make_cache_key(&params.text, &params.language, params.config_hash);

        // Calculate TTL based on quality
        let ttl = self.calculate_ttl(&params.quality_metrics);

        // Calculate size before moving values
        let estimated_size =
            self.estimate_result_size(&params.text, &params.phonemes, &params.audio_buffer);

        // Create cached result
        let cached_result = CachedSynthesisResult {
            text: params.text.clone(),
            language: params.language.clone(),
            config_hash: params.config_hash,
            phonemes: params.phonemes,
            mel_spectrogram: params.mel_spectrogram,
            audio_buffer: params.audio_buffer,
            metadata: params.metadata,
            cache_metadata: CacheMetadata {
                cached_at: SystemTime::now(),
                expires_at: SystemTime::now() + Duration::from_secs(ttl),
                last_accessed: SystemTime::now(),
                access_count: 0,
                hit_count: 0,
                priority: self.determine_priority(&params.quality_metrics),
                pinned: false,
                source: CacheSource::Fresh,
                checksum: self.calculate_checksum(&params.text, params.config_hash),
            },
            quality_metrics: params.quality_metrics.clone(),
            size_bytes: estimated_size,
        };

        // Ensure capacity
        self.ensure_capacity(cached_result.size_bytes).await?;

        // Store in memory cache
        {
            let mut cache = self.memory_cache.write();
            let mut current_usage = self.current_memory_usage.write();
            let mut lru_queue = self.lru_queue.write();

            let result_size = cached_result.size_bytes;
            cache.insert(cache_key.clone(), cached_result.clone());
            *current_usage += result_size;
            lru_queue.push_front(cache_key.clone());
        }

        // Update similarity index
        if self.config.enable_similarity_cache {
            self.update_similarity_index(&params.text, &cache_key).await;
        }

        // Store quality metrics
        {
            let mut quality_cache = self.quality_cache.write();
            quality_cache.insert(cache_key.clone(), params.quality_metrics);
        }

        // Update statistics
        self.update_cache_stats(&cached_result).await;

        // Persist to disk if enabled
        if self.config.enable_disk_cache {
            self.persist_to_disk(&cache_key, &cached_result).await?;
        }

        info!(
            "Cached synthesis result for '{}' ({})",
            params.text, cache_key
        );
        Ok(())
    }

    /// Calculate TTL based on quality metrics
    fn calculate_ttl(&self, quality_metrics: &QualityMetrics) -> u64 {
        if quality_metrics.overall_score >= 0.9 {
            self.config.high_quality_ttl_seconds
        } else if quality_metrics.overall_score >= 0.5 {
            self.config.default_ttl_seconds
        } else {
            self.config.low_quality_ttl_seconds
        }
    }

    /// Determine result priority based on quality
    fn determine_priority(&self, quality_metrics: &QualityMetrics) -> ResultPriority {
        if quality_metrics.overall_score >= 0.95 {
            ResultPriority::Critical
        } else if quality_metrics.overall_score >= 0.8 {
            ResultPriority::High
        } else if quality_metrics.overall_score >= 0.6 {
            ResultPriority::Normal
        } else {
            ResultPriority::Low
        }
    }

    /// Ensure sufficient cache capacity
    async fn ensure_capacity(&self, required_bytes: usize) -> Result<()> {
        let current_usage = *self.current_memory_usage.read();
        let max_bytes = self.config.memory_cache_size_mb * 1024 * 1024;

        if current_usage + required_bytes > max_bytes {
            self.evict_lru_results(required_bytes).await?;
        }

        Ok(())
    }

    /// Evict LRU results to free space
    async fn evict_lru_results(&self, required_bytes: usize) -> Result<()> {
        let mut freed_bytes = 0;
        let mut evicted_count = 0;

        loop {
            let key_to_evict = {
                let mut lru_queue = self.lru_queue.write();
                lru_queue.pop_back()
            };

            if let Some(key) = key_to_evict {
                // Check if result is pinned
                let can_evict = {
                    let cache = self.memory_cache.read();
                    cache
                        .get(&key)
                        .map(|result| !result.cache_metadata.pinned)
                        .unwrap_or(false)
                };

                if can_evict {
                    if let Some(result) = self.remove_result(&key).await? {
                        freed_bytes += result.size_bytes;
                        evicted_count += 1;

                        if freed_bytes >= required_bytes {
                            break;
                        }
                    }
                }
            } else {
                // No more results to evict
                break;
            }
        }

        // Update statistics
        {
            let mut stats = self.stats.write();
            stats.results_evicted += evicted_count;
        }

        info!(
            "Evicted {} results, freed {} bytes",
            evicted_count, freed_bytes
        );
        Ok(())
    }

    /// Remove a result from cache
    async fn remove_result(&self, key: &str) -> Result<Option<CachedSynthesisResult>> {
        let removed_result = {
            let mut cache = self.memory_cache.write();
            cache.remove(key)
        };

        if let Some(ref result) = removed_result {
            // Update memory usage
            {
                let mut current_usage = self.current_memory_usage.write();
                *current_usage = current_usage.saturating_sub(result.size_bytes);
            }

            // Remove from quality cache
            {
                let mut quality_cache = self.quality_cache.write();
                quality_cache.remove(key);
            }

            // Update statistics
            {
                let mut stats = self.stats.write();
                stats.basic_stats.total_entries = stats.basic_stats.total_entries.saturating_sub(1);
                stats.basic_stats.memory_usage_bytes = *self.current_memory_usage.read();
            }
        }

        Ok(removed_result)
    }

    /// Update similarity index
    async fn update_similarity_index(&self, text: &str, cache_key: &str) {
        let mut similarity_index = self.similarity_index.write();

        similarity_index
            .entry(text.to_string())
            .or_default()
            .push(cache_key.to_string());

        // Limit the number of similar results
        for similar_keys in similarity_index.values_mut() {
            if similar_keys.len() > self.config.max_similar_results {
                similar_keys.truncate(self.config.max_similar_results);
            }
        }
    }

    /// Update access information
    async fn update_access_info(&self, cache_key: &str) {
        let mut access_frequency = self.access_frequency.write();
        let now = SystemTime::now();

        let access_info = access_frequency
            .entry(cache_key.to_string())
            .or_insert_with(|| AccessInfo {
                access_count: 0,
                last_access: now,
                frequency: 0.0,
                access_history: VecDeque::new(),
            });

        access_info.access_count += 1;
        access_info.last_access = now;
        access_info.access_history.push_front(now);

        // Keep only recent history
        while access_info.access_history.len() > 100 {
            access_info.access_history.pop_back();
        }

        // Calculate frequency (accesses per hour)
        let hour_ago = now - Duration::from_secs(3600);
        let recent_accesses = access_info
            .access_history
            .iter()
            .filter(|&&access_time| access_time >= hour_ago)
            .count();

        access_info.frequency = recent_accesses as f64;
    }

    /// Update hit statistics
    async fn update_hit_stats(&self) {
        let mut stats = self.stats.write();
        stats.results_served_from_cache += 1;

        // Update hit rate
        let total_requests = stats.results_served_from_cache + stats.cache_misses;
        stats.basic_stats.hit_rate =
            ((stats.results_served_from_cache as f64 / total_requests as f64) * 100.0) as f32;
        stats.basic_stats.miss_rate = 100.0 - stats.basic_stats.hit_rate;
    }

    /// Update miss statistics
    async fn update_miss_stats(&self) {
        let mut stats = self.stats.write();
        stats.cache_misses += 1;

        // Update miss rate
        let total_requests = stats.results_served_from_cache + stats.cache_misses;
        stats.basic_stats.miss_rate =
            ((stats.cache_misses as f64 / total_requests as f64) * 100.0) as f32;
        stats.basic_stats.hit_rate = 100.0 - stats.basic_stats.miss_rate;
    }

    /// Update cache statistics
    async fn update_cache_stats(&self, result: &CachedSynthesisResult) {
        let mut stats = self.stats.write();

        stats.results_generated += 1;
        stats.basic_stats.total_entries += 1;
        stats.basic_stats.memory_usage_bytes = *self.current_memory_usage.read();

        // Update quality distribution
        let quality = result.quality_metrics.overall_score;
        if quality >= 0.9 {
            stats.quality_distribution.excellent += 1;
        } else if quality >= 0.7 {
            stats.quality_distribution.good += 1;
        } else if quality >= 0.5 {
            stats.quality_distribution.fair += 1;
        } else {
            stats.quality_distribution.poor += 1;
        }

        // Update language distribution
        *stats
            .language_distribution
            .entry(result.language.clone())
            .or_insert(0) += 1;

        // Update text length distribution
        let length_category = match result.text.len() {
            0..=100 => "short",
            101..=500 => "medium",
            501..=2000 => "long",
            _ => "very_long",
        };
        *stats
            .text_length_distribution
            .entry(length_category.to_string())
            .or_insert(0) += 1;

        // Update average quality
        let total_quality =
            stats.avg_quality_score * (stats.results_generated - 1) as f64 + quality;
        stats.avg_quality_score = total_quality / stats.results_generated as f64;

        // Update average synthesis time
        let synthesis_time = result.metadata.synthesis_duration_ms as f64;
        let total_time =
            stats.avg_synthesis_time_ms * (stats.results_generated - 1) as f64 + synthesis_time;
        stats.avg_synthesis_time_ms = total_time / stats.results_generated as f64;
    }

    /// Persist result to disk
    async fn persist_to_disk(&self, cache_key: &str, result: &CachedSynthesisResult) -> Result<()> {
        if let Some(ref cache_dir) = self.disk_cache_dir {
            let file_path = cache_dir.join(format!("{cache_key}.cache"));

            // Serialize result (simplified - would use proper serialization in practice)
            let serialized = serde_json::to_vec(result)
                .map_err(|e| VoirsError::cache_error(format!("Failed to serialize result: {e}")))?;

            // Write to disk
            let mut file = fs::File::create(&file_path).await.map_err(|e| {
                VoirsError::cache_error(format!("Failed to create cache file: {e}"))
            })?;

            file.write_all(&serialized)
                .await
                .map_err(|e| VoirsError::cache_error(format!("Failed to write cache file: {e}")))?;

            debug!("Persisted result to disk: {}", file_path.display());
        }

        Ok(())
    }

    /// Make cache key
    fn make_cache_key(&self, text: &str, language: &str, config_hash: u64) -> String {
        use std::collections::hash_map::DefaultHasher;

        let mut hasher = DefaultHasher::new();
        text.hash(&mut hasher);
        language.hash(&mut hasher);
        config_hash.hash(&mut hasher);

        format!("synthesis_{}_{:x}", language, hasher.finish())
    }

    /// Calculate checksum
    fn calculate_checksum(&self, text: &str, config_hash: u64) -> String {
        use std::collections::hash_map::DefaultHasher;

        let mut hasher = DefaultHasher::new();
        text.hash(&mut hasher);
        config_hash.hash(&mut hasher);

        format!("{:x}", hasher.finish())
    }

    /// Estimate result size
    fn estimate_result_size(&self, text: &str, phonemes: &[Phoneme], audio: &AudioBuffer) -> usize {
        text.len() +
        phonemes.len() * 32 + // Rough estimate for phoneme data
        audio.samples().len() * 4 + // Assuming 32-bit samples
        1024 // Metadata overhead
    }

    /// Clear expired results
    pub async fn cleanup_expired(&self) -> Result<usize> {
        let now = SystemTime::now();
        let mut expired_keys = Vec::new();

        // Find expired results
        {
            let cache = self.memory_cache.read();
            for (key, result) in cache.iter() {
                if result.cache_metadata.expires_at <= now && !result.cache_metadata.pinned {
                    expired_keys.push(key.clone());
                }
            }
        }

        // Remove expired results
        let mut removed_count = 0;
        for key in expired_keys {
            if self.remove_result(&key).await?.is_some() {
                removed_count += 1;
            }
        }

        // Update statistics
        {
            let mut stats = self.stats.write();
            stats.results_expired += removed_count;
        }

        Ok(removed_count as usize)
    }

    /// Clear all cached results
    pub async fn clear(&self) -> Result<()> {
        {
            let mut cache = self.memory_cache.write();
            let mut current_usage = self.current_memory_usage.write();
            let mut lru_queue = self.lru_queue.write();
            let mut access_frequency = self.access_frequency.write();
            let mut similarity_index = self.similarity_index.write();
            let mut quality_cache = self.quality_cache.write();

            cache.clear();
            *current_usage = 0;
            lru_queue.clear();
            access_frequency.clear();
            similarity_index.clear();
            quality_cache.clear();
        }

        // Reset statistics
        {
            let mut stats = self.stats.write();
            stats.basic_stats.total_entries = 0;
            stats.basic_stats.memory_usage_bytes = 0;
        }

        info!("Cleared all synthesis results from cache");
        Ok(())
    }

    /// Get cache statistics
    pub fn stats(&self) -> ResultCacheStats {
        self.stats.read().clone()
    }

    /// Get cache usage summary
    pub async fn get_usage_summary(&self) -> CacheUsageSummary {
        let cache = self.memory_cache.read();
        let current_usage = *self.current_memory_usage.read();
        let max_bytes = self.config.memory_cache_size_mb * 1024 * 1024;

        CacheUsageSummary {
            total_results: cache.len(),
            memory_usage_bytes: current_usage,
            memory_usage_mb: current_usage / (1024 * 1024),
            memory_utilization: (current_usage as f64 / max_bytes as f64) * 100.0,
            avg_result_size: if !cache.is_empty() {
                current_usage / cache.len()
            } else {
                0
            },
        }
    }
}

/// Cache usage summary
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CacheUsageSummary {
    /// Total number of cached results
    pub total_results: usize,

    /// Memory usage in bytes
    pub memory_usage_bytes: usize,

    /// Memory usage in MB
    pub memory_usage_mb: usize,

    /// Memory utilization percentage
    pub memory_utilization: f64,

    /// Average result size in bytes
    pub avg_result_size: usize,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        audio::AudioBuffer,
        types::{MelSpectrogram, Phoneme},
    };

    #[tokio::test]
    async fn test_synthesis_result_cache_creation() {
        let config = ResultCacheConfig::default();
        let cache = SynthesisResultCache::new(config, None).unwrap();

        let summary = cache.get_usage_summary().await;
        assert_eq!(summary.total_results, 0);
    }

    #[tokio::test]
    async fn test_result_caching_and_retrieval() {
        let config = ResultCacheConfig::default();
        let cache = SynthesisResultCache::new(config, None).unwrap();

        let text = "Hello, world!";
        let language = "en";
        let config_hash = 12345u64;
        let phonemes = vec![Phoneme::new("h"), Phoneme::new("É›")];
        let mel = MelSpectrogram::new(vec![vec![0.5; 100]; 80], 22050, 256);
        let audio = AudioBuffer::sine_wave(440.0, 1.0, 22050, 0.5);

        let metadata = SynthesisMetadata {
            voice_id: "test_voice".to_string(),
            model_versions: HashMap::new(),
            synthesis_duration_ms: 100,
            audio_duration_seconds: 1.0,
            sample_rate: 22050,
            bit_depth: 16,
            channels: 1,
            parameters: HashMap::new(),
            stage_timings: HashMap::new(),
        };

        let quality_metrics = QualityMetrics {
            overall_score: 0.85,
            audio_quality: AudioQualityMetrics {
                snr_db: 30.0,
                thd_percent: 0.1,
                frequency_response_score: 0.9,
                dynamic_range_db: 60.0,
                spectral_quality_score: 0.85,
            },
            pronunciation_accuracy: 0.9,
            naturalness_score: 0.8,
            intelligibility_score: 0.95,
            prosody_quality: 0.8,
            confidence_score: 0.85,
            error_metrics: ErrorMetrics {
                pronunciation_errors: 0,
                timing_errors: 0,
                spectral_artifacts: 0,
                discontinuities: 0,
            },
        };

        // Cache the result
        let cache_params = SynthesisCacheParams {
            text: text.to_string(),
            language: language.to_string(),
            config_hash,
            phonemes,
            mel_spectrogram: mel,
            audio_buffer: audio,
            metadata,
            quality_metrics,
        };

        cache.put_synthesis_result(cache_params).await.unwrap();

        // Retrieve the result
        let result = cache
            .get_synthesis_result(text, language, config_hash)
            .await;
        assert!(result.is_some());

        let cached = result.unwrap();
        assert_eq!(cached.text, text);
        assert_eq!(cached.language, language);
    }

    #[tokio::test]
    async fn test_result_expiration() {
        let config = ResultCacheConfig {
            default_ttl_seconds: 1, // Very short TTL
            ..Default::default()
        };

        let cache = SynthesisResultCache::new(config, None).unwrap();

        let text = "Test expiration";
        let language = "en";
        let config_hash = 67890u64;
        let phonemes = vec![Phoneme::new("t")];
        let mel = MelSpectrogram::new(vec![vec![0.5; 10]; 10], 22050, 256);
        let audio = AudioBuffer::sine_wave(440.0, 0.1, 22050, 0.5);

        let metadata = SynthesisMetadata {
            voice_id: "test_voice".to_string(),
            model_versions: HashMap::new(),
            synthesis_duration_ms: 50,
            audio_duration_seconds: 0.1,
            sample_rate: 22050,
            bit_depth: 16,
            channels: 1,
            parameters: HashMap::new(),
            stage_timings: HashMap::new(),
        };

        let quality_metrics = QualityMetrics {
            overall_score: 0.7,
            audio_quality: AudioQualityMetrics {
                snr_db: 25.0,
                thd_percent: 0.2,
                frequency_response_score: 0.8,
                dynamic_range_db: 50.0,
                spectral_quality_score: 0.7,
            },
            pronunciation_accuracy: 0.8,
            naturalness_score: 0.7,
            intelligibility_score: 0.9,
            prosody_quality: 0.7,
            confidence_score: 0.7,
            error_metrics: ErrorMetrics {
                pronunciation_errors: 1,
                timing_errors: 0,
                spectral_artifacts: 0,
                discontinuities: 0,
            },
        };

        let cache_params = SynthesisCacheParams {
            text: text.to_string(),
            language: language.to_string(),
            config_hash,
            phonemes,
            mel_spectrogram: mel,
            audio_buffer: audio,
            metadata,
            quality_metrics,
        };

        cache.put_synthesis_result(cache_params).await.unwrap();

        // Should find it immediately
        assert!(cache
            .get_synthesis_result(text, language, config_hash)
            .await
            .is_some());

        // Wait for expiration
        tokio::time::sleep(tokio::time::Duration::from_secs(2)).await;

        // Should not find it after expiration
        assert!(cache
            .get_synthesis_result(text, language, config_hash)
            .await
            .is_none());
    }

    #[tokio::test]
    async fn test_cache_cleanup() {
        let config = ResultCacheConfig::default();
        let cache = SynthesisResultCache::new(config, None).unwrap();

        // Add a test result
        let text = "Cleanup test";
        let language = "en";
        let config_hash = 99999u64;
        let phonemes = vec![Phoneme::new("k")];
        let mel = MelSpectrogram::new(vec![vec![0.5; 10]; 10], 22050, 256);
        let audio = AudioBuffer::sine_wave(440.0, 0.1, 22050, 0.5);

        let metadata = SynthesisMetadata {
            voice_id: "test_voice".to_string(),
            model_versions: HashMap::new(),
            synthesis_duration_ms: 30,
            audio_duration_seconds: 0.1,
            sample_rate: 22050,
            bit_depth: 16,
            channels: 1,
            parameters: HashMap::new(),
            stage_timings: HashMap::new(),
        };

        let quality_metrics = QualityMetrics {
            overall_score: 0.6,
            audio_quality: AudioQualityMetrics {
                snr_db: 20.0,
                thd_percent: 0.3,
                frequency_response_score: 0.7,
                dynamic_range_db: 40.0,
                spectral_quality_score: 0.6,
            },
            pronunciation_accuracy: 0.7,
            naturalness_score: 0.6,
            intelligibility_score: 0.8,
            prosody_quality: 0.6,
            confidence_score: 0.6,
            error_metrics: ErrorMetrics {
                pronunciation_errors: 2,
                timing_errors: 1,
                spectral_artifacts: 0,
                discontinuities: 0,
            },
        };

        let cache_params = SynthesisCacheParams {
            text: text.to_string(),
            language: language.to_string(),
            config_hash,
            phonemes,
            mel_spectrogram: mel,
            audio_buffer: audio,
            metadata,
            quality_metrics,
        };

        cache.put_synthesis_result(cache_params).await.unwrap();

        // Cleanup (should find 0 expired since we just added it)
        let expired_count = cache.cleanup_expired().await.unwrap();
        assert_eq!(expired_count, 0);

        // Clear all
        cache.clear().await.unwrap();
        let summary = cache.get_usage_summary().await;
        assert_eq!(summary.total_results, 0);
    }
}