public async Task SynthesizeSpeechAsync()
        {
            Mock <TextToSpeech.TextToSpeechClient> mockGrpcClient = new Mock <TextToSpeech.TextToSpeechClient>(MockBehavior.Strict);
            SynthesizeSpeechRequest expectedRequest = new SynthesizeSpeechRequest
            {
                Input       = new SynthesisInput(),
                Voice       = new VoiceSelectionParams(),
                AudioConfig = new AudioConfig(),
            };
            SynthesizeSpeechResponse expectedResponse = new SynthesizeSpeechResponse
            {
                AudioContent = ByteString.CopyFromUtf8("16"),
            };

            mockGrpcClient.Setup(x => x.SynthesizeSpeechAsync(expectedRequest, It.IsAny <CallOptions>()))
            .Returns(new Grpc.Core.AsyncUnaryCall <SynthesizeSpeechResponse>(Task.FromResult(expectedResponse), null, null, null, null));
            TextToSpeechClient       client      = new TextToSpeechClientImpl(mockGrpcClient.Object, null);
            SynthesisInput           input       = new SynthesisInput();
            VoiceSelectionParams     voice       = new VoiceSelectionParams();
            AudioConfig              audioConfig = new AudioConfig();
            SynthesizeSpeechResponse response    = await client.SynthesizeSpeechAsync(input, voice, audioConfig);

            Assert.Same(expectedResponse, response);
            mockGrpcClient.VerifyAll();
        }
Esempio n. 2
0
        private Stream GetAudioStream(string message)
        {
            SynthesisInput input = new SynthesisInput
            {
                Text = message
            };

            VoiceSelectionParams voiceSelection = new VoiceSelectionParams
            {
                LanguageCode = "es-ES",
                Name         = "es-ES-Standard-A"
            };

            AudioConfig audioConfig = new AudioConfig
            {
                AudioEncoding = AudioEncoding.Linear16,
                SpeakingRate  = Configuration.BasicConfiguration.MessageSpeed
            };

            try
            {
                SynthesizeSpeechResponse response = Client.SynthesizeSpeech(input, voiceSelection, audioConfig);
                return(new MemoryStream(response.AudioContent.ToByteArray()));
            }
            catch (Exception ex)
            {
                TwitchBot.Instance.LogMessage(LogSeverity.Error, ex.Message, ex.StackTrace);
                return(null);
            }
        }
Esempio n. 3
0
        public static int Main(string[] args)
        {
            // Create client
            TextToSpeechClient client = TextToSpeechClient.Create();

            // Initialize request argument(s)
            SynthesisInput input = new SynthesisInput
            {
                Text = "test",
            };
            VoiceSelectionParams voice = new VoiceSelectionParams
            {
                LanguageCode = "en-US",
            };
            AudioConfig audioConfig = new AudioConfig
            {
                AudioEncoding = AudioEncoding.Mp3,
            };

            // Call API method
            SynthesizeSpeechResponse response = client.SynthesizeSpeech(input, voice, audioConfig);

            // Show the result
            Console.WriteLine(response);

            // Success
            Console.WriteLine("Smoke test passed OK");
            return(0);
        }
Esempio n. 4
0
        public async Task <string> Utter(string text, string dir, VoiceId voice)
        {
            string speaker = "Chatbot";

            ConsoleLogger.WriteLine(speaker, $"{text}");

            string fileName = (text + "-" + voice).GetMd5Hash() + ".mp3";

            string recordBasePath = $"wwwroot{Path.DirectorySeparatorChar}voice";
            string filePath       = $"{dir}{Path.DirectorySeparatorChar}{recordBasePath}{Path.DirectorySeparatorChar}{fileName}";

            if (File.Exists(filePath))
            {
                return($"/voice/{fileName}");
            }

            SynthesizeSpeechRequest sreq = new SynthesizeSpeechRequest();

            sreq.Text         = text;
            sreq.OutputFormat = OutputFormat.Mp3;
            sreq.VoiceId      = voice;
            SynthesizeSpeechResponse sres = await polly.SynthesizeSpeechAsync(sreq);

            using (FileStream fileStream = File.Create(filePath))
            {
                sres.AudioStream.CopyTo(fileStream);
                fileStream.Flush();
                fileStream.Close();
            }

            return($"/voice/{fileName}");
        }
