使用DirectMusic

// include dsound, dmusic
#include <dsound.h> 
#include <dmksctrl.h>
#include <dmusici.h>
#include <dmusicc.h>
#include <dmusicf.h>
// direct music globalsI
DirectMusicPerformance    *dm_perf = NULL;
IDirectMusicLoader         *dm_loader = NULL;
IDirectMusicSegment        *dm_segment = NULL;
IDirectMusicSegmentState   *dm_segstate = NULL;


int Game_Init(void *parms = NULL, int num_parms = 0)
{
// this is called once after the initial window is created and
// before the main event loop is entered, do all your initialization
// here

// set up directmusic
if (FAILED(CoInitialize(NULL)))
   {    
   // Terminate the application.
   return(0);
   }   // end if


// create the performance
dm_perf = CreatePerformance();

if (dm_perf == NULL)
   {
   return(0);// Failure -- performance not created
   } // end if


// initialize the performance
if (FAILED(dm_perf->Init(NULL, NULL, main_window_handle)))
   {
   return(0);// Failure -- performance not initialized
   } // end if 

// add the port to the performance
if (FAILED(dm_perf->AddPort(NULL)))
   {    
   return(0);// Failure -- port not initialized
   } // end if

// create the loader to load object(s) such as midi file
dm_loader = CreateLoader();

if (dm_loader == NULL)
   {
    return(0);// Failure -- loader not created
   } // end if

// release the old segment
if (dm_segment)
   {    
   dm_segment->Release();    
   dm_segment = NULL;
   } // end if

// load the segment
if (dm_loader)
   {
   dm_segment = LoadMIDISegment(dm_loader,L"BATTLE.MID");
   } // end if

// start the song
if (dm_segment)
   {
   dm_perf->PlaySegment(dm_segment, 0, 0, &dm_segstate);
   }  // end if

// return success or failure or your own return code here
return(1);

} // end Game_Init

/


IDirectMusicPerformance* CreatePerformance(void)
{
// this function creates the performance

IDirectMusicPerformance* pPerf;     

if (FAILED(CoCreateInstance(CLSID_DirectMusicPerformance,
                            NULL,
                            CLSCTX_INPROC,
                            IID_IDirectMusicPerformance,
                            (void**)&pPerf)))    
   {
   // return null        
   pPerf = NULL;    
   } // end if

   return pPerf;

} // end CreatePerformance



IDirectMusicLoader* CreateLoader(void)
{
// this function creates the loader

    IDirectMusicLoader* pLoader;
 
    if (FAILED(CoCreateInstance(
            CLSID_DirectMusicLoader,
            NULL,
            CLSCTX_INPROC, 
            IID_IDirectMusicLoader,
            (void**)&pLoader)))
    {
    pLoader = NULL;
    } // end if

    return pLoader;

} // end CreateLoader

///

IDirectMusicSegment* LoadMIDISegment(IDirectMusicLoader* pLoader, 
                                     WCHAR *wszMidiFileName )
{
// this function loads a midi segment off disk

DMUS_OBJECTDESC ObjDesc; 
HRESULT hr;
IDirectMusicSegment* pSegment = NULL;
 
// get current working directory
char szDir[_MAX_PATH];
WCHAR wszDir[_MAX_PATH];
 
if(_getcwd( szDir, _MAX_PATH ) == NULL)
  {
  return NULL;
  } // end if

// convert to wide characters
MULTI_TO_WIDE(wszDir, szDir);

// set the search directory
hr = pLoader->SetSearchDirectory(GUID_DirectMusicAllTypes,wszDir, FALSE);

if (FAILED(hr)) 
   {
   return NULL;
   } // end if
 
// setup object description
ZeroMemory(&ObjDesc, sizeof(DMUS_OBJECTDESC));
ObjDesc.dwSize = sizeof(DMUS_OBJECTDESC);
ObjDesc.guidClass = CLSID_DirectMusicSegment;
wcscpy(ObjDesc.wszFileName, wszMidiFileName );

ObjDesc.dwValidData = DMUS_OBJ_CLASS | DMUS_OBJ_FILENAME;
 
// load the object and query it for the IDirectMusicSegment interface
// This is done in a single call to IDirectMusicLoader::GetObject
// note that loading the object also initializes the tracks and does 
// everything else necessary to get the MIDI data ready for playback.

hr = pLoader->GetObject(&ObjDesc,IID_IDirectMusicSegment, (void**) &pSegment);

if (FAILED(hr))
   return(0);
 
// ensure that the segment plays as a standard MIDI file
// you now need to set a parameter on the band track
// Use the IDirectMusicSegment::SetParam method and let 
// DirectMusic find the trackby passing -1 (or 0xFFFFFFFF) in the dwGroupBits method parameter.

hr = pSegment->SetParam(GUID_StandardMIDIFile,-1, 0, 0, (void*)dm_perf);

if (FAILED(hr))
   return(0);
  
// This step is necessary because DirectMusic handles program changes and 
// bank selects differently for standard MIDI files than it does for MIDI 
// content authored specifically for DirectMusic. 
// The GUID_StandardMIDIFile parameter must be set before the instruments are downloaded. 

// The next step is to download the instruments. 
// This is necessary even for playing a simple MIDI file 
// because the default software synthesizer needs the DLS data 
// for the General MIDI instrument set
// If you skip this step, the MIDI file will play silently.
// Again, you call SetParam on the segment, this time specifying the GUID_Download parameter:

hr = pSegment->SetParam(GUID_Download, -1, 0, 0, (void*)dm_perf);

if (FAILED(hr))
   return(0);

// return the pointer
return pSegment;
 
} // end LoadSegment

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值