Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


C# SlimDX Xaudio2 driving me crazy


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 Grain   Members   -  Reputation: 461

Like
0Likes
Like

Posted 12 July 2013 - 11:46 PM

I have an older C# application that I'm revisiting and it uses SlimDX for the audio. But it used and older (2008) build of SlimDX. I've installed the latest SlimDX SDK and need to make some minor edits to the code as things have changed with it.  For the most part it works. The problem I'm having now is that if I have more than one instance of my Speaker class the audio stutters and has random pops and clicks when before it could handle many instances. The Speaker class is just a simple wrapper around SlimDX that I can feed raw audio samples.
 
Here is the relevant code that changed:
Original

override public bool BufferSoundData(double[] data)
        {
            if (sourceVoice.State.BuffersQueued > 1)
            {
                return false;
            }
            else
            {

                byte[] SoundBytes = new byte[data.Length * bytesPerSample];
                if (waveFormat.BitsPerSample == 8)
                    for (int i = 0; i < SoundBytes.Length; i++)
                    {
                        SoundBytes[i] = (byte)(data[i] * 128 + 128);
                    }
                if (waveFormat.BitsPerSample == 16)
                    for (int i = 0, j = 0; i < SoundBytes.Length; i += 2, j++)
                    {
                        System.Int16 word = (System.Int16)(data[j] * (System.Int16.MaxValue));
                        SoundBytes[i] = (byte)(word % 256);
                        SoundBytes[i + 1] = (byte)(word / 256);
                    }
                if (waveFormat.BitsPerSample == 32)
                    for (int i = 0, j = 0; i < SoundBytes.Length; i += 4, j++)
                    {
                        float flt = (float)data[j];
                        byte[] tmp = System.BitConverter.GetBytes(flt);
                        SoundBytes[i] = tmp[0];
                        SoundBytes[i + 1] = tmp[1];
                        SoundBytes[i + 2] = tmp[2];
                        SoundBytes[i + 3] = tmp[3];
                    }
                    buffer.AudioData = bData; 
                    buffer.AudioBytes = bData.Length; 
                    buffer.Flags = BufferFlags.None; 
                    sourceVoice.SubmitSourceBuffer(buffer); 
                    return true; 
              } 
}

 

New:

override public bool BufferSoundData(double[] data)
        {
            if (sourceVoice.State.BuffersQueued > 1)
            {
                return false;
            }
            else
            {
                byte[] SoundBytes = new byte[data.Length * bytesPerSample];
                if (waveFormat.BitsPerSample == 8)
                    for (int i = 0; i < SoundBytes.Length; i++)
                    {
                        SoundBytes[i] = (byte)(data[i] * 128 + 128);
                    }
                if (waveFormat.BitsPerSample == 16)
                    for (int i = 0, j = 0; i < SoundBytes.Length; i += 2, j++)
                    {
                        System.Int16 word = (System.Int16)(data[j] * (System.Int16.MaxValue));
                        SoundBytes[i] = (byte)(word % 256);
                        SoundBytes[i + 1] = (byte)(word / 256);
                    }
                if (waveFormat.BitsPerSample == 32)
                    for (int i = 0, j = 0; i < SoundBytes.Length; i += 4, j++)
                    {
                        float flt = (float)data[j];
                        byte[] tmp = System.BitConverter.GetBytes(flt);
                        SoundBytes[i] = tmp[0];
                        SoundBytes[i + 1] = tmp[1];
                        SoundBytes[i + 2] = tmp[2];
                        SoundBytes[i + 3] = tmp[3];
                    }

                buffer.AudioData.SetLength(0);
                buffer.AudioData.Write(SoundBytes, 0, SoundBytes.Length);
                buffer.AudioData.Position = 0;
                buffer.AudioBytes = SoundBytes.Length;
                buffer.Flags = BufferFlags.None;
                sourceVoice.SubmitSourceBuffer(buffer);
                return true;
            }
        }

Here is the class definition and constructor

