0
0

I am using FMOD in a project for school. I have it set up as simply as possible. I load an mp3 and play it. However, I find that as I keep hitting the button to play the sound, eventually the sound just stops. Well, I load three sounds, and they are used for the button system. I also have a stream loaded for music. For some reason, if I keep pressing the button, and thus keep playing the sounds, eventually all three sounds will simply stop playing at all. Though the stream will continue, and even restart. The others simply won’t play. I have error checking code running, but they don’t read that anything is wrong. Indeed, all the functions return FMOD_OK. If anyone could help that would be nice, here is my code…

This is where I ask the sounds to play…
int PressButton::Do()
{
//Take care of any animation
animcon.Do();

switch(state)
{
case NORMAL:
    {
        if(MDI->x() > bound.right || MDI->x() < bound.left || MDI->y() > bound.bottom || MDI->y() < bound.top)
        {
            //mouse outside box
        } else
        {

            //Okay, so the mouse is over the button
            if(mouseover)
                mouseover->PlayDefault();
            animcon.SetCurrAnim(0);
            animcon.SetForward();
            animcon.Start();
            state = MOUSEOVER;
        }
    } break;
case MOUSEOVER:
    {
        if(MDI->x() > bound.right || MDI->x() < bound.left || MDI->y() > bound.bottom || MDI->y() < bound.top)
        {
            //mouse outside box
            if(mouseoff)
                mouseoff->PlayDefault();
            animcon.SetCurrAnim(0);
            animcon.SetBackward();
            animcon.Start();
            state = NORMAL;

        } else
        {
            //mouse still in box
            if(MDI->PressedLeft())
            {
                if(pressed)
                    pressed->PlayDefault();

                animcon.SetState(1,0,true,false);
                animcon.Start();
                state = PRESSED;
            }
        }
    } break;
case PRESSED:
    {
        if(MDI->x() > bound.right || MDI->x() < bound.left || MDI->y() > bound.bottom || MDI->y() < bound.top)
        {
            //mouse outside box
            if(mouseoff)
                mouseoff->PlayDefault();
            animcon.SetCurrAnim(0);
            animcon.SetCurrFrame(animcon.getcurrframecount()-1);
            animcon.SetBackward();
            animcon.Start();
            state = NORMAL;

        } else
        {

            //mouse still inside box
            if(MDI->ReleasedLeft())
            {
                animcon.SetState(0,0,true,false);
                animcon.Stop();
                state = NORMAL;
                return(true);
            }
        }
    } break;
default:break;
}

return(false);

}

SoundSystem Wrapper.cpp

include "SoundSystem Wrapper.h"

//The sound system interface
int SoundSystem::Init()
{
if(F_FAILED(FMOD::System_Create(&system)))
{
return(ERRORMSG("Could not create FMOD sound system."));
}

if(F_FAILED(system->setSpeakerMode(FMOD_SPEAKERMODE_STEREO)))
{
    return(ERRORMSG("Could set speaker mode to stereo."));
}

if(F_FAILED(system->init(2000, FMOD_INIT_NORMAL, 0)))
{
    return(ERRORMSG("Could not initialize FMOD sound system."));
}

return(OKAY);

}

int SoundSystem::CleanUp()
{
if(system)
{
if(F_FAILED(system->release()))
{
return(ERRORMSG("Could not release sound system?"));
}
}
system = NULL;

return(OKAY);

}

//The sound head class
const double SOUNDCUTOFF = 512.0;
const double SOUNDPANOFF = 512.0;
const int CALCFRAMECOUNT = 17;

int SoundHead::PlayDefault(double x, double y, float scale, float volume)
{
FMOD::Channel *channel;
if(F_FAILED(mainss->getsystem()->playSound(FMOD_CHANNEL_FREE, sound, false, &channel)))
{
ERRORMSG("Could not create channel from sound.");
}
if(channel == NULL)
{
ERRORMSG("Channel not created?");
}

if(x != 0.0 || y != 0.0)
{
    {
        double totvol;
        double distance = (sqrt(((x*x)+(y*y))))/SOUNDCUTOFF;
        if(distance <= 1.0 && distance >= 0.0)
        {
            totvol = volume - distance;

            channel->setVolume(totvol);
        }
        else
        {
            totvol = 0;
        }

        float pan = x/SOUNDPANOFF;
        if(pan >= -1.0 && pan <= 1.0)
        {
            channel->setPan(pan);
        }
        else
        {
        }
    }

    if(scale != 0.0)
    {
        channel->setFrequency(dfreq * scale);
        scale = 0.0;
    }
}

if(F_FAILED(channel->setPaused(false)))
{
    ERRORMSG("channel not exists.");
}
return(OKAY);

}

FMOD::Channel* SoundHead::MakeChannel()
{
FMOD::Channel *channel;
if(F_FAILED(mainss->getsystem()->playSound(FMOD_CHANNEL_FREE, sound, false, &channel)))
{
ERRORMSG("Could not create channel from sound.");
}

return(channel);

}

int SoundHead::LoadFile(char filename[], bool streamt)
{
if(!full)
{
stream = streamt;
if(stream)
{
if(F_FAILED((mainss->getsystem())->createStream(filename, FMOD_DEFAULT, 0, &sound)))
{
return(ERRORMSG("Could not load sound file."));
}
}else
{
if(F_FAILED(mainss->getsystem()->createSound(filename, FMOD_DEFAULT, 0, &sound)))
{
return(ERRORMSG(CONCAT("Could not load sound from file:", filename)));
}
}

    sound->getDefaults(&dfreq, &dvol, &dpan, NULL);

    full = true;
}else
{
    return(ERRORMSG("Sound Head already full."));
}

return(OKAY);

}

