Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

117 Zeilen
4.3KB

  1. /* Audio Library for Teensy 3.X
  2. * Copyright (c) 2015, Colin Duffy
  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 AudioTuner_h_
  27. #define AudioTuner_h_
  28. #include "AudioStream.h"
  29. /****************************************************************/
  30. #define SAMPLE_RATE_DIVIDE_BY_1 1 // 44100 sample rate
  31. #define SAMPLE_RATE_DIVIDE_BY_2 2 // 22050 sample rate
  32. #define SAMPLE_RATE_DIVIDE_BY_4 4 // 11025 sample rate
  33. #define SAMPLE_RATE_DIVIDE_BY_8 8 // 5512.5 sample rate
  34. #define SAMPLE_RATE_DIVIDE_BY_16 16 // 2756.25 sample rate
  35. #define SAMPLE_RATE_DIVIDE_BY_32 32 // 1378.125 sample rate
  36. /****************************************************************
  37. * Safe to adjust these values below *
  38. ****************************************************************/
  39. // Adjust number of samples to collect in buffer here, also effects
  40. // convergence speed and resolution.
  41. #define NUM_SAMPLES 2048 // make a power of two
  42. // larger the divide-by, less resolution and lower the frequency for
  43. // a given number of samples that can be detected. Also effects
  44. // convergence speed.
  45. #define SAMPLE_SKIP SAMPLE_RATE_DIVIDE_BY_2
  46. /****************************************************************/
  47. class AudioTuner : public AudioStream
  48. {
  49. public:
  50. AudioTuner( void ) : AudioStream( 1, inputQueueArray ),
  51. enabled( false ), new_output(false),
  52. next_buffer(1), process_buffer(false),
  53. running_sum(0), block_count(0),
  54. yin_idx(1)
  55. {
  56. set_params( 0.05f );// threshold set 15ms
  57. }
  58. /**
  59. * sets threshold value and window length
  60. *
  61. * @param thresh
  62. */
  63. void set_params( float thresh );
  64. /**
  65. * triggers true when valid frequency is found
  66. *
  67. * @return flag to indicate valid frequency is found
  68. */
  69. bool available( void );
  70. /**
  71. * get frequency
  72. *
  73. * @return frequency in hertz
  74. */
  75. float read( void );
  76. /**
  77. * get predicitity
  78. *
  79. * @return probability of correct freq found
  80. */
  81. float probability( void );
  82. /**
  83. * Audio Library calls this update function ~2.9ms
  84. */
  85. virtual void update( void );
  86. private:
  87. /**
  88. * check the sampled data for fundmental frequency
  89. *
  90. * @param yin buffer to hold sum*tau value
  91. * @param rs buffer to hold running sum for sampled window
  92. * @param head buffer index
  93. * @param tau lag we are currently working on this gets incremented
  94. *
  95. * @return tau
  96. */
  97. uint16_t estimate( int64_t *yin, int64_t *rs, uint16_t head, uint16_t tau );
  98. int16_t buffer[NUM_SAMPLES*2] __attribute__ ( ( aligned ( 4 ) ) );
  99. float periodicity, threshold, data;
  100. int64_t rs_buffer[5], yin_buffer[5];
  101. uint64_t running_sum;
  102. uint16_t block_count, tau_global;
  103. uint8_t next_buffer, yin_idx;
  104. bool enabled, process_buffer;
  105. volatile bool new_output;
  106. audio_block_t *inputQueueArray[1];
  107. };
  108. #endif