diff --git a/src/Tuner.cpp b/src/Tuner.cpp index f4fe208..d043298 100644 --- a/src/Tuner.cpp +++ b/src/Tuner.cpp @@ -77,9 +77,13 @@ void Tuner::ComputeFrame(int16_t v) void Tuner::AudioCb(const QAudioBuffer &buffer) { const int16_t *ptr = buffer.constData(); - int nbFrame = buffer.sampleCount(); + const int nbFrame = buffer.sampleCount(); + AudioAnalyse(ptr, nbFrame); +} - while (nbFrame--) ComputeFrame(*ptr++); +void Tuner::AudioAnalyse(const int16_t *ptr, int nb_frame) +{ + while (nb_frame--) ComputeFrame(*ptr++); if (freq != cross->Freq()) { freq = cross->Freq(); @@ -129,7 +133,7 @@ int Tuner::GetOctave() return octave; } -QString Tuner::GetNoteName() +const char* Tuner::GetNoteName() { return scale->NoteName(note); } diff --git a/src/Tuner.hpp b/src/Tuner.hpp index e9196ba..a1548fc 100644 --- a/src/Tuner.hpp +++ b/src/Tuner.hpp @@ -44,13 +44,15 @@ class Tuner : public QObject { void Start(); void Stop(); + void AudioAnalyse(const int16_t *buffer, int size); + bool GetRunning(); void SetRunning(bool r); double GetFreq(); int GetNote(); int GetOctave(); double GetDeviation(); - QString GetNoteName(); + const char* GetNoteName(); signals: void runningChanged(); diff --git a/src/desktop.cpp b/src/desktop.cpp index df0a65e..c36a7fe 100644 --- a/src/desktop.cpp +++ b/src/desktop.cpp @@ -2,10 +2,40 @@ #include #include +#include +#include + #include "Tuner.hpp" +static void analyse_file(const char *filename) +{ + using namespace std; + cout << "analyse file " << filename << endl; + ifstream fin; + fin.open(filename); + + const int nb_frame = 1024; + Tuner *tuner = new Tuner(); + int16_t buffer[nb_frame]; + + while (1) { + fin.read((char*) buffer, sizeof(buffer)); + tuner->AudioAnalyse(buffer, sizeof(buffer) >> 1); + + cout << tuner->GetFreq() << " " << tuner->GetNoteName() << " " << tuner->GetOctave() << endl; + + if (fin.eof()) break; + } + delete tuner; +} + Q_DECL_EXPORT int main(int argc, char* argv[]) { + if (argc == 2) { + analyse_file(argv[1]); + return 0; + } + qmlRegisterType("LJTuner", 1, 0, "Tuner"); QGuiApplication app(argc, argv); diff --git a/src/scale/Scale.cpp b/src/scale/Scale.cpp index de3f597..aeaaa32 100644 --- a/src/scale/Scale.cpp +++ b/src/scale/Scale.cpp @@ -62,7 +62,7 @@ int Scale::FindNote(double freq, int &octave, double &deviation) int note = 0; octave = findOctave(freq); - std::cerr << octave << " " << freq << std::endl; + //std::cerr << octave << " " << freq << std::endl; assert(freq >= actualRange[0] && freq <= actualRange[1]); while (actualNoteFreq[note] < freq && note < nbNote - 1) note++;