mirror of
https://github.com/WarmUpTill/SceneSwitcher.git
synced 2026-07-01 00:41:07 -05:00
Rework handling of fade threads
Previous implementation did not allow ongoing fades to be stopped. New implementations allows waiting for fade to complete.
This commit is contained in:
parent
a86f7d0fd4
commit
ac5b2c3f9e
|
|
@ -325,7 +325,7 @@ AdvSceneSwitcher.action.audio.type.mute="Mute"
|
|||
AdvSceneSwitcher.action.audio.type.unmute="Unmute"
|
||||
AdvSceneSwitcher.action.audio.type.sourceVolume="Set source volume"
|
||||
AdvSceneSwitcher.action.audio.type.masterVolume="Set master volume"
|
||||
AdvSceneSwitcher.action.audio.fade="{{fade}}Fade over {{duration}} seconds"
|
||||
AdvSceneSwitcher.action.audio.fade="{{fade}}Fade over {{duration}} seconds. {{wait}} Wait for fade to complete."
|
||||
AdvSceneSwitcher.action.audio.entry="{{actions}} {{audioSources}} {{volume}}"
|
||||
AdvSceneSwitcher.action.recording="Recording"
|
||||
AdvSceneSwitcher.action.recording.type.stop="Stop recording"
|
||||
|
|
|
|||
|
|
@ -437,13 +437,6 @@ void SwitcherData::Stop()
|
|||
server.stop();
|
||||
client.disconnect();
|
||||
|
||||
for (auto &t : audioHelperThreads) {
|
||||
if (t.joinable()) {
|
||||
t.join();
|
||||
}
|
||||
}
|
||||
audioHelperThreads.clear();
|
||||
|
||||
if (showSystemTrayNotifications) {
|
||||
DisplayTrayMessage(
|
||||
obs_module_text("AdvSceneSwitcher.pluginName"),
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ enum class AudioAction {
|
|||
class MacroActionAudio : public MacroAction {
|
||||
public:
|
||||
MacroActionAudio(Macro *m) : MacroAction(m) {}
|
||||
virtual ~MacroActionAudio();
|
||||
bool PerformAction();
|
||||
void LogAction();
|
||||
bool Save(obs_data_t *obj);
|
||||
|
|
@ -32,8 +33,15 @@ public:
|
|||
int _volume = 0;
|
||||
bool _fade = false;
|
||||
Duration _duration;
|
||||
bool _wait = false;
|
||||
|
||||
private:
|
||||
void StartSourceFade();
|
||||
void StartMasterFade();
|
||||
void FadeSourceVolume();
|
||||
void FadeMasterVolume();
|
||||
std::thread _fadeThread;
|
||||
|
||||
static bool _registered;
|
||||
static const std::string id;
|
||||
};
|
||||
|
|
@ -61,6 +69,7 @@ private slots:
|
|||
void VolumeChanged(int value);
|
||||
void FadeChanged(int value);
|
||||
void DurationChanged(double seconds);
|
||||
void WaitChanged(int value);
|
||||
signals:
|
||||
void HeaderInfoChanged(const QString &);
|
||||
|
||||
|
|
@ -70,6 +79,7 @@ protected:
|
|||
QSpinBox *_volumePercent;
|
||||
QCheckBox *_fade;
|
||||
DurationSelection *_duration;
|
||||
QCheckBox *_wait;
|
||||
QHBoxLayout *_fadeLayout;
|
||||
std::shared_ptr<MacroActionAudio> _entryData;
|
||||
|
||||
|
|
|
|||
|
|
@ -88,6 +88,7 @@ public:
|
|||
bool MatchOnChange() { return _matchOnChange; }
|
||||
int GetCount() { return _count; };
|
||||
void ResetCount() { _count = 0; };
|
||||
bool GetStop() { return _stop; }
|
||||
void Stop() { _stop = true; }
|
||||
std::deque<std::shared_ptr<MacroCondition>> &Conditions()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -111,7 +111,6 @@ struct SwitcherData {
|
|||
StartupBehavior startupBehavior = PERSIST;
|
||||
AutoStartEvent autoStartEvent = AutoStartEvent::NEVER;
|
||||
|
||||
std::vector<std::thread> audioHelperThreads;
|
||||
std::atomic_bool masterAudioFadeActive = {false};
|
||||
std::unordered_map<std::string, std::atomic_bool> activeAudioFades;
|
||||
|
||||
|
|
|
|||
|
|
@ -18,30 +18,39 @@ const static std::map<AudioAction, std::string> actionTypes = {
|
|||
"AdvSceneSwitcher.action.audio.type.masterVolume"},
|
||||
};
|
||||
|
||||
MacroActionAudio::~MacroActionAudio()
|
||||
{
|
||||
if (_fadeThread.joinable()) {
|
||||
_fadeThread.join();
|
||||
}
|
||||
}
|
||||
|
||||
constexpr auto fadeInterval = std::chrono::milliseconds(100);
|
||||
constexpr float minFade = 0.000001f;
|
||||
|
||||
void fadeSourceVolume(Duration duration, float vol, OBSWeakSource audioSource)
|
||||
void MacroActionAudio::FadeSourceVolume()
|
||||
{
|
||||
auto s = obs_weak_source_get_source(audioSource);
|
||||
auto s = obs_weak_source_get_source(_audioSource);
|
||||
if (!s) {
|
||||
return;
|
||||
}
|
||||
float vol = (float)_volume / 100.0f;
|
||||
float curVol = obs_source_get_volume(s);
|
||||
obs_source_release(s);
|
||||
bool volIncrease = curVol <= vol;
|
||||
int nrSteps = duration.seconds * 1000 / fadeInterval.count();
|
||||
int nrSteps = _duration.seconds * 1000 / fadeInterval.count();
|
||||
float volDiff = (volIncrease) ? vol - curVol : curVol - vol;
|
||||
float volStep = volDiff / nrSteps;
|
||||
|
||||
if (volStep < minFade) {
|
||||
switcher->activeAudioFades[GetWeakSourceName(audioSource)] =
|
||||
switcher->activeAudioFades[GetWeakSourceName(_audioSource)] =
|
||||
false;
|
||||
return;
|
||||
}
|
||||
|
||||
for (int step = 0; step < nrSteps && !switcher->stop; ++step) {
|
||||
auto s = obs_weak_source_get_source(audioSource);
|
||||
auto macro = GetMacro();
|
||||
for (int step = 0; step < nrSteps && !macro->GetStop(); ++step) {
|
||||
auto s = obs_weak_source_get_source(_audioSource);
|
||||
if (!s) {
|
||||
return;
|
||||
}
|
||||
|
|
@ -51,13 +60,14 @@ void fadeSourceVolume(Duration duration, float vol, OBSWeakSource audioSource)
|
|||
obs_source_release(s);
|
||||
}
|
||||
|
||||
switcher->activeAudioFades[GetWeakSourceName(audioSource)] = false;
|
||||
switcher->activeAudioFades[GetWeakSourceName(_audioSource)] = false;
|
||||
}
|
||||
void fadeMasterVolume(Duration duration, float vol)
|
||||
void MacroActionAudio::FadeMasterVolume()
|
||||
{
|
||||
float vol = (float)_volume / 100.0f;
|
||||
float curVol = obs_get_master_volume();
|
||||
bool volIncrease = curVol <= vol;
|
||||
int nrSteps = duration.seconds * 1000 / fadeInterval.count();
|
||||
int nrSteps = _duration.seconds * 1000 / fadeInterval.count();
|
||||
float volDiff = (volIncrease) ? vol - curVol : curVol - vol;
|
||||
float volStep = volDiff / nrSteps;
|
||||
|
||||
|
|
@ -66,7 +76,8 @@ void fadeMasterVolume(Duration duration, float vol)
|
|||
return;
|
||||
}
|
||||
|
||||
for (int step = 0; step < nrSteps && !switcher->stop; ++step) {
|
||||
auto macro = GetMacro();
|
||||
for (int step = 0; step < nrSteps && !macro->GetStop(); ++step) {
|
||||
curVol = (volIncrease) ? curVol + volStep : curVol - volStep;
|
||||
obs_set_master_volume(curVol);
|
||||
std::this_thread::sleep_for(fadeInterval);
|
||||
|
|
@ -75,25 +86,33 @@ void fadeMasterVolume(Duration duration, float vol)
|
|||
switcher->masterAudioFadeActive = false;
|
||||
}
|
||||
|
||||
void startSourceFade(Duration &duration, float vol, OBSWeakSource audioSource)
|
||||
void MacroActionAudio::StartSourceFade()
|
||||
{
|
||||
if (!audioSource) {
|
||||
if (!_audioSource) {
|
||||
return;
|
||||
}
|
||||
auto it =
|
||||
switcher->activeAudioFades.find(GetWeakSourceName(audioSource));
|
||||
auto it = switcher->activeAudioFades.find(
|
||||
GetWeakSourceName(_audioSource));
|
||||
if (it != switcher->activeAudioFades.end() && it->second == true) {
|
||||
blog(LOG_WARNING,
|
||||
"Audio fade for volume of %s already active! New fade request will be ignored!",
|
||||
GetWeakSourceName(audioSource).c_str());
|
||||
GetWeakSourceName(_audioSource).c_str());
|
||||
return;
|
||||
}
|
||||
switcher->activeAudioFades[GetWeakSourceName(audioSource)] = true;
|
||||
switcher->audioHelperThreads.emplace_back(fadeSourceVolume, duration,
|
||||
vol, audioSource);
|
||||
switcher->activeAudioFades[GetWeakSourceName(_audioSource)] = true;
|
||||
if (_wait) {
|
||||
FadeSourceVolume();
|
||||
} else {
|
||||
if (_fadeThread.joinable()) {
|
||||
return;
|
||||
}
|
||||
_fadeThread =
|
||||
std::thread(&MacroActionAudio::FadeSourceVolume, this);
|
||||
_fadeThread.detach();
|
||||
}
|
||||
}
|
||||
|
||||
void startMasterFade(Duration &duration, float vol)
|
||||
void MacroActionAudio::StartMasterFade()
|
||||
{
|
||||
|
||||
if (switcher->masterAudioFadeActive) {
|
||||
|
|
@ -102,8 +121,16 @@ void startMasterFade(Duration &duration, float vol)
|
|||
return;
|
||||
}
|
||||
switcher->masterAudioFadeActive = true;
|
||||
switcher->audioHelperThreads.emplace_back(fadeMasterVolume, duration,
|
||||
vol);
|
||||
if (_wait) {
|
||||
FadeMasterVolume();
|
||||
} else {
|
||||
if (_fadeThread.joinable()) {
|
||||
return;
|
||||
}
|
||||
_fadeThread =
|
||||
std::thread(&MacroActionAudio::FadeMasterVolume, this);
|
||||
_fadeThread.detach();
|
||||
}
|
||||
}
|
||||
|
||||
bool MacroActionAudio::PerformAction()
|
||||
|
|
@ -118,15 +145,14 @@ bool MacroActionAudio::PerformAction()
|
|||
break;
|
||||
case AudioAction::SOURCE_VOLUME:
|
||||
if (_fade && _duration.seconds != 0) {
|
||||
startSourceFade(_duration, (float)_volume / 100.0f,
|
||||
_audioSource);
|
||||
StartSourceFade();
|
||||
} else {
|
||||
obs_source_set_volume(s, (float)_volume / 100.0f);
|
||||
}
|
||||
break;
|
||||
case AudioAction::MASTER_VOLUME:
|
||||
if (_fade && _duration.seconds != 0) {
|
||||
startMasterFade(_duration, (float)_volume / 100.0f);
|
||||
StartMasterFade();
|
||||
} else {
|
||||
obs_set_master_volume((float)_volume / 100.0f);
|
||||
}
|
||||
|
|
@ -162,6 +188,7 @@ bool MacroActionAudio::Save(obs_data_t *obj)
|
|||
obs_data_set_int(obj, "action", static_cast<int>(_action));
|
||||
obs_data_set_int(obj, "volume", _volume);
|
||||
obs_data_set_bool(obj, "fade", _fade);
|
||||
obs_data_set_bool(obj, "wait", _wait);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
@ -174,6 +201,11 @@ bool MacroActionAudio::Load(obs_data_t *obj)
|
|||
_action = static_cast<AudioAction>(obs_data_get_int(obj, "action"));
|
||||
_volume = obs_data_get_int(obj, "volume");
|
||||
_fade = obs_data_get_bool(obj, "fade");
|
||||
if (obs_data_has_user_value(obj, "wait")) {
|
||||
_wait = obs_data_get_bool(obj, "wait");
|
||||
} else {
|
||||
_wait = false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
@ -203,6 +235,7 @@ MacroActionAudioEdit::MacroActionAudioEdit(
|
|||
_volumePercent->setMaximum(2000);
|
||||
_volumePercent->setSuffix("%");
|
||||
_fade = new QCheckBox();
|
||||
_wait = new QCheckBox();
|
||||
_duration = new DurationSelection(parent, false);
|
||||
|
||||
populateActionSelection(_actions);
|
||||
|
|
@ -219,11 +252,13 @@ MacroActionAudioEdit::MacroActionAudioEdit(
|
|||
SLOT(FadeChanged(int)));
|
||||
QWidget::connect(_duration, SIGNAL(DurationChanged(double)), this,
|
||||
SLOT(DurationChanged(double)));
|
||||
QWidget::connect(_wait, SIGNAL(stateChanged(int)), this,
|
||||
SLOT(WaitChanged(int)));
|
||||
|
||||
std::unordered_map<std::string, QWidget *> widgetPlaceholders = {
|
||||
{"{{audioSources}}", _audioSources}, {"{{actions}}", _actions},
|
||||
{"{{volume}}", _volumePercent}, {"{{fade}}", _fade},
|
||||
{"{{duration}}", _duration},
|
||||
{"{{duration}}", _duration}, {"{{wait}}", _wait},
|
||||
};
|
||||
QHBoxLayout *entryLayout = new QHBoxLayout;
|
||||
placeWidgets(obs_module_text("AdvSceneSwitcher.action.audio.entry"),
|
||||
|
|
@ -272,6 +307,7 @@ void MacroActionAudioEdit::UpdateEntryData()
|
|||
_volumePercent->setValue(_entryData->_volume);
|
||||
_fade->setChecked(_entryData->_fade);
|
||||
_duration->SetDuration(_entryData->_duration);
|
||||
_wait->setChecked(_entryData->_wait);
|
||||
SetWidgetVisibility();
|
||||
}
|
||||
|
||||
|
|
@ -327,3 +363,13 @@ void MacroActionAudioEdit::DurationChanged(double seconds)
|
|||
std::lock_guard<std::mutex> lock(switcher->m);
|
||||
_entryData->_duration.seconds = seconds;
|
||||
}
|
||||
|
||||
void MacroActionAudioEdit::WaitChanged(int value)
|
||||
{
|
||||
if (_loading || !_entryData) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> lock(switcher->m);
|
||||
_entryData->_wait = value;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,6 +30,7 @@ Macro::Macro(const std::string &name)
|
|||
Macro::~Macro()
|
||||
{
|
||||
_die = true;
|
||||
_stop = true;
|
||||
if (_thread.joinable()) {
|
||||
_thread.join();
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user