This is a wrapper of Acephei VOSK , With this, you can add continuous offline speech recognition feature to your application,

NOTE:
  1. As it works offline the app should be complied with the voice model. It will increase the app size by 30-40Mb.
  2. The accuracy depends on the voice model. You can train your own voice model. For more details check the models download link below.
  3. Remember to add RECORD_AUDIO permission.
How to use:
  1. Download the required voice model from here.
  2. Change the file name to a simple one like "model.zip"
  3. Copy it to the Files folder of your project.
  4. Now to use that model check the attached example.

SpeechToText

Author:
@Biswajit
Version: 1.5
  • SpeechToText
    • Events:
      • Error (message As String)
      • FinalResult (text As String)
      • MicrophoneBuffer (buffer() As Byte)
      • PartialResult (text As String)
      • Paused (paused As Boolean)
      • ReadyToListen
      • ReadyToListenEx new
      • ReadyToRead
      • Restarted
      • Result (text As String)
    • Fields:
      • sampleRate As Int
        Default 16000
    • Functions:
      • cancel As Boolean
        Cancel microphone recognition. Do not post any new events, simply cancel processing.
        Does nothing if recognition is not active.
        Return type: @return:true if recognition was actually stopped
      • FeedExternalBuffer (ExBuffer As Byte()) new
        For recognizing the external audio buffer, feed the buffer here.
        ExBuffer: The external audio byte buffer.
      • Initialize (eventName As String, modelPath As String)
        Initialize the object.
        eventName: The event name prefix.
        modelPath: The model folder path.
      • pause (pause As Boolean)
        Pause microphone recognition.
        pause: Pass true to pause and false to continue.
      • prepareAudioFile (audioPath As String, predefinedWords As String)
        Prepare the audio file for recognition. On success Eventname_ReadyToRead event will be raised.
        Call startReading to start reading the file.
        audioPath: Audio file path.
        predefinedWords: Add some predefined words/phrase as JSON string. Can be blank.
      • prepareListenerEx (predefinedWords As String) new
        Prepare the listener for external audio buffer. On success Eventname_ReadyToListenEx event will be raised.
        Call startListeningEx to start listening.
        predefinedWords: Add some predefined words/phrase as JSON string. Can be blank.
      • prepareMicrophone (predefinedWords As String)
        Prepare the microphone for listening. On success Eventname_ReadyToListen event will be raised.
        Call startListening to start listening.
        predefinedWords: Add some predefined words/phrase as JSON string. Can be blank.
      • reset
        Resets microphone recognizer in a thread, starts microphone recognition over again
      • shutdown
        Shutdown the microphone recognizer and release the recorder.
        Call this on activity or service closing event.
      • startListening (timeout As Int) As Boolean
        Starts microphone recognition. After specified timeout listening stops and the
        endOfSpeech signals about that. Does nothing if recognition is active.
        timeout: timeout in milliseconds to listen. -1 = infinite;
        Return type: @return:true if recognition was actually started
      • startListeningEx As Boolean new
        Starts external audio buffer recognition.
        Return type: @return:true if recognition was actually started
      • startReading (timeout As Int) As Boolean
        Starts file recognition. After specified timeout listening stops and the
        endOfSpeech signals about that. Does nothing if recognition is active.
        timeout: timeout in milliseconds to listen. -1 = infinite;
        Return type: @return:true if recognition was actually started
      • stop As Boolean
        Stops microphone/file recognition. Listener should receive final result if there is
        any. Does nothing if recognition is not active.
        Call this on activity or service closing event.
        Return type: @return:true if recognition was actually stopped
Downloads:
  1. Library
  2. Example
  3. Voice Model
  4. Test app
Update:
  • Version 1.1:
    1. Added audio file to text functionality. (For now only WAV format is supported)
    2. Added predefined word/phrase detection functionality.
    3. Merged startListening and startListening2 together. Pass -1 for continuous recognition.
  • Version 1.2:
    1. Added MicrophoneBuffer event where you will receive the microphone audio buffer while using voice recognition.
  • Version 1.3:
    1. Added method to change the sampling rate.
  • Version 1.4:
    1. Fixed the app crashing issue while calling shutdown without stating the recognizer
  • Version 1.5:
    1. Added option to feed external audio buffer. Instead of using the internal audio recorder you can feed external audio buffer from another audio source.
      (Check the latest example project)
    2. Updated VOSK and JNA library. (Please delete old dependencies before coping the new ones.)

