From 578eac68112dc1fd8f6204691dbebd0e6331fb73 Mon Sep 17 00:00:00 2001 From: sigonasr2 Date: Mon, 28 Aug 2023 16:30:26 -0500 Subject: [PATCH] Added support for panning sound. --- olcCodeJam2023Entry/VirusAttack.cpp | 26 +++++++++++++++++---- olcCodeJam2023Entry/VirusAttack.h | 4 +++- olcCodeJam2023Entry/olcPGEX_AudioListener.h | 17 ++++++++++++++ olcCodeJam2023Entry/olcPGEX_AudioSource.h | 11 ++++++--- 4 files changed, 49 insertions(+), 9 deletions(-) diff --git a/olcCodeJam2023Entry/VirusAttack.cpp b/olcCodeJam2023Entry/VirusAttack.cpp index bd281a7..639c18a 100644 --- a/olcCodeJam2023Entry/VirusAttack.cpp +++ b/olcCodeJam2023Entry/VirusAttack.cpp @@ -51,8 +51,7 @@ bool VirusAttack::OnUserCreate(){ IMAGES[MATRIX]->Sprite()->SetSampleMode(Sprite::PERIODIC); AL.AudioSystemInit(); - AS_Test.AL = &AL; - AS_Test.LoadAudioSample(0, "./assets/test.wav"); + InitializeSounds(); units.push_back(std::make_unique(vf2d{128,128},IMAGES,true)); units.push_back(std::make_unique(vf2d{129,129},IMAGES,true)); @@ -76,6 +75,21 @@ bool VirusAttack::OnUserCreate(){ return true; } +void VirusAttack::InitializeSounds(){ + int soundIndex=0; + auto LoadSound=[&](Audio&audio,std::string soundFilename){ + audio.AL=&AL; + audio.LoadAudioSample(soundIndex,std::string("./assets/"+soundFilename).c_str()); + soundIndex++; + }; + + AS_Test.SetDefaults(5,1,0,1,false); + AS_Test.fPlaySpeed=5; + + LoadSound(AS_Test,"test.wav"); + LoadSound(explosion,"SampleA.wav"); +} + void VirusAttack::HandleDraggingSelection(){ auto NotClickingOnMinimap=[&](){return !(GetMouseX()>=ScreenWidth()-64&&GetMouseY()>=ScreenHeight()-64);}; if(GetMouse(0).bPressed){ @@ -312,9 +326,11 @@ bool VirusAttack::OnUserUpdate(float fElapsedTime){ HandleRightClickMove(); HandlePanAndZoom(fElapsedTime); HandleMinimapClick(); - - if (GetKey(olc::Key::P).bPressed) - AS_Test.Play(); + AL.vecPos=game.GetWorldOffset()+GetScreenSize()/2; + AL.OnUserUpdate(fElapsedTime); + if (GetKey(olc::Key::P).bPressed){ + AS_Test.Play(1,1); + } for(auto&tile:TileManager::visibleTiles){ tile.second-=fElapsedTime; diff --git a/olcCodeJam2023Entry/VirusAttack.h b/olcCodeJam2023Entry/VirusAttack.h index 0644044..7f1809d 100644 --- a/olcCodeJam2023Entry/VirusAttack.h +++ b/olcCodeJam2023Entry/VirusAttack.h @@ -35,7 +35,8 @@ private: std::map>IMAGES; olcPGEX_AudioListener AL; - olcPGEX_AudioSource AS_Test; + Audio AS_Test,explosion; + Audio*bgm; TileTransformedView game; @@ -58,6 +59,7 @@ private: void UpdateMatrixTexture(float fElapsedTime); void RenderCollectionPoints(CollectionPoint*cp); void RenderFogOfWar(); + void InitializeSounds(); public: VirusAttack(); diff --git a/olcCodeJam2023Entry/olcPGEX_AudioListener.h b/olcCodeJam2023Entry/olcPGEX_AudioListener.h index f0a5108..6b3777d 100644 --- a/olcCodeJam2023Entry/olcPGEX_AudioListener.h +++ b/olcCodeJam2023Entry/olcPGEX_AudioListener.h @@ -170,6 +170,12 @@ Justin Richards #include "soloud.h" #include "soloud_wav.h" +struct PlayingInstance{ + vf2d pos; + float vol; + int handle; +}; + class olcPGEX_AudioListener : public olc::PGEX { public: @@ -201,6 +207,7 @@ public: // Vector of Audio Samples std::vector audioSamples; + std::vector handles; std::list wavs; @@ -220,6 +227,8 @@ public: // Calculate distance between listener and source float GetDistance(olc::vf2d sourcePos, bool returnRoot = true); + + void OnUserUpdate(float fElapsedTime); }; #ifdef AUDIO_LISTENER_IMPLEMENTATION @@ -280,6 +289,14 @@ float olcPGEX_AudioListener::GetDistance(olc::vf2d sourcePos, bool returnRoot) } +void olcPGEX_AudioListener::OnUserUpdate(float fElapsedTime){ + std::erase_if(handles,[&](PlayingInstance&handle){return !soloud.isValidVoiceHandle(handle.handle);}); + for(PlayingInstance&handle:handles){ + soloud.setPan(handle.handle,std::clamp((handle.pos.x-vecPos.x)/1024,-1.f,1.f)); + soloud.setVolume(handle.handle,handle.vol*std::max(0.f,abs(1-std::min(1.0f,(GetDistance(handle.pos)/1024.f))))); + } +} + #endif // AUDIO_LISTENER_IMPLEMENTATION #endif \ No newline at end of file diff --git a/olcCodeJam2023Entry/olcPGEX_AudioSource.h b/olcCodeJam2023Entry/olcPGEX_AudioSource.h index 473e5b1..7bf6d17 100644 --- a/olcCodeJam2023Entry/olcPGEX_AudioSource.h +++ b/olcCodeJam2023Entry/olcPGEX_AudioSource.h @@ -101,7 +101,6 @@ public: olc::vf2d pos = { 0.0f, 0.0f }; - // Instruct Audio Listener to load this sound (if not loaded already) void LoadAudioSample(int ID, const char* fileName); @@ -141,17 +140,21 @@ void olcPGEX_AudioSource::Play(float speed, float vol, bool looping, bool paused { // Set parameters fPlaySpeed = speed; - fVolume = vol; + fVolume = vol*std::max(0.f,abs(1-std::min(1.0f,(AL->GetDistance(pos)/1024.f)))); bLooping = looping; bPaused = paused; // Assign a handle to this instance of the sound we are about to play handle = AL->soloud.play(*AL->GetAudioSampleByID(nID)->wav, fVolume, 0.0f, bPaused); + AL->handles.push_back({pos,vol,handle}); + // Set speed and looping AL->soloud.setRelativePlaySpeed(handle, fPlaySpeed); AL->soloud.setLooping(handle, looping); + AL->soloud.setPan(handle,std::clamp((pos.x-AL->vecPos.x)/1024,-1.f,1.f)); + // Update Play status bIsPlaying = true; } @@ -221,4 +224,6 @@ void olcPGEX_AudioSource::SetDefaults(float speed, float vol, float minVol, floa } #endif // AUDIO_SOURCE_IMPLEMENTATION -#endif \ No newline at end of file +#endif + +typedef olcPGEX_AudioSource Audio; \ No newline at end of file