Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

134 lines
5.0KB

  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_44100 1 // 44100 sample rate
  27. #define SAMPLE_RATE_22050 2 // 22050 sample rate
  28. #define SAMPLE_RATE_11025 4 // 11025 sample rate
  29. /****************************************************************/
  30. /****************************************************************
  31. * Safe to adjust these values below *
  32. * *
  33. * These two parameters define how this object works. *
  34. * *
  35. * 1. NUM_SAMPLES - Size of the buffer. Since object uses *
  36. * double buffering this value will be 4x in bytes of *
  37. * memory. !!! Must be power of 2 !!!! *
  38. * *
  39. * 2. SAMPLE_RATE - Just what it says. *
  40. * *
  41. * These two parameters work hand in hand. For example if you *
  42. * want a high sample rate but do not allocate enough buffer *
  43. * space, you will be limit how low of a frequency you can *
  44. * measure. If you then increase the buffer you use up *
  45. * precious ram and slow down the system since it takes longer *
  46. * to processes the buffer. *
  47. * *
  48. * Play around with these values to find what best suits your *
  49. * needs. The max number of buffers you can have is 8192 bins. *
  50. ****************************************************************/
  51. // !!! Must be power of 2 !!!!
  52. #define NUM_SAMPLES 2048 // make a power of two
  53. // Use defined sample rates above^
  54. #define SAMPLE_RATE SAMPLE_RATE_22050
  55. /****************************************************************/
  56. class AudioTuner : public AudioStream
  57. {
  58. public:
  59. /**
  60. * constructor to setup Audio Library and initialize
  61. *
  62. * @return none
  63. */
  64. AudioTuner( void ) : AudioStream( 1, inputQueueArray ), enabled( false ), new_output(false) {}
  65. /**
  66. * initialize variables and start conversion
  67. *
  68. * @param threshold Allowed uncertainty
  69. * @param cpu_max How much cpu usage before throttling
  70. */
  71. void initialize( float threshold, float cpu_max);
  72. /**
  73. * sets threshold value
  74. *
  75. * @param thresh
  76. */
  77. void threshold( float p );
  78. /**
  79. * triggers true when valid frequency is found
  80. *
  81. * @return flag to indicate valid frequency is found
  82. */
  83. bool available( void );
  84. /**
  85. * get frequency
  86. *
  87. * @return frequency in hertz
  88. */
  89. float read( void );
  90. /**
  91. * get predicitity
  92. *
  93. * @return probability of frequency found
  94. */
  95. float probability( void );
  96. /**
  97. * Audio Library calls this update function ~2.9ms
  98. */
  99. virtual void update( void );
  100. private:
  101. /**
  102. * check the sampled data for fundamental frequency
  103. *
  104. * @param yin buffer to hold sum*tau value
  105. * @param rs buffer to hold running sum for sampled window
  106. * @param head buffer index
  107. * @param tau lag we are currently working on this gets incremented
  108. *
  109. * @return tau
  110. */
  111. uint16_t estimate( int64_t *yin, int64_t *rs, uint16_t head, uint16_t tau );
  112. int16_t buffer[NUM_SAMPLES*2] __attribute__ ( ( aligned ( 4 ) ) );
  113. float periodicity, yin_threshold, data, cpu_usage_max;
  114. int64_t rs_buffer[5], yin_buffer[5];
  115. uint64_t running_sum;
  116. uint16_t tau_global, count_global, tau_cycles;
  117. uint8_t yin_idx;
  118. bool enabled, process_buffer, next_buffer;
  119. volatile bool new_output;
  120. audio_block_t *inputQueueArray[1];
  121. };
  122. #endif