Latest 1.2.0
Homepage https://github.com/icaksama/iSphinx
License MIT
Platforms ios 9.0
Frameworks Foundation, CoreAudio, AVFoundation, CoreMedia
Authors

Creator
Travis
GitHub license
Code Size
Pod Version
Platform
Download Total
Docs

iOS library for offline speech recognition base on Pocketsphinx engine. Add speech recognition feature into your iOS app with Cocoapods. iSphinx gives simple configuration and implementation for your app without dealing with Pocketsphinx assets and configuration.

Features

  • [x] High accuracy
  • [x] Support swift 4 and iOS 9 above
  • [x] Build dictionary on the fly
  • [x] Build language model (Arpa File) on the fly
  • [x] Build JSGF Grammar on the fly
  • [x] Support PCM Recorder 16bits / mono little endian (wav file)
  • [x] Scoring system every single word (range 0.0 – 1.0)
  • [x] Detect unsupported words
  • [x] Rejecting Out-Of-Vocabulary (OOV) based on keyword spotting
  • [x] Speaker Adaptation (in progress)
  • [x] SIMPLE TO USE & FAST!

Preview

I have tried to speak in different word order:

Cocoapods

Add to Podfile :

pod 'iSphinx', '~> 1.2.0'

How to Use

Add Request Permissions

  • Add request permission for recording in your Info.plist

Add The Listener

First, impletent the iSphinxDelegete in your class/ViewController :

import iSphinx

class ViewController: UIViewController, iSphinxDelegete {
    var isphinx: iSphinx = iSphinx()

    override func viewDidLoad() {
        super.viewDidLoad()
        isphinx.delegete = self
    }
}

iSphinxDelegete have some methods must be implement in your class/ViewController :

func iSphinxDidStop(reason: String, code: Int) {
    if code == 500 { // 500 code for error
        print(reason)
    } else if code == 522 { // 522 code for timed out
        print(reason)
    } else if code == 200 { // 200 code for finish speech
        print(reason)
    }
}

func iSphinxFinalResult(result: String, hypArr: [String], scores: [Double]) {
    print("Full Result : (result)")
    // NOTE :
    // [x] parameter "result" : Give final response with ??? values when word out-of-vocabulary.
    // [x] parameter "hypArr" : Give final response in original words without ??? values.

    // Get score from every single word. hypArr length equal with scores length
    for score in scores {
        print(score)
    }

    // Get array word
    for word in hypArr {
        print(word)
    }
}

func iSphinxPartialResult(partialResult: String) {
    print(partialResult)
}

func iSphinxUnsupportedWords(words: [String]) {
    var unsupportedWords = ""
    for word in words {
        unsupportedWords += word + ", "
    }
    print("Unsupported words : (unsupportedWords)")
}

func iSphinxDidSpeechDetected() {
    print("Speech detected!")
}

Prepare Speech Recognition

You need to prepare speech recognition before use that. You can add new parameters in onPreExecute to increase accuracy or performance.

isphinx.prepareISphinx(onPreExecute: { (config) in
    // You can add new parameter pocketshinx here
    self.isphinx.silentToDetect = 1.0
    self.isphinx.isStopAtEndOfSpeech = true
    // config.setString(key: "-parameter", value: "value")
}) { (isSuccess) in
    if isSuccess {
        print("Preparation success!")
    }
}

Update Language Model / Grammar

You can update the vocabulary with language model or JSGF Grammar on the fly. Make sure to remove the punctuation before update vocabulary/grammar. Default punctuation will be remove in iSphinx are (.), (,) ,(?), (!), (_), (-), (), (:)

// Update vocabulary with language model from single string
isphinx.updateVocabulary(text: "YOUR VOCABULARIES!", oovWords: ["WORDS DISTRUBER", ...]) {
    print("Vocabulary updated!")
}

// Update vocabulary with language model from array string
isphinx.updateVocabulary(text: "YOUR VOCABULARIES!", oovWords: ["WORDS DISTRUBER", ...]) {
    print("Vocabulary updated!")
}

// Update vocabulary with JSGF Grammar from string
isphinx.updateGrammar(text: "YOUR GRAMMAR", oogWords: ["WORDS DISTRUBER", ...]) {
    print("Grammar updated!")
}

Start Speech Recognition

You can start speech recognition with timout or not.

// Start speech recognition with Timeout in seconds
isphinx.startISphinx(timeoutInSec: 10)

// Start speech recognition without Timeout
isphinx.startISphinx()

Stop Speech Recognition

You can stop speech recognition manually.

// Stop speech recognition manually.
isphinx.stopISphinx()

Play Audio Record

Make sure play the audio record after speech recognizer is done.

isphinx.getRecorder().play {
    print("Play audio finish!")
}

Note : Please take a look at iSphinxDemo for detail usage.

MIT License

Copyright (c) 2018 Saiful Irham Wicaksana

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Latest podspec

{
    "name": "iSphinx",
    "version": "1.2.0",
    "license": "MIT",
    "summary": "iSphinx is pocketsphinx in swift language for offline speech recognition, gives very simple configuration without dealing with assets.",
    "homepage": "https://github.com/icaksama/iSphinx",
    "social_media_url": "https://www.facebook.com/icaksama.fanpage",
    "authors": {
        "icaksama": "[email protected]"
    },
    "source": {
        "git": "https://github.com/icaksama/iSphinx.git",
        "tag": "1.2.0"
    },
    "platforms": {
        "ios": "9.0"
    },
    "frameworks": [
        "Foundation",
        "CoreAudio",
        "AVFoundation",
        "CoreMedia"
    ],
    "preserve_paths": [
        "iSphinx/Sphinx/include/**/*.{h}",
        "iSphinx/Sphinx/include/*.{modulemap}"
    ],
    "vendored_libraries": [
        "iSphinx/Sphinx/lib/pocketsphinx/libpocketsphinx.a",
        "iSphinx/Sphinx/lib/sphinxbase/libsphinxbase.a"
    ],
    "libraries": [
        "pocketsphinx",
        "sphinxbase"
    ],
    "resource_bundles": {
        "Assets": [
            "iSphinx/iSphinx/Assets/*.{arpa,wav,txt}"
        ],
        "AcousticModel": "iSphinx/iSphinx/Assets/en-us-ptm/*"
    },
    "source_files": [
        "iSphinx/iSphinx/*.{swift,h}",
        "iSphinx/iSphinx/iSphinx Utilities/*.{swift}"
    ],
    "pod_target_xcconfig": {
        "SWIFT_VERSION": "4.0",
        "ENABLE_BITCODE": "NO",
        "SWIFT_OPTIMIZATION_LEVEL": "-Onone",
        "HEADER_SEARCH_PATHS": "${PODS_TARGET_SRCROOT}/iSphinx/Sphinx/Include/**",
        "SWIFT_INCLUDE_PATHS": "${PODS_TARGET_SRCROOT}/iSphinx/Sphinx/Include",
        "LIBRARY_SEARCH_PATHS": "${PODS_TARGET_SRCROOT}/iSphinx/Sphinx/lib/pocketsphinx ${PODS_TARGET_SRCROOT}/iSphinx/Sphinx/lib/sphinxbase",
        "OTHER_LDFLAGS": "-lsphinxbase -lpocketsphinx"
    },
    "xcconfig": {
        "HEADER_SEARCH_PATHS": "${PODS_TARGET_SRCROOT}/iSphinx/Sphinx/Include/**",
        "SWIFT_OPTIMIZATION_LEVEL": "$(inherited)",
        "ENABLE_BITCODE": "$(inherited)"
    }
}

Pin It on Pinterest

Share This