Skip to content

Commit

Permalink
feat(tonality): ✨ add isTheoretical getter
Browse files Browse the repository at this point in the history
  • Loading branch information
albertms10 committed Jan 20, 2024
1 parent 7b3acd4 commit c76bbcb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
18 changes: 17 additions & 1 deletion lib/src/tonality/tonality.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
part of '../../music_notes.dart';

/// A musical tonality.
/// A musical tonality, also known as key.
///
/// See [Key (music)](https://en.wikipedia.org/wiki/Key_(music)).
///
/// ---
/// See also:
/// * [Note].
/// * [Mode].
/// * [KeySignature].
@immutable
final class Tonality implements Comparable<Tonality> {
/// The tonal center representing this [Tonality].
Expand Down Expand Up @@ -56,6 +59,19 @@ final class Tonality implements Comparable<Tonality> {
KeySignature.empty.tonality(mode)!.note.fifthsDistanceWith(note),
);

/// Whether this [Tonality] is theoretical, whose [keySignature] would have
/// at least one [Accidental.doubleFlat] or [Accidental.doubleSharp].
///
/// See [Theoretical key](https://en.wikipedia.org/wiki/Theoretical_key).
///
/// Example:
/// ```dart
/// Note.e.major.isTheoretical == false
/// Note.g.sharp.major.isTheoretical == true
/// Note.f.flat.minor.isTheoretical == true
/// ```
bool get isTheoretical => (keySignature.distance?.abs() ?? 0) > 7;

/// Returns the scale notes of this [Tonality].
///
/// Example:
Expand Down
18 changes: 18 additions & 0 deletions test/src/tonality/tonality_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,24 @@ void main() {
});
});

group('.isTheoretical', () {
test('returns whether this Tonality is theoretical', () {
expect(Note.c.flat.major.isTheoretical, false);
expect(Note.a.flat.minor.isTheoretical, false);
expect(Note.c.minor.isTheoretical, false);
expect(Note.e.major.isTheoretical, false);
expect(Note.c.sharp.major.isTheoretical, false);
expect(Note.a.sharp.minor.isTheoretical, false);

expect(Note.f.flat.major.isTheoretical, true);
expect(Note.d.flat.minor.isTheoretical, true);
expect(Note.c.sharp.sharp.minor.isTheoretical, true);
expect(Note.a.flat.flat.major.isTheoretical, true);
expect(Note.g.sharp.major.isTheoretical, true);
expect(Note.e.sharp.minor.isTheoretical, true);
});
});

group('.scale', () {
test('returns the scale notes of this Tonality', () {
expect(
Expand Down

0 comments on commit c76bbcb

Please sign in to comment.