npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vosk-speech-recognition-capacitor

v1.0.3

Published

Speech recognition module for capacitor.js using Vosk library

Downloads

303

Readme

vosk-speech-recognition-capacitor

Speech recognition module for capacitor.js using Vosk library

Installation

npm install vosk-speech-recognition-capacitor
npx cap sync

Models

Vosk uses prebuilt models to perform speech recognition offline. You have to download the model(s) that you need on Vosk official website Avoid using too heavy models, because the computation time required to load them into your app could lead to bad user experience. Then, unzip the model in your app folder. If you just need to use the iOS version, put the model folder wherever you want, and import it as described below. If you need both iOS and Android to work, you can avoid to copy the model twice for both projects by importing the model from the Android assets folder in XCode.

Experimental: Loading a model dynamically into the app storage, aside from the main bundle is a new and experimental feature. Would love for you all to test, and let us know if it is a viable option. If you choose to download a model to your app’s storage (preferably internal), you can pass the model directory path when calling vosk.loadModel(path).

To download and load a model as part of an app's Main Bundle, just do as follows:

Android

In Android Studio, open the project manager, right-click on your project folder, and go to New > Folder > Assets folder. Screenshot 2024-10-24 172652

Then, put the model folder inside the assets folder created. In your file tree, it should be located in android/app/src/main/assets. So, if you downloaded the French model named model-fr-fr, you should access the model by going to android/app/src/main/assets/model-fr-fr. In Android Studio, your project structure should look like this:

Screenshot 2024-10-24 172755

You can import as many models as you want.

Permissions

To use the microphone for speech recognition, you need to request the appropriate permissions in your Android app. Make sure to add the following permissions in your AndroidManifest.xml file:

<uses-permission android:name="android.permission.RECORD_AUDIO"/>

API

Methods

| Method | Argument | Return | Description | |----------|-------------------|----------------|--------------------------------------------------------------------------| | loadModel | options: { modelName: string } | Promise<void> | Loads the voice model for recognition; required before using start(). | | unload | none | Promise<void> | Unloads the model and stops the recognizer. | | start | options?: VoskOptions | Promise<void> | Starts the recognizer; triggers onResult() event. | | stop | none | Promise<void> | Stops the recognizer; listener receives the final result if available. | | pause | none | Promise<void> | Pauses the recognizer. | | resume | none | Promise<void> | Resumes the recognizer. | | addListener | eventName: 'onPartialResults' | 'onFinalResults' | 'onResult', listenerFunc | Promise<PluginListenerHandle> | Adds a listener for specific events. | | removeAllListeners | none | Promise<void> | Removes all listeners for the plugin. |

Event Listeners

| Method | Promise return | Description | |---|---|---| | onPartialResults | The recognized word as a string | Called when partial recognition result is available.| | onResult | The recognized word as a string | Called after silence occured. | | onFinalResults | The recognized word as a string | Called after stream end, like a stop() call |

VoskOptions

interface VoskOptions {
  grammar?: string[];  // Set of phrases the recognizer will seek
  timeout?: number;    // Timeout in milliseconds to listen
}

Example Usage

Here’s how to use the API:

import { Vosk } from 'vosk-speech-recognition-capacitor';

// Create a new Vosk instance
const vosk = new Vosk();

// Load the model
vosk.loadModel({ modelName: 'model-en-us' }).then(() => {
  // Start the recognizer
  vosk.start({
    grammar: ['left', 'right', '[unk]'],
    timeout: 5000,
  }).then(() => {
    console.log('Recognizer started successfully');
    
    // Add listener for results
    vosk.addListener('onResult', (data) => {
      console.log('Recognized result:', data.result);
    });
  });
});

Complete example

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

License

MIT