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: 554
  • CloudTTS.b4xlib
    3 KB · Views: 523
Last edited:

catyinwong

Active Member
Licensed User
Longtime User
Does it require higher version than B4A 8.3? My BA4 seems to be unable to recognize the liabrary
 

DonManfred

Expert
Licensed User
Longtime User

cooperlegend

Active Member
Licensed User
Longtime User
Runs really well on B4A, but on B4J I have some issues.

1) If you send a second string of text it repeats the previous one.
2) Library functions available don't match the list above... "StartedSpeak" is missing, functions like "Audio_complete", "JobDone" included.
3) Functions "IsSpeaking" and "StopSpeaking" are crashing on null reference.
4) I also had to download and include the jAudioClip library... Is this correct?

Thanks in advance :)
 

Multiverse app

Active Member
Licensed User
Longtime User
1) If you send a second string of text it repeats the previous one.
2) Library functions available don't match the list above... "StartedSpeak" is missing, functions like "Audio_complete", "JobDone" included.
3) Functions "IsSpeaking" and "StopSpeaking" are crashing on null reference.
Unfortunately, these are the shortcomings of jAudioClip.

Once the jAudioClip object is initialized with one URI, it is not possible to change the audio clip. Also, the library also does not provide a resume, pause or onfinished event. Unless there is a better alternative to playing audio, I do not think this will be functional.
 

cooperlegend

Active Member
Licensed User
Longtime User
I noticed that it wrote the mp3 file of the speech each time, but even deleting this didn't help. I have written to Stevel05 to see if he can help :)
 

Multiverse app

Active Member
Licensed User
Longtime User
I noticed that it wrote the mp3 file of the speech each time
Yes, the file is written correctly. If you manually play it, it works correctly. I also tried making a local object of jAudioClip, so that it can be initialized every time. Confusingly still, the issue persisted.

I have written to Stevel05 to see if he can help :)
thanks
 

stevel05

Expert
Licensed User
Longtime User
I also tried making a local object of jAudioClip, so that it can be initialized every time. Confusingly still, the issue persisted.

You can use a global object but try creating a new object each time i.e. with a Dim statement, then initialize it with the new text.
 

stevel05

Expert
Licensed User
Longtime User
I've just checked the JAudioclip Library, you can use
B4X:
Clip = AudipClip_Static.NewAudioclip(URI)
to change the sound played.
 

cooperlegend

Active Member
Licensed User
Longtime User
Thanks Steve, Hopefully this will sort the issue and a new library can be create for the CloudTTS :)
 
Top