Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start linear ramps at the initial volume set #1318

Merged
merged 2 commits into from
Jun 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/flowgraph/RampLinear.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ void RampLinear::setLengthInFrames(int32_t frames) {

void RampLinear::setTarget(float target) {
mTarget.store(target);
// If the ramp has not been used then start immediately at this level.
if (mLastCallCount == kInitialCallCount) {
forceCurrent(target);
}
}

float RampLinear::interpolateCurrent() {
Expand Down
23 changes: 16 additions & 7 deletions tests/testFlowgraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,31 +80,40 @@ TEST(test_flowgraph, module_mono_to_stereo) {
}

TEST(test_flowgraph, module_ramp_linear) {
constexpr int singleNumOutput = 1;
constexpr int rampSize = 5;
constexpr int numOutput = 100;
constexpr float value = 1.0f;
constexpr float target = 100.0f;
constexpr float initialTarget = 10.0f;
constexpr float finalTarget = 100.0f;
constexpr float tolerance = 0.0001f; // arbitrary
float output[numOutput] = {};
RampLinear rampLinear{1};
SinkFloat sinkFloat{1};

rampLinear.input.setValue(value);
rampLinear.setLengthInFrames(rampSize);
rampLinear.setTarget(target);
rampLinear.forceCurrent(0.0f);

rampLinear.output.connect(&sinkFloat.input);

// Check that the values go to the initial target instantly.
rampLinear.setTarget(initialTarget);
int32_t singleNumRead = sinkFloat.read(output, singleNumOutput);
ASSERT_EQ(singleNumRead, singleNumOutput);
EXPECT_NEAR(value * initialTarget, output[0], tolerance);

// Now set target and check that the linear ramp works as expected.
rampLinear.setTarget(finalTarget);
int32_t numRead = sinkFloat.read(output, numOutput);
const float incrementSize = (finalTarget - initialTarget) / rampSize;
ASSERT_EQ(numOutput, numRead);
constexpr float tolerance = 0.0001f; // arbitrary

int i = 0;
for (; i < rampSize; i++) {
float expected = i * value * target / rampSize;
float expected = value * (initialTarget + i * incrementSize);
EXPECT_NEAR(expected, output[i], tolerance);
}
for (; i < numOutput; i++) {
float expected = value * target;
float expected = value * finalTarget;
EXPECT_NEAR(expected, output[i], tolerance);
}
}
Expand Down