2016-01-03 19:32:36 +03:00
|
|
|
/* Copyright 2016 (C) Louis-Joseph Fournier
|
|
|
|
* louisjoseph.fournier@gmail.com
|
|
|
|
*
|
|
|
|
* This file is part of SailTuner.
|
|
|
|
*
|
|
|
|
* SailTuner is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* SailTuner is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2015-12-26 22:46:02 +03:00
|
|
|
#include <QAudioRecorder>
|
|
|
|
#include <QAudioProbe>
|
|
|
|
|
2016-01-03 17:13:13 +03:00
|
|
|
#include <fstream>
|
|
|
|
|
2015-12-26 22:46:02 +03:00
|
|
|
#include "audio/LinearFilter.hpp"
|
|
|
|
#include "audio/ZeroCross.hpp"
|
2015-12-27 20:31:26 +03:00
|
|
|
#include "scale/Scale.hpp"
|
2016-01-04 00:35:08 +03:00
|
|
|
#include "scale/Temperaments.hpp"
|
2015-12-26 22:46:02 +03:00
|
|
|
|
|
|
|
class Tuner : public QObject {
|
|
|
|
Q_OBJECT
|
|
|
|
Q_PROPERTY(bool running READ GetRunning WRITE SetRunning NOTIFY runningChanged)
|
|
|
|
Q_PROPERTY(double freq READ GetFreq NOTIFY freqChanged)
|
2015-12-27 20:31:26 +03:00
|
|
|
Q_PROPERTY(double deviation READ GetDeviation NOTIFY deviationChanged)
|
|
|
|
Q_PROPERTY(int note READ GetNote NOTIFY noteChanged)
|
|
|
|
Q_PROPERTY(int octave READ GetOctave NOTIFY octaveChanged)
|
2016-01-02 12:19:37 +03:00
|
|
|
Q_PROPERTY(bool found READ GetFound NOTIFY foundChanged)
|
2015-12-27 20:31:26 +03:00
|
|
|
Q_PROPERTY(QString noteName READ GetNoteName NOTIFY noteNameChanged)
|
2016-01-04 00:35:08 +03:00
|
|
|
Q_PROPERTY(int temperament_idx READ GetTemperamentIndex WRITE SetTemperamentIndex NOTIFY temperamentChanged)
|
|
|
|
Q_PROPERTY(QStringList temperament_list READ GetTemperamentList)
|
2015-12-26 22:46:02 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
QAudioRecorder *recorder;
|
|
|
|
QAudioEncoderSettings settings;
|
|
|
|
QAudioProbe *probe;
|
|
|
|
|
|
|
|
LinearFilter<int16_t> *high_filter;
|
|
|
|
ZeroCross<int16_t> *cross;
|
2015-12-27 20:31:26 +03:00
|
|
|
Scale *scale;
|
2016-01-04 00:35:08 +03:00
|
|
|
Temperaments *temperaments;
|
2016-01-03 17:13:13 +03:00
|
|
|
static const char *filename_record;
|
|
|
|
std::ofstream file_record;
|
2015-12-26 22:46:02 +03:00
|
|
|
|
2016-01-02 12:19:37 +03:00
|
|
|
bool running, found;
|
2015-12-27 20:31:26 +03:00
|
|
|
double freq, deviation;
|
2015-12-29 23:32:57 +03:00
|
|
|
int note, octave, nb_sample_running;
|
2016-01-03 15:18:42 +03:00
|
|
|
int note_found, octave_found, count_found, count_not_found;
|
2016-01-02 19:49:35 +03:00
|
|
|
int nb_deviation, deviation_start;
|
|
|
|
double deviation_sum;
|
2015-12-26 22:46:02 +03:00
|
|
|
|
|
|
|
static const int rate = 16000;
|
|
|
|
static const int defaultNbFrame = 1024;
|
|
|
|
static const int defaultFreqMin = 50;
|
|
|
|
static const int defaultFreqMax = 2000;
|
2015-12-29 23:32:57 +03:00
|
|
|
static const int nbSamplePreventRunning = rate * 40; // 40 seconds
|
2016-01-02 12:19:37 +03:00
|
|
|
/// number of analyses to confirm a note
|
|
|
|
static const int nbConfirm = 3;
|
|
|
|
/// number of analyses to drop a note
|
|
|
|
static const int nbDefect = 20;
|
2016-01-02 19:49:35 +03:00
|
|
|
/// number of deviation values for average
|
|
|
|
static const int nbDeviationValues = 8;
|
2016-01-02 12:19:37 +03:00
|
|
|
|
2016-01-02 19:49:35 +03:00
|
|
|
double deviation_values[nbDeviationValues];
|
2015-12-26 22:46:02 +03:00
|
|
|
|
|
|
|
inline void ComputeFrame(int16_t v);
|
2016-01-02 12:19:37 +03:00
|
|
|
void SetFound(int note, int octave, double deviation);
|
|
|
|
void SetNotFound();
|
2016-01-02 19:49:35 +03:00
|
|
|
void ResetDeviation();
|
|
|
|
void UpdateDeviation(double d);
|
2015-12-26 22:46:02 +03:00
|
|
|
|
|
|
|
private slots:
|
|
|
|
void AudioCb(const QAudioBuffer &buffer);
|
|
|
|
|
|
|
|
public:
|
|
|
|
Tuner();
|
|
|
|
~Tuner();
|
|
|
|
|
|
|
|
void Start();
|
|
|
|
void Stop();
|
|
|
|
|
2015-12-28 16:16:08 +03:00
|
|
|
void AudioAnalyse(const int16_t *buffer, int size);
|
|
|
|
|
2015-12-26 22:46:02 +03:00
|
|
|
bool GetRunning();
|
|
|
|
void SetRunning(bool r);
|
|
|
|
double GetFreq();
|
2015-12-27 20:31:26 +03:00
|
|
|
int GetNote();
|
|
|
|
int GetOctave();
|
|
|
|
double GetDeviation();
|
2016-01-02 12:19:37 +03:00
|
|
|
bool GetFound();
|
2015-12-28 16:16:08 +03:00
|
|
|
const char* GetNoteName();
|
2016-01-04 00:35:08 +03:00
|
|
|
unsigned int GetTemperamentIndex();
|
|
|
|
void SetTemperamentIndex(unsigned int idx);
|
|
|
|
QStringList GetTemperamentList() const;
|
2015-12-26 22:46:02 +03:00
|
|
|
|
2016-01-03 17:13:13 +03:00
|
|
|
/// analyse a file for debug
|
|
|
|
static void analyse_file(const char *filename);
|
|
|
|
/// write a file with raw audio
|
|
|
|
static void set_record(const char *filename_record);
|
|
|
|
|
2015-12-26 22:46:02 +03:00
|
|
|
signals:
|
|
|
|
void runningChanged();
|
|
|
|
void freqChanged();
|
2015-12-27 20:31:26 +03:00
|
|
|
void noteChanged();
|
|
|
|
void noteNameChanged();
|
|
|
|
void octaveChanged();
|
|
|
|
void deviationChanged();
|
2016-01-02 12:19:37 +03:00
|
|
|
void foundChanged();
|
2016-01-04 00:35:08 +03:00
|
|
|
void temperamentChanged();
|
2015-12-26 22:46:02 +03:00
|
|
|
};
|