Tag Archives: directx

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.

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.

A much better day…

Today’s stuff.

  • Imported the sound example code from P2DG chapter 7 into MSVC and successfully compiled.
  • And runs! With sound!
  •  withSound
  • Next we try to put the audio class into a console app (main as opposed to winmain) and see if that can be made to work.
  • That went pretty smoothly. I had to add the DirectX “include” directories, add stdafx.h to the audio.cpp file and cast some char* to LPWSTR. The next step is to fill out the main() so that the audio is initialized and then run a loop to send different audio commands. A good task for Monday.