ChainFilterPlugin.cxx 4.02 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * 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.
 */

20
#include "config.h"
Max Kellermann's avatar
Max Kellermann committed
21
#include "ChainFilterPlugin.hxx"
22 23 24
#include "filter/FilterPlugin.hxx"
#include "filter/FilterInternal.hxx"
#include "filter/FilterRegistry.hxx"
25
#include "AudioFormat.hxx"
26
#include "util/ConstBuffer.hxx"
27
#include "util/StringBuffer.hxx"
28
#include "util/RuntimeError.hxx"
29

30
#include <memory>
31 32
#include <list>

33 34
#include <assert.h>

35 36 37 38 39 40 41 42 43 44
class ChainFilter final : public Filter {
	struct Child {
		const char *name;
		Filter *filter;

		Child(const char *_name, Filter *_filter)
			:name(_name), filter(_filter) {}
		~Child() {
			delete filter;
		}
45

46 47 48
		Child(const Child &) = delete;
		Child &operator=(const Child &) = delete;
	};
Max Kellermann's avatar
Max Kellermann committed
49

50
	std::list<Child> children;
51

52
public:
53 54 55
	explicit ChainFilter(AudioFormat _audio_format)
		:Filter(_audio_format) {}

56
	void Append(const char *name, Filter *filter) {
57 58 59 60
		assert(out_audio_format.IsValid());
		out_audio_format = filter->GetOutAudioFormat();
		assert(out_audio_format.IsValid());

61
		children.emplace_back(name, filter);
62
	}
63

64
	/* virtual methods from class Filter */
65
	void Reset() override;
66
	ConstBuffer<void> FilterPCM(ConstBuffer<void> src) override;
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
};

class PreparedChainFilter final : public PreparedFilter {
	struct Child {
		const char *name;
		PreparedFilter *filter;

		Child(const char *_name, PreparedFilter *_filter)
			:name(_name), filter(_filter) {}
		~Child() {
			delete filter;
		}

		Child(const Child &) = delete;
		Child &operator=(const Child &) = delete;

83
		Filter *Open(const AudioFormat &prev_audio_format);
84 85 86 87 88 89 90 91
	};

	std::list<Child> children;

public:
	void Append(const char *name, PreparedFilter *filter) {
		children.emplace_back(name, filter);
	}
92

93
	/* virtual methods from class PreparedFilter */
94
	Filter *Open(AudioFormat &af) override;
95 96
};

97
static PreparedFilter *
98
chain_filter_init(gcc_unused const ConfigBlock &block)
99
{
100
	return new PreparedChainFilter();
101 102
}

103
Filter *
104
PreparedChainFilter::Child::Open(const AudioFormat &prev_audio_format)
105
{
106
	AudioFormat conv_audio_format = prev_audio_format;
107
	Filter *new_filter = filter->Open(conv_audio_format);
108

109
	if (conv_audio_format != prev_audio_format) {
110
		delete new_filter;
111

112 113
		throw FormatRuntimeError("Audio format not supported by filter '%s': %s",
					 name,
114
					 ToString(prev_audio_format).c_str());
115 116
	}

117
	return new_filter;
118 119
}

120
Filter *
121
PreparedChainFilter::Open(AudioFormat &in_audio_format)
122
{
123
	std::unique_ptr<ChainFilter> chain(new ChainFilter(in_audio_format));
124

125
	for (auto &child : children) {
126
		AudioFormat audio_format = chain->GetOutAudioFormat();
127
		auto *filter = child.Open(audio_format);
128 129
		chain->Append(child.name, filter);
	}
130

131
	return chain.release();
132 133
}

134 135 136 137 138 139 140
void
ChainFilter::Reset()
{
	for (auto &child : children)
		child.filter->Reset();
}

141
ConstBuffer<void>
142
ChainFilter::FilterPCM(ConstBuffer<void> src)
143
{
144
	for (auto &child : children) {
145 146
		/* feed the output of the previous filter as input
		   into the current one */
147
		src = child.filter->FilterPCM(src);
148 149 150 151 152 153
	}

	/* return the output of the last filter */
	return src;
}

154
const FilterPlugin chain_filter_plugin = {
Max Kellermann's avatar
Max Kellermann committed
155 156
	"chain",
	chain_filter_init,
157 158
};

159
PreparedFilter *
160 161
filter_chain_new(void)
{
162
	return new PreparedChainFilter();
163 164 165
}

void
166 167
filter_chain_append(PreparedFilter &_chain, const char *name,
		    PreparedFilter *filter)
168
{
169
	PreparedChainFilter &chain = (PreparedChainFilter &)_chain;
170

171
	chain.Append(name, filter);
172
}