00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00030 #ifndef _SG_SAMPLE_HXX
00031 #define _SG_SAMPLE_HXX 1
00032
00033 #ifndef __cplusplus
00034 # error This library requires C++
00035 #endif
00036
00037 #include <string>
00038
00039 #include <simgear/compiler.h>
00040 #include <simgear/debug/logstream.hxx>
00041 #include <simgear/structure/SGReferenced.hxx>
00042 #include <simgear/structure/SGSharedPtr.hxx>
00043 #include <simgear/math/SGMath.hxx>
00044
00045
00046
00051 class SGSoundSample : public SGReferenced {
00052 public:
00053
00058 SGSoundSample();
00059
00066 SGSoundSample( const char *path, const char *file );
00067
00078 SGSoundSample( void** data, int len, int freq, int format=AL_FORMAT_MONO8 );
00079 SGSoundSample( const unsigned char** data, int len, int freq,
00080 int format = AL_FORMAT_MONO8 );
00081
00085 ~SGSoundSample ();
00086
00091 inline bool is_file() const { return _is_file; }
00092
00099 bool has_changed() {
00100 bool b = _changed; _changed = false; return b;
00101 }
00102
00109 bool has_static_data_changed() {
00110 bool b = _static_changed; _static_changed = false; return b;
00111 }
00112
00118 void play( bool loop ) {
00119 _playing = true; _loop = loop; _changed = true;
00120 }
00121
00126 inline bool is_looping() { return _loop; }
00127
00131 void stop() {
00132 _playing = false; _changed = true;
00133 }
00134
00139 inline void play_once() { play(false); }
00140
00145 inline void play_looped() { play(true); }
00146
00151 inline bool is_playing() { return _playing; }
00152
00158 inline void set_data( const unsigned char **data ) {
00159 _data = (unsigned char*)*data; *data = NULL;
00160 }
00161 inline void set_data( void **data ) {
00162 _data = (unsigned char*)*data; *data = NULL;
00163 }
00164
00169 inline void* get_data() const { return _data; }
00170
00174 void free_data() {
00175 if ( _data != NULL ) free( _data ); _data = NULL;
00176 }
00177
00182 void set_source(unsigned int sid) {
00183 _source = sid; _valid_source = true; _changed = true;
00184 }
00185
00190 inline unsigned int get_source() { return _source; }
00191
00196 inline bool is_valid_source() const { return _valid_source; }
00197
00201 inline void no_valid_source() { _valid_source = false; }
00202
00207 void set_buffer(unsigned int bid) {
00208 _buffer = bid; _valid_buffer = true; _changed = true;
00209 }
00210
00215 inline unsigned int get_buffer() { return _buffer; }
00216
00221 inline bool is_valid_buffer() const { return _valid_buffer; }
00222
00226 inline void no_valid_buffer() { _valid_buffer = false; }
00227
00233 inline void set_pitch( float p ) {
00234 if (p > 2.0) p = 2.0; else if (p < 0.01) p = 0.01;
00235 _pitch = p; _changed = true;
00236 }
00237
00242 inline float get_pitch() { return _pitch; }
00243
00250 inline void set_master_volume( float v ) {
00251 if (v > 1.0) v = 1.0; else if (v < 0.0) v = 0.0;
00252 _master_volume = v; _changed = true;
00253 }
00254
00261 inline void set_volume( float v ) {
00262 if (v > 1.0) v = 1.0; else if (v < 0.0) v = 0.0;
00263 _volume = v; _changed = true;
00264 }
00265
00270 inline float get_volume() { return _volume * _master_volume; }
00271
00276 inline void set_format( int format ) { _format = format; }
00277
00282 inline int get_format() { return _format; }
00283
00288 inline void set_frequency( int freq ) { _freq = freq; }
00289
00294 inline int get_frequency() { return _freq; }
00295
00300 inline void set_size( size_t size ) { _size = size; }
00301
00306 inline size_t get_size() const { return _size; }
00307
00313 inline void set_relative_position( const SGVec3f& pos ) {
00314 _relative_pos = toVec3d(pos); _changed = true;
00315 }
00316
00321 inline void set_position( const SGVec3d& pos ) {
00322 _base_pos = pos; _changed = true;
00323 }
00324
00330 SGVec3d& get_position() { return _absolute_pos; }
00331
00336 inline void set_orientation( const SGQuatd& ori ) {
00337 _orientation = ori; _changed = true;
00338 }
00339
00340 inline void set_rotation( const SGQuatd& ec2body ) {
00341 _rotation = ec2body; _changed = true;
00342 }
00343
00349 inline void set_direction( const SGVec3f& dir ) {
00350 _direction = toVec3d(dir); _static_changed = true;
00351 }
00352
00360 void set_audio_cone( float inner, float outer, float gain ) {
00361 _inner_angle = inner; _outer_angle = outer; _outer_gain = gain;
00362 _static_changed = true;
00363 }
00364
00370 SGVec3f& get_orientation() { return _orivec; }
00371
00376 float get_innerangle() { return _inner_angle; }
00377
00382 float get_outerangle() { return _outer_angle; }
00383
00388 float get_outergain() { return _outer_gain; }
00389
00395 inline void set_velocity( const SGVec3f& vel ) {
00396 _velocity = vel; _changed = true;
00397 }
00398
00404 SGVec3f& get_velocity() { return _velocity; }
00405
00406
00412 inline void set_reference_dist( float dist ) {
00413 _reference_dist = dist; _static_changed = true;
00414 }
00415
00421 inline float get_reference_dist() { return _reference_dist; }
00422
00423
00429 inline void set_max_dist( float dist ) {
00430 _max_dist = dist; _static_changed = true;
00431 }
00432
00438 inline float get_max_dist() { return _max_dist; }
00439
00444 inline std::string get_sample_name() const { return _refname; }
00445
00446 void update_pos_and_orientation();
00447
00448 private:
00449
00450
00451 SGVec3d _absolute_pos;
00452 SGVec3d _relative_pos;
00453 SGVec3d _direction;
00454 SGVec3f _velocity;
00455
00456
00457 SGQuatd _orientation;
00458 SGVec3f _orivec;
00459 SGVec3d _base_pos;
00460
00461 SGQuatd _rotation;
00462
00463 std::string _refname;
00464 unsigned char* _data;
00465
00466
00467 int _format;
00468 size_t _size;
00469 int _freq;
00470
00471
00472 bool _valid_buffer;
00473 unsigned int _buffer;
00474
00475
00476 bool _valid_source;
00477 unsigned int _source;
00478
00479
00480 float _inner_angle;
00481 float _outer_angle;
00482 float _outer_gain;
00483
00484 float _pitch;
00485 float _volume;
00486 float _master_volume;
00487 float _reference_dist;
00488 float _max_dist;
00489 bool _loop;
00490
00491 bool _playing;
00492 bool _changed;
00493 bool _static_changed;
00494 bool _is_file;
00495
00496 string random_string();
00497 };
00498
00499
00500 #endif // _SG_SAMPLE_HXX
00501
00502