public void SelectVoiceByHints(VoiceGender gender, VoiceAge age, int voiceAlternate, CultureInfo culture) { Helpers.ThrowIfNull(culture, nameof(culture)); if (voiceAlternate < 0) { throw new ArgumentOutOfRangeException(nameof(voiceAlternate), SR.Get(SRID.PromptBuilderInvalidVariant)); } if (!VoiceInfo.ValidateGender(gender)) { throw new ArgumentException(SR.Get(SRID.EnumInvalid, "VoiceGender"), nameof(gender)); } if (!VoiceInfo.ValidateAge(age)) { throw new ArgumentException(SR.Get(SRID.EnumInvalid, "VoiceAge"), nameof(age)); } TTSVoice ttsVoice = VoiceSynthesizer.GetEngine(null, culture, gender, age, voiceAlternate, true); if (ttsVoice == null) { // No match - throw throw new InvalidOperationException(SR.Get(SRID.SynthesizerSetVoiceNoMatch)); } VoiceSynthesizer.Voice = ttsVoice; }
protected async Task <string> GetAmazonSynthSpeech(string text, TTSVoice voicePreference, TTSPitch pitchPreference, string filename = null) { AmazonSynthesizeSpeechRequest synthesisRequest = voicePreference.GetAmazonTTSSpeechRequest(); synthesisRequest.TextType = TextType.Ssml; synthesisRequest.Text = PrepareAmazonSSML(text, pitchPreference); // Perform the Text-to-Speech request, passing the text input // with the selected voice parameters and audio file type AmazonSynthesizeSpeechResponse synthesisResponse = await amazonClient.SynthesizeSpeechAsync(synthesisRequest); // Write the binary AudioContent of the response to file. string filepath; if (string.IsNullOrWhiteSpace(filename)) { filepath = Path.Combine(TTSFilesPath, $"{Guid.NewGuid()}.mp3"); } else { filepath = Path.Combine(TTSFilesPath, $"{filename}.mp3"); } using (Stream file = new FileStream(filepath, FileMode.Create)) { await synthesisResponse.AudioStream.CopyToAsync(file); await file.FlushAsync(); file.Close(); } return(filepath); }
/// <summary> /// Configures the voice. /// </summary> private static void ConfigureVoice() { SpWaveFormatEx fFormat = new SpWaveFormatEx(); ttSVoice = new TTSVoice(); //ttSVoice.SPVoice.Voice = ttSVoice.SPVoice.GetVoices("gender=female", "").Item(0); StartService("AudioSrv", 20 * 1000); /* set the audio out to the telephony device*/ ttSVoice.MMSysAudioOut.DeviceId = (int)w2cDrv.Device.Info.WaveOutDrvID; WriteLog("Sound DeviceId: " + w2cDrv.Device.Info.WaveOutDrvID.ToStr()); fFormat.FormatTag = 1; fFormat.Channels = 1; fFormat.SamplesPerSec = 8000; fFormat.AvgBytesPerSec = 16000; fFormat.BlockAlign = 2; fFormat.BitsPerSample = 16; ttSVoice.MMSysAudioOut.Format.SetWaveFormatEx(fFormat); /* Prevent SAPI from changing the wave format when the device changes*/ ttSVoice.SPVoice.AllowAudioOutputFormatChangesOnNextSet = false; ttSVoice.SPVoice.AudioOutputStream = ttSVoice.MMSysAudioOut; }
public static TTSVoice TranslateTTSVoice(this string voiceString) { if (ttsVoiceLookup == null) { ttsVoiceLookup = new Dictionary <string, TTSVoice>(); for (TTSVoice voice = 0; voice < TTSVoice.MAX; voice++) { ttsVoiceLookup.Add(voice.Serialize().ToLowerInvariant(), voice); } } string cleanedString = voiceString.Trim().ToLowerInvariant(); if (ttsVoiceLookup.ContainsKey(cleanedString)) { return(ttsVoiceLookup[cleanedString]); } if (cleanedString == "default") { return(TTSVoice.Unassigned); } return(TTSVoice.MAX); }
public void SelectVoice(string name) { Helpers.ThrowIfEmptyOrNull(name, "name"); TTSVoice engine = VoiceSynthesizer.GetEngine(name, CultureInfo.CurrentUICulture, VoiceGender.NotSet, VoiceAge.NotSet, 1, true); if (engine == null || name != engine.VoiceInfo.Name) { throw new ArgumentException(SR.Get(SRID.SynthesizerSetVoiceNoMatch)); } VoiceSynthesizer.Voice = engine; }
public static TTSService GetTTSService(this TTSVoice voice) { switch (voice) { case TTSVoice.en_AU_Standard_A: case TTSVoice.en_AU_Standard_B: case TTSVoice.en_AU_Standard_C: case TTSVoice.en_AU_Standard_D: case TTSVoice.en_IN_Standard_A: case TTSVoice.en_IN_Standard_B: case TTSVoice.en_IN_Standard_C: case TTSVoice.en_IN_Standard_D: case TTSVoice.en_GB_Standard_A: case TTSVoice.en_GB_Standard_B: case TTSVoice.en_GB_Standard_C: case TTSVoice.en_GB_Standard_D: case TTSVoice.en_GB_Standard_F: case TTSVoice.en_US_Standard_B: case TTSVoice.en_US_Standard_C: case TTSVoice.en_US_Standard_D: case TTSVoice.en_US_Standard_E: case TTSVoice.en_US_Standard_G: case TTSVoice.en_US_Standard_H: case TTSVoice.en_US_Standard_I: case TTSVoice.en_US_Standard_J: return(TTSService.Google); case TTSVoice.en_AU_Nicole: case TTSVoice.en_AU_Olivia: case TTSVoice.en_AU_Russell: case TTSVoice.en_GB_Amy: case TTSVoice.en_GB_Emma: case TTSVoice.en_GB_Brian: case TTSVoice.en_IN_Aditi: case TTSVoice.en_IN_Raveena: case TTSVoice.en_US_Ivy: case TTSVoice.en_US_Joanna: case TTSVoice.en_US_Kendra: case TTSVoice.en_US_Kimberly: case TTSVoice.en_US_Salli: case TTSVoice.en_US_Joey: case TTSVoice.en_US_Justin: case TTSVoice.en_US_Matthew: case TTSVoice.en_GB_WLS_Geraint: return(TTSService.Amazon); case TTSVoice.Unassigned: goto case TTSVoice.en_US_Joanna; default: BGC.Debug.LogError($"Unsupported TTSVoice {voice}"); goto case TTSVoice.en_US_Standard_B; } }
protected async Task <string> GetSynthSpeech(string text, TTSVoice voicePreference, TTSPitch pitchPreference) { switch (voicePreference.GetTTSService()) { case TTSService.Amazon: return(await GetAmazonSynthSpeech(text, voicePreference, pitchPreference)); case TTSService.Google: return(await GetGoogleSynthSpeech(text, voicePreference, pitchPreference)); default: communication.SendErrorMessage($"Unsupported TTSVoice for TTSService {voicePreference}"); goto case TTSService.Google; } }
protected async Task <string> GetGoogleSynthSpeech(string text, TTSVoice voicePreference, TTSPitch pitchPreference, string filename = null) { VoiceSelectionParams voice = voicePreference.GetGoogleVoiceSelectionParams(); AudioConfig config = new AudioConfig { AudioEncoding = AudioEncoding.Mp3, Pitch = pitchPreference.GetSemitoneShift() }; //TTS SynthesisInput input = new SynthesisInput { Ssml = PrepareGoogleSSML(text) }; // Perform the Text-to-Speech request, passing the text input // with the selected voice parameters and audio file type GoogleSynthesizeSpeechResponse response = await googleClient.SynthesizeSpeechAsync(input, voice, config); // Write the binary AudioContent of the response to file. string filepath; if (string.IsNullOrWhiteSpace(filename)) { filepath = Path.Combine(TTSFilesPath, $"{Guid.NewGuid()}.mp3"); } else { filepath = Path.Combine(TTSFilesPath, $"{filename}.mp3"); } using (Stream file = new FileStream(filepath, FileMode.Create)) { response.AudioContent.WriteTo(file); } return(filepath); }
public async Task <AudioRequest> TTSRequest(TTSVoice voicePreference, TTSPitch pitchPreference, Effect effectsChain, string[] splitTTSText) { if (splitTTSText.Any(x => x.Contains('/') || x.Contains('!'))) { List <AudioRequest> audioRequestSegments = new List <AudioRequest>(); //Complex parsing StringBuilder stringbuilder = new StringBuilder(); foreach (string ttsWord in splitTTSText) { if (ttsWord.Contains('/') || ttsWord.Contains('!')) { foreach (string ttsWordSegment in SplitStringByCommandRegex(ttsWord)) { if (ttsWordSegment.StartsWith('/')) { //Sound Effect SoundEffect soundEffect = soundEffectSystem.GetSoundEffectByAlias(ttsWordSegment); if (soundEffect is null) { //Unrecognized, append as is stringbuilder.Append(ttsWordSegment); } else { //Output current if (stringbuilder.Length > 0) { string filename = await GetSynthSpeech(stringbuilder.ToString(), voicePreference, pitchPreference); audioRequestSegments.Add(new AudioFileRequest(filename, effectsChain)); stringbuilder.Clear(); } audioRequestSegments.Add(new SoundEffectRequest(soundEffect)); } } else if (ttsWordSegment.StartsWith('!')) { //Command AudioRequest request = AudioRequest.ParseCommand(ttsWordSegment.ToLower()); if (request is null) { //Unrecognized, append as is stringbuilder.Append(ttsWordSegment); } else { //Output current if (stringbuilder.Length > 0) { string filename = await GetSynthSpeech(stringbuilder.ToString(), voicePreference, pitchPreference); audioRequestSegments.Add(new AudioFileRequest(filename, effectsChain)); stringbuilder.Clear(); } audioRequestSegments.Add(request); } } else { stringbuilder.Append(ttsWordSegment); } } if (stringbuilder.Length > 0) { stringbuilder.Append(' '); } } else { stringbuilder.Append(ttsWord); stringbuilder.Append(' '); } } if (stringbuilder.Length > 0) { string filename = await GetSynthSpeech(stringbuilder.ToString(), voicePreference, pitchPreference); audioRequestSegments.Add(new AudioFileRequest(filename, effectsChain)); stringbuilder.Clear(); } return(new ConcatenatedAudioRequest(audioRequestSegments)); } else { //Simple parsing string ttsSpeech = string.Join(' ', splitTTSText); string filename = await GetSynthSpeech(ttsSpeech, voicePreference, pitchPreference); return(new AudioFileRequest(filename, effectsChain)); } }
public Task <AudioRequest> TTSRequest(TTSVoice voicePreference, TTSPitch pitchPreference, Effect effectsChain, string ttsText) => TTSRequest(voicePreference, pitchPreference, effectsChain, ttsText.Split(' ', options: StringSplitOptions.RemoveEmptyEntries));
public static string Serialize(this TTSVoice voice) { switch (voice) { case TTSVoice.en_AU_Standard_A: return("en-AU-Standard-A"); case TTSVoice.en_AU_Standard_B: return("en-AU-Standard-B"); case TTSVoice.en_AU_Standard_C: return("en-AU-Standard-C"); case TTSVoice.en_AU_Standard_D: return("en-AU-Standard-D"); case TTSVoice.en_IN_Standard_A: return("en-IN-Standard-A"); case TTSVoice.en_IN_Standard_B: return("en-IN-Standard-B"); case TTSVoice.en_IN_Standard_C: return("en-IN-Standard-C"); case TTSVoice.en_IN_Standard_D: return("en-IN-Standard-D"); case TTSVoice.en_GB_Standard_A: return("en-GB-Standard-A"); case TTSVoice.en_GB_Standard_B: return("en-GB-Standard-B"); case TTSVoice.en_GB_Standard_C: return("en-GB-Standard-C"); case TTSVoice.en_GB_Standard_D: return("en-GB-Standard-D"); case TTSVoice.en_GB_Standard_F: return("en-GB-Standard-F"); case TTSVoice.en_US_Standard_B: return("en-US-Standard-B"); case TTSVoice.en_US_Standard_C: return("en-US-Standard-C"); case TTSVoice.en_US_Standard_D: return("en-US-Standard-D"); case TTSVoice.en_US_Standard_E: return("en-US-Standard-E"); case TTSVoice.en_US_Standard_G: return("en-US-Standard-G"); case TTSVoice.en_US_Standard_H: return("en-US-Standard-H"); case TTSVoice.en_US_Standard_I: return("en-US-Standard-I"); case TTSVoice.en_US_Standard_J: return("en-US-Standard-J"); case TTSVoice.en_AU_Nicole: return("Nicole"); case TTSVoice.en_AU_Olivia: return("Olivia"); case TTSVoice.en_AU_Russell: return("Russell"); case TTSVoice.en_GB_Amy: return("Amy"); case TTSVoice.en_GB_Emma: return("Emma"); case TTSVoice.en_GB_Brian: return("Brian"); case TTSVoice.en_IN_Aditi: return("Aditi"); case TTSVoice.en_IN_Raveena: return("Raveena"); case TTSVoice.en_US_Ivy: return("Ivy"); case TTSVoice.en_US_Joanna: return("Joanna"); case TTSVoice.en_US_Kendra: return("Kendra"); case TTSVoice.en_US_Kimberly: return("Kimberly"); case TTSVoice.en_US_Salli: return("Salli"); case TTSVoice.en_US_Joey: return("Joey"); case TTSVoice.en_US_Justin: return("Justin"); case TTSVoice.en_US_Matthew: return("Matthew"); case TTSVoice.en_GB_WLS_Geraint: return("Geraint"); case TTSVoice.Unassigned: return("Unassigned"); default: BGC.Debug.LogError($"Unsupported TTSVoice {voice}"); goto case TTSVoice.en_US_Standard_B; } }
public static Amazon.Polly.Model.SynthesizeSpeechRequest GetAmazonTTSSpeechRequest(this TTSVoice voice) { Amazon.Polly.Model.SynthesizeSpeechRequest synthesisRequest = new Amazon.Polly.Model.SynthesizeSpeechRequest { OutputFormat = OutputFormat.Mp3, Engine = Engine.Standard, LexiconNames = awsLexicons }; switch (voice) { case TTSVoice.en_AU_Standard_A: case TTSVoice.en_AU_Standard_B: case TTSVoice.en_AU_Standard_C: case TTSVoice.en_AU_Standard_D: case TTSVoice.en_IN_Standard_A: case TTSVoice.en_IN_Standard_B: case TTSVoice.en_IN_Standard_C: case TTSVoice.en_IN_Standard_D: case TTSVoice.en_GB_Standard_A: case TTSVoice.en_GB_Standard_B: case TTSVoice.en_GB_Standard_C: case TTSVoice.en_GB_Standard_D: case TTSVoice.en_GB_Standard_F: case TTSVoice.en_US_Standard_B: case TTSVoice.en_US_Standard_C: case TTSVoice.en_US_Standard_D: case TTSVoice.en_US_Standard_E: case TTSVoice.en_US_Standard_G: case TTSVoice.en_US_Standard_H: case TTSVoice.en_US_Standard_I: case TTSVoice.en_US_Standard_J: BGC.Debug.LogError($"Tried to get Amazon VoiceId from Google TTS Voice {voice}"); goto case TTSVoice.en_GB_Brian; case TTSVoice.en_AU_Nicole: synthesisRequest.VoiceId = VoiceId.Nicole; break; //Olivia is unsupported case TTSVoice.en_AU_Olivia: synthesisRequest.VoiceId = VoiceId.Nicole; break; case TTSVoice.en_AU_Russell: synthesisRequest.VoiceId = VoiceId.Russell; break; case TTSVoice.en_GB_Amy: synthesisRequest.VoiceId = VoiceId.Amy; break; case TTSVoice.en_GB_Emma: synthesisRequest.VoiceId = VoiceId.Emma; break; case TTSVoice.en_GB_Brian: synthesisRequest.VoiceId = VoiceId.Brian; break; case TTSVoice.en_IN_Aditi: synthesisRequest.VoiceId = VoiceId.Aditi; synthesisRequest.LanguageCode = LanguageCode.EnIN; break; case TTSVoice.en_IN_Raveena: synthesisRequest.VoiceId = VoiceId.Raveena; synthesisRequest.LanguageCode = LanguageCode.EnIN; break; case TTSVoice.en_US_Ivy: synthesisRequest.VoiceId = VoiceId.Ivy; break; case TTSVoice.en_US_Joanna: synthesisRequest.VoiceId = VoiceId.Joanna; break; case TTSVoice.en_US_Kendra: synthesisRequest.VoiceId = VoiceId.Kendra; break; case TTSVoice.en_US_Kimberly: synthesisRequest.VoiceId = VoiceId.Kimberly; break; case TTSVoice.en_US_Salli: synthesisRequest.VoiceId = VoiceId.Salli; break; case TTSVoice.en_US_Joey: synthesisRequest.VoiceId = VoiceId.Joey; break; case TTSVoice.en_US_Justin: synthesisRequest.VoiceId = VoiceId.Justin; break; case TTSVoice.en_US_Matthew: synthesisRequest.VoiceId = VoiceId.Matthew; break; case TTSVoice.en_GB_WLS_Geraint: synthesisRequest.VoiceId = VoiceId.Geraint; break; case TTSVoice.Unassigned: goto case TTSVoice.en_US_Joanna; default: BGC.Debug.LogError($"TTS Voice not supported {voice}"); goto case TTSVoice.Unassigned; } return(synthesisRequest); }
public static VoiceSelectionParams GetGoogleVoiceSelectionParams(this TTSVoice voice) { switch (voice) { case TTSVoice.Unassigned: return(new VoiceSelectionParams { LanguageCode = "en-US", SsmlGender = SsmlVoiceGender.Neutral }); case TTSVoice.en_AU_Standard_A: case TTSVoice.en_AU_Standard_B: case TTSVoice.en_AU_Standard_C: case TTSVoice.en_AU_Standard_D: return(new VoiceSelectionParams { Name = voice.GetGoogleTTSVoiceString(), LanguageCode = "en-AU", SsmlGender = SsmlVoiceGender.Neutral }); case TTSVoice.en_IN_Standard_A: case TTSVoice.en_IN_Standard_B: case TTSVoice.en_IN_Standard_C: case TTSVoice.en_IN_Standard_D: return(new VoiceSelectionParams { Name = voice.GetGoogleTTSVoiceString(), LanguageCode = "en-IN", SsmlGender = SsmlVoiceGender.Neutral }); case TTSVoice.en_GB_Standard_A: case TTSVoice.en_GB_Standard_B: case TTSVoice.en_GB_Standard_C: case TTSVoice.en_GB_Standard_D: case TTSVoice.en_GB_Standard_F: return(new VoiceSelectionParams { Name = voice.GetGoogleTTSVoiceString(), LanguageCode = "en-GB", SsmlGender = SsmlVoiceGender.Neutral }); case TTSVoice.en_US_Standard_B: case TTSVoice.en_US_Standard_C: case TTSVoice.en_US_Standard_D: case TTSVoice.en_US_Standard_E: case TTSVoice.en_US_Standard_G: case TTSVoice.en_US_Standard_H: case TTSVoice.en_US_Standard_I: case TTSVoice.en_US_Standard_J: return(new VoiceSelectionParams { Name = voice.GetGoogleTTSVoiceString(), LanguageCode = "en-US", SsmlGender = SsmlVoiceGender.Neutral }); case TTSVoice.en_AU_Nicole: case TTSVoice.en_AU_Olivia: case TTSVoice.en_AU_Russell: case TTSVoice.en_GB_Amy: case TTSVoice.en_GB_Emma: case TTSVoice.en_GB_Brian: case TTSVoice.en_IN_Aditi: case TTSVoice.en_IN_Raveena: case TTSVoice.en_US_Ivy: case TTSVoice.en_US_Joanna: case TTSVoice.en_US_Kendra: case TTSVoice.en_US_Kimberly: case TTSVoice.en_US_Salli: case TTSVoice.en_US_Joey: case TTSVoice.en_US_Justin: case TTSVoice.en_US_Matthew: case TTSVoice.en_GB_WLS_Geraint: BGC.Debug.LogError($"Tried to get Google VoiceSelectionParams from AWS TTS Voice {voice}"); goto case TTSVoice.Unassigned; default: BGC.Debug.LogError($"TTS Voice not supported {voice}"); goto case TTSVoice.Unassigned; } }
private static string GetGoogleTTSVoiceString(this TTSVoice voice) { switch (voice) { case TTSVoice.en_AU_Standard_A: return("en-AU-Standard-A"); case TTSVoice.en_AU_Standard_B: return("en-AU-Standard-B"); case TTSVoice.en_AU_Standard_C: return("en-AU-Standard-C"); case TTSVoice.en_AU_Standard_D: return("en-AU-Standard-D"); case TTSVoice.en_IN_Standard_A: return("en-IN-Standard-A"); case TTSVoice.en_IN_Standard_B: return("en-IN-Standard-B"); case TTSVoice.en_IN_Standard_C: return("en-IN-Standard-C"); case TTSVoice.en_IN_Standard_D: return("en-IN-Standard-D"); case TTSVoice.en_GB_Standard_A: return("en-GB-Standard-A"); case TTSVoice.en_GB_Standard_B: return("en-GB-Standard-B"); case TTSVoice.en_GB_Standard_C: return("en-GB-Standard-C"); case TTSVoice.en_GB_Standard_D: return("en-GB-Standard-D"); case TTSVoice.en_GB_Standard_F: return("en-GB-Standard-F"); case TTSVoice.en_US_Standard_B: return("en-US-Standard-B"); case TTSVoice.en_US_Standard_C: return("en-US-Standard-C"); case TTSVoice.en_US_Standard_D: return("en-US-Standard-D"); case TTSVoice.en_US_Standard_E: return("en-US-Standard-E"); case TTSVoice.en_US_Standard_G: return("en-US-Standard-G"); case TTSVoice.en_US_Standard_H: return("en-US-Standard-H"); case TTSVoice.en_US_Standard_I: return("en-US-Standard-I"); case TTSVoice.en_US_Standard_J: return("en-US-Standard-J"); case TTSVoice.en_AU_Nicole: case TTSVoice.en_AU_Olivia: case TTSVoice.en_AU_Russell: case TTSVoice.en_GB_Amy: case TTSVoice.en_GB_Emma: case TTSVoice.en_GB_Brian: case TTSVoice.en_IN_Aditi: case TTSVoice.en_IN_Raveena: case TTSVoice.en_US_Ivy: case TTSVoice.en_US_Joanna: case TTSVoice.en_US_Kendra: case TTSVoice.en_US_Kimberly: case TTSVoice.en_US_Salli: case TTSVoice.en_US_Joey: case TTSVoice.en_US_Justin: case TTSVoice.en_US_Matthew: case TTSVoice.en_GB_WLS_Geraint: BGC.Debug.LogError($"Tried to get Google Voice string from AWS TTS Voice {voice}"); goto case TTSVoice.en_US_Standard_B; case TTSVoice.Unassigned: goto case TTSVoice.en_US_Standard_B; default: BGC.Debug.LogError($"TTS Voice not supported {voice}"); goto case TTSVoice.en_US_Standard_B; } }