B4A Class Android Speech Recognition API Wrapper

Here is a wrapper for the Android speech recognition API using JavaObject complete with Source code.

It's based on the code found here: http://www.truiton.com/2014/06/android-speech-recognition-without-dialog-custom-activity/

There is not much to it, if you create a new project, you just need to add the Record Audio permission to your Manifest:

B4X:
AddPermission(android.permission.RECORD_AUDIO)

It supposedly works offline if you download a language pack, but I haven't tried that yet.

A very basic Gui is provided, but you can soon get rid of that if you prefer.

You can see in the code where to change the Language Preference. I'm afraid the best list I could find is on this page: http://developer.android.com/reference/java/util/Locale.html

Full documentation is here: http://developer.android.com/reference/android/speech/SpeechRecognizer.html

Let me know how you get on with it.
 

Attachments

  • sr.zip
    9.6 KB · Views: 756
Last edited:

rboeck

Well-Known Member
Licensed User
Today i got a Google installation on my device; the update is from yesterday - 2 sept 2016 - and the pause delay after recognition is completly away!! Now i works for me as ever wished.
 

canalrun

Well-Known Member
Licensed User
In post #7 of this thread:
https://www.b4x.com/android/forum/threads/android-speech-recognition-api-wrapper.62959/#post-414575

I uploaded a test app for B4A that tests stevel05's library.

I have an App that is essentially not much more than this test app. When I use my App on all five of my test phones and my tablet it works almost 100% of the time, but when friends use it on their phones (having approximately the same capabilities on their phones), or if I use it on their phones, sometimes it works, sometimes it doesn't recognize the end of speech, sometimes it abruptly stops before the end of speech, and sometimes it does not return results.

My test devices range from Android 4.x to 5.x. The friends phones are similar, or even the same model of phone. My tablet is 6.0.1.

It does not seem to be an android version problem.

My test devices work connected to Wi-Fi or cellular data.

I have tested on my friends phones and it doesn't work consistently on their phones when I speak.

Any ideas of what the problem could be?
Could people download and quickly try the test app in post #7?

Thanks,
Barry.
 

Syd Wright

Well-Known Member
Licensed User
In post #7 of this thread:
https://www.b4x.com/android/forum/threads/android-speech-recognition-api-wrapper.62959/#post-414575

I uploaded a test app for B4A that tests stevel05's library.

I have an App that is essentially not much more than this test app. When I use my App on all five of my test phones and my tablet it works almost 100% of the time, but when friends use it on their phones (having approximately the same capabilities on their phones), or if I use it on their phones, sometimes it works, sometimes it doesn't recognize the end of speech, sometimes it abruptly stops before the end of speech, and sometimes it does not return results.

My test devices range from Android 4.x to 5.x. The friends phones are similar, or even the same model of phone. My tablet is 6.0.1.

It does not seem to be an android version problem.

My test devices work connected to Wi-Fi or cellular data.

I have tested on my friends phones and it doesn't work consistently on their phones when I speak.

Any ideas of what the problem could be?
Could people download and quickly try the test app in post #7?

Thanks,
Barry.
The problem is caused by the Google App. Since about March this year there have been severe problems with the speech recognition part of this app (which is needed to offer speech recognition in any app because the separate Google Voice app is no longer available). Many developers have reported these problems. Only recently (a couple of weeks ago) most problems were resolved by Google. Make sure that your users have at least Google App version 6.3.38 installed! That should solve most problems.
 

JackKirk

Well-Known Member
Licensed User
I have taken stevel05's class, totally tidied it up, added full beep management, made it able to handle offline speech recognition and documented the bejesus out of it - see attached zip containing class and ultra simple example of usage.

