Skip to content

Commit c51487b

Browse files
authored
feat(TS) Migrate modules\detection\TrackVADEmitter.ts to TS (#2826)
1 parent 5e6dea9 commit c51487b

File tree

2 files changed

+43
-34
lines changed

2 files changed

+43
-34
lines changed

modules/RTC/JitsiTrack.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ export default class JitsiTrack extends Listenable {
5353
private type: MediaType;
5454
private handlers: Map<string, MediaStreamTrackEventHandler>;
5555
protected containers: HTMLElement[];
56-
protected stream: IExtendedMediaStream;
56+
/**
57+
* @internal
58+
*/
59+
stream: IExtendedMediaStream;
5760
/**
5861
* @internal
5962
*/

modules/detection/TrackVADEmitter.js renamed to modules/detection/TrackVADEmitter.ts

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
1+
import JitsiLocalTrack from '../RTC/JitsiLocalTrack';
12
import RTC from '../RTC/RTC';
23
import EventEmitter from '../util/EventEmitter';
34
import { createAudioContext } from '../webaudio/WebAudioUtils';
45

56
import { DetectionEvents } from './DetectionEvents';
67

8+
export interface IVadProcessor {
9+
calculateAudioFrameVAD: (pcmSample: Float32Array | number[]) => number;
10+
getRequiredPCMFrequency: () => number;
11+
getSampleLength: () => number;
12+
}
13+
14+
export type AudioContextType = AudioContext;
15+
export type ScriptProcessorNodeType = ScriptProcessorNode;
16+
export type MediaStreamAudioSourceNodeType = MediaStreamAudioSourceNode;
17+
718
/**
819
* Connects an audio JitsiLocalTrack to a vadProcessor using WebAudio ScriptProcessorNode.
920
* Once an object is created audio from the local track flows through the ScriptProcessorNode as raw PCM.
@@ -14,6 +25,16 @@ import { DetectionEvents } from './DetectionEvents';
1425
* @fires VAD_SCORE_PUBLISHED
1526
*/
1627
export default class TrackVADEmitter extends EventEmitter {
28+
private _procNodeSampleRate: number;
29+
private _vadProcessor: IVadProcessor;
30+
private _localTrack: JitsiLocalTrack;
31+
private _bufferResidue: Float32Array;
32+
private _audioContext: AudioContextType;
33+
private _vadSampleSize: number;
34+
private _audioSource!: MediaStreamAudioSourceNodeType;
35+
private _audioProcessingNode!: ScriptProcessorNodeType;
36+
private _destroyed?: boolean;
37+
1738
/**
1839
* Constructor.
1940
*
@@ -22,7 +43,7 @@ export default class TrackVADEmitter extends EventEmitter {
2243
* @param {Object} vadProcessor - VAD processor that allows us to calculate VAD score for PCM samples.
2344
* @param {JitsiLocalTrack} jitsiLocalTrack - JitsiLocalTrack corresponding to micDeviceId.
2445
*/
25-
constructor(procNodeSampleRate, vadProcessor, jitsiLocalTrack) {
46+
constructor(procNodeSampleRate: number, vadProcessor: IVadProcessor, jitsiLocalTrack: JitsiLocalTrack) {
2647
super();
2748

2849
/**
@@ -77,11 +98,15 @@ export default class TrackVADEmitter extends EventEmitter {
7798
* - <tt>calculateAudioFrameVAD(pcmSample)</tt> - Process a 32 float pcm sample of getSampleLength size.
7899
* @returns {Promise<TrackVADEmitter>} - Promise resolving in a new instance of TrackVADEmitter.
79100
*/
80-
static create(micDeviceId, procNodeSampleRate, vadProcessor) {
101+
static create(
102+
micDeviceId: string,
103+
procNodeSampleRate: number,
104+
vadProcessor: IVadProcessor
105+
): Promise<TrackVADEmitter> {
81106
return RTC.obtainAudioAndVideoPermissions({
82107
devices: [ 'audio' ],
83108
micDeviceId
84-
}).then(localTrack => {
109+
}).then((localTrack: JitsiLocalTrack[]) => {
85110
// We only expect one audio track when specifying a device id.
86111
if (!localTrack[0]) {
87112
throw new Error(`Failed to create jitsi local track for device id: ${micDeviceId}`);
@@ -99,7 +124,7 @@ export default class TrackVADEmitter extends EventEmitter {
99124
*
100125
* @returns {void}
101126
*/
102-
_initializeAudioContext() {
127+
_initializeAudioContext(): void {
103128
this._audioSource = this._audioContext.createMediaStreamSource(this._localTrack.stream);
104129

105130
// TODO AudioProcessingNode is deprecated in the web audio specifications and the recommended replacement
@@ -122,7 +147,7 @@ export default class TrackVADEmitter extends EventEmitter {
122147
* @returns {void}
123148
* @fires VAD_SCORE_PUBLISHED
124149
*/
125-
_onAudioProcess(audioEvent) {
150+
_onAudioProcess(audioEvent: AudioProcessingEvent): void {
126151
// Prepend the residue PCM buffer from the previous process callback.
127152
const inData = audioEvent.inputBuffer.getChannelData(0);
128153
const completeInData = [ ...this._bufferResidue, ...inData ];
@@ -144,15 +169,15 @@ export default class TrackVADEmitter extends EventEmitter {
144169
});
145170
}
146171

147-
this._bufferResidue = completeInData.slice(i, completeInData.length);
172+
this._bufferResidue = new Float32Array(completeInData.slice(i, completeInData.length));
148173
}
149174

150175
/**
151176
* Connects the nodes in the AudioContext to start the flow of audio data.
152177
*
153178
* @returns {void}
154179
*/
155-
_connectAudioGraph() {
180+
_connectAudioGraph(): void {
156181
this._audioProcessingNode.onaudioprocess = this._onAudioProcess;
157182
this._audioSource.connect(this._audioProcessingNode);
158183
this._audioProcessingNode.connect(this._audioContext.destination);
@@ -163,10 +188,10 @@ export default class TrackVADEmitter extends EventEmitter {
163188
*
164189
* @returns {void}
165190
*/
166-
_disconnectAudioGraph() {
191+
_disconnectAudioGraph(): void {
167192
// Even thought we disconnect the processing node it seems that some callbacks remain queued,
168193
// resulting in calls with and uninitialized context.
169-
// eslint-disable-next-line no-empty-function
194+
// eslint-disable-next-line @typescript-eslint/no-empty-function
170195
this._audioProcessingNode.onaudioprocess = () => {};
171196
this._audioProcessingNode.disconnect();
172197
this._audioSource.disconnect();
@@ -177,36 +202,17 @@ export default class TrackVADEmitter extends EventEmitter {
177202
*
178203
* @returns {void}
179204
*/
180-
_cleanupResources() {
205+
_cleanupResources(): void {
181206
this._disconnectAudioGraph();
182207
this._localTrack.stopStream();
183208
}
184209

185-
/**
186-
* Get the associated track device ID.
187-
*
188-
* @returns {string}
189-
*/
190-
getDeviceId() {
191-
return this._localTrack.getDeviceId();
192-
}
193-
194-
195-
/**
196-
* Get the associated track label.
197-
*
198-
* @returns {string}
199-
*/
200-
getTrackLabel() {
201-
return this._localTrack.getDeviceLabel();
202-
}
203-
204210
/**
205211
* Start the emitter by connecting the audio graph.
206212
*
207213
* @returns {void}
208214
*/
209-
start() {
215+
start(): void {
210216
this._connectAudioGraph();
211217
}
212218

@@ -215,17 +221,17 @@ export default class TrackVADEmitter extends EventEmitter {
215221
*
216222
* @returns {void}
217223
*/
218-
stop() {
224+
stop(): void {
219225
this._disconnectAudioGraph();
220-
this._bufferResidue = [];
226+
this._bufferResidue = new Float32Array([]);
221227
}
222228

223229
/**
224230
* Destroy TrackVADEmitter instance (release resources and stop callbacks).
225231
*
226232
* @returns {void}
227233
*/
228-
destroy() {
234+
destroy(): void {
229235
if (this._destroyed) {
230236
return;
231237
}

0 commit comments

Comments
 (0)