@@ -13,6 +13,7 @@ export default class WebAudioController extends WebAudio {
13
13
private pauseAt : number
14
14
private pickAt : number
15
15
private playing : boolean
16
+ private audioBufferSourceNode ! : AudioBufferSourceNode | null
16
17
17
18
constructor ( props : IllestWaveformProps ) {
18
19
super ( props )
@@ -33,45 +34,43 @@ export default class WebAudioController extends WebAudio {
33
34
}
34
35
35
36
public play ( ) : void {
36
- const offset = this . pickAt ? this . pickAt : this . pauseAt
37
+ this . audioBufferSourceNode && this . disconnectDestination ( )
38
+ this . createAudioBufferSourceNode ( )
37
39
this . connectDestination ( )
38
- this . audioBufferSourceNode . start ( 0 , offset )
40
+
41
+ const offset = this . pickAt ? this . pickAt : this . pauseAt
42
+ this . audioBufferSourceNode ! . start ( 0 , offset )
39
43
this . startAt = this . audioCtx . currentTime - offset
40
44
this . pauseAt = 0
41
45
this . playing = true
42
46
}
43
47
44
48
public pause ( ) : void {
45
49
const elapsed = this . audioCtx . currentTime - this . startAt
46
- this . stop ( )
50
+ this . disconnectDestination ( )
51
+ this . initializeState ( )
47
52
this . pauseAt = elapsed
48
53
}
49
54
50
55
public pick ( pickedTime : number ) : void {
51
56
this . pickAt = pickedTime
52
57
if ( ! this . playing ) return
53
- this . stopSource ( )
58
+ this . disconnectDestination ( )
54
59
this . play ( )
55
60
}
56
61
57
62
public replay ( ) : void {
58
- if ( this . audioBufferSourceNode ) this . stop ( )
63
+ if ( this . audioBufferSourceNode ) {
64
+ this . disconnectDestination ( )
65
+ this . initializeState ( )
66
+ }
59
67
this . play ( )
60
68
}
61
69
62
70
public finish ( ) : void {
63
71
this . pauseAt = 0
64
- this . stop ( )
65
- }
66
-
67
- private stop ( ) : void {
68
- this . stopSource ( )
69
- this . initializeState ( )
70
- }
71
-
72
- private stopSource ( ) {
73
72
this . disconnectDestination ( )
74
- this . audioBufferSourceNode . stop ( )
73
+ this . initializeState ( )
75
74
}
76
75
77
76
private initializeState ( ) {
@@ -80,4 +79,21 @@ export default class WebAudioController extends WebAudio {
80
79
this . pauseAt = 0
81
80
this . pickAt = 0
82
81
}
82
+
83
+ protected connectDestination ( ) : void {
84
+ this . audioBufferSourceNode ! . connect ( this . audioCtx . destination )
85
+ }
86
+
87
+ private createAudioBufferSourceNode ( ) : void {
88
+ if ( this . audioBufferSourceNode ) return
89
+ this . audioBufferSourceNode = this . audioCtx . createBufferSource ( )
90
+ this . audioBufferSourceNode . buffer = this . audioBuffer
91
+ }
92
+
93
+ protected disconnectDestination ( ) : void {
94
+ if ( ! this . audioBufferSourceNode ) return
95
+ this . audioBufferSourceNode . disconnect ( )
96
+ this . audioBufferSourceNode . stop ( )
97
+ this . audioBufferSourceNode = null
98
+ }
83
99
}
0 commit comments