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 lines
4.5KB

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