Clock.h 855 B

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