Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

effect_envelope.h 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 "Arduino.h"
  29. #include "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.0); // default values...
  38. attack(1.5);
  39. hold(0.5);
  40. decay(15.0);
  41. sustain(0.667);
  42. release(30.0);
  43. }
  44. void noteOn();
  45. void noteOff();
  46. void delay(float milliseconds) {
  47. delay_count = milliseconds2count(milliseconds);
  48. }
  49. void attack(float milliseconds) {
  50. attack_count = milliseconds2count(milliseconds);
  51. if (attack_count == 0) attack_count = 1;
  52. }
  53. void hold(float milliseconds) {
  54. hold_count = milliseconds2count(milliseconds);
  55. }
  56. void decay(float milliseconds) {
  57. decay_count = milliseconds2count(milliseconds);
  58. if (decay_count == 0) decay_count = 1;
  59. }
  60. void sustain(float level) {
  61. if (level < 0.0) level = 0;
  62. else if (level > 1.0) level = 1.0;
  63. sustain_mult = level * 1073741824.0;
  64. }
  65. void release(float milliseconds) {
  66. release_count = milliseconds2count(milliseconds);
  67. if (release_count == 0) release_count = 1;
  68. }
  69. using AudioStream::release;
  70. virtual void update(void);
  71. private:
  72. uint16_t milliseconds2count(float milliseconds) {
  73. if (milliseconds < 0.0) milliseconds = 0.0;
  74. uint32_t c = ((uint32_t)(milliseconds*SAMPLES_PER_MSEC)+7)>>3;
  75. if (c > 65535) c = 65535; // allow up to 11.88 seconds
  76. return c;
  77. }
  78. audio_block_t *inputQueueArray[1];
  79. // state
  80. uint8_t state; // idle, delay, attack, hold, decay, sustain, release
  81. uint16_t count; // how much time remains in this state, in 8 sample units
  82. int32_t mult_hires; // attenuation, 0=off, 0x40000000=unity gain
  83. int32_t inc_hires; // amount to change mult_hires every 8 samples
  84. // settings
  85. uint16_t delay_count;
  86. uint16_t attack_count;
  87. uint16_t hold_count;
  88. uint16_t decay_count;
  89. int32_t sustain_mult;
  90. uint16_t release_count;
  91. };
  92. #undef SAMPLES_PER_MSEC
  93. #endif