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

Discussion in 'Additional libraries, classes and official updates' started by Multiverse app, Mar 23, 2019.

  1. Multiverse app

    Multiverse app Active Member Licensed User

    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.
    [​IMG]


    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:
    Code:
    Dim cTTS As CloudTTS
        cTTS.Initialize(Me, 
    "cTTS""YOUR API KEY")
        cTTS.Speak(
    "This is the text to speak")
    Listing voices:
    Code:
    cTTS.ListAllAvailableVoices
    Using the listed voices:
    Code:
    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, 00100%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.
     

    Attached Files:

    Last edited: Mar 29, 2019
  2. Mashiane

    Mashiane Expert Licensed User

    Are these 1 million character counts compounded or per case? Secondly, this requires a constant internet connection for use?
     
  3. DonManfred

    DonManfred Expert Licensed User

    i guess compounded....
    10x100.000 (per case) will reach the maximum
     
    Mashiane likes this.
  4. Multiverse app

    Multiverse app Active Member Licensed User

    No, only when you call .speak and .listallavailable voices methods.
     
    Last edited: Mar 25, 2019
    Mashiane likes this.
  5. catyinwong

    catyinwong Active Member Licensed User

    Does it require higher version than B4A 8.3? My BA4 seems to be unable to recognize the liabrary
     
  6. DonManfred

    DonManfred Expert Licensed User

    Multiverse app and Peter Simpson like this.
  7. Multiverse app

    Multiverse app Active Member Licensed User

    New release
    1. Added ConvertToMP3
    2. Fixed crash on isInitialized
    3. Added event StartedSpeaking. This will be called when the speech actually starts playing
     
    Erel and inakigarm like this.
  8. cooperlegend

    cooperlegend Active Member Licensed 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 likes this.
  9. Multiverse app

    Multiverse app Active Member Licensed User

    Thanks for the feedback, I will have a look at the library this weekend

    Yes, it depends on jAudioClip for playing audio on B4J
     
  10. cooperlegend

    cooperlegend Active Member Licensed User

    Excellent, thank you for the quick response :)

    John
     
  11. Multiverse app

    Multiverse app Active Member Licensed User

    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.
     
  12. cooperlegend

    cooperlegend Active Member Licensed User

    Would it be possible to somehow clear jAudioClip and re-initialize each time ?
     
  13. Multiverse app

    Multiverse app Active Member Licensed User

    Tried that, still the same issue..
     
  14. cooperlegend

    cooperlegend Active Member Licensed 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 likes this.
  15. Multiverse app

    Multiverse app Active Member Licensed User

    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.

    thanks
     
  16. stevel05

    stevel05 Expert Licensed User

    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.
     
  17. stevel05

    stevel05 Expert Licensed User

    I've just checked the JAudioclip Library, you can use
    Code:
    Clip = AudipClip_Static.NewAudioclip(URI)
    to change the sound played.
     
    DonManfred likes this.
  18. cooperlegend

    cooperlegend Active Member Licensed User

    Thanks Steve, Hopefully this will sort the issue and a new library can be create for the CloudTTS :)
     
  19. Multiverse app

    Multiverse app Active Member Licensed User

    updated post #1
    Audio should play fine now. Make sure to update the jaudioclip library.
    The lib still lacks resume/pause for b4j
     
    Last edited: Jul 14, 2019
  20. cooperlegend

    cooperlegend Active Member Licensed User

    Brilliant much appreciated. I will update to this as soon as I am back in the office week Monday :)
     
    Multiverse app likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice