// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown // SPDX-License-Identifier: GPL-3.0-or-later #import "SizzleSession+Record.h" #import "SizzleSession+Internal.h" #include #include using namespace sizzle; #include "host/MasterRecorder.hpp" namespace { using sizzle::host::MasterRecorder; /// Android's take name: sizzle-yyyyMMdd-HHmmss.wav (-2, -3, ... if taken). NSURL *finishTake(NSURL *dir, const std::string &path, int64_t startMillis) { NSDateFormatter *df = [NSDateFormatter new]; df.locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"]; df.dateFormat = @"yyyyMMdd-HHmmss"; NSString *base = [@"sizzle-" stringByAppendingString:[df stringFromDate:[NSDate dateWithTimeIntervalSince1970:startMillis / 1000.0]]]; NSURL *dst = [dir URLByAppendingPathComponent:[base stringByAppendingPathExtension:@"wav"]]; for (int i = 2; [NSFileManager.defaultManager fileExistsAtPath:dst.path]; ++i) dst = [dir URLByAppendingPathComponent:[NSString stringWithFormat:@"%@-%d.wav", base, i]]; NSURL *src = [NSURL fileURLWithPath:@(path.c_str())]; return [NSFileManager.defaultManager moveItemAtURL:src toURL:dst error:nil] ? dst : nil; } MasterRecorder *gRecorder = nullptr; // lives as long as the app (the session never goes away) NSURL *gLastTake = nil; bool gArmed = false; NSInteger gTail = 2; } // namespace @implementation SizzleSession (Record) - (NSURL *)recordingsDirectory { NSURL *docs = [NSFileManager.defaultManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask].firstObject; NSURL *dir = [docs URLByAppendingPathComponent:@"Recordings"]; [NSFileManager.defaultManager createDirectoryAtURL:dir withIntermediateDirectories:YES attributes:nil error:nil]; return dir; } - (void)rec_apply { NSURL *dir = self.recordingsDirectory; const bool armed = gArmed; const int tail = (int)gTail; [self edit:^(model::Project &, engine::Engine &e) { if (!gRecorder) { gRecorder = new MasterRecorder(e.sampleRate()); gRecorder->setPath([dir URLByAppendingPathComponent:@".take.wav"].path.UTF8String); gRecorder->onStarted = [] { dispatch_async(dispatch_get_main_queue(), ^{ [[NSNotificationCenter defaultCenter] postNotificationName:SizzleSessionDidChange object:nil]; }); }; gRecorder->onFinished = [dir](const std::string &path, int64_t start) { NSURL *done = path.empty() ? nil : finishTake(dir, path, start); dispatch_async(dispatch_get_main_queue(), ^{ if (done) gLastTake = done; [[NSNotificationCenter defaultCenter] postNotificationName:SizzleSessionDidChange object:nil]; }); }; e.setMasterSink(gRecorder); } e.setRecordingArm(armed, tail); }]; } - (BOOL)recordArmed { return gArmed; } - (void)setRecordArmed:(BOOL)armed { gArmed = armed; [self rec_apply]; } - (NSInteger)recordTailBars { return gTail; } - (void)setRecordTailBars:(NSInteger)bars { gTail = std::clamp(bars, 0, 4); [self rec_apply]; } - (BOOL)masterRecording { return gRecorder && gRecorder->isActive(); } - (nullable NSURL *)lastRecordingURL { return gLastTake; } @end