keyword-spotter.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /** @typedef {import('./types').KeywordSpotterConfig} KeywordSpotterConfig */
  2. /** @typedef {import('./types').KeywordSpotterHandle} KeywordSpotterHandle */
  3. /** @typedef {import('./types').KeywordResult} KeywordResult */
  4. /** @typedef {import('./streaming-asr').OnlineStream} OnlineStream */
  5. const addon = require('./addon.js');
  6. const streaming_asr = require('./streaming-asr.js');
  7. /**
  8. * KeywordSpotter handles keyword detection.
  9. */
  10. class KeywordSpotter {
  11. /**
  12. * @param {KeywordSpotterConfig} config
  13. */
  14. constructor(config) {
  15. this.handle = addon.createKeywordSpotter(config);
  16. this.config = config
  17. }
  18. /**
  19. * Create an OnlineStream for the spotter.
  20. * @returns {OnlineStream}
  21. */
  22. createStream() {
  23. const handle = addon.createKeywordStream(this.handle);
  24. return new streaming_asr.OnlineStream(handle);
  25. }
  26. /**
  27. * @param {OnlineStream} stream
  28. * @returns {boolean}
  29. */
  30. isReady(stream) {
  31. return addon.isKeywordStreamReady(this.handle, stream.handle);
  32. }
  33. /**
  34. * Trigger decode on a stream.
  35. * @param {OnlineStream} stream
  36. */
  37. decode(stream) {
  38. addon.decodeKeywordStream(this.handle, stream.handle);
  39. }
  40. /**
  41. * Reset a stream.
  42. * @param {OnlineStream} stream
  43. */
  44. reset(stream) {
  45. addon.resetKeywordStream(this.handle, stream.handle);
  46. }
  47. /**
  48. * Get the keyword result for a stream.
  49. * @param {OnlineStream} stream
  50. * @returns {KeywordResult}
  51. */
  52. getResult(stream) {
  53. const jsonStr = addon.getKeywordResultAsJson(this.handle, stream.handle);
  54. return JSON.parse(jsonStr);
  55. }
  56. }
  57. module.exports = {
  58. KeywordSpotter,
  59. }