MusicChunk.cxx 1.95 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 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 44
MusicChunk::Write(const AudioFormat af,
		  float data_time, uint16_t _bit_rate)
45
{
46
	assert(CheckFormat(af));
47
	assert(length == 0 || audio_format.IsValid());
48

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

53 54
		bit_rate = _bit_rate;
		times = data_time;
55 56
	}

57
	const size_t frame_size = af.GetFrameSize();
58
	size_t num_frames = (sizeof(data) - length) / frame_size;
59
	if (num_frames == 0)
60
		return WritableBuffer<void>::Null();
61

62
#ifndef NDEBUG
63
	audio_format = af;
64 65
#endif

66
	return { data + length, num_frames * frame_size };
67 68 69
}

bool
70
MusicChunk::Expand(const AudioFormat af, size_t _length)
71
{
72
	const size_t frame_size = af.GetFrameSize();
73

74
	assert(length + _length <= sizeof(data));
75
	assert(audio_format == af);
76

77
	length += _length;
78

79
	return length + frame_size > sizeof(data);
80
}