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.

78 lines
1.4KB

  1. #pragma once
  2. class ClockMult
  3. {
  4. public:
  5. /**
  6. * Clocks the multiplier by one sample.
  7. * Returns 0 or more high speed clocks.
  8. */
  9. void sampleClock();
  10. /**
  11. * Sends one reference tick to the multiplier. This is
  12. * the "input" to the multiplier.
  13. */
  14. void refClock();
  15. /**
  16. * 0..1 saw at the same rate as multiplier output.
  17. */
  18. float getSaw() const
  19. {
  20. return sawPhase;
  21. }
  22. /**
  23. * The binary pulse output of the clock multiplier
  24. * (note: doesn't work yet)
  25. */
  26. bool getMultipliedClock() const
  27. {
  28. return clockOutValue;
  29. }
  30. /**
  31. * @param x >= 1 is the multiplier factor
  32. * x == 0 : free run
  33. */
  34. void setMultiplier(int x);
  35. float _getFreq() const
  36. {
  37. return learnedFrequency;
  38. }
  39. void setFreeRunFreq(float f)
  40. {
  41. freeRunFreq = f;
  42. }
  43. private:
  44. enum class State
  45. {
  46. INIT,
  47. TRAINING,
  48. RUNNING
  49. };
  50. int trainingCounter = 12345;
  51. int learnedPeriod = 999;
  52. float learnedFrequency = 0;
  53. int freqMultFactor = 0; // if 0, free run, else mult by n.
  54. State state = State::INIT;
  55. bool clockOutValue = 0;
  56. int clockOutTimer = 0;
  57. float sawPhase = 0;
  58. float freeRunFreq = 0;
  59. void startNewClock();
  60. bool isFreeRun() const
  61. {
  62. return freqMultFactor == 0;
  63. }
  64. void sampleClockFreeRunMode();
  65. void sampleClockLockedMode();
  66. };