3
3
namespace OneBitSoftware . InputLanguageScreamer . Desktop ;
4
4
5
5
using System ;
6
+ using System . Collections . Generic ;
6
7
using System . IO ;
7
8
using System . Linq ;
8
9
using System . Media ;
@@ -18,6 +19,7 @@ public class LanguageAudioPlayer
18
19
private readonly string audioDirectory ;
19
20
private IWavePlayer ? wavePlayer ;
20
21
private AudioFileReader ? audioFileReader ;
22
+ private readonly Dictionary < string , string > audioFileCache ;
21
23
22
24
/// <summary>
23
25
/// Initializes the audio player with the specified audio directory
@@ -26,6 +28,31 @@ public class LanguageAudioPlayer
26
28
public LanguageAudioPlayer ( string audioDirectory )
27
29
{
28
30
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
+ }
29
56
}
30
57
31
58
/// <summary>
@@ -73,12 +100,18 @@ private string GetLanguageDisplayName(InputLanguage language)
73
100
{
74
101
// Extract language name from culture info
75
102
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 } ") ;
76
107
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
78
113
if ( cultureName . Contains ( "English" ) )
79
114
return "English" ;
80
- if ( cultureName . Contains ( "Bulgarian" ) )
81
- return "Bulgarian" ;
82
115
83
116
// For other languages, try to extract the first word
84
117
var firstWord = cultureName . Split ( ' ' ) [ 0 ] ;
@@ -92,16 +125,13 @@ private string GetLanguageDisplayName(InputLanguage language)
92
125
/// <returns>Path to the audio file, or null if not found</returns>
93
126
private string ? FindAudioFileForLanguage ( string languageName )
94
127
{
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 ;
105
135
}
106
136
107
137
/// <summary>
0 commit comments