9
9
using System . Threading . Tasks ;
10
10
using UnityEngine ;
11
11
12
- namespace SpeechMod . Unity ;
12
+ namespace SpeechMod . Unity . Voices ;
13
13
14
14
public class EdgeVoiceUnity : MonoBehaviour
15
15
{
16
16
private const string TOKEN = "6A5AA1D4EAFF4E9FB37E23D68491D6F4" ;
17
- private int _playIndex ;
18
- private List < Task < EdgeVoiceClient > > _voiceTasks = new ( ) ;
19
17
private static EdgeVoiceUnity s_Instance ;
18
+ public int PlayIndex ;
19
+ public List < Task < EdgeVoiceClient > > VoiceTasks = new ( ) ;
20
+ public CancellationToken CancellationToken ;
20
21
21
22
private EdgeVoiceUnity ( )
22
23
{
@@ -41,57 +42,56 @@ public static string[] SetAvailableVoices()
41
42
42
43
public static bool IsSpeaking ( )
43
44
{
44
- if ( s_Instance ? . _voiceTasks == null || s_Instance . _voiceTasks . Count == 0 )
45
+ if ( s_Instance ? . VoiceTasks == null || s_Instance . VoiceTasks . Count == 0 )
45
46
return false ;
46
47
47
- if ( s_Instance . _playIndex < 0 || s_Instance . _playIndex >= s_Instance . _voiceTasks . Count )
48
+ if ( s_Instance . PlayIndex < 0 || s_Instance . PlayIndex >= s_Instance . VoiceTasks . Count )
48
49
return false ;
49
50
50
- if ( s_Instance . _playIndex + 1 < s_Instance . _voiceTasks . Count )
51
+ if ( s_Instance . PlayIndex + 1 < s_Instance . VoiceTasks . Count )
51
52
return true ;
52
53
53
- var edgeVoiceClient = s_Instance . _voiceTasks [ s_Instance . _playIndex ] ? . Result ;
54
+ var edgeVoiceClient = s_Instance . VoiceTasks [ s_Instance . PlayIndex ] ? . Result ;
54
55
return edgeVoiceClient is { IsSpeaking : true } ;
55
56
}
56
57
57
58
public static string GetStatusMessage ( )
58
59
{
59
- if ( s_Instance ? . _voiceTasks == null )
60
- return "Idle" ;
60
+ if ( s_Instance ? . VoiceTasks == null )
61
+ return EdgeVoiceClient . EdgeVoiceClientState . Ready . ToString ( ) ;
61
62
62
- if ( s_Instance . _playIndex < 0 || s_Instance . _playIndex >= s_Instance . _voiceTasks . Count )
63
- {
64
- s_Instance . _playIndex = 0 ;
65
- return "Idle" ;
66
- }
63
+ if ( s_Instance . PlayIndex < 0 || s_Instance . PlayIndex >= s_Instance . VoiceTasks . Count )
64
+ return EdgeVoiceClient . EdgeVoiceClientState . Ready . ToString ( ) ;
67
65
68
- var edgeVoiceClient = s_Instance . _voiceTasks [ s_Instance . _playIndex ] ? . Result ;
66
+ var edgeVoiceClient = s_Instance . VoiceTasks [ s_Instance . PlayIndex ] ? . Result ;
69
67
if ( edgeVoiceClient != null )
70
68
return edgeVoiceClient . CurrentState . ToString ( ) ;
71
69
72
- return " Error" ;
70
+ return EdgeVoiceClient . EdgeVoiceClientState . Error . ToString ( ) ;
73
71
}
74
72
75
73
public static void Speak ( EdgeVoiceDto edgeVoiceDTO )
76
74
{
77
- var cancellationToken = new CancellationToken ( ) ;
78
- SpeakInternal ( edgeVoiceDTO , cancellationToken ) ;
75
+ s_Instance . CancellationToken = new CancellationToken ( ) ;
76
+ SpeakInternal ( edgeVoiceDTO ) ;
79
77
}
80
78
81
79
public static void SpeakMulti ( EdgeVoiceDto [ ] edgeVoiceDTOs )
82
80
{
83
- var cancellationToken = new CancellationToken ( ) ;
84
- SpeakMultiInternal ( edgeVoiceDTOs , cancellationToken ) ;
81
+ s_Instance . CancellationToken = new CancellationToken ( ) ;
82
+ SpeakMultiInternal ( edgeVoiceDTOs ) ;
85
83
}
86
84
87
85
public static void Stop ( )
88
86
{
89
87
if ( ! IsSpeaking ( ) )
90
88
return ;
91
89
92
- var edgeVoiceClient = s_Instance ? . _voiceTasks ? [ s_Instance . _playIndex ] ? . Result ;
90
+ s_Instance ? . StopCoroutine ( PlayMultipleCoroutine ( ) ) ;
91
+
92
+ var edgeVoiceClient = s_Instance ? . VoiceTasks ? [ s_Instance . PlayIndex ] ? . Result ;
93
93
edgeVoiceClient ? . Stop ( ) ;
94
- s_Instance . _playIndex = 0 ;
94
+ s_Instance . PlayIndex = 0 ;
95
95
}
96
96
97
97
void OnDestroy ( )
@@ -124,39 +124,40 @@ private static async Task<string[]> SetAvailableVoicesInternal()
124
124
return Main . EdgeAvailableVoices ? . Select ( x => x . ShortName ) . ToArray ( ) ;
125
125
}
126
126
127
- private static async Task SpeakInternal ( EdgeVoiceDto edgeVoiceDTO , CancellationToken cancellationToken )
127
+ private static async Task SpeakInternal ( EdgeVoiceDto edgeVoiceDTO )
128
128
{
129
- Debug . Log ( $ "SpeakInternal: '{ edgeVoiceDTO . Voice } ' - R: { edgeVoiceDTO . Rate } , P: { edgeVoiceDTO . Pitch } , V: { edgeVoiceDTO . Volume } ") ;
129
+ #if DEBUG
130
+ Debug . Log ( $ "SpeakInternal: { edgeVoiceDTO . Text } ") ;
131
+ #endif
130
132
131
133
Stop ( ) ;
132
134
Reset ( ) ;
133
135
134
- s_Instance . _voiceTasks . Add ( new Task < EdgeVoiceClient > ( ( ) =>
136
+ s_Instance . VoiceTasks . Add ( new Task < EdgeVoiceClient > ( ( ) =>
135
137
{
136
138
var edgeVoice = new EdgeVoiceClient ( ) ;
137
139
edgeVoice . Load ( edgeVoiceDTO , TOKEN ) ;
138
140
return edgeVoice ;
139
141
} ) ) ;
140
142
141
- var single = s_Instance . _voiceTasks . First ( ) ;
143
+ var single = s_Instance . VoiceTasks . First ( ) ;
144
+
145
+ //s_Instance.StartCoroutine(SpeakingCoroutine());
142
146
143
147
single . Start ( ) ;
144
148
await single ;
145
149
146
- if ( cancellationToken . IsCancellationRequested )
150
+ if ( s_Instance . CancellationToken . IsCancellationRequested )
147
151
return ;
148
152
149
- Debug . Log ( "Play" ) ;
150
153
single . Result ? . Play ( ) ;
151
154
}
152
155
153
- private static async Task SpeakMultiInternal ( EdgeVoiceDto [ ] edgeVoiceDTOs , CancellationToken cancellationToken )
156
+ private static async Task SpeakMultiInternal ( EdgeVoiceDto [ ] edgeVoiceDTOs )
154
157
{
155
- foreach ( var edgeVoiceDto in edgeVoiceDTOs )
156
- {
157
- Debug . Log ( $ "Speak: '{ edgeVoiceDto . Text } ' '{ edgeVoiceDto . Voice } '") ;
158
- }
159
-
158
+ #if DEBUG
159
+ Debug . Log ( $ "SpeakMultiInternal: { edgeVoiceDTOs . Length } ") ;
160
+ #endif
160
161
Stop ( ) ;
161
162
Reset ( ) ;
162
163
@@ -165,60 +166,67 @@ private static async Task SpeakMultiInternal(EdgeVoiceDto[] edgeVoiceDTOs, Cance
165
166
if ( string . IsNullOrWhiteSpace ( edgeVoiceDTO . Text ) )
166
167
continue ;
167
168
168
- s_Instance . _voiceTasks ? . Add ( new Task < EdgeVoiceClient > ( ( ) =>
169
+ s_Instance . VoiceTasks ? . Add ( new Task < EdgeVoiceClient > ( ( ) =>
169
170
{
170
171
var edgeVoice = new EdgeVoiceClient ( ) ;
171
172
edgeVoice . Load ( edgeVoiceDTO , TOKEN ) ;
172
173
return edgeVoice ;
173
174
} ) ) ;
174
175
}
175
176
176
- foreach ( var task in s_Instance . _voiceTasks )
177
+ //s_Instance.StartCoroutine(SpeakingCoroutine());
178
+
179
+ foreach ( var task in s_Instance . VoiceTasks )
177
180
{
178
181
task . Start ( ) ;
179
182
}
180
183
181
- await s_Instance . _voiceTasks . First ( ) ;
184
+ await s_Instance . VoiceTasks . First ( ) ;
182
185
183
- if ( cancellationToken . IsCancellationRequested )
186
+ if ( s_Instance . CancellationToken . IsCancellationRequested )
184
187
return ;
185
188
186
- var voiceClient = s_Instance . _voiceTasks [ s_Instance . _playIndex ] ? . Result ;
189
+ var voiceClient = s_Instance . VoiceTasks [ s_Instance . PlayIndex ] ? . Result ;
187
190
if ( voiceClient != null )
188
191
{
189
- Debug . Log ( "Play first" ) ;
190
192
voiceClient . Play ( ) ;
191
193
192
- Debug . Log ( "Starting Coroutine" ) ;
193
- s_Instance . StartCoroutine ( PlayMultipleCoroutine ( cancellationToken ) ) ;
194
+ s_Instance . StartCoroutine ( PlayMultipleCoroutine ( ) ) ;
194
195
}
195
196
}
196
197
197
- private static IEnumerator PlayMultipleCoroutine ( CancellationToken cancellationToken )
198
+ private static IEnumerator PlayMultipleCoroutine ( )
198
199
{
199
- Debug . Log ( "Play Multiple Coroutine" ) ;
200
- while ( s_Instance . _playIndex < s_Instance . _voiceTasks . Count )
200
+ while ( s_Instance . PlayIndex < s_Instance . VoiceTasks . Count )
201
201
{
202
- if ( cancellationToken . IsCancellationRequested )
202
+ if ( s_Instance . CancellationToken . IsCancellationRequested )
203
203
break ;
204
204
205
- var edgeVoiceClient = s_Instance . _voiceTasks [ s_Instance . _playIndex ] . Result ;
205
+ var edgeVoiceClient = s_Instance . VoiceTasks [ s_Instance . PlayIndex ] . Result ;
206
206
if ( edgeVoiceClient is { IsSpeaking : true } )
207
207
yield return new WaitForSeconds ( 0.1f ) ;
208
208
else
209
209
{
210
- if ( ++ s_Instance . _playIndex >= s_Instance . _voiceTasks . Count )
210
+ if ( ++ s_Instance . PlayIndex >= s_Instance . VoiceTasks . Count )
211
211
break ;
212
- Debug . Log ( "Play next" ) ;
213
- s_Instance . _voiceTasks [ s_Instance . _playIndex ] ? . Result ? . Play ( ) ;
212
+
213
+ s_Instance . VoiceTasks [ s_Instance . PlayIndex ] ? . Result ? . Play ( ) ;
214
214
}
215
215
}
216
216
}
217
217
218
+ private static IEnumerator SpeakingCoroutine ( )
219
+ {
220
+ while ( IsSpeaking ( ) )
221
+ {
222
+ yield return new WaitForEndOfFrame ( ) ;
223
+ }
224
+ }
225
+
218
226
private static void Reset ( )
219
227
{
220
- s_Instance ? . StopAllCoroutines ( ) ;
221
- s_Instance . _playIndex = 0 ;
222
- s_Instance . _voiceTasks = new List < Task < EdgeVoiceClient > > ( ) ;
228
+ s_Instance ? . StopCoroutine ( PlayMultipleCoroutine ( ) ) ;
229
+ s_Instance . PlayIndex = 0 ;
230
+ s_Instance . VoiceTasks = new List < Task < EdgeVoiceClient > > ( ) ;
223
231
}
224
232
}
0 commit comments