//------------------------------------------------------------------------------
// Lamp : Open source game middleware
// Copyright (C) 2004  Junpei Ohtani ( Email : junpee@users.sourceforge.jp )
//
// 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
 * ステレオサウンド実装
 * @author Junpee
 */

#include "LampBasic.h"
#include "Sound/Stereo/StereoSound.h"

namespace Lamp{

//------------------------------------------------------------------------------
// コンストラクタ
StereoSound::StereoSound(DirectSoundBuffer* soundBuffer) :
	SoundBuffer(soundBuffer), pan_(0.f){
	// 初期データの取得
	long decibelPan;
	if(DirectXFailed(soundBuffer->GetPan(&decibelPan))){
		ErrorOut("StereoSound::StereoSound() パンの取得に失敗しました。");
	}
	int absDecibelPan = Math::abs(decibelPan);
	pan_ = 1.f - decibelToVolume(-absDecibelPan);
	if(decibelPan < 0.f){ pan_ = -pan_; }
}
//------------------------------------------------------------------------------
// デストラクタ
StereoSound::~StereoSound(){
}
//------------------------------------------------------------------------------
// ステレオサウンドデータのコピー
void StereoSound::copyStereoSoundData(StereoSound* destination){
	copySoundBufferData(destination);
}
//------------------------------------------------------------------------------
// パンの設定
void StereoSound::setPan(float pan){
	if(pan_ == pan){ return; }
	Assert(pan >= -1.f);
	Assert(pan <= 1.f);
	float absPan = Math::abs(pan);
	if(absPan <= 0.002f){// 0.002f以下はdbが0になる
		if(DirectXFailed(getSoundBuffer()->SetPan(DSBPAN_CENTER))){
			ErrorOut("StereoSound::setPan() パンの設定に失敗しました。");
		}
	}else{
		// Panは絶対値が0から1に近づく程、音量が下がる
		int db = volumeToDecibel(1.f - absPan);
		if(db == 0){ db = DSBPAN_LEFT; }
		Assert((db <= DSBPAN_CENTER) && (db >= DSBPAN_LEFT));
		if(pan > 0.f){ db = -db; }
		if(DirectXFailed(getSoundBuffer()->SetPan(db))){
			ErrorOut("StereoSound::setPan() パンの設定に失敗しました。");
		}
	}
	pan_ = pan;
}
//------------------------------------------------------------------------------
// リセット
void StereoSound::reset(Reset flags){
	SoundBuffer::reset(flags);
	if((flags & resetPan) != 0){ setPan(0.f); }
}
//------------------------------------------------------------------------------
// 文字列への変換
String StereoSound::toString() const{
	String result, temp;
	result = SoundBuffer::toString();
	temp.format("Pan(%.2f) BufferSize(%7dbyte)\n",
		getPan(), getBufferSize());
	result += temp;
	return result;
}
//------------------------------------------------------------------------------
} // End of namespace Lamp
//------------------------------------------------------------------------------
