public async Task StreamingSynthesize(string synthesizeInput, string audioName)
        {
            int sampleRate = 48000;
            var request    = new SynthesizeSpeechRequest
            {
                AudioConfig = new AudioConfig
                {
                    AudioEncoding   = Tinkoff.Cloud.Tts.V1.AudioEncoding.Linear16,
                    SampleRateHertz = sampleRate
                },
                Input = new SynthesisInput
                {
                    Text = synthesizeInput
                }
            };

            var stream = _clientTTS.StreamingSynthesize(request, GetMetadataTTS());

            var audioBuffer = new List <Byte[]>();

            while (await stream.ResponseStream.MoveNext())
            {
                audioBuffer.Add(stream.ResponseStream.Current.AudioChunk.ToByteArray());
            }

            var audioBytes = audioBuffer.SelectMany(byteArr => byteArr).ToArray();

            using (var writer = new WaveFileWriter(audioName, new WaveFormat(sampleRate, 1)))
            {
                writer.Write(audioBytes, 0, audioBytes.Length);
            }
        }
        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();
        }
Пример #3
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);
                        }
                    }
                }
            }
        }
Пример #4
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);
        }
Пример #5
0
        public byte[] SynthesizeSpeech(VoiceId voiceId, string text, OutputFormat format = null, Engine engine = null)
        {
            if (format == null)
            {
                format = OutputFormat.Mp3;
            }
            var request = new SynthesizeSpeechRequest();

            request.OutputFormat = format;
            request.VoiceId      = voiceId;
            // Prefer neural voices.
            if (engine != null)
            {
                request.Engine = engine;
            }
            request.Text     = text;
            request.TextType = TextType.Ssml;
            var ms         = new MemoryStream();
            var response   = Client.SynthesizeSpeech(request);
            var bufferSize = 2 * 1024;
            var buffer     = new byte[bufferSize];
            int readBytes;
            var inputStream = response.AudioStream;

            while ((readBytes = inputStream.Read(buffer, 0, bufferSize)) > 0)
            {
                ms.Write(buffer, 0, readBytes);
            }
            var bytes = ms.ToArray();

            ms.Dispose();
            return(bytes);
        }
Пример #6
0
        public void SynthesizeSpeechMarks()
        {
            var outputFileName          = "speechMarks.json";
            var synthesizeSpeechRequest = new SynthesizeSpeechRequest()
            {
                OutputFormat    = OutputFormat.Json,
                SpeechMarkTypes = new List <string>()
                {
                    SpeechMarkType.Viseme, SpeechMarkType.Word
                },
                VoiceId = VoiceId.Joanna,
                Text    = "This is a sample text to be synthesized."
            };

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

                    var 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);
            }
        }
Пример #7
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);
        }
Пример #8
0
        static async Task Main()
        {
            // クライアントを作成
            var region = RegionEndpoint.APNortheast1;
            var client = new AmazonPollyClient(region);

            // Amazon Polly を呼び出してテキストを音声に変換する
            var request = new SynthesizeSpeechRequest
            {
                VoiceId      = VoiceId.Mizuki,
                LanguageCode = LanguageCode.JaJP,
                OutputFormat = OutputFormat.Mp3,
                Text         = "こんにちは"
            };
            var resp = await client.SynthesizeSpeechAsync(request);

            // 取得した音声データをファイルに保存する
            const string fileName = "result.mp3";

            using (var stream = new MemoryStream())
            {
                await resp.AudioStream.CopyToAsync(stream);

                await File.WriteAllBytesAsync(fileName, stream.ToArray());
            }
        }
        private async Task GetTweetAudioAsync(string ssml, string voiceName, string languageCode, Stream destination)
        {
            string args    = "-hide_banner -loglevel panic -i pipe:0 -ac 2 -f s16le -ar 48000 pipe:1";
            var    request = new SynthesizeSpeechRequest
            {
                Input = new SynthesisInput
                {
                    Ssml = ssml
                },
                Voice = new VoiceSelectionParams
                {
                    LanguageCode = languageCode,
                    Name         = voiceName
                },
                AudioConfig = new AudioConfig
                {
                    AudioEncoding = AudioEncoding.OggOpus,
                }
            };

            var response = await _ttsClient.SynthesizeSpeechAsync(request);

            using MemoryStream audioContent = new MemoryStream();
            response.AudioContent.WriteTo(audioContent);

            audioContent.Seek(0, SeekOrigin.Begin);
            await(audioContent | Cli.Wrap("ffmpeg.exe").WithArguments(args) | destination).ExecuteAsync();
        }
Пример #10
0
        /// <summary>
        ///     Making Voice Using Poly Service
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public async Task <SynthesizeSpeechResponse> GetSynthesizeSpeechFromTextAsync(string text)
        {
            if (_pollyClient == null ||
                string.IsNullOrEmpty(text))
            {
                return(null);
            }

            var request = new SynthesizeSpeechRequest
            {
                Text         = $"<speak>{text}</speak>",
                OutputFormat = OutputFormat.Mp3,
                VoiceId      = VoiceId.Seoyeon,
                LanguageCode = "ko-KR",
                TextType     = TextType.Ssml
            };

            try
            {
                var response = await _pollyClient.SynthesizeSpeechAsync(request);

                return(response);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
Пример #11
0
        private void ConvertButton_Click(object sender, RoutedEventArgs e)
        {
            var client  = new AmazonPollyClient();
            var request = new SynthesizeSpeechRequest
            {
                Text         = MyTextBox.Text,
                OutputFormat = OutputFormat.Mp3,
                VoiceId      = VoiceId.Mia
            };

            var response = client.SynthesizeSpeech(request);

            var folder   = AppDomain.CurrentDomain.BaseDirectory;
            var filename = $"{folder}{Guid.NewGuid()}.mp3";

            using (var fs = File.Create(filename))
            {
                response.AudioStream.CopyTo(fs);
                fs.Flush();
                fs.Close();
            }

            var player = new MediaPlayer();

            player.Open(new Uri(filename, UriKind.Absolute));
            player.MediaEnded += (s2, e2) => Title = "Player ended";
            player.Play();
            Title = "Playing audio...";
        }
Пример #12
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();
        }
Пример #13
0
        internal virtual SynthesizeSpeechResponse SynthesizeSpeech(SynthesizeSpeechRequest request)
        {
            var marshaller   = SynthesizeSpeechRequestMarshaller.Instance;
            var unmarshaller = SynthesizeSpeechResponseUnmarshaller.Instance;

            return(Invoke <SynthesizeSpeechRequest, SynthesizeSpeechResponse>(request, marshaller, unmarshaller));
        }
Пример #14
0
        /// <summary>
        /// Synthesizes UTF-8 input, plain text or SSML, to a stream of bytes. SSML input must
        /// be valid, well-formed SSML. Some alphabets might not be available with all the voices
        /// (for example, Cyrillic might not be read at all by English voices) unless phoneme
        /// mapping is used. For more information, see <a href="http://docs.aws.amazon.com/polly/latest/dg/how-text-to-speech-works.html">How
        /// it Works</a>.
        /// </summary>
        /// <param name="request">Container for the necessary parameters to execute the SynthesizeSpeech service method.</param>
        ///
        /// <returns>The response from the SynthesizeSpeech service method, as returned by Polly.</returns>
        /// <exception cref="Amazon.Polly.Model.InvalidSampleRateException">
        /// The specified sample rate is not valid.
        /// </exception>
        /// <exception cref="Amazon.Polly.Model.InvalidSsmlException">
        /// The SSML you provided is invalid. Verify the SSML syntax, spelling of tags and values,
        /// and then try again.
        /// </exception>
        /// <exception cref="Amazon.Polly.Model.LexiconNotFoundException">
        /// Amazon Polly can't find the specified lexicon. This could be caused by a lexicon that
        /// is missing, its name is misspelled or specifying a lexicon that is in a different
        /// region.
        ///
        ///
        /// <para>
        /// Verify that the lexicon exists, is in the region (see <a>ListLexicons</a>) and that
        /// you spelled its name is spelled correctly. Then try again.
        /// </para>
        /// </exception>
        /// <exception cref="Amazon.Polly.Model.ServiceFailureException">
        /// An unknown condition has caused a service failure.
        /// </exception>
        /// <exception cref="Amazon.Polly.Model.TextLengthExceededException">
        /// The value of the "Text" parameter is longer than the accepted limits. The limit for
        /// input text is a maximum of 3000 characters total, of which no more than 1500 can be
        /// billed characters. SSML tags are not counted as billed characters.
        /// </exception>
        /// <seealso href="http://docs.aws.amazon.com/goto/WebAPI/polly-2016-06-10/SynthesizeSpeech">REST API Reference for SynthesizeSpeech Operation</seealso>
        public SynthesizeSpeechResponse SynthesizeSpeech(SynthesizeSpeechRequest request)
        {
            var marshaller   = new SynthesizeSpeechRequestMarshaller();
            var unmarshaller = SynthesizeSpeechResponseUnmarshaller.Instance;

            return(Invoke <SynthesizeSpeechRequest, SynthesizeSpeechResponse>(request, marshaller, unmarshaller));
        }
Пример #15
0
        public void TestLexiconNamesNull()
        {
            var request = new SynthesizeSpeechRequest();
            var url     = SynthesizeSpeechUtil.GeneratePresignedUrl(RegionEndpoint.USWest2, request);

            Assert.IsFalse(url.Contains("LexiconNames"));
        }
 /// <summary>
 /// Synthesizes speech synchronously: receive results after all text input
 /// has been processed.
 /// </summary>
 /// <param name="request">
 /// The request object containing all of the parameters for the API call.
 /// </param>
 /// <param name="callSettings">
 /// If not null, applies overrides to this RPC call.
 /// </param>
 /// <returns>
 /// The RPC response.
 /// </returns>
 public override SynthesizeSpeechResponse SynthesizeSpeech(
     SynthesizeSpeechRequest request,
     gaxgrpc::CallSettings callSettings = null)
 {
     Modify_SynthesizeSpeechRequest(ref request, ref callSettings);
     return(_callSynthesizeSpeech.Sync(request, callSettings));
 }
Пример #17
0
        private void btn_speak_Click(object sender, EventArgs e)
        {
            var client  = new AmazonPollyClient();
            var request = new SynthesizeSpeechRequest
            {
                Text         = txt_Text.Text,
                OutputFormat = OutputFormat.Mp3,
                VoiceId      = VoiceId.Joey
            };
            var response = client.SynthesizeSpeech(request);

            var folder   = AppDomain.CurrentDomain.BaseDirectory;
            var filename = $"{folder}{Guid.NewGuid()}.mp3";

            using (var fileStream = File.Create(filename))
            {
                response.AudioStream.CopyTo(fileStream);
                fileStream.Flush();
                fileStream.Close();
            }



            IWavePlayer     waveOutDevice   = new WaveOut();
            AudioFileReader audioFileReader = new AudioFileReader(filename);

            waveOutDevice.Init(audioFileReader);
            waveOutDevice.Play();
        }
Пример #18
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();
        }
