Panda3D
openalAudioSound.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 openalAudioSound.h
10  * @author Ben Buchwald <bb2@alumni.cmu.edu>
11  */
12 
13 #ifndef __OPENAL_AUDIO_SOUND_H__
14 #define __OPENAL_AUDIO_SOUND_H__
15 
16 #include "pandabase.h"
17 
18 #include "audioSound.h"
19 #include "movieAudioCursor.h"
20 #include "trueClock.h"
21 #include "openalAudioManager.h"
22 
23 // OSX uses the OpenAL framework
24 #ifdef HAVE_OPENAL_FRAMEWORK
25  #include <OpenAL/al.h>
26  #include <OpenAL/alc.h>
27 #else
28  #include <AL/al.h>
29  #include <AL/alc.h>
30 #endif
31 
32 class EXPCL_OPENAL_AUDIO OpenALAudioSound : public AudioSound {
33  friend class OpenALAudioManager;
34 
35 public:
36 
38 
39  // For best compatibility, set the loop_count, start_time, volume, and
40  // balance, prior to calling play(). You may set them while they're
41  // playing, but it's implementation specific whether you get the results.
42  void play();
43  void stop();
44 
45  // loop: false = play once; true = play forever. inits to false.
46  void set_loop(bool loop=true);
47  bool get_loop() const;
48 
49  // loop_count: 0 = forever; 1 = play once; n = play n times. inits to 1.
50  void set_loop_count(unsigned long loop_count=1);
51  unsigned long get_loop_count() const;
52 
53  // 0 = beginning; length() = end. inits to 0.0.
54  void set_time(PN_stdfloat time=0.0);
55  PN_stdfloat get_time() const;
56 
57  // 0 = minimum; 1.0 = maximum. inits to 1.0.
58  void set_volume(PN_stdfloat volume=1.0);
59  PN_stdfloat get_volume() const;
60 
61  // -1.0 is hard left 0.0 is centered 1.0 is hard right inits to 0.0.
62  void set_balance(PN_stdfloat balance_right=0.0);
63  PN_stdfloat get_balance() const;
64 
65  // play_rate is any positive float value. inits to 1.0.
66  void set_play_rate(PN_stdfloat play_rate=1.0f);
67  PN_stdfloat get_play_rate() const;
68 
69  // inits to manager's state.
70  void set_active(bool active=true);
71  bool get_active() const;
72 
73  // This is the string that throw_event() will throw when the sound finishes
74  // playing. It is not triggered when the sound is stopped with stop().
75  void set_finished_event(const std::string& event);
76  const std::string& get_finished_event() const;
77 
78  const std::string &get_name() const;
79 
80  // return: playing time in seconds.
81  PN_stdfloat length() const;
82 
83  // Controls the position of this sound's emitter. pos is a pointer to an
84  // xyz triplet of the emitter's position. vel is a pointer to an xyz
85  // triplet of the emitter's velocity.
86  void set_3d_attributes(PN_stdfloat px, PN_stdfloat py, PN_stdfloat pz, PN_stdfloat vx, PN_stdfloat vy, PN_stdfloat vz);
87  void get_3d_attributes(PN_stdfloat *px, PN_stdfloat *py, PN_stdfloat *pz, PN_stdfloat *vx, PN_stdfloat *vy, PN_stdfloat *vz);
88 
89  void set_3d_min_distance(PN_stdfloat dist);
90  PN_stdfloat get_3d_min_distance() const;
91 
92  void set_3d_max_distance(PN_stdfloat dist);
93  PN_stdfloat get_3d_max_distance() const;
94 
95  void set_3d_drop_off_factor(PN_stdfloat factor);
96  PN_stdfloat get_3d_drop_off_factor() const;
97 
98  AudioSound::SoundStatus status() const;
99 
100  void finished();
101 
102 private:
104  MovieAudio *movie,
105  bool positional,
106  int mode);
107  INLINE void set_calibrated_clock(double rtc, double t, double playrate);
108  INLINE double get_calibrated_clock(double rtc) const;
109  void correct_calibrated_clock(double rtc, double t);
110  void cache_time(double rtc);
111  void cleanup();
112  void restart_stalled_audio();
113  void delete_queued_buffers();
114  ALuint make_buffer(int samples, int channels, int rate, unsigned char *data);
115  void queue_buffer(ALuint buffer, int samples, int loop_index, double time_offset);
116  int read_stream_data(int bytelen, unsigned char *data);
117  void pull_used_buffers();
118  void push_fresh_buffers();
119  INLINE bool require_sound_data();
120  INLINE void release_sound_data(bool force);
121 
122  INLINE bool is_valid() const;
123  INLINE bool is_playing() const;
124  INLINE bool has_sound_data() const;
125 
126 private:
127 
128  PT(MovieAudio) _movie;
129  OpenALAudioManager::SoundData *_sd;
130 
131  struct QueuedBuffer {
132  ALuint _buffer;
133  int _samples;
134  int _loop_index;
135  double _time_offset;
136  };
137 
138  int _playing_loops;
139  PN_stdfloat _playing_rate;
140 
141  pdeque<QueuedBuffer> _stream_queued;
142  int _loops_completed;
143 
144  ALuint _source;
145  PT(OpenALAudioManager) _manager;
146 
147  PN_stdfloat _volume; // 0..1.0
148  PN_stdfloat _balance; // -1..1
149  PN_stdfloat _play_rate; // 0..1.0
150 
151  bool _positional;
152  ALfloat _location[3];
153  ALfloat _velocity[3];
154 
155  PN_stdfloat _min_dist;
156  PN_stdfloat _max_dist;
157  PN_stdfloat _drop_off_factor;
158 
159  double _length;
160  int _loop_count;
161 
162  int _desired_mode;
163 
164  // The calibrated clock is initialized when the sound starts playing, and is
165  // periodically corrected thereafter.
166  double _calibrated_clock_base;
167  double _calibrated_clock_scale;
168  double _calibrated_clock_decavg;
169 
170  // The start_time field affects the next call to play.
171  double _start_time;
172 
173  // The current_time field is updated every frame during the AudioManager
174  // update. Updates need to be atomic, because get_time can be called in the
175  // cull thread.
176  PN_stdfloat _current_time;
177 
178  // This is the string that throw_event() will throw when the sound finishes
179  // playing. It is not triggered when the sound is stopped with stop().
180  std::string _finished_event;
181 
182  Filename _basename;
183 
184  // _active is for things like a 'turn off sound effects' in a preferences
185  // pannel. _active is not about whether a sound is currently playing. Use
186  // status() for info on whether the sound is playing.
187  bool _active;
188  bool _paused;
189 
190 public:
191  static TypeHandle get_class_type() {
192  return _type_handle;
193  }
194  static void init_type() {
195  AudioSound::init_type();
196  register_type(_type_handle, "OpenALAudioSound", AudioSound::get_class_type());
197  }
198  virtual TypeHandle get_type() const {
199  return get_class_type();
200  }
201  virtual TypeHandle force_init_type() {
202  init_type();
203  return get_class_type();
204  }
205 
206 private:
207  static TypeHandle _type_handle;
208 };
209 
210 #include "openalAudioSound.I"
211 
212 #endif /* __OPENAL_AUDIO_SOUND_H__ */
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
virtual void set_volume(PN_stdfloat)
Sets listener gain.
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.
PN_stdfloat get_play_rate() const
get the overall speed/pitch/play rate
virtual PN_stdfloat get_volume() const
Gets listener gain.
virtual bool is_valid()
This is mostly for debugging, but it it could be used to detect errors in a release build if you don'...
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:39
void set_play_rate(PN_stdfloat play_rate)
set the overall play rate
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
virtual void set_active(bool)
Turn on/off Warning: not implemented.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.