Skip to content

Commit

Permalink
Move SoundProps from Sound asset to AudioSource
Browse files Browse the repository at this point in the history
  • Loading branch information
KredeGC committed Sep 4, 2024
1 parent 997723c commit 29d82bf
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 74 deletions.
18 changes: 1 addition & 17 deletions Mahakam/src/Mahakam/Asset/SoundAssetImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,14 @@ namespace Mahakam
{
Sound* sound = static_cast<Sound*>(asset);

const SoundProps& props = sound->GetProps();

node["Filepath"] << sound->GetFilepath();
node["Volume"] << props.Volume;
node["Loop"] << props.Loop;
}

Asset<void> SoundAssetImporter::Deserialize(ryml::NodeRef& node)
{
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());
}
}
3 changes: 0 additions & 3 deletions Mahakam/src/Mahakam/Asset/SoundAssetImporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,5 @@ namespace Mahakam

virtual void Serialize(ryml::NodeRef& node, void* asset) override;
virtual Asset<void> Deserialize(ryml::NodeRef& node) override;

private:
SoundProps DeserializeProps(ryml::NodeRef& node);
};
}
11 changes: 11 additions & 0 deletions Mahakam/src/Mahakam/Audio/AudioSource.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include "SoundProps.h"

#include "Mahakam/Core/Core.h"

#include "Mahakam/Asset/Asset.h"
Expand Down Expand Up @@ -28,6 +30,15 @@ namespace Mahakam
virtual void SetDataSource(Scope<AudioDataSource> 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;

Expand Down
11 changes: 3 additions & 8 deletions Mahakam/src/Mahakam/Audio/Sound.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once

#include "AudioEngine.h"
#include "SoundProps.h"

#include "Mahakam/Asset/Asset.h"

Expand All @@ -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<Sound> Create(const std::string& filepath, const SoundProps& props = {}) { return CreateImpl(filepath, props, AudioEngine::GetContext()); }
inline static Asset<Sound> Create(const std::string& filepath, AudioContext* context, const SoundProps& props = {}) { return CreateImpl(filepath, props, context); }
inline static Asset<Sound> Create(const std::string& filepath) { return CreateImpl(filepath, AudioEngine::GetContext()); }
inline static Asset<Sound> Create(const std::string& filepath, AudioContext* context) { return CreateImpl(filepath, context); }

private:
MH_DECLARE_FUNC(CreateImpl, Asset<Sound>, const std::string& filepath, const SoundProps& props, AudioContext* context);
MH_DECLARE_FUNC(CreateImpl, Asset<Sound>, const std::string& filepath, AudioContext* context);
};
}
25 changes: 2 additions & 23 deletions Mahakam/src/Mahakam/Editor/Resource/SoundResourceImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,54 +12,33 @@
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<void> SoundResourceImporter::CreateAsset(ryml::NodeRef& node)
{
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());
}
}
4 changes: 0 additions & 4 deletions Mahakam/src/Mahakam/Editor/Resource/SoundResourceImporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ namespace Mahakam
{
private:
std::filesystem::path m_Filepath;
SoundProps m_Props;

public:
SoundResourceImporter();
Expand All @@ -21,8 +20,5 @@ namespace Mahakam
virtual void OnImport(ryml::NodeRef& node) override;

virtual Asset<void> CreateAsset(ryml::NodeRef& node) override;

private:
SoundProps DeserializeProps(ryml::NodeRef& node);
};
}
8 changes: 8 additions & 0 deletions Mahakam/src/Mahakam/Scene/ComponentRegistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 0 additions & 2 deletions Mahakam/src/Platform/SteamAudio/MiniAudioDataSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
13 changes: 4 additions & 9 deletions Mahakam/src/Platform/SteamAudio/MiniAudioSound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
namespace Mahakam
{
//Asset<Sound> Sound::Create(const std::string& filepath, AudioContext* context)
MH_DEFINE_FUNC(Sound::CreateImpl, Asset<Sound>, const std::string& filepath, const SoundProps& props, AudioContext* context)
MH_DEFINE_FUNC(Sound::CreateImpl, Asset<Sound>, const std::string& filepath, AudioContext* context)
{
return CreateAsset<MiniAudioSound>(filepath, props, static_cast<MiniAudioContext*>(context));
return CreateAsset<MiniAudioSound>(filepath, static_cast<MiniAudioContext*>(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)
Expand All @@ -32,9 +32,4 @@ namespace Mahakam
// // Reached the end.
//}
}

void MiniAudioSound::SetProps(const SoundProps& props)
{
m_Props = props;
}
}
7 changes: 1 addition & 6 deletions Mahakam/src/Platform/SteamAudio/MiniAudioSound.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
}
28 changes: 26 additions & 2 deletions Mahakam/src/Platform/SteamAudio/MiniAudioSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ namespace Mahakam
return CreateScope<MiniAudioSource>(static_cast<MiniAudioContext*>(context));
};

MiniAudioSource::MiniAudioSource(MiniAudioContext* context)
: m_Context(context)
MiniAudioSource::MiniAudioSource(MiniAudioContext* context) :
m_Context(context),
m_Props()
{
ma_steamaudio_binaural_node_config binauralNodeConfig;

Expand Down Expand Up @@ -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;
Expand Down
11 changes: 11 additions & 0 deletions Mahakam/src/Platform/SteamAudio/MiniAudioSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ namespace Mahakam
// Need to keep a reference to data source
Scope<AudioDataSource> m_DataSource;

SoundProps m_Props;

glm::vec4 m_Source{ 0 };

public:
Expand All @@ -36,6 +38,15 @@ namespace Mahakam
virtual void SetDataSource(Scope<AudioDataSource> 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; }

Expand Down

0 comments on commit 29d82bf

Please sign in to comment.