summaryrefslogtreecommitdiff
path: root/src/audio/fatfs_audio_input.cpp
blob: 14176eae2174cade842d0b9cc2ead49d235c5f4f (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
#include "fatfs_audio_input.hpp"

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

#include "esp_heap_caps.h"
#include "freertos/portmacro.h"

#include "audio_element.hpp"
#include "chunk.hpp"
#include "stream_buffer.hpp"
#include "stream_event.hpp"
#include "stream_info.hpp"
#include "stream_message.hpp"

static const char* kTag = "SRC";

namespace audio {

// 32KiB to match the minimum himen region size.
static const std::size_t kChunkSize = 32 * 1024;

FatfsAudioInput::FatfsAudioInput(std::shared_ptr<drivers::SdStorage> storage)
    : IAudioElement(),
      storage_(storage),
      current_file_(),
      is_file_open_(false) {}

FatfsAudioInput::~FatfsAudioInput() {}

auto FatfsAudioInput::HasUnprocessedInput() -> bool {
  return is_file_open_;
}

auto FatfsAudioInput::ProcessStreamInfo(const StreamInfo& info)
    -> cpp::result<void, AudioProcessingError> {
  if (is_file_open_) {
    f_close(&current_file_);
    is_file_open_ = false;
  }

  if (!info.path) {
    return cpp::fail(UNSUPPORTED_STREAM);
  }
  ESP_LOGI(kTag, "opening file %s", info.path->c_str());
  std::string path = *info.path;
  FRESULT res = f_open(&current_file_, path.c_str(), FA_READ);
  if (res != FR_OK) {
    ESP_LOGE(kTag, "failed to open file! res: %i", res);
    return cpp::fail(IO_ERROR);
  }

  is_file_open_ = true;

  StreamInfo new_info(info);
  new_info.chunk_size = kChunkSize;
  ESP_LOGI(kTag, "chunk size: %u bytes", kChunkSize);

  auto event = StreamEvent::CreateStreamInfo(input_events_, new_info);
  SendOrBufferEvent(std::unique_ptr<StreamEvent>(event));

  return {};
}

auto FatfsAudioInput::ProcessChunk(const cpp::span<std::byte>& chunk)
    -> cpp::result<size_t, AudioProcessingError> {
  return cpp::fail(UNSUPPORTED_STREAM);
}

auto FatfsAudioInput::Process() -> cpp::result<void, AudioProcessingError> {
  if (is_file_open_) {
    auto dest_event = std::unique_ptr<StreamEvent>(
        StreamEvent::CreateChunkData(input_events_, kChunkSize));
    UINT bytes_read = 0;

    ESP_LOGI(kTag, "reading from file");
    FRESULT result = f_read(&current_file_, dest_event->chunk_data.raw_bytes,
                            kChunkSize, &bytes_read);
    if (result != FR_OK) {
      ESP_LOGE(kTag, "file I/O error %d", result);
      return cpp::fail(IO_ERROR);
    }

    ESP_LOGI(kTag, "sending file data (%u bytes)", bytes_read);
    dest_event->chunk_data.bytes =
        dest_event->chunk_data.bytes.first(bytes_read);
    SendOrBufferEvent(std::move(dest_event));

    if (f_eof(&current_file_)) {
      f_close(&current_file_);
      is_file_open_ = false;
    }
  }
  return {};
}

}  // namespace audio