Tuner: filter result with time

This commit is contained in:
Louis-Joseph Fournier 2016-01-02 10:19:37 +01:00
parent 2a19d30ab6
commit fa4735f34c
3 changed files with 67 additions and 8 deletions

View file

@ -25,7 +25,7 @@ Item {
width: Math.min(parent.width, parent.height * 1.5) width: Math.min(parent.width, parent.height * 1.5)
height: width / 2 height: width / 2
level: tuner.deviation * 100 level: tuner.found ? tuner.deviation * 100 : -50
} }
ScaleToise { ScaleToise {

View file

@ -34,6 +34,8 @@ Tuner::Tuner()
running = false; running = false;
freq = deviation = 0; freq = deviation = 0;
note = octave = 0; note = octave = 0;
found = false;
count_found = count_not_found = 0;
high_filter = new LinearFilter<int16_t>(3, a10, b10); high_filter = new LinearFilter<int16_t>(3, a10, b10);
@ -70,6 +72,7 @@ Tuner::~Tuner()
void Tuner::Start() void Tuner::Start()
{ {
cerr << __func__ << endl; cerr << __func__ << endl;
count_found = count_not_found = 0;
nb_sample_running = 0; nb_sample_running = 0;
blank_prevent(true); blank_prevent(true);
high_filter->Clear(); high_filter->Clear();
@ -101,6 +104,45 @@ void Tuner::AudioCb(const QAudioBuffer &buffer)
AudioAnalyse(ptr, nbFrame); AudioAnalyse(ptr, nbFrame);
} }
void Tuner::SetFound(int n, int o, double d)
{
if (n != note_found) {
note_found = n;
count_found = 0;
SetNotFound();
}
else if (count_found++ >= nbConfirm) {
count_not_found = 0;
if (!found) {
found = true;
foundChanged();
}
if (note != n) {
note = n;
noteChanged();
}
if (octave != o) {
octave = o;
octaveChanged();
}
if (deviation != d) {
deviation = d;
deviationChanged();
}
}
}
void Tuner::SetNotFound()
{
if (count_not_found++ >= nbDefect) {
count_found = 0;
if (found) {
found = false;
foundChanged();
}
}
}
void Tuner::AudioAnalyse(const int16_t *ptr, int nb_frame) void Tuner::AudioAnalyse(const int16_t *ptr, int nb_frame)
{ {
nb_sample_running += nb_frame; nb_sample_running += nb_frame;
@ -112,12 +154,13 @@ void Tuner::AudioAnalyse(const int16_t *ptr, int nb_frame)
freqChanged(); freqChanged();
if (freq) { if (freq) {
note = scale->FindNote(freq, octave, deviation); int n, o;
noteChanged(); double d;
noteNameChanged(); n = scale->FindNote(freq, o, d);
octaveChanged(); SetFound(n, o, d);
deviationChanged(); }
//std::cerr << note << " " << scale->NoteName(note) << std::endl; else { // no freq
SetNotFound();
} }
} }
@ -164,3 +207,8 @@ const char* Tuner::GetNoteName()
{ {
return scale->NoteName(note); return scale->NoteName(note);
} }
bool Tuner::GetFound()
{
return found;
}

View file

@ -12,6 +12,7 @@ class Tuner : public QObject {
Q_PROPERTY(double deviation READ GetDeviation NOTIFY deviationChanged) Q_PROPERTY(double deviation READ GetDeviation NOTIFY deviationChanged)
Q_PROPERTY(int note READ GetNote NOTIFY noteChanged) Q_PROPERTY(int note READ GetNote NOTIFY noteChanged)
Q_PROPERTY(int octave READ GetOctave NOTIFY octaveChanged) Q_PROPERTY(int octave READ GetOctave NOTIFY octaveChanged)
Q_PROPERTY(bool found READ GetFound NOTIFY foundChanged)
Q_PROPERTY(QString noteName READ GetNoteName NOTIFY noteNameChanged) Q_PROPERTY(QString noteName READ GetNoteName NOTIFY noteNameChanged)
private: private:
@ -23,17 +24,25 @@ class Tuner : public QObject {
ZeroCross<int16_t> *cross; ZeroCross<int16_t> *cross;
Scale *scale; Scale *scale;
bool running; bool running, found;
double freq, deviation; double freq, deviation;
int note, octave, nb_sample_running; int note, octave, nb_sample_running;
int note_found, count_found, count_not_found;
static const int rate = 16000; static const int rate = 16000;
static const int defaultNbFrame = 1024; static const int defaultNbFrame = 1024;
static const int defaultFreqMin = 50; static const int defaultFreqMin = 50;
static const int defaultFreqMax = 2000; static const int defaultFreqMax = 2000;
static const int nbSamplePreventRunning = rate * 40; // 40 seconds static const int nbSamplePreventRunning = rate * 40; // 40 seconds
/// number of analyses to confirm a note
static const int nbConfirm = 3;
/// number of analyses to drop a note
static const int nbDefect = 20;
inline void ComputeFrame(int16_t v); inline void ComputeFrame(int16_t v);
void SetFound(int note, int octave, double deviation);
void SetNotFound();
private slots: private slots:
void AudioCb(const QAudioBuffer &buffer); void AudioCb(const QAudioBuffer &buffer);
@ -53,6 +62,7 @@ class Tuner : public QObject {
int GetNote(); int GetNote();
int GetOctave(); int GetOctave();
double GetDeviation(); double GetDeviation();
bool GetFound();
const char* GetNoteName(); const char* GetNoteName();
signals: signals:
@ -62,4 +72,5 @@ class Tuner : public QObject {
void noteNameChanged(); void noteNameChanged();
void octaveChanged(); void octaveChanged();
void deviationChanged(); void deviationChanged();
void foundChanged();
}; };