diff --git a/src/audio/AudioEngine.h b/src/audio/AudioEngine.h index fa972027..09d9ceed 100644 --- a/src/audio/AudioEngine.h +++ b/src/audio/AudioEngine.h @@ -118,7 +118,7 @@ namespace Audio virtual void stop() = 0; }; - using SoundRef = std::shared_ptr; + using SoundPtr = std::shared_ptr; using SoundStream = std::function; struct Orientation { @@ -161,15 +161,15 @@ namespace Audio /** * @brief Creates a sound object from an audio buffer * - * @return SoundRef + * @return SoundPtr */ - virtual SoundRef createSound(const std::int16_t* buf, std::size_t len, Format, std::size_t samplingFreq) = 0; + virtual SoundPtr createSound(const std::int16_t* buf, std::size_t len, Format, std::size_t samplingFreq) = 0; /** * @brief Creates a sound object from an audio stream * - * @return SoundRef + * @return SoundPtr */ - virtual SoundRef createSound(SoundStream, Format, std::size_t samplingFreq) = 0; + virtual SoundPtr createSound(SoundStream, Format, std::size_t samplingFreq) = 0; }; } diff --git a/src/audio/AudioWorld.cpp b/src/audio/AudioWorld.cpp index 7b197318..1bc2659d 100644 --- a/src/audio/AudioWorld.cpp +++ b/src/audio/AudioWorld.cpp @@ -110,7 +110,7 @@ namespace World LogInfo() << "All segments loaded."; m_MusicSource = m_Engine.getAudioEngine().createSound( - [&](std::int16_t* buf, std::size_t len) -> int { + [&](auto buf, auto len) -> int { if (m_exiting) return -1; m_musicContext->renderBlock(buf, len); @@ -171,7 +171,7 @@ namespace World try { - const std::int16_t* dataPtr = (std::int16_t*)(wav.getData()); + auto dataPtr = reinterpret_cast(wav.getData()); auto format = wav.getChannels() == 1 ? Audio::Format::Mono : Audio::Format::Stereo; auto sound = m_Engine.getAudioEngine().createSound(dataPtr, wav.getDataSize(), format, wav.getRate()); snd->sound = sound; @@ -201,7 +201,7 @@ namespace World return loadAudioVDF(m_VDFSIndex, name); } - Audio::SoundRef AudioWorld::playSound(Handle::SfxHandle h, const Math::float3& position, bool relative, float maxDist) + Audio::SoundPtr AudioWorld::playSound(Handle::SfxHandle h, const Math::float3& position, bool relative, float maxDist) { Sound& snd = m_Allocator.getElement(h); @@ -321,12 +321,12 @@ namespace World } while (v.isValid()); } - Audio::SoundRef AudioWorld::playSound(Handle::SfxHandle h) + Audio::SoundPtr AudioWorld::playSound(Handle::SfxHandle h) { return playSound(h, Math::float3(0, 0, 0), true); } - Audio::SoundRef AudioWorld::playSound(const std::string& name) + Audio::SoundPtr AudioWorld::playSound(const std::string& name) { // Check if that sound has already been loaded. If not, load it now. Handle::SfxHandle h = loadAudioVDF(name); @@ -340,12 +340,12 @@ namespace World return playSound(h, Math::float3(0, 0, 0), true); } - Audio::SoundRef AudioWorld::playSound(Handle::SfxHandle h, const Math::float3& position, float maxDist) + Audio::SoundPtr AudioWorld::playSound(Handle::SfxHandle h, const Math::float3& position, float maxDist) { return playSound(h, position, false, maxDist); } - Audio::SoundRef AudioWorld::playSound(const std::string& name, const Math::float3& position, float maxDist) + Audio::SoundPtr AudioWorld::playSound(const std::string& name, const Math::float3& position, float maxDist) { // Check if that sound has already been loaded. If not, load it now. Handle::SfxHandle h = loadAudioVDF(name); @@ -359,7 +359,7 @@ namespace World return playSound(h, position, false, maxDist); } - Audio::SoundRef AudioWorld::playSoundVariantRandom(const std::string& name, const Math::float3& position, float maxDist) + Audio::SoundPtr AudioWorld::playSoundVariantRandom(const std::string& name, const Math::float3& position, float maxDist) { // Check if that sound has already been loaded. If not, load it now. Handle::SfxHandle h = loadAudioVDF(name); @@ -373,14 +373,14 @@ namespace World return playSoundVariantRandom(h, position, maxDist); } - Audio::SoundRef AudioWorld::playSoundVariantRandom(Handle::SfxHandle h, const Math::float3& position, float maxDist) + Audio::SoundPtr AudioWorld::playSoundVariantRandom(Handle::SfxHandle h, const Math::float3& position, float maxDist) { Sound& snd = m_Allocator.getElement(h); return playSound(snd.variants[rand() % snd.variants.size()], position, false, maxDist); } - Audio::SoundRef AudioWorld::playSoundVariantRandom(const std::string& name) + Audio::SoundPtr AudioWorld::playSoundVariantRandom(const std::string& name) { // Check if that sound has already been loaded. If not, load it now. Handle::SfxHandle h = loadAudioVDF(name); @@ -394,7 +394,7 @@ namespace World return playSoundVariantRandom(h); } - Audio::SoundRef AudioWorld::playSoundVariantRandom(Handle::SfxHandle h) + Audio::SoundPtr AudioWorld::playSoundVariantRandom(Handle::SfxHandle h) { Sound& snd = m_Allocator.getElement(h); diff --git a/src/audio/AudioWorld.h b/src/audio/AudioWorld.h index e7809aab..8a1971ee 100644 --- a/src/audio/AudioWorld.h +++ b/src/audio/AudioWorld.h @@ -67,16 +67,16 @@ namespace World /** * Plays the sound of the given handle/name */ - Audio::SoundRef playSound(Handle::SfxHandle h, const Math::float3& position, bool relative, float maxDist = FLT_MAX); - Audio::SoundRef playSound(Handle::SfxHandle h); - Audio::SoundRef playSound(const std::string& name); - Audio::SoundRef playSoundVariantRandom(const std::string& name); - Audio::SoundRef playSoundVariantRandom(Handle::SfxHandle h); + Audio::SoundPtr playSound(Handle::SfxHandle h, const Math::float3& position, bool relative, float maxDist = FLT_MAX); + Audio::SoundPtr playSound(Handle::SfxHandle h); + Audio::SoundPtr playSound(const std::string& name); + Audio::SoundPtr playSoundVariantRandom(const std::string& name); + Audio::SoundPtr playSoundVariantRandom(Handle::SfxHandle h); - Audio::SoundRef playSound(Handle::SfxHandle h, const Math::float3& position, float maxDist = FLT_MAX); - Audio::SoundRef playSound(const std::string& name, const Math::float3& position, float maxDist = FLT_MAX); - Audio::SoundRef playSoundVariantRandom(const std::string& name, const Math::float3& position, float maxDist = FLT_MAX); - Audio::SoundRef playSoundVariantRandom(Handle::SfxHandle h, const Math::float3& position, float maxDist = FLT_MAX); + Audio::SoundPtr playSound(Handle::SfxHandle h, const Math::float3& position, float maxDist = FLT_MAX); + Audio::SoundPtr playSound(const std::string& name, const Math::float3& position, float maxDist = FLT_MAX); + Audio::SoundPtr playSoundVariantRandom(const std::string& name, const Math::float3& position, float maxDist = FLT_MAX); + Audio::SoundPtr playSoundVariantRandom(Handle::SfxHandle h, const Math::float3& position, float maxDist = FLT_MAX); /** * Plays the segment identified by a name @@ -122,7 +122,7 @@ namespace World { Daedalus::GEngineClasses::C_SFX sfx; std::vector variants; // Instances ending with "_Ax" - Audio::SoundRef sound = nullptr; + Audio::SoundPtr sound; std::string name; }; @@ -146,13 +146,13 @@ namespace World /** * List of currently playing sounds or sounds that have been playing */ - std::list m_Sources; + std::list m_Sources; /** * Audio source for music * */ - Audio::SoundRef m_MusicSource; + Audio::SoundPtr m_MusicSource; /** * Holds the music state diff --git a/src/audio/NullAudioEngine.cpp b/src/audio/NullAudioEngine.cpp index 644fbf78..b28853d2 100644 --- a/src/audio/NullAudioEngine.cpp +++ b/src/audio/NullAudioEngine.cpp @@ -30,12 +30,12 @@ class NullAudioSound final }; } -SoundRef NullAudioEngine::createSound(const std::int16_t*, std::size_t, Format, std::size_t) +SoundPtr NullAudioEngine::createSound(const std::int16_t*, std::size_t, Format, std::size_t) { return std::make_shared(); } -SoundRef NullAudioEngine::createSound(SoundStream, Format, std::size_t) +SoundPtr NullAudioEngine::createSound(SoundStream, Format, std::size_t) { return std::make_shared(); } \ No newline at end of file diff --git a/src/audio/NullAudioEngine.h b/src/audio/NullAudioEngine.h index d00b071e..81a21e30 100644 --- a/src/audio/NullAudioEngine.h +++ b/src/audio/NullAudioEngine.h @@ -24,8 +24,8 @@ namespace Audio void orientation(const Orientation& o) override {} - SoundRef createSound(const std::int16_t* buf, std::size_t len, Format, std::size_t) override; + SoundPtr createSound(const std::int16_t* buf, std::size_t len, Format, std::size_t) override; - SoundRef createSound(SoundStream, Format, std::size_t) override; + SoundPtr createSound(SoundStream, Format, std::size_t) override; }; } \ No newline at end of file diff --git a/src/audio/OpenALAudioEngine.cpp b/src/audio/OpenALAudioEngine.cpp index 6dda4dc6..ccc372df 100644 --- a/src/audio/OpenALAudioEngine.cpp +++ b/src/audio/OpenALAudioEngine.cpp @@ -95,8 +95,8 @@ OpenALAudioEngine::OpenALAudioEngine(const std::string& deviceName) OpenALAudioEngine::~OpenALAudioEngine() { - m_bufferedSounds.erase(std::begin(m_bufferedSounds), std::end(m_bufferedSounds)); - m_streamingSounds.erase(std::begin(m_streamingSounds), std::end(m_streamingSounds)); + m_bufferedSounds.clear(); + m_streamingSounds.clear(); alDeleteSources(m_freeSources.size(), m_freeSources.data()); alcMakeContextCurrent(nullptr); @@ -120,19 +120,19 @@ void OpenALAudioEngine::gain(float g) void OpenALAudioEngine::position(const Math::float3& p) { captureContext(); - alListenerfv(AL_POSITION, (ALfloat*)&p); + alListenerfv(AL_POSITION, reinterpret_cast(&p)); } void OpenALAudioEngine::velocity(const Math::float3& v) { captureContext(); - alListenerfv(AL_VELOCITY, (ALfloat*)&v); + alListenerfv(AL_VELOCITY, reinterpret_cast(&v)); } void OpenALAudioEngine::orientation(const Orientation& o) { captureContext(); - alListenerfv(AL_ORIENTATION, (ALfloat*)&o); + alListenerfv(AL_ORIENTATION, reinterpret_cast(&o)); } namespace Audio @@ -149,7 +149,8 @@ namespace Audio , m_engine(engine) {} public: - OpenALSound(OpenALAudioEngine* engine, const std::int16_t* buf, std::size_t len, Format fmt, std::size_t samplingFreq) + OpenALSound(OpenALAudioEngine* engine, const std::int16_t* buf, + std::size_t len, Format fmt, std::size_t samplingFreq) : m_buffer(0) , m_hasBuffer(true) , m_source(0) @@ -227,19 +228,19 @@ namespace Audio void position(const Math::float3& p) override { getSource(); - alSourcefv(m_source, AL_POSITION, (ALfloat*)&p); + alSourcefv(m_source, AL_POSITION, reinterpret_cast(&p)); } void velocity(const Math::float3& v) override { getSource(); - alSourcefv(m_source, AL_VELOCITY, (ALfloat*)&v); + alSourcefv(m_source, AL_VELOCITY, reinterpret_cast(&v)); } void direction(const Math::float3& d) override { getSource(); - alSourcefv(m_source, AL_DIRECTION, (ALfloat*)&d); + alSourcefv(m_source, AL_DIRECTION, reinterpret_cast(&d)); } void relative(bool r) override @@ -325,7 +326,8 @@ namespace Audio */ static constexpr std::size_t BUFFER_SIZE = 1024; public: - StreamingOpenALSound(OpenALAudioEngine* engine, SoundStream stream, Format fmt, std::size_t sampleRate) + StreamingOpenALSound(OpenALAudioEngine* engine, + SoundStream stream, Format fmt, std::size_t sampleRate) : OpenALSound(engine) , m_stream(stream) , m_format(fmt == Format::Mono ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16) @@ -432,7 +434,7 @@ namespace Audio }; } // namespace Audio -SoundRef OpenALAudioEngine::createSound(const std::int16_t* buf, std::size_t len, Format fmt, std::size_t sampleRate) +SoundPtr OpenALAudioEngine::createSound(const std::int16_t* buf, std::size_t len, Format fmt, std::size_t sampleRate) { captureContext(); auto ptr = std::make_shared(this, buf, len, fmt, sampleRate); @@ -440,7 +442,7 @@ SoundRef OpenALAudioEngine::createSound(const std::int16_t* buf, std::size_t len return ptr; } -SoundRef OpenALAudioEngine::createSound(SoundStream stream, Format fmt, std::size_t sampleRate) +SoundPtr OpenALAudioEngine::createSound(SoundStream stream, Format fmt, std::size_t sampleRate) { captureContext(); auto ptr = std::make_shared(this, stream, fmt, sampleRate); @@ -451,11 +453,12 @@ SoundRef OpenALAudioEngine::createSound(SoundStream stream, Format fmt, std::siz ALuint OpenALAudioEngine::getFreeSource() { captureContext(); + + auto lambda = [](const auto& sound) { return sound.use_count() == 1; }; + // Remove all expired sounds - std::remove_if(std::begin(m_bufferedSounds), std::end(m_bufferedSounds), - [](const std::shared_ptr& sound) { return sound.use_count() == 1; }); - std::remove_if(std::begin(m_streamingSounds), std::end(m_streamingSounds), - [](const std::shared_ptr& sound) { return sound.use_count() == 1; }); + std::remove_if(std::begin(m_bufferedSounds), std::end(m_bufferedSounds), lambda); + std::remove_if(std::begin(m_streamingSounds), std::end(m_streamingSounds), lambda); if (m_freeSources.empty()) { diff --git a/src/audio/OpenALAudioEngine.h b/src/audio/OpenALAudioEngine.h index b4f81675..92113c86 100644 --- a/src/audio/OpenALAudioEngine.h +++ b/src/audio/OpenALAudioEngine.h @@ -32,9 +32,9 @@ namespace Audio void orientation(const Orientation&) override; - SoundRef createSound(const std::int16_t* buf, std::size_t len, Format, std::size_t) override; + SoundPtr createSound(const std::int16_t* buf, std::size_t len, Format, std::size_t) override; - SoundRef createSound(SoundStream, Format, std::size_t) override; + SoundPtr createSound(SoundStream, Format, std::size_t) override; private: ALCdevice* m_device = nullptr; ALCcontext* m_context = nullptr; diff --git a/src/logic/PlayerController.h b/src/logic/PlayerController.h index b8bb5b2d..9b54cb40 100644 --- a/src/logic/PlayerController.h +++ b/src/logic/PlayerController.h @@ -575,6 +575,6 @@ namespace Logic size_t m_LastAniRootPosUpdatedAniHash; // Main noise sound slot. Other sounds using it won't play if there is already a sound playing here. - Audio::SoundRef m_MainNoiseSoundSlot; + Audio::SoundPtr m_MainNoiseSoundSlot; }; } // namespace Logic diff --git a/src/logic/SoundController.h b/src/logic/SoundController.h index 636fe66a..86aaa41f 100644 --- a/src/logic/SoundController.h +++ b/src/logic/SoundController.h @@ -77,7 +77,7 @@ namespace Logic /** * Handle to the currently played or last played sound */ - Audio::SoundRef m_PlayedSound; + Audio::SoundPtr m_PlayedSound; /** * Time when to play this for the next time (seconds) diff --git a/src/logic/messages/EventMessage.h b/src/logic/messages/EventMessage.h index 1b5a4061..710d7353 100644 --- a/src/logic/messages/EventMessage.h +++ b/src/logic/messages/EventMessage.h @@ -603,7 +603,7 @@ namespace Logic /** * Ticket. Can be used to ask AudioWorld if sound is playing. */ - Audio::SoundRef soundTicket; + Audio::SoundPtr soundTicket; }; struct MagicMessage : public NpcMessage