If you like my work, please donate. Your donations will encourage me to add more features in the future.

 
Last edited:

Biswajit

Active Member
Licensed User
Longtime User
How can I restart the SST engine at runtime after reloading the voice module?

When you are changing the model you have to initialize another instance of the recognizer. Create a list and then store the STT object in it after initializing. Then when you want to load another module just stop it and call shutdown. Then just remove the instance from the list and create a new one then initialize it and store it.
 

zed

Active Member
Licensed User
for some strange reason all my replies are "under moderation, awaiting approval". No idea why that is.
Is that usual behaviour in this forum as regards how new accounts are treated
Because you are not licensed. You should donate. You will be licensed and your messages will be instant
 

Paolo Pini

Member
Licensed User
Longtime User
When you are changing the model you have to initialize another instance of the recognizer. Create a list and then store the STT object in it after initializing. Then when you want to load another module just stop it and call shutdown. Then just remove the instance from the list and create a new one then initialize it and store it.
Thanks for your reply,

you means something like this? (i believe no):

B4X:
Private STTList As List
Private STT As SpeechToText
Private STT_1 As SpeechToText    

STTList.Initialize
    
'first load'
STT.Initialize("STT", File.DirInternal & "/" & model_folder_name)
STTList.Add(STT)
STT_1 = STTList.Get(0)

???
Sub STT_ReadyToListen
    STT_1 .stop
     If STT_1.startListening(-1) Then
         Log("ready...")
     Else
         Log("failed...")
     End If
 end sub
    
'rename module for new language etc.'
.....
STT_1.stop
STT_1.shutdown
        
STTList.RemoveAt(0)
        
STT.Initialize("STT", File.DirInternal & "/" & new_model_folder_name)

STTList.Add(STT)
STT_1 = STTList.Get(0)
 

Biswajit

Active Member
Licensed User
Longtime User
you means something like this?
No. Like this,


B4X:
Private STTList As List   

'first load'
STTList.Initialize
CreateSTT("vosk_model_en_us")

Sub CreateSTT(model_folder_name as String)
    Dim STT As SpeechToText
    STT.Initialize("STT", File.DirInternal & "/" & model_folder_name)
    STTList.Add(STT)
End Sub

Sub STT_ReadyToListen
    Dim STT As SpeechToText = STTList.get(0)
    STT.stop
     If STT.startListening(-1) Then
         Log("ready...")
     Else
         Log("failed...")
     End If
 end sub
    
'rename module for new language etc.'
.....

Dim STT As SpeechToText = STTList.get(0)
STT.stop
STT.shutdown
STTList.RemoveAt(0)

'then
CreateSTT("vosk_model_ru")
 

Guenter Becker

Active Member
Licensed User
This is a wrapper of Acephei VOSK , With this, you can add continuous offline speech recognition feature to your application,

NOTE:
  1. As it works offline the app should be complied with the voice model. It will increase the app size by 30-40Mb.
  2. The accuracy depends on the voice model. You can train your own voice model. For more details check the models download link below.
  3. Remember to add RECORD_AUDIO permission.
How to use:
  1. Download the required voice model from here.
  2. Change the file name to a simple one like "model.zip"
  3. Copy it to the Files folder of your project.
  4. Now to use that model check the attached example.

SpeechToText

