Skip to content

Commit

Permalink
audio::ISound add 'PlayAtStart', 'Loop' and 'Background' flags
Browse files Browse the repository at this point in the history
  • Loading branch information
Benualdo committed Sep 15, 2024
1 parent 276edae commit ae8fc6f
Show file tree
Hide file tree
Showing 16 changed files with 269 additions and 53 deletions.
5 changes: 3 additions & 2 deletions data/Prefabs/Football.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,20 @@
</Object>
<Object class="SoundComponent">
<Property type="String" name="m_name" flags="NotVisible" value="New SoundComponent"/>
<Property type="Uint32" name="m_uid" flags="" value="0"/>
<Property type="Uint32" name="m_uid" flags="" value="540222898"/>
<Property type="Uint32" name="m_originalUID" flags="" value="0"/>
<Property type="EnumFlagsU32" name="m_objectFlags" flags="" value="Opened"/>
<Property type="EnumFlagsU32" name="m_flags" flags="Bitfield" value="Enabled"/>
<Property type="Resource" name="m_soundRes" flags="Resource">
<Object class="SoundResource">
<Property type="String" name="m_name" flags="NotVisible" value="New SoundComponent"/>
<Property type="Uint32" name="m_uid" flags="" value="0"/>
<Property type="Uint32" name="m_uid" flags="" value="4096398380"/>
<Property type="Uint32" name="m_originalUID" flags="" value="0"/>
<Property type="EnumFlagsU32" name="m_objectFlags" flags="" value=""/>
<Property type="String" name="m_resourcePath" flags="NotVisible" value="data/Sounds/Soccer/shoot.wav"/>
</Object>
</Property>
<Property type="EnumFlagsU32" name="m_soundFlags" flags="Bitfield" value=""/>
</Object>
<Object class="MeshComponent">
<Property type="String" name="m_name" flags="NotVisible" value="MeshComponent #1"/>
Expand Down
54 changes: 49 additions & 5 deletions data/Scenes/Aiguelongue.scene
Original file line number Diff line number Diff line change
Expand Up @@ -2077,7 +2077,7 @@
<Property type="String" name="m_name" value="Billboards"/>
<Property type="Uint32" name="m_uid" flags="" value="2532357560"/>
<Property type="Uint32" name="m_originalUID" flags="" value="0"/>
<Property type="EnumFlagsU32" name="m_objectFlags" flags="" value=""/>
<Property type="EnumFlagsU32" name="m_objectFlags" flags="" value="Opened"/>
<Property type="EnumFlagsU32" name="m_flags" flags="Bitfield" value="Enabled"/>
<Property type="Float4" name="m_color" flags="Color" x="1" y="1" z="1" w="1"/>
<Property type="Float4x4" name="m_local" flags="Flatten" Ix="1" Iy="0" Iz="0" Iw="0" Jx="0" Jy="1" Jz="0" Jw="0" Kx="0" Ky="0" Kz="1" Kw="0" Tx="0" Ty="0" Tz="0" Tw="1"/>
Expand All @@ -2087,7 +2087,7 @@
<Property type="String" name="m_name" value="Billboard"/>
<Property type="Uint32" name="m_uid" flags="" value="3869079134"/>
<Property type="Uint32" name="m_originalUID" flags="" value="0"/>
<Property type="EnumFlagsU32" name="m_objectFlags" flags="" value="Prefab"/>
<Property type="EnumFlagsU32" name="m_objectFlags" flags="" value="Opened|Prefab"/>
<Property type="EnumFlagsU32" name="m_flags" flags="Bitfield" value="Enabled"/>
<Property type="Float4" name="m_color" flags="Color" x="1" y="1" z="1" w="1"/>
<Property type="Float4x4" name="m_local" flags="Flatten" Ix="-0.00048832496" Iy="-0.99999988" Iz="0" Iw="0" Jx="0.99999988" Jy="-0.00048832496" Jz="0" Jw="0" Kx="0" Ky="0" Kz="1" Kw="0" Tx="7" Ty="-3.0597971e-07" Tz="0" Tw="1"/>
Expand Down Expand Up @@ -2683,11 +2683,53 @@
</Object>
</Property>
</Object>
<Object class="GameObject">
<Property type="String" name="m_name" value="Sound"/>
<Property type="Uint32" name="m_uid" flags="" value="2116459250"/>
<Property type="Uint32" name="m_originalUID" flags="" value="0"/>
<Property type="EnumFlagsU32" name="m_objectFlags" flags="" value="Opened"/>
<Property type="EnumFlagsU32" name="m_flags" flags="Bitfield" value="Enabled"/>
<Property type="Float4" name="m_color" flags="Color" x="1" y="1" z="1" w="1"/>
<Property type="Float4x4" name="m_local" flags="Flatten" Ix="1" Iy="0" Iz="0" Iw="0" Jx="0" Jy="1" Jz="0" Jw="0" Kx="0" Ky="0" Kz="1" Kw="0" Tx="0" Ty="0" Tz="0" Tw="1"/>
<Property type="ObjectPtrVector" name="m_components"/>
<Property type="ObjectPtrVector" name="m_children" flags="NotVisible">
<Object class="GameObject">
<Property type="String" name="m_name" value="BGM"/>
<Property type="Uint32" name="m_uid" flags="" value="1064878902"/>
<Property type="Uint32" name="m_originalUID" flags="" value="2463671653"/>
<Property type="EnumFlagsU32" name="m_objectFlags" flags="" value="Selected"/>
<Property type="EnumFlagsU32" name="m_flags" flags="Bitfield" value="Enabled"/>
<Property type="Float4" name="m_color" flags="Color" x="1" y="1" z="1" w="1"/>
<Property type="Float4x4" name="m_local" flags="Flatten" Ix="1" Iy="0" Iz="0" Iw="0" Jx="0" Jy="1" Jz="0" Jw="0" Kx="0" Ky="0" Kz="1" Kw="0" Tx="0" Ty="0" Tz="0" Tw="1"/>
<Property type="ObjectPtrVector" name="m_components">
<Object class="SoundComponent">
<Property type="String" name="m_name" flags="NotVisible" value="New SoundComponent"/>
<Property type="Uint32" name="m_uid" flags="" value="1740773095"/>
<Property type="Uint32" name="m_originalUID" flags="" value="2213048044"/>
<Property type="EnumFlagsU32" name="m_objectFlags" flags="" value="Opened"/>
<Property type="EnumFlagsU32" name="m_flags" flags="Bitfield" value="Enabled"/>
<Property type="Resource" name="m_soundRes" flags="Resource">
<Object class="SoundResource">
<Property type="String" name="m_name" flags="NotVisible" value=""/>
<Property type="Uint32" name="m_uid" flags="" value="2784075843"/>
<Property type="Uint32" name="m_originalUID" flags="" value="2816057046"/>
<Property type="EnumFlagsU32" name="m_objectFlags" flags="" value=""/>
<Property type="String" name="m_resourcePath" flags="NotVisible" value="data/Sounds/BGM/tally-dub-brotheration-records-2016-140287.mp3"/>
</Object>
</Property>
<Property type="EnumFlagsU32" name="m_soundSettings.m_flags" flags="Bitfield" value="PlayOnStart|Loop"/>
<Property type="Float" name="m_soundSettings.m_volume" flags="HasRange" value="0.2"/>
</Object>
</Property>
<Property type="ObjectPtrVector" name="m_children" flags="NotVisible"/>
</Object>
</Property>
</Object>
<Object class="GameObject">
<Property type="String" name="m_name" value="Debug"/>
<Property type="Uint32" name="m_uid" flags="" value="1949923508"/>
<Property type="Uint32" name="m_originalUID" flags="" value="0"/>
<Property type="EnumFlagsU32" name="m_objectFlags" flags="" value="Selected"/>
<Property type="EnumFlagsU32" name="m_objectFlags" flags="" value=""/>
<Property type="EnumFlagsU32" name="m_flags" flags="Bitfield" value=""/>
<Property type="Float4" name="m_color" flags="Color" x="1" y="1" z="1" w="1"/>
<Property type="Float4x4" name="m_local" flags="Flatten" Ix="1" Iy="0" Iz="0" Iw="0" Jx="0" Jy="1" Jz="0" Jw="0" Kx="0" Ky="0" Kz="1" Kw="0" Tx="0" Ty="0" Tz="0" Tw="1"/>
Expand Down Expand Up @@ -2718,7 +2760,7 @@
<Property type="String" name="m_name" value="TestSound"/>
<Property type="Uint32" name="m_uid" flags="" value="2463671653"/>
<Property type="Uint32" name="m_originalUID" flags="" value="0"/>
<Property type="EnumFlagsU32" name="m_objectFlags" flags="" value="Selected"/>
<Property type="EnumFlagsU32" name="m_objectFlags" flags="" value=""/>
<Property type="EnumFlagsU32" name="m_flags" flags="Bitfield" value="Enabled"/>
<Property type="Float4" name="m_color" flags="Color" x="1" y="1" z="1" w="1"/>
<Property type="Float4x4" name="m_local" flags="Flatten" Ix="1" Iy="0" Iz="0" Iw="0" Jx="0" Jy="1" Jz="0" Jw="0" Kx="0" Ky="0" Kz="1" Kw="0" Tx="0" Ty="0" Tz="0" Tw="1"/>
Expand All @@ -2732,12 +2774,14 @@
<Property type="Resource" name="m_soundRes" flags="Resource">
<Object class="SoundResource">
<Property type="String" name="m_name" flags="NotVisible" value=""/>
<Property type="Uint32" name="m_uid" flags="" value="0"/>
<Property type="Uint32" name="m_uid" flags="" value="2816057046"/>
<Property type="Uint32" name="m_originalUID" flags="" value="0"/>
<Property type="EnumFlagsU32" name="m_objectFlags" flags="" value=""/>
<Property type="String" name="m_resourcePath" flags="NotVisible" value=""/>
</Object>
</Property>
<Property type="EnumFlagsU32" name="m_soundSettings.m_flags" flags="Bitfield" value=""/>
<Property type="Float" name="m_soundSettings.m_volume" flags="HasRange" value="1"/>
</Object>
</Property>
<Property type="ObjectPtrVector" name="m_children" flags="NotVisible"/>
Expand Down
42 changes: 39 additions & 3 deletions src/audio/Audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,21 +121,33 @@ namespace vg::audio
}

