• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
magicstix

Need some help with XAudio2

6 posts in this topic

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?

Edited by magicstix
0

Share this post


Link to post
Share on other sites

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.

1

Share this post


Link to post
Share on other sites

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?

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites
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. 

0

Share this post


Link to post
Share on other sites

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[i] >> 24);
byte byte3 = (byte) (samples[i] >> 16);
byte byte2 = (byte) (samples[i] >> 8);
byte byte1 = (byte) (samples[i] >> 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

Edited by adamw86
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0