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(base_notes): rename NotesBaseNote #121

Merged
merged 3 commits into from
May 14, 2023
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
2 changes: 1 addition & 1 deletion lib/music_notes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ part 'src/interval/interval_size_extension.dart';
part 'src/interval/quality.dart';
part 'src/music.dart';
part 'src/note/accidental.dart';
part 'src/note/base_note.dart';
part 'src/note/enharmonic_note.dart';
part 'src/note/frequency.dart';
part 'src/note/note.dart';
part 'src/note/notes.dart';
part 'src/note/positioned_note.dart';
part 'src/scale/scale.dart';
part 'src/tonality/key_signature.dart';
Expand Down
114 changes: 114 additions & 0 deletions lib/src/note/base_note.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
part of '../../music_notes.dart';

enum BaseNote {
c(1),
d(3),
e(5),
f(6),
g(8),
a(10),
b(12);

final int value;

const BaseNote(this.value);

/// Returns a [BaseNote] enum item that matches with [value]
/// as in [BaseNote], otherwise returns `null`.
///
/// Example:
/// ```dart
/// BaseNote.fromValue(3) == BaseNote.d
/// BaseNote.fromValue(8) == BaseNote.g
/// BaseNote.fromValue(11) == null
/// ```
static BaseNote? fromValue(int value) => values.firstWhereOrNull(
(note) => value.chromaticModExcludeZero == note.value,
);

/// Returns a [BaseNote] enum item that matches with [ordinal].
///
/// Example:
/// ```dart
/// BaseNote.fromOrdinal(3) == BaseNote.e
/// BaseNote.fromOrdinal(7) == BaseNote.b
/// BaseNote.fromOrdinal(10) == BaseNote.e
/// ```
static BaseNote fromOrdinal(int ordinal) =>
BaseNote.values[ordinal.nModExcludeZero(BaseNote.values.length) - 1];

/// Returns the ordinal number of this [BaseNote] enum item.
///
/// Example:
/// ```dart
/// BaseNote.c.ordinal == 1
/// BaseNote.f.ordinal == 4
/// BaseNote.b.ordinal == 7
/// ```
int get ordinal => BaseNote.values.indexOf(this) + 1;

/// Returns the [Interval.size] that conforms between this [BaseNote] enum
/// item and [other].
///
/// Example:
/// ```dart
/// BaseNote.d.intervalSize(BaseNote.f) == 3
/// BaseNote.a.intervalSize(BaseNote.e) == 5
/// BaseNote.a.intervalSize(BaseNote.e, descending: true) == 4
/// ```
int intervalSize(BaseNote other, {bool descending = false}) {
var otherOrdinal = other.ordinal;
if (descending && ordinal < otherOrdinal) {
otherOrdinal -= values.length;
} else if (!descending && ordinal > otherOrdinal) {
otherOrdinal += values.length;
}

return ((otherOrdinal - ordinal) * (descending ? -1 : 1)) + 1;
}

/// Returns the difference in semitones between this [BaseNote] enum item and
/// [other].
///
/// Example:
/// ```dart
/// BaseNote.c.difference(BaseNote.c) == 0
/// BaseNote.c.difference(BaseNote.e) == 4
/// BaseNote.a.difference(BaseNote.d) == -7
/// ```
int difference(BaseNote other) => other.value - value;

/// Returns the positive difference in semitones between this [BaseNote] enum
/// item and [other].
///
/// When [difference] would return a negative value, this method returns the
/// difference with [other] being in the next octave.
///
/// Example:
/// ```dart
/// BaseNote.c.positiveDifference(BaseNote.c) == 0
/// BaseNote.c.positiveDifference(BaseNote.e) == 4
/// BaseNote.a.positiveDifference(BaseNote.d) == 5
/// ```
int positiveDifference(BaseNote other) {
final differenceWithOther = difference(other);

return differenceWithOther.isNegative
? differenceWithOther + chromaticDivisions
: differenceWithOther;
}

/// Returns this [BaseNote] enum item transposed by interval [size].
///
/// Example:
/// ```dart
/// BaseNote.g.transposeBy(1) == BaseNote.g
/// BaseNote.g.transposeBy(5) == BaseNote.d
/// BaseNote.a.transposeBy(-3) == BaseNote.f
/// ```
BaseNote transposeBy(int size) {
assert(size != 0, 'Size must be non-zero');

return fromOrdinal(ordinal + (size.abs() - 1) * size.sign);
}
}
28 changes: 12 additions & 16 deletions lib/src/note/enharmonic_note.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,38 +23,34 @@ final class EnharmonicNote extends Enharmonic<Note>

