FlacPlaylistPlugin.cxx 2.96 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 * 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.
 *
 * 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.
 */

/** \file
 *
 * Playlist plugin that reads embedded cue sheets from the "CUESHEET"
 * tag of a music file.
 */

#include "FlacPlaylistPlugin.hxx"
#include "../PlaylistPlugin.hxx"
28
#include "../MemorySongEnumerator.hxx"
29 30
#include "lib/xiph/FlacMetadataChain.hxx"
#include "lib/xiph/FlacMetadataIterator.hxx"
31
#include "song/DetachedSong.hxx"
32
#include "input/InputStream.hxx"
33
#include "util/RuntimeError.hxx"
34 35 36

#include <FLAC/metadata.h>

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
static auto
ToSongEnumerator(const char *uri,
		 const FLAC__StreamMetadata_CueSheet &c,
		 const unsigned sample_rate,
		 const FLAC__uint64 total_samples) noexcept
{
	std::forward_list<DetachedSong> songs;
	auto tail = songs.before_begin();

	for (unsigned i = 0; i < c.num_tracks; ++i) {
		const auto &track = c.tracks[i];
		if (track.type != 0)
			continue;

		const FLAC__uint64 start = track.offset;
		const FLAC__uint64 end = i + 1 < c.num_tracks
			? c.tracks[i + 1].offset
			: total_samples;

		tail = songs.emplace_after(tail, uri);
		auto &song = *tail;
		song.SetStartTime(SongTime::FromScale(start, sample_rate));
		song.SetEndTime(SongTime::FromScale(end, sample_rate));
	}

	return std::make_unique<MemorySongEnumerator>(std::move(songs));
}

65
static std::unique_ptr<SongEnumerator>
66
flac_playlist_open_stream(InputStreamPtr &&is)
67
{
68
	FlacMetadataChain chain;
69
	if (!chain.Read(*is))
70 71
		throw FormatRuntimeError("Failed to read FLAC metadata: %s",
					 chain.GetStatusString());
72

73
	FlacMetadataIterator iterator((FLAC__Metadata_Chain *)chain);
74

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
	unsigned sample_rate = 0;
	FLAC__uint64 total_samples;

	do {
		auto &block = *iterator.GetBlock();
		switch (block.type) {
		case FLAC__METADATA_TYPE_STREAMINFO:
			sample_rate = block.data.stream_info.sample_rate;
			total_samples = block.data.stream_info.total_samples;
			break;

		case FLAC__METADATA_TYPE_CUESHEET:
			if (sample_rate == 0)
				break;

90
			return ToSongEnumerator("", block.data.cue_sheet,
91
						sample_rate, total_samples);
92

93 94 95 96
		default:
			break;
		}
	} while (iterator.Next());
97

98
	return nullptr;
99 100 101 102 103 104 105
}

static const char *const flac_playlist_suffixes[] = {
	"flac",
	nullptr
};

106 107 108
const PlaylistPlugin flac_playlist_plugin =
	PlaylistPlugin("flac", flac_playlist_open_stream)
	.WithSuffixes(flac_playlist_suffixes);