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).

This commit is contained in:
ehofman
2009-10-28 14:27:47 +00:00
committed by Tim Moore
parent 9fe75dc521
commit af14d65a98
6 changed files with 40 additions and 33 deletions

View File

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

View File

@@ -89,9 +89,17 @@ void SGSampleGroup::update( double dt ) {
unsigned int size = _removed_samples.size();
for (unsigned int i=0; i<size; ) {
SGSoundSample *sample = _removed_samples[i];
ALint result;
ALint result = AL_STOPPED;
if ( sample->is_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");

View File

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

View File

@@ -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.

View File

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

View File

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