/** 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/src/utils/audio/waveparser.cpp

*/

#include "sgx/sgx.hpp"
#include "utils/audio/audio.hpp"

#include "domains/audio/wavedata.hpp"


CAudioData *	
CAudioWaveHandler::loadWave(const sgxString &filename) {
CSGXFile file(filename);

	return loadWave(file);
}

CAudioData *	
CAudioWaveHandler::loadWave(CSGXFile &file) {
CAudioData *pAudioData = NULL;

	if (file.open()) {
		tUINT32 maintag, tag;
		tINT32 size, chunkSize;

		file >> maintag >> size >> tag;

		if (maintag != CAudioData::getTagID("RIFF")) {
			return NULL;
		}

		if (tag != CAudioData::getTagID("WAVE")) {
			return NULL;
		}
		//
		// Now read the chunks
		//
		const tUINT32 fmt = CAudioData::getTagID("fmt ");
		pAudioData = new CAudioData();
		while(size > 0 && !file.isEOF()) {
			file >> tag >> chunkSize;

			tBYTE *pData = new tBYTE[chunkSize];
			file.read(pData, chunkSize);
			//
			if (tag == fmt) {
				pAudioData->addChunk(new CAudioDataWaveHeader(tag, pData, chunkSize));
			} else {
				pAudioData->addChunk(new CAudioDataChunk(tag, pData, chunkSize));
			}
			
		}

	}

	return pAudioData;
}

tBOOL	
CAudioWaveHandler::saveWave(CAudioData *pData) {
SGX_UNUSED(pData);
	return FALSE;
}

