Panda3D
milesAudioManager.h
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file milesAudioManager.h
10  * @author skyler
11  * @date 2001-06-06
12  * Prior system by: cary
13  */
14 
15 #ifndef __MILES_AUDIO_MANAGER_H__ //[
16 #define __MILES_AUDIO_MANAGER_H__
17 
18 #include "pandabase.h"
19 #ifdef HAVE_RAD_MSS //[
20 
21 #include "audioManager.h"
22 #include "pset.h"
23 #include "pmap.h"
24 #include "pdeque.h"
25 #include "pvector.h"
26 #include "thread.h"
27 #include "pmutex.h"
28 #include "lightReMutex.h"
29 #include "conditionVar.h"
30 #include "vector_uchar.h"
31 
32 #include <mss.h>
33 
34 class MilesAudioSound;
35 
36 class EXPCL_MILES_AUDIO MilesAudioManager: public AudioManager {
37 public:
38  // See AudioManager.h for documentation.
39 
40  MilesAudioManager();
41  virtual ~MilesAudioManager();
42 
43  virtual void shutdown();
44 
45  virtual bool is_valid();
46 
47  virtual PT(AudioSound) get_sound(const Filename &file_name, bool positional = false, int mode=SM_heuristic);
48  virtual PT(AudioSound) get_sound(MovieAudio *sound, bool positional = false, int mode=SM_heuristic);
49  virtual void uncache_sound(const Filename &file_name);
50  virtual void clear_cache();
51  virtual void set_cache_limit(unsigned int count);
52  virtual unsigned int get_cache_limit() const;
53 
54  virtual void set_volume(PN_stdfloat volume);
55  virtual PN_stdfloat get_volume() const;
56 
57  void set_play_rate(PN_stdfloat play_rate);
58  PN_stdfloat get_play_rate() const;
59 
60  virtual void set_active(bool active);
61  virtual bool get_active() const;
62 
63  virtual void set_concurrent_sound_limit(unsigned int limit = 0);
64  virtual unsigned int get_concurrent_sound_limit() const;
65 
66  virtual void reduce_sounds_playing_to(unsigned int count);
67  virtual void stop_all_sounds();
68 
69  virtual void update();
70 
71  // Tell the manager that the sound dtor was called.
72  void release_sound(MilesAudioSound *audioSound);
73  void cleanup();
74 
75  // 3D spatialized sound support. Spatialized sound was originally added for
76  // FMOD, so there are parts of the interface in the Miles implementation
77  // that are a little more awkward than 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=nullptr, LVecBase3 *speaker3=nullptr, LVecBase3 *speaker4=nullptr, LVecBase3 *speaker5=nullptr, LVecBase3 *speaker6=nullptr, LVecBase3 *speaker7=nullptr, LVecBase3 *speaker8=nullptr, LVecBase3 *speaker9=nullptr);
87 
88  virtual void output(std::ostream &out) const;
89  virtual void write(std::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 std::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  vector_uchar _raw_data;
132  bool _has_length;
133  PN_stdfloat _length; // in seconds.
134  };
135  typedef pmap<std::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 std::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 //]
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is our own Panda specialization on the default STL map.
Definition: pmap.h:49
void register_type(TypeHandle &type_handle, const std::string &name)
This inline function is just a convenient way to call TypeRegistry::register_type(),...
Definition: register_type.I:22
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A lightweight reentrant mutex.
Definition: lightReMutex.h:30
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A standard mutex, or mutual exclusion lock.
Definition: pmutex.h:38
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is our own Panda specialization on the default STL deque.
Definition: pdeque.h:36
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is our own Panda specialization on the default STL vector.
Definition: pvector.h:42
A condition variable, usually used to communicate information about changing state to a thread that i...
Definition: conditionVar.h:41
virtual void update()
Must be called every frame.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:39
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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:46
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is our own Panda specialization on the default STL set.
Definition: pset.h:49
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
A MovieAudio is actually any source that provides a sequence of audio samples.
Definition: movieAudio.h:44
virtual void set_speaker_configuration(LVecBase3 *speaker1, LVecBase3 *speaker2=nullptr, LVecBase3 *speaker3=nullptr, LVecBase3 *speaker4=nullptr, LVecBase3 *speaker5=nullptr, LVecBase3 *speaker6=nullptr, LVecBase3 *speaker7=nullptr, LVecBase3 *speaker8=nullptr, LVecBase3 *speaker9=nullptr)
For use only with Miles.