B4A Library Another SpeechRecognizer Library

Sample code

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim sr As SpeechToText
    Dim ri As RecognizerIntent
    Dim i As Intent
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Dim EditText1 As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
   
    EditText1.Initialize("")
    EditText1.SingleLine=False
    EditText1.Gravity=Gravity.TOP
    Activity.AddView(EditText1,0,0,100%x,100%y)
 
  
    sr.initialize("sr")
    i.Initialize( ri.ACTION_VOICE_SEARCH_HANDS_FREE,"")
    i.PutExtra(ri.EXTRA_CALLING_PACKAGE,"com.inforpires.testSpeech")
    i.PutExtra(ri.EXTRA_LANGUAGE_MODEL, ri.LANGUAGE_MODEL_FREE_FORM)
    'i.PutExtra(ri.EXTRA_LANGUAGE,"pt_PT")
    i.PutExtra(ri.EXTRA_MAX_RESULTS,3)
    i.PutExtra(ri.EXTRA_PARTIAL_RESULTS,True)
  
End Sub
Sub sr_onResults(results As Object)
  
    Log(results)
    Dim words As List
    'Log(words)
    words.Initialize2(results)
    For Each word As String In words
        Log(word)
        EditText1.Text = EditText1.Text & word
    Next
    EditText1.Text = EditText1.Text & CRLF
  
    sr.startListening(i)
End Sub
Sub sr_onError(erro As Int)

    Log("ERROR : " & erro)
    If erro=8 Then
        sr.stopListening
        sr.destroy
        sr.createSpeechRecognizer
        sr.startListening(i)
    Else
        sr.startListening(i)
    End If  
End Sub

Sub sr_onEndOfSpeech
    Log("End of speech")
    'sr.startListening(i)
End Sub
Sub sr_onRmsChanged(rmsdB As Float)
    'Log("RMS" & rmsdB)
End Sub
Sub Activity_Resume
    If sr.isRecognitionAvailable Then
              
        sr.createSpeechRecognizer
        sr.startListening(i)
        Log("OK")
    Else
        Log("Not OK")
    End If
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    sr.destroy
End Sub

Unzip speech2TextLib.zip into extralibs folder & provide results reports, please.

Nelson Pires
 

Attachments

  • speech2TextLib.zip
    5.1 KB · Views: 1,282
Last edited:

Rusty

Well-Known Member
Licensed User
Longtime User
This works well as straight Text to Speech as it is designed.
Capturing the incoming buffer data
B4X:
Sub sr_onBufferReceived(buffer() As Byte)
    Log("buffer received " & buffer.length)
End Sub
On IceCream Sandwich works great.
On JellyBean it doesn't fire the onBufferReceived event...:(
any ideas how to find the onBufferRecieved event on a modern device?
Thanks,
Rusty

Erel,
Are you aware of any limitations on the buffer received event failing to fire on newer OS's?
Thanks,
Rusty
 
Last edited:

sorex

Expert
Licensed User
Longtime User
I get error 2 in the log. does that mean my i5800 phone doesn't support this feature?

(I have voice dialer tools that came with the OS tho, but never used them)
 

Rusty

Well-Known Member
Licensed User
Longtime User
Here is what I found the Google messages to mean:
B4X:
'An error has occured.
Sub SR_Error(ErrorType As Int)
    SRError = ErrorType
    Select Case ErrorType
        Case SR.RESULT_NO_MATCH            '1
            Log("Error: " & ErrorType & " network timeout")
        Case SR.RESULT_CLIENT_ERROR        '2
            Log("Error: " & ErrorType & " error network")
        Case SR.RESULT_SERVER_ERROR        '3
            Log("Error: " & ErrorType & " error audio recording")
        Case SR.RESULT_NETWORK_ERROR    '4
            Log("Error: " & ErrorType & " error server")
        Case SR.RESULT_AUDIO_ERROR        '5
            Log("Error: " & ErrorType & " error client")       
        Case 6
            Log("Error: " & ErrorType & " speech timeout")       
        Case 7
            Log("Error: " & ErrorType & " no match")
        Case 8
            Log("Error: " & ErrorType & " recognizer busy")
        Case 9
            Log("Error: " & ErrorType & " insufficient permissions")
        Case Else
            Log("Error: " & ErrorType & " Unknown error")
    End Select
Hope this helps.
Rusty
 

sorex

Expert
Licensed User
Longtime User
hmm, my wifi needs to be on to get past that error (thanks for the hint, Rusty)

by now it just gives

ERROR : 7
End of speech
onEndofSpeech
ERROR : 7
End of speech

it seems it doesn't recognize Dutch?
(dutch news was in the background)
 

sorex

Expert
Licensed User
Longtime User
adding

B4X:
i.PutExtra(ri.EXTRA_LANGUAGE,"nl_NL")

