1 // sample.hxx -- Sound sample encapsulation class
3 // Written by Curtis Olson, started April 2004.
5 // Copyright (C) 2004 Curtis L. Olson - http://www.flightgear.org/~curt
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 * Provides a sound sample encapsulation
28 #ifndef _SG_SAMPLE_HXX
29 #define _SG_SAMPLE_HXX 1
32 # error This library requires C++
37 #include <simgear/compiler.h>
38 #include <simgear/debug/logstream.hxx>
39 #include <simgear/structure/SGReferenced.hxx>
40 #include <simgear/structure/SGSharedPtr.hxx>
41 #include <simgear/math/SGMath.hxx>
43 // #include <plib/sg.h>
46 * manages everything we need to know for an individual sound sample
49 class SGSoundSample : public SGReferenced {
53 // Position of the source sound.
54 SGVec3d _absolute_pos; // absolute position
55 SGVec3f _relative_pos; // position relative to the base position
56 SGVec3d _direction; // orientation offset
57 SGVec3d _velocity; // Velocity of the source sound.
59 // The position and orientation of the sound
61 SGQuatd _orientation; // base orientation
63 std::string _sample_name;
66 // configuration values
71 // Buffers hold sound data.
75 // Sources are points emitting sound.
79 // The orientation of the sound (direction and cut-off angles)
87 float _reference_dist;
96 void update_absolute_position();
101 * Empty constructor, can be used to read data to the systems
102 * memory and not to the driver.
108 * @param path Path name to sound
109 * @param file File name of sound
110 should usually be true unless you want to manipulate the data
113 SGSoundSample( const char *path, const char *file );
117 * @param _data Pointer to a memory buffer containing the sample data
118 the application is responsible for freeing the buffer data.
119 * @param len Byte length of array
120 * @param _freq Frequency of the provided data (bytes per second)
121 should usually be true unless you want to manipulate the data
124 SGSoundSample( unsigned char *data, int len, int freq, int format = AL_FORMAT_MONO8 );
129 * detect wheter the sample holds the information of a sound file
131 inline bool is_file() const { return _is_file; }
134 * Test whether this sample has a changed configuration since the last
135 * call. (Calling this function resets the value).
137 inline bool has_changed() {
138 bool b = _changed; _changed = false; return b;
141 inline bool has_static_data_changed() {
142 bool b = _static_changed; _static_changed = false; return b;
147 * Start playing this sample.
149 * @param _loop Define whether the sound should be played in a loop.
151 inline void play( bool loop ) {
152 _playing = true; _loop = loop; _changed = true;
156 * Return if the sample is looping or not.
158 inline bool get_looping() { return _loop; }
161 * Stop playing this sample.
163 * @param sched A pointer to the appropriate scheduler.
166 _playing = false; _changed = true;
170 * Play this sample once.
173 inline void play_once() { play(false); }
176 * Play this sample looped.
179 inline void play_looped() { play(true); }
182 * Test if a sample is currently playing.
183 * @return true if is is playing, false otherwise.
185 inline bool is_playing() { return _playing; }
188 * set the data associated with this sample
190 inline void set_data( unsigned char* data ) {
195 * @return the data associated with this sample
197 inline void* get_data() const { return _data; }
200 * free the data associated with this sample
202 inline void free_data() {
203 if (_data != NULL) { delete[] _data; _data = NULL; }
207 * set the source id of this source
209 inline void set_source(unsigned int s) {
210 _source = s; _valid_source = true; _changed = true;
214 * get the source id of this source
216 inline unsigned int get_source() { return _source; }
219 * detect wheter the source id of the sample is valid
221 inline bool is_valid_source() const { return _valid_source; }
224 * set the source id of the sample to invalid.
226 inline void no_valid_source() {
227 _valid_source = false;
231 * set the buffer id of this source
233 inline void set_buffer(unsigned int b) {
234 _buffer = b; _valid_buffer = true; _changed = true;
238 * get the buffer id of this source
240 inline unsigned int get_buffer() { return _buffer; }
243 * detect wheter the source id of the sample is valid
245 inline bool is_valid_buffer() const { return _valid_buffer; }
248 * set the source id of the sample to invalid.
250 inline void no_valid_buffer() {
251 _valid_buffer = false;
255 * Get the current pitch setting of this sample.
257 inline float get_pitch() { return _pitch; }
260 * Set the pitch of this sample.
262 inline void set_pitch( float p ) {
263 _pitch = p; _changed = true;
267 * Get the current volume setting of this sample.
269 inline float get_volume() { return _volume * _master_volume; }
272 * Set the master (sampel group) volume of this sample.
274 inline void set_master_volume( float v ) {
275 _master_volume = v; _changed = true;
279 * Set the volume of this sample.
281 inline void set_volume( float v ) {
282 _volume = v; _changed = true;
286 * Set the format of the sounds sample
288 inline void set_format( int format ) {
293 * Returns the format of the sounds sample
295 inline int get_format() { return _format; }
299 * Set the frequency of the sounds sample
301 inline void set_frequency( int freq ) {
302 _freq = freq; _changed = true;
306 * Returns the frequency of the sounds sample
308 inline int get_frequency() { return _freq; }
311 * Returns the size of the sounds sample
313 inline void set_size( int size ) {
318 * Returns the size of the sounds sample
320 inline int get_size() const { return _size; }
323 * Set position of the sound source (uses same coordinate system as opengl)
325 void set_relative_position( SGVec3f pos );
328 * Get position of the sound source (uses same coordinate system as opengl)
330 inline float *get_position() const { return toVec3f(_absolute_pos).data(); }
333 * Set the position of the sound source
335 void set_position( SGGeod pos );
338 * Set the orientation of the sound source
340 void set_orientation( SGQuatd ori );
343 * Set the relative direction of the sound source, both for direction
344 * and audio cut-off angles.
346 void set_direction( SGVec3d dir );
349 * Define the audio cone parameters for directional audio
351 inline void set_audio_cone( float inner, float outer, float gain ) {
352 _inner_angle = inner;
353 _outer_angle = outer;
355 _static_changed = true;
359 * Get the orientation of the sound source, the inner or outer angle
362 float *get_orientation();
364 inline float get_innerangle() { return _inner_angle; }
365 inline float get_outerangle() { return _outer_angle; }
366 inline float get_outergain() { return _outer_gain; }
369 * Set velocity of the sound source (uses same coordinate system as opengl)
371 inline void set_velocity( SGVec3d& vel ) {
377 * Get velocity of the sound source (uses same coordinate system as opengl)
379 inline float *get_velocity() { return toVec3f(_velocity).data(); }
383 * Set reference distance of sound (the distance where the gain
386 inline void set_reference_dist( float dist ) {
387 _reference_dist = dist; _static_changed = true;
391 * Get reference distance of sound (the distance where the gain
394 inline float get_reference_dist() { return _reference_dist; }
398 * Set maximum distance of sound (the distance where the sound is
401 void set_max_dist( float dist ) {
402 _max_dist = dist; _static_changed = true;
406 * Get maximum istance of sound (the distance where the sound is
409 inline float get_max_dist() { return _max_dist; }
412 * Get the name of this sample
414 inline std::string get_sample_name() { return _sample_name; }
418 #endif // _SG_SAMPLE_HXX