0
0

I am making a game and right now in visual studio using C. I am creating a new system, channel, and sound for every sound I have. This seems to be the only way to make the game to play different sounds without crashing or randomly choosing one sound to play for every effect. This is what my code looks like for the sound:

include <windows.h>

include <stdio.h>

include <conio.h>

include "fmod.h"

include "fmod_errors.h"

static FMOD_SYSTEM *system1, *system2, *system3;
static FMOD_SOUND *sound1, *sound2, *sound3;
static FMOD_CHANNEL *channel1 = 0, *channel2 = 0, *channel3 = 0;
static FMOD_RESULT result;
static unsigned int version;

void soundInit(void)
{
//creates system2///////////////////
result = FMOD_System_Create(&system2);
//gets the version of FMOD
result = FMOD_System_GetVersion(system2, &version);
//initialize the system
result = FMOD_System_Init(system2, 1, FMOD_INIT_NORMAL, NULL);

//creates system3/////////////////////
result = FMOD_System_Create(&amp;system3);
//gets the version of FMOD
result = FMOD_System_GetVersion(system3, &amp;version);
//initialize the system 
result = FMOD_System_Init(system3, 1, FMOD_INIT_NORMAL, NULL);

}
//different sounds////////////////////
void pigdie(const char *sound)
{
//creates sound
result = FMOD_System_CreateSound(system2, "Music\explosion.ogg", FMOD_HARDWARE, 0, &sound2);

//play sound
result = FMOD_System_PlaySound(system2, FMOD_CHANNEL_FREE, sound2, 0, &amp;channel2);

}
void eggtower(const char *sound)
{
//creates sound
result = FMOD_System_CreateSound(system3, "Music\eggtowerplace.mp3", FMOD_HARDWARE, 0, &sound3);

//play sound
result = FMOD_System_PlaySound(system3, FMOD_CHANNEL_FREE, sound3, 0, &amp;channel3);

}

I don’t have a the error check on right now. So this works for my game I call eggtower("Music\eggtowerplace.mp3"); and it plays perfectly but this seems to a really bad way to code because it uses up a tone of memory and makes the loading time for the game very slow. Is there any better way to make the sound work?

  • You must to post comments
0
0

That doesn’t sound right at all. You shouldn’t need more than one System instance unless you are doing something very unusual.

All you need is something like this:
Initialize System.
Load your sounds and keep their references somewhere.
For each time you want to play a sound you create or reuse a channel.

There is some code based on your own. Just an example, not meant to work.
[code:3vtvcslu]

include <windows.h>

include <stdio.h>

include <conio.h>

include "fmod.h"

include "fmod_errors.h"

static FMOD_SYSTEM *fmod_system
static FMOD_SOUND *explosion_sound, *shot_sound, *hit_sound;
static FMOD_RESULT result;
static unsigned int version;

void CheckResult( FMOD_RESULT result)
{
if(result != FMOD_RESULT.OK)
//do something with error.
}

void soundInit(void)
{
//Create system
CheckResult(FMOD_System_Create(&fmod_system));

//initialize system
CheckResult(FMOD_System_Init(fmod_system, 1, FMOD_INIT_NORMAL, NULL));

//Remember to update in your main loop or you will have trouble with a lot of things that depend on it.
//CheckResult(FMOD_RESULT FMOD_System_Update(fmod_system));

LoadSounds();
}

void LoadSounds()
{
CheckResult( FMOD_System_CreateSound(fmod_system, "explosion.ogg", FMOD_DEFAULT, 0, &explosion_sound));
CheckResult( FMOD_System_CreateSound(fmod_system, "shot.ogg", FMOD_DEFAULT, 0, &shot_sound));
CheckResult( FMOD_System_CreateSound(fmod_system, "hit.ogg", FMOD_DEFAULT, 0, &hit_sound));
}

FMOD_CHANNEL* Play(const char sound)
{
FMOD_CHANNEL
chan;
CheckResult(FMOD_System_PlaySound(fmod_system, FMOD_CHANNEL_FREE, sound, 0, &chan));
return chan;
}
[/code:3vtvcslu]

There you would call soundInit once to initialize your system and load your sounds and then start calling update in a loop somewhere. After that you can just call "Play(explosion_sound)" or something like that. Your biggest mistake there was treating sounds as if they were channels. FMOD_SOUND represents your sound asset, not the actual playing sound, those are represented by the FMOD_CHANNEL instances.

Try reading the examples that come with your FMOD API, at "FMOD SoundSystem\FMOD Programmers API Windows\examples\playsound".

  • You must to post comments
0
0

Right now this is what I have as my new code.
music.h:
//file contains function for music

include "fmod.h"

ifndef MUSIC_H

define MUSIC_H

static FMOD_SOUND *sound1;
static FMOD_SOUND *eggtower;
static FMOD_SOUND *pigdie;

void musicInit(void);
void soundInit(void);
void musicUpdate(void);
void loadSounds(void);
FMOD_CHANNEL* Play(FMOD_SOUND *sound);
void music(const char *music);
void musicUnload(void);

endif

music.cpp:
//FMOD music for the game

include <windows.h>

include <stdio.h>

include <conio.h>

include "fmod.h"

include "fmod_errors.h"

include "music.h"

static FMOD_SYSTEM *system1, *system2;
//static FMOD_SOUND *sound1;
static FMOD_CHANNEL *channel1 = 0;
static FMOD_RESULT result;
static unsigned int version;
static int paused = 0;
static float volume;

