Clock.h 876 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #pragma once
  2. #include <mutex>
  3. #include <atomic>
  4. #include <thread>
  5. #include <chrono>
  6. #include <iostream>
  7. namespace midi {
  8. class Clock
  9. {
  10. typedef std::chrono::high_resolution_clock clock;
  11. typedef clock::time_point time_point;
  12. typedef clock::duration duration;
  13. public:
  14. typedef unsigned int bpm_type;
  15. private:
  16. bpm_type bpm;
  17. std::mutex bpm_mutex;
  18. time_point lastTickTime;
  19. std::thread tickThread;
  20. std::atomic<bool> stopTickThread;
  21. bool running;
  22. std::mutex running_mutex;
  23. public:
  24. Clock();
  25. bpm_type getBpm();
  26. void setBpm(bpm_type bpm);
  27. duration getTickInterval();
  28. void start();
  29. void stop();
  30. bool isRunning();
  31. static inline time_point now()
  32. {
  33. return clock::now();
  34. }
  35. protected:
  36. virtual void tick();
  37. private:
  38. void run();
  39. void setRunning(bool running);
  40. };
  41. } // namespace