1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-03-30 11:06:43 +00:00

Use a separate type for streams

They're basically the same, but it's to help avoid accidents with passing non-
streaming sounds to the stream functions, or vice-versa.
This commit is contained in:
Chris Robinson 2015-11-30 07:32:42 -08:00
parent 816015d6e6
commit 1ce3e7f5b9
8 changed files with 65 additions and 53 deletions

View file

@ -15,6 +15,7 @@ namespace MWWorld
namespace MWSound namespace MWSound
{ {
class Sound; class Sound;
class Stream;
struct Sound_Decoder; struct Sound_Decoder;
typedef boost::shared_ptr<Sound_Decoder> DecoderPtr; typedef boost::shared_ptr<Sound_Decoder> DecoderPtr;
} }
@ -22,6 +23,7 @@ namespace MWSound
namespace MWBase namespace MWBase
{ {
typedef boost::shared_ptr<MWSound::Sound> SoundPtr; typedef boost::shared_ptr<MWSound::Sound> SoundPtr;
typedef boost::shared_ptr<MWSound::Stream> SoundStreamPtr;
/// \brief Interface for sound manager (implemented in MWSound) /// \brief Interface for sound manager (implemented in MWSound)
class SoundManager class SoundManager
@ -104,13 +106,13 @@ namespace MWBase
/// and get an average loudness value (scale [0,1]) at the current time position. /// and get an average loudness value (scale [0,1]) at the current time position.
/// If the actor is not saying anything, returns 0. /// If the actor is not saying anything, returns 0.
virtual SoundPtr playTrack(const MWSound::DecoderPtr& decoder, PlayType type) = 0; virtual SoundStreamPtr playTrack(const MWSound::DecoderPtr& decoder, PlayType type) = 0;
///< Play a 2D audio track, using a custom decoder ///< Play a 2D audio track, using a custom decoder
virtual void stopTrack(SoundPtr sound) = 0; virtual void stopTrack(SoundStreamPtr stream) = 0;
///< Stop the given audio track from playing ///< Stop the given audio track from playing
virtual double getTrackTimeDelay(SoundPtr sound) = 0; virtual double getTrackTimeDelay(SoundStreamPtr stream) = 0;
///< Retives the time delay, in seconds, of the audio track (must be a sound ///< Retives the time delay, in seconds, of the audio track (must be a sound
/// returned by \ref playTrack). Only intended to be called by the track /// returned by \ref playTrack). Only intended to be called by the track
/// decoder's read method. /// decoder's read method.
@ -166,7 +168,7 @@ namespace MWBase
virtual void setListenerPosDir(const osg::Vec3f &pos, const osg::Vec3f &dir, const osg::Vec3f &up) = 0; virtual void setListenerPosDir(const osg::Vec3f &pos, const osg::Vec3f &dir, const osg::Vec3f &up) = 0;
virtual void updatePtr (const MWWorld::Ptr& old, const MWWorld::Ptr& updated) = 0; virtual void updatePtr(const MWWorld::Ptr& old, const MWWorld::Ptr& updated) = 0;
virtual void clear() = 0; virtual void clear() = 0;
}; };

View file

