From 603546f642cabc81e7b554658871890cbd0017d0 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Fri, 1 Mar 2024 14:47:12 +0300 Subject: [PATCH] audio::playStream + speaker relative property --- src/audio/AL/ALAudio.cpp | 8 ++++++++ src/audio/AL/ALAudio.h | 3 +++ src/audio/audio.cpp | 16 ++++++++++++++++ src/audio/audio.h | 38 +++++++++++++++++++++++++++++++++++++- src/coders/ogg.cpp | 2 +- 5 files changed, 65 insertions(+), 2 deletions(-) diff --git a/src/audio/AL/ALAudio.cpp b/src/audio/AL/ALAudio.cpp index 464dd12a..c1798537 100644 --- a/src/audio/AL/ALAudio.cpp +++ b/src/audio/AL/ALAudio.cpp @@ -197,6 +197,14 @@ glm::vec3 ALSpeaker::getVelocity() const { return AL::getSource3f(source, AL_VELOCITY); } +void ALSpeaker::setRelative(bool relative) { + AL_CHECK(alSourcei(source, AL_SOURCE_RELATIVE, relative ? AL_TRUE : AL_FALSE)); +} + +bool ALSpeaker::isRelative() const { + return AL::getSourcei(source, AL_SOURCE_RELATIVE) == AL_TRUE; +} + int ALSpeaker::getPriority() const { return priority; } diff --git a/src/audio/AL/ALAudio.h b/src/audio/AL/ALAudio.h index ca973f48..f8d02c2c 100644 --- a/src/audio/AL/ALAudio.h +++ b/src/audio/AL/ALAudio.h @@ -103,6 +103,9 @@ namespace audio { void setVelocity(glm::vec3 vel) override; glm::vec3 getVelocity() const override; + void setRelative(bool relative) override; + bool isRelative() const override; + int getPriority() const override; }; diff --git a/src/audio/audio.cpp b/src/audio/audio.cpp index dc12f93c..4cf0fdb5 100644 --- a/src/audio/audio.cpp +++ b/src/audio/audio.cpp @@ -208,6 +208,7 @@ void remove_lower_priority_speaker(int priority) { speakerid_t audio::play( Sound* sound, glm::vec3 position, + bool relative, float volume, float pitch, bool loop, @@ -227,6 +228,7 @@ speakerid_t audio::play( speaker->setVolume(volume); speaker->setPitch(pitch); speaker->setLoop(loop); + speaker->setRelative(relative); speaker->play(); return id; } @@ -234,6 +236,7 @@ speakerid_t audio::play( speakerid_t audio::play( std::shared_ptr stream, glm::vec3 position, + bool relative, float volume, float pitch, bool loop @@ -255,10 +258,23 @@ speakerid_t audio::play( speaker->setVolume(volume); speaker->setPitch(pitch); speaker->setLoop(false); + speaker->setRelative(relative); speaker->play(); return id; } +speakerid_t audio::playStream( + const fs::path& file, + glm::vec3 position, + bool relative, + float volume, + float pitch, + bool loop +) { + std::shared_ptr stream (openStream(file, false)); + return play(stream, position, relative, volume, pitch, loop); +} + Speaker* audio::get(speakerid_t id) { auto found = speakers.find(id); if (found == speakers.end()) { diff --git a/src/audio/audio.h b/src/audio/audio.h index 7b14c652..11fc49bc 100644 --- a/src/audio/audio.h +++ b/src/audio/audio.h @@ -234,10 +234,24 @@ namespace audio { /// @return speaker priority value virtual int getPriority() const = 0; + /// @brief Determines if the position is relative to the listener + /// @param relative true - relative to the listener (default: false) + virtual void setRelative(bool relative) = 0; + + /// @brief Determines if the position is relative to the listener + virtual bool isRelative() const = 0; + + /// @brief Check if speaker is playing + inline bool isPlaying() const { + return getState() == State::playing; + } + + /// @brief Check if speaker is paused inline bool isPaused() const { return getState() == State::paused; } + /// @brief Check if speaker is stopped inline bool isStopped() const { return getState() == State::stopped; } @@ -319,6 +333,7 @@ namespace audio { /// @brief Play 3D sound in the world /// @param sound target sound /// @param position sound world position + /// @param relative position speaker relative to listener /// @param volume sound volume [0.0-1.0] /// @param pitch sound pitch multiplier [0.0-...] /// @param loop loop sound @@ -328,15 +343,17 @@ namespace audio { extern speakerid_t play( Sound* sound, glm::vec3 position, + bool relative, float volume, float pitch, bool loop, int priority ); - /// @brief Play stream in the world + /// @brief Play stream /// @param stream target stream /// @param position stream world position + /// @param relative position speaker relative to listener /// @param volume stream volume [0.0-1.0] /// @param pitch stream pitch multiplier [0.0-...] /// @param loop loop stream @@ -344,6 +361,25 @@ namespace audio { extern speakerid_t play( std::shared_ptr stream, glm::vec3 position, + bool relative, + float volume, + float pitch, + bool loop + ); + + /// @brief Play stream from file + /// @param file audio file path + /// @param position stream world position + /// @param relative position speaker relative to listener + /// @param volume stream volume [0.0-1.0] + /// @param pitch stream pitch multiplier [0.0-...] + /// @param loop loop stream + /// @return speaker id or 0 + /// @return + extern speakerid_t playStream( + const fs::path& file, + glm::vec3 position, + bool relative, float volume, float pitch, bool loop diff --git a/src/coders/ogg.cpp b/src/coders/ogg.cpp index 90ac0ef1..a7e8568d 100644 --- a/src/coders/ogg.cpp +++ b/src/coders/ogg.cpp @@ -17,7 +17,7 @@ static inline const char* vorbis_error_message(int code) { case OV_EVERSION: return "vorbis version mismatch"; case OV_EBADHEADER: return "invalid Vorbis bitstream header"; case OV_EFAULT: return "internal logic fault"; - case OV_EINVAL: return "header couldn't be read or are corrupt"; + case OV_EINVAL: return "invalid read operation"; default: return "unknown"; }