ソースを参照

First commit

main
duff2013 9年前
コミット
d4bab6874a
19個のファイルの変更10344行の追加0行の削除
  1. +204
    -0
      AudioTuner.cpp
  2. +117
    -0
      AudioTuner.h
  3. +3
    -0
      README.md
  4. +73
    -0
      examples/Sample_Guitar_Tunning_Notes/Sample_Guitar_Tunning_Notes.ino
  5. +4910
    -0
      examples/Sample_Guitar_Tunning_Notes/a2_note.cpp
  6. +2
    -0
      examples/Sample_Guitar_Tunning_Notes/a2_note.h
  7. +1
    -0
      examples/Sample_Guitar_Tunning_Notes/b3_note.cpp
  8. +2
    -0
      examples/Sample_Guitar_Tunning_Notes/b3_note.h
  9. +1
    -0
      examples/Sample_Guitar_Tunning_Notes/d3_note.cpp
  10. +2
    -0
      examples/Sample_Guitar_Tunning_Notes/d3_note.h
  11. +4912
    -0
      examples/Sample_Guitar_Tunning_Notes/e2_note.cpp
  12. +2
    -0
      examples/Sample_Guitar_Tunning_Notes/e2_note.h
  13. +1
    -0
      examples/Sample_Guitar_Tunning_Notes/e4_note.cpp
  14. +2
    -0
      examples/Sample_Guitar_Tunning_Notes/e4_note.h
  15. +1
    -0
      examples/Sample_Guitar_Tunning_Notes/g3_note.cpp
  16. +2
    -0
      examples/Sample_Guitar_Tunning_Notes/g3_note.h
  17. +24
    -0
      examples/Simple_Sine/Handle_Commands.ino
  18. +60
    -0
      examples/Simple_Sine/Simple_Sine.ino
  19. +25
    -0
      keywords.txt

+ 204
- 0
AudioTuner.cpp ファイルの表示

@@ -0,0 +1,204 @@
/* Audio Library Guitar Tuner
* Copyright (c) 2015, Colin Duffy
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice, development funding notice, and this permission
* notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#include "AudioTuner.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

#define LOOP1(a) a
#define LOOP2(a) a LOOP1(a)
#define LOOP3(a) a LOOP2(a)
#define LOOP8(a) a LOOP3(a) a LOOP3(a)
#define UNROLL(n,a) LOOP##n(a)

#define WINDOW SAMPLE_SKIP - 1

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

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;
}
block_count = count;
if ( process_buffer ) {
digitalWriteFast(0, HIGH);
uint16_t tau;
uint16_t next;
next = next_buffer;
tau = tau_global;
do {
int64_t sum = 0;
const int16_t *end, *buf;
if ( next ) buf = buffer + NUM_SAMPLES;
else buf = buffer;
end = buf + HALF_BUFFER;
do {
int16_t current, lag, delta;
UNROLL( 8,
lag = *( buf + tau );
current = *buf++;
delta = current - lag;
sum += delta*delta;
);
} while ( buf < end );
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;
digitalWriteFast(0, LOW);
return;
}
else if ( tau >= HALF_BUFFER ) {
process_buffer = false;
new_output = false;
digitalWriteFast(0, LOW);
return;
}
} while ( tau <= ( tau_global + 31 ) );
tau_global = tau;
digitalWriteFast(0, LOW);
}
}

/**
* process data in from Audio Library interrupt
*/
uint16_t AudioTuner::estimate( int64_t *yin, int64_t *rs, uint16_t head, uint16_t tau ) {
const int64_t *p = ( int64_t * )yin;
const int64_t *r = ( int64_t * )rs;
uint16_t period = 0, _tau, _head;
_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 )*( p+idx0 ) / r[idx0] );
s1 = ( ( float )*( p+idx1 ) / r[idx1] );
s2 = ( ( float )*( p+idx2 ) / r[idx2] );
if ( s1 < threshold && s1 < s2 ) {
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;
else return _tau + 1;
}
return _tau + 1;
}

/**
* available
*
* @return true if data is ready else false
*/
bool AudioTuner::available( void ) {
__disable_irq();
bool flag = new_output;
if (flag) new_output = false;
__enable_irq();
return flag;
}