Пример #19
0
        public static void SynthesizeSpeech()
        {
            var    client         = new AmazonPollyClient();
            String outputFileName = "speech.mp3";

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

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

                    var 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);
            }
        }
Пример #20
0
        public string PullPhrase(string lyric)
        {
            if (isServerWorking)
            {
                SynthesizeSpeechRequest sreq = new SynthesizeSpeechRequest()
                {
                    TextType     = TextType.Ssml,
                    Text         = "<speak><prosody " + speakstyle + ">" + lyric + "</prosody></speak>",
                    VoiceId      = speakvoice,
                    OutputFormat = OutputFormat.Mp3
                };

                string filename = getFilename(lyric);

                try {
                    OnlineRequestPolly(sreq, filename);
                }
                catch (Exception e) {
                    throw new FileLoadException();
                }

                return(ResourcePathConstants.SpeechCacheFolder + filename);
            }
            else
            {
                throw new FileLoadException();
            }
        }
        /// <summary>
        /// Converts text to speech with the given voice and uploads audio file to S3.
        /// </summary>
        /// <param name="id">Post ID in DynamoDB</param>
        /// <param name="voiceId">Voice to be used in speech</param>
        /// <param name="text">Text to be converted</param>
        /// <returns>The file's URL in S3</returns>
        public async Task <string> ConvertAsync(string id, string voiceId, string text)
        {
            var pollyClient = new AmazonPollyClient();

            IEnumerable <string> textBlocks = TruncateText(text);

            foreach (string block in textBlocks)
            {
                var request = new SynthesizeSpeechRequest
                {
                    OutputFormat = OutputFormat.Mp3,
                    Text         = block,
                    VoiceId      = voiceId
                };

                try
                {
                    var response = await pollyClient.SynthesizeSpeechAsync(request);

                    WriteToFile(response.AudioStream, id);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            return(await UploadAsync(id));
        }
Пример #22
0
        public async Task UtterInStream(string text, VoiceId voice, Action <byte[], int> dataAvailable)
        {
            var req = new SynthesizeSpeechRequest();

            req.VoiceId      = voice;
            req.TextType     = TextType.Text;
            req.Text         = text;
            req.OutputFormat = OutputFormat.Pcm;
            req.SampleRate   = "16000";
            var response = await polly.SynthesizeSpeechAsync(req);

            using (MemoryStream ms = new MemoryStream())
            {
                response.AudioStream.CopyTo(ms);
                ms.Flush();
                ms.Position = 0;

                var buffer = new byte[1 * 640];
                int bytesRead;
                while ((bytesRead = await ms.ReadAsync(buffer, 0, buffer.Length)) > 0)
                {
                    dataAvailable(buffer, bytesRead);
                }
                ;
            }
        }
Пример #23
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}");
        }
