0.37.0 (71): one engine, one version for Android and iOS
- Three source trees: core/ (shared C++ engine, moved from ios/core), app/ (Android UI), ios/app/ (iOS UI) - Android plays through core/ via JNI (EngineHost, jni_engine.cpp): the Oboe callback renders natively; the Kotlin model is mirrored as project text and applied as a diff, so playback continues through edits - The Kotlin engine moves to the unit tests as ReferenceEngine (golden reference) - Shared MasterRecorder in core/src/host, used by both apps; host_test - versions.properties: one versionName/versionCode for both platforms, continuing Android's numbering (iOS 0.6.0/11 -> 0.37.0/71) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
64e21e0bcc
commit
2bc5cb1cbb
@@ -5,128 +5,31 @@
|
||||
#import "SizzleSession+Internal.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <cstdio>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
using namespace sizzle;
|
||||
|
||||
#include "host/MasterRecorder.hpp"
|
||||
|
||||
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(); }
|
||||
using sizzle::host::MasterRecorder;
|
||||
|
||||
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;
|
||||
};
|
||||
/// 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;
|
||||
|
||||
@@ -147,7 +50,20 @@ NSInteger gTail = 2;
|
||||
const int tail = (int)gTail;
|
||||
[self edit:^(model::Project &, engine::Engine &e) {
|
||||
if (!gRecorder) {
|
||||
gRecorder = new MasterRecorder(e.sampleRate(), dir);
|
||||
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);
|
||||
@@ -161,6 +77,6 @@ NSInteger gTail = 2;
|
||||
- (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; }
|
||||
- (nullable NSURL *)lastRecordingURL { return gLastTake; }
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user