-
Notifications
You must be signed in to change notification settings - Fork 0
/
GoogleTextToAudio.cs
178 lines (145 loc) · 6.17 KB
/
GoogleTextToAudio.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
171
172
173
174
175
176
177
178
using System;
using System.Activities;
using System.ComponentModel;
using Google.Cloud.TextToSpeech.V1;
using System.IO;
namespace Google_Text_To_Audio_Activity
{
public class Text_To_Audio_File : CodeActivity
{
public string EnvironmentVariableName = "GOOGLE_APPLICATION_CREDENTIALS";
public SsmlVoiceGender _voiceGender;
[Category("Input")]
[Description("The gender of the voice")]
public SsmlVoiceGender VoiceGender { get => _voiceGender; set => _voiceGender = value; }
[Category("Input")]
[Description("en-US, fr-FR, de-DE, ja-JP")]
[RequiredArgument]
public InArgument<string> LanguageCode { get; set; }
/// input credential file path
[Category("Input")]
[Description("the file path of the JSON file that contains your service account key")]
[RequiredArgument]
public InArgument<string> ServiceAccountFile { get; set; }
/// input text
[Category("Input")]
[Description("text to convert")]
[RequiredArgument]
public InArgument<string> Text { get; set; }
// Select the type of audio file you want returned.
[Category("Input")]
[Description("the type of audio file you want returned")]
public AudioEncoding AudioEncoder { get; set; }
[Category("Input")]
[Description("file name.file extension")]
[RequiredArgument]
public InArgument<string> AudioFilePath { get; set; }
protected override void Execute(CodeActivityContext context)
{
/// set credential file path to GOOGLE_APPLICATION_CREDENTIALS environment variable.
string path = @ServiceAccountFile.Get(context);
Environment.SetEnvironmentVariable(EnvironmentVariableName, path);
// Instantiate a client
var client = TextToSpeechClient.Create();
// Set the text input to be synthesized.
var input = new SynthesisInput
{
//Text = "text to convert in audio file"
Text = Text.Get(context),
};
// Build the voice request
var voice = new VoiceSelectionParams
{
LanguageCode = LanguageCode.Get(context),
SsmlGender = VoiceGender,
};
var config = new AudioConfig
{
AudioEncoding = AudioEncoder,
};
// Perform the Text-to-Speech request, passing the text input
// with the selected voice parameters and audio file type
var response = client.SynthesizeSpeech(new SynthesizeSpeechRequest
{
Input = input,
Voice = voice,
AudioConfig = config
});
// Write the binary AudioContent of the response to an audio file.
using (Stream output = File.Create(AudioFilePath.Get(context)))
{
response.AudioContent.WriteTo(output);
}
}
}
/// <summary>
/// *********************************************************************************************
/// </summary>
public class SSML_To_Audio_File : CodeActivity
{
public string EnvironmentVariableName = "GOOGLE_APPLICATION_CREDENTIALS";
public SsmlVoiceGender _voiceGender;
[Category("Input")]
[Description("The gender of the voice")]
public SsmlVoiceGender VoiceGender { get => _voiceGender; set => _voiceGender = value; }
[Category("Input")]
[Description("en-US, fr-FR, de-DE, ja-JP")]
[RequiredArgument]
public InArgument<string> LanguageCode { get; set; }
/// input credential file path
[Category("Input")]
[Description("the file path of the JSON file that contains your service account key")]
[RequiredArgument]
public InArgument<string> ServiceAccountFile { get; set; }
/// input text
[Category("Input")]
[Description("SSML text to convert")]
[RequiredArgument]
public InArgument<string> SSML { get; set; }
// Select the type of audio file you want returned.
[Category("Input")]
[Description("the type of audio file you want returned")]
public AudioEncoding AudioEncoder { get; set; }
[Category("Input")]
[Description("file name.file extension")]
[RequiredArgument]
public InArgument<string> AudioFilePath { get; set; }
protected override void Execute(CodeActivityContext context)
{
/// set credential file path to GOOGLE_APPLICATION_CREDENTIALS environment variable.
string path = @ServiceAccountFile.Get(context);
Environment.SetEnvironmentVariable(EnvironmentVariableName, path);
// Instantiate a client
var client = TextToSpeechClient.Create();
// Set the text input to be synthesized.
var input = new SynthesisInput
{
//Text = "text to convert in audio file"
Ssml = SSML.Get(context),
};
// Build the voice request
var voice = new VoiceSelectionParams
{
LanguageCode = LanguageCode.Get(context),
SsmlGender = VoiceGender,
};
var config = new AudioConfig
{
AudioEncoding = AudioEncoder,
};
// Perform the Text-to-Speech request, passing the text input
// with the selected voice parameters and audio file type
var response = client.SynthesizeSpeech(new SynthesizeSpeechRequest
{
Input = input,
Voice = voice,
AudioConfig = config
});
// Write the binary AudioContent of the response to an audio file.
using (Stream output = File.Create(AudioFilePath.Get(context)))
{
response.AudioContent.WriteTo(output);
}
}
}
}