/*  $Id: resourcetype_sample.cpp,v 1.9 2003/11/11 10:13:25 grumbel Exp $
**
**  ClanLib Game SDK
**  Copyright (C) 2003  The ClanLib Team
**  For a total list of contributers see the file CREDITS.
**
**  This library is free software; you can redistribute it and/or
**  modify it under the terms of the GNU Lesser General Public
**  License as published by the Free Software Foundation; either
**  version 2.1 of the License, or (at your option) any later version.
**
**  This library is distributed in the hope that it will be useful,
**  but WITHOUT ANY WARRANTY; without even the implied warranty of
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
**  Lesser General Public License for more details.
**
**  You should have received a copy of the GNU Lesser General Public
**  License along with this library; if not, write to the Free Software
**  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
**

	File purpose:
		Sample resource type

*/

#include "Sound/precomp.h"

#ifdef _MSC_VER
#pragma warning (disable:4786)
#endif

#include "resourcetype_sample.h"
#include "API/Core/System/cl_assert.h"
#include "API/Core/System/error.h"
#include "API/Core/Resources/resource.h"
#include "API/Core/Resources/resource_manager.h"
#include "API/Core/IOData/inputsource.h"
#include "API/Core/IOData/inputsource_provider.h"
#include "API/Core/IOData/outputsource.h"
#include "API/Core/IOData/outputsource_provider.h"
#include "API/Sound/soundbuffer.h"
#include "API/Sound/soundprovider.h"
#include "API/Sound/SoundProviders/soundprovider_wave.h"
#include "API/Sound/SoundProviders/soundprovider_factory.h"

/////////////////////////////////////////////////////////////////////////////
// CL_ResourceData_Sample Construction:

CL_ResourceData_Sample::CL_ResourceData_Sample(class CL_Resource &resource)
: CL_ResourceData(resource)
{
	resource.attach_data("sample", this);
}

CL_ResourceData_Sample::~CL_ResourceData_Sample()
{
}

/////////////////////////////////////////////////////////////////////////////
// CL_ResourceData_Sample Attributes:

/////////////////////////////////////////////////////////////////////////////
// CL_ResourceData_Sample Implementation:

void CL_ResourceData_Sample::on_load()
{
	CL_Resource resource = get_resource();
	CL_DomElement &element = resource.get_element();

	CL_SoundProvider *provider = 0;

	std::string name = resource.get_element().get_attribute("file");
	std::string sound_format = resource.get_element().get_attribute("format");
	bool streamed = (element.get_attribute("stream", "no") == "yes");

	provider = CL_SoundProviderFactory::load(
		name,
		streamed,
		sound_format,
		resource.get_manager().get_resource_provider());

	if (provider)
	{
		soundbuffer = new CL_SoundBuffer(provider, true);
	}
	else
	{
		throw CL_Error("Unknown sample format");
	}
}

void CL_ResourceData_Sample::on_unload()
{
	delete soundbuffer;
	soundbuffer = NULL;
}
