summaryrefslogtreecommitdiff
path: root/src/audio/include/fatfs_audio_input.hpp
blob: 21c729beb6c9611988dd47d4e7129e0111db06d0 (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
#pragma once

#include <cstdint>
#include <memory>
#include <string>

#include "chunk.hpp"
#include "freertos/FreeRTOS.h"

#include "freertos/message_buffer.h"
#include "freertos/queue.h"
#include "span.hpp"

#include "audio_element.hpp"
#include "storage.hpp"
#include "stream_buffer.hpp"

namespace audio {

class FatfsAudioInput : public IAudioElement {
 public:
  explicit FatfsAudioInput(std::shared_ptr<drivers::SdStorage> storage);
  ~FatfsAudioInput();

  auto ProcessStreamInfo(const StreamInfo& info)
      -> cpp::result<void, AudioProcessingError> override;
  auto ProcessChunk(const cpp::span<std::byte>& chunk)
      -> cpp::result<std::size_t, AudioProcessingError> override;
  auto ProcessIdle() -> cpp::result<void, AudioProcessingError> override;

  auto SendChunk(cpp::span<std::byte> dest) -> size_t;

  FatfsAudioInput(const FatfsAudioInput&) = delete;
  FatfsAudioInput& operator=(const FatfsAudioInput&) = delete;

 private:
  auto GetRingBufferDistance() const -> size_t;

  std::shared_ptr<drivers::SdStorage> storage_;

  std::byte* raw_file_buffer_;
  cpp::span<std::byte> file_buffer_;
  cpp::span<std::byte>::iterator file_buffer_read_pos_;
  cpp::span<std::byte>::iterator pending_read_pos_;
  cpp::span<std::byte>::iterator file_buffer_write_pos_;

  FIL current_file_;
  bool is_file_open_;
};

}  // namespace audio