Panda3D
Loading...
Searching...
No Matches
audioVolumeAttrib.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 audioVolumeAttrib.cxx
10 * @author darren
11 * @date 2006-12-15
12 */
13
14#include "audioVolumeAttrib.h"
16#include "dcast.h"
17#include "bamReader.h"
18#include "bamWriter.h"
19#include "datagram.h"
20#include "datagramIterator.h"
21#include "config_pgraph.h"
22
23CPT(RenderAttrib) AudioVolumeAttrib::_identity_attrib;
24TypeHandle AudioVolumeAttrib::_type_handle;
25int AudioVolumeAttrib::_attrib_slot;
26
27/**
28 * Use AudioVolumeAttrib::make() to construct a new AudioVolumeAttrib object.
29 */
30AudioVolumeAttrib::
31AudioVolumeAttrib(bool off, PN_stdfloat volume) :
32 _off(off),
33 _volume(volume)
34{
35 nassertv(_volume >= 0.f);
36 _has_volume = !IS_NEARLY_EQUAL(_volume, 1.0f);
37}
38
39/**
40 * Constructs an identity audio volume attrib.
41 */
42CPT(RenderAttrib) AudioVolumeAttrib::
43make_identity() {
44 // We make identity a special case and store a pointer forever once we find
45 // it the first time.
46 if (_identity_attrib == nullptr) {
47 AudioVolumeAttrib *attrib = new AudioVolumeAttrib(false, 1.0f);;
48 _identity_attrib = return_new(attrib);
49 }
50
51 return _identity_attrib;
52}
53
54/**
55 * Constructs a new AudioVolumeAttrib object that indicates audio volume
56 * should be scaled by the indicated factor.
57 */
58CPT(RenderAttrib) AudioVolumeAttrib::
59make(PN_stdfloat volume) {
60 AudioVolumeAttrib *attrib = new AudioVolumeAttrib(false, volume);
61 return return_new(attrib);
62}
63
64/**
65 * Constructs a new AudioVolumeAttrib object that ignores any
66 * AudioVolumeAttrib inherited from above. You may also specify an additional
67 * volume scale to apply to geometry below (using set_volume()).
68 */
69CPT(RenderAttrib) AudioVolumeAttrib::
70make_off() {
71 AudioVolumeAttrib *attrib =
72 new AudioVolumeAttrib(true, 1.0f);
73 return return_new(attrib);
74}
75
76/**
77 * Returns a RenderAttrib that corresponds to whatever the standard default
78 * properties for render attributes of this type ought to be.
79 */
80CPT(RenderAttrib) AudioVolumeAttrib::
81make_default() {
82 return return_new(new AudioVolumeAttrib(false, 1.0f));
83}
84
85/**
86 * Returns a new AudioVolumeAttrib, just like this one, but with the volume
87 * changed to the indicated value.
88 */
89CPT(RenderAttrib) AudioVolumeAttrib::
90set_volume(PN_stdfloat volume) const {
91 AudioVolumeAttrib *attrib = new AudioVolumeAttrib(*this);
92 assert(volume >= 0.f);
93 attrib->_volume = volume;
94 attrib->_has_volume = !IS_NEARLY_EQUAL(volume, 1.0f);
95 return return_new(attrib);
96}
97
98/**
99 *
100 */
101void AudioVolumeAttrib::
102output(std::ostream &out) const {
103 out << get_type() << ":";
104 if (is_off()) {
105 out << "off";
106 }
107 if (has_volume()) {
108 out << "(" << get_volume() << ")";
109
110 } else if (!is_off()) {
111 out << "identity";
112 }
113}
114
115/**
116 * Intended to be overridden by derived AudioVolumeAttrib types to return a
117 * unique number indicating whether this AudioVolumeAttrib is equivalent to
118 * the other one.
119 *
120 * This should return 0 if the two AudioVolumeAttrib objects are equivalent, a
121 * number less than zero if this one should be sorted before the other one,
122 * and a number greater than zero otherwise.
123 *
124 * This will only be called with two AudioVolumeAttrib objects whose
125 * get_type() functions return the same.
126 */
127int AudioVolumeAttrib::
128compare_to_impl(const RenderAttrib *other) const {
129 const AudioVolumeAttrib *ta = (const AudioVolumeAttrib *)other;
130
131 if (_off != ta->_off) {
132 return (int)_off - (int)ta->_off;
133 }
134
135 if (_volume != ta->_volume) {
136 return _volume < ta->_volume ? -1 : 1;
137 }
138
139 return 0;
140}
141
142/**
143 * Intended to be overridden by derived RenderAttrib types to return a unique
144 * hash for these particular properties. RenderAttribs that compare the same
145 * with compare_to_impl(), above, should return the same hash; RenderAttribs
146 * that compare differently should return a different hash.
147 */
148size_t AudioVolumeAttrib::
149get_hash_impl() const {
150 size_t hash = 0;
151 hash = int_hash::add_hash(hash, (int)_off);
152 hash = float_hash().add_hash(hash, _volume);
153 return hash;
154}
155
156/**
157 * Intended to be overridden by derived RenderAttrib types to specify how two
158 * consecutive RenderAttrib objects of the same type interact.
159 *
160 * This should return the result of applying the other RenderAttrib to a node
161 * in the scene graph below this RenderAttrib, which was already applied. In
162 * most cases, the result is the same as the other RenderAttrib (that is, a
163 * subsequent RenderAttrib completely replaces the preceding one). On the
164 * other hand, some kinds of RenderAttrib (for instance, ColorTransformAttrib)
165 * might combine in meaningful ways.
166 */
167CPT(RenderAttrib) AudioVolumeAttrib::
168compose_impl(const RenderAttrib *other) const {
169 const AudioVolumeAttrib *ta = (const AudioVolumeAttrib *)other;
170
171 if (ta->is_off()) {
172 return ta;
173 }
174
175 AudioVolumeAttrib *attrib = new AudioVolumeAttrib(is_off(), ta->_volume * _volume);
176 return return_new(attrib);
177}
178
179/**
180 * Intended to be overridden by derived RenderAttrib types to specify how two
181 * consecutive RenderAttrib objects of the same type interact.
182 *
183 * See invert_compose() and compose_impl().
184 */
185CPT(RenderAttrib) AudioVolumeAttrib::
186invert_compose_impl(const RenderAttrib *other) const {
187 if (is_off()) {
188 return other;
189 }
190 const AudioVolumeAttrib *ta = (const AudioVolumeAttrib *)other;
191
192 PN_stdfloat new_volume = _volume == 0.0f ? 1.0f : ta->_volume / _volume;
193
194 AudioVolumeAttrib *attrib = new AudioVolumeAttrib(false, new_volume);
195 return return_new(attrib);
196}
197
198/**
199 * Tells the BamReader how to create objects of type AudioVolumeAttrib.
200 */
201void AudioVolumeAttrib::
202register_with_read_factory() {
203 BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
204}
205
206/**
207 * Writes the contents of this object to the datagram for shipping out to a
208 * Bam file.
209 */
211write_datagram(BamWriter *manager, Datagram &dg) {
212 RenderAttrib::write_datagram(manager, dg);
213
214 // We cheat, and modify the bam stream without upping the bam version. We
215 // can do this since we know that no existing bam files have an
216 // AudioVolumeAttrib in them.
217 dg.add_bool(_off);
218 dg.add_stdfloat(_volume);
219}
220
221/**
222 * This function is called by the BamReader's factory when a new object of
223 * type AudioVolumeAttrib is encountered in the Bam file. It should create
224 * the AudioVolumeAttrib and extract its information from the file.
225 */
226TypedWritable *AudioVolumeAttrib::
227make_from_bam(const FactoryParams &params) {
228 AudioVolumeAttrib *attrib = new AudioVolumeAttrib(false, 1.0f);
229 DatagramIterator scan;
230 BamReader *manager;
231
232 parse_params(params, scan, manager);
233 attrib->fillin(scan, manager);
234
235 return attrib;
236}
237
238/**
239 * This internal function is called by make_from_bam to read in all of the
240 * relevant data from the BamFile for the new AudioVolumeAttrib.
241 */
242void AudioVolumeAttrib::
243fillin(DatagramIterator &scan, BamReader *manager) {
244 RenderAttrib::fillin(scan, manager);
245
246 _off = scan.get_bool();
247 _volume = scan.get_stdfloat();
248 nassertv(_volume >= 0.f);
249 _has_volume = !IS_NEARLY_EQUAL(_volume, 1.0f);
250}
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void parse_params(const FactoryParams &params, DatagramIterator &scan, BamReader *&manager)
Takes in a FactoryParams, passed from a WritableFactory into any TypedWritable's make function,...
Definition bamReader.I:275
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
Applies a scale to audio volume for positional sounds in the scene graph.
bool is_off() const
Returns true if the AudioVolumeAttrib will ignore any color scales inherited from above,...
has_volume
Returns true if the AudioVolumeAttrib has a non-identity volume, false otherwise (in which case it mi...
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
get_volume
Returns the volume to be applied to sounds.
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition bamReader.h:110
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition bamReader.I:177
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition bamWriter.h:63
A class to retrieve the individual data elements previously stored in a Datagram.
PN_stdfloat get_stdfloat()
Extracts either a 32-bit or a 64-bit floating-point number, according to Datagram::set_stdfloat_doubl...
bool get_bool()
Extracts a boolean value.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition datagram.h:38
void add_stdfloat(PN_stdfloat value)
Adds either a 32-bit or a 64-bit floating-point number, according to set_stdfloat_double().
Definition datagram.I:133
void add_bool(bool value)
Adds a boolean value to the datagram.
Definition datagram.I:34
An instance of this class is passed to the Factory when requesting it to do its business and construc...
void register_factory(TypeHandle handle, CreateFunc *func, void *user_data=nullptr)
Registers a new kind of thing the Factory will be able to create.
Definition factory.I:73
This is the base class for a number of render attributes (other than transform) that may be set on sc...
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
TypeHandle is the identifier used to differentiate C++ class types.
Definition typeHandle.h:81
Base class for objects that can be written to and read from Bam files.
This hash_compare class hashes a float or a double.
size_t add_hash(size_t start, const Key &key) const
Adds the indicated key into a running hash.
static size_t add_hash(size_t start, const Key &key)
Adds the indicated key into a running hash.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.