Fixed a bug where the fix for the volume would cause music loops to stop working

This commit is contained in:
JappaWakkaP3D 2020-10-25 16:21:33 +01:00 committed by Vitaly Mikhailov
parent 620f4d8325
commit 3799887e15

View File

@ -94,221 +94,221 @@ Public Class MusicManager
Private Shared _isFadingIn As Boolean = False Private Shared _isFadingIn As Boolean = False
' NAudio properties ' NAudio properties
Public Shared outputDevice As WaveOutEvent Public Shared outputDevice As WaveOutEvent
Public Shared audioFile As WaveChannel32 Public Shared audioFile As VorbisWaveReader
Public Shared _loop As WaveStream Public Shared _stream As WaveChannel32
Public Shared Property MasterVolume As Single = 1.0F Public Shared Property MasterVolume As Single = 1.0F
Public Shared ReadOnly Property CurrentSong As SongContainer Public Shared ReadOnly Property CurrentSong As SongContainer
Get Get
Return _currentSong Return _currentSong
End Get End Get
End Property End Property
Public Shared Property Muted As Boolean Public Shared Property Muted As Boolean
Get Get
Return _muted Return _muted
End Get End Get
Set(value As Boolean) Set(value As Boolean)
If _muted <> value Then If _muted <> value Then
_muted = value _muted = value
If _muted = True Then If _muted = True Then
If outputDevice IsNot Nothing Then If outputDevice IsNot Nothing Then
Volume = 0.0F Volume = 0.0F
Core.GameMessage.ShowMessage(Localization.GetString("game_message_audio_off"), 12, FontManager.MainFont, Color.White) Core.GameMessage.ShowMessage(Localization.GetString("game_message_audio_off"), 12, FontManager.MainFont, Color.White)
End If End If
Else Else
If outputDevice IsNot Nothing Then If outputDevice IsNot Nothing Then
If _isPausedForSound = True Then If _isPausedForSound = True Then
_muted = True _muted = True
Volume = 0.0F Volume = 0.0F
Else Else
Volume = 1.0F Volume = 1.0F
Core.GameMessage.ShowMessage(Localization.GetString("game_message_audio_on"), 12, FontManager.MainFont, Color.White) Core.GameMessage.ShowMessage(Localization.GetString("game_message_audio_on"), 12, FontManager.MainFont, Color.White)
End If End If
End If End If
End If End If
End If End If
End Set End Set
End Property End Property
Public Shared Property Paused As Boolean Public Shared Property Paused As Boolean
Get Get
Return _paused Return _paused
End Get End Get
Set(value As Boolean) Set(value As Boolean)
If _paused <> value Then If _paused <> value Then
_paused = value _paused = value
If _paused = True Then If _paused = True Then
If outputDevice IsNot Nothing Then If outputDevice IsNot Nothing Then
outputDevice.Pause() outputDevice.Pause()
_introMuteTime = Date.Now _introMuteTime = Date.Now
End If End If
Else Else
ResumePlayback() ResumePlayback()
End If End If
End If End If
End Set End Set
End Property End Property
Public Shared Sub Setup() Public Shared Sub Setup()
MasterVolume = 1.0F MasterVolume = 1.0F
If Muted = True Then If Muted = True Then
Volume = 0.0F Volume = 0.0F
Else Else
Volume = 1.0F Volume = 1.0F
End If
_nextSong = ""
_fadeSpeed = DEFAULT_FADE_SPEED
_isFadingOut = False
_isFadingIn = False
_isLooping = True
End Sub
Public Shared Sub Clear()
_songs.Clear()
LoadMusic(False)
End Sub
Public Shared Sub ClearCurrentlyPlaying()
' cleans all remains of currently playing songs
_currentSongExists = False
_currentSong = Nothing
_currentSongName = NO_MUSIC
_isIntroStarted = False
End Sub
Public Shared Sub PlayNoMusic()
' fades out current track and sets to NO_MUSIC
Play(NO_MUSIC)
End Sub
Public Shared Sub Update()
If _isPausedForSound Then
If Date.Now >= _pausedUntil Then
If MusicManager.Paused = True Then
_isPausedForSound = False
Paused = False
End If
End If
Else
' fading
If _isFadingOut Then
Volume -= _fadeSpeed
If Volume <= 0F Then
Volume = 0F
_isFadingOut = False
_isFadingIn = True
Dim song = GetSong(_nextSong)
If Not song Is Nothing Then
Play(song)
_nextSong = ""
If _fadeIntoIntro Then
_fadeIntoIntro = False
_introEndTime = Date.Now + song.Duration
_isIntroStarted = True
Else
_isLooping = True
End If
Else
' no song found, do not fade into anything
_fadeIntoIntro = False
ClearCurrentlyPlaying()
_isFadingIn = False
If Muted = True Then
Volume = 0.0F
Else
Volume = 1.0F
End If
If _nextSong = NO_MUSIC Then
_nextSong = ""
MusicManager.Stop()
End If
End If
End If
ElseIf _isFadingIn Then
If Muted = True Then
Volume = 0.0F
_isFadingIn = False
Else
Volume += _fadeSpeed
If Volume >= 1.0F Then
Volume = 1.0F
_isFadingIn = False
End If
End If
End If
' intro
If _isIntroStarted Then
If Paused = False Then
If Date.Now >= _introEndTime Then
Dim song = GetSong(_introContinueSong)
_isLooping = True
_isIntroStarted = False
Play(song)
End If
End If
End If
End If
If Core.GameInstance.IsActive AndAlso _lastVolume <> (Volume * MasterVolume) Then
UpdateVolume()
End If
End Sub
Public Shared Sub UpdateVolume()
_lastVolume = Volume * MasterVolume
If Not audioFile Is Nothing Then
audioFile.Volume = Volume * MasterVolume
End If End If
End Sub _nextSong = ""
_fadeSpeed = DEFAULT_FADE_SPEED
_isFadingOut = False
_isFadingIn = False
_isLooping = True
End Sub
Public Shared Sub PauseForSound(ByVal sound As SoundEffect) Public Shared Sub Clear()
_isPausedForSound = True _songs.Clear()
_pausedUntil = Date.Now + sound.Duration LoadMusic(False)
MusicManager.Pause() End Sub
End Sub
Public Shared Sub Pause() Public Shared Sub ClearCurrentlyPlaying()
MusicManager.Paused = True ' cleans all remains of currently playing songs
End Sub _currentSongExists = False
_currentSong = Nothing
_currentSongName = NO_MUSIC
_isIntroStarted = False
End Sub
Public Shared Sub [Stop]() Public Shared Sub PlayNoMusic()
If Not outputDevice Is Nothing Then ' fades out current track and sets to NO_MUSIC
outputDevice.Stop() Play(NO_MUSIC)
outputDevice.Dispose() End Sub
End If
_isIntroStarted = False
End Sub
Public Shared Sub ResumePlayback() Public Shared Sub Update()
If Not _currentSong Is Nothing Then If _isPausedForSound Then
If Date.Now >= _pausedUntil Then
If MusicManager.Paused = True Then
_isPausedForSound = False
Paused = False
End If
End If
Else
If outputDevice IsNot Nothing Then ' fading
' if an intro was playing while the music player was paused, calculate its end time If _isFadingOut Then
If outputDevice.PlaybackState = PlaybackState.Paused AndAlso _isIntroStarted Then Volume -= _fadeSpeed
Dim pauseTime As TimeSpan = Date.Now.Subtract(_introMuteTime)
_introEndTime = _introEndTime + pauseTime
End If If Volume <= 0F Then
outputDevice.Play()
End If
End If
End Sub Volume = 0F
_isFadingOut = False
_isFadingIn = True
Dim song = GetSong(_nextSong)
If Not song Is Nothing Then
Play(song)
_nextSong = ""
If _fadeIntoIntro Then
_fadeIntoIntro = False
_introEndTime = Date.Now + song.Duration
_isIntroStarted = True
Else
_isLooping = True
End If
Else
' no song found, do not fade into anything
_fadeIntoIntro = False
ClearCurrentlyPlaying()
_isFadingIn = False
If Muted = True Then
Volume = 0.0F
Else
Volume = 1.0F
End If
If _nextSong = NO_MUSIC Then
_nextSong = ""
MusicManager.Stop()
End If
End If
End If
ElseIf _isFadingIn Then
If Muted = True Then
Volume = 0.0F
_isFadingIn = False
Else
Volume += _fadeSpeed
If Volume >= 1.0F Then
Volume = 1.0F
_isFadingIn = False
End If
End If
End If
' intro
If _isIntroStarted Then
If Paused = False Then
If Date.Now >= _introEndTime Then
Dim song = GetSong(_introContinueSong)
_isLooping = True
_isIntroStarted = False
Play(song)
End If
End If
End If
End If
If Core.GameInstance.IsActive AndAlso _lastVolume <> (Volume * MasterVolume) Then
UpdateVolume()
End If
End Sub
Public Shared Sub UpdateVolume()
_lastVolume = Volume * MasterVolume
If Not _stream Is Nothing Then
_stream.Volume = Volume * MasterVolume
End If
End Sub
Public Shared Sub PauseForSound(ByVal sound As SoundEffect)
_isPausedForSound = True
_pausedUntil = Date.Now + sound.Duration
MusicManager.Pause()
End Sub
Public Shared Sub Pause()
MusicManager.Paused = True
End Sub
Public Shared Sub [Stop]()
If Not outputDevice Is Nothing Then
outputDevice.Stop()
outputDevice.Dispose()
End If
_isIntroStarted = False
End Sub
Public Shared Sub ResumePlayback()
If Not _currentSong Is Nothing Then
If outputDevice IsNot Nothing Then
' if an intro was playing while the music player was paused, calculate its end time
If outputDevice.PlaybackState = PlaybackState.Paused AndAlso _isIntroStarted Then
Dim pauseTime As TimeSpan = Date.Now.Subtract(_introMuteTime)
_introEndTime = _introEndTime + pauseTime
End If
outputDevice.Play()
End If
End If
End Sub
Private Shared Sub Play(song As SongContainer) Private Shared Sub Play(song As SongContainer)
If Not song Is Nothing Then If Not song Is Nothing Then
@ -317,13 +317,13 @@ Public Class MusicManager
outputDevice.Dispose() outputDevice.Dispose()
End If End If
outputDevice = New WaveOutEvent() outputDevice = New WaveOutEvent()
audioFile = New NAudio.Wave.WaveChannel32(New VorbisWaveReader(song.Song)) audioFile = New VorbisWaveReader(song.Song)
_loop = New LoopStream(audioFile, _isLooping) _stream = New NAudio.Wave.WaveChannel32(New LoopStream(audioFile, _isLooping))
outputDevice.Init(_loop) outputDevice.Init(_stream)
If Paused = False Then If Paused = False Then
outputDevice.Play() outputDevice.Play()
End If End If
audioFile.Volume = Volume * MasterVolume _stream.Volume = Volume * MasterVolume
_currentSongExists = True _currentSongExists = True
_currentSongName = song.Name _currentSongName = song.Name
_currentSong = song _currentSong = song