/*  $Id: soundoutput.cpp,v 1.6 2003/09/08 15:34:13 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
**
*/

#include "Sound/precomp.h"
#include "API/Sound/soundoutput.h"
#include "API/Sound/soundoutput_description.h"
#include "API/Sound/soundfilter.h"
#include "API/Sound/sound.h"
#include "API/Core/System/thread.h"
#include "soundoutput_generic.h"
#ifdef WIN32
#include "Win32/soundoutput_directsound.h"
#else
#include "Unix/soundoutput_oss.h"
#endif

/////////////////////////////////////////////////////////////////////////////
// CL_SoundOutput construction:

CL_SoundOutput::CL_SoundOutput() : impl(0)
{
}

CL_SoundOutput::CL_SoundOutput(int mixing_frequency) : impl(0)
{
	CL_SoundOutput_Description desc;
	desc.set_mixing_frequency(mixing_frequency);
	operator =(CL_SoundOutput(desc));
}

CL_SoundOutput::CL_SoundOutput(const CL_SoundOutput_Description &desc) : impl(0)
{
#ifdef WIN32
	impl = new CL_SoundOutput_DirectSound(desc.get_mixing_frequency());
#else
	impl = new CL_SoundOutput_OSS(desc.get_mixing_frequency());
#endif
	// We start the thread here because we have to be sure os specific code has initialized first.
	// And that cannot be done from the constructor of CL_SoundOutput_Generic.
	impl->thread.start();
	impl->thread.set_priority(cl_priority_highest);
	impl->add_ref();
	CL_Sound::select_output(this);
}

CL_SoundOutput::CL_SoundOutput(const CL_SoundOutput &copy) : impl(copy.impl)
{
	if (impl) impl->add_ref();
	CL_Sound::select_output(this);
}

CL_SoundOutput::~CL_SoundOutput()
{
	if (impl) impl->release_ref();
}

/////////////////////////////////////////////////////////////////////////////
// CL_SoundOutput attributes:

const std::string &CL_SoundOutput::get_name() const
{
	CL_MutexSection mutex_lock(&impl->mutex);
	return impl->name;
}

int CL_SoundOutput::get_mixing_frequency() const
{
	CL_MutexSection mutex_lock(&impl->mutex);
	return impl->mixing_frequency;
}

float CL_SoundOutput::get_global_volume() const
{
	CL_MutexSection mutex_lock(&impl->mutex);
	return impl->volume;
}

float CL_SoundOutput::get_global_pan() const
{
	CL_MutexSection mutex_lock(&impl->mutex);
	return impl->pan;
}

/////////////////////////////////////////////////////////////////////////////
// CL_SoundOutput operations:

CL_SoundOutput &CL_SoundOutput::operator =(const CL_SoundOutput &copy)
{
	if (impl) impl->release_ref();
	impl = copy.impl;
	if (impl) impl->add_ref();
	CL_Sound::select_output(this);
	return *this;
}

void CL_SoundOutput::stop_all()
{
}
	
void CL_SoundOutput::set_global_volume(float volume)
{
	if (impl)
	{
		CL_MutexSection mutex_lock(&impl->mutex);
		impl->volume = volume;
	}
}

void CL_SoundOutput::set_global_pan(float pan)
{
	if (impl)
	{
		CL_MutexSection mutex_lock(&impl->mutex);
		impl->pan = pan;
	}
}

void CL_SoundOutput::add_filter(CL_SoundFilter *filter, bool delete_filter)
{
	if (impl)
	{
		CL_MutexSection mutex_lock(&impl->mutex);
		impl->filters.push_back(filter);
		impl->delete_filters.push_back(delete_filter);
	}
}

void CL_SoundOutput::remove_filter(CL_SoundFilter *filter)
{
	if (impl)
	{
		CL_MutexSection mutex_lock(&impl->mutex);
		for (std::vector<CL_SoundFilter *>::size_type i=0; i<impl->filters.size(); i++)
		{
			if (impl->filters[i] == filter)
			{
				if (impl->delete_filters[i]) delete impl->filters[i];
				impl->filters.erase(impl->filters.begin()+i);
				impl->delete_filters.erase(impl->delete_filters.begin()+i);
				break;
			}
		}
	}
}

/////////////////////////////////////////////////////////////////////////////
// CL_SoundOutput implementation:
