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:
Reactorcoremeltdown
2026-11-16 14:49:46 +01:00
co-authored by Claude Opus 5.5
parent 64e21e0bcc
commit 2bc5cb1cbb
107 changed files with 2921 additions and 1880 deletions
+19 -69
View File
@@ -3,11 +3,12 @@
// Native Oboe (AAudio) output backend for Sizzletracker.
//
// Opens a low-latency mono float Oboe output stream. On each real-time data
// callback it calls back into Kotlin (NativeAudioBridge.renderAudio) to fill a
// reused Java float[] from the shared AudioEngine.fillBlock, then copies that
// into Oboe's output buffer. This keeps ONE implementation of the synth/mixer
// (in Kotlin) while delivering audio through the lower-latency native path.
// Opens a low-latency stereo float Oboe output stream. Each real-time data
// callback renders straight from the shared C++ engine (core/, through the
// EngineHost that jni_engine.cpp owns): no JVM call on the audio thread, so no
// garbage-collector pause can glitch it.
#include "host/EngineHost.hpp"
#include <jni.h>
#include <android/log.h>
@@ -25,13 +26,11 @@
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
// The engine host (jni_engine.cpp); null until the app creates it.
sizzle::host::EngineHost *sizzleHost();
namespace {
JavaVM *g_vm = nullptr;
jobject g_callback = nullptr; // global ref to the NativeAudioBridge
jmethodID g_renderMethod = nullptr;
jfloatArray g_buffer = nullptr; // reused Java float[] of g_frames length
int g_frames = 0;
int32_t g_prevXRuns = 0; // last-seen underrun count, for adaptive sizing
int32_t g_sampleRate = 48000; // remembered so the stream can be reopened on disconnect
std::atomic<bool> g_wantRunning{false}; // false once nativeStop is called, so a
@@ -39,21 +38,9 @@ std::atomic<bool> g_wantRunning{false}; // false once nativeStop is called, so a
// the app deliberately shut down
// How glitch-resistant the output buffer starts (in device bursts). The buffer
// then grows adaptively (see onAudioReady) if underruns still occur. Profiling
// showed the JVM render is fast on average (<1 ms) but spikes to 3-7 ms when a GC
// pause freezes the callback thread; 4 bursts (~8 ms on a 96-frame device) absorbs
// those from the first note instead of glitching until adaptive growth catches up.
// then grows adaptively (see onAudioReady) if underruns still occur. (Sized when
// the render went through the JVM and its GC pauses; kept, the cost is latency only.)
constexpr int32_t kInitialBursts = 8;
constexpr int32_t kMaxBufferFrames = 8192; // cap for the reused Java buffer
// Fetch a JNIEnv for the (persistent) audio callback thread, attaching once.
JNIEnv *callbackEnv() {
JNIEnv *env = nullptr;
if (g_vm->GetEnv(reinterpret_cast<void **>(&env), JNI_VERSION_1_6) == JNI_EDETACHED) {
g_vm->AttachCurrentThread(&env, nullptr);
}
return env;
}
class SizzleCallback : public oboe::AudioStreamDataCallback {
public:
@@ -76,19 +63,8 @@ public:
stream->getBufferSizeInFrames() + stream->getFramesPerBurst());
}
JNIEnv *env = callbackEnv();
if (!env || !g_callback || !g_renderMethod || !g_buffer) {
for (int i = 0; i < numFrames * 2; ++i) out[i] = 0.0f; // stereo silence
return oboe::DataCallbackResult::Continue;
}
const int n = numFrames < g_frames ? numFrames : g_frames;
// Ask Kotlin to render n frames; fillBlock writes 2*n interleaved-stereo
// samples into the reused Java buffer ...
env->CallVoidMethod(g_callback, g_renderMethod, g_buffer, n);
// ... then copy them out to Oboe (2 samples/frame) and pad any remainder with
// silence (the Java buffer is sized to the stream capacity, so rare).
env->GetFloatArrayRegion(g_buffer, 0, n * 2, out);
for (int i = n * 2; i < numFrames * 2; ++i) out[i] = 0.0f;
if (sizzle::host::EngineHost *host = sizzleHost()) host->render(out, numFrames);
else for (int i = 0; i < numFrames * 2; ++i) out[i] = 0.0f; // stereo silence
// Adaptive buffer sizing (timing path): a callback that renders slower than
// its deadline will drain the buffer; a couple in a row means we're about to
@@ -174,7 +150,7 @@ public:
SizzleErrorCallback g_errorCallback;
// Opens, buffers and starts the low-latency output stream. Used both at initial
// start and to recover from a disconnect. Reuses the already-allocated g_buffer.
// start and to recover from a disconnect.
bool openAndStartStream() {
oboe::AudioStreamBuilder builder;
builder.setDirection(oboe::Direction::Output)
@@ -200,56 +176,30 @@ bool openAndStartStream() {
g_stream.reset();
return false;
}
LOGI("Oboe stream started (%d Hz, burst=%d, bufferSize=%d, capacity=%d, javaBuf=%d)",
LOGI("Oboe stream started (%d Hz, burst=%d, bufferSize=%d, capacity=%d)",
g_sampleRate, burst, g_stream->getBufferSizeInFrames(),
g_stream->getBufferCapacityInFrames(), g_frames);
g_stream->getBufferCapacityInFrames());
return true;
}
} // namespace
extern "C" JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void * /*reserved*/) {
g_vm = vm;
return JNI_VERSION_1_6;
}
extern "C" JNIEXPORT jboolean JNICALL
Java_cc_tiredsysadmin_android_sizzle_audio_NativeAudioBridge_nativeStart(
JNIEnv *env, jobject /*thiz*/, jobject callback, jint sampleRate, jint framesPerCallback) {
g_callback = env->NewGlobalRef(callback);
jclass cls = env->GetObjectClass(callback);
g_renderMethod = env->GetMethodID(cls, "renderAudio", "([FI)V");
JNIEnv * /*env*/, jobject /*thiz*/, jint sampleRate) {
g_prevXRuns = 0;
g_sampleRate = sampleRate;
g_wantRunning = true;
// Allocate the reused Java render buffer up front, sized to our maximum callback,
// so the stream can be transparently reopened on a disconnect without touching
// JNI from Oboe's (non-Java) error thread. The data callback only ever renders
// min(numFrames, g_frames) frames, and low-latency bursts are far smaller.
g_frames = kMaxBufferFrames;
if (g_frames < framesPerCallback) g_frames = framesPerCallback;
// Interleaved stereo: 2 samples per frame.
g_buffer = static_cast<jfloatArray>(env->NewGlobalRef(env->NewFloatArray(g_frames * 2)));
if (!openAndStartStream()) {
env->DeleteGlobalRef(g_buffer);
g_buffer = nullptr;
return JNI_FALSE;
}
return JNI_TRUE;
return openAndStartStream() ? JNI_TRUE : JNI_FALSE;
}
extern "C" JNIEXPORT void JNICALL
Java_cc_tiredsysadmin_android_sizzle_audio_NativeAudioBridge_nativeStop(
JNIEnv *env, jobject /*thiz*/) {
JNIEnv * /*env*/, jobject /*thiz*/) {
g_wantRunning = false; // stop the disconnect-recovery from reopening
if (g_stream) {
g_stream->stop();
g_stream->close();
g_stream.reset();
}
if (g_buffer) { env->DeleteGlobalRef(g_buffer); g_buffer = nullptr; }
if (g_callback) { env->DeleteGlobalRef(g_callback); g_callback = nullptr; }
g_renderMethod = nullptr;
}