Android Question TTS TextToSpeech problem with Englih

Giusy

Active Member
Licensed User
Hi,
this is my routine
B4X:
Sub TTS1_Ready (Success As Boolean)
    If Success Then
        TTS1.SpeechRate = 0.8
        TTS1.Pitch = 0.7
        Select Case Main.language
            Case 1
            TTS1.SetLanguage("it","IT")
            Case 2
                TTS1.SetLanguage("fr","FR")
                Case 3
                    TTS1.SetLanguage("en","EN")
                    Case 4
                        TTS1.SetLanguage("es","ES")
                       
        End Select
       
    Else
        Msgbox("Error initializing TTS engine.", "")
    End If
End Sub

My default language is Italian, so no problem when main.language = 1
When the language is 2 or 4, he proposed the installation of French and Spanish, so no problem.
But when the language is English, it does not propose me any installation and speaks in Italian.

Is there any error in the routine?

Thanks
 

Giusy

Active Member
Licensed User
Hi,
this is my routine
B4X:
Sub TTS1_Ready (Success As Boolean)
    If Success Then
        TTS1.SpeechRate = 0.8
        TTS1.Pitch = 0.7
        Select Case Main.language
            Case 1
            TTS1.SetLanguage("it","IT")
            Case 2
                TTS1.SetLanguage("fr","FR")
                Case 3
                    TTS1.SetLanguage("en","EN")
                    Case 4
                        TTS1.SetLanguage("es","ES")
                      
        End Select
      
    Else
        Msgbox("Error initializing TTS engine.", "")
    End If
End Sub

My default language is Italian, so no problem when main.language = 1
When the language is 2 or 4, he proposed the installation of French and Spanish, so no problem.
But when the language is English, it does not propose me any installation and speaks in Italian.

Is there any error in the routine?

Thanks
[SOLVED]
with TTS1.SetLanguage("en","") :)
TTS1.SetLanguage("en","EN")
 
Upvote 0

emexes

Expert
Licensed User
Probably something to do with "EN" not being a recognised country code.

"UK" or "US" or "GB" might work, though.

Or if you want some character, perhaps "AU" ;-)
 
Last edited:
Upvote 0

emexes

Expert
Licensed User
Perhaps you can make it auto-detect the language that is being read, by checking the text for the ten most common words of each supported language, and assuming it is whichever language had the most hits.

B4X:
Sub LanguageMatch(StringToRead As String, LanguageWords() As String) As Float

    Dim CheckString As String= " " & StringToRead.ToLowerCase & " "

    Dim Punctuation As String = ",;:.!?()/"
    For I = 0 To Punctuation.Length - 1
        CheckString.Replace(Punctuation.CharAt(I), " ")
    Next I

    Dim NumHits As Int = 0
    For I = 0 To LanguageWords.Length - 1
        If CheckString.Contains(" " & LanguageWords(I) & " ") Then
            NumHits = NumHits + 1
        End If
    Next I

    Return NumHits / LanguageWords.Length

End Sub

Dim EnglishWords() As String = Array Of String("the", "be", "to", "of", "and", "a", "in", "that", "have", "i")
Dim FrenchWords() As String = Array Of String("oui")
Dim GermanWords() As String = Array Of String("ja")

Dim BestLanguage As String = "it"    'default to Italian
Dim BestLanguageMatch As Float = 0

Dim Temp As Float

Temp = LanguageMatch(StringToRead, EnglishWords)
If Temp > BestLanguageMatch Then
    BestLanguage = "en"
    BestLanguageMatch = Temp
End If

Temp = LanguageMatch(StringToRead, FrenchWords)
If Temp > BestLanguageMatch Then
    BestLanguage = "fr"
    BestLanguageMatch = Temp
End If

Temp = LanguageMatch(StringToRead, GermanWords)
If Temp > BestLanguageMatch Then
    BestLanguage = "de"
    BestLanguageMatch = Temp
End If
 
Upvote 0

emexes

Expert
Licensed User
Hi @emexesThanks, I solved the problem :) (see above)
Understood.

Was just confirming that the country code does actually work too: it gives distinctly different accents for US, UK and Australia. Perhaps it does similar for Spanish and Portuguese languages in North/Central/South American countries too.

Also, I quite like having english text read in a French or Italian accent, adds a bit of class to it ;-)
 
Upvote 0

Giusy

Active Member
Licensed User
Understood.

Was just confirming that the country code does actually work too: it gives distinctly different accents for US, UK and Australia. Perhaps it does similar for Spanish and Portuguese languages in North/Central/South American countries too.

Also, I quite like having english text read in a French or Italian accent, adds a bit of class to it ;-)
Thanks @emexes
you are always the best :D
 
Upvote 0
Top