MusicChunk.cxx 1.98 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
#include "config.h"
21
#include "MusicChunk.hxx"
22
#include "AudioFormat.hxx"
23
#include "tag/Tag.hxx"
24

25 26
#include <assert.h>

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

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

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

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

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

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

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

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

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

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

72
	length += _length;
73

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