Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(scalable): ♻️ extract isEnharmonicWith extension method #430

Merged
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
50 changes: 50 additions & 0 deletions lib/src/class_mixin.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import 'package:collection/collection.dart' show IterableEquality;

/// A Class mixin.
mixin ClassMixin<Class> {
/// The number of semitones that define this [Class].
int get semitones;

/// Creates a new [Class] from [semitones].
Class toClass();

/// Whether [Class] is enharmonically equivalent to [other].
///
/// See [Enharmonic equivalence](https://en.wikipedia.org/wiki/Enharmonic_equivalence).
///
/// Example:
/// ```dart
/// Note.g.sharp.isEnharmonicWith(Note.a.flat) == true
/// Note.c.isEnharmonicWith(Note.b.sharp) == true
/// Note.e.isEnharmonicWith(Note.f) == false
/// ```
bool isEnharmonicWith(ClassMixin<Class> other) =>
toClass() == other.toClass();
}

/// A Class iterable.
extension ClassIterable<Class> on Iterable<ClassMixin<Class>> {
/// The [Class] representation of this [Iterable].
Iterable<Class> toClass() => map((interval) => interval.toClass());

/// Whether this [Iterable] is enharmonically equivalent to [other].
///
/// See [Enharmonic equivalence](https://en.wikipedia.org/wiki/Enharmonic_equivalence).
///
/// Example:
/// ```dart
/// [Note.c.sharp, Note.f, Note.a.flat]
/// .isEnharmonicWith([Note.d.flat, Note.e.sharp, Note.g.sharp])
/// == true
///
/// [Note.d.sharp].isEnharmonicWith([Note.a.flat]) == false
///
/// const [Interval.m2, Interval.m3, Interval.M2]
/// .isEnharmonicWith(const [Interval.m2, Interval.A2, Interval.d3])
/// == true
///
/// const [Interval.m2].isEnharmonicWith(const [Interval.P4]) == false
/// ```
bool isEnharmonicWith(Iterable<ClassMixin<Class>> other) =>
IterableEquality<Class>().equals(toClass(), other.toClass());
}
7 changes: 6 additions & 1 deletion lib/src/interval/interval.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import 'package:meta/meta.dart' show immutable;
import 'package:music_notes/utils.dart';

