Panda3D
milesAudioManager.h
1 // Filename: milesAudioManager.h
2 // Created by: skyler (June 6, 2001)
3 // Prior system by: cary
4 //
5 ////////////////////////////////////////////////////////////////////
6 //
7 // PANDA 3D SOFTWARE
8 // Copyright (c) Carnegie Mellon University. All rights reserved.
9 //
10 // All use of this software is subject to the terms of the revised BSD
11 // license. You should have received a copy of this license along
12 // with this source code in a file named "LICENSE."
13 //
14 ////////////////////////////////////////////////////////////////////
15 
16 #ifndef __MILES_AUDIO_MANAGER_H__ //[
17 #define __MILES_AUDIO_MANAGER_H__
18 
19 #include "pandabase.h"
20 #ifdef HAVE_RAD_MSS //[
21 
22 #include "audioManager.h"
23 #include "mss.h"
24 #include "pset.h"
25 #include "pmap.h"
26 #include "pdeque.h"
27 #include "pvector.h"
28 #include "thread.h"
29 #include "pmutex.h"
30 #include "lightReMutex.h"
31 #include "conditionVar.h"
32 
33 class MilesAudioSound;
34 
35 class EXPCL_MILES_AUDIO MilesAudioManager: public AudioManager {
36 public:
37  // See AudioManager.h for documentation.
38 
39  MilesAudioManager();
40  virtual ~MilesAudioManager();
41 
42  virtual void shutdown();
43 
44  virtual bool is_valid();
45 
46  virtual PT(AudioSound) get_sound(const string &file_name, bool positional = false, int mode=SM_heuristic);
47  virtual PT(AudioSound) get_sound(MovieAudio *sound, bool positional = false, int mode=SM_heuristic);
48  virtual void uncache_sound(const string &file_name);
49  virtual void clear_cache();
50  virtual void set_cache_limit(unsigned int count);
51  virtual unsigned int get_cache_limit() const;
52 
53  virtual void set_volume(PN_stdfloat volume);
54  virtual PN_stdfloat get_volume() const;
55 
56  void set_play_rate(PN_stdfloat play_rate);
57  PN_stdfloat get_play_rate() const;
58 
59  virtual void set_active(bool active);
60  virtual bool get_active() const;
61 
62  virtual void set_concurrent_sound_limit(unsigned int limit = 0);
63  virtual unsigned int get_concurrent_sound_limit() const;
64 
65  virtual void reduce_sounds_playing_to(unsigned int count);
66  virtual void stop_all_sounds();
67 
68  virtual void update();
69 
70  // Tell the manager that the sound dtor was called.
71  void release_sound(MilesAudioSound *audioSound);
72  void cleanup();
73 
74  // 3D spatialized sound support.
75  // Spatialized sound was originally added for FMOD, so there are parts of the
76  // interface in the Miles implementation that are a little more awkward than
77  // they would be otherwise.
78  virtual void audio_3d_set_listener_attributes(PN_stdfloat px, PN_stdfloat py, PN_stdfloat pz, PN_stdfloat vx, PN_stdfloat xy, PN_stdfloat xz, PN_stdfloat fx, PN_stdfloat fy, PN_stdfloat fz, PN_stdfloat ux, PN_stdfloat uy, PN_stdfloat uz);
79  virtual void audio_3d_get_listener_attributes(PN_stdfloat *px, PN_stdfloat *py, PN_stdfloat *pz, PN_stdfloat *vx, PN_stdfloat *vy, PN_stdfloat *vz, PN_stdfloat *fx, PN_stdfloat *fy, PN_stdfloat *fz, PN_stdfloat *ux, PN_stdfloat *uy, PN_stdfloat *uz);
80  virtual void audio_3d_set_distance_factor(PN_stdfloat factor);
81  virtual PN_stdfloat audio_3d_get_distance_factor() const;
82  virtual void audio_3d_set_doppler_factor(PN_stdfloat factor);
83  virtual PN_stdfloat audio_3d_get_doppler_factor() const;
84  virtual void audio_3d_set_drop_off_factor(PN_stdfloat factor);
85  virtual PN_stdfloat audio_3d_get_drop_off_factor() const;
86  virtual void set_speaker_configuration(LVecBase3 *speaker1, LVecBase3 *speaker2=NULL, LVecBase3 *speaker3=NULL, LVecBase3 *speaker4=NULL, LVecBase3 *speaker5=NULL, LVecBase3 *speaker6=NULL, LVecBase3 *speaker7=NULL, LVecBase3 *speaker8=NULL, LVecBase3 *speaker9=NULL);
87 
88  virtual void output(ostream &out) const;
89  virtual void write(ostream &out) const;
90 
91 private:
92  bool do_is_valid();
93  void do_reduce_sounds_playing_to(unsigned int count);
94  void do_clear_cache();
95 
96  void start_service_stream(HSTREAM stream);
97  void stop_service_stream(HSTREAM stream);
98 
99  void most_recently_used(const string &path);
100  void uncache_a_sound();
101 
102  void starting_sound(MilesAudioSound *audio);
103  void stopping_sound(MilesAudioSound *audio);
104 
105  class SoundData;
106  PT(SoundData) load(const Filename &file_name);
107 
108  void thread_main(volatile bool &keep_running);
109  void do_service_streams();
110 
111 private:
112  class StreamThread : public Thread {
113  public:
114  StreamThread(MilesAudioManager *mgr);
115  virtual void thread_main();
116 
117  MilesAudioManager *_mgr;
118  volatile bool _keep_running;
119  };
120 
121  // The sound cache:
122  class SoundData : public ReferenceCount {
123  public:
124  SoundData();
125  ~SoundData();
126  PN_stdfloat get_length();
127  void set_length(PN_stdfloat length);
128 
129  Filename _basename;
130  S32 _file_type;
131  pvector<unsigned char> _raw_data;
132  bool _has_length;
133  PN_stdfloat _length; // in seconds.
134  };
135  typedef pmap<string, PT(SoundData) > SoundMap;
136  SoundMap _sounds;
137 
138  typedef pset<MilesAudioSound *> AudioSet;
139  // The offspring of this manager:
140  AudioSet _sounds_on_loan;
141 
142  typedef pset<MilesAudioSound *> SoundsPlaying;
143  // The sounds from this manager that are currently playing:
144  SoundsPlaying _sounds_playing;
145 
146  // The Least Recently Used mechanism:
147  typedef pdeque<const string *> LRU;
148  LRU _lru;
149  // State:
150  PN_stdfloat _volume;
151  PN_stdfloat _play_rate;
152  bool _active;
153  int _cache_limit;
154  bool _cleanup_required;
155  unsigned int _concurrent_sound_limit;
156 
157  bool _is_valid;
158  bool _hasMidiSounds;
159 
160  // This mutex protects everything above.
161  LightReMutex _lock;
162  bool _sounds_finished;
163 
164  typedef pvector<HSTREAM> Streams;
165  PT(StreamThread) _stream_thread;
166  Streams _streams;
167  Mutex _streams_lock;
168  ConditionVar _streams_cvar;
169 
170 public:
171  static TypeHandle get_class_type() {
172  return _type_handle;
173  }
174  static void init_type() {
175  AudioManager::init_type();
176  register_type(_type_handle, "MilesAudioManager",
177  AudioManager::get_class_type());
178  }
179  virtual TypeHandle get_type() const {
180  return get_class_type();
181  }
182  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
183 
184 private:
185  static TypeHandle _type_handle;
186 
187  friend class MilesAudioSound;
188  friend class MilesAudioSample;
189  friend class MilesAudioSequence;
190  friend class MilesAudioStream;
191 };
192 
193 EXPCL_MILES_AUDIO AudioManager *Create_MilesAudioManager();
194 
195 
196 #endif //]
197 
198 #endif //]
199 
200 
This is the base class for all three-component vectors and points.
Definition: lvecBase3.h:105
This is our own Panda specialization on the default STL map.
Definition: pmap.h:52
A lightweight reentrant mutex.
Definition: lightReMutex.h:34
A standard mutex, or mutual exclusion lock.
Definition: pmutex.h:44
This is our own Panda specialization on the default STL deque.
Definition: pdeque.h:38
A condition variable, usually used to communicate information about changing state to a thread that i...
Definition: conditionVar.h:47
virtual void update()
Must be called every frame.
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:44
virtual void shutdown()
Call this at exit time to shut down the audio system.
A base class for all things that want to be reference-counted.
A thread; that is, a lightweight process.
Definition: thread.h:51
This is our own Panda specialization on the default STL set.
Definition: pset.h:52
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
A MovieAudio is actually any source that provides a sequence of audio samples.
Definition: movieAudio.h:48
virtual void set_speaker_configuration(LVecBase3 *speaker1, LVecBase3 *speaker2=NULL, LVecBase3 *speaker3=NULL, LVecBase3 *speaker4=NULL, LVecBase3 *speaker5=NULL, LVecBase3 *speaker6=NULL, LVecBase3 *speaker7=NULL, LVecBase3 *speaker8=NULL, LVecBase3 *speaker9=NULL)
For use only with Miles.