Skip to content

orthagonal/electron-voice

Repository files navigation

electron-voice

A fast, complete speech-to-text interface for Electron

  • works with any Vosk model
  • runs the audio device in a dedicated process separate from Electron browser, so it is very fast and not subject to any browser security limitations
  • sends identified speech back to Electron using the Neon bridge
  • can do generalized speech recognition
  • can take in a custom grammar to limit the recognized words

You can install the project with npm. In the project directory, run:

$ npm install

This fully installs the project, including installing any dependencies and running the build.

Building vosk-electron

If you have already installed the project and only want to run the build, run:

$ npm run build

This command uses the cargo-cp-artifact utility to run the Rust build and copy the built library into ./index.node.

electron_main.js Example

// import the module just like any other node dependency
const voiceModule = require('index.node');  

// define a handler that gets called any time new words are identified
function onWordsFound(words) {
  console.log(words);
}
// resets the recognizer to prevent it from getting bogged down
const MAX_WORDS = 25;

// get list of devices
const devices = voiceModule.listDevices();
// select a device from the list:
voiceModule.setMicName(devices[2]);
// download models from https://alphacephei.com/vosk/models
voiceModule.setPathToModel('vosk-model-small-en-us-0.15');
// you can specify a grammar to limit the recognized words and
// improve accuracy, this step is optional
voiceModule.setGrammar('["hello", "world"]');
voiceModule.startListener(onWordsFound, MAX_WORDS); 
// wait for the model to load, the big ones can take a few minutes:    
let interval;
interval = setInterval(() => {
  if (voiceModule.isModelLoaded()) {
    console.log('speech model loaded!');
    mainWindow.webContents.send('modelReady', true);
    clearInterval(interval);
  }
}, 1000);

index.node

The Node addon—i.e., a binary Node module—generated by building the project. This is the main module for this package, as dictated by the "main" key in package.json.

Under the hood, a Node addon is a dynamically-linked shared object. The "build" script produces this file by copying it from within the target/ directory, which is where the Rust build produces the shared object.

Learn More

To learn more about Neon, see the Neon documentation.

To learn more about Rust, see the Rust documentation.

To learn more about Node, see the Node documentation.

for different platforms:

rustup default stable-x86_64-pc-windows-msvc # For 64-bit rustup default stable-i686-pc-windows-msvc # For 32-bit

binaries: https://github.com/alphacep/vosk-api/releases

models: https://alphacephei.com/vosk/models

About

real-time voice-to-text in electron

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published