Skip to content
This repository has been archived by the owner on Apr 29, 2024. It is now read-only.

Commit

Permalink
[SoundService] add alt-tab sound fader
Browse files Browse the repository at this point in the history
  • Loading branch information
espkk committed Jan 6, 2022
1 parent e17637c commit f06450c
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/apps/engine/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "compiler.h"
#include "file_service.h"
#include "s_debug.h"
#include "v_sound_service.h"
#include "storm/fs.h"
#include "watermark.hpp"

Expand Down Expand Up @@ -69,11 +70,19 @@ void HandleWindowEvent(const storm::OSWindow::Event &event)
{
bActive = true;
core.AppState(bActive);
if (const auto soundService = static_cast<VSoundService *>(core.CreateService("SoundService")))
{
soundService->SetActiveWithFade(true);
}
}
else if (event == storm::OSWindow::FocusLost)
{
bActive = false;
core.AppState(bActive);
if (const auto soundService = static_cast<VSoundService *>(core.CreateService("SoundService")))
{
soundService->SetActiveWithFade(false);
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/libs/sound_service/include/v_sound_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ class VSoundService : public SERVICE

virtual void SetEnabled(bool _enabled) = 0;
virtual void LoadAliasFile(const char *_filename) = 0;

virtual void SetActiveWithFade(bool active) = 0;

tSoundStatistics soundStatistics;
};

Expand Down
64 changes: 63 additions & 1 deletion src/libs/sound_service/src/sound_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,16 @@ bool SoundService::Init()
return false;
}
core.Trace("Using FMOD %08x", FMOD_VERSION);

CHECKFMODERR(system->setSoftwareChannels(64));
CHECKFMODERR(system->setOutput(FMOD_OUTPUTTYPE_AUTODETECT));
CHECKFMODERR(system->init(MAX_SOUNDS_SLOTS, FMOD_INIT_NORMAL, nullptr));
CHECKFMODERR(system->set3DSettings(1.0, DISTANCEFACTOR, 1.0f));

if (const auto ini = fio->OpenIniFile(core.EngineIniFileName()))
{
fadeTimeInSeconds = ini->GetFloat("sound", "fade_time", 0.5f);
}

SoundsActive = 2; // 0 and 1 are special

InitAliases();
Expand Down Expand Up @@ -830,6 +834,64 @@ void SoundService::SetEnabled(bool _enabled)
{
}

void SoundService::SetActiveWithFade(const bool active)
{
if (fadeTimeInSeconds == 0.0f)
{
return;
}

if (active)
{
system->mixerResume();
}

FMOD::ChannelGroup* mastergroup;
CHECKFMODERR(system->getMasterChannelGroup(&mastergroup));
unsigned long long parentclock;
int rate;
system->getSoftwareFormat(&rate, nullptr, nullptr);
mastergroup->getDSPClock(nullptr, &parentclock);

const auto dsp_clock_start = parentclock;
const auto dsp_clock_end = static_cast<unsigned long long>(parentclock + fadeTimeInSeconds * rate);

if (active)
{
mastergroup->setDelay(dsp_clock_start, 0, false);
}

for (const auto& PlayingSound : PlayingSounds)
{
if (PlayingSound.bFree)
continue;

float volume = 0.0f;

if (PlayingSound.type == VOLUME_FX)
{
volume = fFXVolume;
}
else if (PlayingSound.type == VOLUME_MUSIC)
{
volume = fMusicVolume;
}
else if (PlayingSound.type == VOLUME_SPEECH)
{
volume = fSpeechVolume;
}

PlayingSound.channel->addFadePoint(dsp_clock_start, active ? 0.0f : volume);
PlayingSound.channel->addFadePoint(dsp_clock_end, active ? volume : 0.0f);
}

if (!active)
{
mastergroup->setDelay(dsp_clock_start, dsp_clock_end, false);
system->mixerSuspend();
}
}

void SoundService::SoundStop(TSD_ID _id, long _time)
{
bool is_playing; // boal fix
Expand Down
4 changes: 4 additions & 0 deletions src/libs/sound_service/src/sound_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ class SoundService : public VSoundService

float fPitch;

float fadeTimeInSeconds = 0.5f;

public:
SoundService();
~SoundService() override;
Expand Down Expand Up @@ -210,6 +212,8 @@ class SoundService : public VSoundService
bool AddScheme(const char *_schemeName) override;
void SetEnabled(bool _enabled) override;

void SetActiveWithFade(bool active) override;

void DebugDraw();
void DebugPrint3D(const CVECTOR &pos3D, float rad, long line, float alpha, uint32_t color, float scale,
const char *format, ...) const;
Expand Down

0 comments on commit f06450c

Please sign in to comment.