summaryrefslogtreecommitdiff
path: root/src/playlist/plugins/FlacPlaylistPlugin.cxx
blob: f409afd925ace8bf7cf531c62442b3fcc5dab4f3 (plain)
1
2
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
28
29
30
31
32
33
34
35
36
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
 * Copyright 2003-2019 The Music Player Daemon Project
 * 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"
#include "../MemorySongEnumerator.hxx"
#include "song/DetachedSong.hxx"
#include "fs/Traits.hxx"
#include "fs/AllocatedPath.hxx"
#include "fs/NarrowPath.hxx"
#include "util/ScopeExit.hxx"

#include <FLAC/metadata.h>

static std::unique_ptr<SongEnumerator>
flac_playlist_open_uri(const char *uri,
		       gcc_unused Mutex &mutex)
{
	if (!PathTraitsUTF8::IsAbsolute(uri))
		/* only local files supported */
		return nullptr;

	const auto path_fs = AllocatedPath::FromUTF8Throw(uri);

	const NarrowPath narrow_path_fs(path_fs);

	FLAC__StreamMetadata *cuesheet;
	if (!FLAC__metadata_get_cuesheet(narrow_path_fs, &cuesheet))
		return nullptr;

	AtScopeExit(cuesheet) { FLAC__metadata_object_delete(cuesheet); };

	FLAC__StreamMetadata streaminfo;
	if (!FLAC__metadata_get_streaminfo(narrow_path_fs, &streaminfo) ||
	    streaminfo.data.stream_info.sample_rate == 0) {
		return nullptr;
	}

	const unsigned sample_rate = streaminfo.data.stream_info.sample_rate;
	const FLAC__uint64 total_samples = streaminfo.data.stream_info.total_samples;

	std::forward_list<DetachedSong> songs;
	auto tail = songs.before_begin();

	const auto &c = cuesheet->data.cue_sheet;
	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));
}

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

const struct playlist_plugin flac_playlist_plugin = {
	"flac",

	nullptr,
	nullptr,
	flac_playlist_open_uri,
	nullptr,

	nullptr,
	flac_playlist_suffixes,
	nullptr,
};