Partition.cxx 3.39 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * 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 "config.h"
#include "Partition.hxx"
22
#include "Instance.hxx"
23
#include "song/DetachedSong.hxx"
24
#include "mixer/Volume.hxx"
25
#include "IdleFlags.hxx"
26
#include "client/Listener.hxx"
27

28
Partition::Partition(Instance &_instance,
29
		     const char *_name,
30 31
		     unsigned max_length,
		     unsigned buffer_chunks,
32
		     AudioFormat configured_audio_format,
33
		     const ReplayGainConfig &replay_gain_config)
34
	:instance(_instance),
35
	 name(_name),
36
	 listener(new ClientListener(instance.event_loop, *this)),
37
	 global_events(instance.event_loop, BIND_THIS_METHOD(OnGlobalEvent)),
38
	 playlist(max_length, *this),
39
	 outputs(*this),
40
	 pc(*this, outputs, buffer_chunks,
41
	    configured_audio_format, replay_gain_config)
42
{
43
	UpdateEffectiveReplayGainMode();
44 45
}

46 47
Partition::~Partition() noexcept = default;

48 49 50
void
Partition::EmitIdle(unsigned mask)
{
51
	instance.EmitIdle(mask);
52 53
}

54
void
55
Partition::UpdateEffectiveReplayGainMode()
56
{
57
	auto mode = replay_gain_mode;
58
	if (mode == ReplayGainMode::AUTO)
59
	    mode = playlist.queue.random
60 61
		    ? ReplayGainMode::TRACK
		    : ReplayGainMode::ALBUM;
62

63
	pc.LockSetReplayGainMode(mode);
64

65 66 67
	outputs.SetReplayGainMode(mode);
}

68 69
#ifdef ENABLE_DATABASE

70
const Database *
71
Partition::GetDatabase() const
72
{
73
	return instance.GetDatabase();
74 75
}

76 77 78 79 80 81
const Database &
Partition::GetDatabaseOrThrow() const
{
	return instance.GetDatabaseOrThrow();
}

82
void
83
Partition::DatabaseModified(const Database &db)
84
{
85
	playlist.DatabaseModified(db);
86
	EmitIdle(IDLE_DATABASE);
87 88
}

89 90
#endif

91 92 93
void
Partition::TagModified()
{
94 95
	auto song = pc.LockReadTaggedSong();
	if (song)
96
		playlist.TagModified(std::move(*song));
97 98
}

99 100 101 102 103 104
void
Partition::TagModified(const char *uri, const Tag &tag) noexcept
{
	playlist.TagModified(uri, tag);
}

105 106 107 108 109
void
Partition::SyncWithPlayer()
{
	playlist.SyncWithPlayer(pc);
}
110

111 112 113 114 115 116
void
Partition::BorderPause()
{
	playlist.BorderPause(pc);
}

117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
void
Partition::OnQueueModified()
{
	EmitIdle(IDLE_PLAYLIST);
}

void
Partition::OnQueueOptionsChanged()
{
	EmitIdle(IDLE_OPTIONS);
}

void
Partition::OnQueueSongStarted()
{
	EmitIdle(IDLE_PLAYER);
}

135
void
136
Partition::OnPlayerSync() noexcept
137
{
138
	EmitGlobalEvent(SYNC_WITH_PLAYER);
139 140 141
}

void
142
Partition::OnPlayerTagModified() noexcept
143
{
144
	EmitGlobalEvent(TAG_MODIFIED);
145 146
}

147 148 149 150 151 152
void
Partition::OnBorderPause() noexcept
{
	EmitGlobalEvent(BORDER_PAUSE);
}

153 154 155 156 157 158
void
Partition::OnMixerVolumeChanged(gcc_unused Mixer &mixer, gcc_unused int volume)
{
	InvalidateHardwareVolume();

	/* notify clients */
159
	EmitIdle(IDLE_MIXER);
160
}
161 162 163 164

void
Partition::OnGlobalEvent(unsigned mask)
{
165
	if ((mask & SYNC_WITH_PLAYER) != 0)
166
		SyncWithPlayer();
167 168 169

	if ((mask & TAG_MODIFIED) != 0)
		TagModified();
170 171 172

	if ((mask & BORDER_PAUSE) != 0)
		BorderPause();
173
}