@@ -41,13 +41,13 @@ class SamplesQueue {
41
41
}
42
42
}
43
43
44
- export function audioNode ( ci : CommandInterface ) {
44
+ export function audioNode ( ci : CommandInterface ) : ( volume : number ) => void {
45
45
const sampleRate = ci . soundFrequency ( ) ;
46
46
const channels = 1 ;
47
47
48
48
if ( sampleRate === 0 ) {
49
49
console . warn ( "Can't create audio node with sampleRate === 0, ingnoring" ) ;
50
- return ;
50
+ return ( ) => { } ;
51
51
}
52
52
53
53
let audioContext : AudioContext | null = null ;
@@ -66,7 +66,7 @@ export function audioNode(ci: CommandInterface) {
66
66
}
67
67
68
68
if ( audioContext == null ) {
69
- return ;
69
+ return ( ) => { } ;
70
70
}
71
71
72
72
const samplesQueue = new SamplesQueue ( ) ;
@@ -148,7 +148,12 @@ export function audioNode(ci: CommandInterface) {
148
148
} ;
149
149
150
150
audioNode . onaudioprocess = ci . directSound !== undefined ? onDirectProcess : onQueueProcess ;
151
- audioNode . connect ( audioContext . destination ) ;
151
+
152
+ const gainNode = audioContext . createGain ( ) ;
153
+ gainNode . connect ( audioContext . destination ) ;
154
+ audioNode . connect ( gainNode ) ;
155
+
156
+ gainNode . gain . value = 1.0 ;
152
157
153
158
const resumeWebAudio = ( ) => {
154
159
if ( audioContext !== null && audioContext . state === "suspended" ) {
@@ -163,11 +168,16 @@ export function audioNode(ci: CommandInterface) {
163
168
ci . events ( ) . onExit ( ( ) => {
164
169
if ( audioContext !== null ) {
165
170
audioNode . disconnect ( ) ;
171
+ gainNode . disconnect ( ) ;
166
172
audioContext . close ( ) ;
167
173
}
168
174
169
175
document . removeEventListener ( "click" , resumeWebAudio ) ;
170
176
document . removeEventListener ( "touchstart" , resumeWebAudio ) ;
171
177
document . removeEventListener ( "keydown" , resumeWebAudio ) ;
172
178
} ) ;
179
+
180
+ return ( volume : number ) => {
181
+ gainNode . gain . value = volume ;
182
+ } ;
173
183
}
0 commit comments