瀏覽代碼

update - see revision

main
duff2013 9 年之前
父節點
當前提交
4158c208e7
共有 8 個文件被更改,包括 199 次插入103 次删除
  1. +109
    -47
      AudioTuner.cpp
  2. +41
    -18
      AudioTuner.h
  3. +28
    -30
      README.md
  4. +8
    -4
      examples/Sample_Guitar_Tunning_Notes/Sample_Guitar_Tunning_Notes.ino
  5. 二進制
      examples/Simple_Sine/Simple_Sine.cpp.elf
  6. +8
    -3
      examples/Simple_Sine/Simple_Sine.ino
  7. +1
    -1
      library.properties
  8. +4
    -0
      revision.md

+ 109
- 47
AudioTuner.cpp 查看文件

*/ */


#include "AudioTuner.h" #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 LOOP1(a) a
#define LOOP2(a) a LOOP1(a) #define LOOP2(a) a LOOP1(a)
#define LOOP8(a) a LOOP3(a) a LOOP3(a) #define LOOP8(a) a LOOP3(a) a LOOP3(a)
#define UNROLL(n,a) LOOP##n(a) #define UNROLL(n,a) LOOP##n(a)


#define WINDOW SAMPLE_SKIP - 1

/** /**
* audio update function.
* Audio update function.
*/ */
void AudioTuner::update( void ) { void AudioTuner::update( void ) {
audio_block_t *block; audio_block_t *block;
const int16_t *p, *end; const int16_t *p, *end;
block = receiveReadOnly( ); block = receiveReadOnly( );
if ( !block ) return; if ( !block ) return;
if ( !enabled ) { if ( !enabled ) {
p = block->data; p = block->data;
end = p + AUDIO_BLOCK_SAMPLES; 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; uint16_t *dst;
if ( next_buffer ) dst = ( uint16_t * )buffer; if ( next_buffer ) dst = ( uint16_t * )buffer;
else dst = ( uint16_t * )buffer + NUM_SAMPLES; else dst = ( uint16_t * )buffer + NUM_SAMPLES;
uint8_t get_sample = 0;
uint16_t count = block_count;
// gather data
do { 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 ); release( block );

/*
* If buffer full switch to start filling next
* buffer and process the just filled buffer.
*/
if ( count >= NUM_SAMPLES ) { if ( count >= NUM_SAMPLES ) {
//digitalWriteFast(2, !digitalReadFast(2)); //digitalWriteFast(2, !digitalReadFast(2));
next_buffer = !next_buffer; 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 ) { if ( process_buffer ) {
digitalWriteFast(0, HIGH);
//digitalWriteFast(0, HIGH);
uint16_t tau; uint16_t tau;
uint16_t next; uint16_t next;
next = next_buffer; next = next_buffer;
lag = *( buf + tau ); lag = *( buf + tau );
current = *buf++; current = *buf++;
delta = current - lag; delta = current - lag;
//sum = multiply_accumulate_32x32_rshift32_rounded(sum, delta, delta);
sum += delta*delta; sum += delta*delta;
); );
} while ( buf < end ); } while ( buf < end );
tau = estimate( yin_buffer, rs_buffer, yin_idx, tau ); tau = estimate( yin_buffer, rs_buffer, yin_idx, tau );
if ( tau == 0 ) { if ( tau == 0 ) {
process_buffer = false;
new_output = true;
process_buffer = false;
new_output = true;
//digitalWriteFast(0, LOW); //digitalWriteFast(0, LOW);
return; return;
} }
else if ( tau >= HALF_BUFFER ) { else if ( tau >= HALF_BUFFER ) {
process_buffer = false;
new_output = false;
process_buffer = false;
new_output = false;
//digitalWriteFast(0, LOW); //digitalWriteFast(0, LOW);
return; return;
} }
} while ( tau <= ( tau_global + 31 ) );
} while ( tau <= cycles );
tau_global = tau; tau_global = tau;
//digitalWriteFast(0, LOW); //digitalWriteFast(0, LOW);
} }
_head = head; _head = head;
if ( _tau > 4 ) { if ( _tau > 4 ) {
uint16_t idx0, idx1, idx2; uint16_t idx0, idx1, idx2;
idx0 = _head; idx0 = _head;
idx1 = _head + 1; idx1 = _head + 1;
s1 = ( ( float )*( p+idx1 ) / r[idx1] ); s1 = ( ( float )*( p+idx1 ) / r[idx1] );
s2 = ( ( float )*( p+idx2 ) / r[idx2] ); s2 = ( ( float )*( p+idx2 ) / r[idx2] );
if ( s1 < threshold && s1 < s2 ) {
if ( s1 < yin_threshold && s1 < s2 ) {
uint16_t period = _tau - 3; uint16_t period = _tau - 3;
periodicity = 1 - s1; periodicity = 1 - s1;
data = period + 0.5f * ( s0 - s2 ) / ( s0 - 2.0f * s1 + s2 ); data = period + 0.5f * ( s0 - s2 ) / ( s0 - 2.0f * s1 + s2 );
return 0; return 0;
} }
if ( s1 > 2.2 ) return _tau + 2;
if ( s1 > 2.4 ) return _tau + 2;
else return _tau + 1; else return _tau + 1;
} }
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 * available
* *
* @return true if data is ready else false * @return true if data is ready else false
*/ */
bool AudioTuner::available( void ) { bool AudioTuner::available( void ) {
__disable_irq();
__disable_irq( );
bool flag = new_output; bool flag = new_output;
if (flag) new_output = false;
__enable_irq();
if ( flag ) new_output = false;
__enable_irq( );
return flag; return flag;
} }


