private void listVoices_ItemChecked(object sender, ItemCheckedEventArgs e)
        {
            //QuestToSpeech.Voice s = Array.Find(tmpSelectedList.ToArray(), delegate (QuestToSpeech.Voice tv) { return (tv.Gender == v.Gender && tv.Name == v.Name && tv.LangCode == v.LangCode && tv.Module == v.Module); });

            if (drawn)
            {
                QuestToSpeech.Voice voice = new QuestToSpeech.Voice()
                {
                    Name     = e.Item.SubItems[0].Text,
                    Gender   = e.Item.SubItems[1].Text == "Female" ? QuestToSpeech.Gender.Female : QuestToSpeech.Gender.Male,
                    LangCode = e.Item.SubItems[3].Text,
                    Module   = QuestToSpeech.GetModule(e.Item.SubItems[2].Text)
                };

                if (e.Item.Checked)
                {
                    if (!tmpSelectedList.Exists(vx => QuestToSpeech.IsVoice(vx, voice)))
                    {
                        tmpSelectedList.Add(voice);
                    }
                }
                else
                {
                    if (tmpSelectedList.Exists(vx => QuestToSpeech.IsVoice(vx, voice)))
                    {
                        tmpSelectedList.Remove(tmpSelectedList.Find(vx => QuestToSpeech.IsVoice(vx, voice)));
                    }
                }
            }

            lblNumTotalSelected.Text = string.Format("(Total selected {0})", tmpSelectedList.Count);
        }
 public QuestToSpeechConfig()           // set defaults
 {
     EnabledModules = new QuestToSpeech.Module[] { QuestToSpeech.Module.Windows };
     SelectedVoices = new QuestToSpeech.Voice[0];
     AzureAPI       = null;
     AwsAPI         = null;
 }
        private void btnTestVoice_Click(object sender, EventArgs e)
        {
            if (listVoices.SelectedItems.Count > 0)
            {
                ListViewItem i = listVoices.SelectedItems[0];

                testString = Prompt.InputDialog(string.Format("Voice: {2}, {0} ({1})", i.SubItems[0].Text, i.SubItems[1].Text, i.SubItems[2].Text), "Enter a sentence to speak", testString == null ? "" : testString);

                if (!string.IsNullOrWhiteSpace(testString))
                {
                    QuestToSpeech.Voice v = new QuestToSpeech.Voice()
                    {
                        Name     = i.SubItems[0].Text,
                        Gender   = i.SubItems[1].Text == "Female" ? QuestToSpeech.Gender.Female : QuestToSpeech.Gender.Male,
                        LangCode = i.SubItems[3].Text,
                        Module   = QuestToSpeech.GetModule(i.SubItems[2].Text)
                    };

                    Thread t = new Thread(() => VoiceTestSpeak(testString, v));
                    t.Start();
                }
            }
            else
            {
                MessageBox.Show("No row selected");
            }
        }
Пример #4
0
        public static string Speak(string txt, QuestToSpeech.Voice voice, string filePath)
        {
            try {
                TextToSpeechClient client = TextToSpeechClient.Create();

                SynthesizeSpeechResponse res = client.SynthesizeSpeech(new SynthesizeSpeechRequest {
                    Input = new SynthesisInput {
                        Text = txt
                    },
                    Voice = new VoiceSelectionParams {
                        Name         = voice.Name,
                        LanguageCode = voice.LangCode,
                        SsmlGender   = voice.Gender == QuestToSpeech.Gender.Female ? SsmlVoiceGender.Female : SsmlVoiceGender.Male
                    },
                    AudioConfig = new AudioConfig {
                        AudioEncoding = AudioEncoding.Linear16
                    }
                });

                using (FileStream output = File.Create(filePath)) {
                    res.AudioContent.WriteTo(output);
                }
            } catch (Exception ex) {
                return(string.Format("GoogleTTS Exception: {0}", ex.InnerException == null ? ex.Message : ex.InnerException.ToString()));
            }

            return(null);
        }
