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

Sound.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/System/Sound.h"
00027 #include "Core/Utility/StringTokenizer.h"
00028 
00029 namespace Lamp{
00030 
00031 //------------------------------------------------------------------------------
00032 // 生成、破棄
00033 //------------------------------------------------------------------------------
00034 // コンストラクタ
00035 Sound::Sound(){
00036 }
00037 //------------------------------------------------------------------------------
00038 // デストラクタ
00039 Sound::~Sound(){
00040 }
00041 //------------------------------------------------------------------------------
00042 // リセット
00043 void Sound::reset(Reset flags){
00044     if((flags & resetName) != 0){ setName(""); }
00045     if((flags & resetCursor) != 0){ stop(); }
00046     if((flags & resetPriority) != 0){ setPriority(priorityDefault); }
00047     if((flags & resetLoop) != 0){ setLoop(false); }
00048     if((flags & resetLoopCursor) != 0){ setLoopCursor(0); }
00049     if((flags & resetVolume) != 0){ setVolume(1.f); }
00050     if((flags & resetFrequency) != 0){ setOriginalFrequency(); }
00051     if((flags & resetFade) != 0){ stopFade(); }
00052 }
00053 //------------------------------------------------------------------------------
00054 // コメントオプションの適用
00055 void Sound::applyCommentOption(){
00056     StringTokenizer tokenizer(getComment());
00057     while(tokenizer.hasMoreTokens()){
00058         String token = tokenizer.getNextToken();
00059         if(token == "-loop"){
00060             setLoop(true);
00061         }else if(token == "-loopCursor"){
00062             if(!tokenizer.hasMoreTokens()){ break; }
00063             String subToken = tokenizer.getNextToken();
00064             u_int cursor;
00065             if(!subToken.parseUInt(&cursor)){ continue; }
00066             setLoopCursor(cursor * getOneSampleBytes());
00067         }else if(token == "-loopTime"){
00068             if(!tokenizer.hasMoreTokens()){ break; }
00069             String subToken = tokenizer.getNextToken();
00070             float time;
00071             if(!subToken.parseFloat(&time)){ continue; }
00072             setLoopTime(time);
00073         }
00074     }
00075 }
00076 //------------------------------------------------------------------------------
00077 // 文字列への変換
00078 String Sound::toString() const{
00079     String result, temp;
00080     if(!getName().isEmpty()){ result += getName() + "  "; }
00081 
00082     State state = getState();
00083     if(state == statePlay){ result += "Play     "; }
00084     else if(state == stateSuspend){ result += "Suspend  "; }
00085     else if(state == stateStop){ result += "Stop     "; }
00086     else if(state == stateLost){ result += "Lost     "; }
00087 
00088     temp.format("Size(%7dbyte)\nLength(%6.2f/%6.2fsec)  ",
00089         getSize(), getCurrentTime(), getTimeLength());
00090     result += temp;
00091 
00092     temp.format("Sample(%2d)  Channel(%1d)  Bit(%2d)\n",
00093         getSample(), getChannel(), getBit());
00094     result += temp;
00095 
00096     if(isLoop()){
00097         temp.format("Loop(%6.2fsec)", getLoopTime());
00098         result += temp;
00099     }else{ result += "Once"; }
00100 
00101     temp.format("  Priority(%d)", getPriority());
00102     result += temp;
00103 
00104     Focus focus = getFocus();
00105     if(focus == focusNormal){
00106         result += "  Focus(Normal)\n";
00107     }else if(focus == focusSticky){
00108         result += "  Focus(Sticky)\n";
00109     }else if(focus == focusGlobal){
00110         result += "  Focus(Global)\n";
00111     }
00112 
00113     temp.format("Volume(%4.2f)  Frequency(%7dHz)\n",
00114         getVolume(), getFrequency());
00115     result += temp;
00116     if(!getComment().isEmpty()){ result += "Comment " + getComment() + "\n"; }
00117     return result;
00118 }
00119 //------------------------------------------------------------------------------
00120 // ボリュームデシベル変換
00121 int Sound::volumeToDecibel(float volume){
00122     Assert(volume >= 0.f);
00123     Assert(volume <= 1.f);
00124     if(volume <= 0.002f){// 0.002f以下はdbが0になる
00125         return DSBVOLUME_MIN;
00126     }
00127     // 10dbで人の耳に聞こえる音量が半分になるとする(エネルギーを半分にするなら6db)
00128     // 33.22 = -10db / log10(0.5f)、100.fはDirectSoundが1db = 100.fなので
00129     int db = (int)(33.22f * 100.f * Math::log10(volume));
00130     Assert(db <= DSBVOLUME_MAX);
00131     Assert(db >= DSBVOLUME_MIN);
00132     return db;
00133 }
00134 //------------------------------------------------------------------------------
00135 // デシベルボリューム変換
00136 float Sound::decibelToVolume(int decibel){
00137     Assert(decibel <= DSBVOLUME_MAX);
00138     Assert(decibel >= DSBVOLUME_MIN);
00139     if(decibel == DSBVOLUME_MIN){ return 0.f; }
00140     // 10dbで人の耳に聞こえる音量が半分になるとする(エネルギーを半分にするなら6db)
00141     // 33.22 = -10db / log10(0.5f)、100.fはDirectSoundが1db = 100.fなので
00142     float volume = Math::pow(10, (decibel / 33.22f / 100.f));
00143     return volume;
00144 }
00145 //------------------------------------------------------------------------------
00146 } // End of namespace Lamp
00147 //------------------------------------------------------------------------------

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