// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown // SPDX-License-Identifier: GPL-3.0-or-later // The engine's MasterSink as a WAV writer (MasterRecorder.kt): the audio thread // writes interleaved stereo floats into a single-producer/single-consumer ring // (about two seconds), a writer thread drains it into a 16-bit stereo WAV at // [path] and patches the header sizes at the end. No allocation, locks or I/O on // the audio thread. Used by both apps' engine hosts. #pragma once #include #include #include #include #include #include #include #include "sizzle/Engine.hpp" namespace sizzle::host { class MasterRecorder final : public engine::MasterSink { public: explicit MasterRecorder(int sampleRate); ~MasterRecorder() override; /** Where the next take is written (set before arming). */ void setPath(std::string path) { path_ = std::move(path); } /** Called when a take starts capturing (the thread that started it: main or MIDI). */ std::function onStarted; /** Called on the writer thread when the file is finished: its path, or "" when * writing failed, and the take's start in milliseconds since the epoch. */ std::function onFinished; bool isActive() const override { return active_.load(); } bool begin() override; void write(float left, float right) override; void endCapture() override { capturing_.store(false); } private: bool writeLoop(const std::string &path); void writeHeader(FILE *f, uint64_t sampleCount); const int sr_; std::vector ring_; std::string path_; std::atomic writePos_{0}, readPos_{0}; std::atomic capturing_{false}, active_{false}; std::thread writer_; }; } // namespace sizzle::host