From 49dcac5c21f63fd253c0291f537eef019cc8a0bd Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 26 Aug 2021 17:17:55 +0200 Subject: [PATCH] filter/TwoFilters: add class PreparedTwoFilters --- src/filter/plugins/TwoFilters.cxx | 21 +++++++++++++++++++++ src/filter/plugins/TwoFilters.hxx | 19 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/src/filter/plugins/TwoFilters.cxx b/src/filter/plugins/TwoFilters.cxx index be84a3dbf..213df8548 100644 --- a/src/filter/plugins/TwoFilters.cxx +++ b/src/filter/plugins/TwoFilters.cxx @@ -18,7 +18,10 @@ */ #include "TwoFilters.hxx" +#include "pcm/AudioFormat.hxx" #include "util/ConstBuffer.hxx" +#include "util/RuntimeError.hxx" +#include "util/StringBuffer.hxx" ConstBuffer TwoFilters::FilterPCM(ConstBuffer src) @@ -37,3 +40,21 @@ TwoFilters::Flush() return second->Flush(); } + +std::unique_ptr +PreparedTwoFilters::Open(AudioFormat &audio_format) +{ + auto a = first->Open(audio_format); + + const auto &a_out_format = a->GetOutAudioFormat(); + auto b_in_format = a_out_format; + auto b = second->Open(b_in_format); + + if (b_in_format != a_out_format) + throw FormatRuntimeError("Audio format not supported by filter '%s': %s", + second_name.c_str(), + ToString(a_out_format).c_str()); + + return std::make_unique(std::move(a), + std::move(b)); +} diff --git a/src/filter/plugins/TwoFilters.hxx b/src/filter/plugins/TwoFilters.hxx index 76b338347..28fa31948 100644 --- a/src/filter/plugins/TwoFilters.hxx +++ b/src/filter/plugins/TwoFilters.hxx @@ -21,8 +21,10 @@ #define MPD_TWO_FILTERS_HXX #include "filter/Filter.hxx" +#include "filter/Prepared.hxx" #include +#include /** * A #Filter implementation which chains two other filters. @@ -46,4 +48,21 @@ public: ConstBuffer Flush() override; }; +/** + * Like #TwoFilters, but implements the #PreparedFilter interface. + */ +class PreparedTwoFilters final : public PreparedFilter { + std::unique_ptr first, second; + std::string second_name; + +public: + template + PreparedTwoFilters(F &&_first, S &&_second, N &&_second_name) noexcept + :first(std::forward(_first)), + second(std::forward(_second)), + second_name(std::forward(_second_name)) {} + + std::unique_ptr Open(AudioFormat &audio_format) override; +}; + #endif