00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "LampBasic.h"
00026 #include "Sound/3D/SoundListener.h"
00027 #include "Sound/System/SoundDefinition.h"
00028
00029 namespace Lamp{
00030
00031
00032
00033 SoundListener::SoundListener(DirectSoundPrimaryBuffer* primaryBuffer) :
00034 listener_(NULL), distanceFactor_(0.f){
00035
00036 if(DirectXFailed(primaryBuffer->QueryInterface(
00037 DirectSound3DListenerInterfaceID, (void**)&listener_))){
00038 ErrorOut("SoundListener::SoundListener() リスナの取得に失敗しました。");
00039 }
00040
00041 DS3DLISTENER param;
00042 param.dwSize = sizeof(DS3DLISTENER);
00043 if(DirectXFailed(listener_->GetAllParameters(¶m))){
00044 ErrorOut("SoundListener::SoundListener() "
00045 "パラメータの取得に失敗しました。");
00046 }
00047 position_.set(param.vPosition.x, param.vPosition.x, param.vPosition.z);
00048 velocity_.set(param.vVelocity.x, param.vVelocity.x, param.vVelocity.z);
00049 frontDirection_.set(param.vOrientFront.x,
00050 param.vOrientFront.y, param.vOrientFront.z);
00051 upDirection_.set(param.vOrientTop.x,
00052 param.vOrientTop.y, param.vOrientTop.z);
00053 rolloffFactor_ = param.flRolloffFactor;
00054 dopplerFactor_ = param.flDopplerFactor;
00055
00056 Assert(distanceFactor_ != SoundDefinition::distanceFactor);
00057 setDistanceFactor(SoundDefinition::distanceFactor);
00058 apply3DSettings();
00059 }
00060
00061
00062 SoundListener::~SoundListener(){
00063 SafeRelease(listener_);
00064 }
00065
00066
00067 void SoundListener::setPosition(const Vector3& position){
00068 if(position_ == position){ return; }
00069 if(DirectXFailed(listener_->SetPosition(
00070 -position.x, position.y, position.z, DS3D_DEFERRED))){
00071 ErrorOut("SoundListener::setPosition() 位置の設定に失敗しました。");
00072 }
00073 position_ = position;
00074 }
00075
00076
00077 void SoundListener::setVelocity(const Vector3& velocity){
00078 if(velocity_ == velocity){ return; }
00079 if(DirectXFailed(listener_->SetVelocity(
00080 -velocity.x, velocity.y, velocity.z, DS3D_DEFERRED))){
00081 ErrorOut("SoundListener::setVelocity() 速度の設定に失敗しました。");
00082 }
00083 velocity_ = velocity;
00084 }
00085
00086
00087 void SoundListener::setPositionAndVelocity(
00088 const Vector3& position, float millisecond){
00089 Assert(millisecond >= 0.f);
00090 if(millisecond < Math::epsilon){
00091 setVelocity(Vector3::zero);
00092 }else{
00093 Vector3 velocity = position - position_;
00094 velocity *= (1000.f / millisecond);
00095 setVelocity(velocity);
00096 }
00097 setPosition(position);
00098 }
00099
00100
00101 void SoundListener::setDirection(
00102 const Vector3& frontDirection, const Vector3& upDirection){
00103 if((frontDirection_ == frontDirection) &&
00104 (upDirection_ == upDirection)){ return; }
00105 Assert(!frontDirection.isZero());
00106 Assert(!upDirection.isZero());
00107 if(DirectXFailed(listener_->SetOrientation(
00108 -frontDirection.x, frontDirection.y, frontDirection.z,
00109 -upDirection.x, upDirection.y, upDirection.z, DS3D_DEFERRED))){
00110 ErrorOut("SoundListener::setDirection() 方向の設定に失敗しました。");
00111 }
00112 frontDirection_ = frontDirection;
00113 upDirection_ = upDirection;
00114 }
00115
00116
00117 void SoundListener::setDistanceFactor(float distanceFactor){
00118 Assert(distanceFactor > 0.f);
00119 if(distanceFactor_ == distanceFactor){ return; }
00120 if(DirectXFailed(listener_->SetDistanceFactor(
00121 distanceFactor, DS3D_DEFERRED))){
00122 ErrorOut("SoundListener::setDistanceFactor() "
00123 "距離係数の設定に失敗しました。");
00124 }
00125 distanceFactor_ = distanceFactor;
00126 }
00127
00128
00129 void SoundListener::setRolloffFactor(float rolloffFactor){
00130 Assert(DS3D_MAXROLLOFFFACTOR >= rolloffFactor);
00131 Assert(DS3D_MINROLLOFFFACTOR <= rolloffFactor);
00132 if(rolloffFactor_ == rolloffFactor){ return; }
00133 if(DirectXFailed(listener_->SetRolloffFactor(
00134 rolloffFactor, DS3D_DEFERRED))){
00135 ErrorOut("SoundListener::setRolloffFactor() "
00136 "ロールオフ係数の設定に失敗しました。");
00137 }
00138 rolloffFactor_ = rolloffFactor;
00139 }
00140
00141
00142 void SoundListener::setDopplerFactor(float dopplerFactor){
00143 Assert(DS3D_MAXDOPPLERFACTOR >= dopplerFactor);
00144 Assert(DS3D_MINDOPPLERFACTOR <= dopplerFactor);
00145 if(dopplerFactor_ == dopplerFactor){ return; }
00146 if(DirectXFailed(listener_->SetDopplerFactor(
00147 dopplerFactor, DS3D_DEFERRED))){
00148 ErrorOut("SoundListener::setDopplerFactor() "
00149 "ドップラー係数の設定に失敗しました。");
00150 }
00151 dopplerFactor_ = dopplerFactor;
00152 }
00153
00154
00155 String SoundListener::toString() const{
00156 String result, temp;
00157 temp.format("Position ( %.1f, %.1f, %.1f )\n",
00158 position_.x, position_.y, position_.z);
00159 result += temp;
00160 temp.format("Velocity ( %.1f, %.1f, %.1f )\n",
00161 velocity_.x, velocity_.y, velocity_.z);
00162 result += temp;
00163 temp.format("Direction Front( %.2f, %.2f, %.2f ) Up( %.2f, %.2f, %.2f )\n",
00164 frontDirection_.x, frontDirection_.y, frontDirection_.z,
00165 upDirection_.x, upDirection_.y, upDirection_.z);
00166 result += temp;
00167 temp.format("Distance %.2f Rolloff %.2f Doppler %.2f\n",
00168 distanceFactor_, rolloffFactor_, dopplerFactor_);
00169 result += temp;
00170 return result;
00171 }
00172
00173
00174 void SoundListener::apply3DSettings(){
00175 if(DirectXFailed(listener_->CommitDeferredSettings())){
00176 ErrorOut("SoundListener::apply3DSettings() "
00177 "パラメータの適用に失敗しました。");
00178 }
00179 }
00180
00181 }
00182