// 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 #include #include #include #include "host/EngineHost.hpp" using sizzle::host::EngineHost; namespace { std::atomic 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 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 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(); } /** "\t" 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 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); }