Skip to content

Commit

Permalink
feat(scale): allow passing EnharmonicNote to fromNote
Browse files Browse the repository at this point in the history
  • Loading branch information
albertms10 committed May 7, 2023
1 parent 6ec881a commit 31ec64b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 13 deletions.
7 changes: 4 additions & 3 deletions lib/src/scale/scale.dart
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ class Scale {
Interval.minorSecond,
]);

/// Returns the scale of notes starting from [note].
/// Returns the scale of notes starting from [transposable].
///
/// Example:
/// ```dart
Expand All @@ -199,8 +199,9 @@ class Scale {
/// == const [Note.c, Note.d, Note.eFlat, Note.f, Note.g, Note.a, Note.b,
/// Note.c]
/// ```
List<Note> fromNote(Note note) => intervalSteps.fold(
[note],
List<Transposable<T>> fromNote<T>(Transposable<T> transposable) =>
intervalSteps.fold(
[transposable],
(scaleNotes, interval) =>
[...scaleNotes, scaleNotes.last.transposeBy(interval)],
);
Expand Down
2 changes: 1 addition & 1 deletion lib/src/tonality/tonality.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class Tonality implements Comparable<Tonality> {
/// == const [Note.e, Note.fSharp, Note.g, Note.a, Note.b, Note.d, Note.d,
/// Note.e]
/// ```
List<Note> get scaleNotes => mode.scale.fromNote(note);
List<Transposable<Note>> get scaleNotes => mode.scale.fromNote(note);

@override
String toString() => '$note ${mode.name}';
Expand Down
2 changes: 1 addition & 1 deletion lib/src/transposable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ part of '../music_notes.dart';
// ignore: one_member_abstracts
abstract class Transposable<T> {
/// Returns a transposed [T] by [interval] from this [T].
T transposeBy(Interval interval);
Transposable<T> transposeBy(Interval interval);
}
45 changes: 37 additions & 8 deletions test/src/scale/scale_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,15 @@ void main() {

test('should return the whole-tone scale notes starting from Note', () {
expect(
Scale.wholeTone.fromNote(Note.c),
Scale.wholeTone.fromNote(EnharmonicNote.c),
const [
Note.c,
Note.d,
Note.e,
Note.fSharp,
Note.gSharp,
Note.aSharp,
Note.c,
EnharmonicNote.c,
EnharmonicNote.d,
EnharmonicNote.e,
EnharmonicNote.fSharp,
EnharmonicNote.gSharp,
EnharmonicNote.aSharp,
EnharmonicNote.c,
],
);
expect(
Expand Down Expand Up @@ -213,6 +213,24 @@ void main() {
Note.c,
],
);
expect(
Scale.chromatic.fromNote(EnharmonicNote.cSharp),
const [
EnharmonicNote.cSharp,
EnharmonicNote.d,
EnharmonicNote.dSharp,
EnharmonicNote.e,
EnharmonicNote.f,
EnharmonicNote.fSharp,
EnharmonicNote.g,
EnharmonicNote.gSharp,
EnharmonicNote.a,
EnharmonicNote.aSharp,
EnharmonicNote.b,
EnharmonicNote.c,
EnharmonicNote.cSharp,
],
);
expect(
Scale.chromatic.fromNote(Note.dFlat),
const [
Expand Down Expand Up @@ -251,6 +269,17 @@ void main() {
Note.fSharp,
],
);
expect(
Scale.majorPentatonic.fromNote(EnharmonicNote.fSharp),
const [
EnharmonicNote.fSharp,
EnharmonicNote.gSharp,
EnharmonicNote.aSharp,
EnharmonicNote.cSharp,
EnharmonicNote.dSharp,
EnharmonicNote.fSharp,
],
);
},
);

Expand Down

0 comments on commit 31ec64b

Please sign in to comment.