UNPKG

sonus-api-caller

Version:

Open source cross platform decentralized always-on framework

97 lines (76 loc) 2.41 kB
'use strict' const record = require('node-record-lpcm16') const stream = require('stream') const { Detector, Models } = require('snowboy') const ERROR = { NOT_STARTED: "NOT_STARTED", INVALID_INDEX: "INVALID_INDEX" } const Sonus = {} Sonus.annyang = require('./lib/annyang-core.js') Sonus.init = (options) => { // don't mutate options const opts = Object.assign({}, options), models = new Models(), sonus = new stream.Writable() sonus.mic = {} sonus.recordProgram = opts.recordProgram sonus.device = opts.device sonus.started = false // If we don't have any hotwords passed in, add the default global model opts.hotwords = opts.hotwords || [1] opts.hotwords.forEach(model => { models.add({ file: model.file || 'node_modules/snowboy/resources/snowboy.umdl', sensitivity: model.sensitivity || '0.5', hotwords: model.hotword || 'default' }) }) // defaults opts.models = models opts.resource = opts.resource || 'node_modules/snowboy/resources/common.res' opts.audioGain = opts.audioGain || 2.0 opts.language = opts.language || 'en-US' //https://cloud.google.com/speech/docs/languages const detector = sonus.detector = new Detector(opts) detector.on('silence', () => sonus.emit('silence')) detector.on('sound', () => sonus.emit('sound')) // When a hotword is detected pipe the audio stream to speech detection detector.on('hotword', (index, hotword) => { sonus.trigger(index, hotword) }) sonus.trigger = (index, hotword) => { if (sonus.started) { try { let triggerHotword = (index == 0) ? hotword : models.lookup(index) sonus.emit('hotword', index, triggerHotword) console.log("here we should ping an api") } catch (e) { throw ERROR.INVALID_INDEX } } else { throw ERROR.NOT_STARTED } } sonus.pause = () => { record.pause() } sonus.resume = () => { record.resume() } return sonus } Sonus.start = sonus => { sonus.mic = record.start({ threshold: 0, device: sonus.device || null, recordProgram: sonus.recordProgram || "rec", verbose: false }) sonus.mic.pipe(sonus.detector) sonus.started = true } Sonus.trigger = (sonus, index, hotword) => sonus.trigger(index, hotword) Sonus.pause = () => record.pause() Sonus.resume = () => record.resume() Sonus.stop = () => record.stop() module.exports = Sonus