Skip to main content
GameDev.net gamedev.net
🔒 Locked

DirectMusic Help(Code Included)

Started by Plexmark Jun 7, 2004 at 2:46 PM 5 replies 1k views
Original Post
Plexmark
Plexmark
Below is the class I am trying to use to play music in my 2d game. It was taken from examples and pieced together into a seperate class as this is my first attempt at adding sound to a game. I included the header and .cpp file, I can get it to work when I just hardcode in what file to play, what I am looking to do is be able to send a variable to the function and play that one. I have tried to convert the string to a WCHAR pointer and send it and it still wont work. Maybe someone who is good with directmusic would see the problem.
#pragma once

#define INITGUID
#include <windows.h>
#include <string>
#include <dmusicc.h>
#include <dmusici.h>

using namespace std;

class DMusic
{
public:
	DMusic()
	{
	    IDirectMusicLoader8*      g_pLoader = NULL;
	    IDirectMusicPerformance8* g_pPerformance = NULL;
	    IDirectMusicSegment8*     g_pSegment = NULL;
	}
	int DMusic::DMusic_Repeat(WCHAR* name);
	int DMusic::Close();
private:
	IDirectMusicLoader8*      g_pLoader;
	IDirectMusicPerformance8* g_pPerformance;
	IDirectMusicSegment8*     g_pSegment;
};
 
And here is the actual code, like I said, I can get it to work if I hard code in the file I want to play perfect, but cannot get it to accept a variable. Also if you know a better way please point it out, as I said, this is my first time using directmusic and am taking this from a straight program and trying to develop my own class.
   
#include "DMusic.h"

int DMusic::DMusic_Repeat(WCHAR* filename)
{
    // Initialize COM    

	CoInitialize(NULL);        

	// Create loader object    

	CoCreateInstance( CLSID_DirectMusicLoader, NULL, CLSCTX_INPROC,                       
					 IID_IDirectMusicLoader8, (void**)&g_pLoader );    

	// Create performance object    

	CoCreateInstance( CLSID_DirectMusicPerformance, NULL, CLSCTX_INPROC,                       
					 IID_IDirectMusicPerformance8, (void**)&g_pPerformance );    

	// Initialize the performance with the standard audio path.    

	// This initializes both DirectMusic and DirectSound and     

	// sets up the synthesizer.     


	g_pPerformance->InitAudio( NULL, NULL, NULL,                                
							  DMUS_APATH_SHARED_STEREOPLUSREVERB, 64,                               
							  DMUS_AUDIOF_ALL, NULL );    
	
	CHAR strPath[512];    

	if( GetWindowsDirectory( strPath, MAX_PATH+1 ) == 0 )        
		return 0;    

	strcat( strPath, "\\media" );    
	// Tell DirectMusic where the default search path is    

	WCHAR wstrSearchPath[MAX_PATH+1];    
	MultiByteToWideChar( CP_ACP, 0, strPath, -1,                          
						wstrSearchPath, MAX_PATH );    

	wstrSearchPath[MAX_PATH] = 0;	
	g_pLoader->SetSearchDirectory( GUID_DirectMusicAllTypes, 	                               
								  wstrSearchPath, FALSE );        

	
	WCHAR wstrFileName[50] = filename;
	//WCHAR wstrFileName[MAX_PATH] = L"ding.wav";   	


	if( FAILED( g_pLoader->LoadObjectFromFile( CLSID_DirectMusicSegment,                                               
												IID_IDirectMusicSegment8,
												wstrFileName,
												(LPVOID*) &g_pSegment ) ) )    
	{        
		MessageBox( NULL, "Media not found, sample will now quit",
					"DirectMusic Tutorial", MB_OK );        
	
	g_pPerformance->CloseDown();        
	g_pLoader->Release();         
	g_pPerformance->Release();        
	
	CoUninitialize();        
	return 0;    
}    
 
Thanks again for anyones help, sorry for the spacing issues, I just copied and pasted it out of .net Plex [edited by - plexmark on June 7, 2004 3:49:12 PM]
Plexmark-----------------------------If I can help send me a message: ICQ:15998198
Sr_Guapo
Sr_Guapo
I think he wants to pass the filename as a string to the function, instead of hardcoding the filename in the program...
Sean Henley [C++ Tutor]Rensselaer Polytechnic Institute
Plexmark
Plexmark
Sorry for the confusion, yes I would like to pass a filename instead of hardcoding it. I tried to convert it to unicode as I believe that is what it has to be in to work, but still cannot get it to accept it.

Thanks,
Plex
Plexmark-----------------------------If I can help send me a message: ICQ:15998198
Merlin85
Merlin85
Try the following.

First off, I recommend a small change to your code. I would replace:

WCHAR wstrFileName[50] = filename;

//WCHAR wstrFileName[MAX_PATH] = L"ding.wav";
if( FAILED( g_pLoader->LoadObjectFromFile(CLSID_DirectMusicSegment,IID_IDirectMusicSegment8,wstrFileName,LPVOID*)&g_pSegment)))

