Skip to content

Commit

Permalink
fix(pitch): 🐛 address isEnharmonicWith not taking octave into acc…
Browse files Browse the repository at this point in the history
…ount

Signed-off-by: Albert Mañosa <26429103+albertms10@users.noreply.github.com>
  • Loading branch information
albertms10 committed Mar 31, 2024
1 parent 34d5ad7 commit b674f7a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/src/note/pitch.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:meta/meta.dart' show immutable;
import 'package:music_notes/utils.dart';

import '../enharmonic.dart';
import '../harmony/chord.dart';
import '../harmony/chord_pattern.dart';
import '../interval/interval.dart';
Expand Down Expand Up @@ -332,9 +333,13 @@ final class Pitch extends Scalable<Pitch> implements Comparable<Pitch> {
/// calculating the octave height, as it depends on the note name.
/// This correctly handles cases with the same number of semitones
/// but in different octaves (e.g., B♯3 but C4, or C♭4 but B3).
int _semitonesWithoutAccidental(int semitones, Note referenceNote) =>
static int _semitonesWithoutAccidental(int semitones, Note referenceNote) =>
semitones - referenceNote.accidental.semitones;

@override
bool isEnharmonicWith(Enharmonic<PitchClass> other) =>
semitones == other.semitones;

/// Transposes this [Pitch] by [interval].
///
/// Example:
Expand Down
37 changes: 37 additions & 0 deletions test/src/note/pitch_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,43 @@ void main() {
});
});

group('.toClass()', () {
test('creates a new PitchClass from semitones', () {
expect(Note.a.inOctave(4).toClass(), PitchClass.a);
expect(Note.a.sharp.inOctave(2).toClass(), PitchClass.aSharp);
expect(Note.e.flat.inOctave(-1).toClass(), PitchClass.dSharp);
expect(Note.b.sharp.sharp.inOctave(7).toClass(), PitchClass.cSharp);
});
});

group('.isEnharmonicWith()', () {
test(
'returns whether this Pitch is enharmonically equivalent to other',
() {
expect(
Note.e.inOctave(4).isEnharmonicWith(Note.e.inOctave(4)),
isTrue,
);
expect(
Note.a.sharp.inOctave(4).isEnharmonicWith(Note.b.flat.inOctave(4)),
isTrue,
);
expect(
Note.a.inOctave(2).isEnharmonicWith(Note.b.flat.inOctave(4)),
isFalse,
);
expect(
Note.e.flat.inOctave(-1).isEnharmonicWith(Note.e.flat.inOctave(3)),
isFalse,
);
expect(
Note.b.sharp.inOctave(3).isEnharmonicWith(Note.c.inOctave(4)),
isTrue,
);
},
);
});

group('.frequency()', () {
test('returns the Frequency of this Pitch at 440 Hz', () {
expect(
Expand Down

0 comments on commit b674f7a

Please sign in to comment.