Skip to content

Commit

Permalink
Implement EffectNode::has_cycles()
Browse files Browse the repository at this point in the history
  • Loading branch information
SirBob01 committed May 17, 2023
1 parent 056a2d5 commit bf9e1fa
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 60 deletions.
34 changes: 18 additions & 16 deletions src/Sound/EffectNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,35 @@

namespace Dynamo::Sound {
b8 EffectNode::has_cycles() {
// TODO: Implement
}

const std::vector<EffectConnection> &EffectNode::get_outgoing() const {
return _outgoing;
std::unordered_set<EffectNode *> visited;
std::vector<std::reference_wrapper<EffectNode>> stack;
while (!stack.empty()) {
EffectNode &node = stack.back();
stack.pop_back();
if (visited.count(&node) > 0) {
return true;
}
for (EffectNode &child : node._outgoing) {
stack.push_back(child);
}
}
return false;
}

void EffectNode::connect(EffectNode &destination,
u32 input_channel,
u32 output_channel) {
_outgoing.push_back({destination, input_channel, output_channel});
void EffectNode::connect(EffectNode &destination) {
_outgoing.push_back(destination);
if (has_cycles()) {
Log::error("Jukebox EffectNode graph disallows cycles.");
}
}

void EffectNode::disconnect() { _outgoing.clear(); }

void EffectNode::disconnect(EffectNode &destination,
u32 input_channel,
u32 output_channel) {
void EffectNode::disconnect(EffectNode &destination) {
auto it = _outgoing.begin();
while (it != _outgoing.end()) {
EffectConnection &edge = *it;
if (&edge.outnode.get() == &destination &&
edge.input_channel == input_channel &&
edge.output_channel == output_channel) {
EffectNode &node = *it;
if (&node == &destination) {
it = _outgoing.erase(it);
return;
} else {
Expand Down
52 changes: 8 additions & 44 deletions src/Sound/EffectNode.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <functional>
#include <unordered_set>
#include <vector>

#include "../Types.hpp"
Expand All @@ -14,36 +15,12 @@ namespace Dynamo::Sound {
*/
class EffectNode;

/**
* @brief Connection with an outgoing EffectNode.
*
*/
struct EffectConnection {
/**
* @brief Out node.
*
*/
std::reference_wrapper<EffectNode> outnode;

/**
* @brief Input channel index.
*
*/
u32 input_channel;

/**
* @brief Output channel index.
*
*/
u32 output_channel;
};

/**
* @brief Node in an audio processing graph.
*
*/
class EffectNode {
std::vector<EffectConnection> _outgoing;
std::vector<std::reference_wrapper<EffectNode>> _outgoing;

private:
/**
Expand All @@ -61,39 +38,26 @@ namespace Dynamo::Sound {
virtual ~EffectNode() = 0;

/**
* @brief Get the outgoing connections.
* @brief Connect an outgoing EffectNode.
*
* @return const std::vector<EffectConnection>&
*/
const std::vector<EffectConnection> &get_outgoing() const;

/**
* @brief Connect another EffectNode.
* The resulting graph must not result in a cycle.
*
* @param destination
* @param input_channel
* @param output_channel
*/
void connect(EffectNode &destination,
u32 input_channel = 0,
u32 output_channel = 0);
void connect(EffectNode &destination);

/**
* @brief Disconnect all outgoing edges.
* @brief Disconnect all outgoing nodes.
*
*/
void disconnect();

/**
* @brief Disconnect an outgoing edge.
* @brief Disconnect an outgoing node.
*
* @param destination
* @param input_channel
* @param output_channel
*/
void disconnect(EffectNode &destination,
u32 input_channel = 0,
u32 output_channel = 0);
void disconnect(EffectNode &destination);

/**
* @brief Run the Sound through the graph rooted at this node.
Expand Down

0 comments on commit bf9e1fa

Please sign in to comment.