Skip to content

Commit

Permalink
add shortestPitchDiff function
Browse files Browse the repository at this point in the history
  • Loading branch information
danferns committed Sep 21, 2024
1 parent ba1080e commit c5f7c94
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
14 changes: 14 additions & 0 deletions src/test/keyutilstest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,17 @@ TEST_F(KeyUtilsTest, GetCompatibleKeys) {
mixxx::track::io::key::A_MINOR,
mixxx::track::io::key::G_MINOR));
}

TEST_F(KeyUtilsTest, ShortestPitchDiff) {
// test no pitch difference in octaves
double diff = KeyUtils::shortestPitchDiff(12.0, 24.0);
EXPECT_EQ(0.0, diff);
// test for symmetry
diff = KeyUtils::shortestPitchDiff(6.0, 19.0);
EXPECT_EQ(1.0, diff);
diff = KeyUtils::shortestPitchDiff(19.0, 6.0);
EXPECT_EQ(1.0, diff);
// test that the difference is wrapped around boundaries
diff = KeyUtils::shortestPitchDiff(1.0, 23.0);
EXPECT_EQ(2.0, diff);
}
6 changes: 3 additions & 3 deletions src/track/keyutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -716,11 +716,11 @@ double KeyUtils::trackSyncPitchDifference(
const double delta2 = powerOf2ToSemitoneChange(100 / bpm2.value());

// get the resulting key for each track at 100BPM
const double resPitch1 = normalizePitch(keyToNumericValue(key1) - 1 + delta1);
const double resPitch2 = normalizePitch(keyToNumericValue(key2) - 1 + delta2);
const double resPitch1 = keyToNumericValue(key1) - 1 + delta1;
const double resPitch2 = keyToNumericValue(key2) - 1 + delta2;

// return the pitch difference when both tracks are played at the same tempo
return normalizePitch(resPitch1 - resPitch2);
return shortestPitchDiff(resPitch1, resPitch2);
}

// Calculates Similarity (0 to 1, -1 if invalid) between two tracks based on their key and BPM.
Expand Down
6 changes: 6 additions & 0 deletions src/track/keyutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ class KeyUtils {
return std::min(CWSteps, 12 - CWSteps);
}

// returns the shortest pitch difference up or down, handling octaves
static inline double shortestPitchDiff(double pitch1, double pitch2) {
const double normPitchDiff = normalizePitch(pitch1 - pitch2);
return std::min(normPitchDiff, 12.0 - normPitchDiff);
}

static QString keyToString(mixxx::track::io::key::ChromaticKey key,
KeyNotation notation = KeyNotation::Custom);

Expand Down

0 comments on commit c5f7c94

Please # to comment.