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.

analyze_tonedetect.cpp 4.5KB

10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* Audio Library for Teensy 3.X
  2. * Copyright (c) 2014, Paul Stoffregen, paul@pjrc.com
  3. *
  4. * Development of this audio library was funded by PJRC.COM, LLC by sales of
  5. * Teensy and Audio Adaptor boards. Please support PJRC's efforts to develop
  6. * open source software by purchasing Teensy or other PJRC products.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice, development funding notice, and this permission
  16. * notice shall be included in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include "analyze_tonedetect.h"
  27. #include "utility/dspinst.h"
  28. static inline int32_t multiply_32x32_rshift30(int32_t a, int32_t b) __attribute__((always_inline));
  29. static inline int32_t multiply_32x32_rshift30(int32_t a, int32_t b)
  30. {
  31. return ((int64_t)a * (int64_t)b) >> 30;
  32. }
  33. //#define TONE_DETECT_FAST
  34. void AudioAnalyzeToneDetect::update(void)
  35. {
  36. audio_block_t *block;
  37. int32_t q0, q1, q2, coef;
  38. const int16_t *p, *end;
  39. uint16_t n;
  40. block = receiveReadOnly();
  41. if (!block) return;
  42. if (!enabled) {
  43. release(block);
  44. return;
  45. }
  46. p = block->data;
  47. end = p + AUDIO_BLOCK_SAMPLES;
  48. n = count;
  49. coef = coefficient;
  50. q1 = s1;
  51. q2 = s2;
  52. do {
  53. // the Goertzel algorithm is kinda magical ;-)
  54. #ifdef TONE_DETECT_FAST
  55. q0 = (*p++) + (multiply_32x32_rshift32_rounded(coef, q1) << 2) - q2;
  56. #else
  57. q0 = (*p++) + multiply_32x32_rshift30(coef, q1) - q2;
  58. // TODO: is this only 1 cycle slower? if so, always use it
  59. #endif
  60. q2 = q1;
  61. q1 = q0;
  62. if (--n == 0) {
  63. out1 = q1;
  64. out2 = q2;
  65. q1 = 0; // TODO: does clearing these help or hinder?
  66. q2 = 0;
  67. new_output = true;
  68. n = length;
  69. }
  70. } while (p < end);
  71. count = n;
  72. s1 = q1;
  73. s2 = q2;
  74. release(block);
  75. }
  76. void AudioAnalyzeToneDetect::set_params(int32_t coef, uint16_t cycles, uint16_t len)
  77. {
  78. __disable_irq();
  79. coefficient = coef;
  80. ncycles = cycles;
  81. length = len;
  82. count = len;
  83. s1 = 0;
  84. s2 = 0;
  85. enabled = true;
  86. __enable_irq();
  87. //Serial.printf("Tone: coef=%d, ncycles=%d, length=%d\n", coefficient, ncycles, length);
  88. }
  89. float AudioAnalyzeToneDetect::read(void)
  90. {
  91. int32_t coef, q1, q2, power;
  92. uint16_t len;
  93. __disable_irq();
  94. coef = coefficient;
  95. q1 = out1;
  96. q2 = out2;
  97. len = length;
  98. __enable_irq();
  99. #ifdef TONE_DETECT_FAST
  100. power = multiply_32x32_rshift32_rounded(q2, q2);
  101. power = multiply_accumulate_32x32_rshift32_rounded(power, q1, q1);
  102. power = multiply_subtract_32x32_rshift32_rounded(power,
  103. multiply_32x32_rshift30(q1, q2), coef);
  104. power <<= 4;
  105. #else
  106. int64_t power64;
  107. power64 = (int64_t)q2 * (int64_t)q2;
  108. power64 += (int64_t)q1 * (int64_t)q1;
  109. power64 -= (((int64_t)q1 * (int64_t)q2) >> 30) * (int64_t)coef;
  110. power = power64 >> 28;
  111. #endif
  112. return sqrtf((float)power) / (float)len;
  113. }
  114. AudioAnalyzeToneDetect::operator bool()
  115. {
  116. int32_t coef, q1, q2, power, trigger;
  117. uint16_t len;
  118. __disable_irq();
  119. coef = coefficient;
  120. q1 = out1;
  121. q2 = out2;
  122. len = length;
  123. __enable_irq();
  124. #ifdef TONE_DETECT_FAST
  125. power = multiply_32x32_rshift32_rounded(q2, q2);
  126. power = multiply_accumulate_32x32_rshift32_rounded(power, q1, q1);
  127. power = multiply_subtract_32x32_rshift32_rounded(power,
  128. multiply_32x32_rshift30(q1, q2), coef);
  129. power <<= 4;
  130. #else
  131. int64_t power64;
  132. power64 = (int64_t)q2 * (int64_t)q2;
  133. power64 += (int64_t)q1 * (int64_t)q1;
  134. power64 -= (((int64_t)q1 * (int64_t)q2) >> 30) * (int64_t)coef;
  135. power = power64 >> 28;
  136. #endif
  137. trigger = (uint32_t)len * thresh;
  138. trigger = multiply_32x32_rshift32(trigger, trigger);
  139. Serial.printf("bool: power=%d, trig=%d\n", power, trigger);
  140. return (power >= trigger);
  141. // TODO: this should really remember if it's retuned true previously,
  142. // so it can give a single true response each time a tone is seen.
  143. }