You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
2.6KB

  1. // Copyright 2013 Olivier Gillet.
  2. //
  3. // Author: Olivier Gillet (ol.gillet@gmail.com)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. // See http://creativecommons.org/licenses/MIT/ for more information.
  24. //
  25. // -----------------------------------------------------------------------------
  26. //
  27. // Internal clock.
  28. #ifndef YARNS_INTERNAL_CLOCK_H_
  29. #define YARNS_INTERNAL_CLOCK_H_
  30. #include "stmlib/stmlib.h"
  31. namespace yarns {
  32. class InternalClock {
  33. public:
  34. InternalClock() {
  35. phase_ = 0;
  36. phase_increment_ = 0;
  37. }
  38. ~InternalClock() { }
  39. inline void Start(uint32_t tempo, uint8_t swing) {
  40. phase_ = 0;
  41. swing_step_ = 11;
  42. set_tempo(tempo);
  43. set_swing(swing);
  44. }
  45. inline void set_tempo(uint32_t tempo) {
  46. phase_increment_ = 178957UL * tempo / 10; // For 48kHz
  47. // phase_increment_ = 128849UL * tempo / 6; // For 40kHz
  48. }
  49. inline void set_swing(uint32_t swing) {
  50. swing_amount_ = swing * (0x80000000 / 3 / 100);
  51. }
  52. inline bool Process() {
  53. uint32_t half_cycle = 0x80000000;
  54. if (swing_step_ < 6) {
  55. half_cycle += swing_amount_;
  56. } else {
  57. half_cycle -= swing_amount_;
  58. }
  59. bool tick = false;
  60. if (phase_ >= half_cycle) {
  61. tick = true;
  62. phase_ -= half_cycle;
  63. ++swing_step_;
  64. if (swing_step_ >= 12) {
  65. swing_step_ = 0;
  66. }
  67. }
  68. phase_ += phase_increment_;
  69. return tick;
  70. }
  71. private:
  72. uint32_t phase_;
  73. uint32_t phase_increment_;
  74. uint32_t swing_amount_;
  75. uint8_t swing_step_;
  76. DISALLOW_COPY_AND_ASSIGN(InternalClock);
  77. };
  78. } // namespace yarns
  79. #endif // YARNS_INTERNAL_CLOCK_H_