frequency.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright (c) 2019 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef API_UNITS_FREQUENCY_H_
  11. #define API_UNITS_FREQUENCY_H_
  12. #ifdef UNIT_TEST
  13. #include <ostream> // no-presubmit-check TODO(webrtc:8982)
  14. #endif // UNIT_TEST
  15. #include <cstdlib>
  16. #include <limits>
  17. #include <string>
  18. #include <type_traits>
  19. #include "api/units/time_delta.h"
  20. #include "rtc_base/units/unit_base.h"
  21. namespace webrtc {
  22. class Frequency final : public rtc_units_impl::RelativeUnit<Frequency> {
  23. public:
  24. template <typename T>
  25. static constexpr Frequency MilliHertz(T value) {
  26. static_assert(std::is_arithmetic<T>::value, "");
  27. return FromValue(value);
  28. }
  29. template <typename T>
  30. static constexpr Frequency Hertz(T value) {
  31. static_assert(std::is_arithmetic<T>::value, "");
  32. return FromFraction(1'000, value);
  33. }
  34. template <typename T>
  35. static constexpr Frequency KiloHertz(T value) {
  36. static_assert(std::is_arithmetic<T>::value, "");
  37. return FromFraction(1'000'000, value);
  38. }
  39. Frequency() = delete;
  40. template <typename T = int64_t>
  41. constexpr T hertz() const {
  42. return ToFraction<1000, T>();
  43. }
  44. template <typename T = int64_t>
  45. constexpr T millihertz() const {
  46. return ToValue<T>();
  47. }
  48. private:
  49. friend class rtc_units_impl::UnitBase<Frequency>;
  50. using RelativeUnit::RelativeUnit;
  51. static constexpr bool one_sided = true;
  52. };
  53. inline constexpr Frequency operator/(int64_t nominator,
  54. const TimeDelta& interval) {
  55. constexpr int64_t kKiloPerMicro = 1000 * 1000000;
  56. RTC_DCHECK_LE(nominator, std::numeric_limits<int64_t>::max() / kKiloPerMicro);
  57. RTC_CHECK(interval.IsFinite());
  58. RTC_CHECK(!interval.IsZero());
  59. return Frequency::MilliHertz(nominator * kKiloPerMicro / interval.us());
  60. }
  61. inline constexpr TimeDelta operator/(int64_t nominator,
  62. const Frequency& frequency) {
  63. constexpr int64_t kMegaPerMilli = 1000000 * 1000;
  64. RTC_DCHECK_LE(nominator, std::numeric_limits<int64_t>::max() / kMegaPerMilli);
  65. RTC_CHECK(frequency.IsFinite());
  66. RTC_CHECK(!frequency.IsZero());
  67. return TimeDelta::Micros(nominator * kMegaPerMilli / frequency.millihertz());
  68. }
  69. inline constexpr double operator*(Frequency frequency, TimeDelta time_delta) {
  70. return frequency.hertz<double>() * time_delta.seconds<double>();
  71. }
  72. inline constexpr double operator*(TimeDelta time_delta, Frequency frequency) {
  73. return frequency * time_delta;
  74. }
  75. std::string ToString(Frequency value);
  76. inline std::string ToLogString(Frequency value) {
  77. return ToString(value);
  78. }
  79. #ifdef UNIT_TEST
  80. inline std::ostream& operator<<( // no-presubmit-check TODO(webrtc:8982)
  81. std::ostream& stream, // no-presubmit-check TODO(webrtc:8982)
  82. Frequency value) {
  83. return stream << ToString(value);
  84. }
  85. #endif // UNIT_TEST
  86. } // namespace webrtc
  87. #endif // API_UNITS_FREQUENCY_H_