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.

effect_envelope.cpp 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /* Audio Library for Teensy 3.X
  2. * Copyright (c) 2017, Paul Stoffregen, paul@pjrc.com
  3. *
  4. * Development of this audio library was funded by PJRC.COM, LLC by sales of
  5. * Teensy and Audio Adaptor boards. Please support PJRC's efforts to develop
  6. * open source software by purchasing Teensy or other PJRC products.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice, development funding notice, and this permission
  16. * notice shall be included in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include <core/Arduino.h>
  27. #include "audio/effect_envelope.h"
  28. #define STATE_IDLE 0
  29. #define STATE_DELAY 1
  30. #define STATE_ATTACK 2
  31. #define STATE_HOLD 3
  32. #define STATE_DECAY 4
  33. #define STATE_SUSTAIN 5
  34. #define STATE_RELEASE 6
  35. #define STATE_FORCED 7
  36. void AudioEffectEnvelope::noteOn(void)
  37. {
  38. __disable_irq();
  39. if (state == STATE_IDLE || state == STATE_DELAY || release_forced_count == 0) {
  40. mult_hires = 0;
  41. count = delay_count;
  42. if (count > 0) {
  43. state = STATE_DELAY;
  44. inc_hires = 0;
  45. } else {
  46. state = STATE_ATTACK;
  47. count = attack_count;
  48. inc_hires = 0x40000000 / (int32_t)count;
  49. }
  50. } else if (state != STATE_FORCED) {
  51. state = STATE_FORCED;
  52. count = release_forced_count;
  53. inc_hires = (-mult_hires) / (int32_t)count;
  54. }
  55. __enable_irq();
  56. }
  57. void AudioEffectEnvelope::noteOff(void)
  58. {
  59. __disable_irq();
  60. if (state != STATE_IDLE && state != STATE_FORCED) {
  61. state = STATE_RELEASE;
  62. count = release_count;
  63. inc_hires = (-mult_hires) / (int32_t)count;
  64. }
  65. __enable_irq();
  66. }
  67. void AudioEffectEnvelope::update(void)
  68. {
  69. audio_block_t *block;
  70. uint32_t *p, *end;
  71. uint32_t sample12, sample34, sample56, sample78, tmp1, tmp2;
  72. block = receiveWritable();
  73. if (!block) return;
  74. if (state == STATE_IDLE) {
  75. release(block);
  76. return;
  77. }
  78. p = (uint32_t *)(block->data);
  79. end = p + AUDIO_BLOCK_SAMPLES/2;
  80. while (p < end) {
  81. // we only care about the state when completing a region
  82. if (count == 0) {
  83. if (state == STATE_ATTACK) {
  84. count = hold_count;
  85. if (count > 0) {
  86. state = STATE_HOLD;
  87. mult_hires = 0x40000000;
  88. inc_hires = 0;
  89. } else {
  90. state = STATE_DECAY;
  91. count = decay_count;
  92. inc_hires = (sustain_mult - 0x40000000) / (int32_t)count;
  93. }
  94. continue;
  95. } else if (state == STATE_HOLD) {
  96. state = STATE_DECAY;
  97. count = decay_count;
  98. inc_hires = (sustain_mult - 0x40000000) / (int32_t)count;
  99. continue;
  100. } else if (state == STATE_DECAY) {
  101. state = STATE_SUSTAIN;
  102. count = 0xFFFF;
  103. mult_hires = sustain_mult;
  104. inc_hires = 0;
  105. } else if (state == STATE_SUSTAIN) {
  106. count = 0xFFFF;
  107. } else if (state == STATE_RELEASE) {
  108. state = STATE_IDLE;
  109. while (p < end) {
  110. *p++ = 0;
  111. *p++ = 0;
  112. *p++ = 0;
  113. *p++ = 0;
  114. }
  115. break;
  116. } else if (state == STATE_FORCED) {
  117. mult_hires = 0;
  118. count = delay_count;
  119. if (count > 0) {
  120. state = STATE_DELAY;
  121. inc_hires = 0;
  122. } else {
  123. state = STATE_ATTACK;
  124. count = attack_count;
  125. inc_hires = 0x40000000 / (int32_t)count;
  126. }
  127. } else if (state == STATE_DELAY) {
  128. state = STATE_ATTACK;
  129. count = attack_count;
  130. inc_hires = 0x40000000 / count;
  131. continue;
  132. }
  133. }
  134. int32_t mult = mult_hires >> 14;
  135. int32_t inc = inc_hires >> 17;
  136. // process 8 samples, using only mult and inc (16 bit resolution)
  137. sample12 = *p++;
  138. sample34 = *p++;
  139. sample56 = *p++;
  140. sample78 = *p++;
  141. p -= 4;
  142. mult += inc;
  143. tmp1 = signed_multiply_32x16b(mult, sample12);
  144. mult += inc;
  145. tmp2 = signed_multiply_32x16t(mult, sample12);
  146. sample12 = pack_16b_16b(tmp2, tmp1);
  147. mult += inc;
  148. tmp1 = signed_multiply_32x16b(mult, sample34);
  149. mult += inc;
  150. tmp2 = signed_multiply_32x16t(mult, sample34);
  151. sample34 = pack_16b_16b(tmp2, tmp1);
  152. mult += inc;
  153. tmp1 = signed_multiply_32x16b(mult, sample56);
  154. mult += inc;
  155. tmp2 = signed_multiply_32x16t(mult, sample56);
  156. sample56 = pack_16b_16b(tmp2, tmp1);
  157. mult += inc;
  158. tmp1 = signed_multiply_32x16b(mult, sample78);
  159. mult += inc;
  160. tmp2 = signed_multiply_32x16t(mult, sample78);
  161. sample78 = pack_16b_16b(tmp2, tmp1);
  162. *p++ = sample12;
  163. *p++ = sample34;
  164. *p++ = sample56;
  165. *p++ = sample78;
  166. // adjust the long-term gain using 30 bit resolution (fix #102)
  167. // https://github.com/PaulStoffregen/Audio/issues/102
  168. mult_hires += inc_hires;
  169. count--;
  170. }
  171. transmit(block);
  172. release(block);
  173. }
  174. bool AudioEffectEnvelope::isActive()
  175. {
  176. uint8_t current_state = *(volatile uint8_t *)&state;
  177. if (current_state == STATE_IDLE) return false;
  178. return true;
  179. }
  180. bool AudioEffectEnvelope::isSustain()
  181. {
  182. uint8_t current_state = *(volatile uint8_t *)&state;
  183. if (current_state == STATE_SUSTAIN) return true;
  184. return false;
  185. }