Tool TTSFunctions

Hi everyone,

I gathered in a Codefile a lot of the code about TTS that goes around in the Forum and I added to it some code and changed it a bit to fit my needs. I created this Codefile with a bunch of Subs and Functions that would be useful (I believe) to all of you. So here is it...

(23/8/2023)
*Changed the SynthesizeToFile sub to adapt the changes in the newer androids (* the zip file with the example has not been changed so please do replace the TTSFunction code module)

(1/3/2019)
* Changed the GetListOfAvailableLanguages function which now accepts the TTS package and returns the available language of the given TTS or accepts just "" for the default TTS.
* Added SpeakLongText function which speaks text without limit in length
* Added StopSpeakingLongText function to make the Speaking of a Long Text Stop
* Added SetVoice function which sets one of the voices you have got as a return from the function GetVoices. See the example of usage in the zip file
* Added GetVoices function which returns all the voices available in the TTS or just the voices of the given locale if we set the OnlyCurrentLocale boolean to true. See the example of usage in the zip file
* Added GetTTSCurrentLocale function which returns the current locale set to the TTS
(28-10-2018)
* Added the GetListOfAvailableLanguages function which gets the available locales in the TTS engine...
(27-6-2018)
* Added the StartTextToSpeechSettings function to show the text to speech settings to the user
* Changed the SetEngineByPackage to return a TTS object {now it is called as tts = SetEngineByPackage(ttspackage, tts) }

Thanks to the excellent Martin Pearman's tool, here is my code module's current version documentation:

TTSFunctions
Code module
Subs in this code module will be accessible from all modules.
  • GetTTSNamesAndPackages (t As TTS) As List
    Get the names, packages and default state (if the tts is default) of the TTSs
    Uses TTS Lib, Reflection Lib, Phone Lib
  • SetEngineByPackage (sPackage As String, t As TTS) As TTS
    Set TTS engine by packagename
    Uses Reflection Lib, TTS Lib
    Returns a TTS object
  • StartTextToSpeechSettings
    Open the Text to speech engine settings to set what you want
  • IsTTSSpeaking (t As TTS) As Boolean
    Check if TTS is speaking
    Uses JavaObject Lib, TTS Lib
  • GetDefaultEngine (t As TTS) As String
    Get Default Engine of TTS
    Uses JavaObject Lib, TTS Lib
  • GetMaxInputLength (t As TTS) As Long
    Get max input length of TTS
    Uses JavaObject Lib, TTS Lib
  • SynthesizeToFile (t As TTS, subFolderToPutTheFile As String, filename As String, textToSpeak As String)
    TTS - synthesize to file
    Uses JavaObject Lib, TTS Lib, RuntimePermissions Lib
  • PlaySynthesizedFile (subFolderWhereTheFileIsLocated As String, fileName As String)
    Play synthesized File From TTS
    Uses RuntimePermissions Lib
  • GetListOfAvailableLanguages (sPackage As String) As ResumableSub
    Get all available languages of the TTS
    Uses Reflection Lib, JavaObject Lib
    ---------------------------------------------------------------------------------
    Requires API 21 (Android 5.0) and above
    It is called like this:
    Wait For (TTSFunctions.GetListOfAvailableLanguages(package or "")) Complete (sLangs As String)
    manipulate the sLangs variable
  • SpeakLongText (t As TTS, TextToSpeak As String)
    Speaks long texts more than MaxLength of TTS
    Uses TTS Lib
  • StopSpeakingLongText (t As TTS)
    Stops the speaking of long texts more than MaxLength of TTS
    Uses TTS Lib
  • SetVoice (t As TTS, Voice As String)
    Set a voice to the TTS
    Uses TTS Lib, JavaObject Lib
  • GetVoices (t As TTS, OnlyCurrentLocale As Boolean) As String()
    Get Voices of Current Engine of TTS with option to get the voices of current locale only.
    Uses JavaObject Lib, TTS Lib
  • GetTTSCurrentLocale (t As TTS) As String
    Gets the current locale of the TTS
    Uses JavaObject Lib, TTS Lib, Phone Lib




Cheers
 

Attachments

  • TTSFunctions.bas
    12.7 KB · Views: 207
  • TTSFunctions.zip
    22.2 KB · Views: 229
Last edited:

hatzisn

Expert
Licensed User
Longtime User
Added some new features see post#1.
 

hatzisn

Expert
Licensed User
Longtime User
Added a new feature in the TTSFunctions (Sub StartTextToSpeechSettings). See post#1 and get the updated codefile in this post.
 

hatzisn

Expert
Licensed User
Longtime User
Added a new feature in the TTSFunctions (Sub GetListOfAvailableLanguages). See post#1 and get the updated code file in this post.
 

hatzisn

Expert
Licensed User
Longtime User
Added new features in the TTSFunctions

The voices functions require API 21 and more...

(1/3/2019)
* Changed the GetListOfAvailableLanguages function which now accepts the TTS package and returns the available language of the given TTS or accepts just "" for the default TTS.
* Added SpeakLongText function which speaks text without limit in length
* Added StopSpeakingLongText function to make the Speaking of a Long Text Stop
* Added SetVoice function which sets one of the voices you have got as a return from the function GetVoices. See the example of usage in the zip file
* Added GetVoices function which returns all the voices available in the TTS or just the voices of the given locale if we set the OnlyCurrentLocale boolean to true. See the example of usage in the zip file
* Added GetTTSCurrentLocale function which returns the current locale set to the TTS

See post #1
 

Almora

Well-Known Member
Licensed User
Longtime User
hi..
Thanks for your work ..

