我们正在尝试创建一个大学项目(统一的 TTS 门户),客户可以在其中选择各自的提供商(Google、Amazon Polly),并为各自的提供商提供访问密钥和密钥。
配置完成后,有一个 api /texttospeach,其中他们将仅传递配置 Id 和文本,服务需要将其转换为语音并返回响应。
这里的服务任务是
正如我们在下面的代码中看到的,我们正在为每个请求构建 TextToSpeechClient _client。这是一个好方法吗?请推荐。
public class GoogleCloudTextToSpeechVendorAsync : TTSVendor
{
private TextToSpeechClient _client;
private GoogleTTSConfig _googleTTSConfig;
public GoogleCloudTextToSpeechVendorAsync(GoogleTTSConfig googleTTSConfig)
{
this._googleTTSConfig = googleTTSConfig;
}
/// <summary>
///
/// </summary>
public async Task BuildAsync()
{
byte[] byteArray = Convert.FromBase64String(this._googleTTSConfig.Base64ServiceAccount);
var serviceAccount = Encoding.UTF8.GetString(byteArray);
serviceAccount = serviceAccount.Replace(@"\\", @"\");
GoogleCredential googleCred = GoogleCredential.FromJson(serviceAccount.ToString());
ChannelCredentials channelCreds = googleCred.ToChannelCredentials();
TextToSpeechClientBuilder clientBuilder = new TextToSpeechClientBuilder()
{
ChannelCredentials = channelCreds,
Endpoint = "texttospeech.googleapis.com"
};
_client = await clientBuilder.BuildAsync();
}
public async Task<byte[]> TrySpeak(string voiceName, string textToSpeak)
{
var input = new SynthesisInput();
input.Text = textToSpeak;
VoiceSelectionParams voiceSelection = new VoiceSelectionParams();
voiceSelection.Name = voiceName;
voiceSelection.SsmlGender = SsmlVoiceGender.Unspecified;
AudioConfig audioConfig = new AudioConfig();
audioConfig.SampleRateHertz = 8000;
audioConfig.AudioEncoding = AudioEncoding.Linear16;
var synthesizeSpeechResponse = await _client.SynthesizeSpeechAsync(input, voiceSelection, audioConfig);
return synthesizeSpeechResponse.AudioContent.ToByteArray();
}
}
不适用
正如我们在下面的代码中看到的,我们正在为每个请求构建 TextToSpeechClient _client。这是一个好方法吗?请推荐。
我相信如果要调用不同的提供商,那么你需要调用相应的提供商。可以通过应用Strategy模式来实现。它还增强了“关注点分离”。我的意思是一个班级只有一个目标。 假设我们有一些应该根据类型调用的各种提供者。
因此用户输入的代码如下所示:
public enum ProviderType
{
Google, Amazon, Polly
}
BaseProvider 的抽象类看起来像这样:
public abstract class BaseProvider
{
public DateTime StartDateTime { get; set; } = DateTime.Now;
public abstract string Build();
}
具体的实现是这样的:
public class GoogleProvider : BaseProvider
{
public override string Build() =>
"I am a GoogleProvider. Object created: " + StartDateTime.ToString();
}
public class AmazonProvider : BaseProvider
{
public override string Build() =>
"I am a AmazonProvider. Object created: " + StartDateTime.ToString();
}
然后
ProviderFactory
看起来像这样:
public class ProviderFactory
{
private Dictionary<ProviderType, BaseProvider> _ProviderByType;
public ProviderFactory()
{
_ProviderByType = new Dictionary<ProviderType, BaseProvider>()
{
{ ProviderType.Google, new GoogleProvider() },
{ ProviderType.Amazon, new AmazonProvider() }
};
}
public BaseProvider GetInstance(ProviderType providerType) =>
_ProviderByType[providerType];
}