/** SGX

The   sss    gggg x   x   
     s      g      x x    Engine
      sss   g  gg   x
         s  g   g  x x          Copyright 1995-2009.
     ssss    gggg x   x         Steven Goodwin. All Rights Reserved.
    
This version has been released under the GNU Public License v2.0.

Please see the license files for details.

File: sgx/include/domains/audio/wavedata.hpp

*/

#ifndef SGX_DOMAIN_AUDIO_WAVEDATA_HPP
#define SGX_DOMAIN_AUDIO_WAVEDATA_HPP	1


class CAudioDataChunk {
public:
	tUINT32		m_iTag;
	//
	tUINT32		m_iSize;
	tBYTE *		m_pData;

	CAudioDataChunk(const tUINT32 tag, tBYTE *pData, const tUINT32 sz) {
		m_iTag  = tag;
		m_iSize = sz;
		m_pData = pData;
	}

	~CAudioDataChunk() {
		delete m_pData;
	}

};



class CAudioData {
public:

	~CAudioData() {
		sgxVector<CAudioDataChunk *>::iterator	it = m_Chunks.begin();
		for(;it!=m_Chunks.end();++it) {
			delete *it;
		}
	}

	void addChunk(CAudioDataChunk *pChunk) {
		m_Chunks.push_back(pChunk);
	}

	static tUINT32 getTagID(const sgxString &tag) {
		tUINT32 id = 0;
		id |= tag[0] | (tag[1] << 8) | (tag[2] << 16) | (tag[3] << 24);
		return id;
	}

	CAudioDataChunk * getChunk(const sgxString &tag) {
		return getChunk(getTagID(tag));
	}

	CAudioDataChunk * getChunk(const tUINT32 tag) {
		sgxVector<CAudioDataChunk *>::iterator	it = m_Chunks.begin();
		for(;it!=m_Chunks.end();++it) {
			if ((*it)->m_iTag == tag) {
				return *it;
			}
		}
		return NULL;
	}

	tREAL32 getDuration();

protected:
	sgxVector<CAudioDataChunk *>	m_Chunks;
};


class CAudioDataWaveHeader : public CAudioDataChunk {
public:
	tUINT32		m_iChannels;
	tUINT32		m_iSampleRate;	// in samples per second
	tUINT32		m_iDataRate;	// in bytes per second
	tUINT32		m_iBitDepth;

	CAudioDataWaveHeader(const tUINT32 tag, tBYTE *pData, const tUINT32 sz) :
	CAudioDataChunk(tag, pData, sz) {
		m_iChannels = *(tUINT16 *)(pData+2);
		m_iSampleRate = *(tUINT32 *)(pData+4);
		m_iDataRate = *(tUINT32 *)(pData+8);
		m_iBitDepth = *(tUINT16 *)(pData+14);
	}

};


#endif /* SGX_DOMAIN_AUDIO_WAVEDATA_HPP */
