diff --git a/src/Tuner.cpp b/src/Tuner.cpp index 1e43826..f1d19eb 100644 --- a/src/Tuner.cpp +++ b/src/Tuner.cpp @@ -6,6 +6,9 @@ #include #include +#include +#include + #include "Tuner.hpp" using namespace std; @@ -14,6 +17,8 @@ using namespace std; static double a10[] = { 1 , -2.99214602, 2.98432286, -0.99217678 }; static double b10[] = { 0.99608071, -2.98824212, 2.98824212, -0.99608071 }; +const char * Tuner::filename_record = NULL; + // function to prevent screen blank static void blank_prevent(bool prevent) @@ -37,6 +42,8 @@ Tuner::Tuner() found = false; count_found = count_not_found = 0; + if (filename_record) file_record.open(filename_record); + high_filter = new LinearFilter(3, a10, b10); ZeroCross::Config cross_config({rate, defaultNbFrame, defaultFreqMin, defaultFreqMax}); @@ -63,6 +70,7 @@ Tuner::Tuner() Tuner::~Tuner() { + if (filename_record && file_record.is_open()) file_record.close(); delete high_filter; delete cross; delete recorder; @@ -176,6 +184,8 @@ void Tuner::AudioAnalyse(const int16_t *ptr, int nb_frame) { nb_sample_running += nb_frame; + if (filename_record && file_record.is_open()) file_record.write((char*) ptr, nb_frame * sizeof(int16_t)); + while (nb_frame--) ComputeFrame(*ptr++); if (freq != cross->Freq()) { @@ -241,3 +251,36 @@ bool Tuner::GetFound() { return found; } + +/// Set a filename to record raw audio stream + +void Tuner::set_record(const char *f) +{ + filename_record = f; +} + +/// analyse a file (static function) +void Tuner::analyse_file(const char *filename) +{ + 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() << " "; + if (tuner->GetFound()) + cout << tuner->GetNoteName() << " " << tuner->GetOctave() << " " << tuner->GetDeviation(); + cout << endl; + + if (fin.eof()) break; + } + fin.close(); + delete tuner; +} diff --git a/src/Tuner.hpp b/src/Tuner.hpp index 74d6ed7..aebef0c 100644 --- a/src/Tuner.hpp +++ b/src/Tuner.hpp @@ -1,6 +1,8 @@ #include #include +#include + #include "audio/LinearFilter.hpp" #include "audio/ZeroCross.hpp" #include "scale/Scale.hpp" @@ -23,6 +25,8 @@ class Tuner : public QObject { LinearFilter *high_filter; ZeroCross *cross; Scale *scale; + static const char *filename_record; + std::ofstream file_record; bool running, found; double freq, deviation; @@ -72,6 +76,11 @@ class Tuner : public QObject { bool GetFound(); const char* GetNoteName(); + /// 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); + signals: void runningChanged(); void freqChanged(); diff --git a/src/desktop.cpp b/src/desktop.cpp index 72ea0bd..0912013 100644 --- a/src/desktop.cpp +++ b/src/desktop.cpp @@ -7,35 +7,10 @@ #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() << " "; - if (tuner->GetFound()) - cout << tuner->GetNoteName() << " " << tuner->GetOctave() << " " << tuner->GetDeviation(); - cout << endl; - - if (fin.eof()) break; - } - delete tuner; -} - Q_DECL_EXPORT int main(int argc, char* argv[]) { if (argc == 2) { - analyse_file(argv[1]); + Tuner::analyse_file(argv[1]); return 0; } diff --git a/src/sailfish.cpp b/src/sailfish.cpp index 1fa1702..6b5ff15 100644 --- a/src/sailfish.cpp +++ b/src/sailfish.cpp @@ -27,6 +27,14 @@ class Main { Q_DECL_EXPORT int main(int argc, char* argv[]) { + if (argc == 2) { + Tuner::analyse_file(argv[1]); + return 0; + } + else if (argc == 3 && strcmp(argv[1], "record") == 0) { + Tuner::set_record(argv[2]); + } + qmlRegisterType("harbour.sailtuner.tuner", 1, 0, "Tuner"); Main *appli = new Main(argc, argv); return appli->Launch();