cphx
8/15/2010 11:08:00 PM
i worked out some errors. please refer to this code instead. The
problem is still persisting, anyway :(
frm_main.frm
Dim WaveFormat As WAVEFORMATEX
Dim DS_BufferDesc As DSBUFFERDESC
Private Sub cmd_play_stream_Click()
Dim PtrWavData As Long
Call DSound_OpenWav(App.Path & "\Sound.wav", 1, WaveFormat)
PtrWavData = DSound_GetPtrWavData()
DS_BufferDesc.lBufferBytes = Wav_ChunkSize
MsgBox WaveFormat.lSamplesPerSec
i = 0
Call DSound_GetWavData(PtrWavData + (Wav_ChunkSize * i),
Wav_ChunkSize)
Call DSound_SetPriBuffer(Wav_Buffer(), Wav_ChunkSize,
DS_BufferDesc, WaveFormat)
Call DSound_PriBuffer_Play
Call DSound_CloseWav
End Sub
Private Sub Form_Load()
Call DSound_Init
End Sub
mod_dsound.bas
Dim DX As DirectX7
Dim DS As DirectSound
Global DS_Buffer_Pri As DirectSoundBuffer
Global DS_Buffer_Sec As DirectSoundBuffer
Global Wav_Buffer() As Byte
Global Wav_ChunkSize As Long
Private Type Wave_Format_Ex
Magic As String * 12
FormatID As String * 4
BlockSize As Long
FormatTag As Integer
Channels As Integer
SamplesPerSec As Long
AvgBytesPerSec As Long
BlockAlign As Integer
BitsPerSample As Integer
End Type
Public Sub DSound_Init()
' Init Direct X / Direct Sound
Set DX = New DirectX7
Set DS = DX.DirectSoundCreate("")
' Set the DirectSound object's cooperative level (Priority gives us
sole control)
DS.SetCooperativeLevel frm_main.hWnd, DSSCL_PRIORITY
End Sub
Public Function DSound_OpenWav(File$, Start%, WaveFormat As
WAVEFORMATEX)
Dim Buffer As Wave_Format_Ex
Open File$ For Binary As #1
Get #1, Start%, Buffer
With WaveFormat
.lAvgBytesPerSec = Buffer.AvgBytesPerSec
.lSamplesPerSec = Buffer.SamplesPerSec
.nBitsPerSample = Buffer.BitsPerSample
.nBlockAlign = Buffer.BlockAlign
.nChannels = Buffer.Channels
.nFormatTag = Buffer.FormatTag
End With
End Function
Public Function DSound_GetPtrWavData(Optional Start%) As Long
Dim Buffer As String * 4
Dim StartFlag As Byte
If Start% <> 0 Then StartFlag = 1
For i = 1 + Start% - StartFlag To LOF(1)
Get #1, i, Buffer
If UCase(Buffer) = "DATA" Then
DSound_GetPtrWavData = i + 4
Get #1, , Wav_ChunkSize
Exit Function
End If
Next i
DSound_GetPtrWavData = -1
End Function
Public Function DSound_GetWavData(Start As Long, ChunkSize As Long)
ReDim Wav_Buffer(ChunkSize)
Get #1, Start, Wav_Buffer()
End Function
Public Function DSound_CloseWav()
Close #1
End Function
Function DSound_SetPriBuffer(Data() As Byte, BlockSize As Long,
BufferDesc As DSBUFFERDESC, WaveFormat As WAVEFORMATEX)
Set DS_Buffer_Pri = DS.CreateSoundBuffer(BufferDesc, WaveFormat)
' Load the buffer with data
DS_Buffer_Pri.WriteBuffer 0, BlockSize, Data(1),
DSBLOCK_ENTIREBUFFER
End Function
Function DSound_PriBuffer_Play()
DS_Buffer_Pri.Play DSBPLAY_DEFAULT
End Function