Initial commit: Sizzletracker Android music tracker
A retro, grid-based music tracker for Android (Kotlin + Jetpack Compose, single Activity) with four equally-capable input methods (touch, keyboard, gamepad, MIDI) and four tabs: tracker, mixer, toolbox, settings. Highlights: - Tracker: Canvas-drawn 4-track pattern grid over an 8-lane arrangement roll, with a glyph cache and draw-phase state reads so the playhead and edits redraw without per-frame recomposition. - Audio: sample-accurate sequencer feeding a shared AudioEngine, driven by either a Kotlin AudioTrack loop or native Oboe/AAudio via JNI (16 KB-aligned native libs). media3 MediaSession for lock-screen/headset transport. - Toolbox: 16 instrument/effect slots with a 2-octave audition keyboard; single-tap select, double-tap edit, long-press clear. - Note entry: long-press cell popups (piano keyboard / value steppers) plus keyboard/gamepad stepping that resumes from the last note/channel entered. Velocity capped at 0x7F, channel at 16. - Selection/clipboard (cut/copy/paste/delete) and .sng import/export compatible with the reference desktop tool. - A `profile` build type (non-debuggable, debug-signed) for realistic on-device performance testing. - Developer handover documentation under docs/. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
// 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.
|
||||
|
||||
#include <jni.h>
|
||||
#include <android/log.h>
|
||||
#include <oboe/Oboe.h>
|
||||
#include <memory>
|
||||
|
||||
#define LOG_TAG "SizzleNative"
|
||||
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
|
||||
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
|
||||
|
||||
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;
|
||||
|
||||
// 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:
|
||||
oboe::DataCallbackResult onAudioReady(oboe::AudioStream *stream, void *audioData,
|
||||
int32_t numFrames) override {
|
||||
auto *out = static_cast<float *>(audioData);
|
||||
JNIEnv *env = callbackEnv();
|
||||
if (!env || !g_callback || !g_renderMethod || !g_buffer) {
|
||||
for (int i = 0; i < numFrames; ++i) out[i] = 0.0f;
|
||||
return oboe::DataCallbackResult::Continue;
|
||||
}
|
||||
const int n = numFrames < g_frames ? numFrames : g_frames;
|
||||
// Ask Kotlin to render n frames into the reused Java buffer ...
|
||||
env->CallVoidMethod(g_callback, g_renderMethod, g_buffer, n);
|
||||
// ... then copy them out to Oboe (and pad any remainder with silence).
|
||||
env->GetFloatArrayRegion(g_buffer, 0, n, out);
|
||||
for (int i = n; i < numFrames; ++i) out[i] = 0.0f;
|
||||
return oboe::DataCallbackResult::Continue;
|
||||
}
|
||||
};
|
||||
|
||||
SizzleCallback g_dataCallback;
|
||||
std::shared_ptr<oboe::AudioStream> g_stream;
|
||||
|
||||
} // 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_com_reactorcoremeltdown_sizzletracker_audio_NativeAudioBridge_nativeStart(
|
||||
JNIEnv *env, jobject /*thiz*/, jobject callback, jint sampleRate, jint framesPerCallback) {
|
||||
g_frames = framesPerCallback;
|
||||
g_callback = env->NewGlobalRef(callback);
|
||||
jclass cls = env->GetObjectClass(callback);
|
||||
g_renderMethod = env->GetMethodID(cls, "renderAudio", "([FI)V");
|
||||
g_buffer = static_cast<jfloatArray>(env->NewGlobalRef(env->NewFloatArray(framesPerCallback)));
|
||||
|
||||
oboe::AudioStreamBuilder builder;
|
||||
builder.setDirection(oboe::Direction::Output)
|
||||
->setPerformanceMode(oboe::PerformanceMode::LowLatency)
|
||||
->setSharingMode(oboe::SharingMode::Shared)
|
||||
->setFormat(oboe::AudioFormat::Float)
|
||||
->setChannelCount(oboe::ChannelCount::Mono)
|
||||
->setSampleRate(sampleRate)
|
||||
->setFramesPerDataCallback(framesPerCallback)
|
||||
->setDataCallback(&g_dataCallback);
|
||||
|
||||
oboe::Result result = builder.openStream(g_stream);
|
||||
if (result != oboe::Result::OK) {
|
||||
LOGE("Failed to open Oboe stream: %s", oboe::convertToText(result));
|
||||
return JNI_FALSE;
|
||||
}
|
||||
result = g_stream->requestStart();
|
||||
if (result != oboe::Result::OK) {
|
||||
LOGE("Failed to start Oboe stream: %s", oboe::convertToText(result));
|
||||
g_stream->close();
|
||||
g_stream.reset();
|
||||
return JNI_FALSE;
|
||||
}
|
||||
LOGI("Oboe stream started (%d Hz, %d frames/callback)", sampleRate, framesPerCallback);
|
||||
return JNI_TRUE;
|
||||
}
|
||||
|
||||
extern "C" JNIEXPORT void JNICALL
|
||||
Java_com_reactorcoremeltdown_sizzletracker_audio_NativeAudioBridge_nativeStop(
|
||||
JNIEnv *env, jobject /*thiz*/) {
|
||||
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