P3D-Legacy/P3D/Resources/Sound/SoundManager.vb

86 lines
3.0 KiB
VB.net
Raw Normal View History

2016-09-07 18:50:38 +02:00
Public Class SoundManager
2018-02-22 03:40:20 +01:00
Const POKEMON_CRY_VOLUME_MULTIPLIER As Single = 0.6F
2016-09-07 18:50:38 +02:00
2018-02-22 03:40:20 +01:00
Shared _sounds As Dictionary(Of String, SoundEffect) = New Dictionary(Of String, SoundEffect)
2016-09-07 18:50:38 +02:00
2018-02-22 03:40:20 +01:00
Public Shared Volume As Single = 1.0F
Public Shared Muted As Boolean = False
2016-09-07 18:50:38 +02:00
2018-02-22 03:40:20 +01:00
Private Declare Function GetAudioOutputDevices Lib "winmm.dll" Alias "waveOutGetNumDevs" () As Integer
Private Shared Function HasOutputDeviceAvailable() As Boolean
Return GetAudioOutputDevices() > 0
2016-09-07 18:50:38 +02:00
End Function
2018-02-22 03:40:20 +01:00
Public Shared Sub Clear()
_sounds.Clear()
End Sub
2016-09-07 18:50:38 +02:00
2018-02-22 03:40:20 +01:00
Public Shared Sub PlaySound(soundFile As String)
PlaySound(soundFile, 0.0F, 0.0F, Volume, False)
2016-09-07 18:50:38 +02:00
End Sub
2018-02-22 03:40:20 +01:00
Public Shared Sub PlaySound(soundFile As String, stopMusic As Boolean)
PlaySound(soundFile, 0.0F, 0.0F, Volume, stopMusic)
End Sub
2016-09-07 18:50:38 +02:00
2018-02-22 03:40:20 +01:00
Public Shared Sub PlaySound(soundFile As String, pitch As Single, pan As Single, volume As Single, stopMusic As Boolean)
2016-09-07 18:50:38 +02:00
2018-02-22 03:40:20 +01:00
If Not Muted Then
2016-09-07 18:50:38 +02:00
2018-02-22 03:40:20 +01:00
Dim key = soundFile.ToLowerInvariant()
Dim sound As SoundEffect = Nothing
If Not _sounds.TryGetValue(key, sound) Then
2016-09-07 18:50:38 +02:00
2018-02-22 03:40:20 +01:00
' load sound
Dim filePath = Path.Combine(GameController.GamePath, "Content\Sounds", soundFile & ".wav")
If File.Exists(filePath) Then
Using stream As New FileStream(filePath, FileMode.OpenOrCreate)
Try
sound = SoundEffect.FromStream(stream)
_sounds.Add(key, sound)
Catch ex As Exception
Logger.Log(Logger.LogTypes.ErrorMessage, "Failed to load sound at """ & soundFile & """: " & ex.Message)
End Try
End Using
2016-09-07 18:50:38 +02:00
End If
2018-02-22 03:40:20 +01:00
2016-09-07 18:50:38 +02:00
End If
2018-02-22 03:40:20 +01:00
If sound IsNot Nothing Then
2016-09-07 18:50:38 +02:00
2018-02-22 03:40:20 +01:00
If HasOutputDeviceAvailable() Then
2016-09-07 18:50:38 +02:00
2018-02-22 03:40:20 +01:00
Logger.Debug("SoundEffect [" & soundFile & "]")
2016-09-07 18:50:38 +02:00
2018-02-22 03:40:20 +01:00
sound.Play(volume, pitch, pan)
2016-09-07 18:50:38 +02:00
2018-02-22 03:40:20 +01:00
If stopMusic = True Then
MusicManager.PauseForSound(sound)
2016-09-07 18:50:38 +02:00
End If
2018-02-22 03:40:20 +01:00
2016-09-07 18:50:38 +02:00
Else
2018-02-22 03:40:20 +01:00
Logger.Log(Logger.LogTypes.ErrorMessage, "Failed to play sound: no audio device available.")
2016-09-07 18:50:38 +02:00
End If
2018-02-22 03:40:20 +01:00
2016-09-07 18:50:38 +02:00
End If
2018-02-22 03:40:20 +01:00
2016-09-07 18:50:38 +02:00
End If
End Sub
2018-02-22 03:40:20 +01:00
Public Shared Sub PlayPokemonCry(pokemonId As Integer)
PlaySound("Cries/" + pokemonId.ToString(), 0F, 0F, Volume * POKEMON_CRY_VOLUME_MULTIPLIER, False)
2016-09-07 18:50:38 +02:00
End Sub
2018-02-22 03:40:20 +01:00
Public Shared Sub PlayPokemonCry(pokemonId As Integer, pitch As Single, pan As Single)
PlaySound("Cries/" + pokemonId.ToString(), pitch, pan, Volume * POKEMON_CRY_VOLUME_MULTIPLIER, False)
2016-09-07 18:50:38 +02:00
End Sub
2018-02-22 03:40:20 +01:00
Public Shared Sub PlayPokemonCry(pokemonId As Integer, pitch As Single, pan As Single, volume As Single)
PlaySound("Cries/" + pokemonId.ToString(), pitch, pan, volume * POKEMON_CRY_VOLUME_MULTIPLIER, False)
2016-09-07 18:50:38 +02:00
End Sub
End Class