home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / VBSamples / Demos / AirHockey / cAudio.cls next >
Encoding:
Visual Basic class definition  |  2001-10-08  |  4.7 KB  |  164 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "cAudio"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. Option Explicit
  15.  
  16. 'We will keep our Audio vars here
  17. Private dmPerf As DirectMusicPerformance8
  18. Private dmLoad As DirectMusicLoader8
  19. Private dmMusic As DirectMusicSegment8
  20. Private dmMusicPath As DirectMusicAudioPath8
  21.  
  22. Private dmSegBank As cAudioFile
  23. Private dmSegHit As cAudioFile
  24. Private dmScore As cAudioFile
  25.  
  26. Public PlaySounds As Boolean
  27. Public PlayMusic As Boolean
  28.  
  29. Private mlSoundVolume As Long
  30. Private mlMusicVolume As Long
  31.  
  32. Private Sub InitializeBackgroundMusic(ByVal sPath As String)
  33.     If dmMusicPath Is Nothing Then 'We haven't created our path yet
  34.         Set dmMusicPath = dmPerf.CreateStandardAudioPath(DMUS_APATH_SHARED_STEREOPLUSREVERB, 128, True)
  35.     End If
  36.     If Not (dmMusic Is Nothing) Then
  37.         dmMusic.Unload dmMusicPath
  38.         Set dmMusic = Nothing
  39.     End If
  40.     Set dmMusic = dmLoad.LoadSegment(sPath)
  41.     dmMusic.Download dmMusicPath
  42.     dmMusic.SetStandardMidiFile
  43. End Sub
  44.  
  45. Public Sub StartBackgroundMusic()
  46.     If Not PlayMusic Then Exit Sub
  47.     If Not (dmMusic Is Nothing) Then
  48.         'Keep repeating over and over again
  49.         dmMusic.SetRepeats INFINITE
  50.         dmPerf.PlaySegmentEx dmMusic, DMUS_SEGF_DEFAULT, 0, dmMusicPath, dmMusicPath
  51.     End If
  52. End Sub
  53.  
  54. Public Sub StopBackgroundMusic()
  55.     If Not (dmMusic Is Nothing) Then
  56.         'Lets just stop
  57.         dmPerf.StopEx dmMusic, 0, 0
  58.     End If
  59. End Sub
  60.  
  61. Public Sub PlayBankSound()
  62.     If Not PlaySounds Then Exit Sub
  63.     'Play the sound that happens when the puck hits the side wall
  64.     dmSegBank.Play dmPerf
  65. End Sub
  66.  
  67. Public Sub PlayHitSound()
  68.     If Not PlaySounds Then Exit Sub
  69.     'Play the sound that happens when a paddle hits the puck
  70.     dmSegHit.Play dmPerf
  71. End Sub
  72.  
  73. Public Sub PlayScoreSound()
  74.     If Not PlaySounds Then Exit Sub
  75.     'Play the sound that happens when we score
  76.     dmScore.Play dmPerf
  77. End Sub
  78.  
  79. Public Property Let MusicVolume(ByVal lVol As Long)
  80.     mlMusicVolume = lVol
  81.     'Actually set the volume
  82.     If Not (dmMusicPath Is Nothing) Then dmMusicPath.SetVolume lVol, 0
  83. End Property
  84.  
  85. Public Property Get MusicVolume() As Long
  86.     MusicVolume = mlMusicVolume
  87. End Property
  88.  
  89. Public Property Let SoundVolume(ByVal lVol As Long)
  90.     mlSoundVolume = lVol
  91.     'Actually set the volume
  92.     If Not (dmPerf Is Nothing) Then
  93.         If Not (dmPerf.GetDefaultAudioPath Is Nothing) Then dmPerf.GetDefaultAudioPath.SetVolume lVol, 0
  94.     End If
  95. End Property
  96.  
  97. Public Property Get SoundVolume() As Long
  98.     SoundVolume = mlSoundVolume
  99. End Property
  100.  
  101. Public Function InitAudio() As Boolean
  102.     
  103.     Dim lCount As Long, dma As DMUS_AUDIOPARAMS
  104.     
  105.     InitAudio = True
  106.     On Error GoTo FailedInit
  107.     'Create our objects
  108.     Set dmPerf = dx.DirectMusicPerformanceCreate
  109.     Set dmLoad = dx.DirectMusicLoaderCreate
  110.     
  111.     'Create a default audio path
  112.     dmPerf.InitAudio frmAir.hwnd, DMUS_AUDIOF_ALL, dma, , DMUS_APATH_SHARED_STEREOPLUSREVERB, 128
  113.     
  114.     'Create the sound objects
  115.     Set dmSegBank = New cAudioFile
  116.     Set dmSegHit = New cAudioFile
  117.     Set dmScore = New cAudioFile
  118.     'Load each of the sounds
  119.     dmSegBank.InitSounds dmPerf, dmLoad, App.path & "\sounds\", "bank", ".wav"
  120.     dmSegHit.InitSounds dmPerf, dmLoad, App.path & "\sounds\", "hit", ".wav"
  121.     dmScore.InitSounds dmPerf, dmLoad, App.path & "\sounds\", "score", ".wav", True
  122.     
  123.     InitializeBackgroundMusic App.path & "\sounds\music.mid"
  124.     'Init the volume
  125.     SoundVolume = mlSoundVolume
  126.     MusicVolume = mlMusicVolume
  127.     Exit Function
  128.  
  129. FailedInit:
  130.     InitAudio = False
  131. End Function
  132.  
  133. Private Sub Class_Initialize()
  134.     PlaySounds = True
  135.     Set dmSegBank = Nothing
  136.     Set dmSegHit = Nothing
  137.     Set dmScore = Nothing
  138.     Set dmMusic = Nothing
  139.     Set dmPerf = Nothing
  140.     Set dmLoad = Nothing
  141. End Sub
  142.  
  143. Private Sub Class_Terminate()
  144.     'On Error Resume Next
  145.     'Unload all of our sounds off of the audio path and destroy them
  146.     StopBackgroundMusic
  147.     Set dmSegBank = Nothing
  148.     Set dmSegHit = Nothing
  149.     Set dmScore = Nothing
  150.     If Not (dmMusic Is Nothing) Then
  151.         dmMusic.Unload dmMusicPath
  152.         Set dmMusic = Nothing
  153.     End If
  154.     
  155.     Set dmMusicPath = Nothing
  156.     If Not (dmPerf Is Nothing) Then
  157.         'Closedown
  158.         dmPerf.CloseDown
  159.     End If
  160.     'Destroy the rest of the objects
  161.     Set dmPerf = Nothing
  162.     Set dmLoad = Nothing
  163. End Sub
  164.