You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AudioTuner.cpp 8.1KB

9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
9 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. #if SAMPLE_RATE == SAMPLE_RATE_44100
  25. #define SAMPLE_RATE_EXACT AUDIO_SAMPLE_RATE_EXACT / 1
  26. #elif SAMPLE_RATE == SAMPLE_RATE_22050
  27. #define SAMPLE_RATE_EXACT AUDIO_SAMPLE_RATE_EXACT / 2
  28. #elif SAMPLE_RATE == SAMPLE_RATE_11025
  29. #define SAMPLE_RATE_EXACT AUDIO_SAMPLE_RATE_EXACT / 4
  30. #endif
  31. #define HALF_BUFFER NUM_SAMPLES / 2
  32. #define LOOP1(a) a
  33. #define LOOP2(a) a LOOP1(a)
  34. #define LOOP3(a) a LOOP2(a)
  35. #define LOOP8(a) a LOOP3(a) a LOOP3(a)
  36. #define UNROLL(n,a) LOOP##n(a)
  37. /**
  38. * Audio update function.
  39. */
  40. void AudioTuner::update( void ) {
  41. audio_block_t *block;
  42. const int16_t *p, *end;
  43. block = receiveReadOnly( );
  44. if ( !block ) return;
  45. if ( !enabled ) {
  46. release( block );
  47. return;
  48. }
  49. p = block->data;
  50. end = p + AUDIO_BLOCK_SAMPLES;
  51. /*
  52. * Double buffering, one fills while the other is processed
  53. * 2x the throughput.
  54. */
  55. uint16_t *dst;
  56. bool next = next_buffer;
  57. if ( next ) {
  58. //digitalWriteFast(6, HIGH);
  59. dst = ( uint16_t * )buffer;
  60. }
  61. else {
  62. //digitalWriteFast(6, LOW);
  63. dst = ( uint16_t * )buffer + NUM_SAMPLES;
  64. }
  65. // gather data/and release block
  66. uint16_t count = count_global;
  67. do {
  68. *( dst+count++ ) = *( uint16_t * )p;
  69. p += SAMPLE_RATE;
  70. } while ( p < end );
  71. release( block );
  72. /*
  73. * If buffer full switch to start filling next
  74. * buffer and process the just filled buffer.
  75. */
  76. if ( count >= NUM_SAMPLES ) {
  77. //digitalWriteFast(2, !digitalReadFast(2));
  78. __disable_irq();
  79. next_buffer = !next_buffer;
  80. process_buffer = true;
  81. count_global = 0;
  82. tau_global = 1;
  83. yin_idx = 1;
  84. running_sum = 0;
  85. count = 0;
  86. __enable_irq();
  87. }
  88. count_global = count;// update global count
  89. /*
  90. * Set the number of cycles to be processed per receiving block.
  91. */
  92. uint16_t cycles;
  93. const uint16_t usage_max = cpu_usage_max;
  94. if ( AudioProcessorUsage( ) > usage_max ) {
  95. #if NUM_SAMPLES >= 8192
  96. cycles = tau_global + 2;
  97. #elif NUM_SAMPLES == 4096
  98. cycles = tau_global + 4;
  99. #elif NUM_SAMPLES == 2048
  100. cycles = tau_global + 8;
  101. #elif NUM_SAMPLES <= 1024
  102. cycles = tau_global + 32;
  103. #endif
  104. }
  105. else {
  106. #if NUM_SAMPLES >= 8192
  107. cycles = tau_global + 8;
  108. #elif NUM_SAMPLES == 4096
  109. cycles = tau_global + 16;
  110. #elif NUM_SAMPLES == 2048
  111. cycles = tau_global + 32;
  112. #elif NUM_SAMPLES <= 1024
  113. cycles = tau_global + 64;
  114. #endif
  115. }
  116. if ( process_buffer ) {
  117. //digitalWriteFast(0, HIGH);
  118. uint16_t tau;
  119. next = next_buffer;
  120. tau = tau_global;
  121. do {
  122. int64_t sum = 0;
  123. const int16_t *end, *buf;
  124. if ( next ) {
  125. //digitalWriteFast(4, LOW);
  126. buf = buffer + NUM_SAMPLES;
  127. }
  128. else {
  129. //digitalWriteFast(4, HIGH);
  130. buf = buffer;
  131. }
  132. end = buf + HALF_BUFFER;
  133. // TODO: How to make faster?
  134. do {
  135. int16_t current, lag, delta;
  136. UNROLL( 8,
  137. lag = *( buf + tau );
  138. current = *buf++;
  139. delta = current - lag;
  140. //sum = multiply_accumulate_32x32_rshift32_rounded(sum, delta, delta);
  141. sum += delta*delta;
  142. );
  143. } while ( buf < end );
  144. running_sum += sum;
  145. yin_buffer[yin_idx] = sum*tau;
  146. rs_buffer[yin_idx] = running_sum;
  147. yin_idx = ( ++yin_idx >= 5 ) ? 0 : yin_idx;
  148. tau = estimate( yin_buffer, rs_buffer, yin_idx, tau );
  149. if ( tau == 0 ) {
  150. process_buffer = false;
  151. new_output = true;
  152. //digitalWriteFast(0, LOW);
  153. return;
  154. }
  155. else if ( tau >= HALF_BUFFER ) {
  156. process_buffer = false;
  157. new_output = false;
  158. //digitalWriteFast(0, LOW);
  159. return;
  160. }
  161. } while ( tau <= cycles );
  162. tau_global = tau;
  163. //digitalWriteFast(0, LOW);
  164. }
  165. }
  166. /**
  167. * check the sampled data for fundmental frequency
  168. *
  169. * @param yin buffer to hold sum*tau value
  170. * @param rs buffer to hold running sum for sampled window
  171. * @param head buffer index
  172. * @param tau lag we are currently working on this gets incremented
  173. *
  174. * @return tau
  175. */
  176. uint16_t AudioTuner::estimate( int64_t *yin, int64_t *rs, uint16_t head, uint16_t tau ) {
  177. const int64_t *p = ( int64_t * )yin;
  178. const int64_t *r = ( int64_t * )rs;
  179. uint16_t _tau, _head;
  180. _tau = tau;
  181. _head = head;
  182. if ( _tau > 4 ) {
  183. uint16_t idx0, idx1, idx2;
  184. idx0 = _head;
  185. idx1 = _head + 1;
  186. idx1 = ( idx1 >= 5 ) ? 0 : idx1;
  187. idx2 = head + 2;
  188. idx2 = ( idx2 >= 5 ) ? 0 : idx2;
  189. float s0, s1, s2;
  190. s0 = ( ( float )*( p+idx0 ) / *( r+idx0 ) );
  191. s1 = ( ( float )*( p+idx1 ) / *( r+idx1 ) );
  192. s2 = ( ( float )*( p+idx2 ) / *( r+idx2 ) );
  193. if ( s1 < yin_threshold && s1 < s2 ) {
  194. uint16_t period = _tau - 3;
  195. periodicity = 1 - s1;
  196. data = period + 0.5f * ( s0 - s2 ) / ( s0 - 2.0f * s1 + s2 );
  197. return 0;
  198. }
  199. //if ( s1 > 2.4 ) return _tau + 2;
  200. //else return _tau + 1;
  201. }
  202. return _tau + 1;
  203. }
  204. /**
  205. * Initialise
  206. *
  207. * @param threshold Allowed uncertainty
  208. * @param cpu_max How much cpu usage before throttling
  209. */
  210. void AudioTuner::initialize( float threshold, float cpu_max ) {
  211. __disable_irq( );
  212. cpu_usage_max = cpu_max*100;
  213. yin_threshold = threshold;
  214. process_buffer = false;
  215. periodicity = 0.0f;
  216. next_buffer = true;
  217. running_sum = 0;
  218. count_global = 0;
  219. yin_idx = 1;
  220. data = 0;
  221. enabled = true;
  222. __enable_irq( );
  223. }
  224. /**
  225. * available
  226. *
  227. * @return true if data is ready else false
  228. */
  229. bool AudioTuner::available( void ) {
  230. __disable_irq( );
  231. bool flag = new_output;
  232. if ( flag ) new_output = false;
  233. __enable_irq( );
  234. return flag;
  235. }
  236. /**
  237. * read processes the data samples for the Yin algorithm.
  238. *
  239. * @return frequency in hertz
  240. */
  241. float AudioTuner::read( void ) {
  242. __disable_irq( );
  243. float d = data;
  244. __enable_irq( );
  245. return SAMPLE_RATE_EXACT / d;
  246. }
  247. /**
  248. * Periodicity of the sampled signal from Yin algorithm from read function.
  249. *
  250. * @return periodicity
  251. */
  252. float AudioTuner::probability( void ) {
  253. __disable_irq( );
  254. float p = periodicity;
  255. __enable_irq( );
  256. return p;
  257. }
  258. /**
  259. * Initialise parameters.
  260. *
  261. * @param thresh Allowed uncertainty
  262. */
  263. void AudioTuner::threshold( float p ) {
  264. __disable_irq( );
  265. yin_threshold = p;
  266. __enable_irq( );
  267. }