diff --git a/simgear/sound/sample_group.cxx b/simgear/sound/sample_group.cxx index 22f9f9bf..c85e386f 100644 --- a/simgear/sound/sample_group.cxx +++ b/simgear/sound/sample_group.cxx @@ -46,9 +46,9 @@ SGSampleGroup::SGSampleGroup () : _refname(""), _active(false), _tied_to_listener(false), - _position(SGVec3d::zeros()), - _velocity(SGVec3f::zeros()), - _orientation(SGVec3f::zeros()) + _velocity(SGVec3d::zeros()), + _position(SGGeod()), + _orientation(SGQuatd::zeros()) { _samples.clear(); } @@ -58,9 +58,9 @@ SGSampleGroup::SGSampleGroup ( SGSoundMgr *smgr, const string &refname ) : _refname(refname), _active(false), _tied_to_listener(false), - _position(SGVec3d::zeros()), - _velocity(SGVec3f::zeros()), - _orientation(SGVec3f::zeros()) + _velocity(SGVec3d::zeros()), + _position(SGGeod()), + _orientation(SGQuatd::zeros()) { _smgr->add(this, refname); _active = _smgr->is_working(); @@ -104,12 +104,6 @@ void SGSampleGroup::update( double dt ) { if ( _smgr->request_buffer(sample) == SGSoundMgr::NO_BUFFER ) continue; - if ( _tied_to_listener && _smgr->has_changed() ) { - sample->set_base_position( _smgr->get_position_vec() ); - sample->set_orientation( _smgr->get_direction() ); - sample->set_velocity( _smgr->get_velocity() ); - } - // start playing the sample ALboolean looping = sample->get_looping() ? AL_TRUE : AL_FALSE; ALuint buffer = sample->get_buffer(); @@ -294,29 +288,9 @@ bool SGSampleGroup::stop( const string& refname ) { return true; } - -// set source position of all managed sounds -void SGSampleGroup::set_position( SGVec3d pos ) { - if ( isnan(pos.data()[0]) || isnan(pos.data()[1]) || isnan(pos.data()[2]) ) - { - SG_LOG( SG_GENERAL, SG_ALERT, "NAN's found in SampleGroup postion"); - return; - } - - if ( !_tied_to_listener && _position != pos ) { - sample_map_iterator sample_current = _samples.begin(); - sample_map_iterator sample_end = _samples.end(); - for ( ; sample_current != sample_end; ++sample_current ) { - SGSoundSample *sample = sample_current->second; - sample->set_base_position( pos ); - } - _position = pos; - } -} - // set source velocity of all managed sounds -void SGSampleGroup::set_velocity( SGVec3f vel ) { - if ( isnan(vel.data()[0]) || isnan(vel.data()[1]) || isnan(vel.data()[2]) ) +void SGSampleGroup::set_velocity( SGVec3d &vel ) { + if ( isnan(vel[0]) || isnan(vel[1]) || isnan(vel[2]) ) { SG_LOG( SG_GENERAL, SG_ALERT, "NAN's found in SampleGroup velocity"); return; @@ -329,16 +303,25 @@ void SGSampleGroup::set_velocity( SGVec3f vel ) { SGSoundSample *sample = sample_current->second; sample->set_velocity( vel ); } + _velocity = vel; } } // ste the source orientation of all managed sounds -void SGSampleGroup::set_orientation( SGVec3f ori ) { - if ( isnan(ori.data()[0]) || isnan(ori.data()[1]) || isnan(ori.data()[2]) ) - { - SG_LOG( SG_GENERAL, SG_ALERT, "NAN's found in SampleGroup orientation"); - return; +void SGSampleGroup::set_position( SGGeod pos ) { + + sample_map_iterator sample_current = _samples.begin(); + sample_map_iterator sample_end = _samples.end(); + for ( ; sample_current != sample_end; ++sample_current ) { + SGSoundSample *sample = sample_current->second; + sample->set_position( pos ); } + _position = pos; +} + + +// ste the source orientation of all managed sounds +void SGSampleGroup::set_orientation( SGQuatd ori ) { if (_orientation != ori) { sample_map_iterator sample_current = _samples.begin(); @@ -347,6 +330,21 @@ void SGSampleGroup::set_orientation( SGVec3f ori ) { SGSoundSample *sample = sample_current->second; sample->set_orientation( ori ); } + _orientation = ori; + } +} + +void SGSampleGroup::set_volume( float vol ) +{ + _volume = vol; + if (_volume < 0.0) _volume = 0.0; + if (_volume > 1.0) _volume = 1.0; + + sample_map_iterator sample_current = _samples.begin(); + sample_map_iterator sample_end = _samples.end(); + for ( ; sample_current != sample_end; ++sample_current ) { + SGSoundSample *sample = sample_current->second; + sample->set_master_volume( _volume ); } } @@ -354,9 +352,15 @@ void SGSampleGroup::update_sample_config( SGSoundSample *sample ) { if ( sample->is_valid_source() ) { unsigned int source = sample->get_source(); - alSourcefv( source, AL_POSITION, sample->get_position()); - alSourcefv( source, AL_DIRECTION, sample->get_orientation() ); - alSourcefv( source, AL_VELOCITY, sample->get_velocity() ); + if ( _tied_to_listener && _smgr->has_changed() ) { + alSourcefv( source, AL_POSITION, _smgr->get_position().data() ); + alSourcefv( source, AL_DIRECTION, _smgr->get_direction().data() ); + alSourcefv( source, AL_VELOCITY, _smgr->get_velocity().data() ); + } else { + alSourcefv( source, AL_POSITION, sample->get_position()); + alSourcefv( source, AL_DIRECTION, sample->get_orientation() ); + alSourcefv( source, AL_VELOCITY, sample->get_velocity() ); + } testForALError("position and orientation"); alSourcef( source, AL_PITCH, sample->get_pitch() ); @@ -377,20 +381,6 @@ void SGSampleGroup::update_sample_config( SGSoundSample *sample ) { } } -void SGSampleGroup::set_volume( float vol ) -{ - _volume = vol; - if (_volume < 0.0) _volume = 0.0; - if (_volume > 1.0) _volume = 1.0; - - sample_map_iterator sample_current = _samples.begin(); - sample_map_iterator sample_end = _samples.end(); - for ( ; sample_current != sample_end; ++sample_current ) { - SGSoundSample *sample = sample_current->second; - sample->set_master_volume( _volume ); - } -} - bool SGSampleGroup::testForError(void *p, string s) { if (p == NULL) { diff --git a/simgear/sound/sample_group.hxx b/simgear/sound/sample_group.hxx index 1344df44..5bb504a9 100644 --- a/simgear/sound/sample_group.hxx +++ b/simgear/sound/sample_group.hxx @@ -138,20 +138,20 @@ public: */ void set_volume( float vol ); - /** - * set the positions of all managed sound sources - */ - void set_position( SGVec3d pos ); - /** * set the velocities of all managed sound sources */ - void set_velocity( SGVec3f vel ); + void set_velocity( SGVec3d& vel ); + + /** + * set the position of all managed sound sources + */ + void set_position( SGGeod pos ); /** * set the orientation of all managed sound sources */ - void set_orientation( SGVec3f ori ); + void set_orientation( SGQuatd ori ); inline void tie_to_listener() { _tied_to_listener = true; } @@ -165,9 +165,9 @@ private: float _volume; bool _tied_to_listener; - SGVec3d _position; - SGVec3f _velocity; - SGVec3f _orientation; + SGVec3d _velocity; + SGGeod _position; + SGQuatd _orientation; sample_map _samples; diff --git a/simgear/sound/sample_openal.cxx b/simgear/sound/sample_openal.cxx index 8eb83a68..526e27c7 100644 --- a/simgear/sound/sample_openal.cxx +++ b/simgear/sound/sample_openal.cxx @@ -39,12 +39,12 @@ // empty constructor SGSoundSample::SGSoundSample() : - _absolute_pos(SGVec3d::zeros().data()), - _relative_pos(SGVec3f::zeros().data()), - _base_pos(SGVec3d::zeros().data()), - _orientation(SGVec3f::zeros().data()), - _direction(SGVec3f::zeros().data()), - _velocity(SGVec3f::zeros().data()), + _absolute_pos(SGVec3d::zeros()), + _relative_pos(SGVec3f::zeros()), + _direction(SGVec3d::zeros()), + _velocity(SGVec3d::zeros()), + _base_pos(SGGeod()), + _orientation(SGQuatd::zeros()), _sample_name(""), _data(NULL), _format(AL_FORMAT_MONO8), @@ -72,12 +72,12 @@ SGSoundSample::SGSoundSample() : // constructor SGSoundSample::SGSoundSample( const char *path, const char *file ) : - _absolute_pos(SGVec3d::zeros().data()), - _relative_pos(SGVec3f::zeros().data()), - _base_pos(SGVec3d::zeros().data()), - _orientation(SGVec3f::zeros().data()), - _direction(SGVec3f::zeros().data()), - _velocity(SGVec3f::zeros().data()), + _absolute_pos(SGVec3d::zeros()), + _relative_pos(SGVec3f::zeros()), + _direction(SGVec3d::zeros()), + _velocity(SGVec3d::zeros()), + _base_pos(SGGeod()), + _orientation(SGQuatd::zeros()), _format(AL_FORMAT_MONO8), _size(0), _freq(0), @@ -111,12 +111,12 @@ SGSoundSample::SGSoundSample( const char *path, const char *file ) : // constructor SGSoundSample::SGSoundSample( unsigned char *data, int len, int freq, int format ) : - _absolute_pos(SGVec3d::zeros().data()), - _relative_pos(SGVec3f::zeros().data()), - _base_pos(SGVec3d::zeros().data()), - _orientation(SGVec3f::zeros().data()), - _direction(SGVec3f::zeros().data()), - _velocity(SGVec3f::zeros().data()), + _absolute_pos(SGVec3d::zeros()), + _relative_pos(SGVec3f::zeros()), + _direction(SGVec3d::zeros()), + _velocity(SGVec3d::zeros()), + _base_pos(SGGeod()), + _orientation(SGQuatd::zeros()), _data(data), _format(format), _size(len), @@ -148,46 +148,37 @@ SGSoundSample::SGSoundSample( unsigned char *data, int len, int freq, int format SGSoundSample::~SGSoundSample() { } -float *SGSoundSample::get_orientation() { -#if 0 - SGQuatf quat = SGQuatf::fromAngleAxis(_orientation); - SGVec3f orient = quat.transform(_direction); - return orient.data(); -#else - return _orientation.data(); -#endif -} - -void SGSoundSample::set_base_position( SGVec3d pos ) { - _base_pos = pos; +void SGSoundSample::set_orientation( SGQuatd ori ) { + _orientation = ori; update_absolute_position(); _changed = true; } +void SGSoundSample::set_direction( SGVec3d dir ) { + _direction = dir; + update_absolute_position(); + _changed = true; +} + +float *SGSoundSample::get_orientation() { + SGQuatd orient = SGQuatd::fromLonLat(_base_pos) * _orientation; + SGVec3d orivec = -orient.rotate(_direction); + return toVec3f(orivec).data(); +} + void SGSoundSample::set_relative_position( SGVec3f pos ) { _relative_pos = pos; update_absolute_position(); _changed = true; } -void SGSoundSample::set_orientation( SGVec3f ori ) { - _orientation = ori; - update_absolute_position(); - _changed = true; -} - -void SGSoundSample::set_direction( SGVec3f dir ) { - _direction = dir; +void SGSoundSample::set_position( SGGeod pos ) { + _base_pos = pos; update_absolute_position(); _changed = true; } void SGSoundSample::update_absolute_position() { -#if 0 - SGQuatf orient = SGQuatf::fromAngleAxis(_orientation); - SGVec3f modified_relative_pos = orient.transform(_relative_pos); - _absolute_pos = _base_pos + toVec3d(modified_relative_pos); -#else - _absolute_pos = _base_pos; -#endif + _absolute_pos = -SGVec3d::fromGeod(_base_pos); + // TODO: add relative position } diff --git a/simgear/sound/sample_openal.hxx b/simgear/sound/sample_openal.hxx index f44596a8..59d26581 100644 --- a/simgear/sound/sample_openal.hxx +++ b/simgear/sound/sample_openal.hxx @@ -40,9 +40,7 @@ #include #include -#include - -using std::string; +// #include /** * manages everything we need to know for an individual sound sample @@ -55,16 +53,14 @@ private: // Position of the source sound. SGVec3d _absolute_pos; // absolute position SGVec3f _relative_pos; // position relative to the base position - SGVec3d _base_pos; // base position + SGVec3d _direction; // orientation offset + SGVec3d _velocity; // Velocity of the source sound. - // The orientation of the sound (direction and cut-off angles) - SGVec3f _orientation; // base orientation - SGVec3f _direction; // orientation offset + // The position and orientation of the sound + SGGeod _base_pos; + SGQuatd _orientation; // base orientation - // Velocity of the source sound. - SGVec3f _velocity; - - string _sample_name; + std::string _sample_name; unsigned char *_data; // configuration values @@ -326,7 +322,6 @@ public: /** * Set position of the sound source (uses same coordinate system as opengl) */ - void set_base_position( SGVec3d pos ); void set_relative_position( SGVec3f pos ); /** @@ -335,16 +330,20 @@ public: inline float *get_position() const { return toVec3f(_absolute_pos).data(); } /** - * Set the orientation of the sound source, both for direction - * and audio cut-off angles. + * Set the position of the sound source */ - void set_orientation( SGVec3f ori ); + void set_position( SGGeod pos ); + + /** + * Set the orientation of the sound source + */ + void set_orientation( SGQuatd ori ); /** * Set the relative direction of the sound source, both for direction * and audio cut-off angles. */ - void set_direction( SGVec3f dir ); + void set_direction( SGVec3d dir ); /** * Define the audio cone parameters for directional audio @@ -369,14 +368,15 @@ public: /** * Set velocity of the sound source (uses same coordinate system as opengl) */ - inline void set_velocity( SGVec3f vel ) { - _velocity = SGVec3f(vel); _changed = true; + inline void set_velocity( SGVec3d& vel ) { + _velocity = vel; + _changed = true; } /** * Get velocity of the sound source (uses same coordinate system as opengl) */ - inline float *get_velocity() { return _velocity.data(); } + inline float *get_velocity() { return toVec3f(_velocity).data(); } /** @@ -411,7 +411,7 @@ public: /** * Get the name of this sample */ - inline string get_sample_name() { return _sample_name; } + inline std::string get_sample_name() { return _sample_name; } }; diff --git a/simgear/sound/soundmgr_openal.cxx b/simgear/sound/soundmgr_openal.cxx index 11a10fde..12c4b954 100644 --- a/simgear/sound/soundmgr_openal.cxx +++ b/simgear/sound/soundmgr_openal.cxx @@ -59,8 +59,9 @@ SGSoundMgr::SGSoundMgr() : _volume(0.0), _device(NULL), _context(NULL), - _position(SGVec3d::zeros().data()), - _velocity(SGVec3f::zeros().data()), + _position(SGVec3d::zeros()), + _velocity(SGVec3d::zeros()), + _orientation(SGQuatd::zeros()), _devname(NULL) { #if defined(ALUT_API_MAJOR_VERSION) && ALUT_API_MAJOR_VERSION >= 1 @@ -111,13 +112,13 @@ void SGSoundMgr::init() { _context = context; _working = true; - _orientation[0] = 0.0; _orientation[1] = 0.0; _orientation[2] = -1.0; - _orientation[3] = 0.0; _orientation[4] = 1.0; _orientation[5] = 0.0; + _at_up_vec[0] = 0.0; _at_up_vec[1] = 0.0; _at_up_vec[2] = -1.0; + _at_up_vec[3] = 0.0; _at_up_vec[4] = 1.0; _at_up_vec[5] = 0.0; alListenerf( AL_GAIN, 0.2f ); - alListenerfv( AL_POSITION, toVec3f(_position).data() ); - alListenerfv( AL_ORIENTATION, _orientation ); - alListenerfv( AL_VELOCITY, _velocity.data() ); + alListenerfv( AL_POSITION, SGVec3f::zeros().data() ); + alListenerfv( AL_ORIENTATION, _at_up_vec ); + alListenerfv( AL_VELOCITY, toVec3f(_velocity).data() ); alDopplerFactor(0.5); alDopplerVelocity(340.3); // speed of sound in meters per second. @@ -227,9 +228,9 @@ void SGSoundMgr::update_late( double dt ) { if (_changed) { alListenerf( AL_GAIN, _volume ); - alListenerfv( AL_VELOCITY, _velocity.data() ); - alListenerfv( AL_ORIENTATION, _orientation ); + alListenerfv( AL_ORIENTATION, _at_up_vec ); alListenerfv( AL_POSITION, toVec3f(_position).data() ); + alListenerfv( AL_VELOCITY, toVec3f(_velocity).data() ); // alDopplerVelocity(340.3); // TODO: altitude dependent testForALError("update"); _changed = false; @@ -334,14 +335,16 @@ void SGSoundMgr::set_volume( float v ) */ void SGSoundMgr::set_orientation( SGQuatd ori ) { + _orientation = ori; + SGVec3d sgv_up = ori.rotate(SGVec3d::e2()); SGVec3d sgv_at = ori.rotate(SGVec3d::e3()); - _orientation[0] = sgv_at[0]; - _orientation[1] = sgv_at[1]; - _orientation[2] = sgv_at[2]; - _orientation[3] = sgv_up[0]; - _orientation[4] = sgv_up[1]; - _orientation[5] = sgv_up[2]; + _at_up_vec[0] = sgv_at[0]; + _at_up_vec[1] = sgv_at[1]; + _at_up_vec[2] = sgv_at[2]; + _at_up_vec[3] = sgv_up[0]; + _at_up_vec[4] = sgv_up[1]; + _at_up_vec[5] = sgv_up[2]; _changed = true; } diff --git a/simgear/sound/soundmgr_openal.hxx b/simgear/sound/soundmgr_openal.hxx index ba1e5522..4baad69b 100644 --- a/simgear/sound/soundmgr_openal.hxx +++ b/simgear/sound/soundmgr_openal.hxx @@ -134,35 +134,32 @@ public: /** * set the position of the listener (in opengl coordinates) */ - void set_position( SGVec3d pos ) { - if (_position != pos) { - _position = pos; - _changed = true; - } + void set_position( const SGVec3d& pos ) { + _position = -pos; + _changed = true; } - inline double *get_position() { return _position.data(); } - inline SGVec3d get_position_vec() { return _position; }; + inline SGVec3f get_position() { return toVec3f(_position); } /** - * set the velocity of the listener (in opengl coordinates) + * set the velocity direction of the listener (in opengl coordinates) */ - void set_velocity( SGVec3f vel ) { - if (_velocity != vel) { - _velocity = vel; - _changed = true; - } + void set_velocity( SGVec3d& dir ) { + _velocity = dir; + _changed = true; } - inline SGVec3f get_velocity() { return _velocity; } + inline SGVec3f get_velocity() { return toVec3f(_velocity); } /** * set the orientation of the listener (in opengl coordinates) */ void set_orientation( SGQuatd ori ); + inline const SGQuatd& get_orientation() { return _orientation; } + inline SGVec3f get_direction() { - return SGVec3f(_orientation[0], _orientation[1], _orientation[2]); + return SGVec3f(_at_up_vec[0], _at_up_vec[1], _at_up_vec[2]); } enum { @@ -221,11 +218,12 @@ private: SGVec3d _position; // Velocity of the listener. - SGVec3f _velocity; + SGVec3d _velocity; // Orientation of the listener. // first 3 elements are "at" vector, second 3 are "up" vector - ALfloat _orientation[6]; + SGQuatd _orientation; + ALfloat _at_up_vec[6]; sample_group_map _sample_groups; buffer_map _buffers; diff --git a/simgear/sound/xmlsound.cxx b/simgear/sound/xmlsound.cxx index 9fe4fdb1..4edad960 100644 --- a/simgear/sound/xmlsound.cxx +++ b/simgear/sound/xmlsound.cxx @@ -240,20 +240,20 @@ SGXmlSound::init(SGPropertyNode *root, SGPropertyNode *node, // // Orientation // - SGVec3f dir = SGVec3f::zeros(); + SGVec3d dir = SGVec3d::zeros(); float inner, outer, outer_gain; inner = outer = 360.0; outer_gain = 0.0; prop = node->getChild("orientation"); if ( prop != NULL ) { - dir[0] = prop->getDoubleValue("x", 0.0); - dir[1] = -prop->getDoubleValue("y", 0.0); - dir[2] = prop->getDoubleValue("z", 0.0); + dir = SGVec3d(-prop->getDoubleValue("x", 0.0), + -prop->getDoubleValue("y", 0.0), + -prop->getDoubleValue("z", 0.0)); inner = prop->getDoubleValue("inner-angle", 360.0); outer = prop->getDoubleValue("outer-angle", 360.0); outer_gain = prop->getDoubleValue("outer-gain", 0.0); } - + // // Initialize the sample //