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

LampSound.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  * Lampサウンド実装
00022  * @author Junpee
00023  */
00024 
00025 #include "LampBasic.h"
00026 #include "Sound/System/LampSound.h"
00027 #include "Sound/System/SoundDefinition.h"
00028 #include "Sound/System/SoundCapacity.h"
00029 #include "Sound/System/SoundManager.h"
00030 #include "Sound/3D/SoundListener.h"
00031 
00032 namespace Lamp{
00033 
00034 // DirectSound
00035 DirectSound* LampSound::directSound_ = NULL;
00036 // プライマリバッファ
00037 DirectSoundPrimaryBuffer* LampSound::primaryBuffer_ = NULL;
00038 // サウンド能力
00039 SoundCapacity* LampSound::soundCapacity_ = NULL;
00040 // サウンドマネージャ
00041 SoundManager* LampSound::soundManager_ = NULL;
00042 // サウンドリスナ
00043 SoundListener* LampSound::soundListener_;
00044 // サンプル数
00045 int LampSound::sample_ = 0;
00046 // チャンネル数
00047 int LampSound::channel_ = 0;
00048 // ビット数
00049 int LampSound::bit_ = 0;
00050 // 初期化フラグ
00051 bool LampSound::isInitialized_ = false;
00052 
00053 //------------------------------------------------------------------------------
00054 // 初期化
00055 bool LampSound::initialize(HWND windowHandle){
00056     if(isInitialized_){ return true; }
00057     LampCore::initialize();
00058     // DirectSound作成
00059     if(DirectXFailed(DirectSoundCreate8(
00060         &DSDEVID_DefaultPlayback, &directSound_, NULL))){
00061         ErrorOut("LampSound::initialize() DirectSoundの作成に失敗しました");
00062         return false;
00063     }
00064     // 協調レベルの設定
00065     if(DirectXFailed(directSound_->SetCooperativeLevel(
00066         windowHandle, DSSCL_PRIORITY))){
00067         ErrorOut("LampSound::initialize() 協調レベルの設定に失敗しました");
00068         return false;
00069     }
00070     // サウンドメモリの再配置
00071     if(DirectXFailed(directSound_->Compact())){
00072         ErrorOut("LampSound::initialize() サウンドメモリの再配置に失敗しました");
00073         return false;
00074     }
00075     // サウンド能力の取得
00076     soundCapacity_ = new SoundCapacity();
00077     if(!soundCapacity_->initialize(directSound_)){
00078         ErrorOut("LampSound::initialize() サウンド能力の取得に失敗しました");
00079         return false;
00080     }
00081     // プライマリバッファの作成
00082     DSBUFFERDESC bufferDescription;
00083     ::memset(&bufferDescription, 0, sizeof(DSBUFFERDESC));
00084     bufferDescription.dwSize = sizeof(DSBUFFERDESC);
00085     // プライマリバッファ、3Dリスナ
00086     bufferDescription.dwFlags = (DSBCAPS_PRIMARYBUFFER | DSBCAPS_CTRL3D);
00087     if(DirectXFailed(directSound_->CreateSoundBuffer(
00088         &bufferDescription, &primaryBuffer_, NULL))){
00089         ErrorOut("LampSound::initialize() "
00090             "プライマリバッファの作成に失敗しました");
00091         return false;
00092     }
00093     // プライマリバッファのフォーマット指定
00094     WAVEFORMATEX waveFormat;
00095     ::memset(&waveFormat, 0, sizeof(WAVEFORMATEX));
00096     waveFormat.wFormatTag = WAVE_FORMAT_PCM;
00097     if(SoundDefinition::primaryBufferStereo){
00098         waveFormat.nChannels = 2;
00099     }else{
00100         waveFormat.nChannels = 1;
00101     }
00102     waveFormat.nSamplesPerSec = SoundDefinition::primaryBufferRate;
00103     waveFormat.wBitsPerSample = SoundDefinition::primaryBufferBit;
00104     waveFormat.nBlockAlign =
00105         (waveFormat.nChannels * waveFormat.wBitsPerSample) / 8;
00106     waveFormat.nAvgBytesPerSec =
00107         waveFormat.nSamplesPerSec * waveFormat.nBlockAlign;
00108     if(DirectXFailed(primaryBuffer_->SetFormat(&waveFormat))){
00109         ErrorOut("LampSound::initialize() "
00110             "プライマリバッファのフォーマット設定に失敗しました");
00111         return false;
00112     }
00113     // フォーマットの取得
00114     ::memset(&waveFormat, 0, sizeof(WAVEFORMATEX));
00115     if(DirectXFailed(primaryBuffer_->GetFormat(
00116         &waveFormat, sizeof(WAVEFORMATEX), NULL))){
00117         ErrorOut("LampSound::initialize() "
00118             "プライマリバッファのフォーマット取得に失敗しました");
00119         return false;
00120     }
00121     sample_ = waveFormat.nSamplesPerSec;
00122     channel_ = waveFormat.nChannels;
00123     bit_ = waveFormat.wBitsPerSample;
00124     // サウンドマネージャ初期化
00125     soundManager_ = new SoundManager(directSound_);
00126     // サウンドリスナの初期化
00127     soundListener_ = new SoundListener(primaryBuffer_);
00128     // 初期化フラグ立てる
00129     isInitialized_ = true;
00130     return true;
00131 }
00132 //------------------------------------------------------------------------------
00133 // 後始末
00134 void LampSound::finalize(){
00135     // サウンドリスナ解放
00136     SafeDelete(soundListener_);
00137     // サウンドマネージャ解放
00138     SafeDelete(soundManager_);
00139     // サウンド能力解放
00140     SafeDelete(soundCapacity_);
00141     // プライマリバッファ解放
00142     SafeRelease(primaryBuffer_);
00143     // DirectSound解放
00144     SafeRelease(directSound_);
00145     // 初期化フラグクリア
00146     isInitialized_ = false;
00147 }
00148 //------------------------------------------------------------------------------
00149 // プレゼンテーション
00150 void LampSound::presentation(){
00151     if(!isInitialized_){ return; }
00152     // サウンドの更新
00153     soundManager_->update();
00154     // 3D設定を適用する
00155     soundListener_->apply3DSettings();
00156 }
00157 //------------------------------------------------------------------------------
00158 // 文字列への変換
00159 String LampSound::toString(){
00160     Assert(isInitialized_);
00161     String result;
00162     result.format("PrimarySoundBuffer  %dHz %dChannel %dBit",
00163         getSample(), getChannel(), getBit());
00164     return result;
00165 }
00166 //------------------------------------------------------------------------------
00167 } // End of namespace Lamp
00168 //------------------------------------------------------------------------------

Generated on Wed Mar 16 10:29:31 2005 for Lamp by doxygen 1.3.2