Use a premade Sound object for the output's playSound functions

openmw-38
Chris Robinson 9 years ago
parent 53718a5ca0
commit 2ee3265b66

@ -648,9 +648,8 @@ size_t OpenAL_Output::getSoundDataSize(Sound_Handle data) const
} }
MWBase::SoundPtr OpenAL_Output::playSound(Sound_Handle data, float vol, float basevol, float pitch, int flags, float offset) void OpenAL_Output::playSound(MWBase::SoundPtr sound, Sound_Handle data, float offset)
{ {
boost::shared_ptr<Sound> sound;
ALuint source; ALuint source;
if(mFreeSources.empty()) if(mFreeSources.empty())
@ -659,16 +658,15 @@ MWBase::SoundPtr OpenAL_Output::playSound(Sound_Handle data, float vol, float ba
mFreeSources.pop_front(); mFreeSources.pop_front();
try { try {
sound.reset(new Sound(vol, basevol, pitch, flags));
alSourcef(source, AL_REFERENCE_DISTANCE, 1.0f); alSourcef(source, AL_REFERENCE_DISTANCE, 1.0f);
alSourcef(source, AL_MAX_DISTANCE, 1000.0f); alSourcef(source, AL_MAX_DISTANCE, 1000.0f);
alSourcef(source, AL_ROLLOFF_FACTOR, 0.0f); alSourcef(source, AL_ROLLOFF_FACTOR, 0.0f);
alSourcei(source, AL_SOURCE_RELATIVE, AL_TRUE); alSourcei(source, AL_SOURCE_RELATIVE, AL_TRUE);
alSourcei(source, AL_LOOPING, (flags&MWBase::SoundManager::Play_Loop) ? AL_TRUE : AL_FALSE); alSourcei(source, AL_LOOPING, sound->getIsLooping() ? AL_TRUE : AL_FALSE);
ALfloat gain = vol*basevol; ALfloat gain = sound->getRealVolume();
if(!(flags&MWBase::SoundManager::Play_NoEnv) && mListenerEnv == Env_Underwater) ALfloat pitch = sound->getPitch();
if(sound->getUseEnv() && mListenerEnv == Env_Underwater)
{ {
gain *= 0.9f; gain *= 0.9f;
pitch *= 0.7f; pitch *= 0.7f;
@ -681,12 +679,12 @@ MWBase::SoundPtr OpenAL_Output::playSound(Sound_Handle data, float vol, float ba
alSource3f(source, AL_VELOCITY, 0.0f, 0.0f, 0.0f); alSource3f(source, AL_VELOCITY, 0.0f, 0.0f, 0.0f);
alSourcef(source, AL_SEC_OFFSET, offset/pitch); alSourcef(source, AL_SEC_OFFSET, offset/pitch);
alSourcei(source, AL_BUFFER, GET_PTRID(data)); throwALerror();
alSourcei(source, AL_BUFFER, GET_PTRID(data));
alSourcePlay(source); alSourcePlay(source);
throwALerror(); throwALerror();
sound->mHandle = MAKE_PTRID(source);
mActiveSounds.push_back(sound); mActiveSounds.push_back(sound);
} }
catch(std::exception&) { catch(std::exception&) {
@ -694,13 +692,11 @@ MWBase::SoundPtr OpenAL_Output::playSound(Sound_Handle data, float vol, float ba
throw; throw;
} }
return sound; sound->mHandle = MAKE_PTRID(source);
} }
MWBase::SoundPtr OpenAL_Output::playSound3D(Sound_Handle data, const osg::Vec3f &pos, float vol, float basevol, float pitch, void OpenAL_Output::playSound3D(MWBase::SoundPtr sound, Sound_Handle data, float offset)
float mindist, float maxdist, int flags, float offset)
{ {
boost::shared_ptr<Sound> sound;
ALuint source; ALuint source;
if(mFreeSources.empty()) if(mFreeSources.empty())
@ -709,18 +705,19 @@ MWBase::SoundPtr OpenAL_Output::playSound3D(Sound_Handle data, const osg::Vec3f
mFreeSources.pop_front(); mFreeSources.pop_front();
try { try {
sound.reset(new Sound(pos, vol, basevol, pitch, mindist, maxdist, flags)); alSourcef(source, AL_REFERENCE_DISTANCE, sound->getMinDistance());
alSourcef(source, AL_MAX_DISTANCE, sound->getMaxDistance());
alSourcef(source, AL_REFERENCE_DISTANCE, mindist);
alSourcef(source, AL_MAX_DISTANCE, maxdist);
alSourcef(source, AL_ROLLOFF_FACTOR, 1.0f); alSourcef(source, AL_ROLLOFF_FACTOR, 1.0f);
alSourcei(source, AL_SOURCE_RELATIVE, AL_FALSE); alSourcei(source, AL_SOURCE_RELATIVE, AL_FALSE);
alSourcei(source, AL_LOOPING, (flags&MWBase::SoundManager::Play_Loop) ? AL_TRUE : AL_FALSE); alSourcei(source, AL_LOOPING, sound->getIsLooping() ? AL_TRUE : AL_FALSE);
ALfloat gain = vol*basevol; const osg::Vec3f &pos = sound->getPosition();
ALfloat maxdist = sound->getMaxDistance();
ALfloat gain = sound->getRealVolume();
ALfloat pitch = sound->getPitch();
if((pos - mListenerPos).length2() > maxdist*maxdist) if((pos - mListenerPos).length2() > maxdist*maxdist)
gain = 0.0f; gain = 0.0f;
if(!(flags&MWBase::SoundManager::Play_NoEnv) && mListenerEnv == Env_Underwater) if(sound->getUseEnv() && mListenerEnv == Env_Underwater)
{ {
gain *= 0.9f; gain *= 0.9f;
pitch *= 0.7f; pitch *= 0.7f;
@ -733,12 +730,12 @@ MWBase::SoundPtr OpenAL_Output::playSound3D(Sound_Handle data, const osg::Vec3f
alSource3f(source, AL_VELOCITY, 0.0f, 0.0f, 0.0f); alSource3f(source, AL_VELOCITY, 0.0f, 0.0f, 0.0f);
alSourcef(source, AL_SEC_OFFSET, offset/pitch); alSourcef(source, AL_SEC_OFFSET, offset/pitch);
alSourcei(source, AL_BUFFER, GET_PTRID(data)); throwALerror();
alSourcei(source, AL_BUFFER, GET_PTRID(data));
alSourcePlay(source); alSourcePlay(source);
throwALerror(); throwALerror();
sound->mHandle = MAKE_PTRID(source);
mActiveSounds.push_back(sound); mActiveSounds.push_back(sound);
} }
catch(std::exception&) { catch(std::exception&) {
@ -746,14 +743,12 @@ MWBase::SoundPtr OpenAL_Output::playSound3D(Sound_Handle data, const osg::Vec3f
throw; throw;
} }
return sound; sound->mHandle = MAKE_PTRID(source);
} }
void OpenAL_Output::stopSound(MWBase::SoundPtr sound) void OpenAL_Output::stopSound(MWBase::SoundPtr sound)
{ {
if(!sound->mHandle) if(!sound->mHandle) return;
return;
ALuint source = GET_PTRID(sound->mHandle); ALuint source = GET_PTRID(sound->mHandle);
sound->mHandle = 0; sound->mHandle = 0;
@ -766,8 +761,7 @@ void OpenAL_Output::stopSound(MWBase::SoundPtr sound)
bool OpenAL_Output::isSoundPlaying(MWBase::SoundPtr sound) bool OpenAL_Output::isSoundPlaying(MWBase::SoundPtr sound)
{ {
if(!sound->mHandle) if(!sound->mHandle) return false;
return false;
ALuint source = GET_PTRID(sound->mHandle); ALuint source = GET_PTRID(sound->mHandle);
ALint state; ALint state;
@ -911,8 +905,7 @@ MWBase::SoundStreamPtr OpenAL_Output::streamSound3D(DecoderPtr decoder, const os
void OpenAL_Output::stopStream(MWBase::SoundStreamPtr sound) void OpenAL_Output::stopStream(MWBase::SoundStreamPtr sound)
{ {
if(!sound->mHandle) if(!sound->mHandle) return;
return;
OpenAL_SoundStream *stream = reinterpret_cast<OpenAL_SoundStream*>(sound->mHandle); OpenAL_SoundStream *stream = reinterpret_cast<OpenAL_SoundStream*>(sound->mHandle);
ALuint source = stream->mSource; ALuint source = stream->mSource;
@ -930,16 +923,14 @@ void OpenAL_Output::stopStream(MWBase::SoundStreamPtr sound)
double OpenAL_Output::getStreamDelay(MWBase::SoundStreamPtr sound) double OpenAL_Output::getStreamDelay(MWBase::SoundStreamPtr sound)
{ {
if(!sound->mHandle) if(!sound->mHandle) return 0.0;
return 0.0;
OpenAL_SoundStream *stream = reinterpret_cast<OpenAL_SoundStream*>(sound->mHandle); OpenAL_SoundStream *stream = reinterpret_cast<OpenAL_SoundStream*>(sound->mHandle);
return stream->getStreamDelay(); return stream->getStreamDelay();
} }
double OpenAL_Output::getStreamOffset(MWBase::SoundStreamPtr sound) double OpenAL_Output::getStreamOffset(MWBase::SoundStreamPtr sound)
{ {
if(!sound->mHandle) if(!sound->mHandle) return 0.0;
return 0.0;
OpenAL_SoundStream *stream = reinterpret_cast<OpenAL_SoundStream*>(sound->mHandle); OpenAL_SoundStream *stream = reinterpret_cast<OpenAL_SoundStream*>(sound->mHandle);
boost::lock_guard<boost::mutex> lock(mStreamThread->mMutex); boost::lock_guard<boost::mutex> lock(mStreamThread->mMutex);
return stream->getStreamOffset(); return stream->getStreamOffset();
@ -947,8 +938,7 @@ double OpenAL_Output::getStreamOffset(MWBase::SoundStreamPtr sound)
bool OpenAL_Output::isStreamPlaying(MWBase::SoundStreamPtr sound) bool OpenAL_Output::isStreamPlaying(MWBase::SoundStreamPtr sound)
{ {
if(!sound->mHandle) if(!sound->mHandle) return false;
return false;
OpenAL_SoundStream *stream = reinterpret_cast<OpenAL_SoundStream*>(sound->mHandle); OpenAL_SoundStream *stream = reinterpret_cast<OpenAL_SoundStream*>(sound->mHandle);
boost::lock_guard<boost::mutex> lock(mStreamThread->mMutex); boost::lock_guard<boost::mutex> lock(mStreamThread->mMutex);
return stream->isPlaying(); return stream->isPlaying();

@ -47,9 +47,8 @@ namespace MWSound
virtual void unloadSound(Sound_Handle data); virtual void unloadSound(Sound_Handle data);
virtual size_t getSoundDataSize(Sound_Handle data) const; virtual size_t getSoundDataSize(Sound_Handle data) const;
virtual MWBase::SoundPtr playSound(Sound_Handle data, float vol, float basevol, float pitch, int flags, float offset); virtual void playSound(MWBase::SoundPtr sound, Sound_Handle data, float offset);
virtual MWBase::SoundPtr playSound3D(Sound_Handle data, const osg::Vec3f &pos, virtual void playSound3D(MWBase::SoundPtr sound, Sound_Handle data, float offset);
float vol, float basevol, float pitch, float min, float max, int flags, float offset);
virtual void stopSound(MWBase::SoundPtr sound); virtual void stopSound(MWBase::SoundPtr sound);
virtual bool isSoundPlaying(MWBase::SoundPtr sound); virtual bool isSoundPlaying(MWBase::SoundPtr sound);
virtual void updateSound(MWBase::SoundPtr sound); virtual void updateSound(MWBase::SoundPtr sound);

@ -22,7 +22,6 @@ namespace MWSound
protected: protected:
Sound_Instance mHandle; Sound_Instance mHandle;
friend class Sound_Output;
friend class OpenAL_Output; friend class OpenAL_Output;
public: public:
@ -43,11 +42,13 @@ namespace MWSound
const osg::Vec3f &getPosition() const { return mPos; } const osg::Vec3f &getPosition() const { return mPos; }
float getRealVolume() const { return mVolume * mBaseVolume; } float getRealVolume() const { return mVolume * mBaseVolume; }
float getPitch() const { return mPitch; } float getPitch() const { return mPitch; }
float getMinDistance() const { return mMinDistance; }
float getMaxDistance() const { return mMaxDistance; } float getMaxDistance() const { return mMaxDistance; }
MWBase::SoundManager::PlayType getPlayType() const MWBase::SoundManager::PlayType getPlayType() const
{ return (MWBase::SoundManager::PlayType)(mFlags&MWBase::SoundManager::Play_TypeMask); } { return (MWBase::SoundManager::PlayType)(mFlags&MWBase::SoundManager::Play_TypeMask); }
bool getUseEnv() const { return !(mFlags&MWBase::SoundManager::Play_NoEnv); } bool getUseEnv() const { return !(mFlags&MWBase::SoundManager::Play_NoEnv); }
bool getIsLooping() const { return mFlags&MWBase::SoundManager::Play_Loop; }
bool getDistanceCull() const { return mFlags&MWBase::SoundManager::Play_RemoveAtDistance; } bool getDistanceCull() const { return mFlags&MWBase::SoundManager::Play_RemoveAtDistance; }
bool getIs3D() const { return mFlags&Play_3D; } bool getIs3D() const { return mFlags&Play_3D; }

@ -31,11 +31,8 @@ namespace MWSound
virtual void unloadSound(Sound_Handle data) = 0; virtual void unloadSound(Sound_Handle data) = 0;
virtual size_t getSoundDataSize(Sound_Handle data) const = 0; virtual size_t getSoundDataSize(Sound_Handle data) const = 0;
/// @param offset Number of seconds into the sound to start playback. virtual void playSound(MWBase::SoundPtr sound, Sound_Handle data, float offset) = 0;
virtual MWBase::SoundPtr playSound(Sound_Handle data, float vol, float basevol, float pitch, int flags, float offset) = 0; virtual void playSound3D(MWBase::SoundPtr sound, Sound_Handle data, float offset) = 0;
/// @param offset Number of seconds into the sound to start playback.
virtual MWBase::SoundPtr playSound3D(Sound_Handle data, const osg::Vec3f &pos,
float vol, float basevol, float pitch, float min, float max, int flags, float offset) = 0;
virtual void stopSound(MWBase::SoundPtr sound) = 0; virtual void stopSound(MWBase::SoundPtr sound) = 0;
virtual bool isSoundPlaying(MWBase::SoundPtr sound) = 0; virtual bool isSoundPlaying(MWBase::SoundPtr sound) = 0;
virtual void updateSound(MWBase::SoundPtr sound) = 0; virtual void updateSound(MWBase::SoundPtr sound) = 0;

@ -508,9 +508,8 @@ namespace MWSound
Sound_Buffer *sfx = loadSound(Misc::StringUtils::lowerCase(soundId)); Sound_Buffer *sfx = loadSound(Misc::StringUtils::lowerCase(soundId));
float basevol = volumeFromType(type); float basevol = volumeFromType(type);
sound = mOutput->playSound(sfx->mHandle, sound.reset(new Sound(volume * sfx->mVolume, basevol, pitch, mode|type|Play_2D));
volume * sfx->mVolume, basevol, pitch, mode|type|Play_2D, offset mOutput->playSound(sound, sfx->mHandle, offset);
);
if(sfx->mUses++ == 0) if(sfx->mUses++ == 0)
{ {
SoundList::iterator iter = std::find(mUnusedBuffers.begin(), mUnusedBuffers.end(), sfx); SoundList::iterator iter = std::find(mUnusedBuffers.begin(), mUnusedBuffers.end(), sfx);
@ -522,6 +521,7 @@ namespace MWSound
catch(std::exception&) catch(std::exception&)
{ {
//std::cout <<"Sound Error: "<<e.what()<< std::endl; //std::cout <<"Sound Error: "<<e.what()<< std::endl;
sound.reset();
} }
return sound; return sound;
} }
@ -544,14 +544,16 @@ namespace MWSound
return MWBase::SoundPtr(); return MWBase::SoundPtr();
if(!(mode&Play_NoPlayerLocal) && ptr == MWMechanics::getPlayer()) if(!(mode&Play_NoPlayerLocal) && ptr == MWMechanics::getPlayer())
sound = mOutput->playSound(sfx->mHandle, {
volume * sfx->mVolume, basevol, pitch, mode|type|Play_2D, offset sound.reset(new Sound(volume * sfx->mVolume, basevol, pitch, mode|type|Play_2D));
); mOutput->playSound(sound, sfx->mHandle, offset);
}
else else
sound = mOutput->playSound3D(sfx->mHandle, {
objpos, volume * sfx->mVolume, basevol, pitch, sfx->mMinDist, sfx->mMaxDist, sound.reset(new Sound(objpos, volume * sfx->mVolume, basevol, pitch,
mode|type|Play_3D, offset sfx->mMinDist, sfx->mMaxDist, mode|type|Play_3D));
); mOutput->playSound3D(sound, sfx->mHandle, offset);
}
if(sfx->mUses++ == 0) if(sfx->mUses++ == 0)
{ {
SoundList::iterator iter = std::find(mUnusedBuffers.begin(), mUnusedBuffers.end(), sfx); SoundList::iterator iter = std::find(mUnusedBuffers.begin(), mUnusedBuffers.end(), sfx);
@ -563,6 +565,7 @@ namespace MWSound
catch(std::exception&) catch(std::exception&)
{ {
//std::cout <<"Sound Error: "<<e.what()<< std::endl; //std::cout <<"Sound Error: "<<e.what()<< std::endl;
sound.reset();
} }
return sound; return sound;
} }
@ -579,10 +582,9 @@ namespace MWSound
Sound_Buffer *sfx = loadSound(Misc::StringUtils::lowerCase(soundId)); Sound_Buffer *sfx = loadSound(Misc::StringUtils::lowerCase(soundId));
float basevol = volumeFromType(type); float basevol = volumeFromType(type);
sound = mOutput->playSound3D(sfx->mHandle, sound.reset(new Sound(initialPos, volume * sfx->mVolume, basevol, pitch,
initialPos, volume * sfx->mVolume, basevol, pitch, sfx->mMinDist, sfx->mMaxDist, sfx->mMinDist, sfx->mMaxDist, mode|type|Play_3D));
mode|type|Play_3D, offset mOutput->playSound3D(sound, sfx->mHandle, offset);
);
if(sfx->mUses++ == 0) if(sfx->mUses++ == 0)
{ {
SoundList::iterator iter = std::find(mUnusedBuffers.begin(), mUnusedBuffers.end(), sfx); SoundList::iterator iter = std::find(mUnusedBuffers.begin(), mUnusedBuffers.end(), sfx);
@ -594,6 +596,7 @@ namespace MWSound
catch(std::exception &) catch(std::exception &)
{ {
//std::cout <<"Sound Error: "<<e.what()<< std::endl; //std::cout <<"Sound Error: "<<e.what()<< std::endl;
sound.reset();
} }
return sound; return sound;
} }

Loading…
Cancel
Save