_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();
}
_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();
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();
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;
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();
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 );
}
}
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() );
}
}
-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) {
void set_volume( float vol );
/**
- * set the positions of all managed sound sources
+ * set the velocities of all managed sound sources
*/
- void set_position( SGVec3d pos );
+ void set_velocity( SGVec3d& vel );
/**
- * set the velocities of all managed sound sources
+ * set the position of all managed sound sources
*/
- void set_velocity( SGVec3f vel );
+ 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; }
float _volume;
bool _tied_to_listener;
- SGVec3d _position;
- SGVec3f _velocity;
- SGVec3f _orientation;
+ SGVec3d _velocity;
+ SGGeod _position;
+ SGQuatd _orientation;
sample_map _samples;
// 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),
// 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),
// 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),
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_relative_position( SGVec3f pos ) {
- _relative_pos = pos;
+void SGSoundSample::set_direction( SGVec3d dir ) {
+ _direction = dir;
update_absolute_position();
_changed = true;
}
-void SGSoundSample::set_orientation( SGVec3f ori ) {
- _orientation = ori;
+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_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
}
#include <simgear/structure/SGSharedPtr.hxx>
#include <simgear/math/SGMath.hxx>
-#include <plib/sg.h>
-
-using std::string;
+// #include <plib/sg.h>
/**
* manages everything we need to know for an individual sound sample
// Position of the source sound.
SGVec3d _absolute_pos; // absolute position
SGVec3f _relative_pos; // position relative to the base position
- SGVec3d _base_pos; // base position
-
- // The orientation of the sound (direction and cut-off angles)
- SGVec3f _orientation; // base orientation
- SGVec3f _direction; // orientation offset
+ SGVec3d _direction; // orientation offset
+ SGVec3d _velocity; // Velocity of the source sound.
- // Velocity of the source sound.
- SGVec3f _velocity;
+ // The position and orientation of the sound
+ SGGeod _base_pos;
+ SGQuatd _orientation; // base orientation
- string _sample_name;
+ std::string _sample_name;
unsigned char *_data;
// configuration values
/**
* Set position of the sound source (uses same coordinate system as opengl)
*/
- void set_base_position( SGVec3d pos );
void set_relative_position( SGVec3f pos );
/**
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_position( SGGeod pos );
+
+ /**
+ * Set the orientation of the sound source
*/
- void set_orientation( SGVec3f ori );
+ 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
/**
* 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(); }
/**
/**
* Get the name of this sample
*/
- inline string get_sample_name() { return _sample_name; }
+ inline std::string get_sample_name() { return _sample_name; }
};
_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
_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.
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;
*/
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;
}
/**
* 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 {
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;
//
// 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
//