Some notes on beep management:
  • This is done via the XSpeechRecognizer_Beep_Mute_Time parameter of the class's Initialize method.
  • If set to 0 you will understand the need for beep muting.
  • If set to 800 (as suggested by Rick Harris in this post:
    https://www.b4x.com/android/forum/t...chrecognizer-library.27409/page-3#post-220692
    then this will typically eliminate all but a miniscule beep every so often.
  • If set to a value of 4000-6000 or higher you can totally eliminate beeps - at this level restoration of music volume is occurring at a slower rate than occurrence of error number 6 meaning music volume is never restored until the final Flush in Activity_Pause.
  • In my view this is not responsible behaviour - if something else wants to use the music channel then the user won't hear it - not sure what that could be (any comment here?)
  • If you were OK with this behaviour then you could simplify the class by taking out all the music muting/restoration and just mute in the Start method and restore in the Flush method.
Some notes on offline speech recognition:
  • This was the whole reason for my embarking on this exercise.
  • It is simply achieved via the following statement in the classes Start method:
    SpeechRecognizerIntent.PutExtra("android.speech.extra.PREFER_OFFLINE", True)
  • But to take advantage of it you have to make sure the phone is configured for offline speech recognition, the user notes in my app on this subject are as follows:
...you may be able to improve performance by configuring your phone for offline speech recognition - this stops Android offloading speech recognition to a remote server, eliminating reliance on (possibly flakey) mobile networks and significantly reducing latency.

Offline speech recognition is a fairly recent Android enhancement which may or may not be available on your phone - to implement it on a Samsung S5 running Android 6 (Marshmallow):

●Ensure you have Google app installed - yes, there is an app simply called 'Google' - go to [Play Store] and search for 'Google' - it should be first hit.
●Go to [Settings]/[Language and input]/[Google voice typing].
●Under [Languages] ensure you have language checked that you want speech recognition to be done in.
●Under [Offline speech recognition] ensure you have downloaded language pack that matches that checked in previous step (look under [ALL] tab for downloadable language packs).
●Test by turning on [Flight mode] then ...

Happy coding...

EDIT

I have found that there has recently been a change of behaviour of speech recognition - at least on my Samsung S5 - it no longer raises ERROR_SPEECH_TIMEOUT onError event when a speech timeout actually occurs - as a consequence (using my original effort) you get about a 5 sec window to say what you have to say and then it goes dead.

I have updated my zip (XSpeechRecognizer2.zip) which solves this rather weird development by simulating a ERROR_SPEECH_TIMEOUT onError event at a user defined interval.

If you set this interval to less than about 5000 millisecs everything works as before.


EDIT 2

As of 12 Jun 17 I have found that the problem referred to in the above edit no longer applies - looks like they have fixed it.

So you can either rip out anything that refers to:

Obj_force_speech_timeout
XSpeechRecognizer_Speech_Timeout

Or, when initializing:

Obj_speech.Initialize(Me, "Event_obj_speech", 800, 0)

if the last parameter is set to 0 it turns the (now redundant) fix off.


A NEW OBSERVATION

As of 12 Jun 17 (and probably before) on my Samsung S5, if you use the statement:

RecognizeSpeech_Intent.PutExtra("android.speech.extra.PREFER_OFFLINE", True)

in the Start procedure, this now seems to mean "you can only do offline" rather than the previous "you can do either offline or online".

Happy coding...
 

Attachments

  • XSpeechRecognizer2.zip
    12.5 KB · Views: 335
Last edited:

JohnC

Expert
Licensed User
Is there a way to use an audio file (of recorded speech) as the input audio/speech (to be translated) instead of using the microphone with this lib?
 

canalrun

Well-Known Member
Licensed User
Last edited:

xpectmore

Member
Licensed User
I have taken stevel05's class, totally tidied it up, added full beep management, made it able to handle offline speech recognition and documented the bejesus out of it - see attached zip containing class and ultra simple example of usage.

Some notes on beep management:
  • This is done via the XSpeechRecognizer_Beep_Mute_Time parameter of the class's Initialize method.
  • If set to 0 you will understand the need for beep muting.
  • If set to 800 (as suggested by Rick Harris in this post:
    https://www.b4x.com/android/forum/t...chrecognizer-library.27409/page-3#post-220692
    then this will typically eliminate all but a miniscule beep every so often.
  • If set to a value of 4000-6000 or higher you can totally eliminate beeps - at this level restoration of music volume is occurring at a slower rate than occurrence of error number 6 meaning music volume is never restored until the final Flush in Activity_Pause.
  • In my view this is not responsible behaviour - if something else wants to use the music channel then the user won't hear it - not sure what that could be (any comment here?)
  • If you were OK with this behaviour then you could simplify the class by taking out all the music muting/restoration and just mute in the Start method and restore in the Flush method.
Some notes on offline speech recognition:
  • This was the whole reason for my embarking on this exercise.
  • It is simply achieved via the following statement in the classes Start method:
    SpeechRecognizerIntent.PutExtra("android.speech.extra.PREFER_OFFLINE", True)
  • But to take advantage of it you have to make sure the phone is configured for offline speech recognition, the user notes in my app on this subject are as follows:


Happy coding...
good job and Steve's too...
for the problem of increase volume/sensitivity of microphone i suggest
http://stackoverflow.com/questions/...ivity-while-recording-audio-in-android-solved
with my 0 java limited experience and almost 5% b4a i am not able to do it

or
B4X:
void     setStreamVolume(int streamType, int index, int flags)

Sets the volume index for a particular stream.
in
https://developer.android.com/reference/android/media/AudioManager.html
or
https://developer.android.com/reference/android/media/AudioManager.html#setStreamVolume(int, int, int)
 
Last edited:

Rusty

Well-Known Member
Licensed User
Is there a way to turn off the microphone?
I am able to initialize; start; receive results; issue a StopListening, but the RMS input keeps firing... To me, this implies the microphone is still listening...
Thanks in advance,
Rusty
 

Rusty

Well-Known Member
Licensed User
Is there a way to find out the supported languages both online and offline?
Thanks
Rusty
 

stevel05

Expert
Licensed User
There is a link to a list of supported languages in the first post, I don't know if this has been updated since.
 

Rusty

Well-Known Member
Licensed User
Thanks Steve,
I missed that...
But even with that, I'm not sure how to create a "request" and receive a list of installed language packs...
Any ideas?
Rusty
 

stevel05

Expert
Licensed User
No I couldn't find the answer quickly. Search google and see if you can find some Java code that will do what you want.
 

Multiverse app

Active Member
Licensed User
This is a small (my first ever) wrap of Jackkirk's work.
Added:
  • onReadyForSpeech,
  • onPartialResults,
  • onBufferReceived,
  • Now, The Voice recognition automatically detects language and gives results accordingly. Works both offline and online.
  • More accurate results
 

Attachments

  • XSpeechRecognizer.zip
    16 KB · Views: 337

xpectmore

Member
Licensed User
It's a very nice work and i tested several Speech recognizer and all XSpeechRecognizer versions for 2 weeks in several combinations ...
suppressing that beep is useless,it's also nice work,it's working!

I made some tests for a BASIC b4a AI (artificial intelligence) project and one version ( i made using all resources i found in this forum ) had : the speech recognizer on ,then there is a class strCPU with parsing grabbed text and is returning the expected text for a expected phrase using REGEX ,then is there a tts (a GOOGLE one with the answer) put on a MediaPlayerStream mp.Play and in the same time i found a way (not a headset) while is tts playing i still use the recognizer seccond time to detect words as : "stop" ,"stay" ,"excuse me" and a name : "Cora" (to stop current mp.Play).
At this point if i use that music suppress to suppress beeps i think i close tts as well , tts i need.
SO in this stupid case - my case- your beep suppress is useless
 
Top