|
frequency 1.0.0
Type-safe frequency handling library modeled after std::chrono
|
A frequency value with a representation and precision. More...
#include <frequency/frequency.hpp>
Public Types | |
| using | rep = Rep |
| The representation type. | |
| using | precision = typename Precision::type |
| The precision as a std::ratio. | |
Static Public Member Functions | |
| static constexpr frequency | zero () noexcept |
| Returns a zero frequency. | |
| static constexpr frequency | min () noexcept |
| Returns the minimum representable frequency. | |
| static constexpr frequency | max () noexcept |
| Returns the maximum representable frequency. | |
A frequency value with a representation and precision.
Similar to std::chrono::duration, frequency represents a quantity of cycles per second. The precision is expressed as a std::ratio of one Hertz.
| Rep | Arithmetic type for the tick count. |
| Precision | A std::ratio representing the precision (default: 1 Hz). |
Definition at line 281 of file frequency.hpp.
| using freq::frequency< Rep, Precision >::precision = typename Precision::type |
The precision as a std::ratio.
Definition at line 290 of file frequency.hpp.
The representation type.
Definition at line 288 of file frequency.hpp.
|
constexprdefault |
Constructs a zero frequency.
Referenced by freq::frequency< Rep, Precision >::max(), freq::frequency< Rep, Precision >::min(), freq::frequency< Rep, Precision >::octave_shift(), freq::frequency< Rep, Precision >::operator++(), freq::frequency< Rep, Precision >::operator--(), freq::frequency< Rep, Precision >::semitone_shift(), and freq::frequency< Rep, Precision >::zero().
|
inlineexplicitconstexpr |
Constructs from a tick count.
Implicit for inexact types, explicit otherwise to prevent accidental precision loss.
| Rep2 | The source representation type. |
| r | The tick count. |
Definition at line 307 of file frequency.hpp.
|
inlineconstexpr |
Constructs from another frequency with different representation or precision.
Implicit when the conversion is lossless (target precision evenly divides source precision).
| Rep2 | Source representation type. |
| Precision2 | Source precision. |
| f | The source frequency. |
Definition at line 323 of file frequency.hpp.
|
inlineexplicitconstexpr |
Explicit constructor for lossy precision conversions.
| Rep2 | Source representation type. |
| Precision2 | Source precision. |
| f | The source frequency. |
Definition at line 336 of file frequency.hpp.
|
default |
|
inlineconstexpr |
Returns the tick count.
Definition at line 343 of file frequency.hpp.
Referenced by freq::operator/(), freq::operator<=>(), freq::operator==(), and freq::frequency< Rep, Precision >::period().
|
inlineconstexpr |
Returns the nth harmonic of this frequency.
The nth harmonic is n times the fundamental frequency. For example, the 2nd harmonic of 440 Hz is 880 Hz.
This method provides a semantically clear way to express harmonic relationships, equivalent to freq * n.
| n | The harmonic number (must be positive). |
Definition at line 522 of file frequency.hpp.
References freq::frequency_cast().
Returns the maximum representable frequency.
Definition at line 408 of file frequency.hpp.
References freq::frequency< Rep, Precision >::frequency().
Returns the minimum representable frequency.
Definition at line 405 of file frequency.hpp.
References freq::frequency< Rep, Precision >::frequency().
|
inline |
Returns this frequency shifted by a number of octaves.
Shifts the frequency up or down by the specified number of octaves. Each octave represents a doubling (positive) or halving (negative) of frequency. For example, shifting 440 Hz up by 1 octave yields 880 Hz.
Fractional octaves are supported and particularly useful with floating-point types:
| T | The type of the octave value (default: double). |
| octaves | The number of octaves to shift (positive = up, negative = down). |
Definition at line 565 of file frequency.hpp.
References freq::frequency< Rep, Precision >::frequency(), and freq::frequency_cast().
|
inline |
Calculates the interval in octaves between this frequency and another.
Returns the number of octaves between this frequency and the other frequency. Positive values indicate this frequency is higher, negative values indicate lower. For example, octaves_from(440 Hz) called on 880 Hz returns 1.0 (one octave higher).
| T | The return type for the octave count (default: double). |
| other | The reference frequency to compare against. |
Definition at line 620 of file frequency.hpp.
References freq::frequency_cast().
|
inlineconstexpr |
Definition at line 394 of file frequency.hpp.
References freq::frequency_cast().
|
inlineconstexpr |
Definition at line 387 of file frequency.hpp.
References freq::frequency_cast().
|
inlineconstexpr |
Definition at line 377 of file frequency.hpp.
References freq::frequency_cast().
|
inlineconstexpr |
Definition at line 345 of file frequency.hpp.
|
inlineconstexpr |
Definition at line 353 of file frequency.hpp.
|
inlineconstexpr |
Definition at line 358 of file frequency.hpp.
References freq::frequency< Rep, Precision >::frequency().
|
inlineconstexpr |
Definition at line 367 of file frequency.hpp.
References freq::frequency_cast().
|
inlineconstexpr |
Definition at line 349 of file frequency.hpp.
|
inlineconstexpr |
Definition at line 360 of file frequency.hpp.
|
inlineconstexpr |
Definition at line 365 of file frequency.hpp.
References freq::frequency< Rep, Precision >::frequency().
|
inlineconstexpr |
Definition at line 372 of file frequency.hpp.
References freq::frequency_cast().
|
inlineconstexpr |
Definition at line 382 of file frequency.hpp.
References freq::frequency_cast().
|
default |
|
inlineconstexpr |
Returns the period of this frequency as a duration.
The period is the inverse of the frequency. For example, a frequency of 1000 Hz has a period of 1 millisecond.
For integer-to-integer conversions, this method uses exact integer arithmetic with wider intermediate types (128-bit when available) to minimize overflow risk. Common conversions like hertz{1000} to milliseconds will produce exact results.
When either the frequency or duration uses floating-point representation, the calculation falls back to floating-point arithmetic.
| Duration | The target duration type. Must satisfy the duration_like concept. |
Definition at line 442 of file frequency.hpp.
References freq::frequency< Rep, Precision >::count(), and freq::frequency_cast().
|
inline |
Returns this frequency shifted by a number of semitones.
Shifts the frequency up or down by the specified number of semitones. There are 12 semitones per octave in equal temperament tuning. For example, shifting 440 Hz up by 12 semitones yields 880 Hz (one octave).
Fractional semitones enable precise microtonal adjustments:
| T | The type of the semitone value (default: double). |
| semitones | The number of semitones to shift (positive = up, negative = down). |
Definition at line 599 of file frequency.hpp.
References freq::frequency< Rep, Precision >::frequency(), and freq::frequency_cast().
|
inline |
Calculates the interval in semitones between this frequency and another.
Returns the number of semitones between this frequency and the other frequency. There are 12 semitones per octave in equal temperament tuning. Positive values indicate this frequency is higher, negative values indicate lower.
| T | The return type for the semitone count (default: double). |
| other | The reference frequency to compare against. |
Definition at line 636 of file frequency.hpp.
References freq::frequency_cast().
|
inlineconstexpr |
Returns the nth subharmonic of this frequency.
The nth subharmonic is the fundamental frequency divided by n. For example, the 2nd subharmonic of 880 Hz is 440 Hz.
This method provides a semantically clear way to express subharmonic relationships, equivalent to freq / n.
| n | The subharmonic divisor (must be positive). |
Definition at line 536 of file frequency.hpp.
References freq::frequency_cast().
|
inlineconstexpr |
Calculates the wavelength for this frequency.
Returns the wavelength for this frequency given the time it takes for a wave to travel one unit of distance. The relationship is: wavelength = period / time_per_unit_distance
For electromagnetic waves in vacuum, use std::chrono::nanoseconds(3) for approximately 1 nanosecond per 0.3 meters (since light travels ~0.3 meters per nanosecond).
| Distance | The target distance type. Must satisfy the distance_like concept. |
| Duration | The duration type representing time per unit distance. Must satisfy duration_like. |
| time_per_unit_distance | The time it takes to travel one unit of the target distance. |
Definition at line 658 of file frequency.hpp.
References freq::frequency_cast().
|
inlineconstexpr |
Calculates the wavelength for this frequency given a propagation velocity.
Returns the wavelength for this frequency given the propagation velocity. The relationship is: wavelength = velocity / frequency
| Distance | The target distance type. Must satisfy the distance_like concept. |
| velocity | The propagation velocity in meters per second. Defaults to the speed of light (299,792,458 m/s). |
Definition at line 710 of file frequency.hpp.
References freq::frequency_cast().
|
inlinestaticconstexprnoexcept |
Returns a zero frequency.
Definition at line 402 of file frequency.hpp.
References freq::frequency< Rep, Precision >::frequency().
Referenced by freq::abs().