@@ -41,13 +41,13 @@ class SamplesQueue {
4141 }
4242}
4343
44- export function audioNode ( ci : CommandInterface ) {
44+ export function audioNode ( ci : CommandInterface ) : ( volume : number ) => void {
4545 const sampleRate = ci . soundFrequency ( ) ;
4646 const channels = 1 ;
4747
4848 if ( sampleRate === 0 ) {
4949 console . warn ( "Can't create audio node with sampleRate === 0, ingnoring" ) ;
50- return ;
50+ return ( ) => { } ;
5151 }
5252
5353 let audioContext : AudioContext | null = null ;
@@ -66,7 +66,7 @@ export function audioNode(ci: CommandInterface) {
6666 }
6767
6868 if ( audioContext == null ) {
69- return ;
69+ return ( ) => { } ;
7070 }
7171
7272 const samplesQueue = new SamplesQueue ( ) ;
@@ -148,7 +148,12 @@ export function audioNode(ci: CommandInterface) {
148148 } ;
149149
150150 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 ;
152157
153158 const resumeWebAudio = ( ) => {
154159 if ( audioContext !== null && audioContext . state === "suspended" ) {
@@ -163,11 +168,16 @@ export function audioNode(ci: CommandInterface) {
163168 ci . events ( ) . onExit ( ( ) => {
164169 if ( audioContext !== null ) {
165170 audioNode . disconnect ( ) ;
171+ gainNode . disconnect ( ) ;
166172 audioContext . close ( ) ;
167173 }
168174
169175 document . removeEventListener ( "click" , resumeWebAudio ) ;
170176 document . removeEventListener ( "touchstart" , resumeWebAudio ) ;
171177 document . removeEventListener ( "keydown" , resumeWebAudio ) ;
172178 } ) ;
179+
180+ return ( volume : number ) => {
181+ gainNode . gain . value = volume ;
182+ } ;
173183}
0 commit comments