Skip to content

Commit

Permalink
feat(note): add circleOfFifthsDistance getter (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
albertms10 committed May 8, 2023
1 parent 7f011a5 commit a26265f
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/src/note/note.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@ class Note implements MusicItem, Transposable<Note> {
PositionedNote inOctave(int octave) =>
PositionedNote(note, accidental, octave);

/// Returns the distance in relation to the circle of fifths.
///
/// Example:
/// ```dart
/// Note.c.circleOfFifthsDistance == 0
/// Note.d.circleOfFifthsDistance == 2
/// Note.aFlat.circleOfFifthsDistance == -4
/// ```
int get circleOfFifthsDistance =>
Tonality(this, TonalMode.major).keySignature.distance;

/// Returns the exact fifths distance between this and [other].
///
/// Example:
Expand Down
10 changes: 10 additions & 0 deletions lib/src/tonality/key_signature.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ class KeySignature implements Comparable<KeySignature> {
: Accidental.sharp,
);

/// Returns the fifths distance of this [KeySignature].
///
/// Example:
/// ```dart
/// const KeySignature(0).distance == 0
/// const KeySignature(3, Accidental.sharp).distance == 3
/// const KeySignature(4, Accidental.flat).distance == -4
/// ```
int get distance => accidentals * (accidental == Accidental.flat ? -1 : 1);

/// Returns the [Note] that corresponds to the major [Tonality] of this
/// [KeySignature].
///
Expand Down
20 changes: 20 additions & 0 deletions test/src/note/note_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,26 @@ void main() {
);
});

group('.circleOfFifthsDistance', () {
test('should return the circle of fifths distance of this Note', () {
expect(const Note(Notes.c, Accidental.flat).circleOfFifthsDistance, -7);
expect(Note.gFlat.circleOfFifthsDistance, -6);
expect(Note.dFlat.circleOfFifthsDistance, -5);
expect(Note.aFlat.circleOfFifthsDistance, -4);
expect(Note.eFlat.circleOfFifthsDistance, -3);
expect(Note.bFlat.circleOfFifthsDistance, -2);
expect(Note.f.circleOfFifthsDistance, -1);
expect(Note.c.circleOfFifthsDistance, 0);
expect(Note.g.circleOfFifthsDistance, 1);
expect(Note.d.circleOfFifthsDistance, 2);
expect(Note.a.circleOfFifthsDistance, 3);
expect(Note.e.circleOfFifthsDistance, 4);
expect(Note.b.circleOfFifthsDistance, 5);
expect(Note.fSharp.circleOfFifthsDistance, 6);
expect(Note.cSharp.circleOfFifthsDistance, 7);
});
});

group('.exactFifthsDistance()', () {
test(
'should return the fifths distance between this and other Note',
Expand Down
10 changes: 10 additions & 0 deletions test/src/tonality/key_signature_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ void main() {
});
});

group('.distance', () {
test('should return the fifths distance of this KeySignature', () {
expect(const KeySignature(7, Accidental.flat).distance, -7);
expect(const KeySignature(2, Accidental.flat).distance, -2);
expect(const KeySignature(0).distance, 0);
expect(const KeySignature(1, Accidental.sharp).distance, 1);
expect(const KeySignature(5, Accidental.sharp).distance, 5);
});
});

group('.majorNote', () {
test(
'should return the Note that corresponds to the major Tonality of '
Expand Down

0 comments on commit a26265f

Please sign in to comment.