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.

85 lines
1.6KB

  1. #include "Bjorklund.hpp"
  2. namespace SynthDevKit {
  3. Bjorklund::Bjorklund (uint8_t maxSlots) {
  4. if (maxSlots > MAX_SLOTS) {
  5. throw BJORK_ERROR_TOO_MANY;
  6. }
  7. numSlots = maxSlots;
  8. numSteps = maxSlots;
  9. // set step[0] to off as an initial value
  10. steps[0] = 0;
  11. reset();
  12. }
  13. void Bjorklund::reset ( ) {
  14. _step = 0;
  15. currentStep = 0;
  16. }
  17. void Bjorklund::buildString (int8_t level) {
  18. if (level == -1) {
  19. steps[_step] = 0;
  20. _step++;
  21. } else if (level == -2) {
  22. steps[_step] = 1;
  23. _step++;
  24. } else {
  25. for (uint8_t i = 0; i < _count[level]; i++) {
  26. buildString(level-1);
  27. }
  28. if (_remainders[level] !=0) {
  29. buildString(level-2);
  30. }
  31. }
  32. }
  33. void Bjorklund::computeResults( ) {
  34. if (numSteps > numSlots) {
  35. numSteps = numSlots;
  36. }
  37. _divisor = numSlots - numSteps;
  38. _remainders[0] = numSteps;
  39. _level = 0;
  40. do {
  41. _count[_level] = _divisor / _remainders[_level];
  42. _remainders[_level + 1] = _divisor % _remainders[_level];
  43. _divisor = _remainders[_level];
  44. _level++;
  45. } while (_remainders[_level] > 1);
  46. _count[_level] = _divisor;
  47. buildString (_level);
  48. }
  49. void Bjorklund::update (uint8_t steps, uint8_t slots) {
  50. if (slots > MAX_SLOTS) {
  51. throw BJORK_ERROR_TOO_MANY;
  52. }
  53. if (slots != numSlots || steps != numSteps) {
  54. numSlots = slots;
  55. numSteps = steps;
  56. reset();
  57. computeResults();
  58. }
  59. }
  60. uint8_t Bjorklund::stepValue ( ) {
  61. uint8_t ret = steps[currentStep];
  62. currentStep++;
  63. if (currentStep == numSlots) {
  64. currentStep = 0;
  65. }
  66. return ret;
  67. }
  68. }