Fork of Tangara with customizations
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tangara-fw/src/audio/audio_decoder.cpp

125 lines
3.3 KiB

#include "audio_decoder.hpp"
2 years ago
#include <string.h>
2 years ago
#include <cstddef>
#include <cstdint>
#include <memory>
2 years ago
#include "freertos/FreeRTOS.h"
#include "esp_heap_caps.h"
2 years ago
#include "freertos/message_buffer.h"
#include "freertos/portmacro.h"
2 years ago
#include "audio_element.hpp"
#include "chunk.hpp"
#include "fatfs_audio_input.hpp"
#include "stream_info.hpp"
namespace audio {
static const std::size_t kSamplesPerChunk = 256;
AudioDecoder::AudioDecoder()
: IAudioElement(),
stream_info_({}),
has_samples_to_send_(false),
needs_more_input_(true) {}
2 years ago
AudioDecoder::~AudioDecoder() {}
auto AudioDecoder::HasUnprocessedInput() -> bool {
return !needs_more_input_ || has_samples_to_send_;
}
auto AudioDecoder::ProcessStreamInfo(const StreamInfo& info)
-> cpp::result<void, AudioProcessingError> {
stream_info_ = info;
if (info.chunk_size) {
chunk_reader_.emplace(*info.chunk_size);
} else {
// TODO.
}
// Reuse the existing codec if we can. This will help with gapless playback,
// since we can potentially just continue to decode as we were before,
// without any setup overhead.
if (current_codec_->CanHandleFile(info.path.value_or(""))) {
current_codec_->ResetForNewStream();
return {};
}
auto result = codecs::CreateCodecForFile(*info.path);
if (result) {
current_codec_ = std::move(*result);
} else {
return cpp::fail(UNSUPPORTED_STREAM);
}
// TODO: defer until first header read, so we can give better info about
// sample rate, chunk size, etc.
auto downstream_info = StreamEvent::CreateStreamInfo(
input_events_, std::make_unique<StreamInfo>(info));
downstream_info->stream_info->bits_per_sample = 32;
downstream_info->stream_info->sample_rate = 48'000;
chunk_size_ = 128;
downstream_info->stream_info->chunk_size = chunk_size_;
SendOrBufferEvent(std::move(downstream_info));
return {};
}
auto AudioDecoder::ProcessChunk(const cpp::span<std::byte>& chunk)
-> cpp::result<size_t, AudioProcessingError> {
if (current_codec_ == nullptr || !chunk_reader_) {
// Should never happen, but fail explicitly anyway.
return cpp::fail(UNSUPPORTED_STREAM);
}
current_codec_->SetInput(chunk_reader_->HandleNewData(chunk));
return {};
}
auto AudioDecoder::Process() -> cpp::result<void, AudioProcessingError> {
if (has_samples_to_send_) {
// Writing samples is relatively quick (it's just a bunch of memcopy's), so
// do them all at once.
while (has_samples_to_send_ && !IsOverBuffered()) {
auto buffer = StreamEvent::CreateChunkData(input_events_, chunk_size_);
auto write_res =
current_codec_->WriteOutputSamples(buffer->chunk_data.bytes);
buffer->chunk_data.bytes =
buffer->chunk_data.bytes.first(write_res.first);
has_samples_to_send_ = !write_res.second;
if (!SendOrBufferEvent(std::move(buffer))) {
2 years ago
return {};
}
2 years ago
}
// We will process the next frame during the next call to this method.
return {};
2 years ago
}
if (!needs_more_input_) {
auto res = current_codec_->ProcessNextFrame();
if (res.has_error()) {
// todo
return {};
}
needs_more_input_ = res.value();
has_samples_to_send_ = true;
if (needs_more_input_) {
chunk_reader_->HandleLeftovers(current_codec_->GetInputPosition());
}
}
return {};
}
} // namespace audio