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.

216 lines
7.1KB

  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. // Calibration settings.
  28. #include "clouds/cv_scaler.h"
  29. #include <algorithm>
  30. #include <cmath>
  31. #include "stmlib/dsp/dsp.h"
  32. #include "clouds/resources.h"
  33. namespace clouds {
  34. using namespace std;
  35. /* static */
  36. CvTransformation CvScaler::transformations_[ADC_CHANNEL_LAST] = {
  37. // ADC_POSITION_POTENTIOMETER_CV,
  38. { true, false, 0.05f },
  39. // ADC_DENSITY_POTENTIOMETER_CV,
  40. { true, false, 0.01f },
  41. // ADC_SIZE_POTENTIOMETER,
  42. { false, false, 0.01f },
  43. // ADC_SIZE_CV,
  44. { false, true, 0.1f },
  45. // ADC_PITCH_POTENTIOMETER,
  46. { false, false, 0.01f },
  47. // ADC_V_OCT_CV,
  48. { false, false, 1.00f },
  49. // ADC_BLEND_POTENTIOMETER,
  50. { false, false, 0.05f },
  51. // ADC_BLEND_CV,
  52. { false, true, 0.2f },
  53. // ADC_TEXTURE_POTENTIOMETER,
  54. { false, false, 0.01f },
  55. // ADC_TEXTURE_CV,
  56. { false, true, 0.01f }
  57. };
  58. void CvScaler::Init(CalibrationData* calibration_data) {
  59. adc_.Init();
  60. gate_input_.Init();
  61. calibration_data_ = calibration_data;
  62. fill(&smoothed_adc_value_[0], &smoothed_adc_value_[ADC_CHANNEL_LAST], 0.0f);
  63. note_ = 0.0f;
  64. fill(&blend_[0], &blend_[BLEND_PARAMETER_LAST], 0.0f);
  65. fill(&blend_mod_[0], &blend_mod_[BLEND_PARAMETER_LAST], 0.0f);
  66. previous_blend_knob_value_ = 0.0f;
  67. blend_parameter_ = BLEND_PARAMETER_DRY_WET;
  68. blend_knob_quantized_ = -1.0f;
  69. blend_knob_touched_ = false;
  70. fill(&previous_trigger_[0], &previous_trigger_[kAdcLatency], false);
  71. fill(&previous_gate_[0], &previous_gate_[kAdcLatency], false);
  72. }
  73. void CvScaler::UpdateBlendParameters(float knob_value, float cv) {
  74. // Update the blending settings (base value and modulation) from the
  75. // Blend knob and CV.
  76. for (int32_t i = 0; i < BLEND_PARAMETER_LAST; ++i) {
  77. float target = i == blend_parameter_ ? cv : 0.0f;
  78. float coefficient = i == blend_parameter_ ? 0.1f : 0.002f;
  79. blend_mod_[i] += coefficient * (target - blend_mod_[i]);
  80. }
  81. // Determines if the blend knob has been touched.
  82. if (blend_knob_quantized_ == -1.0f) {
  83. blend_knob_quantized_ = knob_value;
  84. }
  85. blend_knob_touched_ = fabs(knob_value - blend_knob_quantized_) > 0.02f;
  86. if (blend_knob_touched_) {
  87. blend_knob_quantized_ = knob_value;
  88. }
  89. if (previous_blend_knob_value_ == -1.0f) {
  90. blend_[blend_parameter_] = knob_value;
  91. previous_blend_knob_value_ = knob_value;
  92. blend_knob_origin_ = knob_value;
  93. }
  94. float parameter_value = blend_[blend_parameter_];
  95. float delta = knob_value - previous_blend_knob_value_;
  96. float skew_ratio = delta > 0.0f
  97. ? (1.001f - parameter_value) / (1.001f - previous_blend_knob_value_)
  98. : (0.001f + parameter_value) / (0.001f + previous_blend_knob_value_);
  99. CONSTRAIN(skew_ratio, 0.1f, 10.0f);
  100. if (fabs(knob_value - blend_knob_origin_) < 0.02f) {
  101. delta = 0.0f;
  102. } else {
  103. blend_knob_origin_ = -1.0f;
  104. }
  105. parameter_value += skew_ratio * delta;
  106. CONSTRAIN(parameter_value, 0.0f, 1.0f);
  107. blend_[blend_parameter_] = parameter_value;
  108. previous_blend_knob_value_ = knob_value;
  109. }
  110. void CvScaler::Read(Parameters* parameters) {
  111. for (size_t i = 0; i < ADC_CHANNEL_LAST; ++i) {
  112. const CvTransformation& transformation = transformations_[i];
  113. float value = adc_.float_value(i);
  114. if (transformation.flip) {
  115. value = 1.0f - value;
  116. }
  117. if (transformation.remove_offset) {
  118. value -= calibration_data_->offset[i];
  119. }
  120. smoothed_adc_value_[i] += transformation.filter_coefficient * \
  121. (value - smoothed_adc_value_[i]);
  122. }
  123. parameters->position = smoothed_adc_value_[ADC_POSITION_POTENTIOMETER_CV];
  124. float texture = smoothed_adc_value_[ADC_TEXTURE_POTENTIOMETER];
  125. texture -= smoothed_adc_value_[ADC_TEXTURE_CV] * 2.0f;
  126. CONSTRAIN(texture, 0.0f, 1.0f);
  127. parameters->texture = texture;
  128. float density = smoothed_adc_value_[ADC_DENSITY_POTENTIOMETER_CV];
  129. CONSTRAIN(density, 0.0f, 1.0f);
  130. parameters->density = density;
  131. parameters->size = smoothed_adc_value_[ADC_SIZE_POTENTIOMETER];
  132. parameters->size -= smoothed_adc_value_[ADC_SIZE_CV];
  133. CONSTRAIN(parameters->size, 0.0f, 1.0f);
  134. UpdateBlendParameters(
  135. smoothed_adc_value_[ADC_BLEND_POTENTIOMETER],
  136. -smoothed_adc_value_[ADC_BLEND_CV] * 2.0f);
  137. float dry_wet = blend_[BLEND_PARAMETER_DRY_WET];
  138. dry_wet += blend_mod_[BLEND_PARAMETER_DRY_WET];
  139. dry_wet = dry_wet * 1.05f - 0.025f;
  140. CONSTRAIN(dry_wet, 0.0f, 1.0f);
  141. parameters->dry_wet = dry_wet;
  142. float reverb_amount = blend_[BLEND_PARAMETER_REVERB];
  143. reverb_amount += blend_mod_[BLEND_PARAMETER_REVERB];
  144. CONSTRAIN(reverb_amount, 0.0f, 1.0f);
  145. parameters->reverb = reverb_amount;
  146. float feedback = blend_[BLEND_PARAMETER_FEEDBACK];
  147. feedback += blend_mod_[BLEND_PARAMETER_FEEDBACK];
  148. CONSTRAIN(feedback, 0.0f, 1.0f);
  149. parameters->feedback = feedback;
  150. float stereo_spread = blend_[BLEND_PARAMETER_STEREO_SPREAD];
  151. stereo_spread += blend_mod_[BLEND_PARAMETER_STEREO_SPREAD];
  152. CONSTRAIN(stereo_spread, 0.0f, 1.0f);
  153. parameters->stereo_spread = stereo_spread;
  154. parameters->pitch = stmlib::Interpolate(
  155. lut_quantized_pitch,
  156. smoothed_adc_value_[ADC_PITCH_POTENTIOMETER],
  157. 1024.0f);
  158. float note = calibration_data_->pitch_offset;
  159. note += smoothed_adc_value_[ADC_V_OCT_CV] * calibration_data_->pitch_scale;
  160. if (fabs(note - note_) > 0.5f) {
  161. note_ = note;
  162. } else {
  163. ONE_POLE(note_, note, 0.2f)
  164. }
  165. parameters->pitch += note_;
  166. CONSTRAIN(parameters->pitch, -48.0f, 48.0f);
  167. gate_input_.Read();
  168. if (gate_input_.freeze_rising_edge()) {
  169. parameters->freeze = true;
  170. } else if (gate_input_.freeze_falling_edge()) {
  171. parameters->freeze = false;
  172. }
  173. parameters->trigger = previous_trigger_[0];
  174. parameters->gate = previous_gate_[0];
  175. for (int i = 0; i < kAdcLatency - 1; ++i) {
  176. previous_trigger_[i] = previous_trigger_[i + 1];
  177. previous_gate_[i] = previous_gate_[i + 1];
  178. }
  179. previous_trigger_[kAdcLatency - 1] = gate_input_.trigger_rising_edge();
  180. previous_gate_[kAdcLatency - 1] = gate_input_.gate();
  181. adc_.Convert();
  182. }
  183. } // namespace clouds