0.42.0 (78): preset randomizer for five devices (Android and iOS)
A RANDOM button in the preset bar of the Synth, Morph Pad, Grain Cloud, Wavetable and Ambience Reverb editors: it rolls a playable patch and names it. The rules live in core/src/model/Randomizer.cpp and are shared, so a seed gives the same patch on both platforms. Android reaches it over JNI as preset text and imports it; iOS writes the Params into the slot. Neither touches the slot's own files — a loaded sample or an imported wavetable survives a roll, because the dice are for the settings, not for the sounds. Scattering values over the parameter ranges produces silence and the occasional scream, so the rules encode three things instead: something always sounds (a layer is always up, an envelope that decays to nothing gets a body, a filter is never shut, a reverb is never fully dry); levels share a budget and resonance, drive and saturation stay out of the range where a patch screams; and tunings come from consonant intervals rather than a uniform draw. core/tests/randomizer_test.cpp is the specification: it renders every device over hundreds of seeds through the real voice and fails on anything out of range, silent, clipping or non-finite. Every bound that looks arbitrary was put there by a seed it caught — a band-pass parked above the note (peak 0.010 against a 0.3 median), a Vel -> Cutoff mod slot closing the filter (0.41 -> 0.02), a choir-only Morph Pad through its own band-pass, and MORPH TO, which a roll now leaves alone because MORPH TIME carries a held note all the way onto a factory drone at a level the randomizer never chose (1.33 with every layer of its own at zero). --dump <TYPE> <seed> [key=value ...] re-renders one roll with overrides, which is how each of those was found. Also fixes the shared engine not compiling outside Apple's toolchain: a missing <cstdint> in core/include/sizzle/Model.hpp. No change to the apps, but the engine's tests and benchmark now build with GCC too — which is how the rules above were iterated while the Mac was unreachable. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5
parent
e78b2c1418
commit
0d6ed4694f
@@ -12,6 +12,7 @@
|
||||
#include <string>
|
||||
|
||||
#include "host/EngineHost.hpp"
|
||||
#include "sizzle/Randomizer.hpp"
|
||||
|
||||
using sizzle::host::EngineHost;
|
||||
|
||||
@@ -127,6 +128,25 @@ 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 preset randomizer (core/src/model/Randomizer.cpp). Pure functions of type and
|
||||
// seed — no engine involved — so unlike everything above they work before
|
||||
// nativeCreate and cannot be affected by what the audio thread is doing.
|
||||
JNI_FN(jboolean, nativeCanRandomize)(JNIEnv *env, jobject, jstring type) {
|
||||
return sizzle::model::canRandomize(str(env, type).c_str()) ? JNI_TRUE : JNI_FALSE;
|
||||
}
|
||||
|
||||
/** A random patch as preset text (io/PresetIo's format), for PresetIo.import. */
|
||||
JNI_FN(jstring, nativeRandomPreset)(JNIEnv *env, jobject, jstring type, jint seed) {
|
||||
const std::string text = sizzle::model::randomPresetText(str(env, type).c_str(), (uint32_t)seed);
|
||||
return env->NewStringUTF(text.c_str());
|
||||
}
|
||||
|
||||
/** The name that goes with that seed (PresetIo.import does not carry NAME through). */
|
||||
JNI_FN(jstring, nativeRandomName)(JNIEnv *env, jobject, jstring type, jint seed) {
|
||||
const std::string name = sizzle::model::randomName(str(env, type).c_str(), (uint32_t)seed);
|
||||
return env->NewStringUTF(name.c_str());
|
||||
}
|
||||
|
||||
/** 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();
|
||||
|
||||
@@ -27,6 +27,18 @@ object NativeEngine {
|
||||
|
||||
fun removeSample(id: String) { if (created) nativeRemoveSample(id) }
|
||||
|
||||
// ---- the preset randomizer (core/src/model/Randomizer.cpp) ----
|
||||
// Gated on [loaded], not [created]: these need no engine, only the library.
|
||||
|
||||
/** Does this device type have a randomizer? */
|
||||
fun canRandomize(type: String): Boolean = loaded && nativeCanRandomize(type)
|
||||
|
||||
/** A random patch for [type] as preset text, or "" — feed it to PresetIo.import. */
|
||||
fun randomPreset(type: String, seed: Int): String = if (loaded) nativeRandomPreset(type, seed) else ""
|
||||
|
||||
/** The name belonging to that same [seed]. */
|
||||
fun randomName(type: String, seed: Int): String = if (loaded) nativeRandomName(type, seed) else ""
|
||||
|
||||
external fun nativeCreate(sampleRate: Int)
|
||||
external fun nativeLoad(text: String)
|
||||
external fun nativeSync(text: String)
|
||||
@@ -62,4 +74,7 @@ object NativeEngine {
|
||||
external fun nativeExternalClockStart()
|
||||
external fun nativeExternalClockStop()
|
||||
external fun nativeRender(out: FloatArray, frames: Int)
|
||||
external fun nativeCanRandomize(type: String): Boolean
|
||||
external fun nativeRandomPreset(type: String, seed: Int): String
|
||||
external fun nativeRandomName(type: String, seed: Int): String
|
||||
}
|
||||
|
||||
@@ -1405,6 +1405,32 @@ class AppViewModel(
|
||||
return ok
|
||||
}
|
||||
|
||||
/** Does this device have a dice button? (The randomizer lives in core/.) */
|
||||
fun canRandomize(type: cc.tiredsysadmin.android.sizzle.model.ToolboxType): Boolean =
|
||||
cc.tiredsysadmin.android.sizzle.audio.NativeEngine.canRandomize(type.name)
|
||||
|
||||
/**
|
||||
* Roll a new patch into [slot]: a playable one, generated by core/'s randomizer
|
||||
* (the same rules and the same seeds as iOS). The slot's own files are kept —
|
||||
* a loaded sample or an imported wavetable is the user's, and a dice roll of the
|
||||
* *settings* must not throw it away — so the keys the device's parameter list
|
||||
* doesn't name (`sfSample`, `sfFile`, the Sampler's pads) are put back afterwards.
|
||||
*/
|
||||
fun randomizeSlot(slot: cc.tiredsysadmin.android.sizzle.model.ToolboxSlot): Boolean {
|
||||
val type = slot.type ?: return false
|
||||
val seed = java.util.concurrent.ThreadLocalRandom.current().nextInt()
|
||||
val text = cc.tiredsysadmin.android.sizzle.audio.NativeEngine.randomPreset(type.name, seed)
|
||||
if (text.isBlank()) return false
|
||||
val keep = slot.values.filterKeys { key -> type.params.none { it.key == key } }
|
||||
if (!cc.tiredsysadmin.android.sizzle.io.PresetIo.import(slot, text)) return false
|
||||
keep.forEach { (k, v) -> slot.set(k, v) }
|
||||
slot.name = cc.tiredsysadmin.android.sizzle.audio.NativeEngine.randomName(type.name, seed)
|
||||
.ifBlank { type.displayName }
|
||||
engine.rebuildFxChains()
|
||||
touched()
|
||||
return true
|
||||
}
|
||||
|
||||
/** Import a preset from picked bytes: a `.zip` bundle (Sampler) or plain text. */
|
||||
fun importPreset(slot: cc.tiredsysadmin.android.sizzle.model.ToolboxSlot, bytes: ByteArray): Boolean {
|
||||
val isZip = bytes.size >= 2 && bytes[0] == 'P'.code.toByte() && bytes[1] == 'K'.code.toByte()
|
||||
|
||||
@@ -49,8 +49,10 @@ import androidx.compose.runtime.rememberCoroutineScope
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.hapticfeedback.HapticFeedbackType
|
||||
import androidx.compose.ui.input.pointer.pointerInput
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.platform.LocalHapticFeedback
|
||||
import androidx.compose.ui.text.font.FontFamily
|
||||
import androidx.compose.ui.text.style.TextAlign
|
||||
import androidx.compose.ui.text.style.TextOverflow
|
||||
@@ -532,6 +534,7 @@ private fun MidiLearnPopup(vm: AppViewModel, slot: ToolboxSlot, onClose: () -> U
|
||||
private fun PresetBar(vm: AppViewModel, slot: ToolboxSlot) {
|
||||
val c = LocalRetro.current
|
||||
val context = LocalContext.current
|
||||
val haptics = LocalHapticFeedback.current
|
||||
@Suppress("UNUSED_VARIABLE") val rev = vm.revision // refresh the list after save/import
|
||||
val type = slot.type ?: return
|
||||
|
||||
@@ -589,6 +592,16 @@ private fun PresetBar(vm: AppViewModel, slot: ToolboxSlot) {
|
||||
RetroButton("DELETE", danger = true) { confirmDelete = current }
|
||||
Spacer(Modifier.width(20.dp))
|
||||
}
|
||||
// RANDOM only for the devices core/'s randomizer covers (the Synth, Morph
|
||||
// Pad, Grain Cloud, Wavetable and Ambience). It replaces the settings in
|
||||
// place, like loading a preset; the slot's own sample or table is kept.
|
||||
if (vm.canRandomize(type)) {
|
||||
RetroButton("RANDOM") {
|
||||
haptics.performHapticFeedback(HapticFeedbackType.LongPress)
|
||||
vm.randomizeSlot(slot)
|
||||
selected = null
|
||||
}
|
||||
}
|
||||
RetroButton("SAVE") { showSave = true }
|
||||
RetroButton("IMPORT") {
|
||||
// Accept text presets and (for the Sampler) zip bundles.
|
||||
|
||||
Reference in New Issue
Block a user