Skip to content
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
11 changes: 9 additions & 2 deletions Semantics.Music/Scale.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ public ScaleDegree DegreeOf(PitchClass pitchClass)
}

// Chromatic: spell against the lower neighbour (raised/sharp) or the upper
// neighbour (lowered/flat), whichever is closer; tie prefers the flat.
// neighbour (lowered/flat), whichever is closer; tie prefers the flat, except
// above the last degree, where a flat would wrap to a lowered tonic and the
// raised last degree (the minor-key leading tone, #7) is the conventional reading.
int lowerIndex = -1;
for (int i = 0; i < offsets.Count; i++)
{
Expand All @@ -98,7 +100,12 @@ public ScaleDegree DegreeOf(PitchClass pitchClass)
int flatDegree = upperIndex >= 0 ? upperIndex + 1 : 1;
int flatAlteration = semitone - (upperIndex >= 0 ? offsets[upperIndex] : offsets[0] + 12);

return Math.Abs(flatAlteration) <= Math.Abs(sharpAlteration)
bool flatWraps = upperIndex < 0;
bool preferFlat = flatWraps
? Math.Abs(flatAlteration) < Math.Abs(sharpAlteration)
: Math.Abs(flatAlteration) <= Math.Abs(sharpAlteration);

return preferFlat
? new ScaleDegree(flatDegree, flatAlteration)
: new ScaleDegree(sharpDegree, sharpAlteration);
}
Expand Down
17 changes: 17 additions & 0 deletions Semantics.Test/Music/KeyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,23 @@ public void RomanNumeral_ChromaticGetsAccidental()
Assert.AreEqual("bII", CMajor.RomanNumeralOf(Chord.Parse("Db")));
}

[TestMethod]
public void RomanNumeral_MinorLeadingToneChordIsSharpSeven()
{
Key aMinor = Key.Parse("A aeolian");
Assert.AreEqual("#vii°", aMinor.RomanNumeralOf(Chord.Parse("G#dim")));
Assert.AreEqual("#vii°", Key.Parse("G mixolydian").RomanNumeralOf(Chord.Parse("F#dim")));
}

[TestMethod]
public void RomanNumeral_MinorLeadingToneChordRoundTrips()
{
Key aMinor = Key.Parse("A aeolian");
Chord leadingTone = Chord.Parse("G#dim");
Chord roundTripped = aMinor.ChordFromRomanNumeral(aMinor.RomanNumeralOf(leadingTone));
Assert.AreEqual(leadingTone, roundTripped);
}

[TestMethod]
public void RomanNumeral_DistinctDegreesNeverShareALabel()
{
Expand Down
11 changes: 11 additions & 0 deletions Semantics.Test/Music/ScaleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ public void DegreeOf_SharpFourIsCloserToFourthRaised()
Assert.AreEqual(-1, tritone.Alteration);
}

[TestMethod]
public void DegreeOf_RaisedSeventhAboveAFlatSeventhIsSharpSeven()
{
// G# in A aeolian sits between the flat seventh (G) and the octave; it is the
// leading tone (#7), not a lowered tonic wrapped round the octave.
Scale aMinor = Scale.Create(PitchClass.Parse("A"), Mode.Aeolian);
ScaleDegree leadingTone = aMinor.DegreeOf(PitchClass.Parse("G#"));
Assert.AreEqual(7, leadingTone.Degree);
Assert.AreEqual(1, leadingTone.Alteration);
}

[TestMethod]
public void ToStringIsRootSpaceMode()
{
Expand Down
Loading