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.

AudioTuner.h 4.1KB

9 vuotta sitten
9 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. AudioTuner( void ) : AudioStream( 1, inputQueueArray ),
  47. enabled( false ), new_output(false),
  48. next_buffer(1), process_buffer(false),
  49. running_sum(0), block_count(0),
  50. yin_idx(1)
  51. {
  52. set_params( 0.05f );// threshold set 15ms
  53. }
  54. /**
  55. * sets threshold value and window length
  56. *
  57. * @param thresh
  58. */
  59. void set_params( float thresh );
  60. /**
  61. * triggers true when valid frequency is found
  62. *
  63. * @return flag to indicate valid frequency is found
  64. */
  65. bool available( void );
  66. /**
  67. * get frequency
  68. *
  69. * @return frequency in hertz
  70. */
  71. float read( void );
  72. /**
  73. * get predicitity
  74. *
  75. * @return probability of correct freq found
  76. */
  77. float probability( void );
  78. /**
  79. * Audio Library calls this update function ~2.9ms
  80. */
  81. virtual void update( void );
  82. private:
  83. /**
  84. * check the sampled data for fundmental frequency
  85. *
  86. * @param yin buffer to hold sum*tau value
  87. * @param rs buffer to hold running sum for sampled window
  88. * @param head buffer index
  89. * @param tau lag we are currently working on this gets incremented
  90. *
  91. * @return tau
  92. */
  93. uint16_t estimate( int64_t *yin, int64_t *rs, uint16_t head, uint16_t tau );
  94. int16_t buffer[NUM_SAMPLES*2] __attribute__ ( ( aligned ( 4 ) ) );
  95. float periodicity, threshold, data;
  96. int64_t rs_buffer[5], yin_buffer[5];
  97. uint64_t running_sum;
  98. uint16_t block_count, tau_global;
  99. uint8_t next_buffer, yin_idx;
  100. bool enabled, process_buffer;
  101. volatile bool new_output;
  102. audio_block_t *inputQueueArray[1];
  103. };
  104. #endif