Author:
@Biswajit
Version: 1.5
  • SpeechToText
    • Events:
      • Error (message As String)
      • FinalResult (text As String)
      • MicrophoneBuffer (buffer() As Byte)
      • PartialResult (text As String)
      • Paused (paused As Boolean)
      • ReadyToListen
      • ReadyToListenEx new
      • ReadyToRead
      • Restarted
      • Result (text As String)
    • Fields:
      • sampleRate As Int
        Default 16000
    • Functions:
      • cancel As Boolean
        Cancel microphone recognition. Do not post any new events, simply cancel processing.
        Does nothing if recognition is not active.
        Return type: @return:true if recognition was actually stopped
      • FeedExternalBuffer (ExBuffer As Byte()) new
        For recognizing the external audio buffer, feed the buffer here.
        ExBuffer: The external audio byte buffer.
      • Initialize (eventName As String, modelPath As String)
        Initialize the object.
        eventName: The event name prefix.
        modelPath: The model folder path.
      • pause (pause As Boolean)
        Pause microphone recognition.
        pause: Pass true to pause and false to continue.
      • prepareAudioFile (audioPath As String, predefinedWords As String)
        Prepare the audio file for recognition. On success Eventname_ReadyToRead event will be raised.
        Call startReading to start reading the file.
        audioPath: Audio file path.
        predefinedWords: Add some predefined words/phrase as JSON string. Can be blank.
      • prepareListenerEx (predefinedWords As String) new
        Prepare the listener for external audio buffer. On success Eventname_ReadyToListenEx event will be raised.
        Call startListeningEx to start listening.
        predefinedWords: Add some predefined words/phrase as JSON string. Can be blank.
      • prepareMicrophone (predefinedWords As String)
        Prepare the microphone for listening. On success Eventname_ReadyToListen event will be raised.
        Call startListening to start listening.
        predefinedWords: Add some predefined words/phrase as JSON string. Can be blank.
      • reset
        Resets microphone recognizer in a thread, starts microphone recognition over again
      • shutdown
        Shutdown the microphone recognizer and release the recorder.
        Call this on activity or service closing event.
      • startListening (timeout As Int) As Boolean
        Starts microphone recognition. After specified timeout listening stops and the
        endOfSpeech signals about that. Does nothing if recognition is active.
        timeout: timeout in milliseconds to listen. -1 = infinite;
        Return type: @return:true if recognition was actually started
      • startListeningEx As Boolean new
        Starts external audio buffer recognition.
        Return type: @return:true if recognition was actually started
      • startReading (timeout As Int) As Boolean
        Starts file recognition. After specified timeout listening stops and the
        endOfSpeech signals about that. Does nothing if recognition is active.
        timeout: timeout in milliseconds to listen. -1 = infinite;
        Return type: @return:true if recognition was actually started
      • stop As Boolean
        Stops microphone/file recognition. Listener should receive final result if there is
        any. Does nothing if recognition is not active.
        Call this on activity or service closing event.
        Return type: @return:true if recognition was actually stopped
Downloads:
  1. Library
  2. Example
  3. Voice Model
  4. Test app
Update:
  • Version 1.1:
    1. Added audio file to text functionality. (For now only WAV format is supported)
    2. Added predefined word/phrase detection functionality.
    3. Merged startListening and startListening2 together. Pass -1 for continuous recognition.
  • Version 1.2:
    1. Added MicrophoneBuffer event where you will receive the microphone audio buffer while using voice recognition.
  • Version 1.3:
    1. Added method to change the sampling rate.
  • Version 1.4:
    1. Fixed the app crashing issue while calling shutdown without stating the recognizer
  • Version 1.5:
    1. Added option to feed external audio buffer. Instead of using the internal audio recorder you can feed external audio buffer from another audio source.
      (Check the latest example project)
    2. Updated VOSK and JNA library. (Please delete old dependencies before coping the new ones.)

If you like my work, please donate. Your donations will encourage me to add more features in the future.

ERROR BUG? ERROR
Happy Easter,
Using the lib I felt into some big troubles I am not able to solve.

  1. I downloaded all the files named in the post including a new model from the given model page "vosk-model-small-de-0.15.zip"
  2. Installed the test apk and tested it -> working
  3. Used the example project as it is, compiled as release - > working
  4. added "#BridgeLogger: True" to code main

  5. code added:
    #Region  Activity Attributes
        #FullScreen: False
        #IncludeTitle: True
    #End Region
    
    #BridgeLogger: True
  6. renamed "model.zip" of the example project into "modelX.zip"
  7. copied "vosk-modell..." into the files folder and renamed it to "model.zip"

