pocketsphinx on Android

Introduction

Current tutorial describes a most recent updated verison which is available through subversion pocketsphinx-android-demo. Later we will provide a link for direct downloading.

PocketSphinx Android demo

Building and running from Eclipse

You should have ADT plugin installed. Also will need Android SDK installed either manually or with ADT plugin.

  1. Checkout the source code from Subversion.
  2. Go to File → Import → Android → Existing Android Code Into Workspace.
     Import existing project
  3. Specify path to the source directory.
     Chose the source directory
  4. Go to Project → Properties → Android and set the project build target.
     Build target
  5. Go to Window → Show View → Other → LogCat to open logcat window.
     LogCat view
  6. Go to “Run → Run” to run the application. You must have physical device attached or virtual device setup. Make sure you attached the content of logcat if you are asking questions on the forum.
     LogCat content

Building and running from command-line

  1. Checkout the source code.
  2. Attach your physical device or setup a virtual device.
  3. Run
    ant debug install

    It will build and install application signed with a debug key. To build and install a release version run

    ant release install
  4. Manually run the application from the device application menu.

Using pocketsphinx-android

Referencing the library in Android project

Put the obtained jar (the one without ”-nolib” suffix) into “libs” directory of your Android project. It should be automatically referenced either by a building script or by IDE - depending on what you are using. Currently the only officially support IDE-s are Android Studio and Eclipse (see below).

Due to a nasty bug in ADT plugin it is currently impossible to include pocketsphinx-android as a single jar. You will have to use ”-nolib” version of the library and add shared libraries manually. For example, if you are building application for ARM architecture, you will have “libs/pocketsphinx-android-0.8-nolib.jar” and “libs/armeabi/libpocketsphinx_jni.so”.

Including resource files

The standard way to ship resource files with your application in Android is to put them in assets/ directory of your project. But in order to make them available for pocketsphinx files should have physical path, as long as they are within .apk they don't have one. SphinxUtil from pocketsphinx-android provides two methods to automatically copy asset files to external storage of the target device.

  1. SphinxUtil#copyAssets. Recursively copies files from an assets directory of .apk to external storage and returns the root of the destination directory. This method is quite slow, so we don't recommend it for production. Method accepts application context and the starting path within assets/ directory.
  2. SphinxUtil#syncAssets. Synchronizes resources reading items from assets.lst file located on the top assets/. Before copying it matches MD5 checksums of an asset and a file on external storage with the same name if such exists. It only does actualy copying if there is incomplete information (no file on external storage, no any of two .md5 files) or there is hash mismatch. PocketSphinxAndroidDemo contains ant script that generates assets.lst as well as .md5 files, look for assets.xml.

Sample application

The classes and methods of pocketsphinx-android were designed to resemble the same workflow used in pocketsphinx, except that basic data structures organized into classes and functions working with them are turned into methods of the corresponding classes. So if you are familiar with pocketsphinx you should feel comfortable with pocketsphinx-android too.

Decoder is an entry-point class to the decoding process. It accepts an instance of Config class whose properties should be filled beforehand. The properties keys and values are the same as those are passed in command-line to pocketsphinx binaries. Read more about tweaking pocketsphinx performance.

Config config = Decoder.defaultConfig();
config.setFloat("-samprate", 8000);
config.setInt("-maxhmmpf", 10000);
config.setBoolean("-bestpath", false);
config.setBoolean("-remove_noise", false);
 
Decoder decoder = new Decoder(config);

Decoding loop is also pretty similar to the native examples:

decoder.startUtt(null);
short[] buffer = new short[1024];
int nread;
 
while ((nread = recorder.read(buffer)) > 0) {
    decoder.processRaw(buffer, nread, false, false);
 
decoder.endUtt();    
Hypothesis hypothesis = decoder.hyp();    
String text = hypothesis.getHypstr();
int score = hypothesis.getBestScore();

Building pocketsphinx-android

You shouldn't build unless you understand what you are doing. Use prebuilt binaries instead.

Build dependencies

Building steps

  1. Create empty directory and set it as the current working directory.
  2. Checkout pocketsphinx-android sources.
  3. Checkout pocketsphinx sources.
  4. Checkout sphinxbase sources.
  5. Create new file “build.properties” and provide the following properties:
    • sdk.dir - path to Android SDK
    • sdk.vsn - Android SDK version
    • ndk.dir - path to Android NDK
  6. Go to “pocketsphinx-android” and run
    ant jar
Make sure you have “pocketsphinx-android” in the same parent directory as “pocketsphinx” and “sphinxbase” are as the building script refers them using exactly these names. If done right you will find multiple shared libraries in “lib” - each in the directory with the target platform name, and two jars in “build”.
 
tutorialandroid.txt · Last modified: 2014/02/05 09:05 by admin
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki