summaryrefslogtreecommitdiff
path: root/src/audio/track_queue.cpp
blob: c1187107601ff02b2cf735dd72b38973c3e013fb (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
 * Copyright 2023 jacqueline <me@jacqueline.id.au>
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */

#include "track_queue.hpp"
#include <stdint.h>

#include <algorithm>
#include <memory>
#include <mutex>
#include <optional>
#include <variant>

#include "audio_events.hpp"
#include "audio_fsm.hpp"
#include "cppbor.h"
#include "cppbor_parse.h"
#include "database.hpp"
#include "event_queue.hpp"
#include "memory_resource.hpp"
#include "tasks.hpp"
#include "track.hpp"
#include "ui_fsm.hpp"

namespace audio {

[[maybe_unused]] static constexpr char kTag[] = "tracks";

auto notifyChanged(bool current_changed) -> void {
  QueueUpdate ev{.current_changed = current_changed};
  events::Ui().Dispatch(ev);
  events::Audio().Dispatch(ev);
}

TrackQueue::TrackQueue(tasks::Worker& bg_worker)
    : mutex_(),
      bg_worker_(bg_worker),
      pos_(0),
      tracks_(&memory::kSpiRamResource) {}

auto TrackQueue::current() const -> std::optional<database::TrackId> {
  const std::shared_lock<std::shared_mutex> lock(mutex_);
  if (pos_ >= tracks_.size()) {
    return {};
  }
  return tracks_[pos_];
}

auto TrackQueue::peekNext(std::size_t limit) const
    -> std::vector<database::TrackId> {
  const std::shared_lock<std::shared_mutex> lock(mutex_);
  std::vector<database::TrackId> out;
  for (size_t i = pos_ + 1; i < pos_ + limit + 1 && i < tracks_.size(); i++) {
    out.push_back(i);
  }
  return out;
}

auto TrackQueue::peekPrevious(std::size_t limit) const
    -> std::vector<database::TrackId> {
  const std::shared_lock<std::shared_mutex> lock(mutex_);
  std::vector<database::TrackId> out;
  for (size_t i = pos_ - 1; i < pos_ - limit - 1 && i >= tracks_.size(); i--) {
    out.push_back(i);
  }
  return out;
}

auto TrackQueue::currentPosition() const -> size_t {
  const std::shared_lock<std::shared_mutex> lock(mutex_);
  return pos_;
}

auto TrackQueue::totalSize() const -> size_t {
  const std::shared_lock<std::shared_mutex> lock(mutex_);
  return tracks_.size();
}

auto TrackQueue::insert(Item i) -> void {
  bool current_changed = pos_ == tracks_.size();
  if (std::holds_alternative<database::TrackId>(i)) {
    const std::unique_lock<std::shared_mutex> lock(mutex_);
    tracks_.push_back(std::get<database::TrackId>(i));
    notifyChanged(current_changed);
  } else if (std::holds_alternative<database::TrackIterator>(i)) {
    bg_worker_.Dispatch<void>([=, this]() {
      database::TrackIterator it = std::get<database::TrackIterator>(i);
      size_t working_pos = pos_;
      while (true) {
        auto next = *it;
        if (!next) {
          break;
        }
        const std::unique_lock<std::shared_mutex> lock(mutex_);
        tracks_.insert(tracks_.begin() + working_pos, *next);
        working_pos++;
        it++;
      }
      notifyChanged(current_changed);
    });
  }
}

auto TrackQueue::append(Item i) -> void {
  bool current_changed = pos_ == tracks_.size();
  if (std::holds_alternative<database::TrackId>(i)) {
    const std::unique_lock<std::shared_mutex> lock(mutex_);
    tracks_.push_back(std::get<database::TrackId>(i));
    notifyChanged(current_changed);
  } else if (std::holds_alternative<database::TrackIterator>(i)) {
    bg_worker_.Dispatch<void>([=, this]() {
      database::TrackIterator it = std::get<database::TrackIterator>(i);
      while (true) {
        auto next = *it;
        if (!next) {
          break;
        }
        const std::unique_lock<std::shared_mutex> lock(mutex_);
        tracks_.push_back(*next);
        it++;
      }
      notifyChanged(current_changed);
    });
  }
}

auto TrackQueue::next() -> void {
  const std::unique_lock<std::shared_mutex> lock(mutex_);
  pos_ = std::min<size_t>(pos_ + 1, tracks_.size());

  notifyChanged(true);
}

auto TrackQueue::previous() -> void {
  const std::unique_lock<std::shared_mutex> lock(mutex_);
  if (pos_ > 0) {
    pos_--;
  }

  notifyChanged(true);
}

auto TrackQueue::skipTo(database::TrackId id) -> void {
  const std::unique_lock<std::shared_mutex> lock(mutex_);
  for (size_t i = pos_; i < tracks_.size(); i++) {
    if (tracks_[i] == id) {
      pos_ = i;
    }
  }

  notifyChanged(true);
}

auto TrackQueue::clear() -> void {
  const std::unique_lock<std::shared_mutex> lock(mutex_);
  pos_ = 0;
  tracks_.clear();

  notifyChanged(true);
}

}  // namespace audio