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);
}
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;
}

View File

@ -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;
};

View File

@ -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> 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> 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()) {

View File

@ -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> 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

View File

@ -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";
}