Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

VADReportingService.ts 9.7KB

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