Skip to content

Commit

Permalink
Extract pitchDifftoKeyWheelSteps as a function
Browse files Browse the repository at this point in the history
  • Loading branch information
danferns committed Aug 12, 2024
1 parent bf6fc2c commit 253c237
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
14 changes: 5 additions & 9 deletions src/track/keyutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -737,23 +737,19 @@ double KeyUtils::trackSimilarity(mixxx::track::io::key::ChromaticKey key1,

const int roundedPitchDiff = normalizePitch(round(pitchDiff));
double cents = abs(roundedPitchDiff - pitchDiff);
// bring cents value back to [0-0.5] for pitchDiff values > 11.50
if (cents > 11) {
cents = 12 - cents;
}

// Open Key number also conveniently gives us the index in the Keywheel
// Keys use 1-based indexing (0 is INVALID)
const int okNum = keyToOpenKeyNumber(keyFromNumericValue(roundedPitchDiff + 1));
// rank: distance between tracks on the Circle of Fifths,
// when played at the same tempo.
const int rank = std::min(okNum, 12 - okNum);
const int keyWheelSteps = pitchDiffToKeywheelSteps(roundedPitchDiff);

// normalize rank between 0-1 by dividing by 7 (no. of rank values, including 0)
// normalize between 0-1 by dividing by 7 (no. of steps values, including 0)
// invert so 1.0 means most compatible
const double normRank = (1 - rank / 7);
const double normKWSteps = (1 - keyWheelSteps / 7);
// this 1-16x^4 curve forgives the effect of lower detune values on the similarity
const double normCents = (1 - 16 * pow(cents, 4)); // 0-1

// calculate final similarity and return it
return normRank * normCents;
return normKWSteps * normCents;
}
9 changes: 9 additions & 0 deletions src/track/keyutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ class KeyUtils {
return openKeyNumberToKey(keyToOpenKeyNumber(key), true);
}

// Given pitch difference of 2 keys, returns their distance on the keywheel
static inline int pitchDiffToKeywheelSteps(int pitchDiff) {
// Open Key number also conveniently gives us the clockwise index in the Keywheel
// Keys use 1-based indexing (0 is INVALID)
const int CWSteps = keyToOpenKeyNumber(keyFromNumericValue(pitchDiff + 1));
// it's a wheel, so check if counter-clockwise direction has fewer steps
return std::min(CWSteps, 12 - CWSteps);
}

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

Expand Down

0 comments on commit 253c237

Please sign in to comment.