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.

119 lines
3.4KB

  1. // Copyright 2014 Emilie Gillet.
  2. //
  3. // Author: Emilie Gillet (emilie.o.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. // Discriminate an ADC signal into audio or CV ; and provide RMS stats.
  28. #pragma once
  29. #include <stmlib/stmlib.h>
  30. namespace streams
  31. {
  32. class AudioCvMeter
  33. {
  34. public:
  35. AudioCvMeter() { }
  36. ~AudioCvMeter() { }
  37. void Init()
  38. {
  39. peak_ = 0;
  40. zero_crossing_interval_ = 0;
  41. average_zero_crossing_interval_ = 0;
  42. previous_sample_ = 0;
  43. cv_ = false;
  44. }
  45. void Process(int32_t sample, uint32_t timestep_us)
  46. {
  47. if ((sample >> 1) * previous_sample_ < 0 ||
  48. zero_crossing_interval_ >= (4096L * kHardwareTimestep_us) / timestep_us)
  49. {
  50. int32_t error = zero_crossing_interval_ - average_zero_crossing_interval_;
  51. average_zero_crossing_interval_ += error >> 3;
  52. zero_crossing_interval_ = 0;
  53. }
  54. else
  55. {
  56. ++zero_crossing_interval_;
  57. }
  58. if (cv_ && average_zero_crossing_interval_ < (200L * kHardwareTimestep_us) / timestep_us)
  59. {
  60. cv_ = false;
  61. }
  62. else if (!cv_ && average_zero_crossing_interval_ > (400L * kHardwareTimestep_us) / timestep_us)
  63. {
  64. cv_ = true;
  65. }
  66. previous_sample_ = sample;
  67. if (sample < 0)
  68. {
  69. sample = -sample;
  70. }
  71. int32_t error = sample - peak_;
  72. int32_t coefficient = 33; // 250ms at 1kHz
  73. if (error > 0)
  74. {
  75. coefficient = 809; // 10ms at 1kHz
  76. }
  77. coefficient = (coefficient * timestep_us) / kHardwareTimestep_us;
  78. peak_ += error * coefficient >> 15;
  79. }
  80. inline bool cv() const
  81. {
  82. return cv_;
  83. }
  84. inline int32_t peak() const
  85. {
  86. return peak_;
  87. }
  88. private:
  89. // Hardware Streams updates its LEDs at 4kHz (250us period), but we may be
  90. // using a different rate here. We take this into account when filtering
  91. // or interval-counting so that the software LEDs feel the same.
  92. static constexpr uint32_t kHardwareTimestep_us = 250;
  93. bool cv_;
  94. int32_t peak_;
  95. int32_t zero_crossing_interval_;
  96. int32_t average_zero_crossing_interval_;
  97. int32_t previous_sample_;
  98. };
  99. } // namespace streams