//    tonalisa - software to look at overtone-structures
//    Copyright (C) 2016  Dominik Schmidt-Philipp
//
//    This program 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.
//
//    This program 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.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
#ifndef PEAKS_H
#define PEAKS_H

#include <QObject>
#include "clm.h"


class Peaks : public QObject
{
  Q_OBJECT
  Q_PROPERTY(QList<double> freqs READ freqs WRITE setFreqs NOTIFY peaksChanged)
  Q_PROPERTY(QList<double> amps READ amps WRITE setAmps NOTIFY peaksChanged)
  Q_PROPERTY(QList<double> peaks READ peaks NOTIFY peaksChanged)
  Q_PROPERTY(double freqBase READ freqBase WRITE setFreqBase NOTIFY freqBaseChanged)
  Q_PROPERTY(QList<double> fftData READ fftData WRITE setFftData NOTIFY fftDataChanged)
  Q_PROPERTY(double sampleRate READ sampleRate WRITE setSampleRate NOTIFY sampleRateChanged)
  Q_PROPERTY(int windowIndex READ windowIndex WRITE setWindowIndex NOTIFY windowChanged)
  Q_PROPERTY(int windowSize READ windowSize WRITE setWindowSize NOTIFY windowChanged)
  Q_PROPERTY(double threshold READ threshold WRITE setThreshold NOTIFY thresholdChanged)
  
 public:
  Peaks(QObject *parent=0);

  // getter functions:
  QList<double> freqs() const;
  QList<double> amps() const;
  QList<double> peaks() const;
  double freqBase() const;
  QList<double> fftData();
  double sampleRate() const;
  int windowIndex() const;
  int windowSize() const;
  double threshold() const;

  // setter functions:
  void setFreqBase(const double &freqBase);
  void setFftData(QList<double> &fftData);
  void setSampleRate(const double &sampleRate);
  void setWindowIndex(const int &indx);
  void setWindowSize(const int &windowSize);
  void setFreqs(QList<double> &freqs);
  void setAmps(QList<double> &amps);
  void setThreshold(const double &threshold);

  Q_INVOKABLE void resetPeaks();
  Q_INVOKABLE void findHighestPeak();
  Q_INVOKABLE void addPeak(double freq, double amp);
  Q_INVOKABLE void oscSendPeaks();
  
 signals:
  void freqBaseChanged();
  void peaksChanged();
  void fftDataChanged();
  void sampleRateChanged();
  void windowChanged();
  void thresholdChanged();
  
 private:
  QList<double> m_freqs;
  QList<double> m_amps;
  QList<double> m_fftData;
  double m_sampleRate;
  double m_freqBase;
  long m_fftSize;
  mus_fft_window_t m_windowIndex;
  int m_windowSize;
  double m_threshold;

  void new_fftData();
  QVector<double> interpolatePeak(int k_beta);
};
#endif // PEAKS_H
