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.

228 lines
6.0KB

  1. // Copyright 2011 Olivier Gillet.
  2. //
  3. // Author: Olivier Gillet (ol.gillet@gmail.com)
  4. //
  5. // This program is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. //
  16. // -----------------------------------------------------------------------------
  17. //
  18. // Global clock.
  19. #include "grids/pattern_generator.h"
  20. #include <avr/eeprom.h>
  21. #include <avr/pgmspace.h>
  22. #include "avrlib/op.h"
  23. #include "grids/resources.h"
  24. namespace grids {
  25. using namespace avrlib;
  26. /* static */
  27. Options PatternGenerator::options_;
  28. /* static */
  29. uint8_t PatternGenerator::pulse_;
  30. /* static */
  31. uint8_t PatternGenerator::step_;
  32. /* static */
  33. bool PatternGenerator::first_beat_;
  34. /* static */
  35. bool PatternGenerator::beat_;
  36. /* static */
  37. uint8_t PatternGenerator::euclidean_step_[kNumParts];
  38. /* static */
  39. uint8_t PatternGenerator::state_;
  40. /* static */
  41. uint8_t PatternGenerator::pulse_duration_counter_;
  42. /* static */
  43. uint8_t PatternGenerator::part_perturbation_[kNumParts];
  44. /* static */
  45. PatternGeneratorSettings PatternGenerator::settings_;
  46. /* static */
  47. uint8_t PatternGenerator::factory_testing_;
  48. /* extern */
  49. PatternGenerator pattern_generator;
  50. static const prog_uint8_t* drum_map[5][5] = {
  51. { node_10, node_8, node_0, node_9, node_11 },
  52. { node_15, node_7, node_13, node_12, node_6 },
  53. { node_18, node_14, node_4, node_5, node_3 },
  54. { node_23, node_16, node_21, node_1, node_2 },
  55. { node_24, node_19, node_17, node_20, node_22 },
  56. };
  57. /* static */
  58. uint8_t PatternGenerator::ReadDrumMap(
  59. uint8_t step,
  60. uint8_t instrument,
  61. uint8_t x,
  62. uint8_t y) {
  63. uint8_t i = x >> 6;
  64. uint8_t j = y >> 6;
  65. const prog_uint8_t* a_map = drum_map[i][j];
  66. const prog_uint8_t* b_map = drum_map[i + 1][j];
  67. const prog_uint8_t* c_map = drum_map[i][j + 1];
  68. const prog_uint8_t* d_map = drum_map[i + 1][j + 1];
  69. uint8_t offset = (instrument * kStepsPerPattern) + step;
  70. uint8_t a = pgm_read_byte(a_map + offset);
  71. uint8_t b = pgm_read_byte(b_map + offset);
  72. uint8_t c = pgm_read_byte(c_map + offset);
  73. uint8_t d = pgm_read_byte(d_map + offset);
  74. return U8Mix(U8Mix(a, b, x << 2), U8Mix(c, d, x << 2), y << 2);
  75. }
  76. /* static */
  77. void PatternGenerator::EvaluateDrums() {
  78. // At the beginning of a pattern, decide on perturbation levels.
  79. if (step_ == 0) {
  80. for (uint8_t i = 0; i < kNumParts; ++i) {
  81. uint8_t randomness = options_.swing
  82. ? 0 : settings_.options.drums.randomness >> 2;
  83. part_perturbation_[i] = U8U8MulShift8(Random::GetByte(), randomness);
  84. }
  85. }
  86. uint8_t instrument_mask = 1;
  87. uint8_t x = settings_.options.drums.x;
  88. uint8_t y = settings_.options.drums.y;
  89. uint8_t accent_bits = 0;
  90. for (uint8_t i = 0; i < kNumParts; ++i) {
  91. uint8_t level = ReadDrumMap(step_, i, x, y);
  92. if (level < 255 - part_perturbation_[i]) {
  93. level += part_perturbation_[i];
  94. } else {
  95. // The sequencer from Anushri uses a weird clipping rule here. Comment
  96. // this line to reproduce its behavior.
  97. level = 255;
  98. }
  99. uint8_t threshold = ~settings_.density[i];
  100. if (level > threshold) {
  101. if (level > 192) {
  102. accent_bits |= instrument_mask;
  103. }
  104. state_ |= instrument_mask;
  105. }
  106. instrument_mask <<= 1;
  107. }
  108. if (output_clock()) {
  109. state_ |= accent_bits ? OUTPUT_BIT_COMMON : 0;
  110. state_ |= step_ == 0 ? OUTPUT_BIT_RESET : 0;
  111. } else {
  112. state_ |= accent_bits << 3;
  113. }
  114. }
  115. /* static */
  116. void PatternGenerator::EvaluateEuclidean() {
  117. // Refresh only on sixteenth notes.
  118. if (step_ & 1) {
  119. return;
  120. }
  121. // Euclidean pattern generation
  122. uint8_t instrument_mask = 1;
  123. uint8_t reset_bits = 0;
  124. for (uint8_t i = 0; i < kNumParts; ++i) {
  125. uint8_t length = (settings_.options.euclidean_length[i] >> 3) + 1;
  126. uint8_t density = settings_.density[i] >> 3;
  127. uint16_t address = U8U8Mul(length - 1, 32) + density;
  128. while (euclidean_step_[i] >= length) {
  129. euclidean_step_[i] -= length;
  130. }
  131. uint32_t step_mask = 1L << static_cast<uint32_t>(euclidean_step_[i]);
  132. uint32_t pattern_bits = pgm_read_dword(lut_res_euclidean + address);
  133. if (pattern_bits & step_mask) {
  134. state_ |= instrument_mask;
  135. }
  136. if (euclidean_step_[i] == 0) {
  137. reset_bits |= instrument_mask;
  138. }
  139. instrument_mask <<= 1;
  140. }
  141. if (output_clock()) {
  142. state_ |= reset_bits ? OUTPUT_BIT_COMMON : 0;
  143. state_ |= (reset_bits == 0x07) ? OUTPUT_BIT_RESET : 0;
  144. } else {
  145. state_ |= reset_bits << 3;
  146. }
  147. }
  148. /* static */
  149. void PatternGenerator::LoadSettings() {
  150. options_.unpack(eeprom_read_byte(NULL));
  151. factory_testing_ = eeprom_read_byte((uint8_t*)(1)) + 1;
  152. }
  153. /* static */
  154. void PatternGenerator::SaveSettings() {
  155. eeprom_write_byte(NULL, options_.pack());
  156. ++factory_testing_;
  157. if (factory_testing_ >= 5) {
  158. factory_testing_ = 5;
  159. }
  160. eeprom_write_byte((uint8_t*)(1), factory_testing_);
  161. }
  162. /* static */
  163. void PatternGenerator::Evaluate() {
  164. state_ = 0;
  165. pulse_duration_counter_ = 0;
  166. Random::Update();
  167. // Highest bits: clock and random bit.
  168. state_ |= 0x40;
  169. state_ |= Random::state() & 0x80;
  170. if (output_clock()) {
  171. state_ |= OUTPUT_BIT_CLOCK;
  172. }
  173. // Refresh only at step changes.
  174. if (pulse_ != 0) {
  175. return;
  176. }
  177. if (options_.output_mode == OUTPUT_MODE_EUCLIDEAN) {
  178. EvaluateEuclidean();
  179. } else {
  180. EvaluateDrums();
  181. }
  182. }
  183. /* static */
  184. int8_t PatternGenerator::swing_amount() {
  185. if (options_.swing && output_mode() == OUTPUT_MODE_DRUMS) {
  186. int8_t value = U8U8MulShift8(settings_.options.drums.randomness, 42 + 1);
  187. return (!(step_ & 2)) ? value : -value;
  188. } else {
  189. return 0;
  190. }
  191. }
  192. } // namespace grids