SoundChannel * SoundSystem::FindChannel(int32 priority)
{
	Deque<SoundChannel*>::iterator it;
	Deque<SoundChannel*>::iterator itEnd = channelsPool.end();
	for(it = channelsPool.begin(); it != itEnd; ++it)
	{
		SoundChannel * ch = *it;
		if(SoundChannel::STATE_FREE == ch->GetState())
		{
			return ch;
		}
	}

	for(it = channelsPool.begin(); it != itEnd; ++it)
	{
		SoundChannel * ch = *it;
		if(ch->GetProirity() < priority)
		{
			ch->Stop();
			return ch;
		}
	}

	return 0;
}
void SoundSystem::Update()
{
	Deque<SoundChannel*>::iterator it;
	Deque<SoundChannel*>::iterator itEnd = channelsPool.end();
	for(it = channelsPool.begin(); it != itEnd; ++it)
	{
		SoundChannel * ch = *it;
		if(SoundChannel::STATE_FREE != ch->GetState())
		{
			ch->Update();
		}
	}

	List<SoundInstance*>::iterator sit = soundInstances.begin();
	List<SoundInstance*>::iterator sEnd = soundInstances.end();
	while(sit != sEnd)
	{
		if(!(*sit)->Update())
		{
			sit = soundInstances.begin();
			continue;
		}
		++sit;
	}
}
void SoundSystem::Resume()
{
#ifdef __DAVASOUND_AL__
	alcProcessContext(context);
	Deque<SoundChannel*>::iterator it;
	Deque<SoundChannel*>::iterator itEnd = channelsPool.end();
	for(it = channelsPool.begin(); it != itEnd; ++it)
	{
		SoundChannel * ch = *it;
		if(SoundChannel::STATE_PAUSED == ch->GetState())
		{
			ch->Pause(false);
		}
	}
#endif
}
void SoundSystem::Suspend()
{
	Deque<SoundChannel*>::iterator it;
	Deque<SoundChannel*>::iterator itEnd = channelsPool.end();
	for(it = channelsPool.begin(); it != itEnd; ++it)
	{
		SoundChannel * ch = *it;
		if(SoundChannel::STATE_PLAYING == ch->GetState())
		{
			ch->Pause(true);
		}
	}
#ifdef __DAVASOUND_AL__
	alcSuspendContext(context);
#endif
}