C++/VB - Crazy sound in Directsound

Asked By Ji
24-Mar-08 07:33 PM
I'm using Directsound to play two wavs.  Sound 1 is a looped wav and is run
with the looped flag set in Play().  Sound 2 is a sound that lasts about a
second and isn't looped.  Sound 1 turns off asynchronously via a button
control (using Stop()).  The next occurrance of sound 1 after sound 2 is
turned off has a loud unwanted pop that happens at the beginning of it.

I've tried everything I can think of and have started shooting in the dark.
Set the play position in the buffers to zero when turning sound 1 off,
polling sound 1 and waiting to turn it off until it's at a place I know is a
zero crossing.  I even tried releasing all the buffers and then
reinitializing everything after turning sound 1 off, but still get the pop
that goes along with the next occurrance of sound 2.  How to fix?  At least
one demo I've seen of how to use Directsound also has a similar problem.
Vista
(1)
XP
(1)
DirectShow
(1)
XAudio
(1)
Directsound
(1)
Windows
(1)
Audio
(1)
Programatic
(1)
  Chris P. replied...
24-Mar-08 10:06 PM
I think it's a bug that causes garbage to get left behind in the primary
buffer.  For single shot sounds you can get by it by padding the end of the
buffer with silence.  Not sure how you could fix this for a looping buffer,
maybe try starting the volume at -10000 (-100dB) and then ramping it up?

Does it happen with both Software and Hardware buffers?

--
http://www.chrisnet.net/code.htm
[MS MVP for DirectShow / MediaFoundation]
  Ji replied...
25-Mar-08 01:08 AM
Hi Chris,

Incidently the problem is at the beginning of the unlooped sound only, not
as I typo'ed below.  Also, if I run sound 1 unlooped for testing, then
there's no problem with sound 2.

I've set the flags to force hardware and software buffers only, with no
effect on it.  I've set just about every other usable combination of flags as
well, with no effect.

I tried your volume suggestion but it seemed to have no effect on the problem.

Your suggestion about garbage being left behind in the primary buffer is
interesting.   Sound two is actually briefer than I said and I can't honestly
tell by ear if the pop is at the beginning or at the end.  I'm too worn out
by it at the moment to scope it or look at a recording of it, to check :)

I would like to try your suggestion of padding the primary buffer with
silence.  How should I do that?   Is there a programatic way to clear or zero
the primary buffer or pointers?  I could load and play a short wav of silence
as well, at the end of sound 1, but may run into time constraints with that.
I might try making the looped wav smaller than the unlooped one with trailing
silence on the unlooped one, here too in a minute.  Or perhaps is there a way
to load a long wav of silence into the primary buffer to clear it, without
playing it?


Thanks,
Jim
  Chris P. replied...
25-Mar-08 01:59 PM
You can't access the primary buffer directly when using secondary buffers.
My statement about garbage in the buffer is only a guess based on what I've
heard and experienced and is not proven.  The primary buffer is running any
time any of your associated secondary buffers are running.  Perhaps it is
enough to have a silence buffer on continuous loop to keep the primary
buffer running.  I'm not sure if it is the primary buffer "restart" that
causes the problem or the actual mixing of specific secondary buffers.

--
http://www.chrisnet.net/code.htm
[MS MVP for DirectShow / MediaFoundation]
  Ji replied...
26-Mar-08 01:55 AM
Running a loop of silence for the duration of the program has solved that
problem.  About 1/2 second of silence looped.  Thanks!

I now notice light static intermittently when my original looped sound is
turned off, which I can live with.  Would be good to address that too though.
Setting the vol of that buffer to -10000 before stopping has no effect.


Jim
  Chris P. replied...
26-Mar-08 09:55 AM
Great.


Light static that appears at the moment you stop and fades away or
continuous light static any time the looped buffer isn't running?

If possible make sure all your samples are at the same sample rate.  The
resampler in Windows isn't that good.  Do make sure that the resampler
quality is set to the highest setting (default on XP) by going to Sounds
and Audio Settings -> Speaker settings Advanced -> Performance -> Sample
rate conversion quality set to 'Best'.

--
http://www.chrisnet.net/code.htm
[MS MVP for DirectShow / MediaFoundation]
  Ji replied...
26-Mar-08 04:14 PM
The audio settings are as you described.

The sound I'm talking about now is a brief crackle when the looped sound
I've been talking about is turned off only.  There should be silence at that
point.  Nothing else is running there except the silence loop.

That crackle happens maybe 50% of the time and reminds me of what you might
hear on an AM radio from switching a bad light switch.  Low volume though,
almost liveable, but seeking perfection ;)

Setting the volume on that sound to minimum before turning it off has no
effect on the crackle.  Incidentally, the silence loop must have its buffer
set to high volume before it will do its magic fix.

Jim
  Chris P. replied...
26-Mar-08 06:04 PM
If you are seeking perfection I would not use DirectSound at all :)


Now that is interesting, I did not expect that.

--
http://www.chrisnet.net/code.htm
[MS MVP for DirectShow / MediaFoundation]
  Ji replied...
26-Mar-08 09:24 PM
I shouldn't have phrased it exactly that way.  I tried only 0, which fixed
the problem, and -10000, which had no effect.  I can't say -9999 wouldn't
work.

I'm new to windows audio.  What is considered superior to Directsound for
playing wav files?
  Chris P. replied...
27-Mar-08 11:31 AM
Fair enough.  It may be smart enough to disable the buffer if the volume is
'off'.


Depends what you're using it for.  For game audio (or similar) I would look
at the new XNA stuff, multi-channel use ASIO (3rd party API), Vista general
use WASAPI.  DirectSound has too many bugs they never bothered to fix so I
don't use it for new applications if can be helped.  I did use it for a
full-duplex voice chat app and it was a PITA.

I would think that either Xact or XAudio 2 should suit your needs.  XACT is
easy to use high level, XAudio is a more complete direct replacement for
DirectSound.

--
http://www.chrisnet.net/code.htm
[MS MVP for DirectShow / MediaFoundation]
Create New Account