Attribute VB_Name = "DXAudio"
'// Programmed by Ervin Louie B. Pe Benito
'// DXAudio.bas
'// Direct Sound module
Option Explicit
Private DS As DirectSound8  'Main Part of DirectSound
Private DSEnum As DirectSoundEnum8  'DirectSound Enumeration
Private DMPerf As DirectMusicPerformance8   'Main Part of DirectMusic
Private DMLoader As DirectMusicLoader8  'DirectMusic Loader
Public Sfxpath As String

Private Type Sounds
    Sound As DirectSoundSecondaryBuffer8    'Holds The Sound File To Play
    DefaultFrequency As Long
End Type

'Sound effects in the game
Public Sfx(1 To 12) As Sounds

'Music Buffer
Private MusicEvent As Long  'A Music Event Handle
Private Music(1 To 9) As DirectMusicSegment8    'Holds The Music Piece To Play

Public Sub InitializeMusic()
    SfxInit
    MusicInit
End Sub

Private Sub SfxInit()
Dim DSBDesc As DSBUFFERDESC 'Describes Sound File

'Retrieve List of Sound Devices
Set DSEnum = Dx.GetDSEnum

'Use Primary Device (1) To Create DirectSound Object
Set DS = Dx.DirectSoundCreate(DSEnum.GetGuid(1))

'Set Cooperative Level So The Game Has The Priority Over Sound
DS.SetCooperativeLevel frmMain.hWnd, DSSCL_PRIORITY

'Create Sound Buffer
DSBDesc.lFlags = DSBCAPS_CTRLFREQUENCY Or DSBCAPS_CTRLPAN Or DSBCAPS_CTRLVOLUME
Set Sfx(1).Sound = DS.CreateSoundBufferFromFile(Sfxpath & "click.wav", DSBDesc)
Set Sfx(2).Sound = DS.CreateSoundBufferFromFile(Sfxpath & "bolohit.wav", DSBDesc)
Set Sfx(3).Sound = DS.CreateSoundBufferFromFile(Sfxpath & "garandhit.wav", DSBDesc)
Set Sfx(4).Sound = DS.CreateSoundBufferFromFile(Sfxpath & "cannon.wav", DSBDesc)
Set Sfx(5).Sound = DS.CreateSoundBufferFromFile(Sfxpath & "stepdirt1.wav", DSBDesc)
Set Sfx(6).Sound = DS.CreateSoundBufferFromFile(Sfxpath & "stepdirt2.wav", DSBDesc)
Set Sfx(7).Sound = DS.CreateSoundBufferFromFile(Sfxpath & "lifeup.wav", DSBDesc)
Set Sfx(8).Sound = DS.CreateSoundBufferFromFile(Sfxpath & "miss.wav", DSBDesc)
Set Sfx(9).Sound = DS.CreateSoundBufferFromFile(Sfxpath & "expup.wav", DSBDesc)
Set Sfx(10).Sound = DS.CreateSoundBufferFromFile(Sfxpath & "dead.wav", DSBDesc)
Set Sfx(11).Sound = DS.CreateSoundBufferFromFile(Sfxpath & "levelup.wav", DSBDesc)
Set Sfx(12).Sound = DS.CreateSoundBufferFromFile(Sfxpath & "comedyclick.wav", DSBDesc)
End Sub


Private Sub MusicInit()
Dim dmParams As DMUS_AUDIOPARAMS

'Setup DirectSound For Music
Set DMPerf = Dx.DirectMusicPerformanceCreate
Set DMLoader = Dx.DirectMusicLoaderCreate
DMPerf.InitAudio frmMain.hWnd, DMUS_AUDIOF_ALL, dmParams, Nothing, DMUS_APATH_DYNAMIC_STEREO, 128
DMPerf.SetMasterAutoDownload True

'Setup Messaging System
DMPerf.AddNotificationType DMUS_NOTIFY_ON_SEGMENT
MusicEvent = Dx.CreateEvent(frmMain)
DMPerf.SetNotificationHandle MusicEvent

'Configure The Loader
DMLoader.SetSearchDirectory App.Path & "\Filipino Music\"
End Sub
    
