Skip to content

Instantly share code, notes, and snippets.

@Hermitter
Last active June 19, 2018 19:09
Show Gist options
  • Save Hermitter/bd2ac06270a09c942dd18fd8b2b207a3 to your computer and use it in GitHub Desktop.
Save Hermitter/bd2ac06270a09c942dd18fd8b2b207a3 to your computer and use it in GitHub Desktop.
MATRIX CORE Wakeword Audio Play Example
// INITIAL VARIABLES \\
var matrix_ip = '127.0.0.1';// Local Device IP
var matrix_wakeword_base_port = 60001; // Wakeword base port
var matrix_io = require('matrix-protos').matrix_io;// MATRIX Protocol Buffers
var zmq = require('zeromq');// Asynchronous Messaging Framework
// ASSETS FOLDER \\
// Pocket Spinx Language Modeling Files (obtained from http://www.speech.cs.cmu.edu/tools/lmtool-new.html)
const LM_PATH = '/home/pi/assets/9854.lm';// CHANGE THIS NAME TO YOUR FILE NAME
const DIC_PATH = '/home/pi/assets/9854.dic';// CHANGE THIS NAME TO YOUR FILE NAME
//Sound File (the one you want to play)
const SOUND_FILE = '/home/pi/assets/sound.wav';// CHANGE THIS NAME TO YOUR FILE NAME
// BASE PORT \\
// Create a Pusher socket
var configSocket = zmq.socket('push');
// Connect Pusher to Base port
configSocket.connect('tcp://' + matrix_ip + ':' + matrix_wakeword_base_port /* config */);
// Create driver configuration
var config = matrix_io.malos.v1.driver.DriverConfig.create(
{ // Create & Set wakeword configurations
wakeword: matrix_io.malos.v1.io.WakeWordParams.create({
lmPath: LM_PATH,// Language model file path
dicPath: DIC_PATH,// Dictation file path
channel: matrix_io.malos.v1.io.WakeWordParams.MicChannel.channel8,// Desired MATRIX microphone
enableVerbose: false// Enable verbose option
})
});
// Send configuration to MATRIX device
configSocket.send(matrix_io.malos.v1.driver.DriverConfig.encode(config).finish());
console.log('Listening for wakewords');
// DATA UPDATE PORT \\
// Create a Subscriber socket
var updateSocket = zmq.socket('sub');
// Connect Subscriber to Base port
updateSocket.connect('tcp://' + matrix_ip + ':' + (matrix_wakeword_base_port + 3));
// Subscribe to messages
updateSocket.subscribe('');
// On Message
updateSocket.on('message', function(wakeword_buffer) {
// Extract message
var wakeWordData = matrix_io.malos.v1.io.WakeWordParams.decode(wakeword_buffer);
// Run actions based on the phrase heard
switch(wakeWordData.wakeWord) {
// Play audio file voice command
case "MIA PLAY SOUND":
playSound(SOUND_FILE);
break;
// Test wakeword voice command
case "MIA HEAR ME":
console.log('I heard you');
break;
}
});
// FUNCTIONS \\
// - Play audio file
function playSound(sound_file_path) {
// Start a child process
const execSync = require('child_process').execSync;
// Execute command to play audio file in child process
code = execSync("aplay " + sound_file_path);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment