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.

182 satır
5.1KB

  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 "effect_envelope.h"
  27. #define STATE_IDLE 0
  28. #define STATE_DELAY 1
  29. #define STATE_ATTACK 2
  30. #define STATE_HOLD 3
  31. #define STATE_DECAY 4
  32. #define STATE_SUSTAIN 5
  33. #define STATE_RELEASE 6
  34. #define STATE_FORCED 7
  35. void AudioEffectEnvelope::noteOn(void)
  36. {
  37. __disable_irq();
  38. if (state == STATE_IDLE || state == STATE_DELAY || release_forced_count == 0) {
  39. mult_hires = 0;
  40. count = delay_count;
  41. if (count > 0) {
  42. state = STATE_DELAY;
  43. inc_hires = 0;
  44. } else {
  45. state = STATE_ATTACK;
  46. count = attack_count;
  47. inc_hires = 0x40000000 / (int32_t)count;
  48. }
  49. } else if (state != STATE_FORCED) {
  50. state = STATE_FORCED;
  51. count = release_forced_count;
  52. inc_hires = (-mult_hires) / (int32_t)count;
  53. }
  54. __enable_irq();
  55. }
  56. void AudioEffectEnvelope::noteOff(void)
  57. {
  58. __disable_irq();
  59. if (state != STATE_IDLE && state != STATE_FORCED) {
  60. state = STATE_RELEASE;
  61. count = release_count;
  62. inc_hires = (-mult_hires) / (int32_t)count;
  63. }
  64. __enable_irq();
  65. }
  66. void AudioEffectEnvelope::update(void)
  67. {
  68. audio_block_t *block;
  69. uint32_t *p, *end;
  70. uint32_t sample12, sample34, sample56, sample78, tmp1, tmp2;
  71. block = receiveWritable();
  72. if (!block) return;
  73. if (state == STATE_IDLE) {
  74. release(block);
  75. return;
  76. }
  77. p = (uint32_t *)(block->data);
  78. end = p + AUDIO_BLOCK_SAMPLES/2;
  79. while (p < end) {
  80. // we only care about the state when completing a region
  81. if (count == 0) {
  82. if (state == STATE_ATTACK) {
  83. count = hold_count;
  84. if (count > 0) {
  85. state = STATE_HOLD;
  86. mult_hires = 0x40000000;
  87. inc_hires = 0;
  88. } else {
  89. state = STATE_DECAY;
  90. count = decay_count;
  91. inc_hires = (sustain_mult - 0x40000000) / (int32_t)count;
  92. }
  93. continue;
  94. } else if (state == STATE_HOLD) {
  95. state = STATE_DECAY;
  96. count = decay_count;
  97. inc_hires = (sustain_mult - 0x40000000) / (int32_t)count;
  98. continue;
  99. } else if (state == STATE_DECAY) {
  100. state = STATE_SUSTAIN;
  101. count = 0xFFFF;
  102. mult_hires = sustain_mult;
  103. inc_hires = 0;
  104. } else if (state == STATE_SUSTAIN) {
  105. count = 0xFFFF;
  106. } else if (state == STATE_RELEASE) {
  107. state = STATE_IDLE;
  108. while (p < end) {
  109. *p++ = 0;
  110. *p++ = 0;
  111. *p++ = 0;
  112. *p++ = 0;
  113. }
  114. break;
  115. } else if (state == STATE_FORCED) {
  116. mult_hires = 0;
  117. count = delay_count;
  118. if (count > 0) {
  119. state = STATE_DELAY;
  120. inc_hires = 0;
  121. } else {
  122. state = STATE_ATTACK;
  123. count = attack_count;
  124. inc_hires = 0x40000000 / (int32_t)count;
  125. }
  126. } else if (state == STATE_DELAY) {
  127. state = STATE_ATTACK;
  128. count = attack_count;
  129. inc_hires = 0x40000000 / count;
  130. continue;
  131. }
  132. }
  133. int32_t mult = mult_hires >> 14;
  134. int32_t inc = inc_hires >> 17;
  135. // process 8 samples, using only mult and inc (16 bit resolution)
  136. sample12 = *p++;
  137. sample34 = *p++;
  138. sample56 = *p++;
  139. sample78 = *p++;
  140. p -= 4;
  141. mult += inc;
  142. tmp1 = signed_multiply_32x16b(mult, sample12);
  143. mult += inc;
  144. tmp2 = signed_multiply_32x16t(mult, sample12);
  145. sample12 = pack_16b_16b(tmp2, tmp1);
  146. mult += inc;
  147. tmp1 = signed_multiply_32x16b(mult, sample34);
  148. mult += inc;
  149. tmp2 = signed_multiply_32x16t(mult, sample34);
  150. sample34 = pack_16b_16b(tmp2, tmp1);
  151. mult += inc;
  152. tmp1 = signed_multiply_32x16b(mult, sample56);
  153. mult += inc;
  154. tmp2 = signed_multiply_32x16t(mult, sample56);
  155. sample56 = pack_16b_16b(tmp2, tmp1);
  156. mult += inc;
  157. tmp1 = signed_multiply_32x16b(mult, sample78);
  158. mult += inc;
  159. tmp2 = signed_multiply_32x16t(mult, sample78);
  160. sample78 = pack_16b_16b(tmp2, tmp1);
  161. *p++ = sample12;
  162. *p++ = sample34;
  163. *p++ = sample56;
  164. *p++ = sample78;
  165. // adjust the long-term gain using 30 bit resolution (fix #102)
  166. // https://github.com/PaulStoffregen/Audio/issues/102
  167. mult_hires += inc_hires;
  168. count--;
  169. }
  170. transmit(block);
  171. release(block);
  172. }