• Advertisement
Sign in to follow this  

Sound Help!! (C++) Urgent help required!

This topic is 4728 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, im using C++ to make my game for my project with directx 9. and im having trouble with the sound in the game!! the code that i am currently using is: PlaySound("Sounds/Gun_44magnum.wav",NULL,SND_ASYNC); where "Sounds/Gun_44magnum.wav" is the FILENAME for the sound. Now wat actually happens is that i say when i load my level up with music and then my character shoots a bullet than the music stops playing as the gunshot sound is run. How can i stop the sound from overwriting the music? I need to set the music to the background so that nothing else interferes with it! can someone help me with it and tell me wat to do please!! as i need to hand this in real soon [couple of days]!! all help is greatly appreciated thnx.

Share this post


Link to post
Share on other sites
Advertisement
how? can u help a bit more please!

cause im new to this.

thnx.

Share this post


Link to post
Share on other sites
There's an article in reference section that covers the very basics.
[edit]
If you want to use background music and sound effects I'd rather suggest using some free audio library instead. A list is available here.
[/edit]

Good luck!
Pat.

Share this post


Link to post
Share on other sites
ahh man too tell u the truth it still doesnt asnwer my question!!

cause i want to use PlaySound() for the sound!!!

isnt there anyway to apply some code to that so that it never turns off even if another sound file is played???

Share this post


Link to post
Share on other sites
Not sure, but it sounds like only have one sound buffer or are always using the primary one. I belive you need a different sound buffer for every sound, or as you described, the only one that will play is the most resently started.

Matt Hughson

Share this post


Link to post
Share on other sites
Quote:
Original post by Nads
ahh man too tell u the truth it still doesnt asnwer my question!!

cause i want to use PlaySound() for the sound!!!

isnt there anyway to apply some code to that so that it never turns off even if another sound file is played???


I don't know.

On the other hand, why do you choose to you use PlaySound? I'll assume it's because of its simplicity. However, you may find that fmod is almost as easy and has more features than you would ever desire. DirectAudio is easy too, but it almost requires a wrapper library to be useful .. like most DirectX components. There's also BASS, but I do no know much about the library.

Most people will tell you not to use PlaySound because it accesses and reads the sound file every time you call the function, essentially making it slow.

Don't be discouraged if another route seems more difficult at first.

Share this post


Link to post
Share on other sites
If you read the documentation on PlaySound, it explains that only one sound can be played at a time. If you are using DirectX for the graphics, you can use it for the sound too. I use OpenGL for graphics and OpenAL for sound. FMOD and BASS are supposed to be pretty good. PlaySound is very slow cuz as said before, it loads the sound each time the function is called. The WIN32 API has functions to play MID files in the background, then you can use PlaySound over that, but I recommend the other libraries for the speed and simplicity, and in some cases 3d sound. You can't get that with WIN32.

Share this post


Link to post
Share on other sites
Hi people,

thnx alot for everyones sujjestions!

Ive done away with the PlaySound() as u lot said, and used the DirectMusic appraoch, so that i cant play two files simultaneously! EG. music with gunshots!

But ive gotten a little stuck! Ive written all the code and initialised everything in my winmain() and also initialised the sounds directory but i dont know to call the function to play the sound in my other file, cause i want to play it in my other file. [although it doesnt play anything at all at the moment!]

