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.

194 lines
5.4KB

  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 WARPS_CV_SCALER_H_
  29. #define WARPS_CV_SCALER_H_
  30. #include "stmlib/stmlib.h"
  31. #include "warps/drivers/adc.h"
  32. #include "warps/drivers/codec.h"
  33. #include "warps/drivers/normalization_probe.h"
  34. #include "warps/settings.h"
  35. namespace warps {
  36. class Parameters;
  37. class NormalizationDetector {
  38. public:
  39. NormalizationDetector() { }
  40. ~NormalizationDetector() { }
  41. void Init(float lp_coefficient, float threshold) {
  42. score_ = 0.0f;
  43. state_ = false;
  44. threshold_ = threshold;
  45. lp_coefficient_ = lp_coefficient;
  46. }
  47. void Process(float x, float y) {
  48. // x is supposed to be centered!
  49. score_ += lp_coefficient_ * (x * y - score_);
  50. float hysteresis = state_ ? -0.05f * threshold_ : 0.0f;
  51. state_ = score_ >= (threshold_ + hysteresis);
  52. }
  53. inline bool normalized() const { return state_; }
  54. inline float score() const { return score_; }
  55. private:
  56. float score_;
  57. float lp_coefficient_;
  58. float threshold_;
  59. bool state_;
  60. DISALLOW_COPY_AND_ASSIGN(NormalizationDetector);
  61. };
  62. class CvScaler {
  63. public:
  64. CvScaler() { }
  65. ~CvScaler() { }
  66. void Init(CalibrationData* calibration_data);
  67. void Read(Parameters* parameters);
  68. void DetectAudioNormalization(Codec::Frame* in, size_t size);
  69. void StartCalibration() {
  70. normalization_probe_enabled_ = false;
  71. normalization_probe_forced_state_ = false;
  72. }
  73. void CalibrateC1() {
  74. cv_c1_ = adc_.float_value(ADC_LEVEL_1_CV);
  75. }
  76. void CalibrateOffsets() {
  77. for (size_t i = 1; i < 4; ++i) {
  78. calibration_data_->offset[i] = adc_.float_value(i);
  79. }
  80. }
  81. bool CalibrateC3() {
  82. float c3 = adc_.float_value(ADC_LEVEL_1_CV); // 0.5818181818181818
  83. float c1 = cv_c1_; // 0.8
  84. float delta = c3 - c1;
  85. bool success = delta > -0.4f && delta < -0.1f;
  86. if (success) {
  87. calibration_data_->pitch_scale = 24.0f / (c3 - c1);
  88. calibration_data_->pitch_offset = 12.0f - \
  89. calibration_data_->pitch_scale * c1;
  90. calibration_data_->offset[0] = c1 + 0.5f * (c1 - c3);
  91. }
  92. normalization_probe_enabled_ = true;
  93. return success;
  94. }
  95. void StartNormalizationCalibration() {
  96. normalization_probe_enabled_ = false;
  97. normalization_probe_forced_state_ = false;
  98. }
  99. void CalibrateLow() {
  100. cv_low_[0] = adc_.float_value(ADC_LEVEL_1_CV);
  101. cv_low_[1] = adc_.float_value(ADC_LEVEL_2_CV);
  102. normalization_probe_forced_state_ = true;
  103. }
  104. bool CalibrateHigh() {
  105. bool success = true;
  106. for (int i = 0; i < 2; ++i) {
  107. float high = adc_.float_value(ADC_LEVEL_1_CV + i);
  108. float threshold = (cv_low_[i] + high) * 0.5f;
  109. bool within_range = threshold >= 0.8f && threshold <= 0.9f;
  110. if (within_range) {
  111. calibration_data_->normalization_detection_threshold[i] = threshold;
  112. }
  113. success = success && within_range;
  114. }
  115. normalization_probe_enabled_ = true;
  116. return success;
  117. }
  118. inline bool ready_for_calibration() const {
  119. return adc_.float_value(ADC_LEVEL_2_CV) > 0.8f && \
  120. adc_.float_value(ADC_LEVEL_1_CV) > 0.7f;
  121. }
  122. inline uint8_t easter_egg_digit() const {
  123. if (lp_state_[ADC_LEVEL_1_POT] < 0.05f && \
  124. lp_state_[ADC_LEVEL_2_POT] < 0.05f && \
  125. lp_state_[ADC_PARAMETER_POT] < 0.05f) {
  126. return static_cast<uint8_t>(
  127. UnwrapPot(lp_state_[ADC_ALGORITHM_POT]) * 8.0f + 0.5f);
  128. } else {
  129. return 0;
  130. }
  131. }
  132. inline uint8_t adc_value(size_t index) const {
  133. return adc_.value(index) >> 8;
  134. }
  135. inline uint8_t normalization(size_t index) const {
  136. return normalization_detector_[index].normalized() ? 255 : 0;
  137. }
  138. float UnwrapPot(float x) const;
  139. private:
  140. void DetectNormalization();
  141. Adc adc_;
  142. CalibrationData* calibration_data_;
  143. NormalizationProbe normalization_probe_;
  144. NormalizationDetector normalization_detector_[4];
  145. bool normalization_probe_value_[2];
  146. bool normalization_probe_enabled_;
  147. bool normalization_probe_forced_state_;
  148. float lp_state_[ADC_LAST];
  149. float note_cv_;
  150. float note_pot_;
  151. float note_pot_quantized_;
  152. float cv_c1_;
  153. float cv_low_[2];
  154. DISALLOW_COPY_AND_ASSIGN(CvScaler);
  155. };
  156. } // namespace warps
  157. #endif // WARPS_CV_SCALER_H_