@override
Set<Note> get spellings {
final note = Notes.fromValue(semitones);
final baseNote = BaseNote.fromValue(semitones);

if (note != null) {
final noteBelow = Notes.fromOrdinal(note.ordinal - 1);
final noteAbove = Notes.fromOrdinal(note.ordinal + 1);
if (baseNote != null) {
final noteBelow = BaseNote.fromOrdinal(baseNote.ordinal - 1);
final noteAbove = BaseNote.fromOrdinal(baseNote.ordinal + 1);

return SplayTreeSet<Note>.of({
Note(
noteBelow,
Accidental((note.value - noteBelow.value).chromaticModExcludeZero),
Accidental(
(baseNote.value - noteBelow.value).chromaticModExcludeZero,
),
),
Note(note),
Note(baseNote),
Note(
noteAbove,
Accidental(
note.value -
baseNote.value -
noteAbove.value -
(note.value > noteAbove.value ? chromaticDivisions : 0),
(baseNote.value > noteAbove.value ? chromaticDivisions : 0),
),
),
});
}

return SplayTreeSet<Note>.of({
Note(
Notes.fromValue(semitones - 1)!,
Accidental.sharp,
),
Note(
Notes.fromValue(semitones + 1)!,
Accidental.flat,
),
Note(BaseNote.fromValue(semitones - 1)!, Accidental.sharp),
Note(BaseNote.fromValue(semitones + 1)!, Accidental.flat),
});
}

Expand Down
66 changes: 34 additions & 32 deletions lib/src/note/note.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@ part of '../../music_notes.dart';

@immutable
final class Note implements Comparable<Note>, Transposable<Note> {
final Notes note;
final BaseNote baseNote;
final Accidental accidental;

const Note(this.note, [this.accidental = Accidental.natural]);

static const Note c = Note(Notes.c);
static const Note cSharp = Note(Notes.c, Accidental.sharp);
static const Note dFlat = Note(Notes.d, Accidental.flat);
static const Note d = Note(Notes.d);
static const Note dSharp = Note(Notes.d, Accidental.sharp);
static const Note eFlat = Note(Notes.e, Accidental.flat);
static const Note e = Note(Notes.e);
static const Note f = Note(Notes.f);
static const Note fSharp = Note(Notes.f, Accidental.sharp);
static const Note gFlat = Note(Notes.g, Accidental.flat);
static const Note g = Note(Notes.g);
static const Note gSharp = Note(Notes.g, Accidental.sharp);
static const Note aFlat = Note(Notes.a, Accidental.flat);
static const Note a = Note(Notes.a);
static const Note aSharp = Note(Notes.a, Accidental.sharp);
static const Note bFlat = Note(Notes.b, Accidental.flat);
static const Note b = Note(Notes.b);
const Note(this.baseNote, [this.accidental = Accidental.natural]);

static const Note c = Note(BaseNote.c);
static const Note cSharp = Note(BaseNote.c, Accidental.sharp);
static const Note dFlat = Note(BaseNote.d, Accidental.flat);
static const Note d = Note(BaseNote.d);
static const Note dSharp = Note(BaseNote.d, Accidental.sharp);
static const Note eFlat = Note(BaseNote.e, Accidental.flat);
static const Note e = Note(BaseNote.e);
static const Note f = Note(BaseNote.f);
static const Note fSharp = Note(BaseNote.f, Accidental.sharp);
static const Note gFlat = Note(BaseNote.g, Accidental.flat);
static const Note g = Note(BaseNote.g);
static const Note gSharp = Note(BaseNote.g, Accidental.sharp);
static const Note aFlat = Note(BaseNote.a, Accidental.flat);
static const Note a = Note(BaseNote.a);
static const Note aSharp = Note(BaseNote.a, Accidental.sharp);
static const Note bFlat = Note(BaseNote.b, Accidental.flat);
static const Note b = Note(BaseNote.b);

/// Returns the [Note] from the [Tonality] given its [accidentals] number,
/// [mode] and optional [accidental].
Expand All @@ -50,15 +50,15 @@ final class Note implements Comparable<Note>, Transposable<Note> {
a.circleOfFifthsDistance.compareTo(b.circleOfFifthsDistance);

/// Returns the number of semitones that correspond to this [Note]
/// from [Notes.c].
/// from [BaseNote.c].
///
/// Example:
/// ```dart
/// Note.d.semitones == 3
/// Note.fSharp.semitones == 7
/// ```
int get semitones =>
(note.value + accidental.semitones).chromaticModExcludeZero;
(baseNote.value + accidental.semitones).chromaticModExcludeZero;

/// Returns the difference in semitones between this [Note] and [other].
///
Expand All @@ -75,9 +75,9 @@ final class Note implements Comparable<Note>, Transposable<Note> {
/// Example:
/// ```dart
/// Note.c.inOctave(3)
/// == const PositionedNote(Notes.c, Accidental.natural, 3);
/// == const PositionedNote(BaseNote.c, Accidental.natural, 3);
/// Note.aFlat.inOctave(2)
/// == const PositionedNote(Notes.a, Accidental.flat, 2);
/// == const PositionedNote(BaseNote.a, Accidental.flat, 2);
/// ```
PositionedNote inOctave(int octave) => PositionedNote(this, octave);

Expand Down Expand Up @@ -159,7 +159,7 @@ final class Note implements Comparable<Note>, Transposable<Note> {
/// Note.d.exactInterval(Note.aFlat) == Interval.diminishedFifth
/// ```
Interval exactInterval(Note other) {
final intervalSize = note.intervalSize(other.note);
final intervalSize = baseNote.intervalSize(other.baseNote);

return Interval.fromDelta(
intervalSize,
Expand All @@ -176,10 +176,10 @@ final class Note implements Comparable<Note>, Transposable<Note> {
/// ```
@override
Note transposeBy(Interval interval) {
final transposedNote = note.transposeBy(interval.size);
final transposedNote = baseNote.transposeBy(interval.size);
final positiveDifference = interval.isDescending
? transposedNote.positiveDifference(note)
: note.positiveDifference(transposedNote);
? transposedNote.positiveDifference(baseNote)
: baseNote.positiveDifference(transposedNote);

return Note(
transposedNote,
Expand All @@ -194,19 +194,21 @@ final class Note implements Comparable<Note>, Transposable<Note> {

@override
String toString() =>
note.name.toUpperCase() +
baseNote.name.toUpperCase() +
(accidental != Accidental.natural ? accidental.symbol : '');

@override
bool operator ==(Object other) =>
other is Note && note == other.note && accidental == other.accidental;
other is Note &&
baseNote == other.baseNote &&
accidental == other.accidental;

@override
int get hashCode => Object.hash(note, accidental);
int get hashCode => Object.hash(baseNote, accidental);

@override
int compareTo(Note other) => compareMultiple([
() => semitones.compareTo(other.semitones),
() => note.value.compareTo(other.note.value),
() => baseNote.value.compareTo(other.baseNote.value),
]);
}
Loading