/**
* read processes the data samples for the Yin algorithm.
*
* @return frequency in hertz
*/
float AudioTuner::read( void ) {
return SAMPLE_RATE / data;
}
/**
* Periodicity of the sampled signal from Yin algorithm from read function.
*
* @return periodicity
*/
float AudioTuner::probability( void ) {
return periodicity;
}
/**
* Initialise parameters.
*
* @param thresh Allowed uncertainty
*/
void AudioTuner::set_params( float thresh ) {
__disable_irq( );
//arm_cfft_radix4_init_q15(&fft_inst, 1024, 0, 1);
//arm_cfft_radix4_init_q15(&ifft_inst, 1024, 1, 1);
threshold = thresh;
periodicity = 0.0f;
block_count = 0;
enabled = true;
__enable_irq( );
}

+ 117
- 0
AudioTuner.h ファイルの表示

@@ -0,0 +1,117 @@
/* Audio Library for Teensy 3.X
* Copyright (c) 2015, Colin Duffy
*
* Development of this audio library was funded by PJRC.COM, LLC by sales of
* Teensy and Audio Adaptor boards. Please support PJRC's efforts to develop
* open source software by purchasing Teensy or other PJRC products.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice, development funding notice, and this permission
* notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#ifndef AudioTuner_h_
#define AudioTuner_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
/****************************************************************
* Safe to adjust these values below *
****************************************************************/
// Adjust number of samples to collect in buffer here, also effects
// convergence speed and resolution.
#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
/****************************************************************/

class AudioTuner : public AudioStream
{
public:
AudioTuner( void ) : AudioStream( 1, inputQueueArray ),
enabled( false ), new_output(false),
next_buffer(1), process_buffer(false),
running_sum(0), block_count(0),
yin_idx(1)
{
set_params( 0.05f );// threshold set 15ms
}
/**
* sets threshold value and window length
*
* @param thresh
*/
void set_params( float thresh );
/**
* triggers true when valid frequency is found
*
* @return flag to indicate valid frequency is found
*/
bool available( void );
/**
* get frequency
*
* @return frequency in hertz
*/
float read( void );
/**
* get predicitity
*
* @return probability of correct freq found
*/
float probability( void );
/**
* Audio Library calls this update function ~2.9ms
*/
virtual void update( void );
private:
/**
* check the sampled data for fundmental 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
*
* @return 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 ) ) );
float periodicity, threshold, data;
int64_t rs_buffer[5], yin_buffer[5];
uint64_t running_sum;
uint16_t block_count, tau_global;
uint8_t next_buffer, yin_idx;
bool enabled, process_buffer;
volatile bool new_output;
audio_block_t *inputQueueArray[1];
};
#endif

+ 3
- 0
README.md ファイルの表示

@@ -0,0 +1,3 @@
#Guitar Tuner for Teensy 3.1

Software algorithm (YIN) for guitar and bass tuning using a Teensy Audio Library. This audio object's algorithm is somewhat memory and processor hungry but will allow you to detect lower frequencies than the Audio library's Goertzel algorithm will.

+ 73
- 0
examples/Sample_Guitar_Tunning_Notes/Sample_Guitar_Tunning_Notes.ino ファイルの表示

@@ -0,0 +1,73 @@
/*
C C# D Eb E F F# G G# A Bb B
0 16.35 17.32 18.35 19.45 20.60 21.83 23.12 24.50 25.96 27.50 29.14 30.87
1 32.70 34.65 36.71 38.89 41.20 43.65 46.25 49.00 51.91 55.00 58.27 61.74
2 65.41 69.30 73.42 77.78 82.41 87.31 92.50 98.00 103.8 110.0 116.5 123.5
3 130.8 138.6 146.8 155.6 164.8 174.6 185.0 196.0 207.7 220.0 233.1 246.9
4 261.6 277.2 293.7 311.1 329.6 349.2 370.0 392.0 415.3 440.0 466.2 493.9
5 523.3 554.4 587.3 622.3 659.3 698.5 740.0 784.0 830.6 880.0 932.3 987.8
6 1047 1109 1175 1245 1319 1397 1480 1568 1661 1760 1865 1976
7 2093 2217 2349 2489 2637 2794 2960 3136 3322 3520 3729 3951
8 4186 4435 4699 4978 5274 5588 5920 6272 6645 7040 7459 7902
Guitar strings are E2=82.41Hz, A2=110Hz, D3=146.8Hz, G3=196Hz, B3=246.9Hz, E4=329.6Hz
Bass strings are (5th string) B0=30.87Hz, (4th string) E1=41.20Hz, A1=55Hz, D2=73.42Hz, G2=98Hz
This example tests the yin algorithm with actual notes from nylon string guitar recorded
as wav format at 16B @ 44100smpls/sec. Since the decay of the notes will be longer than what
the teensy can store in flash these notes are truncated to ~120,000B or about 1/2 of the whole
signal.
*/
#include <AudioTuner.h>
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
//---------------------------------------------------------------------------------------
#include "e2_note.h"
#include "a2_note.h"
#include "d3_note.h"
#include "g3_note.h"
#include "b3_note.h"
#include "e4_note.h"
//---------------------------------------------------------------------------------------
AudioTuner tuner;
AudioOutputAnalog dac;
AudioPlayMemory wav_note;
AudioMixer4 mixer;
//---------------------------------------------------------------------------------------
AudioConnection patchCord0(wav_note, 0, mixer, 0);
AudioConnection patchCord1(mixer, 0, tuner, 0);
AudioConnection patchCord2(mixer, 0, dac, 0);
//---------------------------------------------------------------------------------------
IntervalTimer playNoteTimer;

void playNote(void) {
if (!wav_note.isPlaying()) {
wav_note.play(e2_note);
//wav_note.play(a2_note);
//wav_note.play(d3_note);
//wav_note.play(g3_note);
//wav_note.play(b3_note);
//wav_note.play(e4_note);
digitalWriteFast(LED_BUILTIN, !digitalReadFast(LED_BUILTIN));
}
}
//---------------------------------------------------------------------------------------
void setup() {
// put your setup code here, to run once:
AudioMemory(4);
pinMode(LED_BUILTIN, OUTPUT);
playNoteTimer.begin(playNote, 1000);
}

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

}

