B4A Library [B4X] b4xlib Cloud Text-To-Speech

What is Cloud Text-to-Speech?
Google Cloud Text-to-Speech converts text into human-like speech in more than 100 voices across 20+ languages and variants. It applies groundbreaking research in speech synthesis (WaveNet) and Google's powerful neural networks to deliver high-fidelity audio. With this easy-to-use API, you can create lifelike interactions with your users that transform customer service, device interaction, and other applications.
1*Q87Ixh1-hfHr_We87vuumw@2x.png



Google's Google Assistant uses the same API for turning text into speech. Take a listen to the sample:
https://cloud.google.com/text-to-speech/docs/audio/Hol_before.wav

Read more here: https://cloud.google.com/text-to-speech/

Since it is an API, this means that this library can be platform independent, delivering the higher quality voices for 20+ languages at no additional installation on any device.

Installation:
1. Place CloudTTS.b4xlib in your additional libraries folder
2. Go to https://console.cloud.google.com/apis/api/texttospeech.googleapis.com and enable Cloud Text to speech API
3. Go to "Credentials" and generate an API key
this API key will be used in the code:




Usage:

Using the library is pretty simple:
B4X:
    Dim cTTS As CloudTTS
    cTTS.Initialize(Me, "cTTS", "YOUR API KEY")
    cTTS.Speak("This is the text to speak")

Listing voices:
B4X:
    cTTS.ListAllAvailableVoices

Using the listed voices:
B4X:
Sub cTTS_AvailableVoices(mapVoiceData As Map)
    If mapVoiceData<>Null Then
        Dim lstVoice As List=mapVoiceData.Get("voices")
        lstVoices.Clear
        For i=0 To lstVoice.Size-1
            Dim maptmp As Map=lstVoice.Get(i)
            lstVoices.AddSingleLine2(maptmp.Get("name"), maptmp.Get("name")&"#"&maptmp.Get("ssmlGender"))
        Next
        If lstVoices.Parent=Null Then Activity.AddView(lstVoices, 0, 0, 100%x, txtSpeak.Height)
    End If
End Sub

Sub lstVoices_ItemClick (Position As Int, Value As Object)
    Dim strvoice As String=Value
    Dim name, ssmlGender As String
 
    'Get the name and ssmlGender
    name=strvoice.SubString2(0, strvoice.IndexOf("#"))
    ssmlGender=strvoice.SubString(strvoice.IndexOf("#")+1)
 
    cTTS.setvoice3(name, ssmlGender)
    ToastMessageShow("Voice changed to "&name, False)
 
    lstVoices.RemoveView
End Sub

Example for Android: https://drive.google.com/open?id=1jyTZHBzOvgwo8CFlbcaXHnjeWJJhmSSR


Documentation:


CloudTTS
Author:
Multiverse Software
Version: 1
  • CloudTTS
    Events:
    • onComplete
    • onError (errormessage As String)
    • AvailableVoices
    • StartedSpeaking
    Fields:
    • languagegendermale As String
    • languagegenderfemale As String
    Methods:
    • isinitialized As boolean
      Returns True if the CloudTTS Object is initialized. Else, returns False
    • initialize (ba As anywheresoftware.b4a.BA, Cloud_TTS_Parent As Object, Cloud_TTS_Event_Name As String, Cloud_TTS_API_KEY As String) As String
      [Method] - initializes Cloud TTS

      CloudTTS_Parent = pointer to parent module (module creating instance of this class)
      CloudTTS_Event_Name = event name stub
      API_KEY = The API key for your app, which can be received from console.cloud.google.com
    • isspeaking As boolean
      Checks if TTS is currently Speaking
    • listallavailablevoices As anywheresoftware.b4a.keywords.Common.ResumableSubWrapper
      [Method] - Lists all the available voices
      Calls the method CloudTTS_AvailableVoices when all the voices are retrived
      Returns Null if error
      Documentation: https://cloud.google.com/text-to-speech/docs/reference/rest/v1/voices/list
    • listallavailablevoices2 (Cloud_TTS_LangCode As String) As anywheresoftware.b4a.keywords.Common.ResumableSubWrapper
      [Method] - Lists all the available voices in a particular language
      Calls the method CloudTTS_AvailableVoices when all the voices are retrived
      Returns Null if error
      Documentation: https://cloud.google.com/text-to-speech/docs/reference/rest/v1/voices/list
    • pausespeaking As String
      Pauses the Speaking, if already speaking
    • resumespeaking As String
      Resumes the Speaking, if already speaking
    • setvoice (lanCode As String) As String
      Sets the Voice for TTS to speak
      lanCode = The Language code, dafault is "en-US"
    • setvoice2 (lanCode As String, VoiceGender As String) As String
      Sets the Voice for TTS to speak
      lanCode = The Language code, dafault is "en-US"
      VoiceGender = The Voice Gender: MALE, FEMALE
    • setvoice3 (lanName As String, VoiceGender As String) As String
      Sets the Voice for TTS to speak
      lanName = The Language name
      VoiceGender = The Voice Gender: MALE, FEMALE
    • speak (strText As String) As void
      Speaks the Text
      strText = Text to Speak
    • ConvertToMP3(toSpeak As String, fileDir As String, fileName As String)
      Converts the given text into mp3 and saves it to the desired path
      toSpeak- Text to convert
      fileDir- Directory to save file in
      fileName- Name of the file to save as
      ConvertToMP3("This is the text to save", File.DirInternal, "CloudTTS.mp3")
    • stopspeaking As String
      Stops the Speaking, if already speaking
    • strPrevToSpeak
      Returns the String with last spoken text

Pricing:
First Million characters are free every month:
upload_2019-3-23_17-20-34.png


The library works on B4A and B4J. If someone can port it to B4i, it will be a complete package.
 

Attachments

  • Cloud Text-to-Speech ex B4J.zip
    148.9 KB · Views: 675
  • CloudTTS.b4xlib
    3 KB · Views: 644
Last edited:
Top