diff --git a/kraken/CMakeLists.txt b/kraken/CMakeLists.txt index ddce0ac..45938bd 100644 --- a/kraken/CMakeLists.txt +++ b/kraken/CMakeLists.txt @@ -21,6 +21,7 @@ add_source_and_header(resources/animation_curve/KRAnimationCurve) add_source_and_header(resources/animation_curve/KRAnimationCurveManager) add_source_and_header(resources/audio/KRAudioManager) add_source_and_header(resources/audio/KRAudioSample) +add_source_and_header(resources/audio/KRAudioSampleBinding) add_source_and_header(resources/bundle/KRBundle) add_source_and_header(resources/bundle/KRBundleManager) add_source_and_header(resources/material/KRMaterial) diff --git a/kraken/nodes/KRAudioSource.cpp b/kraken/nodes/KRAudioSource.cpp index 0db226c..8262831 100755 --- a/kraken/nodes/KRAudioSource.cpp +++ b/kraken/nodes/KRAudioSource.cpp @@ -60,7 +60,6 @@ KRAudioSource::KRAudioSource(KRScene& scene, std::string name) : KRNode(scene, n m_playing = false; m_is3d = true; m_isPrimed = false; - m_audioFile = NULL; m_gain = 1.0f; m_pitch = 1.0f; m_looping = false; @@ -91,7 +90,7 @@ std::string KRAudioSource::getElementName() tinyxml2::XMLElement* KRAudioSource::saveXML(tinyxml2::XMLNode* parent) { tinyxml2::XMLElement* e = KRNode::saveXML(parent); - e->SetAttribute("sample", m_audio_sample_name.c_str()); + e->SetAttribute("sample", m_sample.getName().c_str()); e->SetAttribute("gain", m_gain); e->SetAttribute("pitch", m_pitch); e->SetAttribute("looping", m_looping ? "true" : "false"); @@ -106,7 +105,12 @@ tinyxml2::XMLElement* KRAudioSource::saveXML(tinyxml2::XMLNode* parent) void KRAudioSource::loadXML(tinyxml2::XMLElement* e) { - m_audio_sample_name = e->Attribute("sample"); + const char* szAudioSampleName = e->Attribute("sample"); + if (szAudioSampleName == nullptr) { + m_sample.clear(); + } else { + m_sample.setName(szAudioSampleName); + } float gain = 1.0f; if (e->QueryFloatAttribute("gain", &gain) != tinyxml2::XML_SUCCESS) { @@ -166,10 +170,8 @@ void KRAudioSource::loadXML(tinyxml2::XMLElement* e) void KRAudioSource::prime() { if (!m_isPrimed) { - if (m_audioFile == NULL && m_audio_sample_name.size() != 0) { - m_audioFile = getContext().getAudioManager()->get(m_audio_sample_name); - } - if (m_audioFile) { + m_sample.load(&getContext()); + if (m_sample.isLoaded()) { // Prime the buffer queue m_nextBufferIndex = 0; for (int i = 0; i < KRENGINE_AUDIO_BUFFERS_PER_SOURCE; i++) { @@ -183,9 +185,9 @@ void KRAudioSource::prime() void KRAudioSource::queueBuffer() { - KRAudioBuffer* buffer = m_audioFile->getBuffer(m_nextBufferIndex); + KRAudioBuffer* buffer = m_sample.get()->getBuffer(m_nextBufferIndex); m_audioBuffers.push(buffer); - m_nextBufferIndex = (m_nextBufferIndex + 1) % m_audioFile->getBufferCount(); + m_nextBufferIndex = (m_nextBufferIndex + 1) % m_sample.get()->getBufferCount(); } void KRAudioSource::render(RenderInfo& ri) @@ -365,20 +367,18 @@ bool KRAudioSource::isPlaying() void KRAudioSource::setSample(const std::string& sound_name) { - m_audio_sample_name = sound_name; + m_sample.setName(sound_name); } std::string KRAudioSource::getSample() { - return m_audio_sample_name; + return m_sample.getName(); } KRAudioSample* KRAudioSource::getAudioSample() { - if (m_audioFile == NULL && m_audio_sample_name.size() != 0) { - m_audioFile = getContext().getAudioManager()->get(m_audio_sample_name); - } - return m_audioFile; + m_sample.load(&getContext()); + return m_sample.get(); } void KRAudioSource::advanceFrames(int frame_count) diff --git a/kraken/nodes/KRAudioSource.h b/kraken/nodes/KRAudioSource.h index 5c13391..d3afd46 100755 --- a/kraken/nodes/KRAudioSource.h +++ b/kraken/nodes/KRAudioSource.h @@ -33,6 +33,7 @@ #include "KREngine-common.h" #include "resources/KRResource.h" +#include "resources/audio/KRAudioSampleBinding.h" #include "KRNode.h" class KRAudioSample; @@ -132,9 +133,7 @@ private: int m_currentBufferFrame; // Siren Audio Engine frame number within current buffer void advanceBuffer(); - std::string m_audio_sample_name; - - KRAudioSample* m_audioFile; + KRAudioSampleBinding m_sample; unsigned int m_sourceID; float m_gain; float m_pitch; diff --git a/kraken/resources/audio/KRAudioSampleBinding.cpp b/kraken/resources/audio/KRAudioSampleBinding.cpp new file mode 100644 index 0000000..ad1db38 --- /dev/null +++ b/kraken/resources/audio/KRAudioSampleBinding.cpp @@ -0,0 +1,56 @@ +// +// KRAudioSampleBinding.cpp +// Kraken Engine +// +// Copyright 2025 Kearwood Gilbert. All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other materials +// provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY KEARWOOD GILBERT ''AS IS'' AND ANY EXPRESS OR IMPLIED +// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KEARWOOD GILBERT OR +// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// The views and conclusions contained in the software and documentation are those of the +// authors and should not be interpreted as representing official policies, either expressed +// or implied, of Kearwood Gilbert. +// + +#include "KREngine-common.h" +#include "KRAudioSampleBinding.h" + +#include "KRContext.h" +#include "KRAudioSample.h" +#include "KRAudioManager.h" + + +KRAudioSample* KRAudioSampleBinding::get() +{ + return static_cast(m_resource); +} + +bool KRAudioSampleBinding::load(KRContext* context) +{ + if (m_name.size() == 0) { + return true; + } + if (m_resource != nullptr) { + return true; + } + m_resource = context->getAudioManager()->get(m_name); + + return (m_resource != nullptr); +} diff --git a/kraken/resources/audio/KRAudioSampleBinding.h b/kraken/resources/audio/KRAudioSampleBinding.h new file mode 100644 index 0000000..640ea9d --- /dev/null +++ b/kraken/resources/audio/KRAudioSampleBinding.h @@ -0,0 +1,46 @@ +// +// KRAudioSampleBinding.h +// Kraken Engine +// +// Copyright 2025 Kearwood Gilbert. All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other materials +// provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY KEARWOOD GILBERT ''AS IS'' AND ANY EXPRESS OR IMPLIED +// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KEARWOOD GILBERT OR +// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// The views and conclusions contained in the software and documentation are those of the +// authors and should not be interpreted as representing official policies, either expressed +// or implied, of Kearwood Gilbert. +// + +#pragma once + +#include "KREngine-common.h" +#include "resources/KRResourceBinding.h" + +class KRAudioSample; + +class KRAudioSampleBinding : public KRResourceBinding +{ +public: + KRAudioSample* get(); + + bool load(KRContext* context) override final; +private: +}; \ No newline at end of file