Advertisement

Need some help with XAudio2

Started by December 25, 2012 09:27 PM
5 comments, last by adamw86 11 years, 10 months ago

Hi all, I've been trying to get some streaming code working with XAudio2. Unfortunately at best all I've gotten are a handful of clicks, and so I was wondering if someone could provide a little help.

I've implemented the code according to a few tutorials I've seen, and it's fairly simple. I just have one source voice and a master voice, and I'm submitting buffers to the source voice periodically.

Here's the setup code:


//Audio init section

	if(FAILED(CoInitializeEx(NULL, COINIT_MULTITHREADED)))
	{
		return false;
	}
	UINT32 flags = XAUDIO2_DEBUG_ENGINE;

	if(FAILED(XAudio2Create(&g_xAudioEngine)))
	{
		MessageBox(NULL, L"Failed on XAudio2Create", L"Sadface", MB_OK);
		CoUninitialize();
		return false;
	}

	if(FAILED(g_xAudioEngine->CreateMasteringVoice(&g_masterVoice, XAUDIO2_DEFAULT_CHANNELS,(UINT32) SAMPLERATE, 0,0,NULL)))
	{
		MessageBox(NULL, L"Failed to create mastering voice!", L"Sadface", MB_OK);
		CoUninitialize();
		return false;
	}
	WAVEFORMATEX wfx = {0};

	wfx.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
	wfx.nChannels = 1;
	wfx.nSamplesPerSec = 44100;
	wfx.nAvgBytesPerSec = 44100 * sizeof(float);
	wfx.nBlockAlign = sizeof(float);
	wfx.wBitsPerSample = sizeof(float) * 8;
	wfx.cbSize = 0;

	std::stringstream debugstream;

	debugstream << "nBlockAlign: " << wfx.nBlockAlign << " bitspersample: " << wfx.wBitsPerSample << std::endl;
	OutputDebugStringA(debugstream.str().c_str());

	if(FAILED(g_xAudioEngine->CreateSourceVoice(&g_sourceVoice, (WAVEFORMATEX*)&wfx)))
	{
		MessageBox(NULL, L"Failed to create source voice!", L"sadface", MB_OK);
	}

I've also tried this with signed 16-bit PCM instead of IEEE float to no avail.

Here's where I'm feeding the source voice:


void streamNextChunk(const boost::system::error_code& error, boost::asio::deadline_timer & timer)
{
	static bool comInitThisThread = false;
	if(!comInitThisThread)
	{
		CoInitializeEx(NULL, COINIT_MULTITHREADED);
		comInitThisThread = true;
	}
	timer.expires_from_now(boost::posix_time::milliseconds(1000));
	timer.async_wait(boost::bind(streamNextChunk, _1, boost::ref(timer)));

	static __int64 sampleCount = 0;
	float* buff = new float[44100];
	
	for(size_t i = 0 ;i < 44100; i++)
	{
		float t = (float)sampleCount++;
		t /= 44100.0;
		buff[i] =  cos(2 * 3.14159 * t * 260);
		
	}
	XAUDIO2_BUFFER xAudioBuff = {0};
	xAudioBuff.AudioBytes = (44100) * sizeof(float);
	xAudioBuff.pAudioData = (BYTE*) buff;
	

	if(FAILED(g_sourceVoice->SubmitSourceBuffer(&xAudioBuff)))
	{
		OutputDebugStringA("Failed on submit source buffer!!! D:\n");
	}
	g_sourceVoice->Start(0, XAUDIO2_COMMIT_NOW);
	XAUDIO2_VOICE_STATE state;

	g_sourceVoice->GetState(&state);
	float volume; 
    g_sourceVoice->GetVolume(&volume);
	std::stringstream debugstream;
	float gvolume;
	g_masterVoice->GetVolume(&gvolume);
	debugstream << "PTR: " << buff << " Q: " << state.BuffersQueued << " S: " << state.SamplesPlayed << " SC: " << sampleCount << " V: " << volume << " GV: " << gvolume << std::endl;

	OutputDebugStringA(debugstream.str().c_str());
	
}

This function is on a boost asio timer that ensures it's called once per second. The timer is handled in a separate thread from where the XAudio2 library is initialized (the ASIO run thread). The function also generates 1 second of data each call. As you can see I'm trying to generate a simple 2600Hz test tone, but at best all I have been able to get are some short clicks. (Yes I'm aware this leaks memory, but at the moment my main concern is getting any sound output at all). The debug printouts show that the source voice seems to be queuing the buffers and the samplesplayed count is increasing as expected.

This code seems to be simple enough, and mostly matches the code examples I've seen for streaming wav files off a disk (with the obvious exception that I'm creating the data on the fly).

Can anyone tell me what I'm missing?

It was my understanding you had to use WAVEFORMATEXTENSIBLE as it includes channels->speaker mappings, as well as a subformat (don't ask why this latter value is needed). Have a try.

Note you're not supposed to fetch buffers routinely. You're supposed to fetch them when they run out, after receiving a buffer exhausted signal.

Previously "Krohm"

Advertisement

I gave your suggestion a try, and it still doesn't work unfortunately. I also tried using __int16 and WAVE_FORMAT_PCM as my format/datatype and still nothing. I'm not getting any debug warnings saying the buffers are starved nor any other complaints from XAUDIO2... I'm also positive the streamNextChunk function is being called since it has debug printouts that are indicating XAUDIO2 is queuing buffers and increasing its sample count.

Here's my latest setup code attempt:



	//Audio init section

	if(FAILED(CoInitializeEx(NULL, COINIT_MULTITHREADED)))
	{
		return false;
	}
	UINT32 flags = XAUDIO2_DEBUG_ENGINE;

	if(FAILED(XAudio2Create(&g_xAudioEngine,flags)))
	{
		MessageBox(NULL, L"Failed on XAudio2Create", L"Sadface", MB_OK);
		CoUninitialize();
		return false;
	}
	
	if(FAILED(g_xAudioEngine->CreateMasteringVoice(&g_masterVoice, XAUDIO2_DEFAULT_CHANNELS,(UINT32) SAMPLERATE, 0,0,NULL)))
	{
		MessageBox(NULL, L"Failed to create mastering voice!", L"Sadface", MB_OK);
		CoUninitialize();
		return false;
	}
	XAUDIO2_BUFFER buff = {0};
	WAVEFORMATEX wfx = {0};

	wfx.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
	wfx.nChannels = 1;
	wfx.nSamplesPerSec = 44100;
	wfx.nAvgBytesPerSec = 44100 * sizeof(float);
	wfx.nBlockAlign = sizeof(float);
	wfx.wBitsPerSample = sizeof(float) * 8;
	wfx.cbSize = 22;

	WAVEFORMATEXTENSIBLE wfxe = {0};
	wfxe.Format = wfx;
	wfxe.SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
	wfxe.Samples.wValidBitsPerSample = 32;
	wfxe.dwChannelMask = SPEAKER_FRONT_CENTER;
	

	std::stringstream debugstream;

	debugstream << "nBlockAlign: " << wfx.nBlockAlign << " bitspersample: " << wfx.wBitsPerSample << std::endl;
	OutputDebugStringA(debugstream.str().c_str());

	
	if(FAILED(g_xAudioEngine->CreateSourceVoice(&g_sourceVoice, (WAVEFORMATEX*)&wfxe)))
	{
		MessageBox(NULL, L"Failed to create source voice!", L"sadface", MB_OK);
	}
	

Any other suggestions to try?

As an aside, if I purposely change the timing of the callback to say, 1.2 seconds, but still only generate 1 second of data each time around, I'll get audible clicks on a 1 second interval, but no debug warning messages saying the voice is starved for data nor any warnings about audio glitches. This seems a little suspicious to me.

Clicks? Maybe you put an incorrect data into the buffer and play using the voice interface,

Do you parse the wave file for extracting the buffer?

PCM is supported by all versions of XAudio2.

This is a low level API and I thing you IEEE would not work.

(but I do not know this format)

P. S.

Which XAudio2 version is your program linked against?

For PCM, WAVEFORMATEX would be enough.

Clicks? Maybe you put an incorrect data into the buffer and play using the voice interface,

Do you parse the wave file for extracting the buffer?

PCM is supported by all versions of XAudio2.

This is a low level API and I thing you IEEE would not work.

(but I do not know this format)

P. S.

Which XAudio2 version is your program linked against?

For PCM, WAVEFORMATEX would be enough.

I'm not using a wav file; I'm generating the data for the buffers on the fly (as shown in the streamNextChunk function). According to the header, I'm using XAudio2 version 2.7 (from the June 2010 SDK).

The clicks I'm hearing are presumably a buffer underflow since I'm purposely starving the voice, but I would expect this to trigger a "voice starved for data" warning or a warning about an audio glitch; it does not.

Advertisement

magicstix,
The reason you are getting a clicking is because you are not correctly converting your float samples to bytes in your streamNextChunk function. X86-based OS like Windows use "little-endian" bit ordering for byte arrays. Your 32-bit(4-byte) samples are arranged MSB-->LSB, where the order goes highest byte to lowest byte called "big endian"(like a written number). You will need to correctly convert each sample into 4-bytes to populate the sound buffer which is "little-endian": LSB-->MSB. The clicking is a result of each sample having its 4-bytes in the reverse order(DCBA), and the audio engine expecting the correct order: ABCD. Here is some code that should fix for you:



// Write each 32-bit sample as 4 bytes(order reversed) into your XAUDIO2_BUFFER (buf)

for(int i=0; i < buf.AudioBytes/4; i++) {

// Convert each sample to 4 bytes
byte byte4 = (byte) (samples >> 24);
byte byte3 = (byte) (samples >> 16);
byte byte2 = (byte) (samples >> 8);
byte byte1 = (byte) (samples >> 0);

// Insert each each sample into buffer in reverse byte-order
buf.audioData[4*i] = byte4;
buf.audioData[4*i+1] = byte3;
buf.audioData[4*i+2] = byte2;
buf.audioData[4*i+3] = byte1;
}

Hope this helps!
Adam

This topic is closed to new replies.

Advertisement