int SoundHead::CleanUp()
{
if(full)
{
if(sound)
sound->release();
sound = NULL;
full = false;
} else
{
//do nothing there is nothing there anyway
}
return(OKAY);
}

//The SoundInstance Class

int SoundInstance::StopSound()
{
if(playing)
{
playing = false;
channel->stop();
} else
{
//do nothing already stopped
}
return(OKAY);
}

int SoundInstance::StartSound(bool loopingt)
{
if(!playing)
{
looping = loopingt;
channel = shead->MakeChannel();
playing = true;
Calculate();
}else
{

}

return(OKAY);

}

int SoundInstance::Calculate()
{

if(playing)
{
    bool still;
    channel->isPlaying(&still);
    if(!still)
    {
        if(looping)
        {
            playing = false;
            StartSound(looping);
        }
        else
        {
            playing = false;
            channel = NULL;
            return(OKAY);
        }

    }


    if(counter++ < CALCFRAMECOUNT)
    {
    if(x != 0.0 || y != 0.0)
    {
        double totvol;
        double distance = (sqrt(((x*x)+(y*y))))/SOUNDCUTOFF;
        if(distance <= 1.0 && distance >= 0.0)
        {
            totvol = volume - distance;

            channel->setVolume(totvol);
        }
        else
        {
            totvol = 0;
        }

        pan = x/SOUNDPANOFF;
        if(pan >= -1.0 && pan <= 1.0)
        {
            channel->setPan(pan);
        }
        else
        {
        }
    }

    if(scale != 0.0)
    {
        channel->setFrequency(frequency * scale);
        scale = 0.0;
    }
    }else
        counter = 0;

}

return(OKAY);

}

SoundSystemWrapper.h

ifndef SOUNDSYSTEMWRAPPER

define SOUNDSYSTEMWRAPPER

include "fmod.hpp"

include "fmod_errors.h"

//The Glorious includes!

include "ErrorHandling.h"

include "Windows Wrapper.h"

include <windows.h>

include <windowsx.h>

include <mmsystem.h>

include <conio.h>

include <stdlib.h>

include <malloc.h>

include <memory.h>

include <string.h>

include <stdarg.h>

include <stdio.h>

include <math.h>

include <io.h>

include <fcntl.h>

include <sys/timeb.h>

include <time.h>

//FAILED for FMOD!

define F_FAILED(a) (FMOD_OK != a)

//The Sound System Interface
class SoundSystem
{
private:
FMOD::System *system;
public:
SoundSystem() {system = NULL;}
~SoundSystem() {CleanUp();}
FMOD::System *getsystem() {return(system);}
int Init();
int CleanUp();
};

//The Sound Head class
class SoundHead
{
private:
SoundSystem *mainss;
FMOD::Sound *sound;
bool stream;
bool full;
float dfreq;
float dvol;
float dpan;
public:
SoundHead(SoundSystem *mainsst) {full = false; mainss = mainsst;}
SoundHead(SoundSystem *mainsst, char filename[], bool streamt) {full = false; mainss = mainsst; LoadFile(filename, streamt);}
~SoundHead() {CleanUp();}

float getdfreq() {return(dfreq);}
float getdvol() {return(dvol);}
float getdpan() {return(dpan);}
int LoadFile(char filename[], bool streamt);
int CleanUp();
FMOD::Channel* MakeChannel();
int PlayDefault(double x = 0.0, double y = 0.0, float scale = 0.0, float volume = 1.0);

};

//The SoundInstance Class
class SoundInstance
{
private:
SoundHead *shead;
bool playing;
bool looping;
double x; //These represent distance from center (where the person is who is hearing the sound 0,0 = no calculation bias
double y;
float frequency;
float volume;
float pan;
float scale;
FMOD::Channel *channel;
int counter; //only calculate every CALCFRAMECOUNT frames
public:
SoundInstance() {shead = NULL; playing = false; looping = false; counter = 0;}
SoundInstance(SoundHead *sheadt) {shead = sheadt; playing = false; looping = false; counter = 0; frequency = shead->getdfreq(); pan = shead->getdpan(); volume = shead->getdvol(); scale = 0.0;}
~SoundInstance() {StopSound();}

int sethead(SoundHead *sheadt) {if(!playing) {shead = sheadt; playing = false; looping = false; counter = 0; frequency = shead-&gt;getdfreq(); pan = shead-&gt;getdpan(); volume = shead-&gt;getdvol(); scale = 0.0; return(OKAY);}else return(ERRORMSG(&quot;Sound instance playing.&quot;));}
int Set(double xt, double yt) {x = xt; y = yt; return(Calculate());}
int SetScale(float scalet) {scale = scalet; return(Calculate());}
int SetVolume(float volumet) {volume = volumet; return(Calculate());}
int SetXYScale(double xt, double yt, float scalet) {x = xt; y = yt; scale = scalet; return(Calculate());}
int SetXYVolume(double xt, double yt, float volumet) {x = xt; y = yt; volume = volumet; return(Calculate());}
int Set(double xt, double yt, float scalet, float volumet) {x = xt; y = yt; scale = scalet; volume = volumet; return(Calculate());}
int Calculate();
int StartSound(bool loopingt);
int StopSound();

};

endif

  • You must to post comments
0
0

Okay, well, I tried the obvious thing and switched my other three from full loads into memory to streams. And seems to have done the trick.
But I wonder why?

  • You must to post comments
0
0

I encountered the same type of problem when I first started using FMOD, the solution is….

drumroll….

call FMOD::update in your main loop

  • You must to post comments
Showing 2 results
Your Answer

Please first to submit.