33namespace OneBitSoftware . InputLanguageScreamer . Desktop ;
44
55using System ;
6+ using System . Collections . Generic ;
67using System . IO ;
78using System . Linq ;
89using System . Media ;
@@ -18,6 +19,7 @@ public class LanguageAudioPlayer
1819 private readonly string audioDirectory ;
1920 private IWavePlayer ? wavePlayer ;
2021 private AudioFileReader ? audioFileReader ;
22+ private readonly Dictionary < string , string > audioFileCache ;
2123
2224 /// <summary>
2325 /// Initializes the audio player with the specified audio directory
@@ -26,6 +28,31 @@ public class LanguageAudioPlayer
2628 public LanguageAudioPlayer ( string audioDirectory )
2729 {
2830 this . audioDirectory = audioDirectory ;
31+ this . audioFileCache = new Dictionary < string , string > ( StringComparer . OrdinalIgnoreCase ) ;
32+ CacheAudioFiles ( ) ;
33+ }
34+
35+ /// <summary>
36+ /// Caches all available audio files at startup to avoid disk operations during language changes
37+ /// </summary>
38+ private void CacheAudioFiles ( )
39+ {
40+ try
41+ {
42+ if ( Directory . Exists ( audioDirectory ) )
43+ {
44+ var audioFiles = Directory . GetFiles ( audioDirectory , "*.mp3" ) ;
45+ foreach ( var file in audioFiles )
46+ {
47+ var languageName = Path . GetFileNameWithoutExtension ( file ) ;
48+ audioFileCache [ languageName ] = file ;
49+ }
50+ }
51+ }
52+ catch ( Exception )
53+ {
54+ // Silently handle any errors during caching
55+ }
2956 }
3057
3158 /// <summary>
@@ -73,12 +100,18 @@ private string GetLanguageDisplayName(InputLanguage language)
73100 {
74101 // Extract language name from culture info
75102 var cultureName = language . Culture . EnglishName ;
103+ var layoutName = language . LayoutName ;
104+
105+ // Debug info - can be removed after fixing
106+ Console . WriteLine ( $ "Culture: { cultureName } , Layout: { layoutName } , LCID: { language . Culture . LCID } ") ;
76107
77- // Handle common language mappings
108+ // Handle Bulgarian specifically - check both culture name and layout
109+ if ( cultureName . Contains ( "Bulgarian" ) || layoutName . Contains ( "Bulgarian" ) )
110+ return "Bulgarian" ;
111+
112+ // Handle English
78113 if ( cultureName . Contains ( "English" ) )
79114 return "English" ;
80- if ( cultureName . Contains ( "Bulgarian" ) )
81- return "Bulgarian" ;
82115
83116 // For other languages, try to extract the first word
84117 var firstWord = cultureName . Split ( ' ' ) [ 0 ] ;
@@ -92,16 +125,13 @@ private string GetLanguageDisplayName(InputLanguage language)
92125 /// <returns>Path to the audio file, or null if not found</returns>
93126 private string ? FindAudioFileForLanguage ( string languageName )
94127 {
95- // Try exact match first
96- var exactMatch = Path . Combine ( audioDirectory , $ "{ languageName } .mp3") ;
97- if ( File . Exists ( exactMatch ) )
98- return exactMatch ;
99-
100- // Try case-insensitive search
101- var audioFiles = Directory . GetFiles ( audioDirectory , "*.mp3" ) ;
102- return audioFiles . FirstOrDefault ( file =>
103- Path . GetFileNameWithoutExtension ( file )
104- . Equals ( languageName , StringComparison . OrdinalIgnoreCase ) ) ;
128+ // Use the cached audio file if available
129+ if ( audioFileCache . TryGetValue ( languageName , out var audioFile ) )
130+ {
131+ return audioFile ;
132+ }
133+
134+ return null ;
105135 }
106136
107137 /// <summary>
0 commit comments