iOS 0.6.0: milestone 5 (recording, MIDI, input bindings, themes, bundles, backups)
- Master recording (MasterSink ring + writer thread) and the sample recorder - CoreMIDI in/out: sound module, clock send/follow, virtual "Sizzle" ports; platform-free parser/module/clock in core (midi_test) - Hardware keyboard and gamepads via GameController; rebindable keyboard, gamepad and MIDI CC actions; per-parameter MIDI learn - Colour themes (.szt), project bundles (.szgz), backup & restore; ZIP in core with zlib (zip_test against jar and unzip) - Persistent app settings; audio pauses in the background unless sound module Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
435305e42a
commit
64e21e0bcc
@@ -0,0 +1,166 @@
|
||||
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#import "SizzleSession+Record.h"
|
||||
#import "SizzleSession+Internal.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <cstdio>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
using namespace sizzle;
|
||||
|
||||
namespace {
|
||||
|
||||
/// MasterRecorder.kt: a single-producer / single-consumer ring of interleaved
|
||||
/// stereo floats (about two seconds), drained by a writer thread into a 16-bit
|
||||
/// stereo WAV whose header sizes are patched at the end.
|
||||
class MasterRecorder final : public engine::MasterSink {
|
||||
public:
|
||||
MasterRecorder(int sampleRate, NSURL *dir) : sr(sampleRate), ring((size_t)std::max(2, sampleRate * 4)), dir(dir) {}
|
||||
~MasterRecorder() override { capturing.store(false); if (writer.joinable()) writer.join(); }
|
||||
|
||||
bool isActive() const override { return active.load(); }
|
||||
|
||||
/// Main thread (Play) or the MIDI thread (an external clock Start).
|
||||
bool begin() override {
|
||||
if (active.load()) return false;
|
||||
if (writer.joinable()) writer.join(); // the previous take's thread has finished (active is false)
|
||||
writePos.store(0); readPos.store(0);
|
||||
active.store(true);
|
||||
capturing.store(true);
|
||||
NSDate *start = [NSDate date];
|
||||
NSURL *temp = [dir URLByAppendingPathComponent:@".take.wav"];
|
||||
writer = std::thread([this, temp, start] {
|
||||
const bool ok = writeLoop(temp.fileSystemRepresentation);
|
||||
NSURL *done = ok ? finish(temp, start) : nil;
|
||||
active.store(false);
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
if (done) lastTake = done;
|
||||
[[NSNotificationCenter defaultCenter] postNotificationName:SizzleSessionDidChange object:nil];
|
||||
});
|
||||
});
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
[[NSNotificationCenter defaultCenter] postNotificationName:SizzleSessionDidChange object:nil];
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
/// Audio thread: no allocation, no locks; drops the frame if the writer starved.
|
||||
void write(float left, float right) override {
|
||||
if (!capturing.load(std::memory_order_relaxed)) return;
|
||||
const uint64_t w = writePos.load(std::memory_order_relaxed);
|
||||
if (w - readPos.load(std::memory_order_acquire) > ring.size() - 2) return;
|
||||
ring[w % ring.size()] = left;
|
||||
ring[(w + 1) % ring.size()] = right;
|
||||
writePos.store(w + 2, std::memory_order_release);
|
||||
}
|
||||
|
||||
void endCapture() override { capturing.store(false); }
|
||||
|
||||
NSURL *lastTake = nil; // main thread
|
||||
|
||||
private:
|
||||
bool writeLoop(const char *path) {
|
||||
FILE *f = std::fopen(path, "wb");
|
||||
if (!f) { capturing.store(false); return false; }
|
||||
writeHeader(f, 0);
|
||||
uint64_t samples = 0;
|
||||
constexpr size_t chunk = 4096;
|
||||
int16_t out[chunk];
|
||||
bool ok = true;
|
||||
for (;;) {
|
||||
const uint64_t w = writePos.load(std::memory_order_acquire);
|
||||
const uint64_t r = readPos.load(std::memory_order_relaxed);
|
||||
if (r < w) {
|
||||
const size_t n = (size_t)std::min<uint64_t>(w - r, chunk);
|
||||
for (size_t i = 0; i < n; ++i) {
|
||||
const float s = std::clamp(ring[(r + i) % ring.size()], -1.f, 1.f);
|
||||
out[i] = (int16_t)(int)(s * 32767.f); // little-endian host
|
||||
}
|
||||
if (std::fwrite(out, 2, n, f) != n) ok = false;
|
||||
samples += n;
|
||||
readPos.store(r + n, std::memory_order_release);
|
||||
} else if (!capturing.load()) {
|
||||
break;
|
||||
} else {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(5));
|
||||
}
|
||||
}
|
||||
writeHeader(f, samples);
|
||||
ok = std::fclose(f) == 0 && ok;
|
||||
return ok;
|
||||
}
|
||||
|
||||
void writeHeader(FILE *f, uint64_t sampleCount) {
|
||||
const uint32_t data = (uint32_t)(sampleCount * 2);
|
||||
auto u32 = [f](uint32_t v) { uint8_t b[4] = {(uint8_t)v, (uint8_t)(v >> 8), (uint8_t)(v >> 16), (uint8_t)(v >> 24)}; std::fwrite(b, 1, 4, f); };
|
||||
auto u16 = [f](uint16_t v) { uint8_t b[2] = {(uint8_t)v, (uint8_t)(v >> 8)}; std::fwrite(b, 1, 2, f); };
|
||||
std::fseek(f, 0, SEEK_SET);
|
||||
std::fwrite("RIFF", 1, 4, f); u32(36 + data); std::fwrite("WAVE", 1, 4, f);
|
||||
std::fwrite("fmt ", 1, 4, f); u32(16); u16(1); u16(2); u32((uint32_t)sr); u32((uint32_t)sr * 4); u16(4); u16(16);
|
||||
std::fwrite("data", 1, 4, f); u32(data);
|
||||
std::fseek(f, 0, SEEK_END);
|
||||
}
|
||||
|
||||
/// Give the finished take its date-time name (Android: sizzle-yyyyMMdd-HHmmss.wav).
|
||||
NSURL *finish(NSURL *temp, NSDate *start) {
|
||||
NSDateFormatter *df = [NSDateFormatter new];
|
||||
df.locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
|
||||
df.dateFormat = @"yyyyMMdd-HHmmss";
|
||||
NSString *base = [@"sizzle-" stringByAppendingString:[df stringFromDate:start]];
|
||||
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]];
|
||||
return [NSFileManager.defaultManager moveItemAtURL:temp toURL:dst error:nil] ? dst : nil;
|
||||
}
|
||||
|
||||
const int sr;
|
||||
std::vector<float> ring;
|
||||
NSURL *dir;
|
||||
std::atomic<uint64_t> writePos{0}, readPos{0};
|
||||
std::atomic<bool> capturing{false}, active{false};
|
||||
std::thread writer;
|
||||
};
|
||||
|
||||
MasterRecorder *gRecorder = nullptr; // lives as long as the app (the session never goes away)
|
||||
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(), dir);
|
||||
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<NSInteger>(bars, 0, 4); [self rec_apply]; }
|
||||
|
||||
- (BOOL)masterRecording { return gRecorder && gRecorder->isActive(); }
|
||||
- (nullable NSURL *)lastRecordingURL { return gRecorder ? gRecorder->lastTake : nil; }
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user