Answered
0
0

Hello, I am new here and I also am pretty much a beginner in programming.
My English may not be that good (I’m not a native speaker).

I am currently working on a small project in CodeBlocks (MinGW) for some certification and I encountered a problem with FMOD EX. As the title says, everytime I am running the game, everything is fine, but after the sounds have been played 16 times FMOD simply stops and won’t play the sounds again until I reopen the game. I am using the latest version of FMOD EX (4.44.61).

    /******** CLASS DEFINITION ********/

class Sound {
private:
    static bool on; //is sound on?
    static bool possible; //is it possible to play sound?
    static char * currentSound; //currently played sound
    //FMOD-specific stuff
    static FMOD_BOOL isplaying;
    static FMOD_RESULT result;
    static FMOD_SYSTEM * fmodsystem;
    static FMOD_SOUND * sound,*sound2,*sound3;
    static FMOD_CHANNEL * channel,*channel2,*channel3;

public:
    static void initialise (void); //initialises sound

    //sound control
    static void setVolume (float v); //sets the actual playing sound's volume
    static void load (const char * filename); //loads a soundfile
    static void load2 (const char * filename); //loads a soundfile
    static void load3 (const char * filename); //loads a soundfile
    static void play (bool pause = false); //plays a sound (may be started paused; no argument for unpaused)
    static void play2 (bool pause = false); //plays a sound (may be started paused; no argument for unpaused)
    static void play3 (bool pause = false); //plays a sound (may be started paused; no argument for unpaused)
};


/******** CLASS VARIABLE DECLARATIONS ********/

bool Sound::on = true; //is sound on?
bool Sound::possible = true; //is it possible to play sound?
char * Sound::currentSound; //currently played sound
//FMOD-specific stuff
FMOD_BOOL Sound::isplaying;
FMOD_RESULT Sound::result;
FMOD_SYSTEM * Sound::fmodsystem;
FMOD_SOUND * Sound::sound, *Sound::sound2, *Sound::sound3;
FMOD_CHANNEL * Sound::channel,*Sound::channel2,*Sound::channel3;

/******** METHODS' IMPLEMENTATIONS ********/

//initialises sound
void Sound::initialise (void) {
    //create the sound system. If fails, sound is set to impossible
    result = FMOD_System_Create(&fmodsystem);
    if (result != FMOD_OK) possible = false;
    //if initialise the sound system. If fails, sound is set to impossible
    if (possible) result = FMOD_System_Init(fmodsystem, NUM_CHANNELS, FMOD_INIT_NORMAL, 0);
    if (result != FMOD_OK) possible = false;
    //sets initial sound volume (mute)
    if (possible) FMOD_Channel_SetVolume(channel,0.0f);
}


//sets the actual playing sound's volume
void Sound::setVolume (float v) {
    if (possible && on && v >= 0.0f && v <= 1.0f) {
        FMOD_Channel_SetVolume(channel,v);
    }
}

//loads a soundfile
void Sound::load (const char * filename) {
    currentSound = (char *)filename;
    if (possible && on) {
        result = FMOD_Sound_Release(sound);
        result = FMOD_System_CreateStream(fmodsystem, currentSound, FMOD_SOFTWARE, 0, &sound);
        if (result != FMOD_OK) possible = false;
    }
}

//loads a soundfile
void Sound::load2 (const char * filename) {
    currentSound = (char *)filename;
    if (possible && on) {
        result = FMOD_Sound_Release(sound2);
        result = FMOD_System_CreateStream(fmodsystem, currentSound, FMOD_SOFTWARE, 0, &sound2);
        if (result != FMOD_OK) possible = false;
    }
}

//loads a soundfile
void Sound::load3 (const char * filename) {
    currentSound = (char *)filename;
    if (possible && on) {
        result = FMOD_Sound_Release(sound3);
        result = FMOD_System_CreateStream(fmodsystem, currentSound, FMOD_SOFTWARE, 0, &sound3);
        if (result != FMOD_OK) possible = false;
    }
}

//plays a sound (no argument to leave pause as dafault)
void Sound::play (bool pause) {
    if (possible && on) {
        result = FMOD_System_PlaySound(fmodsystem,FMOD_CHANNEL_REUSE, sound, pause, &channel);
        FMOD_Channel_SetMode(channel,FMOD_LOOP_OFF);
        FMOD_System_Update(fmodsystem);
        FMOD_Channel_IsPlaying(channel,&isplaying);
        if (isplaying == false) {FMOD_Channel_Stop(channel); channel=NULL;}
    }
}

void Sound::play2 (bool pause) {
    if (possible && on) {
        result = FMOD_System_PlaySound(fmodsystem,FMOD_CHANNEL_REUSE, sound2, pause, &channel2);
        FMOD_Channel_SetMode(channel2,FMOD_LOOP_OFF);
        FMOD_System_Update(fmodsystem);
        FMOD_Channel_IsPlaying(channel2,&isplaying);
        if (isplaying == false) {FMOD_Channel_Stop(channel2); channel2=NULL;}
    }
}

void Sound::play3 (bool pause) {
    if (possible && on) {
        result = FMOD_System_PlaySound(fmodsystem,FMOD_CHANNEL_REUSE, sound3, pause, &channel3);
        FMOD_Channel_SetMode(channel3,FMOD_LOOP_OFF);
        FMOD_System_Update(fmodsystem);
        FMOD_Channel_IsPlaying(channel3,&isplaying);
        if (isplaying == false) {FMOD_Channel_Stop(channel3); channel3=NULL;}
    }
}

other code
.
.
.

if (cond) {Sound::initialise();
                    Sound::load("Sunete//wrongmove.wav");
                    Sound::play();}
if (cond) {Sound::initialise();
                    Sound::load2("Sunete//rightmove.wav");
                    Sound::play2();}
if (cond) {Sound::initialise();
                    Sound::load3("Sunete//hint.wav");
                    Sound::play3();}

This is how my code looks right now and I am quite desperate about this. I have tried almost everything I could find on the internet regarding this problem and I was not able to fix it.

As it goes, I am using FMOD EX to play 3 sounds when some conditions are met. It is all going all right but after the code has played a total of 16 sounds (doesn’t matter if it played 16 times the same sound) it simply stops and the only way to get the sounds back is to reopen the game. I do not know if it is worth to mention, but I also use twice PlaySound (from MSDN) to play some background music (but I have tried to run the game without those and the results are the same). The code you have seen before it is all that I am using for the game regarding FMOD EX (There is not anything else that could cause this in my full code).I don’t know what may be causing this.

If someone can help, I would be eternally grateful!

  • You must to post comments
Best Answer
0
0

I will try that. And regarding the setVolume I know I do not use it, I was going to delete it. I will come back after editing the code. Thank you for the help!

Best regards,
Gabriel.

UPDATE: I did what you said, but the problem is still there. After 16 sounds have been played, the sound disappears. Any more ideas?

UPDATE #2: Problem solved. If anyone is just as stupid as me, the solution is so trivial that I am embarrassed way too much about it. All I had to do was to move Sound::initialise(); IN THE MAIN FUNCTION (not in other functions AS I DID). *[Sorry for Caps Lock, just wanted to emphasize what my mistake was, maybe it will help others.].

Request

This topic can be closed.

  • You must to post comments
0
0

You should call initialize once, not each time you play a sound.

Also in initialize you’re using

if (possible) FMOD_Channel_SetVolume(channel,0.0f);

You should delete this, you’re referencing an unused variable at this point.

  • You must to post comments
Showing 2 results
Your Answer

Please first to submit.