diff --git a/src/source.cpp b/src/source.cpp --- a/src/source.cpp +++ b/src/source.cpp @@ -26,6 +26,7 @@ void Source::connect(Sink* sink) Q_ASSERT(!sinks.contains(sink)); sinks.append(sink); + sink->setNumChannels(numChannels(), hasX()); } void Source::disconnect(Sink* sink) @@ -43,10 +44,10 @@ void Source::feedOut(const SamplePack& d } } -void Source::feedNumChannels(unsigned nc, bool x) const +void Source::updateNumChannels() const { for (auto sink : sinks) { - sink->setNumChannels(nc, x); + sink->setNumChannels(numChannels(), hasX()); } } diff --git a/src/source.h b/src/source.h --- a/src/source.h +++ b/src/source.h @@ -41,8 +41,9 @@ public: protected: /// Feeds "in" given data to connected sinks void feedOut(const SamplePack& data) const; - /// Updates "number of channels" of connected sinks - void feedNumChannels(unsigned nc, bool x = false) const; + /// Updates "number of channels" of connected sinks. Must be + /// called when num. channels or hasX changes. + void updateNumChannels() const; private: QList sinks; diff --git a/tests/test.cpp b/tests/test.cpp --- a/tests/test.cpp +++ b/tests/test.cpp @@ -63,7 +63,6 @@ public: _hasX = false; }; -public: void feedIn(const SamplePack& data) { REQUIRE(data.numChannels() == numChannels()); @@ -119,3 +118,65 @@ TEST_CASE("sink", "[memory, stream]") REQUIRE(follower.numChannels() == 2); REQUIRE(follower.hasX() == true); } + +class TestSource : public Source +{ +public: + int _numChannels; + bool _hasX; + + TestSource(unsigned nc, bool x) + { + _numChannels = nc; + _hasX = x; + }; + + virtual unsigned numChannels() const + { + return _numChannels; + }; + + virtual bool hasX() const + { + return _hasX; + }; + + void _feed(const SamplePack& data) const + { + feedOut(data); + }; + + void _setNumChannels(unsigned nc, bool x) + { + _numChannels = nc; + _hasX = x; + + updateNumChannels(); + }; +}; + +TEST_CASE("source", "[memory, stream]") +{ + TestSink sink; + + TestSource source(3, false); + + REQUIRE(source.numChannels() == 3); + REQUIRE(source.hasX() == false); + + source.connect(&sink); + REQUIRE(sink.numChannels() == 3); + REQUIRE(sink.hasX() == false); + + source._setNumChannels(5, true); + REQUIRE(sink.numChannels() == 5); + REQUIRE(sink.hasX() == true); + + SamplePack pack(100, 5, true); + source._feed(pack); + REQUIRE(sink.totalFed == 100); + + source.disconnect(&sink); + source._feed(pack); + REQUIRE(sink.totalFed == 100); +}