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: Extract Frequency #112

Merged
merged 3 commits into from
May 13, 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
1 change: 1 addition & 0 deletions lib/music_notes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ part 'src/music.dart';
part 'src/music_item.dart';
part 'src/note/accidental.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';
Expand Down
24 changes: 24 additions & 0 deletions lib/src/note/frequency.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
part of '../../music_notes.dart';

/// Represents an absolute pitch, a physical frequency.
@immutable
class Frequency {
const Frequency(this.hertz);

final double hertz;

/// Whether this [Frequency] is inside the human hearing range.
///
/// Example:
/// ```dart
/// const Frequency(880).isHumanAudible == true
/// Note.a.inOctave(4).equalTemperamentFrequency().isHumanAudible == true
/// Note.g.inOctave(12).equalTemperamentFrequency(442).isHumanAudible == false
/// ```
bool get isHumanAudible {
const minFrequency = 20;
const maxFrequency = 20000;

return hertz >= minFrequency && hertz <= maxFrequency;
}
}
37 changes: 12 additions & 25 deletions lib/src/note/positioned_note.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,35 +71,22 @@ final class PositionedNote extends Note {
);
}

/// Returns the equal temperament frequency in Hertzs of this [PositionedNote]
/// from the A4 note reference.
/// Returns the equal temperament [Frequency] of this [PositionedNote] from
/// the A4 note reference.
///
/// Example:
/// ```dart
/// Note.a.inOctave(4).equalTemperamentFrequency() == 440
/// Note.gSharp.inOctave(4).equalTemperamentFrequency() == 415.3
/// Note.c.inOctave(4).equalTemperamentFrequency() == 261.63
/// Note.a.inOctave(4).equalTemperamentFrequency(338) == 338
/// Note.bFlat.inOctave(4).equalTemperamentFrequency(338) == 464.04
/// Note.a.inOctave(4).equalTemperamentFrequency() == const Frequency(440)
/// Note.gSharp.inOctave(4).equalTemperamentFrequency()
/// == const Frequency(415.3)
/// Note.c.inOctave(4).equalTemperamentFrequency() == const Frequency(261.63)
/// Note.a.inOctave(4).equalTemperamentFrequency(338) == const Frequency(338)
/// Note.bFlat.inOctave(4).equalTemperamentFrequency(338)
/// == const Frequency(464.04)
/// ```
double equalTemperamentFrequency([double a4Hertzs = 440]) =>
a4Hertzs * math.pow(sqrt12_2, Note.a.inOctave(4).difference(this));

/// Whether this [Note] is inside the human hearing range at [a4Hertzs].
///
/// Example:
/// ```dart
/// Note.a.inOctave(4).isHumanAudibleAt() == true
/// Note.d.inOctave(0).isHumanAudibleAt() == false
/// Note.g.inOctave(12).isHumanAudibleAt(442) == false
/// ```
bool isHumanAudibleAt([double a4Hertzs = 440]) {
final frequency = equalTemperamentFrequency(a4Hertzs);
const minFrequency = 20;
const maxFrequency = 20000;

return frequency >= minFrequency && frequency <= maxFrequency;
}
Frequency equalTemperamentFrequency([double a4Hertz = 440]) => Frequency(
a4Hertz * math.pow(sqrt12_2, Note.a.inOctave(4).difference(this)),
);

/// Returns the string representation of this [Note] following the
/// [scientific pitch notation](https://en.wikipedia.org/wiki/Scientific_pitch_notation).
Expand Down
2 changes: 2 additions & 0 deletions test/src/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'interval/quality_test.dart' as quality_test;
import 'music_test.dart' as music_test;
import 'note/accidental_test.dart' as accidental_test;
import 'note/enharmonic_note_test.dart' as enharmonic_note_test;
import 'note/frequency_test.dart' as frequency_test;
import 'note/note_test.dart' as note_test;
import 'note/notes_test.dart' as notes_test;
import 'note/positioned_note_test.dart' as positioned_note_test;
Expand All @@ -24,6 +25,7 @@ void main() {
music_test.main();
accidental_test.main();
enharmonic_note_test.main();
frequency_test.main();
note_test.main();
notes_test.main();
positioned_note_test.main();
Expand Down
16 changes: 16 additions & 0 deletions test/src/note/frequency_test.dart
plammens marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import 'package:music_notes/music_notes.dart';
import 'package:test/test.dart';

void main() {
group('Frequency', () {
group('.isHumanAudible', () {
test('should return whether the frequency is audible by humans', () {
expect(const Frequency(0).isHumanAudible, isFalse);
expect(const Frequency(100).isHumanAudible, isTrue);
expect(const Frequency(400).isHumanAudible, isTrue);
expect(const Frequency(15000).isHumanAudible, isTrue);
expect(const Frequency(100000).isHumanAudible, isFalse);
});
});
});
}
Loading