Panda3D
openalAudioSound.I
1 // Filename: openalAudioSound.I
2 // Created by: Ben Buchwald <bb2@alumni.cmu.edu>
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 ////////////////////////////////////////////////////////////////////
16 // Function: OpenALAudioSound::set_calibrated_clock
17 // Access: public
18 // Description: Sets the sound's calibrated clock.
19 //
20 // OpenAL is not very accurate at reporting how much
21 // time has elapsed within a buffer. However, it does
22 // accurately report when it has finished playing a
23 // buffer. So we use a hybrid clock algorithm.
24 // When OpenAL is in the middle of a buffer,
25 // we use a real-time-clock to estimate how far the
26 // sound has gotten. Each time OpenAL reaches the end
27 // of a buffer (which it does every 1/4 second or so),
28 // we calibrate our real-time-clock by speeding it up
29 // or slowing it down.
30 ////////////////////////////////////////////////////////////////////
31 INLINE void OpenALAudioSound::
32 set_calibrated_clock(double rtc, double t, double accel) {
33  _calibrated_clock_scale = _playing_rate * accel;
34  _calibrated_clock_base = rtc - (t / _calibrated_clock_scale);
35 }
36 
37 ////////////////////////////////////////////////////////////////////
38 // Function: OpenALAudioSound::get_calibrated_clock
39 // Access: public
40 // Description: Returns the value of the calibrated clock.
41 ////////////////////////////////////////////////////////////////////
42 INLINE double OpenALAudioSound::
43 get_calibrated_clock(double rtc) const {
44  return (rtc - _calibrated_clock_base) * _calibrated_clock_scale;
45 }
46 
47 ////////////////////////////////////////////////////////////////////
48 // Function: OpenALAudioSound::require_sound_data
49 // Access: Private
50 // Description: Makes sure the sound data record is present,
51 // and if not, obtains it.
52 ////////////////////////////////////////////////////////////////////
53 void OpenALAudioSound::
54 require_sound_data() {
55  if (_sd==0) {
56  _sd = _manager->get_sound_data(_movie, _desired_mode);
57  if (_sd==0) {
58  audio_error("Could not open audio " << _movie->get_filename());
59  cleanup();
60  }
61  }
62 }
63 
64 ////////////////////////////////////////////////////////////////////
65 // Function: OpenALAudioSound::release_sound_data
66 // Access: Private
67 // Description: Checks if the sound data record is present and
68 // releasable, and if so, releases it.
69 ////////////////////////////////////////////////////////////////////
70 void OpenALAudioSound::
71 release_sound_data() {
72  if ((_sd!=0) && (!_movie->get_filename().empty())) {
73  _manager->decrement_client_count(_sd);
74  _sd = 0;
75  }
76 }