summaryrefslogtreecommitdiff
path: root/src/audio/fatfs_audio_input.cpp
blob: 29d323907743d09a8a32f214b40eef2648ec29ad (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
/*
 * Copyright 2023 jacqueline <me@jacqueline.id.au>
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */

#include "fatfs_audio_input.hpp"

#include <algorithm>
#include <climits>
#include <cstddef>
#include <cstdint>
#include <functional>
#include <future>
#include <memory>
#include <mutex>
#include <string>
#include <variant>

#include "esp_heap_caps.h"
#include "esp_log.h"
#include "ff.h"
#include "freertos/portmacro.h"
#include "freertos/projdefs.h"
#include "readahead_source.hpp"
#include "span.hpp"

#include "audio_events.hpp"
#include "audio_fsm.hpp"
#include "audio_source.hpp"
#include "codec.hpp"
#include "event_queue.hpp"
#include "fatfs_source.hpp"
#include "future_fetcher.hpp"
#include "spi.hpp"
#include "tag_parser.hpp"
#include "tasks.hpp"
#include "track.hpp"
#include "types.hpp"

[[maybe_unused]] static const char* kTag = "SRC";

namespace audio {

FatfsAudioInput::FatfsAudioInput(database::ITagParser& tag_parser,
                                 tasks::WorkerPool& bg_worker)
    : IAudioSource(),
      tag_parser_(tag_parser),
      bg_worker_(bg_worker),
      new_stream_mutex_(),
      new_stream_(),
      has_new_stream_(false) {}

FatfsAudioInput::~FatfsAudioInput() {}

auto FatfsAudioInput::SetPath(std::optional<std::string> path) -> void {
  if (path) {
    SetPath(*path);
  } else {
    SetPath();
  }
}

auto FatfsAudioInput::SetPath(const std::string& path,uint32_t offset) -> void {
  std::lock_guard<std::mutex> guard{new_stream_mutex_};
  if (OpenFile(path, offset)) {
    has_new_stream_ = true;
    has_new_stream_.notify_one();
  }
}

auto FatfsAudioInput::SetPath() -> void {
  std::lock_guard<std::mutex> guard{new_stream_mutex_};
  new_stream_.reset();
  has_new_stream_ = true;
  has_new_stream_.notify_one();
}

auto FatfsAudioInput::HasNewStream() -> bool {
  return has_new_stream_;
}

auto FatfsAudioInput::NextStream() -> std::shared_ptr<TaggedStream> {
  while (true) {
    has_new_stream_.wait(false);

    {
      std::lock_guard<std::mutex> guard{new_stream_mutex_};
      if (!has_new_stream_.exchange(false)) {
        // If the new stream went away, then we need to go back to waiting.
        continue;
      }

      if (new_stream_ == nullptr) {
        continue;
      }

      auto stream = new_stream_;
      new_stream_ = nullptr;
      return stream;
    }
  }
}

auto FatfsAudioInput::OpenFile(const std::string& path,uint32_t offset) -> bool {
  ESP_LOGI(kTag, "opening file %s", path.c_str());

  auto tags = tag_parser_.ReadAndParseTags(path);
  if (!tags) {
    ESP_LOGE(kTag, "failed to read tags");
    return false;
  }
  if (!tags->title()) {
    tags->title(path);
  }

  auto stream_type = ContainerToStreamType(tags->encoding());
  if (!stream_type.has_value()) {
    ESP_LOGE(kTag, "couldn't match container to stream");
    return false;
  }

  std::unique_ptr<FIL> file = std::make_unique<FIL>();
  FRESULT res;

  {
    auto lock = drivers::acquire_spi();
    res = f_open(file.get(), path.c_str(), FA_READ);
  }

  if (res != FR_OK) {
    ESP_LOGE(kTag, "failed to open file! res: %i", res);
    return false;
  }

  auto source =
      std::make_unique<FatfsSource>(stream_type.value(), std::move(file));
  new_stream_.reset(new TaggedStream(tags, std::move(source), path, offset));
  return true;
}

auto FatfsAudioInput::ContainerToStreamType(database::Container enc)
    -> std::optional<codecs::StreamType> {
  switch (enc) {
    case database::Container::kMp3:
      return codecs::StreamType::kMp3;
    case database::Container::kWav:
      return codecs::StreamType::kWav;
    case database::Container::kOgg:
      return codecs::StreamType::kVorbis;
    case database::Container::kFlac:
      return codecs::StreamType::kFlac;
    case database::Container::kOpus:
      return codecs::StreamType::kOpus;
    case database::Container::kUnsupported:
    default:
      return {};
  }
}

}  // namespace audio