with the following:

//WCHAR wstrFileName[50] = filename;
//WCHAR wstrFileName[MAX_PATH] = L"ding.wav";

if( FAILED( g_pLoader->LoadObjectFromFile(CLSID_DirectMusicSegment,IID_IDirectMusicSegment8,filename,LPVOID*)&g_pSegment)))


The only difference here is the LoadObjectFromFile function. Instead of passing "wstrFileName" pass "filename".

Then make a call to your function like this:
DMusic->DMusic_Repeat(L"ding.wav");
Plexmark
Plexmark
Thanks for the help, and to help people searching as it has helped me so much, I will post the code that works.

It is a seperate class to play .wav files in your game, this one is set to repeat indef but can be easily changed for single tons as well.

Also, I was wondering if anyone knew a way to load sound into memory if I have a small sound that gets played alot to play it faster without a delay or overhead on the cpu.

I am working on that now.

Here is the class code:


#pragma once

#define INITGUID
#include <windows.h>
#include <string>
#include <dmusicc.h>
#include <dmusici.h>

using namespace std;

class DMusic
{
public:
DMusic()
{
IDirectMusicLoader8* g_pLoader = NULL;
IDirectMusicPerformance8* g_pPerformance = NULL;
IDirectMusicSegment8* g_pSegment = NULL;
}
int DMusic::DMusic_Repeat(WCHAR* name);
int DMusic::Close();
private:
IDirectMusicLoader8* g_pLoader;
IDirectMusicPerformance8* g_pPerformance;
IDirectMusicSegment8* g_pSegment;
};


and...



int DMusic::DMusic_Repeat(WCHAR* filename)
{
// Initialize COM

CoInitialize(NULL);

// Create loader object

CoCreateInstance( CLSID_DirectMusicLoader, NULL, CLSCTX_INPROC,
IID_IDirectMusicLoader8, (void**)&g_pLoader );

// Create performance object

CoCreateInstance( CLSID_DirectMusicPerformance, NULL, CLSCTX_INPROC,
IID_IDirectMusicPerformance8, (void**)&g_pPerformance );

// Initialize the performance with the standard audio path.

// This initializes both DirectMusic and DirectSound and

// sets up the synthesizer.


g_pPerformance->InitAudio( NULL, NULL, NULL,
DMUS_APATH_SHARED_STEREOPLUSREVERB, 64,
DMUS_AUDIOF_ALL, NULL );

CHAR strPath[512];

if( GetWindowsDirectory( strPath, MAX_PATH+1 ) == 0 )
return 0;

strcat( strPath, "\\media" );
// Tell DirectMusic where the default search path is

WCHAR wstrSearchPath[MAX_PATH+1];
MultiByteToWideChar( CP_ACP, 0, strPath, -1,
wstrSearchPath, MAX_PATH );

wstrSearchPath[MAX_PATH] = 0;
g_pLoader->SetSearchDirectory( GUID_DirectMusicAllTypes,
wstrSearchPath, FALSE );


if( FAILED( g_pLoader->LoadObjectFromFile( CLSID_DirectMusicSegment,
IID_IDirectMusicSegment8,
filename,
(LPVOID*) &g_pSegment ) ) )
{
MessageBox( NULL, "Media not found, sample will now quit",
"DirectMusic Tutorial", MB_OK );

g_pPerformance->CloseDown();
g_pLoader->Release();
g_pPerformance->Release();

CoUninitialize();
return 0;
}

// Download the segment''s instruments to the synthesizer

g_pSegment->Download( g_pPerformance );
// Play segment on the default audio path


g_pSegment->SetLoopPoints(0, 0);
g_pSegment->SetRepeats(-1);

g_pPerformance->PlaySegmentEx( g_pSegment, NULL, NULL, 0,
0, NULL, NULL, NULL );

return 0;
}



and the call to the function is as follows....



DMusic music;

music.DMusic_Repeat(L"followme.wav");



Thanks for the help Merlin

Plex
Plexmark-----------------------------If I can help send me a message: ICQ:15998198

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.