home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Software Sampler / Visual_Basic_Software_Sampler_Visual_Basic_Programmers_Journal_June_1996.iso / issues / 04apr96 / code / p90.txt < prev    next >
Text File  |  1996-04-24  |  9KB  |  317 lines

  1. LISTING 1.
  2. Option Explicit
  3. '-------------------------------------------------------
  4. ' WAVEMIX.BAS
  5. ' This module contains declarations for all the 
  6. ' functions in the WaveMix DLL, and provides some
  7. ' higher-level Basic functions to make using WaveMix 
  8. ' simpler.
  9. '-------------------------------------------------------
  10. Global hMixSession As Long
  11. Global lpWaveMix() As Long
  12. Global WaveHandle As Long
  13. Global WAVMIX_Quiet As Integer
  14.  
  15. Global Const WAVEMIX_MAXCHANNELS = 8
  16.  
  17. Type tChannelInfo
  18.     Loops As Long
  19.     WaveFile As String
  20. End Type
  21.  
  22. Type tWaveMixInfo
  23.     wSize As Integer
  24.     bVersionMajor As String * 1
  25.     bVersionMinor As String * 1
  26.     szDate(12) As String
  27.     dwFormats As Long
  28. End Type
  29.  
  30. Type tMixConfig
  31.     wSize As Integer
  32.     dwFlagsLo As Integer
  33.     dwFlagsHi As Integer
  34.     wChannels As Integer
  35.     wSamplingRate As Integer
  36. End Type
  37.  
  38. Private Type tMixPlayParams
  39.     wSize                As Integer
  40.     hMixSessionLo        As Integer
  41.     hMixSessionHi        As Integer
  42.     iChannelLo        As Integer
  43.     iChannelHi        As Integer
  44.     lpMixWaveLo        As Integer
  45.     lpMixWaveHi        As Integer
  46.     hWndNotifyLo        As Integer
  47.     hWndNotifyHi        As Integer
  48.     dwFlagsLo            As Integer
  49.     dwFlagsHi            As Integer
  50.     wLoops                As Integer
  51. End Type
  52.  
  53. Declare Function WaveMixInit Lib _
  54.     "WAVMIX32.DLL" ()As Long
  55. Declare Function WaveMixConfigureInit Lib _
  56.     "WAVMIX32.DLL" (lpConfig As tMixConfig) As Long
  57. Declare Function WaveMixActivate Lib "WAVMIX32.DLL" _
  58.     (ByVal hMixSession As Long, ByVal fActivate As _
  59.     Integer) As Long
  60. Declare Function WaveMixOpenWave Lib "WAVMIX32.DLL" _
  61.     (ByVal hMixSession As Long, ByVal szWaveFilename _
  62.     As String, ByVal hInst As Long, ByVal dwFlags _
  63.     As Long) As Long
  64. Declare Function WaveMixOpenChannel Lib "WAVMIX32.DLL" _
  65.     (ByVal hMixSession As Long, ByVal iChannel As Long, _
  66.     ByVal dwFlags As Long) As Long
  67. Declare Function WaveMixPlay Lib "WAVMIX32.DLL" _
  68.     (lpMixPlayParams As Any) As Integer
  69. Declare Function WaveMixFlushChannel Lib _ 
  70.     "WAVMIX32.DLL" _
  71.     (ByVal hMixSession As Long, ByVal iChannel As _
  72.     Integer, ByVal dwFlags As Long) As Integer
  73. Declare Function WaveMixCloseChannel Lib 
  74.     "WAVMIX32.DLL" _
  75.     (ByVal hMixSession As Long, ByVal iChannel As _
  76.     Integer, ByVal dwFlags As Long) As Integer
  77. Declare Function WaveMixFreeWave Lib "WAVMIX32.DLL" _
  78.     (ByVal hMixSession As Long, _
  79.     ByVal lpMixWave As Long) _
  80.     As Integer
  81. Declare Function WaveMixCloseSession Lib _
  82.     "WAVMIX32.DLL"(ByVal hMixSession As Long) _
  83.     As Integer
  84. Declare Sub WaveMixPump Lib "WAVMIX32.DLL" ()
  85. Declare Function WaveMixGetInfo Lib "WAVMIX32.DLL" _
  86.     (lpWaveMixInfo As tWaveMixInfo) As Integer
  87.  
  88. '-------------------------------------------------------
  89. ' These two functions are needed to reverse the word 
  90. ' ordering of fields in the WaveMixPlay function.
  91. '-------------------------------------------------------
  92. Private Function HiWord(ByVal l As Long) As Integer
  93.     l = l \ &H10000
  94.     HiWord = Val("&H" & Hex$(l))
  95. End Function
  96.  
  97. Private Function LoWord(ByVal l As Long) As Integer
  98.     l = l And &HFFFF&
  99.     LoWord = Val("&H" & Hex$(l))
  100. End Function
  101.  
  102. Sub WAVMIX_SetFile(FileName As String, AChannel As Long)
  103. '-------------------------------------------------------
  104. ' Assign a new wave file, FileName, to the specified 
  105. ' channel, AChannel.  If this channel is currently 
  106. ' assigned another wave file, stop playing the channel
  107. ' and free the active wave file.
  108. '-------------------------------------------------------
  109. Dim rc As Long
  110.  
  111.     If WAVMIX_Quiet Then Exit Sub
  112.  
  113.     If AChannel > UBound(lpWaveMix) Then
  114.         ReDim Preserve lpWaveMix(AChannel)
  115.         WaveHandle = AChannel
  116.     End If
  117.     
  118.     ' If another wave is currently assigned to this
  119.     ' channel, free it.
  120.     If lpWaveMix(AChannel) <> 0 Then
  121.         WAVMIX_StopChannel AChannel
  122.         rc = WaveMixFreeWave(hMixSession, _
  123.             lpWaveMix(AChannel))
  124.     End If
  125.  
  126.     ' Open the new wave and assign it to this channel.
  127.     lpWaveMix(AChannel) = WaveMixOpenWave(hMixSession, _
  128.         FileName, 0, 0)
  129.     rc = WaveMixOpenChannel(hMixSession, AChannel, 0)
  130. End Sub
  131.  
  132.  
  133. Sub WAVMIX_Close()
  134. '-------------------------------------------------------
  135. ' Stop playing all channels and free all wave files, 
  136. ' then close down this WaveMix session.
  137. '-------------------------------------------------------
  138. Dim rc As Long
  139. Dim i As Integer
  140.  
  141.     If WAVMIX_Quiet Then Exit Sub
  142.  
  143.     If (hMixSession <> 0) Then
  144.         For i = 0 To UBound(lpWaveMix)
  145.             If lpWaveMix(i) <> 0 Then
  146.                 WAVMIX_StopChannel CLng(i)
  147.                 rc = WaveMixFreeWave(hMixSession, _
  148.                     lpWaveMix(i))
  149.             End If
  150.         Next
  151.         rc = WaveMixCloseSession(hMixSession)
  152.         hMixSession = 0
  153.     End If
  154. End Sub
  155.  
  156. Function WAVMIX_InitMixer() As Integer
  157. '-------------------------------------------------------
  158. ' Initialize and activate the WaveMix DLL.
  159. '-------------------------------------------------------
  160. Dim rc As Long
  161. Dim config As tMixConfig
  162.  
  163.     If WAVMIX_Quiet Then Exit Function
  164.  
  165.     WaveHandle = 0
  166.     ReDim lpWaveMix(0)
  167.     ChDir App.Path
  168.     
  169.     config.wSize = Len(config)
  170.     config.dwFlagsHi = 1
  171.     config.dwFlagsLo = 0
  172.     'Allow stereo sound
  173.     config.wChannels = 2
  174.     hMixSession = WaveMixConfigureInit(config)
  175.     rc = WaveMixActivate(hMixSession, True)
  176.  
  177.     If (rc <> 0) Then
  178.         WAVMIX_InitMixer = False
  179.         Call WaveMixCloseSession(hMixSession)
  180.         hMixSession = 0
  181.     Else
  182.         WAVMIX_InitMixer = True
  183.     End If
  184. End Function
  185.  
  186. Sub WAVMIX_StopChannel(ByVal ChannelNum As Long)
  187. '-------------------------------------------------------
  188. ' Stop playing the specified channel.
  189. '-------------------------------------------------------
  190. Dim rc As Integer
  191.  
  192.     If WAVMIX_Quiet Then Exit Sub
  193.     If (hMixSession = 0) Then Exit Sub
  194.     
  195.     rc = WaveMixFlushChannel(hMixSession, ChannelNum, 0)
  196. End Sub
  197.  
  198. Sub WAVMIX_Activate(Activate As Long)
  199. '-------------------------------------------------------
  200. ' Activate the WaveMix DLL.
  201. '-------------------------------------------------------
  202. Dim rc As Integer
  203.  
  204.     If WAVMIX_Quiet Then Exit Sub
  205.     If (hMixSession = 0) Then Exit Sub
  206.  
  207.     rc = WaveMixActivate(hMixSession, Activate)
  208. End Sub
  209.  
  210. Sub WAVMIX_PlayChannel(ChannelNum As Long, _
  211.     LoopWave As Long)
  212. '-------------------------------------------------------
  213. ' Play a specified channel, and indicate whether the 
  214. ' sound should be looped.
  215. '-------------------------------------------------------
  216. Dim params As tMixPlayParams
  217. Dim rc As Long
  218.  
  219.     If WAVMIX_Quiet Then Exit Sub
  220.     If ChannelNum > UBound(lpWaveMix) Then Exit Sub
  221.     If (hMixSession = 0) Then Exit Sub
  222.  
  223.     params.wSize = Len(params)
  224.     params.hMixSessionLo = LoWord(hMixSession)
  225.     params.hMixSessionHi = HiWord(hMixSession)
  226.     params.iChannelLo = LoWord(ChannelNum)
  227.     params.iChannelHi = HiWord(ChannelNum)
  228.     params.lpMixWaveLo = LoWord(lpWaveMix(ChannelNum))
  229.     params.lpMixWaveHi = HiWord(lpWaveMix(ChannelNum))
  230.     params.hWndNotifyLo = 0
  231.     params.hWndNotifyHi = 0
  232.     params.dwFlagsHi = 5
  233.     params.dwFlagsLo = 0
  234.     params.wLoops = LoopWave
  235.     rc = WaveMixPlay(params)
  236. End Sub
  237.  
  238.  
  239.  
  240. LISTING 2.
  241. Option Explicit
  242. '-------------------------------------------------------
  243. ' cWaveMix Class - encapsulates the WavMix32 DLL.
  244. ' Visual Basic 4.0
  245. ' written by Mark Pruett
  246. '-------------------------------------------------------
  247.  
  248. Dim Channel(0 To 7) As tChannelInfo
  249.  
  250. Public CurrentChannel As Long
  251.  
  252. '-------------------------------------------------------
  253. ' The Initialize Event - makes sure the WaveMix DLL is
  254. ' started properly.
  255. '-------------------------------------------------------
  256. Private Sub Class_Initialize()
  257.     If Not WAVMIX_InitMixer() Then
  258.         MsgBox "Unable to Initialize WaveMix DLL", _
  259.             vbOKOnly Or vbExclamation, _
  260.             "WaveMix Error"
  261.         End
  262.     End If
  263. End Sub
  264.  
  265. '-------------------------------------------------------
  266. ' The Terminate Event- makes sure the WaveMix DLL is 
  267. ' shut down properly.
  268. '-------------------------------------------------------
  269. Private Sub Class_Terminate()
  270.     WAVMIX_Close
  271. End Sub
  272.  
  273. '-------------------------------------------------------
  274. ' The Play Method-starts playing the wave file 
  275. ' associated with the current channel.
  276. '-------------------------------------------------------
  277. Public Sub Play()
  278.     WAVMIX_PlayChannel CurrentChannel, _
  279.         Channel(CurrentChannel).Loops
  280. End Sub
  281.  
  282. '-------------------------------------------------------
  283. ' The FileName Property - associates the current channel
  284. ' with a particular wave file.
  285. '-------------------------------------------------------
  286. Public Property Get FileName() As String
  287.     FileName = Channel(CurrentChannel).WaveFile
  288. End Property
  289.  
  290. Public Property Let FileName(ByVal AFileName As String)
  291.     Channel(CurrentChannel).WaveFile = AFileName
  292.     WAVMIX_SetFile Channel(CurrentChannel).WaveFile, _
  293.         CurrentChannel
  294. End Property
  295.  
  296. '-------------------------------------------------------
  297. ' The Quit Method - Stops the current channel if it's
  298. ' playing.
  299. '-------------------------------------------------------
  300. Public Sub Quit()
  301.     WAVMIX_StopChannel CurrentChannel
  302. End Sub
  303.  
  304. '-------------------------------------------------------
  305. ' The AutoLoop Property - determines if the current 
  306. ' channel will loop its wave file.
  307. '-------------------------------------------------------
  308. Public Property Get AutoLoop()
  309.     AutoLoop = Channel(CurrentChannel).Loops
  310. End Property
  311.  
  312. Public Property Let AutoLoop(LoopWave)
  313.     Channel(CurrentChannel).Loops = LoopWave
  314. End Property
  315.  
  316.  
  317.