Return SoundPtr objects from playSound[3D]

Note that each Sound object currently contains "precious" resources even after
the sound is stopped. The reference should be reliquished as soon as it's no
longer needed (the SoundManager will make sure the sound continues to play
until it's finished).
actorid
Chris Robinson 13 years ago
parent 293f33914e
commit be337ef7cc

@ -5,9 +5,6 @@ namespace MWSound
{ {
class Sound class Sound
{ {
virtual void stop() = 0;
virtual bool isPlaying() = 0;
virtual void setVolume(float volume) = 0;
virtual void update(const float *pos) = 0; virtual void update(const float *pos) = 0;
Sound& operator=(const Sound &rhs); Sound& operator=(const Sound &rhs);
@ -20,6 +17,10 @@ namespace MWSound
float mMaxDistance; float mMaxDistance;
public: public:
virtual void stop() = 0;
virtual bool isPlaying() = 0;
virtual void setVolume(float volume) = 0;
Sound() : mVolume(1.0f) Sound() : mVolume(1.0f)
, mBaseVolume(1.0f) , mBaseVolume(1.0f)
, mMinDistance(20.0f) /* 1 * min_range_scale */ , mMinDistance(20.0f) /* 1 * min_range_scale */

@ -185,7 +185,8 @@ namespace MWSound
std::string filePath = std::string("Sound/")+filename; std::string filePath = std::string("Sound/")+filename;
const ESM::Position &pos = ptr.getCellRef().pos; const ESM::Position &pos = ptr.getCellRef().pos;
SoundPtr sound = mOutput->playSound3D(filePath, pos.pos, basevol, 1.0f, 20.0f, 12750.0f, false); SoundPtr sound = mOutput->playSound3D(filePath, pos.pos, basevol, 1.0f,
20.0f, 12750.0f, false);
sound->mBaseVolume = basevol; sound->mBaseVolume = basevol;
mActiveSounds[sound] = std::make_pair(ptr, std::string("_say_sound")); mActiveSounds[sound] = std::make_pair(ptr, std::string("_say_sound"));
@ -202,15 +203,16 @@ namespace MWSound
} }
void SoundManager::playSound(const std::string& soundId, float volume, float pitch, bool loop) SoundPtr SoundManager::playSound(const std::string& soundId, float volume, float pitch, bool loop)
{ {
SoundPtr sound;
try try
{ {
float basevol = 1.0f; /* TODO: volume settings */ float basevol = 1.0f; /* TODO: volume settings */
float min, max; float min, max;
std::string file = lookup(soundId, basevol, min, max); std::string file = lookup(soundId, basevol, min, max);
SoundPtr sound = mOutput->playSound(file, volume*basevol, pitch, loop); sound = mOutput->playSound(file, volume*basevol, pitch, loop);
sound->mVolume = volume; sound->mVolume = volume;
sound->mBaseVolume = basevol; sound->mBaseVolume = basevol;
sound->mMinDistance = min; sound->mMinDistance = min;
@ -222,11 +224,14 @@ namespace MWSound
{ {
std::cout <<"Sound Error: "<<e.what()<< std::endl; std::cout <<"Sound Error: "<<e.what()<< std::endl;
} }
return sound;
} }
void SoundManager::playSound3D(MWWorld::Ptr ptr, const std::string& soundId, SoundPtr SoundManager::playSound3D(MWWorld::Ptr ptr, const std::string& soundId,
float volume, float pitch, bool loop, bool untracked) float volume, float pitch, bool loop,
bool untracked)
{ {
SoundPtr sound;
try try
{ {
// Look up the sound in the ESM data // Look up the sound in the ESM data
@ -235,7 +240,7 @@ namespace MWSound
std::string file = lookup(soundId, basevol, min, max); std::string file = lookup(soundId, basevol, min, max);
const ESM::Position &pos = ptr.getCellRef().pos; const ESM::Position &pos = ptr.getCellRef().pos;
SoundPtr sound = mOutput->playSound3D(file, pos.pos, volume*basevol, pitch, min, max, loop); sound = mOutput->playSound3D(file, pos.pos, volume*basevol, pitch, min, max, loop);
sound->mVolume = volume; sound->mVolume = volume;
sound->mBaseVolume = basevol; sound->mBaseVolume = basevol;
sound->mMinDistance = min; sound->mMinDistance = min;
@ -248,6 +253,7 @@ namespace MWSound
{ {
std::cout <<"Sound Error: "<<e.what()<< std::endl; std::cout <<"Sound Error: "<<e.what()<< std::endl;
} }
return sound;
} }
void SoundManager::stopSound3D(MWWorld::Ptr ptr, const std::string& soundId) void SoundManager::stopSound3D(MWWorld::Ptr ptr, const std::string& soundId)

@ -87,12 +87,12 @@ namespace MWSound
bool sayDone(MWWorld::Ptr reference) const; bool sayDone(MWWorld::Ptr reference) const;
///< Is actor not speaking? ///< Is actor not speaking?
void playSound(const std::string& soundId, float volume, float pitch, bool loop=false); SoundPtr playSound(const std::string& soundId, float volume, float pitch, bool loop=false);
///< Play a sound, independently of 3D-position ///< Play a sound, independently of 3D-position
void playSound3D(MWWorld::Ptr reference, const std::string& soundId, SoundPtr playSound3D(MWWorld::Ptr reference, const std::string& soundId,
float volume, float pitch, bool loop, float volume, float pitch, bool loop,
bool untracked=false); bool untracked=false);
///< Play a sound from an object ///< Play a sound from an object
void stopSound3D(MWWorld::Ptr reference, const std::string& soundId); void stopSound3D(MWWorld::Ptr reference, const std::string& soundId);

Loading…
Cancel
Save