Getting some sound in your DX games
Here is some intro code for you. Just got my book on directx 9 audio programming (3D math book is now on the way) and it has taught me a lot about audio programming and much about music synthesis, tracks, segments, etc.,etc. Very good book.
Here is some code to get you up and running.
Code:
IDirectMusicPerformance8 *pPerformance=NULL;
IDirectMusicLoader8 *pLoader=NULL;
IDirectMusicSegment8 *pSegment=NULL;
//
//Init COM
//
CoInitialize(NULL);
//
//Create loader
//
CoCreateInstance(CLSID_DirectMusicLoader,
NULL,
CLSCTX_INPROC,
IID_IDirectMusicLoader8,
(void **)&pLoader);
//
//Create Performance
//
CoCreateInstance(CLSID_DirectMusicPerformance,
NULL,
CLSCTX_INPROC,
IID_IDirectMusicPerformance8,
(void **)&pPerformance);
//
//Init performance
//
pPerformance->InitAudio(NULL,
NULL,
NULL,
DMUS_APATH_DYNAMIC_STEREO,
2,
DMUS_AUDIOF_ALL,
NULL);
//
//Load WAVE from current directory
//assumes "anywavefile.wav" is file name
if (SUCEEDED(pLoader->LoadObjectFromFile(
CLSID_DirectMusicSegment,
IID_IDirectMusicSegment8,
L"anywavefile.wav",
(LPVOID *)&pSegment))) {}
//
//Place wave data in synth
//
pSegment->Download(pPerformance);
//
//Finally, play the thing
//
pPerformance->PlaySegmentEx(
pSegment,
NULL,
NULL,
0,
0,
NULL,
NULL,
NULL);
//Pause for sound to play
//Insert code here
//
//Unload data
//
pSegment->Unload(pPerformance);
//
//Release segment from memory
//
pSegment->Release();
//
//Close down performance and release
//
pPerformance->CloseDown();
pPerformance->Release();
//
//Release loader
//
pLoader->Release();
//
//Done with COM
//
CoUninitialize();
Requires DirectX 9.0 and MSVC version 6.0+