duff2013 7年前
コミット
69b66c6145
9個のファイルの変更265行の追加173行の削除
  1. +119
    -117
      AudioTuner.cpp
  2. +52
    -34
      AudioTuner.h
  3. +19
    -16
      README.md
  4. +4
    -2
      examples/Sample_Guitar_Tunning_Notes/Sample_Guitar_Tunning_Notes.ino
  5. +31
    -0
      examples/Sample_Guitar_Tunning_Notes/coeff.h
  6. +3
    -1
      examples/Simple_Sine/Simple_Sine.ino
  7. +31
    -0
      examples/Simple_Sine/coeff.h
  8. +1
    -2
      library.properties
  9. +5
    -1
      revision.md

+ 119
- 117
AudioTuner.cpp ファイルの表示

@@ -1,4 +1,4 @@
/* Audio Library Guitar and Bass Tuner
/* Audio Library Note Frequency Detection & Guitar/Bass Tuner
* Copyright (c) 2015, Colin Duffy
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -20,27 +20,12 @@
* THE SOFTWARE.
*/

#include "AudioTuner.h"
#include "analyze_notefreq_fast.h"
#include "utility/dspinst.h"
#include "arm_math.h"
#include "Arduino.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++;
}
#define NUM_SAMPLES ( AUDIO_GUITARTUNER_BLOCKS << 7 )

void AudioTuner::update( void ) {
@@ -54,119 +39,122 @@ void AudioTuner::update( void ) {
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( );
}
/**
* "factor" is the new block size calculatedby
* the decimated shift to incremnt the buffer
* address.
*/
const uint8_t factor = AUDIO_BLOCK_SAMPLES >> decimation_shift;
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;
//digitalWriteFast(LED_BUILTIN, !digitalReadFast(LED_BUILTIN));
// filter and decimate block by block the incoming signal and store in a buffer.
arm_fir_decimate_fast_q15( &firDecimateInst, block->data, AudioBuffer + ( state * factor ), AUDIO_BLOCK_SAMPLES );
/**
* when half the blocks + 1 of the total
* blocks have been stored in the buffer
* start processing the data.
*/
if ( state++ >= AUDIO_GUITARTUNER_BLOCKS >> 1 ) {
if ( process_buffer ) process( AudioBuffer );
if ( state == 0 ) process_buffer = true;
}
release( block );
}

