sequencer
beat_duration.hpp
Go to the documentation of this file.
1 #pragma once
2 
6 
7 #include <chrono>
8 #include <limits>
9 #include <ostream>
10 
11 namespace sequencer
12 {
14  {
15  public:
16  static constexpr auto ticks_per_unit = 1024 * 1024 * 9;
17 
18  using rep = double;
20 
21  constexpr explicit beat_duration( rep beats ) noexcept : duration_( beats )
22  {
23  }
24 
25  constexpr rep to_double() const noexcept
26  {
27  return duration_.to_double();
28  }
29 
30  constexpr beat_duration& operator+=( beat_duration other ) noexcept
31  {
32  duration_ += other.duration_;
33  return *this;
34  }
35 
36  constexpr beat_duration operator-() noexcept
37  {
38  return beat_duration( -to_double() );
39  }
40 
41  constexpr bool operator<( beat_duration other ) const noexcept
42  {
43  return duration_ < other.duration_;
44  }
45 
46  constexpr bool operator<=( beat_duration other ) const noexcept
47  {
48  return duration_ <= other.duration_;
49  }
50 
51  private:
52  internal_rep duration_;
53  };
54 
55  constexpr bool operator==( beat_duration lhs, beat_duration rhs ) noexcept
56  {
57  return lhs.to_double() == rhs.to_double();
58  }
59 
60  constexpr beat_duration operator+( beat_duration lhs, beat_duration rhs ) noexcept
61  {
62  return lhs += rhs;
63  }
64 
65  template < class Rep, class Period >
66  constexpr beat_duration operator*( const std::chrono::duration< Rep, Period >& duration,
67  beats_per_minute tempo ) noexcept
68  {
69  return beat_duration{std::chrono::duration_cast< chrono::minutes >( duration ).count() *
70  tempo.to_double()};
71  }
72 
73  template < class Rep, class Period >
74  constexpr beat_duration
76  const std::chrono::duration< Rep, Period >& duration ) noexcept
77  {
78  return duration * tempo;
79  }
80 
81  constexpr chrono::minutes operator/( beat_duration beats, beats_per_minute tempo ) noexcept
82  {
83  return chrono::minutes{beats.to_double() / tempo.to_double()};
84  }
85 
86  inline std::ostream& operator<<( std::ostream& os, beat_duration beats )
87  {
88  return os << beats.to_double() << " beats";
89  }
90 
91  constexpr beat_duration operator"" _beats( long double value ) noexcept
92  {
93  return beat_duration( value );
94  }
95 
96  constexpr beat_duration operator"" _beats( unsigned long long int value ) noexcept
97  {
98  return beat_duration( value );
99  }
100 
101  static_assert( std::chrono::seconds( 30 ) * 120.0_bpm == beat_duration( 60.0 ) );
102  static_assert( ( std::chrono::seconds( 30 ) * 120.0_bpm ) / 120.0_bpm ==
103  chrono::seconds( 30.0 ) );
104 } // namespace sequencer
105 
106 namespace std
107 {
108  template <>
109  class numeric_limits< sequencer::beat_duration >
110  {
111  public:
112  static constexpr sequencer::beat_duration epsilon() noexcept
113  {
114  return sequencer::beat_duration{std::numeric_limits< double >::epsilon()};
115  }
116 
117  static constexpr sequencer::beat_duration max() noexcept
118  {
120  std::numeric_limits< sequencer::beat_duration::internal_rep >::max().to_double()};
121  }
122  };
123 } // namespace std
constexpr chrono::minutes operator/(beat_duration beats, beats_per_minute tempo) noexcept
Definition: beat_duration.hpp:81
double rep
Definition: beat_duration.hpp:18
constexpr beat_duration operator-() noexcept
Definition: beat_duration.hpp:36
static constexpr auto ticks_per_unit
Definition: beat_duration.hpp:16
constexpr bool operator<=(beat_duration other) const noexcept
Definition: beat_duration.hpp:46
constexpr beat_duration(rep beats) noexcept
Definition: beat_duration.hpp:21
constexpr bool operator==(beat_duration lhs, beat_duration rhs) noexcept
Definition: beat_duration.hpp:55
constexpr rep to_double() const noexcept
Definition: beat_duration.hpp:25
Definition: beat_duration.hpp:106
constexpr double to_double() const noexcept
Definition: fixed_point_type.hpp:28
static constexpr sequencer::beat_duration max() noexcept
Definition: beat_duration.hpp:117
std::chrono::duration< double, std::ratio< 1, 1 > > seconds
Definition: units.hpp:8
Definition: beat_duration.hpp:13
Definition: beats_per_minute.hpp:8
constexpr beat_duration operator*(const std::chrono::duration< Rep, Period > &duration, beats_per_minute tempo) noexcept
Definition: beat_duration.hpp:66
constexpr beat_duration operator+(beat_duration lhs, beat_duration rhs) noexcept
Definition: beat_duration.hpp:60
static constexpr sequencer::beat_duration epsilon() noexcept
Definition: beat_duration.hpp:112
Definition: fixed_point_type.hpp:16
std::chrono::duration< double, std::ratio< 60, 1 > > minutes
Definition: units.hpp:9
std::ostream & operator<<(std::ostream &os, beat_duration beats)
Definition: beat_duration.hpp:86
constexpr bool operator<(beat_duration other) const noexcept
Definition: beat_duration.hpp:41
constexpr beat_duration & operator+=(beat_duration other) noexcept
Definition: beat_duration.hpp:30