-
Notifications
You must be signed in to change notification settings - Fork 166
Expand file tree
/
Copy pathWindowsTextToSpeechSubsystem.cs
More file actions
217 lines (190 loc) · 8.11 KB
/
Copy pathWindowsTextToSpeechSubsystem.cs
File metadata and controls
217 lines (190 loc) · 8.11 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Copyright (c) Mixed Reality Toolkit Contributors
// Licensed under the BSD 3-Clause
using MixedReality.Toolkit.Subsystems;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Scripting;
#if WINDOWS_UWP
using System;
using System.Linq;
using Windows.Media.SpeechSynthesis;
using Windows.Storage.Streams;
#endif // WINDOWS_UWP
namespace MixedReality.Toolkit.Speech.Windows
{
/// <summary>
/// A Unity subsystem that extends <see cref="MixedReality.Toolkit.Subsystems.TextToSpeechSubsystem">TextToSpeechSubsystem</see>
/// so to expose the text to speech services available on Windows platforms. This subsystem is enabled for Windows Standalone and
/// Universal Windows Applications.
/// </summary>
/// <remarks>
/// This subsystem can be configured using the <see cref="MixedReality.Toolkit.Speech.Windows.WindowsKeywordRecognitionSubsystemConfig">WindowsKeywordRecognitionSubsystemConfig</see> Unity asset.
/// </remarks>
[Preserve]
[MRTKSubsystem(
Name = "org.mixedrealitytoolkit.windowsspeech.texttospeech",
DisplayName = "Windows Text-To-Speech Subsystem",
Author = "Microsoft",
ProviderType = typeof(WindowsTextToSpeechSubsystemProvider),
SubsystemTypeOverride = typeof(WindowsTextToSpeechSubsystem),
ConfigType = typeof(WindowsTextToSpeechSubsystemConfig))]
public class WindowsTextToSpeechSubsystem : TextToSpeechSubsystem
{
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
static void Register()
{
// Fetch subsystem metadata from the attribute.
var cinfo = XRSubsystemHelpers.ConstructCinfo<WindowsTextToSpeechSubsystem, TextToSpeechSubsystemCinfo>();
if (!Register(cinfo))
{
Debug.LogError($"Failed to register the {cinfo.Name} subsystem.");
}
}
/// <summary>
/// A subsystem provider for <see cref="WindowsTextToSpeechSubsystem"/> that exposes methods on the Windows
/// speech synthesizer systems.
/// </summary>
[Preserve]
class WindowsTextToSpeechSubsystemProvider : Provider
{
private WindowsTextToSpeechSubsystemConfig config;
#if WINDOWS_UWP
private SpeechSynthesizer synthesizer;
private VoiceInformation voiceInfo;
#endif
public WindowsTextToSpeechSubsystemProvider() : base()
{ }
/// <inheritdoc/>
public override void Start()
{
config = XRSubsystemHelpers.GetConfiguration<WindowsTextToSpeechSubsystemConfig, WindowsTextToSpeechSubsystem>();
#if WINDOWS_UWP
synthesizer = new SpeechSynthesizer();
#endif
}
/// <inheritdoc/>
public override void Destroy()
{
#if WINDOWS_UWP
if (synthesizer != null)
{
synthesizer.Dispose();
synthesizer = null;
}
#endif // WINDOWS_UWP
}
#region ITextToSpeechSubsystem implementation
#if !(WINDOWS_UWP || ((UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN) && MSFT_TTS_WIN_PRESENT))
private bool haveLogged = false;
#endif
/// <inheritdoc/>
public override async Task<bool> TrySpeak(string phrase, AudioSource audioSource)
{
if (audioSource == null)
{
Debug.LogError("Must specify the AudioSource object on which the text to speech data should be applied.");
return false;
}
byte[] waveData = await Synthesize(phrase);
if (waveData == null)
{
return false;
}
// Convert from bytes to floats and create an AudioClip.
if (!TextToSpeechHelpers.TryConvertWaveData(
waveData,
out int samples,
out int sampleRate,
out int channels,
out float[] audioFloats))
{
Debug.LogError("Failed to convert speech audio format.");
return false;
}
audioSource.clip = TextToSpeechHelpers.CreateAudioClip(
"SynthesizedText",
audioFloats,
samples,
channels,
sampleRate);
audioSource.Play();
return true;
}
/// <summary>
/// Synthesizes the specified phrase.
/// </summary>
/// <param name="phrase">The phrase to be synthesized.</param>
/// <returns>The audio (wave) data upon successful synthesis, or null.</returns>
private async Task<byte[]> Synthesize(string phrase)
{
if (string.IsNullOrWhiteSpace(phrase))
{
Debug.LogWarning("Nothing to speak");
return null;
}
#if WINDOWS_UWP
// Change voice?
if (config.Voice != TextToSpeechVoice.Default)
{
// See if it's never been found or is changing
if ((voiceInfo == null) || (!voiceInfo.DisplayName.Contains(config.VoiceName)))
{
// Search for voice info
voiceInfo = SpeechSynthesizer.AllVoices.Where(v => v.DisplayName.Contains(config.VoiceName)).FirstOrDefault();
// If found, select
if (voiceInfo != null)
{
synthesizer.Voice = voiceInfo;
}
else
{
Debug.LogErrorFormat("TTS voice {0} could not be found.", config.VoiceName);
}
}
}
else
{
synthesizer.Voice = SpeechSynthesizer.DefaultVoice;
}
SpeechSynthesisStream synthStream = await synthesizer.SynthesizeTextToStreamAsync(phrase);
// Allocate a byte array to receive the wave data
byte[] waveData = new byte[(uint)synthStream.Size];
// Read the wave data.
using (IInputStream stream = synthStream.GetInputStreamAt(0))
{
// We can safely close the synthesis stream.
synthStream.Dispose();
using (DataReader reader = new DataReader(stream))
{
await reader.LoadAsync((uint)waveData.Length);
reader.ReadBytes(waveData);
}
}
return waveData;
#elif (UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN) && MSFT_TTS_WIN_PRESENT
return await Task.Run(() =>
{
if (!Microsoft.MixedReality.Toolkit.Speech.Windows.WinRTTextToSpeechPInvokes.TrySynthesizePhraseWithCustomVoice(phrase, config.VoiceName, out System.IntPtr nativeData, out int length))
{
Debug.LogError("Failed to synthesize the phrase");
return null;
}
byte[] waveData = new byte[length];
System.Runtime.InteropServices.Marshal.Copy(nativeData, waveData, 0, length);
// We can safely free the native data.
Microsoft.MixedReality.Toolkit.Speech.Windows.WinRTTextToSpeechPInvokes.FreeSynthesizedData(nativeData);
return waveData;
});
#else
if (!haveLogged)
{
Debug.LogError("The Windows Text-To-Speech subsystem is not supported on the current platform.");
haveLogged = true;
}
return await Task.FromResult<byte[]>(null);
#endif
}
#endregion TextToSpeechSubsystem implementation
}
}
}