public async Task <AudioClip> PreloadSpeech(string text, string voiceName, float rateChange, float pitchChange) { using (var audioData = await tts.GetDecodedAudioAsync(text, voiceName, rateChange, pitchChange)) { var reader = new BinaryReader(audioData.DataStream); var clip = AudioClip.Create( text, (int)audioData.Samples, audioData.Format.Channels, audioData.Format.SampleRate, true, floats => { for (var i = 0; i < floats.Length; ++i) { floats[i] = reader.ReadSingle(); } }, pos => { reader.BaseStream.Position = pos; }); clip.LoadAudioData(); return(clip); } }
private static async Task DecodeAudioAsync(string text, TextToSpeechClient ttsClient, Voice voice) { var audio = await ttsClient .GetDecodedAudioAsync(text, voice.ShortName) .ConfigureAwait(false); await PlayAsync(audio) .ConfigureAwait(false); }
private static async Task DecodeAudioAsync(string text, TextToSpeechClient ttsClient, Voice voice) { foreach (var outputFormat in TextToSpeechStreamClient.SupportedFormats) { Console.Write(outputFormat.Name); Console.Write(":> "); var audio = await ttsClient .GetDecodedAudioAsync(outputFormat, text, voice.ShortName) .ConfigureAwait(false); await PlayAsync(audio) .ConfigureAwait(false); } }