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.
This commit is contained in:
curt
2004-04-28 20:37:49 +00:00
parent e34aae9982
commit 6e511de7db
3 changed files with 49 additions and 6 deletions

View File

@@ -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;
}

View File

@@ -46,6 +46,8 @@
# include <AL/alut.h>
#endif
#include <plib/sg.h>
#include <simgear/debug/logstream.hxx>
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 );
}
/**

View File

@@ -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 );