1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
|
/* -*-c++-*- $Id$ */
/**
* OsgAL - OpenSceneGraph Audio Library
* Copyright (C) 2004 VRlab, Ume University
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
#ifndef __OAL_SoundManager_h__
#define __OAL_SoundManager_h__
#include <string>
#include <map>
#include <queue>
#include <functional>
#include <stack>
#include <osg/ref_ptr>
#include "osg/Timer"
#include "osg/Matrix"
#include "osgAL/Export"
#include "osgAL/SoundState"
#include "openalpp/alpp.h"
#include "openalpp/filestream.h"
namespace osgAL
{
/// A SoundManager handles the sound system.
/*!
This class initialises the Sound system. It also keeps track of loaded sound samples.
It is based on the Singleton Design pattern.
To manage a limited resource of Sound sources (which is usually around 32)
a schema has to be set up.
This class initially initializes sound sources and places them into a pool of available sources.
Whenever someone calls allocateSource() the sound manager tries to free a source
and return a pointer to it. If there are no sources available it goes through the
active sources and tries to find one with a lower priority. If it still fails, an exception is
thrown.
Otherwise the found source is returned.
The allocateSource() has an argument indicating that if the source should be placed in the
list of active sources. If this argument is false, the caller is responsible to
call releaseSource() when finished using the source.
SoundManager also handles SoundStates.
Whenever pushSoundEvent() is called, a FlyWeight set of SoundStates is inquired for a free SoundState.
The argument of pushSoundEvent() is copied to the free SoundState and that SoundState
is then pushed to the queue of waiting SoundStates.
When SoundManager::update() is called, the queue of waiting SoundStates is inquired and for each
SoundState a Source is allocated.
When the SoundManager realizes that a SoundState is not playing anymore, it is removed from
the list of active SoundStates and put back in to the FlyWeight set of available SoundStates.
*/
class OSGAL_EXPORT SoundManager : public osg::Referenced
{
public:
/// Return the singleton object
static SoundManager* instance( void );
/*! Deallocates any allocated memory, this method have to be called prior to the end of main()
OpenAL doesnt like destruction of sources outside the main() function.
*/
virtual void shutdown();
/// Return a pointer to the singleton object
/// Returns true if the SoundManager is initalized
bool initialized() { return m_initialized; }
/*!
Push a SoundState to the queue of waiting SoundStates
When the update method later on is called, this queue is inquired and each
waiting SoundEvent will be put in the active state and put to a list of active
SoundStates with a Source allocated.
\param state - The state that will be pushed to the waiting queue
\param priority - The priority of the state, 0 lowest
*/
bool pushSoundEvent(SoundState *state, unsigned int priority=0);
/*!
Return a pointer to a Sample.
Each Sample will be stored in a cache with its associated path (if add_to_cache is true)
Each call to getSample will first look in the cache and try to find the
path there. If it can be found, it will return to that Sample.
Otherwise it will be loaded from disk.
*/
openalpp::Sample* getSample( const std::string& path, bool add_to_cache=true );
/*!
Clear the sample cache with all loaded samples.
*/
void clearSampleCache(void) { m_sample_cache.clear(); }
/*!
Return a pointer to a Stream
Each Stream will be stored in a cache with its associated path.
Each call to getStream will first look in the cache and try to find the
path there. If it can be found, it will return to that Stream.
Otherwise it will be loaded from disk.
*/
openalpp::Stream* getStream( const std::string& path, bool add_to_cache=true );
/*!
Clear the Stream cache with all loaded streams.
*/
void clearStreamCache(void) { m_stream_cache.clear(); }
/*!
Return a pointer to the SoundState with the name id.
\return Null if no matching SoundState can be found.
*/
SoundState *findSoundState(const std::string& id);
/*!
Puts the source into the list of available sound sources
*/
void releaseSource(openalpp::Source *source);
/// Initializes the SoundManager
void init(unsigned int num_soundsources, float sound_velocity=343);
/*!
*/
void update();
/// Return a pointer to the listener
openalpp::Listener *getListener();
/// Return a pointer to the Sound environment
openalpp::AudioEnvironment *getEnvironment();
/*!
Add a SoundState to the list of existing sound states
*/
void addSoundState(osgAL::SoundState *state) { m_sound_states[state->getName()] = state; }
/// Removes the sound state from the list of existing soundstates
bool removeSoundState(const std::string& id);
/*!
Removes the sound state from the list of existing soundstates
*/
bool removeSoundState(osgAL::SoundState *state);
/*!
To make it possible to flip left and right ear for the listner this vector specified
will be multiplied with the up vector everytime the orientation of the listener is calculated
The default value is (1, 1, 1). Make sure not to set anyone to zero, this will be caught
and an exception will be thrown.
*/
void setListenerDirection(const osg::Vec3& dir);
const osg::Vec3& getListenerDirection() const { return m_listener_direction; }
/// Set the transformation matrix for the listener
void setListenerMatrix( const osg::Matrix& matrix);
/// Return the current listener matrix
const osg::Matrix &getListenerMatrix( ) const { return m_listener_matrix; }
/*!
Tries to find an available sound Source
\param if mutual_use then the source is registrated in the soundmanager as a source that can be
reused by a call to getSource with higher priority.
If on the other hand mutual_use is false, then it is allocated
\return Pointer to an available sound Source
*/
openalpp::Source *allocateSource(unsigned int priority, bool mutual_use=true) { return getSource(priority, mutual_use); }
/// Set the maximum velocity used in doppler calculation
void setMaxVelocity(float vel) { m_max_velocity = (float)fabs(vel); }
/// Returns the maximum velocity used in doppler calculation
float getMaxVelocity() const { return m_max_velocity; }
/*!
Returns the number of free and available SoundSoures for allocation.
Should be between 0 .. getNumSources()
*/
unsigned int getNumAvailableSources() { return m_available_soundsources.size(); }
/*!
Returns the number of initialized SoundSources.
Should be between 0..
*/
unsigned int getNumSources() { return m_soundsources.size(); }
/*!
Returns the number of SoundSources currently in use.
Should be between 0..getNumSources()
*/
unsigned int getNumActiveSources() { return m_active_soundsources.size(); }
/// Set if the velocity is clamped (using the MaxVelocity attribute)
void setClampVelocity(bool c) { m_clamp_velocity = c; }
/// Get true if the velocity is clamped (using the MaxVelocity attribute)
bool getClampVelocity() { return m_clamp_velocity; }
/// Get const true if the velocity is clamped (using the MaxVelocity attribute)
const bool getClampVelocity() const { return m_clamp_velocity; }
/// Set the update frequency for both sources and listener, to update pos, dir and occlusions
void setUpdateFrequency( float frequency ) {
if(frequency < 0) m_update_frequency = 1/100.0;
else m_update_frequency = frequency;
}
/// Get the update frequency for both sources and listener, to update pos, dir and occlusions
float getUpdateFrequency() { return m_update_frequency; }
/// Get const the update frequency for both sources and listener, to update pos, dir and occlusions
const float getUpdateFrequency() const { return m_update_frequency; }
private:
openalpp::Source *getSource(unsigned int priority, bool registrate_as_active=true, int depth=0);
/// Destructor
~SoundManager();
/// Default Constructor
SoundManager( void );
/// Class that handles all the soundstates. See FlyWeight Design pattern
class SoundStateFlyWeight : public osg::Referenced {
public:
SoundStateFlyWeight(unsigned int no_states);
virtual ~SoundStateFlyWeight();
SoundState *getSoundState(SoundState *state);
void releaseSoundState(SoundState *state);
void clean() { m_sound_states.resize(0); }
private:
std::vector< osg::ref_ptr<SoundState> > m_sound_states;
std::stack< SoundState *> m_available_states;
};
osg::ref_ptr<SoundStateFlyWeight> m_sound_state_FlyWeight;
void resetSource(openalpp::Source *source);
typedef std::map<std::string, openalpp::ref_ptr<openalpp::Sample> > SampleMap;
typedef SampleMap::iterator SampleMapIterator;
typedef SampleMap::value_type SampleMapValType;
SampleMap m_sample_cache;
typedef std::map<std::string, openalpp::ref_ptr<openalpp::FileStream> > StreamMap;
typedef StreamMap::iterator StreamMapIterator;
typedef StreamMap::value_type StreamMapValType;
StreamMap m_stream_cache;
openalpp::ref_ptr<openalpp::Listener> m_listener;
openalpp::ref_ptr<openalpp::AudioEnvironment> m_sound_environment;
typedef std::vector<openalpp::ref_ptr<openalpp::Source> > SourceVector;
typedef std::vector<std::pair< unsigned int, openalpp::ref_ptr<openalpp::Source> > > ActiveSourceVector;
ActiveSourceVector m_active_soundsources;
SourceVector m_soundsources;
// A matrix containing the position and orientation of the listener
osg::Matrix m_listener_matrix;
SourceVector m_available_soundsources;
typedef std::map<std::string, osg::ref_ptr<SoundState> > SoundStateMap;
SoundStateMap m_sound_states;
class SoundStateQueueItem {
public:
SoundStateQueueItem(unsigned int prio, SoundState *state) : m_prio(prio), m_state(state) {}
bool operator <(const SoundStateQueueItem& item) const { return m_prio < item.m_prio; }
bool operator ==(const SoundStateQueueItem& item) { return m_prio == item.m_prio; }
SoundState *getState() const { return m_state; }
unsigned int getPriority() { return m_prio; }
private:
unsigned int m_prio;
SoundState *m_state;
};
typedef std::priority_queue<SoundStateQueueItem> SoundStateQueue;
SoundStateQueue m_sound_state_queue;
typedef std::vector<osg::ref_ptr<SoundState> > SoundStateVector;
SoundStateVector m_active_sound_states;
bool m_initialized;
osg::Vec3 m_last_pos;
float m_max_velocity;
osg::Timer m_timer;
osg::Timer_t m_last_tick;
bool m_first_run;
bool m_clamp_velocity;
float m_update_frequency;
osg::Vec3 m_listener_direction;
};
inline openalpp::Listener *SoundManager::getListener()
{
if (!m_initialized) {
throw std::runtime_error("SoundManager::getListener(): No sound system inialized");
return 0L;
}
return m_listener.get();
}
inline openalpp::AudioEnvironment *SoundManager::getEnvironment()
{
if (!m_initialized) {
throw std::runtime_error("SoundManager::getListener(): Soundsystem is not inialized");
return 0L;
}
return m_sound_environment.get();
}
} // namespace osgAL
#define g_SoundManager osgAL::SoundManager::instance()
#endif
/*------------------------------------------
* $Source$
* $Revision$
* $Date$
* $Author$
$Locker$
Author: Anders Backman
VRlab, Ume University, 2004
* $Log$
* Revision 1.2 2006/07/24 15:31:10 loic
* autotools
*
* Revision 1.1.1.3 2005/09/07 16:11:36 loic
* upstream update
*
* Revision 1.7 2005/05/27 07:21:35 vr-anders
* Alberto Jaspe (videaLAB / University of La Corua)
* - Several miscelaneus bugfixes.
* - Added support for reading/writing from/to osg files.
* - Important bugfixes in doppler issues.
* - Using just one update frequency for listener and sources, in SoundManager.
* - Using a state for clamp function for max velocites in both listener and sources,
* disabled by default.
* - Replaced the FilePath Manager for the osgDB support.
* - Added a new example called osgalviewer, that acts like osgviewer, but with some
* features oriented to treat with Sound Nodes.
* - Compiled and tested with OSG 0.9.9.
*
* Revision 1.6 2005/01/31 10:49:53 vrlab
* *** empty log message ***
*
* Revision 1.5 2004/11/11 07:47:35 andersb
* Added a simple occlude method for occluding soundsources against geometry
*
* Revision 1.4 2004/04/20 12:26:03 andersb
* Added SoundRoot
*
* Revision 1.3 2004/03/05 09:42:30 andersb
* *** empty log message ***
*
* Revision 1.2 2004/03/03 07:50:49 andersb
* *** empty log message ***
*
* Revision 1.1.1.1 2004/03/02 07:20:58 andersb
* Initial Version of the osgAL library
*
*
--------------------------------------------*/
|