Using google’s Text to Speech in Vb.NET

Imports System.IO
Imports Google.Cloud.TextToSpeech.V1
'Install-Package Google.Cloud.TextToSpeech.V1 -Version 2.1.0
Module Program
    Const JsonPath As String = "C:\Users\MyUser\repos\GoogleSpeechToText\GoogleSpeechToText\client_secrets.json"
    Dim MyTextToSpeechClient As TextToSpeechClient
    Dim OutputFile As String = "Output.mp3"
    Sub Main(args As String())
        Dim FileText As String = IO.File.ReadAllText("D:\TextFileToConvert.txt").Trim
    End Sub

    Public Async Function Run(ByVal Text As String) As Task 'Upload to Youtube
            Dim MyTextToSpeechClientBuilder As New TextToSpeechClientBuilder()
            MyTextToSpeechClientBuilder.CredentialsPath = JsonPath
            MyTextToSpeechClient = MyTextToSpeechClientBuilder.Build()

            Dim response = MyTextToSpeechClient.ListVoices("en")

            For Each Myvoice In response.Voices
                Console.WriteLine($"{Myvoice.Name} ({Myvoice.SsmlGender}); Language codes: {String.Join(", ", Myvoice.LanguageCodes)}")
        Catch ex As Exception
        End Try

        Dim input As SynthesisInput = New SynthesisInput With {
            .Text = Text
        Dim voice As VoiceSelectionParams = New VoiceSelectionParams With {
            .LanguageCode = "en-US",
            .SsmlGender = SsmlVoiceGender.Neutral
        Dim config As AudioConfig = New AudioConfig With {
            .AudioEncoding = AudioEncoding.Mp3
        Dim response2 = MyTextToSpeechClient.SynthesizeSpeech(New SynthesizeSpeechRequest With {
            .Input = input,
            .Voice = voice,
            .AudioConfig = config

        Using output As Stream = File.Create(OutputFile)
            Console.WriteLine($"Audio content written to file: " & OutputFile)
        End Using

    End Function
End Module

Popular Posts

Leave a Reply

Your email address will not be published. Required fields are marked *