Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

211 lines
6.1KB

  1. /* Audio Library Guitar and 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 "AudioTuner.h"
  23. #define HALF_BUFFER NUM_SAMPLES / 2
  24. #define QUARTER_BUFFER NUM_SAMPLES / 4
  25. #define EIGTH_BUFFER NUM_SAMPLES / 8
  26. #define SIXTEENTH_BUFFER NUM_SAMPLES / 16
  27. #define SAMPLE_RATE AUDIO_SAMPLE_RATE_EXACT / SAMPLE_SKIP
  28. #define LOOP1(a) a
  29. #define LOOP2(a) a LOOP1(a)
  30. #define LOOP3(a) a LOOP2(a)
  31. #define LOOP8(a) a LOOP3(a) a LOOP3(a)
  32. #define UNROLL(n,a) LOOP##n(a)
  33. #define WINDOW SAMPLE_SKIP - 1
  34. /**
  35. * audio update function.
  36. */
  37. void AudioTuner::update( void ) {
  38. audio_block_t *block;
  39. const int16_t *p, *end;
  40. block = receiveReadOnly( );
  41. if ( !block ) return;
  42. if ( !enabled ) {
  43. release( block );
  44. return;
  45. }
  46. p = block->data;
  47. end = p + AUDIO_BLOCK_SAMPLES;
  48. uint16_t *dst;
  49. if ( next_buffer ) dst = ( uint16_t * )buffer;
  50. else dst = ( uint16_t * )buffer + NUM_SAMPLES;
  51. uint8_t get_sample = 0;
  52. uint16_t count = block_count;
  53. do {
  54. if ( get_sample++ >= WINDOW ) {
  55. *( dst+count++ ) = *( uint16_t * )p;
  56. get_sample = 0;
  57. }
  58. } while ( p++ < end );
  59. release( block );
  60. if ( count >= NUM_SAMPLES ) {
  61. //digitalWriteFast(2, !digitalReadFast(2));
  62. next_buffer = !next_buffer;
  63. process_buffer = true;
  64. tau_global = 1;
  65. yin_idx = 1;
  66. running_sum = 0;
  67. count = 0;
  68. }
  69. block_count = count;
  70. if ( process_buffer ) {
  71. //digitalWriteFast(0, HIGH);
  72. uint16_t tau;
  73. uint16_t next;
  74. next = next_buffer;
  75. tau = tau_global;
  76. do {
  77. int64_t sum = 0;
  78. const int16_t *end, *buf;
  79. if ( next ) buf = buffer + NUM_SAMPLES;
  80. else buf = buffer;
  81. end = buf + HALF_BUFFER;
  82. do {
  83. int16_t current, lag, delta;
  84. UNROLL( 8,
  85. lag = *( buf + tau );
  86. current = *buf++;
  87. delta = current - lag;
  88. sum += delta*delta;
  89. );
  90. } while ( buf < end );
  91. running_sum += sum;
  92. yin_buffer[yin_idx] = sum*tau;
  93. rs_buffer[yin_idx] = running_sum;
  94. yin_idx = ( ++yin_idx >= 5 ) ? 0 : yin_idx;
  95. tau = estimate( yin_buffer, rs_buffer, yin_idx, tau );
  96. if ( tau == 0 ) {
  97. process_buffer = false;
  98. new_output = true;
  99. //digitalWriteFast(0, LOW);
  100. return;
  101. }
  102. else if ( tau >= HALF_BUFFER ) {
  103. process_buffer = false;
  104. new_output = false;
  105. //digitalWriteFast(0, LOW);
  106. return;
  107. }
  108. } while ( tau <= ( tau_global + 31 ) );
  109. tau_global = tau;
  110. //digitalWriteFast(0, LOW);
  111. }
  112. }
  113. /**
  114. * check the sampled data for fundmental frequency
  115. *
  116. * @param yin buffer to hold sum*tau value
  117. * @param rs buffer to hold running sum for sampled window
  118. * @param head buffer index
  119. * @param tau lag we are currently working on this gets incremented
  120. *
  121. * @return tau
  122. */
  123. uint16_t AudioTuner::estimate( int64_t *yin, int64_t *rs, uint16_t head, uint16_t tau ) {
  124. const int64_t *p = ( int64_t * )yin;
  125. const int64_t *r = ( int64_t * )rs;
  126. uint16_t period = 0, _tau, _head;
  127. _tau = tau;
  128. _head = head;
  129. if ( _tau > 4 ) {
  130. uint16_t idx0, idx1, idx2;
  131. idx0 = _head;
  132. idx1 = _head + 1;
  133. idx1 = ( idx1 >= 5 ) ? 0 : idx1;
  134. idx2 = head + 2;
  135. idx2 = ( idx2 >= 5 ) ? 0 : idx2;
  136. float s0, s1, s2;
  137. s0 = ( ( float )*( p+idx0 ) / r[idx0] );
  138. s1 = ( ( float )*( p+idx1 ) / r[idx1] );
  139. s2 = ( ( float )*( p+idx2 ) / r[idx2] );
  140. if ( s1 < threshold && s1 < s2 ) {
  141. period = _tau - 3;
  142. periodicity = 1 - s1;
  143. data = period + 0.5f * ( s0 - s2 ) / ( s0 - 2.0f * s1 + s2 );
  144. return 0;
  145. }
  146. if ( s1 > 2.2 ) return _tau + 2;
  147. else return _tau + 1;
  148. }
  149. return _tau + 1;
  150. }
  151. /**
  152. * available
  153. *
  154. * @return true if data is ready else false
  155. */
  156. bool AudioTuner::available( void ) {
  157. __disable_irq();
  158. bool flag = new_output;
  159. if (flag) new_output = false;
  160. __enable_irq();
  161. return flag;
  162. }
  163. /**
  164. * read processes the data samples for the Yin algorithm.
  165. *
  166. * @return frequency in hertz
  167. */
  168. float AudioTuner::read( void ) {
  169. return SAMPLE_RATE / data;
  170. }
  171. /**
  172. * Periodicity of the sampled signal from Yin algorithm from read function.
  173. *
  174. * @return periodicity
  175. */
  176. float AudioTuner::probability( void ) {
  177. return periodicity;
  178. }
  179. /**
  180. * Initialise parameters.
  181. *
  182. * @param thresh Allowed uncertainty
  183. */
  184. void AudioTuner::set_params( float thresh ) {
  185. __disable_irq( );
  186. //arm_cfft_radix4_init_q15(&fft_inst, 1024, 0, 1);
  187. //arm_cfft_radix4_init_q15(&ifft_inst, 1024, 1, 1);
  188. threshold = thresh;
  189. periodicity = 0.0f;
  190. block_count = 0;
  191. enabled = true;
  192. __enable_irq( );
  193. }