From: curt Date: Wed, 28 Apr 2004 20:37:49 +0000 (+0000) Subject: Add support for specifying a positional offset relative to the listener. X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=6e511de7db128d6eefca58f822a7b23deb405bfc;hp=e34aae9982a0ef5d0bf51583cc3fa95e8a8c1387;p=simgear.git Add support for specifying a positional offset relative to the listener. This allows us to "place" cockpit sounds. For example, we can make left engine sound come out of the left speaker, right engine out the right speaker, etc. --- diff --git a/simgear/sound/sample_openal.cxx b/simgear/sound/sample_openal.cxx index d2242d3c..77f0737a 100644 --- a/simgear/sound/sample_openal.cxx +++ b/simgear/sound/sample_openal.cxx @@ -80,9 +80,8 @@ SGSoundSample::SGSoundSample( const char *path, const char *file, SG_LOG( SG_GENERAL, SG_DEBUG, "From file sounds sample = " << samplepath.str() ); - ALuint error; - - source_pos[0] = 0.0; source_pos[1] = 0.0; source_pos[2] = 0.0; + source_pos[0] = 0.0; source_pos[1] = 0.0; source_pos[2] = 0.0; + offset_pos[0] = 0.0; offset_pos[1] = 0.0; offset_pos[2] = 0.0; source_vel[0] = 0.0; source_vel[1] = 0.0; source_vel[2] = 0.0; // clear errors from elsewhere? @@ -90,7 +89,7 @@ SGSoundSample::SGSoundSample( const char *path, const char *file, // create an OpenAL buffer handle alGenBuffers(1, &buffer); - error = alGetError(); + ALuint error = alGetError(); if ( error != AL_NO_ERROR ) { print_openal_error( error ); throw sg_exception("Failed to gen OpenAL buffer."); @@ -152,11 +151,17 @@ SGSoundSample::SGSoundSample( unsigned char *_data, int len, int _freq ) : sample_name = "unknown, generated from data"; source_pos[0] = 0.0; source_pos[1] = 0.0; source_pos[2] = 0.0; + offset_pos[0] = 0.0; offset_pos[1] = 0.0; offset_pos[2] = 0.0; source_vel[0] = 0.0; source_vel[1] = 0.0; source_vel[2] = 0.0; + // clear errors from elsewhere? + alGetError(); + // Load wav data into a buffer. alGenBuffers(1, &buffer); - if (alGetError() != AL_NO_ERROR) { + ALuint error = alGetError(); + if ( error != AL_NO_ERROR ) { + print_openal_error( error ); throw sg_exception("Failed to gen buffer." ); return; } diff --git a/simgear/sound/sample_openal.hxx b/simgear/sound/sample_openal.hxx index e0488979..a7bfe03f 100644 --- a/simgear/sound/sample_openal.hxx +++ b/simgear/sound/sample_openal.hxx @@ -46,6 +46,8 @@ # include #endif +#include + #include SG_USING_STD(string); @@ -70,6 +72,9 @@ private: // Position of the source sound. ALfloat source_pos[3]; + // A constant offset to be applied to the final source_pos + ALfloat offset_pos[3]; + // Velocity of the source sound. ALfloat source_vel[3]; @@ -85,6 +90,7 @@ private: double max_dist; ALboolean loop; + public: /** @@ -199,7 +205,26 @@ public: source_pos[0] = pos[0]; source_pos[1] = pos[1]; source_pos[2] = pos[2]; - alSourcefv( source, AL_POSITION, source_pos ); + + sgVec3 final_pos; + sgAddVec3( final_pos, source_pos, offset_pos ); + + alSourcefv( source, AL_POSITION, final_pos ); + } + + /** + * Set "constant" offset position of sound source (uses same + * coordinate system as opengl) + */ + inline void set_offset_pos( ALfloat *pos ) { + offset_pos[0] = pos[0]; + offset_pos[1] = pos[1]; + offset_pos[2] = pos[2]; + + sgVec3 final_pos; + sgAddVec3( final_pos, source_pos, offset_pos ); + + alSourcefv( source, AL_POSITION, final_pos ); } /** diff --git a/simgear/sound/xmlsound.cxx b/simgear/sound/xmlsound.cxx index 3899be7c..58be2356 100644 --- a/simgear/sound/xmlsound.cxx +++ b/simgear/sound/xmlsound.cxx @@ -235,6 +235,18 @@ SGXmlSound::init(SGPropertyNode *root, SGPropertyNode *node, SGSoundMgr *sndmgr, p += pitch.offset; } + // + // Relative position + // + sgVec3 offset_pos; + sgSetVec3( offset_pos, 0.0, 0.0, 0.0 ); + SGPropertyNode_ptr pos = node->getChild("position"); + if ( pos != NULL ) { + offset_pos[0] = pos->getDoubleValue("x", 0.0); + offset_pos[1] = pos->getDoubleValue("y", 0.0); + offset_pos[2] = pos->getDoubleValue("z", 0.0); + } + // // Initialize the sample // @@ -247,6 +259,7 @@ SGXmlSound::init(SGPropertyNode *root, SGPropertyNode *node, SGSoundMgr *sndmgr, _mgr->add( _sample, _name ); } + _sample->set_offset_pos( offset_pos ); _sample->set_volume(v); _sample->set_reference_dist( reference_dist ); _sample->set_max_dist( max_dist );