From af14d65a9856119264f987cec44fa8c37c26d6c6 Mon Sep 17 00:00:00 2001 From: ehofman Date: Wed, 28 Oct 2009 14:27:47 +0000 Subject: [PATCH] Fix a bug where a sample was removed from the sample list before it was stopped. Proper listener orientation when inside the airplane (need to find a good solution for outside view). --- simgear/sound/openal_test2.cxx | 18 +++++++++--------- simgear/sound/sample_group.cxx | 23 ++++++++++++++++------- simgear/sound/sample_openal.cxx | 8 ++------ simgear/sound/sample_openal.hxx | 2 +- simgear/sound/soundmgr_openal.cxx | 10 ++++++---- simgear/sound/soundmgr_openal.hxx | 12 ++++++------ 6 files changed, 40 insertions(+), 33 deletions(-) diff --git a/simgear/sound/openal_test2.cxx b/simgear/sound/openal_test2.cxx index ac65e555..6ab355e3 100644 --- a/simgear/sound/openal_test2.cxx +++ b/simgear/sound/openal_test2.cxx @@ -30,7 +30,7 @@ int main( int argc, char *argv[] ) { sample1->set_pitch(1.0); sample1->play_looped(); sgr->add(sample1, "sound1"); - smgr->update_late(1.0); + smgr->update(1.0); printf("playing sample1\n"); sleep(1); @@ -39,7 +39,7 @@ int main( int argc, char *argv[] ) { sample2->set_pitch(0.4); sample2->play_looped(); sgr->add(sample2, "sound2"); - smgr->update_late(1.0); + smgr->update(1.0); printf("playing sample2\n"); sleep(1); @@ -48,7 +48,7 @@ int main( int argc, char *argv[] ) { sample3->set_pitch(0.8); sample3->play_looped(); sgr->add(sample3, "sound3"); - smgr->update_late(1.0); + smgr->update(1.0); printf("playing sample3\n"); sleep(1); @@ -57,7 +57,7 @@ int main( int argc, char *argv[] ) { sample4->set_pitch(1.2); sample4->play_looped(); sgr->add(sample4, "sound4"); - smgr->update_late(1.0); + smgr->update(1.0); printf("playing sample4\n"); sleep(1); @@ -66,7 +66,7 @@ int main( int argc, char *argv[] ) { sample5->set_pitch(1.6); sample5->play_looped(); sgr->add(sample5, "sound5"); - smgr->update_late(1.0); + smgr->update(1.0); printf("playing sample5\n"); sleep(1); @@ -75,24 +75,24 @@ int main( int argc, char *argv[] ) { sample6->set_pitch(2.0); sample6->play_looped(); sgr->add(sample6, "sound6"); - smgr->update_late(1.0); + smgr->update(1.0); printf("playing sample6\n"); sleep(1); for (int i=0; i<10; i++) { sleep(1); - smgr->update_late(1); + smgr->update(1); } sgr->stop("sound1"); sgr->stop("sound2"); sgr->stop("sound3"); sleep(0.5); - smgr->update_late(0.5); + smgr->update(0.5); sgr->stop("sound4"); sgr->stop("sound5"); sgr->stop("sound6"); - smgr->update_late(1); + smgr->update(1); sleep(1); smgr->unbind(); diff --git a/simgear/sound/sample_group.cxx b/simgear/sound/sample_group.cxx index 63ae7197..90278dc5 100644 --- a/simgear/sound/sample_group.cxx +++ b/simgear/sound/sample_group.cxx @@ -89,9 +89,17 @@ void SGSampleGroup::update( double dt ) { unsigned int size = _removed_samples.size(); for (unsigned int i=0; iis_valid_source() ) { + if ( sample->is_looping() ) { + sample->no_valid_source(); + _smgr->release_source( sample->get_source() ); + } + else + alGetSourcei( sample->get_source(), AL_SOURCE_STATE, &result ); + } - alGetSourcei( sample->get_source(), AL_SOURCE_STATE, &result ); if ( result == AL_STOPPED ) { ALuint buffer = sample->get_buffer(); alDeleteBuffers( 1, &buffer ); @@ -128,7 +136,7 @@ void SGSampleGroup::update( double dt ) { sample->set_source( source ); update_sample_config( sample ); - ALboolean looping = sample->get_looping() ? AL_TRUE : AL_FALSE; + ALboolean looping = sample->is_looping() ? AL_TRUE : AL_FALSE; alSourcei( source, AL_LOOPING, looping ); alSourcef( source, AL_ROLLOFF_FACTOR, 1.0 ); alSourcei( source, AL_SOURCE_RELATIVE, AL_FALSE ); @@ -366,14 +374,15 @@ void SGSampleGroup::update_sample_config( SGSoundSample *sample ) { float *position, *orientation, *velocity; if ( _tied_to_listener ) { position = _smgr->get_position().data(); - orientation = _smgr->get_velocity().data(); - velocity = _smgr->get_direction().data(); + orientation = _smgr->get_direction().data(); + velocity = _smgr->get_velocity().data(); } else { sample->update_absolute_position(); position = sample->get_position(); - orientation = sample->get_velocity(); - velocity = sample->get_orientation(); + orientation = sample->get_orientation(); + velocity = sample->get_velocity(); } + if (dist(_smgr->get_position(), sample->get_position_vec()) > 50000) printf("source and listener distance greater than 50km!\n"); if (isNaN(position)) printf("NaN in source position\n"); diff --git a/simgear/sound/sample_openal.cxx b/simgear/sound/sample_openal.cxx index 21f81a39..05fc1ed8 100644 --- a/simgear/sound/sample_openal.cxx +++ b/simgear/sound/sample_openal.cxx @@ -112,9 +112,6 @@ SGSoundSample::SGSoundSample( const char *path, const char *file ) : samplepath.append( file ); } _refname = samplepath.str(); - - SG_LOG( SG_GENERAL, SG_DEBUG, "From file sounds sample = " - << samplepath.str() ); } // constructor @@ -191,8 +188,7 @@ SGSoundSample::SGSoundSample( void** data, int len, int freq, int format ) : // destructor SGSoundSample::~SGSoundSample() { - if (_data) free( _data ); - _data = NULL; + if (_data) free(_data); } void SGSoundSample::update_absolute_position() { @@ -218,7 +214,7 @@ void SGSoundSample::update_absolute_position() { string SGSoundSample::random_string() { static const char *r = "0123456789abcdefghijklmnopqrstuvwxyz" "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; - string rstr; + string rstr = "System generated name: "; for (int i=0; i<10; i++) { rstr.push_back( r[rand() % strlen(r)] ); } diff --git a/simgear/sound/sample_openal.hxx b/simgear/sound/sample_openal.hxx index abef4c7b..972a57c4 100644 --- a/simgear/sound/sample_openal.hxx +++ b/simgear/sound/sample_openal.hxx @@ -123,7 +123,7 @@ public: * Check if this audio sample is set to be continuous looping. * @return Return true if this audio sample is set to looping. */ - inline bool get_looping() { return _loop; } + inline bool is_looping() { return _loop; } /** * Schedule this audio sample to stop playing. diff --git a/simgear/sound/soundmgr_openal.cxx b/simgear/sound/soundmgr_openal.cxx index 217039a6..dcb06b5a 100644 --- a/simgear/sound/soundmgr_openal.cxx +++ b/simgear/sound/soundmgr_openal.cxx @@ -66,6 +66,7 @@ SGSoundMgr::SGSoundMgr() : _position(SGVec3d::zeros()), _velocity(SGVec3d::zeros()), _orientation(SGQuatd::zeros()), + _orient_offs(SGQuatd::zeros()), _devname(NULL) { #if defined(ALUT_API_MAJOR_VERSION) && ALUT_API_MAJOR_VERSION >= 1 @@ -242,7 +243,7 @@ void SGSoundMgr::unbind () } // run the audio scheduler -void SGSoundMgr::update_late( double dt ) { +void SGSoundMgr::update( double dt ) { if (_active) { sample_group_map_iterator sample_grp_current = _sample_groups.begin(); sample_group_map_iterator sample_grp_end = _sample_groups.end(); @@ -346,12 +347,13 @@ void SGSoundMgr::set_volume( float v ) * is undefined. If the two vectors are linearly dependent, * behavior is undefined. */ -void SGSoundMgr::set_orientation( SGQuatd ori ) +void SGSoundMgr::set_orientation( const SGQuatd& ori, const SGQuatd& offs ) { _orientation = ori; + _orient_offs = offs; - SGVec3d sgv_up = ori.rotate(SGVec3d::e2()); - SGVec3d sgv_at = ori.rotate(SGVec3d::e3()); + SGVec3d sgv_up = _orient_offs.rotate(SGVec3d::e2()); + SGVec3d sgv_at = _orient_offs.rotate(SGVec3d::e3()); _at_up_vec[0] = sgv_at[0]; _at_up_vec[1] = sgv_at[1]; _at_up_vec[2] = sgv_at[2]; diff --git a/simgear/sound/soundmgr_openal.hxx b/simgear/sound/soundmgr_openal.hxx index 58e63111..bc3851f9 100644 --- a/simgear/sound/soundmgr_openal.hxx +++ b/simgear/sound/soundmgr_openal.hxx @@ -98,8 +98,7 @@ public: void init(); void bind(); void unbind(); - void update(double dt) {}; - void update_late(double dt); + void update(double dt); void suspend(); void resume(); @@ -175,9 +174,8 @@ public: * This is in the same coordinate system as OpenGL; y=up, z=back, x=right. * @param vel Velocity vector of the OpenAL listener */ - void set_velocity( SGVec3d& dir ) { - _velocity = dir; - _changed = true; + void set_velocity( SGVec3d& vel ) { + _velocity = vel; _changed = true; } /** @@ -191,13 +189,14 @@ public: * Set the orientation of the sound manager * @param ori Quaternation containing the orientation information */ - void set_orientation( SGQuatd ori ); + void set_orientation( const SGQuatd& ori, const SGQuatd& offs ); /** * Get the orientation of the sound manager * @return Quaternation containing the orientation information */ inline const SGQuatd& get_orientation() { return _orientation; } + inline const SGQuatd& get_orientation_offset() { return _orient_offs; } /** * Get the direction vector of the sound manager @@ -290,6 +289,7 @@ private: // Orientation of the listener. // first 3 elements are "at" vector, second 3 are "up" vector SGQuatd _orientation; + SGQuatd _orient_offs; ALfloat _at_up_vec[6]; sample_group_map _sample_groups; -- 2.39.5