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.

247 lines
5.7KB

  1. // Copyright 2014 Olivier Gillet.
  2. //
  3. // Author: Olivier Gillet (ol.gillet@gmail.com)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. // See http://creativecommons.org/licenses/MIT/ for more information.
  24. //
  25. // -----------------------------------------------------------------------------
  26. //
  27. // Simple AD envelope - adapted from Peaks' multistage envelope.
  28. #ifndef STREAMS_ENVELOPE_H_
  29. #define STREAMS_ENVELOPE_H_
  30. #include "stmlib/stmlib.h"
  31. #include "streams/meta_parameters.h"
  32. namespace streams {
  33. enum EnvelopeShape {
  34. ENV_SHAPE_LINEAR,
  35. ENV_SHAPE_EXPONENTIAL,
  36. ENV_SHAPE_QUARTIC
  37. };
  38. const uint16_t kMaxNumSegments = 8;
  39. class Envelope {
  40. public:
  41. Envelope() { }
  42. ~Envelope() { }
  43. void Init();
  44. void Process(
  45. int16_t audio,
  46. int16_t excite,
  47. uint16_t* gain,
  48. uint16_t* frequency);
  49. void Configure(bool alternate, int32_t* parameters, int32_t* globals) {
  50. uint16_t a, d;
  51. if (globals) {
  52. a = globals[0];
  53. d = globals[2];
  54. ComputeAmountOffset(
  55. parameters[1],
  56. &target_frequency_amount_,
  57. &target_frequency_offset_);
  58. } else {
  59. ComputeAttackDecay(parameters[0], &a, &d);
  60. ComputeAmountOffset(
  61. parameters[1],
  62. &target_frequency_amount_,
  63. &target_frequency_offset_);
  64. }
  65. if (a != attack_ || d != decay_ || alternate != alternate_) {
  66. attack_ = a;
  67. decay_ = d;
  68. alternate_ = alternate;
  69. if (alternate_) {
  70. set_ar(a, d);
  71. } else {
  72. set_ad(a, d);
  73. }
  74. set_hard_reset(true);
  75. }
  76. }
  77. inline void set_time(uint16_t segment, uint16_t time) {
  78. time_[segment] = time;
  79. }
  80. inline void set_level(uint16_t segment, int16_t level) {
  81. level_[segment] = level;
  82. }
  83. inline void set_num_segments(uint16_t num_segments) {
  84. num_segments_ = num_segments;
  85. }
  86. inline void set_sustain_point(uint16_t sustain_point) {
  87. sustain_point_ = sustain_point;
  88. }
  89. inline void set_ad(uint16_t attack, uint16_t decay) {
  90. num_segments_ = 2;
  91. sustain_point_ = 0;
  92. level_[0] = 0;
  93. level_[1] = 32767;
  94. level_[2] = 0;
  95. time_[0] = attack;
  96. time_[1] = decay;
  97. shape_[0] = ENV_SHAPE_LINEAR;
  98. shape_[1] = ENV_SHAPE_EXPONENTIAL;
  99. }
  100. inline void set_adr(
  101. uint16_t attack,
  102. uint16_t decay,
  103. uint16_t sustain,
  104. uint16_t release) {
  105. num_segments_ = 3;
  106. sustain_point_ = 0;
  107. level_[0] = 0;
  108. level_[1] = 32767;
  109. level_[2] = sustain;
  110. level_[3] = 0;
  111. time_[0] = attack;
  112. time_[1] = decay;
  113. time_[2] = release;
  114. shape_[0] = ENV_SHAPE_LINEAR;
  115. shape_[1] = ENV_SHAPE_LINEAR;
  116. shape_[2] = ENV_SHAPE_LINEAR;
  117. }
  118. inline void set_ar(uint16_t attack, uint16_t decay) {
  119. num_segments_ = 2;
  120. sustain_point_ = 1;
  121. level_[0] = 0;
  122. level_[1] = 32767;
  123. level_[2] = 0;
  124. time_[0] = attack;
  125. time_[1] = decay;
  126. shape_[0] = ENV_SHAPE_LINEAR;
  127. shape_[1] = ENV_SHAPE_LINEAR;
  128. }
  129. inline void set_adsar(
  130. uint16_t attack,
  131. uint16_t decay,
  132. uint16_t sustain,
  133. uint16_t release) {
  134. num_segments_ = 4;
  135. sustain_point_ = 2;
  136. level_[0] = 0;
  137. level_[1] = 32767;
  138. level_[2] = sustain;
  139. level_[3] = 32767;
  140. level_[4] = 0;
  141. time_[0] = attack;
  142. time_[1] = decay;
  143. time_[2] = attack;
  144. time_[3] = release;
  145. shape_[0] = ENV_SHAPE_LINEAR;
  146. shape_[1] = ENV_SHAPE_LINEAR;
  147. shape_[2] = ENV_SHAPE_LINEAR;
  148. shape_[3] = ENV_SHAPE_LINEAR;
  149. }
  150. inline void set_adar(
  151. uint16_t attack,
  152. uint16_t decay,
  153. uint16_t sustain,
  154. uint16_t release) {
  155. num_segments_ = 4;
  156. sustain_point_ = 0;
  157. level_[0] = 0;
  158. level_[1] = 32767;
  159. level_[2] = sustain;
  160. level_[3] = 32767;
  161. level_[4] = 0;
  162. time_[0] = attack;
  163. time_[1] = decay;
  164. time_[2] = attack;
  165. time_[3] = release;
  166. shape_[0] = ENV_SHAPE_LINEAR;
  167. shape_[1] = ENV_SHAPE_LINEAR;
  168. shape_[2] = ENV_SHAPE_LINEAR;
  169. shape_[3] = ENV_SHAPE_LINEAR;
  170. }
  171. inline void set_hard_reset(bool hard_reset) {
  172. hard_reset_ = hard_reset;
  173. }
  174. private:
  175. bool gate_;
  176. int16_t level_[kMaxNumSegments];
  177. uint16_t time_[kMaxNumSegments];
  178. EnvelopeShape shape_[kMaxNumSegments];
  179. int16_t segment_;
  180. int16_t start_value_;
  181. int16_t value_;
  182. uint32_t phase_;
  183. uint32_t phase_increment_;
  184. uint16_t num_segments_;
  185. uint16_t sustain_point_;
  186. int32_t target_frequency_amount_;
  187. int32_t target_frequency_offset_;
  188. int32_t frequency_amount_;
  189. int32_t frequency_offset_;
  190. uint16_t attack_;
  191. uint16_t decay_;
  192. bool alternate_;
  193. bool hard_reset_;
  194. int32_t rate_modulation_;
  195. int32_t gate_level_;
  196. DISALLOW_COPY_AND_ASSIGN(Envelope);
  197. };
  198. } // namespace streams
  199. #endif // STREAMS_ENVELOPE_H_