Switch to in-place declarations and to the ISO9613 distance attenuation model for AeonWave which also calcualtes the proper sound velocity based on atmospheric properties

This commit is contained in:
Erik Hofman
2019-11-04 14:54:06 +01:00
parent c7efa81efe
commit 99d5b0dc8a
6 changed files with 101 additions and 122 deletions

View File

@@ -4,7 +4,7 @@
// Modified to match the new SoundSystem by Erik Hofman, October 2009
//
// Copyright (C) 2004 Curtis L. Olson - http://www.flightgear.org/~curt
// Copyright (C) 2009 Erik Hofman <erik@ehofman.com>
// Copyright (C) 2009-2019 Erik Hofman <erik@ehofman.com>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
@@ -49,24 +49,6 @@ using std::string;
// empty constructor
SGSoundSampleInfo::SGSoundSampleInfo() :
_refname(random_string()),
_bits(16),
_tracks(1),
_samples(0),
_frequency(22500),
_compressed(false),
_loop(false),
_static_changed(true),
_playing(false),
_pitch(1.0f),
_volume(1.0f),
_master_volume(1.0f),
_use_pos_props(false),
_out_of_range(false),
_inner_angle(360.0f),
_outer_angle(360.0f),
_outer_gain(0.0f),
_reference_dist(500.0f),
_max_dist(3000.0f),
_absolute_pos(SGVec3d::zeros()),
_relative_pos(SGVec3d::zeros()),
_direction(SGVec3d::zeros()),
@@ -97,27 +79,9 @@ std::string SGSoundSampleInfo::random_string()
// SGSoundSample
//
// empty constructor
SGSoundSample::SGSoundSample() :
_is_file(false),
_changed(true),
_valid_source(false),
_source(SGSoundMgr::NO_SOURCE),
_data(NULL),
_valid_buffer(false),
_buffer(SGSoundMgr::NO_BUFFER)
{
}
// constructor
SGSoundSample::SGSoundSample(const char *file, const SGPath& currentDir) :
_is_file(true),
_changed(true),
_valid_source(false),
_source(SGSoundMgr::NO_SOURCE),
_data(NULL),
_valid_buffer(false),
_buffer(SGSoundMgr::NO_BUFFER)
_is_file(true)
{
SGPath p = simgear::ResourceManager::instance()->findPath(file, currentDir);
_refname = p.utf8Str();
@@ -125,13 +89,7 @@ SGSoundSample::SGSoundSample(const char *file, const SGPath& currentDir) :
// constructor
SGSoundSample::SGSoundSample( const unsigned char** data,
int len, int freq, int format ) :
_is_file(false),
_changed(true),
_valid_source(false),
_source(SGSoundMgr::NO_SOURCE),
_valid_buffer(false),
_buffer(SGSoundMgr::NO_BUFFER)
int len, int freq, int format )
{
SG_LOG( SG_SOUND, SG_DEBUG, "In memory sounds sample" );
_data = (unsigned char*)*data; *data = NULL;

View File

@@ -1,10 +1,10 @@
// sample.hxx -- Audio sample encapsulation class
//
//
// Written by Curtis Olson, started April 2004.
// Modified to match the new SoundSystem by Erik Hofman, October 2009
//
// Copyright (C) 2004 Curtis L. Olson - http://www.flightgear.org/~curt
// Copyright (C) 2009 Erik Hofman <erik@ehofman.com>
// Copyright (C) 2009-2019 Erik Hofman <erik@ehofman.com>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
@@ -32,7 +32,8 @@
#include <simgear/math/SGMath.hxx>
#include <simgear/props/props.hxx>
#include "soundmgr.hxx"
enum {
SG_SAMPLE_MONO = 1,
SG_SAMPLE_STEREO = 2,
@@ -76,7 +77,7 @@ public:
* Returns the block alignment of this audio sample.
* @return block alignment in bytes
*/
inline unsigned int get_block_align() {
inline unsigned int get_block_align() {
return _block_align;
}
@@ -164,10 +165,22 @@ public:
/**
* Get maximum distance (in meters) of this sound.
* This is the distance where this sound is no longer audible.
* @return dist Maximum distance
* @return Maximum distance
*/
inline float get_max_dist() { return _max_dist; }
/**
* Get the temperature (in degrees Celsius) at the current altitude.
* @return temperature in degrees Celsius
*/
inline float get_temperature() { return _degC; }
/**
* Get the relative humidity at the current altitude.
* @return Percent relative humidity (0.0 to 1.0)
*/
inline float get_humidity() { return _humidity; }
/**
* Test if static data of audio sample configuration has changed.
* Calling this function will reset the flag so calling it a second
@@ -181,32 +194,34 @@ public:
protected:
// static sound emitter info
std::string _refname;
unsigned int _bits;
unsigned int _tracks;
unsigned int _samples;
unsigned int _frequency;
unsigned int _block_align;
bool _compressed;
bool _loop;
unsigned int _bits = 16;
unsigned int _tracks = 1;
unsigned int _samples = 0;
unsigned int _frequency = 22500;
unsigned int _block_align = 2;
bool _compressed = false;
bool _loop = false;
// dynamic sound emitter info (non 3d)
bool _static_changed;
bool _playing;
bool _static_changed = true;
bool _playing = false;
float _pitch;
float _volume;
float _master_volume;
float _pitch = 1.0f;
float _volume = 1.0f;
float _master_volume = 1.0f;
// dynamic sound emitter info (3d)
bool _use_pos_props;
bool _out_of_range;
bool _use_pos_props = false;
bool _out_of_range = false;
float _inner_angle;
float _outer_angle;
float _outer_gain;
float _inner_angle = 360.0f;
float _outer_angle = 360.0f;
float _outer_gain = 0.0f;
float _reference_dist;
float _max_dist;
float _reference_dist = 500.0f;
float _max_dist = 3000.0f;
float _humidity = 0.5f;
float _degC = 20.0f;
SGPropertyNode_ptr _pos_prop[3];
SGVec3d _absolute_pos; // absolute position
@@ -233,7 +248,7 @@ public:
* Empty constructor, can be used to read data to the systems
* memory and not to the driver.
*/
SGSoundSample();
SGSoundSample() {};
/**
* Constructor
@@ -307,7 +322,7 @@ public:
*/
inline void play_once() { play(false); }
/**
/**
* Schedule this audio sample to play looped.
* @see #play
*/
@@ -389,7 +404,7 @@ public:
*/
inline void set_buffer(unsigned int bid) {
_buffer = bid; _valid_buffer = true; _changed = true;
}
}
/**
* Get the buffer-id of this source
@@ -409,7 +424,7 @@ public:
inline void no_valid_buffer() { _valid_buffer = false; }
/**
* Set the playback pitch of this audio sample.
* Set the playback pitch of this audio sample.
* Should be between 0.0 and 2.0 for maximum compatibility.
* @param p Pitch
*/
@@ -540,6 +555,17 @@ public:
_velocity = vel; _changed = true;
}
/**
* Set both the temperature and relative humidity at the current altitude.
* @param temp Temperature in degrees Celsius
* @param hum Percent relative humidity (0.0 to 1.0)
*/
inline void set_atmosphere(float temp, float hum) {
if (fabsf(_degC - temp) > 1.0f || fabsf(_humidity - hum) > 0.1f) {
_degC = temp, _humidity = hum; _static_changed = true;
}
}
/**
* Set reference distance (in meters) of this sound.
* This is the distance where the gain will be half.
@@ -563,19 +589,19 @@ public:
void update_pos_and_orientation();
protected:
bool _is_file;
bool _changed;
bool _is_file = false;
bool _changed = true;
// Sources are points emitting sound.
bool _valid_source;
unsigned int _source;
bool _valid_source = false;
unsigned int _source = SGSoundMgr::NO_SOURCE;
private:
unsigned char* _data;
unsigned char* _data = NULL;
// Buffers hold sound data.
bool _valid_buffer;
unsigned int _buffer;
bool _valid_buffer = false;
unsigned int _buffer = SGSoundMgr::NO_BUFFER;
};
#endif // _SG_SAMPLE_HXX

View File

@@ -2,7 +2,7 @@
//
// Written for the new SoundSystem by Erik Hofman, October 2009
//
// Copyright (C) 2009 Erik Hofman <erik@ehofman.com>
// Copyright (C) 2009-2019 Erik Hofman <erik@ehofman.com>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
@@ -31,16 +31,7 @@
#include "soundmgr.hxx"
#include "sample_group.hxx"
SGSampleGroup::SGSampleGroup () :
_smgr(NULL),
_refname(""),
_active(false),
_changed(false),
_pause(false),
_volume(1.0),
_tied_to_listener(false),
_velocity(SGVec3d::zeros()),
_orientation(SGQuatd::zeros())
SGSampleGroup::SGSampleGroup ()
{
_samples.clear();
}
@@ -48,14 +39,7 @@ SGSampleGroup::SGSampleGroup () :
SGSampleGroup::SGSampleGroup ( SGSoundMgr *smgr,
const std::string &refname ):
_smgr(smgr),
_refname(refname),
_active(false),
_changed(false),
_pause(false),
_volume(1.0),
_tied_to_listener(false),
_velocity(SGVec3d::zeros()),
_orientation(SGQuatd::zeros())
_refname(refname)
{
_smgr->add(this, refname);
_samples.clear();
@@ -318,6 +302,7 @@ void SGSampleGroup::update_pos_and_orientation() {
sample->set_rotation( ec2body );
sample->set_position(base_position);
sample->set_velocity( velocity );
sample->set_atmosphere( _degC, _humidity );
// Test if a sample is farther away than max distance, if so
// stop the sound playback and free it's source.

View File

@@ -6,7 +6,7 @@
//
// Written for the new SoundSystem by Erik Hofman, October 2009
//
// Copyright (C) 2009 Erik Hofman <erik@ehofman.com>
// Copyright (C) 2009-2019 Erik Hofman <erik@ehofman.com>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
@@ -131,7 +131,7 @@ public:
* @return true if the audio sample exsists and is scheduled for playing
*/
bool play( const std::string& refname, bool looping = false );
/**
* Request to start playing the referred audio sample looping.
* @param refname Reference name of the audio sample to start playing
@@ -173,7 +173,7 @@ public:
/**
* Set the velocity vector of this sample group.
* This is in the local frame coordinate system; x=north, y=east, z=down
* @param vel Velocity vector
* @param vel Velocity vector
*/
void set_velocity( const SGVec3d& vel ) {
_velocity = vel; _changed = true;
@@ -196,29 +196,41 @@ public:
_orientation = ori; _changed = true;
}
/**
* Set both the temperature and relative humidity at the current altitude.
* @param temp Temperature in degrees Celsius
* @param hum Percent relative humidity (0.0 to 1.0)
*/
void set_atmosphere(float temp, float hum ) {
_degC = temp, _humidity = hum; _changed = true;
}
/**
* Tie this sample group to the listener position, orientation and velocity
*/
void tie_to_listener() { _tied_to_listener = true; }
protected:
SGSoundMgr *_smgr;
std::string _refname;
bool _active;
SGSoundMgr *_smgr = NULL;
std::string _refname = "";
bool _active = false;
private:
void cleanup_removed_samples();
void start_playing_sample(SGSoundSample *sample);
void check_playing_sample(SGSoundSample *sample);
bool _changed;
bool _pause;
float _volume;
bool _tied_to_listener;
SGVec3d _velocity;
bool _changed = false;
bool _pause = false;
float _volume = 1.0f;
float _humidity = 0.5f;
float _degC = 20.0f;
bool _tied_to_listener = false;
SGVec3d _velocity = SGVec3d::zeros();
SGQuatd _orientation = SGQuatd::zeros();
SGGeod _base_pos;
SGQuatd _orientation;
sample_map _samples;
std::vector< SGSharedPtr<SGSoundSample> > _removed_samples;

View File

@@ -11,7 +11,7 @@
// Modified for the new SoundSystem by Erik Hofman, October 2009
//
// Copyright (C) 2001 Curtis L. Olson - http://www.flightgear.org/~curt
// Copyright (C) 2009 Erik Hofman <erik@ehofman.com>
// Copyright (C) 2009-2019 Erik Hofman <erik@ehofman.com>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
@@ -68,7 +68,7 @@ public:
* Select a specific sound device.
* Requires a init/reinit call before sound is actually switched.
*/
inline void select_device(const char* devname) {_device_name = devname;}
inline void select_device(const char* devname) { _device_name = devname; }
/**
* Test is the sound manager is in a working condition.

View File

@@ -191,7 +191,7 @@ void SGSoundMgr::init()
TRY( d->_aax.set(dsp) );
dsp = aax::dsp(d->_aax, AAX_DISTANCE_FILTER);
TRY( dsp.set(AAX_AL_INVERSE_DISTANCE_CLAMPED) );
TRY( dsp.set(AAX_ISO9613_DISTANCE) );
TRY( d->_aax.set(dsp) );
dsp = aax::dsp(d->_aax, AAX_VELOCITY_EFFECT);
@@ -313,12 +313,6 @@ void SGSoundMgr::update( double dt )
TRY( dsp.set(AAX_GAIN, _volume) );
TRY( d->_aax.set(dsp) );
#if 0
// TODO: altitude dependent
dsp = d->_aax.get(AAX_VELOCITY_EFFECT);
TRY( dsp.set(AAX_SOUND_VELOCITY, 340.3f) );
TRY( d->_aax.set(dsp) );
#endif
aax::Matrix64 mtx = d->_mtx;
mtx.inverse();
TRY( d->_aax.sensor_matrix(mtx) );
@@ -568,7 +562,7 @@ void SGSoundMgr::sample_play( SGSoundSample *sample )
aax::dsp dsp = emitter.get(AAX_DISTANCE_FILTER);
TRY( dsp.set(AAX_ROLLOFF_FACTOR, 0.3f) );
TRY( dsp.set(AAX_AL_INVERSE_DISTANCE_CLAMPED) );
TRY( dsp.set(AAX_ISO9613_DISTANCE) );
TRY( emitter.set(dsp) );
TRY( emitter.set(AAX_LOOPING, sample->is_looping()) );
@@ -666,6 +660,9 @@ void SGSoundMgr::update_sample_config( SGSoundSample *sample, SGVec3d& position,
dsp = emitter.get(AAX_DISTANCE_FILTER);
TRY( dsp.set(AAX_REF_DISTANCE, sample->get_reference_dist()) );
TRY( dsp.set(AAX_MAX_DISTANCE, sample->get_max_dist()) );
TRY( dsp.set(AAX_RELATIVE_HUMIDITY, sample->get_humidity()) );
TRY( dsp.set(AAX_TEMPERATURE, sample->get_temperature(),
AAX_DEGREES_CELSIUS) );
TRY( emitter.set(dsp) );
}
}
@@ -693,6 +690,7 @@ vector<std::string> SGSoundMgr::get_available_devices()
}
}
}
testForError("get_available_devices");
#endif
return devices;
}