Skip to content

Commit d4b2bf4

Browse files
committed
fix: select screen music reset
Fixes https://discord.com/channels/233345562261323776/233363722934943744/1497285469761962024 Related to #3537 fix attempt.
1 parent 22e9f25 commit d4b2bf4

3 files changed

Lines changed: 25 additions & 8 deletions

File tree

src/script.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2884,6 +2884,7 @@ func systemScriptInit(l *lua.LState) {
28842884
sys.statsLog.finalizeMatch()
28852885
}
28862886
// Cleanup
2887+
sys.restorePauseVolume()
28872888
sys.timerStart = 0
28882889
sys.timerRounds = []int32{}
28892890
sys.scoreStart = [2]float32{}

src/sound.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

331334
func (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)

src/system.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -926,22 +926,30 @@ func (s *System) tickSound() {
926926

927927
if s.paused {
928928
// Apply BGM pause volume once per pause, even when the original BGM volume is 0.
929-
// volRestore cannot be used as the latch because 0 is a valid volume and also
930-
// the default value when no BGM is playing.
931-
if !s.pauseVolumeApplied {
929+
// volRestore cannot be used as the latch because 0 is a valid volume.
930+
if !s.bgm.pauseVolumeApplied {
932931
s.bgm.volRestore = s.bgm.bgmVolume
933932
s.bgm.bgmVolume = int(s.cfg.Sound.PauseMasterVolume * s.bgm.bgmVolume / 100.0)
934933
s.bgm.UpdateVolume()
935-
s.pauseVolumeApplied = true
934+
s.bgm.pauseVolumeApplied = true
936935
}
937936

938937
// Run every paused tick so sounds started while paused are also softened.
938+
s.pauseVolumeApplied = true
939939
s.softenAllSound()
940-
} else if s.pauseVolumeApplied {
941-
// Restore all volume
940+
} else if s.pauseVolumeApplied || s.bgm.pauseVolumeApplied {
941+
s.restorePauseVolume()
942+
}
943+
}
944+
945+
func (s *System) restorePauseVolume() {
946+
if s.bgm.pauseVolumeApplied {
942947
s.bgm.bgmVolume = s.bgm.volRestore
943948
s.bgm.volRestore = 0
949+
s.bgm.pauseVolumeApplied = false
944950
s.bgm.UpdateVolume()
951+
}
952+
if s.pauseVolumeApplied {
945953
s.restoreAllVolume()
946954
s.pauseVolumeApplied = false
947955
}
@@ -2148,6 +2156,7 @@ func (s *System) resetRoundState() {
21482156

21492157
s.resetFrameTime()
21502158

2159+
s.restorePauseVolume()
21512160
s.paused = false
21522161
s.introSkipCall = false
21532162
s.roundResetFlg = false

0 commit comments

Comments
 (0)