bool cOAL_OggStream::CreateFromFile(const wstring &asFilename)
{
	DEF_FUNC_NAME("cOAL_OggStream::Load()");
	
	if(mbStatus==false)
		return false;

	int lOpenResult;
	FILE *pStreamFile = OpenFileW(asFilename, L"rb");

	if (pStreamFile == NULL)
		return false;

	msFilename = asFilename;

	// If not an Ogg file, set status and exit

	lOpenResult = ov_open_callbacks(pStreamFile, &movStreamHandle,
									NULL, 0, OV_CALLBACKS_DEFAULT);
	if(lOpenResult<0)	
	{
		fclose(pStreamFile);
		mbIsValidHandle = false;
		mbStatus = false;
		return false;
	}
	mbIsValidHandle = true;

	// Get file info
	vorbis_info *viFileInfo = ov_info ( &movStreamHandle, -1 );

	mlChannels = viFileInfo->channels;
	mlFrequency = viFileInfo->rate;
	mlSamples = (long) ov_pcm_total ( &movStreamHandle, -1 );
	mFormat = (mlChannels == 2) ? AL_FORMAT_STEREO16 : AL_FORMAT_MONO16;

	mfTotalTime = ov_time_total( &movStreamHandle, -1 );

	return true;
}
bool cOAL_OggSample::CreateFromFile(const wstring &asFilename)
{
	DEF_FUNC_NAME("cOAL_OggSample::Load()");
	FUNC_USES_AL;

	if(mbStatus==false)
		return false;

	Reset();
	
	char *pPCMBuffer;
	bool bEOF = false;
	int lOpenResult;
	int lCurrent_section;
	long lDataSize = 0;

	msFilename = asFilename;

	FILE *fileHandle = OpenFileW(asFilename, L"rb");
	
	// If no file is present, set the error status and return
	if(!fileHandle)
	{
		mbStatus = false;
		return false;
	}

	// If not an Ogg file, set status and exit
	OggVorbis_File ovFileHandle;
	if((lOpenResult = ov_open_callbacks(fileHandle, &ovFileHandle, NULL, 0, gCallbacks))<0)	
	{
		fclose ( fileHandle );
		mbStatus = false;
		return false;
	}

	// Get file info
	vorbis_info *viFileInfo = ov_info ( &ovFileHandle, -1 );
	mlChannels = viFileInfo->channels;
	mFormat = (mlChannels == 2)?AL_FORMAT_STEREO16:AL_FORMAT_MONO16;
	mlFrequency = viFileInfo->rate;
	mlSamples = (long) ov_pcm_total ( &ovFileHandle, -1 );
	mfTotalTime = ov_time_total( &ovFileHandle, -1 );

	// Reserve memory for 'mlChannels' channels of 'mlSamples' * 2 bytes of data each
	int lSizeInBytes = mlSamples * mlChannels * GetBytesPerSample();
	pPCMBuffer = (char *) malloc (lSizeInBytes);
	memset (pPCMBuffer, 0, lSizeInBytes);

	// Loop which loads chunks of decoded data into a buffer
	while(!bEOF)
	{
		long lChunkSize = ov_read ( &ovFileHandle			,										
									pPCMBuffer + lDataSize	, 
									STREAMING_BLOCK_SIZE	, 
									SYS_ENDIANNESS			,
									2, 1, &lCurrent_section );

		if(lChunkSize == 0)
			bEOF = true;
		// If we get a negative value, then something went wrong. Clean up and set error status.
		else if(lChunkSize < 0)
		{
			free(pPCMBuffer);
			ov_clear(&ovFileHandle);
			// ov_clear closes the file handle for us
			mbStatus = false;
			return false;
		}
		else 
			lDataSize += lChunkSize;
	}

	cOAL_Buffer* pBuffer = mvBuffers[0];
	if(lDataSize)
	{
		// If something went wrong, set error status. Clean up afterwards.
		mbStatus = pBuffer->Feed((ALvoid*)pPCMBuffer, lDataSize);
	}
	free(pPCMBuffer);
	ov_clear(&ovFileHandle);
	// ov_clear closes the file handle for us

	return true;
}