Merge branch 'main' into file-browser
This commit is contained in:
@@ -1,9 +0,0 @@
|
||||
# Copyright 2023 jacqueline <me@jacqueline.id.au>
|
||||
#
|
||||
# SPDX-License-Identifier: GPL-3.0-only
|
||||
|
||||
idf_component_register(
|
||||
SRCS "app_console.cpp"
|
||||
INCLUDE_DIRS "include"
|
||||
REQUIRES "dev_console" "events" "database")
|
||||
target_compile_options(${COMPONENT_LIB} PRIVATE ${EXTRA_WARNINGS})
|
||||
@@ -1,14 +0,0 @@
|
||||
# Copyright 2023 jacqueline <me@jacqueline.id.au>
|
||||
#
|
||||
# SPDX-License-Identifier: GPL-3.0-only
|
||||
|
||||
idf_component_register(
|
||||
SRCS "audio_decoder.cpp" "fatfs_audio_input.cpp" "i2s_audio_output.cpp"
|
||||
"track_queue.cpp" "audio_fsm.cpp" "audio_converter.cpp" "resample.cpp"
|
||||
"fatfs_source.cpp" "bt_audio_output.cpp" "readahead_source.cpp"
|
||||
"audio_source.cpp"
|
||||
INCLUDE_DIRS "include"
|
||||
REQUIRES "codecs" "drivers" "cbor" "result" "tasks" "span" "memory" "tinyfsm"
|
||||
"database" "system_fsm" "speexdsp" "millershuffle" "libcppbor")
|
||||
|
||||
target_compile_options(${COMPONENT_LIB} PRIVATE ${EXTRA_WARNINGS})
|
||||
@@ -1,148 +0,0 @@
|
||||
/*
|
||||
* Copyright 2023 jacqueline <me@jacqueline.id.au>
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-only
|
||||
*/
|
||||
|
||||
#include "audio_decoder.hpp"
|
||||
#include <stdint.h>
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <deque>
|
||||
#include <memory>
|
||||
#include <variant>
|
||||
|
||||
#include "cbor.h"
|
||||
#include "esp_err.h"
|
||||
#include "esp_heap_caps.h"
|
||||
#include "esp_log.h"
|
||||
#include "freertos/portmacro.h"
|
||||
#include "freertos/projdefs.h"
|
||||
#include "freertos/queue.h"
|
||||
#include "freertos/ringbuf.h"
|
||||
#include "i2s_dac.hpp"
|
||||
#include "span.hpp"
|
||||
|
||||
#include "audio_converter.hpp"
|
||||
#include "audio_events.hpp"
|
||||
#include "audio_fsm.hpp"
|
||||
#include "audio_sink.hpp"
|
||||
#include "audio_source.hpp"
|
||||
#include "codec.hpp"
|
||||
#include "event_queue.hpp"
|
||||
#include "fatfs_audio_input.hpp"
|
||||
#include "sample.hpp"
|
||||
#include "tasks.hpp"
|
||||
#include "track.hpp"
|
||||
#include "types.hpp"
|
||||
#include "ui_fsm.hpp"
|
||||
|
||||
namespace audio {
|
||||
|
||||
[[maybe_unused]] static const char* kTag = "audio_dec";
|
||||
|
||||
static constexpr std::size_t kCodecBufferLength =
|
||||
drivers::kI2SBufferLengthFrames * sizeof(sample::Sample);
|
||||
|
||||
auto Decoder::Start(std::shared_ptr<IAudioSource> source,
|
||||
std::shared_ptr<SampleConverter> sink) -> Decoder* {
|
||||
Decoder* task = new Decoder(source, sink);
|
||||
tasks::StartPersistent<tasks::Type::kAudioDecoder>([=]() { task->Main(); });
|
||||
return task;
|
||||
}
|
||||
|
||||
Decoder::Decoder(std::shared_ptr<IAudioSource> source,
|
||||
std::shared_ptr<SampleConverter> mixer)
|
||||
: source_(source), converter_(mixer), codec_(), current_format_() {
|
||||
ESP_LOGI(kTag, "allocating codec buffer, %u KiB", kCodecBufferLength / 1024);
|
||||
codec_buffer_ = {
|
||||
reinterpret_cast<sample::Sample*>(heap_caps_calloc(
|
||||
kCodecBufferLength, sizeof(sample::Sample), MALLOC_CAP_DMA)),
|
||||
kCodecBufferLength};
|
||||
}
|
||||
|
||||
void Decoder::Main() {
|
||||
for (;;) {
|
||||
if (source_->HasNewStream() || !stream_) {
|
||||
std::shared_ptr<TaggedStream> new_stream = source_->NextStream();
|
||||
if (new_stream && BeginDecoding(new_stream)) {
|
||||
stream_ = new_stream;
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (ContinueDecoding()) {
|
||||
stream_.reset();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
auto Decoder::BeginDecoding(std::shared_ptr<TaggedStream> stream) -> bool {
|
||||
// Ensure any previous codec is freed before creating a new one.
|
||||
codec_.reset();
|
||||
codec_.reset(codecs::CreateCodecForType(stream->type()).value_or(nullptr));
|
||||
if (!codec_) {
|
||||
ESP_LOGE(kTag, "no codec found for stream");
|
||||
return false;
|
||||
}
|
||||
|
||||
auto open_res = codec_->OpenStream(stream, stream->Offset());
|
||||
if (open_res.has_error()) {
|
||||
ESP_LOGE(kTag, "codec failed to start: %s",
|
||||
codecs::ICodec::ErrorString(open_res.error()).c_str());
|
||||
return false;
|
||||
}
|
||||
stream->SetPreambleFinished();
|
||||
current_sink_format_ = IAudioOutput::Format{
|
||||
.sample_rate = open_res->sample_rate_hz,
|
||||
.num_channels = open_res->num_channels,
|
||||
.bits_per_sample = 16,
|
||||
};
|
||||
|
||||
std::optional<uint32_t> duration;
|
||||
if (open_res->total_samples) {
|
||||
duration = open_res->total_samples.value() / open_res->num_channels /
|
||||
open_res->sample_rate_hz;
|
||||
}
|
||||
|
||||
converter_->beginStream(std::make_shared<TrackInfo>(TrackInfo{
|
||||
.tags = stream->tags(),
|
||||
.uri = stream->Filepath(),
|
||||
.duration = duration,
|
||||
.start_offset = stream->Offset(),
|
||||
.bitrate_kbps = open_res->sample_rate_hz,
|
||||
.encoding = stream->type(),
|
||||
.format = *current_sink_format_,
|
||||
}));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
auto Decoder::ContinueDecoding() -> bool {
|
||||
auto res = codec_->DecodeTo(codec_buffer_);
|
||||
if (res.has_error()) {
|
||||
converter_->endStream();
|
||||
return true;
|
||||
}
|
||||
|
||||
if (res->samples_written > 0) {
|
||||
converter_->continueStream(codec_buffer_.first(res->samples_written));
|
||||
}
|
||||
|
||||
if (res->is_stream_finished) {
|
||||
converter_->endStream();
|
||||
codec_.reset();
|
||||
}
|
||||
|
||||
return res->is_stream_finished;
|
||||
}
|
||||
|
||||
} // namespace audio
|
||||
@@ -1,161 +0,0 @@
|
||||
/*
|
||||
* 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
|
||||
@@ -1,56 +0,0 @@
|
||||
/*
|
||||
* Copyright 2023 jacqueline <me@jacqueline.id.au>
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-only
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
|
||||
#include "audio_converter.hpp"
|
||||
#include "audio_events.hpp"
|
||||
#include "audio_sink.hpp"
|
||||
#include "audio_source.hpp"
|
||||
#include "codec.hpp"
|
||||
#include "track.hpp"
|
||||
#include "types.hpp"
|
||||
|
||||
namespace audio {
|
||||
|
||||
/*
|
||||
* Handle to a persistent task that takes bytes from the given source, decodes
|
||||
* them into sample::Sample (normalised to 16 bit signed PCM), and then
|
||||
* forwards the resulting stream to the given converter.
|
||||
*/
|
||||
class Decoder {
|
||||
public:
|
||||
static auto Start(std::shared_ptr<IAudioSource> source,
|
||||
std::shared_ptr<SampleConverter> converter) -> Decoder*;
|
||||
|
||||
auto Main() -> void;
|
||||
|
||||
Decoder(const Decoder&) = delete;
|
||||
Decoder& operator=(const Decoder&) = delete;
|
||||
|
||||
private:
|
||||
Decoder(std::shared_ptr<IAudioSource> source,
|
||||
std::shared_ptr<SampleConverter> converter);
|
||||
|
||||
auto BeginDecoding(std::shared_ptr<TaggedStream>) -> bool;
|
||||
auto ContinueDecoding() -> bool;
|
||||
|
||||
std::shared_ptr<IAudioSource> source_;
|
||||
std::shared_ptr<SampleConverter> converter_;
|
||||
|
||||
std::shared_ptr<codecs::IStream> stream_;
|
||||
std::unique_ptr<codecs::ICodec> codec_;
|
||||
|
||||
std::optional<codecs::ICodec::OutputFormat> current_format_;
|
||||
std::optional<IAudioOutput::Format> current_sink_format_;
|
||||
|
||||
cpp::span<sample::Sample> codec_buffer_;
|
||||
};
|
||||
|
||||
} // namespace audio
|
||||
@@ -1,66 +0,0 @@
|
||||
/*
|
||||
* Copyright 2023 jacqueline <me@jacqueline.id.au>
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-only
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <future>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "ff.h"
|
||||
#include "freertos/portmacro.h"
|
||||
|
||||
#include "audio_source.hpp"
|
||||
#include "codec.hpp"
|
||||
#include "future_fetcher.hpp"
|
||||
#include "tag_parser.hpp"
|
||||
#include "tasks.hpp"
|
||||
#include "types.hpp"
|
||||
|
||||
namespace audio {
|
||||
|
||||
/*
|
||||
* Audio source that fetches data from a FatFs (or exfat i guess) filesystem.
|
||||
*
|
||||
* All public methods are safe to call from any task.
|
||||
*/
|
||||
class FatfsAudioInput : public IAudioSource {
|
||||
public:
|
||||
explicit FatfsAudioInput(database::ITagParser&, tasks::WorkerPool&);
|
||||
~FatfsAudioInput();
|
||||
|
||||
/*
|
||||
* Immediately cease reading any current source, and begin reading from the
|
||||
* given file path.
|
||||
*/
|
||||
auto SetPath(std::optional<std::string>) -> void;
|
||||
auto SetPath(const std::string&,uint32_t offset = 0) -> void;
|
||||
auto SetPath() -> void;
|
||||
|
||||
auto HasNewStream() -> bool override;
|
||||
auto NextStream() -> std::shared_ptr<TaggedStream> override;
|
||||
|
||||
FatfsAudioInput(const FatfsAudioInput&) = delete;
|
||||
FatfsAudioInput& operator=(const FatfsAudioInput&) = delete;
|
||||
|
||||
private:
|
||||
auto OpenFile(const std::string& path,uint32_t offset) -> bool;
|
||||
|
||||
auto ContainerToStreamType(database::Container)
|
||||
-> std::optional<codecs::StreamType>;
|
||||
|
||||
database::ITagParser& tag_parser_;
|
||||
tasks::WorkerPool& bg_worker_;
|
||||
|
||||
std::mutex new_stream_mutex_;
|
||||
std::shared_ptr<TaggedStream> new_stream_;
|
||||
|
||||
std::atomic<bool> has_new_stream_;
|
||||
};
|
||||
|
||||
} // namespace audio
|
||||
@@ -1,10 +0,0 @@
|
||||
# Copyright 2023 jacqueline <me@jacqueline.id.au>
|
||||
#
|
||||
# SPDX-License-Identifier: GPL-3.0-only
|
||||
|
||||
idf_component_register(
|
||||
SRCS "battery.cpp"
|
||||
INCLUDE_DIRS "include"
|
||||
REQUIRES "drivers" "events")
|
||||
|
||||
target_compile_options(${COMPONENT_LIB} PRIVATE ${EXTRA_WARNINGS})
|
||||
@@ -6,7 +6,7 @@ idf_component_register(
|
||||
SRCS "dr_flac.cpp" "codec.cpp" "mad.cpp" "opus.cpp" "vorbis.cpp"
|
||||
"source_buffer.cpp" "sample.cpp" "wav.cpp"
|
||||
INCLUDE_DIRS "include"
|
||||
REQUIRES "result" "span" "libmad" "drflac" "tremor" "opusfile" "memory" "util"
|
||||
REQUIRES "result" "libmad" "drflac" "tremor" "opusfile" "memory" "util"
|
||||
"komihash")
|
||||
|
||||
target_compile_options("${COMPONENT_LIB}" PRIVATE ${EXTRA_WARNINGS})
|
||||
|
||||
@@ -9,8 +9,8 @@
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
|
||||
#include "mad.hpp"
|
||||
#include "dr_flac.hpp"
|
||||
#include "mad.hpp"
|
||||
#include "opus.hpp"
|
||||
#include "types.hpp"
|
||||
#include "vorbis.hpp"
|
||||
|
||||
@@ -100,7 +100,7 @@ auto DrFlacDecoder::OpenStream(std::shared_ptr<IStream> input, uint32_t offset)
|
||||
return format;
|
||||
}
|
||||
|
||||
auto DrFlacDecoder::DecodeTo(cpp::span<sample::Sample> output)
|
||||
auto DrFlacDecoder::DecodeTo(std::span<sample::Sample> output)
|
||||
-> cpp::result<OutputInfo, Error> {
|
||||
size_t frames_to_read = output.size() / flac_->channels / 2;
|
||||
|
||||
|
||||
@@ -6,19 +6,16 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <sys/_stdint.h>
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <span>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "result.hpp"
|
||||
#include "sample.hpp"
|
||||
#include "span.hpp"
|
||||
#include "types.hpp"
|
||||
|
||||
#include "memory_resource.hpp"
|
||||
@@ -35,7 +32,7 @@ class IStream {
|
||||
|
||||
auto type() -> StreamType { return t_; }
|
||||
|
||||
virtual auto Read(cpp::span<std::byte> dest) -> ssize_t = 0;
|
||||
virtual auto Read(std::span<std::byte> dest) -> ssize_t = 0;
|
||||
|
||||
virtual auto CanSeek() -> bool = 0;
|
||||
|
||||
@@ -54,7 +51,7 @@ class IStream {
|
||||
/*
|
||||
* Called by codecs to indicate that they've finished parsing any header data
|
||||
* within this stream, and are about to begin decoding.
|
||||
*
|
||||
*
|
||||
* Currently used as a hint to the readahead stream to begin prefetching file
|
||||
* data.
|
||||
*/
|
||||
@@ -117,7 +114,7 @@ class ICodec {
|
||||
* Decodes metadata or headers from the given input stream, and returns the
|
||||
* format for the samples that will be decoded from it.
|
||||
*/
|
||||
virtual auto OpenStream(std::shared_ptr<IStream> input,uint32_t offset)
|
||||
virtual auto OpenStream(std::shared_ptr<IStream> input, uint32_t offset)
|
||||
-> cpp::result<OutputFormat, Error> = 0;
|
||||
|
||||
struct OutputInfo {
|
||||
@@ -128,7 +125,7 @@ class ICodec {
|
||||
/*
|
||||
* Writes PCM samples to the given output buffer.
|
||||
*/
|
||||
virtual auto DecodeTo(cpp::span<sample::Sample> destination)
|
||||
virtual auto DecodeTo(std::span<sample::Sample> destination)
|
||||
-> cpp::result<OutputInfo, Error> = 0;
|
||||
};
|
||||
|
||||
|
||||
@@ -10,13 +10,13 @@
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <span>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "dr_flac.h"
|
||||
#include "sample.hpp"
|
||||
#include "source_buffer.hpp"
|
||||
#include "span.hpp"
|
||||
|
||||
#include "codec.hpp"
|
||||
|
||||
@@ -27,10 +27,10 @@ class DrFlacDecoder : public ICodec {
|
||||
DrFlacDecoder();
|
||||
~DrFlacDecoder();
|
||||
|
||||
auto OpenStream(std::shared_ptr<IStream> input,uint32_t offset)
|
||||
auto OpenStream(std::shared_ptr<IStream> input, uint32_t offset)
|
||||
-> cpp::result<OutputFormat, Error> override;
|
||||
|
||||
auto DecodeTo(cpp::span<sample::Sample> destination)
|
||||
auto DecodeTo(std::span<sample::Sample> destination)
|
||||
-> cpp::result<OutputInfo, Error> override;
|
||||
|
||||
DrFlacDecoder(const DrFlacDecoder&) = delete;
|
||||
@@ -38,7 +38,7 @@ class DrFlacDecoder : public ICodec {
|
||||
|
||||
private:
|
||||
std::shared_ptr<IStream> input_;
|
||||
drflac *flac_;
|
||||
drflac* flac_;
|
||||
};
|
||||
|
||||
} // namespace codecs
|
||||
|
||||
@@ -11,11 +11,11 @@
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <span>
|
||||
|
||||
#include "mad.h"
|
||||
#include "sample.hpp"
|
||||
#include "source_buffer.hpp"
|
||||
#include "span.hpp"
|
||||
|
||||
#include "codec.hpp"
|
||||
|
||||
@@ -29,7 +29,7 @@ class MadMp3Decoder : public ICodec {
|
||||
auto OpenStream(std::shared_ptr<IStream> input,uint32_t offset)
|
||||
-> cpp::result<OutputFormat, Error> override;
|
||||
|
||||
auto DecodeTo(cpp::span<sample::Sample> destination)
|
||||
auto DecodeTo(std::span<sample::Sample> destination)
|
||||
-> cpp::result<OutputInfo, Error> override;
|
||||
|
||||
MadMp3Decoder(const MadMp3Decoder&) = delete;
|
||||
|
||||
@@ -10,12 +10,12 @@
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <span>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "opusfile.h"
|
||||
#include "sample.hpp"
|
||||
#include "span.hpp"
|
||||
|
||||
#include "codec.hpp"
|
||||
|
||||
@@ -26,10 +26,10 @@ class XiphOpusDecoder : public ICodec {
|
||||
XiphOpusDecoder();
|
||||
~XiphOpusDecoder();
|
||||
|
||||
auto OpenStream(std::shared_ptr<IStream> input,uint32_t offset)
|
||||
auto OpenStream(std::shared_ptr<IStream> input, uint32_t offset)
|
||||
-> cpp::result<OutputFormat, Error> override;
|
||||
|
||||
auto DecodeTo(cpp::span<sample::Sample> destination)
|
||||
auto DecodeTo(std::span<sample::Sample> destination)
|
||||
-> cpp::result<OutputInfo, Error> override;
|
||||
|
||||
XiphOpusDecoder(const XiphOpusDecoder&) = delete;
|
||||
|
||||
@@ -9,8 +9,7 @@
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
|
||||
#include "span.hpp"
|
||||
#include <span>
|
||||
|
||||
#include "codec.hpp"
|
||||
|
||||
@@ -22,15 +21,15 @@ class SourceBuffer {
|
||||
~SourceBuffer();
|
||||
|
||||
auto Refill(IStream* src) -> bool;
|
||||
auto AddBytes(std::function<size_t(cpp::span<std::byte>)> writer) -> void;
|
||||
auto ConsumeBytes(std::function<size_t(cpp::span<std::byte>)> reader) -> void;
|
||||
auto AddBytes(std::function<size_t(std::span<std::byte>)> writer) -> void;
|
||||
auto ConsumeBytes(std::function<size_t(std::span<std::byte>)> reader) -> void;
|
||||
auto Empty() -> void;
|
||||
|
||||
SourceBuffer(const SourceBuffer&) = delete;
|
||||
SourceBuffer& operator=(const SourceBuffer&) = delete;
|
||||
|
||||
private:
|
||||
const cpp::span<std::byte> buffer_;
|
||||
const std::span<std::byte> buffer_;
|
||||
size_t bytes_in_buffer_;
|
||||
size_t offset_of_bytes_;
|
||||
};
|
||||
|
||||
@@ -10,12 +10,12 @@
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <span>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "ivorbisfile.h"
|
||||
#include "sample.hpp"
|
||||
#include "span.hpp"
|
||||
|
||||
#include "codec.hpp"
|
||||
|
||||
@@ -26,10 +26,10 @@ class TremorVorbisDecoder : public ICodec {
|
||||
TremorVorbisDecoder();
|
||||
~TremorVorbisDecoder();
|
||||
|
||||
auto OpenStream(std::shared_ptr<IStream> input,uint32_t offset)
|
||||
auto OpenStream(std::shared_ptr<IStream> input, uint32_t offset)
|
||||
-> cpp::result<OutputFormat, Error> override;
|
||||
|
||||
auto DecodeTo(cpp::span<sample::Sample> destination)
|
||||
auto DecodeTo(std::span<sample::Sample> destination)
|
||||
-> cpp::result<OutputInfo, Error> override;
|
||||
|
||||
TremorVorbisDecoder(const TremorVorbisDecoder&) = delete;
|
||||
|
||||
@@ -34,7 +34,7 @@ class WavDecoder : public ICodec {
|
||||
auto OpenStream(std::shared_ptr<IStream> input,uint32_t offset)
|
||||
-> cpp::result<OutputFormat, Error> override;
|
||||
|
||||
auto DecodeTo(cpp::span<sample::Sample> destination)
|
||||
auto DecodeTo(std::span<sample::Sample> destination)
|
||||
-> cpp::result<OutputInfo, Error> override;
|
||||
|
||||
WavDecoder(const WavDecoder&) = delete;
|
||||
|
||||
+5
-5
@@ -74,7 +74,7 @@ auto MadMp3Decoder::OpenStream(std::shared_ptr<IStream> input, uint32_t offset)
|
||||
while (!eof && !got_header) {
|
||||
eof = buffer_.Refill(input_.get());
|
||||
|
||||
buffer_.ConsumeBytes([&](cpp::span<std::byte> buf) -> size_t {
|
||||
buffer_.ConsumeBytes([&](std::span<std::byte> buf) -> size_t {
|
||||
mad_stream_buffer(stream_.get(),
|
||||
reinterpret_cast<const unsigned char*>(buf.data()),
|
||||
buf.size_bytes());
|
||||
@@ -130,7 +130,7 @@ auto MadMp3Decoder::OpenStream(std::shared_ptr<IStream> input, uint32_t offset)
|
||||
}
|
||||
need_refill = false;
|
||||
|
||||
buffer_.ConsumeBytes([&](cpp::span<std::byte> buf) -> size_t {
|
||||
buffer_.ConsumeBytes([&](std::span<std::byte> buf) -> size_t {
|
||||
mad_stream_buffer(stream_.get(),
|
||||
reinterpret_cast<const unsigned char*>(buf.data()),
|
||||
buf.size());
|
||||
@@ -156,13 +156,13 @@ auto MadMp3Decoder::OpenStream(std::shared_ptr<IStream> input, uint32_t offset)
|
||||
return output;
|
||||
}
|
||||
|
||||
auto MadMp3Decoder::DecodeTo(cpp::span<sample::Sample> output)
|
||||
auto MadMp3Decoder::DecodeTo(std::span<sample::Sample> output)
|
||||
-> cpp::result<OutputInfo, Error> {
|
||||
if (current_sample_ < 0 && !is_eos_) {
|
||||
if (!is_eof_) {
|
||||
is_eof_ = buffer_.Refill(input_.get());
|
||||
if (is_eof_) {
|
||||
buffer_.AddBytes([&](cpp::span<std::byte> buf) -> size_t {
|
||||
buffer_.AddBytes([&](std::span<std::byte> buf) -> size_t {
|
||||
if (buf.size() < MAD_BUFFER_GUARD) {
|
||||
is_eof_ = false;
|
||||
return 0;
|
||||
@@ -174,7 +174,7 @@ auto MadMp3Decoder::DecodeTo(cpp::span<sample::Sample> output)
|
||||
}
|
||||
}
|
||||
|
||||
buffer_.ConsumeBytes([&](cpp::span<std::byte> buf) -> size_t {
|
||||
buffer_.ConsumeBytes([&](std::span<std::byte> buf) -> size_t {
|
||||
mad_stream_buffer(stream_.get(),
|
||||
reinterpret_cast<const unsigned char*>(buf.data()),
|
||||
buf.size());
|
||||
|
||||
+1
-1
@@ -140,7 +140,7 @@ auto XiphOpusDecoder::OpenStream(std::shared_ptr<IStream> input,
|
||||
};
|
||||
}
|
||||
|
||||
auto XiphOpusDecoder::DecodeTo(cpp::span<sample::Sample> output)
|
||||
auto XiphOpusDecoder::DecodeTo(std::span<sample::Sample> output)
|
||||
-> cpp::result<OutputInfo, Error> {
|
||||
int samples_written = op_read_stereo(opus_, output.data(), output.size());
|
||||
|
||||
|
||||
@@ -39,7 +39,7 @@ auto SourceBuffer::Refill(IStream* src) -> bool {
|
||||
return false;
|
||||
}
|
||||
bool eof = false;
|
||||
AddBytes([&](cpp::span<std::byte> buf) -> size_t {
|
||||
AddBytes([&](std::span<std::byte> buf) -> size_t {
|
||||
ssize_t bytes_read = src->Read(buf);
|
||||
// Treat read errors as EOF.
|
||||
eof = bytes_read <= 0;
|
||||
@@ -48,7 +48,7 @@ auto SourceBuffer::Refill(IStream* src) -> bool {
|
||||
return eof;
|
||||
}
|
||||
|
||||
auto SourceBuffer::AddBytes(std::function<size_t(cpp::span<std::byte>)> writer)
|
||||
auto SourceBuffer::AddBytes(std::function<size_t(std::span<std::byte>)> writer)
|
||||
-> void {
|
||||
if (offset_of_bytes_ > 0) {
|
||||
std::memmove(buffer_.data(), buffer_.data() + offset_of_bytes_,
|
||||
@@ -61,9 +61,9 @@ auto SourceBuffer::AddBytes(std::function<size_t(cpp::span<std::byte>)> writer)
|
||||
}
|
||||
|
||||
auto SourceBuffer::ConsumeBytes(
|
||||
std::function<size_t(cpp::span<std::byte>)> reader) -> void {
|
||||
size_t bytes_consumed = std::invoke(
|
||||
reader, buffer_.subspan(offset_of_bytes_, bytes_in_buffer_));
|
||||
std::function<size_t(std::span<std::byte>)> reader) -> void {
|
||||
size_t bytes_consumed =
|
||||
std::invoke(reader, buffer_.subspan(offset_of_bytes_, bytes_in_buffer_));
|
||||
assert(bytes_consumed <= bytes_in_buffer_);
|
||||
|
||||
bytes_in_buffer_ -= bytes_consumed;
|
||||
|
||||
@@ -8,14 +8,14 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <span>
|
||||
|
||||
#include "catch2/catch.hpp"
|
||||
#include "span.hpp"
|
||||
|
||||
#include "test.mp3.hpp"
|
||||
|
||||
void load_mp3(cpp::span<std::byte> dest) {
|
||||
cpp::span<std::byte> src(reinterpret_cast<std::byte*>(test_mp3),
|
||||
void load_mp3(std::span<std::byte> dest) {
|
||||
std::span<std::byte> src(reinterpret_cast<std::byte*>(test_mp3),
|
||||
test_mp3_len);
|
||||
std::copy(src.begin(), src.begin() + dest.size(), dest.begin());
|
||||
}
|
||||
|
||||
@@ -129,7 +129,7 @@ auto TremorVorbisDecoder::OpenStream(std::shared_ptr<IStream> input,
|
||||
};
|
||||
}
|
||||
|
||||
auto TremorVorbisDecoder::DecodeTo(cpp::span<sample::Sample> output)
|
||||
auto TremorVorbisDecoder::DecodeTo(std::span<sample::Sample> output)
|
||||
-> cpp::result<OutputInfo, Error> {
|
||||
int unused = 0;
|
||||
long bytes_written =
|
||||
|
||||
+31
-28
@@ -20,24 +20,24 @@ namespace codecs {
|
||||
|
||||
[[maybe_unused]] static const char kTag[] = "wav";
|
||||
|
||||
static inline auto bytes_to_u16(cpp::span<std::byte const, 2> bytes)
|
||||
static inline auto bytes_to_u16(std::span<std::byte const, 2> bytes)
|
||||
-> uint16_t {
|
||||
return (uint16_t)bytes[0] | (uint16_t)bytes[1] << 8;
|
||||
}
|
||||
|
||||
static inline auto bytes_to_u32(cpp::span<std::byte const, 4> bytes)
|
||||
static inline auto bytes_to_u32(std::span<std::byte const, 4> bytes)
|
||||
-> uint32_t {
|
||||
return (uint32_t)bytes[0] | (uint32_t)bytes[1] << 8 |
|
||||
(uint32_t)bytes[2] << 16 | (uint32_t)bytes[3] << 24;
|
||||
}
|
||||
|
||||
static inline auto bytes_to_str(cpp::span<std::byte const> bytes)
|
||||
static inline auto bytes_to_str(std::span<std::byte const> bytes)
|
||||
-> std::string {
|
||||
return std::string(reinterpret_cast<const char*>(bytes.data()),
|
||||
bytes.size_bytes());
|
||||
bytes.size_bytes());
|
||||
}
|
||||
|
||||
static int16_t convert_f32_to_16_bit(cpp::span<const std::byte> bytes) {
|
||||
static int16_t convert_f32_to_16_bit(std::span<const std::byte> bytes) {
|
||||
uint64_t val = 0;
|
||||
val = (uint8_t)bytes[3];
|
||||
val = (val << 8) | (uint8_t)bytes[2];
|
||||
@@ -57,7 +57,7 @@ static int16_t convert_f32_to_16_bit(cpp::span<const std::byte> bytes) {
|
||||
return sample::FromDouble(*fval);
|
||||
}
|
||||
|
||||
static int16_t convert_f64_to_16_bit(cpp::span<const std::byte> bytes) {
|
||||
static int16_t convert_f64_to_16_bit(std::span<const std::byte> bytes) {
|
||||
uint64_t val = 0;
|
||||
val = (uint8_t)bytes[7];
|
||||
val = (val << 8) | (uint8_t)bytes[6];
|
||||
@@ -71,7 +71,7 @@ static int16_t convert_f64_to_16_bit(cpp::span<const std::byte> bytes) {
|
||||
return sample::FromDouble(*fval);
|
||||
}
|
||||
|
||||
static int16_t convert_to_16_bit(cpp::span<const std::byte> bytes) {
|
||||
static int16_t convert_to_16_bit(std::span<const std::byte> bytes) {
|
||||
int depth = bytes.size();
|
||||
int32_t val = 0;
|
||||
// If 8-bit Assume Unsigned
|
||||
@@ -82,10 +82,13 @@ static int16_t convert_to_16_bit(cpp::span<const std::byte> bytes) {
|
||||
switch (depth) {
|
||||
case 4:
|
||||
val = (uint8_t)bytes[3];
|
||||
[[fallthrough]];
|
||||
case 3:
|
||||
val = (val << 8) | (uint8_t)bytes[2];
|
||||
[[fallthrough]];
|
||||
case 2:
|
||||
val = (val << 8) | (uint8_t)bytes[1];
|
||||
[[fallthrough]];
|
||||
case 1:
|
||||
val = (val << 8) | (uint8_t)bytes[0];
|
||||
}
|
||||
@@ -98,7 +101,7 @@ WavDecoder::WavDecoder() : input_(), buffer_() {}
|
||||
|
||||
WavDecoder::~WavDecoder() {}
|
||||
|
||||
auto WavDecoder::OpenStream(std::shared_ptr<IStream> input,uint32_t offset)
|
||||
auto WavDecoder::OpenStream(std::shared_ptr<IStream> input, uint32_t offset)
|
||||
-> cpp::result<OutputFormat, Error> {
|
||||
input_ = input;
|
||||
|
||||
@@ -123,7 +126,7 @@ auto WavDecoder::OpenStream(std::shared_ptr<IStream> input,uint32_t offset)
|
||||
// - end of this part, next header we care about is 'data'
|
||||
// - and then the next 4 bytes = 32 bit int = size of data
|
||||
|
||||
auto buffer_span = cpp::span{buf};
|
||||
auto buffer_span = std::span{buf};
|
||||
|
||||
std::string riff = bytes_to_str(buffer_span.subspan(0, 4));
|
||||
if (riff != "RIFF") {
|
||||
@@ -131,7 +134,7 @@ auto WavDecoder::OpenStream(std::shared_ptr<IStream> input,uint32_t offset)
|
||||
return cpp::fail(Error::kMalformedData);
|
||||
}
|
||||
|
||||
uint32_t file_size = bytes_to_u32(buffer_span.subspan(4, 4)) + 8;
|
||||
// uint32_t file_size = bytes_to_u32(buffer_span.subspan(4, 4)) + 8;
|
||||
|
||||
std::string fmt_header = bytes_to_str(buffer_span.subspan(12, 4));
|
||||
ESP_LOGI(kTag, "fmt header found? %s",
|
||||
@@ -142,9 +145,9 @@ auto WavDecoder::OpenStream(std::shared_ptr<IStream> input,uint32_t offset)
|
||||
}
|
||||
|
||||
// Size of the fmt header, should be 16, 18 or 40
|
||||
uint32_t fmt_header_size = bytes_to_u32(buffer_span.subspan(16, 4));
|
||||
// uint32_t fmt_header_size = bytes_to_u32(buffer_span.subspan(16, 4));
|
||||
|
||||
wave_format_ = bytes_to_u16(buffer_span.subspan(20, 2));
|
||||
wave_format_ = bytes_to_u16(buffer_span.subspan<20, 2>());
|
||||
if (wave_format_ == kWaveFormatPCM) {
|
||||
ESP_LOGD(kTag, "wave format: PCM");
|
||||
} else if (wave_format_ == kWaveFormatExtensible) {
|
||||
@@ -156,17 +159,17 @@ auto WavDecoder::OpenStream(std::shared_ptr<IStream> input,uint32_t offset)
|
||||
return cpp::fail(Error::kUnsupportedFormat);
|
||||
}
|
||||
|
||||
num_channels_ = bytes_to_u16(buffer_span.subspan(22, 2));
|
||||
num_channels_ = bytes_to_u16(buffer_span.subspan<22, 2>());
|
||||
|
||||
uint32_t samples_per_second = bytes_to_u32(buffer_span.subspan(24, 4));
|
||||
uint32_t samples_per_second = bytes_to_u32(buffer_span.subspan<24, 4>());
|
||||
|
||||
uint32_t avg_bytes_per_second = bytes_to_u32(buffer_span.subspan(28, 4));
|
||||
// uint32_t avg_bytes_per_second = bytes_to_u32(buffer_span.subspan(28, 4));
|
||||
|
||||
uint16_t block_align = bytes_to_u16(buffer_span.subspan(32, 2));
|
||||
uint16_t block_align = bytes_to_u16(buffer_span.subspan<32, 2>());
|
||||
|
||||
bytes_per_sample_ = block_align / num_channels_;
|
||||
|
||||
uint16_t bits_per_sample = bytes_to_u16(buffer_span.subspan(34, 2));
|
||||
// uint16_t bits_per_sample = bytes_to_u16(buffer_span.subspan(34, 2));
|
||||
|
||||
// find the start of the data chunk
|
||||
std::array<std::byte, 4> data_tag = {std::byte{0x64}, std::byte{0x61},
|
||||
@@ -180,7 +183,7 @@ auto WavDecoder::OpenStream(std::shared_ptr<IStream> input,uint32_t offset)
|
||||
int data_chunk_index = std::distance(buffer_span.begin(), data_loc.begin());
|
||||
|
||||
uint32_t data_chunk_size =
|
||||
bytes_to_u32(buffer_span.subspan(data_chunk_index + 4, 4));
|
||||
bytes_to_u32(buffer_span.subspan(data_chunk_index + 4, 4).first<4>());
|
||||
|
||||
// calculate number of samples
|
||||
int number_of_samples = data_chunk_size / bytes_per_sample_;
|
||||
@@ -188,20 +191,20 @@ auto WavDecoder::OpenStream(std::shared_ptr<IStream> input,uint32_t offset)
|
||||
// extension to the fmt chunk size (0 or 22)
|
||||
uint16_t extension_size = 0;
|
||||
if (wave_format_ == kWaveFormatExtensible) {
|
||||
extension_size = bytes_to_u16(buffer_span.subspan(36, 2));
|
||||
extension_size = bytes_to_u16(buffer_span.subspan<36, 2>());
|
||||
}
|
||||
|
||||
// Parse extension if applicable
|
||||
if (extension_size == 22) {
|
||||
// Valid bits per sample
|
||||
uint16_t valid_bits_per_sample = bytes_to_u16(buffer_span.subspan(38, 2));
|
||||
// uint16_t valid_bits_per_sample = bytes_to_u16(buffer_span.subspan(38,
|
||||
// 2));
|
||||
|
||||
uint32_t speaker_mask = bytes_to_u32(buffer_span.subspan(40, 4));
|
||||
// uint32_t speaker_mask = bytes_to_u32(buffer_span.subspan(40, 4));
|
||||
|
||||
// Parse subformat
|
||||
subformat_ = bytes_to_u16(buffer_span.subspan(44, 2));
|
||||
if (!(subformat_ == kWaveFormatPCM ||
|
||||
subformat_ == kWaveFormatIEEEFloat)) {
|
||||
subformat_ = bytes_to_u16(buffer_span.subspan<44, 2>());
|
||||
if (!(subformat_ == kWaveFormatPCM || subformat_ == kWaveFormatIEEEFloat)) {
|
||||
ESP_LOGW(kTag, "WAVE extensible subformat_ not supported");
|
||||
return cpp::fail(Error::kUnsupportedFormat);
|
||||
}
|
||||
@@ -210,7 +213,8 @@ auto WavDecoder::OpenStream(std::shared_ptr<IStream> input,uint32_t offset)
|
||||
int64_t data_offset = offset * samples_per_second * bytes_per_sample_;
|
||||
|
||||
// Seek track to start of data
|
||||
input->SeekTo(data_chunk_index + 8 + data_offset, IStream::SeekFrom::kStartOfStream);
|
||||
input->SeekTo(data_chunk_index + 8 + data_offset,
|
||||
IStream::SeekFrom::kStartOfStream);
|
||||
|
||||
output_format_ = {.num_channels = (uint8_t)num_channels_,
|
||||
.sample_rate_hz = samples_per_second,
|
||||
@@ -219,12 +223,12 @@ auto WavDecoder::OpenStream(std::shared_ptr<IStream> input,uint32_t offset)
|
||||
return output_format_;
|
||||
}
|
||||
|
||||
auto WavDecoder::DecodeTo(cpp::span<sample::Sample> output)
|
||||
auto WavDecoder::DecodeTo(std::span<sample::Sample> output)
|
||||
-> cpp::result<OutputInfo, Error> {
|
||||
bool is_eof = buffer_.Refill(input_.get());
|
||||
size_t samples_written = 0;
|
||||
|
||||
buffer_.ConsumeBytes([&](cpp::span<std::byte> buf) -> size_t {
|
||||
buffer_.ConsumeBytes([&](std::span<std::byte> buf) -> size_t {
|
||||
size_t bytes_read = buf.size_bytes();
|
||||
size_t frames_read =
|
||||
bytes_read / bytes_per_sample_ / output_format_.num_channels;
|
||||
@@ -254,7 +258,6 @@ auto WavDecoder::DecodeTo(cpp::span<sample::Sample> output)
|
||||
return samples_written * bytes_per_sample_;
|
||||
});
|
||||
|
||||
|
||||
return OutputInfo{.samples_written = samples_written,
|
||||
.is_stream_finished = samples_written == 0 && is_eof};
|
||||
}
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
# Copyright 2023 jacqueline <me@jacqueline.id.au>
|
||||
#
|
||||
# SPDX-License-Identifier: GPL-3.0-only
|
||||
|
||||
idf_component_register(
|
||||
SRCS "env_esp.cpp" "database.cpp" "track.cpp" "records.cpp"
|
||||
"file_gatherer.cpp" "tag_parser.cpp" "index.cpp"
|
||||
INCLUDE_DIRS "include"
|
||||
REQUIRES "result" "span" "esp_psram" "fatfs" "libtags" "komihash" "cbor"
|
||||
"tasks" "memory" "util" "tinyfsm" "events" "opusfile" "libcppbor")
|
||||
|
||||
target_compile_options(${COMPONENT_LIB} PRIVATE ${EXTRA_WARNINGS})
|
||||
|
||||
set(LEVELDB_BUILD_TESTS OFF)
|
||||
set(LEVELDB_BUILD_BENCHMARKS OFF)
|
||||
set(LEVELDB_INSTALL OFF)
|
||||
|
||||
set(CMAKE_POLICY_DEFAULT_CMP0077 NEW)
|
||||
|
||||
add_subdirectory($ENV{PROJ_PATH}/lib/leveldb ${CMAKE_CURRENT_BINARY_DIR}/leveldb)
|
||||
|
||||
target_link_libraries(${COMPONENT_LIB} PUBLIC leveldb)
|
||||
@@ -1,9 +0,0 @@
|
||||
# Copyright 2023 jacqueline <me@jacqueline.id.au>
|
||||
#
|
||||
# SPDX-License-Identifier: GPL-3.0-only
|
||||
|
||||
idf_component_register(
|
||||
SRCS "console.cpp"
|
||||
INCLUDE_DIRS "include"
|
||||
REQUIRES "console" "memory")
|
||||
target_compile_options(${COMPONENT_LIB} PRIVATE ${EXTRA_WARNINGS})
|
||||
@@ -7,6 +7,6 @@ idf_component_register(
|
||||
"i2c.cpp" "bluetooth.cpp" "spi.cpp" "display.cpp" "display_init.cpp"
|
||||
"samd.cpp" "wm8523.cpp" "nvs.cpp" "haptics.cpp" "spiffs.cpp"
|
||||
INCLUDE_DIRS "include"
|
||||
REQUIRES "esp_adc" "fatfs" "result" "lvgl" "span" "tasks" "nvs_flash" "spiffs"
|
||||
"bt" "tinyfsm" "util")
|
||||
REQUIRES "esp_adc" "fatfs" "result" "lvgl" "nvs_flash" "spiffs" "bt"
|
||||
"tasks" "tinyfsm" "util" "libcppbor")
|
||||
target_compile_options(${COMPONENT_LIB} PRIVATE ${EXTRA_WARNINGS})
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@
|
||||
* SPDX-License-Identifier: GPL-3.0-only
|
||||
*/
|
||||
|
||||
#include "adc.hpp"
|
||||
#include "drivers/adc.hpp"
|
||||
#include <cstdint>
|
||||
|
||||
#include "esp_adc/adc_cali.h"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "bluetooth.hpp"
|
||||
#include "drivers/bluetooth.hpp"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
@@ -25,12 +25,11 @@
|
||||
#include "freertos/portmacro.h"
|
||||
#include "freertos/projdefs.h"
|
||||
#include "freertos/timers.h"
|
||||
#include "sample.hpp"
|
||||
#include "tinyfsm/include/tinyfsm.hpp"
|
||||
|
||||
#include "bluetooth_types.hpp"
|
||||
#include "drivers/bluetooth_types.hpp"
|
||||
#include "drivers/nvs.hpp"
|
||||
#include "memory_resource.hpp"
|
||||
#include "nvs.hpp"
|
||||
#include "tasks.hpp"
|
||||
|
||||
namespace drivers {
|
||||
@@ -39,6 +38,7 @@ namespace drivers {
|
||||
|
||||
DRAM_ATTR static StreamBufferHandle_t sStream = nullptr;
|
||||
DRAM_ATTR static std::atomic<float> sVolumeFactor = 1.f;
|
||||
DRAM_ATTR static std::atomic<uint32_t> sSamplesUsed = 0;
|
||||
|
||||
static tasks::WorkerPool* sBgWorker;
|
||||
|
||||
@@ -74,6 +74,13 @@ IRAM_ATTR auto a2dp_data_cb(uint8_t* buf, int32_t buf_size) -> int32_t {
|
||||
}
|
||||
size_t bytes_received = xStreamBufferReceive(stream, buf, buf_size, 0);
|
||||
|
||||
size_t samples_received = bytes_received / 2;
|
||||
if (UINT32_MAX - sSamplesUsed < samples_received) {
|
||||
sSamplesUsed = samples_received - (UINT32_MAX - sSamplesUsed);
|
||||
} else {
|
||||
sSamplesUsed += samples_received;
|
||||
}
|
||||
|
||||
// Apply software volume scaling.
|
||||
int16_t* samples = reinterpret_cast<int16_t*>(buf);
|
||||
float factor = sVolumeFactor.load();
|
||||
@@ -166,6 +173,10 @@ auto Bluetooth::SetVolumeFactor(float f) -> void {
|
||||
sVolumeFactor = f;
|
||||
}
|
||||
|
||||
auto Bluetooth::SamplesUsed() -> uint32_t {
|
||||
return sSamplesUsed;
|
||||
}
|
||||
|
||||
auto Bluetooth::SetEventHandler(std::function<void(bluetooth::Event)> cb)
|
||||
-> void {
|
||||
auto lock = bluetooth::BluetoothState::lock();
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
* SPDX-License-Identifier: GPL-3.0-only
|
||||
*/
|
||||
|
||||
#include "display.hpp"
|
||||
#include "drivers/display.hpp"
|
||||
#include <stdint.h>
|
||||
|
||||
#include <cmath>
|
||||
@@ -31,11 +31,11 @@
|
||||
#include "hal/spi_types.h"
|
||||
#include "lvgl/lvgl.h"
|
||||
|
||||
#include "display_init.hpp"
|
||||
#include "gpios.hpp"
|
||||
#include "drivers/display_init.hpp"
|
||||
#include "drivers/gpios.hpp"
|
||||
#include "drivers/spi.hpp"
|
||||
#include "misc/lv_color.h"
|
||||
#include "soc/soc.h"
|
||||
#include "spi.hpp"
|
||||
#include "tasks.hpp"
|
||||
|
||||
[[maybe_unused]] static const char* kTag = "DISPLAY";
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
* SPDX-License-Identifier: GPL-3.0-only
|
||||
*/
|
||||
|
||||
#include "display_init.hpp"
|
||||
#include "drivers/display_init.hpp"
|
||||
|
||||
namespace drivers {
|
||||
namespace displays {
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
* SPDX-License-Identifier: GPL-3.0-only
|
||||
*/
|
||||
|
||||
#include "gpios.hpp"
|
||||
#include "drivers/gpios.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
#include "esp_intr_alloc.h"
|
||||
#include "hal/gpio_types.h"
|
||||
|
||||
#include "i2c.hpp"
|
||||
#include "drivers/i2c.hpp"
|
||||
|
||||
namespace drivers {
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
* SPDX-License-Identifier: GPL-3.0-only
|
||||
*/
|
||||
|
||||
#include "haptics.hpp"
|
||||
#include "drivers/haptics.hpp"
|
||||
#include <stdint.h>
|
||||
|
||||
#include <cstdint>
|
||||
@@ -21,7 +21,7 @@
|
||||
#include "hal/gpio_types.h"
|
||||
#include "hal/i2c_types.h"
|
||||
|
||||
#include "i2c.hpp"
|
||||
#include "drivers/i2c.hpp"
|
||||
|
||||
namespace drivers {
|
||||
|
||||
@@ -54,7 +54,8 @@ Haptics::Haptics(const std::variant<ErmMotor, LraMotor>& motor) {
|
||||
// Set library
|
||||
// TODO(robin): try the other libraries and test response. C is marginal, D
|
||||
// too much?
|
||||
WriteRegister(Register::kWaveformLibrary, static_cast<uint8_t>(kDefaultErmLibrary));
|
||||
WriteRegister(Register::kWaveformLibrary,
|
||||
static_cast<uint8_t>(kDefaultErmLibrary));
|
||||
|
||||
} else if (std::holds_alternative<LraMotor>(motor)) {
|
||||
ESP_LOGI(kTag, "Setting up LRA motor...");
|
||||
@@ -75,7 +76,8 @@ Haptics::Haptics(const std::variant<ErmMotor, LraMotor>& motor) {
|
||||
ControlMask::kLraOpenLoop);
|
||||
|
||||
// Set library; only option is the LRA one for, well, LRA motors.
|
||||
WriteRegister(Register::kWaveformLibrary, static_cast<uint8_t>(Library::LRA));
|
||||
WriteRegister(Register::kWaveformLibrary,
|
||||
static_cast<uint8_t>(Library::LRA));
|
||||
}
|
||||
|
||||
// Set mode (internal trigger, on writing 1 to Go register)
|
||||
@@ -123,7 +125,6 @@ auto Haptics::SetWaveformEffect(Effect effect) -> void {
|
||||
current_effect_ = effect;
|
||||
}
|
||||
|
||||
|
||||
auto Haptics::TourEffects() -> void {
|
||||
TourEffects(Effect::kFirst, Effect::kLast, kDefaultErmLibrary);
|
||||
}
|
||||
@@ -174,7 +175,6 @@ auto Haptics::TourLibraries(Effect from, Effect to) -> void {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
auto Haptics::PowerDown() -> void {
|
||||
WriteRegister(Register::kMode, static_cast<uint8_t>(Mode::kInternalTrigger) |
|
||||
ModeMask::kStandby);
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@
|
||||
* SPDX-License-Identifier: GPL-3.0-only
|
||||
*/
|
||||
|
||||
#include "i2c.hpp"
|
||||
#include "drivers/i2c.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
|
||||
+21
-6
@@ -4,7 +4,8 @@
|
||||
* SPDX-License-Identifier: GPL-3.0-only
|
||||
*/
|
||||
|
||||
#include "i2s_dac.hpp"
|
||||
#include "drivers/i2s_dac.hpp"
|
||||
#include <stdint.h>
|
||||
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
@@ -25,11 +26,11 @@
|
||||
#include "hal/gpio_types.h"
|
||||
#include "hal/i2c_types.h"
|
||||
|
||||
#include "gpios.hpp"
|
||||
#include "drivers/gpios.hpp"
|
||||
#include "drivers/i2c.hpp"
|
||||
#include "drivers/wm8523.hpp"
|
||||
#include "hal/i2s_types.h"
|
||||
#include "i2c.hpp"
|
||||
#include "soc/clk_tree_defs.h"
|
||||
#include "wm8523.hpp"
|
||||
|
||||
namespace drivers {
|
||||
|
||||
@@ -140,7 +141,7 @@ auto I2SDac::SetPaused(bool paused) -> void {
|
||||
}
|
||||
}
|
||||
|
||||
static volatile bool sSwapWords = false;
|
||||
DRAM_ATTR static volatile bool sSwapWords = false;
|
||||
|
||||
auto I2SDac::Reconfigure(Channels ch, BitsPerSample bps, SampleRate rate)
|
||||
-> void {
|
||||
@@ -207,7 +208,7 @@ auto I2SDac::Reconfigure(Channels ch, BitsPerSample bps, SampleRate rate)
|
||||
}
|
||||
}
|
||||
|
||||
auto I2SDac::WriteData(const cpp::span<const std::byte>& data) -> void {
|
||||
auto I2SDac::WriteData(const std::span<const std::byte>& data) -> void {
|
||||
std::size_t bytes_written = 0;
|
||||
esp_err_t err = i2s_channel_write(i2s_handle_, data.data(), data.size_bytes(),
|
||||
&bytes_written, portMAX_DELAY);
|
||||
@@ -216,6 +217,8 @@ auto I2SDac::WriteData(const cpp::span<const std::byte>& data) -> void {
|
||||
}
|
||||
}
|
||||
|
||||
DRAM_ATTR static volatile uint32_t sSamplesRead = 0;
|
||||
|
||||
extern "C" IRAM_ATTR auto callback(i2s_chan_handle_t handle,
|
||||
i2s_event_data_t* event,
|
||||
void* user_ctx) -> bool {
|
||||
@@ -234,6 +237,14 @@ extern "C" IRAM_ATTR auto callback(i2s_chan_handle_t handle,
|
||||
size_t bytes_written =
|
||||
xStreamBufferReceiveFromISR(src, buf, event->size, &ret);
|
||||
|
||||
// Assume 16 bit samples.
|
||||
size_t samples = bytes_written / 2;
|
||||
if (UINT32_MAX - sSamplesRead < samples) {
|
||||
sSamplesRead = samples - (UINT32_MAX - sSamplesRead);
|
||||
} else {
|
||||
sSamplesRead = sSamplesRead + samples;
|
||||
}
|
||||
|
||||
// The ESP32's I2S peripheral has a different endianness to its processors.
|
||||
// ESP-IDF handles this difference for stereo channels, but not for mono
|
||||
// channels. We therefore sometimes need to swap each pair of words as they're
|
||||
@@ -275,6 +286,10 @@ auto I2SDac::SetSource(StreamBufferHandle_t buffer) -> void {
|
||||
}
|
||||
}
|
||||
|
||||
auto I2SDac::SamplesUsed() -> uint32_t {
|
||||
return sSamplesRead;
|
||||
}
|
||||
|
||||
auto I2SDac::set_channel(bool enabled) -> void {
|
||||
if (i2s_active_ == enabled) {
|
||||
return;
|
||||
|
||||
@@ -12,11 +12,11 @@
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/stream_buffer.h>
|
||||
#include <stdint.h>
|
||||
#include "bluetooth_types.hpp"
|
||||
#include "drivers/bluetooth_types.hpp"
|
||||
#include "drivers/nvs.hpp"
|
||||
#include "esp_a2dp_api.h"
|
||||
#include "esp_avrc_api.h"
|
||||
#include "esp_gap_bt_api.h"
|
||||
#include "nvs.hpp"
|
||||
#include "tasks.hpp"
|
||||
#include "tinyfsm.hpp"
|
||||
#include "tinyfsm/include/tinyfsm.hpp"
|
||||
@@ -44,6 +44,7 @@ class Bluetooth {
|
||||
|
||||
auto SetSource(StreamBufferHandle_t) -> void;
|
||||
auto SetVolumeFactor(float) -> void;
|
||||
auto SamplesUsed() -> uint32_t;
|
||||
|
||||
auto SetEventHandler(std::function<void(bluetooth::Event)> cb) -> void;
|
||||
};
|
||||
@@ -15,8 +15,8 @@
|
||||
#include "result.hpp"
|
||||
#include "tasks.hpp"
|
||||
|
||||
#include "display_init.hpp"
|
||||
#include "gpios.hpp"
|
||||
#include "drivers/display_init.hpp"
|
||||
#include "drivers/gpios.hpp"
|
||||
|
||||
namespace drivers {
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <span>
|
||||
#include <utility>
|
||||
|
||||
#include "driver/i2s_std.h"
|
||||
@@ -20,9 +21,8 @@
|
||||
#include "freertos/portmacro.h"
|
||||
#include "freertos/stream_buffer.h"
|
||||
#include "result.hpp"
|
||||
#include "span.hpp"
|
||||
|
||||
#include "gpios.hpp"
|
||||
#include "drivers/gpios.hpp"
|
||||
#include "sys/_stdint.h"
|
||||
|
||||
namespace drivers {
|
||||
@@ -68,9 +68,11 @@ class I2SDac {
|
||||
|
||||
auto Reconfigure(Channels ch, BitsPerSample bps, SampleRate rate) -> void;
|
||||
|
||||
auto WriteData(const cpp::span<const std::byte>& data) -> void;
|
||||
auto WriteData(const std::span<const std::byte>& data) -> void;
|
||||
auto SetSource(StreamBufferHandle_t buffer) -> void;
|
||||
|
||||
auto SamplesUsed() -> uint32_t;
|
||||
|
||||
// Not copyable or movable.
|
||||
I2SDac(const I2SDac&) = delete;
|
||||
I2SDac& operator=(const I2SDac&) = delete;
|
||||
@@ -13,9 +13,8 @@
|
||||
#include "esp_err.h"
|
||||
#include "nvs.h"
|
||||
|
||||
#include "bluetooth_types.hpp"
|
||||
#include "drivers/bluetooth_types.hpp"
|
||||
#include "lru_cache.hpp"
|
||||
#include "tasks.hpp"
|
||||
|
||||
namespace drivers {
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
#include "ff.h"
|
||||
#include "result.hpp"
|
||||
|
||||
#include "gpios.hpp"
|
||||
#include "drivers/gpios.hpp"
|
||||
|
||||
namespace drivers {
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
#include "esp_err.h"
|
||||
#include "result.hpp"
|
||||
|
||||
#include "gpios.hpp"
|
||||
#include "drivers/gpios.hpp"
|
||||
|
||||
namespace drivers {
|
||||
|
||||
+10
-15
@@ -4,22 +4,19 @@
|
||||
* SPDX-License-Identifier: GPL-3.0-only
|
||||
*/
|
||||
|
||||
#include "nvs.hpp"
|
||||
#include <stdint.h>
|
||||
#include <sys/_stdint.h>
|
||||
#include "drivers/nvs.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
|
||||
#include "bluetooth.hpp"
|
||||
#include "bluetooth_types.hpp"
|
||||
#include "cppbor.h"
|
||||
#include "cppbor_parse.h"
|
||||
#include "drivers/bluetooth.hpp"
|
||||
#include "drivers/bluetooth_types.hpp"
|
||||
#include "drivers/wm8523.hpp"
|
||||
#include "esp_log.h"
|
||||
#include "nvs.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "tasks.hpp"
|
||||
#include "wm8523.hpp"
|
||||
|
||||
namespace drivers {
|
||||
|
||||
@@ -42,8 +39,8 @@ static constexpr char kKeyDisplayRows[] = "disprows";
|
||||
static constexpr char kKeyHapticMotorType[] = "hapticmtype";
|
||||
static constexpr char kKeyDbAutoIndex[] = "dbautoindex";
|
||||
|
||||
static auto nvs_get_string(nvs_handle_t nvs, const char* key)
|
||||
-> std::optional<std::string> {
|
||||
static auto nvs_get_string(nvs_handle_t nvs,
|
||||
const char* key) -> std::optional<std::string> {
|
||||
size_t len = 0;
|
||||
if (nvs_get_blob(nvs, key, NULL, &len) != ESP_OK) {
|
||||
return {};
|
||||
@@ -190,8 +187,7 @@ auto NvsStorage::Read() -> void {
|
||||
lock_polarity_.read(handle_);
|
||||
display_cols_.read(handle_);
|
||||
display_rows_.read(handle_);
|
||||
haptic_motor_type_.read(handle_),
|
||||
brightness_.read(handle_);
|
||||
haptic_motor_type_.read(handle_), brightness_.read(handle_);
|
||||
sensitivity_.read(handle_);
|
||||
amp_max_vol_.read(handle_);
|
||||
amp_cur_vol_.read(handle_);
|
||||
@@ -208,8 +204,7 @@ auto NvsStorage::Write() -> bool {
|
||||
lock_polarity_.write(handle_);
|
||||
display_cols_.write(handle_);
|
||||
display_rows_.write(handle_);
|
||||
haptic_motor_type_.write(handle_),
|
||||
brightness_.write(handle_);
|
||||
haptic_motor_type_.write(handle_), brightness_.write(handle_);
|
||||
sensitivity_.write(handle_);
|
||||
amp_max_vol_.write(handle_);
|
||||
amp_cur_vol_.write(handle_);
|
||||
@@ -290,8 +285,8 @@ auto NvsStorage::BluetoothVolume(const bluetooth::mac_addr_t& mac) -> uint8_t {
|
||||
return bt_volumes_.Get(mac).value_or(50);
|
||||
}
|
||||
|
||||
auto NvsStorage::BluetoothVolume(const bluetooth::mac_addr_t& mac, uint8_t vol)
|
||||
-> void {
|
||||
auto NvsStorage::BluetoothVolume(const bluetooth::mac_addr_t& mac,
|
||||
uint8_t vol) -> void {
|
||||
std::lock_guard<std::mutex> lock{mutex_};
|
||||
bt_volumes_dirty_ = true;
|
||||
bt_volumes_.Put(mac, vol);
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
* SPDX-License-Identifier: GPL-3.0-only
|
||||
*/
|
||||
|
||||
#include "samd.hpp"
|
||||
#include "drivers/samd.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
#include <optional>
|
||||
@@ -15,7 +15,7 @@
|
||||
#include "hal/gpio_types.h"
|
||||
#include "hal/i2c_types.h"
|
||||
|
||||
#include "i2c.hpp"
|
||||
#include "drivers/i2c.hpp"
|
||||
|
||||
enum Registers : uint8_t {
|
||||
kSamdFirmwareVersion = 0,
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@
|
||||
* SPDX-License-Identifier: GPL-3.0-only
|
||||
*/
|
||||
|
||||
#include "spi.hpp"
|
||||
#include "drivers/spi.hpp"
|
||||
|
||||
#include "driver/sdspi_host.h"
|
||||
#include "driver/spi_common.h"
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
* SPDX-License-Identifier: GPL-3.0-only
|
||||
*/
|
||||
|
||||
#include "spiffs.hpp"
|
||||
#include "drivers/spiffs.hpp"
|
||||
|
||||
#include "esp_err.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
* SPDX-License-Identifier: GPL-3.0-only
|
||||
*/
|
||||
|
||||
#include "storage.hpp"
|
||||
#include "drivers/storage.hpp"
|
||||
|
||||
#include <atomic>
|
||||
#include <memory>
|
||||
@@ -23,7 +23,7 @@
|
||||
#include "hal/spi_types.h"
|
||||
#include "sdmmc_cmd.h"
|
||||
|
||||
#include "gpios.hpp"
|
||||
#include "drivers/gpios.hpp"
|
||||
#include "memory_resource.hpp"
|
||||
|
||||
[[maybe_unused]] static const char* kTag = "SDSTORAGE";
|
||||
|
||||
@@ -10,8 +10,8 @@
|
||||
|
||||
#include "catch2/catch.hpp"
|
||||
|
||||
#include "gpios.hpp"
|
||||
#include "i2c.hpp"
|
||||
#include "drivers/gpios.hpp"
|
||||
#include "drivers/i2c.hpp"
|
||||
#include "i2c_fixture.hpp"
|
||||
|
||||
namespace drivers {
|
||||
|
||||
@@ -4,11 +4,11 @@
|
||||
* SPDX-License-Identifier: GPL-3.0-only
|
||||
*/
|
||||
|
||||
#include "gpios.hpp"
|
||||
#include "drivers/gpios.hpp"
|
||||
|
||||
#include "catch2/catch.hpp"
|
||||
|
||||
#include "i2c.hpp"
|
||||
#include "drivers/i2c.hpp"
|
||||
#include "i2c_fixture.hpp"
|
||||
|
||||
namespace drivers {
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
* SPDX-License-Identifier: GPL-3.0-only
|
||||
*/
|
||||
|
||||
#include "storage.hpp"
|
||||
#include "drivers/storage.hpp"
|
||||
|
||||
#include <dirent.h>
|
||||
|
||||
@@ -14,10 +14,10 @@
|
||||
|
||||
#include "catch2/catch.hpp"
|
||||
|
||||
#include "gpios.hpp"
|
||||
#include "i2c.hpp"
|
||||
#include "drivers/gpios.hpp"
|
||||
#include "drivers/i2c.hpp"
|
||||
#include "drivers/spi.hpp"
|
||||
#include "i2c_fixture.hpp"
|
||||
#include "spi.hpp"
|
||||
#include "spi_fixture.hpp"
|
||||
|
||||
namespace drivers {
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
* SPDX-License-Identifier: GPL-3.0-only
|
||||
*/
|
||||
|
||||
#include "touchwheel.hpp"
|
||||
#include "drivers/touchwheel.hpp"
|
||||
#include <stdint.h>
|
||||
|
||||
#include <cstdint>
|
||||
@@ -18,7 +18,7 @@
|
||||
#include "hal/gpio_types.h"
|
||||
#include "hal/i2c_types.h"
|
||||
|
||||
#include "i2c.hpp"
|
||||
#include "drivers/i2c.hpp"
|
||||
|
||||
namespace drivers {
|
||||
|
||||
|
||||
@@ -3,14 +3,14 @@
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-only
|
||||
*/
|
||||
#include "wm8523.hpp"
|
||||
#include "drivers/wm8523.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include "esp_err.h"
|
||||
|
||||
#include "drivers/i2c.hpp"
|
||||
#include "hal/i2c_types.h"
|
||||
#include "i2c.hpp"
|
||||
|
||||
namespace drivers {
|
||||
namespace wm8523 {
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
# Copyright 2023 jacqueline <me@jacqueline.id.au>
|
||||
#
|
||||
# SPDX-License-Identifier: GPL-3.0-only
|
||||
|
||||
idf_component_register(
|
||||
SRCS "event_queue.cpp"
|
||||
INCLUDE_DIRS "include"
|
||||
REQUIRES "tinyfsm" "ui")
|
||||
target_compile_options(${COMPONENT_LIB} PRIVATE ${EXTRA_WARNINGS})
|
||||
@@ -0,0 +1,7 @@
|
||||
# Copyright 2024 jacqueline <me@jacqueline.id.au>
|
||||
#
|
||||
# SPDX-License-Identifier: GPL-3.0-only
|
||||
|
||||
idf_component_register(
|
||||
SRCS "font_fusion_10.c" "font_fusion_12.c" "splash.c"
|
||||
REQUIRES "lvgl")
|
||||
@@ -1,13 +0,0 @@
|
||||
# Copyright 2023 jacqueline <me@jacqueline.id.au>
|
||||
#
|
||||
# SPDX-License-Identifier: GPL-3.0-only
|
||||
|
||||
idf_component_register(
|
||||
SRCS "input_touch_wheel.cpp" "input_touch_dpad.cpp" "input_trigger.cpp"
|
||||
"input_volume_buttons.cpp" "lvgl_input_driver.cpp" "feedback_haptics.cpp"
|
||||
"device_factory.cpp" "input_nav_buttons.cpp" "input_hook.cpp"
|
||||
"input_hook_actions.cpp"
|
||||
INCLUDE_DIRS "include"
|
||||
REQUIRES "drivers" "lvgl" "events" "system_fsm")
|
||||
|
||||
target_compile_options(${COMPONENT_LIB} PRIVATE ${EXTRA_WARNINGS})
|
||||
@@ -1,60 +0,0 @@
|
||||
/*
|
||||
* Copyright 2023 jacqueline <me@jacqueline.id.au>
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-only
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <deque>
|
||||
#include <memory>
|
||||
#include <set>
|
||||
|
||||
#include "core/lv_group.h"
|
||||
#include "device_factory.hpp"
|
||||
#include "feedback_device.hpp"
|
||||
#include "gpios.hpp"
|
||||
#include "hal/lv_hal_indev.h"
|
||||
|
||||
#include "input_device.hpp"
|
||||
#include "nvs.hpp"
|
||||
#include "property.hpp"
|
||||
#include "touchwheel.hpp"
|
||||
|
||||
namespace input {
|
||||
|
||||
/*
|
||||
* Implementation of an LVGL input device. This class composes multiple
|
||||
* IInputDevice and IFeedbackDevice instances together into a single LVGL
|
||||
* device.
|
||||
*/
|
||||
class LvglInputDriver {
|
||||
public:
|
||||
LvglInputDriver(drivers::NvsStorage& nvs, DeviceFactory&);
|
||||
|
||||
auto mode() -> lua::Property& { return mode_; }
|
||||
|
||||
auto read(lv_indev_data_t* data) -> void;
|
||||
auto feedback(uint8_t) -> void;
|
||||
|
||||
auto registration() -> lv_indev_t* { return registration_; }
|
||||
auto lock(bool l) -> void { is_locked_ = l; }
|
||||
|
||||
auto pushHooks(lua_State* L) -> int;
|
||||
|
||||
private:
|
||||
drivers::NvsStorage& nvs_;
|
||||
DeviceFactory& factory_;
|
||||
|
||||
lua::Property mode_;
|
||||
lv_indev_drv_t driver_;
|
||||
lv_indev_t* registration_;
|
||||
|
||||
std::vector<std::shared_ptr<IInputDevice>> inputs_;
|
||||
std::vector<std::shared_ptr<IFeedbackDevice>> feedbacks_;
|
||||
|
||||
bool is_locked_;
|
||||
};
|
||||
|
||||
} // namespace input
|
||||
@@ -1,127 +0,0 @@
|
||||
/*
|
||||
* Copyright 2023 jacqueline <me@jacqueline.id.au>
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-only
|
||||
*/
|
||||
|
||||
#include "lvgl_input_driver.hpp"
|
||||
#include <stdint.h>
|
||||
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <variant>
|
||||
|
||||
#include "device_factory.hpp"
|
||||
#include "feedback_haptics.hpp"
|
||||
#include "input_touch_wheel.hpp"
|
||||
#include "input_trigger.hpp"
|
||||
#include "input_volume_buttons.hpp"
|
||||
#include "lauxlib.h"
|
||||
#include "lua.h"
|
||||
#include "lvgl.h"
|
||||
#include "nvs.hpp"
|
||||
#include "property.hpp"
|
||||
|
||||
[[maybe_unused]] static constexpr char kTag[] = "input";
|
||||
|
||||
namespace input {
|
||||
|
||||
static void read_cb(lv_indev_drv_t* drv, lv_indev_data_t* data) {
|
||||
LvglInputDriver* instance =
|
||||
reinterpret_cast<LvglInputDriver*>(drv->user_data);
|
||||
instance->read(data);
|
||||
}
|
||||
|
||||
static void feedback_cb(lv_indev_drv_t* drv, uint8_t event) {
|
||||
LvglInputDriver* instance =
|
||||
reinterpret_cast<LvglInputDriver*>(drv->user_data);
|
||||
instance->feedback(event);
|
||||
}
|
||||
|
||||
auto intToMode(int raw) -> std::optional<drivers::NvsStorage::InputModes> {
|
||||
switch (raw) {
|
||||
case 0:
|
||||
return drivers::NvsStorage::InputModes::kButtonsOnly;
|
||||
case 1:
|
||||
return drivers::NvsStorage::InputModes::kButtonsWithWheel;
|
||||
case 2:
|
||||
return drivers::NvsStorage::InputModes::kDirectionalWheel;
|
||||
case 3:
|
||||
return drivers::NvsStorage::InputModes::kRotatingWheel;
|
||||
default:
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
LvglInputDriver::LvglInputDriver(drivers::NvsStorage& nvs,
|
||||
DeviceFactory& factory)
|
||||
: nvs_(nvs),
|
||||
factory_(factory),
|
||||
mode_(static_cast<int>(nvs.PrimaryInput()),
|
||||
[&](const lua::LuaValue& val) {
|
||||
if (!std::holds_alternative<int>(val)) {
|
||||
return false;
|
||||
}
|
||||
auto mode = intToMode(std::get<int>(val));
|
||||
if (!mode) {
|
||||
return false;
|
||||
}
|
||||
nvs.PrimaryInput(*mode);
|
||||
inputs_ = factory.createInputs(*mode);
|
||||
return true;
|
||||
}),
|
||||
driver_(),
|
||||
registration_(nullptr),
|
||||
inputs_(factory.createInputs(nvs.PrimaryInput())),
|
||||
feedbacks_(factory.createFeedbacks()),
|
||||
is_locked_(false) {
|
||||
lv_indev_drv_init(&driver_);
|
||||
driver_.type = LV_INDEV_TYPE_ENCODER;
|
||||
driver_.read_cb = read_cb;
|
||||
driver_.feedback_cb = feedback_cb;
|
||||
driver_.user_data = this;
|
||||
driver_.long_press_time = kLongPressDelayMs;
|
||||
driver_.long_press_repeat_time = kRepeatDelayMs;
|
||||
|
||||
registration_ = lv_indev_drv_register(&driver_);
|
||||
}
|
||||
|
||||
auto LvglInputDriver::read(lv_indev_data_t* data) -> void {
|
||||
// TODO: we should pass lock state on to the individual devices, since they
|
||||
// may wish to either ignore the lock state, or power down until unlock.
|
||||
if (is_locked_) {
|
||||
return;
|
||||
}
|
||||
for (auto&& device : inputs_) {
|
||||
device->read(data);
|
||||
}
|
||||
}
|
||||
|
||||
auto LvglInputDriver::feedback(uint8_t event) -> void {
|
||||
if (is_locked_) {
|
||||
return;
|
||||
}
|
||||
for (auto&& device : feedbacks_) {
|
||||
device->feedback(event);
|
||||
}
|
||||
}
|
||||
|
||||
auto LvglInputDriver::pushHooks(lua_State* L) -> int {
|
||||
lua_newtable(L);
|
||||
|
||||
for (auto& dev : inputs_) {
|
||||
lua_pushlstring(L, dev->name().data(), dev->name().size());
|
||||
lua_newtable(L);
|
||||
|
||||
for (auto& hook : dev->hooks()) {
|
||||
lua_pushlstring(L, hook.name().data(), hook.name().size());
|
||||
hook.pushHooks(L);
|
||||
lua_rawset(L, -3);
|
||||
}
|
||||
|
||||
lua_rawset(L, -3);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
} // namespace input
|
||||
@@ -6,6 +6,6 @@ idf_component_register(
|
||||
SRCS "collation.cpp" "strxfrm_l.c"
|
||||
INCLUDE_DIRS "include"
|
||||
PRIV_INCLUDE_DIRS "priv_include"
|
||||
REQUIRES "span" "esp_partition" "spi_flash")
|
||||
REQUIRES "esp_partition" "spi_flash")
|
||||
|
||||
target_compile_options(${COMPONENT_LIB} PRIVATE ${EXTRA_WARNINGS})
|
||||
|
||||
@@ -9,10 +9,10 @@
|
||||
#include <cstddef>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <span>
|
||||
#include <string>
|
||||
|
||||
#include "esp_partition.h"
|
||||
#include "span.hpp"
|
||||
|
||||
#include "strxfrm.h"
|
||||
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
# Copyright 2023 jacqueline <me@jacqueline.id.au>
|
||||
#
|
||||
# SPDX-License-Identifier: GPL-3.0-only
|
||||
|
||||
idf_component_register(
|
||||
SRCS "lua_theme.cpp" "lua_thread.cpp" "bridge.cpp" "property.cpp" "lua_database.cpp"
|
||||
"lua_queue.cpp" "lua_version.cpp" "lua_theme.cpp" "lua_controls.cpp" "registry.cpp"
|
||||
"lua_screen.cpp" "lua_filesystem.cpp" "file_iterator.cpp"
|
||||
INCLUDE_DIRS "include"
|
||||
REQUIRES "drivers" "lvgl" "tinyfsm" "events" "system_fsm" "database" "fatfs"
|
||||
"esp_timer" "battery" "esp-idf-lua" "luavgl" "lua-linenoise" "lua-term"
|
||||
"esp_app_format")
|
||||
target_compile_options(${COMPONENT_LIB} PRIVATE ${EXTRA_WARNINGS})
|
||||
@@ -2,8 +2,5 @@
|
||||
#
|
||||
# SPDX-License-Identifier: GPL-3.0-only
|
||||
|
||||
idf_component_register(
|
||||
SRCS "main.cpp"
|
||||
INCLUDE_DIRS "."
|
||||
REQUIRES "audio" "ui" "system_fsm" "events")
|
||||
idf_component_register(SRCS "main.cpp" INCLUDE_DIRS "." REQUIRES "tangara")
|
||||
target_compile_options(${COMPONENT_LIB} PRIVATE ${EXTRA_WARNINGS})
|
||||
|
||||
+6
-6
@@ -7,14 +7,14 @@
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/portmacro.h"
|
||||
|
||||
#include "i2c.hpp"
|
||||
#include "system_events.hpp"
|
||||
#include "tinyfsm.hpp"
|
||||
|
||||
#include "audio_fsm.hpp"
|
||||
#include "event_queue.hpp"
|
||||
#include "system_fsm.hpp"
|
||||
#include "ui_fsm.hpp"
|
||||
#include "audio/audio_fsm.hpp"
|
||||
#include "drivers/i2c.hpp"
|
||||
#include "events/event_queue.hpp"
|
||||
#include "system_fsm/system_events.hpp"
|
||||
#include "system_fsm/system_fsm.hpp"
|
||||
#include "ui/ui_fsm.hpp"
|
||||
|
||||
extern "C" void app_main(void) {
|
||||
ESP_ERROR_CHECK(drivers::init_i2c());
|
||||
|
||||
@@ -8,9 +8,9 @@
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <span>
|
||||
|
||||
#include "esp32/himem.h"
|
||||
#include "span.hpp"
|
||||
|
||||
/*
|
||||
* Wrapper around an ESP-IDF himem allocation, which uses RAII to clean up after
|
||||
@@ -62,14 +62,14 @@ class MappableRegion {
|
||||
}
|
||||
}
|
||||
|
||||
auto Get() -> cpp::span<std::byte> {
|
||||
auto Get() -> std::span<std::byte> {
|
||||
if (bytes_ == nullptr) {
|
||||
return {};
|
||||
}
|
||||
return {bytes_, size};
|
||||
}
|
||||
|
||||
auto Map(const HimemAlloc<size>& alloc) -> cpp::span<std::byte> {
|
||||
auto Map(const HimemAlloc<size>& alloc) -> std::span<std::byte> {
|
||||
assert(bytes_ == nullptr);
|
||||
ESP_ERROR_CHECK(esp_himem_map(alloc.handle, range_handle, 0, 0, size, 0,
|
||||
reinterpret_cast<void**>(&bytes_)));
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
# Copyright 2023 jacqueline <me@jacqueline.id.au>
|
||||
#
|
||||
# SPDX-License-Identifier: GPL-3.0-only
|
||||
|
||||
idf_component_register(
|
||||
SRCS "system_fsm.cpp" "running.cpp" "booting.cpp" "idle.cpp"
|
||||
"service_locator.cpp"
|
||||
INCLUDE_DIRS "include"
|
||||
REQUIRES "tinyfsm" "drivers" "database" "ui" "result" "events" "audio"
|
||||
"app_console" "battery" "locale")
|
||||
target_compile_options(${COMPONENT_LIB} PRIVATE ${EXTRA_WARNINGS})
|
||||
@@ -0,0 +1,22 @@
|
||||
# Copyright 2024 jacqueline <me@jacqueline.id.au>
|
||||
#
|
||||
# SPDX-License-Identifier: GPL-3.0-only
|
||||
|
||||
idf_component_register(
|
||||
SRC_DIRS "app_console" "audio" "battery" "database" "dev_console" "events"
|
||||
"input" "lua" "system_fsm" "ui"
|
||||
INCLUDE_DIRS "."
|
||||
REQUIRES "codecs" "drivers" "locale" "memory" "tasks" "util" "graphics"
|
||||
"tinyfsm" "lvgl" "esp_timer" "luavgl" "esp_app_format" "libcppbor" "libtags"
|
||||
"komihash" "result" "esp_psram" "fatfs" "millershuffle" "speexdsp" "console"
|
||||
"esp-idf-lua" "lua-linenoise" "lua-term")
|
||||
target_compile_options(${COMPONENT_LIB} PRIVATE ${EXTRA_WARNINGS})
|
||||
|
||||
set(LEVELDB_BUILD_TESTS OFF)
|
||||
set(LEVELDB_BUILD_BENCHMARKS OFF)
|
||||
set(LEVELDB_INSTALL OFF)
|
||||
|
||||
set(CMAKE_POLICY_DEFAULT_CMP0077 NEW)
|
||||
|
||||
add_subdirectory($ENV{PROJ_PATH}/lib/leveldb ${CMAKE_CURRENT_BINARY_DIR}/leveldb)
|
||||
target_link_libraries(${COMPONENT_LIB} PUBLIC leveldb)
|
||||
@@ -4,11 +4,9 @@
|
||||
* SPDX-License-Identifier: GPL-3.0-only
|
||||
*/
|
||||
|
||||
#include "app_console.hpp"
|
||||
#include "app_console/app_console.hpp"
|
||||
|
||||
#include <dirent.h>
|
||||
#include <stdint.h>
|
||||
#include <sys/_stdint.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
@@ -21,11 +19,7 @@
|
||||
#include <string>
|
||||
|
||||
#include "FreeRTOSConfig.h"
|
||||
#include "audio_events.hpp"
|
||||
#include "audio_fsm.hpp"
|
||||
#include "bluetooth.hpp"
|
||||
#include "bluetooth_types.hpp"
|
||||
#include "database.hpp"
|
||||
|
||||
#include "esp_app_desc.h"
|
||||
#include "esp_console.h"
|
||||
#include "esp_err.h"
|
||||
@@ -34,19 +28,25 @@
|
||||
#include "esp_intr_alloc.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_system.h"
|
||||
#include "event_queue.hpp"
|
||||
#include "ff.h"
|
||||
#include "freertos/FreeRTOSConfig_arch.h"
|
||||
#include "freertos/projdefs.h"
|
||||
#include "haptics.hpp"
|
||||
#include "index.hpp"
|
||||
#include "lua_registry.hpp"
|
||||
|
||||
#include "drivers/bluetooth.hpp"
|
||||
#include "drivers/bluetooth_types.hpp"
|
||||
#include "drivers/haptics.hpp"
|
||||
#include "drivers/samd.hpp"
|
||||
#include "memory_resource.hpp"
|
||||
#include "samd.hpp"
|
||||
#include "service_locator.hpp"
|
||||
#include "system_events.hpp"
|
||||
#include "track.hpp"
|
||||
#include "ui_events.hpp"
|
||||
|
||||
#include "audio/audio_events.hpp"
|
||||
#include "audio/audio_fsm.hpp"
|
||||
#include "database/database.hpp"
|
||||
#include "database/index.hpp"
|
||||
#include "database/track.hpp"
|
||||
#include "events/event_queue.hpp"
|
||||
#include "lua/lua_registry.hpp"
|
||||
#include "system_fsm/service_locator.hpp"
|
||||
#include "system_fsm/system_events.hpp"
|
||||
#include "ui/ui_events.hpp"
|
||||
|
||||
namespace console {
|
||||
|
||||
@@ -8,12 +8,12 @@
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "bluetooth.hpp"
|
||||
#include "console.hpp"
|
||||
#include "database.hpp"
|
||||
#include "samd.hpp"
|
||||
#include "service_locator.hpp"
|
||||
#include "track_queue.hpp"
|
||||
#include "audio/track_queue.hpp"
|
||||
#include "drivers/bluetooth.hpp"
|
||||
#include "dev_console/console.hpp"
|
||||
#include "database/database.hpp"
|
||||
#include "drivers/samd.hpp"
|
||||
#include "system_fsm/service_locator.hpp"
|
||||
|
||||
namespace console {
|
||||
|
||||
@@ -0,0 +1,199 @@
|
||||
/*
|
||||
* Copyright 2023 jacqueline <me@jacqueline.id.au>
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-only
|
||||
*/
|
||||
|
||||
#include "audio/audio_decoder.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <cmath>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <deque>
|
||||
#include <memory>
|
||||
#include <span>
|
||||
#include <variant>
|
||||
|
||||
#include "esp_err.h"
|
||||
#include "esp_heap_caps.h"
|
||||
#include "esp_log.h"
|
||||
#include "freertos/portmacro.h"
|
||||
#include "freertos/projdefs.h"
|
||||
#include "freertos/queue.h"
|
||||
|
||||
#include "audio/audio_events.hpp"
|
||||
#include "audio/audio_fsm.hpp"
|
||||
#include "audio/audio_sink.hpp"
|
||||
#include "audio/audio_source.hpp"
|
||||
#include "audio/processor.hpp"
|
||||
#include "codec.hpp"
|
||||
#include "database/track.hpp"
|
||||
#include "drivers/i2s_dac.hpp"
|
||||
#include "events/event_queue.hpp"
|
||||
#include "sample.hpp"
|
||||
#include "tasks.hpp"
|
||||
#include "types.hpp"
|
||||
#include "ui/ui_fsm.hpp"
|
||||
|
||||
namespace audio {
|
||||
|
||||
static const char* kTag = "decoder";
|
||||
|
||||
/*
|
||||
* The size of the buffer used for holding decoded samples. This buffer is
|
||||
* allocated in internal memory for greater speed, so be careful when
|
||||
* increasing its size.
|
||||
*/
|
||||
static constexpr std::size_t kCodecBufferLength =
|
||||
drivers::kI2SBufferLengthFrames * sizeof(sample::Sample);
|
||||
|
||||
auto Decoder::Start(std::shared_ptr<SampleProcessor> sink) -> Decoder* {
|
||||
Decoder* task = new Decoder(sink);
|
||||
tasks::StartPersistent<tasks::Type::kAudioDecoder>([=]() { task->Main(); });
|
||||
return task;
|
||||
}
|
||||
|
||||
auto Decoder::open(std::shared_ptr<TaggedStream> stream) -> void {
|
||||
NextStream* next = new NextStream();
|
||||
next->stream = stream;
|
||||
// The decoder services its queue very quickly, so blocking on this write
|
||||
// should be fine. If we discover contention here, then adding more space for
|
||||
// items to next_stream_ should be fine too.
|
||||
xQueueSend(next_stream_, &next, portMAX_DELAY);
|
||||
}
|
||||
|
||||
Decoder::Decoder(std::shared_ptr<SampleProcessor> processor)
|
||||
: processor_(processor), next_stream_(xQueueCreate(1, sizeof(void*))) {
|
||||
ESP_LOGI(kTag, "allocating codec buffer, %u KiB", kCodecBufferLength / 1024);
|
||||
codec_buffer_ = {
|
||||
reinterpret_cast<sample::Sample*>(heap_caps_calloc(
|
||||
kCodecBufferLength, sizeof(sample::Sample), MALLOC_CAP_DMA)),
|
||||
kCodecBufferLength};
|
||||
}
|
||||
|
||||
/*
|
||||
* Main decoding loop. Handles watching for new streams, or continuing to nudge
|
||||
* along the current stream if we have one.
|
||||
*/
|
||||
void Decoder::Main() {
|
||||
for (;;) {
|
||||
// Check whether there's a new stream to begin. If we're idle, then we
|
||||
// simply park and wait forever for a stream to arrive.
|
||||
TickType_t wait_time = stream_ ? 0 : portMAX_DELAY;
|
||||
NextStream* next;
|
||||
if (xQueueReceive(next_stream_, &next, wait_time)) {
|
||||
// Copy the data out of the queue, then clean up the item.
|
||||
std::shared_ptr<TaggedStream> new_stream = next->stream;
|
||||
delete next;
|
||||
|
||||
// If we were already decoding, then make sure we finish up the current
|
||||
// file gracefully.
|
||||
if (stream_) {
|
||||
finishDecode(true);
|
||||
}
|
||||
|
||||
// Ensure there's actually stream data; we might have been given nullptr
|
||||
// as a signal to stop.
|
||||
if (!new_stream) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Start decoding the new stream.
|
||||
prepareDecode(new_stream);
|
||||
}
|
||||
|
||||
if (!continueDecode()) {
|
||||
finishDecode(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
auto Decoder::prepareDecode(std::shared_ptr<TaggedStream> stream) -> void {
|
||||
auto stub_track = std::make_shared<TrackInfo>(TrackInfo{
|
||||
.tags = stream->tags(),
|
||||
.uri = stream->Filepath(),
|
||||
.duration = {},
|
||||
.start_offset = {},
|
||||
.bitrate_kbps = {},
|
||||
.encoding = stream->type(),
|
||||
.format = {},
|
||||
});
|
||||
|
||||
codec_.reset(codecs::CreateCodecForType(stream->type()).value_or(nullptr));
|
||||
if (!codec_) {
|
||||
ESP_LOGE(kTag, "no codec found for stream");
|
||||
events::Audio().Dispatch(
|
||||
internal::DecodingFailedToStart{.track = stub_track});
|
||||
return;
|
||||
}
|
||||
|
||||
auto open_res = codec_->OpenStream(stream, stream->Offset());
|
||||
if (open_res.has_error()) {
|
||||
ESP_LOGE(kTag, "codec failed to start: %s",
|
||||
codecs::ICodec::ErrorString(open_res.error()).c_str());
|
||||
events::Audio().Dispatch(
|
||||
internal::DecodingFailedToStart{.track = stub_track});
|
||||
return;
|
||||
}
|
||||
|
||||
// Decoding started okay! Fill out the rest of the track info for this
|
||||
// stream.
|
||||
stream_ = stream;
|
||||
track_ = std::make_shared<TrackInfo>(TrackInfo{
|
||||
.tags = stream->tags(),
|
||||
.uri = stream->Filepath(),
|
||||
.duration = {},
|
||||
.start_offset = stream->Offset(),
|
||||
.bitrate_kbps = {},
|
||||
.encoding = stream->type(),
|
||||
.format =
|
||||
{
|
||||
.sample_rate = open_res->sample_rate_hz,
|
||||
.num_channels = open_res->num_channels,
|
||||
.bits_per_sample = 16,
|
||||
},
|
||||
});
|
||||
|
||||
if (open_res->total_samples) {
|
||||
track_->duration = open_res->total_samples.value() /
|
||||
open_res->num_channels / open_res->sample_rate_hz;
|
||||
}
|
||||
|
||||
events::Audio().Dispatch(internal::DecodingStarted{.track = track_});
|
||||
processor_->beginStream(track_);
|
||||
}
|
||||
|
||||
auto Decoder::continueDecode() -> bool {
|
||||
auto res = codec_->DecodeTo(codec_buffer_);
|
||||
if (res.has_error()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (res->samples_written > 0) {
|
||||
processor_->continueStream(codec_buffer_.first(res->samples_written));
|
||||
}
|
||||
|
||||
return !res->is_stream_finished;
|
||||
}
|
||||
|
||||
auto Decoder::finishDecode(bool cancel) -> void {
|
||||
assert(track_);
|
||||
|
||||
// Tell everyone we're finished.
|
||||
if (cancel) {
|
||||
events::Audio().Dispatch(internal::DecodingCancelled{.track = track_});
|
||||
} else {
|
||||
events::Audio().Dispatch(internal::DecodingFinished{.track = track_});
|
||||
}
|
||||
processor_->endStream(cancel);
|
||||
|
||||
// Clean up after ourselves.
|
||||
stream_.reset();
|
||||
codec_.reset();
|
||||
track_.reset();
|
||||
}
|
||||
|
||||
} // namespace audio
|
||||
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* Copyright 2023 jacqueline <me@jacqueline.id.au>
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-only
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
|
||||
#include "audio/audio_events.hpp"
|
||||
#include "audio/audio_sink.hpp"
|
||||
#include "audio/audio_source.hpp"
|
||||
#include "audio/processor.hpp"
|
||||
#include "codec.hpp"
|
||||
#include "database/track.hpp"
|
||||
#include "types.hpp"
|
||||
|
||||
namespace audio {
|
||||
|
||||
/*
|
||||
* Handle to a persistent task that takes encoded bytes from arbitrary sources,
|
||||
* decodes them into sample::Sample (normalised to 16 bit signed PCM), and then
|
||||
* streams them onward to the sample processor.
|
||||
*/
|
||||
class Decoder {
|
||||
public:
|
||||
static auto Start(std::shared_ptr<SampleProcessor>) -> Decoder*;
|
||||
|
||||
auto open(std::shared_ptr<TaggedStream>) -> void;
|
||||
|
||||
Decoder(const Decoder&) = delete;
|
||||
Decoder& operator=(const Decoder&) = delete;
|
||||
|
||||
private:
|
||||
Decoder(std::shared_ptr<SampleProcessor>);
|
||||
|
||||
auto Main() -> void;
|
||||
|
||||
auto prepareDecode(std::shared_ptr<TaggedStream>) -> void;
|
||||
auto continueDecode() -> bool;
|
||||
auto finishDecode(bool cancel) -> void;
|
||||
|
||||
std::shared_ptr<SampleProcessor> processor_;
|
||||
|
||||
// Struct used with the next_stream_ queue.
|
||||
struct NextStream {
|
||||
std::shared_ptr<TaggedStream> stream;
|
||||
};
|
||||
QueueHandle_t next_stream_;
|
||||
|
||||
std::shared_ptr<codecs::IStream> stream_;
|
||||
std::unique_ptr<codecs::ICodec> codec_;
|
||||
std::shared_ptr<TrackInfo> track_;
|
||||
|
||||
std::span<sample::Sample> codec_buffer_;
|
||||
};
|
||||
|
||||
} // namespace audio
|
||||
@@ -12,10 +12,10 @@
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
#include "audio_sink.hpp"
|
||||
#include "audio/audio_sink.hpp"
|
||||
#include "tinyfsm.hpp"
|
||||
|
||||
#include "track.hpp"
|
||||
#include "database/track.hpp"
|
||||
#include "types.hpp"
|
||||
|
||||
namespace audio {
|
||||
@@ -84,13 +84,6 @@ struct PlaybackUpdate : tinyfsm::Event {
|
||||
struct SetTrack : tinyfsm::Event {
|
||||
std::variant<std::string, database::TrackId, std::monostate> new_track;
|
||||
std::optional<uint32_t> seek_to_second;
|
||||
|
||||
enum Transition {
|
||||
kHardCut,
|
||||
kGapless,
|
||||
// TODO: kCrossFade
|
||||
};
|
||||
Transition transition;
|
||||
};
|
||||
|
||||
struct TogglePlayPause : tinyfsm::Event {
|
||||
@@ -138,17 +131,33 @@ struct OutputModeChanged : tinyfsm::Event {};
|
||||
|
||||
namespace internal {
|
||||
|
||||
struct DecodingStarted : tinyfsm::Event {
|
||||
std::shared_ptr<TrackInfo> track;
|
||||
};
|
||||
|
||||
struct DecodingFailedToStart : tinyfsm::Event {
|
||||
std::shared_ptr<TrackInfo> track;
|
||||
};
|
||||
|
||||
struct DecodingCancelled : tinyfsm::Event {
|
||||
std::shared_ptr<TrackInfo> track;
|
||||
};
|
||||
|
||||
struct DecodingFinished : tinyfsm::Event {
|
||||
std::shared_ptr<TrackInfo> track;
|
||||
};
|
||||
|
||||
struct StreamStarted : tinyfsm::Event {
|
||||
std::shared_ptr<TrackInfo> track;
|
||||
IAudioOutput::Format src_format;
|
||||
IAudioOutput::Format dst_format;
|
||||
IAudioOutput::Format sink_format;
|
||||
uint32_t cue_at_sample;
|
||||
};
|
||||
|
||||
struct StreamUpdate : tinyfsm::Event {
|
||||
uint32_t samples_sunk;
|
||||
struct StreamEnded : tinyfsm::Event {
|
||||
uint32_t cue_at_sample;
|
||||
};
|
||||
|
||||
struct StreamEnded : tinyfsm::Event {};
|
||||
struct StreamHeartbeat : tinyfsm::Event {};
|
||||
|
||||
} // namespace internal
|
||||
|
||||
@@ -4,15 +4,13 @@
|
||||
* SPDX-License-Identifier: GPL-3.0-only
|
||||
*/
|
||||
|
||||
#include "audio_fsm.hpp"
|
||||
#include <stdint.h>
|
||||
#include "audio/audio_fsm.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
#include <future>
|
||||
#include <memory>
|
||||
#include <variant>
|
||||
|
||||
#include "audio_sink.hpp"
|
||||
#include "bluetooth_types.hpp"
|
||||
#include "cppbor.h"
|
||||
#include "cppbor_parse.h"
|
||||
#include "esp_heap_caps.h"
|
||||
@@ -20,25 +18,28 @@
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/portmacro.h"
|
||||
#include "freertos/projdefs.h"
|
||||
|
||||
#include "audio_converter.hpp"
|
||||
#include "audio_decoder.hpp"
|
||||
#include "audio_events.hpp"
|
||||
#include "bluetooth.hpp"
|
||||
#include "bt_audio_output.hpp"
|
||||
#include "event_queue.hpp"
|
||||
#include "fatfs_audio_input.hpp"
|
||||
#include "future_fetcher.hpp"
|
||||
#include "i2s_audio_output.hpp"
|
||||
#include "i2s_dac.hpp"
|
||||
#include "nvs.hpp"
|
||||
#include "sample.hpp"
|
||||
#include "service_locator.hpp"
|
||||
#include "system_events.hpp"
|
||||
#include "tinyfsm.hpp"
|
||||
#include "track.hpp"
|
||||
#include "track_queue.hpp"
|
||||
#include "wm8523.hpp"
|
||||
|
||||
#include "audio/audio_decoder.hpp"
|
||||
#include "audio/audio_events.hpp"
|
||||
#include "audio/audio_sink.hpp"
|
||||
#include "audio/bt_audio_output.hpp"
|
||||
#include "audio/fatfs_stream_factory.hpp"
|
||||
#include "audio/i2s_audio_output.hpp"
|
||||
#include "audio/stream_cues.hpp"
|
||||
#include "audio/track_queue.hpp"
|
||||
#include "database/future_fetcher.hpp"
|
||||
#include "database/track.hpp"
|
||||
#include "drivers/bluetooth.hpp"
|
||||
#include "drivers/bluetooth_types.hpp"
|
||||
#include "drivers/i2s_dac.hpp"
|
||||
#include "drivers/nvs.hpp"
|
||||
#include "drivers/storage.hpp"
|
||||
#include "drivers/wm8523.hpp"
|
||||
#include "events/event_queue.hpp"
|
||||
#include "sample.hpp"
|
||||
#include "system_fsm/service_locator.hpp"
|
||||
#include "system_fsm/system_events.hpp"
|
||||
|
||||
namespace audio {
|
||||
|
||||
@@ -46,12 +47,14 @@ namespace audio {
|
||||
|
||||
std::shared_ptr<system_fsm::ServiceLocator> AudioState::sServices;
|
||||
|
||||
std::shared_ptr<FatfsAudioInput> AudioState::sFileSource;
|
||||
std::shared_ptr<FatfsStreamFactory> AudioState::sStreamFactory;
|
||||
|
||||
std::unique_ptr<Decoder> AudioState::sDecoder;
|
||||
std::shared_ptr<SampleConverter> AudioState::sSampleConverter;
|
||||
std::shared_ptr<SampleProcessor> AudioState::sSampleProcessor;
|
||||
|
||||
std::shared_ptr<IAudioOutput> AudioState::sOutput;
|
||||
std::shared_ptr<I2SAudioOutput> AudioState::sI2SOutput;
|
||||
std::shared_ptr<BluetoothAudioOutput> AudioState::sBtOutput;
|
||||
std::shared_ptr<IAudioOutput> AudioState::sOutput;
|
||||
|
||||
// Two seconds of samples for two channels, at a representative sample rate.
|
||||
constexpr size_t kDrainLatencySamples = 48000 * 2 * 2;
|
||||
@@ -61,30 +64,33 @@ constexpr size_t kDrainBufferSize =
|
||||
StreamBufferHandle_t AudioState::sDrainBuffer;
|
||||
std::optional<IAudioOutput::Format> AudioState::sDrainFormat;
|
||||
|
||||
std::shared_ptr<TrackInfo> AudioState::sCurrentTrack;
|
||||
uint64_t AudioState::sCurrentSamples;
|
||||
bool AudioState::sCurrentTrackIsFromQueue;
|
||||
StreamCues AudioState::sStreamCues;
|
||||
|
||||
std::shared_ptr<TrackInfo> AudioState::sNextTrack;
|
||||
uint64_t AudioState::sNextTrackCueSamples;
|
||||
bool AudioState::sNextTrackIsFromQueue;
|
||||
|
||||
bool AudioState::sIsResampling;
|
||||
bool AudioState::sIsPaused = true;
|
||||
|
||||
auto AudioState::currentPositionSeconds() -> std::optional<uint32_t> {
|
||||
if (!sCurrentTrack || !sDrainFormat) {
|
||||
return {};
|
||||
auto AudioState::emitPlaybackUpdate(bool paused) -> void {
|
||||
std::optional<uint32_t> position;
|
||||
auto current = sStreamCues.current();
|
||||
if (current.first && sDrainFormat) {
|
||||
position = (current.second /
|
||||
(sDrainFormat->num_channels * sDrainFormat->sample_rate)) +
|
||||
current.first->start_offset.value_or(0);
|
||||
}
|
||||
return sCurrentSamples /
|
||||
(sDrainFormat->num_channels * sDrainFormat->sample_rate);
|
||||
|
||||
PlaybackUpdate event{
|
||||
.current_track = current.first,
|
||||
.track_position = position,
|
||||
.paused = paused,
|
||||
};
|
||||
|
||||
events::System().Dispatch(event);
|
||||
events::Ui().Dispatch(event);
|
||||
}
|
||||
|
||||
void AudioState::react(const QueueUpdate& ev) {
|
||||
SetTrack cmd{
|
||||
.new_track = std::monostate{},
|
||||
.seek_to_second = {},
|
||||
.transition = SetTrack::Transition::kHardCut,
|
||||
};
|
||||
|
||||
auto current = sServices->track_queue().current();
|
||||
@@ -97,20 +103,13 @@ void AudioState::react(const QueueUpdate& ev) {
|
||||
if (!ev.current_changed) {
|
||||
return;
|
||||
}
|
||||
sNextTrackIsFromQueue = true;
|
||||
cmd.transition = SetTrack::Transition::kHardCut;
|
||||
break;
|
||||
case QueueUpdate::kRepeatingLastTrack:
|
||||
sNextTrackIsFromQueue = true;
|
||||
cmd.transition = SetTrack::Transition::kGapless;
|
||||
break;
|
||||
case QueueUpdate::kTrackFinished:
|
||||
if (!ev.current_changed) {
|
||||
cmd.new_track = std::monostate{};
|
||||
} else {
|
||||
sNextTrackIsFromQueue = true;
|
||||
}
|
||||
cmd.transition = SetTrack::Transition::kGapless;
|
||||
break;
|
||||
case QueueUpdate::kDeserialised:
|
||||
default:
|
||||
@@ -123,32 +122,9 @@ void AudioState::react(const QueueUpdate& ev) {
|
||||
}
|
||||
|
||||
void AudioState::react(const SetTrack& ev) {
|
||||
// Remember the current track if there is one, since we need to preserve some
|
||||
// of the state if it turns out this SetTrack event corresponds to seeking
|
||||
// within the current track.
|
||||
std::string prev_uri;
|
||||
bool prev_from_queue = false;
|
||||
if (sCurrentTrack) {
|
||||
prev_uri = sCurrentTrack->uri;
|
||||
prev_from_queue = sCurrentTrackIsFromQueue;
|
||||
}
|
||||
|
||||
if (ev.transition == SetTrack::Transition::kHardCut) {
|
||||
sCurrentTrack.reset();
|
||||
sCurrentSamples = 0;
|
||||
sCurrentTrackIsFromQueue = false;
|
||||
clearDrainBuffer();
|
||||
}
|
||||
|
||||
if (std::holds_alternative<std::monostate>(ev.new_track)) {
|
||||
ESP_LOGI(kTag, "playback finished, awaiting drain");
|
||||
sFileSource->SetPath();
|
||||
awaitEmptyDrainBuffer();
|
||||
sCurrentTrack.reset();
|
||||
sDrainFormat.reset();
|
||||
sCurrentSamples = 0;
|
||||
sCurrentTrackIsFromQueue = false;
|
||||
transit<states::Standby>();
|
||||
sDecoder->open({});
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -157,96 +133,76 @@ void AudioState::react(const SetTrack& ev) {
|
||||
auto new_track = ev.new_track;
|
||||
uint32_t seek_to = ev.seek_to_second.value_or(0);
|
||||
sServices->bg_worker().Dispatch<void>([=]() {
|
||||
std::optional<std::string> path;
|
||||
std::shared_ptr<TaggedStream> stream;
|
||||
if (std::holds_alternative<database::TrackId>(new_track)) {
|
||||
auto db = sServices->database().lock();
|
||||
if (db) {
|
||||
path = db->getTrackPath(std::get<database::TrackId>(new_track));
|
||||
}
|
||||
stream = sStreamFactory->create(std::get<database::TrackId>(new_track),
|
||||
seek_to);
|
||||
} else if (std::holds_alternative<std::string>(new_track)) {
|
||||
path = std::get<std::string>(new_track);
|
||||
stream =
|
||||
sStreamFactory->create(std::get<std::string>(new_track), seek_to);
|
||||
}
|
||||
|
||||
if (path) {
|
||||
if (*path == prev_uri) {
|
||||
// This was a seek or replay within the same track; don't forget where
|
||||
// the track originally came from.
|
||||
sNextTrackIsFromQueue = prev_from_queue;
|
||||
}
|
||||
sFileSource->SetPath(*path, seek_to);
|
||||
} else {
|
||||
sFileSource->SetPath();
|
||||
}
|
||||
sDecoder->open(stream);
|
||||
});
|
||||
}
|
||||
|
||||
void AudioState::react(const TogglePlayPause& ev) {
|
||||
sIsPaused = !ev.set_to.value_or(sIsPaused);
|
||||
if (!sIsPaused && is_in_state<states::Standby>() && sCurrentTrack) {
|
||||
if (!sIsPaused && is_in_state<states::Standby>() &&
|
||||
sStreamCues.current().first) {
|
||||
transit<states::Playback>();
|
||||
} else if (sIsPaused && is_in_state<states::Playback>()) {
|
||||
transit<states::Standby>();
|
||||
}
|
||||
}
|
||||
|
||||
void AudioState::react(const internal::DecodingFinished& ev) {
|
||||
// If we just finished playing whatever's at the front of the queue, then we
|
||||
// need to advanve and start playing the next one ASAP in order to continue
|
||||
// gaplessly.
|
||||
sServices->bg_worker().Dispatch<void>([=]() {
|
||||
auto& queue = sServices->track_queue();
|
||||
auto current = queue.current();
|
||||
if (!current) {
|
||||
return;
|
||||
}
|
||||
auto db = sServices->database().lock();
|
||||
if (!db) {
|
||||
return;
|
||||
}
|
||||
auto path = db->getTrackPath(*current);
|
||||
if (!path) {
|
||||
return;
|
||||
}
|
||||
if (*path == ev.track->uri) {
|
||||
queue.finish();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void AudioState::react(const internal::StreamStarted& ev) {
|
||||
sDrainFormat = ev.dst_format;
|
||||
sIsResampling = ev.src_format != ev.dst_format;
|
||||
|
||||
sNextTrack = ev.track;
|
||||
sNextTrackCueSamples = sCurrentSamples + (kDrainLatencySamples / 2);
|
||||
|
||||
ESP_LOGI(kTag, "new stream %s %u ch @ %lu hz (resample=%i)",
|
||||
ev.track->uri.c_str(), sDrainFormat->num_channels,
|
||||
sDrainFormat->sample_rate, sIsResampling);
|
||||
}
|
||||
|
||||
void AudioState::react(const internal::StreamEnded&) {
|
||||
ESP_LOGI(kTag, "stream ended");
|
||||
|
||||
if (sCurrentTrackIsFromQueue) {
|
||||
sServices->track_queue().finish();
|
||||
} else {
|
||||
tinyfsm::FsmList<AudioState>::dispatch(SetTrack{
|
||||
.new_track = std::monostate{},
|
||||
.seek_to_second = {},
|
||||
.transition = SetTrack::Transition::kGapless,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void AudioState::react(const internal::StreamUpdate& ev) {
|
||||
sCurrentSamples += ev.samples_sunk;
|
||||
|
||||
if (sNextTrack && sCurrentSamples >= sNextTrackCueSamples) {
|
||||
ESP_LOGI(kTag, "next track is now sinking");
|
||||
sCurrentTrack = sNextTrack;
|
||||
sCurrentSamples -= sNextTrackCueSamples;
|
||||
sCurrentSamples += sNextTrack->start_offset.value_or(0) *
|
||||
(sDrainFormat->num_channels * sDrainFormat->sample_rate);
|
||||
sCurrentTrackIsFromQueue = sNextTrackIsFromQueue;
|
||||
|
||||
sNextTrack.reset();
|
||||
sNextTrackCueSamples = 0;
|
||||
sNextTrackIsFromQueue = false;
|
||||
if (sDrainFormat != ev.sink_format) {
|
||||
sDrainFormat = ev.sink_format;
|
||||
ESP_LOGI(kTag, "sink_format=%u ch @ %lu hz", sDrainFormat->num_channels,
|
||||
sDrainFormat->sample_rate);
|
||||
}
|
||||
|
||||
if (sCurrentTrack) {
|
||||
PlaybackUpdate event{
|
||||
.current_track = sCurrentTrack,
|
||||
.track_position = currentPositionSeconds(),
|
||||
.paused = !is_in_state<states::Playback>(),
|
||||
};
|
||||
events::System().Dispatch(event);
|
||||
events::Ui().Dispatch(event);
|
||||
}
|
||||
sStreamCues.addCue(ev.track, ev.cue_at_sample);
|
||||
|
||||
if (sCurrentTrack && !sIsPaused && !is_in_state<states::Playback>()) {
|
||||
ESP_LOGI(kTag, "ready to play!");
|
||||
if (!sIsPaused && !is_in_state<states::Playback>()) {
|
||||
transit<states::Playback>();
|
||||
} else {
|
||||
// Make sure everyone knows we've got a track ready to go, even if we're
|
||||
// not playing it yet. This mostly matters when restoring the queue from
|
||||
// disk after booting.
|
||||
emitPlaybackUpdate(true);
|
||||
}
|
||||
}
|
||||
|
||||
void AudioState::react(const internal::StreamEnded& ev) {
|
||||
sStreamCues.addCue({}, ev.cue_at_sample);
|
||||
}
|
||||
|
||||
void AudioState::react(const system_fsm::BluetoothEvent& ev) {
|
||||
if (ev.event != drivers::bluetooth::Event::kConnectionStateChanged) {
|
||||
return;
|
||||
@@ -282,14 +238,6 @@ void AudioState::react(const StepDownVolume& ev) {
|
||||
}
|
||||
}
|
||||
|
||||
void AudioState::react(const system_fsm::HasPhonesChanged& ev) {
|
||||
if (ev.has_headphones) {
|
||||
ESP_LOGI(kTag, "headphones in!");
|
||||
} else {
|
||||
ESP_LOGI(kTag, "headphones out!");
|
||||
}
|
||||
}
|
||||
|
||||
void AudioState::react(const SetVolume& ev) {
|
||||
if (ev.db.has_value()) {
|
||||
if (sOutput->SetVolumeDb(ev.db.value())) {
|
||||
@@ -349,7 +297,7 @@ void AudioState::react(const OutputModeChanged& ev) {
|
||||
break;
|
||||
}
|
||||
sOutput->mode(IAudioOutput::Modes::kOnPaused);
|
||||
sSampleConverter->SetOutput(sOutput);
|
||||
sSampleProcessor->SetOutput(sOutput);
|
||||
|
||||
// Bluetooth volume isn't 'changed' until we've connected to a device.
|
||||
if (new_mode == drivers::NvsStorage::Output::kHeadphones) {
|
||||
@@ -360,43 +308,6 @@ void AudioState::react(const OutputModeChanged& ev) {
|
||||
}
|
||||
}
|
||||
|
||||
auto AudioState::clearDrainBuffer() -> void {
|
||||
// Tell the decoder to stop adding new samples. This might not take effect
|
||||
// immediately, since the decoder might currently be stuck waiting for space
|
||||
// to become available in the drain buffer.
|
||||
sFileSource->SetPath();
|
||||
|
||||
auto mode = sOutput->mode();
|
||||
if (mode == IAudioOutput::Modes::kOnPlaying) {
|
||||
// If we're currently playing, then the drain buffer will be actively
|
||||
// draining on its own. Just keep trying to reset until it works.
|
||||
while (xStreamBufferReset(sDrainBuffer) != pdPASS) {
|
||||
}
|
||||
} else {
|
||||
// If we're not currently playing, then we need to actively pull samples
|
||||
// out of the drain buffer to unblock the decoder.
|
||||
while (!xStreamBufferIsEmpty(sDrainBuffer)) {
|
||||
// Read a little to unblock the decoder.
|
||||
uint8_t drain[2048];
|
||||
xStreamBufferReceive(sDrainBuffer, drain, sizeof(drain), 0);
|
||||
|
||||
// Try to quickly discard the rest.
|
||||
xStreamBufferReset(sDrainBuffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
auto AudioState::awaitEmptyDrainBuffer() -> void {
|
||||
if (is_in_state<states::Playback>()) {
|
||||
for (int i = 0; i < 10 && !xStreamBufferIsEmpty(sDrainBuffer); i++) {
|
||||
vTaskDelay(pdMS_TO_TICKS(250));
|
||||
}
|
||||
}
|
||||
if (!xStreamBufferIsEmpty(sDrainBuffer)) {
|
||||
clearDrainBuffer();
|
||||
}
|
||||
}
|
||||
|
||||
auto AudioState::commitVolume() -> void {
|
||||
auto mode = sServices->nvs().OutputMode();
|
||||
auto vol = sOutput->GetVolume();
|
||||
@@ -427,8 +338,7 @@ void Uninitialised::react(const system_fsm::BootComplete& ev) {
|
||||
sDrainBuffer = xStreamBufferCreateStatic(
|
||||
kDrainBufferSize, sizeof(sample::Sample), storage, meta);
|
||||
|
||||
sFileSource.reset(
|
||||
new FatfsAudioInput(sServices->tag_parser(), sServices->bg_worker()));
|
||||
sStreamFactory.reset(new FatfsStreamFactory(*sServices));
|
||||
sI2SOutput.reset(new I2SAudioOutput(sDrainBuffer, sServices->gpios()));
|
||||
sBtOutput.reset(new BluetoothAudioOutput(sDrainBuffer, sServices->bluetooth(),
|
||||
sServices->bg_worker()));
|
||||
@@ -462,10 +372,10 @@ void Uninitialised::react(const system_fsm::BootComplete& ev) {
|
||||
.left_bias = nvs.AmpLeftBias(),
|
||||
});
|
||||
|
||||
sSampleConverter.reset(new SampleConverter());
|
||||
sSampleConverter->SetOutput(sOutput);
|
||||
sSampleProcessor.reset(new SampleProcessor(sDrainBuffer));
|
||||
sSampleProcessor->SetOutput(sOutput);
|
||||
|
||||
Decoder::Start(sFileSource, sSampleConverter);
|
||||
sDecoder.reset(Decoder::Start(sSampleProcessor));
|
||||
|
||||
transit<Standby>();
|
||||
}
|
||||
@@ -477,7 +387,8 @@ void Standby::react(const system_fsm::KeyLockChanged& ev) {
|
||||
if (!ev.locking) {
|
||||
return;
|
||||
}
|
||||
sServices->bg_worker().Dispatch<void>([this]() {
|
||||
auto current = sStreamCues.current();
|
||||
sServices->bg_worker().Dispatch<void>([=]() {
|
||||
auto db = sServices->database().lock();
|
||||
if (!db) {
|
||||
return;
|
||||
@@ -490,17 +401,24 @@ void Standby::react(const system_fsm::KeyLockChanged& ev) {
|
||||
}
|
||||
db->put(kQueueKey, queue.serialise());
|
||||
|
||||
if (sCurrentTrack) {
|
||||
if (current.first && sDrainFormat) {
|
||||
uint32_t seconds = (current.second / (sDrainFormat->num_channels *
|
||||
sDrainFormat->sample_rate)) +
|
||||
current.first->start_offset.value_or(0);
|
||||
cppbor::Array current_track{
|
||||
cppbor::Tstr{sCurrentTrack->uri},
|
||||
cppbor::Uint{currentPositionSeconds().value_or(0)},
|
||||
cppbor::Tstr{current.first->uri},
|
||||
cppbor::Uint{seconds},
|
||||
};
|
||||
db->put(kCurrentFileKey, current_track.toString());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void Standby::react(const system_fsm::StorageMounted& ev) {
|
||||
void Standby::react(const system_fsm::SdStateChanged& ev) {
|
||||
auto state = sServices->sd();
|
||||
if (state != drivers::SdState::kMounted) {
|
||||
return;
|
||||
}
|
||||
sServices->bg_worker().Dispatch<void>([]() {
|
||||
auto db = sServices->database().lock();
|
||||
if (!db) {
|
||||
@@ -524,7 +442,6 @@ void Standby::react(const system_fsm::StorageMounted& ev) {
|
||||
events::Audio().Dispatch(SetTrack{
|
||||
.new_track = filename,
|
||||
.seek_to_second = pos,
|
||||
.transition = SetTrack::Transition::kHardCut,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -540,32 +457,47 @@ void Standby::react(const system_fsm::StorageMounted& ev) {
|
||||
});
|
||||
}
|
||||
|
||||
static TimerHandle_t sHeartbeatTimer;
|
||||
|
||||
static void heartbeat(TimerHandle_t) {
|
||||
events::Audio().Dispatch(internal::StreamHeartbeat{});
|
||||
}
|
||||
|
||||
void Playback::entry() {
|
||||
ESP_LOGI(kTag, "audio output resumed");
|
||||
sOutput->mode(IAudioOutput::Modes::kOnPlaying);
|
||||
emitPlaybackUpdate(false);
|
||||
|
||||
PlaybackUpdate event{
|
||||
.current_track = sCurrentTrack,
|
||||
.track_position = currentPositionSeconds(),
|
||||
.paused = false,
|
||||
};
|
||||
|
||||
events::System().Dispatch(event);
|
||||
events::Ui().Dispatch(event);
|
||||
if (!sHeartbeatTimer) {
|
||||
sHeartbeatTimer =
|
||||
xTimerCreate("stream", pdMS_TO_TICKS(250), true, NULL, heartbeat);
|
||||
}
|
||||
xTimerStart(sHeartbeatTimer, portMAX_DELAY);
|
||||
}
|
||||
|
||||
void Playback::exit() {
|
||||
ESP_LOGI(kTag, "audio output paused");
|
||||
xTimerStop(sHeartbeatTimer, portMAX_DELAY);
|
||||
sOutput->mode(IAudioOutput::Modes::kOnPaused);
|
||||
emitPlaybackUpdate(true);
|
||||
}
|
||||
|
||||
PlaybackUpdate event{
|
||||
.current_track = sCurrentTrack,
|
||||
.track_position = currentPositionSeconds(),
|
||||
.paused = true,
|
||||
};
|
||||
void Playback::react(const system_fsm::SdStateChanged& ev) {
|
||||
if (sServices->sd() != drivers::SdState::kMounted) {
|
||||
transit<Standby>();
|
||||
}
|
||||
}
|
||||
|
||||
events::System().Dispatch(event);
|
||||
events::Ui().Dispatch(event);
|
||||
void Playback::react(const internal::StreamHeartbeat& ev) {
|
||||
sStreamCues.update(sOutput->samplesUsed());
|
||||
|
||||
if (sStreamCues.hasStream()) {
|
||||
emitPlaybackUpdate(false);
|
||||
} else {
|
||||
// Finished the current stream, and there's nothing upcoming. We must be
|
||||
// finished.
|
||||
transit<Standby>();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace states
|
||||
@@ -11,24 +11,25 @@
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "audio_sink.hpp"
|
||||
#include "service_locator.hpp"
|
||||
#include "audio/stream_cues.hpp"
|
||||
#include "tinyfsm.hpp"
|
||||
|
||||
#include "audio_decoder.hpp"
|
||||
#include "audio_events.hpp"
|
||||
#include "bt_audio_output.hpp"
|
||||
#include "database.hpp"
|
||||
#include "display.hpp"
|
||||
#include "fatfs_audio_input.hpp"
|
||||
#include "gpios.hpp"
|
||||
#include "i2s_audio_output.hpp"
|
||||
#include "i2s_dac.hpp"
|
||||
#include "storage.hpp"
|
||||
#include "system_events.hpp"
|
||||
#include "tag_parser.hpp"
|
||||
#include "track.hpp"
|
||||
#include "track_queue.hpp"
|
||||
#include "audio/audio_decoder.hpp"
|
||||
#include "audio/audio_events.hpp"
|
||||
#include "audio/audio_sink.hpp"
|
||||
#include "audio/bt_audio_output.hpp"
|
||||
#include "audio/fatfs_stream_factory.hpp"
|
||||
#include "audio/i2s_audio_output.hpp"
|
||||
#include "audio/track_queue.hpp"
|
||||
#include "database/database.hpp"
|
||||
#include "database/tag_parser.hpp"
|
||||
#include "database/track.hpp"
|
||||
#include "drivers/display.hpp"
|
||||
#include "drivers/gpios.hpp"
|
||||
#include "drivers/i2s_dac.hpp"
|
||||
#include "drivers/storage.hpp"
|
||||
#include "system_fsm/service_locator.hpp"
|
||||
#include "system_fsm/system_events.hpp"
|
||||
|
||||
namespace audio {
|
||||
|
||||
@@ -46,13 +47,13 @@ class AudioState : public tinyfsm::Fsm<AudioState> {
|
||||
void react(const SetTrack&);
|
||||
void react(const TogglePlayPause&);
|
||||
|
||||
void react(const internal::DecodingFinished&);
|
||||
void react(const internal::StreamStarted&);
|
||||
void react(const internal::StreamUpdate&);
|
||||
void react(const internal::StreamEnded&);
|
||||
virtual void react(const internal::StreamHeartbeat&) {}
|
||||
|
||||
void react(const StepUpVolume&);
|
||||
void react(const StepDownVolume&);
|
||||
virtual void react(const system_fsm::HasPhonesChanged&);
|
||||
|
||||
void react(const SetVolume&);
|
||||
void react(const SetVolumeLimit&);
|
||||
@@ -62,40 +63,28 @@ class AudioState : public tinyfsm::Fsm<AudioState> {
|
||||
|
||||
virtual void react(const system_fsm::BootComplete&) {}
|
||||
virtual void react(const system_fsm::KeyLockChanged&){};
|
||||
virtual void react(const system_fsm::StorageMounted&) {}
|
||||
virtual void react(const system_fsm::SdStateChanged&) {}
|
||||
virtual void react(const system_fsm::BluetoothEvent&);
|
||||
|
||||
protected:
|
||||
auto clearDrainBuffer() -> void;
|
||||
auto awaitEmptyDrainBuffer() -> void;
|
||||
|
||||
auto playTrack(database::TrackId id) -> void;
|
||||
auto emitPlaybackUpdate(bool paused) -> void;
|
||||
auto commitVolume() -> void;
|
||||
|
||||
static std::shared_ptr<system_fsm::ServiceLocator> sServices;
|
||||
|
||||
static std::shared_ptr<FatfsAudioInput> sFileSource;
|
||||
static std::shared_ptr<FatfsStreamFactory> sStreamFactory;
|
||||
static std::unique_ptr<Decoder> sDecoder;
|
||||
static std::shared_ptr<SampleConverter> sSampleConverter;
|
||||
static std::shared_ptr<SampleProcessor> sSampleProcessor;
|
||||
static std::shared_ptr<I2SAudioOutput> sI2SOutput;
|
||||
static std::shared_ptr<BluetoothAudioOutput> sBtOutput;
|
||||
static std::shared_ptr<IAudioOutput> sOutput;
|
||||
|
||||
static StreamBufferHandle_t sDrainBuffer;
|
||||
|
||||
static std::shared_ptr<TrackInfo> sCurrentTrack;
|
||||
static uint64_t sCurrentSamples;
|
||||
static StreamCues sStreamCues;
|
||||
static std::optional<IAudioOutput::Format> sDrainFormat;
|
||||
static bool sCurrentTrackIsFromQueue;
|
||||
|
||||
static std::shared_ptr<TrackInfo> sNextTrack;
|
||||
static uint64_t sNextTrackCueSamples;
|
||||
static bool sNextTrackIsFromQueue;
|
||||
|
||||
static bool sIsResampling;
|
||||
static bool sIsPaused;
|
||||
|
||||
auto currentPositionSeconds() -> std::optional<uint32_t>;
|
||||
};
|
||||
|
||||
namespace states {
|
||||
@@ -111,7 +100,7 @@ class Uninitialised : public AudioState {
|
||||
class Standby : public AudioState {
|
||||
public:
|
||||
void react(const system_fsm::KeyLockChanged&) override;
|
||||
void react(const system_fsm::StorageMounted&) override;
|
||||
void react(const system_fsm::SdStateChanged&) override;
|
||||
|
||||
using AudioState::react;
|
||||
};
|
||||
@@ -121,6 +110,9 @@ class Playback : public AudioState {
|
||||
void entry() override;
|
||||
void exit() override;
|
||||
|
||||
void react(const system_fsm::SdStateChanged&) override;
|
||||
void react(const internal::StreamHeartbeat&) override;
|
||||
|
||||
using AudioState::react;
|
||||
};
|
||||
|
||||
@@ -75,6 +75,7 @@ class IAudioOutput {
|
||||
|
||||
virtual auto PrepareFormat(const Format&) -> Format = 0;
|
||||
virtual auto Configure(const Format& format) -> void = 0;
|
||||
virtual auto samplesUsed() -> uint32_t = 0;
|
||||
|
||||
auto stream() -> StreamBufferHandle_t { return stream_; }
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
* SPDX-License-Identifier: GPL-3.0-only
|
||||
*/
|
||||
|
||||
#include "audio_source.hpp"
|
||||
#include "audio/audio_source.hpp"
|
||||
#include "codec.hpp"
|
||||
#include "types.hpp"
|
||||
|
||||
@@ -14,13 +14,17 @@ TaggedStream::TaggedStream(std::shared_ptr<database::TrackTags> t,
|
||||
std::unique_ptr<codecs::IStream> w,
|
||||
std::string filepath,
|
||||
uint32_t offset)
|
||||
: codecs::IStream(w->type()), tags_(t), wrapped_(std::move(w)), filepath_(filepath), offset_(offset) {}
|
||||
: codecs::IStream(w->type()),
|
||||
tags_(t),
|
||||
wrapped_(std::move(w)),
|
||||
filepath_(filepath),
|
||||
offset_(offset) {}
|
||||
|
||||
auto TaggedStream::tags() -> std::shared_ptr<database::TrackTags> {
|
||||
return tags_;
|
||||
}
|
||||
|
||||
auto TaggedStream::Read(cpp::span<std::byte> dest) -> ssize_t {
|
||||
auto TaggedStream::Read(std::span<std::byte> dest) -> ssize_t {
|
||||
return wrapped_->Read(dest);
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
|
||||
#include <memory>
|
||||
#include "codec.hpp"
|
||||
#include "track.hpp"
|
||||
#include "database/track.hpp"
|
||||
#include "types.hpp"
|
||||
|
||||
namespace audio {
|
||||
@@ -18,12 +18,11 @@ class TaggedStream : public codecs::IStream {
|
||||
TaggedStream(std::shared_ptr<database::TrackTags>,
|
||||
std::unique_ptr<codecs::IStream> wrapped,
|
||||
std::string path,
|
||||
uint32_t offset = 0
|
||||
);
|
||||
uint32_t offset = 0);
|
||||
|
||||
auto tags() -> std::shared_ptr<database::TrackTags>;
|
||||
|
||||
auto Read(cpp::span<std::byte> dest) -> ssize_t override;
|
||||
auto Read(std::span<std::byte> dest) -> ssize_t override;
|
||||
|
||||
auto CanSeek() -> bool override;
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
* SPDX-License-Identifier: GPL-3.0-only
|
||||
*/
|
||||
|
||||
#include "bt_audio_output.hpp"
|
||||
#include "audio/bt_audio_output.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
@@ -18,12 +18,12 @@
|
||||
#include "freertos/portmacro.h"
|
||||
#include "freertos/projdefs.h"
|
||||
|
||||
#include "gpios.hpp"
|
||||
#include "i2c.hpp"
|
||||
#include "i2s_dac.hpp"
|
||||
#include "drivers/gpios.hpp"
|
||||
#include "drivers/i2c.hpp"
|
||||
#include "drivers/i2s_dac.hpp"
|
||||
#include "drivers/wm8523.hpp"
|
||||
#include "result.hpp"
|
||||
#include "tasks.hpp"
|
||||
#include "wm8523.hpp"
|
||||
|
||||
[[maybe_unused]] static const char* kTag = "BTOUT";
|
||||
|
||||
@@ -121,4 +121,8 @@ auto BluetoothAudioOutput::Configure(const Format& fmt) -> void {
|
||||
// No configuration necessary; the output format is fixed.
|
||||
}
|
||||
|
||||
auto BluetoothAudioOutput::samplesUsed() -> uint32_t {
|
||||
return bluetooth_.SamplesUsed();
|
||||
}
|
||||
|
||||
} // namespace audio
|
||||
@@ -13,10 +13,10 @@
|
||||
|
||||
#include "result.hpp"
|
||||
|
||||
#include "audio_sink.hpp"
|
||||
#include "bluetooth.hpp"
|
||||
#include "gpios.hpp"
|
||||
#include "i2s_dac.hpp"
|
||||
#include "audio/audio_sink.hpp"
|
||||
#include "drivers/bluetooth.hpp"
|
||||
#include "drivers/gpios.hpp"
|
||||
#include "drivers/i2s_dac.hpp"
|
||||
#include "tasks.hpp"
|
||||
|
||||
namespace audio {
|
||||
@@ -45,6 +45,8 @@ class BluetoothAudioOutput : public IAudioOutput {
|
||||
auto PrepareFormat(const Format&) -> Format override;
|
||||
auto Configure(const Format& format) -> void override;
|
||||
|
||||
auto samplesUsed() -> uint32_t override;
|
||||
|
||||
BluetoothAudioOutput(const BluetoothAudioOutput&) = delete;
|
||||
BluetoothAudioOutput& operator=(const BluetoothAudioOutput&) = delete;
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
* SPDX-License-Identifier: GPL-3.0-only
|
||||
*/
|
||||
|
||||
#include "fatfs_source.hpp"
|
||||
#include "audio/fatfs_source.hpp"
|
||||
#include <sys/_stdint.h>
|
||||
|
||||
#include <cstddef>
|
||||
@@ -12,13 +12,13 @@
|
||||
#include <memory>
|
||||
|
||||
#include "esp_log.h"
|
||||
#include "event_queue.hpp"
|
||||
#include "events/event_queue.hpp"
|
||||
#include "ff.h"
|
||||
|
||||
#include "audio_source.hpp"
|
||||
#include "audio/audio_source.hpp"
|
||||
#include "codec.hpp"
|
||||
#include "spi.hpp"
|
||||
#include "system_events.hpp"
|
||||
#include "drivers/spi.hpp"
|
||||
#include "system_fsm/system_events.hpp"
|
||||
#include "types.hpp"
|
||||
|
||||
namespace audio {
|
||||
@@ -33,7 +33,7 @@ FatfsSource::~FatfsSource() {
|
||||
f_close(file_.get());
|
||||
}
|
||||
|
||||
auto FatfsSource::Read(cpp::span<std::byte> dest) -> ssize_t {
|
||||
auto FatfsSource::Read(std::span<std::byte> dest) -> ssize_t {
|
||||
auto lock = drivers::acquire_spi();
|
||||
if (f_eof(file_.get())) {
|
||||
return 0;
|
||||
@@ -13,7 +13,7 @@
|
||||
#include "codec.hpp"
|
||||
#include "ff.h"
|
||||
|
||||
#include "audio_source.hpp"
|
||||
#include "audio/audio_source.hpp"
|
||||
|
||||
namespace audio {
|
||||
|
||||
@@ -26,7 +26,7 @@ class FatfsSource : public codecs::IStream {
|
||||
FatfsSource(codecs::StreamType, std::unique_ptr<FIL> file);
|
||||
~FatfsSource();
|
||||
|
||||
auto Read(cpp::span<std::byte> dest) -> ssize_t override;
|
||||
auto Read(std::span<std::byte> dest) -> ssize_t override;
|
||||
|
||||
auto CanSeek() -> bool override;
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
/*
|
||||
* Copyright 2023 jacqueline <me@jacqueline.id.au>
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-only
|
||||
*/
|
||||
|
||||
#include "audio/fatfs_stream_factory.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "database/database.hpp"
|
||||
#include "esp_log.h"
|
||||
#include "ff.h"
|
||||
#include "freertos/portmacro.h"
|
||||
#include "freertos/projdefs.h"
|
||||
|
||||
#include "audio/audio_source.hpp"
|
||||
#include "audio/fatfs_source.hpp"
|
||||
#include "codec.hpp"
|
||||
#include "database/tag_parser.hpp"
|
||||
#include "database/track.hpp"
|
||||
#include "drivers/spi.hpp"
|
||||
#include "system_fsm/service_locator.hpp"
|
||||
#include "tasks.hpp"
|
||||
#include "types.hpp"
|
||||
|
||||
[[maybe_unused]] static const char* kTag = "SRC";
|
||||
|
||||
namespace audio {
|
||||
|
||||
FatfsStreamFactory::FatfsStreamFactory(system_fsm::ServiceLocator& services)
|
||||
: services_(services) {}
|
||||
|
||||
auto FatfsStreamFactory::create(database::TrackId id, uint32_t offset)
|
||||
-> std::shared_ptr<TaggedStream> {
|
||||
auto db = services_.database().lock();
|
||||
if (!db) {
|
||||
return {};
|
||||
}
|
||||
auto path = db->getTrackPath(id);
|
||||
if (!path) {
|
||||
return {};
|
||||
}
|
||||
return create(*path, offset);
|
||||
}
|
||||
|
||||
auto FatfsStreamFactory::create(std::string path, uint32_t offset)
|
||||
-> std::shared_ptr<TaggedStream> {
|
||||
auto tags = services_.tag_parser().ReadAndParseTags(path);
|
||||
if (!tags) {
|
||||
ESP_LOGE(kTag, "failed to read tags");
|
||||
return {};
|
||||
}
|
||||
|
||||
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 {};
|
||||
}
|
||||
|
||||
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 {};
|
||||
}
|
||||
|
||||
return std::make_shared<TaggedStream>(
|
||||
tags, std::make_unique<FatfsSource>(stream_type.value(), std::move(file)),
|
||||
path, offset);
|
||||
}
|
||||
|
||||
auto FatfsStreamFactory::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
|
||||
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Copyright 2023 jacqueline <me@jacqueline.id.au>
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-only
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <future>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "database/database.hpp"
|
||||
#include "database/track.hpp"
|
||||
#include "ff.h"
|
||||
#include "freertos/portmacro.h"
|
||||
|
||||
#include "audio/audio_source.hpp"
|
||||
#include "codec.hpp"
|
||||
#include "database/future_fetcher.hpp"
|
||||
#include "database/tag_parser.hpp"
|
||||
#include "system_fsm/service_locator.hpp"
|
||||
#include "tasks.hpp"
|
||||
#include "types.hpp"
|
||||
|
||||
namespace audio {
|
||||
|
||||
/*
|
||||
* Utility to create streams that read from files on the sd card.
|
||||
*/
|
||||
class FatfsStreamFactory {
|
||||
public:
|
||||
explicit FatfsStreamFactory(system_fsm::ServiceLocator&);
|
||||
|
||||
auto create(database::TrackId, uint32_t offset = 0)
|
||||
-> std::shared_ptr<TaggedStream>;
|
||||
auto create(std::string, uint32_t offset = 0)
|
||||
-> std::shared_ptr<TaggedStream>;
|
||||
|
||||
FatfsStreamFactory(const FatfsStreamFactory&) = delete;
|
||||
FatfsStreamFactory& operator=(const FatfsStreamFactory&) = delete;
|
||||
|
||||
private:
|
||||
auto ContainerToStreamType(database::Container)
|
||||
-> std::optional<codecs::StreamType>;
|
||||
|
||||
system_fsm::ServiceLocator& services_;
|
||||
};
|
||||
|
||||
} // namespace audio
|
||||
@@ -4,7 +4,7 @@
|
||||
* SPDX-License-Identifier: GPL-3.0-only
|
||||
*/
|
||||
|
||||
#include "i2s_audio_output.hpp"
|
||||
#include "audio/i2s_audio_output.hpp"
|
||||
#include <stdint.h>
|
||||
|
||||
#include <algorithm>
|
||||
@@ -18,12 +18,12 @@
|
||||
#include "freertos/portmacro.h"
|
||||
#include "freertos/projdefs.h"
|
||||
|
||||
#include "audio_sink.hpp"
|
||||
#include "gpios.hpp"
|
||||
#include "i2c.hpp"
|
||||
#include "i2s_dac.hpp"
|
||||
#include "audio/audio_sink.hpp"
|
||||
#include "drivers/gpios.hpp"
|
||||
#include "drivers/i2c.hpp"
|
||||
#include "drivers/i2s_dac.hpp"
|
||||
#include "drivers/wm8523.hpp"
|
||||
#include "result.hpp"
|
||||
#include "wm8523.hpp"
|
||||
|
||||
[[maybe_unused]] static const char* kTag = "I2SOUT";
|
||||
|
||||
@@ -120,7 +120,7 @@ auto I2SAudioOutput::SetVolumePct(uint_fast8_t val) -> bool {
|
||||
if (val > 100) {
|
||||
return false;
|
||||
}
|
||||
uint16_t vol = (val * (max_volume_ - kMinVolume))/100 + kMinVolume;
|
||||
uint16_t vol = (val * (max_volume_ - kMinVolume)) / 100 + kMinVolume;
|
||||
SetVolume(vol);
|
||||
return true;
|
||||
}
|
||||
@@ -133,7 +133,8 @@ auto I2SAudioOutput::GetVolumeDb() -> int_fast16_t {
|
||||
}
|
||||
|
||||
auto I2SAudioOutput::SetVolumeDb(int_fast16_t val) -> bool {
|
||||
SetVolume(val * 4 + static_cast<int>(drivers::wm8523::kLineLevelReferenceVolume) - 2);
|
||||
SetVolume(val * 4 +
|
||||
static_cast<int>(drivers::wm8523::kLineLevelReferenceVolume) - 2);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -229,4 +230,8 @@ auto I2SAudioOutput::Configure(const Format& fmt) -> void {
|
||||
current_config_ = fmt;
|
||||
}
|
||||
|
||||
auto I2SAudioOutput::samplesUsed() -> uint32_t {
|
||||
return dac_->SamplesUsed();
|
||||
}
|
||||
|
||||
} // namespace audio
|
||||
@@ -11,9 +11,9 @@
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "audio_sink.hpp"
|
||||
#include "gpios.hpp"
|
||||
#include "i2s_dac.hpp"
|
||||
#include "audio/audio_sink.hpp"
|
||||
#include "drivers/gpios.hpp"
|
||||
#include "drivers/i2s_dac.hpp"
|
||||
#include "result.hpp"
|
||||
|
||||
namespace audio {
|
||||
@@ -43,6 +43,8 @@ class I2SAudioOutput : public IAudioOutput {
|
||||
auto PrepareFormat(const Format&) -> Format override;
|
||||
auto Configure(const Format& format) -> void override;
|
||||
|
||||
auto samplesUsed() -> uint32_t override;
|
||||
|
||||
I2SAudioOutput(const I2SAudioOutput&) = delete;
|
||||
I2SAudioOutput& operator=(const I2SAudioOutput&) = delete;
|
||||
|
||||
@@ -4,23 +4,24 @@
|
||||
* SPDX-License-Identifier: GPL-3.0-only
|
||||
*/
|
||||
|
||||
#include "audio_converter.hpp"
|
||||
#include "audio/processor.hpp"
|
||||
#include <stdint.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <limits>
|
||||
|
||||
#include "audio_events.hpp"
|
||||
#include "audio_sink.hpp"
|
||||
#include "audio/audio_events.hpp"
|
||||
#include "audio/audio_sink.hpp"
|
||||
#include "drivers/i2s_dac.hpp"
|
||||
#include "esp_heap_caps.h"
|
||||
#include "esp_log.h"
|
||||
#include "event_queue.hpp"
|
||||
#include "events/event_queue.hpp"
|
||||
#include "freertos/portmacro.h"
|
||||
#include "freertos/projdefs.h"
|
||||
#include "i2s_dac.hpp"
|
||||
|
||||
#include "resample.hpp"
|
||||
#include "audio/resample.hpp"
|
||||
#include "sample.hpp"
|
||||
#include "tasks.hpp"
|
||||
|
||||
@@ -32,14 +33,15 @@ static constexpr std::size_t kSourceBufferLength = kSampleBufferLength * 2;
|
||||
|
||||
namespace audio {
|
||||
|
||||
SampleConverter::SampleConverter()
|
||||
SampleProcessor::SampleProcessor(StreamBufferHandle_t sink)
|
||||
: commands_(xQueueCreate(1, sizeof(Args))),
|
||||
resampler_(nullptr),
|
||||
source_(xStreamBufferCreateWithCaps(kSourceBufferLength,
|
||||
sizeof(sample::Sample) * 2,
|
||||
MALLOC_CAP_DMA)),
|
||||
sink_(sink),
|
||||
leftover_bytes_(0),
|
||||
samples_sunk_(0) {
|
||||
samples_written_(0) {
|
||||
input_buffer_ = {
|
||||
reinterpret_cast<sample::Sample*>(heap_caps_calloc(
|
||||
kSampleBufferLength, sizeof(sample::Sample), MALLOC_CAP_DMA)),
|
||||
@@ -55,47 +57,52 @@ SampleConverter::SampleConverter()
|
||||
tasks::StartPersistent<tasks::Type::kAudioConverter>([&]() { Main(); });
|
||||
}
|
||||
|
||||
SampleConverter::~SampleConverter() {
|
||||
SampleProcessor::~SampleProcessor() {
|
||||
vQueueDelete(commands_);
|
||||
vStreamBufferDelete(source_);
|
||||
}
|
||||
|
||||
auto SampleConverter::SetOutput(std::shared_ptr<IAudioOutput> output) -> void {
|
||||
// FIXME: We should add synchronisation here, but we should be careful about
|
||||
// not impacting performance given that the output will change only very
|
||||
// rarely (if ever).
|
||||
sink_ = output;
|
||||
auto SampleProcessor::SetOutput(std::shared_ptr<IAudioOutput> output) -> void {
|
||||
assert(xStreamBufferIsEmpty(sink_));
|
||||
// FIXME: We should add synchronisation here, but we should be careful
|
||||
// about not impacting performance given that the output will change only
|
||||
// very rarely (if ever).
|
||||
output_ = output;
|
||||
samples_written_ = output_->samplesUsed();
|
||||
}
|
||||
|
||||
auto SampleConverter::beginStream(std::shared_ptr<TrackInfo> track) -> void {
|
||||
auto SampleProcessor::beginStream(std::shared_ptr<TrackInfo> track) -> void {
|
||||
Args args{
|
||||
.track = new std::shared_ptr<TrackInfo>(track),
|
||||
.samples_available = 0,
|
||||
.is_end_of_stream = false,
|
||||
.clear_buffers = false,
|
||||
};
|
||||
xQueueSend(commands_, &args, portMAX_DELAY);
|
||||
}
|
||||
|
||||
auto SampleConverter::continueStream(cpp::span<sample::Sample> input) -> void {
|
||||
auto SampleProcessor::continueStream(std::span<sample::Sample> input) -> void {
|
||||
Args args{
|
||||
.track = nullptr,
|
||||
.samples_available = input.size(),
|
||||
.is_end_of_stream = false,
|
||||
.clear_buffers = false,
|
||||
};
|
||||
xQueueSend(commands_, &args, portMAX_DELAY);
|
||||
xStreamBufferSend(source_, input.data(), input.size_bytes(), portMAX_DELAY);
|
||||
}
|
||||
|
||||
auto SampleConverter::endStream() -> void {
|
||||
auto SampleProcessor::endStream(bool cancelled) -> void {
|
||||
Args args{
|
||||
.track = nullptr,
|
||||
.samples_available = 0,
|
||||
.is_end_of_stream = true,
|
||||
.clear_buffers = cancelled,
|
||||
};
|
||||
xQueueSend(commands_, &args, portMAX_DELAY);
|
||||
}
|
||||
|
||||
auto SampleConverter::Main() -> void {
|
||||
auto SampleProcessor::Main() -> void {
|
||||
for (;;) {
|
||||
Args args;
|
||||
while (!xQueueReceive(commands_, &args, portMAX_DELAY)) {
|
||||
@@ -109,43 +116,44 @@ auto SampleConverter::Main() -> void {
|
||||
handleContinueStream(args.samples_available);
|
||||
}
|
||||
if (args.is_end_of_stream) {
|
||||
handleEndStream();
|
||||
handleEndStream(args.clear_buffers);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
auto SampleConverter::handleBeginStream(std::shared_ptr<TrackInfo> track)
|
||||
auto SampleProcessor::handleBeginStream(std::shared_ptr<TrackInfo> track)
|
||||
-> void {
|
||||
if (track->format != source_format_) {
|
||||
resampler_.reset();
|
||||
source_format_ = track->format;
|
||||
// The new stream has a different format to the previous stream (or there
|
||||
// was no previous stream).
|
||||
// First, clean up our filters.
|
||||
resampler_.reset();
|
||||
leftover_bytes_ = 0;
|
||||
|
||||
auto new_target = sink_->PrepareFormat(track->format);
|
||||
if (new_target != target_format_) {
|
||||
// The new format is different to the old one. Wait for the sink to
|
||||
// drain before continuing.
|
||||
while (!xStreamBufferIsEmpty(sink_->stream())) {
|
||||
ESP_LOGI(kTag, "waiting for sink stream to drain...");
|
||||
// TODO(jacqueline): Get the sink drain ISR to notify us of this
|
||||
// via semaphore instead of busy-ish waiting.
|
||||
vTaskDelay(pdMS_TO_TICKS(10));
|
||||
}
|
||||
|
||||
sink_->Configure(new_target);
|
||||
// If the output is idle, then we can reconfigure it to the closest format
|
||||
// to our new source.
|
||||
// If the output *wasn't* idle, then we can't reconfigure without an
|
||||
// audible gap in playback. So instead, we simply keep the same target
|
||||
// format and begin resampling.
|
||||
if (xStreamBufferIsEmpty(sink_)) {
|
||||
target_format_ = output_->PrepareFormat(track->format);
|
||||
output_->Configure(target_format_);
|
||||
}
|
||||
target_format_ = new_target;
|
||||
}
|
||||
|
||||
samples_sunk_ = 0;
|
||||
if (xStreamBufferIsEmpty(sink_)) {
|
||||
samples_written_ = output_->samplesUsed();
|
||||
}
|
||||
|
||||
events::Audio().Dispatch(internal::StreamStarted{
|
||||
.track = track,
|
||||
.src_format = source_format_,
|
||||
.dst_format = target_format_,
|
||||
.sink_format = target_format_,
|
||||
.cue_at_sample = samples_written_,
|
||||
});
|
||||
}
|
||||
|
||||
auto SampleConverter::handleContinueStream(size_t samples_available) -> void {
|
||||
auto SampleProcessor::handleContinueStream(size_t samples_available) -> void {
|
||||
// Loop until we finish reading all the bytes indicated. There might be
|
||||
// leftovers from each iteration, and from this process as a whole,
|
||||
// depending on the resampling stage.
|
||||
@@ -182,7 +190,7 @@ auto SampleConverter::handleContinueStream(size_t samples_available) -> void {
|
||||
}
|
||||
}
|
||||
|
||||
auto SampleConverter::handleSamples(cpp::span<sample::Sample> input) -> size_t {
|
||||
auto SampleProcessor::handleSamples(std::span<sample::Sample> input) -> size_t {
|
||||
if (source_format_ == target_format_) {
|
||||
// The happiest possible case: the input format matches the output
|
||||
// format already.
|
||||
@@ -192,7 +200,7 @@ auto SampleConverter::handleSamples(cpp::span<sample::Sample> input) -> size_t {
|
||||
|
||||
size_t samples_used = 0;
|
||||
while (samples_used < input.size()) {
|
||||
cpp::span<sample::Sample> output_source;
|
||||
std::span<sample::Sample> output_source;
|
||||
if (source_format_.sample_rate != target_format_.sample_rate) {
|
||||
if (resampler_ == nullptr) {
|
||||
ESP_LOGI(kTag, "creating new resampler for %lu -> %lu",
|
||||
@@ -223,8 +231,8 @@ auto SampleConverter::handleSamples(cpp::span<sample::Sample> input) -> size_t {
|
||||
return samples_used;
|
||||
}
|
||||
|
||||
auto SampleConverter::handleEndStream() -> void {
|
||||
if (resampler_) {
|
||||
auto SampleProcessor::handleEndStream(bool clear_bufs) -> void {
|
||||
if (resampler_ && !clear_bufs) {
|
||||
size_t read, written;
|
||||
std::tie(read, written) = resampler_->Process({}, resampled_buffer_, true);
|
||||
|
||||
@@ -233,33 +241,31 @@ auto SampleConverter::handleEndStream() -> void {
|
||||
}
|
||||
}
|
||||
|
||||
// Send a final update to finish off this stream's samples.
|
||||
if (samples_sunk_ > 0) {
|
||||
events::Audio().Dispatch(internal::StreamUpdate{
|
||||
.samples_sunk = samples_sunk_,
|
||||
});
|
||||
samples_sunk_ = 0;
|
||||
if (clear_bufs) {
|
||||
assert(xStreamBufferReset(sink_));
|
||||
samples_written_ = output_->samplesUsed();
|
||||
}
|
||||
|
||||
// FIXME: This discards any leftover samples, but there probably shouldn't be
|
||||
// any leftover samples. Can this be an assert instead?
|
||||
leftover_bytes_ = 0;
|
||||
|
||||
events::Audio().Dispatch(internal::StreamEnded{});
|
||||
events::Audio().Dispatch(internal::StreamEnded{
|
||||
.cue_at_sample = samples_written_,
|
||||
});
|
||||
}
|
||||
|
||||
auto SampleConverter::sendToSink(cpp::span<sample::Sample> samples) -> void {
|
||||
// Update the number of samples sunk so far *before* actually sinking them,
|
||||
// since writing to the stream buffer will block when the buffer gets full.
|
||||
samples_sunk_ += samples.size();
|
||||
if (samples_sunk_ >=
|
||||
target_format_.sample_rate * target_format_.num_channels) {
|
||||
events::Audio().Dispatch(internal::StreamUpdate{
|
||||
.samples_sunk = samples_sunk_,
|
||||
});
|
||||
samples_sunk_ = 0;
|
||||
}
|
||||
auto SampleProcessor::sendToSink(std::span<sample::Sample> samples) -> void {
|
||||
auto data = std::as_bytes(samples);
|
||||
xStreamBufferSend(sink_, data.data(), data.size(), portMAX_DELAY);
|
||||
|
||||
xStreamBufferSend(sink_->stream(),
|
||||
reinterpret_cast<std::byte*>(samples.data()),
|
||||
samples.size_bytes(), portMAX_DELAY);
|
||||
uint32_t samples_before_overflow =
|
||||
std::numeric_limits<uint32_t>::max() - samples_written_;
|
||||
if (samples_before_overflow < samples.size()) {
|
||||
samples_written_ = samples.size() - samples_before_overflow;
|
||||
} else {
|
||||
samples_written_ += samples.size();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace audio
|
||||
@@ -10,11 +10,11 @@
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
|
||||
#include "audio_events.hpp"
|
||||
#include "audio_sink.hpp"
|
||||
#include "audio_source.hpp"
|
||||
#include "audio/audio_events.hpp"
|
||||
#include "audio/audio_sink.hpp"
|
||||
#include "audio/audio_source.hpp"
|
||||
#include "audio/resample.hpp"
|
||||
#include "codec.hpp"
|
||||
#include "resample.hpp"
|
||||
#include "sample.hpp"
|
||||
|
||||
namespace audio {
|
||||
@@ -25,49 +25,52 @@ namespace audio {
|
||||
* format of the current output device. The resulting samples are forwarded
|
||||
* to the output device's sink stream.
|
||||
*/
|
||||
class SampleConverter {
|
||||
class SampleProcessor {
|
||||
public:
|
||||
SampleConverter();
|
||||
~SampleConverter();
|
||||
SampleProcessor(StreamBufferHandle_t sink);
|
||||
~SampleProcessor();
|
||||
|
||||
auto SetOutput(std::shared_ptr<IAudioOutput>) -> void;
|
||||
|
||||
auto beginStream(std::shared_ptr<TrackInfo>) -> void;
|
||||
auto continueStream(cpp::span<sample::Sample>) -> void;
|
||||
auto endStream() -> void;
|
||||
auto continueStream(std::span<sample::Sample>) -> void;
|
||||
auto endStream(bool cancelled) -> void;
|
||||
|
||||
private:
|
||||
auto Main() -> void;
|
||||
|
||||
auto handleBeginStream(std::shared_ptr<TrackInfo>) -> void;
|
||||
auto handleContinueStream(size_t samples_available) -> void;
|
||||
auto handleEndStream() -> void;
|
||||
auto handleEndStream(bool cancel) -> void;
|
||||
|
||||
auto handleSamples(cpp::span<sample::Sample>) -> size_t;
|
||||
auto handleSamples(std::span<sample::Sample>) -> size_t;
|
||||
|
||||
auto sendToSink(cpp::span<sample::Sample>) -> void;
|
||||
auto sendToSink(std::span<sample::Sample>) -> void;
|
||||
|
||||
struct Args {
|
||||
std::shared_ptr<TrackInfo>* track;
|
||||
size_t samples_available;
|
||||
bool is_end_of_stream;
|
||||
bool clear_buffers;
|
||||
};
|
||||
QueueHandle_t commands_;
|
||||
|
||||
std::unique_ptr<Resampler> resampler_;
|
||||
|
||||
StreamBufferHandle_t source_;
|
||||
cpp::span<sample::Sample> input_buffer_;
|
||||
cpp::span<std::byte> input_buffer_as_bytes_;
|
||||
StreamBufferHandle_t sink_;
|
||||
|
||||
cpp::span<sample::Sample> resampled_buffer_;
|
||||
std::span<sample::Sample> input_buffer_;
|
||||
std::span<std::byte> input_buffer_as_bytes_;
|
||||
|
||||
std::shared_ptr<IAudioOutput> sink_;
|
||||
std::span<sample::Sample> resampled_buffer_;
|
||||
|
||||
std::shared_ptr<IAudioOutput> output_;
|
||||
IAudioOutput::Format source_format_;
|
||||
IAudioOutput::Format target_format_;
|
||||
size_t leftover_bytes_;
|
||||
|
||||
uint32_t samples_sunk_;
|
||||
uint32_t samples_written_;
|
||||
};
|
||||
|
||||
} // namespace audio
|
||||
@@ -4,7 +4,7 @@
|
||||
* SPDX-License-Identifier: GPL-3.0-only
|
||||
*/
|
||||
|
||||
#include "readahead_source.hpp"
|
||||
#include "audio/readahead_source.hpp"
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
@@ -14,10 +14,10 @@
|
||||
#include "esp_log.h"
|
||||
#include "ff.h"
|
||||
|
||||
#include "audio_source.hpp"
|
||||
#include "audio/audio_source.hpp"
|
||||
#include "codec.hpp"
|
||||
#include "drivers/spi.hpp"
|
||||
#include "freertos/portmacro.h"
|
||||
#include "spi.hpp"
|
||||
#include "tasks.hpp"
|
||||
#include "types.hpp"
|
||||
|
||||
@@ -41,7 +41,7 @@ ReadaheadSource::~ReadaheadSource() {
|
||||
vStreamBufferDeleteWithCaps(buffer_);
|
||||
}
|
||||
|
||||
auto ReadaheadSource::Read(cpp::span<std::byte> dest) -> ssize_t {
|
||||
auto ReadaheadSource::Read(std::span<std::byte> dest) -> ssize_t {
|
||||
size_t bytes_written = 0;
|
||||
// Fill the destination from our buffer, until either the buffer is drained
|
||||
// or the destination is full.
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user