You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 satır
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. digitalWriteFast(2, LOW);
  66. }
  67. /**
  68. * initialize variables and start conversion
  69. *
  70. * @param threshold Allowed uncertainty
  71. * @param cpu_max How much cpu usage before throttling
  72. */
  73. void initialize( float threshold, uint8_t cpu_max);
  74. /**
  75. * sets threshold value
  76. *
  77. * @param thresh
  78. */
  79. void threshold( float p );
  80. /**
  81. * triggers true when valid frequency is found
  82. *
  83. * @return flag to indicate valid frequency is found
  84. */
  85. bool available( void );
  86. /**
  87. * get frequency
  88. *
  89. * @return frequency in hertz
  90. */
  91. float read( void );
  92. /**
  93. * get predicitity
  94. *
  95. * @return probability of frequency found
  96. */
  97. float probability( void );
  98. /**
  99. * Audio Library calls this update function ~2.9ms
  100. */
  101. virtual void update( void );
  102. private:
  103. /**
  104. * check the sampled data for fundamental frequency
  105. *
  106. * @param yin buffer to hold sum*tau value
  107. * @param rs buffer to hold running sum for sampled window
  108. * @param head buffer index
  109. * @param tau lag we are currently working on this gets incremented
  110. *
  111. * @return tau
  112. */
  113. uint16_t estimate( int64_t *yin, int64_t *rs, uint16_t head, uint16_t tau );
  114. int16_t buffer[NUM_SAMPLES*2] __attribute__ ( ( aligned ( 4 ) ) );
  115. float periodicity, yin_threshold, data;
  116. int64_t rs_buffer[5], yin_buffer[5];
  117. uint64_t running_sum;
  118. uint16_t tau_global, count_global, tau_cycles, cpu_usage_max;
  119. uint8_t next_buffer, yin_idx;
  120. bool enabled, process_buffer;
  121. volatile bool new_output;
  122. audio_block_t *inputQueueArray[1];
  123. };
  124. #endif