Skip to content

Instantly share code, notes, and snippets.

@svenvdvoort
Created February 17, 2019 10:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save svenvdvoort/f4c69af899b9d061c238635610041b70 to your computer and use it in GitHub Desktop.
Save svenvdvoort/f4c69af899b9d061c238635610041b70 to your computer and use it in GitHub Desktop.
snips-asr-adapter
var mqtt = require('mqtt')
const snipsASR = require('./snips-asr-adapter.js')
var snipsMQTT = mqtt.connect('mqtt://localhost:1883')
const specializedASR = new snipsASR({
port: 1884,
})
Snips ASR error: INFO:snips_asr: Connecting using MQTT site-id default
Snips ASR error: INFO:snips_asr_hermes: Using model from "/usr/share/snips/assistant/custom_asr"
Snips ASR error: INFO:snips_kaldi::decode::model: Loading model v1
Snips ASR error: INFO:snips_asr_hermes : Preparing decoder
Snips ASR error: INFO:snips_asr_hermes : Idle
ASR connected
subscribed, packet: Packet {
cmd: 'subscribe',
retain: false,
qos: 1,
dup: false,
length: 36,
topic: null,
payload: null,
subscriptions: [ { topic: 'hermes/audioServer/+/audioFrame', qos: 0 } ],
messageId: 1 }
# so no output from snips-asr process...
const net = require('net')
const mqtt = require('mqtt')
const mqttCon = require('mqtt-connection')
const { spawn } = require('child_process')
const EventEmitter = require('events')
module.exports = class ASRadapter extends EventEmitter {
constructor(options) {
super()
this.port = options.port || 1884
this.server = new net.Server()
this.server.on('connection', function (stream) {
var client = mqttCon(stream)
// client connected
client.on('connect', function (packet) {
console.log('ASR connected')
// acknowledge the connect packet
client.connack({ returnCode: 0 });
})
// client published
client.on('publish', function (packet) {
console.log('Client published:', packet)
if(packet.topic === 'hermes/asr/textCaptured') {
console.log('captured text, packet:', packet)
this.emit('textCaptured', 'replace this')
}
// send a puback with messageId (for QoS > 0)
if(packet.qos > 0) {
client.puback({ messageId: packet.messageId })
}
})
// client pinged
client.on('pingreq', function () {
// send a pingresp
client.pingresp()
});
// client subscribed
client.on('subscribe', function (packet) {
// send a suback with messageId and granted QoS level
console.log('Client subscribed, packet:', packet)
client.suback({ granted: [packet.qos], messageId: packet.messageId })
})
// timeout idle streams after 5 minutes
stream.setTimeout(1000 * 60 * 5)
// connection error handling
client.on('close', function () {
console.log('Client closed connection')
//client.destroy()
})
client.on('error', function (error) {
console.log('Connection error:', error)
//client.destroy()
})
client.on('disconnect', function () {
console.log('Client disconnected')
//client.destroy()
})
// stream timeout
stream.on('timeout', function () {
console.log('Client connection timed out')
//client.destroy();
})
})
// listen on given port
this.server.listen(this.port, () => {
//now start our very own ASR process
this.ASRprocess = spawn('snips-asr', ['--mqtt', 'localhost:' + this.port])
this.ASRprocess.stdout.on('data', (data) => {
console.log('Snips ASR:', data.toString())
})
this.ASRprocess.stderr.on('data', (data) => {
console.log('Snips ASR error:', data.toString())
})
this.ASRprocess.on('close', (code) => {
console.log('ASR process exited with code ' + code)
})
})
}
//TODO add 'startListening()' and 'stopListening()' functions
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment