|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
-
-
- #include "AudioTuner.h"
- #include "utility/dspinst.h"
- #include "arm_math.h"
-
- #define HALF_BLOCKS AUDIO_BLOCKS * 64
-
- #define LOOP1(a) a
- #define LOOP2(a) a LOOP1(a)
- #define LOOP3(a) a LOOP2(a)
- #define LOOP4(a) a LOOP3(a)
- #define LOOP8(a) a LOOP3(a) a LOOP3(a)
- #define LOOP16(a) a LOOP8(a) a LOOP2(a) a LOOP3(a)
- #define LOOP32(a) a LOOP16(a) a LOOP8(a) a LOOP1(a) a LOOP3(a)
- #define LOOP64(a) a LOOP32(a) a LOOP16(a) a LOOP8(a) a LOOP2(a) a LOOP1(a)
- #define UNROLL(n,a) LOOP##n(a)
-
- static void copy_buffer(void *destination, const void *source) {
- const uint16_t *src = (const uint16_t *)source;
- uint16_t *dst = (uint16_t *)destination;
- for (int i=0; i < AUDIO_BLOCK_SAMPLES; i++) *dst++ = *src++;
- }
-
- void AudioTuner::update( void ) {
-
- audio_block_t *block;
-
- block = receiveReadOnly();
- if (!block) return;
-
- if ( !enabled ) {
- release( block );
- return;
- }
-
- digitalWriteFast(2, HIGH);
- if ( next_buffer ) {
- blocklist1[state++] = block;
- if ( !first_run && process_buffer ) process( );
- } else {
- blocklist2[state++] = block;
- if ( !first_run && process_buffer ) process( );
- }
-
- if ( state >= AUDIO_BLOCKS ) {
- if ( next_buffer ) {
- if ( !first_run && process_buffer ) process( );
- for ( int i = 0; i < AUDIO_BLOCKS; i++ ) copy_buffer( AudioBuffer+( i * 0x80 ), blocklist1[i]->data );
- for ( int i = 0; i < AUDIO_BLOCKS; i++ ) release(blocklist1[i] );
- } else {
- if ( !first_run && process_buffer ) process( );
- for ( int i = 0; i < AUDIO_BLOCKS; i++ ) copy_buffer( AudioBuffer+( i * 0x80 ), blocklist2[i]->data );
- for ( int i = 0; i < AUDIO_BLOCKS; i++ ) release( blocklist2[i] );
- }
- process_buffer = true;
- first_run = false;
- state = 0;
-
- }
- }
-
- FASTRUN void AudioTuner::process( void ) {
-
-
- const int16_t *p;
- p = AudioBuffer;
-
- uint16_t cycles = 64;;
- uint16_t tau = tau_global;
- do {
- uint16_t x = 0;
- int64_t sum = 0;
-
- do {
-
-
- int16_t current, lag, delta;
-
- lag = *( ( int16_t * )p + ( x+tau ) );
- current = *( ( int16_t * )p+x );
- delta = ( current-lag );
- sum += delta * delta;
- #if F_CPU == 144000000
- x += 8;
- #elif F_CPU == 120000000
- x += 12;
- #elif F_CPU == 96000000
- x += 16;
- #elif F_CPU < 96000000
- x += 32;
- #endif
-
- } while ( x <= HALF_BLOCKS );
-
- running_sum += sum;
- yin_buffer[yin_idx] = sum*tau;
- rs_buffer[yin_idx] = running_sum;
- yin_idx = ( ++yin_idx >= 5 ) ? 0 : yin_idx;
- tau = estimate( yin_buffer, rs_buffer, yin_idx, tau );
-
- if ( tau == 0 ) {
- process_buffer = false;
- new_output = true;
- yin_idx = 1;
- running_sum = 0;
- tau_global = 1;
-
-
- return;
- }
- } while ( --cycles );
-
- if ( tau >= HALF_BLOCKS ) {
- process_buffer = false;
- new_output = false;
- yin_idx = 1;
- running_sum = 0;
- tau_global = 1;
-
- return;
- }
- tau_global = tau;
-
- }
-
-
- uint16_t AudioTuner::estimate( int64_t *yin, int64_t *rs, uint16_t head, uint16_t tau ) {
- const int64_t *y = ( int64_t * )yin;
- const int64_t *r = ( int64_t * )rs;
- uint16_t _tau, _head;
- const float thresh = yin_threshold;
- _tau = tau;
- _head = head;
-
- if ( _tau > 4 ) {
-
- uint16_t idx0, idx1, idx2;
- idx0 = _head;
- idx1 = _head + 1;
- idx1 = ( idx1 >= 5 ) ? 0 : idx1;
- idx2 = head + 2;
- idx2 = ( idx2 >= 5 ) ? 0 : idx2;
-
- float s0, s1, s2;
- s0 = ( ( float )*( y+idx0 ) / *( r+idx0 ) );
- s1 = ( ( float )*( y+idx1 ) / *( r+idx1 ) );
- s2 = ( ( float )*( y+idx2 ) / *( r+idx2 ) );
-
- if ( s1 < thresh && s1 < s2 ) {
- uint16_t period = _tau - 3;
- periodicity = 1 - s1;
- data = period + 0.5f * ( s0 - s2 ) / ( s0 - 2.0f * s1 + s2 );
- return 0;
- }
- }
- return _tau + 1;
- }
-
-
- void AudioTuner::initialize( float threshold ) {
- __disable_irq( );
- process_buffer = false;
- yin_threshold = threshold;
- periodicity = 0.0f;
- next_buffer = true;
- running_sum = 0;
- tau_global = 1;
- first_run = true;
- yin_idx = 1;
- enabled = true;
- state = 0;
- data = 0.0f;
- __enable_irq( );
- }
-
-
- bool AudioTuner::available( void ) {
- __disable_irq( );
- bool flag = new_output;
- if ( flag ) new_output = false;
- __enable_irq( );
- return flag;
- }
-
-
- float AudioTuner::read( void ) {
- __disable_irq( );
- float d = data;
- __enable_irq( );
- return AUDIO_SAMPLE_RATE_EXACT / d;
- }
-
-
- float AudioTuner::probability( void ) {
- __disable_irq( );
- float p = periodicity;
- __enable_irq( );
- return p;
- }
-
-
- void AudioTuner::threshold( float p ) {
- __disable_irq( );
- yin_threshold = p;
- __enable_irq( );
- }
|