Skip to content

Commit

Permalink
refactor(frequency): ♻️ expose referenceTemperature parameter (#472)
Browse files Browse the repository at this point in the history
Signed-off-by: Albert Mañosa <26429103+albertms10@users.noreply.github.com>
  • Loading branch information
albertms10 committed Apr 1, 2024
1 parent 0ae9ef5 commit fd96403
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
7 changes: 6 additions & 1 deletion lib/src/note/closest_pitch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,14 @@ class ClosestPitch {
Frequency frequency({
TuningSystem tuningSystem = const EqualTemperament.edo12(),
Celsius temperature = Celsius.reference,
Celsius referenceTemperature = Celsius.reference,
}) =>
Frequency(
pitch.frequency(tuningSystem: tuningSystem, temperature: temperature) *
pitch.frequency(
tuningSystem: tuningSystem,
temperature: temperature,
referenceTemperature: referenceTemperature,
) *
cents.ratio,
);

Expand Down
7 changes: 5 additions & 2 deletions lib/src/note/frequency.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,11 @@ extension type const Frequency._(num hertz) implements num {
ClosestPitch closestPitch({
TuningSystem tuningSystem = const EqualTemperament.edo12(),
Celsius temperature = Celsius.reference,
Celsius referenceTemperature = Celsius.reference,
}) {
final cents = Ratio(at(temperature) / tuningSystem.fork.frequency).cents;
final cents = Ratio(
at(temperature, referenceTemperature) / tuningSystem.fork.frequency,
).cents;
final semitones = tuningSystem.fork.pitch.semitones + (cents / 100).round();

final closestPitch = PitchClass(semitones)
Expand Down Expand Up @@ -88,7 +91,7 @@ extension type const Frequency._(num hertz) implements num {
/// See [Change of pitch with change of temperature](https://sengpielaudio.com/calculator-pitchchange.htm).
///
/// ![Effect of a Local Temperature Change in an Organ Pipe](https://sengpielaudio.com/TonhoehenaenderungDurchTemperaturaenderung.gif)
Frequency at(Celsius temperature, {Celsius reference = Celsius.reference}) =>
Frequency at(Celsius temperature, [Celsius reference = Celsius.reference]) =>
Frequency(hertz * temperature.ratio(reference));

/// The harmonic at [index] from this [Frequency], including negative
Expand Down
5 changes: 4 additions & 1 deletion lib/src/note/pitch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -423,9 +423,10 @@ final class Pitch extends Scalable<Pitch> implements Comparable<Pitch> {
Frequency frequency({
TuningSystem tuningSystem = const EqualTemperament.edo12(),
Celsius temperature = Celsius.reference,
Celsius referenceTemperature = Celsius.reference,
}) =>
Frequency(tuningSystem.fork.frequency * tuningSystem.ratio(this))
.at(temperature);
.at(temperature, referenceTemperature);

/// Creates a new [TuningFork] from this [Pitch] at a given [frequency].
///
Expand All @@ -449,6 +450,7 @@ final class Pitch extends Scalable<Pitch> implements Comparable<Pitch> {
required int upToIndex,
TuningSystem tuningSystem = const EqualTemperament.edo12(),
Celsius temperature = Celsius.reference,
Celsius referenceTemperature = Celsius.reference,
}) =>
frequency(
tuningSystem: tuningSystem,
Expand All @@ -460,6 +462,7 @@ final class Pitch extends Scalable<Pitch> implements Comparable<Pitch> {
(frequency) => frequency.closestPitch(
tuningSystem: tuningSystem,
temperature: temperature,
referenceTemperature: referenceTemperature,
),
)
.toSet();
Expand Down

0 comments on commit fd96403

Please sign in to comment.