Example #1
0
        protected override void Execute(CodeActivityContext context)
        {
            var             text         = Text.Get(context);
            var             languageCode = LanguageCode.Get(context);
            var             serviceAcc   = ServiceAccountFile.Get(context);
            SsmlVoiceGender gender       = (SsmlVoiceGender)Enum.Parse(typeof(SsmlVoiceGender), Gender.ToString());

            Recognize.TextToSpeech(text, languageCode, gender, serviceAcc);
        }
Example #2
0
        public static void TextToSpeech(string text, string languageCode, SsmlVoiceGender gender, string serviceAcc)
        {
            GoogleCredential credentials = GoogleCredential.FromFile(serviceAcc);

            TextToSpeechClient client = TextToSpeechClient.Create(credentials);

            SynthesizeSpeechResponse response = client.SynthesizeSpeech(
                new SynthesisInput()
            {
                Text = text
            },
                new VoiceSelectionParams()
            {
                LanguageCode = languageCode,
                SsmlGender   = gender
            },
                new AudioConfig()
            {
                AudioEncoding = AudioEncoding.Linear16
            }
                );

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

            File.WriteAllBytes(speechFile, response.AudioContent);
            System.Media.SoundPlayer player = new System.Media.SoundPlayer();

            player.SoundLocation = speechFile;
            player.PlaySync();
            try
            {
                File.Delete(speechFile);
            }
            catch
            {
                Console.WriteLine("Cannot delete the file");
            }
        }
Example #3
0
        public async Task <Stream> TextToAudioStreamAsync(string text, string language = "fr-FR", SsmlVoiceGender voiceGender = SsmlVoiceGender.Male, CancellationToken token = default)
        {
            var request = new SynthesizeSpeechRequest
            {
                AudioConfig = new AudioConfig
                {
                    AudioEncoding = AudioEncoding.OggOpus,
                },
                Input = new SynthesisInput
                {
                    Text = text
                },
                Voice = new VoiceSelectionParams
                {
                    LanguageCode = language,
                    SsmlGender   = voiceGender,
                },
            };

            var response = await ttsClient.SynthesizeSpeechAsync(request, token);

            using var opusStream = new MemoryStream();
            response.AudioContent.WriteTo(opusStream);
            opusStream.Position = 0;

            var opusDecoder = new OpusDecoder(48000, 2);
            var oggIn       = new OpusOggReadStream(opusDecoder, opusStream);

            var pcmStream = new MemoryStream();

            while (oggIn.HasNextPacket)
            {
                short[] packet = oggIn.DecodeNextPacket();
                if (packet != null)
                {
                    for (int i = 0; i < packet.Length; i++)
                    {
                        byte[] bytes = BitConverter.GetBytes(packet[i]);
                        pcmStream.Write(bytes, 0, bytes.Length);
                    }
                }
            }

            pcmStream.Position = 0;
            return(pcmStream);
        }
        public void CheckForSpeechInDisk(Speech speech, string word, string languageCode, SsmlVoiceGender gender)
        {
            if (!File.Exists("wwwroot/assets/speeches/" + speech.Code + ".mp3"))
            {
                var response = new ResultModel <SynthesizeSpeechResponse>();
                if (notFoundLanguageCodes.Any(x => x.Code == languageCode))
                {
                    response = new ResultModel <SynthesizeSpeechResponse>
                    {
                        Success      = false,
                        ErrorMessage = notFoundLanguageCodes.Find(x => x.Code == languageCode).ErrorMessage
                    };
                }
                else
                {
                    response = DownloadWord(new SpeechModel {
                        Text = word, LanguageCode = languageCode, Gender = gender
                    });
                }

                if (!response.Success)
                {
                    speech.ErrorMessage = response.ErrorMessage;
                    speech.Status       = SpeechStatus.Error;
                    db.Query <int>(@"
                        update [dbo].[speech] SET 
                            Code = @Code, 
                            AddedDate = @AddedDate, 
                            Status = @Status, 
                            ErrorMessage = @ErrorMessage
                        Where Id = @Id
                        ", speech).SingleOrDefault();
                }
                else
                {
                    // Write the response to the output file.
                    using FileStream output = File.Create("wwwroot/assets/speeches/" + speech.Code + ".mp3");
                    response.Data.AudioContent.WriteTo(output);

                    // To change status of speech to success
                    speech.Status = SpeechStatus.Success;
                    db.Query <Speech>("update dbo.Speech SET Status = @Status where Id = @Id", speech).SingleOrDefault();
                }
            }
            else
            {
                // To change status of speech to success
                speech.Status = SpeechStatus.Success;
                db.Query <Speech>("update dbo.Speech SET Status = @Status where Id = @Id", speech).SingleOrDefault();
            }
        }
Example #5
0
        // Generate
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                if (!AuthenticateWithGoogle())
                {
                    return;
                }

                // Set the text input to be synthesized.
                SynthesisInput input = new SynthesisInput();

                if (checkBox_SSML.Checked)
                {
                    input.Ssml = richTextBox_Text.Text;
                }
                else
                {
                    input.Text = richTextBox_Text.Text;
                }

                // Build the voice request, select the language code ("en-US"),
                // and the SSML voice gender ("neutral").

                SsmlVoiceGender selected = (SsmlVoiceGender)Enum.Parse(typeof(SsmlVoiceGender), comboBox_Gender.Text);

                VoiceSelectionParams voice = new VoiceSelectionParams
                {
                    LanguageCode = LanguageCode,
                    SsmlGender   = selected
                };

                // Select the type of audio file you want returned.
                AudioConfig config = new AudioConfig
                {
                    AudioEncoding = AudioEncoding.Mp3,
                    Pitch         = (double)numericUpDown_Pitch.Value,
                    SpeakingRate  = (double)numericUpDown_Speed.Value
                };

                // Perform the Text-to-Speech request, passing the text input
                // with the selected voice parameters and audio file type
                var response = client.SynthesizeSpeech(new SynthesizeSpeechRequest
                {
                    Input       = input,
                    Voice       = voice,
                    AudioConfig = config
                });

                // Write the binary AudioContent of the response to an MP3 file.
                using (Stream output = File.Create(outputFile))
                {
                    response.AudioContent.WriteTo(output);
                    Log("Audio content written to " + outputFile + "");
                }
            }
            catch (Exception e2)
            {
                Log("Error: " + e2.Message);
            }
        }