audio::playStream + speaker relative property

This commit is contained in:
MihailRis 2024-03-01 14:47:12 +03:00
parent 2fab1593c8
commit 603546f642
5 changed files with 65 additions and 2 deletions

View File

@ -197,6 +197,14 @@ glm::vec3 ALSpeaker::getVelocity() const {
return AL::getSource3f(source, AL_VELOCITY); 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 { int ALSpeaker::getPriority() const {
return priority; return priority;
} }

View File

@ -103,6 +103,9 @@ namespace audio {
void setVelocity(glm::vec3 vel) override; void setVelocity(glm::vec3 vel) override;
glm::vec3 getVelocity() const override; glm::vec3 getVelocity() const override;
void setRelative(bool relative) override;
bool isRelative() const override;
int getPriority() const override; int getPriority() const override;
}; };

View File

@ -208,6 +208,7 @@ void remove_lower_priority_speaker(int priority) {
speakerid_t audio::play( speakerid_t audio::play(
Sound* sound, Sound* sound,
glm::vec3 position, glm::vec3 position,
bool relative,
float volume, float volume,
float pitch, float pitch,
bool loop, bool loop,
@ -227,6 +228,7 @@ speakerid_t audio::play(
speaker->setVolume(volume); speaker->setVolume(volume);
speaker->setPitch(pitch); speaker->setPitch(pitch);
speaker->setLoop(loop); speaker->setLoop(loop);
speaker->setRelative(relative);
speaker->play(); speaker->play();
return id; return id;
} }
@ -234,6 +236,7 @@ speakerid_t audio::play(
speakerid_t audio::play( speakerid_t audio::play(
std::shared_ptr<Stream> stream, std::shared_ptr<Stream> stream,
glm::vec3 position, glm::vec3 position,
bool relative,
float volume, float volume,
float pitch, float pitch,
bool loop bool loop
@ -255,10 +258,23 @@ speakerid_t audio::play(
speaker->setVolume(volume); speaker->setVolume(volume);
speaker->setPitch(pitch); speaker->setPitch(pitch);
speaker->setLoop(false); speaker->setLoop(false);
speaker->setRelative(relative);
speaker->play(); speaker->play();
return id; 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> stream (openStream(file, false));
return play(stream, position, relative, volume, pitch, loop);
}
Speaker* audio::get(speakerid_t id) { Speaker* audio::get(speakerid_t id) {
auto found = speakers.find(id); auto found = speakers.find(id);
if (found == speakers.end()) { if (found == speakers.end()) {

View File

@ -234,10 +234,24 @@ namespace audio {
/// @return speaker priority value /// @return speaker priority value
virtual int getPriority() const = 0; 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 { inline bool isPaused() const {
return getState() == State::paused; return getState() == State::paused;
} }
/// @brief Check if speaker is stopped
inline bool isStopped() const { inline bool isStopped() const {
return getState() == State::stopped; return getState() == State::stopped;
} }
@ -319,6 +333,7 @@ namespace audio {
/// @brief Play 3D sound in the world /// @brief Play 3D sound in the world
/// @param sound target sound /// @param sound target sound
/// @param position sound world position /// @param position sound world position
/// @param relative position speaker relative to listener
/// @param volume sound volume [0.0-1.0] /// @param volume sound volume [0.0-1.0]
/// @param pitch sound pitch multiplier [0.0-...] /// @param pitch sound pitch multiplier [0.0-...]
/// @param loop loop sound /// @param loop loop sound
@ -328,15 +343,17 @@ namespace audio {
extern speakerid_t play( extern speakerid_t play(
Sound* sound, Sound* sound,
glm::vec3 position, glm::vec3 position,
bool relative,
float volume, float volume,
float pitch, float pitch,
bool loop, bool loop,
int priority int priority
); );
/// @brief Play stream in the world /// @brief Play stream
/// @param stream target stream /// @param stream target stream
/// @param position stream world position /// @param position stream world position
/// @param relative position speaker relative to listener
/// @param volume stream volume [0.0-1.0] /// @param volume stream volume [0.0-1.0]
/// @param pitch stream pitch multiplier [0.0-...] /// @param pitch stream pitch multiplier [0.0-...]
/// @param loop loop stream /// @param loop loop stream
@ -344,6 +361,25 @@ namespace audio {
extern speakerid_t play( extern speakerid_t play(
std::shared_ptr<Stream> stream, std::shared_ptr<Stream> stream,
glm::vec3 position, 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 volume,
float pitch, float pitch,
bool loop bool loop

View File

@ -17,7 +17,7 @@ static inline const char* vorbis_error_message(int code) {
case OV_EVERSION: return "vorbis version mismatch"; case OV_EVERSION: return "vorbis version mismatch";
case OV_EBADHEADER: return "invalid Vorbis bitstream header"; case OV_EBADHEADER: return "invalid Vorbis bitstream header";
case OV_EFAULT: return "internal logic fault"; 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: default:
return "unknown"; return "unknown";
} }