Panda3D
milesAudioSound.cxx
1 // Filename: milesAudioSound.cxx
2 // Created by: drose (30Jul07)
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 #include "milesAudioSound.h"
16 #ifdef HAVE_RAD_MSS //[
17 
18 #include "milesAudioManager.h"
19 
20 TypeHandle MilesAudioSound::_type_handle;
21 
22 #undef miles_audio_debug
23 
24 #ifndef NDEBUG //[
25 #define miles_audio_debug(x) \
26  audio_debug("MilesAudioSound \""<<get_name()<<"\" "<< x )
27 #else //][
28 #define miles_audio_debug(x) ((void)0)
29 #endif //]
30 
31 ////////////////////////////////////////////////////////////////////
32 // Function: MilesAudioSound::Constructor
33 // Access: Private
34 // Description:
35 ////////////////////////////////////////////////////////////////////
36 MilesAudioSound::
37 MilesAudioSound(MilesAudioManager *manager,
38  const string &file_name) :
39  _manager(manager),
40  _file_name(file_name),
41  _volume(1.0f), _balance(0), _play_rate(1.0f),
42  _loop_count(1),
43  _active(true),
44  _paused(false),
45  _start_time(0.0f),
46  _got_start_time(false)
47 {
48  nassertv(!file_name.empty());
49 }
50 
51 ////////////////////////////////////////////////////////////////////
52 // Function: MilesAudioSound::set_loop
53 // Access: Public, Virtual
54 // Description:
55 ////////////////////////////////////////////////////////////////////
56 void MilesAudioSound::
57 set_loop(bool loop) {
58  // loop count of 0 means always loop
59  set_loop_count((loop)?0:1);
60 }
61 
62 ////////////////////////////////////////////////////////////////////
63 // Function: MilesAudioSound::get_loop
64 // Access: Public, Virtual
65 // Description:
66 ////////////////////////////////////////////////////////////////////
67 bool MilesAudioSound::
68 get_loop() const {
69  return (_loop_count == 0);
70 }
71 
72 ////////////////////////////////////////////////////////////////////
73 // Function: MilesAudioSound::
74 // Access: Public, Virtual
75 // Description:
76 ////////////////////////////////////////////////////////////////////
77 void MilesAudioSound::
78 set_loop_count(unsigned long loop_count) {
79  if (_loop_count != loop_count) {
80  _loop_count = loop_count;
81  if (status() == PLAYING) {
82  // hack:
83  // For now, the loop count is picked up when the sound starts playing.
84  // There may be a way to change the loop count of a playing sound, but
85  // I'm going to focus on other things. If you would like to change the
86  // need to stop and start the sound, feel free. Or, maybe I'll spend
87  // time on it in the future. Please set the loop option before starting
88  // the sound.
89  play();
90  }
91  }
92 }
93 
94 ////////////////////////////////////////////////////////////////////
95 // Function: MilesAudioSound::
96 // Access: Public, Virtual
97 // Description:
98 ////////////////////////////////////////////////////////////////////
99 unsigned long MilesAudioSound::
100 get_loop_count() const {
101  return _loop_count;
102 }
103 
104 ////////////////////////////////////////////////////////////////////
105 // Function: MilesAudioSound::get_volume
106 // Access: Public, Virtual
107 // Description:
108 ////////////////////////////////////////////////////////////////////
109 PN_stdfloat MilesAudioSound::
110 get_volume() const {
111  return _volume;
112 }
113 
114 ////////////////////////////////////////////////////////////////////
115 // Function: MilesAudioSound::get_balance
116 // Access: Public, Virtual
117 // Description:
118 ////////////////////////////////////////////////////////////////////
119 PN_stdfloat MilesAudioSound::
120 get_balance() const {
121  return _balance;
122 }
123 
124 ////////////////////////////////////////////////////////////////////
125 // Function: MilesAudioSound::get_play_rate
126 // Access: Public, Virtual
127 // Description:
128 ////////////////////////////////////////////////////////////////////
129 PN_stdfloat MilesAudioSound::
130 get_play_rate() const {
131  return _play_rate;
132 }
133 
134 ////////////////////////////////////////////////////////////////////
135 // Function: MilesAudioSound::set_time
136 // Access: Public, Virtual
137 // Description:
138 ////////////////////////////////////////////////////////////////////
139 void MilesAudioSound::
140 set_time(PN_stdfloat time) {
141  miles_audio_debug("set_time(time="<<time<<")");
142 
143  // Mark this position for the next play().
144  _start_time = time;
145  _got_start_time = true;
146 }
147 
148 ////////////////////////////////////////////////////////////////////
149 // Function: MilesAudioSound::set_active
150 // Access: Public, Virtual
151 // Description:
152 ////////////////////////////////////////////////////////////////////
153 void MilesAudioSound::
154 set_active(bool active) {
155  if (_manager == (MilesAudioManager *)NULL) {
156  return;
157  }
158 
159  miles_audio_debug("set_active(active="<<active<<")");
160  if (_active != active) {
161  _active = active;
162  if (_active) {
163  // ...activate the sound.
164  if (_paused && _loop_count==0) {
165  // ...this sound was looping when it was paused.
166  _paused = false;
167  play();
168  }
169 
170  } else {
171  // ...deactivate the sound.
172  if (status() == PLAYING) {
173  if (_loop_count == 0) {
174  // ...we're pausing a looping sound.
175  _paused = true;
176  }
177  _start_time = get_time();
178  _got_start_time = true;
179  stop();
180  }
181  }
182  }
183 }
184 
185 ////////////////////////////////////////////////////////////////////
186 // Function: MilesAudioSound::get_active
187 // Access: Public, Virtual
188 // Description:
189 ////////////////////////////////////////////////////////////////////
190 bool MilesAudioSound::
191 get_active() const {
192  return _active;
193 }
194 
195 ////////////////////////////////////////////////////////////////////
196 // Function: MilesAudioSound::set_finished_event
197 // Access: Public, Virtual
198 // Description: This is no longer implemented.
199 ////////////////////////////////////////////////////////////////////
200 void MilesAudioSound::
201 set_finished_event(const string &event) {
202  _finished_event = event;
203 }
204 
205 ////////////////////////////////////////////////////////////////////
206 // Function: MilesAudioSound::get_finished_event
207 // Access: Public, Virtual
208 // Description: This is no longer implemented.
209 ////////////////////////////////////////////////////////////////////
210 const string &MilesAudioSound::
211 get_finished_event() const {
212  return _finished_event;
213 }
214 
215 ////////////////////////////////////////////////////////////////////
216 // Function: MilesAudioSound::get_name
217 // Access: Public, Virtual
218 // Description:
219 ////////////////////////////////////////////////////////////////////
220 const string &MilesAudioSound::
221 get_name() const {
222  return _file_name;
223 }
224 
225 ////////////////////////////////////////////////////////////////////
226 // Function: MilesAudioSound::cleanup
227 // Access: Public, Virtual
228 // Description: Stops the sound from playing and releases any
229 // associated resources, in preparation for releasing
230 // the sound or shutting down the sound system.
231 ////////////////////////////////////////////////////////////////////
232 void MilesAudioSound::
233 cleanup() {
234 }
235 
236 #endif //]
AudioSound::SoundStatus status() const
Get status of the sound.
void play()
Plays a sound.
void set_loop_count(unsigned long loop_count=1)
Panda uses 0 to mean loop forever.
void stop()
Stop a sound.
PN_stdfloat get_time() const
Gets the play position within the sound.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85