// Asura_Platform_Audio.cpp
// Thomas Young - 15.09.08
// Copyright (C) 2008 Rebellion, All Rights Reserved.

// I N C L U D E S ///////////////////////////////////////////////////////////

#include "Core/Asura.h"

#if defined ASURA_USE_AUDIO_SYSTEM

//Asura headers
#include "ChunkSystem/Asura_Chunk_ResourceFile.h"
#include "Debugging/Asura_Assert.h"
#include "Sound/Asura_Audio_System.h"
#include "Sound/Asura_Sound_Mixer.h"

//PS3 Headers
#include "Sound/Asura_PS3_Multistream_Audio.h"

// F U N C T I O N   D E F I N I T I O N S ///////////////////////////////////

void Asura_Audio_System::Platform_Initialise()
{
	Asura_MultiStream_Audio::Initialise();
}


void Asura_Audio_System::Platform_Reset()
{
	
}


void Asura_Audio_System::Platform_Shutdown()
{
	Asura_MultiStream_Audio::Shutdown();
}


void Asura_Audio_System::Platform_Update()
{
	Asura_MultiStream_Audio::Update();
}


void Asura_Audio_System::Platform_CreateDSPChain( const Asura_Hash_ID uHash, const Asura_Audio_DSPChain& xChain )
{
	Asura_MultiStream_Audio::CreateDSPChain( uHash, &xChain );
}


Asura_Handle Asura_Audio_System::Platform_CreateVoice ( const Asura_Hash_ID uWave, const bool bFilter, const bool bMusic, const Asura_Hash_ID uDSPChain )
{
	Asura_Handle				iHandle	= Asura_MultiStream_Audio::CreateVoice ( uWave );
	Asura_MultiStream_Voice*	pxVoice	= reinterpret_cast<Asura_MultiStream_Voice*>( Asura_MultiStream_Audio::GetVoice	( iHandle ) );

	if( pxVoice )
	{
		pxVoice->SetVoiceDSPChain		( uDSPChain	);
	}

	return iHandle;
}


Asura_Handle Asura_Audio_System::Platform_CreateStreamingVoice( const Asura_Hash_ID uStreamHash, const bool bFilter, const bool bMusic, const float fTimeOffset, const Asura_Hash_ID uDSPChain )
{
	Asura_Handle							iHandle = Asura_MultiStream_Audio::CreateStreamingVoice();
	Asura_MultiStream_MP3_Streaming_Voice*	pxVoice = reinterpret_cast<Asura_MultiStream_MP3_Streaming_Voice*>( Asura_MultiStream_Audio::GetVoice( iHandle ) );

	if( pxVoice )
	{
		pxVoice->SetVoiceDSPChain	( uDSPChain		);
		pxVoice->SetStartOffset		( fTimeOffset	);
		pxVoice->Prepare			( uStreamHash	);
	}

	return iHandle;
}

Asura_Handle Asura_Audio_System::Platform_CreateSubmixVoice( const bool bFilter, const bool bMusic, const Asura_Hash_ID uDSPChain )
{
	Asura_Handle						iHandle = Asura_MultiStream_Audio::CreateSubmixVoice();
	Asura_MultiStream_Submix_Voice*		pxVoice = reinterpret_cast<Asura_MultiStream_Submix_Voice*>( Asura_MultiStream_Audio::GetVoice( iHandle ) );

	if( pxVoice )
	{
		pxVoice->SetVoiceDSPChain		( uDSPChain	);
	}

	return iHandle;
}

void Asura_Audio_System::Platform_DestroyVoice( const Asura_Handle iVoiceHandle )
{
	Asura_MultiStream_Audio::DestroyVoice( iVoiceHandle );
}


Asura_Audio_Voice* Asura_Audio_System::Platform_GetVoice( const Asura_Handle iVoiceHandle )
{
	return Asura_MultiStream_Audio::GetVoice( iVoiceHandle );
}


void Asura_Audio_System::Platform_SetMasterDSP( const Asura_Hash_ID uDSPChain, const float fFadeTime )
{
}


bool Asura_Audio_System::Platform_LoadWave( Asura_Chunk_Stream& xStream, const u_int uFileSize, const u_int uSubType, const u_int uFlags, const char* const szFilename )
{
	bool bAlreadyExists;
	Asura_MultiStream_Buffer* pxBuffer = Asura_MultiStream_Audio::CreateBuffer( szFilename, bAlreadyExists );
	if( pxBuffer && !bAlreadyExists )
	{
		if( pxBuffer->Load( xStream, szFilename, uFileSize ) )
		{
			if( uFlags & Asura_Chunk_ResourceFile::SF_LOOPING )
			{
				pxBuffer->SetAsLooping();
			}
			return true;
		}

		Asura_Assertf( false, ("Failed to load file '%s'.", szFilename) );
	}
	return false;
}


#if defined ASURA_AUDIO_STORE_FILENAMES

const char* Asura_Audio_System::Platform_GetWaveFilename( const Asura_Hash_ID uWave )
{
	return Asura_MultiStream_Audio::GetWavFilename( uWave );
}

#endif // defined ASURA_AUDIO_STORE_FILENAMES


#endif // defined ASURA_USE_AUDIO_SYSTEM
