/
Prompt.cs
170 lines (141 loc) · 4.51 KB
/
Prompt.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;
using System.IO;
using System.Speech.Internal;
namespace System.Speech.Synthesis
{
[DebuggerDisplay("{_text}")]
public class Prompt
{
#region Constructors
public Prompt(string textToSpeak)
: this(textToSpeak, SynthesisTextFormat.Text)
{
}
public Prompt(PromptBuilder promptBuilder)
{
Helpers.ThrowIfNull(promptBuilder, nameof(promptBuilder));
_text = promptBuilder.ToXml();
_media = SynthesisMediaType.Ssml;
}
// Disable parameter validation check for empty strings
public Prompt(string textToSpeak, SynthesisTextFormat media)
{
Helpers.ThrowIfNull(textToSpeak, nameof(textToSpeak));
switch (_media = (SynthesisMediaType)media)
{
case SynthesisMediaType.Text:
case SynthesisMediaType.Ssml:
_text = textToSpeak;
break;
default:
throw new ArgumentException(SR.Get(SRID.SynthesizerUnknownMediaType), nameof(media));
}
}
internal Prompt(Uri promptFile, SynthesisMediaType media)
{
Helpers.ThrowIfNull(promptFile, nameof(promptFile));
switch (_media = media)
{
case SynthesisMediaType.Text:
case SynthesisMediaType.Ssml:
string localPath;
string mimeType;
Uri baseUri;
using (Stream stream = s_resourceLoader.LoadFile(promptFile, out mimeType, out baseUri, out localPath))
{
try
{
using (var reader = new StreamReader(stream))
{
_text = reader.ReadToEnd();
}
}
finally
{
s_resourceLoader.UnloadFile(localPath);
}
}
break;
case SynthesisMediaType.WaveAudio:
_text = promptFile.ToString();
_audio = promptFile;
break;
default:
throw new ArgumentException(SR.Get(SRID.SynthesizerUnknownMediaType), nameof(media));
}
}
#endregion
#region public Properties
public bool IsCompleted
{
get
{
return _completed;
}
internal set
{
_completed = value;
}
}
internal object Synthesizer
{
set
{
if (value != null && (_synthesizer != null || _completed))
{
throw new ArgumentException(SR.Get(SRID.SynthesizerPromptInUse), nameof(value));
}
_synthesizer = value;
}
}
/// <summary>
/// What errors occurred during this operation?
/// </summary>
internal Exception Exception { get; set; }
#endregion
#region Internal Fields
/// <summary>
/// Could be some raw text or SSML doc or the file name (wave file)
/// </summary>
internal string _text;
/// <summary>
/// Audio data
/// </summary>
internal Uri _audio;
/// <summary>
/// Unused at this point
/// </summary>
internal SynthesisMediaType _media;
/// <summary>
/// Is this prompt played asynchronously
/// </summary>
internal bool _syncSpeak;
#endregion
#region Private Fields
/// <summary>
/// Is this SpeakToken canceled before it was completed?
/// </summary>
private bool _completed;
/// <summary>
/// The synthesizer this prompt is played on
/// </summary>
private object _synthesizer;
private static ResourceLoader s_resourceLoader = new();
#endregion
}
#region Public Enums
public enum SynthesisMediaType
{
Text = 0,
Ssml = 1,
WaveAudio
}
public enum SynthesisTextFormat
{
Text = 0,
Ssml = 1,
}
#endregion
}