import '../class_mixin.dart';
import '../note/note.dart';
import '../scalable.dart';
import 'interval_class.dart';
Expand All @@ -16,7 +17,9 @@ import 'size.dart';
/// * [Quality].
/// * [IntervalClass].
@immutable
final class Interval implements Comparable<Interval> {
final class Interval
with ClassMixin<IntervalClass>
implements Comparable<Interval> {
/// Number of lines and spaces (or alphabet letters) spanning the two notes,
/// including the beginning and end.
final Size size;
Expand Down Expand Up @@ -221,6 +224,7 @@ final class Interval implements Comparable<Interval> {
/// Interval.A4.semitones == 6
/// (-Interval.M3).semitones == -4
/// ```
@override
int get semitones => (size.semitones.abs() + quality.semitones) * size.sign;

/// Whether this [Interval] is descending.
Expand Down Expand Up @@ -391,6 +395,7 @@ final class Interval implements Comparable<Interval> {
/// Interval.d4.toClass() == IntervalClass.M3
/// Interval.P8.toClass() == IntervalClass.P1
/// ```
@override
IntervalClass toClass() => IntervalClass(semitones);

/// The string representation of this [Interval] based on [system].
Expand Down
10 changes: 0 additions & 10 deletions lib/src/note/note.dart
Original file line number Diff line number Diff line change
Expand Up @@ -278,16 +278,6 @@ final class Note extends Scalable<Note> implements Comparable<Note> {
respellByAccidental(Accidental.natural) ??
respellByAccidental(Accidental(accidental.semitones.sign))!;

/// Whether this [Note] is enharmonically equivalent to [other].
///
/// Example:
/// ```dart
/// Note.g.sharp.isEnharmonicWith(Note.a.flat) == true
/// Note.c.isEnharmonicWith(Note.b.sharp) == true
/// Note.e.isEnharmonicWith(Note.f) == false
/// ```
bool isEnharmonicWith(Note other) => toPitchClass() == other.toPitchClass();

/// This [Note] positioned in the given [octave] as a [Pitch].
///
/// Example:
Expand Down
19 changes: 5 additions & 14 deletions lib/src/scalable.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import 'class_mixin.dart';
import 'interval/interval.dart';
import 'interval/interval_class.dart';
import 'music.dart';
import 'note/pitch_class.dart';
import 'transposable.dart';

/// A interface for items that can form scales.
abstract class Scalable<T extends Scalable<T>> implements Transposable<T> {
abstract class Scalable<T extends Scalable<T>>
with ClassMixin<PitchClass>
implements Transposable<T> {
/// Creates a new [Scalable].
const Scalable();

/// The number of semitones that define this [Scalable].
int get semitones;

/// Creates a new [PitchClass] from [semitones].
///
/// Example:
Expand All @@ -20,6 +19,7 @@ abstract class Scalable<T extends Scalable<T>> implements Transposable<T> {
/// Note.e.sharp.inOctave(2).toClass() == PitchClass.f
/// Note.c.flat.flat.inOctave(5).toClass() == PitchClass.aSharp
/// ```
@override
PitchClass toClass() => PitchClass(semitones);

/// The [Interval] between this [Scalable] and [other].
Expand Down Expand Up @@ -51,9 +51,6 @@ extension ScalableIterable<T extends Scalable<T>> on Iterable<T> {
}
}

/// The [PitchClass] representation of this [ScalableIterable].
Iterable<PitchClass> toClass() => map((scalable) => scalable.toClass());

/// Transposes this [Iterable] by [interval].
Iterable<T> transposeBy(Interval interval) =>
map((item) => item.transposeBy(interval));
Expand Down Expand Up @@ -109,9 +106,3 @@ extension ScalableIterable<T extends Scalable<T>> on Iterable<T> {
}
}
}

/// An Interval iterable.
extension IntervalIterable<T extends Interval> on Iterable<T> {
/// The [PitchClass] representation of this [IntervalIterable].
Iterable<IntervalClass> toClass() => map((interval) => interval.toClass());
}
15 changes: 7 additions & 8 deletions lib/src/scale/scale.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import 'package:collection/collection.dart'
show IterableEquality, ListEquality, UnmodifiableListView;
show ListEquality, UnmodifiableListView;
import 'package:meta/meta.dart' show immutable;

import '../class_mixin.dart';
import '../harmony/chord.dart';
import '../harmony/harmonic_function.dart';
import '../interval/interval.dart';
import '../interval/quality.dart';
import '../interval/size.dart';
import '../note/pitch_class.dart';
import '../scalable.dart';
import '../transposable.dart';
import 'scale_degree.dart';
Expand Down Expand Up @@ -148,19 +148,18 @@ class Scale<T extends Scalable<T>> implements Transposable<Scale<T>> {

/// Whether this [Scale] is enharmonically equivalent to [other].
///
/// See [Enharmonic equivalence](https://en.wikipedia.org/wiki/Enharmonic_equivalence).
///
/// Example:
/// ```dart
/// const Scale([Note.c, Note.d, Note.f, Note.g])
/// .isEnharmonicWith(Scale([Note.b.sharp, Note.d, Note.e.sharp, Note.g]))
/// == true
/// ```
bool isEnharmonicWith(Scale<T> other) =>
const IterableEquality<PitchClass>()
.equals(_degrees.toClass(), other._degrees.toClass()) &&
const IterableEquality<PitchClass>().equals(
(_descendingDegrees ?? const []).toClass(),
(other._descendingDegrees ?? const []).toClass(),
);
_degrees.isEnharmonicWith(other._degrees) &&
(_descendingDegrees ?? const [])
.isEnharmonicWith(other._descendingDegrees ?? const []);

/// Transposes this [Scale] by [interval].
///
Expand Down
17 changes: 8 additions & 9 deletions lib/src/scale/scale_pattern.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import 'package:collection/collection.dart'
show IterableEquality, UnmodifiableListView;
import 'package:collection/collection.dart' show UnmodifiableListView;
import 'package:meta/meta.dart' show immutable;

import '../class_mixin.dart';
import '../harmony/chord_pattern.dart';
import '../interval/interval.dart';
import '../interval/interval_class.dart';
import '../scalable.dart';
import 'scale.dart';
import 'scale_degree.dart';
Expand Down Expand Up @@ -353,7 +352,9 @@ final class ScalePattern {
Interval _addNextStepTo(int ordinal) =>
_stepFrom(ordinal) + _stepFrom(ordinal + 1);

/// Whether this [Scale] is enharmonically equivalent to [other].
/// Whether this [ScalePattern] is enharmonically equivalent to [other].
///
/// See [Enharmonic equivalence](https://en.wikipedia.org/wiki/Enharmonic_equivalence).
///
/// Example:
/// ```dart
Expand All @@ -362,11 +363,9 @@ final class ScalePattern {
/// == true
/// ```
bool isEnharmonicWith(ScalePattern other) =>
const IterableEquality<IntervalClass>()
.equals(_intervalSteps.toClass(), other._intervalSteps.toClass()) &&
const IterableEquality<IntervalClass>().equals(
(_descendingIntervalSteps ?? const []).toClass(),
(other._descendingIntervalSteps ?? const []).toClass(),
_intervalSteps.isEnharmonicWith(other._intervalSteps) &&
(_descendingIntervalSteps ?? const []).isEnharmonicWith(
other._descendingIntervalSteps ?? const [],
);

/// The name associated with this [ScalePattern].
Expand Down