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.

157 lines
4.2KB

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