|
| constexpr | frequency ()=default |
| | Constructs a zero frequency.
|
| |
| | frequency (const frequency &)=default |
| |
template<typename Rep2 >
requires std::convertible_to<const Rep2&, rep> && (treat_as_inexact_v<rep> || !treat_as_inexact_v<Rep2>) |
| constexpr | frequency (const Rep2 &r) |
| | Constructs from a tick count.
|
| |
template<typename Rep2 , typename Precision2 >
requires std::convertible_to<const Rep2&, rep> && detail::_lossless_conversion<Rep2, Precision2, rep, precision> && detail::_quantity_castable<frequency<Rep2, Precision2>, frequency> |
| constexpr | frequency (const frequency< Rep2, Precision2 > &f) |
| | Constructs from another frequency with different representation or precision.
|
| |
template<typename Rep2 , typename Precision2 >
requires (!detail::_lossless_conversion<Rep2, Precision2, rep, precision> || !std::convertible_to<const Rep2&, rep>) && detail::_quantity_castable<frequency<Rep2, Precision2>, frequency> |
| constexpr | frequency (const frequency< Rep2, Precision2 > &f) |
| | Explicit constructor for lossy conversions.
|
| |
| | ~frequency ()=default |
| |
| frequency & | operator= (const frequency &)=default |
| |
| constexpr rep | count () const |
| | Returns the tick count.
|
| |
| constexpr frequency< typename std::common_type< rep >::type, precision > | operator+ () const |
| |
| constexpr frequency< typename std::common_type< rep >::type, precision > | operator- () const |
| |
| constexpr frequency & | operator++ () |
| |
| constexpr frequency | operator++ (int) |
| |
| constexpr frequency & | operator-- () |
| |
| constexpr frequency | operator-- (int) |
| |
| constexpr frequency & | operator+= (const frequency &f) |
| |
| constexpr frequency & | operator-= (const frequency &f) |
| |
| constexpr frequency & | operator*= (const rep &r) |
| |
| constexpr frequency & | operator/= (const rep &r) |
| |
| constexpr frequency & | operator%= (const rep &r) |
| |
| constexpr frequency & | operator%= (const frequency &f) |
| |
| template<duration_like Duration> |
| constexpr Duration | period () const |
| | Returns the period of this frequency as a duration.
|
| |
| constexpr frequency | harmonic (unsigned int n) const |
| | Returns the nth harmonic of this frequency.
|
| |
| constexpr frequency | subharmonic (unsigned int n) const |
| | Returns the nth subharmonic of this frequency.
|
| |
| template<typename T = double> |
| frequency | octave_shift (T octaves) const |
| | Returns this frequency shifted by a number of octaves.
|
| |
| template<typename T = double> |
| frequency | semitone_shift (T semitones) const |
| | Returns this frequency shifted by a number of semitones.
|
| |
| template<typename T = double> |
| T | octaves_from (const frequency &other) const |
| | Calculates the interval in octaves between this frequency and another.
|
| |
| template<typename T = double> |
| T | semitones_from (const frequency &other) const |
| | Calculates the interval in semitones between this frequency and another.
|
| |
| template<distance_like Distance, duration_like Duration> |
| constexpr Distance | wavelength (const Duration &time_per_unit_distance) const |
| | Calculates the wavelength for this frequency.
|
| |
| template<distance_like Distance> |
| constexpr Distance | wavelength (double velocity=299792458.0) const |
| | Calculates the wavelength for this frequency given a propagation velocity.
|
| |
template<typename Rep, typename Precision>
class quanta::frequency::frequency< Rep, Precision >
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.
Basic Examples
auto wifi = 2400_MHz;
auto sum = 1000_Hz + 500_Hz;
auto ratio = 1000_Hz / 100_Hz;
frequency< int64_t > hertz
Frequency with 1 Hz precision.
frequency< int64_t, std::kilo > kilohertz
Frequency with 1000 Hz (kilohertz) precision.
constexpr scaled_t scaled
Selects the scaled form in to_string().
Floating-Point Examples
hertz_d concert_pitch{440.0};
hertz_d fundamental{100.0};
hertz_d perfect_fifth = fundamental * 1.5;
hertz_d major_third = fundamental * 1.25;
kilohertz_d am_radio{540.5};
A frequency value with a representation and precision.
frequency semitone_shift(T semitones) const
Returns this frequency shifted by a number of semitones.
- Template Parameters
-
| Rep | Arithmetic type for the tick count. |
| Precision | A std::ratio representing the precision (default: 1 Hz). |
Definition at line 222 of file frequency.hpp.
template<typename Rep , typename Precision >
template<typename Rep2 , typename Precision2 >
requires std::convertible_to<const Rep2&,
rep> && detail::_lossless_conversion<Rep2, Precision2,
rep,
precision> && detail::_quantity_castable<
frequency<Rep2, Precision2>,
frequency>
Constructs from another frequency with different representation or precision.
Implicit when the conversion is lossless (the target rep is floating-point, or the target precision evenly divides the source precision and the source rep is not floating-point) and the source rep converts implicitly to the target rep. Any other conversion that the cast can perform is explicit (see below).
- Template Parameters
-
| Rep2 | Source representation type. |
| Precision2 | Source precision. |
- Parameters
-
Definition at line 268 of file frequency.hpp.
template<typename Rep , typename Precision >
template<typename Rep2 , typename Precision2 >
requires (!detail::_lossless_conversion<Rep2, Precision2,
rep,
precision> || !std::convertible_to<const Rep2&,
rep>) && detail::_quantity_castable<
frequency<Rep2, Precision2>,
frequency>
Explicit constructor for lossy conversions.
Every conversion that the cast can perform and that is not implicit (see above) is explicit: a lossy one (to a coarser precision, or from a floating-point rep to an integral one), or one to a rep that the source rep converts to only explicitly. The value is that of the domain's cast, truncated toward zero. The cast can perform the conversion when the conversion factor between the precisions fits in a std::ratio and the reps are integral or have a common type with intmax_t through which the count converts.
- Template Parameters
-
| Rep2 | Source representation type. |
| Precision2 | Source precision. |
- Parameters
-
Definition at line 291 of file frequency.hpp.
template<typename Rep , typename Precision >
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.
- Parameters
-
| n | The harmonic number (must be positive). |
- Returns
- A frequency representing the nth harmonic.
Definition at line 472 of file frequency.hpp.
template<typename Rep , typename Precision >
template<typename T = double>
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:
auto tritone = a4.octave_shift(0.5);
hertz_d a4_d{440.0};
frequency octave_shift(T octaves) const
Returns this frequency shifted by a number of octaves.
- Template Parameters
-
| T | The type of the octave value (default: double). |
- Parameters
-
| octaves | The number of octaves to shift (positive = up, negative = down). |
- Returns
- A frequency representing this frequency shifted by the specified octaves.
Definition at line 515 of file frequency.hpp.
References quanta::frequency::frequency< Rep, Precision >::frequency().
template<typename Rep , typename Precision >
template<typename T = double>
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).
- Template Parameters
-
| T | The return type for the octave count (default: double). |
- Parameters
-
| other | The reference frequency to compare against. |
- Returns
- The number of octaves between the two frequencies.
Definition at line 570 of file frequency.hpp.
template<typename Rep , typename Precision >
template<duration_like Duration>
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.
- Template Parameters
-
| Duration | The target duration type, a std::chrono::duration (see duration_like). |
- Returns
- The period as a duration.
- Note
- For integer duration types, be aware that the result may truncate if the exact period is not representable (e.g., gigahertz{2}.period<nanoseconds>() truncates to 0 since the true period is 0.5 ns).
hertz{1000}.period<std::chrono::milliseconds>()
kilohertz{1}.period<std::chrono::milliseconds>()
hertz{100}.period<std::chrono::microseconds>()
using hertz_d = frequency<double>;
hertz_d{440.0}.period<std::chrono::duration<double, std::milli>>()
Definition at line 397 of file frequency.hpp.
References quanta::frequency::frequency< Rep, Precision >::count().
template<typename Rep , typename Precision >
template<typename T = double>
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:
- Template Parameters
-
| T | The type of the semitone value (default: double). |
- Parameters
-
| semitones | The number of semitones to shift (positive = up, negative = down). |
- Returns
- A frequency representing this frequency shifted by the specified semitones.
Definition at line 549 of file frequency.hpp.
References quanta::frequency::frequency< Rep, Precision >::frequency().
template<typename Rep , typename Precision >
template<typename T = double>
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.
- Template Parameters
-
| T | The return type for the semitone count (default: double). |
- Parameters
-
| other | The reference frequency to compare against. |
- Returns
- The number of semitones between the two frequencies.
Definition at line 586 of file frequency.hpp.
template<typename Rep , typename Precision >
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.
- Parameters
-
| n | The subharmonic divisor (must be positive). |
- Returns
- A frequency representing the nth subharmonic.
Definition at line 486 of file frequency.hpp.
template<typename Rep , typename Precision >
template<distance_like Distance, duration_like Duration>
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).
- Template Parameters
-
| Distance | The target distance type, e.g. quanta::spatial::millimeters. Must satisfy the distance_like concept (so it cannot be a std::chrono::duration). |
| Duration | The duration type representing time per unit distance, a std::chrono::duration. |
- Parameters
-
| time_per_unit_distance | The time it takes to travel one unit of the target distance. |
- Returns
- The wavelength as a distance-like value.
- Note
- This method uses the period() method internally and performs ratio arithmetic to convert between time and distance domains.
Definition at line 609 of file frequency.hpp.
template<typename Rep , typename Precision >
template<distance_like Distance>
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
- Template Parameters
-
| Distance | The target distance type, e.g. quanta::spatial::millimeters. Must satisfy the distance_like concept (so it cannot be a std::chrono::duration). |
- Parameters
-
| velocity | The propagation velocity in meters per second. Defaults to the speed of light (299,792,458 m/s). |
- Returns
- The wavelength as a distance-like value.
- Note
- For electromagnetic waves in vacuum, use the default speed of light. For other media or wave types, specify the appropriate propagation velocity. For sound in air at 20°C, use approximately 343 m/s.
auto lambda = (2400_MHz).wavelength<quanta::spatial::millimeters>();
Definition at line 667 of file frequency.hpp.