瀏覽代碼

feat(ts) migrate webaudio to TS

master
Naman Jain 6 月之前
父節點
當前提交
efcb1ece77
沒有連結到貢獻者的電子郵件帳戶。
共有 3 個檔案被更改,包括 29 行新增22 行删除
  1. 15
    8
      modules/webaudio/AudioMixer.ts
  2. 0
    14
      modules/webaudio/WebAudioUtils.js
  3. 14
    0
      modules/webaudio/WebAudioUtils.ts

modules/webaudio/AudioMixer.js → modules/webaudio/AudioMixer.ts 查看文件

@@ -9,6 +9,12 @@ const logger = getLogger('modules/webaudio/AudioMixer');
9 9
  * MediaStream.
10 10
  */
11 11
 export default class AudioMixer {
12
+    private _started: boolean;
13
+    private _streamsToMix: MediaStream[];
14
+    private _streamMSSArray: MediaStreamAudioSourceNode[];
15
+    private _audioContext?: AudioContext;
16
+    private _mixedMSD?: MediaStreamAudioDestinationNode;
17
+
12 18
     /**
13 19
      * Create AudioMixer instance.
14 20
      */
@@ -23,8 +29,8 @@ export default class AudioMixer {
23 29
      *
24 30
      * @param {MediaStream} stream - MediaStream to be mixed.
25 31
      */
26
-    addMediaStream(stream) {
27
-        if (!stream.getAudioTracks()) {
32
+    addMediaStream(stream: MediaStream): void {
33
+        if (!stream.getAudioTracks() || stream.getAudioTracks().length === 0) {
28 34
             logger.warn('Added MediaStream doesn\'t contain audio tracks.');
29 35
         }
30 36
 
@@ -35,12 +41,12 @@ export default class AudioMixer {
35 41
      * At this point a WebAudio ChannelMergerNode is created and and the two associated MediaStreams are connected to
36 42
      * it; the resulting mixed MediaStream is returned.
37 43
      *
38
-     * @returns {MediaStream} - MediaStream containing added streams mixed together, or null if no MediaStream
44
+     * @returns {MediaStream | null} - MediaStream containing added streams mixed together, or null if no MediaStream
39 45
      * is added.
40 46
      */
41
-    start() {
47
+    start(): MediaStream | null {
42 48
         // If the mixer was already started just return the existing mixed stream.
43
-        if (this._started) {
49
+        if (this._started && this._mixedMSD) {
44 50
             return this._mixedMSD.stream;
45 51
         }
46 52
 
@@ -54,10 +60,10 @@ export default class AudioMixer {
54 60
 
55 61
         this._started = true;
56 62
 
57
-        this._mixedMSD = this._audioContext.createMediaStreamDestination();
63
+        this._mixedMSD = this._audioContext!.createMediaStreamDestination();
58 64
 
59 65
         for (const stream of this._streamsToMix) {
60
-            const streamMSS = this._audioContext.createMediaStreamSource(stream);
66
+            const streamMSS = this._audioContext!.createMediaStreamSource(stream);
61 67
 
62 68
             streamMSS.connect(this._mixedMSD);
63 69
 
@@ -73,7 +79,7 @@ export default class AudioMixer {
73 79
      *
74 80
      * @returns {void}
75 81
      */
76
-    reset() {
82
+    reset(): void {
77 83
         this._started = false;
78 84
         this._streamsToMix = [];
79 85
 
@@ -87,5 +93,6 @@ export default class AudioMixer {
87 93
         if (this._audioContext) {
88 94
             this._audioContext = undefined;
89 95
         }
96
+        this._mixedMSD = undefined;
90 97
     }
91 98
 }

+ 0
- 14
modules/webaudio/WebAudioUtils.js 查看文件

@@ -1,14 +0,0 @@
1
-/**
2
- * Adapter that creates AudioContext objects depending on the browser.
3
- *
4
- * @returns {AudioContext} - Return a new AudioContext or undefined if the browser does not support it.
5
- */
6
-export function createAudioContext(options) {
7
-    const AudioContextImpl = window.AudioContext || window.webkitAudioContext;
8
-
9
-    if (!AudioContextImpl) {
10
-        return undefined;
11
-    }
12
-
13
-    return new AudioContextImpl(options);
14
-}

+ 14
- 0
modules/webaudio/WebAudioUtils.ts 查看文件

@@ -0,0 +1,14 @@
1
+/**
2
+ * Adapter that creates AudioContext objects depending on the browser.
3
+ *
4
+ * @returns {AudioContext | undefined} - Return a new AudioContext or undefined if the browser does not support it.
5
+ */
6
+export function createAudioContext(options?: AudioContextOptions): AudioContext | undefined {
7
+    const AudioContextImpl = window.AudioContext || (window as any).webkitAudioContext;
8
+
9
+    if (!AudioContextImpl) {
10
+        return undefined;
11
+    }
12
+
13
+    return new AudioContextImpl(options);
14
+}

Loading…
取消
儲存