您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

VADReportingService.js 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. import { getLogger } from '@jitsi/logger';
  2. import EventEmitter from 'events';
  3. import * as DetectionEvents from './DetectionEvents';
  4. import TrackVADEmitter from './TrackVADEmitter';
  5. const logger = getLogger(__filename);
  6. /**
  7. * Sample rate used by TrackVADEmitter, this value determines how often the ScriptProcessorNode is going to call the
  8. * process audio function and with what sample size.
  9. * Basically lower values mean more callbacks with lower processing times bigger values less callbacks with longer
  10. * processing times. This value is somewhere in the middle, so we strike a balance between flooding with callbacks
  11. * and processing time. Possible values 256, 512, 1024, 2048, 4096, 8192, 16384. Passing other values will default
  12. * to closes neighbor.
  13. */
  14. const SCRIPT_NODE_SAMPLE_RATE = 4096;
  15. /**
  16. * Voice activity detection reporting service. The service create TrackVADEmitters for the provided devices and
  17. * publishes an average of their VAD score over the specified interval via EventEmitter.
  18. * The service is not reusable if destroyed a new one needs to be created, i.e. when a new device is added to the system
  19. * a new service needs to be created and the old discarded.
  20. */
  21. export default class VADReportingService extends EventEmitter {
  22. /**
  23. *
  24. * @param {number} intervalDelay - Delay at which to publish VAD score for monitored devices.
  25. *
  26. * @constructor
  27. */
  28. constructor(intervalDelay) {
  29. super();
  30. /**
  31. * Map containing context for devices currently being monitored by the reporting service.
  32. */
  33. this._contextMap = new Map();
  34. /**
  35. * State flag, check if the instance was destroyed.
  36. */
  37. this._destroyed = false;
  38. /**
  39. * Delay at which to publish VAD score for monitored devices.
  40. */
  41. this._intervalDelay = intervalDelay;
  42. /**
  43. * Identifier for the interval publishing stats on the set interval.
  44. */
  45. this._intervalId = null;
  46. logger.log(`Constructed VADReportingService with publish interval of: ${intervalDelay}`);
  47. }
  48. /**
  49. * Factory methods that creates the TrackVADEmitters for the associated array of devices and instantiates
  50. * a VADReportingService.
  51. *
  52. * @param {Array<MediaDeviceInfo>} micDeviceList - Device list that is monitored inside the service.
  53. * @param {number} intervalDelay - Delay at which to publish VAD score for monitored devices.
  54. * @param {Object} createVADProcessor - Function that creates a Voice activity detection processor. The processor
  55. * needs to implement the following functions:
  56. * - <tt>getSampleLength()</tt> - Returns the sample size accepted by getSampleLength.
  57. * - <tt>getRequiredPCMFrequency()</tt> - Returns the PCM frequency at which the processor operates.
  58. * - <tt>calculateAudioFrameVAD(pcmSample)</tt> - Process a 32 float pcm sample of getSampleLength size.
  59. *
  60. * @returns {Promise<VADReportingService>}
  61. */
  62. static create(micDeviceList, intervalDelay, createVADProcessor) {
  63. const vadReportingService = new VADReportingService(intervalDelay);
  64. const emitterPromiseArray = [];
  65. const audioDeviceList = micDeviceList.filter(device => device.kind === 'audioinput');
  66. // Create a TrackVADEmitter for each provided audio input device.
  67. for (const micDevice of audioDeviceList) {
  68. logger.log(`Initializing VAD context for mic: ${micDevice.label} -> ${micDevice.deviceId}`);
  69. const emitterPromise = createVADProcessor()
  70. .then(rnnoiseProcessor =>
  71. TrackVADEmitter.create(micDevice.deviceId, SCRIPT_NODE_SAMPLE_RATE, rnnoiseProcessor))
  72. .then(emitter => {
  73. emitter.on(
  74. DetectionEvents.VAD_SCORE_PUBLISHED,
  75. vadReportingService._devicePublishVADScore.bind(vadReportingService)
  76. );
  77. emitter.start();
  78. return {
  79. vadEmitter: emitter,
  80. deviceInfo: micDevice,
  81. scoreArray: []
  82. };
  83. });
  84. emitterPromiseArray.push(emitterPromise);
  85. }
  86. // Once all the TrackVADEmitter promises are resolved get the ones that were successfully initialized and start
  87. // monitoring them.
  88. return Promise.allSettled(emitterPromiseArray).then(outcomeArray => {
  89. const successfulPromises = outcomeArray.filter(p => p.status === 'fulfilled');
  90. const rejectedPromises = outcomeArray.filter(p => p.status === 'rejected');
  91. const availableDeviceContexts = successfulPromises.map(p => p.value);
  92. const rejectReasons = rejectedPromises.map(p => p.value);
  93. for (const reason of rejectReasons) {
  94. logger.error('Failed to acquire audio device with error: ', reason);
  95. }
  96. vadReportingService._setVADContextArray(availableDeviceContexts);
  97. vadReportingService._startPublish();
  98. return vadReportingService;
  99. });
  100. }
  101. /**
  102. * Destroy TrackVADEmitters and clear the context map.
  103. *
  104. * @returns {void}
  105. */
  106. _clearContextMap() {
  107. for (const vadContext of this._contextMap.values()) {
  108. vadContext.vadEmitter.destroy();
  109. }
  110. this._contextMap.clear();
  111. }
  112. /**
  113. * Set the watched device contexts.
  114. *
  115. * @param {Array<VADDeviceContext>} vadContextArray - List of mics.
  116. * @returns {void}
  117. */
  118. _setVADContextArray(vadContextArray) {
  119. for (const vadContext of vadContextArray) {
  120. this._contextMap.set(vadContext.deviceInfo.deviceId, vadContext);
  121. }
  122. }
  123. /**
  124. * Start the setInterval reporting process.
  125. *
  126. * @returns {void}.
  127. */
  128. _startPublish() {
  129. logger.log('VADReportingService started publishing.');
  130. this._intervalId = setInterval(() => {
  131. this._reportVadScore();
  132. }, this._intervalDelay);
  133. }
  134. /**
  135. * Function called at set interval with selected compute. The result will be published on the set callback.
  136. *
  137. * @returns {void}
  138. * @fires VAD_REPORT_PUBLISHED
  139. */
  140. _reportVadScore() {
  141. const vadComputeScoreArray = [];
  142. const computeTimestamp = Date.now();
  143. // Go through each device and compute cumulated VAD score.
  144. for (const [ deviceId, vadContext ] of this._contextMap) {
  145. const nrOfVADScores = vadContext.scoreArray.length;
  146. let vadSum = 0;
  147. vadContext.scoreArray.forEach(vadScore => {
  148. vadSum += vadScore.score;
  149. });
  150. // TODO For now we just calculate the average score for each device, more compute algorithms will be added.
  151. const avgVAD = vadSum / nrOfVADScores;
  152. vadContext.scoreArray = [];
  153. vadComputeScoreArray.push({
  154. timestamp: computeTimestamp,
  155. score: avgVAD,
  156. deviceId
  157. });
  158. }
  159. logger.log('VADReportingService reported.', vadComputeScoreArray);
  160. /**
  161. * Once the computation for all the tracked devices is done, fire an event containing all the necessary
  162. * information.
  163. *
  164. * @event VAD_REPORT_PUBLISHED
  165. * @type Array<Object> with the following structure:
  166. * @property {Date} timestamp - Timestamo at which the compute took place.
  167. * @property {number} avgVAD - Average VAD score over monitored period of time.
  168. * @property {string} deviceId - Associate local audio device ID.
  169. */
  170. this.emit(DetectionEvents.VAD_REPORT_PUBLISHED, vadComputeScoreArray);
  171. }
  172. /**
  173. * Callback method passed to vad emitters in order to publish their score.
  174. *
  175. * @param {Object} vadScore -VAD score emitted by.
  176. * @param {Date} vadScore.timestamp - Exact time at which processed PCM sample was generated.
  177. * @param {number} vadScore.score - VAD score on a scale from 0 to 1 (i.e. 0.7).
  178. * @param {string} vadScore.deviceId - Device id of the associated track.
  179. * @returns {void}
  180. * @listens VAD_SCORE_PUBLISHED
  181. */
  182. _devicePublishVADScore(vadScore) {
  183. const context = this._contextMap.get(vadScore.deviceId);
  184. if (context) {
  185. context.scoreArray.push(vadScore);
  186. }
  187. }
  188. /**
  189. * Destroy the VADReportingService, stops the setInterval reporting, destroys the emitters and clears the map.
  190. * After this call the instance is no longer usable.
  191. *
  192. * @returns {void}.
  193. */
  194. destroy() {
  195. if (this._destroyed) {
  196. return;
  197. }
  198. logger.log('Destroying VADReportingService.');
  199. if (this._intervalId) {
  200. clearInterval(this._intervalId);
  201. this._intervalId = null;
  202. }
  203. this._clearContextMap();
  204. this._destroyed = true;
  205. }
  206. }