made it working, sort of.
 

vpires

Member
Licensed User
Longtime User
@sorex : try uncommenting the line
i.PutExtra(ri.EXTRA_LANGUAGE,"pt_PT"), passing to it nl_NL

@Rusty : quick google shows in stackoverflow :
"Since ICS, onBufferReceived is not called any more. You cannot use speech recognizer and getting audio at the same time. – Hoan Nguyen May 5 '13 at 21:50"

Nelson
 

vpires

Member
Licensed User
Longtime User
Just for fun.
Take the following sample, adapt to your language ( the PT-PT stuff, there are 2 places to change ). Check the speechToText, httpUtils, json & tts libs.
It should : listen to your voice, recognize the text, send it to a on line translation service, speak the returned response.

B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim sr As SpeechToText
    Dim ri As RecognizerIntent
    Dim i As Intent
    Dim tt As TTS
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Dim EditText1,edittext2 As EditText
    Dim pronto=False As Boolean
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("1")
   
    EditText1.Initialize("")
    EditText1.SingleLine=False
    EditText1.Gravity=Gravity.TOP
    Activity.AddView(EditText1,0,0,100%x,50%y-5dip)
   
    edittext2.Initialize("")
    edittext2.SingleLine=False
    edittext2.Gravity=Gravity.TOP
    Activity.AddView(edittext2,0,50%y+5dip,100%x,100%y-10dip)
       
    tt.Initialize("tt")
    tt.SetLanguage("pt","PT")
       
    sr.initialize("sr")
    i.Initialize( ri.ACTION_VOICE_SEARCH_HANDS_FREE,"")
    i.PutExtra(ri.EXTRA_CALLING_PACKAGE,"com.inforpires.testSpeech")
    i.PutExtra(ri.EXTRA_LANGUAGE_MODEL, ri.LANGUAGE_MODEL_FREE_FORM)
   
   
    i.PutExtra(ri.EXTRA_LANGUAGE,"pt_PT")
    i.PutExtra(ri.EXTRA_MAX_RESULTS,3)
    i.PutExtra(ri.EXTRA_PARTIAL_RESULTS,True)
    ' traduz("fogo")
   
End Sub
Sub tt_Ready (Success As Boolean)
    pronto = Success
End Sub
Sub sr_onResults(results As Object)
    Dim frase As String
   
    frase=""
    Dim words As List
    words.Initialize2(results)
    For Each word As String In words
        frase = frase & word
    Next
    EditText1.Text = frase & CRLF & EditText1.text
    If pronto Then
        traduz(frase)
    End If
    sr.startListening(i)
End Sub
Sub sr_onError(erro As Int)

    Log("ERROR : " & erro)
    If erro=8 Then
        sr.stopListening
        sr.destroy
        sr.createSpeechRecognizer
        sr.startListening(i)
    Else
        sr.startListening(i)
    End If   
End Sub
Sub sr_onEndOfSpeech
    Log("End of speech")
    'sr.startListening(i)
End Sub
Sub sr_onRmsChanged(rmsdB As Float)
    'Log("RMS" & rmsdB)
End Sub
Sub Activity_Resume

    If sr.isRecognitionAvailable Then
               
        sr.createSpeechRecognizer
        sr.startListening(i)
        Log("OK")
    Else
        Log("Not OK")
    End If
    If tt.IsInitialized=False Then
        pronto=False
        tt.Initialize("tt")
    End If
   
End Sub
Sub Activity_Pause (UserClosed As Boolean)
    sr.destroy
    tt.Release
End Sub
Sub traduz(frase As String)
    Dim o As HttpJob
   
    o.Initialize("traducao",Me)
    o.Download2("http://api.mymemory.translated.net/get", Array As String("q",frase,"langpair","pt|en"))

End Sub
Sub jobDone(job As HttpJob)
    Dim s As String

    If job.Success Then
        s=job.GetString
        job.Release
        finalTraducao(s)
    Else
        job.Release
        Log("error in response")
    End If
End Sub
Sub finalTraducao(s As String)
    Dim j As JSONParser
    Dim m,m1 As Map
    Dim traduzido As String
   
    j.Initialize(s)
    m=j.NextObject
    m1=m.Get("responseData")

    fala(m1.Get("translatedText"))
End Sub
Sub fala(s As String)
    edittext2.text = s & CRLF & edittext2.Text
    If pronto Then
        tt.Speak(s,False)
    End If
End Sub
 

JakeBullet70

Well-Known Member
Licensed User
Longtime User
I have been really playing with this today and have found something odd.

Sometimes in the LOG window you get 'onEndofSpeech' and the voice engine seems to pause for 15-30 seconds
Then its followed by a 'Speech Recognizer Error: 7 no match' and things start working again.

Any ideas? I would like to start using this in my all but that pause is killing me. ;)

