您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

250 行
7.2KB

  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. #include "analyze_notefreq.h"
  23. #include "utility/dspinst.h"
  24. #include "arm_math.h"
  25. #define HALF_BLOCKS AUDIO_GUITARTUNER_BLOCKS * 64
  26. /**
  27. * Copy internal blocks of data to class buffer
  28. *
  29. * @param destination destination address
  30. * @param source source address
  31. */
  32. static void copy_buffer(void *destination, const void *source) {
  33. const uint16_t *src = (const uint16_t *)source;
  34. uint16_t *dst = (uint16_t *)destination;
  35. for (int i=0; i < AUDIO_BLOCK_SAMPLES; i++) *dst++ = *src++;
  36. }
  37. /**
  38. * Virtual function to override from Audio Library
  39. */
  40. void AudioAnalyzeNoteFrequency::update( void ) {
  41. audio_block_t *block;
  42. block = receiveReadOnly();
  43. if (!block) return;
  44. if ( !enabled ) {
  45. release( block );
  46. return;
  47. }
  48. if ( next_buffer ) {
  49. blocklist1[state++] = block;
  50. if ( !first_run && process_buffer ) process( );
  51. } else {
  52. blocklist2[state++] = block;
  53. if ( !first_run && process_buffer ) process( );
  54. }
  55. if ( state >= AUDIO_GUITARTUNER_BLOCKS ) {
  56. if ( next_buffer ) {
  57. if ( !first_run && process_buffer ) process( );
  58. for ( int i = 0; i < AUDIO_GUITARTUNER_BLOCKS; i++ ) copy_buffer( AudioBuffer+( i * 0x80 ), blocklist1[i]->data );
  59. for ( int i = 0; i < AUDIO_GUITARTUNER_BLOCKS; i++ ) release(blocklist1[i] );
  60. } else {
  61. if ( !first_run && process_buffer ) process( );
  62. for ( int i = 0; i < AUDIO_GUITARTUNER_BLOCKS; i++ ) copy_buffer( AudioBuffer+( i * 0x80 ), blocklist2[i]->data );
  63. for ( int i = 0; i < AUDIO_GUITARTUNER_BLOCKS; i++ ) release( blocklist2[i] );
  64. }
  65. process_buffer = true;
  66. first_run = false;
  67. state = 0;
  68. }
  69. }
  70. /**
  71. * Start the Yin algorithm
  72. *
  73. * TODO: Significant speed up would be to use spectral domain to find fundamental frequency.
  74. * This paper explains: https://aubio.org/phd/thesis/brossier06thesis.pdf -> Section 3.2.4
  75. * page 79. Might have to downsample for low fundmental frequencies because of fft buffer
  76. * size limit.
  77. */
  78. FASTRUN void AudioAnalyzeNoteFrequency::process( void ) {
  79. const int16_t *p;
  80. p = AudioBuffer;
  81. uint16_t cycles = 64;
  82. uint16_t tau = tau_global;
  83. do {
  84. uint16_t x = 0;
  85. int64_t sum = 0;
  86. do {
  87. int16_t current, lag, delta;
  88. lag = *( ( int16_t * )p + ( x+tau ) );
  89. current = *( ( int16_t * )p+x );
  90. delta = ( current-lag );
  91. sum += delta * delta;
  92. #if F_CPU == 144000000
  93. x += 8;
  94. #elif F_CPU == 120000000
  95. x += 12;
  96. #elif F_CPU == 96000000
  97. x += 16;
  98. #elif F_CPU < 96000000
  99. x += 32;
  100. #endif
  101. } while ( x <= HALF_BLOCKS );
  102. running_sum += sum;
  103. yin_buffer[yin_idx] = sum*tau;
  104. rs_buffer[yin_idx] = running_sum;
  105. yin_idx = ( ++yin_idx >= 5 ) ? 0 : yin_idx;
  106. tau = estimate( yin_buffer, rs_buffer, yin_idx, tau );
  107. if ( tau == 0 ) {
  108. process_buffer = false;
  109. new_output = true;
  110. yin_idx = 1;
  111. running_sum = 0;
  112. tau_global = 1;
  113. return;
  114. }
  115. } while ( --cycles );
  116. if ( tau >= HALF_BLOCKS ) {
  117. process_buffer = false;
  118. new_output = false;
  119. yin_idx = 1;
  120. running_sum = 0;
  121. tau_global = 1;
  122. return;
  123. }
  124. tau_global = tau;
  125. }
  126. /**
  127. * check the sampled data for fundamental frequency
  128. *
  129. * @param yin buffer to hold sum*tau value
  130. * @param rs buffer to hold running sum for sampled window
  131. * @param head buffer index
  132. * @param tau lag we are currently working on gets incremented
  133. *
  134. * @return tau
  135. */
  136. uint16_t AudioAnalyzeNoteFrequency::estimate( int64_t *yin, int64_t *rs, uint16_t head, uint16_t tau ) {
  137. const int64_t *y = ( int64_t * )yin;
  138. const int64_t *r = ( int64_t * )rs;
  139. uint16_t _tau, _head;
  140. const float thresh = yin_threshold;
  141. _tau = tau;
  142. _head = head;
  143. if ( _tau > 4 ) {
  144. uint16_t idx0, idx1, idx2;
  145. idx0 = _head;
  146. idx1 = _head + 1;
  147. idx1 = ( idx1 >= 5 ) ? 0 : idx1;
  148. idx2 = head + 2;
  149. idx2 = ( idx2 >= 5 ) ? 0 : idx2;
  150. float s0, s1, s2;
  151. s0 = ( ( float )*( y+idx0 ) / *( r+idx0 ) );
  152. s1 = ( ( float )*( y+idx1 ) / *( r+idx1 ) );
  153. s2 = ( ( float )*( y+idx2 ) / *( r+idx2 ) );
  154. if ( s1 < thresh && s1 < s2 ) {
  155. uint16_t period = _tau - 3;
  156. periodicity = 1 - s1;
  157. data = period + 0.5f * ( s0 - s2 ) / ( s0 - 2.0f * s1 + s2 );
  158. return 0;
  159. }
  160. }
  161. return _tau + 1;
  162. }
  163. /**
  164. * Initialise
  165. *
  166. * @param threshold Allowed uncertainty
  167. */
  168. void AudioAnalyzeNoteFrequency::begin( float threshold ) {
  169. __disable_irq( );
  170. process_buffer = false;
  171. yin_threshold = threshold;
  172. periodicity = 0.0f;
  173. next_buffer = true;
  174. running_sum = 0;
  175. tau_global = 1;
  176. first_run = true;
  177. yin_idx = 1;
  178. enabled = true;
  179. state = 0;
  180. data = 0.0f;
  181. __enable_irq( );
  182. }
  183. /**
  184. * available
  185. *
  186. * @return true if data is ready else false
  187. */
  188. bool AudioAnalyzeNoteFrequency::available( void ) {
  189. __disable_irq( );
  190. bool flag = new_output;
  191. if ( flag ) new_output = false;
  192. __enable_irq( );
  193. return flag;
  194. }
  195. /**
  196. * read processes the data samples for the Yin algorithm.
  197. *
  198. * @return frequency in hertz
  199. */
  200. float AudioAnalyzeNoteFrequency::read( void ) {
  201. __disable_irq( );
  202. float d = data;
  203. __enable_irq( );
  204. return AUDIO_SAMPLE_RATE_EXACT / d;
  205. }
  206. /**
  207. * Periodicity of the sampled signal from Yin algorithm from read function.
  208. *
  209. * @return periodicity
  210. */
  211. float AudioAnalyzeNoteFrequency::probability( void ) {
  212. __disable_irq( );
  213. float p = periodicity;
  214. __enable_irq( );
  215. return p;
  216. }
  217. /**
  218. * Initialise parameters.
  219. *
  220. * @param thresh Allowed uncertainty
  221. */
  222. void AudioAnalyzeNoteFrequency::threshold( float p ) {
  223. __disable_irq( );
  224. yin_threshold = p;
  225. __enable_irq( );
  226. }