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/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){
00125 return DSBVOLUME_MIN;
00126 }
00127
00128
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
00141
00142 float volume = Math::pow(10, (decibel / 33.22f / 100.f));
00143 return volume;
00144 }
00145
00146 }
00147