MusicChunk.cxx 1.96 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 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
#include "MusicChunk.hxx"
21
#include "AudioFormat.hxx"
22
#include "tag/Tag.hxx"
23

24 25
#include <assert.h>

26 27
MusicChunkInfo::MusicChunkInfo() noexcept = default;
MusicChunkInfo::~MusicChunkInfo() noexcept = default;
28

29 30
#ifndef NDEBUG
bool
31
MusicChunkInfo::CheckFormat(const AudioFormat other_format) const noexcept
32
{
33
	assert(other_format.IsValid());
34

35
	return length == 0 || audio_format == other_format;
36 37 38
}
#endif

39
WritableBuffer<void>
40
MusicChunk::Write(const AudioFormat af,
41
		  SongTime data_time, uint16_t _bit_rate) noexcept
42
{
43
	assert(CheckFormat(af));
44
	assert(length == 0 || audio_format.IsValid());
45

46
	if (length == 0) {
47
		/* if the chunk is empty, nobody has set bitRate and
48
		   time yet */
49

50
		bit_rate = _bit_rate;
51
		time = data_time;
52 53 54 55

#ifndef NDEBUG
		audio_format = af;
#endif
56 57
	}

58
	const size_t frame_size = af.GetFrameSize();
59
	size_t num_frames = (sizeof(data) - length) / frame_size;
60
	return { data + length, num_frames * frame_size };
61 62 63
}

bool
64
MusicChunk::Expand(const AudioFormat af, size_t _length) noexcept
65
{
66
	const size_t frame_size = af.GetFrameSize();
67

68
	assert(length + _length <= sizeof(data));
69
	assert(audio_format == af);
70

71
	length += _length;
72

73
	return length + frame_size > sizeof(data);
74
}