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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. #if defined(KINETISK)
  29. static inline int32_t multiply_32x32_rshift30(int32_t a, int32_t b) __attribute__((always_inline));
  30. static inline int32_t multiply_32x32_rshift30(int32_t a, int32_t b)
  31. {
  32. return ((int64_t)a * (int64_t)b) >> 30;
  33. }
  34. //#define TONE_DETECT_FAST
  35. void AudioAnalyzeToneDetect::update(void)
  36. {
  37. audio_block_t *block;
  38. int32_t q0, q1, q2, coef;
  39. const int16_t *p, *end;
  40. uint16_t n;
  41. block = receiveReadOnly();
  42. if (!block) return;
  43. if (!enabled) {
  44. release(block);
  45. return;
  46. }
  47. p = block->data;
  48. end = p + AUDIO_BLOCK_SAMPLES;
  49. n = count;
  50. coef = coefficient;
  51. q1 = s1;
  52. q2 = s2;
  53. do {
  54. // the Goertzel algorithm is kinda magical ;-)
  55. #ifdef TONE_DETECT_FAST
  56. q0 = (*p++) + (multiply_32x32_rshift32_rounded(coef, q1) << 2) - q2;
  57. #else
  58. q0 = (*p++) + multiply_32x32_rshift30(coef, q1) - q2;
  59. // TODO: is this only 1 cycle slower? if so, always use it
  60. #endif
  61. q2 = q1;
  62. q1 = q0;
  63. if (--n == 0) {
  64. out1 = q1;
  65. out2 = q2;
  66. q1 = 0; // TODO: does clearing these help or hinder?
  67. q2 = 0;
  68. new_output = true;
  69. n = length;
  70. }
  71. } while (p < end);
  72. count = n;
  73. s1 = q1;
  74. s2 = q2;
  75. release(block);
  76. }
  77. void AudioAnalyzeToneDetect::set_params(int32_t coef, uint16_t cycles, uint16_t len)
  78. {
  79. __disable_irq();
  80. coefficient = coef;
  81. ncycles = cycles;
  82. length = len;
  83. count = len;
  84. s1 = 0;
  85. s2 = 0;
  86. enabled = true;
  87. __enable_irq();
  88. //Serial.printf("Tone: coef=%d, ncycles=%d, length=%d\n", coefficient, ncycles, length);
  89. }
  90. float AudioAnalyzeToneDetect::read(void)
  91. {
  92. int32_t coef, q1, q2, power;
  93. uint16_t len;
  94. __disable_irq();
  95. coef = coefficient;
  96. q1 = out1;
  97. q2 = out2;
  98. len = length;
  99. __enable_irq();
  100. #ifdef TONE_DETECT_FAST
  101. power = multiply_32x32_rshift32_rounded(q2, q2);
  102. power = multiply_accumulate_32x32_rshift32_rounded(power, q1, q1);
  103. power = multiply_subtract_32x32_rshift32_rounded(power,
  104. multiply_32x32_rshift30(q1, q2), coef);
  105. power <<= 4;
  106. #else
  107. int64_t power64;
  108. power64 = (int64_t)q2 * (int64_t)q2;
  109. power64 += (int64_t)q1 * (int64_t)q1;
  110. power64 -= (((int64_t)q1 * (int64_t)q2) >> 30) * (int64_t)coef;
  111. power = power64 >> 28;
  112. #endif
  113. return sqrtf((float)power) / (float)len;
  114. }
  115. AudioAnalyzeToneDetect::operator bool()
  116. {
  117. int32_t coef, q1, q2, power, trigger;
  118. uint16_t len;
  119. __disable_irq();
  120. coef = coefficient;
  121. q1 = out1;
  122. q2 = out2;
  123. len = length;
  124. __enable_irq();
  125. #ifdef TONE_DETECT_FAST
  126. power = multiply_32x32_rshift32_rounded(q2, q2);
  127. power = multiply_accumulate_32x32_rshift32_rounded(power, q1, q1);
  128. power = multiply_subtract_32x32_rshift32_rounded(power,
  129. multiply_32x32_rshift30(q1, q2), coef);
  130. power <<= 4;
  131. #else
  132. int64_t power64;
  133. power64 = (int64_t)q2 * (int64_t)q2;
  134. power64 += (int64_t)q1 * (int64_t)q1;
  135. power64 -= (((int64_t)q1 * (int64_t)q2) >> 30) * (int64_t)coef;
  136. power = power64 >> 28;
  137. #endif
  138. trigger = (uint32_t)len * thresh;
  139. trigger = multiply_32x32_rshift32(trigger, trigger);
  140. //Serial.printf("bool: power=%d, trig=%d\n", power, trigger);
  141. return (power >= trigger);
  142. // TODO: this should really remember if it's retuned true previously,
  143. // so it can give a single true response each time a tone is seen.
  144. }
  145. #elif defined(KINETISL)
  146. void AudioAnalyzeToneDetect::update(void)
  147. {
  148. audio_block_t *block;
  149. block = receiveReadOnly();
  150. if (block) release(block);
  151. }
  152. #endif