protected async Task <string> GetAmazonSynthSpeech(string text, TTSVoice voicePreference, TTSPitch pitchPreference, string filename = null)
        {
            AmazonSynthesizeSpeechRequest synthesisRequest = voicePreference.GetAmazonTTSSpeechRequest();

            synthesisRequest.TextType = TextType.Ssml;
            synthesisRequest.Text     = PrepareAmazonSSML(text, pitchPreference);

            // Perform the Text-to-Speech request, passing the text input
            // with the selected voice parameters and audio file type
            AmazonSynthesizeSpeechResponse synthesisResponse = await amazonClient.SynthesizeSpeechAsync(synthesisRequest);

            // Write the binary AudioContent of the response to file.
            string filepath;

            if (string.IsNullOrWhiteSpace(filename))
            {
                filepath = Path.Combine(TTSFilesPath, $"{Guid.NewGuid()}.mp3");
            }
            else
            {
                filepath = Path.Combine(TTSFilesPath, $"{filename}.mp3");
            }


            using (Stream file = new FileStream(filepath, FileMode.Create))
            {
                await synthesisResponse.AudioStream.CopyToAsync(file);

                await file.FlushAsync();

                file.Close();
            }

            return(filepath);
        }
        public override void DownloadItem(PhraseItem item, string folder)
        {
            String SSMLText = String.Format(@"
<speak>
  {0}
</speak >
", item.Phrase.Replace("&", "&amp;"));

            try
            {
                //new Task(() =>
                //{
                Amazon.Polly.Model.SynthesizeSpeechRequest ssr = new Amazon.Polly.Model.SynthesizeSpeechRequest();

                ssr.TextType     = TextType.Ssml;
                ssr.Text         = SSMLText;
                ssr.VoiceId      = polly.DescribeVoices(new Amazon.Polly.Model.DescribeVoicesRequest()).Voices.Find(n => n.Name == SelectedVoice.Name).Id;
                ssr.OutputFormat = OutputFormat.Mp3;

                using (FileStream output = File.Create(String.Format("{0}\\mp3\\{1}\\{2}.mp3", folder, item.Folder, item.FileName)))
                {
                    polly.SynthesizeSpeech(ssr).AudioStream.CopyTo(output);
                }

                ConvertToWav(item, folder, false, new String[] { Name, SelectedVoice.Name, SelectedDiscreteSpeed, SelectedDiscreteVolume });

                //}).Start();
            }
            catch (Exception Ex)
            {
                Logger.Log(Ex.ToString());
                item.DownloadComplete = false;
            }
        }
        public override void Play(PhraseItem item)
        {
            if (!disabled)
            {
                Amazon.Polly.Model.SynthesizeSpeechRequest ssr = new Amazon.Polly.Model.SynthesizeSpeechRequest();

                ssr.Text         = item.Phrase;
                ssr.VoiceId      = polly.DescribeVoices(new Amazon.Polly.Model.DescribeVoicesRequest()).Voices.Find(n => n.Name == SelectedVoice.Name).Id;
                ssr.OutputFormat = OutputFormat.Mp3;
                byte[] mp3;

                using (MemoryStream output = new MemoryStream())
                {
                    polly.SynthesizeSpeech(ssr).AudioStream.CopyTo(output);
                    mp3 = output.ToArray();
                }

                MainWindow.PlayAudioStream(mp3);
            }
        }
Esempio n. 4
0
        static void Main(string[] args)
        {
            if (args.Count() == 0)
            {
                Console.WriteLine("Basic text to speach converter");
                Console.WriteLine("Expecting text in the command line parameter");
                Environment.Exit(0);
            }

            Console.WriteLine("Creating Amazon Polly client");
            Amazon.Polly.AmazonPollyClient             cl  = new Amazon.Polly.AmazonPollyClient(RegionEndpoint.USWest2);
            Amazon.Polly.Model.SynthesizeSpeechRequest req = new Amazon.Polly.Model.SynthesizeSpeechRequest();
            req.Text         = String.Join(" ", args);
            req.VoiceId      = Amazon.Polly.VoiceId.Salli;
            req.OutputFormat = Amazon.Polly.OutputFormat.Mp3;
            req.SampleRate   = "8000";
            req.TextType     = Amazon.Polly.TextType.Text;

            Console.WriteLine("Sending Amazon Polly request: " + req.Text);
            Amazon.Polly.Model.SynthesizeSpeechResponse resp = cl.SynthesizeSpeech(req);

            MemoryStream local_stream = new MemoryStream();

            resp.AudioStream.CopyTo(local_stream);
            local_stream.Position = 0;
            Console.WriteLine("Got mp3 stream, lenght: " + local_stream.Length.ToString());

            NAudio.Wave.Mp3FileReader             reader      = new NAudio.Wave.Mp3FileReader(local_stream);
            NAudio.Wave.WaveStream                wave_stream = NAudio.Wave.WaveFormatConversionStream.CreatePcmStream(reader);
            NAudio.Wave.BlockAlignReductionStream ba_stream   = new NAudio.Wave.BlockAlignReductionStream(wave_stream);
            NAudio.Wave.WaveOut wout = new NAudio.Wave.WaveOut();

            Console.Write("Playing stream...");
            wout.Init(ba_stream);
            wout.Play();
            while (wout.PlaybackState == NAudio.Wave.PlaybackState.Playing)
            {
                Thread.Sleep(100);
            }
            Console.WriteLine("..Done");
        }
Esempio n. 5
0
        private IEnumerator speak(Model.Wrapper wrapper, bool isNative)
        {
            //Debug.Log("Speak: " + wrapper);
            connectToAmazon();

            if (client == null)
            {
                Debug.LogWarning("'client' is null! Did you add the correct Cognito credentials?");
            }
            else
            {
                if (wrapper == null)
                {
                    Debug.LogWarning("'wrapper' is null!");
                }
                else
                {
                    if (string.IsNullOrEmpty(wrapper.Text))
                    {
                        Debug.LogWarning("'wrapper.Text' is null or empty!");
                    }
                    else
                    {
                        if (!Util.Helper.isInternetAvailable)
                        {
                            string errorMessage = "Internet is not available - can't use AWS Polly right now!";
                            Debug.LogError(errorMessage);
                            onErrorInfo(wrapper, errorMessage);
                        }
                        else
                        {
                            yield return(null); //return to the main process (uid)

                            string voiceCulture = getVoiceCulture(wrapper);
                            string voiceName    = getVoiceName(wrapper);

                            System.Text.StringBuilder sbXML = new System.Text.StringBuilder();

                            //SSML
                            sbXML.Append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
                            sbXML.Append("<speak version=\"1.0\" xmlns=\"http://www.w3.org/2001/10/synthesis\" xml:lang=\"");
                            sbXML.Append(voiceCulture);
                            sbXML.Append("\"");
                            //sbXML.Append (" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"");
                            //sbXML.Append (" xsi:schemaLocation=\"http://www.w3.org/2001/10/synthesis http://www.w3.org/TR/speech-synthesis/synthesis.xsd\"");
                            sbXML.Append(">");

                            sbXML.Append(prepareProsody(wrapper));

                            sbXML.Append("</speak>");

                            Amazon.Polly.Model.SynthesizeSpeechRequest synthesizeSpeechPresignRequest = new Amazon.Polly.Model.SynthesizeSpeechRequest();
                            synthesizeSpeechPresignRequest.Text         = sbXML.ToString();
                            synthesizeSpeechPresignRequest.TextType     = Amazon.Polly.TextType.Ssml;
                            synthesizeSpeechPresignRequest.VoiceId      = voiceName;
                            synthesizeSpeechPresignRequest.OutputFormat = Amazon.Polly.OutputFormat.Ogg_vorbis;

                            silence = false;

                            if (!isNative)
                            {
                                onSpeakAudioGenerationStart(wrapper);
                            }

                            string outputFile      = getOutputFile(wrapper.Uid);
                            bool   success         = false;
                            bool   requestFinished = false;

                            client.SynthesizeSpeechAsync(synthesizeSpeechPresignRequest, (responseObject) =>
                            {
                                if (responseObject.Exception == null)
                                {
                                    using (System.IO.FileStream fileStream = System.IO.File.Create(outputFile))
                                    {
                                        copyTo(responseObject.Response.AudioStream, fileStream);
                                    }

                                    success = true;
                                }
                                else
                                {
                                    string errorMessage = "Could not speak the text: " + wrapper + System.Environment.NewLine + "Error: " + responseObject.Exception;
                                    Debug.LogError(errorMessage);
                                    onErrorInfo(wrapper, errorMessage);
                                }

                                requestFinished = true;
                            });

                            do
                            {
                                yield return(null);
                            } while (!requestFinished);

                            if (success)
                            {
                                yield return(playAudioFile(wrapper, Util.Constants.PREFIX_FILE + outputFile, outputFile, AudioFileType, isNative));
                            }
                        }
                    }
                }
            }
        }
Esempio n. 6
0
        public static Amazon.Polly.Model.SynthesizeSpeechRequest GetAmazonTTSSpeechRequest(this TTSVoice voice)
        {
            Amazon.Polly.Model.SynthesizeSpeechRequest synthesisRequest = new Amazon.Polly.Model.SynthesizeSpeechRequest
            {
                OutputFormat = OutputFormat.Mp3,
                Engine       = Engine.Standard,
                LexiconNames = awsLexicons
            };

            switch (voice)
            {
            case TTSVoice.en_AU_Standard_A:
            case TTSVoice.en_AU_Standard_B:
            case TTSVoice.en_AU_Standard_C:
            case TTSVoice.en_AU_Standard_D:
            case TTSVoice.en_IN_Standard_A:
            case TTSVoice.en_IN_Standard_B:
            case TTSVoice.en_IN_Standard_C:
            case TTSVoice.en_IN_Standard_D:
            case TTSVoice.en_GB_Standard_A:
            case TTSVoice.en_GB_Standard_B:
            case TTSVoice.en_GB_Standard_C:
            case TTSVoice.en_GB_Standard_D:
            case TTSVoice.en_GB_Standard_F:
            case TTSVoice.en_US_Standard_B:
            case TTSVoice.en_US_Standard_C:
            case TTSVoice.en_US_Standard_D:
            case TTSVoice.en_US_Standard_E:
            case TTSVoice.en_US_Standard_G:
            case TTSVoice.en_US_Standard_H:
            case TTSVoice.en_US_Standard_I:
            case TTSVoice.en_US_Standard_J:
                BGC.Debug.LogError($"Tried to get Amazon VoiceId from Google TTS Voice {voice}");
                goto case TTSVoice.en_GB_Brian;

            case TTSVoice.en_AU_Nicole:
                synthesisRequest.VoiceId = VoiceId.Nicole;
                break;

            //Olivia is unsupported
            case TTSVoice.en_AU_Olivia:
                synthesisRequest.VoiceId = VoiceId.Nicole;
                break;

            case TTSVoice.en_AU_Russell:
                synthesisRequest.VoiceId = VoiceId.Russell;
                break;

            case TTSVoice.en_GB_Amy:
                synthesisRequest.VoiceId = VoiceId.Amy;
                break;

            case TTSVoice.en_GB_Emma:
                synthesisRequest.VoiceId = VoiceId.Emma;
                break;

            case TTSVoice.en_GB_Brian:
                synthesisRequest.VoiceId = VoiceId.Brian;
                break;

            case TTSVoice.en_IN_Aditi:
                synthesisRequest.VoiceId      = VoiceId.Aditi;
                synthesisRequest.LanguageCode = LanguageCode.EnIN;
                break;

            case TTSVoice.en_IN_Raveena:
                synthesisRequest.VoiceId      = VoiceId.Raveena;
                synthesisRequest.LanguageCode = LanguageCode.EnIN;
                break;

            case TTSVoice.en_US_Ivy:
                synthesisRequest.VoiceId = VoiceId.Ivy;
                break;

            case TTSVoice.en_US_Joanna:
                synthesisRequest.VoiceId = VoiceId.Joanna;
                break;

            case TTSVoice.en_US_Kendra:
                synthesisRequest.VoiceId = VoiceId.Kendra;
                break;

            case TTSVoice.en_US_Kimberly:
                synthesisRequest.VoiceId = VoiceId.Kimberly;
                break;

            case TTSVoice.en_US_Salli:
                synthesisRequest.VoiceId = VoiceId.Salli;
                break;

            case TTSVoice.en_US_Joey:
                synthesisRequest.VoiceId = VoiceId.Joey;
                break;

            case TTSVoice.en_US_Justin:
                synthesisRequest.VoiceId = VoiceId.Justin;
                break;

            case TTSVoice.en_US_Matthew:
                synthesisRequest.VoiceId = VoiceId.Matthew;
                break;

            case TTSVoice.en_GB_WLS_Geraint:
                synthesisRequest.VoiceId = VoiceId.Geraint;
                break;

            case TTSVoice.Unassigned: goto case TTSVoice.en_US_Joanna;

            default:
                BGC.Debug.LogError($"TTS Voice not supported {voice}");
                goto case TTSVoice.Unassigned;
            }

            return(synthesisRequest);
        }