/* * Copyright 2023 jacqueline * * SPDX-License-Identifier: GPL-3.0-only */ #pragma once #include #include #include #include "audio_events.hpp" #include "audio_sink.hpp" #include "audio_source.hpp" #include "codec.hpp" #include "resample.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 SampleConverter { public: SampleConverter(); ~SampleConverter(); 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