diff --git a/Editor/src/ComponentsPanel/AudioEmitterPanel.h b/Editor/src/ComponentsPanel/AudioEmitterPanel.h index 912107b8..8c15f795 100644 --- a/Editor/src/ComponentsPanel/AudioEmitterPanel.h +++ b/Editor/src/ComponentsPanel/AudioEmitterPanel.h @@ -60,7 +60,7 @@ public: ImGui::Text("Volume"); ImGui::TableNextColumn(); - ImGui::DragFloat("##Volume", &component.Volume, 0.00f, 1.0f); + ImGui::DragFloat("##Volume", &component.Volume, 0.001f, 0.0f, 2.0f); ImGui::TableNextColumn(); ComponentTableReset(component.Volume, 1.0f); diff --git a/Editor/src/Windows/EditorInterface.cpp b/Editor/src/Windows/EditorInterface.cpp index 76d4a158..838f1318 100644 --- a/Editor/src/Windows/EditorInterface.cpp +++ b/Editor/src/Windows/EditorInterface.cpp @@ -119,13 +119,6 @@ namespace Nuake { ImGui::SameLine(); - if (ImGui::Button(ICON_FA_MUSIC, ImVec2(30, 30))) - { - Nuake::AudioManager::Get().QueueWavAudio("jump.wav"); - } - - ImGui::SameLine(); - ImGui::Dummy(ImVec2(ImGui::GetContentRegionAvail().x - 120, 30)); ImGui::SameLine(); diff --git a/Editor/src/Windows/FileSystemUI.cpp b/Editor/src/Windows/FileSystemUI.cpp index 7c2fb4a1..b0053cb7 100644 --- a/Editor/src/Windows/FileSystemUI.cpp +++ b/Editor/src/Windows/FileSystemUI.cpp @@ -248,6 +248,10 @@ namespace Nuake { dragType = "_Image"; } + else if (fileExtension == ".wav" || fileExtension == ".ogg") + { + dragType = "_AudioFile"; + } ImGui::SetDragDropPayload(dragType.c_str(), (void*)(pathBuffer), sizeof(pathBuffer)); ImGui::Text(file->GetName().c_str()); diff --git a/Nuake/src/Audio/AudioManager.cpp b/Nuake/src/Audio/AudioManager.cpp index 6702315e..d031f5ef 100644 --- a/Nuake/src/Audio/AudioManager.cpp +++ b/Nuake/src/Audio/AudioManager.cpp @@ -45,19 +45,19 @@ namespace Nuake { } - void AudioManager::QueueWavAudio(const std::string& filePath) + void AudioManager::QueueWavAudio(const AudioRequest& request) { // Acquire mutex lock and push to queue const std::lock_guard lock(m_AudioQueueMutex); // Check if file exists and load - const bool fileExists = FileSystem::FileExists(filePath, true); - if (fileExists && !IsWavLoaded(filePath)) + const bool fileExists = FileSystem::FileExists(request.audioFile, true); + if (fileExists && !IsWavLoaded(request.audioFile)) { - LoadWavAudio(filePath); + LoadWavAudio(request.audioFile); } - m_AudioQueue.push({ filePath }); + m_AudioQueue.push(request); } bool AudioManager::IsWavLoaded(const std::string& filePath) const @@ -81,10 +81,12 @@ namespace Nuake { // Check if we have audio queued while (!m_AudioQueue.empty()) { - auto& currentAudio = m_AudioQueue.front(); + AudioRequest& currentAudio = m_AudioQueue.front(); - // If file exists, play it - m_Soloud->play(m_WavSamples[currentAudio.audioFile]); + SoLoud::handle soloudHandle = m_Soloud->play(m_WavSamples[currentAudio.audioFile]); + m_Soloud->setVolume(soloudHandle, currentAudio.volume); + m_Soloud->setPan(soloudHandle, currentAudio.pan); + m_Soloud->setRelativePlaySpeed(soloudHandle, currentAudio.speed); // Remove item from queue. m_AudioQueue.pop(); diff --git a/Nuake/src/Audio/AudioManager.h b/Nuake/src/Audio/AudioManager.h index d2f0fc16..a4bd0488 100644 --- a/Nuake/src/Audio/AudioManager.h +++ b/Nuake/src/Audio/AudioManager.h @@ -11,9 +11,13 @@ namespace SoLoud } // Temp code -struct audioQueueRequest +struct AudioRequest { std::string audioFile; + float volume = 1.0f; + float pan = 0.0f; + float speed = 1.0f; + }; namespace SoLoud @@ -35,7 +39,7 @@ namespace Nuake std::thread m_AudioThread; std::mutex m_AudioQueueMutex; std::atomic m_AudioQueued = { false }; - std::queue m_AudioQueue; + std::queue m_AudioQueue; std::unordered_map m_WavSamples; public: @@ -57,7 +61,7 @@ namespace Nuake void PlayTTS(const std::string& text); - void QueueWavAudio(const std::string& filePath); + void QueueWavAudio(const AudioRequest& request); bool IsWavLoaded(const std::string& filePath) const; void LoadWavAudio(const std::string& filePath); private: diff --git a/Nuake/src/Scene/Components/AudioEmitterComponent.cpp b/Nuake/src/Scene/Components/AudioEmitterComponent.cpp new file mode 100644 index 00000000..17550e8b --- /dev/null +++ b/Nuake/src/Scene/Components/AudioEmitterComponent.cpp @@ -0,0 +1,22 @@ +#include "AudioEmitterComponent.h" + +namespace Nuake { + json AudioEmitterComponent::Serialize() + { + BEGIN_SERIALIZE(); + SERIALIZE_VAL(FilePath); + SERIALIZE_VAL(Volume); + SERIALIZE_VAL(Pan); + SERIALIZE_VAL(PlaybackSpeed); + END_SERIALIZE(); + } + + bool AudioEmitterComponent::Deserialize(const json& j) + { + DESERIALIZE_VAL(FilePath); + DESERIALIZE_VAL(Volume); + DESERIALIZE_VAL(Pan); + DESERIALIZE_VAL(PlaybackSpeed); + return true; + } +} \ No newline at end of file diff --git a/Nuake/src/Scene/Entities/Entity.cpp b/Nuake/src/Scene/Entities/Entity.cpp index 3b6f7c25..cc74c9fa 100644 --- a/Nuake/src/Scene/Entities/Entity.cpp +++ b/Nuake/src/Scene/Entities/Entity.cpp @@ -75,36 +75,39 @@ namespace Nuake SERIALIZE_OBJECT_REF_LBL("SkinnedModelComponent", GetComponent()) if (HasComponent()) SERIALIZE_OBJECT_REF_LBL("BoneComponent", GetComponent()) + if (HasComponent()) + SERIALIZE_OBJECT_REF_LBL("AudioEmitterComponent", GetComponent()) END_SERIALIZE(); } bool Entity::Deserialize(const json& j) { - DESERIALIZE_COMPONENT(TransformComponent) + DESERIALIZE_COMPONENT(TransformComponent); DESERIALIZE_COMPONENT(VisibilityComponent) else { AddComponent(); } - DESERIALIZE_COMPONENT(NameComponent) - DESERIALIZE_COMPONENT(ParentComponent) - DESERIALIZE_COMPONENT(ParticleEmitterComponent) - DESERIALIZE_COMPONENT(SpriteComponent) - DESERIALIZE_COMPONENT(CameraComponent) - DESERIALIZE_COMPONENT(QuakeMapComponent) - DESERIALIZE_COMPONENT(LightComponent) - DESERIALIZE_COMPONENT(ModelComponent) - DESERIALIZE_COMPONENT(WrenScriptComponent) - DESERIALIZE_COMPONENT(CharacterControllerComponent) - DESERIALIZE_COMPONENT(BoxColliderComponent) - DESERIALIZE_COMPONENT(CapsuleColliderComponent) - DESERIALIZE_COMPONENT(CylinderColliderComponent) - DESERIALIZE_COMPONENT(MeshColliderComponent) - DESERIALIZE_COMPONENT(SphereColliderComponent) - DESERIALIZE_COMPONENT(RigidBodyComponent) - DESERIALIZE_COMPONENT(BSPBrushComponent) - DESERIALIZE_COMPONENT(BoneComponent) - DESERIALIZE_COMPONENT(SkinnedModelComponent) + DESERIALIZE_COMPONENT(NameComponent); + DESERIALIZE_COMPONENT(ParentComponent); + DESERIALIZE_COMPONENT(ParticleEmitterComponent); + DESERIALIZE_COMPONENT(SpriteComponent); + DESERIALIZE_COMPONENT(CameraComponent); + DESERIALIZE_COMPONENT(QuakeMapComponent); + DESERIALIZE_COMPONENT(LightComponent); + DESERIALIZE_COMPONENT(ModelComponent); + DESERIALIZE_COMPONENT(WrenScriptComponent); + DESERIALIZE_COMPONENT(CharacterControllerComponent); + DESERIALIZE_COMPONENT(BoxColliderComponent); + DESERIALIZE_COMPONENT(CapsuleColliderComponent); + DESERIALIZE_COMPONENT(CylinderColliderComponent); + DESERIALIZE_COMPONENT(MeshColliderComponent); + DESERIALIZE_COMPONENT(SphereColliderComponent); + DESERIALIZE_COMPONENT(RigidBodyComponent); + DESERIALIZE_COMPONENT(BSPBrushComponent); + DESERIALIZE_COMPONENT(BoneComponent); + DESERIALIZE_COMPONENT(SkinnedModelComponent); + DESERIALIZE_COMPONENT(AudioEmitterComponent); return true; } diff --git a/Nuake/src/Scene/Scene.cpp b/Nuake/src/Scene/Scene.cpp index 6fcf724d..b3e2985d 100644 --- a/Nuake/src/Scene/Scene.cpp +++ b/Nuake/src/Scene/Scene.cpp @@ -1,19 +1,22 @@ #pragma once +#include "Scene.h" +#include "Entities/Entity.h" + +#include "src/Core/Core.h" +#include "src/Core/OS.h" +#include "src/Core/Physics/PhysicsManager.h" + #include "src/Scene/Systems/ScriptingSystem.h" #include "src/Scene/Systems/PhysicsSystem.h" #include "src/Scene/Systems/TransformSystem.h" #include "src/Scene/Systems/QuakeMapBuilder.h" #include "src/Scene/Systems/ParticleSystem.h" #include "src/Scene/Systems/AnimationSystem.h" +#include #include "src/Rendering/SceneRenderer.h" -#include "Scene.h" -#include "Entities/Entity.h" - #include "src/Rendering/Renderer.h" #include "src/Rendering/Textures/MaterialManager.h" -#include "src/Core/Physics/PhysicsManager.h" -#include "src/Core/Core.h" #include @@ -26,13 +29,13 @@ #include "src/Scene/Components/BSPBrushComponent.h" #include "src/Scene/Components/InterfaceComponent.h" #include "src/Scene/Components/SkinnedModelComponent.h" +#include "src/Scene/Components/BoneComponent.h" #include #include #include #include -#include "src/Core/OS.h" -#include "Components/BoneComponent.h" + namespace Nuake { @@ -53,6 +56,7 @@ namespace Nuake m_Systems.push_back(CreateRef(this)); m_Systems.push_back(CreateRef(this)); m_Systems.push_back(CreateRef(this)); + m_Systems.push_back(CreateRef(this)); m_SceneRenderer = new SceneRenderer(); m_SceneRenderer->Init(); diff --git a/Nuake/src/Scene/Systems/AudioSystem.cpp b/Nuake/src/Scene/Systems/AudioSystem.cpp new file mode 100644 index 00000000..7c821b61 --- /dev/null +++ b/Nuake/src/Scene/Systems/AudioSystem.cpp @@ -0,0 +1,66 @@ +#include "AudioSystem.h" + +#include "src/Scene/Scene.h" +#include "src/Scene/Entities/Entity.h" +#include "src/Scene/Components/AudioEmitterComponent.h" + +#include "src/Audio/AudioManager.h" + +#include + + +namespace Nuake +{ + AudioSystem::AudioSystem(Scene* scene) + { + m_Scene = scene; + } + + bool AudioSystem::Init() + { + return true; + } + + void AudioSystem::Update(Timestep ts) + { + auto view = m_Scene->m_Registry.view(); + for (auto& e : view) + { + auto [transformComponent, audioEmitterComponent] = view.get(e); + + if (audioEmitterComponent.FilePath.empty()) + { + continue; + } + + if (audioEmitterComponent.IsPlaying) + { + const std::string absoluteFilePath = FileSystem::RelativeToAbsolute(audioEmitterComponent.FilePath); + + AudioRequest audioRequest; + audioRequest.audioFile = absoluteFilePath; + audioRequest.pan = audioEmitterComponent.Pan; + audioRequest.volume = audioEmitterComponent.Volume; + audioRequest.speed = audioEmitterComponent.PlaybackSpeed; + + AudioManager::Get().QueueWavAudio(std::move(audioRequest)); + audioEmitterComponent.IsPlaying = false; + } + } + } + + void AudioSystem::FixedUpdate(Timestep ts) + { + + } + + void AudioSystem::EditorUpdate() + { + + } + + void AudioSystem::Exit() + { + + } +} diff --git a/Nuake/src/Scene/Systems/AudioSystem.h b/Nuake/src/Scene/Systems/AudioSystem.h new file mode 100644 index 00000000..ba7d74e7 --- /dev/null +++ b/Nuake/src/Scene/Systems/AudioSystem.h @@ -0,0 +1,21 @@ +#pragma once +#include +#include + +namespace Nuake +{ + class AudioSystem : public System + { + private: + std::unordered_map m_StatusCache; + + public: + AudioSystem(Scene* scene); + bool Init() override; + void Update(Timestep ts) override; + void Draw() override {} + void EditorUpdate() override; + void FixedUpdate(Timestep ts) override; + void Exit() override; + }; +}