-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathApp.Voice.cs
More file actions
131 lines (102 loc) · 2.92 KB
/
Copy pathApp.Voice.cs
File metadata and controls
131 lines (102 loc) · 2.92 KB
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
using System.Speech.Synthesis;
using System.Windows;
namespace MarvinsAIRA
{
public partial class App : Application
{
private SpeechSynthesizer? _voice_speechSynthesizer = null;
public void InitializeVoice()
{
WriteLine( "InitializeVoice called.", true );
if ( _voice_speechSynthesizer != null )
{
WriteLine( "...disposing of old voice syntehsizer..." );
_voice_speechSynthesizer.Pause();
_voice_speechSynthesizer.SpeakAsyncCancelAll();
_voice_speechSynthesizer.Dispose();
_voice_speechSynthesizer = null;
WriteLine( "...old voice syntehsizer has been disposed..." );
}
WriteLine( "...creating new voice synthesizer..." );
_voice_speechSynthesizer = new();
try
{
SpeechApiReflectionHelper.InjectOneCoreVoices( _voice_speechSynthesizer );
}
catch ( Exception )
{
WriteLine( "...note - exception thrown while trying to inject one core voices into the speech synthesizer..." );
}
var installedVoices = _voice_speechSynthesizer.GetInstalledVoices();
var firstInstalledVoice = string.Empty;
var selectedVoiceFound = false;
SerializableDictionary<string, string> voiceList = [];
foreach ( var installedVoice in installedVoices )
{
if ( !voiceList.ContainsKey( installedVoice.VoiceInfo.Name ) )
{
voiceList.Add( installedVoice.VoiceInfo.Name, installedVoice.VoiceInfo.Description );
if ( firstInstalledVoice == string.Empty )
{
firstInstalledVoice = installedVoice.VoiceInfo.Name;
}
if ( Settings.SelectedVoice == installedVoice.VoiceInfo.Name )
{
selectedVoiceFound = true;
}
}
}
if ( !selectedVoiceFound )
{
Settings.SelectedVoice = firstInstalledVoice;
}
Settings.UpdateVoiceList( voiceList );
_voice_speechSynthesizer.SetOutputToDefaultAudioDevice();
if ( Settings.SelectedVoice != string.Empty )
{
_voice_speechSynthesizer.SelectVoice( Settings.SelectedVoice );
}
_voice_speechSynthesizer.Rate = 1;
WriteLine( "...voice synthesizer has been created." );
UpdateVolume();
}
public void Say( string message, string? value = null, bool interrupt = false, bool alsoAddToChatQueue = true )
{
message ??= string.Empty;
if ( value != null )
{
if ( value == string.Empty )
{
return;
}
message = message.Replace( ":value:", value );
}
if ( message == string.Empty )
{
return;
}
if ( Settings.EnableSpeechSynthesizer && ( _voice_speechSynthesizer != null ) )
{
if ( interrupt )
{
_voice_speechSynthesizer.Pause();
_voice_speechSynthesizer.SpeakAsyncCancelAll();
_voice_speechSynthesizer.Resume();
}
_voice_speechSynthesizer.SpeakAsync( message );
}
if ( alsoAddToChatQueue )
{
Chat( message );
}
}
public void UpdateVolume()
{
if ( _voice_speechSynthesizer != null )
{
_voice_speechSynthesizer.Volume = Settings.SpeechSynthesizerVolume;
}
}
}
}