BrunoMoureau / PocketSphinxXam

Speech Recognition with PocketSphinx in Xamarin.Forms project (only available for Android)
Other
3 stars 4 forks source link

PocketSphinxXam

Description

Speech Recognition with PocketSphinx in Xamarin.Forms project (only available for Android)

Here's an example of a Xamarin.Forms speech recognition project using PocketSphinx. I made it with the speech recognition engine PocketSphinx from CMUSphinx (website : https://cmusphinx.github.io/).

I compiled the library and generated the wrapper C#-C following the explanations on their website. I added the libraries (.so) to the project and wrote some logic to use it (inspirated of the java classes distributed on the website).

How to compile

I built this for Xamarin.Android on Windows 10 and I am not used to other operating systems...

The first thing you need to do is to download several files and tools to perform the compilation.

Download

IDE


Android Studio

Projects


SphinxBase

PocketSphinx

PocketSphinx-Android

Tools


Gradle

Swig

Java SDK

Installation

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.1'
    }
}

apply plugin: 'com.android.library'

android {
    compileSdkVersion 27                    // Your Android SDK version
    buildToolsVersion "27.0.3"                  // Your Android SDK Tools version

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 27                 // Your Android SDK version
        versionCode 5
        versionName "5prealpha"
        setProperty("archivesBaseName", "pocketsphinx-android-$versionName")
    externalNativeBuild {
        cmake {
        }
    }
        ndk {
        abiFilters 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a'
    }
    }
    externalNativeBuild {
    cmake {
            path "CMakeLists.txt"
    }
    }
}

task mkdir {
    doLast {
        new File('build/generated-src/csharp').mkdirs()     // New folder called csharp instead of java
        new File('build/generated-src/cpp').mkdirs()
    }
}

def sphinxbase_home = "$System.env.SPINXBASE_HOME"      // The environment variable used to locate sphinxbase-master repository
def pocketsphinx_home = "$System.env.POCKETSPHINX_HOME"     // The environment variable used to locate pocketsphinx-master repository

task swigSb(type: Exec) {                   // SphinxBase swig task to build the C# wrapper and the android native library '.so'
    commandLine 'swig',
    "-I$sphinxbase_home/include", "-I$sphinxbase_home/swig",
    "-csharp", "-dllimport", "android-pocketsphinx.so", "-namespace", "SphinxBase",
    "-outdir", "build/generated-src/csharp", "-o", "build/generated-src/cpp/sphinxbase_wrap.c",
    "$sphinxbase_home/swig/sphinxbase.i"
}

task swigPs(type: Exec) {                   // PocketSphinx swig task to build the C# wrapper and the android native library '.so'
    commandLine 'swig',
        "-I$sphinxbase_home/swig",
        "-I$pocketsphinx_home/include",
        "-I$pocketsphinx_home/swig",
        "-csharp", "-dllimport", "android-pocketsphinx.so", "-namespace", "PocketSphinx",
        "-outdir", "build/generated-src/csharp", "-o", "build/generated-src/cpp/pocketsphinx_wrap.c",
        "$pocketsphinx_home/swig/pocketsphinx.i"
}

preBuild.dependsOn mkdir, swigSb, swigPs

This speech recognition engine is under license :

Copyright (c) 1999-2016 Carnegie Mellon University. All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

This work was supported in part by funding from the Defense Advanced Research Projects Agency and the National Science Foundation of the United States of America, and the CMU Sphinx Speech Consortium.

THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.