Пример #24
0
        /// <summary>
        /// Initiates the asynchronous execution of the SynthesizeSpeech operation.
        /// </summary>
        ///
        /// <param name="request">Container for the necessary parameters to execute the SynthesizeSpeech operation on AmazonPollyClient.</param>
        /// <param name="callback">An AsyncCallback delegate that is invoked when the operation completes.</param>
        /// <param name="state">A user-defined state object that is passed to the callback procedure. Retrieve this object from within the callback
        ///          procedure using the AsyncState property.</param>
        ///
        /// <returns>An IAsyncResult that can be used to poll or wait for results, or both; this value is also needed when invoking EndSynthesizeSpeech
        ///         operation.</returns>
        /// <seealso href="http://docs.aws.amazon.com/goto/WebAPI/polly-2016-06-10/SynthesizeSpeech">REST API Reference for SynthesizeSpeech Operation</seealso>
        public IAsyncResult BeginSynthesizeSpeech(SynthesizeSpeechRequest request, AsyncCallback callback, object state)
        {
            var marshaller   = new SynthesizeSpeechRequestMarshaller();
            var unmarshaller = SynthesizeSpeechResponseUnmarshaller.Instance;

            return(BeginInvoke <SynthesizeSpeechRequest>(request, marshaller, unmarshaller,
                                                         callback, state));
        }
Пример #25
0
        /// <summary>
        /// Initiates the asynchronous execution of the SynthesizeSpeech operation.
        /// </summary>
        ///
        /// <param name="request">Container for the necessary parameters to execute the SynthesizeSpeech operation.</param>
        /// <param name="cancellationToken">
        ///     A cancellation token that can be used by other objects or threads to receive notice of cancellation.
        /// </param>
        /// <returns>The task object representing the asynchronous operation.</returns>
        /// <seealso href="http://docs.aws.amazon.com/goto/WebAPI/polly-2016-06-10/SynthesizeSpeech">REST API Reference for SynthesizeSpeech Operation</seealso>
        public virtual Task <SynthesizeSpeechResponse> SynthesizeSpeechAsync(SynthesizeSpeechRequest request, System.Threading.CancellationToken cancellationToken = default(CancellationToken))
        {
            var marshaller   = SynthesizeSpeechRequestMarshaller.Instance;
            var unmarshaller = SynthesizeSpeechResponseUnmarshaller.Instance;

            return(InvokeAsync <SynthesizeSpeechRequest, SynthesizeSpeechResponse>(request, marshaller,
                                                                                   unmarshaller, cancellationToken));
        }
Пример #26
0
        internal virtual SynthesizeSpeechResponse SynthesizeSpeech(SynthesizeSpeechRequest request)
        {
            var options = new InvokeOptions();

            options.RequestMarshaller    = SynthesizeSpeechRequestMarshaller.Instance;
            options.ResponseUnmarshaller = SynthesizeSpeechResponseUnmarshaller.Instance;

            return(Invoke <SynthesizeSpeechResponse>(request, options));
        }
Пример #27
0
		public async Task<ConvertTextResponse> AddItem(ConvertTextRequest request) {
			HttpClient httpClient = new HttpClient();
			// string uri = "https://api.rss2json.com/v1/api.json?rss_url=https%3A%2F%2Ftechcrunch.com%2Ffeed%2F";
			var response = await httpClient.GetAsync(request.Uri);
			string responseContent = await response.Content.ReadAsStringAsync();
			dynamic JsonResponse = JObject.Parse(responseContent);
			dynamic items = JsonResponse.items;

			var item = items[0];
			string guid = item.guid;
			int index = guid.IndexOf("?p=");
			string itemId = guid.Substring(index + 3, 7);

			var voicesResponse = _provider.Polly.DescribeVoicesAsync(new DescribeVoicesRequest 
			{
				LanguageCode = request.Language
			});

			Random random = new Random();
			int r = random.Next(voicesResponse.Result.Voices.Count);
			string voice = voicesResponse.Result.Voices[r].Id;

			var pollyRequest = new SynthesizeSpeechRequest {
				OutputFormat = OutputFormat.Mp3,
				Text = item.description,
				TextType = "text",
				VoiceId = voice
			};

			var pollyResponse = await _provider.Polly.SynthesizeSpeechAsync(pollyRequest);
			if (pollyResponse == null
				|| pollyResponse.HttpStatusCode != (HttpStatusCode)200) {
				throw new Exception("Text could not be converted to audio.");
			}
			var memoryStream = new MemoryStream();
			pollyResponse.AudioStream.CopyTo(memoryStream);
			var s3Response = await _provider.S3.PutObjectAsync(new PutObjectRequest {
				BucketName = _provider.Bucket,
				Key = itemId + ".mp3",
				InputStream = memoryStream
			});
			if (s3Response == null
				|| s3Response.HttpStatusCode != (HttpStatusCode)200) {
				throw new Exception("Unable to save audio file to s3");
			}
			
			await _provider.SnsClient.PublishAsync(new PublishRequest {
                TopicArn = _provider.NotificationTopic,
                Message = "The audio file is ready: https://s3.amazonaws.com/" + _provider.Bucket + "/" + itemId + ".mp3",
				Subject = item.title
            });

			return new ConvertTextResponse {
				FileName = itemId + ".mp3",
				Voice = voice
			};
		}
