From b242ba998699208c87dc066158964de0866b61e2 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Tue, 7 May 2024 14:19:19 +1000 Subject: Improve decoder's interface to accept streams --- src/tangara/audio/processor.hpp | 73 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 src/tangara/audio/processor.hpp (limited to 'src/tangara/audio/processor.hpp') diff --git a/src/tangara/audio/processor.hpp b/src/tangara/audio/processor.hpp new file mode 100644 index 00000000..8f197d97 --- /dev/null +++ b/src/tangara/audio/processor.hpp @@ -0,0 +1,73 @@ +/* + * Copyright 2023 jacqueline + * + * SPDX-License-Identifier: GPL-3.0-only + */ + +#pragma once + +#include +#include +#include + +#include "audio/audio_events.hpp" +#include "audio/audio_sink.hpp" +#include "audio/audio_source.hpp" +#include "audio/resample.hpp" +#include "codec.hpp" +#include "sample.hpp" + +namespace audio { + +/* + * Handle to a persistent task that converts samples between formats (sample + * rate, channels, bits per sample), in order to put samples in the preferred + * format of the current output device. The resulting samples are forwarded + * to the output device's sink stream. + */ +class SampleProcessor { + public: + SampleProcessor(); + ~SampleProcessor(); + + auto SetOutput(std::shared_ptr) -> void; + + auto beginStream(std::shared_ptr) -> void; + auto continueStream(std::span) -> void; + auto endStream() -> void; + + private: + auto Main() -> void; + + auto handleBeginStream(std::shared_ptr) -> void; + auto handleContinueStream(size_t samples_available) -> void; + auto handleEndStream() -> void; + + auto handleSamples(std::span) -> size_t; + + auto sendToSink(std::span) -> void; + + struct Args { + std::shared_ptr* track; + size_t samples_available; + bool is_end_of_stream; + }; + QueueHandle_t commands_; + + std::unique_ptr resampler_; + + StreamBufferHandle_t source_; + std::span input_buffer_; + std::span input_buffer_as_bytes_; + + std::span resampled_buffer_; + + std::shared_ptr sink_; + IAudioOutput::Format source_format_; + IAudioOutput::Format target_format_; + size_t leftover_bytes_; + + uint32_t samples_sunk_; +}; + +} // namespace audio -- cgit v1.2.3