Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

# SoundListener.cpp

Go to the documentation of this file.

    00001 //------------------------------------------------------------------------------
    00002 // Lamp : Open source game middleware
    00003 // Copyright (C) 2004  Junpei Ohtani ( Email : junpee@users.sourceforge.jp )
    00004 //
    00005 // This library is free software; you can redistribute it and/or
    00006 // modify it under the terms of the GNU Lesser General Public
    00007 // License as published by the Free Software Foundation; either
    00008 // version 2.1 of the License, or (at your option) any later version.
    00009 //
    00010 // This library is distributed in the hope that it will be useful,
    00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00013 // Lesser General Public License for more details.
    00014 //
    00015 // You should have received a copy of the GNU Lesser General Public
    00016 // License along with this library; if not, write to the Free Software
    00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    00018 //------------------------------------------------------------------------------
    00019
    00020 /** @file
    00021  * 実装
    00022  * @author Junpee
    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(&param))){
    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 // 3D設定の適用
    00174 void SoundListener::apply3DSettings(){
    00175     if(DirectXFailed(listener_->CommitDeferredSettings())){
    00176         ErrorOut("SoundListener::apply3DSettings() "
    00177             "パラメータの適用に失敗しました。");
    00178     }
    00179 }
    00180 //------------------------------------------------------------------------------
    00181 } // End of namespace Lamp
    00182 //------------------------------------------------------------------------------