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.

157 lines
4.2KB

  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. #ifndef CLOUDS_CV_SCALER_H_
  29. #define CLOUDS_CV_SCALER_H_
  30. #include "stmlib/stmlib.h"
  31. #include "clouds/settings.h"
  32. #include "clouds/drivers/adc.h"
  33. #include "clouds/drivers/gate_input.h"
  34. #include "clouds/dsp/parameters.h"
  35. namespace clouds {
  36. enum BlendParameter {
  37. BLEND_PARAMETER_DRY_WET,
  38. BLEND_PARAMETER_STEREO_SPREAD,
  39. BLEND_PARAMETER_FEEDBACK,
  40. BLEND_PARAMETER_REVERB,
  41. BLEND_PARAMETER_LAST
  42. };
  43. struct CvTransformation {
  44. bool flip;
  45. bool remove_offset;
  46. float filter_coefficient;
  47. };
  48. class CvScaler {
  49. public:
  50. CvScaler() { }
  51. ~CvScaler() { }
  52. void Init(CalibrationData* calibration_data);
  53. void Read(Parameters* parameters);
  54. void CalibrateC1() {
  55. cv_c1_ = adc_.float_value(ADC_V_OCT_CV);
  56. }
  57. void CalibrateOffsets() {
  58. for (size_t i = 0; i < ADC_CHANNEL_LAST; ++i) {
  59. calibration_data_->offset[i] = adc_.float_value(i);
  60. }
  61. }
  62. bool CalibrateC3() {
  63. float c3 = adc_.float_value(ADC_V_OCT_CV); // 0.4848 v0.1 ; 0.3640 v0.2
  64. float c1 = cv_c1_; // 0.6666 v0.1 ; 0.6488 v0.2
  65. float delta = c3 - c1;
  66. if (delta > -0.5f && delta < -0.0f) {
  67. calibration_data_->pitch_scale = 24.0f / (c3 - c1);
  68. calibration_data_->pitch_offset = 12.0f - \
  69. calibration_data_->pitch_scale * c1;
  70. return true;
  71. } else {
  72. return false;
  73. }
  74. }
  75. inline uint8_t adc_value(size_t index) const {
  76. return adc_.value(index) >> 8;
  77. }
  78. inline bool gate(size_t index) const {
  79. return index == 0 ? gate_input_.freeze() : gate_input_.trigger();
  80. }
  81. inline void set_blend_parameter(BlendParameter parameter) {
  82. blend_parameter_ = parameter;
  83. blend_knob_origin_ = previous_blend_knob_value_;
  84. }
  85. inline void MatchKnobPosition() {
  86. previous_blend_knob_value_ = -1.0f;
  87. }
  88. inline BlendParameter blend_parameter() const {
  89. return blend_parameter_;
  90. }
  91. inline float blend_value(BlendParameter parameter) const {
  92. return blend_[parameter];
  93. }
  94. inline void set_blend_value(BlendParameter parameter, float value) {
  95. blend_[parameter] = value;
  96. }
  97. inline bool blend_knob_touched() const {
  98. return blend_knob_touched_;
  99. }
  100. void UnlockBlendKnob() {
  101. previous_blend_knob_value_ = -1.0f;
  102. }
  103. private:
  104. void UpdateBlendParameters(float knob, float cv);
  105. static const int kAdcLatency = 5;
  106. Adc adc_;
  107. GateInput gate_input_;
  108. CalibrationData* calibration_data_;
  109. float smoothed_adc_value_[ADC_CHANNEL_LAST];
  110. static CvTransformation transformations_[ADC_CHANNEL_LAST];
  111. float note_;
  112. BlendParameter blend_parameter_;
  113. float blend_[BLEND_PARAMETER_LAST];
  114. float blend_mod_[BLEND_PARAMETER_LAST];
  115. float previous_blend_knob_value_;
  116. float blend_knob_origin_;
  117. float blend_knob_quantized_;
  118. bool blend_knob_touched_;
  119. float cv_c1_;
  120. bool previous_trigger_[kAdcLatency];
  121. bool previous_gate_[kAdcLatency];
  122. DISALLOW_COPY_AND_ASSIGN(CvScaler);
  123. };
  124. } // namespace clouds
  125. #endif // CLOUDS_CV_SCALER_H_