Text to Speech (Google)
TsgcTextToSpeechGoogle 使用 Google Cloud Text-to-Speech 合成语音。它会签名一个服务账户 JWT、请求音频、解码为文件,并通过您指定的音频播放器播放,因此丰富的语音和语言目录只有一个属性之遥。
TsgcTextToSpeechGoogle 使用 Google Cloud Text-to-Speech 合成语音。它会签名一个服务账户 JWT、请求音频、解码为文件,并通过您指定的音频播放器播放,因此丰富的语音和语言目录只有一个属性之遥。
一个 Google Cloud 语音后端。把服务账户 JSON 粘贴到选项中,选择语言、性别和语音,然后调用一个方法即可。
sgcAI 是自包含的。该组件随 sgcAI 一同交付,并附带它所基于的 sgcWebSockets Core 运行时,因此无需购买基础授权。
提供服务账户凭据,挑选一个语音,再关联一个音频播放器。
uses
sgcAI, sgcAI_TextToSpeech_Google, sgcAI_AudioPlayer_MCI;
var
oTTS: TsgcTextToSpeechGoogle;
begin
oTTS := TsgcTextToSpeechGoogle.Create(nil);
oTTS.AudioPlayer := sgcAudioPlayerMCI1;
// The service account JSON, pasted as-is.
oTTS.GoogleOptions.Settings.LoadFromFile('service-account.json');
oTTS.GoogleOptions.Language := 'en-US';
oTTS.GoogleOptions.VoiceId := 'en-US-Neural2-C';
oTTS.GoogleOptions.AudioEncoding := 'MP3';
oTTS.GoogleOptions.FileName := 'speech.mp3';
oTTS.TextToSpeech('The order has been shipped.');
end;
// includes: sgcAI.hpp, sgcAI_TextToSpeech_Google.hpp
TsgcTextToSpeechGoogle *oTTS = new TsgcTextToSpeechGoogle(NULL);
oTTS->AudioPlayer = sgcAudioPlayerMCI1;
oTTS->GoogleOptions->Settings->LoadFromFile("service-account.json");
oTTS->GoogleOptions->Language = "en-US";
oTTS->GoogleOptions->VoiceId = "en-US-Neural2-C";
oTTS->TextToSpeech("The order has been shipped.");
您最常使用的成员。
Settings 保存服务账户 JSON,Language 和 VoiceId 用于选择语音,未给出 id 时由 Gender 挑选默认语音,AudioEncoding 和 FileName 则控制合成后的音频。
AudioPlayer 负责播放合成出的文件。指定一个 TsgcAudioPlayerMCI,响应一到达就会开始播放。
该组件用服务账户中的私钥构建并签名一个 JWT,然后缓存访问令牌直到它过期,因此长时间运行的应用程序不必为每句话重新认证一次。
TextToSpeech(aText) 完成整个流程:请求、解码、写入、播放,并清理临时文件。
OnStart 和 OnStop 包裹整个操作,正是它们让聊天机器人和翻译器组件能在回复朗读完毕后恢复录音。
与系统语音组件和 Amazon 语音组件共用同一个接口,因此聊天机器人、翻译器和助手可以接受其中任意一个,无需改动代码。
该组件所用服务和协议的权威资料来源。