25 #if defined(_DEBUG) && defined(_WINDOWS)
26 #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
34 template class Singleton<Sound_Source_Pool>;
36 Sound_Source_Pool * Sound_Source_Pool::create() {
37 return new Sound_Source_Pool;
40 Singleton<Sound_Source_Pool>::Uninit Sound_Source_Pool::g_uninit;
41 Singleton<Sound_Source_Pool>::Reinit Sound_Source_Pool::g_reinit;
43 Sound_Source_Pool::Sound_Source_Pool()
44 : m_replacement_policy(new Replacement_Policy()),
45 delete_m_replacement_policy(
true),
53 sr.lend_pre_uninit(&g_uninit);
54 sr.lend_post_reinit(&g_reinit);
57 Sound_Source_Pool::~Sound_Source_Pool() {
62 for(std::vector<Sound_Source *>::iterator it = m_handles.begin();
63 it != m_handles.end();
66 (*it)->m_remove_from_Pool_on_destruction =
false;
69 if(delete_m_replacement_policy)
70 delete m_replacement_policy;
96 return (*
this)(*lhs, *rhs);
100 : listener_position(listener_position_)
126 if(rhs_gain < lhs_gain)
128 if(lhs_gain < rhs_gain)
135 return *m_replacement_policy;
139 set_Replacement_Policy(replacement_policy);
140 delete_m_replacement_policy =
true;
144 set_Replacement_Policy(replacement_policy);
145 delete_m_replacement_policy =
false;
149 for(std::vector<Sound_Source *>::iterator it = m_handles.begin();
150 it != m_handles.end();
153 if((*it)->is_playing())
159 for(std::vector<Sound_Source *>::iterator it = m_handles.begin();
160 it != m_handles.end();
163 if((*it)->is_paused())
169 for(std::vector<Sound_Source *>::iterator it = m_playing_and_destroying.begin();
170 it != m_playing_and_destroying.end();
175 m_playing_and_destroying.clear();
181 const std::vector<Sound_Source *>::iterator &begin,
182 const std::vector<Sound_Source *>::iterator &
end,
185 typedef std::vector<Sound_Source *>::difference_type diff_t;
186 const diff_t
size = end - begin;
191 std::vector<Sound_Source *> less, greater;
192 std::vector<Sound_Source *>::iterator pivot = begin + size / 2;
195 for(std::vector<Sound_Source *>::iterator
x = begin;
x !=
end; ++
x)
197 if(policy(*
x, pivotValue))
200 greater.push_back(*
x);
203 const std::vector<Sound_Source *>::iterator less_end = std::copy(less.begin(), less.end(), begin);
204 *less_end = pivotValue;
205 const std::vector<Sound_Source *>::iterator greater_begin = less_end + 1;
206 std::copy(greater.begin(), greater.end(), greater_begin);
218 std::vector<Sound_Source *> keepers;
219 keepers.reserve(m_playing_and_destroying.size());
220 for(std::vector<Sound_Source *>::iterator it = m_playing_and_destroying.begin();
221 it != m_playing_and_destroying.end();
224 if((*it)->is_stopped()) {
225 ZENI_LOGD(
"Removing Sound_Source from Sound_Source_Pool");
229 keepers.push_back(*it);
231 m_playing_and_destroying.swap(keepers);
240 std::vector<Sound_Source_HW *> unassigned_hw;
241 const size_t needed_hw = m_handles.size();
246 for(std::vector<Sound_Source *>::iterator it = m_handles.begin();
247 it != m_handles.end();
250 if((*it)->is_assigned())
257 while(needed_hw > given_hw
258 #ifdef TEST_NASTY_CONDITIONS
266 ZENI_LOGD(
"Sound_Source_HW added to Sound_Source_Pool");
267 unassigned_hw.push_back(sshw);
271 ZENI_LOGD(
"Failed to add Sound_Source_HW to Sound_Source_Pool");
278 const size_t cut = needed_hw - given_hw;
281 sound_quicksort(m_handles.begin(), m_handles.end(), *m_replacement_policy);
283 for(
size_t i = 0;
i != cut; ++
i) {
287 unassigned_hw.push_back(source.unassign());
293 std::vector<Sound_Source_HW *>::iterator jt = unassigned_hw.begin();
295 for(
size_t i = cut;
i != needed_hw; ++
i) {
305 void Sound_Source_Pool::play_and_destroy(
Sound_Source *
const &sound_source) {
306 m_playing_and_destroying.push_back(sound_source);
307 sound_source->
play();
310 void Sound_Source_Pool::set_Replacement_Policy(Sound_Source_Pool::Replacement_Policy *
const &replacement_policy) {
311 assert(replacement_policy);
313 if(delete_m_replacement_policy)
314 delete m_replacement_policy;
316 m_replacement_policy = replacement_policy;
319 void Sound_Source_Pool::insert_Sound_Source(
Sound_Source &sound_source) {
320 m_handles.push_back(&sound_source);
323 void Sound_Source_Pool::remove_Sound_Source(
Sound_Source &sound_source) {
326 if(sound_source.m_hw) {
327 delete sound_source.m_hw;
328 sound_source.m_hw = 0;
331 m_handles.erase(std::find(m_handles.begin(), m_handles.end(), &sound_source));
334 void Sound_Source_Pool::destroy_all_hw() {
335 for(std::vector<Sound_Source *>::iterator it = m_handles.begin();
336 it != m_handles.end();
339 if((*it)->is_assigned()) {
340 Sound_Source_HW * hw = (*it)->unassign();
void pause_all()
Pause all Sound_Sources.
Sounds & get_Sounds()
Get access to the singleton.
#define NASTY_SOUND_SOURCE_CAP
float get_gain() const
Get the gain.
Sound_Source_Pool & get_Sound_Source_Pool()
Get access to the singleton.
virtual bool operator()(const Sound_Source &lhs, const Sound_Source &rhs) const
(Default) Priority Sort: Playing/Not-Playing, Priority, Gain, Recency
int get_priority() const
Get the Sound_Source's priority.
Time_HQ get_unstop_time() const
Get the Time_HQ at which the Sound_Source was most recently played or unpaused.
void purge()
Purge all Sound_Source_HW and all playing_and_destroying (created by play_sound(...))
Positional_Replacement_Policy(const Point3f &listener_position_)
static void remove_post_reinit(Event::Handler *const &handler)
void play()
Begin playing or unpause the Sound_Source.
void unpause_all()
Unpause all paused Sound_Sources.
A 3D Point represented with floats.
void update()
Redistribute hardware Sound_Sources according to the Replacement_Policy. Newer Sound_Sources are impl...
Sound & get_Sound()
Get access to the singleton.
friend void play_sound(const String &sound_name, const float &pitch, const float &gain, const Point3f &position, const Vector3f &velocity)
A Featureful 3-Space Vector Class.
static void remove_pre_uninit(Event::Handler *const &handler)
void give_Replacement_Policy(Replacement_Policy *const &replacement_policy)
Give the Sound_Source_Pool a new Replacement_Policy (which it will delete later)
static Sound_Source_Pool & get()
void ZENI_LOGD(const Zeni::String &str)
virtual bool operator()(const Sound_Source &lhs, const Sound_Source &rhs) const
Positional Priority Sort: Playing/Not-Playing, Priority, Computed Gain, Recency.
float calculate_gain(const Point3f &listener_position) const
Get the actual volume gain, given gain+position+near_clamp+far_clamp+rolloff.
This class manages Sound_Sources.
bool is_playing() const
Check to see if the Sound_Source is playing.
const Replacement_Policy & get_Replacement_Policy() const
Get the Replacement_Policy.
static void sound_quicksort(const std::vector< Sound_Source * >::iterator &begin, const std::vector< Sound_Source * >::iterator &end, const Sound_Source_Pool::Replacement_Policy &policy)
GLsizei GLsizei GLchar * source
bool is_assigned() const
Check to see if the Sound_Source is assigned to actual hardware.
void lend_Replacement_Policy(Replacement_Policy *const &replacement_policy)
Give the Sound_Source_Pool a new Replacement_Policy (which it will NEVER delete)
friend class Sound_Source