Skip to content

Commit

Permalink
Merge f0272a6 into f87d063
Browse files Browse the repository at this point in the history
  • Loading branch information
albertms10 committed Apr 22, 2023
2 parents f87d063 + f0272a6 commit ef58cca
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/music_notes.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
library music_notes;

import 'dart:collection' show SplayTreeSet;
import 'dart:math' as math show min;
import 'dart:math' as math show min, pow;

import 'package:collection/collection.dart' show IterableExtension;
import 'package:meta/meta.dart';
Expand Down
2 changes: 2 additions & 0 deletions lib/src/music.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ part of '../music_notes.dart';
/// Number of chromatic divisions in an octave.
const int chromaticDivisions = 12;

final double sqrt12_2 = math.pow(2, 1 / 12).toDouble();

/// [Set] of [EnharmonicNote]s that form the chromatic scale.
final Set<EnharmonicNote> chromaticScale = SplayTreeSet<EnharmonicNote>.of({
for (var i = 1; i <= chromaticDivisions; i++) EnharmonicNote(i),
Expand Down
14 changes: 14 additions & 0 deletions lib/src/note/note.dart
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,20 @@ class Note implements MusicItem {
);
}

/// Returns the frequency in Hertzs of this [Note] from the A4 reference.
///
/// Example:
/// ```dart
/// Note.a.equalTemperamentFrequency() == 440
/// Note.gSharp.equalTemperamentFrequency() == 415.3
/// Note.c.equalTemperamentFrequency() == 261.63
/// Note.a.equalTemperamentFrequency(338) == 338
/// Note.bFlat.equalTemperamentFrequency(338) == 464.04
/// ```
double equalTemperamentFrequency([double a4Hertzs = 440]) {
return a4Hertzs * math.pow(sqrt12_2, Note.a.difference(this));
}

/// Returns the string representation of this [Note] following the
/// [scientific pitch notation](https://en.wikipedia.org/wiki/Scientific_pitch_notation).
///
Expand Down
72 changes: 72 additions & 0 deletions test/src/note/note_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,78 @@ void main() {
});
});

group('.hertzsFromA()', () {
test('should return the hertzs of this Note from 440 Hz', () {
expect(Note.c.equalTemperamentFrequency(), closeTo(261.63, 0.01));
expect(Note.cSharp.equalTemperamentFrequency(), closeTo(277.18, 0.01));
expect(Note.dFlat.equalTemperamentFrequency(), closeTo(277.18, 0.01));
expect(Note.d.equalTemperamentFrequency(), closeTo(293.66, 0.01));
expect(Note.dSharp.equalTemperamentFrequency(), closeTo(311.13, 0.01));
expect(Note.eFlat.equalTemperamentFrequency(), closeTo(311.13, 0.01));
expect(Note.e.equalTemperamentFrequency(), closeTo(329.63, 0.01));
expect(Note.f.equalTemperamentFrequency(), closeTo(349.23, 0.01));
expect(Note.fSharp.equalTemperamentFrequency(), closeTo(369.99, 0.01));
expect(Note.gFlat.equalTemperamentFrequency(), closeTo(369.99, 0.01));
expect(Note.g.equalTemperamentFrequency(), closeTo(392, 0.01));
expect(Note.gSharp.equalTemperamentFrequency(), closeTo(415.3, 0.01));
expect(Note.aFlat.equalTemperamentFrequency(), closeTo(415.3, 0.01));
expect(Note.a.equalTemperamentFrequency(), 440);
expect(Note.aSharp.equalTemperamentFrequency(), closeTo(466.16, 0.01));
expect(Note.bFlat.equalTemperamentFrequency(), closeTo(466.16, 0.01));
expect(Note.b.equalTemperamentFrequency(), closeTo(493.88, 0.01));
});

test('should return the hertzs of this Note from 438 Hz', () {
expect(Note.c.equalTemperamentFrequency(438), closeTo(260.44, 0.01));
expect(
Note.cSharp.equalTemperamentFrequency(438),
closeTo(275.92, 0.01),
);
expect(
Note.dFlat.equalTemperamentFrequency(438),
closeTo(275.92, 0.01),
);
expect(Note.d.equalTemperamentFrequency(438), closeTo(292.33, 0.01));
expect(
Note.dSharp.equalTemperamentFrequency(438),
closeTo(309.71, 0.01),
);
expect(
Note.eFlat.equalTemperamentFrequency(438),
closeTo(309.71, 0.01),
);
expect(Note.e.equalTemperamentFrequency(438), closeTo(328.13, 0.01));
expect(Note.f.equalTemperamentFrequency(438), closeTo(347.64, 0.01));
expect(
Note.fSharp.equalTemperamentFrequency(438),
closeTo(368.31, 0.01),
);
expect(
Note.gFlat.equalTemperamentFrequency(438),
closeTo(368.31, 0.01),
);
expect(Note.g.equalTemperamentFrequency(438), closeTo(390.21, 0.01));
expect(
Note.gSharp.equalTemperamentFrequency(438),
closeTo(413.42, 0.01),
);
expect(
Note.aFlat.equalTemperamentFrequency(438),
closeTo(413.42, 0.01),
);
expect(Note.a.equalTemperamentFrequency(438), 438);
expect(
Note.aSharp.equalTemperamentFrequency(438),
closeTo(464.04, 0.01),
);
expect(
Note.bFlat.equalTemperamentFrequency(438),
closeTo(464.04, 0.01),
);
expect(Note.b.equalTemperamentFrequency(438), closeTo(491.64, 0.01));
});
});

group('.scientificName', () {
test(
'should return the scientific pitch notation name for this Note',
Expand Down

0 comments on commit ef58cca

Please sign in to comment.