sequencer
clock_base.hpp
Go to the documentation of this file.
1 #pragma once
2 
5 
6 namespace sequencer::midi
7 {
8  class clock_base
9  {
10  public:
11  static constexpr int initial_pulses_per_quarter_note = 24;
12 
13  constexpr clock_base() noexcept = default;
14 
15  constexpr explicit clock_base(
16  beat_time_point start_time,
17  int pulses_per_quarter_note = initial_pulses_per_quarter_note ) noexcept
18  : last_update_( start_time ), start_time_( start_time ),
19  pulses_per_quarter_note_( pulses_per_quarter_note )
20  {
21  }
22 
23  template < typename Sender >
24  void update( beat_time_point t, Sender sender )
25  {
26  using namespace realtime;
27 
28  if ( !running_ && started_ )
29  {
30  sender( continue_ ? realtime_continue() : realtime_start() );
31  running_ = true;
32  continue_ = true;
33  }
34  else if ( running_ && !started_ )
35  {
36  sender( realtime_stop() );
37  running_ = false;
38  }
39 
40  if ( running_ )
41  {
42  const auto dt = tick();
43  while ( last_update_ + dt <= t )
44  {
45  last_update_ += dt;
46  sender( realtime_clock() );
47  }
48  }
49  }
50 
51  constexpr bool is_started() const noexcept
52  {
53  return started_;
54  }
55 
56  void reset() noexcept
57  {
58  started_ = false;
59  continue_ = false;
60  last_update_ = start_time_;
61  }
62 
63  constexpr void start() noexcept
64  {
65  started_ = true;
66  }
67 
68  constexpr void stop() noexcept
69  {
70  started_ = false;
71  }
72 
73  constexpr int pulses_per_quarter_note() const noexcept
74  {
75  return pulses_per_quarter_note_;
76  }
77 
78  constexpr beat_duration tick() const noexcept
79  {
80  return beat_duration( 1.0 / pulses_per_quarter_note() );
81  }
82 
83  private:
84  beat_time_point last_update_;
85  beat_time_point start_time_;
86  int pulses_per_quarter_note_{initial_pulses_per_quarter_note};
87  bool started_ = false;
88  bool running_ = false;
89  bool continue_ = false;
90  };
91 
92 } // namespace sequencer::midi
Definition: clock_base.hpp:8
constexpr void start() noexcept
Definition: clock_base.hpp:63
constexpr clock_base() noexcept=default
constexpr beat_duration tick() const noexcept
Definition: clock_base.hpp:78
Definition: beat_duration.hpp:13
constexpr bool is_started() const noexcept
Definition: clock_base.hpp:51
constexpr int pulses_per_quarter_note() const noexcept
Definition: clock_base.hpp:73
void reset() noexcept
Definition: clock_base.hpp:56
void update(beat_time_point t, Sender sender)
Definition: clock_base.hpp:24
constexpr auto realtime_stop
Definition: byte.hpp:30
constexpr void stop() noexcept
Definition: clock_base.hpp:68
constexpr auto realtime_continue
Definition: byte.hpp:29
Definition: clock.hpp:13
Definition: beat_time_point.hpp:8
static constexpr int initial_pulses_per_quarter_note
Definition: clock_base.hpp:11
constexpr auto realtime_clock
Definition: byte.hpp:27
constexpr auto realtime_start
Definition: byte.hpp:28