//--------------------------------------------------------------------------------------
PlaySoundHandle Audio::PlaySound(const ISound * _sound)
PlaySoundHandle Audio::Play(const ISound * _sound, const SoundSettings & _settings)
{
if (_sound)
{
if (auto * slAudioSource = ((Sound *)_sound)->getSLAudioSource())
{
return (PlaySoundHandle)m_soloud.play(*slAudioSource);
PlaySoundHandle handle;

if (asBool(SoundFlags::Background & _settings.m_flags))
handle = (PlaySoundHandle)m_soloud.playBackground(*slAudioSource, _settings.m_volume);
else
handle = (PlaySoundHandle)m_soloud.play(*slAudioSource, _settings.m_volume);

if (asBool(SoundFlags::Loop & _settings.m_flags))
m_soloud.setLooping((SoLoud::handle)handle, true);
else
m_soloud.setLooping((SoLoud::handle)handle, false);

return handle;
}
}

return PlaySoundHandle(0);
}

//--------------------------------------------------------------------------------------
bool Audio::StopSound(const PlaySoundHandle & _handle)
bool Audio::Stop(const PlaySoundHandle & _handle)
{
if (0 != _handle)
{
Expand All @@ -145,4 +157,28 @@ namespace vg::audio

return false;
}

//--------------------------------------------------------------------------------------
bool Audio::SetVolume(const PlaySoundHandle & _handle, float _volume)
{
if (0 != _handle)
{
m_soloud.setVolume((SoLoud::handle)_handle, _volume);
return true;
}

return false;
}

//--------------------------------------------------------------------------------------
bool Audio::SetLooping(const PlaySoundHandle & _handle, bool _looping)
{
if (0 != _handle)
{
m_soloud.setLooping((SoLoud::handle)_handle, _looping);
return true;
}

return false;
}
}
6 changes: 4 additions & 2 deletions src/audio/Audio.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ namespace vg::audio
void Update (float _dt) final override;
IAudioOptions * GetOptions () const final override;
ISound * CreateSound (const core::string & _path) final override;
PlaySoundHandle PlaySound (const ISound * _sound) final override;
bool StopSound (const PlaySoundHandle & _handle) final override;
PlaySoundHandle Play (const ISound * _sound, const SoundSettings & _settings) final override;
bool Stop (const PlaySoundHandle & _handle) final override;
bool SetVolume (const PlaySoundHandle & _handle, float _volume) final override;
bool SetLooping (const PlaySoundHandle & _handle, bool _looping) final override;

