Hesitation blues using DirectSound
- From: gilad <gilad@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Thu, 19 May 2005 09:37:17 -0600
I am working on a VB.NET demo that uses a DLL I've created, "madxlib"
(http://www.arbingersys.com/concerns.html#madxlib) to decode an MP3 file to WAV and play it using DirectX/DirectSound. I have it playing, but every time I read in a new block of bytes from the MP3 file there is noticable hesitation in the output.
The way I am playing the audio is as follows:
When "play" is pressed, the SecondaryBuffer ("secBuf") and BufferDescription ("bufDesc") objects are instantiated and the appropriate values are set. The size set for the buffer of secBuf is equal to approximately 2 seconds worth of audio data.
I declare a Byte buffer, "playBuffer" to be half the size of the buffer used by secBuf (playBuffer holds approximately 1 second of audio data). I have a function called "ReadMP3()" that reads from the MP3 file and fills playBuffer with decoded PCM samples.
I call ReadMP3() twice initially, filling the first and second half of secBuf's buffer. I set a flag "lastFill" to indicate which part of the buffer has been filled (in this case the second half). I enable a timer, then set the SecondaryBuffer secBuf to play with the following line:
secBuf.Play(0, BufferPlayFlags.Looping)
The timer code is included below. What it basically does is at every interval that it is called, it checks the lastFill flag and the play position of the buffer, and if the play position IS NOT in the half that needs to be filled, then it fills this half with data. I am not locking the buffer. My thought was that while it was playing one half of the buffer, I could fill the other, and keep switching this way until the file had been played, resulting in seamless play. It hasn't worked.
What am I doing wrong? Is this the wrong way to go about using DirectSound? It plays the file all the way through without problems--I just get the hesitation each time it reads from the file to the buffer. Any help would be appreciated. Thanks, J.A.
Private Sub Timer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs)
If lastFill = 0 Then
' under 50%, fill last half
If (secBuf.PlayPosition / secBuf.Caps.BufferBytes * 100) < 50 Then
If (secBuf.Status.BufferLost) Then secBuf.Restore()
ReadMP3()
secBuf.Write(buffPart, playBuffer, LockFlag.None)
lastFill = 1End If
ElseIf lastFill = 1 Then
' Over 50%, fill first half
If (secBuf.PlayPosition / secBuf.Caps.BufferBytes * 100) > 50 Then
If (secBuf.Status.BufferLost) Then secBuf.Restore()
ReadMP3()
secBuf.Write(0, playBuffer, LockFlag.None)
lastFill = 0
End If End If
End Sub .
- Follow-Ups:
- RE: Hesitation blues using DirectSound
- From: DopeSlice
- RE: Hesitation blues using DirectSound
- Prev by Date: Re: Filter processing speed
- Next by Date: RE: Hesitation blues using DirectSound
- Previous by thread: Dx audio filter----Ultrafunk.
- Next by thread: RE: Hesitation blues using DirectSound
- Index(es):
Relevant Pages
|