Esempio n. 5
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);
        }
Esempio n. 6
0
        public static void SynthesizeSpeech()
        {
            AmazonPollyClient client         = new AmazonPollyClient();
            String            outputFileName = "speech.mp3";

            SynthesizeSpeechRequest synthesizeSpeechRequest = new SynthesizeSpeechRequest()
            {
                OutputFormat = OutputFormat.Mp3,
                VoiceId      = VoiceId.Joanna,
                Text         = "This is a sample text to be synthesized."
            };

            try
            {
                using (FileStream outputStream = new FileStream(outputFileName, FileMode.Create, FileAccess.Write))
                {
                    SynthesizeSpeechResponse synthesizeSpeechResponse = client.SynthesizeSpeech(synthesizeSpeechRequest);
                    byte[] buffer = new byte[2 * 1024];
                    int    readBytes;

                    Stream inputStream = synthesizeSpeechResponse.AudioStream;
                    while ((readBytes = inputStream.Read(buffer, 0, 2 * 1024)) > 0)
                    {
                        outputStream.Write(buffer, 0, readBytes);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception caught: " + e.Message);
            }
        }
Esempio n. 7
0
        public static void TestPolly2()
        {
            using (AmazonPollyClient pc = new AmazonPollyClient())
            {
                SynthesizeSpeechRequest sreq = new SynthesizeSpeechRequest();
                sreq.Text         = "Your Sample Text Here 123";
                sreq.OutputFormat = OutputFormat.Mp3;
                sreq.VoiceId      = VoiceId.Salli;
                SynthesizeSpeechResponse sres = pc.SynthesizeSpeechAsync(sreq).GetAwaiter().GetResult();

                using (var memoryStream = new MemoryStream())
                {
                    sres.AudioStream.CopyTo(memoryStream);
                    memoryStream.Flush();

                    memoryStream.Position = 0;

                    string outputFile = @".\TestAudio\output-from-polly.wav";

                    using (Mp3FileReader reader = new Mp3FileReader(memoryStream))
                    {
                        using (WaveStream pcmStream = WaveFormatConversionStream.CreatePcmStream(reader))
                        {
                            WaveFileWriter.CreateWaveFile(outputFile, pcmStream);
                        }
                    }
                }
            }
        }
Esempio n. 8
0
        bool SynthesizeSpeech(string filename, string line, string voice, List <string> lexiconNames)
        {
            try
            {
                SynthesizeSpeechRequest sreq = new SynthesizeSpeechRequest();
                sreq.Text         = line;
                sreq.OutputFormat = _fileFormat;
                sreq.VoiceId      = voice;

                if (lexiconNames != null)
                {
                    sreq.LexiconNames = lexiconNames;
                }

                if (checkBoxSSML.Checked)
                {
                    sreq.TextType = "SSML";
                }

                SynthesizeSpeechResponse sres = _pc.SynthesizeSpeech(sreq);

                using (var fileStream = File.Create(_outputFolder + @"\" + @filename))
                {
                    sres.AudioStream.CopyTo(fileStream);
                    fileStream.Flush();
                    fileStream.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error in: " + filename + "\n" + ex.Message, "Amazon Error");
                return(false);
            }
            return(true);
        }
Esempio n. 9
0
        public async stt::Task SynthesizeSpeechAsync()
        {
            moq::Mock <TextToSpeech.TextToSpeechClient> mockGrpcClient = new moq::Mock <TextToSpeech.TextToSpeechClient>(moq::MockBehavior.Strict);
            SynthesizeSpeechRequest request = new SynthesizeSpeechRequest
            {
                Input       = new SynthesisInput(),
                Voice       = new VoiceSelectionParams(),
                AudioConfig = new AudioConfig(),
            };
            SynthesizeSpeechResponse expectedResponse = new SynthesizeSpeechResponse
            {
                AudioContent = proto::ByteString.CopyFromUtf8("audio_content20992f23"),
                Timepoints   = { new Timepoint(), },
                AudioConfig  = new AudioConfig(),
            };

            mockGrpcClient.Setup(x => x.SynthesizeSpeechAsync(request, moq::It.IsAny <grpccore::CallOptions>())).Returns(new grpccore::AsyncUnaryCall <SynthesizeSpeechResponse>(stt::Task.FromResult(expectedResponse), null, null, null, null));
            TextToSpeechClient       client = new TextToSpeechClientImpl(mockGrpcClient.Object, null);
            SynthesizeSpeechResponse responseCallSettings = await client.SynthesizeSpeechAsync(request.Input, request.Voice, request.AudioConfig, gaxgrpc::CallSettings.FromCancellationToken(st::CancellationToken.None));

            xunit::Assert.Same(expectedResponse, responseCallSettings);
            SynthesizeSpeechResponse responseCancellationToken = await client.SynthesizeSpeechAsync(request.Input, request.Voice, request.AudioConfig, st::CancellationToken.None);

            xunit::Assert.Same(expectedResponse, responseCancellationToken);
            mockGrpcClient.VerifyAll();
        }
Esempio n. 10
0
        async public void getAudio(String text, String path)
        {
            AmazonPollyClient pc = new AmazonPollyClient(Amazon.RegionEndpoint.USEast1);

            SynthesizeSpeechRequest sreq = new SynthesizeSpeechRequest();

            sreq.Text         = text;
            sreq.OutputFormat = OutputFormat.Mp3;
            sreq.VoiceId      = VoiceId.Vitoria;
            sreq.SampleRate   = "8000";
            SynthesizeSpeechResponse sres = pc.SynthesizeSpeech(sreq);


            FileStream fileStream = File.Create(path);

            sres.AudioStream.CopyTo(fileStream);
            fileStream.Flush();
            fileStream.Close();
            FileStream fs = File.OpenRead(path);

            using (Mp3FileReader reader = new Mp3FileReader(fs))
            {
                var newFormat = new WaveFormat(8000, 16, 1);
                using (var converter = new WaveFormatConversionStream(newFormat, reader))
                {
                    var pathw = path + ".wav";
                    WaveFileWriter.CreateWaveFile(pathw, converter);
                }
            }
            fs.Dispose();
            File.Delete(path);
        }
Esempio n. 11
0
        public void SynthesizeSpeechRequestObject()
        {
            moq::Mock <TextToSpeech.TextToSpeechClient> mockGrpcClient = new moq::Mock <TextToSpeech.TextToSpeechClient>(moq::MockBehavior.Strict);
            SynthesizeSpeechRequest request = new SynthesizeSpeechRequest
            {
                Input              = new SynthesisInput(),
                Voice              = new VoiceSelectionParams(),
                AudioConfig        = new AudioConfig(),
                EnableTimePointing =
                {
                    SynthesizeSpeechRequest.Types.TimepointType.Unspecified,
                },
            };
            SynthesizeSpeechResponse expectedResponse = new SynthesizeSpeechResponse
            {
                AudioContent = proto::ByteString.CopyFromUtf8("audio_content20992f23"),
                Timepoints   = { new Timepoint(), },
                AudioConfig  = new AudioConfig(),
            };

            mockGrpcClient.Setup(x => x.SynthesizeSpeech(request, moq::It.IsAny <grpccore::CallOptions>())).Returns(expectedResponse);
            TextToSpeechClient       client   = new TextToSpeechClientImpl(mockGrpcClient.Object, null);
            SynthesizeSpeechResponse response = client.SynthesizeSpeech(request);

            xunit::Assert.Same(expectedResponse, response);
            mockGrpcClient.VerifyAll();
        }
        public void SimpleExample()
        {
            // Sample: SynthesizeSpeech
            // Additional: SynthesizeSpeech(SynthesisInput,VoiceSelectionParams,AudioConfig,CallSettings)
            TextToSpeechClient client = TextToSpeechClient.Create();
            // The input can be provided as text or SSML.
            SynthesisInput input = new SynthesisInput
            {
                Text = "This is a demonstration of the Google Cloud Text-to-Speech API"
            };
            // You can specify a particular voice, or ask the server to pick based
            // on specified criteria.
            VoiceSelectionParams voiceSelection = new VoiceSelectionParams
            {
                LanguageCode = "en-US",
                SsmlGender   = SsmlVoiceGender.Female
            };
            // The audio configuration determines the output format and speaking rate.
            AudioConfig audioConfig = new AudioConfig
            {
                AudioEncoding = AudioEncoding.Mp3
            };
            SynthesizeSpeechResponse response = client.SynthesizeSpeech(input, voiceSelection, audioConfig);

            using (Stream output = File.Create("sample.mp3"))
            {
                // response.AudioContent is a ByteString. This can easily be converted into
                // a byte array or written to a stream.
                response.AudioContent.WriteTo(output);
            }
            // End sample
        }
Esempio n. 13
0
        private void OnlineRequestPolly(SynthesizeSpeechRequest sreq, string name)
        {
            SynthesizeSpeechResponse sres = apc.SynthesizeSpeech(sreq);

            using (FileStream fileStream = File.Create("Assets/Resources/" + ResourcePathConstants.SpeechCacheFolder + name + ".mp3")) {
                sres.AudioStream.CopyTo(fileStream);
                fileStream.Flush();
                fileStream.Close();
                Debug.Log("New speech file created succesfully");
            }
        }
Esempio n. 14
0
        private static void t2sOut()
        {
            while (runSpeech)
            {
                if (currentText == lastText)
                {
                    continue;
                }


                if (currentText.StartsWith("+"))
                {
                    continue;
                }


                currentText = currentText.Replace('^', ' ').Replace(Environment.NewLine, " ").Replace("$s", "").Replace("$h", "").Replace("$g", "").Replace("$e", "").Replace("$u", "").Replace("$b", "").Replace("$8", "").Replace("$l", "").Replace("$q", "").Replace("$9", "").Replace("$a", "").Replace("$7", "").Replace("<", "").Replace("$r", "");

                /*
                 * if (Game1.player.isMarried())
                 *  currentText = currentText.Replace(" " + Game1.player.spouse + " ", " your spouse ").Replace(" " + Game1.player.spouse, " your spouse").Replace(Game1.player.spouse + " ", "Your spouse ");
                 *
                 * currentText.Replace(" " + Game1.player.Name + " ", " Farmer ").Replace(" " + Game1.player.Name, " Farmer").Replace(Game1.player.Name + " ", "Farmer ");
                 */

                int hash = currentText.GetHashCode();
                ensureFolderStructureExists(Path.Combine(Path.Combine(tmppath, speakerName), "speech.mp3"));
                string   file     = Path.Combine(Path.Combine(tmppath, speakerName), "speech" + hash + ".mp3");
                FileInfo fileInfo = new FileInfo(file);

                if (!fileInfo.Exists)
                {
                    SynthesizeSpeechRequest sreq = new SynthesizeSpeechRequest();
                    sreq.Text         = currentText;
                    sreq.OutputFormat = OutputFormat.Mp3;
                    sreq.VoiceId      = currentVoice;
                    SynthesizeSpeechResponse sres = pc.SynthesizeSpeech(sreq);

                    using (var fileStream = File.Create(file))
                    {
                        sres.AudioStream.CopyTo(fileStream);
                        fileStream.Flush();
                        fileStream.Close();
                    }
                }
                MediaPlayer.Stop();
                if (Game1.activeClickableMenu is DialogueBox || Game1.hudMessages.Count > 0 || speak)
                {
                    speak = false;
                    MediaPlayer.Play(Song.FromUri("speech" + hash, new System.Uri(Path.Combine(Path.Combine(Path.Combine("Content", "TTS"), speakerName), "speech" + hash + ".mp3"), System.UriKind.Relative)));
                }
                lastText = currentText;
            }
        }
 /// <summary>Snippet for SynthesizeSpeech</summary>
 public void SynthesizeSpeech()
 {
     // Snippet: SynthesizeSpeech(SynthesisInput, VoiceSelectionParams, AudioConfig, CallSettings)
     // Create client
     TextToSpeechClient textToSpeechClient = TextToSpeechClient.Create();
     // Initialize request argument(s)
     SynthesisInput       input       = new SynthesisInput();
     VoiceSelectionParams voice       = new VoiceSelectionParams();
     AudioConfig          audioConfig = new AudioConfig();
     // Make the request
     SynthesizeSpeechResponse response = textToSpeechClient.SynthesizeSpeech(input, voice, audioConfig);
     // End snippet
 }
Esempio n. 16
0
        public void Start(RichTextBox richText, string AccessKeyID, string SecretAccessKey, VoiceId voiceId)
        {
            BasicAWSCredentials awsCredentials =
                new BasicAWSCredentials(AccessKeyID, SecretAccessKey);
            //"AccessKeyID", "SecretAccessKey"

            AmazonPollyClient amazonPollyClient =
                new AmazonPollyClient(awsCredentials, RegionEndpoint.EUCentral1);

            SynthesizeSpeechRequest  synthesizeSpeechRequest  = MakeSynthesizeSpeechRequest(richText.Text, voiceId);
            SynthesizeSpeechResponse synthesizeSpeechResponse =
                amazonPollyClient.SynthesizeSpeech(synthesizeSpeechRequest);

            CreateMp3File(synthesizeSpeechResponse.AudioStream);
        }
Esempio n. 17
0
        private void button1_Click(object sender, EventArgs e)
        {
            string inpTemp = input.Text;

            string inpStr = xuly(inpTemp);

            long inp;
            bool success = Int64.TryParse(inpStr, out inp);


            if (viBtn.Checked == true)
            {
                String payload = ChuyenSoSangChuoi(inp);


                TextToSpeechClient client = TextToSpeechClient.Create();

                SynthesizeSpeechResponse response = client.SynthesizeSpeech(
                    new SynthesisInput()
                {
                    Text = payload
                },
                    new VoiceSelectionParams()
                {
                    LanguageCode = "vi-VN",
                    Name         = "vi-VN-Standard-A"
                },
                    new AudioConfig()
                {
                    AudioEncoding = AudioEncoding.Linear16
                }
                    );

                string speechFile = Path.Combine(Directory.GetCurrentDirectory(), "sample.wav");

                File.WriteAllBytes(speechFile, response.AudioContent.ToByteArray());


                System.Media.SoundPlayer player = new System.Media.SoundPlayer(speechFile);
                player.Play();
            }
            else
            {
                var synthesizer = new SpeechSynthesizer();
                synthesizer.SetOutputToDefaultAudioDevice();
                synthesizer.Speak(NumberToWords(inp));
            }
        }
 /// <summary>Snippet for SynthesizeSpeech</summary>
 public void SynthesizeSpeech_RequestObject()
 {
     // Snippet: SynthesizeSpeech(SynthesizeSpeechRequest, CallSettings)
     // Create client
     TextToSpeechClient textToSpeechClient = TextToSpeechClient.Create();
     // Initialize request argument(s)
     SynthesizeSpeechRequest request = new SynthesizeSpeechRequest
     {
         Input       = new SynthesisInput(),
         Voice       = new VoiceSelectionParams(),
         AudioConfig = new AudioConfig(),
     };
     // Make the request
     SynthesizeSpeechResponse response = textToSpeechClient.SynthesizeSpeech(request);
     // End snippet
 }
        /// <summary>
        /// Unmarshaller the response from the service to the response class.
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public override AmazonWebServiceResponse Unmarshall(JsonUnmarshallerContext context)
        {
            SynthesizeSpeechResponse response = new SynthesizeSpeechResponse();

            response.AudioStream = context.Stream;
            if (context.ResponseData.IsHeaderPresent("Content-Type"))
            {
                response.ContentType = context.ResponseData.GetHeaderValue("Content-Type");
            }
            if (context.ResponseData.IsHeaderPresent("x-amzn-RequestCharacters"))
            {
                response.RequestCharacters = int.Parse(context.ResponseData.GetHeaderValue("x-amzn-RequestCharacters"), CultureInfo.InvariantCulture);
            }

            return(response);
        }
        /// <summary>Snippet for SynthesizeSpeechAsync</summary>
        public async Task SynthesizeSpeechAsync()
        {
            // Snippet: SynthesizeSpeechAsync(SynthesisInput, VoiceSelectionParams, AudioConfig, CallSettings)
            // Additional: SynthesizeSpeechAsync(SynthesisInput, VoiceSelectionParams, AudioConfig, CancellationToken)
            // Create client
            TextToSpeechClient textToSpeechClient = await TextToSpeechClient.CreateAsync();

            // Initialize request argument(s)
            SynthesisInput       input       = new SynthesisInput();
            VoiceSelectionParams voice       = new VoiceSelectionParams();
            AudioConfig          audioConfig = new AudioConfig();
            // Make the request
            SynthesizeSpeechResponse response = await textToSpeechClient.SynthesizeSpeechAsync(input, voice, audioConfig);

            // End snippet
        }
        public static void runPolly(string inputText)   //TODO make polly config settings accessible from editor
        {
            SynthesizeSpeechRequest sreq = new SynthesizeSpeechRequest();

            sreq.Text         = inputText;
            sreq.OutputFormat = OutputFormat.Mp3;
            sreq.VoiceId      = VoiceId.Matthew;
            SynthesizeSpeechResponse sres = pc.SynthesizeSpeech(sreq);

            using (var fileStream = File.Create(@"Output.mp3"))

            {
                sres.AudioStream.CopyTo(fileStream);
                fileStream.Flush();
                fileStream.Close();
            }
        }
        public async static Task <string> PollyDemo(IAmazonPolly pollyClient, IAmazonS3 S3Client, string text)
        {
            string result = null;
            SynthesizeSpeechRequest synthesizeRequest = new SynthesizeSpeechRequest()
            {
                LanguageCode = LanguageCode.EnUS,
                OutputFormat = "mp3",
                SampleRate   = "8000",
                Text         = text,
                TextType     = "text",
                VoiceId      = "Joanna"
            };

            try
            {
                Task <SynthesizeSpeechResponse> synthesizeTask    = pollyClient.SynthesizeSpeechAsync(synthesizeRequest);
                SynthesizeSpeechResponse        syntheizeResponse = await synthesizeTask;

                Console.WriteLine(syntheizeResponse.ContentType);
                Console.WriteLine(syntheizeResponse.RequestCharacters);

                using (MemoryStream ms = new MemoryStream())
                {
                    syntheizeResponse.AudioStream.CopyTo(ms);
                    Console.WriteLine(ms.Length);

                    // Upload image to S3 bucket
                    string bucketName = "reinvent-indiamazones";
                    //string key = dto.text;
                    string key = "pollytest";
                    await Task.Run(() => S3Util.UploadToS3(S3Client, bucketName, key, ms));

                    // TODO : need to check the file exists in S3
                    result = S3Util.GetPresignedURL(S3Client, bucketName, key);
                }
                //syntheizeResponse.AudioStream.CopyTo(result);
                //result.Flush();
            }
            catch (AmazonPollyException pollyException)
            {
                Console.WriteLine(pollyException.Message, pollyException.InnerException);
            }

            return(result);
        }
Esempio n. 23
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);
        }
Esempio n. 24
0
        public static void TestPolly()
        {
            using (AmazonPollyClient pc = new AmazonPollyClient())
            {
                SynthesizeSpeechRequest sreq = new SynthesizeSpeechRequest();
                sreq.Text         = "Your Sample Text Here 123";
                sreq.OutputFormat = OutputFormat.Mp3;
                sreq.VoiceId      = VoiceId.Salli;
                SynthesizeSpeechResponse sres = pc.SynthesizeSpeechAsync(sreq).GetAwaiter().GetResult();

                using (var fileStream = File.Create(@".\TestAudio\output-from-polly.mp3"))
                {
                    sres.AudioStream.CopyTo(fileStream);
                    fileStream.Flush();
                    fileStream.Close();
                }
            }
        }
Esempio n. 25
0
        private void Speak(string text)
        {
            if (client == null)
            {
                return;
            }

            // Set the text input to be synthesized.
            SynthesisInput input = new SynthesisInput
            {
                Text = text
            };


            // Select the type of audio file you want returned.
            AudioConfig config = new AudioConfig
            {
                AudioEncoding = AudioEncoding.Mp3,
                // Pitch = -5,
                SpeakingRate = Rate
            };

            // Perform the Text-to-Speech request, passing the text input
            // with the selected voice parameters and audio file type
            SynthesizeSpeechResponse response = client.SynthesizeSpeech(new SynthesizeSpeechRequest
            {
                Input = input,
                Voice = new VoiceSelectionParams
                {
                    LanguageCode = languages[Settings.Default.languageIndex],
                    Name         = voiceTypes[Settings.Default.useWavenetVoices ? 0 : 1, Settings.Default.languageIndex, Settings.Default.ttsVoice]
                },
                AudioConfig = config
            });

            // Write the AudioContent of the response to an MP3 file.
            string s64      = response.AudioContent.ToBase64();
            string filePath = Path.Combine(Path.GetTempPath(), "ttsoutput64_" + DateTime.Now.ToFileTime() + ".mp3");

            File.WriteAllBytes(filePath, Convert.FromBase64String(s64));

            // play the file
            ttsQueue.Enqueue(filePath);
        }
Esempio n. 26
0
        /// <summary>
        /// Returns temporary file with MP3 for input text, synthetized in call to Amazon Polly.
        /// </summary>
        /// <param name="text">Text to become speech.</param>
        /// <param name="voice">Voice. Default is Joanna in English, US.</param>
        /// <returns></returns>
        static public string GetSpeech(string text, string voice = "")
        {
            SynthesizeSpeechRequest speechRequest = new SynthesizeSpeechRequest();

            speechRequest.Text         = text;
            speechRequest.OutputFormat = OutputFormat.Mp3;
            speechRequest.VoiceId      = VoiceIdFromString(voice);
            SynthesizeSpeechResponse speech = PollyClient.SynthesizeSpeech(speechRequest);

            string speechFilename = Path.Combine(Path.GetTempPath(), DateTime.Now.Ticks.ToString(CultureInfo.InvariantCulture) + "_" + Guid.NewGuid().ToString() + ".mp3");

            using (var fileStream = File.Create(speechFilename))
            {
                speech.AudioStream.CopyTo(fileStream);
                fileStream.Flush();
            }

            return(speechFilename);
        }
Esempio n. 27
0
    /// <summary>
    /// Google CloudからのレスポンスをAudioClipに書き出し,再生する
    /// </summary>
    /// <param name="response">Google Cloudからのレスポンス</param>
    private static void SetAudioClip(SynthesizeSpeechResponse response)
    {
        var bytes = response.AudioContent.ToByteArray();

        // byte[]をAudioClipで利用できる形に変換する
        var wav = new WAV(bytes);

        Debug.Log("Get Response: Elapsed time " + _stopwatch.ElapsedMilliseconds + "ms.\nData Length: " +
                  (wav.SampleCount * (1f / wav.Frequency) * 1000f).ToString("F0") + "ms.");
        _context.Post(_ =>
        {
            // AudioSourceに新しいAudioClipを貼り付ける
            audioSource.clip = AudioClip.Create("TextToSpeech", wav.SampleCount, 1, wav.Frequency, false);
            audioSource.clip.SetData(wav.LeftChannel, 0);

            // AudioClipを再生
            audioSource.Play();
        }, null);
    }
Esempio n. 28
0
        public async Task ProcessMessageAsync(SQSEvent.SQSMessage message)
        {
            //_logger.LogLine("Processing message \n" + message.Body);

            ConvertTextToSpeechContract contract =
                JsonConvert.DeserializeObject <ConvertTextToSpeechContract>(message.Body);

            DetectDominantLanguageResponse dominantLanguageResponse = AwsService.Comprehend
                                                                      .DetectDominantLanguage(contract.TextContent);

            string dominantLanguageCode = dominantLanguageResponse
                                          .Languages
                                          .OrderByDescending(l => l.Score)
                                          .First()
                                          .LanguageCode;

            Amazon.Polly.LanguageCode languageCode = null;

            switch (dominantLanguageCode)
            {
            case "en":
                languageCode = Amazon.Polly.LanguageCode.EnUS;
                break;

            //TODO: Handle other languages
            default:
                throw new Exception("Could not find the language specified");
            }

            SynthesizeSpeechResponse systhesisResponse = AwsService
                                                         .Polly.SynthesizeSpeech(contract.TextContent, languageCode);

            MemoryStream inputStream = new MemoryStream(ReadToEnd(systhesisResponse.AudioStream));

            string contentType = "audio/mpeg";
            string bucketName  = Environment.GetEnvironmentVariable("ALEXA_READER_BUCKET");

            AwsService.S3.PutObject(inputStream, contract.AudioFilePathToSave,
                                    bucketName, contentType);

            //_logger.LogLine("Processed message \n" + message.Body);
        }
Esempio n. 29
0
        private static async Task RecordFile()
        {
            AmazonPollyClient pc = new AmazonPollyClient(AmazonCredHelper.AwsCredintals, Amazon.RegionEndpoint.USEast2);

            SynthesizeSpeechRequest sreq = new SynthesizeSpeechRequest();

            sreq.Text         = textToVoice;
            sreq.OutputFormat = OutputFormat.Mp3;
            sreq.VoiceId      = VoiceId.Joey;
            SynthesizeSpeechResponse sres = await pc.SynthesizeSpeechAsync(sreq);

            var path = Path.Combine(dirpath, outputFilename + ".mp3");

            using (var fileStream = File.Create(path))
            {
                sres.AudioStream.CopyTo(fileStream);
                fileStream.Flush();
                fileStream.Close();
            }
        }
        public static void Main(string[] args)
        {
            AmazonPollyClient       client  = new AmazonPollyClient("yourID", "yourSecretKey", RegionEndpoint.USEast1);
            SynthesizeSpeechRequest request = new SynthesizeSpeechRequest();

            request.OutputFormat = OutputFormat.Mp3;
            request.Text         = "This is my first conversion";
            request.TextType     = TextType.Text;
            request.VoiceId      = VoiceId.Nicole;
            SynthesizeSpeechResponse response = client.SynthesizeSpeech(request);

            Console.WriteLine("ContentType: " + response.ContentType);
            Console.WriteLine("RequestCharacters: " + response.RequestCharacters);
            FileStream destination = File.Open(@"c:\temp\myfirstconversion.mp3", FileMode.Create);

            response.AudioStream.CopyTo(destination);
            Console.WriteLine("Destination length: {0}", destination.Length.ToString());
            destination.Close();
            Console.Read();
        }