Skip to content

Commit

Permalink
new SoundDynamic, Ambient wip
Browse files Browse the repository at this point in the history
  • Loading branch information
cryham committed Nov 29, 2024
1 parent 6d0b5dd commit 1261ad6
Show file tree
Hide file tree
Showing 14 changed files with 379 additions and 53 deletions.
4 changes: 4 additions & 0 deletions data/sounds/sounds.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,7 @@ ambient/1
{
sound 1.0 fluids/water_cont.wav
}
ambient/2
{
sound 1.0 hud/win0.ogg
}
9 changes: 5 additions & 4 deletions docs/Roadmap.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ Marked: `crucial`, **Next or Big**, _Important_.
----
### 🪄 Effects

- HDR, bloom, [fix fireflies](https://catlikecoding.com/unity/tutorials/custom-srp/hdr/)
- lens flare, [shaders](https://www.shadertoy.com/results?query=lens+flare)
- lens *flare*, [shaders](https://www.shadertoy.com/results?query=lens+flare), [code](https://nehe.gamedev.net/tutorial/3d_lens_flare_with_occlusion_testing/16007/)
- sunrays, [shaders](https://www.shadertoy.com/results?query=tag%3Dgodrays)
- **Soft** Particles
- HDR, bloom, [fix fireflies](https://catlikecoding.com/unity/tutorials/custom-srp/hdr/), adjust all tracks
- *Soft* Particles
- old motion blur


Expand All @@ -27,7 +27,6 @@ sound engine from [RoR PR](https://github.com/RigsOfRods/rigs-of-rods/pull/3182)
or replace with [ogre-audiovideo](https://github.com/OGRECave/ogre-audiovideo))
or add **new** code for:
- dynamic hit [sounds pool](https://github.com/kcat/openal-soft/issues/972#issuecomment-1934265230) (e.g. 10), auto removed after play
- ambient sound, reload buffer for track
- more tire sounds, load only used (few, pool e.g. 2), or:
- init more sources, gui opt, [ref 1](https://github.com/kcat/openal-soft/issues/736), [ref 2](https://github.com/kcat/openal-soft/issues/972)
- ed mode, track sounds: waterfall, volcano, river..
Expand Down Expand Up @@ -77,6 +76,8 @@ car glass reflect cube sky pos bad on Test1-Flat?
own rqg for veget on horiz? to skip in wtr refl etc

specular on terrain
trails after particles/clouds
car glass, pipes, beam etc not visible in fluid if seen from above

rivers wrong way
waterfalls refract cuts off above cam y
Expand Down
11 changes: 10 additions & 1 deletion src/game/Game_SceneInit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,8 @@ void App::LoadCleanUp()

if (dstTrk)
{
pGame->snd->DestroyAmbient(); // 🔉

scn->refl.DestroyFluids();
scn->refl.DestroyRTT();

Expand Down Expand Up @@ -323,7 +325,7 @@ void App::LoadCleanUp()
{
scn->DelRoadDens();
scn->grass->Destroy(); // 🌿
scn->DestroyVegets(); // 🌳🪨
scn->DestroyVegets(); // 🌳🪨

DestroyObjects(true); // 📦🏢
DestroyCollects(true); // 💎
Expand Down Expand Up @@ -820,6 +822,13 @@ void App::LoadView(int c)
// ⏱️ HUD etc 10 last
void App::LoadMisc()
{
/* static int a = 1;
pGame->snd->CreateAmbient("ambient/"+toStr(a)); // 🔉
++a;
if (a > 2) a = 1;
// sc->ambientSnd
// sc->ambientVol
*/
bool rev = pSet->game.track_reversed;
/**if (pGame && !pGame->cars.empty()) //todo: move this into gui track tab chg evt, for cur game type
gcom->UpdGuiRdStats(scn->road, scn->sc, gcom->sListTrack,
Expand Down
2 changes: 1 addition & 1 deletion src/game/Game_Update.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void App::update( float dt )
#endif


if (pSet->g.ssao) //-
if (pSet->g.ssao) //- in ReflectListener::passEarlyPreExecute
UpdateSSAO(mCamera); // 🕳️
if (pSet->gi)
UpdateGI(); // 🌄
Expand Down
2 changes: 1 addition & 1 deletion src/game/Hud_Graphs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ void App::GraphsNewVals() // Game
graphs[0]->UpdTitle("Sounds Info\n"+snd->sReverb);
graphs[1]->UpdTitle("\nbuf: "+toStr(snd->buffers_used_max)+" /"+toStr(SoundBaseMgr::MAX_BUFFERS)+"\n"+
"src: "+toStr(snd->sources_use)+" /"+toStr(snd->hw_sources_num)+" inst: "+toStr(Sound::instances));
graphs[2]->UpdTitle("hw: "+iToStr(snd->hw_sources_use,2)+" /"+toStr(SoundBaseMgr::HW_SRC));
graphs[2]->UpdTitle("hw: "+iToStr(snd->hw_sources_use,2)+" /"+toStr(SoundBaseMgr::ALL_SRC));

bool info = pSet->sounds_info;
if (info)
Expand Down
2 changes: 1 addition & 1 deletion src/sound/SoundBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void SoundBase::computeAudibility(Vector3 pos1)
{
audibility = gain;
return;
}
}

// 3d
float distance = (pos1 - pos).length();
Expand Down
3 changes: 2 additions & 1 deletion src/sound/SoundBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
// https://github.com/RigsOfRods/rigs-of-rods
#pragma once
#include <AL/al.h>
#include <Ogre.h>
#include <OgreVector3.h>
#include <OgreString.h>

enum RecomputeSource {
REASON_PLAY, REASON_STOP, REASON_GAIN, REASON_LOOP, REASON_PTCH, REASON_POS, REASON_VEL, REASON_SEEK };
Expand Down
23 changes: 11 additions & 12 deletions src/sound/SoundBaseMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,9 @@ const float SoundBaseMgr::ROLLOFF_FACTOR = 0.05f; // 0.05 0.1 1.0 new
// Init
//---------------------------------------------------------------------------------------------
SoundBaseMgr::SoundBaseMgr()
:buffers_use(0), buffers_used_max(0), sources_use(0)
,hw_sources_use(0), hw_sources_num(0)
,context(NULL), device(NULL)
,slot(0), effect(0), master_volume(1.f)
{
hw_sources_map.resize(HW_SRC,0);
hw_sources.resize(HW_SRC,0);
hw_sources_map.resize(ALL_SRC,0);
hw_sources.resize(ALL_SRC,0);
sources.resize(MAX_BUFFERS,0);
buffers.resize(MAX_BUFFERS,0);
buffer_file.resize(MAX_BUFFERS);
Expand Down Expand Up @@ -73,7 +69,7 @@ bool SoundBaseMgr::Init(std::string snd_device, bool reverb1)

// ALint attr[7/*reverb ? 7 : 4*/] = { 0 };
// attr[0] = ALC_MONO_SOURCES;
// attr[1] = 4*1024; // ok, can do more HW_SRC = 4096
// attr[1] = pSet->; //4*1024; // ok, can do more CREATE_SRC = 4096
// attr[2] = ALC_STEREO_SOURCES;
// attr[3] = 256;
// attr[4] = ALC_MAX_AUXILIARY_SENDS;
Expand Down Expand Up @@ -118,17 +114,20 @@ bool SoundBaseMgr::Init(std::string snd_device, bool reverb1)
// get function pointers
alGenEffects = (LPALGENEFFECTS)alGetProcAddress("alGenEffects");
alDeleteEffects = (LPALDELETEEFFECTS)alGetProcAddress("alDeleteEffects");

alIsEffect = (LPALISEFFECT)alGetProcAddress("alIsEffect");
alEffecti = (LPALEFFECTI)alGetProcAddress("alEffecti");
alEffectf = (LPALEFFECTF)alGetProcAddress("alEffectf");
alEffectfv = (LPALEFFECTFV)alGetProcAddress("alEffectfv");

alGenAuxiliaryEffectSlots = (LPALGENAUXILIARYEFFECTSLOTS)alGetProcAddress("alGenAuxiliaryEffectSlots");
alAuxiliaryEffectSloti = (LPALAUXILIARYEFFECTSLOTI)alGetProcAddress("alAuxiliaryEffectSloti");
alDeleteAuxiliaryEffectSlots = (LPALDELETEAUXILIARYEFFECTSLOTS)alGetProcAddress("alDeleteAuxiliaryEffectSlots");


// doppler
alDopplerFactor(0.f); // 1.f todo: vel..
//alSpeedOfSound(343.3f);
//alDopplerVelocity(343.f);

alDistanceModel(AL_INVERSE_DISTANCE_CLAMPED); //+
Expand Down Expand Up @@ -183,7 +182,7 @@ void SoundBaseMgr::CreateSources()
if (!device) return;
LogO("@ @ Creating hw sources.");
int i;
for (i = 0; i < HW_SRC; ++i)
for (i = 0; i < ALL_SRC; ++i)
{
alGetError();
alGenSources(1, &hw_sources[i]);
Expand All @@ -196,9 +195,9 @@ void SoundBaseMgr::CreateSources()
//LogO(toStr(i)+" +SRC: "+toStr(hw_sources[i]));
++hw_sources_num;
}
LogO("@ @ Created hw sources: "+toStr(hw_sources_num)+" / "+toStr(HW_SRC));
LogO("@ @ Created hw sources: "+toStr(hw_sources_num)+" / "+toStr(ALL_SRC));

for (i = 0; i < HW_SRC; ++i)
for (i = 0; i < ALL_SRC; ++i)
hw_sources_map[i] = -1;

buffers_used_max = buffers_use; // save for info
Expand All @@ -212,7 +211,7 @@ void SoundBaseMgr::DestroySources(bool all)

LogO("@ @ Destroying hw sources.");
int i;
for (int i = 0; i < HW_SRC; ++i)
for (int i = 0; i < ALL_SRC; ++i)
{
//LogO(toStr(i)+" -SRC: "+toStr(hw_sources[i]));
alSourceStop(hw_sources[i]);
Expand Down Expand Up @@ -361,7 +360,7 @@ void SoundBaseMgr::recomputeSource(int id, int reason, float fl, Vector3* vec)
}else
{
// now, compute who is the faintest
// note: we know the table m_hardware_sources_map is full!
// note: we know hw_sources_map is full!
float fv = 1.0f;
int al_faintest = 0;
for (int i=0; i < hw_sources_num; i++)
Expand Down
47 changes: 27 additions & 20 deletions src/sound/SoundBaseMgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <AL/al.h>
#include <AL/alc.h>
#include <AL/efx.h>
class SoundBase; struct REVERB_PRESET;
class SoundBase; class SoundDynamic; struct REVERB_PRESET;

//#define REVERB_BROWSER // 🧰 _Tool_ keys 1,2: change prev,next reverb preset

Expand All @@ -32,23 +32,24 @@ class SoundBaseMgr

// const
static const float MAX_DISTANCE, REF_DISTANCE, ROLLOFF_FACTOR;
static const unsigned int HW_SRC = 256; //par, set..
static const unsigned int CREATE_SRC = 256; //par, context, all
static const unsigned int ALL_SRC = 226; //par, set.. const sources[], rest is for dynamic pool
static const unsigned int MAX_BUFFERS = 1024; //

// reverb ---
void SetReverb(std::string name);

bool reverb;
bool reverb =1;
std::string sReverb; // info
void InitReverbMap();
std::map <std::string, int> mapReverbs;
ALuint LoadEffect(const REVERB_PRESET* reverb);

// var
int hw_sources_num; // total number of available hardware sources < HW_SRC
int hw_sources_use;
int sources_use;
int buffers_use, buffers_used_max;
int hw_sources_num = 0; // total number of available hardware sources < ALL_SRC
int hw_sources_use = 0;
int sources_use = 0;
int buffers_use = 0, buffers_used_max = 0;

//private:
void recomputeAllSources();
Expand All @@ -62,17 +63,22 @@ class SoundBaseMgr
bool loadWAVFile(Ogre::String file, ALuint buffer, int& outSamples);
bool loadOGGFile(Ogre::String file, ALuint buffer, int& outSamples);

// ambient sound (own, low level)
// ALuint amb_source, amb_buffer;


// active audio sources (hardware sources)
// SR3 new, dynamic sounds ----
SoundDynamic* ambient =0; // one, looped, always play

// std::list<SoundDynamic*> dynamics; // auto removed
// void UpdateDynamic(float dt);


// audio sources ----
std::vector<SoundBase*> sources;

// active audio hardware sources
std::vector<int> hw_sources_map; // stores the hardware index for each source. -1 = unmapped
std::vector<ALuint> hw_sources; // this buffer contains valid AL handles up to hw_sources_num

// audio sources
std::vector<SoundBase*> sources;


// helper for calculating the most audible sources
std::pair<int, float> src_audible[MAX_BUFFERS];

Expand All @@ -82,17 +88,18 @@ class SoundBaseMgr

Ogre::Vector3 camera_position{0,0,0};

// al vars
ALCdevice* device;
ALCcontext* context;
// AL vars ----
ALCdevice* device =0;
ALCcontext* context =0;

ALuint slot, effect;
ALuint slot =0, effect =0;

float master_volume;
float master_volume = 1.f;


// function pointers
// AL function pointers
bool _checkALErrors(const char* file, int line);
private:
LPALGENEFFECTS alGenEffects;
LPALDELETEEFFECTS alDeleteEffects;
LPALISEFFECT alIsEffect;
Expand Down
19 changes: 11 additions & 8 deletions src/sound/SoundBaseMgrUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ SoundBase* SoundBaseMgr::createSound(String file, String name)
bool SoundBaseMgr::loadWAVFile(String file, ALuint buffer, int& outSamples)
{
outSamples = 0;
//LogO("@ Loading WAV file "+file);
LogO("@ Loading WAV: "+file);

// create the Stream
std::string path = PATHS::Sounds()+"/"+file;
Expand Down Expand Up @@ -180,8 +180,9 @@ bool SoundBaseMgr::loadWAVFile(String file, ALuint buffer, int& outSamples)
free(bdata);

if (error != AL_NO_ERROR)
{ LogO("@ OpenAL error while loading buffer for "+file+" : "+toStr(error)); return false; }

{ LogO("@ OpenAL error while loading buffer for "+file+" : "+toStr(error));
return false;
}
fd.close(); fi.close();
return true;
}
Expand All @@ -192,7 +193,7 @@ bool SoundBaseMgr::loadWAVFile(String file, ALuint buffer, int& outSamples)
bool SoundBaseMgr::loadOGGFile(String file, ALuint buffer, int& outSamples)
{
outSamples = 0;
//LogO("Loading OGG file "+file);
LogO("Loading OGG: "+file);

String path = PATHS::Sounds()+"/"+file;
FILE* fp = fopen(path.c_str(), "rb");
Expand Down Expand Up @@ -231,13 +232,15 @@ bool SoundBaseMgr::loadOGGFile(String file, ALuint buffer, int& outSamples)
delete[] bdata; // stream will be closed by itself

if (error != AL_NO_ERROR)
{ LogO("@ OpenAL error while loading buffer for "+file+" : "+toStr(error)); return false; }

//note: no need to call fclose(); ov_clear does it for us
{ LogO("@ OpenAL error while loading buffer for "+file+" : "+toStr(error));
return false;
}
// note: no need to call fclose(); ov_clear does it for us
ov_clear(&oggFile);
return true;
}
LogO("@ Can't open OGG sound file: "+file); return false;
LogO("@ Can't open OGG sound file: "+file);
return false;
}


Expand Down
Loading

0 comments on commit 1261ad6

Please sign in to comment.