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

@hakerzp/node-personal-wakeword

v1.1.2

Published

Personal wake word detector

Downloads

13

Readme

node-personal-wakeword

Based on https://medium.com/snips-ai/machine-learning-on-voice-a-gentle-introduction-with-snips-personal-wake-word-detector-133bd6fb568e

Installation

npm i @hakerzp/node-personal-wakeword

Usage

const WakewordDetector = require('@hakerzp/node-personal-wakeword')
const Mic              = require('mic')
const Stream           = require('stream')

async function main() {
  // Create a new wakeword detection engine
  let detector = new WakewordDetector({
    /*
    sampleRate: 16000,
    bitLength: 16,
    frameShiftMS: 10.0,
    frameLengthMS: 30.0, // Must be a multiple of frameShiftMS
    vad: true, // Use VAD detection
    vadMode: WakewordDetector.VadMode.AGGRESSIVE, // See node-vad modes
    vadDebounceTime: 500,
    band: 5, // DTW window width
    ref: 0.22, // See Snips paper for explanation about this parameter
    preEmphasisCoefficient: 0.97, // Pre-emphasis ratio
    */
    threshold: 0.5 // Default value
  })

  // *****

  // KEYWORD MANAGEMENT

  // Add a new keyword using multiple "templates"
  await detector.addKeyword('alexa', [
    // WAV templates (trimmed with no noise!)
    './keywords/alexa1.wav',
    './keywords/alexa2.wav',
    './keywords/alexa3.wav'
  ], {
    // Options
    disableAveraging: true, // Disabled by default, disable templates averaging (note that resources consumption will increase)
    threshold: 0.52 // Per keyword threshold
  })

  // Keywords can be enabled/disabled at runtime
  detector.disableKeyword('alexa')
  detector.enableKeyword('alexa')

  // *****

  // EVENTS

  // The detector will emit a "ready" event when its internal audio frame buffer is filled
  detector.on('ready', () => {
    console.log('listening...')
  })

  // The detector will emit an "error" event when it encounters an error (VAD, feature extraction, etc.)
  detector.on('error', err => {
    console.error(err.stack)
  })

  // The detector will emit a "vad-silence" event when no voice is heard
  detector.on('vad-silence', () => {
    console.log('Hearing silence...')
  })

  // The detector will emit a "vad-voice" event when it hears a voice
  detector.on('vad-voice', () => {
    console.log('Hearing voices...')
  })

  // The detector will emit a "data" event when it has detected a keyword in the audio stream
  /* The event payload is:
    {
      "keyword"     : "alexa", // The detected keyword
      "score"       : 0.56878768987, // The detection score
      "threshold"   : 0.5, // The detection threshold used (global or keyword)
      "frames"      : 89, // The number of audio frames used in the detection
      "timestamp"   : 1592574404789, // The detection timestamp (ms)
      "audioData"   : <Buffer> // The utterance audio data (can be written to a file for debugging)
    }
  */
  detector.on('data', ({keyword, score, threshold, timestamp}) => {
    console.log(`Detected "${keyword}" with score ${score} / ${threshold}`)
  })

  // Note that as the detector is a transform stream the standard "data" event also works...
  // I just added the "keyword" event for clarity :)

  // *****

  // STREAMS

  // As an alternative to events, the detector is a transform stream that takes audio buffers in and output keyword detection payload
  const detectionStream = new Stream.Writable({
    objectMode: true,
    write: (data, enc, done) => {
      // `data` is equivalent to "data" event payload
      console.log(data)
      done()
    }
  })

  detector.pipe(detectionStream)

  // *****

  // Create an audio stream from an audio recorder (arecord, sox, etc.)
  let recorder = Mic({
    channels      : detector.channels, // Defaults to 1
    rate          : detector.sampleRate, // Defaults to 16000
    bitwidth      : detector.bitLength // Defaults to 16
  })

  let stream = recorder.getAudioStream()

  // Pipe to wakeword detector
  stream.pipe(detector)

  recorder.start()

  // Destroy the recorder and detector after 10s
  setTimeout(() => {
    stream.unpipe(detector)
    stream.removeAllListeners()
    stream.destroy()
    stream = null

    recorder = null

    detector.removeAllListeners()
    detector.destroy()
    detector = null
  }, 10000)
}

main()