I have some questions about fading in and fading out a playing sound. Here is my situation, there are two areas in my application and each have a sound. When the player enter an area, sound belongs to the will play with 5 seconds fade-in, and when the player leave it, the sound will add 5 seconds fade-out then stop. The FMOD_Channel_AddFadePoint works fine when player leave an area and re-enter the area after 5 seconds or enter the area again after leave the area 5 seconds. Following is my code:
void Sound::fadein(float fade_time)
{
if (!isPlaying())
return;
int rate = 0;
FMOD_System_GetSoftwareFormat(SoundUtil::FMODSystem, &rate, 0, 0);
unsigned long long dspclock = 0u;
FMOD_Channel_GetDSPClock(d_p->channel, nullptr, &dspclock);
FMOD_Channel_AddFadePoint(d_p->channel, dspclock, 0.f);
FMOD_Channel_AddFadePoint(d_p->channel, dspclock + unsigned long long(rate * fade_time), 1.f);
}
void Sound::fadeout(float fade_time)
{
if (!isPlaying())
return;
int rate = 0;
FMOD_System_GetSoftwareFormat(SoundUtil::FMODSystem, &rate, 0, 0);
unsigned long long dspclock = 0u;
FMOD_Channel_GetDSPClock(d_p->channel, nullptr, &dspclock);
FMOD_Channel_AddFadePoint(d_p->channel, dspclock, 1.f));
FMOD_Channel_AddFadePoint(d_p->channel, dspclock + unsigned long long(rate * fade_time), 0.f);
FMOD_Channel_SetDelay(d_p->channel, 0, dspclock + unsigned long long(rate * fade_time), true);
}
However, sometimes the player will re-enter area before fade-out tick finished, for example leave area at 10 o’clock and re-enter it at 10 o’click and 3 second, I find the fade-out will still tick. I check the document and find another api: FMOD_Channel_SetFadePointRamp, it says “This is a helper function that automatically ramps from the current fade volume to the newly provided volume at a specified time. It will clear any fade points set after this time.”, I think it will meet my requirement, but it doesn’t work fine.
So how can I do at this situation? How can I get actual volume(not the volume set by FMOD_Channel_SetVolume) in fade process and stop it then begin a new fade process?