VG_INLINE SoLoud::Soloud & getSLEngine ();

Expand Down
12 changes: 12 additions & 0 deletions src/audio/Audio_Consts.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,17 @@ namespace vg::audio
Flac
);

vg_enum_class(SoundFlags, core::u32,
PlayOnStart = 0x00000001,
Loop = 0x00000002,
Background = 0x80000000
);

struct SoundSettings
{
SoundFlags m_flags = (SoundFlags)0x0;
float m_volume = 1.0f;
};

using PlaySoundHandle = core::u64;
}
6 changes: 4 additions & 2 deletions src/audio/IAudio.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ namespace vg::audio
virtual void Update (float _dt) = 0;
virtual IAudioOptions * GetOptions () const = 0;
virtual ISound * CreateSound (const core::string & _path) = 0;
virtual PlaySoundHandle PlaySound (const ISound * _sound) = 0;
virtual bool StopSound (const PlaySoundHandle & _handle) = 0;
virtual PlaySoundHandle Play (const ISound * _sound, const SoundSettings & _settings) = 0;
virtual bool Stop (const PlaySoundHandle & _handle) = 0;
virtual bool SetVolume (const PlaySoundHandle & _handle, float _volume) = 0;
virtual bool SetLooping (const PlaySoundHandle & _handle, bool _looping) = 0;
};
}
4 changes: 4 additions & 0 deletions src/audio/ISound.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
#pragma once
#include "core/Model/Model.h"
#include "audio/Audio_Consts.h"

