Книга: DirectX 8 Programming Tutorial

Setting up DirectX Audio

Setting up DirectX Audio

To setup DirectX Audio we need to create two objects: the performance object and the loader object. The performance object is the top-level object in DirectX Audio, it handles the flow of data from the source to the synthesizer. The loader object loads the files (wav and midi) into sound segments that can be played later. We only need one of each of these objects for the whole application, so we will create them as member variables of our CGame class. Their definitions are shown below:

IDirectMusicPerformance8* m_pDirectAudioPerformance;
IDirectMusicLoader8* m_pDirectAudioLoader;

Before we can create our objects, we need to initialise the COM library. We need to do this because DirectX Audio is pure COM. Don't worry too much about what this means, all you need to do is call the CoInitialize function (shown below) before you can create the DirectX Audio objects. To keep it simple, this is done in the CGame constructor.

CoInitialize(NULL);

Now that we have initialised COM, we need to create and initialise our two DirectX Audio objects. To do this, we have a new method of CGame called InitialiseDirectAudio which is shown below. This method is called from our Initialise method, take a look at the code and I'll explain in a moment.

bool CGame::InitialiseDirectAudio(HWND hWnd) {
 LogInfo("<br>Initialise DirectAudio:");
 //Create the DirectAudio performance object
 if (CoCreateInstance(CLSID_DirectMusicPerformance, NULL, CLSCTX_INPROC, IID_IDirectMusicPerformance8, (void**) &m_pDirectAudioPerformance) != S_OK) {
  LogError("<li>Failed to create the DirectAudio perfomance object.");
  return false;
 } else {
  LogInfo("<li>DirectAudio perfomance object created OK.");
 }
 //Create the DirectAudio loader object
 if (CoCreateInstance(CLSID_DirectMusicLoader, NULL, CLSCTX_INPROC, IID_IDirectMusicLoader8, (void**) &m_pDirectAudioLoader) != S_OK) {
  LogError("<li>Failed to create the DirectAudio loader object.");
  return false;
 } else {
  LogInfo("<li>DirectAudio loader object created OK.");
 }
 //Initialise the performance object
 if (FAILED(m_pDirectAudioPerformance->InitAudio(NULL, NULL, hWnd, DMUS_APATH_SHARED_STEREOPLUSREVERB, 64, DMUS_AUDIOF_ALL, NULL))) {
  LogError("<li>Failed to initialise the DirectAudio perfomance object.");
  return false;
 } else {
  LogInfo("<li>Initialised the DirectAudio perfomance object OK.");
 }
 //Get the our applications "sounds" directory.
 CHAR strSoundPath[MAX_PATH];
 GetCurrentDirectory(MAX_PATH, strSoundPath);
 strcat(strSoundPath, "Sounds");
 //Convert the path to unicode.
 WCHAR wstrSoundPath[MAX_PATH];
 MultiByteToWideChar(CP_ACP, 0, strSoundPath, –1, wstrSoundPath, MAX_PATH);
 //Set the search directory.
 if (FAILED(m_pDirectAudioLoader->SetSearchDirectory(GUID_DirectMusicAllTypes, wstrSoundPath, FALSE))) {
  LogError("<li>Failed to set the search directory '%s'.", strSoundPath);
  return false;
 } else {
  LogInfo("<li>Search directory '%s' set OK.", strSoundPath);
 }
 return true;
}

So, what does this code do? Well, we use CoCreateInstance to create our performance and loader objects. Once we have done this, we need to initialise the performance object by calling the InitAudio method. The parameters used above for InitAudio are fairly typical, so you will probably just want to use the same. Take a look in the SDK for a full description of the InitAudio method and it's parameters. Finally, we set the search directory for our loader object. The search directory is the folder that the loader object will look in to find the files that you want to load. In the code above, we will set the search directory to the "Sounds" folder which is inside our project folder. Now we are ready to load and play sounds.

Оглавление книги


Генерация: 0.039. Запросов К БД/Cache: 0 / 0
поделиться
Вверх Вниз