• Create Account

#ActualGrain

Posted 13 July 2013 - 12:06 AM

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.

#6Grain

Posted 13 July 2013 - 12:05 AM

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.

#5Grain

Posted 12 July 2013 - 11:52 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[] bData = new byte[data.Length * bytesPerSample];
if (waveFormat.BitsPerSample == 8)
for (int i = 0; i < bData.Length; i++)
{
bData[i] = (byte)(data[i] * 128 + 128);
}
if (waveFormat.BitsPerSample == 16)
for (int i = 0, j = 0; i < bData.Length; i += 2, j++)
{
System.Int16 word = (System.Int16)(data[j] * (System.Int16.MaxValue));
bData[i] = (byte)(word % 256);
bData[i + 1] = (byte)(word / 256);
}
if (waveFormat.BitsPerSample == 32)
for (int i = 0, j = 0; i < bData.Length; i += 4, j++)
{
float flt = (float)data[j];
byte[] tmp = System.BitConverter.GetBytes(flt);
bData[i] = tmp[0];
bData[i + 1] = tmp[1];
bData[i + 2] = tmp[2];
bData[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.

#4Grain

Posted 12 July 2013 - 11:51 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[] bData = new byte[data.Length * bytesPerSample];
if (waveFormat.BitsPerSample == 8)
for (int i = 0; i < bData.Length; i++)
{
bData[i] = (byte)(data[i] * 128 + 128);
}
if (waveFormat.BitsPerSample == 16)
for (int i = 0, j = 0; i < bData.Length; i += 2, j++)
{
System.Int16 word = (System.Int16)(data[j] * (System.Int16.MaxValue));
bData[i] = (byte)(word % 256);
bData[i + 1] = (byte)(word / 256);
}
if (waveFormat.BitsPerSample == 32)
for (int i = 0, j = 0; i < bData.Length; i += 4, j++)
{
float flt = (float)data[j];
byte[] tmp = System.BitConverter.GetBytes(flt);
bData[i] = tmp[0];
bData[i + 1] = tmp[1];
bData[i + 2] = tmp[2];
bData[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.

#3Grain

Posted 12 July 2013 - 11:50 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[] bData = new byte[data.Length * bytesPerSample];
if (waveFormat.BitsPerSample == 8)
for (int i = 0; i < bData.Length; i++)
{
bData[i] = (byte)(data[i] * 128 + 128);
}
if (waveFormat.BitsPerSample == 16)
for (int i = 0, j = 0; i < bData.Length; i += 2, j++)
{
System.Int16 word = (System.Int16)(data[j] * (System.Int16.MaxValue));
bData[i] = (byte)(word % 256);
bData[i + 1] = (byte)(word / 256);
}
if (waveFormat.BitsPerSample == 32)
for (int i = 0, j = 0; i < bData.Length; i += 4, j++)
{
float flt = (float)data[j];
byte[] tmp = System.BitConverter.GetBytes(flt);
bData[i] = tmp[0];
bData[i + 1] = tmp[1];
bData[i + 2] = tmp[2];
bData[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). 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.

#2Grain

Posted 12 July 2013 - 11:49 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[] bData = new byte[data.Length * bytesPerSample];
if (waveFormat.BitsPerSample == 8)
for (int i = 0; i < bData.Length; i++)
{
bData[i] = (byte)(data[i] * 128 + 128);
}
if (waveFormat.BitsPerSample == 16)
for (int i = 0, j = 0; i < bData.Length; i += 2, j++)
{
System.Int16 word = (System.Int16)(data[j] * (System.Int16.MaxValue));
bData[i] = (byte)(word % 256);
bData[i + 1] = (byte)(word / 256);
}
if (waveFormat.BitsPerSample == 32)
for (int i = 0, j = 0; i < bData.Length; i += 4, j++)
{
float flt = (float)data[j];
byte[] tmp = System.BitConverter.GetBytes(flt);
bData[i] = tmp[0];
bData[i + 1] = tmp[1];
bData[i + 2] = tmp[2];
bData[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). 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.

PARTNERS