Ejemplo n.º 1
0
        public async Task <string> ObtainSpeechPathAsync(string text, string language = "en-US", bool male = true)
        {
            TextToSpeechClient client = TextToSpeechClient.Create();

            var response = await client.SynthesizeSpeechAsync(new SynthesizeSpeechRequest
            {
                Input = new SynthesisInput
                {
                    Text = text
                },
                Voice = new VoiceSelectionParams
                {
                    LanguageCode = language,
                    SsmlGender   = male ? SsmlVoiceGender.Male : SsmlVoiceGender.Female
                },
                AudioConfig = new AudioConfig
                {
                    AudioEncoding = AudioEncoding.Mp3
                }
            });

            var filename = Path.GetTempFileName();

            using (Stream output = File.Create(filename))
            {
                response.AudioContent.WriteTo(output);
                _logger.Log($"Audio content written to temp file '{filename}'");
            }

            return(filename);
        }
Ejemplo n.º 2
0
        public async Task GenerateSpeechFile(string text, string path)
        {
            TextToSpeechClient client = TextToSpeechClient.Create();

            SynthesisInput input = new SynthesisInput
            {
                Text = text
            };

            VoiceSelectionParams voice = new VoiceSelectionParams
            {
                LanguageCode = "en-US",
                SsmlGender   = SsmlVoiceGender.Neutral
            };

            AudioConfig config = new AudioConfig
            {
                AudioEncoding = AudioEncoding.Mp3
            };

            var response = await client.SynthesizeSpeechAsync(new SynthesizeSpeechRequest
            {
                Input       = input,
                Voice       = voice,
                AudioConfig = config
            });

            using (Stream output = File.Create(path))
            {
                response.AudioContent.WriteTo(output);
            }
        }
        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();
        }
Ejemplo n.º 4
0
        private static async Task SpeakGoogle(ISpeaker speaker, string textToSpeech, string user)
        {
            textToSpeech = textToSpeech.Replace("\"", "\"\"");

            // Instantiate a client
            TextToSpeechClient client = TextToSpeechClient.Create();

            // Set the text input to be synthesized.
            SynthesisInput input = new SynthesisInput
            {
            //Text = textToSpeech,
                Ssml = File.ReadAllText("Speakers/SSML.xml").Replace("{text}", textToSpeech).Replace("{voice}", speaker.Voice).Replace("{posmsg}", speaker.Diction).Replace("{alert}", speaker.Alert),
            };

            // Build the voice request, select the language code ("en-US"),
            // and the SSML voice gender ("neutral").
            VoiceSelectionParams voice = new VoiceSelectionParams
            {
                LanguageCode = speaker.Accent.ToString(),
                //SsmlGender = SsmlVoiceGender.Neutral
            };

            // Select the type of audio file you want returned.
            AudioConfig config = new AudioConfig
            {
                AudioEncoding = AudioEncoding.Mp3
            };

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

            // create a temp file with .ps1 extension  
            var cFile = System.IO.Path.GetTempPath() + Guid.NewGuid() + ".mp3";

            // Write the binary AudioContent of the response to an MP3 file.
            using (Stream output = File.Create(cFile))
                response.AudioContent.WriteTo(output);

            Sounds.RandomTrollSound();

            SpeakerCore.PreSpeech(user);

            SpeakerCore.ExecuteMP3File(cFile);

            await AutomaticTranslator.Translate(textToSpeech);

        }
Ejemplo n.º 5
0
        public async Task <string> ToAudio(string transcript)
        {
            if (toSpeechClient == null)
            {
                return("");
            }
            var BUFFER_SIZE    = 2048;
            var timeStamp      = DateTime.Now.ToString("MM-dd-yyyy_HH_mm_ss");
            var outputFileName = $@".\vocalized\{timeStamp}.mp3";

            try
            {
                using (var fileStream = File.Create(outputFileName))
                {
                    var response = await toSpeechClient.SynthesizeSpeechAsync(new SynthesizeSpeechRequest
                    {
                        Input = new SynthesisInput
                        {
                            Text = transcript
                        },
                        // Note: voices can also be specified by name
                        Voice = new VoiceSelectionParams
                        {
                            Name         = settingsService.settings.googleSettings.Voice.Name,
                            LanguageCode = settingsService.settings.generalSettings.TextInputLanguage
                        },
                        AudioConfig = new AudioConfig
                        {
                            AudioEncoding = AudioEncoding.Mp3
                        }
                    });

                    var    ms     = new MemoryStream(response.AudioContent.ToByteArray());
                    byte[] buffer = new byte[BUFFER_SIZE];
                    int    readBytes;
                    using (BufferedStream bufferedInput = new BufferedStream(ms))
                    {
                        while ((readBytes = await bufferedInput.ReadAsync(buffer, 0, buffer.Length)) > 0)
                        {
                            await fileStream.WriteAsync(buffer, 0, readBytes);
                        }
                    }
                }
                return(outputFileName);
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception caught: " + e);
                MessageBox.Show("Exception caught: " + e);
            }
            return(outputFileName);
        }
Ejemplo n.º 6
0
    /// <summary>
    /// リクエストを送信する
    /// </summary>
    /// <param name="text">音声合成を行う対象の文</param>
    public static void CreateRequest(string text)
    {
        var request = new SynthesizeSpeechRequest
        {
            Input = new SynthesisInput {
                Text = text
            },
            AudioConfig = _audioConfig,
            Voice       = _voiceSelectionParams
        };

        _stopwatch.Restart();

        // リクエストを非同期で送信し,返ってきた後に再生するメソッドに投げる
        Task.Run(async() => { SetAudioClip(await _client.SynthesizeSpeechAsync(request)); });
    }
        /// <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
        }
