//---------------------------------------------------------------------------

#ifndef AudioTrackH
#define AudioTrackH
//---------------------------------------------------------------------------

#include "FxDef.h"
#include "BaseTrack.h"

#include <vector>
using namespace std;

#define MODE_INIT	0
#define MODE_CHANGE 1
#define MODE_INSERT 2
#define MODE_DELETE 3     //删除小节
#define MODE_MOVE	4     //移动（小节/音频）
#define MODE_COPY	5     //复制（小节/音频）
#define MODE_ERASE	6     //擦除音频
#define MODE_LOAD	7
#define MODE_RECORD	8
#define MODE_RELOAD	9
#define OPT_BARS	0x10

#define POINTS_PER_BAR 120
#define MAX_AUDIOTRACK 17

typedef struct tagWaveBar   //波形小节
{
	//每小节最多POINTS_PER_BAR个音量值
	DWORD aVols[POINTS_PER_BAR];

	tagWaveBar()
	{
		memset( this, 0, sizeof(tagWaveBar) );
	}

	tagWaveBar( tagWaveBar* pSrc )
	{
		memcpy( this, pSrc, sizeof(tagWaveBar));
	}
} TWaveBar;

typedef vector<TWaveBar*> TWaveBarList;
typedef vector<TAudioFx*> TAudioFxList;

class TAudioTrack : public TBaseTrack
{
public:
	TWaveBarList lstBars;
	TAudioFxList lstFxs;

	TAudioTrack() : TBaseTrack()
	{
	}

	TAudioTrack( TAudioTrack* pSrc ) : TBaseTrack( pSrc->state, pSrc->volume, pSrc->pan, pSrc->gain )
	{
	}

	~TAudioTrack()
	{
		while( !lstBars.empty() ){
			delete lstBars.back();
			lstBars.pop_back();
		}

		while( !lstFxs.empty() ){
			delete lstFxs.back();
			lstFxs.pop_back();
		}
	}
};

typedef vector<TAudioTrack*> TAudioTrackList;

class TAudio
{
private:
	TAudioTrackList m_lstTracks;

public:
	TAudioTrackList& AudioTracks() { return m_lstTracks; };
	void CopyTrackList( int iStartBar, int nCopyBars, TAudioTrackList* lstAudio, bool bFx=false );
	TAudioTrack* CopyTrack( int iTrack, int iStartBar, int nCopyBars, bool bFx=false,
																	TAudioTrack* pDest=NULL );
	void ResetTracks( TAudioTrackList* lstAudio );
	void RestoreAudio( TOpusStageList* lstStages, TAudioTrackList* lstAudio, bool bUndo );
	bool Empty(){ return m_lstTracks.size()==1 && m_lstTracks[0]->lstBars.empty(); };

	void WriteProj( FILE* pFile );
	int ReadProj( FILE* pFile, DWORD dwVersion );

	TAudio() {
		m_lstTracks.push_back( new TAudioTrack );
	};

	~TAudio(){
		while( m_lstTracks.size()>0 ){
			delete m_lstTracks.back();
			m_lstTracks.pop_back();
		}
	}
};

extern TAudio 				g_Audio;
extern int					g_iSelAudio;			//当前处理的音轨
extern TAudioTrackList& g_aAudioTracks;  	//全曲音频轨道（最大4组）

/////////////////////////////////////////////
//兼容早期版本
typedef struct tagEffect
{
	int iBarStart, iBarEnd;
	int iPointStart, iPointEnd;
	bool bOn;
	int iPreset;
	float *aParams;
	tagEffect* pNext;

	tagEffect(){
		memset( this, 0, sizeof(tagEffect) );
		iPreset = -1;
	}

	tagEffect( tagEffect* pSrc ){
		pNext = NULL;
		iBarStart = pSrc->iBarStart;
		iBarEnd = pSrc->iBarEnd;
		iPointStart = pSrc->iPointStart;
		iPointEnd = pSrc->iPointEnd;
		iPreset = pSrc->iPreset;
		bOn = pSrc->bOn;
		if( pSrc->aParams ){
			aParams = new float[6];
			memcpy( aParams, pSrc->aParams, sizeof(float)*6 );
		}
	}

	~tagEffect(){
		if( aParams )
			delete aParams;
	}
}TEffect, *PTEffect;
//
//////////////////////////////////////////////
/***************************************************************************************/
// 全局函数
/***************************************************************************************/
TAudioTrack* NewAudioTrack( int nBar, bool bAddUndo=true );
void DeleteAudioTrack( int iTrack, bool bAddUndo=true );
void DuplicateRawBars();
TAudioTrack* DuplicateAudioTrack( TAudioTrack* pSrc );
bool MoveAudio( int iSrcTrack, double dblStart, double dblEnd,
				int iDestTrack, double dblInsert, int iOption ); //返回：是否刷新整个音轨
//int GetShowWave( int iTrack ); //获取当前显示的音轨
int CopyWave( int iTrack, int iStartBar, int iStartPoint,
							int iEndBar, int iEndPoint, DWORD** aWave ); //复制波形
void RemoveWaveBars( int iTrack, int iStartBar, int nDelBars );
void InsertWaveBars( int iTrack, int iInsertBar, TWaveBarList lstCopyBars );
void RestoreWaveBars( int iInsBar, int nBars, DWORD* aWave );
TAudioTrack* CopyAudioTrack( int iTrack, int iStartBar, int nCopyBars );
void InsertAudioTrack( int iTrack, int iInsertBar, int nBars, TAudioTrack* pSrcTrack, int iStartBar=0 );

//---------------------------------------------------------------------------
#endif
