#pragma once #include #include #include #include #include #include #include #include namespace fluidsim::port { class SimulationRingBuffer { public: explicit SimulationRingBuffer(std::size_t capacity); std::size_t available_samples() const; std::size_t free_space() const; bool write(float left, float right, std::stop_token stop = {}); bool wait_for_space(std::size_t target_fill, std::stop_token stop); void reset_consumer(); // Only while both producer and audio consumer are stopped. void discard(); std::size_t read_interpolated(std::span left, std::span right, double speed); private: std::vector left_, right_; std::size_t mask_{}; std::atomic write_head_{}; std::atomic read_head_{}; double read_position_{}; bool consumer_initialized_{}; mutable std::mutex wait_mutex_; std::condition_variable space_available_; }; } // namespace fluidsim::port