Ejemplo n.º 8
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);
        }
Ejemplo n.º 9
0
        private async Task QueueText(string text, int index, GoogleSpeechRequest request, GoogleSpeechResult result)
        {
            var response = await _client.SynthesizeSpeechAsync(
                input : new SynthesisInput
            {
                Text = text
            },
                voice : request.VoiceSelection,
                audioConfig : request.AudioConfig
                );

            result.DigestBytes(index, response.AudioContent);

            if (request.CallBack != null)
            {
                await request.CallBack(text.Length);
            }
        }
        protected async Task <string> GetGoogleSynthSpeech(string text, TTSVoice voicePreference, TTSPitch pitchPreference, string filename = null)
        {
            VoiceSelectionParams voice = voicePreference.GetGoogleVoiceSelectionParams();

            AudioConfig config = new AudioConfig
            {
                AudioEncoding = AudioEncoding.Mp3,
                Pitch         = pitchPreference.GetSemitoneShift()
            };

            //TTS
            SynthesisInput input = new SynthesisInput
            {
                Ssml = PrepareGoogleSSML(text)
            };

            // Perform the Text-to-Speech request, passing the text input
            // with the selected voice parameters and audio file type
            GoogleSynthesizeSpeechResponse response = await googleClient.SynthesizeSpeechAsync(input, voice, config);

            // 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))
            {
                response.AudioContent.WriteTo(file);
            }

            return(filepath);
        }
Ejemplo n.º 11
0
        /// <summary>Snippet for SynthesizeSpeechAsync</summary>
        public async Task SynthesizeSpeechRequestObjectAsync()
        {
            // Snippet: SynthesizeSpeechAsync(SynthesizeSpeechRequest, CallSettings)
            // Additional: SynthesizeSpeechAsync(SynthesizeSpeechRequest, CancellationToken)
            // Create client
            TextToSpeechClient textToSpeechClient = await TextToSpeechClient.CreateAsync();

            // Initialize request argument(s)
            SynthesizeSpeechRequest request = new SynthesizeSpeechRequest
            {
                Input              = new SynthesisInput(),
                Voice              = new VoiceSelectionParams(),
                AudioConfig        = new AudioConfig(),
                EnableTimePointing =
                {
                    SynthesizeSpeechRequest.Types.TimepointType.Unspecified,
                },
            };
            // Make the request
            SynthesizeSpeechResponse response = await textToSpeechClient.SynthesizeSpeechAsync(request);

            // End snippet
        }
Ejemplo n.º 12
0
        async Task Run()
        {
            using (var cts = new CancellationTokenSource())
            {
                Console.InputEncoding   = Encoding.Unicode;
                Console.OutputEncoding  = Encoding.Unicode;
                Console.CancelKeyPress += (s, e) =>
                {
                    e.Cancel = true;
                    cts.Cancel();
                };

                try
                {
                    using var input  = new StreamReader(Console.OpenStandardInput(), Encoding.Unicode);
                    using var api    = PickBestApi(input, PortAudioHostApi.SupportedHostApis, cts.Token);
                    using var device = GetOutputDevice(input, api.Devices, cts.Token);

                    Console.Write("Saisir le code de langue voulu (défaut : fr-FR): ");
                    if (!ReadLine(input, cts.Token, out string language))
                    {
                        return;
                    }
                    if (string.IsNullOrWhiteSpace(language))
                    {
                        language = "fr-FR";
                    }

                    Console.WriteLine("Choisir le type de voix (femme/homme):");
                    Console.WriteLine("[0] - Non spécifié");
                    Console.WriteLine("[1] - Homme");
                    Console.WriteLine("[2] - Femme");
                    Console.WriteLine("[2] - Neutre");

                    if (!ReadLine(input, cts.Token, out string voiceGenderStr) || !int.TryParse(voiceGenderStr, out int voiceGenderInt))
                    {
                        return;
                    }

                    var voiceGender = (SsmlVoiceGender)voiceGenderInt;

                    Console.WriteLine($"Prêt à parler sur {device.Name}");

                    while (!cts.IsCancellationRequested)
                    {
                        Console.Write("->");

                        // Handle user input
                        if (!ReadLine(input, cts.Token, out string line) || line is null)
                        {
                            return;
                        }

                        Console.WriteLine("Récupération du son pour le texte : " + line);
                        Console.WriteLine("Son récupéré, lecture...");

                        using var audioStream = await TextToAudioStreamAsync(line, language, voiceGender, cts.Token);

                        using var pump = new PortAudioDevicePump(
                                  device,
                                  2,
                                  new PortAudioSampleFormat(PortAudioSampleFormat.PortAudioNumberFormat.Signed, 2),
                                  device.DefaultLowOutputLatency,
                                  48000,
                                  (buffer, offset, count) => audioStream.Read(buffer, offset, count));

                        using var handle     = new ManualResetEventSlim(false);
                        pump.StreamFinished += FinishedHandler;
                        pump.Start();
                        handle.Wait();
                        pump.StreamFinished -= FinishedHandler;

                        void FinishedHandler(object sender, EventArgs eventArgs) => handle.Set();

                        Console.WriteLine("Lecture terminée");
                    }
                }
                catch (OperationCanceledException)
                {
                }
            }

            async Task <Stream> TextToAudioStreamAsync(string text, string language, SsmlVoiceGender voiceGender, CancellationToken token)
            {
                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++)
                            {
                                var bytes = BitConverter.GetBytes(packet[i]);
                                pcmStream.Write(bytes, 0, bytes.Length);
                            }
                        }
                    }

                    pcmStream.Position = 0;
                    return(pcmStream);
                }
            }
        }