Public Sub getMusic(sName As String, iIndex As Integer, Optional bMidi As Boolean = True)
'Set The Music File
Music_Set sName, iIndex, bMidi

'Set Music Tempo And Volume
Music_Edit 0, 200

End Sub

Public Sub Music_Set(filename As String, iIndex As Integer, bMidi As Boolean)

'Load The Audio File (.WAV, .MID, .MIDI, .SEG, and .RMI only)
If bMidi = True Then
    'Make This Call If Music Type Is A .MID, .MIDI, or .RMI
    Set Music(iIndex) = DMLoader.LoadSegment(App.Path & "\Filipino Music\" & filename & ".mid")
    Music(iIndex).SetStandardMidiFile
Else
    Set Music(iIndex) = DMLoader.LoadSegment(App.Path & "\Filipino Music\" & filename & ".wav")
End If
End Sub

Public Sub Music_Edit(Optional Tempo As Single = 0, Optional Volume As Long = 201)

'Check To Update Tempo
If Tempo <> 0 Then

    'Make Sure Tempo Is In Range
    If Tempo < 0.01 Then Tempo = 0.01
    If Tempo > 4 Then Tempo = 4
    
    'Apply The Tempo Changes
    DMPerf.SetMasterTempo Tempo
    
End If

'Check To Update Volume
If Volume <> 201 Then
    
    'Make Sure Volume Is In Range
    If Volume < -2500 Then Volume = -2500
    If Volume > 200 Then Volume = 200
    
    'Apply The Volume Changes
    DMPerf.SetMasterVolume Volume
    
End If
    
End Sub

Public Sub Music_Stop(iIndex As Integer)
'Stop The Current Music Playing
DMPerf.StopEx Music(iIndex), 0, DMUS_SEGF_DEFAULT
End Sub

Public Sub Music_Start(iIndex As Integer, Optional Loops As Long = -1)
Music(iIndex).SetRepeats -1 'Set Number of Loops; -1 For Unlimited
DMPerf.PlaySegmentEx Music(iIndex), 0, 0
End Sub

Public Sub Sound_Play(SoundBuffer As Integer)
'Play The Selected Sound
Sfx(SoundBuffer).Sound.Play DSBPLAY_DEFAULT
End Sub

Public Sub Sound_Stop(SoundBuffer As Integer)
'Stop & Reset The Selected Sound
Sfx(SoundBuffer).Sound.Stop
Sfx(SoundBuffer).Sound.SetCurrentPosition 0
End Sub

Public Sub Sound_Pause(SoundBuffer As Integer)
'Pause The Selected Sound
Sfx(SoundBuffer).Sound.Stop
End Sub

Public Sub Sound_Edit(SoundBuffer As Integer, Optional Frequency As Long = 1, Optional Volume As Long = 1, Optional Balance As Integer = 10001)

'Check To Update Frequency
If Frequency <> 1 Then

    'Make Sure Frequency Is In Range
    If Frequency < 100 Then Frequency = 100
    If Frequency > 100000 Then Frequency = 100000
    
    'Apply The Frequency Changes
    Sfx(SoundBuffer).Sound.SetFrequency Frequency
    
End If

'Check If To Update Volume
If Volume <> 1 Then

    'Make Sure Volume Is In Range
    If Volume < -10000 Then Volume = 10000
    If Volume > 0 Then Volume = 0
    
    'Apply The Volume Changes
    Sfx(SoundBuffer).Sound.SetVolume Volume
    
End If

'Check To Update Balance
If Balance <> 10001 Then

    'Make Sure Balance Is In Range
    If Balance < -10000 Then Balance = -10000
    If Balance > 10000 Then Balance = 10000
    
    'Apply The Balance Change
    Sfx(SoundBuffer).Sound.SetPan Balance

End If

End Sub
Public Sub TerminateSounds()
Dim x As Integer
    For x = 1 To 9
        Set Music(x) = Nothing
    Next
    For x = 1 To 12
        Set Sfx(x).Sound = Nothing
    Next
    Set DMPerf = Nothing
    Set DMLoader = Nothing
    Set DS = Nothing
End Sub
