summaryrefslogtreecommitdiff
path: root/src/tangara/audio/playlist.hpp
blob: 1e05e9c449fd4b1165f6b547178c61c78511c9b7 (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

/*
 * Copyright 2024 ailurux <ailuruxx@gmail.com>
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */

#pragma once

#include <string>
#include <variant>

#include "ff.h"

#include "database/database.hpp"
#include "database/track.hpp"

namespace audio {

/*
 * Owns and manages a playlist file.
 * Each line in the playlist file is the absolute filepath of the track to play.
 * In order to avoid mapping to byte offsets, each line must contain only a
 * filepath (ie, no comments are supported). This limitation may be removed
 * later if benchmarks show that the file can be quickly scanned from 'bookmark'
 * offsets. This is a subset of the m3u format and ideally will be
 * import/exportable to and from this format, to better support playlists from
 * beets import and other music management software.
 */
class Playlist {
 public:
  Playlist(const std::string& playlistFilepath);
  virtual ~Playlist();
  using Item =
      std::variant<database::TrackId, database::TrackIterator, std::string>;
  virtual auto open() -> bool;

  auto filepath() const -> std::string;
  auto currentPosition() const -> size_t;
  auto size() const -> size_t;
  auto value() const -> std::string;
  auto atEnd() const -> bool;

  auto next() -> void;
  auto prev() -> void;
  auto skipTo(size_t position) -> void;

  auto serialiseCache() -> bool;
  auto deserialiseCache() -> bool;
  auto close() -> void;

 protected:
  const std::string filepath_;

  mutable std::mutex mutex_;
  size_t total_size_;
  ssize_t pos_;

  FIL file_;
  bool file_open_;
  bool file_error_;

  std::string current_value_;

  /* List of offsets determined by sample size */
  std::pmr::vector<FSIZE_t> offset_cache_;

  /*
   * How many tracks per offset saved (ie, a value of 100 means every 100 tracks
   * the file offset is saved) This speeds up searches, especially in the case
   * of shuffling a lot of tracks.
   */
  const uint32_t sample_size_;

 protected:
  auto skipToLocked(size_t position) -> void;
  auto countItems() -> void;
  auto advanceBy(ssize_t amt) -> bool;
  auto nextItem(std::span<TCHAR>) -> std::optional<std::string_view>;
  auto skipToWithoutCache(size_t position) -> void;
};

class MutablePlaylist : public Playlist {
 public:
  MutablePlaylist(const std::string& playlistFilepath);
  auto open() -> bool override;

  auto clear() -> bool;
  auto append(Item i) -> void;

 private:
  auto clearLocked() -> bool;
};

}  // namespace audio