Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

effect_envelope.h 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. #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. }
  52. void hold(float milliseconds) {
  53. hold_count = milliseconds2count(milliseconds);
  54. }
  55. void decay(float milliseconds) {
  56. decay_count = milliseconds2count(milliseconds);
  57. }
  58. void sustain(float level) {
  59. if (level < 0.0) level = 0;
  60. else if (level > 1.0) level = 1.0;
  61. sustain_mult = level * 65536.0;
  62. }
  63. void release(float milliseconds) {
  64. release_count = milliseconds2count(milliseconds);
  65. }
  66. using AudioStream::release;
  67. virtual void update(void);
  68. private:
  69. uint16_t milliseconds2count(float milliseconds) {
  70. if (milliseconds < 0.0) milliseconds = 0.0;
  71. uint32_t c = ((uint32_t)(milliseconds*SAMPLES_PER_MSEC)+7)>>3;
  72. if (c > 1103) return 1103; // allow up to 200 ms
  73. return c;
  74. }
  75. audio_block_t *inputQueueArray[1];
  76. // state
  77. uint8_t state; // idle, delay, attack, hold, decay, sustain, release
  78. uint16_t count; // how much time remains in this state, in 8 sample units
  79. int32_t mult; // attenuation, 0=off, 0x10000=unity gain
  80. int32_t inc; // amount to change mult on each sample
  81. // settings
  82. uint16_t delay_count;
  83. uint16_t attack_count;
  84. uint16_t hold_count;
  85. uint16_t decay_count;
  86. int32_t sustain_mult;
  87. uint16_t release_count;
  88. };
  89. #undef SAMPLES_PER_MSEC
  90. #endif