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 >
Wrap
Text File
|
1996-04-24
|
9KB
|
317 lines
LISTING 1.
Option Explicit
'-------------------------------------------------------
' WAVEMIX.BAS
' This module contains declarations for all the
' functions in the WaveMix DLL, and provides some
' higher-level Basic functions to make using WaveMix
' simpler.
'-------------------------------------------------------
Global hMixSession As Long
Global lpWaveMix() As Long
Global WaveHandle As Long
Global WAVMIX_Quiet As Integer
Global Const WAVEMIX_MAXCHANNELS = 8
Type tChannelInfo
Loops As Long
WaveFile As String
End Type
Type tWaveMixInfo
wSize As Integer
bVersionMajor As String * 1
bVersionMinor As String * 1
szDate(12) As String
dwFormats As Long
End Type
Type tMixConfig
wSize As Integer
dwFlagsLo As Integer
dwFlagsHi As Integer
wChannels As Integer
wSamplingRate As Integer
End Type
Private Type tMixPlayParams
wSize As Integer
hMixSessionLo As Integer
hMixSessionHi As Integer
iChannelLo As Integer
iChannelHi As Integer
lpMixWaveLo As Integer
lpMixWaveHi As Integer
hWndNotifyLo As Integer
hWndNotifyHi As Integer
dwFlagsLo As Integer
dwFlagsHi As Integer
wLoops As Integer
End Type
Declare Function WaveMixInit Lib _
"WAVMIX32.DLL" ()As Long
Declare Function WaveMixConfigureInit Lib _
"WAVMIX32.DLL" (lpConfig As tMixConfig) As Long
Declare Function WaveMixActivate Lib "WAVMIX32.DLL" _
(ByVal hMixSession As Long, ByVal fActivate As _
Integer) As Long
Declare Function WaveMixOpenWave Lib "WAVMIX32.DLL" _
(ByVal hMixSession As Long, ByVal szWaveFilename _
As String, ByVal hInst As Long, ByVal dwFlags _
As Long) As Long
Declare Function WaveMixOpenChannel Lib "WAVMIX32.DLL" _
(ByVal hMixSession As Long, ByVal iChannel As Long, _
ByVal dwFlags As Long) As Long
Declare Function WaveMixPlay Lib "WAVMIX32.DLL" _
(lpMixPlayParams As Any) As Integer
Declare Function WaveMixFlushChannel Lib _
"WAVMIX32.DLL" _
(ByVal hMixSession As Long, ByVal iChannel As _
Integer, ByVal dwFlags As Long) As Integer
Declare Function WaveMixCloseChannel Lib
"WAVMIX32.DLL" _
(ByVal hMixSession As Long, ByVal iChannel As _
Integer, ByVal dwFlags As Long) As Integer
Declare Function WaveMixFreeWave Lib "WAVMIX32.DLL" _
(ByVal hMixSession As Long, _
ByVal lpMixWave As Long) _
As Integer
Declare Function WaveMixCloseSession Lib _
"WAVMIX32.DLL"(ByVal hMixSession As Long) _
As Integer
Declare Sub WaveMixPump Lib "WAVMIX32.DLL" ()
Declare Function WaveMixGetInfo Lib "WAVMIX32.DLL" _
(lpWaveMixInfo As tWaveMixInfo) As Integer
'-------------------------------------------------------
' These two functions are needed to reverse the word
' ordering of fields in the WaveMixPlay function.
'-------------------------------------------------------
Private Function HiWord(ByVal l As Long) As Integer
l = l \ &H10000
HiWord = Val("&H" & Hex$(l))
End Function
Private Function LoWord(ByVal l As Long) As Integer
l = l And &HFFFF&
LoWord = Val("&H" & Hex$(l))
End Function
Sub WAVMIX_SetFile(FileName As String, AChannel As Long)
'-------------------------------------------------------
' Assign a new wave file, FileName, to the specified
' channel, AChannel. If this channel is currently
' assigned another wave file, stop playing the channel
' and free the active wave file.
'-------------------------------------------------------
Dim rc As Long
If WAVMIX_Quiet Then Exit Sub
If AChannel > UBound(lpWaveMix) Then
ReDim Preserve lpWaveMix(AChannel)
WaveHandle = AChannel
End If
' If another wave is currently assigned to this
' channel, free it.
If lpWaveMix(AChannel) <> 0 Then
WAVMIX_StopChannel AChannel
rc = WaveMixFreeWave(hMixSession, _
lpWaveMix(AChannel))
End If
' Open the new wave and assign it to this channel.
lpWaveMix(AChannel) = WaveMixOpenWave(hMixSession, _
FileName, 0, 0)
rc = WaveMixOpenChannel(hMixSession, AChannel, 0)
End Sub
Sub WAVMIX_Close()
'-------------------------------------------------------
' Stop playing all channels and free all wave files,
' then close down this WaveMix session.
'-------------------------------------------------------
Dim rc As Long
Dim i As Integer
If WAVMIX_Quiet Then Exit Sub
If (hMixSession <> 0) Then
For i = 0 To UBound(lpWaveMix)
If lpWaveMix(i) <> 0 Then
WAVMIX_StopChannel CLng(i)
rc = WaveMixFreeWave(hMixSession, _
lpWaveMix(i))
End If
Next
rc = WaveMixCloseSession(hMixSession)
hMixSession = 0
End If
End Sub
Function WAVMIX_InitMixer() As Integer
'-------------------------------------------------------
' Initialize and activate the WaveMix DLL.
'-------------------------------------------------------
Dim rc As Long
Dim config As tMixConfig
If WAVMIX_Quiet Then Exit Function
WaveHandle = 0
ReDim lpWaveMix(0)
ChDir App.Path
config.wSize = Len(config)
config.dwFlagsHi = 1
config.dwFlagsLo = 0
'Allow stereo sound
config.wChannels = 2
hMixSession = WaveMixConfigureInit(config)
rc = WaveMixActivate(hMixSession, True)
If (rc <> 0) Then
WAVMIX_InitMixer = False
Call WaveMixCloseSession(hMixSession)
hMixSession = 0
Else
WAVMIX_InitMixer = True
End If
End Function
Sub WAVMIX_StopChannel(ByVal ChannelNum As Long)
'-------------------------------------------------------
' Stop playing the specified channel.
'-------------------------------------------------------
Dim rc As Integer
If WAVMIX_Quiet Then Exit Sub
If (hMixSession = 0) Then Exit Sub
rc = WaveMixFlushChannel(hMixSession, ChannelNum, 0)
End Sub
Sub WAVMIX_Activate(Activate As Long)
'-------------------------------------------------------
' Activate the WaveMix DLL.
'-------------------------------------------------------
Dim rc As Integer
If WAVMIX_Quiet Then Exit Sub
If (hMixSession = 0) Then Exit Sub
rc = WaveMixActivate(hMixSession, Activate)
End Sub
Sub WAVMIX_PlayChannel(ChannelNum As Long, _
LoopWave As Long)
'-------------------------------------------------------
' Play a specified channel, and indicate whether the
' sound should be looped.
'-------------------------------------------------------
Dim params As tMixPlayParams
Dim rc As Long
If WAVMIX_Quiet Then Exit Sub
If ChannelNum > UBound(lpWaveMix) Then Exit Sub
If (hMixSession = 0) Then Exit Sub
params.wSize = Len(params)
params.hMixSessionLo = LoWord(hMixSession)
params.hMixSessionHi = HiWord(hMixSession)
params.iChannelLo = LoWord(ChannelNum)
params.iChannelHi = HiWord(ChannelNum)
params.lpMixWaveLo = LoWord(lpWaveMix(ChannelNum))
params.lpMixWaveHi = HiWord(lpWaveMix(ChannelNum))
params.hWndNotifyLo = 0
params.hWndNotifyHi = 0
params.dwFlagsHi = 5
params.dwFlagsLo = 0
params.wLoops = LoopWave
rc = WaveMixPlay(params)
End Sub
LISTING 2.
Option Explicit
'-------------------------------------------------------
' cWaveMix Class - encapsulates the WavMix32 DLL.
' Visual Basic 4.0
' written by Mark Pruett
'-------------------------------------------------------
Dim Channel(0 To 7) As tChannelInfo
Public CurrentChannel As Long
'-------------------------------------------------------
' The Initialize Event - makes sure the WaveMix DLL is
' started properly.
'-------------------------------------------------------
Private Sub Class_Initialize()
If Not WAVMIX_InitMixer() Then
MsgBox "Unable to Initialize WaveMix DLL", _
vbOKOnly Or vbExclamation, _
"WaveMix Error"
End
End If
End Sub
'-------------------------------------------------------
' The Terminate Event- makes sure the WaveMix DLL is
' shut down properly.
'-------------------------------------------------------
Private Sub Class_Terminate()
WAVMIX_Close
End Sub
'-------------------------------------------------------
' The Play Method-starts playing the wave file
' associated with the current channel.
'-------------------------------------------------------
Public Sub Play()
WAVMIX_PlayChannel CurrentChannel, _
Channel(CurrentChannel).Loops
End Sub
'-------------------------------------------------------
' The FileName Property - associates the current channel
' with a particular wave file.
'-------------------------------------------------------
Public Property Get FileName() As String
FileName = Channel(CurrentChannel).WaveFile
End Property
Public Property Let FileName(ByVal AFileName As String)
Channel(CurrentChannel).WaveFile = AFileName
WAVMIX_SetFile Channel(CurrentChannel).WaveFile, _
CurrentChannel
End Property
'-------------------------------------------------------
' The Quit Method - Stops the current channel if it's
' playing.
'-------------------------------------------------------
Public Sub Quit()
WAVMIX_StopChannel CurrentChannel
End Sub
'-------------------------------------------------------
' The AutoLoop Property - determines if the current
' channel will loop its wave file.
'-------------------------------------------------------
Public Property Get AutoLoop()
AutoLoop = Channel(CurrentChannel).Loops
End Property
Public Property Let AutoLoop(LoopWave)
Channel(CurrentChannel).Loops = LoopWave
End Property