Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

270 lines
8.1KB

  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_fast.h"
  23. #include "utility/dspinst.h"
  24. #include "arm_math.h"
  25. #include "Arduino.h"
  26. #define NUM_SAMPLES ( AUDIO_GUITARTUNER_BLOCKS << 7 )
  27. void AudioTuner::update( void ) {
  28. audio_block_t *block;
  29. block = receiveReadOnly();
  30. if (!block) return;
  31. if ( !enabled ) {
  32. release( block );
  33. return;
  34. }
  35. /**
  36. * "factor" is the new block size calculatedby
  37. * the decimated shift to incremnt the buffer
  38. * address.
  39. */
  40. const uint8_t factor = AUDIO_BLOCK_SAMPLES >> decimation_shift;
  41. // filter and decimate block by block the incoming signal and store in a buffer.
  42. arm_fir_decimate_fast_q15( &firDecimateInst, block->data, AudioBuffer + ( state * factor ), AUDIO_BLOCK_SAMPLES );
  43. /**
  44. * when half the blocks + 1 of the total
  45. * blocks have been stored in the buffer
  46. * start processing the data.
  47. */
  48. if ( state++ >= AUDIO_GUITARTUNER_BLOCKS >> 1 ) {
  49. if ( process_buffer ) process( AudioBuffer );
  50. if ( state == 0 ) process_buffer = true;
  51. }
  52. release( block );
  53. }
  54. /**
  55. * Start the Yin algorithm
  56. *
  57. * TODO: Significant speed up would be to use spectral domain to find fundamental frequency.
  58. * This paper explains: https://aubio.org/phd/thesis/brossier06thesis.pdf -> Section 3.2.4
  59. * page 79. Might have to downsample for low fundmental frequencies because of fft buffer
  60. * size limit.
  61. */
  62. void AudioTuner::process( int16_t *p ) {
  63. const uint16_t inner_cycles = ( NUM_SAMPLES >> decimation_shift ) >> 1;
  64. uint16_t outer_cycles = inner_cycles / AUDIO_GUITARTUNER_BLOCKS;
  65. uint16_t tau = tau_global;
  66. do {
  67. uint64_t sum = 0;
  68. int32_t a1, a2, b1, b2, c1, c2, d1, d2;
  69. int32_t out1, out2, out3, out4;
  70. uint16_t blkCnt;
  71. int16_t * cur = p;
  72. int16_t * lag = p + tau;
  73. // unrolling the inner loop by 8
  74. blkCnt = inner_cycles >> 3;
  75. do
  76. {
  77. // a(n), b(n), c(n), d(n) each hold two samples
  78. a1 = *__SIMD32( cur )++;
  79. a2 = *__SIMD32( cur )++;
  80. b1 = *__SIMD32( lag )++;
  81. b2 = *__SIMD32( lag )++;
  82. c1 = *__SIMD32( cur )++;
  83. c2 = *__SIMD32( cur )++;
  84. d1 = *__SIMD32( lag )++;
  85. d2 = *__SIMD32( lag )++;
  86. // subract two samples at a time
  87. out1 = __QSUB16( a1, b1 );
  88. out2 = __QSUB16( a2, b2 );
  89. out3 = __QSUB16( c1, d1 );
  90. out4 = __QSUB16( c2, d2 );
  91. // square the difference
  92. sum = multiply_accumulate_16tx16t_add_16bx16b( sum, out1, out1 );
  93. sum = multiply_accumulate_16tx16t_add_16bx16b( sum, out2, out2 );
  94. sum = multiply_accumulate_16tx16t_add_16bx16b( sum, out3, out3 );
  95. sum = multiply_accumulate_16tx16t_add_16bx16b( sum, out4, out4 );
  96. } while( --blkCnt );
  97. uint64_t rs = running_sum;
  98. rs += sum;
  99. yin_buffer[yin_idx] = sum*tau;
  100. rs_buffer[yin_idx] = rs;
  101. running_sum = rs;
  102. yin_idx = ( ++yin_idx >= 5 ) ? 0 : yin_idx;
  103. tau = estimate( yin_buffer, rs_buffer, yin_idx, tau );
  104. if ( tau == 0 ) {
  105. process_buffer = false;
  106. new_output = true;
  107. yin_idx = 1;
  108. running_sum = 0;
  109. tau_global = 1;
  110. state = 0;
  111. return;
  112. }
  113. } while ( --outer_cycles );
  114. if ( tau >= inner_cycles ) {
  115. process_buffer = true;
  116. new_output = false;
  117. yin_idx = 1;
  118. running_sum = 0;
  119. tau_global = 1;
  120. state = 0;
  121. return;
  122. }
  123. tau_global = tau;
  124. }
  125. /**
  126. * check the sampled data for fundamental frequency
  127. *
  128. * @param yin buffer to hold sum*tau value
  129. * @param rs buffer to hold running sum for sampled window
  130. * @param head buffer index
  131. * @param tau lag we are curly working on gets incremented
  132. *
  133. * @return tau
  134. */
  135. uint16_t AudioTuner::estimate( uint64_t *yin, uint64_t *rs, uint16_t head, uint16_t tau ) {
  136. const uint64_t *y = ( uint64_t * )yin;
  137. const uint64_t *r = ( uint64_t * )rs;
  138. uint16_t _tau, _head;
  139. const float thresh = yin_threshold;
  140. _tau = tau;
  141. _head = head;
  142. if ( _tau > 4 ) {
  143. uint16_t idx0, idx1, idx2;
  144. idx0 = _head;
  145. idx1 = _head + 1;
  146. idx1 = ( idx1 >= 5 ) ? 0 : idx1;
  147. idx2 = _head + 2;
  148. idx2 = ( idx2 >= 5 ) ? idx2 - 5 : idx2;
  149. // maybe fixed point would be better here? But how?
  150. float s0, s1, s2;
  151. s0 = ( ( float )*( y+idx0 ) / ( float )*( r+idx0 ) );
  152. s1 = ( ( float )*( y+idx1 ) / ( float )*( r+idx1 ) );
  153. s2 = ( ( float )*( y+idx2 ) / ( float )*( 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 AudioTuner::begin( float threshold, int16_t *coeff, uint8_t taps, uint8_t factor ) {
  169. __disable_irq( );
  170. process_buffer = true;
  171. yin_threshold = threshold;
  172. periodicity = 0.0f;
  173. running_sum = 0;
  174. tau_global = 1;
  175. yin_idx = 1;
  176. enabled = true;
  177. state = 0;
  178. data = 0.0f;
  179. decimation_factor = factor;
  180. decimation_shift = log( factor ) / log( 2 );
  181. coeff_size = taps;
  182. coeff_p = coeff;
  183. arm_fir_decimate_init_q15( &firDecimateInst, coeff_size, decimation_factor, coeff_p, &coeff_state[0], AUDIO_BLOCK_SAMPLES );
  184. __enable_irq( );
  185. }
  186. /**
  187. * available
  188. *
  189. * @return true if data is ready else false
  190. */
  191. bool AudioTuner::available( void ) {
  192. __disable_irq( );
  193. bool flag = new_output;
  194. if ( flag ) new_output = false;
  195. __enable_irq( );
  196. return flag;
  197. }
  198. /**
  199. * read processes the data samples for the Yin algorithm.
  200. *
  201. * @return frequency in hertz
  202. */
  203. float AudioTuner::read( void ) {
  204. __disable_irq( );
  205. float d = data;
  206. __enable_irq( );
  207. return ( AUDIO_SAMPLE_RATE_EXACT / decimation_factor ) / d;
  208. }
  209. /**
  210. * Periodicity of the sampled signal.
  211. *
  212. * @return periodicity
  213. */
  214. float AudioTuner::probability( void ) {
  215. __disable_irq( );
  216. float p = periodicity;
  217. __enable_irq( );
  218. return p;
  219. }
  220. /**
  221. * Initialise parameters.
  222. *
  223. * @param thresh Allowed uncertainty
  224. */
  225. void AudioTuner::coeff( int16_t *p, int n ) {
  226. //coeff_size = n;
  227. //coeff_p = p;
  228. //arm_fir_decimate_init_q15(&firDecimateInst, coeff_size, 4, coeff_p, coeff_state, 128);
  229. }
  230. /**
  231. * Initialise parameters.
  232. *
  233. * @param thresh Allowed uncertainty
  234. */
  235. void AudioTuner::threshold( float p ) {
  236. __disable_irq( );
  237. yin_threshold = p;
  238. __enable_irq( );
  239. }