From f961e01452bf1e1465ed804ec0e66a918d14c87f Mon Sep 17 00:00:00 2001 From: Louis-Joseph Fournier Date: Sat, 2 Jan 2016 17:49:35 +0100 Subject: [PATCH] Tuner: make average for deviation result --- src/Tuner.cpp | 35 +++++++++++++++++++++++++++++++---- src/Tuner.hpp | 7 +++++++ 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/src/Tuner.cpp b/src/Tuner.cpp index aee436d..951f243 100644 --- a/src/Tuner.cpp +++ b/src/Tuner.cpp @@ -74,6 +74,7 @@ void Tuner::Start() cerr << __func__ << endl; count_found = count_not_found = 0; nb_sample_running = 0; + ResetDeviation(); blank_prevent(true); high_filter->Clear(); cross->Clear(); @@ -104,12 +105,36 @@ void Tuner::AudioCb(const QAudioBuffer &buffer) AudioAnalyse(ptr, nbFrame); } +void Tuner::ResetDeviation() +{ + // reset deviation values + nb_deviation = 0; + deviation_start = 0; + deviation_sum = 0; +} + +void Tuner::UpdateDeviation(double d) +{ + if (nb_deviation == nbDeviationValues) { + deviation_sum -= deviation_values[deviation_start]; + deviation_start = (deviation_start + 1) % nbDeviationValues; + nb_deviation--; + } + deviation_values[(deviation_start + nb_deviation) % nbDeviationValues] = d; + nb_deviation++; + deviation_sum += d; + deviation = deviation_sum / nb_deviation; +} + void Tuner::SetFound(int n, int o, double d) { if (n != note_found) { note_found = n; count_found = 0; SetNotFound(); + + ResetDeviation(); + UpdateDeviation(d); } else if (count_found++ >= nbConfirm) { count_not_found = 0; @@ -125,10 +150,11 @@ void Tuner::SetFound(int n, int o, double d) octave = o; octaveChanged(); } - if (deviation != d) { - deviation = d; - deviationChanged(); - } + UpdateDeviation(d); + deviationChanged(); + } + else { + UpdateDeviation(d); } } @@ -138,6 +164,7 @@ void Tuner::SetNotFound() count_found = 0; if (found) { found = false; + ResetDeviation(); foundChanged(); } } diff --git a/src/Tuner.hpp b/src/Tuner.hpp index e90333e..fc77cb1 100644 --- a/src/Tuner.hpp +++ b/src/Tuner.hpp @@ -28,6 +28,8 @@ class Tuner : public QObject { double freq, deviation; int note, octave, nb_sample_running; int note_found, count_found, count_not_found; + int nb_deviation, deviation_start; + double deviation_sum; static const int rate = 16000; static const int defaultNbFrame = 1024; @@ -38,11 +40,16 @@ class Tuner : public QObject { static const int nbConfirm = 3; /// number of analyses to drop a note static const int nbDefect = 20; + /// number of deviation values for average + static const int nbDeviationValues = 8; + double deviation_values[nbDeviationValues]; inline void ComputeFrame(int16_t v); void SetFound(int note, int octave, double deviation); void SetNotFound(); + void ResetDeviation(); + void UpdateDeviation(double d); private slots: void AudioCb(const QAudioBuffer &buffer);