Пример #28
0
        public static async Task Main(string[] args)
        {
            var client = new AmazonS3Client(RegionEndpoint.EUWest2);
            ListBucketsResponse buckets_response = await client.ListBucketsAsync();

            foreach (var bucket in buckets_response.Buckets)
            {
                ListObjectsV2Request req = new ListObjectsV2Request();
                req.BucketName = bucket.BucketName;
                // NOTE if this finds a folder it includes the folder as an object, and its content as
                // more objects.  I thought folders didn't exist as such, but it seems they do.
                ListObjectsV2Response res = await client.ListObjectsV2Async(req);

                int len = res.S3Objects.Count;
                Console.WriteLine(bucket.BucketName + " date:" + bucket.CreationDate + " has " + len + " objects.");
                if (len > 0)
                {
                    foreach (var obj in res.S3Objects)
                    {
                        Console.WriteLine("   " + obj.Key + " " + obj.Size + " " + obj.StorageClass + " " + obj.ETag);
                    }
                }
            }

            var pollyclient = new AmazonPollyClient();
            SynthesizeSpeechRequest request = new SynthesizeSpeechRequest();

            request.Text         = "Hello my lovely little world.";
            request.OutputFormat = OutputFormat.Mp3;
            request.VoiceId      = VoiceId.Emma;
            var pollyres = await pollyclient.SynthesizeSpeechAsync(request);

            var fs    = new FileStream("c:/tmp/hw.mp3", FileMode.Create);
            var instr = pollyres.AudioStream;

            byte[] bytes = new byte[256];
            int    count = 0;

            while (true) //count < instr.Length)
            {
                int c = instr.Read(bytes, 0, 256);
                if (c == 0)
                {
                    break;
                }
                fs.Write(bytes, 0, c);
                count += c;
            }
            fs.Close();
            var okater = new NetCoreAudio.Player();

            okater.Play("c:/tmp/hw.mp3").Wait();
            Console.WriteLine("Thanks for all the fish");
            Console.ReadLine();
        }
Пример #29
0
        /// <summary>
        /// Convert text to speech to an audio file then save it to a S3 bucket
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task <ConvertTextResponse> AddTextToSpeechFileToBucket(ConvertTextRequest request)
        {
            if (string.IsNullOrEmpty(request.Content))
            {
                throw new ArgumentNullException("Content cannot be empty");
            }
            _logger.Info($"Content requested to convert: {request.Content}");
            var pollyRequest = new SynthesizeSpeechRequest {
                OutputFormat = OutputFormat.Mp3,
                Text         = request.Content,
                TextType     = "text",
                VoiceId      = VoiceId.Amy,      // https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/Polly/TVoiceId.html
                LanguageCode = LanguageCode.EnUS // https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/Polly/TLanguageCode.html
            };

            // Send request to Amazon Polly to convert text to audio.
            var synthesizeSpeechResponse = await _pollyClient.SynthesizeSpeechAsync(pollyRequest);

            if (synthesizeSpeechResponse == null ||
                synthesizeSpeechResponse.HttpStatusCode != (HttpStatusCode)200)
            {
                throw new Exception("Text could not be converted to audio.");
            }
            _logger.Info($"Requested characters was: {synthesizeSpeechResponse.RequestCharacters}");

            // Stream the audio from Amazon Polly to a S3 bucket using the filename provided by the request
            PutObjectResponse s3Response;

            using (var memoryStream = new MemoryStream()) {
                synthesizeSpeechResponse.AudioStream.CopyTo(memoryStream);
                s3Response = await _s3Client.PutObjectAsync(new PutObjectRequest {
                    BucketName  = _s3BucketName,
                    Key         = request.FileName,
                    InputStream = memoryStream
                });
            }
            if (s3Response == null ||
                s3Response.HttpStatusCode != (HttpStatusCode)200)
            {
                throw new Exception("Unable to save audio file to s3");
            }

            // Publish to a SNS topic that a new audio file has been saved.
            var publishRequest = new PublishRequest {
                TopicArn = _notificationSnsTopic,
                Message  = "An audio file was saved",
                Subject  = "New MP3 File"
            };
            await _snsClient.PublishAsync(publishRequest);

            return(new ConvertTextResponse {
                FileName = request.FileName
            });
        }
Пример #30
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;
            }
        }