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.

179 lines
6.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. #include "warps/cv_scaler.h"
  29. #include <algorithm>
  30. #include <cmath>
  31. #include "stmlib/dsp/dsp.h"
  32. #include "stmlib/system/storage.h"
  33. #include "stmlib/utils/random.h"
  34. #include "warps/dsp/parameters.h"
  35. #include "warps/resources.h"
  36. namespace warps {
  37. using namespace std;
  38. using namespace stmlib;
  39. void CvScaler::Init(CalibrationData* calibration_data) {
  40. calibration_data_ = calibration_data;
  41. adc_.Init();
  42. normalization_probe_.Init();
  43. note_cv_ = 0.0f;
  44. note_pot_ = 0.0f;
  45. fill(&lp_state_[0], &lp_state_[ADC_LAST], 0.0f);
  46. for (int32_t i = 0; i < 4; ++i) {
  47. normalization_detector_[i].Init(0.01f, 0.5f);
  48. }
  49. normalization_probe_enabled_ = true;
  50. }
  51. float CvScaler::UnwrapPot(float x) const {
  52. return Interpolate(lut_pot_curve, x, 512.0f);
  53. }
  54. #define BIND(destination, NAME, unwrap, scale, lp_coefficient, attenuate) \
  55. { \
  56. lp_state_[ADC_ ## NAME ## _POT] += 0.33f * lp_coefficient * (adc_.float_value(ADC_ ## NAME ## _POT) - lp_state_[ADC_ ## NAME ## _POT]); \
  57. lp_state_[ADC_ ## NAME ## _CV] += lp_coefficient * (adc_.float_value(ADC_ ## NAME ## _CV) - lp_state_[ADC_ ## NAME ## _CV]); \
  58. float pot = lp_state_[ADC_ ## NAME ## _POT]; \
  59. if (unwrap) pot = UnwrapPot(pot); \
  60. float cv = calibration_data_->offset[ADC_ ## NAME ## _CV] - lp_state_[ADC_ ## NAME ## _CV]; \
  61. float value = attenuate ? (pot * pot * cv * scale) : (pot + cv * scale); \
  62. CONSTRAIN(value, 0.0f, 1.0f); \
  63. destination = value; \
  64. }
  65. void CvScaler::DetectNormalization() {
  66. // Check if the value read by the ADC is correlated with the noise sent to the
  67. // switch. If so, we can conclude that nothing is connected in the input.
  68. float x = normalization_probe_value_[1] ? 1.0f : -1.0f;
  69. for (size_t i = 0; i < 2; ++i) {
  70. float y = adc_.float_value(ADC_LEVEL_1_CV + i) - calibration_data_->normalization_detection_threshold[i];
  71. if (y > -0.1f && y < 0.1f) {
  72. y = y > 0.0f ? -1.0f : 1.0f;
  73. } else {
  74. y = 0.0f;
  75. }
  76. normalization_detector_[i].Process(x, y);
  77. }
  78. // Flip normalization probe for next round.
  79. normalization_probe_value_[1] = normalization_probe_value_[0];
  80. normalization_probe_value_[0] = (Random::GetWord() >> 31) & 1;
  81. bool new_state = normalization_probe_enabled_
  82. ? normalization_probe_value_[0]
  83. : normalization_probe_forced_state_;
  84. normalization_probe_.Write(new_state);
  85. }
  86. void CvScaler::DetectAudioNormalization(Codec::Frame* in, size_t size) {
  87. for (int32_t channel = 0; channel < 2; ++channel) {
  88. int32_t count = 0;
  89. short* input_samples = &in->l + channel;
  90. for (size_t i = 0; i < size; i += 16) {
  91. short s = input_samples[i * 2];
  92. if (s > 50 && s < 1500) {
  93. ++count;
  94. } else if (s > -1500 && s < -50) {
  95. --count;
  96. }
  97. }
  98. float y = static_cast<float>(count) / static_cast<float>(size >> 4);
  99. float x = normalization_probe_value_[0] ? -1.0f : 1.0f;
  100. normalization_detector_[channel + 2].Process(x, y);
  101. if (normalization_detector_[channel + 2].normalized()) {
  102. for (size_t i = 0; i < size; ++i) {
  103. input_samples[i * 2] = 0;
  104. }
  105. }
  106. }
  107. }
  108. void CvScaler::Read(Parameters* p) {
  109. // Modulation parameters.
  110. BIND(p->channel_drive[0], LEVEL_1, false, 1.6f, 0.08f, true);
  111. BIND(p->channel_drive[1], LEVEL_2, false, 1.6f, 0.08f, true);
  112. BIND(p->modulation_algorithm, ALGORITHM, true, 2.0f, 0.08f, false);
  113. BIND(p->modulation_parameter, PARAMETER, false, 2.0f, 0.08f, false);
  114. // Prevent wavefolder bleed caused by a slight offset in the pot or ADC.
  115. if (p->modulation_algorithm <= 0.125f) {
  116. p->modulation_algorithm = p->modulation_algorithm * 1.08f - 0.01f;
  117. CONSTRAIN(p->modulation_algorithm, 0.0f, 1.0f);
  118. }
  119. // Easter egg parameter mappings.
  120. p->frequency_shift_pot = lp_state_[ADC_ALGORITHM_POT];
  121. float frequency_shift_cv = -lp_state_[ADC_ALGORITHM_CV];
  122. frequency_shift_cv += calibration_data_->offset[ADC_ALGORITHM_CV];
  123. p->frequency_shift_cv = frequency_shift_cv * 2.0f;
  124. CONSTRAIN(p->frequency_shift_cv, -1.0f, 1.0f);
  125. float phase_shift = lp_state_[ADC_ALGORITHM_POT] + frequency_shift_cv * 2.0f;
  126. CONSTRAIN(phase_shift, 0.0f, 1.0f);
  127. p->phase_shift = phase_shift;
  128. // Internal oscillator parameters.
  129. float note;
  130. note = calibration_data_->pitch_offset;
  131. note += adc_.float_value(ADC_LEVEL_1_CV) * calibration_data_->pitch_scale;
  132. float interval = note - note_cv_;
  133. if (interval < -0.4f || interval > 0.4f) {
  134. note_cv_ = note;
  135. } else {
  136. note_cv_ += 0.1f * interval;
  137. }
  138. note = 60.0f * adc_.float_value(ADC_LEVEL_1_POT) + 12.0f;
  139. note_pot_ += 0.1f * (note - note_pot_);
  140. p->note = note_pot_ + note_cv_;
  141. DetectNormalization();
  142. for (int32_t i = 0; i < 2; ++i) {
  143. if (normalization_detector_[i].normalized()) {
  144. float pot = lp_state_[ADC_LEVEL_1_POT + i];
  145. p->channel_drive[i] = pot * pot;
  146. }
  147. }
  148. if (normalization_detector_[0].normalized()) {
  149. p->note = note_pot_ + 24.0f;
  150. }
  151. adc_.Convert();
  152. }
  153. } // namespace warps