您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

112 行
3.9KB

  1. /* Audio Library Guitar and Bass Tuner
  2. * Copyright (c) 2015, Colin Duffy
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice, development funding notice, and this permission
  12. * notice shall be included in all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. */
  22. #ifndef AudioTuner_h_
  23. #define AudioTuner_h_
  24. #include "AudioStream.h"
  25. /****************************************************************/
  26. #define SAMPLE_RATE_DIVIDE_BY_1 1 // 44100 sample rate
  27. #define SAMPLE_RATE_DIVIDE_BY_2 2 // 22050 sample rate
  28. #define SAMPLE_RATE_DIVIDE_BY_4 4 // 11025 sample rate
  29. #define SAMPLE_RATE_DIVIDE_BY_8 8 // 5512.5 sample rate
  30. #define SAMPLE_RATE_DIVIDE_BY_16 16 // 2756.25 sample rate
  31. #define SAMPLE_RATE_DIVIDE_BY_32 32 // 1378.125 sample rate
  32. /****************************************************************
  33. * Safe to adjust these values below *
  34. ****************************************************************/
  35. // Adjust number of samples to collect in buffer here, also effects
  36. // convergence speed and resolution.
  37. #define NUM_SAMPLES 2048 // make a power of two
  38. // larger the divide-by, less resolution and lower the frequency for
  39. // a given number of samples that can be detected. Also effects
  40. // convergence speed.
  41. #define SAMPLE_SKIP SAMPLE_RATE_DIVIDE_BY_2
  42. /****************************************************************/
  43. class AudioTuner : public AudioStream
  44. {
  45. public:
  46. /**
  47. * constructor to setup Audio Library and initialize
  48. *
  49. * @return none
  50. */
  51. AudioTuner( void ) : AudioStream( 1, inputQueueArray ), enabled( false ), new_output(false){ }
  52. /**
  53. * sets threshold value
  54. *
  55. * @param thresh
  56. */
  57. void set_threshold( float thresh );
  58. /**
  59. * triggers true when valid frequency is found
  60. *
  61. * @return flag to indicate valid frequency is found
  62. */
  63. bool available( void );
  64. /**
  65. * get frequency
  66. *
  67. * @return frequency in hertz
  68. */
  69. float read( void );
  70. /**
  71. * get predicitity
  72. *
  73. * @return probability of correct freq found
  74. */
  75. float probability( void );
  76. /**
  77. * Audio Library calls this update function ~2.9ms
  78. */
  79. virtual void update( void );
  80. private:
  81. /**
  82. * check the sampled data for fundmental frequency
  83. *
  84. * @param yin buffer to hold sum*tau value
  85. * @param rs buffer to hold running sum for sampled window
  86. * @param head buffer index
  87. * @param tau lag we are currently working on this gets incremented
  88. *
  89. * @return tau
  90. */
  91. uint16_t estimate( int64_t *yin, int64_t *rs, uint16_t head, uint16_t tau );
  92. int16_t buffer[NUM_SAMPLES*2] __attribute__ ( ( aligned ( 4 ) ) );
  93. float periodicity, threshold, data;
  94. int64_t rs_buffer[5], yin_buffer[5];
  95. uint64_t running_sum;
  96. uint16_t block_count, tau_global;
  97. uint8_t next_buffer, yin_idx;
  98. bool enabled, process_buffer;
  99. volatile bool new_output;
  100. audio_block_t *inputQueueArray[1];
  101. };
  102. #endif