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 <assert.h>
#include <math.h> #include <math.h>
static Filter *
CreateVolumeFilter()
{
return filter_new(&volume_filter_plugin, ConfigBlock(),
IgnoreError());
}
class SoftwareMixer final : public Mixer { class SoftwareMixer final : public Mixer {
Filter *filter; Filter *filter = nullptr;
/**
* 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;
/** /**
* The current volume in percent (0..100). * The current volume in percent (0..100).
@ -56,18 +41,11 @@ class SoftwareMixer final : public Mixer {
public: public:
SoftwareMixer(MixerListener &_listener) SoftwareMixer(MixerListener &_listener)
:Mixer(software_mixer_plugin, _listener), :Mixer(software_mixer_plugin, _listener)
filter(CreateVolumeFilter())
{ {
assert(filter != nullptr);
} }
virtual ~SoftwareMixer() { void SetFilter(Filter *_filter);
if (owns_filter)
delete filter;
}
Filter *GetFilter();
/* virtual methods from class Mixer */ /* virtual methods from class Mixer */
virtual bool Open(gcc_unused Error &error) override { virtual bool Open(gcc_unused Error &error) override {
@ -115,7 +93,9 @@ SoftwareMixer::SetVolume(unsigned new_volume, gcc_unused Error &error)
assert(new_volume <= 100); assert(new_volume <= 100);
volume = new_volume; volume = new_volume;
volume_filter_set(filter, PercentVolumeToSoftwareVolume(new_volume));
if (filter != nullptr)
volume_filter_set(filter, PercentVolumeToSoftwareVolume(new_volume));
return true; return true;
} }
@ -124,19 +104,19 @@ const MixerPlugin software_mixer_plugin = {
true, true,
}; };
inline Filter * inline void
SoftwareMixer::GetFilter() SoftwareMixer::SetFilter(Filter *_filter)
{ {
assert(owns_filter); filter = _filter;
owns_filter = false; if (filter != nullptr)
return filter; volume_filter_set(filter,
PercentVolumeToSoftwareVolume(volume));
} }
Filter * void
software_mixer_get_filter(Mixer *mixer) software_mixer_set_filter(Mixer &mixer, Filter *filter)
{ {
SoftwareMixer *sm = (SoftwareMixer *)mixer; SoftwareMixer &sm = (SoftwareMixer &)mixer;
assert(sm->IsPlugin(software_mixer_plugin)); sm.SetFilter(filter);
return sm->GetFilter();
} }

View File

@ -24,10 +24,12 @@ class Mixer;
class Filter; class Filter;
/** /**
* Returns the (volume) filter associated with this mixer. All users * Attach a #VolumeFilter to this mixer. The #VolumeFilter is the
* of this mixer plugin should install this filter. * 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 * void
software_mixer_get_filter(Mixer *mixer); software_mixer_set_filter(Mixer &mixer, Filter *filter);
#endif #endif

View File

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

View File

@ -189,6 +189,12 @@ struct AudioOutput {
*/ */
Filter *filter = nullptr; 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 * The replay_gain_filter_plugin instance of this audio
* output. * output.