expo-wakeword
Version:
Voice/Wake-word detection library for Expo (React Native)
196 lines • 7.75 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.useModel = void 0;
const react_1 = require("react");
const KeyWordRNBridge_1 = require("./KeyWordRNBridge");
var license = "MTczODEwMTYwMDAwMA==-Vmv1jwEG+Fbog9LoblZnVT4TzAXDhZs7l9O18A+8ul8=";
const keyWordRNBridgeInstances = [];
function findInstanceById(id) {
return keyWordRNBridgeInstances.find(config => config.id === id);
}
// Create an array of instance configurations
var instanceConfigs = [
{ id: 'need_help_now', modelName: 'need_help_now.onnx', threshold: 0.9999, bufferCnt: 3, sticky: false },
];
// Helper function to format the ONNX file name
const formatWakeWord = (fileName) => {
return fileName
.replace(/_/g, ' ') // Use global flag to replace all underscores
.replace('.onnx', '')
.replace(/\b\w/g, (char) => char.toUpperCase()); // Capitalize each word
};
// Function to add a new instance dynamically
//async function addInstance(
// conf: instanceConfig)
async function addInstance(conf, callback) {
const id = conf.id;
const instanceConf = findInstanceById(id);
if (instanceConf != null) {
console.log("Found Instance: ", id, "starting to listen");
const instance = instanceConf.instance;
await instance.startKeywordDetection(conf.threshold);
return instance;
}
const instance = await (0, KeyWordRNBridge_1.createKeyWordRNBridgeInstance)(id, conf.sticky);
let isLicesed = false;
if (!instance) {
console.error(`Failed to create instance ${id}`);
return null;
}
console.log(`Instance ${id} created ${instance}`);
await instance.createInstance(conf.modelName, conf.threshold, conf.bufferCnt);
console.log(`Instance ${id} createInstance() called`);
isLicesed = await instance.setKeywordDetectionLicense(license);
console.log(`Instance ${id} created ${instance} and licensed ${isLicesed}`);
keyWordRNBridgeInstances.push({ id, instance });
// Set up event listener
// Clean all listeners keywordRNBridgeEmitter.removeAllListeners('onKeywordDetectionEvent');
const eventListener = instance.onKeywordDetectionEvent((phrase) => {
phrase = formatWakeWord(id);
console.log(`Instance ${id} detected: ${id} with phrase`, phrase);
// callback(phrase); Does not work on IOS
callback(phrase);
});
console.log(`Instance ${id} calling startKeywordDetection()`);
await instance.startKeywordDetection(conf.threshold);
console.log(`Instance ${id} started Keyword Detection`);
return instance;
}
// Function to remove an instance dynamically
function removeInstance(id) {
const instanceIndex = keyWordRNBridgeInstances.findIndex((item) => item.id === id);
if (instanceIndex !== -1) {
const { instance } = keyWordRNBridgeInstances[instanceIndex];
instance
.stopKeywordDetection()
.then(() => instance.destroyInstance())
.then(() => {
instance.removeListeners();
console.log(`Instance ${id} stopped and destroyed`);
keyWordRNBridgeInstances.splice(instanceIndex, 1);
})
.catch((error) => console.error(`Error stopping instance ${id}: ${error.message}`));
}
else {
console.error(`Instance ${id} not found`);
}
}
/**
* Custom hook for handling keyword detection using KeyWordRNBridge
* @returns An object with functions and state for keyword detection
*/
const useModel = () => {
// State to track whether the keyword detection is currently active
const [isListening, setIsListening] = (0, react_1.useState)(false);
let currentEventListener = [];
/**
* Set the keyword detection license
* @param licenseKey - The license key
*/
// const setLicense = useCallback(async (licenseKey: any) => {
// try {
// await KeyWordRNBridge.setKeywordDetectionLicense(licenseKey);
// } catch (error) {
// console.error("[useModel] Error setting license:", error);
// }
// }, []);
/**
* Sets the keyword detection license
* @param licenseKey - The linceseKey
*/
const setKeywordDetectionLicense = (0, react_1.useCallback)(async (licenseKey) => {
license = licenseKey;
}, []);
/**
* Load the keyword detection model
* @param modelFileName - The name of the model file to load
* @param threshold - The detection threshold
* @param bufferCount - The number of audio buffers
*/
const loadModel = (0, react_1.useCallback)(async (useConfigs, callback) => {
console.log("loadModel()");
instanceConfigs = useConfigs;
let element = null;
console.log("loadModel() - instanceConfigs == ", instanceConfigs);
try {
instanceConfigs.forEach(async (element) => {
console.log('Adding element:', element);
const id = await addInstance(element, callback);
});
}
catch (error) {
console.error("[useModel] Error loading model:", error);
}
}, []);
/**
* Stop listening for the keyword
*/
const startListening = (0, react_1.useCallback)(async () => {
try {
keyWordRNBridgeInstances.forEach(async (element) => {
const instance = element.instance;
const conf = instanceConfigs.find(element => element.id === instance.instanceId);
if (conf) {
await instance.startKeywordDetection(conf.threshold);
}
else {
console.error(`No configuration found for instance ID: ${instance.instanceId}`);
}
/*if (instance.isSticky == false) {
instance.stopKeywordDetection();
} else if (Platform.OS != 'ios') {
instance.stopKeywordDetection();
}*/
});
setIsListening(true);
}
catch (error) {
console.error("Error starting keyword detection:", error);
}
}, []);
/**
* Stop listening for the keyword
*/
const stopListening = (0, react_1.useCallback)(async () => {
try {
keyWordRNBridgeInstances.forEach(async (element) => {
const instance = element.instance;
await instance.stopKeywordDetection();
/*if (instance.isSticky == false) {
instance.stopKeywordDetection();
} else if (Platform.OS != 'ios') {
instance.stopKeywordDetection();
}*/
});
setIsListening(false);
}
catch (error) {
console.error("Error stopping keyword detection:", error);
}
}, []);
/**
* Cleanup effect to stop listening when the component unmounts
* or when the isListening state changes
*/
(0, react_1.useEffect)(() => {
console.log("isListening updated:", isListening);
return () => {
if (isListening) {
stopListening();
// Clean all listeners keywordRNBridgeEmitter.removeAllListeners('onKeywordDetectionEvent');
(0, KeyWordRNBridge_1.removeAllRNBridgeListeners)();
}
};
}, [isListening, stopListening]);
// Return an object with the necessary functions and state
return {
isListening,
startListening,
loadModel,
setKeywordDetectionLicense,
stopListening,
};
};
exports.useModel = useModel;
exports.default = exports.useModel; // Add this line to allow default import
//# sourceMappingURL=useModel.js.map