@@ -21,12 +21,17 @@ | |||
*/ | |||
#include "AudioTuner.h" | |||
#include "utility/dspinst.h" | |||
#define HALF_BUFFER NUM_SAMPLES / 2 | |||
#define QUARTER_BUFFER NUM_SAMPLES / 4 | |||
#define EIGTH_BUFFER NUM_SAMPLES / 8 | |||
#define SIXTEENTH_BUFFER NUM_SAMPLES / 16 | |||
#define SAMPLE_RATE AUDIO_SAMPLE_RATE_EXACT / SAMPLE_SKIP | |||
#if SAMPLE_RATE == SAMPLE_RATE_44100 | |||
#define SAMPLE_RATE_EXACT AUDIO_SAMPLE_RATE_EXACT / 1 | |||
#elif SAMPLE_RATE == SAMPLE_RATE_22050 | |||
#define SAMPLE_RATE_EXACT AUDIO_SAMPLE_RATE_EXACT / 2 | |||
#elif SAMPLE_RATE == SAMPLE_RATE_11025 | |||
#define SAMPLE_RATE_EXACT AUDIO_SAMPLE_RATE_EXACT / 4 | |||
#endif | |||
#define HALF_BUFFER NUM_SAMPLES / 2 | |||
#define LOOP1(a) a | |||
#define LOOP2(a) a LOOP1(a) | |||
@@ -34,15 +39,14 @@ | |||
#define LOOP8(a) a LOOP3(a) a LOOP3(a) | |||
#define UNROLL(n,a) LOOP##n(a) | |||
#define WINDOW SAMPLE_SKIP - 1 | |||
/** | |||
* audio update function. | |||
* Audio update function. | |||
*/ | |||
void AudioTuner::update( void ) { | |||
audio_block_t *block; | |||
const int16_t *p, *end; | |||
block = receiveReadOnly( ); | |||
if ( !block ) return; | |||
if ( !enabled ) { | |||
@@ -53,33 +57,70 @@ void AudioTuner::update( void ) { | |||
p = block->data; | |||
end = p + AUDIO_BLOCK_SAMPLES; | |||
/* | |||
* Set the number of cycles to processed per receiving block. | |||
* | |||
*/ | |||
uint16_t cycles; | |||
const uint16_t usage_max = cpu_usage_max; | |||
if ( AudioProcessorUsage( ) > usage_max ) { | |||
#if NUM_SAMPLES >= 8192 | |||
cycles = tau_global + 2; | |||
#elif NUM_SAMPLES == 4096 | |||
cycles = tau_global + 4; | |||
#elif NUM_SAMPLES == 2048 | |||
cycles = tau_global + 8; | |||
#elif NUM_SAMPLES <= 1024 | |||
cycles = tau_global + 16; | |||
#endif | |||
} | |||
else { | |||
#if NUM_SAMPLES >= 8192 | |||
cycles = tau_global + 8; | |||
#elif NUM_SAMPLES == 4096 | |||
cycles = tau_global + 16; | |||
#elif NUM_SAMPLES == 2048 | |||
cycles = tau_global + 32; | |||
#elif NUM_SAMPLES <= 1024 | |||
cycles = tau_global + 64; | |||
#endif | |||
} | |||
uint16_t count = count_global; | |||
/* | |||
* Double buffering, one fill while the other is processed | |||
* 2x the throughput. | |||
*/ | |||
uint16_t *dst; | |||
if ( next_buffer ) dst = ( uint16_t * )buffer; | |||
else dst = ( uint16_t * )buffer + NUM_SAMPLES; | |||
uint8_t get_sample = 0; | |||
uint16_t count = block_count; | |||
// gather data | |||
do { | |||
if ( get_sample++ >= WINDOW ) { | |||
*( dst+count++ ) = *( uint16_t * )p; | |||
get_sample = 0; | |||
} | |||
} while ( p++ < end ); | |||
*( dst+count++ ) = *( uint16_t * )p; | |||
p += SAMPLE_RATE; | |||
} while ( p < end ); | |||
release( block ); | |||
/* | |||
* If buffer full switch to start filling next | |||
* buffer and process the just filled buffer. | |||
*/ | |||
if ( count >= NUM_SAMPLES ) { | |||
//digitalWriteFast(2, !digitalReadFast(2)); | |||
next_buffer = !next_buffer; | |||
process_buffer = true; | |||
tau_global = 1; | |||
yin_idx = 1; | |||
running_sum = 0; | |||
count = 0; | |||
process_buffer = true; | |||
count_global = 0; | |||
tau_global = 1; | |||
yin_idx = 1; | |||
running_sum = 0; | |||
count = 0; | |||
} | |||
block_count = count; | |||
count_global = count;// update global count | |||
if ( process_buffer ) { | |||
digitalWriteFast(0, HIGH); | |||
//digitalWriteFast(0, HIGH); | |||
uint16_t tau; | |||
uint16_t next; | |||
next = next_buffer; | |||
@@ -96,6 +137,7 @@ void AudioTuner::update( void ) { | |||
lag = *( buf + tau ); | |||
current = *buf++; | |||
delta = current - lag; | |||
//sum = multiply_accumulate_32x32_rshift32_rounded(sum, delta, delta); | |||
sum += delta*delta; | |||
); | |||
} while ( buf < end ); | |||
@@ -108,19 +150,19 @@ void AudioTuner::update( void ) { | |||
tau = estimate( yin_buffer, rs_buffer, yin_idx, tau ); | |||
if ( tau == 0 ) { | |||
process_buffer = false; | |||
new_output = true; | |||
process_buffer = false; | |||
new_output = true; | |||
//digitalWriteFast(0, LOW); | |||
return; | |||
} | |||
else if ( tau >= HALF_BUFFER ) { | |||
process_buffer = false; | |||
new_output = false; | |||
process_buffer = false; | |||
new_output = false; | |||
//digitalWriteFast(0, LOW); | |||
return; | |||
} | |||
} while ( tau <= ( tau_global + 31 ) ); | |||
} while ( tau <= cycles ); | |||
tau_global = tau; | |||
//digitalWriteFast(0, LOW); | |||
} | |||
@@ -144,6 +186,7 @@ uint16_t AudioTuner::estimate( int64_t *yin, int64_t *rs, uint16_t head, uint16_ | |||
_head = head; | |||
if ( _tau > 4 ) { | |||
uint16_t idx0, idx1, idx2; | |||
idx0 = _head; | |||
idx1 = _head + 1; | |||
@@ -156,29 +199,50 @@ uint16_t AudioTuner::estimate( int64_t *yin, int64_t *rs, uint16_t head, uint16_ | |||
s1 = ( ( float )*( p+idx1 ) / r[idx1] ); | |||
s2 = ( ( float )*( p+idx2 ) / r[idx2] ); | |||
if ( s1 < threshold && s1 < s2 ) { | |||
if ( s1 < yin_threshold && s1 < s2 ) { | |||
uint16_t period = _tau - 3; | |||
periodicity = 1 - s1; | |||
data = period + 0.5f * ( s0 - s2 ) / ( s0 - 2.0f * s1 + s2 ); | |||
return 0; | |||
} | |||
if ( s1 > 2.2 ) return _tau + 2; | |||
if ( s1 > 2.4 ) return _tau + 2; | |||
else return _tau + 1; | |||
} | |||
return _tau + 1; | |||
} | |||
/** | |||
* Initialise | |||
* | |||
* @param threshold Allowed uncertainty | |||
* @param cpu_max How much cpu usage before throttling | |||
*/ | |||
void AudioTuner::initialize( float threshold, uint8_t cpu_max ) { | |||
__disable_irq( ); | |||
cpu_usage_max = cpu_max; | |||
yin_threshold = threshold; | |||
process_buffer = false; | |||
periodicity = 0.0f; | |||
next_buffer = 1; | |||
running_sum = 0; | |||
count_global = 0; | |||
yin_idx = 1; | |||
data = 0; | |||
enabled = true; | |||
__enable_irq( ); | |||
} | |||
/** | |||
* available | |||
* | |||
* @return true if data is ready else false | |||
*/ | |||
bool AudioTuner::available( void ) { | |||
__disable_irq(); | |||
__disable_irq( ); | |||
bool flag = new_output; | |||
if (flag) new_output = false; | |||
__enable_irq(); | |||
if ( flag ) new_output = false; | |||
__enable_irq( ); | |||
return flag; | |||
} | |||
@@ -188,7 +252,11 @@ bool AudioTuner::available( void ) { | |||
* @return frequency in hertz | |||
*/ | |||
float AudioTuner::read( void ) { | |||
return SAMPLE_RATE / data; | |||
__disable_irq( ); | |||
float d = data; | |||
__enable_irq( ); | |||
d = SAMPLE_RATE_EXACT / d; | |||
return d; | |||
} | |||
/** | |||
@@ -197,7 +265,10 @@ float AudioTuner::read( void ) { | |||
* @return periodicity | |||
*/ | |||
float AudioTuner::probability( void ) { | |||
return periodicity; | |||
__disable_irq( ); | |||
float p = periodicity; | |||
__enable_irq( ); | |||
return p; | |||
} | |||
/** | |||
@@ -205,17 +276,8 @@ float AudioTuner::probability( void ) { | |||
* | |||
* @param thresh Allowed uncertainty | |||
*/ | |||
void AudioTuner::set_threshold( float thresh ) { | |||
void AudioTuner::threshold( float p ) { | |||
__disable_irq( ); | |||
threshold = thresh; | |||
process_buffer = false; | |||
periodicity = 0.0f; | |||
next_buffer = 1; | |||
running_sum = 0; | |||
block_count = 0; | |||
block_count = 0; | |||
enabled = true; | |||
yin_idx = 1; | |||
data = 0; | |||
__enable_irq( ); | |||
yin_threshold = p; | |||
__enable_irq( ); | |||
} |
@@ -25,23 +25,37 @@ | |||
#include "AudioStream.h" | |||
/****************************************************************/ | |||
#define SAMPLE_RATE_DIVIDE_BY_1 1 // 44100 sample rate | |||
#define SAMPLE_RATE_DIVIDE_BY_2 2 // 22050 sample rate | |||
#define SAMPLE_RATE_DIVIDE_BY_4 4 // 11025 sample rate | |||
#define SAMPLE_RATE_DIVIDE_BY_8 8 // 5512.5 sample rate | |||
#define SAMPLE_RATE_DIVIDE_BY_16 16 // 2756.25 sample rate | |||
#define SAMPLE_RATE_DIVIDE_BY_32 32 // 1378.125 sample rate | |||
#define SAMPLE_RATE_44100 1 // 44100 sample rate | |||
#define SAMPLE_RATE_22050 2 // 22050 sample rate | |||
#define SAMPLE_RATE_11025 4 // 11025 sample rate | |||
/****************************************************************/ | |||
/**************************************************************** | |||
* Safe to adjust these values below * | |||
* * | |||
* These two parameters define how this object works. * | |||
* * | |||
* 1. NUM_SAMPLES - Size of the buffer. Since object uses * | |||
* double buffering this value will be 4x in bytes of * | |||
* memory. !!! Must be power of 2 !!!! * | |||
* * | |||
* 2. SAMPLE_RATE - Just what it says. * | |||
* * | |||
* These two parameters work hand in hand. For example if you * | |||
* want a high sample rate but do not allocate enough buffer * | |||
* space, you will be limit how low of a frequency you can * | |||
* measure. If you then increase the buffer you use up * | |||
* precious ram and slow down the system since it takes longer * | |||
* to processes the buffer. * | |||
* * | |||
* Play around with these values to find what best suits your * | |||
* needs. The max number of buffers you can have is 8192 bins. * | |||
****************************************************************/ | |||
// Adjust number of samples to collect in buffer here, also effects | |||
// convergence speed and resolution. | |||
// !!! Must be power of 2 !!!! | |||
#define NUM_SAMPLES 2048 // make a power of two | |||
// larger the divide-by, less resolution and lower the frequency for | |||
// a given number of samples that can be detected. Also effects | |||
// convergence speed. | |||
#define SAMPLE_SKIP SAMPLE_RATE_DIVIDE_BY_2 | |||
// Use defined sample rates above^ | |||
#define SAMPLE_RATE SAMPLE_RATE_22050 | |||
/****************************************************************/ | |||
class AudioTuner : public AudioStream | |||
@@ -52,14 +66,23 @@ public: | |||
* | |||
* @return none | |||
*/ | |||
AudioTuner( void ) : AudioStream( 1, inputQueueArray ), enabled( false ), new_output(false){ } | |||
AudioTuner( void ) : AudioStream( 1, inputQueueArray ), enabled( false ), new_output(false) { | |||
digitalWriteFast(2, LOW); | |||
} | |||
/** | |||
* initialize variables and start conversion | |||
* | |||
* @param threshold Allowed uncertainty | |||
* @param cpu_max How much cpu usage before throttling | |||
*/ | |||
void initialize( float threshold, uint8_t cpu_max); | |||
/** | |||
* sets threshold value | |||
* | |||
* @param thresh | |||
*/ | |||
void set_threshold( float thresh ); | |||
void threshold( float p ); | |||
/** | |||
* triggers true when valid frequency is found | |||
@@ -77,7 +100,7 @@ public: | |||
/** | |||
* get predicitity | |||
* | |||
* @return probability of correct freq found | |||
* @return probability of frequency found | |||
*/ | |||
float probability( void ); | |||
@@ -88,7 +111,7 @@ public: | |||
private: | |||
/** | |||
* check the sampled data for fundmental frequency | |||
* check the sampled data for fundamental frequency | |||
* | |||
* @param yin buffer to hold sum*tau value | |||
* @param rs buffer to hold running sum for sampled window | |||
@@ -100,10 +123,10 @@ private: | |||
uint16_t estimate( int64_t *yin, int64_t *rs, uint16_t head, uint16_t tau ); | |||
int16_t buffer[NUM_SAMPLES*2] __attribute__ ( ( aligned ( 4 ) ) ); | |||
float periodicity, threshold, data; | |||
float periodicity, yin_threshold, data; | |||
int64_t rs_buffer[5], yin_buffer[5]; | |||
uint64_t running_sum; | |||
uint16_t block_count, tau_global; | |||
uint16_t tau_global, count_global, tau_cycles, cpu_usage_max; | |||
uint8_t next_buffer, yin_idx; | |||
bool enabled, process_buffer; | |||
volatile bool new_output; |
@@ -1,6 +1,6 @@ | |||
<p align="center"> | |||
<b>Guitar and Bass Tuner Library</b><br> | |||
<b>Teensy 3.1 v2.0</b><br> | |||
<b>Teensy 3.1/2 v2.1</b><br> | |||
</p> | |||
>Software algorithm ([YIN]) for guitar and bass tuning using a Teensy Audio Library. This audio object's algorithm can be some what memory and processor hungry but will allow you to detect with fairly good accuracy the fundamental frequencies f<sub>o</sub> from electric guitars and basses. | |||
@@ -43,48 +43,46 @@ | |||
>Many optimizations have been done to the [YIN] algorithm for frequencies between 29-360Hz. | |||
>>While its still using a brute force method ( n<sup>2</sup> ) for finding the fundamental frequency f<sub>o</sub>, it is tuned to skip certain <b>tau</b> (<img src="http://latex.numberempire.com/render?%5Cinline%20%5Chuge%20%5Cmathbf%7B%5Ctau%7D&sig=845639da85c0dd8e2de679817b06639c"/></img>) values and focus mostly on frequencies found in the bass and guitar. | |||
>>>The input is double buffered so while you are processing one buffer it is filling the other to double throughput. | |||
>>>>There are a few parameters that can be adjusted to "dial in" the algorithm for better estimations. The defaults are what I found that have the best trade off for speed and accuracy. | |||
>>>>There are a few parameters that can be adjusted to "dial in" the algorithm for better estimations located in AudioTuner.h. The defaults below are what I found that have the best trade off for speed and accuracy. | |||
<h4>AudioTuner.h</h4> | |||
``` | |||
/****************************************************************/ | |||
#define SAMPLE_RATE_DIVIDE_BY_1 1 // 44100 sample rate | |||
#define SAMPLE_RATE_DIVIDE_BY_2 2 // 22050 sample rate | |||
#define SAMPLE_RATE_DIVIDE_BY_4 4 // 11025 sample rate | |||
#define SAMPLE_RATE_DIVIDE_BY_8 8 // 5512.5 sample rate | |||
#define SAMPLE_RATE_DIVIDE_BY_16 16 // 2756.25 sample rate | |||
#define SAMPLE_RATE_DIVIDE_BY_32 32 // 1378.125 sample rate | |||
#define SAMPLE_RATE_44100 1 // 44100 sample rate | |||
#define SAMPLE_RATE_22050 2 // 22050 sample rate | |||
#define SAMPLE_RATE_11025 4 // 11025 sample rate | |||
/****************************************************************/ | |||
/**************************************************************** | |||
* Safe to adjust these values below * | |||
* * | |||
* These two parameters define how this object works. * | |||
* * | |||
* 1. NUM_SAMPLES - Size of the buffer. Since object uses * | |||
* double buffering this value will be 4x in bytes of * | |||
* memory. !!! Must be power of 2 !!!! * | |||
* * | |||
* 2. SAMPLE_RATE - Just what it says. * | |||
* * | |||
* These two parameters work hand in hand. For example if you * | |||
* want a high sample rate but do not allocate enough buffer * | |||
* space, you will be limit how low of a frequency you can * | |||
* measure. If you then increase the buffer you use up * | |||
* precious ram and slow down the system since it takes longer * | |||
* to processes the buffer. * | |||
* * | |||
* Play around with these values to find what best suits your * | |||
* needs. The max number of buffers you can have is 8192 bins. * | |||
****************************************************************/ | |||
// Adjust number of samples to collect in buffer here, also effects | |||
// convergence speed and resolution. | |||
// !!! Must be power of 2 !!!! | |||
#define NUM_SAMPLES 2048 // make a power of two | |||
// larger the divide-by, less resolution and lower the frequency for | |||
// a given number of samples that can be detected. Also effects | |||
// convergence speed. | |||
#define SAMPLE_SKIP SAMPLE_RATE_DIVIDE_BY_2 | |||
// Use defined sample rates above^ | |||
#define SAMPLE_RATE SAMPLE_RATE_22050 | |||
/****************************************************************/ | |||
``` | |||
``` | |||
SAMPLE_RATE_DIVIDE_BY_x --> This sets 'SAMPLE_SKIP' to pass on every (x) data point from | |||
the Audio Block being saved to the buffer, it determines the | |||
sample rate. | |||
``` | |||
``` | |||
NUM_SAMPLES --> This the size of each buffer, there two for double buffering. | |||
``` | |||
``` | |||
SAMPLE_SKIP --> This sets your sample window length and sampling rate. Sample Window Size | |||
is (NUM_SAMPLES * SAMPLE_SKIP) of the ~44100 samples every second. Sample | |||
Rate is (AUDIO_SAMPLE_RATE_EXACT / SAMPLE_SKIP). | |||
``` | |||
<div> | |||
<b>YIN Algorithm</b> | |||
<ol> |
@@ -19,6 +19,7 @@ | |||
the teensy can store in flash these notes are truncated to ~120,000B or about 1/2 of the whole | |||
signal. | |||
*/ | |||
#include <SerialFlash.h> | |||
#include <AudioTuner.h> | |||
#include <Audio.h> | |||
#include <Wire.h> | |||
@@ -56,19 +57,22 @@ void playNote(void) { | |||
} | |||
//--------------------------------------------------------------------------------------- | |||
void setup() { | |||
// put your setup code here, to run once: | |||
AudioMemory(4); | |||
tuner.set_threshold( .05f ); | |||
/* | |||
* Intialize the yin algorithm's threshold | |||
* and percent of current cpu usage used | |||
* before slowing the algorithm down. | |||
*/ | |||
tuner.initialize(.15f, 90); | |||
pinMode(LED_BUILTIN, OUTPUT); | |||
playNoteTimer.begin(playNote, 1000); | |||
} | |||
void loop() { | |||
// put your main code here, to run repeatedly: | |||
// read back fundmental frequency | |||
if (tuner.available()) { | |||
float note = tuner.read(); | |||
float prob = tuner.probability(); | |||
Serial.printf("Note: %3.2f | Probility: %.2f\n", note, prob); | |||
} | |||
} |
@@ -20,6 +20,7 @@ | |||
You can change the amplitude by typing "a " + amplitude in the serial monitor. (0,1) | |||
EX. "a .5" | |||
*/ | |||
#include <SerialFlash.h> | |||
#include <AudioTuner.h> | |||
#include <Audio.h> | |||
#include <Wire.h> | |||
@@ -39,15 +40,19 @@ AudioConnection patchCord3(mixer, 0, dac, 0); | |||
char buffer[10]; | |||
void setup() { | |||
// put your setup code here, to run once: | |||
AudioMemory(4); | |||
tuner.set_threshold( .05f ); | |||
/* | |||
* Intialize the yin algorithm's threshold | |||
* and percent of current cpu usage used | |||
* before slowing the algorithm down. | |||
*/ | |||
tuner.initialize(.15f, 90); | |||
sine.frequency(30.87); | |||
sine.amplitude(1); | |||
} | |||
void loop() { | |||
// put your main code here, to run repeatedly: | |||
// read back fundmental frequency | |||
if (tuner.available()) { | |||
float note = tuner.read(); | |||
float prob = tuner.probability(); |
@@ -1,5 +1,5 @@ | |||
name=AudioTuner | |||
version=2.0 | |||
version=2.1 | |||
author=Colin Duffy | |||
maintainer=Colin Duffy | |||
sentence=Yin algorithm |
@@ -1,2 +1,6 @@ | |||
><b>Updated (10/11/15 v2.1)</b><br> | |||
* Made yin implementation faster and more reliable.<br> | |||
* Improved user interface.<br> | |||
><b>Updated (7/10/15 v2.0)</b><br> | |||
* First commit |