FASTRUN void AudioTuner::process( void ) {
//digitalWriteFast(0, HIGH);
const int16_t *p;
p = AudioBuffer;
/**
* Start the Yin algorithm
*
* TODO: Significant speed up would be to use spectral domain to find fundamental frequency.
* This paper explains: https://aubio.org/phd/thesis/brossier06thesis.pdf -> Section 3.2.4
* page 79. Might have to downsample for low fundmental frequencies because of fft buffer
* size limit.
*/
void AudioTuner::process( int16_t *p ) {
uint16_t cycles = 64;;
const uint16_t inner_cycles = ( NUM_SAMPLES >> decimation_shift ) >> 1;
uint16_t outer_cycles = inner_cycles / AUDIO_GUITARTUNER_BLOCKS;
uint16_t tau = tau_global;
do {
uint16_t x = 0;
int64_t sum = 0;
//uint32_t res;
do {
/*int16_t current1, lag1, current2, lag2;
int32_t val1, val2;
lag1 = *( ( uint32_t * )p + ( x + tau ) );
current1 = *( ( uint32_t * )p + x );
x += 32;
lag2 = *( ( uint32_t * )p + ( x + tau ) );
current2 = *( ( uint32_t * )p + x );
val1 = __PKHBT(current1, current2, 0x10);
val2 = __PKHBT(lag1, lag2, 0x10);
res = __SSUB16( val1, val2 );
sum = __SMLALD(res, res, sum);
//sum = __SMLSLD(delta1, delta2, sum);*/
int16_t current, lag, delta;
//UNROLL(16,
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;
uint64_t sum = 0;
int32_t a1, a2, b1, b2, c1, c2, d1, d2;
int32_t out1, out2, out3, out4;
uint16_t blkCnt;
int16_t * cur = p;
int16_t * lag = p + tau;
// unrolling the inner loop by 8
blkCnt = inner_cycles >> 3;
do
{
// a(n), b(n), c(n), d(n) each hold two samples
a1 = *__SIMD32( cur )++;
a2 = *__SIMD32( cur )++;
b1 = *__SIMD32( lag )++;
b2 = *__SIMD32( lag )++;
c1 = *__SIMD32( cur )++;
c2 = *__SIMD32( cur )++;
d1 = *__SIMD32( lag )++;
d2 = *__SIMD32( lag )++;
// subract two samples at a time
out1 = __QSUB16( a1, b1 );
out2 = __QSUB16( a2, b2 );
out3 = __QSUB16( c1, d1 );
out4 = __QSUB16( c2, d2 );
// square the difference
sum = multiply_accumulate_16tx16t_add_16bx16b( sum, out1, out1 );
sum = multiply_accumulate_16tx16t_add_16bx16b( sum, out2, out2 );
sum = multiply_accumulate_16tx16t_add_16bx16b( sum, out3, out3 );
sum = multiply_accumulate_16tx16t_add_16bx16b( sum, out4, out4 );
} while( --blkCnt );
uint64_t rs = running_sum;
rs += sum;
yin_buffer[yin_idx] = sum*tau;
rs_buffer[yin_idx] = running_sum;
rs_buffer[yin_idx] = rs;
running_sum = rs;
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;
//digitalWriteFast(2, LOW);
//digitalWriteFast(0, LOW);
state = 0;
return;
}
} while ( --cycles );
} while ( --outer_cycles );
if ( tau >= HALF_BLOCKS ) {
process_buffer = false;
if ( tau >= inner_cycles ) {
process_buffer = true;
new_output = false;
yin_idx = 1;
running_sum = 0;
tau_global = 1;
//digitalWriteFast(0, LOW);
state = 0;
return;
}
tau_global = tau;
//digitalWriteFast(0, LOW);
}

/**
* 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
* @param head buffer index
* @param tau lag we are currently working on this gets incremented
* @param tau lag we are curly working on gets incremented
*
* @return 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 AudioTuner::estimate( uint64_t *yin, uint64_t *rs, uint16_t head, uint16_t tau ) {
const uint64_t *y = ( uint64_t * )yin;
const uint64_t *r = ( uint64_t * )rs;
uint16_t _tau, _head;
const float thresh = yin_threshold;
_tau = tau;
@@ -178,13 +166,14 @@ uint16_t AudioTuner::estimate( int64_t *yin, int64_t *rs, uint16_t head, uint16_
idx0 = _head;
idx1 = _head + 1;
idx1 = ( idx1 >= 5 ) ? 0 : idx1;
idx2 = head + 2;
idx2 = ( idx2 >= 5 ) ? 0 : idx2;
idx2 = _head + 2;
idx2 = ( idx2 >= 5 ) ? idx2 - 5 : idx2;
// maybe fixed point would be better here? But how?
float s0, s1, s2;
s0 = ( ( float )*( y+idx0 ) / *( r+idx0 ) );
s1 = ( ( float )*( y+idx1 ) / *( r+idx1 ) );
s2 = ( ( float )*( y+idx2 ) / *( r+idx2 ) );
s0 = ( ( float )*( y+idx0 ) / ( float )*( r+idx0 ) );
s1 = ( ( float )*( y+idx1 ) / ( float )*( r+idx1 ) );
s2 = ( ( float )*( y+idx2 ) / ( float )*( r+idx2 ) );
if ( s1 < thresh && s1 < s2 ) {
uint16_t period = _tau - 3;
@@ -200,21 +189,23 @@ uint16_t AudioTuner::estimate( int64_t *yin, int64_t *rs, uint16_t head, uint16_
* Initialise
*
* @param threshold Allowed uncertainty
* @param cpu_max How much cpu usage before throttling
*/
void AudioTuner::initialize( float threshold ) {
void AudioTuner::begin( float threshold, int16_t *coeff, uint8_t taps, uint8_t factor ) {
__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;
process_buffer = true;
yin_threshold = threshold;
periodicity = 0.0f;
running_sum = 0;
tau_global = 1;
yin_idx = 1;
enabled = true;
state = 0;
data = 0.0f;
decimation_factor = factor;
decimation_shift = log( factor ) / log( 2 );
coeff_size = taps;
coeff_p = coeff;
arm_fir_decimate_init_q15( &firDecimateInst, coeff_size, decimation_factor, coeff_p, &coeff_state[0], AUDIO_BLOCK_SAMPLES );
__enable_irq( );
}

@@ -240,11 +231,11 @@ float AudioTuner::read( void ) {
__disable_irq( );
float d = data;
__enable_irq( );
return AUDIO_SAMPLE_RATE_EXACT / d;
return ( AUDIO_SAMPLE_RATE_EXACT / decimation_factor ) / d;
}

/**
* Periodicity of the sampled signal from Yin algorithm from read function.
* Periodicity of the sampled signal.
*
* @return periodicity
*/
@@ -255,6 +246,17 @@ float AudioTuner::probability( void ) {
return p;
}

/**
* Initialise parameters.
*
* @param thresh Allowed uncertainty
*/
void AudioTuner::coeff( int16_t *p, int n ) {
//coeff_size = n;
//coeff_p = p;
//arm_fir_decimate_init_q15(&firDecimateInst, coeff_size, 4, coeff_p, coeff_state, 128);
}

/**
* Initialise parameters.
*
@@ -264,4 +266,4 @@ void AudioTuner::threshold( float p ) {
__disable_irq( );
yin_threshold = p;
__enable_irq( );
}
}

+ 52
- 34
AudioTuner.h ファイルの表示

@@ -1,4 +1,4 @@
/* Audio Library Guitar and Bass Tuner
/* Audio Library Note Frequency Detection & Guitar/Bass Tuner
* Copyright (c) 2015, Colin Duffy
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -24,20 +24,25 @@
#define AudioTuner_h_

#include "AudioStream.h"
/****************************************************************
* Safe to adjust these values below *
* *
* This parameter defines the size of the buffer. *
* *
* 1. AUDIO_BLOCKS - Buffer size is 128 * AUDIO_BLOCKS. *
* The more AUDIO_BLOCKS the lower the *
* frequency you can detect. The defualt *
* (24) is set to measure down to 29.14 *
* Hz or B(flat)0. *
* *
****************************************************************/
#define AUDIO_BLOCKS 24
/****************************************************************/
#include "arm_math.h"
/***********************************************************************
* Safe to adjust these values below *
* *
* This parameter defines the size of the buffer. *
* *
* 1. AUDIO_GUITARTUNER_BLOCKS - Buffer size is 128 * AUDIO_BLOCKS. *
* The more AUDIO_GUITARTUNER_BLOCKS the lower *
* the frequency you can detect. The default *
* (24) is set to measure down to 29.14 Hz *
* or B(flat)0. *
* *
* 2. MAX_COEFF - Maxium number of coefficeints for the FIR filter. *
* *
***********************************************************************/
#define AUDIO_GUITARTUNER_BLOCKS 24
#define MAX_COEFF 200
/***********************************************************************/

class AudioTuner : public AudioStream {
public:
/**
@@ -45,19 +50,26 @@ public:
*
* @return none
*/
AudioTuner( void ) : AudioStream( 1, inputQueueArray ), enabled( false ), new_output(false) {
AudioTuner( void ) : AudioStream( 1, inputQueueArray ),
data( 0.0 ),
coeff_p( NULL ),
enabled( false ),
new_output( false ),
coeff_size( 0 )
{
}
/**
* initialize variables and start conversion
* initialize variables and start
*
* @param threshold Allowed uncertainty
* @param cpu_max How much cpu usage before throttling
*
* @return none
* @param coeff coefficients for fir filter
* @param taps number of coefficients, even
* @param factor must be power of 2
*/
void initialize( float threshold );
void begin( float threshold, int16_t *coeff, uint8_t taps, uint8_t factor );
/**
* sets threshold value
@@ -87,13 +99,20 @@ public:
*/
float probability( void );
/**
* fir decimation coefficents
*
* @return none
*/
void coeff( int16_t *p, int n );
/**
* Audio Library calls this update function ~2.9ms
*
* @return none
*/
virtual void update( void );

private:
/**
* check the sampled data for fundamental frequency
@@ -105,28 +124,27 @@ private:
*
* @return tau
*/
uint16_t estimate( int64_t *yin, int64_t *rs, uint16_t head, uint16_t tau );
uint16_t estimate( uint64_t *yin, uint64_t *rs, uint16_t head, uint16_t tau );
/**
* process audio data
*
* @return none
*/
void process( void );
void process( int16_t *p );
/**
* Variables
*/
uint64_t running_sum;
float periodicity, yin_threshold, data;
uint64_t running_sum, yin_buffer[5], rs_buffer[5];
uint16_t tau_global;
int64_t rs_buffer[5], yin_buffer[5];
int16_t AudioBuffer[AUDIO_BLOCKS*128] __attribute__ ( ( aligned ( 4 ) ) );
uint8_t yin_idx, state;
float periodicity, yin_threshold, cpu_usage_max, data;
bool enabled, next_buffer, first_run;
volatile bool new_output, process_buffer;
audio_block_t *blocklist1[AUDIO_BLOCKS];
audio_block_t *blocklist2[AUDIO_BLOCKS];
int16_t AudioBuffer[AUDIO_GUITARTUNER_BLOCKS*AUDIO_BLOCK_SAMPLES] __attribute__ ( ( aligned ( 4 ) ) );
int16_t coeff_state[AUDIO_BLOCK_SAMPLES + MAX_COEFF];
int16_t *coeff_p;
uint8_t yin_idx, state, coeff_size, decimation_factor, decimation_shift;
volatile bool new_output, process_buffer, enabled;
audio_block_t *inputQueueArray[1];
arm_fir_decimate_instance_q15 firDecimateInst;
};
#endif
#endif

+ 19
- 16
README.md ファイルの表示

@@ -1,5 +1,5 @@
<p align="center">
<b>Guitar and Bass Tuner Library v2.3</b><br>
<b>Guitar and Bass Tuner Library v3.0</b><br>
<b>Teensy 3.1/2</b><br>
</p>

@@ -49,20 +49,23 @@
<h4>AudioTuner.h</h4>

```
/****************************************************************
* Safe to adjust these values below *
* *
* This parameter defines the size of the buffer. *
* *
* 1. AUDIO_BLOCKS - Buffer size is 128 * AUDIO_BLOCKS. *
* The more AUDIO_BLOCKS the lower the *
* frequency you can detect. The default *
* (24) is set to measure down to 29.14 *
* Hz or B(flat)0. *
* *
****************************************************************/
#define AUDIO_BLOCKS 24
/****************************************************************/
/***********************************************************************
* Safe to adjust these values below *
* *
* This parameter defines the size of the buffer. *
* *
* 1. AUDIO_GUITARTUNER_BLOCKS - Buffer size is 128 * AUDIO_BLOCKS. *
* The more AUDIO_GUITARTUNER_BLOCKS the lower *
* the frequency you can detect. The default *
* (24) is set to measure down to 29.14 Hz *
* or B(flat)0. *
* *
* 2. MAX_COEFF - Maxium number of coefficeints for the FIR filter. *
* *
***********************************************************************/
#define AUDIO_GUITARTUNER_BLOCKS 24
#define MAX_COEFF 200
/***********************************************************************/
```

<div>
@@ -77,4 +80,4 @@
</div>

[YIN]:http://recherche.ircam.fr/equipes/pcm/cheveign/pss/2002_JASA_YIN.pdf
[Teensy Audio Library]:http://www.pjrc.com/teensy/td_libs_Audio.html
[Teensy Audio Library]:http://www.pjrc.com/teensy/td_libs_Audio.html

+ 4
- 2
examples/Sample_Guitar_Tunning_Notes/Sample_Guitar_Tunning_Notes.ino ファイルの表示

@@ -25,6 +25,8 @@
#include <Wire.h>
#include <SPI.h>
#include <SD.h>

#include "coeff.h"
//---------------------------------------------------------------------------------------
#include "e2_note.h"
#include "a2_note.h"
@@ -62,7 +64,7 @@ void setup() {
* Initialize the yin algorithm's absolute
* threshold, this is good number.
*/
tuner.initialize(.15);
tuner.begin(.15, fir_22059_HZ_coefficients, sizeof(fir_22059_HZ_coefficients), 2);
pinMode(LED_BUILTIN, OUTPUT);
playNoteTimer.begin(playNote, 1000);
}
@@ -74,4 +76,4 @@ void loop() {
float prob = tuner.probability();
Serial.printf("Note: %3.2f | Probability: %.2f\n", note, prob);
}
}
}

+ 31
- 0
examples/Sample_Guitar_Tunning_Notes/coeff.h ファイルの表示

@@ -0,0 +1,31 @@
int16_t fir_44117_HZ_coefficients[22] =
{
0, 3, 6, -11, -71, 21,
352, -15, -1202, -6, 5011, 8209,
5011, -6, -1202, -15, 352, 21,
-71, -11, 6, 3
};

int16_t fir_22059_HZ_coefficients[20] =
{
0, 1, -6, -54, 18, 326,
-14, -1178, -6, 5001, 8209, 5001,
-6, -1178, -14, 326, 18, -54,
-6, 1
};

int16_t fir_11029_HZ_coefficients[22] =
{
0, 3, 6, -11, -71, 21,
352, -15, -1202, -6, 5011, 8209,
5011, -6, -1202, -15, 352, 21,
-71, -11, 6, 3
};

int16_t fir_5515_HZ_coefficients[20] =
{
0, 6, 4, -94, -2, 409,
-11, -1267, 23, 5040, 8164, 5040,
23, -1267, -11, 409, -2, -94,
4, 6
};

+ 3
- 1
examples/Simple_Sine/Simple_Sine.ino ファイルの表示

@@ -27,6 +27,8 @@
#include <SPI.h>
#include <SD.h>

#include "coeff.h"

AudioTuner tuner;
AudioSynthWaveformSine sine;
AudioOutputAnalog dac;
@@ -45,7 +47,7 @@ void setup() {
* Initialize the yin algorithm's absolute
* threshold, this is good number.
*/
tuner.initialize(.15);
tuner.begin(.15, fir_22059_HZ_coefficients, sizeof(fir_22059_HZ_coefficients), 2);
sine.frequency(30.87);
sine.amplitude(1);

+ 31
- 0
examples/Simple_Sine/coeff.h ファイルの表示

@@ -0,0 +1,31 @@
int16_t fir_44117_HZ_coefficients[22] =
{
0, 3, 6, -11, -71, 21,
352, -15, -1202, -6, 5011, 8209,
5011, -6, -1202, -15, 352, 21,
-71, -11, 6, 3
};

int16_t fir_22059_HZ_coefficients[20] =
{
0, 1, -6, -54, 18, 326,
-14, -1178, -6, 5001, 8209, 5001,
-6, -1178, -14, 326, 18, -54,
-6, 1
};

int16_t fir_11029_HZ_coefficients[22] =
{
0, 3, 6, -11, -71, 21,
352, -15, -1202, -6, 5011, 8209,
5011, -6, -1202, -15, 352, 21,
-71, -11, 6, 3
};

int16_t fir_5515_HZ_coefficients[20] =
{
0, 6, 4, -94, -2, 409,
-11, -1267, 23, 5040, 8164, 5040,
23, -1267, -11, 409, -2, -94,
4, 6
};

+ 1
- 2
library.properties ファイルの表示

@@ -1,5 +1,5 @@
name=AudioTuner
version=2.3
version=3.0
author=Colin Duffy
maintainer=Colin Duffy
sentence=Yin algorithm
@@ -7,4 +7,3 @@ paragraph=Yin algorithm to detect the fundmental frequency.
category=Signal Input/Output
url=http://github.com/duff2013/AudioTuner
architectures=*


+ 5
- 1
revision.md ファイルの表示

@@ -1,3 +1,7 @@
><b>Updated (02/17/17 v3.0)</b><br>
* Now we decimate the signal before analysis, significant speed up.<br>
* More robust algorithm to determine the fundamental frequency.<br>

><b>Updated (11/23/15 v2.3)</b><br>
* Totally new method to gather and process data, data is available after 24 Blocks of data have been collected (~69.6ms) for all frequencies.<br>
* Double buffer to collect Audio data, while one collects the other buffer is processed.<br>
@@ -12,4 +16,4 @@
* Improved user interface.<br>

><b>Updated (7/10/15 v2.0)</b><br>
* First commit
* First commit

読み込み中…
キャンセル
保存