tested project with release and b4A-bridge on same phone samsung s21
  1. app starts requests permissions and shows layout with the buttons
  2. pressing button "text to speech" app crashes! (test of othe buttons same result)

Log output:
Logger verbunden mit:  samsung SM-G998B

--------- beginning of main

*** Service (starter) Create ***

** Service (starter) Start **

** Activity (main) Create (first time) **

** Activity (main) Resume **

** Activity (main) Pause, UserClosed = false **

sending message to waiting queue (activity_permissionresult)

running waiting messages (1)

Model not found

Model zip not found. Copying...

** Activity (main) Resume **

Model zip unzipping...

Unzipping Files: 1

Unzipping Files: 2

Unzipping Files: 3

Unzipping Files: 4

Unzipping Files: 5

Unzipping Files: 6

Unzipping Files: 7

Unzipping Files: 8

Unzipping Files: 9

Unzipping Files: 10

Unzipping Files: 11

Unzipping Files: 12

Unzipping Files: 13

Unzipping Files: 14

Unzipping Files: 15

** Activity (main) Pause, UserClosed = false **

Examing content of the "model.zip" from vosk and original "model.zip" from example project:
the folder name of the vosk model holds the root folder named "vosk-model-small-de-0.15" the root folder of the "model.zip" of the example project is named "model".
Tried to rename the vosk root folder into "model" and tried test again it also fails.

What goes wrong? It seems for me that in the "INIT Sub" of "SpeechToText" the line "STT.Initialize("STT", File.DirInternal&"/"&Main.model_folder_name)" causes the error due to be not able to install/use the model.

At the end the lib is not usuable using downloaded vosk files from the named model page. I tried some other language files same result.

By the way you could help me if you will prepare and send an example project with the functionality to switch between 2 or more langugaes depending on the locale of the phone.

I need your help!
Thank you Günter Becker
 
Last edited:

gezueb

Active Member
Licensed User
Longtime User
You have to uninstall old instances of the app before you try a new app with the same package name. Just recompile does not change whatever is in the DirInternal folder. Maybe that's the problem. Happy Easter, Georg
 

Guenter Becker

Active Member
Licensed User
Hi,
negative I know it and I did it on the phone. Tryied it again, same result App crashes after clicking upper button.
 

drgottjr

Expert
Licensed User
Longtime User
in addition to changing "vosk-model-small-de-0.15.zip" ----> "model.zip",
YOU HAVE TO CHANGE THE NAME INSIDE THE .ZIP ARCHIVE TOO -----> "model.zip".
if you right click on the full name, you can rename it "model.zip".

see the attached. look at the .zip archive with your desktop unzipper. if your unarchiver doesn't let you rename before unzipping, you need to find a different unarchiver.

note: if you look at the default model.zip, the english model, you will see that it's called
model.zip on the inside. when you install a second language, you need to rename to
"model.zip" in 2 places. i just built and ran the example. first, crash like you. then, i rename in 2 places, and no crash. i wished the app "guten morgen", and it was happy.
 

Attachments

  • vosk.png
    vosk.png
    10.2 KB · Views: 101
  • guten_morgen.png
    guten_morgen.png
    8.5 KB · Views: 99

Guenter Becker

Active Member
Licensed User
in addition to changing "vosk-model-small-de-0.15.zip" ----> "model.zip",
YOU HAVE TO CHANGE THE NAME INSIDE THE .ZIP ARCHIVE TOO -----> "model.zip".
if you right click on the full name, you can rename it "model.zip".

see the attached. look at the .zip archive with your desktop unzipper. if your unarchiver doesn't let you rename before unzipping, you need to find a different unarchiver.

note: if you look at the default model.zip, the english model, you will see that it's called
model.zip on the inside. when you install a second language, you need to rename to
"model.zip" in 2 places. i just built and ran the example. first, crash like you. then, i rename in 2 places, and no crash. i wished the app "guten morgen", and it was happy.
Hi there must be something wrong!!
This is the content of the original zip file of the example project:
1712164348193.png

