Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

AudioTuner.cpp 7.8KB

il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
il y a 9 ans
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. #include "utility/dspinst.h"
  24. #include "arm_math.h"
  25. #define HALF_BLOCKS AUDIO_BLOCKS * 64
  26. #define LOOP1(a) a
  27. #define LOOP2(a) a LOOP1(a)
  28. #define LOOP3(a) a LOOP2(a)
  29. #define LOOP4(a) a LOOP3(a)
  30. #define LOOP8(a) a LOOP3(a) a LOOP3(a)
  31. #define LOOP16(a) a LOOP8(a) a LOOP2(a) a LOOP3(a)
  32. #define LOOP32(a) a LOOP16(a) a LOOP8(a) a LOOP1(a) a LOOP3(a)
  33. #define LOOP64(a) a LOOP32(a) a LOOP16(a) a LOOP8(a) a LOOP2(a) a LOOP1(a)
  34. #define UNROLL(n,a) LOOP##n(a)
  35. static void copy_buffer(void *destination, const void *source) {
  36. const uint16_t *src = (const uint16_t *)source;
  37. uint16_t *dst = (uint16_t *)destination;
  38. for (int i=0; i < AUDIO_BLOCK_SAMPLES; i++) *dst++ = *src++;
  39. }
  40. void AudioTuner::update( void ) {
  41. audio_block_t *block;
  42. block = receiveReadOnly();
  43. if (!block) return;
  44. if ( !enabled ) {
  45. release( block );
  46. return;
  47. }
  48. digitalWriteFast(2, HIGH);
  49. if ( next_buffer ) {
  50. blocklist1[state++] = block;
  51. if ( !first_run && process_buffer ) process( );
  52. } else {
  53. blocklist2[state++] = block;
  54. if ( !first_run && process_buffer ) process( );
  55. }
  56. if ( state >= AUDIO_BLOCKS ) {
  57. if ( next_buffer ) {
  58. if ( !first_run && process_buffer ) process( );
  59. for ( int i = 0; i < AUDIO_BLOCKS; i++ ) copy_buffer( AudioBuffer+( i * 0x80 ), blocklist1[i]->data );
  60. for ( int i = 0; i < AUDIO_BLOCKS; i++ ) release(blocklist1[i] );
  61. } else {
  62. if ( !first_run && process_buffer ) process( );
  63. for ( int i = 0; i < AUDIO_BLOCKS; i++ ) copy_buffer( AudioBuffer+( i * 0x80 ), blocklist2[i]->data );
  64. for ( int i = 0; i < AUDIO_BLOCKS; i++ ) release( blocklist2[i] );
  65. }
  66. process_buffer = true;
  67. first_run = false;
  68. state = 0;
  69. //digitalWriteFast(LED_BUILTIN, !digitalReadFast(LED_BUILTIN));
  70. }
  71. }
  72. FASTRUN void AudioTuner::process( void ) {
  73. //digitalWriteFast(0, HIGH);
  74. const int16_t *p;
  75. p = AudioBuffer;
  76. uint16_t cycles = 64;;
  77. uint16_t tau = tau_global;
  78. do {
  79. uint16_t x = 0;
  80. int64_t sum = 0;
  81. //uint32_t res;
  82. do {
  83. /*int16_t current1, lag1, current2, lag2;
  84. int32_t val1, val2;
  85. lag1 = *( ( uint32_t * )p + ( x + tau ) );
  86. current1 = *( ( uint32_t * )p + x );
  87. x += 32;
  88. lag2 = *( ( uint32_t * )p + ( x + tau ) );
  89. current2 = *( ( uint32_t * )p + x );
  90. val1 = __PKHBT(current1, current2, 0x10);
  91. val2 = __PKHBT(lag1, lag2, 0x10);
  92. res = __SSUB16( val1, val2 );
  93. sum = __SMLALD(res, res, sum);
  94. //sum = __SMLSLD(delta1, delta2, sum);*/
  95. int16_t current, lag, delta;
  96. //UNROLL(16,
  97. lag = *( ( int16_t * )p + ( x+tau ) );
  98. current = *( ( int16_t * )p+x );
  99. delta = ( current-lag );
  100. sum += delta * delta;
  101. #if F_CPU == 144000000
  102. x += 8;
  103. #elif F_CPU == 120000000
  104. x += 12;
  105. #elif F_CPU == 96000000
  106. x += 16;
  107. #elif F_CPU < 96000000
  108. x += 32;
  109. #endif
  110. //);
  111. } while ( x <= HALF_BLOCKS );
  112. running_sum += sum;
  113. yin_buffer[yin_idx] = sum*tau;
  114. rs_buffer[yin_idx] = running_sum;
  115. yin_idx = ( ++yin_idx >= 5 ) ? 0 : yin_idx;
  116. tau = estimate( yin_buffer, rs_buffer, yin_idx, tau );
  117. if ( tau == 0 ) {
  118. process_buffer = false;
  119. new_output = true;
  120. yin_idx = 1;
  121. running_sum = 0;
  122. tau_global = 1;
  123. //digitalWriteFast(2, LOW);
  124. //digitalWriteFast(0, LOW);
  125. return;
  126. }
  127. } while ( --cycles );
  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. //digitalWriteFast(0, LOW);
  135. return;
  136. }
  137. tau_global = tau;
  138. //digitalWriteFast(0, LOW);
  139. }
  140. /**
  141. * check the sampled data for fundmental frequency
  142. *
  143. * @param yin buffer to hold sum*tau value
  144. * @param rs buffer to hold running sum for sampled window
  145. * @param head buffer index
  146. * @param tau lag we are currently working on this gets incremented
  147. *
  148. * @return tau
  149. */
  150. uint16_t AudioTuner::estimate( int64_t *yin, int64_t *rs, uint16_t head, uint16_t tau ) {
  151. const int64_t *y = ( int64_t * )yin;
  152. const int64_t *r = ( int64_t * )rs;
  153. uint16_t _tau, _head;
  154. const float thresh = yin_threshold;
  155. _tau = tau;
  156. _head = head;
  157. if ( _tau > 4 ) {
  158. uint16_t idx0, idx1, idx2;
  159. idx0 = _head;
  160. idx1 = _head + 1;
  161. idx1 = ( idx1 >= 5 ) ? 0 : idx1;
  162. idx2 = head + 2;
  163. idx2 = ( idx2 >= 5 ) ? 0 : idx2;
  164. float s0, s1, s2;
  165. s0 = ( ( float )*( y+idx0 ) / *( r+idx0 ) );
  166. s1 = ( ( float )*( y+idx1 ) / *( r+idx1 ) );
  167. s2 = ( ( float )*( y+idx2 ) / *( r+idx2 ) );
  168. if ( s1 < thresh && s1 < s2 ) {
  169. uint16_t period = _tau - 3;
  170. periodicity = 1 - s1;
  171. data = period + 0.5f * ( s0 - s2 ) / ( s0 - 2.0f * s1 + s2 );
  172. return 0;
  173. }
  174. }
  175. return _tau + 1;
  176. }
  177. /**
  178. * Initialise
  179. *
  180. * @param threshold Allowed uncertainty
  181. * @param cpu_max How much cpu usage before throttling
  182. */
  183. void AudioTuner::initialize( float threshold ) {
  184. __disable_irq( );
  185. process_buffer = false;
  186. yin_threshold = threshold;
  187. periodicity = 0.0f;
  188. next_buffer = true;
  189. running_sum = 0;
  190. tau_global = 1;
  191. first_run = true;
  192. yin_idx = 1;
  193. enabled = true;
  194. state = 0;
  195. data = 0.0f;
  196. __enable_irq( );
  197. }
  198. /**
  199. * available
  200. *
  201. * @return true if data is ready else false
  202. */
  203. bool AudioTuner::available( void ) {
  204. __disable_irq( );
  205. bool flag = new_output;
  206. if ( flag ) new_output = false;
  207. __enable_irq( );
  208. return flag;
  209. }
  210. /**
  211. * read processes the data samples for the Yin algorithm.
  212. *
  213. * @return frequency in hertz
  214. */
  215. float AudioTuner::read( void ) {
  216. __disable_irq( );
  217. float d = data;
  218. __enable_irq( );
  219. return AUDIO_SAMPLE_RATE_EXACT / d;
  220. }
  221. /**
  222. * Periodicity of the sampled signal from Yin algorithm from read function.
  223. *
  224. * @return periodicity
  225. */
  226. float AudioTuner::probability( void ) {
  227. __disable_irq( );
  228. float p = periodicity;
  229. __enable_irq( );
  230. return p;
  231. }
  232. /**
  233. * Initialise parameters.
  234. *
  235. * @param thresh Allowed uncertainty
  236. */
  237. void AudioTuner::threshold( float p ) {
  238. __disable_irq( );
  239. yin_threshold = p;
  240. __enable_irq( );
  241. }