Option Explicit
Private Type FileHeader
lRiff As Long
lFileSize As Long
lWave As Long
lFormat As Long
lFormatLength As Long
End Type
Private Type WaveFormat
wFormatTag As Integer
nChannels As Integer
nSamplesPerSec As Long
nAvgBytesPerSec As Long
nBlockAlign As Integer
wBitsPerSample As Integer
End Type
Private Type ChunkHeader
lType As Long
lLen As Long
End Type
Private DX As New DirectX8
Private DSEnum As DirectSoundEnum8
Private DIS As DirectSound8
Dim DSSecBuffer As DirectSoundSecondaryBuffer8
Private EventsNotify(0) As DSBPOSITIONNOTIFY '*****
Private StopEvent As Long '*****
Implements DirectXEvent8 '*****
Dim WaveData() As Byte
Dim WaveFmt As WaveFormat
Dim DataLength As Long
Private Sub CmdLoad_Click()
Dim BuffDesc As DSBUFFERDESC
Set DSEnum = DX.GetDSEnum
Set DIS = DX.DirectSoundCreate(DSEnum.GetGuid(1))
DIS.SetCooperativeLevel Me.hWnd, DSSCL_NORMAL
Set DSSecBuffer = CreateSoundBufferFromFile(Text1.Text, BuffDesc)
HScrollFreq.Value = DSSecBuffer.GetFrequency() / 3
StopEvent = DX.CreateEvent(Me) '****
'EventsNotify(0).hEventNotify = StopEvent '****
'EventsNotify(0).lOffset = CLng(DataLength / 2) '****
'DSSecBuffer.SetNotificationPositions 1, EventsNotify() '****
End Sub
Private Sub CmdPlay_Click()
DSSecBuffer.SetFrequency (3 * Val(HScrollFreq.Value))
DSSecBuffer.SetVolume HScrollVol.Value
DSSecBuffer.SetCurrentPosition (HScrollPlayFrom.Value / 100) * DataLength
DSSecBuffer.Play DSBPLAY_DEFAULT
End Sub
Private Sub Form_Load()
Text1.Text = "C:\vbsequencer10\data\3816.wav"
End Sub
Private Function CreateSoundBufferFromFile(ByVal FileName As String, ByRef BuffDesc As DSBUFFERDESC) As DirectSoundSecondaryBuffer8
Dim SecBuff As DirectSoundSecondaryBuffer8
Dim FileNum As Integer
Dim f As Long
FileNum = FreeFile
Open FileName For Binary Access Read Lock Write As #1
WaveFmt = WaveReadFormat(FileNum, DataLength)
With BuffDesc.fxFormat
.nFormatTag = WaveFmt.wFormatTag
.nChannels = WaveFmt.nChannels
.nBitsPerSample = WaveFmt.wBitsPerSample
.lSamplesPerSec = WaveFmt.nSamplesPerSec
.nBlockAlign = WaveFmt.nBlockAlign
.lAvgBytesPerSec = WaveFmt.nAvgBytesPerSec
End With
ReDim WaveData(DataLength - 1)
Get FileNum, , WaveData
BuffDesc.lFlags = DSBCAPS_STICKYFOCUS Or DSBCAPS_STATIC Or DSBCAPS_CTRLFREQUENCY Or DSBCAPS_CTRLVOLUME
BuffDesc.lBufferBytes = DataLength
Set SecBuff = DIS.CreateSoundBuffer(BuffDesc)
SecBuff.WriteBuffer 0, DataLength, WaveData(0), DSBLOCK_DEFAULT
Close FileNum
Set CreateSoundBufferFromFile = SecBuff
End Function
Private Function WaveReadFormat(ByVal InFileNum As Integer, ByRef lDataLength As Long) As WaveFormat
Dim header As FileHeader
Dim HdrFormat As WaveFormat
Dim chunk As ChunkHeader
Dim by As Byte
Dim i As Long
Get #InFileNum, 1, header
If header.lRiff <> &H46464952 Then Exit Function
If header.lWave <> &H45564157 Then Exit Function
If header.lFormat <> &H20746D66 Then Exit Function
If header.lFormatLength < 16 Then Exit Function
Get #InFileNum, , HdrFormat
For i = 1 To header.lFormatLength - 16
Get #InFileNum, , by
Next
Get #InFileNum, , chunk
Do While chunk.lType <> &H61746164
For i = 1 To chunk.lLen
Get #InFileNum, , by
Next
Get #InFileNum, , chunk
Loop
lDataLength = chunk.lLen
WaveReadFormat = HdrFormat
End Function
Private Sub DirectXEvent8_DXCallback(ByVal eventid As Long) '****
Select Case eventid '****
Case StopEvent '****
DSSecBuffer.Stop '****
End Select '****
End Sub
Private Sub HScrollPlayTo_Change() '****
DX.DestroyEvent StopEvent '****
StopEvent = DX.CreateEvent(Me) '****
EventsNotify(0).hEventNotify = StopEvent '****
EventsNotify(0).lOffset = ((HScrollPlayTo.Value / 100) * DataLength) '****
DSSecBuffer.SetNotificationPositions 1, EventsNotify() '****
End Sub