openalAudioSound.h 6.1 KB

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