vad.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /** @typedef {import('./types').CircularBufferHandle} CircularBufferHandle */
  2. /** @typedef {import('./types').VoiceActivityDetectorHandle} VoiceActivityDetectorHandle */
  3. /** @typedef {import('./types').SpeechSegment} SpeechSegment */
  4. /** @typedef {import('./types').VadConfig} VadConfig */
  5. const addon = require('./addon.js');
  6. /**
  7. * CircularBuffer stores float32 samples internally.
  8. */
  9. class CircularBuffer {
  10. /**
  11. * @param {number} capacity - capacity in samples (integer)
  12. */
  13. constructor(capacity) {
  14. this.handle = addon.createCircularBuffer(capacity);
  15. }
  16. /**
  17. * Push samples into the buffer.
  18. * @param {Float32Array} samples
  19. */
  20. push(samples) {
  21. addon.circularBufferPush(this.handle, samples);
  22. }
  23. /**
  24. * Get a slice of samples.
  25. * @param {number} startIndex
  26. * @param {number} n
  27. * @param {boolean} [enableExternalBuffer=true]
  28. * @returns {Float32Array}
  29. */
  30. get(startIndex, n, enableExternalBuffer = true) {
  31. return addon.circularBufferGet(
  32. this.handle, startIndex, n, enableExternalBuffer);
  33. }
  34. /**
  35. * Pop n samples from the buffer.
  36. * @param {number} n
  37. */
  38. pop(n) {
  39. return addon.circularBufferPop(this.handle, n);
  40. }
  41. /**
  42. * Get current size in samples.
  43. * @returns {number}
  44. */
  45. size() {
  46. return addon.circularBufferSize(this.handle);
  47. }
  48. /**
  49. * Get head index.
  50. * @returns {number}
  51. */
  52. head() {
  53. return addon.circularBufferHead(this.handle);
  54. }
  55. /** Reset the buffer. */
  56. reset() {
  57. addon.circularBufferReset(this.handle);
  58. }
  59. }
  60. /**
  61. * Voice Activity Detector (VAD).
  62. */
  63. class Vad {
  64. /**
  65. * @param {VadConfig} config
  66. * @param {number} bufferSizeInSeconds
  67. */
  68. constructor(config, bufferSizeInSeconds) {
  69. this.handle =
  70. addon.createVoiceActivityDetector(config, bufferSizeInSeconds);
  71. this.config = config;
  72. }
  73. /**
  74. * Accept raw waveform samples.
  75. * @param {Float32Array} samples
  76. */
  77. acceptWaveform(samples) {
  78. addon.voiceActivityDetectorAcceptWaveform(this.handle, samples);
  79. }
  80. /** @returns {boolean} */
  81. isEmpty() {
  82. return addon.voiceActivityDetectorIsEmpty(this.handle);
  83. }
  84. /** @returns {boolean} */
  85. isDetected() {
  86. return addon.voiceActivityDetectorIsDetected(this.handle);
  87. }
  88. /** Pop the earliest detected speech segment. */
  89. pop() {
  90. addon.voiceActivityDetectorPop(this.handle);
  91. }
  92. /** Clear internal state. */
  93. clear() {
  94. addon.voiceActivityDetectorClear(this.handle);
  95. }
  96. /**
  97. * Get the front speech segment.
  98. * @param {boolean} [enableExternalBuffer=true]
  99. * @returns {SpeechSegment}
  100. */
  101. front(enableExternalBuffer = true) {
  102. return addon.voiceActivityDetectorFront(this.handle, enableExternalBuffer);
  103. }
  104. /** Reset detector state. */
  105. reset() {
  106. addon.voiceActivityDetectorReset(this.handle);
  107. }
  108. /** Flush pending internal buffer. */
  109. flush() {
  110. addon.voiceActivityDetectorFlush(this.handle);
  111. }
  112. }
  113. module.exports = {
  114. Vad,
  115. CircularBuffer,
  116. }