diff --git a/.gitmodules b/.gitmodules index 89f9e604cb..32abdc437d 100644 --- a/.gitmodules +++ b/.gitmodules @@ -4,9 +4,6 @@ [submodule "core/lib/glm"] path = core/lib/glm url = https://github.com/g-truc/glm.git -[submodule "core/lib/openal-soft"] - path = core/lib/openal-soft - url = https://github.com/kcat/openal-soft [submodule "core/lib/stduuid"] path = core/lib/stduuid url = https://github.com/mariusbancila/stduuid.git diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index 4150145166..436724e797 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt @@ -8,7 +8,6 @@ if(WITH_GLFW) option(GLFW_USE_SUBMODULE "Compile GLFW from source?" ON) endif() -option(WITH_OPENAL "With OpenAL?" OFF) option(GLM_USE_SUBMODULE "Compile GLM from source?" ON) set(CUBOS_CORE_ECS_MAX_COMPONENTS "63" CACHE STRING "The maximum number of components registered in an ECS world.") @@ -85,8 +84,8 @@ set(CUBOS_CORE_SOURCE "src/gl/util.cpp" "src/al/audio_device.cpp" - "src/al/oal_audio_device.cpp" - "src/al/oal_audio_device.hpp" + "src/al/miniaudio_device.cpp" + "src/al/miniaudio_device.hpp" "src/ecs/entity/entity.cpp" "src/ecs/entity/hash.cpp" @@ -191,16 +190,6 @@ if(WITH_GLFW) target_compile_definitions(cubos-core PRIVATE WITH_GLFW) endif() -if(WITH_OPENAL) - set(ALSOFT_UTILS OFF CACHE BOOL "" FORCE) - set(ALSOFT_NO_CONFIG_UTIL OFF CACHE BOOL "" FORCE) - set(ALSOFT_EXAMPLES OFF CACHE BOOL "" FORCE) - add_subdirectory(lib/openal-soft) - target_include_directories(cubos-core PRIVATE lib/openal-soft/include) - target_link_libraries(cubos-core PRIVATE OpenAL) - target_compile_definitions(cubos-core PRIVATE WITH_OPENAL) -endif() - if(GLM_USE_SUBMODULE) add_subdirectory(lib/glm SYSTEM) else() diff --git a/core/include/cubos/core/al/audio_device.hpp b/core/include/cubos/core/al/audio_device.hpp index 894498a5c8..74da64d7c2 100644 --- a/core/include/cubos/core/al/audio_device.hpp +++ b/core/include/cubos/core/al/audio_device.hpp @@ -9,6 +9,7 @@ #include #include +#include #include @@ -32,15 +33,6 @@ namespace cubos::core::al /// @ingroup core-al using Source = std::shared_ptr; - /// @brief Possible audio formats. - enum class Format - { - Mono8, - Mono16, - Stereo8, - Stereo16, - }; - /// @brief Audio device interface used to wrap low-level audio rendering APIs. class CUBOS_CORE_API AudioDevice { @@ -51,19 +43,19 @@ namespace cubos::core::al /// @brief Forbid copy construction. AudioDevice(const AudioDevice&) = delete; - /// @brief Creates an audio device from a given device @p specifier. + /// @brief Creates an audio device. /// @see enumerateDevices() - /// @param specifier Device specifier (empty for default). /// @return Audio device, or nullptr on failure. - static std::shared_ptr create(const std::string& specifier = ""); + static std::shared_ptr create(); /// @brief Enumerates the available devices. /// @param[out] devices Vector to fill with the available devices. static void enumerateDevices(std::vector& devices); /// @brief Creates a new audio buffer + /// @param filePath File path to create buffer from. /// @return Handle of the new buffer. - virtual Buffer createBuffer() = 0; + virtual Buffer createBuffer(const void* data, size_t dataSize) = 0; /// @brief Creates a new audio source. /// @return Handle of the new source. @@ -71,16 +63,20 @@ namespace cubos::core::al /// @brief Sets the position of the listener. /// @param position Position. - virtual void setListenerPosition(const glm::vec3& position) = 0; + /// @param listenerIndex Index of the listener + virtual void setListenerPosition(const glm::vec3& position, unsigned int listenerIndex = 0) = 0; /// @brief Sets the orientation of the listener. /// @param forward Forward direction of the listener. /// @param up Up direction of the listener. - virtual void setListenerOrientation(const glm::vec3& forward, const glm::vec3& up) = 0; + /// @param listenerIndex Index of the listener + virtual void setListenerOrientation(const glm::vec3& forward, const glm::vec3& up, + unsigned int listenerIndex = 0) = 0; /// @brief Sets the velocity of the listener. Used to implement the doppler effect. /// @param velocity Velocity of the listener. - virtual void setListenerVelocity(const glm::vec3& velocity) = 0; + /// @param listenerIndex Index of the listener + virtual void setListenerVelocity(const glm::vec3& velocity, unsigned int listenerIndex = 0) = 0; }; /// @brief Namespace to store the abstract types implemented by the audio device implementations. @@ -92,13 +88,6 @@ namespace cubos::core::al public: virtual ~Buffer() = default; - /// @brief Fills the buffer with data. - /// @param format Audio format of the data. - /// @param size Size of the buffer in bytes. - /// @param data Buffer data. - /// @param frequency Audio frequency. - virtual void fill(Format format, std::size_t size, const void* data, std::size_t frequency) = 0; - protected: Buffer() = default; }; @@ -141,26 +130,22 @@ namespace cubos::core::al /// @brief Sets the maximum distance at which the source is audible. /// @param maxDistance Maximum distance. - virtual void setDistance(float maxDistance) = 0; + virtual void setMaxDistance(float maxDistance) = 0; - /// @brief Sets the cone angle of the source, in degrees. By default, 360. - /// @param coneAngle Angle, in degrees. - virtual void setConeAngle(float coneAngle) = 0; + /// @brief Sets the minimum distance at which the source starts to attenuate. + /// @param minDistance Minimum distance. + virtual void setMinDistance(float minDistance) = 0; - /// @brief Sets the cone gain of the source. - /// @todo Find out what this is. + /// @brief Sets the cone angle, in degrees. While also setting the outerGain. + /// @param innerAngle Outer angle, in degrees. + /// @param outerAngle Inner angle, in degrees. /// @param coneGain Gain. - virtual void setConeGain(float coneGain) = 0; + virtual void setCone(float innerAngle, float outerAngle, float outerGain) = 0; /// @brief Sets the cone direction of the source. /// @param direction Direction. virtual void setConeDirection(const glm::vec3& direction) = 0; - /// @brief Sets the distance under which the volume for the source would normally drop - /// by half. - /// @param referenceDistance Distance. - virtual void setReferenceDistance(float referenceDistance) = 0; - /// @brief Plays the source. virtual void play() = 0; diff --git a/core/lib/openal-soft b/core/lib/openal-soft deleted file mode 160000 index d3875f333f..0000000000 --- a/core/lib/openal-soft +++ /dev/null @@ -1 +0,0 @@ -Subproject commit d3875f333fb6abe2f39d82caca329414871ae53b diff --git a/core/src/al/audio_device.cpp b/core/src/al/audio_device.cpp index df78baa603..b77a7fd166 100644 --- a/core/src/al/audio_device.cpp +++ b/core/src/al/audio_device.cpp @@ -1,13 +1,13 @@ -#include "oal_audio_device.hpp" +#include "miniaudio_device.hpp" using namespace cubos::core::al; -std::shared_ptr AudioDevice::create(const std::string& specifier) +std::shared_ptr AudioDevice::create() { - return std::make_shared(specifier); + return std::make_shared(); } void AudioDevice::enumerateDevices(std::vector& devices) { - OALAudioDevice::enumerateDevices(devices); + MiniaudioDevice::enumerateDevices(devices); } diff --git a/core/src/al/miniaudio_device.cpp b/core/src/al/miniaudio_device.cpp new file mode 100644 index 0000000000..dcfdc82534 --- /dev/null +++ b/core/src/al/miniaudio_device.cpp @@ -0,0 +1,243 @@ +#define MINIAUDIO_IMPLEMENTATION +#include "miniaudio_device.hpp" + +#include +#include + +using namespace cubos::core::al; + +class MiniaudioBuffer : public impl::Buffer +{ +public: + std::string path; + + MiniaudioBuffer(const void* data, size_t dataSize) + { + if (ma_decoder_init_memory(data, dataSize, nullptr, &mDecoder) != MA_SUCCESS) + { + CUBOS_CRITICAL("Failed to initialize Decoder from data"); + abort(); + } + } + + ~MiniaudioBuffer() + { + ma_decoder_uninit(&mDecoder); + } + +private: + ma_decoder mDecoder; +}; + +class MiniaudioSource : public impl::Source +{ +public: + MiniaudioSource() + { + if (ma_engine_init(nullptr, &mEngine) != MA_SUCCESS) + { + CUBOS_CRITICAL("Failed to initialize miniaudio engine."); + abort(); + } + } + + ~MiniaudioSource() override + { + ma_sound_uninit(&mSound); + ma_engine_uninit(&mEngine); + } + + void setBuffer(cubos::core::al::Buffer buffer) override + { + auto miniaudioBuffer = std::dynamic_pointer_cast(buffer); + if (ma_sound_init_from_file(&mEngine, miniaudioBuffer->path.c_str(), MA_SOUND_FLAG_STREAM, nullptr, nullptr, + &mSound) != MA_SUCCESS) + { + CUBOS_CRITICAL("Failed while initating sound from buffer file."); + abort(); + } + } + + void setPosition(const glm::vec3& position) override + { + ma_sound_set_position(&mSound, position.x, position.y, position.z); + } + + void setVelocity(const glm::vec3& velocity) override + { + ma_sound_set_velocity(&mSound, velocity.x, velocity.y, velocity.z); + } + + void setGain(float gain) override + { + ma_sound_set_volume(&mSound, gain); + } + + void setPitch(float pitch) override + { + ma_sound_set_pitch(&mSound, pitch); + } + + void setLooping(bool looping) override + { + ma_sound_set_looping(&mSound, static_cast(looping)); + } + + void setRelative(bool relative) override + { + relative ? ma_sound_set_positioning(&mSound, ma_positioning_relative) + : ma_sound_set_positioning(&mSound, ma_positioning_absolute); + } + + void setMaxDistance(float maxDistance) override + { + ma_sound_set_max_distance(&mSound, maxDistance); + } + + void setMinDistance(float minDistance) override + { + ma_sound_set_min_distance(&mSound, minDistance); + } + + void setCone(float innerAngle, float outerAngle, float outerGain = 1.0F) override + { + ma_sound_set_cone(&mSound, innerAngle, outerAngle, outerGain); + } + + void setConeDirection(const glm::vec3& direction) override + { + ma_sound_set_direction(&mSound, direction.x, direction.y, direction.z); + } + + void play() override + { + if (ma_sound_start(&mSound) != MA_SUCCESS) + { + CUBOS_CRITICAL("Failed to start sound."); + abort(); + } + } + +private: + ma_sound mSound; + ma_engine mEngine; +}; + +MiniaudioDevice::MiniaudioDevice() +{ + // Initialize miniaudio context. + if (ma_context_init(nullptr, 0, nullptr, &mContext) != MA_SUCCESS) + { + CUBOS_CRITICAL("Failed to initialize miniaudio context."); + abort(); + } + + // Initialize miniaudio engine + if (ma_engine_init(nullptr, &mEngine) != MA_SUCCESS) + { + CUBOS_CRITICAL("Failed to initialize miniaudio engine."); + abort(); + } + + // Configure the device. + ma_device_config deviceConfig = ma_device_config_init(ma_device_type_playback); + deviceConfig.playback.format = ma_format_f32; // Set to ma_format_unknown to use the device's native format. + deviceConfig.playback.channels = 2; // Set to 0 to use the device's native channel count. + deviceConfig.sampleRate = 48000; // Set to 0 to use the device's native sample rate. + + // Initialize the audio device. + if (ma_device_init(&mContext, &deviceConfig, &mDevice) != MA_SUCCESS) + { + CUBOS_CRITICAL("Failed to initialize audio device."); + ma_context_uninit(&mContext); + abort(); + } + + ma_device_start(&mDevice); +} + +MiniaudioDevice::~MiniaudioDevice() +{ + + ma_device_uninit(&mDevice); + ma_context_uninit(&mContext); +} + +void MiniaudioDevice::enumerateDevices(std::vector& devices) +{ + ma_context context; + if (ma_context_init(nullptr, 0, nullptr, &context) != MA_SUCCESS) + { + CUBOS_CRITICAL("Failed to initialize audio context."); + abort(); + } + + ma_device_info* pPlaybackDeviceInfos; + ma_uint32 playbackDeviceCount; + if (ma_context_get_devices(&context, &pPlaybackDeviceInfos, &playbackDeviceCount, nullptr, nullptr) != MA_SUCCESS) + { + CUBOS_CRITICAL("Failed to enumerate devices."); + ma_context_uninit(&context); // Uninitialize context before aborting + abort(); + } + + for (ma_uint32 i = 0; i < playbackDeviceCount; i++) + { + devices.emplace_back(pPlaybackDeviceInfos[i].name); + } + + ma_context_uninit(&context); +} + +std::string MiniaudioDevice::getDefaultDevice() +{ + ma_context context; + if (ma_context_init(nullptr, 0, nullptr, &context) != MA_SUCCESS) + { + CUBOS_CRITICAL("Failed to initialize audio context."); + abort(); + } + + std::string defaultDeviceName; + ma_context_enumerate_devices( + &context, + [](ma_context*, ma_device_type deviceType, const ma_device_info* pDeviceInfo, void* pUserData) -> ma_bool32 { + auto* pDefaultDeviceName = static_cast(pUserData); + if (deviceType == ma_device_type_playback && pDeviceInfo->isDefault == MA_TRUE) + { + *pDefaultDeviceName = pDeviceInfo->name; // Set the default device name + return MA_FALSE; + } + return MA_TRUE; + }, + &defaultDeviceName); // Pass defaultDeviceName as pUserData + + ma_context_uninit(&context); + return defaultDeviceName; +} + +Buffer MiniaudioDevice::createBuffer(const void* data, size_t dataSize) +{ + return std::make_shared(data, dataSize); +} + +Source MiniaudioDevice::createSource() +{ + return std::make_shared(); +} + +void MiniaudioDevice::setListenerPosition(const glm::vec3& position, ma_uint32 listenerIndex) +{ + ma_engine_listener_set_position(&mEngine, listenerIndex, position.x, position.y, position.z); +} + +void MiniaudioDevice::setListenerOrientation(const glm::vec3& forward, const glm::vec3& up, ma_uint32 listenerIndex) +{ + ma_engine_listener_set_direction(&mEngine, listenerIndex, forward.x, forward.y, forward.z); + ma_engine_listener_set_world_up(&mEngine, listenerIndex, up.x, up.y, up.z); +} + +void MiniaudioDevice::setListenerVelocity(const glm::vec3& velocity, ma_uint32 listenerIndex) +{ + ma_engine_listener_set_velocity(&mEngine, listenerIndex, velocity.x, velocity.y, velocity.z); +} diff --git a/core/src/al/miniaudio_device.hpp b/core/src/al/miniaudio_device.hpp new file mode 100644 index 0000000000..afc90b5be5 --- /dev/null +++ b/core/src/al/miniaudio_device.hpp @@ -0,0 +1,31 @@ +#pragma once + +#include + +namespace cubos::core::al +{ + /// Audio device implementation using miniaudio. + class MiniaudioDevice : public AudioDevice + { + public: + MiniaudioDevice(); + ~MiniaudioDevice() override; + + /// Enumerates the available devices. + /// @param devices The vector to fill with the available devices. + static void enumerateDevices(std::vector& devices); + static std::string getDefaultDevice(); + + Buffer createBuffer(const void* data, size_t dataSize) override; + Source createSource() override; + void setListenerPosition(const glm::vec3& position, ma_uint32 listenerIndex = 0) override; + void setListenerOrientation(const glm::vec3& forward, const glm::vec3& up, + ma_uint32 listenerIndex = 0) override; + void setListenerVelocity(const glm::vec3& velocity, ma_uint32 listenerIndex = 0) override; + + private: + ma_context mContext; + ma_device mDevice; + ma_engine mEngine; + }; +} // namespace cubos::core::al diff --git a/core/src/al/oal_audio_device.cpp b/core/src/al/oal_audio_device.cpp deleted file mode 100644 index 82072af019..0000000000 --- a/core/src/al/oal_audio_device.cpp +++ /dev/null @@ -1,262 +0,0 @@ -#include "oal_audio_device.hpp" - -#include - -#ifdef WITH_OPENAL -#include -#include -#endif // WITH_OPENAL - -#include - -#define UNSUPPORTED() \ - do \ - { \ - CUBOS_CRITICAL("Unsupported when building without OpenAL"); \ - abort(); \ - } while (0) - -using namespace cubos::core::al; - -#ifdef WITH_OPENAL -class OALBuffer : public impl::Buffer -{ -public: - OALBuffer(ALuint id) - : id(id) - { - } - - ~OALBuffer() override - { - alDeleteBuffers(1, &this->id); - } - - void fill(Format format, std::size_t size, const void* data, std::size_t frequency) override - { - ALenum alFormat = 0; - - switch (format) - { - case Format::Mono8: - alFormat = AL_FORMAT_MONO8; - break; - - case Format::Mono16: - alFormat = AL_FORMAT_MONO16; - break; - - case Format::Stereo8: - alFormat = AL_FORMAT_STEREO8; - break; - - case Format::Stereo16: - alFormat = AL_FORMAT_STEREO16; - break; - } - - alBufferData(this->id, alFormat, data, static_cast(size), static_cast(frequency)); - } - - ALuint id; -}; - -class OALSource : public impl::Source -{ -public: - OALSource(ALuint id) - : id(id) - { - } - - ~OALSource() override - { - alDeleteSources(1, &this->id); - } - - void setBuffer(Buffer buffer) override - { - auto oalBuffer = std::dynamic_pointer_cast(buffer); - alSourcei(this->id, AL_BUFFER, static_cast(oalBuffer->id)); - } - - void setPosition(const glm::vec3& position) override - { - alSource3f(this->id, AL_POSITION, position.x, position.y, position.z); - } - - void setVelocity(const glm::vec3& velocity) override - { - alSource3f(this->id, AL_VELOCITY, velocity.x, velocity.y, velocity.z); - } - - void setGain(float gain) override - { - alSourcef(this->id, AL_GAIN, gain); - } - - void setPitch(float pitch) override - { - alSourcef(this->id, AL_PITCH, pitch); - } - - void setLooping(bool looping) override - { - alSourcei(this->id, AL_LOOPING, static_cast(looping)); - } - - void setRelative(bool relative) override - { - alSourcei(this->id, AL_SOURCE_RELATIVE, static_cast(relative)); - } - - void setDistance(float maxDistance) override - { - alSourcef(this->id, AL_MAX_DISTANCE, maxDistance); - } - - void setConeAngle(float coneAngle) override - { - alSourcef(this->id, AL_CONE_OUTER_ANGLE, coneAngle); - } - - void setConeGain(float coneGain) override - { - alSourcef(this->id, AL_CONE_INNER_ANGLE, coneGain); - } - - void setConeDirection(const glm::vec3& direction) override - { - alSource3f(this->id, AL_DIRECTION, direction.x, direction.y, direction.z); - } - - void setReferenceDistance(float referenceDistance) override - { - alSourcef(this->id, AL_REFERENCE_DISTANCE, referenceDistance); - } - - void play() override - { - alSourcePlay(this->id); - } - - ALuint id; -}; -#endif // WITH_OPENAL - -OALAudioDevice::OALAudioDevice(const std::string& specifier) -{ -#ifdef WITH_OPENAL - auto* device = alcOpenDevice(specifier.c_str()); - auto* context = alcCreateContext(device, nullptr); - alcMakeContextCurrent(context); -#else - (void)specifier; - UNSUPPORTED(); -#endif // WITH_OPENAL -} - -OALAudioDevice::~OALAudioDevice() -{ -#ifdef WITH_OPENAL - auto* context = alcGetCurrentContext(); - auto* device = alcGetContextsDevice(context); - alcMakeContextCurrent(nullptr); - alcDestroyContext(context); - alcCloseDevice(device); -#endif // WITH_OPENAL -} - -void OALAudioDevice::enumerateDevices(std::vector& devices) -{ -#ifdef WITH_OPENAL - if (alcIsExtensionPresent(nullptr, "ALC_ENUMERATION_EXT") == AL_FALSE) - { - CUBOS_CRITICAL("Missing extension ALC_ENUMERATION_EXT"); - abort(); - } - - const char* pointer = alcGetString(nullptr, ALC_DEVICE_SPECIFIER); - - while (*pointer != '\0') - { - std::string s = pointer; - devices.push_back(s); - pointer += s.size() + 1; - } -#else - (void)devices; - UNSUPPORTED(); -#endif // WITH_OPENAL -} - -std::string OALAudioDevice::getDefaultDevice() -{ -#ifdef WITH_OPENAL - if (alcIsExtensionPresent(nullptr, "ALC_ENUMERATION_EXT") == AL_FALSE) - { - CUBOS_CRITICAL("Missing extension ALC_ENUMERATION_EXT"); - abort(); - } - - return alcGetString(nullptr, ALC_DEFAULT_DEVICE_SPECIFIER); -#else - UNSUPPORTED(); -#endif // WITH_OPENAL -} - -Buffer OALAudioDevice::createBuffer() -{ -#ifdef WITH_OPENAL - ALuint id; - alGenBuffers(1, &id); - - return std::make_shared(id); -#else - UNSUPPORTED(); -#endif // WITH_OPENAL -} - -Source OALAudioDevice::createSource() -{ -#ifdef WITH_OPENAL - ALuint sources; - alGenSources(1, &sources); - - return std::make_shared(sources); -#else - UNSUPPORTED(); -#endif // WITH_OPENAL -} - -void OALAudioDevice::setListenerPosition(const glm::vec3& position) -{ -#ifdef WITH_OPENAL - alListener3f(AL_POSITION, position.x, position.y, position.z); -#else - (void)position; - UNSUPPORTED(); -#endif // WITH_OPENAL -} - -void OALAudioDevice::setListenerOrientation(const glm::vec3& forward, const glm::vec3& up) -{ -#ifdef WITH_OPENAL - float orientation[6] = {forward.x, forward.y, forward.z, up.x, up.y, up.z}; - alListenerfv(AL_ORIENTATION, orientation); -#else - (void)forward; - (void)up; - UNSUPPORTED(); -#endif // WITH_OPENAL -} - -void OALAudioDevice::setListenerVelocity(const glm::vec3& velocity) -{ -#ifdef WITH_OPENAL - alListener3f(AL_VELOCITY, velocity.x, velocity.y, velocity.z); -#else - (void)velocity; - UNSUPPORTED(); -#endif // WITH_OPENAL -} diff --git a/core/src/al/oal_audio_device.hpp b/core/src/al/oal_audio_device.hpp deleted file mode 100644 index edba976809..0000000000 --- a/core/src/al/oal_audio_device.hpp +++ /dev/null @@ -1,27 +0,0 @@ -#pragma once - -#include - -namespace cubos::core::al -{ - /// Audio device implementation using OpenAL. - /// @see AudioDevice. - class OALAudioDevice : public AudioDevice - { - public: - /// @param specifier Specifies the device to use (empty for default). - OALAudioDevice(const std::string& specifier = ""); - ~OALAudioDevice() override; - - /// Enumerates the available devices. - /// @param devices The vector to fill with the available devices. - static void enumerateDevices(std::vector& devices); - static std::string getDefaultDevice(); - - Buffer createBuffer() override; - Source createSource() override; - void setListenerPosition(const glm::vec3& position) override; - void setListenerOrientation(const glm::vec3& forward, const glm::vec3& up) override; - void setListenerVelocity(const glm::vec3& velocity) override; - }; -} // namespace cubos::core::al