The zip folder is called model.zip and the folder inside is called model! If I follow what you have written I had to name the inside folder also to model.zip. Tested this and the app was not compiled compiler stopped compiling an error.


This is my renamed (vosk) model zip for the german vosk file and insde folder renamed to model:
1712164996727.png


As you see in the log picture of my error post the unzipping is done well by the project but after this the app crahses withou any error report.

I check it again and tried to run with the emulator.
On the run at Class SpeechToText
Sub Init
Log(File.Exists(File.DirInternal, Main.model_folder_name))

STT.Initialize("STT", File.DirInternal&"/"&Main.model_folder_name)
StartBtn.Visible = True
StopBtn.Visible = True
resultPanel.Visible = True
StartBtn.Enabled = True

timer.Initialize("timer",500)
End Sub
An error is logged at line STT.Initialize.... as followes:
logged Error:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create (first time) **
** Activity (main) Resume **
Model found
** Activity (main) Pause, UserClosed = false **
** Activity (speechtotext) Create (first time) **
true
Error occurred on line: 42 (SpeechToText)
java.io.IOException: Failed to create a model
    at org.vosk.Model.<init>(Model.java:14)
    at com.biswajit.vosk.SpeechToText.Initialize(SpeechToText.java:73)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:777)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:354)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:157)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:205)
    at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:262)
    at b4a.example.speechtotext._init(speechtotext.java:382)
    at b4a.example.speechtotext._activity_create(speechtotext.java:374)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:157)
    at b4a.example.speechtotext.afterFirstLayout(speechtotext.java:105)
    at b4a.example.speechtotext.access$000(speechtotext.java:17)
    at b4a.example.speechtotext$WaitForLayout.run(speechtotext.java:83)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loopOnce(Looper.java:201)
    at android.os.Looper.loop(Looper.java:288)
    at android.app.ActivityThread.main(ActivityThread.java:7870)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
 
Last edited:

drgottjr

Expert
Licensed User
Longtime User
my apologies; i meant change vosk-model-small-de-0.15.zip to "model.zip" and then change the root "vosk-model-small-de-0.15" to "model". this seems to be what you have done.

if that is the case, then there is definitely something wrong since:
1) when i initially run the example, i crashed on tapping the top button, but there was a very long crash log in red indicating that the model could not be initialized.
2) when i changed the name of the root in the german language "model.zip" to "model", there was no crash, and the model ran fine.

i remembered from a couple years ago when i first tried the vosk library that the same thing occurred when you wanted to change to a different language. the app is hardcoded to expect "model". regardless of the language you want to use, it has to be called "model".

it is extremely unlikey there is a crash without a post mortem. you may have to run an unfiltered log to see what the exception or error is. if you are not used to the unfiltered log, it will be very, very long, and you will have to plow through each line. the first thing you want to do is click on the "clear" button to clear the log just
before you compile and run. before tapping the top button, clear the log again. immediately there will be a torrent of messages. as soon as the app crashes, you need to grab the vertical scroll bar in the log and drag it back up the top as quickly as you can. otherwise it will just keep scrolling and you will lose whatever crash message there was. once you drag the scroll bar to the top, the logging will appear to stop, and if you "copy all", you can paste the log into notepad for study. visualize this in your head first. once the unfiltered log starts scrolling, you won't be able to catch anything.

when i crashed, there was a regular filtered crash message. something different is happening in your case if there is no visible crash message.
above you say ...causes the error due to be not able to install/use the model.
so you are seeing a message. now you say there is a crash with no message. so i am confused. sorry.
 

Guenter Becker

Active Member
Licensed User
my apologies; i meant change vosk-model-small-de-0.15.zip to "model.zip" and then change the root "vosk-model-small-de-0.15" to "model". this seems to be what you have done.

if that is the case, then there is definitely something wrong since:
1) when i initially run the example, i crashed on tapping the top button, but there was a very long crash log in red indicating that the model could not be initialized.
2) when i changed the name of the root in the german language "model.zip" to "model", there was no crash, and the model ran fine.

