Пример #1
0
        public string GetVoices()
        {
            if (this.synthesizerType == "Azure")
            {
                RenewToken();

                return(SynthesisGetAvailableVoicesAsync().Result);
            }
            else
            {
                using var synth = new System.Speech.Synthesis.SpeechSynthesizer();

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

                string voices = "";

                foreach (var voice in synth.GetInstalledVoices())
                {
                    if (voices.Length > 0)
                    {
                        voices += "|";
                    }

                    voices += voice.VoiceInfo.Name + " (" + voice.VoiceInfo.Culture.Name + ")";
                }

                return(voices);
            }
        }
Пример #2
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);
            }
        }