@@ -303,6 +303,7 @@ type Bgm struct {
303303 filename string
304304 bgmVolume int
305305 volRestore int
306+ pauseVolumeApplied bool
306307 loop int
307308 streamer beep.StreamSeeker
308309 ctrl * beep.Ctrl
@@ -326,6 +327,8 @@ func (bgm *Bgm) Stop() {
326327 })
327328 }
328329 bgm .filename = ""
330+ bgm .volRestore = 0
331+ bgm .pauseVolumeApplied = false
329332}
330333
331334func (bgm * Bgm ) Open (filename string , loop , bgmVolume , bgmLoopStart , bgmLoopEnd , startPosition int , freqmul float32 , loopcount int ) {
@@ -342,6 +345,8 @@ func (bgm *Bgm) Open(filename string, loop, bgmVolume, bgmLoopStart, bgmLoopEnd,
342345 bgm .loop = loop
343346 bgm .bgmVolume = bgmVolume
344347 bgm .freqmul = freqmul
348+ bgm .volRestore = 0
349+ bgm .pauseVolumeApplied = false
345350 // Starve the current music streamer
346351 if bgm .ctrl != nil {
347352 WithSpeakerLock (func () {
@@ -422,11 +427,11 @@ func (bgm *Bgm) Open(filename string, loop, bgmVolume, bgmLoopStart, bgmLoopEnd,
422427 dstFreq := beep .SampleRate (float32 (sys .cfg .Sound .SampleRate ) / bgm .freqmul )
423428 resampler := beep .Resample (Clamp (sys .cfg .Sound .AudioResampleQuality , 1 , 16 ), bgm .sampleRate , dstFreq , bgm .volctrl )
424429 bgm .ctrl = & beep.Ctrl {Streamer : resampler }
425- bgm .volRestore = 0 // need this to prevent paused BGM volume from overwriting the new BGM volume
426430 if sys .paused && sys .pauseVolumeApplied {
427431 // A new BGM can start while the game is already paused.
428432 bgm .volRestore = bgm .bgmVolume
429433 bgm .bgmVolume = int (sys .cfg .Sound .PauseMasterVolume * bgm .bgmVolume / 100.0 )
434+ bgm .pauseVolumeApplied = true
430435 }
431436 bgm .UpdateVolume ()
432437 bgm .streamer .Seek (startPosition )
@@ -619,6 +624,8 @@ func (bgm *Bgm) OpenFromStreamer(stream beep.Streamer, srcSampleRate beep.Sample
619624 bgm .loop = 0
620625 bgm .bgmVolume = bgmVolume
621626 bgm .freqmul = 1
627+ bgm .volRestore = 0
628+ bgm .pauseVolumeApplied = false
622629
623630 // Starve the current music streamer
624631 if bgm .ctrl != nil {
@@ -640,11 +647,11 @@ func (bgm *Bgm) OpenFromStreamer(stream beep.Streamer, srcSampleRate beep.Sample
640647 dstFreq := beep .SampleRate (float32 (sys .cfg .Sound .SampleRate ) / bgm .freqmul )
641648 resampler := beep .Resample (Clamp (sys .cfg .Sound .AudioResampleQuality , 1 , 16 ), bgm .sampleRate , dstFreq , bgm .volctrl )
642649 bgm .ctrl = & beep.Ctrl {Streamer : resampler }
643- bgm .volRestore = 0
644650 if sys .paused && sys .pauseVolumeApplied {
645651 // A video-backed BGM can also be attached while pause is active.
646652 bgm .volRestore = bgm .bgmVolume
647653 bgm .bgmVolume = int (sys .cfg .Sound .PauseMasterVolume * bgm .bgmVolume / 100.0 )
654+ bgm .pauseVolumeApplied = true
648655 }
649656 bgm .UpdateVolume ()
650657 speaker .Play (bgm .ctrl )
0 commit comments