Skip to content

Commit

Permalink
feat(amplimix): Clip and Clamp nodes.
Browse files Browse the repository at this point in the history
Signed-off-by: Axel Nana <[email protected]>
  • Loading branch information
na2axl committed Sep 17, 2024
1 parent b421fa0 commit 3c91f73
Show file tree
Hide file tree
Showing 8 changed files with 212 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,10 @@ set(SA_SOURCE
src/Mixer/Nodes/AmbisonicPanningNode.h
src/Mixer/Nodes/AttenuationNode.cpp
src/Mixer/Nodes/AttenuationNode.h
src/Mixer/Nodes/ClampNode.cpp
src/Mixer/Nodes/ClampNode.h
src/Mixer/Nodes/ClipNode.cpp
src/Mixer/Nodes/ClipNode.h
src/Mixer/Nodes/NearFieldEffectNode.cpp
src/Mixer/Nodes/NearFieldEffectNode.h
src/Mixer/Nodes/OcclusionNode.cpp
Expand Down
2 changes: 2 additions & 0 deletions src/Core/DefaultPlugins.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@
#include <Mixer/Nodes/AmbisonicMixerNode.h>
#include <Mixer/Nodes/AmbisonicPanningNode.h>
#include <Mixer/Nodes/AttenuationNode.h>
#include <Mixer/Nodes/ClampNode.h>
#include <Mixer/Nodes/ClipNode.h>
#include <Mixer/Nodes/NearFieldEffectNode.h>
#include <Mixer/Nodes/OcclusionNode.h>
#include <Mixer/Nodes/ReflectionsNode.h>
Expand Down
6 changes: 6 additions & 0 deletions src/Core/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ namespace SparkyStudios::Audio::Amplitude
static AmUniquePtr<MemoryPoolKind::Engine, AmbisonicMixerNode> sAmbisonicMixerNodePlugin = nullptr;
static AmUniquePtr<MemoryPoolKind::Engine, AmbisonicPanningNode> sAmbisonicPanningNodePlugin = nullptr;
static AmUniquePtr<MemoryPoolKind::Engine, AttenuationNode> sAttenuationNodePlugin = nullptr;
static AmUniquePtr<MemoryPoolKind::Engine, ClampNode> sClampNodePlugin = nullptr;
static AmUniquePtr<MemoryPoolKind::Engine, ClipNode> sClipNodePlugin = nullptr;
static AmUniquePtr<MemoryPoolKind::Engine, NearFieldEffectNode> sNearFieldEffectNodePlugin = nullptr;
static AmUniquePtr<MemoryPoolKind::Engine, OcclusionNode> sOcclusionNodePlugin = nullptr;
static AmUniquePtr<MemoryPoolKind::Engine, ReflectionsNode> sReflectionsNodePlugin = nullptr;
Expand Down Expand Up @@ -377,6 +379,8 @@ namespace SparkyStudios::Audio::Amplitude
sAmbisonicMixerNodePlugin.reset(ampoolnew(MemoryPoolKind::Engine, AmbisonicMixerNode));
sAmbisonicPanningNodePlugin.reset(ampoolnew(MemoryPoolKind::Engine, AmbisonicPanningNode));
sAttenuationNodePlugin.reset(ampoolnew(MemoryPoolKind::Engine, AttenuationNode));
sClampNodePlugin.reset(ampoolnew(MemoryPoolKind::Engine, ClampNode));
sClipNodePlugin.reset(ampoolnew(MemoryPoolKind::Engine, ClipNode));
sNearFieldEffectNodePlugin.reset(ampoolnew(MemoryPoolKind::Engine, NearFieldEffectNode));
sOcclusionNodePlugin.reset(ampoolnew(MemoryPoolKind::Engine, OcclusionNode));
sReflectionsNodePlugin.reset(ampoolnew(MemoryPoolKind::Engine, ReflectionsNode));
Expand Down Expand Up @@ -428,6 +432,8 @@ namespace SparkyStudios::Audio::Amplitude
sAmbisonicMixerNodePlugin.reset(nullptr);
sAmbisonicPanningNodePlugin.reset(nullptr);
sAttenuationNodePlugin.reset(nullptr);
sClampNodePlugin.reset(nullptr);
sClipNodePlugin.reset(nullptr);
sNearFieldEffectNodePlugin.reset(nullptr);
sOcclusionNodePlugin.reset(nullptr);
sReflectionsNodePlugin.reset(nullptr);
Expand Down
11 changes: 0 additions & 11 deletions src/Mixer/Node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,17 +257,6 @@ namespace SparkyStudios::Audio::Amplitude
return;

*_buffer = *output;

for (AmSize c = 0; c < _buffer->GetChannelCount(); c++)
{
const auto& inChannel = _buffer->GetChannel(c);
auto& outChannel = _buffer->GetChannel(c);

for (AmSize i = 0, l = _buffer->GetFrameCount(); i < l; i++)
outChannel[i] = inChannel[i] <= -1.65f ? -0.9862875f
: inChannel[i] >= 1.65f ? 0.9862875f
: 0.87f * inChannel[i] - 0.1f * inChannel[i] * inChannel[i] * inChannel[i];
}
}

void OutputNodeInstance::Reset()
Expand Down
47 changes: 47 additions & 0 deletions src/Mixer/Nodes/ClampNode.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) 2024-present Sparky Studios. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <Mixer/Nodes/ClampNode.h>

namespace SparkyStudios::Audio::Amplitude
{
ClampNodeInstance::ClampNodeInstance()
: ProcessorNodeInstance(false)
{}

const AudioBuffer* ClampNodeInstance::Process(const AudioBuffer* input)
{
_output = *input;

for (AmSize c = 0; c < _output.GetChannelCount(); c++)
{
const auto& inChannel = _output[c];
/* */ auto& outChannel = _output[c];

for (AmSize i = 0, l = _output.GetFrameCount(); i < l; i++)
{
const AmReal32& x = inChannel[i];
/* */ AmReal32& y = outChannel[i];

y = std::max(AM_AUDIO_SAMPLE_MIN, std::min(AM_AUDIO_SAMPLE_MAX, x));
}
}

return &_output;
}

ClampNode::ClampNode()
: Node("Clamp")
{}
} // namespace SparkyStudios::Audio::Amplitude
53 changes: 53 additions & 0 deletions src/Mixer/Nodes/ClampNode.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) 2024-present Sparky Studios. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#ifndef _AM_IMPLEMENTATION_MIXER_NODES_CLAMP_NODE_H
#define _AM_IMPLEMENTATION_MIXER_NODES_CLAMP_NODE_H

#include <SparkyStudios/Audio/Amplitude/Core/Memory.h>
#include <SparkyStudios/Audio/Amplitude/Mixer/Node.h>

namespace SparkyStudios::Audio::Amplitude
{
class ClampNodeInstance final : public ProcessorNodeInstance
{
public:
ClampNodeInstance();

const AudioBuffer* Process(const AudioBuffer* input) override;

private:
AudioBuffer _output;
};

class ClampNode final : public Node
{
public:
ClampNode();

[[nodiscard]] AM_INLINE NodeInstance* CreateInstance() const override
{
return ampoolnew(MemoryPoolKind::Amplimix, ClampNodeInstance);
}

void DestroyInstance(NodeInstance* instance) const override
{
ampooldelete(MemoryPoolKind::Amplimix, ClampNodeInstance, (ClampNodeInstance*)instance);
}
};
} // namespace SparkyStudios::Audio::Amplitude

#endif // _AM_IMPLEMENTATION_MIXER_NODES_CLAMP_NODE_H
47 changes: 47 additions & 0 deletions src/Mixer/Nodes/ClipNode.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) 2024-present Sparky Studios. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <Mixer/Nodes/ClipNode.h>

namespace SparkyStudios::Audio::Amplitude
{
ClipNodeInstance::ClipNodeInstance()
: ProcessorNodeInstance(false)
{}

const AudioBuffer* ClipNodeInstance::Process(const AudioBuffer* input)
{
_output = *input;

for (AmSize c = 0; c < _output.GetChannelCount(); c++)
{
const auto& inChannel = _output[c];
/* */ auto& outChannel = _output[c];

for (AmSize i = 0, l = _output.GetFrameCount(); i < l; i++)
{
const AmReal32& x = inChannel[i];
/* */ AmReal32& y = outChannel[i];

y = x <= -1.65f ? -0.9862875f : x >= 1.65f ? 0.9862875f : 0.87f * x - 0.1f * x * x * x;
}
}

return &_output;
}

ClipNode::ClipNode()
: Node("Clip")
{}
} // namespace SparkyStudios::Audio::Amplitude
53 changes: 53 additions & 0 deletions src/Mixer/Nodes/ClipNode.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) 2024-present Sparky Studios. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#ifndef _AM_IMPLEMENTATION_MIXER_NODES_CLIP_NODE_H
#define _AM_IMPLEMENTATION_MIXER_NODES_CLIP_NODE_H

#include <SparkyStudios/Audio/Amplitude/Core/Memory.h>
#include <SparkyStudios/Audio/Amplitude/Mixer/Node.h>

namespace SparkyStudios::Audio::Amplitude
{
class ClipNodeInstance final : public ProcessorNodeInstance
{
public:
ClipNodeInstance();

const AudioBuffer* Process(const AudioBuffer* input) override;

private:
AudioBuffer _output;
};

class ClipNode final : public Node
{
public:
ClipNode();

[[nodiscard]] AM_INLINE NodeInstance* CreateInstance() const override
{
return ampoolnew(MemoryPoolKind::Amplimix, ClipNodeInstance);
}

void DestroyInstance(NodeInstance* instance) const override
{
ampooldelete(MemoryPoolKind::Amplimix, ClipNodeInstance, (ClipNodeInstance*)instance);
}
};
} // namespace SparkyStudios::Audio::Amplitude

#endif // _AM_IMPLEMENTATION_MIXER_NODES_CLIP_NODE_H

0 comments on commit 3c91f73

Please sign in to comment.