Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

267 rindas
7.8KB

  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. next_buffer = false;
  61. } else {
  62. if ( !first_run && process_buffer ) process( );
  63. for ( int i = 0; i < AUDIO_GUITARTUNER_BLOCKS; i++ ) copy_buffer( AudioBuffer+( i * 0x80 ), blocklist2[i]->data );
  64. for ( int i = 0; i < AUDIO_GUITARTUNER_BLOCKS; i++ ) release( blocklist2[i] );
  65. next_buffer = true;
  66. }
  67. process_buffer = true;
  68. first_run = false;
  69. state = 0;
  70. }
  71. }
  72. /**
  73. * Start the Yin algorithm
  74. *
  75. * TODO: Significant speed up would be to use spectral domain to find fundamental frequency.
  76. * This paper explains: https://aubio.org/phd/thesis/brossier06thesis.pdf -> Section 3.2.4
  77. * page 79. Might have to downsample for low fundmental frequencies because of fft buffer
  78. * size limit.
  79. */
  80. void AudioAnalyzeNoteFrequency::process( void ) {
  81. const int16_t *p;
  82. p = AudioBuffer;
  83. uint16_t cycles = 64;
  84. uint16_t tau = tau_global;
  85. do {
  86. uint16_t x = 0;
  87. uint64_t sum = 0;
  88. do {
  89. int16_t current, lag, delta;
  90. lag = *( ( int16_t * )p + ( x+tau ) );
  91. current = *( ( int16_t * )p+x );
  92. delta = ( current-lag );
  93. sum += delta * delta;
  94. x += 4;
  95. lag = *( ( int16_t * )p + ( x+tau ) );
  96. current = *( ( int16_t * )p+x );
  97. delta = ( current-lag );
  98. sum += delta * delta;
  99. x += 4;
  100. lag = *( ( int16_t * )p + ( x+tau ) );
  101. current = *( ( int16_t * )p+x );
  102. delta = ( current-lag );
  103. sum += delta * delta;
  104. x += 4;
  105. lag = *( ( int16_t * )p + ( x+tau ) );
  106. current = *( ( int16_t * )p+x );
  107. delta = ( current-lag );
  108. sum += delta * delta;
  109. x += 4;
  110. } while ( x < HALF_BLOCKS );
  111. uint64_t rs = running_sum;
  112. rs += sum;
  113. yin_buffer[yin_idx] = sum*tau;
  114. rs_buffer[yin_idx] = rs;
  115. running_sum = rs;
  116. yin_idx = ( ++yin_idx >= 5 ) ? 0 : yin_idx;
  117. tau = estimate( yin_buffer, rs_buffer, yin_idx, tau );
  118. if ( tau == 0 ) {
  119. process_buffer = false;
  120. new_output = true;
  121. yin_idx = 1;
  122. running_sum = 0;
  123. tau_global = 1;
  124. return;
  125. }
  126. } while ( --cycles );
  127. //digitalWriteFast(10, LOW);
  128. if ( tau >= HALF_BLOCKS ) {
  129. process_buffer = false;
  130. new_output = false;
  131. yin_idx = 1;
  132. running_sum = 0;
  133. tau_global = 1;
  134. return;
  135. }
  136. tau_global = tau;
  137. }
  138. /**
  139. * check the sampled data for fundamental frequency
  140. *
  141. * @param yin buffer to hold sum*tau value
  142. * @param rs buffer to hold running sum for sampled window
  143. * @param head buffer index
  144. * @param tau lag we are currently working on gets incremented
  145. *
  146. * @return tau
  147. */
  148. uint16_t AudioAnalyzeNoteFrequency::estimate( uint64_t *yin, uint64_t *rs, uint16_t head, uint16_t tau ) {
  149. const uint64_t *y = ( uint64_t * )yin;
  150. const uint64_t *r = ( uint64_t * )rs;
  151. uint16_t _tau, _head;
  152. const float thresh = yin_threshold;
  153. _tau = tau;
  154. _head = head;
  155. if ( _tau > 4 ) {
  156. uint16_t idx0, idx1, idx2;
  157. idx0 = _head;
  158. idx1 = _head + 1;
  159. idx1 = ( idx1 >= 5 ) ? 0 : idx1;
  160. idx2 = head + 2;
  161. idx2 = ( idx2 >= 5 ) ? 0 : idx2;
  162. float s0, s1, s2;
  163. s0 = ( ( float )*( y+idx0 ) / *( r+idx0 ) );
  164. s1 = ( ( float )*( y+idx1 ) / *( r+idx1 ) );
  165. s2 = ( ( float )*( y+idx2 ) / *( r+idx2 ) );
  166. if ( s1 < thresh && s1 < s2 ) {
  167. uint16_t period = _tau - 3;
  168. periodicity = 1 - s1;
  169. data = period + 0.5f * ( s0 - s2 ) / ( s0 - 2.0f * s1 + s2 );
  170. return 0;
  171. }
  172. }
  173. return _tau + 1;
  174. }
  175. /**
  176. * Initialise
  177. *
  178. * @param threshold Allowed uncertainty
  179. */
  180. void AudioAnalyzeNoteFrequency::begin( float threshold ) {
  181. __disable_irq( );
  182. process_buffer = false;
  183. yin_threshold = threshold;
  184. periodicity = 0.0f;
  185. next_buffer = true;
  186. running_sum = 0;
  187. tau_global = 1;
  188. first_run = true;
  189. yin_idx = 1;
  190. enabled = true;
  191. state = 0;
  192. data = 0.0f;
  193. __enable_irq( );
  194. }
  195. /**
  196. * available
  197. *
  198. * @return true if data is ready else false
  199. */
  200. bool AudioAnalyzeNoteFrequency::available( void ) {
  201. __disable_irq( );
  202. bool flag = new_output;
  203. if ( flag ) new_output = false;
  204. __enable_irq( );
  205. return flag;
  206. }
  207. /**
  208. * read processes the data samples for the Yin algorithm.
  209. *
  210. * @return frequency in hertz
  211. */
  212. float AudioAnalyzeNoteFrequency::read( void ) {
  213. __disable_irq( );
  214. float d = data;
  215. __enable_irq( );
  216. return AUDIO_SAMPLE_RATE_EXACT / d;
  217. }
  218. /**
  219. * Periodicity of the sampled signal from Yin algorithm from read function.
  220. *
  221. * @return periodicity
  222. */
  223. float AudioAnalyzeNoteFrequency::probability( void ) {
  224. __disable_irq( );
  225. float p = periodicity;
  226. __enable_irq( );
  227. return p;
  228. }
  229. /**
  230. * Initialise parameters.
  231. *
  232. * @param thresh Allowed uncertainty
  233. */
  234. void AudioAnalyzeNoteFrequency::threshold( float p ) {
  235. __disable_irq( );
  236. yin_threshold = p;
  237. __enable_irq( );
  238. }