Implemented KRAudioSampleBinding, applied to KRAudioSource

This commit is contained in:
2025-11-11 15:18:19 -08:00
parent 1a602d30b1
commit 8c8aa1893c
5 changed files with 120 additions and 18 deletions

View File

@@ -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)

View File

@@ -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)

View File

@@ -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;

View File

@@ -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<KRAudioSample*>(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);
}

View File

@@ -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:
};