for example: [i want to play it here (which is in a different .cpp file]

void changeStage()
{
if (gamestage==1)
{
// Play Sound
}
...


Also heres the code ive written in the winMain:


int APIENTRY WinMain(HINSTANCE Now,HINSTANCE hPrevInst,LPSTR pCmdLine ,int nCmdShow)
{
AdventureGame.StartGame(Now);

while(true)
{
if (!AdventureGame.NotFinished())
return exitgame;
AdventureGame.MainLoop();
FireBulletLoop();
}
return donotexit;

/////////////////////////////////////////////////////////////////////////

CoInitialize(NULL);

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

CoCreateInstance(CLSID_DirectMusicPerformance, NULL,
CLSCTX_INPROC, IID_IDirectMusicPerformance8,
(void**)&g_pPerformance );
//////////////////////////////////////////////////////////////////////////
g_pPerformance->InitAudio(
NULL, // IDirectMusic interface not needed.
NULL, // IDirectSound interface not needed.
NULL, // Window handle.
DMUS_APATH_SHARED_STEREOPLUSREVERB, // Default audiopath type.
64, // Number of performance channels.
DMUS_AUDIOF_ALL, // Features on synthesizer.
NULL // Audio parameters; use defaults.
);
/////////////////////////////////////////////////////////////////////////

// Find the Windows media directory.
CHAR Sounds[512];
if( GetWindowsDirectory( Sounds, MAX_PATH+1 ) == 0 )
return 0;
strcat( Sounds, "MGS2 Main Theme" );

// Convert to Unicode.

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

// Set the search directory.

g_pLoader->SetSearchDirectory(
GUID_DirectMusicAllTypes, // Types of files sought.
wstrSearchPath, // Where to look.
FALSE // Don't clear object data.
);
////////////////////////////////////////////////////////////////////////

WCHAR wstrFileName[MAX_PATH] = L"MGS2 Main Theme.wav";

if (FAILED(g_pLoader->LoadObjectFromFile(
CLSID_DirectMusicSegment, // Class identifier.
IID_IDirectMusicSegment8, // ID of desired interface.
wstrFileName, // Filename.
(LPVOID*) &g_pSegment // Pointer that receives interface.
)))
{
MessageBox( NULL, "Media not found, sample will now quit.",
"DirectMusic", MB_OK );
g_pPerformance->CloseDown();
g_pLoader->Release();
g_pPerformance->Release();
CoUninitialize();
return 0;
}
///////////////////////////////////////////////////////////////////////////
g_pSegment->Download( g_pPerformance );
//////////////////////////////////////////////////////////////////////////

g_pPerformance->PlaySegmentEx(
g_pSegment, // Segment to play.
NULL, // Not used.
NULL, // For transitions.
0, // Flags.
0, // Start time; 0 is immediate.
NULL, // Pointer that receives segment state.
NULL, // Object to stop.
NULL // Audiopath, if not default.
);
MessageBox( NULL, "Click OK to Exit.", "Play Audio", MB_OK );
////////////////////////////////////////////////////////////////////////////

g_pPerformance->Stop(
NULL, // Stop all segments.
NULL, // Stop all segment states.
0, // Do it immediately.
0 // Flags.
);

g_pSegment->Unload(g_pPerformance);
g_pPerformance->CloseDown();

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

CoUninitialize();

return 0; // Return value for WinMain.
}// End of WinMain.



plz try to help me ASAP as my deadline for prototype is on Thursday!!!

Thank You very much!

Share this post


Link to post
Share on other sites
Quote:
Original post by davidx9
I'd just use FMOD, very quick, very simple...



isit! ok can i have a link to it please, cause the one that someone gave above doesnt seem to work!!

Share this post


Link to post
Share on other sites
www.fmod.org

I'm such a nice guy, i'll even give you some code:


FSOUND_Init(44100, 32, 0);

sound1 = FSOUND_Sample_Load(FSOUND_FREE, "..\\Sounds\\outdoor_1.mp3", FSOUND_LOOP_OFF, 0, 0);

FSOUND_PlaySound(0, sound1);


Thats it!!

EXTRA - Make sure you follow the examples in the docs, to have multiple sounds at once, its very simple, but you should understand the difference between samples and streams.

Share this post


Link to post
Share on other sites
oh thank u soo much!!

it does look very simple and easy to use i hope it will work for me!!!

thanx alot m8, u were a great help!!

if i have any questions on playing a sound file while a music file is being played ill ask u and i hope u will help ;D , but thnx really it was very kind of u!!

if u could give me a quick exmaple of that[music with sound] that would be very helpful!!

Share this post


Link to post
Share on other sites
Quote:
Original post by davidx9
www.fmod.org

I'm such a nice guy, i'll even give you some code:


FSOUND_Init(44100, 32, 0);

sound1 = FSOUND_Sample_Load(FSOUND_FREE, "..\\Sounds\\outdoor_1.mp3", FSOUND_LOOP_OFF, 0, 0);

FSOUND_PlaySound(0, sound1);


Thats it!!

EXTRA - Make sure you follow the examples in the docs, to have multiple sounds at once, its very simple, but you should understand the difference between samples and streams.




its not working for me!! [doesnt compile]
i tried the code in the tutorial , i put it in the place where i wanted to use this sound, but it doesnt work!!

heres the code i used:

#include "fmod.h" //this is right at the top

FMUSIC_MODULE* handle;

...
//then sometime later i have the function if someone presses fire use the sound
...

//When the User presses the FIRE key F.
if (AdventureGame.keypressed[DIK_F] & 0x80 )
{

FSOUND_Init (44100, 32, 0);

handle=FMUSIC_LoadSong ("MGS2 Main Theme.wav");

FMUSIC_SetLooping (handle, false);

FMUSIC_PlaySong (handle);

FMUSIC_FreeSong (handle);
FSOUND_Close();

....

can anyone see anything wrong with that????

I get 6 differnt error messages wen i try to compile it which all look like this!! :-

EGG error LNK2019: unresolved external symbol _FMUSIC_PlaySong@4 referenced in function "public: void __thiscall GameFramework::MainGameInput(void)" (?MainGameInput@GameFramework@@QAEXXZ)


someone plz help urgently!!
thnx.

Share this post


Link to post
Share on other sites
This is the second post I've seen today of someone who doesn't even know the basics of his programming language and wants to make a game.

You need to link to the file "fmodvc.lib". That file will be included with the FMOD sdk you downloaded. Just copy it into the same folder as your project and add the line

#pragma comment (lib, "fmodvc.lib)

in ONE of your source or header files. you do NOT need to repeat it.

Secondly, read the FMOD documentation. The big page with all the examples will take you 5 minutes to read properly. You will then know that:

FSOUND_Init only needs to be called once (when the program is starting up).
FSOUND_LoadSong only needs to be called once per music file
FSOUND_SetLoop only needs to be called once per music file.
FSOUND_Update needs to be called every frame (or in a loop).
FSOUND_FreeSong only needs to be called once (when the program is shutting down).
FSOUND_Close only needs to be called once (when the program is shutting down).

You will probably get a microsecond of sound coming through the sound card before you delete the song and shut down FMOD. I hate to come down on you like this but documentation is provided for a reason. FMOD also comes with a lot of examples (in the "Samples" folder I think) which contain working programs. I'm pretty sure one of them covers loading music.

Please please please RTFM in future.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement