Question

I'm using XAudio2 with SlimDX and I've managed to get it playing a short (~8second) wav file on loop, however as it approaches the end of the first loop, the audio begins to stutter and the stuttering continues into the next loop, getting worse and worse as time goes on.

I turned on the debug profile and in the output window I get these errors:

XAUDIO2: WARNING: Spent 5.63ms in the OnVoiceProcessingPassStart callback
XAUDIO2: WARNING: Spent 5.60ms in the OnVoiceProcessingPassStart callback
XAUDIO2: WARNING: Spent 5.59ms in the OnVoiceProcessingPassStart callback
XAUDIO2: WARNING: Spent 5.69ms in the OnVoiceProcessingPassStart callback

And these coincide with when the stuttering occurs. I am doing nothing in these callbacks (I haven't even added anything to the events), and yet it's slowing down. I've added my code below for reference:

Wave class for holding the data stream and the buffer:

public class Wave
    {
        public WaveStream Data { get; private set; }

        public AudioBuffer Buffer { get; private set; }

        public Wave(string path, bool repeating)
        {
            Data = new WaveStream(path);
            Buffer = new AudioBuffer();
            Buffer.AudioBytes = (int)Data.Length;
            Buffer.AudioData = Data;
            if (repeating)
            {
                Buffer.Flags = BufferFlags.EndOfStream;
            }
            else
            {
                Buffer.Flags = BufferFlags.None;
            }
            Buffer.PlayBegin = 0;
            Buffer.PlayLength = 0;
            Buffer.LoopBegin = 0;
            Buffer.LoopCount = 100;
            Buffer.LoopLength = 0;
        }
    }

Sound class for holding the XAudio engine and the voices, and to cover adding/removing voices:

public class Sound
    {
        private XAudio2 audio;
        private MasteringVoice master;
        private List<SourceVoice> sources;

        public Sound()
        {
            audio = new XAudio2(XAudio2Flags.DebugEngine, ProcessorSpecifier.AnyProcessor);
            master = new MasteringVoice(audio);
            sources = new List<SourceVoice>();
        }

        public void AddSound(Wave wave)
        {
            SlimDX.Multimedia.WaveFormat format = wave.Data.Format;
            SourceVoice source = new SourceVoice(audio, format);
            source.Start();
            source.SubmitSourceBuffer(wave.Buffer);
            sources.Add(source);
        }
    }

And to run it, I use:

Wave wave = new Wave("Music2/untitled.wav", true);
            Sound sound = new Sound();
            sound.AddSound(wave);
Was it helpful?

Solution

Rather embarrassingly this one's my own fault. I had a D3D buffer being recreated and destroyed every frame that I'd forgotten to change to a dynamic buffer. This was causing my memory usage to balloon to several gigabytes in size, which meant there probably wasn't enough to allocate for the music.

Once I removed the memory leak, the music sounded fine.

I'm not sure if the XAudio2 exception is very well-worded though...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top