Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

172 lines
4.6KB

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