Jump to content
  • Advertisement
Sign in to follow this  
btownsen4

XAudio2 Thread Jamming

This topic is 3977 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to integrate the Xaudio2 API with my Directx game, and used the XAudio2's limited documentation and sample code to get it going. The problem is that the documentation and sample code say to use a loop to check until the wave file has finished playing. This jams the game, so everything else has to wait for the wave file to finish playing. What is the standard for fixing this problem? I really dont want to mess with multi-threading right now. If anyone else has a better audio API, im open for suggestions.

Share this post


Link to post
Share on other sites
Advertisement
Well, if your game logic is going to run at 30-60fps, why not do the checks once per update, or even per frame?

Share this post


Link to post
Share on other sites
The problem is (to my limited knowledge), you call "Play" from an Object, but calling "Play" can only be done once, and there is no point you can tell "Play" to start from.

Share this post


Link to post
Share on other sites
I figured out how to accomplish this, I had to change the way the SDK Docs said to use XAudio2 by seperating the destroy calls, now I have another problem. I have a mini-framework setup so I call init_xaudio(), then call load_sound(filename), and then play_sound(CWaveFile *). CWaveFile is a class, returned by load_sound(filename). The problem is that the CWaveFile Pointer once used once by play_sound seems to re-construct the class with no data. I have the code below.

#include "sound.h"

//Global Variables
IXAudio2 * pXAudio2 = 0;
IXAudio2SourceVoice * pSourceVoice = 0;
BYTE * pbWaveData = 0;

int init_xaudio2()
{
//Threading Support, Required For XAudio2 To Work
CoInitializeEx(0, COINIT_MULTITHREADED);

//Create XAudio2
if (FAILED(XAudio2Create(&pXAudio2, 0, 0, XAUDIO2_DEFAULT_PROCESSOR)))
{
CoUninitialize();
return 0;
}

//Pointer to MasterVoice
IXAudio2MasteringVoice * pMasterVoice = 0;

//Create MasterVoice
if (FAILED(pXAudio2->CreateMasteringVoice( &pMasterVoice, XAUDIO2_DEFAULT_CHANNELS, XAUDIO2_DEFAULT_SAMPLERATE, 0, 0, 0 )))
{
if (pXAudio2)
pXAudio2->Release();

CoUninitialize();
return 0;
}

return 1;
}

CWaveFile * load_sound(LPWSTR filename)
{
//Read in the wave file
CWaveFile * wave_file = new CWaveFile;
if (FAILED(wave_file->Open(filename, 0, WAVEFILE_READ)))
return 0;

return wave_file;
}

int play_sound(CWaveFile * wave_file)
{
//Clear Current Data
if (pSourceVoice)
{
pSourceVoice->DestroyVoice();
pSourceVoice = 0;
}

if (pbWaveData)
{
delete [] pbWaveData;
pbWaveData = 0;
}

//Get format of wave file
WAVEFORMATEX * pwfx = wave_file->GetFormat();

//Calculate how many bytes and samples are in the wave
DWORD cbWaveSize = wave_file->GetSize();

//Read the sample data into memory
pbWaveData = new BYTE[cbWaveSize];

if(FAILED(wave_file->Read(pbWaveData, cbWaveSize, &cbWaveSize)))
{
if (pbWaveData)
{
delete [] pbWaveData;
pbWaveData = 0;
}
return 0;
}

if(FAILED(pXAudio2->CreateSourceVoice(&pSourceVoice, pwfx, 0, XAUDIO2_DEFAULT_FREQ_RATIO, 0, 0, 0)))
{
if (pSourceVoice)
{
pSourceVoice->DestroyVoice();
pSourceVoice = 0;
}

if (pbWaveData)
{
delete [] pbWaveData;
pbWaveData = 0;
}
return 0;
}

//Submit the wave sample data using an XAUDIO2_BUFFER structure
XAUDIO2_BUFFER buffer = {0};
buffer.pAudioData = pbWaveData;
buffer.Flags = XAUDIO2_END_OF_STREAM;
buffer.AudioBytes = cbWaveSize;
//Error
if(FAILED(pSourceVoice->SubmitSourceBuffer(&buffer)))
{
if (pSourceVoice)
{
pSourceVoice->DestroyVoice();
pSourceVoice = 0;
}

if (pbWaveData)
{
delete [] pbWaveData;
pbWaveData = 0;
}
return 0;
}

if ((pSourceVoice->Start(0, XAUDIO2_COMMIT_NOW)) != S_OK)
{
if (pSourceVoice)
{
pSourceVoice->DestroyVoice();
pSourceVoice = 0;
}

if (pbWaveData)
{
delete [] pbWaveData;
pbWaveData = 0;
}
return 0;
}

return 1;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!