B4X:
Sub SpeechChanged (Value As Int, UserChanged As Boolean)
    If UserChanged Then
        ts.SpeechRate = Value / 10         
    End If
End Sub

I've added this code, but no change.
 

hatzisn

Expert
Licensed User
Longtime User
Hi Almora, did you add this code to the TTSFunctions? Did you send the (ts as TTS,...) argument in this Sub? Did you try setting it outside of TTSFunctions codemodule? Did you checked the UserChanged boolean Value?
 

hatzisn

Expert
Licensed User
Longtime User
No to which of the four? :)
 

hatzisn

Expert
Licensed User
Longtime User
Did You check the value of UserChanged?
 

Almora

Well-Known Member
Licensed User
Longtime User
I was not having TTSFunctions but not a result. The user changes the change in the designer. (SeekBar)


TTSFunctions
B4X:
Sub SpeechChanged (t As TTS, Value As Int, UserChanged As Boolean)
    If UserChanged Then
        t.SpeechRate = Value / 10      
    End If
End Sub
 
Last edited:

hatzisn

Expert
Licensed User
Longtime User
Another chance is that you try to do that when it is already speaking. You should stop the TTS, change the speech rate and then restart the speaking of the text.
 

Almora

Well-Known Member
Licensed User
Longtime User
Yeah, but I don't have much experience, I think. thanks anyway ..
 

hatzisn

Expert
Licensed User
Longtime User
Hi everyone,

I gathered in a Codefile a lot of the code about TTS that goes around in the Forum and I added to it some code and changed it a bit to fit my needs. I created this Codefile with a bunch of Subs and Functions that would be useful (I believe) to all of you. So here is it...

(23/8/2023)
*Changed the SynthesizeToFile sub to adapt the changes in the newer androids (* the zip file with the example has not been changed so please do replace the TTSFunction code module)

(1/3/2019)
* Changed the GetListOfAvailableLanguages function which now accepts the TTS package and returns the available language of the given TTS or accepts just "" for the default TTS.
* Added SpeakLongText function which speaks text without limit in length
* Added StopSpeakingLongText function to make the Speaking of a Long Text Stop
* Added SetVoice function which sets one of the voices you have got as a return from the function GetVoices. See the example of usage in the zip file
* Added GetVoices function which returns all the voices available in the TTS or just the voices of the given locale if we set the OnlyCurrentLocale boolean to true. See the example of usage in the zip file
* Added GetTTSCurrentLocale function which returns the current locale set to the TTS
(28-10-2018)
* Added the GetListOfAvailableLanguages function which gets the available locales in the TTS engine...
(27-6-2018)
* Added the StartTextToSpeechSettings function to show the text to speech settings to the user
* Changed the SetEngineByPackage to return a TTS object {now it is called as tts = SetEngineByPackage(ttspackage, tts) }

Thanks to the excellent Martin Pearman's tool, here is my code module's current version documentation:

TTSFunctions
Code module
Subs in this code module will be accessible from all modules.
  • GetTTSNamesAndPackages (t As TTS) As List
    Get the names, packages and default state (if the tts is default) of the TTSs
    Uses TTS Lib, Reflection Lib, Phone Lib
  • SetEngineByPackage (sPackage As String, t As TTS) As TTS
    Set TTS engine by packagename
    Uses Reflection Lib, TTS Lib
    Returns a TTS object
  • StartTextToSpeechSettings
    Open the Text to speech engine settings to set what you want
  • IsTTSSpeaking (t As TTS) As Boolean
    Check if TTS is speaking
    Uses JavaObject Lib, TTS Lib
  • GetDefaultEngine (t As TTS) As String
    Get Default Engine of TTS
    Uses JavaObject Lib, TTS Lib
  • GetMaxInputLength (t As TTS) As Long
    Get max input length of TTS
    Uses JavaObject Lib, TTS Lib
  • SynthesizeToFile (t As TTS, subFolderToPutTheFile As String, filename As String, textToSpeak As String)
    TTS - synthesize to file
    Uses JavaObject Lib, TTS Lib, RuntimePermissions Lib
  • PlaySynthesizedFile (subFolderWhereTheFileIsLocated As String, fileName As String)
    Play synthesized File From TTS
    Uses RuntimePermissions Lib
  • GetListOfAvailableLanguages (sPackage As String) As ResumableSub
    Get all available languages of the TTS
    Uses Reflection Lib, JavaObject Lib
    ---------------------------------------------------------------------------------
    Requires API 21 (Android 5.0) and above
    It is called like this:
    Wait For (TTSFunctions.GetListOfAvailableLanguages(package or "")) Complete (sLangs As String)
    manipulate the sLangs variable
  • SpeakLongText (t As TTS, TextToSpeak As String)
    Speaks long texts more than MaxLength of TTS
    Uses TTS Lib
  • StopSpeakingLongText (t As TTS)
    Stops the speaking of long texts more than MaxLength of TTS
    Uses TTS Lib
  • SetVoice (t As TTS, Voice As String)
    Set a voice to the TTS
    Uses TTS Lib, JavaObject Lib
  • GetVoices (t As TTS, OnlyCurrentLocale As Boolean) As String()
    Get Voices of Current Engine of TTS with option to get the voices of current locale only.
    Uses JavaObject Lib, TTS Lib
  • GetTTSCurrentLocale (t As TTS) As String
    Gets the current locale of the TTS
    Uses JavaObject Lib, TTS Lib, Phone Lib




Cheers

There is a new version of the TTS functions. The Sub SynthesizeToFile has been changed to adapt the changes in the newer androids.
 
Last edited:
Top