Added .mp3 & .wma support for the music engine

This commit is contained in:
JappaWakka 2021-10-13 18:08:18 +02:00
parent 76fe526d55
commit e6e44cec29
2 changed files with 87 additions and 28 deletions

View File

@ -49,11 +49,23 @@ Public Class LoopStream
Dim IntroContinueSong As SongContainer = MusicManager.GetSong(MusicManager._introContinueSong) Dim IntroContinueSong As SongContainer = MusicManager.GetSong(MusicManager._introContinueSong)
If IntroContinueSong IsNot Nothing Then If IntroContinueSong IsNot Nothing Then
Logger.Debug($"Play song [{IntroContinueSong.Name}]") Logger.Debug($"Play song [{IntroContinueSong.Name}]")
_sourceStream = New VorbisWaveReader(IntroContinueSong.Song) If IntroContinueSong.AudioType = ".ogg" Then
_sourceStream = New VorbisWaveReader(IntroContinueSong.Song)
ElseIf IntroContinueSong.AudioType = ".mp3" Then
_sourceStream = New Mp3FileReader(IntroContinueSong.Song)
ElseIf IntroContinueSong.AudioType = ".wma" Then
_sourceStream = New MediaFoundationReader(IntroContinueSong.Song)
End If
_enableLooping = True _enableLooping = True
_sourceStream.Position = 0 _sourceStream.Position = 0
Else Else
_sourceStream = New VorbisWaveReader(MusicManager.GetSong("silence").Song) If MusicManager.GetSong("silence").AudioType = ".ogg" Then
_sourceStream = New VorbisWaveReader(MusicManager.GetSong("silence").Song)
ElseIf MusicManager.GetSong("silence").AudioType = ".mp3" Then
_sourceStream = New Mp3FileReader(MusicManager.GetSong("silence").Song)
ElseIf MusicManager.GetSong("silence").AudioType = ".wma" Then
_sourceStream = New MediaFoundationReader(MusicManager.GetSong("silence").Song)
End If
_enableLooping = True _enableLooping = True
_sourceStream.Position = 0 _sourceStream.Position = 0
End If End If
@ -108,7 +120,9 @@ Public Class MusicManager
Private Shared _isFadingOut As Boolean = False Private Shared _isFadingOut As Boolean = False
' NAudio properties ' NAudio properties
Public Shared outputDevice As WaveOutEvent Public Shared outputDevice As WaveOutEvent
Public Shared audioFile As VorbisWaveReader Public Shared audioFileOGG As VorbisWaveReader
Public Shared audioFileMP3 As Mp3FileReader
Public Shared audioFileWMA As MediaFoundationReader
Public Shared _stream As WaveChannel32 Public Shared _stream As WaveChannel32
Public Shared Property MasterVolume As Single = 1.0F Public Shared Property MasterVolume As Single = 1.0F
@ -318,8 +332,16 @@ Public Class MusicManager
outputDevice.Dispose() outputDevice.Dispose()
End If End If
outputDevice = New WaveOutEvent() outputDevice = New WaveOutEvent()
audioFile = New VorbisWaveReader(song.Song) If song.AudioType = ".ogg" Then
_stream = New NAudio.Wave.WaveChannel32(New LoopStream(audioFile, _isLooping)) audioFileOGG = New VorbisWaveReader(song.Song)
_stream = New NAudio.Wave.WaveChannel32(New LoopStream(audioFileOGG, _isLooping))
ElseIf song.AudioType = ".mp3" Then
audioFileMP3 = New Mp3FileReader(song.Song)
_stream = New NAudio.Wave.WaveChannel32(New LoopStream(audioFileMP3, _isLooping))
ElseIf song.AudioType = ".wma" Then
audioFileWMA = New MediaFoundationReader(song.Song)
_stream = New NAudio.Wave.WaveChannel32(New LoopStream(audioFileWMA, _isLooping))
End If
outputDevice.Init(_stream) outputDevice.Init(_stream)
If Paused = False Then If Paused = False Then
outputDevice.Play() outputDevice.Play()
@ -449,26 +471,35 @@ Public Class MusicManager
Public Shared Function GetSong(songName As String) As SongContainer Public Shared Function GetSong(songName As String) As SongContainer
Dim key = GetSongName(songName) Dim key = GetSongName(songName)
Dim songFilePath = GameController.GamePath & GameModeManager.ActiveGameMode.ContentPath & "Songs\" & key
Dim defaultSongFilePath = GameController.GamePath & "\Content\" & "Songs\" & key
Dim audiotype = ""
If _songs.ContainsKey(key) = True Then If _songs.ContainsKey(key) = True Then
Return _songs(key) Return _songs(key)
Else Else
Dim defaultSongFilePath = GameController.GamePath & "\Content\" & "Songs\" & key & ".ogg" If System.IO.File.Exists(songFilePath & ".ogg") = True Or System.IO.File.Exists(defaultSongFilePath & ".ogg") = True Then
Dim songFilePath = GameController.GamePath & GameModeManager.ActiveGameMode.ContentPath & "Songs\" & key & ".ogg" audiotype = ".ogg"
If File.Exists(songFilePath) Then ElseIf System.IO.File.Exists(songFilePath & ".mp3") = True Or System.IO.File.Exists(defaultSongFilePath & ".mp3") = True Then
If AddSong(key, False) = True Then audiotype = ".mp3"
Return _songs(key) ElseIf System.IO.File.Exists(songFilePath & ".wma") = True Or System.IO.File.Exists(defaultSongFilePath & ".wma") = True Then
End If audiotype = ".wma"
ElseIf File.Exists(defaultSongFilePath) Then
If AddSong(key, False) = True Then
Return _songs(key)
End If
Else
Logger.Log(Logger.LogTypes.Warning, "MusicManager.vb: Cannot find music file """ & songName & """. Return nothing.")
End If End If
Return Nothing
End If End If
If File.Exists(songFilePath & audiotype) Then
If AddSong(key, False) = True Then
Return _songs(key)
End If
ElseIf File.Exists(defaultSongFilePath & audiotype) Then
If AddSong(key, False) = True Then
Return _songs(key)
End If
Else
Logger.Log(Logger.LogTypes.Warning, "MusicManager.vb: Cannot find music file """ & songName & """. Return nothing.")
End If
Return Nothing
End Function End Function
@ -483,7 +514,7 @@ Public Class MusicManager
Private Shared Function AddSong(ByVal Name As String, ByVal forceReplace As Boolean) As Boolean Private Shared Function AddSong(ByVal Name As String, ByVal forceReplace As Boolean) As Boolean
Try Try
Dim cContent As ContentManager = ContentPackManager.GetContentManager("Songs\" & Name, ".ogg") Dim cContent As ContentManager = ContentPackManager.GetContentManager("Songs\" & Name, ".ogg,.mp3,.wma")
Dim loadSong As Boolean = False Dim loadSong As Boolean = False
Dim removeSong As Boolean = False Dim removeSong As Boolean = False
@ -497,20 +528,26 @@ Public Class MusicManager
If loadSong = True Then If loadSong = True Then
Dim songFilePath As String = Nothing Dim songFilePath As String = Nothing
Dim audioType As String = Nothing
If System.IO.File.Exists(GameController.GamePath & "\" & cContent.RootDirectory & "\Songs\" & Name & ".ogg") = True Then If System.IO.File.Exists(GameController.GamePath & "\" & cContent.RootDirectory & "\Songs\" & Name & ".ogg") = True Then
songFilePath = GameController.GamePath & "\" & cContent.RootDirectory & "\Songs\" & Name & ".ogg" audioType = ".ogg"
ElseIf System.IO.File.Exists(GameController.GamePath & "\" & cContent.RootDirectory & "\Songs\" & Name & ".mp3") = True Then
audioType = ".mp3"
ElseIf System.IO.File.Exists(GameController.GamePath & "\" & cContent.RootDirectory & "\Songs\" & Name & ".wma") = True Then
audioType = ".wma"
Else Else
Logger.Log(Logger.LogTypes.Warning, "MusicManager.vb: Song at """ & GameController.GamePath & "\" & cContent.RootDirectory & "\Songs\" & Name & """ was not found!") Logger.Log(Logger.LogTypes.Warning, "MusicManager.vb: Song at """ & GameController.GamePath & "\" & cContent.RootDirectory & "\Songs\" & Name & """ was not found!")
Return False Return False
End If End If
If Not songFilePath Is Nothing Then If Not audioType Is Nothing Then
songFilePath = GameController.GamePath & "\" & cContent.RootDirectory & "\Songs\" & Name & audioType
If removeSong = True Then If removeSong = True Then
_songs.Remove(GetSongName(Name)) _songs.Remove(GetSongName(Name))
End If End If
Dim Duration = GetSongDuration(songFilePath) Dim Duration = GetSongDuration(songFilePath)
_songs.Add(GetSongName(Name), New SongContainer(songFilePath, Name, Duration, cContent.RootDirectory)) _songs.Add(GetSongName(Name), New SongContainer(songFilePath, Name, Duration, cContent.RootDirectory, audioType))
End If End If
End If End If
Catch ex As Exception Catch ex As Exception
@ -522,7 +559,7 @@ Public Class MusicManager
Public Shared Sub LoadMusic(ByVal forceReplace As Boolean) Public Shared Sub LoadMusic(ByVal forceReplace As Boolean)
For Each musicFile As String In System.IO.Directory.GetFiles(GameController.GamePath & GameModeManager.ActiveGameMode.ContentPath & "Songs\", "*.*", IO.SearchOption.AllDirectories) For Each musicFile As String In System.IO.Directory.GetFiles(GameController.GamePath & GameModeManager.ActiveGameMode.ContentPath & "Songs\", "*.*", IO.SearchOption.AllDirectories)
If musicFile.EndsWith(".ogg") = True Then If musicFile.EndsWith(".ogg") = True Or musicFile.EndsWith(".mp3") = True Or musicFile.EndsWith(".wma") = True Then
Dim isIntro As Boolean = False Dim isIntro As Boolean = False
If musicFile.Contains("\Songs\intro\") = True Then If musicFile.Contains("\Songs\intro\") = True Then
isIntro = True isIntro = True
@ -542,7 +579,7 @@ Public Class MusicManager
Dim path As String = GameController.GamePath & "\ContentPacks\" & c & "\Songs\" Dim path As String = GameController.GamePath & "\ContentPacks\" & c & "\Songs\"
If System.IO.Directory.Exists(path) = True Then If System.IO.Directory.Exists(path) = True Then
For Each musicFile As String In System.IO.Directory.GetFiles(path, "*.*", IO.SearchOption.AllDirectories) For Each musicFile As String In System.IO.Directory.GetFiles(path, "*.*", IO.SearchOption.AllDirectories)
If musicFile.EndsWith(".ogg") = True Then If musicFile.EndsWith(".ogg") = True Or musicFile.EndsWith(".mp3") = True Or musicFile.EndsWith(".wma") = True Then
Dim isIntro As Boolean = False Dim isIntro As Boolean = False
If musicFile.Contains("\Songs\intro\") = True Then If musicFile.Contains("\Songs\intro\") = True Then
isIntro = True isIntro = True
@ -562,10 +599,30 @@ Public Class MusicManager
End Sub End Sub
Private Shared Function GetSongDuration(songFilePath As String) As TimeSpan Private Shared Function GetSongDuration(songFilePath As String) As TimeSpan
Dim DurationOGG As VorbisWaveReader = Nothing
Dim DurationMP3 As Mp3FileReader = Nothing
Dim DurationWMA As MediaFoundationReader = Nothing
Dim SongDuration As TimeSpan = Nothing
If songFilePath.Contains(".ogg") Then
DurationOGG = New VorbisWaveReader(songFilePath)
SongDuration = DurationOgg.TotalTime
ElseIf songFilePath.Contains(".mp3") Then
DurationMP3 = New Mp3FileReader(songFilePath)
SongDuration = DurationMP3.TotalTime
ElseIf songFilePath.Contains(".wma") Then
DurationWMA = New MediaFoundationReader(songFilePath)
SongDuration = DurationWMA.TotalTime
End If
Dim DurationFile As VorbisWaveReader = New VorbisWaveReader(songFilePath) If DurationOGG IsNot Nothing Then
Dim SongDuration = DurationFile.TotalTime DurationOGG.Dispose()
DurationFile.Dispose() End If
If DurationMP3 IsNot Nothing Then
DurationMP3.Dispose()
End If
If DurationWMA IsNot Nothing Then
DurationWMA.Dispose()
End If
Return SongDuration Return SongDuration
End Function End Function

View File

@ -6,12 +6,14 @@ Public Class SongContainer
Public Name As String Public Name As String
Public Origin As String Public Origin As String
Public Duration As TimeSpan Public Duration As TimeSpan
Public AudioType As String
Public Sub New(song As String, name As String, duration As TimeSpan, origin As String) Public Sub New(song As String, name As String, duration As TimeSpan, origin As String, type As String)
Me.Song = song Me.Song = song
Me.Name = name Me.Name = name
Me.Origin = origin Me.Origin = origin
Me.Duration = duration Me.Duration = duration
Me.AudioType = type
End Sub End Sub