Jump to content

  • Log In with Google      Sign In   
  • Create Account

14 years ago on June 15th Gamedev.net was first launched! We want to thank all of you for being part of our community and hope the best years are ahead of us. Happy birthday Gamedev.net!

#Actualtrinith_006

Posted 13 January 2012 - 10:15 AM

Hi Mike,

I appreciate the response, that's actually not a bad idea and pretty easy to implement. That said, it doesn't seem to be the issue. I changed over the code to use a single device and the same issue is present.

I should mention that I'm not locked to SlimDX... if you (or anybody else) has thoughts on alternatives I'm all ears (eyes?). My engine is structured in such a way that I can replace the component that does the actual sound playing fairly easily so I should be able to use any old sound provider. My only requirements are that it can play in a thread and that I can play/stop it (pause is a nice to have but not required). Currently SlimDX using XAudio2 grants me this functionality. I did try SlimDX with DirectSound, but was getting an odd artifact at the end of playback... just a little click sound, so I went with XAudio2.

Here's the updated class for you or any others looking at this thread.

public class SoundPlayer
{
	private SourceVoice m_sourceVoice = null;

	public SoundPlayer()
	{
	}

	public void Load(XAudio2 device, string fileName)
	{
		WaveStream waveStream = new WaveStream(fileName);

		AudioBuffer buffer = new AudioBuffer();
		buffer.AudioData = waveStream;
		buffer.AudioBytes = (int)waveStream.Length;
		buffer.Flags = BufferFlags.EndOfStream;

		m_sourceVoice = new SourceVoice(device, waveStream.Format);
		m_sourceVoice.SubmitSourceBuffer(buffer);
	}

	public void Load2(XAudio2 device, string fileName)
	{
		byte[] fileData = File.ReadAllBytes(fileName);
		MemoryStream ms = new MemoryStream(fileData);

		WaveStream waveStream = new WaveStream(ms);

		AudioBuffer buffer = new AudioBuffer();
		buffer.AudioData = waveStream;
		buffer.AudioBytes = (int)waveStream.Length;
		buffer.Flags = BufferFlags.EndOfStream;

		m_sourceVoice = new SourceVoice(device, waveStream.Format);
		m_sourceVoice.SubmitSourceBuffer(buffer);
	}

	public void Play()
	{
		Thread t = new Thread(PlayMethod);
		t.Start();
	}

	private void PlayMethod()
	{
		m_sourceVoice.Start();
		while (m_sourceVoice.State.BuffersQueued > 0)
		{
			Thread.Sleep(10);
		}

		m_sourceVoice.Dispose();
	}
}

Then using it...

public class TestHarness
{
	private XAudio2 m_device = new XAudio2();

	public TestHarness()
	{
		MasteringVoice masteringVoice = new MasteringVoice(m_device);
	}

	public void RunTest()
	{
		SoundPlayer p1 = new SoundPlayer();
		SoundPlayer p2 = new SoundPlayer();
		SoundPlayer p3 = new SoundPlayer();

		p1.Load2(m_device, @"..\..\klaxon2.wav");
		p2.Load2(m_device, @"..\..\klaxon2.wav");
		p3.Load2(m_device, @"..\..\warn2.wav");

		p1.Play();
		//p2.Play();
		//p3.Play();
	}
}

#1trinith_006

Posted 13 January 2012 - 09:42 AM

Hi Mike,

I appreciate the response, that's actually not a bad idea and pretty easy to implement. That said, it doesn't seem to be the issue. I changed over the code to use a single device and the same issue is present. Here's the updated class for you or any others looking at this thread.

public class SoundPlayer
{
    private SourceVoice m_sourceVoice = null;

    public SoundPlayer()
    {
    }

    public void Load(XAudio2 device, string fileName)
    {
        WaveStream waveStream = new WaveStream(fileName);

        AudioBuffer buffer = new AudioBuffer();
        buffer.AudioData = waveStream;
        buffer.AudioBytes = (int)waveStream.Length;
        buffer.Flags = BufferFlags.EndOfStream;

        m_sourceVoice = new SourceVoice(device, waveStream.Format);
        m_sourceVoice.SubmitSourceBuffer(buffer);
    }

    public void Load2(XAudio2 device, string fileName)
    {
        byte[] fileData = File.ReadAllBytes(fileName);
        MemoryStream ms = new MemoryStream(fileData);

        WaveStream waveStream = new WaveStream(ms);

        AudioBuffer buffer = new AudioBuffer();
        buffer.AudioData = waveStream;
        buffer.AudioBytes = (int)waveStream.Length;
        buffer.Flags = BufferFlags.EndOfStream;

        m_sourceVoice = new SourceVoice(device, waveStream.Format);
        m_sourceVoice.SubmitSourceBuffer(buffer);
    }

    public void Play()
    {
        Thread t = new Thread(PlayMethod);
        t.Start();
    }

    private void PlayMethod()
    {
        m_sourceVoice.Start();
        while (m_sourceVoice.State.BuffersQueued > 0)
        {
            Thread.Sleep(10);
        }

        m_sourceVoice.Dispose();
    }
}

Then using it...

public class TestHarness
{
    private XAudio2 m_device = new XAudio2();

    public TestHarness()
    {
        MasteringVoice masteringVoice = new MasteringVoice(m_device);
    }

    public void RunTest()
    {
        SoundPlayer p1 = new SoundPlayer();
        SoundPlayer p2 = new SoundPlayer();
        SoundPlayer p3 = new SoundPlayer();

        p1.Load2(m_device, @"..\..\klaxon2.wav");
        p2.Load2(m_device, @"..\..\klaxon2.wav");
        p3.Load2(m_device, @"..\..\warn2.wav");

        p1.Play();
        //p2.Play();
        //p3.Play();
    }
}

PARTNERS