Skip to content

Commit

Permalink
bugfix and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandreRouma committed May 16, 2022
1 parent 34eca2d commit 89f08ab
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
16 changes: 8 additions & 8 deletions src/dsp/chain.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace dsp {
void addBlock(Processor<T, T>* block, bool enabled) {
// Check if block is already part of the chain
if (blockExists(block)) {
throw std::runtime_error("Tried to add a block that is already part of the chain");
throw std::runtime_error("[chain] Tried to add a block that is already part of the chain");
}

// Add to the list
Expand All @@ -47,7 +47,7 @@ namespace dsp {
void removeBlock(Processor<T, T>* block Func onOutputChange) {
// Check if block is part of the chain
if (!blockExists(block)) {
throw std::runtime_error("Tried to remove a block that is not part of the chain");
throw std::runtime_error("[chain] Tried to remove a block that is not part of the chain");
}

// Disable the block
Expand All @@ -62,7 +62,7 @@ namespace dsp {
void enableBlock(Processor<T, T>* block, Func onOutputChange) {
// Check that the block is part of the chain
if (!blockExists(block)) {
throw std::runtime_error("Tried to enable a block that isn't part of the chain");
throw std::runtime_error("[chain] Tried to enable a block that isn't part of the chain");
}

// If already enable, don't do anything
Expand Down Expand Up @@ -93,12 +93,16 @@ namespace dsp {
void disableBlock(Processor<T, T>* block, Func onOutputChange) {
// Check that the block is part of the chain
if (!blockExists(block)) {
throw std::runtime_error("Tried to enable a block that isn't part of the chain");
throw std::runtime_error("[chain] Tried to enable a block that isn't part of the chain");
}

// If already disabled, don't do anything
if (!states[block]) { return; }

// Stop disabled block
block->stop();
states[block] = false;

// Gather blocks before and after the block to disable
Processor<T, T>* before = blockBefore(block);
Processor<T, T>* after = blockAfter(block);
Expand All @@ -111,10 +115,6 @@ namespace dsp {
out = before ? &before->out : _in;
onOutputChange(out);
}

// Stop disabled block
block->stop();
states[block] = false;
}

void start() {
Expand Down
35 changes: 33 additions & 2 deletions src/dsp/routing/splitter.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,42 @@ namespace dsp::routing {

Splitter(stream<T>* in) { base_type::init(in); }

void bindStream(stream<T>* stream) {
assert(base_type::_block_init);
std::lock_guard<std::recursive_mutex> lck(base_type::ctrlMtx);

// Check that the stream isn't already bound
if (std::find(streams.begin(), streams.end(), stream) != streams.end()) {
throw std::runtime_error("[Splitter] Tried to bind stream to that is already bound");
}

// Add to the list
base_type::tempStop();
streams.push_back(stream);
base_type::tempStart();
}

void unbindStream(stream<T>* stream) {
assert(base_type::_block_init);
std::lock_guard<std::recursive_mutex> lck(base_type::ctrlMtx);

// Check that the stream is bound
auto sit = std::find(streams.begin(), streams.end(), stream);
if (sit == streams.end()) {
throw std::runtime_error("[Splitter] Tried to unbind stream to that isn't bound");
}

// Add to the list
base_type::tempStop();
streams.erase(sit);
base_type::tempStart();
}

int run() {
int count = _in->read();
if (count < 0) { return -1; }

for (const auto& stream : outStreams) {
for (const auto& stream : streams) {
memcpy(stream->writeBuf, _in->readBuf, count * sizeof(T));
if (!stream->swap(count)) {
_in->flush();
Expand All @@ -28,7 +59,7 @@ namespace dsp::routing {
}

protected:
std::vector<stream<T>*> outStreams;
std::vector<stream<T>*> streams;

};
}

0 comments on commit 89f08ab

Please sign in to comment.