diff options
| author | jacqueline <me@jacqueline.id.au> | 2023-08-10 15:33:00 +1000 |
|---|---|---|
| committer | jacqueline <me@jacqueline.id.au> | 2023-08-10 15:33:00 +1000 |
| commit | d8fc77101dcf80a3643a00b3446dca1e390ce997 (patch) | |
| tree | 9e03881f3857c7b4c6a0b6e3a062947daecc69d1 /src/audio/fatfs_source.cpp | |
| parent | 67caeb6e3cda44205ba8fe783274b20dc7ea216e (diff) | |
| download | tangara-fw-d8fc77101dcf80a3643a00b3446dca1e390ce997.tar.gz | |
Give codecs complete control of their input files
Diffstat (limited to 'src/audio/fatfs_source.cpp')
| -rw-r--r-- | src/audio/fatfs_source.cpp | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/src/audio/fatfs_source.cpp b/src/audio/fatfs_source.cpp new file mode 100644 index 00000000..6a9aea47 --- /dev/null +++ b/src/audio/fatfs_source.cpp @@ -0,0 +1,70 @@ +/* + * Copyright 2023 jacqueline <me@jacqueline.id.au> + * + * SPDX-License-Identifier: GPL-3.0-only + */ + +#include "fatfs_source.hpp" +#include <sys/_stdint.h> + +#include <cstddef> +#include <cstdint> +#include <memory> + +#include "esp_log.h" +#include "ff.h" + +#include "audio_source.hpp" +#include "codec.hpp" +#include "types.hpp" + +namespace audio { + +static constexpr char kTag[] = "fatfs_src"; + +FatfsSource::FatfsSource(codecs::StreamType t, std::unique_ptr<FIL> file) + : IStream(t), file_(std::move(file)) {} + +FatfsSource::~FatfsSource() { + f_close(file_.get()); +} + +auto FatfsSource::Read(cpp::span<std::byte> dest) -> ssize_t { + if (f_eof(file_.get())) { + ESP_LOGI(kTag, "read from empty file"); + return 0; + } + UINT bytes_read = 0; + FRESULT res = f_read(file_.get(), dest.data(), dest.size(), &bytes_read); + if (res != FR_OK) { + ESP_LOGE(kTag, "error reading from file"); + return -1; + } + ESP_LOGI(kTag, "read %u bytes into %p (%u)", bytes_read, dest.data(), + dest.size_bytes()); + return bytes_read; +} + +auto FatfsSource::CanSeek() -> bool { + return true; +} + +auto FatfsSource::SeekTo(int64_t destination, SeekFrom from) -> void { + ESP_LOGI(kTag, "seeking to %llu", destination); + switch (from) { + case SeekFrom::kStartOfStream: + f_lseek(file_.get(), destination); + break; + case SeekFrom::kEndOfStream: + f_lseek(file_.get(), f_size(file_.get()) + destination); + break; + case SeekFrom::kCurrentPosition: + f_lseek(file_.get(), f_tell(file_.get()) + destination); + break; + } +} + +auto FatfsSource::CurrentPosition() -> int64_t { + return f_tell(file_.get()); +} +} // namespace audio |