Thanks
 

Attachments

  • upload_2014-2-23_7-54-6.png
    upload_2014-2-23_7-54-6.png
    115.7 KB · Views: 398

Rusty

Well-Known Member
Licensed User
Longtime User
I have been really playing with this today and have found something odd.

Sometimes in the LOG window you get 'onEndofSpeech' and the voice engine seems to pause for 15-30 seconds
Then its followed by a 'Speech Recognizer Error: 7 no match' and things start working again.

Any ideas? I would like to start using this in my all but that pause is killing me. ;)

Thanks
Did you find a solution to this problem? we are having the same with error 7 and error 1 (network timeout).
Anything you found might be helpful.
Thanks,
Rusty
 

Beja

Expert
Licensed User
Longtime User
Hi Nelson,
One problem.. when, for example, I say 2 plus 2, I see on the screen 2 plus 2, instead of 4 ;)

Thanks so much for the great reco lib, and here is what I found:

The recognition is greatly accurate, but....
1- Samsung DUOS GT-18552, running 4.1.2
2- Multiple reco returns.. e.g., when I say ok, it returns something like: OKOkay iOK go.. and when I say five, it returns 5five50... and so on..
It doesn't return the first reco result but other possibilities.. this makes it difficult to design a voice response system.
3- after a few reco sessions, it hangs for a long time.

best.
 

Tem

Member
Licensed User
Longtime User
Hi Nelson,

2- Multiple reco returns.. e.g., when I say ok, it returns something like: OKOkay iOK go.. and when I say five, it returns 5five50... and so on..
It doesn't return the first reco result but other possibilities.. this makes it difficult to design a voice response system.

To get first result try i.e.
Label1.Text = words.Get(0)
 

JakeBullet70

Well-Known Member
Licensed User
Longtime User
Did you find a solution to this problem? we are having the same with error 7 and error 1 (network timeout).
Anything you found might be helpful.
Thanks,
Rusty

No, never did find anything to fix that. I just put my code on the back burner and am hoping to find something in the future.
 

Alberto Iglesias

Well-Known Member
Licensed User
Longtime User
Nelson,

Supoem-se que essa biblioteca funcione para reconhecimento de voz ONLINE, correto?

De novo com as Google Glass, que vem somente com a lingua "ENGLISH" (en) instalada, estou tentando colocar o reconhecimento vocal para Portugues, mas não vai, sempre acaba reconhecido como Ingles.

Ja tentei com a sua biblioteca (retorna sempre erro 6) e com a do Erel, usando VR.Language = "pt" e simplesmente é ignorado, retornando sempre em Ingles.

Testei em um outro android que tem outras linguas instaladas e funciona, ou seja, parece que tem que ter a lingua instalada, mesmo sendo feito de forma ONLINE.

2 Perguntas: Sabe se tem alguma forma de instalar uma lingua a partir do ADB? (Lembrando que NAO tenho Settings no Glass)
Alguma forma 100% ONLINE de reconhecimento vocal?


Obrigadíssimo!
 

Alberto Iglesias

Well-Known Member
Licensed User
Longtime User
Hello Rick,

My problem was the Voice Recognizer when you don´t have anyway to change que configuration, and then I search a way to do this, and I found a solution, but need to apply the "android.permission.CHANGE_CONFIGURATION"

and this configuration, some devices works without "Root", like my Smartwatch for example, but in Google Glass NO, and then I put the instructions, how to do that througt with a cable and ADB.EXE

To apply this permission in NO ROOTED devices, just simple execute this command:

adb shell pm grant com.visualnet.glassbrazil android.permission.CHANGE_CONFIGURATION

where: com.visualnet.glassbrazil is the name of your package and now works very whell

look my 3 apps to change configuration in Google Glass

https://play.google.com/store/apps/details?id=com.visualnet.glassbrazil
https://play.google.com/store/apps/details?id=com.visualnet.glassspain
https://play.google.com/store/apps/details?id=com.visualnet.glassfrench

Now I can SAY "OK GLASS" and "BRAZIL" and then the device can Talk and Listen in Portuguese, easly with no pain!
 

hookshy

Well-Known Member
Licensed User
Longtime User
Can this library work without internet connection ?
How is the speech to text converstion done ?
 
Last edited:

peacemaker

Expert
Licensed User
Longtime User
Strange, lib works OK (online and offline, if offline-language is downloaded) in debug-mode. But when to compile at last the release (or obfuscated release - the same) - no recognition. Who knows what the issue is ?
 
Last edited:

Chris160179

Member
Licensed User
Longtime User
Hallo @all,

would it be possible to add the intent "EXTRA_PREFER_OFFLINE" to this Library?
Or is there a way to edit the library?

At this Moment the intent is not supportet with this library.

Thx an have a nice Sunday
 
Top