Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

158 linhas
5.0KB

  1. /* Audio Library Note Frequency Detection & Guitar/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. #include "arm_math.h"
  26. /***********************************************************************
  27. * Safe to adjust these values below *
  28. * *
  29. * This parameter defines the size of the buffer. *
  30. * *
  31. * 1. AUDIO_GUITARTUNER_BLOCKS - Buffer size is 128 * AUDIO_BLOCKS. *
  32. * The more AUDIO_GUITARTUNER_BLOCKS the lower *
  33. * the frequency you can detect. The default *
  34. * (24) is set to measure down to 29.14 Hz *
  35. * or B(flat)0. *
  36. * *
  37. * 2. MAX_COEFF - Maxium number of coefficeints for the FIR filter. *
  38. * *
  39. ***********************************************************************/
  40. #define AUDIO_GUITARTUNER_BLOCKS 24
  41. #define MAX_COEFF 200
  42. /***********************************************************************/
  43. class AudioTuner : public AudioStream {
  44. public:
  45. /**
  46. * constructor to setup Audio Library and initialize
  47. *
  48. * @return none
  49. */
  50. AudioTuner( void ) : AudioStream( 1, inputQueueArray ),
  51. data( 0.0 ),
  52. coeff_p( NULL ),
  53. enabled( false ),
  54. new_output( false ),
  55. coeff_size( 0 )
  56. {
  57. }
  58. /**
  59. * initialize variables and start
  60. *
  61. * @param threshold Allowed uncertainty
  62. * @param coeff coefficients for fir filter
  63. * @param taps number of coefficients, even
  64. * @param factor must be power of 2
  65. */
  66. void begin( float threshold, int16_t *coeff, uint8_t taps, uint8_t factor );
  67. /**
  68. * sets threshold value
  69. *
  70. * @param thresh
  71. * @return none
  72. */
  73. void threshold( float p );
  74. /**
  75. * triggers true when valid frequency is found
  76. *
  77. * @return flag to indicate valid frequency is found
  78. */
  79. bool available( void );
  80. /**
  81. * get frequency
  82. *
  83. * @return frequency in hertz
  84. */
  85. float read( void );
  86. /**
  87. * get predicitity
  88. *
  89. * @return probability of frequency found
  90. */
  91. float probability( void );
  92. /**
  93. * fir decimation coefficents
  94. *
  95. * @return none
  96. */
  97. void coeff( int16_t *p, int n );
  98. /**
  99. * disable yin
  100. *
  101. * @return none
  102. */
  103. void disable( void );
  104. /**
  105. * Audio Library calls this update function ~2.9ms
  106. *
  107. * @return none
  108. */
  109. virtual void update( void );
  110. private:
  111. /**
  112. * check the sampled data for fundamental frequency
  113. *
  114. * @param yin buffer to hold sum*tau value
  115. * @param rs buffer to hold running sum for sampled window
  116. * @param head buffer index
  117. * @param tau lag we are currently working on this gets incremented
  118. *
  119. * @return tau
  120. */
  121. uint16_t estimate( uint64_t *yin, uint64_t *rs, uint16_t head, uint16_t tau );
  122. /**
  123. * process audio data
  124. *
  125. * @return none
  126. */
  127. void process( int16_t *p );
  128. /**
  129. * Variables
  130. */
  131. float periodicity, yin_threshold, data;
  132. uint64_t running_sum, yin_buffer[5], rs_buffer[5];
  133. uint16_t tau_global;
  134. int16_t AudioBuffer[AUDIO_GUITARTUNER_BLOCKS*AUDIO_BLOCK_SAMPLES] __attribute__ ( ( aligned ( 4 ) ) );
  135. int16_t coeff_state[AUDIO_BLOCK_SAMPLES + MAX_COEFF];
  136. int16_t *coeff_p;
  137. uint8_t yin_idx, state, coeff_size, decimation_factor, decimation_shift;
  138. volatile bool new_output, process_buffer, enabled;
  139. audio_block_t *inputQueueArray[1];
  140. arm_fir_decimate_instance_q15 firDecimateInst;
  141. };
  142. #endif