Skip to content

Commit

Permalink
Implement AudioNode
Browse files Browse the repository at this point in the history
  • Loading branch information
SirBob01 committed May 15, 2023
1 parent ba9a2f5 commit 17cd2c5
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/Sound/AudioGraph/AudioNode.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "./AudioNode.hpp"
#include <cstdio>

namespace Dynamo::Sound {
const std::vector<AudioConnection> &AudioNode::get_output_edges() const {
return _output_edges;
}

void AudioNode::connect(AudioNode &destination,
u32 input_channel,
u32 output_channel) {
_output_edges.push_back({destination, input_channel, output_channel});
}

void AudioNode::disconnect() { _output_edges.clear(); }

void AudioNode::disconnect(AudioNode &destination,
u32 input_channel,
u32 output_channel) {
auto it = _output_edges.begin();
while (it != _output_edges.end()) {
AudioConnection &edge = *it;
if (&edge.destination.get() == &destination &&
edge.input_channel == input_channel &&
edge.output_channel == output_channel) {
it = _output_edges.erase(it);
return;
} else {
it++;
}
}
Log::error("AudioNode - Invalid disconnection.");
}
} // namespace Dynamo::Sound
103 changes: 103 additions & 0 deletions src/Sound/AudioGraph/AudioNode.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#pragma once

#include <functional>
#include <vector>

#include "../../Types.hpp"
#include "../Sound.hpp"

namespace Dynamo::Sound {
/**
* @brief Forward declare the AudioNode class.
*
*/
class AudioNode;

/**
* @brief Connection between two AudioNodes.
*
*/
struct AudioConnection {
/**
* @brief Destination node.
*
*/
std::reference_wrapper<AudioNode> destination;

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

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

/**
* @brief Node in an audio processing graph.
*
*/
class AudioNode {
std::vector<AudioConnection> _output_edges;

public:
/**
* @brief Destroy the AudioNode object.
*
*/
virtual ~AudioNode() = 0;

/**
* @brief Get the output edges object.
*
* @return const std::vector<AudioConnection>&
*/
const std::vector<AudioConnection> &get_output_edges() const;

/**
* @brief Connect another AudioNode.
*
* @param destination
* @param input_channel
* @param output_channel
*/
void connect(AudioNode &destination,
u32 input_channel = 0,
u32 output_channel = 0);

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

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

/**
* @brief Process a single channel and write it to the output
* channel.
*
* @param src
* @param src_length
* @param dst
* @param dst_length
*/
virtual void process(WaveSample *src,
u32 src_length,
WaveSample *dst,
u32 dst_length) = 0;
};
} // namespace Dynamo::Sound

0 comments on commit 17cd2c5

Please sign in to comment.