Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

effect_granular.cpp 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Copyright (c) 2018 John-Michael Reed
  3. * bleeplabs.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 all
  13. * 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 THE
  21. * SOFTWARE.
  22. */
  23. #include <Arduino.h>
  24. #include "effect_granular.h"
  25. void AudioEffectGranular::begin(int16_t *sample_bank_def, int16_t max_len_def)
  26. {
  27. max_sample_len = max_len_def;
  28. grain_mode = 0;
  29. read_head = 0;
  30. write_head = 0;
  31. prev_input = 0;
  32. playpack_rate = 512;
  33. accumulator = 0;
  34. allow_len_change = true;
  35. sample_loaded = false;
  36. sample_bank = sample_bank_def;
  37. }
  38. void AudioEffectGranular::freeze(int16_t activate, int16_t playpack_rate_def, int16_t freeze_length_def)
  39. {
  40. if (activate == 0) {
  41. grain_mode = 0;
  42. allow_len_change = true;
  43. return;
  44. }
  45. __disable_irq();
  46. grain_mode = 1;
  47. rate(playpack_rate_def);
  48. if (max_sample_len <= 1500) {
  49. freeze_len = max_sample_len;
  50. } else {
  51. freeze_len = ((max_sample_len - 1500) * freeze_length_def / 1023) + 1500;
  52. }
  53. sample_loaded = false;
  54. write_en = false;
  55. sample_req = true;
  56. __enable_irq();
  57. Serial.print("in = ");
  58. Serial.print(freeze_length_def);
  59. Serial.print(", freeze len = ");
  60. Serial.println(freeze_len);
  61. }
  62. void AudioEffectGranular::shift(int16_t activate, int16_t playpack_rate_def, int16_t grain_length_def)
  63. {
  64. if (activate == 0) {
  65. grain_mode = 0;
  66. allow_len_change = true;
  67. return;
  68. }
  69. __disable_irq();
  70. grain_mode = 2;
  71. rate(playpack_rate_def);
  72. if (allow_len_change) {
  73. if (grain_length_def < 100) grain_length_def = 100;
  74. int maximum = (max_sample_len - 1) / 3;
  75. if (grain_length_def > maximum) grain_length_def = maximum;
  76. glitch_len = grain_length_def;
  77. }
  78. sample_loaded = false;
  79. write_en = false;
  80. sample_req = true;
  81. __enable_irq();
  82. }
  83. void AudioEffectGranular::rate(int16_t playpack_rate_def)
  84. {
  85. playpack_rate = playpack_rate_def;
  86. }
  87. void AudioEffectGranular::update(void)
  88. {
  89. audio_block_t *block;
  90. if (sample_bank == NULL) {
  91. block = receiveReadOnly(0);
  92. if (block) release(block);
  93. return;
  94. }
  95. block = receiveWritable(0);
  96. if (!block) return;
  97. if (grain_mode == 0) {
  98. // passthrough, no granular effect
  99. prev_input = block->data[AUDIO_BLOCK_SAMPLES-1];
  100. }
  101. else if (grain_mode == 1) {
  102. //when activated the last
  103. for (int j = 0; j < AUDIO_BLOCK_SAMPLES; j++) {
  104. if (sample_req) {
  105. // only begin capture on zero cross
  106. int16_t current_input = block->data[j];
  107. if ((current_input < 0 && prev_input >= 0) ||
  108. (current_input >= 0 && prev_input < 0)) {
  109. write_en = true;
  110. write_head = 0;
  111. read_head = 0;
  112. sample_req = false;
  113. Serial.println("begin freeze capture");
  114. } else {
  115. prev_input = current_input;
  116. }
  117. }
  118. if (write_en) {
  119. sample_bank[write_head++] = block->data[j];
  120. if (write_head >= freeze_len) {
  121. sample_loaded = true;
  122. }
  123. if (write_head >= max_sample_len) {
  124. write_en = false;
  125. Serial.println("end freeze capture");
  126. }
  127. }
  128. if (sample_loaded) {
  129. if (playpack_rate >= 0) {
  130. accumulator += playpack_rate;
  131. read_head = accumulator >> 9;
  132. }
  133. if (read_head >= freeze_len) {
  134. accumulator = 0;
  135. read_head = 0;
  136. }
  137. block->data[j] = sample_bank[read_head];
  138. }
  139. }
  140. }
  141. else if (grain_mode == 2) {
  142. //GLITCH SHIFT
  143. //basic granular synth thingy
  144. // the shorter the sample the max_sample_len the more tonal it is.
  145. // Longer it has more definition. It's a bit roboty either way which
  146. // is obv great and good enough for noise music.
  147. for (int k = 0; k < AUDIO_BLOCK_SAMPLES; k++) {
  148. // only start recording when the audio is crossing zero to minimize pops
  149. if (sample_req) {
  150. int16_t current_input = block->data[k];
  151. if ((current_input < 0 && prev_input >= 0) ||
  152. (current_input >= 0 && prev_input < 0)) {
  153. write_en = true;
  154. } else {
  155. prev_input = current_input;
  156. }
  157. }
  158. if (write_en) {
  159. sample_req = false;
  160. allow_len_change = true; // Reduces noise by not allowing the
  161. // length to change after the sample has been
  162. // recored. Kind of not too much though
  163. if (write_head >= glitch_len) {
  164. write_head = 0;
  165. sample_loaded = true;
  166. write_en = false;
  167. allow_len_change = false;
  168. }
  169. sample_bank[write_head] = block->data[k];
  170. write_head++;
  171. }
  172. if (sample_loaded) {
  173. //move it to the middle third of the bank.
  174. //3 "seperate" banks are used
  175. float fade_len = 20.00;
  176. int16_t m2 = fade_len;
  177. for (int m = 0; m < 2; m++) {
  178. // I'm off by one somewhere? why is there a tick at the
  179. // beginning of this only when it's combined with the
  180. // fade out???? ooor am i osbserving that incorrectly
  181. // either wait it works enough
  182. sample_bank[m + glitch_len] = 0;
  183. }
  184. for (int m = 2; m < glitch_len-m2; m++) {
  185. sample_bank[m + glitch_len] = sample_bank[m];
  186. }
  187. for (int m = glitch_len-m2; m < glitch_len; m++) {
  188. // fade out the end. You can just make fadet=0
  189. // but it's a little too daleky
  190. float fadet = sample_bank[m] * (m2 / fade_len);
  191. sample_bank[m + glitch_len] = (int16_t)fadet;
  192. m2--;
  193. }
  194. sample_loaded = false;
  195. prev_input = block->data[k];
  196. sample_req = true;
  197. }
  198. accumulator += playpack_rate;
  199. read_head = (accumulator >> 9);
  200. if (read_head >= glitch_len) {
  201. read_head -= glitch_len;
  202. accumulator = 0;
  203. for (int m = 0; m < glitch_len; m++) {
  204. sample_bank[m + (glitch_len*2)] = sample_bank[m+glitch_len];
  205. // sample_bank[m + (glitch_len*2)] = (m%20)*1000;
  206. }
  207. }
  208. block->data[k] = sample_bank[read_head + (glitch_len*2)];
  209. }
  210. }
  211. transmit(block);
  212. release(block);
  213. }