Skip to content

Commit

Permalink
✨ feat(interval): add respellBySize method (#195)
Browse files Browse the repository at this point in the history
  • Loading branch information
albertms10 committed Jun 21, 2023
1 parent df80288 commit ecc33fe
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/src/interval/interval.dart
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,19 @@ final class Interval implements Comparable<Interval> {
/// ```
Interval get simplified => Interval._(size.simplified, quality);

/// Returns this [Interval] respelled by [size] while keeping the same
/// number of [semitones].
///
/// Example:
/// ```dart
/// Interval.A4.respellBySize(5) == Interval.d5
/// Interval.d3.respellBySize(2) == Interval.M2
/// ```
Interval respellBySize(int size) => Interval._(
size,
Quality.fromInterval(size, semitones.abs() - size.semitones.abs()),
);

/// Returns the iteration distance of this [Interval] between [note1] and
/// [note2].
///
Expand Down
21 changes: 21 additions & 0 deletions test/src/interval/interval_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,27 @@ void main() {
});
});

group('.respellBySize()', () {
test('should return this Interval respelled by size', () {
expect(Interval.A4.respellBySize(5), Interval.d5);
expect(Interval.d5.respellBySize(4), Interval.A4);
expect(Interval.M2.respellBySize(3), Interval.d3);
expect(
Interval.M3.respellBySize(5),
const Interval.perfect(5, PerfectQuality.triplyDiminished),
);
expect(Interval.P1.respellBySize(2), Interval.d2);
expect(Interval.m2.respellBySize(1), Interval.A1);

expect((-Interval.M3).respellBySize(-4), -Interval.d4);
expect((-Interval.d5).respellBySize(-4), -Interval.A4);
expect(
(-Interval.P4).respellBySize(-5),
const Interval.perfect(-5, PerfectQuality.doublyDiminished),
);
});
});

group('.circleFrom()', () {
test('should return the circle of this Interval', () {
expect(Interval.P5.circleFrom(Note.c, distance: 0), const [Note.c]);
Expand Down

0 comments on commit ecc33fe

Please sign in to comment.