Partition.cxx 3.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 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 "DetachedSong.hxx"
24
#include "mixer/Volume.hxx"
25
#include "IdleFlags.hxx"
26

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

45 46 47
void
Partition::EmitIdle(unsigned mask)
{
48
	instance.EmitIdle(mask);
49 50
}

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

60
	pc.LockSetReplayGainMode(mode);
61

62 63 64
	outputs.SetReplayGainMode(mode);
}

65 66
#ifdef ENABLE_DATABASE

67
const Database *
68
Partition::GetDatabase() const
69
{
70
	return instance.GetDatabase();
71 72
}

73 74 75 76 77 78
const Database &
Partition::GetDatabaseOrThrow() const
{
	return instance.GetDatabaseOrThrow();
}

79
void
80
Partition::DatabaseModified(const Database &db)
81
{
82
	playlist.DatabaseModified(db);
83
	EmitIdle(IDLE_DATABASE);
84 85
}

86 87
#endif

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

void
Partition::SyncWithPlayer()
{
	playlist.SyncWithPlayer(pc);
}
101

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
void
Partition::OnQueueModified()
{
	EmitIdle(IDLE_PLAYLIST);
}

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

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

120
void
121
Partition::OnPlayerSync() noexcept
122
{
123
	EmitGlobalEvent(SYNC_WITH_PLAYER);
124 125 126
}

void
127
Partition::OnPlayerTagModified() noexcept
128
{
129
	EmitGlobalEvent(TAG_MODIFIED);
130 131
}

132 133 134 135 136 137
void
Partition::OnMixerVolumeChanged(gcc_unused Mixer &mixer, gcc_unused int volume)
{
	InvalidateHardwareVolume();

	/* notify clients */
138
	EmitIdle(IDLE_MIXER);
139
}
140 141 142 143

void
Partition::OnGlobalEvent(unsigned mask)
{
144
	if ((mask & SYNC_WITH_PLAYER) != 0)
145
		SyncWithPlayer();
146 147 148

	if ((mask & TAG_MODIFIED) != 0)
		TagModified();
149
}