您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

229 行
5.9KB

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