Пример #5
0
        public static string Speak(string txt, QuestToSpeech.Voice voice, string filePath, AzureAPIConfig config)
        {
            try {
                SpeakAsync(txt, voice, filePath, config).Wait();
            } catch (Exception ex) {
                return(string.Format("AzureTTS Exception: ", ex.InnerException == null ? ex.Message : ex.InnerException.ToString()));
            }

            return(null);
        }
Пример #6
0
        public static string Speak(string txt, QuestToSpeech.Voice voice, string filePath)
        {
            try {
                using (SpeechSynthesizer tts = new SpeechSynthesizer()) {
                    tts.SelectVoice(voice.Name);
                    tts.SetOutputToWaveFile(filePath, new SpeechAudioFormatInfo(32000, AudioBitsPerSample.Sixteen, AudioChannel.Mono));
                    tts.Speak(txt);
                }
            } catch (Exception ex) {
                return(string.Format("WindowsTTS Exception: {0}", ex.InnerException == null ? ex.Message : ex.InnerException.ToString()));
            }

            return(null);
        }
Пример #7
0
        public static string Speak(string txt, QuestToSpeech.Voice voice, string filePath, AwsAPIConfig config)
        {
            try {
                using (AmazonPollyClient client = new AmazonPollyClient(config.AccessKey, config.SecretKey, Amazon.RegionEndpoint.GetBySystemName(config.Region))) {
                    string nameWoLang = voice.Name.Replace(voice.LangCode + "-", "");

                    SynthesizeSpeechRequest req = new SynthesizeSpeechRequest()
                    {
                        OutputFormat = OutputFormat.Mp3,
                        LanguageCode = voice.LangCode,
                        VoiceId      = (VoiceId)typeof(VoiceId).GetField(nameWoLang).GetValue(null),
                        Text         = txt
                    };

                    using (FileStream fileStream = new FileStream(filePath + ".mp3", FileMode.Create, FileAccess.Write)) {
                        SynthesizeSpeechResponse res = client.SynthesizeSpeech(req);
                        byte[] buffer = new byte[2 * 1024];
                        int    readBytes;

                        using (Stream audioStream = res.AudioStream) {
                            while ((readBytes = audioStream.Read(buffer, 0, 2 * 1024)) > 0)
                            {
                                fileStream.Write(buffer, 0, readBytes);
                            }
                        }
                    }

                    // convert to wav and delete mp3
                    if (File.Exists(filePath + ".mp3"))
                    {
                        using (Mp3FileReader mp3 = new Mp3FileReader(filePath + ".mp3")) {
                            using (WaveStream pcm = WaveFormatConversionStream.CreatePcmStream(mp3)) {
                                WaveFileWriter.CreateWaveFile(filePath, pcm);
                            }
                        }

                        File.Delete(filePath + ".mp3");
                    }
                }
            } catch (Exception ex) {
                return(ex.InnerException == null ? ex.Message : ex.InnerException.ToString());
            }

            return(null);
        }
        private void VoiceTestSpeak(string txt, QuestToSpeech.Voice voice)
        {
            string err   = null;
            string fPath = Path.Combine(QuestToSpeech.TempDirectory, string.Format("test-{0}.wav", Util.MD5Hash(testString + voice.Name + voice.Module)));

            if (!File.Exists(fPath))
            {
                if (voice.Module == QuestToSpeech.Module.Windows)
                {
                    err = WindowsTTS.Speak(testString, voice, fPath);
                }
                else if (voice.Module == QuestToSpeech.Module.Azure)
                {
                    err = AzureTTS.Speak(testString, voice, fPath, azureAPIConfig);
                }
                else if (voice.Module == QuestToSpeech.Module.Google)
                {
                    err = GoogleTTS.Speak(testString, voice, fPath);
                }
                else if (voice.Module == QuestToSpeech.Module.AWS)
                {
                    err = AwsTTS.Speak(testString, voice, fPath, awsAPIConfig);
                }
            }

            if (err != null)
            {
                MessageBox.Show(err);
                return;
            }

            if (File.Exists(fPath))
            {
                if (player != null)
                {
                    player.Stop();
                    player.Dispose();
                }

                player = new SoundPlayer(fPath);
                player.Play();
            }
        }
Пример #9
0
        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));
                            }
                        }
                    }
                }
            }
        }