Introduction
Using DirectMusic for Audio Playback.
A Note on DirectX :
Microsoft DirectX is an advanced suite of multimedia application programming
interfaces (APIs) built into Microsoft Windows; operating systems. DirectX
provides a standard development platform for Windows-based PCs by enabling
software developers to access specialized hardware features without having
to write hardware-specific code. This technology was first introduced in
1995 and is a recognized standard for multimedia application development on
the Windows platform.
A Note for programmers :
Q: Is knowledge of COM essential for using DirectMusic ?
A: No. The basics of intializing the COM library and getting an interface
would do.This tutorial also helps you in doing those 2 requisites. So,
you'd just have to copy and paste the intialization routines in your
application and it would do just fine.
Q: Does DirectMusic involve a steep learning curve ?
A: Absolutely not. Just follow the steps outlined below and you can blindly
use DirectMusic in your application.
Implementation
This tutorial describes how to use DirectMusic from a MFC-based application.
Step 1 : Downloading DirectX
You need to have DirectX installed on your machine and you can download
it The Microsoft DirectX Site. After installing it, you can proceed to build your
application.
Step 2 : Libraries and Headers
To include DirectMusic in your application, you need to include the header files
and libraries required for it. To do just that,add the following statements in
your header file.
Note : You can find the below mentioned files in your machine after you've installed
DirectX. Copy the required files into your application folder before proceeding.
// To use GUIDs successfully in our application
#define INITGUID
// Includes for DirectMusic,DirectSound and the WinMM library
#include <dmusici.h>
#include <mmsystem.h>
#include <dsound.h>
#include "dxerr9.h"
#include "dxutil.h"
#include "dsutil.h"
#include "dmutil.h"
#include "mmreg.h"
// Libraries for DirectMusic,DirectSound and the WinMM library
#pragma comment(lib,"winmm.lib")
#pragma comment(lib,"dsound.lib")
#pragma comment(lib,"dxguid.lib")
#pragma comment(lib,"DxErr9.lib")
Step 3 : Declaring the Variables
Declare the following variables in your header file.
CMusicManager* m_pMusicManager;
CMusicSegment* m_pMusicSegment;
HANDLE m_hDMusicMessageEvent;
Step 4 : Initialize DirectMusic
In your application's Initialization routine, add the following code.
This can be called from your Application's Constructor too.
// Initialize DirectMusic
m_hDMusicMessageEvent = CreateEvent( NULL, false, false, NULL );
if(m_hDMusicMessageEvent==NULL)
return;
m_pMusicManager = new CMusicManager();
if(m_pMusicManager == NULL)
return;
if(FAILED(m_pMusicManager->Initialize(NULL,128,DMUS_APATH_DYNAMIC_STEREO,0)))
return;
// Register segment notification
IDirectMusicPerformance* pPerf = m_pMusicManager->GetPerformance();
if(pPerf==NULL)
return;
GUID guid = GUID_NOTIFICATION_SEGMENT;
if(FAILED(pPerf->AddNotificationType(guid )))
return;
if(FAILED(pPerf->SetNotificationHandle(m_hDMusicMessageEvent, 0 )))
return;
Step 5 : Playing an Audio File
Here szFileName is the Name and path of the Audio file that you want to play
// Free any previous segment, and make a new one
if(m_pMusicSegment)
{
delete m_pMusicSegment;
m_pMusicSegment = NULL;
}
// Have the loader collect any garbage now that the old segment has been released
m_pMusicManager->CollectGarbage();
// Load the file into a DirectMusic segment
if( FAILED( m_pMusicManager->CreateSegmentFromFile( &m_pMusicSegment, szFileName,true, false ) ) )
return;
// To set the looping to NONE. If you want the segment to loop over and over, use
// m_pMusicSegment->SetRepeats(DMUS_SEG_REPEAT_INFINITE);
m_pMusicSegment->SetRepeats(0);
if(FAILED(m_pMusicSegment->Play(DMUS_SEGF_BEAT)))
return;
Step 6 : Stopping the Playback
m_pMusicManager->StopAll();
Step 7 : Setting the Volume level
Here lVolume is a long variable that can be any value between -4000 and 0
-4000 indicates lowest Volume and 0 indicates Maximum Volume.
IDirectMusicAudioPath *pDirectMusicAudioPath;
IDirectSoundBuffer *pDirectSoundBuffer;
DWORD dwBuffer = 0;
pDirectMusicAudioPath = m_pMusicManager->GetDefaultAudioPath();
if(pDirectMusicAudioPath==NULL)
return;
if (S_OK != pDirectMusicAudioPath->GetObjectInPath( DMUS_PCHANNEL_ALL, DMUS_PATH_BUFFER, dwBuffer,
GUID_NULL, 0, IID_IDirectSoundBuffer, (void**) &pDirectSoundBuffer))
return;
if(FAILED(pDirectSoundBuffer->SetVolume(lVolume)))
return;
Step 8 : Muting Audio
As mentioned in Step 7, you can set the Volume levels. Setting it to a
very low level, for example -10,000 would effectively mute the audio.
To Release the mute, you just have to set a volume with a parameter
between -4000 and 0.
IDirectMusicAudioPath *pDirectMusicAudioPath;
IDirectSoundBuffer *pDirectSoundBuffer;
DWORD dwBuffer = 0;
pDirectMusicAudioPath = m_pMusicManager->GetDefaultAudioPath();
if(pDirectMusicAudioPath==NULL)
return;
if (S_OK != pDirectMusicAudioPath->GetObjectInPath(DMUS_PCHANNEL_ALL, DMUS_PATH_BUFFER, dwBuffer,GUID_NULL, 0, IID_IDirectSoundBuffer, (void**) &pDirectSoundBuffer))
return;
if(FAILED(pDirectSoundBuffer->SetVolume(-10000)))
return;
Step 9 : Setting the Playback Rate
Sometimes, you might want to slow down the playback rate or increase it.
To do that, you need to call the following piece of code. Here,
dPlaybackRate is a double variable which indicates the speed of playback.
For example, 2.0 increases the speed by 100%, 0.5 effectively halves
the speed of play.
IDirectMusicAudioPath *pDirectMusicAudioPath;
IDirectSoundBuffer *pDirectSoundBuffer;
DWORD dwBuffer = 0;
pDirectMusicAudioPath = m_pMusicManager->GetDefaultAudioPath();
if(pDirectMusicAudioPath==NULL)
return;
if (S_OK != pDirectMusicAudioPath->GetObjectInPath( DMUS_PCHANNEL_ALL, DMUS_PATH_BUFFER, dwBuffer,
GUID_NULL, 0, IID_IDirectSoundBuffer, (void**) &pDirectSoundBuffer))
return;
if(FAILED(pDirectSoundBuffer->SetFrequency(uiKiloHertz)))
return;
pDirectSoundBuffer->Release();
Step 10 : Releasing the components used
Now, that we've finished using the DirectMusic Library, its time for us to
cleanup the code. Uninitialize ( or ) Release the components used and then
Uninitialize the COM Library too.
CloseHandle(m_hDMusicMessageEvent);
if( m_pMusicManager )
m_pMusicManager->StopAll();
if(m_pMusicSegment)
delete m_pMusicSegment;
if(m_pMusicManager)
delete m_pMusicManager;
Conclusion :
THATS ALL FOLKS. The end of a simple tutorial for using DirectMusic within your applications.
All Luck.
|