# Different-Range FFT Algorithm

@bmillier, what was your easy way to disable the notefreq function when not needed? I'm doing the same thing, integrating the guitar tuner into a bigger project.
I have a newer version on GitHub that has disable function implemented and also it can decimate the incoming signal and low pass filter it (for aliasing) to help with processor usage. You can decimate 2, 4, 8 which corresponds to ~22050, ~11025 and ~5512.5Hz sample rates. You have to add in the coefficients for the filter though, the examples show this though they are probably not great filter coefficients but worked well for my tests.

Thank you both. The updated version seems to be much better at recognizing singing as well.

Everyone, thanks for this discussion! I followed a link from the example code to this forum thread. I had no idea there'd be such great sample code. I bought a Teensy 3.2 after seeing the Adafruit FFT: Fun with Fourier Transforms project.

My goal is to create a conference-badge style instrument tuner in time for Defcon 28 (2020). I've decided to keep it simple and detect just one tone, like Concert A4 (440 Hz). (hackaday page) So this code is right on point!

I'm definitely going to try to use it to tune a double bass, and so the conversation about the tuba was helpful. The fundamental frequency of its low E is also 41-ish Hz, but I can tune the instrument by using a tune on e.g. a harmonic played on the A string.

Oh, I forgot to mention: I'm also going to try an entirely different method: using AI / machine learning. Now that TensorFlow Light enables some TensorFlow models to run on Arduino-compatible chips, this opens the possibility of "training" a model which can recognize an A4 440Hz being played on various instruments.

I have a Teensy 4.0 and have built the circuit on the AudioTuner github page. I'm working out of the example Sample_Guitar_Tunning_Notes. But when I verify it, I get errors, see below.

I have used an Arduino code developed by Amanda Ghassaei and got that to work, but it's jumpy. I am hoping the Teensy will give me better results at developing a solid guitar/bass tuner.

Is the Sample_Guitar_Tunning_Notes example the best place to start? It has a lot of output simulations that I probably don't need... I only want to read an analog input.

Thanks!

***
In file included from C:\Users\rgw\AppData\Local\Temp\1\.arduinoIDE-unsaved2023911-12792-nd89vl.of4bg\Sample_Guitar_Tunning_Notes\Sample_Guitar_Tunning_Notes.ino:23:
c:\Users\rgw\Dropbox (Personal)\Ryan\Circuits\libraries\AudioTuner/AudioTuner.h: In constructor 'AudioTuner::AudioTuner()':
c:\Users\rgw\Dropbox (Personal)\Ryan\Circuits\libraries\AudioTuner/AudioTuner.h:153:47: warning: 'AudioTuner::enabled' will be initialized after [-Wreorder]
153 | volatile bool new_output, process_buffer, enabled;
| ^~~~~~~
c:\Users\rgw\Dropbox (Personal)\Ryan\Circuits\libraries\AudioTuner/AudioTuner.h:153:19: warning: 'volatile bool AudioTuner::new_output' [-Wreorder]
153 | volatile bool new_output, process_buffer, enabled;
| ^~~~~~~~~~
c:\Users\rgw\Dropbox (Personal)\Ryan\Circuits\libraries\AudioTuner/AudioTuner.h:53:5: warning: when initialized here [-Wreorder]
53 | AudioTuner( void ) : AudioStream( 1, inputQueueArray ),
| ^~~~~~~~~~
c:\Users\rgw\Dropbox (Personal)\Ryan\Circuits\libraries\AudioTuner/AudioTuner.h:153:19: warning: 'AudioTuner::new_output' will be initialized after [-Wreorder]
153 | volatile bool new_output, process_buffer, enabled;
| ^~~~~~~~~~
c:\Users\rgw\Dropbox (Personal)\Ryan\Circuits\libraries\AudioTuner/AudioTuner.h:152:30: warning: 'uint8_t AudioTuner::coeff_size' [-Wreorder]
152 | uint8_t yin_idx, state, coeff_size, decimation_factor, decimation_shift;
| ^~~~~~~~~~
c:\Users\rgw\Dropbox (Personal)\Ryan\Circuits\libraries\AudioTuner/AudioTuner.h:53:5: warning: when initialized here [-Wreorder]
53 | AudioTuner( void ) : AudioStream( 1, inputQueueArray ),
| ^~~~~~~~~~
Memory Usage on Teensy 4.0:
FLASH: code:77700, data:226492, headers:9148 free for files:1718276
RAM1: variables:237536, code:75640, padding:22664 free for local variables:188448
RAM2: variables:20224 free for malloc/new:504064
***

None of those are errors, just warnings. The build completed and you should be able to flash the teensy and test

OK, it flashed ok. But I think I need a little help understanding what's going on here:

I think the RED section are just aliases, right?
What does the BLUE section do?
It looks like the GREEN section is trying to play the E2 note, others are commented out, and then light up the onboard LED?
The GOLD section seems to be setting up memory for the final section.
Then the final section should read what's being played in the GREEN section and print it out, but nothing happens so far. The onboard LED blinks every ~8 seconds.
I don't have a guitar plugged in at this point, although I can. I need to dig into those included libraries and figure out which one is looking at the analog input from Teensy A2/16.

Thanks!

*****************
#include <SerialFlash.h>
#include <AudioTuner.h>
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>

#include "coeff.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);
}
}

//---------------------------------------------------------------------------------------
void setup() {
AudioMemory(30);
/*
* Initialize the yin algorithm's absolute
* threshold, this is good number.
*/
tuner.begin(.15, fir_22059_HZ_coefficients, sizeof(fir_22059_HZ_coefficients), 2);
pinMode(LED_BUILTIN, OUTPUT);
playNoteTimer.begin(playNote, 1000);
}

void loop() {
if (tuner.available()) {
float prob = tuner.probability();
Serial.printf("Note: %3.2f | Probability: %.2f\n", note, prob);
}
}
********************

Libraries:

#include <SerialFlash.h> This is a Teensy library for SPI flash memory.
#include <AudioTuner.h> Can't find this library. It might be mis-named. This seems to be where the YIN stuff is, but I'm not sure if it's loaded in or not. How do I check?
#include <Audio.h> Arduino PLAY library, not sure if it's used here or not.
#include <Wire.h> No idea what this does.
#include <SPI.h> No idea what this does.
#include <SD.h> Probably related to an SD card?

#include "coeff.h" All these seem to be included in the original .imo.
#include "e2_note.h"
#include "a2_note.h"
#include "d3_note.h"
#include "g3_note.h"
#include "b3_note.h"
#include "e4_note.h"

Red is not "aliases", it declares some objects.
Blue declares some connection objects to tie the audio objects together.
Gold is the setup function, which gets called at startup for you, so is used to finish initializing stuff.
Green is a declaration of a timer object, and a function. setup() gives the timer object this function as its action.

OK. I just can't get these examples from AudioTuner to work. The 2 examples are:
Sample_Guitar_Tunning_Notes
Simple_Sine

Neither works. I built the circuit as described on the Github readme.