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.

analyze_notefreq.h 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 AudioAnalyzeNoteFrequency_h_
  23. #define AudioAnalyzeNoteFrequency_h_
  24. #include "AudioStream.h"
  25. /***********************************************************************
  26. * Safe to adjust these values below *
  27. * *
  28. * This parameter defines the size of the buffer. *
  29. * *
  30. * 1. AUDIO_GUITARTUNER_BLOCKS - Buffer size is 128 * AUDIO_BLOCKS. *
  31. * The more AUDIO_GUITARTUNER_BLOCKS the lower *
  32. * the frequency you can detect. The default *
  33. * (24) is set to measure down to 29.14 Hz *
  34. * or B(flat)0. *
  35. * *
  36. ***********************************************************************/
  37. #define AUDIO_GUITARTUNER_BLOCKS 24
  38. /***********************************************************************/
  39. class AudioAnalyzeNoteFrequency : public AudioStream {
  40. public:
  41. /**
  42. * constructor to setup Audio Library and initialize
  43. *
  44. * @return none
  45. */
  46. AudioAnalyzeNoteFrequency( void ) : AudioStream( 1, inputQueueArray ), enabled( false ), new_output(false) {
  47. }
  48. /**
  49. * initialize variables and start conversion
  50. *
  51. * @param threshold Allowed uncertainty
  52. * @param cpu_max How much cpu usage before throttling
  53. *
  54. * @return none
  55. */
  56. void begin( float threshold );
  57. /**
  58. * sets threshold value
  59. *
  60. * @param thresh
  61. * @return none
  62. */
  63. void threshold( float p );
  64. /**
  65. * triggers true when valid frequency is found
  66. *
  67. * @return flag to indicate valid frequency is found
  68. */
  69. bool available( void );
  70. /**
  71. * get frequency
  72. *
  73. * @return frequency in hertz
  74. */
  75. float read( void );
  76. /**
  77. * get predicitity
  78. *
  79. * @return probability of frequency found
  80. */
  81. float probability( void );
  82. /**
  83. * Audio Library calls this update function ~2.9ms
  84. *
  85. * @return none
  86. */
  87. virtual void update( void );
  88. private:
  89. /**
  90. * check the sampled data for fundamental frequency
  91. *
  92. * @param yin buffer to hold sum*tau value
  93. * @param rs buffer to hold running sum for sampled window
  94. * @param head buffer index
  95. * @param tau lag we are currently working on this gets incremented
  96. *
  97. * @return tau
  98. */
  99. uint16_t estimate( int64_t *yin, int64_t *rs, uint16_t head, uint16_t tau );
  100. /**
  101. * process audio data
  102. *
  103. * @return none
  104. */
  105. void process( void );
  106. /**
  107. * Variables
  108. */
  109. uint64_t running_sum;
  110. uint16_t tau_global;
  111. int64_t rs_buffer[5], yin_buffer[5];
  112. int16_t AudioBuffer[AUDIO_GUITARTUNER_BLOCKS*128] __attribute__ ( ( aligned ( 4 ) ) );
  113. uint8_t yin_idx, state;
  114. float periodicity, yin_threshold, cpu_usage_max, data;
  115. bool enabled, next_buffer, first_run;
  116. volatile bool new_output, process_buffer;
  117. audio_block_t *blocklist1[AUDIO_GUITARTUNER_BLOCKS];
  118. audio_block_t *blocklist2[AUDIO_GUITARTUNER_BLOCKS];
  119. audio_block_t *inputQueueArray[1];
  120. };
  121. #endif