Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
* Use OBSSourceAutoRelease
* Do not return true if source selection is invalid
  • Loading branch information
WarmUpTill committed Dec 17, 2023
1 parent 4bdebb7 commit 267855f
Showing 1 changed file with 28 additions and 31 deletions.
59 changes: 28 additions & 31 deletions src/macro-core/macro-condition-audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,17 @@ MacroConditionAudio::~MacroConditionAudio()
bool MacroConditionAudio::CheckOutputCondition()
{
bool ret = false;
auto s = obs_weak_source_get_source(_audioSource.GetSource());
OBSSourceAutoRelease source =
obs_weak_source_get_source(_audioSource.GetSource());

// peak will have a value from -60 db to 0 db so we need to scale it
double curVolume = ((double)_peak + 60) * 1.7;

switch (_outputCondition) {
case OutputCondition::ABOVE:
// peak will have a value from -60 db to 0 db
ret = curVolume > _volume;
break;
case OutputCondition::BELOW:
// peak will have a value from -60 db to 0 db
ret = curVolume < _volume;
break;
default:
Expand All @@ -84,21 +84,21 @@ bool MacroConditionAudio::CheckOutputCondition()

// Reset for next check
_peak = -std::numeric_limits<float>::infinity();
obs_source_release(s);
if (_audioSource.GetType() == SourceSelection::Type::VARIABLE) {
ResetVolmeter();
}

return ret;
return ret && source;
}

bool MacroConditionAudio::CheckVolumeCondition()
{
bool ret = false;
auto s = obs_weak_source_get_source(_audioSource.GetSource());
OBSSourceAutoRelease source =
obs_weak_source_get_source(_audioSource.GetSource());

float curVolume = obs_source_get_volume(s);
bool muted = obs_source_muted(s);
float curVolume = obs_source_get_volume(source);
bool muted = obs_source_muted(source);

switch (_volumeCondition) {
case VolumeCondition::ABOVE:
Expand Down Expand Up @@ -126,10 +126,9 @@ bool MacroConditionAudio::CheckVolumeCondition()
}

SetTempVarValue("configured_volume", std::to_string(curVolume));
SetTempVarValue("muted", muted ? "true" : "false");
SetTempVarValue("muted", (source && muted) ? "true" : "false");

obs_source_release(s);
return ret;
return ret && source;
}

bool MacroConditionAudio::CheckSyncOffset()
Expand All @@ -139,17 +138,17 @@ bool MacroConditionAudio::CheckSyncOffset()
}

bool ret = false;
auto s = obs_weak_source_get_source(_audioSource.GetSource());
auto curOffset = obs_source_get_sync_offset(s) / nsPerMs;
OBSSourceAutoRelease source =
obs_weak_source_get_source(_audioSource.GetSource());
auto curOffset = obs_source_get_sync_offset(source) / nsPerMs;
if (_outputCondition == OutputCondition::ABOVE) {
ret = curOffset > _syncOffset;
} else {
ret = curOffset < _syncOffset;
}
SetVariableValue(std::to_string(curOffset));
SetTempVarValue("sync_offset", std::to_string(curOffset));
obs_source_release(s);
return ret;
return ret && source;
}

bool MacroConditionAudio::CheckMonitor()
Expand All @@ -159,13 +158,13 @@ bool MacroConditionAudio::CheckMonitor()
}

bool ret = false;
auto s = obs_weak_source_get_source(_audioSource.GetSource());
ret = obs_source_get_monitoring_type(s) == _monitorType;
OBSSourceAutoRelease source =
obs_weak_source_get_source(_audioSource.GetSource());
ret = obs_source_get_monitoring_type(source) == _monitorType;
SetVariableValue("");
SetTempVarValue("monitor",
std::to_string(obs_source_get_monitoring_type(s)));
obs_source_release(s);
return ret;
std::to_string(obs_source_get_monitoring_type(source)));
return ret && source;
}

bool MacroConditionAudio::CheckBalance()
Expand All @@ -175,17 +174,17 @@ bool MacroConditionAudio::CheckBalance()
}

bool ret = false;
auto s = obs_weak_source_get_source(_audioSource.GetSource());
auto curBalance = obs_source_get_balance_value(s);
OBSSourceAutoRelease source =
obs_weak_source_get_source(_audioSource.GetSource());
auto curBalance = obs_source_get_balance_value(source);
if (_outputCondition == OutputCondition::ABOVE) {
ret = curBalance > _balance;
} else {
ret = curBalance < _balance;
}
SetVariableValue(std::to_string(curBalance));
SetTempVarValue("balance", std::to_string(curBalance));
obs_source_release(s);
return ret;
return ret && source;
}

bool MacroConditionAudio::CheckCondition()
Expand Down Expand Up @@ -239,13 +238,12 @@ obs_volmeter_t *AddVolmeterToSource(MacroConditionAudio *entry,
obs_volmeter_t *volmeter = obs_volmeter_create(OBS_FADER_LOG);
obs_volmeter_add_callback(volmeter, MacroConditionAudio::SetVolumeLevel,
entry);
obs_source_t *as = obs_weak_source_get_source(source);
if (!obs_volmeter_attach_source(volmeter, as)) {
const char *name = obs_source_get_name(as);
OBSSourceAutoRelease audioSource = obs_weak_source_get_source(source);
if (!obs_volmeter_attach_source(volmeter, audioSource)) {
const char *name = obs_source_get_name(audioSource);
blog(LOG_WARNING, "failed to attach volmeter to source %s",
name);
}
obs_source_release(as);

return volmeter;
}
Expand Down Expand Up @@ -455,10 +453,9 @@ MacroConditionAudioEdit::MacroConditionAudioEdit(
void MacroConditionAudioEdit::UpdateVolmeterSource()
{
delete _volMeter;
obs_source_t *soruce = obs_weak_source_get_source(
OBSSourceAutoRelease soruce = obs_weak_source_get_source(
_entryData->_audioSource.GetSource());
_volMeter = new VolControl(soruce);
obs_source_release(soruce);
_volMeter = new VolControl(soruce.Get());

QLayout *layout = this->layout();
layout->addWidget(_volMeter);
Expand Down

0 comments on commit 267855f

Please sign in to comment.