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.

270 lines
7.5KB

  1. //
  2. // Topograph
  3. // A port of "Mutable Instruments Grids" for VCV Rack
  4. // Author: Dale Johnson (valley.audio.soft@gmail.com)
  5. // Date: 4/12/2017
  6. //
  7. // Copyright 2011 Olivier Gillet.
  8. //
  9. // Author: Olivier Gillet (ol.gillet@gmail.com)
  10. //
  11. // This program is free software: you can redistribute it and/or modify
  12. // it under the terms of the GNU General Public License as published by
  13. // the Free Software Foundation, either version 3 of the License, or
  14. // (at your option) any later version.
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. // You should have received a copy of the GNU General Public License
  20. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. //
  22. #include "TopographPatternGenerator.hpp"
  23. uint8_t U8U8MulShift8(uint8_t a, uint8_t b) {
  24. return (a * b) >> 8;
  25. }
  26. uint8_t U8Mix(uint8_t a, uint8_t b, uint8_t balance) {
  27. return (a * (255 - balance) + b * balance) / 255;
  28. }
  29. PatternGenerator::PatternGenerator() {
  30. _pulse = 0;
  31. _beat = 0;
  32. _firstBeat = 0;
  33. _step = 0;
  34. for(int i = 0; i < 3; ++i) {
  35. _euclideanStep[i] = 0;
  36. }
  37. _state = 0;
  38. _accentBits = 0;
  39. }
  40. void PatternGenerator::tick(uint8_t numPulses) {
  41. evaluate();
  42. _beat = (_step & 0x7) == 0;
  43. _firstBeat = _step == 0;
  44. _pulse += numPulses;
  45. // Wrap into ppqn steps.
  46. while (_pulse >= kPulsesPerStep) {
  47. _pulse -= kPulsesPerStep;
  48. if (!(_step & 1)) {
  49. for (uint8_t i = 0; i < kNumParts; ++i) {
  50. ++_euclideanStep[i];
  51. }
  52. }
  53. ++_step;
  54. }
  55. // Wrap into step sequence steps.
  56. if (_step >= kStepsPerPattern) {
  57. _step -= kStepsPerPattern;
  58. }
  59. }
  60. void PatternGenerator::reset() {
  61. _step = 0;
  62. _pulse = 0;
  63. for(long i = 0; i < 3; ++i) {
  64. _euclideanStep[i] = 0;
  65. }
  66. }
  67. void PatternGenerator::setMapX(uint8_t x) {
  68. _settings.x = x;
  69. }
  70. void PatternGenerator::setMapY(uint8_t y) {
  71. _settings.y = y;
  72. }
  73. void PatternGenerator::setBDDensity(uint8_t density) {
  74. _settings.density[0] = density;
  75. }
  76. void PatternGenerator::setSDDensity(uint8_t density) {
  77. _settings.density[1] = density;
  78. }
  79. void PatternGenerator::setHHDensity(uint8_t density) {
  80. _settings.density[2] = density;
  81. }
  82. void PatternGenerator::setDrumDensity(uint8_t channel, uint8_t density) {
  83. _settings.density[channel] = density;
  84. }
  85. void PatternGenerator::setEuclideanLength(uint8_t channel, uint8_t length){
  86. _settings.euclidean_length[channel] = length;
  87. }
  88. void PatternGenerator::setRandomness(uint8_t randomness) {
  89. _settings.randomness = randomness;
  90. }
  91. void PatternGenerator::setAccentAltMode(bool accAlt){
  92. _settings.accAlt = accAlt;
  93. }
  94. void PatternGenerator::setPatternMode(PatternGeneratorMode mode) {
  95. _settings.patternMode = mode;
  96. }
  97. uint8_t PatternGenerator::getAllStates() const {
  98. return _state;
  99. }
  100. uint8_t PatternGenerator::getDrumState(uint8_t channel) const {
  101. uint8_t mask[6] = {1,2,4,8,16,32};
  102. return (_state & mask[channel]) >> channel;
  103. }
  104. PatternGeneratorMode PatternGenerator::getPatternMode() const {
  105. return _settings.patternMode;
  106. }
  107. uint8_t PatternGenerator::getBeat() const {
  108. return _beat;
  109. }
  110. uint8_t PatternGenerator::getEuclideanLength(uint8_t channel) {
  111. return _settings.euclidean_length[channel];
  112. }
  113. uint8_t PatternGenerator::readDrumMap(uint8_t step, uint8_t instrument, uint8_t x, uint8_t y) {
  114. uint8_t r = 0;
  115. if(_settings.patternMode == PATTERN_HENRI) {
  116. uint8_t i = (int)floor(x * 3.0 / 255.0);
  117. uint8_t j = (int)floor(y * 3.0 / 255.0);
  118. const uint8_t* a_map = drum_map[i][j];
  119. const uint8_t* b_map = drum_map[i + 1][j];
  120. const uint8_t* c_map = drum_map[i][j + 1];
  121. const uint8_t* d_map = drum_map[i + 1][j + 1];
  122. uint8_t offset = (instrument * kStepsPerPattern) + step;
  123. uint8_t a = a_map[offset];
  124. uint8_t b = b_map[offset];
  125. uint8_t c = c_map[offset];
  126. uint8_t d = d_map[offset];
  127. uint8_t maxValue = 127;
  128. r = (( a * x + b * (maxValue - x) ) * y + (c * x + d * (maxValue - x)) *
  129. ( maxValue - y )) / maxValue / maxValue;
  130. }
  131. else {
  132. uint8_t i = x >> 6;
  133. uint8_t j = y >> 6;
  134. const uint8_t* a_map = drum_map[i][j];
  135. const uint8_t* b_map = drum_map[i + 1][j];
  136. const uint8_t* c_map = drum_map[i][j + 1];
  137. const uint8_t* d_map = drum_map[i + 1][j + 1];
  138. uint8_t offset = (instrument * kStepsPerPattern) + step;
  139. uint8_t a = *(a_map + offset);
  140. uint8_t b = *(b_map + offset);
  141. uint8_t c = *(c_map + offset);
  142. uint8_t d = *(d_map + offset);
  143. r = U8Mix(U8Mix(a, b, x << 2), U8Mix(c, d, x << 2), y << 2);
  144. }
  145. return r;
  146. }
  147. void PatternGenerator::evaluate() {
  148. _state = 0;
  149. _state |= 0x40;
  150. if (_settings.accAlt) {
  151. _state |= OUTPUT_BIT_CLOCK;
  152. }
  153. // Refresh only at step changes.
  154. if (_pulse != 0) {
  155. return;
  156. }
  157. if (_settings.patternMode == PATTERN_EUCLIDEAN) {
  158. evaluateEuclidean();
  159. } else {
  160. evaluateDrums();
  161. }
  162. }
  163. void PatternGenerator::evaluateEuclidean() {
  164. // Refresh only on sixteenth notes.
  165. if (_step & 1) {
  166. return;
  167. }
  168. // Euclidean pattern generation
  169. uint8_t instrument_mask = 1;
  170. uint8_t reset_bits = 0;
  171. for (uint8_t i = 0; i < kNumParts; ++i) {
  172. uint8_t length = (_settings.euclidean_length[i] >> 3) + 1;
  173. uint8_t density = _settings.density[i] >> 3;
  174. uint16_t address = (length - 1) * 32 + density;
  175. while (_euclideanStep[i] >= length) {
  176. _euclideanStep[i] -= length;
  177. }
  178. uint32_t step_mask = 1L << static_cast<uint32_t>(_euclideanStep[i]);
  179. uint32_t pattern_bits = *(lut_res_euclidean + address);
  180. if (pattern_bits & step_mask) {
  181. _state |= instrument_mask;
  182. }
  183. if (_euclideanStep[i] == 0) {
  184. reset_bits |= instrument_mask;
  185. }
  186. instrument_mask <<= 1;
  187. }
  188. if (_settings.accAlt) {
  189. _state |= reset_bits ? OUTPUT_BIT_COMMON : 0;
  190. _state |= (reset_bits == 0x07) ? OUTPUT_BIT_RESET : 0;
  191. } else {
  192. _state |= reset_bits << 3;
  193. }
  194. }
  195. void PatternGenerator::evaluateDrums() {
  196. // At the beginning of a pattern, decide on perturbation levels.
  197. if (_step == 0) {
  198. for (uint8_t i = 0; i < kNumParts; ++i) {
  199. uint8_t randomNum = (uint8_t)rand() % 256;
  200. uint8_t randomness = _settings.swing ? 0 : _settings.randomness >> 2;
  201. _partPerturbation_[i] = U8U8MulShift8(randomNum, randomness);
  202. }
  203. }
  204. uint8_t instrument_mask = 1;
  205. uint8_t x = _settings.x;
  206. uint8_t y = _settings.y;
  207. _accentBits = 0;
  208. for (uint8_t i = 0; i < kNumParts; ++i) {
  209. uint8_t level = readDrumMap(_step, i, x, y);
  210. if (level < 255 - _partPerturbation_[i]) {
  211. level += _partPerturbation_[i];
  212. }
  213. else {
  214. // The sequencer from Anushri uses a weird clipping rule here. Comment
  215. // this line to reproduce its behavior.
  216. level = 255;
  217. }
  218. uint8_t threshold = ~_settings.density[i];
  219. if (level > threshold) {
  220. if (level > 192) {
  221. _accentBits |= instrument_mask;
  222. }
  223. _state |= instrument_mask;
  224. }
  225. instrument_mask <<= 1;
  226. }
  227. if (_settings.accAlt) {
  228. _state |= _accentBits ? OUTPUT_BIT_COMMON : 0;
  229. _state |= _step == 0 ? OUTPUT_BIT_RESET : 0;
  230. }
  231. else {
  232. _state |= _accentBits << 3;
  233. }
  234. }