// --------------------------------------------------------------------------------------- // ds3d_update_listener() // // returns: 0 => success // -1 => failure // int ds3d_update_listener(vec3d *pos, vec3d *vel, matrix *orient) { if (Cmdline_no_3d_sound) { nprintf(("Sound", "Aborting ds3d_update_buffer due to Cmdline_no_3d_sound...")); return -1; } if (pos) { OpenAL_ErrorPrint( alListener3f(AL_POSITION, pos->xyz.x, pos->xyz.y, -pos->xyz.z) ); } if (vel) { OpenAL_ErrorPrint( alListener3f(AL_VELOCITY, vel->xyz.x, vel->xyz.y, vel->xyz.z) ); } if (orient) { ALfloat alOrient[6]; alOrient[0] = orient->vec.fvec.xyz.x; alOrient[1] = orient->vec.fvec.xyz.y; alOrient[2] = -orient->vec.fvec.xyz.z; alOrient[3] = orient->vec.uvec.xyz.x; alOrient[4] = orient->vec.uvec.xyz.y; alOrient[5] = -orient->vec.uvec.xyz.z; OpenAL_ErrorPrint( alListenerfv(AL_ORIENTATION, alOrient) ); } return 0; }
// --------------------------------------------------------------------------------------- // ds3d_update_buffer() // // parameters: channel => identifies the 3D sound to update // min => the distance at which sound doesn't get any louder // max => the distance at which sound doesn't attenuate any further // pos => world position of sound // vel => velocity of the objects producing the sound // // returns: 0 => success // -1 => failure // // int ds3d_update_buffer(int channel, float min, float max, vec3d *pos, vec3d *vel) { if (Cmdline_no_3d_sound) { nprintf(("Sound", "Aborting ds3d_update_buffer due to Cmdline_no_3d_sound...")); return -1; } if (channel < 0) { return 0; } ALuint source_id = Channels[channel].source_id; ALfloat rolloff = 1.0f; if (pos) { OpenAL_ErrorPrint( alSource3f(source_id, AL_POSITION, pos->xyz.x, pos->xyz.y, -pos->xyz.z) ); } if (vel) { OpenAL_ErrorPrint( alSource3f(source_id, AL_VELOCITY, vel->xyz.x, vel->xyz.y, vel->xyz.z) ); //OpenAL_ErrorPrint( alDopplerFactor(1.0f) ); } else { OpenAL_ErrorPrint( alSource3f(source_id, AL_VELOCITY, 0.0f, 0.0f, 0.0f) ); OpenAL_ErrorPrint( alDopplerFactor(0.0f) ); } if (max <= min) { rolloff = 0.0f; } else { #define MIN_GAIN 0.05f rolloff = (min / (min + (max - min))) / MIN_GAIN; if (rolloff < 0.0f) { rolloff = 0.0f; } } OpenAL_ErrorPrint( alSourcef(source_id, AL_ROLLOFF_FACTOR, rolloff) ); OpenAL_ErrorPrint( alSourcef(source_id, AL_REFERENCE_DISTANCE, min) ); OpenAL_ErrorPrint( alSourcef(source_id, AL_MAX_DISTANCE, max) ); return 0; }
// --------------------------------------------------------------------------------------- // ds3d_update_listener() // // returns: 0 => success // -1 => failure // int ds3d_update_listener(vec3d* pos, vec3d* vel, matrix* orient) { if (DS3D_inited == FALSE) return 0; #ifdef USE_OPENAL // set the listener position if ( pos != NULL ) { OpenAL_ErrorPrint( alListener3f(AL_POSITION, pos->xyz.x, pos->xyz.y, pos->xyz.z) ); } // set the listener velocity if ( vel != NULL ) { OpenAL_ErrorPrint( alListener3f(AL_VELOCITY, vel->xyz.x, vel->xyz.y, vel->xyz.z) ); } // set the listener orientation if ( orient != NULL ) { // uvec is up/top vector, fvec is at/front vector ALfloat list_orien[] = { orient->vec.fvec.xyz.x, orient->vec.fvec.xyz.y, orient->vec.fvec.xyz.z, orient->vec.uvec.xyz.x, orient->vec.uvec.xyz.y, orient->vec.uvec.xyz.z }; OpenAL_ErrorPrint( alListenerfv(AL_ORIENTATION, list_orien) ); } #else HRESULT hr; if (pDS3D_listener == NULL) return -1; // set the listener position if (pos != NULL) { hr = pDS3D_listener->SetPosition(pos->xyz.x, pos->xyz.y, pos->xyz.z, DS3D_DEFERRED); } // set the listener veclocity if (vel != NULL) { hr = pDS3D_listener->SetVelocity(vel->xyz.x, vel->xyz.y, vel->xyz.z, DS3D_DEFERRED); } if (orient != NULL) { hr = pDS3D_listener->SetOrientation(orient->vec.fvec.xyz.x, orient->vec.fvec.xyz.y, orient->vec.fvec.xyz.z, orient->vec.uvec.xyz.x, orient->vec.uvec.xyz.y, orient->vec.uvec.xyz.z, DS3D_DEFERRED); } float rolloff_factor = 1.0f; if (ds_using_a3d() == true) { rolloff_factor = 3.0f; // A3D rolloff } else { rolloff_factor = 3.0f; // EAX rolloff } hr = pDS3D_listener->SetRolloffFactor(rolloff_factor, DS3D_DEFERRED); hr = pDS3D_listener->SetDopplerFactor(1.0f, DS3D_DEFERRED); hr = pDS3D_listener->CommitDeferredSettings(); if (hr != DS_OK) { nprintf(("SOUND", "Error in pDS3D_listener->CommitDeferredSettings(): %s\n", get_DSERR_text(hr))); return -1; } #endif return 0; }
// --------------------------------------------------------------------------------------- // ds3d_update_buffer() // // parameters: channel => identifies the 3D sound to update // min => the distance at which sound doesn't get any louder // max => the distance at which sound doesn't attenuate any further // pos => world position of sound // vel => velocity of the objects producing the sound // // returns: 0 => success // -1 => failure // // int ds3d_update_buffer(int channel, float min, float max, vec3d* pos, vec3d* vel) { if (DS3D_inited == FALSE) return 0; if (channel == -1) return 0; #ifdef USE_OPENAL // as used by DS3D // OpenAL_ErrorPrint( alDistanceModel(AL_INVERSE_DISTANCE_CLAMPED) ); // set the min distance OpenAL_ErrorPrint( alSourcef(Channels[channel].source_id, AL_REFERENCE_DISTANCE, min) ); // set the max distance // OpenAL_ErrorPrint( alSourcef(Channels[channel].source_id, AL_MAX_DISTANCE, max) ); OpenAL_ErrorPrint( alSourcef(Channels[channel].source_id, AL_MAX_DISTANCE, 40000.0f) ); // set rolloff factor OpenAL_ErrorPrint( alSourcef(Channels[channel].source_id, AL_ROLLOFF_FACTOR, 1.0f) ); // set doppler OpenAL_ErrorPrint( alDopplerVelocity(10000.0f) ); OpenAL_ErrorPrint( alDopplerFactor(0.0f) ); // TODO: figure out why using a value of 1 sounds bad // set the buffer position if ( pos != NULL ) { ALfloat alpos[] = { pos->xyz.x, pos->xyz.y, pos->xyz.z }; OpenAL_ErrorPrint( alSourcefv(Channels[channel].source_id, AL_POSITION, alpos) ); } // set the buffer velocity if ( vel != NULL ) { ALfloat alvel[] = { vel->xyz.x, vel->xyz.y, vel->xyz.z }; OpenAL_ErrorPrint( alSourcefv(Channels[channel].source_id, AL_VELOCITY, alvel) ); } else { ALfloat alvel[] = { 0.0f, 0.0f, 0.0f }; OpenAL_ErrorPrint( alSourcefv(Channels[channel].source_id, AL_VELOCITY, alvel) ); } #else HRESULT hr; LPDIRECTSOUND3DBUFFER pds3db; float max_dist, min_dist; pds3db = Channels[channel].pds3db; Assert(pds3db != NULL); // set the buffer position if (pos != NULL) { hr = pds3db->SetPosition(pos->xyz.x, pos->xyz.y, pos->xyz.z, DS3D_DEFERRED); } // set the buffer veclocity if (vel != NULL) { hr = pds3db->SetVelocity(vel->xyz.x, vel->xyz.y, vel->xyz.z, DS3D_DEFERRED); } else { hr = pds3db->SetVelocity(0.0f, 0.0f, 0.0f, DS3D_DEFERRED); } // set the min distance hr = pds3db->GetMinDistance(&min_dist); hr = pds3db->SetMinDistance(min, DS3D_DEFERRED); // set the max distance hr = pds3db->GetMaxDistance(&max_dist); // hr = pds3db->SetMaxDistance( max, DS3D_DEFERRED ); hr = pds3db->SetMaxDistance(100000.0f, DS3D_DEFERRED); #endif return 0; }