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

25 26
#include <assert.h>

27
MusicChunk::~MusicChunk()
28
{
Max Kellermann's avatar
Max Kellermann committed
29
	delete tag;
30
}
31

32 33
#ifndef NDEBUG
bool
34
MusicChunk::CheckFormat(const AudioFormat other_format) const
35
{
36
	assert(other_format.IsValid());
37

38
	return length == 0 || audio_format == other_format;
39 40 41
}
#endif

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

49
	if (length == 0) {
50
		/* if the chunk is empty, nobody has set bitRate and
51
		   time yet */
52

53
		bit_rate = _bit_rate;
54
		time = data_time;
55 56 57 58

#ifndef NDEBUG
		audio_format = af;
#endif
59 60
	}

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

bool
67
MusicChunk::Expand(const AudioFormat af, size_t _length)
68
{
69
	const size_t frame_size = af.GetFrameSize();
70

71
	assert(length + _length <= sizeof(data));
72
	assert(audio_format == af);
73

74
	length += _length;
75

76
	return length + frame_size > sizeof(data);
77
}