Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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=1;
  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 == 1 ) {
  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) return;
  82. block = receiveWritable(0);
  83. if (!block) return;
  84. if (grain_mode == 3) {
  85. //through
  86. /*
  87. for (int i = 0; i < AUDIO_BLOCK_SAMPLES; i++) {
  88. write_head++;
  89. if (write_head >= max_sample_len) {
  90. write_head = 0;
  91. }
  92. sample_bank[write_head] = block->data[i];
  93. }
  94. */
  95. }
  96. if (grain_mode == 0) {
  97. //capture audio but dosen't output
  98. //this needs to be happening at all times if you want to
  99. // use grain_mode = 1, the simple "freeze" sampler.
  100. for (int i = 0; i < AUDIO_BLOCK_SAMPLES; i++) {
  101. write_head++;
  102. if (write_head >= max_sample_len) {
  103. write_head = 0;
  104. }
  105. sample_bank[write_head] = block->data[i];
  106. }
  107. }
  108. if (grain_mode == 1) {
  109. //when activated the last
  110. for (int j = 0; j < AUDIO_BLOCK_SAMPLES; j++) {
  111. if (playpack_rate >= 0) {
  112. accumulator += playpack_rate;
  113. read_head = accumulator >> 9;
  114. }
  115. if (read_head >= freeze_len) {
  116. accumulator = 0;
  117. read_head -= max_sample_len;
  118. }
  119. block->data[j] = sample_bank[read_head];
  120. }
  121. }
  122. if (grain_mode == 2) {
  123. //GLITCH SHIFT
  124. //basic granular synth thingy
  125. // the shorter the sample the max_sample_len the more tonal it is.
  126. // Longer it has more definition. It's a bit roboty either way which
  127. // is obv great and good enough for noise music.
  128. for (int k = 0; k < AUDIO_BLOCK_SAMPLES; k++) {
  129. int16_t current_input = block->data[k];
  130. //we only want to start recodeing when the audio is crossing zero to minimize pops
  131. if ((current_input<0 && prev_input>0)) {
  132. zero_cross_down=1;
  133. } else {
  134. zero_cross_down=0;
  135. }
  136. prev_input=current_input;
  137. if (zero_cross_down == 1 && sample_req == 1) {
  138. write_en=1;
  139. }
  140. if (write_en==1) {
  141. sample_req=0;
  142. allow_len_change=1; //reduces noise by not allowing the length to
  143. // change after the sample has been recored.
  144. // kind of not too much though
  145. if (write_head >= glitch_len) {
  146. glitch_cross_len=glitch_len;
  147. write_head = 0;
  148. sample_loaded = 1;
  149. write_en=0;
  150. allow_len_change=0;
  151. }
  152. sample_bank[write_head] = block->data[k];
  153. write_head++;
  154. }
  155. if (sample_loaded == 1) {
  156. //move it to the middle third of the bank.
  157. //3 "seperate" banks are used
  158. float fade_len=20.00;
  159. int16_t m2=fade_len;
  160. for (int m = 0; m < 2; m++) {
  161. // I'm off by one somewhere? why is there a tick at the
  162. // beginning of this only when it's combined with the
  163. // fade out???? ooor am i osbserving that incorrectly
  164. // either wait it works enough
  165. sample_bank[m + glitch_len] = 0;
  166. }
  167. for (int m = 2; m < glitch_len-m2; m++) {
  168. sample_bank[m + glitch_len] = sample_bank[m];
  169. }
  170. for (int m = glitch_len-m2; m < glitch_len; m++) {
  171. // fade out the end. You can just make fadet=0
  172. // but it's a little too daleky
  173. float fadet=sample_bank[m]*(m2/fade_len);
  174. sample_bank[m + glitch_len] = (int16_t)fadet;
  175. m2--;
  176. }
  177. sample_loaded = 0;
  178. sample_req=1;
  179. }
  180. accumulator += playpack_rate;
  181. read_head = (accumulator >> 9);
  182. if (read_head >= glitch_len) {
  183. read_head -= (glitch_len);
  184. accumulator=0;
  185. for (int m = 0; m < glitch_len; m++) {
  186. sample_bank[m + (glitch_len*2)] = sample_bank[m+glitch_len];
  187. // sample_bank[m + (glitch_len*2)] = (m%20)*1000;
  188. }
  189. }
  190. block->data[k] = sample_bank[read_head+(glitch_len*2)];
  191. }
  192. }
  193. transmit(block);
  194. release(block);
  195. }