Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualGrain

Posted 22 July 2013 - 12:48 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.


#1Grain

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. 


PARTNERS