Skip to content

Commit

Permalink
Merge branch 'main' into fix/address-large-closest-pitch-spellings-fo…
Browse files Browse the repository at this point in the history
…r-harmonics-with-temperature

Signed-off-by: Albert Mañosa <26429103+albertms10@users.noreply.github.com>
  • Loading branch information
albertms10 committed Apr 1, 2024
2 parents a673720 + 495b869 commit 48b97a9
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 8 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 @@ -461,6 +463,7 @@ final class Pitch extends Scalable<Pitch> implements Comparable<Pitch> {
.closestPitch(
tuningSystem: tuningSystem,
temperature: temperature,
referenceTemperature: referenceTemperature,
)
.respelledSimple,
)
Expand Down
7 changes: 5 additions & 2 deletions lib/src/tuning/cent.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ extension type const Cent(num value) implements num {
/// The unit symbol for [Cent].
static const unitSymbol = '¢';

/// The [Cent] divisions per semitone.
static const divisionsPerSemitone = Cent(100);

/// The number of cents in an [Interval.P8].
static const octaveCents = Cent(chromaticDivisions * 100);
static const octave = Cent(chromaticDivisions * divisionsPerSemitone);

/// The [Ratio] for this [Cent].
Ratio get ratio => Ratio(math.pow(2, value / octaveCents));
Ratio get ratio => Ratio(math.pow(2, value / octave));

/// This [Cent] formatted as a string.
///
Expand Down
2 changes: 1 addition & 1 deletion lib/src/tuning/ratio.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ extension type const Ratio._(num value) implements num {
/// edo19.ratioFromSemitones(1).cents == const Cent(63.16)
/// edo19.ratioFromSemitones(10).cents == const Cent(631.58)
/// ```
Cent get cents => Cent(math.log(value) / math.log(2) * Cent.octaveCents);
Cent get cents => Cent(math.log(value) / math.log(2) * Cent.octave);
}
2 changes: 1 addition & 1 deletion test/src/note/closest_pitch_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void main() {
});
});

group('.respellSimple', () {
group('.respelledSimple', () {
test('respells this ClosestPitch to the simplest expression', () {
expect(ClosestPitch.parse('A4+36').respelledSimple.toString(), 'A4+36');
expect(
Expand Down

0 comments on commit 48b97a9

Please sign in to comment.