#ifndef CAUDIOPLAYERPROC_H
#define CAUDIOPLAYERPROC_H

#include <QObject>
#include <QMediaPlayer>
#include <QMediaPlaylist>
#include "portaudio.h"
#include "MixerMicrophoneValue.h"

//wav头的结构如下所示：
typedef   struct   {
    char         fccID[4];
    unsigned   long       dwSize;
    char         fccType[4];
}HEADER;

typedef   struct   {
    char         fccID[4];
    unsigned   long       dwSize;
    unsigned   short     wFormatTag;
    unsigned   short     wChannels;
    unsigned   long       dwSamplesPerSec;
    unsigned   long       dwAvgBytesPerSec;
    unsigned   short     wBlockAlign;
    unsigned   short     uiBitsPerSample;
}FMT;

typedef   struct   {
    char         fccID[4];
    unsigned   long       dwSize;
}DATA;


class CAudioPlayerProc : public QObject
{
    Q_OBJECT
public:
    explicit CAudioPlayerProc(QObject *parent = nullptr);
    ~CAudioPlayerProc();

    int GetMediaDuration(QString sAudio);
    void setAudio(QString sAudio);
    int playAudio(QString sAudio, bool IsRepeat = false);
    void stopPlay();
    void pausePlay();
	void play();
    void setVol(int vol);

    void setMicVol(int nVol);
    void setHeadSetVol(int nVol);

    bool getDeviceinfo(std::string &sName);
    bool startRecord(std::string sFile);
    bool stopRecord();
    bool isRecording();
    QString getErrMsg();

    static int SAMPLE_RATE;//采样率
    static int NUM_CHANNELS; //通道
    static int FRAMES_PER_BUFFER;
private slots:
    void onMetaDataAvailableChanged(bool available);
signals:
    void durationChange(int nSecord);
    void audioLoud(int nLoud);
private:
    void calcVol(short *p, int nsize);
    void setWavHead();
    const char* PaGetErrorText(int errorCode);

    static int recordCb(
        const void *input, void *output,
        unsigned long frameCount,
        const PaStreamCallbackTimeInfo* timeInfo,
        PaStreamCallbackFlags statusFlags,
        void *userData );

     QMediaPlayer *m_pAudioPlayer;
     QMediaPlaylist  m_playlist;

     FILE  *m_RawFile;
     PaStreamParameters m_inputParameters;
     PaStreamParameters m_outputParameters;
     PaStream *m_stream;
     QString m_sErrMsg;

     static CAudioPlayerProc *pThis;
     static int m_Vol;
     MixerMicrophoneValue m_mic;
};

extern std::shared_ptr<CAudioPlayerProc> g_audioPalyerPtr;

#endif // CAUDIOPLAYERPROC_H
