瀏覽代碼

feat(ts) migrate webaudio to TS

master
Naman Jain 6 月之前
父節點
當前提交
efcb1ece77
No account linked to committer's email address
共有 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
  * MediaStream.
9
  * MediaStream.
10
  */
10
  */
11
 export default class AudioMixer {
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
      * Create AudioMixer instance.
19
      * Create AudioMixer instance.
14
      */
20
      */
23
      *
29
      *
24
      * @param {MediaStream} stream - MediaStream to be mixed.
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
             logger.warn('Added MediaStream doesn\'t contain audio tracks.');
34
             logger.warn('Added MediaStream doesn\'t contain audio tracks.');
29
         }
35
         }
30
 
36
 
35
      * At this point a WebAudio ChannelMergerNode is created and and the two associated MediaStreams are connected to
41
      * At this point a WebAudio ChannelMergerNode is created and and the two associated MediaStreams are connected to
36
      * it; the resulting mixed MediaStream is returned.
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
      * is added.
45
      * is added.
40
      */
46
      */
41
-    start() {
47
+    start(): MediaStream | null {
42
         // If the mixer was already started just return the existing mixed stream.
48
         // If the mixer was already started just return the existing mixed stream.
43
-        if (this._started) {
49
+        if (this._started && this._mixedMSD) {
44
             return this._mixedMSD.stream;
50
             return this._mixedMSD.stream;
45
         }
51
         }
46
 
52
 
54
 
60
 
55
         this._started = true;
61
         this._started = true;
56
 
62
 
57
-        this._mixedMSD = this._audioContext.createMediaStreamDestination();
63
+        this._mixedMSD = this._audioContext!.createMediaStreamDestination();
58
 
64
 
59
         for (const stream of this._streamsToMix) {
65
         for (const stream of this._streamsToMix) {
60
-            const streamMSS = this._audioContext.createMediaStreamSource(stream);
66
+            const streamMSS = this._audioContext!.createMediaStreamSource(stream);
61
 
67
 
62
             streamMSS.connect(this._mixedMSD);
68
             streamMSS.connect(this._mixedMSD);
63
 
69
 
73
      *
79
      *
74
      * @returns {void}
80
      * @returns {void}
75
      */
81
      */
76
-    reset() {
82
+    reset(): void {
77
         this._started = false;
83
         this._started = false;
78
         this._streamsToMix = [];
84
         this._streamsToMix = [];
79
 
85
 
87
         if (this._audioContext) {
93
         if (this._audioContext) {
88
             this._audioContext = undefined;
94
             this._audioContext = undefined;
89
         }
95
         }
96
+        this._mixedMSD = undefined;
90
     }
97
     }
91
 }
98
 }

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

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 查看文件

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…
取消
儲存