diff --git a/Mahakam/src/Mahakam/Asset/SoundAssetImporter.cpp b/Mahakam/src/Mahakam/Asset/SoundAssetImporter.cpp index 8449d041..236d93c9 100644 --- a/Mahakam/src/Mahakam/Asset/SoundAssetImporter.cpp +++ b/Mahakam/src/Mahakam/Asset/SoundAssetImporter.cpp @@ -18,11 +18,7 @@ namespace Mahakam { Sound* sound = static_cast(asset); - const SoundProps& props = sound->GetProps(); - node["Filepath"] << sound->GetFilepath(); - node["Volume"] << props.Volume; - node["Loop"] << props.Loop; } Asset SoundAssetImporter::Deserialize(ryml::NodeRef& node) @@ -30,18 +26,6 @@ namespace Mahakam std::filesystem::path filepath; DeserializeYAMLNode(node, "Filepath", filepath); - SoundProps props = DeserializeProps(node); - - return Sound::Create(filepath.string(), props); - } - - SoundProps SoundAssetImporter::DeserializeProps(ryml::NodeRef& node) - { - SoundProps props; - - DeserializeYAMLNode(node, "Volume", props.Volume); - DeserializeYAMLNode(node, "Loop", props.Loop); - - return props; + return Sound::Create(filepath.string()); } } \ No newline at end of file diff --git a/Mahakam/src/Mahakam/Asset/SoundAssetImporter.h b/Mahakam/src/Mahakam/Asset/SoundAssetImporter.h index 1554e147..3c7add40 100644 --- a/Mahakam/src/Mahakam/Asset/SoundAssetImporter.h +++ b/Mahakam/src/Mahakam/Asset/SoundAssetImporter.h @@ -26,8 +26,5 @@ namespace Mahakam virtual void Serialize(ryml::NodeRef& node, void* asset) override; virtual Asset Deserialize(ryml::NodeRef& node) override; - - private: - SoundProps DeserializeProps(ryml::NodeRef& node); }; } \ No newline at end of file diff --git a/Mahakam/src/Mahakam/Audio/AudioSource.h b/Mahakam/src/Mahakam/Audio/AudioSource.h index 1b934b9b..3b00b4fc 100644 --- a/Mahakam/src/Mahakam/Audio/AudioSource.h +++ b/Mahakam/src/Mahakam/Audio/AudioSource.h @@ -1,5 +1,7 @@ #pragma once +#include "SoundProps.h" + #include "Mahakam/Core/Core.h" #include "Mahakam/Asset/Asset.h" @@ -28,6 +30,15 @@ namespace Mahakam virtual void SetDataSource(Scope dataSource) = 0; virtual AudioDataSource* GetDataSource() const = 0; + virtual const SoundProps& GetProps() const = 0; + virtual void SetProps(const SoundProps& props) = 0; + + virtual void SetVolume(float volume) = 0; + virtual float GetVolume() const = 0; + + virtual void SetLooping(bool loop) = 0; + virtual bool GetLooping() const = 0; + virtual void SetInterpolation(bool interpolate) = 0; virtual bool GetInterpolation() const = 0; diff --git a/Mahakam/src/Mahakam/Audio/Sound.h b/Mahakam/src/Mahakam/Audio/Sound.h index 63947938..a5da8962 100644 --- a/Mahakam/src/Mahakam/Audio/Sound.h +++ b/Mahakam/src/Mahakam/Audio/Sound.h @@ -1,7 +1,6 @@ #pragma once #include "AudioEngine.h" -#include "SoundProps.h" #include "Mahakam/Asset/Asset.h" @@ -12,14 +11,10 @@ namespace Mahakam public: virtual const std::string& GetFilepath() const = 0; - virtual const SoundProps& GetProps() const = 0; - - virtual void SetProps(const SoundProps& props) = 0; - - inline static Asset Create(const std::string& filepath, const SoundProps& props = {}) { return CreateImpl(filepath, props, AudioEngine::GetContext()); } - inline static Asset Create(const std::string& filepath, AudioContext* context, const SoundProps& props = {}) { return CreateImpl(filepath, props, context); } + inline static Asset Create(const std::string& filepath) { return CreateImpl(filepath, AudioEngine::GetContext()); } + inline static Asset Create(const std::string& filepath, AudioContext* context) { return CreateImpl(filepath, context); } private: - MH_DECLARE_FUNC(CreateImpl, Asset, const std::string& filepath, const SoundProps& props, AudioContext* context); + MH_DECLARE_FUNC(CreateImpl, Asset, const std::string& filepath, AudioContext* context); }; } \ No newline at end of file diff --git a/Mahakam/src/Mahakam/Editor/Resource/SoundResourceImporter.cpp b/Mahakam/src/Mahakam/Editor/Resource/SoundResourceImporter.cpp index 6aecd3fe..494353fa 100644 --- a/Mahakam/src/Mahakam/Editor/Resource/SoundResourceImporter.cpp +++ b/Mahakam/src/Mahakam/Editor/Resource/SoundResourceImporter.cpp @@ -12,35 +12,26 @@ namespace Mahakam { SoundResourceImporter::SoundResourceImporter() : - ResourceImporter("Sound", ".sound", "sound"), - m_Props() {} + ResourceImporter("Sound", ".sound", "sound") {} void SoundResourceImporter::OnResourceOpen(const std::filesystem::path& filepath) { m_Filepath = filepath; - m_Props = SoundProps(); } void SoundResourceImporter::OnImportOpen(ryml::NodeRef& node) { DeserializeYAMLNode(node, "Filepath", m_Filepath); - - m_Props = DeserializeProps(node); } void SoundResourceImporter::OnRender() { - ImGui::DragFloat("Sound Volume", &m_Props.Volume, 0.01f, 0.0f); - ImGui::Checkbox("Sound Looping", &m_Props.Loop); - GUI::DrawDragDropField("File path", m_ImporterProps.Extension, m_Filepath); } void SoundResourceImporter::OnImport(ryml::NodeRef& node) { node["Filepath"] << m_Filepath; - node["Volume"] << m_Props.Volume; - node["Loop"] << m_Props.Loop; } Asset SoundResourceImporter::CreateAsset(ryml::NodeRef& node) @@ -48,18 +39,6 @@ namespace Mahakam std::filesystem::path filepath; DeserializeYAMLNode(node, "Filepath", filepath); - SoundProps props = DeserializeProps(node); - - return Sound::Create(filepath.string(), props); - } - - SoundProps SoundResourceImporter::DeserializeProps(ryml::NodeRef& node) - { - SoundProps props; - - DeserializeYAMLNode(node, "Volume", props.Volume); - DeserializeYAMLNode(node, "Loop", props.Loop); - - return props; + return Sound::Create(filepath.string()); } } \ No newline at end of file diff --git a/Mahakam/src/Mahakam/Editor/Resource/SoundResourceImporter.h b/Mahakam/src/Mahakam/Editor/Resource/SoundResourceImporter.h index 902fd2cf..9857efca 100644 --- a/Mahakam/src/Mahakam/Editor/Resource/SoundResourceImporter.h +++ b/Mahakam/src/Mahakam/Editor/Resource/SoundResourceImporter.h @@ -10,7 +10,6 @@ namespace Mahakam { private: std::filesystem::path m_Filepath; - SoundProps m_Props; public: SoundResourceImporter(); @@ -21,8 +20,5 @@ namespace Mahakam virtual void OnImport(ryml::NodeRef& node) override; virtual Asset CreateAsset(ryml::NodeRef& node) override; - - private: - SoundProps DeserializeProps(ryml::NodeRef& node); }; } \ No newline at end of file diff --git a/Mahakam/src/Mahakam/Scene/ComponentRegistry.cpp b/Mahakam/src/Mahakam/Scene/ComponentRegistry.cpp index 0eacd5ff..fc41b1f5 100644 --- a/Mahakam/src/Mahakam/Scene/ComponentRegistry.cpp +++ b/Mahakam/src/Mahakam/Scene/ComponentRegistry.cpp @@ -192,6 +192,14 @@ namespace Mahakam source.Play(); // TODO: TEMPORARY, REMOVE WHEN PLAY MODE IS IMPL } + float volume = source.GetVolume(); + if (ImGui::DragFloat("Volume", &volume, 0.01f, 0.0f, 1.0f)) + source.SetVolume(volume); + + bool looping = source.GetLooping(); + if (ImGui::Checkbox("Loop", &looping)) + source.SetLooping(looping); + float spatialBlend = source.GetSpatialBlend(); if (ImGui::DragFloat("Spatial blend", &spatialBlend, 0.01f, 0.0f, 1.0f)) source.SetSpatialBlend(spatialBlend); diff --git a/Mahakam/src/Platform/SteamAudio/MiniAudioDataSource.cpp b/Mahakam/src/Platform/SteamAudio/MiniAudioDataSource.cpp index 73cecd62..d7898c0c 100644 --- a/Mahakam/src/Platform/SteamAudio/MiniAudioDataSource.cpp +++ b/Mahakam/src/Platform/SteamAudio/MiniAudioDataSource.cpp @@ -20,8 +20,6 @@ namespace Mahakam ma_result result = ma_resource_manager_data_source_init(resourceManager, filepath.c_str(), 0, NULL, &m_DataSource); MH_ASSERT(result == MA_SUCCESS, "Failed to initialize data source"); - - ma_data_source_set_looping(&m_DataSource, m_Sound->GetProps().Loop); } MiniAudioDataSource::~MiniAudioDataSource() diff --git a/Mahakam/src/Platform/SteamAudio/MiniAudioSound.cpp b/Mahakam/src/Platform/SteamAudio/MiniAudioSound.cpp index 431ddea1..bd144b05 100644 --- a/Mahakam/src/Platform/SteamAudio/MiniAudioSound.cpp +++ b/Mahakam/src/Platform/SteamAudio/MiniAudioSound.cpp @@ -6,13 +6,13 @@ namespace Mahakam { //Asset Sound::Create(const std::string& filepath, AudioContext* context) - MH_DEFINE_FUNC(Sound::CreateImpl, Asset, const std::string& filepath, const SoundProps& props, AudioContext* context) + MH_DEFINE_FUNC(Sound::CreateImpl, Asset, const std::string& filepath, AudioContext* context) { - return CreateAsset(filepath, props, static_cast(context)); + return CreateAsset(filepath, static_cast(context)); }; - MiniAudioSound::MiniAudioSound(const std::string& filepath, const SoundProps& props, MiniAudioContext* context) - : m_Filepath(filepath), m_Props(props) + MiniAudioSound::MiniAudioSound(const std::string& filepath, MiniAudioContext* context) + : m_Filepath(filepath) { // TODO: Use stb_vorbis as internal format? // TODO: Read file as binary (wav, mp3) @@ -32,9 +32,4 @@ namespace Mahakam // // Reached the end. //} } - - void MiniAudioSound::SetProps(const SoundProps& props) - { - m_Props = props; - } } \ No newline at end of file diff --git a/Mahakam/src/Platform/SteamAudio/MiniAudioSound.h b/Mahakam/src/Platform/SteamAudio/MiniAudioSound.h index 745446c7..93f6b1fc 100644 --- a/Mahakam/src/Platform/SteamAudio/MiniAudioSound.h +++ b/Mahakam/src/Platform/SteamAudio/MiniAudioSound.h @@ -9,16 +9,11 @@ namespace Mahakam { private: std::string m_Filepath; - SoundProps m_Props; public: - MiniAudioSound(const std::string& filepath, const SoundProps& props, MiniAudioContext* context); + MiniAudioSound(const std::string& filepath, MiniAudioContext* context); ~MiniAudioSound() = default; virtual const std::string& GetFilepath() const override { return m_Filepath; } - - virtual const SoundProps& GetProps() const override { return m_Props; } - - virtual void SetProps(const SoundProps& props) override; }; } \ No newline at end of file diff --git a/Mahakam/src/Platform/SteamAudio/MiniAudioSource.cpp b/Mahakam/src/Platform/SteamAudio/MiniAudioSource.cpp index 9bfa4caf..f356b2be 100644 --- a/Mahakam/src/Platform/SteamAudio/MiniAudioSource.cpp +++ b/Mahakam/src/Platform/SteamAudio/MiniAudioSource.cpp @@ -11,8 +11,9 @@ namespace Mahakam return CreateScope(static_cast(context)); }; - MiniAudioSource::MiniAudioSource(MiniAudioContext* context) - : m_Context(context) + MiniAudioSource::MiniAudioSource(MiniAudioContext* context) : + m_Context(context), + m_Props() { ma_steamaudio_binaural_node_config binauralNodeConfig; @@ -74,6 +75,29 @@ namespace Mahakam } } + void MiniAudioSource::SetProps(const SoundProps& props) + { + // Update volume, if it has changed + if (props.Volume != m_Props.Volume) + SetVolume(props.Volume); + + // Update looping, if it has changed + if (props.Loop != m_Props.Loop) + SetLooping(props.Loop); + } + + void MiniAudioSource::SetVolume(float volume) + { + m_Props.Volume = volume; + ma_sound_set_volume(&m_MaSound, volume); + } + + void MiniAudioSource::SetLooping(bool loop) + { + m_Props.Loop = loop; + ma_sound_set_looping(&m_MaSound, loop ? MA_TRUE : MA_FALSE); + } + void MiniAudioSource::SetInterpolation(bool interpolate) { m_Node.interpolate = interpolate; diff --git a/Mahakam/src/Platform/SteamAudio/MiniAudioSource.h b/Mahakam/src/Platform/SteamAudio/MiniAudioSource.h index 3fdefd57..1f2fab3e 100644 --- a/Mahakam/src/Platform/SteamAudio/MiniAudioSource.h +++ b/Mahakam/src/Platform/SteamAudio/MiniAudioSource.h @@ -24,6 +24,8 @@ namespace Mahakam // Need to keep a reference to data source Scope m_DataSource; + SoundProps m_Props; + glm::vec4 m_Source{ 0 }; public: @@ -36,6 +38,15 @@ namespace Mahakam virtual void SetDataSource(Scope dataSource) override; virtual AudioDataSource* GetDataSource() const override { return m_DataSource.get(); } + virtual const SoundProps& GetProps() const override { return m_Props; } + virtual void SetProps(const SoundProps& props) override; + + virtual void SetVolume(float volume) override; + virtual float GetVolume() const override { return m_Props.Volume; } + + virtual void SetLooping(bool loop) override; + virtual bool GetLooping() const override { return m_Props.Loop; } + virtual void SetInterpolation(bool interpolate) override; virtual bool GetInterpolation() const override { return m_Node.interpolate; }