mixer/software: move Filter management to the AudioOutput

This commit is contained in:
Max Kellermann 2016-07-01 18:23:53 +02:00
parent d93271e86d
commit a43b0f5253
4 changed files with 43 additions and 41 deletions

View File

@ -31,23 +31,8 @@
#include <assert.h>
#include <math.h>
static Filter *
CreateVolumeFilter()
{
return filter_new(&volume_filter_plugin, ConfigBlock(),
IgnoreError());
}
class SoftwareMixer final : public Mixer {
Filter *filter;
/**
* If this is true, then this object "owns" the #Filter
* instance (see above). It will be set to false by
* software_mixer_get_filter(); after that, the caller will be
* responsible for the #Filter.
*/
bool owns_filter = true;
Filter *filter = nullptr;
/**
* The current volume in percent (0..100).
@ -56,18 +41,11 @@ class SoftwareMixer final : public Mixer {
public:
SoftwareMixer(MixerListener &_listener)
:Mixer(software_mixer_plugin, _listener),
filter(CreateVolumeFilter())
:Mixer(software_mixer_plugin, _listener)
{
assert(filter != nullptr);
}
virtual ~SoftwareMixer() {
if (owns_filter)
delete filter;
}
Filter *GetFilter();
void SetFilter(Filter *_filter);
/* virtual methods from class Mixer */
virtual bool Open(gcc_unused Error &error) override {
@ -115,6 +93,8 @@ SoftwareMixer::SetVolume(unsigned new_volume, gcc_unused Error &error)
assert(new_volume <= 100);
volume = new_volume;
if (filter != nullptr)
volume_filter_set(filter, PercentVolumeToSoftwareVolume(new_volume));
return true;
}
@ -124,19 +104,19 @@ const MixerPlugin software_mixer_plugin = {
true,
};
inline Filter *
SoftwareMixer::GetFilter()
inline void
SoftwareMixer::SetFilter(Filter *_filter)
{
assert(owns_filter);
filter = _filter;
owns_filter = false;
return filter;
if (filter != nullptr)
volume_filter_set(filter,
PercentVolumeToSoftwareVolume(volume));
}
Filter *
software_mixer_get_filter(Mixer *mixer)
void
software_mixer_set_filter(Mixer &mixer, Filter *filter)
{
SoftwareMixer *sm = (SoftwareMixer *)mixer;
assert(sm->IsPlugin(software_mixer_plugin));
return sm->GetFilter();
SoftwareMixer &sm = (SoftwareMixer &)mixer;
sm.SetFilter(filter);
}

View File

@ -24,10 +24,12 @@ class Mixer;
class Filter;
/**
* Returns the (volume) filter associated with this mixer. All users
* of this mixer plugin should install this filter.
* Attach a #VolumeFilter to this mixer. The #VolumeFilter is the
* entity which actually applies the volume; it is created and managed
* by the output. Mixer::SetVolume() calls will be forwarded to
* volume_filter_set().
*/
Filter *
software_mixer_get_filter(Mixer *mixer);
void
software_mixer_set_filter(Mixer &mixer, Filter *filter);
#endif

View File

@ -102,6 +102,13 @@ audio_output_mixer_type(const ConfigBlock &block)
"hardware"));
}
static Filter *
CreateVolumeFilter()
{
return filter_new(&volume_filter_plugin, ConfigBlock(),
IgnoreError());
}
static Mixer *
audio_output_load_mixer(EventLoop &event_loop, AudioOutput &ao,
const ConfigBlock &block,
@ -110,6 +117,8 @@ audio_output_load_mixer(EventLoop &event_loop, AudioOutput &ao,
MixerListener &listener,
Error &error)
{
assert(ao.volume_filter == nullptr);
Mixer *mixer;
switch (audio_output_mixer_type(block)) {
@ -135,8 +144,13 @@ audio_output_load_mixer(EventLoop &event_loop, AudioOutput &ao,
IgnoreError());
assert(mixer != nullptr);
ao.volume_filter = CreateVolumeFilter();
assert(ao.volume_filter != nullptr);
filter_chain_append(filter_chain, "software_mixer",
software_mixer_get_filter(mixer));
ao.volume_filter);
software_mixer_set_filter(*mixer, ao.volume_filter);
return mixer;
}

View File

@ -189,6 +189,12 @@ struct AudioOutput {
*/
Filter *filter = nullptr;
/**
* The #VolumeFilter instance of this audio output. It is
* used by the #SoftwareMixer.
*/
Filter *volume_filter = nullptr;
/**
* The replay_gain_filter_plugin instance of this audio
* output.