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

effect_envelope.cpp 4.5KB

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