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
@@ -1,15 +1,32 @@
|
||||
# Native build for the Oboe (AAudio) low-latency output backend.
|
||||
# Native build: the shared C++ engine (../../../../core, the same code the iOS app
|
||||
# runs), its JNI glue (jni_engine.cpp) and the Oboe (AAudio) low-latency output.
|
||||
cmake_minimum_required(VERSION 3.22.1)
|
||||
project(sizzle_native)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
# Oboe ships as a prefab package inside the com.google.oboe:oboe AAR; AGP exposes
|
||||
# it here when `buildFeatures { prefab = true }` is set in build.gradle.kts.
|
||||
find_package(oboe REQUIRED CONFIG)
|
||||
|
||||
add_library(sizzle_native SHARED native_audio.cpp)
|
||||
# The engine as an object library, not a static one: the devices are found through
|
||||
# weak placeholder makers (core/src/fx/Registry.cpp), so every object must be linked
|
||||
# in or the placeholders would win. Optimised even in debug builds (the DSP must
|
||||
# keep up in real time).
|
||||
set(CORE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../../../core)
|
||||
file(GLOB CORE_SRC CONFIGURE_DEPENDS ${CORE_DIR}/src/*.cpp ${CORE_DIR}/src/*/*.cpp)
|
||||
add_library(sizzle_core OBJECT ${CORE_SRC})
|
||||
target_include_directories(sizzle_core PUBLIC ${CORE_DIR}/include ${CORE_DIR}/src ${CORE_DIR}/src/fx)
|
||||
target_compile_options(sizzle_core PRIVATE -O2 -Wno-unused-parameter)
|
||||
|
||||
add_library(sizzle_native SHARED native_audio.cpp jni_engine.cpp $<TARGET_OBJECTS:sizzle_core>)
|
||||
target_include_directories(sizzle_native PRIVATE ${CORE_DIR}/include ${CORE_DIR}/src ${CORE_DIR}/src/fx)
|
||||
target_compile_options(sizzle_native PRIVATE -O2)
|
||||
|
||||
target_link_libraries(sizzle_native
|
||||
oboe::oboe
|
||||
z
|
||||
log)
|
||||
|
||||
# Align ELF load segments to 16 KB so the library works on Android 15+ devices
|
||||
|
||||
@@ -0,0 +1,137 @@
|
||||
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
// JNI for audio/NativeEngine.kt: the Android app drives the shared C++ engine
|
||||
// (core/src/host/EngineHost) through these. The Kotlin model stays the UI's
|
||||
// state; NativeEngine mirrors it here as project text (nativeSync).
|
||||
|
||||
#include <jni.h>
|
||||
|
||||
#include <atomic>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
|
||||
#include "host/EngineHost.hpp"
|
||||
|
||||
using sizzle::host::EngineHost;
|
||||
|
||||
namespace {
|
||||
|
||||
std::atomic<EngineHost *> g_host{nullptr};
|
||||
|
||||
// A finished master take, until Kotlin collects it (nativeTakeFinished).
|
||||
std::mutex g_takeLock;
|
||||
std::string g_takePath;
|
||||
int64_t g_takeStart = 0;
|
||||
bool g_takeReady = false;
|
||||
|
||||
std::string str(JNIEnv *env, jstring s) {
|
||||
if (!s) return {};
|
||||
const char *c = env->GetStringUTFChars(s, nullptr);
|
||||
std::string out = c ? c : "";
|
||||
if (c) env->ReleaseStringUTFChars(s, c);
|
||||
return out;
|
||||
}
|
||||
|
||||
EngineHost *host() { return g_host.load(std::memory_order_acquire); }
|
||||
|
||||
} // namespace
|
||||
|
||||
sizzle::host::EngineHost *sizzleHost() { return host(); }
|
||||
|
||||
#define JNI_FN(ret, name) extern "C" JNIEXPORT ret JNICALL Java_cc_tiredsysadmin_android_sizzle_audio_NativeEngine_##name
|
||||
#define H if (EngineHost *h = host()) h
|
||||
|
||||
JNI_FN(void, nativeCreate)(JNIEnv *, jobject, jint sampleRate) {
|
||||
if (host()) return;
|
||||
auto *h = new EngineHost(sampleRate); // lives as long as the process
|
||||
h->recorder().onFinished = [](const std::string &path, int64_t start) {
|
||||
std::lock_guard<std::mutex> g(g_takeLock);
|
||||
g_takePath = path;
|
||||
g_takeStart = start;
|
||||
g_takeReady = true;
|
||||
};
|
||||
g_host.store(h, std::memory_order_release);
|
||||
}
|
||||
|
||||
JNI_FN(void, nativeLoad)(JNIEnv *env, jobject, jstring text) { H->loadProjectText(str(env, text)); }
|
||||
JNI_FN(void, nativeSync)(JNIEnv *env, jobject, jstring text) { H->syncProjectText(str(env, text)); }
|
||||
JNI_FN(void, nativeRebuildFxChains)(JNIEnv *, jobject) { H->rebuildFxChains(); }
|
||||
JNI_FN(void, nativeRebuildArrangement)(JNIEnv *, jobject) { H->rebuildArrangement(); }
|
||||
JNI_FN(void, nativeRefreshLoopPlaylist)(JNIEnv *, jobject) { H->refreshLoopPlaylist(); }
|
||||
|
||||
JNI_FN(void, nativePlay)(JNIEnv *, jobject, jint beat) { H->play(beat); }
|
||||
JNI_FN(void, nativePause)(JNIEnv *, jobject) { H->pause(); }
|
||||
JNI_FN(void, nativeStop)(JNIEnv *, jobject) { H->stop(); }
|
||||
JNI_FN(void, nativePanic)(JNIEnv *, jobject) { H->panic(); }
|
||||
|
||||
/** Playing in bit 62, the line in bits 31..61, the beat in bits 0..30. */
|
||||
JNI_FN(jlong, nativeTransport)(JNIEnv *, jobject) {
|
||||
EngineHost *h = host();
|
||||
if (!h) return 0;
|
||||
const auto t = h->transport();
|
||||
return ((jlong)(t.isPlaying ? 1 : 0) << 62) | ((jlong)(t.currentLine & 0x7FFFFFFF) << 31) | (jlong)(t.currentBeat & 0x7FFFFFFF);
|
||||
}
|
||||
|
||||
JNI_FN(void, nativeLiveNoteOn)(JNIEnv *, jobject, jint pitch, jint vel) { H->liveNoteOn(pitch, vel); }
|
||||
JNI_FN(void, nativeLiveNoteOff)(JNIEnv *, jobject, jint pitch) { H->liveNoteOff(pitch); }
|
||||
JNI_FN(void, nativeAuditionSlotOn)(JNIEnv *, jobject, jint slot, jint midi) { H->auditionSlotOn(slot, midi); }
|
||||
JNI_FN(void, nativeAuditionSlotOff)(JNIEnv *, jobject, jint midi) { H->auditionSlotOff(midi); }
|
||||
JNI_FN(void, nativeBusNoteOn)(JNIEnv *, jobject, jint ch, jint midi, jint vel) { H->busNoteOn(ch, midi, vel); }
|
||||
JNI_FN(void, nativeBusNoteOff)(JNIEnv *, jobject, jint ch, jint midi) { H->busNoteOff(ch, midi); }
|
||||
JNI_FN(jstring, nativeDescribeBusTargets)(JNIEnv *env, jobject, jint ch, jint midi) {
|
||||
EngineHost *h = host();
|
||||
return env->NewStringUTF(h ? h->describeBusTargets(ch, midi).c_str() : "");
|
||||
}
|
||||
|
||||
JNI_FN(jfloat, nativeChannelMeter)(JNIEnv *, jobject, jint ch) { EngineHost *h = host(); return h ? h->channelMeter(ch) : 0.f; }
|
||||
JNI_FN(jboolean, nativeChannelClipped)(JNIEnv *, jobject, jint ch) { EngineHost *h = host(); return h && h->channelClipped(ch); }
|
||||
JNI_FN(jint, nativeEffectScope)(JNIEnv *env, jobject, jint slot, jfloatArray out) {
|
||||
EngineHost *h = host();
|
||||
if (!h || !out) return -1;
|
||||
const jsize cap = env->GetArrayLength(out);
|
||||
jfloat *buf = env->GetFloatArrayElements(out, nullptr);
|
||||
const int n = h->effectScope(slot, buf, cap);
|
||||
env->ReleaseFloatArrayElements(out, buf, 0);
|
||||
return n;
|
||||
}
|
||||
JNI_FN(jfloat, nativeEffectPlayhead)(JNIEnv *, jobject, jint slot) { EngineHost *h = host(); return h ? h->effectPlayhead(slot) : -1.f; }
|
||||
|
||||
JNI_FN(void, nativeSetMidiOutEnabled)(JNIEnv *, jobject, jboolean on) { H->setMidiOutEnabled(on); }
|
||||
JNI_FN(jint, nativePollMidiOut)(JNIEnv *, jobject) { EngineHost *h = host(); return h ? h->pollMidiOut() : -1; }
|
||||
|
||||
JNI_FN(void, nativePutSample)(JNIEnv *env, jobject, jstring id, jfloatArray data, jint rate, jint loopStart, jint loopEnd) {
|
||||
EngineHost *h = host();
|
||||
if (!h || !data) return;
|
||||
const jsize n = env->GetArrayLength(data);
|
||||
std::vector<float> v((size_t)n);
|
||||
env->GetFloatArrayRegion(data, 0, n, v.data());
|
||||
h->putSample(str(env, id), sizzle::Sample(std::move(v), rate, loopStart, loopEnd));
|
||||
}
|
||||
JNI_FN(void, nativeRemoveSample)(JNIEnv *env, jobject, jstring id) { H->removeSample(str(env, id)); }
|
||||
|
||||
JNI_FN(void, nativeSetRecordPath)(JNIEnv *env, jobject, jstring path) { H->recorder().setPath(str(env, path)); }
|
||||
JNI_FN(void, nativeSetRecordingArm)(JNIEnv *, jobject, jboolean armed, jint tail) { H->setRecordingArm(armed, tail); }
|
||||
JNI_FN(void, nativeSetRecordTailBars)(JNIEnv *, jobject, jint tail) { H->setRecordTailBars(tail); }
|
||||
JNI_FN(jboolean, nativeRecordingActive)(JNIEnv *, jobject) { EngineHost *h = host(); return h && h->recorder().isActive(); }
|
||||
/** "<start millis>\t<path>" of a take that finished since the last call (path empty
|
||||
* when writing failed), or null. */
|
||||
JNI_FN(jstring, nativeTakeFinished)(JNIEnv *env, jobject) {
|
||||
std::lock_guard<std::mutex> g(g_takeLock);
|
||||
if (!g_takeReady) return nullptr;
|
||||
g_takeReady = false;
|
||||
return env->NewStringUTF((std::to_string(g_takeStart) + "\t" + g_takePath).c_str());
|
||||
}
|
||||
/** Shutting down: finish a take being captured so its file is complete. */
|
||||
JNI_FN(void, nativeEndCapture)(JNIEnv *, jobject) { H->recorder().endCapture(); }
|
||||
JNI_FN(void, nativeExternalClockStart)(JNIEnv *, jobject) { H->externalClockStart(); }
|
||||
JNI_FN(void, nativeExternalClockStop)(JNIEnv *, jobject) { H->externalClockStop(); }
|
||||
|
||||
/** The AudioTrack fallback's render: [frames] interleaved stereo frames into [out]. */
|
||||
JNI_FN(void, nativeRender)(JNIEnv *env, jobject, jfloatArray out, jint frames) {
|
||||
EngineHost *h = host();
|
||||
jfloat *buf = env->GetFloatArrayElements(out, nullptr);
|
||||
if (h) h->render(buf, frames);
|
||||
else for (jint i = 0; i < frames * 2; ++i) buf[i] = 0.f;
|
||||
env->ReleaseFloatArrayElements(out, buf, 0);
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user