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