MusicChunk.hxx 3.81 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2013 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 "ReplayGainInfo.hxx"
24
#include "util/WritableBuffer.hxx"
25

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

30
#include <stdint.h>
31
#include <stddef.h>
32

33
static constexpr size_t CHUNK_SIZE = 4096;
34

35
struct AudioFormat;
Max Kellermann's avatar
Max Kellermann committed
36
struct Tag;
37

38 39
/**
 * A chunk of music data.  Its format is defined by the
40
 * MusicPipe::Push() caller.
41 42
 */
struct music_chunk {
43 44 45
	/** the next chunk in a linked list */
	struct music_chunk *next;

46 47 48 49 50 51
	/**
	 * An optional chunk which should be mixed into this chunk.
	 * This is used for cross-fading.
	 */
	struct music_chunk *other;

52 53 54 55 56 57
	/**
	 * 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;

58 59 60 61 62 63 64 65 66 67 68 69 70
	/** number of bytes stored in this chunk */
	uint16_t length;

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

	/** the time stamp within the song */
	float times;

	/**
	 * An optional tag associated with this chunk (and the
	 * following chunks); appears at song boundaries.  The tag
	 * object is owned by this chunk, and must be freed when this
71
	 * chunk is deinitialized.
72
	 */
Max Kellermann's avatar
Max Kellermann committed
73
	Tag *tag;
74

75 76 77 78
	/**
	 * Replay gain information associated with this chunk.
	 * Only valid if the serial is not 0.
	 */
79
	ReplayGainInfo replay_gain_info;
80 81 82 83 84 85 86 87

	/**
	 * 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.
	 */
	unsigned replay_gain_serial;

88
	/** the data (probably PCM) */
89
	uint8_t data[CHUNK_SIZE];
90 91

#ifndef NDEBUG
92
	AudioFormat audio_format;
93
#endif
94

95 96 97 98 99
	music_chunk()
		:other(nullptr),
		 length(0),
		 tag(nullptr),
		 replay_gain_serial(0) {}
100

101
	~music_chunk();
102

103 104 105
	bool IsEmpty() const {
		return length == 0 && tag == nullptr;
	}
106

107
#ifndef NDEBUG
108 109 110 111 112
	/**
	 * Checks if the audio format if the chunk is equal to the
	 * specified audio_format.
	 */
	gcc_pure
113
	bool CheckFormat(AudioFormat audio_format) const;
114 115
#endif

116 117 118 119 120 121 122 123 124 125 126 127
	/**
	 * Prepares appending to the music chunk.  Returns a buffer
	 * where you may write into.  After you are finished, call
	 * music_chunk_expand().
	 *
	 * @param chunk the music_chunk object
	 * @param audio_format the audio format for the appended data;
	 * 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
	 * @param max_length_r the maximum write length is returned
	 * here
128
	 * @return a writable buffer, or nullptr if the chunk is full
129
	 */
130 131
	WritableBuffer<void> Write(AudioFormat af,
				   float data_time, uint16_t bit_rate);
132

133 134 135 136 137 138 139 140 141 142
	/**
	 * Increases the length of the chunk after the caller has written to
	 * the buffer returned by music_chunk_write().
	 *
	 * @param chunk the music_chunk object
	 * @param audio_format the audio format for the appended data; must
	 * 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
	 */
143
	bool Expand(AudioFormat af, size_t length);
144 145
};

146
#endif