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.

108 line
3.6KB

  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. #ifndef effect_envelope_h_
  27. #define effect_envelope_h_
  28. #include <core/Arduino.h>
  29. #include <core/AudioStream.h>
  30. #include "./utility/dspinst.h"
  31. #define SAMPLES_PER_MSEC (AUDIO_SAMPLE_RATE_EXACT/1000.0)
  32. class AudioEffectEnvelope : public AudioStream
  33. {
  34. public:
  35. AudioEffectEnvelope() : AudioStream(1, inputQueueArray) {
  36. state = 0;
  37. delay(0.0f); // default values...
  38. attack(10.5f);
  39. hold(2.5f);
  40. decay(35.0f);
  41. sustain(0.5f);
  42. release(300.0f);
  43. releaseNoteOn(5.0f);
  44. }
  45. void noteOn();
  46. void noteOff();
  47. void delay(float milliseconds) {
  48. delay_count = milliseconds2count(milliseconds);
  49. }
  50. void attack(float milliseconds) {
  51. attack_count = milliseconds2count(milliseconds);
  52. if (attack_count == 0) attack_count = 1;
  53. }
  54. void hold(float milliseconds) {
  55. hold_count = milliseconds2count(milliseconds);
  56. }
  57. void decay(float milliseconds) {
  58. decay_count = milliseconds2count(milliseconds);
  59. if (decay_count == 0) decay_count = 1;
  60. }
  61. void sustain(float level) {
  62. if (level < 0.0) level = 0;
  63. else if (level > 1.0) level = 1.0;
  64. sustain_mult = level * 1073741824.0;
  65. }
  66. void release(float milliseconds) {
  67. release_count = milliseconds2count(milliseconds);
  68. if (release_count == 0) release_count = 1;
  69. }
  70. void releaseNoteOn(float milliseconds) {
  71. release_forced_count = milliseconds2count(milliseconds);
  72. if (release_count == 0) release_count = 1;
  73. }
  74. bool isActive();
  75. bool isSustain();
  76. using AudioStream::release;
  77. virtual void update(void);
  78. private:
  79. uint16_t milliseconds2count(float milliseconds) {
  80. if (milliseconds < 0.0) milliseconds = 0.0;
  81. uint32_t c = ((uint32_t)(milliseconds*SAMPLES_PER_MSEC)+7)>>3;
  82. if (c > 65535) c = 65535; // allow up to 11.88 seconds
  83. return c;
  84. }
  85. audio_block_t *inputQueueArray[1];
  86. // state
  87. uint8_t state; // idle, delay, attack, hold, decay, sustain, release, forced
  88. uint16_t count; // how much time remains in this state, in 8 sample units
  89. int32_t mult_hires; // attenuation, 0=off, 0x40000000=unity gain
  90. int32_t inc_hires; // amount to change mult_hires every 8 samples
  91. // settings
  92. uint16_t delay_count;
  93. uint16_t attack_count;
  94. uint16_t hold_count;
  95. uint16_t decay_count;
  96. int32_t sustain_mult;
  97. uint16_t release_count;
  98. uint16_t release_forced_count;
  99. };
  100. #undef SAMPLES_PER_MSEC
  101. #endif