Re #307 Some cosmetics for the sound engine code

git-svn-id: https://svn.code.sf.net/p/speed-dreams/code/trunk@4146 30fe4595-0a0c-4342-8851-515496e4dcbd

Former-commit-id: 00d112d8bf611b5070b20b8613b681c4681589e6
Former-commit-id: 92654d05e3bc282dc27c5b4d847f0dfb8a7d79cb
This commit is contained in:
pouillot 2011-11-13 18:30:39 +00:00
parent ad85bfb341
commit bc7d8195e8
11 changed files with 178 additions and 137 deletions

View file

@ -60,6 +60,7 @@ CarSoundData::CarSoundData(int id, SoundInterface* sound_interface)
attenuation = 0.0f;
}
void CarSoundData::setEngineSound (Sound* engine_sound, float rpm_scale)
{
this->engine_sound = engine_sound;
@ -76,6 +77,52 @@ void CarSoundData::setTurboParameters (bool turbo_on, float turbo_rpm, float tur
fprintf (stderr, "warning: turbo lag %f <= 0\n", turbo_lag);
}
}
Sound* CarSoundData::getEngineSound () const
{
return engine_sound;
}
void CarSoundData::copyEngPri (SoundPri& epri) const
{
epri = eng_pri;
}
void CarSoundData::setCarPosition (sgVec3 p)
{
for (int i=0; i<3; i++) {
position[i] = p[i];
}
}
void CarSoundData::setCarSpeed (sgVec3 u)
{
for (int i=0; i<3; i++) {
speed[i] = u[i];
}
}
void CarSoundData::getCarPosition (sgVec3 p) const
{
for (int i=0; i<3; i++) {
p[i] = position[i];
}
}
void CarSoundData::getCarSpeed (sgVec3 u) const
{
for (int i=0; i<3; i++) {
u[i] = speed[i];
}
}
void CarSoundData::setListenerPosition (sgVec3 p)
{
for (int i=0; i<3; i++) {
listener_position[i] = p[i];
}
}
void CarSoundData::update (tCarElt* car)
{
assert (car->index == eng_pri.id);
@ -345,9 +392,9 @@ void CarSoundData::calculateCollisionSound (tCarElt* car)
return;
}
int collision = car->priv.collision;
const int collision = car->priv.collision;
if (collision) {
if (collision & 1) {
if (collision & SEM_COLLISION) {
skid_metal.a = (tdble)(car->_speed_xy * 0.01);
skid_metal.f = (tdble)(.5+0.5*skid_metal.a);
drag_collision.f = skid_metal.f;
@ -355,23 +402,23 @@ void CarSoundData::calculateCollisionSound (tCarElt* car)
skid_metal.a = 0;
}
if ((collision & 16 )) {
if (collision & SEM_COLLISION_Z_CRASH) {
bottom_crash = true;
}
if ((collision & 8 )) {
if (collision & SEM_COLLISION_Z) {
bang = true;
}
if (((collision & 1) ==0) ||
((collision & 2)
&&(skid_metal.a >drag_collision.a))) {
// No crash sound if a dragging sound is already playing.
if (!(collision & SEM_COLLISION)
|| ((collision & SEM_COLLISION_XYSCENE) && skid_metal.a > drag_collision.a)) {
crash = true;
}
}
drag_collision.a = 0.9f*drag_collision.a + skid_metal.a;
if (drag_collision.a>1.0f) {
if (drag_collision.a > 1.0f) {
drag_collision.a = 1.0f;
}
skid_metal.a = drag_collision.a;

View file

@ -28,8 +28,10 @@ typedef struct WheelSoundData_
} WheelSoundData;
/// Manages the source sound of each individual car.
class CarSoundData {
class CarSoundData
{
protected:
sgVec3 listener_position;
sgVec3 position;
sgVec3 speed;
@ -41,7 +43,9 @@ protected:
void calculateTyreSound (tCarElt* car);
void calculateGearChangeSound (tCarElt* car);
void calculateCollisionSound (tCarElt* car);
public:
SoundPri eng_pri;
WheelSoundData wheel[4];
float attenuation; ///< global distance attenuation
@ -62,46 +66,24 @@ public:
QSoundChar skid_metal;
int prev_gear;
CarSoundData (int id, SoundInterface* sound_interface);
void setEngineSound (Sound* engine_sound, float rpm_scale);
void setTurboParameters (bool turbo_on, float turbo_rpm, float turbo_lag);
Sound* getEngineSound () {return engine_sound;}
void copyEngPri (SoundPri& epri) {epri = eng_pri;}
void setCarPosition (sgVec3 p)
{
for (int i=0; i<3; i++) {
position[i] = p[i];
}
}
void setCarSpeed (sgVec3 u)
{
for (int i=0; i<3; i++) {
speed[i] = u[i];
}
}
void getCarPosition (sgVec3 p)
{
for (int i=0; i<3; i++) {
p[i] = position[i];
}
}
void getCarSpeed (sgVec3 u)
{
for (int i=0; i<3; i++) {
u[i] = speed[i];
}
}
void setListenerPosition (sgVec3 p)
{
for (int i=0; i<3; i++) {
listener_position[i] = p[i];
}
}
void update (tCarElt* car);
bool gear_changing;
bool bottom_crash;
bool bang;
bool crash;
public:
CarSoundData (int id, SoundInterface* sound_interface);
void setEngineSound (Sound* engine_sound, float rpm_scale);
void setTurboParameters (bool turbo_on, float turbo_rpm, float turbo_lag);
Sound* getEngineSound () const;
void copyEngPri (SoundPri& epri) const;
void setCarPosition (sgVec3 p);
void setCarSpeed (sgVec3 u);
void getCarPosition (sgVec3 p) const;
void getCarSpeed (sgVec3 u) const;
void setListenerPosition (sgVec3 p);
void update (tCarElt* car);
};
#endif /* CAR_SOUND_DATA_H */

View file

@ -27,14 +27,9 @@
OpenalSound::OpenalSound(const char* filename, OpenalSoundInterface* sitf,
int flags, bool loop, bool static_pool)
: Sound(flags, loop)
{
this->loop = loop;
this->flags = flags;
this->static_pool = static_pool;
volume = 0.0f;
pitch = 1.0f;
lowpass = 1.0f;
poolindex = -1;
itf = sitf;
@ -43,9 +38,6 @@ OpenalSound::OpenalSound(const char* filename, OpenalSoundInterface* sitf,
REFERENCE_DISTANCE = 5.0f;
ROLLOFF_FACTOR = 0.5f;
playing = false;
paused = false;
int i;
for (i = 0; i<3; i++) {
source_position[i] = 0.0f;
@ -53,7 +45,8 @@ OpenalSound::OpenalSound(const char* filename, OpenalSoundInterface* sitf,
zeroes[i] = 0.0f;
}
GfOut("OpenAL : Creating %s source from %s\n", static_pool ? "static" : "dynamic", filename);
GfLogTrace("OpenAL : Creating %s source from %s\n",
static_pool ? "static" : "dynamic", filename);
int error = alGetError();
if (error != AL_NO_ERROR) {
@ -214,7 +207,6 @@ void OpenalSound::setLPFilter(float lp)
this->lowpass = lp;
}
void OpenalSound::setReferenceDistance(float dist)
{
if (static_pool) {
@ -229,7 +221,6 @@ void OpenalSound::setReferenceDistance(float dist)
}
}
void OpenalSound::setSource (sgVec3 p, sgVec3 u)
{
for (int i=0; i<3; i++) {
@ -238,7 +229,6 @@ void OpenalSound::setSource (sgVec3 p, sgVec3 u)
}
}
void OpenalSound::getSource(sgVec3 p, sgVec3 u)
{
for (int i=0; i<3; i++) {
@ -247,7 +237,6 @@ void OpenalSound::getSource(sgVec3 p, sgVec3 u)
}
}
void OpenalSound::play()
{
start();
@ -257,7 +246,7 @@ void OpenalSound::start()
{
if (static_pool) {
if (is_enabled) {
if (playing==false) {
if (!playing) {
if (loop) {
playing = true;
}
@ -281,7 +270,7 @@ void OpenalSound::start()
}
// play
if (playing==false) {
if (!playing) {
if (loop) {
playing = true;
}
@ -291,12 +280,11 @@ void OpenalSound::start()
}
}
void OpenalSound::stop()
{
if (static_pool) {
if (is_enabled) {
if (playing==true) {
if (playing) {
playing = false;
alSourceStop (source);
}
@ -304,7 +292,7 @@ void OpenalSound::stop()
} else {
// Shared source.
if (itf->getSourcePool()->releaseSource(this, &poolindex)) {
if (playing==true) {
if (playing) {
playing = false;
alSourceStop (source);
}
@ -314,7 +302,7 @@ void OpenalSound::stop()
void OpenalSound::resume()
{
if (paused==true) {
if (paused) {
paused = false;
}
}
@ -322,14 +310,14 @@ void OpenalSound::resume()
void OpenalSound::pause()
{
if (paused==false) {
if (!paused) {
paused = true;
}
}
void OpenalSound::update ()
{
ALfloat zero_velocity[3] = {0.0f, 0.0f, 0.0f};
static const ALfloat zero_velocity[3] = {0.0f, 0.0f, 0.0f};
if (static_pool) {
if (is_enabled) {
alSourcefv (source, AL_POSITION, source_position);

View file

@ -42,7 +42,6 @@ protected:
ALfloat zeroes[3]; ///< just a vector of 0s
ALfloat back[6]; ///< direction of back
ALfloat front[6]; ///< direction of front
bool playing, paused;
ALfloat MAX_DISTANCE; ///< maximum allowed distance
ALfloat MAX_DISTANCE_LOW; ///< maximum allowed distance
ALfloat REFERENCE_DISTANCE; ///< reference distance for sound
@ -70,16 +69,6 @@ public:
virtual void resume();
virtual void pause();
virtual void update();
/// Return true if playing
virtual bool isPlaying()
{
return playing;
}
/// Return true if paused.
virtual bool isPaused()
{
return paused;
}
};

View file

@ -184,11 +184,10 @@ void OpenalSoundInterface::setNCars(int n_cars)
car_src = new SoundSource[n_cars];
}
Sound* OpenalSoundInterface::addSample (const char* filename, int flags, bool loop, bool static_pool)
{
Sound* sound = new OpenalSound(filename, this, flags, loop, static_pool);
sound->setVolume(1.0f);
sound->setVolume(1.0f); // Will be automatically scaled down by the global gain.
sound_list.push_back(sound);
return sound;
}
@ -199,12 +198,12 @@ void OpenalSoundInterface::update(CarSoundData** car_sound_data, int n_cars, sgV
ALfloat listener_pos[3];
ALfloat listener_speed[3];
ALfloat listener_orientation[6];
ALfloat zeros[] = {0.0f, 0.0f, 0.0f};
static const ALfloat zeros[] = {0.0f, 0.0f, 0.0f};
int i;
for (i = 0; i<3; i++) {
listener_pos[i] = p_obs[i];
listener_speed[i] = 0;// u_obs[i];
listener_speed[i] = 0;// u_obs[i]; // TODO: Try restoring this, needed !
listener_orientation[i] = c_obs[i];
listener_orientation[i+3] = a_obs[i];
}
@ -220,7 +219,7 @@ void OpenalSoundInterface::update(CarSoundData** car_sound_data, int n_cars, sgV
for (i = 0; i<n_cars; i++) {
car_sound_data[i]->copyEngPri(engpri[i]);
int id = engpri[i].id;
const int id = engpri[i].id;
sgVec3 p;
sgVec3 u;
car_sound_data[id]->getCarPosition(p);
@ -233,12 +232,12 @@ void OpenalSoundInterface::update(CarSoundData** car_sound_data, int n_cars, sgV
qsort((void*) engpri, n_cars, sizeof(SoundPri), &sortSndPriority);
int nsrc = MIN(sourcepool->getNbSources(), n_engine_sounds);
const int nsrc = MIN(sourcepool->getNbSources(), n_engine_sounds);
// Reverse order is important to gain free sources from stopped engine sounds
// before attempting to start new ones.
for (i = n_cars - 1; i >= 0; i--) {
int id = engpri[i].id;
const int id = engpri[i].id;
sgVec3 p;
sgVec3 u;
CarSoundData* sound_data = car_sound_data[id];
@ -297,7 +296,6 @@ void OpenalSoundInterface::update(CarSoundData** car_sound_data, int n_cars, sgV
}
}
// other looping sounds
road.snd = road_ride_sound;
sortSingleQueue (car_sound_data, &road, n_cars);
@ -332,10 +330,9 @@ void OpenalSoundInterface::update(CarSoundData** car_sound_data, int n_cars, sgV
setMaxSoundCar (car_sound_data, &axle);
// On-off sounds
sgVec3 p, u;
for (id = 0; id<n_cars; id++) {
CarSoundData* sound_data = car_sound_data[id];
sgVec3 p;
sgVec3 u = {0, 0, 0};
const CarSoundData* sound_data = car_sound_data[id];
if (sound_data->crash) {
if (++curCrashSnd>=NB_CRASH_SOUND) {
curCrashSnd = 0;
@ -343,7 +340,7 @@ void OpenalSoundInterface::update(CarSoundData** car_sound_data, int n_cars, sgV
sound_data->getCarPosition(p);
sound_data->getCarSpeed(u);
crash_sound[curCrashSnd]->setSource (p, u);
crash_sound[curCrashSnd]->setVolume (1.0f);
crash_sound[curCrashSnd]->setVolume (1.0f); // Will be automatically scaled down by the global gain.
crash_sound[curCrashSnd]->setPitch (1.0f);
crash_sound[curCrashSnd]->update();
crash_sound[curCrashSnd]->start();
@ -353,7 +350,7 @@ void OpenalSoundInterface::update(CarSoundData** car_sound_data, int n_cars, sgV
sound_data->getCarPosition(p);
sound_data->getCarSpeed(u);
bang_sound->setSource (p, u);
bang_sound->setVolume (1.0f);
bang_sound->setVolume (1.0f); // Will be automatically scaled down by the global gain.
bang_sound->setPitch (1.0f);
bang_sound->update();
bang_sound->start();
@ -363,7 +360,7 @@ void OpenalSoundInterface::update(CarSoundData** car_sound_data, int n_cars, sgV
sound_data->getCarPosition(p);
sound_data->getCarSpeed(u);
bottom_crash_sound->setSource (p, u);
bottom_crash_sound->setVolume (1.0f);
bottom_crash_sound->setVolume (1.0f); // Will be automatically scaled down by the global gain.
bottom_crash_sound->setPitch (1.0f);
bottom_crash_sound->update();
bottom_crash_sound->start();
@ -374,16 +371,14 @@ void OpenalSoundInterface::update(CarSoundData** car_sound_data, int n_cars, sgV
sound_data->getCarSpeed(u);
gear_change_sound->setSource (p, u);
gear_change_sound->setReferenceDistance (1.0f);
gear_change_sound->setVolume (1.0f);
gear_change_sound->setVolume (1.0f); // Will be automatically scaled down by the global gain.
gear_change_sound->setPitch (1.0f);
gear_change_sound->update();
gear_change_sound->start();
}
}
}
void OpenalSoundInterface::initSharedSourcePool(void)
{
const int nbdynsources = OSI_MAX_SOURCES - n_static_sources_in_use;
@ -392,7 +387,6 @@ void OpenalSoundInterface::initSharedSourcePool(void)
GfLogInfo(" Dynamic sources: %d\n", sourcepool->getNbSources());
}
bool OpenalSoundInterface::getStaticSource(ALuint *source)
{
// Do we have a source left for static assigned sources?

View file

@ -25,11 +25,9 @@
/// Create a new PLib sound. It requires a scheduler to be set up
/// and a filename to read data from.
PlibSound::PlibSound(slScheduler* sched, const char* filename, int flags, bool loop)
: Sound(flags)
: Sound(flags, loop)
{
this->sched = sched;
this->loop = loop;
MAX_VOL = 1.0f;
sample = new slSample (filename, sched);
if (flags & ACTIVE_VOLUME) {
volume_env = new slEnvelope(1, SL_SAMPLE_ONE_SHOT);
@ -64,11 +62,6 @@ PlibSound::PlibSound(slScheduler* sched, const char* filename, int flags, bool l
if (flags & ACTIVE_LP_FILTER) {
lowpass_env->setStep(0, 0.0, 1.0f);
}
volume = 0.0f;
pitch = 1.0f;
lowpass = 1.0f;
playing = false;
paused = false;
}
/// Destructor.
@ -119,7 +112,7 @@ void PlibSound::start()
{
// TODO: consistency check?
if (loop) {
if (playing == false) {
if (!playing) {
playing = true;
sched->loopSample (sample);
}
@ -132,7 +125,7 @@ void PlibSound::start()
/// Stop the sample
void PlibSound::stop()
{
if (playing == true) {
if (playing) {
playing = false;
sched->stopSample (sample);
}

View file

@ -32,8 +32,6 @@ protected:
slEnvelope* pitch_env; ///< pitch envelope
slEnvelope* lowpass_env; ///< low pass filter envelope
slScheduler* sched; ///< plib sl scheduler (see sl.h)
bool playing; ///< Sound is playing
bool paused; ///< sound is paused
public:
PlibSound(slScheduler* sched,
const char* filename,
@ -49,16 +47,6 @@ public:
virtual void resume();
virtual void pause();
virtual void update();
/// True if the sound is playing.
virtual bool isPlaying()
{
return playing;
}
/// Truye if the sound is paused.
virtual bool isPaused()
{
return paused;
}
};
#endif

View file

@ -23,6 +23,24 @@
#include "SoundInterface.h"
/// Construct a sound.
Sound::Sound(int flags, bool loop)
{
this->flags = flags;
MAX_VOL = 1.0f;
volume = 0.0f;
pitch = 1.0f;
lowpass = 1.0f;
this->loop = loop;
playing = false;
paused = false;
}
/// Destructor
Sound::~Sound()
{
}
/// Set the volume \note effect not consistent across backends
void Sound::setVolume(float vol)
{
@ -41,6 +59,47 @@ void Sound::setLPFilter(float lp)
this->lowpass = lp;
}
void Sound::setSource(sgVec3 p, sgVec3 u)
{
}
float Sound::getVolume() const
{
return volume;
}
float Sound::getPitch() const
{
return pitch;
}
float Sound::getLPfilter() const
{
return lowpass;
}
void Sound::setReferenceDistance (float dist)
{
// Do nothing implementation.
}
void Sound::getSource (sgVec3 p, sgVec3 u) const
{
// Do nothing implementation.
}
/// True if the sound is playing.
bool Sound::isPlaying() const
{
return playing;
}
/// True if the sound is paused.
bool Sound::isPaused() const
{
return paused;
}
/// Create a sound source
SoundSource::SoundSource()
{
@ -131,3 +190,4 @@ void SoundSource::setListener (sgVec3 p, sgVec3 u)
u_lis[i] = u[i];
}
}

View file

@ -59,36 +59,38 @@ class Sound
float pitch; ///< Current pitch
float lowpass; ///< Current low pass filter
bool loop; ///< Whether it's a looping sound
bool playing; ///< Sound is playing
bool paused; ///< sound is paused
public:
/// Consruct a sound.
Sound(int flags = (ACTIVE_VOLUME|ACTIVE_PITCH))
{
this->flags = flags;
}
/// Construct a sound.
Sound(int flags = (ACTIVE_VOLUME|ACTIVE_PITCH), bool loop = false);
/// Destructor
virtual ~Sound() {}
virtual ~Sound();
virtual void setVolume(float vol);
virtual void setPitch(float pitch);
virtual void setLPFilter(float lp);
virtual void setSource(sgVec3 p, sgVec3 u) {}
virtual float getVolume() {return volume;}
virtual float getPitch() {return pitch;}
virtual float getLPfilter() {return lowpass;}
virtual void setReferenceDistance (float dist) {}
virtual void getSource (sgVec3 p, sgVec3 u) {}
virtual void setSource(sgVec3 p, sgVec3 u);
virtual void setReferenceDistance (float dist);
//virtual void setListener(sgVec3 p, sgVec3 u) = 0;
virtual float getVolume() const;
virtual float getPitch() const;
virtual float getLPfilter() const;
virtual void getSource (sgVec3 p, sgVec3 u) const;
virtual void play() = 0;
virtual void start() = 0;
virtual void stop() = 0;
virtual void resume() = 0;
virtual void pause() = 0;
virtual void update() = 0;
virtual bool isPlaying() = 0;
virtual bool isPaused() = 0;
virtual bool isPlaying() const;
virtual bool isPaused() const;
};
/** Sound source management.

View file

@ -174,11 +174,11 @@ grShutdownSound(int ncars)
float
void
grRefreshSound(tSituation *s, cGrCamera *camera)
{
if (sound_mode == DISABLED) {
return 0.0f;
return;
}
// TODO: Fix for a lot of cars.
@ -208,8 +208,6 @@ grRefreshSound(tSituation *s, cGrCamera *camera)
sound_interface->update (car_sound_data, s->_ncars,
*p_camera, *u_camera, c_camera, *a_camera);
}
return 0.0f;
}
void grMuteSound(bool bOn)

View file

@ -26,7 +26,7 @@ class cGrCamera; //Declared in ""grcam.h"
extern void grInitSound(tSituation* s, int ncars);
extern void grShutdownSound(int ncars);
extern float grRefreshSound(tSituation *s, cGrCamera *camera);
extern void grRefreshSound(tSituation *s, cGrCamera *camera);
extern void grMuteSound(bool bOn = true);
#endif /* _GRSOUND_H_ */