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.

206 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. // Single grain synthesis.
  28. #ifndef CLOUDS_DSP_GRAIN_H_
  29. #define CLOUDS_DSP_GRAIN_H_
  30. #include "stmlib/stmlib.h"
  31. #include "stmlib/dsp/dsp.h"
  32. #include "clouds/dsp/audio_buffer.h"
  33. #include "clouds/resources.h"
  34. namespace clouds {
  35. enum GrainQuality {
  36. GRAIN_QUALITY_LOW,
  37. GRAIN_QUALITY_MEDIUM,
  38. GRAIN_QUALITY_HIGH
  39. };
  40. class Grain {
  41. public:
  42. Grain() { }
  43. ~Grain() { }
  44. void Init() {
  45. active_ = false;
  46. envelope_phase_ = 2.0f;
  47. }
  48. void Start(
  49. int32_t pre_delay,
  50. int32_t buffer_size,
  51. int32_t start,
  52. int32_t width,
  53. int32_t phase_increment,
  54. float window_shape,
  55. float gain_l,
  56. float gain_r,
  57. GrainQuality recommended_quality) {
  58. pre_delay_ = pre_delay;
  59. width_ = width;
  60. first_sample_ = (start + buffer_size) % buffer_size;
  61. phase_increment_ = phase_increment;
  62. phase_ = 0;
  63. envelope_phase_ = 0.0f;
  64. envelope_phase_increment_ = 2.0f / static_cast<float>(width);
  65. if (window_shape >= 0.5f) {
  66. envelope_smoothness_ = (window_shape - 0.5f) * 2.0f;
  67. envelope_slope_ = 0.0f;
  68. } else {
  69. envelope_smoothness_ = 0.0f;
  70. envelope_slope_ = 0.5f / (window_shape + 0.01f);
  71. }
  72. active_ = true;
  73. gain_l_ = gain_l;
  74. gain_r_ = gain_r;
  75. recommended_quality_ = recommended_quality;
  76. }
  77. template<bool use_lut_for_envelope, GrainQuality quality>
  78. inline void RenderEnvelope(float* destination, size_t size) {
  79. const float increment = envelope_phase_increment_;
  80. const float smoothness = envelope_smoothness_;
  81. const float slope = envelope_slope_;
  82. float phase = envelope_phase_;
  83. while (size--) {
  84. float gain = phase;
  85. gain = gain >= 1.0f ? 2.0f - gain : gain;
  86. if (use_lut_for_envelope) {
  87. if (quality == GRAIN_QUALITY_HIGH) {
  88. float window = 0.0f;
  89. window = stmlib::Interpolate(lut_window, gain, 4096.0f);
  90. gain += smoothness * (window - gain);
  91. }
  92. } else {
  93. if (quality >= GRAIN_QUALITY_MEDIUM) {
  94. gain *= slope;
  95. if (gain >= 1.0f) gain = 1.0f;
  96. }
  97. }
  98. phase += increment;
  99. if (phase >= 2.0f) {
  100. *destination = -1.0f;
  101. break;
  102. }
  103. *destination++ = gain;
  104. }
  105. envelope_phase_ = phase;
  106. }
  107. template<int32_t num_channels, GrainQuality quality, Resolution resolution>
  108. inline void OverlapAdd(
  109. const AudioBuffer<resolution>* buffer,
  110. float* destination,
  111. float* envelope,
  112. size_t size) {
  113. if (!active_) {
  114. return;
  115. }
  116. // Rendering is done on 32-sample long blocks. The pre-delay allows grains
  117. // to start at arbitrary samples within a block, rather than at block
  118. // boundaries.
  119. while (pre_delay_ && size) {
  120. destination += 2;
  121. --size;
  122. --pre_delay_;
  123. }
  124. // Pre-render the envelope in one pass.
  125. if (envelope_smoothness_ == 0.0f) {
  126. RenderEnvelope<false, quality>(envelope, size);
  127. } else {
  128. RenderEnvelope<true, quality>(envelope, size);
  129. }
  130. const int32_t phase_increment = phase_increment_;
  131. const int32_t first_sample = first_sample_;
  132. const float gain_l = gain_l_;
  133. const float gain_r = gain_r_;
  134. int32_t phase = phase_;
  135. while (size--) {
  136. int32_t sample_index = first_sample + (phase >> 16);
  137. float gain = *envelope++;
  138. if (gain == -1.0f) {
  139. active_ = false;
  140. break;
  141. }
  142. float l = buffer[0].template Read<InterpolationMethod(quality)>(
  143. sample_index, phase & 65535) * gain;
  144. if (num_channels == 1) {
  145. *destination++ += l * gain_l;
  146. *destination++ += l * gain_r;
  147. } else if (num_channels == 2) {
  148. float r = buffer[1].template Read<InterpolationMethod(quality)>(
  149. sample_index, phase & 65535) * gain;
  150. *destination++ += l * gain_l + r * (1.0f - gain_r);
  151. *destination++ += r * gain_r + l * (1.0f - gain_l);
  152. }
  153. phase += phase_increment;
  154. }
  155. phase_ = phase;
  156. }
  157. inline bool active() { return active_; }
  158. inline GrainQuality recommended_quality() const {
  159. return recommended_quality_;
  160. }
  161. private:
  162. int32_t first_sample_;
  163. int32_t width_;
  164. int32_t phase_;
  165. int32_t phase_increment_;
  166. int32_t pre_delay_;
  167. float envelope_smoothness_;
  168. float envelope_slope_;
  169. float envelope_phase_;
  170. float envelope_phase_increment_;
  171. float gain_l_;
  172. float gain_r_;
  173. bool active_;
  174. GrainQuality recommended_quality_;
  175. DISALLOW_COPY_AND_ASSIGN(Grain);
  176. };
  177. } // namespace clouds
  178. #endif // CLOUDS_DSP_GRAIN_H_