void ERRCHECK(FMOD_RESULT result)
{
if (result != FMOD_OK)
{
printf("FMOD error! (%d) %s\n", result, FMOD_ErrorString(result));
exit(-1);
}
}

// Create a System object and initialize.
void musicInit(void)
{
//creates sysmtem1
result = FMOD_System_Create(&system1);
ERRCHECK(result);
result = FMOD_System_GetVersion(system1, &version);
ERRCHECK(result);
result = FMOD_System_Init(system1, 1, FMOD_INIT_NORMAL, NULL);
ERRCHECK(result);
}

void loadSounds(void)
{
//creates sound
result = FMOD_System_CreateSound(system2, "Music\explosion.ogg", FMOD_HARDWARE, 0, &pigdie);

//creates sound
result = FMOD_System_CreateSound(system2, &quot;Music\\eggtowerplace.mp3&quot;, FMOD_HARDWARE, 0, &amp;eggtower);

}

void soundInit(void)
{
//creates system2///////////////////
result = FMOD_System_Create(&system2);
ERRCHECK(result);
//gets the version of FMOD
result = FMOD_System_GetVersion(system2, &version);
ERRCHECK(result);
//initialize the system
result = FMOD_System_Init(system2, 1, FMOD_INIT_NORMAL, NULL);
ERRCHECK(result);

loadSounds();

}

void musicUpdate(void)
{
result = FMOD_System_Update(system1); //keeps music playing
}

FMOD_CHANNEL* Play(FMOD_SOUND sound)
{
FMOD_CHANNEL
channel2;
result = FMOD_System_PlaySound(system2, FMOD_CHANNEL_FREE, sound, 0, &channel2);
ERRCHECK(result);
return channel2;
}

void music(const char *music)
{
//creates song
result = FMOD_System_CreateStream(system1, "Music\Chyptune.mp3", FMOD_HARDWARE | FMOD_LOOP_NORMAL | FMOD_2D, 0, &sound1);
ERRCHECK(result);
result = FMOD_System_PlaySound(system1, FMOD_CHANNEL_FREE, sound1, 0, &channel1);
ERRCHECK(result);
}

void musicUnload(void)
{
result = FMOD_Sound_Release(sound1);
result = FMOD_Sound_Release(eggtower);
result = FMOD_Sound_Release(pigdie);
result = FMOD_System_Close(system1);
result = FMOD_System_Release(system1);
result = FMOD_System_Close(system2);
result = FMOD_System_Release(system2);
}

I call Play(eggtower); in my gamestatemanager.
Right now this is my function is now and with the ERRCHECK it will close the program when I call the function:

FMOD_CHANNEL* Play(FMOD_SOUND sound)
{
FMOD_CHANNEL
channel2;
result = FMOD_System_PlaySound(system2, FMOD_CHANNEL_FREE, sound, 0, &channel2);
ERRCHECK(result);
return channel2;
}

if I change it to:

FMOD_CHANNEL* Play(FMOD_SOUND sound)
{
FMOD_CHANNEL
channel2;
result = FMOD_System_PlaySound(system2, FMOD_CHANNEL_FREE, eggtower, 0, &channel2);
ERRCHECK(result);
return channel2;
}

eggtower will make all the sounds eggtower even if I call Play(pigdie).
So it won’t close the game but I can only play one sound.

  • You must to post comments
0
0

Why are you using two system objects? You could load that ‘Chyptune.mp3’ stream in the ‘loadSounds’ method using the same system object as the other sounds. You should update every system object you’re using.

What error are you getting when you call the unmodified Play?
[code:2qz9go4u]
FMOD_CHANNEL* Play(FMOD_SOUND sound)
{
FMOD_CHANNEL
channel2;
result = FMOD_System_PlaySound(system2, FMOD_CHANNEL_FREE, sound, 0, &channel2);
ERRCHECK(result);
return channel2;
}
[/code:2qz9go4u]

You are telling the Play method to only use the eggtower sound in that modified method. That is why it only plays that.

Do use the "[ code] your code [ /code]" tags when pasting code here. It makes your code a lot more readable, just like the one I wrote above.

  • You must to post comments
0
0

Everything will compile without an Error and the code will work but if I hit space (which is what one of the sounds is set to right now) the ERRCHECK I have will exit the program. If I debug the program I get FMOD_ERR_INVALID_PARAM from the ERRCHECK.

But if I write the ERRCHECK like this
[code:sph8a3je]void ERRCHECK(FMOD_RESULT result)
{
if(result != FMOD_OK || result != FMOD_ERR_INVALID_PARAM)
{
printf("FMOD error! (%d) %s\n", result, FMOD_ErrorString(result));
exit(-1);
}
}[/code:sph8a3je]

The program still just shuts down when I hit space. So it appears that the sound in:

[code:sph8a3je]result = FMOD_System_PlaySound(system2, FMOD_CHANNEL_FREE, sound, 0, &channel2);[/code:sph8a3je]

isn’t doing anything or accessing these:

[code:sph8a3je]static FMOD_SOUND *sound1;
static FMOD_SOUND *eggtower;
static FMOD_SOUND *pigdie;[/code:sph8a3je]

  • You must to post comments
0
0

Send me your code via PM here in the forum. I’ll check it out. 😉

  • You must to post comments
Showing 5 results
Your Answer

Please first to submit.