Tag Archives: xaudio2

Final cleanup on the proof-of concept

  • Ugh.
  • rain
  • Adding comments and velocity accessors to audio code
    • Comments are done
    • Checked in project to Subversion repo, then checked out and compiled. All seems to be working fine.
  • Cleaning up #includes
    • I had been getting the following annoying warning once I started including items like <unordered_map>: 1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cstring(21): warning C4995: ‘strcat’: name was marked as #pragma deprecated. In the cleanup, I finally got around to testing the need for includes and deleted <strsafe.h>, which seems to be the source of my grief. Yay!
  • Ready to make the audio code a library. Monday. Then time to design the experimental interface.
  • And now that I think I know what’s going on, a good article shows up…

In the end, this is how you do it:

// create the instance
BasicAudio *ba = new BasicAudio(); 

// initialize
ba->init(); 

// create a sound from a file. In this case a WAV. There can be many of these.
ba->createSound(L"music", L"Wavs\\MusicMono.wav", 0); 

// get the instance to the sound and start(), stop(), run() etc.
ba->getSoundByName(L"music")->start(); 
loop{
	// change some audio condition

	// play the voice on a specified channel or play in 3D
	ba->playOnChannelVoice(L"music", channelIndex); 
 	ba->play3DVoice(L"music");

	// optional - run periodic checks.
	ba->run() 
}
ba->destroy();

Approaching understanding

Today was a good day of slow progress! Here’s the play by play.

  • Building a copy of XAudio2BasicSound, cleverly calling it ConsoleSound2.
  • Since it depends on SDKwavefile, I had to pull SAFE_DELETE, SAFE_DELETE_ARRAY, and SAFE_RELEASE from dxut.h. It also requires dxerr.h. but that’s in the regular directx include directory.
  • Extremely empty code right now, but it compiles and links. On a side note, the compiler should accept the environment variable $(DXSDK_DIR), but it doesn’t. And adding macros is an enormous pain, as near as I can tell.
  • The code is ported over and works. It’s all stuck in a main() right now. I need to clean up and build an Audio class. Actually, looking at the way the code is set up, a WavSampleSound class might be better. It probably could extend from a SampleSound class, but I’ll break that out later…
  • Class is mostly done and running!
  •  ConsoleSound2
  • The only bug that I have is that I can run the sound only once. I still need to figure out how to reload the buffer.

3D sound compiled and running, if not understood all that well…

Today’s twists and turns:

  • More Audio3d.
  • Starting to add in the 3dAudio based on this and this and this.
  • Got an audio link error and chased that down, discovering a few things along the way.
  • There is directX help for C++ in (“C:\Program Files (x86)\Microsoft DirectX SDK (June 2010)\Documentation\DirectX9\directx_sdk.chm”). I don’t know it it’s any good, but I’m going to look though it next. Note that this is DX9, not 11. I’m *hoping* that this is all up to date…
  • Pointed Configuration Properties->Linker->General->Additional Library Directories at (C:\Program Files (x86)\Microsoft DirectX SDK (June 2010)\Lib\x86), and added the following files to Configuration Properties->Linker->Input->additional dependencies”
    • d3d11.lib
      d3dx11.lib
      winmm.lib
      xinput.lib
      X3DAudio.lib
    • This is a change from the libraries that the demo book points at, which were dxd9 libs. Recompiled and ran the code with no problems. I did attempt to clean up the xact3dInstance pointer in ~Audio3d(), but delete[] threw an exception
  • Reading through the DX documentation, which, though it is in the DX9 folder, contains information about DX11. Whatever.
  • The documentation is kind of what you’d expect, but it let me find the tutorials and demos, which are quite nice. On my machine, the demos that I’m interested in are located here: (C:\Program Files (x86)\Microsoft DirectX SDK (June 2010)\Samples\C++\XAudio2). It contains 4 projects:
    • XAudio2BasicSound
    • XAudioBasicStream
    • XAudio2CustomAPO
    • XAudio2Sound3D
  • Each project has source and MSVC2008 and MSVC2010 project files. I’ve just opened up the XAudio2Sound3D MSVC2010 project, and it compiled and ran just fine: XAudio2demo
  • This is almost exactly what I intend to build for the test environment, so that’s pretty cool. And using the buttons or the keyboard, I can drive the sound emitter around my head. Woohoo!
  • Next goal is to build up a copy of XAudio2BasicSound and then add the 3D components (without the graphics) from XAudio2Sound3D.

A working console app!

Good progress today

Looks like everything is working in the proof of concept! Here’s the code:

#include "stdafx.h"
#include <Windows.h>
#include <conio.h>
#include "audio.h"

int _tmain(int argc, _TCHAR* argv[])
{
	int keyIn;
	HRESULT hr;                 // standard return type
	LPTSTR buf;
	LPTSTR targetDir = "C:\\Programming 2D Games in DirectX 11\\Chapter 7 - Sound\\ConsoleSound\\ConsoleSound\\ConsoleSound";
	Audio *audio = new Audio();

	SetCurrentDirectory(targetDir);
	buf = (LPTSTR)calloc(256, sizeof(char));
	GetCurrentDirectory(256, buf);
	printf("Current dir: '%s'\n", buf);
	if (*WAVE_BANK != '' && *SOUND_BANK != '')  // if sound files defined
	{
		if( FAILED( hr = audio->initialize() ) )
		{
			if( hr == HRESULT_FROM_WIN32( ERROR_FILE_NOT_FOUND ) ){
				printf("Failed to initialize sound system because media file not found.\n");
				printf("hit return to exit\n");
				getchar();
				return -1;
			}
			else{
				printf("Failed to initialize sound system.\n");
				printf("hit return to exit\n");
				getchar();
				return -1;
			}
		}
	}

	printf("Type 'x' to quit, 1, 2, or 3 for sounds.\n");
	bool doit = true;
	while(doit){
		if(kbhit()){
			keyIn = getch();

			printf("key = %c\n", keyIn);
			switch(keyIn){
			case 'x' : doit = false; 
				break;
			case '1' : audio->playCue(BEEP1);                  // play sound
				break;
			case '2' : audio->playCue(BEEP2);                  // play sound
				break;
			case '3' : audio->playCue(BEEP3);                  // play sound
				break;
			}
		}
		audio->run();                       // perform periodic sound engine tasks
		Sleep(100);
	}
	SAFE_DELETE(audio);

	printf("hit return to exit");
	getchar();
	return 0;
	}

Tomorrow, we’ll try to add positional sound.