intervalInSemitones static method
Calculates the interval in semitones between two pitches.
Pitch.midiNumber already rounds the alteration into the note number, so only the microtonal remainder of each pitch is added on top of the MIDI difference (adding the full alteration again would double-count it).
Implementation
static double intervalInSemitones(Pitch pitch1, Pitch pitch2) {
final fraction1 = pitch1.effectiveAlter - pitch1.effectiveAlter.round();
final fraction2 = pitch2.effectiveAlter - pitch2.effectiveAlter.round();
return (pitch2.midiNumber - pitch1.midiNumber).toDouble() +
fraction2 -
fraction1;
}