i remembered from a couple years ago when i first tried the vosk library that the same thing occurred when you wanted to change to a different language. the app is hardcoded to expect "model". regardless of the language you want to use, it has to be called "model".

it is extremely unlikey there is a crash without a post mortem. you may have to run an unfiltered log to see what the exception or error is. if you are not used to the unfiltered log, it will be very, very long, and you will have to plow through each line. the first thing you want to do is click on the "clear" button to clear the log just
before you compile and run. before tapping the top button, clear the log again. immediately there will be a torrent of messages. as soon as the app crashes, you need to grab the vertical scroll bar in the log and drag it back up the top as quickly as you can. otherwise it will just keep scrolling and you will lose whatever crash message there was. once you drag the scroll bar to the top, the logging will appear to stop, and if you "copy all", you can paste the log into notepad for study. visualize this in your head first. once the unfiltered log starts scrolling, you won't be able to catch anything.

when i crashed, there was a regular filtered crash message. something different is happening in your case if there is no visible crash message.

so you are seeing a message. now you say there is a crash with no message. so i am confused. sorry.

OK, thank you did what you said but no better result. Attached the Log FIle unfiltered.
As I read it seems that the model folder in Internal Folder does not contain any model may be unpacking is not working correct. Don't no why using origin example project.
 

Attachments

  • LOGNotFIltered.txt
    35.7 KB · Views: 158

drgottjr

Expert
Licensed User
Longtime User
the only way to know if your archiver is un-zipping files is to look for the results. they are apparently unzipped in "File.DirInteral/model/"

so, in main, change this:
B4X:
Sub unzip_UnZipProgression(Count As Int, FileName As String)
    Log("Unzipping Files: "&Count)
End Sub

to this:
B4X:
Sub unzip_UnZipProgression(Count As Int, FileName As String)
    Log("Unzipping Files: "&FileName)
End Sub

the original code just gives you a count. the modified version shows you the names of the files as they are un-zipped. pick 1 and look for it in File.DirInternal/model/.
if it's there, the archiver is working. if it's not there, either the archiver is not un-zipping or they have been un-zipped elsewhere.

i changed File.DirInternal/model/ to Starter.rp.GetSafeDirDefaultExternal("")/model/ where i could see everything from the desktop. it works with DirInternal, but you're working blind. see attached view from desktop. everything was there, so un-zipping wasn't the problem.
 

Attachments

  • tree.png
    tree.png
    302.7 KB · Views: 82
Last edited:

Guenter Becker

Active Member
Licensed User
the only way to know if your archiver is un-zipping files is to look for the results. they are apparently unzipped in "File.DirInteral/model/"

so, in main, change this:
B4X:
Sub unzip_UnZipProgression(Count As Int, FileName As String)
    Log("Unzipping Files: "&Count)
End Sub

to this:
B4X:
Sub unzip_UnZipProgression(Count As Int, FileName As String)
    Log("Unzipping Files: "&FileName)
End Sub

the original code just gives you a count. the modified version shows you the names of the files as they are un-zipped. pick 1 and look for it in File.DirInternal/model/.
if it's there, the archiver is working. if it's not there, either the archiver is not un-zipping or they have been un-zipped elsewhere.

i changed File.DirInternal/model/ to Starter.rp.GetSafeDirDefaultExternal("")/model/ where i could see everything from the desktop. it works with DirInternal, but you're working blind. see attached view from desktop. everything was there, so un-zipping wasn't the problem.
Ok, did changes.
in Starter: Public DirInternal = rp.GetSafeDirDefaultExternal("")
in all classes Dir.Internal > starter.dirinternal
changed Unzip Progression
run got this log and result

