|
|
@@ -1,284 +0,0 @@
|
|
1
|
|
-// @flow
|
|
2
|
|
-
|
|
3
|
|
-import { createRnnoiseProcessorPromise } from '../../../../rnnoise';
|
|
4
|
|
-import EventEmitter from 'events';
|
|
5
|
|
-import logger from '../../logger';
|
|
6
|
|
-import JitsiMeetJS, { JitsiDetectionEvents } from '../../../lib-jitsi-meet';
|
|
7
|
|
-import { VAD_REPORT_PUBLISHED } from './Events';
|
|
8
|
|
-
|
|
9
|
|
-/**
|
|
10
|
|
- * Sample rate used by TrackVADEmitter, this value determines how often the ScriptProcessorNode is going to call the
|
|
11
|
|
- * process audio function and with what sample size.
|
|
12
|
|
- * Basically lower values mean more callbacks with lower processing times bigger values less callbacks with longer
|
|
13
|
|
- * processing times. This value is somewhere in the middle, so we strike a balance between flooding with callbacks
|
|
14
|
|
- * and processing time. Possible values 256, 512, 1024, 2048, 4096, 8192, 16384. Passing other values will default
|
|
15
|
|
- * to closes neighbor.
|
|
16
|
|
- */
|
|
17
|
|
-const SCRIPT_NODE_SAMPLE_RATE = 4096;
|
|
18
|
|
-
|
|
19
|
|
-/**
|
|
20
|
|
- * Context that contains the emitter and additional information about the device.
|
|
21
|
|
- */
|
|
22
|
|
-type VADDeviceContext = {
|
|
23
|
|
-
|
|
24
|
|
- /**
|
|
25
|
|
- * MediaDeviceInfo for associated context
|
|
26
|
|
- */
|
|
27
|
|
- deviceInfo: MediaDeviceInfo,
|
|
28
|
|
-
|
|
29
|
|
- /**
|
|
30
|
|
- * Array with VAD scores publish from the emitter.
|
|
31
|
|
- */
|
|
32
|
|
- scoreArray: Array<Object>,
|
|
33
|
|
-
|
|
34
|
|
- /**
|
|
35
|
|
- * TrackVADEmitter associated with media device
|
|
36
|
|
- */
|
|
37
|
|
- vadEmitter: Object
|
|
38
|
|
-};
|
|
39
|
|
-
|
|
40
|
|
-/**
|
|
41
|
|
- * Voice activity detection reporting service. The service create TrackVADEmitters for the provided devices and
|
|
42
|
|
- * publishes an average of their VAD score over the specified interval via EventEmitter.
|
|
43
|
|
- * 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
|
|
44
|
|
- * a new service needs to be created and the old discarded.
|
|
45
|
|
- */
|
|
46
|
|
-export default class VADReportingService extends EventEmitter {
|
|
47
|
|
- /**
|
|
48
|
|
- * Map containing context for devices currently being monitored by the reporting service.
|
|
49
|
|
- */
|
|
50
|
|
- _contextMap: Map<string, VADDeviceContext>;
|
|
51
|
|
-
|
|
52
|
|
- /**
|
|
53
|
|
- * State flag, check if the instance was destroyed.
|
|
54
|
|
- */
|
|
55
|
|
- _destroyed: boolean = false;
|
|
56
|
|
-
|
|
57
|
|
- /**
|
|
58
|
|
- * Delay at which to publish VAD score for monitored devices.
|
|
59
|
|
- */
|
|
60
|
|
- _intervalDelay: number;
|
|
61
|
|
-
|
|
62
|
|
- /**
|
|
63
|
|
- * Identifier for the interval publishing stats on the set interval.
|
|
64
|
|
- */
|
|
65
|
|
- _intervalId: ?IntervalID;
|
|
66
|
|
-
|
|
67
|
|
- /**
|
|
68
|
|
- * Constructor.
|
|
69
|
|
- *
|
|
70
|
|
- * @param {number} intervalDelay - Delay at which to publish VAD score for monitored devices.
|
|
71
|
|
- * @param {Function} publishScoreCallBack - Function called on the specific interval with the calculated VAD score.
|
|
72
|
|
- */
|
|
73
|
|
- constructor(intervalDelay: number) {
|
|
74
|
|
- super();
|
|
75
|
|
- this._contextMap = new Map();
|
|
76
|
|
- this._intervalDelay = intervalDelay;
|
|
77
|
|
-
|
|
78
|
|
- logger.log(`Constructed VADReportingService with publish interval of: ${intervalDelay}`);
|
|
79
|
|
- }
|
|
80
|
|
-
|
|
81
|
|
- /**
|
|
82
|
|
- * Factory methods that creates the TrackVADEmitters for the associated array of devices and instantiates
|
|
83
|
|
- * a VADReportingService.
|
|
84
|
|
- *
|
|
85
|
|
- * @param {Array<MediaDeviceInfo>} micDeviceList - Device list that is monitored inside the service.
|
|
86
|
|
- * @param {number} intervalDelay - Delay at which to publish VAD score for monitored devices.
|
|
87
|
|
- * @param {Function} publishScoreCallBack - Function called on the specific interval with the calculated VAD score.
|
|
88
|
|
- *
|
|
89
|
|
- * @returns {Promise<VADReportingService>}
|
|
90
|
|
- */
|
|
91
|
|
- static async create(micDeviceList: Array<MediaDeviceInfo>, intervalDelay: number) {
|
|
92
|
|
- const vadReportingService = new VADReportingService(intervalDelay);
|
|
93
|
|
- const emitterPromiseArray = [];
|
|
94
|
|
-
|
|
95
|
|
- // Create a TrackVADEmitter for each provided audioinput device.
|
|
96
|
|
- for (const micDevice of micDeviceList) {
|
|
97
|
|
- if (micDevice.kind !== 'audioinput') {
|
|
98
|
|
- logger.warn(`Provided device ${micDevice.label} -> ${micDevice.deviceId}, is not audioinput ignoring!`);
|
|
99
|
|
-
|
|
100
|
|
- return;
|
|
101
|
|
- }
|
|
102
|
|
-
|
|
103
|
|
- logger.log(`Initializing VAD context for mic: ${micDevice.label} -> ${micDevice.deviceId}`);
|
|
104
|
|
-
|
|
105
|
|
- const rnnoiseProcessor = await createRnnoiseProcessorPromise();
|
|
106
|
|
-
|
|
107
|
|
- const emitterPromise = JitsiMeetJS.createTrackVADEmitter(
|
|
108
|
|
- micDevice.deviceId,
|
|
109
|
|
- SCRIPT_NODE_SAMPLE_RATE,
|
|
110
|
|
- rnnoiseProcessor
|
|
111
|
|
- ).then(emitter => {
|
|
112
|
|
- emitter.on(
|
|
113
|
|
- JitsiDetectionEvents.VAD_SCORE_PUBLISHED,
|
|
114
|
|
- vadReportingService._devicePublishVADScore.bind(vadReportingService)
|
|
115
|
|
- );
|
|
116
|
|
-
|
|
117
|
|
- return {
|
|
118
|
|
- vadEmitter: emitter,
|
|
119
|
|
- deviceInfo: micDevice,
|
|
120
|
|
- scoreArray: []
|
|
121
|
|
- };
|
|
122
|
|
- });
|
|
123
|
|
-
|
|
124
|
|
- emitterPromiseArray.push(emitterPromise);
|
|
125
|
|
- }
|
|
126
|
|
-
|
|
127
|
|
- // Once all the TrackVADEmitter promises are resolved check if all of them resolved properly if not reject
|
|
128
|
|
- // the promise and clear the already created emitters.
|
|
129
|
|
- // $FlowFixMe - allSettled is not part of flow prototype even though it's a valid Promise function
|
|
130
|
|
- return Promise.allSettled(emitterPromiseArray).then(outcomeArray => {
|
|
131
|
|
- const vadContextArray = [];
|
|
132
|
|
- const rejectedEmitterPromiseArray = [];
|
|
133
|
|
-
|
|
134
|
|
- for (const outcome of outcomeArray) {
|
|
135
|
|
- if (outcome.status === 'fulfilled') {
|
|
136
|
|
- vadContextArray.push(outcome.value);
|
|
137
|
|
- } else {
|
|
138
|
|
- // Promise was rejected.
|
|
139
|
|
- logger.error(`Create TrackVADEmitter promise failed with ${outcome.reason}`);
|
|
140
|
|
-
|
|
141
|
|
- rejectedEmitterPromiseArray.push(outcome);
|
|
142
|
|
- }
|
|
143
|
|
- }
|
|
144
|
|
-
|
|
145
|
|
- // Check if there were any rejected promises and clear the already created ones list.
|
|
146
|
|
- if (rejectedEmitterPromiseArray.length > 0) {
|
|
147
|
|
- logger.error('Cleaning up remaining VADDeviceContext, due to create fail!');
|
|
148
|
|
-
|
|
149
|
|
- for (const context of vadContextArray) {
|
|
150
|
|
- context.vadEmitter.destroy();
|
|
151
|
|
- }
|
|
152
|
|
-
|
|
153
|
|
- // Reject create promise if one emitter failed to instantiate, we might one just ignore it,
|
|
154
|
|
- // leaving it like this for now
|
|
155
|
|
- throw new Error('Create VADReportingService failed due to TrackVADEmitter creation issues!');
|
|
156
|
|
- }
|
|
157
|
|
-
|
|
158
|
|
- vadReportingService._setVADContextArray(vadContextArray);
|
|
159
|
|
- vadReportingService._startPublish();
|
|
160
|
|
-
|
|
161
|
|
- return vadReportingService;
|
|
162
|
|
- });
|
|
163
|
|
- }
|
|
164
|
|
-
|
|
165
|
|
- /**
|
|
166
|
|
- * Destroy TrackVADEmitters and clear the context map.
|
|
167
|
|
- *
|
|
168
|
|
- * @returns {void}
|
|
169
|
|
- */
|
|
170
|
|
- _clearContextMap() {
|
|
171
|
|
- for (const vadContext of this._contextMap.values()) {
|
|
172
|
|
- vadContext.vadEmitter.destroy();
|
|
173
|
|
- }
|
|
174
|
|
- this._contextMap.clear();
|
|
175
|
|
- }
|
|
176
|
|
-
|
|
177
|
|
- /**
|
|
178
|
|
- * Set the watched device contexts.
|
|
179
|
|
- *
|
|
180
|
|
- * @param {Array<VADDeviceContext>} vadContextArray - List of mics.
|
|
181
|
|
- * @returns {void}
|
|
182
|
|
- */
|
|
183
|
|
- _setVADContextArray(vadContextArray: Array<VADDeviceContext>): void {
|
|
184
|
|
- for (const vadContext of vadContextArray) {
|
|
185
|
|
- this._contextMap.set(vadContext.deviceInfo.deviceId, vadContext);
|
|
186
|
|
- }
|
|
187
|
|
- }
|
|
188
|
|
-
|
|
189
|
|
- /**
|
|
190
|
|
- * Start the setInterval reporting process.
|
|
191
|
|
- *
|
|
192
|
|
- * @returns {void}.
|
|
193
|
|
- */
|
|
194
|
|
- _startPublish() {
|
|
195
|
|
- logger.log('VADReportingService started publishing.');
|
|
196
|
|
- this._intervalId = setInterval(() => {
|
|
197
|
|
- this._reportVadScore();
|
|
198
|
|
- }, this._intervalDelay);
|
|
199
|
|
- }
|
|
200
|
|
-
|
|
201
|
|
- /**
|
|
202
|
|
- * Function called at set interval with selected compute. The result will be published on the set callback.
|
|
203
|
|
- *
|
|
204
|
|
- * @returns {void}
|
|
205
|
|
- * @fires VAD_REPORT_PUBLISHED
|
|
206
|
|
- */
|
|
207
|
|
- _reportVadScore() {
|
|
208
|
|
- const vadComputeScoreArray = [];
|
|
209
|
|
- const computeTimestamp = Date.now();
|
|
210
|
|
-
|
|
211
|
|
- // Go through each device and compute cumulated VAD score.
|
|
212
|
|
-
|
|
213
|
|
- for (const [ deviceId, vadContext ] of this._contextMap) {
|
|
214
|
|
- const nrOfVADScores = vadContext.scoreArray.length;
|
|
215
|
|
- let vadSum = 0;
|
|
216
|
|
-
|
|
217
|
|
- vadContext.scoreArray.forEach(vadScore => {
|
|
218
|
|
- vadSum += vadScore.score;
|
|
219
|
|
- });
|
|
220
|
|
-
|
|
221
|
|
- // TODO For now we just calculate the average score for each device, more compute algorithms will be added.
|
|
222
|
|
- const avgVAD = vadSum / nrOfVADScores;
|
|
223
|
|
-
|
|
224
|
|
- vadContext.scoreArray = [];
|
|
225
|
|
-
|
|
226
|
|
- vadComputeScoreArray.push({
|
|
227
|
|
- timestamp: computeTimestamp,
|
|
228
|
|
- score: avgVAD,
|
|
229
|
|
- deviceId
|
|
230
|
|
- });
|
|
231
|
|
- }
|
|
232
|
|
-
|
|
233
|
|
- /**
|
|
234
|
|
- * Once the computation for all the tracked devices is done, fire an event containing all the necessary
|
|
235
|
|
- * information.
|
|
236
|
|
- *
|
|
237
|
|
- * @event VAD_REPORT_PUBLISHED
|
|
238
|
|
- * @type Array<Object> with the following structure:
|
|
239
|
|
- * @property {Date} timestamp - Timestamo at which the compute took place.
|
|
240
|
|
- * @property {number} avgVAD - Average VAD score over monitored period of time.
|
|
241
|
|
- * @property {string} deviceId - Associate local audio device ID.
|
|
242
|
|
- */
|
|
243
|
|
- this.emit(VAD_REPORT_PUBLISHED, vadComputeScoreArray);
|
|
244
|
|
- }
|
|
245
|
|
-
|
|
246
|
|
- /**
|
|
247
|
|
- * Callback method passed to vad emitters in order to publish their score.
|
|
248
|
|
- *
|
|
249
|
|
- * @param {Object} vadScore -VAD score emitted by.
|
|
250
|
|
- * @param {Date} vadScore.timestamp - Exact time at which processed PCM sample was generated.
|
|
251
|
|
- * @param {number} vadScore.score - VAD score on a scale from 0 to 1 (i.e. 0.7).
|
|
252
|
|
- * @param {string} vadScore.deviceId - Device id of the associated track.
|
|
253
|
|
- * @returns {void}
|
|
254
|
|
- * @listens VAD_SCORE_PUBLISHED
|
|
255
|
|
- */
|
|
256
|
|
- _devicePublishVADScore(vadScore: Object) {
|
|
257
|
|
- const context = this._contextMap.get(vadScore.deviceId);
|
|
258
|
|
-
|
|
259
|
|
- if (context) {
|
|
260
|
|
- context.scoreArray.push(vadScore);
|
|
261
|
|
- }
|
|
262
|
|
- }
|
|
263
|
|
-
|
|
264
|
|
- /**
|
|
265
|
|
- * Destroy the VADReportingService, stops the setInterval reporting, destroys the emitters and clears the map.
|
|
266
|
|
- * After this call the instance is no longer usable.
|
|
267
|
|
- *
|
|
268
|
|
- * @returns {void}.
|
|
269
|
|
- */
|
|
270
|
|
- destroy() {
|
|
271
|
|
- if (this._destroyed) {
|
|
272
|
|
- return;
|
|
273
|
|
- }
|
|
274
|
|
-
|
|
275
|
|
- logger.log('Destroying VADReportingService.');
|
|
276
|
|
-
|
|
277
|
|
- if (this._intervalId) {
|
|
278
|
|
- clearInterval(this._intervalId);
|
|
279
|
|
- this._intervalId = null;
|
|
280
|
|
- }
|
|
281
|
|
- this._clearContextMap();
|
|
282
|
|
- this._destroyed = true;
|
|
283
|
|
- }
|
|
284
|
|
-}
|