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.

analyze_tonedetect.h 2.8KB

10 år sedan
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. #ifndef analyze_tonedetect_h_
  27. #define analyze_tonedetect_h_
  28. #include "Arduino.h"
  29. #include "AudioStream.h"
  30. class AudioAnalyzeToneDetect : public AudioStream
  31. {
  32. public:
  33. AudioAnalyzeToneDetect(void)
  34. : AudioStream(1, inputQueueArray), thresh(6554), enabled(false) { }
  35. void frequency(float freq, uint16_t cycles=10) {
  36. set_params((int32_t)(cos((double)freq
  37. * (2.0 * 3.14159265358979323846 / AUDIO_SAMPLE_RATE_EXACT))
  38. * (double)2147483647.999), cycles,
  39. (float)AUDIO_SAMPLE_RATE_EXACT / freq * (float)cycles + 0.5f);
  40. }
  41. void set_params(int32_t coef, uint16_t cycles, uint16_t len);
  42. bool available(void) {
  43. __disable_irq();
  44. bool flag = new_output;
  45. if (flag) new_output = false;
  46. __enable_irq();
  47. return flag;
  48. }
  49. float read(void);
  50. void threshold(float level) {
  51. if (level < 0.01f) thresh = 655;
  52. else if (level > 0.99f) thresh = 64881;
  53. else thresh = level * 65536.0f + 0.5f;
  54. }
  55. operator bool(); // true if at or above threshold, false if below
  56. virtual void update(void);
  57. private:
  58. int32_t coefficient; // Goertzel algorithm coefficient
  59. int32_t s1, s2; // Goertzel algorithm state
  60. int32_t out1, out2; // Goertzel algorithm state output
  61. uint16_t length; // number of samples to analyze
  62. uint16_t count; // how many left to analyze
  63. uint16_t ncycles; // number of waveform cycles to seek
  64. uint16_t thresh; // threshold, 655 to 64881 (1% to 99%)
  65. bool enabled;
  66. volatile bool new_output;
  67. audio_block_t *inputQueueArray[1];
  68. };
  69. #endif