Skip to content

Commit

Permalink
refactor(frequency): ♻️ move at and extract Celsius.ratio methods
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 Mar 31, 2024
1 parent 4838a0e commit 34574b3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
26 changes: 8 additions & 18 deletions lib/src/note/frequency.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ extension type const Frequency._(num hertz) implements num {
);
}

/// This [Frequency] at [temperature], based on [reference].
///
/// 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(hertz * temperature.ratio(reference));

/// The harmonic at [index] from this [Frequency], including negative
/// values as part of the [undertone series](https://en.wikipedia.org/wiki/Undertone_series).
///
Expand Down Expand Up @@ -126,21 +134,3 @@ extension type const Frequency._(num hertz) implements num {
/// ```
String format() => '$hertz $hertzUnitSymbol';
}

/// A Frequency extension based on temperature.
extension TemperatureFrequency on Frequency {
/// Speed of sound at [Celsius.zero] in m/s.
static const _baseSpeedOfSound = 331.3;

/// The speed of sound in m/s based on [temperature].
///
/// See [Speed of sound in ideal gases and air](https://en.wikipedia.org/wiki/Speed_of_sound#Speed_of_sound_in_ideal_gases_and_air).
static num speedOfSoundAt(Celsius temperature) =>
_baseSpeedOfSound + 0.6 * temperature;

/// This [Frequency] at [temperature], based on [reference].
Frequency at(Celsius temperature, {Celsius reference = Celsius.reference}) =>
Frequency(
hertz * (speedOfSoundAt(temperature) / speedOfSoundAt(reference)),
);
}
20 changes: 20 additions & 0 deletions lib/src/tuning/temperature.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,28 @@
import 'ratio.dart';

/// The representation of a Celsius temperature.
extension type const Celsius(num degrees) implements num {
/// The absolute zero [Celsius] temperature.
static const zero = Celsius(0);

/// The reference [Celsius] temperature.
static const reference = Celsius(20);

/// Speed of sound at [Celsius.zero] in m/s.
static const _baseSpeedOfSound = 331.3;

/// The increase per degree [Celsius] in m/s.
static const _speedFactorPerDegreeCelsius = 0.6;

/// The speed of sound in m/s based on [temperature].
static num _speedOfSoundAt(Celsius temperature) =>
_baseSpeedOfSound + _speedFactorPerDegreeCelsius * temperature;

/// The speed of sound [Ratio] between this [Celsius] temperature and
/// [reference].
///
/// See [Speed of sound in ideal gases and air](https://en.wikipedia.org/wiki/Speed_of_sound#Speed_of_sound_in_ideal_gases_and_air).
Ratio ratio([Celsius reference = reference]) => Ratio(
_speedOfSoundAt(this) / _speedOfSoundAt(reference),
);
}

0 comments on commit 34574b3

Please sign in to comment.