LOG unzipping:
*** Service (starter) Create ***
/storage/emulated/0/Android/data/STT.example/files
** Service (starter) Start **
** Activity (main) Create (first time) **
** Activity (main) Resume **
Model not found
Model zip found. Unzipping...
Unzipping Files: 1 /data/user/0/STT.example/files/model/model/COPYING
Unzipping Files: 2 /data/user/0/STT.example/files/model/model/README
Unzipping Files: 3 /data/user/0/STT.example/files/model/model/am/final.mdl
Unzipping Files: 4 /data/user/0/STT.example/files/model/model/conf/mfcc.conf
Unzipping Files: 5 /data/user/0/STT.example/files/model/model/conf/model.conf
Unzipping Files: 6 /data/user/0/STT.example/files/model/model/graph/disambig_tid.int
Unzipping Files: 7 /data/user/0/STT.example/files/model/model/graph/Gr.fst
Unzipping Files: 8 /data/user/0/STT.example/files/model/model/graph/HCLr.fst
Unzipping Files: 9 /data/user/0/STT.example/files/model/model/graph/phones/word_boundary.int
Unzipping Files: 10 /data/user/0/STT.example/files/model/model/ivector/final.dubm
Unzipping Files: 11 /data/user/0/STT.example/files/model/model/ivector/final.ie
Unzipping Files: 12 /data/user/0/STT.example/files/model/model/ivector/final.mat
Unzipping Files: 13 /data/user/0/STT.example/files/model/model/ivector/global_cmvn.stats
Unzipping Files: 14 /data/user/0/STT.example/files/model/model/ivector/online_cmvn.conf
Unzipping Files: 15 /data/user/0/STT.example/files/model/model/ivector/splice.conf
1712223613249.png

looked in the folder on the device:
1712223127734.png


Identified errors:
a) archiver unzipping LOG -> ...files/model/model/... think this wrong one model to much. Don't now where it comes from.
b) device files folder -> only model.zip but no model sub folder?
 

drgottjr

Expert
Licensed User
Longtime User
the files are being un-zipped to File.DirInternal but you're looking for them in rp.GetSafeDirDefaultExternal("").
/data/user/0 is File.DirInternal
/storage/emulated/0/Android/data/STT.example/files is rp.GetSafeDirDefaultExternal("")

you did:
in Starter: Public DirInternal = rp.GetSafeDirDefaultExternal("")
in all classes Dir.Internal > starter.dirinternal
this isn't exactly what i suggested, and it's possible you didn't implement it fully (the files are in File.DirInternal but you're looking elsewhere...) i changed references to File.DirInternal to rp.GetSafeDirDefaultExternal(""). you approached the change differently.

did you delete the project before making the changes? you should, otherwise the files had already been un-zipped to File.DirInternal in your previous attempts. (by the way, you can see that the archiver does work.)

did you recently go back and download the 4 files from the developer's post #1 many pages ago? i downloaded them a couple days ago and built clean from them. the only change i made in order to avoid the "failed to create model" crash was to change vosk-model-small-de-0.15.zip to "model.zip" and then change the root "vosk-model-small-de-0.15" to "model".

yes, you have 1 "model" too much. how that happened i don't know.

zip the project in its current state and post it. just the b4a project itself. you need to remove model.zip from the assets before trying to post (41MB, too big for the forum).
important:
1) do not simply delete model.zip from the files folder. this confuses the IDE.
2) first make a backup of model.zip or make sure you already have one.
3) then remove model.zip in the IDE's file manager tab. this is what the IDE wants.
4) save the project
5) export the project and post

i need to run what you're trying to run. if your project is not the example, just say so now. i'm expecting to see the example plus, at most, a few log() statements you may have added. if you've incorporating the example into a larger project, then we're talking about something different. i'm interested in getting the example, as posted by the developer, to run for you. if i can run it, you should be able to too.
 

Guenter Becker

Active Member
Licensed User
the files are being un-zipped to File.DirInternal but you're looking for them in rp.GetSafeDirDefaultExternal("").
/data/user/0 is File.DirInternal
/storage/emulated/0/Android/data/STT.example/files is rp.GetSafeDirDefaultExternal("")

you did:

this isn't exactly what i suggested, and it's possible you didn't implement it fully (the files are in File.DirInternal but you're looking elsewhere...) i changed references to File.DirInternal to rp.GetSafeDirDefaultExternal(""). you approached the change differently.

did you delete the project before making the changes? you should, otherwise the files had already been un-zipped to File.DirInternal in your previous attempts. (by the way, you can see that the archiver does work.)

