Pārlūkot izejas kodu

feat(ts) migrate VADTalkMutedDetection to TS

dev0
Yash 8 mēnešus atpakaļ
vecāks
revīzija
4e075c02d8
Revīzijas autora e-pasta adrese nav piesaistīta nevienam kontam
1 mainītis faili ar 20 papildinājumiem un 11 dzēšanām
  1. 20
    11
      modules/detection/VADTalkMutedDetection.ts

modules/detection/VADTalkMutedDetection.js → modules/detection/VADTalkMutedDetection.ts Parādīt failu

1
 import EventEmitter from '../util/EventEmitter';
1
 import EventEmitter from '../util/EventEmitter';
2
 import { calculateAverage } from '../util/MathUtil';
2
 import { calculateAverage } from '../util/MathUtil';
3
-
4
 import { DETECTOR_STATE_CHANGE, VAD_TALK_WHILE_MUTED } from './DetectionEvents';
3
 import { DETECTOR_STATE_CHANGE, VAD_TALK_WHILE_MUTED } from './DetectionEvents';
5
 
4
 
6
-
7
 /**
5
 /**
8
  * The threshold which the average VAD values for a span of time needs to exceed to trigger an event.
6
  * The threshold which the average VAD values for a span of time needs to exceed to trigger an event.
9
  * @type {number}
7
  * @type {number}
28
  */
26
  */
29
 const PROCESS_TIME_FRAME_SPAN_MS = 700;
27
 const PROCESS_TIME_FRAME_SPAN_MS = 700;
30
 
28
 
29
+export interface IVADScore {
30
+    deviceId: string;
31
+    score: number;
32
+    timestamp: Date;
33
+}
34
+
31
 /**
35
 /**
32
  * Detect if provided VAD score which is generated on a muted device is voice and fires an event.
36
  * Detect if provided VAD score which is generated on a muted device is voice and fires an event.
33
  */
37
  */
34
 export default class VADTalkMutedDetection extends EventEmitter {
38
 export default class VADTalkMutedDetection extends EventEmitter {
39
+    private _processing: boolean;
40
+    private _scoreArray: number[];
41
+    private _active: boolean;
42
+    private _processTimeout?: ReturnType<typeof setTimeout>;
43
+
35
     /**
44
     /**
36
      * Creates <tt>VADTalkMutedDetection</tt>
45
      * Creates <tt>VADTalkMutedDetection</tt>
37
      * @constructor
46
      * @constructor
54
          * Current mute state of the audio track being monitored.
63
          * Current mute state of the audio track being monitored.
55
          */
64
          */
56
         this._active = false;
65
         this._active = false;
57
-
58
         this._calculateVADScore = this._calculateVADScore.bind(this);
66
         this._calculateVADScore = this._calculateVADScore.bind(this);
59
     }
67
     }
60
 
68
 
63
      * @returns {void}
71
      * @returns {void}
64
      * @fires VAD_TALK_WHILE_MUTED
72
      * @fires VAD_TALK_WHILE_MUTED
65
      */
73
      */
66
-    _calculateVADScore() {
67
-        const score = calculateAverage(this._scoreArray);
74
+    private _calculateVADScore(): void {
75
+        const score = calculateAverage(new Float32Array(this._scoreArray));
68
 
76
 
69
         if (score > VAD_AVG_THRESHOLD) {
77
         if (score > VAD_AVG_THRESHOLD) {
70
             this.emit(VAD_TALK_WHILE_MUTED);
78
             this.emit(VAD_TALK_WHILE_MUTED);
84
      * @param {boolean} active
92
      * @param {boolean} active
85
      * @fires DETECTOR_STATE_CHANGE
93
      * @fires DETECTOR_STATE_CHANGE
86
      */
94
      */
87
-    _setActiveState(active) {
95
+    private _setActiveState(active: boolean): void {
88
         this._active = active;
96
         this._active = active;
89
         this.emit(DETECTOR_STATE_CHANGE, this._active);
97
         this.emit(DETECTOR_STATE_CHANGE, this._active);
90
     }
98
     }
94
      *
102
      *
95
      * @param {boolean} isMuted - Is the device muted or not.
103
      * @param {boolean} isMuted - Is the device muted or not.
96
      */
104
      */
97
-    changeMuteState(isMuted) {
105
+    public changeMuteState(isMuted: boolean): void {
98
         // This service only needs to run when the microphone is muted.
106
         // This service only needs to run when the microphone is muted.
99
         this._setActiveState(isMuted);
107
         this._setActiveState(isMuted);
100
         this.reset();
108
         this.reset();
105
      *
113
      *
106
      * @returns {boolean}
114
      * @returns {boolean}
107
      */
115
      */
108
-    isActive() {
116
+    public isActive(): boolean {
109
         return this._active;
117
         return this._active;
110
     }
118
     }
111
 
119
 
118
      * @param {string} vadScore.deviceId - Device id of the associated track.
126
      * @param {string} vadScore.deviceId - Device id of the associated track.
119
      * @listens VAD_SCORE_PUBLISHED
127
      * @listens VAD_SCORE_PUBLISHED
120
      */
128
      */
121
-    processVADScore(vadScore) {
129
+    public processVADScore(vadScore: IVADScore): void {
122
         if (!this._active) {
130
         if (!this._active) {
123
             return;
131
             return;
124
         }
132
         }
125
-
126
         // There is a processing phase on going, add score to buffer array.
133
         // There is a processing phase on going, add score to buffer array.
134
+
135
+
127
         if (this._processing) {
136
         if (this._processing) {
128
             this._scoreArray.push(vadScore.score);
137
             this._scoreArray.push(vadScore.score);
129
 
138
 
146
      *
155
      *
147
      * @returns {void}
156
      * @returns {void}
148
      */
157
      */
149
-    reset() {
158
+    public reset(): void {
150
         this._processing = false;
159
         this._processing = false;
151
         this._scoreArray = [];
160
         this._scoreArray = [];
152
         clearTimeout(this._processTimeout);
161
         clearTimeout(this._processTimeout);

Notiek ielāde…
Atcelt
Saglabāt