Hesitation blues using DirectSound

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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 = 1

            End 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
.



Relevant Pages

  • RE: Hesitation blues using DirectSound
    ... am refreshing the buffer on a seperate thread. ... > to WAV and play it using DirectX/DirectSound. ... > used by secBuf (playBuffer holds approximately 1 second of audio data). ...
    (microsoft.public.win32.programmer.directx.audio)
  • Re: Tone generation
    ... Now I'm searching, without results, for a method to play two or more ... different frequences, and effectively it plays two tones together, but ... Is there a method to play two tones togeter, opening the wave device ... to fill a buffer, sent after through the waveoutwrite api. ...
    (microsoft.public.win32.programmer.mmedia)
  • Re: **CAUTION**WMP10 is Malware
    ... > rewrite your file and means I have a bad version of WMP on my machine. ... >>> Pick an mp3 file that you know you can burn ... >>> play it with WMP10 then after it plays check the date of the file. ...
    (microsoft.public.windowsmedia.player)
  • Re: Playing Voice Modem BYTE() Buffer.
    ... I've tried 8-bit linear pcm as well as 16-bit. ... I've also tried unsigned pcm which uses 8 bits per sample. ... It still goes through the .play when I hear ... > what format is the DSound buffer that you just allocated? ...
    (microsoft.public.vb.directx)
  • Is my MP3 files wierd, or Musicmatch Jukebox is too great
    ... play this file using Windows Media Player 9, it seems as it is playing ... I thought that this MP3 file has corrupted somewhat so I would ... attempt to play it using Musicmatch Jukebox, ... there's no sound on these players...except Musicmatch ...
    (microsoft.public.windowsxp.music)