Beispiel #1
0
        private async Task <string> SynthesisGetAvailableVoicesAsync()
        {
            try {
                string voices = "";

                var config = SpeechConfig.FromSubscription(subscriptionKey, region);

                using (var synthesizer = new Microsoft.CognitiveServices.Speech.SpeechSynthesizer(config, null)) {
                    using (var result = await synthesizer.GetVoicesAsync()) {
                        if (result.Reason == ResultReason.VoicesListRetrieved)
                        {
                            foreach (var voice in result.Voices)
                            {
                                if (voices.Length > 0)
                                {
                                    voices += "|";
                                }

                                voices += voice.ShortName + " (" + voice.Locale + ")";
                            }
                        }
                    }
                }

                return(voices);
            }
            catch (Exception e) {
                return("");
            }
        }
        private async void btnVoice_Click(object sender, EventArgs e)
        {
            SpeechConfig config = null;

            Microsoft.CognitiveServices.Speech.SpeechSynthesizer synthesizer = null;
            if (txtKey.Text == "" || txtRegion.Text == "")
            {
                MessageBox.Show("Both Key and Region fields are required.");
                return;
            }
            else
            {
                // Creates an instance of a speech config with specified subscription key and service region.
                config      = SpeechConfig.FromSubscription(txtKey.Text, txtRegion.Text);
                synthesizer = new Microsoft.CognitiveServices.Speech.SpeechSynthesizer(config);
            }
            using (var result = await synthesizer.GetVoicesAsync(""))
            {
                if (result.Reason == ResultReason.VoicesListRetrieved)
                {
                    // API key and region are valid. Open the form to select a voice, passing in the voices list and config object.
                    System.Collections.ObjectModel.ReadOnlyCollection <Microsoft.CognitiveServices.Speech.VoiceInfo> VoicesList = result.Voices;
                    frmSelectAzureVoice frm = new frmSelectAzureVoice(VoicesList, config);
                    frm.ShowDialog();
                    if (frm.DialogResult == DialogResult.OK)
                    {
                        if (frm.SelectedVoice != null)
                        {
                            Properties.Settings.Default.AzureVoice = frm.SelectedVoice;
                        }
                    }
                }
                else if (result.Reason == ResultReason.Canceled)
                {
                    MessageBox.Show($"Error retrieving voices list: {result.ErrorDetails}");
                }
            }
        }
Beispiel #3
0
        private bool SynthesizeAudio(string outputFile, bool isSsml, string text)
        {
            if (this.synthesizerType == "Windows")
            {
                using var synth = new System.Speech.Synthesis.SpeechSynthesizer();

                try {
                    synth.InjectOneCoreVoices();
                }
                catch {
                }

                synth.Rate   = rate;
                synth.Volume = volume;

                synth.SetOutputToWaveFile(outputFile);

                finished = false;
                failed   = false;

                synth.SpeakCompleted += OnSpeakCompleted;

                if (isSsml)
                {
                    synth.SpeakSsmlAsync(text);
                }
                else
                {
                    synth.SpeakAsync(text);
                }

                while (!finished)
                {
                    Thread.Sleep(100);
                }

                return(true);
            }
            else
            {
                RenewToken();

                using var audioConfig = AudioConfig.FromWavFileOutput(outputFile);
                using var synthesizer = new Microsoft.CognitiveServices.Speech.SpeechSynthesizer(config, audioConfig);

                finished = false;
                failed   = false;

                synthesizer.SynthesisCanceled  += OnSynthesisCanceled;
                synthesizer.SynthesisCompleted += OnSynthesisCompleted;

                if (isSsml)
                {
                    synthesizer.SpeakSsmlAsync(text);
                }
                else
                {
                    synthesizer.SpeakTextAsync(text);
                }

                while (!finished)
                {
                    Thread.Sleep(100);
                }

                return(!failed);
            }
        }