abstract public class Speaker
    {
        abstract public void Dispose();
        abstract public void Play();
        abstract public void Stop();
        abstract public bool BufferSoundData(double[] data);
    }

    public class Speaker_XAudio2 : Speaker
    {
        XAudio2 device;
        MasteringVoice masteringVoice;
        SourceVoice sourceVoice;
        WaveFormat waveFormat;
        int bytesPerSample;
        AudioBuffer buffer;

        public Speaker_XAudio2(short BitsPerSample, short Channels, int SamplesPerSecond)
        {
            WaveFormat format = new SlimDX.Multimedia.WaveFormat();
            format.BitsPerSample = BitsPerSample;
            format.Channels = Channels;
            format.SamplesPerSecond = SamplesPerSecond;
            format.BlockAlignment = (short)(format.Channels * format.BitsPerSample / 8);
            format.AverageBytesPerSecond = format.SamplesPerSecond * format.BlockAlignment;
            format.FormatTag = SlimDX.WaveFormatTag.IeeeFloat;

            device = new XAudio2();
            masteringVoice = new MasteringVoice(device);
            sourceVoice = new SourceVoice(device, format);
            buffer = new AudioBuffer(); buffer.AudioData = new System.IO.MemoryStream();  ----This is also new, due to SlimDX changes.
            waveFormat = format; 
            bytesPerSample = waveFormat.BitsPerSample / 8;
       }
}

           
Again the new code works just fine, so long as there is only one instance being played. (other instances can exist, just not be actively played from without having sound issues). And the old code allowed for what seemed like an unlimited number of simultaneous sounds to be played.
 
I suppose I could avoid this problem altogether by mixing the sounds together in software before feeding them to SlimDX, but I really rather not go down that road.


Edited by Grain, 13 July 2013 - 12:06 AM.


Sponsor:

#2 Grain   Members   -  Reputation: 461

Like
0Likes
Like

Posted 22 July 2013 - 12:42 PM

Bump. I made a new class derived from Speaker called Speaker_XNA that as you can guess uses XNA to enable sound functionality and objects of this class works perfectly in place of a Speaker_XAudio2 object.  
 
But I'd still like to get my audio working with slimDX if at all possible. 
 
Here's the code for the XNA speaker.
 

    public class Speaker_XNA : Speaker
    {
        DynamicSoundEffectInstance DynSoundInst;
        short bitspersample;

        public Speaker_XNA(short BitsPerSample, short Channels, int SamplesPerSecond)
        {
            FrameworkDispatcher.Update();
            DynSoundInst = new DynamicSoundEffectInstance(SamplesPerSecond, (AudioChannels)Channels);
            bitspersample = BitsPerSample;
        }
}
override public bool BufferSoundData(double[] data) 
        {

            //FrameworkDispatcher.Update();
            if (DynSoundInst.PendingBufferCount > 1)
            {
                return false;
            }
            else
            {
                byte[] SoundBytes;
                if (bitspersample == 8)
                {
                    SoundBytes = new byte[data.Length];
                    for (int i = 0; i < SoundBytes.Length; i++)
                    {
                        SoundBytes[i] = (byte)(data[i] * 128 + 128);
                    }
                }
                if (bitspersample == 16)
                {
                    SoundBytes = new byte[data.Length * 2];
                    for (int i = 0, j = 0; i < SoundBytes.Length; i += 2, j++)
                    {
                        System.Int16 word = (System.Int16)(data[j] * (System.Int16.MaxValue));
                        SoundBytes[i] = (byte)(word % 256);
                        SoundBytes[i + 1] = (byte)(word / 256);
                    }
                }
                if (bitspersample == 32)
                {
                    SoundBytes = new byte[data.Length * 2];
                    for (int i = 0, j = 0; i < SoundBytes.Length; i += 2, j++)
                    {
                        System.Int16 word = (System.Int16)(data[j] * (System.Int16.MaxValue));
                        SoundBytes[i] = (byte)(word % 256);
                        SoundBytes[i + 1] = (byte)(word / 256);
                    }
                    //for (int i = 0, j = 0; i < SoundBytes.Length; i += 4, j++)
                    //{
                    //    float flt = (float)data[j];
                    //    byte[] tmp = System.BitConverter.GetBytes(flt);
                    //    SoundBytes[i] = tmp[0];
                    //    SoundBytes[i + 1] = tmp[1];
                    //    SoundBytes[i + 2] = tmp[2];
                    //    SoundBytes[i + 3] = tmp[3];
                    //}
                }
                else SoundBytes = new byte[1];
                DynSoundInst.SubmitBuffer(SoundBytes);
                return true;
            }
        }
    }

 It's pretty much the same except I had to convert down in the case of 32 bit data because XNA only handles 16 bit sound.


Edited by Grain, 22 July 2013 - 12:48 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS