private static async Task <SpeechSynthesisResult> SynthesizeOnceAsyncInternal(string key, string region, string endpointId = null, string voiceName = null, string text = "Hello World!") { SpeechSynthesisResult synthesisResult = null; var config = SpeechConfig.FromSubscription(key, region); if (!string.IsNullOrEmpty(endpointId)) { config.EndpointId = endpointId; } if (!string.IsNullOrEmpty(voiceName)) { config.SpeechSynthesisVoiceName = voiceName; } // Creates a speech synthesizer using the default speaker as audio output. using (var synthesizer = new SpeechSynthesizer(config)) { synthesisResult = await synthesizer.SpeakTextAsync(text).ConfigureAwait(false); } return(synthesisResult); }
/// <summary> /// Based on the Azure SDK example, ensure that speech synthesis was successful /// </summary> /// <param name="result"></param> private bool SpeechWasSynthesized(SpeechSynthesisResult result) { if (result.Reason == ResultReason.SynthesizingAudioCompleted) { return(true); } if (result.Reason == ResultReason.Canceled) { var cancellation = SpeechSynthesisCancellationDetails.FromResult(result); DebugLogger.LogMessage(LogLevel.Error, Constants.ApplicationName, $"AzureSpeechModule: CANCELED: Reason={cancellation.Reason}", DateTime.Now); if (cancellation.Reason == CancellationReason.Error) { DebugLogger.LogMessage(LogLevel.Error, Constants.ApplicationName, $"AzureSpeechModule: CANCELED: ErrorCode={cancellation.ErrorCode}", DateTime.Now); DebugLogger.LogMessage(LogLevel.Error, Constants.ApplicationName, $"AzureSpeechModule: CANCELED: ErrorDetails=[{cancellation.ErrorDetails}]", DateTime.Now); DebugLogger.LogMessage(LogLevel.Error, Constants.ApplicationName, "AzureSpeechModule: CANCELED: Did you update the subscription info?", DateTime.Now); } } return(false); }
// Speech synthesis with backup subscription region. public static async Task SynthesizeOnceToSpeakerAsyncSwitchSecondaryRegion() { // Create a speech resource with primary subscription key and service region. // Also create a speech resource with secondary subscription key and service region SpeechSynthesisResult synthesisResult = await SynthesizeOnceAsyncInternal("PrimarySubscriptionKey", "PrimarySubscriptionRegion"); if (synthesisResult.Reason == ResultReason.Canceled) { SpeechSynthesisCancellationDetails details = SpeechSynthesisCancellationDetails.FromResult(synthesisResult); if (details.ErrorCode == CancellationErrorCode.ConnectionFailure || details.ErrorCode == CancellationErrorCode.ServiceUnavailable || details.ErrorCode == CancellationErrorCode.ServiceTimeout) { synthesisResult = await SynthesizeOnceAsyncInternal("SecondarySubscriptionKey", "SecondarySubscriptionRegion"); } } }
// Custom voice with backup // If custom voice service is unavailable, you can fallback to a standard platform voice. public static async Task SynthesizeOnceUseCustomVoiceToSpeakerAsyncSwitchPlatformVoice() { // Create a custom voice speech resource with subscription key, service region, endpoint ID and voice name. SpeechSynthesisResult synthesisResult = await SynthesizeOnceAsyncInternal("YourSubscriptionKey", "YourServiceRegion", "YourEndpointId", "YourCustomVoiceName"); if (synthesisResult.Reason == ResultReason.Canceled) { SpeechSynthesisCancellationDetails details = SpeechSynthesisCancellationDetails.FromResult(synthesisResult); if (details.ErrorCode == CancellationErrorCode.ConnectionFailure || details.ErrorCode == CancellationErrorCode.ServiceUnavailable || details.ErrorCode == CancellationErrorCode.ServiceTimeout || details.ErrorDetails.Contains("Error code: 1007")) { // Synthesize using a standard platform voice, e.g. en-US-JennyNeural synthesisResult = await SynthesizeOnceAsyncInternal("YourSubscriptionKey", "YourServiceRegion", null, "YourPlatformVoiceName"); } } }
private async void Play(SpeechSynthesisResult speech) { //var stream = new MemoryStream(speech.AudioData); //var mPlayer = new MediaPlayer(); //mPlayer.AudioCategory = MediaPlayerAudioCategory.Speech; //mPlayer.Source = MediaSource.CreateFromStream(stream.AsRandomAccessStream(), "audio/wave"); //mPlayer.Play(); using var audioStream = AudioDataStream.FromResult(speech); var filePath = Path.Combine(ApplicationData.Current.LocalFolder.Path, "outputaudio_for_playback.wav"); await audioStream.SaveToWaveFileAsync(filePath); var mediaPlayer = new MediaPlayer(); //mediaPlayer.MediaEnded += (sender, args) => { // var file = StorageFile.GetFileFromPathAsync(filePath).GetResults(); // file.DeleteAsync(); //}; mediaPlayer.Source = MediaSource.CreateFromStorageFile(await StorageFile.GetFileFromPathAsync(filePath)); mediaPlayer.Play(); }
// Custom voice with backup. // If custom voice service is unavailable, you can fallback to custom voice service in another region. public static async Task SynthesizeOnceUseCustomVoiceToSpeakerAsyncSwitchSecondaryRegion() { // Create a custom voice resource with primary subscription key and service region. // Also create a speech resource with secondary subscription key and service region. // Copy Custom Voice model from primary region to secondary region and deploy. SpeechSynthesisResult synthesisResult = await SynthesizeOnceAsyncInternal("PrimarySubscriptionKey", "PrimarySubscriptionRegion", "YourEndpointIdOnPrimaryRegion", "YourCustomVoiceName"); if (synthesisResult.Reason == ResultReason.Canceled) { SpeechSynthesisCancellationDetails details = SpeechSynthesisCancellationDetails.FromResult(synthesisResult); if (details.ErrorCode == CancellationErrorCode.ConnectionFailure || details.ErrorCode == CancellationErrorCode.ServiceUnavailable || details.ErrorCode == CancellationErrorCode.ServiceTimeout || details.ErrorDetails.Contains("Error code: 1007")) { // Synthesize using same custom voice model in secondary region. synthesisResult = await SynthesizeOnceAsyncInternal("SecondarySubscriptionKey", "SecondarySubscriptionRegion", "YourEndpointIdOnSecondaryRegion", "YourCustomVoiceName"); } } }
public static async Task SpeakAsync(string txt, QuestToSpeech.Voice voice, string filePath, AzureAPIConfig config) { SpeechConfig speechConfig = SpeechConfig.FromSubscription(config.Key, config.Region); speechConfig.SpeechSynthesisVoiceName = voice.Name; speechConfig.SpeechSynthesisLanguage = voice.LangCode; using (AudioConfig fileOutput = AudioConfig.FromWavFileOutput(filePath)) { using (SpeechSynthesizer tts = new SpeechSynthesizer(speechConfig, fileOutput)) { using (SpeechSynthesisResult result = await tts.SpeakTextAsync(txt)) { if (result.Reason == ResultReason.Canceled) { var cancellation = SpeechSynthesisCancellationDetails.FromResult(result); if (cancellation.Reason == CancellationReason.Error) { throw new Exception(string.Format("API Error (Code: {0}): {1}", cancellation.ErrorCode, cancellation.ErrorDetails)); } } } } } }