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.

47 lines
1.4KB

  1. #ifndef analyze_tonedetect_h_
  2. #define analyze_tonedetect_h_
  3. #include "AudioStream.h"
  4. class AudioAnalyzeToneDetect : public AudioStream
  5. {
  6. public:
  7. AudioAnalyzeToneDetect(void)
  8. : AudioStream(1, inputQueueArray), thresh(6554), enabled(false) { }
  9. void frequency(float freq, uint16_t cycles=10) {
  10. set_params((int32_t)(cos((double)freq
  11. * (2.0 * 3.14159265358979323846 / AUDIO_SAMPLE_RATE_EXACT))
  12. * (double)2147483647.999), cycles,
  13. (float)AUDIO_SAMPLE_RATE_EXACT / freq * (float)cycles + 0.5f);
  14. }
  15. void set_params(int32_t coef, uint16_t cycles, uint16_t len);
  16. bool available(void) {
  17. __disable_irq();
  18. bool flag = new_output;
  19. if (flag) new_output = false;
  20. __enable_irq();
  21. return flag;
  22. }
  23. float read(void);
  24. void threshold(float level) {
  25. if (level < 0.01f) thresh = 655;
  26. else if (level > 0.99f) thresh = 64881;
  27. else thresh = level * 65536.0f + 0.5f;
  28. }
  29. operator bool(); // true if at or above threshold, false if below
  30. virtual void update(void);
  31. private:
  32. int32_t coefficient; // Goertzel algorithm coefficient
  33. int32_t s1, s2; // Goertzel algorithm state
  34. int32_t out1, out2; // Goertzel algorithm state output
  35. uint16_t length; // number of samples to analyze
  36. uint16_t count; // how many left to analyze
  37. uint16_t ncycles; // number of waveform cycles to seek
  38. uint16_t thresh; // threshold, 655 to 64881 (1% to 99%)
  39. bool enabled;
  40. volatile bool new_output;
  41. audio_block_t *inputQueueArray[1];
  42. };
  43. #endif