No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

158 líneas
4.9KB

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