@ -92,7 +92,7 @@ namespace MWSound
mDecoderBridge.reset(); mDecoderBridge.reset();
} }
MWBase::SoundPtr mAudioTrack; MWBase::SoundStreamPtr mAudioTrack;
boost::shared_ptr<MWSoundDecoderBridge> mDecoderBridge; boost::shared_ptr<MWSoundDecoderBridge> mDecoderBridge;
}; };
@ -163,7 +163,8 @@ namespace MWSound
boost::shared_ptr<MWSound::MovieAudioDecoder> decoder(new MWSound::MovieAudioDecoder(videoState)); boost::shared_ptr<MWSound::MovieAudioDecoder> decoder(new MWSound::MovieAudioDecoder(videoState));
decoder->setupFormat(); decoder->setupFormat();
MWBase::SoundPtr sound = MWBase::Environment::get().getSoundManager()->playTrack(decoder->mDecoderBridge, MWBase::SoundManager::Play_TypeMovie); MWBase::SoundManager *sndMgr = MWBase::Environment::get().getSoundManager();
MWBase::SoundStreamPtr sound = sndMgr->playTrack(decoder->mDecoderBridge, MWBase::SoundManager::Play_TypeMovie);
if (!sound.get()) if (!sound.get())
{ {
decoder.reset(); decoder.reset();

View file

@ -776,9 +776,9 @@ bool OpenAL_Output::isSoundPlaying(MWBase::SoundPtr sound)
} }
MWBase::SoundPtr OpenAL_Output::streamSound(DecoderPtr decoder, float basevol, float pitch, int flags) MWBase::SoundStreamPtr OpenAL_Output::streamSound(DecoderPtr decoder, float basevol, float pitch, int flags)
{ {
boost::shared_ptr<Sound> sound; MWBase::SoundStreamPtr sound;
OpenAL_SoundStream *stream = 0; OpenAL_SoundStream *stream = 0;
ALuint source; ALuint source;
@ -810,7 +810,7 @@ MWBase::SoundPtr OpenAL_Output::streamSound(DecoderPtr decoder, float basevol, f
alSource3f(source, AL_VELOCITY, 0.0f, 0.0f, 0.0f); alSource3f(source, AL_VELOCITY, 0.0f, 0.0f, 0.0f);
throwALerror(); throwALerror();
sound.reset(new Sound(osg::Vec3f(0.0f, 0.0f, 0.0f), 1.0f, basevol, pitch, 1.0f, 1000.0f, flags)); sound.reset(new Stream(osg::Vec3f(0.0f, 0.0f, 0.0f), 1.0f, basevol, pitch, 1.0f, 1000.0f, flags));
stream = new OpenAL_SoundStream(source, decoder); stream = new OpenAL_SoundStream(source, decoder);
mStreamThread->add(stream); mStreamThread->add(stream);
sound->mHandle = stream; sound->mHandle = stream;
@ -826,9 +826,9 @@ MWBase::SoundPtr OpenAL_Output::streamSound(DecoderPtr decoder, float basevol, f
return sound; return sound;
} }
MWBase::SoundPtr OpenAL_Output::streamSound3D(DecoderPtr decoder, const osg::Vec3f &pos, float volume, float basevol, float pitch, float mindist, float maxdist, int flags) MWBase::SoundStreamPtr OpenAL_Output::streamSound3D(DecoderPtr decoder, const osg::Vec3f &pos, float volume, float basevol, float pitch, float mindist, float maxdist, int flags)
{ {
boost::shared_ptr<Sound> sound; MWBase::SoundStreamPtr sound;
OpenAL_SoundStream *stream = 0; OpenAL_SoundStream *stream = 0;
ALuint source; ALuint source;
@ -862,7 +862,7 @@ MWBase::SoundPtr OpenAL_Output::streamSound3D(DecoderPtr decoder, const osg::Vec
alSource3f(source, AL_VELOCITY, 0.0f, 0.0f, 0.0f); alSource3f(source, AL_VELOCITY, 0.0f, 0.0f, 0.0f);
throwALerror(); throwALerror();
sound.reset(new Sound(pos, volume, basevol, pitch, mindist, maxdist, flags)); sound.reset(new Stream(pos, volume, basevol, pitch, mindist, maxdist, flags));
stream = new OpenAL_SoundStream(source, decoder); stream = new OpenAL_SoundStream(source, decoder);
mStreamThread->add(stream); mStreamThread->add(stream);
sound->mHandle = stream; sound->mHandle = stream;
@ -878,7 +878,7 @@ MWBase::SoundPtr OpenAL_Output::streamSound3D(DecoderPtr decoder, const osg::Vec
return sound; return sound;
} }
void OpenAL_Output::stopStream(MWBase::SoundPtr sound) void OpenAL_Output::stopStream(MWBase::SoundStreamPtr sound)
{ {
if(!sound->mHandle) if(!sound->mHandle)
return; return;
@ -897,7 +897,7 @@ void OpenAL_Output::stopStream(MWBase::SoundPtr sound)
delete stream; delete stream;
} }
double OpenAL_Output::getStreamDelay(MWBase::SoundPtr sound) double OpenAL_Output::getStreamDelay(MWBase::SoundStreamPtr sound)
{ {
if(!sound->mHandle) if(!sound->mHandle)
return 0.0; return 0.0;
@ -905,7 +905,7 @@ double OpenAL_Output::getStreamDelay(MWBase::SoundPtr sound)
return stream->getStreamDelay(); return stream->getStreamDelay();
} }
double OpenAL_Output::getStreamOffset(MWBase::SoundPtr sound) double OpenAL_Output::getStreamOffset(MWBase::SoundStreamPtr sound)
{ {
if(!sound->mHandle) if(!sound->mHandle)
return 0.0; return 0.0;
@ -914,7 +914,7 @@ double OpenAL_Output::getStreamOffset(MWBase::SoundPtr sound)
return stream->getStreamOffset(); return stream->getStreamOffset();
} }
bool OpenAL_Output::isStreamPlaying(MWBase::SoundPtr sound) bool OpenAL_Output::isStreamPlaying(MWBase::SoundStreamPtr sound)
{ {
if(!sound->mHandle) if(!sound->mHandle)
return false; return false;

View file

@ -26,7 +26,7 @@ namespace MWSound
typedef std::vector<MWBase::SoundPtr> SoundVec; typedef std::vector<MWBase::SoundPtr> SoundVec;
SoundVec mActiveSounds; SoundVec mActiveSounds;
typedef std::vector<MWBase::SoundPtr> StreamVec; typedef std::vector<MWBase::SoundStreamPtr> StreamVec;
StreamVec mActiveStreams; StreamVec mActiveStreams;
Environment mLastEnvironment; Environment mLastEnvironment;
@ -45,13 +45,13 @@ namespace MWSound
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 MWBase::SoundPtr streamSound(DecoderPtr decoder, float basevol, float pitch, int flags); virtual MWBase::SoundStreamPtr streamSound(DecoderPtr decoder, float basevol, float pitch, int flags);
virtual MWBase::SoundPtr streamSound3D(DecoderPtr decoder, const osg::Vec3f &pos, virtual MWBase::SoundStreamPtr streamSound3D(DecoderPtr decoder, const osg::Vec3f &pos,
float vol, float basevol, float pitch, float min, float max, int flags); float vol, float basevol, float pitch, float min, float max, int flags);
virtual void stopStream(MWBase::SoundPtr sound); virtual void stopStream(MWBase::SoundStreamPtr sound);
virtual double getStreamDelay(MWBase::SoundPtr sound); virtual double getStreamDelay(MWBase::SoundStreamPtr sound);
virtual double getStreamOffset(MWBase::SoundPtr sound); virtual double getStreamOffset(MWBase::SoundStreamPtr sound);
virtual bool isStreamPlaying(MWBase::SoundPtr sound); virtual bool isStreamPlaying(MWBase::SoundStreamPtr sound);
virtual void startUpdate(); virtual void startUpdate();
virtual void finishUpdate(); virtual void finishUpdate();

View file

@ -5,8 +5,7 @@
namespace MWSound namespace MWSound
{ {
class Sound class Sound {
{
Sound& operator=(const Sound &rhs); Sound& operator=(const Sound &rhs);
Sound(const Sound &rhs); Sound(const Sound &rhs);
@ -51,7 +50,17 @@ namespace MWSound
, mMinDistance(mindist), mMaxDistance(maxdist), mFlags(flags) , mMinDistance(mindist), mMaxDistance(maxdist), mFlags(flags)
, mFadeOutTime(0.0f), mHandle(0) , mFadeOutTime(0.0f), mHandle(0)
{ } { }
~Sound() { } };
// Same as above, but it's a different type since the output handles them differently
class Stream : public Sound {
Stream& operator=(const Stream &rhs);
Stream(const Stream &rhs);
public:
Stream(const osg::Vec3f& pos, float vol, float basevol, float pitch, float mindist, float maxdist, int flags)
: Sound(pos, vol, basevol, pitch, mindist, maxdist, flags)
{ }
}; };
} }

View file

@ -38,13 +38,13 @@ namespace MWSound
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 MWBase::SoundPtr streamSound(DecoderPtr decoder, float basevol, float pitch, int flags) = 0; virtual MWBase::SoundStreamPtr streamSound(DecoderPtr decoder, float basevol, float pitch, int flags) = 0;
virtual MWBase::SoundPtr streamSound3D(DecoderPtr decoder, const osg::Vec3f &pos, virtual MWBase::SoundStreamPtr streamSound3D(DecoderPtr decoder, const osg::Vec3f &pos,
float vol, float basevol, float pitch, float min, float max, int flags) = 0; float vol, float basevol, float pitch, float min, float max, int flags) = 0;
virtual void stopStream(MWBase::SoundPtr sound) = 0; virtual void stopStream(MWBase::SoundStreamPtr sound) = 0;
virtual double getStreamDelay(MWBase::SoundPtr sound) = 0; virtual double getStreamDelay(MWBase::SoundStreamPtr sound) = 0;
virtual double getStreamOffset(MWBase::SoundPtr sound) = 0; virtual double getStreamOffset(MWBase::SoundStreamPtr sound) = 0;
virtual bool isStreamPlaying(MWBase::SoundPtr sound) = 0; virtual bool isStreamPlaying(MWBase::SoundStreamPtr sound) = 0;
virtual void startUpdate() = 0; virtual void startUpdate() = 0;
virtual void finishUpdate() = 0; virtual void finishUpdate() = 0;

View file

@ -241,7 +241,7 @@ namespace MWSound
return decoder; return decoder;
} }
MWBase::SoundPtr SoundManager::playVoice(DecoderPtr decoder, const osg::Vec3f &pos, bool playlocal) MWBase::SoundStreamPtr SoundManager::playVoice(DecoderPtr decoder, const osg::Vec3f &pos, bool playlocal)
{ {
MWBase::World* world = MWBase::Environment::get().getWorld(); MWBase::World* world = MWBase::Environment::get().getWorld();
static const float fAudioMinDistanceMult = world->getStore().get<ESM::GameSetting>().find("fAudioMinDistanceMult")->getFloat(); static const float fAudioMinDistanceMult = world->getStore().get<ESM::GameSetting>().find("fAudioMinDistanceMult")->getFloat();
@ -392,7 +392,7 @@ namespace MWSound
mPendingSaySounds[ptr] = std::make_pair(decoder, loudness); mPendingSaySounds[ptr] = std::make_pair(decoder, loudness);
else else
{ {
MWBase::SoundPtr sound = playVoice(decoder, objpos, (ptr == MWMechanics::getPlayer())); MWBase::SoundStreamPtr sound = playVoice(decoder, objpos, (ptr == MWMechanics::getPlayer()));
mActiveSaySounds[ptr] = std::make_pair(sound, loudness); mActiveSaySounds[ptr] = std::make_pair(sound, loudness);
} }
} }
@ -407,7 +407,7 @@ namespace MWSound
SaySoundMap::const_iterator snditer = mActiveSaySounds.find(ptr); SaySoundMap::const_iterator snditer = mActiveSaySounds.find(ptr);
if(snditer != mActiveSaySounds.end()) if(snditer != mActiveSaySounds.end())
{ {
MWBase::SoundPtr sound = snditer->second.first; MWBase::SoundStreamPtr sound = snditer->second.first;
Sound_Loudness *loudness = snditer->second.second; Sound_Loudness *loudness = snditer->second.second;
float sec = mOutput->getStreamOffset(sound); float sec = mOutput->getStreamOffset(sound);
return loudness->getLoudnessAtTime(sec); return loudness->getLoudnessAtTime(sec);
@ -432,7 +432,7 @@ namespace MWSound
mPendingSaySounds[MWWorld::Ptr()] = std::make_pair(decoder, loudness); mPendingSaySounds[MWWorld::Ptr()] = std::make_pair(decoder, loudness);
else else
{ {
MWBase::SoundPtr sound = playVoice(decoder, osg::Vec3f(), true); MWBase::SoundStreamPtr sound = playVoice(decoder, osg::Vec3f(), true);
mActiveSaySounds[MWWorld::Ptr()] = std::make_pair(sound, loudness); mActiveSaySounds[MWWorld::Ptr()] = std::make_pair(sound, loudness);
} }
} }
@ -466,9 +466,9 @@ namespace MWSound
} }
MWBase::SoundPtr SoundManager::playTrack(const DecoderPtr& decoder, PlayType type) MWBase::SoundStreamPtr SoundManager::playTrack(const DecoderPtr& decoder, PlayType type)
{ {
MWBase::SoundPtr track; MWBase::SoundStreamPtr track;
if(!mOutput->isInitialized()) if(!mOutput->isInitialized())
return track; return track;
try try
@ -482,14 +482,14 @@ namespace MWSound
return track; return track;
} }
void SoundManager::stopTrack(MWBase::SoundPtr sound) void SoundManager::stopTrack(MWBase::SoundStreamPtr stream)
{ {
mOutput->stopStream(sound); mOutput->stopStream(stream);
} }
double SoundManager::getTrackTimeDelay(MWBase::SoundPtr sound) double SoundManager::getTrackTimeDelay(MWBase::SoundStreamPtr stream)
{ {
return mOutput->getStreamDelay(sound); return mOutput->getStreamDelay(stream);
} }
@ -876,7 +876,7 @@ namespace MWSound
const ESM::Position &pos = ptr.getRefData().getPosition(); const ESM::Position &pos = ptr.getRefData().getPosition();
const osg::Vec3f objpos(pos.asVec3()); const osg::Vec3f objpos(pos.asVec3());
MWBase::SoundPtr sound = playVoice(decoder, MWBase::SoundStreamPtr sound = playVoice(decoder,
objpos, (ptr == MWMechanics::getPlayer()) objpos, (ptr == MWMechanics::getPlayer())
); );
mActiveSaySounds[ptr] = std::make_pair(sound, loudness); mActiveSaySounds[ptr] = std::make_pair(sound, loudness);
@ -895,7 +895,7 @@ namespace MWSound
while(sayiter != mActiveSaySounds.end()) while(sayiter != mActiveSaySounds.end())
{ {
MWWorld::Ptr ptr = sayiter->first; MWWorld::Ptr ptr = sayiter->first;
MWBase::SoundPtr sound = sayiter->second.first; MWBase::SoundStreamPtr sound = sayiter->second.first;
if(!ptr.isEmpty() && sound->getIs3D()) if(!ptr.isEmpty() && sound->getIs3D())
{ {
const ESM::Position &pos = ptr.getRefData().getPosition(); const ESM::Position &pos = ptr.getRefData().getPosition();
@ -963,7 +963,7 @@ namespace MWSound
SaySoundMap::iterator sayiter = mActiveSaySounds.begin(); SaySoundMap::iterator sayiter = mActiveSaySounds.begin();
for(;sayiter != mActiveSaySounds.end();++sayiter) for(;sayiter != mActiveSaySounds.end();++sayiter)
{ {
MWBase::SoundPtr sound = sayiter->second.first; MWBase::SoundStreamPtr sound = sayiter->second.first;
sound->setBaseVolume(volumeFromType(sound->getPlayType())); sound->setBaseVolume(volumeFromType(sound->getPlayType()));
} }
if(mMusic) if(mMusic)

View file

@ -79,7 +79,7 @@ namespace MWSound
typedef std::deque<Sound_Buffer*> SoundList; typedef std::deque<Sound_Buffer*> SoundList;
SoundList mUnusedBuffers; SoundList mUnusedBuffers;
boost::shared_ptr<Sound> mMusic; MWBase::SoundStreamPtr mMusic;
std::string mCurrentPlaylist; std::string mCurrentPlaylist;
typedef std::pair<MWBase::SoundPtr,Sound_Buffer*> SoundBufferRefPair; typedef std::pair<MWBase::SoundPtr,Sound_Buffer*> SoundBufferRefPair;
@ -87,7 +87,7 @@ namespace MWSound
typedef std::map<MWWorld::Ptr,SoundBufferRefPairList> SoundMap; typedef std::map<MWWorld::Ptr,SoundBufferRefPairList> SoundMap;
SoundMap mActiveSounds; SoundMap mActiveSounds;
typedef std::pair<MWBase::SoundPtr,Sound_Loudness*> SoundLoudnessPair; typedef std::pair<MWBase::SoundStreamPtr,Sound_Loudness*> SoundLoudnessPair;
typedef std::map<MWWorld::Ptr,SoundLoudnessPair> SaySoundMap; typedef std::map<MWWorld::Ptr,SoundLoudnessPair> SaySoundMap;
SaySoundMap mActiveSaySounds; SaySoundMap mActiveSaySounds;
@ -113,7 +113,7 @@ namespace MWSound
// to start streaming // to start streaming
DecoderPtr loadVoice(const std::string &voicefile, Sound_Loudness **lipdata); DecoderPtr loadVoice(const std::string &voicefile, Sound_Loudness **lipdata);
MWBase::SoundPtr playVoice(DecoderPtr decoder, const osg::Vec3f &pos, bool playlocal); MWBase::SoundStreamPtr playVoice(DecoderPtr decoder, const osg::Vec3f &pos, bool playlocal);
void streamMusicFull(const std::string& filename); void streamMusicFull(const std::string& filename);
void updateSounds(float duration); void updateSounds(float duration);
@ -170,13 +170,13 @@ namespace MWSound
/// and get an average loudness value (scale [0,1]) at the current time position. /// and get an average loudness value (scale [0,1]) at the current time position.
/// If the actor is not saying anything, returns 0. /// If the actor is not saying anything, returns 0.
virtual MWBase::SoundPtr playTrack(const DecoderPtr& decoder, PlayType type); virtual MWBase::SoundStreamPtr playTrack(const DecoderPtr& decoder, PlayType type);
///< Play a 2D audio track, using a custom decoder ///< Play a 2D audio track, using a custom decoder
virtual void stopTrack(MWBase::SoundPtr sound); virtual void stopTrack(MWBase::SoundStreamPtr stream);
///< Stop the given audio track from playing ///< Stop the given audio track from playing
virtual double getTrackTimeDelay(MWBase::SoundPtr sound); virtual double getTrackTimeDelay(MWBase::SoundStreamPtr stream);
///< Retives the time delay, in seconds, of the audio track (must be a sound ///< Retives the time delay, in seconds, of the audio track (must be a sound
/// returned by \ref playTrack). Only intended to be called by the track /// returned by \ref playTrack). Only intended to be called by the track
/// decoder's read method. /// decoder's read method.