+ 4910
- 0
examples/Sample_Guitar_Tunning_Notes/a2_note.cpp
ファイル差分が大きすぎるため省略します
ファイルの表示


+ 2
- 0
examples/Sample_Guitar_Tunning_Notes/a2_note.h ファイルの表示

@@ -0,0 +1,2 @@
#include "Arduino.h"
extern const unsigned int a2_note[53971];

+ 1
- 0
examples/Sample_Guitar_Tunning_Notes/b3_note.cpp
ファイル差分が大きすぎるため省略します
ファイルの表示


+ 2
- 0
examples/Sample_Guitar_Tunning_Notes/b3_note.h ファイルの表示

@@ -0,0 +1,2 @@
#include "Arduino.h"
extern const unsigned int b3_note[53990];

+ 1
- 0
examples/Sample_Guitar_Tunning_Notes/d3_note.cpp
ファイル差分が大きすぎるため省略します
ファイルの表示


+ 2
- 0
examples/Sample_Guitar_Tunning_Notes/d3_note.h ファイルの表示

@@ -0,0 +1,2 @@
#include "Arduino.h"
extern const unsigned int d3_note[53974];

+ 4912
- 0
examples/Sample_Guitar_Tunning_Notes/e2_note.cpp
ファイル差分が大きすぎるため省略します
ファイルの表示


+ 2
- 0
examples/Sample_Guitar_Tunning_Notes/e2_note.h ファイルの表示

@@ -0,0 +1,2 @@
#include "Arduino.h"
extern const unsigned int e2_note[53990];

+ 1
- 0
examples/Sample_Guitar_Tunning_Notes/e4_note.cpp
ファイル差分が大きすぎるため省略します
ファイルの表示


+ 2
- 0
examples/Sample_Guitar_Tunning_Notes/e4_note.h ファイルの表示

@@ -0,0 +1,2 @@
#include "Arduino.h"
extern const unsigned int e4_note[53990];