did you recently go back and download the 4 files from the developer's post #1 many pages ago? i downloaded them a couple days ago and built clean from them. the only change i made in order to avoid the "failed to create model" crash was to change vosk-model-small-de-0.15.zip to "model.zip" and then change the root "vosk-model-small-de-0.15" to "model".

yes, you have 1 "model" too much. how that happened i don't know.

zip the project in its current state and post it. just the b4a project itself. you need to remove model.zip from the assets before trying to post (41MB, too big for the forum).
important:
1) do not simply delete model.zip from the files folder. this confuses the IDE.
2) first make a backup of model.zip or make sure you already have one.
3) then remove model.zip in the IDE's file manager tab. this is what the IDE wants.
4) save the project
5) export the project and post

i need to run what you're trying to run. if your project is not the example, just say so now. i'm expecting to see the example plus, at most, a few log() statements you may have added. if you've incorporating the example into a larger project, then we're talking about something different. i'm interested in getting the example, as posted by the developer, to run for you. if i can run it, you should be able to too.
 

Guenter Becker

Active Member
Licensed User
Thank you,

ok all back to the roots. Deleted all my work from PC and Device.
Fresh Reloading of all named files in the post.
Done some small modification to code in example project.
Testet project on device without success.

Createt Project ZIP FIle. Find Text File Action.Text in the Project folder to get information about what I have done and Log output.
Please download file: Download here
 

Paolo Pini

Member
Licensed User
Longtime User
Im not sure to have understood your problem, anyway I can say how i managed my application STT+TTS to recognise and speak in many language.

1 - I saved all language model inside the app.
2024-04-05 13_43_29-SpeechToText_OTG - B4A.png


B4X:
'**************************************
Sub Process_Globals
    Private xui As XUI
    Public model_folder_name As String = "model"
    Private model_zip_name As String = "model.zip"
    
    Private rp As RuntimePermissions
    Private timer As Timer
    Dim awake As PhoneWakeState
    Dim STTList As List
    
    'STT
    Private STT As SpeechToText
    
    'TTS
    Dim TTS1 As TTS = Null
    Dim AppLang As String
    Dim Lang As String
    
    Dim FlgUsaTTS As Boolean = True
    
    Dim CfgData As Map
End Sub

then I call this routine that unzip the model file depending on the language required.

B4X:
'**************************************
Sub CreateSTT(Lingua As String)
    Log("CREATESTT")
    
    Dim STT As SpeechToText
    
    File.Copy(File.DirAssets, "model_" & Lingua & ".zip", File.DirInternal, model_zip_name)
    Dim ar As Archiver
    ar.AsyncUnZip(File.DirInternal,model_zip_name,File.DirInternal,"unzip")
    
    Log(Lingua)
    
    'unzip_UnZipDone
    Wait For unzip_UnZipDone(Completed As Boolean, Files As Int)
    
    If Completed=True Then
        STT.Initialize("STT", File.DirInternal & "/" & model_folder_name)
        STTList.Add(STT)
    End If
    
    STT.prepareMicrophone("")
    
    If FlgUsaTTS Then
        If TTS1.IsInitialized = False Then
            Log("init TTS")
            TTS1.Initialize("TTS1")
            TTS1.SetLanguage(Lingua, "")
            TTS1.Speak("START"), True)
        Else
            Log("TTS already")
            TTS1.SetLanguage(Lingua, "")
            TTS1.Speak("READY"), True)
        End If
    End If
End Sub

B4X:
'**************************************
Sub unzip_UnZipDone(CompletedWithoutError As Boolean, NbOfFiles As Int)
    If CompletedWithoutError Then
        ProgressDialogHide
        Log("Unzip completed")
    Else
        Log("Unzip Failed")
        MsgboxAsync("Failed","")
        Wait For MsgBox_Result (Result As Int)
    End If
    File.Delete(File.DirInternal,model_zip_name)
End Sub

'**************************************
Sub unzip_UnZipProgression(Count As Int, FileName As String)
    Log("Unzip File: "&Count)
End Sub

I hope this can be helpful.
 
Top