namespace vg::audio
{
class ISound : public core::Model
{
public:
VG_CLASS_DECL_ABSTRACT(ISound, core::Model);

//virtual void SetSoundFlags(SoundFlags _flags, bool _enabled) = 0;
//virtual SoundFlags GetSoundFlags() const = 0;
};
}
23 changes: 23 additions & 0 deletions src/audio/Sound/Sound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ namespace vg::audio
m_slAudioSource = new SoLoud::Wav();
VG_VERIFY(SoLoud::SO_NO_ERROR == ((SoLoud::Wav*)m_slAudioSource)->load(_name.c_str()));
break;

case AudioSourceType::Mp3:
m_slAudioSource = new SoLoud::Wav();
VG_VERIFY(SoLoud::SO_NO_ERROR == ((SoLoud::Wav *)m_slAudioSource)->load(_name.c_str()));
break;
}
}

Expand All @@ -44,6 +49,24 @@ namespace vg::audio
VG_SAFE_DELETE(m_slAudioSource);
}

////--------------------------------------------------------------------------------------
//void Sound::SetSoundFlags(SoundFlags _flags, bool _enabled)
//{
// if (_enabled)
// m_soundFlags |= _flags;
// else
// m_soundFlags &= ~_flags;
//
// if (asBool(SoundFlags::Loop & _flags))
// m_slAudioSource->setLooping(_enabled);
//}
//
////--------------------------------------------------------------------------------------
//SoundFlags Sound::GetSoundFlags() const
//{
// return m_soundFlags;
//}

//--------------------------------------------------------------------------------------
AudioSourceType Sound::getAudioSourceTypeFromFilename(const core::string & _filename)
{
Expand Down
5 changes: 4 additions & 1 deletion src/audio/Sound/Sound.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#pragma once
#include "audio/ISound.h"
#include "audio/Audio_Consts.h"

namespace vg::audio
{
Expand All @@ -12,12 +11,16 @@ namespace vg::audio
Sound(const core::string & _name, core::IObject * _parent);
~Sound();

//void SetSoundFlags(SoundFlags _flags, bool _enabled) final override;
//SoundFlags GetSoundFlags() const final override;

SoLoud::AudioSource * getSLAudioSource() const { return m_slAudioSource; }

protected:
static AudioSourceType getAudioSourceTypeFromFilename(const core::string & _filename);

private:
//SoundFlags m_soundFlags = (SoundFlags)0x0;
AudioSourceType m_audioSourceType = AudioSourceType::None;
SoLoud::AudioSource * m_slAudioSource = nullptr;
};
Expand Down
2 changes: 1 addition & 1 deletion src/core/Component/Component.inl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace vg::core
if (_enabled)
m_flags |= _flags;
else
(std::underlying_type<ComponentFlags>::type&)m_flags &= ~(std::underlying_type<ComponentFlags>::type&)_flags;
m_flags &= ~_flags;
}

//--------------------------------------------------------------------------------------
Expand Down
6 changes: 3 additions & 3 deletions src/editor/ImGui/Window/ImGuiWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2299,11 +2299,11 @@ namespace vg::editor
if (!strcmp(_resource->GetClassName(), "PrefabResource"))
anyVisibleProperty = false;

if (anyVisibleProperty)
//if (anyVisibleProperty)
{
const string resourceLabel = fmt::sprintf("%s###%s", _prop->GetDisplayName(), _resource->GetResourcePath());

if (ImGui::TreeNodeEx(resourceLabel.c_str(), ImGuiTreeNodeFlags_DefaultOpen))
//if (ImGui::TreeNodeEx(resourceLabel.c_str(), ImGuiTreeNodeFlags_DefaultOpen))
{
ImGuiWindow::displayObject(resourceObject);

Expand All @@ -2328,7 +2328,7 @@ namespace vg::editor

ImGui::Spacing();

ImGui::TreePop();
//ImGui::TreePop();
}
}
}
Expand Down
Loading

0 comments on commit ae8fc6f

Please sign in to comment.