#include "log.hpp"
ManagedSoundSource::ManagedSoundSource(SoundManager* sound_manager, const std::string& name)
- : name(name), soundManager(sound_manager)
+ : name(name), soundSource(0), soundManager(sound_manager)
{
- soundSource = soundManager->create_sound_source(name);
- if(!soundSource) {
- log_warning << "Couldn't create managed sound source for \"" << name << "\"" << std::endl;
- return;
- }
}
ManagedSoundSource::ManagedSoundSource(const ManagedSoundSource& other)
- : name(other.name), soundManager(other.soundManager)
+ : name(other.name), soundSource(0), soundManager(other.soundManager)
{
- soundSource = soundManager->create_sound_source(name);
}
ManagedSoundSource::~ManagedSoundSource()
delete soundSource;
}
+bool
+ManagedSoundSource::preload()
+{
+ if (soundSource) return true;
+ soundSource = soundManager->create_sound_source(name);
+ return (soundSource != 0);
+}
+
+void
+ManagedSoundSource::release()
+{
+ if (!soundSource) return;
+ if (playing()) soundSource->stop();
+ delete soundSource;
+ soundSource = 0;
+}
+
+void
+ManagedSoundSource::play()
+{
+ if (!preload()) {
+ log_warning << "Couldn't play \"" << name << "\"" << std::endl;
+ return;
+ }
+ soundSource->play();
+}
+
+void
+ManagedSoundSource::stop()
+{
+ // FIXME: calling release() instead of stop() seems necessary due to an unconfirmed sound bug
+ release();
+}
+
+bool
+ManagedSoundSource::playing()
+{
+ if (!soundSource) return false;
+ return soundSource->playing();
+}
+
* pre-loads SoundSource and indicates success.
* If pre-loading failed, the SoundSource will be loaded at first use
*/
- bool preload() { return (soundSource != 0); /* TODO: implement */ }
+ bool preload();
/**
* stops playing and temporarily releases memory for the SoundSource.
* Memory will be re-allocated on next use
*/
- void release() { if (playing()) stop(); /* TODO: implement*/ }
+ void release();
- void play() { if (preload()) soundSource->play(); }
- void stop() { if (preload()) soundSource->stop(); }
- bool playing() { return ((preload())?(soundSource->playing()):(false)); }
+ void play();
+ void stop();
+ bool playing();
void set_looping(bool looping) { if (preload()) soundSource->set_looping(looping); }
void set_gain(float gain) { if (preload()) soundSource->set_gain(gain); }
void set_pitch(float pitch) { if (preload()) soundSource->set_pitch(pitch); }