Skip to content

Commit

Permalink
✨ feat(pitch_class): move modulo operation to the constructor (#246)
Browse files Browse the repository at this point in the history
  • Loading branch information
albertms10 committed Aug 5, 2023
1 parent 71d8ab5 commit e3d788f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 16 deletions.
11 changes: 3 additions & 8 deletions lib/src/note/pitch_class.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ part of '../../music_notes.dart';
final class PitchClass extends Enharmonic<Note>
implements Scalable<PitchClass> {
/// Creates a new [PitchClass] from [semitones].
const PitchClass(super.semitones)
: assert(
semitones >= 0 && semitones < chromaticDivisions,
'Semitones must be in chromatic divisions range',
);
const PitchClass(int semitones) : super(semitones % chromaticDivisions);

/// Pitch class 0, which corresponds to [Note.c].
static const c = PitchClass(0);
Expand Down Expand Up @@ -149,7 +145,7 @@ final class PitchClass extends Enharmonic<Note>
/// ```
@override
PitchClass transposeBy(Interval interval) =>
PitchClass((semitones + interval.semitones).chromaticMod);
PitchClass(semitones + interval.semitones);

/// Returns the [Interval] between this [PitchClass] and [other].
///
Expand Down Expand Up @@ -206,6 +202,5 @@ final class PitchClass extends Enharmonic<Note>
/// ```
///
/// See [Pitch-class multiplication modulo 12](https://en.wikipedia.org/wiki/Multiplication_(music)#Pitch-class_multiplication_modulo_12).
PitchClass operator *(int factor) =>
PitchClass((semitones * factor).chromaticMod);
PitchClass operator *(int factor) => PitchClass(semitones * factor);
}
15 changes: 7 additions & 8 deletions test/src/note/pitch_class_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ import 'package:test/test.dart';
void main() {
group('PitchClass', () {
group('constructor', () {
test(
'should throw an assertion error when arguments are incorrect',
() {
expect(() => PitchClass(-2), throwsA(isA<AssertionError>()));
expect(() => PitchClass(13), throwsA(isA<AssertionError>()));
},
);
test('should create a new PitchClass from semitones', () {
// ignore: prefer_const_constructors
expect(PitchClass(-2), PitchClass.aSharp);
// ignore: prefer_const_constructors
expect(PitchClass(13), PitchClass.cSharp);
});
});

group('.spellings()', () {
Expand Down Expand Up @@ -320,7 +319,7 @@ void main() {
});

group('.interval()', () {
test('should return the Interval between this Note and other', () {
test('should return the Interval between this PitchClass and other', () {
expect(PitchClass.c.interval(PitchClass.c), Interval.P1);
expect(PitchClass.c.interval(PitchClass.cSharp), Interval.m2);

Expand Down

0 comments on commit e3d788f

Please sign in to comment.