* @return frequency in hertz * @return frequency in hertz
*/ */
float AudioTuner::read( void ) { float AudioTuner::read( void ) {
return SAMPLE_RATE / data;
__disable_irq( );
float d = data;
__enable_irq( );
d = SAMPLE_RATE_EXACT / d;
return d;
} }


/** /**
* @return periodicity * @return periodicity
*/ */
float AudioTuner::probability( void ) { float AudioTuner::probability( void ) {
return periodicity;
__disable_irq( );
float p = periodicity;
__enable_irq( );
return p;
} }


/** /**
* *
* @param thresh Allowed uncertainty * @param thresh Allowed uncertainty
*/ */
void AudioTuner::set_threshold( float thresh ) {
void AudioTuner::threshold( float p ) {
__disable_irq( ); __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( );
} }

+ 41
- 18
AudioTuner.h 查看文件



#include "AudioStream.h" #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 * * 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 #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 class AudioTuner : public AudioStream
* *
* @return none * @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 * sets threshold value
* *
* @param thresh * @param thresh
*/ */
void set_threshold( float thresh );
void threshold( float p );
/** /**
* triggers true when valid frequency is found * triggers true when valid frequency is found
/** /**
* get predicitity * get predicitity
* *
* @return probability of correct freq found
* @return probability of frequency found
*/ */
float probability( void ); float probability( void );
private: private:
/** /**
* check the sampled data for fundmental frequency
* check the sampled data for fundamental frequency
* *
* @param yin buffer to hold sum*tau value * @param yin buffer to hold sum*tau value
* @param rs buffer to hold running sum for sampled window * @param rs buffer to hold running sum for sampled window
uint16_t estimate( int64_t *yin, int64_t *rs, uint16_t head, uint16_t tau ); uint16_t estimate( int64_t *yin, int64_t *rs, uint16_t head, uint16_t tau );
int16_t buffer[NUM_SAMPLES*2] __attribute__ ( ( aligned ( 4 ) ) ); 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]; int64_t rs_buffer[5], yin_buffer[5];
uint64_t running_sum; 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; uint8_t next_buffer, yin_idx;
bool enabled, process_buffer; bool enabled, process_buffer;
volatile bool new_output; volatile bool new_output;

+ 28
- 30
README.md 查看文件

<p align="center"> <p align="center">
<b>Guitar and Bass Tuner Library</b><br> <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> </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. >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.
>Many optimizations have been done to the [YIN] algorithm for frequencies between 29-360Hz. >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. >>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. >>>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> <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 * * 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 #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> <div>
<b>YIN Algorithm</b> <b>YIN Algorithm</b>
<ol> <ol>

+ 8
- 4
examples/Sample_Guitar_Tunning_Notes/Sample_Guitar_Tunning_Notes.ino 查看文件

the teensy can store in flash these notes are truncated to ~120,000B or about 1/2 of the whole the teensy can store in flash these notes are truncated to ~120,000B or about 1/2 of the whole
signal. signal.
*/ */
#include <SerialFlash.h>
#include <AudioTuner.h> #include <AudioTuner.h>
#include <Audio.h> #include <Audio.h>
#include <Wire.h> #include <Wire.h>
} }
//--------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------
void setup() { void setup() {
// put your setup code here, to run once:
AudioMemory(4); 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); pinMode(LED_BUILTIN, OUTPUT);
playNoteTimer.begin(playNote, 1000); playNoteTimer.begin(playNote, 1000);
} }


void loop() { void loop() {
// put your main code here, to run repeatedly:
// read back fundmental frequency
if (tuner.available()) { if (tuner.available()) {
float note = tuner.read(); float note = tuner.read();
float prob = tuner.probability(); float prob = tuner.probability();
Serial.printf("Note: %3.2f | Probility: %.2f\n", note, prob); Serial.printf("Note: %3.2f | Probility: %.2f\n", note, prob);
} }
} }

二進制
examples/Simple_Sine/Simple_Sine.cpp.elf 查看文件


+ 8
- 3
examples/Simple_Sine/Simple_Sine.ino 查看文件

You can change the amplitude by typing "a " + amplitude in the serial monitor. (0,1) You can change the amplitude by typing "a " + amplitude in the serial monitor. (0,1)
EX. "a .5" EX. "a .5"
*/ */
#include <SerialFlash.h>
#include <AudioTuner.h> #include <AudioTuner.h>
#include <Audio.h> #include <Audio.h>
#include <Wire.h> #include <Wire.h>
char buffer[10]; char buffer[10];


void setup() { void setup() {
// put your setup code here, to run once:
AudioMemory(4); 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.frequency(30.87);
sine.amplitude(1); sine.amplitude(1);
} }


void loop() { void loop() {
// put your main code here, to run repeatedly:
// read back fundmental frequency
if (tuner.available()) { if (tuner.available()) {
float note = tuner.read(); float note = tuner.read();
float prob = tuner.probability(); float prob = tuner.probability();

+ 1
- 1
library.properties 查看文件

name=AudioTuner name=AudioTuner
version=2.0
version=2.1
author=Colin Duffy author=Colin Duffy
maintainer=Colin Duffy maintainer=Colin Duffy
sentence=Yin algorithm sentence=Yin algorithm

+ 4
- 0
revision.md 查看文件

><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> ><b>Updated (7/10/15 v2.0)</b><br>
* First commit * First commit

Loading…
取消
儲存