MusicChunk.hxx 4.13 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
 * 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.
14 15 16 17
 *
 * 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.
18 19
 */

20 21
#ifndef MPD_MUSIC_CHUNK_HXX
#define MPD_MUSIC_CHUNK_HXX
22

23
#include "Chrono.hxx"
24
#include "ReplayGainInfo.hxx"
25
#include "util/WritableBuffer.hxx"
26

27
#ifndef NDEBUG
28
#include "AudioFormat.hxx"
29 30
#endif

31 32
#include <memory>

33
#include <stdint.h>
34
#include <stddef.h>
35

36
static constexpr size_t CHUNK_SIZE = 4096;
37

38
struct AudioFormat;
Max Kellermann's avatar
Max Kellermann committed
39
struct Tag;
40
struct MusicChunk;
41

42
/**
43
 * Meta information for #MusicChunk.
44
 */
45
struct MusicChunkInfo {
46
	/** the next chunk in a linked list */
47
	MusicChunk *next;
48

49 50 51 52
	/**
	 * An optional chunk which should be mixed into this chunk.
	 * This is used for cross-fading.
	 */
53
	MusicChunk *other = nullptr;
54

55 56 57 58 59 60
	/**
	 * An optional tag associated with this chunk (and the
	 * following chunks); appears at song boundaries.
	 */
	std::unique_ptr<Tag> tag;

61 62 63 64 65 66
	/**
	 * The current mix ratio for cross-fading: 1.0 means play 100%
	 * of this chunk, 0.0 means play 100% of the "other" chunk.
	 */
	float mix_ratio;

67
	/** number of bytes stored in this chunk */
68
	uint16_t length = 0;
69 70 71 72 73

	/** current bit rate of the source file */
	uint16_t bit_rate;

	/** the time stamp within the song */
74
	SignedSongTime time;
75

76 77 78 79
	/**
	 * Replay gain information associated with this chunk.
	 * Only valid if the serial is not 0.
	 */
80
	ReplayGainInfo replay_gain_info;
81

82 83 84 85 86 87 88 89
	/**
	 * A magic value for #replay_gain_serial which omits updating
	 * the #ReplayGainFilter.  This is used by "silence" chunks
	 * (see PlayerThread::SendSilence()) so they don't affect the
	 * replay gain.
	 */
	static constexpr unsigned IGNORE_REPLAY_GAIN = ~0u;

90 91 92 93 94
	/**
	 * A serial number for checking if replay gain info has
	 * changed since the last chunk.  The magic value 0 indicates
	 * that there is no replay gain info available.
	 */
95
	unsigned replay_gain_serial;
96

97
#ifndef NDEBUG
98
	AudioFormat audio_format;
99
#endif
100

101 102
	MusicChunkInfo() noexcept;
	~MusicChunkInfo() noexcept;
103

104 105
	MusicChunkInfo(const MusicChunkInfo &) = delete;
	MusicChunkInfo &operator=(const MusicChunkInfo &) = delete;
106

107 108 109
	bool IsEmpty() const {
		return length == 0 && tag == nullptr;
	}
110

111
#ifndef NDEBUG
112 113 114 115 116
	/**
	 * Checks if the audio format if the chunk is equal to the
	 * specified audio_format.
	 */
	gcc_pure
117
	bool CheckFormat(AudioFormat audio_format) const noexcept;
118
#endif
119 120 121 122 123 124 125 126
};

/**
 * A chunk of music data.  Its format is defined by the
 * MusicPipe::Push() caller.
 */
struct MusicChunk : MusicChunkInfo {
	/** the data (probably PCM) */
127
	uint8_t data[CHUNK_SIZE - sizeof(MusicChunkInfo)];
128

129 130 131
	/**
	 * Prepares appending to the music chunk.  Returns a buffer
	 * where you may write into.  After you are finished, call
132
	 * Expand().
133
	 *
Max Kellermann's avatar
Max Kellermann committed
134
	 * @param af the audio format for the appended data;
135 136 137
	 * must stay the same for the life cycle of this chunk
	 * @param data_time the time within the song
	 * @param bit_rate the current bit rate of the source file
138
	 * @return a writable buffer, or nullptr if the chunk is full
139
	 */
140
	WritableBuffer<void> Write(AudioFormat af,
141
				   SongTime data_time,
142
				   uint16_t bit_rate) noexcept;
143

144 145
	/**
	 * Increases the length of the chunk after the caller has written to
146
	 * the buffer returned by Write().
147
	 *
Max Kellermann's avatar
Max Kellermann committed
148
	 * @param af the audio format for the appended data; must
149 150 151 152
	 * stay the same for the life cycle of this chunk
	 * @param length the number of bytes which were appended
	 * @return true if the chunk is full
	 */
153
	bool Expand(AudioFormat af, size_t length) noexcept;
154 155
};

156 157
static_assert(sizeof(MusicChunk) == CHUNK_SIZE, "Wrong size");

158
#endif