11using System ;
2+ using System . Collections . Concurrent ;
3+ using System . Collections . Generic ;
24using System . IO ;
35using System . Threading . Tasks ;
46using PySpeechServiceClient ;
@@ -17,6 +19,7 @@ internal class PyTextToSpeechCommunicator : ICommunicator
1719 private bool _isEnabled ;
1820 private bool _isSpeaking ;
1921 private int volume ;
22+ private ConcurrentDictionary < string , SpeechRequest > _pendingRequests = [ ] ;
2023
2124 public PyTextToSpeechCommunicator ( IPySpeechService pySpeechService , TrackerOptionsAccessor trackerOptionsAccessor )
2225 {
@@ -42,13 +45,15 @@ public PyTextToSpeechCommunicator(IPySpeechService pySpeechService, TrackerOptio
4245
4346 _pySpeechService . SpeakCommandResponded += ( _ , args ) =>
4447 {
48+ _pendingRequests . TryGetValue ( args . Response . FullMessage , out var request ) ;
49+
4550 if ( args . Response . IsStartOfChunk )
4651 {
47- VisemeReached ? . Invoke ( this , new SpeakingUpdatedEventArgs ( true , null ) ) ;
52+ VisemeReached ? . Invoke ( this , new SpeakingUpdatedEventArgs ( true , request ) ) ;
4853 }
4954 else if ( args . Response . IsEndOfChunk )
5055 {
51- VisemeReached ? . Invoke ( this , new SpeakingUpdatedEventArgs ( false , null ) ) ;
56+ VisemeReached ? . Invoke ( this , new SpeakingUpdatedEventArgs ( false , request ) ) ;
5257 }
5358
5459 if ( args . Response . IsStartOfMessage )
@@ -60,6 +65,12 @@ public PyTextToSpeechCommunicator(IPySpeechService pySpeechService, TrackerOptio
6065 {
6166 SpeakCompleted ? . Invoke ( this , new SpeakCompletedEventArgs ( TimeSpan . FromSeconds ( 3 ) ) ) ;
6267
68+ if ( request != null )
69+ {
70+ _pendingRequests . TryRemove (
71+ new KeyValuePair < string , SpeechRequest > ( args . Response . FullMessage , request ) ) ;
72+ }
73+
6374 if ( ! args . Response . HasAnotherRequest )
6475 {
6576 _isSpeaking = false ;
@@ -109,6 +120,8 @@ public void Say(SpeechRequest request)
109120 {
110121 if ( ! _isEnabled || ! _pySpeechService . IsSpeechEnabled ) return ;
111122
123+ _pendingRequests . TryAdd ( request . Text , request ) ;
124+
112125 if ( request . Wait )
113126 {
114127 _pySpeechService . Speak ( request . Text , GetSpeechSettings ( ) ) ;
0 commit comments