diff options
author | Max Kellermann <max@musicpd.org> | 2020-11-04 16:54:41 +0100 |
---|---|---|
committer | Max Kellermann <max@musicpd.org> | 2020-11-04 20:15:19 +0100 |
commit | 0c965d05734d18b4cfd2e4b5ac1b2a6ca0c6515d (patch) | |
tree | 4a01a167d357555eae6b6ae84808609465f54a65 /src | |
parent | 77c14692c9b7cb85efb8e1fb1a6024284a75d53c (diff) |
filter/AutoConvert: move the Filter class to TwoFilters.cxx
Diffstat (limited to 'src')
-rw-r--r-- | src/filter/plugins/AutoConvertFilterPlugin.cxx | 49 | ||||
-rw-r--r-- | src/filter/plugins/TwoFilters.cxx | 39 | ||||
-rw-r--r-- | src/filter/plugins/TwoFilters.hxx | 49 | ||||
-rw-r--r-- | src/filter/plugins/meson.build | 1 |
4 files changed, 92 insertions, 46 deletions
diff --git a/src/filter/plugins/AutoConvertFilterPlugin.cxx b/src/filter/plugins/AutoConvertFilterPlugin.cxx index d3eb38447..b9bbd256f 100644 --- a/src/filter/plugins/AutoConvertFilterPlugin.cxx +++ b/src/filter/plugins/AutoConvertFilterPlugin.cxx @@ -19,6 +19,7 @@ #include "AutoConvertFilterPlugin.hxx" #include "ConvertFilterPlugin.hxx" +#include "TwoFilters.hxx" #include "filter/Filter.hxx" #include "filter/Prepared.hxx" #include "pcm/AudioFormat.hxx" @@ -27,33 +28,6 @@ #include <cassert> #include <memory> -class AutoConvertFilter final : public Filter { - /** - * The underlying filter. - */ - std::unique_ptr<Filter> filter; - - /** - * A convert_filter, just in case conversion is needed. nullptr - * if unused. - */ - std::unique_ptr<Filter> convert; - -public: - AutoConvertFilter(std::unique_ptr<Filter> &&_filter, - std::unique_ptr<Filter> &&_convert) - :Filter(_filter->GetOutAudioFormat()), - filter(std::move(_filter)), convert(std::move(_convert)) {} - - void Reset() noexcept override { - filter->Reset(); - convert->Reset(); - } - - ConstBuffer<void> FilterPCM(ConstBuffer<void> src) override; - ConstBuffer<void> Flush() override; -}; - class PreparedAutoConvertFilter final : public PreparedFilter { /** * The underlying filter. @@ -88,25 +62,8 @@ PreparedAutoConvertFilter::Open(AudioFormat &in_audio_format) auto convert = convert_filter_new(in_audio_format, child_audio_format); - return std::make_unique<AutoConvertFilter>(std::move(new_filter), - std::move(convert)); -} - -ConstBuffer<void> -AutoConvertFilter::FilterPCM(ConstBuffer<void> src) -{ - src = convert->FilterPCM(src); - return filter->FilterPCM(src); -} - -ConstBuffer<void> -AutoConvertFilter::Flush() -{ - auto result = convert->Flush(); - if (!result.IsNull()) - return filter->FilterPCM(result); - - return filter->Flush(); + return std::make_unique<TwoFilters>(std::move(convert), + std::move(new_filter)); } std::unique_ptr<PreparedFilter> diff --git a/src/filter/plugins/TwoFilters.cxx b/src/filter/plugins/TwoFilters.cxx new file mode 100644 index 000000000..0defa5799 --- /dev/null +++ b/src/filter/plugins/TwoFilters.cxx @@ -0,0 +1,39 @@ +/* + * Copyright 2003-2020 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "TwoFilters.hxx" +#include "util/ConstBuffer.hxx" + +ConstBuffer<void> +TwoFilters::FilterPCM(ConstBuffer<void> src) +{ + return second->FilterPCM(first->FilterPCM(src)); +} + +ConstBuffer<void> +TwoFilters::Flush() +{ + auto result = first->Flush(); + if (!result.IsNull()) + /* Flush() output from the first Filter must be + filtered by the second Filter */ + return second->FilterPCM(result); + + return second->Flush(); +} diff --git a/src/filter/plugins/TwoFilters.hxx b/src/filter/plugins/TwoFilters.hxx new file mode 100644 index 000000000..cbd86a6df --- /dev/null +++ b/src/filter/plugins/TwoFilters.hxx @@ -0,0 +1,49 @@ +/* + * Copyright 2003-2020 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_WITH_CONVERT_FILTER_HXX +#define MPD_WITH_CONVERT_FILTER_HXX + +#include "filter/Filter.hxx" + +#include <memory> + +/** + * A #Filter implementation which chains two other filters. + */ +class TwoFilters final : public Filter { + std::unique_ptr<Filter> first, second; + +public: + template<typename F, typename S> + TwoFilters(F &&_first, S &&_second) noexcept + :Filter(_second->GetOutAudioFormat()), + first(std::forward<F>(_first)), + second(std::forward<S>(_second)) {} + + void Reset() noexcept override { + first->Reset(); + second->Reset(); + } + + ConstBuffer<void> FilterPCM(ConstBuffer<void> src) override; + ConstBuffer<void> Flush() override; +}; + +#endif diff --git a/src/filter/plugins/meson.build b/src/filter/plugins/meson.build index 8ad526c3d..cf313f20b 100644 --- a/src/filter/plugins/meson.build +++ b/src/filter/plugins/meson.build @@ -14,6 +14,7 @@ filter_plugins = static_library( 'filter_plugins', '../../AudioCompress/compress.c', 'NullFilterPlugin.cxx', + 'TwoFilters.cxx', 'ChainFilterPlugin.cxx', 'AutoConvertFilterPlugin.cxx', 'ConvertFilterPlugin.cxx', |