コード例 #1
0
        private void SpeakButton_Click(object sender, EventArgs e)
        {
            StatusTextBox.Text = "";
            var promptBuilder = new System.Speech.Synthesis.PromptBuilder();

            promptBuilder.AppendText(MessageTextBox.Text);
            var client = new Voices.AmazonPolly(
                SettingsManager.Options.AmazonAccessKey,
                SettingsManager.Options.AmazonSecretKey,
                SettingsManager.Options.AmazonRegionSystemName
                );
            var voice = (Voice)((InstalledVoiceEx)VoicesComboBox.SelectedItem).Voice;

            Amazon.Polly.Engine engine = null;
            if (voice.SupportedEngines.Contains(Amazon.Polly.Engine.Neural))
            {
                engine = Amazon.Polly.Engine.Neural;
            }
            var buffer = client.SynthesizeSpeech(voice.Id, promptBuilder.ToXml(), null, engine);
            var result = client.LastException == null ? "Success" : client.LastException.Message;

            StatusTextBox.Text = string.Format("{0:HH:mm:ss}: {1}", DateTime.Now, result);
            var item = Global.DecodeToPlayItem(buffer);

            Global.playlist.Add(item);
        }
コード例 #2
0
        /// <summary>
        /// Convert XML to WAV bytes. WAV won't have the header, so you have to add it separately.
        /// </summary>
        static void ConvertXmlToWav(PlayItem item)
        {
            var query = System.Web.HttpUtility.ParseQueryString(item.VoiceSourceKeys ?? "");
            // Default is local.
            var source = VoiceSource.Local;

            Enum.TryParse(query[InstalledVoiceEx._KeySource], true, out source);
            var voiceId = query[InstalledVoiceEx._KeyVoiceId];

            byte[] wavBytes;
            if (source == VoiceSource.Amazon)
            {
                var region = query[InstalledVoiceEx._KeyRegion];
                var engine = query[InstalledVoiceEx._KeyEngine];
                var client = new Voices.AmazonPolly(
                    SettingsManager.Options.AmazonAccessKey,
                    SettingsManager.Options.AmazonSecretKey,
                    region
                    );
                wavBytes = client.SynthesizeSpeech(voiceId, item.Xml, Amazon.Polly.OutputFormat.Mp3, engine);
                if (wavBytes != null && wavBytes.Length > 0)
                {
                    var pi = DecodeToPlayItem(wavBytes);
                    item.WavHead  = pi.WavHead;
                    item.WavData  = pi.WavData;
                    item.Duration = pi.Duration;
                    pi.Dispose();
                }
            }
            else
            {
                wavBytes = ConvertSsmlXmlToWav(voiceId, item.Xml, item.WavHead);
                if (wavBytes != null && wavBytes.Length > 0)
                {
                    item.WavData  = wavBytes;
                    item.Duration = AudioHelper.GetDuration(wavBytes.Length, item.WavHead.SampleRate, item.WavHead.BitsPerSample, item.WavHead.Channels);
                }
            }
        }