diff --git a/inc/hgl/audio/AudioBuffer.h b/inc/hgl/audio/AudioBuffer.h index 7cc2f46..cadee57 100644 --- a/inc/hgl/audio/AudioBuffer.h +++ b/inc/hgl/audio/AudioBuffer.h @@ -3,7 +3,7 @@ #include // #include -#include +//#include #include namespace hgl @@ -30,7 +30,7 @@ namespace hgl AudioBuffer(void *,int,AudioFileType); ///<本类构造函数 AudioBuffer(InputStream *,int,AudioFileType); ///<本类构造函数 - AudioBuffer(const os_char *filename=0,AudioFileType=aftNone); ///<本类构造函数 + AudioBuffer(const os_char *filename=0,AudioFileType=AudioFileType::None); ///<本类构造函数 // AudioBuffer(HAC *,const os_char *,AudioFileType=aftNone); ///<本类构造函数 virtual ~AudioBuffer(); ///<本类析构函数 @@ -38,7 +38,7 @@ namespace hgl bool Load(void *,int,AudioFileType); ///<从内存中加载音频数据 bool Load(InputStream *,int,AudioFileType); ///<从流中加载音频数据 - bool Load(const os_char *,AudioFileType=aftNone); ///<从文件中加载音频数据 + bool Load(const os_char *,AudioFileType=AudioFileType::None); ///<从文件中加载音频数据 // bool Load(HAC *,const os_char *,AudioFileType=aftNone); ///<从HAC包的文件中加载音频数据 void Clear(); ///<清除数据 diff --git a/inc/hgl/audio/AudioFileType.h b/inc/hgl/audio/AudioFileType.h index a971e2e..36fdc98 100644 --- a/inc/hgl/audio/AudioFileType.h +++ b/inc/hgl/audio/AudioFileType.h @@ -2,20 +2,21 @@ #define HGL_AUDIO_FILE_TYPE_INCLUDE #include +#include namespace hgl { /** * 音频文件格式 */ - enum AudioFileType + enum class AudioFileType { - aftNone=0, ///<起始定义,如使用表示自动根据扩展名识别 + None=0, ///<起始定义,如使用表示自动根据扩展名识别 - aftWAV, /// #include #include -#include +#include using namespace openal; namespace hgl { @@ -17,6 +17,14 @@ namespace hgl struct AudioPlugInInterface; + enum class PlayState //播放器状态 + { + None=0, + Play, + Pause, + Exit + }; + /** * 使用AudioPlayer创建的音频播放器类,一般用于背景音乐等独占的音频处理。 * 这个音频播放器将使用一个单独的线程,在播放器被删除时线程也会被关闭。 @@ -25,35 +33,91 @@ namespace hgl { ThreadMutex lock; - public: + protected: - enum PlayState //播放器状态 + ALbyte *audio_data; + int audio_data_size; + + void *audio_ptr; ///<音频数据指针 + + char *audio_buffer; + int audio_buffer_size; + uint audio_buffer_count; ///<播放数据计数 + + AudioPlugInInterface *decode; + + ALenum format; ///<音频数据格式 + ALsizei rate; ///<音频数据采样率 + + struct { - psNone=0, - psPlay, - psPause, - psExit - }; + bool open; + uint64 time; + float gap; - #include + struct + { + float gain; + double time; + }start,end; + }auto_gain; ///<自动增益 + + bool ReadData(ALuint); + bool UpdateBuffer(); + void ClearBuffer(); + + bool Playback(); + + bool IsExitDelete()const override{return false;} + bool Execute() override; + + void InitPrivate(); + bool Load(AudioFileType); + + protected: + + volatile bool loop; + volatile PlayState ps; + + AudioSource audiosource; + ALuint source; + ALuint buffer[3]; + double total_time; + double wait_time; + + double gain; + + double start_time; + + double fade_in_time; + double fade_out_time; public: //属性 - Property Index; ///<音源索引 - Property Time; ///<音频总时间 + uint GetIndex()const{return audiosource.index;} ///<获取音源索引 - Property State; ///<播放器状态 - Property SourceState; ///<音源状态 - Property MinGain; ///<最小增益 - Property MaxGain; ///<最大增益 + double GetTotalTime()const{return total_time;} ///<获取音频总时长 - Property Loop; ///<是否循环播放虚拟变量 + PlayState GetPlayState()const{return ps;} ///<获取播放器状态 - Property Pitch; ///<播放频率 - Property Gain; ///<音量增益幅度 - Property ConeGain; ///< + int GetSourceState()const{return audiosource.GetState();} ///<获取音源索引 - Property RolloffFactor; ///< + bool IsLoop(); ///<是否循环播放 + void SetLoop(bool); ///<设置循环播放 + + float GetGain()const{return audiosource.gain;} ///<获取音量增益 + float GetMinGain()const{return audiosource.GetMinGain();} ///<获取音量最小增益 + float GetMaxGain()const{return audiosource.GetMaxGain();} ///<获取音量最大增益 + float GetConeGain()const{return audiosource.cone_gain;} ///<获取音量锥形增益 + + void SetGain(float val){audiosource.SetGain(val);} ///<设置音量增益 + void SetConeGain(float val){audiosource.SetConeGain(val);} ///<设置音量锥形增益 + + float GetPitch()const{return audiosource.pitch;} ///<获取播放频率 + void SetPitch(float val){audiosource.SetPitch(val);} ///<设置播放频率 + + float GetRolloffFactor()const{return audiosource.rolloff_factor;} ///<获取Rolloff因子 + void SetRolloffFactor(float f){audiosource.SetRolloffFactor(f);} ///<设置Rolloff因子 public: //属性方法 @@ -76,13 +140,13 @@ namespace hgl AudioPlayer(); AudioPlayer(io::InputStream *,int,AudioFileType); - AudioPlayer(const os_char *,AudioFileType=aftNone); -// AudioPlayer(HAC *,const os_char *,AudioFileType=aftNone); + AudioPlayer(const os_char *,AudioFileType=AudioFileType::None); +// AudioPlayer(HAC *,const os_char *,AudioFileType=AudioFileType::None); virtual ~AudioPlayer(); virtual bool Load(io::InputStream *,int,AudioFileType); ///<从流中加载一个音频文件 - virtual bool Load(const os_char *,AudioFileType=aftNone); ///<加载一个音频文件 -// virtual bool Load(HAC *,const os_char *,AudioFileType=aftNone); ///<从HAC包中加载一个音频文件 + virtual bool Load(const os_char *,AudioFileType=AudioFileType::None); ///<加载一个音频文件 +// virtual bool Load(HAC *,const os_char *,AudioFileType=AudioFileType::None); ///<从HAC包中加载一个音频文件 virtual void Play(bool=true); ///<播放音频 virtual void Stop(); ///<停止播放 @@ -93,7 +157,7 @@ namespace hgl virtual double GetPlayTime(); ///<取得已播放时间(单位秒) virtual void SetFadeTime(double,double); ///<设置淡入淡出时间 - virtual void AutoGain(float,double); ///<自动音量 + virtual void AutoGain(float,double,const double cur_time); ///<自动音量 };//class AudioPlayer }//namespace hgl #endif//HGL_AUDIO_PLAYER_INCLUDE diff --git a/inc/hgl/audio/AudioScene.h b/inc/hgl/audio/AudioScene.h index da9e9f7..fea6d49 100644 --- a/inc/hgl/audio/AudioScene.h +++ b/inc/hgl/audio/AudioScene.h @@ -1,7 +1,7 @@ #ifndef HGL_AUDIO_SCENE_INCLUDE #define HGL_AUDIO_SCENE_INCLUDE -#include +#include #include #include #include @@ -119,7 +119,7 @@ namespace hgl AudioListener *listener; ///<收聽者 ObjectPool source_pool; ///<音源数据池 - Set source_list; ///<音源列表 + SortedSets source_list; ///<音源列表 protected: diff --git a/inc/hgl/audio/AudioSource.Attrib.h b/inc/hgl/audio/AudioSource.Attrib.h deleted file mode 100644 index ca0cdad..0000000 --- a/inc/hgl/audio/AudioSource.Attrib.h +++ /dev/null @@ -1,67 +0,0 @@ - friend class AudioPlayer; - -private: - - void InitPrivate(); - - AudioBuffer *Buffer; - -protected: - - uint index; - - bool pause; - - bool loop; - float pitch; - float gain; - float cone_gain; - Vector3f position; - Vector3f velocity; - Vector3f direction; - float ref_dist,max_dist; - uint distance_model; - float rolloff_factor; - ConeAngle angle; - - float doppler_factor; - float doppler_velocity; - -public: - - uint GetIndex()const{return index;} - - double GetCurTime(); - void SetCurTime(const double &); - - int GetState(); - float GetMinGain(); - float GetMaxGain(); - - const bool GetLoop()const{return loop;} - - const float GetPitch()const{return pitch;} - const float GetGain()const{return gain;} - const float GetConeGain()const{return cone_gain;} - - const uint GetDistanceModel()const{return distance_model;} - const float GetRolloffFactor()const{return rolloff_factor;} - - const void GetDistance(float &rd,float &md)const - { - rd=ref_dist; - md=max_dist; - } - - void GetDoppler(float &factor,float &velocity) - { - factor=doppler_factor; - velocity=doppler_velocity; - } - - virtual void SetLoop(bool); - void SetPitch(float); - void SetGain(float); - void SetConeGain(float); - void SetDistanceModel(uint); - void SetRolloffFactor(float); diff --git a/inc/hgl/audio/AudioSource.h b/inc/hgl/audio/AudioSource.h index 7c014c4..1747479 100644 --- a/inc/hgl/audio/AudioSource.h +++ b/inc/hgl/audio/AudioSource.h @@ -3,6 +3,8 @@ #include #include +#include +#include namespace hgl { @@ -13,41 +15,95 @@ namespace hgl */ class AudioSource ///音频源类 { - #include + friend class AudioPlayer; + + private: + + void InitPrivate(); + + AudioBuffer *Buffer; + + protected: + + uint index; + + bool pause; + + bool loop; + float pitch; + float gain; + float cone_gain; + Vector3f position; + Vector3f velocity; + Vector3f direction; + float ref_dist,max_dist; + uint distance_model; + float rolloff_factor; + ConeAngle angle; + + float doppler_factor; + float doppler_velocity; public: //属性 - Property Index; + uint GetIndex()const{return index;} ///<获取当前音源索引 + int GetState()const; ///<获取当前音源状态 - Property CurTime; ///<当前播放到的时间 + const bool IsNone ()const{return GetState()==AL_NONE; } + const bool IsStopped ()const{return GetState()==AL_STOPPED;} + const bool IsPaused ()const{return GetState()==AL_PAUSED;} + const bool IsPlaying ()const{return GetState()==AL_PLAYING;} - Property State; ///<音源状态 - Property MinGain; ///<最小增益 - Property MaxGain; ///<最大增益 + double GetCurTime()const; ///<获取当前播放到的时间 + void SetCurTime(const double &); ///<设置当前播放时间 - Property Loop; ///<是否循环播放虚拟变量 + float GetMinGain()const; ///<获取最小增益 + float GetMaxGain()const; ///<获取最大增益 - Property Pitch; ///<播放频率 - Property Gain; ///<音量增益幅度 - Property ConeGain; ///<音源锥增益 + const bool IsLoop()const{return loop;} ///<是否循环播放 + virtual void SetLoop(bool); ///<设置是否循环播放 - Property DistanceModel; ///<距离衰减模型(默认钳位倒数距离模型) - Property RolloffFactor; ///<音源衰减因(>=0,默认1.0) + const float GetPitch()const{return pitch;} ///<获取播放频率 + void SetPitch(float); ///<设置播放频率 - public: //属性方法 + const float GetGain()const{return gain;} ///<获取音量增益幅度 + void SetGain(float); ///<设置音量增益幅度 + const float GetConeGain()const{return cone_gain;} ///<获取锥形音量增益幅度 + void SetConeGain(float); ///<设置锥形音量增益幅度 - const Vector3f &GetPosition(){return position;} - const Vector3f &GetVelocity(){return velocity;} - const Vector3f &GetDirection(){return direction;} - const ConeAngle &GetAngle(){return angle;} + const uint GetDistanceModel()const{return distance_model;} ///<获取音量距离衰减模型 + void SetDistanceModel(uint); ///<设置音量距离衰减模型 - void SetPosition(const Vector3f &); - void SetVelocity(const Vector3f &); - void SetDirection(const Vector3f &); - void SetDistance(const float &ref_distance,const float &max_distance); - void SetConeAngle(const ConeAngle &); - void SetDopplerFactor(const float &); ///<设置多普勒效果强度 - void SetDopplerVelocity(const float &); ///<设置多普勒速度 + const float GetRolloffFactor()const{return rolloff_factor;} ///<获取音量衰减因子 + void SetRolloffFactor(float); ///<设置音量衰减因子(>=0,默认1.0) + + void GetDoppler(float &factor,float &velocity)const ///<获取多普勒强度和速度 + { + factor=doppler_factor; + velocity=doppler_velocity; + } + + void SetDopplerFactor(const float &); ///<设置多普勒效果强度 + void SetDopplerVelocity(const float &); ///<设置多普勒速度 + + const void GetDistance(float &rd,float &md)const ///<获取音源距离范围 + { + rd=ref_dist; + md=max_dist; + } + void SetDistance(const float &ref_distance,const float &max_distance); ///<设置音源距离范围 + + const Vector3f &GetPosition()const{return position;} ///<获取音源位置 + void SetPosition(const Vector3f &); ///<设置音源位置 + + const Vector3f &GetVelocity()const{return velocity;} ///<获取速度 + void SetVelocity(const Vector3f &); ///<设置速度 + + const Vector3f &GetDirection()const{return direction;} ///<获取发声方向 + void SetDirection(const Vector3f &); ///<设置发声方向 + + const ConeAngle &GetAngle()const{return angle;} ///<获取发声锥形角度 + void SetConeAngle(const ConeAngle &); ///<设置发声锥形角度 public: //方法 @@ -65,8 +121,8 @@ namespace hgl virtual bool Create(); ///<创建音源 virtual void Close(); ///<关闭音源 - bool Link(AudioBuffer *); ///<绑定一个音频缓冲区 - void Unlink(); ///<解除绑定 + bool Link(AudioBuffer *); ///<绑定一个音频缓冲区 + void Unlink(); ///<解除绑定 };//class AudioSource }//namespace hgl #endif//HGL_AUDIO_SOURCE_INCLUDE diff --git a/inc/hgl/audio/Listener.h b/inc/hgl/audio/Listener.h index 09c8a51..7bfd18e 100644 --- a/inc/hgl/audio/Listener.h +++ b/inc/hgl/audio/Listener.h @@ -1,7 +1,7 @@ #ifndef HGL_LISTENER_INCLUDE #define HGL_LISTENER_INCLUDE -#include +#include namespace hgl { /** diff --git a/src/AudioBuffer.cpp b/src/AudioBuffer.cpp index 4c6aa53..65c6ddc 100644 --- a/src/AudioBuffer.cpp +++ b/src/AudioBuffer.cpp @@ -136,9 +136,9 @@ namespace hgl if(alLastError())return(ok=false); - if(aft<=aftNone||aft>=aftEnd) + if(!RangeCheck(aft)) { - LOG_ERROR(OS_TEXT("未知的音频文件类型!AudioFileType:")+OSString(aft)); + LOG_ERROR(OS_TEXT("未知的音频文件类型!AudioFileType:")+OSString::valueOf((int)aft)); alDeleteBuffers(1,&Index); RETURN_FALSE; } @@ -168,10 +168,10 @@ namespace hgl if(!alGenBuffers)RETURN_FALSE; if(!in)RETURN_FALSE; if(size<=0)RETURN_FALSE; - - if(aft<=aftNone||aft>=aftEnd) + + if(!RangeCheck(aft)) { - LOG_ERROR(OS_TEXT("未知的音频文件类型!AudioFileType:")+OSString(aft)); + LOG_ERROR(OS_TEXT("未知的音频文件类型!AudioFileType:")+OSString::valueOf((int)aft)); ok=false; } else @@ -198,8 +198,8 @@ namespace hgl if(!alGenBuffers)RETURN_FALSE; aft=CheckAudioFileType(filename); - - if(!aft) + + if(!RangeCheck(aft)) { LOG_ERROR(OS_TEXT("未知的音频文件类型!AudioFile: ")+OSString(filename)); ok=false; diff --git a/src/AudioDecode.cpp b/src/AudioDecode.cpp index 7d505d9..648ac2f 100644 --- a/src/AudioDecode.cpp +++ b/src/AudioDecode.cpp @@ -4,13 +4,12 @@ using namespace openal; namespace hgl { - /*PlugInManage( AudioInterface, //管理器名称,仅此一次,所以随便命名 - AudioPlugInInterface, //接口结构名称,来自外部 - AudioInterfaceCheck, //检查接口函数名称,要对外用 - u"Audio", //插件前缀 - 2); //当前所用版本 - */ + //PlugInManage( AudioInterface, //管理器名称,仅此一次,所以随便命名 + // AudioPlugInInterface, //接口结构名称,来自外部 + // AudioInterfaceCheck, //检查接口函数名称,要对外用 + // u"Audio", //插件前缀 + // 2); //当前所用版本 - PlugInManage(Audio,"Audio",2); - PlugInManage(AudioFloat,"Audio",3); + //PlugInManage(Audio,"Audio",2); + //PlugInManage(AudioFloat,"Audio",3); }//namespace hgl diff --git a/src/AudioDecode.h b/src/AudioDecode.h index f060d14..0603789 100644 --- a/src/AudioDecode.h +++ b/src/AudioDecode.h @@ -2,7 +2,7 @@ #define HGL_AUDIO_DECODE_INCLUDE #include -#include +#include using namespace openal; namespace hgl diff --git a/src/AudioFileType.cpp b/src/AudioFileType.cpp index 645d7fd..659b180 100644 --- a/src/AudioFileType.cpp +++ b/src/AudioFileType.cpp @@ -1,5 +1,6 @@ #include #include +#include namespace hgl { @@ -11,17 +12,17 @@ namespace hgl const AudioFormatExt audio_format_ext_name[]= { - {OS_TEXT("wav"), aftWAV }, - {OS_TEXT("ogg"), aftVorbis }, - {OS_TEXT("opus"), aftOpus }, - {OS_TEXT(""), aftNone } + {OS_TEXT("wav"), AudioFileType::Wav }, + {OS_TEXT("ogg"), AudioFileType::Vorbis }, + {OS_TEXT("opus"), AudioFileType::Opus }, + {OS_TEXT(""), AudioFileType::None } }; AudioFileType CheckAudioExtName(const os_char *ext_name) { auto *afp=audio_format_ext_name; - while(afp->type) + while(afp->type!=AudioFileType::None) { if(hgl::strcmp(ext_name,afp->name)==0) return(afp->type); @@ -29,7 +30,7 @@ namespace hgl ++afp; } - return(aftNone); + return(AudioFileType::None); } AudioFileType CheckAudioFileType(const os_char *filename) @@ -40,7 +41,7 @@ namespace hgl ext=hgl::strrchr(filename,hgl::strlen(filename),'.'); if(!ext) - return(aftNone); + return(AudioFileType::None); ++ext; @@ -50,9 +51,8 @@ namespace hgl return CheckAudioExtName(extname); } - const os_char audio_decode_name[aftEnd][32]= + const os_char audio_decode_name[size_t(AudioFileType::RANGE_SIZE)][32]= { - OS_TEXT(""), OS_TEXT("Wav"), OS_TEXT("Vorbis"), OS_TEXT("Opus") @@ -60,8 +60,8 @@ namespace hgl const os_char *GetAudioDecodeName(const AudioFileType aft) { - if(aft<=aftNone||aft>=aftEnd)return(nullptr); + if(!RangeCheck(aft))return(nullptr); - return audio_decode_name[aft]; + return audio_decode_name[(size_t)aft-(size_t)AudioFileType::BEGIN_RANGE]; } }//namespace hgl diff --git a/src/AudioManage.cpp b/src/AudioManage.cpp index 735c066..896db20 100644 --- a/src/AudioManage.cpp +++ b/src/AudioManage.cpp @@ -25,7 +25,7 @@ namespace hgl { if(!buffer)return(true); - if(source->State==AL_PLAYING)return(false); + if(source->IsPlaying())return(false); source->Unlink(); @@ -49,7 +49,7 @@ namespace hgl buffer=new AudioBuffer(filename); source->Link(buffer); - source->Gain=gain; + source->SetGain(gain); source->Play(false); } }//namespace hgl diff --git a/src/AudioPlayer.cpp b/src/AudioPlayer.cpp index e7a6401..0a93c12 100644 --- a/src/AudioPlayer.cpp +++ b/src/AudioPlayer.cpp @@ -1,8 +1,9 @@ #include -#include -#include +#include +#include #include #include +#include #include"AudioDecode.h" namespace hgl @@ -29,29 +30,13 @@ namespace hgl decode=nullptr; - ps=psNone; + ps=PlayState::None; - { - hglSetPropertyRead( Index, this,AudioPlayer::GetIndex ); - hglSetPropertyRead( Time, this,AudioPlayer::GetTime ); - - hglSetPropertyRead( State, this,AudioPlayer::GetPlayState ); - hglSetPropertyRead( SourceState, this,AudioPlayer::GetSourceState); - hglSetPropertyRead( MinGain, this,AudioPlayer::GetMinGain ); - hglSetPropertyRead( MaxGain, this,AudioPlayer::GetMaxGain ); - - hglSetProperty( Loop, this,AudioPlayer::GetLoop, AudioPlayer::SetLoop ); - - hglSetProperty( Pitch, this,AudioPlayer::GetPitch, AudioPlayer::SetPitch ); - hglSetProperty( Gain, this,AudioPlayer::GetGain, AudioPlayer::SetGain ); - hglSetProperty( ConeGain, this,AudioPlayer::GetConeGain, AudioPlayer::SetConeGain ); - - hglSetProperty( RolloffFactor, this,AudioPlayer::GetRolloffFactor, AudioPlayer::SetRolloffFactor ); - } + total_time=0; if(!audiosource.Create())return; - audiosource.Loop=false; + audiosource.SetLoop(false); source=audiosource.index; @@ -106,7 +91,7 @@ namespace hgl if(decode) { - audio_ptr=decode->Open(audio_data,audio_data_size,&format,&rate,&time); + audio_ptr=decode->Open(audio_data,audio_data_size,&format,&rate,&total_time); audio_buffer_size=(AudioTime(format,rate)+9)/10; // 1/10 秒 @@ -117,8 +102,8 @@ namespace hgl wait_time=0.1; - if(wait_time>time/3.0f) - wait_time=time/10.0f; + if(wait_time>total_time/3.0f) + wait_time=total_time/10.0f; return(true); } @@ -140,9 +125,9 @@ namespace hgl Clear(); - if(aft<=aftNone||aft>=aftEnd) + if(!RangeCheck(aft)) { - LOG_ERROR(OS_TEXT("未支持的音频文件类型!AudioFileType: ")+OSString(aft)); + LOG_ERROR(OS_TEXT("未支持的音频文件类型!AudioFileType: ")+OSString::valueOf((int)aft)); return(false); } @@ -168,10 +153,10 @@ namespace hgl if(!alGenBuffers)return(false); if(!filename||!(*filename))return(false); - if(aft<=aftNone||aft>=aftEnd) + if(!RangeCheck(aft)) aft=CheckAudioFileType(filename); - if(aft<=aftNone||aft>=aftEnd) + if(!RangeCheck(aft)) { LOG_ERROR(OS_TEXT("未知的音频文件类型!AudioFile: ")+OSString(filename)); return(false); @@ -236,10 +221,10 @@ namespace hgl audio_ptr=nullptr; - time=0; + total_time=0; } - bool AudioPlayer::GetLoop() + bool AudioPlayer::IsLoop() { lock.Lock(); bool rv=loop; @@ -299,12 +284,12 @@ namespace hgl alSourcePlay(source); start_time=GetDoubleTime(); - ps=psPlay; + ps=PlayState::Play; return(true); } else { - ps=psExit; + ps=PlayState::Exit; return(false); } @@ -322,7 +307,7 @@ namespace hgl loop=_loop; - if(ps==psNone||ps==psPause) //未启动线程 + if(ps==PlayState::None||ps==PlayState::Pause) //未启动线程 Start(); Playback(); //Execute执行有检测Lock,所以不必担心该操作会引起线程冲突 @@ -342,7 +327,7 @@ namespace hgl lock.Lock(); if(Thread::IsLive()) - ps=psExit; + ps=PlayState::Exit; else thread_is_live=false; @@ -351,7 +336,7 @@ namespace hgl if(thread_is_live) Thread::WaitExit(); - ps=psNone; + ps=PlayState::None; } /** @@ -363,8 +348,8 @@ namespace hgl lock.Lock(); - if(ps==psPlay) - ps=psPause; + if(ps==PlayState::Play) + ps=PlayState::Pause; lock.Unlock(); } @@ -378,9 +363,9 @@ namespace hgl lock.Lock(); - if(ps==psPause) + if(ps==PlayState::Pause) { - ps=psPlay; + ps=PlayState::Play; Thread::Start(); } @@ -404,9 +389,9 @@ namespace hgl audiosource.SetGain(((cur_time-start_time)/fade_in_time)*gain); } else - if(cur_time-start_time>time-fade_out_time) //淡出时间 + if(cur_time-start_time>total_time-fade_out_time) //淡出时间 { - audiosource.SetGain(((time-(cur_time-start_time))/fade_out_time)*gain); + audiosource.SetGain(((total_time-(cur_time-start_time))/fade_out_time)*gain); } if(auto_gain.open) @@ -415,14 +400,14 @@ namespace hgl { auto_gain.open=false; - Gain=auto_gain.end.gain; + SetGain(auto_gain.end.gain); if(auto_gain.end.gain<=0) - ps=psExit; + ps=PlayState::Exit; } else { - Gain=auto_gain.start.gain+auto_gain.gap*((cur_time-auto_gain.start.time)/auto_gain.time); + SetGain(auto_gain.start.gain+auto_gain.gap*((cur_time-auto_gain.start.time)/auto_gain.time)); } } @@ -468,13 +453,13 @@ namespace hgl { lock.Lock(); - if(ps==psPlay) //被要求播放 + if(ps==PlayState::Play) //被要求播放 { if(!UpdateBuffer()) { if(loop) //被要求循环播放 { - if(SourceState!=AL_STOPPED) //等它放完 + if(GetSourceState()!=AL_STOPPED) //等它放完 Playback(); } else @@ -482,18 +467,18 @@ namespace hgl //退出 lock.Unlock(); - ps=psNone; + ps=PlayState::None; return(false); } } else { - if(SourceState!=AL_PLAYING) + if(GetSourceState()!=AL_PLAYING) alSourcePlay(source); } } else - if(ps==psPause) //被要求暂停 + if(ps==PlayState::Pause) //被要求暂停 { alSourcePause(source); @@ -501,7 +486,7 @@ namespace hgl return(false); } else - if(ps==psExit) //被要求暂停或退出 + if(ps==PlayState::Exit) //被要求暂停或退出 { alSourceStop(source); alSourcei(source,AL_BUFFER,0); @@ -535,14 +520,17 @@ namespace hgl /** * 自动调整增益 + * @param target_gain 目标增益 + * @param adjust_time 到达目标增益要经过的时间 + * @param cur_time 当前时间 */ - void AudioPlayer::AutoGain(float target_gain,double adjust_time) + void AudioPlayer::AutoGain(float target_gain,double adjust_time,const double cur_time) { if(!audio_data)return; lock.Lock(); - auto_gain.start.gain=Gain; - auto_gain.start.time=GetDoubleTime(); + auto_gain.start.gain=GetGain(); + auto_gain.start.time=cur_time; auto_gain.end.gain=target_gain; auto_gain.end.time=auto_gain.start.time+adjust_time; diff --git a/src/AudioScene.cpp b/src/AudioScene.cpp index b895f69..368e616 100644 --- a/src/AudioScene.cpp +++ b/src/AudioScene.cpp @@ -169,16 +169,15 @@ namespace hgl if(!asi->source) { - asi->source=source_pool.Acquire(); - - if(!asi->source)return(false); + if(!source_pool.Acquire(asi->source)) + return(false); } asi->source->Link(asi->buffer); - asi->source->Gain=asi->gain; - asi->source->DistanceModel=asi->distance_model; - asi->source->RolloffFactor=asi->rolloff_factor; + asi->source->SetGain(asi->gain); + asi->source->SetDistanceModel(asi->distance_model); + asi->source->SetRolloffFactor(asi->rolloff_factor); asi->source->SetDistance(asi->ref_distance,asi->max_distance); asi->source->SetPosition(asi->cur_pos); asi->source->SetConeAngle(asi->cone_angle); @@ -187,7 +186,7 @@ namespace hgl asi->source->SetDopplerFactor(asi->doppler_factor); asi->source->SetDopplerVelocity(0); - asi->source->CurTime=time_off; + asi->source->SetCurTime(time_off); asi->source->Play(asi->loop); OnToHear(asi); @@ -200,7 +199,7 @@ namespace hgl if(!asi)return(false); if(!asi->source)return(false); - if(asi->source->State==AL_STOPPED) //停播状态 + if(asi->source->GetState()==AL_STOPPED) //停播状态 { if(!asi->loop) //不是循环播放 { diff --git a/src/AudioSource.cpp b/src/AudioSource.cpp index 568d7e7..61cfccb 100644 --- a/src/AudioSource.cpp +++ b/src/AudioSource.cpp @@ -1,6 +1,6 @@ #include #include -#include +#include using namespace openal; namespace hgl @@ -21,23 +21,6 @@ namespace hgl { index=InvalidIndex; Buffer=nullptr; - - hglSetPropertyRead( Index, this,AudioSource::GetIndex); - - hglSetProperty( CurTime, this,AudioSource::GetCurTime, AudioSource::SetCurTime); - - hglSetPropertyRead( State, this,AudioSource::GetState); - hglSetPropertyRead( MinGain, this,AudioSource::GetMinGain); - hglSetPropertyRead( MaxGain, this,AudioSource::GetMaxGain); - - hglSetProperty( Loop, this,AudioSource::GetLoop, AudioSource::SetLoop); - - hglSetProperty( Pitch, this,AudioSource::GetPitch, AudioSource::SetPitch); - hglSetProperty( Gain, this,AudioSource::GetGain, AudioSource::SetGain); - hglSetProperty( ConeGain, this,AudioSource::GetConeGain, AudioSource::SetConeGain); - - hglSetProperty( DistanceModel, this,AudioSource::GetDistanceModel, AudioSource::SetDistanceModel); - hglSetProperty( RolloffFactor, this,AudioSource::GetRolloffFactor, AudioSource::SetRolloffFactor); } /** @@ -69,7 +52,7 @@ namespace hgl Close(); } - double AudioSource::GetCurTime() + double AudioSource::GetCurTime() const { if(!alGetSourcei)return(0); if(index==InvalidIndex)return(0); @@ -89,7 +72,7 @@ namespace hgl alSourcef(index,AL_SEC_OFFSET,ct); } - int AudioSource::GetState() + int AudioSource::GetState() const { if(!alGetSourcei)return(0); if(index==InvalidIndex)return(AL_NONE); @@ -101,7 +84,7 @@ namespace hgl return(state); } - float AudioSource::GetMinGain() + float AudioSource::GetMinGain() const { if(!alGetSourcef)return(0); if(index==InvalidIndex)return(0); @@ -113,7 +96,7 @@ namespace hgl return(min); } - float AudioSource::GetMaxGain() + float AudioSource::GetMaxGain() const { if(!alGetSourcef)return(0); if(index==InvalidIndex)return(0); @@ -267,7 +250,7 @@ namespace hgl if(!Buffer ||Buffer->Time<=0)return(false); - if(State==AL_PLAYING) + if(IsPlaying()) alSourceStop(index); alSourcePlay(index); @@ -288,7 +271,7 @@ namespace hgl if(!Buffer ||Buffer->Time<=0)return(false); - if(State==AL_PLAYING) + if(IsPlaying()) alSourceStop(index); alSourcePlay(index); diff --git a/src/Listener.cpp b/src/Listener.cpp index cfdef54..b5f2308 100644 --- a/src/Listener.cpp +++ b/src/Listener.cpp @@ -1,6 +1,6 @@ #include #include -#include +#include using namespace openal; namespace hgl diff --git a/src/OpenAL.cpp b/src/OpenAL.cpp index 38f2d1b..8962b9a 100644 --- a/src/OpenAL.cpp +++ b/src/OpenAL.cpp @@ -3,7 +3,7 @@ #include #include #include -#include +#include #include #include @@ -37,90 +37,88 @@ namespace openal void PutOpenALInfo(); //-------------------------------------------------------------------------------------------------- + bool OnFindedOpenALDynamicLinkLibrary(const OSString &filename,void *user_data,bool exist) + { + if(!exist)return(true); + + AudioEM=LoadExternalModule(filename); + + if(!AudioEM) + { + LOG_INFO(OS_TEXT("找到一个OpenAL动态链接库,但加载失败!文件名: ")+OSString(filename)); + return(true); + } + + LOG_INFO(OS_TEXT("加载OpenAL动态链接库功成。文件名: ")+OSString(filename)); + + return(false); + } + //-------------------------------------------------------------------------------------------------- /** * 加载OpenAL * @return 加载OpenAL是否成功 */ bool LoadOpenAL(const os_char *filename) { - const os_char oalfn[][HGL_MAX_PATH]= + OSStringList oalpn; + OSStringList oalfn= { #if HGL_OS == HGL_OS_Windows - OS_TEXT("\\OpenAL32.dll"), - OS_TEXT("\\ct_oal.dll"), - OS_TEXT("\\nvOpenAL.dll"), - OS_TEXT("\\soft_oal.dll"), - OS_TEXT("\\wrap_oal.dll"), + OS_TEXT("OpenAL32.dll"), + OS_TEXT("ct_oal.dll"), + OS_TEXT("nvOpenAL.dll"), + OS_TEXT("soft_oal.dll"), + OS_TEXT("wrap_oal.dll"), #elif (HGL_OS == HGL_OS_Linux)||(HGL_OS == HGL_OS_FreeBSD)||(HGL_OS == HGL_OS_NetBSD)||(HGL_OS == HGL_OS_OpenBSD) - OS_TEXT("/libopenal.so"), - OS_TEXT("/libopenal.so.1"), + OS_TEXT("libopenal.so"), + OS_TEXT("libopenal.so.1"), #elif HGL_OS == HGL_OS_MacOS - OS_TEXT("/libopenal.dylib"), + OS_TEXT("libopenal.dylib"), #endif//HGL_OS == HGL_OS_Windows - OS_TEXT("\x0") }; int count=0; - const os_char *pi_path=GetString(hfsPlugInPath); - os_char pifn[HGL_MAX_PATH]; - os_char dllfn[HGL_MAX_PATH]; os_char *final_filename=nullptr; + { + OSString pn; + + filesystem::GetCurrentPath(pn); + oalpn.Add(pn); + + pn=filesystem::MergeFilename(pn,OS_TEXT("Plug-Ins")); + if(filesystem::IsDirectory(pn)) + oalpn.Add(pn); + + filesystem::GetOSLibararyPath(pn); + oalpn.Add(pn); + } + CloseOpenAL(); if(filename) { - hgl::strcpy(dllfn,HGL_MAX_PATH,hgl::info::GetString(hgl::info::hfsOSLibraryPath).c_str()); - hgl::strcat(dllfn,HGL_MAX_PATH,HGL_DIRECTORY_SEPARATOR); - hgl::strcat(dllfn,HGL_MAX_PATH,filename,HGL_MAX_PATH); - - hgl::strcpy(pifn,HGL_MAX_PATH,pi_path); - hgl::strcat(pifn,HGL_MAX_PATH,HGL_DIRECTORY_SEPARATOR); - hgl::strcat(pifn,HGL_MAX_PATH,filename,HGL_MAX_PATH); - - if(filesystem::FileExist(filename ))final_filename=(os_char *)filename;else - if(filesystem::FileExist(dllfn ))final_filename=dllfn;else - if(filesystem::FileExist(pifn ))final_filename=pifn; - - AudioEM=LoadExternalModule(final_filename); + filesystem::FindFileOnPaths(filename,oalpn,nullptr,OnFindedOpenALDynamicLinkLibrary); } else { - do - { - hgl::strcpy(pifn,HGL_MAX_PATH,pi_path); - hgl::strcat(pifn,HGL_MAX_PATH,oalfn[count],HGL_MAX_PATH); - - hgl::strcpy(dllfn,HGL_MAX_PATH,hgl::info::GetString(hgl::info::hfsOSLibraryPath).c_str()); - hgl::strcat(dllfn,HGL_MAX_PATH,oalfn[count],HGL_MAX_PATH); - - if(filesystem::FileExist(dllfn))final_filename=dllfn;else - if(filesystem::FileExist(pifn ))final_filename=pifn;else - continue; - - AudioEM=LoadExternalModule(final_filename); - - if(AudioEM)break; //如果加载成功 - - }while(oalfn[++count][0]); + filesystem::FindFileOnPaths(oalfn,oalpn,nullptr,OnFindedOpenALDynamicLinkLibrary); } if(AudioEM) { - LOG_INFO(OS_TEXT("加载OpenAL成功!使用动态链接库: ")+OSString(final_filename)); - - return (AL_TRUE); + return true; } else { #if HGL_OS == HGL_OS_Windows LOG_ERROR( OS_TEXT("加载OpenAL动态链接库失败!OpenAL动态链接库可能是: OpenAL32.dll、soft_oal.dll、wrap_oal.dll、ct_oal.dll、nvOpenAL.dll\n") - OS_TEXT("软件实现的OpenAL32.DLL、wrap_oal可在http://www.openal.org上下载、soft_oal.dll可在http://kcat.strangesoft.net/openal.html下载!\n") + OS_TEXT("软件实现的OpenAL32.DLL、wrap_oal可在http://www.openal.or上下载、soft_oal.dll可在https://openal-soft.org下载!\n") OS_TEXT("硬件实现的OpenAL32.DLL请到对应您声卡的厂商网站下载对应的驱动程序! 下载完成后可其放入Windows\\System32目录下或应用程序Plug-Ins目录下即可!")); #else - LOG_ERROR( OS_TEXT("加载OpenAL动态链接库失败!")); + LOG_ERROR( OS_TEXT("加载OpenAL动态链接库失败,或没有找到OpenAL动态链接库!")); #endif//#if HGL_OS == HGL_OS_Windows - return (AL_FALSE); + return false; } }