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.

165 lines
5.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. // CV scaler.
  28. #ifndef ELEMENTS_CV_SCALER_H_
  29. #define ELEMENTS_CV_SCALER_H_
  30. #include "stmlib/stmlib.h"
  31. #include "elements/drivers/cv_adc.h"
  32. #include "elements/drivers/pots_adc.h"
  33. #include "elements/drivers/gate_input.h"
  34. namespace elements {
  35. struct CalibrationSettings {
  36. float pitch_offset;
  37. float pitch_scale;
  38. float offset[CV_ADC_CHANNEL_LAST];
  39. uint8_t boot_in_easter_egg_mode;
  40. uint8_t padding[63];
  41. };
  42. class Patch;
  43. class PerformanceState;
  44. enum Law {
  45. LAW_LINEAR,
  46. LAW_QUADRATIC_BIPOLAR,
  47. LAW_QUARTIC_BIPOLAR,
  48. LAW_QUANTIZED_NOTE
  49. };
  50. class CvScaler {
  51. public:
  52. CvScaler() { }
  53. ~CvScaler() { }
  54. void Init();
  55. void Read(Patch* patch, PerformanceState* state);
  56. bool ready_for_calibration() {
  57. return pot_lp_[POT_EXCITER_BOW_TIMBRE_ATTENUVERTER] < -3.0f &&
  58. pot_lp_[POT_EXCITER_BLOW_META_ATTENUVERTER] < -3.0f &&
  59. pot_lp_[POT_EXCITER_BLOW_TIMBRE_ATTENUVERTER] < -3.0f &&
  60. pot_lp_[POT_EXCITER_STRIKE_META_ATTENUVERTER] < -3.0f &&
  61. pot_lp_[POT_EXCITER_STRIKE_TIMBRE_ATTENUVERTER] < -3.0f &&
  62. pot_lp_[POT_RESONATOR_GEOMETRY_ATTENUVERTER] < -3.0f &&
  63. pot_lp_[POT_RESONATOR_BRIGHTNESS_ATTENUVERTER] < -3.0f &&
  64. pot_lp_[POT_RESONATOR_DAMPING_ATTENUVERTER] < -3.0f &&
  65. pot_lp_[POT_RESONATOR_POSITION_ATTENUVERTER] < -3.0f &&
  66. pot_lp_[POT_SPACE_ATTENUVERTER] < -3.0f;
  67. }
  68. bool easter_egg() {
  69. return pot_lp_[POT_EXCITER_BOW_TIMBRE_ATTENUVERTER] < -3.0f &&
  70. pot_lp_[POT_EXCITER_BLOW_META_ATTENUVERTER] < -3.0f &&
  71. pot_lp_[POT_EXCITER_BLOW_TIMBRE_ATTENUVERTER] < -3.0f &&
  72. pot_lp_[POT_EXCITER_STRIKE_META_ATTENUVERTER] < -3.0f &&
  73. pot_lp_[POT_EXCITER_STRIKE_TIMBRE_ATTENUVERTER] < -3.0f &&
  74. pot_lp_[POT_EXCITER_BLOW_TIMBRE] < 0.2f &&
  75. pot_lp_[POT_EXCITER_BOW_TIMBRE] < 0.2f &&
  76. pot_lp_[POT_EXCITER_STRIKE_TIMBRE] < 0.2f &&
  77. pot_lp_[POT_RESONATOR_GEOMETRY_ATTENUVERTER] > 3.0f &&
  78. pot_lp_[POT_RESONATOR_BRIGHTNESS_ATTENUVERTER] > 3.0f &&
  79. pot_lp_[POT_RESONATOR_DAMPING_ATTENUVERTER] > 3.0f &&
  80. pot_lp_[POT_RESONATOR_POSITION_ATTENUVERTER] > 3.0f &&
  81. pot_lp_[POT_SPACE_ATTENUVERTER] > 3.0f &&
  82. pot_lp_[POT_RESONATOR_DAMPING] > 0.8f &&
  83. pot_lp_[POT_RESONATOR_POSITION] > 0.8f &&
  84. pot_lp_[POT_SPACE] > 0.8f;
  85. }
  86. void CalibrateC1() {
  87. cv_c1_ = cv_.float_value(CV_ADC_V_OCT);
  88. }
  89. void CalibrateOffsets() {
  90. for (size_t i = 0; i < CV_ADC_CHANNEL_LAST; ++i) {
  91. calibration_settings_.offset[i] = cv_.float_value(i);
  92. }
  93. }
  94. bool CalibrateC3() {
  95. float c3 = cv_.float_value(CV_ADC_V_OCT); // 0.4848 v0.1 ; 0.3640 v0.2
  96. float c1 = cv_c1_; // 0.6666 v0.1 ; 0.6488 v0.2
  97. float delta = c3 - c1;
  98. if (delta > -0.4f && delta < -0.1f) {
  99. calibration_settings_.pitch_scale = 24.0f / (c3 - c1);
  100. calibration_settings_.pitch_offset = 12.0f - \
  101. calibration_settings_.pitch_scale * c1;
  102. return true;
  103. }
  104. return false;
  105. }
  106. void SaveCalibration();
  107. inline bool gate() const { return gate_; }
  108. inline uint8_t pot_value(size_t index) const {
  109. return pots_.value(index) >> 8;
  110. }
  111. inline uint8_t cv_value(size_t index) const {
  112. return cv_.value(index) >> 8;
  113. // return static_cast<uint8_t>(cv_.float_value(index) * 256.0f);
  114. }
  115. inline bool boot_in_easter_egg_mode() const {
  116. return calibration_settings_.boot_in_easter_egg_mode;
  117. }
  118. inline void set_boot_in_easter_egg_mode(bool boot_in_easter_egg_mode) {
  119. calibration_settings_.boot_in_easter_egg_mode = boot_in_easter_egg_mode;
  120. }
  121. private:
  122. void ReadPanelPots();
  123. PotsAdc pots_;
  124. CvAdc cv_;
  125. CalibrationSettings calibration_settings_;
  126. GateInput gate_input_;
  127. bool gate_;
  128. bool previous_gate_;
  129. float pot_raw_[POT_LAST];
  130. float pot_lp_[POT_LAST];
  131. float pot_quantized_[POT_LAST];
  132. float note_;
  133. float modulation_;
  134. float cv_c1_;
  135. uint16_t version_token_;
  136. static Law law_[POT_LAST];
  137. DISALLOW_COPY_AND_ASSIGN(CvScaler);
  138. };
  139. } // namespace elements
  140. #endif // ELEMENTS_CV_SCALER_H_