Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,13 @@ size_t computeNumberOfChannels(const HostGraph::HostVertex *node) {
/// @brief If `node` needs a newly negotiated channel layout, allocates a
/// replacement `DSPAudioBuffer` on the host thread, records its width as
/// pending, and returns it. Returns `nullptr` when no swap is needed (no audio
/// payload, or the pending buffer already has `computedNumberOfChannels`).
/// payload, a source, or the pending buffer already has
/// `computedNumberOfChannels`).
std::shared_ptr<audioapi::DSPAudioBuffer> buildNegotiatedBufferIfNeeded(
HostGraph::HostVertex *node,
size_t computedNumberOfChannels) {
auto *audio = audioNodeOf(node);
if (audio == nullptr) {
if (audio == nullptr || ownsOutputBuffer(node)) {
return nullptr;
}

Expand Down Expand Up @@ -321,10 +322,10 @@ auto HostGraph::addEdge(HostVertex *from, HostVertex *to) -> Result<AGEvent, Gra
return Err(GraphError::CYCLE_DETECTED);
}

// `connect()` already rejects a source as destination; negotiation below
// would otherwise build a buffer narrower than the content the source
// writes into it.
assert(!ownsOutputBuffer(to));
// `connect()` rejects a source as destination, so only the bridge of one of
// its AudioParams (no audio payload) reaches it, to order the param before
// its owner.
assert(!ownsOutputBuffer(to) || audioNodeOf(from) == nullptr);

from->outputs.push_back(to);
to->inputs.push_back(from);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
#include <audioapi/core/OfflineAudioContext.h>
#include <audioapi/core/effects/GainNode.h>
#include <audioapi/core/sources/OscillatorNode.h>
#include <audioapi/core/utils/Constants.h>
#include <audioapi/core/utils/graph/BridgeNode.h>
#include <audioapi/core/utils/graph/Graph.h>
#include <audioapi/core/utils/graph/NodeHandle.h>
#include <audioapi/types/NodeOptions.h>
#include <gtest/gtest.h>
#include <test/src/MockAudioEventHandlerRegistry.h>

#include <algorithm>
#include <memory>
Expand Down Expand Up @@ -526,6 +531,79 @@ TEST_F(BridgeGraphWrapperTest, ConcurrentWithMockGraphProcessor) {
EXPECT_TRUE(processor.allocationClean());
}

// =========================================================================
// G. Bridge into a source node (real AudioNodes, channel negotiation)
// =========================================================================

/// A source owns its output buffer: its width comes from what it renders
/// (OscillatorNode is always mono), not from its inputs. The only edge that may
/// end at a source is the bridge of one of its own AudioParams, which carries
/// no audio payload and must not trigger a channel renegotiation of the source.
class BridgeIntoSourceTest : public ::testing::Test {
protected:
using HostVertex = HostGraph::HostVertex;
static constexpr float kSampleRate = 44100.0f;

std::shared_ptr<MockAudioEventHandlerRegistry> eventRegistry_ =
std::make_shared<MockAudioEventHandlerRegistry>();
std::shared_ptr<OfflineAudioContext> context_ =
std::make_shared<OfflineAudioContext>(2, RENDER_QUANTUM_SIZE, kSampleRate, eventRegistry_);
std::shared_ptr<Graph> graph_ = context_->getGraph();

HostVertex *oscillatorVertex_ = nullptr;
OscillatorNode *oscillator_ = nullptr;

void SetUp() override {
oscillatorVertex_ =
graph_->addNode(std::make_unique<OscillatorNode>(context_, OscillatorOptions()));
oscillator_ =
static_cast<OscillatorNode *>(oscillatorVertex_->handle->audioNode->asAudioNode());
graph_->processEvents();
}

/// Mirrors AudioParamHostObject::connectToGraph.
HostVertex *addFrequencyBridge() {
auto *bridge =
graph_->addNode(std::make_unique<BridgeNode>(oscillator_->getFrequencyParam().get()));
EXPECT_TRUE(graph_->addEdge(bridge, oscillatorVertex_).is_ok());
return bridge;
}
};

TEST_F(BridgeIntoSourceTest, ParamBridgeEdgeKeepsSourceOutputBuffer) {
const auto bufferBeforeBridge = oscillator_->getNegotiatedBuffer();
ASSERT_EQ(bufferBeforeBridge->getNumberOfChannels(), 1u);
// channelCount differs from the rendered width, so a renegotiation driven
// by it would visibly widen the buffer.
ASSERT_EQ(oscillator_->getChannelCount(), 2u);

addFrequencyBridge();
graph_->processEvents();

EXPECT_EQ(oscillator_->getNegotiatedBuffer(), bufferBeforeBridge);
EXPECT_EQ(oscillator_->getOutputBuffer()->getNumberOfChannels(), 1u);
}

TEST_F(BridgeIntoSourceTest, ModulatorConnectedToParamKeepsSourceOutputBuffer) {
auto *bridge = addFrequencyBridge();
graph_->processEvents();
const auto bufferBeforeModulator = oscillator_->getNegotiatedBuffer();

// The negotiation cascade starts at the bridge and walks toward the
// destination, reaching the oscillator through bridge -> oscillator.
auto *modulator = graph_->addNode(std::make_unique<GainNode>(context_, GainOptions()));
ASSERT_TRUE(graph_->addEdge(modulator, bridge).is_ok());
graph_->processEvents();

EXPECT_EQ(oscillator_->getNegotiatedBuffer(), bufferBeforeModulator);
EXPECT_EQ(oscillator_->getOutputBuffer()->getNumberOfChannels(), 1u);

ASSERT_TRUE(graph_->removeEdge(modulator, bridge).is_ok());
graph_->processEvents();

EXPECT_EQ(oscillator_->getNegotiatedBuffer(), bufferBeforeModulator);
}

// =========================================================================
// F. Fuzz test with bridge nodes
// =========================================================================
Expand Down
Loading