Skip to content

Commit

Permalink
Combine frames and scratch_rate into one optional because they are al…
Browse files Browse the repository at this point in the history
…ways valid.
  • Loading branch information
daschuer committed Sep 13, 2024
1 parent 26d3029 commit 22b3103
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 33 deletions.
4 changes: 2 additions & 2 deletions src/effects/backends/builtin/autopaneffect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ void AutoPanEffect::processChannel(
double period = m_pPeriodParameter->value();
const auto smoothing = static_cast<float>(0.5 - m_pSmoothingParameter->value());

if (groupFeatures.beat_length_frames.has_value()) {
if (groupFeatures.beat_length.has_value()) {
// period is a number of beats
double beats = std::max(roundToFraction(period, 2), 0.25);
period = beats * *groupFeatures.beat_length_frames;
period = beats * groupFeatures.beat_length->frames;

// TODO(xxx) sync phase
//if (groupFeatures.has_beat_fraction) {
Expand Down
4 changes: 2 additions & 2 deletions src/effects/backends/builtin/echoeffect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ void EchoEffect::processChannel(
const auto pingpong_frac = static_cast<CSAMPLE_GAIN>(m_pPingPongParameter->value());

int delay_frames;
if (groupFeatures.beat_length_frames.has_value()) {
if (groupFeatures.beat_length.has_value()) {
// period is a number of beats
if (m_pQuantizeParameter->toBool()) {
period = std::max(roundToFraction(period, 4), 1 / 8.0);
Expand All @@ -142,7 +142,7 @@ void EchoEffect::processChannel(
} else if (period < 1 / 8.0) {
period = 1 / 8.0;
}
delay_frames = static_cast<int>(period * *groupFeatures.beat_length_frames);
delay_frames = static_cast<int>(period * groupFeatures.beat_length->frames);
} else {
// period is a number of seconds
period = std::max(period, 1 / 8.0);
Expand Down
4 changes: 2 additions & 2 deletions src/effects/backends/builtin/flangereffect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,13 @@ void FlangerEffect::processChannel(
const GroupFeatureState& groupFeatures) {
double lfoPeriodParameter = m_pSpeedParameter->value();
double lfoPeriodFrames;
if (groupFeatures.beat_length_frames.has_value()) {
if (groupFeatures.beat_length.has_value()) {
// lfoPeriodParameter is a number of beats
lfoPeriodParameter = std::max(roundToFraction(lfoPeriodParameter, 2.0), kMinLfoBeats);
if (m_pTripletParameter->toBool()) {
lfoPeriodParameter /= 3.0;
}
lfoPeriodFrames = lfoPeriodParameter * *groupFeatures.beat_length_frames;
lfoPeriodFrames = lfoPeriodParameter * groupFeatures.beat_length->frames;
} else {
// lfoPeriodParameter is a number of seconds
lfoPeriodFrames = std::max(lfoPeriodParameter, kMinLfoBeats) *
Expand Down
20 changes: 7 additions & 13 deletions src/effects/backends/builtin/metronomeeffect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,11 @@ void MetronomeEffect::processChannel(
double bufferEnd = gs->m_framesSinceClickStart + engineParameters.framesPerBuffer();

double nextClickStart = bufferEnd; // default to "no new click";
if (m_pSyncParameter->toBool() && groupFeatures.beat_length_frames.has_value()) {
if (groupFeatures.beat_fraction_buffer_end.has_value()) {
double beatLength = *groupFeatures.beat_length_frames;
if (groupFeatures.scratch_rate.has_value()) {
if (*groupFeatures.scratch_rate != 0.0) {
beatLength /= *groupFeatures.scratch_rate;
} else {
// no transport, nothing to do.
return;
}
}

if (m_pSyncParameter->toBool() && groupFeatures.beat_fraction_buffer_end.has_value()) {
// Sync enabled and have a track with beats
if (groupFeatures.beat_length.has_value() && groupFeatures.beat_length->scratch_rate) {

Check failure on line 99 in src/effects/backends/builtin/metronomeeffect.cpp

View workflow job for this annotation

GitHub Actions / clazy

implicit conversion turns floating-point number into integer: 'const double' to 'bool' [-Werror,-Wfloat-conversion]

Check failure on line 99 in src/effects/backends/builtin/metronomeeffect.cpp

View workflow job for this annotation

GitHub Actions / clang-tidy

implicit conversion turns floating-point number into integer: 'const double' to 'bool' [clang-diagnostic-float-conversion]
double beatLength = groupFeatures.beat_length->frames /
groupFeatures.beat_length->scratch_rate;
double beatToBufferEnd;
if (beatLength > 0) {
beatToBufferEnd =
Expand All @@ -122,7 +115,8 @@ void MetronomeEffect::processChannel(
nextClickStart = bufferEnd - beatToBufferEnd;
}
} else {
nextClickStart = *groupFeatures.beat_length_frames;
// no transport, nothing to do.
return;
}
} else {
nextClickStart = engineParameters.sampleRate() * 60 / m_pBpmParameter->value();
Expand Down
4 changes: 2 additions & 2 deletions src/effects/backends/builtin/phasereffect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,13 @@ void PhaserEffect::processChannel(

double periodParameter = m_pLFOPeriodParameter->value();
double periodSamples;
if (groupFeatures.beat_length_frames.has_value()) {
if (groupFeatures.beat_length.has_value()) {
// periodParameter is a number of beats
periodParameter = std::max(roundToFraction(periodParameter, 2.0), 1 / 4.0);
if (m_pTripletParameter->toBool()) {
periodParameter /= 3.0;
}
periodSamples = periodParameter * *groupFeatures.beat_length_frames;
periodSamples = periodParameter * groupFeatures.beat_length->frames;
} else {
// periodParameter is a number of seconds
periodSamples = std::max(periodParameter, 1 / 4.0) * engineParameters.sampleRate();
Expand Down
8 changes: 4 additions & 4 deletions src/effects/backends/builtin/tremoloeffect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ void TremoloEffect::processChannel(
bool tripletDisabling = pState->tripletEnabled && !m_pTripletParameter->toBool();

if (enableState == EffectEnableState::Enabling || quantizeEnabling || tripletDisabling) {
if (gf.beat_length_frames.has_value() && gf.beat_fraction_buffer_end.has_value()) {
if (gf.beat_length.has_value() && gf.beat_fraction_buffer_end.has_value()) {
currentFrame = static_cast<unsigned int>(*gf.beat_fraction_buffer_end *
*gf.beat_length_frames);
gf.beat_length->frames);
} else {
currentFrame = 0;
}
Expand All @@ -149,7 +149,7 @@ void TremoloEffect::processChannel(

int framePerPeriod;
double rate = m_pRateParameter->value();
if (gf.beat_length_frames.has_value() && gf.beat_fraction_buffer_end.has_value()) {
if (gf.beat_length.has_value() && gf.beat_fraction_buffer_end.has_value()) {
if (m_pQuantizeParameter->toBool()) {
const auto divider = static_cast<int>(log2(rate));
rate = pow(2, divider);
Expand All @@ -158,7 +158,7 @@ void TremoloEffect::processChannel(
rate *= 3.0;
}
}
const auto framePerBeat = static_cast<int>(*gf.beat_length_frames);
const auto framePerBeat = static_cast<int>(gf.beat_length->frames);
framePerPeriod = static_cast<int>(framePerBeat / rate);
} else {
framePerPeriod = static_cast<int>(engineParameters.sampleRate() / rate);
Expand Down
3 changes: 1 addition & 2 deletions src/engine/controls/bpmcontrol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1108,8 +1108,7 @@ void BpmControl::collectFeatures(GroupFeatureState* pGroupFeatures, double speed
&beatFraction)) {
const double rateRatio = m_pRateRatio->get();
if (rateRatio != 0.0) {
pGroupFeatures->beat_length_frames = beatLengthFrames / rateRatio;
pGroupFeatures->scratch_rate = speed / rateRatio;
pGroupFeatures->beat_length = {beatLengthFrames / rateRatio, speed / rateRatio};
}
pGroupFeatures->beat_fraction_buffer_end = beatFraction;
}
Expand Down
15 changes: 9 additions & 6 deletions src/engine/effects/groupfeaturestate.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@

#include <optional>

struct GroupFeatureBeatLength {
// Beat length adjusted by the rate slider
double frames;
// Rate change by temporary actions like scratching
// and not the rate slider.
double scratch_rate;
};

/// GroupFeatureState communicates metadata about EngineChannels to EffectProcessors.
struct GroupFeatureState {
GroupFeatureState() = default;

// Adjusted by the rate slider
std::optional<double> beat_length_frames;

// Rate change by temporary actions like scratching
// and not the rate slider.
std::optional<double> scratch_rate;
std::optional<GroupFeatureBeatLength> beat_length;

// Beat fraction (0.0 to 1.0) of the position at the buffer end.
// Previous beat is in the current or earlier buffer. The next beat
Expand Down

0 comments on commit 22b3103

Please sign in to comment.