SoundDecoder * SoundHandler::GetSoundDecoder(const u8 * sound, int length)
{
	const u8 * check = sound;
	int counter = 0;

	while(check[0] == 0 && counter < length)
	{
		check++;
		counter++;
	}

	if(counter >= length)
		return NULL;

	u32 * magic = (u32 *) check;

	if(magic[0] == 0x4f676753) // 'OggS'
	{
	    return new OggDecoder(sound, length);
	}
	else if(magic[0] == 0x52494646) // 'RIFF'
	{
		return new WavDecoder(sound, length);
	}
	else if(CheckMP3Signature(check) == true)
	{
		return new Mp3Decoder(sound, length);
	}

	return new SoundDecoder(sound, length);
}
SoundDecoder * SoundHandler::GetSoundDecoder(const char * filepath)
{
	u32 magic;
	CFile f(filepath, CFile::ReadOnly);
	if(f.size() == 0)
		return NULL;

	do
	{
		f.read((u8 *) &magic, 1);
	}
	while(((u8 *) &magic)[0] == 0 && f.tell() < f.size());

	if(f.tell() == f.size())
		return NULL;

	f.seek(f.tell()-1, SEEK_SET);
	f.read((u8 *) &magic, 4);
	f.close();

	if(magic == 0x4f676753) // 'OggS'
	{
	    return new OggDecoder(filepath);
	}
	else if(magic == 0x52494646) // 'RIFF'
	{
		return new WavDecoder(filepath);
	}
	else if(CheckMP3Signature((u8 *) &magic) == true)
	{
		return new Mp3Decoder(filepath);
	}

	return new SoundDecoder(filepath);
}
SoundDecoder * SoundHandler::GetSoundDecoder(const char * filepath)
{
	u32 magic;
	CFile f(filepath, "rb");
	if(f.size() == 0)
		return NULL;

	do
	{
		f.read((u8 *) &magic, 1);
	}
	while(((u8 *) &magic)[0] == 0 && f.tell() < f.size());

	if(f.tell() == f.size())
		return NULL;

	f.seek(f.tell()-1, SEEK_SET);
	f.read((u8 *) &magic, 4);
	f.close();

	if(magic == 'OggS')
	{
		return new OggDecoder(filepath);
	}
	else if(magic == 'RIFF')
	{
		return new WavDecoder(filepath);
	}
	else if(magic == 'BNS ')
	{
		return new BNSDecoder(filepath);
	}
	else if(magic == 'FORM')
	{
		return new AifDecoder(filepath);
	}
	else if(CheckMP3Signature((u8 *) &magic) == true)
	{
		return new Mp3Decoder(filepath);
	}

	return new SoundDecoder(filepath);
}