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.

96 lines
2.8KB

  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. // Discriminate an ADC signal into audio or CV ; and provide RMS stats.
  28. #ifndef STREAMS_AUDIO_CV_METER_H_
  29. #define STREAMS_AUDIO_CV_METER_H_
  30. #include "stmlib/stmlib.h"
  31. namespace streams {
  32. class AudioCvMeter {
  33. public:
  34. AudioCvMeter() { }
  35. ~AudioCvMeter() { }
  36. void Init() {
  37. peak_ = 0;
  38. zero_crossing_interval_ = 0;
  39. average_zero_crossing_interval_ = 0;
  40. previous_sample_ = 0;
  41. cv_ = false;
  42. }
  43. void Process(int32_t sample) {
  44. if ((sample >> 1) * previous_sample_ < 0 ||
  45. zero_crossing_interval_ >= 4096) {
  46. int32_t error = zero_crossing_interval_ - average_zero_crossing_interval_;
  47. average_zero_crossing_interval_ += error >> 3;
  48. zero_crossing_interval_ = 0;
  49. } else {
  50. ++zero_crossing_interval_;
  51. }
  52. if (cv_ && average_zero_crossing_interval_ < 200) {
  53. cv_ = false;
  54. } else if (!cv_ && average_zero_crossing_interval_ > 400) {
  55. cv_ = true;
  56. }
  57. previous_sample_ = sample;
  58. if (sample < 0) {
  59. sample = -sample;
  60. }
  61. int32_t error = sample - peak_;
  62. int32_t coefficient = 33; // 250ms at 1kHz
  63. if (error > 0) {
  64. coefficient = 809; // 10ms at 1kHz
  65. }
  66. peak_ += error * coefficient >> 15;
  67. }
  68. inline bool cv() const { return cv_; }
  69. inline int32_t peak() const { return peak_; }
  70. private:
  71. bool cv_;
  72. int32_t peak_;
  73. int32_t zero_crossing_interval_;
  74. int32_t average_zero_crossing_interval_;
  75. int32_t previous_sample_;
  76. DISALLOW_COPY_AND_ASSIGN(AudioCvMeter);
  77. };
  78. } // namespace streams
  79. #endif // STREAMS_AUDIO_CV_METER_H_