Skip to content

Commit

Permalink
delete unnecessary getposition function
Browse files Browse the repository at this point in the history
  • Loading branch information
anokta committed Feb 18, 2024
1 parent 7d5f004 commit 3f37dff
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 37 deletions.
5 changes: 0 additions & 5 deletions barelymusician/composition/duration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@

namespace barely {

double GetPosition(int step, int step_count) noexcept {
assert(step_count > 0);
return static_cast<double>(step) / static_cast<double>(step_count);
}

double QuantizePosition(double position, double resolution, double amount) noexcept {
assert(resolution > 0.0);
assert(amount >= 0.0 && amount <= 1.0);
Expand Down
21 changes: 7 additions & 14 deletions barelymusician/composition/duration.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,13 @@
namespace barely {

/// Common note values in relation to quarter note beat duration.
inline constexpr int kQuarterNotesPerBeat = 1;
inline constexpr int kEighthNotesPerBeat = 2;
inline constexpr int kEighthTripletNotesPerBeat = 3;
inline constexpr int kSixteenthNotesPerBeat = 4;
inline constexpr int kSixteenthTripletNotesPerBeat = 6;
inline constexpr int kThirtySecondNotesPerBeat = 8;
inline constexpr int kThirtySecondTripletNotesPerBeat = 12;

/// Returns quantized position for a given number of beat steps.
///
/// @param step Quantized step.
/// @param step_count Number of steps per beat.
/// @return Quantized position in beats.
double GetPosition(int step, int step_count) noexcept;
inline constexpr double kQuarterNotesPerBeat = 1.0;
inline constexpr double kEighthNotesPerBeat = 2.0;
inline constexpr double kEighthTripletNotesPerBeat = 3.0;
inline constexpr double kSixteenthNotesPerBeat = 4.0;
inline constexpr double kSixteenthTripletNotesPerBeat = 6.0;
inline constexpr double kThirtySecondNotesPerBeat = 8.0;
inline constexpr double kThirtySecondTripletNotesPerBeat = 12.0;

/// Returns quantized position.
///
Expand Down
20 changes: 3 additions & 17 deletions barelymusician/composition/duration_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,17 @@
namespace barely {
namespace {

class PositionTest : public testing::TestWithParam<int> {};

// Tests that expected positions are returned with respect to the given steps.
TEST_P(PositionTest, GetPosition) {
constexpr int kBeatCount = 4;
const int step_count = GetParam();

for (int beat = 0; beat < kBeatCount; ++beat) {
for (int i = 0; i < step_count; ++i) {
const double expected_position =
static_cast<double>(beat) + static_cast<double>(i) / static_cast<double>(step_count);
EXPECT_DOUBLE_EQ(GetPosition(step_count * beat + i, step_count), expected_position);
}
}
}
class DurationTestWithParam : public testing::TestWithParam<double> {};

// Tests that the position gets quantized as expected with respect to the given resolution.
TEST_P(PositionTest, QuantizePosition) {
TEST_P(DurationTestWithParam, QuantizePosition) {
constexpr double kPosition = 0.99;
const double resolution = 1.0 / static_cast<double>(GetParam());
EXPECT_DOUBLE_EQ(QuantizePosition(kPosition, resolution), 1.0);
EXPECT_DOUBLE_EQ(QuantizePosition(1.0 - kPosition, resolution), 0.0);
}

INSTANTIATE_TEST_SUITE_P(NoteDurationTest, PositionTest,
INSTANTIATE_TEST_SUITE_P(DurationTest, DurationTestWithParam,
testing::Values(kQuarterNotesPerBeat, kEighthNotesPerBeat,
kEighthTripletNotesPerBeat, kSixteenthNotesPerBeat,
kSixteenthTripletNotesPerBeat, kThirtySecondNotesPerBeat,
Expand Down
2 changes: 1 addition & 1 deletion examples/demo/musician_demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ void ComposeLine(double octave_offset, double intensity, int bar, int beat, int
void ComposeDrums(int bar, int beat, int beat_count, Random& random, Instrument& instrument,
Performer& performer) {
const auto get_beat = [](int step) {
return barely::GetPosition(step, barely::kSixteenthNotesPerBeat);
return static_cast<double>(step) / barely::kSixteenthNotesPerBeat;
};
const auto add_note = [&](double begin_position, double end_position, double pitch,
double intensity) {
Expand Down

0 comments on commit 3f37dff

Please sign in to comment.