//    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 AUDIOFILE_H
#define AUDIOFILE_H

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

class AudioFile : public QObject
{
    Q_OBJECT
    
    // Active Audio File:
    //-------------------
    // Path
    Q_PROPERTY(QString path READ path WRITE setPath NOTIFY pathChanged)
    // Samples
    Q_PROPERTY(QList<double> samples READ samples NOTIFY samplesChanged)
    // properties
    Q_PROPERTY(int sampleRate READ sampleRate NOTIFY sampleRateChanged)
	
    // Fourier Transform:
    // ------------------
    // size
    Q_PROPERTY(long npts READ npts WRITE setNpts NOTIFY nptsChanged)
    // FFT data
    Q_PROPERTY(QList<double> spectrum READ spectrum NOTIFY spectrumChanged)
    Q_PROPERTY(int windowIndex READ windowIndex WRITE setWindowIndex NOTIFY windowChanged)
    Q_PROPERTY(int windowSize READ windowSize WRITE setWindowSize NOTIFY windowSizeChanged)
    // selection
    Q_PROPERTY(float select1 READ select1 WRITE setSelect1 NOTIFY selectionChanged)
    Q_PROPERTY(float select2 READ select2 /*WRITE setSelect2*/ NOTIFY selectionChanged)

  public:
    AudioFile(QObject *parent = 0);
	
    Q_INVOKABLE void calculateFFT();
	
    QString path() const;
    int sampleRate() const;
    QList<double> samples() const;
    long npts() const;
    QList<double> spectrum() const;
    int windowIndex() const;
    long windowSize() const;
    float select1() const;
    float select2() const;
    
    void setPath( QString &path );
    void setNpts( long &npts );
    void setWindowIndex(const int &indx);
    void setWindowSize( int &windowSize);
    void setSelect1( const float &sample );
    //    void setSelect2( const float &sample );

  signals:
    void pathChanged();
    void samplesChanged();
    void sampleRateChanged();
    void nptsChanged();
    void spectrumChanged();
    void windowChanged();
    void windowSizeChanged();
    void selectionChanged();

  private:
    QString m_path;
    QList<double> m_samples;
    bool validFile;
    int m_sampleRate;
    mus_long_t framples;
    int chans;
    float m_select1;
    float m_select2;
    mus_long_t m_fftSize;
    QList<double> m_spectrum;
    void read_samples();
    int read_header();
    mus_fft_window_t m_windowIndex;
    long m_windowSize;
    mus_long_t nextPowerOfTwo(long long x);
    
};
#endif // AUDIOFILE_H