+ 1
- 0
examples/Sample_Guitar_Tunning_Notes/g3_note.cpp
ファイル差分が大きすぎるため省略します
ファイルの表示


+ 2
- 0
examples/Sample_Guitar_Tunning_Notes/g3_note.h ファイルの表示

@@ -0,0 +1,2 @@
#include "Arduino.h"
extern const unsigned int g3_note[53965];

+ 24
- 0
examples/Simple_Sine/Handle_Commands.ino ファイルの表示

@@ -0,0 +1,24 @@
void handleCmds( String cmd ) {
String p = cmd;

if (p.startsWith("f ")) {
p.trim();
p = p.substring(2);
float t = p.toFloat();
Serial.print("new frequency: ");
Serial.println(t);
//AudioNoInterrupts(); // disable audio library momentarily
sine.frequency(p.toFloat());
//AudioInterrupts(); // enable, both tones will start together
}
else if (p.startsWith("a ")) {
p.trim();
p = p.substring(2);
float t = p.toFloat();
Serial.print("new amplitude: ");
Serial.println(t);
AudioNoInterrupts(); // disable audio library momentarily
sine.amplitude(p.toFloat());
AudioInterrupts(); // enable, both tones will start together
}
}

+ 60
- 0
examples/Simple_Sine/Simple_Sine.ino ファイルの表示

@@ -0,0 +1,60 @@
/*
C C# D Eb E F F# G G# A Bb B
0 16.35 17.32 18.35 19.45 20.60 21.83 23.12 24.50 25.96 27.50 29.14 30.87
1 32.70 34.65 36.71 38.89 41.20 43.65 46.25 49.00 51.91 55.00 58.27 61.74
2 65.41 69.30 73.42 77.78 82.41 87.31 92.50 98.00 103.8 110.0 116.5 123.5
3 130.8 138.6 146.8 155.6 164.8 174.6 185.0 196.0 207.7 220.0 233.1 246.9
4 261.6 277.2 293.7 311.1 329.6 349.2 370.0 392.0 415.3 440.0 466.2 493.9
5 523.3 554.4 587.3 622.3 659.3 698.5 740.0 784.0 830.6 880.0 932.3 987.8
6 1047 1109 1175 1245 1319 1397 1480 1568 1661 1760 1865 1976
7 2093 2217 2349 2489 2637 2794 2960 3136 3322 3520 3729 3951
8 4186 4435 4699 4978 5274 5588 5920 6272 6645 7040 7459 7902
Guitar strings are E2=82.41Hz, A2=110Hz, D3=146.8Hz, G3=196Hz, B3=246.9Hz, E4=329.6Hz
Bass strings are (5th string) B0=30.87Hz, (4th string) E1=41.20Hz, A1=55Hz, D2=73.42Hz, G2=98Hz
You can change the frequency by typing "f " + frequency in the serial monitor.
EX. "f 30.87"
You can change the amplitude by typing "a " + amplitude in the serial monitor. (0,1)
EX. "a .5"
*/
#include <AudioTuner.h>
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>

AudioTuner tuner;
AudioSynthWaveformSine sine;
AudioOutputAnalog dac;
AudioMixer4 mixer;

AudioConnection patchCord1(sine, 0, mixer, 0);
AudioConnection patchCord2(mixer, 0, tuner, 0);
AudioConnection patchCord3(mixer, 0, dac, 0);

// hold serial commands
char buffer[10];

void setup() {
// put your setup code here, to run once:
AudioMemory(4);
sine.frequency(30.87);
sine.amplitude(1);
}

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

+ 25
- 0
keywords.txt ファイルの表示

@@ -0,0 +1,25 @@
#######################################
# Syntax Coloring Map AudioTuner.h
#######################################

#######################################
# Datatypes (KEYWORD1)
#######################################
AudioTuner KEYWORD1
#######################################
# Methods and Functions (KEYWORD2)
#######################################
AudioTuner KEYWORD2
available KEYWORD2
probability KEYWORD2
read KEYWORD2
#######################################
# Instances (KEYWORD2)
#######################################

#######################################
# Constants (LITERAL1)
#######################################
threshold LITERAL1
periodicity LITERAL1
tuner LITERAL1

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